DBA Data[Home] [Help]

PACKAGE BODY: APPS.CSD_REPAIR_ESTIMATE_PVT

Source


1 Package BODY Csd_Repair_Estimate_Pvt AS
2     /* $Header: csdvestb.pls 120.18.12020000.2 2013/03/27 19:43:27 swai ship $ */
3 
4     -- ---------------------------------------------------------
5     -- Define global variables
6     -- ---------------------------------------------------------
7 
8     G_PKG_NAME  CONSTANT VARCHAR2(30) := 'CSD_REPAIR_ESTIMATE_PVT';
9     G_FILE_NAME CONSTANT VARCHAR2(12) := 'csdvestb.pls';
10     g_debug NUMBER := Csd_Gen_Utility_Pvt.g_debug_level;
11     ----Begin change for 3931317, wrpper aPI forward port
12 
13     C_EST_STATUS_ACCEPTED CONSTANT VARCHAR2(30) := 'ACCEPTED';
14     C_EST_STATUS_REJECTED CONSTANT VARCHAR2(30) := 'REJECTED';
15     C_EST_STATUS_NEW      CONSTANT VARCHAR2(30) := 'NEW';
16     C_REP_STATUS_APPROVED CONSTANT VARCHAR2(30) := 'A';
17     C_REP_STATUS_REJECTED CONSTANT VARCHAR2(30) := 'R';
18     G_DEBUG_LEVEL         CONSTANT NUMBER := TO_NUMBER(NVL(Fnd_Profile.value('CSD_DEBUG_LEVEL'),
19                                                            '0'));
20     ----End change for 3931317, wrpper aPI forward port
21 
22     -- swai: Bug 14804275 FP of bug 14752421 - begin change for create estimate from WIP
23     -- The following constants represent inventory transaction types.
24     -- Of the many INV txn types only the following ones are used in the package.
25     G_MTL_TXN_TYPE_COMP_ISSUE CONSTANT NUMBER := 35;
26     G_MTL_TXN_TYPE_COMP_RETURN CONSTANT NUMBER := 43;
27 
28 
29     -- The following constant represent inventory transaction source type.
30     -- Of the many types only the following one is used in the package.
31     G_MTL_TXN_SOURCE_TYPE_WIP CONSTANT NUMBER := 5;
32     -- swai: Bug 14804275 FP of bug 14752421 - end change for create estimate from WIP
33 
34     /*--------------------------------------------------*/
35     /* swai: 12.1 Service costing (bug 6960295)         */
36     /* procedure name: process_estimate_lines           */
37     /* description   : procedure used to create/update  */
38     /*                 delete charge lines. This        */
39     /*                 procedure allows the overriding  */
40     /*                 of the create/update/delete cost */
41     /*                 flag introduced in the Charges   */
42     /*                 API for 12.1 release             */
43     /*--------------------------------------------------*/
44 
45     PROCEDURE PROCESS_ESTIMATE_LINES(p_api_version      IN NUMBER,
46                                      p_commit           IN VARCHAR2 := Fnd_Api.g_false,
47                                      p_init_msg_list    IN VARCHAR2 := Fnd_Api.g_false,
48                                      p_validation_level IN NUMBER := Fnd_Api.g_valid_level_full,
49                                      p_action           IN VARCHAR2,
50                                      p_cs_cost_flag     IN VARCHAR2 := 'Y',
51                                      x_Charges_Rec      IN OUT NOCOPY Cs_Charge_Details_Pub.Charges_Rec_Type,
52                                      x_return_status    OUT NOCOPY VARCHAR2,
53                                      x_msg_count        OUT NOCOPY NUMBER,
54                                      x_msg_data         OUT NOCOPY VARCHAR2) IS
55         l_api_name    CONSTANT VARCHAR2(30) := 'PROCESS_ESTIMATE_LINES';
56         l_api_version CONSTANT NUMBER := 1.0;
57         l_msg_count             NUMBER;
58         l_msg_data              VARCHAR2(100);
59         l_msg_index             NUMBER;
60         l_estimate_detail_id    NUMBER;
61         x_object_version_number NUMBER;
62         x_line_number           NUMBER;
63         x_cost_id               NUMBER; -- swai: 12.1 Service costing uptake bug 6960295
64 
65     BEGIN
66         -- Standard Start of API savepoint
67         SAVEPOINT process_estimate_lines;
68 
69         -- Standard call to check for call compatibility.
70         IF NOT Fnd_Api.Compatible_API_Call(l_api_version,
71                                            p_api_version,
72                                            l_api_name,
73                                            G_PKG_NAME)
74         THEN
75             RAISE Fnd_Api.G_EXC_UNEXPECTED_ERROR;
76         END IF;
77 
78         -- Initialize message list if p_init_msg_list is set to TRUE.
79         IF Fnd_Api.to_Boolean(p_init_msg_list)
80         THEN
81             Fnd_Msg_Pub.initialize;
82         END IF;
83 
84         -- Initialize API return status to success
85         x_return_status := Fnd_Api.G_RET_STS_SUCCESS;
86 
87         -- Api body starts
88         IF (g_debug > 0)
89         THEN
90             Csd_Gen_Utility_Pvt.dump_api_info(p_pkg_name => G_PKG_NAME,
91                                               p_api_name => l_api_name);
92         END IF;
93         IF (g_debug > 0)
94         THEN
95             Csd_Gen_Utility_Pvt.ADD('p_action =' || p_action);
96         END IF;
97 
98         -- Based on the action, call the respective charges public api to
99         -- to create/update/delete the charge lines.
100         IF p_action = 'CREATE'
101         THEN
102 
103             IF (g_debug > 0)
104             THEN
105                 Csd_Gen_Utility_Pvt.ADD('Creating the charge lines ');
106             END IF;
107 
108             Cs_Charge_Details_Pub.Create_Charge_Details(p_api_version           => p_api_version,
109                                                         p_init_msg_list         => p_init_msg_list,
110                                                         p_commit                => p_commit,
111                                                         p_validation_level      => p_validation_level,
112                                                         p_transaction_control   => Fnd_Api.G_TRUE,
113                                                         p_Charges_Rec           => x_charges_rec,
114                                                         p_create_cost_detail    => p_cs_cost_flag,  -- swai: 12.1 service costing uptake bug 6960295
115                                                         x_object_version_number => x_object_version_number,
116                                                         x_estimate_detail_id    => l_estimate_detail_id,
117                                                         x_line_number           => x_line_number,
118                                                         x_return_status         => x_return_status,
119                                                         x_msg_count             => x_msg_count,
120                                                         x_msg_data              => x_msg_data,
121                                                         x_cost_id               => x_cost_id);  -- swai: 12.1 service costing uptake bug 6960295
122 
123             IF NOT (x_return_status = Fnd_Api.G_RET_STS_SUCCESS)
124             THEN
125                 IF (g_debug > 0)
126                 THEN
127                     Csd_Gen_Utility_Pvt.ADD('Create_Charge_Details failed ');
128                 END IF;
129 
130                 RAISE Fnd_Api.G_EXC_ERROR;
131             END IF;
132 
133             x_charges_rec.estimate_detail_id := l_estimate_detail_id;
134 
135         ELSIF p_action = 'UPDATE'
136         THEN
137 
138             IF (g_debug > 0)
139             THEN
140                 Csd_Gen_Utility_Pvt.ADD('l_Charges_Rec.estimate_detail_id =' ||
141                                         x_Charges_Rec.estimate_detail_id);
142             END IF;
143 
144             IF (g_debug > 0)
145             THEN
146                 Csd_Gen_Utility_Pvt.ADD('Updating the charge lines ');
147             END IF;
148 
149             IF (g_debug > 0)
150             THEN
151                 Csd_Gen_Utility_Pvt.ADD('Estimate Detail Id = ' ||
152                                         x_Charges_Rec.estimate_detail_id);
153                 Csd_Gen_Utility_Pvt.ADD('x_Charges_Rec.business_process_id=' ||
154                                         x_Charges_Rec.business_process_id);
155             END IF;
156 
157             IF ((NVL(x_Charges_Rec.business_process_id, Fnd_Api.G_MISS_NUM) =
158                Fnd_Api.G_MISS_NUM) AND
159                x_Charges_Rec.estimate_detail_id IS NOT NULL)
160             THEN
161                 BEGIN
162                     SELECT business_process_id
163                       INTO x_Charges_Rec.business_process_id
164                       FROM cs_estimate_details
165                      WHERE estimate_detail_id =
166                            x_Charges_Rec.estimate_detail_id;
167                 EXCEPTION
168                     WHEN NO_DATA_FOUND THEN
169                         IF (g_debug > 0)
170                         THEN
171                             Csd_Gen_Utility_Pvt.ADD('No Business business_process_id');
172                         END IF;
173                         RAISE Fnd_Api.G_EXC_ERROR;
174                     WHEN TOO_MANY_ROWS THEN
175                         IF (g_debug > 0)
176                         THEN
177                             Csd_Gen_Utility_Pvt.ADD('Too many business_process_id');
178                         END IF;
179                         RAISE Fnd_Api.G_EXC_ERROR;
180                 END;
181             END IF;
182 
183             IF (g_debug > 0)
184             THEN
185                 Csd_Gen_Utility_Pvt.ADD('x_Charges_Rec.business_process_id=' ||
186                                         x_Charges_Rec.business_process_id);
187             END IF;
188 
189             Cs_Charge_Details_Pub.Update_Charge_Details(p_api_version           => p_api_version,
190                                                         p_init_msg_list         => p_init_msg_list,
191                                                         p_commit                => p_commit,
192                                                         p_validation_level      => p_validation_level,
193                                                         p_transaction_control   => Fnd_Api.G_TRUE,
194                                                         p_Charges_Rec           => x_Charges_Rec,
195                                                         p_update_cost_detail    => p_cs_cost_flag,  -- swai: 12.1 service costing uptake bug 6960295
196                                                         x_object_version_number => x_object_version_number,
197                                                         x_return_status         => x_return_status,
198                                                         x_msg_count             => x_msg_count,
199                                                         x_msg_data              => x_msg_data);
200 
201             IF NOT (x_return_status = Fnd_Api.G_RET_STS_SUCCESS)
202             THEN
203                 IF (g_debug > 0)
204                 THEN
205                     Csd_Gen_Utility_Pvt.ADD('update_charge_details failed');
206                 END IF;
207 
208                 RAISE Fnd_Api.G_EXC_ERROR;
209             END IF;
210 
211         ELSIF p_action = 'DELETE'
212         THEN
213 
214             IF (g_debug > 0)
215             THEN
216                 Csd_Gen_Utility_Pvt.ADD('l_estimate_detail_id =' ||
217                                         l_estimate_detail_id);
218             END IF;
219 
220             IF (g_debug > 0)
221             THEN
222                 Csd_Gen_Utility_Pvt.ADD('Deleting the charge lines ');
223             END IF;
224 
225             l_estimate_detail_id := x_charges_rec.estimate_detail_id;
226 
227             Cs_Charge_Details_Pub.Delete_Charge_Details(p_api_version         => p_api_version,
228                                                         p_init_msg_list       => p_init_msg_list,
229                                                         p_commit              => p_commit,
230                                                         p_validation_level    => p_validation_level,
231                                                         p_transaction_control => Fnd_Api.G_TRUE,
232                                                         p_estimate_detail_id  => l_estimate_detail_id,
233                                                         p_delete_cost_detail  => p_cs_cost_flag,  -- swai: 12.1 service costing uptake bug 6960295
234                                                         x_return_status       => x_return_status,
235                                                         x_msg_count           => x_msg_count,
236                                                         x_msg_data            => x_msg_data);
237 
238             IF NOT (x_return_status = Fnd_Api.G_RET_STS_SUCCESS)
239             THEN
240                 IF (g_debug > 0)
241                 THEN
242                     Csd_Gen_Utility_Pvt.ADD('Delete_Charge_Details failed ');
243                 END IF;
244 
245                 RAISE Fnd_Api.G_EXC_ERROR;
246             END IF;
247 
248         ELSE
249             IF (g_debug > 0)
250             THEN
251                 Csd_Gen_Utility_Pvt.ADD('Invalid action is passed ');
252             END IF;
253 
254             Fnd_Message.SET_NAME('CSD', 'CSD_INVALID_ACTION');
255             Fnd_Message.SET_TOKEN('ACTION', p_action);
256             Fnd_Msg_Pub.ADD;
257             RAISE Fnd_Api.G_EXC_ERROR;
258 
259         END IF;
260 
261         -- Api body ends here
262         -- Standard check of p_commit.
263         IF Fnd_Api.To_Boolean(p_commit)
264         THEN
265             COMMIT WORK;
266         END IF;
267 
268         -- Standard call to get message count and IF count is  get message info.
269         Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
270                                   p_data  => x_msg_data);
271     EXCEPTION
272         WHEN Fnd_Api.G_EXC_ERROR THEN
273             ROLLBACK TO process_estimate_lines;
274             x_return_status := Fnd_Api.G_RET_STS_ERROR;
275             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
276                                       p_data  => x_msg_data);
277         WHEN Fnd_Api.G_EXC_UNEXPECTED_ERROR THEN
278             x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
279             ROLLBACK TO process_estimate_lines;
280             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
281                                       p_data  => x_msg_data);
282         WHEN OTHERS THEN
283             x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
284             ROLLBACK TO process_estimate_lines;
285             IF Fnd_Msg_Pub.Check_Msg_Level(Fnd_Msg_Pub.G_MSG_LVL_UNEXP_ERROR)
286             THEN
287                 Fnd_Msg_Pub.Add_Exc_Msg(G_PKG_NAME, l_api_name);
288             END IF;
289             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
290                                       p_data  => x_msg_data);
291     END process_estimate_lines;
292 
293     /*--------------------------------------------------*/
294     /* procedure name: update_ro_group_estimate         */
295     /* description   : procedure used to update         */
296     /*                 repair group for estimate changes*/
297     /*--------------------------------------------------*/
298     PROCEDURE UPDATE_RO_GROUP_ESTIMATE(p_api_version           IN NUMBER,
299                                        p_commit                IN VARCHAR2 := Fnd_Api.g_false,
300                                        p_init_msg_list         IN VARCHAR2 := Fnd_Api.g_false,
301                                        p_validation_level      IN NUMBER := Fnd_Api.g_valid_level_full,
302                                        p_repair_line_id        IN NUMBER,
303                                        x_object_version_number OUT NOCOPY NUMBER,
304                                        x_return_status         OUT NOCOPY VARCHAR2,
305                                        x_msg_count             OUT NOCOPY NUMBER,
306                                        x_msg_data              OUT NOCOPY VARCHAR2) IS
307         l_api_name    CONSTANT VARCHAR2(30) := 'UPDATE_RO_GROUP_ESTIMATE';
308         l_api_version CONSTANT NUMBER := 1.0;
309         l_msg_index       NUMBER;
310         l_return_status   VARCHAR2(10);
311         l_msg_count       NUMBER;
312         l_msg_data        VARCHAR2(2000);
313         l_group_quantity  NUMBER;
314         l_group_ovn       NUMBER;
315         l_repair_group_id NUMBER;
316         l_count           NUMBER;
317         l_tot_approved    NUMBER;
318         l_tot_no_approval NUMBER;
319         l_tot_rejected    NUMBER;
320         l_rep_group_rec   Csd_Repair_Groups_Pvt.REPAIR_ORDER_GROUP_REC;
321 
322     BEGIN
323 
324         IF (g_debug > 0)
325         THEN
326             Csd_Gen_Utility_Pvt.ADD('Inside UPDATE_RO_GROUP_ESTIMATE Procedure p_repair_line_id =' ||
327                                     p_repair_line_id);
328         END IF;
329 
330         -- Standard Start of API savepoint
331         SAVEPOINT UPDATE_RO_GROUP_ESTIMATE;
332 
333         -- Standard call to check for call compatibility.
334         IF NOT Fnd_Api.Compatible_API_Call(l_api_version,
335                                            p_api_version,
336                                            l_api_name,
337                                            G_PKG_NAME)
338         THEN
339             RAISE Fnd_Api.G_EXC_UNEXPECTED_ERROR;
340         END IF;
341 
342         -- Initialize message list if p_init_msg_list is set to TRUE.
343         IF Fnd_Api.to_Boolean(p_init_msg_list)
344         THEN
345             Fnd_Msg_Pub.initialize;
346         END IF;
347 
348         -- Initialize API return status to success
349         x_return_status := Fnd_Api.G_RET_STS_SUCCESS;
350 
351         -- Api body starts
352         IF (g_debug > 0)
353         THEN
354             Csd_Gen_Utility_Pvt.dump_api_info(p_pkg_name => G_PKG_NAME,
355                                               p_api_name => l_api_name);
356         END IF;
357         -- Check the required parameter
358         Csd_Process_Util.Check_Reqd_Param(p_param_value => p_repair_line_id,
359                                           p_param_name  => 'REPAIR_LINE_ID',
360                                           p_api_name    => l_api_name);
361 
362         IF (g_debug > 0)
363         THEN
364             Csd_Gen_Utility_Pvt.ADD('Validate repair line id');
365         END IF;
366 
367         -- Validate the repair line ID
368         IF NOT
369             (Csd_Process_Util.Validate_rep_line_id(p_repair_line_id => p_repair_line_id))
370         THEN
371             RAISE Fnd_Api.G_EXC_ERROR;
372         END IF;
373 
374         BEGIN
375             SELECT grp.repair_group_id,
376                    grp.group_quantity,
377                    grp.object_version_number
378               INTO l_repair_group_id, l_group_quantity, l_group_ovn
379               FROM CSD_REPAIR_ORDER_GROUPS grp
380              WHERE EXISTS
381              (SELECT 'x'
382                       FROM CSD_REPAIRS rep
383                      WHERE rep.repair_group_id = grp.repair_group_id
384                        AND rep.repair_line_id = p_repair_line_id);
385 
386         EXCEPTION
387             WHEN NO_DATA_FOUND THEN
388                 NULL;
389                 -- FND_MESSAGE.SET_NAME('CSD','CSD_API_RO_GRP_UPD_NO_DATA');
390             -- FND_MESSAGE.SET_TOKEN('REPAIR_LINE_ID',p_repair_line_id);
391             -- FND_MSG_PUB.ADD;
392             -- RAISE FND_API.G_EXC_ERROR;
393             WHEN OTHERS THEN
394                 NULL;
395                 -- FND_MESSAGE.SET_NAME('CSD','CSD_API_RO_GRP_UPD_OTHERS');
396             -- FND_MESSAGE.SET_TOKEN('REPAIR_LINE_ID',p_repair_line_id);
397             -- FND_MSG_PUB.ADD;
398             -- RAISE FND_API.G_EXC_ERROR;
399         END;
400 
401         IF (l_repair_group_id IS NOT NULL)
402         THEN
403 
404             l_count           := 0;
405             l_tot_approved    := 0;
406             l_tot_rejected    := 0;
407             l_tot_no_approval := 0;
408 
409             BEGIN
410                 SELECT COUNT(*)
411                   INTO l_tot_approved
412                   FROM CSD_REPAIRS
413                  WHERE repair_group_id = l_repair_group_id
414                    AND approval_status = 'A'
415                    AND approval_required_flag = 'Y';
416             EXCEPTION
417                 WHEN OTHERS THEN
418                     IF (g_debug > 0)
419                     THEN
420                         Csd_Gen_Utility_Pvt.ADD(' OTHERS l_tot_approved =' ||
421                                                 l_tot_approved);
422                     END IF;
423 
424             END;
425 
426             BEGIN
427                 SELECT COUNT(*)
428                   INTO l_tot_rejected
429                   FROM CSD_REPAIRS
430                  WHERE repair_group_id = l_repair_group_id
431                    AND approval_status = 'R'
432                    AND approval_required_flag = 'Y';
433             EXCEPTION
434                 WHEN OTHERS THEN
435                     IF (g_debug > 0)
436                     THEN
437                         Csd_Gen_Utility_Pvt.ADD(' OTHERS l_tot_rejected =' ||
438                                                 l_tot_rejected);
439                     END IF;
440 
441             END;
442 
443             BEGIN
444                 SELECT COUNT(*)
445                   INTO l_tot_no_approval
446                   FROM CSD_REPAIRS
447                  WHERE repair_group_id = l_repair_group_id
448                    AND approval_required_flag = 'N';
449             EXCEPTION
450                 WHEN OTHERS THEN
451                     IF (g_debug > 0)
452                     THEN
453                         Csd_Gen_Utility_Pvt.ADD(' OTHERS l_tot_no_approval =' ||
454                                                 l_tot_no_approval);
455                     END IF;
456 
457             END;
458 
459             IF (g_debug > 0)
460             THEN
461                 Csd_Gen_Utility_Pvt.ADD('l_tot_approved =' ||
462                                         l_tot_approved);
463             END IF;
464 
465             IF (g_debug > 0)
466             THEN
467                 Csd_Gen_Utility_Pvt.ADD('l_tot_rejected =' ||
468                                         l_tot_rejected);
469             END IF;
470 
471             IF (g_debug > 0)
472             THEN
473                 Csd_Gen_Utility_Pvt.ADD('l_tot_no_approval =' ||
474                                         l_tot_no_approval);
475             END IF;
476 
477             -- total of approved/rejected repairs with approval required = Y and approval required = N
478             -- Assumption no approval are not allowed to be rejected
479             l_count                               := NVL(l_tot_approved, 0) +
480                                                      NVL(l_tot_rejected, 0) +
481                                                      NVL(l_tot_no_approval,
482                                                          0);
483             l_rep_group_rec.repair_group_id       := l_repair_group_id;
484             l_rep_group_rec.object_version_number := l_group_ovn;
485 
486             -- check if all group qty have been approved/rejected
487             IF (NVL(l_group_quantity, 0) = NVL(l_tot_no_approval, 0))
488             THEN
489                 IF (g_debug > 0)
490                 THEN
491                     Csd_Gen_Utility_Pvt.ADD('EST_NOT_REQD ');
492                 END IF;
493 
494                 l_rep_group_rec.group_approval_status := 'EST_NOT_REQD';
495                 l_rep_group_rec.approved_quantity     := NVL(l_tot_no_approval,
496                                                              0);
497             ELSIF (l_group_quantity > l_count)
498             THEN
499                 IF (g_debug > 0)
500                 THEN
501                     Csd_Gen_Utility_Pvt.ADD('PARTIAL_APPRD ');
502                 END IF;
503 
504                 l_rep_group_rec.group_approval_status := 'PARTIAL_APPRD';
505                 l_rep_group_rec.approved_quantity     := NVL(l_tot_approved,
506                                                              0) +
507                                                          NVL(l_tot_no_approval,
508                                                              0);
509             ELSIF (l_group_quantity =
510                   NVL(l_tot_approved, 0) + NVL(l_tot_no_approval, 0))
511             THEN
512                 IF (g_debug > 0)
513                 THEN
514                     Csd_Gen_Utility_Pvt.ADD('APPROVED ');
515                 END IF;
516 
517                 l_rep_group_rec.group_approval_status := 'APPROVED';
518                 l_rep_group_rec.approved_quantity     := NVL(l_tot_approved,
519                                                              0) +
520                                                          NVL(l_tot_no_approval,
521                                                              0);
522             ELSIF (l_group_quantity = NVL(l_tot_rejected, 0))
523             THEN
524                 IF (g_debug > 0)
525                 THEN
526                     Csd_Gen_Utility_Pvt.ADD('REJECTED ');
527                 END IF;
528 
529                 l_rep_group_rec.group_approval_status := 'REJECTED';
530                 l_rep_group_rec.approved_quantity     := 0;
531             END IF;
532 
533             IF (g_debug > 0)
534             THEN
535                 Csd_Gen_Utility_Pvt.ADD('CSD_REPAIR_ESTIMATE_PVT.UPDATE_RO_GROUP_ESTIMATE Update Group RO call');
536             END IF;
537 
538             Csd_Repair_Groups_Pvt.UPDATE_REPAIR_GROUPS(p_api_version            => 1.0,
539                                                        p_commit                 => 'F',
540                                                        p_init_msg_list          => 'T',
541                                                        p_validation_level       => Fnd_Api.g_valid_level_full,
542                                                        x_repair_order_group_rec => l_rep_group_rec,
543                                                        x_return_status          => l_return_status,
544                                                        x_msg_count              => l_msg_count,
545                                                        x_msg_data               => l_msg_data);
546 
547             IF (g_debug > 0)
548             THEN
549                 Csd_Gen_Utility_Pvt.ADD('CSD_REPAIR_ESTIMATE_PVT.UPDATE_RO_GROUP_ESTIMATE UPDATE_REPAIR_GROUPS :' ||
550                                         x_return_status);
551             END IF;
552 
553             IF l_return_status <> 'S'
554             THEN
555                 x_return_status := Fnd_Api.G_RET_STS_ERROR;
556                 Fnd_Message.SET_NAME('CSD', 'CSD_API_RO_GROUP_EST_FAIL');
557                 Fnd_Msg_Pub.ADD;
558                 RAISE Fnd_Api.G_EXC_ERROR;
559             ELSIF l_return_status = 'S'
560             THEN
561                 x_object_version_number := l_rep_group_rec.object_version_number;
562             END IF;
563 
564             -- Api body ends here
565 
566             -- Standard check of p_commit.
567             IF Fnd_Api.To_Boolean(p_commit)
568             THEN
569                 COMMIT WORK;
570             END IF;
571 
572             -- Standard call to get message count and IF count is  get message info.
573             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
574                                       p_data  => x_msg_data);
575 
576         END IF;
577 
578     EXCEPTION
579         WHEN Fnd_Api.G_EXC_ERROR THEN
580             ROLLBACK TO UPDATE_RO_GROUP_ESTIMATE;
581             x_return_status := Fnd_Api.G_RET_STS_ERROR;
582             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
583                                       p_data  => x_msg_data);
584         WHEN Fnd_Api.G_EXC_UNEXPECTED_ERROR THEN
585             x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
586             ROLLBACK TO UPDATE_RO_GROUP_ESTIMATE;
587             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
588                                       p_data  => x_msg_data);
589         WHEN OTHERS THEN
590             x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
591             ROLLBACK TO UPDATE_RO_GROUP_ESTIMATE;
592             IF Fnd_Msg_Pub.Check_Msg_Level(Fnd_Msg_Pub.G_MSG_LVL_UNEXP_ERROR)
593             THEN
594                 Fnd_Msg_Pub.Add_Exc_Msg(G_PKG_NAME, l_api_name);
595             END IF;
596             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
597                                       p_data  => x_msg_data);
598 
599     END UPDATE_RO_GROUP_ESTIMATE;
600 
601     /*--------------------------------------------------*/
602     /* procedure name: create_repair_estimate           */
603     /* description   : procedure used to create         */
604     /*                 repair estimate headers          */
605     /*--------------------------------------------------*/
606 
607     PROCEDURE CREATE_REPAIR_ESTIMATE(p_api_version      IN NUMBER,
608                                      p_commit           IN VARCHAR2 := Fnd_Api.g_false,
609                                      p_init_msg_list    IN VARCHAR2 := Fnd_Api.g_false,
610                                      p_validation_level IN NUMBER := Fnd_Api.g_valid_level_full,
611                                      x_estimate_rec     IN OUT NOCOPY REPAIR_ESTIMATE_REC,
612                                      x_estimate_id      OUT NOCOPY NUMBER,
613                                      x_return_status    OUT NOCOPY VARCHAR2,
614                                      x_msg_count        OUT NOCOPY NUMBER,
615                                      x_msg_data         OUT NOCOPY VARCHAR2) IS
616 
617         l_api_name    CONSTANT VARCHAR2(30) := 'CREATE_REPAIR_ESTIMATE';
618         l_api_version CONSTANT NUMBER := 1.0;
619         l_msg_count         NUMBER;
620         l_msg_data          VARCHAR2(100);
621         l_msg_index         NUMBER;
622         l_dummy             VARCHAR2(1);
623         l_incident_id       NUMBER := NULL;
624         l_est_count         NUMBER := 0;
625         l_est_status_code   VARCHAR2(30);
626         l_api_return_status VARCHAR2(3);
627         l_group_obj_ver_num NUMBER;
628 
629     BEGIN
630         -- Standard Start of API savepoint
631         SAVEPOINT create_repair_estimate;
632 
633         -- Standard call to check for call compatibility.
634         IF NOT Fnd_Api.Compatible_API_Call(l_api_version,
635                                            p_api_version,
636                                            l_api_name,
637                                            G_PKG_NAME)
638         THEN
639             RAISE Fnd_Api.G_EXC_UNEXPECTED_ERROR;
640         END IF;
641 
642         -- Initialize message list if p_init_msg_list is set to TRUE.
643         IF Fnd_Api.to_Boolean(p_init_msg_list)
644         THEN
645             Fnd_Msg_Pub.initialize;
646         END IF;
647 
648         -- Initialize API return status to success
649         x_return_status := Fnd_Api.G_RET_STS_SUCCESS;
650 
651         -- Api body starts
652         IF (g_debug > 0)
653         THEN
654             Csd_Gen_Utility_Pvt.dump_api_info(p_pkg_name => G_PKG_NAME,
655                                               p_api_name => l_api_name);
656         END IF;
657         -- Dump the in parameters in the log file
658         -- if the debug level > 5
659         -- If fnd_profile.value('CSD_DEBUG_LEVEL') > 5 then
660 --        IF (g_debug > 5)
661 --        THEN
662 --            Csd_Gen_Utility_Pvt.dump_estimate_rec(p_estimate_rec => x_estimate_rec);
663 --        END IF;
664 
665         IF (g_debug > 0)
666         THEN
667             Csd_Gen_Utility_Pvt.ADD('Check reqd parameter');
668         END IF;
669 
670         -- Check the required parameter
671         Csd_Process_Util.Check_Reqd_Param(p_param_value => x_estimate_rec.repair_line_id,
672                                           p_param_name  => 'REPAIR_LINE_ID',
673                                           p_api_name    => l_api_name);
674 
675         -- swai 11.5.10
676         -- remove validation since these are not required anymore
677         /*
678          -- Check the required parameter
679          CSD_PROCESS_UTIL.Check_Reqd_Param
680          ( p_param_value    => x_estimate_rec.work_summary,
681            p_param_name     => 'WORK_SUMMARY',
682            p_api_name       => l_api_name);
683 
684          -- Check the required parameter
685          CSD_PROCESS_UTIL.Check_Reqd_Param
686          ( p_param_value    => x_estimate_rec.lead_time,
687            p_param_name     => 'LEAD_TIME',
688            p_api_name       => l_api_name);
689 
690          -- Check the required parameter
691          CSD_PROCESS_UTIL.Check_Reqd_Param
692          ( p_param_value    => x_estimate_rec.lead_time_uom,
693            p_param_name     => 'LEAD_TIME_UOM',
694            p_api_name       => l_api_name);
695         */
696 
697         -- Check the required parameter
698         Csd_Process_Util.Check_Reqd_Param(p_param_value => x_estimate_rec.estimate_status,
699                                           p_param_name  => 'ESTIMATE_STATUS',
700                                           p_api_name    => l_api_name);
701 
702         IF (g_debug > 0)
703         THEN
704             Csd_Gen_Utility_Pvt.ADD('Validate repair line id');
705         END IF;
706 
707         -- Validate the repair line ID
708         IF NOT
709             (Csd_Process_Util.Validate_rep_line_id(p_repair_line_id => x_estimate_rec.repair_line_id))
710         THEN
711             RAISE Fnd_Api.G_EXC_ERROR;
712         END IF;
713 
714         IF (g_debug > 0)
715         THEN
716             Csd_Gen_Utility_Pvt.ADD('Check if there is only one estimate per repair order');
717         END IF;
718 
719         BEGIN
720             SELECT COUNT(*)
721               INTO l_est_count
722               FROM CSD_REPAIR_ESTIMATE
723              WHERE repair_line_id = x_estimate_rec.repair_line_id;
724         EXCEPTION
725             WHEN OTHERS THEN
726                 NULL;
727         END;
728 
729         IF l_est_count > 0
730         THEN
731             IF (g_debug > 0)
732             THEN
733                 Csd_Gen_Utility_Pvt.ADD('Estimate already exists for the repair line Id: ' ||
734                                         x_estimate_rec.repair_line_id);
735             END IF;
736 
737             Fnd_Message.SET_NAME('CSD', 'CSD_API_ESTIMATE_EXISTS');
738             Fnd_Message.SET_TOKEN('REPAIR_LINE_ID',
739                                   x_estimate_rec.repair_line_id);
740             Fnd_Msg_Pub.ADD;
741             RAISE Fnd_Api.G_EXC_ERROR;
742         END IF;
743 
744         IF (g_debug > 0)
745         THEN
746             Csd_Gen_Utility_Pvt.ADD('Validate the estimate status');
747         END IF;
748 
749         BEGIN
750             SELECT lookup_code
751               INTO l_est_status_code
752               FROM fnd_lookups
753              WHERE lookup_type = 'CSD_ESTIMATE_STATUS'
754                AND lookup_code = x_estimate_rec.estimate_status;
755         EXCEPTION
756             WHEN NO_DATA_FOUND THEN
757                 Fnd_Message.SET_NAME('CSD', 'CSD_EST_STATUS_MISSING');
758                 Fnd_Message.SET_TOKEN('ESTIMATE_STATUS',
759                                       x_estimate_rec.estimate_status);
760                 Fnd_Msg_Pub.ADD;
761                 RAISE Fnd_Api.G_EXC_ERROR;
762         END;
763         /*
764                IF l_est_status_code in ('ACCEPTED','REJECTED') then
765 
766                 IF x_estimate_rec.estimate_reason_code is null then
767 
768                  FND_MESSAGE.SET_NAME('CSD','CSD_EST_REASON_CODE__MISSING');
769                    FND_MESSAGE.SET_TOKEN('REPAIR_ESTIMATE_ID',x_estimate_rec.repair_estimate_id);
770                    FND_MSG_PUB.ADD;
771                    RAISE FND_API.G_EXC_ERROR;
772 
773                 END IF;
774 
775               END IF;
776         */
777 
778         -- Assigning object version number
779         x_estimate_rec.object_version_number := 1;
780 
781         Csd_Repair_Estimate_Pkg.Insert_Row(px_REPAIR_ESTIMATE_ID   => x_estimate_rec.repair_estimate_id,
782                                            p_REPAIR_LINE_ID        => x_estimate_rec.repair_line_id,
783                                            p_ESTIMATE_STATUS       => x_estimate_rec.estimate_status,
784                                            p_ESTIMATE_DATE         => x_estimate_rec.estimate_date,
785                                            p_WORK_SUMMARY          => x_estimate_rec.work_summary,
786                                            p_PO_NUMBER             => x_estimate_rec.po_number,
787                                            p_LEAD_TIME             => x_estimate_rec.lead_time,
788                                            p_LEAD_TIME_UOM         => x_estimate_rec.lead_time_uom,
789                                            p_ESTIMATE_FREEZE_FLAG  => x_estimate_rec.estimate_freeze_flag,
790                                            p_ESTIMATE_REASON_CODE  => x_estimate_rec.estimate_reason_code,
791                                            p_NOT_TO_EXCEED         => x_estimate_rec.not_to_exceed,
792                                            p_LAST_UPDATE_DATE      => SYSDATE,
793                                            p_CREATION_DATE         => SYSDATE,
794                                            p_LAST_UPDATED_BY       => Fnd_Global.USER_ID,
795                                            p_CREATED_BY            => Fnd_Global.USER_ID,
796                                            p_LAST_UPDATE_LOGIN     => Fnd_Global.LOGIN_ID,
797                                            p_ATTRIBUTE1            => x_estimate_rec.ATTRIBUTE1,
798                                            p_ATTRIBUTE2            => x_estimate_rec.ATTRIBUTE2,
799                                            p_ATTRIBUTE3            => x_estimate_rec.ATTRIBUTE3,
800                                            p_ATTRIBUTE4            => x_estimate_rec.ATTRIBUTE4,
801                                            p_ATTRIBUTE5            => x_estimate_rec.ATTRIBUTE5,
802                                            p_ATTRIBUTE6            => x_estimate_rec.ATTRIBUTE6,
803                                            p_ATTRIBUTE7            => x_estimate_rec.ATTRIBUTE7,
804                                            p_ATTRIBUTE8            => x_estimate_rec.ATTRIBUTE8,
805                                            p_ATTRIBUTE9            => x_estimate_rec.ATTRIBUTE9,
806                                            p_ATTRIBUTE10           => x_estimate_rec.ATTRIBUTE10,
807                                            p_ATTRIBUTE11           => x_estimate_rec.ATTRIBUTE11,
808                                            p_ATTRIBUTE12           => x_estimate_rec.ATTRIBUTE12,
809                                            p_ATTRIBUTE13           => x_estimate_rec.ATTRIBUTE13,
810                                            p_ATTRIBUTE14           => x_estimate_rec.ATTRIBUTE14,
811                                            p_ATTRIBUTE15           => x_estimate_rec.ATTRIBUTE15,
812                                            p_CONTEXT               => x_estimate_rec.CONTEXT,
813                                            p_OBJECT_VERSION_NUMBER => 1);
814 
815         -- Api body ends here
816 
817         -- travi 052002 code
818         -- Call to update group estimate status and approved quantity
819         IF (x_estimate_rec.estimate_status IN ('ACCEPTED', 'REJECTED'))
820         THEN
821 
822             UPDATE_RO_GROUP_ESTIMATE(p_api_version           => 1.0,
823                                      p_commit                => Fnd_Api.g_false,
824                                      p_init_msg_list         => Fnd_Api.g_true,
825                                      p_validation_level      => Fnd_Api.g_valid_level_full,
826                                      p_repair_line_id        => x_estimate_rec.repair_line_id,
827                                      x_object_version_number => l_group_obj_ver_num,
828                                      x_return_status         => l_api_return_status,
829                                      x_msg_count             => x_msg_count,
830                                      x_msg_data              => x_msg_data);
831 
832             IF (l_api_return_status <> 'S')
833             THEN
834                 RAISE Fnd_Api.G_EXC_ERROR;
835             END IF;
836 
837         END IF;
838 
839         -- Standard check of p_commit.
840         IF Fnd_Api.To_Boolean(p_commit)
841         THEN
842             COMMIT WORK;
843         END IF;
844 
845         -- Standard call to get message count and IF count is  get message info.
846         Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
847                                   p_data  => x_msg_data);
848     EXCEPTION
849         WHEN Fnd_Api.G_EXC_ERROR THEN
850             x_return_status := Fnd_Api.G_RET_STS_ERROR;
851             ROLLBACK TO create_repair_estimate;
852             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
853                                       p_data  => x_msg_data);
854         WHEN Fnd_Api.G_EXC_UNEXPECTED_ERROR THEN
855             x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
856             ROLLBACK TO create_repair_estimate;
857             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
858                                       p_data  => x_msg_data);
859         WHEN OTHERS THEN
860             x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
861             ROLLBACK TO create_repair_estimate;
862             IF Fnd_Msg_Pub.Check_Msg_Level(Fnd_Msg_Pub.G_MSG_LVL_UNEXP_ERROR)
863             THEN
864                 Fnd_Msg_Pub.Add_Exc_Msg(G_PKG_NAME, l_api_name);
865             END IF;
866             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
867                                       p_data  => x_msg_data);
868     END create_repair_estimate;
869 
870     /*--------------------------------------------------*/
871     /* procedure name: update_repair_estimate           */
872     /* description   : procedure used to update         */
873     /*                 repair estimate lines            */
874     /*                                                  */
875     /*--------------------------------------------------*/
876 
877     PROCEDURE UPDATE_REPAIR_ESTIMATE(p_api_version      IN NUMBER,
878                                      p_commit           IN VARCHAR2 := Fnd_Api.g_false,
879                                      p_init_msg_list    IN VARCHAR2 := Fnd_Api.g_false,
880                                      p_validation_level IN NUMBER := Fnd_Api.g_valid_level_full,
881                                      x_estimate_rec     IN OUT NOCOPY REPAIR_ESTIMATE_REC,
882                                      x_return_status    OUT NOCOPY VARCHAR2,
883                                      x_msg_count        OUT NOCOPY NUMBER,
884                                      x_msg_data         OUT NOCOPY VARCHAR2) IS
885 
886         l_api_name    CONSTANT VARCHAR2(30) := 'UPDATE_REPAIR_ESTIMATE';
887         l_api_version CONSTANT NUMBER := 1.0;
888         l_msg_count         NUMBER;
889         l_msg_data          VARCHAR2(100);
890         l_msg_index         NUMBER;
891         l_upd_estimate_flag VARCHAR2(1) := '';
892         l_approval_status   VARCHAR2(1);
893         l_estimate_id       NUMBER;
894         l_obj_ver_num       NUMBER;
895         l_est_status_code   VARCHAR2(30);
896         l_api_return_status VARCHAR2(3);
897         l_group_obj_ver_num NUMBER;
898 
899         CURSOR repair_estimate(p_est_id IN NUMBER) IS
900             SELECT a.repair_estimate_id,
901                    a.object_version_number,
902                    b.approval_status
903               FROM CSD_REPAIR_ESTIMATE a, CSD_REPAIRS b
904              WHERE a.repair_line_id = b.repair_line_id
905                AND a.repair_estimate_id = p_est_id;
906 
907     BEGIN
908         -- Standard Start of API savepoint
909         SAVEPOINT update_repair_estimate;
910 
911         -- Standard call to check for call compatibility.
912         IF NOT Fnd_Api.Compatible_API_Call(l_api_version,
913                                            p_api_version,
914                                            l_api_name,
915                                            G_PKG_NAME)
916         THEN
917             RAISE Fnd_Api.G_EXC_UNEXPECTED_ERROR;
918         END IF;
919 
920         -- Initialize message list if p_init_msg_list is set to TRUE.
921         IF Fnd_Api.to_Boolean(p_init_msg_list)
922         THEN
923             Fnd_Msg_Pub.initialize;
924         END IF;
925 
926         -- Initialize API return status to success
927         x_return_status := Fnd_Api.G_RET_STS_SUCCESS;
928 
929         -- Api body starts
930         IF (g_debug > 0)
931         THEN
932             Csd_Gen_Utility_Pvt.dump_api_info(p_pkg_name => G_PKG_NAME,
933                                               p_api_name => l_api_name);
934         END IF;
935         -- Dump the in parameters in the log file
936         -- if the debug level > 5
937         --If fnd_profile.value('CSD_DEBUG_LEVEL') > 5 then
938 --        IF (g_debug > 5)
939 --        THEN
940 --            Csd_Gen_Utility_Pvt.dump_estimate_rec(p_estimate_rec => x_estimate_rec);
941 --        END IF;
942 
943         IF (g_debug > 0)
944         THEN
945             Csd_Gen_Utility_Pvt.ADD('Check reqd parameter: Repair Estimate Id');
946         END IF;
947 
948         IF (g_debug > 0)
949         THEN
950             Csd_Gen_Utility_Pvt.ADD('Repair Estimate Id =' ||
951                                     x_estimate_rec.repair_estimate_id);
952         END IF;
953 
954         -- Check the required parameter
955         Csd_Process_Util.Check_Reqd_Param(p_param_value => x_estimate_rec.repair_estimate_id,
956                                           p_param_name  => 'REPAIR_ESTIMATE_ID',
957                                           p_api_name    => l_api_name);
958 
959         IF NVL(x_estimate_rec.repair_estimate_id, Fnd_Api.G_MISS_NUM) <>
960            Fnd_Api.G_MISS_NUM
961         THEN
962 
963             OPEN repair_estimate(x_estimate_rec.repair_estimate_id);
964             FETCH repair_estimate
965                 INTO l_estimate_id, l_obj_ver_num, l_approval_status;
966 
967             IF repair_estimate%NOTFOUND
968             THEN
969                 Fnd_Message.SET_NAME('CSD', 'CSD_API_ESTIMATE_MISSING');
970                 Fnd_Message.SET_TOKEN('REPAIR_ESTIMATE_ID', l_estimate_id);
971                 Fnd_Msg_Pub.ADD;
972                 RAISE Fnd_Api.G_EXC_ERROR;
973             END IF;
974 
975             IF repair_estimate%ISOPEN
976             THEN
977                 CLOSE repair_estimate;
978             END IF;
979 
980         END IF;
981 
982         IF NVL(x_estimate_rec.object_version_number, Fnd_Api.G_MISS_NUM) <>
983            l_obj_ver_num
984         THEN
985             IF (g_debug > 0)
986             THEN
987                 Csd_Gen_Utility_Pvt.ADD('object version number does not match');
988             END IF;
989 
990             Fnd_Message.SET_NAME('CSD', 'CSD_OBJ_VER_MISMATCH');
991             Fnd_Message.SET_TOKEN('REPAIR_ESTIMATE_ID', l_estimate_id);
992             Fnd_Msg_Pub.ADD;
993             RAISE Fnd_Api.G_EXC_ERROR;
994         END IF;
995 
996         IF (g_debug > 0)
997         THEN
998             Csd_Gen_Utility_Pvt.ADD('Validate the estimate status');
999         END IF;
1000 
1001         BEGIN
1002             SELECT lookup_code
1003               INTO l_est_status_code
1004               FROM fnd_lookups
1005              WHERE lookup_type = 'CSD_ESTIMATE_STATUS'
1006                AND lookup_code = x_estimate_rec.estimate_status;
1007         EXCEPTION
1008             WHEN NO_DATA_FOUND THEN
1009                 Fnd_Message.SET_NAME('CSD', 'CSD_EST_STATUS_MISSING');
1010                 Fnd_Message.SET_TOKEN('ESTIMATE_STATUS',
1011                                       x_estimate_rec.estimate_status);
1012                 Fnd_Msg_Pub.ADD;
1013                 RAISE Fnd_Api.G_EXC_ERROR;
1014         END;
1015         /*
1016                IF l_est_status_code in ('ACCEPTED','REJECTED') then
1017 
1018                 IF x_estimate_rec.estimate_reason_code is null then
1019 
1020                  FND_MESSAGE.SET_NAME('CSD','CSD_EST_REASON_CODE__MISSING');
1021                    FND_MESSAGE.SET_TOKEN('REPAIR_ESTIMATE_ID',x_estimate_rec.repair_estimate_id);
1022                    FND_MSG_PUB.ADD;
1023                    RAISE FND_API.G_EXC_ERROR;
1024 
1025                 END IF;
1026               END IF;
1027         */
1028 
1029         IF (g_debug > 0)
1030         THEN
1031             Csd_Gen_Utility_Pvt.ADD('l_approval_status =' ||
1032                                     l_approval_status);
1033         END IF;
1034 
1035         -- Estimate lines are allowed to update only
1036         -- if it is not frozen
1037 
1038         -- IF NVL(l_approval_status,'Z') <> 'A' THEN
1039 
1040         IF (g_debug > 0)
1041         THEN
1042             Csd_Gen_Utility_Pvt.ADD('Calling CSD_REPAIR_ESTIMATE_PKG.Update_Row');
1043         END IF;
1044 
1045         Csd_Repair_Estimate_Pkg.Update_Row(p_REPAIR_ESTIMATE_ID    => x_estimate_rec.repair_estimate_id,
1046                                            p_REPAIR_LINE_ID        => x_estimate_rec.repair_line_id,
1047                                            p_ESTIMATE_STATUS       => x_estimate_rec.estimate_status,
1048                                            p_ESTIMATE_DATE         => x_estimate_rec.estimate_date,
1049                                            p_WORK_SUMMARY          => x_estimate_rec.work_summary,
1050                                            p_PO_NUMBER             => x_estimate_rec.po_number,
1051                                            p_LEAD_TIME             => x_estimate_rec.lead_time,
1052                                            p_LEAD_TIME_UOM         => x_estimate_rec.lead_time_uom,
1053                                            p_ESTIMATE_FREEZE_FLAG  => x_estimate_rec.estimate_freeze_flag,
1054                                            p_ESTIMATE_REASON_CODE  => x_estimate_rec.estimate_reason_code,
1055                                            p_NOT_TO_EXCEED         => x_estimate_rec.not_to_exceed,
1056                                            p_LAST_UPDATE_DATE      => SYSDATE,
1057                                            p_CREATION_DATE         => SYSDATE,
1058                                            p_LAST_UPDATED_BY       => Fnd_Global.USER_ID,
1059                                            p_CREATED_BY            => Fnd_Global.USER_ID,
1060                                            p_LAST_UPDATE_LOGIN     => Fnd_Global.LOGIN_ID,
1061                                            p_ATTRIBUTE1            => x_estimate_rec.ATTRIBUTE1,
1062                                            p_ATTRIBUTE2            => x_estimate_rec.ATTRIBUTE2,
1063                                            p_ATTRIBUTE3            => x_estimate_rec.ATTRIBUTE3,
1064                                            p_ATTRIBUTE4            => x_estimate_rec.ATTRIBUTE4,
1065                                            p_ATTRIBUTE5            => x_estimate_rec.ATTRIBUTE5,
1066                                            p_ATTRIBUTE6            => x_estimate_rec.ATTRIBUTE6,
1067                                            p_ATTRIBUTE7            => x_estimate_rec.ATTRIBUTE7,
1068                                            p_ATTRIBUTE8            => x_estimate_rec.ATTRIBUTE8,
1069                                            p_ATTRIBUTE9            => x_estimate_rec.ATTRIBUTE9,
1070                                            p_ATTRIBUTE10           => x_estimate_rec.ATTRIBUTE10,
1071                                            p_ATTRIBUTE11           => x_estimate_rec.ATTRIBUTE11,
1072                                            p_ATTRIBUTE12           => x_estimate_rec.ATTRIBUTE12,
1073                                            p_ATTRIBUTE13           => x_estimate_rec.ATTRIBUTE13,
1074                                            p_ATTRIBUTE14           => x_estimate_rec.ATTRIBUTE14,
1075                                            p_ATTRIBUTE15           => x_estimate_rec.ATTRIBUTE15,
1076                                            p_CONTEXT               => x_estimate_rec.CONTEXT,
1077                                            p_OBJECT_VERSION_NUMBER => l_obj_ver_num + 1);
1078 
1079         x_estimate_rec.object_version_number := l_obj_ver_num + 1;
1080 
1081         -- END IF; -- end of update estimate
1082 
1083         -- Api body ends here
1084         -- travi 052002 code
1085         -- Call to update group estimate status and approved quantity
1086         IF (x_estimate_rec.estimate_status IN ('ACCEPTED', 'REJECTED'))
1087         THEN
1088 
1089             UPDATE_RO_GROUP_ESTIMATE(p_api_version           => 1.0,
1090                                      p_commit                => Fnd_Api.g_false,
1091                                      p_init_msg_list         => Fnd_Api.g_true,
1092                                      p_validation_level      => Fnd_Api.g_valid_level_full,
1093                                      p_repair_line_id        => x_estimate_rec.repair_line_id,
1094                                      x_object_version_number => l_group_obj_ver_num,
1095                                      x_return_status         => l_api_return_status,
1096                                      x_msg_count             => x_msg_count,
1097                                      x_msg_data              => x_msg_data);
1098 
1099             IF (l_api_return_status <> 'S')
1100             THEN
1101                 RAISE Fnd_Api.G_EXC_ERROR;
1102             END IF;
1103 
1104         END IF;
1105 
1106         -- Standard check of p_commit.
1107         IF Fnd_Api.To_Boolean(p_commit)
1108         THEN
1109             COMMIT WORK;
1110         END IF;
1111 
1112         -- Standard call to get message count and IF count is  get message info.
1113         Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
1114                                   p_data  => x_msg_data);
1115     EXCEPTION
1116         WHEN Fnd_Api.G_EXC_ERROR THEN
1117             x_return_status := Fnd_Api.G_RET_STS_ERROR;
1118             ROLLBACK TO update_repair_estimate;
1119             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
1120                                       p_data  => x_msg_data);
1121         WHEN Fnd_Api.G_EXC_UNEXPECTED_ERROR THEN
1122             x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
1123             ROLLBACK TO update_repair_estimate;
1124             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
1125                                       p_data  => x_msg_data);
1126         WHEN OTHERS THEN
1127             x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
1128             ROLLBACK TO update_repair_estimate;
1129             IF Fnd_Msg_Pub.Check_Msg_Level(Fnd_Msg_Pub.G_MSG_LVL_UNEXP_ERROR)
1130             THEN
1131                 Fnd_Msg_Pub.Add_Exc_Msg(G_PKG_NAME, l_api_name);
1132             END IF;
1133             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
1134                                       p_data  => x_msg_data);
1135     END update_repair_estimate;
1136 
1137     /*--------------------------------------------------*/
1138     /* procedure name: delete_repair_estimate           */
1139     /* description   : procedure used to delete         */
1140     /*                 repair estimate header           */
1141     /*                                                  */
1142     /*--------------------------------------------------*/
1143 
1144     PROCEDURE DELETE_REPAIR_ESTIMATE(p_api_version      IN NUMBER,
1145                                      p_commit           IN VARCHAR2 := Fnd_Api.g_false,
1146                                      p_init_msg_list    IN VARCHAR2 := Fnd_Api.g_false,
1147                                      p_validation_level IN NUMBER := Fnd_Api.g_valid_level_full,
1148                                      p_estimate_id      IN NUMBER,
1149                                      x_return_status    OUT NOCOPY VARCHAR2,
1150                                      x_msg_count        OUT NOCOPY NUMBER,
1151                                      x_msg_data         OUT NOCOPY VARCHAR2) IS
1152         l_api_name    CONSTANT VARCHAR2(30) := 'DELETE_REPAIR_ESTIMATE';
1153         l_api_version CONSTANT NUMBER := 1.0;
1154         l_msg_count          NUMBER;
1155         l_msg_data           VARCHAR2(100);
1156         l_msg_index          NUMBER;
1157         l_Charges_Rec        Cs_Charge_Details_Pub.charges_rec_type;
1158         x_estimate_detail_id NUMBER;
1159         l_est_detail_id      NUMBER;
1160         l_delete_allow       VARCHAR2(1);
1161         l_approval_status    VARCHAR2(1);
1162         l_est_line_count     NUMBER;
1163 
1164     BEGIN
1165         -- Standard Start of API savepoint
1166         SAVEPOINT delete_repair_estimate;
1167 
1168         -- Standard call to check for call compatibility.
1169         IF NOT Fnd_Api.Compatible_API_Call(l_api_version,
1170                                            p_api_version,
1171                                            l_api_name,
1172                                            G_PKG_NAME)
1173         THEN
1174             RAISE Fnd_Api.G_EXC_UNEXPECTED_ERROR;
1175         END IF;
1176 
1177         -- Initialize message list if p_init_msg_list is set to TRUE.
1178         IF Fnd_Api.to_Boolean(p_init_msg_list)
1179         THEN
1180             Fnd_Msg_Pub.initialize;
1181         END IF;
1182 
1183         -- Initialize API return status to success
1184         x_return_status := Fnd_Api.G_RET_STS_SUCCESS;
1185 
1186         -- Api body starts
1187         IF (g_debug > 0)
1188         THEN
1189             Csd_Gen_Utility_Pvt.dump_api_info(p_pkg_name => G_PKG_NAME,
1190                                               p_api_name => l_api_name);
1191         END IF;
1192         IF (g_debug > 0)
1193         THEN
1194             Csd_Gen_Utility_Pvt.ADD('Check reqd parameter: Estimate Id ');
1195         END IF;
1196 
1197         IF (g_debug > 0)
1198         THEN
1199             Csd_Gen_Utility_Pvt.ADD('Repair Estimate Id  =' ||
1200                                     p_estimate_id);
1201         END IF;
1202 
1203         -- Check the required parameter
1204         Csd_Process_Util.Check_Reqd_Param(p_param_value => p_estimate_id,
1205                                           p_param_name  => 'REPAIR_ESTIMATE_ID',
1206                                           p_api_name    => l_api_name);
1207 
1208         IF (g_debug > 0)
1209         THEN
1210             Csd_Gen_Utility_Pvt.ADD('Validate repair estimate id');
1211         END IF;
1212 
1213         -- Validate the repair line ID
1214         IF NOT
1215             (Csd_Process_Util.Validate_estimate_id(p_estimate_id => p_estimate_id))
1216         THEN
1217             RAISE Fnd_Api.G_EXC_ERROR;
1218         END IF;
1219 
1220         BEGIN
1221             SELECT b.approval_status
1222               INTO l_approval_status
1223               FROM CSD_REPAIR_ESTIMATE a, CSD_REPAIRS b
1224              WHERE a.repair_line_id = b.repair_line_id
1225                AND a.repair_estimate_id = p_estimate_id;
1226         EXCEPTION
1227             WHEN NO_DATA_FOUND THEN
1228                 IF (g_debug > 0)
1229                 THEN
1230                     Csd_Gen_Utility_Pvt.ADD('Estimate ID missing');
1231                 END IF;
1232 
1233                 Fnd_Message.SET_NAME('CSD', 'CSD_API_ESTIMATE_MISSING');
1234                 Fnd_Message.SET_TOKEN('REPAIR_ESTIMATE_ID', p_estimate_id);
1235                 Fnd_Msg_Pub.ADD;
1236                 RAISE Fnd_Api.G_EXC_ERROR;
1237         END;
1238 
1239         BEGIN
1240             SELECT COUNT(*)
1241               INTO l_est_line_count
1242               FROM CSD_REPAIR_ESTIMATE_LINES
1243              WHERE repair_estimate_id = p_estimate_id;
1244         EXCEPTION
1245             WHEN OTHERS THEN
1246                 NULL;
1247         END;
1248 
1249         IF l_est_line_count > 0
1250         THEN
1251             IF (g_debug > 0)
1252             THEN
1253                 Csd_Gen_Utility_Pvt.ADD('Estimate Lines exists for this estimate');
1254             END IF;
1255 
1256             Fnd_Message.SET_NAME('CSD', 'CSD_ESTIMATE_LINE_EXISTS');
1257             Fnd_Message.SET_TOKEN('REPAIR_ESTIMATE_ID', p_estimate_id);
1258             Fnd_Msg_Pub.ADD;
1259             RAISE Fnd_Api.G_EXC_ERROR;
1260         END IF;
1261 
1262         IF NVL(l_approval_status, 'Z') <> 'A'
1263         THEN
1264             IF (g_debug > 0)
1265             THEN
1266                 Csd_Gen_Utility_Pvt.ADD('Call CSD_REPAIR_ESTIMATE_PKG.Delete_Row');
1267             END IF;
1268 
1269             Csd_Repair_Estimate_Pkg.Delete_Row(p_REPAIR_ESTIMATE_ID => p_estimate_id);
1270         ELSE
1271             Fnd_Message.SET_NAME('CSD', 'CSD_EST_DELETE_NOT_ALLOWED');
1272             Fnd_Message.SET_TOKEN('REPAIR_ESTIMATE_ID', p_estimate_id);
1273             Fnd_Msg_Pub.ADD;
1274             RAISE Fnd_Api.G_EXC_ERROR;
1275         END IF;
1276 
1277         -- Api body ends here
1278         -- Standard check of p_commit.
1279         IF Fnd_Api.To_Boolean(p_commit)
1280         THEN
1281             COMMIT WORK;
1282         END IF;
1283 
1284         -- Standard call to get message count and IF count is  get message info.
1285         Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
1286                                   p_data  => x_msg_data);
1287     EXCEPTION
1288         WHEN Fnd_Api.G_EXC_ERROR THEN
1289             x_return_status := Fnd_Api.G_RET_STS_ERROR;
1290             ROLLBACK TO delete_repair_estimate;
1291             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
1292                                       p_data  => x_msg_data);
1293         WHEN Fnd_Api.G_EXC_UNEXPECTED_ERROR THEN
1294             x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
1295             ROLLBACK TO delete_repair_estimate;
1296             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
1297                                       p_data  => x_msg_data);
1298         WHEN OTHERS THEN
1299             x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
1300             ROLLBACK TO delete_repair_estimate;
1301             IF Fnd_Msg_Pub.Check_Msg_Level(Fnd_Msg_Pub.G_MSG_LVL_UNEXP_ERROR)
1302             THEN
1303                 Fnd_Msg_Pub.Add_Exc_Msg(G_PKG_NAME, l_api_name);
1304             END IF;
1305             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
1306                                       p_data  => x_msg_data);
1307     END delete_repair_estimate;
1308 
1309     /*--------------------------------------------------*/
1310     /* procedure name: lock_repair_estimate             */
1311     /* description   : procedure used to create         */
1312     /*                 repair estimate headers          */
1313     /*--------------------------------------------------*/
1314 
1315     PROCEDURE lock_repair_estimate(p_api_version      IN NUMBER,
1316                                    p_commit           IN VARCHAR2 := Fnd_Api.g_false,
1317                                    p_init_msg_list    IN VARCHAR2 := Fnd_Api.g_false,
1318                                    p_validation_level IN NUMBER := Fnd_Api.g_valid_level_full,
1319                                    p_estimate_rec     IN REPAIR_ESTIMATE_REC,
1320                                    x_return_status    OUT NOCOPY VARCHAR2,
1321                                    x_msg_count        OUT NOCOPY NUMBER,
1322                                    x_msg_data         OUT NOCOPY VARCHAR2) IS
1323 
1324         l_api_name    CONSTANT VARCHAR2(30) := 'LOCK_REPAIR_ESTIMATE';
1325         l_api_version CONSTANT NUMBER := 1.0;
1326         l_msg_count NUMBER;
1327         l_msg_data  VARCHAR2(100);
1328         l_msg_index NUMBER;
1329 
1330     BEGIN
1331         -- Standard Start of API savepoint
1332         SAVEPOINT lock_repair_estimate;
1333 
1334         -- Standard call to check for call compatibility.
1335         IF NOT Fnd_Api.Compatible_API_Call(l_api_version,
1336                                            p_api_version,
1337                                            l_api_name,
1338                                            G_PKG_NAME)
1339         THEN
1340             RAISE Fnd_Api.G_EXC_UNEXPECTED_ERROR;
1341         END IF;
1342 
1343         -- Initialize message list if p_init_msg_list is set to TRUE.
1344         IF Fnd_Api.to_Boolean(p_init_msg_list)
1345         THEN
1346             Fnd_Msg_Pub.initialize;
1347         END IF;
1348 
1349         -- Initialize API return status to success
1350         x_return_status := Fnd_Api.G_RET_STS_SUCCESS;
1351 
1352         Csd_Repair_Estimate_Pkg.Lock_Row(p_REPAIR_ESTIMATE_ID    => p_estimate_rec.repair_estimate_id,
1353                                          p_OBJECT_VERSION_NUMBER => p_estimate_rec.OBJECT_VERSION_NUMBER);
1354 
1355         -- Standard check of p_commit.
1356         IF Fnd_Api.To_Boolean(p_commit)
1357         THEN
1358             COMMIT WORK;
1359         END IF;
1360 
1361         -- Standard call to get message count and IF count is  get message info.
1362         Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
1363                                   p_data  => x_msg_data);
1364     EXCEPTION
1365         WHEN Fnd_Api.G_EXC_ERROR THEN
1366             x_return_status := Fnd_Api.G_RET_STS_ERROR;
1367             ROLLBACK TO lock_repair_estimate;
1368             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
1369                                       p_data  => x_msg_data);
1370         WHEN Fnd_Api.G_EXC_UNEXPECTED_ERROR THEN
1371             x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
1372             ROLLBACK TO lock_repair_estimate;
1373             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
1374                                       p_data  => x_msg_data);
1375         WHEN OTHERS THEN
1376             x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
1377             ROLLBACK TO lock_repair_estimate;
1378             IF Fnd_Msg_Pub.Check_Msg_Level(Fnd_Msg_Pub.G_MSG_LVL_UNEXP_ERROR)
1379             THEN
1380                 Fnd_Msg_Pub.Add_Exc_Msg(G_PKG_NAME, l_api_name);
1381             END IF;
1382             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
1383                                       p_data  => x_msg_data);
1384     END lock_repair_estimate;
1385 
1386     /*--------------------------------------------------*/
1387     /* procedure name: create_repair_estimate_lines     */
1388     /* description   : procedure used to create         */
1389     /*                 repair estimate lines            */
1390     /*--------------------------------------------------*/
1391 
1392     PROCEDURE CREATE_REPAIR_ESTIMATE_LINES(p_api_version       IN NUMBER,
1393                                            p_commit            IN VARCHAR2 := Fnd_Api.g_false,
1394                                            p_init_msg_list     IN VARCHAR2 := Fnd_Api.g_false,
1395                                            p_validation_level  IN NUMBER := Fnd_Api.g_valid_level_full,
1396                                            x_estimate_line_rec IN OUT NOCOPY CSD_REPAIR_ESTIMATE_PVT.REPAIR_ESTIMATE_LINE_REC,
1397                                            x_estimate_line_id  OUT NOCOPY NUMBER,
1398                                            x_return_status     OUT NOCOPY VARCHAR2,
1399                                            x_msg_count         OUT NOCOPY NUMBER,
1400                                            x_msg_data          OUT NOCOPY VARCHAR2) IS
1401 
1402         l_api_name    CONSTANT VARCHAR2(30) := 'CREATE_REPAIR_ESTIMATE_LINES';
1403         l_api_version CONSTANT NUMBER := 1.0;
1404         l_msg_count           NUMBER;
1405         l_msg_data            VARCHAR2(100);
1406         l_msg_index           NUMBER;
1407         l_serial_flag         BOOLEAN := FALSE;
1408         l_dummy               VARCHAR2(1);
1409         l_Charges_Rec         Cs_Charge_Details_Pub.Charges_Rec_Type;
1410         x_estimate_detail_id  NUMBER := NULL;
1411         l_incident_id         NUMBER := NULL;
1412         l_reference_number    VARCHAR2(30) := '';
1413         l_contract_number     VARCHAR2(120) := '';
1414         l_bus_process_id      NUMBER := NULL;
1415         l_repair_type_ref     VARCHAR2(3) := '';
1416         l_line_type_id        NUMBER := NULL;
1417         l_txn_billing_type_id NUMBER := NULL;
1418         l_party_id            NUMBER := NULL;
1419         l_account_id          NUMBER := NULL;
1420         l_order_header_id     NUMBER := NULL;
1421         l_release_status      VARCHAR2(10) := '';
1422         l_curr_code           VARCHAR2(10) := '';
1423         l_line_category_code  VARCHAR2(30) := '';
1424         l_ship_from_org_id    NUMBER := NULL;
1425         l_order_line_id       NUMBER := NULL;
1426         -- passing in from form
1427         -- l_coverage_id            NUMBER := NULL;
1428         -- l_coverage_name          VARCHAR2(30) := '';
1429         -- l_txn_group_id           NUMBER := NULL;
1430         --
1431         l_unit_selling_price NUMBER := NULL;
1432         l_item_cost          NUMBER := NULL;
1433 
1434         CURSOR order_rec(p_incident_id IN NUMBER) IS
1435             SELECT customer_id, account_id
1436               FROM cs_incidents_all_b
1437              WHERE incident_id = p_incident_id;
1438 
1439     BEGIN
1440         -- Standard Start of API savepoint
1441         SAVEPOINT create_repair_estimate_lines;
1442 
1443         -- Standard call to check for call compatibility.
1444         IF NOT Fnd_Api.Compatible_API_Call(l_api_version,
1445                                            p_api_version,
1446                                            l_api_name,
1447                                            G_PKG_NAME)
1448         THEN
1449             RAISE Fnd_Api.G_EXC_UNEXPECTED_ERROR;
1450         END IF;
1451 
1452         -- Initialize message list if p_init_msg_list is set to TRUE.
1453         IF Fnd_Api.to_Boolean(p_init_msg_list)
1454         THEN
1455             Fnd_Msg_Pub.initialize;
1456         END IF;
1457 
1458         -- Initialize API return status to success
1459         x_return_status := Fnd_Api.G_RET_STS_SUCCESS;
1460 
1461         -- Api body starts
1462         IF (g_debug > 0)
1463         THEN
1464             Csd_Gen_Utility_Pvt.dump_api_info(p_pkg_name => G_PKG_NAME,
1465                                               p_api_name => l_api_name);
1466         END IF;
1467         -- Dump the in parameters in the log file
1468         -- if the debug level > 5
1469         -- If fnd_profile.value('CSD_DEBUG_LEVEL') > 5 then
1470 --        IF (g_debug > 5)
1471 --        THEN
1472 --            Csd_Gen_Utility_Pvt.dump_estimate_line_rec(p_estimate_line_rec => x_estimate_line_rec);
1473 --        END IF;
1474 
1475         IF (g_debug > 0)
1476         THEN
1477             Csd_Gen_Utility_Pvt.ADD('Check reqd parameter');
1478         END IF;
1479 
1480         IF (g_debug > 0)
1481         THEN
1482             Csd_Gen_Utility_Pvt.ADD('Repair Line ID =' ||
1483                                     x_estimate_line_rec.repair_line_id);
1484         END IF;
1485 
1486         IF (g_debug > 0)
1487         THEN
1488             Csd_Gen_Utility_Pvt.ADD('txn_billing_type_id =' ||
1489                                     x_estimate_line_rec.txn_billing_type_id);
1490         END IF;
1491 
1492         -- Check the required parameter
1493         Csd_Process_Util.Check_Reqd_Param(p_param_value => x_estimate_line_rec.repair_line_id,
1494                                           p_param_name  => 'REPAIR_LINE_ID',
1495                                           p_api_name    => l_api_name);
1496 
1497         -- Check the required parameter
1498         Csd_Process_Util.Check_Reqd_Param(p_param_value => x_estimate_line_rec.txn_billing_type_id,
1499                                           p_param_name  => 'TXN_BILLING_TYPE_ID',
1500                                           p_api_name    => l_api_name);
1501 
1502         -- Check the required parameter
1503         Csd_Process_Util.Check_Reqd_Param(p_param_value => x_estimate_line_rec.inventory_item_id,
1504                                           p_param_name  => 'INVENTORY_ITEM_ID',
1505                                           p_api_name    => l_api_name);
1506 
1507         -- Check the required parameter
1508         Csd_Process_Util.Check_Reqd_Param(p_param_value => x_estimate_line_rec.unit_of_measure_code,
1509                                           p_param_name  => 'UNIT_OF_MEASURE_CODE',
1510                                           p_api_name    => l_api_name);
1511 
1512         -- Check the required parameter
1513         Csd_Process_Util.Check_Reqd_Param(p_param_value => x_estimate_line_rec.estimate_quantity,
1514                                           p_param_name  => 'ESTIMATE_QUANTITY',
1515                                           p_api_name    => l_api_name);
1516 
1517         -- Check the required parameter
1518         Csd_Process_Util.Check_Reqd_Param(p_param_value => x_estimate_line_rec.price_list_id,
1519                                           p_param_name  => 'PRICE_LIST_ID',
1520                                           p_api_name    => l_api_name);
1521 
1522         IF (g_debug > 0)
1523         THEN
1524             Csd_Gen_Utility_Pvt.ADD('Validate repair estimate id');
1525         END IF;
1526 
1527         -- Validate the repair line ID
1528         IF NOT
1529             (Csd_Process_Util.Validate_estimate_id(p_estimate_id => x_estimate_line_rec.repair_estimate_id))
1530         THEN
1531             RAISE Fnd_Api.G_EXC_ERROR;
1532         END IF;
1533 
1534         IF (g_debug > 0)
1535         THEN
1536             Csd_Gen_Utility_Pvt.ADD('Validate repair line id');
1537         END IF;
1538 
1539         -- Validate the repair line ID
1540         IF NOT
1541             (Csd_Process_Util.Validate_rep_line_id(p_repair_line_id => x_estimate_line_rec.repair_line_id))
1542         THEN
1543             RAISE Fnd_Api.G_EXC_ERROR;
1544         END IF;
1545 
1546         -- Get the service request
1547         BEGIN
1548             SELECT incident_id
1549               INTO l_incident_id
1550               FROM CSD_REPAIRS
1551              WHERE repair_line_id = x_estimate_line_rec.repair_line_id;
1552         EXCEPTION
1553             WHEN NO_DATA_FOUND THEN
1554                 Fnd_Message.SET_NAME('CSD', 'CSD_API_INV_REP_LINE_ID');
1555                 Fnd_Message.SET_TOKEN('REPAIR_LINE_ID',
1556                                       x_estimate_line_rec.repair_line_id);
1557                 Fnd_Msg_Pub.ADD;
1558                 RAISE Fnd_Api.G_EXC_ERROR;
1559         END;
1560 
1561         IF (g_debug > 0)
1562         THEN
1563             Csd_Gen_Utility_Pvt.ADD('l_incident_id  =' || l_incident_id);
1564         END IF;
1565 
1566         -- Get the business process id
1567         -- Forward port bug fix# 2756313
1568         --      l_bus_process_id := CSD_PROCESS_UTIL.GET_BUS_PROCESS(l_incident_id);
1569 
1570         l_bus_process_id := Csd_Process_Util.GET_BUS_PROCESS(x_estimate_line_rec.repair_line_id);
1571 
1572         IF (g_debug > 0)
1573         THEN
1574             Csd_Gen_Utility_Pvt.ADD('l_bus_process_id =' ||
1575                                     l_bus_process_id);
1576         END IF;
1577 
1578         IF l_bus_process_id < 0
1579         THEN
1580             IF NVL(x_estimate_line_rec.business_process_id,
1581                    Fnd_Api.G_MISS_NUM) <> Fnd_Api.G_MISS_NUM
1582             THEN
1583                 l_bus_process_id := x_estimate_line_rec.business_process_id;
1584             ELSE
1585                 IF (g_debug > 0)
1586                 THEN
1587                     Csd_Gen_Utility_Pvt.ADD('Business process does not exist ');
1588                 END IF;
1589 
1590                 RAISE Fnd_Api.G_EXC_ERROR;
1591             END IF;
1592         END IF;
1593 
1594         /* contract information passed from form
1595         IF (g_debug > 0 ) THEN
1596               csd_gen_utility_pvt.ADD('Getting the Coverage and txn Group Id');
1597         END IF;
1598 
1599         IF (g_debug > 0 ) THEN
1600               csd_gen_utility_pvt.ADD('contract_line_id ='||x_estimate_line_rec.contract_id);
1601         END IF;
1602 
1603 
1604               IF NVL(x_estimate_line_rec.contract_id,FND_API.G_MISS_NUM) <> FND_API.G_MISS_NUM THEN
1605 
1606                 Begin
1607                  SELECT cov.actual_coverage_id ,
1608                         cov.coverage_name,
1609                         ent.txn_group_id
1610                  INTO   l_coverage_id,
1611                         l_coverage_name,
1612                         l_txn_group_id
1613                  FROM   oks_ent_coverages_v cov,
1614                         oks_ent_txn_groups_v ent
1615                  WHERE  cov.contract_number = x_estimate_line_rec.contract_number -- travi change
1616                   AND   cov.actual_coverage_id = ent.coverage_id
1617                   AND   ent.business_process_id = l_bus_process_id;
1618 
1619         -- travi comment
1620         --         WHERE  cov.contract_line_id = x_estimate_line_rec.contract_id
1621 
1622                 Exception
1623                  When no_data_found then
1624                   FND_MESSAGE.SET_NAME('CSD','CSD_API_CONTRACT_MISSING');
1625                   FND_MESSAGE.SET_TOKEN('CONTRACT_LINE_ID',x_estimate_line_rec.contract_id);
1626                   FND_MSG_PUB.ADD;
1627         IF (g_debug > 0 ) THEN
1628                   csd_gen_utility_pvt.ADD('Contract Line Id missing');
1629         END IF;
1630 
1631                   RAISE FND_API.G_EXC_ERROR;
1632                 End;
1633 
1634                  x_estimate_line_rec.coverage_id := l_coverage_id;
1635                  x_estimate_line_rec.coverage_txn_group_id := l_txn_group_id;
1636 
1637         IF (g_debug > 0 ) THEN
1638                 csd_gen_utility_pvt.ADD('l_coverage_id  ='||l_coverage_id);
1639         END IF;
1640 
1641         IF (g_debug > 0 ) THEN
1642                 csd_gen_utility_pvt.ADD('l_txn_group_id ='||l_txn_group_id);
1643         END IF;
1644 
1645 
1646               End If;
1647         */
1648 
1649         IF l_incident_id IS NOT NULL
1650         THEN
1651             OPEN order_rec(l_incident_id);
1652             FETCH order_rec
1653                 INTO l_party_id, l_account_id;
1654 
1655             IF order_rec%NOTFOUND OR l_party_id IS NULL
1656             THEN
1657                 Fnd_Message.SET_NAME('CSD', 'CSD_API_PARTY_MISSING');
1658                 Fnd_Message.SET_TOKEN('INCIDENT_ID', l_incident_id);
1659                 Fnd_Msg_Pub.ADD;
1660                 RAISE Fnd_Api.G_EXC_ERROR;
1661             END IF;
1662 
1663             IF order_rec%ISOPEN
1664             THEN
1665                 CLOSE order_rec;
1666             END IF;
1667 
1668         END IF;
1669 
1670         IF (g_debug > 0)
1671         THEN
1672             Csd_Gen_Utility_Pvt.ADD('l_party_id   =' || l_party_id);
1673         END IF;
1674 
1675         IF (g_debug > 0)
1676         THEN
1677             Csd_Gen_Utility_Pvt.ADD('l_account_id =' || l_account_id);
1678         END IF;
1679 
1680         IF (g_debug > 0)
1681         THEN
1682             Csd_Gen_Utility_Pvt.ADD('x_estimate_line_rec.txn_billing_type_id =' ||
1683                                     x_estimate_line_rec.txn_billing_type_id);
1684         END IF;
1685 
1686         IF (g_debug > 0)
1687         THEN
1688             Csd_Gen_Utility_Pvt.ADD('x_estimate_line_rec.organization_id     =' ||
1689                                     x_estimate_line_rec.organization_id);
1690         END IF;
1691 
1692         -- Derive the txn_billing type and line category code
1693         -- from the transaction type
1694         Csd_Process_Util.GET_LINE_TYPE(p_txn_billing_type_id => x_estimate_line_rec.txn_billing_type_id,
1695                                        -- Following line commented (and substituted) for the bug 3337344.
1696                                        -- p_org_id              => x_estimate_line_rec.organization_id,
1697                                        -- Organization_id passed could really be Service Validation Org, whereas
1698                                        -- it should really be Operating Unit. CSD_PROCESS_UTIL.get_org_id procedure
1699                                        -- ensures that the OU is passed.
1700                                        p_org_id             => Csd_Process_Util.get_org_id(x_estimate_line_rec.incident_id),
1701                                        x_line_type_id       => l_line_type_id,
1702                                        x_line_category_code => l_line_category_code,
1703                                        x_return_status      => x_return_status);
1704 
1705         IF NOT (x_return_status = Fnd_Api.G_RET_STS_SUCCESS)
1706         THEN
1707             RAISE Fnd_Api.G_EXC_ERROR;
1708         END IF;
1709 
1710         IF (g_debug > 0)
1711         THEN
1712             Csd_Gen_Utility_Pvt.ADD('l_line_type_id        =' ||
1713                                     l_line_type_id);
1714         END IF;
1715 
1716         IF (g_debug > 0)
1717         THEN
1718             Csd_Gen_Utility_Pvt.ADD('l_line_category_code  =' ||
1719                                     l_line_category_code);
1720         END IF;
1721 
1722         IF (g_debug > 0)
1723         THEN
1724             Csd_Gen_Utility_Pvt.ADD('x_estimate_line_rec.price_list_id =' ||
1725                                     x_estimate_line_rec.price_list_id);
1726         END IF;
1727 
1728         -- If line_type_id Or line_category_code is null
1729         -- then raise error
1730         IF l_line_type_id IS NULL OR l_line_category_code IS NULL
1731         THEN
1732             Fnd_Message.SET_NAME('CSD', 'CSD_API_LINE_TYPE_MISSING');
1733             Fnd_Message.SET_TOKEN('TXN_BILLING_TYPE_ID',
1734                                   x_estimate_line_rec.txn_billing_type_id);
1735             Fnd_Msg_Pub.ADD;
1736             RAISE Fnd_Api.G_EXC_ERROR;
1737         END IF;
1738 
1739         -- Get the currency code
1740         IF NVL(x_estimate_line_rec.price_list_id, Fnd_Api.G_MISS_NUM) <>
1741            Fnd_Api.G_MISS_NUM
1742         THEN
1743             BEGIN
1744                 SELECT currency_code
1745                   INTO l_curr_code
1746                   FROM oe_price_lists
1747                  WHERE price_list_id = x_estimate_line_rec.price_list_id;
1748             EXCEPTION
1749                 WHEN NO_DATA_FOUND THEN
1750                     Fnd_Message.SET_NAME('CSD',
1751                                          'CSD_API_INV_PRICE_LIST_ID');
1752                     Fnd_Message.SET_TOKEN('PRICE_LIST_ID',
1753                                           x_estimate_line_rec.price_list_id);
1754                     Fnd_Msg_Pub.ADD;
1755                     RAISE Fnd_Api.G_EXC_ERROR;
1756             END;
1757         END IF;
1758 
1759         IF (g_debug > 0)
1760         THEN
1761             Csd_Gen_Utility_Pvt.ADD('l_curr_code     =' || l_curr_code);
1762         END IF;
1763 
1764         -- If l_curr_code is null then raise error
1765         IF l_curr_code IS NULL
1766         THEN
1767             Fnd_Message.SET_NAME('CSD', 'CSD_API_INV_CURR_CODE');
1768             Fnd_Message.SET_TOKEN('PRICE_LIST_ID',
1769                                   x_estimate_line_rec.price_list_id);
1770             Fnd_Msg_Pub.ADD;
1771             RAISE Fnd_Api.G_EXC_ERROR;
1772         END IF;
1773 
1774         --
1775         -- Commented for bug# 2857134 (forward port bugfix from 11.5.8)
1776         --
1777         --       IF NVL(x_estimate_line_rec.item_cost,FND_API.G_MISS_NUM)= FND_API.G_MISS_NUM THEN
1778         --IF (g_debug > 0 ) THEN
1779         --         csd_gen_utility_pvt.ADD('Get item cost' );
1780         --END IF;
1781         --
1782         --
1783         --        BEGIN
1784         --           select decode(item_cost,0 ,null,item_cost)
1785         --           into l_item_cost
1786         --           from cst_item_costs
1787         --           where inventory_item_id = x_estimate_line_rec.inventory_item_id
1788         --           and  organization_id   = cs_std.get_item_valdn_orgzn_id
1789         --           and  cost_type_id = 1;
1790         --
1791         --           x_estimate_line_rec.item_cost               := l_item_cost ;
1792         --
1793         --         EXCEPTION
1794         --          WHEN NO_DATA_FOUND THEN
1795         --IF (g_debug > 0 ) THEN
1796         --          csd_gen_utility_pvt.ADD('Could not get item cost' );
1797         --END IF;
1798         --
1799         --           FND_MESSAGE.SET_NAME('CSD','CSD_ITEM_COST_MISSING');
1800         --           FND_MSG_PUB.ADD;
1801         --           RAISE FND_API.G_EXC_ERROR;
1802         --        END;
1803         --      END IF;
1804         --
1805         --
1806 
1807         -- assigning values for the charge record
1808         x_estimate_line_rec.incident_id         := l_incident_id;
1809         x_estimate_line_rec.business_process_id := l_bus_process_id;
1810         x_estimate_line_rec.line_type_id        := l_line_type_id;
1811         x_estimate_line_rec.currency_code       := l_curr_code;
1812         x_estimate_line_rec.line_category_code  := l_line_category_code;
1813 
1814         -- travi new code
1815         --x_estimate_line_rec.charge_line_type        := ;
1816         --x_estimate_line_rec.apply_contract_discount := ;
1817 
1818         IF (g_debug > 0)
1819         THEN
1820             Csd_Gen_Utility_Pvt.ADD('Convert estimate line rec to charges rec');
1821         END IF;
1822 
1823         -- always create estimate lines with interface to OE flag as 'N'
1824         x_estimate_line_rec.interface_to_om_flag := 'N';
1825 
1826         -- Convert the estimate record to
1827         -- charge record
1828         Csd_Process_Util.CONVERT_EST_TO_CHG_REC(p_estimate_line_rec => x_estimate_line_rec,
1829                                                 x_charges_rec       => l_Charges_Rec,
1830                                                 x_return_status     => x_return_status);
1831 
1832         IF NOT (x_return_status = Fnd_Api.G_RET_STS_SUCCESS)
1833         THEN
1834             RAISE Fnd_Api.G_EXC_ERROR;
1835         END IF;
1836 
1837         IF (g_debug > 0)
1838         THEN
1839             Csd_Gen_Utility_Pvt.ADD('Call process_estimate_lines to create charge lines ');
1840         END IF;
1841 
1842         process_estimate_lines(p_api_version      => 1.0,
1843                                p_commit           => Fnd_Api.g_false,
1844                                p_init_msg_list    => Fnd_Api.g_false, -- swai 11.5.10, set to g_false
1845                                p_validation_level => Fnd_Api.g_valid_level_full,
1846                                p_action           => 'CREATE',
1847                                x_Charges_Rec      => l_Charges_Rec,
1848                                x_return_status    => x_return_status,
1849                                x_msg_count        => x_msg_count,
1850                                x_msg_data         => x_msg_data);
1851 
1852         IF NOT (x_return_status = Fnd_Api.G_RET_STS_SUCCESS)
1853         THEN
1854             RAISE Fnd_Api.G_EXC_ERROR;
1855         END IF;
1856 
1857         IF (g_debug > 0)
1858         THEN
1859             Csd_Gen_Utility_Pvt.ADD('NEW ESTIMATE DETAIL ID =' ||
1860                                     l_Charges_Rec.estimate_detail_id);
1861         END IF;
1862 
1863         IF (g_debug > 0)
1864         THEN
1865             Csd_Gen_Utility_Pvt.ADD('Call csd_repair_estimate_lines_pkg.insert_row to create repair estimate lines ');
1866         END IF;
1867 
1868         -- travi forward port Bug # 2789754 fix added override_charge_flag
1869         Csd_Repair_Estimate_Lines_Pkg.Insert_Row(px_REPAIR_ESTIMATE_LINE_ID  => x_estimate_line_rec.repair_estimate_line_id,
1870                                                  p_REPAIR_ESTIMATE_ID        => x_estimate_line_rec.repair_estimate_id,
1871                                                  p_ESTIMATE_DETAIL_ID        => l_Charges_Rec.estimate_detail_id,
1872                                                  p_LAST_UPDATE_DATE          => SYSDATE,
1873                                                  p_CREATION_DATE             => SYSDATE,
1874                                                  p_LAST_UPDATED_BY           => Fnd_Global.USER_ID,
1875                                                  p_CREATED_BY                => Fnd_Global.USER_ID,
1876                                                  p_LAST_UPDATE_LOGIN         => Fnd_Global.LOGIN_ID,
1877                                                  p_ITEM_COST                 => x_estimate_line_rec.item_cost,
1878                                                  p_RESOURCE_ID               => x_estimate_line_rec.resource_id,
1879                                                  p_OVERRIDE_CHARGE_FLAG      => x_estimate_line_rec.override_charge_flag,
1880                                                  p_JUSTIFICATION_NOTES       => x_estimate_line_rec.justification_notes,
1881                                                  p_ATTRIBUTE1                => x_estimate_line_rec.ATTRIBUTE1,
1882                                                  p_ATTRIBUTE2                => x_estimate_line_rec.ATTRIBUTE2,
1883                                                  p_ATTRIBUTE3                => x_estimate_line_rec.ATTRIBUTE3,
1884                                                  p_ATTRIBUTE4                => x_estimate_line_rec.ATTRIBUTE4,
1885                                                  p_ATTRIBUTE5                => x_estimate_line_rec.ATTRIBUTE5,
1886                                                  p_ATTRIBUTE6                => x_estimate_line_rec.ATTRIBUTE6,
1887                                                  p_ATTRIBUTE7                => x_estimate_line_rec.ATTRIBUTE7,
1888                                                  p_ATTRIBUTE8                => x_estimate_line_rec.ATTRIBUTE8,
1889                                                  p_ATTRIBUTE9                => x_estimate_line_rec.ATTRIBUTE9,
1890                                                  p_ATTRIBUTE10               => x_estimate_line_rec.ATTRIBUTE10,
1891                                                  p_ATTRIBUTE11               => x_estimate_line_rec.ATTRIBUTE11,
1892                                                  p_ATTRIBUTE12               => x_estimate_line_rec.ATTRIBUTE12,
1893                                                  p_ATTRIBUTE13               => x_estimate_line_rec.ATTRIBUTE13,
1894                                                  p_ATTRIBUTE14               => x_estimate_line_rec.ATTRIBUTE14,
1895                                                  p_ATTRIBUTE15               => x_estimate_line_rec.ATTRIBUTE15,
1896                                                  p_CONTEXT                   => x_estimate_line_rec.CONTEXT,
1897                                                  p_OBJECT_VERSION_NUMBER     => 1,
1898                                                  p_EST_LINE_SOURCE_TYPE_CODE => x_estimate_line_rec.EST_LINE_SOURCE_TYPE_CODE,
1899                                                  p_EST_LINE_SOURCE_ID1       => x_estimate_line_rec.EST_LINE_SOURCE_ID1,
1900                                                  p_EST_LINE_SOURCE_ID2       => x_estimate_line_rec.EST_LINE_SOURCE_ID2,
1901                                                  p_RO_SERVICE_CODE_ID        => x_estimate_line_rec.RO_SERVICE_CODE_ID);
1902 
1903         x_estimate_line_rec.object_version_number := 1;
1904 
1905         -- Api body ends here
1906 
1907         -- Standard check of p_commit.
1908         IF Fnd_Api.To_Boolean(p_commit)
1909         THEN
1910             COMMIT WORK;
1911         END IF;
1912 
1913         -- Standard call to get message count and IF count is  get message info.
1914         Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
1915                                   p_data  => x_msg_data);
1916     EXCEPTION
1917         WHEN Fnd_Api.G_EXC_ERROR THEN
1918             x_return_status := Fnd_Api.G_RET_STS_ERROR;
1919             ROLLBACK TO create_repair_estimate_lines;
1920             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
1921                                       p_data  => x_msg_data);
1922         WHEN Fnd_Api.G_EXC_UNEXPECTED_ERROR THEN
1923             x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
1924             ROLLBACK TO create_repair_estimate_lines;
1925             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
1926                                       p_data  => x_msg_data);
1927         WHEN OTHERS THEN
1928             x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
1929             ROLLBACK TO create_repair_estimate_lines;
1930             IF Fnd_Msg_Pub.Check_Msg_Level(Fnd_Msg_Pub.G_MSG_LVL_UNEXP_ERROR)
1931             THEN
1932                 Fnd_Msg_Pub.Add_Exc_Msg(G_PKG_NAME, l_api_name);
1933             END IF;
1934             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
1935                                       p_data  => x_msg_data);
1936     END create_repair_estimate_lines;
1937 
1938     /*--------------------------------------------------*/
1939     /* procedure name: update_repair_estimate_lines     */
1940     /* description   : procedure used to update         */
1941     /*                 repair estimate lines            */
1942     /*                                                  */
1943     /*--------------------------------------------------*/
1944 
1945     PROCEDURE UPDATE_REPAIR_ESTIMATE_LINES(p_api_version       IN NUMBER,
1946                                            p_commit            IN VARCHAR2 := Fnd_Api.g_false,
1947                                            p_init_msg_list     IN VARCHAR2 := Fnd_Api.g_false,
1948                                            p_validation_level  IN NUMBER := Fnd_Api.g_valid_level_full,
1949                                            x_estimate_line_rec IN OUT NOCOPY CSD_REPAIR_ESTIMATE_PVT.REPAIR_ESTIMATE_LINE_REC,
1950                                            x_return_status     OUT NOCOPY VARCHAR2,
1951                                            x_msg_count         OUT NOCOPY NUMBER,
1952                                            x_msg_data          OUT NOCOPY VARCHAR2) IS
1953 
1954         l_api_name    CONSTANT VARCHAR2(30) := 'UPDATE_REPAIR_ESTIMATE_LINES';
1955         l_api_version CONSTANT NUMBER := 1.0;
1956         l_msg_count          NUMBER;
1957         l_msg_data           VARCHAR2(100);
1958         l_msg_index          NUMBER;
1959         l_upd_charge_flag    VARCHAR2(1) := '';
1960         l_dummy              VARCHAR2(1);
1961         l_Charges_Rec        Cs_Charge_Details_Pub.charges_rec_type;
1962         x_estimate_detail_id NUMBER := NULL;
1963         l_incident_id        NUMBER := NULL;
1964         l_party_id           NUMBER := NULL;
1965         l_account_id         NUMBER := NULL;
1966         l_order_header_id    NUMBER := NULL;
1967         l_curr_code          VARCHAR2(10) := '';
1968         l_picking_rule_id    NUMBER := NULL;
1969         l_est_detail_id      NUMBER := NULL;
1970         l_repair_line_id     NUMBER := NULL;
1971         l_booked_flag        VARCHAR2(1) := '';
1972         l_allow_ship         VARCHAR2(1) := '';
1973         l_obj_ver_num        NUMBER := NULL;
1974         l_ship_from_org_id   NUMBER := NULL;
1975         l_order_line_id      NUMBER := NULL;
1976         l_coverage_id        NUMBER := NULL;
1977         -- Bugfix 3617932, vkjain.
1978         -- Increasing the column length to 150
1979         -- l_coverage_name          VARCHAR2(150) := '';
1980         l_txn_group_id       NUMBER := NULL;
1981         l_bus_process_id     NUMBER := NULL;
1982         l_unit_selling_price NUMBER := NULL;
1983         l_serial_flag        BOOLEAN := FALSE;
1984 
1985         CURSOR estimate_line(p_est_line_id IN NUMBER) IS
1986             SELECT estimate_detail_id, object_version_number
1987               FROM CSD_REPAIR_ESTIMATE_LINES
1988              WHERE repair_estimate_line_id = p_est_line_id;
1989 
1990     BEGIN
1991         -- Standard Start of API savepoint
1992         SAVEPOINT update_repair_estimate_lines;
1993 
1994         -- Standard call to check for call compatibility.
1995         IF NOT Fnd_Api.Compatible_API_Call(l_api_version,
1996                                            p_api_version,
1997                                            l_api_name,
1998                                            G_PKG_NAME)
1999         THEN
2000             RAISE Fnd_Api.G_EXC_UNEXPECTED_ERROR;
2001         END IF;
2002 
2003         -- Initialize message list if p_init_msg_list is set to TRUE.
2004         IF Fnd_Api.to_Boolean(p_init_msg_list)
2005         THEN
2006             Fnd_Msg_Pub.initialize;
2007         END IF;
2008 
2009         -- Initialize API return status to success
2010         x_return_status := Fnd_Api.G_RET_STS_SUCCESS;
2011 
2012         -- Api body starts
2013         IF (g_debug > 0)
2014         THEN
2015             Csd_Gen_Utility_Pvt.dump_api_info(p_pkg_name => G_PKG_NAME,
2016                                               p_api_name => l_api_name);
2017         END IF;
2018         -- Dump the in parameters in the log file
2019         -- if the debug level > 5
2020         -- If fnd_profile.value('CSD_DEBUG_LEVEL') > 5 then
2021 --        IF (g_debug > 5)
2022 --        THEN
2023 --            Csd_Gen_Utility_Pvt.dump_estimate_line_rec(p_estimate_line_rec => x_estimate_line_rec);
2024 --        END IF;
2025 
2026         IF (g_debug > 0)
2027         THEN
2028             Csd_Gen_Utility_Pvt.ADD('Check reqd parameter: Repair Estimate Line id');
2029         END IF;
2030 
2031         IF (g_debug > 0)
2032         THEN
2033             Csd_Gen_Utility_Pvt.ADD('Repair Estimate Line Id =' ||
2034                                     x_estimate_line_rec.repair_estimate_line_id);
2035         END IF;
2036 
2037         -- Check the required parameter
2038         Csd_Process_Util.Check_Reqd_Param(p_param_value => x_estimate_line_rec.repair_estimate_line_id,
2039                                           p_param_name  => 'ESTIMATE_LINE_ID',
2040                                           p_api_name    => l_api_name);
2041 
2042         IF (g_debug > 0)
2043         THEN
2044             Csd_Gen_Utility_Pvt.ADD('Validate Estimate Line Id');
2045         END IF;
2046 
2047         -- Validate the repair line ID
2048         IF NOT
2049             (Csd_Process_Util.Validate_estimate_line_id(p_estimate_line_id => x_estimate_line_rec.repair_estimate_line_id))
2050         THEN
2051             RAISE Fnd_Api.G_EXC_ERROR;
2052         END IF;
2053 
2054         IF NVL(x_estimate_line_rec.repair_estimate_line_id,
2055                Fnd_Api.G_MISS_NUM) <> Fnd_Api.G_MISS_NUM
2056         THEN
2057 
2058             OPEN estimate_line(x_estimate_line_rec.repair_estimate_line_id);
2059             FETCH estimate_line
2060                 INTO l_est_detail_id, l_obj_ver_num;
2061             CLOSE estimate_line;
2062         END IF;
2063 
2064         IF NVL(x_estimate_line_rec.object_version_number,
2065                Fnd_Api.G_MISS_NUM) <> l_obj_ver_num
2066         THEN
2067             IF (g_debug > 0)
2068             THEN
2069                 Csd_Gen_Utility_Pvt.ADD('object version number does not match');
2070             END IF;
2071 
2072             Fnd_Message.SET_NAME('CSD', 'CSD_OBJ_VER_MISMATCH');
2073             Fnd_Message.SET_TOKEN('REPAIR_ESTIMATE_LINE_ID',
2074                                   x_estimate_line_rec.repair_estimate_line_id);
2075             Fnd_Msg_Pub.ADD;
2076             RAISE Fnd_Api.G_EXC_ERROR;
2077         END IF;
2078 
2079         IF NVL(x_estimate_line_rec.estimate_detail_id, Fnd_Api.G_MISS_NUM) <>
2080            Fnd_Api.G_MISS_NUM
2081         THEN
2082             IF x_estimate_line_rec.estimate_detail_id <> l_est_detail_id
2083             THEN
2084                 IF (g_debug > 0)
2085                 THEN
2086                     Csd_Gen_Utility_Pvt.ADD('The estimate detail id cannot to changed');
2087                 END IF;
2088 
2089                 RAISE Fnd_Api.G_EXC_ERROR;
2090             END IF;
2091         ELSE
2092             x_estimate_line_rec.estimate_detail_id := l_est_detail_id;
2093         END IF;
2094 
2095         IF NVL(x_estimate_line_rec.repair_line_id, Fnd_Api.G_MISS_NUM) <>
2096            Fnd_Api.G_MISS_NUM
2097         THEN
2098             IF x_estimate_line_rec.repair_line_id <> l_repair_line_id
2099             THEN
2100                 IF (g_debug > 0)
2101                 THEN
2102                     Csd_Gen_Utility_Pvt.ADD('The repair line id cannot to changed');
2103                 END IF;
2104 
2105                 RAISE Fnd_Api.G_EXC_ERROR;
2106             END IF;
2107         ELSE
2108             x_estimate_line_rec.repair_line_id := l_repair_line_id;
2109         END IF;
2110 
2111         BEGIN
2112             SELECT business_process_id
2113               INTO l_bus_process_id
2114               FROM cs_estimate_details
2115              WHERE estimate_detail_id = l_est_detail_id
2116                AND order_header_id IS NULL;
2117             l_upd_charge_flag := 'Y';
2118         EXCEPTION
2119             WHEN NO_DATA_FOUND THEN
2120                 l_upd_charge_flag := 'N';
2121         END;
2122 
2123         /* contract information passed from form
2124               IF NVL(x_estimate_line_rec.contract_id,FND_API.G_MISS_NUM) <> FND_API.G_MISS_NUM THEN
2125 
2126         IF (g_debug > 0 ) THEN
2127                csd_gen_utility_pvt.ADD('Getting the Coverage and txn Group Id');
2128         END IF;
2129 
2130         IF (g_debug > 0 ) THEN
2131                csd_gen_utility_pvt.ADD('contract_line_id ='||x_estimate_line_rec.contract_id);
2132         END IF;
2133 
2134         IF (g_debug > 0 ) THEN
2135                csd_gen_utility_pvt.ADD('l_bus_process_id ='||l_bus_process_id);
2136         END IF;
2137 
2138 
2139                 Begin
2140                  SELECT cov.actual_coverage_id ,
2141                         -- cov.coverage_name, -- Commented for bugfix 3617932
2142                         ent.txn_group_id
2143                  into   l_coverage_id,
2144                         -- l_coverage_name, -- Commented for bugfix 3617932
2145                         l_txn_group_id
2146                  FROM   oks_ent_coverages_v cov,
2147                         oks_ent_txn_groups_v ent
2148                  WHERE cov.contract_number = x_estimate_line_rec.contract_number -- takwong, fixed bug#2510068
2149                -- cov.contract_line_id = x_estimate_line_rec.contract_id
2150                   AND    cov.actual_coverage_id = ent.coverage_id
2151                   AND    ent.business_process_id = l_bus_process_id;
2152                 Exception
2153                  When no_data_found then
2154                   FND_MESSAGE.SET_NAME('CSD','CSD_API_CONTRACT_MISSING');
2155                   FND_MESSAGE.SET_TOKEN('CONTRACT_LINE_ID',x_estimate_line_rec.contract_id);
2156                   FND_MSG_PUB.ADD;
2157         IF (g_debug > 0 ) THEN
2158                   csd_gen_utility_pvt.ADD('Contract Line Id missing');
2159         END IF;
2160 
2161                   RAISE FND_API.G_EXC_ERROR;
2162                 End;
2163 
2164                  x_estimate_line_rec.coverage_id := l_coverage_id;
2165                  x_estimate_line_rec.coverage_txn_group_id := l_txn_group_id;
2166         IF (g_debug > 0 ) THEN
2167                  csd_gen_utility_pvt.ADD('l_coverage_id  ='||l_coverage_id);
2168         END IF;
2169 
2170         IF (g_debug > 0 ) THEN
2171                  csd_gen_utility_pvt.ADD('l_txn_group_id ='||l_txn_group_id);
2172         END IF;
2173 
2174 
2175               End If;
2176         */
2177 
2178         IF (g_debug > 0)
2179         THEN
2180             Csd_Gen_Utility_Pvt.ADD('l_upd_charge_flag =' ||
2181                                     l_upd_charge_flag);
2182         END IF;
2183 
2184         IF x_estimate_line_rec.item_cost = 0
2185         THEN
2186             IF (g_debug > 0)
2187             THEN
2188                 Csd_Gen_Utility_Pvt.ADD('item cost is zero');
2189             END IF;
2190 
2191             x_estimate_line_rec.item_cost := NULL;
2192         END IF;
2193 
2194         -- Charge lines are allowed to update only
2195         -- if it is not interfaced to OM
2196 
2197         IF l_upd_charge_flag = 'Y'
2198         THEN
2199 
2200             IF (g_debug > 0)
2201             THEN
2202                 Csd_Gen_Utility_Pvt.ADD('Convert estimate to charges rec');
2203             END IF;
2204 
2205             Csd_Process_Util.CONVERT_EST_TO_CHG_REC(p_estimate_line_rec => x_estimate_line_rec,
2206                                                     x_charges_rec       => l_Charges_Rec,
2207                                                     x_return_status     => x_return_status);
2208 
2209             IF NOT (x_return_status = Fnd_Api.G_RET_STS_SUCCESS)
2210             THEN
2211                 IF (g_debug > 0)
2212                 THEN
2213                     Csd_Gen_Utility_Pvt.ADD('csd_process_util.convert_to_chg_rec failed');
2214                 END IF;
2215 
2216                 RAISE Fnd_Api.G_EXC_ERROR;
2217             END IF;
2218 
2219             l_Charges_Rec.estimate_detail_id := l_est_detail_id;
2220 
2221             IF (g_debug > 0)
2222             THEN
2223                 Csd_Gen_Utility_Pvt.ADD('Call process_estimate_lines to update charge lines ');
2224             END IF;
2225 
2226             IF (g_debug > 0)
2227             THEN
2228                 Csd_Gen_Utility_Pvt.ADD('Estimate Detail Id = ' ||
2229                                         l_Charges_Rec.estimate_detail_id);
2230             END IF;
2231 
2232             process_estimate_lines(p_api_version      => 1.0,
2233                                    p_commit           => Fnd_Api.g_false,
2234                                    p_init_msg_list    => Fnd_Api.g_true,
2235                                    p_validation_level => Fnd_Api.g_valid_level_full,
2236                                    p_action           => 'UPDATE',
2237                                    x_Charges_Rec      => l_Charges_Rec,
2238                                    x_return_status    => x_return_status,
2239                                    x_msg_count        => x_msg_count,
2240                                    x_msg_data         => x_msg_data);
2241 
2242             IF NOT (x_return_status = Fnd_Api.G_RET_STS_SUCCESS)
2243             THEN
2244                 IF (g_debug > 0)
2245                 THEN
2246                     Csd_Gen_Utility_Pvt.ADD('process_estimate_lines failed ');
2247                 END IF;
2248 
2249                 RAISE Fnd_Api.G_EXC_ERROR;
2250             END IF;
2251 
2252         END IF; -- end of update charge line
2253 
2254         IF (g_debug > 0)
2255         THEN
2256             Csd_Gen_Utility_Pvt.ADD('Call csd_repair_estimate_line_pkg.update_row to update the repair estimate');
2257         END IF;
2258 
2259         IF (g_debug > 0)
2260         THEN
2261             Csd_Gen_Utility_Pvt.ADD('x_estimate_line_rec.repair_estimate_line_id =' ||
2262                                     x_estimate_line_rec.repair_estimate_line_id);
2263         END IF;
2264 
2265         -- travi forward port Bug # 2789754 fix added override_charge_flag
2266         Csd_Repair_Estimate_Lines_Pkg.Update_Row(p_REPAIR_ESTIMATE_LINE_ID   => x_estimate_line_rec.repair_estimate_line_id,
2267                                                  p_REPAIR_ESTIMATE_ID        => x_estimate_line_rec.repair_estimate_id,
2268                                                  p_ESTIMATE_DETAIL_ID        => x_estimate_line_rec.estimate_detail_id,
2269                                                  p_LAST_UPDATE_DATE          => SYSDATE,
2270                                                  p_CREATION_DATE             => SYSDATE,
2271                                                  p_LAST_UPDATED_BY           => Fnd_Global.USER_ID,
2272                                                  p_CREATED_BY                => Fnd_Global.USER_ID,
2273                                                  p_LAST_UPDATE_LOGIN         => Fnd_Global.USER_ID,
2274                                                  p_ITEM_COST                 => x_estimate_line_rec.item_cost,
2275                                                  p_RESOURCE_ID               => x_estimate_line_rec.resource_id,
2276                                                  p_OVERRIDE_CHARGE_FLAG      => x_estimate_line_rec.override_charge_flag,
2277                                                  p_JUSTIFICATION_NOTES       => x_estimate_line_rec.justification_notes,
2278                                                  p_ATTRIBUTE1                => x_estimate_line_rec.ATTRIBUTE1,
2279                                                  p_ATTRIBUTE2                => x_estimate_line_rec.ATTRIBUTE2,
2280                                                  p_ATTRIBUTE3                => x_estimate_line_rec.ATTRIBUTE3,
2281                                                  p_ATTRIBUTE4                => x_estimate_line_rec.ATTRIBUTE4,
2282                                                  p_ATTRIBUTE5                => x_estimate_line_rec.ATTRIBUTE5,
2283                                                  p_ATTRIBUTE6                => x_estimate_line_rec.ATTRIBUTE6,
2284                                                  p_ATTRIBUTE7                => x_estimate_line_rec.ATTRIBUTE7,
2285                                                  p_ATTRIBUTE8                => x_estimate_line_rec.ATTRIBUTE8,
2286                                                  p_ATTRIBUTE9                => x_estimate_line_rec.ATTRIBUTE9,
2287                                                  p_ATTRIBUTE10               => x_estimate_line_rec.ATTRIBUTE10,
2288                                                  p_ATTRIBUTE11               => x_estimate_line_rec.ATTRIBUTE11,
2289                                                  p_ATTRIBUTE12               => x_estimate_line_rec.ATTRIBUTE12,
2290                                                  p_ATTRIBUTE13               => x_estimate_line_rec.ATTRIBUTE13,
2291                                                  p_ATTRIBUTE14               => x_estimate_line_rec.ATTRIBUTE14,
2292                                                  p_ATTRIBUTE15               => x_estimate_line_rec.ATTRIBUTE15,
2293                                                  p_CONTEXT                   => x_estimate_line_rec.CONTEXT,
2294                                                  p_OBJECT_VERSION_NUMBER     => l_obj_ver_num + 1,
2295                                                  p_EST_LINE_SOURCE_TYPE_CODE => x_estimate_line_rec.EST_LINE_SOURCE_TYPE_CODE,
2296                                                  p_EST_LINE_SOURCE_ID1       => x_estimate_line_rec.EST_LINE_SOURCE_ID1,
2297                                                  p_EST_LINE_SOURCE_ID2       => x_estimate_line_rec.EST_LINE_SOURCE_ID2,
2298                                                  p_RO_SERVICE_CODE_ID        => x_estimate_line_rec.RO_SERVICE_CODE_ID);
2299 
2300         x_estimate_line_rec.object_version_number := l_obj_ver_num + 1;
2301 
2302         -- Api body ends here
2303 
2304         -- Standard check of p_commit.
2305         IF Fnd_Api.To_Boolean(p_commit)
2306         THEN
2307             COMMIT WORK;
2308         END IF;
2309 
2310         -- Standard call to get message count and IF count is  get message info.
2311         Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
2312                                   p_data  => x_msg_data);
2313     EXCEPTION
2314         WHEN Fnd_Api.G_EXC_ERROR THEN
2315             x_return_status := Fnd_Api.G_RET_STS_ERROR;
2316             ROLLBACK TO update_repair_estimate_lines;
2317             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
2318                                       p_data  => x_msg_data);
2319         WHEN Fnd_Api.G_EXC_UNEXPECTED_ERROR THEN
2320             x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
2321             ROLLBACK TO update_repair_estimate_lines;
2322             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
2323                                       p_data  => x_msg_data);
2324         WHEN OTHERS THEN
2325             x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
2326             ROLLBACK TO update_repair_estimate_lines;
2327             IF Fnd_Msg_Pub.Check_Msg_Level(Fnd_Msg_Pub.G_MSG_LVL_UNEXP_ERROR)
2328             THEN
2329                 Fnd_Msg_Pub.Add_Exc_Msg(G_PKG_NAME, l_api_name);
2330             END IF;
2331             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
2332                                       p_data  => x_msg_data);
2333     END update_repair_estimate_lines;
2334 
2335     /*--------------------------------------------------*/
2336     /* procedure name: delete_repair_estimate_lines     */
2337     /* description   : procedure used to delete         */
2338     /*                 repair estimate lines            */
2339     /*                                                  */
2340     /*--------------------------------------------------*/
2341 
2342     PROCEDURE DELETE_REPAIR_ESTIMATE_LINES(p_api_version      IN NUMBER,
2343                                            p_commit           IN VARCHAR2 := Fnd_Api.g_false,
2344                                            p_init_msg_list    IN VARCHAR2 := Fnd_Api.g_false,
2345                                            p_validation_level IN NUMBER := Fnd_Api.g_valid_level_full,
2346                                            p_estimate_line_id IN NUMBER,
2347                                            x_return_status    OUT NOCOPY VARCHAR2,
2348                                            x_msg_count        OUT NOCOPY NUMBER,
2349                                            x_msg_data         OUT NOCOPY VARCHAR2) IS
2350         l_api_name    CONSTANT VARCHAR2(30) := 'DELETE_REPAIR_ESTIMATE_LINES';
2351         l_api_version CONSTANT NUMBER := 1.0;
2352         l_msg_count          NUMBER;
2353         l_msg_data           VARCHAR2(100);
2354         l_msg_index          NUMBER;
2355         l_Charges_Rec        Cs_Charge_Details_Pub.charges_rec_type;
2356         x_estimate_detail_id NUMBER;
2357         l_est_detail_id      NUMBER;
2358         l_delete_allow       VARCHAR2(1);
2359 
2360     BEGIN
2361         -- Standard Start of API savepoint
2362         SAVEPOINT delete_repair_estimate_lines;
2363 
2364         -- Standard call to check for call compatibility.
2365         IF NOT Fnd_Api.Compatible_API_Call(l_api_version,
2366                                            p_api_version,
2367                                            l_api_name,
2368                                            G_PKG_NAME)
2369         THEN
2370             RAISE Fnd_Api.G_EXC_UNEXPECTED_ERROR;
2371         END IF;
2372 
2373         -- Initialize message list if p_init_msg_list is set to TRUE.
2374         IF Fnd_Api.to_Boolean(p_init_msg_list)
2375         THEN
2376             Fnd_Msg_Pub.initialize;
2377         END IF;
2378 
2379         -- Initialize API return status to success
2380         x_return_status := Fnd_Api.G_RET_STS_SUCCESS;
2381 
2382         -- Api body starts
2383         IF (g_debug > 0)
2384         THEN
2385             Csd_Gen_Utility_Pvt.dump_api_info(p_pkg_name => G_PKG_NAME,
2386                                               p_api_name => l_api_name);
2387         END IF;
2388         IF (g_debug > 0)
2389         THEN
2390             Csd_Gen_Utility_Pvt.ADD('Check reqd parameter: Estimate Line Id ');
2391         END IF;
2392 
2393         IF (g_debug > 0)
2394         THEN
2395             Csd_Gen_Utility_Pvt.ADD('Repair Estimate Line Id  =' ||
2396                                     p_estimate_line_id);
2397         END IF;
2398 
2399         -- Check the required parameter
2400         Csd_Process_Util.Check_Reqd_Param(p_param_value => p_estimate_line_id,
2401                                           p_param_name  => 'REPAIR_ESTIMATE_LINE_ID',
2402                                           p_api_name    => l_api_name);
2403 
2404         IF (g_debug > 0)
2405         THEN
2406             Csd_Gen_Utility_Pvt.ADD('Validate repair estimate line id');
2407         END IF;
2408 
2409         -- Validate the repair line ID
2410         IF NOT
2411             (Csd_Process_Util.Validate_estimate_line_id(p_estimate_line_id => p_estimate_line_id))
2412         THEN
2413             RAISE Fnd_Api.G_EXC_ERROR;
2414         END IF;
2415 
2416         IF (g_debug > 0)
2417         THEN
2418             Csd_Gen_Utility_Pvt.ADD('check if rocord is allowed to delete');
2419         END IF;
2420 
2421         -- The estimate line is allowed to delete
2422         -- only if it is not interfaced
2423         BEGIN
2424             SELECT a.estimate_detail_id
2425               INTO l_est_detail_id
2426               FROM CSD_REPAIR_ESTIMATE_LINES a, cs_estimate_details b
2427              WHERE a.estimate_detail_id = b.estimate_detail_id
2428                AND a.repair_estimate_line_id = p_estimate_line_id
2429                AND b.order_header_id IS NULL;
2430             l_delete_allow := 'Y';
2431         EXCEPTION
2432             WHEN NO_DATA_FOUND THEN
2433                 l_delete_allow := 'N';
2434                 IF (g_debug > 0)
2435                 THEN
2436                     Csd_Gen_Utility_Pvt.ADD('Estimate Line is interfaced,so it cannot be deleted');
2437                 END IF;
2438 
2439                 Fnd_Message.SET_NAME('CSD', 'CSD_API_DELETE_NOT_ALLOWED');
2440                 Fnd_Message.SET_TOKEN('REPAIR_ESTIMATE_LINE_ID',
2441                                       p_estimate_line_id);
2442                 Fnd_Msg_Pub.ADD;
2443                 RAISE Fnd_Api.G_EXC_ERROR;
2444         END;
2445 
2446         IF (g_debug > 0)
2447         THEN
2448             Csd_Gen_Utility_Pvt.ADD('l_delete_allow     =' ||
2449                                     l_delete_allow);
2450         END IF;
2451 
2452         IF (g_debug > 0)
2453         THEN
2454             Csd_Gen_Utility_Pvt.ADD('Estimate Detail Id =' ||
2455                                     l_est_detail_id);
2456         END IF;
2457 
2458         IF l_delete_allow = 'Y'
2459         THEN
2460 
2461             l_Charges_Rec.estimate_detail_id := l_est_detail_id;
2462 
2463             IF (g_debug > 0)
2464             THEN
2465                 Csd_Gen_Utility_Pvt.ADD('Call process_estimate_lines to delete');
2466             END IF;
2467 
2468             process_estimate_lines(p_api_version      => 1.0,
2469                                    p_commit           => Fnd_Api.g_false,
2470                                    p_init_msg_list    => Fnd_Api.g_true,
2471                                    p_validation_level => Fnd_Api.g_valid_level_full,
2472                                    p_action           => 'DELETE',
2473                                    x_Charges_Rec      => l_Charges_Rec,
2474                                    x_return_status    => x_return_status,
2475                                    x_msg_count        => x_msg_count,
2476                                    x_msg_data         => x_msg_data);
2477 
2478             IF NOT (x_return_status = Fnd_Api.G_RET_STS_SUCCESS)
2479             THEN
2480                 RAISE Fnd_Api.G_EXC_ERROR;
2481             END IF;
2482 
2483             IF (g_debug > 0)
2484             THEN
2485                 Csd_Gen_Utility_Pvt.ADD('Call csd_product_transactions_pkg.Delete_Row');
2486             END IF;
2487 
2488             Csd_Repair_Estimate_Lines_Pkg.Delete_Row(p_REPAIR_ESTIMATE_LINE_ID => p_estimate_line_id);
2489 
2490         END IF; --end of delete
2491 
2492         -- Api body ends here
2493 
2494         -- Standard check of p_commit.
2495         IF Fnd_Api.To_Boolean(p_commit)
2496         THEN
2497             COMMIT WORK;
2498         END IF;
2499 
2500         -- Standard call to get message count and IF count is  get message info.
2501         Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
2502                                   p_data  => x_msg_data);
2503     EXCEPTION
2504         WHEN Fnd_Api.G_EXC_ERROR THEN
2505             x_return_status := Fnd_Api.G_RET_STS_ERROR;
2506             ROLLBACK TO delete_repair_estimate_lines;
2507             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
2508                                       p_data  => x_msg_data);
2509         WHEN Fnd_Api.G_EXC_UNEXPECTED_ERROR THEN
2510             x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
2511             ROLLBACK TO delete_repair_estimate_lines;
2512             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
2513                                       p_data  => x_msg_data);
2514         WHEN OTHERS THEN
2515             x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
2516             ROLLBACK TO delete_repair_estimate_lines;
2517             IF Fnd_Msg_Pub.Check_Msg_Level(Fnd_Msg_Pub.G_MSG_LVL_UNEXP_ERROR)
2518             THEN
2519                 Fnd_Msg_Pub.Add_Exc_Msg(G_PKG_NAME, l_api_name);
2520             END IF;
2521             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
2522                                       p_data  => x_msg_data);
2523     END delete_repair_estimate_lines;
2524 
2525     /*--------------------------------------------------*/
2526     /* procedure name: lock_repair_estimate_lines       */
2527     /* description   : procedure used to create         */
2528     /*                 repair estimate lines            */
2529     /*--------------------------------------------------*/
2530 
2531     PROCEDURE LOCK_REPAIR_ESTIMATE_LINES(p_api_version       IN NUMBER,
2532                                          p_commit            IN VARCHAR2 := Fnd_Api.g_false,
2533                                          p_init_msg_list     IN VARCHAR2 := Fnd_Api.g_false,
2534                                          p_validation_level  IN NUMBER := Fnd_Api.g_valid_level_full,
2535                                          p_estimate_line_rec IN REPAIR_ESTIMATE_LINE_REC,
2536                                          x_return_status     OUT NOCOPY VARCHAR2,
2537                                          x_msg_count         OUT NOCOPY NUMBER,
2538                                          x_msg_data          OUT NOCOPY VARCHAR2) IS
2539 
2540         l_api_name    CONSTANT VARCHAR2(30) := 'LOCK_REPAIR_ESTIMATE_LINES';
2541         l_api_version CONSTANT NUMBER := 1.0;
2542         l_msg_count NUMBER;
2543         l_msg_data  VARCHAR2(100);
2544         l_msg_index NUMBER;
2545 
2546     BEGIN
2547         -- Standard Start of API savepoint
2548         SAVEPOINT lock_repair_estimate_lines;
2549 
2550         -- Standard call to check for call compatibility.
2551         IF NOT Fnd_Api.Compatible_API_Call(l_api_version,
2552                                            p_api_version,
2553                                            l_api_name,
2554                                            G_PKG_NAME)
2555         THEN
2556             RAISE Fnd_Api.G_EXC_UNEXPECTED_ERROR;
2557         END IF;
2558 
2559         -- Initialize message list if p_init_msg_list is set to TRUE.
2560         IF Fnd_Api.to_Boolean(p_init_msg_list)
2561         THEN
2562             Fnd_Msg_Pub.initialize;
2563         END IF;
2564 
2565         -- Initialize API return status to success
2566         x_return_status := Fnd_Api.G_RET_STS_SUCCESS;
2567 
2568         Csd_Repair_Estimate_Lines_Pkg.Lock_Row(p_REPAIR_ESTIMATE_LINE_ID => p_estimate_line_rec.repair_estimate_line_id,
2569                                                p_OBJECT_VERSION_NUMBER   => p_estimate_line_rec.object_version_number);
2570 
2571         -- Standard check of p_commit.
2572         IF Fnd_Api.To_Boolean(p_commit)
2573         THEN
2574             COMMIT WORK;
2575         END IF;
2576 
2577         -- Standard call to get message count and IF count is  get message info.
2578         Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
2579                                   p_data  => x_msg_data);
2580     EXCEPTION
2581         WHEN Fnd_Api.G_EXC_ERROR THEN
2582             x_return_status := Fnd_Api.G_RET_STS_ERROR;
2583             ROLLBACK TO lock_repair_estimate_lines;
2584             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
2585                                       p_data  => x_msg_data);
2586         WHEN Fnd_Api.G_EXC_UNEXPECTED_ERROR THEN
2587             x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
2588             ROLLBACK TO lock_repair_estimate_lines;
2589             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
2590                                       p_data  => x_msg_data);
2591         WHEN OTHERS THEN
2592             x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
2593             ROLLBACK TO lock_repair_estimate_lines;
2594             IF Fnd_Msg_Pub.Check_Msg_Level(Fnd_Msg_Pub.G_MSG_LVL_UNEXP_ERROR)
2595             THEN
2596                 Fnd_Msg_Pub.Add_Exc_Msg(G_PKG_NAME, l_api_name);
2597             END IF;
2598             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
2599                                       p_data  => x_msg_data);
2600     END lock_repair_estimate_lines;
2601 
2602     PROCEDURE REPAIR_ESTIMATE_PRINT(p_api_version      IN NUMBER,
2603                                     p_commit           IN VARCHAR2 := Fnd_Api.g_false,
2604                                     p_init_msg_list    IN VARCHAR2 := Fnd_Api.g_false,
2605                                     p_validation_level IN NUMBER := Fnd_Api.g_valid_level_full,
2606                                     p_repair_line_id   IN NUMBER,
2607                                     x_request_id       OUT NOCOPY NUMBER,
2608                                     x_return_status    OUT NOCOPY VARCHAR2,
2609                                     x_msg_count        OUT NOCOPY NUMBER,
2610                                     x_msg_data         OUT NOCOPY VARCHAR2) IS
2611         l_api_name    CONSTANT VARCHAR2(30) := 'REPAIR_ESTIMATE_PRINT';
2612         l_api_version CONSTANT NUMBER := 1.0;
2613         l_msg_count      NUMBER;
2614         l_msg_data       VARCHAR2(200);
2615         l_msg_index      NUMBER;
2616         l_est_count      NUMBER := 0;
2617         l_request_id     NUMBER := 0;  -- valase :6499519
2618         l_submit_Status  BOOLEAN;
2619         l_printer_name   VARCHAR2(80);
2620         l_print_required VARCHAR2(10);
2621         -- valase: 6499519  below
2622         l_layout_status  BOOLEAN;
2623         l_print_mode     VARCHAR2(30);
2624         l_xdo_conc_name  VARCHAR(30);
2625         l_template_code  VARCHAR(80);
2626         l_language       VARCHAR(2);
2627         l_territory      VARCHAR(2);
2628         CURSOR get_default_template_name(p_xdo_conc_name IN VARCHAR) IS
2629             SELECT template_code
2630               FROM fnd_concurrent_programs
2631              WHERE concurrent_program_name = p_xdo_conc_name; -- case sensitive
2632 		                                                    -- user must input con code with exactly as defined
2633         CURSOR get_lang_terr IS
2634             SELECT lower(iso_language), iso_territory
2635               FROM fnd_languages
2636              WHERE language_code = userenv('LANG');
2637         -- valase:6499519 , above
2638     BEGIN
2639         -- Standard Start of API savepoint
2640         SAVEPOINT estimate_print;
2641         -- Standard call to check for call compatibility.
2642         IF NOT Fnd_Api.Compatible_API_Call(l_api_version,
2643                                            p_api_version,
2644                                            l_api_name,
2645                                            G_PKG_NAME)
2646         THEN
2647             RAISE Fnd_Api.G_EXC_UNEXPECTED_ERROR;
2648         END IF;
2649         -- Initialize message list if p_init_msg_list is set to TRUE.
2650         IF Fnd_Api.to_Boolean(p_init_msg_list)
2651         THEN
2652             Fnd_Msg_Pub.initialize;
2653         END IF;
2654         -- Initialize API return status to success
2655         x_return_status := Fnd_Api.G_RET_STS_SUCCESS;
2656         -- start of the concurrent submission
2657         l_printer_name := Fnd_Profile.value('CSD_PRINTER_NAME');
2658         IF (g_debug > 0)
2659         THEN
2660             Csd_Gen_Utility_Pvt.ADD('l_printer_name =' || l_printer_name);
2661         END IF;
2662         -- Check if the printer setup is required
2663         -- l_print_required := fnd_profile.value('CSD_EST_PRINTER_REQ');
2664         -- For bugfix 3398079. vkjain.
2665         -- The profile name was misspelt.
2666         l_print_required := Fnd_Profile.value('CSD_PRINTER_REQ');
2667         IF (g_debug > 0)
2668         THEN
2669             Csd_Gen_Utility_Pvt.ADD('l_print_required =' ||
2670                                     l_print_required);
2671         END IF;
2672         -- Check for the printer setup
2673         IF l_printer_name IS NOT NULL
2674         THEN
2675             l_submit_status := Fnd_Request.set_print_options(printer        => l_printer_name,
2676                                                              style          => 'PORTRAIT',
2677                                                              copies         => 1,
2678                                                              save_output    => TRUE,
2679                                                              print_together => 'N');
2680             IF l_submit_status
2681             THEN
2682                 IF (g_debug > 0)
2683                 THEN
2684                     Csd_Gen_Utility_Pvt.ADD('Set Print Option successfull');
2685                 END IF;
2686             ELSE
2687                 IF (g_debug > 0)
2688                 THEN
2689                     Csd_Gen_Utility_Pvt.ADD('Set Print Option Failed');
2690                 END IF;
2691                 Fnd_Message.SET_NAME('CSD', 'CSD_EST_PRINT_OPTION_ERROR');
2692                 Fnd_Message.SET_TOKEN('REPAIR_LINE_ID', p_repair_line_id);
2693                 Fnd_Msg_Pub.ADD;
2694             END IF;
2695         ELSIF (l_print_required = 'Y')
2696         THEN
2697             Fnd_Message.SET_NAME('CSD', 'CSD_EST_NOPRINTER_SETUP');
2698             Fnd_Message.SET_TOKEN('REPAIR_LINE_ID', p_repair_line_id);
2699             Fnd_Msg_Pub.ADD;
2700         END IF;
2701         -- valase:6499519, rfieldma: 6532016  below
2702         -- get print mode
2703         l_print_mode := FND_PROFILE.value('CSD_ESTRT_PRINT_MODE');
2704 
2705         IF (l_print_mode is not null and l_print_mode <> 'ORAPT' ) THEN -- one of the XDO output types
2706             --get concurrent name
2707             l_xdo_conc_name :=FND_PROFILE.value('CSD_CUST_ESTRT_CON_NAME');
2708             IF l_xdo_conc_name is null THEN
2709                 l_xdo_conc_name := 'CSDERT';
2710             END IF;
2711             -- get default template code
2712             OPEN get_default_template_name(l_xdo_conc_name);
2713             FETCH get_default_template_name
2714              INTO l_template_code;
2715             CLOSE get_default_template_name;
2716             -- no default tempalte defined, raise error
2717             IF (l_template_code is null) THEN
2718                 Fnd_Message.SET_NAME('CSD', 'CSD_XDO_ESTRT_NO_TEMPLATE');
2719                 Fnd_Message.SET_TOKEN('PROF_NAME', csd_repairs_util.get_user_profile_option_name('CSD_CUST_ESTRT_CON_NAME'));
2720                 x_request_id := l_request_id;
2721                 Fnd_Msg_Pub.ADD;
2722                 RAISE Fnd_Api.G_EXC_ERROR;
2723             END IF;
2724             -- get current session language and territory
2725             OPEN get_lang_terr;
2726             FETCH get_lang_terr
2727              INTO l_language, l_territory;
2728             CLOSE get_lang_terr;
2729 
2730             -- XML publisher report
2731             -- set layout
2732             l_layout_status := Fnd_Request.add_layout('CSD', --template_appl_name
2733                                                        l_template_code, --'CSD_XDOESTRT_RTF', --template_code
2734                                                        l_language, --'en', 'ja',  --template_language
2735                                                        l_territory, --'US', 'JP' --template_territory
2736                                                        l_print_mode --output_format
2737                                                      );
2738             -- Submit the Concurrent Program
2739             l_request_id := Fnd_Request.submit_request('CSD',
2740                                                         l_xdo_conc_name,
2741                                                        'Depot Repair Estimate Report',
2742                                                         NULL,
2743                                                         FALSE,
2744                                                         p_repair_line_id);
2745         ELSE -- not defined or ORAPT (oracle reports)
2746              -- print in Oracle report format
2747              -- Submit the Concurrent Program
2748             l_request_id := Fnd_Request.submit_request('CSD',
2749                                                        'CSDESTRT',
2750                                                        'Depot Repair Estimate Report',
2751                                                         NULL,
2752                                                         FALSE,
2753                                                         p_repair_line_id);
2754         END IF;
2755         -- valase:6499519  above
2756         IF (g_debug > 0)
2757         THEN
2758             Csd_Gen_Utility_Pvt.ADD('Concurrent request Id =' ||
2759                                     TO_CHAR(l_request_id));
2760         END IF;
2761         IF (l_request_id <> 0)
2762         THEN
2763             COMMIT;
2764         END IF;
2765         x_request_id := l_request_id;
2766         -- Standard check of p_commit.
2767         IF Fnd_Api.To_Boolean(p_commit)
2768         THEN
2769             COMMIT WORK;
2770         END IF;
2771         -- Standard call to get message count and IF count is  get message info.
2772         Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
2773                                   p_data  => x_msg_data);
2774     EXCEPTION
2775         WHEN Fnd_Api.G_EXC_ERROR THEN
2776             x_return_status := Fnd_Api.G_RET_STS_ERROR;
2777             ROLLBACK TO estimate_print;
2778             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
2779                                       p_data  => x_msg_data);
2780         WHEN Fnd_Api.G_EXC_UNEXPECTED_ERROR THEN
2781             x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
2782             ROLLBACK TO estimate_print;
2783             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
2784                                       p_data  => x_msg_data);
2785         WHEN OTHERS THEN
2786             x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
2787             ROLLBACK TO estimate_print;
2788             IF Fnd_Msg_Pub.Check_Msg_Level(Fnd_Msg_Pub.G_MSG_LVL_UNEXP_ERROR)
2789             THEN
2790                 Fnd_Msg_Pub.Add_Exc_Msg(G_PKG_NAME, l_api_name);
2791             END IF;
2792             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
2793                                       p_data  => x_msg_data);
2794     END REPAIR_ESTIMATE_PRINT;
2795 
2796     PROCEDURE SUBMIT_REPAIR_ESTIMATE_LINES(p_api_version      IN NUMBER,
2797                                            p_commit           IN VARCHAR2,
2798                                            p_init_msg_list    IN VARCHAR2,
2799                                            p_validation_level IN NUMBER,
2800                                            p_repair_line_id   IN NUMBER,
2801                                            x_return_status    OUT NOCOPY VARCHAR2,
2802                                            x_msg_count        OUT NOCOPY NUMBER,
2803                                            x_msg_data         OUT NOCOPY VARCHAR2) IS
2804 
2805         l_api_name    CONSTANT VARCHAR2(30) := 'SUBMIT_REPAIR_ESTIMATE_LINES';
2806         l_api_version CONSTANT NUMBER := 1.0;
2807         l_estimate_detail_id  NUMBER;
2808         l_order_header_id     NUMBER;
2809         l_msg_count           NUMBER;
2810         l_msg_data            VARCHAR2(200);
2811         l_msg_index           NUMBER;
2812         l_incident_id         NUMBER := NULL;
2813         l_party_id            NUMBER := NULL;
2814         l_order_category_code VARCHAR2(30);
2815         l_account_id          NUMBER := NULL;
2816         l_add_to_same_order   BOOLEAN := TRUE;
2817         l_last_est_detail_id  NUMBER := NULL;
2818         l_order_number        VARCHAR2(30) := '';
2819         l_org_src_ref         VARCHAR2(30) := '';
2820         l_org_src_header_id   NUMBER;
2821         l_orig_po_num         VARCHAR2(50);
2822 
2823         l_est_line_rec Cs_Charge_Details_Pub.Charges_Rec_Type;
2824 
2825         CURSOR ESTIMATE(p_rep_line_id IN NUMBER) IS
2826             SELECT ced.estimate_detail_id, ced.purchase_order_num
2827               FROM CSD_REPAIR_ESTIMATE       cre,
2828                    CSD_REPAIR_ESTIMATE_LINES crel,
2829                    cs_estimate_details       ced
2830              WHERE cre.repair_line_id = p_rep_line_id
2831                AND cre.repair_estimate_id = crel.repair_estimate_id
2832                AND crel.estimate_detail_id = ced.estimate_detail_id
2833                AND ced.order_header_id IS NULL
2834                AND ced.interface_to_oe_flag = 'N';
2835 
2836     BEGIN
2837 
2838         -- Standard Start of API savepoint
2839         SAVEPOINT submit_estimate_lines;
2840 
2841         -- Standard call to check for call compatibility.
2842         IF NOT Fnd_Api.Compatible_API_Call(l_api_version,
2843                                            p_api_version,
2844                                            l_api_name,
2845                                            G_PKG_NAME)
2846         THEN
2847             RAISE Fnd_Api.G_EXC_UNEXPECTED_ERROR;
2848         END IF;
2849 
2850         -- Initialize message list if p_init_msg_list is set to TRUE.
2851         IF Fnd_Api.to_Boolean(p_init_msg_list)
2852         THEN
2853             Fnd_Msg_Pub.initialize;
2854         END IF;
2855 
2856         -- Initialize API return status to success
2857         x_return_status := Fnd_Api.G_RET_STS_SUCCESS;
2858 
2859         BEGIN
2860             SELECT original_source_header_id, original_source_reference
2861               INTO l_org_src_header_id, l_org_src_ref
2862               FROM CSD_REPAIRS
2863              WHERE repair_line_id = p_repair_line_id;
2864         EXCEPTION
2865             WHEN NO_DATA_FOUND THEN
2866                 Fnd_Message.SET_NAME('CSD', 'CSD_API_INV_REP_LINE_ID');
2867                 Fnd_Message.SET_TOKEN('REPAIR_LINE_ID', p_repair_line_id);
2868                 Fnd_Msg_Pub.ADD;
2869                 RAISE Fnd_Api.G_EXC_ERROR;
2870         END;
2871 
2872         IF l_org_src_ref = 'RMA'
2873         THEN
2874 
2875             BEGIN
2876                 SELECT header_id
2877                   INTO l_order_header_id
2878                   FROM oe_order_headers_all ooh, oe_order_types_v oot
2879                  WHERE ooh.order_type_id = oot.order_type_id
2880                    AND ooh.header_id = l_org_src_header_id
2881                    AND oot.order_category_code IN ('MIXED', 'ORDER');
2882                 l_add_to_same_order := TRUE;
2883             EXCEPTION
2884                 WHEN OTHERS THEN
2885                     l_add_to_same_order := FALSE;
2886                     IF (g_debug > 0)
2887                     THEN
2888                         Csd_Gen_Utility_Pvt.ADD('in others exception ');
2889                     END IF;
2890             END;
2891 
2892         ELSE
2893 
2894             -- Take any sales order of order type either Mixed or order
2895             -- and add to the sales order
2896             BEGIN
2897                 SELECT MAX(ced.order_header_id)
2898                   INTO l_order_header_id
2899                   FROM CSD_PRODUCT_TRANSACTIONS cpt,
2900                        cs_estimate_details      ced,
2901                        oe_order_headers_all     ooh,
2902                        oe_order_types_v         oot
2903                  WHERE ooh.order_type_id = oot.order_type_id
2904                    AND ooh.header_id = ced.order_header_id
2905                    AND oot.order_category_code IN ('MIXED', 'ORDER')
2906                    AND cpt.estimate_detail_id = ced.estimate_detail_id
2907                    AND cpt.repair_line_id = p_repair_line_id
2908                    AND ced.order_header_id IS NOT NULL
2909                    AND ced.interface_to_oe_flag = 'Y';
2910             EXCEPTION
2911                 WHEN OTHERS THEN
2912                     IF (g_debug > 0)
2913                     THEN
2914                         Csd_Gen_Utility_Pvt.ADD('in others exception ');
2915                     END IF;
2916             END;
2917 
2918             IF l_order_header_id = 0
2919             THEN
2920                 l_add_to_same_order := FALSE;
2921             ELSE
2922                 l_add_to_same_order := TRUE;
2923             END IF;
2924 
2925         END IF;
2926 
2927         -- updating the estimate with the interface_to_oe_flag
2928         FOR est IN estimate(p_repair_line_id)
2929         LOOP
2930 
2931             l_est_line_rec.estimate_detail_id   := est.estimate_detail_id;
2932             l_est_line_rec.interface_to_oe_flag := 'Y';
2933             l_last_est_detail_id                := est.estimate_detail_id;
2934 
2935             IF l_add_to_same_order
2936             THEN
2937                 l_est_line_rec.add_to_order_flag := 'Y';
2938                 IF l_org_src_ref = 'RMA'
2939                 THEN
2940                     l_est_line_rec.order_header_id := l_org_src_header_id;
2941                 ELSE
2942                     l_est_line_rec.order_header_id := l_order_header_id;
2943                 END IF;
2944                 --
2945                 --  Fix for forward port bug # 2826897
2946                 --
2947                 IF est.purchase_order_num IS NOT NULL
2948                 THEN
2949                     BEGIN
2950                         SELECT ced.purchase_order_num
2951                           INTO l_orig_po_num
2952                           FROM CSD_PRODUCT_TRANSACTIONS cpt,
2953                                cs_estimate_details      ced
2954                          WHERE cpt.estimate_detail_id =
2955                                ced.estimate_detail_id
2956                            AND cpt.repair_line_id = p_repair_line_id
2957                            AND ced.order_header_id =
2958                                l_est_line_rec.order_header_id
2959                            AND ced.purchase_order_num =
2960                                est.purchase_order_num;
2961                     EXCEPTION
2962                         WHEN NO_DATA_FOUND THEN
2963                             l_est_line_rec.add_to_order_flag := 'F';
2964                             l_est_line_rec.order_header_id   := Fnd_Api.G_MISS_NUM;
2965                         WHEN TOO_MANY_ROWS THEN
2966                             NULL;
2967                     END;
2968                 END IF;
2969             ELSE
2970                 l_est_line_rec.add_to_order_flag := 'F';
2971                 l_est_line_rec.order_header_id   := Fnd_Api.G_MISS_NUM;
2972             END IF;
2973             IF (g_debug > 0)
2974             THEN
2975                 Csd_Gen_Utility_Pvt.ADD('Updating estimate lines  with add to order flag for estimate_detail_id = ' ||
2976                                         est.estimate_detail_id);
2977             END IF;
2978 
2979             Csd_Process_Pvt.PROCESS_CHARGE_LINES(p_api_version        => 1.0,
2980                                                  p_commit             => Fnd_Api.g_false,
2981                                                  p_init_msg_list      => Fnd_Api.g_true,
2982                                                  p_validation_level   => Fnd_Api.g_valid_level_full,
2983                                                  p_action             => 'UPDATE',
2984                                                  p_Charges_Rec        => l_est_line_rec,
2985                                                  x_estimate_detail_id => l_estimate_detail_id,
2986                                                  x_return_status      => x_return_status,
2987                                                  x_msg_count          => x_msg_count,
2988                                                  x_msg_data           => x_msg_data);
2989 
2990             IF NOT (x_return_status = Fnd_Api.G_RET_STS_SUCCESS)
2991             THEN
2992                 RAISE Fnd_Api.G_EXC_ERROR;
2993             END IF;
2994 
2995         END LOOP;
2996 
2997         BEGIN
2998             SELECT cia.incident_id, cia.customer_id, cia.account_id
2999               INTO l_incident_id, l_party_id, l_account_id
3000               FROM cs_incidents_all_b cia, CSD_REPAIRS cr
3001              WHERE cia.incident_id = cr.incident_id
3002                AND cr.repair_line_id = p_repair_line_id;
3003         EXCEPTION
3004             WHEN NO_DATA_FOUND THEN
3005                 Fnd_Message.SET_NAME('CSD', 'CSD_API_INV_REP_LINE_ID');
3006                 Fnd_Message.SET_TOKEN('REPAIR_LINE_ID', p_repair_line_id);
3007                 Fnd_Msg_Pub.ADD;
3008                 RAISE Fnd_Api.G_EXC_ERROR;
3009         END;
3010 
3011         IF (g_debug > 0)
3012         THEN
3013             Csd_Gen_Utility_Pvt.ADD('Creating the Sales Order');
3014             Csd_Gen_Utility_Pvt.ADD('l_incident_id    =' || l_incident_id);
3015             Csd_Gen_Utility_Pvt.ADD('l_party_id       =' || l_party_id);
3016             Csd_Gen_Utility_Pvt.ADD('l_account_id     =' || l_account_id);
3017         END IF;
3018 
3019         Cs_Charge_Create_Order_Pub.Submit_Order(p_api_version      => 1.0,
3020                                                 p_init_msg_list    => 'T',
3021                                                 p_commit           => 'F',
3022                                                 p_validation_level => Fnd_Api.g_valid_level_full,
3023                                                 p_incident_id      => l_incident_id,
3024                                                 p_party_id         => l_party_id,
3025                                                 p_account_id       => l_account_id,
3026                                                 p_book_order_flag  => 'N',
3027                                                 x_return_status    => x_return_status,
3028                                                 x_msg_count        => x_msg_count,
3029                                                 x_msg_data         => x_msg_data);
3030 
3031         IF NOT (x_return_status = Fnd_Api.G_RET_STS_SUCCESS)
3032         THEN
3033             RAISE Fnd_Api.G_EXC_ERROR;
3034         END IF;
3035 
3036         BEGIN
3037             SELECT oe.order_number
3038               INTO l_order_number
3039               FROM oe_order_headers_all oe, cs_estimate_details ced
3040              WHERE oe.header_id = ced.order_header_id
3041                AND ced.estimate_detail_id = l_last_est_detail_id;
3042             Fnd_Message.SET_NAME('CSD', 'CSD_EST_ORDER_NUMBER');
3043             Fnd_Message.SET_TOKEN('ORDER_NUMBER', l_order_number);
3044             Fnd_Msg_Pub.ADD;
3045         EXCEPTION
3046             WHEN NO_DATA_FOUND THEN
3047                 Fnd_Message.SET_NAME('CSD', 'CSD_EST_NOT_INTERFACED');
3048                 Fnd_Msg_Pub.ADD;
3049                 IF (g_debug > 0)
3050                 THEN
3051                     Csd_Gen_Utility_Pvt.ADD('Estimate not interfaced to OM ');
3052                 END IF;
3053         END;
3054 
3055         -- Standard check of p_commit.
3056         IF Fnd_Api.To_Boolean(p_commit)
3057         THEN
3058             COMMIT WORK;
3059         END IF;
3060 
3061         -- Standard call to get message count and IF count is  get message info.
3062         Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
3063                                   p_data  => x_msg_data);
3064     EXCEPTION
3065         WHEN Fnd_Api.G_EXC_ERROR THEN
3066             x_return_status := Fnd_Api.G_RET_STS_ERROR;
3067             ROLLBACK TO submit_estimate_lines;
3068             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
3069                                       p_data  => x_msg_data);
3070         WHEN Fnd_Api.G_EXC_UNEXPECTED_ERROR THEN
3071             x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
3072             ROLLBACK TO submit_estimate_lines;
3073             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
3074                                       p_data  => x_msg_data);
3075         WHEN OTHERS THEN
3076             x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
3077             ROLLBACK TO submit_estimate_lines;
3078             IF Fnd_Msg_Pub.Check_Msg_Level(Fnd_Msg_Pub.G_MSG_LVL_UNEXP_ERROR)
3079             THEN
3080                 Fnd_Msg_Pub.Add_Exc_Msg(G_PKG_NAME, l_api_name);
3081             END IF;
3082             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
3083                                       p_data  => x_msg_data);
3084 
3085     END SUBMIT_REPAIR_ESTIMATE_LINES;
3086 
3087     --
3088     -- swai 11.5.10
3089     -- new apis
3090     --
3091     /*--------------------------------------------------*/
3092     /* procedure name: get_total_estimated_charge       */
3093     /* description   : given a repair line id, returns  */
3094     /*                 the total charge (includes any   */
3095     /*                 contract calculations)  If no    */
3096     /*                 estimate lines for the repair    */
3097     /*                 line, return null.               */
3098     /*--------------------------------------------------*/
3099 
3100     PROCEDURE get_total_estimated_charge(p_repair_line_id   IN NUMBER,
3101                                          x_estimated_charge OUT NOCOPY NUMBER,
3102                                          x_return_status    OUT NOCOPY VARCHAR2) IS
3103         lc_debug_level CONSTANT NUMBER := Fnd_Log.G_CURRENT_RUNTIME_LEVEL;
3104         lc_stat_level  CONSTANT NUMBER := Fnd_Log.LEVEL_STATEMENT;
3105         lc_proc_level  CONSTANT NUMBER := Fnd_Log.LEVEL_PROCEDURE;
3106         lc_event_level CONSTANT NUMBER := Fnd_Log.LEVEL_EVENT;
3107         lc_excep_level CONSTANT NUMBER := Fnd_Log.LEVEL_EXCEPTION;
3108         lc_error_level CONSTANT NUMBER := Fnd_Log.LEVEL_ERROR;
3109         lc_unexp_level CONSTANT NUMBER := Fnd_Log.LEVEL_UNEXPECTED;
3110         lc_mod_name    CONSTANT VARCHAR2(100) := 'csd.plsql.csd_repair_estimate_pvt.get_total_estimated_charge';
3111     BEGIN
3112         IF (Fnd_Log.Level_Procedure >= Fnd_Log.G_Current_Runtime_Level)
3113         THEN
3114             Fnd_Log.STRING(Fnd_Log.Level_Procedure,
3115                            lc_mod_name || '.BEGIN',
3116                            'Entered get_total_estimated_charge');
3117         END IF;
3118 
3119         x_return_status := Fnd_Api.G_RET_STS_SUCCESS;
3120 
3121         SELECT SUM(NVL(charge, 0))
3122           INTO x_estimated_charge
3123           FROM csd_repair_estimate_lines_v
3124          WHERE repair_line_id = p_repair_line_id
3125            AND billing_type IN ('M', 'L', 'E');
3126 
3127         IF (Fnd_Log.Level_Statement >= Fnd_Log.G_Current_Runtime_Level)
3128         THEN
3129             Fnd_Log.STRING(Fnd_Log.Level_Statement,
3130                            lc_mod_name || 'out_parameter',
3131                            'x_estimated_charge: ' || x_estimated_charge);
3132         END IF;
3133         IF (Fnd_Log.Level_Procedure >= Fnd_Log.G_Current_Runtime_Level)
3134         THEN
3135             Fnd_Log.STRING(Fnd_Log.Level_Procedure,
3136                            lc_mod_name || '.END',
3137                            'Leaving get_total_estimated_charge');
3138         END IF;
3139     EXCEPTION
3140         WHEN NO_DATA_FOUND THEN
3141             x_return_status    := Fnd_Api.G_RET_STS_ERROR;
3142             x_estimated_charge := NULL;
3143         WHEN OTHERS THEN
3144             x_return_status    := Fnd_Api.G_RET_STS_ERROR;
3145             x_estimated_charge := NULL;
3146     END; -- PROCEDURE get_total_estimated_charge
3147 
3148     /*--------------------------------------------------*/
3149     /* procedure name: Autocreate_Estimate_Lines        */
3150     /* description   : The main procedure that is       */
3151     /*                 triggered by the click of the    */
3152     /*                 button on the `Estimates' tab.   */
3153     /*                 Creates all the estimate lines   */
3154     /*                 for TASK or BOM.                 */
3155     /*                                                  */
3156     /* x_warning_flag : FND_API.G_TRUE if any non-fatal */
3157     /*                  errors occured. FND_API.G_FALSE */
3158     /*                  if everything was successful.   */
3159     /*                  Note that this value could be   */
3160     /*                  G_TRUE even if x_return_status  */
3161     /*                  is G_RET_STS_SUCCESS            */
3162     /* called from:  Depot Repair UI                    */
3163     /*--------------------------------------------------*/
3164 
3165     PROCEDURE Autocreate_Estimate_Lines(p_api_version         IN NUMBER,
3166                                         p_commit              IN VARCHAR2,
3167                                         p_init_msg_list       IN VARCHAR2,
3168                                         p_validation_level    IN NUMBER,
3169                                         x_return_status       OUT NOCOPY VARCHAR2,
3170                                         x_msg_count           OUT NOCOPY NUMBER,
3171                                         x_msg_data            OUT NOCOPY VARCHAR2,
3172                                         p_repair_line_id      IN NUMBER,
3173                                         p_estimate_id         IN NUMBER, -- required
3174                                         p_repair_type_id      IN NUMBER, -- required
3175                                         p_business_process_id IN NUMBER, -- required
3176                                         p_currency_code       IN VARCHAR2, -- required
3177                                         p_incident_id         IN NUMBER, -- required
3178                                         p_repair_mode         IN VARCHAR2, -- required
3179                                         p_inventory_item_id   IN NUMBER, -- required
3180                                         p_organization_id     IN NUMBER, -- required
3181                                         x_warning_flag        OUT NOCOPY VARCHAR2) IS
3182         -- CONSTANTS --
3183         lc_debug_level CONSTANT NUMBER := Fnd_Log.G_CURRENT_RUNTIME_LEVEL;
3184         lc_stat_level  CONSTANT NUMBER := Fnd_Log.LEVEL_STATEMENT;
3185         lc_proc_level  CONSTANT NUMBER := Fnd_Log.LEVEL_PROCEDURE;
3186         lc_event_level CONSTANT NUMBER := Fnd_Log.LEVEL_EVENT;
3187         lc_excep_level CONSTANT NUMBER := Fnd_Log.LEVEL_EXCEPTION;
3188         lc_error_level CONSTANT NUMBER := Fnd_Log.LEVEL_ERROR;
3189         lc_unexp_level CONSTANT NUMBER := Fnd_Log.LEVEL_UNEXPECTED;
3190         lc_mod_name    CONSTANT VARCHAR2(100) := 'csd.plsql.csd_repair_estimate_pvt.autocreate_estimate_lines';
3191         lc_api_name    CONSTANT VARCHAR2(30) := 'Autocreate_Estimate_Lines';
3192         lc_api_version CONSTANT NUMBER := 1.0;
3193 
3194         -- VARIABLES --
3195         l_default_contract_line_id NUMBER := NULL;
3196         l_default_price_list_id    NUMBER := NULL;
3197         l_num_service_codes        NUMBER := 0;
3198         l_contract_validated       BOOLEAN;
3199         l_est_lines_tbl            CSD_REPAIR_ESTIMATE_PVT.REPAIR_ESTIMATE_LINE_TBL;
3200         l_return_status            VARCHAR2(1);
3201         l_msg_count                NUMBER;
3202         l_msg_data                 VARCHAR2(200);
3203         l_estimate_line_id         NUMBER;
3204         l_ro_sc_rec_type           Csd_Ro_Service_Codes_Pvt.RO_SERVICE_CODE_REC_TYPE;
3205         l_obj_ver_number           NUMBER;
3206         l_prev_ro_sc_id            NUMBER;
3207         l_item_name                VARCHAR2(40);
3208         l_warning_flag             VARCHAR2(1) := Fnd_Api.G_FALSE;
3209 
3210 		--bug#11902612
3211 		l_default_po			   VARCHAR2(80);
3212 		--bug#11902612
3213 
3214         -- EXCEPTIONS --
3215         CSD_EST_SC_NO_APPL EXCEPTION; -- no service codes to apply
3216         CSD_EST_DEF_PL EXCEPTION; -- no default price list
3217         CSD_EST_INAPPL_MODE EXCEPTION; -- cannot apply service code for repair mode
3218 
3219         -- CURSORS --
3220         CURSOR c_ro_sc_obj_version(p_ro_service_code_id NUMBER) IS
3221             SELECT object_version_number
3222               FROM CSD_RO_SERVICE_CODES
3223              WHERE ro_service_code_id = p_ro_service_code_id;
3224 
3225 		--bug#11902612
3226 		CURSOR c_default_po(p_repair_line_id NUMBER) IS
3227 			SELECT default_po_num
3228 			FROM CSD_REPAIRS
3229 			WHERE repair_line_id = p_repair_line_id;
3230 		--bug#11902612
3231 
3232     BEGIN
3233         -- Standard Start of API savepoint
3234         SAVEPOINT Autocreate_Estimate_Lines;
3235 
3236         -- Standard call to check for call compatibility.
3237         IF NOT Fnd_Api.Compatible_API_Call(lc_api_version,
3238                                            p_api_version,
3239                                            lc_api_name,
3240                                            G_PKG_NAME)
3241         THEN
3242             RAISE Fnd_Api.G_EXC_UNEXPECTED_ERROR;
3243         END IF;
3244 
3245         -- Initialize message list if p_init_msg_list is set to TRUE.
3246         IF Fnd_Api.to_Boolean(p_init_msg_list)
3247         THEN
3248             Fnd_Msg_Pub.initialize;
3249         END IF;
3250 
3251         IF (Fnd_Log.Level_Procedure >= Fnd_Log.G_Current_Runtime_Level)
3252         THEN
3253             Fnd_Log.STRING(Fnd_Log.Level_Procedure,
3254                            lc_mod_name || '.BEGIN',
3255                            'Entered Autocreate_Estimate_Lines');
3256         END IF;
3257 
3258         -- log parameters
3259         IF (Fnd_Log.Level_Statement >= Fnd_Log.G_Current_Runtime_Level)
3260         THEN
3261             Fnd_Log.STRING(Fnd_Log.Level_Statement,
3262                            lc_mod_name || '.parameter_logging',
3263                            'p_api_version: ' || p_api_version);
3264             Fnd_Log.STRING(Fnd_Log.Level_Statement,
3265                            lc_mod_name || '.parameter_logging',
3266                            'p_commit: ' || p_commit);
3267             Fnd_Log.STRING(Fnd_Log.Level_Statement,
3268                            lc_mod_name || '.parameter_logging',
3269                            'p_init_msg_list: ' || p_init_msg_list);
3270             Fnd_Log.STRING(Fnd_Log.Level_Statement,
3271                            lc_mod_name || '.parameter_logging',
3272                            'p_validation_level: ' || p_validation_level);
3273             Fnd_Log.STRING(Fnd_Log.Level_Statement,
3274                            lc_mod_name || '.parameter_logging',
3275                            'p_repair_line_id: ' || p_repair_line_id);
3276             Fnd_Log.STRING(Fnd_Log.Level_Statement,
3277                            lc_mod_name || '.parameter_logging',
3278                            'p_estimate_id: ' || p_estimate_id);
3279             Fnd_Log.STRING(Fnd_Log.Level_Statement,
3280                            lc_mod_name || '.parameter_logging',
3281                            'p_repair_type_id: ' || p_repair_type_id);
3282             Fnd_Log.STRING(Fnd_Log.Level_Statement,
3283                            lc_mod_name || '.parameter_logging',
3284                            'p_business_process_id: ' ||
3285                            p_business_process_id);
3286             Fnd_Log.STRING(Fnd_Log.Level_Statement,
3287                            lc_mod_name || '.parameter_logging',
3288                            'p_currency_code: ' || p_currency_code);
3289             Fnd_Log.STRING(Fnd_Log.Level_Statement,
3290                            lc_mod_name || '.parameter_logging',
3291                            'p_incident_id: ' || p_incident_id);
3292             Fnd_Log.STRING(Fnd_Log.Level_Statement,
3293                            lc_mod_name || '.parameter_logging',
3294                            'p_repair_mode: ' || p_repair_mode);
3295             Fnd_Log.STRING(Fnd_Log.Level_Statement,
3296                            lc_mod_name || '.parameter_logging',
3297                            'p_inventory_item_id: ' || p_inventory_item_id);
3298             Fnd_Log.STRING(Fnd_Log.Level_Statement,
3299                            lc_mod_name || '.parameter_logging',
3300                            'p_organization_id: ' || p_organization_id);
3301         END IF;
3302 
3303         -- Initialize API return status to success
3304         x_return_status := Fnd_Api.G_RET_STS_SUCCESS;
3305 
3306         --
3307         -- Begin API Body
3308         --
3309 
3310         -- initialize the warning flag
3311         x_warning_flag := Fnd_Api.G_FALSE;
3312 
3313         -- Check the required parameters
3314         IF (Fnd_Log.Level_Procedure >= Fnd_Log.G_Current_Runtime_Level)
3315         THEN
3316             Fnd_Log.STRING(Fnd_Log.Level_Procedure,
3317                            lc_mod_name,
3318                            'Checking required parameters');
3319         END IF;
3320 
3321         Csd_Process_Util.Check_Reqd_Param(p_param_value => p_repair_line_id,
3322                                           p_param_name  => 'REPAIR_LINE_ID',
3323                                           p_api_name    => lc_api_name);
3324 
3325         Csd_Process_Util.Check_Reqd_Param(p_param_value => p_estimate_id,
3326                                           p_param_name  => 'ESTIMATE_ID',
3327                                           p_api_name    => lc_api_name);
3328 
3329         Csd_Process_Util.Check_Reqd_Param(p_param_value => p_repair_type_id,
3330                                           p_param_name  => 'REPAIR_TYPE_ID',
3331                                           p_api_name    => lc_api_name);
3332 
3333         Csd_Process_Util.Check_Reqd_Param(p_param_value => p_business_process_id,
3334                                           p_param_name  => 'BUSINESS_PROCESS_ID',
3335                                           p_api_name    => lc_api_name);
3336 
3337         Csd_Process_Util.Check_Reqd_Param(p_param_value => p_currency_code,
3338                                           p_param_name  => 'CURRENCY_CODE',
3339                                           p_api_name    => lc_api_name);
3340 
3341         Csd_Process_Util.Check_Reqd_Param(p_param_value => p_incident_id,
3342                                           p_param_name  => 'INCIDENT_ID',
3343                                           p_api_name    => lc_api_name);
3344 
3345         Csd_Process_Util.Check_Reqd_Param(p_param_value => p_repair_mode,
3346                                           p_param_name  => 'REPAIR_MODE',
3347                                           p_api_name    => lc_api_name);
3348 
3349         Csd_Process_Util.Check_Reqd_Param(p_param_value => p_inventory_item_id,
3350                                           p_param_name  => 'INVENTORY_ITEM_ID',
3351                                           p_api_name    => lc_api_name);
3352 
3353         Csd_Process_Util.Check_Reqd_Param(p_param_value => p_organization_id,
3354                                           p_param_name  => 'ORGANIZATION_ID',
3355                                           p_api_name    => lc_api_name);
3356 
3357         -- Check number of service codes to get mle lines for
3358         IF (Fnd_Log.Level_Procedure >= Fnd_Log.G_Current_Runtime_Level)
3359         THEN
3360             Fnd_Log.STRING(Fnd_Log.Level_Procedure,
3361                            lc_mod_name,
3362                            'Counting the number of service codes to apply');
3363         END IF;
3364         BEGIN
3365             SELECT COUNT(*)
3366               INTO l_num_service_codes
3367               FROM CSD_RO_SERVICE_CODES
3368              WHERE repair_line_id = p_repair_line_id
3369                AND NVL(applicable_flag, 'N') = 'Y'
3370                AND NVL(applied_to_est_flag, 'N') = 'N';
3371             -- Commented for bugfix 3473869.vkjain.
3372             -- and nvl(applied_to_work_flag, 'N') = 'N';
3373         EXCEPTION
3374             WHEN OTHERS THEN
3375                 l_num_service_codes := 0;
3376         END;
3377 
3378         IF (Fnd_Log.Level_Statement >= Fnd_Log.G_Current_Runtime_Level)
3379         THEN
3380             Fnd_Log.STRING(Fnd_Log.Level_Statement,
3381                            lc_mod_name,
3382                            'l_num_service_codes: ' || l_num_service_codes);
3383         END IF;
3384 
3385         -- if no applicable service codes, see if we need to
3386         -- raise an error
3387         IF (l_num_service_codes <= 0)
3388         THEN
3389             -- bug fix 3388891
3390             -- only give an error if it is wip or if
3391             -- in task mode profile option is not set to get tasks from soln
3392             IF (p_repair_mode = 'WIP') OR
3393                (NVL(Fnd_Profile.value('CSD_USE_TASK_FROM_SOLN'), 'N') = 'N')
3394             THEN
3395                 RAISE CSD_EST_SC_NO_APPL;
3396             END IF;
3397         END IF;
3398 
3399         -- Get default Contract.
3400         IF (Fnd_Log.Level_Procedure >= Fnd_Log.G_Current_Runtime_Level)
3401         THEN
3402             Fnd_Log.STRING(Fnd_Log.Level_Procedure,
3403                            lc_mod_name,
3404                            'Calling CSD_CHARGE_LINE_UTIL.Get_DefaultContract');
3405         END IF;
3406 
3407         l_default_contract_line_id := Csd_Charge_Line_Util.Get_DefaultContract(p_repair_line_id);
3408 
3409         IF (Fnd_Log.Level_Procedure >= Fnd_Log.G_Current_Runtime_Level)
3410         THEN
3411             -- log results from previous call
3412             Fnd_Log.STRING(Fnd_Log.Level_Procedure,
3413                            lc_mod_name,
3414                            'Returned from CSD_CHARGE_LINE_UTIL.Get_DefaultContract');
3415             Fnd_Log.STRING(Fnd_Log.Level_Procedure,
3416                            lc_mod_name,
3417                            'l_default_contract_line_id = ' ||
3418                            l_default_contract_line_id);
3419             -- log the next call
3420             Fnd_Log.STRING(Fnd_Log.Level_Procedure,
3421                            lc_mod_name,
3422                            'calling CSD_CHARGE_LINE_UTIL.Get_RO_PriceList');
3423         END IF;
3424 
3425         l_default_price_list_id := Csd_Charge_Line_Util.Get_RO_PriceList(p_repair_line_id);
3426         IF l_default_price_list_id IS NULL
3427         THEN
3428             RAISE CSD_EST_DEF_PL;
3429         END IF;
3430 
3431         IF (Fnd_Log.Level_Procedure >= Fnd_Log.G_Current_Runtime_Level)
3432         THEN
3433             -- log results from previous call
3434             Fnd_Log.STRING(Fnd_Log.Level_Procedure,
3435                            lc_mod_name,
3436                            'Returned from CSD_CHARGE_LINE_UTIL.Get_RO_PriceList');
3437             Fnd_Log.STRING(Fnd_Log.Level_Procedure,
3438                            lc_mod_name,
3439                            'l_default_price_list_id = ' ||
3440                            l_default_price_list_id);
3441             -- log the next call
3442             Fnd_Log.STRING(Fnd_Log.Level_Procedure,
3443                            lc_mod_name,
3444                            'Calling CSD_GEN_ERRMSGS_PVT.purge_entity_msgs');
3445         END IF;
3446 
3447         -- initialize local return status before calling procedures
3448         l_return_status := Fnd_Api.G_RET_STS_SUCCESS;
3449 
3450         -- purge any existing messages before processing
3451         -- Module ACT (source entity ESTIMATE).
3452         Csd_Gen_Errmsgs_Pvt.purge_entity_msgs(p_api_version             => 1.0,
3453                                               p_commit                  => Fnd_Api.G_TRUE,
3454                                               p_init_msg_list           => Fnd_Api.G_FALSE,
3455                                               p_validation_level        => Fnd_Api.G_VALID_LEVEL_FULL,
3456                                               p_module_code             => 'EST',
3457                                               p_source_entity_id1       => p_repair_line_id,
3458                                               p_source_entity_type_code => NULL,
3459                                               p_source_entity_id2       => NULL,
3460                                               x_return_status           => l_return_status,
3461                                               x_msg_count               => l_msg_count,
3462                                               x_msg_data                => l_msg_data);
3463         IF (Fnd_Log.Level_Procedure >= Fnd_Log.G_Current_Runtime_Level)
3464         THEN
3465             Fnd_Log.STRING(Fnd_Log.Level_Procedure,
3466                            lc_mod_name,
3467                            'Returned from CSD_GEN_ERRMSGS_PVT.purge_entity_msgs');
3468         END IF;
3469         IF (l_return_status <> Fnd_Api.G_RET_STS_SUCCESS)
3470         THEN
3471             RAISE Fnd_Api.G_EXC_ERROR;
3472         END IF;
3473 
3474         -- Now, get info for estimate lines into l_est_lines_tbl
3475         IF p_repair_mode = 'TASK'
3476         THEN
3477             IF (Fnd_Log.Level_Procedure >= Fnd_Log.G_Current_Runtime_Level)
3478             THEN
3479                 Fnd_Log.STRING(Fnd_Log.Level_Procedure,
3480                                lc_mod_name,
3481                                'Calling CSD_REPAIR_ESTIMATE_PVT.Get_Estimates_From_Task');
3482             END IF;
3483             Get_Estimates_From_Task(p_api_version         => 1.0,
3484                                     p_commit              => p_commit,
3485                                     p_init_msg_list       => Fnd_Api.G_FALSE,
3486                                     p_validation_level    => p_validation_level,
3487                                     x_return_status       => l_return_status,
3488                                     x_msg_count           => l_msg_count,
3489                                     x_msg_data            => l_msg_data,
3490                                     p_repair_line_id      => p_repair_line_id,
3491                                     p_estimate_id         => p_estimate_id,
3492                                     p_repair_type_id      => p_repair_type_id,
3493                                     p_business_process_id => p_business_process_id,
3494                                     p_currency_code       => p_currency_code,
3495                                     p_incident_id         => p_incident_id,
3496                                     p_repair_mode         => p_repair_mode,
3497                                     p_inventory_item_id   => p_inventory_item_id,
3498                                     p_organization_id     => p_organization_id,
3499                                     p_price_list_id       => l_default_price_list_id,
3500                                     p_contract_line_id    => l_default_contract_line_id,
3501                                     x_est_lines_tbl       => l_est_lines_tbl,
3502                                     x_warning_flag        => l_warning_flag);
3503             IF (Fnd_Log.Level_Procedure >= Fnd_Log.G_Current_Runtime_Level)
3504             THEN
3505                 Fnd_Log.STRING(Fnd_Log.Level_Procedure,
3506                                lc_mod_name,
3507                                'Returned from CSD_REPAIR_ESTIMATE_PVT.Get_Estimates_From_Task');
3508             END IF;
3509         ELSIF p_repair_mode = 'WIP'
3510         THEN
3511             IF (Fnd_Log.Level_Procedure >= Fnd_Log.G_Current_Runtime_Level)
3512             THEN
3513                 Fnd_Log.STRING(Fnd_Log.Level_Procedure,
3514                                lc_mod_name,
3515                                'Calling CSD_REPAIR_ESTIMATE_PVT.Get_Estimates_From_BOM');
3516             END IF;
3517             Get_Estimates_From_BOM(p_api_version         => 1.0,
3518                                    p_commit              => p_commit,
3519                                    p_init_msg_list       => Fnd_Api.G_FALSE,
3520                                    p_validation_level    => p_validation_level,
3521                                    x_return_status       => l_return_status,
3522                                    x_msg_count           => l_msg_count,
3523                                    x_msg_data            => l_msg_data,
3524                                    p_repair_line_id      => p_repair_line_id,
3525                                    p_estimate_id         => p_estimate_id,
3526                                    p_repair_type_id      => p_repair_type_id,
3527                                    p_business_process_id => p_business_process_id,
3528                                    p_currency_code       => p_currency_code,
3529                                    p_incident_id         => p_incident_id,
3530                                    p_repair_mode         => p_repair_mode,
3531                                    p_inventory_item_id   => p_inventory_item_id,
3532                                    p_organization_id     => p_organization_id,
3533                                    p_price_list_id       => l_default_price_list_id,
3534                                    p_contract_line_id    => l_default_contract_line_id,
3535                                    x_est_lines_tbl       => l_est_lines_tbl,
3536                                    x_warning_flag        => l_warning_flag);
3537             IF (Fnd_Log.Level_Procedure >= Fnd_Log.G_Current_Runtime_Level)
3538             THEN
3539                 Fnd_Log.STRING(Fnd_Log.Level_Procedure,
3540                                lc_mod_name,
3541                                'Returned from CSD_REPAIR_ESTIMATE_PVT.Get_Estimates_From_BOM');
3542             END IF;
3543         ELSE
3544             IF (Fnd_Log.Level_Statement >= Fnd_Log.G_Current_Runtime_Level)
3545             THEN
3546                 Fnd_Log.STRING(Fnd_Log.Level_Statement,
3547                                lc_mod_name,
3548                                'Unknown Repair Mode: ' || p_repair_mode);
3549             END IF;
3550             RAISE CSD_EST_INAPPL_MODE;
3551         END IF;
3552 
3553         -- Loop through the estimates table of records and insert into Estimate lines and Charge lines.
3554         IF NOT (l_return_status = Fnd_Api.G_RET_STS_SUCCESS)
3555         THEN
3556             RAISE Fnd_Api.G_EXC_ERROR;
3557         ELSE
3558             -- if any warnings were raised already, then save them
3559             -- before attempting to create estimate lines
3560             IF (l_warning_flag = Fnd_Api.G_TRUE)
3561             THEN
3562                 x_warning_flag := l_warning_flag;
3563                 IF (Fnd_Log.Level_Procedure >=
3564                    Fnd_Log.G_Current_Runtime_Level)
3565                 THEN
3566                     Fnd_Log.STRING(Fnd_Log.Level_Procedure,
3567                                    lc_mod_name,
3568                                    'Calling CSD_GEN_ERRMSGS_PVT.Save_Fnd_Msgs');
3569                     Fnd_Log.STRING(Fnd_Log.Level_Procedure,
3570                                    lc_mod_name,
3571                                    'Number of messages in stack: ' ||
3572                                    Fnd_Msg_Pub.count_msg);
3573                 END IF;
3574                 Csd_Gen_Errmsgs_Pvt.Save_Fnd_Msgs(p_api_version             => 1.0,
3575                                                   p_commit                  => Fnd_Api.G_FALSE,
3576                                                   p_init_msg_list           => Fnd_Api.G_FALSE,
3577                                                   p_validation_level        => 0,
3578                                                   p_module_code             => 'EST',
3579                                                   p_source_entity_id1       => p_repair_line_id,
3580                                                   p_source_entity_type_code => 'ESTIMATE',
3581                                                   p_source_entity_id2       => p_estimate_id,
3582                                                   x_return_status           => l_return_status,
3583                                                   x_msg_count               => l_msg_count,
3584                                                   x_msg_data                => l_msg_data);
3585                 IF NOT (l_return_status = Fnd_Api.G_RET_STS_SUCCESS)
3586                 THEN
3587                     RAISE Fnd_Api.G_EXC_ERROR;
3588                 END IF;
3589                 IF (Fnd_Log.Level_Procedure >=
3590                    Fnd_Log.G_Current_Runtime_Level)
3591                 THEN
3592                     Fnd_Log.STRING(Fnd_Log.Level_Procedure,
3593                                    lc_mod_name,
3594                                    'Returned from CSD_GEN_ERRMSGS_PVT.Save_Fnd_Msgs');
3595                     Fnd_Log.STRING(Fnd_Log.Level_Procedure,
3596                                    lc_mod_name,
3597                                    'Number of messages in stack: ' ||
3598                                    Fnd_Msg_Pub.count_msg);
3599                 END IF;
3600             END IF;
3601 
3602             IF (Fnd_Log.Level_Statement >= Fnd_Log.G_Current_Runtime_Level)
3603             THEN
3604                 Fnd_Log.STRING(Fnd_Log.Level_Statement,
3605                                lc_mod_name,
3606                                'Creating repair estimate lines');
3607             END IF;
3608 
3609 			--bug#11902612
3610 			OPEN c_default_po(p_repair_line_id);
3611 			FETCH c_default_po
3612 				INTO l_default_po;
3613 			CLOSE c_default_po;
3614 			--bug#11902612
3615 
3616             FOR i IN 1 .. l_est_lines_tbl.COUNT
3617             LOOP
3618                 BEGIN
3619                     IF (Fnd_Log.Level_Statement >=
3620                        Fnd_Log.G_Current_Runtime_Level)
3621                     THEN
3622                         Fnd_Log.STRING(Fnd_Log.Level_Statement,
3623                                        lc_mod_name,
3624                                        'Calling create_repair_estimate_lines for line ' || i);
3625                     END IF;
3626 					--bug#11902612
3627 					l_est_lines_tbl(i).purchase_order_num := l_default_po;
3628 					--bug#11902612
3629 
3630                     create_repair_estimate_lines(p_api_version       => 1.0,
3631                                                  p_commit            => Fnd_Api.G_FALSE,
3632                                                  p_init_msg_list     => Fnd_Api.G_FALSE,
3633                                                  p_validation_level  => 0,
3634                                                  x_estimate_line_rec => l_est_lines_tbl(i),
3635                                                  x_estimate_line_id  => l_estimate_line_id, -- throw away
3636                                                  x_return_status     => l_return_status,
3637                                                  x_msg_count         => l_msg_count,
3638                                                  x_msg_data          => l_msg_data);
3639                 EXCEPTION
3640                     WHEN Fnd_Api.G_EXC_ERROR THEN
3641                         l_return_status := Fnd_Api.G_RET_STS_ERROR;
3642                     WHEN OTHERS THEN
3643                         l_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
3644                 END;
3645 
3646                 IF (Fnd_Log.Level_Statement >=
3647                    Fnd_Log.G_Current_Runtime_Level)
3648                 THEN
3649                     Fnd_Log.STRING(Fnd_Log.Level_Statement,
3650                                    lc_mod_name,
3651                                    'x_estimate_line_rec = ' ||
3652                                    TO_CHAR(l_est_lines_tbl(i)
3653                                            .repair_estimate_line_id));
3654                     -- FND_LOG.STRING(Fnd_Log.Level_Procedure, lc_mod_name,
3655                     --               'x_return_status = '||l_return_status);
3656                 END IF;
3657 
3658                 -- If SC have been applied to create estimate lines successfully,
3659                 -- make sure to update CSD_RO_SERVICE_CODES table with the value `Y' for the
3660                 -- column that indicates `applied to estimates'.
3661                 IF (l_return_status = Fnd_Api.G_RET_STS_SUCCESS)
3662                 THEN
3663                     -- if previous ro_sc_id is same as current, no need to update
3664                     -- ro_service_code id, since it should have been done already
3665                     IF (NVL(l_prev_ro_sc_id, -999) <>
3666                        NVL(l_est_lines_tbl(i).RO_SERVICE_CODE_ID, -999)) AND
3667                        (l_est_lines_tbl(i).RO_SERVICE_CODE_ID IS NOT NULL)
3668                     THEN
3669 
3670                         IF (Fnd_Log.Level_Statement >=
3671                            Fnd_Log.G_Current_Runtime_Level)
3672                         THEN
3673                             Fnd_Log.STRING(Fnd_Log.Level_Statement,
3674                                            lc_mod_name,
3675                                            'l_prev_ro_sc_id = ' ||
3676                                            TO_CHAR(l_prev_ro_sc_id));
3677                             Fnd_Log.STRING(Fnd_Log.Level_Statement,
3678                                            lc_mod_name,
3679                                            'l_est_lines_tbl(i).RO_SERVICE_CODE_ID = ' ||
3680                                            TO_CHAR(l_est_lines_tbl(i)
3681                                                    .RO_SERVICE_CODE_ID));
3682                         END IF;
3683 
3684                         l_ro_sc_rec_type.ro_service_code_id  := l_est_lines_tbl(i)
3685                                                                .RO_SERVICE_CODE_ID;
3686                         l_ro_sc_rec_type.applied_to_est_flag := 'Y';
3687                         OPEN c_ro_sc_obj_version(l_est_lines_tbl(i)
3688                                                  .RO_SERVICE_CODE_ID);
3689                         FETCH c_ro_sc_obj_version
3690                             INTO l_ro_sc_rec_type.object_version_number;
3691                         CLOSE c_ro_sc_obj_version;
3692 
3693                         IF (Fnd_Log.Level_Procedure >=
3694                            Fnd_Log.G_Current_Runtime_Level)
3695                         THEN
3696                             Fnd_Log.STRING(Fnd_Log.Level_Procedure,
3697                                            lc_mod_name,
3698                                            'Calling CSD_RO_SERVICE_CODES_PVT.Update_RO_Service_Code');
3699                         END IF;
3700                         Csd_Ro_Service_Codes_Pvt.Update_RO_Service_Code(p_api_version         => 1.0,
3701                                                                         p_commit              => Fnd_Api.G_FALSE,
3702                                                                         p_init_msg_list       => Fnd_Api.G_FALSE,
3703                                                                         p_validation_level    => 0,
3704                                                                         p_ro_service_code_rec => l_ro_sc_rec_type,
3705                                                                         x_obj_ver_number      => l_obj_ver_number,
3706                                                                         x_return_status       => l_return_status,
3707                                                                         x_msg_count           => l_msg_count,
3708                                                                         x_msg_data            => l_msg_data);
3709 
3710                         IF (l_return_status = Fnd_Api.G_RET_STS_SUCCESS)
3711                         THEN
3712                             l_prev_ro_sc_id := l_est_lines_tbl(i)
3713                                               .RO_SERVICE_CODE_ID;
3714                         ELSE
3715                             -- not able to update csd_ro_service_codes table to set as applied to est
3716                             x_warning_flag := Fnd_Api.G_TRUE;
3717                             IF (Fnd_Log.Level_Statement >=
3718                                Fnd_Log.G_Current_Runtime_Level)
3719                             THEN
3720                                 Fnd_Log.STRING(Fnd_Log.Level_Statement,
3721                                                lc_mod_name,
3722                                                'Adding message CSD_EST_SC_APPLIED_ERR to FND_MSG stack');
3723                             END IF;
3724                             Fnd_Message.SET_NAME('CSD',
3725                                                  'CSD_EST_SC_APPLIED_ERR');
3726                             Fnd_Msg_Pub.ADD;
3727                             IF (Fnd_Log.Level_Procedure >=
3728                                Fnd_Log.G_Current_Runtime_Level)
3729                             THEN
3730                                 Fnd_Log.STRING(Fnd_Log.Level_Procedure,
3731                                                lc_mod_name,
3732                                                'Calling CSD_GEN_ERRMSGS_PVT.Save_Fnd_Msgs');
3733                                 Fnd_Log.STRING(Fnd_Log.Level_Procedure,
3734                                                lc_mod_name,
3735                                                'Number of messages in stack: ' ||
3736                                                Fnd_Msg_Pub.count_msg);
3737                             END IF;
3738                             Csd_Gen_Errmsgs_Pvt.Save_Fnd_Msgs(p_api_version             => 1.0,
3739                                                               p_commit                  => Fnd_Api.G_FALSE,
3740                                                               p_init_msg_list           => Fnd_Api.G_FALSE,
3741                                                               p_validation_level        => 0,
3742                                                               p_module_code             => 'EST',
3743                                                               p_source_entity_id1       => p_repair_line_id,
3744                                                               p_source_entity_type_code => l_est_lines_tbl(i)
3745                                                                                           .est_line_source_type_code,
3746                                                               p_source_entity_id2       => l_est_lines_tbl(i)
3747                                                                                           .est_line_source_id1,
3748                                                               x_return_status           => l_return_status,
3749                                                               x_msg_count               => l_msg_count,
3750                                                               x_msg_data                => l_msg_data);
3751                             IF NOT
3752                                 (l_return_status = Fnd_Api.G_RET_STS_SUCCESS)
3753                             THEN
3754                                 RAISE Fnd_Api.G_EXC_ERROR;
3755                             END IF;
3756                             IF (Fnd_Log.Level_Procedure >=
3757                                Fnd_Log.G_Current_Runtime_Level)
3758                             THEN
3759                                 Fnd_Log.STRING(Fnd_Log.Level_Procedure,
3760                                                lc_mod_name,
3761                                                'Returned from CSD_GEN_ERRMSGS_PVT.Save_Fnd_Msgs');
3762                                 Fnd_Log.STRING(Fnd_Log.Level_Procedure,
3763                                                lc_mod_name,
3764                                                'Number of messages in stack: ' ||
3765                                                Fnd_Msg_Pub.count_msg);
3766                             END IF;
3767                         END IF;
3768                     END IF;
3769                 ELSE
3770                     -- not able to create repair estimate line from record in table
3771                     -- log a warning and save the error messages.
3772                     IF (Fnd_Log.Level_Procedure >=
3773                        Fnd_Log.G_Current_Runtime_Level)
3774                     THEN
3775                         Fnd_Log.STRING(Fnd_Log.Level_Procedure,
3776                                        lc_mod_name,
3777                                        'Unable to create repair estimate line for index ' || i);
3778                     END IF;
3779 
3780                     x_warning_flag := Fnd_Api.G_TRUE;
3781                     BEGIN
3782                         SELECT concatenated_segments
3783                           INTO l_item_name
3784                           FROM mtl_system_items_kfv
3785                          WHERE inventory_item_id = l_est_lines_tbl(i)
3786                         .inventory_item_id
3787                            AND organization_id = l_est_lines_tbl(i)
3788                         .organization_id;
3789                     EXCEPTION
3790                         WHEN NO_DATA_FOUND THEN
3791                             l_item_name := TO_CHAR(l_est_lines_tbl(i)
3792                                                    .inventory_item_id);
3793                         WHEN OTHERS THEN
3794                             l_item_name := TO_CHAR(l_est_lines_tbl(i)
3795                                                    .inventory_item_id);
3796                     END;
3797 
3798                     IF (Fnd_Log.Level_Statement >=
3799                        Fnd_Log.G_Current_Runtime_Level)
3800                     THEN
3801                         Fnd_Log.STRING(Fnd_Log.Level_Statement,
3802                                        lc_mod_name,
3803                                        'Adding message CSD_EST_LINE_CREATED_ERR to FND_MSG stack');
3804                     END IF;
3805                     Fnd_Message.SET_NAME('CSD', 'CSD_EST_LINE_CREATED_ERR');
3806                     Fnd_Message.SET_TOKEN('ITEM', l_item_name);
3807                     Fnd_Msg_Pub.ADD;
3808                     IF (Fnd_Log.Level_Procedure >=
3809                        Fnd_Log.G_Current_Runtime_Level)
3810                     THEN
3811                         Fnd_Log.STRING(Fnd_Log.Level_Procedure,
3812                                        lc_mod_name,
3813                                        'Calling CSD_GEN_ERRMSGS_PVT.Save_Fnd_Msgs');
3814                         Fnd_Log.STRING(Fnd_Log.Level_Procedure,
3815                                        lc_mod_name,
3816                                        'Number of messages in stack: ' ||
3817                                        Fnd_Msg_Pub.count_msg);
3818                     END IF;
3819                     Csd_Gen_Errmsgs_Pvt.Save_Fnd_Msgs(p_api_version             => 1.0,
3820                                                       p_commit                  => Fnd_Api.G_FALSE,
3821                                                       p_init_msg_list           => Fnd_Api.G_FALSE,
3822                                                       p_validation_level        => 0,
3823                                                       p_module_code             => 'EST',
3824                                                       p_source_entity_id1       => p_repair_line_id,
3825                                                       p_source_entity_type_code => l_est_lines_tbl(i)
3826                                                                                   .est_line_source_type_code,
3827                                                       p_source_entity_id2       => l_est_lines_tbl(i)
3828                                                                                   .est_line_source_id1,
3829                                                       x_return_status           => l_return_status,
3830                                                       x_msg_count               => l_msg_count,
3831                                                       x_msg_data                => l_msg_data);
3832                     IF NOT (l_return_status = Fnd_Api.G_RET_STS_SUCCESS)
3833                     THEN
3834                         RAISE Fnd_Api.G_EXC_ERROR;
3835                     END IF;
3836                     IF (Fnd_Log.Level_Procedure >=
3837                        Fnd_Log.G_Current_Runtime_Level)
3838                     THEN
3839                         Fnd_Log.STRING(Fnd_Log.Level_Procedure,
3840                                        lc_mod_name,
3841                                        'Returned from CSD_GEN_ERRMSGS_PVT.Save_Fnd_Msgs');
3842                         Fnd_Log.STRING(Fnd_Log.Level_Procedure,
3843                                        lc_mod_name,
3844                                        'Number of messages in stack: ' ||
3845                                        Fnd_Msg_Pub.count_msg);
3846                     END IF;
3847                 END IF;
3848             END LOOP;
3849 
3850         END IF;
3851         --
3852         -- End API Body
3853         --
3854 
3855         -- Standard check of p_commit.
3856         IF Fnd_Api.To_Boolean(p_commit)
3857         THEN
3858             COMMIT WORK;
3859         END IF;
3860 
3861         -- Standard call to get message count and IF count is  get message info.
3862         Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
3863                                   p_data  => x_msg_data);
3864 
3865         IF (Fnd_Log.Level_Statement >= Fnd_Log.G_Current_Runtime_Level)
3866         THEN
3867             Fnd_Log.STRING(Fnd_Log.Level_Statement,
3868                            lc_mod_name || '.out_parameter',
3869                            'x_warning_flag: ' || x_warning_flag);
3870         END IF;
3871 
3872         IF (Fnd_Log.Level_Procedure >= Fnd_Log.G_Current_Runtime_Level)
3873         THEN
3874             Fnd_Log.STRING(Fnd_Log.Level_Procedure,
3875                            lc_mod_name || '.END',
3876                            'Leaving Autocreate_Estimate_Lines');
3877         END IF;
3878 
3879     EXCEPTION
3880         WHEN CSD_EST_SC_NO_APPL THEN
3881             ROLLBACK TO Autocreate_Estimate_Lines;
3882             -- No service codes found to apply
3883             x_return_status := Fnd_Api.G_RET_STS_ERROR;
3884             x_warning_flag  := Fnd_Api.G_FALSE;
3885 
3886             -- save message in fnd stack
3887             IF (Fnd_Log.Level_Statement >= Fnd_Log.G_Current_Runtime_Level)
3888             THEN
3889                 Fnd_Log.STRING(Fnd_Log.Level_Statement,
3890                                lc_mod_name,
3891                                'Adding message CSD_EST_SC_NO_APPL to FND_MSG stack');
3892             END IF;
3893             Fnd_Message.SET_NAME('CSD', 'CSD_EST_SC_NO_APPL');
3894             Fnd_Msg_Pub.ADD;
3895             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
3896                                       p_data  => x_msg_data);
3897 
3898             -- save message in debug log
3899             IF (Fnd_Log.Level_Exception >= Fnd_Log.G_Current_Runtime_Level)
3900             THEN
3901                 Fnd_Log.STRING(Fnd_Log.Level_Exception,
3902                                lc_mod_name,
3903                                'No service codes found to apply');
3904             END IF;
3905         WHEN CSD_EST_DEF_PL THEN
3906             -- Unable to determine default pricelist
3907             ROLLBACK TO Autocreate_Estimate_Lines;
3908             x_return_status := Fnd_Api.G_RET_STS_ERROR;
3909             x_warning_flag  := Fnd_Api.G_FALSE;
3910 
3911             -- save message in fnd stack
3912             IF (Fnd_Log.Level_Statement >= Fnd_Log.G_Current_Runtime_Level)
3913             THEN
3914                 Fnd_Log.STRING(Fnd_Log.Level_Statement,
3915                                lc_mod_name,
3916                                'Adding message CSD_EST_NO_DEF_PL to FND_MSG stack');
3917             END IF;
3918             Fnd_Message.SET_NAME('CSD', 'CSD_EST_NO_DEF_PL');
3919             Fnd_Msg_Pub.ADD;
3920             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
3921                                       p_data  => x_msg_data);
3922 
3923             -- save message in debug log
3924             IF (Fnd_Log.Level_Exception >= Fnd_Log.G_Current_Runtime_Level)
3925             THEN
3926                 Fnd_Log.STRING(Fnd_Log.Level_Exception,
3927                                lc_mod_name,
3928                                'Unable to determine default pricelist');
3929             END IF;
3930 
3931         WHEN CSD_EST_INAPPL_MODE THEN
3932             -- cannot apply service code for repair mode
3933             ROLLBACK TO Autocreate_Estimate_Lines;
3934             x_return_status := Fnd_Api.G_RET_STS_ERROR;
3935             x_warning_flag  := Fnd_Api.G_FALSE;
3936 
3937             -- save message in fnd stack
3938             IF (Fnd_Log.Level_Statement >= Fnd_Log.G_Current_Runtime_Level)
3939             THEN
3940                 Fnd_Log.STRING(Fnd_Log.Level_Statement,
3941                                lc_mod_name,
3942                                'Adding message CSD_EST_INAPPL_MODE to FND_MSG stack');
3943             END IF;
3944             DECLARE
3945                 l_repair_mode_name VARCHAR2(80) := '';
3946             BEGIN
3947                 SELECT meaning
3948                   INTO l_repair_mode_name
3949                   FROM fnd_lookups
3950                  WHERE lookup_type = 'CSD_REPAIR_MODE'
3951                    AND lookup_code = p_repair_mode;
3952                 Fnd_Message.SET_NAME('CSD', 'CSD_EST_INAPPL_MODE');
3953                 Fnd_Message.SET_TOKEN('MODE', l_repair_mode_name);
3954                 Fnd_Msg_Pub.ADD;
3955             EXCEPTION
3956                 WHEN NO_DATA_FOUND THEN
3957                     l_repair_mode_name := p_repair_mode;
3958             END;
3959             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
3960                                       p_data  => x_msg_data);
3961 
3962             -- save message in debug log
3963             IF (Fnd_Log.Level_Exception >= Fnd_Log.G_Current_Runtime_Level)
3964             THEN
3965                 Fnd_Log.STRING(Fnd_Log.Level_Exception,
3966                                lc_mod_name,
3967                                'Cannot apply service code for repair mode');
3968             END IF;
3969         WHEN Fnd_Api.G_EXC_ERROR THEN
3970             ROLLBACK TO Autocreate_Estimate_Lines;
3971             x_return_status := Fnd_Api.G_RET_STS_ERROR;
3972 
3973             /*
3974             -- TO DO: Add seeded err message
3975             -- save message in fnd stack
3976             if (Fnd_Log.Level_Statement>= Fnd_Log.G_Current_Runtime_Level) then
3977                 FND_LOG.STRING(Fnd_Log.Level_Statement, lc_mod_name,
3978                                'Adding message err_name to FND_MSG stack');
3979             end if;
3980             FND_MESSAGE.SET_NAME('CSD','err_name');
3981             FND_MESSAGE.SET_TOKEN('toke_name', 'token_value');
3982             FND_MSG_PUB.ADD;
3983             */
3984             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
3985                                       p_data  => x_msg_data);
3986 
3987             -- save message in debug log
3988             IF (Fnd_Log.Level_Exception >= Fnd_Log.G_Current_Runtime_Level)
3989             THEN
3990                 Fnd_Log.STRING(Fnd_Log.Level_Exception,
3991                                lc_mod_name,
3992                                'EXC_ERROR[' || x_msg_data || ']');
3993             END IF;
3994         WHEN Fnd_Api.G_EXC_UNEXPECTED_ERROR THEN
3995             ROLLBACK TO Autocreate_Estimate_Lines;
3996             x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
3997 
3998             -- save message in fnd stack
3999             IF Fnd_Msg_Pub.Check_Msg_Level(Fnd_Msg_Pub.G_MSG_LVL_UNEXP_ERROR)
4000             THEN
4001                 IF (Fnd_Log.Level_Statement >=
4002                    Fnd_Log.G_Current_Runtime_Level)
4003                 THEN
4004                     Fnd_Log.STRING(Fnd_Log.Level_Statement,
4005                                    lc_mod_name,
4006                                    'Adding message using FND_MSG_PUB.Add_Exc_Msg to FND_MSG stack');
4007                 END IF;
4008                 Fnd_Msg_Pub.Add_Exc_Msg(G_PKG_NAME, lc_api_name);
4009             END IF;
4010             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
4011                                       p_data  => x_msg_data);
4012 
4013             -- save message in debug log
4014             IF (Fnd_Log.Level_Exception >= Fnd_Log.G_Current_Runtime_Level)
4015             THEN
4016                 Fnd_Log.STRING(Fnd_Log.Level_Exception,
4017                                lc_mod_name,
4018                                'EXC_UNEXPECTED_ERROR[' || x_msg_data || ']');
4019             END IF;
4020         WHEN OTHERS THEN
4021             ROLLBACK TO Autocreate_Estimate_Lines;
4022             x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
4023 
4024             -- save message in fnd stack
4025             IF Fnd_Msg_Pub.Check_Msg_Level(Fnd_Msg_Pub.G_MSG_LVL_UNEXP_ERROR)
4026             THEN
4027                 IF (Fnd_Log.Level_Statement >=
4028                    Fnd_Log.G_Current_Runtime_Level)
4029                 THEN
4030                     Fnd_Log.STRING(Fnd_Log.Level_Statement,
4031                                    lc_mod_name,
4032                                    'Adding message using FND_MSG_PUB.Add_Exc_Msg to FND_MSG stack');
4033                 END IF;
4034                 Fnd_Msg_Pub.Add_Exc_Msg(G_PKG_NAME, lc_api_name);
4035             END IF;
4036             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
4037                                       p_data  => x_msg_data);
4038 
4039             -- save message in debug log
4040             IF (Fnd_Log.Level_Exception >= Fnd_Log.G_Current_Runtime_Level)
4041             THEN
4042                 -- create a seeded message
4043                 Fnd_Log.STRING(Fnd_Log.Level_Exception,
4044                                lc_mod_name,
4045                                'SQL Message[' || SQLERRM || ']');
4046             END IF;
4047 
4048     END Autocreate_Estimate_Lines;
4049 
4050     /*--------------------------------------------------*/
4051     /* procedure name: Get_Estimates_From_Task          */
4052     /* description   : Fetches ML lines for the tasks   */
4053     /*                 associated via Service Codes and */
4054     /*                 (optionally) Solution.           */
4055     /*                                                  */
4056     /* x_warning_flag : FND_API.G_TRUE if any non-fatal */
4057     /*                  errors occured. FND_API.G_FALSE */
4058     /*                  if everything was successful.   */
4059     /*                  Note that this value could be   */
4060     /*                  G_TRUE even if x_return_status  */
4061     /*                  is G_RET_STS_SUCCESS            */
4062     /* called from:  Autocreate_Estimate_Lines          */
4063     /*--------------------------------------------------*/
4064     PROCEDURE Get_Estimates_From_Task(p_api_version         IN NUMBER,
4065                                       p_commit              IN VARCHAR2,
4066                                       p_init_msg_list       IN VARCHAR2,
4067                                       p_validation_level    IN NUMBER,
4068                                       x_return_status       OUT NOCOPY VARCHAR2,
4069                                       x_msg_count           OUT NOCOPY NUMBER,
4070                                       x_msg_data            OUT NOCOPY VARCHAR2,
4071                                       p_repair_line_id      IN NUMBER,
4072                                       p_estimate_id         IN NUMBER,
4073                                       p_repair_type_id      IN NUMBER,
4074                                       p_business_process_id IN NUMBER,
4075                                       p_currency_code       IN VARCHAR2,
4076                                       p_incident_id         IN NUMBER,
4077                                       p_repair_mode         IN VARCHAR2,
4078                                       p_inventory_item_id   IN NUMBER,
4079                                       p_organization_id     IN NUMBER,
4080                                       p_price_list_id       IN NUMBER,
4081                                       p_contract_line_id    IN NUMBER,
4082                                       x_est_lines_tbl       OUT NOCOPY CSD_REPAIR_ESTIMATE_PVT.REPAIR_ESTIMATE_LINE_TBL,
4083                                       x_warning_flag        OUT NOCOPY VARCHAR2) IS
4084         -- CONSTANTS --
4085         lc_debug_level CONSTANT NUMBER := Fnd_Log.G_CURRENT_RUNTIME_LEVEL;
4086         lc_stat_level  CONSTANT NUMBER := Fnd_Log.LEVEL_STATEMENT;
4087         lc_proc_level  CONSTANT NUMBER := Fnd_Log.LEVEL_PROCEDURE;
4088         lc_event_level CONSTANT NUMBER := Fnd_Log.LEVEL_EVENT;
4089         lc_excep_level CONSTANT NUMBER := Fnd_Log.LEVEL_EXCEPTION;
4090         lc_error_level CONSTANT NUMBER := Fnd_Log.LEVEL_ERROR;
4091         lc_unexp_level CONSTANT NUMBER := Fnd_Log.LEVEL_UNEXPECTED;
4092         lc_mod_name    CONSTANT VARCHAR2(100) := 'csd.plsql.csd_repair_estimate_pvt.get_estimates_from_task';
4093         lc_api_name    CONSTANT VARCHAR2(30) := 'Get_Estimates_From_Task';
4094         lc_api_version CONSTANT NUMBER := 1.0;
4095 
4096         -- VARIABLES --
4097         l_use_tasks_from_sol      VARCHAR(1);
4098         l_labor_inventory_item_id NUMBER;
4099         l_materials_from_sc_tbl   MLE_LINES_TBL_TYPE;
4100         l_labor_from_sc_tbl       MLE_LINES_TBL_TYPE;
4101         l_materials_from_sol_tbl  MLE_LINES_TBL_TYPE;
4102         l_labor_from_sol_tbl      MLE_LINES_TBL_TYPE;
4103         l_task_threshold          NUMBER;
4104         l_warning_flag            VARCHAR2(1);
4105         l_return_status           VARCHAR2(1);
4106         l_msg_count               NUMBER;
4107         l_msg_data                VARCHAR2(2000);
4108 
4109         -- bugfix 3543135, vkjain.
4110         l_No_Eligible_Lines BOOLEAN := TRUE;
4111 
4112         -- EXCEPTIONS --
4113         CSD_EST_NO_AUTOGEN EXCEPTION;
4114 
4115         -- CURSORS --
4116 
4117         /* bugfix 3468680. vkjain
4118            The selling price should be derived via API call instead of
4119            selecting from the query.
4120            The cursors are replaced by new ones.
4121            Following changes are made to the existing cursors -
4122            -- Cursors do not accept p_price_list_id as IN parameter anymore.
4123            -- They do not select the list_price from the QP view.
4124            -- The reference to QP view is removed from the FROM part of statements.
4125            -- All joins for the QP view commented in the WHERE clause.
4126            -- The selling price is derived in the Convert_to_est_lines procedure
4127               by calling CSD_PROCESS_UTIL.GET_CHARGE_SELLING_PRICE API.
4128 
4129           -- Cursor gets:
4130           -- Information to create material lines from task groups attached to
4131           -- srvice codes linked to the repair line id.
4132           cursor c_material_lines_from_sc (p_repair_line_id NUMBER,
4133                                           p_price_list_id  NUMBER) is
4134             SELECT b.inventory_item_id,
4135                    b.primary_uom_code uom,
4136                    b.manual_quantity manual_qty,
4137                    ceil(b.rollup_quantity_used/b.rollup_times_used) rollup_qty,
4138                    a.actual_times_used times_used,
4139                    c.list_price selling_price,
4140                    d.concatenated_segments item_name,
4141                    d.comms_nl_trackable_flag,
4142                    f.txn_billing_type_id,
4143                    'SERVICE_CODE' est_line_source_type_code,
4144                    ROSC.Service_code_id est_line_source_id1,
4145                    null est_line_source_id2,
4146                    ROSC.RO_Service_code_id
4147             FROM CSD_RO_SERVICE_CODES ROSC,
4148                  CSD_SERVICE_CODES_VL SC,
4149                  CSD_SC_WORK_ENTITIES WRK,
4150                  CSP_PRODUCT_TASKS a,
4151                  CSP_TASK_PARTS  b,
4152                  QP_PRICE_LIST_LINES_V c,
4153                  MTL_SYSTEM_ITEMS_KFV d,
4154                  CSD_REPAIR_TYPES_SAR e,
4155                  CS_TXN_BILLING_TYPES f,
4156                  JTF_TASK_TEMPLATES_VL g
4157             WHERE ROSC.repair_line_id = p_repair_line_id
4158                 and ROSC.applicable_flag = 'Y'
4159                 -- and ROSC.applied_to_work_flag = 'N' -- bugfix 3473869.vkjain.
4160                 and ROSC.applied_to_est_flag = 'N'
4161                 and WRK.Service_code_id = ROSC.Service_code_id
4162                 and WRK.work_entity_type_code = 'TASK'
4163                 and SC.Service_code_Id = ROSC.Service_code_id
4164                 and nvl(SC.active_from, SYSDATE) <= SYSDATE
4165                 and nvl(SC.active_to, SYSDATE) >= SYSDATE
4166                 and g.task_group_id = WRK.work_entity_id1
4167                 and a.task_template_id = g.task_template_id
4168                 and a.product_id = p_inventory_item_id
4169                 and b.product_task_id = a.product_task_id
4170                 and (b.manual_quantity IS NOT NULL OR
4171                      b.rollup_quantity_used IS NOT NULL)
4172                 and b.substitute_item IS NULL
4173                 and c.price_list_id(+) = p_price_list_id
4174                 and c.inventory_item_id(+) = b.inventory_item_id
4175                 and c.unit_code(+) = b.primary_uom_code
4176                 and nvl(c.start_date_active, SYSDATE) <= SYSDATE  -- swai added
4177                 and nvl(c.end_date_active, SYSDATE) >= SYSDATE    -- swai added
4178                 and d.inventory_item_id = b.inventory_item_id
4179                 and d.organization_id = p_organization_id
4180                 and e.repair_type_id = p_repair_type_id
4181                 and f.txn_billing_type_id = e.txn_billing_type_id(+)
4182                 and f.billing_type = d.material_billable_flag(+)
4183                 and d.material_billable_flag IS NOT NULL;
4184 
4185           -- Cursor gets:
4186           -- Information to create labor lines from task groups attached to
4187           -- service codes linked to the repair line id.
4188           cursor c_labor_lines_from_sc (p_repair_line_id NUMBER,
4189                                         p_price_list_id  NUMBER,
4190                                         p_labor_inventory_item_id NUMBER) is
4191                 SELECT p_labor_inventory_item_id inventory_item_id,
4192                        b.planned_effort_uom uom,
4193                        b.planned_effort quantity,
4194                        c.list_price selling_price,
4195                        d.concatenated_segments item_name,
4196                        d.comms_nl_trackable_flag,
4197                        f.txn_billing_type_id,
4198                        'SERVICE_CODE' est_line_source_type_code,
4199                        ROSC.Service_code_id est_line_source_id1,
4200                        null est_line_source_id2,
4201                        ROSC.RO_Service_code_id
4202                 FROM CSD_RO_SERVICE_CODES ROSC,
4203                      CSD_SERVICE_CODES_VL SC,
4204                      CSD_SC_WORK_ENTITIES WRK,
4205                      JTF_TASK_TEMPLATES_B  b,
4206                      QP_PRICE_LIST_LINES_V c,
4207                      MTL_SYSTEM_ITEMS_KFV d,
4208                      CSD_REPAIR_TYPES_SAR e,
4209                      CS_TXN_BILLING_TYPES f,
4210                      JTF_TASK_TEMPLATES_VL g
4211                 WHERE ROSC.repair_line_id = p_repair_line_id
4212                     and ROSC.applicable_flag = 'Y'
4213                     -- and ROSC.applied_to_work_flag = 'N' -- bugfix 3473869.vkjain.
4214                     and ROSC.applied_to_est_flag = 'N'
4215                     and WRK.Service_code_id = ROSC.Service_code_id
4216                     and WRK.work_entity_type_code = 'TASK'
4217                     and SC.Service_code_Id = ROSC.Service_code_id
4218                     and nvl(SC.active_from, SYSDATE) <= SYSDATE
4219                     and nvl(SC.active_to, SYSDATE) >= SYSDATE
4220                     and g.task_group_id = WRK.work_entity_id1
4221                     and b.task_template_id = g.task_template_id
4222                     and b.planned_effort is NOT NULL
4223                     and b.planned_effort_uom is NOT NULL
4224                     and c.price_list_id(+) = p_price_list_id
4225                     and c.inventory_item_id(+) = p_labor_inventory_item_id
4226                     and c.unit_code(+) = b.planned_effort_uom
4227                     and nvl(c.start_date_active, SYSDATE) <= SYSDATE  -- swai added
4228                     and nvl(c.end_date_active, SYSDATE) >= SYSDATE    -- swai added
4229                     and d.inventory_item_id = p_labor_inventory_item_id
4230                     and d.organization_id = p_organization_id
4231                     and e.repair_type_id = p_repair_type_id
4232                     and f.txn_billing_type_id = e.txn_billing_type_id(+)
4233                     and f.billing_type = d.material_billable_flag(+)
4234                     and d.material_billable_flag IS NOT NULL;
4235 
4236 
4237           -- Cursor gets:
4238           -- Information to create material lines from task groups attached to
4239           -- solutions linked to the repair line id.
4240           cursor c_material_lines_from_sol (p_repair_line_id NUMBER,
4241                                         p_price_list_id  NUMBER,
4242                                         p_inventory_item_id NUMBER,
4243                                         p_organization_id NUMBER,
4244                                         p_repair_type_id NUMBER) is
4245                 SELECT b.inventory_item_id,
4246                        b.primary_uom_code uom,
4247                        b.manual_quantity manual_qty,
4248                        ceil(b.rollup_quantity_used/b.rollup_times_used) rollup_qty,
4249                        a.actual_times_used times_used,
4250                        c.list_price selling_price,
4251                        d.concatenated_segments item_name,
4252                        d.comms_nl_trackable_flag,
4253                        f.txn_billing_type_id,
4254                        'SOLUTION' est_line_source_type_code,
4255                        SetLinkRO.set_id est_line_source_id1,
4256                        null est_line_source_id2,
4257                        null ro_service_code_id
4258                 FROM
4259                      cs_kb_set_links SetLinkTG, --link to task group
4260                      cs_kb_set_links SetLinkRO, -- link to repair order
4261                      cs_kb_sets_vl KBSets,
4262                      jtf_task_temp_groups_vl JtfTaskTempGroupEO,
4263                      csp_product_tasks a,
4264                      csp_task_parts  b,
4265                      qp_price_list_lines_v c,
4266                      mtl_system_items_kfv d,
4267                      csd_repair_types_sar e,
4268                      cs_txn_billing_types f,
4269                      jtf_task_templates_vl g
4270                 WHERE
4271                         g.task_group_id = JtfTaskTempGroupEO.task_template_group_id
4272                     and a.task_template_id = g.task_template_id
4273                     and a.product_id = p_inventory_item_id
4274                     and b.product_task_id = a.product_task_id
4275                     and (b.manual_quantity IS NOT NULL OR
4276                          b.rollup_quantity_used IS NOT NULL)
4277                     and b.substitute_item IS NULL
4278                     and c.price_list_id(+) = p_price_list_id
4279                     and c.inventory_item_id(+) = b.inventory_item_id
4280                     and c.unit_code(+) = b.primary_uom_code
4281                     and nvl(c.start_date_active, SYSDATE) <= SYSDATE  -- swai added
4282                     and nvl(c.end_date_active, SYSDATE) >= SYSDATE    -- swai added
4283                     and d.inventory_item_id = b.inventory_item_id
4284                     and d.material_billable_flag IS NOT NULL
4285                     and d.organization_id = p_organization_id
4286                     and e.repair_type_id = p_repair_type_id
4287                     and f.txn_billing_type_id = e.txn_billing_type_id(+)
4288                     and f.billing_type = d.material_billable_flag(+)
4289                     and SetLinkRO.other_id = p_repair_line_id
4290                     and SetLinkRO.object_code = 'DR'
4291                     and SetLinkRO.link_type = 'S'
4292                     and SetLinkTG.set_id = SetLinkRO.set_id
4293                     and SetLinkTG.other_id = JtfTaskTempGroupEO.task_template_group_id
4294                     and SetLinkTG.object_code = 'CS_KB_TASK_TEMPLATE_GRP'
4295                     and JtfTaskTempGroupEO.application_id = 512  -- depot repair task groups only
4296                     and KBSets.set_id = SetLinkTG.set_id
4297                     and KBSets.status = 'PUB'               -- only published solutions
4298                     and not exists                          -- not already brought in
4299                         ( select 'x'
4300                           from  csd_repair_estimate_lines_v csd
4301                           where csd.repair_line_id = SetLinkRO.other_id
4302                             and csd.est_line_source_type_code = 'SOLUTION'
4303                             and csd.est_line_source_id1 = KBSets.set_id);
4304 
4305           -- Cursor gets:
4306           -- Information to create labor lines from task groups attached to
4307           -- solutions linked to the repair line id.
4308           cursor c_labor_lines_from_sol (p_repair_line_id NUMBER,
4309                                         p_price_list_id  NUMBER,
4310                                         p_labor_inventory_item_id NUMBER,
4311                                         p_organization_id NUMBER,
4312                                         p_repair_type_id NUMBER) is
4313                 SELECT p_labor_inventory_item_id inventory_item_id,
4314                        b.planned_effort_uom uom,
4315                        b.planned_effort quantity,
4316                        c.list_price selling_price,
4317                        d.concatenated_segments item_name,
4318                        d.comms_nl_trackable_flag,
4319                        f.txn_billing_type_id,
4320                        'SOLUTION' est_line_source_type_code,
4321                        SetLinkRO.set_id est_line_source_id1,
4322                        null est_line_source_id2,
4323                        null ro_service_code_id
4324                 FROM
4325                      cs_kb_set_links SetLinkTG, --link to task group
4326                      cs_kb_set_links SetLinkRO, -- link to repair order
4327                      cs_kb_sets_vl KBSets,
4328                      jtf_task_temp_groups_vl JtfTaskTempGroupEO,
4329                      jtf_task_templates_b  b,
4330                      qp_price_list_lines_v c,
4331                      mtl_system_items_kfv d,
4332                      csd_repair_types_sar e,
4333                      cs_txn_billing_types f,
4334                      jtf_task_templates_vl g
4335                 WHERE
4336                         SetLinkRO.other_id = p_repair_line_id
4337                     and SetLinkRO.object_code = 'DR'
4338                     and SetLinkRO.link_type = 'S'
4339                     and SetLinkTG.set_id = SetLinkRO.set_id
4340                     and SetLinkTG.other_id = JtfTaskTempGroupEO.task_template_group_id
4341                     and SetLinkTG.object_code = 'CS_KB_TASK_TEMPLATE_GRP'
4342                     and g.task_group_id = JtfTaskTempGroupEO.task_template_group_id
4343                     and b.task_template_id = g.task_template_id
4344                     and b.planned_effort is NOT NULL
4345                     and b.planned_effort_uom is NOT NULL
4346                     and c.price_list_id(+) = p_price_list_id
4347                     and c.inventory_item_id(+) = p_labor_inventory_item_id
4348                     and c.unit_code(+) = b.planned_effort_uom
4349                     and nvl(c.start_date_active, SYSDATE) <= SYSDATE  -- swai added
4350                     and nvl(c.end_date_active, SYSDATE) >= SYSDATE    -- swai added
4351                     and d.inventory_item_id = p_labor_inventory_item_id
4352                     and d.organization_id = p_organization_id
4353                     and e.repair_type_id = p_repair_type_id
4354                     and f.txn_billing_type_id = e.txn_billing_type_id(+)
4355                     and f.billing_type = d.material_billable_flag(+)
4356                     and d.material_billable_flag IS NOT NULL
4357                     and JtfTaskTempGroupEO.application_id = 512  -- depot repair task groups only
4358                     and KBSets.set_id = SetLinkTG.set_id
4359                     and KBSets.status = 'PUB'               -- only published solutions
4360                     and not exists                          -- not already brought in
4361                         ( select 'x'
4362                           from  csd_repair_estimate_lines_v csd
4363                           where csd.repair_line_id = SetLinkRO.other_id
4364                             and csd.est_line_source_type_code = 'SOLUTION'
4365                             and csd.est_line_source_id1 = KBSets.set_id);
4366         */
4367 
4368         -- Cursor gets:
4369         -- Information to create material lines from task groups attached to
4370         -- srvice codes linked to the repair line id.
4371         CURSOR c_material_lines_from_sc(p_repair_line_id NUMBER) IS
4372         -- p_price_list_id  NUMBER) is , bug 3468680, vkjain
4373             SELECT b.inventory_item_id,
4374                    b.primary_uom_code uom,
4375                    b.manual_quantity manual_qty,
4376                    CEIL(b.rollup_quantity_used / b.rollup_times_used) rollup_qty,
4377                    a.actual_times_used times_used,
4378                    -- c.list_price selling_price,
4379                    d.concatenated_segments item_name,
4380                    d.comms_nl_trackable_flag,
4381                    f.txn_billing_type_id,
4382                    'SERVICE_CODE' est_line_source_type_code,
4383                    ROSC.Service_code_id est_line_source_id1,
4384                    NULL est_line_source_id2,
4385                    ROSC.RO_Service_code_id
4386               FROM CSD_RO_SERVICE_CODES ROSC,
4387                    CSD_SERVICE_CODES_VL SC,
4388                    CSD_SC_WORK_ENTITIES WRK,
4389                    CSP_PRODUCT_TASKS    a,
4390                    CSP_TASK_PARTS       b,
4391                    -- QP_PRICE_LIST_LINES_V c,
4392                    MTL_SYSTEM_ITEMS_KFV  d,
4393                    CSD_REPAIR_TYPES_SAR  e,
4394                    CS_TXN_BILLING_TYPES  f,
4395                    JTF_TASK_TEMPLATES_VL g
4396              WHERE ROSC.repair_line_id = p_repair_line_id
4397                AND ROSC.applicable_flag = 'Y'
4398                   -- and ROSC.applied_to_work_flag = 'N' -- bugfix 3473869.vkjain.
4399                AND ROSC.applied_to_est_flag = 'N'
4400                AND WRK.Service_code_id = ROSC.Service_code_id
4401                AND WRK.work_entity_type_code = 'TASK'
4402                AND SC.Service_code_Id = ROSC.Service_code_id
4403                AND NVL(SC.active_from, SYSDATE) <= SYSDATE
4404                AND NVL(SC.active_to, SYSDATE) >= SYSDATE
4405                AND g.task_group_id = WRK.work_entity_id1
4406                AND a.task_template_id = g.task_template_id
4407                AND a.product_id = p_inventory_item_id
4408                AND b.product_task_id = a.product_task_id
4409                AND (b.manual_quantity IS NOT NULL OR
4410                    b.rollup_quantity_used IS NOT NULL)
4411                AND b.substitute_item IS NULL
4412                   -- Following lines commented by vkjain, bugfix 3468680
4413                   -- and c.price_list_id(+) = p_price_list_id
4414                   -- and c.inventory_item_id(+) = b.inventory_item_id
4415                   -- and c.unit_code(+) = b.primary_uom_code
4416                   -- and nvl(c.start_date_active, SYSDATE) <= SYSDATE  -- swai added
4417                   -- and nvl(c.end_date_active, SYSDATE) >= SYSDATE    -- swai added
4418                AND d.inventory_item_id = b.inventory_item_id
4419                AND d.organization_id = p_organization_id
4420                AND e.repair_type_id = p_repair_type_id
4421                AND f.txn_billing_type_id = e.txn_billing_type_id(+)
4422                AND f.billing_type = d.material_billable_flag(+)
4423                AND d.material_billable_flag IS NOT NULL;
4424 
4425         -- Cursor gets:
4426         -- Information to create labor lines from task groups attached to
4427         -- service codes linked to the repair line id.
4428         CURSOR c_labor_lines_from_sc(p_repair_line_id NUMBER,
4429         -- p_price_list_id  NUMBER, bug 3468680, vkjain
4430         p_labor_inventory_item_id NUMBER) IS
4431             SELECT p_labor_inventory_item_id inventory_item_id,
4432                    b.planned_effort_uom      uom,
4433                    b.planned_effort          quantity,
4434                    -- c.list_price selling_price,
4435                    d.concatenated_segments item_name,
4436                    d.comms_nl_trackable_flag,
4437                    f.txn_billing_type_id,
4438                    'SERVICE_CODE' est_line_source_type_code,
4439                    ROSC.Service_code_id est_line_source_id1,
4440                    NULL est_line_source_id2,
4441                    ROSC.RO_Service_code_id
4442               FROM CSD_RO_SERVICE_CODES ROSC,
4443                    CSD_SERVICE_CODES_VL SC,
4444                    CSD_SC_WORK_ENTITIES WRK,
4445                    JTF_TASK_TEMPLATES_B b,
4446                    -- QP_PRICE_LIST_LINES_V c,
4447                    MTL_SYSTEM_ITEMS_KFV  d,
4448                    CSD_REPAIR_TYPES_SAR  e,
4449                    CS_TXN_BILLING_TYPES  f,
4450                    JTF_TASK_TEMPLATES_VL g
4451              WHERE ROSC.repair_line_id = p_repair_line_id
4452                AND ROSC.applicable_flag = 'Y'
4453                   -- and ROSC.applied_to_work_flag = 'N' -- bugfix 3473869.vkjain.
4454                AND ROSC.applied_to_est_flag = 'N'
4455                AND WRK.Service_code_id = ROSC.Service_code_id
4456                AND WRK.work_entity_type_code = 'TASK'
4457                AND SC.Service_code_Id = ROSC.Service_code_id
4458                AND NVL(SC.active_from, SYSDATE) <= SYSDATE
4459                AND NVL(SC.active_to, SYSDATE) >= SYSDATE
4460                AND g.task_group_id = WRK.work_entity_id1
4461                AND b.task_template_id = g.task_template_id
4462                AND b.planned_effort IS NOT NULL
4463                AND b.planned_effort_uom IS NOT NULL
4464                   -- Following lines commented by vkjain, bugfix 3468680
4465                   -- and c.price_list_id(+) = p_price_list_id
4466                   -- and c.inventory_item_id(+) = p_labor_inventory_item_id
4467                   -- and c.unit_code(+) = b.planned_effort_uom
4468                   -- and nvl(c.start_date_active, SYSDATE) <= SYSDATE  -- swai added
4469                   -- and nvl(c.end_date_active, SYSDATE) >= SYSDATE    -- swai added
4470                AND d.inventory_item_id = p_labor_inventory_item_id
4471                AND d.organization_id = p_organization_id
4472                AND e.repair_type_id = p_repair_type_id
4473                AND f.txn_billing_type_id = e.txn_billing_type_id(+)
4474                AND f.billing_type = d.material_billable_flag(+)
4475                AND d.material_billable_flag IS NOT NULL;
4476 
4477         -- Cursor gets:
4478         -- Information to create material lines from task groups attached to
4479         -- solutions linked to the repair line id.
4480         CURSOR c_material_lines_from_sol(p_repair_line_id NUMBER,
4481         -- p_price_list_id  NUMBER, bug 3468680, vkjain
4482         p_inventory_item_id NUMBER, p_organization_id NUMBER, p_repair_type_id NUMBER) IS
4483             SELECT b.inventory_item_id,
4484                    b.primary_uom_code uom,
4485                    b.manual_quantity manual_qty,
4486                    CEIL(b.rollup_quantity_used / b.rollup_times_used) rollup_qty,
4487                    a.actual_times_used times_used,
4488                    -- c.list_price selling_price,
4489                    d.concatenated_segments item_name,
4490                    d.comms_nl_trackable_flag,
4491                    f.txn_billing_type_id,
4492                    'SOLUTION' est_line_source_type_code,
4493                    SetLinkRO.set_id est_line_source_id1,
4494                    NULL est_line_source_id2,
4495                    NULL ro_service_code_id
4496               FROM cs_kb_set_links         SetLinkTG, --link to task group
4497                    cs_kb_set_links         SetLinkRO, -- link to repair order
4498                    cs_kb_sets_vl           KBSets,
4499                    jtf_task_temp_groups_vl JtfTaskTempGroupEO,
4500                    csp_product_tasks       a,
4501                    csp_task_parts          b,
4502                    -- qp_price_list_lines_v c,
4503                    mtl_system_items_kfv  d,
4504                    CSD_REPAIR_TYPES_SAR  e,
4505                    cs_txn_billing_types  f,
4506                    jtf_task_templates_vl g
4507              WHERE g.task_group_id =
4508                    JtfTaskTempGroupEO.task_template_group_id
4509                AND a.task_template_id = g.task_template_id
4510                AND a.product_id = p_inventory_item_id
4511                AND b.product_task_id = a.product_task_id
4512                AND (b.manual_quantity IS NOT NULL OR
4513                    b.rollup_quantity_used IS NOT NULL)
4514                AND b.substitute_item IS NULL
4515                   -- Following lines commented by vkjain, bugfix 3468680
4516                   -- and c.price_list_id(+) = p_price_list_id
4517                   -- and c.inventory_item_id(+) = b.inventory_item_id
4518                   -- and c.unit_code(+) = b.primary_uom_code
4519                   -- and nvl(c.start_date_active, SYSDATE) <= SYSDATE  -- swai added
4520                   -- and nvl(c.end_date_active, SYSDATE) >= SYSDATE    -- swai added
4521                AND d.inventory_item_id = b.inventory_item_id
4522                AND d.material_billable_flag IS NOT NULL
4523                AND d.organization_id = p_organization_id
4524                AND e.repair_type_id = p_repair_type_id
4525                AND f.txn_billing_type_id = e.txn_billing_type_id(+)
4526                AND f.billing_type = d.material_billable_flag(+)
4527                AND SetLinkRO.other_id = p_repair_line_id
4528                AND SetLinkRO.object_code = 'DR'
4529                AND SetLinkRO.link_type = 'S'
4530                AND SetLinkTG.set_id = SetLinkRO.set_id
4531                AND SetLinkTG.other_id =
4532                    JtfTaskTempGroupEO.task_template_group_id
4533                AND SetLinkTG.object_code = 'CS_KB_TASK_TEMPLATE_GRP'
4534                AND JtfTaskTempGroupEO.application_id = 512 -- depot repair task groups only
4535                AND KBSets.set_id = SetLinkTG.set_id
4536                AND KBSets.status = 'PUB' -- only published solutions
4537                AND NOT EXISTS -- not already brought in
4538              (SELECT 'x'
4539                       FROM csd_repair_estimate_lines_v csd
4540                      WHERE csd.repair_line_id = SetLinkRO.other_id
4541                        AND csd.est_line_source_type_code = 'SOLUTION'
4542                        AND csd.est_line_source_id1 = KBSets.set_id);
4543 
4544         -- Cursor gets:
4545         -- Information to create labor lines from task groups attached to
4546         -- solutions linked to the repair line id.
4547         CURSOR c_labor_lines_from_sol(p_repair_line_id NUMBER,
4548         -- p_price_list_id  NUMBER, bug 3468680, vkjain
4549         p_labor_inventory_item_id NUMBER, p_organization_id NUMBER, p_repair_type_id NUMBER) IS
4550             SELECT p_labor_inventory_item_id inventory_item_id,
4551                    b.planned_effort_uom      uom,
4552                    b.planned_effort          quantity,
4553                    -- c.list_price selling_price,
4554                    d.concatenated_segments item_name,
4555                    d.comms_nl_trackable_flag,
4556                    f.txn_billing_type_id,
4557                    'SOLUTION' est_line_source_type_code,
4558                    SetLinkRO.set_id est_line_source_id1,
4559                    NULL est_line_source_id2,
4560                    NULL ro_service_code_id
4561               FROM cs_kb_set_links         SetLinkTG, --link to task group
4562                    cs_kb_set_links         SetLinkRO, -- link to repair order
4563                    cs_kb_sets_vl           KBSets,
4564                    jtf_task_temp_groups_vl JtfTaskTempGroupEO,
4565                    jtf_task_templates_b    b,
4566                    -- qp_price_list_lines_v c,
4567                    mtl_system_items_kfv  d,
4568                    CSD_REPAIR_TYPES_SAR  e,
4569                    cs_txn_billing_types  f,
4570                    jtf_task_templates_vl g
4571              WHERE SetLinkRO.other_id = p_repair_line_id
4572                AND SetLinkRO.object_code = 'DR'
4573                AND SetLinkRO.link_type = 'S'
4574                AND SetLinkTG.set_id = SetLinkRO.set_id
4575                AND SetLinkTG.other_id =
4576                    JtfTaskTempGroupEO.task_template_group_id
4577                AND SetLinkTG.object_code = 'CS_KB_TASK_TEMPLATE_GRP'
4578                AND g.task_group_id =
4579                    JtfTaskTempGroupEO.task_template_group_id
4580                AND b.task_template_id = g.task_template_id
4581                AND b.planned_effort IS NOT NULL
4582                AND b.planned_effort_uom IS NOT NULL
4583                   -- Following lines commented by vkjain, bugfix 3468680
4584                   -- and c.price_list_id(+) = p_price_list_id
4585                   -- and c.inventory_item_id(+) = p_labor_inventory_item_id
4586                   -- and c.unit_code(+) = b.planned_effort_uom
4587                   -- and nvl(c.start_date_active, SYSDATE) <= SYSDATE  -- swai added
4588                   -- and nvl(c.end_date_active, SYSDATE) >= SYSDATE    -- swai added
4589                AND d.inventory_item_id = p_labor_inventory_item_id
4590                AND d.organization_id = p_organization_id
4591                AND e.repair_type_id = p_repair_type_id
4592                AND f.txn_billing_type_id = e.txn_billing_type_id(+)
4593                AND f.billing_type = d.material_billable_flag(+)
4594                AND d.material_billable_flag IS NOT NULL
4595                AND JtfTaskTempGroupEO.application_id = 512 -- depot repair task groups only
4596                AND KBSets.set_id = SetLinkTG.set_id
4597                AND KBSets.status = 'PUB' -- only published solutions
4598                AND NOT EXISTS -- not already brought in
4599              (SELECT 'x'
4600                       FROM csd_repair_estimate_lines_v csd
4601                      WHERE csd.repair_line_id = SetLinkRO.other_id
4602                        AND csd.est_line_source_type_code = 'SOLUTION'
4603                        AND csd.est_line_source_id1 = KBSets.set_id);
4604 
4605         -- Cursor gets:
4606         -- Tasks template groups associated with solutions for a repair line
4607         -- where the task template groups do not have any task templates
4608         -- defined for the repair line's product
4609         CURSOR c_sol_tasks_no_prod(p_repair_line_id NUMBER) IS
4610             SELECT KBSets.set_id                             solution_id, -- solution id
4611                    JtfTaskTempGroupEO.task_template_group_id task_group_id, -- task template group id
4612                    JtfTaskTempGroupEO.template_group_name    task_group_name -- task template group name
4613               FROM cs_kb_set_links         SetLinkTG, --link to task group
4614                    cs_kb_set_links         SetLinkRO, -- link to repair order
4615                    cs_kb_sets_vl           KBSets,
4616                    jtf_task_temp_groups_vl JtfTaskTempGroupEO,
4617                    CSD_REPAIRS             repairs
4618              WHERE SetLinkRO.other_id = p_repair_line_id
4619                AND SetLinkRO.object_code = 'DR'
4620                AND SetLinkRO.link_type = 'S'
4621                AND SetLinkTG.set_id = SetLinkRO.set_id
4622                AND SetLinkTG.other_id =
4623                    JtfTaskTempGroupEO.task_template_group_id
4624                AND SetLinkTG.object_code = 'CS_KB_TASK_TEMPLATE_GRP'
4625                AND JtfTaskTempGroupEO.application_id = 512 -- depot repair task groups only
4626                AND KBSets.set_id = SetLinkTG.set_id
4627                AND KBSets.status = 'PUB' -- only published solutions
4628                AND repairs.repair_line_id = SetLinkRO.other_id
4629                AND NOT EXISTS -- not already brought in
4630              (SELECT 'x'
4631                       FROM csd_repair_estimate_lines_v csd
4632                      WHERE csd.repair_line_id = SetLinkRO.other_id
4633                        AND csd.est_line_source_type_code = 'SOLUTION'
4634                        AND csd.est_line_source_id1 = KBSets.set_id)
4635                AND NOT EXISTS -- no template for this product
4636              (SELECT 'x'
4637                       FROM csp_product_tasks       x,
4638                            jtf_task_templates_vl   y,
4639                            jtf_task_temp_groups_vl z
4640                      WHERE x.product_id = repairs.inventory_item_id
4641                        AND x.task_template_id = y.task_template_id
4642                        AND y.task_group_id = SetLinkTG.other_id);
4643 
4644     BEGIN
4645         -- Standard Start of API savepoint
4646         SAVEPOINT Get_Estimates_From_Task;
4647 
4648         -- Standard call to check for call compatibility.
4649         IF NOT Fnd_Api.Compatible_API_Call(lc_api_version,
4650                                            p_api_version,
4651                                            lc_api_name,
4652                                            G_PKG_NAME)
4653         THEN
4654             RAISE Fnd_Api.G_EXC_UNEXPECTED_ERROR;
4655         END IF;
4656 
4657         -- Initialize message list if p_init_msg_list is set to TRUE.
4658         IF Fnd_Api.to_Boolean(p_init_msg_list)
4659         THEN
4660             Fnd_Msg_Pub.initialize;
4661         END IF;
4662 
4663         -- Initialize API return status to success
4664         x_return_status := Fnd_Api.G_RET_STS_SUCCESS;
4665 
4666         -- initialize the warning flag
4667         x_warning_flag := Fnd_Api.G_FALSE;
4668 
4669         --
4670         -- Begin API Body
4671         --
4672         IF (Fnd_Log.Level_Procedure >= Fnd_Log.G_Current_Runtime_Level)
4673         THEN
4674             Fnd_Log.STRING(Fnd_Log.Level_Procedure,
4675                            lc_mod_name || '.BEGIN',
4676                            'Entering CSD_REPAIR_ESTIMATE_PVT.Get_Estimates_From_Task');
4677         END IF;
4678         -- log parameters
4679         IF (Fnd_Log.Level_Statement >= Fnd_Log.G_Current_Runtime_Level)
4680         THEN
4681             Fnd_Log.STRING(Fnd_Log.Level_Statement,
4682                            lc_mod_name || '.parameter_logging',
4683                            'p_api_version: ' || p_api_version);
4684             Fnd_Log.STRING(Fnd_Log.Level_Statement,
4685                            lc_mod_name || '.parameter_logging',
4686                            'p_commit: ' || p_commit);
4687             Fnd_Log.STRING(Fnd_Log.Level_Statement,
4688                            lc_mod_name || '.parameter_logging',
4689                            'p_init_msg_list: ' || p_init_msg_list);
4690             Fnd_Log.STRING(Fnd_Log.Level_Statement,
4691                            lc_mod_name || '.parameter_logging',
4692                            'p_validation_level: ' || p_validation_level);
4693             Fnd_Log.STRING(Fnd_Log.Level_Statement,
4694                            lc_mod_name || '.parameter_logging',
4695                            'p_repair_line_id: ' || p_repair_line_id);
4696             Fnd_Log.STRING(Fnd_Log.Level_Statement,
4697                            lc_mod_name || '.parameter_logging',
4698                            'p_estimate_id: ' || p_estimate_id);
4699             Fnd_Log.STRING(Fnd_Log.Level_Statement,
4700                            lc_mod_name || '.parameter_logging',
4701                            'p_repair_type_id: ' || p_repair_type_id);
4702             Fnd_Log.STRING(Fnd_Log.Level_Statement,
4703                            lc_mod_name || '.parameter_logging',
4704                            'p_business_process_id: ' ||
4705                            p_business_process_id);
4706             Fnd_Log.STRING(Fnd_Log.Level_Statement,
4707                            lc_mod_name || '.parameter_logging',
4708                            'p_currency_code: ' || p_currency_code);
4709             Fnd_Log.STRING(Fnd_Log.Level_Statement,
4710                            lc_mod_name || '.parameter_logging',
4711                            'p_incident_id: ' || p_incident_id);
4712             Fnd_Log.STRING(Fnd_Log.Level_Statement,
4713                            lc_mod_name || '.parameter_logging',
4714                            'p_repair_mode: ' || p_repair_mode);
4715             Fnd_Log.STRING(Fnd_Log.Level_Statement,
4716                            lc_mod_name || '.parameter_logging',
4717                            'p_inventory_item_id: ' || p_inventory_item_id);
4718             Fnd_Log.STRING(Fnd_Log.Level_Statement,
4719                            lc_mod_name || '.parameter_logging',
4720                            'p_organization_id: ' || p_organization_id);
4721             Fnd_Log.STRING(Fnd_Log.Level_Statement,
4722                            lc_mod_name || '.parameter_logging',
4723                            'p_price_list_id: ' || p_price_list_id);
4724             Fnd_Log.STRING(Fnd_Log.Level_Statement,
4725                            lc_mod_name || '.parameter_logging',
4726                            'p_contract_line_id: ' || p_contract_line_id);
4727         END IF;
4728 
4729         -- get labor inventory item from profile option
4730         l_labor_inventory_item_id := Fnd_Profile.value('CSD_DEFAULT_EST_LABOR');
4731 
4732         -- get use tasks from solution profile option
4733         l_use_tasks_from_sol := NVL(Fnd_Profile.value('CSD_USE_TASK_FROM_SOLN'),
4734                                     'N');
4735 
4736         -- get threshold for tasks - to be used when determining manual vs rollup qty
4737         l_task_threshold := TO_NUMBER(NVL(Fnd_Profile.value('CSP_PROD_TASK_HIST_RULE'),
4738                                           '0'));
4739 
4740         IF (Fnd_Log.Level_Statement >= Fnd_Log.G_Current_Runtime_Level)
4741         THEN
4742             Fnd_Log.STRING(Fnd_Log.Level_Statement,
4743                            lc_mod_name,
4744                            'l_labor_inventory_item_id = ' ||
4745                            l_labor_inventory_item_id);
4746             Fnd_Log.STRING(Fnd_Log.Level_Statement,
4747                            lc_mod_name,
4748                            'l_use_tasks_from_sol = ' ||
4749                            l_use_tasks_from_sol);
4750             Fnd_Log.STRING(Fnd_Log.Level_Statement,
4751                            lc_mod_name,
4752                            'l_task_threshold = ' || l_task_threshold);
4753         END IF;
4754 
4755         --
4756         -- (1) Material lines from Service Codes
4757         --
4758         DECLARE
4759             l_count NUMBER := 0;
4760         BEGIN
4761             IF (Fnd_Log.Level_Procedure >= Fnd_Log.G_Current_Runtime_Level)
4762             THEN
4763                 Fnd_Log.STRING(Fnd_Log.Level_Procedure,
4764                                lc_mod_name,
4765                                'Getting task material lines from service codes');
4766             END IF;
4767             -- bugfix 3468680, vkjain
4768             -- FOR r1 in c_material_lines_from_sc (p_repair_line_id, p_price_list_id)
4769             FOR r1 IN c_material_lines_from_sc(p_repair_line_id)
4770             LOOP
4771                 IF (r1.manual_qty IS NOT NULL) OR
4772                    (l_task_threshold <= r1.times_used)
4773                 THEN
4774                     l_count := l_count + 1;
4775                     IF (r1.manual_qty IS NOT NULL)
4776                     THEN
4777                         l_materials_from_sc_tbl(l_count).quantity := r1.manual_qty;
4778                     ELSE
4779                         -- if (l_task_threshold <= r1.times_used)
4780                         l_materials_from_sc_tbl(l_count).quantity := r1.rollup_qty;
4781                     END IF;
4782                     l_materials_from_sc_tbl(l_count).inventory_item_id := r1.inventory_item_id;
4783                     l_materials_from_sc_tbl(l_count).uom := r1.uom;
4784                     -- Bugfix 3468680, vkjain
4785                     -- l_materials_from_sc_tbl(l_count).selling_price := r1.selling_price;
4786                     l_materials_from_sc_tbl(l_count).item_name := r1.item_name;
4787                     l_materials_from_sc_tbl(l_count).comms_nl_trackable_flag := r1.comms_nl_trackable_flag;
4788                     l_materials_from_sc_tbl(l_count).txn_billing_type_id := r1.txn_billing_type_id;
4789                     l_materials_from_sc_tbl(l_count).est_line_source_type_code := r1.est_line_source_type_code;
4790                     l_materials_from_sc_tbl(l_count).est_line_source_id1 := r1.est_line_source_id1;
4791                     l_materials_from_sc_tbl(l_count).est_line_source_id2 := r1.est_line_source_id2;
4792                     l_materials_from_sc_tbl(l_count).ro_service_code_id := r1.ro_service_code_id;
4793                 END IF;
4794             END LOOP;
4795         END;
4796 
4797         IF (Fnd_Log.Level_Procedure >= Fnd_Log.G_Current_Runtime_Level)
4798         THEN
4799             Fnd_Log.STRING(Fnd_Log.Level_Procedure,
4800                            lc_mod_name,
4801                            'material line count = ' ||
4802                            l_materials_from_sc_tbl.COUNT);
4803         END IF;
4804         IF (l_materials_from_sc_tbl.COUNT > 0)
4805         THEN
4806             -- bugfix 3543135, vkjain.
4807             l_No_Eligible_Lines := FALSE;
4808 
4809             Convert_To_Est_Lines(p_api_version         => 1.0,
4810                                  p_commit              => Fnd_Api.G_FALSE,
4811                                  p_init_msg_list       => Fnd_Api.G_FALSE,
4812                                  p_validation_level    => p_validation_level,
4813                                  x_return_status       => x_return_status,
4814                                  x_msg_count           => x_msg_count,
4815                                  x_msg_data            => x_msg_data,
4816                                  p_repair_line_id      => p_repair_line_id,
4817                                  p_estimate_id         => p_estimate_id,
4818                                  p_repair_type_id      => p_repair_type_id,
4819                                  p_business_process_id => p_business_process_id,
4820                                  p_currency_code       => p_currency_code,
4821                                  p_incident_id         => p_incident_id,
4822                                  p_organization_id     => p_organization_id,
4823                                  p_price_list_id       => p_price_list_id,
4824                                  p_contract_line_id    => p_contract_line_id,
4825                                  p_MLE_lines_tbl       => l_materials_from_sc_tbl,
4826                                  x_est_lines_tbl       => x_est_lines_tbl,
4827                                  x_warning_flag        => l_warning_flag);
4828             IF NOT (x_return_status = Fnd_Api.G_RET_STS_SUCCESS)
4829             THEN
4830                 x_warning_flag := Fnd_Api.G_TRUE;
4831             END IF;
4832             IF l_warning_flag <> Fnd_Api.G_FALSE
4833             THEN
4834                 x_warning_flag := l_warning_flag;
4835             END IF;
4836         END IF;
4837 
4838         --
4839         -- (2) Labor lines from Service Codes
4840         --
4841         IF (l_labor_inventory_item_id IS NOT NULL)
4842         THEN
4843             DECLARE
4844                 l_count NUMBER := 0;
4845             BEGIN
4846                 IF (Fnd_Log.Level_Procedure >=
4847                    Fnd_Log.G_Current_Runtime_Level)
4848                 THEN
4849                     Fnd_Log.STRING(Fnd_Log.Level_Procedure,
4850                                    lc_mod_name,
4851                                    'Getting task labor lines from service codes');
4852                 END IF;
4853                 -- bugfix 3468680, vkjain
4854                 -- FOR r1 in c_labor_lines_from_sc (p_repair_line_id, p_price_list_id, l_labor_inventory_item_id)
4855                 FOR r1 IN c_labor_lines_from_sc(p_repair_line_id,
4856                                                 l_labor_inventory_item_id)
4857                 LOOP
4858                     l_count := l_count + 1;
4859                     l_labor_from_sc_tbl(l_count).inventory_item_id := r1.inventory_item_id;
4860                     l_labor_from_sc_tbl(l_count).uom := r1.uom;
4861                     l_labor_from_sc_tbl(l_count).quantity := r1.quantity;
4862                     -- Bugfix 3468680, vkjain
4863                     -- l_labor_from_sc_tbl(l_count).selling_price := r1.selling_price;
4864                     l_labor_from_sc_tbl(l_count).item_name := r1.item_name;
4865                     l_labor_from_sc_tbl(l_count).comms_nl_trackable_flag := r1.comms_nl_trackable_flag;
4866                     l_labor_from_sc_tbl(l_count).txn_billing_type_id := r1.txn_billing_type_id;
4867                     l_labor_from_sc_tbl(l_count).est_line_source_type_code := r1.est_line_source_type_code;
4868                     l_labor_from_sc_tbl(l_count).est_line_source_id1 := r1.est_line_source_id1;
4869                     l_labor_from_sc_tbl(l_count).est_line_source_id2 := r1.est_line_source_id2;
4870                     l_labor_from_sc_tbl(l_count).ro_service_code_id := r1.ro_service_code_id;
4871                 END LOOP;
4872             END;
4873 
4874             IF (Fnd_Log.Level_Procedure >= Fnd_Log.G_Current_Runtime_Level)
4875             THEN
4876                 Fnd_Log.STRING(Fnd_Log.Level_Procedure,
4877                                lc_mod_name,
4878                                'labor line count = ' ||
4879                                l_labor_from_sc_tbl.COUNT);
4880             END IF;
4881             IF (l_labor_from_sc_tbl.COUNT > 0)
4882             THEN
4883                 -- bugfix 3543135, vkjain.
4884                 l_No_Eligible_Lines := FALSE;
4885 
4886                 Convert_To_Est_Lines(p_api_version         => 1.0,
4887                                      p_commit              => Fnd_Api.G_FALSE,
4888                                      p_init_msg_list       => Fnd_Api.G_FALSE,
4889                                      p_validation_level    => p_validation_level,
4890                                      x_return_status       => x_return_status,
4891                                      x_msg_count           => x_msg_count,
4892                                      x_msg_data            => x_msg_data,
4893                                      p_repair_line_id      => p_repair_line_id,
4894                                      p_estimate_id         => p_estimate_id,
4895                                      p_repair_type_id      => p_repair_type_id,
4896                                      p_business_process_id => p_business_process_id,
4897                                      p_currency_code       => p_currency_code,
4898                                      p_incident_id         => p_incident_id,
4899                                      p_organization_id     => p_organization_id,
4900                                      p_price_list_id       => p_price_list_id,
4901                                      p_contract_line_id    => p_contract_line_id,
4902                                      p_MLE_lines_tbl       => l_labor_from_sc_tbl,
4903                                      x_est_lines_tbl       => x_est_lines_tbl,
4904                                      x_warning_flag        => l_warning_flag);
4905                 IF NOT (x_return_status = Fnd_Api.G_RET_STS_SUCCESS)
4906                 THEN
4907                     x_warning_flag := Fnd_Api.G_TRUE;
4908                 END IF;
4909                 IF l_warning_flag <> Fnd_Api.G_FALSE
4910                 THEN
4911                     x_warning_flag := l_warning_flag;
4912                 END IF;
4913             END IF;
4914         END IF;
4915 
4916         --
4917         -- Use tasks associated with solutions
4918         --
4919         IF (l_use_tasks_from_sol = 'Y')
4920         THEN
4921             --
4922             -- Before getting any material lines from Solutions, check to
4923             -- see if there are any task groups with no task templates for the
4924             -- repair line's product.  Log a warning for each task group.
4925             --
4926             DECLARE
4927                 l_return_status VARCHAR2(1);
4928                 l_msg_count     NUMBER;
4929                 l_msg_data      VARCHAR2(2000);
4930             BEGIN
4931                 IF (Fnd_Log.Level_Procedure >=
4932                    Fnd_Log.G_Current_Runtime_Level)
4933                 THEN
4934                     Fnd_Log.STRING(Fnd_Log.Level_Procedure,
4935                                    lc_mod_name,
4936                                    'Getting all solution tasks with no product template');
4937                 END IF;
4938                 FOR r1 IN c_sol_tasks_no_prod(p_repair_line_id)
4939                 LOOP
4940                     x_warning_flag := Fnd_Api.G_TRUE;
4941                     IF (Fnd_Log.Level_Statement >=
4942                        Fnd_Log.G_Current_Runtime_Level)
4943                     THEN
4944                         Fnd_Log.STRING(Fnd_Log.Level_Statement,
4945                                        lc_mod_name,
4946                                        'Adding message CSD_EST_SOL_TASK_NO_PROD to FND_MSG stack');
4947                         Fnd_Log.STRING(Fnd_Log.Level_Statement,
4948                                        lc_mod_name,
4949                                        'no task template for task group id ' ||
4950                                        r1.task_group_id);
4951                     END IF;
4952                     Fnd_Message.SET_NAME('CSD', 'CSD_EST_SOL_TASK_NO_PROD');
4953                     Fnd_Message.SET_TOKEN('GROUP_ID', r1.task_group_id);
4954                     Fnd_Message.SET_TOKEN('GROUP_NAME', r1.task_group_name);
4955                     Fnd_Msg_Pub.ADD;
4956                     IF (Fnd_Log.Level_Procedure >=
4957                        Fnd_Log.G_Current_Runtime_Level)
4958                     THEN
4959                         Fnd_Log.STRING(Fnd_Log.Level_Procedure,
4960                                        lc_mod_name,
4961                                        'Calling CSD_GEN_ERRMSGS_PVT.Save_Fnd_Msgs');
4962                         Fnd_Log.STRING(Fnd_Log.Level_Procedure,
4963                                        lc_mod_name,
4964                                        'Number of messages in stack: ' ||
4965                                        Fnd_Msg_Pub.count_msg);
4966                     END IF;
4967                     Csd_Gen_Errmsgs_Pvt.Save_Fnd_Msgs(p_api_version             => 1.0,
4968                                                       p_commit                  => Fnd_Api.G_FALSE,
4969                                                       p_init_msg_list           => Fnd_Api.G_FALSE,
4970                                                       p_validation_level        => 0,
4971                                                       p_module_code             => 'EST',
4972                                                       p_source_entity_id1       => p_repair_line_id,
4973                                                       p_source_entity_type_code => 'SOLUTION',
4974                                                       p_source_entity_id2       => r1.solution_id,
4975                                                       x_return_status           => l_return_status,
4976                                                       x_msg_count               => l_msg_count,
4977                                                       x_msg_data                => l_msg_data);
4978                     IF NOT (l_return_status = Fnd_Api.G_RET_STS_SUCCESS)
4979                     THEN
4980                         RAISE Fnd_Api.G_EXC_ERROR;
4981                     END IF;
4982                     IF (Fnd_Log.Level_Procedure >=
4983                        Fnd_Log.G_Current_Runtime_Level)
4984                     THEN
4985                         Fnd_Log.STRING(Fnd_Log.Level_Procedure,
4986                                        lc_mod_name,
4987                                        'Returned from CSD_GEN_ERRMSGS_PVT.Save_Fnd_Msgs');
4988                         Fnd_Log.STRING(Fnd_Log.Level_Procedure,
4989                                        lc_mod_name,
4990                                        'Number of messages in stack: ' ||
4991                                        Fnd_Msg_Pub.count_msg);
4992                     END IF;
4993                 END LOOP;
4994             END;
4995 
4996             --
4997             -- (3) Material lines from Solutions
4998             --
4999             DECLARE
5000                 l_count NUMBER := 0;
5001             BEGIN
5002                 IF (Fnd_Log.Level_Procedure >=
5003                    Fnd_Log.G_Current_Runtime_Level)
5004                 THEN
5005                     Fnd_Log.STRING(Fnd_Log.Level_Procedure,
5006                                    lc_mod_name,
5007                                    'Getting task material lines from solutions');
5008                 END IF;
5009                 FOR r1 IN c_material_lines_from_sol(p_repair_line_id,
5010                                                     -- p_price_list_id, bugfix 3468680, vkjain
5011                                                     p_inventory_item_id,
5012                                                     p_organization_id,
5013                                                     p_repair_type_id)
5014                 LOOP
5015                     IF (r1.manual_qty IS NOT NULL) OR
5016                        (l_task_threshold <= r1.times_used)
5017                     THEN
5018                         l_count := l_count + 1;
5019                         IF (r1.manual_qty IS NOT NULL)
5020                         THEN
5021                             l_materials_from_sol_tbl(l_count).quantity := r1.manual_qty;
5022                         ELSE
5023                             -- if (l_task_threshold <= r1.times_used)
5024                             l_materials_from_sol_tbl(l_count).quantity := r1.rollup_qty;
5025                         END IF;
5026                         l_materials_from_sol_tbl(l_count).inventory_item_id := r1.inventory_item_id;
5027                         l_materials_from_sol_tbl(l_count).uom := r1.uom;
5028                         -- Bugfix 3468680, vkjain
5029                         -- l_materials_from_sol_tbl(l_count).selling_price := r1.selling_price;
5030                         l_materials_from_sol_tbl(l_count).item_name := r1.item_name;
5031                         l_materials_from_sol_tbl(l_count).comms_nl_trackable_flag := r1.comms_nl_trackable_flag;
5032                         l_materials_from_sol_tbl(l_count).txn_billing_type_id := r1.txn_billing_type_id;
5033                         l_materials_from_sol_tbl(l_count).est_line_source_type_code := r1.est_line_source_type_code;
5034                         l_materials_from_sol_tbl(l_count).est_line_source_id1 := r1.est_line_source_id1;
5035                         l_materials_from_sol_tbl(l_count).est_line_source_id2 := r1.est_line_source_id2;
5036                         l_materials_from_sol_tbl(l_count).ro_service_code_id := r1.ro_service_code_id;
5037                     END IF;
5038                 END LOOP;
5039             END;
5040 
5041             IF (Fnd_Log.Level_Procedure >= Fnd_Log.G_Current_Runtime_Level)
5042             THEN
5043                 Fnd_Log.STRING(Fnd_Log.Level_Procedure,
5044                                lc_mod_name,
5045                                'material line count = ' ||
5046                                l_materials_from_sol_tbl.COUNT);
5047             END IF;
5048             IF (l_materials_from_sol_tbl.COUNT > 0)
5049             THEN
5050                 -- bugfix 3543135, vkjain.
5051                 l_No_Eligible_Lines := FALSE;
5052 
5053                 Convert_To_Est_Lines(p_api_version         => 1.0,
5054                                      p_commit              => Fnd_Api.G_FALSE,
5055                                      p_init_msg_list       => Fnd_Api.G_FALSE,
5056                                      p_validation_level    => p_validation_level,
5057                                      x_return_status       => x_return_status,
5058                                      x_msg_count           => x_msg_count,
5059                                      x_msg_data            => x_msg_data,
5060                                      p_repair_line_id      => p_repair_line_id,
5061                                      p_estimate_id         => p_estimate_id,
5062                                      p_repair_type_id      => p_repair_type_id,
5063                                      p_business_process_id => p_business_process_id,
5064                                      p_currency_code       => p_currency_code,
5065                                      p_incident_id         => p_incident_id,
5066                                      p_organization_id     => p_organization_id,
5067                                      p_price_list_id       => p_price_list_id,
5068                                      p_contract_line_id    => p_contract_line_id,
5069                                      p_MLE_lines_tbl       => l_materials_from_sol_tbl,
5070                                      x_est_lines_tbl       => x_est_lines_tbl,
5071                                      x_warning_flag        => l_warning_flag);
5072                 IF NOT (x_return_status = Fnd_Api.G_RET_STS_SUCCESS)
5073                 THEN
5074                     x_warning_flag := Fnd_Api.G_TRUE;
5075                 END IF;
5076                 IF l_warning_flag <> Fnd_Api.G_FALSE
5077                 THEN
5078                     x_warning_flag := l_warning_flag;
5079                 END IF;
5080             END IF;
5081         END IF;
5082 
5083         --
5084         -- (4) Labor lines from Solutions
5085         --
5086         IF (l_use_tasks_from_sol = 'Y') AND
5087            (l_labor_inventory_item_id IS NOT NULL)
5088         THEN
5089             DECLARE
5090                 l_count NUMBER := 0;
5091             BEGIN
5092                 IF (Fnd_Log.Level_Procedure >=
5093                    Fnd_Log.G_Current_Runtime_Level)
5094                 THEN
5095                     Fnd_Log.STRING(Fnd_Log.Level_Procedure,
5096                                    lc_mod_name,
5097                                    'Getting task labor lines from solutions');
5098                 END IF;
5099                 FOR r1 IN c_labor_lines_from_sol(p_repair_line_id,
5100                                                  -- p_price_list_id, bugfix 3468680, vkjain
5101                                                  l_labor_inventory_item_id,
5102                                                  p_organization_id,
5103                                                  p_repair_type_id)
5104                 LOOP
5105                     l_count := l_count + 1;
5106                     l_labor_from_sol_tbl(l_count).inventory_item_id := r1.inventory_item_id;
5107                     l_labor_from_sol_tbl(l_count).uom := r1.uom;
5108                     l_labor_from_sol_tbl(l_count).quantity := r1.quantity;
5109                     -- Bugfix 3468680, vkjain
5110                     -- l_labor_from_sol_tbl(l_count).selling_price := r1.selling_price;
5111                     l_labor_from_sol_tbl(l_count).item_name := r1.item_name;
5112                     l_labor_from_sol_tbl(l_count).comms_nl_trackable_flag := r1.comms_nl_trackable_flag;
5113                     l_labor_from_sol_tbl(l_count).txn_billing_type_id := r1.txn_billing_type_id;
5114                     l_labor_from_sol_tbl(l_count).est_line_source_type_code := r1.est_line_source_type_code;
5115                     l_labor_from_sol_tbl(l_count).est_line_source_id1 := r1.est_line_source_id1;
5116                     l_labor_from_sol_tbl(l_count).est_line_source_id2 := r1.est_line_source_id2;
5117                     l_labor_from_sol_tbl(l_count).ro_service_code_id := r1.ro_service_code_id;
5118                 END LOOP;
5119             END;
5120 
5121             IF (Fnd_Log.Level_Procedure >= Fnd_Log.G_Current_Runtime_Level)
5122             THEN
5123                 Fnd_Log.STRING(Fnd_Log.Level_Procedure,
5124                                lc_mod_name,
5125                                'labor line count = ' ||
5126                                l_labor_from_sol_tbl.COUNT);
5127                 Fnd_Log.STRING(Fnd_Log.Level_Procedure,
5128                                lc_mod_name,
5129                                'Calling Convert_To_Est_Lines');
5130             END IF;
5131             IF (l_labor_from_sol_tbl.COUNT > 0)
5132             THEN
5133                 -- bugfix 3543135, vkjain.
5134                 l_No_Eligible_Lines := FALSE;
5135 
5136                 Convert_To_Est_Lines(p_api_version         => 1.0,
5137                                      p_commit              => Fnd_Api.G_FALSE,
5138                                      p_init_msg_list       => Fnd_Api.G_FALSE,
5139                                      p_validation_level    => p_validation_level,
5140                                      x_return_status       => x_return_status,
5141                                      x_msg_count           => x_msg_count,
5142                                      x_msg_data            => x_msg_data,
5143                                      p_repair_line_id      => p_repair_line_id,
5144                                      p_estimate_id         => p_estimate_id,
5145                                      p_repair_type_id      => p_repair_type_id,
5146                                      p_business_process_id => p_business_process_id,
5147                                      p_currency_code       => p_currency_code,
5148                                      p_incident_id         => p_incident_id,
5149                                      p_organization_id     => p_organization_id,
5150                                      p_price_list_id       => p_price_list_id,
5151                                      p_contract_line_id    => p_contract_line_id,
5152                                      p_MLE_lines_tbl       => l_labor_from_sol_tbl,
5153                                      x_est_lines_tbl       => x_est_lines_tbl,
5154                                      x_warning_flag        => l_warning_flag);
5155                 IF NOT (x_return_status = Fnd_Api.G_RET_STS_SUCCESS)
5156                 THEN
5157                     x_warning_flag := Fnd_Api.G_TRUE;
5158                 END IF;
5159                 IF l_warning_flag <> Fnd_Api.G_FALSE
5160                 THEN
5161                     x_warning_flag := l_warning_flag;
5162                 END IF;
5163             END IF;
5164         END IF;
5165 
5166         -- bugfix 3543135, vkjain.
5167         -- IF (x_est_lines_tbl.count <= 0) THEN
5168         IF (l_No_Eligible_Lines)
5169         THEN
5170             IF (Fnd_Log.Level_Procedure >= Fnd_Log.G_Current_Runtime_Level)
5171             THEN
5172                 Fnd_Log.STRING(Fnd_Log.Level_Procedure,
5173                                lc_mod_name,
5174                                'no estimate lines available to autogenerate from tasks');
5175             END IF;
5176             RAISE CSD_EST_NO_AUTOGEN;
5177         END IF;
5178 
5179         IF (Fnd_Log.Level_Statement >= Fnd_Log.G_Current_Runtime_Level)
5180         THEN
5181             Fnd_Log.STRING(Fnd_Log.Level_Statement,
5182                            lc_mod_name || '.out_parameter',
5183                            'x_warning_flag: ' || x_warning_flag);
5184         END IF;
5185         IF (Fnd_Log.Level_Procedure >= Fnd_Log.G_Current_Runtime_Level)
5186         THEN
5187             Fnd_Log.STRING(Fnd_Log.Level_Procedure,
5188                            lc_mod_name || '.END',
5189                            'Leaving CSD_REPAIR_ESTIMATE_PVT.Get_Estimates_From_Task');
5190         END IF;
5191         --
5192         -- End API Body
5193         --
5194 
5195         -- Standard check of p_commit.
5196         IF Fnd_Api.To_Boolean(p_commit)
5197         THEN
5198             COMMIT WORK;
5199         END IF;
5200 
5201         -- Standard call to get message count and IF count is  get message info.
5202         Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
5203                                   p_data  => x_msg_data);
5204     EXCEPTION
5205         WHEN CSD_EST_NO_AUTOGEN THEN
5206             x_warning_flag := Fnd_Api.G_TRUE;
5207 
5208             -- save message in fnd stack
5209             IF (Fnd_Log.Level_Statement >= Fnd_Log.G_Current_Runtime_Level)
5210             THEN
5211                 Fnd_Log.STRING(Fnd_Log.Level_Statement,
5212                                lc_mod_name,
5213                                'Adding message CSD_EST_NO_AUTOGEN to FND_MSG stack');
5214             END IF;
5215             Fnd_Message.SET_NAME('CSD', 'CSD_EST_NO_AUTOGEN');
5216             Fnd_Msg_Pub.ADD;
5217             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
5218                                       p_data  => x_msg_data);
5219 
5220             -- save message in debug log
5221             IF (Fnd_Log.Level_Exception >= Fnd_Log.G_Current_Runtime_Level)
5222             THEN
5223                 Fnd_Log.STRING(Fnd_Log.Level_Exception,
5224                                lc_mod_name,
5225                                'No estimate lines autogenerated');
5226             END IF;
5227 
5228         WHEN Fnd_Api.G_EXC_ERROR THEN
5229             ROLLBACK TO Get_Estimates_From_Task;
5230             x_return_status := Fnd_Api.G_RET_STS_ERROR;
5231 
5232             /*
5233             -- TO DO: Add seeded err message
5234             -- save message in fnd stack
5235             if (Fnd_Log.Level_Statement>= Fnd_Log.G_Current_Runtime_Level) then
5236                 FND_LOG.STRING(Fnd_Log.Level_Statement, lc_mod_name,
5237                                'Adding message ERR_NAME to FND_MSG stack');
5238             end if;
5239             FND_MESSAGE.SET_NAME('CSD','err_name');
5240             FND_MESSAGE.SET_TOKEN('toke_name', 'token_value');
5241             FND_MSG_PUB.ADD;
5242             */
5243             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
5244                                       p_data  => x_msg_data);
5245 
5246             -- save message in debug log
5247             IF (Fnd_Log.Level_Exception >= Fnd_Log.G_Current_Runtime_Level)
5248             THEN
5249                 Fnd_Log.STRING(Fnd_Log.Level_Exception,
5250                                lc_mod_name,
5251                                'EXC_ERROR[' || x_msg_data || ']');
5252             END IF;
5253 
5254         WHEN Fnd_Api.G_EXC_UNEXPECTED_ERROR THEN
5255             ROLLBACK TO Get_Estimates_From_Task;
5256             x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
5257 
5258             -- save message in fnd stack
5259             IF Fnd_Msg_Pub.Check_Msg_Level(Fnd_Msg_Pub.G_MSG_LVL_UNEXP_ERROR)
5260             THEN
5261                 IF (Fnd_Log.Level_Statement >=
5262                    Fnd_Log.G_Current_Runtime_Level)
5263                 THEN
5264                     Fnd_Log.STRING(Fnd_Log.Level_Statement,
5265                                    lc_mod_name,
5266                                    'Adding message using FND_MSG_PUB.Add_Exc_Msg to FND_MSG stack');
5267                 END IF;
5268                 Fnd_Msg_Pub.Add_Exc_Msg(G_PKG_NAME, lc_api_name);
5269             END IF;
5270             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
5271                                       p_data  => x_msg_data);
5272 
5273             -- save message in debug log
5274             IF (Fnd_Log.Level_Exception >= Fnd_Log.G_Current_Runtime_Level)
5275             THEN
5276                 Fnd_Log.STRING(Fnd_Log.Level_Exception,
5277                                lc_mod_name,
5278                                'EXC_UNEXPECTED_ERROR[' || x_msg_data || ']');
5279             END IF;
5280 
5281         WHEN OTHERS THEN
5282             ROLLBACK TO Get_Estimates_From_Task;
5283             x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
5284 
5285             -- save message in fnd stack
5286             IF Fnd_Msg_Pub.Check_Msg_Level(Fnd_Msg_Pub.G_MSG_LVL_UNEXP_ERROR)
5287             THEN
5288                 IF (Fnd_Log.Level_Statement >=
5289                    Fnd_Log.G_Current_Runtime_Level)
5290                 THEN
5291                     Fnd_Log.STRING(Fnd_Log.Level_Statement,
5292                                    lc_mod_name,
5293                                    'Adding message using FND_MSG_PUB.Add_Exc_Msg to FND_MSG stack');
5294                 END IF;
5295                 Fnd_Msg_Pub.Add_Exc_Msg(G_PKG_NAME, lc_api_name);
5296             END IF;
5297             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
5298                                       p_data  => x_msg_data);
5299 
5300             -- save message in debug log
5301             IF (Fnd_Log.Level_Exception >= Fnd_Log.G_Current_Runtime_Level)
5302             THEN
5303                 -- create a seeded message
5304                 Fnd_Log.STRING(Fnd_Log.Level_Exception,
5305                                lc_mod_name,
5306                                'SQL Message[' || SQLERRM || ']');
5307             END IF;
5308     END Get_Estimates_From_Task;
5309 
5310     /*--------------------------------------------------*/
5311     /* procedure name: Get_Estimates_From_BOM           */
5312     /* description   : Fetches ML lines for the         */
5313     /*                 BOM/Route references associated  */
5314     /*                 via Service Codes.               */
5315     /*                                                  */
5316     /* x_warning_flag : FND_API.G_TRUE if any non-fatal */
5317     /*                  errors occured. FND_API.G_FALSE */
5318     /*                  if everything was successful.   */
5319     /*                  Note that this value could be   */
5320     /*                  G_TRUE even if x_return_status  */
5321     /*                  is G_RET_STS_SUCCESS            */
5322     /* called from:  Autocreate_Estimate_Lines          */
5323     /*--------------------------------------------------*/
5324     PROCEDURE Get_Estimates_From_BOM(p_api_version         IN NUMBER,
5325                                      p_commit              IN VARCHAR2,
5326                                      p_init_msg_list       IN VARCHAR2,
5327                                      p_validation_level    IN NUMBER,
5328                                      x_return_status       OUT NOCOPY VARCHAR2,
5329                                      x_msg_count           OUT NOCOPY NUMBER,
5330                                      x_msg_data            OUT NOCOPY VARCHAR2,
5331                                      p_repair_line_id      IN NUMBER,
5332                                      p_estimate_id         IN NUMBER,
5333                                      p_repair_type_id      IN NUMBER,
5334                                      p_business_process_id IN NUMBER,
5335                                      p_currency_code       IN VARCHAR2,
5336                                      p_incident_id         IN NUMBER,
5337                                      p_repair_mode         IN VARCHAR2,
5338                                      p_inventory_item_id   IN NUMBER,
5339                                      p_organization_id     IN NUMBER,
5340                                      p_price_list_id       IN NUMBER,
5341                                      p_contract_line_id    IN NUMBER,
5342                                      x_est_lines_tbl       OUT NOCOPY CSD_REPAIR_ESTIMATE_PVT.REPAIR_ESTIMATE_LINE_TBL,
5343                                      x_warning_flag        OUT NOCOPY VARCHAR2) IS
5344 
5345         -- CONSTANTS --
5346         lc_debug_level CONSTANT NUMBER := Fnd_Log.G_CURRENT_RUNTIME_LEVEL;
5347         lc_stat_level  CONSTANT NUMBER := Fnd_Log.LEVEL_STATEMENT;
5348         lc_proc_level  CONSTANT NUMBER := Fnd_Log.LEVEL_PROCEDURE;
5349         lc_event_level CONSTANT NUMBER := Fnd_Log.LEVEL_EVENT;
5350         lc_excep_level CONSTANT NUMBER := Fnd_Log.LEVEL_EXCEPTION;
5351         lc_error_level CONSTANT NUMBER := Fnd_Log.LEVEL_ERROR;
5352         lc_unexp_level CONSTANT NUMBER := Fnd_Log.LEVEL_UNEXPECTED;
5353         lc_mod_name    CONSTANT VARCHAR2(100) := 'csd.plsql.csd_repair_estimate_pvt.get_estimates_from_bom';
5354         lc_api_name    CONSTANT VARCHAR2(30) := 'Get_Estimates_From_BOM';
5355         lc_api_version CONSTANT NUMBER := 1.0;
5356 
5357         -- VARIABLES --
5358         l_material_lines_tbl MLE_LINES_TBL_TYPE;
5359         l_labor_lines_tbl    MLE_LINES_TBL_TYPE;
5360         l_warning_flag       VARCHAR2(1);
5361 
5362         -- bugfix 3543135, vkjain.
5363         l_No_Eligible_Lines BOOLEAN := TRUE;
5364 
5365         -- EXCEPTIONS --
5366         CSD_EST_NO_AUTOGEN EXCEPTION;
5367 
5368         -- CURSORS --
5369 
5370         /* bugfix 3468680. vkjain
5371           The selling price should be derived via API call instead of
5372           selecting from the query.
5373           The cursors are replaced by new ones.
5374           Following changes are made to the existing cursors -
5375           -- Cursors do not accept p_price_list_id as IN parameter anymore.
5376           -- They do not select the list_price from the QP view.
5377           -- The reference to QP view is removed from the FROM part of statements.
5378           -- All joins for the QP view commented in the WHERE clause.
5379           -- The selling price is derived in the Convert_to_est_lines procedure
5380              by calling CSD_PROCESS_UTIL.GET_CHARGE_SELLING_PRICE API.
5381 
5382         cursor c_material_lines_from_bom (p_repair_line_id NUMBER,
5383                                           p_repair_type_id NUMBER,
5384                                           p_price_list_id  NUMBER) is
5385            SELECT BOM.component_item_id       INVENTORY_ITEM_ID,
5386                   BOM.primary_uom_code        UOM,
5387                   BOM.component_quantity      QUANTITY,
5388                   PRICE.list_price            SELLING_PRICE,
5389                   MTL.concatenated_segments   ITEM_NAME,
5390                   MTL.comms_nl_trackable_flag COMMS_NL_TRACKABLE_FLAG,
5391                   SAR.txn_billing_type_id     TXN_BILLING_TYPE_ID,
5392                   'SERVICE_CODE'              EST_LINE_SOURCE_TYPE_CODE,
5393                   ROSC.Service_Code_id        EST_LINE_SOURCE_ID1,
5394                   null                        EST_LINE_SOURCE_ID2,
5395                   ROSC.RO_Service_Code_id     RO_SERVICE_CODE_ID
5396            FROM CSD_RO_SERVICE_CODES       ROSC,
5397                 CSD_SERVICE_CODES_VL       SC,
5398                 CSD_SC_WORK_ENTITIES       WRK,
5399                 BOM_INVENTORY_COMPONENTS_V BOM,
5400                 QP_PRICE_LIST_LINES_V      PRICE,
5401                 MTL_SYSTEM_ITEMS_KFV       MTL,
5402                 CSD_REPAIR_TYPES_SAR       RTSAR,
5403                 CS_TXN_BILLING_TYPES       SAR
5404            WHERE ROSC.repair_line_id = p_repair_line_id
5405                and ROSC.applicable_flag = 'Y'
5406                -- and ROSC.applied_to_work_flag = 'N' -- bugfix 3473869.vkjain.
5407                and ROSC.applied_to_est_flag = 'N'
5408                and WRK.Service_code_id = ROSC.Service_code_id
5409                and WRK.work_entity_type_code = 'BOM'
5410                and SC.Service_code_Id = ROSC.Service_code_id
5411                and nvl(SC.active_from, SYSDATE) <= SYSDATE
5412                and nvl(SC.active_to, SYSDATE) >= SYSDATE
5413                and WRK.work_entity_id3 = cs_std.get_item_valdn_orgzn_id
5414                -- and WRK.work_entity_id1 IS NOT NULL
5415                and BOM.bill_sequence_id = WRK.work_entity_id1
5416                and nvl(BOM.effectivity_date , SYSDATE) <= SYSDATE -- swai bug 3323274
5417                and nvl(BOM.disable_date , SYSDATE) >= SYSDATE     -- swai bug 3323274
5418                and PRICE.price_list_id(+) = p_price_list_id
5419                and PRICE.inventory_item_id(+) = BOM.component_item_id
5420                and PRICE.unit_code(+) = BOM.primary_uom_code
5421                and nvl(PRICE.start_date_active, SYSDATE) <= SYSDATE  -- swai added
5422                and nvl(PRICE.end_date_active, SYSDATE) >= SYSDATE    -- swai added
5423                and MTL.inventory_item_id = BOM.component_item_id
5424                and MTL.organization_id = WRK.work_entity_id3
5425                and RTSAR.repair_type_id = p_repair_type_id
5426                and SAR.txn_billing_type_id = RTSAR.txn_billing_type_id(+)
5427                and SAR.billing_type = MTL.material_billable_flag(+)
5428                and MTL.material_billable_flag IS NOT NULL;
5429 
5430          cursor c_labor_lines_from_bom (p_repair_line_id NUMBER,
5431                                         p_repair_type_id NUMBER,
5432                                         p_price_list_id  NUMBER) is
5433            SELECT
5434                   RES.billable_item_id        INVENTORY_ITEM_ID,
5435                   RES.unit_of_measure         UOM,
5436                   -- OPRES.assigned_units        QUANTITY, -- Replaced by following line to fix 3365436. vkjain.
5437                   OPRES.usage_rate_or_amount  QUANTITY,
5438                   PRICE.list_price            SELLING_PRICE,
5439                   MTL.concatenated_segments   ITEM_NAME,
5440                   MTL.comms_nl_trackable_flag COMMS_NL_TRACKABLE_FLAG,
5441                   SAR.txn_billing_type_id     TXN_BILLING_TYPE_ID,
5442                   'SERVICE_CODE'              EST_LINE_SOURCE_TYPE_CODE,
5443                   ROSC.Service_Code_id        EST_LINE_SOURCE_ID1,
5444                   null                        EST_LINE_SOURCE_ID2,
5445                   ROSC.RO_Service_Code_id     RO_SERVICE_CODE_ID,
5446                 RES.resource_id             RESOURCE_ID -- vkjain. 3449978
5447            FROM CSD_RO_SERVICE_CODES    ROSC,
5448                 CSD_SC_WORK_ENTITIES    WRK,
5449                 CSD_SERVICE_CODES_VL    SC,
5450                 BOM_OPERATION_SEQUENCES OPSEQ,
5451                 BOM_OPERATION_RESOURCES OPRES,
5452                 QP_PRICE_LIST_LINES_V   PRICE,
5453                 MTL_SYSTEM_ITEMS_KFV    MTL,
5454                 CSD_REPAIR_TYPES_SAR    RTSAR,
5455                 CS_TXN_BILLING_TYPES    SAR,
5456                 BOM_RESOURCES           RES
5457            WHERE ROSC.repair_line_id = p_repair_line_id
5458                and ROSC.applicable_flag = 'Y'
5459                -- and ROSC.applied_to_work_flag = 'N' -- bugfix 3473869.vkjain.
5460                and ROSC.applied_to_est_flag = 'N'
5461                and WRK.Service_code_id = ROSC.Service_code_id
5462                and WRK.work_entity_type_code = 'BOM'
5463                and SC.Service_code_Id = ROSC.Service_code_id
5464                and nvl(SC.active_from, SYSDATE) <= SYSDATE
5465                and nvl(SC.active_to, SYSDATE) >= SYSDATE
5466                and WRK.work_entity_id3 = cs_std.get_item_valdn_orgzn_id
5467                -- and WRK.work_entity_id2 IS NOT NULL
5468                and OPSEQ.ROUTING_SEQUENCE_ID = WRK.work_entity_id2
5469                and nvl(OPSEQ.effectivity_date , SYSDATE) <= SYSDATE -- swai bug 3323274
5470                and nvl(OPSEQ.disable_date , SYSDATE) >= SYSDATE     -- swai bug 3323274
5471                and OPRES.OPERATION_SEQUENCE_ID = OPSEQ.OPERATION_SEQUENCE_ID
5472                and RES.RESOURCE_ID = OPRES.RESOURCE_ID
5473                and PRICE.price_list_id(+) = p_price_list_id
5474                and nvl(PRICE.start_date_active, SYSDATE) <= SYSDATE  -- swai added
5475                and nvl(PRICE.end_date_active, SYSDATE) >= SYSDATE    -- swai added
5476                and PRICE.inventory_item_id(+) = RES.billable_item_id
5477                and PRICE.unit_code(+) = RES.unit_of_measure
5478                and MTL.inventory_item_id = RES.billable_item_id
5479                and MTL.organization_id = WRK.work_entity_id3
5480                and MTL.material_billable_flag IS NOT NULL
5481                and RTSAR.repair_type_id = p_repair_type_id
5482                and SAR.txn_billing_type_id = RTSAR.txn_billing_type_id(+)
5483                and SAR.billing_type = MTL.material_billable_flag(+);
5484               */
5485 
5486         CURSOR c_material_lines_from_bom(p_repair_line_id NUMBER, p_repair_type_id NUMBER) IS
5487         -- p_price_list_id  NUMBER) is, bug 3468680, vkjain
5488             SELECT BOM.component_item_id  INVENTORY_ITEM_ID,
5489                    BOM.primary_uom_code   UOM,
5490                    BOM.component_quantity QUANTITY,
5491                    -- PRICE.list_price            SELLING_PRICE,
5492                    MTL.concatenated_segments ITEM_NAME,
5493                    MTL.comms_nl_trackable_flag COMMS_NL_TRACKABLE_FLAG,
5494                    SAR.txn_billing_type_id TXN_BILLING_TYPE_ID,
5495                    'SERVICE_CODE' EST_LINE_SOURCE_TYPE_CODE,
5496                    ROSC.Service_Code_id EST_LINE_SOURCE_ID1,
5497                    NULL EST_LINE_SOURCE_ID2,
5498                    ROSC.RO_Service_Code_id RO_SERVICE_CODE_ID
5499               FROM CSD_RO_SERVICE_CODES       ROSC,
5500                    CSD_SERVICE_CODES_VL       SC,
5501                    CSD_SC_WORK_ENTITIES       WRK,
5502                    BOM_INVENTORY_COMPONENTS_V BOM,
5503                    -- QP_PRICE_LIST_LINES_V      PRICE,
5504                    MTL_SYSTEM_ITEMS_KFV MTL,
5505                    CSD_REPAIR_TYPES_SAR RTSAR,
5506                    CS_TXN_BILLING_TYPES SAR
5507              WHERE ROSC.repair_line_id = p_repair_line_id
5508                AND ROSC.applicable_flag = 'Y'
5509                   -- and ROSC.applied_to_work_flag = 'N' -- bugfix 3473869.vkjain.
5510                AND ROSC.applied_to_est_flag = 'N'
5511                AND WRK.Service_code_id = ROSC.Service_code_id
5512                AND WRK.work_entity_type_code = 'BOM'
5513                AND SC.Service_code_Id = ROSC.Service_code_id
5514                AND NVL(SC.active_from, SYSDATE) <= SYSDATE
5515                AND NVL(SC.active_to, SYSDATE) >= SYSDATE
5516                AND WRK.work_entity_id3 = Cs_Std.get_item_valdn_orgzn_id
5517                   -- and WRK.work_entity_id1 IS NOT NULL
5518                AND BOM.bill_sequence_id = WRK.work_entity_id1
5519                AND NVL(BOM.effectivity_date, SYSDATE) <= SYSDATE -- swai bug 3323274
5520                AND NVL(BOM.disable_date, SYSDATE) >= SYSDATE -- swai bug 3323274
5521                   -- Following lines commented by vkjain, bugfix 3468680
5522                   -- and PRICE.price_list_id(+) = p_price_list_id
5523                   -- and PRICE.inventory_item_id(+) = BOM.component_item_id
5524                   -- and PRICE.unit_code(+) = BOM.primary_uom_code
5525                   -- and nvl(PRICE.start_date_active, SYSDATE) <= SYSDATE  -- swai added
5526                   -- and nvl(PRICE.end_date_active, SYSDATE) >= SYSDATE    -- swai added
5527                AND MTL.inventory_item_id = BOM.component_item_id
5528                AND MTL.organization_id = WRK.work_entity_id3
5529                AND RTSAR.repair_type_id = p_repair_type_id
5530                AND SAR.txn_billing_type_id = RTSAR.txn_billing_type_id(+)
5531                AND SAR.billing_type = MTL.material_billable_flag(+)
5532                AND MTL.material_billable_flag IS NOT NULL;
5533 
5534         CURSOR c_labor_lines_from_bom(p_repair_line_id NUMBER, p_repair_type_id NUMBER) IS
5535         -- p_price_list_id  NUMBER) is -- bug 3468680, vkjain
5536             SELECT RES.billable_item_id INVENTORY_ITEM_ID,
5537                    RES.unit_of_measure  UOM,
5538                    -- OPRES.assigned_units        QUANTITY, -- Replaced by following line to fix 3365436. vkjain.
5539                    OPRES.usage_rate_or_amount QUANTITY,
5540                    -- PRICE.list_price            SELLING_PRICE,
5541                    MTL.concatenated_segments ITEM_NAME,
5542                    MTL.comms_nl_trackable_flag COMMS_NL_TRACKABLE_FLAG,
5543                    SAR.txn_billing_type_id TXN_BILLING_TYPE_ID,
5544                    'SERVICE_CODE' EST_LINE_SOURCE_TYPE_CODE,
5545                    ROSC.Service_Code_id EST_LINE_SOURCE_ID1,
5546                    NULL EST_LINE_SOURCE_ID2,
5547                    ROSC.RO_Service_Code_id RO_SERVICE_CODE_ID,
5548                    RES.resource_id RESOURCE_ID -- vkjain. 3449978
5549               FROM CSD_RO_SERVICE_CODES    ROSC,
5550                    CSD_SC_WORK_ENTITIES    WRK,
5551                    CSD_SERVICE_CODES_VL    SC,
5552                    BOM_OPERATION_SEQUENCES OPSEQ,
5553                    BOM_OPERATION_RESOURCES OPRES,
5554                    -- QP_PRICE_LIST_LINES_V   PRICE,
5555                    MTL_SYSTEM_ITEMS_KFV MTL,
5556                    CSD_REPAIR_TYPES_SAR RTSAR,
5557                    CS_TXN_BILLING_TYPES SAR,
5558                    BOM_RESOURCES        RES
5559              WHERE ROSC.repair_line_id = p_repair_line_id
5560                AND ROSC.applicable_flag = 'Y'
5561                   -- and ROSC.applied_to_work_flag = 'N' -- bugfix 3473869.vkjain.
5562                AND ROSC.applied_to_est_flag = 'N'
5563                AND WRK.Service_code_id = ROSC.Service_code_id
5564                AND WRK.work_entity_type_code = 'BOM'
5565                AND SC.Service_code_Id = ROSC.Service_code_id
5566                AND NVL(SC.active_from, SYSDATE) <= SYSDATE
5567                AND NVL(SC.active_to, SYSDATE) >= SYSDATE
5568                AND WRK.work_entity_id3 = Cs_Std.get_item_valdn_orgzn_id
5569                   -- and WRK.work_entity_id2 IS NOT NULL
5570                AND OPSEQ.ROUTING_SEQUENCE_ID = WRK.work_entity_id2
5571                AND NVL(OPSEQ.effectivity_date, SYSDATE) <= SYSDATE -- swai bug 3323274
5572                AND NVL(OPSEQ.disable_date, SYSDATE) >= SYSDATE -- swai bug 3323274
5573                AND OPRES.OPERATION_SEQUENCE_ID =
5574                    OPSEQ.OPERATION_SEQUENCE_ID
5575                AND RES.RESOURCE_ID = OPRES.RESOURCE_ID
5576                   -- Following lines commented by vkjain, bugfix 3468680
5577                   -- and PRICE.price_list_id(+) = p_price_list_id
5578                   -- and nvl(PRICE.start_date_active, SYSDATE) <= SYSDATE  -- swai added
5579                   -- and nvl(PRICE.end_date_active, SYSDATE) >= SYSDATE    -- swai added
5580                   -- and PRICE.inventory_item_id(+) = RES.billable_item_id
5581                   -- and PRICE.unit_code(+) = RES.unit_of_measure
5582                AND MTL.inventory_item_id = RES.billable_item_id
5583                AND MTL.organization_id = WRK.work_entity_id3
5584                AND MTL.material_billable_flag IS NOT NULL
5585                AND RTSAR.repair_type_id = p_repair_type_id
5586                AND SAR.txn_billing_type_id = RTSAR.txn_billing_type_id(+)
5587                AND SAR.billing_type = MTL.material_billable_flag(+);
5588 
5589     BEGIN
5590         -- Standard Start of API savepoint
5591         SAVEPOINT Get_Estimates_From_BOM;
5592 
5593         -- Standard call to check for call compatibility.
5594         IF NOT Fnd_Api.Compatible_API_Call(lc_api_version,
5595                                            p_api_version,
5596                                            lc_api_name,
5597                                            G_PKG_NAME)
5598         THEN
5599             RAISE Fnd_Api.G_EXC_UNEXPECTED_ERROR;
5600         END IF;
5601 
5602         -- Initialize message list if p_init_msg_list is set to TRUE.
5603         IF Fnd_Api.to_Boolean(p_init_msg_list)
5604         THEN
5605             Fnd_Msg_Pub.initialize;
5606         END IF;
5607 
5608         IF (Fnd_Log.Level_Procedure >= Fnd_Log.G_Current_Runtime_Level)
5609         THEN
5610             Fnd_Log.STRING(Fnd_Log.Level_Procedure,
5611                            lc_mod_name || '.BEGIN',
5612                            'Entering CSD_REPAIR_ESTIMATE_PVT.Get_Estimates_From_BOM');
5613         END IF;
5614         -- log parameters
5615         IF (Fnd_Log.Level_Statement >= Fnd_Log.G_Current_Runtime_Level)
5616         THEN
5617             Fnd_Log.STRING(Fnd_Log.Level_Statement,
5618                            lc_mod_name || '.parameter_logging',
5619                            'p_api_version: ' || p_api_version);
5620             Fnd_Log.STRING(Fnd_Log.Level_Statement,
5621                            lc_mod_name || '.parameter_logging',
5622                            'p_commit: ' || p_commit);
5623             Fnd_Log.STRING(Fnd_Log.Level_Statement,
5624                            lc_mod_name || '.parameter_logging',
5625                            'p_init_msg_list: ' || p_init_msg_list);
5626             Fnd_Log.STRING(Fnd_Log.Level_Statement,
5627                            lc_mod_name || '.parameter_logging',
5628                            'p_validation_level: ' || p_validation_level);
5629             Fnd_Log.STRING(Fnd_Log.Level_Statement,
5630                            lc_mod_name || '.parameter_logging',
5631                            'p_repair_line_id: ' || p_repair_line_id);
5632             Fnd_Log.STRING(Fnd_Log.Level_Statement,
5633                            lc_mod_name || '.parameter_logging',
5634                            'p_estimate_id: ' || p_estimate_id);
5635             Fnd_Log.STRING(Fnd_Log.Level_Statement,
5636                            lc_mod_name || '.parameter_logging',
5637                            'p_repair_type_id: ' || p_repair_type_id);
5638             Fnd_Log.STRING(Fnd_Log.Level_Statement,
5639                            lc_mod_name || '.parameter_logging',
5640                            'p_business_process_id: ' ||
5641                            p_business_process_id);
5642             Fnd_Log.STRING(Fnd_Log.Level_Statement,
5643                            lc_mod_name || '.parameter_logging',
5644                            'p_currency_code: ' || p_currency_code);
5645             Fnd_Log.STRING(Fnd_Log.Level_Statement,
5646                            lc_mod_name || '.parameter_logging',
5647                            'p_incident_id: ' || p_incident_id);
5648             Fnd_Log.STRING(Fnd_Log.Level_Statement,
5649                            lc_mod_name || '.parameter_logging',
5650                            'p_repair_mode: ' || p_repair_mode);
5651             Fnd_Log.STRING(Fnd_Log.Level_Statement,
5652                            lc_mod_name || '.parameter_logging',
5653                            'p_inventory_item_id: ' || p_inventory_item_id);
5654             Fnd_Log.STRING(Fnd_Log.Level_Statement,
5655                            lc_mod_name || '.parameter_logging',
5656                            'p_organization_id: ' || p_organization_id);
5657             Fnd_Log.STRING(Fnd_Log.Level_Statement,
5658                            lc_mod_name || '.parameter_logging',
5659                            'p_price_list_id: ' || p_price_list_id);
5660             Fnd_Log.STRING(Fnd_Log.Level_Statement,
5661                            lc_mod_name || '.parameter_logging',
5662                            'p_contract_line_id: ' || p_contract_line_id);
5663         END IF;
5664 
5665         -- Initialize API return status to success
5666         x_return_status := Fnd_Api.G_RET_STS_SUCCESS;
5667 
5668         -- initialize the warning flag
5669         x_warning_flag := Fnd_Api.G_FALSE;
5670 
5671         --
5672         -- Begin API Body
5673         --
5674 
5675         --
5676         -- Get Material Lines from BOM
5677         --
5678         DECLARE
5679             l_count NUMBER := 0;
5680         BEGIN
5681             IF (Fnd_Log.Level_Procedure >= Fnd_Log.G_Current_Runtime_Level)
5682             THEN
5683                 Fnd_Log.STRING(Fnd_Log.Level_Procedure,
5684                                lc_mod_name,
5685                                'Getting BOM material lines from service codes');
5686             END IF;
5687             -- Bugfix 3468680, vkjain
5688             -- FOR r1 in c_material_lines_from_bom (p_repair_line_id, p_repair_type_id, p_price_list_id)
5689             FOR r1 IN c_material_lines_from_bom(p_repair_line_id,
5690                                                 p_repair_type_id)
5691             LOOP
5692                 l_count := l_count + 1;
5693                 l_material_lines_tbl(l_count).inventory_item_id := r1.inventory_item_id;
5694                 l_material_lines_tbl(l_count).uom := r1.uom;
5695                 l_material_lines_tbl(l_count).quantity := r1.quantity;
5696                 -- Bugfix 3468680, vkjain
5697                 -- l_material_lines_tbl(l_count).selling_price := r1.selling_price;
5698                 l_material_lines_tbl(l_count).item_name := r1.item_name;
5699                 l_material_lines_tbl(l_count).comms_nl_trackable_flag := r1.comms_nl_trackable_flag;
5700                 l_material_lines_tbl(l_count).txn_billing_type_id := r1.txn_billing_type_id;
5701                 l_material_lines_tbl(l_count).est_line_source_type_code := r1.est_line_source_type_code;
5702                 l_material_lines_tbl(l_count).est_line_source_id1 := r1.est_line_source_id1;
5703                 l_material_lines_tbl(l_count).est_line_source_id2 := r1.est_line_source_id2;
5704                 l_material_lines_tbl(l_count).ro_service_code_id := r1.ro_service_code_id;
5705             END LOOP;
5706         END;
5707 
5708         -- bugfix 3543135, vkjain.
5709         IF (l_material_lines_tbl.COUNT > 0)
5710         THEN
5711             l_No_Eligible_Lines := FALSE;
5712         END IF;
5713 
5714 --        IF (g_debug >= 5)
5715 --        THEN
5716 --            Csd_Gen_Utility_Pvt.dump_mle_lines_tbl_type(l_material_lines_tbl);
5717 --        END IF;
5718 
5719         IF (Fnd_Log.Level_Procedure >= Fnd_Log.G_Current_Runtime_Level)
5720         THEN
5721             Fnd_Log.STRING(Fnd_Log.Level_Procedure,
5722                            lc_mod_name,
5723                            'material line count = ' ||
5724                            l_material_lines_tbl.COUNT);
5725             Fnd_Log.STRING(Fnd_Log.Level_Procedure,
5726                            lc_mod_name,
5727                            'Calling Convert_To_Est_Lines');
5728         END IF;
5729 
5730         Convert_To_Est_Lines(p_api_version         => 1.0,
5731                              p_commit              => Fnd_Api.G_FALSE,
5732                              p_init_msg_list       => Fnd_Api.G_FALSE,
5733                              p_validation_level    => p_validation_level,
5734                              x_return_status       => x_return_status,
5735                              x_msg_count           => x_msg_count,
5736                              x_msg_data            => x_msg_data,
5737                              p_repair_line_id      => p_repair_line_id,
5738                              p_estimate_id         => p_estimate_id,
5739                              p_repair_type_id      => p_repair_type_id,
5740                              p_business_process_id => p_business_process_id,
5741                              p_currency_code       => p_currency_code,
5742                              p_incident_id         => p_incident_id,
5743                              p_organization_id     => p_organization_id,
5744                              p_price_list_id       => p_price_list_id,
5745                              p_contract_line_id    => p_contract_line_id,
5746                              p_MLE_lines_tbl       => l_material_lines_tbl,
5747                              x_est_lines_tbl       => x_est_lines_tbl,
5748                              x_warning_flag        => l_warning_flag);
5749 
5750 --        IF (g_debug >= 5)
5751 --        THEN
5752 --            Csd_Gen_Utility_Pvt.dump_repair_estimate_line_tbl(x_est_lines_tbl);
5753 --        END IF;
5754 
5755         IF NOT (x_return_status = Fnd_Api.G_RET_STS_SUCCESS)
5756         THEN
5757             RAISE Fnd_Api.G_EXC_ERROR;
5758         END IF;
5759 
5760         IF l_warning_flag <> Fnd_Api.G_FALSE
5761         THEN
5762             x_warning_flag := l_warning_flag;
5763         END IF;
5764         --
5765         -- Get Labor Lines from BOM
5766         --
5767         DECLARE
5768             l_count NUMBER := 0;
5769         BEGIN
5770             IF (Fnd_Log.Level_Procedure >= Fnd_Log.G_Current_Runtime_Level)
5771             THEN
5772                 Fnd_Log.STRING(Fnd_Log.Level_Procedure,
5773                                lc_mod_name,
5774                                'Getting BOM labor lines from service codes');
5775             END IF;
5776             -- bugfix 3468680, vkjain
5777             -- FOR r2 in c_labor_lines_from_bom (p_repair_line_id, p_repair_type_id, p_price_list_id)
5778             FOR r2 IN c_labor_lines_from_bom(p_repair_line_id,
5779                                              p_repair_type_id)
5780             LOOP
5781                 l_count := l_count + 1;
5782                 l_labor_lines_tbl(l_count).inventory_item_id := r2.inventory_item_id;
5783                 l_labor_lines_tbl(l_count).uom := r2.uom;
5784                 l_labor_lines_tbl(l_count).quantity := r2.quantity;
5785                 -- Bugfix 3468680, vkjain
5786                 -- l_labor_lines_tbl(l_count).selling_price := r2.selling_price;
5787                 l_labor_lines_tbl(l_count).item_name := r2.item_name;
5788                 l_labor_lines_tbl(l_count).comms_nl_trackable_flag := r2.comms_nl_trackable_flag;
5789                 l_labor_lines_tbl(l_count).txn_billing_type_id := r2.txn_billing_type_id;
5790                 l_labor_lines_tbl(l_count).est_line_source_type_code := r2.est_line_source_type_code;
5791                 l_labor_lines_tbl(l_count).est_line_source_id1 := r2.est_line_source_id1;
5792                 l_labor_lines_tbl(l_count).est_line_source_id2 := r2.est_line_source_id2;
5793                 l_labor_lines_tbl(l_count).ro_service_code_id := r2.ro_service_code_id;
5794                 -- vkjain. Bugfix 3449978.
5795                 l_labor_lines_tbl(l_count).resource_id := r2.resource_id;
5796             END LOOP;
5797         END;
5798 
5799         -- bugfix 3543135, vkjain.
5800         IF (l_labor_lines_tbl.COUNT > 0)
5801         THEN
5802             l_No_Eligible_Lines := FALSE;
5803         END IF;
5804 
5805 --        IF (g_debug >= 5)
5806 --        THEN
5807 --            Csd_Gen_Utility_Pvt.dump_mle_lines_tbl_type(l_labor_lines_tbl);
5808 --        END IF;
5809 
5810         IF (Fnd_Log.Level_Procedure >= Fnd_Log.G_Current_Runtime_Level)
5811         THEN
5812             Fnd_Log.STRING(Fnd_Log.Level_Procedure,
5813                            lc_mod_name,
5814                            'labor line count = ' || l_labor_lines_tbl.COUNT);
5815             Fnd_Log.STRING(Fnd_Log.Level_Procedure,
5816                            lc_mod_name,
5817                            'Calling Convert_To_Est_Lines');
5818         END IF;
5819         Convert_To_Est_Lines(p_api_version         => 1.0,
5820                              p_commit              => Fnd_Api.G_FALSE,
5821                              p_init_msg_list       => Fnd_Api.G_FALSE,
5822                              p_validation_level    => p_validation_level,
5823                              x_return_status       => x_return_status,
5824                              x_msg_count           => x_msg_count,
5825                              x_msg_data            => x_msg_data,
5826                              p_repair_line_id      => p_repair_line_id,
5827                              p_estimate_id         => p_estimate_id,
5828                              p_repair_type_id      => p_repair_type_id,
5829                              p_business_process_id => p_business_process_id,
5830                              p_currency_code       => p_currency_code,
5831                              p_incident_id         => p_incident_id,
5832                              p_organization_id     => p_organization_id,
5833                              p_price_list_id       => p_price_list_id,
5834                              p_contract_line_id    => p_contract_line_id,
5835                              p_MLE_lines_tbl       => l_labor_lines_tbl,
5836                              x_est_lines_tbl       => x_est_lines_tbl,
5837                              x_warning_flag        => l_warning_flag);
5838 
5839 --        IF (g_debug >= 5)
5840 --        THEN
5841 --            Csd_Gen_Utility_Pvt.dump_repair_estimate_line_tbl(x_est_lines_tbl);
5842 --        END IF;
5843 
5844         IF NOT (x_return_status = Fnd_Api.G_RET_STS_SUCCESS)
5845         THEN
5846             RAISE Fnd_Api.G_EXC_ERROR;
5847         END IF;
5848         IF l_warning_flag <> Fnd_Api.G_FALSE
5849         THEN
5850             x_warning_flag := l_warning_flag;
5851         END IF;
5852 
5853         -- bugfix 3543135, vkjain.
5854         -- IF (NVL(x_est_lines_tbl.count, 0) <= 0) THEN
5855         IF (l_No_Eligible_Lines)
5856         THEN
5857             IF (Fnd_Log.Level_Procedure >= Fnd_Log.G_Current_Runtime_Level)
5858             THEN
5859                 Fnd_Log.STRING(Fnd_Log.Level_Procedure,
5860                                lc_mod_name,
5861                                'no estimate lines available to autogenerate from BOM');
5862             END IF;
5863             RAISE CSD_EST_NO_AUTOGEN;
5864         END IF;
5865 
5866         IF (Fnd_Log.Level_Statement >= Fnd_Log.G_Current_Runtime_Level)
5867         THEN
5868             Fnd_Log.STRING(Fnd_Log.Level_Statement,
5869                            lc_mod_name || '.out_parameter',
5870                            'x_warning_flag: ' || x_warning_flag);
5871         END IF;
5872         IF (Fnd_Log.Level_Procedure >= Fnd_Log.G_Current_Runtime_Level)
5873         THEN
5874             Fnd_Log.STRING(Fnd_Log.Level_Procedure,
5875                            lc_mod_name || '.END',
5876                            'Leaving CSD_REPAIR_ESTIMATE_PVT.Get_Estimates_From_BOM');
5877         END IF;
5878 
5879         --
5880         -- End API Body
5881         --
5882 
5883         -- Standard check of p_commit.
5884         IF Fnd_Api.To_Boolean(p_commit)
5885         THEN
5886             COMMIT WORK;
5887         END IF;
5888 
5889         -- Standard call to get message count and IF count is  get message info.
5890         Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
5891                                   p_data  => x_msg_data);
5892     EXCEPTION
5893         WHEN CSD_EST_NO_AUTOGEN THEN
5894             x_warning_flag := Fnd_Api.G_TRUE;
5895 
5896             -- save message in fnd stack
5897             IF (Fnd_Log.Level_Statement >= Fnd_Log.G_Current_Runtime_Level)
5898             THEN
5899                 Fnd_Log.STRING(Fnd_Log.Level_Statement,
5900                                lc_mod_name,
5901                                'Adding message CSD_EST_NO_AUTOGEN to FND_MSG stack');
5902             END IF;
5903             Fnd_Message.SET_NAME('CSD', 'CSD_EST_NO_AUTOGEN');
5904             Fnd_Msg_Pub.ADD;
5905             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
5906                                       p_data  => x_msg_data);
5907 
5908             -- save message in debug log
5909             IF (Fnd_Log.Level_Exception >= Fnd_Log.G_Current_Runtime_Level)
5910             THEN
5911                 Fnd_Log.STRING(Fnd_Log.Level_Exception,
5912                                lc_mod_name,
5913                                'No estimate lines autogenerated');
5914             END IF;
5915         WHEN Fnd_Api.G_EXC_ERROR THEN
5916             ROLLBACK TO Get_Estimates_From_BOM;
5917             x_return_status := Fnd_Api.G_RET_STS_ERROR;
5918 
5919             /*
5920             -- TO DO: Add seeded err message
5921             -- save message in fnd stack
5922                 if (Fnd_Log.Level_Statement>= Fnd_Log.G_Current_Runtime_Level) then
5923                     FND_LOG.STRING(Fnd_Log.Level_Statement, lc_mod_name,
5924                                    'Adding message err_name to FND_MSG stack');
5925                 end if;
5926             FND_MESSAGE.SET_NAME('CSD','err_name');
5927             FND_MESSAGE.SET_TOKEN('toke_name', 'token_value');
5928             FND_MSG_PUB.ADD;
5929             */
5930             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
5931                                       p_data  => x_msg_data);
5932 
5933             -- save message in debug log
5934             IF (Fnd_Log.Level_Exception >= Fnd_Log.G_Current_Runtime_Level)
5935             THEN
5936                 Fnd_Log.STRING(Fnd_Log.Level_Exception,
5937                                lc_mod_name,
5938                                'EXC_ERROR[' || x_msg_data || ']');
5939             END IF;
5940 
5941         WHEN Fnd_Api.G_EXC_UNEXPECTED_ERROR THEN
5942             ROLLBACK TO Get_Estimates_From_BOM;
5943             x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
5944 
5945             -- save message in fnd stack
5946             IF Fnd_Msg_Pub.Check_Msg_Level(Fnd_Msg_Pub.G_MSG_LVL_UNEXP_ERROR)
5947             THEN
5948                 IF (Fnd_Log.Level_Statement >=
5949                    Fnd_Log.G_Current_Runtime_Level)
5950                 THEN
5951                     Fnd_Log.STRING(Fnd_Log.Level_Statement,
5952                                    lc_mod_name,
5953                                    'Adding message using FND_MSG_PUB.Add_Exc_Msg to FND_MSG stack');
5954                 END IF;
5955                 Fnd_Msg_Pub.Add_Exc_Msg(G_PKG_NAME, lc_api_name);
5956             END IF;
5957             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
5958                                       p_data  => x_msg_data);
5959 
5960             -- save message in debug log
5961             IF (Fnd_Log.Level_Exception >= Fnd_Log.G_Current_Runtime_Level)
5962             THEN
5963                 Fnd_Log.STRING(Fnd_Log.Level_Exception,
5964                                lc_mod_name,
5965                                'EXC_UNEXPECTED_ERROR[' || x_msg_data || ']');
5966             END IF;
5967 
5968         WHEN OTHERS THEN
5969             ROLLBACK TO Get_Estimates_From_BOM;
5970             x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
5971             IF Fnd_Msg_Pub.Check_Msg_Level(Fnd_Msg_Pub.G_MSG_LVL_UNEXP_ERROR)
5972             THEN
5973                 IF (Fnd_Log.Level_Statement >=
5974                    Fnd_Log.G_Current_Runtime_Level)
5975                 THEN
5976                     Fnd_Log.STRING(Fnd_Log.Level_Statement,
5977                                    lc_mod_name,
5978                                    'Adding message using FND_MSG_PUB.Add_Exc_Msg to FND_MSG stack');
5979                 END IF;
5980                 Fnd_Msg_Pub.Add_Exc_Msg(G_PKG_NAME, lc_api_name);
5981             END IF;
5982             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
5983                                       p_data  => x_msg_data);
5984 
5985             -- save message in debug log
5986             IF (Fnd_Log.Level_Exception >= Fnd_Log.G_Current_Runtime_Level)
5987             THEN
5988                 -- create a seeded message
5989                 Fnd_Log.STRING(Fnd_Log.Level_Exception,
5990                                lc_mod_name,
5991                                'SQL Message[' || SQLERRM || ']');
5992             END IF;
5993     END Get_Estimates_From_BOM;
5994 
5995     /*--------------------------------------------------*/
5996     /* procedure name: Convert_To_Est_Lines             */
5997     /* description   : The procedure to manipulate      */
5998     /*                 different structures. It converts*/
5999     /*                 data from MLE_LINES_REC_TYPE to  */
6000     /*                 REPAIR_ESTIMATE_LINE_REC. It also*/
6001     /*                 sets the item cost and logs      */
6002     /*                 warnings.                        */
6003     /*                                                  */
6004     /* x_warning_flag : FND_API.G_TRUE if any non-fatal */
6005     /*                  errors occured. FND_API.G_FALSE */
6006     /*                  if everything was successful.   */
6007     /*                  Note that this value could be   */
6008     /*                  G_TRUE even if x_return_status  */
6009     /*                  is G_RET_STS_SUCCESS            */
6010     /* called from:  Get_Estimates_From_BOM             */
6011     /*               Get_Estimates_From_Task            */
6012     /*--------------------------------------------------*/
6013     PROCEDURE Convert_To_Est_Lines(p_api_version         IN NUMBER,
6014                                    p_commit              IN VARCHAR2,
6015                                    p_init_msg_list       IN VARCHAR2,
6016                                    p_validation_level    IN NUMBER,
6017                                    x_return_status       OUT NOCOPY VARCHAR2,
6018                                    x_msg_count           OUT NOCOPY NUMBER,
6019                                    x_msg_data            OUT NOCOPY VARCHAR2,
6020                                    p_repair_line_id      IN NUMBER,
6021                                    p_estimate_id         IN NUMBER,
6022                                    p_repair_type_id      IN NUMBER,
6023                                    p_business_process_id IN NUMBER,
6024                                    p_currency_code       IN VARCHAR2,
6025                                    p_incident_id         IN NUMBER,
6026                                    p_organization_id     IN NUMBER,
6027                                    p_price_list_id       IN NUMBER,
6028                                    p_contract_line_id    IN NUMBER,
6029                                    p_MLE_lines_tbl       IN MLE_LINES_TBL_TYPE,
6030                                    x_est_lines_tbl       IN OUT NOCOPY REPAIR_ESTIMATE_LINE_TBL,
6031                                    x_warning_flag        OUT NOCOPY VARCHAR2) IS
6032         -- CONSTANTS --
6033         lc_debug_level CONSTANT NUMBER := Fnd_Log.G_CURRENT_RUNTIME_LEVEL;
6034         lc_stat_level  CONSTANT NUMBER := Fnd_Log.LEVEL_STATEMENT;
6035         lc_proc_level  CONSTANT NUMBER := Fnd_Log.LEVEL_PROCEDURE;
6036         lc_event_level CONSTANT NUMBER := Fnd_Log.LEVEL_EVENT;
6037         lc_excep_level CONSTANT NUMBER := Fnd_Log.LEVEL_EXCEPTION;
6038         lc_error_level CONSTANT NUMBER := Fnd_Log.LEVEL_ERROR;
6039         lc_unexp_level CONSTANT NUMBER := Fnd_Log.LEVEL_UNEXPECTED;
6040         lc_mod_name    CONSTANT VARCHAR2(100) := 'csd.plsql.csd_repair_estimate_pvt.convert_to_est_lines';
6041         lc_api_name    CONSTANT VARCHAR2(30) := 'Convert_To_Est_Lines';
6042         lc_api_version CONSTANT NUMBER := 1.0;
6043 
6044         -- VARIABLES --
6045         l_error           BOOLEAN;
6046         l_numRows         NUMBER;
6047         l_curRow          NUMBER;
6048         l_price_list_name VARCHAR2(240);
6049         l_num_subtypes    NUMBER;
6050         l_ext_price       NUMBER;
6051         l_selling_price   NUMBER;
6052         l_no_charge_flag  VARCHAR2(1);
6053 
6054         l_return_status VARCHAR2(1);
6055         l_msg_count     NUMBER;
6056         l_msg_data      VARCHAR2(2000);
6057 
6058         -- Pricing attributes
6059         l_pricing_rec Csd_Process_Util.pricing_attr_rec;
6060 		--bug#3875036
6061 		l_account_id						NUMBER        := NULL;
6062 
6063         -- cursors --
6064         /* -- no longer validating this due to performance
6065          cursor count_csi_txn_subtypes (p_txn_billing_type_id NUMBER) is
6066             -- number of txn subtypes for a given txn billing type
6067             SELECT count(*)
6068             FROM   csi_txn_sub_types ib,
6069                    cs_txn_billing_types cs
6070             WHERE  ib.cs_transaction_type_id = cs.transaction_type_id
6071                    and cs.txn_billing_type_id = p_txn_billing_type_id;
6072         */
6073 
6074         CURSOR c_transaction_type(p_txn_billing_type_id NUMBER) IS
6075             SELECT cs.transaction_type_id
6076               FROM cs_txn_billing_types cs
6077              WHERE cs.txn_billing_type_id = p_txn_billing_type_id;
6078 
6079     BEGIN
6080         -- Standard Start of API savepoint
6081         SAVEPOINT Convert_To_Est_Lines;
6082 
6083         -- Standard call to check for call compatibility.
6084         IF NOT Fnd_Api.Compatible_API_Call(lc_api_version,
6085                                            p_api_version,
6086                                            lc_api_name,
6087                                            G_PKG_NAME)
6088         THEN
6089             RAISE Fnd_Api.G_EXC_UNEXPECTED_ERROR;
6090         END IF;
6091 
6092         -- Initialize message list if p_init_msg_list is set to TRUE.
6093         IF Fnd_Api.to_Boolean(p_init_msg_list)
6094         THEN
6095             Fnd_Msg_Pub.initialize;
6096         END IF;
6097 
6098         IF (Fnd_Log.Level_Procedure >= Fnd_Log.G_Current_Runtime_Level)
6099         THEN
6100             Fnd_Log.STRING(Fnd_Log.Level_Procedure,
6101                            lc_mod_name || '.BEGIN',
6102                            'Entering CSD_REPAIR_ESTIMATE_PVT.Convert_To_Est_Lines');
6103         END IF;
6104         -- log parameters
6105         IF (Fnd_Log.Level_Statement >= Fnd_Log.G_Current_Runtime_Level)
6106         THEN
6107             Fnd_Log.STRING(Fnd_Log.Level_Statement,
6108                            lc_mod_name || '.parameter_logging',
6109                            'p_api_version: ' || p_api_version);
6110             Fnd_Log.STRING(Fnd_Log.Level_Statement,
6111                            lc_mod_name || '.parameter_logging',
6112                            'p_commit: ' || p_commit);
6113             Fnd_Log.STRING(Fnd_Log.Level_Statement,
6114                            lc_mod_name || '.parameter_logging',
6115                            'p_init_msg_list: ' || p_init_msg_list);
6116             Fnd_Log.STRING(Fnd_Log.Level_Statement,
6117                            lc_mod_name || '.parameter_logging',
6118                            'p_validation_level: ' || p_validation_level);
6119             Fnd_Log.STRING(Fnd_Log.Level_Statement,
6120                            lc_mod_name || '.parameter_logging',
6121                            'p_repair_line_id: ' || p_repair_line_id);
6122             Fnd_Log.STRING(Fnd_Log.Level_Statement,
6123                            lc_mod_name || '.parameter_logging',
6124                            'p_estimate_id: ' || p_estimate_id);
6125             Fnd_Log.STRING(Fnd_Log.Level_Statement,
6126                            lc_mod_name || '.parameter_logging',
6127                            'p_repair_type_id: ' || p_repair_type_id);
6128             Fnd_Log.STRING(Fnd_Log.Level_Statement,
6129                            lc_mod_name || '.parameter_logging',
6130                            'p_business_process_id: ' ||
6131                            p_business_process_id);
6132             Fnd_Log.STRING(Fnd_Log.Level_Statement,
6133                            lc_mod_name || '.parameter_logging',
6134                            'p_currency_code: ' || p_currency_code);
6135             Fnd_Log.STRING(Fnd_Log.Level_Statement,
6136                            lc_mod_name || '.parameter_logging',
6137                            'p_incident_id: ' || p_incident_id);
6138             Fnd_Log.STRING(Fnd_Log.Level_Statement,
6139                            lc_mod_name || '.parameter_logging',
6140                            'p_organization_id: ' || p_organization_id);
6141             Fnd_Log.STRING(Fnd_Log.Level_Statement,
6142                            lc_mod_name || '.parameter_logging',
6143                            'p_price_list_id: ' || p_price_list_id);
6144             Fnd_Log.STRING(Fnd_Log.Level_Statement,
6145                            lc_mod_name || '.parameter_logging',
6146                            'p_contract_line_id: ' || p_contract_line_id);
6147         END IF;
6148 
6149         -- Initialize API return status to success
6150         x_return_status := Fnd_Api.G_RET_STS_SUCCESS;
6151 
6152         -- initialize the warning flag
6153         x_warning_flag := Fnd_Api.G_FALSE;
6154 
6155 	/* bug#3875036 */
6156 		l_account_id := CSD_CHARGE_LINE_UTIL.Get_SR_AccountId(p_repair_line_id);
6157 
6158         --
6159         -- Begin API Body
6160         --
6161         l_numRows := p_MLE_lines_tbl.COUNT;
6162         FOR i IN 1 .. l_numRows
6163         LOOP
6164             l_error := FALSE;
6165             IF (Fnd_Log.Level_Procedure >= Fnd_Log.G_Current_Runtime_Level)
6166             THEN
6167                 Fnd_Log.STRING(Fnd_Log.Level_Procedure,
6168                                lc_mod_name,
6169                                'Processing MLE line ' || i);
6170             END IF;
6171 
6172             -- Initialize the selling price variable.
6173             -- bugfix 3468680, vkjain.
6174             l_selling_price := NULL;
6175 
6176             -- get the selling price of the item
6177             -- if no selling price, then we cannot determine charge, so
6178             -- log a warning.
6179             BEGIN
6180                 IF (Fnd_Log.Level_Procedure >=
6181                    Fnd_Log.G_Current_Runtime_Level)
6182                 THEN
6183                     Fnd_Log.STRING(Fnd_Log.Level_Procedure,
6184                                    lc_mod_name,
6185                                    'Calling CSD_PROCESS_UTIL.get_charge_selling_price');
6186                 END IF;
6187 
6188                 Csd_Process_Util.GET_CHARGE_SELLING_PRICE(p_inventory_item_id    => p_MLE_lines_tbl(i)
6189                                                                                    .inventory_item_id,
6190                                                           p_price_list_header_id => p_price_list_id,
6191                                                           p_unit_of_measure_code => p_MLE_lines_tbl(i).uom,
6192                                                           p_currency_code        => p_currency_code,
6193                                                           p_quantity_required    => p_MLE_lines_tbl(i).quantity,
6194 														  p_account_id			  => l_account_id,  /* bug#3875036 */
6195 														  p_org_id => p_organization_id, -- added for R12
6196                                                           p_pricing_rec          => l_pricing_rec,
6197                                                           x_selling_price        => l_selling_price,
6198                                                           x_return_status        => l_return_status,
6199                                                           x_msg_count            => l_msg_count,
6200                                                           x_msg_data             => l_msg_data);
6201                 IF (Fnd_Log.Level_Procedure >=
6202                    Fnd_Log.G_Current_Runtime_Level)
6203                 THEN
6204                     Fnd_Log.STRING(Fnd_Log.Level_Procedure,
6205                                    lc_mod_name,
6206                                    'Returned from CSD_PROCESS_UTIL.get_charge_selling_price');
6207                 END IF;
6208                 IF (Fnd_Log.Level_Statement >=
6209                    Fnd_Log.G_Current_Runtime_Level)
6210                 THEN
6211                     Fnd_Log.STRING(Fnd_Log.Level_Statement,
6212                                    lc_mod_name,
6213                                    'l_selling_price = ' || l_selling_price);
6214                 END IF;
6215             EXCEPTION
6216                 WHEN Fnd_Api.G_EXC_ERROR THEN
6217                     l_return_status := Fnd_Api.G_RET_STS_ERROR;
6218                     IF (Fnd_Log.Level_Procedure >=
6219                        Fnd_Log.G_Current_Runtime_Level)
6220                     THEN
6221                         Fnd_Log.STRING(Fnd_Log.Level_Procedure,
6222                                        lc_mod_name,
6223                                        'Exception FND_API.G_EXC_ERROR occurred in CSD_PROCESS_UTIL.get_charge_selling_price');
6224                     END IF;
6225                 WHEN OTHERS THEN
6226                     l_return_status := Fnd_Api.G_RET_STS_ERROR;
6227                     IF (Fnd_Log.Level_Procedure >=
6228                        Fnd_Log.G_Current_Runtime_Level)
6229                     THEN
6230                         Fnd_Log.STRING(Fnd_Log.Level_Procedure,
6231                                        lc_mod_name,
6232                                        'Exception OTHERS occurred in CSD_PROCESS_UTIL.get_charge_selling_price');
6233                     END IF;
6234             END;
6235 
6236             IF (l_return_status <> Fnd_Api.G_RET_STS_SUCCESS) OR
6237                (l_selling_price IS NULL)
6238             THEN
6239                 IF (Fnd_Log.Level_Statement >=
6240                    Fnd_Log.G_Current_Runtime_Level)
6241                 THEN
6242                     Fnd_Log.STRING(Fnd_Log.Level_Statement,
6243                                    lc_mod_name,
6244                                    'Adding message CSD_EST_NOPRICE_ITEM_UOM to FND_MSG stack');
6245                 END IF;
6246                 Fnd_Message.SET_NAME('CSD', 'CSD_EST_NOPRICE_ITEM_UOM');
6247                 BEGIN
6248                     SELECT name
6249                       INTO l_price_list_name
6250                       FROM qp_list_headers_tl
6251                      WHERE list_header_id = p_price_list_id;
6252                 EXCEPTION
6253                     WHEN NO_DATA_FOUND THEN
6254                         l_price_list_name := p_price_list_id;
6255                         IF (Fnd_Log.Level_Procedure >=
6256                            Fnd_Log.G_Current_Runtime_Level)
6257                         THEN
6258                             Fnd_Log.STRING(Fnd_Log.Level_Procedure,
6259                                            lc_mod_name,
6260                                            'Exception NO_DATA_FOUND occurred while querying for price list name');
6261                         END IF;
6262                     WHEN OTHERS THEN
6263                         l_price_list_name := p_price_list_id;
6264                         IF (Fnd_Log.Level_Procedure >=
6265                            Fnd_Log.G_Current_Runtime_Level)
6266                         THEN
6267                             Fnd_Log.STRING(Fnd_Log.Level_Procedure,
6268                                            lc_mod_name,
6269                                            'Exception OTHERS occurred while querying for price list name');
6270                         END IF;
6271                 END;
6272                 Fnd_Message.SET_TOKEN('ITEM', p_MLE_lines_tbl(i).item_name);
6273                 Fnd_Message.SET_TOKEN('PRICELIST', l_price_list_name);
6274                 Fnd_Message.SET_TOKEN('UOM', p_MLE_lines_tbl(i).uom);
6275                 Fnd_Msg_Pub.ADD;
6276                 l_error := TRUE;
6277                 IF (Fnd_Log.Level_Statement >=
6278                    Fnd_Log.G_Current_Runtime_Level)
6279                 THEN
6280                     Fnd_Log.STRING(Fnd_Log.Level_Statement,
6281                                    lc_mod_name,
6282                                    'Unable to get price for item-uom combination in pricelist ' ||
6283                                    p_price_list_id);
6284                 END IF;
6285             END IF;
6286 
6287             -- check txn billing type
6288             IF (Fnd_Log.Level_Procedure >= Fnd_Log.G_Current_Runtime_Level)
6289             THEN
6290                 Fnd_Log.STRING(Fnd_Log.Level_Procedure,
6291                                lc_mod_name,
6292                                'Checking for required txn billing type id');
6293             END IF;
6294             IF (p_MLE_lines_tbl(i).txn_billing_type_id IS NULL)
6295             THEN
6296                 IF (Fnd_Log.Level_Statement >=
6297                    Fnd_Log.G_Current_Runtime_Level)
6298                 THEN
6299                     Fnd_Log.STRING(Fnd_Log.Level_Statement,
6300                                    lc_mod_name,
6301                                    'Adding message CSD_EST_NO_ITEM_SAR to FND_MSG stack');
6302                 END IF;
6303                 Fnd_Message.SET_NAME('CSD', 'CSD_EST_NO_ITEM_SAR');
6304                 Fnd_Message.SET_TOKEN('ITEM', p_MLE_lines_tbl(i).item_name);
6305                 Fnd_Msg_Pub.ADD;
6306                 l_error := TRUE;
6307                 IF (Fnd_Log.Level_Statement >=
6308                    Fnd_Log.G_Current_Runtime_Level)
6309                 THEN
6310                     Fnd_Log.STRING(Fnd_Log.Level_Statement,
6311                                    lc_mod_name,
6312                                    'Error - txn_billing_type_id is null');
6313                 ELSE
6314                     IF (Fnd_Log.Level_Procedure >=
6315                        Fnd_Log.G_Current_Runtime_Level)
6316                     THEN
6317                         Fnd_Log.STRING(Fnd_Log.Level_Procedure,
6318                                        lc_mod_name,
6319                                        'Txn billing type id is available');
6320                     END IF;
6321                 END IF;
6322 
6323             END IF;
6324 
6325             IF (l_error = FALSE)
6326             THEN
6327                 l_curRow := x_est_lines_tbl.COUNT + 1;
6328 
6329                 -- default no_charge if it is set up in charges
6330                 -- this is probably a rare case
6331                 IF (Fnd_Log.Level_Procedure >=
6332                    Fnd_Log.G_Current_Runtime_Level)
6333                 THEN
6334                     Fnd_Log.STRING(Fnd_Log.Level_Procedure,
6335                                    lc_mod_name,
6336                                    'Getting no charge flag to determine extended price');
6337                 END IF;
6338                 l_no_charge_flag := Csd_Process_Util.Get_No_Chg_Flag(p_MLE_lines_tbl(i)
6339                                                                      .txn_billing_type_id);
6340                 IF (NVL(l_no_charge_flag, 'N') = 'Y')
6341                 THEN
6342                     l_ext_price := 0;
6343                 ELSE
6344                     -- l_ext_price := nvl(p_MLE_lines_tbl(i).quantity, 0) * nvl(p_MLE_lines_tbl(i).selling_price,0);
6345                     -- bugfix 3468680 vkjain. Using the price from the Pricing Engine API.
6346                     l_ext_price := NVL(p_MLE_lines_tbl(i).quantity, 0) *
6347                                    l_selling_price;
6348                 END IF;
6349                 IF (Fnd_Log.Level_Statement >=
6350                    Fnd_Log.G_Current_Runtime_Level)
6351                 THEN
6352                     Fnd_Log.STRING(Fnd_Log.Level_Statement,
6353                                    lc_mod_name,
6354                                    'l_no_charge_flag = ' ||
6355                                    l_no_charge_flag);
6356                     Fnd_Log.STRING(Fnd_Log.Level_Statement,
6357                                    lc_mod_name,
6358                                    'l_ext_price = ' || l_ext_price);
6359                 END IF;
6360 
6361                 --
6362                 -- Derive after waranty cost
6363                 -- note: this portion of code is very similar to
6364                 -- ESTIMATE_UTILS.copy_unitprice_nochg_values in CSDREPLN.pld
6365                 -- consider modularizing code for this
6366                 --
6367                 IF (p_contract_line_id IS NOT NULL) AND
6368                    (p_business_process_id IS NOT NULL) AND
6369                    (NVL(l_no_charge_flag, 'N') = 'N')
6370                 THEN
6371                     BEGIN
6372                         IF (Fnd_Log.Level_Procedure >=
6373                            Fnd_Log.G_Current_Runtime_Level)
6374                         THEN
6375                             Fnd_Log.STRING(Fnd_Log.Level_Procedure,
6376                                            lc_mod_name,
6377                                            'Calling CSD_CHARGE_LINE_UTIL.Get_CoverageInfo');
6378                         END IF;
6379                         Csd_Charge_Line_Util.Get_CoverageInfo(p_contract_line_id      => p_contract_line_id,
6380                                                               p_business_process_id   => p_business_process_id,
6381                                                               x_return_status         => x_return_status,
6382                                                               x_msg_count             => x_msg_count,
6383                                                               x_msg_data              => x_msg_data,
6384                                                               x_contract_id           => x_est_lines_tbl(l_curRow)
6385                                                                                         .contract_id,
6386                                                               x_contract_number       => x_est_lines_tbl(l_curRow)
6387                                                                                         .contract_number,
6388                                                               x_coverage_id           => x_est_lines_tbl(l_curRow)
6389                                                                                         .coverage_id,
6390                                                               x_coverage_txn_group_id => x_est_lines_tbl(l_curRow)
6391                                                                                         .coverage_txn_group_id);
6392                         IF (Fnd_Log.Level_Statement >=
6393                            Fnd_Log.G_Current_Runtime_Level)
6394                         THEN
6395                             Fnd_Log.STRING(Fnd_Log.Level_Statement,
6396                                            lc_mod_name,
6397                                            'x_contract_id = ' ||
6398                                             x_est_lines_tbl(l_curRow)
6399                                            .contract_id);
6400                             Fnd_Log.STRING(Fnd_Log.Level_Statement,
6401                                            lc_mod_name,
6402                                            'x_contract_number = ' ||
6403                                             x_est_lines_tbl(l_curRow)
6404                                            .contract_number);
6405                             Fnd_Log.STRING(Fnd_Log.Level_Statement,
6406                                            lc_mod_name,
6407                                            'x_coverage_id = ' ||
6408                                             x_est_lines_tbl(l_curRow)
6409                                            .coverage_id);
6410                             Fnd_Log.STRING(Fnd_Log.Level_Statement,
6411                                            lc_mod_name,
6412                                            'x_coverage_txn_group_id = ' ||
6413                                             x_est_lines_tbl(l_curRow)
6414                                            .coverage_txn_group_id);
6415                         END IF;
6416 
6417                     EXCEPTION
6418                         WHEN Fnd_Api.G_EXC_ERROR THEN
6419                             l_return_status := Fnd_Api.G_RET_STS_ERROR;
6420                         WHEN OTHERS THEN
6421                             l_return_status := Fnd_Api.G_RET_STS_ERROR;
6422                     END;
6423 
6424                     IF (l_return_status <> Fnd_Api.G_RET_STS_SUCCESS)
6425                     THEN
6426                         l_error := TRUE;
6427                     END IF;
6428 
6429                     BEGIN
6430                         IF (Fnd_Log.Level_Procedure >=
6431                            Fnd_Log.G_Current_Runtime_Level)
6432                         THEN
6433                             Fnd_Log.STRING(Fnd_Log.Level_Procedure,
6434                                            lc_mod_name,
6435                                            'Calling CSD_CHARGE_LINE_UTIL.get_discountedprice');
6436                         END IF;
6437                         Csd_Charge_Line_Util.GET_DISCOUNTEDPRICE(p_api_version         => 1.0,
6438                                                                  p_init_msg_list       => Fnd_Api.G_FALSE,
6439                                                                  p_contract_line_id    => p_contract_line_id,
6440                                                                  p_repair_type_id      => p_repair_type_id,
6441                                                                  p_txn_billing_type_id => p_MLE_lines_tbl(i)
6442                                                                                          .txn_billing_type_id,
6443                                                                  p_coverage_txn_grp_id => x_est_lines_tbl(l_curRow)
6444                                                                                          .coverage_txn_group_id,
6445                                                                  p_extended_price      => l_ext_price,
6446                                                                  p_no_charge_flag      => l_no_charge_flag,
6447                                                                  x_discounted_price    => x_est_lines_tbl(l_curRow)
6448                                                                                          .after_warranty_cost,
6449                                                                  x_return_status       => l_return_status,
6450                                                                  x_msg_count           => l_msg_count,
6451                                                                  x_msg_data            => l_msg_data);
6452                         IF (Fnd_Log.Level_Statement >=
6453                            Fnd_Log.G_Current_Runtime_Level)
6454                         THEN
6455                             Fnd_Log.STRING(Fnd_Log.Level_Statement,
6456                                            lc_mod_name,
6457                                            'x_discounted_price = ' ||
6458                                             x_est_lines_tbl(l_curRow)
6459                                            .after_warranty_cost);
6460                         END IF;
6461                     EXCEPTION
6462                         WHEN Fnd_Api.G_EXC_ERROR THEN
6463                             l_return_status := Fnd_Api.G_RET_STS_ERROR;
6464                         WHEN OTHERS THEN
6465                             l_return_status := Fnd_Api.G_RET_STS_ERROR;
6466                     END;
6467                     IF (l_return_status <> Fnd_Api.G_RET_STS_SUCCESS)
6468                     THEN
6469                         IF (Fnd_Log.Level_Statement >=
6470                            Fnd_Log.G_Current_Runtime_Level)
6471                         THEN
6472                             Fnd_Log.STRING(Fnd_Log.Level_Statement,
6473                                            lc_mod_name,
6474                                            'Adding message CSD_EST_ESTIMATED_CHARGE_ERR to FND_MSG stack');
6475                         END IF;
6476                         Fnd_Message.SET_NAME('CSD',
6477                                              'CSD_EST_ESTIMATED_CHARGE_ERR');
6478                         Fnd_Message.SET_TOKEN('CONTRACT_NUMBER',
6479                                               x_est_lines_tbl(l_curRow)
6480                                               .contract_number);
6481                         Fnd_Msg_Pub.ADD;
6482                         l_error := TRUE;
6483                     END IF;
6484                     -- Bugfix 3843770, vkjain.
6485                     -- The contract discount field is not being populated for AutoCreate
6486                     -- estimate lines.
6487                     x_est_lines_tbl(l_curRow).contract_discount_amount := l_ext_price -
6488                                                                           NVL(x_est_lines_tbl(l_curRow)
6489                                                                               .after_warranty_cost,
6490                                                                               0);
6491                 ELSE
6492                     -- p_contract_line_id is null or p_business_process_id is null
6493                     x_est_lines_tbl(l_curRow).after_warranty_cost := l_ext_price;
6494                 END IF; -- end derive after warranty cost
6495 
6496                 -- Added by vkjain. 3449978.
6497                 x_est_lines_tbl(l_curRow).resource_id := p_MLE_lines_tbl(i)
6498                                                         .resource_id;
6499 
6500                 BEGIN
6501                     IF (Fnd_Log.Level_Procedure >=
6502                        Fnd_Log.G_Current_Runtime_Level)
6503                     THEN
6504                         Fnd_Log.STRING(Fnd_Log.Level_Procedure,
6505                                        lc_mod_name,
6506                                        'Calling CSD_COST_ANALYSIS_PVT.arcs in forms/US/CSDREPLN.fmb
6507 				   ItemCost');
6508                     END IF;
6509 
6510                     -- Bugfix 3449978. vkjain.
6511                     -- We attempt to get the resource_id for labor lines.
6512                     -- The Get_InvItemCost procedure is replaced by
6513                     -- Get_ResItemCost. The Get_ResItemCost procedure
6514                     -- gets the reosurce cost if it exists and otherwise
6515                     -- gets the item cost.
6516                     -- CSD_COST_ANALYSIS_PVT.Get_InvItemCost(
6517                     Csd_Cost_Analysis_Pvt.Get_ResItemCost(
6518                                                           -- p_api_version           =>     1.0,
6519                                                           -- p_commit                =>     fnd_api.g_false,
6520                                                           -- p_init_msg_list         =>     fnd_api.g_false,
6521                                                           -- p_validation_level      =>     fnd_api.g_valid_level_full,
6522                                                           x_return_status     => l_return_status,
6523                                                           x_msg_count         => l_msg_count,
6524                                                           x_msg_data          => l_msg_data,
6525                                                           p_inventory_item_id => p_MLE_lines_tbl(i)
6526                                                                                 .inventory_item_id,
6527                                                           p_organization_id   => p_organization_id,
6528                                                           p_bom_resource_id   => p_MLE_lines_tbl(i)
6529                                                                                 .resource_id,
6530                                                           p_charge_date       => SYSDATE,
6531                                                           p_currency_code     => p_currency_code,
6532                                                           p_chg_line_uom_code => p_MLE_lines_tbl(i).uom, --sangigup 3356020
6533                                                           x_item_cost         => x_est_lines_tbl(l_curRow)
6534                                                                                 .item_cost);
6535                     IF (Fnd_Log.Level_Statement >=
6536                        Fnd_Log.G_Current_Runtime_Level)
6537                     THEN
6538                         Fnd_Log.STRING(Fnd_Log.Level_Statement,
6539                                        lc_mod_name,
6540                                        'x_item_cost = ' ||
6541                                        x_est_lines_tbl(l_curRow).item_cost);
6542                     END IF;
6543                 EXCEPTION
6544                     WHEN Fnd_Api.G_EXC_ERROR THEN
6545                         l_return_status := Fnd_Api.G_RET_STS_ERROR;
6546                     WHEN OTHERS THEN
6547                         l_return_status := Fnd_Api.G_RET_STS_ERROR;
6548                 END;
6549 
6550                 IF (l_return_status <> Fnd_Api.G_RET_STS_SUCCESS)
6551                 THEN
6552                     x_est_lines_tbl(l_curRow).item_cost := NULL;
6553                     --
6554                     -- TO DO: give warning message that cost could not be determined?
6555                     -- x_warning_flag  := FND_API.G_TRUE;
6556                     -- x_return_status := FND_API.G_RET_STS_ERROR;
6557                     --    if (Fnd_Log.Level_Statement>= Fnd_Log.G_Current_Runtime_Level) then
6558                     --        FND_LOG.STRING(Fnd_Log.Level_Statement, lc_mod_name,
6559                     --                       'Adding message CSD_EST_ESTIMATED_CHARGE_ERR to FND_MSG stack');
6560                     --    end if;
6561                     -- FND_MESSAGE.SET_NAME('CSD','CSD_EST_ESTIMATED_CHARGE_ERR');
6562                     -- FND_MESSAGE.SET_TOKEN('CONTRACT_NUMBER',x_est_lines_tbl(l_curRow).contract_number);
6563                     -- FND_MSG_PUB.ADD;
6564                     -- l_error := TRUE;
6565                 END IF;
6566 
6567                 -- set transaction type (Material, labor, expense)
6568                 IF (Fnd_Log.Level_Procedure >=
6569                    Fnd_Log.G_Current_Runtime_Level)
6570                 THEN
6571                     Fnd_Log.STRING(Fnd_Log.Level_Procedure,
6572                                    lc_mod_name,
6573                                    'Getting transaction type id');
6574                 END IF;
6575                 OPEN c_transaction_type(p_MLE_lines_tbl(i)
6576                                         .txn_billing_type_id);
6577                 FETCH c_transaction_type
6578                     INTO x_est_lines_tbl(l_curRow) .transaction_type_id;
6579                 CLOSE c_transaction_type;
6580 
6581                 -- values from individual params
6582                 x_est_lines_tbl(l_curRow).repair_estimate_id := p_estimate_id; -- required param
6583                 x_est_lines_tbl(l_curRow).repair_line_id := p_repair_line_id; -- required param
6584                 x_est_lines_tbl(l_curRow).incident_id := p_incident_id; -- derived value, do we need this?
6585                 x_est_lines_tbl(l_curRow).business_process_id := p_business_process_id; -- optional value?, used if derived value not available
6586                 x_est_lines_tbl(l_curRow).currency_code := p_currency_code; -- derived value, do we need this?
6587                 x_est_lines_tbl(l_curRow).organization_id := p_organization_id; -- required param
6588                 x_est_lines_tbl(l_curRow).price_list_id := p_price_list_id; -- required param
6589 
6590                 -- values from MLE_LINES
6591                 x_est_lines_tbl(l_curRow).txn_billing_type_id := p_MLE_lines_tbl(i)
6592                                                                 .txn_billing_type_id; -- required param
6593                 x_est_lines_tbl(l_curRow).inventory_item_id := p_MLE_lines_tbl(i)
6594                                                               .inventory_item_id; -- required param
6595                 x_est_lines_tbl(l_curRow).unit_of_measure_code := p_MLE_lines_tbl(i).uom; -- required param
6596                 x_est_lines_tbl(l_curRow).estimate_quantity := p_MLE_lines_tbl(i)
6597                                                               .quantity; -- required param
6598                 -- x_est_lines_tbl(l_curRow).selling_price        := p_MLE_lines_tbl(i).selling_price;
6599                 -- bugfix 3468680, vkjain.
6600                 -- Setting the selling price that is from the Pricing Engine.
6601                 x_est_lines_tbl(l_curRow).selling_price := l_selling_price;
6602                 x_est_lines_tbl(l_curRow).est_line_source_type_code := p_MLE_lines_tbl(i)
6603                                                                       .est_line_source_type_code;
6604                 x_est_lines_tbl(l_curRow).est_line_source_id1 := p_MLE_lines_tbl(i)
6605                                                                 .est_line_source_id1;
6606                 x_est_lines_tbl(l_curRow).est_line_source_id2 := p_MLE_lines_tbl(i)
6607                                                                 .est_line_source_id2;
6608                 x_est_lines_tbl(l_curRow).ro_service_code_id := p_MLE_lines_tbl(i)
6609                                                                .ro_service_code_id;
6610                 -- bug#7212629, subhat.
6611                 -- the contract line id information is not passed to
6612                 -- charges API, but the contract id is passed.
6613                 -- we need to pass contract line id, if not
6614                 -- CSD_CHARGE_LINE_UTIL.GET_DISCOUNTEDPRICE will fail in CSDESTIM.pld.
6615 
6616                 x_est_lines_tbl(l_curRow).contract_line_id := p_contract_line_id;
6617                 --end bug#7212629, subhat.
6618 
6619                 -- null items
6620                 x_est_lines_tbl(l_curRow).reference_number := NULL;
6621                 x_est_lines_tbl(l_curRow).order_number := NULL;
6622                 x_est_lines_tbl(l_curRow).source_number := NULL;
6623                 x_est_lines_tbl(l_curRow).original_source_number := NULL;
6624                 x_est_lines_tbl(l_curRow).original_system_reference := NULL;
6625                 x_est_lines_tbl(l_curRow).lot_number := NULL;
6626                 x_est_lines_tbl(l_curRow).instance_id := NULL;
6627                 x_est_lines_tbl(l_curRow).instance_number := NULL;
6628                 x_est_lines_tbl(l_curRow).coverage_bill_rate_id := NULL;
6629                 x_est_lines_tbl(l_curRow).sub_inventory := NULL;
6630                 x_est_lines_tbl(l_curRow).return_reason := NULL;
6631                 x_est_lines_tbl(l_curRow).last_update_date := NULL;
6632                 x_est_lines_tbl(l_curRow).last_updated_by := NULL;
6633                 x_est_lines_tbl(l_curRow).created_by := NULL;
6634                 x_est_lines_tbl(l_curRow).last_update_login := NULL;
6635                 x_est_lines_tbl(l_curRow).security_group_id := NULL;
6636 
6637                 -- non-null items
6638                 x_est_lines_tbl(l_curRow).return_by_date := SYSDATE;
6639                 x_est_lines_tbl(l_curRow).creation_date := SYSDATE;
6640                 x_est_lines_tbl(l_curRow).original_source_code := 'DR'; -- check for constants
6641                 x_est_lines_tbl(l_curRow).source_code := 'DR';
6642                 x_est_lines_tbl(l_curRow).charge_line_type := 'ESTIMATE'; -- check for constants to use
6643                 x_est_lines_tbl(l_curRow).no_charge_flag := l_no_charge_flag;
6644                 x_est_lines_tbl(l_curRow).override_charge_flag := NVL(l_no_charge_flag,
6645                                                                       'N'); -- charge is only overridden if no charge
6646                 x_est_lines_tbl(l_curRow).interface_to_om_flag := 'N';
6647 
6648                 -- TO DO: get default po number from repair estimate
6649                 -- x_est_lines_tbl(l_curRow).purchase_order_num        := name_in('repair_estimate_line_det.po_number');
6650 
6651             END IF; -- end if l_error = FALSE.
6652 
6653             -- Note: Do NOT elsif this statement with the above "if (l_error = FALSE)",
6654             -- since the above section could potentially set l_error=TRUE as well.
6655             IF (l_error = TRUE)
6656             THEN
6657                 x_warning_flag := Fnd_Api.G_TRUE;
6658                 IF (Fnd_Log.Level_Procedure >=
6659                    Fnd_Log.G_Current_Runtime_Level)
6660                 THEN
6661                     Fnd_Log.STRING(Fnd_Log.Level_Procedure,
6662                                    lc_mod_name,
6663                                    'Calling CSD_GEN_ERRMSGS_PVT.Save_Fnd_Msgs');
6664                     Fnd_Log.STRING(Fnd_Log.Level_Procedure,
6665                                    lc_mod_name,
6666                                    'Number of messages in stack: ' ||
6667                                    Fnd_Msg_Pub.count_msg);
6668                 END IF;
6669                 Csd_Gen_Errmsgs_Pvt.Save_Fnd_Msgs(p_api_version             => 1.0,
6670                                                   p_commit                  => Fnd_Api.G_FALSE,
6671                                                   p_init_msg_list           => Fnd_Api.G_FALSE,
6672                                                   p_validation_level        => 0,
6673                                                   p_module_code             => 'EST',
6674                                                   p_source_entity_id1       => p_repair_line_id,
6675                                                   p_source_entity_type_code => p_MLE_lines_tbl(i)
6676                                                                               .est_line_source_type_code,
6677                                                   p_source_entity_id2       => p_MLE_lines_tbl(i)
6678                                                                               .est_line_source_id1,
6679                                                   x_return_status           => l_return_status,
6680                                                   x_msg_count               => l_msg_count,
6681                                                   x_msg_data                => l_msg_data);
6682                 IF NOT (l_return_status = Fnd_Api.G_RET_STS_SUCCESS)
6683                 THEN
6684                     RAISE Fnd_Api.G_EXC_ERROR;
6685                 END IF;
6686                 IF (Fnd_Log.Level_Procedure >=
6687                    Fnd_Log.G_Current_Runtime_Level)
6688                 THEN
6689                     Fnd_Log.STRING(Fnd_Log.Level_Procedure,
6690                                    lc_mod_name,
6691                                    'Returned from CSD_GEN_ERRMSGS_PVT.Save_Fnd_Msgs');
6692                     Fnd_Log.STRING(Fnd_Log.Level_Procedure,
6693                                    lc_mod_name,
6694                                    'Number of messages in stack: ' ||
6695                                    Fnd_Msg_Pub.count_msg);
6696                 END IF;
6697             END IF;
6698         END LOOP;
6699 
6700         IF (Fnd_Log.Level_Statement >= Fnd_Log.G_Current_Runtime_Level)
6701         THEN
6702             Fnd_Log.STRING(Fnd_Log.Level_Statement,
6703                            lc_mod_name || '.out_parameter',
6704                            'x_warning_flag: ' || x_warning_flag);
6705         END IF;
6706         IF (Fnd_Log.Level_Procedure >= Fnd_Log.G_Current_Runtime_Level)
6707         THEN
6708             Fnd_Log.STRING(Fnd_Log.Level_Procedure,
6709                            lc_mod_name || '.END',
6710                            'Leaving CSD_REPAIR_ESTIMATE_PVT.Convert_To_Est_Lines');
6711         END IF;
6712 
6713         --
6714         -- End API Body
6715         --
6716 
6717         -- Standard check of p_commit.
6718         IF Fnd_Api.To_Boolean(p_commit)
6719         THEN
6720             COMMIT WORK;
6721         END IF;
6722 
6723         -- Standard call to get message count and IF count is  get message info.
6724         Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
6725                                   p_data  => x_msg_data);
6726     EXCEPTION
6727         WHEN Fnd_Api.G_EXC_ERROR THEN
6728             ROLLBACK TO Convert_To_Est_Lines;
6729             x_return_status := Fnd_Api.G_RET_STS_ERROR;
6730 
6731             /*
6732             -- TO DO: Add seeded err message
6733             -- save message in fnd stack
6734             if (Fnd_Log.Level_Statement>= Fnd_Log.G_Current_Runtime_Level) then
6735                 FND_LOG.STRING(Fnd_Log.Level_Statement, lc_mod_name,
6736                                'Adding message err_name to FND_MSG stack');
6737             end if;
6738             FND_MESSAGE.SET_NAME('CSD','err_name');
6739             FND_MESSAGE.SET_TOKEN('toke_name', 'token_value');
6740             FND_MSG_PUB.ADD;
6741             */
6742             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
6743                                       p_data  => x_msg_data);
6744 
6745             -- save message in debug log
6746             IF (Fnd_Log.Level_Exception >= Fnd_Log.G_Current_Runtime_Level)
6747             THEN
6748                 Fnd_Log.STRING(Fnd_Log.Level_Exception,
6749                                lc_mod_name,
6750                                'EXC_ERROR[' || x_msg_data || ']');
6751             END IF;
6752         WHEN Fnd_Api.G_EXC_UNEXPECTED_ERROR THEN
6753             ROLLBACK TO Convert_To_Est_Lines;
6754             x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
6755 
6756             -- save message in fnd stack
6757             IF Fnd_Msg_Pub.Check_Msg_Level(Fnd_Msg_Pub.G_MSG_LVL_UNEXP_ERROR)
6758             THEN
6759                 IF (Fnd_Log.Level_Statement >=
6760                    Fnd_Log.G_Current_Runtime_Level)
6761                 THEN
6762                     Fnd_Log.STRING(Fnd_Log.Level_Statement,
6763                                    lc_mod_name,
6764                                    'Adding message using FND_MSG_PUB.Add_Exc_Msg to FND_MSG stack');
6765                 END IF;
6766                 Fnd_Msg_Pub.Add_Exc_Msg(G_PKG_NAME, lc_api_name);
6767             END IF;
6768             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
6769                                       p_data  => x_msg_data);
6770 
6771             -- save message in debug log
6772             IF (Fnd_Log.Level_Exception >= Fnd_Log.G_Current_Runtime_Level)
6773             THEN
6774                 Fnd_Log.STRING(Fnd_Log.Level_Exception,
6775                                lc_mod_name,
6776                                'EXC_UNEXPECTED_ERROR[' || x_msg_data || ']');
6777             END IF;
6778         WHEN OTHERS THEN
6779             ROLLBACK TO Convert_To_Est_Lines;
6780             x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
6781 
6782             -- save message in fnd stack
6783             IF Fnd_Msg_Pub.Check_Msg_Level(Fnd_Msg_Pub.G_MSG_LVL_UNEXP_ERROR)
6784             THEN
6785                 IF (Fnd_Log.Level_Statement >=
6786                    Fnd_Log.G_Current_Runtime_Level)
6787                 THEN
6788                     Fnd_Log.STRING(Fnd_Log.Level_Statement,
6789                                    lc_mod_name,
6790                                    'Adding message using FND_MSG_PUB.Add_Exc_Msg to FND_MSG stack');
6791                 END IF;
6792                 Fnd_Msg_Pub.Add_Exc_Msg(G_PKG_NAME, lc_api_name);
6793             END IF;
6794             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
6795                                       p_data  => x_msg_data);
6796 
6797             -- save message in debug log
6798             IF (Fnd_Log.Level_Exception >= Fnd_Log.G_Current_Runtime_Level)
6799             THEN
6800                 -- create a seeded message
6801                 Fnd_Log.STRING(Fnd_Log.Level_Exception,
6802                                lc_mod_name,
6803                                'SQL Message[' || SQLERRM || ']');
6804             END IF;
6805     END Convert_To_Est_Lines;
6806     --
6807     -- end swai 11.5.10
6808     --
6809     ----Begin change for 3931317, wrpper aPI forward port
6810     PROCEDURE debug(msg VARCHAR2) IS
6811     BEGIN
6812         IF (G_DEBUG_LEVEL >= 0)
6813         THEN
6814             Csd_Gen_Utility_Pvt.ADD(msg);
6815             --DBMS_OUTPUT.PUT_LINE(msg);
6816         END IF;
6817     END DEBUG;
6818 
6819     /*----------------------------------------------------------------------------*/
6820     /* procedure name: CREATE_ESTIMATE_HEADER                                     */
6821     /* description   : Wrapper procedure used to create estimate header           */
6822     /*   Updates repair order estimate approved flag and creates Depot estimate   */
6823     /*   header record with information like, summary, lead time etc              */
6824     /*----------------------------------------------------------------------------*/
6825 
6826     PROCEDURE CREATE_ESTIMATE_HEADER(p_api_version      IN NUMBER,
6827                                      p_init_msg_list    IN VARCHAR2,
6828                                      p_commit           IN VARCHAR2,
6829                                      p_validation_level IN NUMBER,
6830                                      x_return_status    OUT NOCOPY VARCHAR2,
6831                                      x_msg_count        OUT NOCOPY NUMBER,
6832                                      x_msg_data         OUT NOCOPY VARCHAR2,
6833                                      p_estimate_hdr_rec IN Csd_Repair_Estimate_Pub.ESTIMATE_HDR_REC,
6834                                      x_estimate_hdr_id      OUT NOCOPY NUMBER ) IS
6835         l_incident_id     NUMBER;
6836         l_tmp_count       NUMBER;
6837         l_repln_obj_Ver   NUMBER;
6838         l_repln_quantity  NUMBER;
6839         l_approval_Status VARCHAR2(30);
6840         l_return_status   VARCHAR2(30);
6841         l_msg_count       NUMBER;
6842         l_msg_Data        VARCHAR2(2000);
6843         l_api_name    CONSTANT VARCHAR2(30) := 'CREATE_ESTIMATE_HEADER';
6844         l_api_version CONSTANT NUMBER := 1.0;
6845 
6846         l_estimate_hdr_rec Csd_Repair_Estimate_Pub.ESTIMATE_HDR_REC;
6847         l_est_hdr_pvt_rec     Csd_Repair_Estimate_Pvt.REPAIR_ESTIMATE_REC;
6848     BEGIN
6849 
6850         IF (Fnd_Log.level_procedure >= Fnd_Log.g_current_runtime_level)
6851         THEN
6852             Fnd_Log.STRING(Fnd_Log.level_procedure,
6853                            'csd.plsql.csd_repair_Estimate_pvt.create_estimate_header.begin',
6854                            'Entering create_estimate_header');
6855         END IF;
6856         -- Standard Start of API savepoint
6857         SAVEPOINT CREATE_ESTIMATE_HEADER_PVT;
6858 
6859         -- Standard call to check for call compatibility.
6860         IF NOT Fnd_Api.Compatible_API_Call(l_api_version,
6861                                            p_api_version,
6862                                            l_api_name,
6863                                            G_PKG_NAME)
6864         THEN
6865             RAISE Fnd_Api.G_EXC_UNEXPECTED_ERROR;
6866         END IF;
6867 
6868         -- Initialize message list if p_init_msg_list is set to TRUE.
6869         IF Fnd_Api.to_Boolean(p_init_msg_list)
6870         THEN
6871             Fnd_Msg_Pub.initialize;
6872         END IF;
6873 
6874         -- Initialize API return status to success
6875         x_return_status := Fnd_Api.G_RET_STS_SUCCESS;
6876 
6877         debug('Start of estiamte hdr creation for repair lineid[=' ||
6878               p_estimate_hdr_rec.repair_line_id || ']');
6879 
6880         l_estimate_hdr_rec := p_estimate_hdr_rec;
6881 
6882         csd_estimate_utils_pvt.validate_est_hdr_rec(p_estimate_hdr_rec => l_estimate_hdr_rec,
6883                                                 p_validation_level => p_validation_level);
6884 
6885         csd_estimate_utils_pvt.default_est_hdr_rec(l_estimate_hdr_rec);
6886 
6887         csd_estimate_utils_pvt.validate_defaulted_est_hdr(l_estimate_hdr_rec,
6888                                                       p_validation_level);
6889 
6890         --If the estiamte_status is accepted or rejected then update the
6891         -- repair order status.
6892         IF (l_estimate_hdr_rec.estimate_status = C_EST_STATUS_ACCEPTED)
6893         THEN
6894             l_approval_Status := C_REP_STATUS_APPROVED;
6895         ELSIF (l_estimate_hdr_rec.estimate_status = C_EST_STATUS_REJECTED)
6896         THEN
6897             l_approval_Status := C_REP_STATUS_REJECTED;
6898         END IF;
6899 
6900         IF (l_estimate_hdr_rec.estimate_status = C_EST_STATUS_ACCEPTED OR
6901            l_estimate_hdr_rec.estimate_status = C_EST_STATUS_REJECTED)
6902         THEN
6903 
6904             debug('status is accepted or rejected');
6905 
6906             IF (l_estimate_hdr_rec.estimate_reason_code IS NOT NULL AND NOT
6907                 Csd_Estimate_Utils_Pvt.VALIDATE_REASON(l_estimate_hdr_rec.estimate_reason_code,
6908                                                                                                                 l_estimate_hdr_rec.estimate_status))
6909             THEN
6910                 debug('Invalid estimate_reason_code[' ||
6911                       l_estimate_hdr_rec.estimate_reason_code || ']');
6912                 RAISE Fnd_Api.G_EXC_ERROR;
6913             END IF;
6914 
6915             Csd_Repairs_Grp.UPDATE_APPROVAL_STATUS(p_repair_line_id        => l_estimate_hdr_rec.repair_line_id,
6916                                                    p_new_approval_status   => l_approval_Status,
6917                                                    p_old_approval_status   => NULL,
6918                                                    p_quantity              => l_estimate_hdr_rec.repair_line_quantity,
6919                                                    p_org_contact_id        => NULL,
6920                                                    p_reason                => '',
6921                                                    p_object_version_number => l_estimate_hdr_rec.ro_object_version_number,
6922                                                    x_return_status         => l_return_status,
6923                                                    x_msg_count             => l_msg_count,
6924                                                    x_msg_data              => l_msg_data);
6925             debug('Updated ro status');
6926             IF (l_return_status <> Fnd_Api.G_RET_STS_SUCCESS)
6927             THEN
6928                 debug('Updated ro status failed, x_msg_data[' ||
6929                       l_msg_data || ']');
6930                 RAISE Fnd_Api.G_EXC_ERROR;
6931             END IF;
6932 
6933         ELSE
6934             l_estimate_hdr_rec.estimate_reason_code := NULL;
6935         END IF;
6936         debug('creating estimate header');
6937 
6938         csd_estimate_utils_pvt.COPY_TO_EST_HDR_REC(l_estimate_hdr_rec,
6939                                                l_est_hdr_pvt_rec);
6940         Csd_Repair_Estimate_Pvt.create_repair_estimate(p_api_version      => 1.0,
6941                                                        p_commit           => Fnd_Api.G_FALSE,
6942                                                        p_init_msg_list    => Fnd_Api.G_FALSE,
6943                                                        p_validation_level => Fnd_Api.G_VALID_LEVEL_FULL,
6944                                                        x_estimate_rec     => l_est_hdr_pvt_rec,
6945                                                        x_estimate_id      => x_estimate_hdr_id,
6946                                                        x_return_status    => l_return_status,
6947                                                        x_msg_count        => l_msg_count,
6948                                                        x_msg_data         => l_msg_data);
6949 
6950         debug('after creating the estimate header');
6951 
6952         IF (l_return_status <> Fnd_Api.G_RET_STS_SUCCESS)
6953         THEN
6954             debug('create estiamte header failed, x_msg_data[' ||
6955                   l_msg_data || ']');
6956             RAISE Fnd_Api.G_EXC_ERROR;
6957         END IF;
6958 
6959         -- Standard check of p_commit.
6960         IF Fnd_Api.To_Boolean(p_commit)
6961         THEN
6962             COMMIT WORK;
6963         END IF;
6964 
6965         IF (Fnd_Log.level_procedure >= Fnd_Log.g_current_runtime_level)
6966         THEN
6967             Fnd_Log.STRING(Fnd_Log.level_procedure,
6968                            'csd.plsql.csd_repair_Estimate_pvt.create_estimate_header.end',
6969                            'Leaving create_estimate_header');
6970         END IF;
6971 
6972         -- Standard call to get message count and IF count is  get message info.
6973         Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
6974                                   p_data  => x_msg_data);
6975     EXCEPTION
6976         WHEN Fnd_Api.G_EXC_ERROR THEN
6977             x_return_status := Fnd_Api.G_RET_STS_ERROR;
6978             ROLLBACK TO create_estimate_header_pvt;
6979             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
6980                                       p_data  => x_msg_data);
6981             IF (Fnd_Log.level_error >= Fnd_Log.g_current_runtime_level)
6982             THEN
6983                 Fnd_Log.STRING(Fnd_Log.level_error,
6984                                'csd.plsql.csd_repair_estimate_pvt.create_estimate_header',
6985                                'EXC_ERROR[' || x_msg_data || ']');
6986             END IF;
6987         WHEN Fnd_Api.G_EXC_UNEXPECTED_ERROR THEN
6988             x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
6989             ROLLBACK TO create_estimate_header_pvt;
6990             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
6991                                       p_data  => x_msg_data);
6992             IF (Fnd_Log.level_exception >= Fnd_Log.g_current_runtime_level)
6993             THEN
6994                 Fnd_Log.STRING(Fnd_Log.level_exception,
6995                                'csd.plsql.csd_repair_estimate_pvt.create_estimate_header',
6996                                'EXC_UNEXP_ERROR[' || x_msg_data || ']');
6997             END IF;
6998         WHEN OTHERS THEN
6999             x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
7000             ROLLBACK TO create_estimate_header_pvt;
7001             IF Fnd_Msg_Pub.Check_Msg_Level(Fnd_Msg_Pub.G_MSG_LVL_UNEXP_ERROR)
7002             THEN
7003                 Fnd_Msg_Pub.Add_Exc_Msg(G_PKG_NAME, l_api_name);
7004             END IF;
7005             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
7006                                       p_data  => x_msg_data);
7007             IF (Fnd_Log.level_exception >= Fnd_Log.g_current_runtime_level)
7008             THEN
7009                 Fnd_Log.STRING(Fnd_Log.level_exception,
7010                                'csd.plsql.csd_repair_estimate_pvt.create_estimate_header',
7011                                'SQL MEssage[' || SQLERRM || ']');
7012             END IF;
7013 
7014     END CREATE_ESTIMATE_HEADER;
7015 
7016     /*----------------------------------------------------------------------------*/
7017     /* procedure name: CREATE_ESTIMATE_LINE                                       */
7018     /* description   : private procedure used to create estimate line             */
7019     /*   Creates Depot estimate line record and submits                           */
7020     /*   based on some validations.                                               */
7021     /*----------------------------------------------------------------------------*/
7022 
7023     PROCEDURE CREATE_ESTIMATE_LINE(p_api_version       IN NUMBER,
7024                                    p_init_msg_list     IN VARCHAR2,
7025                                    p_commit            IN VARCHAR2,
7026 							p_validation_level  IN NUMBER,
7027                                    x_return_status     OUT NOCOPY VARCHAR2,
7028                                    x_msg_count         OUT NOCOPY NUMBER,
7029                                    x_msg_data          OUT NOCOPY VARCHAR2,
7030                                    p_estimate_line_rec IN Csd_Repair_Estimate_Pub.ESTIMATE_LINE_REC,
7031                                    x_estimate_line_id  OUT NOCOPY NUMBER) IS
7032 
7033         l_tmp_count      NUMBER;
7034         l_repair_type_id NUMBER;
7035         l_uom_Code       VARCHAR2(3);
7036         l_return_status  VARCHAR2(30);
7037         l_msg_count      NUMBER;
7038         l_msg_Data       VARCHAR2(2000);
7039         l_api_name    CONSTANT VARCHAR2(30) := 'CREATE_ESTIMATE_LINE';
7040         l_api_version CONSTANT NUMBER := 1.0;
7041         l_no_charge_flag   VARCHAR2(10);
7042         l_contract_line_id NUMBER;
7043 
7044         l_est_pvt_line_rec   CSD_REPAIR_ESTIMATE_PVT.REPAIR_ESTIMATE_LINE_REC;
7045         l_estimate_line_rec Csd_Repair_Estimate_Pub.ESTIMATE_LINE_REC;
7046 
7047     BEGIN
7048 
7049         -- Standard Start of API savepoint
7050         SAVEPOINT CREATE_ESTIMATE_LINE_PVT;
7051 
7052         -- Standard call to check for call compatibility.
7053         IF NOT Fnd_Api.Compatible_API_Call(l_api_version,
7054                                            p_api_version,
7055                                            l_api_name,
7056                                            G_PKG_NAME)
7057         THEN
7058             RAISE Fnd_Api.G_EXC_UNEXPECTED_ERROR;
7059         END IF;
7060 
7061         -- Initialize message list if p_init_msg_list is set to TRUE.
7062         IF Fnd_Api.to_Boolean(p_init_msg_list)
7063         THEN
7064             Fnd_Msg_Pub.initialize;
7065         END IF;
7066 
7067         -- Initialize API return status to success
7068         x_return_status := Fnd_Api.G_RET_STS_SUCCESS;
7069 
7070         debug('Start of estimate line creation for estimate id[=' ||
7071               p_estimate_line_rec.repair_estimate_id || ']');
7072 
7073         IF (fnd_log.level_procedure >= fnd_log.g_current_runtime_level)
7074         THEN
7075             fnd_log.STRING(fnd_log.level_procedure,
7076                            'csd.plsql.csd_repair_estimate_pvt.create_estimate_line.begin',
7077                            'Entering create_estimate_line');
7078         END IF;
7079 
7080         l_estimate_line_rec := p_estimate_line_rec;
7081 
7082         csd_estimate_utils_pvt.validate_est_LINE_rec(l_estimate_line_rec,
7083                                                  p_validation_level);
7084 
7085         csd_estimate_utils_pvt.default_est_line_rec(l_estimate_line_rec);
7086 
7087         csd_estimate_utils_pvt.validate_defaulted_est_line(l_estimate_line_rec,
7088                                                        p_validation_level);
7089 
7090         -- yvchen: bug 12545399
7091         -- uncomment this procedure since we need to convert the public record
7092         -- to a private one before calling create_repair_estimate_lines.
7093         csd_estimate_utils_pvt.copy_to_est_pvt_line_rec(l_estimate_line_rec,
7094                                                     l_est_pvt_line_rec);
7095 
7096         debug('Calling create estimate_lines...');
7097 
7098         create_repair_estimate_lines(p_api_version       => 1.0,
7099                                      p_commit            => Fnd_Api.G_FALSE,
7100                                      p_init_msg_list     => Fnd_Api.G_FALSE,
7101                                      p_validation_level  => Fnd_Api.G_VALID_LEVEL_FULL,
7102                                      x_estimate_line_rec => l_est_pvt_line_rec,
7103                                      x_estimate_line_id  => x_estimate_line_id,
7104                                      x_return_status     => l_return_status,
7105                                      x_msg_count         => l_msg_count,
7106                                      x_msg_data          => l_msg_data);
7107 
7108         IF (l_return_status <> Fnd_Api.G_RET_STS_SUCCESS)
7109         THEN
7110             debug('create estimate_lines failed..');
7111             RAISE Fnd_Api.G_EXC_ERROR;
7112         END IF;
7113 
7114         -- Standard check of p_commit.
7115         IF Fnd_Api.To_Boolean(p_commit)
7116         THEN
7117             COMMIT WORK;
7118         END IF;
7119 
7120         -- Standard call to get message count and IF count is  get message info.
7121         Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
7122                                   p_data  => x_msg_data);
7123 
7124         IF (fnd_log.level_procedure >= fnd_log.g_current_runtime_level)
7125         THEN
7126             fnd_log.STRING(fnd_log.level_procedure,
7127                            'csd.plsql.csd_repair_estimate_pvt.create_estimate_line.end',
7128                            'leaving create_estimate_line');
7129         END IF;
7130     EXCEPTION
7131         WHEN Fnd_Api.G_EXC_ERROR THEN
7132             x_return_status := Fnd_Api.G_RET_STS_ERROR;
7133             ROLLBACK TO CREATE_ESTIMATE_LINE_PVT;
7134             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
7135                                       p_data  => x_msg_data);
7136             IF (Fnd_Log.level_error >= Fnd_Log.g_current_runtime_level)
7137             THEN
7138                 Fnd_Log.STRING(Fnd_Log.level_error,
7139                                'csd.plsql.csd_repair_estimate_pvt.create_estimate_line',
7140                                'EXC_ERROR[' || x_msg_data || ']');
7141             END IF;
7142 
7143         WHEN Fnd_Api.G_EXC_UNEXPECTED_ERROR THEN
7144             x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
7145             ROLLBACK TO CREATE_ESTIMATE_LINE_PVT;
7146             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
7147                                       p_data  => x_msg_data);
7148             IF (Fnd_Log.level_exception >= Fnd_Log.g_current_runtime_level)
7149             THEN
7150                 Fnd_Log.STRING(Fnd_Log.level_exception,
7151                                'csd.plsql.csd_repair_estimate_pvt.create_estimate_line',
7152                                'EXC_UNEXP_ERROR[' || x_msg_data || ']');
7153             END IF;
7154         WHEN OTHERS THEN
7155             x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
7156             ROLLBACK TO CREATE_ESTIMATE_LINE_PVT;
7157             IF Fnd_Msg_Pub.Check_Msg_Level(Fnd_Msg_Pub.G_MSG_LVL_UNEXP_ERROR)
7158             THEN
7159                 Fnd_Msg_Pub.Add_Exc_Msg(G_PKG_NAME, l_api_name);
7160             END IF;
7161             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
7162                                       p_data  => x_msg_data);
7163             IF (Fnd_Log.level_exception >= Fnd_Log.g_current_runtime_level)
7164             THEN
7165                 Fnd_Log.STRING(Fnd_Log.level_exception,
7166                                'csd.plsql.csd_repair_estimate_pvt.create_estimate_line',
7167                                'SQL MEssage[' || SQLERRM || ']');
7168             END IF;
7169 
7170     END CREATE_ESTIMATE_LINE;
7171 
7172     /*----------------------------------------------------------------------------*/
7173     /* procedure name: UPDATE_ESTIMATE_HEADER                                     */
7174     /* description   : procedure used to update estimate header                  */
7175     /*   Updates repair order estimate approved flag and creates Depot estimate   */
7176     /*   header record with information like, summary, lead time etc              */
7177     /*  Change History  : Created 24-June-2005 by Vijay                          */
7178     /*----------------------------------------------------------------------------*/
7179     PROCEDURE UPDATE_ESTIMATE_HEADER(p_api_version           IN NUMBER,
7180                                      p_init_msg_list         IN VARCHAR2,
7181                                      p_commit                IN VARCHAR2,
7182 							  p_validation_level      IN NUMBER,
7183                                      x_return_status         OUT NOCOPY VARCHAR2,
7184                                      x_msg_count             OUT NOCOPY NUMBER,
7185                                      x_msg_data              OUT NOCOPY VARCHAR2,
7186                                      p_estimate_hdr_rec      IN Csd_Repair_Estimate_Pub.ESTIMATE_HDR_REC,
7187                                      x_object_version_number OUT NOCOPY NUMBER) IS
7188 
7189         l_return_status VARCHAR2(30);
7190         l_approval_Status VARCHAR2(30);
7191         l_msg_count     NUMBER;
7192         l_msg_Data      VARCHAR2(2000);
7193         l_api_name    CONSTANT VARCHAR2(30) := 'UPDATE_ESTIMATE_HEADER';
7194         l_api_version CONSTANT NUMBER := 1.0;
7195 
7196         l_est_pvt_hdr_Rec CSD_REPAIR_ESTIMATE_PVT.REPAIR_ESTIMATE_REC;
7197         l_estimate_hdr_rec Csd_Repair_Estimate_Pub.ESTIMATE_HDR_REC;
7198 
7199         l_est_status_changed BOOLEAN;
7200 	   l_est_hdr_id         NUMBER;
7201 
7202         CURSOR CUR_EST_HDR(p_estimate_header_id NUMBER) IS
7203             SELECT REPAIR_ESTIMATE_ID,
7204                    REPAIR_LINE_ID,
7205                    ESTIMATE_STATUS,
7206                    ESTIMATE_DATE,
7207                    WORK_SUMMARY,
7208                    PO_NUMBER,
7209                    LEAD_TIME,
7210                    LEAD_TIME_UOM,
7211                    CREATION_DATE,
7212                    CREATED_BY,
7213                    LAST_UPDATED_BY,
7214                    LAST_UPDATE_DATE,
7215                    LAST_UPDATE_LOGIN,
7216                    CONTEXT,
7217                    ATTRIBUTE1,
7218                    ATTRIBUTE2,
7219                    ATTRIBUTE3,
7220                    ATTRIBUTE4,
7221                    ATTRIBUTE5,
7222                    ATTRIBUTE6,
7223                    ATTRIBUTE7,
7224                    ATTRIBUTE8,
7225                    ATTRIBUTE9,
7226                    ATTRIBUTE10,
7227                    ATTRIBUTE11,
7228                    ATTRIBUTE12,
7229                    ATTRIBUTE13,
7230                    ATTRIBUTE14,
7231                    ATTRIBUTE15,
7232                    OBJECT_VERSION_NUMBER,
7233                    ESTIMATE_REASON_CODE,
7234                    NOT_TO_EXCEED -- swai: bug 9462789 allow update of NTE value
7235               FROM CSD_REPAIR_ESTIMATE
7236              WHERE REPAIR_ESTIMATE_ID = p_estimate_header_id;
7237 
7238     BEGIN
7239         -- Standard Start of API savepoint
7240         SAVEPOINT UPDATE_ESTIMATE_HEADER_PVT;
7241 
7242         -- Standard call to check for call compatibility.
7243         IF NOT Fnd_Api.Compatible_API_Call(l_api_version,
7244                                            p_api_version,
7245                                            l_api_name,
7246                                            G_PKG_NAME)
7247         THEN
7248             RAISE Fnd_Api.G_EXC_UNEXPECTED_ERROR;
7249         END IF;
7250 
7251         -- Initialize message list if p_init_msg_list is set to TRUE.
7252         IF Fnd_Api.to_Boolean(p_init_msg_list)
7253         THEN
7254             Fnd_Msg_Pub.initialize;
7255         END IF;
7256 
7257         -- Initialize API return status to success
7258         x_return_status := Fnd_Api.G_RET_STS_SUCCESS;
7259 
7260         debug('Start of update estimate header for estimate id[=' ||
7261               p_estimate_hdr_rec.repair_estimate_id || ']');
7262 
7263         IF (fnd_log.level_procedure >= fnd_log.g_current_runtime_level)
7264         THEN
7265             fnd_log.STRING(fnd_log.level_procedure,
7266                            'csd.plsql.csd_repair_estimate_pvt.update_estimate_header.begin',
7267                            'Entering update_estimate_header');
7268         END IF;
7269 
7270 
7271         l_estimate_hdr_rec := p_estimate_hdr_rec;
7272 
7273         OPEN CUR_EST_HDR(p_estimate_hdr_rec.repair_estimate_id);
7274         FETCH CUR_EST_HDR
7275             INTO l_est_pvt_hdr_Rec.repair_estimate_id,
7276 		  l_est_pvt_hdr_Rec.repair_line_id,
7277 		  l_est_pvt_hdr_Rec.estimate_status,
7278 		  l_est_pvt_hdr_Rec.estimate_date,
7279 		  l_est_pvt_hdr_Rec.work_summary,
7280 		  l_est_pvt_hdr_Rec.po_number,
7281 		  l_est_pvt_hdr_Rec.lead_time,
7282 		  l_est_pvt_hdr_Rec.lead_time_uom,
7283 		  l_est_pvt_hdr_Rec.creation_date,
7284 		  l_est_pvt_hdr_Rec.created_by,
7285 		  l_est_pvt_hdr_Rec.last_updated_by,
7286 		  l_est_pvt_hdr_Rec.last_update_date,
7287 		  l_est_pvt_hdr_Rec.last_update_login,
7288 		  l_est_pvt_hdr_Rec.context,
7289 		  l_est_pvt_hdr_Rec.attribute1,
7290 		  l_est_pvt_hdr_Rec.attribute2,
7291 		  l_est_pvt_hdr_Rec.attribute3,
7292 		  l_est_pvt_hdr_Rec.attribute4,
7293 		  l_est_pvt_hdr_Rec.attribute5,
7294 		  l_est_pvt_hdr_Rec.attribute6,
7295 		  l_est_pvt_hdr_Rec.attribute7,
7296 		  l_est_pvt_hdr_Rec.attribute8,
7297 		  l_est_pvt_hdr_Rec.attribute9,
7298 		  l_est_pvt_hdr_Rec.attribute10,
7299 		  l_est_pvt_hdr_Rec.attribute11,
7300 		  l_est_pvt_hdr_Rec.attribute12,
7301 		  l_est_pvt_hdr_Rec.attribute13,
7302 		  l_est_pvt_hdr_Rec.attribute14,
7303 		  l_est_pvt_hdr_Rec.attribute15,
7304 		  l_est_pvt_hdr_Rec.object_version_number,
7305 		  l_est_pvt_hdr_Rec.estimate_reason_code,
7306           l_est_pvt_hdr_Rec.not_to_exceed;  -- swai: bug 9462789
7307 
7308         IF (CUR_EST_HDR%NOTFOUND)
7309         THEN
7310             Fnd_Message.SET_NAME('CSD', 'CSD_INV_ESTIMATE_HEADER');
7311             Fnd_Msg_Pub.ADD;
7312             RAISE Fnd_Api.G_EXC_ERROR;
7313         END IF;
7314 
7315         IF (l_est_pvt_hdr_Rec.repair_line_id <>
7316            p_Estimate_hdr_Rec.repair_line_id)
7317         THEN
7318             Fnd_Message.SET_NAME('CSD', 'CSD_INV_ESTIMATE_INPUT');
7319             Fnd_Msg_Pub.ADD;
7320             RAISE Fnd_Api.G_EXC_ERROR;
7321         END IF;
7322     CLOSE CUR_EST_HDR;
7323 
7324         l_est_status_changed := FALSE;
7325         IF (p_Estimate_hdr_Rec.estimate_status IS NOT NULL AND
7326            p_Estimate_hdr_Rec.estimate_status <>
7327            l_est_pvt_hdr_Rec.estimate_status)
7328         THEN
7329             l_est_status_changed := TRUE;
7330         END IF;
7331 
7332 --bug#9733109
7333         --If the estiamte_status is accepted or rejected then update the
7334         -- repair order status.
7335         IF (l_estimate_hdr_rec.estimate_status = C_EST_STATUS_ACCEPTED)
7336         THEN
7337             l_approval_Status := C_REP_STATUS_APPROVED;
7338         ELSIF (l_estimate_hdr_rec.estimate_status = C_EST_STATUS_REJECTED)
7339         THEN
7340             l_approval_Status := C_REP_STATUS_REJECTED;
7341         END IF;
7342 --bug#9733109
7343 
7344 --bug#9733109
7345         IF (l_est_status_changed AND
7346            (p_estimate_hdr_rec.estimate_status = C_EST_STATUS_ACCEPTED OR
7347            p_estimate_hdr_rec.estimate_status = C_EST_STATUS_REJECTED))
7348         THEN
7349 --bug#9733109
7350             debug('status is accepted or rejected');
7351 
7352             IF (l_est_pvt_hdr_rec.estimate_reason_code IS NOT NULL AND NOT
7353                 csd_estimate_utils_pvt.VALIDATE_REASON(l_estimate_hdr_rec.estimate_reason_code,
7354                                                                                                                 l_estimate_hdr_rec.estimate_status))
7355             THEN
7356                 debug('Invalid estimate_reason_code[' ||
7357                       l_estimate_hdr_rec.estimate_reason_code || ']');
7358                 RAISE Fnd_Api.G_EXC_ERROR;
7359             END IF;
7360 
7361             Csd_Repairs_Grp.UPDATE_APPROVAL_STATUS(p_repair_line_id        => l_estimate_hdr_rec.repair_line_id,
7362                                                    p_new_approval_status   => l_approval_Status,
7363                                                    p_old_approval_status   => NULL,
7364                                                    p_quantity              => l_estimate_hdr_rec.repair_line_quantity,
7365                                                    p_org_contact_id        => NULL,
7366                                                    p_reason                => '',
7367                                                    p_object_version_number => l_estimate_hdr_rec.ro_object_version_number,
7368                                                    x_return_status         => l_return_status,
7369                                                    x_msg_count             => l_msg_count,
7370                                                    x_msg_data              => l_msg_data);
7371             debug('Updated ro status');
7372             IF (l_return_status <> Fnd_Api.G_RET_STS_SUCCESS)
7373             THEN
7374                 debug('Updated ro status failed, x_msg_data[' ||
7375                       l_msg_data || ']');
7376                 RAISE Fnd_Api.G_EXC_ERROR;
7377             END IF;
7378 
7379         ELSE
7380             l_estimate_hdr_rec.estimate_reason_code := NULL;
7381         END IF;
7382 
7383         csd_estimate_utils_pvt.COPY_TO_EST_HDR_REC_UPD(l_estimate_hdr_rec,
7384                                                    l_est_pvt_hdr_rec);
7385 
7386         Csd_Repair_Estimate_Pvt.update_repair_estimate(p_api_version      => 1.0,
7387                                                        p_commit           => Fnd_Api.G_FALSE,
7388                                                        p_init_msg_list    => Fnd_Api.G_FALSE,
7389                                                        p_validation_level => Fnd_Api.G_VALID_LEVEL_FULL,
7390                                                        x_estimate_rec     => l_est_pvt_hdr_rec,
7391                                                        x_return_status    => l_return_status,
7392                                                        x_msg_count        => l_msg_count,
7393                                                        x_msg_data         => l_msg_data);
7394 
7395         debug('after creating the estimate header');
7396         x_object_version_number := l_estimate_hdr_rec.object_version_number;
7397 
7398         IF (l_return_status <> Fnd_Api.G_RET_STS_SUCCESS)
7399         THEN
7400             debug('update estiamte header failed, x_msg_data[' ||
7401                   l_msg_data || ']');
7402             RAISE Fnd_Api.G_EXC_ERROR;
7403         END IF;
7404 
7405         -- Standard check of p_commit.
7406         IF Fnd_Api.To_Boolean(p_commit)
7407         THEN
7408             COMMIT WORK;
7409         END IF;
7410 
7411         -- Standard call to get message count and IF count is  get message info.
7412         Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
7413                                   p_data  => x_msg_data);
7414 
7415         IF (fnd_log.level_procedure >= fnd_log.g_current_runtime_level)
7416         THEN
7417             fnd_log.STRING(fnd_log.level_procedure,
7418                            'csd.plsql.csd_repair_estimate_pvt.update_estimate_header.end',
7419                            'leaving update_estimate_header');
7420         END IF;
7421     EXCEPTION
7422         WHEN Fnd_Api.G_EXC_ERROR THEN
7423             x_return_status := Fnd_Api.G_RET_STS_ERROR;
7424             ROLLBACK TO UPDATE_ESTIMATE_HEADER_PVT;
7425             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
7426                                       p_data  => x_msg_data);
7427             IF (Fnd_Log.level_error >= Fnd_Log.g_current_runtime_level)
7428             THEN
7429                 Fnd_Log.STRING(Fnd_Log.level_error,
7430                                'csd.plsql.csd_repair_estimate_pvt.update_estimate_header',
7431                                'EXC_ERROR[' || x_msg_data || ']');
7432             END IF;
7433 
7434         WHEN Fnd_Api.G_EXC_UNEXPECTED_ERROR THEN
7435             x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
7436             ROLLBACK TO UPDATE_ESTIMATE_HEADER_PVT;
7437             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
7438                                       p_data  => x_msg_data);
7439             IF (Fnd_Log.level_exception >= Fnd_Log.g_current_runtime_level)
7440             THEN
7441                 Fnd_Log.STRING(Fnd_Log.level_exception,
7442                                'csd.plsql.csd_repair_estimate_pvt.update_estimate_header',
7443                                'EXC_UNEXP_ERROR[' || x_msg_data || ']');
7444             END IF;
7445         WHEN OTHERS THEN
7446             x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
7447             Rollback TO UPDATE_ESTIMATE_HEADER_PVT;
7448             IF Fnd_Msg_Pub.Check_Msg_Level(Fnd_Msg_Pub.G_MSG_LVL_UNEXP_ERROR)
7449             THEN
7450                 Fnd_Msg_Pub.Add_Exc_Msg(G_PKG_NAME, l_api_name);
7451             END IF;
7452             Fnd_Msg_Pub.Count_And_Get(p_count => x_msg_count,
7453                                       p_data  => x_msg_data);
7454             IF (Fnd_Log.level_exception >= Fnd_Log.g_current_runtime_level)
7455             THEN
7456                 Fnd_Log.STRING(Fnd_Log.level_exception,
7457                                'csd.plsql.csd_repair_estimate_pvt.update_estimate_header',
7458                                'SQL MEssage[' || SQLERRM || ']');
7459             END IF;
7460 
7461     END UPDATE_ESTIMATE_HEADER;
7462 
7463     /*----------------------------------------------------------------------------*/
7464     /* procedure name: UPDATE_ESTIMATE_LINE                                       */
7465     /* description   :  procedure used to update  estimate line                  */
7466     /*   Updates Depot estimate line record and submits                           */
7467     /*   based on some validations.                                               */
7468     /*  Change History  : Created 24-June-2005 by Vijay                           */
7469     /*----------------------------------------------------------------------------*/
7470 
7471     PROCEDURE UPDATE_ESTIMATE_LINE(p_api_version           IN NUMBER,
7472                                    p_init_msg_list         IN VARCHAR2,
7473                                    p_commit                IN VARCHAR2,
7474                                    p_validation_level      IN NUMBER,
7475                                    x_return_status         OUT NOCOPY VARCHAR2,
7476                                    x_msg_count             OUT NOCOPY NUMBER,
7477                                    x_msg_data              OUT NOCOPY VARCHAR2,
7478                                    p_estimate_line_rec     IN Csd_Repair_Estimate_Pub.ESTIMATE_LINE_REC,
7479                                    x_object_version_number OUT NOCOPY NUMBER) IS
7480     BEGIN
7481         NULL;
7482     END UPDATE_ESTIMATE_LINE;
7483 
7484 
7485     -- swai: Bug 14804275 FP of bug 14752421 new procedure for creating estimates from wip
7486     /*--------------------------------------------------------------------*/
7487     /* procedure name: Import_Estimates_From_Wip                          */
7488     /* description : Procedure is used to import WIP debrief lines into   */
7489     /*               repair estimate lines. We consider material and      */
7490     /*               resource transactions to create charge/repair estimate */
7491     /*               lines.                                               */
7492     /*                                                                    */
7493     /* Called from : CSD_HV_WIP_JOB_PVT.generate_estimate_from_wip        */
7494     /*                                                                    */
7495     /* x_warning_flag : This flag communicates to the calling procedure   */
7496     /*                  whether there are any messages logged that can be */
7497     /*                  displayed to the user. If the value is G_TRUE     */
7498     /*                  then it indicates that one or more message have   */
7499     /*                  been logged.                                      */
7500     /*                                                                    */
7501     /*--------------------------------------------------------------------*/
7502 
7503     PROCEDURE Import_Estimates_From_Wip( p_api_version IN NUMBER,
7504                                          p_commit IN VARCHAR2,
7505                                          p_init_msg_list IN VARCHAR2,
7506                                          p_validation_level IN NUMBER,
7507                                          x_return_status OUT NOCOPY VARCHAR2,
7508                                          x_msg_count OUT NOCOPY NUMBER,
7509                                          x_msg_data OUT NOCOPY VARCHAR2,
7510                                          p_repair_line_id IN NUMBER,
7511                                          p_repair_estimate_id IN NUMBER,
7512                                          p_repair_type_id IN NUMBER,
7513                                          p_business_process_id IN NUMBER,
7514                                          p_currency_code IN VARCHAR2,
7515                                          p_incident_id IN NUMBER,
7516                                          p_organization_id IN NUMBER,
7517                                          x_warning_flag OUT NOCOPY VARCHAR2 ) IS
7518 
7519     -- Constants --
7520     lc_debug_level           CONSTANT NUMBER := FND_LOG.G_CURRENT_RUNTIME_LEVEL;
7521     lc_stat_level            CONSTANT NUMBER := FND_LOG.LEVEL_STATEMENT;
7522     lc_proc_level            CONSTANT NUMBER := FND_LOG.LEVEL_PROCEDURE;
7523     lc_event_level           CONSTANT NUMBER := FND_LOG.LEVEL_EVENT;
7524     lc_excep_level           CONSTANT NUMBER := FND_LOG.LEVEL_EXCEPTION;
7525     lc_error_level           CONSTANT NUMBER := FND_LOG.LEVEL_ERROR;
7526     lc_unexp_level           CONSTANT NUMBER := FND_LOG.LEVEL_UNEXPECTED;
7527     lc_mod_name              CONSTANT VARCHAR2(100)  := 'csd.plsql.csd_repair_estimate_pvt.import_estimates_from_wip';
7528     lc_api_name              CONSTANT VARCHAR2(30)   := 'IMPORT_ESTIMATES_FROM_WIP';
7529     lc_api_version           CONSTANT NUMBER         := 1.0;
7530 
7531     -- Although both the constants have the same value, they have
7532     -- been deliberatly defined as different. We use diff constants as
7533     -- they really represent different entities and hence any future
7534     -- maintenenace will be easier.
7535     G_ESTIMATE_MSG_ENTITY_WIP     CONSTANT VARCHAR2(30)  := 'WIP';
7536     G_ESTIMATE_SOURCE_CODE_WIP    CONSTANT VARCHAR2(30)  := 'WIP';
7537 
7538     -- For charge line type 'estimate'.
7539     G_CHARGE_LINE_TYPE_ESTIMATE   CONSTANT VARCHAR2(30)  := 'ESTIMATE';
7540 
7541     -- Variables --
7542     l_repair_estimate_id                NUMBER;
7543     l_repair_estimate_status            VARCHAR2(30);
7544     l_repair_estimate_status_name       VARCHAR2(80);
7545     l_repair_estimate_rec               CSD_REPAIR_ESTIMATE_PVT.REPAIR_ESTIMATE_REC;
7546 
7547     -- Stores default contract and pricelist info.
7548     l_default_contract_line_id           NUMBER        := NULL;
7549     l_default_price_list_hdr_id          NUMBER        := NULL;
7550 
7551     l_default_po_number csd_repairs.default_po_num%type; -- po number from RO should be defaulted
7552 
7553     -- Keeps the count of WIP jobs
7554     l_wip_count NUMBER := 0;
7555 
7556     -- Keeps the count of total estimate lines creates
7557     l_total_estimates_lines NUMBER := 0;
7558 
7559     -- Cursors --
7560 
7561     -- cursor to get the current active repair estimate header id.
7562     -- currently, there is either no header or 1 header.  This cursor is a placeholder,
7563     -- in anticipation of estimate versioning, in which a repair order
7564     -- can have more than one estimate header.
7565     CURSOR c_cur_repair_estimate_info IS
7566     SELECT est.repair_estimate_id,
7567            est.estimate_status,
7568            lkp.meaning
7569       FROM CSD_REPAIR_ESTIMATE est, fnd_lookups lkp
7570      WHERE est.repair_line_id = p_repair_line_id
7571        and lkp.lookup_code = est.estimate_status
7572        and lkp.lookup_type = 'CSD_ESTIMATE_STATUS'
7573        and rownum = 1;
7574 
7575     -- cursor to get the estimate lines for a given estimate header
7576     CURSOR c_estimate_lines (p_estimate_header_id NUMBER)IS
7577     SELECT lin.repair_estimate_line_id
7578       FROM CSD_REPAIR_ESTIMATE_LINES lin
7579      WHERE lin.repair_estimate_id = p_estimate_header_id;
7580 
7581 
7582     -- It forms the outer most loop for the processing.
7583     -- Ensures that we process one WIP job at a time.
7584     -- A note on the view CSD_ESTIMATES_FROM_WIP_V.
7585     --   1. It gets all the WIP jobs for an RO that have the
7586     --      statuses - 1 Unreleased, 3 Released
7587     --   2. The view makes sure that any WIP jobs that is/are shared
7588     --      with other repair orders are not fetched.
7589     CURSOR c_eligible_WIP_Jobs IS
7590       SELECT XREF.wip_entity_id, XREF.organization_id WIP_Organization_Id,
7591              XREF.inventory_item_id, XREF.JOB_NAME
7592         FROM CSD_ESTIMATES_FROM_WIP_V XREF
7593        WHERE XREF.repair_line_id = p_repair_line_id;
7594 
7595 
7596     -- Gets all the WIP jobs for the repair order that are
7597     -- shared with other repair orders.
7598     -- WIP jobs that are 'shared' across repair orders are
7599     -- ineligble for importing of WIP debrief lines.
7600     CURSOR c_ineligible_WIP_Jobs IS
7601       SELECT XREF.wip_entity_id,
7602              WENT.WIP_ENTITY_NAME JOB_NAME
7603         FROM CSD_REPAIR_JOB_XREF XREF,
7604              WIP_ENTITIES WENT
7605        WHERE XREF.repair_line_id = p_repair_line_id
7606          AND WENT.wip_entity_id = XREF.wip_entity_id
7607          AND EXISTS
7608              (SELECT 'x'
7609                 FROM CSD_REPAIR_JOB_XREF RJOB
7610                WHERE RJOB.wip_entity_id = XREF.wip_entity_id
7611               HAVING COUNT(*) > 1
7612              );
7613 
7614     -- Gets the estimate lines from material transactions.
7615     -- Points to note:
7616     -- 1. We consider only 'Component Issue' and 'Component Return'.
7617     -- 2. 'Component Issue' is -ve quantity and 'Component Return' is +ve
7618     --    from inventory point of view. But it's other way around from
7619     --    Depot Repair POV.
7620     -- 3. The SUM function is intended to cancel out any +ve and -ve qty.
7621     -- 4. If an item, that is same as the Assembly item, is issued/returned
7622     --    to the job then we do not consider it to be a material line.
7623     -- 5. We use primary quantity/UOM and NOT transactional quantity/UOM.
7624     -- 6. The view CSD_ESTIMATES_FROM_WIP_V considers only the WIP jobs that have
7625     --    not been imported.
7626 
7627     -- this query gets the net qty for materials
7628     CURSOR c_est_lines_from_materials( l_wip_entity_id NUMBER,
7629                                           l_inventory_item_id NUMBER) IS
7630         SELECT mmt.inventory_item_id INVENTORY_ITEM_ID,
7631                MSI.primary_uom_code UOM,
7632                SUM( DECODE( MMT.transaction_type_id
7633                          , G_MTL_TXN_TYPE_COMP_ISSUE, ABS( mmt.primary_quantity )
7634                          , G_MTL_TXN_TYPE_COMP_RETURN,
7635                          ( -1 * ABS( mmt.primary_quantity )))) QUANTITY,
7636                MSI.concatenated_segments ITEM_NAME,
7637                MSI.comms_nl_trackable_flag IB_TRACKABLE_FLAG,
7638                TXBT.txn_billing_type_id, TXBT.transaction_type_id,
7639                G_ESTIMATE_SOURCE_CODE_WIP ESTIMATE_SOURCE_CODE,
7640                l_wip_entity_id ESTIMATE_SOURCE_ID,
7641                mmt.operation_seq_num OPERATION_SEQ_NUM
7642           FROM MTL_MATERIAL_TRANSACTIONS MMT, MTL_SYSTEM_ITEMS_KFV MSI,
7643                CSD_REPAIR_TYPES_SAR RTYP, CS_TXN_BILLING_TYPES TXBT
7644          WHERE MMT.transaction_source_id = l_wip_entity_id
7645            AND MMT.transaction_source_type_id = G_MTL_TXN_SOURCE_TYPE_WIP
7646            AND MMT.transaction_type_id IN( G_MTL_TXN_TYPE_COMP_ISSUE,
7647                                            G_MTL_TXN_TYPE_COMP_RETURN )
7648            AND MMT.inventory_item_id <> l_inventory_item_id
7649            AND MSI.inventory_item_id = MMT.inventory_item_id
7650            AND MSI.organization_id = p_organization_id
7651            AND MSI.material_billable_flag IS NOT NULL
7652            AND TXBT.billing_type = MSI.material_billable_flag
7653            AND RTYP.repair_type_id = p_repair_type_id
7654            AND TXBT.txn_billing_type_id = RTYP.txn_billing_type_id
7655            AND TRUNC(NVL(TXBT.start_date_active, SYSDATE)) <= TRUNC(SYSDATE)
7656            AND TRUNC(NVL(TXBT.end_date_active, SYSDATE)) >= TRUNC(SYSDATE)
7657       GROUP BY MMT.inventory_item_id, MSI.primary_uom_code,
7658                MSI.concatenated_segments, MSI.comms_nl_trackable_flag,
7659                TXBT.txn_billing_type_id, TXBT.transaction_type_id,
7660                l_wip_entity_id, mmt.operation_seq_num
7661       ORDER BY MMT.inventory_item_id;
7662 
7663     -- this query gets only the positive issued qty
7664     -- Note: positive issue qty are negative values in mtl_material_transactions table
7665     -- We will not count G_MTL_TXN_TYPE_COMP_RETURN transactions
7666     -- or negatively issued G_MTL_TXN_TYPE_COMP_ISSUE transactions
7667     CURSOR c_est_lines_from_materials1( l_wip_entity_id NUMBER,
7668                                           l_inventory_item_id NUMBER) IS
7669         SELECT mmt.inventory_item_id INVENTORY_ITEM_ID,
7670                MSI.primary_uom_code UOM,
7671                SUM(decode(sign(mmt.primary_quantity), -1, abs(mmt.primary_quantity), 0 )) QUANTITY,
7672                MSI.concatenated_segments ITEM_NAME,
7673                MSI.comms_nl_trackable_flag IB_TRACKABLE_FLAG,
7674                TXBT.txn_billing_type_id, TXBT.transaction_type_id,
7675                G_ESTIMATE_SOURCE_CODE_WIP ESTIMATE_SOURCE_CODE,
7676                l_wip_entity_id ESTIMATE_SOURCE_ID,
7677                mmt.operation_seq_num OPERATION_SEQ_NUM
7678           FROM MTL_MATERIAL_TRANSACTIONS MMT, MTL_SYSTEM_ITEMS_KFV MSI,
7679                CSD_REPAIR_TYPES_SAR RTYP, CS_TXN_BILLING_TYPES TXBT
7680          WHERE MMT.transaction_source_id = l_wip_entity_id
7681            AND MMT.transaction_source_type_id = G_MTL_TXN_SOURCE_TYPE_WIP
7682            AND MMT.inventory_item_id <> l_inventory_item_id
7683            AND MSI.inventory_item_id = MMT.inventory_item_id
7684            AND MSI.organization_id = p_organization_id
7685            AND MSI.material_billable_flag IS NOT NULL
7686            AND TXBT.billing_type = MSI.material_billable_flag
7687            AND RTYP.repair_type_id = p_repair_type_id
7688            AND TXBT.txn_billing_type_id = RTYP.txn_billing_type_id
7689            AND TRUNC(NVL(TXBT.start_date_active, SYSDATE)) <= TRUNC(SYSDATE)
7690            AND TRUNC(NVL(TXBT.end_date_active, SYSDATE)) >= TRUNC(SYSDATE)
7691            AND MMT.transaction_type_id = G_MTL_TXN_TYPE_COMP_ISSUE
7692       GROUP BY MMT.inventory_item_id, MSI.primary_uom_code,
7693                MSI.concatenated_segments, MSI.comms_nl_trackable_flag,
7694                TXBT.txn_billing_type_id, TXBT.transaction_type_id,
7695                l_wip_entity_id, mmt.operation_seq_num
7696       ORDER BY MMT.inventory_item_id;
7697 
7698       -- Gets the untransacted materials from material requirements
7699       CURSOR c_est_lines_from_material_reqs( l_wip_entity_id NUMBER,
7700                                           l_inventory_item_id NUMBER) IS
7701         SELECT wro.inventory_item_id INVENTORY_ITEM_ID,
7702                MSI.primary_uom_code UOM,
7703                (wro.REQUIRED_QUANTITY - wro.QUANTITY_ISSUED) QUANTITY,
7704                MSI.concatenated_segments ITEM_NAME,
7705                MSI.comms_nl_trackable_flag IB_TRACKABLE_FLAG,
7706                TXBT.txn_billing_type_id, TXBT.transaction_type_id,
7707                G_ESTIMATE_SOURCE_CODE_WIP ESTIMATE_SOURCE_CODE,
7708                l_wip_entity_id ESTIMATE_SOURCE_ID,
7709                wro.operation_seq_num OPERATION_SEQ_NUM
7710           FROM WIP_REQUIREMENT_OPERATIONS wro, MTL_SYSTEM_ITEMS_KFV MSI,
7711                CSD_REPAIR_TYPES_SAR RTYP, CS_TXN_BILLING_TYPES TXBT
7712          WHERE wro.wip_entity_id = l_wip_entity_id
7713            AND wro.inventory_item_id <> l_inventory_item_id
7714            AND MSI.inventory_item_id = wro.inventory_item_id
7715            AND MSI.organization_id = p_organization_id
7716            AND MSI.material_billable_flag IS NOT NULL
7717            AND TXBT.billing_type = MSI.material_billable_flag
7718            AND RTYP.repair_type_id = p_repair_type_id
7719            AND TXBT.txn_billing_type_id = RTYP.txn_billing_type_id
7720            AND TRUNC(NVL(TXBT.start_date_active, SYSDATE)) <= TRUNC(SYSDATE)
7721            AND TRUNC(NVL(TXBT.end_date_active, SYSDATE)) >= TRUNC(SYSDATE)
7722       ORDER BY wro.inventory_item_id;
7723 
7724     -- Gets the estimate lines from resource transactions.
7725     -- Points to note are -
7726     -- 1. Resource transactions are represented in WIP transactions table.
7727     -- 2. We assume the 'billable_item_id' to the inventory item for
7728     --    which we create estimate lines.
7729     -- 3. The view CSD_ESTIMATES_FROM_WIP_V considers only the WIP jobs that have
7730     --    not been imported.
7731     -- 4. We use primary quantity/UOM and NOT transactional quantity/UOM.
7732     -- 5. For resource transactions, we assume, that the txn primary UOM
7733     --    and the billable item is a valid combination for getting the
7734     --    selling price for a pricelist.
7735     CURSOR c_est_lines_from_resources( l_wip_entity_id NUMBER) IS
7736         SELECT RES.billable_item_id INVENTORY_ITEM_ID,
7737                WTXN.primary_uom UOM,
7738                SUM( NVL( WTXN.primary_quantity, 0 )) QUANTITY,
7739                MSI.concatenated_segments ITEM_NAME,
7740                MSI.comms_nl_trackable_flag IB_TRACKABLE_FLAG,
7741                TXBT.txn_billing_type_id, TXBT.transaction_type_id,
7742                G_ESTIMATE_SOURCE_CODE_WIP ESTIMATE_SOURCE_CODE,
7743                l_wip_entity_id ESTIMATE_SOURCE_ID,
7744                wtxn.operation_seq_num OPERATION_SEQ_NUM,
7745                RES.resource_id RESOURCE_ID -- Added for ER 3607765, vkjain.
7746           FROM WIP_TRANSACTIONS WTXN, BOM_RESOURCES RES,
7747                MTL_SYSTEM_ITEMS_KFV MSI, CSD_REPAIR_TYPES_SAR RTYP,
7748                CS_TXN_BILLING_TYPES TXBT
7749          WHERE WTXN.wip_entity_id = l_wip_entity_id
7750            AND WTXN.transaction_type IN( 1, 2, 3 )
7751            AND RES.resource_id = WTXN.resource_id
7752            AND MSI.inventory_item_id = RES.billable_item_id
7753            AND MSI.organization_id = p_organization_id
7754            AND MSI.material_billable_flag IS NOT NULL
7755            AND TXBT.billing_type = MSI.material_billable_flag
7756            AND RTYP.repair_type_id = p_repair_type_id
7757            AND TXBT.txn_billing_type_id = RTYP.txn_billing_type_id
7758            AND TRUNC(NVL(TXBT.start_date_active, SYSDATE)) <= TRUNC(SYSDATE)
7759            AND TRUNC(NVL(TXBT.end_date_active, SYSDATE)) >= TRUNC(SYSDATE)
7760       GROUP BY RES.billable_item_id, WTXN.primary_uom,
7761                MSI.concatenated_segments, MSI.comms_nl_trackable_flag,
7762                TXBT.txn_billing_type_id, TXBT.transaction_type_id,
7763                l_wip_entity_id,  wtxn.operation_seq_num , RES.resource_id
7764       ORDER BY RES.billable_item_id;
7765 
7766     -- Gets the untransacted resources from resource requirements
7767     CURSOR c_est_lines_from_resource_reqs( l_wip_entity_id NUMBER) IS
7768         SELECT RES.billable_item_id INVENTORY_ITEM_ID,
7769                WOR.UOM_CODE UOM,
7770                nvl( WTD.TRANSACTION_QUANTITY,
7771                decode ( sign (
7772                  ( WOR.USAGE_RATE_OR_AMOUNT * DECODE(WOR.BASIS_TYPE,1, WO.SCHEDULED_QUANTITY,1) )
7773                     -  WOR.APPLIED_RESOURCE_UNITS  -
7774                     - csd_hv_wip_job_pvt.get_pending_quantity ( wor.wip_entity_id,
7775                         wor.operation_seq_num, wor.resource_seq_num, WOR.UOM_CODE )
7776                  ) ,  1 ,
7777                  round (
7778                  (( WOR.USAGE_RATE_OR_AMOUNT * DECODE(WOR.BASIS_TYPE,1, WO.SCHEDULED_QUANTITY,1) )
7779                    -  WOR.APPLIED_RESOURCE_UNITS
7780                    - csd_hv_wip_job_pvt.get_pending_quantity ( wor.wip_entity_id,
7781                            wor.operation_seq_num, wor.resource_seq_num, WOR.UOM_CODE )
7782                   ), 6 ),
7783                   0 )
7784                   )  QUANTITY,
7785                MSI.concatenated_segments ITEM_NAME,
7786                MSI.comms_nl_trackable_flag IB_TRACKABLE_FLAG,
7787                TXBT.txn_billing_type_id, TXBT.transaction_type_id,
7788                G_ESTIMATE_SOURCE_CODE_WIP ESTIMATE_SOURCE_CODE,
7789                l_wip_entity_id ESTIMATE_SOURCE_ID,
7790                WOR.operation_seq_num OPERATION_SEQ_NUM,
7791                RES.resource_id RESOURCE_ID
7792           FROM WIP_OPERATION_RESOURCES WOR, wip_operations wo,
7793                CSD_WIP_TRANSACTION_DETAILS  WTD, BOM_RESOURCES RES,
7794                MTL_SYSTEM_ITEMS_KFV MSI, CSD_REPAIR_TYPES_SAR RTYP,
7795                CS_TXN_BILLING_TYPES TXBT
7796          WHERE WOR.wip_entity_id = l_wip_entity_id
7797            AND RES.resource_id = WOR.resource_id
7798            AND MSI.inventory_item_id = RES.billable_item_id
7799            AND MSI.organization_id = p_organization_id
7800            AND MSI.material_billable_flag IS NOT NULL
7801            AND TXBT.billing_type = MSI.material_billable_flag
7802            AND RTYP.repair_type_id = p_repair_type_id
7803            AND TXBT.txn_billing_type_id = RTYP.txn_billing_type_id
7804            AND TRUNC(NVL(TXBT.start_date_active, SYSDATE)) <= TRUNC(SYSDATE)
7805            AND TRUNC(NVL(TXBT.end_date_active, SYSDATE)) >= TRUNC(SYSDATE)
7806            AND WOR.WIP_ENTITY_ID = WO.WIP_ENTITY_ID
7807            AND WOR.OPERATION_SEQ_NUM = WO.OPERATION_SEQ_NUM
7808            AND WOR.ORGANIZATION_ID = WO.ORGANIZATION_ID
7809            AND NVL(WOR.REPETITIVE_SCHEDULE_ID,-1)=NVL(WO.REPETITIVE_SCHEDULE_ID,-1)
7810            AND WOR. WIP_ENTITY_ID =  WTD. WIP_ENTITY_ID(+)
7811            AND WOR. OPERATION_SEQ_NUM = WTD. OPERATION_SEQ_NUM(+)
7812            AND   WOR.RESOURCE_SEQ_NUM = WTD. RESOURCE_SEQ_NUM(+)
7813       ORDER BY RES.billable_item_id;
7814   BEGIN
7815 
7816     -- Standard Start of API savepoint
7817     SAVEPOINT Import_Estimates_wip_sp;
7818 
7819     -- Standard call to check for call compatibility.
7820     IF NOT FND_API.Compatible_API_Call( lc_api_version,
7821                                         p_api_version,
7822                                         lc_api_name,
7823                                         G_PKG_NAME ) THEN
7824       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7825     END IF;
7826 
7827     -- Initialize message list if p_init_msg_list is set to TRUE.
7828     IF FND_API.to_Boolean( p_init_msg_list ) THEN
7829       FND_MSG_PUB.initialize;
7830     END IF;
7831 
7832     -- logging
7833     if (lc_proc_level >= lc_debug_level) then
7834         FND_LOG.STRING(lc_proc_level, lc_mod_name || '.BEGIN',
7835                        'Entering csd_repair_estimate_pvt.import_estimates_from_wip');
7836     end if;
7837 
7838     -- log parameters
7839     if (lc_stat_level >= lc_debug_level) then
7840         FND_LOG.STRING(lc_stat_level, lc_mod_name || '.parameter_logging',
7841               'p_api_version: ' || p_api_version);
7842         FND_LOG.STRING(lc_stat_level, lc_mod_name || '.parameter_logging',
7843               'p_commit: ' || p_commit);
7844         FND_LOG.STRING(lc_stat_level, lc_mod_name || '.parameter_logging',
7845               'p_init_msg_list: ' || p_init_msg_list);
7846         FND_LOG.STRING(lc_stat_level, lc_mod_name || '.parameter_logging',
7847               'p_validation_level: ' || p_validation_level);
7848         FND_LOG.STRING(lc_stat_level, lc_mod_name || '.parameter_logging',
7849               'p_repair_line_id: ' || p_repair_line_id);
7850         FND_LOG.STRING(lc_stat_level, lc_mod_name || '.parameter_logging',
7851               'p_repair_estimate_id: ' || p_repair_estimate_id);
7852         FND_LOG.STRING(lc_stat_level, lc_mod_name || '.parameter_logging',
7853               'p_repair_type_id: ' || p_repair_type_id);
7854         FND_LOG.STRING(lc_stat_level, lc_mod_name || '.parameter_logging',
7855               'p_business_process_id: ' || p_business_process_id);
7856         FND_LOG.STRING(lc_stat_level, lc_mod_name || '.parameter_logging',
7857               'p_currency_code: ' || p_currency_code);
7858         FND_LOG.STRING(lc_stat_level, lc_mod_name || '.parameter_logging',
7859               'p_incident_id: ' || p_incident_id);
7860         FND_LOG.STRING(lc_stat_level, lc_mod_name || '.parameter_logging',
7861               'p_organization_id: ' || p_organization_id);
7862     end if;
7863 
7864     -- Initialize API return status to success
7865     x_return_status := FND_API.G_RET_STS_SUCCESS;
7866 
7867     --
7868     -- Begin API Body
7869     --
7870 
7871     x_warning_flag := FND_API.G_FALSE;
7872 
7873     -- Validate mandatory input parameters.
7874       if (lc_proc_level >= lc_debug_level) then
7875           FND_LOG.STRING(lc_proc_level, lc_mod_name,
7876                        'Calling CSD_PROCESS_UTIL.Check_Reqd_Param for p_repair_line_id');
7877       end if;
7878       CSD_PROCESS_UTIL.Check_Reqd_Param
7879       ( p_param_value    => p_repair_line_id,
7880         p_param_name     => 'REPAIR_LINE_ID',
7881         p_api_name       => lc_api_name);
7882 
7883       if (lc_proc_level >= lc_debug_level) then
7884           FND_LOG.STRING(lc_proc_level, lc_mod_name,
7885                        'Calling CSD_PROCESS_UTIL.Check_Reqd_Param for p_repair_type_id');
7886       end if;
7887 
7888       CSD_PROCESS_UTIL.Check_Reqd_Param
7889       ( p_param_value    => p_repair_type_id,
7890         p_param_name     => 'REPAIR_TYPE_ID',
7891         p_api_name       => lc_api_name);
7892 
7893       if (lc_proc_level >= lc_debug_level) then
7894           FND_LOG.STRING(lc_proc_level, lc_mod_name,
7895                        'Calling CSD_PROCESS_UTIL.Check_Reqd_Param for p_business_process_id');
7896       end if;
7897 
7898       CSD_PROCESS_UTIL.Check_Reqd_Param
7899       ( p_param_value    => p_business_process_id,
7900         p_param_name     => 'BUSINESS_PROCESS_ID',
7901         p_api_name       => lc_api_name);
7902 
7903       if (lc_proc_level >= lc_debug_level) then
7904           FND_LOG.STRING(lc_proc_level, lc_mod_name,
7905                        'Calling CSD_PROCESS_UTIL.Check_Reqd_Param for p_currency_code');
7906       end if;
7907 
7908       CSD_PROCESS_UTIL.Check_Reqd_Param
7909       ( p_param_value    => p_currency_code,
7910         p_param_name     => 'CURRENCY_CODE',
7911         p_api_name       => lc_api_name);
7912 
7913       if (lc_proc_level >= lc_debug_level) then
7914           FND_LOG.STRING(lc_proc_level, lc_mod_name,
7915                        'Calling CSD_PROCESS_UTIL.Check_Reqd_Param for p_incident_id');
7916       end if;
7917 
7918       CSD_PROCESS_UTIL.Check_Reqd_Param
7919       ( p_param_value    => p_incident_id,
7920         p_param_name     => 'INCIDENT_ID',
7921         p_api_name       => lc_api_name);
7922 
7923       if (lc_proc_level >= lc_debug_level) then
7924           FND_LOG.STRING(lc_proc_level, lc_mod_name,
7925                        'Calling CSD_PROCESS_UTIL.Check_Reqd_Param for p_organization_id');
7926       end if;
7927 
7928       CSD_PROCESS_UTIL.Check_Reqd_Param
7929       ( p_param_value    => p_organization_id,
7930         p_param_name     => 'ORGANIZATION_ID',
7931         p_api_name       => lc_api_name);
7932 
7933       if (lc_stat_level >= lc_debug_level) then
7934           FND_LOG.STRING(lc_stat_level, lc_mod_name,
7935                        'Done checking required params');
7936       end if;
7937 
7938     -- We make API calls to get default contract and price list
7939     -- for the repair order. An assumption is that the contract
7940     -- and the pricelist will together make sense. So we do not
7941     -- unnecessarily validate them.
7942 
7943     if (lc_proc_level >= lc_debug_level) then
7944         FND_LOG.STRING(lc_proc_level, lc_mod_name,
7945                      'Calling CSD_CHARGE_LINE_UTIL.Get_DefaultContract');
7946     end if;
7947 
7948     -- Get default Contract.
7949     l_default_contract_line_id := CSD_CHARGE_LINE_UTIL.Get_DefaultContract( p_repair_line_id );
7950 
7951     if (lc_proc_level >= lc_debug_level) then
7952         FND_LOG.STRING(lc_proc_level, lc_mod_name,
7953                      'Returned from CSD_CHARGE_LINE_UTIL.Get_DefaultContract. '
7954                      || 'l_default_contract_line_id = ' || l_default_contract_line_id);
7955     end if;
7956 
7957     if (lc_proc_level >= lc_debug_level) then
7958         FND_LOG.STRING(lc_proc_level, lc_mod_name,
7959                      'Calling CSD_CHARGE_LINE_UTIL.Get_RO_PriceList');
7960     end if;
7961 
7962     -- Get default pricelist for the repair order.
7963     l_default_price_list_hdr_id := CSD_CHARGE_LINE_UTIL.Get_RO_PriceList(p_repair_line_id);
7964 
7965     if (lc_proc_level >= lc_debug_level) then
7966         FND_LOG.STRING(lc_proc_level, lc_mod_name,
7967                      'Returned from CSD_CHARGE_LINE_UTIL.Get_RO_PriceList. '
7968                      || 'l_default_price_list_hdr_id = ' ||l_default_price_list_hdr_id);
7969     end if;
7970 
7971     IF ( l_default_price_list_hdr_id IS NULL ) THEN
7972       if (lc_proc_level >= lc_debug_level) then
7973          FND_LOG.STRING(lc_proc_level, lc_mod_name,
7974                         'Unable to determine default price list for the repair order.');
7975       end if;
7976       -- Unable to determine default pricelist
7977       FND_MESSAGE.SET_NAME( 'CSD', 'CSD_EST_DEFAULT_PL_IMPORT');
7978       -- Unable to determine default price list for the repair order.
7979       -- A default price list must be selected for the repair order to import estimate lines.
7980       FND_MSG_PUB.ADD;
7981       RAISE FND_API.G_EXC_ERROR;
7982     END IF;
7983 
7984     -- Log info messages for all the wip jobs that are shared with other
7985     -- repair orders.
7986     if (lc_stat_level >= lc_debug_level) then
7987        FND_LOG.STRING(lc_stat_level, lc_mod_name,
7988               'Begin LOOP through c_ineligible_WIP_Jobs');
7989     end if;
7990 
7991     FOR inelgible_WIP_rec IN c_ineligible_WIP_Jobs LOOP
7992        IF (lc_stat_level >= lc_debug_level) then
7993            FND_LOG.STRING(lc_stat_level, lc_mod_name,
7994                           'The WIP job ' || inelgible_WIP_rec.JOB_NAME || 'is shared across Repair Orders'
7995                           || '. It is not imported');
7996        END IF;
7997        -- Add an INFO message indicating whether the job will not be imported.
7998        FND_MESSAGE.SET_NAME( 'CSD', 'CSD_EST_INELIGIBLE_WIP');
7999        -- The WIP job $JOB_NAME is submitted for more than one repair order.
8000        -- The estimate lines, for a WIP job that is shared across repair orders,
8001        -- can not be submitted.
8002        FND_MESSAGE.set_token( 'JOB_NAME', inelgible_WIP_rec.JOB_NAME );
8003        FND_MSG_PUB.add_detail( p_message_type => FND_MSG_PUB.G_INFORMATION_MSG );
8004 
8005     END LOOP; -- c_ineligible_WIP_Jobs cursor
8006 
8007     if (lc_stat_level >= lc_debug_level) then
8008        FND_LOG.STRING(lc_stat_level, lc_mod_name,
8009                       'End LOOP c_ineligible_WIP_Jobs');
8010     end if;
8011 
8012     if (p_repair_estimate_id is not null) then
8013         l_repair_estimate_id := p_repair_estimate_id;
8014         if (lc_stat_level >= lc_debug_level) then
8015            FND_LOG.STRING(lc_stat_level, lc_mod_name,
8016                       'Estimate header passed in. l_repair_estimate_id='||l_repair_estimate_id);
8017         end if;
8018     else
8019         -- estimate header was not specified, so need to get it or create it
8020         open c_cur_repair_estimate_info;
8021         fetch c_cur_repair_estimate_info
8022         into l_repair_estimate_id, l_repair_estimate_status, l_repair_estimate_status_name;
8023         close c_cur_repair_estimate_info;
8024 
8025         if (lc_stat_level >= lc_debug_level) then
8026            FND_LOG.STRING(lc_stat_level, lc_mod_name,
8027                       'Estimate header not passed in. Queried repair_estimate_id='||l_repair_estimate_id);
8028         end if;
8029         IF (l_repair_estimate_id is null) THEN
8030             -- required fields
8031             l_repair_estimate_rec.repair_line_id := p_repair_line_id;
8032             l_repair_estimate_rec.estimate_date := sysdate;
8033             l_repair_estimate_rec.estimate_status := 'DRAFT';  -- lookup type CSD_ESTIMATE_STATUS (hardcode for now)
8034 
8035             if (lc_stat_level >= lc_debug_level) then
8036                FND_LOG.STRING(lc_stat_level, lc_mod_name,'Calling CREATE_REPAIR_ESTIMATE');
8037             end if;
8038             CREATE_REPAIR_ESTIMATE(p_api_version =>1.0,
8039                                      p_commit           => Fnd_Api.g_false,
8040                                      p_init_msg_list    => Fnd_Api.g_false,
8041                                      p_validation_level => Fnd_Api.g_valid_level_full,
8042                                      x_estimate_rec     => l_repair_estimate_rec,
8043                                      x_estimate_id      => l_repair_estimate_id,
8044                                      x_return_status    => x_return_status,
8045                                      x_msg_count        => x_msg_count,
8046                                      x_msg_data         => x_msg_data);
8047             IF ( x_return_status <> FND_API.G_RET_STS_SUCCESS ) THEN
8048                if (lc_proc_level >= lc_debug_level) then
8049                    FND_LOG.STRING(lc_proc_level, lc_mod_name,
8050                               'Unable to create repair estimate header for repair_line_id ' || p_repair_line_id);
8051                end if;
8052                RAISE FND_API.G_EXC_ERROR;
8053             END IF;
8054             -- swai: create_repair_estimate doesn't actually return header id in x_estimate_id,
8055             -- so get it from the record instead.
8056             l_repair_estimate_id := l_repair_estimate_rec.repair_estimate_id;
8057             if (lc_proc_level >= lc_debug_level) then
8058                 FND_LOG.STRING(lc_proc_level, lc_mod_name,
8059                           'CREATE_REPAIR_ESTIMATE returned l_repair_estimate_id='||l_repair_estimate_id);
8060             end if;
8061         ELSE
8062             IF (l_repair_estimate_status <> 'NEW') THEN
8063                 -- only estimates in new status can be regenerated
8064                 if (lc_proc_level >= lc_debug_level) then
8065                    FND_LOG.STRING(lc_proc_level, lc_mod_name,
8066                               'Estimate header status is not NEW.  Status =' || l_repair_estimate_status);
8067                 end if;
8068                 FND_MESSAGE.SET_NAME( 'CSD', 'CSD_GEN_EST_STATUS_ERR');
8069                 FND_MESSAGE.set_token( 'STATUS', l_repair_estimate_status_name);
8070                 FND_MSG_PUB.add_detail(p_message_type => FND_MSG_PUB.G_ERROR_MSG);
8071                 RAISE FND_API.G_EXC_ERROR;
8072             ELSE
8073                 -- found estimte header, so change status to DRAFT
8074                 -- required fields
8075                 l_repair_estimate_rec.repair_estimate_id := l_repair_estimate_id;
8076                 l_repair_estimate_rec.estimate_status := 'DRAFT';  -- lookup type CSD_ESTIMATE_STATUS (hardcode for now)
8077 
8078                 select object_version_number
8079                 into l_repair_estimate_rec.object_version_number
8080                 from csd_repair_estimate
8081                 where repair_estimate_id = l_repair_estimate_id;
8082 
8083                 if (lc_stat_level >= lc_debug_level) then
8084                    FND_LOG.STRING(lc_stat_level, lc_mod_name,'Calling UPDATE_REPAIR_ESTIMATE');
8085                 end if;
8086                 UPDATE_REPAIR_ESTIMATE(p_api_version =>1.0,
8087                                                  p_commit           => Fnd_Api.g_false,
8088                                                  p_init_msg_list    => Fnd_Api.g_false,
8089                                                  p_validation_level => Fnd_Api.g_valid_level_full,
8090                                                  x_estimate_rec     => l_repair_estimate_rec,
8091                                                  x_return_status    => x_return_status,
8092                                                  x_msg_count        => x_msg_count,
8093                                                  x_msg_data         => x_msg_data);
8094                 IF ( x_return_status <> FND_API.G_RET_STS_SUCCESS ) THEN
8095                    if (lc_proc_level >= lc_debug_level) then
8096                        FND_LOG.STRING(lc_proc_level, lc_mod_name,
8097                                   'Unable to update repair estimate header for repair_line_id ' || p_repair_line_id);
8098                    end if;
8099                    RAISE FND_API.G_EXC_ERROR;
8100                 END IF;
8101                 if (lc_proc_level >= lc_debug_level) then
8102                     FND_LOG.STRING(lc_proc_level, lc_mod_name,
8103                               'UPDATE_REPAIR_ESTIMATE returned l_repair_estimate_id='||l_repair_estimate_id);
8104                 end if;
8105 
8106                 -- Now delete any existing estimate lines so that the estimate can be regenerated
8107                 FOR curr_est_line_rec IN c_estimate_lines(l_repair_estimate_id) LOOP
8108                     if (lc_stat_level >= lc_debug_level) then
8109                        FND_LOG.STRING(lc_stat_level, lc_mod_name,'Calling DELETE_REPAIR_ESTIMATE_LINES');
8110                     end if;
8111                     DELETE_REPAIR_ESTIMATE_LINES(p_api_version =>1.0,
8112                                                      p_commit           => Fnd_Api.g_false,
8113                                                      p_init_msg_list    => Fnd_Api.g_false,
8114                                                      p_validation_level => Fnd_Api.g_valid_level_full,
8115                                                      p_estimate_line_id => curr_est_line_rec.repair_estimate_line_id,
8116                                                      x_return_status    => x_return_status,
8117                                                      x_msg_count        => x_msg_count,
8118                                                      x_msg_data         => x_msg_data);
8119                     IF ( x_return_status <> FND_API.G_RET_STS_SUCCESS ) THEN
8120                        if (lc_proc_level >= lc_debug_level) then
8121                            FND_LOG.STRING(lc_proc_level, lc_mod_name,
8122                                       'Unable to delete repair estimate line ' || curr_est_line_rec.repair_estimate_line_id);
8123                        end if;
8124                        RAISE FND_API.G_EXC_ERROR;
8125                     END IF;
8126                     if (lc_proc_level >= lc_debug_level) then
8127                         FND_LOG.STRING(lc_proc_level, lc_mod_name,
8128                                   'Returned from DELETE_REPAIR_ESTIMATE_LINES');
8129                     end if;
8130                 END LOOP;
8131             END IF;
8132         END IF;
8133     end if;
8134 
8135     -- The following is the outermost loop to ensure that we
8136     -- process one WIP Job at a time.
8137     -- The idea is that if we get into an error while processing
8138     -- a WIP Job, we skip that one, log a ERROR message and
8139     -- continue with next WIP job.
8140     if (lc_stat_level >= lc_debug_level) then
8141        FND_LOG.STRING(lc_stat_level, lc_mod_name,
8142                       'Begin LOOP through c_eligible_WIP_Jobs');
8143     end if;
8144 
8145     FOR curr_WIP_job_rec IN c_eligible_WIP_Jobs LOOP
8146       l_wip_count := l_wip_count + 1;
8147 
8148       if (lc_stat_level >= lc_debug_level) then
8149           FND_LOG.STRING(lc_stat_level, lc_mod_name,
8150                       'WIP count (l_wip_count) is = ' || l_wip_count);
8151       end if;
8152 
8153       DECLARE
8154         l_estimate_lines_tbl    CSD_REPAIR_ESTIMATE_PVT.REPAIR_ESTIMATE_LINE_TBL;
8155         l_Estimates_count       NUMBER      := 0;
8156         l_return_status       VARCHAR2(1)  := FND_API.G_RET_STS_SUCCESS;
8157         l_msg_count           NUMBER;
8158         l_msg_data            VARCHAR2(200);
8159 
8160       BEGIN
8161 
8162         if (lc_stat_level >= lc_debug_level) then
8163            FND_LOG.STRING(lc_stat_level, lc_mod_name,
8164                           'Inside the BLOCK for processing one WIP job at a time.');
8165         end if;
8166 
8167         /****** Processing the Material transactions specific data - START  *********/
8168 
8169         if (lc_stat_level >= lc_debug_level) then
8170            FND_LOG.STRING(lc_stat_level, lc_mod_name,
8171                           'Start: Processing material transactions for the WIP job - ' || curr_WIP_job_rec.JOB_NAME);
8172         end if;
8173 
8174 
8175         -- First, we process the material transaction lines for all the WIP
8176         -- jobs. Getting the table for all the lines.
8177         DECLARE
8178           l_count NUMBER := 0;
8179         BEGIN
8180 
8181            IF ((nvl(fnd_profile.value('CSD_IMPORT_WIP_MTL_TO_ESTIMATES'),'NONE') in ('TRANS', 'ALL'))) THEN
8182                if (nvl(fnd_profile.value('CSD_IMPORT_WIP_TO_ESTIMATES_NET_QTY'),'Y') = 'N') THEN
8183                   if (lc_stat_level >= lc_debug_level) then
8184                      FND_LOG.STRING(lc_stat_level, lc_mod_name,
8185                                     'Begin: loop through the cursor c_est_lines_from_materials1.');
8186                   end if;
8187                   FOR Estimates_rec IN c_est_lines_from_materials1( curr_WIP_job_rec.wip_entity_id,
8188                                                                      curr_WIP_job_rec.inventory_item_id) LOOP
8189                     if (Estimates_rec.quantity > 0) then
8190                         l_count := l_count + 1;
8191                         if (lc_stat_level >= lc_debug_level) then
8192                            FND_LOG.STRING(lc_stat_level, lc_mod_name, 'count='||l_Count);
8193                            FND_LOG.STRING(lc_stat_level, lc_mod_name, 'inventory_item_id='||Estimates_rec.inventory_item_id);
8194                            FND_LOG.STRING(lc_stat_level, lc_mod_name, 'quantity='||Estimates_rec.quantity);
8195                         end if;
8196                         l_estimate_lines_tbl( l_count ).inventory_item_id := Estimates_rec.inventory_item_id;
8197                         l_estimate_lines_tbl( l_count ).unit_of_measure_code := Estimates_rec.uom;
8198                         l_estimate_lines_tbl( l_count ).estimate_quantity := Estimates_rec.quantity;
8199                         l_estimate_lines_tbl( l_count ).txn_billing_type_id := Estimates_rec.txn_billing_type_id;
8200                         l_estimate_lines_tbl( l_count ).transaction_type_id := Estimates_rec.transaction_type_id;
8201                         l_estimate_lines_tbl( l_count ).EST_LINE_SOURCE_TYPE_CODE := Estimates_rec.estimate_source_code;
8202                         l_estimate_lines_tbl( l_count ).EST_LINE_SOURCE_ID1 := Estimates_rec.estimate_source_id;
8203                         l_estimate_lines_tbl( l_count ).EST_LINE_SOURCE_ID2 := Estimates_rec.operation_seq_num;
8204                         l_estimate_lines_tbl( l_count ).repair_line_id := p_repair_line_id;
8205                         l_estimate_lines_tbl( l_count ).repair_estimate_id := l_repair_estimate_id;
8206                         l_estimate_lines_tbl( l_count ).business_process_id := p_business_process_id;
8207                         l_estimate_lines_tbl( l_count ).currency_code := p_currency_code;
8208                         l_estimate_lines_tbl( l_count ).incident_id := p_incident_id;
8209                         l_estimate_lines_tbl( l_count ).organization_id := p_organization_id;
8210                         l_estimate_lines_tbl( l_count ).price_list_id := l_default_price_list_hdr_id;
8211                         l_estimate_lines_tbl( l_count ).contract_line_id := l_default_contract_line_id;
8212                         l_estimate_lines_tbl( l_count ).charge_line_type := G_CHARGE_LINE_TYPE_ESTIMATE;
8213                     end if;
8214                   END LOOP;
8215                   if (lc_stat_level >= lc_debug_level) then
8216                      FND_LOG.STRING(lc_stat_level, lc_mod_name,
8217                                     'End: loop through the cursor c_est_lines_from_materials1.');
8218                   end if;
8219               else
8220                   if (lc_stat_level >= lc_debug_level) then
8221                      FND_LOG.STRING(lc_stat_level, lc_mod_name,
8222                                     'Begin: loop through the cursor c_est_lines_from_materials.');
8223                   end if;
8224                   FOR Estimates_rec IN c_est_lines_from_materials( curr_WIP_job_rec.wip_entity_id,
8225                                                                      curr_WIP_job_rec.inventory_item_id) LOOP
8226                     if (Estimates_rec.quantity > 0) then
8227                         l_count := l_count + 1;
8228                         if (lc_stat_level >= lc_debug_level) then
8229                            FND_LOG.STRING(lc_stat_level, lc_mod_name, 'count='||l_Count);
8230                            FND_LOG.STRING(lc_stat_level, lc_mod_name, 'inventory_item_id='||Estimates_rec.inventory_item_id);
8231                            FND_LOG.STRING(lc_stat_level, lc_mod_name, 'quantity='||Estimates_rec.quantity);
8232                         end if;
8233                         l_estimate_lines_tbl( l_count ).inventory_item_id := Estimates_rec.inventory_item_id;
8234                         l_estimate_lines_tbl( l_count ).unit_of_measure_code := Estimates_rec.uom;
8235                         l_estimate_lines_tbl( l_count ).estimate_quantity := Estimates_rec.quantity;
8236                         l_estimate_lines_tbl( l_count ).txn_billing_type_id := Estimates_rec.txn_billing_type_id;
8237                         l_estimate_lines_tbl( l_count ).transaction_type_id := Estimates_rec.transaction_type_id;
8238                         l_estimate_lines_tbl( l_count ).EST_LINE_SOURCE_TYPE_CODE := Estimates_rec.estimate_source_code;
8239                         l_estimate_lines_tbl( l_count ).EST_LINE_SOURCE_ID1 := Estimates_rec.estimate_source_id;
8240                         l_estimate_lines_tbl( l_count ).EST_LINE_SOURCE_ID2 := Estimates_rec.operation_seq_num;
8241                         l_estimate_lines_tbl( l_count ).repair_line_id := p_repair_line_id;
8242                         l_estimate_lines_tbl( l_count ).repair_estimate_id := l_repair_estimate_id;
8243                         l_estimate_lines_tbl( l_count ).business_process_id := p_business_process_id;
8244                         l_estimate_lines_tbl( l_count ).currency_code := p_currency_code;
8245                         l_estimate_lines_tbl( l_count ).incident_id := p_incident_id;
8246                         l_estimate_lines_tbl( l_count ).organization_id := p_organization_id;
8247                         l_estimate_lines_tbl( l_count ).price_list_id := l_default_price_list_hdr_id;
8248                         l_estimate_lines_tbl( l_count ).contract_line_id := l_default_contract_line_id;
8249                         l_estimate_lines_tbl( l_count ).charge_line_type := G_CHARGE_LINE_TYPE_ESTIMATE;
8250                     end if;
8251                   END LOOP;
8252                   if (lc_stat_level >= lc_debug_level) then
8253                      FND_LOG.STRING(lc_stat_level, lc_mod_name,
8254                                     'End: loop through the cursor c_est_lines_from_materials.');
8255                   end if;
8256               end if;
8257           END IF;
8258 
8259           IF ((nvl(fnd_profile.value('CSD_IMPORT_WIP_MTL_TO_ESTIMATES'),'NONE') in ('UNTRANS', 'ALL'))) THEN
8260               if (lc_stat_level >= lc_debug_level) then
8261                  FND_LOG.STRING(lc_stat_level, lc_mod_name,
8262                                 'Begin: loop through the cursor c_est_lines_from_material_reqs.');
8263               end if;
8264               FOR Estimates_rec IN c_est_lines_from_material_reqs( curr_WIP_job_rec.wip_entity_id,
8265                                                                  curr_WIP_job_rec.inventory_item_id) LOOP
8266                 if (Estimates_rec.quantity > 0) then
8267                     l_count := l_count + 1;
8268                     if (lc_stat_level >= lc_debug_level) then
8269                        FND_LOG.STRING(lc_stat_level, lc_mod_name, 'count='||l_Count);
8270                        FND_LOG.STRING(lc_stat_level, lc_mod_name, 'inventory_item_id='||Estimates_rec.inventory_item_id);
8271                        FND_LOG.STRING(lc_stat_level, lc_mod_name, 'quantity='||Estimates_rec.quantity);
8272                     end if;
8273                     l_estimate_lines_tbl( l_count ).inventory_item_id := Estimates_rec.inventory_item_id;
8274                     l_estimate_lines_tbl( l_count ).unit_of_measure_code := Estimates_rec.uom;
8275                     l_estimate_lines_tbl( l_count ).estimate_quantity := Estimates_rec.quantity;
8276                     l_estimate_lines_tbl( l_count ).txn_billing_type_id := Estimates_rec.txn_billing_type_id;
8277                     l_estimate_lines_tbl( l_count ).transaction_type_id := Estimates_rec.transaction_type_id;
8278                     l_estimate_lines_tbl( l_count ).EST_LINE_SOURCE_TYPE_CODE := Estimates_rec.estimate_source_code;
8279                     l_estimate_lines_tbl( l_count ).EST_LINE_SOURCE_ID1 := Estimates_rec.estimate_source_id;
8280                     l_estimate_lines_tbl( l_count ).EST_LINE_SOURCE_ID2 := Estimates_rec.operation_seq_num;
8281                     l_estimate_lines_tbl( l_count ).repair_line_id := p_repair_line_id;
8282                     l_estimate_lines_tbl( l_count ).repair_estimate_id := l_repair_estimate_id;
8283                     l_estimate_lines_tbl( l_count ).business_process_id := p_business_process_id;
8284                     l_estimate_lines_tbl( l_count ).currency_code := p_currency_code;
8285                     l_estimate_lines_tbl( l_count ).incident_id := p_incident_id;
8286                     l_estimate_lines_tbl( l_count ).organization_id := p_organization_id;
8287                     l_estimate_lines_tbl( l_count ).price_list_id := l_default_price_list_hdr_id;
8288                     l_estimate_lines_tbl( l_count ).contract_line_id := l_default_contract_line_id;
8289                     l_estimate_lines_tbl( l_count ).charge_line_type := G_CHARGE_LINE_TYPE_ESTIMATE;
8290                  end if;
8291               END LOOP;
8292               if (lc_stat_level >= lc_debug_level) then
8293                  FND_LOG.STRING(lc_stat_level, lc_mod_name,
8294                                 'End: loop through the cursor c_est_lines_from_material_reqs.');
8295               end if;
8296           END IF;
8297         END;
8298 
8299         if (lc_stat_level >= lc_debug_level) then
8300            FND_LOG.STRING(lc_stat_level, lc_mod_name,
8301                           'End: Processing material transactions for the WIP job - ' || curr_WIP_job_rec.JOB_NAME);
8302            FND_LOG.STRING(lc_stat_level, lc_mod_name,
8303                           'Estimate line count =  ' || l_estimate_lines_tbl.COUNT);
8304         end if;
8305 
8306         /****** Processing the Material transactions specific data - END  *********/
8307 
8308 
8309 
8310         /****** Processing the Resource/WIP transactions specific data - START  *********/
8311 
8312         if (lc_stat_level >= lc_debug_level) then
8313            FND_LOG.STRING(lc_stat_level, lc_mod_name,
8314                           'Start: Processing Resource/WIP transactions for the WIP job - ' || curr_WIP_job_rec.JOB_NAME);
8315         end if;
8316         -- Now, we process the WIP transaction lines (for resources) for all
8317         -- the WIP jobs. Getting the table for all the lines.
8318         DECLARE
8319           l_count NUMBER := l_estimate_lines_tbl.count; -- append to the materials in the table
8320         BEGIN
8321           IF ((nvl(fnd_profile.value('CSD_IMPORT_WIP_RES_TO_ESTIMATES'),'NONE') in ('TRANS', 'ALL'))) THEN
8322               if (lc_stat_level >= lc_debug_level) then
8323                  FND_LOG.STRING(lc_stat_level, lc_mod_name,
8324                                 'Begin: loop through the cursor c_est_lines_from_resources.');
8325               end if;
8326               FOR Estimates_rec IN c_est_lines_from_resources( curr_WIP_job_rec.wip_entity_id) LOOP
8327                 if (Estimates_rec.quantity > 0) then
8328                     l_count := l_count + 1;
8329                     if (lc_stat_level >= lc_debug_level) then
8330                        FND_LOG.STRING(lc_stat_level, lc_mod_name, 'count='||l_Count);
8331                        FND_LOG.STRING(lc_stat_level, lc_mod_name, 'inventory_item_id='||Estimates_rec.inventory_item_id);
8332                        FND_LOG.STRING(lc_stat_level, lc_mod_name, 'quantity='||Estimates_rec.quantity);
8333                     end if;
8334                     l_estimate_lines_tbl( l_count ).inventory_item_id := Estimates_rec.inventory_item_id;
8335                     l_estimate_lines_tbl( l_count ).unit_of_measure_code := Estimates_rec.uom;
8336                     l_estimate_lines_tbl( l_count ).estimate_quantity := Estimates_rec.quantity;
8337                     l_estimate_lines_tbl( l_count ).txn_billing_type_id := Estimates_rec.txn_billing_type_id;
8338                     l_estimate_lines_tbl( l_count ).transaction_type_id := Estimates_rec.transaction_type_id;
8339                     l_estimate_lines_tbl( l_count ).EST_LINE_SOURCE_TYPE_CODE := Estimates_rec.estimate_source_code;
8340                     l_estimate_lines_tbl( l_count ).EST_LINE_SOURCE_ID1 := Estimates_rec.estimate_source_id;
8341                     l_estimate_lines_tbl( l_count ).EST_LINE_SOURCE_ID2 := Estimates_rec.operation_seq_num;
8342                     l_estimate_lines_tbl( l_count ).repair_line_id := p_repair_line_id;
8343                     l_estimate_lines_tbl( l_count ).repair_estimate_id := l_repair_estimate_id;
8344                     l_estimate_lines_tbl( l_count ).business_process_id := p_business_process_id;
8345                     l_estimate_lines_tbl( l_count ).currency_code := p_currency_code;
8346                     l_estimate_lines_tbl( l_count ).incident_id := p_incident_id;
8347                     l_estimate_lines_tbl( l_count ).organization_id := p_organization_id;
8348                     l_estimate_lines_tbl( l_count ).price_list_id := l_default_price_list_hdr_id;
8349                     l_estimate_lines_tbl( l_count ).contract_line_id := l_default_contract_line_id;
8350                     l_estimate_lines_tbl( l_count ).charge_line_type := G_CHARGE_LINE_TYPE_ESTIMATE;
8351                     l_estimate_lines_tbl( l_count ).resource_id := Estimates_rec.resource_id;
8352                 end if;
8353               END LOOP;
8354               if (lc_stat_level >= lc_debug_level) then
8355                  FND_LOG.STRING(lc_stat_level, lc_mod_name,
8356                                 'End: loop through the cursor c_est_lines_from_resources.');
8357               end if;
8358           END IF;
8359 
8360           IF ((nvl(fnd_profile.value('CSD_IMPORT_WIP_RES_TO_ESTIMATES'),'NONE') in ('UNTRANS', 'ALL'))) THEN
8361               if (lc_stat_level >= lc_debug_level) then
8362                  FND_LOG.STRING(lc_stat_level, lc_mod_name,
8363                                 'Begin: loop through the cursor c_est_lines_from_resource_reqs.');
8364               end if;
8365               FOR Estimates_rec IN c_est_lines_from_resource_reqs( curr_WIP_job_rec.wip_entity_id) LOOP
8366                 if (Estimates_rec.quantity > 0) then
8367                     l_count := l_count + 1;
8368                     if (lc_stat_level >= lc_debug_level) then
8369                        FND_LOG.STRING(lc_stat_level, lc_mod_name, 'count='||l_Count);
8370                        FND_LOG.STRING(lc_stat_level, lc_mod_name, 'inventory_item_id='||Estimates_rec.inventory_item_id);
8371                        FND_LOG.STRING(lc_stat_level, lc_mod_name, 'quantity='||Estimates_rec.quantity);
8372                     end if;
8373                     l_estimate_lines_tbl( l_count ).inventory_item_id := Estimates_rec.inventory_item_id;
8374                     l_estimate_lines_tbl( l_count ).unit_of_measure_code := Estimates_rec.uom;
8375                     l_estimate_lines_tbl( l_count ).estimate_quantity := Estimates_rec.quantity;
8376                     l_estimate_lines_tbl( l_count ).txn_billing_type_id := Estimates_rec.txn_billing_type_id;
8377                     l_estimate_lines_tbl( l_count ).transaction_type_id := Estimates_rec.transaction_type_id;
8378                     l_estimate_lines_tbl( l_count ).EST_LINE_SOURCE_TYPE_CODE := Estimates_rec.estimate_source_code;
8379                     l_estimate_lines_tbl( l_count ).EST_LINE_SOURCE_ID1 := Estimates_rec.estimate_source_id;
8380                     l_estimate_lines_tbl( l_count ).EST_LINE_SOURCE_ID2 := Estimates_rec.operation_seq_num;
8381                     l_estimate_lines_tbl( l_count ).repair_line_id := p_repair_line_id;
8382                     l_estimate_lines_tbl( l_count ).repair_estimate_id := l_repair_estimate_id;
8383                     l_estimate_lines_tbl( l_count ).business_process_id := p_business_process_id;
8384                     l_estimate_lines_tbl( l_count ).currency_code := p_currency_code;
8385                     l_estimate_lines_tbl( l_count ).incident_id := p_incident_id;
8386                     l_estimate_lines_tbl( l_count ).organization_id := p_organization_id;
8387                     l_estimate_lines_tbl( l_count ).price_list_id := l_default_price_list_hdr_id;
8388                     l_estimate_lines_tbl( l_count ).contract_line_id := l_default_contract_line_id;
8389                     l_estimate_lines_tbl( l_count ).charge_line_type := G_CHARGE_LINE_TYPE_ESTIMATE;
8390                     l_estimate_lines_tbl( l_count ).resource_id := Estimates_rec.resource_id;
8391                 end if;
8392               END LOOP;
8393               if (lc_stat_level >= lc_debug_level) then
8394                  FND_LOG.STRING(lc_stat_level, lc_mod_name,
8395                                 'End: loop through the cursor c_est_lines_from_resource_reqs.');
8396               end if;
8397           END IF;
8398         END;
8399 
8400         if (lc_stat_level >= lc_debug_level) then
8401            FND_LOG.STRING(lc_stat_level, lc_mod_name,
8402                           'END: Processing Resource/WIP transactions for the WIP job - ' || curr_WIP_job_rec.JOB_NAME);
8403            FND_LOG.STRING(lc_stat_level, lc_mod_name,
8404                           'Estimate line count =  ' || l_estimate_lines_tbl.COUNT);
8405         end if;
8406 
8407         /****** Processing the Resource/WIP transactions specific data - END *********/
8408 
8409         -- PO number from RO should be defaulted on estimate lines created from WIP.
8410         begin
8411           select default_po_num
8412             into l_default_po_number
8413             from csd_repairs
8414            where repair_line_id = p_repair_line_id;
8415         exception
8416             when no_data_found then
8417               l_default_po_number := Null;
8418             when others then
8419               l_default_po_number := Null;
8420         end;
8421 
8422         l_Estimates_count := l_estimate_lines_tbl.COUNT;
8423 
8424         -- Insert repair estimate line for each record in the tbl.
8425         -- If any row encounters any error, stop processing
8426         -- any further.
8427         FOR i IN 1..l_Estimates_count LOOP
8428           BEGIN
8429               If l_default_po_number is not null then
8430                 l_estimate_lines_tbl( i ).purchase_order_num := l_default_po_number ;
8431               end if;
8432 
8433               if (lc_stat_level >= lc_debug_level) then
8434                 FND_LOG.STRING(lc_stat_level, lc_mod_name,
8435                                'Calling procedure CSD_REPAIR_ESTIMATE_PVT.create_repair_estimate_lines');
8436                 FND_LOG.STRING(lc_stat_level, lc_mod_name,
8437                                'index =  ' || TO_CHAR( i ));
8438               end if;
8439               CSD_REPAIR_ESTIMATE_PVT.create_repair_estimate_lines( p_api_version           => 1.0,
8440                                                                   p_commit                => FND_API.G_FALSE,
8441                                                                   p_init_msg_list         => FND_API.G_FALSE,
8442                                                                   p_validation_level      => 0,
8443                                                                   x_estimate_line_rec     => l_estimate_lines_tbl( i ),
8444                                                                   x_estimate_line_id      => l_estimate_lines_tbl( i ).repair_estimate_line_id,
8445                                                                   x_return_status         => l_return_status,
8446                                                                   x_msg_count             => l_msg_count,
8447                                                                   x_msg_data              => l_msg_data );
8448 
8449               IF ( l_return_status <> FND_API.G_RET_STS_SUCCESS ) THEN
8450                 if (lc_proc_level >= lc_debug_level) then
8451                    FND_LOG.STRING(lc_proc_level, lc_mod_name,
8452                                   'Unable to create repair estimate lines for index = ' || i);
8453                 end if;
8454                 -- swai: do not raise exception.  Flag as warning and process the next line.
8455                 -- RAISE FND_API.G_EXC_ERROR;
8456                 x_warning_flag := FND_API.G_TRUE;
8457                 -- Add an ERROR message.
8458                 -- An error encountered while creating an estimate line for item $ITEM
8459                 FND_MESSAGE.SET_NAME( 'CSD', 'CSD_EST_LINE_CREATED_ERR');
8460                 FND_MESSAGE.set_token( 'ITEM', l_estimate_lines_tbl( i ).inventory_item_id );
8461                 FND_MSG_PUB.add_detail(p_message_type => FND_MSG_PUB.G_ERROR_MSG);
8462               ELSE
8463                 l_total_estimates_lines := l_total_estimates_lines +1;
8464                 if (lc_proc_level >= lc_debug_level) then
8465                    FND_LOG.STRING(lc_proc_level, lc_mod_name,
8466                                   'Estimate line created:' ||  l_estimate_lines_tbl( i ).repair_estimate_line_id);
8467                 end if;
8468               END IF;
8469            EXCEPTION
8470               WHEN FND_API.G_EXC_ERROR THEN
8471                 x_warning_flag := FND_API.G_TRUE;
8472                 -- Add an ERROR message.
8473                 -- An error encountered while creating Estimate line $INDEX for inventory item $INV_ITEM_ID
8474                 FND_MESSAGE.SET_NAME( 'CSD', 'CSD_EST_LINE_CREATED_ERR');
8475                 FND_MESSAGE.set_token( 'ITEM', l_estimate_lines_tbl( i ).inventory_item_id );
8476                 FND_MSG_PUB.add_detail(p_message_type => FND_MSG_PUB.G_ERROR_MSG);
8477            END;
8478         END LOOP;
8479 
8480         if (lc_proc_level >= lc_debug_level) then
8481            FND_LOG.STRING(lc_proc_level, lc_mod_name,
8482                           'End of loop for creating estimate lines. l_Estimates_count = ' || l_Estimates_count);
8483         end if;
8484 
8485         IF ( x_warning_flag <> FND_API.G_FALSE ) THEN
8486           -- Import for the WIP Job $JOB_NAME completed with warnings.
8487           -- $ESTIMATE_LINE_COUNT new repair estimate line(s) were created for the job.
8488           FND_MESSAGE.SET_NAME( 'CSD', 'CSD_EST_WIP_IMPORT_W_WARN' );
8489           FND_MESSAGE.set_token( 'JOB_NAME', curr_WIP_job_rec.JOB_NAME );
8490           FND_MESSAGE.set_token( 'ESTIMATE_LINE_COUNT', l_Estimates_count );
8491           FND_MSG_PUB.add_detail( p_message_type => FND_MSG_PUB.G_INFORMATION_MSG );
8492           if (lc_stat_level >= lc_debug_level) then
8493              FND_LOG.STRING(lc_stat_level, lc_mod_name,
8494                             'Debrief lines import for the WIP Job ' || curr_WIP_job_rec.JOB_NAME
8495                             || ' completed with warnings. l_Estimates_count = ' || l_Estimates_count);
8496           end if;
8497         END IF;
8498 
8499         EXCEPTION
8500          -- If there is an error for a specific WIP Job then we
8501          -- do not consider it be a fatal error. We do not stop
8502          -- the process but continue with the next WIP job
8503 
8504           WHEN FND_API.G_EXC_ERROR THEN
8505             x_warning_flag := FND_API.G_TRUE;
8506 
8507             -- Add an ERROR message.
8508             FND_MESSAGE.SET_NAME( 'CSD', 'CSD_EST_WIP_IMPORT_ERROR');
8509             -- An error encountered while importing WIP lines into Estimates for the WIP entity - $JOB_NAME.
8510             FND_MESSAGE.set_token( 'JOB_NAME', curr_WIP_job_rec.JOB_NAME );
8511             FND_MSG_PUB.add_detail(p_message_type => FND_MSG_PUB.G_ERROR_MSG);
8512 
8513           WHEN OTHERS THEN
8514             ROLLBACK TO curr_wip_job_sp;
8515              x_warning_flag := FND_API.G_TRUE;
8516 
8517             if (lc_proc_level >= lc_debug_level) then
8518                FND_LOG.STRING(lc_proc_level, lc_mod_name,
8519                               'Encountered an OTHERS error while creating a repair estimate lines ' ||
8520                               'for the WIP Job ' || curr_WIP_job_rec.JOB_NAME ||
8521                               '. SQLCODE = ' || SQLCODE || '. SQLERRM = ' || SQLERRM);
8522             end if;
8523 
8524             IF FND_MSG_PUB.Check_Msg_Level( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR ) THEN
8525               FND_MSG_PUB.Add_Exc_Msg( G_PKG_NAME, lc_api_name );
8526             END IF;
8527 
8528             FND_MESSAGE.SET_NAME( 'CSD', 'CSD_EST_WIP_IMP_JOB_ERR');
8529             -- Unknown error encountered while importing WIP debrief lines to Estimates for the WIP entity $JOB_NAME. SQLCODE = $SQLCODE, SQLERRM = $SQLERRM.
8530             FND_MESSAGE.SET_TOKEN('SQLCODE', SQLCODE);
8531             FND_MESSAGE.SET_TOKEN('SQLERRM', SQLERRM);
8532             FND_MESSAGE.set_token( 'JOB_NAME', curr_WIP_job_rec.JOB_NAME );
8533             FND_MSG_PUB.add_detail(p_message_type => FND_MSG_PUB.G_ERROR_MSG);
8534       END;
8535     END LOOP; -- for cursor c_eligible_WIP_Jobs
8536 
8537     if (lc_stat_level >= lc_debug_level) then
8538        FND_LOG.STRING(lc_stat_level, lc_mod_name,
8539                       'End LOOP c_eligible_WIP_Jobs');
8540     end if;
8541 
8542     IF( l_wip_count <= 0 ) THEN
8543      x_warning_flag := FND_API.G_TRUE;
8544        FND_MESSAGE.SET_NAME( 'CSD', 'CSD_EST_NO_INELIGIBLE_WIP');
8545        -- No eligible WIP jobs found for import
8546        -- FND_MSG_PUB.add_detail(p_message_type => FND_MSG_PUB.G_INFORMATION_MSG);
8547        -- FND_MSG_PUB.add_detail(p_message_type => FND_MSG_PUB.G_WARNING_MSG);
8548        FND_MSG_PUB.add;
8549 
8550        -- if no lines were imported, then raise an error so we can rollback and save
8551        -- any pre-existing lines from being deleted/archived.
8552        RAISE FND_API.G_EXC_ERROR;
8553     END IF;
8554 
8555     IF( l_total_estimates_lines <= 0 ) THEN
8556      x_warning_flag := FND_API.G_TRUE;
8557        FND_MESSAGE.SET_NAME( 'CSD', 'CSD_EST_NO_LINES_ELIGIBLE_WIP');
8558        FND_MSG_PUB.add;
8559 
8560        -- if no lines were imported, then raise an error so we can rollback and save
8561        -- any pre-existing lines from being deleted/archived.
8562        RAISE FND_API.G_EXC_ERROR;
8563     END IF;
8564 
8565     --
8566     -- End API Body
8567     --
8568 
8569     -- Standard check of p_commit.
8570     IF FND_API.To_Boolean( p_commit ) THEN
8571       COMMIT WORK;
8572     END IF;
8573 
8574     -- logging
8575     if (lc_proc_level >= lc_debug_level) then
8576         FND_LOG.STRING(lc_proc_level, lc_mod_name || '.END',
8577                        'Leaving CSD_REPAIR_ESTIMATE_PVT.IMPORT_ESTIMATES_FROM_WIP');
8578     end if;
8579 
8580     EXCEPTION
8581       WHEN FND_API.G_EXC_ERROR THEN
8582         x_return_status := FND_API.G_RET_STS_ERROR;
8583         ROLLBACK TO Import_Estimates_wip_sp;
8584         FND_MSG_PUB.Count_And_Get( p_count => x_msg_count,
8585                                    p_data  => x_msg_data );
8586 
8587         -- save message in debug log
8588         IF (lc_excep_level >= lc_debug_level) THEN
8589            FND_LOG.STRING(lc_excep_level, lc_mod_name,
8590                           'EXC_ERROR['||x_msg_data||']');
8591         END IF;
8592 
8593       WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8594         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8595         ROLLBACK TO Import_Estimates_wip_sp;
8596         FND_MSG_PUB.Count_And_Get( p_count => x_msg_count,
8597                                    p_data  => x_msg_data );
8598 
8599         -- save message in debug log
8600         IF (lc_excep_level >= lc_debug_level) THEN
8601            FND_LOG.STRING(lc_excep_level, lc_mod_name,
8602                           'EXC_UNEXPECTED_ERROR['||x_msg_data||']');
8603         END IF;
8604 
8605       WHEN OTHERS THEN
8606         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8607         ROLLBACK TO Import_Estimates_wip_sp;
8608 
8609         IF FND_MSG_PUB.Check_Msg_Level( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR ) THEN
8610           FND_MSG_PUB.Add_Exc_Msg( G_PKG_NAME, lc_api_name );
8611         END IF;
8612 
8613         -- save message in debug log
8614         IF (lc_excep_level >= lc_debug_level) THEN
8615            FND_LOG.STRING(lc_excep_level, lc_mod_name,
8616                           'WHEN OTHERS THEN. SQL Message['||SQLERRM||']');
8617         END IF;
8618 
8619         FND_MESSAGE.SET_NAME( 'CSD', 'CSD_EST_WIP_IMPORT_OTHERS');
8620         -- Unknown error encountered while importing WIP lines to Estimates. SQLCODE = $SQLCODE, SQLERRM = $SQLERRM
8621         FND_MESSAGE.SET_TOKEN('SQLCODE', SQLCODE);
8622         FND_MESSAGE.SET_TOKEN('SQLERRM', SQLERRM);
8623         FND_MSG_PUB.add_detail(p_message_type => FND_MSG_PUB.G_ERROR_MSG);
8624         FND_MSG_PUB.Count_And_Get( p_count => x_msg_count,
8625                                    p_data  => x_msg_data );
8626   END Import_Estimates_From_Wip;
8627 
8628 END Csd_Repair_Estimate_Pvt;