DBA Data[Home] [Help]

PACKAGE BODY: APPS.CSD_UPDATE_PROGRAMS_PVT

Source


1 Package Body CSD_UPDATE_PROGRAMS_PVT as
2 /* $Header: csddrclb.pls 120.12.12010000.6 2008/12/05 01:55:12 takwong ship $ */
3 
4 /* --------------------------------------*/
5 /* Define global variables               */
6 /* --------------------------------------*/
7 
8 G_PKG_NAME  CONSTANT VARCHAR2(30)  := 'CSD_UPDATE_PROGRAMS_PVT';
9 G_FILE_NAME CONSTANT VARCHAR2(30)  := 'csddrclb.pls';
10 
11 -- Global variable for storing the debug level
12 G_debug_level number   := FND_LOG.G_CURRENT_RUNTIME_LEVEL;
13 
14 -- Added by Vijay 10/22/04
15 TYPE RCPT_LINES_Rec_Type IS RECORD
16 (
17     rma_number               oe_order_headers_all.order_number%type,
18     rma_header_id            oe_order_headers_all.header_id%type ,
19     line_id                  oe_order_lines_all.line_id%type ,
20     rma_line_number          oe_order_lines_all.line_number%type ,
21     inventory_item_id        oe_order_lines_all.inventory_item_id%type,
22     org_name                 hr_all_organization_units.name%type ,
23     organization_id          rcv_transactions.organization_id%type,
24     unit_of_measure          rcv_transactions.unit_of_measure%type,
25     received_quantity        rcv_transactions.quantity%type ,
26     received_date            rcv_transactions.transaction_date%type ,
27     transaction_id           rcv_transactions.transaction_id%type,
28     subinventory             rcv_transactions.subinventory%type,
29     locator_id               rcv_transactions.locator_id%type,
30     transaction_type         rcv_transactions.transaction_type%type,
31     ro_serial_number         csd_repairs.serial_number%type ,
32     repair_number            csd_repairs.repair_number%type,
33     ro_uom                   csd_repairs.unit_of_measure%type,
34     ro_item_id               csd_repairs.inventory_item_id%type ,
35     product_transaction_id   csd_product_transactions.product_transaction_id%type,
36     repair_line_id           csd_product_transactions.repair_line_id%type,
37     action_code              csd_product_transactions.action_code%type,
38     source_serial_number     csd_product_transactions.source_serial_number%type,
39     source_instance_id       csd_product_transactions.source_instance_id%type,
40     prod_txn_recd_qty        csd_product_transactions.quantity_received%type,
41     estimate_quantity        cs_estimate_details.quantity_required%type ,
42     est_order_line_id        cs_estimate_details.order_line_id%type ,
43     prod_txn_item_id         cs_estimate_details.inventory_item_id%type
44  );
45 
46 TYPE IO_RCPT_LINES_Rec_Type IS RECORD
47 (
48     product_transaction_id csd_product_transactions.PRODUCT_TRANSACTION_ID%type,
49     prod_txn_status        csd_product_transactions.PROD_TXN_STATUS%type,
50     repair_line_id         csd_product_transactions.REPAIR_LINE_ID%type,
51     order_header_id        csd_product_transactions.ORDER_HEADER_ID%type,
52     order_line_id          csd_product_transactions.ORDER_LINE_ID%type,
53     req_header_id          csd_product_transactions.REQ_HEADER_ID%type,
54     req_line_id            csd_product_transactions.REPAIR_LINE_ID%type,
55     prod_txn_rcvd_qty      csd_product_transactions.quantity_received%type,
56     ro_qty                 csd_repairs.quantity%type,
57     ro_rcvd_qty            csd_repairs.quantity_rcvd%type,
58     inventory_item_id      csd_repairs.inventory_item_id%type,
59     ro_uom                 csd_repairs.unit_of_measure%type,
60     requisition_number     po_requisition_headers_all.segment1%type,
61     ordered_quantity       oe_order_lines_all.ordered_quantity%type,
62 	order_number           oe_order_headers_all.order_number%type
63  );
64 
65 TYPE SHIP_LINES_Rec_Type IS RECORD
66 (
67     shipped_serial_num     wsh_serial_numbers.fm_serial_number%type,
68     lot_number             wsh_delivery_details.lot_number%type ,
69     revision               wsh_delivery_details.revision%type ,
70     subinv                 wsh_delivery_details.subinventory%type ,
71     requested_quantity     wsh_delivery_details.requested_quantity%type,
72     shipped_quantity       wsh_delivery_details.shipped_quantity%type,
73     delivery_detail_id     wsh_delivery_details.delivery_detail_id%type,
74     shipped_uom            wsh_delivery_details.requested_quantity_uom%type ,
75     inventory_item_id      wsh_delivery_details.inventory_item_id%type ,
76     organization_id        wsh_delivery_details.organization_id%type,
77     order_number           wsh_delivery_details.source_header_number%type ,
78     sales_order_header     wsh_delivery_details.source_header_id%type ,
79     locator_id             wsh_delivery_details.locator_id%type,
80     order_line_number      oe_order_lines_all.line_number%type ,
81     date_shipped           oe_order_lines_all.actual_shipment_date%type ,
82     line_id                oe_order_lines_all.line_id%type ,             --Bug#6779806
83     repair_number          csd_repairs.repair_number%type,
84     repair_line_id         csd_repairs.repair_line_id%type,
85     ro_uom                 csd_repairs.unit_of_measure%type ,
86     ro_item_id             csd_repairs.inventory_item_id%type ,
87     estimate_quantity      cs_estimate_details.quantity_required%type ,
88     prod_txn_serial_num    csd_product_transactions.source_serial_number%type ,
89     source_instance_id     csd_product_transactions.source_instance_id%type,
90     product_transaction_id csd_product_transactions.product_transaction_id%type,
91     action_code            csd_product_transactions.action_code%type,
92     delivery_name          wsh_new_deliveries.name%type ,
93     org_name               hr_all_organization_units.name%type
94 );
95 TYPE IO_SHIP_LINES_Rec_Type IS RECORD
96 (
97  header_id                    oe_order_lines_all.header_id%type,
98  line_id                      oe_order_lines_all.line_id%type,
99  ordered_quantity             oe_order_lines_all.ordered_quantity%type,
100  req_header_id                oe_order_lines_all.source_document_id%type ,
101  req_line_id                  oe_order_lines_all.source_document_line_id%type ,
102  req_number                   oe_order_lines_all.orig_sys_document_ref%type ,
103  inventory_item_id           oe_order_lines_all.inventory_item_id%type,
104  shipment_date                oe_order_lines_all.actual_shipment_date%type ,
105  delivery_detail_id          wsh_delivery_details.delivery_detail_id%type,
106  shipped_quantity             wsh_delivery_details.shipped_quantity%type,
107  del_line_serial_num          wsh_serial_numbers.fm_serial_number%type ,
108  lot_number                   wsh_delivery_details .lot_number%type,
109  subinventory                 wsh_delivery_details .subinventory%type,
110  locator_id                   wsh_delivery_details .locator_id%type,
111  organization_id              wsh_delivery_details .organization_id%type,
112  released_status              wsh_delivery_details .released_status%type,
113  requested_quantity           wsh_delivery_details .requested_quantity%type,
114  order_number                 wsh_delivery_details .source_header_number%type ,
115  source_organization_id       po_requisition_lines_all.source_organization_id%type,
116  source_subinventory          po_requisition_lines_all.source_subinventory%type,
117  destination_organization_id  po_requisition_lines_all.destination_organization_id%type,
118  destination_subinventory     po_requisition_lines_all.destination_subinventory%type,
119  serial_number_control_code   mtl_system_items.serial_number_control_code%type,
120  lot_control_code             mtl_system_items.lot_control_code%type,
121  requisition_number           po_requisition_headers_all.segment1%type ,
122  source_org_name              hr_all_organization_units.name%type ,
123  destination_org_name         hr_all_organization_units.name%type ,
124  txn_source_id                mtl_txn_request_lines.txn_source_id%type
125 );
126 
127 TYPE JOB_COMPLETION_Rec_Type IS RECORD
128 (
129     repair_job_xref_id         CSD_REPAIR_JOB_XREF.repair_job_xref_id%type,
130     wip_entity_id              CSD_REPAIR_JOB_XREF.wip_entity_id%type,
131     repair_line_id             CSD_REPAIR_JOB_XREF.repair_line_id%type,
132     allocated_comp_qty         CSD_REPAIR_JOB_XREF.quantity_completed%type ,
133     allocated_job_qty          CSD_REPAIR_JOB_XREF.quantity%type ,
134     organization_id            CSD_REPAIR_JOB_XREF.organization_id%type,
135     repair_number              CSD_REPAIRS.repair_number%type,
136     promise_date               CSD_REPAIRS.promise_date%type,
137     ro_serial_num              CSD_REPAIRS.serial_number%type ,
138     ro_item_id                 CSD_REPAIRS.inventory_item_id%type ,
139     job_completed_qty          WIP_DISCRETE_JOBS.quantity_completed%type ,
140     job_qty                    WIP_DISCRETE_JOBS.start_quantity%type ,
141     date_completed             WIP_DISCRETE_JOBS.date_completed%type,
142     job_item_id                WIP_DISCRETE_JOBS.primary_item_id%type,
143     wip_entity_name            WIP_ENTITIES.wip_entity_name%type,
144     serial_number_control_code MTL_SYSTEM_ITEMS.serial_number_control_code%type
145 
146 );
147 
148 TYPE JOB_CREATION_Rec_Type IS RECORD
149 (
150  repair_job_xref_id CSD_REPAIR_JOB_XREF.repair_job_xref_id%type,
151  repair_line_id     CSD_REPAIR_JOB_XREF.repair_line_id%type,
152  organization_id    CSD_REPAIR_JOB_XREF.organization_id%type,
153  allocated_job_qty  CSD_REPAIR_JOB_XREF.quantity%type ,
154  wip_entity_id      WIP_ENTITIES.wip_entity_id%type,
155  wip_entity_name    WIP_ENTITIES.wip_entity_name%type,
156  job_qty            WIP_DISCRETE_JOBS.start_quantity%type ,
157  creation_date      WIP_DISCRETE_JOBS.creation_date%type
158 );
159 
160 --------------------
161  PROCEDURE check_for_cancelled_order(p_repair_line_id NUMBER);
162 /*-------------------------------------------------------------------------------------*/
163 /* Function  name: DEBUG                                                               */
164 /* Description   : Logs the debug message                                              */
165 /* Called from   : Called from Update API                                              */
166 /*                                                                                     */
167 /* STANDARD PARAMETERS                                                                 */
168 /*   In Parameters :                                                                   */
169 /*      p_message        Required    Debug message that needs to be logged             */
170 /*      p_mod_name       Required    Module name                                       */
171 /*      p_severity_level Required    Severity level                                    */
172 /*   Output Parameters:                                                                */
173 /* NON-STANDARD PARAMETERS                                                             */
174 /*   In Parameters                                                                     */
175 /*   Out parameters                                                                    */
176 /* Change Hist :                                                                       */
177 /*   09/20/03  vlakaman  Initial Creation.                                             */
178 /*-------------------------------------------------------------------------------------*/
179 
180 Procedure DEBUG
181           (p_message  in varchar2,
182            p_mod_name in varchar2,
183            p_severity_level in number
184            ) IS
185 
186   -- Variables used in FND Log
187   l_stat_level   number   := FND_LOG.LEVEL_STATEMENT;
188   l_proc_level   number   := FND_LOG.LEVEL_PROCEDURE;
189   l_event_level  number   := FND_LOG.LEVEL_EVENT;
190   l_excep_level  number   := FND_LOG.LEVEL_EXCEPTION;
191   l_error_level  number   := FND_LOG.LEVEL_ERROR;
192   l_unexp_level  number   := FND_LOG.LEVEL_UNEXPECTED;
193 
194 BEGIN
195 
196   IF p_severity_level = 1 THEN
197     IF ( l_stat_level >= G_debug_level) THEN
198         FND_LOG.STRING(l_stat_level,p_mod_name,p_message);
199     END IF;
200   ELSIF p_severity_level = 2 THEN
201     IF ( l_proc_level >= G_debug_level) THEN
202         FND_LOG.STRING(l_proc_level,p_mod_name,p_message);
203     END IF;
204   ELSIF p_severity_level = 3 THEN
205     IF ( l_event_level >= G_debug_level) THEN
206         FND_LOG.STRING(l_event_level,p_mod_name,p_message);
207     END IF;
208   ELSIF p_severity_level = 4 THEN
209     IF ( l_excep_level >= G_debug_level) THEN
210         FND_LOG.STRING(l_excep_level,p_mod_name,p_message);
211     END IF;
212   ELSIF p_severity_level = 5 THEN
213     IF ( l_error_level >= G_debug_level) THEN
214         FND_LOG.STRING(l_error_level,p_mod_name,p_message);
215     END IF;
216   ELSIF p_severity_level = 6 THEN
217     IF ( l_unexp_level >= G_debug_level) THEN
218         FND_LOG.STRING(l_unexp_level,p_mod_name,p_message);
219     END IF;
220   END IF;
221 
222 END DEBUG;
223 
224 /*-------------------------------------------------------------------------------------*/
225 /* Function  name: INIT_ACTIVITY_REC                                                   */
226 /* Description   : Initialize the activity record                                      */
227 /* Called from   : Called from Update API                                              */
228 /*                                                                                     */
229 /* STANDARD PARAMETERS                                                                 */
230 /*   In Parameters :                                                                   */
231 /*   Output Parameters:                                                                */
232 /* NON-STANDARD PARAMETERS                                                             */
233 /*   In Parameters                                                                     */
234 /*   Out parameters                                                                    */
235 /* Change Hist :                                                                       */
236 /*   09/20/03  vlakaman  Initial Creation.                                             */
237 /*-------------------------------------------------------------------------------------*/
238 
239 Function INIT_ACTIVITY_REC RETURN csd_update_programs_pvt.activity_rec_type IS
240    l_activity_rec  activity_rec_type;
241 BEGIN
242       -- Initialize all the in the activity record
243       l_activity_rec.REPAIR_HISTORY_ID  := NULL;
244       l_activity_rec.REPAIR_LINE_ID     := NULL;
245       l_activity_rec.REQUEST_ID         := NULL;
246       l_activity_rec.PROGRAM_ID         := NULL;
247       l_activity_rec.PROGRAM_APPLICATION_ID  := NULL;
248       l_activity_rec.PROGRAM_UPDATE_DATE     := NULL;
249       l_activity_rec.EVENT_CODE         := NULL;
250       l_activity_rec.ACTION_CODE        := NULL;
251       l_activity_rec.EVENT_DATE         := NULL;
252       l_activity_rec.QUANTITY           := NULL;
253       l_activity_rec.PARAMN1            := NULL;
254       l_activity_rec.PARAMN2            := NULL;
255       l_activity_rec.PARAMN3            := NULL;
256       l_activity_rec.PARAMN4            := NULL;
257       l_activity_rec.PARAMN5            := NULL;
258       l_activity_rec.PARAMN6            := NULL;
259       l_activity_rec.PARAMN7            := NULL;
260       l_activity_rec.PARAMN8            := NULL;
261       l_activity_rec.PARAMN9            := NULL;
262       l_activity_rec.PARAMN10           := NULL;
263       l_activity_rec.PARAMC1            := NULL;
264       l_activity_rec.PARAMC2            := NULL;
265       l_activity_rec.PARAMC3            := NULL;
266       l_activity_rec.PARAMC4            := NULL;
267       l_activity_rec.PARAMC5            := NULL;
268       l_activity_rec.PARAMC6            := NULL;
269       l_activity_rec.PARAMC7            := NULL;
270       l_activity_rec.PARAMC8            := NULL;
271       l_activity_rec.PARAMC9            := NULL;
272       l_activity_rec.PARAMC10           := NULL;
273       l_activity_rec.PARAMD1            := NULL;
274       l_activity_rec.PARAMD2            := NULL;
275       l_activity_rec.PARAMD3            := NULL;
276       l_activity_rec.PARAMD4            := NULL;
277       l_activity_rec.PARAMD5            := NULL;
278       l_activity_rec.PARAMD6            := NULL;
279       l_activity_rec.PARAMD7            := NULL;
280       l_activity_rec.PARAMD8            := NULL;
281       l_activity_rec.PARAMD9            := NULL;
282       l_activity_rec.PARAMD10           := NULL;
283       l_activity_rec.ATTRIBUTE_CATEGORY := NULL;
284       l_activity_rec.ATTRIBUTE1         := NULL;
285       l_activity_rec.ATTRIBUTE2         := NULL;
286       l_activity_rec.ATTRIBUTE3         := NULL;
287       l_activity_rec.ATTRIBUTE4         := NULL;
288       l_activity_rec.ATTRIBUTE5         := NULL;
289       l_activity_rec.ATTRIBUTE6         := NULL;
290       l_activity_rec.ATTRIBUTE7         := NULL;
291       l_activity_rec.ATTRIBUTE8         := NULL;
292       l_activity_rec.ATTRIBUTE9         := NULL;
293       l_activity_rec.ATTRIBUTE10        := NULL;
294       l_activity_rec.ATTRIBUTE11        := NULL;
295       l_activity_rec.ATTRIBUTE12        := NULL;
296       l_activity_rec.ATTRIBUTE13        := NULL;
297       l_activity_rec.ATTRIBUTE14        := NULL;
298       l_activity_rec.ATTRIBUTE15        := NULL;
299       l_activity_rec.OBJECT_VERSION_NUMBER   := NULL;
300 
301   -- Return the initialized activity record
302   RETURN l_activity_rec;
303 END INIT_ACTIVITY_REC;
304 
305 /*-------------------------------------------------------------------------------------*/
306 /* Procedure name: Convert_to_RO_uom                                                   */
307 /* Description   : Procedure to convert the qty into Repair Order UOM                  */
308 /*                 Logistics lines could be created with different UOM than the one on */
309 /*                 repair order                                                        */
310 /* Called from   : Called from Update API (SO_RCV_UPDATE,SO_SHIP_UPDATE)               */
311 /*                                                                                     */
312 /* STANDARD PARAMETERS                                                                 */
313 /*   In Parameters :                                                                   */
314 /*                                                                                     */
315 /*   Output Parameters:                                                                */
316 /*     x_return_status     VARCHAR2      Return status of the API                      */
317 /*                                                                                     */
318 /* NON-STANDARD PARAMETERS                                                             */
319 /*   In Parameters                                                                     */
320 /*     p_to_uom          VARCHAR2 Required   RO Unit of measure                        */
321 /*     p_item_id         NUMBER   Required   Inventory Item Id                         */
322 /*     p_from_uom        VARCHAR2 Conditionaly Required Needed for receiving lines     */
323 /*     p_from_uom_code   VARCHAR2 Conditionaly Required Needed for shipping lines      */
324 /*     p_from_quantity   NUMBER   Required   Transaction quantity                      */
325 /*   Out parameters                                                                    */
326 /*     x_result_quantity   NUMBER        converted qty in Repair Order UOM             */
327 /*                                                                                     */
328 /* Change Hist :                                                                       */
329 /*   09/20/03  vlakaman  Initial Creation.                                             */
330 /*-------------------------------------------------------------------------------------*/
331 
332 Procedure CONVERT_TO_RO_UOM
333           (x_return_status   OUT NOCOPY  VARCHAR2,
334            p_to_uom_code     IN varchar2,
335            p_item_id         IN NUMBER,
336            p_from_uom        IN varchar2,
337            p_from_uom_code   in varchar2,
338            p_from_quantity   IN number,
339            x_result_quantity OUT NOCOPY number
340            ) IS
341 
342   -- Standard variables
343   l_api_name         CONSTANT VARCHAR2(30)   := 'CONVERT_TO_RO_UOM';
344   l_api_version      CONSTANT NUMBER         := 1.0;
345 
346   -- Variables used in the API
347   l_from_uom_code    mtl_units_of_measure.uom_code%type;
348 
349   -- Variables used in FND Log
350   l_error_level  number   := FND_LOG.LEVEL_ERROR;
351   l_mod_name     varchar2(2000) := 'csd.plsql.csd_update_programs_pvt.convert_to_ro_uom';
352 
353 BEGIN
354     -- Standard Start of API savepoint
355     SAVEPOINT  CONV_TO_RO_UOM;
356 
357     -- Initialize API return status to success
358     x_return_status := FND_API.G_RET_STS_SUCCESS;
359 
360     -- Api body starts
361     Debug('At the Beginning of Convert_to_RO_uom',l_mod_name,1);
362 
363     -- Derive the uom code from uom name
364     IF (NVL(p_from_uom_code, 'ZZZ') = 'ZZZ') THEN
365       Begin
366         select uom_code
367         into l_from_uom_code
368         from mtl_units_of_measure
369         where unit_of_measure = p_from_uom;
370       Exception
371         WHEN NO_DATA_FOUND THEN
372           IF ( l_error_level >= G_debug_level) THEN
373              fnd_message.set_name('CSD','CSD_UOM_MISSING');
374              fnd_message.set_token('UOM',p_from_uom);
375              FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
376           ELSE
377              fnd_message.set_name('CSD','CSD_UOM_MISSING');
378              fnd_message.set_token('UOM',p_from_uom);
379              fnd_msg_pub.add;
380           END IF;
381           RAISE FND_API.G_EXC_ERROR;
382         WHEN TOO_MANY_ROWS THEN
383           IF ( l_error_level >= G_debug_level) THEN
384              fnd_message.set_name('CSD','CSD_MANY_UOM_FOUND');
385              fnd_message.set_token('UOM',p_from_uom);
386              FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
387           ELSE
388              fnd_message.set_name('CSD','CSD_MANY_UOM_FOUND');
389              fnd_message.set_token('UOM',p_from_uom);
390              fnd_msg_pub.add;
391           END IF;
392           RAISE FND_API.G_EXC_ERROR;
393       End;
394    Else
395        -- assign the uom code if it is passed to the api
396        l_from_uom_code := p_from_uom_code;
397    END IF;
398 
399     -- Call the inventory api to convert the quantity
400     -- from one UOM to another UOM.
401     -- Only if the repair order UOM is different than the
402     -- l_from_uom_code, the inv api is called
403    IF p_to_uom_code <> l_from_uom_code THEN
404         x_result_quantity := inv_convert.inv_um_convert(
405                                      p_item_id ,
406                                      2,
407                                      p_from_quantity,
408                                      l_from_uom_code,
409                                      p_to_uom_code,
410                                      null,
411                                      null);
412 
413        -- if the inv_convert api fails then it returns -99999
414        IF  x_result_quantity < 0 then
415           IF ( l_error_level >= G_debug_level) THEN
416              fnd_message.set_name('CSD','CSD_INV_UM_CONV_FAILED');
417              FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
418           ELSE
419              fnd_message.set_name('CSD','CSD_INV_UM_CONV_FAILED');
420              fnd_msg_pub.add;
421           END IF;
422           RAISE FND_API.G_EXC_ERROR;
423        END IF;
424    ELSE
425        x_result_quantity := p_from_quantity;
426    END IF;
427 
428 EXCEPTION
429     WHEN FND_API.G_EXC_ERROR THEN
430           Debug('In FND_API.G_EXC_ERROR exception',l_mod_name,4);
431           ROLLBACK TO CONV_TO_RO_UOM;
432           x_return_status := FND_API.G_RET_STS_ERROR ;
433     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
434           Debug('In FND_API.G_EXC_UNEXPECTED_ERROR exception',l_mod_name,4);
435           ROLLBACK TO CONV_TO_RO_UOM;
436           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
437     WHEN OTHERS THEN
438           Debug('In OTHERS exception',l_mod_name,4);
439           ROLLBACK TO CONV_TO_RO_UOM;
440           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
441 End CONVERT_TO_RO_UOM;
442 
443 /*-------------------------------------------------------------------------------------*/
444 /* Procedure name: LOG_ACTIVITY                                                        */
445 /* Description   : Procedure called for logging activity                               */
446 /*                                                                                     */
447 /* Called from   : Called from all the api                                             */
448 /* STANDARD PARAMETERS                                                                 */
449 /*  In Parameters :                                                                    */
450 /*    p_api_version       NUMBER    Required  Api Version number                       */
451 /*    p_init_msg_list     VARCHAR2  Optional  To Initialize message stack              */
452 /*    p_commit            VARCHAR2  Optional  Commits in API                           */
453 /*    p_validation_level  NUMBER    Optional  validation level                         */
454 /*                                                                                     */
455 /*  Output Parameters:                                                                 */
456 /*   x_return_status     VARCHAR2      Return status of the API                        */
457 /*   x_msg_count         NUMBER        Number of messages in stack                     */
458 /*   x_msg_data          VARCHAR2      Error Message from message stack                */
459 /*                                                                                     */
460 /* NON-STANDARD PARAMETERS                                                             */
461 /*   In Parameters                                                                     */
462 /*    p_activity_rec    RECORD TYPE   Activity record type                             */
463 /* Output Parameter :                                                                  */
464 /* Change Hist :                                                                       */
465 /*   09/20/03  vlakaman  Initial Creation.                                             */
466 /*-------------------------------------------------------------------------------------*/
467 
468 Procedure LOG_ACTIVITY
469         ( p_api_version          IN   NUMBER,
470           p_commit               IN   VARCHAR2,
471           p_init_msg_list        IN   VARCHAR2,
472           p_validation_level     IN   NUMBER,
473           x_return_status        OUT  NOCOPY  VARCHAR2,
474           x_msg_count            OUT  NOCOPY  NUMBER,
475           x_msg_data             OUT  NOCOPY  VARCHAR2,
476           p_activity_rec         IN   activity_rec_type
477          ) IS
478 
479   -- Standard Variables
480   l_api_name          CONSTANT VARCHAR2(30)   := 'LOG_ACTIVITY';
481   l_api_version       CONSTANT NUMBER         := 1.0;
482 
483   -- Variables used in API
484   l_rep_hist_id       NUMBER;
485 
486   -- Variable used in FND log
487   l_error_level  number   := FND_LOG.LEVEL_ERROR;
488   l_mod_name     varchar2(2000) := 'csd.plsql.csd_update_programs_pvt.log_activity';
489 
490 BEGIN
491    -- Standard Start of API savepoint
492    SAVEPOINT  LOG_ACTIVITY;
493 
494    -- Initialize API return status to success
495    x_return_status := FND_API.G_RET_STS_SUCCESS;
496 
497    -- Debug messages
498    Debug('At the beginning of log_activity api',l_mod_name,1);
499 
500    -- Standard call to check for call compatibility.
501    IF NOT FND_API.Compatible_API_Call (l_api_version,
502                                        p_api_version,
503                                        l_api_name   ,
504                                        G_PKG_NAME   )
505        THEN
506           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
507    END IF;
508 
509    -- Initialize message list if p_init_msg_list is set to TRUE.
510    IF FND_API.to_Boolean( p_init_msg_list ) THEN
511          FND_MSG_PUB.initialize;
512    END IF;
513 
514    -- Debug messages
515    Debug('p_EVENT_CODE ='||p_activity_rec.EVENT_CODE,l_mod_name,1);
516    Debug('p_activity_rec.paramn1 ='||p_activity_rec.paramn1,l_mod_name,1);
517    Debug('p_activity_rec.paramn2 ='||p_activity_rec.paramn2,l_mod_name,1);
518 
519    -- need not validate the values passed in activity record
520    -- as these values are validated by the calling API
521    -- If this API is used by other programs, then we need to
522    -- validate the values passed in the activity record
523 
524    -- Debug messages
525    Debug('Calling Validate_And_Write',l_mod_name,2);
526 
527    -- Since the repair history Id is in/out parameter
528    -- I need to define a variable and pass the variable to API
529    l_rep_hist_id := p_activity_rec.REPAIR_HISTORY_ID;
530 
531     -- Calling Validate_And_Write to log activity
532     -- Except for the who columns, all the values from
533     -- p_activity_rec are passed to the Validate_And_Write api.
534     CSD_TO_FORM_REPAIR_HISTORY.Validate_And_Write (
535                                 P_Api_Version_Number      => p_api_version,
536                                 P_Init_Msg_List           => p_init_msg_list,
537                                 P_Commit                  => p_commit,
538                                 p_validation_level        => null, --p_validation_level,
539                                 p_action_code             => p_activity_rec.action_code,
540                                 px_REPAIR_HISTORY_ID      => l_rep_hist_id,
541                                 p_OBJECT_VERSION_NUMBER   => p_activity_rec.object_version_number,
542                                 p_REQUEST_ID              => p_activity_rec.request_id,
543                                 p_PROGRAM_ID              => p_activity_rec.program_id,
544                                 p_PROGRAM_APPLICATION_ID  => p_activity_rec.program_application_id,
545                                 p_PROGRAM_UPDATE_DATE     => p_activity_rec.program_update_date,
546                                 p_CREATED_BY              => fnd_global.user_id,
547                                 p_CREATION_DATE           => sysdate,
548                                 p_LAST_UPDATED_BY         => fnd_global.user_id,
549                                 p_LAST_UPDATE_DATE        => sysdate,
550                                 p_REPAIR_LINE_ID          => p_activity_rec.repair_line_id,
551                                 p_EVENT_CODE  => p_activity_rec.event_code,
552                                 p_EVENT_DATE  => p_activity_rec.event_date,
553                                 p_QUANTITY    => p_activity_rec.quantity,
554                                 p_PARAMN1     => p_activity_rec.paramn1,
555                                 p_PARAMN2     => p_activity_rec.paramn2,
556                                 p_PARAMN3     => p_activity_rec.paramn3,
557                                 p_PARAMN4     => p_activity_rec.paramn4,
558                                 p_PARAMN5     => p_activity_rec.paramn5,
559                                 p_PARAMN6     => p_activity_rec.paramn6,
560                                 p_PARAMN7     => p_activity_rec.paramn7,
561                                 p_PARAMN8     => p_activity_rec.paramn8,
562                                 p_PARAMN9     => p_activity_rec.paramn9,
563                                 p_PARAMN10    => p_activity_rec.paramn10,
564                                 p_PARAMC1     => p_activity_rec.paramc1,
565                                 p_PARAMC2     => p_activity_rec.paramc2,
566                                 p_PARAMC3     => p_activity_rec.paramc3,
567                                 p_PARAMC4     => p_activity_rec.paramc4,
568                                 p_PARAMC5     => p_activity_rec.paramc5,
569                                 p_PARAMC6     => p_activity_rec.paramc6,
570                                 p_PARAMC7     => p_activity_rec.paramc7,
571                                 p_PARAMC8     => p_activity_rec.paramc8,
572                                 p_PARAMC9     => p_activity_rec.paramc9,
573                                 p_PARAMC10    => p_activity_rec.paramc10,
574                                 p_PARAMD1     => p_activity_rec.paramd1,
575                                 p_PARAMD2     => p_activity_rec.paramd2,
576                                 p_PARAMD3     => p_activity_rec.paramd3,
577                                 p_PARAMD4     => p_activity_rec.paramd4,
578                                 p_PARAMD5     => p_activity_rec.paramd5,
579                                 p_PARAMD6     => p_activity_rec.paramd6,
580                                 p_PARAMD7     => p_activity_rec.paramd7,
581                                 p_PARAMD8     => p_activity_rec.paramd8,
582                                 p_PARAMD9     => p_activity_rec.paramd9,
583                                 p_PARAMD10    => p_activity_rec.paramd10,
584                                 p_ATTRIBUTE_CATEGORY  => p_activity_rec.attribute_category,
585                                 p_ATTRIBUTE1    => p_activity_rec.attribute1,
586                                 p_ATTRIBUTE2    => p_activity_rec.attribute2,
587                                 p_ATTRIBUTE3    => p_activity_rec.attribute3,
588                                 p_ATTRIBUTE4    => p_activity_rec.attribute4,
589                                 p_ATTRIBUTE5    => p_activity_rec.attribute5,
590                                 p_ATTRIBUTE6    => p_activity_rec.attribute6,
591                                 p_ATTRIBUTE7    => p_activity_rec.attribute7,
592                                 p_ATTRIBUTE8    => p_activity_rec.attribute8,
593                                 p_ATTRIBUTE9    => p_activity_rec.attribute9,
594                                 p_ATTRIBUTE10   => p_activity_rec.attribute10,
595                                 p_ATTRIBUTE11   => p_activity_rec.attribute11,
596                                 p_ATTRIBUTE12   => p_activity_rec.attribute12,
597                                 p_ATTRIBUTE13   => p_activity_rec.attribute13,
598                                 p_ATTRIBUTE14   => p_activity_rec.attribute14,
599                                 p_ATTRIBUTE15   => p_activity_rec.attribute15,
600                                 p_LAST_UPDATE_LOGIN => null,
601                                 X_Return_Status => x_return_status,
602                                 X_Msg_Count     => x_msg_count,
603                                 X_Msg_Data      => x_msg_data  );
604 
605    -- Debug messages
606    Debug('x_return_status from Validate_And_Write ='||x_return_status,l_mod_name,2);
607 
608    IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
609         Debug('Validate_And_Write api failed ',l_mod_name,4);
610         RAISE FND_API.G_EXC_ERROR;
611    END IF;
612 
613    -- Standard check of p_commit.
614    IF FND_API.To_Boolean( p_commit ) THEN
615        COMMIT WORK;
616    END IF;
617 
618    -- Standard call to get message count and IF count is  get message info.
619    FND_MSG_PUB.Count_And_Get
620            (p_count  =>  x_msg_count,
621             p_data   =>  x_msg_data );
622  EXCEPTION
623     WHEN FND_API.G_EXC_ERROR THEN
624           Debug('In FND_API.G_EXC_ERROR exception',l_mod_name,4);
625           ROLLBACK TO  LOG_ACTIVITY;
626           x_return_status := FND_API.G_RET_STS_ERROR ;
627           FND_MSG_PUB.Count_And_Get
628                 (p_count  =>  x_msg_count,
629                  p_data   =>  x_msg_data  );
630     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
631           Debug('In FND_API.G_EXC_UNEXPECTED_ERROR exception',l_mod_name,4);
632           ROLLBACK TO  LOG_ACTIVITY;
633           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
634           FND_MSG_PUB.Count_And_Get
635                 ( p_count  =>  x_msg_count,
636                   p_data   =>  x_msg_data );
637     WHEN OTHERS THEN
638           Debug('In OTHERS exception',l_mod_name,4 );
639           ROLLBACK TO  LOG_ACTIVITY;
640           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
641           IF  FND_MSG_PUB.Check_Msg_Level
642                   (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
643           THEN
644               FND_MSG_PUB.Add_Exc_Msg
645                   (G_PKG_NAME ,
646                    l_api_name  );
647           END IF;
648           FND_MSG_PUB.Count_And_Get
649                   (p_count  =>  x_msg_count,
650                    p_data   =>  x_msg_data );
651 END  LOG_ACTIVITY;
652 
653 /*-------------------------------------------------------------------------------------*/
654 /* Procedure name: JOB_COMPLETION_UPDATE                                               */
655 /* Description   : Procedure called from wip_update API to update the completed qty    */
656 /*                 It also logs activity for the job completion                        */
657 /* Called from   : Called from WIP_Update API                                          */
658 /* STANDARD PARAMETERS                                                                 */
659 /*  In Parameters :                                                                    */
660 /*    p_api_version       NUMBER    Required  Api Version number                       */
661 /*    p_init_msg_list     VARCHAR2  Optional  To Initialize message stack              */
662 /*    p_commit            VARCHAR2  Optional  Commits in API                           */
663 /*    p_validation_level  NUMBER    Optional  validation level                         */
664 /*                                                                                     */
665 /*  Output Parameters:                                                                 */
666 /*   x_return_status     VARCHAR2      Return status of the API                        */
667 /*   x_msg_count         NUMBER        Number of messages in stack                     */
668 /*   x_msg_data          VARCHAR2      Error Message from message stack                */
669 /*                                                                                     */
670 /* NON-STANDARD PARAMETERS                                                             */
671 /*   In Parameters                                                                     */
672 /*    p_repair_line_id   NUMBER   Optional   Repair Order Line Id                      */
673 /* Output Parm :                                                                       */
674 /* Change Hist :                                                                       */
675 /*   09/20/03  vlakaman  Initial Creation.                                             */
676 /*-------------------------------------------------------------------------------------*/
677 
678 Procedure  JOB_COMPLETION_UPDATE
679         ( p_api_version          IN   NUMBER,
680           p_commit               IN   VARCHAR2,
681           p_init_msg_list        IN   VARCHAR2,
682           p_validation_level     IN   NUMBER,
683           x_return_status        OUT  NOCOPY  VARCHAR2,
684           x_msg_count            OUT  NOCOPY  NUMBER,
685           x_msg_data             OUT  NOCOPY  VARCHAR2,
686           p_repair_line_id       IN   NUMBER
687 	  ) IS
688 
689   -- Standard Variables
690   l_api_name          CONSTANT VARCHAR2(30)   := 'WIP_UPDATE';
691   l_api_version       CONSTANT NUMBER         := 1.0;
692 
693   -- Variables used in the program
694   l_rep_hist_id       NUMBER;
695   l_total_rec         NUMBER:= 0;
696   l_completed_qty     NUMBER;
697   l_remaining_qty     NUMBER;
698   l_total_qty         NUMBER;
699   l_update_qty        NUMBER;
700   l_count             NUMBER;
701   l_dummy             varchar2(30);
702   l_commit_size       NUMBER := 500;
703   l_msg_text          VARCHAR2(2000);
704   l_mtl_trx_id        NUMBER;
705   l_mtl_trx_date      DATE;
706   l_mtl_serial_num    mtl_unit_transactions.serial_number%type;
707   l_mtl_subinv        mtl_material_transactions.subinventory_code%type;
708   l_SN_mismatch       BOOLEAN := FALSE;
709 
710   l_ro_count NUMBER ; ---Added by vijay to put ro count in JC event
711 
712   -- Define a record of activity_rec_type
713   l_activity_rec      activity_rec_type;
714 
715   -- Variable used in FND log
716   l_error_level  number   := FND_LOG.LEVEL_ERROR;
717   l_mod_name     varchar2(2000) := 'csd.plsql.csd_update_programs_pvt.job_completion_update';
718 
719   -- User defined exception
720   SKIP_RECORD   EXCEPTION;
721 
722    -- This cursor gets all the records in csd_repair_job_xref for which
723    -- the completed qty is less than the allocated
724    -- for the given repair line id,  11/9/04: Vijay
725    Cursor REPAIR_JOBS( p_rep_line_id in number )IS
726     SELECT
727       crj.repair_job_xref_id,
728       crj.wip_entity_id,
729       crj.repair_line_id,
730       crj.quantity_completed allocated_comp_qty,
731       crj.quantity allocated_job_qty,
732       crj.organization_id,
733       cra.repair_number,
734       cra.promise_date,
735       cra.serial_number ro_serial_num,
736       cra.inventory_item_id ro_item_id,
737       wdj.quantity_completed job_completed_qty,
738       wdj.start_quantity job_qty,
739       wdj.date_completed,
740       wdj.primary_item_id job_item_id,
741       we.wip_entity_name,
742       mtl.serial_number_control_code
743     from CSD_REPAIR_JOB_XREF crj,
744          CSD_REPAIRS cra,
745          WIP_DISCRETE_JOBS wdj,
746          WIP_ENTITIES we,
747          MTL_SYSTEM_ITEMS mtl
748     where wdj.wip_entity_id  = crj.wip_entity_id
749      and  we.wip_entity_id   = wdj.wip_entity_id
750      and  crj.repair_line_id = cra.repair_line_id
751      and  crj.organization_id = mtl.organization_id
752      and  crj.inventory_item_id = mtl.inventory_item_id
753      and  (crj.quantity - nvl(crj.quantity_completed,0)) > 0
754      and   cra.repair_line_id  = p_rep_line_id
755      and  nvl(wdj.quantity_completed,0) > 0
756      order by crj.wip_entity_id, cra.promise_date;
757 
758    -- This cursor gets all the records in csd_repair_job_xref for which
759    -- the completed qty is less than the allocated
760    -- all records irrespective of rep_line_id 11/9/04: Vijay
761    Cursor REPAIR_JOBS_ALL IS
762     SELECT
763       crj.repair_job_xref_id,
764       crj.wip_entity_id,
765       crj.repair_line_id,
766       crj.quantity_completed allocated_comp_qty,
767       crj.quantity allocated_job_qty,
768       crj.organization_id,
769       cra.repair_number,
770       cra.promise_date,
771       cra.serial_number ro_serial_num,
772       cra.inventory_item_id ro_item_id,
773       wdj.quantity_completed job_completed_qty,
774       wdj.start_quantity job_qty,
775       wdj.date_completed,
776       wdj.primary_item_id job_item_id,
777       we.wip_entity_name,
778       mtl.serial_number_control_code
779     from CSD_REPAIR_JOB_XREF crj,
780          CSD_REPAIRS cra,
781          WIP_DISCRETE_JOBS wdj,
782          WIP_ENTITIES we,
783          MTL_SYSTEM_ITEMS mtl
784     where wdj.wip_entity_id  = crj.wip_entity_id
785      and  we.wip_entity_id   = wdj.wip_entity_id
786      and  crj.repair_line_id = cra.repair_line_id
787      and  crj.organization_id = mtl.organization_id
788      and  crj.inventory_item_id = mtl.inventory_item_id
789      and  (crj.quantity - nvl(crj.quantity_completed,0)) > 0
790      and  nvl(wdj.quantity_completed,0) > 0
791      order by crj.wip_entity_id, cra.promise_date;
792 
793    -- Added by Vijay 11/9/04
794    JOB JOB_COMPLETION_REC_TYPE;
795 
796 
797    -- This cursor gets all the material txns for the wip_entity_id
798    -- WIP Completion Txn Types has transaction_action_id=31
799    -- and transaction_source_type_id = 5
800    Cursor Get_mtl_txns (p_entity_id in number) IS
801     select subinventory_code,
802            transaction_quantity,
803            transaction_id,
804            transaction_date
805     from  mtl_material_transactions mtl
806     where mtl.transaction_source_id      = p_entity_id
807      and  mtl.transaction_source_type_id = 5  -- Job or Schedule
808      and  mtl.transaction_action_id      = 31;-- Wip Assembly Completion
809 
810 
811 BEGIN
812    -- Standard Start of API savepoint
813    SAVEPOINT  JOB_COMPLETION_UPDATE;
814 
815    -- Initialize API return status to success
816    x_return_status := FND_API.G_RET_STS_SUCCESS;
817 
818    -- Debug messages
819    Debug('At the Beginning of JOB_COMPLETION_UPDATE',l_mod_name,1);
820 
821    -- Standard call to check for call compatibility.
822    IF NOT FND_API.Compatible_API_Call (l_api_version,
823                                        p_api_version,
824                                        l_api_name   ,
825                                        G_PKG_NAME   )
826        THEN
827           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
828    END IF;
829 
830    -- Initialize message list if p_init_msg_list is set to TRUE.
831    IF FND_API.to_Boolean( p_init_msg_list ) THEN
832          FND_MSG_PUB.initialize;
833    END IF;
834 
835    -- Keep the record count
836    l_count := 0;
837 
838    -- Code does not handle the following scenarios
839    --   1. Cancelled WIP Job
840    --        Eric is going to check why a Job is cancelled. Based on that
841    --        a course of action will be taken
842    --   2. Overcompletion
843    --        It has decided to ignore the overcompleted quantity
844    --   3. Return to WIP Job after completion
845    --        It needs to be handled asap
846    -- It will be discussed with PM and done as phase II project
847 
848    -- Get all the records that have job completed
849    --FOR JOB in Repair_Jobs( p_repair_line_id )
850    IF(p_repair_line_Id is null) THEN
851      OPEN Repair_Jobs_ALL;
852    ELSE
853      OPEN Repair_Jobs(p_repair_line_id);
854    END IF;
855 
856    LOOP
857     BEGIN
858 
859       IF(p_repair_line_Id is null) THEN
860         FETCH  Repair_Jobs_ALL INTO Job;
861         EXIT WHEN Repair_Jobs_ALL%NOTFOUND;
862       ELSE
863         FETCH Repair_Jobs INTO Job;
864         EXIT WHEN Repair_Jobs%NOTFOUND;
865       END IF;
866       -- savepoint
867       SAVEPOINT  Job_Completion;
868 
869           -- Debug messages
870        Debug('wip_entity_id      ='||TO_CHAR(job.wip_entity_id),l_mod_name,1);
871   	   Debug('repair_line_id     ='||TO_CHAR(job.repair_line_id),l_mod_name,1);
872        Debug('quantity_completed ='||TO_CHAR(job.job_completed_qty),l_mod_name,1);
873 	   Debug('quantity           ='||TO_CHAR(job.job_qty),l_mod_name,1);
874 
875 	   --Get the sum of completed quantity from csd_repair_job_xref
876 	   select nvl(sum(quantity_completed),0), count(repair_line_id) ---Added by vijay
877          into l_completed_qty, l_ro_count   ----------------------------to put ro count in JC event
878          from csd_repair_job_xref
879          where wip_entity_id = JOB.wip_entity_id;
880 
881          l_ro_count := nvl(l_ro_Count,0); --Added by vijay to put ro count in JC event
882 
883          -- Calculate the remaining qty to be processed
884          -- Using NVL in SQL statement is much expensive than in a variable
885    	    l_remaining_qty := nvl(JOB.job_completed_qty,0) - l_completed_qty;
886 
887          -- Debug messages
888          Debug('l_remaining_qty    ='||TO_CHAR(l_remaining_qty),l_mod_name,1);
889    	    Debug('l_completed_qty    ='||TO_CHAR(l_completed_qty),l_mod_name,1);
890 
891          -- Using NVL in SQL statement is much expensive than in a variable
892          IF ((nvl(JOB.allocated_job_qty,0) - nvl(JOB.allocated_comp_qty,0)) <= l_remaining_qty) then
893 	         l_remaining_qty := (nvl(JOB.allocated_job_qty,0) - nvl(JOB.allocated_comp_qty,0)) ;
894          END IF;
895 
896           -- Debug messages
897          Debug('l_remaining_qty        ='||TO_CHAR(l_remaining_qty),l_mod_name,1);
898          Debug('Serial Num Control Code='||JOB.serial_number_control_code,l_mod_name,1);
899 
900           -- Only if the remaining qty > 0 then process the record
901           -- It needs to be processed differently depending on the item definition
902           --   1. Serialized at inventory receipt and Pre-defined
903           --   2. Non-Serialized and Serial Control at sales order issue
904           --
905           -- If the item is pre-defined or at inventory receipt, the serial number
906           -- is entered while completing the job. The serial number is stored in
907           -- mtl_unit_transactions table. So the serial number can be matched to
908           -- the serial number on repair order and the repair order is updated
909           -- with the completed qty. But if the wip job is for upgrade, then the
910           -- repair order item is different then the item on the WIP job. So if
911           -- it is upgrade then the completion qty is updated based on the
912           -- promise date
913           --
914           -- If the item is non-serialized and serialized at SO issue then
915           -- the completion qty is updated based on the promise date
916 
917          IF l_remaining_qty > 0 then
918             IF JOB.serial_number_control_code in (2,5) THEN
919                 -- Serialized at inventory receipt and Pre-defined
920                 -- In WIP_DISCRETE_JOBS
921                 --   Job Name    Job Id      Start Qty   Completion Qty   Completion Subinv
922                 --    W1          1121         10             10
923                 --
924                 --  MTL_MATERIAL_TRANSACTIONS
925                 --    Txn Id   Job ID   Txn Qty    Completion Subinv
926                 --      M1      1121      2           FGI
927                 --      M2      1121      8           Stores
928                 --
929                 -- MTL_UNIT_TRANSACTIONS
930                 --
931                 --   Txn Id  Serial Number   Subinv
932                 --     M1       SN1           FGI
933                 --     M2       SN2           FGI
934                 --     .         .
935                 --     M10      SN10          Stores
936                 -- The wip job is created for the same item on the repair order
937 			 -- It is not an upgrade
938                 IF JOB.ro_item_id = JOB.job_item_id THEN
939                    BEGIN
940                      Select mt.transaction_id,
941                             mt.transaction_date,
942                             mt.subinventory_code
943                      into   l_mtl_trx_id,
944                             l_mtl_trx_date,
945                             l_mtl_subinv
946                      from  mtl_material_transactions mt,
947                            mtl_unit_transactions mut
948                      where mt.transaction_id = mut.transaction_id
949                       and  mt.transaction_source_id      = JOB.wip_entity_id
950                       and  mt.transaction_source_type_id = 5  -- Job or Schedule
951                       and  mt.transaction_action_id      = 31 -- Wip Assembly Completion
952                       and  mut.serial_number             = JOB.ro_serial_num
953                       and  mt.inventory_item_id          = JOB.ro_item_id;
954 
955                         -- Update csd_repair_job_xref with qty completed
956                         update csd_repair_job_xref
957                         set quantity_completed = nvl(quantity_completed,0) + 1,
958 		                  object_version_number = object_version_number+1,
959                             last_update_date   = sysdate,
960                             last_updated_by    = fnd_global.user_id,
961                             last_update_login  = fnd_global.login_id
962                         where repair_job_xref_id = JOB.repair_job_xref_id;
963                         IF SQL%NOTFOUND THEN
964                             IF ( l_error_level >= G_debug_level)  THEN
965                                FND_MESSAGE.SET_NAME('CSD','CSD_JOB_XREF_UPD_FAILED');
966                                FND_MESSAGE.SET_TOKEN('REP_JOB_XREF_ID',JOB.repair_job_xref_id );
967                                FND_LOG.MESSAGE(l_error_level,l_mod_name,FALSE);
968                             ELSE
969                                FND_MESSAGE.SET_NAME('CSD','CSD_JOB_XREF_UPD_FAILED');
970                                FND_MESSAGE.SET_TOKEN('REP_JOB_XREF_ID',JOB.repair_job_xref_id );
971                                FND_MSG_PUB.ADD;
972                             END IF;
973                             RAISE FND_API.G_EXC_ERROR;
974                         End IF;
975 
976                         -- Initialize the activity rec
977                         l_activity_rec := INIT_ACTIVITY_REC ;
978 
979                          -- Assign the values for activity record
980                         l_activity_rec.REPAIR_HISTORY_ID := l_rep_hist_id;
981                         l_activity_rec.REPAIR_LINE_ID :=  JOB.repair_line_id;
982                         l_activity_rec.EVENT_CODE     :=  'JC';
983                         l_activity_rec.ACTION_CODE    :=  0;
984                         l_activity_rec.EVENT_DATE     :=  l_mtl_trx_date;
985                         l_activity_rec.QUANTITY       :=  JOB.job_completed_qty;
986                         l_activity_rec.PARAMN1        :=  JOB.organization_id;
987                         l_activity_rec.PARAMN3        :=  l_mtl_trx_id;
988                         l_activity_rec.PARAMN4        :=  JOB.wip_entity_id;
989                         l_activity_rec.PARAMN5        :=  1;
990                         l_activity_rec.PARAMN6        :=  l_ro_count;
991                         l_activity_rec.PARAMC1        :=  l_mtl_subinv;
992                         l_activity_rec.PARAMC2        :=  JOB.wip_entity_name;
993                         l_activity_rec.PARAMC3        :=  JOB.ro_serial_num;
994                         l_activity_rec.OBJECT_VERSION_NUMBER := null;
995 
996                         Debug('Calling LOG_ACTIVITY',l_mod_name,2);
997 
998                         -- Calling LOG_ACTIVITY for logging activity
999                         -- for job completion
1000                         LOG_ACTIVITY
1001                             ( p_api_version     => p_api_version,
1002                               p_commit          => p_commit,
1003                               p_init_msg_list   => p_init_msg_list,
1004                               p_validation_level => p_validation_level,
1005                               x_return_status   => x_return_status,
1006                               x_msg_count       => x_msg_count,
1007                               x_msg_data        => x_msg_data,
1008                               p_activity_rec    => l_activity_rec );
1009 
1010                        Debug('x_return_status from LOG_ACTIVITY ='||x_return_status,l_mod_name,2);
1011                        IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
1012                           Debug('LOG_ACTIVITY api failed ',l_mod_name,4);
1013                           RAISE FND_API.G_EXC_ERROR;
1014                        END IF;
1015                         l_SN_mismatch := FALSE;
1016                    EXCEPTION
1017                      WHEN NO_DATA_FOUND THEN
1018                         l_SN_mismatch := TRUE;
1019                    END;
1020 
1021                    -- While issuing the assembly to WIP Job, user could issue
1022 			    -- a wrong serial number. So that could result in mismatch
1023 			    -- of serial number
1024 			    -- Repair Job XRef
1025 			    -- RO Num  Item   Qty  Job  Serial Num
1026 			    -- R1       A      1    W1     Sn1
1027 			    -- R2       A      1    W1     Sn2
1028 			    -- R3       A      1    W1     Sn3
1029 			    --
1030 			    -- Wip Discrete Jobs
1031 			    --  Job  Start Qty  Comp Qty
1032 			    --   W1      3         3
1033 			    --
1034 			    -- MTL Material Transactions
1035 			    -- Mtl Txn Id   Txn Qty   SubInv
1036 			    --   M1           2        FGI
1037 			    --   M2           1        Stores
1038 			    -- MTL Unit Txns
1039 			    -- Mtl Txn Id    Serial Num
1040 			    --   M1            Sn2
1041 			    --   M1            Sn9
1042 			    --   M2            Sn11
1043 			    -- It needs to be matched in the following manner
1044 			    -- RO Num  RO Serial Num   Comp Serial Num
1045 			    -- R1            Sn1         Sn9
1046 			    -- R2            Sn2         Sn2
1047 			    -- R3            Sn3         Sn11
1048                    IF l_SN_mismatch THEN
1049                       BEGIN
1050 					Select mt.transaction_id,
1051                                 mt.transaction_date,
1052                                 mut.serial_number,
1053                                 mt.subinventory_code
1054                          into   l_mtl_trx_id,
1055                                 l_mtl_trx_date,
1056                                 l_mtl_serial_num,
1057                                 l_mtl_subinv
1058                          from  mtl_material_transactions mt,
1059                                mtl_unit_transactions mut
1060                          where mt.transaction_id = mut.transaction_id
1061                           and  mt.transaction_source_id      = JOB.wip_entity_id
1062                           and  mt.transaction_source_type_id = 5  -- Job or Schedule
1063                           and  mt.transaction_action_id      = 31 -- Wip Assembly Completion
1064                           and  mut.serial_number not in (select crh.paramc3
1065                                                          from   csd_repair_history crh,
1066                                                                 csd_repair_job_xref crj
1067                                                          where  crh.repair_line_id = crj.repair_line_id
1068                                                           and   crj.wip_entity_id  = JOB.wip_entity_id
1069                                                           and   crh.event_code     = 'JC')
1070                           and  mut.serial_number not in (Select cra.serial_number
1071 					                                from   csd_repairs cra,
1072 											         csd_repair_job_xref crj
1073 											  where cra.repair_line_id = crj.repair_line_id
1074 											   and  crj.wip_entity_id  = JOB.wip_entity_id
1075 											   and  cra.serial_number is not null)
1076 		     		 and rownum = 1;
1077 
1078                         -- Update csd_repair_job_xref with qty completed
1079                         update csd_repair_job_xref
1080                         set quantity_completed = nvl(quantity_completed,0) + 1,
1081 		                  object_version_number = object_version_number+1,
1082                             last_update_date   = sysdate,
1083                             last_updated_by    = fnd_global.user_id,
1084                             last_update_login  = fnd_global.login_id
1085                         where repair_job_xref_id = JOB.repair_job_xref_id;
1086                         IF SQL%NOTFOUND THEN
1087                             IF ( l_error_level >= G_debug_level)  THEN
1088                                FND_MESSAGE.SET_NAME('CSD','CSD_JOB_XREF_UPD_FAILED');
1089                                FND_MESSAGE.SET_TOKEN('REP_JOB_XREF_ID',JOB.repair_job_xref_id );
1090                                FND_LOG.MESSAGE(l_error_level,l_mod_name,FALSE);
1091                             ELSE
1092                                FND_MESSAGE.SET_NAME('CSD','CSD_JOB_XREF_UPD_FAILED');
1093                                FND_MESSAGE.SET_TOKEN('REP_JOB_XREF_ID',JOB.repair_job_xref_id );
1094                                FND_MSG_PUB.ADD;
1095                             END IF;
1096                             RAISE FND_API.G_EXC_ERROR;
1097                         End IF;
1098 
1099 				    -- Initialize the activity rec
1100                         l_activity_rec := INIT_ACTIVITY_REC ;
1101 
1102                          -- Assign the values for activity record
1103                         l_activity_rec.REPAIR_HISTORY_ID := l_rep_hist_id;
1104                         l_activity_rec.REPAIR_LINE_ID :=  JOB.repair_line_id;
1105                         l_activity_rec.EVENT_CODE     :=  'JCA';
1106                         l_activity_rec.ACTION_CODE    :=  0;
1107                         l_activity_rec.EVENT_DATE     :=  l_mtl_trx_date;
1108                         l_activity_rec.QUANTITY       :=  null;
1109                         l_activity_rec.PARAMN1        :=  JOB.organization_id;
1110                         l_activity_rec.PARAMN3        :=  l_mtl_trx_id;
1111                         l_activity_rec.PARAMN4        :=  JOB.wip_entity_id;
1112                         l_activity_rec.PARAMN6        :=  l_ro_count;
1113                         l_activity_rec.PARAMC1        :=  l_mtl_serial_num ;
1114                         l_activity_rec.PARAMC2        :=  JOB.ro_serial_num ;
1115                         l_activity_rec.OBJECT_VERSION_NUMBER := null;
1116 
1117                         Debug('Calling LOG_ACTIVITY ',l_mod_name,2);
1118                         -- Calling LOG_ACTIVITY for logging activity
1119                         -- When the completed serial number does not match
1120 				    -- with the repair order serial number
1121                         LOG_ACTIVITY
1122                             ( p_api_version     => p_api_version,
1123                               p_commit          => p_commit,
1124                               p_init_msg_list   => p_init_msg_list,
1125                               p_validation_level => p_validation_level,
1126                               x_return_status   => x_return_status,
1127                               x_msg_count       => x_msg_count,
1128                               x_msg_data        => x_msg_data,
1129                               p_activity_rec    => l_activity_rec );
1130 
1131                        Debug('x_return_status from LOG_ACTIVITY ='||x_return_status,l_mod_name,2);
1132                        IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
1133                           Debug('LOG_ACTIVITY api failed ',l_mod_name,4);
1134                           RAISE FND_API.G_EXC_ERROR;
1135                        END IF;
1136 
1137 
1138 				    -- Initialize the activity rec
1139                         l_activity_rec := INIT_ACTIVITY_REC ;
1140 
1141                          -- Assign the values for activity record
1142                         l_activity_rec.REPAIR_HISTORY_ID := l_rep_hist_id;
1143                         l_activity_rec.REPAIR_LINE_ID :=  JOB.repair_line_id;
1144                         l_activity_rec.EVENT_CODE     :=  'JC';
1145                         l_activity_rec.ACTION_CODE    :=  0;
1146                         l_activity_rec.EVENT_DATE     :=  l_mtl_trx_date;
1147                         l_activity_rec.QUANTITY       :=  JOB.job_completed_qty;
1148                         l_activity_rec.PARAMN1        :=  JOB.organization_id;
1149                         l_activity_rec.PARAMN3        :=  l_mtl_trx_id;
1150                         l_activity_rec.PARAMN4        :=  JOB.wip_entity_id;
1151                         l_activity_rec.PARAMN5        :=  1;
1152                         l_activity_rec.PARAMN6        :=  l_ro_count;
1153                         l_activity_rec.PARAMC1        :=  l_mtl_subinv;
1154                         l_activity_rec.PARAMC2        :=  JOB.wip_entity_name;
1155                         l_activity_rec.PARAMC3        :=  l_mtl_serial_num;
1156                         l_activity_rec.OBJECT_VERSION_NUMBER := null;
1157 
1158                         Debug('Calling LOG_ACTIVITY ',l_mod_name,2);
1159                         -- Calling LOG_ACTIVITY for logging activity
1160                         -- for job completion
1161                         LOG_ACTIVITY
1162                             ( p_api_version     => p_api_version,
1163                               p_commit          => p_commit,
1164                               p_init_msg_list   => p_init_msg_list,
1165                               p_validation_level => p_validation_level,
1166                               x_return_status   => x_return_status,
1167                               x_msg_count       => x_msg_count,
1168                               x_msg_data        => x_msg_data,
1169                               p_activity_rec    => l_activity_rec );
1170 
1171                        Debug('x_return_status from LOG_ACTIVITY ='||x_return_status,l_mod_name,2);
1172                        IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
1173                           Debug('LOG_ACTIVITY api failed ',l_mod_name,4);
1174                           RAISE FND_API.G_EXC_ERROR;
1175                        END IF;
1176 
1177                       EXCEPTION
1178 				     WHEN NO_DATA_FOUND THEN
1179                            NULL;
1180 				  END;
1181 
1182 			    END IF;
1183 
1184                 ELSE
1185 			     -- In case of upgrade, the wip job is submitted for the upgraded item,
1186 				-- which is different then the item on the repair order
1187                       BEGIN
1188                          Select mt.transaction_id,
1189                                 mt.transaction_date,
1190                                 mut.serial_number,
1191                                 mt.subinventory_code
1192                          into   l_mtl_trx_id,
1193                                 l_mtl_trx_date,
1194                                 l_mtl_serial_num,
1195                                 l_mtl_subinv
1196                          from  mtl_material_transactions mt,
1197                                mtl_unit_transactions mut
1198                          where mt.transaction_id = mut.transaction_id
1199                           and  mt.transaction_source_id      = JOB.wip_entity_id
1200                           and  mt.transaction_source_type_id = 5  -- Job or Schedule
1201                           and  mt.transaction_action_id      = 31 -- Wip Assembly Completion
1202                           and  mut.serial_number not in (select crh.paramc3
1203                                                          from   csd_repair_history crh,
1204                                                                 csd_repair_job_xref crj
1205                                                          where  crh.repair_line_id = crj.repair_line_id
1206                                                           and   crj.wip_entity_id  = JOB.wip_entity_id
1207                                                           and   crh.event_code     = 'JC')
1208                           and rownum = 1;
1209 
1210                         -- Update csd_repair_job_xref with qty completed
1211                         update csd_repair_job_xref
1212                         set quantity_completed = nvl(quantity_completed,0) + 1,
1213 		                  object_version_number = object_version_number+1,
1214                             last_update_date = sysdate,
1215                             last_updated_by  = fnd_global.user_id,
1216                             last_update_login = fnd_global.login_id
1217                         where repair_job_xref_id = JOB.repair_job_xref_id;
1218                         IF SQL%NOTFOUND THEN
1219                             IF ( l_error_level >= G_debug_level)  THEN
1220                                FND_MESSAGE.SET_NAME('CSD','CSD_JOB_XREF_UPD_FAILED');
1221                                FND_MESSAGE.SET_TOKEN('REP_JOB_XREF_ID',JOB.repair_job_xref_id );
1222                                FND_LOG.MESSAGE(l_error_level,l_mod_name,FALSE);
1223                             ELSE
1224                                FND_MESSAGE.SET_NAME('CSD','CSD_JOB_XREF_UPD_FAILED');
1225                                FND_MESSAGE.SET_TOKEN('REP_JOB_XREF_ID',JOB.repair_job_xref_id );
1226                                FND_MSG_PUB.ADD;
1227                             END IF;
1228                             RAISE FND_API.G_EXC_ERROR;
1229                         End IF;
1230 
1231                         -- Initialize the activity rec
1232                         l_activity_rec := INIT_ACTIVITY_REC ;
1233 
1234                          -- Assign the values for activity record
1235                         l_activity_rec.REPAIR_HISTORY_ID := l_rep_hist_id;
1236                         l_activity_rec.REPAIR_LINE_ID :=  JOB.repair_line_id;
1237                         l_activity_rec.EVENT_CODE     :=  'JC';
1238                         l_activity_rec.ACTION_CODE    :=  0;
1239                         l_activity_rec.EVENT_DATE     :=  l_mtl_trx_date;
1240                         l_activity_rec.QUANTITY       :=  JOB.job_completed_qty;
1241                         l_activity_rec.PARAMN1        :=  JOB.organization_id;
1242                         l_activity_rec.PARAMN3        :=  l_mtl_trx_id;
1243                         l_activity_rec.PARAMN4        :=  JOB.wip_entity_id;
1244                         l_activity_rec.PARAMN5        :=  1;
1245                         l_activity_rec.PARAMN6        :=  l_ro_count;
1246                         l_activity_rec.PARAMC1        :=  l_mtl_subinv;
1247                         l_activity_rec.PARAMC2        :=  JOB.wip_entity_name;
1248                         l_activity_rec.PARAMC3        :=  l_mtl_serial_num;
1249                         l_activity_rec.OBJECT_VERSION_NUMBER := null;
1250 
1251                         Debug('Calling LOG_ACTIVITY ',l_mod_name,2);
1252                         -- Calling LOG_ACTIVITY for logging activity
1253                         -- for job completion
1254                         LOG_ACTIVITY
1255                             ( p_api_version     => p_api_version,
1256                               p_commit          => p_commit,
1257                               p_init_msg_list   => p_init_msg_list,
1258                               p_validation_level => p_validation_level,
1259                               x_return_status   => x_return_status,
1260                               x_msg_count       => x_msg_count,
1261                               x_msg_data        => x_msg_data,
1262                               p_activity_rec    => l_activity_rec );
1263 
1264                        Debug('x_return_status from LOG_ACTIVITY ='||x_return_status,l_mod_name,2);
1265                        IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
1266                           Debug('LOG_ACTIVITY api failed ',l_mod_name,4);
1267                           RAISE FND_API.G_EXC_ERROR;
1268                        END IF;
1269 
1270                       EXCEPTION
1271                          WHEN NO_DATA_FOUND THEN
1272                            IF ( l_error_level >= G_debug_level)  THEN
1273                               FND_MESSAGE.SET_NAME('CSD','CSD_INV_WIP_ENTITY_ID');
1274                               FND_MESSAGE.SET_TOKEN('WIP_ENTITY_ID',JOB.wip_entity_id );
1275                               FND_LOG.MESSAGE(l_error_level,l_mod_name,FALSE);
1276                            ELSE
1277                               FND_MESSAGE.SET_NAME('CSD','CSD_INV_WIP_ENTITY_ID');
1278                               FND_MESSAGE.SET_TOKEN('WIP_ENTITY_ID',JOB.wip_entity_id );
1279                               FND_MSG_PUB.ADD;
1280                            END IF;
1281                            RAISE FND_API.G_EXC_ERROR;
1282                      END;
1283                 END IF;
1284 
1285             ELSE
1286                 -- This scenario is for the serialized item at sales order issue and non-serialized
1287                 -- In WIP_DISCRETE_JOBS
1288                 --   Job Name    Job Id      Start Qty   Completion Qty   Completion Subinv
1289                 --    W1          1121         10             10
1290                 --
1291                 --  MTL_MATERIAL_TRANSACTIONS
1292                 --    Txn Id   Job ID   Txn Qty    Completion Subinv
1293                 --      M1      1121      2           FGI
1294                 --      M2      1121      8           Stores
1295               FOR MTL in Get_mtl_txns(JOB.wip_entity_id)
1296      	        LOOP
1297                  BEGIN
1298                     -- Debug messages
1299                    Debug('l_remaining_qty    ='||TO_CHAR(l_remaining_qty),l_mod_name,1);
1300         	        IF l_remaining_qty <= 0 then
1301                         -- exit the loop if nothing to process
1302                         Exit;
1303     	             ELSE
1304                       SELECT nvl(SUM(paramn5),0)
1305             		  INTO l_total_qty
1306             		  FROM CSD_REPAIR_HISTORY
1307             		  WHERE paramn3   = MTL.transaction_id
1308             		   AND  paramn4   = JOB.wip_entity_id
1309             		   AND  event_Code= 'JC';
1310 
1311                         -- Debug messages
1312                         Debug('l_total_qty    ='||TO_CHAR(l_total_qty),l_mod_name,1);
1313 
1314                         IF (nvl(MTL.transaction_quantity,0) - l_total_qty)> 0 THEN
1315              		   If (l_remaining_qty > (nvl(MTL.transaction_quantity,0) - l_total_qty)) then
1316                              l_remaining_qty  := l_remaining_qty - (nvl(MTL.transaction_quantity,0) - l_total_qty);
1317             		         l_update_qty     := (nvl(MTL.transaction_quantity,0) - l_total_qty);
1318              		   ELSE
1319             			    l_update_Qty     := l_remaining_qty;
1320             			    l_remaining_qty  := 0;
1321             		   END IF;
1322 
1323                             -- Debug messages
1324                             Debug('l_update_Qty    ='||TO_CHAR(l_update_Qty),l_mod_name,1);
1325                             Debug('l_remaining_qty ='||TO_CHAR(l_remaining_qty),l_mod_name,1);
1326 
1327                             -- Update csd_repair_job_xref with qty completed
1328                             update csd_repair_job_xref
1329                             set quantity_completed = nvl(quantity_completed,0) + NVL(l_update_qty,0),
1330 		                      object_version_number = object_version_number+1,
1331                                 last_update_date = sysdate,
1332                                 last_updated_by  = fnd_global.user_id,
1333                                 last_update_login = fnd_global.login_id
1334                             where repair_job_xref_id = JOB.repair_job_xref_id;
1335                             IF SQL%NOTFOUND THEN
1336                                 IF ( l_error_level >= G_debug_level)  THEN
1337                                    FND_MESSAGE.SET_NAME('CSD','CSD_JOB_XREF_UPD_FAILED');
1338                                    FND_MESSAGE.SET_TOKEN('REP_JOB_XREF_ID',JOB.repair_job_xref_id );
1339                                    FND_LOG.MESSAGE(l_error_level,l_mod_name,FALSE);
1340                                 ELSE
1341                                    FND_MESSAGE.SET_NAME('CSD','CSD_JOB_XREF_UPD_FAILED');
1342                                    FND_MESSAGE.SET_TOKEN('REP_JOB_XREF_ID',JOB.repair_job_xref_id );
1343                                    FND_MSG_PUB.ADD;
1344                                 END IF;
1345                                 RAISE FND_API.G_EXC_ERROR;
1346                             End IF;
1347 
1348                             -- Initialize the activity rec
1349                             l_activity_rec := INIT_ACTIVITY_REC ;
1350 
1351                              -- Assign the values for activity record
1352                             l_activity_rec.REPAIR_HISTORY_ID := l_rep_hist_id;
1353                             l_activity_rec.REPAIR_LINE_ID :=  JOB.repair_line_id;
1354                             l_activity_rec.EVENT_CODE     :=  'JC';
1355                             l_activity_rec.ACTION_CODE    :=  0;
1356                             l_activity_rec.EVENT_DATE     :=  MTL.transaction_date;
1357                             l_activity_rec.QUANTITY       :=  JOB.job_completed_qty;
1358                             l_activity_rec.PARAMN1        :=  JOB.organization_id;
1359                             l_activity_rec.PARAMN3        :=  MTL.transaction_id;
1360                             l_activity_rec.PARAMN4        :=  JOB.wip_entity_id;
1361                             l_activity_rec.PARAMN5        :=  l_update_qty;
1362                             l_activity_rec.PARAMN6        :=  l_ro_count;
1363                             l_activity_rec.PARAMC1        :=  MTL.subinventory_code;
1364                             l_activity_rec.PARAMC2        :=  JOB.wip_entity_name;
1365                             l_activity_rec.OBJECT_VERSION_NUMBER := null;
1366 
1367                             Debug('Calling LOG_ACTIVITY ',l_mod_name,2);
1368 
1369                             -- Calling LOG_ACTIVITY for logging activity
1370                             -- for job completion
1371                             LOG_ACTIVITY
1372                                 ( p_api_version     => p_api_version,
1373                                   p_commit          => p_commit,
1374                                   p_init_msg_list   => p_init_msg_list,
1375                                   p_validation_level => p_validation_level,
1376                                   x_return_status   => x_return_status,
1377                                   x_msg_count       => x_msg_count,
1378                                   x_msg_data        => x_msg_data,
1379                                   p_activity_rec    => l_activity_rec );
1380 
1381                            Debug('x_return_status from LOG_ACTIVITY ='||x_return_status,l_mod_name,2);
1382                            IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
1383                               Debug('LOG_ACTIVITY api failed ',l_mod_name,4);
1384                               RAISE FND_API.G_EXC_ERROR;
1385                            END IF;
1386                       END IF; --IF (MTL.transaction_quantity - l_total_qty)> 0
1387                   END IF;-- if l_remaining_qty <=0
1388 
1389               EXCEPTION
1390                    WHEN FND_API.G_EXC_ERROR THEN
1391                         RAISE FND_API.G_EXC_ERROR;
1392               END;
1393             END LOOP; -- MTL cursor
1394 
1395         END IF; -- end if serial_number_control_code (2,5)
1396      END IF;-- if l_remaining_qty >0
1397 
1398     -- Increment if the record is processed successfully
1399     l_total_rec := l_total_rec + 1;
1400     Exception
1401        WHEN FND_API.G_EXC_ERROR THEN
1402           ROLLBACK TO JOB_COMPLETION;
1403           --x_return_status := FND_API.G_RET_STS_ERROR ;
1404           -- In case of error, exit the loop. It could rarely fail
1405           -- rollback the current record but commit the processed records
1406           --exit;
1407        WHEN SKIP_RECORD THEN
1408           NULL;
1409        WHEN OTHERS THEN
1410           ROLLBACK TO JOB_COMPLETION;
1411           --x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
1412           -- In case of error, exit the loop. It could rarely fail
1413           -- rollback the current record but commit the processed records
1414           --exit;
1415     END;
1416 
1417      -- Commit for every 500 records
1418      -- one should COMMIT less frequently within a PL/SQL loop to
1419      -- prevent ORA-1555 (Snapshot too old) errors
1420      l_count := l_count+1;
1421      IF mod(l_count, l_commit_size) = 0 THEN -- Commit every 500 records
1422         IF FND_API.To_Boolean( p_commit ) THEN
1423             COMMIT WORK;
1424         END IF;
1425      END IF;
1426 
1427   END LOOP; -- JOB Cursor
1428 
1429   --vijay 11/9/04
1430 
1431    IF(Repair_Jobs_ALL%ISOPEN ) THEN
1432        CLOSE Repair_Jobs_ALL;
1433    END IF;
1434    IF(Repair_Jobs%ISOPEN ) THEN
1435        CLOSE Repair_Jobs;
1436    END IF;
1437 
1438 
1439   -- Log messages for the number of records processed
1440   fnd_message.set_name('CSD','CSD_DRC_WIP_REC_PROC');
1441   fnd_message.set_token('TOT_REC',to_char(l_total_rec));
1442   FND_MSG_PUB.ADD;
1443 
1444   -- Retrive the message from the msg stack
1445   l_msg_text := fnd_message.get;
1446 
1447   -- Log the number of records processed in concurrent program output and log file
1448   fnd_file.put_line(fnd_file.log,l_msg_text);
1449   fnd_file.put_line(fnd_file.output,l_msg_text);
1450 
1451   -- Debug message
1452   Debug(l_msg_text,l_mod_name,1 );
1453 
1454   -- Standard check of p_commit.
1455   IF FND_API.To_Boolean( p_commit ) THEN
1456        COMMIT WORK;
1457   END IF;
1458 
1459   -- Standard call to get message count and IF count is  get message info.
1460   FND_MSG_PUB.Count_And_Get
1461            (p_count  =>  x_msg_count,
1462             p_data   =>  x_msg_data );
1463  EXCEPTION
1464     WHEN FND_API.G_EXC_ERROR THEN
1465           Debug('In FND_API.G_EXC_ERROR exception',l_mod_name,4);
1466           ROLLBACK TO JOB_COMPLETION_UPDATE;
1467           x_return_status := FND_API.G_RET_STS_ERROR ;
1468           FND_MSG_PUB.Count_And_Get
1469                 (p_count  =>  x_msg_count,
1470                  p_data   =>  x_msg_data  );
1471     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1472           Debug('In FND_API.G_EXC_UNEXPECTED_ERROR exception',l_mod_name,4);
1473           ROLLBACK TO JOB_COMPLETION_UPDATE;
1474           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
1475           FND_MSG_PUB.Count_And_Get
1476                 ( p_count  =>  x_msg_count,
1477                   p_data   =>  x_msg_data );
1478     WHEN OTHERS THEN
1479           Debug('In OTHERS exception',l_mod_name,4);
1480           ROLLBACK TO JOB_COMPLETION_UPDATE;
1481           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
1482           IF  FND_MSG_PUB.Check_Msg_Level
1483                   (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
1484           THEN
1485               FND_MSG_PUB.Add_Exc_Msg
1486                   (G_PKG_NAME ,
1487                    l_api_name  );
1488           END IF;
1489           FND_MSG_PUB.Count_And_Get
1490                   (p_count  =>  x_msg_count,
1491                    p_data   =>  x_msg_data );
1492 END JOB_COMPLETION_UPDATE;
1493 
1494 /*-------------------------------------------------------------------------------------*/
1495 /* Procedure name: JOB_CREATION_UPDATE                                                 */
1496 /* Description   : Procedure called from wip_update API to update the wip entity Id    */
1497 /*                 for the new jobs created by the WIP Mass Load concurrent program    */
1498 /* Called from   : Called from WIP_Update API                                          */
1499 /* STANDARD PARAMETERS                                                                 */
1500 /*  In Parameters :                                                                    */
1501 /*    p_api_version       NUMBER    Required  Api Version number                       */
1502 /*    p_init_msg_list     VARCHAR2  Optional  To Initialize message stack              */
1503 /*    p_commit            VARCHAR2  Optional  Commits in API                           */
1504 /*    p_validation_level  NUMBER    Optional  validation level                         */
1505 /*                                                                                     */
1506 /*  Output Parameters:                                                                 */
1507 /*   x_return_status     VARCHAR2      Return status of the API                        */
1508 /*   x_msg_count         NUMBER        Number of messages in stack                     */
1509 /*   x_msg_data          VARCHAR2      Error Message from message stack                */
1510 /*                                                                                     */
1511 /* NON-STANDARD PARAMETERS                                                             */
1512 /*   In Parameters                                                                     */
1513 /*    p_repair_line_id   NUMBER   Optional   Repair Order Line Id                      */
1514 /* Output Parameter :                                                                  */
1515 /* Change Hist :                                                                       */
1516 /*   09/20/03  vlakaman  Initial Creation.                                             */
1517 /*                                                                                     */
1518 /*-------------------------------------------------------------------------------------*/
1519 
1520 Procedure  JOB_CREATION_UPDATE
1521         ( p_api_version          IN   NUMBER,
1522           p_commit               IN   VARCHAR2,
1523           p_init_msg_list        IN   VARCHAR2,
1524           p_validation_level     IN   NUMBER,
1525           x_return_status        OUT  NOCOPY  VARCHAR2,
1526           x_msg_count            OUT  NOCOPY  NUMBER,
1527           x_msg_data             OUT  NOCOPY  VARCHAR2,
1528           p_repair_line_id       IN   NUMBER
1529   	    ) IS
1530 
1531   --Standard variables
1532   l_api_name             CONSTANT VARCHAR2(30)   := 'JOB_CREATION_UPDATE';
1533   l_api_version          CONSTANT NUMBER         := 1.0;
1534 
1535   -- Variables used in the program
1536   l_rep_hist_id       NUMBER;
1537   l_total_rec         NUMBER:= 0;
1538   l_update_qty        NUMBER;
1539   l_count             NUMBER;
1540   l_dummy             varchar2(30);
1541   l_commit_size       NUMBER := 500;
1542 
1543   -- activity record
1544   l_activity_rec      activity_rec_type;
1545 
1546   -- Variable used in FND log
1547   l_error_level  number   := FND_LOG.LEVEL_ERROR;
1548   l_mod_name     varchar2(2000) := 'csd.plsql.csd_update_programs_pvt.job_creation_update';
1549 
1550   -- This cursor gets all the records in csd_repair_job_xref
1551   -- that does not have wip_entity_id.
1552   --Cursor split into two to remove OR condition
1553   -- Vijay 11/9/04
1554   Cursor JOB_CREATION( p_rep_line_id in number )IS
1555     SELECT
1556       crj.repair_job_xref_id,
1557       crj.repair_line_id,
1558       crj.organization_id,
1559       crj.quantity allocated_job_qty,
1560       we.wip_entity_id,
1561       we.wip_entity_name,
1562       wdj.start_quantity job_qty,
1563       wdj.creation_date
1564     from  CSD_REPAIR_JOB_XREF crj,
1565           WIP_ENTITIES we,
1566           wip_discrete_jobs wdj
1567     where wdj.wip_entity_id   = we.wip_entity_id
1568      and  crj.job_name        = we.wip_entity_name
1569      and  crj.organization_id = we.organization_id
1570      and  crj.repair_line_id = p_rep_line_id
1571      and  crj.wip_entity_id is null;
1572   -- This cursor gets all the records in csd_repair_job_xref
1573   -- that does not have wip_entity_id.
1574   Cursor JOB_CREATION_ALL IS
1575     SELECT
1576       crj.repair_job_xref_id,
1577       crj.repair_line_id,
1578       crj.organization_id,
1579       crj.quantity allocated_job_qty,
1580       we.wip_entity_id,
1581       we.wip_entity_name,
1582       wdj.start_quantity job_qty,
1583       wdj.creation_date
1584     from  CSD_REPAIR_JOB_XREF crj,
1585           WIP_ENTITIES we,
1586           wip_discrete_jobs wdj
1587     where wdj.wip_entity_id   = we.wip_entity_id
1588      and  crj.job_name        = we.wip_entity_name
1589      and  crj.organization_id = we.organization_id
1590      and  crj.wip_entity_id is null;
1591 --Vijay 11/9/04
1592   K JOB_CREATION_Rec_Type;
1593 BEGIN
1594    -- Standard Start of API savepoint
1595    SAVEPOINT   JOB_CREATION_UPDATE;
1596 
1597    -- Initialize API return status to success
1598    x_return_status := FND_API.G_RET_STS_SUCCESS;
1599 
1600    -- Debug messages
1601    Debug('At the Beginning of JOB_CREATION_UPDATE',l_mod_name,1);
1602 
1603    -- Standard call to check for call compatibility.
1604    IF NOT FND_API.Compatible_API_Call (l_api_version,
1605                                        p_api_version,
1606                                        l_api_name   ,
1607                                        G_PKG_NAME   )
1608        THEN
1609           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1610    END IF;
1611 
1612    -- Initialize message list if p_init_msg_list is set to TRUE.
1613    IF FND_API.to_Boolean( p_init_msg_list ) THEN
1614          FND_MSG_PUB.initialize;
1615    END IF;
1616 
1617    -- Keeping the running total of the records
1618    -- for commiting purpose
1619    l_count := 0;
1620 
1621     -- When Jobs are created from the UI, a record is inserted in csd_repair_job_xref
1622     -- with the job name. Once the WIP Job is created in wip_discrete_jobs
1623     -- then the update program will find the wip_entity_id and update it.
1624     -- Also it updates the quantity_in_wip in the csd_repairs with Job start quantity
1625     --
1626     -- When the WIP Job is submitted from the UI, the record would look as follows:
1627     --  CSD_REPAIR_JOB_XREF
1628     --   Repair Job   Repair Order   Allocated Qty   Job Name  Job Qty   Wip_entity_ID
1629     --     RJ1         R1                1            JOB1        3
1630     --     RJ2         R2                1            JOB1        3
1631     --     RJ3         R3                1            JOB1        3
1632     --
1633     -- After Running the Update Program the record would look as follows:
1634     --  CSD_REPAIR_JOB_XREF
1635     --   Repair Job   Repair Order   Allocated Qty   Job Name  Job Qty   Wip_entity_ID
1636     --     RJ1         R1                1            JOB1        3         1121
1637     --     RJ2         R2                1            JOB1        3         1121
1638     --     RJ3         R3                1            JOB1        3         1121
1639     --FOR K IN Job_Creation ( p_repair_line_id )
1640    IF(p_repair_line_Id is null) THEN
1641      OPEN Job_Creation_ALL;
1642    ELSE
1643      OPEN Job_Creation (p_repair_line_id);
1644    END IF;
1645 
1646    LOOP
1647     BEGIN
1648 
1649       IF(p_repair_line_Id is null) THEN
1650         FETCH  Job_Creation_ALL INTO K;
1651         EXIT WHEN Job_Creation_ALL%NOTFOUND;
1652       ELSE
1653         FETCH Job_Creation  INTO K;
1654         EXIT WHEN Job_Creation %NOTFOUND;
1655       END IF;
1656          -- savepoint
1657          SAVEPOINT  Job_Creation;
1658 
1659          -- Debug Messages
1660          Debug('wip_entity_id    ='||K.wip_entity_id,l_mod_name,1);
1661          Debug('Logging activity for Job creation',l_mod_name,1);
1662 
1663          -- Initialize the activity rec
1664          l_activity_rec := INIT_ACTIVITY_REC ;
1665 
1666          -- Assign the values for activity record
1667          l_activity_rec.REPAIR_HISTORY_ID := l_rep_hist_id;
1668          l_activity_rec.REPAIR_LINE_ID := K.repair_line_id;
1669          l_activity_rec.EVENT_CODE     := 'JS';
1670          l_activity_rec.ACTION_CODE    := 0;
1671          l_activity_rec.EVENT_DATE     := K.creation_date;
1672          l_activity_rec.QUANTITY       := K.job_qty;
1673          l_activity_rec.PARAMN1        := k.wip_entity_id;
1674          l_activity_rec.PARAMN2        := k.organization_id;
1675          l_activity_rec.PARAMN5        :=  K.allocated_job_qty;
1676          l_activity_rec.PARAMC1        :=  K.wip_entity_name;
1677          l_activity_rec.OBJECT_VERSION_NUMBER := null;
1678 
1679          -- Debug Messages
1680          Debug('Calling LOG_ACTIVITY',l_mod_name,2);
1681 
1682          -- Calling LOG_ACTIVITY for logging activity
1683          -- for job creation
1684          LOG_ACTIVITY
1685                 ( p_api_version     => p_api_version,
1686                   p_commit          => p_commit,
1687                   p_init_msg_list   => p_init_msg_list,
1688                   p_validation_level => p_validation_level,
1689                   x_return_status   => x_return_status,
1690                   x_msg_count       => x_msg_count,
1691                   x_msg_data        => x_msg_data,
1692                   p_activity_rec    => l_activity_rec );
1693 
1694          -- Debug Messages
1695          Debug('Return Status from LOG_ACTIVITY:'||x_return_status,l_mod_name,2);
1696 
1697          IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
1698              Debug('LOG_ACTIVITY api failed ',l_mod_name,4);
1699              RAISE FND_API.G_EXC_ERROR;
1700          END IF;
1701 
1702          -- Updating csd_repair_job_xref with the wip_entity_id
1703          Update csd_repair_job_xref
1704          set wip_entity_id        = K.wip_entity_id,
1705 		   object_version_number = object_version_number+1,
1706              last_update_date = sysdate,
1707              last_updated_by  = fnd_global.user_id,
1708              last_update_login = fnd_global.login_id
1709          where repair_job_xref_id = K.repair_job_xref_id;
1710          IF SQL%NOTFOUND THEN
1711              IF ( l_error_level >= G_debug_level)  THEN
1712                  FND_MESSAGE.SET_NAME('CSD','CSD_JOB_XREF_UPD_FAILED');
1713                  FND_MESSAGE.SET_TOKEN('REP_JOB_XREF_ID',K.repair_job_xref_id );
1714                  FND_LOG.MESSAGE(l_error_level,l_mod_name,FALSE);
1715              ELSE
1716                  FND_MESSAGE.SET_NAME('CSD','CSD_JOB_XREF_UPD_FAILED');
1717                  FND_MESSAGE.SET_TOKEN('REP_JOB_XREF_ID',K.repair_job_xref_id );
1718                  FND_MSG_PUB.ADD;
1719              END IF;
1720              RAISE FND_API.G_EXC_ERROR;
1721          End IF;
1722 
1723          -- Updating repair order with the Job allocated qty
1724          Update csd_repairs
1725          set  quantity_in_wip = NVL(quantity_in_wip,0) + K.allocated_job_qty,
1726               last_update_date = sysdate,
1727               last_updated_by  = fnd_global.user_id,
1728 		    object_version_number = object_version_number+1,
1729               last_update_login = fnd_global.login_id
1730          where repair_line_id = K.repair_line_id;
1731          IF SQL%NOTFOUND THEN
1732              IF ( l_error_level >= G_debug_level)  THEN
1733                  FND_MESSAGE.SET_NAME('CSD','CSD_RO_UPD_FAILED');
1734                  FND_MESSAGE.SET_TOKEN('REPAIR_LINE_ID',K.repair_line_id );
1735                  FND_LOG.MESSAGE(l_error_level,l_mod_name,FALSE);
1736              ELSE
1737                  FND_MESSAGE.SET_NAME('CSD','CSD_RO_UPD_FAILED');
1738                  FND_MESSAGE.SET_TOKEN('REPAIR_LINE_ID',K.repair_line_id );
1739                  FND_MSG_PUB.ADD;
1740              END IF;
1741              RAISE FND_API.G_EXC_ERROR;
1742          End IF;
1743 
1744        Exception
1745            WHEN FND_API.G_EXC_ERROR THEN
1746               ROLLBACK TO JOB_CREATION;
1747               --x_return_status := FND_API.G_RET_STS_ERROR ;
1748               -- exit the loop in case of error. Commit the processed records
1749               -- but rollback the current error record
1750               --exit;
1751            WHEN OTHERS THEN
1752               ROLLBACK TO JOB_CREATION;
1753               --x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
1754               -- exit the loop in case of error. Commit the processed records
1755               -- but rollback the current error record
1756               --exit;
1757        END;
1758          -- Commit for every 500 records
1759          -- one should COMMIT less frequently within a PL/SQL loop to
1760          -- prevent ORA-1555 (Snapshot too old) errors
1761          l_count := l_count+1;
1762  	    IF mod(l_count, l_commit_size) = 0 THEN -- Commit every 500 records
1763              IF FND_API.To_Boolean( p_commit ) THEN
1764                  COMMIT WORK;
1765              END IF;
1766 	    END IF;
1767   END LOOP; -- End of Job Creation activity
1768 
1769   --Vijay 11/9/04
1770 
1771   IF(Job_Creation_ALL%ISOPEN ) THEN
1772        CLOSE Job_Creation_ALL;
1773   END IF;
1774   IF(Job_Creation%ISOPEN ) THEN
1775       CLOSE Job_Creation;
1776   END IF;
1777 
1778 
1779   -- Standard check of p_commit.
1780   IF FND_API.To_Boolean( p_commit ) THEN
1781        COMMIT WORK;
1782   END IF;
1783 
1784   -- Standard call to get message count and IF count is  get message info.
1785   FND_MSG_PUB.Count_And_Get
1786            (p_count  =>  x_msg_count,
1787             p_data   =>  x_msg_data );
1788  EXCEPTION
1789     WHEN FND_API.G_EXC_ERROR THEN
1790           Debug('In FND_API.G_EXC_ERROR exception',l_mod_name,4);
1791           ROLLBACK TO  JOB_CREATION_UPDATE;
1792           x_return_status := FND_API.G_RET_STS_ERROR ;
1793           FND_MSG_PUB.Count_And_Get
1794                 (p_count  =>  x_msg_count,
1795                  p_data   =>  x_msg_data  );
1796     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1797           Debug('In FND_API.G_EXC_UNEXPECTED_ERROR exception',l_mod_name,4);
1798           ROLLBACK TO  JOB_CREATION_UPDATE;
1799           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
1800           FND_MSG_PUB.Count_And_Get
1801                 ( p_count  =>  x_msg_count,
1802                   p_data   =>  x_msg_data );
1803     WHEN OTHERS THEN
1804           Debug('In OTHERS exception',l_mod_name,4);
1805           ROLLBACK TO  JOB_CREATION_UPDATE;
1806           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
1807           IF  FND_MSG_PUB.Check_Msg_Level
1808                   (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
1809           THEN
1810               FND_MSG_PUB.Add_Exc_Msg
1811                   (G_PKG_NAME ,
1812                    l_api_name  );
1813           END IF;
1814           FND_MSG_PUB.Count_And_Get
1815                   (p_count  =>  x_msg_count,
1816                    p_data   =>  x_msg_data );
1817 END JOB_CREATION_UPDATE;
1818 
1819 /*-------------------------------------------------------------------------------------*/
1820 /* Procedure name: RECEIPTS_UPDATE                                                     */
1821 /* Description   : Procedure called from the UI to update the depot tables             */
1822 /*                 for the receipts against RMA/Internal Requisitions. It calls        */
1823 /*                 RMA_RCV_UPDATE and IO_RCV_UPDATE to process RMA and IO respectively */
1824 /* Called from   : Called from Depot Repair UI                                         */
1825 /* STANDARD PARAMETERS                                                                 */
1826 /*  In Parameters :                                                                    */
1827 /*    p_api_version       NUMBER    Required  Api Version number                       */
1828 /*    p_init_msg_list     VARCHAR2  Optional  To Initialize message stack              */
1829 /*    p_commit            VARCHAR2  Optional  Commits in API                           */
1830 /*    p_validation_level  NUMBER    Optional  validation level                         */
1831 /*                                                                                     */
1832 /*  Output Parameters:                                                                 */
1833 /*   x_return_status     VARCHAR2      Return status of the API                        */
1834 /*   x_msg_count         NUMBER        Number of messages in stack                     */
1835 /*   x_msg_data          VARCHAR2      Error Message from message stack                */
1836 /*                                                                                     */
1837 /* NON-STANDARD PARAMETERS                                                             */
1838 /*   In Parameters                                                                     */
1839 /*    p_order_header_id  NUMBER   Optional   Interal sales order Id                    */
1840 /*    p_internal_order_flag VARCHAR2 Required  Order Type; Possible values -'Y','N'    */
1841 /*    p_repair_line_id   NUMBER   Optional   Repair Order Line Id                      */
1842 /* Output Parameter :                                                                  */
1843 /* Change Hist :                                                                       */
1844 /*   09/20/03  vlakaman  Initial Creation.                                             */
1845 /*-------------------------------------------------------------------------------------*/
1846 
1847 Procedure  RECEIPTS_UPDATE
1848         ( p_api_version          IN   NUMBER,
1849           p_commit               IN   VARCHAR2,
1850           p_init_msg_list        IN   VARCHAR2,
1851           p_validation_level     IN   NUMBER,
1852           x_return_status        OUT  NOCOPY  VARCHAR2,
1853           x_msg_count            OUT  NOCOPY  NUMBER,
1854           x_msg_data             OUT  NOCOPY  VARCHAR2,
1855           p_internal_order_flag  IN   VARCHAR2,
1856           p_order_header_id      IN   NUMBER,
1857           p_repair_line_id       IN   NUMBER,
1858           p_past_num_of_days     IN   NUMBER DEFAULT NULL   ----bug#6753684, 6742512
1859 	  ) IS
1860 
1861   --Standard variables
1862   l_api_name             CONSTANT VARCHAR2(30)   := 'RECEIPTS_UPDATE';
1863   l_api_version          CONSTANT NUMBER         := 1.0;
1864 
1865   -- Variable used in FND log
1866   l_error_level  number   := FND_LOG.LEVEL_ERROR;
1867   l_mod_name     varchar2(2000) := 'csd.plsql.csd_update_programs_pvt.receipts_update';
1868 
1869 BEGIN
1870    -- Standard Start of API savepoint
1871    SAVEPOINT  RECEIPTS_UPDATE;
1872 
1873    -- Initialize API return status to success
1874    x_return_status := FND_API.G_RET_STS_SUCCESS;
1875 
1876    -- Log the api name in the log file
1877    Debug('At the Beginning of Receipts_update',l_mod_name,1);
1878    Debug('p_internal_order_flag   ='||p_internal_order_flag,l_mod_name,1);
1879    Debug('p_order_header_id       ='||p_order_header_id,l_mod_name,1);
1880    Debug('p_repair_line_id        ='||p_repair_line_id,l_mod_name,1);
1881 
1882    -- Standard call to check for call compatibility.
1883    IF NOT FND_API.Compatible_API_Call (l_api_version,
1884                                        p_api_version,
1885                                        l_api_name   ,
1886                                        G_PKG_NAME   )
1887        THEN
1888           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1889    END IF;
1890 
1891    -- Initialize message list if p_init_msg_list is set to TRUE.
1892    IF FND_API.to_Boolean( p_init_msg_list ) THEN
1893          FND_MSG_PUB.initialize;
1894    END IF;
1895 
1896    -- Api body starts
1897    IF p_internal_order_flag = 'Y' then
1898 
1899        Debug('Calling IO_SHIP_UPDATE API',l_mod_name,2);
1900        -- Call the api for processing the shipment against
1901        -- internal orders
1902        IO_SHIP_UPDATE
1903         ( p_api_version          => p_api_version,
1904           p_commit               => p_commit     ,
1905           p_init_msg_list        => p_init_msg_list,
1906           p_validation_level     => p_validation_level,
1907           x_return_status        => x_return_status,
1908           x_msg_count            => x_msg_count,
1909           x_msg_data             => x_msg_data ,
1910           p_order_header_id      => p_order_header_id );
1911 
1912        -- Debug messages
1913        Debug('Return Status from IO_SHIP_UPDATE API :'||x_return_status,l_mod_name,2);
1914        IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
1915           Debug('IO_SHIP_UPDATE failed',l_mod_name,4);
1916           RAISE FND_API.G_EXC_ERROR;
1917        END IF;
1918 
1919        Debug('Calling IO_RCV_UPDATE API',l_mod_name,2);
1920        -- Call the api for processing the receipt against
1921        -- internal requisition
1922        IO_RCV_UPDATE
1923         ( p_api_version          => p_api_version,
1924           p_commit               => p_commit     ,
1925           p_init_msg_list        => p_init_msg_list,
1926           p_validation_level     => p_validation_level,
1927           x_return_status        => x_return_status,
1928           x_msg_count            => x_msg_count,
1929           x_msg_data             => x_msg_data ,
1930           p_order_header_id      => p_order_header_id );
1931 
1932        -- Debug messages
1933        Debug('Return Status from IO_RCV_UPDATE API :'||x_return_status,l_mod_name,2);
1934        IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
1935           Debug('IO_RCV_UPDATE failed',l_mod_name,4);
1936           RAISE FND_API.G_EXC_ERROR;
1937        END IF;
1938    Else
1939         -- Debug messages
1940         Debug('Calling RMA_RCV_UPDATE API',l_mod_name,2);
1941         -- Call the api for processing the receipts against RMA
1942         RMA_RCV_UPDATE
1943         ( p_api_version          => p_api_version,
1944           p_commit               => p_commit     ,
1945           p_init_msg_list        => p_init_msg_list,
1946           p_validation_level     => p_validation_level,
1947           x_return_status        => x_return_status,
1948           x_msg_count            => x_msg_count,
1949           x_msg_data             => x_msg_data ,
1950           p_repair_line_id       => p_repair_line_id,
1951           p_past_num_of_days     => p_past_num_of_days);
1952 
1953        -- Debug messages
1954        Debug('Return Status from RMA_RCV_UPDATE API :'||x_return_status,l_mod_name,2);
1955        IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
1956           Debug('RMA_RCV_UPDATE failed',l_mod_name,4);
1957           RAISE FND_API.G_EXC_ERROR;
1958        END IF;
1959   End If;
1960   -- bug#7497790, 12.1 FP, subhat.
1961 	-- automatically update RO status when item is received.
1962 	-- dont show any error messages. Pass p_validation_level = fnd_api.g_valid_level_full to receive messages.
1963 		csd_repairs_util.auto_update_ro_status(
1964                         p_api_version 	 => 1,
1965                         p_commit       	 => p_commit,
1966                         p_init_msg_list  => p_init_msg_list,
1967                         p_repair_line_id => p_repair_line_id,
1968                         x_return_status  => x_return_status,
1969                         x_msg_count      => x_msg_count,
1970                         x_msg_data       => x_msg_data,
1971                         p_event          => 'RECEIVE',
1972 						            p_validation_level => fnd_api.g_valid_level_none);
1973 	-- end bug#7497790, 12.1 FP, subhat.
1974 
1975   -- Standard check of p_commit.
1976   IF FND_API.To_Boolean( p_commit ) THEN
1977        COMMIT WORK;
1978   END IF;
1979 
1980   -- Standard call to get message count and IF count is  get message info.
1981   FND_MSG_PUB.Count_And_Get
1982            (p_count  =>  x_msg_count,
1983             p_data   =>  x_msg_data );
1984  EXCEPTION
1985     WHEN FND_API.G_EXC_ERROR THEN
1986           Debug('In FND_API.G_EXC_ERROR exception',l_mod_name,4 );
1987           -- As we are committing the processed records  in the inner APIs
1988 		-- so we rollback only if the p_commit='F'
1989 		IF NOT(FND_API.To_Boolean( p_commit )) THEN
1990               ROLLBACK TO RECEIPTS_UPDATE;
1991           END IF;
1992           x_return_status := FND_API.G_RET_STS_ERROR ;
1993           FND_MSG_PUB.Count_And_Get
1994                 (p_count  =>  x_msg_count,
1995                  p_data   =>  x_msg_data  );
1996     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1997           Debug('In FND_API.G_EXC_UNEXPECTED_ERROR exception',l_mod_name,4 );
1998           IF ( l_error_level >= G_debug_level)  THEN
1999               fnd_message.set_name('CSD','CSD_SQL_ERROR');
2000               fnd_message.set_token('SQLERRM',SQLERRM);
2001               fnd_message.set_token('SQLCODE',SQLCODE);
2002               FND_LOG.MESSAGE(l_error_level,l_mod_name,FALSE);
2003           END If;
2004           -- As we are committing the processed records  in the inner APIs
2005 		-- so we rollback only if the p_commit='F'
2006 		IF NOT(FND_API.To_Boolean( p_commit )) THEN
2007               ROLLBACK TO RECEIPTS_UPDATE;
2008           END IF;
2009           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
2010           FND_MSG_PUB.Count_And_Get
2011                 ( p_count  =>  x_msg_count,
2012                   p_data   =>  x_msg_data );
2013     WHEN OTHERS THEN
2014           Debug('In OTHERS exception',l_mod_name,4 );
2015           IF ( l_error_level >= G_debug_level)  THEN
2016               fnd_message.set_name('CSD','CSD_SQL_ERROR');
2017               fnd_message.set_token('SQLERRM',SQLERRM);
2018               fnd_message.set_token('SQLCODE',SQLCODE);
2019               FND_LOG.MESSAGE(l_error_level,l_mod_name,FALSE);
2020           END If;
2021           -- As we are committing the processed records  in the inner APIs
2022 		-- so we rollback only if the p_commit='F'
2023 		IF NOT(FND_API.To_Boolean( p_commit )) THEN
2024               ROLLBACK TO RECEIPTS_UPDATE;
2025           END IF;
2026 		x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
2027           IF  FND_MSG_PUB.Check_Msg_Level
2028                   (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
2029           THEN
2030               FND_MSG_PUB.Add_Exc_Msg
2031                   (G_PKG_NAME ,
2032                    l_api_name  );
2033           END IF;
2034           FND_MSG_PUB.Count_And_Get
2035                   (p_count  =>  x_msg_count,
2036                    p_data   =>  x_msg_data );
2037 END RECEIPTS_UPDATE;
2038 
2039 /*-------------------------------------------------------------------------------------*/
2040 /* Procedure name: RMA_RCV_UPDATE                                                      */
2041 /* Description   : Procedure called from the update API to update the depot tables     */
2042 /*                 for the receipts against RMA. It also logs activities for accept    */
2043 /*                 reject txn lines                                                    */
2044 /* Called from   : Called from RECEIPTS_UPDATE API                                     */
2045 /* STANDARD PARAMETERS                                                                 */
2046 /*  In Parameters :                                                                    */
2047 /*    p_api_version       NUMBER    Required  Api Version number                       */
2048 /*    p_init_msg_list     VARCHAR2  Optional  To Initialize message stack              */
2049 /*    p_commit            VARCHAR2  Optional  Commits in API                           */
2050 /*    p_validation_level  NUMBER    Optional  validation level                         */
2051 /*                                                                                     */
2052 /*  Output Parameters:                                                                 */
2053 /*   x_return_status     VARCHAR2      Return status of the API                        */
2054 /*   x_msg_count         NUMBER        Number of messages in stack                     */
2055 /*   x_msg_data          VARCHAR2      Error Message from message stack                */
2056 /*                                                                                     */
2057 /* NON-STANDARD PARAMETERS                                                             */
2058 /*   In Parameters                                                                     */
2059 /*    p_repair_line_id   NUMBER   Optional   Repair Order Line Id                      */
2060 /* Output Parameter :                                                                  */
2061 /* Change Hist :                                                                       */
2062 /*   09/20/03  vlakaman  Initial Creation.                                             */
2063 /*-------------------------------------------------------------------------------------*/
2064 
2065 Procedure RMA_RCV_UPDATE
2066         ( p_api_version          IN   NUMBER,
2067           p_commit               IN   VARCHAR2,
2068           p_init_msg_list        IN   VARCHAR2,
2069           p_validation_level     IN   NUMBER,
2070           x_return_status        OUT  NOCOPY  VARCHAR2,
2071           x_msg_count            OUT  NOCOPY  NUMBER,
2072           x_msg_data             OUT  NOCOPY  VARCHAR2,
2073           p_repair_line_id       IN   NUMBER,
2074           p_past_num_of_days     IN   NUMBER  DEFAULT NULL)  ----bug#6753684, 6742512
2075 IS
2076 
2077 
2078   -- Standard Variables
2079   l_api_name            CONSTANT VARCHAR2(30)   := 'RMA_RCV_UPDATE';
2080   l_api_version         CONSTANT NUMBER         := 1.0;
2081 
2082   -- Variables used in the program
2083   l_total_records       number;
2084   l_rep_hist_id         number;
2085   l_result_quantity     number;
2086   l_repair_line_id      NUMBER;
2087   l_prod_txn_id         NUMBER;
2088   l_accept_qty          NUMBER;
2089   l_reject_qty          NUMBER;
2090   l_st_serial_num       mtl_serial_numbers.serial_number%type;
2091   l_dummy               varchar2(30);
2092   l_lot_number          mtl_lot_numbers.lot_number%type;
2093   l_ro_reject_qty       NUMBER;
2094   l_commit_size         NUMBER := 500;
2095   l_srl_ctl_code        mtl_system_items.serial_number_control_code%type;
2096   l_lot_ctl_code        mtl_system_items.lot_control_code%type;
2097   l_ib_flag             mtl_system_items.comms_nl_trackable_flag%type;
2098   l_instance_id         csi_item_instances.instance_id%type;
2099   l_prod_txn_status     csd_product_transactions.prod_txn_status%type;
2100 
2101   -- activity record
2102   l_activity_rec      activity_rec_type;
2103 
2104 --bug#6753684, 6742512
2105   l_From_Date  Date;
2106   l_TO_Date    Date;
2107 
2108   -- Variables for FND Log
2109   l_error_level  number   := FND_LOG.LEVEL_ERROR;
2110   l_mod_name     varchar2(2000) := 'csd.plsql.csd_update_programs_pvt.rma_rcv_update';
2111 
2112   -- User defined exceptions
2113   SKIP_RECORD  EXCEPTION;
2114 
2115   CURSOR RECEIPT_LINES IS
2116   SELECT
2117        oeh.order_number rma_number,
2118        oeh.header_id rma_header_id,
2119        oel.line_id ,
2120        oel.line_number rma_line_number,
2121        oel.inventory_item_id,
2122        haou.name org_name,
2123        rcvt.organization_id,
2124        rcvt.unit_of_measure,
2125        rcvt.quantity received_quantity,
2126        rcvt.transaction_date received_date,
2127        rcvt.transaction_id,
2128        rcvt.subinventory,
2129        rcvt.locator_id,
2130        rcvt.transaction_type,
2131        cra.serial_number ro_serial_number,
2132        cra.repair_number,
2133        cra.unit_of_measure ro_uom,
2134        cra.inventory_item_id ro_item_id,
2135        cpt.product_transaction_id,
2136        cpt.repair_line_id,
2137        cpt.action_code,
2138        cpt.source_serial_number,
2139 	  cpt.source_instance_id,
2140 	  cpt.quantity_received prod_txn_recd_qty,
2141        abs(ced.quantity_required) estimate_quantity,
2142        ced.order_line_id est_order_line_id,
2143        ced.inventory_item_id prod_txn_item_id
2144   FROM csd_product_transactions cpt,
2145        cs_estimate_details ced,
2146        csd_repairs cra,
2147        rcv_transactions rcvt,
2148        oe_order_headers_all oeh,
2149        oe_order_lines_all oel,
2150        hr_all_organization_units haou
2151   WHERE cpt.action_type    in ('RMA', 'RMA_THIRD_PTY') -- excluded walk-in-receipt as it is going off
2152   AND   cpt.prod_txn_status    in (  'BOOKED', 'SUBMITTED')
2153   AND   ced.order_header_id is not null
2154   AND   rcvt.oe_order_line_id = ced.order_line_id    ----bug#6753684, 6742512
2155   AND   rcvt.oe_order_header_id = ced.order_header_id  ----bug#6753684, 6742512
2156   AND   ced.source_code        = 'DR'
2157   AND   ced.estimate_detail_id = cpt.estimate_detail_id
2158   AND   cra.repair_line_id     = cpt.repair_line_id
2159   AND   oeh.header_id          = ced.order_header_id
2160   AND   oel.header_id          = oeh.header_id
2161   AND   rcvt.oe_order_line_id  = oel.line_id
2162   AND   rcvt.transaction_type in ('DELIVER','ACCEPT','REJECT')
2163   AND   rcvt.source_document_code = 'RMA'
2164   AND   rcvt.organization_id   = haou.organization_id
2165   AND   NOT EXISTS
2166         (SELECT 'X'
2167          FROM  csd_repair_history crh
2168          WHERE crh.repair_line_id = cpt.repair_line_id
2169           AND  event_code         = decode(rcvt.transaction_type,
2170                                            'DELIVER','RR',
2171                                            'ACCEPT', 'IP',
2172                                            'REJECT','IP','')
2173           AND  paramn1            = rcvt.transaction_id)
2174 
2175   AND  ((ced.QUANTITY_REQUIRED < -1
2176 	           AND oel.line_id in ( Select line_id
2177                               from oe_order_lines_all oel1
2178              	               start with oel1.line_id = ced.order_line_id
2179      		                 connect by prior oel1.line_id = oel1.split_from_line_id
2180      		                 and oel1.shipped_quantity is not null
2181      		                 and oel1.header_id = oeh.header_id))
2182 			OR (ced.QUANTITY_REQUIRED = -1
2183 			    AND ced.ORDER_LINE_ID = oel.LINE_ID));
2184 
2185 /* Fixed bug#6753684,
2186 
2187 	New cursor added for batch processing.This cursor takes two parameter
2188 	p_from_date and p_to_date. These dates are compared with the creation date
2189 	of repair order. Only repair order created during this period are considered
2190 	for Depot Receipt Update to improve performance.
2191 */
2192   CURSOR RECEIPT_LINES_BY_DATE( p_from_Date Date, p_to_Date Date ) IS
2193   SELECT
2194        oeh.order_number rma_number,
2195        oeh.header_id rma_header_id,
2196        oel.line_id ,
2197        oel.line_number rma_line_number,
2198        oel.inventory_item_id,
2199        haou.name org_name,
2200        rcvt.organization_id,
2201        rcvt.unit_of_measure,
2202        rcvt.quantity received_quantity,
2203        rcvt.transaction_date received_date,
2204        rcvt.transaction_id,
2205        rcvt.subinventory,
2206        rcvt.locator_id,
2207        rcvt.transaction_type,
2208        cra.serial_number ro_serial_number,
2209        cra.repair_number,
2210        cra.unit_of_measure ro_uom,
2211        cra.inventory_item_id ro_item_id,
2212        cpt.product_transaction_id,
2213        cpt.repair_line_id,
2214        cpt.action_code,
2215        cpt.source_serial_number,
2216 	  cpt.source_instance_id,
2217 	  cpt.quantity_received prod_txn_recd_qty,
2218        abs(ced.quantity_required) estimate_quantity,
2219        ced.order_line_id est_order_line_id,
2220        ced.inventory_item_id prod_txn_item_id
2221   FROM csd_product_transactions cpt,
2222        cs_estimate_details ced,
2223        csd_repairs cra,
2224        rcv_transactions rcvt,
2225        oe_order_headers_all oeh,
2226        oe_order_lines_all oel,
2227        hr_all_organization_units haou
2228   WHERE cra.creation_date between p_from_date and p_to_date
2229   AND   cpt.action_type    in ('RMA', 'RMA_THIRD_PTY') -- excluded walk-in-receipt as it is going off
2230   AND   cpt.prod_txn_status    in (  'BOOKED', 'SUBMITTED')
2231   AND   ced.order_header_id is not null
2232   AND   rcvt.oe_order_line_id = ced.order_line_id    ----bug#6753684, 6742512
2233   AND   rcvt.oe_order_header_id = ced.order_header_id  ----bug#6753684, 6742512
2234   AND   ced.source_code        = 'DR'
2235   AND   ced.estimate_detail_id = cpt.estimate_detail_id
2236   AND   cra.repair_line_id     = cpt.repair_line_id
2237   AND   oeh.header_id          = ced.order_header_id
2238   AND   oel.header_id          = oeh.header_id
2239   AND   rcvt.oe_order_line_id  = oel.line_id
2240   AND   rcvt.transaction_type in ('DELIVER','ACCEPT','REJECT')
2241   AND   rcvt.source_document_code = 'RMA'
2242   AND   rcvt.organization_id   = haou.organization_id
2243   AND   NOT EXISTS
2244         (SELECT 'X'
2245          FROM  csd_repair_history crh
2246          WHERE crh.repair_line_id = cpt.repair_line_id
2247           AND  event_code         = decode(rcvt.transaction_type,
2248                                            'DELIVER','RR',
2249                                            'ACCEPT', 'IP',
2250                                            'REJECT','IP','')
2251           AND  paramn1            = rcvt.transaction_id)
2252 
2253   AND  ((ced.QUANTITY_REQUIRED < -1
2254 	           AND oel.line_id in ( Select line_id
2255                               from oe_order_lines_all oel1
2256              	               start with oel1.line_id = ced.order_line_id
2257      		                 connect by prior oel1.line_id = oel1.split_from_line_id
2258      		                 and oel1.shipped_quantity is not null
2259      		                 and oel1.header_id = oeh.header_id))
2260 			OR (ced.QUANTITY_REQUIRED = -1
2261 			    AND ced.ORDER_LINE_ID = oel.LINE_ID));
2262 
2263 
2264   CURSOR RECEIPT_LINES_RO(p_repair_line_id NUMBER) IS
2265   SELECT
2266      oeh.order_number rma_number,
2267      oeh.header_id rma_header_id,
2268      oel.line_id ,
2269      oel.line_number rma_line_number,
2270      oel.inventory_item_id,
2271      haou.name org_name,
2272      rcvt.organization_id,
2273      rcvt.unit_of_measure,
2274      rcvt.quantity received_quantity,
2275      rcvt.transaction_date received_date,
2276      rcvt.transaction_id,
2277      rcvt.subinventory,
2278      rcvt.locator_id,
2279      rcvt.transaction_type,
2280      cra.serial_number ro_serial_number,
2281      cra.repair_number,
2282      cra.unit_of_measure ro_uom,
2283      cra.inventory_item_id ro_item_id,
2284      cpt.product_transaction_id,
2285      cpt.repair_line_id,
2286      cpt.action_code,
2287      cpt.source_serial_number,
2288      cpt.source_instance_id,
2289      cpt.quantity_received prod_txn_recd_qty,
2290      abs(ced.quantity_required) estimate_quantity,
2291      ced.order_line_id est_order_line_id,
2292      ced.inventory_item_id prod_txn_item_id
2293  FROM hr_all_organization_units haou,
2294       csd_repairs cra,
2295       oe_order_headers_all oeh,
2296       oe_order_lines_all oel,
2297       rcv_transactions rcvt,
2298       cs_estimate_details ced,
2299       csd_product_transactions cpt
2300  WHERE cpt.repair_line_id = p_repair_line_id
2301        AND   cpt.action_type    in ('RMA', 'RMA_THIRD_PTY') -- excluded walk-in-receipt as it is going off
2302        AND   cpt.prod_txn_status    in (  'BOOKED', 'SUBMITTED')
2303        AND   ced.order_header_id is not null
2304        AND   rcvt.oe_order_line_id = ced.order_line_id       ----bug#6753684, 6742512
2305        AND   rcvt.oe_order_header_id = ced.order_header_id   ----bug#6753684, 6742512
2306        AND   ced.source_code        = 'DR'
2307        AND   ced.estimate_detail_id = cpt.estimate_detail_id
2308        AND   cra.repair_line_id     = cpt.repair_line_id
2309        AND   oeh.header_id          = ced.order_header_id
2310        AND   oel.header_id          = oeh.header_id
2311        AND   rcvt.oe_order_line_id  = oel.line_id
2312        AND   rcvt.transaction_type in ('DELIVER','ACCEPT','REJECT')
2313        AND   rcvt.source_document_code = 'RMA'
2314        AND   rcvt.organization_id   = haou.organization_id
2315        AND   NOT EXISTS
2316              (SELECT 'X'
2317               FROM  csd_repair_history crh
2318               WHERE crh.repair_line_id = cpt.repair_line_id
2319                AND  event_code         = decode(rcvt.transaction_type,
2320                                                 'DELIVER','RR',
2321                                                 'ACCEPT', 'IP',
2322                                                 'REJECT','IP','')
2323                AND  paramn1            = rcvt.transaction_id)
2324 
2325 
2326        AND  ((ced.QUANTITY_REQUIRED < -1
2327 	           AND oel.line_id in ( Select line_id
2328                               from oe_order_lines_all oel1
2329              	               start with oel1.line_id = ced.order_line_id
2330      		                 connect by prior oel1.line_id = oel1.split_from_line_id
2331      		                 and oel1.shipped_quantity is not null
2332      		                 and oel1.header_id = oeh.header_id))
2333 			OR (ced.QUANTITY_REQUIRED = -1
2334 			    AND ced.ORDER_LINE_ID = oel.LINE_ID));
2335 
2336   I RCPT_LINES_Rec_Type;
2337 
2338 
2339  --- cursor for Cancelled orders...
2340  CURSOR Cur_Cancelled_repair_lines IS
2341  SELECT cra.REPAIR_LINE_ID
2342  FROM csd_repairs cra,
2343       cs_estimate_details ced,
2344       csd_product_transactions cpt
2345  WHERE  cpt.action_type    in ('RMA', 'RMA_THIRD_PTY')
2346        AND   cpt.prod_txn_status    in (  'BOOKED', 'SUBMITTED')
2347        AND   ced.order_header_id is not null
2348        AND   ced.source_code        = 'DR'
2349        AND   ced.estimate_detail_id = cpt.estimate_detail_id
2350        AND   cra.repair_line_id     = cpt.repair_line_id;
2351 
2352 
2353   -- Cursor that gets the serial number and lot number
2354   -- for the rcv txn id
2355   Cursor rcv_txn_serial_num (p_txn_id in number) is
2356   select rst.serial_num,
2357          rst.lot_num
2358   from rcv_serial_transactions rst,
2359        rcv_transactions rt
2360   where rt.transaction_id   = p_txn_id
2361    and  rst.transaction_id  = rt.transaction_id;
2362 
2363 
2364 BEGIN
2365    -- Standard Start of API savepoint
2366    SAVEPOINT  RMA_RCV_UPDATE;
2367 
2368    -- Initialize API return status to success
2369    x_return_status := FND_API.G_RET_STS_SUCCESS;
2370 
2371    -- Debug messages
2372    Debug('At the Beginning of RMA_RCV_UPDATE',l_mod_name,1);
2373    Debug('Repair Line Id ='||to_char(p_repair_line_id),l_mod_name,1);
2374 
2375    -- Standard call to check for call compatibility.
2376    IF NOT FND_API.Compatible_API_Call (l_api_version,
2377                                        p_api_version,
2378                                        l_api_name   ,
2379                                        G_PKG_NAME   )
2380        THEN
2381           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2382    END IF;
2383 
2384    -- Initialize message list if p_init_msg_list is set to TRUE.
2385    IF FND_API.to_Boolean( p_init_msg_list ) THEN
2386          FND_MSG_PUB.initialize;
2387    END IF;
2388 
2389    -- Api body starts
2390 
2391    -- Validate the repair line id
2392    -- bugfix 4423818 : We need to validate only if p_repair_line_id is NOT NULL
2393    IF(p_repair_line_id is NOT NULL ) THEN
2394      IF NOT(csd_process_util.validate_rep_line_id(p_repair_line_id)) THEN
2395         RAISE FND_API.G_EXC_ERROR;
2396      END IF;
2397    END IF;
2398 
2399    -- Assign the number of processed records in this variable
2400    l_total_records := 0;
2401 
2402    -- Selects all the receipts lines that have accept,reject,deliver
2403    -- transaction type
2404    -- 1. Serialized
2405    --  BEFORE RECEIVING
2406    --  Repair Order
2407    --    RO NUM  RO Type  Qty    SN   rcvd Qty
2408    --      R1      RR      1     SN1
2409    --  Product Txn
2410    --   Prod Txn   RO Num   Est Line  Est Qty   SN   Subinv   rcvd Qty
2411    --     P1        R1         C1       1       SN1
2412    --
2413    -- AFTER RECEIVING and running the update program
2414    --  Repair Order
2415    --    RO NUM  RO Type  Qty   SN   rcvd Qty
2416    --      R1      RR      1    SN9       1
2417    --
2418    --  RCV Lines
2419    --   Txn Id  Txn Type  Parent Txn Id  Ord Line  Qty  SN    Subinv
2420    --     T1     ACCEPT                    L1
2421    --     T2     DELIVER      T1           L1       1   SN9    FGI
2422    --
2423    --  Product Txn
2424    --   Prod Txn   RO Num   Est Line  Est Qty   SN     Subinv  Rcvd Qty
2425    --     P1        R1         C1       1       SN9    FGI        1
2426    --
2427    -- 2. Non-Serialized
2428    --  BEFORE RECEIVING
2429    --  Repair Order
2430    --    RO NUM  RO Type  Qty    SN   rcvd Qty
2431    --      R1      RR      3
2432    --  Product Txn
2433    --   Prod Txn   RO Num   Est Line  Est Qty   SN   Subinv    rcvd Qty
2434    --     P1        R1         C1       3
2435    -- AFTER SHIPMENT and running the update program
2436    --  Repair Order
2437    --    RO NUM  RO Type  Qty   SN   rcvd Qty  rejected Qty
2438    --      R1      RR      3            2         1
2439    --
2440    --  RCV Lines
2441    --   Txn Id  Txn Type  Parent Txn Id  Ord Line  Qty  SN    Subinv
2442    --     T1     ACCEPT                    L1
2443    --     T2     DELIVER      T1           L1       2               FGI
2444    --     T3     REJECT                    L1       1
2445    --
2446    --  Product Txn
2447    --   Prod Txn   RO Num   Est Line  Est Qty   SN     Subinv  rcvd Qty
2448    --     P1        R1         C1       3               FGI        2
2449    --
2450 
2451    IF(p_repair_line_id is null) THEN
2452         If (p_past_num_of_days Is Null) Then
2453             OPEN RECEIPT_LINES;
2454         Else
2455             l_From_Date := sysdate - (p_past_num_of_days +1 ) ;
2456             l_TO_Date   := Sysdate + 1;
2457             OPEN RECEIPT_LINES_BY_DATE(l_From_Date , l_To_Date);        ----bug#6753684, 6742512
2458         End If;
2459    ELSE
2460         OPEN RECEIPT_LINES_RO(p_repair_line_id);
2461    END IF;
2462 
2463 
2464    LOOP
2465 
2466     IF(p_repair_line_id is null) THEN
2467         If (p_past_num_of_days Is Null) Then
2468             FETCH RECEIPT_LINES INTO I;
2469             exit when RECEIPT_LINES%NOTFOUND;
2470         else
2471             FETCH RECEIPT_LINES_BY_DATE INTO I;             ----bug#6753684, 6742512
2472             exit when RECEIPT_LINES_BY_DATE%NOTFOUND;
2473         end if;
2474     ELSE
2475         FETCH RECEIPT_LINES_RO INTO I;
2476         exit when RECEIPT_LINES_RO%NOTFOUND;
2477     END IF;
2478 
2479 
2480     BEGIN
2481      -- savepoint
2482      SAVEPOINT RECEIPT_LINES;
2483 
2484      IF I.transaction_type = 'ACCEPT' THEN
2485         -- Log activities for the accept transaction
2486 
2487          l_accept_qty := I.received_quantity;
2488          l_reject_qty := 0;
2489 
2490          -- Initialize the activity rec
2491          l_activity_rec := INIT_ACTIVITY_REC ;
2492 
2493          -- Assign the values for activity record
2494          l_activity_rec.REPAIR_HISTORY_ID := l_rep_hist_id;
2495          l_activity_rec.REPAIR_LINE_ID := I.repair_line_id;
2496          l_activity_rec.EVENT_CODE     := 'IP';
2497          l_activity_rec.ACTION_CODE    := 0;
2498          l_activity_rec.EVENT_DATE     := I.received_date;
2499          l_activity_rec.QUANTITY       := null;
2500          l_activity_rec.PARAMN1        := I.transaction_id;
2501          l_activity_rec.PARAMN2        := I.rma_header_id;
2502          l_activity_rec.PARAMN3        := l_accept_qty;
2503          l_activity_rec.PARAMN4        := l_reject_qty;
2504          l_activity_rec.PARAMN5        := I.line_id;
2505          l_activity_rec.PARAMC1        := I.rma_number;
2506          l_activity_rec.PARAMC2        := I.subinventory;
2507          l_activity_rec.OBJECT_VERSION_NUMBER := null;
2508 
2509          -- Debug Messages
2510          Debug('Calling LOG_ACTIVITY',l_mod_name,2);
2511 
2512          -- Calling LOG_ACTIVITY for logging activity
2513          -- accept receiving txn
2514          LOG_ACTIVITY
2515               ( p_api_version     => p_api_version,
2516                 p_commit          => p_commit,
2517                 p_init_msg_list   => p_init_msg_list,
2518                 p_validation_level => p_validation_level,
2519                 x_return_status   => x_return_status,
2520                 x_msg_count       => x_msg_count,
2521                 x_msg_data        => x_msg_data,
2522                 p_activity_rec    => l_activity_rec );
2523 
2524          -- Debug Messages
2525         Debug('Return Status from LOG_ACTIVITY:'||x_return_status,l_mod_name,2);
2526         IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
2527              Debug('LOG_ACTIVITY api failed ',l_mod_name,4);
2528              RAISE FND_API.G_EXC_ERROR;
2529         END IF;
2530 
2531       ELSIF I.transaction_type = 'REJECT' then
2532          -- Log activities for the reject transactions
2533 
2534           l_accept_qty := 0;
2535           l_reject_qty := I.received_quantity;
2536 
2537           -- Initialize the activity rec
2538           l_activity_rec := INIT_ACTIVITY_REC ;
2539 
2540           -- Assign the values for activity record
2541           l_activity_rec.REPAIR_HISTORY_ID := l_rep_hist_id;
2542           l_activity_rec.REPAIR_LINE_ID := I.repair_line_id;
2543           l_activity_rec.EVENT_CODE     := 'IP';
2544           l_activity_rec.ACTION_CODE    := 0;
2545           l_activity_rec.EVENT_DATE     := I.received_date;
2546           l_activity_rec.QUANTITY       := null;
2547           l_activity_rec.PARAMN1        := I.transaction_id;
2548           l_activity_rec.PARAMN2        := I.rma_header_id;
2549           l_activity_rec.PARAMN3        := l_accept_qty;
2550           l_activity_rec.PARAMN4        := l_reject_qty;
2551           l_activity_rec.PARAMN5        := I.line_id;
2552           l_activity_rec.PARAMC1        := I.rma_number;
2553           l_activity_rec.PARAMC2        := I.subinventory;
2554           l_activity_rec.OBJECT_VERSION_NUMBER := null;
2555 
2556          -- Debug Messages
2557          Debug('Calling LOG_ACTIVITY',l_mod_name,2);
2558 
2559          -- Calling LOG_ACTIVITY for logging activity
2560          -- reject receiving transactions
2561          LOG_ACTIVITY
2562               ( p_api_version     => p_api_version,
2563                 p_commit          => p_commit,
2564                 p_init_msg_list   => p_init_msg_list,
2565                 p_validation_level => p_validation_level,
2566                 x_return_status   => x_return_status,
2567                 x_msg_count       => x_msg_count,
2568                 x_msg_data        => x_msg_data,
2569                 p_activity_rec    => l_activity_rec );
2570 
2571          -- Debug Messages
2572         Debug('Return Status from LOG_ACTIVITY:'||x_return_status,l_mod_name,2);
2573         IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
2574              Debug('LOG_ACTIVITY api failed ',l_mod_name,4);
2575              RAISE FND_API.G_EXC_ERROR;
2576         END IF;
2577 
2578       ElSIF I.transaction_type = 'DELIVER' then
2579          --Log activities for the deliver transaction
2580 	    --Log actvities for the receipts of the item and the received serial number does
2581          --not match with the one on the product txns
2582 
2583         --Debug messages
2584         Debug('Repair_Line_id ='||to_char(i.repair_line_id),l_mod_name,1);
2585         Debug('Rma Number     ='||I.rma_number,l_mod_name,1);
2586         Debug('Transaction_id ='||to_char(i.transaction_id),l_mod_name,1);
2587         Debug('Inv_Item id    ='||to_char(i.inventory_item_id),l_mod_name,1);
2588         Debug('Receiving Org  ='||to_char(i.organization_id),l_mod_name,1);
2589 
2590         -- Check if the item is serialized or non-serialized
2591         Begin
2592            select serial_number_control_code,
2593                 lot_control_code,
2594                 comms_nl_trackable_flag
2595  	      into l_srl_ctl_code,
2596                 l_lot_ctl_code,
2597                 l_ib_flag
2598 	      from mtl_system_items
2599 	      where inventory_item_id  = i.inventory_item_id
2600 	       and  organization_id    = i.organization_id;
2601         Exception
2602           When no_data_found then
2603             IF ( l_error_level >= G_debug_level) THEN
2604                fnd_message.set_name('CSD','CSD_INV_ITEM_ID');
2605                fnd_message.set_token('ITEM_ID',I.inventory_item_id);
2606                FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
2607             ELSE
2608                fnd_message.set_name('CSD','CSD_INV_ITEM_ID');
2609                fnd_message.set_token('ITEM_ID',I.inventory_item_id);
2610                fnd_msg_pub.add;
2611             END IF;
2612             RAISE FND_API.G_EXC_ERROR;
2613           When others then
2614             Debug('In Others exception',l_mod_name,4);
2615             RAISE FND_API.G_EXC_ERROR;
2616        End;
2617 
2618 	  Debug('Serial Num Ctl Code  ='||TO_CHAR(l_srl_ctl_code),l_mod_name,1);
2619 	  Debug('Lot Ctl Code         ='||TO_CHAR(l_lot_ctl_code),l_mod_name,1);
2620 	  Debug('IB Flag              ='||l_ib_flag,l_mod_name,1);
2621 
2622        IF l_srl_ctl_code in (2,5,6) THEN
2623 
2624          -- Item is serialized
2625          -- Opening the cursor for getting the received
2626          -- serial number
2627          Open rcv_txn_serial_num(i.transaction_id );
2628          Fetch rcv_txn_serial_num into l_st_serial_num, l_lot_number;
2629          IF (rcv_txn_serial_num%NOTFOUND) THEN
2630              IF ( l_error_level >= G_debug_level)  THEN
2631                  FND_MESSAGE.SET_NAME('CSD','CSD_SERIAL_NUM_MISSING');
2632                  FND_MESSAGE.SET_TOKEN('TXN_ID',i.transaction_id);
2633                  FND_LOG.MESSAGE(l_error_level,l_mod_name,FALSE);
2634              ELSE
2635                  FND_MESSAGE.SET_NAME('CSD','CSD_SERIAL_NUM_MISSING');
2636                  FND_MESSAGE.SET_TOKEN('TXN_ID',i.transaction_id);
2637                  FND_MSG_PUB.ADD;
2638              END IF;
2639              RAISE FND_API.G_EXC_ERROR;
2640          END IF;
2641 	    -- Close the cursor
2642          If rcv_txn_serial_num %isopen then
2643              close rcv_txn_serial_num ;
2644          End if;
2645 
2646          Debug('Rcv  Txn  Serial_num  ='||l_st_serial_num,l_mod_name,1);
2647          Debug('Prod Txn  serial_num  ='||I.SOURCE_SERIAL_NUMBER,l_mod_name,1);
2648          Debug('Lot number            ='||l_lot_number,l_mod_name,1);
2649 
2650          -- Get the instance Id from the rcvd serial number
2651          IF NVL(l_ib_flag,'N') = 'Y' THEN
2652 	     BEGIN
2653             Select instance_id
2654             into   l_instance_id
2655             from  csi_item_instances
2656             where inventory_item_id = I.inventory_item_id
2657              and  serial_number     = l_st_serial_num;
2658 		   --and  instance_usage_code = 'IN_INVENTORY';
2659           EXCEPTION
2660            WHEN NO_DATA_FOUND THEN
2661              IF ( l_error_level >= G_debug_level)  THEN
2662                  FND_MESSAGE.SET_NAME('CSD','CSD_INSTANCE_MISSING');
2663                  FND_MESSAGE.SET_TOKEN('SERIAL_NUM',l_st_serial_num);
2664                  FND_LOG.MESSAGE(l_error_level,l_mod_name,FALSE);
2665              ELSE
2666                  FND_MESSAGE.SET_NAME('CSD','CSD_INSTANCE_MISSING');
2667                  FND_MESSAGE.SET_TOKEN('SERIAL_NUM',l_st_serial_num);
2668                  FND_MSG_PUB.ADD;
2669              END IF;
2670              RAISE FND_API.G_EXC_ERROR;
2671 	       Debug(' Could not find any IB instance for the Serial Num ='||l_st_serial_num, l_mod_name,1);
2672            WHEN TOO_MANY_ROWS THEN
2673              IF ( l_error_level >= G_debug_level)  THEN
2674                  FND_MESSAGE.SET_NAME('CSD','CSD_FOUND_MANY_INSTANCE');
2675                  FND_MESSAGE.SET_TOKEN('SERIAL_NUM',l_st_serial_num);
2676                  FND_LOG.MESSAGE(l_error_level,l_mod_name,FALSE);
2677              ELSE
2678                  FND_MESSAGE.SET_NAME('CSD','CSD_FOUND_MANY_INSTANCE');
2679                  FND_MESSAGE.SET_TOKEN('SERIAL_NUM',l_st_serial_num);
2680                  FND_MSG_PUB.ADD;
2681              END IF;
2682              RAISE FND_API.G_EXC_ERROR;
2683 	       Debug(' Found many IB instance for the Serial Num ='||l_st_serial_num, l_mod_name,1);
2684           END;
2685          ELSE
2686              l_instance_id := NULL;
2687 	   END IF;
2688 
2689          -- If the item on prod txn is same as the order line and
2690          -- the serial number does not match with the received serial number
2691          -- then log activity for serial number mismatch
2692          IF (l_st_serial_num <> I.SOURCE_SERIAL_NUMBER) AND
2693             (I.prod_txn_item_id = I.inventory_item_id) THEN
2694 
2695                 -- Initialize the activity rec
2696                 l_activity_rec := INIT_ACTIVITY_REC ;
2697 
2698                 -- Assign the values for activity record
2699                 l_activity_rec.REPAIR_HISTORY_ID := l_rep_hist_id;
2700                 l_activity_rec.REPAIR_LINE_ID := I.repair_line_id;
2701                 l_activity_rec.EVENT_CODE     := 'RSC';
2702                 l_activity_rec.ACTION_CODE    := 0;
2703                 l_activity_rec.EVENT_DATE     := I.received_date;
2704                 l_activity_rec.QUANTITY       := null;
2705                 l_activity_rec.PARAMN1        := I.transaction_id;
2706                 l_activity_rec.PARAMN2        := i.rma_line_number;
2707                 l_activity_rec.PARAMN6        := I.rma_header_id;
2708                 l_activity_rec.PARAMC1        := I.subinventory;
2709                 l_activity_rec.PARAMC2        := I.rma_number;
2710                 l_activity_rec.PARAMC3        := I.source_serial_number; -- prod txn serial num
2711                 l_activity_rec.PARAMC4        := l_st_serial_num;        -- rcvd serial num
2712                 l_activity_rec.OBJECT_VERSION_NUMBER := null;
2713 
2714                 -- Debug Messages
2715                 Debug('Calling LOG_ACTIVITY',l_mod_name,2);
2716 
2717                 -- Calling LOG_ACTIVITY for logging activity
2718                 -- Serial number mismatch
2719                 LOG_ACTIVITY
2720                        ( p_api_version     => p_api_version,
2721                          p_commit          => p_commit,
2722                          p_init_msg_list   => p_init_msg_list,
2723                          p_validation_level => p_validation_level,
2724                          x_return_status   => x_return_status,
2725                          x_msg_count       => x_msg_count,
2726                          x_msg_data        => x_msg_data,
2727                          p_activity_rec    => l_activity_rec );
2728 
2729                 -- Debug Messages
2730                 Debug('Return Status from LOG_ACTIVITY:'||x_return_status,l_mod_name,2);
2731                 IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
2732                    Debug('LOG_ACTIVITY api failed ',l_mod_name,4);
2733                    RAISE FND_API.G_EXC_ERROR;
2734                 END IF;
2735          End IF;
2736 
2737        ELSE
2738            -- Non-Serialized item
2739            l_st_serial_num := NULL;
2740 
2741 		 IF nvl(l_ib_flag,'N')= 'Y' THEN
2742 		    l_instance_id   := I.source_instance_id;
2743 		 Else
2744 		    l_instance_id   := NULL;
2745            End if;
2746 
2747            --lot_control_code = 1 No control
2748            --lot_control_code = 2 Full control
2749            IF l_lot_ctl_code = 2 THEN
2750               BEGIN
2751                  Select lot_num
2752                  into  l_lot_number
2753                  from  rcv_lot_transactions
2754                  where source_transaction_id = I.transaction_id;
2755               EXCEPTION
2756                 WHEN NO_DATA_FOUND THEN
2757                   IF ( l_error_level >= G_debug_level) THEN
2758                       fnd_message.set_name('CSD','CSD_INV_ITEM_ID');
2759                       fnd_message.set_token('ITEM_ID',I.inventory_item_id);
2760                       FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
2761                   ELSE
2762                       fnd_message.set_name('CSD','CSD_INV_ITEM_ID');
2763                       fnd_message.set_token('ITEM_ID',I.inventory_item_id);
2764                       fnd_msg_pub.add;
2765                   END IF;
2766                   RAISE FND_API.G_EXC_ERROR;
2767               END;
2768            ELSE
2769               l_lot_number := NULL;
2770            END IF;
2771        END IF; -- End of logging activity for SN change
2772 
2773 
2774        -- Only if the item on RO matches with the item
2775        -- on order_line_id, call the convert api
2776        IF I.ro_item_id = I.inventory_item_id THEN
2777 
2778           Debug('Calling CONVERT_TO_RO_UOM ',l_mod_name,2);
2779           -- Converting the received qty to UOM
2780           -- on the repair order
2781           CONVERT_TO_RO_UOM
2782             ( x_return_status   => x_return_status
2783              ,p_to_uom_code     => i.ro_uom
2784              ,p_item_id         => i.inventory_item_id
2785              ,p_from_uom        => i.unit_of_measure
2786              ,p_from_uom_code   => NULL
2787              ,p_from_quantity   => i.received_quantity
2788              ,x_result_quantity => l_result_quantity);
2789 
2790           Debug('Return Status from CONVERT_TO_RO_UOM '||x_return_status,l_mod_name,2);
2791           IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
2792              Debug('CONVERT_TO_RO_UOM failed ',l_mod_name,4);
2793              RAISE FND_API.G_EXC_ERROR;
2794           END IF;
2795        Else
2796           l_result_quantity := NVL(i.received_quantity,0);
2797        END IF;
2798        Debug('l_result_quantity='||TO_CHAR(l_result_quantity),l_mod_name,1);
2799 
2800        -- Update csd_repairs only if the action code is
2801 	  -- cust_prod or exchange
2802        IF I.action_code in ( 'CUST_PROD','EXCHANGE') then
2803 
2804 	    IF I.ro_item_id = I.inventory_item_id THEN
2805 	     -- Update the serial num and instance id if the item on ro
2806 	     -- is same as prod txn
2807           update csd_repairs
2808           set quantity_rcvd = nvl(quantity_rcvd,0)+ l_result_quantity,
2809 		 object_version_number = object_version_number+1,
2810              customer_product_id = l_instance_id,
2811              serial_number = l_st_serial_num,
2812              last_update_date = sysdate,
2813              last_updated_by  = fnd_global.user_id,
2814              last_update_login = fnd_global.login_id
2815           where repair_line_id = I.repair_line_id;
2816           IF SQL%NOTFOUND THEN
2817             IF ( l_error_level >= G_debug_level) THEN
2818                fnd_message.set_name('CSD','CSD_RO_UPD_FAILED');
2819                fnd_message.set_token('REPAIR_LINE_ID',I.repair_line_id);
2820                FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
2821             ELSE
2822                fnd_message.set_name('CSD','CSD_RO_UPD_FAILED');
2823                fnd_message.set_token('REPAIR_LINE_ID',I.repair_line_id);
2824                fnd_msg_pub.add;
2825             END IF;
2826             RAISE FND_API.G_EXC_ERROR;
2827           END IF;
2828 
2829 	    ELSE
2830 	     -- Update the qty if the item on ro
2831 	     -- is not same as prod txn
2832           update csd_repairs
2833           set quantity_rcvd = nvl(quantity_rcvd,0)+ l_result_quantity,
2834 		    object_version_number = object_version_number+1,
2835               last_update_date = sysdate,
2836               last_updated_by  = fnd_global.user_id,
2837               last_update_login = fnd_global.login_id
2838           where repair_line_id = I.repair_line_id;
2839           IF SQL%NOTFOUND THEN
2840             IF ( l_error_level >= G_debug_level) THEN
2841                fnd_message.set_name('CSD','CSD_RO_UPD_FAILED');
2842                fnd_message.set_token('REPAIR_LINE_ID',I.repair_line_id);
2843                FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
2844             ELSE
2845                fnd_message.set_name('CSD','CSD_RO_UPD_FAILED');
2846                fnd_message.set_token('REPAIR_LINE_ID',I.repair_line_id);
2847                fnd_msg_pub.add;
2848             END IF;
2849             RAISE FND_API.G_EXC_ERROR;
2850           END IF;
2851 
2852 	    END IF;
2853        End If;
2854 
2855       IF  nvl(I.prod_txn_recd_qty,0) + nvl(I.received_quantity,0) = I.estimate_quantity THEN
2856         l_prod_txn_status := 'RECEIVED';
2857   	 ELSE
2858         l_prod_txn_status := 'BOOKED';
2859 	 END IF;
2860 
2861       -- Update the quantity received,locator id, lot number
2862       Update csd_product_transactions
2863       set sub_inventory = I.subinventory,
2864           locator_id    = I.locator_id,
2865           lot_number_rcvd      = l_lot_number,
2866           source_instance_id   = l_instance_id,
2867           source_serial_number = l_st_serial_num,
2868           quantity_received = nvl(quantity_received,0) + nvl(I.received_quantity,0),
2869 		prod_txn_status   = l_prod_txn_status,
2870 		object_version_number = object_version_number+1,
2871           last_update_date = sysdate,
2872           last_updated_by  = fnd_global.user_id,
2873           last_update_login = fnd_global.login_id
2874       WHERE  product_transaction_id = i.product_transaction_id;
2875       IF SQL%NOTFOUND THEN
2876          IF ( l_error_level >= G_debug_level) THEN
2877               fnd_message.set_name('CSD','CSD_PROD_TXN_UPD_FAILED');
2878               fnd_message.set_token('PROD_TXN_ID',I.product_transaction_id);
2879               FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
2880          ELSE
2881               fnd_message.set_name('CSD','CSD_PROD_TXN_UPD_FAILED');
2882               fnd_message.set_token('PROD_TXN_ID',I.product_transaction_id);
2883               fnd_msg_pub.add;
2884          END IF;
2885          RAISE FND_API.G_EXC_ERROR;
2886       END IF;
2887 
2888       fnd_message.set_name('CSD','CSD_DRC_RMA_RECEIPT');
2889       fnd_message.set_token('RMA_NO',i.rma_number);
2890       fnd_message.set_token('REP_NO',i.repair_number);
2891       fnd_message.set_token('QTY_RCVD',to_char(i.received_quantity));
2892       FND_MSG_PUB.ADD;
2893 
2894       -- Debug message
2895       Debug(fnd_message.get,l_mod_name,1);
2896 
2897       -- Log messages in concurrent log and output file
2898       fnd_file.put_line(fnd_file.log, fnd_message.get);
2899 
2900       -- Initialize the activity rec
2901       l_activity_rec := INIT_ACTIVITY_REC ;
2902 
2903       -- Assign the values for activity record
2904       l_activity_rec.REPAIR_HISTORY_ID := l_rep_hist_id;
2905       l_activity_rec.REPAIR_LINE_ID := I.repair_line_id;
2906       l_activity_rec.EVENT_CODE     := 'RR';
2907       l_activity_rec.ACTION_CODE    := 0;
2908       l_activity_rec.EVENT_DATE     := I.received_date;
2909       l_activity_rec.QUANTITY       := I.received_quantity;
2910       l_activity_rec.PARAMN1        := I.transaction_id;
2911       l_activity_rec.PARAMN2        := i.rma_line_number;
2912       l_activity_rec.PARAMN3        := i.organization_id;
2913       l_activity_rec.PARAMN6        := I.rma_header_id;
2914       l_activity_rec.PARAMC1        := I.subinventory;
2915       l_activity_rec.PARAMC2        := I.rma_number;
2916       l_activity_rec.PARAMC3        := I.org_name;
2917       l_activity_rec.OBJECT_VERSION_NUMBER := null;
2918 
2919       -- Debug Messages
2920       Debug('Calling LOG_ACTIVITY',l_mod_name,2);
2921 
2922       -- Calling LOG_ACTIVITY for logging activity
2923       -- receipt of the item
2924       LOG_ACTIVITY
2925            ( p_api_version     => p_api_version,
2926              p_commit          => p_commit,
2927              p_init_msg_list   => p_init_msg_list,
2928              p_validation_level => p_validation_level,
2929              x_return_status   => x_return_status,
2930              x_msg_count       => x_msg_count,
2931              x_msg_data        => x_msg_data,
2932              p_activity_rec    => l_activity_rec );
2933 
2934        -- Debug Messages
2935        Debug('Return Status from LOG_ACTIVITY:'||x_return_status,l_mod_name,2);
2936        IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
2937              Debug('LOG_ACTIVITY api failed ',l_mod_name,4);
2938              RAISE FND_API.G_EXC_ERROR;
2939        END IF;
2940      END IF;
2941 
2942      -- Commit for every 500 records
2943      -- one should COMMIT less frequently within a PL/SQL loop to
2944      -- prevent ORA-1555 (Snapshot too old) errors
2945      l_total_records := l_total_records + 1;
2946      IF mod(l_total_records, l_commit_size) = 0 THEN -- Commit every 500 records
2947          IF FND_API.To_Boolean( p_commit ) THEN
2948              COMMIT WORK;
2949          END IF;
2950      END IF;
2951     EXCEPTION
2952        WHEN FND_API.G_EXC_ERROR THEN
2953            ROLLBACK TO RECEIPT_LINES;
2954            x_return_status := FND_API.G_RET_STS_ERROR ;
2955            EXIT;
2956            -- In case of error, exit the loop. Commit the processed records
2957            -- and rollback the error record
2958            -- RAISE FND_API.G_EXC_ERROR;
2959        WHEN SKIP_RECORD THEN
2960            NULL;
2961     END;
2962   END LOOP;
2963 
2964 
2965   ------------ process cancelled orders.
2966    Debug('processing cancelled orders in RMA_RCV_UPDATE',l_mod_name,1);
2967   if(p_repair_line_id is not null) then
2968      Debug('processing repairline['||p_repair_line_id||']',l_mod_name,1);
2969   	Check_for_Cancelled_order(p_repair_line_id);
2970   else
2971   	FOR Repln_Rec in Cur_Cancelled_repair_lines
2972 	LOOP
2973           Debug('processing repairline['||repln_rec.repair_line_id||']',l_mod_name,1);
2974 		check_for_cancelled_order(Repln_rec.Repair_line_id);
2975 	END LOOP;
2976   End if;
2977    Debug('At the end of processing cancelled orders in RMA_RCV_UPDATE',l_mod_name,1);
2978   -----------------------
2979 
2980 
2981  --Added by Vijay 11/4/04
2982   IF(RECEIPT_LINES%ISOPEN) THEN
2983       CLOSE RECEIPT_LINES;
2984   END IF;
2985   IF(RECEIPT_LINES_RO%ISOPEN) THEN
2986       CLOSE RECEIPT_LINES_RO;
2987   END IF;
2988 
2989   IF(RECEIPT_LINES_BY_DATE%ISOPEN) THEN
2990       CLOSE RECEIPT_LINES_BY_DATE;
2991   END IF;
2992 
2993   -- Log seed messages for the number of records
2994   -- processed by rma update
2995   fnd_message.set_name('CSD','CSD_DRC_RMA_TOT_REC_PROC');
2996   fnd_message.set_token('TOT_REC',to_char(l_total_records));
2997   FND_MSG_PUB.ADD;
2998 
2999   -- Debug Messages
3000   Debug(fnd_message.get,l_mod_name,1);
3001 
3002   -- Log the number of records processed in concurrent
3003   -- program output and log file
3004   fnd_file.put_line(fnd_file.log,fnd_message.get);
3005   fnd_file.put_line(fnd_file.output,fnd_message.get);
3006 
3007   -- Standard check of p_commit.
3008   IF FND_API.To_Boolean( p_commit ) THEN
3009        COMMIT WORK;
3010   END IF;
3011 
3012   -- Standard call to get message count and IF count is  get message info.
3013   FND_MSG_PUB.Count_And_Get
3014            (p_count  =>  x_msg_count,
3015             p_data   =>  x_msg_data  );
3016  EXCEPTION
3017     WHEN FND_API.G_EXC_ERROR THEN
3018           Debug('In FND_API.G_EXC_ERROR exception',l_mod_name,4 );
3019           ROLLBACK TO RMA_RCV_UPDATE;
3020           x_return_status := FND_API.G_RET_STS_ERROR ;
3021           FND_MSG_PUB.Count_And_Get
3022                 (p_count  =>  x_msg_count,
3023                  p_data   =>  x_msg_data  );
3024     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3025           Debug('In FND_API.G_EXC_UNEXPECTED_ERROR exception',l_mod_name,4 );
3026           ROLLBACK TO RMA_RCV_UPDATE;
3027           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
3028           FND_MSG_PUB.Count_And_Get
3029                 ( p_count  =>  x_msg_count,
3030                   p_data   =>  x_msg_data );
3031     WHEN OTHERS THEN
3032           Debug('In OTHERS exception',l_mod_name,4 );
3033           ROLLBACK TO RMA_RCV_UPDATE;
3034           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
3035           IF  FND_MSG_PUB.Check_Msg_Level
3036                   (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
3037           THEN
3038               FND_MSG_PUB.Add_Exc_Msg
3039                   (G_PKG_NAME ,
3040                    l_api_name  );
3041           END IF;
3042           FND_MSG_PUB.Count_And_Get
3043                   (p_count  =>  x_msg_count,
3044                    p_data   =>  x_msg_data );
3045 END RMA_RCV_UPDATE;
3046 
3047 /*-------------------------------------------------------------------------------------*/
3048 /* Procedure name: IO_RCV_UPDATE                                                       */
3049 /* Description   : Procedure called from the Update api to update the depot tables     */
3050 /*                 for the receipts against Internal Requisitions                      */
3051 /*                 It also logs activities for accept reject txn lines                 */
3052 /* Called from   : Called from RECEIPTS_UPDATE API                                     */
3053 /* STANDARD PARAMETERS                                                                 */
3054 /*  In Parameters :                                                                    */
3055 /*    p_api_version       NUMBER    Required  Api Version number                       */
3056 /*    p_init_msg_list     VARCHAR2  Optional  To Initialize message stack              */
3057 /*    p_commit            VARCHAR2  Optional  Commits in API                           */
3058 /*    p_validation_level  NUMBER    Optional  validation level                         */
3059 /*                                                                                     */
3060 /*  Output Parameters:                                                                 */
3061 /*   x_return_status     VARCHAR2      Return status of the API                        */
3062 /*   x_msg_count         NUMBER        Number of messages in stack                     */
3063 /*   x_msg_data          VARCHAR2      Error Message from message stack                */
3064 /*                                                                                     */
3065 /* NON-STANDARD PARAMETERS                                                             */
3066 /*   In Parameters                                                                     */
3067 /*    p_order_header_id  NUMBER   Optional   Internal sales order Id                   */
3068 /* Output Parm :                                                                       */
3069 /* Change Hist :                                                                       */
3070 /*   09/20/03  vlakaman  Initial Creation.                                             */
3071 /*-------------------------------------------------------------------------------------*/
3072 
3073 Procedure IO_RCV_UPDATE
3074         ( p_api_version          IN   NUMBER,
3075           p_commit               IN   VARCHAR2,
3076           p_init_msg_list        IN   VARCHAR2,
3077           p_validation_level     IN   NUMBER,
3078           x_return_status        OUT  NOCOPY  VARCHAR2,
3079           x_msg_count            OUT  NOCOPY  NUMBER,
3080           x_msg_data             OUT  NOCOPY  VARCHAR2,
3081           p_order_header_id      IN   NUMBER
3082         ) IS
3083 
3084   -- Standard variables
3085   l_api_name            CONSTANT VARCHAR2(30) := 'IO_RCV_UPDATE';
3086   l_api_version         CONSTANT NUMBER       := 1.0;
3087 
3088   -- Variables used in API
3089   l_rep_hist_id         number;
3090   l_dummy               varchar2(30);
3091   l_serialized_flag     boolean;
3092   l_ord_remaining_qty   number := 0;
3093   l_prod_txn_exists     boolean;
3094   l_prod_txn_id         number := NULL;
3095   l_prod_txn_status     csd_product_transactions.prod_txn_status%type;
3096   l_total_qty           number;
3097   l_total_del_qty       number;
3098   l_total_accept_qty    number;
3099   l_total_reject_qty    number;
3100   l_pt_accept_qty       number;
3101   l_pt_reject_qty       number;
3102   l_pt_del_qty          number;
3103   l_accept_qty          number;
3104   l_reject_qty          number;
3105   l_serial_num          MTL_SERIAL_NUMBERS.SERIAL_NUMBER%TYPE;
3106   l_lot_num             MTL_LOT_NUMBERS.LOT_NUMBER%TYPE;
3107   l_rep_line_id         number;
3108   l_rcvd_qty            number;
3109   l_line_qty            number;
3110   l_line_del_qty        number;
3111   l_sub_inv             varchar2(80);
3112   l_instance_id         number;
3113 
3114   -- activity record
3115   l_activity_rec      activity_rec_type;
3116 
3117   -- Variable for the FND log file
3118   l_error_level  number   := FND_LOG.LEVEL_ERROR;
3119   l_mod_name     varchar2(2000) := 'csd.plsql.csd_update_programs_pvt.io_rcv_update';
3120 
3121   -- User defined exception
3122   SKIP_RECORD    EXCEPTION;
3123   PROCESS_ERROR  EXCEPTION;
3124 
3125   -- Cursor to get the ro and product txn lines
3126   --
3127   CURSOR GET_RO_PROD_TXN_LINES_ALL IS
3128    select  cpt.product_transaction_id,
3129            cpt.prod_txn_status,
3130            cpt.repair_line_id,
3131            cpt.order_header_id,
3132            cpt.order_line_id,
3133            cpt.req_header_id,
3134            cpt.req_line_id,
3135            nvl(cpt.quantity_received,0) prod_txn_rcvd_qty,
3136            cra.quantity ro_qty,
3137            cra.quantity_rcvd ro_rcvd_qty,
3138            cra.inventory_item_id,
3139            cra.unit_of_measure ro_uom,
3140            prh.segment1 requisition_number,
3141            oel.ordered_quantity,
3142 		 oeh.order_number
3143    from  csd_product_transactions cpt,
3144          csd_repairs cra,
3145          po_requisition_headers_all prh,
3146          oe_order_lines_all oel,
3147 	    oe_order_headers_all oeh
3148    where cpt.repair_line_id = cra.repair_line_id
3149     AND  cpt.req_header_id  = prh.requisition_header_id
3150     AND  cpt.order_line_id  = oel.line_id
3151     AND  oel.header_id      = oeh.header_id
3152     AND  cpt.action_type    = 'MOVE_IN'
3153     AND  cpt.action_code    = 'DEFECTIVES'
3154     AND  cpt.prod_txn_status = 'SHIPPED'
3155     AND  cpt.order_line_id is not null
3156     --Vijay 11/4/04
3157 --    AND  (p_ord_header_id IS null OR p_ord_header_id = cpt.order_header_id)
3158     AND  nvl(cra.quantity_rcvd,0) < cra.quantity;
3159 
3160 --Begin Vijay 11/4/04
3161   -- Cursor to get the ro and product txn lines
3162   -- for the order header id
3163   CURSOR GET_RO_PROD_TXN_LINES (p_ord_header_id in number) IS
3164    select  cpt.product_transaction_id,
3165            cpt.prod_txn_status,
3166            cpt.repair_line_id,
3167            cpt.order_header_id,
3168            cpt.order_line_id,
3169            cpt.req_header_id,
3170            cpt.req_line_id,
3171            nvl(cpt.quantity_received,0) prod_txn_rcvd_qty,
3172            cra.quantity ro_qty,
3173            cra.quantity_rcvd ro_rcvd_qty,
3174            cra.inventory_item_id,
3175            cra.unit_of_measure ro_uom,
3176            prh.segment1 requisition_number,
3177            oel.ordered_quantity,
3178 		 oeh.order_number
3179    from  csd_product_transactions cpt,
3180          csd_repairs cra,
3181          po_requisition_headers_all prh,
3182          oe_order_lines_all oel,
3183 	    oe_order_headers_all oeh
3184    where cpt.repair_line_id = cra.repair_line_id
3185     AND  cpt.req_header_id  = prh.requisition_header_id
3186     AND  cpt.order_line_id  = oel.line_id
3187     AND  oel.header_id      = oeh.header_id
3188     AND  cpt.action_type    = 'MOVE_IN'
3189     AND  cpt.action_code    = 'DEFECTIVES'
3190     AND  cpt.prod_txn_status = 'SHIPPED'
3191     AND  cpt.order_line_id is not null
3192     AND   cpt.order_header_id = p_ord_header_id
3193     AND  nvl(cra.quantity_rcvd,0) < cra.quantity;
3194 
3195    RO IO_RCPT_LINES_Rec_Type;
3196 --End Vijay 11/4/04
3197 
3198 
3199   -- Cursor to get all the rcv txn lines of transaction type
3200   -- DELIVER,REJECT,ACCEPT type
3201   CURSOR GET_RCV_LINES (p_req_line_id in number) IS
3202    select rcv.transaction_id,
3203           rcv.quantity rcvd_qty,
3204           rcv.unit_of_measure,
3205           rcv.subinventory,
3206           rcv.locator_id,
3207 		rcv.organization_id,
3208           rcv.transaction_date received_date,
3209           rcv.transaction_type,
3210           rcv.shipment_header_id,
3211           rcv.shipment_line_id,
3212           prl.item_id,
3213           prl.destination_organization_id inv_org_id,
3214           prl.quantity requisition_qty,
3215           prh.segment1 requirement_number,
3216           mtl.serial_number_control_code,
3217 		mtl.comms_nl_trackable_flag ib_flag,
3218           mtl.lot_control_code,
3219     	     hao.name org_name
3220    from  rcv_transactions rcv,
3221          po_requisition_lines_all prl,
3222          po_requisition_headers_all prh,
3223          mtl_system_items mtl,
3224          hr_all_organization_units hao
3225    where rcv.requisition_line_id = prl.requisition_line_id
3226     and  prl.item_id             = mtl.inventory_item_id
3227     and  prl.destination_organization_id = mtl.organization_id
3228     and  prl.requisition_header_id = prh.requisition_header_id
3229     and  rcv.requisition_line_id = p_req_line_id
3230     and  hao.organization_id     = rcv.organization_id
3231     and  rcv.transaction_type in ('DELIVER','ACCEPT','REJECT');
3232 
3233     -- Cursor to get the deliver txn line for the
3234     -- specific txn id
3235     CURSOR DELIVER_LINES (p_txn_id in number) IS
3236       Select rcvt.transaction_id,
3237              rcvt.transaction_date received_date,
3238              rcvt.subinventory,
3239              rcvt.quantity rcvd_qty,
3240              rcvt.organization_id,
3241              rcvt.locator_id,
3242 		   hao.name org_name
3243       from  rcv_transactions rcvt,
3244 	       hr_all_organization_units hao
3245      where rcvt.parent_transaction_id = p_txn_id
3246       and  rcvt.transaction_type  = 'DELIVER';
3247 
3248     CURSOR ORDER_INFO (p_ord_header_id in number) IS
3249      Select distinct
3250 	       order_header_id,
3251 	       order_line_id
3252      from  csd_product_transactions
3253 	where order_header_id = p_ord_header_id
3254       AND  action_type     = 'MOVE_IN'
3255       AND  action_code     = 'DEFECTIVES'
3256 	 AND  prod_txn_status = 'SHIPPED';
3257 
3258 BEGIN
3259    -- Standard Start of API savepoint
3260    SAVEPOINT  IO_RCV_UPDATE;
3261 
3262    -- Initialize API return status to success
3263    x_return_status := FND_API.G_RET_STS_SUCCESS;
3264 
3265    -- Debug messages
3266    Debug('Beginning of IO_RCV_UPDATE',l_mod_name,1);
3267    Debug('Order Header Id='||to_char(p_order_header_id),l_mod_name,1);
3268 
3269    -- Standard call to check for call compatibility.
3270    IF NOT FND_API.Compatible_API_Call (l_api_version,
3271                                        p_api_version,
3272                                        l_api_name   ,
3273                                        G_PKG_NAME   )
3274        THEN
3275           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3276    END IF;
3277 
3278    -- Initialize message list if p_init_msg_list is set to TRUE.
3279    IF FND_API.to_Boolean( p_init_msg_list ) THEN
3280          FND_MSG_PUB.initialize;
3281    END IF;
3282 
3283    -- Api body starts
3284 
3285    -- In case of Internal orders, the product txns are stamped
3286    -- with the order header id and line id.
3287    -- So Validate if it exists in csd_product_transactions
3288    IF  NVL(p_order_header_id,-999) <> -999 THEN
3289       BEGIN
3290           select 'EXISTS'
3291           into  l_dummy
3292           from  oe_order_headers_all oeh,
3293                 po_requisition_headers_all prh
3294           where oeh.source_document_id = prh.requisition_header_id
3295            and  oeh.header_id = p_order_header_id
3296            and  exists (select 'x'
3297                        from csd_product_transactions cpt
3298                        where cpt.action_type = 'MOVE_IN'
3299                         and  cpt.action_code = 'DEFECTIVES'
3300                         and  cpt.order_header_id = oeh.header_id);
3301       EXCEPTION
3302           WHEN NO_DATA_FOUND THEN
3303              IF ( l_error_level >= G_debug_level) THEN
3304                  fnd_message.set_name('CSD','CSD_INV_ORD_HEADER_ID');
3305                  fnd_message.set_token('HEADER_ID',p_order_header_id);
3306                  FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
3307              ELSE
3308                  fnd_message.set_name('CSD','CSD_INV_ORD_HEADER_ID');
3309                  fnd_message.set_token('HEADER_ID',p_order_header_id);
3310                  fnd_msg_pub.add;
3311              END IF;
3312              RAISE FND_API.G_EXC_ERROR;
3313      END;
3314    END IF;
3315 
3316 
3317    --Loops thro the received lines that have txn type -'DELIVER','ACCEPT','REJECT'
3318    --While Processing the ACCEPT/REJECT txn lines, it also check if there is
3319    --any deliver txn lines with the parent txn id. If it finds then it also
3320    --update the delivered qty and logs activity for the deliver txns.
3321    --   Order Line
3322    --
3323    --    Line Id  Header Id   Ord Qty   Split_from_line_id
3324    --     L1          H1        1
3325    --     L2          H1        2           L1
3326    --     L3          H1        1           L2
3327    --
3328    --   RCV Lines
3329    --    Txn Id    Txn Type   Qty   Ord Line Id  Parent Txn Id
3330    --      T1      ACCEPT      1        L1
3331    --      T2      ACCEPT      2        L2
3332    --      T3      REJECT      1        L3
3333    --      T4      DELIVER     1        L1           T1
3334    --      T5      DELIVER     2        L2           T2
3335    --
3336    --
3337    --   1. Non- Serial
3338    --
3339    --     RO    RO  Qty  Prod Txn   Ord Line   Ord Header  Line Qty
3340    --     RO1    4       P1          L1           H1         1
3341    --     RO1    4       P2          L2           H1         2
3342    --     RO1    4       P3          L3           H1         1
3343    --   2. Serial
3344    --
3345    --     RO    RO  Qty  Prod Txn   Ord Line   Ord Header  Line Qty
3346    --     RO1    1        P1          L1           H1         1
3347    --     RO2    1        P2          L2           H1         1
3348    --     RO3    1        P3          L2           H1         1
3349    --     RO4    1        P4          L1           H1         1
3350    --
3351    -- THE PROGRAM IGNORES THE DELIVERED QUANTITY AGAINST THE REJECT TRANSACTIONS
3352    -- NEED TO REVIEW WITH PM AND IMPLEMENT AS A PHASE II PROJECT
3353 
3354    IF(p_order_header_id is null) THEN
3355       OPEN GET_RO_PROD_TXN_LINES_All;
3356    else
3357       OPEN GET_RO_PROD_TXN_LINES(p_order_header_id);
3358    END IF;
3359 
3360 
3361    /* Changed FOr loop for splitting the cursor : vijay 11/4/04*/
3362    --FOR RO IN GET_RO_PROD_TXN_LINES( p_order_header_id )
3363    LOOP
3364      BEGIN
3365        IF(p_order_header_id is null) THEN
3366           FETCH GET_RO_PROD_TXN_LINES_All INTO RO;
3367           EXIT WHEN GET_RO_PROD_TXN_LINES_All%NOTFOUND ;
3368        else
3369           FETCH GET_RO_PROD_TXN_LINES INTO RO;
3370           EXIT WHEN GET_RO_PROD_TXN_LINES%NOTFOUND ;
3371        END IF;
3372 
3373        -- savepoint
3374        SAVEPOINT RCV_LINES;
3375 
3376        -- Debug messages
3377        Debug('In RO loop',l_mod_name,1);
3378 
3379        FOR RCV IN GET_RCV_LINES (RO.req_line_id)
3380        LOOP
3381         BEGIN
3382           -- Debug messages
3383           Debug('In RCV loop',l_mod_name,1);
3384 
3385           IF RCV.transaction_type = 'ACCEPT' THEN
3386               --Handles Inspection Required Routing options
3387 
3388               -- Debug messages
3389               Debug('Processing Accept txn types',l_mod_name,1);
3390 
3391               select nvl(sum(paramn3),0)
3392               into  l_total_accept_qty
3393               from  csd_repair_history crh
3394               where crh.event_code = 'IP'
3395                and  crh.paramn1    = RCV.transaction_id;
3396 
3397               -- Debug messages
3398               Debug('l_total_accept_qty :'||l_total_accept_qty,l_mod_name,1);
3399               IF nvl(l_total_accept_qty,0) >= nvl(RCV.rcvd_qty,0) THEN
3400                   -- Debug messages
3401                   Debug('Skipping the record ',l_mod_name,1);
3402                   RAISE SKIP_RECORD;
3403               END IF;
3404 
3405               select nvl(sum(paramn4),0), nvl(sum(paramn3),0)
3406               into  l_pt_reject_qty, l_pt_accept_qty
3407               from  csd_repair_history crh
3408               where crh.event_code = 'IP'
3409                and  crh.paramn2    = RO.product_transaction_id;
3410                -- Debug messages
3411               Debug('l_pt_reject_qty :'||l_pt_reject_qty,l_mod_name,1);
3412               Debug('l_pt_accept_qty :'||l_pt_accept_qty,l_mod_name,1);
3413 
3414               IF RCV.serial_number_control_code in (2,5,6) THEN
3415                    l_line_qty := RO.ro_qty;
3416               ELSE
3417                    l_line_qty  := RO.ordered_quantity;
3418               END IF;
3419 
3420                -- Debug messages
3421               Debug('l_line_qty :'||l_line_qty,l_mod_name,1);
3422 
3423               IF (l_pt_reject_qty + l_pt_accept_qty) >= l_line_qty THEN
3424                    -- Debug messages
3425                   Debug('Exiting the RCV loop',l_mod_name,1);
3426                   EXIT;
3427               ELSE
3428                   l_accept_qty  :=  l_line_qty -(l_pt_reject_qty + l_pt_accept_qty);
3429                   IF RCV.rcvd_qty < l_accept_qty THEN
3430                        l_accept_qty := RCV.rcvd_qty;
3431                   END IF;
3432               END IF;
3433 
3434 
3435 		    IF RCV.serial_number_control_code in (2,5,6) THEN
3436                  l_accept_qty := 1 ;
3437 		    ELSE
3438                  l_accept_qty := l_accept_qty - l_total_accept_qty ;
3439 		    END IF;
3440 
3441               -- Debug messages
3442               Debug('l_accept_qty :'||l_accept_qty,l_mod_name,1);
3443 
3444               -- Initialize the activity rec
3445               l_activity_rec := INIT_ACTIVITY_REC ;
3446 
3447               -- Assign the values for activity record
3448               l_activity_rec.REPAIR_HISTORY_ID := l_rep_hist_id;
3449               l_activity_rec.REPAIR_LINE_ID := RO.repair_line_id;
3450               l_activity_rec.EVENT_CODE     := 'IP';
3451               l_activity_rec.ACTION_CODE    := 0;
3452               l_activity_rec.EVENT_DATE     := RCV.received_date;
3453               l_activity_rec.QUANTITY       := l_accept_qty;
3454               l_activity_rec.PARAMN1        := RCV.transaction_id;
3455               l_activity_rec.PARAMN2        := RO.product_transaction_id;
3456               l_activity_rec.PARAMN3        := l_accept_qty;
3457               l_activity_rec.PARAMN6        :=  RO.req_header_id;
3458               l_activity_rec.PARAMC1        :=  RO.requisition_number ;
3459               l_activity_rec.PARAMC2        :=  RCV.subinventory;
3460               l_activity_rec.OBJECT_VERSION_NUMBER := null;
3461 
3462               -- Debug Messages
3463               Debug('Calling LOG_ACTIVITY',l_mod_name,2);
3464 
3465               -- Calling LOG_ACTIVITY for logging activity
3466               LOG_ACTIVITY
3467                     ( p_api_version     => p_api_version,
3468                       p_commit          => p_commit,
3469                       p_init_msg_list   => p_init_msg_list,
3470                       p_validation_level => p_validation_level,
3471                       x_return_status   => x_return_status,
3472                       x_msg_count       => x_msg_count,
3473                       x_msg_data        => x_msg_data,
3474                       p_activity_rec    => l_activity_rec );
3475 
3476               -- Debug Messages
3477               Debug('Return Status from LOG_ACTIVITY:'||x_return_status,l_mod_name,2);
3478               IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
3479                  Debug('LOG_ACTIVITY api failed ',l_mod_name,4);
3480                  RAISE FND_API.G_EXC_ERROR;
3481               END IF;
3482 
3483               -- Check if there are "DELIVER" txn lines against the accept lines
3484               -- If found, then process the deliver lines also
3485               FOR DEL IN DELIVER_LINES(RCV.transaction_id)
3486               LOOP
3487                BEGIN
3488                  -- Debug messages
3489                  Debug('In DEL Loop ',l_mod_name,1);
3490 
3491                  select nvl(sum(quantity),0)
3492                   into  l_total_del_qty
3493                   from csd_repair_history crh
3494                  where crh.event_code = 'RRI'
3495                   and  crh.paramn1    = DEL.transaction_id;
3496                  -- Debug messages
3497                  Debug('l_total_del_qty : '||l_total_del_qty,l_mod_name,1);
3498 
3499                  IF l_total_del_qty >= DEL.rcvd_qty THEN
3500                      -- Debug messages
3501                      Debug('Skipping the record',l_mod_name,1);
3502                      RAISE SKIP_RECORD;
3503                  END IF;
3504 
3505                  select nvl(sum(quantity),0)
3506                   into  l_pt_del_qty
3507                   from csd_repair_history crh
3508                  where crh.event_code = 'RRI'
3509                   and  crh.paramn2 = RO.product_transaction_id;
3510                   -- Debug messages
3511                   Debug('l_pt_del_qty : '||l_pt_del_qty,l_mod_name,1);
3512 
3513                  IF RCV.serial_number_control_code in (2,5,6) THEN
3514                      -- SERIALIZED CASE
3515                      Begin
3516                       select rcvt.serial_num,
3517                              rcvt.lot_num
3518                        into  l_serial_num,
3519                              l_lot_num
3520                        from  rcv_serial_transactions rcvt
3521                       where  rcvt.transaction_id = DEL.transaction_id
3522                        and   rownum = 1
3523                        and   not exists (Select 'NOT EXIST'
3524                                          from csd_repairs cra,
3525                                               csd_product_transactions cpt
3526                                          where cra.repair_line_id = cpt.repair_line_id
3527                                           and  cpt.action_type    = 'MOVE_IN'
3528                                           and  cpt.order_header_id = ro.order_header_id
3529                                           and  cra.serial_number  = rcvt.serial_num);
3530                     Exception
3531                         WHEN NO_DATA_FOUND THEN
3532                              IF ( l_error_level >= G_debug_level) THEN
3533                                 fnd_message.set_name('CSD','CSD_SERIAL_NUM_MISSING');
3534                                 fnd_message.set_token('TXN_ID',DEL.transaction_id);
3535                                 FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
3536                              ELSE
3537                                 fnd_message.set_name('CSD','CSD_SERIAL_NUM_MISSING');
3538                                 fnd_message.set_token('TXN_ID',DEL.transaction_id);
3539                                 fnd_msg_pub.add;
3540                              END If;
3541                              RAISE PROCESS_ERROR;
3542                     END;
3543                     -- Get the instance id from
3544 				-- installbase tables
3545 				IF NVL(RCV.ib_flag,'N') = 'Y' THEN
3546 				  BEGIN
3547                          Select instance_id
3548 				     into   l_instance_id
3549 				     from  csi_item_instances
3550 				     where inventory_item_id = RCV.item_id
3551 				      and  serial_number     = l_serial_num;
3552 				  Exception
3553 				     When NO_DATA_FOUND THEN
3554                             IF ( l_error_level >= G_debug_level) THEN
3555                                 fnd_message.set_name('CSD','CSD_ITEM_INSTANCE_MISSING');
3556                                 fnd_message.set_token('SERIAL_NUM',l_serial_num);
3557                                 FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
3558                             ELSE
3559                                 fnd_message.set_name('CSD','CSD_ITEM_INSTANCE_MISSING');
3560                                 fnd_message.set_token('SERIAL_NUM',l_serial_num);
3561                                 fnd_msg_pub.add;
3562                             END If;
3563 				     When Others THEN
3564                             IF ( l_error_level >= G_debug_level) THEN
3565                                 fnd_message.set_name('CSD','CSD_INV_SERIAL_NUM');
3566                                 fnd_message.set_token('SERIAL_NUM',l_serial_num);
3567                                 FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
3568                             ELSE
3569                                 fnd_message.set_name('CSD','CSD_INV_SERIAL_NUM');
3570                                 fnd_message.set_token('SERIAL_NUM',l_serial_num);
3571                                 fnd_msg_pub.add;
3572                             END If;
3573                       End;
3574                     ELSE
3575 				  l_instance_id := NULL;
3576                     END If;
3577 
3578                     l_line_del_qty  := RO.ro_qty;
3579                 ELSE
3580                     -- Non-Serialized Case
3581                     l_serial_num := NULL;
3582                     l_line_del_qty   := RO.ordered_quantity;
3583                     l_instance_id := NULL;
3584 
3585                     --lot_control_code = 1 No control
3586                     --lot_control_code = 2 Full control
3587                    IF RCV.lot_control_code = 2 THEN
3588                      BEGIN
3589                        Select lot_num
3590                        into   l_lot_num
3591                        from rcv_lot_transactions
3592                        where source_transaction_id = DEL.transaction_id;
3593                     EXCEPTION
3594                       WHEN NO_DATA_FOUND THEN
3595                           IF ( l_error_level >= G_debug_level) THEN
3596                               fnd_message.set_name('CSD','CSD_INV_TXN_ID');
3597                               fnd_message.set_token('TXN_ID',DEL.transaction_id);
3598                               FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
3599                           ELSE
3600                               fnd_message.set_name('CSD','CSD_INV_TXN_ID');
3601                               fnd_message.set_token('TXN_ID',DEL.transaction_id);
3602                               fnd_msg_pub.add;
3603                           END IF;
3604                           RAISE FND_API.G_EXC_ERROR;
3605                     END;
3606                   ELSE
3607                       l_lot_num := NULL;
3608                   END IF;
3609                 END IF;
3610 
3611                 IF l_pt_del_qty >= l_line_del_qty THEN
3612                     -- Debug messages
3613                     Debug('Exiting the loop',l_mod_name,1);
3614                     EXIT;
3615                 ELSE
3616                     l_rcvd_qty := l_line_del_qty - l_pt_del_qty ;
3617                     IF DEL.rcvd_qty < l_rcvd_qty THEN
3618                         l_rcvd_qty := DEL.rcvd_qty;
3619                     END IF;
3620                 END IF;
3621 
3622 			 IF RCV.serial_number_control_code in (2,5,6) THEN
3623                    l_rcvd_qty  := 1;
3624 			 ELSE
3625                    l_rcvd_qty  := l_rcvd_qty - l_total_del_qty;
3626                 END IF;
3627 
3628                 -- Debug messages
3629                 Debug('l_rcvd_qty : '||l_rcvd_qty,l_mod_name,1);
3630 
3631                 -- Update the serial number on repair order
3632                 -- with the rcvd serial number
3633                 UPDATE CSD_REPAIRS
3634                 SET SERIAL_NUMBER = l_serial_num,
3635                     quantity_rcvd = nvl(quantity_rcvd,0) + l_rcvd_qty,
3636 			     customer_product_id = l_instance_id,
3637 		          object_version_number = object_version_number+1,
3638                     last_update_date = sysdate,
3639                     last_updated_by  = fnd_global.user_id,
3640                     last_update_login = fnd_global.login_id
3641                 WHERE repair_line_id = RO.repair_line_id;
3642                 IF SQL%NOTFOUND THEN
3643                    IF ( l_error_level >= G_debug_level) THEN
3644                         fnd_message.set_name('CSD','CSD_RO_UPD_FAILED');
3645                         fnd_message.set_token('REPAIR_LINE_ID',RO.repair_line_id);
3646                         FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
3647                    ELSE
3648                         fnd_message.set_name('CSD','CSD_RO_UPD_FAILED');
3649                         fnd_message.set_token('REPAIR_LINE_ID',RO.repair_line_id);
3650                         fnd_msg_pub.add;
3651                    END IF;
3652                    RAISE PROCESS_ERROR;
3653                 END IF;
3654 
3655                 -- Updating the product txn with the serial number,lot number
3656                 -- qty rcvd and subinventory
3657                 -- sub_inventory_rcvd is used for IO and subinventory column
3658                 -- is used for the regular RMA
3659                 UPDATE CSD_PRODUCT_TRANSACTIONS
3660                 SET SOURCE_SERIAL_NUMBER = l_serial_num,
3661 			     source_instance_id   = l_instance_id,
3662                     LOT_NUMBER_RCVD      = l_lot_num,
3663                     LOCATOR_ID           = DEL.locator_id,
3664                     QUANTITY_RECEIVED  = NVL(QUANTITY_RECEIVED,0) + l_rcvd_qty,
3665                     SUB_INVENTORY_RCVD = DEL.subinventory,
3666 		          object_version_number = object_version_number+1,
3667                     last_update_date = sysdate,
3668                     last_updated_by  = fnd_global.user_id,
3669                     last_update_login = fnd_global.login_id
3670                 WHERE product_transaction_id = RO.product_transaction_id;
3671                 IF SQL%NOTFOUND THEN
3672                      IF ( l_error_level >= G_debug_level) THEN
3673                          fnd_message.set_name('CSD','CSD_PROD_TXN_UPD_FAILED');
3674                          fnd_message.set_token('PROD_TXN_ID',RO.product_transaction_id);
3675                          FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
3676                      ELSE
3677                          fnd_message.set_name('CSD','CSD_PROD_TXN_UPD_FAILED');
3678                          fnd_message.set_token('PROD_TXN_ID',RO.product_transaction_id);
3679                          fnd_msg_pub.add;
3680                      END IF;
3681                      RAISE PROCESS_ERROR;
3682                 END IF;
3683 
3684 			 IF RCV.serial_number_control_code in (2,5,6) THEN
3685 			    UPDATE CSD_PRODUCT_TRANSACTIONS
3686                    SET prod_txn_status       = 'RECEIVED',
3687 		             object_version_number = object_version_number+1,
3688                        last_update_date = sysdate,
3689                        last_updated_by  = fnd_global.user_id,
3690                        last_update_login = fnd_global.login_id
3691                    WHERE product_transaction_id = RO.product_transaction_id;
3692                 ELSE
3693 			    UPDATE CSD_PRODUCT_TRANSACTIONS
3694                    SET prod_txn_status       = 'RECEIVED',
3695 		             object_version_number = object_version_number+1,
3696                        last_update_date = sysdate,
3697                        last_updated_by  = fnd_global.user_id,
3698                        last_update_login = fnd_global.login_id
3699                    WHERE quantity_received = RO.ordered_quantity
3700 			    and   product_transaction_id = RO.product_transaction_id;
3701 			 END IF;
3702 
3703                 -- Initialize the activity rec
3704                 l_activity_rec := INIT_ACTIVITY_REC ;
3705 
3706                 -- Assign the values for activity record
3707                 l_activity_rec.REPAIR_HISTORY_ID := l_rep_hist_id;
3708                 l_activity_rec.REPAIR_LINE_ID := RO.repair_line_id;
3709                 l_activity_rec.EVENT_CODE     := 'RRI';
3710                 l_activity_rec.ACTION_CODE    := 0;
3711                 l_activity_rec.EVENT_DATE     := DEL.received_date;
3712                 l_activity_rec.QUANTITY       := l_rcvd_qty;
3713                 l_activity_rec.PARAMN1        := DEL.transaction_id;
3714                 l_activity_rec.PARAMN2        := RO.product_transaction_id;
3715                 l_activity_rec.PARAMN3        := DEL.organization_id;
3716                 l_activity_rec.PARAMN6        := RO.req_header_id;
3717                 l_activity_rec.PARAMC1        := RO.order_number;
3718                 l_activity_rec.PARAMC2        := RO.requisition_number;
3719                 l_activity_rec.PARAMC3        := DEL.org_name;
3720                 l_activity_rec.PARAMC4        := DEL.subinventory;
3721                 l_activity_rec.OBJECT_VERSION_NUMBER := null;
3722 
3723                 -- Debug Messages
3724                 Debug('Calling LOG_ACTIVITY',l_mod_name,2);
3725 
3726                 -- Calling LOG_ACTIVITY for logging activity
3727                 -- Receipt of item against Internal Requisition
3728                 LOG_ACTIVITY
3729                      ( p_api_version     => p_api_version,
3730                        p_commit          => p_commit,
3731                        p_init_msg_list   => p_init_msg_list,
3732                        p_validation_level => p_validation_level,
3733                        x_return_status   => x_return_status,
3734                        x_msg_count       => x_msg_count,
3735                        x_msg_data        => x_msg_data,
3736                        p_activity_rec    => l_activity_rec );
3737 
3738                 -- Debug Messages
3739                 Debug('Return Status from LOG_ACTIVITY:'||x_return_status,l_mod_name,2);
3740                 IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
3741                     Debug('LOG_ACTIVITY api failed ',l_mod_name,4);
3742                     RAISE FND_API.G_EXC_ERROR;
3743                 END IF;
3744 
3745                 IF (l_pt_del_qty + l_rcvd_qty) >= l_line_del_qty  THEN
3746                     Debug(' Exiting the DEL loop',l_mod_name,1);
3747                     EXIT;
3748                 END IF;
3749               Exception
3750                     WHEN PROCESS_ERROR THEN
3751                          Debug('In process_error exception ',l_mod_name,4);
3752                          RAISE PROCESS_ERROR;
3753                     WHEN SKIP_RECORD THEN
3754                          NULL;
3755               END;
3756             END LOOP; -- end of delivery lines
3757 
3758             IF (l_pt_reject_qty+ l_pt_accept_qty + l_accept_qty)>= l_line_qty THEN
3759                   Debug('Exiting RCV the loop ',l_mod_name,1);
3760                   EXIT;
3761             END IF;
3762 
3763        ELSIF RCV.transaction_type = 'REJECT' THEN
3764                -- Handles Inspection Required routing option
3765 
3766                select nvl(sum(paramn4),0)
3767                 into  l_total_reject_qty
3768                from  csd_repair_history crh
3769                where crh.event_code = 'IP'
3770                 and  crh.paramn1    = RCV.transaction_id;
3771                IF l_total_reject_qty >= RCV.rcvd_qty THEN
3772                    -- Debug messages
3773                    Debug('Skipping the record ',l_mod_name,1);
3774                    RAISE SKIP_RECORD;
3775                END IF;
3776 
3777                select nvl(sum(paramn3),0),nvl(sum(paramn4),0)
3778                 into  l_pt_accept_qty, l_pt_reject_qty
3779                from  csd_repair_history crh
3780                where crh.event_code = 'IP'
3781                 and  crh.paramn2 = RO.product_transaction_id;
3782                -- Debug messages
3783               Debug('l_pt_accept_qty'||l_pt_accept_qty,l_mod_name,1);
3784               Debug('l_pt_reject_qty'||l_pt_reject_qty,l_mod_name,1);
3785 
3786               IF RCV.serial_number_control_code in (2,5,6) THEN
3787                   l_line_qty := 1;
3788               ELSE
3789                   l_line_qty  := RO.ordered_quantity;
3790               END IF;
3791               IF l_pt_accept_qty+ l_pt_reject_qty >= l_line_qty THEN
3792                    -- Debug messages
3793                    Debug('Exiting the loop ',l_mod_name,1);
3794                    EXIT;
3795               ELSE
3796                   l_reject_qty := l_line_qty- (l_pt_accept_qty+ l_pt_reject_qty);
3797                   IF RCV.rcvd_qty < l_reject_qty THEN
3798                       l_reject_qty := RCV.rcvd_qty;
3799                   END IF;
3800               END IF;
3801 
3802 		    IF RCV.serial_number_control_code in (2,5,6) THEN
3803                  l_reject_qty := 1;
3804 		    ELSE
3805                  l_reject_qty := l_reject_qty - l_total_reject_qty;
3806               END IF;
3807 
3808               -- Debug messages
3809               Debug('l_reject_qty'||l_reject_qty,l_mod_name,1);
3810 
3811                -- Initialize the activity rec
3812               l_activity_rec := INIT_ACTIVITY_REC ;
3813 
3814                -- Assign the values for activity record
3815               l_activity_rec.REPAIR_HISTORY_ID := l_rep_hist_id;
3816               l_activity_rec.REPAIR_LINE_ID :=  RO.repair_line_id;
3817               l_activity_rec.EVENT_CODE     :=  'IP';
3818               l_activity_rec.ACTION_CODE    :=  0;
3819               l_activity_rec.EVENT_DATE     :=  RCV.received_date;
3820               l_activity_rec.QUANTITY       :=  l_reject_qty;
3821               l_activity_rec.PARAMN1        :=  RCV.transaction_id;
3822               l_activity_rec.PARAMN2        :=  RO.product_transaction_id;
3823               l_activity_rec.PARAMN3        :=  null;
3824               l_activity_rec.PARAMN4        :=  l_reject_qty;
3825               l_activity_rec.PARAMN6        :=  RO.req_header_id;
3826               l_activity_rec.PARAMC1        :=  RO.requisition_number;
3827               l_activity_rec.PARAMC2        :=  rcv.subinventory;
3828               l_activity_rec.OBJECT_VERSION_NUMBER := null;
3829 
3830               -- Debug Messages
3831               Debug('Calling LOG_ACTIVITY',l_mod_name,2);
3832 
3833               -- Calling LOG_ACTIVITY for logging activity
3834               LOG_ACTIVITY
3835                      ( p_api_version     => p_api_version,
3836                        p_commit          => p_commit,
3837                        p_init_msg_list   => p_init_msg_list,
3838                        p_validation_level => p_validation_level,
3839                        x_return_status   => x_return_status,
3840                        x_msg_count       => x_msg_count,
3841                        x_msg_data        => x_msg_data,
3842                        p_activity_rec    => l_activity_rec );
3843 
3844               -- Debug Messages
3845              Debug('Return Status from LOG_ACTIVITY:'||x_return_status,l_mod_name,2);
3846              IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
3847                  Debug('LOG_ACTIVITY api failed ',l_mod_name,4);
3848                  RAISE FND_API.G_EXC_ERROR;
3849              END IF;
3850 
3851               -- Check if there are "DELIVER" txn lines against the reject lines
3852               -- If found, then process the deliver lines also
3853               FOR DEL IN DELIVER_LINES(RCV.transaction_id)
3854               LOOP
3855                BEGIN
3856                  -- Debug messages
3857                  Debug('In DEL Loop ',l_mod_name,1);
3858 
3859                  select nvl(sum(quantity),0)
3860                   into  l_total_del_qty
3861                   from csd_repair_history crh
3862                  where crh.event_code = 'RRI'
3863                   and  crh.paramn1    = DEL.transaction_id;
3864                  -- Debug messages
3865                  Debug('l_total_del_qty : '||l_total_del_qty,l_mod_name,1);
3866 
3867                  IF l_total_del_qty >= DEL.rcvd_qty THEN
3868                      -- Debug messages
3869                      Debug('Skipping the record',l_mod_name,1);
3870                      RAISE SKIP_RECORD;
3871                  END IF;
3872 
3873                  select nvl(sum(quantity),0)
3874                   into  l_pt_del_qty
3875                   from csd_repair_history crh
3876                  where crh.event_code = 'RRI'
3877                   and  crh.paramn2 = RO.product_transaction_id;
3878                   -- Debug messages
3879                   Debug('l_pt_del_qty : '||l_pt_del_qty,l_mod_name,1);
3880 
3881                  IF RCV.serial_number_control_code in (2,5,6) THEN
3882                      -- SERIALIZED CASE
3883                      Begin
3884                       select rcvt.serial_num,
3885                              rcvt.lot_num
3886                        into  l_serial_num,
3887                              l_lot_num
3888                        from  rcv_serial_transactions rcvt
3889                       where  rcvt.transaction_id = DEL.transaction_id
3890                        and   rownum = 1
3891                        and   not exists (Select 'NOT EXIST'
3892                                          from csd_repairs cra,
3893                                               csd_product_transactions cpt
3894                                          where cra.repair_line_id = cpt.repair_line_id
3895                                           and  cpt.action_type    = 'MOVE_IN'
3896                                           and  cpt.order_header_id = ro.order_header_id
3897                                           and  cra.serial_number  = rcvt.serial_num);
3898                     Exception
3899                         WHEN NO_DATA_FOUND THEN
3900                              IF ( l_error_level >= G_debug_level) THEN
3901                                 fnd_message.set_name('CSD','CSD_SERIAL_NUM_MISSING');
3902                                 fnd_message.set_token('TXN_ID',DEL.transaction_id);
3903                                 FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
3904                              ELSE
3905                                 fnd_message.set_name('CSD','CSD_SERIAL_NUM_MISSING');
3906                                 fnd_message.set_token('TXN_ID',DEL.transaction_id);
3907                                 fnd_msg_pub.add;
3908                              END If;
3909                              RAISE PROCESS_ERROR;
3910                     END;
3911                     -- Get the instance id from
3912 		          -- installbase tables
3913 		          IF NVL(RCV.ib_flag,'N') = 'Y' THEN
3914 				  BEGIN
3915                          Select instance_id
3916 			          into   l_instance_id
3917 			          from  csi_item_instances
3918 			          where inventory_item_id = RCV.item_id
3919 			           and  serial_number     = l_serial_num;
3920 		            Exception
3921 			         When NO_DATA_FOUND THEN
3922                           IF ( l_error_level >= G_debug_level) THEN
3923                              fnd_message.set_name('CSD','CSD_ITEM_INSTANCE_MISSING');
3924                              fnd_message.set_token('SERIAL_NUM',l_serial_num);
3925                              FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
3926                           ELSE
3927                              fnd_message.set_name('CSD','CSD_ITEM_INSTANCE_MISSING');
3928                              fnd_message.set_token('SERIAL_NUM',l_serial_num);
3929                              fnd_msg_pub.add;
3930                           END If;
3931 			         When Others THEN
3932                           IF ( l_error_level >= G_debug_level) THEN
3933                              fnd_message.set_name('CSD','CSD_INV_SERIAL_NUM');
3934                              fnd_message.set_token('SERIAL_NUM',l_serial_num);
3935                              FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
3936                           ELSE
3937                              fnd_message.set_name('CSD','CSD_INV_SERIAL_NUM');
3938                              fnd_message.set_token('SERIAL_NUM',l_serial_num);
3939                              fnd_msg_pub.add;
3940                           END If;
3941                       End;
3942                     ELSE
3943 				  l_instance_id := NULL;
3944                     END IF;
3945 
3946                    l_line_del_qty  := RO.ro_qty;
3947                 ELSE
3948                     -- Non-Serialized Case
3949                     l_serial_num := NULL;
3950                     l_line_del_qty   := RO.ordered_quantity;
3951                     l_instance_id  := NULL;
3952 
3953                     --lot_control_code = 1 No control
3954                     --lot_control_code = 2 Full control
3955                    IF RCV.lot_control_code = 2 THEN
3956                      BEGIN
3957                        Select lot_num
3958                        into   l_lot_num
3959                        from rcv_lot_transactions
3960                        where source_transaction_id = DEL.transaction_id;
3961                     EXCEPTION
3962                       WHEN NO_DATA_FOUND THEN
3963                           IF ( l_error_level >= G_debug_level) THEN
3964                               fnd_message.set_name('CSD','CSD_INV_TXN_ID');
3965                               fnd_message.set_token('TXN_ID',DEL.transaction_id);
3966                               FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
3967                           ELSE
3968                               fnd_message.set_name('CSD','CSD_INV_TXN_ID');
3969                               fnd_message.set_token('TXN_ID',DEL.transaction_id);
3970                               fnd_msg_pub.add;
3971                           END IF;
3972                           RAISE FND_API.G_EXC_ERROR;
3973                     END;
3974                   ELSE
3975                       l_lot_num := NULL;
3976                   END IF;
3977                 END IF;
3978 
3979                 IF l_pt_del_qty >= l_line_del_qty THEN
3980                     -- Debug messages
3981                     Debug('Exiting the loop',l_mod_name,1);
3982                     EXIT;
3983                 ELSE
3984                     l_rcvd_qty := l_line_del_qty - l_pt_del_qty ;
3985                     IF DEL.rcvd_qty < l_rcvd_qty THEN
3986                         l_rcvd_qty := DEL.rcvd_qty;
3987                     END IF;
3988                 END IF;
3989 
3990 			 IF RCV.serial_number_control_code in (2,5,6) THEN
3991                    l_rcvd_qty  := 1;
3992 			 ELSE
3993                    l_rcvd_qty  := l_rcvd_qty - l_total_del_qty;
3994                 END IF;
3995 
3996                 -- Debug messages
3997                 Debug('l_rcvd_qty : '||l_rcvd_qty,l_mod_name,1);
3998 
3999                 -- Update the serial number on repair order
4000                 -- with the rcvd serial number
4001                 UPDATE CSD_REPAIRS
4002                 SET SERIAL_NUMBER = l_serial_num,
4003                     quantity_rcvd = nvl(quantity_rcvd,0) + l_rcvd_qty,
4004 				customer_product_id   = l_instance_id,
4005 		          object_version_number = object_version_number+1,
4006                     last_update_date = sysdate,
4007                     last_updated_by  = fnd_global.user_id,
4008                     last_update_login = fnd_global.login_id
4009                 WHERE repair_line_id = RO.repair_line_id;
4010                 IF SQL%NOTFOUND THEN
4011                    IF ( l_error_level >= G_debug_level) THEN
4012                         fnd_message.set_name('CSD','CSD_RO_UPD_FAILED');
4013                         fnd_message.set_token('REPAIR_LINE_ID',RO.repair_line_id);
4014                         FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
4015                    ELSE
4016                         fnd_message.set_name('CSD','CSD_RO_UPD_FAILED');
4017                         fnd_message.set_token('REPAIR_LINE_ID',RO.repair_line_id);
4018                         fnd_msg_pub.add;
4019                    END IF;
4020                    RAISE PROCESS_ERROR;
4021                 END IF;
4022 
4023 
4024                 -- Updating the product txn with the serial number,lot number
4025                 -- qty rcvd and subinventory
4026                 -- sub_inventory_rcvd is used for IO and subinventory column
4027                 -- is used for the regular RMA
4028                 UPDATE CSD_PRODUCT_TRANSACTIONS
4029                 SET SOURCE_SERIAL_NUMBER = l_serial_num,
4030 			     source_instance_id   = l_instance_id,
4031                     LOT_NUMBER_RCVD      = l_lot_num,
4032                     LOCATOR_ID           = DEL.locator_id,
4033                     QUANTITY_RECEIVED  = NVL(QUANTITY_RECEIVED,0) + l_rcvd_qty,
4034                     SUB_INVENTORY_RCVD = DEL.subinventory,
4035 		          object_version_number = object_version_number+1,
4036                     last_update_date = sysdate,
4037                     last_updated_by  = fnd_global.user_id,
4038                     last_update_login = fnd_global.login_id
4039                 WHERE product_transaction_id = RO.product_transaction_id;
4040                 IF SQL%NOTFOUND THEN
4041                      IF ( l_error_level >= G_debug_level) THEN
4042                          fnd_message.set_name('CSD','CSD_PROD_TXN_UPD_FAILED');
4043                          fnd_message.set_token('PROD_TXN_ID',RO.product_transaction_id);
4044                          FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
4045                      ELSE
4046                          fnd_message.set_name('CSD','CSD_PROD_TXN_UPD_FAILED');
4047                          fnd_message.set_token('PROD_TXN_ID',RO.product_transaction_id);
4048                          fnd_msg_pub.add;
4049                      END IF;
4050                      RAISE PROCESS_ERROR;
4051                 END IF;
4052 
4053                 IF RCV.serial_number_control_code in (2,5,6) THEN
4054 			    UPDATE CSD_PRODUCT_TRANSACTIONS
4055                    SET prod_txn_status       = 'RECEIVED',
4056 		             object_version_number = object_version_number+1,
4057                        last_update_date = sysdate,
4058                        last_updated_by  = fnd_global.user_id,
4059                        last_update_login = fnd_global.login_id
4060                    WHERE product_transaction_id = RO.product_transaction_id;
4061 
4062 			 ELSE
4063 			    UPDATE CSD_PRODUCT_TRANSACTIONS
4064                    SET prod_txn_status       = 'RECEIVED',
4065 		             object_version_number = object_version_number+1,
4066                        last_update_date = sysdate,
4067                        last_updated_by  = fnd_global.user_id,
4068                        last_update_login = fnd_global.login_id
4069                    WHERE quantity_received = RO.ordered_quantity
4070 			    and   product_transaction_id = RO.product_transaction_id;
4071                 END IF;
4072 
4073                 -- Initialize the activity rec
4074                 l_activity_rec := INIT_ACTIVITY_REC ;
4075 
4076                 -- Assign the values for activity record
4077                 l_activity_rec.REPAIR_HISTORY_ID := l_rep_hist_id;
4078                 l_activity_rec.REPAIR_LINE_ID := RO.repair_line_id;
4079                 l_activity_rec.EVENT_CODE     := 'RRI';
4080                 l_activity_rec.ACTION_CODE    := 0;
4081                 l_activity_rec.EVENT_DATE     := DEL.received_date;
4082                 l_activity_rec.QUANTITY       := l_rcvd_qty;
4083                 l_activity_rec.PARAMN1        := DEL.transaction_id;
4084                 l_activity_rec.PARAMN2        := RO.product_transaction_id;
4085                 l_activity_rec.PARAMN3        := DEL.organization_id;
4086                 l_activity_rec.PARAMN6        := RO.req_header_id;
4087                 l_activity_rec.PARAMC1        := RO.order_number;
4088                 l_activity_rec.PARAMC2        := RO.requisition_number;
4089                 l_activity_rec.PARAMC3        := DEL.org_name;
4090                 l_activity_rec.PARAMC4        := DEL.subinventory;
4091                 l_activity_rec.OBJECT_VERSION_NUMBER := null;
4092 
4093                 -- Debug Messages
4094                 Debug('Calling LOG_ACTIVITY',l_mod_name,2);
4095 
4096                 -- Calling LOG_ACTIVITY for logging activity
4097                 -- Receipt of item against Internal Requisition
4098                 LOG_ACTIVITY
4099                      ( p_api_version     => p_api_version,
4100                        p_commit          => p_commit,
4101                        p_init_msg_list   => p_init_msg_list,
4102                        p_validation_level => p_validation_level,
4103                        x_return_status   => x_return_status,
4104                        x_msg_count       => x_msg_count,
4105                        x_msg_data        => x_msg_data,
4106                        p_activity_rec    => l_activity_rec );
4107 
4108                 -- Debug Messages
4109                 Debug('Return Status from LOG_ACTIVITY:'||x_return_status,l_mod_name,2);
4110                 IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
4111                     Debug('LOG_ACTIVITY api failed ',l_mod_name,4);
4112                     RAISE FND_API.G_EXC_ERROR;
4113                 END IF;
4114 
4115                 IF (l_pt_del_qty + l_rcvd_qty) >= l_line_del_qty  THEN
4116                     Debug(' Exiting the DEL loop',l_mod_name,1);
4117                     EXIT;
4118                 END IF;
4119               Exception
4120                     WHEN PROCESS_ERROR THEN
4121                          Debug('In process_error exception ',l_mod_name,4);
4122                          RAISE PROCESS_ERROR;
4123                     WHEN SKIP_RECORD THEN
4124                          NULL;
4125               END;
4126             END LOOP; -- end of delivery lines
4127 
4128             IF l_pt_accept_qty+ l_pt_reject_qty+ l_reject_qty >= l_line_qty THEN
4129                  Debug(' Exiting the RCV loop',l_mod_name,1);
4130                  EXIT;
4131             END IF;
4132 
4133       ELSIF RCV.transaction_type = 'DELIVER' THEN
4134          -- Handles the Direct Delivery and standard routing options
4135 
4136            select nvl(sum(quantity),0)
4137             into  l_total_qty
4138             from csd_repair_history crh
4139            where crh.event_code = 'RRI'
4140             and  crh.paramn1    = RCV.transaction_id;
4141            IF l_total_qty >= RCV.rcvd_qty THEN
4142               -- Debug messages
4143               Debug(' Skipping the record',l_mod_name,1);
4144               RAISE SKIP_RECORD;
4145            END IF;
4146 
4147            select nvl(sum(quantity),0)
4148            into  l_pt_del_qty
4149            from  csd_repair_history crh
4150            where crh.event_code = 'RRI'
4151             and  paramn2        = RO.product_transaction_id;
4152 
4153            -- Debug messages
4154            Debug('l_pt_reject_qty='||l_pt_reject_qty,l_mod_name,1);
4155            Debug('l_pt_del_qty   ='||l_pt_del_qty,l_mod_name,1);
4156 
4157            IF RCV.serial_number_control_code in (2,5,6) THEN
4158                -- SERIALIZED AND LOT CONTROLLED CASE
4159               l_line_qty  := RO.ro_qty;
4160                Begin
4161                 select rcvt.serial_num,
4162                        rcvt.lot_num
4163                  into  l_serial_num,
4164                        l_lot_num
4165                  from  rcv_serial_transactions rcvt
4166                 where  rcvt.transaction_id = RCV.transaction_id
4167                  and   rownum = 1
4168                  and   not exists (Select 'NOT EXIST'
4169                                    from csd_repairs cra,
4170                                         csd_product_transactions cpt
4171                                    where cra.repair_line_id = cpt.repair_line_id
4172                                     and  cpt.order_header_id = ro.order_header_id
4173                                     and  cra.serial_number  = rcvt.serial_num);
4174               Exception
4175                 WHEN NO_DATA_FOUND THEN
4176                     IF ( l_error_level >= G_debug_level) THEN
4177                         fnd_message.set_name('CSD','CSD_SERIAL_NUM_MISSING');
4178                         fnd_message.set_token('TXN_ID',RCV.transaction_id);
4179                         FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
4180                     ELSE
4181                         fnd_message.set_name('CSD','CSD_SERIAL_NUM_MISSING');
4182                         fnd_message.set_token('TXN_ID',RCV.transaction_id);
4183                         fnd_msg_pub.add;
4184                     END IF;
4185                     RAISE PROCESS_ERROR;
4186               END;
4187 
4188               -- Get the instance id from
4189 		    -- installbase tables
4190 		    IF NVL(RCV.ib_flag,'N') = 'Y' THEN
4191 		      BEGIN
4192                  Select instance_id
4193 			  into   l_instance_id
4194 			  from csi_item_instances
4195 			  where inventory_item_id = RCV.item_id
4196 			   and  serial_number     = l_serial_num;
4197 		      Exception
4198 			    When NO_DATA_FOUND THEN
4199                       IF ( l_error_level >= G_debug_level) THEN
4200                            fnd_message.set_name('CSD','CSD_ITEM_INSTANCE_MISSING');
4201                            fnd_message.set_token('SERIAL_NUM',l_serial_num);
4202                            FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
4203                       ELSE
4204                            fnd_message.set_name('CSD','CSD_ITEM_INSTANCE_MISSING');
4205                            fnd_message.set_token('SERIAL_NUM',l_serial_num);
4206                            fnd_msg_pub.add;
4207                       END If;
4208 			    When Others THEN
4209                       IF ( l_error_level >= G_debug_level) THEN
4210                            fnd_message.set_name('CSD','CSD_INV_SERIAL_NUM');
4211                            fnd_message.set_token('SERIAL_NUM',l_serial_num);
4212                            FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
4213                       ELSE
4214                            fnd_message.set_name('CSD','CSD_INV_SERIAL_NUM');
4215                            fnd_message.set_token('SERIAL_NUM',l_serial_num);
4216                            fnd_msg_pub.add;
4217                       END If;
4218                End;
4219 	   	    ELSE
4220                 l_instance_id := NULL;
4221 		    END IF;
4222 	    ELSE
4223               -- Non-Serialized Case but lot controlled
4224               l_serial_num := NULL;
4225               l_line_qty   := RO.ordered_quantity;
4226               l_instance_id := NULL;
4227 
4228               --lot_control_code = 1 No control
4229               --lot_control_code = 2 Full control
4230               IF RCV.lot_control_code = 2 THEN
4231                   BEGIN
4232                      Select lot_num
4233                      into   l_lot_num
4234                      from rcv_lot_transactions
4235                      where source_transaction_id = RCV.transaction_id;
4236                   EXCEPTION
4237                      WHEN NO_DATA_FOUND THEN
4238                           IF ( l_error_level >= G_debug_level) THEN
4239                               fnd_message.set_name('CSD','CSD_INV_TXN_ID');
4240                               fnd_message.set_token('TXN_ID',RCV.transaction_id);
4241                               FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
4242                           ELSE
4243                               fnd_message.set_name('CSD','CSD_INV_TXN_ID');
4244                               fnd_message.set_token('TXN_ID',RCV.transaction_id);
4245                               fnd_msg_pub.add;
4246                           END IF;
4247                           RAISE FND_API.G_EXC_ERROR;
4248                  END;
4249               ELSE
4250                   -- Non-Serialized Case but not lot controlled
4251                      l_lot_num := NULL;
4252               END IF;
4253           END IF;
4254 
4255           IF l_pt_del_qty >= l_line_qty  THEN
4256                 Debug(' EXiting the RCV Loop',l_mod_name,1);
4257                 EXIT;
4258           ELSE
4259                 l_rcvd_qty :=  l_line_qty  - l_pt_del_qty;
4260                 IF RCV.rcvd_qty <  l_rcvd_qty THEN
4261                     l_rcvd_qty := RCV.rcvd_qty;
4262                 END IF;
4263           END IF;
4264 
4265           IF RCV.serial_number_control_code in (2,5,6) THEN
4266 		   l_rcvd_qty := 1;
4267 		ELSE
4268 		   l_rcvd_qty := l_rcvd_qty - l_total_qty;
4269           END IF;
4270 
4271           -- Debug messages
4272           Debug('l_rcvd_qty'||l_rcvd_qty,l_mod_name,1);
4273 
4274           -- Update repair order with the rcvd serial number
4275           -- and the rcvd qty
4276           UPDATE CSD_REPAIRS
4277           SET SERIAL_NUMBER = l_serial_num,
4278               quantity_rcvd = nvl(quantity_rcvd,0) + l_rcvd_qty,
4279 		    customer_product_id = l_instance_id,
4280 		    object_version_number = object_version_number+1,
4281               last_update_date = sysdate,
4282               last_updated_by  = fnd_global.user_id,
4283               last_update_login = fnd_global.login_id
4284           WHERE repair_line_id = RO.repair_line_id;
4285           IF SQL%NOTFOUND THEN
4286                IF ( l_error_level >= G_debug_level) THEN
4287                     fnd_message.set_name('CSD','CSD_RO_UPD_FAILED');
4288                     fnd_message.set_token('REPAIR_LINE_ID',RO.repair_line_id);
4289                     FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
4290                ELSE
4291                     fnd_message.set_name('CSD','CSD_RO_UPD_FAILED');
4292                     fnd_message.set_token('REPAIR_LINE_ID',RO.repair_line_id);
4293                     fnd_msg_pub.add;
4294                END IF;
4295                RAISE PROCESS_ERROR;
4296           END IF;
4297 
4298 
4299           -- Update product txn with the rcvd serial number,lot number
4300           -- locator id, sub inv, status and the rcvd qty
4301           UPDATE CSD_PRODUCT_TRANSACTIONS
4302           SET SOURCE_SERIAL_NUMBER = l_serial_num,
4303 		    source_instance_id   = l_instance_id,
4304               LOT_NUMBER_RCVD      = l_lot_num,
4305               QUANTITY_RECEIVED = NVL(QUANTITY_RECEIVED,0) + l_rcvd_qty,
4306               SUB_INVENTORY_RCVD= RCV.subinventory,
4307               LOCATOR_ID        = RCV.locator_id,
4308 		    object_version_number = object_version_number+1,
4309               last_update_date = sysdate,
4310               last_updated_by  = fnd_global.user_id,
4311               last_update_login = fnd_global.login_id
4312           WHERE product_transaction_id = RO.product_transaction_id;
4313           IF SQL%NOTFOUND THEN
4314               IF ( l_error_level >= G_debug_level) THEN
4315                     fnd_message.set_name('CSD','CSD_PROD_TXN_UPD_FAILED');
4316                     fnd_message.set_token('PROD_TXN_ID',RO.product_transaction_id);
4317                     FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
4318               ELSE
4319                     fnd_message.set_name('CSD','CSD_PROD_TXN_UPD_FAILED');
4320                     fnd_message.set_token('PROD_TXN_ID',RO.product_transaction_id);
4321                     fnd_msg_pub.add;
4322               END IF;
4323               RAISE PROCESS_ERROR;
4324           END IF;
4325 
4326           IF RCV.serial_number_control_code in (2,5,6) THEN
4327 		   UPDATE CSD_PRODUCT_TRANSACTIONS
4328              SET prod_txn_status       = 'RECEIVED',
4329                  object_version_number = object_version_number+1,
4330                  last_update_date = sysdate,
4331                  last_updated_by  = fnd_global.user_id,
4332                  last_update_login = fnd_global.login_id
4333              WHERE product_transaction_id = RO.product_transaction_id;
4334           ELSE
4335 		   UPDATE CSD_PRODUCT_TRANSACTIONS
4336              SET prod_txn_status       = 'RECEIVED',
4337                  object_version_number = object_version_number+1,
4338                  last_update_date = sysdate,
4339                  last_updated_by  = fnd_global.user_id,
4340                  last_update_login = fnd_global.login_id
4341              WHERE quantity_received = RO.ordered_quantity
4342              and   product_transaction_id = RO.product_transaction_id;
4343 		END IF;
4344 
4345           -- Initialize the activity rec
4346           l_activity_rec := INIT_ACTIVITY_REC ;
4347 
4348           -- Assign the values for activity record
4349           l_activity_rec.REPAIR_HISTORY_ID := l_rep_hist_id;
4350           l_activity_rec.REPAIR_LINE_ID := RO.repair_line_id;
4351           l_activity_rec.EVENT_CODE     := 'RRI';
4352           l_activity_rec.ACTION_CODE    := 0;
4353           l_activity_rec.EVENT_DATE     := RCV.received_date;
4354           l_activity_rec.QUANTITY       := l_rcvd_qty;
4355           l_activity_rec.PARAMN1        := RCV.transaction_id;
4356           l_activity_rec.PARAMN2        := RO.product_transaction_id;
4357           l_activity_rec.PARAMN3        := RCV.organization_id;
4358           l_activity_rec.PARAMN6        :=  RO.req_header_id;
4359           l_activity_rec.PARAMC1        :=  RO.order_number;
4360           l_activity_rec.PARAMC2        :=  RO.requisition_number;
4361           l_activity_rec.PARAMC3        :=  RCV.org_name;
4362           l_activity_rec.PARAMC4        :=  RCV.subinventory;
4363           l_activity_rec.OBJECT_VERSION_NUMBER := null;
4364 
4365           -- Debug Messages
4366           Debug('Calling LOG_ACTIVITY',l_mod_name,2);
4367 
4368           -- Calling LOG_ACTIVITY for logging activity
4369           LOG_ACTIVITY
4370                 ( p_api_version     => p_api_version,
4371                   p_commit          => p_commit,
4372                   p_init_msg_list   => p_init_msg_list,
4373                   p_validation_level => p_validation_level,
4374                   x_return_status   => x_return_status,
4375                   x_msg_count       => x_msg_count,
4376                   x_msg_data        => x_msg_data,
4377                   p_activity_rec    => l_activity_rec );
4378 
4379             -- Debug Messages
4380             Debug('Return Status from LOG_ACTIVITY:'||x_return_status,l_mod_name,2);
4381             IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
4382                 Debug('LOG_ACTIVITY api failed ',l_mod_name,1);
4383                 RAISE FND_API.G_EXC_ERROR;
4384             END IF;
4385 
4386            IF  l_pt_del_qty+ l_rcvd_qty >= l_line_qty   THEN
4387                 Debug(' exiting the RCV loop',l_mod_name,1);
4388                 EXIT;
4389            END IF;
4390           END IF;
4391         Exception
4392            WHEN PROCESS_ERROR THEN
4393                  Debug(' In Process error exception',l_mod_name,4);
4394                  RAISE PROCESS_ERROR;
4395             WHEN SKIP_RECORD THEN
4396                  NULL;
4397         END;
4398        END LOOP;
4399      Exception
4400         WHEN PROCESS_ERROR THEN
4401             ROLLBACK TO RCV_LINES;
4402             Debug(' In Process error exception, exiting the loop',l_mod_name,1);
4403             -- In case of error, exit the loop. Commit the processed records
4404             -- and rollback the error record
4405             --RAISE FND_API.G_EXC_ERROR;
4406             x_return_status := FND_API.G_RET_STS_ERROR ;
4407             EXIT;
4408      END;
4409    END LOOP;
4410 
4411     --Added by Vijay 11/4/04
4412    IF(GET_RO_PROD_TXN_LINES_All%ISOPEN) THEN
4413        CLOSE GET_RO_PROD_TXN_LINES_All;
4414    END IF;
4415    IF(GET_RO_PROD_TXN_LINES%ISOPEN) THEN
4416        CLOSE GET_RO_PROD_TXN_LINES;
4417    END IF;
4418 
4419 
4420    -- Standard check of p_commit.
4421    IF FND_API.To_Boolean( p_commit ) THEN
4422        COMMIT WORK;
4423    END IF;
4424 
4425    -- Standard call to get message count and IF count is  get message info.
4426    FND_MSG_PUB.Count_And_Get
4427            (p_count  =>  x_msg_count,
4428             p_data   =>  x_msg_data);
4429  EXCEPTION
4430     WHEN FND_API.G_EXC_ERROR THEN
4431           Debug('In FND_API.G_EXC_ERROR exception',l_mod_name,4);
4432           ROLLBACK TO IO_RCV_UPDATE;
4433           x_return_status := FND_API.G_RET_STS_ERROR ;
4434           FND_MSG_PUB.Count_And_Get
4435                 (p_count  =>  x_msg_count,
4436                  p_data   =>  x_msg_data  );
4437     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4438           Debug('In FND_API.G_EXC_UNEXPECTED_ERROR exception',l_mod_name,4);
4439           ROLLBACK TO IO_RCV_UPDATE;
4440           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4441           FND_MSG_PUB.Count_And_Get
4442                 ( p_count  =>  x_msg_count,
4443                   p_data   =>  x_msg_data );
4444     WHEN OTHERS THEN
4445           Debug('In OTHERS exception',l_mod_name,4);
4446           ROLLBACK TO IO_RCV_UPDATE;
4447           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4448           IF  FND_MSG_PUB.Check_Msg_Level
4449                   (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
4450           THEN
4451               FND_MSG_PUB.Add_Exc_Msg
4452                   (G_PKG_NAME ,
4453                    l_api_name  );
4454           END IF;
4455           FND_MSG_PUB.Count_And_Get
4456                   (p_count  =>  x_msg_count,
4457                    p_data   =>  x_msg_data );
4458 END IO_RCV_UPDATE;
4459 
4460 
4461 /*-------------------------------------------------------------------------------------*/
4462 /* Procedure name: IO_RCV_UPDATE_MOVE_OUT                                              */
4463 /* Description   : Procedure called from the Update api to update the depot tables     */
4464 /*                 for the receipts against Internal Requisitions for move out line    */
4465 /*                 It also logs activities for accept reject txn lines                 */
4466 /* Called from   : Called from SHIP_UPDATE API                                         */
4467 /* STANDARD PARAMETERS                                                                 */
4468 /*  In Parameters :                                                                    */
4469 /*    p_api_version       NUMBER    Required  Api Version number                       */
4470 /*    p_init_msg_list     VARCHAR2  Optional  To Initialize message stack              */
4471 /*    p_commit            VARCHAR2  Optional  Commits in API                           */
4472 /*    p_validation_level  NUMBER    Optional  validation level                         */
4473 /*                                                                                     */
4474 /*  Output Parameters:                                                                 */
4475 /*   x_return_status     VARCHAR2      Return status of the API                        */
4476 /*   x_msg_count         NUMBER        Number of messages in stack                     */
4477 /*   x_msg_data          VARCHAR2      Error Message from message stack                */
4478 /*                                                                                     */
4479 /* NON-STANDARD PARAMETERS                                                             */
4480 /*   In Parameters                                                                     */
4481 /*    p_order_header_id  NUMBER   Optional   Internal sales order Id                   */
4482 /* Output Parm :                                                                       */
4483 /* Change Hist :                                                                       */
4484 /*   24-Apr-2007  swai  Initial Creation.  Bug#5564180 / FP#5845995                    */
4485 /*-------------------------------------------------------------------------------------*/
4486 
4487 Procedure IO_RCV_UPDATE_MOVE_OUT
4488         ( p_api_version          IN   NUMBER,
4489           p_commit               IN   VARCHAR2,
4490           p_init_msg_list        IN   VARCHAR2,
4491           p_validation_level     IN   NUMBER,
4492           x_return_status        OUT  NOCOPY  VARCHAR2,
4493           x_msg_count            OUT  NOCOPY  NUMBER,
4494           x_msg_data             OUT  NOCOPY  VARCHAR2,
4495           p_order_header_id      IN   NUMBER
4496         ) IS
4497 
4498   -- Standard variables
4499   l_api_name            CONSTANT VARCHAR2(30) := 'IO_RCV_UPDATE_MOVE_OUT';
4500   l_api_version         CONSTANT NUMBER       := 1.0;
4501 
4502   -- Variables used in API
4503   l_rep_hist_id         number;
4504   l_dummy               varchar2(30);
4505   l_serialized_flag     boolean;
4506   l_ord_remaining_qty   number := 0;
4507   l_prod_txn_exists     boolean;
4508   l_prod_txn_id         number := NULL;
4509   l_prod_txn_status     csd_product_transactions.prod_txn_status%type;
4510   l_total_qty           number;
4511   l_total_del_qty       number;
4512   l_total_accept_qty    number;
4513   l_total_reject_qty    number;
4514   l_pt_accept_qty       number;
4515   l_pt_reject_qty       number;
4516   l_pt_del_qty          number;
4517   l_accept_qty          number;
4518   l_reject_qty          number;
4519   l_serial_num          MTL_SERIAL_NUMBERS.SERIAL_NUMBER%TYPE;
4520   l_lot_num             MTL_LOT_NUMBERS.LOT_NUMBER%TYPE;
4521   l_rep_line_id         number;
4522   l_rcvd_qty            number;
4523   l_line_qty            number;
4524   l_line_del_qty        number;
4525   l_sub_inv             varchar2(80);
4526   l_instance_id         number;
4527 
4528   -- activity record
4529   l_activity_rec      activity_rec_type;
4530 
4531   -- Variable for the FND log file
4532   l_error_level  number   := FND_LOG.LEVEL_ERROR;
4533   l_mod_name     varchar2(2000) := 'csd.plsql.csd_update_programs_pvt.io_rcv_update_move_out';
4534 
4535   -- User defined exception
4536   SKIP_RECORD    EXCEPTION;
4537   PROCESS_ERROR  EXCEPTION;
4538 
4539   -- Cursor to get the ro and product txn lines
4540   --
4541   CURSOR GET_RO_PROD_TXN_LINES_ALL IS
4542    select  cpt.product_transaction_id,
4543            cpt.prod_txn_status,
4544            cpt.repair_line_id,
4545            cpt.order_header_id,
4546            cpt.order_line_id,
4547            cpt.req_header_id,
4548            cpt.req_line_id,
4549            nvl(cpt.quantity_received,0) prod_txn_rcvd_qty,
4550            cra.quantity ro_qty,
4551            cra.quantity_rcvd ro_rcvd_qty,
4552            cra.inventory_item_id,
4553            cra.unit_of_measure ro_uom,
4554            prh.segment1 requisition_number,
4555            oel.ordered_quantity,
4556                  oeh.order_number
4557    from  csd_product_transactions cpt,
4558          csd_repairs cra,
4559          po_requisition_headers_all prh,
4560          oe_order_lines_all oel,
4561             oe_order_headers_all oeh
4562    where cpt.repair_line_id = cra.repair_line_id
4563     AND  cpt.req_header_id  = prh.requisition_header_id
4564     AND  cpt.order_line_id  = oel.line_id
4565     AND  oel.header_id      = oeh.header_id
4566     AND  cpt.action_type    = 'MOVE_OUT'
4567     AND  cpt.action_code    = 'USABLES'
4568     AND  cpt.prod_txn_status = 'SHIPPED'
4569     AND  cpt.order_line_id is not null;
4570     /*Bug#5564180/FP#5845995 below condition is not required for move out line
4571       otherwise once the move in line is received the move out line
4572      will not be updated with status received */
4573 --    AND  nvl(cra.quantity_rcvd,0) < cra.quantity;
4574 
4575   -- Cursor to get the ro and product txn lines
4576   -- for the order header id
4577   CURSOR GET_RO_PROD_TXN_LINES (p_ord_header_id in number) IS
4578    select  cpt.product_transaction_id,
4579            cpt.prod_txn_status,
4580            cpt.repair_line_id,
4581            cpt.order_header_id,
4582            cpt.order_line_id,
4583            cpt.req_header_id,
4584            cpt.req_line_id,
4585            nvl(cpt.quantity_received,0) prod_txn_rcvd_qty,
4586            cra.quantity ro_qty,
4587            cra.quantity_rcvd ro_rcvd_qty,
4588            cra.inventory_item_id,
4589            cra.unit_of_measure ro_uom,
4590            prh.segment1 requisition_number,
4591            oel.ordered_quantity,
4592                  oeh.order_number
4593    from  csd_product_transactions cpt,
4594          csd_repairs cra,
4595          po_requisition_headers_all prh,
4596          oe_order_lines_all oel,
4597             oe_order_headers_all oeh
4598    where cpt.repair_line_id = cra.repair_line_id
4599     AND  cpt.req_header_id  = prh.requisition_header_id
4600     AND  cpt.order_line_id  = oel.line_id
4601     AND  oel.header_id      = oeh.header_id
4602     AND  cpt.action_type    = 'MOVE_OUT'
4603     AND  cpt.action_code    = 'USABLES'
4604     AND  cpt.prod_txn_status = 'SHIPPED'
4605     AND  cpt.order_line_id is not null
4606     AND   cpt.order_header_id = p_ord_header_id;
4607     /*Bug#5564180/FP#5845995 below condition not required for move out line
4608       otherwise once the move in line is received the move out line will
4609      not be updated with status received */
4610 --    AND  nvl(cra.quantity_rcvd,0) < cra.quantity;
4611 
4612    RO IO_RCPT_LINES_Rec_Type;
4613 
4614 
4615   -- Cursor to get all the rcv txn lines of transaction type
4616   -- DELIVER,REJECT,ACCEPT type
4617   CURSOR GET_RCV_LINES (p_req_line_id in number) IS
4618    select rcv.transaction_id,
4619           rcv.quantity rcvd_qty,
4620           rcv.unit_of_measure,
4621           rcv.subinventory,
4622           rcv.locator_id,
4623                 rcv.organization_id,
4624           rcv.transaction_date received_date,
4625           rcv.transaction_type,
4626           rcv.shipment_header_id,
4627           rcv.shipment_line_id,
4628           prl.item_id,
4629           prl.destination_organization_id inv_org_id,
4630           prl.quantity requisition_qty,
4631           prh.segment1 requirement_number,
4632           mtl.serial_number_control_code,
4633                 mtl.comms_nl_trackable_flag ib_flag,
4634           mtl.lot_control_code,
4635              hao.name org_name
4636    from  rcv_transactions rcv,
4637          po_requisition_lines_all prl,
4638          po_requisition_headers_all prh,
4639          mtl_system_items mtl,
4640          hr_all_organization_units hao
4641    where rcv.requisition_line_id = prl.requisition_line_id
4642     and  prl.item_id             = mtl.inventory_item_id
4643     and  prl.destination_organization_id = mtl.organization_id
4644     and  prl.requisition_header_id = prh.requisition_header_id
4645     and  rcv.requisition_line_id = p_req_line_id
4646     and  hao.organization_id     = rcv.organization_id
4647     and  rcv.transaction_type in ('DELIVER','ACCEPT','REJECT');
4648 
4649     -- Cursor to get the deliver txn line for the
4650     -- specific txn id
4651     CURSOR DELIVER_LINES (p_txn_id in number) IS
4652       Select rcvt.transaction_id,
4653              rcvt.transaction_date received_date,
4654              rcvt.subinventory,
4655              rcvt.quantity rcvd_qty,
4656              rcvt.organization_id,
4657              rcvt.locator_id,
4658                    hao.name org_name
4659       from  rcv_transactions rcvt,
4660                hr_all_organization_units hao
4661      where rcvt.parent_transaction_id = p_txn_id
4662       and  rcvt.transaction_type  = 'DELIVER';
4663 
4664     CURSOR ORDER_INFO (p_ord_header_id in number) IS
4665      Select distinct
4666                order_header_id,
4667                order_line_id
4668      from  csd_product_transactions
4669         where order_header_id = p_ord_header_id
4670       AND  action_type     = 'MOVE_OUT'
4671       AND  action_code     = 'USABLES'
4672          AND  prod_txn_status = 'SHIPPED';
4673 
4674 BEGIN
4675    -- Standard Start of API savepoint
4676    SAVEPOINT  IO_RCV_UPDATE_MOVE_OUT;
4677 
4678    -- Initialize API return status to success
4679    x_return_status := FND_API.G_RET_STS_SUCCESS;
4680 
4681    -- Debug messages
4682    Debug('Beginning of IO_RCV_UPDATE_MOVE_OUT',l_mod_name,1);
4683    Debug('Order Header Id='||to_char(p_order_header_id),l_mod_name,1);
4684 
4685    -- Standard call to check for call compatibility.
4686    IF NOT FND_API.Compatible_API_Call (l_api_version,
4687                                        p_api_version,
4688                                        l_api_name   ,
4689                                        G_PKG_NAME   )
4690        THEN
4691           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4692    END IF;
4693 
4694    -- Initialize message list if p_init_msg_list is set to TRUE.
4695    IF FND_API.to_Boolean( p_init_msg_list ) THEN
4696          FND_MSG_PUB.initialize;
4697    END IF;
4698 
4699    -- Api body starts
4700 
4701    -- In case of Internal orders, the product txns are stamped
4702    -- with the order header id and line id.
4703    -- So Validate if it exists in csd_product_transactions
4704    IF  NVL(p_order_header_id,-999) <> -999 THEN
4705       BEGIN
4706           select 'EXISTS'
4707           into  l_dummy
4708           from  oe_order_headers_all oeh,
4709                 po_requisition_headers_all prh
4710           where oeh.source_document_id = prh.requisition_header_id
4711            and  oeh.header_id = p_order_header_id
4712            and  exists (select 'x'
4713                        from csd_product_transactions cpt
4714                        where cpt.action_type = 'MOVE_OUT'
4715                         and  cpt.action_code = 'USABLES'
4716                         and  cpt.order_header_id = oeh.header_id);
4717       EXCEPTION
4718           WHEN NO_DATA_FOUND THEN
4719              IF ( l_error_level >= G_debug_level) THEN
4720                  fnd_message.set_name('CSD','CSD_INV_ORD_HEADER_ID');
4721                  fnd_message.set_token('HEADER_ID',p_order_header_id);
4722                  FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
4723              ELSE
4724                  fnd_message.set_name('CSD','CSD_INV_ORD_HEADER_ID');
4725                  fnd_message.set_token('HEADER_ID',p_order_header_id);
4726                  fnd_msg_pub.add;
4727              END IF;
4728              RAISE FND_API.G_EXC_ERROR;
4729      END;
4730    END IF;
4731 
4732 
4733    --Loops thro the received lines that have txn type -'DELIVER','ACCEPT','REJECT'
4734    --While Processing the ACCEPT/REJECT txn lines, it also check if there is
4735    --any deliver txn lines with the parent txn id. If it finds then it also
4736    --update the delivered qty and logs activity for the deliver txns.
4737    --   Order Line
4738    --
4739    --    Line Id  Header Id   Ord Qty   Split_from_line_id
4740    --     L1          H1        1
4741    --     L2          H1        2           L1
4742    --     L3          H1        1           L2
4743    --
4744    --   RCV Lines
4745    --    Txn Id    Txn Type   Qty   Ord Line Id  Parent Txn Id
4746    --      T1      ACCEPT      1        L1
4747    --      T2      ACCEPT      2        L2
4748    --      T3      REJECT      1        L3
4749    --      T4      DELIVER     1        L1           T1
4750    --      T5      DELIVER     2        L2           T2
4751    --
4752    --
4753    --   1. Non- Serial
4754    --
4755    --     RO    RO  Qty  Prod Txn   Ord Line   Ord Header  Line Qty
4756    --     RO1    4       P1          L1           H1         1
4757    --     RO1    4       P2          L2           H1         2
4758    --     RO1    4       P3          L3           H1         1
4759    --   2. Serial
4760    --
4761    --     RO    RO  Qty  Prod Txn   Ord Line   Ord Header  Line Qty
4762    --     RO1    1        P1          L1           H1         1
4763    --     RO2    1        P2          L2           H1         1
4764    --     RO3    1        P3          L2           H1         1
4765    --     RO4    1        P4          L1           H1         1
4766    --
4767    -- THE PROGRAM IGNORES THE DELIVERED QUANTITY AGAINST THE REJECT TRANSACTIONS
4768    -- NEED TO REVIEW WITH PM AND IMPLEMENT AS A PHASE II PROJECT
4769 
4770    IF(p_order_header_id is null) THEN
4771       OPEN GET_RO_PROD_TXN_LINES_All;
4772    else
4773       OPEN GET_RO_PROD_TXN_LINES(p_order_header_id);
4774    END IF;
4775 
4776 
4777    /* Changed FOr loop for splitting the cursor : vijay 11/4/04*/
4778    --FOR RO IN GET_RO_PROD_TXN_LINES( p_order_header_id )
4779    LOOP
4780      BEGIN
4781        IF(p_order_header_id is null) THEN
4782           FETCH GET_RO_PROD_TXN_LINES_All INTO RO;
4783           EXIT WHEN GET_RO_PROD_TXN_LINES_All%NOTFOUND ;
4784        else
4785           FETCH GET_RO_PROD_TXN_LINES INTO RO;
4786           EXIT WHEN GET_RO_PROD_TXN_LINES%NOTFOUND ;
4787        END IF;
4788 
4789        -- savepoint
4790        SAVEPOINT RCV_LINES;
4791 
4792        -- Debug messages
4793        Debug('In RO loop',l_mod_name,1);
4794 
4795        FOR RCV IN GET_RCV_LINES (RO.req_line_id)
4796        LOOP
4797         BEGIN
4798           -- Debug messages
4799           Debug('In RCV loop',l_mod_name,1);
4800 
4801           IF RCV.transaction_type = 'ACCEPT' THEN
4802               --Handles Inspection Required Routing options
4803 
4804               -- Debug messages
4805               Debug('Processing Accept txn types',l_mod_name,1);
4806 
4807               select nvl(sum(paramn3),0)
4808               into  l_total_accept_qty
4809               from  csd_repair_history crh
4810               where crh.event_code = 'IP'
4811                and  crh.paramn1    = RCV.transaction_id;
4812 
4813               -- Debug messages
4814               Debug('l_total_accept_qty :'||l_total_accept_qty,l_mod_name,1);
4815               IF nvl(l_total_accept_qty,0) >= nvl(RCV.rcvd_qty,0) THEN
4816                   -- Debug messages
4817                   Debug('Skipping the record ',l_mod_name,1);
4818                   RAISE SKIP_RECORD;
4819               END IF;
4820 
4821               select nvl(sum(paramn4),0), nvl(sum(paramn3),0)
4822               into  l_pt_reject_qty, l_pt_accept_qty
4823               from  csd_repair_history crh
4824               where crh.event_code = 'IP'
4825                and  crh.paramn2    = RO.product_transaction_id;
4826                -- Debug messages
4827               Debug('l_pt_reject_qty :'||l_pt_reject_qty,l_mod_name,1);
4828               Debug('l_pt_accept_qty :'||l_pt_accept_qty,l_mod_name,1);
4829 
4830               IF RCV.serial_number_control_code in (2,5,6) THEN
4831                    l_line_qty := RO.ro_qty;
4832               ELSE
4833                    l_line_qty  := RO.ordered_quantity;
4834               END IF;
4835 
4836                -- Debug messages
4837               Debug('l_line_qty :'||l_line_qty,l_mod_name,1);
4838 
4839               IF (l_pt_reject_qty + l_pt_accept_qty) >= l_line_qty THEN
4840                    -- Debug messages
4841                   Debug('Exiting the RCV loop',l_mod_name,1);
4842                   EXIT;
4843               ELSE
4844                   l_accept_qty  :=  l_line_qty -(l_pt_reject_qty + l_pt_accept_qty);
4845                   IF RCV.rcvd_qty < l_accept_qty THEN
4846                        l_accept_qty := RCV.rcvd_qty;
4847                   END IF;
4848               END IF;
4849 
4850 
4851                     IF RCV.serial_number_control_code in (2,5,6) THEN
4852                  l_accept_qty := 1 ;
4853                     ELSE
4854                  l_accept_qty := l_accept_qty - l_total_accept_qty ;
4855                     END IF;
4856 
4857               -- Debug messages
4858               Debug('l_accept_qty :'||l_accept_qty,l_mod_name,1);
4859 
4860               -- Initialize the activity rec
4861               l_activity_rec := INIT_ACTIVITY_REC ;
4862 
4863               -- Assign the values for activity record
4864               l_activity_rec.REPAIR_HISTORY_ID := l_rep_hist_id;
4865               l_activity_rec.REPAIR_LINE_ID := RO.repair_line_id;
4866               l_activity_rec.EVENT_CODE     := 'IP';
4867               l_activity_rec.ACTION_CODE    := 0;
4868               l_activity_rec.EVENT_DATE     := RCV.received_date;
4869               l_activity_rec.QUANTITY       := l_accept_qty;
4870               l_activity_rec.PARAMN1        := RCV.transaction_id;
4871               l_activity_rec.PARAMN2        := RO.product_transaction_id;
4872               l_activity_rec.PARAMN3        := l_accept_qty;
4873               l_activity_rec.PARAMN6        :=  RO.req_header_id;
4874               l_activity_rec.PARAMC1        :=  RO.requisition_number ;
4875               l_activity_rec.PARAMC2        :=  RCV.subinventory;
4876               l_activity_rec.OBJECT_VERSION_NUMBER := null;
4877 
4878               -- Debug Messages
4879               Debug('Calling LOG_ACTIVITY',l_mod_name,2);
4880 
4881               -- Calling LOG_ACTIVITY for logging activity
4882               LOG_ACTIVITY
4883                     ( p_api_version     => p_api_version,
4884                       p_commit          => p_commit,
4885                       p_init_msg_list   => p_init_msg_list,
4886                       p_validation_level => p_validation_level,
4887                       x_return_status   => x_return_status,
4888                       x_msg_count       => x_msg_count,
4889                       x_msg_data        => x_msg_data,
4890                       p_activity_rec    => l_activity_rec );
4891 
4892               -- Debug Messages
4893               Debug('Return Status from LOG_ACTIVITY:'||x_return_status,l_mod_name,2);
4894               IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
4895                  Debug('LOG_ACTIVITY api failed ',l_mod_name,4);
4896                  RAISE FND_API.G_EXC_ERROR;
4897               END IF;
4898 
4899               -- Check if there are "DELIVER" txn lines against the accept lines
4900               -- If found, then process the deliver lines also
4901               FOR DEL IN DELIVER_LINES(RCV.transaction_id)
4902               LOOP
4903                BEGIN
4904                  -- Debug messages
4905                  Debug('In DEL Loop ',l_mod_name,1);
4906 
4907                  select nvl(sum(quantity),0)
4908                   into  l_total_del_qty
4909                   from csd_repair_history crh
4910                  where crh.event_code = 'RRI'
4911                   and  crh.paramn1    = DEL.transaction_id;
4912                  -- Debug messages
4913                  Debug('l_total_del_qty : '||l_total_del_qty,l_mod_name,1);
4914 
4915                  IF l_total_del_qty >= DEL.rcvd_qty THEN
4916                      -- Debug messages
4917                      Debug('Skipping the record',l_mod_name,1);
4918                      RAISE SKIP_RECORD;
4919                  END IF;
4920 
4921                  select nvl(sum(quantity),0)
4922                   into  l_pt_del_qty
4923                   from csd_repair_history crh
4924                  where crh.event_code = 'RRI'
4925                   and  crh.paramn2 = RO.product_transaction_id;
4926                   -- Debug messages
4927                   Debug('l_pt_del_qty : '||l_pt_del_qty,l_mod_name,1);
4928 
4929                  IF RCV.serial_number_control_code in (2,5,6) THEN
4930                      -- SERIALIZED CASE
4931                      Begin
4932                       select rcvt.serial_num,
4933                              rcvt.lot_num
4934                        into  l_serial_num,
4935                              l_lot_num
4936                        from  rcv_serial_transactions rcvt
4937                       where  rcvt.transaction_id = DEL.transaction_id
4938                        and   rownum = 1
4939                        and   not exists (Select 'NOT EXIST'
4940                                          from csd_repairs cra,
4941                                               csd_product_transactions cpt
4942                                          where cra.repair_line_id = cpt.repair_line_id
4943                                           and  cpt.action_type    = 'MOVE_OUT'
4944                                           and  cpt.order_header_id = ro.order_header_id
4945                                           and  cra.serial_number  = rcvt.serial_num);
4946                     Exception
4947                         WHEN NO_DATA_FOUND THEN
4948                              IF ( l_error_level >= G_debug_level) THEN
4949                                 fnd_message.set_name('CSD','CSD_SERIAL_NUM_MISSING');
4950                                 fnd_message.set_token('TXN_ID',DEL.transaction_id);
4951                                 FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
4952                              ELSE
4953                                 fnd_message.set_name('CSD','CSD_SERIAL_NUM_MISSING');
4954                                 fnd_message.set_token('TXN_ID',DEL.transaction_id);
4955                                 fnd_msg_pub.add;
4956                              END If;
4957                              RAISE PROCESS_ERROR;
4958                     END;
4959                     -- Get the instance id from
4960                                 -- installbase tables
4961                                 IF NVL(RCV.ib_flag,'N') = 'Y' THEN
4962                                   BEGIN
4963                          Select instance_id
4964                                      into   l_instance_id
4965                                      from  csi_item_instances
4966                                      where inventory_item_id = RCV.item_id
4967                                       and  serial_number     = l_serial_num;
4968                                   Exception
4969                                      When NO_DATA_FOUND THEN
4970                             IF ( l_error_level >= G_debug_level) THEN
4971                                 fnd_message.set_name('CSD','CSD_ITEM_INSTANCE_MISSING');
4972                                 fnd_message.set_token('SERIAL_NUM',l_serial_num);
4973                                 FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
4974                             ELSE
4975                                 fnd_message.set_name('CSD','CSD_ITEM_INSTANCE_MISSING');
4976                                 fnd_message.set_token('SERIAL_NUM',l_serial_num);
4977                                 fnd_msg_pub.add;
4978                             END If;
4979                                      When Others THEN
4980                             IF ( l_error_level >= G_debug_level) THEN
4981                                 fnd_message.set_name('CSD','CSD_INV_SERIAL_NUM');
4982                                 fnd_message.set_token('SERIAL_NUM',l_serial_num);
4983                                 FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
4984                             ELSE
4985                                 fnd_message.set_name('CSD','CSD_INV_SERIAL_NUM');
4986                                 fnd_message.set_token('SERIAL_NUM',l_serial_num);
4987                                 fnd_msg_pub.add;
4988                             END If;
4989                       End;
4990                     ELSE
4991                                   l_instance_id := NULL;
4992                     END If;
4993 
4994                     l_line_del_qty  := RO.ro_qty;
4995                 ELSE
4996                     -- Non-Serialized Case
4997                     l_serial_num := NULL;
4998                     l_line_del_qty   := RO.ordered_quantity;
4999                     l_instance_id := NULL;
5000 
5001                     --lot_control_code = 1 No control
5002                     --lot_control_code = 2 Full control
5003                    IF RCV.lot_control_code = 2 THEN
5004                      BEGIN
5005                        Select lot_num
5006                        into   l_lot_num
5007                        from rcv_lot_transactions
5008                        where source_transaction_id = DEL.transaction_id;
5009                     EXCEPTION
5010                       WHEN NO_DATA_FOUND THEN
5011                           IF ( l_error_level >= G_debug_level) THEN
5012                               fnd_message.set_name('CSD','CSD_INV_TXN_ID');
5013                               fnd_message.set_token('TXN_ID',DEL.transaction_id);
5014                               FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
5015                           ELSE
5016                               fnd_message.set_name('CSD','CSD_INV_TXN_ID');
5017                               fnd_message.set_token('TXN_ID',DEL.transaction_id);
5018                               fnd_msg_pub.add;
5019                           END IF;
5020                           RAISE FND_API.G_EXC_ERROR;
5021                     END;
5022                   ELSE
5023                       l_lot_num := NULL;
5024                   END IF;
5025                 END IF;
5026 
5027                 IF l_pt_del_qty >= l_line_del_qty THEN
5028                     -- Debug messages
5029                     Debug('Exiting the loop',l_mod_name,1);
5030                     EXIT;
5031                 ELSE
5032                     l_rcvd_qty := l_line_del_qty - l_pt_del_qty ;
5033                     IF DEL.rcvd_qty < l_rcvd_qty THEN
5034                         l_rcvd_qty := DEL.rcvd_qty;
5035                     END IF;
5036                 END IF;
5037 
5038                          IF RCV.serial_number_control_code in (2,5,6) THEN
5039                    l_rcvd_qty  := 1;
5040                          ELSE
5041                    l_rcvd_qty  := l_rcvd_qty - l_total_del_qty;
5042                 END IF;
5043 
5044                 -- Debug messages
5045                 Debug('l_rcvd_qty : '||l_rcvd_qty,l_mod_name,1);
5046 
5047                 -- Update the serial number on repair order
5048                 -- with the rcvd serial number
5049              /*Bug#5564180/FP#5845995 Move out line should not update the received quantity of
5050              RO. Move out line should not change the serial and instance number of RO
5051              so commenting the below query
5052              */
5053              /*
5054                 UPDATE CSD_REPAIRS
5055                 SET SERIAL_NUMBER = l_serial_num,
5056                     quantity_rcvd = nvl(quantity_rcvd,0) + l_rcvd_qty,
5057                              customer_product_id = l_instance_id,
5058                           object_version_number = object_version_number+1,
5059                     last_update_date = sysdate,
5060                     last_updated_by  = fnd_global.user_id,
5061                     last_update_login = fnd_global.login_id
5062                 WHERE repair_line_id = RO.repair_line_id;
5063                 IF SQL%NOTFOUND THEN
5064                    IF ( l_error_level >= G_debug_level) THEN
5065                         fnd_message.set_name('CSD','CSD_RO_UPD_FAILED');
5066                         fnd_message.set_token('REPAIR_LINE_ID',RO.repair_line_id);
5067                         FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
5068                    ELSE
5069                         fnd_message.set_name('CSD','CSD_RO_UPD_FAILED');
5070                         fnd_message.set_token('REPAIR_LINE_ID',RO.repair_line_id);
5071                         fnd_msg_pub.add;
5072                    END IF;
5073                    RAISE PROCESS_ERROR;
5074                 END IF;
5075                 */
5076                 -- Updating the product txn with the serial number,lot number
5077                 -- qty rcvd and subinventory
5078                 -- sub_inventory_rcvd is used for IO and subinventory column
5079                 -- is used for the regular RMA
5080                 UPDATE CSD_PRODUCT_TRANSACTIONS
5081                 SET SOURCE_SERIAL_NUMBER = l_serial_num,
5082                              source_instance_id   = l_instance_id,
5083                     LOT_NUMBER_RCVD      = l_lot_num,
5084                     LOCATOR_ID           = DEL.locator_id,
5085                     QUANTITY_RECEIVED  = NVL(QUANTITY_RECEIVED,0) + l_rcvd_qty,
5086                     SUB_INVENTORY_RCVD = DEL.subinventory,
5087                           object_version_number = object_version_number+1,
5088                     last_update_date = sysdate,
5089                     last_updated_by  = fnd_global.user_id,
5090                     last_update_login = fnd_global.login_id
5091                 WHERE product_transaction_id = RO.product_transaction_id;
5092                 IF SQL%NOTFOUND THEN
5093                      IF ( l_error_level >= G_debug_level) THEN
5094                          fnd_message.set_name('CSD','CSD_PROD_TXN_UPD_FAILED');
5095                          fnd_message.set_token('PROD_TXN_ID',RO.product_transaction_id);
5096                          FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
5097                      ELSE
5098                          fnd_message.set_name('CSD','CSD_PROD_TXN_UPD_FAILED');
5099                          fnd_message.set_token('PROD_TXN_ID',RO.product_transaction_id);
5100                          fnd_msg_pub.add;
5101                      END IF;
5102                      RAISE PROCESS_ERROR;
5103                 END IF;
5104 
5105                          IF RCV.serial_number_control_code in (2,5,6) THEN
5106                             UPDATE CSD_PRODUCT_TRANSACTIONS
5107                    SET prod_txn_status       = 'RECEIVED',
5108                              object_version_number = object_version_number+1,
5109                        last_update_date = sysdate,
5110                        last_updated_by  = fnd_global.user_id,
5111                        last_update_login = fnd_global.login_id
5112                    WHERE product_transaction_id = RO.product_transaction_id;
5113                 ELSE
5114                             UPDATE CSD_PRODUCT_TRANSACTIONS
5115                    SET prod_txn_status       = 'RECEIVED',
5116                              object_version_number = object_version_number+1,
5117                        last_update_date = sysdate,
5118                        last_updated_by  = fnd_global.user_id,
5119                        last_update_login = fnd_global.login_id
5120                    WHERE quantity_received = RO.ordered_quantity
5121                             and   product_transaction_id = RO.product_transaction_id;
5122                          END IF;
5123 
5124                 -- Initialize the activity rec
5125                 l_activity_rec := INIT_ACTIVITY_REC ;
5126 
5127                 -- Assign the values for activity record
5128                 l_activity_rec.REPAIR_HISTORY_ID := l_rep_hist_id;
5129                 l_activity_rec.REPAIR_LINE_ID := RO.repair_line_id;
5130                 l_activity_rec.EVENT_CODE     := 'RRI';
5131                 l_activity_rec.ACTION_CODE    := 0;
5132                 l_activity_rec.EVENT_DATE     := DEL.received_date;
5133                 l_activity_rec.QUANTITY       := l_rcvd_qty;
5134                 l_activity_rec.PARAMN1        := DEL.transaction_id;
5135                 l_activity_rec.PARAMN2        := RO.product_transaction_id;
5136                 l_activity_rec.PARAMN3        := DEL.organization_id;
5137                 l_activity_rec.PARAMN6        := RO.req_header_id;
5138                 l_activity_rec.PARAMC1        := RO.order_number;
5139                 l_activity_rec.PARAMC2        := RO.requisition_number;
5140                 l_activity_rec.PARAMC3        := DEL.org_name;
5141                 l_activity_rec.PARAMC4        := DEL.subinventory;
5142                 l_activity_rec.OBJECT_VERSION_NUMBER := null;
5143 
5144                 -- Debug Messages
5145                 Debug('Calling LOG_ACTIVITY',l_mod_name,2);
5146 
5147                 -- Calling LOG_ACTIVITY for logging activity
5148                 -- Receipt of item against Internal Requisition
5149                 LOG_ACTIVITY
5150                      ( p_api_version     => p_api_version,
5151                        p_commit          => p_commit,
5152                        p_init_msg_list   => p_init_msg_list,
5153                        p_validation_level => p_validation_level,
5154                        x_return_status   => x_return_status,
5155                        x_msg_count       => x_msg_count,
5156                        x_msg_data        => x_msg_data,
5157                        p_activity_rec    => l_activity_rec );
5158 
5159                 -- Debug Messages
5160                 Debug('Return Status from LOG_ACTIVITY:'||x_return_status,l_mod_name,2);
5161                 IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
5162                     Debug('LOG_ACTIVITY api failed ',l_mod_name,4);
5163                     RAISE FND_API.G_EXC_ERROR;
5164                 END IF;
5165 
5166                 IF (l_pt_del_qty + l_rcvd_qty) >= l_line_del_qty  THEN
5167                     Debug(' Exiting the DEL loop',l_mod_name,1);
5168                     EXIT;
5169                 END IF;
5170               Exception
5171                     WHEN PROCESS_ERROR THEN
5172                          Debug('In process_error exception ',l_mod_name,4);
5173                          RAISE PROCESS_ERROR;
5174                     WHEN SKIP_RECORD THEN
5175                          NULL;
5176               END;
5177             END LOOP; -- end of delivery lines
5178 
5179             IF (l_pt_reject_qty+ l_pt_accept_qty + l_accept_qty)>= l_line_qty THEN
5180                   Debug('Exiting RCV the loop ',l_mod_name,1);
5181                   EXIT;
5182             END IF;
5183 
5184        ELSIF RCV.transaction_type = 'REJECT' THEN
5185                -- Handles Inspection Required routing option
5186 
5187                select nvl(sum(paramn4),0)
5188                 into  l_total_reject_qty
5189                from  csd_repair_history crh
5190                where crh.event_code = 'IP'
5191                 and  crh.paramn1    = RCV.transaction_id;
5192                IF l_total_reject_qty >= RCV.rcvd_qty THEN
5193                    -- Debug messages
5194                    Debug('Skipping the record ',l_mod_name,1);
5195                    RAISE SKIP_RECORD;
5196                END IF;
5197 
5198                select nvl(sum(paramn3),0),nvl(sum(paramn4),0)
5199                 into  l_pt_accept_qty, l_pt_reject_qty
5200                from  csd_repair_history crh
5201                where crh.event_code = 'IP'
5202                 and  crh.paramn2 = RO.product_transaction_id;
5203                -- Debug messages
5204               Debug('l_pt_accept_qty'||l_pt_accept_qty,l_mod_name,1);
5205               Debug('l_pt_reject_qty'||l_pt_reject_qty,l_mod_name,1);
5206 
5207               IF RCV.serial_number_control_code in (2,5,6) THEN
5208                   l_line_qty := 1;
5209               ELSE
5210                   l_line_qty  := RO.ordered_quantity;
5211               END IF;
5212               IF l_pt_accept_qty+ l_pt_reject_qty >= l_line_qty THEN
5213                    -- Debug messages
5214                    Debug('Exiting the loop ',l_mod_name,1);
5215                    EXIT;
5216               ELSE
5217                   l_reject_qty := l_line_qty- (l_pt_accept_qty+ l_pt_reject_qty);
5218                   IF RCV.rcvd_qty < l_reject_qty THEN
5219                       l_reject_qty := RCV.rcvd_qty;
5220                   END IF;
5221               END IF;
5222 
5223                     IF RCV.serial_number_control_code in (2,5,6) THEN
5224                  l_reject_qty := 1;
5225                     ELSE
5226                  l_reject_qty := l_reject_qty - l_total_reject_qty;
5227               END IF;
5228 
5229               -- Debug messages
5230               Debug('l_reject_qty'||l_reject_qty,l_mod_name,1);
5231 
5232                -- Initialize the activity rec
5233               l_activity_rec := INIT_ACTIVITY_REC ;
5234 
5235                -- Assign the values for activity record
5236               l_activity_rec.REPAIR_HISTORY_ID := l_rep_hist_id;
5237               l_activity_rec.REPAIR_LINE_ID :=  RO.repair_line_id;
5238               l_activity_rec.EVENT_CODE     :=  'IP';
5239               l_activity_rec.ACTION_CODE    :=  0;
5240               l_activity_rec.EVENT_DATE     :=  RCV.received_date;
5241               l_activity_rec.QUANTITY       :=  l_reject_qty;
5242               l_activity_rec.PARAMN1        :=  RCV.transaction_id;
5243               l_activity_rec.PARAMN2        :=  RO.product_transaction_id;
5244               l_activity_rec.PARAMN3        :=  null;
5245               l_activity_rec.PARAMN4        :=  l_reject_qty;
5246               l_activity_rec.PARAMN6        :=  RO.req_header_id;
5247               l_activity_rec.PARAMC1        :=  RO.requisition_number;
5248               l_activity_rec.PARAMC2        :=  rcv.subinventory;
5249               l_activity_rec.OBJECT_VERSION_NUMBER := null;
5250 
5251               -- Debug Messages
5252               Debug('Calling LOG_ACTIVITY',l_mod_name,2);
5253 
5254               -- Calling LOG_ACTIVITY for logging activity
5255               LOG_ACTIVITY
5256                      ( p_api_version     => p_api_version,
5257                        p_commit          => p_commit,
5258                        p_init_msg_list   => p_init_msg_list,
5259                        p_validation_level => p_validation_level,
5260                        x_return_status   => x_return_status,
5261                        x_msg_count       => x_msg_count,
5262                        x_msg_data        => x_msg_data,
5263                        p_activity_rec    => l_activity_rec );
5264 
5265               -- Debug Messages
5266              Debug('Return Status from LOG_ACTIVITY:'||x_return_status,l_mod_name,2);
5267              IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
5268                  Debug('LOG_ACTIVITY api failed ',l_mod_name,4);
5269                  RAISE FND_API.G_EXC_ERROR;
5270              END IF;
5271 
5272               -- Check if there are "DELIVER" txn lines against the reject lines
5273               -- If found, then process the deliver lines also
5274               FOR DEL IN DELIVER_LINES(RCV.transaction_id)
5275               LOOP
5276                BEGIN
5277                  -- Debug messages
5278                  Debug('In DEL Loop ',l_mod_name,1);
5279 
5280                  select nvl(sum(quantity),0)
5281                   into  l_total_del_qty
5282                   from csd_repair_history crh
5283                  where crh.event_code = 'RRI'
5284                   and  crh.paramn1    = DEL.transaction_id;
5285                  -- Debug messages
5286                  Debug('l_total_del_qty : '||l_total_del_qty,l_mod_name,1);
5287 
5288                  IF l_total_del_qty >= DEL.rcvd_qty THEN
5289                      -- Debug messages
5290                      Debug('Skipping the record',l_mod_name,1);
5291                      RAISE SKIP_RECORD;
5292                  END IF;
5293 
5294                  select nvl(sum(quantity),0)
5295                   into  l_pt_del_qty
5296                   from csd_repair_history crh
5297                  where crh.event_code = 'RRI'
5298                   and  crh.paramn2 = RO.product_transaction_id;
5299                   -- Debug messages
5300                   Debug('l_pt_del_qty : '||l_pt_del_qty,l_mod_name,1);
5301 
5302                  IF RCV.serial_number_control_code in (2,5,6) THEN
5303                      -- SERIALIZED CASE
5304                      Begin
5305                       select rcvt.serial_num,
5306                              rcvt.lot_num
5307                        into  l_serial_num,
5308                              l_lot_num
5309                        from  rcv_serial_transactions rcvt
5310                       where  rcvt.transaction_id = DEL.transaction_id
5311                        and   rownum = 1
5312                        and   not exists (Select 'NOT EXIST'
5313                                          from csd_repairs cra,
5314                                               csd_product_transactions cpt
5315                                          where cra.repair_line_id = cpt.repair_line_id
5316                                           and  cpt.action_type    = 'MOVE_OUT'
5317                                           and  cpt.order_header_id = ro.order_header_id
5318                                           and  cra.serial_number  = rcvt.serial_num);
5319                     Exception
5320                         WHEN NO_DATA_FOUND THEN
5321                              IF ( l_error_level >= G_debug_level) THEN
5322                                 fnd_message.set_name('CSD','CSD_SERIAL_NUM_MISSING');
5323                                 fnd_message.set_token('TXN_ID',DEL.transaction_id);
5324                                 FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
5325                              ELSE
5326                                 fnd_message.set_name('CSD','CSD_SERIAL_NUM_MISSING');
5327                                 fnd_message.set_token('TXN_ID',DEL.transaction_id);
5328                                 fnd_msg_pub.add;
5329                              END If;
5330                              RAISE PROCESS_ERROR;
5331                     END;
5332                     -- Get the instance id from
5333                           -- installbase tables
5334                           IF NVL(RCV.ib_flag,'N') = 'Y' THEN
5335                                   BEGIN
5336                          Select instance_id
5337                                   into   l_instance_id
5338                                   from  csi_item_instances
5339                                   where inventory_item_id = RCV.item_id
5340                                    and  serial_number     = l_serial_num;
5341                             Exception
5342                                  When NO_DATA_FOUND THEN
5343                           IF ( l_error_level >= G_debug_level) THEN
5344                              fnd_message.set_name('CSD','CSD_ITEM_INSTANCE_MISSING');
5345                              fnd_message.set_token('SERIAL_NUM',l_serial_num);
5346                              FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
5347                           ELSE
5348                              fnd_message.set_name('CSD','CSD_ITEM_INSTANCE_MISSING');
5349                              fnd_message.set_token('SERIAL_NUM',l_serial_num);
5350                              fnd_msg_pub.add;
5351                           END If;
5352                                  When Others THEN
5353                           IF ( l_error_level >= G_debug_level) THEN
5354                              fnd_message.set_name('CSD','CSD_INV_SERIAL_NUM');
5355                              fnd_message.set_token('SERIAL_NUM',l_serial_num);
5356                              FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
5357                           ELSE
5358                              fnd_message.set_name('CSD','CSD_INV_SERIAL_NUM');
5359                              fnd_message.set_token('SERIAL_NUM',l_serial_num);
5360                              fnd_msg_pub.add;
5361                           END If;
5362                       End;
5363                     ELSE
5364                                   l_instance_id := NULL;
5365                     END IF;
5366 
5367                    l_line_del_qty  := RO.ro_qty;
5368                 ELSE
5369                     -- Non-Serialized Case
5370                     l_serial_num := NULL;
5371                     l_line_del_qty   := RO.ordered_quantity;
5372                     l_instance_id  := NULL;
5373 
5374                     --lot_control_code = 1 No control
5375                     --lot_control_code = 2 Full control
5376                    IF RCV.lot_control_code = 2 THEN
5377                      BEGIN
5378                        Select lot_num
5379                        into   l_lot_num
5380                        from rcv_lot_transactions
5381                        where source_transaction_id = DEL.transaction_id;
5382                     EXCEPTION
5383                       WHEN NO_DATA_FOUND THEN
5384                           IF ( l_error_level >= G_debug_level) THEN
5385                               fnd_message.set_name('CSD','CSD_INV_TXN_ID');
5386                               fnd_message.set_token('TXN_ID',DEL.transaction_id);
5387                               FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
5388                           ELSE
5389                               fnd_message.set_name('CSD','CSD_INV_TXN_ID');
5390                               fnd_message.set_token('TXN_ID',DEL.transaction_id);
5391                               fnd_msg_pub.add;
5392                           END IF;
5393                           RAISE FND_API.G_EXC_ERROR;
5394                     END;
5395                   ELSE
5396                       l_lot_num := NULL;
5397                   END IF;
5398                 END IF;
5399 
5400                 IF l_pt_del_qty >= l_line_del_qty THEN
5401                     -- Debug messages
5402                     Debug('Exiting the loop',l_mod_name,1);
5403                     EXIT;
5404                 ELSE
5405                     l_rcvd_qty := l_line_del_qty - l_pt_del_qty ;
5406                     IF DEL.rcvd_qty < l_rcvd_qty THEN
5407                         l_rcvd_qty := DEL.rcvd_qty;
5408                     END IF;
5409                 END IF;
5410 
5411                          IF RCV.serial_number_control_code in (2,5,6) THEN
5412                    l_rcvd_qty  := 1;
5413                          ELSE
5414                    l_rcvd_qty  := l_rcvd_qty - l_total_del_qty;
5415                 END IF;
5416 
5417                 -- Debug messages
5418                 Debug('l_rcvd_qty : '||l_rcvd_qty,l_mod_name,1);
5419 
5420                 -- Update the serial number on repair order
5421                 -- with the rcvd serial number
5422                 /*Bug#5564180/FP#5845995 Move out line should not update the received quantity of
5423                 RO. Move out line should not change the serial and instance number of RO
5424                  so commenting the below query
5425                */
5426             /*
5427                 UPDATE CSD_REPAIRS
5428                 SET SERIAL_NUMBER = l_serial_num,
5429                     quantity_rcvd = nvl(quantity_rcvd,0) + l_rcvd_qty,
5430                                 customer_product_id   = l_instance_id,
5431                           object_version_number = object_version_number+1,
5432                     last_update_date = sysdate,
5433                     last_updated_by  = fnd_global.user_id,
5434                     last_update_login = fnd_global.login_id
5435                 WHERE repair_line_id = RO.repair_line_id;
5436                 IF SQL%NOTFOUND THEN
5437                    IF ( l_error_level >= G_debug_level) THEN
5438                         fnd_message.set_name('CSD','CSD_RO_UPD_FAILED');
5439                         fnd_message.set_token('REPAIR_LINE_ID',RO.repair_line_id);
5440                         FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
5441                    ELSE
5442                         fnd_message.set_name('CSD','CSD_RO_UPD_FAILED');
5443                         fnd_message.set_token('REPAIR_LINE_ID',RO.repair_line_id);
5444                         fnd_msg_pub.add;
5445                    END IF;
5446                    RAISE PROCESS_ERROR;
5447                 END IF;
5448                 */
5449 
5450                 -- Updating the product txn with the serial number,lot number
5451                 -- qty rcvd and subinventory
5452                 -- sub_inventory_rcvd is used for IO and subinventory column
5453                 -- is used for the regular RMA
5454                 UPDATE CSD_PRODUCT_TRANSACTIONS
5455                 SET SOURCE_SERIAL_NUMBER = l_serial_num,
5456                              source_instance_id   = l_instance_id,
5457                     LOT_NUMBER_RCVD      = l_lot_num,
5458                     LOCATOR_ID           = DEL.locator_id,
5459                     QUANTITY_RECEIVED  = NVL(QUANTITY_RECEIVED,0) + l_rcvd_qty,
5460                     SUB_INVENTORY_RCVD = DEL.subinventory,
5461                           object_version_number = object_version_number+1,
5462                     last_update_date = sysdate,
5463                     last_updated_by  = fnd_global.user_id,
5464                     last_update_login = fnd_global.login_id
5465                 WHERE product_transaction_id = RO.product_transaction_id;
5466                 IF SQL%NOTFOUND THEN
5467                      IF ( l_error_level >= G_debug_level) THEN
5468                          fnd_message.set_name('CSD','CSD_PROD_TXN_UPD_FAILED');
5469                          fnd_message.set_token('PROD_TXN_ID',RO.product_transaction_id);
5470                          FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
5471                      ELSE
5472                          fnd_message.set_name('CSD','CSD_PROD_TXN_UPD_FAILED');
5473                          fnd_message.set_token('PROD_TXN_ID',RO.product_transaction_id);
5474                          fnd_msg_pub.add;
5475                      END IF;
5476                      RAISE PROCESS_ERROR;
5477                 END IF;
5478 
5479                 IF RCV.serial_number_control_code in (2,5,6) THEN
5480                             UPDATE CSD_PRODUCT_TRANSACTIONS
5481                    SET prod_txn_status       = 'RECEIVED',
5482                              object_version_number = object_version_number+1,
5483                        last_update_date = sysdate,
5484                        last_updated_by  = fnd_global.user_id,
5485                        last_update_login = fnd_global.login_id
5486                    WHERE product_transaction_id = RO.product_transaction_id;
5487 
5488                          ELSE
5489                             UPDATE CSD_PRODUCT_TRANSACTIONS
5490                    SET prod_txn_status       = 'RECEIVED',
5491                              object_version_number = object_version_number+1,
5492                        last_update_date = sysdate,
5493                        last_updated_by  = fnd_global.user_id,
5494                        last_update_login = fnd_global.login_id
5495                    WHERE quantity_received = RO.ordered_quantity
5496                             and   product_transaction_id = RO.product_transaction_id;
5497                 END IF;
5498 
5499                 -- Initialize the activity rec
5500                 l_activity_rec := INIT_ACTIVITY_REC ;
5501 
5502                 -- Assign the values for activity record
5503                 l_activity_rec.REPAIR_HISTORY_ID := l_rep_hist_id;
5504                 l_activity_rec.REPAIR_LINE_ID := RO.repair_line_id;
5505                 l_activity_rec.EVENT_CODE     := 'RRI';
5506                 l_activity_rec.ACTION_CODE    := 0;
5507                 l_activity_rec.EVENT_DATE     := DEL.received_date;
5508                 l_activity_rec.QUANTITY       := l_rcvd_qty;
5509                 l_activity_rec.PARAMN1        := DEL.transaction_id;
5510                 l_activity_rec.PARAMN2        := RO.product_transaction_id;
5511                 l_activity_rec.PARAMN3        := DEL.organization_id;
5512                 l_activity_rec.PARAMN6        := RO.req_header_id;
5513                 l_activity_rec.PARAMC1        := RO.order_number;
5514                 l_activity_rec.PARAMC2        := RO.requisition_number;
5515                 l_activity_rec.PARAMC3        := DEL.org_name;
5516                 l_activity_rec.PARAMC4        := DEL.subinventory;
5517                 l_activity_rec.OBJECT_VERSION_NUMBER := null;
5518 
5519                 -- Debug Messages
5520                 Debug('Calling LOG_ACTIVITY',l_mod_name,2);
5521 
5522                 -- Calling LOG_ACTIVITY for logging activity
5523                 -- Receipt of item against Internal Requisition
5524                 LOG_ACTIVITY
5525                      ( p_api_version     => p_api_version,
5526                        p_commit          => p_commit,
5527                        p_init_msg_list   => p_init_msg_list,
5528                        p_validation_level => p_validation_level,
5529                        x_return_status   => x_return_status,
5530                        x_msg_count       => x_msg_count,
5531                        x_msg_data        => x_msg_data,
5532                        p_activity_rec    => l_activity_rec );
5533 
5534                 -- Debug Messages
5535                 Debug('Return Status from LOG_ACTIVITY:'||x_return_status,l_mod_name,2);
5536                 IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
5537                     Debug('LOG_ACTIVITY api failed ',l_mod_name,4);
5538                     RAISE FND_API.G_EXC_ERROR;
5539                 END IF;
5540 
5541                 IF (l_pt_del_qty + l_rcvd_qty) >= l_line_del_qty  THEN
5542                     Debug(' Exiting the DEL loop',l_mod_name,1);
5543                     EXIT;
5544                 END IF;
5545               Exception
5546                     WHEN PROCESS_ERROR THEN
5547                          Debug('In process_error exception ',l_mod_name,4);
5548                          RAISE PROCESS_ERROR;
5549                     WHEN SKIP_RECORD THEN
5550                          NULL;
5551               END;
5552             END LOOP; -- end of delivery lines
5553 
5554             IF l_pt_accept_qty+ l_pt_reject_qty+ l_reject_qty >= l_line_qty THEN
5555                  Debug(' Exiting the RCV loop',l_mod_name,1);
5556                  EXIT;
5557             END IF;
5558 
5559       ELSIF RCV.transaction_type = 'DELIVER' THEN
5560          -- Handles the Direct Delivery and standard routing options
5561 
5562            select nvl(sum(quantity),0)
5563             into  l_total_qty
5564             from csd_repair_history crh
5565            where crh.event_code = 'RRI'
5566             and  crh.paramn1    = RCV.transaction_id;
5567            IF l_total_qty >= RCV.rcvd_qty THEN
5568               -- Debug messages
5569               Debug(' Skipping the record',l_mod_name,1);
5570               RAISE SKIP_RECORD;
5571            END IF;
5572 
5573            select nvl(sum(quantity),0)
5574            into  l_pt_del_qty
5575            from  csd_repair_history crh
5576            where crh.event_code = 'RRI'
5577             and  paramn2        = RO.product_transaction_id;
5578 
5579            -- Debug messages
5580            Debug('l_pt_reject_qty='||l_pt_reject_qty,l_mod_name,1);
5581            Debug('l_pt_del_qty   ='||l_pt_del_qty,l_mod_name,1);
5582 
5583            IF RCV.serial_number_control_code in (2,5,6) THEN
5584                -- SERIALIZED AND LOT CONTROLLED CASE
5585               l_line_qty  := RO.ro_qty;
5586                Begin
5587                 select rcvt.serial_num,
5588                        rcvt.lot_num
5589                  into  l_serial_num,
5590                        l_lot_num
5591                  from  rcv_serial_transactions rcvt
5592                 where  rcvt.transaction_id = RCV.transaction_id
5593                  and   rownum = 1;
5594 
5595 --bug#7452134.
5596 --Due to bug#7452134, the customer BROOKS AUTOMATION ran into this issue if they
5597 --try to recieve an item from diff operating unit. When user click update logistic
5598 --button, then this error raising. From what I see on the customer's environment
5599 --This code (condition) should not be here. I am not sure why we are checking
5600 --the serial number in the production transaction table. After commented this
5601 --condition, it worked for the customer.
5602 --                 and   not exists (Select 'NOT EXIST'
5603 --                                   from csd_repairs cra,
5604 --                                        csd_product_transactions cpt
5605 --                                   where cra.repair_line_id = cpt.repair_line_id
5606 --                                    and  cpt.order_header_id = ro.order_header_id
5607 --                                    and  cra.serial_number  = rcvt.serial_num);
5608               Exception
5609                 WHEN NO_DATA_FOUND THEN
5610                     IF ( l_error_level >= G_debug_level) THEN
5611                         fnd_message.set_name('CSD','CSD_SERIAL_NUM_MISSING');
5612                         fnd_message.set_token('TXN_ID',RCV.transaction_id);
5613                         FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
5614                     ELSE
5615                         fnd_message.set_name('CSD','CSD_SERIAL_NUM_MISSING');
5616                         fnd_message.set_token('TXN_ID',RCV.transaction_id);
5617                         fnd_msg_pub.add;
5618                     END IF;
5619                     RAISE PROCESS_ERROR;
5620               END;
5621 
5622               -- Get the instance id from
5623                     -- installbase tables
5624                     IF NVL(RCV.ib_flag,'N') = 'Y' THEN
5625                       BEGIN
5626                  Select instance_id
5627                           into   l_instance_id
5628                           from csi_item_instances
5629                           where inventory_item_id = RCV.item_id
5630                            and  serial_number     = l_serial_num;
5631                       Exception
5632                             When NO_DATA_FOUND THEN
5633                       IF ( l_error_level >= G_debug_level) THEN
5634                            fnd_message.set_name('CSD','CSD_ITEM_INSTANCE_MISSING');
5635                            fnd_message.set_token('SERIAL_NUM',l_serial_num);
5636                            FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
5637                       ELSE
5638                            fnd_message.set_name('CSD','CSD_ITEM_INSTANCE_MISSING');
5639                            fnd_message.set_token('SERIAL_NUM',l_serial_num);
5640                            fnd_msg_pub.add;
5641                       END If;
5642                             When Others THEN
5643                       IF ( l_error_level >= G_debug_level) THEN
5644                            fnd_message.set_name('CSD','CSD_INV_SERIAL_NUM');
5645                            fnd_message.set_token('SERIAL_NUM',l_serial_num);
5646                            FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
5647                       ELSE
5648                            fnd_message.set_name('CSD','CSD_INV_SERIAL_NUM');
5649                            fnd_message.set_token('SERIAL_NUM',l_serial_num);
5650                            fnd_msg_pub.add;
5651                       END If;
5652                End;
5653                        ELSE
5654                 l_instance_id := NULL;
5655                     END IF;
5656             ELSE
5657               -- Non-Serialized Case but lot controlled
5658               l_serial_num := NULL;
5659               l_line_qty   := RO.ordered_quantity;
5660               l_instance_id := NULL;
5661 
5662               --lot_control_code = 1 No control
5663               --lot_control_code = 2 Full control
5664               IF RCV.lot_control_code = 2 THEN
5665                   BEGIN
5666                      Select lot_num
5667                      into   l_lot_num
5668                      from rcv_lot_transactions
5669                      where source_transaction_id = RCV.transaction_id;
5670                   EXCEPTION
5671                      WHEN NO_DATA_FOUND THEN
5672                           IF ( l_error_level >= G_debug_level) THEN
5673                               fnd_message.set_name('CSD','CSD_INV_TXN_ID');
5674                               fnd_message.set_token('TXN_ID',RCV.transaction_id);
5675                               FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
5676                           ELSE
5677                               fnd_message.set_name('CSD','CSD_INV_TXN_ID');
5678                               fnd_message.set_token('TXN_ID',RCV.transaction_id);
5679                               fnd_msg_pub.add;
5680                           END IF;
5681                           RAISE FND_API.G_EXC_ERROR;
5682                  END;
5683               ELSE
5684                   -- Non-Serialized Case but not lot controlled
5685                      l_lot_num := NULL;
5686               END IF;
5687           END IF;
5688 
5689           IF l_pt_del_qty >= l_line_qty  THEN
5690                 Debug(' EXiting the RCV Loop',l_mod_name,1);
5691                 EXIT;
5692           ELSE
5693                 l_rcvd_qty :=  l_line_qty  - l_pt_del_qty;
5694                 IF RCV.rcvd_qty <  l_rcvd_qty THEN
5695                     l_rcvd_qty := RCV.rcvd_qty;
5696                 END IF;
5697           END IF;
5698 
5699           IF RCV.serial_number_control_code in (2,5,6) THEN
5700                    l_rcvd_qty := 1;
5701                 ELSE
5702                    l_rcvd_qty := l_rcvd_qty - l_total_qty;
5703           END IF;
5704 
5705           -- Debug messages
5706           Debug('l_rcvd_qty'||l_rcvd_qty,l_mod_name,1);
5707 
5708           -- Update repair order with the rcvd serial number
5709           -- and the rcvd qty
5710                 /*Bug#5564180/FP#5845995 Move out line should not update the received quantity of
5711                  RO. Move out line should not change the serial and instance number of RO
5712                  so commenting the below query
5713                 */
5714         /*
5715           UPDATE CSD_REPAIRS
5716           SET SERIAL_NUMBER = l_serial_num,
5717               quantity_rcvd = nvl(quantity_rcvd,0) + l_rcvd_qty,
5718                     customer_product_id = l_instance_id,
5719                     object_version_number = object_version_number+1,
5720               last_update_date = sysdate,
5721               last_updated_by  = fnd_global.user_id,
5722               last_update_login = fnd_global.login_id
5723           WHERE repair_line_id = RO.repair_line_id;
5724           IF SQL%NOTFOUND THEN
5725                IF ( l_error_level >= G_debug_level) THEN
5726                     fnd_message.set_name('CSD','CSD_RO_UPD_FAILED');
5727                     fnd_message.set_token('REPAIR_LINE_ID',RO.repair_line_id);
5728                     FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
5729                ELSE
5730                     fnd_message.set_name('CSD','CSD_RO_UPD_FAILED');
5731                     fnd_message.set_token('REPAIR_LINE_ID',RO.repair_line_id);
5732                     fnd_msg_pub.add;
5733                END IF;
5734                RAISE PROCESS_ERROR;
5735           END IF;
5736           */
5737 
5738           -- Update product txn with the rcvd serial number,lot number
5739           -- locator id, sub inv, status and the rcvd qty
5740           UPDATE CSD_PRODUCT_TRANSACTIONS
5741           SET SOURCE_SERIAL_NUMBER = l_serial_num,
5742                     source_instance_id   = l_instance_id,
5743               LOT_NUMBER_RCVD      = l_lot_num,
5744               QUANTITY_RECEIVED = NVL(QUANTITY_RECEIVED,0) + l_rcvd_qty,
5745               SUB_INVENTORY_RCVD= RCV.subinventory,
5746               LOCATOR_ID        = RCV.locator_id,
5747                     object_version_number = object_version_number+1,
5748               last_update_date = sysdate,
5749               last_updated_by  = fnd_global.user_id,
5750               last_update_login = fnd_global.login_id
5751           WHERE product_transaction_id = RO.product_transaction_id;
5752           IF SQL%NOTFOUND THEN
5753               IF ( l_error_level >= G_debug_level) THEN
5754                     fnd_message.set_name('CSD','CSD_PROD_TXN_UPD_FAILED');
5755                     fnd_message.set_token('PROD_TXN_ID',RO.product_transaction_id);
5756                     FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
5757               ELSE
5758                     fnd_message.set_name('CSD','CSD_PROD_TXN_UPD_FAILED');
5759                     fnd_message.set_token('PROD_TXN_ID',RO.product_transaction_id);
5760                     fnd_msg_pub.add;
5761               END IF;
5762               RAISE PROCESS_ERROR;
5763           END IF;
5764 
5765           IF RCV.serial_number_control_code in (2,5,6) THEN
5766                    UPDATE CSD_PRODUCT_TRANSACTIONS
5767              SET prod_txn_status       = 'RECEIVED',
5768                  object_version_number = object_version_number+1,
5769                  last_update_date = sysdate,
5770                  last_updated_by  = fnd_global.user_id,
5771                  last_update_login = fnd_global.login_id
5772              WHERE product_transaction_id = RO.product_transaction_id;
5773           ELSE
5774                    UPDATE CSD_PRODUCT_TRANSACTIONS
5775              SET prod_txn_status       = 'RECEIVED',
5776                  object_version_number = object_version_number+1,
5777                  last_update_date = sysdate,
5778                  last_updated_by  = fnd_global.user_id,
5779                  last_update_login = fnd_global.login_id
5780              WHERE quantity_received = RO.ordered_quantity
5781              and   product_transaction_id = RO.product_transaction_id;
5782                 END IF;
5783 
5784           -- Initialize the activity rec
5785           l_activity_rec := INIT_ACTIVITY_REC ;
5786 
5787           -- Assign the values for activity record
5788           l_activity_rec.REPAIR_HISTORY_ID := l_rep_hist_id;
5789           l_activity_rec.REPAIR_LINE_ID := RO.repair_line_id;
5790           l_activity_rec.EVENT_CODE     := 'RRI';
5791           l_activity_rec.ACTION_CODE    := 0;
5792           l_activity_rec.EVENT_DATE     := RCV.received_date;
5793           l_activity_rec.QUANTITY       := l_rcvd_qty;
5794           l_activity_rec.PARAMN1        := RCV.transaction_id;
5795           l_activity_rec.PARAMN2        := RO.product_transaction_id;
5796           l_activity_rec.PARAMN3        := RCV.organization_id;
5797           l_activity_rec.PARAMN6        :=  RO.req_header_id;
5798           l_activity_rec.PARAMC1        :=  RO.order_number;
5799           l_activity_rec.PARAMC2        :=  RO.requisition_number;
5800           l_activity_rec.PARAMC3        :=  RCV.org_name;
5801           l_activity_rec.PARAMC4        :=  RCV.subinventory;
5802           l_activity_rec.OBJECT_VERSION_NUMBER := null;
5803 
5804           -- Debug Messages
5805           Debug('Calling LOG_ACTIVITY',l_mod_name,2);
5806 
5807           -- Calling LOG_ACTIVITY for logging activity
5808           LOG_ACTIVITY
5809                 ( p_api_version     => p_api_version,
5810                   p_commit          => p_commit,
5811                   p_init_msg_list   => p_init_msg_list,
5812                   p_validation_level => p_validation_level,
5813                   x_return_status   => x_return_status,
5814                   x_msg_count       => x_msg_count,
5815                   x_msg_data        => x_msg_data,
5816                   p_activity_rec    => l_activity_rec );
5817 
5818             -- Debug Messages
5819             Debug('Return Status from LOG_ACTIVITY:'||x_return_status,l_mod_name,2);
5820             IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
5821                 Debug('LOG_ACTIVITY api failed ',l_mod_name,1);
5822                 RAISE FND_API.G_EXC_ERROR;
5823             END IF;
5824 
5825            IF  l_pt_del_qty+ l_rcvd_qty >= l_line_qty   THEN
5826                 Debug(' exiting the RCV loop',l_mod_name,1);
5827                 EXIT;
5828            END IF;
5829           END IF;
5830         Exception
5831            WHEN PROCESS_ERROR THEN
5832                  Debug(' In Process error exception',l_mod_name,4);
5833                  RAISE PROCESS_ERROR;
5834             WHEN SKIP_RECORD THEN
5835                  NULL;
5836         END;
5837        END LOOP;
5838      Exception
5839         WHEN PROCESS_ERROR THEN
5840             ROLLBACK TO RCV_LINES;
5841             Debug(' In Process error exception, exiting the loop',l_mod_name,1);
5842             -- In case of error, exit the loop. Commit the processed records
5843             -- and rollback the error record
5844             --RAISE FND_API.G_EXC_ERROR;
5845             x_return_status := FND_API.G_RET_STS_ERROR ;
5846             EXIT;
5847      END;
5848    END LOOP;
5849 
5850     --Added by Vijay 11/4/04
5851    IF(GET_RO_PROD_TXN_LINES_All%ISOPEN) THEN
5852        CLOSE GET_RO_PROD_TXN_LINES_All;
5853    END IF;
5854    IF(GET_RO_PROD_TXN_LINES%ISOPEN) THEN
5855        CLOSE GET_RO_PROD_TXN_LINES;
5856    END IF;
5857 
5858 
5859    -- Standard check of p_commit.
5860    IF FND_API.To_Boolean( p_commit ) THEN
5861        COMMIT WORK;
5862    END IF;
5863 
5864    -- Standard call to get message count and IF count is  get message info.
5865    FND_MSG_PUB.Count_And_Get
5866            (p_count  =>  x_msg_count,
5867             p_data   =>  x_msg_data);
5868  EXCEPTION
5869     WHEN FND_API.G_EXC_ERROR THEN
5870           Debug('In FND_API.G_EXC_ERROR exception',l_mod_name,4);
5871           ROLLBACK TO IO_RCV_UPDATE_MOVE_OUT;
5872           x_return_status := FND_API.G_RET_STS_ERROR ;
5873           FND_MSG_PUB.Count_And_Get
5874                 (p_count  =>  x_msg_count,
5875                  p_data   =>  x_msg_data  );
5876     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5877           Debug('In FND_API.G_EXC_UNEXPECTED_ERROR exception',l_mod_name,4);
5878           ROLLBACK TO IO_RCV_UPDATE_MOVE_OUT;
5879           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
5880           FND_MSG_PUB.Count_And_Get
5881                 ( p_count  =>  x_msg_count,
5882                   p_data   =>  x_msg_data );
5883     WHEN OTHERS THEN
5884           Debug('In OTHERS exception',l_mod_name,4);
5885           ROLLBACK TO IO_RCV_UPDATE_MOVE_OUT;
5886           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
5887           IF  FND_MSG_PUB.Check_Msg_Level
5888                   (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
5889           THEN
5890               FND_MSG_PUB.Add_Exc_Msg
5891                   (G_PKG_NAME ,
5892                    l_api_name  );
5893           END IF;
5894           FND_MSG_PUB.Count_And_Get
5895                   (p_count  =>  x_msg_count,
5896                    p_data   =>  x_msg_data );
5897 END IO_RCV_UPDATE_MOVE_OUT;
5898 
5899 /*-------------------------------------------------------------------------------------*/
5900 /* Procedure name: SHIP_UPDATE                                                         */
5901 /* Description   : Procedure called from the UI to update the depot tables             */
5902 /*                 for the shipment against regular sales order/Internal Sales Order   */
5903 /*                 It calls SO_SHIP_UPDATE and IO_SHIP_UPDATE  to process sales order  */
5904 /*                 and internal sales order                                            */
5905 /* Called from   : Called from Depot Repair UI                                         */
5906 /* STANDARD PARAMETERS                                                                 */
5907 /*  In Parameters :                                                                    */
5908 /*    p_api_version       NUMBER    Required  Api Version number                       */
5909 /*    p_init_msg_list     VARCHAR2  Optional  To Initialize message stack              */
5910 /*    p_commit            VARCHAR2  Optional  Commits in API                           */
5911 /*    p_validation_level  NUMBER    Optional  validation level                         */
5912 /*                                                                                     */
5913 /*  Output Parameters:                                                                 */
5914 /*   x_return_status     VARCHAR2      Return status of the API                        */
5915 /*   x_msg_count         NUMBER        Number of messages in stack                     */
5916 /*   x_msg_data          VARCHAR2      Error Message from message stack                */
5917 /*                                                                                     */
5918 /* NON-STANDARD PARAMETERS                                                             */
5919 /*   In Parameters                                                                     */
5920 /*    p_order_header_id  NUMBER   Optional   Interal sales order Id                    */
5921 /*    p_internal_order_flag VARCHAR2 Required  Order Type; Possible values -'Y','N'    */
5922 /*    p_repair_line_id   NUMBER   Optional   Repair Order Line Id                      */
5923 /* Output Parm :                                                                       */
5924 /* Change Hist :                                                                       */
5925 /*   09/20/03  vlakaman  Initial Creation.                                             */
5926 /*-------------------------------------------------------------------------------------*/
5927 
5928 Procedure  SHIP_UPDATE
5929         ( p_api_version          IN   NUMBER,
5930           p_commit               IN   VARCHAR2,
5931           p_init_msg_list        IN   VARCHAR2,
5932           p_validation_level     IN   NUMBER,
5933           x_return_status        OUT  NOCOPY  VARCHAR2,
5934           x_msg_count            OUT  NOCOPY  NUMBER,
5935           x_msg_data             OUT  NOCOPY  VARCHAR2,
5936           p_internal_order_flag  IN   VARCHAR2,
5937           p_order_header_id      IN   NUMBER,
5938           p_repair_line_id       IN   NUMBER,
5939           p_past_num_of_days     IN   NUMBER DEFAULT NULL)   ----bug#6753684, 6742512
5940 IS
5941 
5942   -- Standard Variables
5943   l_api_name             CONSTANT VARCHAR2(30)   := 'SHIPMENT_UPDATE';
5944   l_api_version          CONSTANT NUMBER         := 1.0;
5945 
5946   -- Variables in FND log
5947   l_error_level  number   := FND_LOG.LEVEL_ERROR;
5948   l_mod_name     varchar2(2000) := 'csd.plsql.csd_update_programs_pvt.ship_update';
5949 
5950 BEGIN
5951    -- Standard Start of API savepoint
5952    SAVEPOINT  SHIP_UPDATE;
5953 
5954    -- Initialize API return status to success
5955    x_return_status := FND_API.G_RET_STS_SUCCESS;
5956 
5957    -- Log the api name in the log file
5958    Debug('At the Beginning of shipment Update ',l_mod_name,1);
5959    Debug('p_internal_order_flag ='||p_internal_order_flag,l_mod_name,1);
5960    Debug('p_order_header_id     ='||p_order_header_id,l_mod_name,1);
5961    Debug('p_repair_line_id      ='||p_repair_line_id,l_mod_name,1);
5962 
5963    -- Standard call to check for call compatibility.
5964    IF NOT FND_API.Compatible_API_Call (l_api_version,
5965                                        p_api_version,
5966                                        l_api_name   ,
5967                                        G_PKG_NAME   )
5968        THEN
5969           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5970    END IF;
5971 
5972    -- Initialize message list if p_init_msg_list is set to TRUE.
5973    IF FND_API.to_Boolean( p_init_msg_list ) THEN
5974          FND_MSG_PUB.initialize;
5975    END IF;
5976 
5977    -- Api body starts
5978    IF p_internal_order_flag = 'Y' then
5979        Debug('Calling IO_SHIP_UPDATE ',l_mod_name,2);
5980        -- Call the api for processing of shipment against internal order
5981        IO_SHIP_UPDATE
5982         ( p_api_version          => p_api_version,
5983           p_commit               => p_commit     ,
5984           p_init_msg_list        => p_init_msg_list,
5985           p_validation_level     => p_validation_level,
5986           x_return_status        => x_return_status,
5987           x_msg_count            => x_msg_count,
5988           x_msg_data             => x_msg_data ,
5989           p_order_header_id      => p_order_header_id);
5990 
5991        Debug('Return status from IO_SHIP_UPDATE '||x_return_status,l_mod_name,2);
5992        IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
5993           Debug('IO_SHIP_UPDATE failed ',l_mod_name,4);
5994           RAISE FND_API.G_EXC_ERROR;
5995        END IF;
5996 
5997       /*Fixed for bug#5564180/FP#5845995
5998         Call to API IO_RCV_UPDATE_MOVE_OUT is added so that
5999         move out line can be updated for receiving in destination
6000         organization.
6001       */
6002        Debug('Calling IO_RCV_UPDATE_MOVE_OUT API',l_mod_name,2);
6003        -- Call the api for processing the receipt against
6004        -- internal requisition
6005        IO_RCV_UPDATE_MOVE_OUT
6006         ( p_api_version          => p_api_version,
6007           p_commit               => p_commit     ,
6008           p_init_msg_list        => p_init_msg_list,
6009           p_validation_level     => p_validation_level,
6010           x_return_status        => x_return_status,
6011           x_msg_count            => x_msg_count,
6012           x_msg_data             => x_msg_data ,
6013           p_order_header_id      => p_order_header_id );
6014 
6015        -- Debug messages
6016        Debug('Return Status from IO_RCV_UPDATE_MOVE_OUT :'||x_return_status,l_mod_name,2);
6017        IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
6018           Debug('IO_RCV_UPDATE_MOVE_OUT failed',l_mod_name,4);
6019           RAISE FND_API.G_EXC_ERROR;
6020        END IF;
6021 
6022 
6023    Else
6024         Debug('Calling SO_SHIP_UPDATE ',l_mod_name,2);
6025         -- Call the api for processing sales order
6026         SO_SHIP_UPDATE
6027         ( p_api_version          => p_api_version,
6028           p_commit               => p_commit     ,
6029           p_init_msg_list        => p_init_msg_list,
6030           p_validation_level     => p_validation_level,
6031           x_return_status        => x_return_status,
6032           x_msg_count            => x_msg_count,
6033           x_msg_data             => x_msg_data ,
6034           p_repair_line_id       => p_repair_line_id,
6035           p_past_num_of_days     => p_past_num_of_days);
6036 
6037        Debug('Return status from SO_SHIP_UPDATE '||x_return_status,l_mod_name,2);
6038        IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
6039           Debug('SO_SHIP_UPDATE failed ',l_mod_name,4);
6040           RAISE FND_API.G_EXC_ERROR;
6041        END IF;
6042   End If;
6043 
6044   -- Standard check of p_commit.
6045   IF FND_API.To_Boolean( p_commit ) THEN
6046          COMMIT WORK;
6047   END IF;
6048 
6049   -- Standard call to get message count and IF count is  get message info.
6050   FND_MSG_PUB.Count_And_Get
6051            (p_count  =>  x_msg_count,
6052             p_data   =>  x_msg_data );
6053  EXCEPTION
6054     WHEN FND_API.G_EXC_ERROR THEN
6055           Debug('In FND_API.G_EXC_ERROR Exception ',l_mod_name,4);
6056           -- As we commit the processed records in the inner APIs
6057 		-- so we rollback only if the p_commit= 'F'
6058 		IF NOT(FND_API.To_Boolean( p_commit )) THEN
6059              ROLLBACK TO SHIP_UPDATE;
6060           END IF;
6061           x_return_status := FND_API.G_RET_STS_ERROR ;
6062           FND_MSG_PUB.Count_And_Get
6063                 (p_count  =>  x_msg_count,
6064                  p_data   =>  x_msg_data  );
6065     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
6066           Debug('In FND_API.G_EXC_UNEXPECTED_ERROR Exception',l_mod_name,4);
6067           IF ( l_error_level >= G_debug_level)  THEN
6068               fnd_message.set_name('CSD','CSD_SQL_ERROR');
6069               fnd_message.set_token('SQLERRM',SQLERRM);
6070               fnd_message.set_token('SQLCODE',SQLCODE);
6071               FND_LOG.MESSAGE(l_error_level,l_mod_name,FALSE);
6072           END If;
6073           -- As we commit the processed records in the inner APIs
6074 		-- so we rollback only if the p_commit= 'F'
6075 		IF NOT(FND_API.To_Boolean( p_commit )) THEN
6076              ROLLBACK TO SHIP_UPDATE;
6077           END IF;
6078           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
6079           FND_MSG_PUB.Count_And_Get
6080                 ( p_count  =>  x_msg_count,
6081                   p_data   =>  x_msg_data );
6082     WHEN OTHERS THEN
6083           Debug('In OTHERS Exception',l_mod_name,4);
6084           IF ( l_error_level >= G_debug_level)  THEN
6085               fnd_message.set_name('CSD','CSD_SQL_ERROR');
6086               fnd_message.set_token('SQLERRM',SQLERRM);
6087               fnd_message.set_token('SQLCODE',SQLCODE);
6088               FND_LOG.MESSAGE(l_error_level,l_mod_name,FALSE);
6089           END If;
6090           -- As we commit the processed records in the inner APIs
6091 		-- so we rollback only if the p_commit= 'F'
6092 		IF NOT(FND_API.To_Boolean( p_commit )) THEN
6093              ROLLBACK TO SHIP_UPDATE;
6094           END IF;
6095 		x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
6096           IF  FND_MSG_PUB.Check_Msg_Level
6097                   (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
6098           THEN
6099               FND_MSG_PUB.Add_Exc_Msg
6100                   (G_PKG_NAME ,
6101                    l_api_name  );
6102           END IF;
6103           FND_MSG_PUB.Count_And_Get
6104                   (p_count  =>  x_msg_count,
6105                    p_data   =>  x_msg_data );
6106 END SHIP_UPDATE;
6107 
6108 /*-------------------------------------------------------------------------------------*/
6109 /* Procedure name: SO_SHIP_UPDATE                                                      */
6110 /* Description   : Procedure called from the Update api to update the depot tables     */
6111 /*                 for the shipment against sales order                                */
6112 /*                 It also logs activities for the deliver txn lines                   */
6113 /* Called from   : Called from SHIP_UPDATE API                                         */
6114 /* STANDARD PARAMETERS                                                                 */
6115 /*  In Parameters :                                                                    */
6116 /*    p_api_version       NUMBER    Required  Api Version number                       */
6117 /*    p_init_msg_list     VARCHAR2  Optional  To Initialize message stack              */
6118 /*    p_commit            VARCHAR2  Optional  Commits in API                           */
6119 /*    p_validation_level  NUMBER    Optional  validation level                         */
6120 /*                                                                                     */
6121 /*  Output Parameters:                                                                 */
6122 /*   x_return_status     VARCHAR2      Return status of the API                        */
6123 /*   x_msg_count         NUMBER        Number of messages in stack                     */
6124 /*   x_msg_data          VARCHAR2      Error Message from message stack                */
6125 /*                                                                                     */
6126 /* NON-STANDARD PARAMETERS                                                             */
6127 /*   In Parameters                                                                     */
6128 /*    p_repair_line_id   NUMBER   Optional   Repair Order Line Id                      */
6129 /* Output Parm :                                                                       */
6130 /* Change Hist :                                                                       */
6131 /*   09/20/03  vlakaman  Initial Creation.                                             */
6132 /*-------------------------------------------------------------------------------------*/
6133 
6134 
6135 PROCEDURE  SO_SHIP_UPDATE
6136         ( p_api_version          IN   NUMBER,
6137           p_commit               IN   VARCHAR2,
6138           p_init_msg_list        IN   VARCHAR2,
6139           p_validation_level     IN   NUMBER,
6140           x_return_status        OUT  NOCOPY  VARCHAR2,
6141           x_msg_count            OUT  NOCOPY  NUMBER,
6142           x_msg_data             OUT  NOCOPY  VARCHAR2,
6143           p_repair_line_id       IN   NUMBER,
6144           p_past_num_of_days     IN   NUMBER  DEFAULT NULL)  ----bug#6753684, 6742512
6145 IS
6146 
6147 
6148 --Cursor split into to by Vijay 11/4/04 one without repair line and
6149 -- one with repair line.
6150   -- Cursor to get all the shipment lines
6151   Cursor SHIPMENT_LINES_ALL is
6152   select
6153     ---changed to fix 3801614 , added dsn.serial_number if dd.serial_number is null
6154     nvl(dd.serial_number, dsn.fm_serial_number) shipped_serial_num,
6155     dd.lot_number lot_number,
6156     dd.revision revision,
6157     dd.subinventory subinv,
6158     dd.requested_quantity,
6159     dd.shipped_quantity,
6160     dd.delivery_detail_id,
6161     dd.requested_quantity_uom shipped_uom,
6162     dd.inventory_item_id ,
6163     dd.organization_id,
6164     dd.source_header_number order_number,
6165     dd.source_header_id sales_order_header,
6166     dd.locator_id,
6167     oel.line_number order_line_number,
6168     oel.actual_shipment_date date_shipped,
6169     oel.line_id,                            --Bug#6779806
6170     cra.repair_number,
6171     cra.repair_line_id,
6172     cra.unit_of_measure ro_uom,
6173     cra.inventory_item_id ro_item_id,
6174     ced.quantity_required estimate_quantity,
6175     cpt.source_serial_number prod_txn_serial_num,
6176     cpt.source_instance_id,
6177     cpt.product_transaction_id,
6178     cpt.action_code,
6179     wnd.name delivery_name,
6180     hao.name org_name
6181   from
6182     csd_product_transactions cpt,
6183     cs_estimate_details ced,
6184     csd_repairs cra,
6185     wsh_delivery_details dd ,
6186     wsh_serial_numbers dsn,--Added to fix 3801614
6187 --Changed to view from table, bug:  4341784
6188     wsh_delivery_assignments_v wda,
6189     wsh_new_deliveries wnd,
6190     oe_order_lines_all oel,
6191     hr_all_organization_units hao
6192  Where cpt.action_type in ('SHIP', 'SHIP_THIRD_PTY') -- Walk-in-issue will be changed to ship
6193   AND  cpt.estimate_detail_id = ced.estimate_detail_id
6194   AND  dd.delivery_detail_id  = wda.delivery_detail_id
6195   AND  dd.delivery_detail_id  = dsn.delivery_detail_id(+) --Added to fix 3801614
6196   AND  dd.organization_id     = hao.organization_id
6197   AND  wda.delivery_id        = wnd.delivery_id
6198   AND  cpt.repair_line_id     = cra.repair_line_id
6199   AND  ced.order_header_id    = oel.header_id
6200   AND  ced.order_line_id      = oel.line_id          ----bug#6753684, 6742512
6201   AND  dd.source_header_id    = ced.order_header_id  ----bug#6753684, 6742512
6202   AND  dd.source_line_id      = ced.order_line_id    ----bug#6753684, 6742512
6203   AND  dd.source_header_id    = oel.header_id     ----bug#6753684, 6742512
6204   AND  dd.source_line_id      = oel.line_id
6205   AND  dd.source_code = 'OE'     -- 4423818
6206   AND  dd.released_status     in ('C','I')
6207   AND  ced.source_code        = 'DR'
6208   AND  not exists
6209         (select 'NOT EXIST'
6210           from csd_repair_history crh
6211          where crh.repair_line_id = cpt.repair_line_id
6212           and  crh.paramn1        = dd.delivery_detail_id
6213           and  event_code         = 'PS')
6214   AND  oel.line_id in ( Select line_id
6215                         from oe_order_lines_all oel1
6216          	            start with oel1.line_id = ced.order_line_id
6217 	    	            connect by prior oel1.line_id = oel1.split_from_line_id
6218 		                and oel1.shipped_quantity is not null
6219 		                and oel1.header_id = oel.header_id);
6220 
6221 /* Fixed bug#6753684, 6742512
6222 	New cursor added for batch processing.This cursor takes two parameter
6223 	p_from_date and p_to_date. These dates are compared with the creation date
6224 	of repair order. Only repair order created during this period are considered
6225 	for Depot Shipment Update to improve performance.
6226 */
6227   Cursor SHIPMENT_LINES_BY_DATE( p_from_Date Date, p_to_Date Date ) is
6228   select
6229     ---changed to fix 3801614 , added dsn.serial_number if dd.serial_number is null
6230     nvl(dd.serial_number, dsn.fm_serial_number) shipped_serial_num,
6231     dd.lot_number lot_number,
6232     dd.revision revision,
6233     dd.subinventory subinv,
6234     dd.requested_quantity,
6235     dd.shipped_quantity,
6236     dd.delivery_detail_id,
6237     dd.requested_quantity_uom shipped_uom,
6238     dd.inventory_item_id ,
6239     dd.organization_id,
6240     dd.source_header_number order_number,
6241     dd.source_header_id sales_order_header,
6242     dd.locator_id,
6243     oel.line_number order_line_number,
6244     oel.actual_shipment_date date_shipped,
6245     oel.line_id,                            --Bug#6779806
6246     cra.repair_number,
6247     cra.repair_line_id,
6248     cra.unit_of_measure ro_uom,
6249     cra.inventory_item_id ro_item_id,
6250     ced.quantity_required estimate_quantity,
6251     cpt.source_serial_number prod_txn_serial_num,
6252     cpt.source_instance_id,
6253     cpt.product_transaction_id,
6254     cpt.action_code,
6255     wnd.name delivery_name,
6256     hao.name org_name
6257   from
6258     csd_product_transactions cpt,
6259     cs_estimate_details ced,
6260     csd_repairs cra,
6261     wsh_delivery_details dd ,
6262     wsh_serial_numbers dsn,--Added to fix 3801614
6263 --Changed to view from table, bug:  4341784
6264     wsh_delivery_assignments_v wda,
6265     wsh_new_deliveries wnd,
6266     oe_order_lines_all oel,
6267     hr_all_organization_units hao
6268 Where cra.creation_date between p_from_date and p_to_date
6269   AND  cpt.action_type in ('SHIP', 'SHIP_THIRD_PTY') -- Walk-in-issue will be changed to ship
6270   AND  cpt.estimate_detail_id = ced.estimate_detail_id
6271   AND  dd.delivery_detail_id  = wda.delivery_detail_id
6272   AND  dd.delivery_detail_id  = dsn.delivery_detail_id(+) --Added to fix 3801614
6273   AND  dd.organization_id     = hao.organization_id
6274   AND  wda.delivery_id        = wnd.delivery_id
6275   AND  cpt.repair_line_id     = cra.repair_line_id
6276   AND  ced.order_header_id    = oel.header_id
6277   AND  ced.order_line_id      = oel.line_id          ----bug#6753684, 6742512
6278   AND  dd.source_header_id    = ced.order_header_id  ----bug#6753684, 6742512
6279   AND  dd.source_line_id      = ced.order_line_id    ----bug#6753684, 6742512
6280   AND  dd.source_header_id    = oel.header_id     ----bug#6753684, 6742512
6281   AND  dd.source_line_id      = oel.line_id
6282   AND  dd.source_code = 'OE'     -- 4423818
6283   AND  dd.released_status     in ('C','I')
6284   AND  ced.source_code        = 'DR'
6285   AND  not exists
6286         (select 'NOT EXIST'
6287           from csd_repair_history crh
6288          where crh.repair_line_id = cpt.repair_line_id
6289           and  crh.paramn1        = dd.delivery_detail_id
6290           and  event_code         = 'PS')
6291   AND  oel.line_id in ( Select line_id
6292                         from oe_order_lines_all oel1
6293          	            start with oel1.line_id = ced.order_line_id
6294 	    	            connect by prior oel1.line_id = oel1.split_from_line_id
6295 		                and oel1.shipped_quantity is not null
6296 		                and oel1.header_id = oel.header_id);
6297 
6298 
6299   -- recrd type var to get the column values into
6300   I SHIP_LINES_Rec_Type ;
6301   -- Cursor to get all the shipment lines for the
6302   -- specific repair line id
6303   -- Changed by Vijay to remove OR condition on repair line if
6304   -- bug fix 3162163
6305 
6306   -- 12.1 FP bug#7551078, subhat.
6307   -- need to pick up only those ship lines which have source type as OE.
6308   -- There can be multiple delivery lines for the same sales order or
6309   -- the source_line_id in wsh_delivery_details can be same for OKE ship lines and
6310   -- OE ship lines.
6311   Cursor SHIPMENT_LINES ( p_repair_line_id in number) is
6312   select
6313     ---changed to fix 3801614 , added dsn.serial_number if dd.serial_number is null
6314     nvl(dd.serial_number, dsn.fm_serial_number) shipped_serial_num,
6315     dd.lot_number lot_number,
6316     dd.revision revision,
6317     dd.subinventory subinv,
6318     dd.requested_quantity,
6319     dd.shipped_quantity,
6320     dd.delivery_detail_id,
6321     dd.requested_quantity_uom shipped_uom,
6322     dd.inventory_item_id ,
6323     dd.organization_id,
6324     dd.source_header_number order_number,
6325     dd.source_header_id sales_order_header,
6326     dd.locator_id,
6327     oel.line_number order_line_number,
6328     oel.actual_shipment_date date_shipped,
6329     oel.line_id,                            --Bug#6779806
6330     cra.repair_number,
6331     cra.repair_line_id,
6332     cra.unit_of_measure ro_uom,
6333     cra.inventory_item_id ro_item_id,
6334     ced.quantity_required estimate_quantity,
6335     cpt.source_serial_number prod_txn_serial_num,
6336     cpt.source_instance_id,
6337     cpt.product_transaction_id,
6338     cpt.action_code,
6339     wnd.name delivery_name,
6340     hao.name org_name
6341   from
6342     csd_product_transactions cpt,
6343     cs_estimate_details ced,
6344     csd_repairs cra,
6345     wsh_delivery_details dd ,
6346     wsh_serial_numbers dsn,--Added to fix 3801614
6347 --Changed to view from table, bug:  4341784
6348     wsh_delivery_assignments_v wda,
6349     wsh_new_deliveries wnd,
6350     oe_order_lines_all oel,
6351     hr_all_organization_units hao
6352  Where cpt.action_type in ('SHIP', 'SHIP_THIRD_PTY') -- Walk-in-issue will be changed to ship
6353   AND  cpt.estimate_detail_id = ced.estimate_detail_id
6354   AND  dd.delivery_detail_id  = wda.delivery_detail_id
6355   AND  dd.delivery_detail_id  = dsn.delivery_detail_id(+) --Added to fix 3801614
6356   AND  dd.organization_id     = hao.organization_id
6357   AND  wda.delivery_id        = wnd.delivery_id
6358   AND  cpt.repair_line_id     = cra.repair_line_id
6359   AND  ced.order_header_id    = oel.header_id
6360   AND  dd.source_line_id      = oel.line_id
6361   AND  dd.released_status     in ('C','I')
6362   AND  ced.source_code        = 'DR'
6363   AND  dd.source_code         = 'OE' -- 12.1 FP bug#7551078, subhat
6364   AND  not exists
6365         (select 'NOT EXIST'
6366           from csd_repair_history crh
6367          where crh.repair_line_id = cpt.repair_line_id
6368           and  crh.paramn1        = dd.delivery_detail_id
6369           and  event_code         = 'PS')
6370   AND  cpt.repair_line_id = p_repair_line_id
6371   AND  oel.line_id in ( Select line_id
6372                         from oe_order_lines_all oel1
6373          	            start with oel1.line_id = ced.order_line_id
6374 	    	            connect by prior oel1.line_id = oel1.split_from_line_id
6375 		                and oel1.shipped_quantity is not null
6376 		                and oel1.header_id = oel.header_id);
6377   -- There is a concern for performance as I am using connect by clause
6378   -- Need to check with the performance team if there is a workaround
6379   -- Otherwise the logic has to be changed
6380 
6381  --- cursor for Cancelled orders...
6382  CURSOR Cur_Cancelled_repair_lines IS
6383  SELECT cra.REPAIR_LINE_ID
6384  FROM csd_repairs cra,
6385       cs_estimate_details ced,
6386       csd_product_transactions cpt
6387  WHERE  cpt.action_type    in ('SHIP', 'SHIP_THIRD_PTY')
6388        AND   cpt.prod_txn_status    in (  'BOOKED', 'SUBMITTED')
6389        AND   ced.order_header_id is not null
6390        AND   ced.source_code        = 'DR'
6391        AND   ced.estimate_detail_id = cpt.estimate_detail_id
6392        AND   cra.repair_line_id     = cpt.repair_line_id;
6393 
6394  --Bug#6779806
6395  CURSOR cur_get_instance_id(p_order_line_id number, p_inventory_item_id number)
6396  IS
6397  SELECT instance_id
6398  FROM csi_item_instances
6399  WHERE last_oe_order_line_id = p_order_line_id
6400  AND inventory_item_id = p_inventory_item_id;
6401 
6402  --Bug#6779806
6403  l_enable_update_instance	VARCHAR2(1);
6404 
6405 
6406   -- Standard variables
6407   l_api_name          CONSTANT VARCHAR2(30)   := 'SO_SHIP_UPDATE';
6408   l_api_version       CONSTANT NUMBER         := 1.0;
6409 
6410   -- Variables used in the program
6411   l_serialized        BOOLEAN;
6412   l_rep_hist_id       NUMBER;
6413   l_result_ship_qty   NUMBER;
6414   l_total_records     NUMBER;
6415   l_dummy             varchar2(30);
6416   l_commit_size       NUMBER := 500;
6417   l_instance_id       csi_item_instances.instance_id%type;
6418   l_ib_flag           mtl_system_items.comms_nl_trackable_flag%type;
6419   l_srl_ctl_code      mtl_system_items.serial_number_control_code%type;
6420 
6421   -- activity record
6422   l_activity_rec      activity_rec_type;
6423 
6424 --bug#6753684, 6742512
6425   l_From_Date  Date;
6426   l_TO_Date    Date;
6427 
6428   -- Variables used for FND Log
6429   l_error_level  number   := FND_LOG.LEVEL_ERROR;
6430   l_mod_name     varchar2(2000) := 'csd.plsql.csd_update_programs_pvt.so_ship_update';
6431 
6432  BEGIN
6433    -- Standard Start of API savepoint
6434    SAVEPOINT  SO_SHIP_UPDATE;
6435 
6436    -- Initialize API return status to success
6437    x_return_status := FND_API.G_RET_STS_SUCCESS;
6438 
6439    -- Debug messages
6440    Debug('At the Beginning of ship Update ',l_mod_name,1);
6441    Debug('Repair Line Id ='||p_repair_line_id,l_mod_name,1);
6442 
6443    -- Standard call to check for call compatibility.
6444    IF NOT FND_API.Compatible_API_Call (l_api_version,
6445                                        p_api_version,
6446                                        l_api_name   ,
6447                                        G_PKG_NAME   )
6448        THEN
6449           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6450    END IF;
6451 
6452    -- Initialize message list if p_init_msg_list is set to TRUE.
6453    IF FND_API.to_Boolean( p_init_msg_list ) THEN
6454          FND_MSG_PUB.initialize;
6455    END IF;
6456 
6457    -- Api body starts
6458 
6459    -- Validate the repair line id
6460    -- bugfix 4423818 : We need to validate only if p_repair_line_id is NOT NULL
6461    IF(p_repair_line_id is NOT NULL ) THEN
6462      IF NOT(csd_process_util.validate_rep_line_id(p_repair_line_id)) THEN
6463         RAISE FND_API.G_EXC_ERROR;
6464      END IF;
6465    END IF;
6466 
6467    --Bug#6779806
6468    --get the profile value:
6469    --CSD: Update Instance Number on Shipped Lines for Non-Serialized Installed Base Item
6470    l_enable_update_instance := nvl(FND_PROFILE.VALUE('CSD_UPDATE_INSTANCE_ID_FOR_NON_S_IB'), 'N');
6471 
6472    -- Keep count of number of records
6473    l_total_records := 0;
6474 
6475    -- Select all the delivery lines that are shipped and does not have
6476    -- activity logged against the repair order
6477    -- 1. Serialized
6478    --  BEFORE SHIPMENT
6479    --  Repair Order
6480    --    RO NUM  RO Type  Qty    SN   shipped Qty
6481    --      R1      RR      1     SN1
6482    --  Product Txn
6483    --   Prod Txn   RO Num   Est Line  Est Qty   SN   Subinv   shipped Qty
6484    --     P1        R1         C1       1       SN1
6485    --
6486    -- AFTER SHIPMENT and running the update program
6487    --  Repair Order
6488    --    RO NUM  RO Type  Qty   SN   shipped Qty
6489    --      R1      RR      1    SN9       1
6490    --
6491    --  Delivery Lines
6492    --   Del Line  Ord Line  Qty  SN    Subinv
6493    --     D1        L1       1   SN9    FGI
6494    --
6495    --  Product Txn
6496    --   Prod Txn   RO Num   Est Line  Est Qty   SN     Subinv  Shipped Qty
6497    --     P1        R1         C1       1       SN9    FGI        1
6498    --
6499    -- 2. Non-Serialized
6500    --  BEFORE SHIPMENT
6501    --  Repair Order
6502    --    RO NUM  RO Type  Qty    SN   shipped Qty
6503    --      R1      RR      3
6504    --  Product Txn
6505    --   Prod Txn   RO Num   Est Line  Est Qty   SN   Subinv    shipped Qty
6506    --     P1        R1         C1       3
6507    -- AFTER SHIPMENT and running the update program
6508    --  Repair Order
6509    --    RO NUM  RO Type  Qty   SN  shipped Qty
6510    --      R1      RR      3            3
6511    --
6512    --  Delivery Lines
6513    --   Del Line  Ord Line  Qty  SN    Subinv
6514    --     D1        L1       1          FGI
6515    --     D2        L2       2          FGI
6516    --  Product Txn
6517    --   Prod Txn   RO Num   Est Line  Est Qty   SN     Subinv  Shipped Qty
6518    --     P1        R1         C1       3               FGI        3
6519    --
6520    /* For loop changed to loop to open different
6521    cursors based on p_repair_line_id value.
6522    Bug fix 3162163, Vijay  11/4/04
6523    */
6524    --FOR I IN SHIPMENT_LINES (p_repair_line_id)
6525    IF(p_repair_line_Id is null) THEN
6526      If (p_past_num_of_days Is Null) Then
6527         OPEN SHIPMENT_LINES_ALL;
6528      Else
6529         l_From_Date := sysdate - (p_past_num_of_days +1 ) ;
6530         l_TO_Date   := Sysdate + 1;
6531         OPEN SHIPMENT_LINES_BY_DATE(l_From_Date , l_To_Date);   ----bug#6753684, 6742512
6532      End If;
6533    ELSE
6534      OPEN SHIPMENT_LINES(p_repair_line_id);
6535    END IF;
6536 
6537    LOOP
6538     BEGIN
6539 
6540       IF(p_repair_line_Id is null) THEN
6541         If (p_past_num_of_days Is Null) Then
6542             FETCH  SHIPMENT_LINES_ALL INTO I;
6543             EXIT WHEN SHIPMENT_LINES_ALL%NOTFOUND;
6544         else
6545             FETCH  SHIPMENT_LINES_BY_DATE INTO I;           ----bug#6753684, 6742512
6546             EXIT WHEN SHIPMENT_LINES_BY_DATE%NOTFOUND;
6547         end if;
6548       ELSE
6549         FETCH SHIPMENT_LINES INTO I;
6550         EXIT WHEN SHIPMENT_LINES%NOTFOUND;
6551       END IF;
6552 
6553 
6554      -- savepoint
6555      SAVEPOINT  SHIPMENT_LINES;
6556 
6557 -- bug#7285024
6558 -- There could be data discrepancy between OM status and Shipping status in which
6559 -- OM thinks the line is shipped but Shipping has not shipped yet.
6560 -- Only process the prod txn line if shipped date from shiping tables is not null.
6561 -- cursor queries have not been changed to incorporate this check due to performance
6562 -- issues with checking not null.
6563 -- Date shipped is checked instead of Shipping status since we are not sure exactly
6564 -- which statuses map to a shipped line, but all shipped items must have a ship date.
6565 -- Note: the date_shipped is required column in the depot history table.
6566 	 If (I.date_shipped is not null) then
6567 
6568 		 Debug('Order number      ='||I.order_number,l_mod_name,1);
6569 		 Debug('Repair number     ='||I.repair_number,l_mod_name,1);
6570 		 Debug('Shipped quantity  ='||TO_CHAR(I.shipped_quantity),l_mod_name,1);
6571 		 Debug('Inventory item id ='||TO_CHAR(I.inventory_item_id),l_mod_name,1);
6572 		 Debug('Organization id   ='||TO_CHAR(I.Organization_id),l_mod_name,1);
6573 
6574 		 Begin
6575 		   select serial_number_control_code,
6576 				  comms_nl_trackable_flag
6577 			into l_srl_ctl_code,
6578 				 l_ib_flag
6579     		  from mtl_system_items
6580     		  where inventory_item_id  = i.inventory_item_id
6581     		   and  organization_id    = i.organization_id;
6582 		 Exception
6583 		   When no_data_found then
6584 			IF ( l_error_level >= G_debug_level) THEN
6585 				  fnd_message.set_name('CSD','CSD_INVALID_INVENTORY_ITEM');
6586 				  FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
6587 			ELSE
6588 				  fnd_message.set_name('CSD','CSD_INVALID_INVENTORY_ITEM');
6589 				  fnd_msg_pub.add;
6590 			END IF;
6591 			RAISE FND_API.G_EXC_ERROR;
6592 		 End;
6593 
6594 		 IF l_srl_ctl_code in (2,5,6) THEN
6595 			Debug('Item is Serialized',l_mod_name,1);
6596 			l_serialized := TRUE;
6597 		 ELSE
6598 			 Debug('Item is Non-Serialized',l_mod_name,1);
6599 			 l_serialized := FALSE;
6600 		 END IF;
6601 
6602 		 IF l_serialized AND
6603 			I.ro_item_id = I.inventory_item_id AND
6604 			I.prod_txn_serial_num <> I.shipped_serial_num THEN
6605 
6606 			  -- Initialize the activity rec
6607 			  l_activity_rec := INIT_ACTIVITY_REC ;
6608 
6609 			  -- Assign the values for activity record
6610 			  l_activity_rec.REPAIR_HISTORY_ID := l_rep_hist_id;
6611 			  l_activity_rec.REPAIR_LINE_ID := I.repair_line_id;
6612 			  l_activity_rec.EVENT_CODE     := 'SSC';
6613 			  l_activity_rec.ACTION_CODE    := 0;
6614 			  l_activity_rec.EVENT_DATE     := I.date_shipped;
6615 			  l_activity_rec.QUANTITY       := null;
6616 			  l_activity_rec.PARAMN1        := i.delivery_detail_id;
6617 			  l_activity_rec.PARAMN2        := i.order_line_number;
6618 			  l_activity_rec.PARAMC2        := i.delivery_name;
6619 			  l_activity_rec.PARAMC3        := i.prod_txn_serial_num;
6620 			  l_activity_rec.PARAMC4        := i.shipped_serial_num;
6621 			  l_activity_rec.OBJECT_VERSION_NUMBER := null;
6622 
6623 			  -- Debug Messages
6624 			  Debug('Calling LOG_ACTIVITY',l_mod_name,2);
6625 
6626 			  -- Calling LOG_ACTIVITY for logging activity
6627 			  -- if serial number on product txn does not match
6628 			  -- with the shipped serial number
6629 			  LOG_ACTIVITY
6630 					 ( p_api_version     => p_api_version,
6631 					   p_commit          => p_commit,
6632 					   p_init_msg_list   => p_init_msg_list,
6633 					   p_validation_level => p_validation_level,
6634 					   x_return_status   => x_return_status,
6635 					   x_msg_count       => x_msg_count,
6636 					   x_msg_data        => x_msg_data,
6637 					   p_activity_rec    => l_activity_rec );
6638 
6639 			   -- Debug Messages
6640 			  Debug('Return Status from LOG_ACTIVITY:'||x_return_status,l_mod_name,2);
6641 			  IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
6642 				 Debug('LOG_ACTIVITY api failed ',l_mod_name,4);
6643 				 RAISE FND_API.G_EXC_ERROR;
6644 			  END IF;
6645 
6646 		 END IF; -- end of serial number mismatch
6647 
6648 		 -- Only if the item on the RO is same as the one on
6649 		 -- product txn, then call the convert API
6650 		 IF I.ro_item_id = I.inventory_item_id THEN
6651 
6652 		  -- Debug messages
6653 		  Debug('Calling CONVERT_TO_RO_uom ',l_mod_name,2);
6654 
6655 		  -- Converting the shipped qty to RO UOM
6656 		  CONVERT_TO_RO_UOM
6657 			( x_return_status   => x_return_status
6658 			 ,p_to_uom_code     => i.ro_uom
6659 			 ,p_item_id         => i.inventory_item_id
6660 			 ,p_from_uom        => NULL
6661 			 ,p_from_uom_code   => i.shipped_uom
6662 			 ,p_from_quantity   => i.shipped_quantity
6663 			 ,x_result_quantity => l_result_ship_qty);
6664 
6665 		  -- Debug messages
6666 		  Debug('Return Status from CONVERT_TO_RO_uom :'||x_return_status,l_mod_name,2);
6667 		  IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
6668 			 Debug('CONVERT_TO_RO_uom api failed ',l_mod_name,4);
6669 			 RAISE FND_API.G_EXC_ERROR;
6670 		  END IF;
6671 		ELSE
6672 			l_result_ship_qty := I.shipped_quantity;
6673 		END IF;
6674 
6675 		-- Update repair orders only for the following action codes
6676 		IF I.action_code in ( 'CUST_PROD','EXCHANGE','REPLACEMENT') then
6677 			-- Updating the repair order with qty
6678 			update csd_repairs
6679 			set quantity_shipped = nvl(quantity_shipped,0)+l_result_ship_qty,
6680 			  object_version_number = object_version_number+1,
6681 			  last_update_date = sysdate,
6682 			  last_updated_by  = fnd_global.user_id,
6683 			  last_update_login = fnd_global.login_id
6684 			where repair_line_id = I.repair_line_id;
6685 			 IF SQL%NOTFOUND THEN
6686 			   IF ( l_error_level >= G_debug_level) THEN
6687 				  fnd_message.set_name('CSD','CSD_RO_UPD_FAILED');
6688 				  fnd_message.set_token('REPAIR_LINE_ID',I.repair_line_id);
6689 				  FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
6690 			   ELSE
6691 				  fnd_message.set_name('CSD','CSD_RO_UPD_FAILED');
6692 				  fnd_message.set_token('REPAIR_LINE_ID',I.repair_line_id);
6693 				  fnd_msg_pub.add;
6694 			   END IF;
6695 			   RAISE FND_API.G_EXC_ERROR;
6696 			END IF;
6697 		END IF;
6698 
6699 
6700 		IF l_serialized and
6701 		   nvl(l_ib_flag,'N') = 'Y' THEN
6702 		  Begin
6703 			Select instance_id
6704 		   into l_instance_id
6705 		   from csi_item_instances
6706 		   where inventory_item_id = I.inventory_item_id
6707 			 and  serial_number      = I.shipped_serial_num;
6708 		  Exception
6709 		   When No_Data_Found then
6710 		  /*Fixed for bug#5563369
6711 		  Correct message name CSD_INSTANCE_MISSING is used instead of
6712 		  CSD_INV_INSTANCE_ID.
6713 		  */
6714 			  IF ( l_error_level >= G_debug_level) THEN
6715 				  fnd_message.set_name('CSD','CSD_INSTANCE_MISSING');
6716 				  fnd_message.set_token('SERIAL_NUM',I.shipped_serial_num);
6717 			  fnd_msg_pub.add;
6718 				  FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
6719 			  fnd_file.put_line(fnd_file.log, fnd_message.get);
6720 			  ELSE
6721 				  fnd_message.set_name('CSD','CSD_INSTANCE_MISSING');
6722 				  fnd_message.set_token('SERIAL_NUM',I.shipped_serial_num);
6723 				  fnd_msg_pub.add;
6724 			  fnd_file.put_line(fnd_file.log, fnd_message.get);
6725 			  /*Fixed for bug#5563369
6726 					Correct message name CSD_INSTANCE_MISSING is used instead of
6727 					CSD_INV_INSTANCE_ID.
6728 				  */
6729 			  END IF;
6730 			  RAISE FND_API.G_EXC_ERROR;
6731 		   When OTHERS then
6732 			  IF ( l_error_level >= G_debug_level) THEN
6733 				  fnd_message.set_name('CSD','CSD_INSTANCE_MISSING');
6734 				  fnd_message.set_token('SERIAL_NUM',I.shipped_serial_num);
6735 			  fnd_msg_pub.add;
6736 				  FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
6737 			  fnd_file.put_line(fnd_file.log, fnd_message.get);
6738 			  ELSE
6739 			  fnd_message.set_name('CSD','CSD_INSTANCE_MISSING');
6740 				  fnd_message.set_token('SERIAL_NUM',I.shipped_serial_num);
6741 				  fnd_msg_pub.add;
6742 			  fnd_file.put_line(fnd_file.log, fnd_message.get);
6743 			  END IF;
6744 			  RAISE FND_API.G_EXC_ERROR;
6745 		 End;
6746 
6747 		--bug#6779806
6748 		ELSIF (nvl(l_ib_flag,'N') = 'Y') THEN
6749 			IF (l_enable_update_instance ='Y') THEN
6750 				Open cur_get_instance_id(I.line_id,I.inventory_item_id);
6751 				Fetch cur_get_instance_id into l_instance_id;
6752 				Close cur_get_instance_id;
6753 			else
6754 				l_instance_id := I.source_instance_id;
6755 			end if;
6756 		Else
6757 		   l_instance_id := I.source_instance_id;
6758 		End If;
6759 
6760 
6761 		-- Updating the product txn with qty,subinventory,lot number
6762 		-- locator id
6763 		update csd_product_transactions
6764 		set sub_inventory = i.subinv,
6765 		  lot_number    = i.lot_number,
6766 			quantity_shipped = nvl(quantity_shipped,0)+I.shipped_quantity,
6767 			locator_id       = i.locator_id,
6768 			source_serial_number = i.shipped_serial_num,
6769 		  source_instance_id   = l_instance_id,
6770 			object_version_number = object_version_number+1,
6771 			last_update_date = sysdate,
6772 			last_updated_by  = fnd_global.user_id,
6773 			last_update_login = fnd_global.login_id
6774 		where product_transaction_id = i.product_transaction_id ;
6775 		IF SQL%NOTFOUND THEN
6776 			IF ( l_error_level >= G_debug_level) THEN
6777 				  fnd_message.set_name('CSD','CSD_PROD_TXN_UPD_FAILED');
6778 				  fnd_message.set_token('PROD_TXN_ID',I.product_transaction_id);
6779 				  FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
6780 			ELSE
6781 				  fnd_message.set_name('CSD','CSD_PROD_TXN_UPD_FAILED');
6782 				  fnd_message.set_token('PROD_TXN_ID',I.product_transaction_id);
6783 				  fnd_msg_pub.add;
6784 			END IF;
6785 			RAISE FND_API.G_EXC_ERROR;
6786 		END IF;
6787 
6788 		-- Updating the product txn with the status
6789 		-- if the line qty is fully rcvd
6790 		update csd_product_transactions
6791 		set prod_txn_status = 'SHIPPED',
6792 			object_version_number = object_version_number+1,
6793 			last_update_date = sysdate,
6794 			last_updated_by  = fnd_global.user_id,
6795 			last_update_login = fnd_global.login_id
6796 		where nvl(quantity_shipped,0) = I.estimate_quantity
6797 		 and  product_transaction_id = i.product_transaction_id ;
6798 
6799 
6800 		--Bug#6779806
6801 		 IF (NOT(l_serialized) and (nvl(l_ib_flag,'N') = 'Y')
6802 			 and (l_instance_id is null)
6803 			 and (l_enable_update_instance ='Y')) then
6804 				--do nothing
6805 				--due to csi_item_instances has not update the instance id yet
6806 				--It is update by the concurent program.
6807 				--if there is not instance id yet, we don't want to
6808 				--update the history table
6809 				null;
6810 		 ELSE
6811 
6812 			 -- Initialize the activity rec
6813 			 l_activity_rec := INIT_ACTIVITY_REC ;
6814 
6815 			 -- Assign the values for activity record
6816 			 l_activity_rec.REPAIR_HISTORY_ID := l_rep_hist_id;
6817 			 l_activity_rec.REPAIR_LINE_ID := I.repair_line_id;
6818 			 l_activity_rec.EVENT_CODE     := 'PS';
6819 			 l_activity_rec.ACTION_CODE    := 0;
6820 			 l_activity_rec.EVENT_DATE     := I.date_shipped;
6821 			 l_activity_rec.QUANTITY       := l_result_ship_qty;
6822 			 l_activity_rec.PARAMN1        := i.delivery_detail_id;
6823 			 l_activity_rec.PARAMN2        := i.order_line_number;
6824 			 l_activity_rec.PARAMN3        := i.organization_id;
6825 			 l_activity_rec.PARAMC2        :=  i.delivery_name;
6826 			 l_activity_rec.PARAMC3        :=  i.org_name;
6827 			 l_activity_rec.PARAMC4        :=  i.subinv;
6828 			 l_activity_rec.OBJECT_VERSION_NUMBER := null;
6829 
6830 			 -- Debug Messages
6831 			 Debug('Calling LOG_ACTIVITY',l_mod_name,2);
6832 
6833 			 -- Calling LOG_ACTIVITY for logging activity
6834 			 -- shipped delivery lines
6835 			 LOG_ACTIVITY
6836 					 ( p_api_version     => p_api_version,
6837 					   p_commit          => p_commit,
6838 					   p_init_msg_list   => p_init_msg_list,
6839 					   p_validation_level => p_validation_level,
6840 					   x_return_status   => x_return_status,
6841 					   x_msg_count       => x_msg_count,
6842 					   x_msg_data        => x_msg_data,
6843 					   p_activity_rec    => l_activity_rec );
6844 
6845 			   -- Debug Messages
6846 			   Debug('Return Status from LOG_ACTIVITY:'||x_return_status,l_mod_name,2);
6847 			   IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
6848 				 Debug('LOG_ACTIVITY api failed ',l_mod_name,4);
6849 				 RAISE FND_API.G_EXC_ERROR;
6850 			   END IF;
6851 
6852 		  END IF;
6853 
6854 	End if; --end If (I.date_shipped is not null) --bug#7285024
6855 
6856     EXCEPTION
6857         WHEN FND_API.G_EXC_ERROR THEN
6858             Debug('In FND_API.G_EXC_ERROR EXCEPTION',l_mod_name,4);
6859             ROLLBACK TO SHIPMENT_LINES;
6860             -- In case of error, exit the loop. Commit the processed records
6861             -- and rollback the error record
6862             --RAISE FND_API.G_EXC_ERROR;
6863             x_return_status := FND_API.G_RET_STS_ERROR ;
6864             EXIT;
6865     END;
6866        -- Commit for every 500 records
6867        -- one should COMMIT less frequently within a PL/SQL loop to
6868        -- prevent ORA-1555 (Snapshot too old) errors
6869        l_total_records := l_total_records + 1;
6870        IF mod(l_total_records, l_commit_size) = 0 THEN -- Commit every 500 records
6871            IF FND_API.To_Boolean( p_commit ) THEN
6872                 COMMIT WORK;
6873            END IF;
6874 	   END IF;
6875    END LOOP;
6876 
6877    IF(SHIPMENT_LINES_ALL%ISOPEN ) THEN
6878        CLOSE SHIPMENT_LINES_ALL;
6879    END IF;
6880    IF(SHIPMENT_LINES%ISOPEN ) THEN
6881        CLOSE SHIPMENT_LINES;
6882    END IF;
6883    IF(SHIPMENT_LINES_BY_DATE%ISOPEN ) THEN
6884        CLOSE SHIPMENT_LINES_BY_DATE;
6885    END IF;
6886 
6887   ------------ process cancelled orders.
6888    Debug('processing cancelled orders in SO_SHIP_UPDATE',l_mod_name,1);
6889   if(p_repair_line_id is not null) then
6890      Debug('processing repairline['||p_repair_line_id||']',l_mod_name,1);
6891   	Check_for_Cancelled_order(p_repair_line_id);
6892   else
6893   	FOR Repln_Rec in Cur_Cancelled_repair_lines
6894 	LOOP
6895           Debug('processing repairline['||repln_rec.repair_line_id||']',l_mod_name,1);
6896 		check_for_cancelled_order(Repln_rec.Repair_line_id);
6897 	END LOOP;
6898   End if;
6899    Debug('At the end of processing cancelled orders in SO_SHIP_UPDATE',l_mod_name,1);
6900   -----------------------
6901 
6902    -- Log seeded messages for the number of processed records
6903    fnd_message.set_name('CSD','CSD_DRC_SHIP_TOTAL_REC_PROC');
6904    fnd_message.set_token('TOT_REC',to_char(l_total_records));
6905    FND_MSG_PUB.ADD;
6906 
6907    -- Debug messages
6908    Debug(fnd_message.get,l_mod_name,1);
6909 
6910    -- Log messages in the concurrent log and output file
6911    fnd_file.put_line(fnd_file.log,fnd_message.get);
6912    fnd_file.put_line(fnd_file.output,fnd_message.get);
6913 
6914    -- Standard check of p_commit.
6915    IF FND_API.To_Boolean( p_commit ) THEN
6916        COMMIT WORK;
6917    END IF;
6918 
6919    -- Standard call to get message count and IF count is  get message info.
6920    FND_MSG_PUB.Count_And_Get
6921           (p_count  =>  x_msg_count,
6922            p_data   =>  x_msg_data);
6923 
6924  EXCEPTION
6925     WHEN FND_API.G_EXC_ERROR THEN
6926           Debug('FND_API.G_EXC_ERROR  Exception',l_mod_name,4);
6927           ROLLBACK TO SO_SHIP_UPDATE;
6928           x_return_status := FND_API.G_RET_STS_ERROR ;
6929           FND_MSG_PUB.Count_And_Get
6930                 (p_count  =>  x_msg_count,
6931                  p_data   =>  x_msg_data  );
6932     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
6933           Debug('FND_API.G_EXC_UNEXPECTED_ERROR Exception',l_mod_name,4);
6934           ROLLBACK TO SO_SHIP_UPDATE;
6935           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
6936           FND_MSG_PUB.Count_And_Get
6937                 ( p_count  =>  x_msg_count,
6938                   p_data   =>  x_msg_data );
6939     WHEN OTHERS THEN
6940           Debug('OTHERS Exception',l_mod_name,4);
6941           ROLLBACK TO SO_SHIP_UPDATE;
6942           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
6943           IF  FND_MSG_PUB.Check_Msg_Level
6944                   (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
6945           THEN
6946               FND_MSG_PUB.Add_Exc_Msg
6947                   (G_PKG_NAME ,
6948                    l_api_name  );
6949           END IF;
6950           FND_MSG_PUB.Count_And_Get
6951                   (p_count  =>  x_msg_count,
6952                    p_data   =>  x_msg_data );
6953 END SO_SHIP_UPDATE;
6954 
6955 
6956 /*-------------------------------------------------------------------------------------*/
6957 /* Procedure name: IO_SHIP_UPDATE                                                      */
6958 /* Description   : Procedure called from the Update api to update the depot tables     */
6959 /*                 for the shipment against Internal sales order                       */
6960 /*                 It also logs activities for the deliver txn lines                   */
6961 /* Called from   : Called from SHIP_UPDATE API                                         */
6962 /* STANDARD PARAMETERS                                                                 */
6963 /*  In Parameters :                                                                    */
6964 /*    p_api_version       NUMBER    Required  Api Version number                       */
6965 /*    p_init_msg_list     VARCHAR2  Optional  To Initialize message stack              */
6966 /*    p_commit            VARCHAR2  Optional  Commits in API                           */
6967 /*    p_validation_level  NUMBER    Optional  validation level                         */
6968 /*                                                                                     */
6969 /*  Output Parameters:                                                                 */
6970 /*   x_return_status     VARCHAR2      Return status of the API                        */
6971 /*   x_msg_count         NUMBER        Number of messages in stack                     */
6972 /*   x_msg_data          VARCHAR2      Error Message from message stack                */
6973 /*                                                                                     */
6974 /* NON-STANDARD PARAMETERS                                                             */
6975 /*   In Parameters                                                                     */
6976 /*    p_order_header_id  NUMBER   Optional   Interal sales order Id                    */
6977 /* Output Parm :                                                                       */
6978 /* Change Hist :                                                                       */
6979 /*   09/20/03  vlakaman  Initial Creation.                                             */
6980 /*-------------------------------------------------------------------------------------*/
6981 
6982 PROCEDURE IO_SHIP_UPDATE
6983         ( p_api_version          IN   NUMBER,
6984           p_commit               IN   VARCHAR2,
6985           p_init_msg_list        IN   VARCHAR2,
6986           p_validation_level     IN   NUMBER,
6987           x_return_status        OUT  NOCOPY  VARCHAR2,
6988           x_msg_count            OUT  NOCOPY  NUMBER,
6989           x_msg_data             OUT  NOCOPY  VARCHAR2,
6990           p_order_header_id      IN   NUMBER
6991         ) IS
6992 
6993   -- Cursor to get all the order lines for a
6994   -- specific order header id
6995   --Chnaged the cursor to remove the OR condition on the repair line id.
6996   --Vijay 11/4/04
6997   --saupadhy 04/15/05 : BUg# 4279958 : Problem: When partial Shipping is done for both
6998   -- move_in and Move_Out lines,only first shiplines information is updated on logistics lines
6999   -- subsequest so lines information is not updated.
7000   -- Cause: Cursors Delivery_Lines and Delivery_lines_all have hard join with mtl_trx_lines table.
7001   -- Records in this table are created only when so line is shipped. In partial shipping scenario, if
7002   -- second line is not shipped at the time update logistics program is run then, it will never be
7003   -- created after update logistics program is run for the first ship line.
7004   -- Solution: Hard Join with Mtl_Trx_line is replaced with outer join.
7005   CURSOR DELIVERY_LINES (p_ord_header_id in number) IS
7006    Select  oel.header_id,
7007            oel.line_id,
7008            oel.ordered_quantity,
7009            oel.source_document_id req_header_id,
7010            oel.source_document_line_id req_line_id,
7011            oel.orig_sys_document_ref req_number,
7012            oel.inventory_item_id,
7013            oel.actual_shipment_date shipment_date,
7014            dd.delivery_detail_id,
7015            dd.shipped_quantity,
7016     ---changed to fix 3801614 , added dsn.serial_number if dd.serial_number is null
7017            nvl(dd.serial_number,dsn.fm_serial_number) del_line_serial_num,
7018            --dd.serial_number del_line_serial_num,
7019            dd.lot_number,
7020            dd.subinventory,
7021            dd.locator_id,
7022            dd.organization_id,
7023            dd.released_status,
7024            dd.requested_quantity,
7025 	     dd.source_header_number order_number,
7026            prl.source_organization_id,
7027 	     prl.source_subinventory,
7028 	     prl.destination_organization_id,
7029 	     prl.destination_subinventory,
7030            mtl.serial_number_control_code,
7031            mtl.lot_control_code,
7032 	     prh.segment1 requisition_number,
7033 	     hao.name source_org_name,
7034 	     hao1.name destination_org_name,
7035            trl.txn_source_id
7036     from   oe_order_lines_all oel,
7037            wsh_delivery_details dd,
7038            wsh_serial_numbers dsn,--Added to fix 3801614
7039            po_requisition_lines_all prl,
7040 	     po_requisition_headers_all prh,
7041            mtl_system_items mtl,
7042 	     hr_all_organization_units hao,
7043 	     hr_all_organization_units hao1,
7044            mtl_txn_request_lines trl
7045    where   oel.header_id   = p_ord_header_id
7046     and    oel.line_id          = dd.source_line_id
7047     and    oel.header_id        = dd.source_header_id
7048     and    prl.requisition_header_id   = prh.requisition_header_id
7049     and    hao.organization_id  = dd.organization_id
7050     and    hao1.organization_id = prl.destination_organization_id
7051     and    oel.source_document_line_id = prl.requisition_line_id
7052     and    oel.ship_from_org_id = mtl.organization_id
7053     and    oel.inventory_item_id= mtl.inventory_item_id
7054     and    dd.move_order_line_id = trl.line_id(+) -- Added to fix 4279958
7055     and    dd.delivery_detail_id  = dsn.delivery_detail_id(+) --Added to fix 3801614
7056     and    exists (Select 'x'
7057                    from csd_product_transactions cpt
7058 			 where cpt.order_header_id = oel.header_id
7059                     and  cpt.prod_txn_status in ('BOOKED','RELEASED'));
7060 
7061 -- New Cursor  for all delivery lines.
7062   CURSOR DELIVERY_LINES_ALL IS
7063    Select  oel.header_id,
7064            oel.line_id,
7065            oel.ordered_quantity,
7066            oel.source_document_id req_header_id,
7067            oel.source_document_line_id req_line_id,
7068            oel.orig_sys_document_ref req_number,
7069            oel.inventory_item_id,
7070            oel.actual_shipment_date shipment_date,
7071            dd.delivery_detail_id,
7072            dd.shipped_quantity,
7073     ---changed to fix 3801614 , added dsn.serial_number if dd.serial_number is null
7074            nvl(dd.serial_number,dsn.fm_serial_number) del_line_serial_num,
7075            --dd.serial_number del_line_serial_num,
7076            dd.lot_number,
7077            dd.subinventory,
7078            dd.locator_id,
7079            dd.organization_id,
7080            dd.released_status,
7081            dd.requested_quantity,
7082 	     dd.source_header_number order_number,
7083            prl.source_organization_id,
7084 	     prl.source_subinventory,
7085 	     prl.destination_organization_id,
7086 	     prl.destination_subinventory,
7087            mtl.serial_number_control_code,
7088            mtl.lot_control_code,
7089 	     prh.segment1 requisition_number,
7090 	     hao.name source_org_name,
7091 	     hao1.name destination_org_name,
7092            trl.txn_source_id
7093     from   oe_order_lines_all oel,
7094            wsh_delivery_details dd,
7095            wsh_serial_numbers dsn,--Added to fix 3801614
7096            po_requisition_lines_all prl,
7097 	     po_requisition_headers_all prh,
7098            mtl_system_items mtl,
7099 	     hr_all_organization_units hao,
7100 	     hr_all_organization_units hao1,
7101            mtl_txn_request_lines trl
7102    where   oel.line_id          = dd.source_line_id
7103     and    oel.header_id        = dd.source_header_id
7104     and    prl.requisition_header_id   = prh.requisition_header_id
7105     and    hao.organization_id  = dd.organization_id
7106     and    hao1.organization_id = prl.destination_organization_id
7107     and    oel.source_document_line_id = prl.requisition_line_id
7108     and    oel.ship_from_org_id = mtl.organization_id
7109     and    oel.inventory_item_id= mtl.inventory_item_id
7110     and    dd.move_order_line_id = trl.line_id(+)  -- Added to fix 4279958
7111     and    dd.delivery_detail_id  = dsn.delivery_detail_id(+) --Added to fix 3801614
7112     and    exists (Select 'x'
7113                    from csd_product_transactions cpt
7114 			 where cpt.order_header_id = oel.header_id
7115                     and  cpt.prod_txn_status in ('BOOKED','RELEASED'));
7116 
7117 
7118 --record tpye to hold delivery lines record selected from the cursor
7119 --above
7120     del  IO_SHIP_LINES_Rec_Type;
7121 
7122    -- Cursor that gets all unit txn
7123    -- for delivery detail id
7124     Cursor MTL_UNIT_TXN ( p_del_line_id in number, p_txn_src_id in number ) is
7125       select
7126          mut.subinventory_code,
7127          mut.locator_id,
7128          mut.serial_number,
7129          mtl.transaction_id
7130       from mtl_material_transactions mtl,
7131            mtl_unit_transactions mut
7132       where mtl.transaction_id     = mut.transaction_id
7133        and  mtl.transaction_source_type_id = 8  -- Internal Order
7134        and  mtl.transaction_type_id  in (50, 62,54,34)
7135        and  mtl.picking_line_id    = p_del_line_id
7136        and  mtl.transaction_source_id = p_txn_src_id ;
7137 
7138    -- Cursor that gets all unit txn
7139    -- for delivery detail id. If the item is lot controlled
7140    -- and serial controlled item then need to join
7141    -- with MTL_TRANSACTION_LOT_NUMBERS
7142     Cursor MTL_UNIT_LOT_TXN ( p_del_line_id in number,p_txn_src_id in number ) is
7143       select
7144          mut.subinventory_code,
7145          mut.locator_id,
7146          mut.serial_number,
7147          mtl.transaction_id
7148       from MTL_TRANSACTION_LOT_NUMBERS mln,
7149            mtl_unit_transactions mut,
7150            mtl_material_transactions mtl
7151       WHERE MLN.SERIAL_TRANSACTION_ID = mut.transaction_id
7152       and  mln.transaction_id = mtl.transaction_id
7153       and  mtl.transaction_source_type_id = 8  -- Internal Order
7154       and  mtl.transaction_type_id  in ( 50,62,54,34)
7155       and  mtl.picking_line_id    = p_del_line_id
7156       and  mtl.transaction_source_id = p_txn_src_id ;
7157 
7158    -- Standard variables
7159    l_api_name          CONSTANT VARCHAR2(30)   := 'IO_SHIP_UPDATE';
7160    l_api_version       CONSTANT NUMBER         := 1.0;
7161 
7162    -- Variables used by the API
7163    l_rep_hist_id         NUMBER;
7164    l_dummy               varchar2(30);
7165    l_total_qty           number;
7166    l_total_del_qty       number;
7167    l_serialized_flag     boolean;
7168    l_log_activity        boolean;
7169    l_prod_txn_exists     boolean;
7170    l_serial_num          mtl_serial_numbers.serial_number%type;
7171    l_lot_num             mtl_lot_numbers.lot_number%type;
7172    l_rep_line_id         number;
7173    l_remaining_qty       number;
7174    l_shipped_qty         number;
7175    l_pt_line_qty         number;
7176    l_pt_shipped_qty      number;
7177    l_total_shipped_qty   number;
7178    l_prod_txn_shipped_qty number;
7179    l_action_type         csd_product_transactions.action_type%type;
7180    l_action_code         csd_product_transactions.action_code%type;
7181    l_prod_txn_id         number := NULL;
7182    l_prod_txn_status     csd_product_transactions.prod_txn_status%type ;
7183    l_release_so_flag     csd_product_transactions.release_sales_order_flag%type ;
7184    l_ship_so_flag        csd_product_transactions.ship_sales_order_flag%type ;
7185    l_qty_shipped         number;
7186 
7187    -- activity record
7188    l_activity_rec      activity_rec_type;
7189 
7190    -- Variables for the FND Log
7191    l_error_level  number   := FND_LOG.LEVEL_ERROR;
7192    l_mod_name     varchar2(2000) := 'csd.plsql.csd_update_programs_pvt.io_ship_update';
7193 
7194    -- User defined exception
7195    PROCESS_ERROR  EXCEPTION;
7196    SKIP_RECORD    EXCEPTION;
7197 
7198  BEGIN
7199 
7200    -- Standard Start of API savepoint
7201    SAVEPOINT  IO_SHIP_UPDATE;
7202 
7203    -- Initialize API return status to success
7204    x_return_status := FND_API.G_RET_STS_SUCCESS;
7205 
7206    Debug('At the Beginning of IO_SHIP_UPDATE',l_mod_name,1 );
7207    Debug('Order Header Id ='||p_order_header_id,l_mod_name,1);
7208 
7209    -- Standard call to check for call compatibility.
7210    IF NOT FND_API.Compatible_API_Call (l_api_version,
7211                                        p_api_version,
7212                                        l_api_name   ,
7213                                        G_PKG_NAME   )
7214        THEN
7215           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7216    END IF;
7217 
7218    -- Initialize message list if p_init_msg_list is set to TRUE.
7219    IF FND_API.to_Boolean( p_init_msg_list ) THEN
7220          FND_MSG_PUB.initialize;
7221    END IF;
7222 
7223    -- Api body starts
7224 
7225    -- In case of Internal orders, the product txns are stamped
7226    -- with the order header id and line id.
7227    -- So Validate if it exists in csd_product_transactions
7228    IF  NVL(p_order_header_id,-999) <> -999 THEN
7229       BEGIN
7230           select 'EXISTS'
7231           into  l_dummy
7232           from  oe_order_headers_all oeh,
7233                 po_requisition_headers_all prh
7234           where oeh.source_document_id = prh.requisition_header_id
7235            and  oeh.header_id = p_order_header_id
7236            and  exists (select 'x'
7237                        from csd_product_transactions cpt
7238                        where cpt.order_header_id = oeh.header_id );
7239       EXCEPTION
7240           WHEN NO_DATA_FOUND THEN
7241              IF ( l_error_level >= G_debug_level) THEN
7242                  fnd_message.set_name('CSD','CSD_INV_ORD_HEADER_ID');
7243                  fnd_message.set_token('HEADER_ID',p_order_header_id);
7244                  FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
7245              ELSE
7246                  fnd_message.set_name('CSD','CSD_INV_ORD_HEADER_ID');
7247                  fnd_message.set_token('HEADER_ID',p_order_header_id);
7248                  fnd_msg_pub.add;
7249              END IF;
7250              RAISE FND_API.G_EXC_ERROR;
7251      END;
7252    END IF;
7253 
7254    --FOR DEL in DELIVERY_LINES(p_order_header_id)
7255    IF(p_order_header_Id is null) THEN
7256      OPEN DELIVERY_LINES_ALL;
7257    ELSE
7258      OPEN DELIVERY_LINES(p_order_header_id);
7259    END IF;
7260 
7261    LOOP
7262     BEGIN
7263 
7264       IF(p_order_header_Id is null) THEN
7265         FETCH  DELIVERY_LINES_ALL INTO del;
7266         EXIT WHEN DELIVERY_LINES_ALL%NOTFOUND;
7267       ELSE
7268         FETCH DELIVERY_LINES INTO del;
7269         EXIT WHEN DELIVERY_LINES%NOTFOUND;
7270       END IF;
7271 
7272        -- Savepoint
7273        SAVEPOINT ORDER_LINES;
7274 
7275        -- Debug messages
7276        Debug('DEL.header_id ='||DEL.header_id,l_mod_name,1);
7277 
7278        -- Get the action type and action code
7279        -- for the product txn line
7280        Begin
7281          select action_type,
7282                 action_code
7283          into   l_action_type,
7284                 l_action_code
7285          from  csd_product_transactions
7286          where order_header_id = DEL.header_id
7287           and  rownum = 1;
7288        EXCEPTION
7289          WHEN NO_DATA_FOUND THEN
7290              IF ( l_error_level >= G_debug_level) THEN
7291                  fnd_message.set_name('CSD','CSD_INV_ORD_HEADER_ID');
7292                  fnd_message.set_token('HEADER_ID',DEL.header_id);
7293                  FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
7294              ELSE
7295                  fnd_message.set_name('CSD','CSD_INV_ORD_HEADER_ID');
7296                  fnd_message.set_token('HEADER_ID',DEL.header_id);
7297                  fnd_msg_pub.add;
7298              END IF;
7299              RAISE FND_API.G_EXC_ERROR;
7300        END;
7301 
7302        -- Debug messages
7303        Debug('l_action_type ='||l_action_type,l_mod_name,1);
7304        Debug('l_action_code ='||l_action_code,l_mod_name,1);
7305 
7306        IF DEL.released_status in ('I','C') THEN
7307             l_release_so_flag := 'Y';
7308             l_ship_so_flag    := 'Y';
7309 	        l_prod_txn_status := 'SHIPPED';
7310        ELSIF DEL.released_status = 'Y' THEN
7311             l_release_so_flag := 'Y';
7312             l_ship_so_flag    := 'N';
7313 	        l_prod_txn_status := 'RELEASED';
7314        ELSE
7315   	      l_release_so_flag := 'N';
7316             l_ship_so_flag    := 'N';
7317 	      l_prod_txn_status := 'BOOKED';
7318        END IF;
7319 
7320 
7321        IF l_action_type = 'MOVE_IN' THEN
7322 
7323             -- Move-In case
7324             Debug('Processing the move-in lines ',l_mod_name,1);
7325 
7326            Select nvl(sum(quantity_shipped),0)
7327            into  l_total_shipped_qty
7328            from  csd_product_transactions
7329            where action_type = 'MOVE_IN'
7330            and   action_code = 'DEFECTIVES'
7331            and   order_line_id = DEL.line_id
7332            and   order_header_id = DEL.header_id;
7333 
7334             -- Debug messages
7335            Debug('l_total_shipped_qty= '||l_total_shipped_qty,l_mod_name,1);
7336 
7337            IF l_total_shipped_qty >= DEL.shipped_quantity THEN
7338                   -- Debug messages
7339                   Debug('Skipping the record ',l_mod_name,1);
7340                   RAISE SKIP_RECORD;
7341            END IF;
7342 
7343          -- Debug messages
7344          Debug('serial_number_control_code ='||DEL.serial_number_control_code,l_mod_name,1);
7345 
7346          IF DEL.serial_number_control_code = 1 THEN
7347 
7348               -- Non-Serialized case
7349               Debug('Item Is Non-Serialized ',l_mod_name,1);
7350 
7351         	  --Initialize the variables
7352    	          l_prod_txn_id := NULL;
7353 
7354               -- Check if the product txn exists
7355               -- for the order line and header id
7356               BEGIN
7357                Select product_transaction_id,
7358                       repair_line_id
7359                into   l_prod_txn_id,
7360                       l_rep_line_id
7361                from  csd_product_transactions
7362                where order_header_id = DEL.header_id
7363                 and  order_line_id   = DEL.line_id
7364                 and  action_type     = l_action_type
7365                 and  action_code     = l_action_code;
7366                 Debug('Product txn exist',l_mod_name,1);
7367                 l_prod_txn_exists := TRUE;
7368               EXCEPTION
7369                 When NO_DATA_FOUND then
7370                   Debug('Product txn does not exist',l_mod_name,1);
7371                   l_prod_txn_exists := FALSE;
7372               END;
7373 
7374             IF l_prod_txn_exists THEN
7375               --If product txn exist then update the shipped qty and the status
7376               UPDATE CSD_PRODUCT_TRANSACTIONS
7377               SET quantity_shipped = nvl(quantity_shipped,0) + nvl(DEL.shipped_quantity,0),
7378                   sub_inventory    =  DEL.subinventory,
7379                   lot_number       =  DEL.lot_number,
7380                   locator_id       =  DEL.locator_id,
7381                   release_sales_order_flag = l_release_so_flag,
7382                   ship_sales_order_flag    = l_ship_so_flag,
7383                   prod_txn_status  = l_prod_txn_status,
7384   		      object_version_number = object_version_number+1,
7385                   last_update_date = sysdate,
7386                   last_updated_by  = fnd_global.user_id,
7387                   last_update_login = fnd_global.login_id
7388               WHERE product_transaction_id = l_prod_txn_id;
7389                IF SQL%NOTFOUND THEN
7390                     IF ( l_error_level >= G_debug_level) THEN
7391                         fnd_message.set_name('CSD','CSD_INV_PROD_TXN_ID');
7392                         fnd_message.set_token('PROD_TXN_ID',l_prod_txn_id);
7393                         FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
7394                     ELSE
7395                         fnd_message.set_name('CSD','CSD_INV_PROD_TXN_ID');
7396                         fnd_message.set_token('PROD_TXN_ID',l_prod_txn_id);
7397                         fnd_msg_pub.add;
7398                     END IF;
7399                     RAISE PROCESS_ERROR;
7400                END IF;
7401 
7402             ELSE -- product txn does not exist
7403     	        -- If product txn does not exist then insert a product txn for the
7404   	        -- split order line
7405               -- Get the repair line id for the order header id
7406               Begin
7407                 Select repair_line_id
7408                  into  l_rep_line_id
7409                 from   csd_product_transactions
7410                 where  order_header_id = DEL.header_id
7411                  and   action_type     = l_action_type
7412                  and   action_code     = l_action_code
7413                  and   rownum = 1;
7414               Exception
7415                 WHEN NO_DATA_FOUND THEN
7416                  IF ( l_error_level >= G_debug_level) THEN
7417                     fnd_message.set_name('CSD','CSD_INV_ORD_HEADER_ID');
7418                     fnd_message.set_token('ORDER_HEADER_ID',DEL.header_id);
7419                     FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
7420                  ELSE
7421                     fnd_message.set_name('CSD','CSD_INV_ORD_HEADER_ID');
7422                     fnd_message.set_token('ORDER_HEADER_ID',DEL.header_id);
7423                     fnd_msg_pub.add;
7424                  END IF;
7425                  RAISE PROCESS_ERROR;
7426               End;
7427               Begin
7428                  Debug('Calling CSD_PRODUCT_TRANSACTIONS_PKG.INSERT_ROW',l_mod_name,2);
7429                  CSD_PRODUCT_TRANSACTIONS_PKG.INSERT_ROW
7430                    (px_PRODUCT_TRANSACTION_ID   => l_prod_txn_id,
7431                     p_REPAIR_LINE_ID            => l_rep_line_id,
7432                     p_ESTIMATE_DETAIL_ID        => NULL,
7433                     p_ACTION_TYPE               => l_action_type,
7434                     p_ACTION_CODE               => l_action_code,
7435                     p_LOT_NUMBER                => DEL.lot_number,
7436                     p_SUB_INVENTORY             => DEL.subinventory,
7437                     p_INTERFACE_TO_OM_FLAG      => 'Y',
7438                     p_BOOK_SALES_ORDER_FLAG     => 'Y',
7439                     p_RELEASE_SALES_ORDER_FLAG  => l_release_so_flag,
7440                     p_SHIP_SALES_ORDER_FLAG     => l_ship_so_flag,
7441                     p_PROD_TXN_STATUS           => l_prod_txn_status,
7442                     p_PROD_TXN_CODE             => '',
7443                     p_LAST_UPDATE_DATE          => sysdate,
7444                     p_CREATION_DATE             => sysdate,
7445                     p_LAST_UPDATED_BY           => fnd_global.user_id,
7446                     p_CREATED_BY                => fnd_global.user_id,
7447                     p_LAST_UPDATE_LOGIN         => null,
7448                     p_ATTRIBUTE1                => '',
7449                     p_ATTRIBUTE2                => '',
7450                     p_ATTRIBUTE3                => '',
7451                     p_ATTRIBUTE4                => '',
7452                     p_ATTRIBUTE5                => '',
7453                     p_ATTRIBUTE6                => '',
7454                     p_ATTRIBUTE7                => '',
7455                     p_ATTRIBUTE8                => '',
7456                     p_ATTRIBUTE9                => '',
7457                     p_ATTRIBUTE10               => '',
7458                     p_ATTRIBUTE11               => '',
7459                     p_ATTRIBUTE12               => '',
7460                     p_ATTRIBUTE13               => '',
7461                     p_ATTRIBUTE14               => '',
7462                     p_ATTRIBUTE15               => '',
7463                     p_CONTEXT                   => '',
7464                     p_OBJECT_VERSION_NUMBER     => 1,
7465                     P_REQ_HEADER_ID             => DEL.req_header_id,
7466            	        P_REQ_LINE_ID               => DEL.req_line_id,
7467             	  P_ORDER_HEADER_ID           => DEL.header_id,
7468             	  P_ORDER_LINE_ID             => DEL.line_id,
7469             	  P_PRD_TXN_QTY_RECEIVED      => 0,
7470             	  P_PRD_TXN_QTY_SHIPPED       => nvl(DEL.shipped_quantity,0),
7471             	  P_SOURCE_SERIAL_NUMBER      => '',
7472             	  P_SOURCE_INSTANCE_ID        => NULL,
7473             	  P_NON_SOURCE_SERIAL_NUMBER  => '',
7474             	  P_NON_SOURCE_INSTANCE_ID    => NULL,
7475                     P_LOCATOR_ID                => DEL.locator_id,
7476             	  P_SUB_INVENTORY_RCVD        => '',
7477             	  P_LOT_NUMBER_RCVD           => '',
7478 			        P_PICKING_RULE_ID           => null,
7479                    P_PROJECT_ID                => null,
7480                    P_TASK_ID                   => null,
7481                    P_UNIT_NUMBER               => '');
7482              Exception
7483                When Others then
7484                  IF ( l_error_level >= G_debug_level) THEN
7485                      fnd_message.set_name('CSD','CSD_PROD_TXN_INSERT_FAILED');
7486                      FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
7487                  ELSE
7488                      fnd_message.set_name('CSD','CSD_PROD_TXN_INSERT_FAILED');
7489                      fnd_msg_pub.add;
7490                  END IF;
7491                  RAISE PROCESS_ERROR;
7492              END;
7493            END IF;
7494 
7495          ELSE -- Serialized scenario
7496 
7497            --Initialize the variables
7498     	     l_prod_txn_id := NULL;
7499 
7500            -- Check if the product txn exists
7501            -- for the order line and header id
7502            BEGIN
7503                  Select product_transaction_id,
7504                         repair_line_id
7505                  into   l_prod_txn_id,
7506                         l_rep_line_id
7507                  from  csd_product_transactions
7508                  where order_header_id = DEL.header_id
7509                   and  order_line_id   = DEL.line_id
7510                   and  action_type     = l_action_type
7511                   and  action_code     = l_action_code
7512                   and  rownum =1;
7513                   Debug('Product txn exist',l_mod_name,1);
7514                   l_prod_txn_exists := TRUE;
7515            EXCEPTION
7516                When NO_DATA_FOUND then
7517                     Debug('Product txn does not exist',l_mod_name,1);
7518                     l_prod_txn_exists := FALSE;
7519            END;
7520 
7521     	     IF DEL.released_status in ('I','C') THEN
7522                 l_qty_shipped := 1;
7523            ELSE
7524                 l_qty_shipped := 0;
7525     	     END IF;
7526 
7527            IF l_prod_txn_exists THEN
7528                        UPDATE CSD_PRODUCT_TRANSACTIONS
7529                        SET quantity_shipped = l_qty_shipped,
7530                            release_sales_order_flag = l_release_so_flag,
7531                            ship_sales_order_flag    = l_ship_so_flag,
7532                            prod_txn_status  = l_prod_txn_status,
7533                            sub_inventory    = DEL.subinventory,
7534                            lot_number       = DEL.lot_number,
7535                            locator_id       = DEL.locator_id,
7536       			   object_version_number = object_version_number+1,
7537                            last_update_date = sysdate,
7538                            last_updated_by  = fnd_global.user_id,
7539                            last_update_login = fnd_global.login_id
7540                        WHERE order_header_id = DEL.header_id
7541                         and  order_line_id   = DEL.line_id
7542                         and  action_type     = l_action_type
7543                         and  action_code     = l_action_code
7544                         and  prod_txn_status in('BOOKED','RELEASED')
7545                         and  rownum <= nvl(DEL.requested_quantity,0) ;
7546                        IF SQL%NOTFOUND THEN
7547                           IF ( l_error_level >= G_debug_level) THEN
7548                              fnd_message.set_name('CSD','CSD_INV_ORDER_LINE_ID');
7549                              fnd_message.set_token('ORDER_LINE_ID',DEL.line_id);
7550                              FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
7551                           ELSE
7552                              fnd_message.set_name('CSD','CSD_INV_ORDER_LINE_ID');
7553                              fnd_message.set_token('ORDER_LINE_ID',DEL.line_id);
7554                              fnd_msg_pub.add;
7555                           END IF;
7556                           RAISE PROCESS_ERROR;
7557                        END IF;
7558            ELSE
7559                        UPDATE CSD_PRODUCT_TRANSACTIONS
7560                        SET quantity_shipped = l_qty_shipped,
7561                            order_line_id    = DEL.line_id,
7562                            release_sales_order_flag = l_release_so_flag,
7563                            ship_sales_order_flag    = l_ship_so_flag,
7564                            prod_txn_status  = l_prod_txn_status,
7565                            sub_inventory    = DEL.subinventory,
7566                            lot_number       = DEL.lot_number,
7567                            locator_id       = DEL.locator_id,
7568   			         object_version_number = object_version_number+1,
7569                            last_update_date = sysdate,
7570                            last_updated_by  = fnd_global.user_id,
7571                            last_update_login = fnd_global.login_id
7572                        WHERE order_header_id = DEL.header_id
7573                         and  action_type     = l_action_type
7574                         and  action_code     = l_action_code
7575                         and  prod_txn_status  in ('BOOKED','RELEASED')
7576                         and  rownum <= nvl(DEL.requested_quantity,0);
7577                        IF SQL%NOTFOUND THEN
7578                           IF ( l_error_level >= G_debug_level) THEN
7579                              fnd_message.set_name('CSD','CSD_INV_ORDER_LINE_ID');
7580                              fnd_message.set_token('ORDER_LINE_ID',DEL.line_id);
7581                              FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
7582                           ELSE
7583                              fnd_message.set_name('CSD','CSD_INV_ORDER_LINE_ID');
7584                              fnd_message.set_token('ORDER_LINE_ID',DEL.line_id);
7585                              fnd_msg_pub.add;
7586                           END IF;
7587                           RAISE PROCESS_ERROR;
7588                        END IF;
7589            END IF;
7590 
7591          END IF;
7592 
7593        ELSE -- action_type = 'MOVE_OUT'
7594 
7595             -- Move-out Lines
7596            Debug('Processing the move-out lines ',l_mod_name,1);
7597 
7598            Select nvl(sum(quantity),0)
7599            into l_total_shipped_qty
7600            from csd_repair_history
7601            where event_code = 'PSI'
7602            and  paramn1    = DEL.delivery_detail_id;
7603 
7604             -- Debug messages
7605            Debug('l_total_shipped_qty= '||l_total_shipped_qty,l_mod_name,1);
7606            IF l_total_shipped_qty >= DEL.shipped_quantity THEN
7607                   -- Debug messages
7608                   Debug('Skipping the record ',l_mod_name,1);
7609                   RAISE SKIP_RECORD;
7610            END IF;
7611 
7612             -- Processing starts for item is pre-deined/serialized inv receipt
7613             -- In case of partial shipment against the move-out internal orders
7614             -- the order lines gets split. But the product txn exist only for the
7615             -- original order line id and header id. In such case the update program
7616             -- will create new product txn lines for the split order lines
7617             --
7618             --BEFORE SPLIT
7619             --
7620             --  Order Lines
7621             --      Line Id    Header Id  Qty  Shipped Qty  PO Line Id  Split_From Line_id
7622             --        L1         H1        10      0          PL1
7623             --
7624             --  Delivery Lines
7625             --    Del Line Id    Ord Line Id   Shipped Qty  Rel Status
7626             --       D1             L1              0          R/Y
7627             --
7628             --  Repair Order
7629             --     RO Number       RO qty   Shipped Qty
7630             --       R1            10          0
7631             --
7632             --  PRODUCT TXN
7633             --    Prod Txn      Line Id   Header Id   PO Line Id    Shipped Qty
7634             --     P1            L1         H1          PL1             0
7635             --
7636             --
7637             --  AFTER ORDER LINE SPLIT (Partial Shipping)
7638             --  Order Lines
7639             --     Line Id    Header Id  Qty  Shipped Qty  PO Line Id  Split_From Line_id
7640             --       L1         H1        6     6          PL1
7641             --       L2         H1        1     1          PL1           L1
7642             --       L3         H1        3     3          PL1           L2
7643             --
7644             --  Delivery Lines
7645             --    Del Line Id    Ord Line Id   Shipped Qty
7646             --       D1             L1              6
7647             --       D2             L2              1
7648             --       D3             L3              3
7649             --
7650             --  Repair Order
7651             --     RO Number       RO qty   Shipped Qty
7652             --       R1            10          0
7653             --
7654             --  PRODUCT TXN
7655             --    Prod Txn      Line Id   Header Id   PO Line Id    Shipped Qty
7656             --     P1            L1         H1          PL1             6
7657             --     P2            L2         H1          PL1             1
7658             --     P3            L3         H1          PL1             3
7659             --
7660             --
7661      	      IF DEL.serial_number_control_code = 1 THEN
7662 
7663           	    --Initialize the variables
7664       	    l_prod_txn_id := NULL;
7665 
7666                 -- Check if the product txn exists
7667                 -- for the order line and header id
7668                 BEGIN
7669                  Select product_transaction_id,
7670                         repair_line_id
7671                  into   l_prod_txn_id,
7672                         l_rep_line_id
7673                  from  csd_product_transactions
7674                  where order_header_id = DEL.header_id
7675                   and  order_line_id   = DEL.line_id
7676                   and  action_type     = l_action_type
7677                   and  action_code     = l_action_code;
7678                   Debug('Product txn exist',l_mod_name,1);
7679                   l_prod_txn_exists := TRUE;
7680                 EXCEPTION
7681                  When NO_DATA_FOUND then
7682                     Debug('Product txn does not exist',l_mod_name,1);
7683                     l_prod_txn_exists := FALSE;
7684                 END;
7685 
7686                 -- Non-Serialized case
7687                 Debug('Item Is Non-Serialized ',l_mod_name,1);
7688 
7689               IF l_prod_txn_exists THEN
7690                 --If product txn exist then update the shipped qty and the status
7691                 UPDATE CSD_PRODUCT_TRANSACTIONS
7692                 SET quantity_shipped = nvl(quantity_shipped,0) + nvl(DEL.shipped_quantity,0),
7693                     sub_inventory    =  DEL.subinventory,
7694                     lot_number       =  DEL.lot_number,
7695                     locator_id       =  DEL.locator_id,
7696                     release_sales_order_flag = l_release_so_flag,
7697                     ship_sales_order_flag    = l_ship_so_flag,
7698                     prod_txn_status  = l_prod_txn_status,
7699     		        object_version_number = object_version_number+1,
7700                     last_update_date = sysdate,
7701                     last_updated_by  = fnd_global.user_id,
7702                     last_update_login = fnd_global.login_id
7703                 WHERE product_transaction_id = l_prod_txn_id;
7704                  IF SQL%NOTFOUND THEN
7705                       IF ( l_error_level >= G_debug_level) THEN
7706                           fnd_message.set_name('CSD','CSD_INV_PROD_TXN_ID');
7707                           fnd_message.set_token('PROD_TXN_ID',l_prod_txn_id);
7708                           FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
7709                       ELSE
7710                           fnd_message.set_name('CSD','CSD_INV_PROD_TXN_ID');
7711                           fnd_message.set_token('PROD_TXN_ID',l_prod_txn_id);
7712                           fnd_msg_pub.add;
7713                       END IF;
7714                       RAISE PROCESS_ERROR;
7715                  END IF;
7716 
7717               ELSE -- product txn does not exist
7718        	    -- If product txn does not exist then insert a product txn for the
7719     	          -- split order line
7720                 -- Get the repair line id for the order header id
7721                 Begin
7722                   Select repair_line_id
7723                    into  l_rep_line_id
7724                   from   csd_product_transactions
7725                   where  order_header_id = DEL.header_id
7726                    and   action_type     = l_action_type
7727                    and   action_code     = l_action_code
7728                    and   rownum = 1;
7729                 Exception
7730                   WHEN NO_DATA_FOUND THEN
7731                    IF ( l_error_level >= G_debug_level) THEN
7732                       fnd_message.set_name('CSD','CSD_INV_ORD_HEADER_ID');
7733                       fnd_message.set_token('ORDER_HEADER_ID',DEL.header_id);
7734                       FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
7735                    ELSE
7736                       fnd_message.set_name('CSD','CSD_INV_ORD_HEADER_ID');
7737                       fnd_message.set_token('ORDER_HEADER_ID',DEL.header_id);
7738                       fnd_msg_pub.add;
7739                    END IF;
7740                    RAISE PROCESS_ERROR;
7741                 End;
7742                 Begin
7743                    Debug('Calling CSD_PRODUCT_TRANSACTIONS_PKG.INSERT_ROW',l_mod_name,2);
7744                    CSD_PRODUCT_TRANSACTIONS_PKG.INSERT_ROW
7745                      (px_PRODUCT_TRANSACTION_ID   => l_prod_txn_id,
7746                       p_REPAIR_LINE_ID            => l_rep_line_id,
7747                       p_ESTIMATE_DETAIL_ID        => NULL,
7748                       p_ACTION_TYPE               => l_action_type,
7749                       p_ACTION_CODE               => l_action_code,
7750                       p_LOT_NUMBER                => DEL.lot_number,
7751                       p_SUB_INVENTORY             => DEL.subinventory,
7752                       p_INTERFACE_TO_OM_FLAG      => 'Y',
7753                       p_BOOK_SALES_ORDER_FLAG     => 'Y',
7754                       p_RELEASE_SALES_ORDER_FLAG  => l_release_so_flag,
7755                       p_SHIP_SALES_ORDER_FLAG     => l_ship_so_flag,
7756                       p_PROD_TXN_STATUS           => l_prod_txn_status,
7757                       p_PROD_TXN_CODE             => '',
7758                       p_LAST_UPDATE_DATE          => sysdate,
7759                       p_CREATION_DATE             => sysdate,
7760                       p_LAST_UPDATED_BY           => fnd_global.user_id,
7761                       p_CREATED_BY                => fnd_global.user_id,
7762                       p_LAST_UPDATE_LOGIN         => null,
7763                       p_ATTRIBUTE1                => '',
7764                       p_ATTRIBUTE2                => '',
7765                       p_ATTRIBUTE3                => '',
7766                       p_ATTRIBUTE4                => '',
7767                       p_ATTRIBUTE5                => '',
7768                       p_ATTRIBUTE6                => '',
7769                       p_ATTRIBUTE7                => '',
7770                       p_ATTRIBUTE8                => '',
7771                       p_ATTRIBUTE9                => '',
7772                       p_ATTRIBUTE10               => '',
7773                       p_ATTRIBUTE11               => '',
7774                       p_ATTRIBUTE12               => '',
7775                       p_ATTRIBUTE13               => '',
7776                       p_ATTRIBUTE14               => '',
7777                       p_ATTRIBUTE15               => '',
7778                       p_CONTEXT                   => '',
7779                       p_OBJECT_VERSION_NUMBER     => 1,
7780                       P_REQ_HEADER_ID             => DEL.req_header_id,
7781              	    P_REQ_LINE_ID               => DEL.req_line_id,
7782               	    P_ORDER_HEADER_ID           => DEL.header_id,
7783               	    P_ORDER_LINE_ID             => DEL.line_id,
7784               	    P_PRD_TXN_QTY_RECEIVED      => 0,
7785               	    P_PRD_TXN_QTY_SHIPPED       => nvl(DEL.shipped_quantity,0),
7786               	    P_SOURCE_SERIAL_NUMBER      => '',
7787               	    P_SOURCE_INSTANCE_ID        => NULL,
7788               	    P_NON_SOURCE_SERIAL_NUMBER  => '',
7789               	    P_NON_SOURCE_INSTANCE_ID    => NULL,
7790                       P_LOCATOR_ID                => DEL.locator_id,
7791               	    P_SUB_INVENTORY_RCVD        => '',
7792               	    P_LOT_NUMBER_RCVD           => '',
7793 			          P_PICKING_RULE_ID           => null,
7794                    P_PROJECT_ID                => null,
7795                    P_TASK_ID                   => null,
7796                    P_UNIT_NUMBER               => '');
7797 
7798                Exception
7799                  When Others then
7800                    IF ( l_error_level >= G_debug_level) THEN
7801                        fnd_message.set_name('CSD','CSD_PROD_TXN_INSERT_FAILED');
7802                        FND_LOG.MESSAGE(l_error_level,l_mod_name, FALSE);
7803                    ELSE
7804                        fnd_message.set_name('CSD','CSD_PROD_TXN_INSERT_FAILED');
7805                        fnd_msg_pub.add;
7806                    END IF;
7807                    RAISE PROCESS_ERROR;
7808                END;
7809              END IF; -- end if prod_txn_exists
7810 
7811              IF DEL.released_status in ('C','I') THEN
7812 
7813                 -- Updating the repair order with shipped_qty
7814                 Update csd_repairs
7815                 set quantity_shipped = nvl(quantity_shipped,0) + DEL.shipped_quantity,
7816                     object_version_number = object_version_number+1,
7817                     last_update_date = sysdate,
7818                     last_updated_by  = fnd_global.user_id,
7819                     last_update_login = fnd_global.login_id
7820                 where repair_line_id = l_rep_line_id;
7821                 IF SQL%NOTFOUND THEN
7822                        IF ( l_error_level >= G_debug_level)  THEN
7823                               FND_MESSAGE.SET_NAME('CSD','CSD_RO_UPD_FAILED');
7824                               FND_MESSAGE.SET_TOKEN('REPAIR_LINE_ID',l_rep_line_id);
7825                               FND_LOG.MESSAGE(l_error_level,l_mod_name,FALSE);
7826                        ELSE
7827                               FND_MESSAGE.SET_NAME('CSD','CSD_RO_UPD_FAILED');
7828                               FND_MESSAGE.SET_TOKEN('REPAIR_LINE_ID',l_rep_line_id);
7829                               FND_MSG_PUB.ADD;
7830                        END IF;
7831                        RAISE PROCESS_ERROR;
7832                 END IF;
7833 
7834               -- Initialize the activity rec
7835               l_activity_rec := INIT_ACTIVITY_REC ;
7836 
7837               -- Assign the values for activity record
7838               l_activity_rec.REPAIR_HISTORY_ID := l_rep_hist_id;
7839               l_activity_rec.REPAIR_LINE_ID := l_rep_line_id;
7840               l_activity_rec.EVENT_CODE     := 'PSI';
7841               l_activity_rec.ACTION_CODE    := 0;
7842               l_activity_rec.EVENT_DATE     := DEL.shipment_date;
7843               l_activity_rec.QUANTITY       := DEL.shipped_quantity;
7844               l_activity_rec.PARAMN1        := DEL.delivery_detail_id;
7845               l_activity_rec.PARAMN2        := l_prod_txn_id;
7846               l_activity_rec.PARAMN3        := DEL.source_organization_id;
7847               l_activity_rec.PARAMN5        := DEL.destination_organization_id;
7848               l_activity_rec.PARAMC1        := DEL.order_number;
7849               l_activity_rec.PARAMC2        := DEL.requisition_number;
7850               l_activity_rec.PARAMC3        := DEL.source_org_name;
7851               l_activity_rec.PARAMC4        := DEL.source_subinventory;
7852               l_activity_rec.PARAMC5        := DEL.destination_org_name;
7853               l_activity_rec.PARAMC6        := DEL.destination_subinventory;
7854               l_activity_rec.OBJECT_VERSION_NUMBER := null;
7855 
7856              -- Debug Messages
7857              Debug('Calling LOG_ACTIVITY',l_mod_name,2);
7858 
7859              -- Calling LOG_ACTIVITY for logging activity
7860              LOG_ACTIVITY
7861                     ( p_api_version     => p_api_version,
7862                       p_commit          => p_commit,
7863                       p_init_msg_list   => p_init_msg_list,
7864                       p_validation_level => p_validation_level,
7865                       x_return_status   => x_return_status,
7866                       x_msg_count       => x_msg_count,
7867                       x_msg_data        => x_msg_data,
7868                       p_activity_rec    => l_activity_rec );
7869 
7870                -- Debug Messages
7871               Debug('Return Status from LOG_ACTIVITY:'||x_return_status,l_mod_name,2);
7872 
7873               IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
7874                  Debug('LOG_ACTIVITY api failed ',l_mod_name,4);
7875                  RAISE FND_API.G_EXC_ERROR;
7876               END IF;
7877 
7878              END IF; -- end of del.released_status in ('c','i')
7879 
7880             ELSE --DEL.serial_number_control_code <> 1 THEN
7881 
7882              Debug('DEL.LOT_CONTROL_CODE ='||DEL.LOT_CONTROL_CODE ,l_mod_name,1);
7883 
7884              -- In case of item serialized at sales order issue, the serial number is stored
7885              -- in the material unit transactions. Need to join the mtl_material_transactions
7886              -- with mtl_unit_transactions to get the serial numbers.
7887              --  Serial Number Control Code = 6 (Serialized at Sales order Issue)
7888              --   Delivery Lines
7889              --     Del Line Id    Order Line Id   Shipped Qty
7890              --        D1            L1              6
7891              --        D2            L2              3
7892              --        D3            L2              1
7893              --
7894              --  Unit Trxs
7895              --   Txn Id    Del Line id  Serial Num
7896              --     T1         D1           SN11
7897              --     T2         D1           SN12
7898              --     T3         D1           SN13
7899              --     T4         D1           SN14
7900              --     T5         D1           SN15
7901              --     T6         D1           SN16
7902              --     T7         D2           SN17
7903              --     T8         D2           SN18
7904              --     T9         D2           SN19
7905              --     T10        D3           SN20
7906              --
7907             IF DEL.LOT_CONTROL_CODE = 1 THEN
7908              Debug(' Item is not lot controlled',l_mod_name,1);
7909 
7910              FOR UT in MTL_UNIT_TXN (DEL.delivery_detail_id, DEL.txn_source_id)
7911              LOOP
7912               BEGIN
7913                   Begin
7914 			    -- saupadhy 3757519 07102004 Commented line Supercession_inv_item_id is null
7915 			    -- as this may not be true.
7916 			    -- Added line cpt.Order_Header_id = Del.Header_Id as this
7917                    Select cpt.product_transaction_id,
7918                           cpt.repair_line_id
7919                     into  l_prod_txn_id,
7920                           l_rep_line_id
7921                    from   csd_product_transactions cpt,
7922                           csd_repairs cra
7923                    where cpt.order_header_id = DEL.Header_id
7924                    and   cpt.action_type    = 'MOVE_OUT'
7925                    and   cpt.action_code    = 'USABLES'
7926                    and   cpt.prod_txn_status  in ('BOOKED', 'RELEASED')
7927                    -- and   cra.supercession_inv_item_id is null
7928   			    and   cpt.source_serial_number is null
7929 			    and   cpt.repair_line_id = cra.repair_line_id
7930   			    and   cra.serial_number  = UT.serial_number ;
7931 
7932                    Debug('Product txn line found',l_mod_name,1);
7933 
7934                    -- Updating the product txns with status,shipped_qty
7935                    Update csd_product_transactions
7936                    set prod_txn_status  = 'SHIPPED',
7937                        quantity_shipped =  1,
7938                        sub_inventory    =  DEL.subinventory,
7939                        locator_id       =  DEL.locator_id,
7940                        lot_number       =  DEL.lot_number,
7941                        release_sales_order_flag = 'Y',
7942                        ship_sales_order_flag = 'Y',
7943                        order_line_id    = DEL.line_id,
7944                        source_serial_number = UT.serial_number,
7945                        object_version_number = object_version_number+1,
7946                        last_update_date = sysdate,
7947                        last_updated_by  = fnd_global.user_id,
7948                        last_update_login = fnd_global.login_id
7949                    where product_transaction_id = l_prod_txn_id;
7950                     IF SQL%NOTFOUND THEN
7951                        IF ( l_error_level >= G_debug_level)  THEN
7952                          FND_MESSAGE.SET_NAME('CSD','CSD_INV_PROD_TXN_ID');
7953                          FND_MESSAGE.SET_TOKEN('PROD_TXN_ID',l_prod_txn_id);
7954                          FND_LOG.MESSAGE(l_error_level,l_mod_name,FALSE);
7955                        ELSE
7956                          FND_MESSAGE.SET_NAME('CSD','CSD_INV_PROD_TXN_ID');
7957                          FND_MESSAGE.SET_TOKEN('PROD_TXN_ID',l_prod_txn_id);
7958                          FND_MSG_PUB.ADD;
7959                        END IF;
7960                        RAISE PROCESS_ERROR;
7961                     END IF;
7962 
7963                    -- Updating the repair order with shipped_qty
7964                    Update csd_repairs
7965                    set quantity_shipped = 1,
7966                        object_version_number = object_version_number+1,
7967                        last_update_date = sysdate,
7968                        last_updated_by  = fnd_global.user_id,
7969                        last_update_login = fnd_global.login_id
7970                    where repair_line_id = l_rep_line_id;
7971                    IF SQL%NOTFOUND THEN
7972                       IF ( l_error_level >= G_debug_level)  THEN
7973                          FND_MESSAGE.SET_NAME('CSD','CSD_RO_UPD_FAILED');
7974                          FND_MESSAGE.SET_TOKEN('REPAIR_LINE_ID',l_rep_line_id);
7975                          FND_LOG.MESSAGE(l_error_level,l_mod_name,FALSE);
7976                       ELSE
7977                          FND_MESSAGE.SET_NAME('CSD','CSD_RO_UPD_FAILED');
7978                          FND_MESSAGE.SET_TOKEN('REPAIR_LINE_ID',l_rep_line_id);
7979                          FND_MSG_PUB.ADD;
7980                       END IF;
7981                       RAISE PROCESS_ERROR;
7982                    END IF;
7983                        -- product txn exist
7984                       l_prod_txn_exists := TRUE;
7985                  Exception
7986                     WHEN NO_DATA_FOUND THEN
7987                        Debug('Product txn line not found',l_mod_name,1);
7988                        -- product txn does not exist
7989                        l_prod_txn_exists := FALSE;
7990                  End;
7991 
7992                         --If the serial number on the unit transactions may not be in the
7993       			-- same order as the product txn. In a delivery some serial numbers could find
7994       			-- repair order/product txn combination. But some may not have a matching serial number
7995       			-- So we do not want to take repair order/product txn which could have a matching serial
7996       			-- number in the later unit transactions
7997       			--
7998       			-- Product Txn
7999       			-- Prod Txn  RO Num Qty RO Serial Num
8000       			--  P1         R1    1     SN1
8001       			--  P2         R2    1     SN2
8002       			--  P3         R3    1     SN3
8003       			--  P4         R4    1     SN4
8004       			--  P5         R5    1     SN5
8005       			--
8006       			-- Delivery Lines
8007       			-- Del Id  Ship Qty Ord Line
8008       			--  D1       2        L1
8009       			--  D2       3        L2
8010       			--
8011       			-- Unit Transactions
8012       			-- Txn Id  Del Id  Qty  Serial Num
8013       			--  M1       D1    1     SN5
8014       			--  M2       D1    1     SN1
8015       			--  M3       D2    1     SN9
8016       			--  M4       D2    1     SN2
8017       			--  M5       D2    1     SN3
8018       			-- The serial number would finally matched as follows:
8019       			-- Ro Num Prod Txn RO SN  UTSerial Num
8020       			--  R1      P1      SN1      SN1
8021       			--  R2      P2      SN2      SN2
8022       			--  R3      P3      SN3      SN3
8023       			--  R4      P4      SN4      SN9
8024       			--  R5      P5      SN5      SN5
8025                  IF NOT(l_prod_txn_exists) THEN
8026                      Begin
8027                        Select cpt.product_transaction_id,
8028                               cpt.repair_line_id
8029                         into  l_prod_txn_id,
8030                               l_rep_line_id
8031                        from   csd_product_transactions cpt,
8032                               csd_repairs cra
8033                        where  cpt.repair_line_id  = cra.repair_line_id
8034                         and   cpt.order_header_id = DEL.header_id
8035                         and   cpt.source_serial_number is null
8036                         and   cpt.action_type    = 'MOVE_OUT'
8037                         and   cpt.action_code    = 'USABLES'
8038                         and   cpt.prod_txn_status in ('BOOKED','RELEASED')
8039                         and   cra.serial_number not in
8040                                          (select
8041                                                mut.serial_number
8042                                           from mtl_material_transactions mtl,
8043                                                mtl_unit_transactions mut,
8044   							     wsh_delivery_details wdd
8045                                           where mtl.transaction_id     = mut.transaction_id
8046                                            and  mtl.transaction_source_type_id = 8  -- Internal Order
8047                                            and  mtl.transaction_type_id  in ( 50,62,54,34)
8048                                            and  mtl.transaction_source_id = DEL.txn_source_id
8049                                            and  mtl.picking_line_id    = wdd.delivery_detail_id
8050   						       and  wdd.source_header_id   = DEL.header_id)
8051                         and   rownum = 1;
8052 
8053                          -- Updating the product txns with status,shipped_qty
8054                          Update csd_product_transactions
8055                          set prod_txn_status  = 'SHIPPED',
8056                              quantity_shipped =  1,
8057                              sub_inventory    =  DEL.subinventory,
8058                              locator_id       =  DEL.locator_id,
8059                              lot_number       =  DEL.lot_number,
8060                              release_sales_order_flag = 'Y',
8061                              ship_sales_order_flag = 'Y',
8062                              order_line_id    = DEL.line_id,
8063                              source_serial_number = UT.serial_number,
8064                              object_version_number = object_version_number+1,
8065                              last_update_date = sysdate,
8066                              last_updated_by  = fnd_global.user_id,
8067                              last_update_login = fnd_global.login_id
8068                          where product_transaction_id = l_prod_txn_id;
8069                          IF SQL%NOTFOUND THEN
8070                                IF ( l_error_level >= G_debug_level)  THEN
8071                                   FND_MESSAGE.SET_NAME('CSD','CSD_PROD_TXN_UPD_FAILED');
8072                                   FND_MESSAGE.SET_TOKEN('PROD_TXN_ID',l_prod_txn_id);
8073                                   FND_LOG.MESSAGE(l_error_level,l_mod_name,FALSE);
8074                                ELSE
8075                                   FND_MESSAGE.SET_NAME('CSD','CSD_PROD_TXN_UPD_FAILED');
8076                                   FND_MESSAGE.SET_TOKEN('PROD_TXN_ID',l_prod_txn_id);
8077                                   FND_MSG_PUB.ADD;
8078                                END IF;
8079                                RAISE PROCESS_ERROR;
8080                          END IF;
8081 
8082                          -- Updating the repair order with shipped_qty
8083                          Update csd_repairs
8084                          set quantity_shipped = 1,
8085                              object_version_number = object_version_number+1,
8086                              last_update_date = sysdate,
8087                              last_updated_by  = fnd_global.user_id,
8088                              last_update_login = fnd_global.login_id
8089                          where repair_line_id = l_rep_line_id;
8090                          IF SQL%NOTFOUND THEN
8091                                IF ( l_error_level >= G_debug_level)  THEN
8092                                  FND_MESSAGE.SET_NAME('CSD','CSD_RO_UPD_FAILED');
8093                                  FND_MESSAGE.SET_TOKEN('REPAIR_LINE_ID',l_rep_line_id);
8094                                  FND_LOG.MESSAGE(l_error_level,l_mod_name,FALSE);
8095                                ELSE
8096                                  FND_MESSAGE.SET_NAME('CSD','CSD_RO_UPD_FAILED');
8097                                  FND_MESSAGE.SET_TOKEN('REPAIR_LINE_ID',l_rep_line_id);
8098                                  FND_MSG_PUB.ADD;
8099                                END IF;
8100                                RAISE PROCESS_ERROR;
8101                          END IF;
8102                      Exception
8103                         WHEN NO_DATA_FOUND THEN
8104                            Debug('In NO_DATA_FOUND exception ',l_mod_name,4);
8105                            RAISE PROCESS_ERROR;
8106                         WHEN OTHERS THEN
8107                            Debug('In Others exception ',l_mod_name,4);
8108                            RAISE PROCESS_ERROR;
8109                      End;
8110                  End If; --not(l_prod_txn_exist)
8111 
8112                       -- Initialize the activity rec
8113                       l_activity_rec := INIT_ACTIVITY_REC ;
8114 
8115                       -- Assign the values for activity record
8116                       l_activity_rec.REPAIR_HISTORY_ID := l_rep_hist_id;
8117                       l_activity_rec.REPAIR_LINE_ID := l_rep_line_id;
8118                       l_activity_rec.EVENT_CODE     := 'PSI';
8119                       l_activity_rec.ACTION_CODE    := 0;
8120                       l_activity_rec.EVENT_DATE     := DEL.shipment_date;
8121                       l_activity_rec.QUANTITY       := 1;
8122                       l_activity_rec.PARAMN1        := DEL.delivery_detail_id;
8123                       l_activity_rec.PARAMN2        := l_prod_txn_id;
8124                       l_activity_rec.PARAMN3        := DEL.source_organization_id;
8125                       l_activity_rec.PARAMN5        := DEL.destination_organization_id;
8126                       l_activity_rec.PARAMC1        := DEL.order_number;
8127                       l_activity_rec.PARAMC2        := DEL.requisition_number;
8128                       l_activity_rec.PARAMC3        := DEL.source_org_name;
8129                       l_activity_rec.PARAMC4        := DEL.source_subinventory;
8130                       l_activity_rec.PARAMC5        := DEL.destination_org_name;
8131                       l_activity_rec.PARAMC6        := DEL.destination_subinventory;
8132                       l_activity_rec.OBJECT_VERSION_NUMBER := null;
8133 
8134                    -- Debug Messages
8135                    Debug('Calling LOG_ACTIVITY',l_mod_name,2);
8136 
8137                    -- Calling LOG_ACTIVITY for logging activity
8138                    -- Shipment against internal sales order
8139                    LOG_ACTIVITY
8140                           ( p_api_version     => p_api_version,
8141                             p_commit          => p_commit,
8142                             p_init_msg_list   => p_init_msg_list,
8143                             p_validation_level => p_validation_level,
8144                             x_return_status   => x_return_status,
8145                             x_msg_count       => x_msg_count,
8146                             x_msg_data        => x_msg_data,
8147                             p_activity_rec    => l_activity_rec );
8148 
8149                   -- Debug Messages
8150                   Debug('Return Status from LOG_ACTIVITY:'||x_return_status,l_mod_name,2);
8151                   IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
8152                      Debug('LOG_ACTIVITY api failed ',l_mod_name,4);
8153                      RAISE FND_API.G_EXC_ERROR;
8154                   END IF;
8155 
8156                EXCEPTION
8157                  WHEN  PROCESS_ERROR  THEN
8158                        Debug('Encountered PROCESS_ERROR exception in UT cursor',l_mod_name,1);
8159                        RAISE FND_API.G_EXC_ERROR;
8160                  WHEN  SKIP_RECORD    THEN
8161                        Debug('Skipping record in UT cursor',l_mod_name,1);
8162                        NULL;
8163                END;
8164              END LOOP;
8165 
8166         ELSE  -- LOT CONTROLLED AND SERIAL CONTROLLED ITEM
8167 
8168            Debug(' Item is lot controlled',l_mod_name,1);
8169 
8170            FOR UT in MTL_UNIT_LOT_TXN (DEL.delivery_detail_id, DEL.txn_source_id )
8171             LOOP
8172               BEGIN
8173                   Begin
8174                    Select cpt.product_transaction_id,
8175                           cpt.repair_line_id
8176                     into  l_prod_txn_id,
8177                           l_rep_line_id
8178                    from   csd_product_transactions cpt,
8179                           csd_repairs cra
8180                    where  cpt.repair_line_id = cra.repair_line_id
8181                     and   cra.supercession_inv_item_id is null
8182   		        and   cpt.source_serial_number is null
8183   			  and   cra.serial_number  = UT.serial_number
8184                     and   cpt.action_type    = 'MOVE_OUT'
8185                     and   cpt.action_code    = 'USABLES'
8186                     and   cpt.prod_txn_status  in ('BOOKED', 'RELEASED');
8187 
8188                    Debug('Product txn line found',l_mod_name,1);
8189 
8190                    -- Updating the product txns with status,shipped_qty
8191                    Update csd_product_transactions
8192                    set prod_txn_status  = 'SHIPPED',
8193                        quantity_shipped =  1,
8194                        sub_inventory    =  DEL.subinventory,
8195                        locator_id       =  DEL.locator_id,
8196                        lot_number       =  DEL.lot_number,
8197                        release_sales_order_flag = 'Y',
8198                        ship_sales_order_flag = 'Y',
8199                        order_line_id    = DEL.line_id,
8200                        source_serial_number = UT.serial_number,
8201                        object_version_number = object_version_number+1,
8202                        last_update_date = sysdate,
8203                        last_updated_by  = fnd_global.user_id,
8204                        last_update_login = fnd_global.login_id
8205                    where product_transaction_id = l_prod_txn_id;
8206                     IF SQL%NOTFOUND THEN
8207                        IF ( l_error_level >= G_debug_level)  THEN
8208                          FND_MESSAGE.SET_NAME('CSD','CSD_INV_PROD_TXN_ID');
8209                          FND_MESSAGE.SET_TOKEN('PROD_TXN_ID',l_prod_txn_id);
8210                          FND_LOG.MESSAGE(l_error_level,l_mod_name,FALSE);
8211                        ELSE
8212                          FND_MESSAGE.SET_NAME('CSD','CSD_INV_PROD_TXN_ID');
8213                          FND_MESSAGE.SET_TOKEN('PROD_TXN_ID',l_prod_txn_id);
8214                          FND_MSG_PUB.ADD;
8215                        END IF;
8216                        RAISE PROCESS_ERROR;
8217                     END IF;
8218 
8219                    -- Updating the repair order with shipped_qty
8220                    Update csd_repairs
8221                    set quantity_shipped = 1,
8222                        object_version_number = object_version_number+1,
8223                        last_update_date = sysdate,
8224                        last_updated_by  = fnd_global.user_id,
8225                        last_update_login = fnd_global.login_id
8226                    where repair_line_id = l_rep_line_id;
8227                    IF SQL%NOTFOUND THEN
8228                       IF ( l_error_level >= G_debug_level)  THEN
8229                          FND_MESSAGE.SET_NAME('CSD','CSD_RO_UPD_FAILED');
8230                          FND_MESSAGE.SET_TOKEN('REPAIR_LINE_ID',l_rep_line_id);
8231                          FND_LOG.MESSAGE(l_error_level,l_mod_name,FALSE);
8232                       ELSE
8233                          FND_MESSAGE.SET_NAME('CSD','CSD_RO_UPD_FAILED');
8234                          FND_MESSAGE.SET_TOKEN('REPAIR_LINE_ID',l_rep_line_id);
8235                          FND_MSG_PUB.ADD;
8236                       END IF;
8237                       RAISE PROCESS_ERROR;
8238                    END IF;
8239                        -- product txn exist
8240                       l_prod_txn_exists := TRUE;
8241                  Exception
8242                     WHEN NO_DATA_FOUND THEN
8243                        Debug('Product txn line not found',l_mod_name,1);
8244                        -- product txn does not exist
8245                        l_prod_txn_exists := FALSE;
8246                  End;
8247 
8248                         --If the serial number on the unit transactions may not be in the
8249       			-- same order as the product txn. In a delivery some serial numbers could find
8250       			-- repair order/product txn combination. But some may not have a matching serial number
8251       			-- So we do not want to take repair order/product txn which could have a matching serial
8252       			-- number in the later unit transactions
8253       			--
8254       			-- Product Txn
8255       			-- Prod Txn  RO Num Qty RO Serial Num
8256       			--  P1         R1    1     SN1
8257       			--  P2         R2    1     SN2
8258       			--  P3         R3    1     SN3
8259       			--  P4         R4    1     SN4
8260       			--  P5         R5    1     SN5
8261       			--
8262       			-- Delivery Lines
8263       			-- Del Id  Ship Qty Ord Line
8264       			--  D1       2        L1
8265       			--  D2       3        L2
8266       			--
8267       			-- Unit Transactions
8268       			-- Txn Id  Del Id  Qty  Serial Num
8269       			--  M1       D1    1     SN5
8270       			--  M2       D1    1     SN1
8271       			--  M3       D2    1     SN9
8272       			--  M4       D2    1     SN2
8273       			--  M5       D2    1     SN3
8274       			-- The serial number would finally matched as follows:
8275       			-- Ro Num Prod Txn RO SN  UTSerial Num
8276       			--  R1      P1      SN1      SN1
8277       			--  R2      P2      SN2      SN2
8278       			--  R3      P3      SN3      SN3
8279       			--  R4      P4      SN4      SN9
8280       			--  R5      P5      SN5      SN5
8281                  IF NOT(l_prod_txn_exists) THEN
8282                      Begin
8283                        Select cpt.product_transaction_id,
8284                               cpt.repair_line_id
8285                         into  l_prod_txn_id,
8286                               l_rep_line_id
8287                        from   csd_product_transactions cpt,
8288                               csd_repairs cra
8289                        where  cpt.repair_line_id  = cra.repair_line_id
8290                         and   cpt.order_header_id = DEL.header_id
8291                         and   cpt.source_serial_number is null
8292                         and   cpt.action_type    = 'MOVE_OUT'
8293                         and   cpt.action_code    = 'USABLES'
8294                         and   cpt.prod_txn_status in ('BOOKED','RELEASED')
8295                         and   cra.serial_number not in
8296                                          (select
8297                                                mut.serial_number
8298                                           from mtl_material_transactions mtl,
8299                                                mtl_unit_transactions mut,
8300   							   wsh_delivery_details wdd
8301                                           where mtl.transaction_id     = mut.transaction_id
8302                                            and  mtl.transaction_source_type_id = 8  -- Internal Order
8303                                            and  mtl.transaction_type_id  in ( 50,62,54,34)
8304                                            and  mtl.transaction_source_id = DEL.txn_source_id
8305                                            and  mtl.picking_line_id    = wdd.delivery_detail_id
8306   						     and  wdd.source_header_id   = DEL.header_id)
8307                         and   rownum = 1;
8308 
8309                          -- Updating the product txns with status,shipped_qty
8310                          Update csd_product_transactions
8311                          set prod_txn_status  = 'SHIPPED',
8312                              quantity_shipped =  1,
8313                              sub_inventory    =  DEL.subinventory,
8314                              locator_id       =  DEL.locator_id,
8315                              lot_number       =  DEL.lot_number,
8316                              release_sales_order_flag = 'Y',
8317                              ship_sales_order_flag = 'Y',
8318                              order_line_id    = DEL.line_id,
8319                              source_serial_number = UT.serial_number,
8320                              object_version_number = object_version_number+1,
8321                              last_update_date = sysdate,
8322                              last_updated_by  = fnd_global.user_id,
8323                              last_update_login = fnd_global.login_id
8324                          where product_transaction_id = l_prod_txn_id;
8325                          IF SQL%NOTFOUND THEN
8326                                IF ( l_error_level >= G_debug_level)  THEN
8327                                   FND_MESSAGE.SET_NAME('CSD','CSD_PROD_TXN_UPD_FAILED');
8328                                   FND_MESSAGE.SET_TOKEN('PROD_TXN_ID',l_prod_txn_id);
8329                                   FND_LOG.MESSAGE(l_error_level,l_mod_name,FALSE);
8330                                ELSE
8331                                   FND_MESSAGE.SET_NAME('CSD','CSD_PROD_TXN_UPD_FAILED');
8332                                   FND_MESSAGE.SET_TOKEN('PROD_TXN_ID',l_prod_txn_id);
8333                                   FND_MSG_PUB.ADD;
8334                                END IF;
8335                                RAISE PROCESS_ERROR;
8336                          END IF;
8337 
8338                          -- Updating the repair order with shipped_qty
8339                          Update csd_repairs
8340                          set quantity_shipped = 1,
8341                              object_version_number = object_version_number+1,
8342                              last_update_date = sysdate,
8343                              last_updated_by  = fnd_global.user_id,
8344                              last_update_login = fnd_global.login_id
8345                          where repair_line_id = l_rep_line_id;
8346                          IF SQL%NOTFOUND THEN
8347                                IF ( l_error_level >= G_debug_level)  THEN
8348                                  FND_MESSAGE.SET_NAME('CSD','CSD_RO_UPD_FAILED');
8349                                  FND_MESSAGE.SET_TOKEN('REPAIR_LINE_ID',l_rep_line_id);
8350                                  FND_LOG.MESSAGE(l_error_level,l_mod_name,FALSE);
8351                                ELSE
8352                                  FND_MESSAGE.SET_NAME('CSD','CSD_RO_UPD_FAILED');
8353                                  FND_MESSAGE.SET_TOKEN('REPAIR_LINE_ID',l_rep_line_id);
8354                                  FND_MSG_PUB.ADD;
8355                                END IF;
8356                                RAISE PROCESS_ERROR;
8357                          END IF;
8358                      Exception
8359                         WHEN NO_DATA_FOUND THEN
8360                            Debug('In NO_DATA_FOUND exception ',l_mod_name,4);
8361                            RAISE PROCESS_ERROR;
8362                         WHEN OTHERS THEN
8363                            Debug('In Others exception ',l_mod_name,4);
8364                            RAISE PROCESS_ERROR;
8365                      End;
8366                  End If; --not(l_prod_txn_exist)
8367 
8368                       -- Initialize the activity rec
8369                       l_activity_rec := INIT_ACTIVITY_REC ;
8370 
8371                       -- Assign the values for activity record
8372                       l_activity_rec.REPAIR_HISTORY_ID := l_rep_hist_id;
8373                       l_activity_rec.REPAIR_LINE_ID := l_rep_line_id;
8374                       l_activity_rec.EVENT_CODE     := 'PSI';
8375                       l_activity_rec.ACTION_CODE    := 0;
8376                       l_activity_rec.EVENT_DATE     := DEL.shipment_date;
8377                       l_activity_rec.QUANTITY       := 1;
8378                       l_activity_rec.PARAMN1        := DEL.delivery_detail_id;
8379                       l_activity_rec.PARAMN2        := l_prod_txn_id;
8380                       l_activity_rec.PARAMN3        := DEL.source_organization_id;
8381                       l_activity_rec.PARAMN5        := DEL.destination_organization_id;
8382                       l_activity_rec.PARAMC1        := DEL.order_number;
8383                       l_activity_rec.PARAMC2        := DEL.requisition_number;
8384                       l_activity_rec.PARAMC3        := DEL.source_org_name;
8385                       l_activity_rec.PARAMC4        := DEL.source_subinventory;
8386                       l_activity_rec.PARAMC5        := DEL.destination_org_name;
8387                       l_activity_rec.PARAMC6        := DEL.destination_subinventory;
8388                       l_activity_rec.OBJECT_VERSION_NUMBER := null;
8389 
8390                    -- Debug Messages
8391                    Debug('Calling LOG_ACTIVITY',l_mod_name,2);
8392 
8393                    -- Calling LOG_ACTIVITY for logging activity
8394                    -- Shipment against internal sales order
8395                    LOG_ACTIVITY
8396                           ( p_api_version     => p_api_version,
8397                             p_commit          => p_commit,
8398                             p_init_msg_list   => p_init_msg_list,
8399                             p_validation_level => p_validation_level,
8400                             x_return_status   => x_return_status,
8401                             x_msg_count       => x_msg_count,
8402                             x_msg_data        => x_msg_data,
8403                             p_activity_rec    => l_activity_rec );
8404 
8405                   -- Debug Messages
8406                   Debug('Return Status from LOG_ACTIVITY:'||x_return_status,l_mod_name,2);
8407                   IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
8408                      Debug('LOG_ACTIVITY api failed ',l_mod_name,4);
8409                      RAISE FND_API.G_EXC_ERROR;
8410                   END IF;
8411 
8412                EXCEPTION
8413                  WHEN  PROCESS_ERROR  THEN
8414                        Debug('Encountered PROCESS_ERROR exception in UT cursor',l_mod_name,1);
8415                        RAISE FND_API.G_EXC_ERROR;
8416                  WHEN  SKIP_RECORD    THEN
8417                        Debug('Skipping record in UT cursor',l_mod_name,1);
8418                        NULL;
8419                END;
8420              END LOOP;
8421 
8422            END IF;
8423 
8424           END IF; -- IF DEL.serial_number_control_code = 1
8425 
8426        END IF; -- end if l_action_type = 'MOVE_IN'
8427 
8428      EXCEPTION
8429        WHEN  PROCESS_ERROR  THEN
8430              Debug('Encountered PROCESS_ERROR exception in DELUT cursor',l_mod_name,1);
8431              RAISE FND_API.G_EXC_ERROR;
8432        WHEN  SKIP_RECORD    THEN
8433              NULL;
8434        WHEN OTHERS THEN
8435              Debug('Encountered OTHERS in DEL cursor',l_mod_name,1);
8436              RAISE FND_API.G_EXC_ERROR;
8437      END;
8438    END LOOP;
8439 
8440    IF(DELIVERY_LINES_ALL%ISOPEN ) THEN
8441        CLOSE DELIVERY_LINES_ALL;
8442    END IF;
8443    IF(DELIVERY_LINES%ISOPEN ) THEN
8444        CLOSE DELIVERY_LINES;
8445    END IF;
8446 
8447 
8448    -- Standard check of p_commit.
8449    IF FND_API.To_Boolean( p_commit ) THEN
8450        COMMIT WORK;
8451    END IF;
8452 
8453    -- Standard call to get message count and IF count is  get message info.
8454    FND_MSG_PUB.Count_And_Get
8455           (p_count  =>  x_msg_count,
8456            p_data   =>  x_msg_data);
8457 
8458  EXCEPTION
8459     WHEN FND_API.G_EXC_ERROR THEN
8460           ROLLBACK TO IO_SHIP_UPDATE;
8461           x_return_status := FND_API.G_RET_STS_ERROR ;
8462           FND_MSG_PUB.Count_And_Get
8463                 (p_count  =>  x_msg_count,
8464                  p_data   =>  x_msg_data  );
8465     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8466           ROLLBACK TO IO_SHIP_UPDATE;
8467           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
8468           FND_MSG_PUB.Count_And_Get
8469                 ( p_count  =>  x_msg_count,
8470                   p_data   =>  x_msg_data );
8471     WHEN OTHERS THEN
8472           ROLLBACK TO IO_SHIP_UPDATE;
8473           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
8474           IF  FND_MSG_PUB.Check_Msg_Level
8475                   (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
8476           THEN
8477               FND_MSG_PUB.Add_Exc_Msg
8478                   (G_PKG_NAME ,
8479                    l_api_name  );
8480           END IF;
8481           FND_MSG_PUB.Count_And_Get
8482                   (p_count  =>  x_msg_count,
8483                    p_data   =>  x_msg_data );
8484 
8485 END IO_SHIP_UPDATE;
8486 
8487 /*-------------------------------------------------------------------------------------*/
8488 /* Procedure name: WIP_UPDATE                                                          */
8489 /* Description  : Procedure called from the UI to update the depot tables              */
8490 /*                for the WIP Job creation/Completion                                  */
8491 /*                                                                                     */
8492 /* Called from   : Called from Depot Repair UI                                         */
8493 /* STANDARD PARAMETERS                                                                 */
8494 /*  In Parameters :                                                                    */
8495 /*    p_api_version       NUMBER    Required  Api Version number                       */
8496 /*    p_init_msg_list     VARCHAR2  Optional  To Initialize message stack              */
8497 /*    p_commit            VARCHAR2  Optional  Commits in API                           */
8498 /*    p_validation_level  NUMBER    Optional  validation level                         */
8499 /*                                                                                     */
8500 /*  Output Parameters:                                                                 */
8501 /*   x_return_status     VARCHAR2      Return status of the API                        */
8502 /*   x_msg_count         NUMBER        Number of messages in stack                     */
8503 /*   x_msg_data          VARCHAR2      Error Message from message stack                */
8504 /*                                                                                     */
8505 /* NON-STANDARD PARAMETERS                                                             */
8506 /*   In Parameters                                                                     */
8507 /*    p_upd_job_completion        Required   Order Type; Possible values -'Y','N'      */
8508 /*    p_repair_line_id   NUMBER   Optional   Repair Order Line Id                      */
8509 /* Output Parm :                                                                       */
8510 /* Change Hist :                                                                       */
8511 /*   09/20/03  vlakaman  Initial Creation.                                             */
8512 /*-------------------------------------------------------------------------------------*/
8513 
8514 Procedure  WIP_UPDATE
8515         ( p_api_version          IN   NUMBER,
8516           p_commit               IN   VARCHAR2,
8517           p_init_msg_list        IN   VARCHAR2,
8518           p_validation_level     IN   NUMBER,
8519           x_return_status        OUT  NOCOPY  VARCHAR2,
8520           x_msg_count            OUT  NOCOPY  NUMBER,
8521           x_msg_data             OUT  NOCOPY  VARCHAR2,
8522           p_upd_job_completion   IN   VARCHAR2,
8523           p_repair_line_id       IN   NUMBER
8524          ) IS
8525 
8526   -- Standard Variables
8527   l_api_name          CONSTANT VARCHAR2(30)   := 'WIP_UPDATE';
8528   l_api_version       CONSTANT NUMBER         := 1.0;
8529 
8530   -- Variables used in the program
8531   l_dummy             varchar2(30);
8532 
8533   -- Variables used for FND log
8534   l_error_level  number   := FND_LOG.LEVEL_ERROR;
8535   l_mod_name     varchar2(2000) := 'csd.plsql.csd_update_programs_pvt.wip_update';
8536 
8537 BEGIN
8538      -- Standard Start of API savepoint
8539      SAVEPOINT  WIP_UPDATE;
8540 
8541      -- Initialize API return status to success
8542      x_return_status := FND_API.G_RET_STS_SUCCESS;
8543 
8544      -- Debug Messages
8545      Debug('At the Beginning of Wip_update ',l_mod_name,1);
8546      Debug('Repair Line Id ='||p_repair_line_id,l_mod_name,1);
8547 
8548      -- Standard call to check for call compatibility.
8549      IF NOT FND_API.Compatible_API_Call (l_api_version,
8550                                          p_api_version,
8551                                          l_api_name   ,
8552                                          G_PKG_NAME   )
8553        THEN
8554           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
8555      END IF;
8556 
8557      -- Initialize message list if p_init_msg_list is set to TRUE.
8558      IF FND_API.to_Boolean( p_init_msg_list ) THEN
8559          FND_MSG_PUB.initialize;
8560      END IF;
8561 
8562      -- Api body starts
8563 
8564      -- Validate the repair line id
8565      IF  p_repair_line_id is not null THEN
8566          IF NOT(csd_process_util.validate_rep_line_id(p_repair_line_id)) THEN
8567               Debug('Validate_rep_line_id failed',l_mod_name,1);
8568               RAISE FND_API.G_EXC_ERROR;
8569          END IF;
8570      END IF;
8571 
8572      -- Debug messages
8573      Debug('Calling  JOB_CREATION_UPDATE',l_mod_name,2);
8574 
8575      JOB_CREATION_UPDATE
8576         ( p_api_version          =>  p_api_version,
8577           p_commit               =>  p_commit,
8578           p_init_msg_list        =>  p_init_msg_list,
8579           p_validation_level     =>  p_validation_level,
8580           x_return_status        =>  x_return_status,
8581           x_msg_count            =>  x_msg_count,
8582           x_msg_data             =>  x_msg_data ,
8583           p_repair_line_id       =>  p_repair_line_id);
8584 
8585      -- Debug messages
8586     Debug('Return Status from  JOB_CREATION_UPDATE :'||x_return_status,l_mod_name,2);
8587 
8588     IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
8589           -- Debug messages
8590           Debug(' JOB_CREATION_UPDATE failed ',l_mod_name,4);
8591           RAISE FND_API.G_EXC_ERROR;
8592     END IF;
8593 
8594     IF NVL(p_upd_job_completion,'Y') = 'Y' THEN
8595       -- Only if the update_job_completion is 'Y', then
8596       -- call the completion update api
8597 
8598       -- Debug messages
8599       Debug('Calling  JOB_COMPLETION_UPDATE',l_mod_name,2);
8600 
8601       JOB_COMPLETION_UPDATE
8602         ( p_api_version          =>  p_api_version,
8603           p_commit               =>  p_commit,
8604           p_init_msg_list        =>  p_init_msg_list,
8605           p_validation_level     =>  p_validation_level,
8606           x_return_status        =>  x_return_status,
8607           x_msg_count            =>  x_msg_count,
8608           x_msg_data             =>  x_msg_data ,
8609           p_repair_line_id       =>  p_repair_line_id);
8610 
8611       -- Debug messages
8612       Debug('Return Status from  JOB_COMPLETION_UPDATE :'||x_return_status,l_mod_name,2);
8613 
8614       IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
8615           -- Debug messages
8616           Debug(' JOB_COMPLETION_UPDATE failed ',l_mod_name,4);
8617           RAISE FND_API.G_EXC_ERROR;
8618       END IF;
8619     END IF;
8620 
8621     -- Standard check of p_commit.
8622     IF FND_API.To_Boolean( p_commit ) THEN
8623         COMMIT WORK;
8624     END IF;
8625 
8626     -- Standard call to get message count and IF count is  get message info.
8627     FND_MSG_PUB.Count_And_Get
8628             (p_count  =>  x_msg_count,
8629              p_data   =>  x_msg_data);
8630   EXCEPTION
8631         WHEN FND_API.G_EXC_ERROR THEN
8632               Debug('In FND_API.G_EXC_ERROR  Exception',l_mod_name,4);
8633               -- As we commit the processed records in the inner APIs
8634 		    -- so we rollback only if the p_commit = 'F'
8635 		    IF NOT(FND_API.To_Boolean( p_commit )) THEN
8636                   ROLLBACK TO WIP_UPDATE;
8637 		    END IF;
8638               x_return_status := FND_API.G_RET_STS_ERROR ;
8639               FND_MSG_PUB.Count_And_Get
8640                     (p_count  =>  x_msg_count,
8641                      p_data   =>  x_msg_data  );
8642         WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8643               Debug('In FND_API.G_EXC_UNEXPECTED_ERROR Exception',l_mod_name,4);
8644               IF ( l_error_level >= G_debug_level)  THEN
8645                  fnd_message.set_name('CSD','CSD_SQL_ERROR');
8646                  fnd_message.set_token('SQLERRM',SQLERRM);
8647                  fnd_message.set_token('SQLCODE',SQLCODE);
8648                  FND_LOG.MESSAGE(l_error_level,l_mod_name,FALSE);
8649               END If;
8650               -- As we commit the processed records in the inner APIs
8651 		    -- so we rollback only if the p_commit = 'F'
8652               IF NOT(FND_API.To_Boolean( p_commit )) THEN
8653                   ROLLBACK TO WIP_UPDATE;
8654 		    END IF;
8655               x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
8656               FND_MSG_PUB.Count_And_Get
8657                     ( p_count  =>  x_msg_count,
8658                       p_data   =>  x_msg_data );
8659         WHEN OTHERS THEN
8660               Debug('In OTHERS Exception',l_mod_name,4);
8661               IF ( l_error_level >= G_debug_level)  THEN
8662                  fnd_message.set_name('CSD','CSD_SQL_ERROR');
8663                  fnd_message.set_token('SQLERRM',SQLERRM);
8664                  fnd_message.set_token('SQLCODE',SQLCODE);
8665                  FND_LOG.MESSAGE(l_error_level,l_mod_name,FALSE);
8666               END If;
8667               -- As we commit the processed records in the inner APIs
8668 		    -- so we rollback only if the p_commit = 'F'
8669               IF NOT(FND_API.To_Boolean( p_commit )) THEN
8670                   ROLLBACK TO WIP_UPDATE;
8671 		    END IF;
8672               x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
8673               IF  FND_MSG_PUB.Check_Msg_Level
8674                       (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
8675               THEN
8676                   FND_MSG_PUB.Add_Exc_Msg
8677                       (G_PKG_NAME ,
8678                        l_api_name  );
8679               END IF;
8680               FND_MSG_PUB.Count_And_Get
8681                       (p_count  =>  x_msg_count,
8682                        p_data   =>  x_msg_data );
8683 End WIP_UPDATE;
8684 
8685 /*-------------------------------------------------------------------------------------*/
8686 /* Procedure name: RECEIPTS_UPDATE_CONC_PROG                                           */
8687 /* Description  : Procedure called by concurrent program to update the depot tables    */
8688 /*                for the receipts against RMA/Internal Requisitions                   */
8689 /*                                                                                     */
8690 /* Called from   : Called from Receipt update concurrent program                       */
8691 /* STANDARD PARAMETERS                                                                 */
8692 /*  In Parameters :                                                                    */
8693 /*                                                                                     */
8694 /*  Output Parameters:                                                                 */
8695 /*   errbuf              VARCHAR2      Error message                                   */
8696 /*   retcode             VARCHAR2      Error Code                                      */
8697 /*                                                                                     */
8698 /* NON-STANDARD PARAMETERS                                                             */
8699 /*   In Parameters                                                                     */
8700 /*    p_order_type       VARCHAR2 Required   Order Type; Possible values- 'I','E'      */
8701 /*    p_order_header_id  NUMBER   Optional   Internal sales Order Id                   */
8702 /*    p_repair_line_id   NUMBER   Optional   Repair Order Line Id                      */
8703 /* Output Parm :                                                                       */
8704 /* Change Hist :                                                                       */
8705 /*   09/20/03  vlakaman  Initial Creation.                                             */
8706 /*-------------------------------------------------------------------------------------*/
8707 
8708 Procedure  RECEIPTS_UPDATE_CONC_PROG
8709            (errbuf              OUT NOCOPY    varchar2,
8710             retcode             OUT NOCOPY    varchar2,
8711             p_order_type        IN            varchar2,
8712             p_order_header_id   IN            number,
8713             p_repair_line_id    IN            number,
8714             p_past_num_of_days  IN            NUMBER  DEFAULT NULL)  ----bug#6753684, 6742512
8715 IS
8716 
8717   --Standard Variables
8718   l_api_name          CONSTANT VARCHAR2(30)   := 'RECEIPTS_UPDATE_CONC_PROG';
8719   l_api_version       CONSTANT NUMBER         := 1.0;
8720 
8721   -- Variable used in the API
8722   l_internal_order_flag  VARCHAR2(1);
8723 
8724   -- Concurrent Program return status
8725   l_success_status    CONSTANT VARCHAR2(1) := '0';
8726   l_warning_status    CONSTANT VARCHAR2(1) := '1';
8727   l_error_status      CONSTANT VARCHAR2(1) := '2';
8728 
8729   -- Variables used in the program
8730   l_return_status     VARCHAR2(30);
8731   l_msg_count         NUMBER;
8732   l_msg_data          VARCHAR2(20000);
8733 
8734   -- Variables for FND Log
8735   l_error_level  number   := FND_LOG.LEVEL_ERROR;
8736   l_mod_name     varchar2(2000) := 'csd.plsql.csd_update_programs_pvt.receipts_update_conc_prog';
8737 
8738 BEGIN
8739       -- Initialize API return status to success
8740       l_return_status := FND_API.G_RET_STS_SUCCESS;
8741 
8742       -- Initialize the error code and error buffer
8743       retcode := l_success_status;
8744       errbuf  := '';
8745 
8746       -- Debug messages
8747       Debug('At the Beginning of receipts_update_conc_prog',l_mod_name,1);
8748 
8749       IF NVL(p_order_type,'E') = 'I' THEN
8750            l_internal_order_flag := 'Y';
8751       Else
8752            l_internal_order_flag := 'N';
8753       End if;
8754 
8755       -- Debug messages
8756       Debug('Calling RECEIPTS_UPDATE',l_mod_name,2);
8757 
8758       -- Api body starts
8759       RECEIPTS_UPDATE
8760         ( p_api_version          =>  l_api_version,
8761           p_commit               =>  FND_API.G_TRUE,
8762           p_init_msg_list        =>  FND_API.G_TRUE,
8763           p_validation_level     =>  FND_API.G_VALID_LEVEL_FULL,
8764           x_return_status        =>  l_return_status,
8765           x_msg_count            =>  l_msg_count,
8766           x_msg_data             =>  l_msg_data,
8767           p_internal_order_flag  =>  l_internal_order_flag,
8768           p_order_header_id      =>  p_order_header_id,
8769           p_repair_line_id       =>  p_repair_line_id,
8770           p_past_num_of_days     =>  p_past_num_of_days);
8771 
8772       -- Debug messages
8773       Debug('Return Status from RECEIPTS_UPDATE :'||l_return_status,l_mod_name,2);
8774 
8775       IF NOT(l_return_status = FND_API.G_RET_STS_SUCCESS) THEN
8776           -- Concatenate the message from the message stack
8777           IF l_msg_count > 1 then
8778             FOR i IN 1..l_msg_count LOOP
8779                 l_msg_data := l_msg_data||FND_MSG_PUB.Get(i,FND_API.G_FALSE) ;
8780             END LOOP ;
8781           END IF ;
8782           Debug(l_msg_data,l_mod_name,4);
8783           RAISE FND_API.G_EXC_ERROR;
8784       END IF;
8785 Exception
8786    WHEN FND_API.G_EXC_ERROR THEN
8787        retcode := l_error_status;
8788        errbuf  := l_msg_data;
8789    WHEN Others then
8790        -- Handle others exception
8791        retcode := l_error_status;
8792        errbuf  := l_msg_data;
8793 END RECEIPTS_UPDATE_CONC_PROG;
8794 
8795 /*-------------------------------------------------------------------------------------*/
8796 /* Procedure name: WIP_UPDATE_CONC_PROG                                                */
8797 /* Description  : Procedure called by concurrent program to update the depot tables    */
8798 /*                for the WIP Job Creation/ Completion                                 */
8799 /*                                                                                     */
8800 /* Called from   : Called from Wip Update Concurrent Program                           */
8801 /* STANDARD PARAMETERS                                                                 */
8802 /*  In Parameters :                                                                    */
8803 /*                                                                                     */
8804 /*  Output Parameters:                                                                 */
8805 /*   errbuf              VARCHAR2      Error message                                   */
8806 /*   retcode             VARCHAR2      Error Code                                      */
8807 /*                                                                                     */
8808 /* NON-STANDARD PARAMETERS                                                             */
8809 /*   In Parameters                                                                     */
8810 /*    p_upd_job_completion        Required   Order Type; Possible values -'Y','N'      */
8811 /*    p_repair_line_id   NUMBER   Optional   Repair Order Line Id                      */
8812 /* Output Parm :                                                                       */
8813 /* Change Hist :                                                                       */
8814 /*   09/20/03  vlakaman  Initial Creation.                                             */
8815 /*-------------------------------------------------------------------------------------*/
8816 
8817 Procedure  WIP_UPDATE_CONC_PROG
8818            (errbuf             OUT NOCOPY    varchar2,
8819             retcode            OUT NOCOPY    varchar2,
8820             p_repair_line_id   IN            number,
8821             p_upd_job_completion IN          varchar2) IS
8822 
8823   -- Standard variables
8824   l_api_name          CONSTANT VARCHAR2(30)   := 'WIP_UPDATE_CONC_PROG';
8825   l_api_version       CONSTANT NUMBER         := 1.0;
8826 
8827   -- Variables used in the program
8828   l_return_status     VARCHAR2(30);
8829   l_msg_count         NUMBER;
8830   l_msg_data          VARCHAR2(20000);
8831 
8832   -- Concurrent Program return status
8833   l_success_status    CONSTANT VARCHAR2(1) := '0';
8834   l_warning_status    CONSTANT VARCHAR2(1) := '1';
8835   l_error_status      CONSTANT VARCHAR2(1) := '2';
8836 
8837   -- Variables for FND log
8838   l_error_level  number   := FND_LOG.LEVEL_ERROR;
8839   l_mod_name     varchar2(2000) := 'csd.plsql.csd_update_programs_pvt.wip_update_conc_prog';
8840 
8841 BEGIN
8842       -- Initialize API return status to success
8843       l_return_status := FND_API.G_RET_STS_SUCCESS;
8844 
8845       -- Initialize the error buffer and error code
8846       retcode := l_success_status;
8847       errbuf  := '';
8848 
8849       -- Debug Statement
8850       Debug('At the Beginning of wip_update_conc_prog',l_mod_name,1);
8851       Debug('p_repair_line_id ='||p_repair_line_id,l_mod_name,1);
8852 
8853       -- Api body starts
8854       -- Debug statement
8855       Debug('Calling WIP_UPDATE',l_mod_name,2);
8856 
8857       -- Call wip update api. If repair line Id is passed,
8858       -- then send the repair line Id. Else it will take all the
8859       -- unprocessed repair orders
8860       WIP_UPDATE
8861         ( p_api_version          =>  l_api_version,
8862           p_commit               =>  FND_API.G_TRUE,
8863           p_init_msg_list        =>  FND_API.G_TRUE,
8864           p_validation_level     =>  FND_API.G_VALID_LEVEL_FULL,
8865           x_return_status        =>  l_return_status,
8866           x_msg_count            =>  l_msg_count,
8867           x_msg_data             =>  l_msg_data,
8868           p_upd_job_completion   =>  p_upd_job_completion,
8869           p_repair_line_id       =>  p_repair_line_id);
8870 
8871      -- Debug statement
8872      Debug('Return Status from WIP_UPDATE :'||l_return_status,l_mod_name,2);
8873 
8874      -- Raise error message if the API returns status <> 'S'
8875      IF NOT(l_return_status = FND_API.G_RET_STS_SUCCESS) THEN
8876          IF l_msg_count > 1 then
8877             FOR i IN 1..l_msg_count LOOP
8878                l_msg_data := l_msg_data||FND_MSG_PUB.Get(i,FND_API.G_FALSE) ;
8879             END LOOP ;
8880          END IF ;
8881          Debug(l_msg_data,l_mod_name,4);
8882          RAISE FND_API.G_EXC_ERROR;
8883      END IF;
8884 
8885 Exception
8886    WHEN FND_API.G_EXC_ERROR THEN
8887        retcode := l_error_status;
8888        errbuf  := l_msg_data;
8889    WHEN Others then
8890        -- Handle others exception
8891        retcode := l_error_status;
8892        errbuf  := l_msg_data;
8893 END WIP_UPDATE_CONC_PROG;
8894 
8895 /*-------------------------------------------------------------------------------------*/
8896 /* Procedure name: SHIP_UPDATE_CONC_PROG                                               */
8897 /* Description  : Procedure called by concurrent program to update the depot tables    */
8898 /*                for the shipment against Sales order/Internal Sales Order            */
8899 /*                                                                                     */
8900 /* Called from   : Called from Receipt update concurrent program                       */
8901 /* STANDARD PARAMETERS                                                                 */
8902 /*  In Parameters :                                                                    */
8903 /*                                                                                     */
8904 /*  Output Parameters:                                                                 */
8905 /*   errbuf              VARCHAR2      Error message                                   */
8906 /*   retcode             VARCHAR2      Eeror Code                                      */
8907 /*                                                                                     */
8908 /* NON-STANDARD PARAMETERS                                                             */
8909 /*   In Parameters                                                                     */
8910 /*    p_order_type       VARCHAR2 Required   Order Type; Possible values- 'I','E'      */
8911 /*    p_order_header_id  NUMBER   Optional   Internal sales Order Id                   */
8912 /*    p_repair_line_id   NUMBER   Optional   Repair Order Line Id                      */
8913 /* Output Parm :                                                                       */
8914 /* Change Hist :                                                                       */
8915 /*   09/20/03  vlakaman  Initial Creation.                                             */
8916 /*-------------------------------------------------------------------------------------*/
8917 
8918 PROCEDURE SHIP_UPDATE_CONC_PROG
8919          (errbuf            OUT NOCOPY  VARCHAR2,
8920           retcode           OUT NOCOPY  VARCHAR2,
8921           p_order_type      IN          VARCHAR2,
8922           p_order_header_id IN          NUMBER,
8923           p_repair_line_id  IN          NUMBER,
8924           p_past_num_of_days  IN        NUMBER DEFAULT NULL)   ----bug#6753684, 6742512
8925 IS
8926 
8927   -- Standard Variables
8928   l_api_version  CONSTANT NUMBER := 1.0;
8929   l_return_status varchar2(1);
8930   l_msg_count     number;
8931   l_msg_data      varchar2(20000);
8932 
8933   -- Variables used in API
8934   l_internal_order_flag  varchar2(1);
8935 
8936     -- Concurrent Program return status
8937   l_success_status    CONSTANT VARCHAR2(1) := '0';
8938   l_warning_status    CONSTANT VARCHAR2(1) := '1';
8939   l_error_status      CONSTANT VARCHAR2(1) := '2';
8940 
8941   -- Variables for the FND Log
8942   l_error_level  number   := FND_LOG.LEVEL_ERROR;
8943   l_mod_name     varchar2(2000) := 'csd.plsql.csd_update_programs_pvt.ship_update_conc_prog';
8944 
8945 Begin
8946       -- Initialize API return status to success
8947       l_return_status := FND_API.G_RET_STS_SUCCESS;
8948 
8949       -- Initialize the error message and error buffer
8950       retcode := l_success_status;
8951       errbuf  := '';
8952 
8953       -- Debug message
8954       Debug('Beginning of ship_update_conc_prog ',l_mod_name,1);
8955 
8956       -- Api body starts
8957       IF NVL(p_order_type,'E') = 'I' THEN
8958           l_internal_order_flag := 'Y';
8959       Else
8960           l_internal_order_flag := 'N';
8961       End if;
8962 
8963       -- Debug message
8964       Debug('Calling SHIP_UPDATE ',l_mod_name,2);
8965 
8966       SHIP_UPDATE
8967         ( p_api_version          =>  l_api_version,
8968           p_commit               =>  FND_API.G_TRUE,
8969           p_init_msg_list        =>  FND_API.G_TRUE,
8970           p_validation_level     =>  FND_API.G_VALID_LEVEL_FULL,
8971           x_return_status        =>  l_return_status,
8972           x_msg_count            =>  l_msg_count,
8973           x_msg_data             =>  l_msg_data,
8974           p_internal_order_flag  =>  l_internal_order_flag,
8975           p_order_header_id      =>  p_order_header_id,
8976           p_repair_line_id       =>  p_repair_line_id,
8977           p_past_num_of_days     =>  p_past_num_of_days);
8978 
8979       -- Debug message
8980       Debug('Return Status from SHIP_UPDATE :'||l_return_status,l_mod_name,2);
8981 
8982       IF NOT(l_return_status = FND_API.G_RET_STS_SUCCESS) THEN
8983          -- Concatenate the message from the message stack
8984          IF l_msg_count > 1 then
8985             FOR i IN 1..l_msg_count LOOP
8986                l_msg_data := l_msg_data||FND_MSG_PUB.Get(i,FND_API.G_FALSE) ;
8987             END LOOP ;
8988          END IF ;
8989          Debug(l_msg_data,l_mod_name,4);
8990          RAISE FND_API.G_EXC_ERROR ;
8991       END IF;
8992 Exception
8993    WHEN FND_API.G_EXC_ERROR THEN
8994        retcode := l_error_status;
8995        errbuf  := l_msg_data;
8996    WHEN Others then
8997        -- Handle others exception
8998        retcode := l_error_status;
8999        errbuf  := l_msg_data;
9000 End SHIP_UPDATE_CONC_PROG;
9001 
9002 
9003 /*-------------------------------------------------------------------------------------*/
9004 /* Procedure name: TASK_UPDATE_CONC_PROG                                               */
9005 /* Description  : Procedure called by concurrent program to update the depot tables    */
9006 /*                for the task creation and update                                     */
9007 /*                                                                                     */
9008 /*                                                                                     */
9009 /* Called from   : Called from Task Update concurrent program                          */
9010 /* STANDARD PARAMETERS                                                                 */
9011 /*  In Parameters :                                                                    */
9012 /*                                                                                     */
9013 /*  Output Parameters:                                                                 */
9014 /*   errbuf              VARCHAR2      Error message                                   */
9015 /*   retcode             VARCHAR2      Error Code                                      */
9016 /*                                                                                     */
9017 /* NON-STANDARD PARAMETERS                                                             */
9018 /*   In Parameters                                                                     */
9019 /*    p_repair_line_id   NUMBER   Optional   Repair Order Line Id                      */
9020 /* Output Parm :                                                                       */
9021 /* Change Hist :                                                                       */
9022 /*   09/20/03  vlakaman  Initial Creation.                                             */
9023 /*-------------------------------------------------------------------------------------*/
9024 
9025 PROCEDURE  TASK_UPDATE_CONC_PROG
9026       ( errbuf                  OUT NOCOPY    varchar2,
9027         retcode                 OUT NOCOPY    varchar2,
9028         p_repair_line_id        IN            number ) is
9029 
9030        CURSOR  c_updated_tasks( p_repair_line_id in number ) is
9031        select  tsk.task_id
9032                ,rep.repair_line_id
9033                --,max(hist.repair_history_id) repair_history_id
9034          from  csd_repair_tasks_v tsk
9035               ,csd_repair_history hist
9036               ,csd_repairs rep
9037         where  rep.repair_line_id = tsk.source_object_id
9038           and tsk.source_object_id = hist.repair_line_id
9039          and tsk.task_id = hist.paramn1
9040           and ( tsk.task_status_id <> hist.paramn5 or tsk.owner_id <> hist.paramn3)
9041         and  rep.repair_line_id = nvl(p_repair_line_id, rep.repair_line_id)  -- travi 181201 change
9042        group by tsk.task_id, rep.repair_line_id;
9043 
9044        CURSOR  c_tasks_to_updt(l_task_id number, l_repair_line_id in number, l_rep_hist_id in number) is
9045        Select  tsk.task_id,            -- hist.paramn1
9046                tsk.last_updated_by,    -- hist.paramn2
9047                tsk.owner_id,           -- hist.paramn3
9048                tsk.assigned_by_id,        -- hist.paramn4
9049                tsk.task_status_id,     -- hist.paramn5
9050                tsk.task_number,        -- hist.paramc1
9051                tsk.owner_type,         -- hist.paramc2
9052                tsk.owner,              -- hist.paramc3
9053                null assignee_type,      -- hist.paramc4
9054                null assignee_name,      -- hist.paramc5
9055                tsk.task_status,        -- hist.paramc6
9056                tsk.planned_start_date, -- hist.paramd1
9057                tsk.actual_start_date,  -- hist.paramd2
9058                tsk.actual_end_date,    -- hist.paramd3
9059                tsk.last_update_date,   -- hist.paramd4
9060                hist.paramc3,           -- tsk.owner
9061                hist.paramc6            -- tsk.task_status
9062          from  CSD_REPAIR_TASKS_V tsk
9063               ,csd_repair_history hist
9064         where  tsk.source_object_type_code = 'DR'
9065           and  tsk.task_id                 = l_task_id
9066           and  tsk.source_object_id        = l_repair_line_id
9067           and  hist.repair_history_id      = l_rep_hist_id
9068           and  hist.paramn1                = tsk.task_id
9069           and  hist.repair_line_id         = tsk.source_object_id
9070           and  (tsk.task_status_id <> hist.paramn5 or tsk.owner_id <> hist.paramn3);
9071 
9072       -- Standard Variables
9073       l_api_name               CONSTANT VARCHAR2(30)   := 'VALIDATE_AND_WRITE';
9074       l_api_version            CONSTANT NUMBER         := 1.0;
9075       l_msg_count              NUMBER;
9076       l_msg_data               VARCHAR2(2000);
9077       l_msg_index              NUMBER;
9078       x_return_status          VARCHAR2(1);
9079       x_msg_count              NUMBER;
9080       x_msg_data               VARCHAR2(2000);
9081 
9082       -- Variables used in program
9083       l_return_status          VARCHAR2(1);
9084       l_repair_history_id      NUMBER;
9085       v_total_records          number;
9086       l_event_code             VARCHAR2(30) := '';
9087       l_task_id                number;
9088       l_repair_line_id         number;
9089       l_rep_hist_id            number;
9090       l_paramn1                NUMBER;
9091       l_paramn2                NUMBER;
9092       l_paramn3                NUMBER;
9093       l_paramn4                NUMBER;
9094       l_paramn5                NUMBER;
9095       l_paramc1                VARCHAR2(240);
9096       l_paramc2                VARCHAR2(240);
9097       l_paramc3                VARCHAR2(240);
9098       l_paramc4                VARCHAR2(240);
9099       l_paramc5                VARCHAR2(240);
9100       l_paramc6                VARCHAR2(240);
9101       l_paramd1                DATE;
9102       l_paramd2                DATE;
9103       l_paramd3                DATE;
9104       l_paramd4                DATE;
9105       l_owner                  VARCHAR2(240);
9106       l_task_status            VARCHAR2(240);
9107 
9108       -- Variables for FND Log
9109       l_error_level  number   := FND_LOG.LEVEL_ERROR;
9110       l_mod_name     varchar2(2000) := 'csd.plsql.csd_update_programs_pvt.task_update_conc_prog';
9111 
9112  BEGIN
9113 
9114  -- Initialize API return status to success
9115  x_return_status := FND_API.G_RET_STS_SUCCESS;
9116 
9117  -- Initialize the error message and error buffer
9118  retcode := '0';
9119  errbuf  := '';
9120 
9121  v_total_records := 0;
9122 
9123  -- travi added p_repair_line_id
9124  FOR R in c_updated_tasks( p_repair_line_id )
9125  loop
9126 
9127     l_event_code := '';
9128     l_task_id        := '';
9129     l_repair_line_id := '';
9130     l_rep_hist_id    := '';
9131     l_paramn1        := ''; -- task id
9132     l_paramn2        := ''; -- last updated by
9133     l_paramn3        := ''; -- owner id
9134     l_paramn4        := ''; -- assigned by id
9135     l_paramn5        := ''; -- status id
9136     l_paramc1        := ''; -- task number
9137     l_paramc2        := ''; -- owner type
9138     l_paramc3        := ''; -- owner name
9139     l_paramc4        := ''; -- null assignee type
9140     l_paramc5        := ''; -- null assignee name
9141     l_paramc6        := ''; -- status
9142     l_paramd1        := ''; -- planned start date
9143     l_paramd2        := ''; -- actual start date
9144     l_paramd3        := ''; -- actual end date
9145     l_paramd4        := ''; -- last updated date
9146     l_owner          := ''; -- tsk.owner
9147     l_task_status    := ''; -- tsk.task_status
9148 
9149      select max(hist2.repair_history_id)
9150      into l_rep_hist_id
9151      from CSD_REPAIR_HISTORY hist2
9152      where hist2.repair_line_id = R.repair_line_id
9153      and hist2.paramn1          = R.task_id;
9154 
9155      l_task_id        := R.task_id;
9156      l_repair_line_id := R.repair_line_id;
9157 
9158      IF (l_rep_hist_id is not null) then
9159 
9160          OPEN c_tasks_to_updt(l_task_id, l_repair_line_id, l_rep_hist_id);
9161          FETCH c_tasks_to_updt
9162           INTO l_paramn1, -- task id
9163                l_paramn2, -- last updated by
9164                l_paramn3, -- owner id
9165                l_paramn4, -- assigned by id
9166                l_paramn5, -- status id
9167                l_paramc1, -- task number
9168                l_paramc2, -- owner type
9169                l_paramc3, -- owner name
9170                l_paramc4, -- null assignee type
9171                l_paramc5, -- null assignee name
9172                l_paramc6, -- status
9173                l_paramd1, -- planned start date
9174                l_paramd2, -- actual start date
9175                l_paramd3, -- actual end date
9176                l_paramd4, -- last updated date
9177                l_owner,   -- tsk.owner
9178                l_task_status;  -- -- tsk.task_status
9179 
9180          CLOSE c_tasks_to_updt;
9181 
9182          if (l_task_status <> l_paramc6) then
9183              l_event_code := 'TSC';
9184          elsif (l_owner <> l_paramc3) then
9185              l_event_code := 'TOC';
9186          end if;
9187 
9188       -- ---------------------------------------------------------
9189       -- Repair history row inserted for TOC or TSC only
9190       -- ---------------------------------------------------------
9191       IF (l_event_code in ('TOC', 'TSC')) then
9192 
9193       -- --------------------------------
9194       -- Begin Update repair task history
9195       -- --------------------------------
9196       -- Standard Start of API savepoint
9197          SAVEPOINT  Update_rep_task_hist;
9198 
9199       -- ---------------
9200       -- Api body starts
9201       -- ---------------
9202          Debug('Calling Validate_And_Write ',l_mod_name,2);
9203 
9204          CSD_TO_FORM_REPAIR_HISTORY.Validate_And_Write
9205           (p_Api_Version_Number       => l_api_version ,
9206            p_init_msg_list            => FND_API.G_FALSE,
9207            p_commit                   => FND_API.G_FALSE,
9208            p_validation_level         => NULL,
9209            p_action_code              => 0,
9210            px_REPAIR_HISTORY_ID       => l_repair_history_id,
9211            p_OBJECT_VERSION_NUMBER    => null,                     -- travi ovn validation
9212            p_REQUEST_ID               => null,
9213            p_PROGRAM_ID               => null,
9214            p_PROGRAM_APPLICATION_ID   => null,
9215            p_PROGRAM_UPDATE_DATE      => null,
9216            p_CREATED_BY               => FND_GLOBAL.USER_ID,
9217            p_CREATION_DATE            => sysdate,
9218            p_LAST_UPDATED_BY          => FND_GLOBAL.USER_ID,
9219            p_LAST_UPDATE_DATE         => sysdate,
9220            p_repair_line_id           => l_repair_line_id,
9221            p_EVENT_CODE               => l_event_code,
9222            p_EVENT_DATE               => sysdate,
9223            p_QUANTITY                 => null,
9224            p_PARAMN1                  => l_paramn1,
9225            p_PARAMN2                  => l_paramn2,
9226            p_PARAMN3                  => l_paramn3,
9227            p_PARAMN4                  => l_paramn4,
9228            p_PARAMN5                  => l_paramn5,
9229            p_PARAMN6                  => null,
9230            p_PARAMN7                  => null,
9231            p_PARAMN8                  => null,
9232            p_PARAMN9                  => null,
9233            p_PARAMN10                 => FND_GLOBAL.USER_ID,
9234            p_PARAMC1                  => l_paramc1,
9235            p_PARAMC2                  => l_paramc2,
9236            p_PARAMC3                  => l_paramc3,
9237            p_PARAMC4                  => l_paramc4,
9238            p_PARAMC5                  => l_paramc5,
9239            p_PARAMC6                  => l_paramc6,
9240            p_PARAMC7                  => null,
9241            p_PARAMC8                  => null,
9242            p_PARAMC9                  => null,
9243            p_PARAMC10                 => null,
9244            p_PARAMD1                  => l_paramd1,
9245            p_PARAMD2                  => l_paramd1,
9246            p_PARAMD3                  => l_paramd1,
9247            p_PARAMD4                  => l_paramd1,
9248            p_PARAMD5                  => null,
9249            p_PARAMD6                  => null,
9250            p_PARAMD7                  => null,
9251            p_PARAMD8                  => null,
9252            p_PARAMD9                  => null,
9253            p_PARAMD10                 => null,
9254            p_ATTRIBUTE_CATEGORY       => null,
9255            p_ATTRIBUTE1               => null,
9256            p_ATTRIBUTE2               => null,
9257            p_ATTRIBUTE3               => null,
9258            p_ATTRIBUTE4               => null,
9259            p_ATTRIBUTE5               => null,
9260            p_ATTRIBUTE6               => null,
9261            p_ATTRIBUTE7               => null,
9262            p_ATTRIBUTE8               => null,
9263            p_ATTRIBUTE9               => null,
9264            p_ATTRIBUTE10              => null,
9265            p_ATTRIBUTE11              => null,
9266            p_ATTRIBUTE12              => null,
9267            p_ATTRIBUTE13              => null,
9268            p_ATTRIBUTE14              => null,
9269            p_ATTRIBUTE15              => null,
9270            p_LAST_UPDATE_LOGIN        => FND_GLOBAL.CONC_LOGIN_ID,
9271            X_Return_Status            => x_return_status,
9272            X_Msg_Count                => x_msg_count,
9273            X_Msg_Data                 => x_msg_data
9274           );
9275 
9276         Debug('Return Status from Validate_And_Write :'||x_return_status,l_mod_name,2);
9277         IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
9278             -- Debug messages
9279             Debug(' JOB_COMPLETION_UPDATE failed ',l_mod_name,4);
9280             RAISE FND_API.G_EXC_ERROR;
9281         END IF;
9282 
9283         -- -------------------
9284         -- Api body ends here
9285         -- -------------------
9286         v_total_records := v_total_records + 1;
9287      end if; -- End of TOC/TSC check
9288 
9289     end if; -- End of check for l_rep_hist_id
9290   end loop;
9291 
9292   -- Standard check of p_commit.
9293   IF FND_API.To_Boolean( FND_API.G_FALSE ) THEN
9294        COMMIT WORK;
9295   END IF;
9296 
9297   -- Standard call to get message count and IF count is  get message info.
9298   FND_MSG_PUB.Count_And_Get
9299          (p_count  =>  x_msg_count,
9300           p_data   =>  x_msg_data );
9301 
9302  EXCEPTION
9303       WHEN FND_API.G_EXC_ERROR THEN
9304           ROLLBACK TO Update_rep_task_hist;
9305             x_return_status := FND_API.G_RET_STS_ERROR ;
9306             FND_MSG_PUB.Count_And_Get
9307                 (p_count  =>  x_msg_count,
9308                  p_data   =>  x_msg_data  );
9309       WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
9310           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
9311           ROLLBACK TO Update_rep_task_hist;
9312           FND_MSG_PUB.Count_And_Get
9313                 ( p_count  =>  x_msg_count,
9314                   p_data   =>  x_msg_data  );
9315       WHEN OTHERS THEN
9316           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
9317           ROLLBACK TO Update_rep_task_hist;
9318               IF  FND_MSG_PUB.Check_Msg_Level
9319                   (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
9320               THEN
9321                   FND_MSG_PUB.Add_Exc_Msg
9322                   (G_PKG_NAME ,
9323                    l_api_name  );
9324               END IF;
9325                   FND_MSG_PUB.Count_And_Get
9326                   (p_count  =>  x_msg_count,
9327                    p_data   =>  x_msg_data );
9328 
9329  END TASK_UPDATE_CONC_PROG;
9330 
9331 
9332  /*-------------------------------------------------------------------------------------*/
9333  /* Procedure name: PROD_TXN_STATUS_UPD                                                 */
9334  /* Description  :  Procedure called to update all the logistics lines status to booked */
9335  /*                                                                                     */
9336  /*                                                                                     */
9337  /* Called from   : csd_process_pvt and CSDREPLN.pld                                    */
9338  /* STANDARD PARAMETERS                                                                 */
9339  /*  In Parameters :                                                                    */
9340  /*                                                                                     */
9341  /*  Output Parameters:                                                                 */
9342  /*                                                                                     */
9343  /* NON-STANDARD PARAMETERS                                                             */
9344  /*   In Parameters                                                                     */
9345  /*    p_repair_line_id   NUMBER                                                        */
9346  /*    p_commit           VARCHAR2                                                      */
9347  /* Output Parm :                                                                       */
9348  /* Change Hist :                                                                       */
9349  /*   12/20/04  mshirkol  Initial Creation. Fix for bug#4020651                         */
9350  /*-------------------------------------------------------------------------------------*/
9351  PROCEDURE  PROD_TXN_STATUS_UPD(p_repair_line_id   in  number,
9352                                 p_commit           in varchar2)
9353 
9354  is
9355 
9356 
9357  CURSOR c_product_transaction_id(p_repair_line_id    IN number) is
9358      SELECT c.product_transaction_id, a.booked_flag
9359      FROM oe_order_lines_all a,
9360           cs_estimate_details b,
9361           csd_product_transactions c
9362      WHERE a.line_id = b.order_line_id
9363      AND b.estimate_detail_id = c.estimate_detail_id
9364      and c.prod_txn_status = 'SUBMITTED'
9365      and a.booked_flag = 'Y'
9366      and c.book_sales_order_flag = 'N'
9367      and b.order_header_id in
9368      (
9369  	 select p.order_header_id
9370  	 from   cs_estimate_details p, csd_product_transactions q
9371  	 where  p.estimate_detail_id=q.estimate_detail_id
9372  	 and    q.repair_line_id=p_repair_line_id
9373       );
9374 
9375  begin
9376 
9377      FOR C in c_product_transaction_id(p_repair_line_id)
9378      Loop
9379          IF C.booked_flag = 'Y' THEN
9380              UPDATE csd_product_transactions
9381              SET prod_txn_status = 'BOOKED', book_sales_order_flag = 'Y'
9382              WHERE product_transaction_id = C.product_transaction_id;
9383          END IF;
9384      end loop;
9385 
9386      IF c_product_transaction_id%isopen then
9387        CLOSE c_product_transaction_id;
9388      END IF;
9389 
9390      if(p_commit = FND_API.G_TRUE) THEN
9391         commit;
9392      END IF;
9393  End;
9394 
9395 
9396  /*-------------------------------------------------------------------------------------*/
9397  /* Procedure name: check_for_cancelled_order                                       */
9398  /* Description  :  Procedure called to update all the logistics lines status to        */
9399  /*                 cancelled if the corresponding order line is cancelled.             */
9400  /*                                                                                     */
9401  /*                                                                                     */
9402  /* Called from   : RMA_RCV_UPDATE, IO_RCV_UPDATE , SHIP_UPDATE, IO_SHIP_UPDATE         */
9403  /* STANDARD PARAMETERS                                                                 */
9404  /*  In Parameters :                                                                    */
9405  /*                                                                                     */
9406  /*  Output Parameters:                                                                 */
9407  /*                                                                                     */
9408  /* NON-STANDARD PARAMETERS                                                             */
9409  /*   In Parameters                                                                     */
9410  /*    p_repair_line_id   NUMBER                                                        */
9411  /* Output Parm :                                                                       */
9412  /* Change Hist :                                                                       */
9413  /*   30/June/2005  vparvath  Initial Creation.                                        */
9414  /*-------------------------------------------------------------------------------------*/
9415  PROCEDURE check_for_cancelled_order(p_repair_line_id NUMBER) IS
9416 
9417   CURSOR CANCELLED_ORDER_LINES(p_repair_line_id NUMBER) IS
9418   SELECT DISTINCT cpt.product_transaction_id PRODUCT_TXN_ID
9419   FROM oe_order_headers_all oeh,
9420       oe_order_lines_all oel,
9421       cs_estimate_details ced,
9422       csd_product_transactions cpt
9423   WHERE cpt.repair_line_id = p_repair_line_id
9424        AND   cpt.action_type    in ('RMA', 'SHIP', 'RMA_THIRD_PTY', 'SHIP_THIRD_PTY')
9425        AND   cpt.prod_txn_status    in (  'BOOKED', 'SUBMITTED')
9426        AND   ced.order_header_id is not null
9427        AND   ced.source_code        = 'DR'
9428        AND   ced.estimate_detail_id = cpt.estimate_detail_id
9429        AND   oeh.header_id          = ced.order_header_id
9430        AND   oel.header_id          = oeh.header_id
9431        and   ced.order_line_id       = oel.line_id
9432        /*fixed for bug#5846050 only cancelled line should be updated not all the lines */
9433        --AND   oel.cancelled_flag     = 'Y'
9434        AND   oel.ordered_quantity     = 0 -- indicates the order line is cancelled.
9435 	  ; -- skip partial ship/receive case, current behavour is
9436 	  -- if the original line is cancelled the product transaction
9437 	  -- will show as cancelled.
9438 	  /***
9439        AND  ((ced.QUANTITY_REQUIRED < -1
9440 	           AND oel.line_id in ( Select line_id
9441                               from oe_order_lines_all oel1
9442              	               start with oel1.line_id = ced.order_line_id
9443      		                 connect by prior oel1.line_id = oel1.split_from_line_id
9444      		                 and oel1.shipped_quantity is not null
9445      		                 and oel1.header_id = oeh.header_id))
9446 			OR (ced.QUANTITY_REQUIRED = -1
9447 			    AND ced.ORDER_LINE_ID = oel.LINE_ID));
9448 	 **********/
9449 
9450   l_product_txn_id NUMBER;
9451   C_PRODTXN_CANCELLED VARCHAR2(30) := 'CANCELLED';
9452 
9453  BEGIN
9454 
9455     FOR ORD_LINES IN CANCELLED_ORDER_LINES( p_repair_line_id) LOOP
9456       l_product_txn_id := ORD_LINES.PRODUCT_TXN_ID;
9457       BEGIN
9458         UPDATE CSD_PRODUCT_TRANSACTIONS
9459         SET PROD_TXN_STATUS = C_PRODTXN_CANCELLED,
9460             LAST_UPDATE_DATE = SYSDATE,
9461             LAST_UPDATE_LOGIN = FND_GLOBAL.LOGIN_ID,
9462             LAST_UPDATED_BY   = FND_GLOBAL.USER_ID
9463         WHERE PRODUCT_TRANSACTION_ID = l_product_txn_id;
9464       EXCEPTION
9465           WHEN OTHERS THEN
9466             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
9467       END;
9468     END LOOP;
9469 
9470  END check_for_cancelled_order;
9471 
9472 
9473 /*-------------------------------------------------------------------------------------*/
9474 /* Procedure name: UPDATE_LOGISTIC_STATUS_WF                                           */
9475 /* Description   : Procedure called from workflow process to update logistics          */
9476 /*                 line status                                                         */
9477 /*                                                                                     */
9478 /* Called from   : Workflow                                                            */
9479 /* PARAMETERS                                                                          */
9480 /*  IN                                                                                 */
9481 /*                                                                                     */
9482 /*   itemtype  - type of the current item                                              */
9483 /*   itemkey   - key of the current item                                               */
9484 /*   actid     - process activity instance id                                          */
9485 /*   funcmode  - function execution mode ('RUN', 'CANCEL', 'TIMEOUT', ...)             */
9486 /*  OUT	                                                                               */
9487 /*   result                                                                            */
9488 /*       - COMPLETE[:<result>]                                                         */
9489 /*           activity has completed with the indicated result                          */
9490 /*       - WAITING                                                                     */
9491 /*           activity is waiting for additional transitions                            */
9492 /*       - DEFERED                                                                     */
9493 /*           execution should be defered to background                                 */
9494 /*       - NOTIFIED[:<notification_id>:<assigned_user>]                                */
9495 /*           activity has notified an external entity that this                        */
9496 /*           step must be performed.  A call to wf_engine.CompleteActivty              */
9497 /*           will signal when this step is complete.  Optional                         */
9498 /*           return of notification ID and assigned user.                              */
9499 /*       - ERROR[:<error_code>]                                                        */
9500 /*           function encountered an error.                                            */
9501 /* Change Hist :                                                                       */
9502 /*   04/18/06  mshirkol  Initial Creation.  ( Fix for bug#5610891 )                    */
9503 /*-------------------------------------------------------------------------------------*/
9504 
9505 Procedure UPDATE_LOGISTIC_STATUS_WF
9506             (itemtype  in varchar2,
9507              itemkey   in varchar2,
9508              actid     in number,
9509              funcmode  in varchar2,
9510              resultout in out nocopy varchar2) IS
9511 
9512 l_line_id               number;
9513 l_orig_source_code      varchar2(10);
9514 l_orig_source_id        number;
9515 l_line_category_code    varchar2(30);
9516 l_order_header_id       number;
9517 l_return_status         varchar2(3);
9518 l_msg_count             number;
9519 l_msg_data              varchar2(2000);
9520 l_msg_index_out         number;
9521 l_module_name           varchar2(30);
9522 
9523 
9524 Cursor get_est_line_details ( p_line_id in number ) is
9525 select
9526   est.original_source_code,
9527   est.original_source_id,
9528   est.line_category_code,
9529   est.order_header_id
9530 from
9531   cs_estimate_details est
9532 where
9533 est.order_line_id = p_line_id;
9534 
9535 BEGIN
9536 
9537   IF ( funcmode = 'RUN' ) THEN
9538 
9539     l_line_id := to_number(itemkey);
9540 
9541     --
9542     -- Derive the wf roles for the Contact id
9543     --
9544     Open get_est_line_details (l_line_id);
9545     Fetch get_est_line_details into l_orig_source_code,
9546       l_orig_source_id,l_line_category_code,l_order_header_id;
9547     Close get_est_line_details;
9548 
9549     IF ( l_orig_source_code = 'DR' ) THEN
9550 
9551       IF ( l_line_category_code = 'RETURN') THEN
9552 
9553         l_module_name := 'LOGISTICS_RECEIPTS_UPDATE';
9554 
9555         CSD_UPDATE_PROGRAMS_PVT.RECEIPTS_UPDATE
9556           ( p_api_version          => 1.0,
9557             p_commit               => 'T',
9558             p_init_msg_list        => 'T',
9559             p_validation_level     => CSD_PROCESS_UTIL.G_VALID_LEVEL_FULL,
9560             x_return_status        => l_return_status,
9561             x_msg_count            => l_msg_count,
9562             x_msg_data             => l_msg_data,
9563             p_internal_order_flag  => 'N',
9564             p_order_header_id      => null,
9565             p_repair_line_id       => l_orig_source_id);
9566 
9567         IF NOT(l_return_status = FND_API.G_RET_STS_SUCCESS) THEN
9568           resultout := 'COMPLETE:ERROR';
9569         ELSE
9570           resultout := 'COMPLETE:SUCCESS';
9571         END IF;
9572 
9573       ELSIF ( l_line_category_code = 'ORDER') THEN
9574 
9575         l_module_name := 'LOGISTICS_SHIP_UPDATE';
9576 
9577         CSD_UPDATE_PROGRAMS_PVT.SHIP_UPDATE
9578           ( p_api_version          => 1.0,
9579             p_commit               => 'T',
9580             p_init_msg_list        => 'T',
9581             p_validation_level     => CSD_PROCESS_UTIL.G_VALID_LEVEL_FULL,
9582             x_return_status        => l_return_status,
9583             x_msg_count            => l_msg_count,
9584             x_msg_data             => l_msg_data,
9585             p_internal_order_flag  => 'N' ,
9586             p_order_header_id      => null,
9587             p_repair_line_id       => l_orig_source_id);
9588 
9589         IF NOT(l_return_status = FND_API.G_RET_STS_SUCCESS) THEN
9590           resultout := 'COMPLETE:ERROR';
9591         ELSE
9592           resultout := 'COMPLETE:SUCCESS';
9593         END IF;
9594 
9595       END IF;
9596 
9597       -- If the return status is error then raise
9598       -- the Business Event
9599       IF NOT(l_return_status = FND_API.G_RET_STS_SUCCESS) THEN
9600 
9601         CSD_REPAIRS_PVT.LAUNCH_WFEXCEPTION_BEVENT(
9602                          p_return_status  => l_return_status,
9603                          p_msg_count      => l_msg_count,
9604                          p_msg_data       => l_msg_data,
9605                          p_repair_line_id => l_orig_source_id,
9606                          p_module_name    => l_module_name);
9607 
9608       END IF;
9609 
9610     END IF;
9611 
9612     return;
9613 
9614   END IF;
9615 
9616 
9617 
9618 EXCEPTION
9619 WHEN OTHERS THEN
9620   WF_CORE.CONTEXT('CSD_UPDATE_PROGRAMS_PVT','UPDATE_LOGISTICS_WF',itemtype,
9621                   itemkey,to_char(actid),funcmode);
9622   raise;
9623 END;
9624 
9625 END CSD_UPDATE_PROGRAMS_PVT;