DBA Data[Home] [Help]

PACKAGE BODY: APPS.INV_RCV_COMMON_APIS

Source


1 PACKAGE BODY inv_rcv_common_apis AS
2   /* $Header: INVRCVCB.pls 120.17.12010000.2 2008/09/30 11:13:46 kkesavar ship $*/
3 
4   --  Global constant holding the package name
5   g_pkg_name CONSTANT VARCHAR2(30) := 'inv_RCV_COMMON_APIS';
6 
7   PROCEDURE print_debug(p_err_msg VARCHAR2, p_level NUMBER) IS
8     l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
9   BEGIN
10     IF (l_debug = 1) THEN
11       inv_mobile_helper_functions.tracelog(p_err_msg => p_err_msg, p_module => 'inv_RCV_COMMON_APIS', p_level => p_level);
12     END IF;
13   END print_debug;
14 
15   PROCEDURE init_startup_values(p_organization_id IN NUMBER) IS
16     l_message VARCHAR2(240);
17     l_debug   NUMBER        := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
18   BEGIN
19     IF (l_debug = 1) THEN
20       print_debug('enter init_startup_values :  10', 1);
21     END IF;
22 
23     -- query po_startup_value
24     init_form_values(
25       p_organization_id
26     , g_po_startup_value.inv_org_id --bug 5195963
27     , g_po_startup_value.org_name
28     , g_po_startup_value.org_location
29     , g_po_startup_value.sob_id
30     , g_po_startup_value.ussgl_value
31     , g_po_startup_value.period_name
32     , g_po_startup_value.gl_date
33     , g_po_startup_value.category_set_id
34     , g_po_startup_value.structure_id
35     , g_po_startup_value.user_id
36     , g_po_startup_value.logon_id
37     , g_po_startup_value.creation_date
38     , g_po_startup_value.update_date
39     , g_po_startup_value.inv_status
40     , g_po_startup_value.po_status
41     , g_po_startup_value.qa_status
42     , g_po_startup_value.wip_status
43     , g_po_startup_value.pa_status
44     , g_po_startup_value.oe_status
45     , g_po_startup_value.override_routing
46     , g_po_startup_value.transaction_mode
47     , g_po_startup_value.receipt_traveller
48     , g_po_startup_value.receipt_num_code
49     , g_po_startup_value.receipt_num_type
50     , g_po_startup_value.po_num_type
51     , g_po_startup_value.coa_id
52     , g_po_startup_value.allow_express
53     , g_po_startup_value.allow_cascade
54     , g_po_startup_value.org_locator_control
55     , g_po_startup_value.negative_inv_receipt_code
56     , g_po_startup_value.gl_set_of_bks_id
57     , g_po_startup_value.blind_receiving_flag
58     , g_po_startup_value.allow_unordered
59     , g_po_startup_value.display_inverse_rate
60     , g_po_startup_value.currency_code
61     , g_po_startup_value.project_reference_enabled
62     , g_po_startup_value.project_control_level
63     , g_po_startup_value.effectivity_control
64     , g_po_startup_value.employee_id
65     , g_po_startup_value.wms_install_status
66     , g_po_startup_value.wms_purchased
67     , l_message
68     );
69 
70     IF (l_debug = 1) THEN
71       print_debug('init_startup_values :  20', 4);
72     END IF;
73 
74     IF g_rcv_global_var.transaction_header_id IS NULL THEN
75       SELECT mtl_material_transactions_s.NEXTVAL
76         INTO g_rcv_global_var.transaction_header_id
77         FROM DUAL;
78     END IF;
79 
80     gen_txn_group_id;
81 
82     IF (l_debug = 1) THEN
83       print_debug('exit init_startup_values :  30', 1);
84     END IF;
85   END init_startup_values;
86 
87   -- Bug 4087032 Need to write a wrapper on LENGTH function as
88   -- it creates compiltaion issues in 8i env.
89   FUNCTION get_serial_length(p_from_ser IN VARCHAR2)
90   return NUMBER is
91   BEGIN
92       return length(p_from_ser);
93   END get_serial_length;
94 
95   -- for testing only   ??   need INV standard api for this
96   FUNCTION get_to_serial_number(p_from_ser VARCHAR2, p_primary_quantity NUMBER)
97     RETURN VARCHAR2 IS
98     l_to_ser      VARCHAR2(30);
99     l_number      NUMBER;
100     l_temp_prefix VARCHAR2(30);
101     l_debug       NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
102   BEGIN
103     inv_validate.number_from_sequence(p_from_ser, l_temp_prefix, l_number);
104     l_number  := l_number + p_primary_quantity - 1;
105     l_to_ser  := SUBSTR(p_from_ser, 1, LENGTH(p_from_ser) - LENGTH(l_number)) || l_number;
106     RETURN l_to_ser;
107   END get_to_serial_number;
108 
109   PROCEDURE insert_mtlt(p_mtlt_rec mtl_transaction_lots_temp%ROWTYPE) IS
110     l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
111   BEGIN
112     INSERT INTO mtl_transaction_lots_temp
113                 (
114                  transaction_temp_id
115                , last_update_date
116                , last_updated_by
117                , creation_date
118                , created_by
119                , last_update_login
120                , request_id
121                , program_application_id
122                , program_id
123                , program_update_date
124                , transaction_quantity
125                , primary_quantity
126                , lot_number
127                , lot_expiration_date
128                , ERROR_CODE
129                , serial_transaction_temp_id
130                , group_header_id
131                , put_away_rule_id
132                , pick_rule_id
133                , description
134                , vendor_id
135                , supplier_lot_number
136                , territory_code
137                , --country_of_origin,
138                  origination_date
139                , date_code
140                , grade_code
141                , change_date
142                , maturity_date
143                , status_id
144                , retest_date
145                , age
146                , item_size
147                , color
148                , volume
149                , volume_uom
150                , place_of_origin
151                , --kill_date,
152                  best_by_date
153                , LENGTH
154                , length_uom
155                , recycled_content
156                , thickness
157                , thickness_uom
158                , width
159                , width_uom
160                , curl_wrinkle_fold
161                , lot_attribute_category
162                , c_attribute1
163                , c_attribute2
164                , c_attribute3
165                , c_attribute4
166                , c_attribute5
167                , c_attribute6
168                , c_attribute7
169                , c_attribute8
170                , c_attribute9
171                , c_attribute10
172                , c_attribute11
173                , c_attribute12
174                , c_attribute13
175                , c_attribute14
176                , c_attribute15
177                , c_attribute16
178                , c_attribute17
179                , c_attribute18
180                , c_attribute19
181                , c_attribute20
182                , d_attribute1
183                , d_attribute2
184                , d_attribute3
185                , d_attribute4
186                , d_attribute5
187                , d_attribute6
188                , d_attribute7
189                , d_attribute8
190                , d_attribute9
191                , d_attribute10
192                , n_attribute1
193                , n_attribute2
194                , n_attribute3
195                , n_attribute4
196                , n_attribute5
197                , n_attribute6
198                , n_attribute7
199                , n_attribute8
200                , n_attribute9
201                , n_attribute10
202                , vendor_name
203                , SECONDARY_QUANTITY --OPM Convergence
204                , SECONDARY_UNIT_OF_MEASURE --OPM Convergence
205                 )
206          VALUES (
207                  p_mtlt_rec.transaction_temp_id
208                , p_mtlt_rec.last_update_date
209                , p_mtlt_rec.last_updated_by
210                , p_mtlt_rec.creation_date
211                , p_mtlt_rec.created_by
212                , p_mtlt_rec.last_update_login
213                , p_mtlt_rec.request_id
214                , p_mtlt_rec.program_application_id
215                , p_mtlt_rec.program_id
216                , p_mtlt_rec.program_update_date
217                , p_mtlt_rec.transaction_quantity
218                , p_mtlt_rec.primary_quantity
219                , p_mtlt_rec.lot_number
220                , p_mtlt_rec.lot_expiration_date
221                , p_mtlt_rec.ERROR_CODE
222                , p_mtlt_rec.serial_transaction_temp_id
223                , p_mtlt_rec.group_header_id
224                , p_mtlt_rec.put_away_rule_id
225                , p_mtlt_rec.pick_rule_id
226                , p_mtlt_rec.description
227                , p_mtlt_rec.vendor_id
228                , p_mtlt_rec.supplier_lot_number
229                , p_mtlt_rec.territory_code
230                , --p_mtlt_rec.country_of_origin,
231                  p_mtlt_rec.origination_date
232                , p_mtlt_rec.date_code
233                , p_mtlt_rec.grade_code
234                , p_mtlt_rec.change_date
235                , p_mtlt_rec.maturity_date
236                , p_mtlt_rec.status_id
237                , p_mtlt_rec.retest_date
238                , p_mtlt_rec.age
239                , p_mtlt_rec.item_size
240                , p_mtlt_rec.color
241                , p_mtlt_rec.volume
242                , p_mtlt_rec.volume_uom
243                , p_mtlt_rec.place_of_origin
244                , --p_mtlt_rec.kill_date,
245                  p_mtlt_rec.best_by_date
246                , p_mtlt_rec.LENGTH
247                , p_mtlt_rec.length_uom
248                , p_mtlt_rec.recycled_content
249                , p_mtlt_rec.thickness
250                , p_mtlt_rec.thickness_uom
251                , p_mtlt_rec.width
252                , p_mtlt_rec.width_uom
253                , p_mtlt_rec.curl_wrinkle_fold
254                , p_mtlt_rec.lot_attribute_category
255                , p_mtlt_rec.c_attribute1
256                , p_mtlt_rec.c_attribute2
257                , p_mtlt_rec.c_attribute3
258                , p_mtlt_rec.c_attribute4
259                , p_mtlt_rec.c_attribute5
260                , p_mtlt_rec.c_attribute6
261                , p_mtlt_rec.c_attribute7
262                , p_mtlt_rec.c_attribute8
263                , p_mtlt_rec.c_attribute9
264                , p_mtlt_rec.c_attribute10
265                , p_mtlt_rec.c_attribute11
266                , p_mtlt_rec.c_attribute12
267                , p_mtlt_rec.c_attribute13
268                , p_mtlt_rec.c_attribute14
269                , p_mtlt_rec.c_attribute15
270                , p_mtlt_rec.c_attribute16
271                , p_mtlt_rec.c_attribute17
272                , p_mtlt_rec.c_attribute18
273                , p_mtlt_rec.c_attribute19
274                , p_mtlt_rec.c_attribute20
275                , p_mtlt_rec.d_attribute1
276                , p_mtlt_rec.d_attribute2
277                , p_mtlt_rec.d_attribute3
278                , p_mtlt_rec.d_attribute4
279                , p_mtlt_rec.d_attribute5
280                , p_mtlt_rec.d_attribute6
281                , p_mtlt_rec.d_attribute7
282                , p_mtlt_rec.d_attribute8
283                , p_mtlt_rec.d_attribute9
284                , p_mtlt_rec.d_attribute10
285                , p_mtlt_rec.n_attribute1
286                , p_mtlt_rec.n_attribute2
287                , p_mtlt_rec.n_attribute3
288                , p_mtlt_rec.n_attribute4
289                , p_mtlt_rec.n_attribute5
290                , p_mtlt_rec.n_attribute6
291                , p_mtlt_rec.n_attribute7
292                , p_mtlt_rec.n_attribute8
293                , p_mtlt_rec.n_attribute9
294                , p_mtlt_rec.n_attribute10
295                , p_mtlt_rec.vendor_name
296                , p_mtlt_rec.SECONDARY_QUANTITY --OPM Convergence
297                , p_mtlt_rec.SECONDARY_UNIT_OF_MEASURE --OPM Convergence
298 );
299   END insert_mtlt;
300 
301   --bug# 2783559
302   -- Nested LPn changes.
303 
304   PROCEDURE check_lot_serial_codes(
305     p_lpn_id             IN            NUMBER
306   , p_req_header_id      IN            NUMBER
307   , p_shipment_header_id IN            NUMBER
308   , x_lot_ser_flag       OUT NOCOPY    VARCHAR2
309   , x_return_status      OUT NOCOPY    VARCHAR2
310   , x_msg_count          OUT NOCOPY    NUMBER
311   , x_msg_data           OUT NOCOPY    VARCHAR2
312   ) IS
313     -- Nested LPN changes changed the cursor to get all items within an LPN
314     -- along with its child LPNs.
315 
316     -- Bug 3440456
317     -- The following cursor is changed for performance
318     --
319     /*
320     CURSOR get_all_items_in_lpn(p_lpn_id NUMBER) IS
321       SELECT wlc.inventory_item_id
322         FROM wms_lpn_contents wlc, wms_license_plate_numbers wln
323        WHERE wln.lpn_id = wlc.parent_lpn_id
324          AND lpn_id IN(SELECT     lpn_id
325                              FROM wms_license_plate_numbers
326                        START WITH lpn_id = p_lpn_id
327                        CONNECT BY parent_lpn_id = PRIOR lpn_id);
328     */
329 
330     CURSOR get_all_items_in_lpn(p_lpn_id NUMBER) IS
331       SELECT wlc.inventory_item_id
332         FROM wms_lpn_contents wlc
333        WHERE wlc.parent_lpn_id
334                        IN ( SELECT lpn_id
335                               FROM wms_license_plate_numbers
336                        START WITH lpn_id = p_lpn_id
337                        CONNECT BY parent_lpn_id = PRIOR lpn_id);
338 
339     l_item_id       NUMBER;
340     l_return_status VARCHAR2(1)    := fnd_api.g_ret_sts_success;
341     l_msg_count     NUMBER;
342     l_msg_data      VARCHAR2(4000);
343     l_progress      VARCHAR2(10);
344     l_lot_ser_flag  VARCHAR(1)     := 'Y';
345     l_debug         NUMBER         := 1;
346   BEGIN
347     l_lot_ser_flag  := 'Y'; -- 'Y -match ' 'N- not match'
348     l_progress      := 10;
349 
350     IF (l_debug = 1) THEN
351       print_debug('lpn_id: ' || TO_CHAR(p_lpn_id), 1);
352       print_debug('shipment num : ' || p_shipment_header_id, 1);
353       print_debug(' req num: ' || p_req_header_id, 1);
354     END IF;
355 
356     OPEN get_all_items_in_lpn(p_lpn_id);
357 
358     LOOP
359       FETCH get_all_items_in_lpn INTO l_item_id;
360       EXIT WHEN get_all_items_in_lpn%NOTFOUND;
361 
362       IF p_req_header_id IS NOT NULL THEN
363         SELECT 'N'
364           INTO l_lot_ser_flag
365           FROM po_requisition_headers prh, po_requisition_lines prl, rcv_shipment_lines rsl, mtl_system_items msi1, mtl_system_items msi2
366          WHERE prh.requisition_header_id = p_req_header_id
367            AND prl.requisition_header_id = prh.requisition_header_id
368            AND rsl.requisition_line_id = prl.requisition_line_id
369            AND rsl.item_id = msi1.inventory_item_id
370            AND rsl.item_id = l_item_id
371            AND msi1.organization_id = rsl.from_organization_id
372            AND(
373                (NVL(msi1.lot_control_code, 1) = 1
374                 AND NVL(msi2.lot_control_code, 1) = 2)
375                OR(NVL(msi1.serial_number_control_code, 1) IN(1, 6)
376                   AND NVL(msi2.serial_number_control_code, 1) IN(2, 5))
377                OR(NVL(msi1.revision_qty_control_code, 1) = 1
378                   AND NVL(msi2.revision_qty_control_code, 1) = 2)
379               )
380            AND rsl.item_id = msi2.inventory_item_id
381            AND msi2.organization_id = rsl.to_organization_id
382            AND ROWNUM = 1;
383       ELSIF p_shipment_header_id IS NOT NULL THEN
384         SELECT 'N'
385           INTO l_lot_ser_flag
386           FROM rcv_shipment_lines rsl, rcv_shipment_headers rsh, mtl_system_items msi1, mtl_system_items msi2
387          WHERE rsh.shipment_header_id = p_shipment_header_id
388            AND rsl.shipment_header_id = rsh.shipment_header_id
389            AND rsl.item_id = msi1.inventory_item_id
390            AND msi1.organization_id = rsl.from_organization_id
391            AND rsl.item_id = l_item_id
392            AND(
393                (NVL(msi1.lot_control_code, 1) = 1
394                 AND NVL(msi2.lot_control_code, 1) = 2)
395                OR(NVL(msi1.serial_number_control_code, 1) IN(1, 6)
396                   AND NVL(msi2.serial_number_control_code, 1) IN(2, 5))
397                OR(NVL(msi1.revision_qty_control_code, 1) = 1
398                   AND NVL(msi2.revision_qty_control_code, 1) = 2)
399               )
400            AND rsl.item_id = msi2.inventory_item_id
401            AND msi2.organization_id = rsl.to_organization_id
402            AND ROWNUM = 1;
403       END IF;
404 
405       IF (l_lot_ser_flag = 'N') THEN
406         EXIT;
407       END IF;
408     END LOOP;
409 
410     x_lot_ser_flag  := l_lot_ser_flag;
411   EXCEPTION
412     WHEN NO_DATA_FOUND THEN -- item controls are valid
413       x_lot_ser_flag  := 'Y';
414     WHEN OTHERS THEN
415       x_lot_ser_flag   := 'N';
416       x_return_status  := fnd_api.g_ret_sts_unexp_error;
417       print_debug(SQLCODE, 1);
418 
419       IF get_all_items_in_lpn%ISOPEN THEN
420         CLOSE get_all_items_in_lpn;
421       END IF;
422 
423       IF SQLCODE IS NOT NULL THEN
424         inv_mobile_helper_functions.sql_error('check_lot_serial_codes', l_progress, SQLCODE);
425       END IF;
426   END check_lot_serial_codes;
427 
428   PROCEDURE insert_msnt(p_msnt_rec mtl_serial_numbers_temp%ROWTYPE) IS
429     l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
430   BEGIN
431     INSERT INTO mtl_serial_numbers_temp
432                 (
433                  transaction_temp_id
434                , last_update_date
435                , last_updated_by
436                , creation_date
437                , created_by
438                , last_update_login
439                , request_id
440                , program_application_id
441                , program_id
442                , program_update_date
443                , vendor_serial_number
444                , vendor_lot_number
445                , fm_serial_number
446                , to_serial_number
447                , serial_prefix
448                , ERROR_CODE
449                , group_header_id
450                , parent_serial_number
451                , end_item_unit_number
452                , serial_attribute_category
453                , territory_code
454                , --country_of_origin,
455                  origination_date
456                , c_attribute1
457                , c_attribute2
458                , c_attribute3
459                , c_attribute4
460                , c_attribute5
461                , c_attribute6
462                , c_attribute7
463                , c_attribute8
464                , c_attribute9
465                , c_attribute10
466                , c_attribute11
467                , c_attribute12
468                , c_attribute13
469                , c_attribute14
470                , c_attribute15
471                , c_attribute16
472                , c_attribute17
473                , c_attribute18
474                , c_attribute19
475                , c_attribute20
476                , d_attribute1
477                , d_attribute2
478                , d_attribute3
479                , d_attribute4
480                , d_attribute5
481                , d_attribute6
482                , d_attribute7
483                , d_attribute8
484                , d_attribute9
485                , d_attribute10
486                , n_attribute1
487                , n_attribute2
488                , n_attribute3
489                , n_attribute4
490                , n_attribute5
491                , n_attribute6
492                , n_attribute7
493                , n_attribute8
494                , n_attribute9
495                , n_attribute10
496                , status_id
497                , time_since_new
498                , cycles_since_new
499                , time_since_overhaul
500                , cycles_since_overhaul
501                , time_since_repair
502                , cycles_since_repair
503                , time_since_visit
504                , cycles_since_visit
505                , time_since_mark
506                , cycles_since_mark
507                , number_of_repairs
508                 )
509          VALUES (
510                  p_msnt_rec.transaction_temp_id
511                , p_msnt_rec.last_update_date
512                , p_msnt_rec.last_updated_by
513                , p_msnt_rec.creation_date
514                , p_msnt_rec.created_by
515                , p_msnt_rec.last_update_login
516                , p_msnt_rec.request_id
517                , p_msnt_rec.program_application_id
518                , p_msnt_rec.program_id
519                , p_msnt_rec.program_update_date
520                , p_msnt_rec.vendor_serial_number
521                , p_msnt_rec.vendor_lot_number
522                , p_msnt_rec.fm_serial_number
523                , p_msnt_rec.to_serial_number
524                , p_msnt_rec.serial_prefix
525                , p_msnt_rec.ERROR_CODE
526                , p_msnt_rec.group_header_id
527                , p_msnt_rec.parent_serial_number
528                , p_msnt_rec.end_item_unit_number
529                , p_msnt_rec.serial_attribute_category
530                , p_msnt_rec.territory_code
531                , --p_msnt_rec.country_of_origin,
532                  p_msnt_rec.origination_date
533                , p_msnt_rec.c_attribute1
534                , p_msnt_rec.c_attribute2
535                , p_msnt_rec.c_attribute3
536                , p_msnt_rec.c_attribute4
537                , p_msnt_rec.c_attribute5
538                , p_msnt_rec.c_attribute6
539                , p_msnt_rec.c_attribute7
540                , p_msnt_rec.c_attribute8
541                , p_msnt_rec.c_attribute9
542                , p_msnt_rec.c_attribute10
543                , p_msnt_rec.c_attribute11
544                , p_msnt_rec.c_attribute12
545                , p_msnt_rec.c_attribute13
546                , p_msnt_rec.c_attribute14
547                , p_msnt_rec.c_attribute15
548                , p_msnt_rec.c_attribute16
549                , p_msnt_rec.c_attribute17
550                , p_msnt_rec.c_attribute18
551                , p_msnt_rec.c_attribute19
552                , p_msnt_rec.c_attribute20
553                , p_msnt_rec.d_attribute1
554                , p_msnt_rec.d_attribute2
555                , p_msnt_rec.d_attribute3
556                , p_msnt_rec.d_attribute4
557                , p_msnt_rec.d_attribute5
558                , p_msnt_rec.d_attribute6
559                , p_msnt_rec.d_attribute7
560                , p_msnt_rec.d_attribute8
561                , p_msnt_rec.d_attribute9
562                , p_msnt_rec.d_attribute10
563                , p_msnt_rec.n_attribute1
564                , p_msnt_rec.n_attribute2
565                , p_msnt_rec.n_attribute3
566                , p_msnt_rec.n_attribute4
567                , p_msnt_rec.n_attribute5
568                , p_msnt_rec.n_attribute6
569                , p_msnt_rec.n_attribute7
570                , p_msnt_rec.n_attribute8
571                , p_msnt_rec.n_attribute9
572                , p_msnt_rec.n_attribute10
573                , p_msnt_rec.status_id
574                , p_msnt_rec.time_since_new
575                , p_msnt_rec.cycles_since_new
576                , p_msnt_rec.time_since_overhaul
577                , p_msnt_rec.cycles_since_overhaul
578                , p_msnt_rec.time_since_repair
579                , p_msnt_rec.cycles_since_repair
580                , p_msnt_rec.time_since_visit
581                , p_msnt_rec.cycles_since_visit
582                , p_msnt_rec.time_since_mark
583                , p_msnt_rec.cycles_since_mark
584                , p_msnt_rec.number_of_repairs
585                 );
586   END insert_msnt;
587 
588   FUNCTION break_serials_only(p_original_tid IN mtl_serial_numbers_temp.transaction_temp_id%TYPE, p_new_transactions_tb IN trans_rec_tb_tp)
589     RETURN BOOLEAN IS
590     CURSOR c_serials IS
591       SELECT *
592         FROM mtl_serial_numbers_temp
593        WHERE transaction_temp_id = p_original_tid;
594 
595     l_msnt_rec                mtl_serial_numbers_temp%ROWTYPE;
596     l_new_transaction_temp_id mtl_serial_numbers_temp.transaction_temp_id%TYPE;
597     l_new_primary_quantity    NUMBER; -- the quanity user wants to split
598     l_transaction_temp_id     mtl_transaction_lots_temp.transaction_temp_id%TYPE;
599     l_from_ser                mtl_serial_numbers_temp.fm_serial_number%TYPE;
600     l_to_ser                  mtl_serial_numbers_temp.to_serial_number%TYPE;
601     l_new_ser                 mtl_serial_numbers_temp.fm_serial_number%TYPE;
602     l_from_ser_num            NUMBER; -- number part of from serial
603     l_to_ser_num              NUMBER; -- number part of to serial
604     l_primary_quantity        NUMBER; -- the quantity within this serial record
605     l_prefix_temp             VARCHAR2(30);
606     l_debug                   NUMBER                                               := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
607   BEGIN
608     FOR i IN 1 .. p_new_transactions_tb.COUNT LOOP -- Loop through all the transaction lines need to be splitted
609       l_new_transaction_temp_id  := p_new_transactions_tb(i).transaction_id;
610       l_new_primary_quantity     := p_new_transactions_tb(i).primary_quantity;
611       OPEN c_serials;
612 
613       LOOP -- Loop through all the lot record for this transaction
614         FETCH c_serials INTO l_msnt_rec;
615         EXIT WHEN c_serials%NOTFOUND;
616         l_from_ser             := l_msnt_rec.fm_serial_number;
617         l_to_ser               := l_msnt_rec.to_serial_number;
618         -- get the actual number of from and to serial
619         inv_validate.number_from_sequence(l_from_ser, l_prefix_temp, l_from_ser_num);
620         inv_validate.number_from_sequence(l_to_ser, l_prefix_temp, l_to_ser_num);
621         l_primary_quantity     := l_to_ser_num - l_from_ser_num + 1; -- initial qty for this ser record
622         l_transaction_temp_id  := l_msnt_rec.transaction_temp_id; -- initial txn_int_id for this ser rec
623 
624         IF (l_primary_quantity > l_new_primary_quantity) -- new quantity detailed completely
625                                                          -- and there is remaining ser qty
626                                                          THEN
627           l_msnt_rec.transaction_temp_id  := l_new_transaction_temp_id;
628           -- need standard INV api to replace this func
629           l_msnt_rec.to_serial_number     := get_to_serial_number(l_from_ser, l_new_primary_quantity);
630           insert_msnt(l_msnt_rec);                          -- insert one line with new to-ser-number and new txn_id
631                                    -- Update the existing ser rec with start serial number  ??
632           l_new_ser                       := get_to_serial_number(l_from_ser, l_new_primary_quantity + 1);
633 
634           UPDATE mtl_serial_numbers_temp
635              SET fm_serial_number = l_new_ser
636            WHERE transaction_temp_id = l_transaction_temp_id
637              AND fm_serial_number = l_from_ser
638              AND to_serial_number = l_to_ser;
639 
640           EXIT; -- exit serial loop
641         ELSIF(l_primary_quantity < l_new_primary_quantity) THEN
642           -- new quantity is partially detailed
643           -- ser rec qty is exhausted
644           -- need to continue ser loop in this case
645 
646           -- Update the ser rec with new transaction interface ID
647           UPDATE mtl_serial_numbers_temp
648              SET transaction_temp_id = l_new_transaction_temp_id
649            WHERE transaction_temp_id = l_transaction_temp_id
650              AND fm_serial_number = l_from_ser
651              AND to_serial_number = l_to_ser;
652 
653           -- reduce the new qty
654           l_new_primary_quantity  := l_new_primary_quantity - l_primary_quantity;
655         ELSIF(l_primary_quantity = l_new_primary_quantity) THEN
656           -- exact match
657 
658           -- Update the lot rec with new transaction interface ID
659           UPDATE mtl_serial_numbers_temp
660              SET transaction_temp_id = l_new_transaction_temp_id
661            WHERE transaction_temp_id = l_transaction_temp_id
662              AND fm_serial_number = l_from_ser
663              AND to_serial_number = l_to_ser;
664 
665           EXIT; -- exit serial loop
666         END IF;
667       END LOOP; -- end serial loop
668 
669       CLOSE c_serials;
670     END LOOP; -- end transaction line loop
671 
672     RETURN TRUE;
673   EXCEPTION
674     WHEN OTHERS THEN
675       IF c_serials%ISOPEN THEN
676         CLOSE c_serials;
677       END IF;
678 
679       RAISE;
680   END break_serials_only;
681 
682   FUNCTION break_lots_only(p_original_tid IN mtl_transaction_lots_temp.transaction_temp_id%TYPE,
683   p_new_transactions_tb IN trans_rec_tb_tp)
684     RETURN BOOLEAN IS
685     CURSOR c_lots IS
686       SELECT   ROWID
687              , transaction_temp_id
688              , last_update_date
689              , last_updated_by
690              , creation_date
691              , created_by
692              , last_update_login
693              , request_id
694              , program_application_id
695              , program_id
696              , program_update_date
697              , transaction_quantity
698              , secondary_quantity --invconv kkillams
699              , primary_quantity
700              , lot_number
701              , lot_expiration_date
702              , ERROR_CODE
703              , serial_transaction_temp_id
704              , group_header_id
705              , put_away_rule_id
706              , pick_rule_id
707              , description
708              , vendor_id
709              , supplier_lot_number
710              , territory_code
711              , origination_date
712              , date_code
713              , grade_code
714              , change_date
715              , maturity_date
716              , status_id
717              , retest_date
718              , age
719              , item_size
720              , color
721              , volume
722              , volume_uom
723              , place_of_origin
724              , best_by_date
725              , LENGTH
726              , length_uom
727              , recycled_content
728              , thickness
729              , thickness_uom
730              , width
731              , width_uom
732              , curl_wrinkle_fold
733              , lot_attribute_category
734              , c_attribute1
735              , c_attribute2
736              , c_attribute3
737              , c_attribute4
738              , c_attribute5
739              , c_attribute6
740              , c_attribute7
741              , c_attribute8
742              , c_attribute9
743              , c_attribute10
744              , c_attribute11
745              , c_attribute12
746              , c_attribute13
747              , c_attribute14
748              , c_attribute15
749              , c_attribute16
750              , c_attribute17
751              , c_attribute18
752              , c_attribute19
753              , c_attribute20
754              , d_attribute1
755              , d_attribute2
756              , d_attribute3
757              , d_attribute4
758              , d_attribute5
759              , d_attribute6
760              , d_attribute7
761              , d_attribute8
762              , d_attribute9
763              , d_attribute10
764              , n_attribute1
765              , n_attribute2
766              , n_attribute3
767              , n_attribute4
768              , n_attribute5
769              , n_attribute6
770              , n_attribute7
771              , n_attribute8
772              , n_attribute9
773              , n_attribute10
774              , vendor_name
775           FROM mtl_transaction_lots_temp
776          WHERE transaction_temp_id = p_original_tid
777       ORDER BY DECODE(
778                  inv_rcv_common_apis.g_order_lots_by
779                , inv_rcv_common_apis.g_order_lots_by_exp_date, lot_expiration_date
780                , inv_rcv_common_apis.g_order_lots_by_creation_date, creation_date
781                , lot_expiration_date
782                );
783 
784     --Changed the order  by for bug 2422193
785     --ORDER BY lot_expiration_date,creation_date;
786 
787     l_mtlt_rec                mtl_transaction_lots_temp%ROWTYPE;
788     l_new_transaction_temp_id mtl_transaction_lots_temp.transaction_temp_id%TYPE;
789     l_new_primary_quantity      NUMBER; -- the quanity user wants to split
790     l_transaction_temp_id     mtl_transaction_lots_temp.transaction_temp_id%TYPE;
791     l_primary_quantity          NUMBER; -- the primary qty for lot
792     l_transaction_quantity       NUMBER;
793     l_sec_transaction_quantity   NUMBER; --invconv kkillams
794     l_new_secondary_quantity    NUMBER; -- the quanity user wants to split
795     l_lot_number              mtl_transaction_lots_temp.lot_number%TYPE;
796     --BUG 2673970
797     l_rowid                   ROWID;
798     l_debug                   NUMBER                                               := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
799   BEGIN
800     FOR i IN 1 .. p_new_transactions_tb.COUNT LOOP -- Loop through all the transaction lines need to be splitted
801       l_new_transaction_temp_id  := p_new_transactions_tb(i).transaction_id;
802       l_new_primary_quantity     := p_new_transactions_tb(i).primary_quantity;
803       l_new_secondary_quantity     := p_new_transactions_tb(i).secondary_quantity;
804 
805       OPEN c_lots;
806       LOOP -- Loop through all the lot record for this transaction
807 
808            --BUG 2673970
809         FETCH c_lots INTO l_rowid
810        , l_mtlt_rec.transaction_temp_id
811        , l_mtlt_rec.last_update_date
812        , l_mtlt_rec.last_updated_by
813        , l_mtlt_rec.creation_date
814        , l_mtlt_rec.created_by
815        , l_mtlt_rec.last_update_login
816        , l_mtlt_rec.request_id
817        , l_mtlt_rec.program_application_id
818        , l_mtlt_rec.program_id
819        , l_mtlt_rec.program_update_date
820        , l_mtlt_rec.transaction_quantity
821        , l_mtlt_rec.secondary_quantity --invconv kkillams
822        , l_mtlt_rec.primary_quantity
823        , l_mtlt_rec.lot_number
824        , l_mtlt_rec.lot_expiration_date
825        , l_mtlt_rec.ERROR_CODE
826        , l_mtlt_rec.serial_transaction_temp_id
827        , l_mtlt_rec.group_header_id
828        , l_mtlt_rec.put_away_rule_id
829        , l_mtlt_rec.pick_rule_id
830        , l_mtlt_rec.description
831        , l_mtlt_rec.vendor_id
832        , l_mtlt_rec.supplier_lot_number
833        , l_mtlt_rec.territory_code
834        , l_mtlt_rec.origination_date
835        , l_mtlt_rec.date_code
836        , l_mtlt_rec.grade_code
837        , l_mtlt_rec.change_date
838        , l_mtlt_rec.maturity_date
839        , l_mtlt_rec.status_id
840        , l_mtlt_rec.retest_date
841        , l_mtlt_rec.age
842        , l_mtlt_rec.item_size
843        , l_mtlt_rec.color
844        , l_mtlt_rec.volume
845        , l_mtlt_rec.volume_uom
846        , l_mtlt_rec.place_of_origin
847        , l_mtlt_rec.best_by_date
848        , l_mtlt_rec.LENGTH
849        , l_mtlt_rec.length_uom
850        , l_mtlt_rec.recycled_content
851        , l_mtlt_rec.thickness
852        , l_mtlt_rec.thickness_uom
853        , l_mtlt_rec.width
854        , l_mtlt_rec.width_uom
855        , l_mtlt_rec.curl_wrinkle_fold
856        , l_mtlt_rec.lot_attribute_category
857        , l_mtlt_rec.c_attribute1
858        , l_mtlt_rec.c_attribute2
859        , l_mtlt_rec.c_attribute3
860        , l_mtlt_rec.c_attribute4
861        , l_mtlt_rec.c_attribute5
862        , l_mtlt_rec.c_attribute6
863        , l_mtlt_rec.c_attribute7
864        , l_mtlt_rec.c_attribute8
865        , l_mtlt_rec.c_attribute9
866        , l_mtlt_rec.c_attribute10
867        , l_mtlt_rec.c_attribute11
868        , l_mtlt_rec.c_attribute12
869        , l_mtlt_rec.c_attribute13
870        , l_mtlt_rec.c_attribute14
871        , l_mtlt_rec.c_attribute15
872        , l_mtlt_rec.c_attribute16
873        , l_mtlt_rec.c_attribute17
874        , l_mtlt_rec.c_attribute18
875        , l_mtlt_rec.c_attribute19
876        , l_mtlt_rec.c_attribute20
877        , l_mtlt_rec.d_attribute1
878        , l_mtlt_rec.d_attribute2
879        , l_mtlt_rec.d_attribute3
880        , l_mtlt_rec.d_attribute4
881        , l_mtlt_rec.d_attribute5
882        , l_mtlt_rec.d_attribute6
883        , l_mtlt_rec.d_attribute7
884        , l_mtlt_rec.d_attribute8
885        , l_mtlt_rec.d_attribute9
886        , l_mtlt_rec.d_attribute10
887        , l_mtlt_rec.n_attribute1
888        , l_mtlt_rec.n_attribute2
889        , l_mtlt_rec.n_attribute3
890        , l_mtlt_rec.n_attribute4
891        , l_mtlt_rec.n_attribute5
892        , l_mtlt_rec.n_attribute6
893        , l_mtlt_rec.n_attribute7
894        , l_mtlt_rec.n_attribute8
895        , l_mtlt_rec.n_attribute9
896        , l_mtlt_rec.n_attribute10
897        , l_mtlt_rec.vendor_name;
898         EXIT WHEN c_lots%NOTFOUND;
899         l_primary_quantity      := l_mtlt_rec.primary_quantity; -- initial qty for this lot
900         l_transaction_temp_id   := l_mtlt_rec.transaction_temp_id; -- initial txn_int_id for this lot
901         l_lot_number            := l_mtlt_rec.lot_number;
902         l_transaction_quantity  := l_mtlt_rec.transaction_quantity;
903         l_sec_transaction_quantity  := l_mtlt_rec.secondary_quantity; --invconv kkillams
904 
905         IF (l_primary_quantity > l_new_primary_quantity)                                                -- new quantity detailed completely
906                                                          -- and there is remaining lot qty
907                                                          THEN
908           l_mtlt_rec.transaction_temp_id   := l_new_transaction_temp_id;
909           l_mtlt_rec.primary_quantity      := l_new_primary_quantity;
910           l_mtlt_rec.transaction_quantity  := l_transaction_quantity * l_new_primary_quantity / l_primary_quantity;
911           --invconv kkillams
912           IF  l_sec_transaction_quantity IS NOT NULL THEN
913             l_mtlt_rec.secondary_quantity  := l_sec_transaction_quantity * l_new_secondary_quantity / l_sec_transaction_quantity;
914           END IF;
915           print_debug('insert_mtlt',1);
916           insert_mtlt(l_mtlt_rec); -- insert one line with new quantity and new txn_id
917           l_primary_quantity               := l_primary_quantity - l_new_primary_quantity;
918           l_transaction_quantity           := l_transaction_quantity - l_mtlt_rec.transaction_quantity;
919           --invconv kkillams
920           IF  l_sec_transaction_quantity IS NOT NULL THEN
921              l_sec_transaction_quantity       := l_sec_transaction_quantity -  l_mtlt_rec.secondary_quantity ;
922           END IF;
923 
924           print_debug('Update 1 mtl_transaction_lots_temp',1);
925           -- Update the existing lot rec with reduced quantity
926           UPDATE mtl_transaction_lots_temp
927              SET primary_quantity = l_primary_quantity
928                , transaction_quantity = l_transaction_quantity
929                , secondary_quantity = l_sec_transaction_quantity
930            WHERE transaction_temp_id = l_transaction_temp_id
931              AND lot_number = l_lot_number
932              AND ROWID = l_rowid;
933 
934           EXIT; -- exit lot loop
935         ELSIF(l_primary_quantity < l_new_primary_quantity) THEN
936           -- new quantity is partially detailed
937           -- lot qty is exhausted
938           -- need to continue lot loop in this case
939 
940           -- Update the lot rec with new transaction interface ID
941           print_debug('Update 2 mtl_transaction_lots_temp',1);
942           UPDATE mtl_transaction_lots_temp
943              SET transaction_temp_id = l_new_transaction_temp_id
944            WHERE transaction_temp_id = l_transaction_temp_id
945              AND lot_number = l_lot_number
946              AND ROWID = l_rowid;
947 
948           -- reduce the new qty
949           l_new_primary_quantity  := l_new_primary_quantity - l_primary_quantity;
950           l_new_secondary_quantity  := l_new_secondary_quantity - l_sec_transaction_quantity; --invconv kkillams
951         ELSIF(l_primary_quantity = l_new_primary_quantity) THEN
952           -- exact match
953 
954           print_debug('Update 3 mtl_transaction_lots_temp',1);
955           -- Update the lot rec with new transaction interface ID
956           UPDATE mtl_transaction_lots_temp
957              SET transaction_temp_id = l_new_transaction_temp_id
958            WHERE transaction_temp_id = l_transaction_temp_id
959              AND lot_number = l_lot_number
960              AND ROWID = l_rowid;
961 
962           EXIT; -- exit lot loop
963         END IF;
964       END LOOP; -- end lot loop
965 
966       CLOSE c_lots;
967     END LOOP; -- end transaction line loop
968 
969     RETURN TRUE;
970   EXCEPTION
971     WHEN OTHERS THEN
972       IF c_lots%ISOPEN THEN
973         CLOSE c_lots;
974       END IF;
975 
976       RAISE;
977   END break_lots_only;
978 
979   FUNCTION break_lots_serials(
980     p_original_tid        IN mtl_transaction_lots_temp.transaction_temp_id%TYPE
981   , p_new_transactions_tb IN trans_rec_tb_tp
982   )
983     RETURN BOOLEAN IS
984     CURSOR c_lots IS
985       SELECT   ROWID
986              , transaction_temp_id
987              , last_update_date
988              , last_updated_by
989              , creation_date
990              , created_by
991              , last_update_login
992              , request_id
993              , program_application_id
994              , program_id
995              , program_update_date
996              , transaction_quantity
997              , secondary_quantity --invconv kkillams
998              , primary_quantity
999              , lot_number
1000              , lot_expiration_date
1001              , ERROR_CODE
1002              , serial_transaction_temp_id
1003              , group_header_id
1004              , put_away_rule_id
1005              , pick_rule_id
1006              , description
1007              , vendor_id
1008              , supplier_lot_number
1009              , territory_code
1010              , origination_date
1011              , date_code
1012              , grade_code
1013              , change_date
1014              , maturity_date
1015              , status_id
1016              , retest_date
1017              , age
1018              , item_size
1019              , color
1020              , volume
1021              , volume_uom
1022              , place_of_origin
1023              , best_by_date
1024              , LENGTH
1025              , length_uom
1026              , recycled_content
1027              , thickness
1028              , thickness_uom
1029              , width
1030              , width_uom
1031              , curl_wrinkle_fold
1032              , lot_attribute_category
1033              , c_attribute1
1034              , c_attribute2
1035              , c_attribute3
1036              , c_attribute4
1037              , c_attribute5
1038              , c_attribute6
1039              , c_attribute7
1040              , c_attribute8
1041              , c_attribute9
1042              , c_attribute10
1043              , c_attribute11
1044              , c_attribute12
1045              , c_attribute13
1046              , c_attribute14
1047              , c_attribute15
1048              , c_attribute16
1049              , c_attribute17
1050              , c_attribute18
1051              , c_attribute19
1052              , c_attribute20
1053              , d_attribute1
1054              , d_attribute2
1055              , d_attribute3
1056              , d_attribute4
1057              , d_attribute5
1058              , d_attribute6
1059              , d_attribute7
1060              , d_attribute8
1061              , d_attribute9
1062              , d_attribute10
1063              , n_attribute1
1064              , n_attribute2
1065              , n_attribute3
1066              , n_attribute4
1067              , n_attribute5
1068              , n_attribute6
1069              , n_attribute7
1070              , n_attribute8
1071              , n_attribute9
1072              , n_attribute10
1073              , vendor_name
1074           FROM mtl_transaction_lots_temp
1075          WHERE transaction_temp_id = p_original_tid
1076       ORDER BY DECODE(
1077                  inv_rcv_common_apis.g_order_lots_by
1078                , inv_rcv_common_apis.g_order_lots_by_exp_date, lot_expiration_date
1079                , inv_rcv_common_apis.g_order_lots_by_creation_date, creation_date
1080                , lot_expiration_date
1081                );
1082 
1083     --Changed the order  by for bug 2422193
1084     --ORDER BY lot_expiration_date,creation_date;
1085     l_mtlt_rec                   mtl_transaction_lots_temp%ROWTYPE;
1086     l_new_transaction_temp_id    mtl_transaction_lots_temp.transaction_temp_id%TYPE;
1087     l_new_primary_quantity       NUMBER; -- the quanity user wants to split
1088     l_transaction_temp_id        mtl_transaction_lots_temp.transaction_temp_id%TYPE;
1089     l_primary_quantity           NUMBER; -- the transaction qty for lot
1090     l_transaction_quantity       NUMBER;
1091     l_lot_number                 mtl_transaction_lots_temp.lot_number%TYPE;
1092     l_serial_transaction_temp_id mtl_serial_numbers_temp.transaction_temp_id%TYPE;
1093     l_tran_rec_tb                trans_rec_tb_tp;
1094     l_sec_transaction_quantity   NUMBER; --invconv kkillams
1095     l_new_secondary_quantity    NUMBER; -- the quanity user wants to split
1096     --BUG 2673970
1097     l_rowid                      ROWID;
1098     l_debug                      NUMBER                                               := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
1099 
1100   BEGIN
1101     FOR i IN 1 .. p_new_transactions_tb.COUNT LOOP -- Loop through all the transaction lines need to be splitted
1102       l_new_transaction_temp_id  := p_new_transactions_tb(i).transaction_id;
1103       l_new_primary_quantity     := p_new_transactions_tb(i).primary_quantity;
1104       l_new_secondary_quantity   := p_new_transactions_tb(i).secondary_quantity; --invconv
1105       OPEN c_lots;
1106 
1107       LOOP -- Loop through all the lot record for this transaction
1108 
1109            --BUG 2673970
1110         FETCH c_lots INTO l_rowid
1111        , l_mtlt_rec.transaction_temp_id
1112        , l_mtlt_rec.last_update_date
1113        , l_mtlt_rec.last_updated_by
1114        , l_mtlt_rec.creation_date
1115        , l_mtlt_rec.created_by
1116        , l_mtlt_rec.last_update_login
1117        , l_mtlt_rec.request_id
1118        , l_mtlt_rec.program_application_id
1119        , l_mtlt_rec.program_id
1120        , l_mtlt_rec.program_update_date
1121        , l_mtlt_rec.transaction_quantity
1122        , l_mtlt_rec.secondary_quantity --invconv kkillams
1123        , l_mtlt_rec.primary_quantity
1124        , l_mtlt_rec.lot_number
1125        , l_mtlt_rec.lot_expiration_date
1126        , l_mtlt_rec.ERROR_CODE
1127        , l_mtlt_rec.serial_transaction_temp_id
1128        , l_mtlt_rec.group_header_id
1129        , l_mtlt_rec.put_away_rule_id
1130        , l_mtlt_rec.pick_rule_id
1131        , l_mtlt_rec.description
1132        , l_mtlt_rec.vendor_id
1133        , l_mtlt_rec.supplier_lot_number
1134        , l_mtlt_rec.territory_code
1135        , l_mtlt_rec.origination_date
1136        , l_mtlt_rec.date_code
1137        , l_mtlt_rec.grade_code
1138        , l_mtlt_rec.change_date
1139        , l_mtlt_rec.maturity_date
1140        , l_mtlt_rec.status_id
1141        , l_mtlt_rec.retest_date
1142        , l_mtlt_rec.age
1143        , l_mtlt_rec.item_size
1144        , l_mtlt_rec.color
1145        , l_mtlt_rec.volume
1146        , l_mtlt_rec.volume_uom
1147        , l_mtlt_rec.place_of_origin
1148        , l_mtlt_rec.best_by_date
1149        , l_mtlt_rec.LENGTH
1150        , l_mtlt_rec.length_uom
1151        , l_mtlt_rec.recycled_content
1152        , l_mtlt_rec.thickness
1153        , l_mtlt_rec.thickness_uom
1154        , l_mtlt_rec.width
1155        , l_mtlt_rec.width_uom
1156        , l_mtlt_rec.curl_wrinkle_fold
1157        , l_mtlt_rec.lot_attribute_category
1158        , l_mtlt_rec.c_attribute1
1159        , l_mtlt_rec.c_attribute2
1160        , l_mtlt_rec.c_attribute3
1161        , l_mtlt_rec.c_attribute4
1162        , l_mtlt_rec.c_attribute5
1163        , l_mtlt_rec.c_attribute6
1164        , l_mtlt_rec.c_attribute7
1165        , l_mtlt_rec.c_attribute8
1166        , l_mtlt_rec.c_attribute9
1167        , l_mtlt_rec.c_attribute10
1168        , l_mtlt_rec.c_attribute11
1169        , l_mtlt_rec.c_attribute12
1170        , l_mtlt_rec.c_attribute13
1171        , l_mtlt_rec.c_attribute14
1172        , l_mtlt_rec.c_attribute15
1173        , l_mtlt_rec.c_attribute16
1174        , l_mtlt_rec.c_attribute17
1175        , l_mtlt_rec.c_attribute18
1176        , l_mtlt_rec.c_attribute19
1177        , l_mtlt_rec.c_attribute20
1178        , l_mtlt_rec.d_attribute1
1179        , l_mtlt_rec.d_attribute2
1180        , l_mtlt_rec.d_attribute3
1181        , l_mtlt_rec.d_attribute4
1182        , l_mtlt_rec.d_attribute5
1183        , l_mtlt_rec.d_attribute6
1184        , l_mtlt_rec.d_attribute7
1185        , l_mtlt_rec.d_attribute8
1186        , l_mtlt_rec.d_attribute9
1187        , l_mtlt_rec.d_attribute10
1188        , l_mtlt_rec.n_attribute1
1189        , l_mtlt_rec.n_attribute2
1190        , l_mtlt_rec.n_attribute3
1191        , l_mtlt_rec.n_attribute4
1192        , l_mtlt_rec.n_attribute5
1193        , l_mtlt_rec.n_attribute6
1194        , l_mtlt_rec.n_attribute7
1195        , l_mtlt_rec.n_attribute8
1196        , l_mtlt_rec.n_attribute9
1197        , l_mtlt_rec.n_attribute10
1198        , l_mtlt_rec.vendor_name;
1199         EXIT WHEN c_lots%NOTFOUND;
1200         l_primary_quantity            := l_mtlt_rec.primary_quantity; -- initial qty for this lot
1201         l_transaction_temp_id         := l_mtlt_rec.transaction_temp_id; -- initial txn_int_id for this lot
1202         l_serial_transaction_temp_id  := l_mtlt_rec.serial_transaction_temp_id;
1203         l_lot_number                  := l_mtlt_rec.lot_number;
1204         l_sec_transaction_quantity    := l_mtlt_rec.secondary_quantity; -- initial qty for this lot
1205 
1206         IF (l_primary_quantity > l_new_primary_quantity)                                                -- new quantity detailed completely
1207                                                          -- and there is remaining lot qty
1208                                                          THEN
1209           l_mtlt_rec.transaction_temp_id     := l_new_transaction_temp_id;
1210           l_mtlt_rec.primary_quantity        := l_new_primary_quantity;
1211           l_transaction_quantity             := l_mtlt_rec.transaction_quantity;
1212           l_mtlt_rec.secondary_quantity      := l_new_secondary_quantity;
1213 
1214           SELECT mtl_material_transactions_s.NEXTVAL
1215             INTO l_mtlt_rec.serial_transaction_temp_id
1216             FROM DUAL;
1217 
1218           l_mtlt_rec.transaction_quantity    := l_transaction_quantity * l_new_primary_quantity / l_primary_quantity;
1219           insert_mtlt(l_mtlt_rec); -- insert one line with new quantity and new txn_id
1220           l_tran_rec_tb(1).transaction_id    := l_mtlt_rec.serial_transaction_temp_id;
1221           l_tran_rec_tb(1).primary_quantity  := l_new_primary_quantity;
1222           IF l_mtlt_rec.secondary_quantity IS NOT NULL THEN --invconv
1223              l_mtlt_rec.secondary_quantity      := l_sec_transaction_quantity * l_new_secondary_quantity / l_sec_transaction_quantity;
1224           END IF;
1225 
1226           IF break_serials_only(l_serial_transaction_temp_id, l_tran_rec_tb) THEN
1227             NULL;
1228           END IF;
1229 
1230           l_primary_quantity                 := l_primary_quantity - l_new_primary_quantity;
1231           l_transaction_quantity             := l_transaction_quantity - l_mtlt_rec.transaction_quantity;
1232           l_sec_transaction_quantity         := l_sec_transaction_quantity - l_mtlt_rec.secondary_quantity;
1233 
1234           -- Update the existing lot rec with reduced quantity
1235           UPDATE mtl_transaction_lots_temp
1236              SET primary_quantity = l_primary_quantity
1237                , transaction_quantity = l_transaction_quantity
1238                , secondary_quantity = l_sec_transaction_quantity
1239            WHERE transaction_temp_id = l_transaction_temp_id
1240              AND lot_number = l_lot_number
1241              AND ROWID = l_rowid;
1242 
1243           EXIT; -- exit lot loop
1244         ELSIF(l_primary_quantity < l_new_primary_quantity) THEN
1245           -- new quantity is partially detailed
1246           -- lot qty is exhausted
1247           -- need to continue lot loop in this case
1248 
1249           -- Update the lot rec with new transaction interface ID
1250           UPDATE mtl_transaction_lots_temp
1251              SET transaction_temp_id = l_new_transaction_temp_id
1252            WHERE transaction_temp_id = l_transaction_temp_id
1253              AND lot_number = l_lot_number
1254              AND ROWID = l_rowid;
1255 
1256           -- reduce the new qty
1257           l_new_primary_quantity           := l_new_primary_quantity - l_primary_quantity;
1258           l_new_secondary_quantity       := l_new_secondary_quantity - l_sec_transaction_quantity;
1259         ELSIF(l_primary_quantity = l_new_primary_quantity) THEN
1260           -- exact match
1261 
1262           -- Update the lot rec with new transaction interface ID
1263           UPDATE mtl_transaction_lots_temp
1264              SET transaction_temp_id = l_new_transaction_temp_id
1265            WHERE transaction_temp_id = l_transaction_temp_id
1266              AND lot_number = l_lot_number
1267              AND ROWID = l_rowid;
1268 
1269           EXIT; -- exit lot loop
1270         END IF;
1271       END LOOP; -- end lot loop
1272 
1273       CLOSE c_lots;
1274     END LOOP; -- end transaction line loop
1275 
1276     RETURN TRUE;
1277   EXCEPTION
1278     WHEN OTHERS THEN
1279       IF c_lots%ISOPEN THEN
1280         CLOSE c_lots;
1281       END IF;
1282 
1283       RAISE;
1284   END break_lots_serials;
1285 
1286   PROCEDURE BREAK(
1287     p_original_tid        IN mtl_transaction_lots_temp.transaction_temp_id%TYPE
1288   , p_new_transactions_tb IN trans_rec_tb_tp
1289   , p_lot_control_code    IN NUMBER
1290   , p_serial_control_code IN NUMBER
1291   ) IS
1292     l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
1293   BEGIN
1294    print_debug('Entered BREAK',1);
1295     --lots and not serials -- ?? VERIFY THIS
1296     IF (p_lot_control_code = 2
1297         AND p_serial_control_code IN(1)) THEN
1298        print_debug('break_lots_only',1);
1299       IF break_lots_only(p_original_tid, p_new_transactions_tb) THEN
1300         NULL;
1301       END IF;
1302     --serials not lots
1303       -- Toshiba Fixes for RMA
1304     ELSIF(p_lot_control_code = 1
1305           AND p_serial_control_code NOT IN(1)) THEN
1306        print_debug('break_serials_only',1);
1307       IF break_serials_only(p_original_tid, p_new_transactions_tb) THEN
1308         NULL;
1309       END IF;
1310     --both lot and serial
1311     ELSIF(p_lot_control_code = 2
1312           AND p_serial_control_code NOT IN(1)) THEN
1313        print_debug('break_lots_serials',1);
1314       IF break_lots_serials(p_original_tid, p_new_transactions_tb) THEN
1315         NULL;
1316       END IF;
1317     END IF;
1318   END BREAK;
1319 
1320   PROCEDURE gen_receipt_num(
1321     x_receipt_num     OUT NOCOPY VARCHAR2
1322   , p_organization_id            NUMBER
1323   , x_return_status   OUT NOCOPY VARCHAR2
1324   , x_msg_count       OUT NOCOPY NUMBER
1325   , x_msg_data        OUT NOCOPY VARCHAR2
1326   ) IS
1327     PRAGMA AUTONOMOUS_TRANSACTION;
1328     l_receipt_exists NUMBER;
1329     l_return_status  VARCHAR2(1)   := fnd_api.g_ret_sts_success;
1330     l_msg_count      NUMBER;
1331     l_msg_data       VARCHAR2(400);
1332     l_progress       VARCHAR2(10);
1333     l_receipt_code   VARCHAR2(25);
1334     l_temp_rcpt_num  VARCHAR(30); --bug6014386
1335     l_debug          NUMBER        := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
1336   BEGIN
1337     x_return_status  := fnd_api.g_ret_sts_success;
1338     l_progress       := '10';
1339 
1340     IF (l_debug = 1) THEN
1341       print_debug('Enter gen_receipt_num 10 ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
1342     END IF;
1343 
1344   /* Commented for bug 6014386
1345     UPDATE rcv_parameters
1346        SET next_receipt_num = next_receipt_num + 1
1347      WHERE organization_id = p_organization_id;
1348 
1349     COMMIT;
1350     l_progress       := '20';
1351 
1352     SELECT TO_CHAR(next_receipt_num)
1353       INTO x_receipt_num
1354       FROM rcv_parameters
1355      WHERE organization_id = p_organization_id;
1356 
1357     l_progress       := '30';
1358 
1359     BEGIN
1360       SELECT 1
1361         INTO l_receipt_exists
1362         FROM rcv_shipment_headers rsh
1363        WHERE receipt_num = x_receipt_num
1364          AND ship_to_org_id = p_organization_id;
1365     EXCEPTION
1366       WHEN NO_DATA_FOUND THEN
1367         l_receipt_exists  := 0;
1368       WHEN OTHERS THEN
1369         RAISE; -- ? multi row selected
1370     END;
1371 
1372     l_progress       := '40';
1373 
1374     IF (l_receipt_exists = 1) THEN
1375       -- need to handle receipt_num generation error  ?
1376       po_message_s.app_error('RCV_RC_RESET_AUTO_NUMBER');
1377     END IF;
1378 
1379     Bug 6014386 , commented upto here*/
1380 
1381     /*Fix for bug6014386 Begin.
1382       If next receipt number is not unique, we need to loop through
1383        and find a unique number instead of erroring the transaction.  */
1384 
1385  	     SELECT to_char(next_receipt_num + 1)
1386  	         INTO l_temp_rcpt_num
1387  	        FROM rcv_parameters
1388  	        WHERE organization_id =  p_organization_id
1389  	         FOR UPDATE OF next_receipt_num;
1390 
1391  	     l_progress       := '20';
1392 
1393  	     LOOP
1394  	       SELECT COUNT(1)
1395  	          INTO   l_receipt_exists
1396  	         FROM   rcv_shipment_headers rsh
1397  	         WHERE  rsh.receipt_num = l_temp_rcpt_num
1398  	         AND   rsh.ship_to_org_id = p_organization_id ;
1399 
1400  	        IF l_receipt_exists = 0 THEN
1401  	             UPDATE rcv_parameters
1402  	              SET next_receipt_num = l_temp_rcpt_num
1403  	             WHERE organization_id = p_organization_id ;
1404 
1405  	             COMMIT;  --commit the autonomous transaction
1406 
1407  	             EXIT;
1408  	         ELSE
1409  	           l_temp_rcpt_num := TO_CHAR(TO_NUMBER(l_temp_rcpt_num) + 1);  --increment the receipt number
1410  	        END IF;
1411  	     END LOOP;
1412 
1413  	      x_receipt_num := l_temp_rcpt_num;
1414 
1415      --Bug6014386.End
1416 
1417     l_progress       := '50';
1418 
1419     IF (l_debug = 1) THEN
1420       print_debug('Exit gen_receipt_num 20 ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
1421     END IF;
1422   EXCEPTION
1423     WHEN OTHERS THEN
1424       x_return_status  := fnd_api.g_ret_sts_unexp_error;
1425 
1426       IF SQLCODE IS NOT NULL THEN
1427         inv_mobile_helper_functions.sql_error('INV_RCV_COMMON_APIS.gen_receipt_num', l_progress, SQLCODE);
1428       END IF;
1429 
1430       fnd_message.set_name('PO', 'PO_SP_GET_NEXT_AUTO_RECEIPT_NM');
1431       fnd_msg_pub.ADD;
1432       fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
1433   END gen_receipt_num;
1434 
1435   PROCEDURE rcv_gen_receipt_num(
1436     x_receipt_num     OUT NOCOPY VARCHAR2
1437   , p_organization_id            NUMBER
1438   , x_return_status   OUT NOCOPY VARCHAR2
1439   , x_msg_count       OUT NOCOPY NUMBER
1440   , x_msg_data        OUT NOCOPY VARCHAR2
1441   ) IS
1442     l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
1443   BEGIN
1444     x_return_status  := fnd_api.g_ret_sts_success;
1445 
1446     IF (l_debug = 1) THEN
1447       print_debug('Enter rcv_gen_receipt_num 10 ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
1448     END IF;
1449 
1450     IF g_rcv_global_var.receipt_num IS NULL THEN
1451       IF (l_debug = 1) THEN
1452         print_debug('Receipt number is null', 4);
1453       END IF;
1454 
1455       inv_rcv_common_apis.gen_receipt_num(
1456         x_receipt_num                => g_rcv_global_var.receipt_num
1457       , p_organization_id            => p_organization_id
1458       , x_return_status              => x_return_status
1459       , x_msg_count                  => x_msg_count
1460       , x_msg_data                   => x_msg_data
1461       );
1462     END IF;
1463 
1464     IF (l_debug = 1) THEN
1465       print_debug('Generated the receipt number:' || g_rcv_global_var.receipt_num, 4);
1466     END IF;
1467 
1468     x_receipt_num    := g_rcv_global_var.receipt_num;
1469   END rcv_gen_receipt_num;
1470 
1471   PROCEDURE rcv_clear_global IS
1472     l_return_status VARCHAR2(1)   := fnd_api.g_ret_sts_success;
1473     l_msg_count     NUMBER;
1474     l_msg_data      VARCHAR2(400);
1475     l_debug         NUMBER        := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
1476   BEGIN
1477     g_po_startup_value                              := NULL;
1478     g_rcv_global_var                                := NULL;
1479     g_lot_status_tb.DELETE;
1480     inv_rcv_std_rcpt_apis.g_shipment_header_id      := NULL;
1481     inv_rcv_std_rcpt_apis.g_rcpt_match_table_gross.DELETE;
1482     inv_rcv_std_rcpt_apis.g_receipt_detail_index    := 1;
1483     inv_rcv_std_rcpt_apis.g_dummy_lpn_id            := NULL;
1484     inv_rcv_std_deliver_apis.g_rcvtxn_detail_index  := 1;
1485     inv_rcv_std_deliver_apis.g_rcvtxn_match_table_gross.DELETE;
1486 
1487     --Calling the procedure to clear the Global variable which conatains Lot Numbers (Bug # 3156689)
1488     clear_lot_rec;
1489 
1490   -- clear the message stack.
1491     fnd_msg_pub.delete_msg;
1492     gen_txn_group_id;
1493 
1494     -- set wms_purchased flag
1495     IF wms_install.check_install(l_return_status, l_msg_count, l_msg_data, NULL) THEN
1496       -- calling lpn_pack_complete to revert the weight/volume change
1497       IF (l_debug = 1) THEN
1498         print_debug('Calling wms_container_pub.lpn_pack_complete  ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 4);
1499         print_debug('wms_container_pub.G_LPN_WT_VOL_CHANGES.count  ' || wms_container_pub.g_lpn_wt_vol_changes.COUNT, 4);
1500       END IF;
1501 
1502       IF wms_container_pub.lpn_pack_complete(1) THEN
1503         NULL;
1504       END IF;
1505     END IF;
1506 
1507     -- Bug 2355294
1508     -- Clear the Label Printing Record Structure
1509     inv_label_pvt1.g_rcv_label_print_rec_tb.DELETE;
1510     COMMIT;
1511   END rcv_clear_global;
1512 
1513   PROCEDURE init_form_values(
1514     p_org_id                    IN            NUMBER
1515   , x_inv_org_id                OUT NOCOPY    NUMBER ----bug 5195963
1516   , x_org_name                  OUT NOCOPY    VARCHAR2
1517   , x_org_location              OUT NOCOPY    VARCHAR2
1518   , x_sob_id                    OUT NOCOPY    NUMBER
1519   , x_ussgl_value               OUT NOCOPY    VARCHAR2
1520   , x_period_name               OUT NOCOPY    VARCHAR2
1521   , x_gl_date                   OUT NOCOPY    DATE
1522   , x_category_set_id           OUT NOCOPY    NUMBER
1523   , x_structure_id              OUT NOCOPY    NUMBER
1524   , x_user_id                   OUT NOCOPY    NUMBER
1525   , x_logon_id                  OUT NOCOPY    NUMBER
1526   , x_creation_date             OUT NOCOPY    DATE
1527   , x_update_date               OUT NOCOPY    DATE
1528   , x_inv_status                OUT NOCOPY    VARCHAR2
1529   , x_po_status                 OUT NOCOPY    VARCHAR2
1530   , x_qa_status                 OUT NOCOPY    VARCHAR2
1531   , x_wip_status                OUT NOCOPY    VARCHAR2
1532   , x_pa_status                 OUT NOCOPY    VARCHAR2
1533   , x_oe_status                 OUT NOCOPY    VARCHAR2
1534   , x_override_routing          OUT NOCOPY    VARCHAR2
1535   , x_transaction_mode          OUT NOCOPY    VARCHAR2
1536   , x_receipt_traveller         OUT NOCOPY    VARCHAR2
1537   , x_receipt_num_code          OUT NOCOPY    VARCHAR2
1538   , x_receipt_num_type          OUT NOCOPY    VARCHAR2
1539   , x_po_num_type               OUT NOCOPY    VARCHAR2
1540   , x_coa_id                    OUT NOCOPY    NUMBER
1541   , x_allow_express             OUT NOCOPY    VARCHAR2
1542   , x_allow_cascade             OUT NOCOPY    VARCHAR2
1543   , x_org_locator_control       OUT NOCOPY    NUMBER
1544   , x_negative_inv_receipt_code OUT NOCOPY    NUMBER
1545   , x_gl_set_of_bks_id          OUT NOCOPY    VARCHAR2
1546   , x_blind_receiving_flag      OUT NOCOPY    VARCHAR2
1547   , x_allow_unordered           OUT NOCOPY    VARCHAR2
1548   , x_display_inverse_rate      OUT NOCOPY    VARCHAR2
1549   , x_currency_code             OUT NOCOPY    VARCHAR2
1550   , x_project_reference_enabled OUT NOCOPY    NUMBER
1551   , x_project_control_level     OUT NOCOPY    NUMBER
1552   , x_effectivity_control       OUT NOCOPY    NUMBER
1553   , x_employee_id               OUT NOCOPY    NUMBER
1554   , x_wms_install_status        OUT NOCOPY    VARCHAR2
1555   , x_wms_purchased             OUT NOCOPY    VARCHAR2
1556   , x_message                   OUT NOCOPY    VARCHAR2
1557   ) IS
1558     l_org_id                NUMBER        := p_org_id;
1559     l_employee_name         VARCHAR2(240);
1560     l_requestor_location_id NUMBER;
1561     l_location_code         VARCHAR2(80);
1562     l_employee_is_buyer     BOOLEAN;
1563     l_is_emp                BOOLEAN;
1564     l_temp                  BOOLEAN;
1565     l_return_status         VARCHAR2(1)   := fnd_api.g_ret_sts_success;
1566     l_msg_count             NUMBER;
1567     l_msg_data              VARCHAR2(400);
1568     l_progress              VARCHAR2(10);
1569     l_debug                 NUMBER        := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
1570   BEGIN
1571     IF (l_debug = 1) THEN
1572       print_debug('enter init_form_values :  10', 1);
1573     END IF;
1574 
1575     l_progress  := '10';
1576 
1577     /* Bug 3440456 */
1578     -- For performance reason this is being as replaced as below.
1579     /*
1580     SELECT ood.set_of_books_id
1581          , sob.currency_code
1582       INTO x_sob_id
1583          , x_currency_code
1584       FROM org_organization_definitions ood, gl_sets_of_books sob
1585      WHERE organization_id = p_org_id
1586        AND sob.set_of_books_id = ood.set_of_books_id;
1587     */
1588     x_inv_org_id := p_org_id; --bug 5195963
1589 
1590     SELECT TO_NUMBER(hoi.org_information1)
1591            , sob.currency_code
1592       INTO x_sob_id
1593            , x_currency_code
1594       FROM hr_organization_information hoi, gl_sets_of_books sob
1595      WHERE hoi.organization_id = p_org_id
1596        AND (hoi.org_information_context || '') = 'Accounting Information'
1597        AND sob.set_of_books_id = to_number(hoi.org_information1);
1598 
1599     l_progress  := '20';
1600 
1601     BEGIN
1602       SELECT location_code
1603         INTO x_org_location
1604         FROM hr_locations hrl, hr_organization_units hou
1605        WHERE hou.location_id = hrl.location_id
1606          AND hou.organization_id = p_org_id;
1607     EXCEPTION
1608       WHEN OTHERS THEN
1609         -- no_data_found, more than one row, etc.
1610         -- for any exception we just don't set org_location
1611         NULL;
1612     END;
1613 
1614     l_progress  := '30';
1615 
1616     IF (l_debug = 1) THEN
1617       print_debug('init_form_values :  20', 4);
1618     END IF;
1619 
1620     po_setup_s1.get_install_status(
1621           x_inv_status
1622         , x_po_status
1623         , x_qa_status
1624         , x_wip_status
1625         , x_oe_status
1626         , x_pa_status);
1627 
1628     IF (l_debug = 1) THEN
1629       print_debug('init_form_values :  30', 4);
1630     END IF;
1631 
1632     l_progress  := '40';
1633     rcv_setup_s2.get_startup_values(
1634       x_sob_id
1635     , l_org_id
1636     , x_org_name
1637     , x_ussgl_value
1638     , x_override_routing
1639     , x_transaction_mode
1640     , x_receipt_traveller
1641     , x_period_name
1642     , x_gl_date
1643     , x_category_set_id
1644     , x_structure_id
1645     , x_receipt_num_code
1646     , x_receipt_num_type
1647     , x_po_num_type
1648     , x_allow_express
1649     , x_allow_cascade
1650     , x_user_id
1651     , x_logon_id
1652     , x_creation_date
1653     , x_update_date
1654     , x_coa_id
1655     , x_org_locator_control
1656     , x_negative_inv_receipt_code
1657     , x_gl_set_of_bks_id
1658     , x_blind_receiving_flag
1659     , x_allow_unordered
1660     );
1661 
1662     IF (l_debug = 1) THEN
1663       print_debug('init_form_values :  40', 4);
1664     END IF;
1665 
1666     l_progress  := '50';
1667 
1668     SELECT user_defined_receipt_num_code
1669          , manual_receipt_num_type
1670       INTO x_receipt_num_code
1671          , x_receipt_num_type
1672       FROM rcv_parameters
1673      WHERE organization_id = p_org_id;
1674 
1675     l_progress  := '60';
1676     fnd_profile.get('DISPLAY_INVERSE_RATE', x_display_inverse_rate);
1677 
1678     IF x_display_inverse_rate IS NULL THEN
1679       x_display_inverse_rate  := 'N';
1680     END IF;
1681 
1682     l_progress  := '70';
1683 
1684     IF p_org_id IS NOT NULL THEN
1685       IF (l_debug = 1) THEN
1686         print_debug('init_form_values :  50', 4);
1687       END IF;
1688 
1689       po_core_s4.get_mtl_parameters(
1690           p_org_id
1691         , NULL
1692         , x_project_reference_enabled
1693         , x_project_control_level);
1694     END IF;
1695 
1696     l_progress  := '80';
1697 
1698     IF (pjm_unit_eff.enabled = 'Y') THEN
1699       x_effectivity_control  := 1;
1700     ELSE
1701       x_effectivity_control  := 2;
1702     END IF;
1703 
1704     l_progress  := '90';
1705 
1706     IF (l_debug = 1) THEN
1707       print_debug('init_form_values :  60', 4);
1708     END IF;
1709 
1710     l_temp := po_employees_sv.get_employee(
1711                   x_employee_id
1712                 , l_employee_name
1713                 , l_requestor_location_id
1714                 , l_location_code
1715                 , l_employee_is_buyer
1716                 , l_is_emp);
1717 
1718     l_progress  := '100';
1719 
1720     -- set wms_installed flag
1721     IF wms_install.check_install(l_return_status, l_msg_count, l_msg_data, p_org_id) THEN
1722       x_wms_install_status  := 'I';
1723     ELSE
1724       x_wms_install_status  := 'U';
1725     END IF;
1726 
1727     l_progress  := '110';
1728 
1729     -- set wms_purchased flag
1730     IF wms_install.check_install(l_return_status, l_msg_count, l_msg_data, NULL) THEN
1731       x_wms_purchased  := 'I';
1732     ELSE
1733       x_wms_purchased  := 'U';
1734     END IF;
1735 
1736     l_progress  := '120';
1737 
1738     IF (l_debug = 1) THEN
1739       print_debug('init_form_values :  70 ', 4);
1740       print_debug('x_wms_install_status = ' || x_wms_install_status, 4);
1741       print_debug('x_wms_purchased = ' || x_wms_purchased, 4);
1742     END IF;
1743 
1744     IF g_rcv_global_var.receipt_num IS NULL THEN
1745       IF (l_debug = 1) THEN
1746         print_debug('init_form_values :  75 ', 4);
1747       END IF;
1748 
1749       inv_rcv_common_apis.rcv_gen_receipt_num(
1750         x_receipt_num                => g_rcv_global_var.receipt_num
1751       , p_organization_id            => p_org_id
1752       , x_return_status              => l_return_status
1753       , x_msg_count                  => l_msg_count
1754       , x_msg_data                   => l_msg_data
1755       );
1756     END IF;
1757 
1758     IF (l_debug = 1) THEN
1759       print_debug('exit init_form_values :  80 ', 1);
1760     END IF;
1761   EXCEPTION
1762     WHEN OTHERS THEN
1763       IF (l_debug = 1) THEN
1764         print_debug('Exitting init_form_values - other exception:' || l_progress || ' ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
1765       END IF;
1766 
1767       --
1768       IF SQLCODE IS NOT NULL THEN
1769         inv_mobile_helper_functions.sql_error('INV_RCV_COMMON_APIS.init_form_values', l_progress, SQLCODE);
1770       END IF;
1771 
1772       --  Get message count and data
1773       fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => l_msg_count, p_data => l_msg_data);
1774       RAISE;
1775   END init_form_values;
1776 
1777   -- Added the overloaded method to return the value of inv patch level
1778   -- and the po patch level to the UI. The earlier method is stubbed out
1779   -- to call this method in order to avoid pre-reqs.
1780   PROCEDURE init_rcv_ui_startup_values(
1781     p_organization_id     IN            NUMBER
1782   , x_org_id              OUT NOCOPY    NUMBER
1783   , x_org_location        OUT NOCOPY    VARCHAR2
1784   , x_org_locator_control OUT NOCOPY    NUMBER
1785   , x_manual_po_num_type  OUT NOCOPY    VARCHAR2
1786   , x_wms_install_status  OUT NOCOPY    VARCHAR2
1787   , x_wms_purchased       OUT NOCOPY    VARCHAR2
1788   , x_return_status       OUT NOCOPY    VARCHAR2
1789   , x_msg_data            OUT NOCOPY    VARCHAR2
1790   , x_inv_patch_level     OUT NOCOPY    NUMBER
1791   , x_po_patch_level      OUT NOCOPY    NUMBER
1792   , x_wms_patch_level     OUT NOCOPY    NUMBER
1793   ) IS
1794     l_msg_count NUMBER;
1795     l_progress  VARCHAR2(5);
1796     l_debug     NUMBER      := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
1797   BEGIN
1798     IF (l_debug = 1) THEN
1799       print_debug('Entering init_rcv_ui_startup_values:' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
1800     END IF;
1801 
1802     x_return_status    := fnd_api.g_ret_sts_success;
1803     x_org_id           := p_organization_id;
1804     x_inv_patch_level  := inv_rcv_common_apis.g_inv_patch_level;
1805     x_po_patch_level   := inv_rcv_common_apis.g_po_patch_level;
1806     x_wms_patch_level  := inv_rcv_common_apis.g_wms_patch_level;
1807     l_progress         := '10';
1808 
1809     /* Bug 3440456 */
1810     -- For performance reason this is being as replaced as below.
1811     /*
1812     SELECT ood.set_of_books_id
1813          , sob.currency_code
1814       INTO g_po_startup_value.sob_id
1815          , g_po_startup_value.currency_code
1816       FROM org_organization_definitions ood, gl_sets_of_books sob
1817      WHERE organization_id = p_organization_id
1818        AND sob.set_of_books_id = ood.set_of_books_id;
1819     */
1820 
1821     SELECT TO_NUMBER(hoi.org_information1)
1822            , sob.currency_code
1823       INTO g_po_startup_value.sob_id
1824          , g_po_startup_value.currency_code
1825       FROM hr_organization_information hoi, gl_sets_of_books sob
1826      WHERE hoi.organization_id = p_organization_id
1827        AND (hoi.org_information_context || '') = 'Accounting Information'
1828        AND sob.set_of_books_id = to_number(hoi.org_information1);
1829 
1830     l_progress         := '20';
1831 
1832     -- set default org location
1833     BEGIN
1834       SELECT location_code
1835         INTO x_org_location
1836         FROM hr_locations hrl, hr_organization_units hou
1837        WHERE hou.location_id = hrl.location_id
1838          AND hou.organization_id = p_organization_id;
1839 
1840       l_progress  := '40';
1841     EXCEPTION
1842       WHEN OTHERS THEN
1843         -- no_data_found, more than one row, etc.
1844         -- for any exception we just don't set org_location
1845         NULL;
1846     END;
1847 
1848     -- set stock locator control code
1849     l_progress         := '50';
1850 
1851     SELECT NVL(stock_locator_control_code, 1)
1852       INTO x_org_locator_control
1853       FROM mtl_parameters
1854      WHERE organization_id = p_organization_id;
1855 
1856     l_progress         := '60';
1857 
1858     -- set manual po number type
1859     BEGIN
1860       l_progress  := '70';
1861 
1862       SELECT NVL(manual_po_num_type, 'ALPHANUMERIC')
1863         INTO x_manual_po_num_type
1864         FROM po_system_parameters
1865        WHERE ROWNUM = 1;
1866 
1867       l_progress  := '80';
1868     EXCEPTION
1869       WHEN OTHERS THEN
1870         x_manual_po_num_type  := 'ALPHANUMERIC';
1871     END;
1872 
1873     -- set wms_installed flag
1874     l_progress         := '90';
1875 
1876     IF wms_install.check_install(x_return_status, l_msg_count, x_msg_data, p_organization_id) THEN
1877       x_wms_install_status  := 'I';
1878     ELSE
1879       x_wms_install_status  := 'U';
1880     END IF;
1881 
1882     IF x_return_status = fnd_api.g_ret_sts_error THEN
1883       fnd_message.set_name('INV', 'WMS_INSTALL_CHK_ERROR'); -- error checking ems installation
1884       fnd_msg_pub.ADD;
1885       RAISE fnd_api.g_exc_error;
1886     END IF;
1887 
1888     l_progress         := '110';
1889 
1890     -- set wms_purchased flag
1891     IF wms_install.check_install(x_return_status, l_msg_count, x_msg_data, NULL) THEN
1892       x_wms_purchased  := 'I';
1893     ELSE
1894       x_wms_purchased  := 'U';
1895     END IF;
1896 
1897     IF x_return_status = fnd_api.g_ret_sts_error THEN
1898       fnd_message.set_name('INV', 'WMS_INSTALL_CHK_ERROR'); -- error checking ems installation
1899       fnd_msg_pub.ADD;
1900       RAISE fnd_api.g_exc_error;
1901     END IF;
1902 
1903     l_progress         := '120';
1904     -- generate the group id to be used for all the records till the
1905     -- user revisits the menu.
1906     gen_txn_group_id;
1907     l_progress         := '130';
1908 
1909     IF (l_debug = 1) THEN
1910       print_debug('Exitting init_rcv_ui_startup_values:' || l_progress || ' ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
1911     END IF;
1912   EXCEPTION
1913     WHEN fnd_api.g_exc_error THEN
1914       x_return_status  := fnd_api.g_ret_sts_error;
1915 
1916       IF (l_debug = 1) THEN
1917         print_debug('Exitting init_rcv_ui_startup_values - execution error:' || l_progress || ' ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
1918       END IF;
1919     WHEN fnd_api.g_exc_unexpected_error THEN
1920       x_return_status  := fnd_api.g_ret_sts_unexp_error;
1921 
1922       IF (l_debug = 1) THEN
1923         print_debug('Exitting init_rcv_ui_startup_values - unexpected error:' || l_progress || ' ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
1924       END IF;
1925     WHEN OTHERS THEN
1926       x_return_status  := fnd_api.g_ret_sts_unexp_error;
1927 
1928       IF (l_debug = 1) THEN
1929         print_debug('Exitting init_rcv_ui_startup_values - other exception:' || l_progress || ' ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
1930       END IF;
1931 
1932       --
1933       IF SQLCODE IS NOT NULL THEN
1934         inv_mobile_helper_functions.sql_error('INV_RCV_COMMON_APIS.init_rcv_ui_startup_values', l_progress, SQLCODE);
1935       END IF;
1936 
1937       --  Get message count and data
1938       fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => l_msg_count, p_data => x_msg_data);
1939   END init_rcv_ui_startup_values;
1940 
1941   PROCEDURE init_rcv_ui_startup_values(
1942     p_organization_id     IN            NUMBER
1943   , x_org_id              OUT NOCOPY    NUMBER
1944   , x_org_location        OUT NOCOPY    VARCHAR2
1945   , x_org_locator_control OUT NOCOPY    NUMBER
1946   , x_manual_po_num_type  OUT NOCOPY    VARCHAR2
1947   , x_wms_install_status  OUT NOCOPY    VARCHAR2
1948   , x_wms_purchased       OUT NOCOPY    VARCHAR2
1949   , x_return_status       OUT NOCOPY    VARCHAR2
1950   , x_msg_data            OUT NOCOPY    VARCHAR2
1951   ) IS
1952     l_inv_patch_level NUMBER;
1953     l_po_patch_level  NUMBER;
1954     l_wms_patch_level NUMBER;
1955   BEGIN
1956     init_rcv_ui_startup_values(
1957       p_organization_id            => p_organization_id
1958     , x_org_id                     => x_org_id
1959     , x_org_location               => x_org_location
1960     , x_org_locator_control        => x_org_locator_control
1961     , x_manual_po_num_type         => x_manual_po_num_type
1962     , x_wms_install_status         => x_wms_install_status
1963     , x_wms_purchased              => x_wms_purchased
1964     , x_return_status              => x_return_status
1965     , x_msg_data                   => x_msg_data
1966     , x_inv_patch_level            => l_inv_patch_level
1967     , x_po_patch_level             => l_po_patch_level
1968     , x_wms_patch_level            => l_wms_patch_level
1969     );
1970   END init_rcv_ui_startup_values;
1971 
1972   /*************************************************
1973   * Name: get_po_routing_id
1974   * This API returns routing id for a given PO header ID
1975   * Routing ID is defined at PO line-location level (po_line_locations_all)
1976   * We use the following rule to set headers routing ID
1977   * If there is one line detail needs inspection the entire PO needs inspection
1978   * elsif there is one line detail needs direct receiving the entire PO direct
1979   * else (all line detail are standard) the entire PO is standard
1980   * rounting lookups: 1. standard   2. Inspect  3. Direct
1981   ******************************************************/
1982   PROCEDURE get_po_routing_id(
1983     x_po_routing_id OUT NOCOPY    NUMBER
1984   , x_is_expense    OUT NOCOPY    VARCHAR2
1985   , p_po_header_id  IN            NUMBER
1986   , p_po_release_id IN            NUMBER
1987   , p_po_line_id    IN            NUMBER
1988   , p_item_id       IN            NUMBER
1989   , p_item_desc     IN            VARCHAR2 DEFAULT NULL
1990   ) IS
1991     --     l_po_routing_id NUMBER := 3; It should not be initialized with value 3,
1992     --                  otherwise, the searching will not go
1993     --                  to item level/org level.
1994     l_po_routing_id    NUMBER;
1995     l_po_ll_routing_id NUMBER;
1996     l_dest_context     VARCHAR2(30);
1997     l_po_dest_context  VARCHAR2(30);
1998 
1999    /* Bug 3812507: Changing the select query in the cursors po_ll_routing_cur
2000    and pod_dest_context_cur to improve performance */
2001 
2002     CURSOR po_ll_routing_cur IS
2003       --   SELECT Nvl(poll.receiving_routing_id, 1) Value 1 should not be selected
2004       --                        in case of Nvl, otherwise the
2005       --                        searching mechanism will not
2006       --                        go to item/org level.
2007 	SELECT poll.receiving_routing_id
2008 	-- p_po_release_id is null and p_po_line_id is null
2009 	FROM po_line_locations poll, po_lines pol
2010 	WHERE pol.po_header_id = p_po_header_id
2011 	AND poll.po_line_id = pol.po_line_id
2012 	AND p_po_release_id is NULL
2013 	AND p_po_line_id is null
2014 	AND (pol.item_id = p_item_id OR (p_item_id IS NULL
2015 	AND pol.item_id IS NULL AND pol.item_description = p_item_desc))
2016 	AND NVL(poll.approved_flag, 'N') = 'Y'
2017 	AND NVL(poll.cancel_flag, 'N') = 'N'
2018 	AND NVL(poll.closed_code, 'OPEN')NOT IN ('CLOSED','CLOSED FOR RECEIVING','FINALLY CLOSED')
2019 	/*Fix for bug #4755862*/
2020 	AND poll.shipment_type IN('STANDARD', 'BLANKET', 'SCHEDULED')
2021 	UNION ALL
2022 	SELECT poll.receiving_routing_id
2023 	-- p_po_release_id is null and p_po_line_id is not null
2024 	FROM po_line_locations poll, po_lines pol
2025 	WHERE poll.po_header_id = p_po_header_id
2026 	AND poll.po_line_id = pol.po_line_id
2027 	AND p_po_release_id is NULL
2028 	AND (p_po_line_id is not null AND poll.po_line_id = p_po_line_id)
2029 	AND (pol.ITEM_ID = p_item_id OR (p_item_id IS NULL
2030 	AND pol.item_id IS NULL AND pol.item_description = p_item_desc ))
2031 	AND NVL(poll.approved_flag, 'N') = 'Y'
2032 	AND NVL(poll.cancel_flag, 'N') = 'N'
2033 	AND NVL(poll.closed_code, 'OPEN') NOT IN ('CLOSED','CLOSED FOR RECEIVING','FINALLY CLOSED')
2034 	/*Fix for bug #4755862*/
2035 	AND poll.shipment_type IN('STANDARD', 'BLANKET', 'SCHEDULED')
2036 	UNION ALL
2037 	SELECT poll.receiving_routing_id
2038 	-- p_po_release_id is not null
2039 	FROM po_line_locations poll, po_lines pol
2040 	WHERE poll.po_header_id = p_po_header_id
2041 	AND poll.po_line_id = pol.po_line_id
2042 	AND (p_po_release_id is NOT NULL AND poll.po_release_id = p_po_release_id)
2043 	AND (p_po_line_id is null or poll.po_line_id = p_po_line_id)
2044 	AND (pol.item_id = p_item_id OR (p_item_id IS NULL
2045 	AND pol.item_id IS NULL AND pol.item_description = p_item_desc))
2046 	AND NVL(poll.approved_flag, 'N') = 'Y'
2047 	AND NVL(poll.cancel_flag, 'N') = 'N'
2048 	AND NVL(poll.closed_code, 'OPEN') NOT IN ('CLOSED','CLOSED FOR RECEIVING','FINALLY CLOSED')
2049 	/*Fix for bug #4755862*/
2050 	AND poll.shipment_type IN('STANDARD', 'BLANKET', 'SCHEDULED');
2051 /*
2052       SELECT poll.receiving_routing_id
2053         FROM po_line_locations poll, po_lines pol
2054        WHERE poll.po_header_id = p_po_header_id
2055          AND NVL(poll.po_release_id, -1) = NVL(p_po_release_id, NVL(poll.po_release_id, -1))
2056          AND NVL(poll.po_line_id, -1) = NVL(p_po_line_id, NVL(poll.po_line_id, -1))
2057          --AND pol.item_id = p_item_id
2058          AND (pol.item_id = p_item_id
2059               OR (p_item_id IS NULL
2060                   AND pol.item_id IS NULL
2061                   AND pol.item_description = p_item_desc
2062                  )
2063              )
2064          AND pol.po_line_id = poll.po_line_id
2065          AND NVL(poll.approved_flag, 'N') = 'Y'
2066          AND NVL(poll.cancel_flag, 'N') = 'N'
2067          AND NVL(poll.closed_code, 'OPEN') <> 'FINALLY CLOSED'
2068          AND poll.shipment_type IN ('STANDARD', 'BLANKET', 'SCHEDULED');
2069 */
2070 
2071     CURSOR pod_dest_context_cur IS
2072 	SELECT DISTINCT NVL(POD.DESTINATION_TYPE_CODE,POD.DESTINATION_CONTEXT)
2073 	-- p_po_release_id is null and p_po_line_id is null
2074 	FROM PO_DISTRIBUTIONS POD, PO_LINES POL, PO_LINE_LOCATIONS POLL
2075 	WHERE POL.PO_HEADER_ID = p_po_header_id
2076 	AND POLL.PO_LINE_ID = POL.PO_LINE_ID
2077 	AND POD.LINE_LOCATION_ID = POLL.LINE_LOCATION_ID
2078 	AND p_po_release_id is NULL
2079 	AND p_po_line_id is NULL
2080 	AND (POL.ITEM_ID = p_item_id OR (p_item_id IS NULL
2081 	AND POL.ITEM_ID IS NULL AND POL.ITEM_DESCRIPTION = p_item_desc ))
2082 	AND NVL(POLL.APPROVED_FLAG,'N') = 'Y'
2083 	AND NVL(POLL.CANCEL_FLAG,'N') = 'N'
2084 	AND NVL(POLL.CLOSED_CODE,'OPEN') <> 'FINALLY CLOSED'
2085 	AND POLL.SHIPMENT_TYPE IN ( 'STANDARD','BLANKET','SCHEDULED' )
2086 	UNION ALL
2087 	SELECT DISTINCT NVL(POD.DESTINATION_TYPE_CODE,POD.DESTINATION_CONTEXT)
2088 	-- p_po_release_id is null and p_po_line_id is not null
2089 	FROM PO_DISTRIBUTIONS POD, PO_LINES POL, PO_LINE_LOCATIONS POLL
2090 	WHERE POLL.PO_HEADER_ID = p_po_header_id
2091 	AND POLL.PO_LINE_ID = POL.PO_LINE_ID
2092 	AND POD.LINE_LOCATION_ID = POLL.LINE_LOCATION_ID
2093 	AND p_po_release_id is NULL
2094 	AND (p_po_line_id is NOT NULL AND POLL.PO_LINE_ID = p_po_line_id)
2095 	AND (POL.ITEM_ID = p_item_id OR (p_item_id IS NULL
2096 	AND POL.ITEM_ID IS NULL AND POL.ITEM_DESCRIPTION = p_item_desc ))
2097 	AND NVL(POLL.APPROVED_FLAG,'N') = 'Y'
2098 	AND NVL(POLL.CANCEL_FLAG,'N') = 'N'
2099 	AND NVL(POLL.CLOSED_CODE,'OPEN') <> 'FINALLY CLOSED'
2100 	AND POLL.SHIPMENT_TYPE IN ( 'STANDARD','BLANKET','SCHEDULED' )
2101 	UNION ALL
2102 	SELECT DISTINCT NVL(POD.DESTINATION_TYPE_CODE,POD.DESTINATION_CONTEXT)
2103 	-- p_po_release_id is not NULL
2104 	FROM PO_DISTRIBUTIONS POD, PO_LINES POL, PO_LINE_LOCATIONS POLL
2105 	WHERE POLL.PO_HEADER_ID = p_po_header_id
2106 	AND POLL.PO_LINE_ID = POL.PO_LINE_ID
2107 	AND POD.LINE_LOCATION_ID = POLL.LINE_LOCATION_ID
2108 	AND (p_po_release_id is NOT NULL AND POLL.PO_RELEASE_ID = p_po_release_id)
2109 	AND (p_po_line_id is null or poll.po_line_id = p_po_line_id)
2110 	AND (pol.item_id = p_item_id OR (p_item_id IS NULL
2111 	AND pol.item_id IS NULL AND pol.item_description = p_item_desc))
2112 	AND NVL(POLL.APPROVED_FLAG,'N') = 'Y'
2113 	AND NVL(POLL.CANCEL_FLAG,'N') = 'N'
2114 	AND NVL(POLL.CLOSED_CODE,'OPEN') <> 'FINALLY CLOSED'
2115 	AND POLL.SHIPMENT_TYPE IN ( 'STANDARD','BLANKET','SCHEDULED' );
2116 /*
2117        SELECT DISTINCT Nvl(pod.destination_type_code,pod.destination_context)
2118                  FROM po_distributions pod, po_lines pol, po_line_locations poll
2119                 WHERE pod.po_header_id = p_po_header_id
2120                   AND NVL(poll.po_release_id, -1) = NVL(p_po_release_id, NVL(poll.po_release_id, -1))
2121                   AND NVL(poll.po_line_id, -1) = NVL(p_po_line_id, NVL(poll.po_line_id, -1))
2122                   --AND pol.item_id = p_item_id
2123                   AND pod.line_location_id = poll.line_location_id
2124                   AND (pol.item_id = p_item_id
2125                        OR (p_item_id IS NULL
2126                            AND pol.item_id IS NULL
2127                            AND pol.item_description = p_item_desc
2128                           )
2129                       )
2130                   AND pol.po_line_id = poll.po_line_id
2131                   AND NVL(poll.approved_flag, 'N') = 'Y'
2132                   AND NVL(poll.cancel_flag, 'N') = 'N'
2133                   AND NVL(poll.closed_code, 'OPEN') <> 'FINALLY CLOSED'
2134                   AND poll.shipment_type IN ('STANDARD', 'BLANKET', 'SCHEDULED');
2135 */
2136 
2137     l_debug            NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
2138   BEGIN
2139     OPEN po_ll_routing_cur;
2140 
2141     IF (l_debug = 1) THEN
2142       print_debug('header_id ' || TO_CHAR(p_po_header_id), 4);
2143       print_debug('line_id ' || TO_CHAR(p_po_line_id), 4);
2144       print_debug('item_id ' || TO_CHAR(p_item_id), 4);
2145       print_debug('item_desc ' || p_item_desc, 4);
2146     END IF;
2147 
2148     x_is_expense     := 'N';
2149 
2150     LOOP
2151       FETCH po_ll_routing_cur INTO l_po_ll_routing_id;
2152       EXIT WHEN po_ll_routing_cur%NOTFOUND;
2153 
2154       IF (l_debug = 1) THEN
2155         print_debug('l_po_ll_routing_id ' || l_po_ll_routing_id, 4);
2156       END IF;
2157 
2158       IF l_po_ll_routing_id = 2 THEN -- inspection
2159         l_po_routing_id  := 2;
2160         EXIT; -- inspection overrides everything
2161       ELSIF l_po_ll_routing_id = 1 THEN -- standard
2162         l_po_routing_id  := 1; -- standard overrides direct
2163       ELSIF(l_po_ll_routing_id = 3
2164             AND NVL(l_po_routing_id, 3) >= 3) THEN -- direct
2165         l_po_routing_id  := 3; -- direct is default if not null
2166       END IF;
2167     END LOOP;
2168 
2169     CLOSE po_ll_routing_cur;
2170 
2171     IF (l_debug = 1) THEN
2172       print_debug('routing_id ' || TO_CHAR(l_po_routing_id), 4);
2173     END IF;
2174 
2175     /*
2176     if l_po_ll_routing_id is not null then
2177          l_po_routing_id := l_po_ll_routing_id;
2178     end if;
2179       */
2180     x_po_routing_id  := l_po_routing_id;
2181     l_dest_context   := 'INITIAL';
2182     OPEN pod_dest_context_cur;
2183 
2184     LOOP
2185       FETCH pod_dest_context_cur INTO l_po_dest_context;
2186       EXIT WHEN pod_dest_context_cur%NOTFOUND;
2187 
2188       IF (l_debug = 1) THEN
2189         print_debug('pod dest context ' || l_po_dest_context, 4);
2190       END IF;
2191 
2192       IF l_dest_context <> l_po_dest_context THEN -- inspection
2193         l_dest_context  := l_po_dest_context;
2194       END IF;
2195     END LOOP;
2196 
2197     IF l_dest_context = 'EXPENSE' THEN
2198       x_is_expense  := 'Y';
2199     END IF;
2200   EXCEPTION
2201     WHEN OTHERS THEN
2202       IF po_ll_routing_cur%ISOPEN THEN
2203         CLOSE po_ll_routing_cur;
2204       END IF;
2205 
2206       IF pod_dest_context_cur%ISOPEN THEN
2207         CLOSE po_ll_routing_cur;
2208       END IF;
2209 
2210       IF (l_debug = 1) THEN
2211         print_debug('Exception while getting the shipment routing', 4);
2212       END IF;
2213 
2214       RAISE;
2215   END get_po_routing_id;
2216 
2217   /*************************************************
2218   * Name: get_asn_routing_id
2219   * This API returns routing id for a given shipment_header_ID,
2220   * lpn_id, po_header_id combination.
2221   * PO_header_id, po_line_id and item_id are queried based on the combination,
2222   * and then passed to get_po_routing_id.
2223   * If any of the lines has a direct routing, this API will return direct.
2224   *******************************************************/
2225   PROCEDURE get_asn_routing_id(
2226     x_asn_routing_id     OUT NOCOPY    NUMBER
2227   , x_return_status      OUT NOCOPY    VARCHAR2
2228   , x_msg_count          OUT NOCOPY    NUMBER
2229   , x_msg_data           OUT NOCOPY    VARCHAR2
2230   , p_shipment_header_id IN            NUMBER
2231   , p_lpn_id             IN            NUMBER
2232   , p_po_header_id       IN            NUMBER
2233   ) IS
2234     l_return_status  VARCHAR2(1)   := fnd_api.g_ret_sts_success;
2235     l_msg_count      NUMBER;
2236     l_msg_data       VARCHAR2(400);
2237     l_progress       VARCHAR2(10);
2238     l_po_header_id   NUMBER;
2239     l_po_line_id     NUMBER;
2240     l_po_release_id  NUMBER;
2241     l_item_id        NUMBER;
2242     l_asn_routing_id NUMBER        := 3; -- direct
2243     l_is_expense     VARCHAR2(1);
2244     l_temp_asn_routing_id NUMBER := 3; --Bug 5500463
2245 
2246     CURSOR l_curs_asn_lpn_content IS
2247       SELECT NVL(p_po_header_id, rsl.po_header_id) po_header_id
2248            , rsl.po_line_id po_line_id
2249            , rsl.po_release_id po_release_id
2250            , rsl.item_id item_id
2251         FROM rcv_shipment_lines rsl
2252        WHERE rsl.shipment_header_id = p_shipment_header_id
2253          AND rsl.po_header_id = NVL(p_po_header_id, rsl.po_header_id)
2254          AND(EXISTS(SELECT 1
2255                       FROM wms_lpn_contents wlc
2256                      WHERE wlc.source_line_id = rsl.po_line_id
2257                        AND wlc.parent_lpn_id = p_lpn_id)
2258              OR p_lpn_id IS NULL);
2259 
2260     -- bug 3213241
2261     CURSOR l_curs_asn_lpn_content_new IS
2262       SELECT NVL(p_po_header_id, rsl.po_header_id) po_header_id
2263            , rsl.po_line_id po_line_id
2264            , rsl.po_release_id po_release_id
2265            , rsl.item_id item_id
2266         FROM rcv_shipment_lines rsl
2267        WHERE rsl.shipment_header_id = p_shipment_header_id
2268          AND rsl.po_header_id = NVL(p_po_header_id, rsl.po_header_id)
2269 	AND (( ( rsl.asn_lpn_id IS NOT NULL
2270 		 AND rsl.asn_lpn_id in
2271 		        (SELECT wlpn.lpn_id
2272 			 FROM wms_license_plate_numbers wlpn
2273 			 start with lpn_id = p_lpn_id
2274 			 CONNECT BY parent_lpn_id = PRIOR lpn_id
2275 			 )
2276 		  )
2277 		OR (rsl.asn_lpn_id IS NULL
2278 		    AND exists (SELECT 1
2279 				FROM wms_lpn_contents wlc
2280 				WHERE wlc.source_line_id = rsl.po_line_id
2281 				AND wlc.parent_lpn_id = p_lpn_id)
2282 		    )
2283               )
2284               OR
2285               (p_lpn_id IS NULL)
2286              );
2287 
2288     l_debug          NUMBER        := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
2289   BEGIN
2290     IF (l_debug = 1) THEN
2291       print_debug('Enter get_asn_routing_id 10 ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
2292       print_debug('p_shipment_header_id => ' || p_shipment_header_id, 4);
2293       print_debug('p_lpn_id => ' || p_lpn_id, 4);
2294       print_debug('p_po_header_id => ' || p_po_header_id, 4);
2295     END IF;
2296 
2297     x_return_status   := fnd_api.g_ret_sts_success;
2298     l_progress        := '10';
2299 
2300     IF ((inv_rcv_common_apis.g_wms_patch_level >= inv_rcv_common_apis.g_patchset_j) or
2301        (inv_rcv_common_apis.g_inv_patch_level >= inv_rcv_common_apis.g_patchset_j)) THEN
2302        IF (l_debug = 1) THEN
2303           print_debug('In GET ASN ROUTING ID for/after patchsetJ  ', 4);
2304        END IF;
2305        OPEN l_curs_asn_lpn_content_new;
2306     ELSE
2307        IF (l_debug = 1) THEN
2308           print_debug('In GET ASN ROUTING ID before patchsetJ  ', 4);
2309        END IF;
2310        OPEN l_curs_asn_lpn_content;
2311     END IF;
2312 
2313     l_progress        := '20';
2314 
2315     LOOP
2316        IF ((inv_rcv_common_apis.g_wms_patch_level >= inv_rcv_common_apis.g_patchset_j) or
2317        (inv_rcv_common_apis.g_inv_patch_level >= inv_rcv_common_apis.g_patchset_j)) THEN
2318          FETCH l_curs_asn_lpn_content_new INTO l_po_header_id, l_po_line_id, l_po_release_id, l_item_id;
2319          EXIT WHEN l_curs_asn_lpn_content_new%NOTFOUND;
2320        ElSE
2321          FETCH l_curs_asn_lpn_content INTO l_po_header_id, l_po_line_id, l_po_release_id, l_item_id;
2322          EXIT WHEN l_curs_asn_lpn_content%NOTFOUND;
2323        END IF;
2324 
2325       l_progress  := '30';
2326 
2327       IF (l_debug = 1) THEN
2328         print_debug('Paramters for calling get_po_routing_id : ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 4);
2329         print_debug('l_po_header_id => ' || l_po_header_id, 4);
2330         print_debug('l_po_release_id => ' || l_po_release_id, 4);
2331         print_debug('l_po_line_id => ' || l_po_line_id, 4);
2332         print_debug('l_item_id => ' || l_item_id, 4);
2333       END IF;
2334 
2335       get_po_routing_id(
2336         x_po_routing_id              => l_asn_routing_id
2337       , x_is_expense                 => l_is_expense
2338       , p_po_header_id               => l_po_header_id
2339       , p_po_release_id              => l_po_release_id
2340       , p_po_line_id                 => l_po_line_id
2341       , p_item_id                    => l_item_id
2342       );
2343 
2344       IF (l_debug = 1) THEN
2345         print_debug('Result of get_po_routing_id : l_asn_routing_id = ' || l_asn_routing_id, 4);
2346       END IF;
2347 
2348       l_progress  := '40';
2349 
2350       /*Begin Bug 5500463
2351       IF l_asn_routing_id <> 3 THEN -- direct found
2352         EXIT;
2353       END IF; */
2354       IF l_asn_routing_id = 2 THEN
2355         EXIT;
2356       ELSIF l_asn_routing_id = 1 THEN
2357         NULL;
2358       ELSIF l_asn_routing_id = 3 THEN
2359         IF (l_temp_asn_routing_id < 3) THEN
2360           l_asn_routing_id := l_temp_asn_routing_id;
2361         END IF;
2362       END IF;
2363 
2364       IF (l_asn_routing_id < 3) THEN
2365         l_temp_asn_routing_id := l_asn_routing_id;
2366       END IF;
2367       --End Bug 5500463
2368     END LOOP;
2369 
2370     l_progress        := '50';
2371     IF ((inv_rcv_common_apis.g_wms_patch_level >= inv_rcv_common_apis.g_patchset_j) or
2372        (inv_rcv_common_apis.g_inv_patch_level >= inv_rcv_common_apis.g_patchset_j)) THEN
2373        CLOSE l_curs_asn_lpn_content_new;
2374     ELSE
2375        CLOSE l_curs_asn_lpn_content;
2376     END IF;
2377 
2378     l_progress        := '60';
2379     x_asn_routing_id  := l_asn_routing_id;
2380 
2381     IF (l_debug = 1) THEN
2382       print_debug('Complete get_asn_routing_id : ' || x_asn_routing_id || ' ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
2383     END IF;
2384   EXCEPTION
2385     WHEN OTHERS THEN
2386       x_return_status  := fnd_api.g_ret_sts_unexp_error;
2387 
2388       IF l_curs_asn_lpn_content%ISOPEN THEN
2389         CLOSE l_curs_asn_lpn_content;
2390       END IF;
2391 
2392       IF l_curs_asn_lpn_content_new%ISOPEN THEN
2393         CLOSE l_curs_asn_lpn_content_new;
2394       END IF;
2395 
2396       IF SQLCODE IS NOT NULL THEN
2397         inv_mobile_helper_functions.sql_error('INV_RCV_COMMON_APIS.get_asn_routing_id', l_progress, SQLCODE);
2398       END IF;
2399 
2400       fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
2401 
2402       IF (l_debug = 1) THEN
2403         print_debug('get_asn_routing_id: Other exception ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
2404       END IF;
2405   END get_asn_routing_id;
2406 
2407   /*************************************************
2408   * Name: get_intshp_routing_id
2409   * This API returns routing id for a given shipment header ID
2410   * Routing ID is defined at shipment line level (rcv_shipment_lines)
2411   * We use the following rule to set headers routing ID
2412   * If there is one line detail needs inspection the entire shipment needs inspection
2413   * elsif there is one line detail needs direct receiving the entire shipmentneeds direct
2414   * else (all line detail are standard) the entire shipment is standard
2415   * rounting lookups: 1. standard   2. Inspect  3. Direct
2416   ******************************************************/
2417   PROCEDURE get_intshp_routing_id(
2418     x_intshp_routing_id  OUT NOCOPY    NUMBER
2419   , x_is_expense         OUT NOCOPY    VARCHAR2
2420   , p_shipment_header_id IN            NUMBER
2421   , p_item_id            IN            NUMBER
2422   , p_item_desc          IN            VARCHAR2 DEFAULT NULL
2423   ) IS
2424     l_intshp_routing_id NUMBER;
2425     l_intran_routing_id NUMBER;
2426     l_po_header_id      NUMBER;
2427     l_is_expense        VARCHAR2(3) := 'N';
2428 
2429     CURSOR intshp_rsl_routing_cur IS
2430       SELECT NVL(routing_header_id, 1)
2431            , po_header_id
2432         FROM rcv_shipment_lines
2433        WHERE shipment_header_id = p_shipment_header_id
2434          AND(
2435              (item_id IS NULL
2436               AND p_item_id IS NULL
2437               AND item_description = p_item_desc
2438               AND source_document_code = 'PO')
2439              OR (item_id = NVL(p_item_id, item_id))
2440             );
2441 
2442     l_debug             NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
2443   BEGIN
2444     OPEN intshp_rsl_routing_cur;
2445 
2446     IF (l_debug = 1) THEN
2447       print_debug('shipment header_id ' || TO_CHAR(p_shipment_header_id), 4);
2448       print_debug('item_id ' || TO_CHAR(p_item_id), 4);
2449     END IF;
2450 
2451     LOOP
2452       FETCH intshp_rsl_routing_cur INTO l_intshp_routing_id, l_po_header_id;
2453       EXIT WHEN intshp_rsl_routing_cur%NOTFOUND;
2454 
2455       IF (l_debug = 1) THEN
2456         print_debug('l_intshp_routing_id: ' || l_intshp_routing_id, 4);
2457       END IF;
2458 
2459       IF l_intshp_routing_id = 2 THEN -- inspection
2460         l_intran_routing_id  := 2;
2461         EXIT; -- inspection overrides everything
2462       ELSIF l_intshp_routing_id = 1 THEN -- standard
2463         l_intran_routing_id  := 1; -- standard overrides direct
2464       ELSIF(l_intshp_routing_id = 3
2465             AND NVL(l_intran_routing_id, 3) >= 3) THEN -- direct
2466         l_intran_routing_id  := 3; -- direct is default if not null
2467       END IF;
2468 
2469       IF l_po_header_id IS NOT NULL THEN
2470         get_po_routing_id(
2471             l_intshp_routing_id
2472           , l_is_expense
2473           , l_po_header_id
2474           , NULL
2475           , NULL
2476           , p_item_id
2477           , p_item_desc);
2478       END IF;
2479     END LOOP;
2480 
2481     CLOSE intshp_rsl_routing_cur;
2482 
2483     IF (l_debug = 1) THEN
2484       print_debug('routing_id ' || TO_CHAR(l_intran_routing_id), 4);
2485       print_debug('is_expesne ' || l_is_expense, 4);
2486     END IF;
2487 
2488     IF l_is_expense = 'N' THEN
2489        BEGIN
2490 	  SELECT 'Y'
2491 	    INTO l_is_expense
2492 	    FROM po_requisition_lines prl
2493 	       , rcv_shipment_lines rsl
2494 	    WHERE prl.requisition_line_id = rsl.requisition_line_id
2495 	    AND prl.destination_type_code = 'EXPENSE'
2496 	    AND rsl.shipment_header_id = p_shipment_header_id
2497 	    AND rsl.item_id = NVL(p_item_id, rsl.item_id)
2498 	    AND ROWNUM = 1;
2499        EXCEPTION
2500 	  WHEN OTHERS THEN
2501 	     l_is_expense := 'N';
2502        END;
2503     END IF;
2504 
2505     IF (l_debug = 1) THEN
2506       print_debug('REQ:is_expesne ' || l_is_expense, 4);
2507     END IF;
2508 
2509     x_intshp_routing_id  := l_intran_routing_id;
2510     x_is_expense := l_is_expense;
2511   EXCEPTION
2512     WHEN OTHERS THEN
2513       IF intshp_rsl_routing_cur%ISOPEN THEN
2514         CLOSE intshp_rsl_routing_cur;
2515       END IF;
2516 
2517       IF (l_debug = 1) THEN
2518         print_debug('Exception while getting the shipment routing', 4);
2519       END IF;
2520 
2521       RAISE;
2522   END get_intshp_routing_id;
2523 
2524   /*************************************************
2525   * Name: get_default_routing_id
2526   * This API returns the default routing id
2527   * It first uses the item level value
2528   * if that is null then it uses the vendor level value
2529   * even if that is null it uses the org level value which if null is = 1
2530   * We use the following rule to set headers routing ID
2531   * rounting lookups: 1. standard   2. Inspect  3. Direct
2532   ******************************************************/
2533   PROCEDURE get_default_routing_id(
2534     x_default_routing_id OUT NOCOPY    NUMBER
2535   , p_item_id            IN            NUMBER
2536   , p_organization_id    IN            NUMBER
2537   , p_vendor_id          IN            NUMBER
2538   ) IS
2539     l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
2540   BEGIN
2541     /*  select item level controls that are not specifid at the purchase
2542     **  order level
2543     */
2544     IF (NVL(p_item_id, 0) <> 0) THEN
2545       BEGIN
2546         IF (l_debug = 1) THEN
2547           print_debug('p_item_id ' || TO_CHAR(p_item_id) || ' ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
2548           print_debug('p_org_id ' || TO_CHAR(p_organization_id) || ' ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
2549         END IF;
2550 
2551         SELECT NVL(x_default_routing_id, receiving_routing_id)
2552           INTO x_default_routing_id
2553           FROM mtl_system_items
2554          WHERE inventory_item_id = p_item_id
2555            AND organization_id = p_organization_id;
2556       EXCEPTION
2557         WHEN NO_DATA_FOUND THEN
2558           NULL;
2559         WHEN OTHERS THEN
2560           RAISE;
2561       END;
2562     END IF;
2563 
2564     IF (l_debug = 1) THEN
2565       print_debug('routing id  ' || TO_CHAR(x_default_routing_id) || ' ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
2566     END IF;
2567 
2568     /*
2569     ** select vendor level controls not defined in the previous levels
2570     */
2571     IF (NVL(p_vendor_id, 0) <> 0) THEN
2572       BEGIN
2573         SELECT NVL(x_default_routing_id, receiving_routing_id)
2574           INTO x_default_routing_id
2575           FROM po_vendors
2576          WHERE vendor_id = p_vendor_id;
2577       EXCEPTION
2578         WHEN NO_DATA_FOUND THEN
2579           NULL;
2580         WHEN OTHERS THEN
2581           RAISE;
2582       END;
2583     END IF;
2584 
2585     /*
2586     ** select organization level controls not defined in the previous levels
2587     */
2588     BEGIN
2589       SELECT NVL(x_default_routing_id, NVL(receiving_routing_id, 1))
2590         INTO x_default_routing_id
2591         FROM rcv_parameters
2592        WHERE organization_id = p_organization_id;
2593     EXCEPTION
2594       WHEN NO_DATA_FOUND THEN
2595         NULL;
2596       WHEN OTHERS THEN
2597         RAISE;
2598     END;
2599   END get_default_routing_id;
2600 
2601   /*************************************************
2602   * Name: get_routing_id
2603   * This API returns routing id for a given PO header ID or shipment header
2604   * id or for the rma
2605   ******************************************************/
2606   PROCEDURE get_routing_id(
2607     x_routing_id         OUT NOCOPY    NUMBER
2608   , x_return_status      OUT NOCOPY    VARCHAR2
2609   , x_msg_count          OUT NOCOPY    NUMBER
2610   , x_msg_data           OUT NOCOPY    VARCHAR2
2611   , x_is_expense         OUT NOCOPY    VARCHAR2
2612   , p_po_header_id       IN            NUMBER
2613   , p_po_release_id      IN            NUMBER
2614   , p_po_line_id         IN            NUMBER
2615   , p_shipment_header_id IN            NUMBER
2616   , p_oe_order_header_id IN            NUMBER
2617   , p_item_id            IN            NUMBER
2618   , p_organization_id    IN            NUMBER
2619   , p_vendor_id          IN            NUMBER
2620   , p_lpn_id             IN            NUMBER DEFAULT NULL
2621   , p_item_desc          IN            VARCHAR2 DEFAULT NULL
2622   , p_from_lpn_id        IN            NUMBER DEFAULT NULL
2623   , p_project_id         IN            NUMBER DEFAULT NULL
2624   , p_task_id            IN            NUMBER DEFAULT NULL
2625   ) IS
2626     l_msg_count             NUMBER;
2627     l_msg_data              VARCHAR2(400);
2628     l_return_status         VARCHAR2(1);
2629     l_progress              VARCHAR2(10);
2630     l_lpn_context           NUMBER        := 5;
2631     l_sub                   VARCHAR2(10);
2632     l_locator_id            NUMBER;
2633     l_restrict_locator_code NUMBER;
2634     l_restrict_sub_code     NUMBER;
2635     l_dummy                 NUMBER;
2636     l_transaction_type      NUMBER        := 18; -- defaulted for PO receipt.
2637     l_is_expense            VARCHAR2(1)   := 'N';
2638     l_lpn_loaded            NUMBER := 0;
2639     l_debug                 NUMBER        := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
2640     l_is_valid_item         VARCHAR2(1) := 'Y';
2641     l_project_id            NUMBER;
2642     l_task_id               NUMBER;
2643     l_pjm_org               NUMBER := 2;
2644   BEGIN
2645     x_return_status  := fnd_api.g_ret_sts_success;
2646     x_is_expense     := 'N';
2647     l_progress       := '10';
2648 
2649     IF (l_debug = 1) THEN
2650       print_debug('Enter get_routing_id 10' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
2651     END IF;
2652 
2653     /** Project Task Commingle check **/
2654     IF ((inv_rcv_common_apis.g_wms_patch_level >= inv_rcv_common_apis.g_patchset_j) AND
2655        (inv_rcv_common_apis.g_PO_patch_level >= inv_rcv_common_apis.g_patchset_j_po) AND
2656         p_lpn_id IS NOT NULL AND
2657         p_lpn_id <> 0 ) THEN
2658 
2659       SELECT NVL(project_reference_enabled, 2)
2660       INTO   l_pjm_org
2661       FROM   mtl_parameters
2662       WHERE  organization_id = p_organization_id;
2663 
2664       --Do the commingling check only for a PJM org
2665       IF (l_pjm_org = 1) THEN
2666 
2667         /* Get Project_id,task_id from move order line and also from rti*/
2668         BEGIN
2669            SELECT project_id,task_id
2670            INTO l_project_id,l_task_id
2671            FROM mtl_txn_request_lines
2672            WHERE lpn_id = p_lpn_id
2673            AND ROWNUM=1;
2674 
2675            IF (l_debug=1) THEN
2676                print_debug('Found project and task from move order ', 3);
2677                print_debug('project_id: ' || l_project_id ,3);
2678                print_debug('task_id ' || l_task_id ,3);
2679            END IF;
2680         EXCEPTION
2681           WHEN no_data_found  THEN
2682             --If no MOL found, then get project and task from RTI
2683             BEGIN
2684              SELECT  project_id,task_id
2685              INTO    l_project_id,l_task_id
2686              FROM    rcv_transactions_interface
2687              WHERE   transfer_lpn_id = p_lpn_id
2688              AND     transaction_type = 'RECEIVE'
2689              AND     transaction_status_code = 'PENDING'
2690              AND     processing_status_code <> 'ERROR'
2691              AND     ROWNUM=1;
2692 
2693              IF (l_debug=1) THEN
2694                print_debug('Found project and task from RTI: ', 3  );
2695                print_debug('project_id: ' || l_project_id , 3);
2696                print_debug('task_id ' || l_task_id , 3);
2697              END IF;
2698             EXCEPTION
2699               WHEN no_data_found THEN
2700                 l_project_id := p_project_id;
2701                 l_task_id := p_task_id;
2702            END;   --End check for RTI
2703         END;    --End check for MOL
2704 
2705         IF (NVL(p_project_id,-9999) <> NVL(l_project_id,-9999)) OR
2706            (NVL(p_task_id,-9999) <> NVL(l_task_id,-9999)) THEN
2707           x_return_status  := fnd_api.g_ret_sts_error;
2708           x_msg_data       := 'INV_PRJ_ERR';
2709           fnd_message.set_name('INV', 'INV_PRJ_ERR');
2710           fnd_msg_pub.ADD;
2711         END IF;   --End if check for proj/task comm error
2712       END IF;   --End IF the current org is PJM org
2713     END IF;   --END if WMS and PO patch levels are >= J and lpn_id is not null
2714 
2715     /** Project Task Commingle check Ends here **/
2716 
2717     IF p_po_header_id IS NULL
2718        AND p_item_id IS NULL
2719        AND p_shipment_header_id IS NULL THEN
2720       IF (l_debug = 1) THEN
2721         print_debug('One time item can only be recd. on PO,  PO not given.', 4);
2722       END IF;
2723 
2724       x_return_status  := fnd_api.g_ret_sts_unexp_error;
2725       RETURN;
2726     END IF;
2727 
2728     IF p_po_header_id IS NOT NULL THEN
2729       l_progress  := '20';
2730 
2731       IF (l_debug = 1) THEN
2732         print_debug('get_routing_id 20' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 4);
2733       END IF;
2734 
2735       IF p_item_id IS NULL THEN
2736         IF p_item_desc IS NULL THEN
2737           IF (l_debug = 1) THEN
2738             print_debug('Item desc. and item id both cannot be null', 4);
2739           END IF;
2740 
2741           x_return_status  := fnd_api.g_ret_sts_unexp_error;
2742           RETURN;
2743         END IF;
2744 
2745         x_is_expense  := 'Y';
2746       END IF;
2747 
2748       get_po_routing_id(
2749           x_routing_id
2750         , l_is_expense
2751         , p_po_header_id
2752         , p_po_release_id
2753         , p_po_line_id
2754         , p_item_id
2755         , p_item_desc);
2756 
2757       IF x_is_expense <> 'Y' THEN
2758         x_is_expense  := l_is_expense;
2759       END IF;
2760 
2761       l_progress  := '30';
2762     ELSIF p_shipment_header_id IS NOT NULL THEN
2763       IF (l_debug = 1) THEN
2764         print_debug('get_routing_id 30' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 4);
2765       END IF;
2766 
2767       l_transaction_type  := 61;
2768       l_progress          := '40';
2769       get_intshp_routing_id(x_routing_id, l_is_expense, p_shipment_header_id, p_item_id, p_item_desc);
2770 
2771       IF x_is_expense <> 'Y' THEN
2772         x_is_expense  := l_is_expense;
2773       END IF;
2774 
2775       l_progress          := '50';
2776     ELSIF p_oe_order_header_id IS NOT NULL THEN
2777       IF (l_debug = 1) THEN
2778         print_debug('get_routing_id 52' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 4);
2779       END IF;
2780 
2781       l_transaction_type  := 15;
2782       l_progress          := '54';
2783 
2784       BEGIN
2785         SELECT return_inspection_requirement
2786           INTO x_routing_id
2787           FROM mtl_system_items
2788          WHERE inventory_item_id = p_item_id
2789            AND organization_id = p_organization_id;
2790       EXCEPTION
2791         WHEN OTHERS THEN
2792           NULL;
2793       END;
2794 
2795       -- 1 return_inspection_requirement implies inspection is reqd.
2796       -- so set the routing to inspection required else set it based
2797       -- on organization level profile.
2798       IF x_routing_id = 1 THEN
2799         x_routing_id  := 2;
2800       ELSE
2801            x_routing_id  := NULL;
2802            -- Bug 3569361
2803            -- Get the routing details from rcv parameters related po enhancement 3124881
2804            Begin
2805              IF (l_debug = 1) THEN
2806                print_debug('get_routing_id 52.1 ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 4);
2807              END IF;
2808 
2809              select rma_receipt_routing_id
2810              into x_routing_id
2811              from rcv_parameters
2812              where organization_id = p_organization_id;
2813            Exception
2814              When OTHERS then
2815                 IF (l_debug = 1) THEN
2816                   print_debug('get_routing_id - can not fetch routing id from rcv parameters ', 4);
2817                   print_debug('get_routing_id 52.2 ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 4);
2818                 END IF;
2819            End;
2820       END IF;
2821 
2822       l_progress          := '56';
2823     END IF;
2824 
2825     IF x_routing_id IS NULL THEN
2826       IF (l_debug = 1) THEN
2827         print_debug('get_routing_id 40' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 4);
2828       END IF;
2829 
2830       l_progress  := '60';
2831       get_default_routing_id(x_routing_id, p_item_id, p_organization_id, p_vendor_id);
2832       l_progress  := '70';
2833     END IF;
2834 
2835     IF (l_debug = 1) THEN
2836       print_debug('get_routing_id complete' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
2837     END IF;
2838 
2839     IF p_lpn_id IS NOT NULL THEN
2840       BEGIN
2841         SELECT NVL(lpn_context, 5)
2842              , NVL(subinventory_code, '@@@')
2843              , NVL(locator_id, -1)
2844           INTO l_lpn_context
2845              , l_sub
2846              , l_locator_id
2847           FROM wms_license_plate_numbers
2848          WHERE lpn_id = p_lpn_id;
2849       EXCEPTION
2850         WHEN OTHERS THEN
2851           l_lpn_context  := 5;
2852       END;
2853 
2854       IF (
2855           (x_routing_id = 3
2856            AND l_lpn_context NOT IN(1, 5, 6, 7)  --Added 6 above for bug # 2169351. added 7 - rnrao
2857                                                 )
2858           OR --Added 3 here for the bug#2129214
2859              (
2860               x_routing_id IN(1, 2)
2861               AND l_lpn_context NOT IN(3, 5, 6, 7)
2862              ) --Added 6 above for bug # 2169351 added 7 - rnrao
2863          ) THEN
2864         x_return_status  := fnd_api.g_ret_sts_error;
2865         --  print_debug('invalid lpn context ',9);
2866         x_msg_data       := 'INV_INVALID_LPN_CONTEXT';
2867         fnd_message.set_name('INV', 'INV_INVALID_LPN_CONTEXT');
2868         fnd_msg_pub.ADD;
2869         RETURN;
2870       END IF;
2871 
2872       IF (l_debug = 1) THEN
2873         print_debug('get_routing_id complete 111:' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
2874       END IF;
2875 
2876       --Do not allow receipt into a loaded LPN
2877       IF (l_lpn_context IN (1, 3)) THEN
2878         l_lpn_loaded := 0;
2879         BEGIN
2880           SELECT 1
2881           INTO   l_lpn_loaded
2882           FROM   DUAL
2883           WHERE  EXISTS(
2884                    SELECT 1
2885                    FROM   wms_dispatched_tasks wdt, mtl_txn_request_lines mtrl
2886                    WHERE  wdt.move_order_line_id = mtrl.line_id
2887                    AND    wdt.organization_id = p_organization_id
2888                    AND    wdt.status = 4
2889                    AND    mtrl.lpn_id IN
2890                      (
2891                       SELECT wlpn1.lpn_id
2892                       FROM   wms_license_plate_numbers wlpn1
2893                       WHERE  wlpn1.outermost_lpn_id =
2894                              (
2895                               SELECT outermost_lpn_id
2896                               FROM   wms_license_plate_numbers wlpn2
2897                               WHERE  wlpn2.lpn_id = p_lpn_id
2898                               )
2899                      )
2900                   );
2901         EXCEPTION
2902           WHEN OTHERS THEN
2903             l_lpn_loaded := 0;
2904         END;
2905 
2906         IF (l_lpn_loaded = 1) THEN
2907           x_return_status  := fnd_api.g_ret_sts_error;
2908           print_debug('get_routing_id: LPN is a loaded LPN',9);
2909           x_msg_data       := 'WMS_CONT_INVALID_LPN';
2910           fnd_message.set_name('WMS', 'WMS_LOADED_ERROR');
2911           fnd_msg_pub.ADD;
2912           fnd_message.set_name('WMS', 'WMS_CONT_INVALID_LPN');
2913           fnd_msg_pub.ADD;
2914         RETURN;
2915         END IF;
2916       END IF;
2917 
2918       -- Do the lpn related validations if the lpn context is 1 that is in inventory.
2919       IF l_lpn_context = 1 THEN
2920         SELECT NVL(restrict_locators_code, 2)
2921              , NVL(restrict_subinventories_code, 2)
2922           INTO l_restrict_locator_code
2923              , l_restrict_sub_code
2924           FROM mtl_system_items
2925          WHERE inventory_item_id = p_item_id
2926            AND organization_id = p_organization_id;
2927 
2928         IF l_sub <> '@@@' THEN
2929           IF l_restrict_sub_code = 1 THEN -- item restricted to predefined subs
2930             BEGIN
2931               SELECT 1
2932                 INTO l_dummy
2933                 FROM DUAL
2934                WHERE EXISTS(
2935                        SELECT 1
2936                          FROM mtl_item_sub_inventories mis
2937                         WHERE mis.organization_id = p_organization_id
2938                           AND mis.inventory_item_id = p_item_id
2939                           AND mis.secondary_inventory = l_sub
2940                           AND inv_material_status_grp.is_status_applicable('TRUE', NULL, l_transaction_type, NULL, NULL, p_organization_id
2941                              , p_item_id, l_sub, NULL, NULL, NULL, 'Z') = 'Y');
2942             EXCEPTION
2943               WHEN OTHERS THEN
2944                 x_return_status  := fnd_api.g_ret_sts_error;
2945                 x_msg_data       := 'INV_INT_RESSUBEXP';
2946                 fnd_message.set_name('INV', 'INV_INT_RESSUBEXP');
2947                 fnd_msg_pub.ADD;
2948                 fnd_message.set_name('WMS', 'WMS_TD_LPN_LOC_NOT_FOUND');
2949                 fnd_msg_pub.ADD;
2950                 RETURN;
2951             END;
2952           ELSE -- item not under subinventory restrictions.
2953             BEGIN
2954               SELECT 1
2955                 INTO l_dummy
2956                 FROM DUAL
2957                WHERE EXISTS(
2958                        SELECT 1
2959                          FROM mtl_secondary_inventories msi
2960                         WHERE msi.organization_id = p_organization_id
2961                           AND NVL(msi.disable_date, TRUNC(SYSDATE + 1)) > TRUNC(SYSDATE)
2962                           AND msi.secondary_inventory_name = l_sub
2963                           AND inv_material_status_grp.is_status_applicable('TRUE', NULL, l_transaction_type, NULL, NULL, p_organization_id
2964                              , p_item_id, l_sub, NULL, NULL, NULL, 'Z') = 'Y');
2965             EXCEPTION
2966               WHEN OTHERS THEN
2967                 x_return_status  := fnd_api.g_ret_sts_error;
2968                 x_msg_data       := 'INV_SUB_RESTRICT';
2969                 fnd_message.set_name('INV', 'INV_SUB_RESTRICT');
2970                 fnd_msg_pub.ADD;
2971                 fnd_message.set_name('INV', 'INV_INVALID_SUBINV');
2972                 fnd_msg_pub.ADD;
2973                 fnd_message.set_name('WMS', 'WMS_TD_LPN_LOC_NOT_FOUND');
2974                 fnd_msg_pub.ADD;
2975                 RETURN;
2976             END;
2977           END IF; -- subinventory restrictions
2978 
2979           IF l_locator_id <> -1 THEN
2980             IF l_restrict_locator_code = 1 THEN
2981               -- item under restricted locator control
2982               BEGIN
2983                 SELECT 1
2984                   INTO l_dummy
2985                   FROM DUAL
2986                  WHERE EXISTS(
2987                          SELECT 1
2988                            FROM mtl_secondary_locators msl
2989                           WHERE msl.organization_id = p_organization_id
2990                             AND msl.inventory_item_id = p_item_id
2991                             AND msl.subinventory_code = l_sub
2992                             AND msl.secondary_locator = l_locator_id
2993                             AND inv_material_status_grp.is_status_applicable(
2994                                  'TRUE'
2995                                , NULL
2996                                , l_transaction_type
2997                                , NULL
2998                                , NULL
2999                                , p_organization_id
3000                                , p_item_id
3001                                , l_sub
3002                                , l_locator_id
3003                                , NULL
3004                                , NULL
3005                                , 'L'
3006                                ) = 'Y');
3007               EXCEPTION
3008                 WHEN OTHERS THEN
3009                   x_return_status  := fnd_api.g_ret_sts_error;
3010                   x_msg_data       := 'INV_CCEOI_LOC_NOT_IN_LIST';
3011                   fnd_message.set_name('INV', 'INV_CCEOI_LOC_NOT_IN_LIST');
3012                   fnd_msg_pub.ADD;
3013                   fnd_message.set_name('WMS', 'WMS_TD_LPN_LOC_NOT_FOUND');
3014                   fnd_msg_pub.ADD;
3015                   RETURN;
3016               END;
3017             ELSE -- item not under restricted locator control
3018               BEGIN
3019                 SELECT 1
3020                   INTO l_dummy
3021                   FROM DUAL
3022                  WHERE EXISTS(
3023                          SELECT 1
3024                            FROM mtl_item_locations mil
3025                           WHERE mil.organization_id = p_organization_id
3026                             AND mil.subinventory_code = l_sub
3027                             AND NVL(mil.disable_date, TRUNC(SYSDATE + 1)) > TRUNC(SYSDATE)
3028                             AND mil.inventory_location_id = l_locator_id
3029                             AND inv_material_status_grp.is_status_applicable(
3030                                  'TRUE'
3031                                , NULL
3032                                , l_transaction_type
3033                                , NULL
3034                                , NULL
3035                                , p_organization_id
3036                                , p_item_id
3037                                , l_sub
3038                                , l_locator_id
3039                                , NULL
3040                                , NULL
3041                                , 'L'
3042                                ) = 'Y');
3043               EXCEPTION
3044                 WHEN OTHERS THEN
3045                   x_return_status  := fnd_api.g_ret_sts_error;
3046                   x_msg_data       := 'INV_TRX_LOCATOR_NA_DUE_MS';
3047                   fnd_message.set_name('INV', 'INV_TRX_LOCATOR_NA_DUE_MS');
3048                   fnd_message.set_token('TOKEN1', '');
3049                   fnd_msg_pub.ADD;
3050                   fnd_message.set_name('WMS', 'WMS_TD_LPN_LOC_NOT_FOUND');
3051                   fnd_msg_pub.ADD;
3052                   RETURN;
3053               END;
3054             END IF; -- locator restrictions
3055           END IF; -- l_locator_id <> -1
3056         END IF; -- l_sub <> '@@@'
3057       END IF; -- l_lpn_context = 1
3058 
3059       -- Nested LPN changes do not update LPN status after patchset J
3060       IF inv_rcv_common_apis.g_po_patch_level < inv_rcv_common_apis.g_patchset_j_po
3061          OR inv_rcv_common_apis.g_inv_patch_level < inv_rcv_common_apis.g_patchset_j
3062          OR inv_rcv_common_apis.g_wms_patch_level < inv_rcv_common_apis.g_patchset_j THEN
3063         IF (l_lpn_context = 5) THEN
3064           -- Bug 2357196
3065           -- For an expense item do not set the lpn context to 1 or 3
3066           IF NVL(x_is_expense, 'N') <> 'Y' THEN
3067             UPDATE wms_license_plate_numbers
3068                SET lpn_context = DECODE(x_routing_id, 3, 1, 3)
3069              WHERE lpn_id = p_lpn_id;
3070           END IF;
3071         END IF;
3072       END IF;
3073     END IF;           -- p_lpn_id <> null
3074             -- print_debug('get_routing_id complete 444:' || to_char(sysdate, 'YYYY-MM-DD HH:DD:SS'),1 );
3075   EXCEPTION
3076     WHEN OTHERS THEN
3077       x_return_status  := fnd_api.g_ret_sts_unexp_error;
3078 
3079       IF (l_debug = 1) THEN
3080         print_debug('Exiting get_routing_id - other exception:' || l_progress || ' ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
3081       END IF;
3082 
3083       IF SQLCODE IS NOT NULL THEN
3084         inv_mobile_helper_functions.sql_error('INV_RCV_COMMON_APIS.get_routing_id', l_progress, SQLCODE);
3085       END IF;
3086 
3087       --  Get message count and data
3088       fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
3089   END get_routing_id;
3090 
3091   PROCEDURE create_lpn(
3092     p_organization_id               NUMBER
3093   , p_lpn             IN            VARCHAR2
3094   , p_lpn_id          OUT NOCOPY    NUMBER
3095   , x_return_status   OUT NOCOPY    VARCHAR2
3096   , x_msg_data        OUT NOCOPY    VARCHAR2
3097   ) IS
3098     l_lpn_rec       wms_container_pub.lpn;
3099     l_return_status VARCHAR2(1);
3100     l_msg_count     NUMBER;
3101     l_msg_data      VARCHAR2(400);
3102     l_progress      VARCHAR2(10);
3103     l_debug         NUMBER                := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
3104   BEGIN
3105     IF (l_debug = 1) THEN
3106       print_debug('create_lpn 10: ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
3107       print_debug('Parameters passed : 10.1  p_lpn = ' || p_lpn, 4);
3108       print_debug('Parameters passed : 10.2  p_organization_id = ' || p_organization_id, 4);
3109     END IF;
3110 
3111     l_progress                      := '10';
3112     x_return_status                 := fnd_api.g_ret_sts_success;
3113     SAVEPOINT rcv_create_lpn_sp;
3114     l_lpn_rec.license_plate_number  := p_lpn;
3115 
3116     IF wms_container_pub.validate_lpn(l_lpn_rec) = inv_validate.f THEN
3117       l_progress  := '20';
3118       wms_container_pub.create_lpn(
3119         p_api_version                => 1.0
3120       , x_return_status              => l_return_status
3121       , x_msg_count                  => l_msg_count
3122       , x_msg_data                   => l_msg_data
3123       , p_lpn                        => p_lpn
3124       , p_organization_id            => p_organization_id
3125       , x_lpn_id                     => p_lpn_id
3126       , p_source                     => 5
3127       );
3128 
3129       IF l_return_status = fnd_api.g_ret_sts_error THEN
3130         fnd_message.set_name('WMS', 'WMS_LPN_GENERATION_FAIL');
3131         fnd_msg_pub.ADD;
3132 
3133         IF (l_debug = 1) THEN
3134           print_debug('create_lpn 20.1:  wms_container_pub.create_lpn RAISE FND_API.G_EXC_ERROR' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS')
3135           , 4);
3136         END IF;
3137 
3138         RAISE fnd_api.g_exc_error;
3139       END IF;
3140 
3141       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3142         fnd_message.set_name('WMS', 'WMS_LPN_GENERATION_FAIL');
3143         fnd_msg_pub.ADD;
3144 
3145         IF (l_debug = 1) THEN
3146           print_debug('create_lpn 20.2: wms_container_pub.create_lpn RAISE FND_API.G_EXC_UNEXPECTED_ERROR;' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 4);
3147         END IF;
3148 
3149         RAISE fnd_api.g_exc_unexpected_error;
3150       END IF;
3151     ELSE -- lpn exists
3152       IF (l_debug = 1) THEN
3153         print_debug('create_lpn 30', 4);
3154       END IF;
3155 
3156       l_progress  := '30';
3157 
3158       SELECT lpn_id
3159         INTO p_lpn_id
3160         FROM wms_license_plate_numbers
3161        WHERE license_plate_number = p_lpn;
3162 
3163       l_progress  := '40';
3164     END IF;
3165 
3166     IF (l_debug = 1) THEN
3167       print_debug('create_lpn compete - x_lpn = ' || p_lpn_id || ' ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
3168     END IF;
3169   EXCEPTION
3170     WHEN fnd_api.g_exc_error THEN
3171       ROLLBACK TO rcv_create_lpn_sp;
3172       x_return_status  := fnd_api.g_ret_sts_error;
3173       fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => l_msg_count, p_data => x_msg_data);
3174     WHEN fnd_api.g_exc_unexpected_error THEN
3175       ROLLBACK TO rcv_create_lpn_sp;
3176       x_return_status  := fnd_api.g_ret_sts_unexp_error;
3177       fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => l_msg_count, p_data => x_msg_data);
3178     WHEN OTHERS THEN
3179       ROLLBACK TO rcv_create_lpn_sp;
3180       x_return_status  := fnd_api.g_ret_sts_unexp_error;
3181 
3182       IF SQLCODE IS NOT NULL THEN
3183         inv_mobile_helper_functions.sql_error('INV_RCV_COMMON_APIS.create_lpn', l_progress, SQLCODE);
3184       END IF;
3185 
3186       fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => l_msg_count, p_data => x_msg_data);
3187   END create_lpn;
3188 
3189   -- This api creates a record in the mtl_transaction_lots_temp
3190   -- It checks if the p_transaction_temp_id is null, if it is, then it
3191   -- generates a new id and returns that.
3192   PROCEDURE insert_lot(
3193     p_transaction_temp_id        IN OUT NOCOPY NUMBER
3194   , p_created_by                 IN            NUMBER
3195   , p_transaction_qty            IN            NUMBER
3196   , p_primary_qty                IN            NUMBER
3197   , p_lot_number                 IN            VARCHAR2
3198   , p_expiration_date            IN            DATE
3199   , p_status_id                  IN            NUMBER := NULL
3200   , x_serial_transaction_temp_id OUT NOCOPY    NUMBER
3201   , x_return_status              OUT NOCOPY    VARCHAR2
3202   , x_msg_data                   OUT NOCOPY    VARCHAR2
3203   , p_secondary_quantity         IN            NUMBER --OPM Convergence
3204 
3205   ) IS
3206     l_return   NUMBER;
3207     l_progress VARCHAR2(10);
3208     l_count    NUMBER       := 0;
3209     l_debug    NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
3210   BEGIN
3211     x_return_status  := fnd_api.g_ret_sts_success;
3212     l_progress       := '10';
3213 
3214     IF (l_debug = 1) THEN
3215       print_debug('Enter insert_lot: 10:' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
3216       print_debug('p_transaction_temp_id:' || p_transaction_temp_id, 4);
3217       print_debug('p_transaction_qty:' || p_transaction_qty, 4);
3218       print_debug('p_primary_qty:' || p_primary_qty, 4);
3219       print_debug('p_lot_number:' || p_lot_number, 4);
3220       print_debug('p_expiration_date:' || p_expiration_date, 4);
3221       print_debug('p_status_id:' || p_status_id, 4);
3222     END IF;
3223 
3224     /* For Bug#2266537. check if the lot being inserted is already there in MTLT
3225        with the same temp_id. If so then the quantity of the lot is updated
3226        instead of generating a new lot.*/
3227     IF p_transaction_temp_id IS NOT NULL THEN
3228       BEGIN
3229         SELECT 1
3230              , serial_transaction_temp_id
3231           INTO l_count
3232              , x_serial_transaction_temp_id
3233           FROM mtl_transaction_lots_temp
3234          WHERE transaction_temp_id = p_transaction_temp_id
3235            AND lot_number = p_lot_number
3236            AND ROWNUM = 1;
3237       EXCEPTION
3238         WHEN NO_DATA_FOUND THEN
3239           l_count  := 0;
3240         WHEN OTHERS THEN
3241           l_count  := 0;
3242 
3243           IF (l_debug = 1) THEN
3244             print_debug('Exception trying to find lot existance', 4);
3245           END IF;
3246       END;
3247     ELSE
3248       l_count  := 0;
3249     END IF;
3250 
3251     IF l_count = 1 THEN
3252       IF (l_debug = 1) THEN
3253         print_debug('Updating mtlt existing rec', 4);
3254       END IF;
3255 
3256       UPDATE mtl_transaction_lots_temp
3257          SET transaction_quantity = transaction_quantity + p_transaction_qty
3258            , primary_quantity = primary_quantity + p_primary_qty
3259        WHERE transaction_temp_id = p_transaction_temp_id
3260          AND lot_number = p_lot_number;
3261     ELSE
3262       IF p_transaction_temp_id IS NULL THEN
3263         SELECT mtl_material_transactions_s.NEXTVAL
3264           INTO p_transaction_temp_id
3265           FROM DUAL;
3266       END IF;
3267 
3268       l_progress  := '20';
3269       l_return    :=
3270         inv_trx_util_pub.insert_lot_trx(
3271           p_trx_tmp_id                 => p_transaction_temp_id
3272         , p_user_id                    => p_created_by
3273         , p_lot_number                 => p_lot_number
3274         , p_trx_qty                    => p_transaction_qty
3275         , p_pri_qty                    => p_primary_qty
3276         , p_exp_date                   => p_expiration_date
3277         , p_status_id                  => p_status_id
3278         , x_ser_trx_id                 => x_serial_transaction_temp_id
3279         , x_proc_msg                   => x_msg_data
3280         , p_secondary_qty              => p_secondary_quantity --OPM Convergence
3281   );
3282       l_progress  := '30';
3283 
3284       -- if the trx manager returned a 1 then it could not insert the row
3285       IF l_return = 1 THEN
3286         l_progress       := '40';
3287         x_return_status  := fnd_api.g_ret_sts_error;
3288       END IF;
3289 
3290       l_progress  := '50';
3291 
3292       IF (l_debug = 1) THEN
3293         print_debug('Exitting insert_lot : 60  ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
3294       END IF;
3295     END IF;
3296   EXCEPTION
3297     WHEN OTHERS THEN
3298       x_return_status  := fnd_api.g_ret_sts_unexp_error;
3299 
3300       IF SQLCODE IS NOT NULL THEN
3301         inv_mobile_helper_functions.sql_error('inv_rcv_common_apis.insert_lot', l_progress, SQLCODE);
3302       END IF;
3303 
3304       IF (l_debug = 1) THEN
3305         print_debug('Exitting insert_lot - other exception:' || l_progress || ' ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
3306       END IF;
3307 
3308       --
3309       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
3310         fnd_msg_pub.add_exc_msg(g_pkg_name, 'insert_lot');
3311       END IF;
3312   END insert_lot;
3313 
3314   -- This api creates a record in the mtl_transaction_serial_temp
3315   -- It checks if the p_transaction_temp_id is null, if it is, then it
3316   -- generates a new id and returns that.
3317   PROCEDURE insert_serial(
3318     p_serial_transaction_temp_id IN OUT NOCOPY NUMBER
3319   , p_org_id                     IN            NUMBER
3320   , p_item_id                    IN            NUMBER
3321   , p_rev                        IN            VARCHAR2
3322   , p_lot                        IN            VARCHAR2
3323   , p_txn_src_id                 IN            NUMBER
3324   , p_txn_action_id              IN            NUMBER
3325   , p_created_by                 IN            NUMBER
3326   , p_from_serial                IN            VARCHAR2
3327   , p_to_serial                  IN            VARCHAR2
3328   , p_status_id                  IN            NUMBER := NULL
3329   , x_return_status              OUT NOCOPY    VARCHAR2
3330   , x_msg_data                   OUT NOCOPY    VARCHAR2
3331   ) IS
3332     l_return    NUMBER;
3333     l_to_serial VARCHAR2(30);
3334     l_progress  VARCHAR2(10);
3335     l_msg_count NUMBER;
3336     l_success   NUMBER       := 0;
3337     l_count     NUMBER       := 0;
3338     l_temp_qty  NUMBER       := 0;
3339     l_debug     NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
3340   BEGIN
3341     x_return_status  := fnd_api.g_ret_sts_success;
3342     l_progress       := '10';
3343 
3344     IF (l_debug = 1) THEN
3345       print_debug('Enter insert_serial: 10:' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
3346     END IF;
3347 
3348     SAVEPOINT rcv_insert_serial_sp;
3349     l_to_serial      := p_to_serial;
3350     /** we are now calling INV_SERIAL_NUMBER_PUB instead of inv_trx_mgr, for "I" project vipathak
3351     **/
3352     l_return         :=
3353       inv_serial_number_pub.validate_serials(
3354         p_org_id                     => p_org_id
3355       , p_item_id                    => p_item_id
3356       , p_qty                        => l_temp_qty
3357       , p_rev                        => p_rev
3358       , p_lot                        => p_lot
3359       , p_start_ser                  => p_from_serial
3360       , p_trx_src_id                 => p_txn_src_id
3361       , p_trx_action_id              => p_txn_action_id
3362       , x_end_ser                    => l_to_serial
3363       , x_proc_msg                   => x_msg_data
3364       );
3365     l_progress       := '20';
3366 
3367     IF l_return = 1 THEN
3368       fnd_message.set_name('INV', 'INVALID_SERIAL_NUMBER');
3369       fnd_msg_pub.ADD;
3370       RAISE fnd_api.g_exc_error;
3371     END IF;
3372 
3373     l_progress       := '25';
3374     l_count          := 0;
3375 
3376    -- Bug 4087032 Need to write a wrapper on LENGTH function as
3377    -- it creates compiltaion issues in 8i env. in the below query
3378 
3379     BEGIN
3380       SELECT 1
3381         INTO l_count
3382         FROM mtl_serial_numbers_temp msnt, mtl_transaction_lots_temp mtlt, mtl_material_transactions_temp mmtt
3383        WHERE (
3384               (p_from_serial BETWEEN msnt.fm_serial_number AND msnt.to_serial_number
3385 	       AND get_serial_Length(p_from_serial) = get_serial_Length(msnt.fm_serial_number)
3386 	       AND get_serial_Length(msnt.fm_serial_number) = get_serial_Length(Nvl(msnt.to_serial_number,msnt.fm_serial_number)))
3387 	      OR
3388 	      (p_to_serial BETWEEN msnt.fm_serial_number AND msnt.to_serial_number
3389 	       AND get_serial_Length(p_to_serial) = get_serial_Length(msnt.fm_serial_number)
3390 	       AND get_serial_Length(msnt.fm_serial_number) = get_serial_Length(Nvl(msnt.to_serial_number,msnt.fm_serial_number)))
3391              )
3392          AND mmtt.inventory_item_id = p_item_id
3393          AND mmtt.organization_id = p_org_id
3394          AND mtlt.transaction_temp_id(+) = mmtt.transaction_temp_id
3395          AND msnt.transaction_temp_id = NVL(mtlt.serial_transaction_temp_id, mmtt.transaction_temp_id);
3396     EXCEPTION
3397       WHEN OTHERS THEN
3398         l_count  := 0;
3399     END;
3400 
3401     IF l_count <> 0 THEN
3402       fnd_message.set_name('INV', 'INVALID_SERIAL_NUMBER');
3403       fnd_msg_pub.ADD;
3404       RAISE fnd_api.g_exc_error;
3405     END IF;
3406 
3407     l_progress       := '30';
3408 
3409     IF p_serial_transaction_temp_id IS NULL THEN
3410       l_progress  := '40';
3411 
3412       SELECT mtl_material_transactions_s.NEXTVAL
3413         INTO p_serial_transaction_temp_id
3414         FROM DUAL;
3415 
3416       l_progress  := '50';
3417     END IF;
3418 
3419     l_progress       := '60';
3420     l_return         :=
3421       inv_trx_util_pub.insert_ser_trx(
3422         p_trx_tmp_id                 => p_serial_transaction_temp_id
3423       , p_user_id                    => p_created_by
3424       , p_fm_ser_num                 => p_from_serial
3425       , p_to_ser_num                 => p_to_serial
3426       , p_status_id                  => p_status_id
3427       , x_proc_msg                   => x_msg_data
3428       );
3429     l_progress       := '70';
3430 
3431     BEGIN
3432       UPDATE mtl_serial_numbers
3433          SET group_mark_id = p_serial_transaction_temp_id
3434        WHERE inventory_item_id = p_item_id
3435          AND serial_number BETWEEN p_from_serial AND p_to_serial
3436          AND LENGTH(serial_number) = LENGTH(p_from_serial);
3437     EXCEPTION
3438       WHEN OTHERS THEN
3439         IF (l_debug = 1) THEN
3440           print_debug('Exception updating grp. id', 4);
3441         END IF;
3442     END;
3443 
3444     IF (l_debug = 1) THEN
3445       print_debug('Insert serial vals' || p_item_id || ':' || p_from_serial || ':' || p_to_serial, 4);
3446       print_debug('Insert serial, inserted with ' || p_serial_transaction_temp_id || ':' || l_success, 4);
3447     END IF;
3448 
3449     -- if the trx manager returned a 1 then it could not insert the row
3450     IF l_return = 1 THEN
3451       RAISE fnd_api.g_exc_error;
3452     END IF;
3453 
3454     l_progress       := '80';
3455 
3456     IF (l_debug = 1) THEN
3457       print_debug('Exitting insert_serial : 90  ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
3458     END IF;
3459   EXCEPTION
3460     WHEN fnd_api.g_exc_error THEN
3461       ROLLBACK TO rcv_insert_serial_sp;
3462       x_return_status  := fnd_api.g_ret_sts_error;
3463 
3464       IF (l_debug = 1) THEN
3465         print_debug('Exitting insert_serial - execution error:' || l_progress || ' ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
3466       END IF;
3467 
3468       --  Get message count and data
3469       fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => l_msg_count, p_data => x_msg_data);
3470     WHEN OTHERS THEN
3471       x_return_status  := fnd_api.g_ret_sts_unexp_error;
3472 
3473       IF SQLCODE IS NOT NULL THEN
3474         inv_mobile_helper_functions.sql_error('inv_rcv_common_apis.insert_serial', l_progress, SQLCODE);
3475       END IF;
3476 
3477       IF (l_debug = 1) THEN
3478         print_debug('Exitting insert_serial - other exception:' || l_progress || ' ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
3479       END IF;
3480 
3481       --
3482       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
3483         fnd_msg_pub.add_exc_msg(g_pkg_name, 'insert_serial');
3484       END IF;
3485   END insert_serial;
3486 --BUG#3062591
3487   PROCEDURE get_uom_code(
3488 			  x_return_status      OUT NOCOPY    VARCHAR2
3489 			, x_uom_code           OUT NOCOPY    VARCHAR2
3490 			, p_po_header_id       IN            NUMBER
3491                         , p_item_id            IN            NUMBER
3492                         , p_organization_id    IN            NUMBER
3493 			, p_line_no            IN            NUMBER    --BUG 4500676
3494 			, p_item_desc          IN            VARCHAR2  --BUG 4500676
3495 			) IS
3496        l_progress   VARCHAR2(10);
3497        l_class      VARCHAR2(10);
3498        l_debug      NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
3499        l_count      NUMBER;
3500   BEGIN
3501         x_return_status  := fnd_api.g_ret_sts_success;
3502         IF (l_debug = 1) THEN
3503              print_debug('Entering get_uom_code',4);
3504              print_debug('Parameters passed : 10.1: p_po_header_id - ' || p_po_header_id , 4);
3505 	     print_debug('Parameters passed : 10.2: p_item_id - ' || p_item_id, 4);
3506 	     print_debug('Parameters passed : 10.3: p_organization_id - ' || p_organization_id, 4);
3507              print_debug('Parameters passed : 10.4: p_line_no - ' || p_line_no , 4);
3508              print_debug('Parameters passed : 10.5: p_item_desc - ' || p_item_desc, 4);
3509         END IF;
3510 
3511         x_uom_code := '@@@';
3512         l_progress := '10';
3513 	l_count    := 0;
3514 
3515 	  --BUG 4500676: Add logic to retrieve uom_code for expense item
3516           IF p_po_header_id IS NOT NULL AND p_item_id IS NULL  and p_line_no is not null THEN
3517             l_progress  := '20';
3518             BEGIN
3519             SELECT mum.uom_code, mum.uom_class
3520              INTO x_uom_code, l_class
3521              FROM po_lines pol, mtl_units_of_measure mum
3522             WHERE pol.po_header_id = p_po_header_id
3523               AND pol.unit_meas_lookup_code IS NOT NULL
3524               AND pol.line_num = p_line_no
3525               AND pol.unit_meas_lookup_code = mum.unit_of_measure
3526               AND pol.po_line_id IN (SELECT poll.po_line_id
3527                            FROM po_line_locations_all poll, po_lines_all po
3528                                   WHERE poll.po_header_id = po.po_header_id
3529                                   AND Nvl(poll.approved_flag,'N') =  'Y'
3530                                   AND Nvl(poll.cancel_flag,'N') = 'N'
3531                                   AND Nvl(poll.closed_code,'OPEN') NOT IN ('FINALLY CLOSED','CLOSED FOR RECEIVING')
3532                                   AND poll.shipment_type IN ('STANDARD','BLANKET','SCHEDULED')
3533                                   AND poll.ship_to_organization_id = p_organization_id
3534                                   AND poll.po_line_id = po.po_line_id
3535                                   AND po.po_header_id = p_po_header_id)
3536               AND ROWNUM=1;
3537             SELECT INV_UI_RCV_LOVS.get_conversion_rate_expense(muom.uom_code,p_organization_id  ,0,x_uom_code )
3538             into x_uom_code
3539             from
3540               mtl_uom_conversions_val_v muc ,
3541               mtl_units_of_measure muom
3542             where muc.uom_class = l_class
3543             and muc.item_id = 0
3544             and nvl(muc.disable_date,sysdate+1)>sysdate
3545             and muc.unit_of_measure = muom.unit_of_measure
3546             and nvl(muom.disable_date,sysdate+1) > sysdate
3547             and muom.uom_code like (x_uom_code)
3548             order by muc.unit_of_measure;
3549             EXCEPTION
3550               WHEN NO_DATA_FOUND THEN
3551               x_uom_code := '@@@';
3552             END;
3553           Elsif p_po_header_id IS NOT NULL AND p_item_id IS NULL  and p_line_no is null and p_item_desc is not null THEN
3554             l_progress  := '30';
3555             BEGIN
3556             SELECT mum.uom_code, mum.uom_class
3557              INTO x_uom_code, l_class
3558              FROM po_lines pol, mtl_units_of_measure mum
3559             WHERE pol.po_header_id = p_po_header_id
3560               AND pol.unit_meas_lookup_code IS NOT NULL
3561               AND pol.unit_meas_lookup_code = mum.unit_of_measure
3562               AND pol.item_description = p_item_desc
3563               AND pol.po_line_id IN (SELECT poll.po_line_id
3564                            FROM po_line_locations_all poll, po_lines_all po
3565                                   WHERE poll.po_header_id = po.po_header_id
3566                                   AND Nvl(poll.approved_flag,'N') =  'Y'
3567                                   AND Nvl(poll.cancel_flag,'N') = 'N'
3568                                   AND Nvl(poll.closed_code,'OPEN') NOT IN ('FINALLY CLOSED','CLOSED FOR RECEIVING')
3569                                   AND poll.shipment_type IN ('STANDARD','BLANKET','SCHEDULED')
3570                                   AND poll.ship_to_organization_id = p_organization_id
3571                                   AND poll.po_line_id = po.po_line_id
3572                                   AND po.po_header_id = p_po_header_id)
3573               AND ROWNUM=1;
3574             SELECT INV_UI_RCV_LOVS.get_conversion_rate_expense(muom.uom_code, p_organization_id  ,0,x_uom_code )
3575             into x_uom_code
3576             from
3577               mtl_uom_conversions_val_v muc ,
3578               mtl_units_of_measure muom
3579             where muc.uom_class = l_class
3580             and muc.item_id = 0
3581             and nvl(muc.disable_date,sysdate+1)>sysdate
3582             and muc.unit_of_measure = muom.unit_of_measure
3583             and nvl(muom.disable_date,sysdate+1) > sysdate
3584             and muom.uom_code like (x_uom_code)
3585             order by muc.unit_of_measure;
3586             EXCEPTION
3587               WHEN NO_DATA_FOUND THEN
3588               x_uom_code := '@@@';
3589             END;
3590 	  --END BUG 4500676
3591 
3592           ELSIF p_po_header_id IS NOT NULL AND p_item_id IS NOT NULL THEN
3593             l_progress  := '20';
3594             BEGIN
3595             SELECT COUNT(DISTINCT pol.unit_meas_lookup_code)
3596              INTO l_count
3597              FROM po_lines pol
3598             WHERE pol.po_header_id = p_po_header_id
3599               AND pol.unit_meas_lookup_code IS NOT NULL
3600               AND pol.item_id = p_item_id
3601 	      AND pol.po_line_id IN (SELECT poll.po_line_id
3602 	                          FROM po_line_locations_all poll, po_lines_all po
3603                                   WHERE poll.po_header_id = po.po_header_id
3604                                   AND Nvl(poll.approved_flag,'N') =  'Y'
3605                                   AND Nvl(poll.cancel_flag,'N') = 'N'
3606                                   AND Nvl(poll.closed_code,'OPEN') NOT IN ('FINALLY CLOSED','CLOSED FOR RECEIVING')
3607                                   AND poll.shipment_type IN ('STANDARD','BLANKET','SCHEDULED')
3608                                   AND poll.ship_to_organization_id = p_organization_id
3609                                   AND poll.po_line_id = po.po_line_id
3610                                   AND po.item_id = p_item_id
3611                                   AND po.po_header_id = p_po_header_id);
3612             EXCEPTION
3613               WHEN NO_DATA_FOUND THEN
3614               l_count  := 0;
3615             END;
3616 
3617           IF l_count = 1 THEN
3618             l_progress  := '30';
3619             BEGIN
3620             SELECT inv_ui_item_lovs.get_conversion_rate(mum.uom_code,
3621                                    p_organization_id,
3622                                    pol.item_id)
3623              INTO x_uom_code
3624              FROM po_lines pol
3625                   , mtl_units_of_measure mum
3626             WHERE pol.po_header_id = p_po_header_id
3627               AND pol.unit_meas_lookup_code IS NOT NULL
3628               AND pol.item_id = p_item_id
3629               AND mum.UNIT_OF_MEASURE(+) = pol.UNIT_MEAS_LOOKUP_CODE
3630               AND pol.po_line_id IN (SELECT poll.po_line_id
3631                                   FROM po_line_locations_all poll, po_lines_all po
3632                                   WHERE poll.po_header_id = po.po_header_id
3633                                   AND Nvl(poll.approved_flag,'N') =  'Y'
3634                                   AND Nvl(poll.cancel_flag,'N') = 'N'
3635                                   AND Nvl(poll.closed_code,'OPEN') NOT IN ('FINALLY CLOSED','CLOSED FOR RECEIVING')
3636                                   AND poll.shipment_type IN ('STANDARD','BLANKET','SCHEDULED')
3637                                   AND poll.ship_to_organization_id = p_organization_id
3638                                   AND poll.po_line_id = po.po_line_id
3639                                   AND po.item_id = p_item_id
3640                                   AND po.po_header_id = p_po_header_id)
3641               AND ROWNUM = 1;
3642             EXCEPTION
3643               WHEN OTHERS THEN
3644                  x_uom_code := '@@@';
3645             END;
3646           END IF;
3647        END IF;
3648 
3649        IF (l_debug = 1) THEN
3650 	  print_debug('x_uom_code:'||x_uom_code,4);
3651        END IF;
3652    EXCEPTION
3653     WHEN OTHERS THEN
3654       x_return_status  := fnd_api.g_ret_sts_unexp_error;
3655       IF SQLCODE IS NOT NULL THEN
3656         inv_mobile_helper_functions.sql_error('inv_rcv_common_apis.get_uom_code', l_progress, SQLCODE);
3657       END IF;
3658 
3659       IF (l_debug = 1) THEN
3660         print_debug('Exitting get_uom_code - other exception:' || l_progress ||' ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
3661       END IF;
3662 
3663       --
3664       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
3665         fnd_msg_pub.add_exc_msg(g_pkg_name, 'get_uom_code');
3666       END IF;
3667 
3668   END get_uom_code;--BUG#3062591
3669 
3670   -- This api is used to return the possible value that can be used for
3671   -- subinventory when the item and PO/Shipment Number/RMA are entered.
3672   -- For RMA it always returns null for subinventory.
3673   PROCEDURE get_sub_code(
3674        x_return_status      OUT NOCOPY VARCHAR2,
3675        x_msg_count          OUT NOCOPY NUMBER,
3676        x_msg_data           OUT NOCOPY VARCHAR2,
3677        x_sub_code           OUT NOCOPY VARCHAR2,
3678        x_locator_segs       OUT NOCOPY VARCHAR2,
3679        x_locator_id         OUT NOCOPY NUMBER,
3680        x_lpn_context        OUT NOCOPY NUMBER,
3681        x_default_source     OUT NOCOPY VARCHAR2,
3682        p_po_header_id       IN         NUMBER,
3683        p_po_release_id      IN         NUMBER,
3684        p_po_line_id         IN         NUMBER,
3685        p_shipment_header_id IN         NUMBER,
3686        p_oe_order_header_id IN         NUMBER,
3687        p_item_id            IN         NUMBER,
3688        p_organization_id    IN         NUMBER,
3689        p_lpn_id             IN         NUMBER DEFAULT NULL,
3690        p_project_id         IN         NUMBER DEFAULT NULL,
3691        p_task_id            IN         NUMBER DEFAULT NULL) IS
3692     l_count               NUMBER;
3693     l_locator_id          NUMBER := -1;
3694     l_lpn_context         NUMBER := 5;
3695     l_lpn_controlled_flag NUMBER := 2;
3696     l_progress            VARCHAR2(10);
3697     l_auto_transact_code  VARCHAR2(10);
3698     l_is_pjm_org          NUMBER := 2;
3699     l_loc_project_id      NUMBER;
3700     l_loc_task_id         NUMBER;
3701     l_debug               NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
3702     l_kanban_card_id      NUMBER := -999; --Bug 4671198
3703     l_count_lpn           NUMBER := 0; --Bug 5928199
3704   BEGIN
3705     x_return_status  := fnd_api.g_ret_sts_success;
3706 
3707     /* Two new parameter x_lpn_context and x_default_source have been added
3708      * to return the lpn_context and a flag that indicates how the
3709      * subinventory and locator have been defaulted. Possible values are:
3710      *   LPN  -> Sub and locator are derived from WMS_LICENSE_PLATE_NUMBERS
3711      *   RTI  -> Sub and locator are derived from RCV_TRANSACTIONS_INTERFACE
3712      *   DOC  -> Sub and locator are derived from document (POD/RSL/REQ)
3713      *   ITD  -> Sub and locator are derived from item transaction defaults
3714      *   NONE -> There is no default sub and locator being returned
3715      * This validation is applicable only if INV and PO patch levels are J or
3716      * higher and is used by the Receipt UI to default the sub and locator beans.
3717      * If patch levels are < J, this variable would hold the value NONE
3718      */
3719     x_default_source := 'NONE';
3720     x_lpn_context := 5;
3721 
3722     IF (l_debug = 1) THEN
3723       print_debug('Enter  get_sub_code: 10:' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
3724       print_debug('Parameters passed : 10.1: p_po_header_id - ' || p_po_header_id, 4);
3725       print_debug('Parameters passed : 10.2: p_po_release_id - ' || p_po_release_id, 4);
3726       print_debug('Parameters passed : 10.3: p_po_line_id - ' || p_po_line_id, 4);
3727       print_debug('Parameters passed : 10.4: p_shipment_header_id - ' || p_shipment_header_id, 4);
3728       print_debug('Parameters passed : 10.5: p_oe_order_header_id - ' || p_oe_order_header_id, 4);
3729       print_debug('Parameters passed : 10.6: p_item_id - ' || p_item_id, 4);
3730       print_debug('Parameters passed : 10.7: p_organization_id - ' || p_organization_id, 4);
3731       print_debug('Parameters passed : 10.8: p_lpn_id - ' || p_lpn_id, 4);
3732       print_debug('Parameters passed : 10.9: p_project_id - ' || p_project_id, 4);
3733       print_debug('Parameters passed : 10.10: p_task_id - ' || p_task_id, 4);
3734     END IF;
3735 
3736     x_sub_code       := '@@@';
3737     x_locator_segs   := '@@@';
3738     x_locator_id     := -1;
3739     l_progress       := '10';
3740 
3741     IF (p_lpn_id IS NOT NULL AND p_lpn_id > 0) THEN
3742       BEGIN
3743         SELECT NVL(subinventory_code, '@@@')
3744              , NVL(locator_id, -1)
3745              , lpn_context
3746           INTO x_sub_code
3747              , l_locator_id
3748              , l_lpn_context
3749           FROM wms_license_plate_numbers
3750          WHERE lpn_id = p_lpn_id;
3751 
3752 
3753        --added for Bug 5928199
3754         select count(*) into l_count_lpn
3755 	from wms_lpn_contents
3756          WHERE parent_lpn_id = p_lpn_id;
3757 
3758         --Set the out variable for lpn_context
3759         x_lpn_context := l_lpn_context;
3760 	--added for Bug 5928199
3761     IF (l_debug = 1) THEN
3762 	print_debug('lpn_context'||l_lpn_context||'l_locator_id'||l_locator_id||'x_sub_code'||x_sub_code,4);
3763     END IF;
3764 
3765         --Bug #3360067
3766         --If the LPN resides in inventory and the current org is PJM-enabled
3767         --get the project_id and task_id from MTL_ITEM_LOCATIONS for this locator
3768         --and compare the project and task of the locator with those passed
3769         --In case of project/task commingling, raise an error indicating the same
3770       --Added extra condition l_count_lpn <> 0 for Bug 5928199
3771            IF (l_lpn_context = 1 AND l_locator_id IS NOT NULL AND l_count_lpn <> 0) THEN
3772 
3773           SELECT NVL(project_reference_enabled, 2)
3774           INTO   l_is_pjm_org
3775           FROM   mtl_parameters
3776           WHERE  organization_id = p_organization_id;
3777 
3778           IF (l_is_pjm_org = 1) THEN
3779             SELECT  mil.project_id
3780                   , mil.task_id
3781             INTO    l_loc_project_id
3782                   , l_loc_task_id
3783             FROM    mtl_item_locations mil
3784             WHERE   mil.organization_id = p_organization_id
3785             AND     mil.inventory_location_id = l_locator_id;
3786 
3787             IF (l_debug = 1) THEN
3788                 print_debug('l_loc_project_id'||l_loc_project_id||'l_loc_task_id'||l_loc_task_id,4);
3789                 print_debug('p_project_id'||p_project_id||'p_task_id'||p_task_id,4);
3790             END IF;
3791 
3792             IF (NVL(p_project_id,-9999) <> NVL(l_loc_project_id,-9999)) OR
3793                (NVL(p_task_id,-9999) <> NVL(l_loc_task_id,-9999)) THEN
3794               x_return_status  := fnd_api.g_ret_sts_error;
3795               x_msg_data       := 'INV_PRJ_ERR';
3796               fnd_message.set_name('INV', 'INV_PRJ_ERR');
3797               fnd_msg_pub.ADD;
3798             END IF;   --End if check for proj/task comm error
3799           END IF;  --END IF current org is PJM enabled
3800         END IF;  --END IF lpn_context = 1
3801 
3802         IF ((inv_rcv_common_apis.g_wms_patch_level >= inv_rcv_common_apis.g_patchset_j) AND
3803             (inv_rcv_common_apis.g_po_patch_level >= inv_rcv_common_apis.g_patchset_j_po)) THEN
3804 
3805           --If the LPN has a sub/loc associated, set the defaulting source
3806           IF x_sub_code <> '@@@' AND l_locator_id IS NOT NULL THEN
3807             x_default_source := 'LPN';
3808           END IF;
3809 
3810           IF (l_lpn_context = 5 AND x_sub_code = '@@@') THEN
3811 
3812             IF (l_debug = 1) THEN
3813               print_debug('get_sub_code: WMS and PO are J or higher: should also check for sub/loc from RTI', 4);
3814             END IF;
3815 
3816             --Get the subinventory, locator_id and routing from rcv_transactions_interface
3817             --If there exists one and it is not direct then error out with the
3818             --"Invalid LPN context" error since we cannot commingle routings in the same LPN
3819             BEGIN
3820               SELECT  NVL(subinventory, '@@@')
3821                     , NVL(locator_id, -1)
3822                     , auto_transact_code
3823               INTO    x_sub_code
3824                     , l_locator_id
3825                     , l_auto_transact_code
3826               FROM    rcv_transactions_interface
3827               WHERE   transfer_lpn_id = p_lpn_id
3828               AND     transaction_type = 'RECEIVE'
3829               AND     transaction_status_code = 'PENDING'
3830               AND     processing_status_code <> 'ERROR'
3831               AND     ROWNUM = 1;
3832 
3833               IF (NVL(l_auto_transact_code, 'RECEIVE') <> 'DELIVER') THEN
3834                 x_return_status  := fnd_api.g_ret_sts_error;
3835                 x_msg_data       := 'INV_CANNOT_COMMINGLE_ROUTING';
3836 		            fnd_message.set_name('INV', 'INV_CANNOT_COMMINGLE_ROUTING');
3837                 fnd_msg_pub.ADD;
3838                 RETURN;
3839               END IF;
3840             EXCEPTION
3841               WHEN OTHERS THEN
3842                 x_sub_code    := '@@@';
3843                 l_locator_id  := -1;
3844             END;
3845 
3846             --If the RTI record has a sub/loc associated, set the defaulting source
3847             IF x_sub_code <> '@@@' THEN
3848               x_default_source := 'RTI';
3849             END IF;
3850 
3851           END IF;  --END IF LPN context is defined but not used
3852         END IF;  --END IF patch levels are J or higher
3853       EXCEPTION
3854         WHEN OTHERS THEN
3855           x_sub_code    := '@@@';
3856           l_locator_id  := -1;
3857           l_lpn_context := 5;
3858       END;
3859     END IF;
3860 
3861     IF x_sub_code = '@@@' THEN
3862       IF p_po_header_id IS NOT NULL THEN
3863         l_progress  := '20';
3864 
3865         BEGIN
3866           SELECT COUNT(DISTINCT pod.destination_subinventory)
3867             INTO l_count
3868             FROM po_distributions pod
3869            WHERE pod.po_header_id = p_po_header_id
3870              AND pod.po_line_id = NVL(p_po_line_id, pod.po_line_id)
3871              AND NVL(pod.po_release_id, -1) = NVL(p_po_release_id, NVL(pod.po_release_id, -1))
3872              AND pod.destination_subinventory IS NOT NULL
3873              AND pod.po_line_id IN(SELECT pol.po_line_id
3874                                      FROM po_lines pol
3875                                     WHERE pol.item_id = p_item_id);
3876         EXCEPTION
3877           WHEN NO_DATA_FOUND THEN
3878             l_count  := 0;
3879         END;
3880         l_progress  := '35';
3881         BEGIN
3882           SELECT pod.destination_subinventory, nvl(kanban_card_id, -999) --Bug 4671198
3883             INTO x_sub_code, l_kanban_card_id --Bug 4671198
3884             FROM po_distributions pod
3885            WHERE pod.po_header_id = p_po_header_id
3886              AND pod.po_line_id = NVL(p_po_line_id, pod.po_line_id)
3887              AND NVL(pod.po_release_id, -1) = NVL(p_po_release_id, NVL(pod.po_release_id, -1))
3888              AND pod.destination_subinventory IS NOT NULL
3889              AND pod.po_line_id IN(SELECT pol.po_line_id
3890                                      FROM po_lines pol
3891                                     WHERE pol.item_id = p_item_id)
3892              AND ROWNUM = 1;
3893         EXCEPTION
3894           WHEN OTHERS THEN
3895             x_sub_code  := '@@@';
3896         END;
3897 
3898   	--Begin bug 4671198
3899   	IF ((x_sub_code <> '@@@') AND (l_kanban_card_id <> -999)) THEN
3900 
3901         	BEGIN
3902           		SELECT NVL(locator_id, -1)
3903           		INTO l_locator_id
3904           		FROM mtl_kanban_cards
3905           		WHERE  kanban_card_id = l_kanban_card_id
3906           		AND    subinventory_name = x_sub_code;
3907 
3908           	EXCEPTION
3909             		WHEN NO_DATA_FOUND THEN
3910               			l_locator_id := -1;
3911         	END;
3912         END IF;
3913 	--End Bug 4671198
3914 
3915 	--If the document (POD) has a sub/loc associated, set the defaulting source
3916         IF x_sub_code <> '@@@' THEN
3917           x_default_source := 'DOC';
3918         END IF;
3919         --END IF;
3920 
3921         l_progress  := '37';
3922       ELSIF p_shipment_header_id IS NOT NULL THEN
3923         l_progress  := '40';
3924 
3925         BEGIN
3926           SELECT COUNT(DISTINCT rsl.to_subinventory)
3927             INTO l_count
3928             FROM rcv_shipment_lines rsl
3929            WHERE rsl.shipment_header_id = p_shipment_header_id
3930              AND rsl.item_id = NVL(p_item_id, rsl.item_id)
3931              AND rsl.to_subinventory IS NOT NULL;
3932         EXCEPTION
3933           WHEN NO_DATA_FOUND THEN
3934             l_count  := 0;
3935         END;
3936 
3937         l_progress  := '50';
3938 
3939         IF l_count = 1 THEN
3940           l_progress  := '55';
3941 
3942           BEGIN
3943             SELECT rsl.to_subinventory
3944                  , NVL(rsl.locator_id, -1)
3945               INTO x_sub_code
3946                  , l_locator_id
3947               FROM rcv_shipment_lines rsl
3948              WHERE rsl.shipment_header_id = p_shipment_header_id
3949                AND rsl.item_id = NVL(p_item_id, rsl.item_id)
3950                AND rsl.to_subinventory IS NOT NULL
3951                AND ROWNUM = 1;
3952           EXCEPTION
3953             WHEN OTHERS THEN
3954               x_sub_code    := '@@@';
3955               l_locator_id  := -1;
3956           END;
3957           --If the document (RSL) has a sub/loc associated, set the defaulting source
3958           IF x_sub_code <> '@@@' THEN
3959             x_default_source := 'DOC';
3960           END IF;
3961         END IF;
3962       END IF;
3963     END IF;
3964 
3965     l_progress       := '60';
3966 
3967     IF x_sub_code = '@@@' THEN
3968       l_progress  := '70';
3969 
3970       BEGIN
3971         SELECT subinventory_code
3972           INTO x_sub_code
3973           FROM mtl_item_sub_defaults
3974          WHERE inventory_item_id = p_item_id
3975            AND organization_id = p_organization_id
3976            AND default_type = 2;
3977       EXCEPTION
3978         WHEN OTHERS THEN
3979           x_sub_code  := '@@@';
3980       END;
3981 
3982       l_progress  := '80';
3983 
3984       /* Bug 2323718: Changed the query so that records are filtered on Project and Task */
3985       IF x_sub_code <> '@@@' THEN
3986         l_progress  := '90';
3987 
3988         --Set the defaulting type to indicate that the sub is available in item defaults
3989         x_default_source := 'ITD';
3990 
3991         BEGIN
3992           SELECT mild.locator_id
3993             INTO l_locator_id
3994             FROM mtl_item_loc_defaults mild, mtl_item_locations mil
3995            WHERE mild.inventory_item_id = p_item_id
3996              AND mild.organization_id = p_organization_id
3997              AND mild.subinventory_code = x_sub_code
3998              AND mil.inventory_location_id = mild.locator_id
3999              AND(p_project_id IS NULL
4000                  OR(p_project_id = -9999
4001                     AND mil.project_id IS NULL)
4002                  OR mil.project_id = p_project_id)
4003              AND NVL(mil.task_id, -1) = NVL(p_task_id, NVL(mil.task_id, -1))
4004              AND mild.default_type = 2;
4005         EXCEPTION
4006           WHEN OTHERS THEN
4007             l_locator_id  := -1;
4008         END;
4009       END IF;
4010 
4011 --BUG 5972088
4012      -- Bug# 7013341, removed the condition x_locator_segs ='@@@' and added
4013      -- the condition l_locator_id = -1 instead, as x_locator_segs is assigned a
4014      -- valid value only at the end of this function once a valid locator ccid
4015      -- is identified from the above code logic.
4016      -- Hence there could be a scenario where l_locator_id is having a valid locator_id
4017      -- but x_locator_segs is still '@@@'.
4018      ELSIF x_sub_code <> '@@@' AND l_locator_id = -1 THEN
4019      -- End of bug# 7013341
4020         l_progress  := '90';
4021         --Set the defaulting type to indicate that the sub is available in item defaults
4022         x_default_source := 'ITD';
4023         BEGIN
4024           SELECT mild.locator_id
4025             INTO l_locator_id
4026             FROM mtl_item_loc_defaults mild, mtl_item_locations mil
4027            WHERE mild.inventory_item_id = p_item_id
4028              AND mild.organization_id = p_organization_id
4029              AND mild.subinventory_code = x_sub_code
4030              AND mil.inventory_location_id = mild.locator_id
4031              AND(p_project_id IS NULL
4032                  OR(p_project_id = -9999
4033                     AND mil.project_id IS NULL)
4034                  OR mil.project_id = p_project_id)
4035              AND NVL(mil.task_id, -1) = NVL(p_task_id, NVL(mil.task_id, -1))
4036              AND mild.default_type = 2;
4037         EXCEPTION
4038           WHEN OTHERS THEN
4039             l_locator_id  := -1;
4040         END;
4041 --END BUG 5972088
4042 
4043     END IF;
4044 
4045     l_progress       := '100';
4046 
4047     /* Bug 2323718: Returning INV_PROJECT.get_locsegs rather than conc. segments */
4048     IF l_locator_id <> -1 THEN
4049       l_progress  := '110';
4050 
4051       BEGIN
4052         SELECT inv_project.get_locsegs(inventory_location_id, organization_id)
4053           INTO x_locator_segs
4054           FROM mtl_item_locations
4055          WHERE organization_id = p_organization_id
4056            AND inventory_location_id = l_locator_id;
4057 
4058         x_locator_id  := l_locator_id;
4059       EXCEPTION
4060         WHEN OTHERS THEN
4061           x_locator_segs  := '@@@';
4062           x_locator_id    := -1;
4063       END;
4064     END IF;
4065 
4066     l_progress       := '120';
4067 
4068     --If the INV or PO patch levels are < J, then we can reset the OUT variable
4069     --x_default_source since it would not be used for UI validations
4070     IF ((inv_rcv_common_apis.g_inv_patch_level < inv_rcv_common_apis.g_patchset_j) OR
4071         (inv_rcv_common_apis.g_po_patch_level < inv_rcv_common_apis.g_patchset_j_po)) THEN
4072       x_default_source := 'NONE';
4073       x_lpn_context := 5;
4074     ELSE
4075       --If there was no sub/locator defaulted then reset the out variable
4076       IF ((NVL(x_sub_code, '@@@') = '@@@') OR (x_locator_id IS NULL OR x_locator_id = -1)) THEN
4077         x_default_source := 'NONE';
4078       END IF;
4079     END IF;
4080 
4081     IF (l_debug = 1) THEN
4082       print_debug('Exitting get_sub_code : 120  ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
4083       print_debug('with values: x_locator_segs: ' || x_locator_segs || 'x_locator_id: ' || x_locator_id || 'x_sub_code: ' || x_sub_code,1);
4084       print_debug('x_lpn_context: ' || x_lpn_context || 'x_default_source: ' || x_default_source, 1);
4085     END IF;
4086 
4087   EXCEPTION
4088     WHEN OTHERS THEN
4089       x_return_status  := fnd_api.g_ret_sts_unexp_error;
4090 
4091       IF SQLCODE IS NOT NULL THEN
4092         inv_mobile_helper_functions.sql_error('inv_rcv_common_apis.get_sub_code', l_progress, SQLCODE);
4093       END IF;
4094 
4095       IF (l_debug = 1) THEN
4096         print_debug('Exitting get_sub_code - other exception:' || l_progress || ' ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
4097       END IF;
4098 
4099       --
4100       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
4101         fnd_msg_pub.add_exc_msg(g_pkg_name, 'get_sub_code');
4102       END IF;
4103   END get_sub_code;
4104 
4105   -- This api is used to return the possible value that can be used for
4106   -- subinventory when the item and PO/Shipment Number/RMA are entered.
4107   -- For RMA it always returns null for subinventory.
4108   PROCEDURE get_sub_code(
4109        x_return_status      OUT NOCOPY VARCHAR2,
4110        x_msg_count          OUT NOCOPY NUMBER,
4111        x_msg_data           OUT NOCOPY VARCHAR2,
4112        x_sub_code           OUT NOCOPY VARCHAR2,
4113        x_locator_segs       OUT NOCOPY VARCHAR2,
4114        x_locator_id         OUT NOCOPY NUMBER,
4115        p_po_header_id       IN         NUMBER,
4116        p_po_release_id      IN         NUMBER,
4117        p_po_line_id         IN         NUMBER,
4118        p_shipment_header_id IN         NUMBER,
4119        p_oe_order_header_id IN         NUMBER,
4120        p_item_id            IN         NUMBER,
4121        p_organization_id    IN         NUMBER,
4122        p_lpn_id             IN         NUMBER DEFAULT NULL,
4123        p_project_id         IN         NUMBER DEFAULT NULL,
4124        p_task_id            IN         NUMBER DEFAULT NULL) IS
4125     l_lpn_context   NUMBER;
4126     l_default_source VARCHAR2(10);
4127     l_debug               NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
4128   BEGIN
4129     IF (l_debug = 1) THEN
4130       print_debug('Enter  get_sub_code: default implementation:' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
4131     END IF;
4132 
4133     get_sub_code(
4134        x_return_status      => x_return_status,
4135        x_msg_count          => x_msg_count,
4136        x_msg_data           => x_msg_data,
4137        x_sub_code           => x_sub_code,
4138        x_locator_segs       => x_locator_segs,
4139        x_locator_id         => x_locator_id,
4140        x_lpn_context        => l_lpn_context,
4141        x_default_source     => l_default_source,
4142        p_po_header_id       => p_po_header_id,
4143        p_po_release_id      => p_po_release_id,
4144        p_po_line_id         => p_po_line_id,
4145        p_shipment_header_id => p_shipment_header_id,
4146        p_oe_order_header_id => p_oe_order_header_id,
4147        p_item_id            => p_item_id,
4148        p_organization_id    => p_organization_id,
4149        p_lpn_id             => p_lpn_id,
4150        p_project_id         => p_project_id,
4151        p_task_id            => p_task_id);
4152   END get_sub_code;
4153 
4154 
4155 --Bug 3890706 - Added the procedure to select the location based on the entry in the PO form.
4156 
4157 PROCEDURE get_location_code
4158 (
4159  x_return_status      OUT NOCOPY VARCHAR2,
4160  x_location_code      OUT NOCOPY VARCHAR2,
4161  p_po_header_id       IN         NUMBER,
4162  p_item_id            IN         NUMBER,
4163  p_po_line_id         IN         NUMBER,
4164  p_po_release_id      IN         NUMBER,
4165  p_organization_id    IN	 NUMBER,
4166  p_shipment_header_id IN         NUMBER DEFAULT NULL,
4167  p_from_lpn_id        IN         NUMBER DEFAULT NULL)
4168 
4169  IS
4170    l_progress           VARCHAR2(10);
4171    l_debug              NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
4172 
4173 BEGIN
4174 
4175    x_location_code  := '' ;
4176    x_return_status  := fnd_api.g_ret_sts_success;
4177 
4178  IF (l_debug = 1) THEN
4179       print_debug('Enter get_location_code: 10:' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
4180       print_debug('Parameters passed : 10.1: p_po_header_id - ' || p_po_header_id, 4);
4181       print_debug('Parameters passed : 10.2: p_item_id - ' || p_item_id, 4);
4182       print_debug('Parameters passed : 10.3: p_po_release_id - ' || p_po_release_id, 4);
4183       print_debug('Parameters passed : 10.4: p_po_line_id - ' || p_po_line_id, 4);
4184       print_debug('Parameters passed : 10.5: org_id - ' || p_organization_id, 4);
4185       print_debug('Parameters passed : 10.6: p_shipment_header_id - ' || p_shipment_header_id, 4);
4186       print_debug('Parameters passed : 10.7: p_from_lpn_id - ' || p_from_lpn_id, 4);
4187  END IF;
4188 
4189  l_progress       := '10';
4190 
4191  IF p_po_header_id IS NOT NULL THEN
4192    SELECT distinct( hl.location_code )
4193                         into x_location_code
4194                         FROM hr_locations hl, po_line_locations poll,po_lines pol
4195                         WHERE hl.location_id            = poll.ship_to_location_id
4196                         AND poll.po_header_id           = p_po_header_id
4197                         AND poll.po_line_id             = NVL(p_po_line_id, poll.po_line_id)
4198                         AND Nvl(pol.item_id,-9999)      = NVL(p_item_id,Nvl(pol.item_id,-9999))--BUG 4500676
4199                         AND poll.ship_to_organization_id= p_organization_id
4200                         AND NVL(poll.po_release_id, -1) = NVL(p_po_release_id, NVL(poll.po_release_id, -1))
4201                         AND pol.po_line_id              = poll.po_line_id
4202 	                AND NVL(poll.po_release_id, -1) = NVL(p_po_release_id, NVL(poll.po_release_id, -1))
4203         		AND Nvl(poll.approved_flag,'N') = 'Y'
4204 		        AND Nvl(poll.cancel_flag,'N')   = 'N'
4205 		        AND Nvl(poll.closed_code,'OPEN') NOT IN ('FINALLY CLOSED','CLOSED FOR RECEIVING')
4206 	        	AND poll.shipment_type IN ('STANDARD','BLANKET','SCHEDULED');
4207 
4208 			l_progress :='20';
4209         IF (l_debug = 1) THEN
4210          print_debug('Checking the value for location: ' || x_location_code, 4);
4211         END IF;
4212   ELSIF p_shipment_header_id IS NOT NULL THEN
4213    SELECT distinct( hl.location_code )
4214                         into x_location_code
4215                         FROM hr_locations hl, rcv_shipment_lines rsl
4216                         WHERE hl.location_id            = rsl.deliver_to_location_id
4217                         AND   rsl.shipment_header_id    = p_shipment_header_id
4218                         AND   rsl.item_id               = NVL(p_item_id, rsl.item_id)
4219                         AND   rsl.to_organization_id    = p_organization_id
4220                         AND   NVL(rsl.asn_lpn_id,-1)    = NVL(p_from_lpn_id,NVL(rsl.asn_lpn_id,-1))
4221                         AND   rsl.shipment_line_status_code IN ('EXPECTED','PARTIALLY RECEIVED');
4222 
4223         IF (l_debug = 1) THEN
4224          print_debug('Checking the value for location of Shipment: ' || x_location_code, 4);
4225         END IF;
4226 
4227  END IF;
4228 
4229  EXCEPTION
4230      WHEN too_many_rows THEN
4231 	l_progress := '30' ;
4232 	IF (l_debug = 1) THEN
4233          print_debug('There are multiple values for location ', 4);
4234         END IF;
4235 	x_location_code := '';
4236 	RETURN;
4237      WHEN no_data_found THEN
4238 	l_progress:= '40' ;
4239 	x_location_code := '';
4240 	RETURN;
4241 
4242      WHEN OTHERS THEN
4243       x_return_status  := fnd_api.g_ret_sts_unexp_error;
4244 
4245       IF SQLCODE IS NOT NULL THEN
4246         inv_mobile_helper_functions.sql_error('inv_rcv_common_apis.get_location_code', l_progress, SQLCODE);
4247       END IF;
4248 
4249       IF (l_debug = 1) THEN
4250         print_debug('Exitting get_location_code - other exception: ' ||  TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
4251       END IF;
4252 
4253       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
4254         fnd_msg_pub.add_exc_msg(g_pkg_name, 'get_location_code');
4255       END IF;
4256 
4257 END get_location_code;
4258 
4259 
4260 --End of fix for the Bug 3890706.
4261 
4262 --Bug 4003683 -Added the procedure to default the revision for the item.
4263 
4264 PROCEDURE GET_REVISION_CODE(
4265       x_return_status      OUT NOCOPY VARCHAR2,
4266       x_revision_code      OUT NOCOPY VARCHAR2,
4267       p_document_type      IN  VARCHAR2 DEFAULT NULL,
4268       p_po_header_id       IN  NUMBER   DEFAULT NULL,
4269       p_po_line_id         IN  NUMBER   DEFAULT NULL,
4270       p_po_release_id      IN  NUMBER DEFAULT NULL,
4271       p_req_header_id      IN  NUMBER DEFAULT NULL,
4272       p_shipment_header_id IN  NUMBER DEFAULT NULL,
4273       p_item_id            IN  NUMBER DEFAULT NULL,
4274       p_organization_id    IN  NUMBER,
4275       p_oe_order_header_id IN  NUMBER DEFAULT NULL)  -- Bug #:5768262 Added parameter p_oe_order_header_id to default the revision of item for RMA
4276 
4277      IS
4278      l_progress           VARCHAR2(10);
4279      l_shipment_header_id NUMBER ;
4280      l_debug              NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
4281      l_return_status      VARCHAR2(1)    := fnd_api.g_ret_sts_success;
4282      l_msg_count          NUMBER;
4283      l_msg_data           VARCHAR2(4000);
4284      l_rcvreq_use_intship VARCHAR2(1) := 'N' ;
4285      l_doc_type           VARCHAR2(50) := ' ';
4286 
4287 
4288   BEGIN
4289      l_shipment_header_id :=  p_shipment_header_id;
4290      x_revision_code  := '' ;
4291      x_return_status  := fnd_api.g_ret_sts_success;
4292 
4293     IF (l_debug = 1) THEN
4294        print_debug('Enter get_revision_code: 10:' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
4295        print_debug('Parameters passed : 10.1: p_document_type - '      || p_document_type,     4);
4296        print_debug('Parameters passed : 10.2: p_po_header_id - '       || p_po_header_id,      4);
4297        print_debug('Parameters passed : 10.3: p_po_line_id - '         || p_po_line_id,        4);
4298        print_debug('Parameters passed : 10.4: p_po_release_id - '      || p_po_release_id,     4);
4299        print_debug('Parameters passed : 10.6: p_req_header_id - '      || p_req_header_id,     4);
4300        print_debug('Parameters passed : 10.7: p_shipment_header_id - ' || p_shipment_header_id,4);
4301        print_debug('Parameters passed : 10.8: p_item_id - '            || p_item_id,4);
4302        print_debug('Parameters passed : 10.8: p_organization_id - '    || p_organization_id,4);
4303        print_debug('Parameters passed : 10.8: p_oe_order_header_id  - '|| p_oe_order_header_id,4);
4304      END IF;
4305 
4306      l_progress       := '10';
4307 
4308      IF  p_document_type= 'PO' THEN
4309        l_progress       := '20';
4310 
4311        IF (l_debug = 1) THEN
4312            print_debug('Entering the document type for PO: ' || l_progress, 4);
4313        END IF;
4314 
4315        SELECT DISTINCT (pol.item_revision)
4316        INTO x_revision_code
4317        FROM po_line_locations poll,po_lines pol
4318        WHERE pol.po_header_id = p_po_header_id
4319        AND NVL(poll.po_line_id,-1)     = NVL(p_po_line_id, NVL(poll.po_line_id, -1))
4320        AND NVL(pol.item_id,-1)         = NVL(p_item_id,NVL(pol.item_id, -1))
4321        AND poll.ship_to_organization_id= p_organization_id
4322        AND NVL(poll.po_release_id, -1) = NVL(p_po_release_id, NVL(poll.po_release_id, -1))
4323        AND pol.po_line_id              = poll.po_line_id
4324        AND NVL(poll.po_release_id, -1) = NVL(p_po_release_id, NVL(poll.po_release_id, -1))
4325        AND Nvl(poll.approved_flag,'N') = 'Y'
4326        AND Nvl(poll.cancel_flag,'N')   = 'N'
4327        AND Nvl(poll.closed_code,'OPEN') NOT IN ('FINALLY CLOSED','CLOSED FOR RECEIVING')
4328        AND poll.shipment_type IN ('STANDARD','BLANKET','SCHEDULED');
4329 
4330 
4331      ELSIF p_document_type IN ( 'REQ', 'INTSHIP' ,'ASN') THEN
4332 
4333         l_progress       := '30';
4334 
4335         IF (l_debug = 1) THEN
4336           print_debug('Entering the document type for REQ/INTSHIP/ASN: ' || l_progress, 4);
4337         END IF;
4338 
4339         IF p_document_type = 'REQ' THEN
4340 
4341            get_req_shipment_header_id(
4342               x_shipment_header_id   => l_shipment_header_id
4343             , x_return_status        => x_return_status
4344             , x_msg_count            => l_msg_count
4345             , x_msg_data             => l_msg_data
4346             , p_organization_id      => p_organization_id
4347             , p_requiition_header_id => p_req_header_id
4348             , p_item_id              => p_item_id
4349             , p_rcv_txn_type         => 'RECEIPT'
4350             , p_lpn_id               => NULL
4351             ) ;
4352 
4353         END IF;
4354 
4355         IF x_return_status <> fnd_api.g_ret_sts_success THEN
4356            IF (l_debug = 1) THEN
4357               print_debug('ERROR occured while getting Shipment Header ID for REQ' , 4);
4358            END IF;
4359            RAISE fnd_api.g_exc_error;
4360         END IF;
4361 
4362         IF p_document_type = 'INTSHIP' THEN
4363           BEGIN
4364             SELECT 'Y'
4365               INTO l_rcvreq_use_intship
4366               FROM dual
4367              WHERE EXISTS ( SELECT 1
4368                               FROM rcv_shipment_lines
4369                              WHERE shipment_header_id = l_shipment_header_id
4370                                AND requisition_line_id IS NOT NULL
4371                                AND source_document_code = 'REQ'
4372                            );
4373            EXCEPTION
4374              WHEN OTHERS THEN
4375                l_rcvreq_use_intship := 'N' ;
4376            END;
4377 
4378            IF l_rcvreq_use_intship = 'Y' THEN
4379              l_doc_type := 'REQ' ;
4380            ELSE
4381              l_doc_type := 'INVENTORY';
4382            END IF;
4383         END IF;
4384 
4385         SELECT DISTINCT(rsl.item_revision)
4386         INTO x_revision_code
4387         FROM rcv_shipment_lines rsl
4388         WHERE rsl.shipment_header_id = l_shipment_header_id
4389         AND rsl.to_organization_id   = p_organization_id
4390         AND rsl.item_id = NVL(p_item_id,rsl.item_id)
4391         AND rsl.source_document_code = DECODE (p_document_type, 'INTSHIP', l_doc_type, 'REQ' ,'REQ', 'ASN','PO', 'REQ' )
4392         AND rsl.shipment_line_status_code in ('EXPECTED','PARTIALLY RECEIVED');
4393 
4394     ELSIF p_document_type = 'RMA'  THEN
4395      /* Bug #: 5768262 Adding code for handling RMA to defulting revision on receive through mobile. */
4396     /* This code will default the revision if RMA contains only one line or more than one line
4397     /* with the same revision.
4398     /* This code will be for 11.5.10 only. We are not handling this functionality in 11.5.9 for an RMA
4399     /* as unable to enter revision for an item on the order line. */
4400      IF ((inv_rcv_common_apis.g_inv_patch_level >= inv_rcv_common_apis.g_patchset_j) AND
4401            (inv_rcv_common_apis.g_po_patch_level  >= inv_rcv_common_apis.g_patchset_j_po)) THEN
4402        l_progress       := '40';
4403 
4404       print_debug('Patchset J code - Defaulting revision for RMA',4);
4405         IF (l_debug = 1) THEN
4406             print_debug('Entering the document type for RMA: ' || l_progress, 4);
4407         END IF;
4408 
4409        SELECT DISTINCT(OEL.item_revision)
4410        INTO x_revision_code
4411              FROM
4412                    OE_ORDER_LINES_all OEL,
4413                    OE_ORDER_HEADERS_all OEH
4414              WHERE OEL.LINE_CATEGORY_CODE='RETURN'
4415                AND OEL.INVENTORY_ITEM_ID = p_item_id
4416                AND nvl(OEL.SHIP_FROM_ORG_ID, OEH.SHIP_FROM_ORG_ID) = p_organization_id
4417                AND OEL.HEADER_ID = OEH.HEADER_ID
4418                AND OEH.HEADER_ID = p_oe_order_header_id
4419                AND OEL.ORDERED_QUANTITY > NVL(OEL.SHIPPED_QUANTITY,0)
4420                AND OEL.FLOW_STATUS_CODE = 'AWAITING_RETURN';
4421      END IF;
4422       /* End of modifications for the Bug #:5768262 */
4423     END IF;
4424 
4425     IF (l_debug = 1) THEN
4426      print_debug('Getting the value for revision: ' || x_revision_code, 4);
4427     END IF;
4428 
4429     l_progress       := '60';
4430 
4431      EXCEPTION
4432 
4433        WHEN NO_DATA_FOUND THEN
4434          IF (l_debug = 1) THEN
4435         print_debug('There are no values for revision to be returned ', 4);
4436          END IF;
4437          x_revision_code := '' ;
4438          RETURN;
4439 
4440 
4441        WHEN TOO_MANY_ROWS THEN
4442          IF (l_debug = 1) THEN
4443         print_debug('There is more than one value for revision to be returned ', 4);
4444          END IF;
4445          x_revision_code := '' ;
4446          RETURN;
4447 
4448        WHEN OTHERS THEN
4449           x_return_status  := fnd_api.g_ret_sts_unexp_error;
4450 
4451       IF SQLCODE IS NOT NULL THEN
4452         inv_mobile_helper_functions.sql_error('inv_rcv_common_apis.get_revision_code', l_progress, SQLCODE);
4453       END IF;
4454 
4455       IF (l_debug = 1) THEN
4456         print_debug('Exitting get_revision_code - other exception: ' ||  TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
4457       END IF;
4458 
4459       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
4460         fnd_msg_pub.add_exc_msg(g_pkg_name, 'get_revision_code');
4461       END IF;
4462 
4463     END GET_REVISION_CODE;
4464 
4465 --End of fix for Bug 4003683
4466 
4467 
4468   /**************************************************************************
4469         As part of the Bug - 2181558, this code is commented out. The process of
4470         copying the lot attributes from the parent lot to the destination lot is
4471         carried out in the INV_LOT_API_PUB package.
4472 
4473   PROCEDURE populatelotattributes
4474     (x_return_status OUT VARCHAR2,
4475      p_organization_id IN NUMBER,
4476      p_from_organization_id IN NUMBER,
4477      p_inventory_item_id IN NUMBER,
4478      p_lot_number IN VARCHAR2,
4479      p_exists IN VARCHAR2)
4480     IS
4481     l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
4482   BEGIN
4483      IF (l_debug = 1) THEN
4484         print_debug('Updating the lot number attributes',4);
4485      END IF;
4486      UPDATE mtl_lot_numbers
4487         SET (VENDOR_ID,
4488          GRADE_CODE,
4489          ORIGINATION_DATE,
4490          DATE_CODE,
4491          STATUS_ID,
4492          CHANGE_DATE,
4493          AGE,
4494          RETEST_DATE,
4495          MATURITY_DATE,
4496          LOT_ATTRIBUTE_CATEGORY,
4497          ITEM_SIZE,
4498          COLOR,
4499          VOLUME,
4500          VOLUME_UOM,
4501          PLACE_OF_ORIGIN,
4502          BEST_BY_DATE,
4503          LENGTH,
4504          LENGTH_UOM,
4505          RECYCLED_CONTENT,
4506          THICKNESS,
4507          THICKNESS_UOM,
4508          WIDTH,
4509          WIDTH_UOM,
4510          CURL_WRINKLE_FOLD,
4511          C_ATTRIBUTE1,
4512          C_ATTRIBUTE2,
4513          C_ATTRIBUTE3,
4514          C_ATTRIBUTE4,
4515          C_ATTRIBUTE5,
4516          C_ATTRIBUTE6,
4517          C_ATTRIBUTE7,
4518          C_ATTRIBUTE8,
4519          C_ATTRIBUTE9,
4520          C_ATTRIBUTE10,
4521          C_ATTRIBUTE11,
4522          C_ATTRIBUTE12,
4523          C_ATTRIBUTE13,
4524          C_ATTRIBUTE14,
4525          C_ATTRIBUTE15,
4526          C_ATTRIBUTE16,
4527          C_ATTRIBUTE17,
4528          C_ATTRIBUTE18,
4529          C_ATTRIBUTE19,
4530          C_ATTRIBUTE20,
4531          D_ATTRIBUTE1,
4532          D_ATTRIBUTE2,
4533          D_ATTRIBUTE3,
4534          D_ATTRIBUTE4,
4535          D_ATTRIBUTE5,
4536          D_ATTRIBUTE6,
4537          D_ATTRIBUTE7,
4538          D_ATTRIBUTE8,
4539          D_ATTRIBUTE9,
4540          D_ATTRIBUTE10,
4541          N_ATTRIBUTE1,
4542          N_ATTRIBUTE2,
4543          N_ATTRIBUTE3,
4544          N_ATTRIBUTE4,
4545          N_ATTRIBUTE5,
4546          N_ATTRIBUTE6,
4547          N_ATTRIBUTE7,
4548          N_ATTRIBUTE8,
4549          N_ATTRIBUTE10,
4550          SUPPLIER_LOT_NUMBER,
4551          N_ATTRIBUTE9,
4552          TERRITORY_CODE,
4553          vendor_name,
4554          attribute_category,
4555          attribute1,
4556          attribute2,
4557          attribute3,
4558          attribute4,
4559          attribute5,
4560          attribute6,
4561          attribute7,
4562          attribute8,
4563          attribute9,
4564          attribute10,
4565          attribute11,
4566          attribute12,
4567          attribute13,
4568          attribute14,
4569          attribute15
4570        ) =
4571        (SELECT VENDOR_ID,
4572            GRADE_CODE,
4573            ORIGINATION_DATE,
4574            DATE_CODE,
4575            STATUS_ID,
4576            CHANGE_DATE,
4577            AGE,
4578            RETEST_DATE,
4579            MATURITY_DATE,
4580            LOT_ATTRIBUTE_CATEGORY,
4581            ITEM_SIZE,
4582            COLOR,
4583            VOLUME,
4584            VOLUME_UOM,
4585            PLACE_OF_ORIGIN,
4586            BEST_BY_DATE,
4587            LENGTH,
4588            LENGTH_UOM,
4589            RECYCLED_CONTENT,
4590            THICKNESS,
4591            THICKNESS_UOM,
4592            WIDTH,
4593            WIDTH_UOM,
4594            CURL_WRINKLE_FOLD,
4595            C_ATTRIBUTE1,
4596            C_ATTRIBUTE2,
4597            C_ATTRIBUTE3,
4598            C_ATTRIBUTE4,
4599            C_ATTRIBUTE5,
4600            C_ATTRIBUTE6,
4601            C_ATTRIBUTE7,
4602            C_ATTRIBUTE8,
4603            C_ATTRIBUTE9,
4604            C_ATTRIBUTE10,
4605            C_ATTRIBUTE11,
4606            C_ATTRIBUTE12,
4607            C_ATTRIBUTE13,
4608            C_ATTRIBUTE14,
4609            C_ATTRIBUTE15,
4610            C_ATTRIBUTE16,
4611            C_ATTRIBUTE17,
4612            C_ATTRIBUTE18,
4613            C_ATTRIBUTE19,
4614            C_ATTRIBUTE20,
4615            D_ATTRIBUTE1,
4616            D_ATTRIBUTE2,
4617            D_ATTRIBUTE3,
4618            D_ATTRIBUTE4,
4619            D_ATTRIBUTE5,
4620            D_ATTRIBUTE6,
4621            D_ATTRIBUTE7,
4622            D_ATTRIBUTE8,
4623            D_ATTRIBUTE9,
4624            D_ATTRIBUTE10,
4625            N_ATTRIBUTE1,
4626            N_ATTRIBUTE2,
4627            N_ATTRIBUTE3,
4628            N_ATTRIBUTE4,
4629            N_ATTRIBUTE5,
4630            N_ATTRIBUTE6,
4631            N_ATTRIBUTE7,
4632            N_ATTRIBUTE8,
4633            N_ATTRIBUTE10,
4634            SUPPLIER_LOT_NUMBER,
4635            N_ATTRIBUTE9,
4636            TERRITORY_CODE,
4637            vendor_name,
4638            attribute_category,
4639            attribute1,
4640            attribute2,
4641            attribute3,
4642            attribute4,
4643            attribute5,
4644            attribute6,
4645            attribute7,
4646            attribute8,
4647            attribute9,
4648            attribute10,
4649            attribute11,
4650            attribute12,
4651            attribute13,
4652            attribute14,
4653            attribute15
4654           FROM mtl_lot_numbers
4655          WHERE organization_id = p_from_organization_id
4656            AND inventory_item_id = p_inventory_item_id
4657            AND lot_number = p_lot_number)
4658        WHERE organization_id = p_organization_id
4659          AND inventory_item_id = p_inventory_item_id
4660          AND lot_number = p_lot_number;
4661   EXCEPTION
4662      WHEN OTHERS THEN
4663         x_return_status := fnd_api.g_ret_sts_unexp_error ;
4664         IF SQLCODE IS NOT NULL THEN
4665        inv_mobile_helper_functions.sql_error('inv_rcv_common_apis.get_sub_code', '10', SQLCODE);
4666         END IF;
4667         IF (l_debug = 1) THEN
4668            print_debug('Exitting populatelotattributes - other exception:'|| to_char(sysdate, 'YYYY-MM-DD HH:DD:SS'), 1);
4669         END IF;
4670         --
4671         IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
4672         THEN
4673            fnd_msg_pub.add_exc_msg
4674              (  g_pkg_name
4675                 , 'populatelotattributes'
4676                 );
4677         END IF;
4678 
4679   END populatelotattributes;
4680   ******************************************************************************************/
4681 
4682   -- This is a wrapper to call inventory INV_LOT_API_PUB.insertLot
4683   -- it stores the inserted lot info in a global variable for
4684   -- transaction exception rollback
4685   PROCEDURE insert_dynamic_lot(
4686     p_api_version              IN            NUMBER
4687   , p_init_msg_list            IN            VARCHAR2 := fnd_api.g_false
4688   , p_commit                   IN            VARCHAR2 := fnd_api.g_false
4689   , p_validation_level         IN            NUMBER := fnd_api.g_valid_level_full
4690   , p_inventory_item_id        IN            NUMBER
4691   , p_organization_id          IN            NUMBER
4692   , p_lot_number               IN            VARCHAR2
4693   , p_expiration_date          IN OUT NOCOPY DATE
4694   , p_transaction_temp_id      IN            NUMBER DEFAULT NULL
4695   , p_transaction_action_id    IN            NUMBER DEFAULT NULL
4696   , p_transfer_organization_id IN            NUMBER DEFAULT NULL
4697   , p_status_id                IN            NUMBER
4698   , p_update_status            IN            VARCHAR2 := 'FALSE'
4699   , x_object_id                OUT NOCOPY    NUMBER
4700   , x_return_status            OUT NOCOPY    VARCHAR2
4701   , x_msg_count                OUT NOCOPY    NUMBER
4702   , x_msg_data                 OUT NOCOPY    VARCHAR2
4703   ) IS
4704     l_exists VARCHAR2(7)  := 'FALSE';
4705     v_temp   VARCHAR2(50);
4706     l_debug  NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
4707     l_stacked_messages VARCHAR2(1000);
4708     l_status_id NUMBER; --Added bug3853202
4709     l_status_enabled VARCHAR2(1); --Added bug3998321
4710     l_dest_status_enabled VARCHAR2(1);  --Added bug4035918
4711   BEGIN
4712     x_return_status  := fnd_api.g_ret_sts_success;
4713 
4714     IF (l_debug = 1) THEN
4715       print_debug('Inside insert_dynamic_lot', 4);
4716     END IF;
4717 
4718     IF inv_lot_api_pub.validate_unique_lot(
4719          p_org_id                     => p_organization_id
4720        , p_inventory_item_id          => p_inventory_item_id
4721        , p_lot_uniqueness             => NULL
4722        , p_auto_lot_number            => p_lot_number
4723        ) THEN
4724         /*Added select for bug 3853202*/
4725 
4726         IF  p_transfer_organization_id IS NOT NULL THEN
4727       BEGIN
4728 	SELECT STATUS_ID
4729 	  INTO l_status_id
4730 	FROM MTL_LOT_NUMBERS
4731 	WHERE LOT_NUMBER = p_lot_number
4732 	  AND ORGANIZATION_ID = p_transfer_organization_id
4733 	  AND INVENTORY_ITEM_ID = p_inventory_item_id;
4734 
4735 	SELECT lot_status_enabled  --Added select for bug3998321
4736 	INTO l_status_enabled
4737 	FROM
4738 	mtl_system_items
4739 	WHERE
4740 	inventory_item_id=p_inventory_item_id and
4741 	organization_id=p_transfer_organization_id;
4742 
4743 	SELECT lot_status_enabled  --Added select for bug4035918
4744 	INTO l_dest_status_enabled
4745 	FROM
4746 	mtl_system_items
4747 	WHERE
4748 	inventory_item_id=p_inventory_item_id and
4749 	organization_id=p_organization_id;
4750 
4751         SELECT 'TRUE'
4752           INTO l_exists
4753           FROM mtl_lot_numbers
4754          WHERE lot_number = p_lot_number
4755            AND organization_id = p_organization_id
4756            AND inventory_item_id = p_inventory_item_id;
4757       EXCEPTION
4758         WHEN NO_DATA_FOUND THEN
4759           l_exists  := 'FALSE';
4760       END;
4761      ELSE
4762 	l_exists := 'FALSE';
4763      END IF;
4764 
4765       IF l_status_id IS NULL THEN  --Added bug 3853202
4766 	l_status_id := p_status_id;
4767       END IF;
4768 
4769       IF (l_debug = 1) THEN
4770         print_debug('Lot uniqueness passed so inserting lot:' || l_exists, 4);
4771       END IF;
4772 
4773       inv_lot_api_pub.insertlot(
4774         p_api_version                => p_api_version
4775       , p_init_msg_list              => p_init_msg_list
4776       , p_commit                     => p_commit
4777       , p_validation_level           => p_validation_level
4778       , p_inventory_item_id          => p_inventory_item_id
4779       , p_organization_id            => p_organization_id
4780       , p_lot_number                 => p_lot_number
4781       , p_expiration_date            => p_expiration_date
4782       , p_transaction_temp_id        => p_transaction_temp_id
4783       , p_transaction_action_id      => p_transaction_action_id
4784       , p_transfer_organization_id   => p_transfer_organization_id
4785       , x_object_id                  => x_object_id
4786       , x_return_status              => x_return_status
4787       , x_msg_count                  => x_msg_count
4788       , x_msg_data                   => x_msg_data
4789 				);
4790       IF (x_return_status = fnd_api.g_ret_sts_success) THEN
4791 	 inv_mobile_helper_functions.get_stacked_messages(l_stacked_messages);
4792 	 IF (l_debug = 1) THEN
4793 	    print_debug('The following messege is removed: ' ||
4794 			l_stacked_messages,1);
4795 	 END IF;
4796 	 x_msg_count := 0;
4797 	 x_msg_data := NULL;
4798       END IF;
4799 
4800     /**************************************************************************
4801      As part of the Bug - 2181558, this code is commented out. The process of
4802      copying the lot attributes from the parent lot to the destination lot is
4803      carried out in the INV_LOT_API_PUB package.
4804 
4805        -- bug 2180480
4806        IF p_transfer_organization_id IS NOT NULL  AND
4807           l_exists = 'FALSE' THEN
4808            -- Can come here from the receiving UI only if it is an
4809            -- intransit shipment receipt or an internal req. receipt
4810            -- for a new lot number
4811            populatelotattributes(x_return_status => x_return_status,
4812                          p_lot_number => p_lot_number,
4813                          p_organization_id => p_organization_id,
4814                          p_from_organization_id => p_transfer_organization_id,
4815                          p_inventory_item_id => p_inventory_item_id,
4816                          p_exists => l_exists);
4817        END IF;
4818     ***************************************************************************/
4819     ELSE
4820       IF (l_debug = 1) THEN
4821         print_debug('Lot uniqueness did not pass so not inserting lot', 4);
4822       END IF;
4823 
4824       x_return_status  := fnd_api.g_ret_sts_unexp_error;
4825       fnd_message.set_name('INV', 'LOT_UNIQUENESS_VIOLATED');
4826       fnd_msg_pub.ADD;
4827       RAISE fnd_api.g_exc_error;
4828       RETURN;
4829     END IF;
4830 
4831     IF (((x_return_status = fnd_api.g_ret_sts_success)
4832         AND(p_update_status = 'TRUE'))
4833         OR (p_transfer_organization_id IS NOT NULL  AND
4834           l_exists = 'FALSE' AND l_status_enabled = 'Y' AND l_dest_status_enabled = 'Y')) THEN  --Added OR condition for bug 3853202, Added l_status_enabled bug3998321
4835 	  --Added l_dest_status_enabled = 'Y' bug4035918
4836       inv_material_status_grp.update_status(
4837         p_api_version_number         => p_api_version
4838       , p_init_msg_lst               => NULL
4839       , x_return_status              => x_return_status
4840       , x_msg_count                  => x_msg_count
4841       , x_msg_data                   => x_msg_data
4842       , p_update_method              => inv_material_status_pub.g_update_method_receive
4843       , p_status_id                  => l_status_id --Changed from p_status_id bug3853202
4844       , p_organization_id            => p_organization_id
4845       , p_inventory_item_id          => p_inventory_item_id
4846       , p_sub_code                   => NULL
4847       , p_locator_id                 => NULL
4848       , p_lot_number                 => p_lot_number
4849       , p_serial_number              => NULL
4850       , p_to_serial_number           => NULL
4851       , p_object_type                => 'O'
4852       );
4853     END IF;
4854   END insert_dynamic_lot;
4855 
4856   -- This is a wrapper to call inventory insert_range_serial
4857   PROCEDURE insert_range_serial(
4858     p_api_version           IN            NUMBER
4859   , p_init_msg_list         IN            VARCHAR2 := fnd_api.g_false
4860   , p_commit                IN            VARCHAR2 := fnd_api.g_false
4861   , p_validation_level      IN            NUMBER := fnd_api.g_valid_level_full
4862   , p_inventory_item_id     IN            NUMBER
4863   , p_organization_id       IN            NUMBER
4864   , p_from_serial_number    IN            VARCHAR2
4865   , p_to_serial_number      IN            VARCHAR2
4866   , p_revision              IN            VARCHAR2
4867   , p_lot_number            IN            VARCHAR2
4868   , p_primary_lot_quantity  IN            NUMBER
4869   , p_transaction_action_id IN            NUMBER
4870   , p_current_status        IN            NUMBER
4871   , p_serial_status_id      IN            NUMBER
4872   , p_update_serial_status  IN            VARCHAR2
4873   , p_inspection_required   IN            NUMBER DEFAULT NULL
4874   , p_hdr_id                IN            NUMBER
4875   , p_from_lpn_id           IN            NUMBER
4876   , p_to_lpn_id             IN            NUMBER
4877   , p_primary_uom_code      IN            VARCHAR2
4878   , p_call_pack_unpack      IN            VARCHAR2
4879   , x_return_status         OUT NOCOPY    VARCHAR2
4880   , x_msg_count             OUT NOCOPY    NUMBER
4881   , x_msg_data              OUT NOCOPY    VARCHAR2
4882   , p_subinventory          IN            VARCHAR2 DEFAULT NULL
4883   , p_locator_id            IN            NUMBER DEFAULT NULL
4884   ) IS
4885     l_object_id                  NUMBER;
4886     l_success                    NUMBER;
4887     l_temp_var                   NUMBER;
4888     l_progress                   VARCHAR2(10);
4889     l_serial_packed_in_other_lpn NUMBER;
4890     l_debug                      NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
4891     l_inv_po_j_higher            BOOLEAN := FALSE;
4892     l_rcv_serial_flag            VARCHAR2(1) := 'N';
4893   BEGIN
4894     IF (l_debug = 1) THEN
4895       print_debug('Enter insert_range_serial: 10:' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
4896     END IF;
4897 
4898     x_return_status  := fnd_api.g_ret_sts_success;
4899     l_progress       := '10';
4900     SAVEPOINT rcv_insert_range_serial_sp;
4901 
4902     -- Reported by Toshiba Issue where same Serials were getting processed
4903     -- Sanity Check to ensure if the Serials are not Yet Received/Packed into a different
4904     -- LPN i,e other than the Transaction LPN.
4905     -- LPN context 7 is added for receioving ASN's
4906     -- Added inventory_item_id check in the below sql because same serial might exist
4907     -- for two diff items.
4908     /* FP-J Lot/Serial Support Enhancement -  Check for status of Resides in Receiving (7) also */
4909     SELECT COUNT(1)
4910       INTO l_serial_packed_in_other_lpn
4911       FROM mtl_serial_numbers msn
4912      WHERE msn.current_status IN (5, 7)
4913        AND EXISTS(SELECT 'x'
4914                     FROM wms_license_plate_numbers wlpn
4915                    WHERE wlpn.lpn_context NOT IN(5, 6, 7)
4916                      AND wlpn.lpn_id = msn.lpn_id)
4917        AND msn.lpn_id IS NOT NULL
4918        AND msn.serial_number BETWEEN p_from_serial_number AND p_to_serial_number
4919        AND Length(msn.serial_number) = Length(p_from_serial_number)
4920        AND Length(p_from_serial_number) = Length(Nvl(p_to_serial_number,p_from_serial_number))
4921        AND msn.inventory_item_id = p_inventory_item_id
4922        AND ROWNUM = 1;
4923 
4924     IF l_serial_packed_in_other_lpn >= 1 THEN
4925       IF (l_debug = 1) THEN
4926         print_debug('Insert_range_serial: Serial Number already Packed/Received  with a Diff LPN '
4927           || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS')
4928         , 1);
4929       END IF;
4930 
4931       fnd_message.set_name('INV', 'INV_DUPLICATE_SERIAL');
4932       fnd_msg_pub.ADD;
4933       fnd_message.set_name('INV', 'INV_FAIL_VALIDATE_SERIAL');
4934       fnd_msg_pub.ADD;
4935       RAISE fnd_api.g_exc_error;
4936     END IF;
4937 
4938     l_progress       := '20';
4939 
4940     /* FP-J Lot/Serial Support Enhancement -
4941      * Get the patch levels for INV and PO. If they are J or higher, we do not
4942      * want to update certain columns like lpn_id, inspection_status,
4943      * subinventory, locator etc. for the serial number.
4944      * So have declared a new flag p_rcv_serial_flag which should be passed as Y
4945      * to skip the updates to the serial
4946      * If either INV or PO J is not installed, then this flag would be set to
4947      * the value 'N' so that the updates continue as usual.
4948      */
4949     IF ((inv_rcv_common_apis.g_inv_patch_level < inv_rcv_common_apis.g_patchset_j) OR
4950         (inv_rcv_common_apis.g_po_patch_level < inv_rcv_common_apis.g_patchset_j_po)) THEN
4951       l_inv_po_j_higher := FALSE;
4952       l_rcv_serial_flag := 'N';
4953     ELSE
4954       l_inv_po_j_higher := TRUE;
4955       l_rcv_serial_flag := 'Y';
4956     END IF;
4957 
4958     inv_serial_number_pub.insert_range_serial(
4959       p_api_version                => p_api_version
4960     , p_init_msg_list              => p_init_msg_list
4961     , p_commit                     => p_commit
4962     , p_validation_level           => p_validation_level
4963     , p_inventory_item_id          => p_inventory_item_id
4964     , p_organization_id            => p_organization_id
4965     , p_from_serial_number         => p_from_serial_number
4966     , p_to_serial_number           => p_to_serial_number
4967     , p_initialization_date        => SYSDATE
4968     , p_completion_date            => NULL
4969     , p_ship_date                  => NULL
4970     , p_revision                   => p_revision
4971     , p_lot_number                 => p_lot_number
4972     , p_current_locator_id         => NULL
4973     , p_subinventory_code          => NULL
4974     , p_trx_src_id                 => NULL
4975     , p_unit_vendor_id             => NULL
4976     , p_vendor_lot_number          => NULL
4977     , p_vendor_serial_number       => NULL
4978     , p_receipt_issue_type         => NULL
4979     , p_txn_src_id                 => NULL
4980     , p_txn_src_name               => NULL
4981     , p_txn_src_type_id            => NULL
4982     , p_transaction_id             => NULL
4983     , p_current_status             => p_current_status
4984     , p_parent_item_id             => NULL
4985     , p_parent_serial_number       => NULL
4986     , p_cost_group_id              => NULL
4987     , p_transaction_action_id      => p_transaction_action_id
4988     , p_transaction_temp_id        => NULL
4989     , p_status_id                  => NULL
4990     , p_inspection_status          => p_inspection_required
4991     , x_object_id                  => l_object_id
4992     , x_return_status              => x_return_status
4993     , x_msg_count                  => x_msg_count
4994     , x_msg_data                   => x_msg_data
4995     , p_rcv_serial_flag            => l_rcv_serial_flag
4996     );
4997 
4998     IF x_return_status <> fnd_api.g_ret_sts_success THEN
4999       fnd_message.set_name('INV', 'INV_LOT_COMMIT_FAILURE');
5000       fnd_msg_pub.ADD;
5001       RAISE fnd_api.g_exc_error;
5002     END IF;
5003 
5004     l_progress       := '30';
5005 
5006     IF x_return_status <> fnd_api.g_ret_sts_success THEN
5007       RAISE fnd_api.g_exc_error;
5008     END IF;
5009 
5010     /* FP-J Lot/Serial Support Enhancement
5011      * If INV and PO patchset levels are "J" or higher, then do not call packunpact
5012      * from UI since it would be handled by the receiving TM.
5013      * Similarly, need not mark the serials since it would be done in the insert_msni
5014      * API upon creating the MSNI interface records
5015      */
5016     IF (l_inv_po_j_higher = FALSE) THEN
5017 
5018       IF p_update_serial_status = 'TRUE' THEN
5019         l_progress  := '40';
5020         inv_material_status_grp.update_status(
5021           p_api_version_number         => p_api_version
5022         , p_init_msg_lst               => NULL
5023         , x_return_status              => x_return_status
5024         , x_msg_count                  => x_msg_count
5025         , x_msg_data                   => x_msg_data
5026         , p_update_method              => inv_material_status_pub.g_update_method_receive
5027         , p_status_id                  => p_serial_status_id
5028         , p_organization_id            => p_organization_id
5029         , p_inventory_item_id          => p_inventory_item_id
5030         , p_sub_code                   => NULL
5031         , p_locator_id                 => NULL
5032         , p_lot_number                 => p_lot_number
5033         , p_serial_number              => p_from_serial_number
5034         , p_to_serial_number           => p_to_serial_number
5035         , p_object_type                => 'S'
5036         );
5037       END IF;
5038 
5039       l_progress       := '50';
5040       serial_check.inv_mark_serial(
5041         from_serial_number           => p_from_serial_number
5042       , to_serial_number             => p_to_serial_number
5043       , item_id                      => p_inventory_item_id
5044       , org_id                       => p_organization_id
5045       , hdr_id                       => p_hdr_id
5046       , temp_id                      => NULL
5047       , lot_temp_id                  => NULL
5048       , success                      => l_success
5049       );
5050       l_progress       := '60';
5051 
5052       IF p_call_pack_unpack = 'TRUE' THEN
5053         l_progress  := '70';
5054         inv_rcv_std_rcpt_apis.packunpack_container(
5055           p_api_version                => p_api_version
5056         , p_init_msg_list              => p_init_msg_list
5057         , p_commit                     => p_commit
5058         , x_return_status              => x_return_status
5059         , x_msg_count                  => x_msg_count
5060         , x_msg_data                   => x_msg_data
5061         , p_from_lpn_id                => p_from_lpn_id
5062         , p_lpn_id                     => p_to_lpn_id
5063         , p_content_item_id            => p_inventory_item_id
5064         , p_revision                   => p_revision
5065         , p_lot_number                 => p_lot_number
5066         , p_from_serial_number         => p_from_serial_number
5067         , p_to_serial_number           => p_to_serial_number
5068         , p_uom                        => p_primary_uom_code
5069         , p_quantity                   => p_primary_lot_quantity
5070         , p_organization_id            => p_organization_id
5071         , p_subinventory               => p_subinventory
5072         , p_locator_id                 => p_locator_id
5073         , p_operation                  => '1'
5074         );
5075       END IF;
5076 
5077       IF x_return_status <> fnd_api.g_ret_sts_success THEN
5078         fnd_message.set_name('WMS', 'WMS_PACK_CONTAINER_FAIL');
5079         fnd_msg_pub.ADD;
5080         RAISE fnd_api.g_exc_error;
5081       END IF;
5082     ELSE
5083       print_debug('insert_range_serial: INV and PO patchset levels are J or higher. No packunpack from UI. No marking from here', 4);
5084     END IF;   --END IF check INV and PO patch levels
5085 
5086     l_progress       := '80';
5087 
5088     IF (l_debug = 1) THEN
5089       print_debug('Exit insert_range_serial 90:' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
5090     END IF;
5091   EXCEPTION
5092     WHEN fnd_api.g_exc_error THEN
5093       ROLLBACK TO rcv_insert_range_serial_sp;
5094       x_return_status  := fnd_api.g_ret_sts_error;
5095 
5096       IF (l_debug = 1) THEN
5097         print_debug('Exitting insert_range_serial - execution error:' || l_progress || ' ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
5098       END IF;
5099 
5100       --  Get message count and data
5101       fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
5102     WHEN fnd_api.g_exc_unexpected_error THEN
5103       ROLLBACK TO rcv_insert_range_serial_sp;
5104 
5105       IF (l_debug = 1) THEN
5106         print_debug('Exitting insert_range_serial - unexpected error:' || l_progress || ' ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
5107       END IF;
5108 
5109       x_return_status  := fnd_api.g_ret_sts_unexp_error;
5110       --  Get message count and data
5111       fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
5112     WHEN OTHERS THEN
5113       ROLLBACK TO rcv_insert_range_serial_sp;
5114 
5115       IF (l_debug = 1) THEN
5116         print_debug('Exitting insert_range_serial - other exceptions:' || l_progress || ' ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
5117       END IF;
5118 
5119       x_return_status  := fnd_api.g_ret_sts_unexp_error;
5120 
5121       IF SQLCODE IS NOT NULL THEN
5122         inv_mobile_helper_functions.sql_error('INV_RCV_COMMON_APIS.insert_range_serial', l_progress, SQLCODE);
5123       END IF;
5124 
5125       --
5126       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
5127         fnd_msg_pub.add_exc_msg(g_pkg_name, 'insert_range_serial');
5128       END IF;
5129 
5130       --  Get message count and data
5131       fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
5132   END;
5133 
5134   PROCEDURE update_serial_status(
5135     p_api_version          IN            NUMBER
5136   , p_init_msg_list        IN            VARCHAR2 := fnd_api.g_false
5137   , p_commit               IN            VARCHAR2 := fnd_api.g_false
5138   , p_validation_level     IN            NUMBER := fnd_api.g_valid_level_full
5139   , p_inventory_item_id    IN            NUMBER
5140   , p_organization_id      IN            NUMBER
5141   , p_from_serial_number   IN            VARCHAR2
5142   , p_to_serial_number     IN            VARCHAR2
5143   , p_current_status       IN            NUMBER
5144   , p_serial_status_id     IN            NUMBER
5145   , p_update_serial_status IN            VARCHAR2
5146   , p_lot_number           IN            VARCHAR2
5147   , p_primary_lot_quantity IN            NUMBER
5148   , p_inspection_required  IN            NUMBER
5149   , p_hdr_id               IN            NUMBER
5150   , p_from_lpn_id          IN            NUMBER
5151   , p_to_lpn_id            IN            NUMBER
5152   , p_revision             IN            VARCHAR2
5153   , p_primary_uom_code     IN            VARCHAR2
5154   , p_call_pack_unpack     IN            VARCHAR2
5155   , x_return_status        OUT NOCOPY    VARCHAR2
5156   , x_msg_count            OUT NOCOPY    NUMBER
5157   , x_msg_data             OUT NOCOPY    VARCHAR2
5158   , p_subinventory         IN            VARCHAR2 DEFAULT NULL
5159   , p_locator_id           IN            NUMBER DEFAULT NULL
5160   , p_txn_src_id           IN            VARCHAR2 DEFAULT NULL
5161   ) IS
5162     l_from_ser_number     NUMBER;
5163     l_to_ser_number       NUMBER;
5164     l_range_numbers       NUMBER;
5165     l_temp_prefix         VARCHAR2(30);
5166     l_cur_serial_number   VARCHAR2(30);
5167     l_cur_ser_number      NUMBER;
5168     l_success             NUMBER;
5169     l_serial_num_length   NUMBER;
5170     l_prefix_length       NUMBER;
5171     l_num_suffix          VARCHAR2(30);
5172     l_progress            VARCHAR2(10);
5173     l_debug               NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
5174     l_restrict_rcpt_ser   VARCHAR2(1)  := NVL(fnd_profile.VALUE('INV_RESTRICT_RCPT_SER'), '0');
5175     l_serial_control_code VARCHAR2(10);
5176     l_txn_cnt             VARCHAR2(10);
5177   BEGIN
5178     IF (l_debug = 1) THEN
5179       print_debug('Enter update_serial_status: 10:' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
5180     END IF;
5181 
5182     l_progress           := '10';
5183     x_return_status      := fnd_api.g_ret_sts_success;
5184     SAVEPOINT rcv_update_serial_sp;
5185     l_progress           := '20';
5186 
5187     --
5188     -- For RMA's and Serial Control at SALES ORDER ISSUE don't allow the same Serial to be re-received
5189     -- when the serial status is 1 and it is already received once
5190     -- Similar Bug 2685220
5191     --
5192 
5193     IF (p_txn_src_id = '12'
5194         AND l_restrict_rcpt_ser = '1') THEN
5195       IF (l_debug = 1) THEN
5196         print_debug('Update Serial Status : RMA and restrict rcpt ser is Set', 1);
5197       END IF;
5198 
5199       get_serial_ctrl(x_return_status, l_serial_control_code, p_organization_id, p_inventory_item_id);
5200 
5201       IF x_return_status <> fnd_api.g_ret_sts_success THEN
5202         IF (l_debug = 1) THEN
5203           print_debug('Update Serial Status : Failed in getting serial control code ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
5204         END IF;
5205 
5206         fnd_message.set_name('INV', 'INV_FAIL_VALIDATE_SERIAL');
5207         fnd_msg_pub.ADD;
5208         RAISE fnd_api.g_exc_error;
5209       END IF;
5210 
5211       IF l_serial_control_code = '6' THEN
5212         IF (l_debug = 1) THEN
5213           print_debug('Update Serial Status : Before Duplicate Serial Check , RMA and Serial Ctrl as SALES ISSUE', 1);
5214         END IF;
5215 
5216         BEGIN
5217           SELECT '1'
5218             INTO l_txn_cnt
5219             FROM DUAL
5220            WHERE EXISTS(
5221                    SELECT '1'
5222                      FROM mtl_serial_numbers
5223                     WHERE inventory_item_id = p_inventory_item_id
5224                       AND current_organization_id = p_organization_id
5225                       AND current_status = 1
5226                       AND serial_number BETWEEN p_from_serial_number AND p_to_serial_number
5227 		      AND Length(serial_number) = Length(p_from_serial_number)
5228 		      AND Length(p_from_serial_number) = Length(Nvl(p_to_serial_number, p_from_serial_number))
5229                       AND last_txn_source_type_id = 12);
5230 
5231           IF l_txn_cnt > 0 THEN
5232             IF (l_debug = 1) THEN
5233               print_debug('Update_serial_status: After Duplicate Serial Check , RMA and Serial Ctrl as SALES ISSUE Failed Here', 1);
5234             END IF;
5235 
5236             fnd_message.set_name('INV', 'INV_FAIL_VALIDATE_SERIAL');
5237             fnd_msg_pub.ADD;
5238             RAISE fnd_api.g_exc_error;
5239           END IF;
5240         EXCEPTION
5241           WHEN NO_DATA_FOUND THEN
5242             NULL;
5243         END;
5244       END IF;
5245     END IF;
5246 
5247     -- get the number part of the to serial
5248     inv_validate.number_from_sequence(p_to_serial_number, l_temp_prefix, l_to_ser_number);
5249     l_progress           := '30';
5250     -- get the number part of the from serial
5251     inv_validate.number_from_sequence(p_from_serial_number, l_temp_prefix, l_from_ser_number);
5252     l_progress           := '40';
5253     -- total number of serials inserted into mtl_serial_numbers
5254     l_range_numbers      := l_to_ser_number - l_from_ser_number + 1;
5255     l_serial_num_length  := LENGTH(p_from_serial_number);
5256     l_prefix_length      := LENGTH(l_temp_prefix);
5257 
5258     FOR i IN 1 .. l_range_numbers LOOP
5259       l_cur_ser_number     := l_from_ser_number + i - 1;
5260       -- concatenate the serial number to be inserted
5261       l_cur_serial_number  := l_temp_prefix || LPAD(l_cur_ser_number, l_serial_num_length - NVL(l_prefix_length, 0), '0');
5262       l_progress           := '50';
5263 
5264       /* FP-J Lot/Serial Support Enhancement
5265        * If INV and PO patchset levels are "J" or higher, then do not call update statis
5266        * from UI since it would be handled by the receiving TM.
5267        */
5268       IF ((inv_rcv_common_apis.g_inv_patch_level < inv_rcv_common_apis.g_patchset_j) OR
5269           (inv_rcv_common_apis.g_po_patch_level < inv_rcv_common_apis.g_patchset_j_po)) THEN
5270         UPDATE mtl_serial_numbers
5271            SET previous_status = current_status
5272              , current_status = p_current_status
5273              , inspection_status = p_inspection_required
5274              , lot_number = p_lot_number
5275              , revision = p_revision
5276              , current_organization_id = p_organization_id
5277         WHERE serial_number = l_cur_serial_number
5278         AND inventory_item_id = p_inventory_item_id;
5279       ELSE
5280         print_debug('update_serial_status: INV and PO patchset levels are J or higher.', 4);
5281         print_debug('update_serial_status: Updating revision lot_number if serial code of the item is predefined and current status is defined but not used', 4);
5282         UPDATE mtl_serial_numbers
5283            SET lot_number = p_lot_number
5284              , revision = p_revision
5285         WHERE serial_number = l_cur_serial_number
5286         AND inventory_item_id = p_inventory_item_id
5287         AND current_status IN (1, 4, 5, 6);
5288       END IF;   --END IF check INV and PO patch levels
5289 
5290       l_progress           := '60';
5291 
5292       IF p_update_serial_status = 'TRUE' THEN
5293         l_progress  := '70';
5294         inv_material_status_grp.update_status(
5295           p_api_version_number         => p_api_version
5296         , p_init_msg_lst               => NULL
5297         , x_return_status              => x_return_status
5298         , x_msg_count                  => x_msg_count
5299         , x_msg_data                   => x_msg_data
5300         , p_update_method              => inv_material_status_pub.g_update_method_receive
5301         , p_status_id                  => p_serial_status_id
5302         , p_organization_id            => p_organization_id
5303         , p_inventory_item_id          => p_inventory_item_id
5304         , p_sub_code                   => NULL
5305         , p_locator_id                 => NULL
5306         , p_lot_number                 => p_lot_number
5307         , p_serial_number              => l_cur_serial_number
5308         , p_to_serial_number           => NULL
5309         , p_object_type                => 'S'
5310         );
5311       END IF;
5312 
5313       l_progress           := '80';
5314 
5315       IF x_return_status <> fnd_api.g_ret_sts_success THEN
5316         RAISE fnd_api.g_exc_error;
5317       END IF;
5318     END LOOP;
5319 
5320     l_progress           := '90';
5321 
5322     /* FP-J Lot/Serial Support Enhancement
5323      * If INV and PO patchset levels are "J" or higher, then do not call packunpact
5324      * from UI since it would be handled by the receiving TM.
5325      * Similarly, need not mark the serials since it would be done in the insert_msni
5326      * API upon creating the MSNI interface records
5327      */
5328     IF ((inv_rcv_common_apis.g_inv_patch_level < inv_rcv_common_apis.g_patchset_j) OR
5329         (inv_rcv_common_apis.g_po_patch_level < inv_rcv_common_apis.g_patchset_j_po)) THEN
5330       serial_check.inv_mark_serial(
5331         from_serial_number           => p_from_serial_number
5332       , to_serial_number             => p_to_serial_number
5333       , item_id                      => p_inventory_item_id
5334       , org_id                       => p_organization_id
5335       , hdr_id                       => p_hdr_id
5336       , temp_id                      => NULL
5337       , lot_temp_id                  => NULL
5338       , success                      => l_success
5339       );
5340       l_progress           := '100';
5341 
5342       IF p_call_pack_unpack = 'TRUE' THEN
5343         l_progress  := '110';
5344         inv_rcv_std_rcpt_apis.packunpack_container(
5345           p_api_version                => p_api_version
5346         , p_init_msg_list              => p_init_msg_list
5347         , p_commit                     => p_commit
5348         , x_return_status              => x_return_status
5349         , x_msg_count                  => x_msg_count
5350         , x_msg_data                   => x_msg_data
5351         , p_from_lpn_id                => p_from_lpn_id
5352         , p_lpn_id                     => p_to_lpn_id
5353         , p_content_item_id            => p_inventory_item_id
5354         , p_revision                   => p_revision
5355         , p_lot_number                 => p_lot_number
5356         , p_from_serial_number         => p_from_serial_number
5357         , p_to_serial_number           => p_to_serial_number
5358         , p_uom                        => p_primary_uom_code
5359         , p_quantity                   => p_primary_lot_quantity
5360         , p_organization_id            => p_organization_id
5361         , p_subinventory               => p_subinventory
5362         , p_locator_id                 => p_locator_id
5363         , p_operation                  => '1'
5364         );
5365       END IF;
5366 
5367       l_progress           := '120';
5368 
5369       IF x_return_status <> fnd_api.g_ret_sts_success THEN
5370         fnd_message.set_name('WMS', 'WMS_PACK_CONTAINER_FAIL');
5371         fnd_msg_pub.ADD;
5372         RAISE fnd_api.g_exc_error;
5373       END IF;
5374     ELSE
5375       print_debug('update_serial_status: INV and PO patchset levels are J or higher. No packunpack from UI. No marking from here', 4);
5376     END IF;   --END IF check INV and PO patch levels
5377 
5378     l_progress           := '130';
5379 
5380     IF (l_debug = 1) THEN
5381       print_debug('Exit update_serial_status 140:' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
5382     END IF;
5383   EXCEPTION
5384     WHEN fnd_api.g_exc_error THEN
5385       ROLLBACK TO rcv_update_serial_sp;
5386       x_return_status  := fnd_api.g_ret_sts_error;
5387 
5388       IF (l_debug = 1) THEN
5389         print_debug('Exitting update_serial_status - execution error:' || l_progress || ' ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
5390       END IF;
5391 
5392       --  Get message count and data
5393       fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
5394     WHEN fnd_api.g_exc_unexpected_error THEN
5395       ROLLBACK TO rcv_update_serial_sp;
5396       x_return_status  := fnd_api.g_ret_sts_unexp_error;
5397 
5398       IF (l_debug = 1) THEN
5399         print_debug('Exitting update_serial_status - unexpected error:' || l_progress || ' ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
5400       END IF;
5401 
5402       --  Get message count and data
5403       fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
5404     WHEN OTHERS THEN
5405       ROLLBACK TO rcv_update_serial_sp;
5406       x_return_status  := fnd_api.g_ret_sts_unexp_error;
5407 
5408       IF (l_debug = 1) THEN
5409         print_debug('Exitting update_serial_status - other exceptions:' || l_progress || ' ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
5410       END IF;
5411 
5412       IF SQLCODE IS NOT NULL THEN
5413         inv_mobile_helper_functions.sql_error('INV_RCV_COMMON_APIS.update_serial_status', l_progress, SQLCODE);
5414       END IF;
5415 
5416       --
5417       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
5418         fnd_msg_pub.add_exc_msg(g_pkg_name, 'update_serial_status');
5419       END IF;
5420 
5421       --  Get message count and data
5422       fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
5423   END update_serial_status;
5424 
5425   PROCEDURE process_lot(
5426     p_api_version              IN            NUMBER
5427   , p_init_msg_list            IN            VARCHAR2 := fnd_api.g_false
5428   , p_commit                   IN            VARCHAR2 := fnd_api.g_false
5429   , p_validation_level         IN            NUMBER := fnd_api.g_valid_level_full
5430   , p_inventory_item_id        IN            NUMBER
5431   , p_organization_id          IN            NUMBER
5432   , p_lot_number               IN            VARCHAR2
5433   , p_expiration_date          IN OUT NOCOPY DATE
5434   , p_transaction_temp_id      IN            NUMBER DEFAULT NULL
5435   , p_transaction_action_id    IN            NUMBER DEFAULT NULL
5436   , p_transfer_organization_id IN            NUMBER DEFAULT NULL
5437   , p_status_id                IN            NUMBER
5438   , p_update_status            IN            VARCHAR2 := 'FALSE'
5439   , p_is_new_lot               IN            VARCHAR2 := 'TRUE'
5440   , p_call_pack_unpack         IN            VARCHAR2 := 'FALSE'
5441   , p_from_lpn_id              IN            NUMBER
5442   , p_to_lpn_id                IN            NUMBER
5443   , p_revision                 IN            VARCHAR2
5444   , p_lot_primary_qty          IN            NUMBER
5445   , p_primary_uom_code         IN            VARCHAR2
5446   , p_transaction_uom_code     IN            VARCHAR2 DEFAULT NULL
5447   , x_object_id                OUT NOCOPY    NUMBER
5448   , x_return_status            OUT NOCOPY    VARCHAR2
5449   , x_msg_count                OUT NOCOPY    NUMBER
5450   , x_msg_data                 OUT NOCOPY    VARCHAR2
5451   , p_subinventory             IN            VARCHAR2 DEFAULT NULL
5452   , p_locator_id               IN            NUMBER DEFAULT NULL
5453   , p_lot_secondary_qty        IN            NUMBER --OPM Convergence
5454   , p_secondary_uom_code       IN            VARCHAR2 --OPM Convergence
5455   ) IS
5456     l_progress VARCHAR2(10);
5457     l_debug    NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
5458   BEGIN
5459     IF (l_debug = 1) THEN
5460       print_debug('Enter process_lot: 10:' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
5461       print_debug('Organization ID = ' || TO_CHAR(p_organization_id), 1);
5462       print_debug('To Organization ID = ' || TO_CHAR(p_transfer_organization_id), 1);
5463       print_debug('transaction_temp_id = ' || TO_CHAR(p_transaction_temp_id), 1);
5464       print_debug('Lot Number = ' || p_lot_number, 1);
5465       print_debug('Item ID = ' || TO_CHAR(p_inventory_item_id), 1);
5466     END IF;
5467 
5468     x_return_status  := fnd_api.g_ret_sts_success;
5469     l_progress       := '10';
5470     SAVEPOINT rcv_process_lot_sp;
5471     inv_rcv_std_rcpt_apis.populate_lot_rec(
5472       p_lot_number                 => p_lot_number
5473     , p_primary_qty                => p_lot_primary_qty
5474     , p_txn_uom_code               => p_transaction_uom_code
5475     , p_org_id                     => p_organization_id
5476     , p_item_id                    => p_inventory_item_id
5477     , p_secondary_quantity         => p_lot_secondary_qty --OPM Convergence
5478     );
5479     l_progress       := '20';
5480 
5481     IF p_is_new_lot = 'TRUE' THEN
5482       l_progress  := '30';
5483       insert_dynamic_lot(
5484         p_api_version                => p_api_version
5485       , p_init_msg_list              => p_init_msg_list
5486       , p_commit                     => p_commit
5487       , p_validation_level           => p_validation_level
5488       , p_inventory_item_id          => p_inventory_item_id
5489       , p_organization_id            => p_organization_id
5490       , p_lot_number                 => p_lot_number
5491       , p_expiration_date            => p_expiration_date
5492       , p_transaction_temp_id        => p_transaction_temp_id
5493       , p_transaction_action_id      => p_transaction_action_id
5494       , p_transfer_organization_id   => p_transfer_organization_id
5495       , p_status_id                  => p_status_id
5496       , p_update_status              => p_update_status
5497       , x_object_id                  => x_object_id
5498       , x_return_status              => x_return_status
5499       , x_msg_count                  => x_msg_count
5500       , x_msg_data                   => x_msg_data
5501       );
5502     END IF;
5503 
5504     IF x_return_status <> fnd_api.g_ret_sts_success THEN
5505       fnd_message.set_name('INV', 'INV_LOT_COMMIT_FAILURE');
5506       fnd_msg_pub.ADD;
5507       RAISE fnd_api.g_exc_error;
5508     END IF;
5509 
5510     l_progress       := '40';
5511 
5512     /* FP-J Lot/Serial Support Enhancement
5513      * If INV and PO patchset levels are "J" or higher, then do not call packunpact
5514      * from UI since it would be handled by the receiving TM.
5515      */
5516     IF ((inv_rcv_common_apis.g_inv_patch_level < inv_rcv_common_apis.g_patchset_j) OR
5517         (inv_rcv_common_apis.g_po_patch_level < inv_rcv_common_apis.g_patchset_j_po)) THEN
5518       IF p_call_pack_unpack = 'TRUE' THEN
5519         l_progress  := '50';
5520         inv_rcv_std_rcpt_apis.packunpack_container(
5521           p_api_version                => p_api_version
5522         , p_init_msg_list              => p_init_msg_list
5523         , p_commit                     => p_commit
5524         , x_return_status              => x_return_status
5525         , x_msg_count                  => x_msg_count
5526         , x_msg_data                   => x_msg_data
5527         , p_from_lpn_id                => p_from_lpn_id
5528         , p_lpn_id                     => p_to_lpn_id
5529         , p_content_item_id            => p_inventory_item_id
5530         , p_revision                   => p_revision
5531         , p_lot_number                 => p_lot_number
5532         , p_quantity                   => p_lot_primary_qty
5533         , p_uom                        => p_primary_uom_code
5534         , p_organization_id            => p_organization_id
5535         , p_subinventory               => p_subinventory
5536         , p_locator_id                 => p_locator_id
5537         , p_operation                  => '1'
5538         );
5539       END IF;
5540     ELSE
5541       print_debug('process_lot: INV and PO patchset levels are J or higher. No packunpack from UI. ', 4);
5542     END IF;   --END IF check INV and PO patch levels
5543 
5544     l_progress       := '60';
5545 
5546     IF x_return_status <> fnd_api.g_ret_sts_success THEN
5547       fnd_message.set_name('WMS', 'WMS_PACK_CONTAINER_FAIL');
5548       fnd_msg_pub.ADD;
5549       RAISE fnd_api.g_exc_error;
5550     END IF;
5551 
5552     IF (l_debug = 1) THEN
5553       print_debug('Exit process_lot 70:' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
5554     END IF;
5555   EXCEPTION
5556     WHEN fnd_api.g_exc_error THEN
5557       ROLLBACK TO rcv_process_lot_sp;
5558       x_return_status  := fnd_api.g_ret_sts_error;
5559 
5560       IF (l_debug = 1) THEN
5561         print_debug('Exitting process_lot - execution error:' || l_progress || ' ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
5562       END IF;
5563 
5564       --  Get message count and data
5565       fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
5566     WHEN fnd_api.g_exc_unexpected_error THEN
5567       ROLLBACK TO rcv_process_lot_sp;
5568       x_return_status  := fnd_api.g_ret_sts_unexp_error;
5569 
5570       IF (l_debug = 1) THEN
5571         print_debug('Exitting process_lot - unexpected error:' || l_progress || ' ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
5572       END IF;
5573 
5574       --  Get message count and data
5575       fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
5576     WHEN OTHERS THEN
5577       ROLLBACK TO rcv_process_lot_sp;
5578       x_return_status  := fnd_api.g_ret_sts_unexp_error;
5579 
5580       IF (l_debug = 1) THEN
5581         print_debug('Exitting process_lot - other exceptions:' || l_progress || ' ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
5582       END IF;
5583 
5584       IF SQLCODE IS NOT NULL THEN
5585         inv_mobile_helper_functions.sql_error('INV_RCV_COMMON_APIS.process_lot', l_progress, SQLCODE);
5586       END IF;
5587 
5588       --
5589       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
5590         fnd_msg_pub.add_exc_msg(g_pkg_name, 'process_lot');
5591       END IF;
5592 
5593       --  Get message count and data
5594       fnd_msg_pub.count_and_get(
5595         p_encoded => fnd_api.g_false,
5596         p_count => x_msg_count,
5597         p_data => x_msg_data);
5598   END process_lot;
5599 
5600   PROCEDURE gen_txn_group_id IS
5601     l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
5602   BEGIN
5603     IF g_rcv_global_var.interface_group_id IS NULL THEN
5604       SELECT rcv_interface_groups_s.NEXTVAL
5605         INTO g_rcv_global_var.interface_group_id
5606         FROM DUAL;
5607     END IF;
5608   END gen_txn_group_id;
5609 
5610   PROCEDURE validate_trx_date(
5611     p_trx_date        IN            DATE
5612   , p_organization_id IN            NUMBER
5613   , p_sob_id          IN            NUMBER
5614   , x_return_status   OUT NOCOPY    VARCHAR2
5615   , x_error_code      OUT NOCOPY    VARCHAR2
5616   ) IS
5617     l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
5618   BEGIN
5619     x_return_status  := fnd_api.g_ret_sts_success;
5620 
5621     IF (p_trx_date > SYSDATE) THEN
5622       x_return_status  := fnd_api.g_ret_sts_error;
5623       fnd_message.set_name('PO', 'RCV_TRX_FUTURE_DATE_NA');
5624       fnd_msg_pub.ADD;
5625       RETURN;
5626     END IF;
5627 
5628     BEGIN
5629       IF NOT(po_dates_s.val_open_period(p_trx_date, p_sob_id, 'SQLGL', p_organization_id)) THEN
5630         x_return_status  := fnd_api.g_ret_sts_error;
5631         fnd_message.set_name('PO', 'PO_PO_ENTER_OPEN_GL_DATE');
5632         fnd_msg_pub.ADD;
5633         RETURN;
5634       END IF;
5635 
5636       IF NOT(po_dates_s.val_open_period(p_trx_date, p_sob_id, 'PO', p_organization_id)) THEN
5637         x_return_status  := fnd_api.g_ret_sts_error;
5638         fnd_message.set_name('PO', 'PO_PO_ENTER_OPEN_GL_DATE');
5639         fnd_msg_pub.ADD;
5640         RETURN;
5641       END IF;
5642     EXCEPTION
5643       WHEN OTHERS THEN
5644         x_return_status  := fnd_api.g_ret_sts_error;
5645 
5646         IF SQLCODE IS NOT NULL THEN
5647           inv_mobile_helper_functions.sql_error('INV_RCV_COMMON_APIS.validate_trx_date', '10', SQLCODE);
5648         END IF;
5649 
5650         fnd_message.set_name('PO', 'PO_PO_ENTER_OPEN_GL_DATE');
5651         fnd_msg_pub.ADD;
5652         RETURN;
5653     END;
5654 
5655     BEGIN
5656       IF NOT(po_dates_s.val_open_period(p_trx_date, p_sob_id, 'INV', p_organization_id)) THEN
5657         x_return_status  := fnd_api.g_ret_sts_error;
5658         fnd_message.set_name('PO', 'PO_INV_NO_OPEN_PERIOD');
5659         fnd_msg_pub.ADD;
5660         RETURN;
5661       END IF;
5662     EXCEPTION
5663       WHEN OTHERS THEN
5664         x_return_status  := fnd_api.g_ret_sts_error;
5665 
5666         IF SQLCODE IS NOT NULL THEN
5667           inv_mobile_helper_functions.sql_error('INV_RCV_COMMON_APIS.validate_trx_date', '20', SQLCODE);
5668         END IF;
5669 
5670         fnd_message.set_name('PO', 'PO_INV_NO_OPEN_PERIOD');
5671         fnd_msg_pub.ADD;
5672         RETURN;
5673     END;
5674   END validate_trx_date;
5675 
5676   -- Bug 2086271
5677   PROCEDURE get_req_shipment_header_id(
5678     x_shipment_header_id   OUT NOCOPY    NUMBER
5679   , x_return_status        OUT NOCOPY    VARCHAR2
5680   , x_msg_count            OUT NOCOPY    NUMBER
5681   , x_msg_data             OUT NOCOPY    VARCHAR2
5682   , p_organization_id      IN            NUMBER
5683   , p_requiition_header_id IN            NUMBER
5684   , p_item_id              IN            NUMBER
5685   , p_rcv_txn_type         IN            VARCHAR2
5686   , p_lpn_id               IN            NUMBER DEFAULT NULL
5687   ) IS
5688     l_from_org_id NUMBER;
5689     l_debug       NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
5690   BEGIN
5691     get_req_shipment_header_id(
5692       x_shipment_header_id         => x_shipment_header_id
5693     , x_from_org_id                => l_from_org_id
5694     , x_return_status              => x_return_status
5695     , x_msg_count                  => x_msg_count
5696     , x_msg_data                   => x_msg_data
5697     , p_organization_id            => p_organization_id
5698     , p_requiition_header_id       => p_requiition_header_id
5699     , p_item_id                    => p_item_id
5700     , p_rcv_txn_type               => p_rcv_txn_type
5701     , p_lpn_id                     => p_lpn_id
5702     );
5703   END get_req_shipment_header_id;
5704 
5705   PROCEDURE get_req_shipment_header_id(
5706     x_shipment_header_id   OUT NOCOPY    NUMBER
5707   , x_from_org_id          OUT NOCOPY    NUMBER
5708   , x_return_status        OUT NOCOPY    VARCHAR2
5709   , x_msg_count            OUT NOCOPY    NUMBER
5710   , x_msg_data             OUT NOCOPY    VARCHAR2
5711   , p_organization_id      IN            NUMBER
5712   , p_requiition_header_id IN            NUMBER
5713   , p_item_id              IN            NUMBER
5714   , p_rcv_txn_type         IN            VARCHAR2
5715   , p_lpn_id               IN            NUMBER DEFAULT NULL
5716   ) IS
5717     l_return_status VARCHAR2(1)   := fnd_api.g_ret_sts_success;
5718     l_msg_count     NUMBER;
5719     l_msg_data      VARCHAR2(400);
5720     l_progress      VARCHAR2(10);
5721     l_debug         NUMBER        := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
5722   BEGIN
5723     x_return_status  := fnd_api.g_ret_sts_success;
5724 
5725     IF (l_debug = 1) THEN
5726       print_debug('Enter get_req_shipment_header_id 10  ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
5727     END IF;
5728 
5729     l_progress       := '10';
5730 
5731     IF p_rcv_txn_type = 'RCVTXN' THEN
5732       l_progress  := '20';
5733 
5734       BEGIN
5735         SELECT DISTINCT rsl.shipment_header_id
5736                       , rsl.from_organization_id
5737                    INTO x_shipment_header_id
5738                       , x_from_org_id
5739                    FROM rcv_shipment_lines rsl
5740                   WHERE item_id = p_item_id
5741                     AND to_organization_id = p_organization_id
5742                     AND EXISTS(
5743                          SELECT 1
5744                            FROM po_requisition_lines prl, rcv_transactions rt, rcv_supply rs
5745                           WHERE prl.requisition_header_id = p_requiition_header_id
5746                             AND rsl.requisition_line_id = prl.requisition_line_id
5747                             AND prl.item_id = p_item_id
5748                             AND prl.source_type_code = 'INVENTORY'
5749                             AND rs.req_line_id = prl.requisition_line_id
5750                             AND rs.rcv_transaction_id = rt.transaction_id
5751                             AND rt.transaction_type <> 'UNORDERED'
5752                             AND rs.quantity > 0
5753                             AND rs.supply_type_code = 'RECEIVING'
5754                             AND rs.to_organization_id = p_organization_id
5755                             AND rt.organization_id = p_organization_id
5756                             AND(
5757                                 EXISTS(
5758                                   SELECT 1
5759                                     FROM rcv_transactions rt1
5760                                    WHERE rt1.transaction_id = rt.transaction_id
5761                                      AND rt1.inspection_status_code <> 'NOT INSPECTED'
5762                                      AND rt1.routing_header_id = 2)
5763                                 OR rt.routing_header_id <> 2
5764                                 OR rt.routing_header_id IS NULL
5765                                ));
5766       EXCEPTION
5767         WHEN TOO_MANY_ROWS THEN
5768           fnd_message.set_name('INV', 'INV_RCV_REQ_SHIP_MISMATCH');
5769           fnd_msg_pub.ADD;
5770 
5771           IF (l_debug = 1) THEN
5772             print_debug(
5773                  'get_req_shipment_header_id 20 - returns more than one shipment header ID for RCVTXN  '
5774               || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS')
5775             , 1
5776             );
5777           END IF;
5778 
5779           RAISE fnd_api.g_exc_error;
5780       END;
5781 
5782       l_progress  := '30';
5783     ELSIF p_rcv_txn_type = 'RECEIPT' THEN
5784       l_progress  := '40';
5785 
5786       IF (p_item_id IS NULL
5787           AND p_lpn_id IS NOT NULL) THEN -- through reqexp.
5788 
5789         /*
5790             Change the below code to cater to Empty LPN scenario,
5791             With Nested LPNs the given LPN may be empty but not its child LPNs.
5792         */
5793 
5794         BEGIN
5795           /* Bug 3440456 */
5796           -- For performance reason this is being as replaced as below.
5797           /*
5798           SELECT DISTINCT rsl.shipment_header_id
5799                         , rsl.from_organization_id
5800                      INTO x_shipment_header_id
5801                         , x_from_org_id
5802                      FROM rcv_shipment_lines rsl, wms_lpn_contents wlc,wms_license_plate_numbers wln
5803                     WHERE rsl.item_id = wlc.inventory_item_id
5804                       AND to_organization_id = p_organization_id
5805                       AND wln.lpn_id IN ( SELECT lpn_id
5806                                           FROM wms_license_plate_numbers
5807                                           START WITH lpn_id = p_lpn_id
5808                                           CONNECT BY parent_lpn_id = PRIOR lpn_id)
5809                       AND wlc.parent_lpn_id = wln.lpn_id
5810                       AND EXISTS(
5811                            SELECT 1
5812                              FROM po_requisition_lines_all prl, mtl_supply ms
5813                             WHERE prl.requisition_header_id = p_requiition_header_id
5814                               AND prl.requisition_header_id = ms.req_header_id
5815                               AND prl.requisition_line_id = ms.req_line_id
5816                               AND ms.supply_type_code = 'SHIPMENT'
5817                               AND ms.quantity > 0
5818                               AND ms.supply_source_id = rsl.shipment_line_id
5819                               AND prl.item_id = wlc.inventory_item_id
5820                               AND prl.item_id = ms.item_id);
5821           */
5822 
5823           SELECT DISTINCT rsl.shipment_header_id
5824                         , rsl.from_organization_id
5825                      INTO x_shipment_header_id
5826                         , x_from_org_id
5827                      FROM rcv_shipment_lines rsl, wms_lpn_contents wlc
5828                     WHERE rsl.item_id = wlc.inventory_item_id
5829                       AND to_organization_id = p_organization_id
5830                       AND wlc.parent_lpn_id IN ( SELECT lpn_id
5831                                           FROM wms_license_plate_numbers
5832                                           START WITH lpn_id = p_lpn_id
5833                                           CONNECT BY parent_lpn_id = PRIOR lpn_id)
5834                       AND EXISTS(
5835                            SELECT 1
5836                              FROM po_requisition_lines_all prl, mtl_supply ms
5837                             WHERE prl.requisition_header_id = p_requiition_header_id
5838                               AND prl.requisition_header_id = ms.req_header_id
5839                               AND prl.requisition_line_id = ms.req_line_id
5840                               AND ms.supply_type_code = 'SHIPMENT'
5841                               AND ms.quantity > 0
5842                               AND ms.supply_source_id = rsl.shipment_line_id
5843                               AND prl.item_id = wlc.inventory_item_id
5844                               AND prl.item_id = ms.item_id);
5845         EXCEPTION
5846           WHEN TOO_MANY_ROWS THEN
5847             fnd_message.set_name('INV', 'INV_RCV_REQ_SHIP_MISMATCH');
5848             fnd_msg_pub.ADD;
5849 
5850             IF (l_debug = 1) THEN
5851               print_debug('get_req_shipment_header_id for reqexp 35 - returns more than one shipment header ID for RECEIPT  ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS')
5852               , 1
5853               );
5854             END IF;
5855 
5856             RAISE fnd_api.g_exc_error;
5857           WHEN NO_DATA_FOUND THEN
5858             fnd_message.set_name('INV', 'INV_LPN_ZERO_AVAIL_QTY');
5859             fnd_msg_pub.ADD;
5860             IF (l_debug = 1) THEN
5861               print_debug('get_req_shipment_header_id for reqexp 40 retruned no rows ', 4);
5862             END IF;
5863 
5864             RAISE fnd_api.g_exc_error;
5865         END; -- Express Int Req Receiving
5866       ELSE
5867         BEGIN
5868           SELECT DISTINCT rsl.shipment_header_id
5869                         , rsl.from_organization_id
5870                      INTO x_shipment_header_id
5871                         , x_from_org_id
5872                      FROM rcv_shipment_lines rsl
5873                     WHERE item_id = p_item_id
5874                       AND to_organization_id = p_organization_id
5875                       AND EXISTS(
5876                            SELECT 1
5877                              FROM po_requisition_lines prl
5878                             WHERE prl.requisition_header_id = p_requiition_header_id
5879                               AND rsl.requisition_line_id = prl.requisition_line_id
5880                               AND prl.item_id = p_item_id);
5881         EXCEPTION
5882           WHEN TOO_MANY_ROWS THEN
5883             fnd_message.set_name('INV', 'INV_RCV_REQ_SHIP_MISMATCH');
5884             fnd_msg_pub.ADD;
5885 
5886             IF (l_debug = 1) THEN
5887               print_debug('get_req_shipment_header_id 30 - returns more than one shipment header ID for RECEIPT  ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
5888             END IF;
5889 
5890             RAISE fnd_api.g_exc_error;
5891         END;
5892 
5893         l_progress  := '50';
5894       END IF;
5895      ELSIF p_rcv_txn_type = 'INSPECT' THEN
5896       --BUG 3421219: Need to handle inspection as well
5897       BEGIN
5898         SELECT DISTINCT rsl.shipment_header_id
5899                       , rsl.from_organization_id
5900                    INTO x_shipment_header_id
5901                       , x_from_org_id
5902                    FROM rcv_shipment_lines rsl
5903                   WHERE item_id = p_item_id
5904                     AND to_organization_id = p_organization_id
5905                     AND EXISTS(
5906                          SELECT 1
5907                            FROM po_requisition_lines prl, rcv_transactions rt, rcv_supply rs
5908                           WHERE prl.requisition_header_id = p_requiition_header_id
5909                             AND rsl.requisition_line_id = prl.requisition_line_id
5910                             AND prl.item_id = p_item_id
5911                             AND prl.source_type_code = 'INVENTORY'
5912                             AND rs.req_line_id = prl.requisition_line_id
5913                             AND rs.rcv_transaction_id = rt.transaction_id
5914                             AND rt.transaction_type <> 'UNORDERED'
5915                             AND rs.quantity > 0
5916                             AND rs.supply_type_code = 'RECEIVING'
5917                             AND rs.to_organization_id = p_organization_id
5918                             AND rt.organization_id = p_organization_id
5919                             AND(EXISTS(
5920                                   SELECT 1
5921                                     FROM rcv_transactions rt1
5922                                    WHERE rt1.transaction_id = rt.transaction_id
5923                                      AND rt1.inspection_status_code = 'NOT INSPECTED'
5924                                      AND rt1.routing_header_id = 2)
5925 				));
5926       EXCEPTION
5927         WHEN TOO_MANY_ROWS THEN
5928           fnd_message.set_name('INV', 'INV_RCV_REQ_SHIP_MISMATCH');
5929           fnd_msg_pub.ADD;
5930 
5931           IF (l_debug = 1) THEN
5932             print_debug(
5933                  'get_req_shipment_header_id 20 - returns more than one shipment header ID for RCVTXN  '
5934               || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS')
5935             , 1
5936             );
5937           END IF;
5938 
5939           RAISE fnd_api.g_exc_error;
5940       END;
5941 
5942       l_progress  := '30';
5943     ELSE
5944       l_progress  := '60';
5945       fnd_message.set_name('INV', 'INV_RCV_TXN_NOT_DEFINED');
5946       fnd_msg_pub.ADD;
5947 
5948       IF (l_debug = 1) THEN
5949         print_debug('get_req_shipment_header_id 30 - receiving Txn type ' || p_rcv_txn_type || ' not defined. ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
5950       END IF;
5951 
5952       RAISE fnd_api.g_exc_unexpected_error;
5953       l_progress  := '70';
5954     END IF;
5955 
5956     IF (l_debug = 1) THEN
5957       print_debug('Exitting get_req_shipment_header_id 60 - x_shipment_header_id = ' || x_shipment_header_id || '  ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
5958     END IF;
5959   EXCEPTION
5960     WHEN fnd_api.g_exc_error THEN
5961       x_return_status  := fnd_api.g_ret_sts_error;
5962 
5963       IF (l_debug = 1) THEN
5964         print_debug('Exiting get_req_shipment_header_id - execution error:' || l_progress || ' ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
5965       END IF;
5966 
5967       --  Get message count and data
5968       fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
5969     WHEN fnd_api.g_exc_unexpected_error THEN
5970       x_return_status  := fnd_api.g_ret_sts_unexp_error;
5971 
5972       IF (l_debug = 1) THEN
5973         print_debug('Exitting get_req_shipment_header_id - unexpected error:' || l_progress || ' '
5974           || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS')
5975         , 1);
5976       END IF;
5977 
5978       --  Get message count and data
5979       fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
5980     WHEN OTHERS THEN
5981       x_return_status  := fnd_api.g_ret_sts_unexp_error;
5982 
5983       IF (l_debug = 1) THEN
5984         print_debug('Exitting get_req_shipment_header_id - other exceptions:' || l_progress || ' '
5985           || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS')
5986         , 1);
5987       END IF;
5988 
5989       IF SQLCODE IS NOT NULL THEN
5990         inv_mobile_helper_functions.sql_error('INV_RCV_COMMON_APIS.get_req_shipment_header_id', l_progress, SQLCODE);
5991       END IF;
5992       fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
5993   END;
5994 
5995   PROCEDURE do_check(
5996     p_organization_id     IN            NUMBER
5997   , p_inventory_item_id   IN            NUMBER
5998   , p_transaction_type_id IN            NUMBER
5999   , p_primary_quantity    IN            NUMBER
6000   , x_return_status       OUT NOCOPY    VARCHAR2
6001   , x_msg_data            OUT NOCOPY    VARCHAR2
6002   , x_msg_count           OUT NOCOPY    NUMBER
6003   ) IS
6004     l_progress     VARCHAR2(10);
6005     l_check_result VARCHAR2(1);
6006     l_seq_num      NUMBER;
6007     l_debug        NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
6008   BEGIN
6009     x_return_status  := fnd_api.g_ret_sts_success;
6010     l_progress       := '10';
6011     --
6012     inv_shortcheckexec_pvt.checkprerequisites(
6013       p_api_version                => 1.0
6014     , p_init_msg_list              => 'F'
6015     , x_return_status              => x_return_status
6016     , x_msg_count                  => x_msg_count
6017     , x_msg_data                   => x_msg_data
6018     , p_sum_detail_flag            => 2
6019     , p_organization_id            => p_organization_id
6020     , p_inventory_item_id          => p_inventory_item_id
6021     , p_transaction_type_id        => p_transaction_type_id
6022     , x_check_result               => l_check_result
6023     );
6024 
6025     --
6026     IF x_return_status <> fnd_api.g_ret_sts_success THEN
6027       fnd_message.set_name('INV', 'INV_RCV_SHORTAGE_FAILED');
6028       fnd_msg_pub.ADD;
6029       RAISE fnd_api.g_exc_error;
6030     END IF;
6031 
6032     l_progress       := '20';
6033 
6034     IF (l_debug = 1) THEN
6035       print_debug('do_check check_result - ' || l_check_result, 4);
6036     END IF;
6037 
6038     --
6039     IF l_check_result = 'T' THEN
6040       inv_shortcheckexec_pvt.execcheck(
6041         p_api_version                => 1.0
6042       , p_init_msg_list              => 'F'
6043       , p_commit                     => 'F'
6044       , x_return_status              => x_return_status
6045       , x_msg_count                  => x_msg_count
6046       , x_msg_data                   => x_msg_data
6047       , p_sum_detail_flag            => 2
6048       , p_organization_id            => p_organization_id
6049       , p_inventory_item_id          => p_inventory_item_id
6050       , p_comp_att_qty_flag          => 1
6051       , p_primary_quantity           => p_primary_quantity
6052       , x_seq_num                    => l_seq_num
6053       , x_check_result               => l_check_result
6054       );
6055 
6056       --
6057       IF x_return_status <> fnd_api.g_ret_sts_success THEN
6058         fnd_message.set_name('INV', 'INV_RCV_SHORTAGE_FAILED');
6059         fnd_msg_pub.ADD;
6060         RAISE fnd_api.g_exc_error;
6061       END IF;
6062 
6063       l_progress  := '30';
6064 
6065       --Bug #4059722
6066       --Need to clear the quantity tree cache for subsequent calls
6067       inv_quantity_tree_pub.clear_quantity_cache;
6068 
6069       IF l_check_result = 'T' THEN
6070         fnd_message.set_name('INV', 'INV_RCV_SHORTAGE_EXISTS');
6071         fnd_msg_pub.ADD;
6072         x_return_status  := 'W';
6073       END IF;
6074     END IF;
6075   EXCEPTION
6076     WHEN fnd_api.g_exc_error THEN
6077       x_return_status  := fnd_api.g_ret_sts_error;
6078 
6079       IF (l_debug = 1) THEN
6080         print_debug('Exiting do_check - execution error:' || l_progress || ' ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
6081       END IF;
6082 
6083       --  Get message count and data
6084       fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
6085     WHEN fnd_api.g_exc_unexpected_error THEN
6086       x_return_status  := fnd_api.g_ret_sts_unexp_error;
6087 
6088       IF (l_debug = 1) THEN
6089         print_debug('Exitting do_check - unexpected error:' || l_progress || ' ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
6090       END IF;
6091 
6092       --  Get message count and data
6093       fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
6094     WHEN OTHERS THEN
6095       x_return_status  := fnd_api.g_ret_sts_unexp_error;
6096 
6097       IF (l_debug = 1) THEN
6098         print_debug('Exitting get_req_shipment_header_id - other exceptions:' || l_progress || ' '
6099           || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS')
6100         , 1);
6101       END IF;
6102 
6103       IF SQLCODE IS NOT NULL THEN
6104         inv_mobile_helper_functions.sql_error('INV_RCV_COMMON_APIS.get_req_shipment_header_id', l_progress, SQLCODE);
6105       END IF;
6106 
6107       --  Get message count and data
6108       fnd_msg_pub.count_and_get(
6109         p_encoded => fnd_api.g_false,
6110         p_count => x_msg_count,
6111         p_data => x_msg_data);
6112   END do_check;
6113 
6114   /*
6115    * Procedure to to get serial control of the item at
6116    * source org. Called directly in the case of Intransit
6117    * shipment transaction.
6118    * For Int Req, the overloaded method calls this procedure
6119    * Written as part of fix for Bug #1751998
6120    */
6121   PROCEDURE get_serial_ctrl(
6122     x_return_status  OUT NOCOPY    VARCHAR2
6123   , x_serial_control OUT NOCOPY    NUMBER
6124   , p_from_org_id    IN            NUMBER
6125   , p_item_id        IN            NUMBER
6126   ) IS
6127     l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
6128   BEGIN
6129     x_return_status  := 'S';
6130 
6131     SELECT serial_number_control_code
6132       INTO x_serial_control
6133       FROM mtl_system_items
6134      WHERE inventory_item_id = p_item_id
6135        AND organization_id = p_from_org_id;
6136   EXCEPTION
6137     WHEN OTHERS THEN
6138       x_return_status  := 'F';
6139   END get_serial_ctrl;
6140 
6141   /* Procedure to get the project and task from the
6142      source document
6143   */
6144   PROCEDURE get_document_project_task(
6145     x_return_status       OUT NOCOPY    VARCHAR2
6146   , x_project_tasks_count OUT NOCOPY    NUMBER
6147   , x_distributions_count OUT NOCOPY    NUMBER
6148   , p_document_type       IN            VARCHAR2
6149   , p_po_header_id        IN            NUMBER
6150   , p_po_line_id          IN            NUMBER
6151   , p_oe_header_id        IN            NUMBER
6152   , p_req_header_id       IN            NUMBER
6153   , p_shipment_header_id  IN            NUMBER
6154   , p_item_id             IN            NUMBER
6155   , p_item_rev            IN            VARCHAR2
6156   ) IS
6157     l_project_tasks_count NUMBER       := 0;
6158     l_distributions_count NUMBER       := 0;
6159     l_progress            VARCHAR2(10);
6160     l_debug               NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
6161   BEGIN
6162     IF (l_debug = 1) THEN
6163       print_debug('Entered get_document_project_task ' || p_document_type, 4);
6164     END IF;
6165 
6166     l_progress             := '10';
6167     x_return_status        := fnd_api.g_ret_sts_success;
6168 
6169     IF p_document_type = 'PO' THEN
6170       SELECT   COUNT(COUNT(*)) -- get the no of project/tasks combinations
6171           INTO l_project_tasks_count
6172           FROM po_distributions_all pod, po_lines_all pol
6173          WHERE pod.po_header_id = p_po_header_id
6174            AND pod.po_line_id = NVL(p_po_line_id, pod.po_line_id)
6175            AND pod.project_id IS NOT NULL
6176            AND pod.po_header_id = pol.po_header_id
6177            AND pod.po_line_id = pol.po_line_id
6178            AND (p_item_id IS NULL OR pol.item_id = p_item_id)
6179            AND ( (p_item_rev IS NULL OR pol.item_revision IS NULL) OR
6180                  (p_item_rev IS NOT NULL AND pol.item_revision IS NOT NULL
6181                   AND pol.item_revision = p_item_rev)
6182                )
6183       GROUP BY pod.project_id, pod.task_id;
6184 
6185       SELECT COUNT(*)
6186         INTO l_distributions_count
6187         FROM po_distributions_all pod, po_lines_all pol
6188        WHERE pod.po_header_id = p_po_header_id
6189          AND pod.po_line_id = NVL(p_po_line_id, pod.po_line_id)
6190          AND pod.po_header_id = pol.po_header_id
6191          AND pod.po_line_id = pol.po_line_id
6192          AND (p_item_id IS NULL OR pol.item_id = p_item_id)
6193          AND ( (p_item_rev IS NULL OR pol.item_revision IS NULL) OR
6194                (p_item_rev IS NOT NULL AND pol.item_revision IS NOT NULL
6195                 AND pol.item_revision = p_item_rev)
6196              );
6197 
6198     ELSIF p_document_type = 'ASN' THEN
6199       SELECT   COUNT(COUNT(*))
6200           INTO l_project_tasks_count
6201           FROM po_distributions_all pod, rcv_shipment_lines rsl
6202          WHERE pod.po_header_id = rsl.po_header_id
6203            AND rsl.shipment_header_id = p_shipment_header_id
6204            AND (p_item_id IS NULL OR rsl.item_id = p_item_id)
6205            AND ( (p_item_rev IS NULL OR rsl.item_revision IS NULL) OR
6206                  (p_item_rev IS NOT NULL AND rsl.item_revision IS NOT NULL
6207                   AND rsl.item_revision = p_item_rev)
6208                )
6209            AND pod.project_id IS NOT NULL
6210       GROUP BY project_id, task_id;
6211 
6212       SELECT   COUNT(COUNT(*))
6213           INTO l_distributions_count
6214           FROM po_distributions_all pod, rcv_shipment_lines rsl
6215          WHERE pod.po_header_id = rsl.po_header_id
6216            AND rsl.po_line_id = pod.po_line_id(+)
6217            AND rsl.po_line_location_id = pod.line_location_id(+)
6218            AND rsl.shipment_header_id = p_shipment_header_id
6219            AND (p_item_id IS NULL OR rsl.item_id = p_item_id)
6220            AND ( (p_item_rev IS NULL OR rsl.item_revision IS NULL) OR
6221                  (p_item_rev IS NOT NULL AND rsl.item_revision IS NOT NULL
6222                   AND rsl.item_revision = p_item_rev)
6223               )
6224       GROUP BY project_id, task_id;
6225     ELSIF p_document_type = 'REQ' THEN
6226       SELECT   COUNT(COUNT(*))
6227           INTO l_project_tasks_count
6228           FROM po_req_distributions_all pod, po_requisition_lines_all pol
6229          WHERE pol.requisition_header_id = p_req_header_id
6230            AND pod.requisition_line_id = pol.requisition_line_id
6231            AND pod.project_id IS NOT NULL
6232            AND (p_item_id IS NULL OR pol.item_id = p_item_id)
6233            AND ( (p_item_rev IS NULL OR pol.item_revision IS NULL) OR
6234                  (p_item_rev IS NOT NULL AND pol.item_revision IS NOT NULL
6235                   AND pol.item_revision = p_item_rev)
6236                )
6237       GROUP BY project_id, task_id;
6238 
6239       SELECT COUNT(*)
6240         INTO l_distributions_count
6241         FROM po_req_distributions_all pod, po_requisition_lines_all pol
6242        WHERE pol.requisition_header_id = p_req_header_id
6243          AND pod.requisition_line_id = pol.requisition_line_id
6244          AND (p_item_id IS NULL OR pol.item_id = p_item_id)
6245          AND ( (p_item_rev IS NULL OR pol.item_revision IS NULL) OR
6246                (p_item_rev IS NOT NULL AND pol.item_revision IS NOT NULL
6247                 AND pol.item_revision = p_item_rev)
6248              );
6249 
6250  /*Added as part of bug - 5928199*/
6251     ELSIF p_document_type = 'INTSHIP' THEN
6252       IF (l_debug = 1) THEN
6253         print_debug('p_document_type: ' || p_document_type, 4);
6254 	print_debug('p_req_header_id: ' || p_req_header_id, 4);
6255 	print_debug('p_item_id: ' || p_item_id, 4);
6256 	print_debug('p_item_rev: ' || p_item_rev, 4);
6257       END IF;
6258 
6259       SELECT   COUNT(COUNT(*))
6260           INTO l_project_tasks_count
6261           FROM po_req_distributions_all pod, po_requisition_lines_all pol
6262          WHERE pol.requisition_header_id = p_req_header_id
6263            AND pod.requisition_line_id = pol.requisition_line_id
6264            AND pod.project_id IS NOT NULL
6265            AND (p_item_id IS NULL OR pol.item_id = p_item_id)
6266            AND ( (p_item_rev IS NULL OR pol.item_revision IS NULL) OR
6267                  (p_item_rev IS NOT NULL AND pol.item_revision IS NOT NULL
6268                   AND pol.item_revision = p_item_rev)
6269                )
6270       GROUP BY project_id, task_id;
6271       IF (l_debug = 1) THEN
6272         print_debug('l_project_tasks_count: ' || l_project_tasks_count, 4);
6273       END IF;
6274 
6275 
6276       SELECT COUNT(*)
6277         INTO l_distributions_count
6278         FROM po_req_distributions_all pod, po_requisition_lines_all pol
6279        WHERE pol.requisition_header_id = p_req_header_id
6280          AND pod.requisition_line_id = pol.requisition_line_id
6281          AND (p_item_id IS NULL OR pol.item_id = p_item_id)
6282          AND ( (p_item_rev IS NULL OR pol.item_revision IS NULL) OR
6283                (p_item_rev IS NOT NULL AND pol.item_revision IS NOT NULL
6284                 AND pol.item_revision = p_item_rev)
6285              );
6286       IF (l_debug = 1) THEN
6287         print_debug('l_distributions_count: ' || l_distributions_count, 4);
6288       END IF;
6289    /*End of Bug - 5928199*/
6290 
6291     ELSIF p_document_type = 'RMA' THEN
6292       SELECT   COUNT(COUNT(*))
6293           INTO l_project_tasks_count
6294           FROM oe_order_lines l
6295          WHERE l.line_category_code = 'RETURN'
6296            AND l.header_id = p_oe_header_id
6297            AND l.project_id IS NOT NULL
6298            AND (p_item_id IS NULL OR l.inventory_item_id = p_item_id)
6299            AND ( (p_item_rev IS NULL OR l.item_revision IS NULL) OR
6300                  (p_item_rev IS NOT NULL AND l.item_revision IS NOT NULL
6301                   AND l.item_revision = p_item_rev)
6302                )
6303       GROUP BY project_id, task_id;
6304 
6305       SELECT COUNT(*)
6306         INTO l_distributions_count
6307         FROM oe_order_lines l
6308        WHERE l.line_category_code = 'RETURN'
6309          AND l.header_id = p_oe_header_id
6310          AND (p_item_id IS NULL OR l.inventory_item_id = p_item_id)
6311          AND ( (p_item_rev IS NULL OR l.item_revision IS NULL) OR
6312                (p_item_rev IS NOT NULL AND l.item_revision IS NOT NULL
6313                 AND l.item_revision = p_item_rev)
6314              );
6315     ELSIF p_document_type = 'RECEIPT' THEN
6316       IF p_po_header_id IS NOT NULL THEN
6317         SELECT   COUNT(COUNT(*)) -- get the no of project/tasks combinations
6318             INTO l_project_tasks_count
6319             FROM po_distributions_all pod, po_lines_all pol
6320            WHERE pod.po_header_id = p_po_header_id
6321              AND pod.project_id IS NOT NULL
6322              AND pod.po_header_id = pol.po_header_id
6323              AND pod.po_line_id = pol.po_line_id
6324              AND (p_item_id IS NULL OR pol.item_id = p_item_id)
6325              AND ( (p_item_rev IS NULL OR pol.item_revision IS NULL) OR
6326                    (p_item_rev IS NOT NULL AND pol.item_revision IS NOT NULL
6327                     AND pol.item_revision = p_item_rev)
6328                   )
6329         GROUP BY pod.project_id, pod.task_id;
6330 
6331         SELECT COUNT(*)
6332           INTO l_distributions_count
6333           FROM po_distributions_all pod, po_lines_all pol
6334          WHERE pod.po_header_id = p_po_header_id
6335            AND pod.po_header_id = pol.po_header_id
6336            AND pod.po_line_id = pol.po_line_id
6337            AND (p_item_id IS NULL OR pol.item_id = p_item_id)
6338            AND ( (p_item_rev IS NULL OR pol.item_revision IS NULL) OR
6339                  (p_item_rev IS NOT NULL AND pol.item_revision IS NOT NULL
6340                   AND pol.item_revision = p_item_rev)
6341                );
6342       ELSIF p_oe_header_id IS NOT NULL THEN
6343         SELECT   COUNT(COUNT(*))
6344             INTO l_project_tasks_count
6345             FROM oe_order_lines l
6346            WHERE l.line_category_code = 'RETURN'
6347              AND l.header_id = p_oe_header_id
6348              AND l.project_id IS NOT NULL
6349              AND (p_item_id IS NULL OR l.inventory_item_id = p_item_id)
6350              AND ( (p_item_rev IS NULL OR l.item_revision IS NULL) OR
6351                  (p_item_rev IS NOT NULL AND l.item_revision IS NOT NULL
6352                   AND l.item_revision = p_item_rev)
6353                )
6354         GROUP BY project_id, task_id;
6355 
6356         SELECT COUNT(*)
6357           INTO l_distributions_count
6358           FROM oe_order_lines l
6359          WHERE l.line_category_code = 'RETURN'
6360            AND l.header_id = p_oe_header_id
6361            AND (p_item_id IS NULL OR l.inventory_item_id = p_item_id)
6362            AND ( (p_item_rev IS NULL OR l.item_revision IS NULL) OR
6363                  (p_item_rev IS NOT NULL AND l.item_revision IS NOT NULL
6364                   AND l.item_revision = p_item_rev)
6365                );
6366       END IF;
6367     END IF;
6368 
6369     IF (l_debug = 1) THEN
6370       print_debug('get_document_project_task: Project count ' || TO_CHAR(l_project_tasks_count) ||
6371                   'distribution count: ' || TO_CHAR(l_distributions_count), 4);
6372     END IF;
6373     x_project_tasks_count  := l_project_tasks_count;
6374     x_distributions_count  := l_distributions_count;
6375   EXCEPTION
6376     WHEN NO_DATA_FOUND THEN
6377       l_progress             := '20';
6378       x_return_status        := fnd_api.g_ret_sts_error;
6379       x_project_tasks_count  := 0;
6380 
6381       IF (l_debug = 1) THEN
6382         print_debug('in get_document_project_task ' || l_progress, 4);
6383       END IF;
6384     WHEN OTHERS THEN
6385       l_progress             := '30';
6386       x_return_status        := fnd_api.g_ret_sts_error;
6387       x_project_tasks_count  := 0;
6388 
6389       IF (l_debug = 1) THEN
6390         print_debug('in get_document_project_task ' || l_progress, 4);
6391       END IF;
6392   END;
6393 
6394   /*
6395    * Procedure to to get serial control of the item at
6396    * source org. Called in the case of Internal
6397    * Requisition transaction.
6398    * First get the source org corresponding to the current line
6399    * and then call the overloaded procedure above to get the
6400    * serial control code at the source org.
6401    * Written as part of fix for Bug #1751998
6402    */
6403   PROCEDURE get_serial_ctrl(
6404     x_return_status  OUT NOCOPY    VARCHAR2
6405   , x_serial_control OUT NOCOPY    NUMBER
6406   , p_to_org_id      IN            NUMBER
6407   , p_ship_head_id   IN            NUMBER
6408   , p_requisition_id IN            NUMBER
6409   , p_item_id        IN            NUMBER
6410   ) IS
6411     l_from_org_id NUMBER := 0;
6412     l_debug       NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
6413   BEGIN
6414     x_return_status  := 'S';
6415 
6416     /*Get the From Organization for the item using shipment header
6417       Id, Requisition #, To Organization Id and Item ID
6418     */
6419     SELECT DISTINCT rsl.from_organization_id
6420                INTO l_from_org_id
6421                FROM rcv_shipment_lines rsl
6422               WHERE item_id = p_item_id
6423                 AND to_organization_id = p_to_org_id
6424                 AND shipment_header_id = p_ship_head_id
6425                 AND EXISTS(
6426                      SELECT 1
6427                        FROM po_requisition_lines prl
6428                       WHERE prl.requisition_header_id = p_requisition_id
6429                         AND rsl.requisition_line_id = prl.requisition_line_id
6430                         AND prl.item_id = p_item_id);
6431 
6432     --Get the serial control of the item at the source org
6433     inv_rcv_common_apis.get_serial_ctrl(x_return_status => x_return_status, x_serial_control => x_serial_control
6434     , p_from_org_id                => l_from_org_id, p_item_id => p_item_id);
6435   EXCEPTION
6436     WHEN OTHERS THEN
6437       x_return_status  := 'F';
6438   END get_serial_ctrl;
6439 
6440   -- MANEESH - BEGIN CHANGES - FOR CROSS REFERENCE ITEM CREATION
6441 
6442   PROCEDURE create_cross_reference(
6443     p_api_version          IN            NUMBER
6444   , p_init_msg_list        IN            VARCHAR2 := fnd_api.g_false
6445   , p_commit               IN            VARCHAR2 := fnd_api.g_false
6446   , p_organization_id      IN            NUMBER
6447   , p_inventory_item_id    IN            NUMBER
6448   , p_cross_reference      IN            VARCHAR2
6449   , p_cross_reference_type IN            VARCHAR2
6450   , x_return_status        OUT NOCOPY    VARCHAR2
6451   , x_msg_count            OUT NOCOPY    NUMBER
6452   , x_msg_data             OUT NOCOPY    VARCHAR2
6453   ) IS
6454     l_progress          VARCHAR2(10);
6455     l_cross_reference   VARCHAR2(25);
6456     l_inventory_item_id NUMBER;
6457     l_user_id           NUMBER;
6458     l_login_id          NUMBER;
6459     l_debug             NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
6460   BEGIN
6461     l_progress       := '0';
6462 
6463     IF (l_debug = 1) THEN
6464       print_debug('Entered CREATE_CROSS_REFERENCE - Progress = ' || l_progress, 9);
6465     END IF;
6466 
6467     x_return_status  := fnd_api.g_ret_sts_success;
6468     SAVEPOINT rcv_create_cross_reference_sp;
6469 
6470     -- Make sure that cross_reference does not exist as a master item.
6471     BEGIN
6472       SELECT inventory_item_id
6473         INTO l_inventory_item_id
6474         FROM mtl_system_items_kfv
6475        WHERE concatenated_segments = p_cross_reference;
6476 
6477       l_progress  := '10';
6478 
6479       IF (l_debug = 1) THEN
6480         print_debug('Cross Reference matches a master item - Progress = ' || l_progress, 9);
6481       END IF;
6482 
6483       fnd_message.set_name('INV', 'INV_CROSS_REF_MATCHES_ITEM');
6484       fnd_msg_pub.ADD;
6485       RAISE fnd_api.g_exc_error;
6486     EXCEPTION
6487       WHEN NO_DATA_FOUND THEN
6488         l_progress  := '15';
6489     END;
6490 
6491     -- Make sure that cross_ref_item does not already exist.
6492     BEGIN
6493       SELECT cross_reference
6494         INTO l_cross_reference
6495         FROM mtl_cross_references
6496        WHERE cross_reference = p_cross_reference
6497          AND cross_reference_type = p_cross_reference_type
6498          AND organization_id = p_organization_id;
6499 
6500       l_progress  := '20';
6501 
6502       IF (l_debug = 1) THEN
6503         print_debug('Cross Reference already exists - Progress =  ' || l_progress, 9);
6504       END IF;
6505 
6506       fnd_message.set_name('INV', 'INV_CROSS_REF_EXISTS');
6507       fnd_msg_pub.ADD;
6508       RAISE fnd_api.g_exc_error;
6509     EXCEPTION
6510       WHEN NO_DATA_FOUND THEN
6511         l_progress  := '25';
6512     END;
6513 
6514     l_user_id        := fnd_global.user_id;
6515     l_login_id       := fnd_global.login_id;
6516     l_progress       := '30';
6517 
6518     -- Insert the record in mtl_cross_references
6519     INSERT INTO mtl_cross_references
6520                 (
6521                  inventory_item_id
6522                , organization_id
6523                , cross_reference_type
6524                , cross_reference
6525                , last_update_date
6526                , last_updated_by
6527                , creation_date
6528                , created_by
6529                , last_update_login
6530                , org_independent_flag
6531                 )
6532          VALUES (
6533                  p_inventory_item_id
6534                , p_organization_id
6535                , p_cross_reference_type
6536                , p_cross_reference
6537                , SYSDATE
6538                , l_user_id
6539                , SYSDATE
6540                , l_user_id
6541                , l_login_id
6542                , 'N'
6543                 );
6544 
6545     x_return_status  := fnd_api.g_ret_sts_success;
6546     l_progress       := '40';
6547 
6548     IF (l_debug = 1) THEN
6549       print_debug('create_cross_reference complete - progress = ' || l_progress, 9);
6550     END IF;
6551   EXCEPTION
6552     WHEN fnd_api.g_exc_error THEN
6553       ROLLBACK TO rcv_create_cross_reference_sp;
6554       x_return_status  := fnd_api.g_ret_sts_error;
6555       fnd_msg_pub.count_and_get(
6556         p_encoded => fnd_api.g_false,
6557         p_count => x_msg_count,
6558         p_data => x_msg_data);
6559     WHEN fnd_api.g_exc_unexpected_error THEN
6560       ROLLBACK TO rcv_create_cross_reference_sp;
6561       x_return_status  := fnd_api.g_ret_sts_unexp_error;
6562       fnd_msg_pub.count_and_get(
6563         p_encoded => fnd_api.g_false,
6564         p_count => x_msg_count,
6565         p_data => x_msg_data);
6566     WHEN OTHERS THEN
6567       ROLLBACK TO rcv_create_cross_reference_sp;
6568       x_return_status  := fnd_api.g_ret_sts_unexp_error;
6569 
6570       IF SQLCODE IS NOT NULL THEN
6571         inv_mobile_helper_functions.sql_error('INV_RCV_COMMON_APIS.create_cross_reference', l_progress, SQLCODE);
6572       END IF;
6573 
6574       fnd_msg_pub.count_and_get(
6575         p_encoded => fnd_api.g_false,
6576         p_count => x_msg_count,
6577         p_data => x_msg_data);
6578   END create_cross_reference;
6579 
6580   -- MANEESH - END CHANGES - FOR CROSS REFERENCE ITEM CREATION
6581 
6582   /*
6583    * Procedure to to get lot control of the item at
6584    * source org. Called directly in the case of Intransit
6585    * shipment transaction.
6586    * For Int Req, the overloaded method calls this procedure
6587    * Written as part of fix for Bug #2156143.
6588    */
6589   PROCEDURE get_lot_ctrl(
6590     x_return_status OUT NOCOPY    VARCHAR2
6591   , x_msg_count     OUT NOCOPY    NUMBER
6592   , x_msg_data      OUT NOCOPY    VARCHAR2
6593   , x_lot_control   OUT NOCOPY    NUMBER
6594   , p_from_org_id   IN            NUMBER
6595   , p_item_id       IN            NUMBER
6596   ) IS
6597     l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
6598   BEGIN
6599     x_return_status  := fnd_api.g_ret_sts_success;
6600 
6601     SELECT lot_control_code
6602       INTO x_lot_control
6603       FROM mtl_system_items
6604      WHERE inventory_item_id = p_item_id
6605        AND organization_id = p_from_org_id;
6606   EXCEPTION
6607     WHEN fnd_api.g_exc_error THEN
6608       x_return_status  := fnd_api.g_ret_sts_error;
6609       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
6610     WHEN fnd_api.g_exc_unexpected_error THEN
6611       x_return_status  := fnd_api.g_ret_sts_unexp_error;
6612       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
6613     WHEN OTHERS THEN
6614       x_return_status  := fnd_api.g_ret_sts_unexp_error;
6615 
6616       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
6617         fnd_msg_pub.add_exc_msg(g_pkg_name, 'GET_LOT_CTRL');
6618       END IF;
6619 
6620       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
6621   END get_lot_ctrl;
6622 
6623   /*
6624    * Procedure to to get lot control of the item at
6625    * source org. Called in the case of Internal
6626    * Requisition transaction.
6627    * First get the source org corresponding to the current line
6628    * and then call the overloaded procedure above to get the
6629    * lot control code at the source org.
6630    * Written as part of fix for Bug #2156143
6631    */
6632   PROCEDURE get_lot_ctrl(
6633     x_return_status  OUT NOCOPY    VARCHAR2
6634   , x_msg_count      OUT NOCOPY    NUMBER
6635   , x_msg_data       OUT NOCOPY    VARCHAR2
6636   , x_lot_control    OUT NOCOPY    NUMBER
6637   , p_to_org_id      IN            NUMBER
6638   , p_ship_head_id   IN            NUMBER
6639   , p_requisition_id IN            NUMBER
6640   , p_item_id        IN            NUMBER
6641   ) IS
6642     l_from_org_id NUMBER := 0;
6643     l_debug       NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
6644   BEGIN
6645     x_return_status  := fnd_api.g_ret_sts_success;
6646 
6647     /*Get the From Organization for the item using shipment header
6648       Id, Requisition #, To Organization Id and Item ID
6649     */
6650     SELECT DISTINCT rsl.from_organization_id
6651                INTO l_from_org_id
6652                FROM rcv_shipment_lines rsl
6653               WHERE item_id = p_item_id
6654                 AND to_organization_id = p_to_org_id
6655                 AND shipment_header_id = p_ship_head_id
6656                 AND EXISTS(
6657                      SELECT 1
6658                        FROM po_requisition_lines prl
6659                       WHERE prl.requisition_header_id = p_requisition_id
6660                         AND rsl.requisition_line_id = prl.requisition_line_id
6661                         AND prl.item_id = p_item_id);
6662 
6663     --Get the lot control of the item at the source org
6664     inv_rcv_common_apis.get_lot_ctrl(
6665       x_return_status              => x_return_status
6666     , x_msg_count                  => x_msg_count
6667     , x_msg_data                   => x_msg_data
6668     , x_lot_control                => x_lot_control
6669     , p_from_org_id                => l_from_org_id
6670     , p_item_id                    => p_item_id
6671     );
6672   EXCEPTION
6673     WHEN fnd_api.g_exc_error THEN
6674       x_return_status  := fnd_api.g_ret_sts_error;
6675       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
6676     WHEN fnd_api.g_exc_unexpected_error THEN
6677       x_return_status  := fnd_api.g_ret_sts_unexp_error;
6678       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
6679     WHEN OTHERS THEN
6680       x_return_status  := fnd_api.g_ret_sts_unexp_error;
6681 
6682       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
6683         fnd_msg_pub.add_exc_msg(g_pkg_name, 'GET_LOT_CTRL');
6684       END IF;
6685 
6686       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
6687   END get_lot_ctrl;
6688 
6689   PROCEDURE get_default_task(
6690     x_return_status      OUT NOCOPY    VARCHAR2
6691   , x_task_number        OUT NOCOPY    VARCHAR2
6692   , p_document_type      IN            VARCHAR2
6693   , p_po_header_id       IN            NUMBER DEFAULT NULL
6694   , p_po_line_id         IN            NUMBER DEFAULT NULL
6695   , p_oe_header_id       IN            NUMBER DEFAULT NULL
6696   , p_req_header_id      IN            NUMBER DEFAULT NULL
6697   , p_shipment_header_id IN            NUMBER DEFAULT NULL
6698   , p_item_id            IN            NUMBER DEFAULT NULL
6699   , p_item_rev           IN            VARCHAR2 DEFAULT NULL
6700   , p_project_id         IN            NUMBER DEFAULT NULL
6701   ) IS
6702     l_progress VARCHAR2(10);
6703     l_task_id  NUMBER;
6704     --l_debug number        := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
6705     l_debug    NUMBER       := 1;
6706   BEGIN
6707     IF (l_debug = 1) THEN
6708       print_debug('Entered get_document_project_task ' || p_document_type, 4);
6709     --dbms_output.put_line('Entered get_document_project_task '|| p_document_type);
6710     END IF;
6711 
6712     l_progress       := '10';
6713     x_return_status  := fnd_api.g_ret_sts_success;
6714 
6715     IF p_project_id IS NULL THEN
6716       IF (l_debug = 1) THEN
6717         print_debug('project id is null', 4);
6718       --dbms_output.put_line('project id is null');
6719       END IF;
6720 
6721       RETURN;
6722     END IF;
6723 
6724     IF p_document_type = 'PO' THEN
6725       BEGIN
6726         SELECT pod.task_id
6727           INTO l_task_id
6728           FROM po_distributions_all pod, po_lines_all pol
6729          WHERE pod.po_header_id = p_po_header_id
6730            AND pod.po_line_id = NVL(p_po_line_id, pod.po_line_id)
6731            AND pod.project_id = p_project_id
6732            AND pod.po_header_id = pol.po_header_id
6733            AND pod.po_line_id = pol.po_line_id
6734            AND(p_item_id IS NULL
6735                OR pol.item_id = p_item_id)
6736            AND(p_item_rev IS NULL
6737                OR pol.item_revision = p_item_rev);
6738       EXCEPTION
6739         WHEN OTHERS THEN
6740           IF (l_debug = 1) THEN
6741             print_debug('error ' || SQLERRM, 4);
6742           --dbms_output.put_line('error '||SQLERRM);
6743           END IF;
6744       END;
6745 
6746       IF (l_debug = 1) THEN
6747         print_debug('task= ' || TO_CHAR(l_task_id), 4);
6748       --dbms_output.put_line('task= '||to_char(l_task_id));
6749       END IF;
6750     ELSIF p_document_type = 'ASN' THEN
6751       BEGIN
6752         SELECT pod.task_id
6753           INTO l_task_id
6754           FROM po_distributions_all pod, rcv_shipment_lines rsl
6755          WHERE pod.po_header_id = rsl.po_header_id
6756            AND rsl.po_line_id = pod.po_line_id(+)
6757            AND rsl.po_line_location_id = pod.line_location_id(+)
6758            AND rsl.shipment_header_id = p_shipment_header_id
6759            AND pod.project_id = p_project_id
6760            AND(p_item_id IS NULL
6761                OR rsl.item_id = p_item_id)
6762            AND(p_item_rev IS NULL
6763                OR rsl.item_revision = p_item_rev);
6764       EXCEPTION
6765         WHEN OTHERS THEN
6766           IF (l_debug = 1) THEN
6767             print_debug('error ' || SQLERRM, 4);
6768           --dbms_output.put_line('error '||SQLERRM);
6769           END IF;
6770       END;
6771     ELSIF p_document_type = 'REQ' THEN
6772       BEGIN
6773         SELECT pod.task_id
6774           INTO l_task_id
6775           FROM po_req_distributions_all pod, po_requisition_lines_all pol
6776          WHERE pol.requisition_header_id = p_req_header_id
6777            AND pod.requisition_line_id = pol.requisition_line_id
6778            AND pod.project_id = p_project_id
6779            AND(p_item_id IS NULL
6780                OR pol.item_id = p_item_id)
6781            AND(p_item_rev IS NULL
6782                OR pol.item_revision = p_item_rev);
6783       EXCEPTION
6784         WHEN OTHERS THEN
6785           IF (l_debug = 1) THEN
6786             print_debug('error ' || SQLERRM, 4);
6787           --dbms_output.put_line('error '||SQLERRM);
6788           END IF;
6789       END;
6790     ELSIF p_document_type = 'RMA' THEN
6791       BEGIN
6792         SELECT l.task_id
6793           INTO l_task_id
6794           FROM oe_order_lines l
6795          WHERE l.line_category_code = 'RETURN'
6796            AND l.header_id = p_oe_header_id
6797            AND l.project_id = p_project_id
6798            AND(p_item_id IS NULL
6799                OR l.inventory_item_id = p_item_id)
6800            AND(p_item_rev IS NULL
6801                OR l.item_revision = p_item_rev);
6802       EXCEPTION
6803         WHEN OTHERS THEN
6804           IF (l_debug = 1) THEN
6805             print_debug('error ' || SQLERRM, 4);
6806           --dbms_output.put_line('error '||SQLERRM);
6807           END IF;
6808       END;
6809     ELSIF p_document_type = 'RECEIPT' THEN
6810       IF p_po_header_id IS NOT NULL THEN
6811         BEGIN
6812           SELECT   pod.task_id
6813               INTO l_task_id
6814               FROM po_distributions_all pod, po_lines_all pol
6815              WHERE pod.po_header_id = p_po_header_id
6816                AND pod.project_id = p_project_id
6817                AND pod.po_header_id = pol.po_header_id
6818                AND pod.po_line_id = pol.po_line_id
6819                AND(p_item_id IS NULL
6820                    OR pol.item_id = p_item_id)
6821                AND(p_item_rev IS NULL
6822                    OR pol.item_revision = p_item_rev)
6823           GROUP BY pod.project_id, pod.task_id;
6824         EXCEPTION
6825           WHEN OTHERS THEN
6826             IF (l_debug = 1) THEN
6827               print_debug('error ' || SQLERRM, 4);
6828             --dbms_output.put_line('error '||SQLERRM);
6829             END IF;
6830         END;
6831       END IF;
6832     ELSIF p_oe_header_id IS NOT NULL THEN
6833       BEGIN
6834         SELECT   task_id
6835             INTO l_task_id
6836             FROM oe_order_lines l
6837            WHERE l.line_category_code = 'RETURN'
6838              AND l.header_id = p_oe_header_id
6839              AND l.project_id = p_project_id
6840              AND(p_item_id IS NULL
6841                  OR l.inventory_item_id = p_item_id)
6842              AND(p_item_rev IS NULL
6843                  OR l.item_revision = p_item_rev)
6844         GROUP BY project_id, task_id;
6845       EXCEPTION
6846         WHEN OTHERS THEN
6847           IF (l_debug = 1) THEN
6848             print_debug('error ' || SQLERRM, 4);
6849           --dbms_output.put_line('error '||SQLERRM);
6850           END IF;
6851       END;
6852     END IF;
6853 
6854     BEGIN
6855       IF (l_task_id IS NOT NULL) THEN
6856         x_task_number  := inv_projectlocator_pub.get_task_number(l_task_id);
6857         print_debug('error ' || SQLERRM, 4);
6858       --dbms_output.put_line('task  '||x_task_number);
6859       END IF;
6860     EXCEPTION
6861       WHEN OTHERS THEN
6862         NULL;
6863     END;
6864   END;
6865 
6866   /**
6867   *   This procedure checks for the following
6868   *   1. Whether the given LPN or its child LPN has contents. If
6869   *   either the given
6870   *      LPN or its child LPNs do not have any contents then
6871   *   through error.
6872   *   2. Check If the LPN is already processed, and there is a
6873   *   RTI record exists
6874   *      for the LPN.
6875 
6876   *  @param  p_lpn_id
6877   *  @param  x_return_status
6878   *  @param  x_msg_count
6879   *  @param  x_msg_data
6880 **/
6881   PROCEDURE validate_nested_lpn(
6882     p_lpn_id        IN            NUMBER
6883   , x_lpn_flag      OUT NOCOPY    VARCHAR2
6884   , x_return_status OUT NOCOPY    VARCHAR2
6885   , x_msg_count     OUT NOCOPY    NUMBER
6886   , x_msg_data      OUT NOCOPY    VARCHAR2
6887   ) IS
6888     l_return_status  VARCHAR2(1)    := fnd_api.g_ret_sts_success;
6889     l_msg_count      NUMBER;
6890     l_msg_data       VARCHAR2(4000);
6891     l_progress       VARCHAR2(10);
6892     l_valid_lpn_flag VARCHAR(1)     := 'Y';
6893     l_debug          NUMBER         := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
6894   BEGIN
6895     x_lpn_flag  := 'N'; -- 'Y -validlpn ' 'N- Invalid lpn'
6896     l_progress  := 10;
6897 
6898     IF (l_debug = 1) THEN
6899       print_debug('lpn_id: ' || TO_CHAR(p_lpn_id), 1);
6900     END IF;
6901 
6902     -- Check at least one child LPN has contents.
6903 
6904     BEGIN
6905        -- Bug 3440456
6906        -- The following is changed because the join with WLPN is unnecessary
6907        /*SELECT 'Y'
6908          INTO x_lpn_flag
6909 	 FROM wms_license_plate_numbers lpn, wms_lpn_contents wlc
6910 	 WHERE lpn.lpn_id = wlc.parent_lpn_id
6911          AND lpn_id IN (SELECT     lpn_id
6912 	                FROM wms_license_plate_numbers wln
6913                         START WITH lpn_id = p_lpn_id
6914                         CONNECT BY parent_lpn_id = PRIOR lpn_id);*/
6915 
6916        -- Bug# 3633708: Performance Fixes
6917        -- The following query is bad because it does a full table scan on WLC
6918        /*SELECT 'Y'
6919 	 INTO x_lpn_flag
6920 	 FROM wms_lpn_contents wlc
6921 	 WHERE wlc.parent_lpn_id IN (SELECT lpn_id
6922 				     FROM wms_license_plate_numbers wln
6923 				     START WITH lpn_id = p_lpn_id
6924 				     CONNECT BY parent_lpn_id = PRIOR lpn_id);*/
6925 
6926        -- Bug# 3633708: Performance Fixes
6927        -- Use this query instead
6928        SELECT 'Y'
6929 	 INTO x_lpn_flag
6930 	 FROM wms_license_plate_numbers wln
6931 	 WHERE EXISTS (SELECT '1'
6932 		       FROM wms_lpn_contents wlc
6933 		       WHERE wlc.parent_lpn_id = wln.lpn_id)
6934 	   START WITH wln.lpn_id = p_lpn_id
6935 	   CONNECT BY wln.parent_lpn_id = PRIOR wln.lpn_id;
6936 
6937     EXCEPTION
6938        WHEN NO_DATA_FOUND THEN -- item controls are valid
6939 	  x_lpn_flag  := 'N';
6940        WHEN TOO_MANY_ROWS THEN
6941 	  x_lpn_flag  := 'Y';
6942     END;
6943 
6944     IF x_lpn_flag = 'N' THEN
6945       RETURN;
6946     END IF;
6947 
6948     -- Check None of the child LPNs are already processed and have pending RTI transactions
6949 
6950     BEGIN
6951       --
6952       -- 3440456
6953       -- Retrieval of x_lpn_flag is changed
6954       --
6955       SELECT 'Y'
6956         INTO x_lpn_flag
6957         FROM wms_license_plate_numbers lpn, wms_lpn_contents wlc, rcv_transactions_interface rti
6958        WHERE lpn.lpn_id = wlc.parent_lpn_id
6959          AND lpn.lpn_id = rti.lpn_id
6960          AND rti.transaction_status_code = 'PENDING'
6961          AND lpn.lpn_id IN(SELECT     lpn_id
6962                                  FROM wms_license_plate_numbers wln
6963                            START WITH lpn_id = p_lpn_id
6964                            CONNECT BY parent_lpn_id = PRIOR lpn_id);
6965     EXCEPTION
6966       WHEN NO_DATA_FOUND THEN
6967         x_lpn_flag  := 'N';
6968       WHEN TOO_MANY_ROWS THEN
6969         x_lpn_flag  := 'Y';
6970     END;
6971   EXCEPTION
6972     WHEN OTHERS THEN
6973       x_lpn_flag       := 'N';
6974       x_return_status  := fnd_api.g_ret_sts_unexp_error;
6975       print_debug(SQLCODE, 1);
6976 
6977       IF SQLCODE IS NOT NULL THEN
6978         l_progress  := 100;
6979         inv_mobile_helper_functions.sql_error('validate_nested_lpns', l_progress, SQLCODE);
6980       END IF;
6981   END validate_nested_lpn;
6982 
6983   /**
6984     * This procedure takes in the LPN and fetches the LPN context,
6985     * subinventory code and locator id.
6986     * If the LPN resides in receiving, it also fetches the subinventory
6987     * and locator for that LPN
6988     **/
6989     PROCEDURE get_rcv_sub_loc(
6990         x_return_status      OUT NOCOPY    VARCHAR2
6991       , x_msg_count          OUT NOCOPY    NUMBER
6992       , x_msg_data           OUT NOCOPY    VARCHAR2
6993       , x_lpn_context        OUT NOCOPY    NUMBER
6994       , x_locator_segs       OUT NOCOPY    VARCHAR2
6995       , x_location_id        OUT NOCOPY    NUMBER
6996       , x_location_code      OUT NOCOPY    VARCHAR2
6997       , x_sub_code           OUT NOCOPY    VARCHAR2
6998       , x_locator_id         OUT NOCOPY    NUMBER
6999       , p_lpn_id             IN            NUMBER
7000       , p_organization_id    IN            NUMBER) IS
7001       l_count       NUMBER;
7002       l_progress    VARCHAR2(10);
7003       l_debug       NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
7004       l_sub_code    mtl_secondary_inventories.secondary_inventory_name%TYPE;
7005       l_locator_id  NUMBER;
7006       l_location_id NUMBER;
7007       l_auto_transact_code VARCHAR2(10);
7008     BEGIN
7009       x_return_status  := fnd_api.g_ret_sts_success;
7010 
7011       IF (l_debug = 1) THEN
7012         print_debug('Enter  get_rcv_sub_loc: 10:' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
7013         print_debug('Parameters passed : 10.1: p_organization_id - ' || p_organization_id, 4);
7014         print_debug('Parameters passed : 10.2: p_lpn_id - ' || p_lpn_id, 4);
7015       END IF;
7016 
7017       --First check the lpn_context
7018       l_progress := '20';
7019       BEGIN
7020         SELECT lpn_context
7021              , subinventory_code
7022              , locator_id
7023         INTO   x_lpn_context
7024              , l_sub_code
7025              , l_locator_id
7026         FROM   wms_license_plate_numbers
7027         WHERE  lpn_id = p_lpn_id;
7028       EXCEPTION
7029       WHEN OTHERS THEN
7030         x_lpn_context := 5;
7031         x_locator_segs := NULL;
7032         x_locator_id := NULL;
7033         x_location_id := NULL;
7034         x_location_code := NULL;
7035         x_sub_code := NULL;
7036         RETURN;
7037       END;
7038 
7039       --If the LPN has a subinventory associated, get the location_code
7040       --from hr_locations using location_id in mtl_secondary_inventories for that sub
7041       IF (l_debug = 1) THEN
7042         print_debug('get_rcv_sub_loc: 20: values from WLPN - context: ' || x_lpn_context ||
7043               ', sub_code: ' || l_sub_code || ', loc_id: ' || l_locator_id, 4);
7044       END IF;
7045 
7046       IF l_sub_code IS NULL THEN
7047         BEGIN
7048           l_progress := '30';
7049           --Get the subinventory, locator_id and routing from rcv_transactions_interface
7050           --If there exists one and it is not direct then error out with the
7051           --"Invalid LPN context" error since we cannot commingle routings in the same LPN
7052           SELECT  subinventory
7053                 , locator_id
7054                 , location_id
7055                 , auto_transact_code
7056           INTO    l_sub_code
7057                 , l_locator_id
7058                 , l_location_id
7059                 , l_auto_transact_code
7060           FROM    rcv_transactions_interface
7061           WHERE   transfer_lpn_id = p_lpn_id
7062           AND     transaction_type = 'RECEIVE'
7063           AND     transaction_status_code = 'PENDING'
7064           AND     processing_status_code <> 'ERROR'
7065           AND     ROWNUM = 1;
7066 
7067 
7068           IF (NVL(l_auto_transact_code, 'RECEIVE') = 'DELIVER') THEN
7069             x_return_status  := fnd_api.g_ret_sts_error;
7070 	    IF ((inv_rcv_common_apis.g_wms_patch_level >= inv_rcv_common_apis.g_patchset_j) or
7071 		(inv_rcv_common_apis.g_inv_patch_level >= inv_rcv_common_apis.g_patchset_j)) THEN
7072 	       x_msg_data       := 'INV_CANNOT_COMMINGLE_ROUTING';
7073 	       fnd_message.set_name('INV', 'INV_CANNOT_COMMINGLE_ROUTING');
7074 	     ELSE
7075 	       x_msg_data       := 'INV_INVALID_LPN_CONTEXT';
7076 	       fnd_message.set_name('INV', 'INV_INVALID_LPN_CONTEXT');
7077 	    END IF;
7078             fnd_msg_pub.ADD;
7079             RETURN;
7080           END IF;
7081         EXCEPTION
7082           WHEN NO_DATA_FOUND THEN
7083             BEGIN
7084               l_progress := 40;
7085               SELECT  to_subinventory
7086                     , to_locator_id
7087                     , location_id
7088               INTO    l_sub_code
7089                     , l_locator_id
7090                     , l_location_id
7091               FROM   rcv_supply
7092               WHERE lpn_id = p_lpn_id
7093               AND   to_organization_id = p_organization_id
7094               AND   ROWNUM = 1;
7095             EXCEPTION
7096               WHEN OTHERS THEN
7097                 x_sub_code := NULL;
7098                 x_locator_id := NULL;
7099                 x_locator_segs := NULL;
7100                 x_location_id := NULL;
7101                 x_location_code := NULL;
7102                 RETURN;
7103             END;
7104           WHEN OTHERS THEN
7105             IF (l_debug = 1) THEN
7106               print_debug('get_sub_code: 20.5: Error occurred while fetching values from RTI', 1);
7107             END IF;
7108             RAISE FND_API.G_EXC_ERROR;
7109         END;
7110       END IF;   --END IF l_sub_code IS NULL
7111 
7112       x_locator_segs   := NULL;
7113       x_location_code  := NULL;
7114 
7115       l_progress       := '50';
7116 
7117       IF l_sub_code IS NOT NULL THEN
7118         BEGIN
7119           SELECT location_code
7120                , location_id
7121           INTO   x_location_code
7122                , x_location_id
7123           from   hr_locations hl
7124           WHERE EXISTS
7125             ( SELECT 1
7126               FROM  mtl_secondary_inventories msi
7127               WHERE organization_id = p_organization_id
7128               AND   secondary_inventory_name = l_sub_code
7129               AND   msi.location_id = hl.location_id)
7130           AND ROWNUM = 1;
7131         EXCEPTION
7132           WHEN OTHERS THEN
7133             x_location_id := NULL;
7134             x_location_code := NULL;
7135         END;
7136       ELSIF l_location_id IS NOT NULL THEN
7137         BEGIN
7138           l_progress := '60';
7139           SELECT location_code
7140                , location_id
7141           INTO   x_location_code
7142                , x_location_id
7143           FROM   hr_locations hl
7144           WHERE  location_id = l_location_id
7145           AND    ROWNUM = 1;
7146         EXCEPTION
7147           WHEN OTHERS THEN
7148             l_sub_code := NULL;
7149             l_locator_id := NULL;
7150             x_location_id := NULL;
7151             x_location_code := NULL;
7152           END;
7153       END IF;
7154 
7155       --Get the locator segments only if sub and locator_id are set
7156       IF (l_sub_code IS NOT NULL AND l_locator_id IS NOT NULL) THEN
7157         l_progress  := '70';
7158         BEGIN
7159           SELECT inv_project.get_locsegs(inventory_location_id, organization_id)
7160             INTO x_locator_segs
7161             FROM mtl_item_locations
7162            WHERE organization_id = p_organization_id
7163              AND inventory_location_id = l_locator_id;
7164         EXCEPTION
7165           WHEN OTHERS THEN
7166             x_locator_segs  := NULL;
7167         END;
7168       END IF;
7169 
7170       --Finally assign the values for output variables
7171       x_sub_code := l_sub_code;
7172       x_locator_id := l_locator_id;
7173 
7174       IF (l_debug = 1) THEN
7175         print_debug(' lpn_context ' || x_lpn_context, 4);
7176         print_debug(' sub_code  ' || x_sub_code, 4);
7177         print_debug(' location_code ' || x_location_code, 4);
7178         print_debug(' location_id   ' || x_location_id, 4);
7179         print_debug(' locator_segs  ' || x_locator_segs, 4);
7180       END IF;
7181 
7182     EXCEPTION
7183       WHEN OTHERS THEN
7184         x_return_status  := fnd_api.g_ret_sts_unexp_error;
7185         IF SQLCODE IS NOT NULL THEN
7186           inv_mobile_helper_functions.sql_error('inv_rcv_common_apis.get_sub_code', l_progress, SQLCODE);
7187         END IF;
7188         IF (l_debug = 1) THEN
7189           print_debug('Exitting get_sub_code - other exception:' || l_progress || ' ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
7190         END IF;
7191         --
7192         IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
7193           fnd_msg_pub.add_exc_msg(g_pkg_name, 'get_sub_code');
7194         END IF;
7195     END get_rcv_sub_loc;
7196 
7197     PROCEDURE validate_from_lpn(
7198       p_lpn_id            IN            NUMBER
7199     , p_req_id            IN            VARCHAR2
7200     , x_lpn_flag          OUT NOCOPY    VARCHAR2
7201     , x_count_of_lpns     OUT NOCOPY    NUMBER
7202     , x_return_status     OUT NOCOPY    VARCHAR2
7203     , x_msg_count         OUT NOCOPY    NUMBER
7204     , x_msg_data          OUT NOCOPY    VARCHAR2
7205     , p_shipment_num      IN            VARCHAR2
7206     , p_org_id            IN            NUMBER
7207     ) IS
7208       l_return_status   VARCHAR2(1)    := fnd_api.g_ret_sts_success;
7209       l_msg_count       NUMBER;
7210       l_msg_data        VARCHAR2(4000);
7211       l_progress        VARCHAR2(10);
7212       l_req_num         VARCHAR2(10);
7213       l_valid_lpn_flag  VARCHAR(1)     := 'Y';
7214       l_order_header_id NUMBER;
7215       l_debug           NUMBER         := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
7216     BEGIN
7217 
7218       -- Intialize variables
7219       x_lpn_flag  := 'Y'; -- 'Y -validlpn ' 'N- Invalid lpn'
7220       x_count_of_lpns := 1;
7221       x_return_status  := fnd_api.g_ret_sts_success;
7222       l_progress  := 10;
7223 
7224 
7225       IF (l_debug = 1) THEN
7226         print_debug('lpn_id: ' || TO_CHAR(p_lpn_id), 1);
7227         print_debug('req id : ' || p_req_id,1);
7228       END IF;
7229       -- Check if there is any existing RTI Record for the given LPN or their childs.
7230 
7231       BEGIN
7232         SELECT 'N' INTO x_lpn_flag
7233         FROM rcv_transactions_interface
7234         WHERE lpn_id IN ( SELECT lpn_id FROM wms_license_plate_numbers
7235                             START WITH lpn_id = p_lpn_id
7236                             CONNECT BY parent_lpn_id = PRIOR lpn_id)
7237         AND transaction_status_code = 'PENDING'
7238 	AND processing_status_code <> 'ERROR'
7239 	AND ROWNUM = 1  ;
7240       EXCEPTION
7241         WHEN no_data_found  THEN
7242           x_lpn_flag := 'Y';
7243         WHEN too_many_rows THEN
7244           x_lpn_flag := 'N';
7245       END;
7246 
7247       IF (l_debug = 1) THEN
7248         print_debug('x_lpn_flag after checking for RTI records: ' || x_lpn_flag, 1);
7249       END IF;
7250 
7251 
7252       -- Get Sales order header id for the given LPN
7253 
7254       l_progress := '10';
7255       IF (p_req_id IS NOT NULL) AND (x_lpn_flag = 'Y') THEN
7256          SELECT segment1
7257            INTO l_req_num
7258            FROM po_requisition_headers_all
7259            WHERE requisition_header_id = p_req_id;
7260 
7261          IF (l_debug = 1) THEN
7262            print_debug('segment1 : ' || l_req_num, 1);
7263          END IF;
7264 
7265 
7266         l_progress := '20';
7267         SELECT header_id
7268           INTO   l_order_header_id
7269           FROM   oe_order_headers_all
7270           WHERE  orig_sys_document_ref = l_req_num
7271           AND    order_source_id  = 10;
7272 
7273         IF (l_debug = 1) THEN
7274           print_debug('oe_order_header_id  : ' || l_order_header_id, 1);
7275         END IF;
7276 
7277 
7278         l_progress := '30';
7279 
7280         /* Bug 5073354 : Changed following query to access the base table wsh_delivery_details
7281         **               instead of using the view wsh_delivery_details_ob_grp_v and
7282         **               wsh_delivery_assignments instead of wsh_delivery_assignments_v.  This
7283         **               is being done to overcome performance issues reported for 10G database.
7284         */
7285 
7286         -- verify the LPN belongs choosen internal order.
7287         BEGIN
7288           SELECT 'Y' INTO x_lpn_flag
7289           FROM wsh_delivery_details wdd,
7290                wsh_delivery_assignments wda,
7291                wsh_delivery_details wdd1
7292           WHERE wdd.lpn_id IN
7293                 (SELECT lpn_id FROM  wms_license_plate_numbers
7294                  START WITH lpn_id = p_lpn_id
7295                  CONNECT BY parent_lpn_id = PRIOR  lpn_id)
7296           AND wdd.delivery_detail_id = wda.parent_delivery_detail_id
7297           AND wda.delivery_detail_id = wdd1.delivery_detail_id
7298           AND NVL(wdd.line_direction,'O') IN ('O','IO')
7299 	        AND wdd1.source_header_id = l_order_header_id
7300 	        AND ROWNUM = 1;
7301         EXCEPTION
7302           WHEN NO_DATA_FOUND THEN
7303             x_lpn_flag := 'N';
7304           WHEN TOO_MANY_ROWS THEN
7305             x_lpn_flag := 'Y';
7306         END;
7307         l_progress := 40;
7308 
7309         IF (l_debug = 1) THEN
7310           print_debug('x_lpn_flag after checking lpn belongs to this internal order : ' || x_lpn_flag, 1);
7311         END IF;
7312 
7313         /* Bug 5073354 : Changed following query to access the base table wsh_delivery_details
7314         **               instead of using the view wsh_delivery_details_ob_grp_v and
7315         **               wsh_delivery_assignments instead of wsh_delivery_assignments_v.  This
7316         **               is being done to overcome performance issues reported for 10G database.
7317         */
7318 
7319         -- Verify this LPN does not belong any other internal requision
7320         IF(x_lpn_flag = 'Y')  THEN
7321 
7322            --BUG 4237975: break up query that joins wlpn with wsh
7323            --to improve performance
7324 	   FOR l_lpn_rec IN (SELECT lpn_id FROM  wms_license_plate_numbers
7325 			     START WITH lpn_id = p_lpn_id
7326 			     CONNECT BY parent_lpn_id = PRIOR  lpn_id) LOOP
7327              BEGIN
7328 		SELECT 'N' INTO x_lpn_flag
7329 		  FROM wsh_delivery_details wdd,
7330 		       wsh_delivery_assignments wda,
7331 		       wsh_delivery_details wdd1
7332 		  WHERE wdd.lpn_id = l_lpn_rec.lpn_id
7333 		  AND wdd.delivery_detail_id = wda.parent_delivery_detail_id
7334 		  AND wda.delivery_detail_id = wdd1.delivery_detail_id
7335 		  AND NVL(wdd.line_direction,'O') IN ('O','IO')
7336 		  AND wdd1.source_header_id <> l_order_header_id
7337 		  AND ROWNUM = 1;
7338 	     EXCEPTION
7339 		WHEN TOO_MANY_ROWS THEN
7340 		   x_lpn_flag := 'N';
7341                 WHEN OTHERS THEN
7342                    NULL;
7343 	     END;
7344 
7345 	     IF (x_lpn_flag = 'N') THEN
7346 		EXIT;
7347 	     END IF;
7348 	  END LOOP;
7349 
7350 
7351           IF (l_debug = 1) THEN
7352             print_debug('x_lpn_flag after checking lpn belongs oter internal orders : ' || x_lpn_flag, 1);
7353           END IF;
7354 
7355         END IF;
7356 
7357 
7358         l_progress :=50;
7359 
7360         IF (l_debug = 1) THEN
7361           print_debug('x_lpn_flag: ' || x_lpn_flag, 4);
7362         END IF;
7363 
7364         /* Bug 5073354 : Changed following query to access the base table wsh_delivery_details
7365         **               instead of using the view wsh_delivery_details_ob_grp_v and
7366         **               wsh_delivery_assignments instead of wsh_delivery_assignments_v.  This
7367         **               is being done to overcome performance issues reported for 10G database.
7368         */
7369 
7370         -- Check are there any LPNs left for this order.
7371         BEGIN
7372           SELECT 1 INTO x_count_of_lpns
7373           FROM wsh_delivery_details wdd,
7374                wsh_delivery_assignments wda,
7375                wsh_delivery_details wdd1
7376           WHERE wdd.lpn_id NOT IN
7377                   (SELECT lpn_id FROM  wms_license_plate_numbers
7378                    START WITH lpn_id = p_lpn_id
7379                    CONNECT BY parent_lpn_id = PRIOR  lpn_id)
7380             AND wdd.delivery_detail_id = wda.parent_delivery_detail_id
7381             AND wda.delivery_detail_id = wdd1.delivery_detail_id
7382             AND NVL(wdd.line_direction,'O') IN ('O','IO')
7383             AND wdd1.source_header_id = l_order_header_id
7384             AND NOT EXISTS
7385                   (SELECT lpn_id FROM rcv_transactions_interface
7386                    WHERE lpn_id = wdd.lpn_id
7387                    AND transaction_status_code = 'PENDING'
7388                    AND processing_status_code <> 'ERROR')
7389 	    AND ROWNUM = 1;
7390 	EXCEPTION
7391           WHEN NO_DATA_FOUND THEN
7392             x_count_of_lpns := 0;
7393           WHEN TOO_MANY_ROWS THEN
7394             x_count_of_lpns := 1;
7395         END;
7396 
7397         IF (l_debug = 1) THEN
7398           print_debug('x_count_of_lpns: ' || x_count_of_lpns, 4);
7399         END IF;
7400 
7401        ELSIF (p_shipment_num IS NOT NULL) THEN
7402 	 IF (l_debug = 1) THEN
7403           print_debug('p_shipment_num = ' || p_shipment_num, 4);
7404         END IF;
7405 
7406 	BEGIN
7407 	   SELECT  1 INTO x_count_of_lpns
7408 	     FROM  wms_license_plate_numbers wlpn1, rcv_shipment_headers rsh
7409 	     WHERE rsh.shipment_num = p_shipment_num
7410 	     AND   wlpn1.source_name = rsh.shipment_num
7411 	     AND   ((wlpn1.lpn_context = 6 AND wlpn1.organization_id = rsh.organization_id) OR
7412 		    (wlpn1.lpn_context = 7 AND wlpn1.organization_id = rsh.ship_to_org_id))
7413 	     AND EXISTS (SELECT wlpn2.lpn_id
7414 			  FROM   wms_license_plate_numbers wlpn2
7415 			  START WITH wlpn2.lpn_id = wlpn1.lpn_id
7416 			  CONNECT BY PRIOR wlpn2.lpn_id = wlpn2.parent_lpn_id
7417 	                 INTERSECT
7418 	                 SELECT rsl.asn_lpn_id
7419 	                 FROM rcv_shipment_lines rsl
7420 			  WHERE rsl.shipment_header_id = rsh.shipment_header_id
7421 			  AND   NOT exists (SELECT 1
7422 					    FROM   rcv_transactions_interface rti
7423 					    WHERE  rti.lpn_id = rsl.asn_lpn_id
7424 					    AND    rti.transfer_lpn_id = rsl.asn_lpn_id
7425 					    AND    rti.to_organization_id = rsl.to_organization_id
7426 					    AND    rti.processing_status_code <> 'ERROR'
7427 					    AND    rti.transaction_status_code <> 'ERROR'
7428 					    )
7429 			  AND rsl.asn_lpn_id NOT IN (SELECT wlpn3.lpn_id
7430 						     FROM   wms_license_plate_numbers wlpn3
7431 						     START WITH wlpn3.lpn_id = p_lpn_id
7432 						     CONNECT BY PRIOR wlpn3.lpn_id = wlpn3.parent_lpn_id
7433 						     )
7434 			 );
7435 	EXCEPTION
7436 	   WHEN no_data_found THEN
7437 	      x_count_of_lpns := 0;
7438 	   WHEN too_many_rows THEN
7439 	      x_count_of_lpns := 1;
7440 	END;
7441 
7442         IF (l_debug = 1) THEN
7443           print_debug('Num of unprocessed LPNs: ' || x_count_of_lpns, 4);
7444         END IF;
7445 
7446       END IF; -- End if for p_req_header_id is not null
7447 
7448       l_progress := 60;
7449 
7450   EXCEPTION
7451     WHEN OTHERS THEN
7452       x_return_status  := fnd_api.g_ret_sts_unexp_error;
7453 
7454       IF SQLCODE IS NOT NULL THEN
7455         inv_mobile_helper_functions.sql_error('inv_rcv_common_apis.validate_from_lpn', l_progress, SQLCODE);
7456       END IF;
7457 
7458       IF (l_debug = 1) THEN
7459         print_debug('Exitting validate_from_lpn - other exception:' || l_progress || ' ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
7460       END IF;
7461 
7462       --
7463       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
7464         fnd_msg_pub.add_exc_msg(g_pkg_name, 'validate_from_lpn');
7465       END IF;
7466 
7467    END validate_from_lpn;
7468 
7469 
7470 /****************************************************
7471  *  This procedure clears the lot numbers from the
7472  *  global variable when there is an error.
7473  *  This procedure has been added for fixing the bug ( # 3156689)
7474  ****************************************************/
7475 
7476 PROCEDURE  clear_lot_rec
7477 IS
7478 BEGIN
7479    print_debug('Enter clear_lot_rec: 1  '|| to_char(sysdate, 'YYYY-MM-DD HH:DD:SS'), 1);
7480 
7481    inv_rcv_std_rcpt_apis.g_rcpt_lot_qty_rec_tb.DELETE;
7482 
7483    print_debug('clear_lot_rec: 2 End of Procedure ',1);
7484 
7485 END clear_lot_rec;
7486 
7487 --<R12 MOAC START>
7488 
7489 /* Function get_operating_unit_id returns the org_id. */
7490 
7491 FUNCTION get_operating_unit_id ( p_receipt_source_code IN VARCHAR2,
7492                                  p_po_header_id        IN NUMBER,
7493                                  p_req_line_id         IN NUMBER,
7494                                  p_oe_order_header_id  IN NUMBER
7495                                 )
7496 RETURN NUMBER IS
7497 
7498 l_operating_unit_id MO_GLOB_ORG_ACCESS_TMP.ORGANIZATION_ID%TYPE;
7499 
7500 l_progress           VARCHAR2(10);
7501 l_debug              NUMBER;
7502 
7503 BEGIN
7504 
7505   IF p_receipt_source_code = 'VENDOR' THEN
7506 
7507     l_progress       := '010';
7508 
7509     IF p_po_header_id IS NOT NULL THEN
7510 
7511       Select org_id
7512       into l_operating_unit_id
7513       from po_headers_all
7514       where po_header_id = p_po_header_id;
7515 
7516     END IF;
7517 
7518   ELSIF p_receipt_source_code = 'INTERNAL ORDER' THEN
7519 
7520     l_progress       := '020';
7521 
7522     IF p_req_line_id IS NOT NULL THEN
7523 
7524       Select org_id
7525       into l_operating_unit_id
7526       from po_requisition_lines_all
7527       where requisition_line_id = p_req_line_id;
7528 
7529     END IF;
7530 
7531   ELSIF p_receipt_source_code = 'CUSTOMER' THEN
7532 
7533     l_progress       := '030';
7534 
7535     IF p_oe_order_header_id IS NOT NULL THEN
7536 
7537       Select org_id
7538       into l_operating_unit_id
7539       from oe_order_headers_all
7540       where header_id = p_oe_order_header_id;
7541 
7542     END IF;
7543 
7544   END IF;
7545 
7546   RETURN l_operating_unit_id;
7547 
7548 EXCEPTION
7549   WHEN NO_DATA_FOUND THEN
7550 
7551     l_debug := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
7552 
7553     IF (l_debug = 1) THEN
7554        print_debug('Error getting org_id in get_operating_unit_id(): '||l_progress, 1);
7555     END IF;
7556 
7557 END get_operating_unit_id;
7558 
7559 --<R12 MOAC END>
7560 
7561 /** Start of fix for bug 5065079 (FP of bug 4651362)
7562   * Following procedure is added to count the number of open shipments for
7563   * an internal requisition.
7564   **/
7565 PROCEDURE count_req_open_shipments
7566   (p_organization_id         IN NUMBER,
7567    p_requisition_header_id   IN NUMBER,
7568    x_return_status           OUT NOCOPY    VARCHAR2 ,
7569    x_msg_count               OUT NOCOPY    NUMBER ,
7570    x_msg_data                OUT NOCOPY    VARCHAR2,
7571    x_open_shipments          OUT NOCOPY    NUMBER
7572    )IS
7573    l_debug         NUMBER        := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
7574    BEGIN
7575     x_return_status  := fnd_api.g_ret_sts_success;
7576     IF (l_debug = 1) THEN
7577       print_debug('Enter check_req_open_shipments 10  ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
7578       print_debug(' p_requisition_header_id =>'||p_requisition_header_id,1);
7579 
7580     END IF;
7581     BEGIN
7582          SELECT count(DISTINCT rsl.shipment_header_id)
7583 	 INTO x_open_shipments
7584          FROM rcv_shipment_lines rsl, po_requisition_lines prl
7585          WHERE to_organization_id = p_organization_id
7586 	 AND nvl(rsl.shipment_line_status_code, ' ') <> 'FULLY RECEIVED'
7587          AND prl.requisition_header_id = p_requisition_header_id
7588          AND  rsl.requisition_line_id = prl.requisition_line_id;
7589 
7590     EXCEPTION
7591        WHEN OTHERS THEN
7592 	  IF (l_debug = 1) THEN
7593 	     print_debug(' Unable to query shipment. SQLCODE:'||SQLCODE||' SQLERM:'||Sqlerrm,1);
7594 	  END IF;
7595 	  x_open_shipments := 0;
7596     END;
7597 
7598     IF (l_debug = 1) THEN
7599        print_debug('x_open_shipments:'||x_open_shipments,1);
7600     END IF;
7601 
7602    EXCEPTION
7603       WHEN OTHERS THEN
7604          x_return_status  := fnd_api.g_ret_sts_unexp_error;
7605 	 fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
7606 	 IF (l_debug = 1) THEN
7607               print_debug('check_req_open_shipments 30 - Unexpected error',1);
7608          END IF;
7609 END count_req_open_shipments;
7610 
7611 -- Bug 5068944 (FP of bug 4992317)
7612 -- Defaulting the UOM from Receipt Transaction for Deliver Transaction of PO
7613 PROCEDURE get_rec_uom_code(
7614                           x_return_status       OUT NOCOPY   VARCHAR2
7615 			, x_uom_code            OUT NOCOPY   VARCHAR2
7616 			, p_shipment_header_id  IN           NUMBER
7617 			, p_item_id            IN            NUMBER
7618                         , p_organization_id    IN            NUMBER
7619 			) IS
7620        l_progress   VARCHAR2(10);
7621        l_debug      NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
7622        l_uom_code   VARCHAR2(3);
7623   BEGIN
7624      x_return_status  := fnd_api.g_ret_sts_success;
7625      x_uom_code := NULL;
7626      IF (l_debug = 1) THEN
7627 	print_debug('Entering get_rec_uom_code',4);
7628 	print_debug('Parameters passed : 10.1: p_shipment_header_id - ' || p_shipment_header_id , 4);
7629 	print_debug('Parameters passed : 10.2: p_item_id - ' || p_item_id, 4);
7630 	print_debug('Parameters passed : 10.3: p_organization_id - ' || p_organization_id, 4);
7631      END IF;
7632 
7633      l_progress := '10';
7634 
7635      IF p_shipment_header_id IS NOT NULL AND p_item_id IS NOT NULL THEN
7636 	l_progress  := '20';
7637         BEGIN
7638 	   SELECT DISTINCT mum.uom_code
7639 	     INTO l_uom_code
7640 	     FROM rcv_transactions rt , rcv_shipment_lines rsl, mtl_units_of_measure mum
7641 	     WHERE rt.transaction_type = 'RECEIVE'
7642 	     AND rsl.item_id = p_item_id
7643 	     AND rt.organization_id = p_organization_id
7644 	     AND rsl.shipment_header_id = rt.shipment_header_id
7645 	     AND rt.unit_of_measure IS NOT NULL
7646 	     AND rt.shipment_header_id = p_shipment_header_id
7647 	     AND mum.unit_of_measure(+) = rt.unit_of_measure;
7648 	EXCEPTION
7649 	   WHEN OTHERS THEN
7650 	      l_uom_code := NULL;
7651 	END;
7652 
7653 	IF (l_uom_code IS NOT NULL) THEN
7654 	   x_uom_code := inv_ui_item_lovs.get_conversion_rate
7655 	                    (l_uom_code,
7656 			     p_organization_id,
7657 			     p_item_id);
7658 	 ELSE
7659 	   x_uom_code := NULL;
7660 	END IF;--END IF (l_uom_code IS NOT NULL) THEN
7661      END IF;--IF p_shipment_header_id IS NOT NULL AND p_item_id IS NOT NULL THEN
7662 
7663      IF (l_debug = 1) THEN
7664 	print_debug('l_uom_code:'||l_uom_code,4);
7665 	print_debug('x_uom_code:'||x_uom_code,4);
7666      END IF;
7667 
7668   EXCEPTION
7669     WHEN OTHERS THEN
7670 
7671       x_return_status  := fnd_api.g_ret_sts_unexp_error;
7672 
7673       IF SQLCODE IS NOT NULL THEN
7674         inv_mobile_helper_functions.sql_error('inv_rcv_common_apis.get_rec_uom_code', l_progress, SQLCODE);
7675       END IF;
7676 
7677       IF (l_debug = 1) THEN
7678         print_debug('Exiting get_rec_uom_code - other exception:' || l_progress ||' ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
7679       END IF;
7680 
7681       --
7682       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
7683         fnd_msg_pub.add_exc_msg(g_pkg_name, 'get_rec_uom_code');
7684       END IF;
7685 
7686 END get_rec_uom_code; -- Bug 5068944 Ends
7687 
7688 END inv_rcv_common_apis;