DBA Data[Home] [Help]

PACKAGE BODY: APPS.WMS_PUTAWAY_UTILS

Source


1 PACKAGE BODY WMS_PUTAWAY_UTILS AS
2 /* $Header: WMSPUTLB.pls 120.71.12020000.15 2013/01/30 06:52:14 azjain ship $*/
3 
4 
5 --WMS_PUTAWAY_UTILS  Package
6 -- File        : WMSPUTLB.pls
7 -- Content     :
8 -- Description :
9 /**
10   *   This is a new package created in 11510 (FPJ)
11   *   This Contains Procedures/Functions for putaway utilites.
12 **/
13 -- Notes       : This package will contain the wrappers (which will be called from the java files)
14 --               for putaway.
15 --               This will also contain the grouping logic stuff which will populate the global
16 --               temp table WMS_PUTAWAY_GROUP_TASKS_GTEMP
17 -- Modified    : Mon Jul 28 18:08:27 GMT+05:30 2003
18 
19 
20 -- Global Variables definition section
21 
22       --Commonly used variables
23       g_version_printed BOOLEAN        := FALSE;
24       g_pkg_name        VARCHAR2(30)   := 'WMS_PUTAWAY_UTILS';
25       --l_debug           NUMBER         := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
26 
27       G_DT_CONSOLIDATED_DROP  CONSTANT VARCHAR2(2) := 'CD';
28       G_DT_ITEM_DROP          CONSTANT VARCHAR2(2) := 'ID';
29       G_DT_USER_DROP          CONSTANT VARCHAR2(2) := 'UD';
30       G_DT_MANUAL_DROP        CONSTANT VARCHAR2(2) := 'MD';
31       G_DT_DROP_ALL           CONSTANT VARCHAR2(2) := 'DA';
32 
33       G_OK_TO_PROCESS         CONSTANT VARCHAR2(10) := 'Y';
34 
35       G_ROW_TP_ALL_TASK       CONSTANT VARCHAR2(15) := 'All Task';
36       G_ROW_TP_GROUP_TASK     CONSTANT VARCHAR2(15) := 'Group Task';
37       G_ROW_TP_LPN_TASK       CONSTANT VARCHAR2(15) := 'LPN Task';
38 
39       G_LPN_CONTEXT_INV       CONSTANT NUMBER := 1;
40       G_LPN_CONTEXT_WIP       CONSTANT NUMBER := 2;
41       G_LPN_CONTEXT_RCV       CONSTANT NUMBER := 3;
42 
43       -- ATF Related constants
44       G_ATF_ACTIVATE_PLAN    CONSTANT NUMBER := 1;
45       G_ATF_ABORT_PLAN       CONSTANT NUMBER := 2;
46       G_ATF_CANCEL_PLAN      CONSTANT NUMBER := 3;
47 
48       G_OP_TYPE_LOAD          CONSTANT NUMBER := wms_globals.g_op_type_load;
49       G_OP_TYPE_DROP          CONSTANT NUMBER := wms_globals.G_OP_TYPE_DROP;
50       G_OP_TYPE_INSPECT       CONSTANT NUMBER := wms_globals.G_OP_TYPE_INSPECT;
51       G_OP_TYPE_PACK          CONSTANT NUMBER := wms_globals.G_OP_TYPE_PACK;
52       G_OP_TYPE_CROSSDOCK     CONSTANT NUMBER := wms_globals.G_OP_TYPE_CROSSDOCK;
53 
54       G_OP_ACTIVITY_INBOUND     CONSTANT NUMBER := wms_globals.G_OP_ACTIVITY_INBOUND;
55       G_TASK_STATUS_DISPATCHED  CONSTANT NUMBER := 3;
56       G_TASK_STATUS_LOADED      CONSTANT NUMBER := 4;
57 
58       /*
59       G_OP_INS_STAT_PENDING     CONSTANT NUMBER := wms_globals.G_OP_INS_STAT_PENDING;
60       G_OP_INS_STAT_ACTIVE      CONSTANT NUMBER := wms_globals.G_OP_INS_STAT_ACTIVE;
61       G_OP_INS_STAT_COMPLETED   CONSTANT NUMBER := wms_globals.G_OP_INS_STAT_COMPLETED;
62       G_OP_INS_STAT_ABORTED     CONSTANT NUMBER := WMS_GLOBALS.G_OP_INS_STAT_ABORTED;
63       G_OP_INS_STAT_CANCELLED   CONSTANT NUMBER := WMS_GLOBALS.G_OP_INS_STAT_CANCELLED;
64       */
65 
66       --Constant to hold values for call type param
67       G_CT_NORMAL_LOAD          CONSTANT NUMBER := 1;
68       G_CT_NORMAL_DROP          CONSTANT NUMBER := 2;
69       G_CT_SINGLE_STEP_DROP     CONSTANT NUMBER := 3;
70       G_CT_INSPECT_TM_FAILED    CONSTANT NUMBER := 4;
71       G_CT_INSPECT_B4_TM        CONSTANT NUMBER := 5;
72 
73       -- Bulk fetch limit.
74       -- This limit will be used wherever bulk collect is used in this package
75       -- It is hardcoded to 2000, but later it can be changed to suit the requirements
76       l_limit          NUMBER  := 2000;
77 
78 
79       -- Define collections for commonly used datatypes. These collections will be
80       -- used for bulk fetch.
81 
82       TYPE lpn_name_tab IS TABLE OF WMS_LICENSE_PLATE_NUMBERS.LICENSE_PLATE_NUMBER%TYPE
83         INDEX BY BINARY_INTEGER;
84       TYPE sub_name_tab IS TABLE OF MTL_SECONDARY_INVENTORIES.SECONDARY_INVENTORY_NAME%TYPE
85         INDEX BY BINARY_INTEGER;
86       TYPE row_type_tab IS TABLE OF WMS_PUTAWAY_GROUP_TASKS_GTMP.ROW_TYPE%TYPE
87         INDEX BY BINARY_INTEGER;
88       TYPE item_tab     IS TABLE OF MTL_SYSTEM_ITEMS_KFV.CONCATENATED_SEGMENTS%TYPE
89         INDEX BY BINARY_INTEGER;
90 
91 
92       TYPE num_tab      IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
93       TYPE date_tab     IS TABLE OF DATE   INDEX BY BINARY_INTEGER;
94       TYPE rowid_tab    IS TABLE OF urowid INDEX BY BINARY_INTEGER;
95       TYPE varchar240_tab IS TABLE OF VARCHAR2(240) INDEX BY BINARY_INTEGER;
96 
97       TYPE uom_tab      IS TABLE OF MTL_UNITS_OF_MEASURE_TL.UOM_CODE%TYPE INDEX BY BINARY_INTEGER;
98       TYPE lot_tab      IS TABLE OF MTL_LOT_NUMBERS.LOT_NUMBER%TYPE       INDEX BY BINARY_INTEGER;
99       TYPE rev_tab      IS TABLE OF MTL_ITEM_REVISIONS.REVISION%TYPE      INDEX BY BINARY_INTEGER;
100 
101       TYPE entity_tab   IS TABLE OF WIP_ENTITIES.WIP_ENTITY_NAME%TYPE     INDEX BY BINARY_INTEGER;
102       TYPE line_tab     IS TABLE OF WIP_LINES.LINE_CODE%TYPE              INDEX BY BINARY_INTEGER;
103       TYPE dept_tab     IS TABLE OF BOM_DEPARTMENTS.DEPARTMENT_CODE%TYPE  INDEX BY BINARY_INTEGER;
104       TYPE schedule_tab IS TABLE OF WIP_ENTITIES.WIP_ENTITY_NAME%TYPE     INDEX BY BINARY_INTEGER;
105 
106       TYPE ser_num_tab  IS TABLE OF mtl_serial_numbers.serial_number%TYPE
107            INDEX BY BINARY_INTEGER;
108       TYPE ser_mark_tab  IS TABLE OF VARCHAR2(1) INDEX BY BINARY_INTEGER;
109 
110       TYPE mol_in_rec IS RECORD(
111           line_id NUMBER
112         , prim_qty NUMBER
113         , sec_qty  NUMBER -- Fix for Bug#9037915
114       );
115       TYPE split_mo_tb_tp IS TABLE OF mol_in_rec INDEX BY BINARY_INTEGER;
116 
117       -- Task Record
118       SUBTYPE task_rec IS wms_dispatched_tasks%ROWTYPE;
119 
120       --Added for bug 5286880
121       g_line_rows WSH_UTIL_CORE.id_tab_type;
122       g_grouping_rows WSH_UTIL_CORE.id_tab_type;
123 
124 
125 /**
126   *   This procedure will call the inv_log_util package to log the messages
127   *   This will prepend package name and the module (which is passed) to the log message
128   *   This will also print the version of the file during the first execution only.
129 
130   *  @param  p_message   The message which is to be logged
131   *  @param  p_module    Procedure/Function from which this is called
132   *  @param  p_level     Log level, if it is not passed a default value of 9 will be assumed.
133 **/
134    PROCEDURE DEBUG(p_message  IN  VARCHAR2,
135                    p_module   IN  VARCHAR2   := ' ',
136                    p_level    IN  NUMBER     := 9 ) IS
137    BEGIN
138 
139      IF NOT g_version_printed THEN
140          inv_log_util.trace('$Header: WMSPUTLB.pls 120.71.12020000.15 2013/01/30 06:52:14 azjain ship $',g_pkg_name, 9);
141          g_version_printed := TRUE;
142      END IF;
143 
144      inv_log_util.trace(p_message, g_pkg_name || '.' || p_module,p_level);
145      --dbms_output.put_line(substr(g_pkg_name || p_module || p_message,1,255) );
146    END DEBUG; -- Procedure debug
147 
148 
149 
150 /**
151   *   This function will return the subinventory_type for the
152   *   subinventory and org combination passed as input. It will
153   *   return the following values
154   *   1 -- Storage Subinventory.
155   *   2 -- Receiving Subinventory.
156 
157   *  @param  p_organization_id   Organization ID
158   *  @param  p_subinventory_code   Subinventory Code
159   *  @ RETURN  number
160 
161 
162 **/
163    FUNCTION Get_Subinventory_Type(
164                   p_organization_id   IN NUMBER,
165                   p_subinventory_code IN VARCHAR2)
166      RETURN NUMBER
167      IS
168         l_debug     NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
169         l_progress  VARCHAR2(10) := '10';
170         l_proc_name VARCHAR2(30) := 'Get_Subinventory_Type:';
171         l_sub_type  NUMBER       := 1;
172    BEGIN
173 
174       IF (l_debug = 1) THEN
175         DEBUG(' Function Entered at ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
176         DEBUG(' p_organization_id   => '||p_organization_id,l_proc_name , 4);
177         DEBUG(' p_subinventory_code => '||p_subinventory_code,l_proc_name, 4);
178       END IF;
179 
180       l_progress := '100';
181       BEGIN
182 
183         l_progress := '110';
184         -- Get teh subinventory type for the passed sub_code
185 
186         SELECT nvl(subinventory_type,1)
187           INTO l_sub_type
188         FROM mtl_secondary_inventories
189           WHERE organization_id = p_organization_id
190                 AND secondary_inventory_name = p_subinventory_code;
191 
192         l_progress := '120';
193 
194       EXCEPTION
195         WHEN OTHERS THEN
196           DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
197           DEBUG(SQLERRM,l_proc_name,1);
198           l_sub_type := -1;
199       END;
200 
201       l_progress := '150';
202       IF (l_debug = 1) THEN
203         DEBUG(' l_sub_type => '|| l_sub_type,l_proc_name,4);
204         DEBUG(' Function Exited at ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
205       END IF;
206       l_progress := '160';
207 
208       -- Return the subinventory type for the i/p parameters passed
209       RETURN l_sub_type;
210 
211    EXCEPTION
212      WHEN OTHERS THEN
213       DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
214       DEBUG(SQLERRM,l_proc_name,1);
215       RETURN -1;
216    END get_subinventory_type; --Function get_subinventory_type
217 
218 
219 
220 
221 /**
222   *   This function will get the count of number of rows in the
223   *   global temporary table WMS_PUTAWAY_GROUP_TASKS_GTMP
224   *   The rows counted will depend on the mode for which it is called
225   *   Mode    Meaning
226   *   ----------------------------------------------------------------
227   *   1       Get the count of all the rows in the temp table
228   *   2       Get the count of only 'All Task' rows in the temp table
229   *   3       Get the count of only 'Group Task' rows in the temp table
230   *   4       Get the count of only 'LPN Task' rows in the temp table
231 
232   *  @param  p_mode   Mode of count
233   *  @ RETURN  NUMBER
234 
235 
236 **/
237    FUNCTION Get_Row_Count (p_mode IN NUMBER)
238    RETURN NUMBER
239    IS
240      l_debug     NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
241      l_progress  VARCHAR2(10) := '10';
242      l_proc_name VARCHAR2(30) := 'Get_Row_Count:';
243      l_rec_count NUMBER       := 0;
244    BEGIN
245 
246      IF (l_debug = 1) THEN
247       DEBUG(' Function Entered at ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
248       DEBUG(' p_mode ==> ' || p_mode,l_proc_name,4);
249      END IF;
250 
251      l_progress := '90';
252 
253      IF (p_mode = 1) THEN --check for p_mode
254 
255       -- Get the count of all rows in the temp table
256       l_progress := '100';
257       SELECT count(ROWID)
258       INTO   l_rec_count
259       FROM   WMS_PUTAWAY_GROUP_TASKS_GTMP;
260       l_progress := '110';
261 
262      ELSIF (p_mode = 2) THEN
263 
264        -- Get the count of all 'All Task' Rows in the temp table
265        l_progress := '200';
266        SELECT count(ROWID)
267        INTO   l_rec_count
268        FROM   WMS_PUTAWAY_GROUP_TASKS_GTMP
269        WHERE  row_type = G_ROW_TP_ALL_TASK;
270        l_progress := '210';
271 
272      ELSIF (p_mode = 3) THEN
273 
274        -- Get the count of all 'Group Task' Rows in the temp table
275        l_progress := '300';
276        SELECT count(ROWID)
277        INTO   l_rec_count
278        FROM   WMS_PUTAWAY_GROUP_TASKS_GTMP
279        WHERE  row_type = G_ROW_TP_GROUP_TASK;
280        l_progress := '310';
281 
282      ELSIF (p_mode = 4) THEN
283 
284        -- Get the count of all 'LPN Task' Rows in the temp table
285        l_progress := '400';
286        SELECT count(ROWID)
287        INTO   l_rec_count
288        FROM   WMS_PUTAWAY_GROUP_TASKS_GTMP
289        WHERE  row_type = G_ROW_TP_LPN_TASK;
290        l_progress := '410';
291 
292      ELSE
293         -- Invalid input passed for p_mode and hence error out
294         l_rec_count := -1;
295      END IF; --check for p_mode
296 
297 
298     --This function can be later modified to return the follwing information
299     -- Item drop count
300     -- Consolidated LPN drop count
301     -- Group task records alone
302     -- All task records
303 
304     l_progress := '900';
305     IF (l_debug = 1) THEN
306       DEBUG(' l_rec_count => '|| l_rec_count,l_proc_name,4);
307       DEBUG(' Function Exited at ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
308     END IF;
309     l_progress := '910';
310 
311     -- Retrun the number of rows in the temp table
312     RETURN l_rec_count;
313 
314     EXCEPTION
315      WHEN OTHERS THEN
316       DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
317       DEBUG(SQLERRM,l_proc_name);
318       RETURN -1;
319      END get_row_count; --Function get_row_count
320 
321 
322 
323  /* Bug#6920388
324 This procedure will unpack the inner LPN of a given LPN if the inner LPN
325 has a picking task loaded from it. Thi will be used in manual load and drop.
326 */
327  PROCEDURE Detach_Loaded_Lpns ( p_lpn_id NUMBER,
328                                 p_org_id NUMBER)
329  IS
330  CURSOR loaded_lpn_cur  IS
331   SELECT wlpn.lpn_id,NVL(wlpn.parent_lpn_id,0),NVL(mmtt.lpn_id,0),NVL(mmtt.content_lpn_id,0),mmtt.transaction_temp_id
332     FROM mtl_material_transactions_temp mmtt ,
333           wms_license_plate_numbers wlpn
334      WHERE wlpn.organization_id = p_org_id
335      AND wlpn.outermost_lpn_id = p_lpn_id
336      AND wlpn.outermost_lpn_id <> wlpn.lpn_id
337      AND mmtt.organization_id = p_org_id
338      AND mmtt.wms_task_type <> WMS_GLOBALS.g_wms_task_type_putaway
339      AND ( mmtt.content_lpn_id= wlpn.lpn_id OR mmtt.lpn_id = wlpn.lpn_id );
340 
341  l_par_lpn_id         NUMBER;
342  l_lpn_id	      NUMBER;
343  l_mmtt_lpn_id        NUMBER;
344  l_mmtt_cont_lpn_id  NUMBER;
345  l_temp_id            NUMBER;
346  l_msg_count          NUMBER;
347  l_msg_data           VARCHAR2(2000);
348  l_return_status      VARCHAR2(3);
349  l_debug           NUMBER         := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
350  BEGIN
351       IF (l_debug = 1) THEN
352        debug('Checking if any of the inner LPNs are loaded for picking tasks');
353       END IF;
354      OPEN loaded_lpn_cur ;
355      LOOP
356         FETCH loaded_lpn_cur into l_lpn_id,l_par_lpn_id,l_mmtt_lpn_id, l_mmtt_cont_lpn_id, l_temp_id ;
357         EXIT WHEN loaded_lpn_cur%NOTFOUND;
358 
359         IF (l_debug = 1) THEN
360           debug('Updating MMTT : temp_id '||l_temp_id);
361         END IF;
362 
363         IF ( l_mmtt_lpn_id > 0 AND l_mmtt_cont_lpn_id > 0 AND l_mmtt_lpn_id <> l_mmtt_cont_lpn_id ) THEN
364          UPDATE mtl_material_transactions_temp mmtt
365           SET  mmtt.lpn_id=null
366          WHERE mmtt.transaction_temp_id = l_temp_id
367          AND   mmtt.lpn_id = l_par_lpn_id
368          AND   mmtt.content_lpn_id = l_lpn_id;
369         END IF;
370 
371         IF (l_par_lpn_id > 0 ) THEN
372         	IF (l_debug = 1) THEN
373         	  debug('Unpacking LPN_id '||l_lpn_id || ' from '||l_par_lpn_id);
374 	        END IF;
375         	wms_container_pvt.packunpack_container
376                   (p_api_version      =>   1.0
377                   ,p_content_lpn_id   =>   l_lpn_id      -- child LPN ID
378                   ,p_lpn_id           =>   l_par_lpn_id  -- Parent LPN ID
379                   ,p_operation        =>   2 -- Unpack
380                   ,p_organization_id  =>   p_org_id
381                   ,p_commit           =>   fnd_api.g_false
382                   ,x_return_status    =>   l_return_status
383                   ,x_msg_count        =>   l_msg_count
384                   ,x_msg_data         =>   l_msg_data);
385 
386 	        IF (l_return_status <> fnd_api.g_ret_sts_success) THEN
387         	   IF (l_debug = 1) THEN
388 	             debug('ERROR!!! while Unpacking.Msg:'||l_msg_data);
389         	   END IF;
390 		   RAISE fnd_api.g_exc_error;
391 		END IF;
392        END IF;
393      END LOOP;
394      CLOSE loaded_lpn_cur;
395       IF (l_debug = 1) THEN
396        debug('Done with  loaded inner LPNs .');
397      END IF;
398 END;
399 
400 
401 /**
402   *   This function will remove all the records in the temporary
403   *   table WMS_PUTAWAY_GROUP_TASKS_GTMP
404   *   The rows that has to be deleted depends on the i/p parameter
405   *   p_del_type
406   *
407   *   p_del_type   Action
408   *   ------------------------------------------------------------
409   *   1        --  Delete tasks belonging to the group which is passed as input.
410   *   2        --  Delete dummy rows which has been inserted for LPNs without contents
411   *   3        --  Deletes all the records in the temp table.
412   *	4	 --  Delete processed tasks belonging to the group which is passed as input.
413   *   This Funciton will delete both the group_tasks and all_task
414   *   rows.
415   *   It will return the number of rows deleted
416   *   -1 in case of failure.
417 
418   *  @param  p_group_id   Group ID for which the records has to be deleted
419   *  @ RETURN  NUMBER
420 
421 
422 **/
423    FUNCTION Remove_Tasks_In_Group(
424                  p_del_type  IN NUMBER
425                 ,p_group_id  IN NUMBER )
426      RETURN NUMBER
427      IS
428      l_debug     NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
429      l_progress  VARCHAR2(10) := '10';
430      l_proc_name VARCHAR2(30) := 'Remove_Tasks_In_Group:';
431      l_rec_count NUMBER       := 0;
432 
433      l_rowid_tab      rowid_tab;
434 
435      -- This cursor will select the dummy rows in the temp table which can be deleted
436      CURSOR c_dummy_task IS
437        SELECT ROWID
438        FROM   WMS_PUTAWAY_GROUP_TASKS_GTMP
439        WHERE  row_type = G_ROW_TP_LPN_TASK;
440 
441 
442    BEGIN
443 
444      IF (l_debug = 1) THEN
445       DEBUG(' Function Entered at ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
446      END IF;
447 
448      IF (l_debug = 1) THEN
449       DEBUG(' p_del_type ==> '||p_del_type,l_proc_name,4);
450       DEBUG(' p_group_id ==> '||p_group_id,l_proc_name,4);
451      END IF;
452 
453       l_progress := '100';
454 
455       IF p_del_type = 1 THEN
456         -- Delete the tasks for the group_id passed
457           l_progress := '105';
458           IF (l_debug = 1) THEN
459            DEBUG(' Deleting tasks for the group id passed',l_proc_name,9);
460           END IF;
461 
462           IF p_group_id = -1 THEN
463             IF (l_debug = 1) THEN
464              DEBUG(' Group ID is -1 and returning -1 ',l_proc_name,9);
465             END IF;
466             RETURN -1;
467           END IF;
468 
469           l_progress := '110';
470           DELETE FROM WMS_PUTAWAY_GROUP_TASKS_GTMP
471             WHERE group_id = p_group_id;
472           l_progress := '120';
473 
474           -- Get the number of rows processed by the above SQL
475           l_progress := '130';
476           l_rec_count := SQL%rowcount;
477           l_progress := '140';
478 
479       ELSIF p_del_type = 2 THEN
480         -- Delete the dummy records which has been inserted for LPNs without contents
481 
482           l_progress := '205';
483           IF (l_debug = 1) THEN
484            DEBUG(' Deleting tasks for the group id passed',l_proc_name,9);
485           END IF;
486 
487           l_progress := '210';
488           OPEN c_dummy_task;
489           l_progress := '220';
490 
491           LOOP --dummy_task_cursor
492 
493             EXIT WHEN c_dummy_task%NOTFOUND;
494             l_progress := '230';
495 
496             -- Fetch the records which needs to be deleted
497             l_progress := '240';
498             FETCH   c_dummy_task
499               BULK COLLECT
500               INTO  l_rowid_tab
501               LIMIT l_limit;
502             l_progress := '250';
503 
504 
505             -- Bulk delete the dummy records
506             FORALL i IN 1 .. l_rowid_tab.COUNT
507               DELETE FROM WMS_PUTAWAY_GROUP_TASKS_GTMP
508               WHERE ROWID = l_rowid_tab(i);
509 
510             l_progress := '260';
511           END LOOP; --dummy_task_cursor
512 
513           l_progress := '270';
514           CLOSE c_dummy_task;
515           l_progress := '280';
516 
517 
518       ELSIF p_del_type = 3 THEN
519               --Delete all records from WMS_PUTAWAY_GROUP_TASKS_GTMP
520 
521               l_progress := '305';
522               IF (l_debug = 1) THEN
523                DEBUG(' Deleting all the rows from the global temp table',l_proc_name,9);
524               END IF;
525 
526               l_progress := '310';
527               DELETE FROM WMS_PUTAWAY_GROUP_TASKS_GTMP;
528               l_progress := '320';
529 
530               -- Get the number of rows processed by the above SQL
531               l_progress := '330';
532               l_rec_count := SQL%rowcount;
533               l_progress := '340';
534 
535       ELSIF p_del_type = 4 THEN  -- Added for Bug 8827145
536            -- Delete the tasks for the group_id passed
537              l_progress := '405';
538              IF (l_debug = 1) THEN
539               DEBUG(' Deleting processed tasks for the group id passed',l_proc_name,9);
540              END IF;
541 
542              IF p_group_id = -1 THEN
543                IF (l_debug = 1) THEN
544                 DEBUG(' Group ID is -1 and returning -1 ',l_proc_name,9);
545                END IF;
546                RETURN -1;
547              END IF;
548 
549              l_progress := '410';
550              DELETE FROM WMS_PUTAWAY_GROUP_TASKS_GTMP
551                WHERE group_id = p_group_id
552                  AND (PROCESS_FLAG = 'Y' OR ROW_TYPE = G_ROW_TP_GROUP_TASK);
553              l_progress := '420';
554 
555              -- Get the number of rows processed by the above SQL
556              l_progress := '430';
557              l_rec_count := SQL%rowcount;
558              l_progress := '440';
559 
560       ELSE
561           l_progress := '800';
562           IF (l_debug = 1) THEN
563            DEBUG(' Invalid value for p_',l_proc_name,9);
564           END IF;
565 
566           l_rec_count := -1;
567       END IF; -- p_del_type check
568 
569       l_progress := '900';
570       IF (l_debug = 1) THEN
571         DEBUG(' l_rec_count => '|| l_rec_count,l_proc_name,4);
572         DEBUG(' Function Exited at ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
573       END IF;
574 
575       -- return the number of rows deleted
576       RETURN l_rec_count;
577 
578    EXCEPTION
579      WHEN OTHERS THEN
580        DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
581        DEBUG(SQLERRM,l_proc_name);
582 
583        -- Close the cursors if they are open
584        IF c_dummy_task%isopen THEN
585          CLOSE c_dummy_task;
586        END IF;
587 
588        RETURN -1;
589    END Remove_Tasks_In_Group;
590 
591 
592    /**
593      *   This function will remove all the processed records in the temporary
594      *   table WMS_PUTAWAY_GROUP_TASKS_GTMP and insert the newly created tasks
595      *   during a partial putaway
596      *  @param  p_group_id   Group ID for which the records has to be deleted
597 
598 
599    **/
600       -- Added for Bug 8827145
601       PROCEDURE Update_Tasks_In_Group(
602          x_return_status           OUT  NOCOPY VARCHAR2
603        , x_msg_count               OUT  NOCOPY NUMBER
604        , x_msg_data                OUT  NOCOPY VARCHAR2
605        , x_rec_count               OUT  NOCOPY NUMBER
606        , p_organization_id         IN   NUMBER
607        , p_drop_type               IN   VARCHAR2
608                    , p_lpn_id                  IN   NUMBER
609        , p_group_id                IN   NUMBER
610        , p_emp_id                  IN   NUMBER)
611         IS
612         l_debug     NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
613         l_progress  VARCHAR2(10) := '10';
614         l_proc_name VARCHAR2(30) := 'Update_Tasks_In_Group:';
615         l_rec_count NUMBER       := 0;
616         l_task_rec        task_rec   := NULL;
617        l_txn_header_id    NUMBER;
618        l_txn_temp_id_tab  num_tab;
619        l_error_code            NUMBER;
620        l_drop_lpn_option  NUMBER;
621        l_consolidation_method_id NUMBER;
622 
623       BEGIN
624 
625        IF (l_debug = 1) THEN
626          DEBUG(' Function Entered at ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
627        END IF;
628 
629        x_return_status  := fnd_api.g_ret_sts_success;
630 
631        SAVEPOINT update_gtmp_sp;
632 
633        l_task_rec.person_id       := p_emp_id;
634        l_task_rec.organization_id := p_organization_id;
635        l_task_rec.user_task_type  := -1;
636 
637           -- To insert the newly split MMTT and MTRL records to GTMP table....
638 
639        FOR gtmp_rec IN (SELECT DISTINCT
640                        mmtt.organization_id,
641                        mmtt.transaction_temp_id,
642                        mmtt.transaction_header_id,
643                        mtrl.lpn_id,
644                        mmtt.cartonization_id,  --INTO LPN ID
645                        mmtt.inventory_item_id,
646                        msik.concatenated_segments item, --Item
647                        mtrl.lot_number,
648                        mmtt.revision,
649                        mmtt.transaction_quantity,
650                        mmtt.transaction_uom,
651                        inv_project.get_locsegs (mmtt.locator_id, mmtt.organization_id ) LOCATOR,
652                        msik.primary_uom_code,
653                        NVL(mmtt.transfer_subinventory,mmtt.subinventory_code) dest_subinventory,
654                        NVL(mmtt.transfer_to_location, mmtt.locator_id)        dest_locator,
655                        NVL (msik.revision_qty_control_code, 1) revision_qty_control_code,
656                        NVL (msik.lot_control_code, 1) lot_control_code,
657                        NVL (msik.serial_number_control_code, 1) serial_number_control_code,
658                        NVL (msik.restrict_subinventories_code, 2) restrict_subinventories_code,
659                        NVL (msik.restrict_locators_code, 2) restrict_locators_code,
660                        NVL (msik.location_control_code, 1) location_control_code,
661                        NVL (msik.allowed_units_lookup_code, 2) allowed_units_lookup_code,
662                        NVL (mtrl.backorder_delivery_detail_id, 0) backorder_delivery_detail_id,
663                        NVL (mtrl.crossdock_type, 0) crossdock_type,
664                        NVL (mmtt.wip_supply_type, 0) wip_supply_type,
665                        mmtt.subinventory_code from_subinventory,
666                        mmtt.locator_id from_locator,
667                        mmtt.transfer_subinventory,
668                        mmtt.transfer_to_location,
669                        mmtt.transfer_organization,
670                        mmtt.transaction_action_id,
671                        mtrl.REFERENCE,
672                        mtrl.line_id,
673                        mtrl.project_id,
674                        mtrl.task_id,
675                        mtrl.txn_source_id,
676                        mmtt.primary_quantity,
677                        mmtt.secondary_transaction_quantity, -- 9037915
678                        mmtt.secondary_uom_code              -- 9037915
679                    FROM mtl_material_transactions_temp mmtt
680                        ,mtl_txn_request_lines mtrl
681                        ,wms_dispatched_tasks wdt
682                        --,mtl_item_locations milk
683                        ,mtl_system_items_kfv msik
684                        ,mtl_txn_request_headers mtrh
685                  WHERE wdt.organization_id = p_organization_id
686                        AND wdt.person_id = Decode(p_drop_type, g_dt_drop_all,p_emp_id,wdt.person_id)
687                        AND wdt.status = 4
688                        AND wdt.task_type   = 2
689                        AND wdt.transaction_temp_id = mmtt.transaction_temp_id
690                        AND mmtt.move_order_line_id = mtrl.line_id
691                        AND NVL (mmtt.wms_task_type, 0) <> -1
692                        AND mtrl.lpn_id IN (SELECT     lpn_id
693                                              FROM wms_license_plate_numbers
694                                              WHERE organization_id = p_organization_id
695                                        START WITH lpn_id = p_lpn_id
696                                        CONNECT BY parent_lpn_id = PRIOR lpn_id)
697                        AND mtrl.header_id = mtrh.header_id
698                        AND mtrh.move_order_type = 6
699                        AND mmtt.organization_id = msik.organization_id
700                        AND mmtt.inventory_item_id = msik.inventory_item_id
701                        AND mmtt.transaction_temp_id NOT IN (SELECT TRANSACTION_TEMP_ID FROM WMS_PUTAWAY_GROUP_TASKS_GTMP)) LOOP
702 
703              IF (l_debug = 1) THEN
704                DEBUG('Calling activate_operation_instance with ...',l_proc_name,9);
705                DEBUG('p_source_task_id    => ' || gtmp_rec.transaction_temp_id,l_proc_name,9);
706                DEBUG('p_activity_type_id  => ' || G_OP_ACTIVITY_INBOUND,l_proc_name,9);
707                DEBUG('p_operation_type_id => ' || G_OP_TYPE_DROP,l_proc_name,9);
708              END IF;
709 
710              l_progress := '250';
711 
712 
713              wms_atf_runtime_pub_apis.activate_operation_instance
714                (  p_source_task_id    => gtmp_rec.transaction_temp_id
715                ,p_activity_id       => G_OP_ACTIVITY_INBOUND
716                ,p_task_execute_rec  => l_task_rec
717                ,p_operation_type_id => G_OP_TYPE_LOAD
718                ,x_return_status     => x_return_status
719                ,x_msg_data          => x_msg_data
720                ,x_msg_count         => x_msg_count
721                ,x_error_code        => l_error_code
722 	       ,x_drop_lpn_option   => l_drop_lpn_option
723                ,x_consolidation_method_id  => l_consolidation_method_id
724                );
725 
726 
727              l_progress := '260';
728 
729              IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
730                 IF (l_debug = 1) THEN
731                    DEBUG(l_error_code || ' Error in activate_operation_instance ' ,l_proc_name,1);
732                 END IF;
733                 l_progress := '270';
734                 RAISE fnd_api.g_exc_error;
735              END IF;
736 
737 
738              INSERT INTO WMS_PUTAWAY_GROUP_TASKS_GTMP
739                (
740                        ORGANIZATION_ID
741                        ,TRANSACTION_TEMP_ID
742                        ,TRANSACTION_HEADER_ID
743                        ,LPN_ID
744                        ,LPN_NAME
745                        ,LPN_CONTEXT
746                        ,PARENT_LPN_ID
747                        ,PARENT_LPN_NAME
748                        ,OUTERMOST_LPN_ID
749                        ,OUTERMOST_LPN_NAME
750                        ,CONSOLIDATED_LPN_ID
751                        ,CONSOLIDATED_LPN_NAME
752                        ,INTO_LPN_ID
753                        ,INTO_LPN_NAME
754                        ,DROP_TYPE
755                        ,DROP_ORDER
756                        ,INVENTORY_ITEM_ID
757                        ,ITEM
758                        ,GROUP_ID
759                        ,LOT_NUMBER
760                        ,REVISION
761                        ,TRANSACTION_QUANTITY
762                        ,TRANSACTION_UOM
763                        ,LOCATOR
764                        ,PRIMARY_UOM_CODE
765                        ,DEST_SUBINVENTORY
766                        ,DEST_LOCATOR
767                        ,REVISION_QTY_CONTROL_CODE
768                        ,LOT_CONTROL_CODE
769                        ,SERIAL_NUMBER_CONTROL_CODE
770                        ,RESTRICT_SUBINVENTORIES_CODE
771                        ,RESTRICT_LOCATORS_CODE
772                        ,LOCATION_CONTROL_CODE
773                        ,ALLOWED_UNITS_LOOKUP_CODE
774                        ,BACKORDER_DELIVERY_DETAIL
775                        ,CROSSDOCK_TYPE
776                        ,WIP_SUPPLY_TYPE
777                        ,FROM_SUBINVENTORY
778                        ,FROM_LOCATOR
779                        ,TRANSFER_SUBINVENTORY
780                        ,TRANSFER_TO_LOCATION
781                        ,TRANSFER_ORGANIZATION
782                        ,TRANSACTION_ACTION_ID
783                        ,REFERENCE
784                        ,LOC_DROPPING_ORDER
785                        ,SUB_DROPPING_ORDER
786                        ,ROW_TYPE
787                        ,MOVE_ORDER_LINE_ID
788                        ,PROJECT_ID
789                        ,TASK_ID
790                        ,TXN_SOURCE_ID
791                        ,PRIMARY_QUANTITY
792                        ,LPN_LEVEL
793                        ,SECONDARY_QUANTITY -- 9037915
794                        ,SECONDARY_UOM      -- 9037915
795                )
796 
797                  SELECT DISTINCT
798                        gtmp_rec.organization_id,
799                        gtmp_rec.transaction_temp_id,
800                        gtmp_rec.transaction_header_id,
801                        gtmp_rec.lpn_id,
802                        wln.license_plate_number,                   --lpn_name
803                        wln.lpn_context,
804                        wln.parent_lpn_id,
805                        NULL,
806                        wln.outermost_lpn_id,        --Outermost LPN ID
807                        (SELECT license_plate_number FROM wms_license_plate_numbers WHERE lpn_id=wln.outermost_lpn_id),                   --Outermost LPN Name
808                        TO_NUMBER(NULL),        --Consolidated LPN ID
809                        NULL,                   --Consolidated LPN Name
810                        gtmp_rec.cartonization_id,  --INTO LPN ID
811                        NULL,                   --INTO LPN Name
812                        G_DT_ITEM_DROP,         --Drop Type
813                        p_group_id,                 --Drop Order
814                        gtmp_rec.inventory_item_id,
815                        gtmp_rec.item, --Item
816                        p_group_id,        --Group ID
817                        gtmp_rec.lot_number,
818                        gtmp_rec.revision,
819                        gtmp_rec.transaction_quantity,
820                        gtmp_rec.transaction_uom,
821                        gtmp_rec.LOCATOR, --inv_project.get_locsegs (milk.inventory_location_id, milk.organization_id ) LOCATOR,
822                        gtmp_rec.primary_uom_code,
823                        gtmp_rec.dest_subinventory,
824                        gtmp_rec.dest_locator,
825                        gtmp_rec.revision_qty_control_code,
826                        gtmp_rec.lot_control_code,
827                        gtmp_rec.serial_number_control_code,
828                        gtmp_rec.restrict_subinventories_code,
829                        gtmp_rec.restrict_locators_code,
830                        gtmp_rec.location_control_code,
831                        gtmp_rec.allowed_units_lookup_code,
832                        gtmp_rec.backorder_delivery_detail_id,
833                        gtmp_rec.crossdock_type,
834                        gtmp_rec.wip_supply_type,
835                        gtmp_rec.from_subinventory,
836                        gtmp_rec.from_locator,
837                        gtmp_rec.transfer_subinventory,
838                        gtmp_rec.transfer_to_location,
839                        gtmp_rec.transfer_organization,
840                        gtmp_rec.transaction_action_id,
841                        gtmp_rec.REFERENCE,
842                        to_number(null), -- milk.dropping_order,
843                        to_number(NULL), -- msi.dropping_order,
844                        G_ROW_TP_ALL_TASK,
845                        gtmp_rec.line_id,
846                        gtmp_rec.project_id,
847                        gtmp_rec.task_id,
848                        gtmp_rec.txn_source_id,
849                        gtmp_rec.primary_quantity,
850                        (SELECT     level
851                                              FROM wms_license_plate_numbers
852                                              WHERE organization_id = p_organization_id
853                                              AND lpn_id=gtmp_rec.lpn_id
854                                              AND ROWNUM=1
855                                        START WITH lpn_id = p_lpn_id
856                                        CONNECT BY parent_lpn_id = PRIOR lpn_id),
857                         gtmp_rec.secondary_transaction_quantity, -- 9037915
858                         gtmp_rec.secondary_uom_code              -- 9037915
859                    FROM wms_license_plate_numbers wln
860                  WHERE wln.organization_id = p_organization_id
861                          AND wln.lpn_id = gtmp_rec.lpn_id;
862 
863 
864        END LOOP;
865 
866        l_progress := '280';
867        -- Remove the already processed records in the GTMP table.
868 
869        x_rec_count := Remove_Tasks_In_Group(
870                          p_del_type  =>  4
871                        , p_group_id  =>  p_group_id);
872 
873        IF (l_debug = 1) THEN
874          DEBUG(' After removing '||x_rec_count||' processed tasks for the group '||p_group_id,l_proc_name,1);
875        END IF;
876 
877        -- This will group the all task rows where drop tpe = 'ID' based on the grouping criteria
878        l_progress := '290';
879        FOR grp_rec IN (SELECT MIN(ROWID) row_id
880                              ,MIN(drop_order) drop_order
881                              ,SUM(transaction_quantity) transaction_quantity
882                              ,SUM(primary_quantity) primary_quantity
883                        FROM  WMS_PUTAWAY_GROUP_TASKS_GTMP
884                        WHERE row_type = G_ROW_TP_ALL_TASK
885                          AND drop_type = G_DT_ITEM_DROP
886                          AND group_id=p_group_id
887                          AND NOT (crossdock_type > 1
888                                    AND backorder_delivery_detail > 0
889                                    AND wip_supply_type = 1
890                                    )
891                        GROUP BY  lpn_id
892                          ,inventory_item_id
893                          ,transaction_uom
894                          ,revision
895                          ,lot_number
896                          ,dest_locator
897                              ,into_lpn_id) LOOP
898 
899              INSERT INTO WMS_PUTAWAY_GROUP_TASKS_GTMP
900              (
901                ORGANIZATION_ID
902               ,TRANSACTION_TEMP_ID
903               ,TRANSACTION_HEADER_ID
904               ,LPN_ID
905               ,LPN_NAME
906               ,LPN_CONTEXT
907               ,PARENT_LPN_ID
908               ,PARENT_LPN_NAME
909               ,OUTERMOST_LPN_ID
910               ,OUTERMOST_LPN_NAME
911               ,CONSOLIDATED_LPN_ID
912               ,CONSOLIDATED_LPN_NAME
913               ,INTO_LPN_ID
914               ,INTO_LPN_NAME
915               ,DROP_TYPE
916               ,DROP_ORDER
917               ,INVENTORY_ITEM_ID
918               ,ITEM
919               ,GROUP_ID
920               ,LOT_NUMBER
921               ,REVISION
922               ,TRANSACTION_QUANTITY
923               ,TRANSACTION_UOM
924               ,LOCATOR
925               ,PRIMARY_UOM_CODE
926               ,DEST_SUBINVENTORY
927               ,DEST_LOCATOR
928               ,REVISION_QTY_CONTROL_CODE
929               ,LOT_CONTROL_CODE
930               ,SERIAL_NUMBER_CONTROL_CODE
931               ,RESTRICT_SUBINVENTORIES_CODE
932               ,RESTRICT_LOCATORS_CODE
933               ,LOCATION_CONTROL_CODE
934               ,ALLOWED_UNITS_LOOKUP_CODE
935               ,BACKORDER_DELIVERY_DETAIL
936               ,CROSSDOCK_TYPE
937               ,WIP_SUPPLY_TYPE
938               ,FROM_SUBINVENTORY
939               ,FROM_LOCATOR
940               ,TRANSFER_SUBINVENTORY
941               ,TRANSFER_TO_LOCATION
942               ,TRANSFER_ORGANIZATION
943               ,TRANSACTION_ACTION_ID
944               ,REFERENCE
945               ,LOC_DROPPING_ORDER
946               ,SUB_DROPPING_ORDER
947               ,ROW_TYPE
948               ,MOVE_ORDER_LINE_ID
949               ,PROJECT_ID
950               ,TASK_ID
951               ,TXN_SOURCE_ID
952               ,PRIMARY_QUANTITY
953               ,PROCESS_FLAG
954               ,WIP_JOB
955               ,WIP_LINE
956               ,WIP_DEPT
957               ,WIP_OP_SEQ
958               ,WIP_ENTITY_TYPE
959               ,WIP_START_DATE
960               ,WIP_SCHEDULE
961               ,WIP_ASSEMBLY
962               ,SECONDARY_QUANTITY -- 9037915
963               ,SECONDARY_UOM      -- 9037915
964              )
965                  SELECT
966                    ORGANIZATION_ID
967                   ,TRANSACTION_TEMP_ID
968                   ,TRANSACTION_HEADER_ID
969                   ,LPN_ID
970                   ,LPN_NAME
971                   ,LPN_CONTEXT
972                   ,PARENT_LPN_ID
973                   ,PARENT_LPN_NAME
974                   ,OUTERMOST_LPN_ID
975                   ,OUTERMOST_LPN_NAME
976                   ,CONSOLIDATED_LPN_ID
977                   ,CONSOLIDATED_LPN_NAME
978                   ,INTO_LPN_ID
979                   ,INTO_LPN_NAME
980                   ,DROP_TYPE
981                   ,grp_rec.drop_order
982                   ,INVENTORY_ITEM_ID
983                   ,ITEM
984                   ,p_group_id
985                   ,LOT_NUMBER
986                   ,REVISION
987                   ,grp_rec.transaction_quantity
988                   ,TRANSACTION_UOM
989                   ,LOCATOR
990                   ,PRIMARY_UOM_CODE
991                   ,DEST_SUBINVENTORY
992                   ,DEST_LOCATOR
993                   ,REVISION_QTY_CONTROL_CODE
994                   ,LOT_CONTROL_CODE
995                   ,SERIAL_NUMBER_CONTROL_CODE
996                   ,RESTRICT_SUBINVENTORIES_CODE
997                   ,RESTRICT_LOCATORS_CODE
998                   ,LOCATION_CONTROL_CODE
999                   ,ALLOWED_UNITS_LOOKUP_CODE
1000                   ,BACKORDER_DELIVERY_DETAIL
1001                   ,CROSSDOCK_TYPE
1002                   ,WIP_SUPPLY_TYPE
1003                   ,FROM_SUBINVENTORY
1004                   ,FROM_LOCATOR
1005                   ,TRANSFER_SUBINVENTORY
1006                   ,TRANSFER_TO_LOCATION
1007                   ,TRANSFER_ORGANIZATION
1008                   ,TRANSACTION_ACTION_ID
1009                   ,REFERENCE
1010                   ,LOC_DROPPING_ORDER
1011                   ,SUB_DROPPING_ORDER
1012                   ,G_ROW_TP_GROUP_TASK
1013                   ,MOVE_ORDER_LINE_ID
1014                   ,PROJECT_ID
1015                   ,TASK_ID
1016                   ,TXN_SOURCE_ID
1017                   ,grp_rec.primary_quantity
1018                   ,PROCESS_FLAG
1019                   ,WIP_JOB
1020                   ,WIP_LINE
1021                   ,WIP_DEPT
1022                   ,WIP_OP_SEQ
1023                   ,WIP_ENTITY_TYPE
1024                   ,WIP_START_DATE
1025                   ,WIP_SCHEDULE
1026                   ,WIP_ASSEMBLY
1027                   ,SECONDARY_QUANTITY -- 9037915
1028                   ,SECONDARY_UOM      -- 9037915
1029                  FROM  WMS_PUTAWAY_GROUP_TASKS_GTMP
1030                  WHERE ROWID = grp_rec.row_id;
1031 
1032        END LOOP;
1033        l_progress := '300';
1034 
1035        SELECT MTL_MATERIAL_TRANSACTIONS_S.NEXTVAL
1036            INTO l_txn_header_id
1037            FROM dual;
1038 
1039        l_progress := '310';
1040 
1041        UPDATE WMS_PUTAWAY_GROUP_TASKS_GTMP
1042          SET  transaction_header_id = l_txn_header_id
1043        WHERE  group_id = p_group_id
1044          RETURNING transaction_temp_id
1045          BULK COLLECT INTO l_txn_temp_id_tab;
1046 
1047        l_progress := '320';
1048 
1049        IF (l_debug = 1) THEN
1050          DEBUG(sql%rowcount || ' Row(s) updated in WMS_PUTAWAY_GROUP_TASKS_GTMP with txn_header_id = ' || l_txn_header_id ||
1051                ' and group_id = ' || p_group_id
1052                ,l_proc_name,9);
1053        END IF;
1054 
1055        l_progress := '330';
1056 
1057 
1058        FORALL i IN 1 ..l_txn_temp_id_tab.COUNT
1059          UPDATE  MTL_MATERIAL_TRANSACTIONS_TEMP
1060            SET   transaction_header_id = l_txn_header_id
1061            WHERE  transaction_temp_id = l_txn_temp_id_tab(i);
1062 
1063        l_progress := '340';
1064 
1065        IF (l_debug = 1) THEN
1066          DEBUG(sql%rowcount || ' Row(s) updated in WMS_PUTAWAY_GROUP_TASKS_GTMP with txn_header_id= ' || l_txn_header_id ,l_proc_name,9);
1067        END IF;
1068 
1069        l_progress := '350';
1070 
1071       EXCEPTION
1072         WHEN FND_API.G_EXC_ERROR THEN
1073               IF (l_debug=1) THEN
1074                 DEBUG('Expected Error at '||l_progress, l_proc_name,1);
1075               END IF;
1076 
1077               IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_error) THEN
1078                  fnd_msg_pub.add_exc_msg(g_pkg_name, l_proc_name, SQLERRM);
1079               END IF; /* fnd_msg.... */
1080 
1081               fnd_msg_pub.count_and_get(
1082                                          p_count => x_msg_count,
1083                                          p_data  => x_msg_data
1084                                         );
1085               --x_error_code := SQLCODE;
1086               x_return_status := fnd_api.g_ret_sts_error;
1087 
1088               ROLLBACK TO update_gtmp_sp;
1089 
1090         WHEN OTHERS THEN
1091               IF (l_debug=1) THEN
1092                 DEBUG('Unexpected error at'||l_progress||' '||SQLERRM, l_proc_name,1);
1093               END IF;
1094 
1095 
1096               IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
1097                  fnd_msg_pub.add_exc_msg(g_pkg_name, l_proc_name, SQLERRM);
1098               END IF; /* fnd_msg.... */
1099 
1100               fnd_msg_pub.count_and_get(
1101                                          p_count => x_msg_count,
1102                                          p_data  => x_msg_data
1103                                         );
1104               --x_error_code := SQLCODE;
1105               x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1106               ROLLBACK TO update_gtmp_sp;
1107       END Update_Tasks_In_Group;
1108 
1109 
1110 
1111 /**
1112   *   This function will update the transaction_header_id of the
1113   *   group(both in the global temp table and in MMTT) with same
1114   *   value.
1115   *   It will also stamp a common group_id for the group.
1116 
1117   *  @ RETURN  NUMBER
1118 
1119 **/
1120    FUNCTION Sync_Group_Tasks
1121      RETURN NUMBER
1122      IS
1123      l_debug            NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
1124      l_progress         VARCHAR2(10) := '10';
1125      l_proc_name        VARCHAR2(30) := 'Sync_Group_Tasks:';
1126      l_rec_count        NUMBER       := 0;
1127 
1128      l_drop_order       NUMBER;
1129      l_txn_header_id    NUMBER;
1130 
1131      l_txn_temp_id_tab  num_tab;
1132 
1133      CURSOR c_all_group_tasks_cursor IS
1134        SELECT drop_order
1135        FROM   WMS_PUTAWAY_GROUP_TASKS_GTMP
1136        WHERE  row_type = G_ROW_TP_GROUP_TASK;
1137 
1138    BEGIN
1139 
1140      IF (l_debug = 1) THEN
1141       DEBUG(' Function Entered at ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
1142      END IF;
1143      l_progress := '100';
1144 
1145      BEGIN
1146         OPEN c_all_group_tasks_cursor;
1147         l_progress := '110';
1148 
1149         LOOP --c_all_group_tasks_cursor loop
1150           FETCH  c_all_group_tasks_cursor
1151             INTO l_drop_order;
1152           l_progress := '120';
1153 
1154           EXIT WHEN c_all_group_tasks_cursor%NOTFOUND;
1155           l_progress := '130';
1156 
1157           --l_txn_header_id := MTL_MATERIAL_TRANSACTIONS_S.NEXTVAL;
1158 
1159           SELECT MTL_MATERIAL_TRANSACTIONS_S.NEXTVAL
1160             INTO l_txn_header_id
1161             FROM dual;
1162           l_progress := '140';
1163 
1164           UPDATE WMS_PUTAWAY_GROUP_TASKS_GTMP
1165             SET  transaction_header_id = l_txn_header_id
1166                 ,group_id              = l_drop_order
1167           WHERE  drop_order = l_drop_order
1168             RETURNING transaction_temp_id
1169             BULK COLLECT INTO l_txn_temp_id_tab;
1170           l_progress := '150';
1171 
1172           l_rec_count := l_rec_count + SQL%rowcount;
1173           IF (l_debug = 1) THEN
1174            DEBUG(sql%rowcount || ' Row(s) updated in WMS_PUTAWAY_GROUP_TASKS_GTMP with txn_header_id = ' || l_txn_header_id ||
1175                  ' and group_id = ' || l_drop_order
1176                   ,l_proc_name,9);
1177           END IF;
1178           l_progress := '160';
1179 
1180 
1181           FORALL i IN 1 ..l_txn_temp_id_tab.COUNT
1182             UPDATE  MTL_MATERIAL_TRANSACTIONS_TEMP
1183               SET   transaction_header_id = l_txn_header_id
1184              WHERE  transaction_temp_id = l_txn_temp_id_tab(i);
1185           l_progress := '170';
1186 
1187           l_rec_count := l_rec_count + SQL%rowcount;
1188           IF (l_debug = 1) THEN
1189            DEBUG(sql%rowcount || ' Row(s) updated in WMS_PUTAWAY_GROUP_TASKS_GTMP with txn_header_id= ' || l_txn_header_id ,l_proc_name,9);
1190           END IF;
1191           l_progress := '180';
1192 
1193 
1194         END LOOP;--c_all_group_tasks_cursor loop
1195 
1196         CLOSE c_all_group_tasks_cursor;
1197         l_progress := '190';
1198 
1199      EXCEPTION
1200        WHEN OTHERS THEN
1201          DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
1202          DEBUG(SQLERRM,l_proc_name);
1203 
1204          IF c_all_group_tasks_cursor%ISOPEN THEN
1205            CLOSE c_all_group_tasks_cursor;
1206          END IF;
1207 
1208          RETURN -1;
1209      END;
1210 
1211      IF (l_debug = 1) THEN
1212       DEBUG(' Function Exited at ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
1213      END IF;
1214      l_progress := '490';
1215 
1216      RETURN l_rec_count;
1217      l_progress := '500';
1218    EXCEPTION
1219      WHEN OTHERS THEN
1220        DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
1221        DEBUG(SQLERRM,l_proc_name);
1222        RETURN -1;
1223 
1224    END Sync_Group_Tasks;
1225 
1226 
1227 
1228 /**
1229   *   This function will mark the consolidated LPN of all the
1230   *   child records of the lpn_id and lpn_name passed.
1231   *   This will also mark the drop_order with the minimum
1232   *   drop_order in that group.
1233   *   This will return the number of rows processed. This will
1234   *   return -1 in case of failure.
1235 
1236   *  @param  p_lpn_id   LPN ID for whose childs the allocated LPN has to be marked.
1237   *  @ RETURN  NUMBER
1238 
1239 
1240 **/
1241    FUNCTION Mark_Consolidated_LPN( p_lpn_id     IN NUMBER
1242                                   ,p_lpn_name   IN VARCHAR2 )
1243      RETURN NUMBER
1244      IS
1245 
1246      l_debug          NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
1247      l_progress       VARCHAR2(10) := '10';
1248      l_proc_name      VARCHAR2(30) := 'Mark_Consolidated_LPN:';
1249      l_rec_count      NUMBER       := 0;
1250      l_min_drop_order NUMBER;
1251 
1252      l_lpn_id_tab   num_tab;
1253 
1254      --This cursor will fetch all the child LPNs for the LPN Passed
1255      CURSOR c_child_lpn_cursor IS
1256         SELECT lpn_id
1257         FROM   wms_license_plate_numbers
1258         START  WITH lpn_id = p_lpn_id
1259         CONNECT BY PRIOR lpn_id = parent_lpn_id;
1260 
1261    BEGIN
1262 
1263      l_progress := '100';
1264 
1265      IF (l_debug = 1) THEN
1266       DEBUG(' p_lpn_id     ==> '||p_lpn_id,l_proc_name,4);
1267       DEBUG(' p_lpn_name   ==> '||p_lpn_name,l_proc_name,4);
1268      END IF;
1269 
1270      l_progress := '130';
1271      OPEN  c_child_lpn_cursor;
1272      l_progress := '140';
1273 
1274      FETCH c_child_lpn_cursor
1275        BULK COLLECT INTO l_lpn_id_tab;
1276      l_progress := '150';
1277 
1278      CLOSE c_child_lpn_cursor;
1279      l_progress := '160';
1280 
1281      SELECT MIN(drop_order)
1282      INTO   l_min_drop_order
1283      FROM   wms_putaway_group_tasks_gtmp
1284      WHERE  lpn_id = p_lpn_id
1285      OR     lpn_id IN ( SELECT lpn_id
1286                         FROM   wms_license_plate_numbers
1287                         START  WITH lpn_id = p_lpn_id
1288                         CONNECT BY PRIOR lpn_id = parent_lpn_id
1289                       );
1290      l_progress := '170';
1291 
1292      --Update the cosolidated lpn for the child LPNs with the lpn passed
1293 /*     UPDATE wms_putaway_group_tasks_gtmp
1294        SET  consolidated_lpn_id    = p_lpn_id
1295            ,consolidated_lpn_name  = l_lpn_name
1296            ,drop_order             = p_drop_order
1297        WHERE lpn_id IN (SELECT lpn_id
1298                         FROM   wms_license_plate_numbers
1299                         START  WITH lpn_id = p_lpn_id
1300                         CONNECT BY PRIOR lpn_id = parent_lpn_id
1301                       );*/
1302 
1303 
1304      -- Do bulk update for all LPN and all itz child the consolidated LPN and the min drop order in that group
1305      FORALL i IN 1 .. l_lpn_id_tab.COUNT
1306       UPDATE wms_putaway_group_tasks_gtmp
1307        SET  consolidated_lpn_id    = p_lpn_id
1308            ,consolidated_lpn_name  = p_lpn_name
1309            ,drop_order             = l_min_drop_order
1310        WHERE lpn_id = l_lpn_id_tab(i);
1311 
1312      l_progress := '240';
1313 
1314      IF (l_debug = 1) THEN
1315       DEBUG('Updated the consolidated LPN columns for the children ',l_proc_name,9);
1316      END IF;
1317 
1318 
1319      l_progress := '250';
1320      l_rec_count := SQL%rowcount;
1321      l_progress := '160';
1322 
1323      IF (l_debug = 1) THEN
1324        DEBUG(' l_rec_count => '|| l_rec_count,l_proc_name,4);
1325        DEBUG(' Function Exited at ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
1326      END IF;
1327      l_progress := '280';
1328 
1329      -- Return the number of rows updated for consolidated drop
1330      RETURN l_rec_count;
1331 
1332    EXCEPTION
1333      WHEN OTHERS THEN
1334      DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
1335      DEBUG(SQLERRM,l_proc_name,1);
1336      RETURN -1;
1337    END Mark_Consolidated_LPN;
1338 
1339 
1340 
1341 
1342 /**
1343   *   This function will mark drop_type as 'ID'(Item Drop) for
1344   *   all the contents of the lpn which is passed as input and
1345   *   all its parents. This will call the same function
1346   *   recursively till it doesn't find parent lpn.
1347   *   This function will return the number of rows updated as
1348   *   Item Drop. This will retun -1 in case of a failure.
1349 
1350   *  @ RETURN  NUMBER
1351 
1352 
1353 **/
1354    FUNCTION Mark_Item_Drop(p_lpn_id IN NUMBER)
1355      RETURN NUMBER
1356      IS
1357      l_debug          NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
1358      l_progress       VARCHAR2(10) := '10';
1359      l_proc_name      VARCHAR2(30) := 'Mark_Item_Drop:';
1360      l_rec_count      NUMBER       := 0;
1361      l_temp           NUMBER       := 0;
1362      l_parent_lpn_id  NUMBER;
1363    BEGIN
1364     IF (l_debug = 1) THEN
1365       DEBUG(' p_lpn_id ==> '||p_lpn_id,'Mark_Item_Drop',4);
1366     END IF;
1367 
1368     BEGIN --ID Marking
1369         -- Select the parent lpn id of the passed lpn
1370         l_progress := '100';
1371         SELECT parent_lpn_id
1372           INTO l_parent_lpn_id
1373           FROM WMS_PUTAWAY_GROUP_TASKS_GTMP
1374           WHERE lpn_id = p_lpn_id
1375                 AND ROWNUM < 2;
1376         l_progress := '110';
1377 
1378         -- Set the drop type to 'ID' for all the contents of the lpn passed
1379         l_progress := '120';
1380         UPDATE WMS_PUTAWAY_GROUP_TASKS_GTMP
1381           SET drop_type = G_DT_ITEM_DROP
1382           WHERE lpn_id = p_lpn_id;
1383         l_progress := '130';
1384 
1385         l_progress := '135';
1386         l_rec_count := SQL%rowcount;
1387         l_progress := '138';
1388 
1389     EXCEPTION
1390       WHEN no_data_found THEN
1391         l_progress := '139';
1392         -- Parent LPN is not in GTMP so mark the parent as null so that the recursion ends.
1393         l_parent_lpn_id := NULL;
1394 
1395       WHEN OTHERS THEN
1396         DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
1397         DEBUG(SQLERRM,l_proc_name,1);
1398         RETURN -1;
1399     END; --ID Marking
1400 
1401 
1402     IF l_parent_lpn_id IS NOT NULL THEN
1403 
1404       l_progress := '140';
1405       -- Call Mark_Item_Drop for the parent LPN
1406       l_temp     := Mark_Item_Drop(l_parent_lpn_id);
1407       l_progress := '150';
1408 
1409       IF l_temp <> -1 THEN
1410         --Increment the record updated counter
1411         l_progress := '160';
1412         l_rec_count := l_rec_count + l_temp;
1413 
1414       ELSE
1415       --returned -1 and hence error so fail
1416         l_progress := '170';
1417         RETURN -1;
1418       END IF;
1419 
1420     END IF;
1421 
1422     IF (l_debug = 1) THEN
1423       DEBUG(' l_rec_count => '|| l_rec_count,l_proc_name,4);
1424       DEBUG(' Function Exited at ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
1425     END IF;
1426     l_progress := '180';
1427 
1428 
1429     -- Return the number of rows marked for item drop
1430     RETURN l_rec_count;
1431 
1432    EXCEPTION
1433      WHEN OTHERS THEN
1434        DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
1435        DEBUG(SQLERRM,l_proc_name,1);
1436        RETURN -1;
1437    END Mark_Item_Drop;
1438 
1439 
1440 /**
1441   *   This function will create the group task row in the global temp table
1442   *   With the data passed as input
1443   *
1444   *   This will return the number of rows inserted as output.
1445   *   This will return -1 in case of failure.
1446   *  @ RETURN  NUMBER
1447 
1448 **/
1449    FUNCTION Insert_Group_Tasks (
1450       l_rowid_tab      IN rowid_tab
1451      ,l_txn_qty_tab    IN num_tab
1452      ,l_pri_qty_tab    IN num_tab
1453      ,l_drop_order_tab IN num_tab
1454      ,l_show_message_tab IN num_tab --R12
1455      ,l_error_code_tab IN varchar240_tab
1456      ,l_drop_lpn_option_tab IN num_tab
1457      ,l_consolidation_method_id_tab IN num_tab )
1458      RETURN NUMBER
1459      IS
1460      l_debug            NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
1461      l_progress         VARCHAR2(10) := '10';
1462      l_proc_name        VARCHAR2(30) := 'Insert_Group_Tasks:';
1463      l_rec_count        NUMBER       := 0;
1464 
1465    BEGIN
1466 
1467      IF (l_debug = 1) THEN
1468        DEBUG('Start of Function ', l_proc_name,9);
1469      END IF;
1470      l_progress := '100';
1471 
1472      -- Printing the i/p parameters
1473      IF (l_debug = 1) THEN
1474        FOR i IN 1 .. l_rowid_tab.COUNT
1475        LOOP
1476          DEBUG('l_txn_qty_tab    ==> ' || l_txn_qty_tab(i),l_proc_name,4);
1477          DEBUG('l_pri_qty_tab    ==> ' || l_pri_qty_tab(i),l_proc_name,4);
1478          DEBUG('l_drop_order_tab ==> ' || l_drop_order_tab(i),l_proc_name,4);
1479          DEBUG('l_show_message_tab ==> ' || l_show_message_tab(i),l_proc_name,4);
1480          DEBUG('l_error_code_tab   ==> ' || l_error_code_tab(i),l_proc_name,4);
1481        END LOOP;
1482      END IF; -- Printing the i/p parameters
1483      l_progress := '110';
1484 
1485        -- This will bulk insert consolidated records for group tasks into the temp table
1486        FORALL i IN 1 .. l_rowid_tab.COUNT
1487           INSERT INTO WMS_PUTAWAY_GROUP_TASKS_GTMP
1488           (
1489             ORGANIZATION_ID
1490            ,TRANSACTION_TEMP_ID
1491            ,TRANSACTION_HEADER_ID
1492            ,LPN_ID
1493            ,LPN_NAME
1494            ,LPN_CONTEXT
1495            ,PARENT_LPN_ID
1496            ,PARENT_LPN_NAME
1497            ,OUTERMOST_LPN_ID
1498            ,OUTERMOST_LPN_NAME
1499            ,CONSOLIDATED_LPN_ID
1500            ,CONSOLIDATED_LPN_NAME
1501            ,INTO_LPN_ID
1502            ,INTO_LPN_NAME
1503            ,DROP_TYPE
1504            ,DROP_ORDER
1505            ,INVENTORY_ITEM_ID
1506            ,ITEM
1507            ,GROUP_ID
1508            ,LOT_NUMBER
1509            ,REVISION
1510            ,TRANSACTION_QUANTITY
1511            ,TRANSACTION_UOM
1512            ,LOCATOR
1513            ,PRIMARY_UOM_CODE
1514            ,DEST_SUBINVENTORY
1515            ,DEST_LOCATOR
1516            ,REVISION_QTY_CONTROL_CODE
1517            ,LOT_CONTROL_CODE
1518            ,SERIAL_NUMBER_CONTROL_CODE
1519            ,RESTRICT_SUBINVENTORIES_CODE
1520            ,RESTRICT_LOCATORS_CODE
1521            ,LOCATION_CONTROL_CODE
1522            ,ALLOWED_UNITS_LOOKUP_CODE
1523            ,BACKORDER_DELIVERY_DETAIL
1524            ,CROSSDOCK_TYPE
1525            ,WIP_SUPPLY_TYPE
1526            ,FROM_SUBINVENTORY
1527            ,FROM_LOCATOR
1528            ,TRANSFER_SUBINVENTORY
1529            ,TRANSFER_TO_LOCATION
1530            ,TRANSFER_ORGANIZATION
1531            ,TRANSACTION_ACTION_ID
1532            ,REFERENCE
1533            ,LOC_DROPPING_ORDER
1534            ,SUB_DROPPING_ORDER
1535            ,ROW_TYPE
1536            ,MOVE_ORDER_LINE_ID
1537            ,PROJECT_ID
1538            ,TASK_ID
1539            ,TXN_SOURCE_ID
1540            ,PRIMARY_QUANTITY
1541            ,PROCESS_FLAG
1542            ,WIP_JOB
1543            ,WIP_LINE
1544            ,WIP_DEPT
1545            ,WIP_OP_SEQ
1546            ,WIP_ENTITY_TYPE
1547            ,WIP_START_DATE
1548            ,WIP_SCHEDULE
1549            ,WIP_ASSEMBLY
1550            ,SECONDARY_QUANTITY --OPM Convergence
1551            ,SECONDARY_UOM --OPM Convergence
1552 	   ,show_message -- R12
1553 	   ,error_code --R12
1554            ,drop_lpn_option --R12
1555 	   ,consolidation_method_id --R12
1556 	   ,sub_lpn_controlled_flag --R12
1557 	 )
1558               SELECT
1559                 ORGANIZATION_ID
1560                ,TRANSACTION_TEMP_ID
1561                ,TRANSACTION_HEADER_ID
1562                ,LPN_ID
1563                ,LPN_NAME
1564                ,LPN_CONTEXT
1565                ,PARENT_LPN_ID
1566                ,PARENT_LPN_NAME
1567                ,OUTERMOST_LPN_ID
1568                ,OUTERMOST_LPN_NAME
1569                ,CONSOLIDATED_LPN_ID
1570                ,CONSOLIDATED_LPN_NAME
1571                ,INTO_LPN_ID
1572                ,INTO_LPN_NAME
1573                ,DROP_TYPE
1574                ,l_drop_order_tab(i)
1575                ,INVENTORY_ITEM_ID
1576                ,ITEM
1577                ,GROUP_ID
1578                ,LOT_NUMBER
1579                ,REVISION
1580                ,l_txn_qty_tab(i)
1581                ,TRANSACTION_UOM
1582                ,LOCATOR
1583                ,PRIMARY_UOM_CODE
1584                ,DEST_SUBINVENTORY
1585                ,DEST_LOCATOR
1586                ,REVISION_QTY_CONTROL_CODE
1587                ,LOT_CONTROL_CODE
1588                ,SERIAL_NUMBER_CONTROL_CODE
1589                ,RESTRICT_SUBINVENTORIES_CODE
1590                ,RESTRICT_LOCATORS_CODE
1591                ,LOCATION_CONTROL_CODE
1592                ,ALLOWED_UNITS_LOOKUP_CODE
1593                -- ,BACKORDER_DELIVERY_DETAIL
1594                ,DECODE(backorder_delivery_detail,NULL,NULL,0,0,get_grouping(backorder_delivery_detail)) --Added for bug 5286880
1595                ,CROSSDOCK_TYPE
1596                ,WIP_SUPPLY_TYPE
1597                ,FROM_SUBINVENTORY
1598                ,FROM_LOCATOR
1599                ,TRANSFER_SUBINVENTORY
1600                ,TRANSFER_TO_LOCATION
1601                ,TRANSFER_ORGANIZATION
1602                ,TRANSACTION_ACTION_ID
1603                ,REFERENCE
1604                ,LOC_DROPPING_ORDER
1605                ,SUB_DROPPING_ORDER
1606                ,G_ROW_TP_GROUP_TASK
1607                ,MOVE_ORDER_LINE_ID
1608                ,PROJECT_ID
1609                ,TASK_ID
1610                ,TXN_SOURCE_ID
1611                ,l_pri_qty_tab(i)
1612                ,PROCESS_FLAG
1613                ,WIP_JOB
1614                ,WIP_LINE
1615                ,WIP_DEPT
1616                ,WIP_OP_SEQ
1617                ,WIP_ENTITY_TYPE
1618                ,WIP_START_DATE
1619                ,WIP_SCHEDULE
1620                ,WIP_ASSEMBLY
1621                ,SECONDARY_QUANTITY --OPM Convergence
1622 	       ,SECONDARY_UOM --OPM Convergence
1623 	       ,l_show_message_tab(i) --R12
1624 	       ,l_error_code_tab(i) --R12
1625 	       ,l_drop_lpn_option_tab(i) --R12
1626 	       ,l_consolidation_method_id_tab(i) --R12
1627                ,sub_lpn_controlled_flag --R12
1628 	      FROM  WMS_PUTAWAY_GROUP_TASKS_GTMP
1629               WHERE ROWID = l_rowid_tab(i);
1630        l_progress := '120';
1631 
1632        l_rec_count := SQL%rowcount;
1633        l_progress := '150';
1634 
1635        IF (l_debug = 1) THEN
1636          DEBUG('l_rec_count = ' || l_rec_count, l_proc_name,9);
1637        END IF;
1638        l_progress := '990';
1639        RETURN l_rec_count;
1640 
1641    EXCEPTION
1642      WHEN OTHERS THEN
1643        DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
1644        DEBUG(SQLERRM,l_proc_name,1);
1645        RETURN -1;
1646 
1647    END Insert_Group_Tasks;
1648 
1649 
1650 /**
1651   *   This function will insert the group_task rows
1652   *   This will insert one group task row per group.
1653   *
1654   *
1655   *   This will return the number of groups created as
1656   *   output.This will return -1 in case of failure.
1657 
1658   *  @ RETURN  NUMBER
1659 
1660 
1661 **/
1662    FUNCTION Group_Consolidated_Drop_Tasks
1663      RETURN NUMBER
1664      IS
1665      l_debug            NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
1666      l_progress         VARCHAR2(10) := '10';
1667      l_proc_name        VARCHAR2(30) := 'Group_Consolidated_Drop_Task:';
1668      l_rec_count        NUMBER       := 0;
1669      l_cur_count        NUMBER       := 0;
1670 
1671      l_rowid_tab        rowid_tab;
1672      l_txn_qty_tab      num_tab;
1673      l_pri_qty_tab      num_tab;
1674      l_drop_order_tab   num_tab;
1675      l_sec_qty_tab      num_tab; --OPM Convergence
1676      l_show_message_tab  num_tab;--R12
1677      l_error_code_tab   varchar240_tab;
1678      l_drop_lpn_option_tab num_tab;
1679      l_consolidation_method_id_tab num_tab;
1680 
1681      CURSOR c_group_tasks_cursor IS
1682        SELECT min(ROWID)
1683              ,SUM(transaction_quantity)
1684              ,SUM(primary_quantity)
1685              ,drop_order
1686              ,SUM(secondary_quantity) --OPM Convergence
1687 	     ,MAX(show_message) --R12
1688 	     ,MAX(error_code) --R12
1689 	     ,MAX(drop_lpn_option) --R12: This value should be unique per group
1690 	     ,MAX(consolidation_method_id) --R12: This value should be unique per group
1691        FROM   WMS_PUTAWAY_GROUP_TASKS_GTMP
1692        WHERE  row_type IN (G_ROW_TP_ALL_TASK, G_ROW_TP_LPN_TASK)
1693               AND drop_type = G_DT_CONSOLIDATED_DROP
1694        GROUP BY  drop_order;
1695 
1696    BEGIN
1697 
1698      IF (l_debug = 1) THEN
1699        DEBUG('Start of Function ', l_proc_name,9);
1700      END IF;
1701      l_progress := '100';
1702 
1703      -- Actual logic here
1704      OPEN c_group_tasks_cursor;
1705      l_progress := '110';
1706 
1707      LOOP --c_group_tasks_cursor
1708 
1709        EXIT WHEN c_group_tasks_cursor%NOTFOUND;
1710        l_progress := '200';
1711 
1712        FETCH c_group_tasks_cursor
1713         BULK COLLECT
1714          INTO  l_rowid_tab
1715               ,l_txn_qty_tab
1716               ,l_pri_qty_tab
1717               ,l_drop_order_tab
1718               ,l_sec_qty_tab --OPM Convergence
1719 	      ,l_show_message_tab --R12
1720 	      ,l_error_code_tab --R12
1721 	      ,l_drop_lpn_option_tab   --R12
1722               ,l_consolidation_method_id_tab  --R12
1723 	 LIMIT l_limit;
1724        l_progress := '210';
1725 
1726       -- Insert group task row into temp table
1727        /* Note: Not passing l_sec_qty_tab as input to insert_group_tasks
1728                 since only l_rowid_tab is used in this function to fetch
1729                 the corresponding records from the gtmp table. Infact all
1730                 the other tables are not needed to pass as inputs to this
1731                 Function
1732         */
1733        l_cur_count := Insert_Group_Tasks (
1734               l_rowid_tab      => l_rowid_tab
1735              ,l_txn_qty_tab    => l_txn_qty_tab
1736              ,l_pri_qty_tab    => l_pri_qty_tab
1737              ,l_drop_order_tab => l_drop_order_tab
1738 	     ,l_show_message_tab=> l_show_message_tab  --R12
1739 	     ,l_error_code_tab  => l_error_code_tab    --R12
1740 	     ,l_drop_lpn_option_tab => l_drop_lpn_option_tab              --R12
1741              ,l_consolidation_method_id_tab => l_consolidation_method_id_tab);	       --R12
1742        l_progress := '230';
1743 
1744        -- Check for errors while inserting group task row
1745        IF l_cur_count = -1 THEN
1746          l_progress := '232';
1747          RAISE fnd_api.g_exc_unexpected_error;
1748        END IF;
1749 
1750        l_rec_count := l_rec_count + l_cur_count;
1751        l_progress := '235';
1752 
1753        IF (l_debug = 1) THEN
1754         DEBUG( l_cur_count|| ' Group task Row(s) inserted in WMS_PUTAWAY_GROUP_TASKS_GTMP '
1755               ,l_proc_name,9);
1756        END IF;
1757        l_progress := '240';
1758 
1759        l_rec_count := l_rec_count + l_cur_count;
1760        l_progress := '250';
1761 
1762      END LOOP;--c_group_tasks_cursor
1763 
1764 
1765      IF (l_debug = 1) THEN
1766        DEBUG(' l_rec_count => '|| l_rec_count,l_proc_name,4);
1767        DEBUG(' Function Exited at ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
1768      END IF;
1769 
1770      l_progress := '990';
1771      CLOSE c_group_tasks_cursor;
1772      l_progress := '1000';
1773 
1774      RETURN l_rec_count;
1775 
1776    EXCEPTION
1777 
1778      WHEN fnd_api.g_exc_unexpected_error THEN
1779        DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
1780        DEBUG(SQLERRM,l_proc_name,1);
1781 
1782        IF c_group_tasks_cursor%isopen THEN
1783          CLOSE c_group_tasks_cursor;
1784        END IF;
1785 
1786        RETURN -1;
1787 
1788      WHEN OTHERS THEN
1789        DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
1790        DEBUG(SQLERRM,l_proc_name,1);
1791 
1792        IF c_group_tasks_cursor%isopen THEN
1793          CLOSE c_group_tasks_cursor;
1794        END IF;
1795 
1796        RETURN -1;
1797 
1798    END Group_Consolidated_Drop_Tasks;
1799 
1800 
1801 
1802 /**
1803   *   This function will group the MMTTs which are marked as
1804   *   'ID' -- Item Drop
1805   *
1806   *   This will return the number of rows processed as
1807   *   output.This will return -1 in case of failure.
1808 
1809   *  @ RETURN  NUMBER
1810 
1811 
1812 **/
1813    FUNCTION Group_Item_Drop_Tasks
1814      RETURN NUMBER
1815      IS
1816      l_debug            NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
1817      l_progress         VARCHAR2(10) := '10';
1818      l_proc_name        VARCHAR2(30) := 'Group_Item_Drop_Tasks:';
1819      l_rec_count        NUMBER       := 0;
1820      l_cur_count        NUMBER       := 0;
1821 
1822      l_rowid_tab        rowid_tab;
1823      l_tempid_tab       num_tab;
1824      l_txn_qty_tab      num_tab;
1825      l_pri_qty_tab      num_tab;
1826      l_drop_order_tab   num_tab;
1827      l_sec_qty_tab      num_tab; --OPM Convergence
1828 
1829      l_lpn_id_tab                num_tab;
1830      l_inventory_item_id_tab     num_tab;
1831      l_transaction_uom_tab       uom_tab;
1832      l_revision_tab              rev_tab;
1833      l_lot_number_tab            lot_tab;
1834      l_dest_locator_tab          num_tab;
1835      l_bk_dl_detail_tab          num_tab;
1836      l_into_lpn_id_tab           num_tab;
1837      l_out_drop_order_tab        num_tab;
1838      l_show_message_tab           num_tab;
1839      l_error_code_tab   varchar240_tab;
1840      l_drop_lpn_option_tab num_tab;
1841      l_consolidation_method_id_tab num_tab;
1842 
1843      -- WIP related variables
1844      l_wip_entity_type_tab       num_tab;
1845      l_job_tab                   entity_tab;
1846      l_line_tab                  line_tab;
1847      l_dept_tab                  dept_tab;
1848      l_operation_seq_num_tab     num_tab;
1849      l_start_date_tab            date_tab;
1850      l_schedule_tab              entity_tab;
1851      l_assembly_tab              item_tab;
1852      l_wip_entity_id_tab         num_tab;
1853 
1854      l_return_status             VARCHAR2(30);
1855      l_msg_count                 NUMBER;
1856      l_msg_data                  VARCHAR2(10000);
1857 
1858      -- This cursor will group the all task rows where drop tpe = 'ID' based on the grouping criteria
1859      -- This will ignore the rows which are cross-docked to WIP Issue
1860      CURSOR c_group_item_drop_tasks_cursor IS
1861       SELECT MIN(ROWID)
1862             ,MIN(drop_order)
1863             ,SUM(transaction_quantity)
1864             ,SUM(primary_quantity)
1865             ,SUM(secondary_quantity) --OPM Convergence
1866             ,MAX(show_message)
1867 	    ,MAX(error_code)
1868 	    ,MAX(drop_lpn_option) --R12: This value should be unique per group
1869 	    ,MAX(consolidation_method_id) --R12: This value should be unique per group
1870 	FROM   WMS_PUTAWAY_GROUP_TASKS_GTMP
1871        WHERE  row_type = G_ROW_TP_ALL_TASK
1872               AND drop_type = G_DT_ITEM_DROP
1873               AND NOT (crossdock_type > 1
1874                        AND backorder_delivery_detail > 0
1875                        AND wip_supply_type = 1
1876                       )
1877        GROUP BY  lpn_id
1878                 ,inventory_item_id
1879                 ,transaction_uom
1880                 ,revision
1881                 ,lot_number
1882                 ,dest_locator
1883 	        ,into_lpn_id
1884 	        --,backorder_delivery_detail; --R12
1885                 ,decode(BACKORDER_DELIVERY_DETAIL,null,null,0,0,get_grouping(BACKORDER_DELIVERY_DETAIL)); --Added for bug 5286880
1886 
1887      -- This cursor will group the all task rows where drop tpe = 'ID' based on the grouping criteria
1888      -- This will consider only the rows which are cross-docked to WIP Issue
1889      CURSOR c_wip_item_drop_tasks_cursor IS
1890       SELECT MIN(ROWID)
1891             ,MIN(drop_order)
1892             ,SUM(transaction_quantity)
1893             ,SUM(primary_quantity)
1894             ,SUM(secondary_quantity) --OPM Convergence
1895 	    ,MAX(show_message)
1896 	    ,MAX(error_code)
1897 	    ,MAX(drop_lpn_option) --R12: This value should be unique per group
1898 	    ,MAX(consolidation_method_id) --R12: This value should be unique per group
1899 	FROM   WMS_PUTAWAY_GROUP_TASKS_GTMP
1900        WHERE  row_type = G_ROW_TP_ALL_TASK
1901               AND drop_type = G_DT_ITEM_DROP
1902               AND crossdock_type > 1
1903               AND backorder_delivery_detail > 0
1904               AND wip_supply_type = 1
1905        GROUP BY  lpn_id
1906                 ,inventory_item_id
1907                 ,transaction_uom
1908                 ,revision
1909                 ,lot_number
1910                 ,dest_locator
1911                 ,into_lpn_id
1912                 ,BACKORDER_DELIVERY_DETAIL
1913                 ,WIP_JOB
1914                 ,WIP_LINE
1915                 ,WIP_DEPT
1916                 ,WIP_OP_SEQ
1917                 ,WIP_ENTITY_TYPE
1918                 ,WIP_START_DATE
1919                 ,WIP_SCHEDULE
1920                 ,WIP_ASSEMBLY;
1921 
1922      CURSOR c_group_tasks_cursor IS
1923         SELECT lpn_id
1924               ,inventory_item_id
1925               ,transaction_uom
1926               ,revision
1927               ,lot_number
1928               ,dest_locator
1929               ,BACKORDER_DELIVERY_DETAIL
1930               ,into_lpn_id
1931               ,drop_order
1932               ,WIP_JOB
1933               ,WIP_LINE
1934               ,WIP_DEPT
1935               ,WIP_OP_SEQ
1936               ,WIP_ENTITY_TYPE
1937               ,WIP_START_DATE
1938               ,WIP_SCHEDULE
1939               ,WIP_ASSEMBLY
1940         FROM   WMS_PUTAWAY_GROUP_TASKS_GTMP
1941         WHERE  drop_type = G_DT_ITEM_DROP
1942                AND row_type = G_ROW_TP_GROUP_TASK;
1943 
1944    BEGIN
1945 
1946      IF (l_debug = 1) THEN
1947        DEBUG('Start of Function ', l_proc_name,9);
1948      END IF;
1949      l_progress := '100';
1950 
1951 
1952      -- Actual logic here
1953      -- Insert the group task rows -- first for the normal tasks
1954      --                               and then for tasks that are cross docked to wip issue
1955 
1956      OPEN c_group_item_drop_tasks_cursor;
1957      l_progress := '110';
1958 
1959      LOOP --c_group_item_drop_tasks_cursor
1960 
1961        EXIT WHEN c_group_item_drop_tasks_cursor%NOTFOUND;
1962        l_progress := '200';
1963 
1964        FETCH c_group_item_drop_tasks_cursor
1965         BULK COLLECT
1966          INTO  l_rowid_tab
1967               ,l_drop_order_tab
1968               ,l_txn_qty_tab
1969               ,l_pri_qty_tab
1970               ,l_sec_qty_tab --OPM Convergence
1971 	      ,l_show_message_tab        --R12
1972 	      ,l_error_code_tab          --R12
1973 	      ,l_drop_lpn_option_tab  --R12
1974               ,l_consolidation_method_id_tab --R12
1975          LIMIT l_limit;
1976        l_progress := '210';
1977 
1978       -- Insert group task row into temp table
1979        /* Note: OPM Convergence Not passing l_sec_qty_tabl to insert_group_tasks,
1980                 since this table will not be used in that function
1981         */
1982        l_cur_count := Insert_Group_Tasks (
1983               l_rowid_tab      => l_rowid_tab
1984              ,l_txn_qty_tab    => l_txn_qty_tab
1985              ,l_pri_qty_tab    => l_pri_qty_tab
1986              ,l_drop_order_tab => l_drop_order_tab
1987 	     ,l_show_message_tab=> l_show_message_tab  --R12
1988 	     ,l_error_code_tab  => l_error_code_tab    --R12
1989 	     ,l_drop_lpn_option_tab => l_drop_lpn_option_tab              --R12
1990              ,l_consolidation_method_id_tab => l_consolidation_method_id_tab);	       --R12
1991 
1992        l_progress := '230';
1993 
1994        -- Check for errors while inserting group task row
1995        IF l_cur_count = -1 THEN
1996          l_progress := '232';
1997          RAISE fnd_api.g_exc_unexpected_error;
1998        END IF;
1999 
2000        l_rec_count := l_rec_count + l_cur_count;
2001        l_progress := '235';
2002 
2003        IF (l_debug = 1) THEN
2004         DEBUG( l_cur_count|| ' Group task Row(s) inserted in WMS_PUTAWAY_GROUP_TASKS_GTMP '
2005               ,l_proc_name,9);
2006        END IF;
2007        l_progress := '240';
2008 
2009      END LOOP;--c_group_item_drop_tasks_cursor
2010 
2011      l_progress := '250';
2012      CLOSE c_group_item_drop_tasks_cursor;
2013      l_progress := '260';
2014 
2015 
2016      OPEN c_wip_item_drop_tasks_cursor;
2017      l_progress := '305';
2018 
2019      LOOP --c_wip_item_drop_tasks_cursor
2020 
2021        EXIT WHEN c_wip_item_drop_tasks_cursor%NOTFOUND;
2022        l_progress := '308';
2023 
2024        FETCH c_wip_item_drop_tasks_cursor
2025         BULK COLLECT
2026          INTO  l_rowid_tab
2027               ,l_drop_order_tab
2028               ,l_txn_qty_tab
2029               ,l_pri_qty_tab
2030 	      ,l_sec_qty_tab --OPM Convergence
2031 	      ,l_show_message_tab --R12
2032 	      ,l_error_code_tab --R12
2033 	      ,l_drop_lpn_option_tab  --R12
2034               ,l_consolidation_method_id_tab  --R12
2035          LIMIT l_limit;
2036        l_progress := '310';
2037 
2038       -- Reset the counter for cur tasks count
2039       l_cur_count := 0;
2040       l_progress := '320';
2041 
2042       -- Insert group task row into temp table
2043        l_cur_count := Insert_Group_Tasks (
2044               l_rowid_tab      => l_rowid_tab
2045              ,l_txn_qty_tab    => l_txn_qty_tab
2046              ,l_pri_qty_tab    => l_pri_qty_tab
2047              ,l_drop_order_tab => l_drop_order_tab
2048 	     ,l_show_message_tab=> l_show_message_tab
2049 	     ,l_error_code_tab  => l_error_code_tab
2050 	     ,l_drop_lpn_option_tab => l_drop_lpn_option_tab              --R12
2051              ,l_consolidation_method_id_tab => l_consolidation_method_id_tab);	       --R12
2052        l_progress := '330';
2053 
2054        -- Check for errors while inserting group task row
2055        IF l_cur_count = -1 THEN
2056          l_progress := '332';
2057          RAISE fnd_api.g_exc_unexpected_error;
2058        END IF;
2059 
2060        l_rec_count := l_rec_count + l_cur_count;
2061        l_progress := '335';
2062 
2063        IF (l_debug = 1) THEN
2064         DEBUG( l_cur_count|| ' Group task Row(s) inserted in WMS_PUTAWAY_GROUP_TASKS_GTMP for crossdocked to WIP ISSUE tasks'
2065               ,l_proc_name,9);
2066        END IF;
2067        l_progress := '340';
2068 
2069      END LOOP;--c_wip_item_drop_tasks_cursor
2070 
2071      l_progress := '350';
2072      CLOSE c_wip_item_drop_tasks_cursor;
2073      l_progress := '360';
2074 
2075      BEGIN -- This will create link between 'Group Task' which was inserteed above with its corresponding 'All Task' rows
2076 
2077          OPEN c_group_tasks_cursor;
2078          l_progress := '600';
2079 
2080          LOOP --c_group_tasks_cursor
2081 
2082              EXIT WHEN c_group_tasks_cursor%NOTFOUND;
2083              l_progress := '610';
2084 
2085              FETCH c_group_tasks_cursor
2086               BULK COLLECT
2087               INTO l_lpn_id_tab
2088                   ,l_inventory_item_id_tab
2089                   ,l_transaction_uom_tab
2090                   ,l_revision_tab
2091                   ,l_lot_number_tab
2092                   ,l_dest_locator_tab
2093                   ,l_bk_dl_detail_tab
2094                   ,l_into_lpn_id_tab
2095                   ,l_out_drop_order_tab
2096                   ,l_job_tab
2097                   ,l_line_tab
2098                   ,l_dept_tab
2099                   ,l_operation_seq_num_tab
2100                   ,l_wip_entity_type_tab
2101                   ,l_start_date_tab
2102                   ,l_schedule_tab
2103                   ,l_assembly_tab
2104                LIMIT l_limit;
2105              l_progress := '620';
2106 
2107 
2108              FORALL i IN 1 ..l_lpn_id_tab.COUNT
2109               UPDATE WMS_PUTAWAY_GROUP_TASKS_GTMP
2110                SET   drop_order = l_out_drop_order_tab(i)
2111                WHERE drop_type  = G_DT_ITEM_DROP
2112                      AND lpn_id                      = l_lpn_id_tab(i)
2113                      AND inventory_item_id           = l_inventory_item_id_tab(i)
2114                      AND transaction_uom             = l_transaction_uom_tab(i)
2115                      AND NVL(revision,'@@@')         = NVL(l_revision_tab(I), '@@@')
2116                      AND NVL(lot_number,'@@@')       = NVL(l_lot_number_tab(i),'@@@')
2117                      AND NVL(dest_locator,-999)      = NVL(l_dest_locator_tab(i),-999)
2118                      --AND NVL(BACKORDER_DELIVERY_DETAIL,-999)  = NVL(l_bk_dl_detail_tab(i),-999) --Commented for bug 5286880
2119                      AND Decode(BACKORDER_DELIVERY_DETAIL,NULL,-999,0,0,Decode(row_type,g_row_tp_all_task,get_grouping(backorder_delivery_detail),backorder_delivery_detail)) = NVL(l_bk_dl_detail_tab(i),-999)--bug12399417
2120                      AND NVL(into_lpn_id,-999)       = NVL(l_into_lpn_id_tab(i),-999)
2121                      AND NVL(wip_job,'@@@')          = NVL(l_job_tab(i),'@@@')
2122                      AND NVL(wip_line,'@@@')         = NVL(l_line_tab(i),'@@@')
2123                      AND NVL(wip_dept,'@@@')         = NVL(l_dept_tab(i),'@@@')
2124                      AND NVL(wip_op_seq,-999)        = NVL(l_operation_seq_num_tab(i),-999)
2125                      AND NVL(wip_entity_type,-999)   = NVL(l_wip_entity_type_tab(i),-999)
2126                      AND NVL(wip_start_date,SYSDATE) = NVL(l_start_date_tab(i),SYSDATE)
2127                      AND NVL(wip_schedule,'@@@')     = NVL(l_schedule_tab(i),'@@@')
2128                      AND NVL(wip_assembly,'@@@')     = NVL(l_assembly_tab(i),'@@@');
2129 
2130              l_progress := '640';
2131 
2132              l_rec_count := l_rec_count + sql%rowcount;
2133              IF (l_debug = 1) THEN
2134               DEBUG(sql%rowcount || ' Group task Row(s) updated in WMS_PUTAWAY_GROUP_TASKS_GTMP '
2135                     ,l_proc_name,9);
2136              END IF;
2137 
2138              l_progress := '650';
2139 
2140        END LOOP; --loop through all the group tasks
2141 
2142        CLOSE c_group_tasks_cursor;
2143        l_progress := '680';
2144 
2145 
2146      EXCEPTION
2147        WHEN OTHERS THEN
2148          RAISE fnd_api.g_exc_unexpected_error;
2149      END;
2150 
2151 
2152      IF (l_debug = 1) THEN
2153        DEBUG(' l_rec_count => '|| l_rec_count,l_proc_name,4);
2154        DEBUG(' Function Exited at ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
2155      END IF;
2156 
2157      l_progress := '1000';
2158      RETURN l_rec_count;
2159 
2160   EXCEPTION
2161      WHEN fnd_api.g_exc_unexpected_error THEN
2162        DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
2163        DEBUG(SQLERRM,l_proc_name,1);
2164 
2165        IF c_group_item_drop_tasks_cursor%isopen THEN
2166          CLOSE c_group_item_drop_tasks_cursor;
2167        END IF;
2168 
2169        IF c_group_tasks_cursor%isopen THEN
2170          CLOSE c_group_tasks_cursor;
2171        END IF;
2172 
2173        IF c_wip_item_drop_tasks_cursor%isopen THEN
2174          CLOSE c_wip_item_drop_tasks_cursor;
2175        END IF;
2176 
2177        RETURN -1;
2178 
2179 
2180      WHEN OTHERS THEN
2181        DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
2182        DEBUG(SQLERRM,l_proc_name,1);
2183 
2184        IF c_group_item_drop_tasks_cursor%isopen THEN
2185          CLOSE c_group_item_drop_tasks_cursor;
2186        END IF;
2187 
2188        IF c_group_tasks_cursor%isopen THEN
2189          CLOSE c_group_tasks_cursor;
2190        END IF;
2191 
2192        IF c_wip_item_drop_tasks_cursor%isopen THEN
2193          CLOSE c_wip_item_drop_tasks_cursor;
2194        END IF;
2195 
2196        RETURN -1;
2197    END Group_Item_Drop_Tasks;
2198 
2199 
2200 
2201 /**
2202   *   This function will group the tasks and will stamp the
2203   *   drop_type properly as item drop/consolidated
2204   *   drop('ID'/'CD'). The logic is
2205   *
2206   *   Loop through all the lpns starting from the innermost LPN
2207   *     If drop type = consolidated drop then
2208   *       Mark item drop if the contents go to diff location or
2209   *          has different to lpn.
2210   *       Mark item drop if there are some contents which
2211   *          doesn't have allocations (under allocation case)
2212   *
2213   *       If parent exists
2214   *           get drop_type, dest sub/loc of parent
2215   *
2216   *           if drop type = consolidated then
2217   *              if the dest sub/loc is null then
2218   *                 update it with childs dest sub/loc
2219   *              else if it is different from childs
2220   *                 mark item drop for the parent
2221   *
2222   *   This will return the number of rows processed as
2223   *   output.This will return -1 in case of failure.
2224 
2225   *  @ RETURN  NUMBER
2226 
2227 
2228 **/
2229    FUNCTION Populate_Drop_Type
2230      RETURN NUMBER
2231      IS
2232      l_debug            NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
2233      l_progress         VARCHAR2(10) := '10';
2234      l_proc_name        VARCHAR2(30) := 'Populate_Drop_Type:';
2235      l_rec_count        NUMBER       := 0;
2236 
2237      l_lpn_id_tab         num_tab;
2238      l_into_lpn_id_tab    num_tab;
2239      l_into_lpn_name_tab  lpn_name_tab;
2240      l_parent_lpn_id_tab  num_tab;
2241      l_dest_sub_tab       sub_name_tab;
2242      l_dest_loc_tab       num_tab;
2243      l_lpn_level_tab      num_tab;
2244      l_row_type_tab       row_type_tab;
2245      l_rowid_tab          rowid_tab;
2246 
2247      l_parent_dest_sub    VARCHAR2(10);
2248      l_parent_dest_loc    NUMBER;
2249      l_parent_drop_order  NUMBER;
2250      l_parent_into_lpn_id NUMBER;
2251      l_drop_order         NUMBER  := 0;
2252      l_parent_into_lpn_name VARCHAR2(30);
2253 
2254      l_rowid              urowid;
2255 
2256      l_loc_count          NUMBER  := 0;
2257      l_to_lpn_count       NUMBER  := 0;
2258      l_qty_disc_count     NUMBER  := 0;
2259 
2260 
2261      -- WIP related variables
2262 
2263      l_del_detail_id_count    NUMBER  := 0;
2264      l_wip_job_count          NUMBER  := 0;
2265      l_wip_line_count         NUMBER  := 0;
2266      l_wip_dept_count         NUMBER  := 0;
2267      l_wip_op_seq_count       NUMBER  := 0;
2268      l_wip_entity_type_count  NUMBER  := 0;
2269      l_wip_start_date_count   NUMBER  := 0;
2270      l_wip_schedule_count     NUMBER  := 0;
2271      l_wip_assembly_count     NUMBER  := 0;
2272 
2273      l_job                WIP_ENTITIES.WIP_ENTITY_NAME%TYPE;
2274      l_line               WIP_LINES.LINE_CODE%TYPE;
2275      l_dept               BOM_DEPARTMENTS.DEPARTMENT_CODE%TYPE;
2276      l_operation_seq_num  NUMBER;
2277      l_wip_entity_type    NUMBER;
2278      l_start_date         DATE;
2279      l_schedule           WIP_ENTITIES.WIP_ENTITY_NAME%TYPE;
2280      l_assembly           MTL_SYSTEM_ITEMS_KFV.CONCATENATED_SEGMENTS%TYPE;
2281      l_wip_entity_id      NUMBER;
2282 
2283      l_crossdock_type             NUMBER;
2284      l_backorder_delivery_detail  NUMBER;
2285      l_wip_supply_type            NUMBER;
2286 
2287      l_crossdock_type_tab        num_tab;
2288      l_bk_dl_detail_tab          num_tab;
2289      l_wip_supply_type_tab       num_tab;
2290      l_wip_entity_type_tab       num_tab;
2291      l_job_tab                   entity_tab;
2292      l_line_tab                  line_tab;
2293      l_dept_tab                  dept_tab;
2294      l_operation_seq_num_tab     num_tab;
2295      l_start_date_tab            date_tab;
2296      l_schedule_tab              entity_tab;
2297      l_assembly_tab              item_tab;
2298      l_wip_entity_id_tab         num_tab;
2299 
2300      l_drop_order_tab        num_tab;
2301      l_outermost_lpn_id_tab  num_tab;
2302 
2303      --BUG 3435079: Used to retrieve WLPN info
2304      l_wpgtt_lpn_id_tab              lpn_name_tab;
2305 
2306      l_lpn_name_tab            lpn_name_tab;
2307      l_parent_lpn_name_tab     lpn_name_tab;
2308      l_outermost_lpn_name_tab  lpn_name_tab;
2309 
2310      --Bug 5286880
2311      l_grouping_count NUMBER;
2312      l_count NUMBER;
2313 
2314 
2315      -- This cursor will select all the levels in the temp table
2316      -- For which the drop type is 'CD'
2317      CURSOR c_lpn_level_cursor IS
2318        SELECT DISTINCT
2319               lpn_level
2320        FROM   wms_putaway_group_tasks_gtmp
2321        WHERE  drop_type = G_DT_CONSOLIDATED_DROP
2322        ORDER BY lpn_level DESC;
2323 
2324      -- This cursor will get all the LPNs, their dest sub/loc and its parent lpn id where drop_type is CD
2325      -- For the level passed
2326      CURSOR c_lpn_cursor(v_lpn_level NUMBER) IS
2327         SELECT DISTINCT
2328                lpn_id
2329               ,parent_lpn_id
2330               ,dest_subinventory
2331               ,dest_locator
2332               ,into_lpn_id
2333               ,into_lpn_name
2334               ,row_type
2335               ,crossdock_type
2336               ,backorder_delivery_detail
2337               ,wip_supply_type
2338               ,WIP_JOB
2339               ,WIP_LINE
2340               ,WIP_DEPT
2341               ,WIP_OP_SEQ
2342               ,WIP_ENTITY_TYPE
2343               ,WIP_START_DATE
2344               ,WIP_SCHEDULE
2345               ,WIP_ASSEMBLY
2346         FROM   WMS_PUTAWAY_GROUP_TASKS_GTMP
2347         WHERE  drop_type = G_DT_CONSOLIDATED_DROP
2348                AND lpn_level = v_lpn_level;
2349 
2350      -- This cursor will get the rowid, dest sub/loc for the lpn passed where drop type is CD
2351      CURSOR c_parent_lpn_cursor(v_parent_lpn_id NUMBER) IS
2352         SELECT   ROWID
2353                 ,dest_subinventory
2354                 ,dest_locator
2355                 ,into_lpn_id
2356                 ,into_lpn_name
2357                 ,crossdock_type
2358                 ,backorder_delivery_detail
2359                 ,wip_supply_type
2360                 ,WIP_JOB
2361                 ,WIP_LINE
2362                 ,WIP_DEPT
2363                 ,WIP_OP_SEQ
2364                 ,WIP_ENTITY_TYPE
2365                 ,WIP_START_DATE
2366                 ,WIP_SCHEDULE
2367                 ,WIP_ASSEMBLY
2368         FROM   WMS_PUTAWAY_GROUP_TASKS_GTMP
2369         WHERE  lpn_id = v_parent_lpn_id
2370                AND drop_type = G_DT_CONSOLIDATED_DROP
2371                AND ROWNUM < 2;
2372 
2373      -- This cursor will get the tasks in proper order and will also be used to resolve
2374      -- the LPN columns.
2375 
2376      --BUG 3435079: For performance reason, do not join with WLPN here,
2377      --because the 'order by' clauses forces the DB to do a full table
2378      --scan of WLPN.  Retrieval of WLPN info will be done later.
2379      CURSOR c_group_tasks_order_by IS
2380             SELECT wpgtt.ROWID
2381 	           ,ROWNUM
2382 	           ,wpgtt.lpn_id               lpn_id
2383             FROM    wms_putaway_group_tasks_gtmp wpgtt
2384             ORDER BY wpgtt.sub_dropping_order,
2385                      wpgtt.dest_subinventory,
2386                      wpgtt.loc_dropping_order,
2387                      wpgtt.LOCATOR,
2388                      wpgtt.inventory_item_id,
2389                      wpgtt.revision,
2390                      wpgtt.lot_number;
2391 
2392 
2393    BEGIN
2394      IF (l_debug = 1) THEN
2395        DEBUG('Start of Function ', l_proc_name,9);
2396      END IF;
2397 
2398       BEGIN --Block to stamp the drop_order
2399 
2400         IF (l_debug = 1) THEN
2401           DEBUG('Stamping the drop_order and resolving the lpn columns ',l_proc_name,9);
2402         END IF;
2403 
2404          l_progress := '20';
2405          OPEN c_group_tasks_order_by;
2406          l_progress := '30';
2407 
2408           LOOP -- c_group_tasks_order_by cursor
2409 
2410               EXIT WHEN c_group_tasks_order_by%NOTFOUND;
2411 
2412               l_progress := '40';
2413               FETCH c_group_tasks_order_by
2414                 BULK COLLECT
2415                 INTO  l_rowid_tab
2416                      ,l_drop_order_tab
2417 		     ,l_wpgtt_lpn_id_tab
2418                 LIMIT l_limit;
2419               l_progress := '50';
2420 
2421                  --Stamp the drop_order in the temp table
2422                  FOR i IN 1..l_rowid_tab.COUNT
2423                  LOOP
2424 
2425                    -- Increment the drop order
2426                    l_drop_order := l_drop_order + 1;
2427                    l_progress   := '60';
2428 
2429 		   --BUG 3435079: For performance reason, get the
2430 		   --LPN info here, instead of getting it in the
2431 		   --c_group_tasks_order_by cursor
2432 		   BEGIN
2433 		      SELECT
2434 			wlpn1.license_plate_number   lpn_name
2435 			,wlpn2.lpn_id               parent_lpn_id
2436 			,wlpn2.license_plate_number parent_lpn_name
2437 			,wlpn3.lpn_id               outermost_lpn_id
2438 			,wlpn3.license_plate_number outermost_lpn_name
2439 			INTO
2440 			l_lpn_name_tab(i)
2441 			,l_parent_lpn_id_tab(i)
2442 			,l_parent_lpn_name_tab(i)
2443 			,l_outermost_lpn_id_tab(i)
2444 			,l_outermost_lpn_name_tab(i)
2445 			FROM
2446 			wms_license_plate_numbers wlpn1
2447 			,wms_license_plate_numbers wlpn2
2448 			,wms_license_plate_numbers wlpn3
2449 			WHERE
2450 			wlpn1.lpn_id = l_wpgtt_lpn_id_tab(i)
2451 			AND wlpn1.outermost_lpn_id = wlpn3.lpn_id
2452 			AND wlpn2.lpn_id(+) = wlpn1.parent_lpn_id;
2453 		   EXCEPTION
2454 		      WHEN too_many_rows THEN
2455 			 IF (l_debug = 1) THEN
2456 			    DEBUG('Too many rows returned when getting LPN, parent lpn, outermost lpn info' || l_parent_lpn_id_tab(i),l_proc_name,9);
2457 			 END IF;
2458 		      WHEN no_data_found THEN
2459 			 IF (l_debug = 1) THEN
2460 			    DEBUG('No rows returned when getting LPN, parent lpn, outermost lpn info' || l_parent_lpn_id_tab(i),l_proc_name,9);
2461 			 END IF;
2462 		      WHEN OTHERS THEN
2463 			 IF (l_debug = 1) THEN
2464 			    DEBUG('Other exception occurred when getting LPN, parent lpn, outermost lpn info' || l_parent_lpn_id_tab(i),l_proc_name,9);
2465 			 END IF;
2466 		   END;
2467 
2468                    -- update the drop order and LPN columns back to temp table
2469                    UPDATE wms_putaway_group_tasks_gtmp
2470                    SET    drop_order          = l_drop_order
2471                          ,lpn_name            = l_lpn_name_tab(i)
2472                          ,parent_lpn_id       = l_parent_lpn_id_tab(i)
2473                          ,parent_lpn_name     = l_parent_lpn_name_tab(i)
2474                          ,outermost_lpn_id    = l_outermost_lpn_id_tab(i)
2475                          ,outermost_lpn_name  = l_outermost_lpn_name_tab(i)
2476                    WHERE  ROWID = l_rowid_tab(i);
2477                    l_progress := '65';
2478                  END LOOP;
2479 
2480                  l_progress := '70';
2481 
2482            END LOOP; -- c_group_tasks_order_by cursor
2483 
2484            CLOSE c_group_tasks_order_by;
2485            l_progress := '75';
2486 
2487       EXCEPTION
2488            WHEN OTHERS THEN
2489               DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
2490               DEBUG(SQLERRM,l_proc_name,1);
2491 
2492               IF  c_group_tasks_order_by%isopen THEN
2493                 CLOSE c_group_tasks_order_by;
2494               END IF;
2495 
2496               RETURN -1;
2497       END; --Block to stamp the drop_order
2498 
2499 
2500       OPEN c_lpn_level_cursor;
2501 
2502       FETCH c_lpn_level_cursor
2503         BULK COLLECT INTO
2504         l_lpn_level_tab;
2505 
2506       CLOSE c_lpn_level_cursor;
2507 
2508 
2509       FOR i IN 1 .. l_lpn_level_tab.COUNT
2510       LOOP --lpn level cursor
2511 
2512              l_progress := '100';
2513              OPEN c_lpn_cursor(l_lpn_level_tab(i));
2514 
2515              l_progress := '110';
2516 
2517              LOOP --c_lpn_cursor loop
2518 
2519 
2520                EXIT WHEN c_lpn_cursor%NOTFOUND;
2521                l_progress := '120';
2522 
2523                l_progress := '130';
2524                FETCH c_lpn_cursor
2525                 BULK COLLECT
2526                 INTO l_lpn_id_tab
2527                     ,l_parent_lpn_id_tab
2528                     ,l_dest_sub_tab
2529                     ,l_dest_loc_tab
2530                     ,l_into_lpn_id_tab
2531                     ,l_into_lpn_name_tab
2532                     ,l_row_type_tab
2533                     ,l_crossdock_type_tab
2534                     ,l_bk_dl_detail_tab
2535                     ,l_wip_supply_type_tab
2536                     ,l_job_tab
2537                     ,l_line_tab
2538                     ,l_dept_tab
2539                     ,l_operation_seq_num_tab
2540                     ,l_wip_entity_type_tab
2541                     ,l_start_date_tab
2542                     ,l_schedule_tab
2543                     ,l_assembly_tab
2544                  LIMIT l_limit;
2545                l_progress := '140';
2546 
2547 
2548                FOR i IN 1 .. l_lpn_id_tab.COUNT
2549                LOOP --lpn_cursor_bulk collect loop
2550 
2551                -- Do all the checks only if the drop type is consolidated drop
2552                --IF l_drop_type = G_DT_CONSOLIDATED_DROP THEN
2553 
2554                    -- Check whether all the contents go to the same location
2555                    -- and whether the suggested to_lpn is same for all the contents within that LPN
2556                    BEGIN -- Check for destination locator/LPN
2557 
2558                      --Don't do these checks for a dummy LPN record since it won't have any contents, MTRL or MMTT
2559                      IF l_row_type_tab(i) <> G_ROW_TP_LPN_TASK THEN --dummy record check
2560 
2561                        IF (l_debug = 1) THEN
2562                          DEBUG('Checking whether CD is possible (check for dest loc/LPN Discrepancy) for LPN ' || l_lpn_id_tab(i),l_proc_name,9);
2563                        END IF;
2564 
2565                        l_progress := 145;
2566                        SELECT  COUNT(DISTINCT NVL(transfer_to_location,locator_id))
2567                               ,COUNT(DISTINCT cartonization_id)
2568                        INTO  l_loc_count
2569                             ,l_to_lpn_count
2570                        FROM  mtl_material_transactions_temp  mmtt
2571                             ,mtl_txn_request_lines mtrl
2572                        WHERE  mmtt.move_order_line_id = mtrl.line_id
2573                               AND mtrl.lpn_id = l_lpn_id_tab(i);
2574                        l_progress := '150';
2575 
2576                        --Begin bug 5286880
2577                        l_count := populate_grouping_rows(l_lpn_id_tab(i));
2578                        l_grouping_count := get_grouping_count(g_grouping_rows);
2579                        IF (l_debug = 1) THEN
2580                                 debug('l_grouping_count='||l_grouping_count, l_proc_name, 9);
2581                        END IF;
2582                        --End bug 5286880
2583 
2584 
2585                        -- If there are no suggestions. This case maynot be possible
2586                        -- but still checking this for safety
2587                        IF l_loc_count = 0 THEN
2588                           l_progress := '160';
2589 
2590                           IF (l_debug = 1) THEN
2591                             DEBUG('There are no MMTTs for this LPN and hence erroring out ' || l_lpn_id_tab(i),l_proc_name,9);
2592                           END IF;
2593 
2594                           RETURN -1;
2595                        END IF;
2596 
2597                        --IF l_loc_count > 1 OR l_to_lpn_count > 1 THEN
2598                        --Commented for bug 5286880 and added one more condition for l_grouping_count
2599                        IF l_loc_count > 1 OR l_to_lpn_count > 1 OR l_grouping_count > 1 THEN
2600 
2601                          IF (l_debug = 1) THEN
2602                            DEBUG('Either locator or LPN Discrepancy. Hence calling Mark Item Drop for LPN ' || l_lpn_id_tab(i),l_proc_name,9);
2603                          END IF;
2604 
2605                          l_progress := '170';
2606 
2607                          -- The contents either doesn't go to same location or same TO LPN
2608                          -- and hence can't be dropped by consolidated drop. So mark item drop
2609                          -- for the lpn and its parents.
2610                          l_rec_count := l_rec_count + Mark_Item_Drop(l_lpn_id_tab(i));
2611 
2612                          l_progress := '180';
2613 
2614 
2615                          IF l_rec_count = -1 THEN
2616                            l_progress := '190';
2617 
2618                            IF (l_debug = 1) THEN
2619                              DEBUG('Mark Item Drop Returned Error and hence Exiting.. ',l_proc_name,9);
2620                            END IF;
2621 
2622                            RETURN -1;
2623                          END IF;
2624 
2625                        END IF; --loc/to_lpn count check
2626                      END IF; --dummy record check
2627                    EXCEPTION
2628                      WHEN OTHERS THEN
2629                        DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
2630                        DEBUG(SQLERRM,l_proc_name,1);
2631                        RETURN -1;
2632                    END; -- Check for destination locator/LPN
2633 
2634 
2635                    -- Check whether there are any other contents in that LPN
2636                    -- which don't have MMTT or allocations is less.
2637                    -- In this case we can't do a consolidated drop but have to do an item drop.
2638                    BEGIN -- Check for other contents/under allocation of the LPN
2639 
2640                      BEGIN
2641                        l_progress := '200';
2642 
2643                        --Don't do these checks for a dummy LPN record since it won't have any contents, MTRL or MMTT
2644                        IF l_row_type_tab(i) <> G_ROW_TP_LPN_TASK THEN --dummy record check
2645 
2646                          IF (l_debug = 1) THEN
2647                            DEBUG('Checking whether CD is possible (check qty disc) for LPN ' || l_lpn_id_tab(i),l_proc_name,9);
2648                          END IF;
2649 
2650                          -- This SQL will check whether there are any incomplete allocations for the LPn passed
2651                          -- Incomplete allocation may be partial allocation or no allocation.
2652                          l_progress := '205';
2653 
2654                            SELECT 1
2655                            INTO l_qty_disc_count
2656                              FROM dual
2657                              WHERE EXISTS (
2658                                  SELECT mtrl.line_id
2659                                  FROM   mtl_txn_request_lines mtrl
2660                                         ,mtl_txn_request_headers mtrh
2661                                  WHERE  1 = 1
2662                                         AND mtrl.header_id = mtrh.header_id
2663                                         AND mtrh.move_order_type = 6
2664                                         AND mtrl.lpn_id = l_lpn_id_tab(i)
2665 					AND mtrl.line_status = 7 --BUG 3352572
2666 					AND ((NOT exists
2667                                                 (SELECT 1
2668                                                  FROM mtl_material_transactions_temp mmtt1
2669                                                  WHERE mtrl.line_status = 7
2670                                                  AND mmtt1.move_order_line_id = mtrl.line_id)
2671                                                ) OR
2672                                               (( (NVL(quantity,0) - NVL(quantity_delivered,0) ) )
2673                                                 > (SELECT SUM(transaction_quantity)
2674                                                    FROM   mtl_material_transactions_temp mmtt
2675                                                    WHERE  mtrl.line_status = 7
2676                                                    AND mmtt.move_order_line_id = mtrl.line_id
2677                                                    ))
2678                                               )
2679                                           );
2680 
2681 
2682                           l_progress := '210';
2683                        END IF;--dummy record check
2684                      EXCEPTION
2685                        WHEN no_data_found THEN
2686                          l_qty_disc_count :=0;
2687                          l_progress := '215';
2688                      END;
2689 
2690 
2691                       IF l_qty_disc_count = 1 THEN
2692                           -- reset the flag
2693                           l_qty_disc_count := 0;
2694 
2695                           IF (l_debug = 1) THEN
2696                             DEBUG('Quantity Discrepancy and hence calling Mark Item Drop for LPN ' || l_lpn_id_tab(i),l_proc_name,9);
2697                           END IF;
2698 
2699 
2700                          l_progress := '220';
2701 
2702                           -- The contents haven't been allocated fully
2703                          -- and hence can't be dropped by consolidated drop. So mark item drop
2704                          -- for the lpn and its parents.
2705                          l_rec_count := l_rec_count + Mark_Item_Drop(l_lpn_id_tab(i));
2706                          l_progress := '230';
2707 
2708                       END IF;
2709 
2710                    EXCEPTION
2711                      WHEN OTHERS THEN
2712                        DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
2713                        DEBUG(SQLERRM,l_proc_name,1);
2714                        RETURN -1;
2715                    END; -- Check for other contents/under allocation of the LPN
2716 
2717 
2718                    BEGIN --wip_integration
2719 
2720                       -- Get the count of crossdocks for all those rows which crossdocked to wip issue
2721                       -- for the current lpn being processed
2722                       l_progress := '250';
2723                       SELECT  COUNT(DISTINCT BACKORDER_DELIVERY_DETAIL)
2724                              ,COUNT(DISTINCT WIP_JOB)
2725                              ,COUNT(DISTINCT WIP_LINE)
2726                              ,COUNT(DISTINCT WIP_DEPT)
2727                              ,COUNT(DISTINCT WIP_OP_SEQ)
2728                              ,COUNT(DISTINCT WIP_ENTITY_TYPE)
2729                              ,COUNT(DISTINCT WIP_START_DATE)
2730                              ,COUNT(DISTINCT WIP_SCHEDULE)
2731                              ,COUNT(DISTINCT WIP_ASSEMBLY)
2732                        INTO  l_del_detail_id_count
2733                             ,l_wip_job_count
2734                             ,l_wip_line_count
2735                             ,l_wip_dept_count
2736                             ,l_wip_op_seq_count
2737                             ,l_wip_entity_type_count
2738                             ,l_wip_start_date_count
2739                             ,l_wip_schedule_count
2740                             ,l_wip_assembly_count
2741                       FROM   WMS_PUTAWAY_GROUP_TASKS_GTMP
2742                       WHERE  row_type = G_ROW_TP_ALL_TASK
2743                              AND crossdock_type > 1
2744                              AND backorder_delivery_detail > 0
2745                              AND wip_supply_type = 1
2746                              AND lpn_id = l_lpn_id_tab(i);
2747 
2748                       l_progress := '260';
2749 
2750                       -- Check for consolidation drop based on number of crossdocks
2751                       IF(l_del_detail_id_count > 1 OR
2752                          l_wip_job_count > 1 OR
2753                          l_wip_line_count > 1 OR
2754                          l_wip_dept_count > 1 OR
2755                          l_wip_op_seq_count > 1 OR
2756                          l_wip_entity_type_count > 1 OR
2757                          l_wip_start_date_count > 1 OR
2758                          l_wip_schedule_count > 1 OR
2759                          l_wip_assembly_count > 1
2760                         ) THEN
2761 
2762                         -- Crossdocked to diff job/schedule/assembly/.... hence it is item drop
2763                         l_progress := '265';
2764 
2765                         IF (l_debug = 1) THEN
2766                           DEBUG('Cross docked to different job/schedule/op seq/assmebly... and hence calling Mark Item Drop for LPN ' || l_lpn_id_tab(i),l_proc_name,9);
2767                         END IF;
2768 
2769                         l_rec_count := l_rec_count + Mark_Item_Drop(l_lpn_id_tab(i));
2770                         l_progress := '270';
2771 
2772                       END IF; -- same job details check
2773 
2774                    EXCEPTION
2775                      WHEN OTHERS THEN
2776                        DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
2777                        DEBUG(SQLERRM,l_proc_name,1);
2778                        RETURN -1;
2779                    END; --wip_integration
2780 
2781 
2782                    -- Get the parent record
2783                    BEGIN
2784                       l_progress := '300';
2785 
2786                       IF l_parent_lpn_id_tab(i) IS NOT NULL THEN
2787 
2788                         l_progress := '310';
2789                         -- Get the details of the parent lpn record
2790                         OPEN  c_parent_lpn_cursor(l_parent_lpn_id_tab(i));
2791                         l_progress := '320';
2792 
2793                         FETCH  c_parent_lpn_cursor
2794                          INTO  l_rowid
2795                               ,l_parent_dest_sub
2796                               ,l_parent_dest_loc
2797                               ,l_parent_into_lpn_id
2798                               ,l_parent_into_lpn_name
2799                               ,l_crossdock_type
2800                               ,l_backorder_delivery_detail
2801                               ,l_wip_supply_type
2802                               ,l_job
2803                               ,l_line
2804                               ,l_dept
2805                               ,l_operation_seq_num
2806                               ,l_wip_entity_type
2807                               ,l_start_date
2808                               ,l_schedule
2809                               ,l_assembly;
2810                          l_progress := '330';
2811 
2812                          CLOSE c_parent_lpn_cursor;
2813 
2814                          -- If there is no parent, check that case and we should not exit out of the loop
2815                          -- We should proceed with the next record and hence the exit is commented out.
2816                          --EXIT WHEN c_parent_lpn_cursor%NOTFOUND;
2817 
2818                          -- If the parent record exists
2819                          IF l_rowid IS NOT NULL THEN
2820                            l_progress := '340';
2821 
2822                            -- Check if the dest locator is null or into LPN is null for the parent
2823                            -- Parent dest loc can be null only if it is a dummy row in that case even the into lpn id will be null
2824                            -- checking it again for safety
2825                            -- wip_supply_type <> 1 check was added to ensure that you select the dummy rows alone
2826                            -- for LPNs which are crossdocked to wip issue (since dest_loc_id and dest_lpn_id can be null for this case)
2827                            IF ( (l_parent_dest_loc IS NULL)    AND
2828                                 (l_parent_into_lpn_id IS NULL) AND
2829                                 (l_wip_supply_type <> 1) ) THEN
2830                              l_progress := '350';
2831 
2832                              -- Dest sub/loc of the parent is null and hence update the dest sub/loc with that of childs
2833                              UPDATE  WMS_PUTAWAY_GROUP_TASKS_GTMP
2834                               SET    dest_subinventory = l_dest_sub_tab(i)
2835                                     ,dest_locator      = l_dest_loc_tab(i)
2836                                     ,into_lpn_id       = l_into_lpn_id_tab(i)
2837                                     ,into_lpn_name     = l_into_lpn_name_tab(i)
2838                                     ,crossdock_type             = l_crossdock_type_tab(i)
2839                                     ,backorder_delivery_detail  = l_bk_dl_detail_tab(i)
2840                                     ,wip_supply_type            = l_wip_supply_type_tab(i)
2841                                     ,WIP_JOB                    = l_job_tab(i)
2842                                     ,WIP_LINE                   = l_line_tab(i)
2843                                     ,WIP_DEPT                   = l_dept_tab(i)
2844                                     ,WIP_OP_SEQ                 = l_operation_seq_num_tab(i)
2845                                     ,WIP_ENTITY_TYPE            = l_wip_entity_type_tab(i)
2846                                     ,WIP_START_DATE             = l_start_date_tab(i)
2847                                     ,WIP_SCHEDULE               = l_schedule_tab(i)
2848                                     ,WIP_ASSEMBLY               = l_assembly_tab(i)
2849                               WHERE  ROWID = l_rowid;
2850                              l_progress := '360';
2851 
2852                              IF (l_debug = 1) THEN
2853                                DEBUG('Parent Dest loc for LPN ' || l_lpn_id_tab(i) || ' updated with dest_locator ' || l_dest_loc_tab(i) ,l_proc_name,9);
2854                                DEBUG('Parent Dest LPN for LPN ' || l_lpn_id_tab(i) || ' updated with into_lpn     ' || l_into_lpn_id_tab(i) ,l_proc_name,9);
2855                                DEBUG('Parent crossdock_type for LPN            ' || l_lpn_id_tab(i) || ' updated with ' || l_crossdock_type_tab(i) ,l_proc_name,9);
2856                                DEBUG('Parent backorder_delivery_detail for LPN ' || l_lpn_id_tab(i) || ' updated with ' || l_bk_dl_detail_tab(i) ,l_proc_name,9);
2857                                DEBUG('Parent wip_supply_type for LPN           ' || l_lpn_id_tab(i) || ' updated with ' || l_wip_supply_type_tab(i) ,l_proc_name,9);
2858                                DEBUG('Parent WIP_JOB for LPN         ' || l_lpn_id_tab(i) || ' updated with ' || l_job_tab(i) ,l_proc_name,9);
2859                                DEBUG('Parent WIP_LINE for LPN        ' || l_lpn_id_tab(i) || ' updated with ' || l_line_tab(i) ,l_proc_name,9);
2860                                DEBUG('Parent WIP_DEPT for LPN        ' || l_lpn_id_tab(i) || ' updated with ' || l_dept_tab(i) ,l_proc_name,9);
2861                                DEBUG('Parent WIP_OP_SEQ for LPN      ' || l_lpn_id_tab(i) || ' updated with ' || l_operation_seq_num_tab(i) ,l_proc_name,9);
2862                                DEBUG('Parent WIP_ENTITY_TYPE for LPN ' || l_lpn_id_tab(i) || ' updated with ' || l_wip_entity_type_tab(i) ,l_proc_name,9);
2863                                DEBUG('Parent WIP_START_DATE for LPN  ' || l_lpn_id_tab(i) || ' updated with ' || l_start_date_tab(i) ,l_proc_name,9);
2864                                DEBUG('Parent WIP_SCHEDULE for LPN    ' || l_lpn_id_tab(i) || ' updated with ' || l_schedule_tab(i) ,l_proc_name,9);
2865                                DEBUG('Parent WIP_ASSEMBLY for LPN    ' || l_lpn_id_tab(i) || ' updated with ' || l_assembly_tab(i) ,l_proc_name,9);
2866                              END IF;
2867 
2868 
2869                            -- Parent record has dest locator/into LPN
2870                            ELSE
2871                              l_progress := '370';
2872 
2873                              -- Check the dest loc/into lpn of parent with the child
2874                               IF ( (l_parent_dest_loc <> l_dest_loc_tab(i)) OR
2875                                    (NVL(l_parent_into_lpn_id,-999) <> NVL(l_into_lpn_id_tab(i),-999) ) ) THEN
2876                                 l_progress := '380';
2877 
2878                                 IF (l_debug = 1) THEN
2879                                   DEBUG('Loc/Into LPN mismatch with the parent and hence calling Mark Item Drop for parent ' || l_parent_lpn_id_tab(i),l_proc_name,9);
2880                                 END IF;
2881 
2882                                  -- Dest loc / Into LPN of parent is not equal that of child and hence
2883                                  -- can't be dropped by consolidated drop. So mark item drop for the Parent.
2884 
2885                                 l_rec_count := l_rec_count + Mark_Item_Drop(l_parent_lpn_id_tab(i));
2886 
2887                                 l_progress := '390';
2888 
2889                               END IF; -- Dest loc check with child
2890 
2891 
2892                               -- wip_integration
2893                               -- WIP Crossdock check (do only if it is crossdocked to wip for a push type
2894                               IF ( l_crossdock_type_tab(i)  > 1  AND
2895                                    l_bk_dl_detail_tab(i)    > 0  AND
2896                                    l_wip_supply_type_tab(i) = 1
2897                                  ) THEN
2898 
2899                                 l_progress := '400';
2900 
2901                                 --Check if parent goes to same job/line/schedule etc.., as that of the child
2902                                 IF ( l_crossdock_type <> l_crossdock_type_tab(i) OR
2903                                      l_backorder_delivery_detail <> l_bk_dl_detail_tab(i) OR
2904                                      l_wip_supply_type <> l_wip_supply_type_tab(i) OR
2905                                      l_job  <> l_job_tab(i) OR
2906                                      l_line <> l_line_tab(i) OR
2907                                      l_dept <> l_dept_tab(i) OR
2908                                      l_operation_seq_num <> l_operation_seq_num_tab(i) OR
2909                                      l_wip_entity_type   <> l_wip_entity_type_tab(i) OR
2910                                      l_start_date <> l_start_date_tab(i) OR
2911                                      l_schedule   <> l_schedule_tab(i) OR
2912                                      l_assembly   <> l_assembly_tab(i)
2913                                    ) THEN
2914                                     -- Dest loc / Into LPN of parent is not equal that of child and hence
2915                                     -- can't be dropped by consolidated drop. So mark item drop for the Parent.
2916                                     l_progress := '410';
2917 
2918                                     IF (l_debug = 1) THEN
2919                                       DEBUG('WIP Details mismatch with the parent and hence calling Mark Item Drop for parent LPN ' || l_parent_lpn_id_tab(i),l_proc_name,9);
2920                                     END IF;
2921 
2922                                     l_rec_count := l_rec_count + Mark_Item_Drop(l_parent_lpn_id_tab(i));
2923                                     l_progress := '420';
2924                                 END IF; --check for parents job/line/schedule etc..,
2925 
2926                               END IF; --WIP Crosdock check
2927 
2928 
2929                            END IF;-- dest loc / into LPN not null check
2930 
2931                          END IF; -- l_rowid not null check
2932 
2933 
2934                       END IF; -- parent_lpn_id not null check
2935                       l_progress := '400';
2936 
2937                    EXCEPTION
2938                      WHEN OTHERS THEN
2939                       DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
2940                       DEBUG(SQLERRM,l_proc_name,1);
2941 
2942                       IF c_parent_lpn_cursor%isopen THEN
2943                         CLOSE c_parent_lpn_cursor;
2944                       END IF;
2945 
2946                       IF c_lpn_cursor%isopen THEN
2947                         CLOSE c_lpn_cursor;
2948                       END IF;
2949 
2950                       RETURN -1;
2951                    END;
2952 
2953         --       END IF; -- drop type check
2954 
2955                END LOOP; --lpn_cursor_bulk collect loop
2956 
2957 
2958              END LOOP; --c_lpn_cursor loop
2959 
2960              CLOSE c_lpn_cursor;
2961 
2962       END LOOP;--lpn level cursor
2963 
2964 
2965       --R12: Revert MDC suggestions for all tasks that is flagged as item drop
2966       BEGIN
2967 	 UPDATE mtl_material_transactions_temp
2968 	   SET  cartonization_id = NULL
2969 	   WHERE transaction_temp_id IN (SELECT gtmp.transaction_temp_id
2970 					 FROM wms_putaway_group_tasks_gtmp gtmp
2971 					 WHERE gtmp.consolidation_method_id = 1 --???MDC
2972 					 AND gtmp.drop_type = g_dt_item_drop);
2973 
2974 	 IF (l_debug = 1) THEN
2975 	    DEBUG('Num of MMTT rows updated:' ||SQL%rowcount,l_proc_name,4);
2976 	 END IF;
2977 
2978 	 UPDATE wms_putaway_group_tasks_gtmp
2979 	   SET  consolidation_method_id = 0
2980 	   WHERE consolidation_method_id = 1
2981 	   AND   drop_type = g_dt_item_drop;
2982 
2983 	 IF (l_debug = 1) THEN
2984 	    DEBUG('Num of GTMP rows updated:' ||SQL%rowcount,l_proc_name,4);
2985 	 END IF;
2986 
2987       EXCEPTION
2988         WHEN OTHERS THEN
2989 	   IF (l_debug = 1) THEN
2990 	      DEBUG('ERROR REVERTING MDC SUGGESTIONS',l_proc_name,4);
2991 	   END IF;
2992 	   RAISE fnd_api.g_exc_unexpected_error;
2993       END;
2994       --R12 END
2995 
2996      l_progress := '500';
2997      IF (l_debug = 1) THEN
2998        DEBUG(' l_rec_count => '|| l_rec_count,l_proc_name,4);
2999        DEBUG(' Function Exited at ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
3000      END IF;
3001      l_progress := '510';
3002 
3003      IF c_parent_lpn_cursor%isopen THEN
3004         CLOSE c_parent_lpn_cursor;
3005      END IF;
3006      l_progress := '515';
3007 
3008      IF c_lpn_cursor%isopen THEN
3009         CLOSE c_lpn_cursor;
3010      END IF;
3011      l_progress := '520';
3012 
3013      -- Return the number of rows populated as item drop in total
3014      RETURN l_rec_count;
3015 
3016     EXCEPTION
3017      WHEN OTHERS THEN
3018        DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
3019        DEBUG(SQLERRM,l_proc_name,1);
3020 
3021        IF c_parent_lpn_cursor%isopen THEN
3022          CLOSE c_parent_lpn_cursor;
3023        END IF;
3024 
3025        IF c_lpn_cursor%isopen THEN
3026          CLOSE c_lpn_cursor;
3027        END IF;
3028 
3029        RETURN -1;
3030      END Populate_Drop_Type;
3031 
3032 
3033 
3034 
3035 /**
3036   *   This function will populate the global temporary table
3037   *   WMS_PUTAWAY_GROUP_TASKS_GTMP with the required data and
3038   *   will return the count of rows in the temporary table as
3039   *   output.
3040 
3041   *   This method will also insert the dummy records for those
3042   *   LPN which doesnt have contents
3043 
3044   *   Drop all scenario is also taken care while inserting
3045   *   For drop all case, we will always start with the outermost lpn id.
3046   *   because we should be suggesting the consolidated lpn wherever possible
3047 
3048   *  @param  p_org_id    Organization ID
3049   *  @param  p_drop_type Drop Type
3050   *  @param  p_emp_id    Employee ID
3051   *  @param  p_lpn_id    LPN ID
3052   *  @ RETURN  NUMBER
3053 
3054 
3055 **/
3056    FUNCTION Populate_Group_Tasks(
3057                   p_org_id          IN NUMBER,
3058                   p_drop_type       IN VARCHAR2,
3059                   p_emp_id          IN NUMBER,
3060                   p_lpn_id          IN NUMBER,
3061           p_item_drop_flag  IN VARCHAR2)
3062      RETURN NUMBER
3063      IS
3064       l_debug     NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
3065       l_progress  VARCHAR2(10) := '10';
3066       l_proc_name VARCHAR2(30) := 'Populate_Group_Tasks:';
3067       l_rec_count NUMBER       := 0;
3068 
3069       l_rowid_tab            rowid_tab;
3070       l_tempid_tab           num_tab;
3071       l_orgid_tab            num_tab;
3072       l_parent_lpn_id_tab    num_tab;
3073 
3074       l_lpn_id_tab              num_tab;
3075       l_lpn_level_tab           num_tab;
3076       l_lpn_context_tab         num_tab;
3077 
3078       l_has_contents  NUMBER := 0;
3079 
3080       -- WIP related variables
3081       l_xdock_type_tab            num_tab;
3082       l_wip_supply_type_tab       num_tab;
3083       l_wip_entity_type_tab       num_tab;
3084       l_job_tab                   entity_tab;
3085       l_line_tab                  line_tab;
3086       l_dept_tab                  dept_tab;
3087       l_operation_seq_num_tab     num_tab;
3088       l_start_date_tab            date_tab;
3089       l_schedule_tab              entity_tab;
3090       l_assembly_tab              item_tab;
3091       l_wip_entity_id_tab         num_tab;
3092 
3093       l_return_status         VARCHAR2(30);
3094       l_msg_count             NUMBER;
3095       l_msg_data              VARCHAR2(10000);
3096 
3097       l_error_code            NUMBER;
3098       l_prim_qty              NUMBER;
3099       l_inspection_flag       NUMBER;
3100       l_load_flag             NUMBER;
3101       l_drop_flag             NUMBER;
3102       l_crossdock_flag        NUMBER;
3103       l_load_prim_quantity    NUMBER;
3104       l_inspect_prim_quantity NUMBER;
3105       l_drop_prim_quantity    NUMBER;
3106       l_xdock_prim_quantity   NUMBER;
3107       b_iscrossdocked         BOOLEAN := FALSE;
3108 
3109       -- This cursor will fetch all the lpns starting with the LPN passed
3110       -- For drop all, this will get all the LPNs which the user has loaded
3111       -- This cursor will also select the dummy LPN rows (lpn without contents)
3112       -- and also the level of the LPN
3113       --For bug10354084, i split the CURSOR c_all_lpn_cursor into two to improve performance
3114       /* bug10354084 */
3115       CURSOR c_nodropall_lpn_cursor IS
3116         SELECT lpn_id
3117               ,parent_lpn_id
3118               ,LEVEL
3119               ,lpn_context
3120         FROM wms_license_plate_numbers wln
3121 	START WITH LPN_ID = p_lpn_id
3122 	CONNECT BY PRIOR wln.lpn_id = wln.parent_lpn_id;
3123 
3124        CURSOR c_dropall_lpn_cursor IS
3125          SELECT lpn_id
3126               ,parent_lpn_id
3127               ,LEVEL
3128               ,lpn_context
3129         FROM wms_license_plate_numbers wln
3130         START WITH LPN_ID IN (
3131                               SELECT DISTINCT wln2.outermost_lpn_id
3132                               FROM   wms_dispatched_tasks     wdt
3133 			            ,mtl_txn_request_lines    mtrl
3134 			            ,mtl_material_transactions_temp mmtt
3135 			            ,mtl_txn_request_headers  mtrh
3136                                     ,wms_license_plate_numbers wln2
3137                               WHERE mtrh.header_id = mtrl.header_id
3138                                     AND mtrh.move_order_type = 6
3139                                     AND mtrl.line_status = 7
3140 			            AND wdt.transaction_temp_id = mmtt.transaction_temp_id
3141 			            AND mmtt.move_order_line_id = mtrl.line_id
3142                                     AND wdt.STATUS = 4
3143                                     AND wdt.person_id = p_emp_id
3144                                     AND wln2.lpn_id = mtrl.lpn_id
3145                               )
3146       CONNECT BY PRIOR wln.lpn_id = wln.parent_lpn_id;
3147       /* bug10354084 */
3148 
3149       -- This cursor will get the temp ids of all the MMTTs in the temp table
3150       -- which are crossdocked to WIP Issue.
3151       CURSOR c_back_ordered_cursor IS
3152         SELECT ROWID
3153               ,transaction_temp_id
3154               ,organization_id
3155               ,Nvl(crossdock_type,0)
3156               ,Nvl(wip_supply_type,0)
3157    FROM  WMS_PUTAWAY_GROUP_TASKS_GTMP
3158         WHERE  1 = 1
3159                AND row_type = G_ROW_TP_ALL_TASK
3160                AND crossdock_type > 1
3161                AND backorder_delivery_detail > 0;
3162                --AND wip_supply_type = 1;
3163 
3164    BEGIN
3165      --Printing the input parameters
3166      IF (l_debug = 1) THEN
3167       DEBUG(' Function Entered at ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
3168       DEBUG(' p_org_id    => '||p_org_id,l_proc_name,4);
3169       DEBUG(' p_drop_type => '||p_drop_type,l_proc_name,4);
3170       DEBUG(' p_emp_id    => '||p_emp_id,l_proc_name,4);
3171       DEBUG(' p_lpn_id    => '||p_lpn_id,l_proc_name,4);
3172      END IF;
3173 
3174      l_progress := '15';
3175 
3176      BEGIN --Block to insert the 'All Task' rows
3177 
3178         /* bug10354084 */
3179 	IF p_drop_type = G_DT_DROP_ALL THEN
3180 	    OPEN c_dropall_lpn_cursor;
3181 	    l_progress := '20';
3182 	  ELSE
3183 	    OPEN c_nodropall_lpn_cursor;
3184 	    l_progress := '21';
3185 	 END IF;
3186 
3187 	    LOOP
3188 	       IF p_drop_type = G_DT_DROP_ALL THEN
3189 		  EXIT WHEN c_dropall_lpn_cursor%NOTFOUND;
3190 		  FETCH c_dropall_lpn_cursor
3191 		  BULK COLLECT
3192 		    INTO  l_lpn_id_tab
3193 		    ,l_parent_lpn_id_tab
3194 		    ,l_lpn_level_tab
3195 		    ,l_lpn_context_tab
3196 		    LIMIT l_limit;
3197 		  l_progress := '25';
3198 		ELSE
3199 		  EXIT WHEN c_nodropall_lpn_cursor%NOTFOUND;
3200 		  FETCH c_nodropall_lpn_cursor
3201 		    BULK COLLECT
3202 		    INTO  l_lpn_id_tab
3203 		    ,l_parent_lpn_id_tab
3204 		    ,l_lpn_level_tab
3205 		    ,l_lpn_context_tab
3206 		    LIMIT l_limit;
3207 		  l_progress := '26';
3208 		    END IF;
3209 	  /* bug10354084 */
3210 
3211 	FOR i IN 1..l_lpn_id_tab.COUNT
3212          LOOP -- all LPNs loop
3213            l_progress := '27';
3214            BEGIN -- Check Whether contents exists for this lpn
3215              l_progress := '30';
3216              SELECT 1
3217              INTO   l_has_contents
3218              FROM   wms_lpn_contents
3219              WHERE  parent_lpn_id = l_lpn_id_tab(i)
3220                     AND ROWNUM < 2;
3221              l_progress := '31';
3222 
3223            EXCEPTION
3224              WHEN no_data_found THEN
3225                l_progress := '33';
3226                l_has_contents := 0;
3227              WHEN OTHERS THEN
3228                l_progress := '35';
3229                RAISE fnd_api.g_exc_unexpected_error;
3230            END;-- Check Whether contents exists for this lpn
3231 
3232            --Check for contents
3233            IF l_has_contents = 0 THEN
3234              l_progress := '40';
3235 
3236              -- Don't insert dummy rows for all item drops
3237              -- In case of all item drop the consolidated lpn will be same as that of the lpn and
3238              -- drop type will be ID so we don't need dummy rows.
3239              IF p_item_drop_flag <> 'Y' THEN
3240 
3241                  l_progress := '45';
3242                  -- LPN don't have contents, so insert a dummy row
3243                  INSERT INTO WMS_PUTAWAY_GROUP_TASKS_GTMP
3244                    (
3245                            ORGANIZATION_ID
3246                           ,LPN_ID
3247                           ,PARENT_LPN_ID
3248                           ,DROP_TYPE
3249                           ,ROW_TYPE
3250                           ,LPN_LEVEL
3251                           ,LPN_CONTEXT
3252                           ,WIP_SUPPLY_TYPE
3253                    )
3254                  VALUES
3255                    (
3256                            p_org_id
3257                           ,l_lpn_id_tab(i)
3258                           ,l_parent_lpn_id_tab(i)
3259                           ,G_DT_CONSOLIDATED_DROP
3260                           ,G_ROW_TP_LPN_TASK
3261                           ,l_lpn_level_tab(i)
3262                           ,l_lpn_context_tab(I)
3263                           ,0
3264                    );
3265                  l_progress := '46';
3266 
3267                  IF (l_debug = 1) THEN
3268                    DEBUG('Inserted dummy row for lpn id ' || l_lpn_id_tab(i),l_proc_name,9);
3269                  END IF;
3270 
3271              END IF;
3272 
3273 
3274            ELSIF l_has_contents = 1 THEN
3275              -- LPN has contents so query from MTRL,WDT and insert into temp table
3276 
3277               l_progress := '50';
3278               --Data has to be ordered in the temp table later before processing, because ORDER BY during insert is not supported
3279               INSERT INTO WMS_PUTAWAY_GROUP_TASKS_GTMP
3280                 (
3281                         ORGANIZATION_ID
3282                        ,TRANSACTION_TEMP_ID
3283                        ,TRANSACTION_HEADER_ID
3284                        ,LPN_ID
3285                        ,LPN_NAME
3286                        ,LPN_CONTEXT
3287                        ,PARENT_LPN_ID
3288                        ,PARENT_LPN_NAME
3289                        ,OUTERMOST_LPN_ID
3290                        ,OUTERMOST_LPN_NAME
3291                        ,CONSOLIDATED_LPN_ID
3292                        ,CONSOLIDATED_LPN_NAME
3293                        ,INTO_LPN_ID
3294                        ,INTO_LPN_NAME
3295                        ,DROP_TYPE
3296                        ,DROP_ORDER
3297                        ,INVENTORY_ITEM_ID
3298                        ,ITEM
3299                        ,GROUP_ID
3300                        ,LOT_NUMBER
3301                        ,REVISION
3302                        ,TRANSACTION_QUANTITY
3303                        ,TRANSACTION_UOM
3304                        ,LOCATOR
3305                        ,PRIMARY_UOM_CODE
3306                        ,DEST_SUBINVENTORY
3307                        ,DEST_LOCATOR
3308                        ,REVISION_QTY_CONTROL_CODE
3309                        ,LOT_CONTROL_CODE
3310                        ,SERIAL_NUMBER_CONTROL_CODE
3311                        ,RESTRICT_SUBINVENTORIES_CODE
3312                        ,RESTRICT_LOCATORS_CODE
3313                        ,LOCATION_CONTROL_CODE
3314                        ,ALLOWED_UNITS_LOOKUP_CODE
3315                        ,BACKORDER_DELIVERY_DETAIL
3316                        ,CROSSDOCK_TYPE
3317                        ,WIP_SUPPLY_TYPE
3318                        ,FROM_SUBINVENTORY
3319                        ,FROM_LOCATOR
3320                        ,TRANSFER_SUBINVENTORY
3321                        ,TRANSFER_TO_LOCATION
3322                        ,TRANSFER_ORGANIZATION
3323                        ,TRANSACTION_ACTION_ID
3324                        ,REFERENCE
3325                        ,LOC_DROPPING_ORDER
3326                        ,SUB_DROPPING_ORDER
3327                        ,ROW_TYPE
3328                        ,MOVE_ORDER_LINE_ID
3329                        ,PROJECT_ID
3330                        ,TASK_ID
3331                        ,TXN_SOURCE_ID
3332                        ,PRIMARY_QUANTITY
3333                        ,LPN_LEVEL
3334                        ,SECONDARY_QUANTITY --OPM Convergence
3335                        ,SECONDARY_UOM --OPM Convergence
3336 		       ,show_message --R12
3337 		       ,error_code --R12
3338 		       ,error_explanation --R12
3339 		)
3340 
3341                   SELECT DISTINCT
3342                         mmtt.organization_id,
3343                         mmtt.transaction_temp_id,
3344                         mmtt.transaction_header_id,
3345                         mtrl.lpn_id,
3346                         NULL,                   --lpn_name
3347                         l_lpn_context_tab(i),
3348                         --TO_NUMBER(NULL),        --Parent LPN ID
3349                         l_parent_lpn_id_tab(i), --Parent LPN ID
3350                         NULL,                   --Parent LPN Name
3351                         TO_NUMBER(NULL),        --Outermost LPN ID
3352                         NULL,                   --Outermost LPN Name
3353                         TO_NUMBER(NULL),        --Consolidated LPN ID
3354                         NULL,                   --Consolidated LPN Name
3355                         mmtt.cartonization_id,  --INTO LPN ID
3356                         NULL,                   --INTO LPN Name
3357                         decode(p_item_drop_flag,'Y',G_DT_ITEM_DROP,G_DT_CONSOLIDATED_DROP),                   --Drop Type
3358                         ROWNUM,                 --Drop Order
3359                         mmtt.inventory_item_id,
3360                         msik.concatenated_segments item, --Item
3361                         TO_NUMBER(NULL),        --Group ID
3362                         mtrl.lot_number,
3363                         mmtt.revision,
3364                         mmtt.transaction_quantity,
3365                         mmtt.transaction_uom,
3366                         NULL LOCATOR, --inv_project.get_locsegs (milk.inventory_location_id, milk.organization_id ) LOCATOR,
3367                         msik.primary_uom_code,
3368                         --DECODE (transaction_action_id, 2, mmtt.transfer_subinventory,mmtt.subinventory_code) dest_subinventory,
3369                         --DECODE (transaction_action_id, 2, mmtt.transfer_to_location, mmtt.locator_id) dest_locator,
3370                         NVL(mmtt.transfer_subinventory,mmtt.subinventory_code) dest_subinventory,
3371                         NVL(mmtt.transfer_to_location, mmtt.locator_id)        dest_locator,
3372                         NVL (msik.revision_qty_control_code, 1),
3373                         NVL (msik.lot_control_code, 1),
3374                         NVL (msik.serial_number_control_code, 1),
3375                         NVL (msik.restrict_subinventories_code, 2),
3376                         NVL (msik.restrict_locators_code, 2),
3377                         NVL (msik.location_control_code, 1),
3378                         NVL (msik.allowed_units_lookup_code, 2),
3379                         NVL (mtrl.backorder_delivery_detail_id, 0),
3380                         NVL (mtrl.crossdock_type, 0),
3381                         NVL (mmtt.wip_supply_type, 0),
3382                         mmtt.subinventory_code from_subinventory,
3383                         mmtt.locator_id from_locator,
3384                         mmtt.transfer_subinventory,
3385                         mmtt.transfer_to_location,
3386                         mmtt.transfer_organization,
3387                         mmtt.transaction_action_id,
3388                         mtrl.REFERENCE,
3389                         to_number(null), -- milk.dropping_order,
3390                         to_number(NULL), -- msi.dropping_order,
3391                         G_ROW_TP_ALL_TASK,
3392                         mtrl.line_id,
3393                         mtrl.project_id,
3394                         mtrl.task_id,
3395                         mtrl.txn_source_id,
3396                         mmtt.primary_quantity,
3397                         l_lpn_level_tab(i),
3398                         mmtt.secondary_transaction_quantity, --OPM Convergence
3399                         mmtt.secondary_uom_code, --OPM Convergence
3400 
3401 		        --R12: Change Management: If suggested loc = wlpn.loc, should show iser wanring message
3402 		        Decode(wln.locator_id,NVL(mmtt.transfer_to_location, mmtt.locator_id),1,0) show_message,
3403 		        mmtt.error_code,
3404 		        mmtt.error_explanation
3405 		        --R12 End
3406 		   FROM mtl_material_transactions_temp mmtt
3407                         ,mtl_txn_request_lines mtrl
3408                         ,wms_dispatched_tasks wdt
3409                         --,mtl_item_locations milk
3410                         ,mtl_system_items_kfv msik
3411                         ,mtl_txn_request_headers mtrh
3412                         ,wms_license_plate_numbers wln
3413                         --,mtl_secondary_inventories msi
3414                   WHERE wdt.organization_id = p_org_id
3415                 -- kajain
3416                 -- added the decode since a lpn loaded by someone
3417                 -- should be eligible to be dropped by some other user
3418                 -- also added a check to make sure that wdt.status = 4
3419                         AND wdt.person_id = Decode(p_drop_type, g_dt_drop_all,p_emp_id,wdt.person_id)
3420                         AND wdt.status = 4
3421                         AND wdt.task_type   = 2
3422                         AND wdt.transaction_temp_id = mmtt.transaction_temp_id
3423                         AND mmtt.move_order_line_id = mtrl.line_id
3424                         AND NVL (mmtt.wms_task_type, 0) <> -1
3425                         AND mtrl.lpn_id = l_lpn_id_tab(i)
3426                         AND wln.organization_id = p_org_id
3427                         AND mtrl.lpn_id    = wln.lpn_id
3428                         AND mtrl.header_id = mtrh.header_id
3429                         AND mtrh.move_order_type = 6
3430                         --AND NVL(mmtt.transfer_to_location,mmtt.locator_id) = milk.inventory_location_id(+)
3431                         --AND msi.organization_id(+) = mmtt.organization_id
3432                         --AND NVL(mmtt.transfer_subinventory,mmtt.subinventory_code)  = msi.secondary_inventory_name(+)
3433                         --AND mmtt.organization_id = milk.organization_id(+)
3434                         AND mmtt.organization_id = msik.organization_id
3435                         AND mmtt.inventory_item_id = msik.inventory_item_id;
3436 
3437               l_progress := '51';
3438 
3439 
3440               IF (l_debug = 1) THEN
3441                 DEBUG('Inserted ' || SQL%rowcount || ' all task rows for lpn id ' || l_lpn_id_tab(i),l_proc_name,9);
3442               END IF;
3443 
3444            END IF;
3445            --Check for contents
3446 
3447          END LOOP; -- all LPNs loop
3448 
3449        END LOOP; --for  c_nodropall_lpn_cursor/c_dropall_lpn_cursor
3450 
3451        /* bug10354084 */
3452        IF c_nodropall_lpn_cursor%ISOPEN THEN
3453          l_progress := '60';
3454          CLOSE c_nodropall_lpn_cursor;
3455          l_progress := '61';
3456        END IF;
3457 
3458        IF c_dropall_lpn_cursor%ISOPEN THEN
3459          l_progress := '62';
3460          CLOSE c_dropall_lpn_cursor;
3461          l_progress := '63';
3462        END IF;
3463        /* bug10354084 */
3464 
3465 
3466      EXCEPTION
3467        WHEN OTHERS THEN
3468          l_progress := '90';
3469          RAISE fnd_api.g_exc_unexpected_error;
3470      END; --Block to insert the 'All Task' rows
3471 
3472 
3473       BEGIN -- WIP Integration
3474 
3475         l_progress := '300';
3476 
3477         IF (l_debug = 1) THEN
3478           DEBUG('Checking whether WIP Crossdock is possible ' ,l_proc_name,9);
3479         END IF;
3480 
3481         OPEN c_back_ordered_cursor;
3482         l_progress := '310';
3483 
3484         FETCH c_back_ordered_cursor
3485           BULK COLLECT
3486           INTO l_rowid_tab
3487               ,l_tempid_tab
3488               ,l_orgid_tab
3489               ,l_xdock_type_tab
3490               ,l_wip_supply_type_tab;
3491         l_progress := '320';
3492 
3493         CLOSE c_back_ordered_cursor;
3494         l_progress := '330';
3495 
3496         FOR i IN 1 .. l_rowid_tab.COUNT
3497         LOOP -- x-dock info loop
3498 
3499           -- Call Validate_Operation for this MMTT to check whetehr the current step is crossdock.
3500           -- Call the WIP Integration APIs only if the current step is crossdock
3501           -- Else reset the WIP Related columns in the temp table.
3502           -- If this is any other xdock reset the xdock columns as well
3503           wms_atf_runtime_pub_apis.validate_operation
3504           ( x_return_status         => l_return_status
3505            ,x_msg_data              => l_msg_data
3506            ,x_msg_count             => l_msg_count
3507            ,x_error_code            => l_error_code
3508            ,x_inspection_flag       => l_inspection_flag
3509            ,x_load_flag             => l_load_flag
3510            ,x_drop_flag             => l_drop_flag
3511            ,x_crossdock_flag        => l_crossdock_flag
3512            ,x_load_prim_quantity    => l_load_prim_quantity
3513            ,x_drop_prim_quantity    => l_drop_prim_quantity
3514            ,x_inspect_prim_quantity => l_inspect_prim_quantity
3515            ,x_crossdock_prim_quantity => l_xdock_prim_quantity
3516            ,p_source_task_id        => l_tempid_tab(i)
3517            ,p_move_order_line_id    => NULL
3518            ,p_inventory_item_id     => NULL
3519            ,p_lpn_id                => NULL
3520            ,p_activity_type_id      => G_OP_ACTIVITY_INBOUND
3521            ,p_organization_id       => l_orgid_tab(i));
3522 
3523           l_progress := '331';
3524           IF (l_return_status <> fnd_api.g_ret_sts_success) THEN
3525             IF (l_debug = 1) THEN
3526                DEBUG(' Validate Operation failed for MMTT = ' || l_tempid_tab(i) || ' hence erroring out' ,l_proc_name,1);
3527                DEBUG(' Error = ' || l_msg_data,l_proc_name,1);
3528             END IF;
3529             --RAISE fnd_api.g_exc_error;
3530             RETURN -1;
3531           ELSE
3532               -- Successful execution. Check whether this step if crossdocked/not
3533              IF (l_debug = 1) THEN
3534                 debug(' l_crossdock_flag is : ' || l_crossdock_flag,l_proc_name,1);
3535              END IF;
3536 
3537              IF (l_crossdock_flag = 1) THEN
3538                  b_iscrossdocked := FALSE;
3539              ELSE
3540                  b_iscrossdocked := TRUE;
3541              END IF;
3542 
3543           END IF;
3544           l_progress := '332';
3545 
3546           IF (l_debug = 1) THEN
3547              DEBUG(' API for temp_id  ' || l_tempid_tab(i) ,l_proc_name,9);
3548           END IF;
3549 
3550           IF (b_iscrossdocked) THEN --iscrossdocked
3551 
3552              l_progress := '332.1';
3553              IF (l_debug = 1) THEN
3554                 DEBUG('XDOCK TYPE: ' || l_xdock_type_tab(i) ,l_proc_name,9);
3555                 DEBUG('WIP_SUPPLY_TYPE: ' || l_wip_supply_type_tab(i) ,l_proc_name,9);
3556                 DEBUG('TEMP_ID: ' || l_tempid_tab(i) ,l_proc_name,9);
3557              END IF;
3558 
3559              IF l_xdock_type_tab(i) = 2 AND l_wip_supply_type_tab(i) = 1 THEN
3560                 IF (l_debug = 1) THEN
3561                   DEBUG('Calling WIP Integration API for temp_id  ' || l_tempid_tab(i) ,l_proc_name,9);
3562                 END IF;
3563                 l_progress := '333';
3564 
3565                 wms_wip_integration.get_wip_info_for_putaway
3566                   (
3567                                  p_temp_id            => l_tempid_tab(i)
3568                                 ,x_wip_entity_type    => l_wip_entity_type_tab(i)
3569                                 ,x_job                => l_job_tab(i)
3570                                 ,x_line               => l_line_tab(i)
3571                                 ,x_dept               => l_dept_tab(i)
3572                                 ,x_operation_seq_num  => l_operation_seq_num_tab(i)
3573                                 ,x_start_date         => l_start_date_tab(i)
3574                                 ,x_schedule           => l_schedule_tab(i)
3575                                 ,x_assembly           => l_assembly_tab(i)
3576                                 ,x_wip_entity_id      => l_wip_entity_id_tab(i)
3577                                 ,x_return_status      => l_return_status
3578                                 ,x_msg_count          => l_msg_count
3579                                 ,x_msg_data           => l_msg_data
3580                                );
3581                 l_progress := '334';
3582 
3583                 IF (l_return_status <> fnd_api.g_ret_sts_success) THEN
3584                    IF (l_debug = 1) THEN
3585                       DEBUG('WIP info for putaway failed for MMTT = ' || l_tempid_tab(i) ,l_proc_name,1);
3586                       DEBUG('Error = ' || l_msg_data,l_proc_name,1);
3587                    END IF;
3588                    --RAISE fnd_api.g_exc_error;
3589                    RETURN -1;
3590                    l_progress := '340';
3591                 END IF;
3592 	     /*If the supply type is 0, and is crossdocked, it is an OPM batch*/
3593             -- bug 9706800 start
3594           ELSIF l_xdock_type_tab(i) = 2 AND l_wip_supply_type_tab(i) = 0 THEN
3595             IF (l_debug             = 1) THEN
3596               DEBUG('Setting WIP Entity Type for temp_id  ' || l_tempid_tab(i) ,l_proc_name,9);
3597             END IF;
3598             l_progress               := '341';
3599             l_wip_entity_type_tab(i) := 10; -- OPM Batch
3600             IF (l_debug               = 1) THEN
3601               DEBUG('WIP Entity Type = ' || l_wip_entity_type_tab(i) ,l_proc_name,1);
3602             END IF;
3603 
3604             IF (l_debug = 1) THEN
3605               DEBUG('Updating WMS_PUTAWAY_GROUP_TASKS_GTMP with WIP_ENTITY_TYPE and BACKORDER_DELIVERY_DETAIL. Temp id is -  ' || l_tempid_tab(i) ,l_proc_name,9);
3606             END IF;
3607             -- Update back the OPM Batch details in to the temp table
3608              UPDATE WMS_PUTAWAY_GROUP_TASKS_GTMP
3609             SET WIP_ENTITY_TYPE         = l_wip_entity_type_tab(i),
3610               BACKORDER_DELIVERY_DETAIL = l_wip_entity_type_tab(i)
3611               WHERE transaction_temp_id = l_tempid_tab(i);
3612 
3613             	l_job_tab(i)               := NULL;
3614             	l_line_tab(i)              := NULL;
3615             	l_dept_tab(i)              := NULL;
3616             	l_operation_seq_num_tab(i) := NULL;
3617             	l_wip_entity_type_tab(i)   := NULL;
3618             	l_start_date_tab(i)        := NULL;
3619             	l_schedule_tab(i)          := NULL;
3620             	l_assembly_tab(i)          := NULL;
3621             -- bug 9706800 end
3622              ELSE
3623                --not crossdocked to WIP Issue
3624                 l_wip_entity_type_tab(i)   := 0;
3625                 l_job_tab(i)               := NULL;
3626                 l_line_tab(i)              := NULL;
3627                 l_dept_tab(i)              := NULL;
3628                 l_operation_seq_num_tab(i) := NULL;
3629                 l_wip_entity_type_tab(i)   := NULL;
3630                 l_start_date_tab(i)        := NULL;
3631                 l_schedule_tab(i)          := NULL;
3632                 l_assembly_tab(i)          := NULL;
3633              END IF;
3634           ELSE
3635             -- The current step is not crossdock for this operation.
3636             -- so reset the crossdock related columns in the temp table.
3637             l_progress := '350';
3638             IF (l_debug = 1) THEN
3639               DEBUG('Resetting the xdock related cols for temp_id  ' || l_tempid_tab(i) ,l_proc_name,9);
3640             END IF;
3641 
3642             UPDATE  WMS_PUTAWAY_GROUP_TASKS_GTMP
3643               SET   wip_supply_type = 0
3644                    ,backorder_delivery_detail = 0
3645                    ,crossdock_type = 0
3646               WHERE transaction_temp_id = l_tempid_tab(i);
3647             l_progress := '355';
3648 
3649             -- Setting these wip related columns explicitly to null
3650             -- so that the a row is created in the pl/sql table for that rowid
3651             -- and hence the bulk update below won't fail.
3652             l_wip_entity_type_tab(i)   := 0;
3653             l_job_tab(i)               := NULL;
3654             l_line_tab(i)              := NULL;
3655             l_dept_tab(i)              := NULL;
3656             l_operation_seq_num_tab(i) := NULL;
3657             l_wip_entity_type_tab(i)   := NULL;
3658             l_start_date_tab(i)        := NULL;
3659             l_schedule_tab(i)          := NULL;
3660             l_assembly_tab(i)          := NULL;
3661 
3662             l_progress := '360';
3663 
3664           END IF; --iscrossdocked
3665 
3666         END LOOP; -- WIP info loop
3667 
3668         l_progress := '370';
3669 
3670         -- Update back these details into the temp table
3671         FORALL i IN 1 .. l_rowid_tab.COUNT
3672           UPDATE WMS_PUTAWAY_GROUP_TASKS_GTMP
3673           SET    WIP_JOB  = l_job_tab(i)
3674                 ,WIP_LINE = l_line_tab(i)
3675                 ,WIP_DEPT = l_dept_tab(i)
3676                 ,WIP_OP_SEQ      = l_operation_seq_num_tab(i)
3677                 ,WIP_ENTITY_TYPE = l_wip_entity_type_tab(i)
3678                 ,WIP_START_DATE  = l_start_date_tab(i)
3679                 ,WIP_SCHEDULE    = l_schedule_tab(i)
3680                 ,WIP_ASSEMBLY    = l_assembly_tab(i)
3681                 ,BACKORDER_DELIVERY_DETAIL = l_wip_entity_type_tab(i)
3682           WHERE  ROWID = l_rowid_tab(i)
3683           AND Nvl(crossdock_type,1) = 2
3684           AND wip_supply_type = 1;
3685 
3686           l_progress := '380';
3687 
3688         IF (l_debug = 1) THEN
3689           DEBUG('After updating the WIP related for ' || l_rowid_tab.COUNT || ' row(s) ' ,l_proc_name,9);
3690         END IF;
3691         l_progress := '390';
3692 
3693       EXCEPTION
3694         WHEN OTHERS THEN
3695           RAISE fnd_api.g_exc_unexpected_error;
3696       END; -- WIP Integration
3697 
3698 
3699       IF (l_debug = 1) THEN
3700         DEBUG(' Before calling pvt function get_row_count ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,9);
3701       END IF;
3702 
3703       l_progress := '500';
3704       --Calling get_row_count to get the number of rows in global temp table
3705       l_rec_count := get_row_count(p_mode => 2);
3706       l_progress := '510';
3707 
3708       IF (l_debug = 1) THEN
3709         DEBUG(' After calling pvt function get_row_count ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,9);
3710         DEBUG(' l_rec_count => '|| l_rec_count,l_proc_name,4);
3711       END IF;
3712 
3713 
3714       l_progress := '900';
3715       IF (l_debug = 1) THEN
3716        DEBUG(' l_rec_count => '|| l_rec_count,l_proc_name,4);
3717        DEBUG(' Function Exited at ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
3718       END IF;
3719       l_progress := '910';
3720 
3721      -- Return the number of rows in the temp table after populate
3722      RETURN l_rec_count;
3723 
3724 
3725    EXCEPTION
3726      WHEN fnd_api.g_exc_error THEN
3727        DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
3728        DEBUG(SQLERRM,l_proc_name,1);
3729 
3730        /* bug10354084 */
3731        IF c_nodropall_lpn_cursor%ISOPEN THEN
3732          CLOSE c_nodropall_lpn_cursor;
3733        END IF;
3734 
3735        IF c_dropall_lpn_cursor%ISOPEN THEN
3736          CLOSE c_dropall_lpn_cursor;
3737        END IF;
3738        /* bug10354084 */
3739 
3740        IF c_back_ordered_cursor%ISOPEN THEN
3741         CLOSE c_back_ordered_cursor;
3742        END IF;
3743 
3744        RETURN -1;
3745 
3746 
3747      WHEN fnd_api.g_exc_unexpected_error THEN
3748        DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
3749        DEBUG(SQLERRM,l_proc_name,1);
3750 
3751        /* bug10354084 */
3752        IF c_nodropall_lpn_cursor%ISOPEN THEN
3753          CLOSE c_nodropall_lpn_cursor;
3754        END IF;
3755 
3756        IF c_dropall_lpn_cursor%ISOPEN THEN
3757          CLOSE c_dropall_lpn_cursor;
3758        END IF;
3759        /* bug10354084 */
3760 
3761        IF c_back_ordered_cursor%ISOPEN THEN
3762         CLOSE c_back_ordered_cursor;
3763        END IF;
3764 
3765        RETURN -1;
3766 
3767 
3768       WHEN OTHERS THEN
3769         DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
3770         DEBUG(SQLERRM,l_proc_name,1);
3771 
3772         /* bug10354084 */
3773         IF c_nodropall_lpn_cursor%ISOPEN THEN
3774           CLOSE c_nodropall_lpn_cursor;
3775         END IF;
3776 
3777 	IF c_dropall_lpn_cursor%ISOPEN THEN
3778           CLOSE c_dropall_lpn_cursor;
3779         END IF;
3780 	/* bug10354084 */
3781 
3782         IF c_back_ordered_cursor%ISOPEN THEN
3783          CLOSE c_back_ordered_cursor;
3784         END IF;
3785 
3786         RETURN -1;
3787      END Populate_group_tasks;
3788 
3789 
3790 /**
3791   *   This function will call the ATF API to activate the
3792   *   operation instance for load. It will call the ATF API
3793   *   wms_atf_runtime_pub_apis.activate_operation_instance for
3794   *   each MMTT. For the immediate and the child contents of the
3795   *   LPN being passed.
3796   *
3797   *   It returns the number of rows for which the plan is
3798   *   activated, will return -1 in case of failure.
3799 
3800   *  @param   x_return_status   Return status of the function - Success, Error, Unexpected Error, Warning etc.,
3801   *  @param   x_msg_count       Count of messages in the stack
3802   *  @param   x_msg_data        Actual message if the count = 1 else it will be null.
3803   *  @param   p_org_id          Organization Identifier
3804   *  @param   p_lpn_id          LPN Identifier
3805   *  @param   p_emp_id          Employee Identifier
3806   *  @ RETURN  NUMBER
3807 
3808 
3809 **/
3810    FUNCTION Activate_Plan_For_Load(
3811        x_return_status OUT  NOCOPY  VARCHAR2
3812       ,x_msg_count     OUT  NOCOPY  NUMBER
3813       ,x_msg_data      OUT  NOCOPY  VARCHAR2
3814       ,p_org_id        IN           NUMBER
3815       ,p_lpn_id        IN           NUMBER
3816       ,p_emp_id        IN           NUMBER )
3817      RETURN NUMBER
3818      IS
3819 
3820      l_debug     NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
3821      l_progress  VARCHAR2(10) := '10';
3822      l_rec_count NUMBER       := 0;
3823      l_proc_name VARCHAR2(30) := 'Activate_Plan_For_Load:';
3824 
3825      l_error_code         NUMBER;
3826      l_task_rec           task_rec   := NULL;
3827      l_tempid_tab         num_tab;
3828 
3829      l_consolidation_method_id NUMBER;
3830      l_drop_lpn_option NUMBER;
3831 
3832      -- This cursor will get the MMTTs assoicated with the contents of the LPN passed
3833      -- along with the MMTTs of the contents of all its child LPNs also.
3834      --Bug5723418.Performance fix.Moved wlpn to FROM clause. Added hint
3835      -- Performane fix as a part of bug 7143123
3836     	CURSOR c_all_mmtt_cursor IS 	-- Bug 7453925
3837 	SELECT  /*+ ORDERED USE_NL(mtrl mmtt) INDEX(MTRL MTL_TXN_REQUEST_LINES_N7) */
3838               mmtt.transaction_temp_id
3839         FROM     ( SELECT lpn_id FROM   wms_license_plate_numbers
3840                                   START  WITH lpn_id = p_lpn_id
3841                                   CONNECT BY PRIOR lpn_id = parent_lpn_id
3842                        ) wlpn,
3843                       mtl_txn_request_lines mtrl,
3844                       mtl_material_transactions_temp mmtt
3845 	WHERE mtrl.line_id = mmtt.move_order_line_id
3846              AND mtrl.line_status = 7
3847  	     AND mtrl.lpn_id = wlpn.lpn_id ;
3848 
3849 
3850    BEGIN
3851 
3852      IF (l_debug = 1) THEN
3853         DEBUG('Start of function ' || l_proc_name ,l_proc_name,9);
3854         DEBUG('  p_org_id ==> '|| p_org_id,l_proc_name,4);
3855         DEBUG('  p_lpn_id ==> '|| p_lpn_id,l_proc_name,4);
3856         DEBUG('  p_emp_id ==> '|| p_emp_id,l_proc_name,4);
3857      END IF;
3858 
3859      -- Set the return status to success.
3860      l_progress := '20';
3861      x_return_status  := fnd_api.g_ret_sts_success;
3862      l_progress := '30';
3863 
3864        -- Setting the values for the task record
3865        -- Neednot set these values for drop since there ia already WDT,
3866        -- ATF will just change the status of this WDT record
3867 
3868        l_task_rec.person_id       := p_emp_id;
3869        l_task_rec.organization_id := p_org_id;
3870        l_task_rec.user_task_type  := -1;
3871 
3872      l_progress := '100';
3873      OPEN c_all_mmtt_cursor;
3874      l_progress := '110';
3875 
3876      LOOP -- c_all_mmtt_cursor loop
3877 
3878        EXIT WHEN c_all_mmtt_cursor%NOTFOUND;
3879        l_progress := '200';
3880 
3881        FETCH c_all_mmtt_cursor
3882          BULK COLLECT
3883           INTO l_tempid_tab
3884          LIMIT l_limit;
3885        l_progress := '210';
3886 
3887        l_rec_count := l_rec_count + l_tempid_tab.COUNT;
3888        l_progress := '220';
3889 
3890        -- Activate each MMTT's
3891        FOR i IN 1 .. l_tempid_tab.COUNT
3892        LOOP --All temp ids loop
3893 
3894           IF (l_debug = 1) THEN
3895              DEBUG('Calling activate_operation_instance with ...',l_proc_name,9);
3896              DEBUG('p_source_task_id    => ' || l_tempid_tab(i),l_proc_name,9);
3897              DEBUG('p_activity_type_id  => ' || G_OP_ACTIVITY_INBOUND,l_proc_name,9);
3898              DEBUG('p_task_execute_rec  => l_task_rec',l_proc_name,9);
3899              DEBUG('p_operation_type_id => ' || G_OP_TYPE_DROP,l_proc_name,9);
3900           END IF;
3901           l_progress := '300';
3902 
3903           wms_atf_runtime_pub_apis.activate_operation_instance
3904              (  p_source_task_id    => l_tempid_tab(i)
3905                ,p_activity_id       => G_OP_ACTIVITY_INBOUND
3906                ,p_task_execute_rec  => l_task_rec
3907                ,p_operation_type_id => G_OP_TYPE_LOAD
3908                ,x_return_status     => x_return_status
3909                ,x_msg_data          => x_msg_data
3910                ,x_msg_count         => x_msg_count
3911                ,x_error_code        => l_error_code
3912 	       ,x_consolidation_method_id => l_consolidation_method_id
3913 	       ,x_drop_lpn_option   => l_drop_lpn_option
3914 		);
3915           l_progress := '310';
3916 
3917           IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
3918              IF (l_debug = 1) THEN
3919                 DEBUG(l_error_code || ' Error in activate_operation_instance ' ,l_proc_name,1);
3920              END IF;
3921              l_progress := '320';
3922 
3923              RAISE fnd_api.g_exc_error;
3924           END IF;
3925 
3926        END LOOP; --All temp ids loop
3927 
3928      END LOOP; -- c_all_mmtt_cursor loop
3929 
3930 
3931      IF c_all_mmtt_cursor%ISOPEN THEN
3932        CLOSE c_all_mmtt_cursor;
3933      END IF;
3934 
3935      l_progress := '990';
3936      --Return the count of rows processed
3937      RETURN l_rec_count;
3938 
3939    EXCEPTION
3940      WHEN fnd_api.g_exc_error THEN
3941        x_return_status  := fnd_api.g_ret_sts_error;
3942        fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
3943        DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
3944        DEBUG(SQLERRM,l_proc_name,1);
3945 
3946        IF c_all_mmtt_cursor%ISOPEN THEN
3947          CLOSE c_all_mmtt_cursor;
3948        END IF;
3949 
3950        RETURN -1;
3951 
3952 
3953      WHEN fnd_api.g_exc_unexpected_error THEN
3954        x_return_status  := fnd_api.g_ret_sts_unexp_error;
3955        fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
3956 
3957        DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
3958        DEBUG(SQLERRM,l_proc_name,1);
3959 
3960        IF c_all_mmtt_cursor%ISOPEN THEN
3961          CLOSE c_all_mmtt_cursor;
3962        END IF;
3963 
3964        RETURN -1;
3965 
3966 
3967      WHEN OTHERS THEN
3968        x_return_status  := fnd_api.g_ret_sts_unexp_error;
3969        fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
3970        DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
3971        DEBUG(SQLERRM,l_proc_name,1);
3972 
3973        IF c_all_mmtt_cursor%ISOPEN THEN
3974          CLOSE c_all_mmtt_cursor;
3975        END IF;
3976 
3977        RETURN -1;
3978 
3979    END Activate_Plan_For_Load;
3980 
3981 
3982 /**
3983   *   This function will call the ATF API to complete the operation instance for load.
3984   *   It will call the ATF API  wms_atf_runtime_pub_apis.activate_operation_instance for
3985   *   each MMTT. For the immediate and the child contents of the LPN being passed.
3986   *
3987   *   It returns the number of rows for which the plan is  completed. will return -1 in case of failure.
3988 
3989   *  @param   x_return_status   Return status of the function - Success, Error, Unexpected Error, Warning etc.,
3990   *  @param   x_msg_count       Count of messages in the stack
3991   *  @param   x_msg_data        Actual message if the count = 1 else it will be null.
3992   *  @param   p_org_id          Organization Identifier
3993   *  @param   p_lpn_id          LPN Identifier
3994   *  @param   p_emp_id          Employee Identifier
3995   *  @ RETURN  NUMBER
3996 
3997 
3998 **/
3999    FUNCTION Complete_Plan_For_Load(
4000        x_return_status OUT  NOCOPY  VARCHAR2
4001       ,x_msg_count     OUT  NOCOPY  NUMBER
4002       ,x_msg_data      OUT  NOCOPY  VARCHAR2
4003       ,p_org_id        IN           NUMBER
4004       ,p_lpn_id        IN           NUMBER
4005       ,p_emp_id        IN           NUMBER )
4006      RETURN NUMBER
4007      IS
4008 
4009      l_debug     NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
4010      l_progress  VARCHAR2(10) := '10';
4011      l_rec_count NUMBER       := 0;
4012      l_proc_name VARCHAR2(30) := 'Complete_Plan_For_Load:';
4013 
4014      l_error_code         NUMBER;
4015      l_tempid_tab         num_tab;
4016 
4017      -- This cursor will get the MMTTs assoicated with the contents of the LPN passed
4018      -- along with the MMTTs of the contents of all its child LPNs also.
4019      --Bug5723418.Performance fix.Moved wlpn to FROM clause.Also added hint
4020      -- Performane fix as a part of bug 7143123
4021     	CURSOR c_all_mmtt_cursor IS 	-- Bug 7453925
4022 	SELECT  /*+ ORDERED USE_NL(mtrl mmtt) INDEX(MTRL MTL_TXN_REQUEST_LINES_N7) */
4023               mmtt.transaction_temp_id
4024         FROM     ( SELECT lpn_id FROM   wms_license_plate_numbers
4025                                   START  WITH lpn_id = p_lpn_id
4026                                   CONNECT BY PRIOR lpn_id = parent_lpn_id
4027                        ) wlpn,
4028                       mtl_txn_request_lines mtrl,
4029                       mtl_material_transactions_temp mmtt
4030 	WHERE mtrl.line_id = mmtt.move_order_line_id
4031              AND mtrl.line_status = 7
4032  	     AND mtrl.lpn_id = wlpn.lpn_id ;
4033 
4034 
4035    BEGIN
4036 
4037      IF (l_debug = 1) THEN
4038         DEBUG('Start of function ' || l_proc_name ,l_proc_name,9);
4039         DEBUG('  p_org_id ==> '|| p_org_id,l_proc_name,4);
4040         DEBUG('  p_lpn_id ==> '|| p_lpn_id,l_proc_name,4);
4041         DEBUG('  p_emp_id ==> '|| p_emp_id,l_proc_name,4);
4042      END IF;
4043 
4044      -- Set the return status to success.
4045      l_progress := '20';
4046      x_return_status  := fnd_api.g_ret_sts_success;
4047      l_progress := '30';
4048 
4049      l_progress := '100';
4050      OPEN c_all_mmtt_cursor;
4051      l_progress := '110';
4052 
4053      LOOP -- c_all_mmtt_cursor loop
4054 
4055        EXIT WHEN c_all_mmtt_cursor%NOTFOUND;
4056        l_progress := '200';
4057 
4058        FETCH c_all_mmtt_cursor
4059          BULK COLLECT
4060           INTO l_tempid_tab
4061          LIMIT l_limit;
4062        l_progress := '210';
4063 
4064        l_rec_count := l_rec_count + l_tempid_tab.COUNT;
4065        l_progress := '220';
4066 
4067        -- Complete the Load Operation for each MMTT's
4068        FOR i IN 1 .. l_tempid_tab.COUNT
4069        LOOP --All temp ids loop
4070 
4071           IF (l_debug = 1) THEN
4072              DEBUG('Calling complete_operation_instance with ...',l_proc_name,9);
4073              DEBUG('p_source_task_id    => ' || l_tempid_tab(i),l_proc_name,9);
4074              DEBUG('p_activity_type_id  => ' || G_OP_ACTIVITY_INBOUND,l_proc_name,9);
4075              DEBUG('p_task_execute_rec  => l_task_rec',l_proc_name,9);
4076              DEBUG('p_operation_type_id => ' || G_OP_TYPE_DROP,l_proc_name,9);
4077           END IF;
4078           l_progress := '300';
4079 
4080           wms_atf_runtime_pub_apis.complete_operation_instance
4081              (  p_source_task_id    => l_tempid_tab(i)
4082                ,p_activity_id       => G_OP_ACTIVITY_INBOUND
4083                ,p_operation_type_id => G_OP_TYPE_LOAD
4084                ,x_return_status     => x_return_status
4085                ,x_msg_data          => x_msg_data
4086                ,x_msg_count         => x_msg_count
4087                ,x_error_code        => l_error_code
4088              );
4089           l_progress := '310';
4090 
4091           IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
4092              IF (l_debug = 1) THEN
4093                 DEBUG(l_error_code || ' Error in complete_operation_instance ' ,l_proc_name,1);
4094              END IF;
4095              l_progress := '320';
4096 
4097              RAISE fnd_api.g_exc_error;
4098           END IF;
4099 
4100        END LOOP; --All temp ids loop
4101 
4102      END LOOP; -- c_all_mmtt_cursor loop
4103 
4104 
4105      IF c_all_mmtt_cursor%ISOPEN THEN
4106        CLOSE c_all_mmtt_cursor;
4107      END IF;
4108 
4109      l_progress := '990';
4110      --Return the count of rows processed
4111      RETURN l_rec_count;
4112 
4113    EXCEPTION
4114      WHEN fnd_api.g_exc_error THEN
4115        x_return_status  := fnd_api.g_ret_sts_error;
4116        fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
4117        DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
4118        DEBUG(SQLERRM,l_proc_name,1);
4119 
4120        IF c_all_mmtt_cursor%ISOPEN THEN
4121          CLOSE c_all_mmtt_cursor;
4122        END IF;
4123 
4124        RETURN -1;
4125 
4126 
4127      WHEN fnd_api.g_exc_unexpected_error THEN
4128        x_return_status  := fnd_api.g_ret_sts_unexp_error;
4129        fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
4130 
4131        DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
4132        DEBUG(SQLERRM,l_proc_name,1);
4133 
4134        IF c_all_mmtt_cursor%ISOPEN THEN
4135          CLOSE c_all_mmtt_cursor;
4136        END IF;
4137 
4138        RETURN -1;
4139 
4140 
4141      WHEN OTHERS THEN
4142        x_return_status  := fnd_api.g_ret_sts_unexp_error;
4143        fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
4144        DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
4145        DEBUG(SQLERRM,l_proc_name,1);
4146 
4147        IF c_all_mmtt_cursor%ISOPEN THEN
4148          CLOSE c_all_mmtt_cursor;
4149        END IF;
4150 
4151        RETURN -1;
4152 
4153    END Complete_Plan_For_Load;
4154 
4155 
4156 
4157 
4158 /**
4159   *   This Procedure will do the ATF integration for the LOAD operation
4160   *   in case of single step drop or load portion of manual drop.
4161   *
4162   *   This procedure inturn will call Activate_Plan_For_Load to activate
4163   *   the plan for load and then will call Complete_Plan_For_Load to complete the LOAD step.
4164   *
4165   *   This procedure will be called from create_grouped_tasks in case of single step drop
4166   *   ie the LPN which has to be dropped is not loaded already.
4167   *
4168   *  @param   x_return_status   Return status of the function - Success, Error, Unexpected Error, Warning etc.,
4169   *  @param   x_msg_count       Count of messages in the stack
4170   *  @param   x_msg_data        Actual message if the count = 1 else it will be null.
4171   *  @param   p_org_id          Organization Identifier
4172   *  @param   p_lpn_id          LPN Identifier
4173   *  @param   p_emp_id          Employee Identifier
4174   *  @ RETURN  NUMBER
4175 
4176 
4177 **/
4178    PROCEDURE Complete_ATF_Load(
4179        x_return_status OUT  NOCOPY  VARCHAR2
4180       ,x_msg_count     OUT  NOCOPY  NUMBER
4181       ,x_msg_data      OUT  NOCOPY  VARCHAR2
4182       ,p_org_id        IN           NUMBER
4183       ,p_lpn_id        IN           NUMBER
4184       ,p_emp_id        IN           NUMBER )
4185      IS
4186 
4187      l_debug     NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
4188      l_progress  VARCHAR2(10) := '10';
4189      l_rec_count NUMBER       := 0;
4190      l_proc_name VARCHAR2(30) := 'Complete_ATF_Load:';
4191 
4192      l_tempid_tab         num_tab;
4193 
4194    BEGIN
4195 
4196      IF (l_debug = 1) THEN
4197         DEBUG('Start of Procedure ' || l_proc_name ,l_proc_name,9);
4198         DEBUG('  p_org_id ==> '|| p_org_id,l_proc_name,4);
4199         DEBUG('  p_lpn_id ==> '|| p_lpn_id,l_proc_name,4);
4200         DEBUG('  p_emp_id ==> '|| p_emp_id,l_proc_name,4);
4201      END IF;
4202 
4203      -- Set the return status to success.
4204      l_progress := '20';
4205      x_return_status  := fnd_api.g_ret_sts_success;
4206      l_progress := '30';
4207 
4208      IF (l_debug = 1) THEN
4209         DEBUG(' Calling Activate Instance for the LOAD Operation ... ' || x_msg_data ,l_proc_name,9);
4210      END IF;
4211 
4212      l_progress := '100';
4213      -- Activate the LOAD operation
4214      l_rec_count := Activate_Plan_For_Load(
4215                              x_return_status  => x_return_status
4216                             ,x_msg_count      => x_msg_count
4217                             ,x_msg_data       => x_msg_data
4218                             ,p_org_id         => p_org_id
4219                             ,p_lpn_id         => p_lpn_id
4220                             ,p_emp_id         => p_emp_id);
4221 
4222      l_progress := '110';
4223 
4224      IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
4225         IF (l_debug = 1) THEN
4226            DEBUG(' Error in Activate_Plan_For_Load ' || x_msg_data ,l_proc_name,1);
4227         END IF;
4228         l_progress := '120';
4229 
4230         RAISE fnd_api.g_exc_error;
4231      ELSE
4232        IF (l_debug = 1) THEN
4233           DEBUG('Successfully called Activate_Plan_For_Load for ' || l_rec_count || ' row(s)' ,l_proc_name,9);
4234        END IF;
4235        l_progress := '130';
4236 
4237      END IF;
4238 
4239      IF (l_debug = 1) THEN
4240         DEBUG(' Calling Complete Instance for the LOAD Operation ... ' || x_msg_data ,l_proc_name,9);
4241      END IF;
4242 
4243      l_progress := '200';
4244      -- Complete the LOAD operation
4245      l_rec_count := Complete_Plan_For_Load(
4246                              x_return_status  => x_return_status
4247                             ,x_msg_count      => x_msg_count
4248                             ,x_msg_data       => x_msg_data
4249                             ,p_org_id         => p_org_id
4250                             ,p_lpn_id         => p_lpn_id
4251                             ,p_emp_id         => p_emp_id);
4252 
4253      l_progress := '210';
4254 
4255      IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
4256         IF (l_debug = 1) THEN
4257            DEBUG(' Error in Complete_Plan_For_Load ' || x_msg_data ,l_proc_name,1);
4258         END IF;
4259         l_progress := '220';
4260 
4261         RAISE fnd_api.g_exc_error;
4262      ELSE
4263         IF (l_debug = 1) THEN
4264            DEBUG('Successfully called Complete_Plan_For_Load for ' || l_rec_count || ' row(s)' ,l_proc_name,9);
4265         END IF;
4266         l_progress := '130';
4267 
4268      END IF;
4269 
4270      l_progress := '990';
4271 
4272    EXCEPTION
4273      WHEN fnd_api.g_exc_error THEN
4274        x_return_status  := fnd_api.g_ret_sts_error;
4275        fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
4276        DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
4277        DEBUG(SQLERRM,l_proc_name,1);
4278 
4279      WHEN fnd_api.g_exc_unexpected_error THEN
4280        x_return_status  := fnd_api.g_ret_sts_unexp_error;
4281        fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
4282        DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
4283        DEBUG(SQLERRM,l_proc_name,1);
4284 
4285      WHEN OTHERS THEN
4286        x_return_status  := fnd_api.g_ret_sts_unexp_error;
4287        fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
4288        DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
4289        DEBUG(SQLERRM,l_proc_name,1);
4290 
4291    END Complete_ATF_Load;
4292 
4293 
4294 
4295 
4296 
4297 /**
4298   *   This function will call the ATF API to activate the operation instance for drop
4299   *   It will call the ATF API wms_atf_runtime_pub_apis.activate_operation_instance
4300   *   for each 'All Task' row in the temp table.
4301   *
4302   *   After call to ATF API the new dest sub/loc/LPN will be updated back to the
4303   *   global temp table.
4304   *
4305   *   It returns the number of rows for which the plan is activated
4306   *   will return -1 in case of failure.
4307 
4308   *  @param  x_return_status  Return status of the function - Success, Error, Unexpected Error, Warning etc.,
4309   *  @param  x_msg_count      Count of messages in the stack
4310   *  @param  x_msg_data       Actual message if the count = 1 else it will be null.
4311   *
4312   *  @ RETURN  number
4313 
4314 
4315 **/
4316      FUNCTION Activate_Plan_For_Drop(
4317                       x_return_status   OUT  NOCOPY  VARCHAR2,
4318                       x_msg_count       OUT  NOCOPY  NUMBER,
4319                       x_msg_data        OUT  NOCOPY  VARCHAR2 )
4320 
4321        RETURN NUMBER
4322        IS
4323         l_debug     NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
4324         l_progress  VARCHAR2(10) := '10';
4325         l_rec_count NUMBER       := 0;
4326         l_proc_name VARCHAR2(30) := 'Activate_Plan_For_Drop:';
4327 
4328         l_error_code         NUMBER;
4329         l_task_rec           task_rec   := NULL;
4330         l_rowid_tab          rowid_tab;
4331         l_tempid_tab         num_tab;
4332         l_into_lpn_id_tab    num_tab;
4333         l_into_lpn_name_tab  lpn_name_tab;
4334         l_dest_sub_tab       sub_name_tab;
4335         l_dest_loc_tab       num_tab;
4336         l_sub_drop_order_tab num_tab;
4337         l_loc_drop_order_tab num_tab;
4338 	l_show_message_tab   num_tab;--R12
4339 	l_error_code_tab     varchar240_tab;
4340 	l_drop_lpn_option_tab num_tab;
4341 	l_consolidation_method_id_tab num_tab;
4342 	l_lpn_controlled_flag_tab num_tab;
4343 	l_lpn_locator_tab    num_tab;
4344 
4345 	l_drop_lpn_option NUMBER;
4346 
4347 	l_consolidation_method_id NUMBER;
4348         -- This cursor will get the transaction_temp_id of all the MMTTs which are going to
4349         -- be processed for this drop.
4350         -- The rowid is also selected inorder to update back the dest sub/loc/LPN to the temp table.
4351         CURSOR c_all_tasks_cursor IS
4352           SELECT ROWID
4353 	         ,transaction_temp_id
4354 	         ,show_message
4355 	         ,error_code
4356           FROM   wms_putaway_group_tasks_gtmp
4357           WHERE  row_type = G_ROW_TP_ALL_TASK;
4358 
4359         -- This cursor will get the MMTT details for the temp id passed
4360         CURSOR c_mmtt_cursor(v_transaction_temp_id NUMBER) IS
4361           SELECT NVL(mmtt.transfer_subinventory,mmtt.subinventory_code) dest_subinventory
4362                 ,NVL(mmtt.transfer_to_location, mmtt.locator_id)        dest_locator
4363                 ,mmtt.cartonization_id        into_lpn_id
4364                 ,wlpn.license_plate_number    into_lpn_name
4365                 ,msi.dropping_order           sub_dropping_order
4366 	        ,milk.dropping_order          loc_dropping_order
4367 	        ,Nvl(msi.lpn_controlled_flag,2) lpn_controlled_flag
4368 	  FROM   mtl_material_transactions_temp mmtt
4369                 ,wms_license_plate_numbers      wlpn
4370                 ,mtl_secondary_inventories      msi
4371                 ,mtl_item_locations_kfv         milk
4372           WHERE mmtt.transaction_temp_id  = v_transaction_temp_id
4373                 AND mmtt.cartonization_id = wlpn.lpn_id(+)
4374                 AND NVL(mmtt.transfer_to_location,mmtt.locator_id) = milk.inventory_location_id(+)
4375                 AND msi.organization_id(+) = mmtt.organization_id
4376                 AND NVL(mmtt.transfer_subinventory,mmtt.subinventory_code)  = msi.secondary_inventory_name(+)
4377                 AND mmtt.organization_id = milk.organization_id(+);
4378 
4379      BEGIN
4380 
4381        IF (l_debug = 1) THEN
4382           DEBUG('Start of function ' || l_proc_name ,l_proc_name,9);
4383        END IF;
4384 
4385 
4386        -- Set the return status to success.
4387        l_progress := '20';
4388        x_return_status  := fnd_api.g_ret_sts_success;
4389        l_progress := '30';
4390 
4391        -- Setting the values for the task record
4392        -- Neednot set these values for drop since there ia already WDT,
4393        -- ATF will just change the status of this WDT record
4394        /*
4395        l_task_rec.person_id       := p_emp_id;
4396        l_task_rec.organization_id := p_org_id;
4397        l_task_rec.user_task_type  := -1;
4398        */
4399 
4400        l_progress := '100';
4401        OPEN c_all_tasks_cursor;
4402        l_progress := '110';
4403 
4404        LOOP -- c_all_tasks_cursor loop
4405 
4406          EXIT WHEN c_all_tasks_cursor%NOTFOUND;
4407          l_progress := '200';
4408 
4409          FETCH c_all_tasks_cursor
4410            BULK COLLECT
4411             INTO l_rowid_tab
4412 	         ,l_tempid_tab
4413 	         ,l_show_message_tab
4414 	         ,l_error_code_tab
4415            LIMIT l_limit;
4416          l_progress := '210';
4417 
4418          l_rec_count := l_rec_count + l_tempid_tab.COUNT;
4419          l_progress := '220';
4420 
4421          -- Activate each MMTT's
4422          FOR i IN 1 .. l_tempid_tab.COUNT
4423          LOOP --All temp ids loop
4424 
4425             IF (l_debug = 1) THEN
4426                DEBUG('Calling activate_operation_instance with ...',l_proc_name,9);
4427                DEBUG('p_source_task_id    => ' || l_tempid_tab(i),l_proc_name,9);
4428                DEBUG('p_activity_type_id  => ' || G_OP_ACTIVITY_INBOUND,l_proc_name,9);
4429                DEBUG('p_task_execute_rec  => l_task_rec',l_proc_name,9);
4430                DEBUG('p_operation_type_id => ' || G_OP_TYPE_DROP,l_proc_name,9);
4431             END IF;
4432             l_progress := '300';
4433 
4434             wms_atf_runtime_pub_apis.activate_operation_instance
4435                (  p_source_task_id    => l_tempid_tab(i)
4436                  ,p_activity_id       => G_OP_ACTIVITY_INBOUND
4437                  ,p_task_execute_rec  => l_task_rec
4438                  ,p_operation_type_id => G_OP_TYPE_DROP
4439                  ,x_return_status     => x_return_status
4440                  ,x_msg_data          => x_msg_data
4441                  ,x_msg_count         => x_msg_count
4442                  ,x_error_code        => l_error_code
4443 		 ,x_consolidation_method_id => l_consolidation_method_id
4444 		 ,x_drop_lpn_option   => l_drop_lpn_option
4445 		  );
4446             l_progress := '310';
4447 
4448 	    IF (l_debug = 1) THEN
4449                DEBUG('activate_operation_instance returned',l_proc_name,9);
4450                DEBUG('x_return_status            => '||x_return_status,l_proc_name,9);
4451                DEBUG('x_consolidation_method_id  => '||l_consolidation_method_id,l_proc_name,9);
4452 	       debug('x_drop_lpn_option          => '||l_drop_lpn_option,l_proc_name,9);
4453 	       debug('x_msg_data                 => '||x_msg_data,l_proc_name,9);
4454 	       debug('x_msg_count                => '||x_msg_count,l_proc_name,9);
4455 	       debug('x_error_code               => '||l_error_code,l_proc_name,9);
4456 	    END IF;
4457 
4458 	    --R12: ATF will pass back the status 'W' if all the
4459 	    --consolidation locators are full.  If so, get the message from
4460 	    --the stack and store it in the GTMP
4461 	    IF (x_return_status = 'W') THEN
4462 	       l_show_message_tab(i) := 2;
4463 	       --Assume that x_msg_data stores the translated string, so
4464 	       --the UI just needs to show the string without any translation
4465 	       l_error_code_tab(i) := x_msg_data;
4466 
4467 	     ELSIF (x_return_status <> fnd_api.g_ret_sts_success) THEN
4468                IF (l_debug = 1) THEN
4469                   DEBUG(l_error_code || ' Error in activate_operation_instance ' ,l_proc_name,1);
4470                END IF;
4471                l_progress := '320';
4472 
4473                RAISE fnd_api.g_exc_error;
4474             END IF;
4475 
4476 	    l_drop_lpn_option_tab(i) := l_drop_lpn_option;
4477 	    l_consolidation_method_id_tab(i) := l_consolidation_method_id;
4478 
4479          -- ATF would have updated the dest sub/loc/LPN in MMTT. So query it and sync it with the temp table.
4480 
4481          -- Querying the dest sub/loc/LPN for the acitvated row
4482          l_progress := '400';
4483          OPEN c_mmtt_cursor(l_tempid_tab(i));
4484          l_progress := '410';
4485 
4486          IF (l_debug = 1) THEN
4487             DEBUG(' Getting the details of the activated row ' ,l_proc_name,9);
4488          END IF;
4489 
4490          FETCH  c_mmtt_cursor
4491            INTO l_dest_sub_tab(i)
4492                ,l_dest_loc_tab(i)
4493                ,l_into_lpn_id_tab(i)
4494                ,l_into_lpn_name_tab(i)
4495                ,l_sub_drop_order_tab(i)
4496 	       ,l_loc_drop_order_tab(i)
4497 	       ,l_lpn_controlled_flag_tab(i);
4498 
4499 	 IF (l_debug = 1) THEN
4500             DEBUG('show_message: '||l_show_message_tab(i),l_proc_name,9);
4501          END IF;
4502 
4503 	 l_progress := '420';
4504 
4505          CLOSE c_mmtt_cursor;
4506          l_progress := '430';
4507 
4508          END LOOP; --All temp ids loop
4509 
4510          IF (l_debug = 1) THEN
4511             DEBUG('Doing a BULK update of temp table with the ATF suggested dest sub/loc/LPN ' ,l_proc_name,9);
4512          END IF;
4513 
4514          -- Do the bulk update on the temp table with the ATF suggested dest sub/loc/lpn
4515          FORALL i IN 1 .. l_tempid_tab.COUNT
4516            UPDATE wms_putaway_group_tasks_gtmp
4517            SET    dest_subinventory  = l_dest_sub_tab(i)
4518                  ,dest_locator       = l_dest_loc_tab(i)
4519                  ,into_lpn_id        = l_into_lpn_id_tab(i)
4520                  ,into_lpn_name      = l_into_lpn_name_tab(i)
4521                  ,locator            = inv_project.get_locsegs (l_dest_loc_tab(i), organization_id)
4522                  ,sub_dropping_order = l_sub_drop_order_tab(i)
4523                  ,loc_dropping_order = l_loc_drop_order_tab(i)
4524 	         ,show_message       = l_show_message_tab(i) --R12
4525 	         ,error_code         = l_error_code_tab(i) --???
4526 	         ,drop_lpn_option  = l_drop_lpn_option_tab(i) --R12
4527 	         ,consolidation_method_id = l_consolidation_method_id_tab(i) --R12
4528 	         ,sub_lpn_controlled_flag = l_lpn_controlled_flag_tab(i) --R12
4529 	   WHERE ROWID = l_rowid_tab(i);
4530          l_progress := '440';
4531 
4532          IF (l_debug = 1) THEN
4533             DEBUG('Done a BULK update of this set of acivated MMTTs. rows = ' || SQL%ROWCOUNT ,l_proc_name,9);
4534          END IF;
4535 
4536 
4537        END LOOP; -- c_all_tasks_cursor loop
4538 
4539        -- Close all the open cursors if any.
4540        IF c_all_tasks_cursor%ISOPEN THEN
4541          l_progress := '500';
4542          CLOSE c_all_tasks_cursor;
4543        END IF;
4544 
4545        IF c_mmtt_cursor%ISOPEN THEN
4546          l_progress := '510';
4547          CLOSE c_mmtt_cursor;
4548        END IF;
4549 
4550        l_progress := '990';
4551        --Return the count of rows processed
4552        RETURN l_rec_count;
4553 
4554      EXCEPTION
4555        WHEN fnd_api.g_exc_error THEN
4556          x_return_status  := fnd_api.g_ret_sts_error;
4557          fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
4558          DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
4559          DEBUG(SQLERRM,l_proc_name,1);
4560 
4561          IF c_all_tasks_cursor%ISOPEN THEN
4562             CLOSE c_all_tasks_cursor;
4563          END IF;
4564 
4565          IF c_mmtt_cursor%ISOPEN THEN
4566            CLOSE c_mmtt_cursor;
4567          END IF;
4568 
4569          RETURN -1;
4570 
4571 
4572        WHEN fnd_api.g_exc_unexpected_error THEN
4573          x_return_status  := fnd_api.g_ret_sts_unexp_error;
4574          fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
4575 
4576          DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
4577          DEBUG(SQLERRM,l_proc_name,1);
4578 
4579          IF c_all_tasks_cursor%ISOPEN THEN
4580             CLOSE c_all_tasks_cursor;
4581          END IF;
4582 
4583          IF c_mmtt_cursor%ISOPEN THEN
4584            CLOSE c_mmtt_cursor;
4585          END IF;
4586 
4587          RETURN -1;
4588 
4589 
4590        WHEN OTHERS THEN
4591          x_return_status  := fnd_api.g_ret_sts_unexp_error;
4592          fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
4593          DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
4594          DEBUG(SQLERRM,l_proc_name,1);
4595 
4596          IF c_all_tasks_cursor%ISOPEN THEN
4597             CLOSE c_all_tasks_cursor;
4598          END IF;
4599 
4600          IF c_mmtt_cursor%ISOPEN THEN
4601            CLOSE c_mmtt_cursor;
4602          END IF;
4603 
4604          RETURN -1;
4605 
4606      END Activate_Plan_For_Drop;
4607 
4608 
4609 /**
4610   *   This procedure will lock the LPNs which are in the temp table.
4611   *   Though the LPN is locked in the method Check_LPN_Validity,
4612   *   We are locking it again since the commits in between will releive the locks.
4613 
4614   *  @param  x_return_status  Return status of the procedure - Success, Error, Unexpected Error, Warning etc.,
4615   *  @param  x_msg_count      Count of messages in the stack
4616   *  @param  x_msg_data       Actual message if the count = 1 else it will be null.
4617 **/
4618   PROCEDURE Lock_LPNs(
4619             x_return_status   OUT  NOCOPY  VARCHAR2
4620            ,x_msg_count       OUT  NOCOPY  NUMBER
4621            ,x_msg_data        OUT  NOCOPY  VARCHAR2 )
4622     IS
4623 
4624     l_debug         NUMBER        := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
4625     l_progress      VARCHAR2(10)  := '10';
4626     l_proc_name     VARCHAR2(30)  := 'Lock_LPNs:';
4627     l_rec_count     NUMBER        := 0;
4628 
4629     -- Need to trap the error returned when
4630     -- unable to lock the lpn record.
4631     record_locked      EXCEPTION;
4632     PRAGMA EXCEPTION_INIT(record_locked, -54);
4633 
4634     l_lpn_id_tab    num_tab;
4635 
4636   BEGIN
4637 
4638     IF (l_debug = 1) THEN
4639       debug(' Start of procedure. Trying to lock all the LPNs in the temp table', l_proc_name,1);
4640     END IF;
4641 
4642     l_progress := '100';
4643     x_return_status  := fnd_api.g_ret_sts_success;
4644     l_progress := '110';
4645 
4646     -- Bulk Lock all LPNs which are in the temp table.
4647     -- So that other user can't work on them
4648 
4649     -- Bug#3821032 We need not lock the LPNs in receiving since it will cause deadlock if called in concurrent mode
4650     -- For receiving LPNs the Check_LPN_Validity wrapper takes care of locking Operation Instance records.
4651     -- So that other user(s) can't work on this LPN.
4652     SELECT  lpn_id
4653       BULK COLLECT
4654       INTO  l_lpn_id_tab
4655       FROM  WMS_LICENSE_PLATE_NUMBERS
4656       WHERE lpn_id IN ( SELECT  DISTINCT lpn_id
4657                         FROM    wms_putaway_group_tasks_gtmp
4658                         WHERE   row_type = G_ROW_TP_ALL_TASK
4659                                 AND lpn_context <> 3
4660                       )
4661       FOR UPDATE NOWAIT;
4662 
4663     l_progress := '120';
4664 
4665     IF (l_debug = 1) THEN
4666       debug(' End of procedure:' || SQL%rowcount || ' LPN(s) locked ', l_proc_name,1);
4667     END IF;
4668 
4669     l_progress := '200';
4670 
4671   EXCEPTION
4672       WHEN no_data_found THEN
4673         debug(' No LPNs found in the temp table for locking', l_proc_name,1);
4674         x_return_status  := fnd_api.g_ret_sts_success;
4675 
4676       WHEN record_locked THEN
4677         debug(' LPN Already locked by someone else', l_proc_name,1);
4678         x_return_status  := fnd_api.g_ret_sts_error;
4679 
4680         fnd_message.set_name('WMS', 'WMS_LPN_UNAVAIL');
4681         fnd_msg_pub.ADD;
4682         fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
4683 
4684       WHEN OTHERS THEN
4685         debug(' Exception while trying to lock LPN(s) ' || SQLERRM, l_proc_name,1);
4686         x_return_status  := fnd_api.g_ret_sts_unexp_error;
4687         fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
4688 
4689     END Lock_LPNs;
4690 
4691 
4692 /**
4693   *   This procedure will populate the grouped tasks temporary
4694   *   table (WMS_PUTAWAY_GROUP_TASKS_GTEMP with the required data
4695   *   and also it will stamp the drop type for the tasks and will
4696   *   group them based on various criterias.
4697 
4698   *  @param  x_return_status  Return status of the procedure - Success, Error, Unexpected Error, Warning etc.,
4699   *  @param  x_msg_count      Count of messages in the stack
4700   *  @param  x_msg_data       Actual message if the count = 1 else it will be null.
4701   *  @param  p_org_id         Organization Id
4702   *  @param  p_drop_type      This indicates for which type of drop the temp table should be populated.
4703   *                           This can have the values
4704   *                              SD - 'System Drop'
4705   *                              DA - 'Drop All'
4706   *                              ED - 'Existing Putaway Drop'
4707   *  @param  p_emp_id         Employee identifier
4708   *  @param  p_lpn_id         LPN for which this procedure is called.
4709   *  @param  p_item_drop_flag Whether it is called for all item drops or not
4710   *  @param  p_lpn_is_loaded  Whether the LPN is already loaded or not.
4711 
4712 **/
4713   PROCEDURE   Create_Grouped_Tasks(
4714                 x_return_status   OUT  NOCOPY  VARCHAR2
4715                ,x_msg_count       OUT  NOCOPY  NUMBER
4716                ,x_msg_data        OUT  NOCOPY  VARCHAR2
4717                ,p_org_id          IN           NUMBER
4718                ,p_drop_type       IN           VARCHAR2
4719                ,p_emp_id          IN           NUMBER
4720                ,p_lpn_id          IN           NUMBER
4721                ,p_item_drop_flag  IN           VARCHAR2
4722                ,p_lpn_is_loaded   IN           VARCHAR2 )
4723     IS
4724     l_debug         NUMBER        := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
4725     l_progress      VARCHAR2(10)  := '10';
4726     l_proc_name     VARCHAR2(30)  := 'Create_Grouped_Tasks:';
4727     l_rec_count     NUMBER        := 0;
4728     l_del_count     NUMBER        := 0;
4729 
4730     l_rowid_tab      rowid_tab;
4731     l_drop_order_tab num_tab;
4732     l_lpn_id_tab     num_tab;
4733     l_lpn_name_tab   lpn_name_tab;
4734     l_lpn_level_tab  num_tab;
4735     l_parent_lpn_id  NUMBER;
4736 
4737     -- This cursor will get all the LPNs and their drop_order where drop_type is CD.
4738     -- This cursor will be used to mark the consolidated LPN of all itz children with the current lpn.
4739     -- Start with the outermost LPN first and progress towards the childs.
4740 
4741     -- For optimization we are marking the consolidated LPN from top down (order by lpn_level asc)
4742     -- and hence if consolidated lpn is marked we neednot mark it again for the children (since Mark Consolidated lpn
4743     -- would have already marked the consolidated lpn to the childs also)
4744     CURSOR c_lpn_cursor IS
4745        SELECT DISTINCT
4746               lpn_id
4747              ,lpn_name
4748              ,lpn_level
4749        FROM   WMS_PUTAWAY_GROUP_TASKS_GTMP
4750        WHERE  consolidated_lpn_id IS NULL
4751               AND drop_type = G_DT_CONSOLIDATED_DROP
4752        ORDER BY lpn_level ASC;
4753 
4754 
4755    BEGIN
4756 
4757       IF (l_debug = 1) THEN
4758          DEBUG(' Procedure Entered at ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
4759          DEBUG(' p_org_id    => '||p_org_id,   l_proc_name,4);
4760          DEBUG(' p_drop_type => '||p_drop_type,l_proc_name,4);
4761          DEBUG(' p_emp_id    => '||p_emp_id,   l_proc_name,4);
4762          DEBUG(' p_lpn_id    => '||p_lpn_id,   l_proc_name,4);
4763          DEBUG(' p_item_drop_flag    => '||p_item_drop_flag,  l_proc_name,4);
4764          DEBUG(' p_lpn_is_loaded     => '||p_lpn_is_loaded, l_proc_name,4);
4765       END IF;
4766 
4767       l_progress := '20';
4768       x_return_status  := fnd_api.g_ret_sts_success;
4769       l_progress := '30';
4770 
4771       -- Always delete the existing records in the temp table before creating it.
4772       -- This code has to be later forked if the grouping logic has to be called for
4773       -- Restamping the drop type. In that case both delete and populate_group_tasks functions
4774       -- should not be called.
4775 
4776       BEGIN -- Delete Existing rows in the temp table
4777 
4778         l_progress := '40';
4779         -- Delete all the rows in the temp table
4780         l_del_count := Remove_Tasks_In_Group(
4781                           p_del_type  =>  3
4782                         );
4783         l_progress := '50';
4784 
4785         IF l_del_count = -1 THEN
4786           --Deletion has failed so raise error.
4787           RAISE fnd_api.g_exc_unexpected_error;
4788         END IF;
4789 
4790         l_progress := '60';
4791 
4792       EXCEPTION
4793         WHEN OTHERS THEN
4794            RAISE fnd_api.g_exc_unexpected_error;
4795       END; -- Delete Existing rows in the temp table
4796 
4797 
4798       -- Check whether the LPN is already loaded or not.
4799       -- If already loaded procced with grouping logic,
4800       -- else call the ATF APIs to simulate the LOAD operation.
4801 
4802       --LPN already loaded check
4803       IF p_lpn_is_loaded <> 'Y' THEN
4804 
4805           l_progress := '100';
4806           -- We have to call the ATF APis to simulate the LOAD operation in case of single step drop
4807           BEGIN -- Simulate LOAAD
4808 
4809 	     --BUG 3495726 Issue 25: Unpack from parent LPN first
4810 	     SELECT parent_lpn_id
4811 	       INTO l_parent_lpn_id
4812 	       FROM wms_license_plate_numbers
4813 	       WHERE lpn_id = p_lpn_id;
4814 
4815 	     IF (l_parent_lpn_id IS NOT NULL) THEN
4816 	       IF (l_debug = 1) THEN
4817 		  DEBUG(' Unpacking LPN from Parent LPN' ,l_proc_name,9);
4818 	       END IF;
4819 	       wms_container_pvt.packunpack_container
4820 		 (p_api_version       =>   1.0
4821 		  ,p_content_lpn_id    =>  p_lpn_id
4822 		  ,p_lpn_id           =>   l_parent_lpn_id
4823 		  ,p_operation        =>   2 /* Unpack */
4824 		  ,p_organization_id  =>   p_org_id
4825 		  ,x_return_status    =>   x_return_status
4826 		  ,x_msg_count        =>   x_msg_count
4827 		  ,x_msg_data         =>   x_msg_data
4828 		  );
4829 	       IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
4830 		  IF (l_debug = 1) THEN
4831 		     DEBUG(' Error Unpacking LPN from Parent LPN' ,l_proc_name,9);
4832 		  END IF;
4833 		  RAISE fnd_api.g_exc_error;
4834 	       END IF;
4835 	    END IF;
4836 
4837             IF (l_debug = 1) THEN
4838                DEBUG(' Calling Complete Instance for the LOAD Operation ... ' || x_msg_data ,l_proc_name,9);
4839             END IF;
4840 
4841             l_progress := '110';
4842             Complete_ATF_Load (x_return_status  => x_return_status
4843                               ,x_msg_count      => x_msg_count
4844                               ,x_msg_data       => x_msg_data
4845                               ,p_org_id         => p_org_id
4846                               ,p_lpn_id         => p_lpn_id
4847                               ,p_emp_id         => p_emp_id );
4848             l_progress := '120';
4849 
4850 
4851             -- Check for the return status
4852             IF x_return_status = fnd_api.g_ret_sts_success THEN
4853               -- Activate completed successfully.
4854               NULL;
4855 
4856             ELSIF x_return_status = fnd_api.g_ret_sts_error THEN
4857               RAISE fnd_api.g_exc_error;
4858 
4859             ELSIF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
4860               RAISE fnd_api.g_exc_unexpected_error;
4861 
4862             END IF;
4863 
4864             l_progress := '130';
4865 
4866           EXCEPTION
4867             WHEN OTHERS THEN
4868               RAISE fnd_api.g_exc_unexpected_error;
4869           END; -- Simulate LOAAD
4870 
4871       END IF; --LPN already loaded check
4872 
4873 
4874 
4875 
4876       --Grouping logic
4877       /*  This procedure is the starting point for groupping logic
4878        *  This inturn will do the following
4879        *
4880        *  1. Populate group tasks with the required data
4881        *  2. Order the temp  table and stamp the drop_order
4882        *  3. Resolve the columns like parent_lpn_id, outermost_lpn_id and LPN_NAME etc.,
4883        *  4. Group the tasks
4884       */
4885 
4886       IF (l_debug = 1) THEN
4887         DEBUG(' Before calling pvt function Populate_group_tasks ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,9);
4888         DEBUG(' p_org_id    => '||p_org_id,   l_proc_name,4);
4889         DEBUG(' p_drop_type => '||p_drop_type,l_proc_name,4);
4890         DEBUG(' p_emp_id    => '||p_emp_id,   l_proc_name,4);
4891         DEBUG(' p_lpn_id    => '||p_lpn_id,   l_proc_name,4);
4892       END IF;
4893 
4894       --Calling Populate_group_tasks to populate the global temp table
4895       l_progress := '200';
4896       l_rec_count := Populate_group_tasks( p_org_id         => p_org_id
4897                                           ,p_drop_type      => p_drop_type
4898                                           ,p_emp_id         => p_emp_id
4899                                           ,p_lpn_id         => p_lpn_id
4900                                           ,p_item_drop_flag => p_item_drop_flag
4901                                          );
4902       l_progress := '210';
4903 
4904       IF (l_debug = 1) THEN
4905         DEBUG(' After calling pvt function Populate_group_tasks ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,9);
4906         DEBUG(' l_rec_count => '|| l_rec_count,l_proc_name,4);
4907       END IF;
4908 
4909       -- Record count take the following values after calling Populate_group_tasks
4910       -- -1  --> In case of failure
4911       --  0  --> No rows found
4912       --  >1 --> In case of success.
4913 
4914       -- Error out since it is failure case
4915       IF l_rec_count = -1 THEN
4916         l_progress := '220';
4917         RAISE fnd_api.g_exc_error;
4918       END IF;
4919 
4920       -- No tasks available. Set the message and display it in the UI
4921       IF l_rec_count = 0 THEN
4922           l_progress := '225';
4923           -- No Tasks found for the data passed
4924           -- Set error message and raise error handle it in exception block.
4925           FND_MESSAGE.SET_NAME('WMS','WMS_NO_TASKS');
4926           fnd_msg_pub.ADD;
4927           -- Instead of raising an error, return with success
4928           RETURN;
4929       END IF;
4930 
4931 
4932       --Call ATF API for stamping the dest sub/loc/LPN
4933       BEGIN --ATF Call to Activate
4934 
4935         l_progress := '250';
4936         l_rec_count := Activate_Plan_For_Drop (x_return_status => x_return_status
4937                                               ,x_msg_count     => x_msg_count
4938                                               ,x_msg_data      => x_msg_data );
4939         l_progress := '260';
4940 
4941 
4942         -- Check for the return status
4943         IF x_return_status = fnd_api.g_ret_sts_success THEN
4944           -- Activate completed successfully.
4945           NULL;
4946 
4947         ELSIF x_return_status = fnd_api.g_ret_sts_error THEN
4948           RAISE fnd_api.g_exc_error;
4949 
4950         ELSIF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
4951           RAISE fnd_api.g_exc_unexpected_error;
4952 
4953         ELSIF l_rec_count = -1 THEN
4954           RAISE fnd_api.g_exc_unexpected_error;
4955 
4956         END IF;
4957 
4958 
4959       EXCEPTION
4960         WHEN OTHERS THEN
4961           RAISE fnd_api.g_exc_unexpected_error;
4962 
4963       END; --ATF Call to Activate
4964 
4965        -- Core Grouping logic
4966          -- call the grouping function for consolidating drop
4967          -- call the API for stamping the consolidated lpn
4968          -- call the grouping function for grouping the MMTTs for item drop
4969          -- call the function to insert group tasks rows
4970 
4971        -- Call the function which will populate the drop type correctly and
4972        -- group the tasks
4973 
4974       --IF p_item_drop_flag <> 'Y' THEN --p_item_drop_flag check
4975          BEGIN  --Populating the drop type
4976 
4977             l_progress := '300';
4978 
4979             IF (l_debug = 1) THEN
4980               DEBUG(' Before calling pvt function Populate_Drop_type ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,9);
4981             END IF;
4982 
4983             -- This function call will group the tasks and populate the drop type correctly
4984             -- After this function call, the temp table will have the tasks grouped correctly.
4985             l_progress := '310';
4986             l_rec_count := Populate_Drop_Type;
4987             l_progress := '320';
4988 
4989             IF (l_debug = 1) THEN
4990               DEBUG(' After calling pvt function Populate_Drop_type ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,9);
4991             END IF;
4992 
4993             IF l_rec_count = -1 THEN -- l_rec_count check
4994               l_progress := '330';
4995               IF (l_debug = 1) THEN
4996                 DEBUG('Populate_Drop_type returned error and hence raising exception ',l_proc_name,1);
4997               END IF;
4998 
4999               l_progress := '340';
5000               RAISE fnd_api.g_exc_error;
5001             END IF; -- l_rec_count check
5002 
5003             l_progress := '350';
5004          EXCEPTION
5005            WHEN OTHERS THEN
5006               RAISE fnd_api.g_exc_unexpected_error;
5007          END;--Populating the drop type
5008 
5009 --      END IF; --p_item_drop_flag check
5010 
5011 
5012         -- Call the function which will mark the consolidated LPN and the drop_order
5013         -- This function will be called only for those LPNs which have drop_type as 'CD'
5014         BEGIN  -- Mark Consolidated LPN
5015 
5016            l_progress := '400';
5017            OPEN c_lpn_cursor;
5018            l_progress := '410';
5019 
5020            LOOP -- c_lpn_cursor
5021 
5022              --BUG 5187983: Requery c_lpn_cursor everytime
5023              --so that it will won't pick up the same lines
5024              --already marked by Mark_Consolidated_LPN
5025              l_lpn_id_tab.delete;
5026              l_lpn_name_tab.delete;
5027              l_lpn_level_tab.delete;
5028 
5029              FETCH   c_lpn_cursor
5030                INTO  l_lpn_id_tab(1)
5031                     ,l_lpn_name_tab(1)
5032                     ,l_lpn_level_tab(1);
5033 
5034              EXIT WHEN c_lpn_cursor%NOTFOUND;
5035              l_progress := '405';
5036 
5037              CLOSE c_lpn_cursor;
5038 
5039              IF (l_debug = 1) Then
5040                 DEBUG('COUNT:'||l_lpn_id_tab.COUNT,l_proc_name,9);
5041                 DEBUG('LPN_ID:'||l_lpn_id_tab(1));
5042                 DEBUG('LPN_NAME:'||l_lpn_name_tab(1));
5043                 DEBUG('LPN_LEVEL:'||l_lpn_level_tab(1));
5044              END IF;
5045 
5046              l_progress := '460';
5047 
5048              IF (l_debug = 1) THEN
5049                DEBUG(' Before calling pvt function Mark_Consolidated_LPN ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,9);
5050              END IF;
5051 
5052              -- This function call will group the tasks and populate the drop type correctly
5053              -- After this function call, the temp table will have the tasks grouped correctly.
5054              l_progress := '470';
5055              l_rec_count := Mark_Consolidated_LPN( p_lpn_id     => l_lpn_id_tab(1)
5056                                                   ,p_lpn_name   => l_lpn_name_tab(1)
5057                                                  );
5058              l_progress := '480';
5059 
5060              IF (l_debug = 1) THEN
5061                 DEBUG(' After calling pvt function Mark_Consolidated_LPN ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,9);
5062              END IF;
5063 
5064              l_progress := '490';
5065 
5066              IF l_rec_count = -1 THEN -- l_rec_count check
5067                 l_progress := '500';
5068                 IF (l_debug = 1) THEN
5069                    DEBUG('Mark_Consolidated_LPN returned error and hence raising exception ',l_proc_name,1);
5070                 END IF;
5071 
5072                 l_progress := '510';
5073                 RAISE fnd_api.g_exc_error;
5074              END IF; -- l_rec_count check
5075 
5076              OPEN c_lpn_cursor;
5077              -- END BUG 5187983
5078            END LOOP; -- c_lpn_cursor
5079 
5080            l_progress := '520';
5081            CLOSE c_lpn_cursor;
5082            l_progress := '525';
5083 
5084 
5085         EXCEPTION
5086           WHEN OTHERS THEN
5087              RAISE fnd_api.g_exc_unexpected_error;
5088         END;-- Mark Consolidated LPN
5089 
5090 
5091         BEGIN --Group_Item_Drop_Tasks
5092 
5093             -- Calling the method to insert the consolidate group tasks
5094             l_rec_count := Group_Item_Drop_Tasks;
5095             l_progress := '620';
5096 
5097             IF l_rec_count = -1 THEN -- l_rec_count check
5098               l_progress := '630';
5099               IF (l_debug = 1) THEN
5100                 DEBUG('Group_Item_Drop_Tasks returned error and hence raising exception ',l_proc_name,1);
5101               END IF;
5102 
5103               l_progress := '640';
5104               RAISE fnd_api.g_exc_error;
5105             END IF; -- l_rec_count check
5106 
5107             l_progress := '650';
5108 
5109         EXCEPTION
5110           WHEN OTHERS THEN
5111              RAISE fnd_api.g_exc_unexpected_error;
5112         END;--Group_Item_Drop_Tasks
5113 
5114 
5115 
5116         BEGIN --Group_Consolidated_Drop_Tasks
5117 
5118             -- Calling the method to insert the consolidate group tasks
5119             l_rec_count := Group_Consolidated_Drop_Tasks;
5120             l_progress := '720';
5121 
5122             IF l_rec_count = -1 THEN -- l_rec_count check
5123               l_progress := '730';
5124               IF (l_debug = 1) THEN
5125                 DEBUG('Group_Consolidated_Drop_Tasks returned error and hence raising exception ',l_proc_name,1);
5126               END IF;
5127 
5128               l_progress := '740';
5129               RAISE fnd_api.g_exc_error;
5130             END IF; -- l_rec_count check
5131 
5132             l_progress := '750';
5133 
5134         EXCEPTION
5135           WHEN OTHERS THEN
5136              RAISE fnd_api.g_exc_unexpected_error;
5137         END;--Group_Consolidated_Drop_Tasks
5138 
5139 
5140         BEGIN --Sync_Group_Tasks
5141 
5142             -- Calling the method to insert common transaction_header_id and group_id for a group
5143             l_rec_count := Sync_Group_Tasks;
5144             l_progress := '820';
5145 
5146             IF l_rec_count = -1 THEN -- l_rec_count check
5147               l_progress := '830';
5148               IF (l_debug = 1) THEN
5149                 DEBUG('Sync_Group_Tasks returned error and hence raising exception ',l_proc_name,1);
5150               END IF;
5151 
5152               l_progress := '840';
5153               RAISE fnd_api.g_exc_error;
5154             END IF; -- l_rec_count check
5155 
5156             l_progress := '850';
5157 
5158         EXCEPTION
5159           WHEN OTHERS THEN
5160              RAISE fnd_api.g_exc_unexpected_error;
5161         END;--Sync_Group_Tasks
5162 
5163 
5164       -- Done with the grouping logic, so go ahead and delete the dummy rows
5165       BEGIN --Delete dummy rows
5166 
5167         l_progress := '940';
5168         -- Delete all the rows in the temp table
5169         l_del_count := Remove_Tasks_In_Group(
5170                           p_del_type  =>  2
5171                         );
5172         l_progress := '950';
5173 
5174       EXCEPTION
5175         WHEN OTHERS THEN
5176            NULL;
5177       END; --Delete dummy rows
5178 
5179       l_progress := '960';
5180       -- Building the temp table is successful.. go ahead and commit the changes
5181       COMMIT;
5182       l_progress := '965';
5183 
5184     -- Lock all the LPNs the user is currently working on
5185 	 -- We are re-locking again since the commit above would have relieved the lock.
5186 	 Lock_LPNs( x_return_status  => x_return_status
5187 		    ,x_msg_count      => x_msg_count
5188 		    ,x_msg_data       => x_msg_data );
5189 
5190 	 l_progress := '970';
5191 
5192 	 IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
5193 	    IF (l_debug = 1) THEN
5194 	       DEBUG(' Error in locking LPNs ' || x_msg_data ,l_proc_name,1);
5195 	    END IF;
5196 
5197 	    l_progress := '980';
5198 	    RAISE fnd_api.g_exc_error;
5199 	 END IF;
5200 
5201 
5202 	 IF (l_debug = 1) THEN
5203 	    DEBUG(' x_return_status => '|| x_return_status,l_proc_name,4);
5204 	    DEBUG(' x_msg_count     => '|| x_msg_count,l_proc_name,4);
5205 	    DEBUG(' x_msg_data      => '|| x_msg_data,l_proc_name,4);
5206 	    DEBUG(' Procedure Exited at ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
5207 	 END IF;
5208 
5209       l_progress := '1000';
5210 
5211    EXCEPTION
5212      WHEN fnd_api.g_exc_error THEN
5213        x_return_status  := fnd_api.g_ret_sts_error;
5214        fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
5215        DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
5216        DEBUG(SQLERRM,l_proc_name,1);
5217 
5218        IF c_lpn_cursor%isopen THEN
5219          CLOSE c_lpn_cursor;
5220        END IF;
5221 
5222      WHEN fnd_api.g_exc_unexpected_error THEN
5223        x_return_status  := fnd_api.g_ret_sts_unexp_error;
5224        fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
5225        DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
5226        DEBUG(SQLERRM,l_proc_name,1);
5227 
5228        IF c_lpn_cursor%isopen THEN
5229          CLOSE c_lpn_cursor;
5230        END IF;
5231 
5232 
5233      WHEN OTHERS THEN
5234        x_return_status  := fnd_api.g_ret_sts_unexp_error;
5235        fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
5236        DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
5237        DEBUG(SQLERRM,l_proc_name,1);
5238 
5239        IF c_lpn_cursor%isopen THEN
5240          CLOSE c_lpn_cursor;
5241        END IF;
5242 
5243 
5244    END Create_grouped_tasks; -- Procedure Create_grouped_tasks
5245 
5246 
5247 /**
5248   *   This procedure will explode the LPN passed and will get all the MMTTs
5249   *   for its contents  (including childs contents) and will call the ATF API to
5250   *   Abort the operation instance.
5251   *
5252   *   The parameter p_call_type will decide what ATF API to call
5253   *   p_call_type   Gloabl Constant      Meaning
5254   *   1             G_ATF_ACTIVATE_PLAN  Activate Plan
5255   *   2             G_ATF_ABORT_PLAN     Abort Plan
5256   *
5257   *  @param  p_call_type      Mode for which the procedure is called
5258   *  @param  p_org_id         Organization Identifier
5259   *  @param  p_lpn_id         LPN identifier
5260   *  @param  p_emp_id         Employee identifier
5261   *
5262   *  @param  x_return_status  Return status of the function - Success, Error, Unexpected Error, Warning etc.,
5263   *  @param  x_msg_count      Count of messages in the stack
5264   *  @param  x_msg_data       Actual message if the count = 1 else it will be null.
5265   *
5266 **/
5267 
5268    -- In case of Manual/User Drop the ATF API's will be still used to create WDTs and WDTH.
5269    -- Before that the existing WDTs, Plans etc has to be deleted and hence Abort plan has to be called first.
5270    PROCEDURE ATF_For_Manual_Drop(
5271               x_return_status  OUT NOCOPY  VARCHAR2
5272              ,x_msg_count      OUT NOCOPY  NUMBER
5273              ,x_msg_data       OUT NOCOPY  VARCHAR2
5274              ,p_call_type      IN          NUMBER
5275              ,p_org_id         IN          NUMBER
5276              ,p_lpn_id         IN          NUMBER
5277              ,p_emp_id         IN          NUMBER
5278              ) IS
5279      l_debug     NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
5280      l_progress  VARCHAR2(10) := '10';
5281      l_rec_count NUMBER       := 0;
5282      l_proc_name VARCHAR2(30) := 'ATF_For_Manual_Drop:';
5283 
5284      l_error_code         NUMBER;
5285      l_tempid_tab         num_tab;
5286      l_task_rec           task_rec   := NULL;
5287 
5288      l_consolidation_method_id NUMBER;
5289      l_drop_lpn_option NUMBER;
5290 
5291      -- This cursor will get the MMTTs assoicated with the contents of the LPN passed
5292      -- along with the MMTTs of the contents of all its child LPNs also.
5293       --Bug5723418.Performance fix.Moved wlpn to FROM clause.
5294       -- Performane fix as a part of bug 7143123
5295      CURSOR c_all_mmtt_cursor IS
5296        SELECT /*+ ORDERED USE_NL(WLPN MTRL MMTT) INDEX(MTRL MTL_TXN_REQUEST_LINES_N7) */
5297 		MMTT.TRANSACTION_TEMP_ID
5298 	FROM
5299 	      ( SELECT LPN_ID
5300 		FROM WMS_LICENSE_PLATE_NUMBERS START WITH LPN_ID = p_lpn_id CONNECT BY PRIOR LPN_ID = PARENT_LPN_ID ) WLPN,
5301 	      MTL_TXN_REQUEST_LINES MTRL,
5302 	      MTL_MATERIAL_TRANSACTIONS_TEMP MMTT
5303 	WHERE MTRL.LINE_ID = MMTT.MOVE_ORDER_LINE_ID
5304 	  AND LINE_STATUS = 7
5305 	  AND MTRL.LPN_ID = WLPN.LPN_ID ;
5306 
5307 
5308    BEGIN
5309 
5310      IF (l_debug = 1) THEN
5311         DEBUG('Start of function ' || l_proc_name ,l_proc_name,9);
5312      END IF;
5313 
5314      -- Set the return status to success.
5315      l_progress := '20';
5316      x_return_status  := fnd_api.g_ret_sts_success;
5317      l_progress := '30';
5318 
5319      -- Setting the values for the task record
5320      -- Thought this is for Drop, The existing WDTs if any for load would have been deleted
5321      -- and hence these records has to be created afresh.
5322      l_task_rec.person_id       := p_emp_id;
5323      l_task_rec.organization_id := p_org_id;
5324      l_task_rec.user_task_type  := -1;
5325 
5326 
5327      l_progress := '100';
5328      OPEN c_all_mmtt_cursor;
5329      l_progress := '110';
5330 
5331      LOOP -- c_all_tasks_cursor loop
5332 
5333        EXIT WHEN c_all_mmtt_cursor%NOTFOUND;
5334        l_progress := '200';
5335 
5336        FETCH c_all_mmtt_cursor
5337          BULK COLLECT
5338           INTO l_tempid_tab
5339          LIMIT l_limit;
5340        l_progress := '210';
5341 
5342        l_rec_count := l_rec_count + l_tempid_tab.COUNT;
5343        l_progress := '220';
5344 
5345        -- Activate each MMTT's
5346        FOR i IN 1 .. l_tempid_tab.COUNT
5347        LOOP --All temp ids loop
5348 
5349          -- Check the mode for which the integration API is called
5350          IF p_call_type = G_ATF_ABORT_PLAN THEN
5351 
5352           IF (l_debug = 1) THEN
5353              DEBUG('Calling abort_operation_instance with ...',l_proc_name,9);
5354              DEBUG('p_source_task_id    => ' || l_tempid_tab(i),l_proc_name,9);
5355              DEBUG('p_activity_type_id  => ' || G_OP_ACTIVITY_INBOUND,l_proc_name,9);
5356           END IF;
5357           l_progress := '300';
5358 
5359 	  --BUG 3666138: Pass in 1 more parameter, p_for_manual_drop, so
5360 	  --that ATF would call revert_loc_sugg_capacity with an
5361 	  --autonomous commit
5362           wms_atf_runtime_pub_apis.Abort_Operation_Plan
5363              (  p_source_task_id    => l_tempid_tab(i)
5364                ,p_activity_type_id  => G_OP_ACTIVITY_INBOUND
5365                ,x_return_status     => x_return_status
5366                ,x_msg_data          => x_msg_data
5367                ,x_msg_count         => x_msg_count
5368 	       ,x_error_code        => l_error_code
5369 	       ,p_for_manual_drop   => true
5370              );
5371 
5372           l_progress := '310';
5373 
5374           IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
5375              IF (l_debug = 1) THEN
5376                 DEBUG(l_error_code || ' Error in abort_operation_instance ' ,l_proc_name,1);
5377              END IF;
5378              l_progress := '320';
5379 
5380              RAISE fnd_api.g_exc_error;
5381           END IF;
5382 
5383 	 ELSIF p_call_type = g_atf_cancel_plan THEN
5384             IF (l_debug = 1) THEN
5385                DEBUG('Calling cancel_operation_plan with ...',l_proc_name,9);
5386                DEBUG('p_source_task_id    => ' || l_tempid_tab(i),l_proc_name,9);
5387                DEBUG('p_activity_type_id  => ' || G_OP_ACTIVITY_INBOUND,l_proc_name,9);
5388             END IF;
5389             l_progress := '330';
5390 
5391             wms_atf_runtime_pub_apis.cancel_operation_plan
5392                (  p_source_task_id    => l_tempid_tab(i)
5393                  ,p_activity_type_id  => G_OP_ACTIVITY_INBOUND
5394                  ,x_return_status     => x_return_status
5395                  ,x_msg_data          => x_msg_data
5396                  ,x_msg_count         => x_msg_count
5397                  ,x_error_code        => l_error_code
5398 		  );
5399             l_progress := '340';
5400 
5401             IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
5402                IF (l_debug = 1) THEN
5403                   DEBUG(l_error_code || ' Error in cancel_operation_plan ' ,l_proc_name,1);
5404                END IF;
5405                l_progress := '350';
5406 
5407                RAISE fnd_api.g_exc_error;
5408             END IF;
5409          ELSIF p_call_type = G_ATF_ACTIVATE_PLAN  THEN
5410             IF (l_debug = 1) THEN
5411                DEBUG('Calling activate_operation_instance with ...',l_proc_name,9);
5412                DEBUG('p_source_task_id    => ' || l_tempid_tab(i),l_proc_name,9);
5413                DEBUG('p_activity_type_id  => ' || G_OP_ACTIVITY_INBOUND,l_proc_name,9);
5414                DEBUG('p_task_execute_rec  => l_task_rec',l_proc_name,9);
5415                DEBUG('p_operation_type_id => ' || G_OP_TYPE_DROP,l_proc_name,9);
5416             END IF;
5417             l_progress := '330';
5418 
5419             wms_atf_runtime_pub_apis.activate_operation_instance
5420                (  p_source_task_id    => l_tempid_tab(i)
5421                  ,p_activity_id       => G_OP_ACTIVITY_INBOUND
5422                  ,p_task_execute_rec  => l_task_rec
5423                  ,p_operation_type_id => G_OP_TYPE_DROP
5424                  ,x_return_status     => x_return_status
5425                  ,x_msg_data          => x_msg_data
5426                  ,x_msg_count         => x_msg_count
5427                  ,x_error_code        => l_error_code
5428 		 ,x_consolidation_method_id => l_consolidation_method_id
5429 		 ,x_drop_lpn_option   => l_drop_lpn_option
5430 		  );
5431             l_progress := '340';
5432 
5433             IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
5434                IF (l_debug = 1) THEN
5435                   DEBUG(l_error_code || ' Error in activate_operation_instance ' ,l_proc_name,1);
5436                END IF;
5437                l_progress := '350';
5438 
5439                RAISE fnd_api.g_exc_error;
5440             END IF;
5441 
5442          ELSE
5443            -- Invalid value for mode, return error
5444            RAISE fnd_api.g_exc_error;
5445 
5446          END IF; -- Check the mode for which the integration API is called
5447 
5448 
5449        END LOOP; --All temp ids loop
5450 
5451      END LOOP; -- c_all_tasks_cursor loop
5452 
5453      -- Close all the open cursors if any.
5454      IF c_all_mmtt_cursor%ISOPEN THEN
5455        l_progress := '500';
5456        CLOSE c_all_mmtt_cursor;
5457      END IF;
5458 
5459 
5460    EXCEPTION
5461 
5462      WHEN fnd_api.g_exc_error THEN
5463        x_return_status  := fnd_api.g_ret_sts_error;
5464        fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
5465        DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
5466        DEBUG(SQLERRM,l_proc_name,1);
5467 
5468        IF c_all_mmtt_cursor%isopen THEN
5469          CLOSE c_all_mmtt_cursor;
5470        END IF;
5471 
5472      WHEN fnd_api.g_exc_unexpected_error THEN
5473        x_return_status  := fnd_api.g_ret_sts_unexp_error;
5474        fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
5475        DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
5476        DEBUG(SQLERRM,l_proc_name,1);
5477 
5478        IF c_all_mmtt_cursor%isopen THEN
5479          CLOSE c_all_mmtt_cursor;
5480        END IF;
5481 
5482 
5483      WHEN OTHERS THEN
5484        x_return_status  := fnd_api.g_ret_sts_unexp_error;
5485        fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
5486        DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
5487        DEBUG(SQLERRM,l_proc_name,1);
5488 
5489        IF c_all_mmtt_cursor%isopen THEN
5490          CLOSE c_all_mmtt_cursor;
5491        END IF;
5492 
5493    END ATF_For_Manual_Drop;
5494 
5495 
5496 
5497 /**
5498   *   This function will call the ATF API to activate the operation instance for inspect.
5499   *   It will call the ATF API  wms_atf_runtime_pub_apis.activate_operation_instance for
5500   *   each MMTT. For the Move Order Line passed.
5501   *
5502   *   ATF will check whether the current step. If it is not inspect, it will inturn call
5503   *   the Abort operation instance to abort the current plan in progress. So we need not
5504   *   call Abort Explicitly in this case.
5505   *
5506   *   It returns the number of rows for which the plan is
5507   *   activated, will return -1 in case of failure.
5508 
5509   *  @param  x_return_status  Return status of the function - Success, Error, Unexpected Error, Warning etc.,
5510   *  @param  x_msg_count      Count of messages in the stack
5511   *  @param  x_msg_data       Actual message if the count = 1 else it will be null.
5512   *  @param   p_org_id          Organization Identifier
5513   *  @param   p_lpn_id          LPN Identifier
5514   *  @param   p_emp_id          Employee Identifier
5515   *
5516   *  @ RETURN  NUMBER
5517 
5518 
5519 **/
5520    FUNCTION Activate_Plan_For_Inspect(
5521               x_return_status  OUT NOCOPY  VARCHAR2
5522              ,x_msg_count      OUT NOCOPY  NUMBER
5523              ,x_msg_data       OUT NOCOPY  VARCHAR2
5524              ,p_org_id         IN          NUMBER
5525              ,p_mo_line_id     IN           NUMBER )
5526      RETURN NUMBER
5527      IS
5528 
5529      l_debug     NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
5530      l_progress  VARCHAR2(10) := '10';
5531      l_rec_count NUMBER       := 0;
5532      l_proc_name VARCHAR2(30) := 'Activate_Plan_For_Inspect:';
5533 
5534      l_error_code    NUMBER;
5535      l_task_rec      task_rec   := NULL;
5536      l_tempid_tab    num_tab;
5537      l_user_id       NUMBER   := fnd_global.user_id;
5538      l_emp_id        NUMBER;
5539 
5540      l_consolidation_method_id NUMBER;
5541      l_drop_lpn_option NUMBER;
5542 
5543      -- This cursor will get the MMTTs assoicated with the move order line passed
5544      CURSOR c_mol_mmtt_cursor IS
5545        SELECT mmtt.transaction_temp_id
5546        FROM   mtl_material_transactions_temp mmtt
5547              ,mtl_txn_request_lines mtrl
5548        WHERE mtrl.line_id = mmtt.move_order_line_id
5549              AND mtrl.line_id = p_mo_line_id
5550              AND mtrl.line_status = 7
5551              AND mtrl.organization_id = p_org_id;
5552 
5553    BEGIN
5554 
5555      IF (l_debug = 1) THEN
5556         DEBUG('Start of function ' || l_proc_name ,l_proc_name,9);
5557         DEBUG('  p_org_id     ==> '|| p_org_id,l_proc_name,4);
5558         DEBUG('  p_mo_line_id ==> '|| p_mo_line_id,l_proc_name,4);
5559      END IF;
5560 
5561      -- Set the return status to success.
5562      l_progress := '20';
5563      x_return_status  := fnd_api.g_ret_sts_success;
5564      l_progress := '30';
5565 
5566     -- Get the employee ID so we can populate
5567     -- the person_id column in WDT properly.
5568     BEGIN -- Get the employee id
5569 
5570       l_progress := '30';
5571       SELECT NVL(employee_id, -1)
5572       INTO   l_emp_id
5573       FROM   fnd_user
5574       WHERE  user_id = l_user_id;
5575       l_progress := '40';
5576 
5577       IF (l_debug = 1) THEN
5578          DEBUG(' Got the employee id =  '|| l_emp_id || ' for the user ' || l_user_id,l_proc_name,9);
5579       END IF;
5580 
5581     EXCEPTION
5582       WHEN NO_DATA_FOUND THEN
5583         DEBUG('Couldnot get the employee id and hence setting it to null ' ,l_proc_name,1);
5584         l_emp_id := NULL;
5585         l_progress := '50';
5586 
5587     END; -- Get the employee id
5588     l_progress := '60';
5589 
5590 
5591      -- Setting the values for the task record
5592      -- Neednot set these values for drop since there ia already WDT,
5593      -- ATF will just change the status of this WDT record
5594      l_task_rec.person_id       := l_emp_id;
5595      l_task_rec.organization_id := p_org_id;
5596      l_task_rec.user_task_type  := -1;
5597 
5598      l_progress := '100';
5599      OPEN c_mol_mmtt_cursor;
5600      l_progress := '110';
5601 
5602      LOOP -- c_all_mmtt_cursor loop
5603 
5604        EXIT WHEN c_mol_mmtt_cursor%NOTFOUND;
5605        l_progress := '200';
5606 
5607        FETCH c_mol_mmtt_cursor
5608          BULK COLLECT
5609           INTO l_tempid_tab
5610          LIMIT l_limit;
5611        l_progress := '210';
5612 
5613        l_rec_count := l_rec_count + l_tempid_tab.COUNT;
5614        l_progress := '220';
5615 
5616        -- Activate each MMTT's
5617        FOR i IN 1 .. l_tempid_tab.COUNT
5618        LOOP --All temp ids loop
5619 
5620             IF (l_debug = 1) THEN
5621                DEBUG('Calling activate_operation_instance with ...',l_proc_name,9);
5622                DEBUG('p_source_task_id    => ' || l_tempid_tab(i),l_proc_name,9);
5623                DEBUG('p_activity_type_id  => ' || G_OP_ACTIVITY_INBOUND,l_proc_name,9);
5624                DEBUG('p_task_execute_rec  => l_task_rec',l_proc_name,9);
5625                DEBUG('p_operation_type_id => ' || G_OP_TYPE_DROP,l_proc_name,9);
5626             END IF;
5627           l_progress := '300';
5628 
5629             wms_atf_runtime_pub_apis.activate_operation_instance
5630                (  p_source_task_id    => l_tempid_tab(i)
5631                  ,p_activity_id       => G_OP_ACTIVITY_INBOUND
5632                  ,p_task_execute_rec  => l_task_rec
5633                  ,p_operation_type_id => G_OP_TYPE_INSPECT
5634                  ,x_return_status     => x_return_status
5635                  ,x_msg_data          => x_msg_data
5636                  ,x_msg_count         => x_msg_count
5637                  ,x_error_code        => l_error_code
5638 		 ,x_consolidation_method_id => l_consolidation_method_id
5639 		 ,x_drop_lpn_option   => l_drop_lpn_option
5640                );
5641           l_progress := '310';
5642 
5643             IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
5644                IF (l_debug = 1) THEN
5645                   DEBUG(l_error_code || ' Error in activate_operation_instance ' ,l_proc_name,1);
5646                END IF;
5647              l_progress := '320';
5648 
5649                RAISE fnd_api.g_exc_error;
5650             END IF;
5651 
5652        END LOOP; --All temp ids loop
5653 
5654      END LOOP; -- c_all_mmtt_cursor loop
5655 
5656 
5657      IF c_mol_mmtt_cursor%ISOPEN THEN
5658        CLOSE c_mol_mmtt_cursor;
5659      END IF;
5660 
5661      l_progress := '990';
5662      --Return the count of rows processed
5663      RETURN l_rec_count;
5664 
5665    EXCEPTION
5666      WHEN fnd_api.g_exc_error THEN
5667        x_return_status  := fnd_api.g_ret_sts_error;
5668        fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
5669        DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
5670        DEBUG(SQLERRM,l_proc_name,1);
5671 
5672        IF c_mol_mmtt_cursor%ISOPEN THEN
5673          CLOSE c_mol_mmtt_cursor;
5674        END IF;
5675 
5676        RETURN -1;
5677 
5678 
5679      WHEN fnd_api.g_exc_unexpected_error THEN
5680        x_return_status  := fnd_api.g_ret_sts_unexp_error;
5681        fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
5682 
5683        DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
5684        DEBUG(SQLERRM,l_proc_name,1);
5685 
5686        IF c_mol_mmtt_cursor%ISOPEN THEN
5687          CLOSE c_mol_mmtt_cursor;
5688        END IF;
5689 
5690        RETURN -1;
5691 
5692 
5693      WHEN OTHERS THEN
5694        x_return_status  := fnd_api.g_ret_sts_unexp_error;
5695        fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
5696        DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
5697        DEBUG(SQLERRM,l_proc_name,1);
5698 
5699        IF c_mol_mmtt_cursor%ISOPEN THEN
5700          CLOSE c_mol_mmtt_cursor;
5701        END IF;
5702 
5703        RETURN -1;
5704 
5705    END Activate_Plan_For_Inspect;
5706 
5707 
5708 /**
5709   *   This function will call the ATF API to cleanup / rollback
5710   *   the operation instance for load / drop / single step drop.
5711   *
5712   *   It will call the ATF APIs for each MMTT in the temp table
5713   *   if group_id is not passed else it will call ATF APIs only
5714   *   for that group in the temp table.
5715   *
5716   *   It returns the number of rows for which the plan is
5717   *   cleanedup / rolled back, will return -1 in case of failure.
5718   *
5719   *   The parameter p_call_type will decide what ATF API to call
5720   *   p_call_type   Gloabl Constant         Meaning
5721   *   ----------------------------------------------------------
5722   *   1             G_CT_NORMAL_LOAD        Called from Load scenario (Manual Load)
5723   *   2             G_CT_NORMAL_DROP        Called from Drop (LPN already loaded)
5724   *   3             G_CT_SINGLE_STEP_DROP   Called from Drop (LPN already loaded)
5725   *   4             G_CT_INSPECT_TM_FAILED  Called from Inspect when it is after tm failure
5726   *   5             G_CT_INSPECT_B4_TM      Called from Inspect when it is before calling TM
5727   *
5728   *   if p_call_type = 3, Rollback_operation_plan will be called
5729   *   else  Cleanup_Operation_Instance will be called.
5730   *
5731   *
5732   *  @param  x_return_status   Return status of the function - Success, Error, Unexpected Error,
5733   *                            Warning etc.,
5734   *  @param  x_msg_count       Count of messages in the stack
5735   *  @param  x_msg_data        Actual message if the count = 1 else it will be null.
5736   *  @param  p_org_id          Organization Identifier
5737   *  @param  p_call_type       Whether cleanup is called for load, drop or single step drop
5738   *  @param  p_lpn_id          LPN Identifier
5739   *  @param  p_group_id        Group ID for which cleanup has to be called.
5740   *                            Can have following values:
5741   *                            -1: Called when one of the tasks failed and
5742   *                                user decided to quit all tasks
5743   *                            -2: Called at the end of all tasks to clean up
5744   *                                task split during partial drop
5745   *                            NULL: Called in cases of Manual Drop, Inspection,
5746   *                                  Manual Load
5747   *                            Others: Called when one of the tasks failed and
5748   *                                    user wants to cancel just this task
5749   *  @ RETURN  NUMBER
5750 
5751 
5752 **/
5753    PROCEDURE Cleanup_ATF(
5754           x_return_status OUT  NOCOPY  VARCHAR2
5755          ,x_msg_count     OUT  NOCOPY  NUMBER
5756          ,x_msg_data      OUT  NOCOPY  VARCHAR2
5757          ,p_org_id        IN           NUMBER
5758          ,p_call_type     IN           NUMBER
5759          ,p_lpn_id        IN           NUMBER
5760          ,p_group_id      IN           NUMBER
5761 	 ,p_parent_lpn_id IN           NUMBER
5762 	 ,p_drop_all      IN           VARCHAR2 --BUG 5075410
5763 	 ,p_emp_id        IN           NUMBER)  --BUG 5075410
5764      IS
5765      l_debug     NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
5766      l_progress  VARCHAR2(10) := '10';
5767      l_rec_count NUMBER       := 0;
5768      l_proc_name VARCHAR2(30) := 'Cleanup_ATF:';
5769 
5770      l_error_code    NUMBER;
5771      l_tempid_tab    num_tab;
5772 
5773      l_op_plan_id       NUMBER;
5774      l_lpn_context      NUMBER;
5775          l_lpn_cxt          NUMBER;
5776          l_parent_lpn_cxt   NUMBER;
5777      l_mmtt_item_id     NUMBER;
5778      l_mmtt_uom         VARCHAR2(3);
5779      l_mmtt_qty         NUMBER;
5780      l_mol_id           NUMBER;
5781      l_mol_uom          VARCHAR2(3);
5782      l_mmtt_qty_mol_uom NUMBER;
5783      l_wms_process_flag NUMBER;
5784      l_outermost_lpn_id NUMBER;
5785 
5786      -- This cursor will get the all the MMTTs assoicated with the lpn_id passed.
5787      -- Cleanup shouldn't be called if there is not WDT and hence added a join with WDT.
5788      CURSOR c_mol_mmtt_cursor IS
5789        SELECT mmtt.transaction_temp_id
5790        FROM   mtl_material_transactions_temp mmtt
5791              ,mtl_txn_request_lines mtrl
5792              ,wms_dispatched_tasks wdt
5793 	     ,(SELECT lpn_id     FROM   wms_license_plate_numbers   /*Bug5723418.*/
5794                                  START  WITH lpn_id = p_lpn_id
5795                                  CONNECT BY PRIOR lpn_id = parent_lpn_id
5796                                 ) wlpn
5797        WHERE wdt.transaction_temp_id = mmtt.transaction_temp_id
5798 	 --  BUG 5075410: WDT MOL is not being kept in sync when split.
5799 	 --  Querying through WDT->MMTT->MTRL should be enough
5800 	 --  AND wdt.move_order_line_id = mtrl.line_id
5801              AND mtrl.line_id = mmtt.move_order_line_id
5802              AND mtrl.line_status = 7
5803              AND mtrl.organization_id = p_org_id
5804              AND mtrl.lpn_id = wlpn.lpn_id;
5805 
5806 
5807      -- This cursor will get the MMTTs assoicated with the group_id passed from the temp table
5808      CURSOR c_tmp_mmtt_cursor IS
5809        SELECT transaction_temp_id
5810        FROM   wms_putaway_group_tasks_gtmp
5811        WHERE  group_id = p_group_id
5812 	 AND row_type = G_ROW_TP_ALL_TASK;
5813 
5814      --BUG 5075410: The c_all_mmtt_cursor is used in the following scenarios:
5815      --1) When user finishes all tasks (p_group_id = -2), system only needs
5816      --   to cleanup the tasks split when User performed partial. So join
5817      --   on task_type/wms_process_flag/transaction_status so that the txn
5818      --   submitted from transaction is picked up
5819      --2) When user presses F2 (p_group_id = -1)
5820      --3) When user encounter an error, and he decides to quit all tasks
5821      --   (p_group_id = -1).  In this cursor, select all the tasks that
5822      --   have drop active op plan or tasks that have wdt status 4 (loaded).
5823      CURSOR c_all_mmtt_cursor IS
5824 	SELECT mmtt.transaction_temp_id
5825 	  FROM wms_dispatched_tasks     wdt
5826 	  ,mtl_txn_request_lines    mtrl
5827 	  ,mtl_material_transactions_temp mmtt
5828 	  ,mtl_txn_request_headers  mtrh
5829 	  WHERE  mtrh.header_id = mtrl.header_id
5830 	  AND mtrh.move_order_type = 6
5831 	  AND mtrl.line_status = 7
5832 	  AND wdt.transaction_temp_id = mmtt.transaction_temp_id
5833 	  AND mmtt.move_order_line_id = mtrl.line_id
5834 	  AND ((p_group_id = -2
5835 		AND Nvl(mtrl.wms_process_flag,1) <> 2   --RCV would set this to 2 for mol being processed
5836 		AND Nvl(mmtt.wms_task_type,2) <> -1     --RCV would set this to -1 for MMTT being processed
5837 		AND Nvl(mmtt.transaction_status,2) <> 3 --WIP would set this to 3 for MMTT being processed
5838 		) OR
5839 	       (p_group_id = -1))
5840 	  AND wdt.STATUS = 4
5841 	  AND wdt.task_type = 2
5842 	  AND mtrl.lpn_id IN (SELECT lpn_id
5843 			      FROM   wms_license_plate_numbers
5844 			      START  WITH lpn_id = l_outermost_lpn_id
5845 			      CONNECT BY PRIOR lpn_id = parent_lpn_id
5846 			      )
5847 	  AND ((mmtt.operation_plan_id IS NOT NULL
5848 		AND exists (SELECT 1
5849 			    FROM   wms_op_operation_instances wooi
5850 			    WHERE  wooi.source_task_id = mmtt.transaction_temp_id
5851 			    AND    wooi.operation_status = 2
5852 			    AND    wooi.operation_type_id = 2))
5853 	       OR
5854 	       (mmtt.operation_plan_id IS NULL))
5855 	  AND p_drop_all <> 'Y'
5856      UNION ALL
5857        SELECT mmtt.transaction_temp_id
5858 	  FROM wms_dispatched_tasks     wdt
5859 	  ,mtl_txn_request_lines    mtrl
5860 	  ,mtl_material_transactions_temp mmtt
5861 	  ,mtl_txn_request_headers  mtrh
5862 	  WHERE  mtrh.header_id = mtrl.header_id
5863 	  AND mtrh.move_order_type = 6
5864 	  AND mtrl.line_status = 7
5865 	  AND wdt.transaction_temp_id = mmtt.transaction_temp_id
5866 	  AND mmtt.move_order_line_id = mtrl.line_id
5867 	  AND ((p_group_id = -2
5868 		AND Nvl(mtrl.wms_process_flag,1) <> 2   --RCV would set this to 2 for mol being processed
5869 		AND Nvl(mmtt.wms_task_type,2) <> -1     --RCV would set this to -1 for MMTT being processed
5870 		AND Nvl(mmtt.transaction_status,2) <> 3 --WIP would set this to 3 for MMTT being processed
5871 		) OR
5872 	       (p_group_id = -1))
5873 	  AND wdt.task_type = 2
5874 	  AND wdt.STATUS = 4
5875 	  AND wdt.person_id = p_emp_id
5876 	  AND ((mmtt.operation_plan_id IS NOT NULL
5877 		AND exists (SELECT 1
5878 			    FROM   wms_op_operation_instances wooi
5879 			    WHERE  wooi.source_task_id = mmtt.transaction_temp_id
5880 			    AND    wooi.operation_status = 2
5881 			    AND    wooi.operation_type_id = 2))
5882 	       OR
5883 	       (mmtt.operation_plan_id IS NULL))
5884           AND p_drop_all = 'Y';
5885 
5886    BEGIN
5887 
5888       --Part of bugfix 4114695 - should initialize this variable so that
5889       --calls from java aren't mislead to think this failed when indeed
5890       --it was a clean exit.  Upon EXCEPTION, this flag is changedw
5891      x_return_status := fnd_api.g_ret_sts_success;
5892 
5893      IF (l_debug = 1) THEN
5894         DEBUG('Start of function ' || l_proc_name ,l_proc_name,9);
5895         DEBUG('  p_call_type ==> '|| p_call_type,l_proc_name,4);
5896         DEBUG('  p_lpn_id    ==> '|| p_lpn_id,l_proc_name,4);
5897         DEBUG('  p_group_id  ==> '|| p_group_id,l_proc_name,4);
5898         DEBUG('  p_parent_lpn_id  ==> '|| p_parent_lpn_id,l_proc_name,4);
5899         DEBUG('  p_drop_all       ==> '|| p_drop_all,l_proc_name,4);
5900         DEBUG('  p_emp_id         ==> '|| p_emp_id,l_proc_name,4);
5901      END IF;
5902 
5903 
5904      -- Check which cursor to open
5905      IF p_group_id IS NOT NULL THEN
5906        -- user has passed group id hence fetch from the temp table
5907        l_progress := '100';
5908 
5909        IF (p_group_id in (-1,-2)) THEN
5910 	  BEGIN
5911 	     SELECT outermost_lpn_id
5912 	       ,    lpn_context
5913 	       INTO l_outermost_lpn_id
5914 	       ,    l_lpn_context
5915 	       FROM wms_license_plate_numbers
5916 	       WHERE lpn_id = p_lpn_id;
5917 	  EXCEPTION
5918 	     WHEN OTHERS THEN
5919 		IF (l_debug = 1) THEN
5920 		   DEBUG('Error querying WLPN. SQLCODE:'||SQLCODE||' SQLERRM:'||SQLERRM,l_proc_name,9);
5921 		END IF;
5922 		RAISE fnd_api.g_exc_error;
5923 	  END;
5924 
5925 	  IF (l_debug = 1) THEN
5926 	     DEBUG('Outermost LPN_ID:'||l_outermost_lpn_id,l_proc_name,9);
5927 	  END IF;
5928 
5929 	  OPEN c_all_mmtt_cursor;
5930 	ELSE
5931 	  OPEN c_tmp_mmtt_cursor;
5932        END IF;
5933 
5934        l_progress := '110';
5935 
5936        IF (p_group_id IN (-1,-2)) THEN
5937 	  FETCH c_all_mmtt_cursor
5938 	    BULK COLLECT
5939 	    INTO l_tempid_tab;
5940 	ELSE
5941 	  FETCH c_tmp_mmtt_cursor
5942 	    BULK COLLECT
5943 	    INTO l_tempid_tab;
5944        END IF;
5945 
5946        l_progress := '120';
5947 
5948        IF (p_group_id IN (-1,-2)) THEN
5949 	  CLOSE c_all_mmtt_cursor;
5950 	ELSE
5951 	  CLOSE c_tmp_mmtt_cursor;
5952        END IF;
5953 
5954        l_progress := '130';
5955 
5956        l_rec_count := l_tempid_tab.COUNT;
5957        l_progress := '140';
5958 
5959      ELSIF p_lpn_id IS NOT NULL THEN
5960        -- user has not passed the group id but passed the lpn id hence fetch from MMTT/MTRL
5961 
5962        l_progress := '200';
5963        OPEN c_mol_mmtt_cursor;
5964        l_progress := '210';
5965 
5966        FETCH c_mol_mmtt_cursor
5967          BULK COLLECT
5968           INTO l_tempid_tab;
5969        l_progress := '220';
5970 
5971        CLOSE c_mol_mmtt_cursor;
5972        l_progress := '230';
5973 
5974        l_rec_count := l_tempid_tab.COUNT;
5975        l_progress := '240';
5976 
5977      ELSE
5978        -- Invalid inputs passed hence error out
5979        l_progress := '299';
5980        RAISE fnd_api.g_exc_error;
5981 
5982      END IF; -- Check which cursor to open
5983 
5984 
5985      IF p_call_type = G_CT_SINGLE_STEP_DROP THEN
5986        -- Single step drop hence we have to call Rollback_operation_plan
5987 
5988 	-- For single step drop, if the LPN is originally nested inside
5989 	-- another LPN, during simulation of load, it will get unpacked
5990 	-- from the outer LPN.  So we must pack it back during the cleanup
5991 	-- here.
5992         /* Start bug 9295496 */
5993         BEGIN
5994 
5995      SELECT lpn_context
5996      INTO   l_lpn_cxt
5997      FROM   wms_license_plate_numbers
5998      WHERE  lpn_id=  p_lpn_id ;
5999 
6000      IF p_parent_lpn_id is not null THEN
6001          SELECT lpn_context
6002      INTO   l_parent_lpn_cxt
6003      FROM   wms_license_plate_numbers
6004      WHERE  lpn_id= p_parent_lpn_id  ;
6005          END IF;
6006 
6007      EXCEPTION
6008      WHEN OTHERS THEN
6009      DEBUG('In exception block while fetching lpn_context:l_parent_lpn_cxt ' || Nvl(l_parent_lpn_cxt, -999) ||
6010                    ' l_lpn_cxt ' || Nvl(l_lpn_cxt, -999), l_proc_name,9);
6011      END;
6012          /* End bug 9295496 */
6013 
6014 	IF (Nvl(p_parent_lpn_id, 0) <> 0 AND Nvl(p_lpn_id, 0) <> 0 AND nvl(l_lpn_cxt,0)=nvl(l_parent_lpn_cxt,0)) THEN  -- bug 9295496 added condition
6015 	  IF (l_debug = 1) THEN
6016 	     DEBUG('Packing LPN ' || Nvl(p_lpn_id, -999) ||
6017 		   ' into Parent LPN ' || Nvl(p_parent_lpn_id, -999), l_proc_name,9);
6018 	  END IF;
6019 
6020 	  wms_container_pvt.packunpack_container
6021 	    (p_api_version       =>   1.0
6022 	     ,p_content_lpn_id   =>   p_lpn_id
6023 	     ,p_lpn_id           =>   p_parent_lpn_id
6024 	     ,p_operation        =>   1 -- Pack
6025 	     ,p_organization_id  =>   p_org_id
6026 	     ,x_return_status    =>   x_return_status
6027 	     ,x_msg_count        =>   x_msg_count
6028 	     ,x_msg_data         =>   x_msg_data
6029 	     );
6030 	  IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
6031 	     IF (l_debug = 1) THEN
6032 		DEBUG('Error packing LPN into original parent...', l_proc_name,9);
6033 	     END IF;
6034 	     RAISE fnd_api.g_exc_error;
6035 	  END IF;
6036        END IF;
6037 
6038        l_lpn_context := NULL;
6039 
6040        -- All MMTTs loop
6041        FOR i IN 1..l_tempid_tab.COUNT
6042        LOOP
6043 
6044            IF (l_debug = 1) THEN
6045               DEBUG('Calling Rollback_operation_plan with ...',l_proc_name,9);
6046               DEBUG('p_source_task_id    => ' || l_tempid_tab(i),l_proc_name,9);
6047               DEBUG('p_activity_type_id  => ' || G_OP_ACTIVITY_INBOUND,l_proc_name,9);
6048            END IF;
6049             l_progress := '300';
6050 
6051            wms_atf_runtime_pub_apis.Rollback_operation_plan
6052               (  p_source_task_id    => l_tempid_tab(i)
6053                 ,p_activity_type_id  => G_OP_ACTIVITY_INBOUND
6054                 ,x_return_status     => x_return_status
6055                 ,x_msg_data          => x_msg_data
6056                 ,x_msg_count         => x_msg_count
6057                 ,x_error_code        => l_error_code
6058               );
6059            l_progress := '310';
6060 
6061            IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
6062               IF (l_debug = 1) THEN
6063                  DEBUG(l_error_code || ' Error in Rollback_operation_plan ' ,l_proc_name,1);
6064               END IF;
6065               l_progress := '320';
6066               x_return_status  := fnd_api.g_ret_sts_error;
6067               /* Don't raise error in case of failure, instead set the ret status as error
6068                  and proceed with the next record */
6069               --RAISE fnd_api.g_exc_error;
6070            END IF;
6071 
6072 	    --Bug 5075410: If the cleanup is called
6073 	    --for a single step drop for an inventory lpn and there is
6074 	    --no operation plan associated with the lpn then, we should
6075 	    --delete the task and close the MOL.
6076 
6077 	    IF (l_debug = 1) THEN
6078 	       debug('Inside the begin, before selection from mmtt',l_proc_name,1);
6079 	    END IF;
6080 
6081 	    BEGIN
6082 	       SELECT mmtt.operation_plan_id
6083 		 ,    mmtt.transaction_uom
6084 		 ,    mmtt.transaction_quantity
6085 		 ,    mmtt.move_order_line_id
6086 		 ,    mmtt.inventory_item_id
6087 		 ,    mtrl.uom_code
6088 		 ,    mtrl.wms_process_flag
6089 		 INTO l_op_plan_id
6090 		 ,    l_mmtt_uom
6091 		 ,    l_mmtt_qty
6092 		 ,    l_mol_id
6093 		 ,    l_mmtt_item_id
6094 		 ,    l_mol_uom
6095 		 ,    l_wms_process_flag
6096 		 FROM mtl_material_transactions_temp mmtt
6097 		 ,    mtl_txn_request_lines mtrl
6098 		 WHERE mmtt.transaction_temp_id = l_tempid_tab(i)
6099 		 AND   mmtt.move_order_line_id = mtrl.line_id;
6100 	    EXCEPTION
6101 	       WHEN OTHERS THEN
6102 		  IF (l_debug = 1) THEN
6103 		     debug('Error querying op_plan_id of MMTT. SQLERRM:'||Sqlerrm,l_proc_name,1);
6104 		  END IF;
6105 		  RAISE fnd_api.g_exc_error;
6106 	    END;
6107 
6108 	    IF (l_lpn_context IS NULL) THEN
6109                BEGIN
6110 		  SELECT lpn_context
6111 		    INTO l_lpn_context
6112 		    FROM wms_license_plate_numbers
6113 		    WHERE lpn_id = p_lpn_id;
6114 	       EXCEPTION
6115 		  WHEN OTHERS THEN
6116 		     IF (l_debug = 1) THEN
6117 			debug('Error querying lpn_context of WLPN. SQLERRM:'||Sqlerrm,l_proc_name,1);
6118 		     END IF;
6119 		     RAISE fnd_api.g_exc_error;
6120 	       END;
6121 	    END IF;
6122 
6123 	    l_mmtt_qty_mol_uom := inv_convert.inv_um_convert
6124 	                             (item_id        =>  l_mmtt_item_id
6125 				      ,precision     =>  NULL
6126 				      ,from_quantity => l_mmtt_qty
6127 				      ,from_unit     => l_mmtt_uom
6128 				      ,to_unit       => l_mol_uom
6129 				      ,from_name =>  NULL
6130 				      ,to_name   =>  NULL
6131 				      );
6132 
6133 	    IF (l_debug = 1) THEN
6134 	       debug('l_op_plan_id:'||l_op_plan_id||
6135 		     ' l_lpn_context:'||l_lpn_context||
6136 		     ' l_mmtt_uom:'||l_mmtt_uom||
6137 		     ' l_mmtt_qty:'||l_mmtt_qty||
6138 		     ' l_mol_id:'||l_mol_id||
6139 		     ' l_mol_uom:'||l_mol_uom||
6140 		     ' l_mmtt_item_id:'||l_mmtt_item_id||
6141 		     ' l_mmtt_qty_mol_uom:'||l_mmtt_qty_mol_uom||
6142 		     ' l_wms_process_flag:'||l_wms_process_flag
6143 		     ,l_proc_name,1);
6144 	    END IF;
6145 
6146 	    IF (l_lpn_context IN (g_lpn_context_inv,g_lpn_context_wip)) THEN
6147 	       --Delete WDT
6148 	       BEGIN
6149 		  DELETE FROM wms_dispatched_tasks
6150 		    WHERE transaction_temp_id = l_tempid_tab(i);
6151 	       EXCEPTION
6152 		  WHEN OTHERS THEN
6153 		     IF (l_debug = 1) THEN
6154 			debug('Error deleting WDT. SQLCODE:'||SQLCODE||' SQLERRM:'||SQLERRM,l_proc_name,1);
6155 		     END IF;
6156 	       END;
6157 
6158 	       IF (l_debug = 1) THEN
6159 		  debug('# of WDT deleted:'||SQL%rowcount,l_proc_name,1);
6160 	       END IF;
6161 	    END IF;
6162 
6163 	    IF (l_op_plan_id IS NULL AND l_lpn_context = g_lpn_context_inv) THEN
6164 	       IF (l_debug = 1) THEN
6165 		  debug('DELETE MMTT/MTLT and close/reduce MOL for:'||l_tempid_tab(i),l_proc_name,1);
6166 	       END IF;
6167 
6168 	       BEGIN
6169 		 --update MOL
6170 		 UPDATE mtl_txn_request_lines mol
6171 		   SET quantity = quantity - l_mmtt_qty_mol_uom
6172 		   ,   quantity_detailed = quantity_detailed - l_mmtt_qty_mol_uom
6173 		   ,   line_status = Decode(quantity-Nvl(quantity_delivered,0)-l_mmtt_qty_mol_uom
6174 					    ,0
6175 					    ,inv_globals.g_to_status_closed
6176 					    ,line_status)
6177 		   WHERE  line_id = l_mol_id;
6178 	       EXCEPTION
6179 		  WHEN OTHERS THEN
6180 		     IF (l_debug = 1) THEN
6181 			debug('Error updating MOL. SQLCODE:'||SQLCODE||' SQLERRM:'||SQLERRM,l_proc_name,1);
6182 		     END IF;
6183 	       END;
6184 
6185 	       IF (l_debug = 1) THEN
6186 		  debug('# of MOL updated/closed:'||SQL%rowcount,l_proc_name,1);
6187 	       END IF;
6188 
6189 	       BEGIN
6190 		  --delete MTLT
6191 		  DELETE FROM mtl_transaction_lots_temp
6192 		    WHERE transaction_temp_id = l_tempid_tab(i);
6193 	       EXCEPTION
6194 		  WHEN OTHERS THEN
6195 		     IF (l_debug = 1) THEN
6196 			debug('Error deleting MTLT. SQLCODE:'||SQLCODE||' SQLERRM:'||SQLERRM,l_proc_name,1);
6197 		     END IF;
6198 	       END;
6199 
6200 	       IF (l_debug = 1) THEN
6201 		  debug('# of MTLT closed:'||SQL%rowcount,l_proc_name,1);
6202 	       END IF;
6203 
6204 	       BEGIN
6205 		  --delete MMTT
6206 		  DELETE FROM mtl_material_transactions_temp
6207 		    WHERE transaction_temp_id = l_tempid_tab(i);
6208 	       EXCEPTION
6209 		  WHEN OTHERS THEN
6210 		     IF (l_debug = 1) THEN
6211 			debug('Error deleting MMTT. SQLCODE:'||SQLCODE||' SQLERRM:'||SQLERRM,l_proc_name,1);
6212 		     END IF;
6213 	       END;
6214 
6215 	       IF (l_debug = 1) THEN
6216 		  debug('# of MMTT closed:'||SQL%rowcount,l_proc_name,1);
6217 	       END IF;
6218 	    END IF;--END IF l_op_plan_id IS NULL AND l_lpn_context = g_lpn_context_inv) THEN
6219 
6220 	    IF (l_wms_process_flag = 2) THEN
6221 	      BEGIN
6222 		 UPDATE mtl_txn_request_lines
6223 		   SET  wms_process_flag = 1
6224 		   WHERE  line_id = l_mol_id;
6225 	      EXCEPTION
6226 		 WHEN OTHERS THEN
6227 		    IF (l_debug = 1) THEN
6228 		       debug('No MOL updated. SQLERRM:'||Sqlerrm,l_proc_name,1);
6229 		    END IF;
6230 	      END;
6231 
6232 	      IF (l_debug = 1) THEN
6233 		 debug('# of MOL with process_flag = 2 updated:'||SQL%rowcount,l_proc_name,1);
6234 	      END IF;
6235 	    END IF;--END IF (l_wms_process_flag = 2) THEN
6236 	    --END BUG 5075410
6237          END LOOP;  -- All MMTTs loop
6238 
6239      ELSIF ( p_call_type = G_CT_NORMAL_LOAD OR
6240              p_call_type = G_CT_NORMAL_DROP OR
6241              p_call_type = G_CT_INSPECT_TM_FAILED OR
6242 	     p_call_type = G_CT_INSPECT_B4_TM) THEN
6243 	-- Normal Load/Drop or Inspect scenario hence call Cleanup_Operation_Instance
6244 
6245 	-- BUG 3288374
6246 	IF (p_call_type = g_ct_inspect_b4_tm) THEN
6247 	   ROLLBACK;
6248 	END IF;
6249 
6250 	-- All MMTTs loop
6251 	FOR i IN 1..l_tempid_tab.COUNT
6252        LOOP
6253 
6254 	  -- BUG 3288374
6255 	  -- If call type is G_CT_INSPECT_B4_TM, then we have to roll back
6256 	  -- AND then delete the dispatched task.  Otherwise, we can just
6257 	  -- call complete_operation_instance
6258 	  IF (p_call_type = g_ct_inspect_b4_tm) THEN
6259 	     IF (l_debug = 1) THEN
6260 		DEBUG('Calling DELETE_DISPATCHED_TASK ...',l_proc_name,9);
6261 		DEBUG('p_source_task_id    => ' || l_tempid_tab(i),l_proc_name,9);
6262 		DEBUG('p_wms_task_type =====> ' || WMS_GLOBALS.G_WMS_TASK_TYPE_PUTAWAY,l_proc_name,9);
6263 	     END IF;
6264 	     l_progress := '305';
6265 
6266 	     WMS_OP_RUNTIME_PVT_APIS.delete_dispatched_task
6267 	       ( p_source_task_id         => l_tempid_tab(i)
6268 		 ,p_wms_task_type         => WMS_GLOBALS.G_WMS_TASK_TYPE_INSPECT
6269 		 ,x_return_status         => x_return_status
6270 		 ,x_msg_count             => x_msg_count
6271 		 ,x_msg_data              => x_msg_data
6272 		 );
6273 	     IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
6274 		IF (l_debug = 1) THEN
6275 		   DEBUG('Error in Delete_dispatched_tasks ' ,l_proc_name,1);
6276 		END IF;
6277 		l_progress := '306';
6278 		x_return_status := fnd_api.g_ret_sts_error;
6279 	     END IF;
6280 
6281 	   ELSE --IF (p_call_type = g_ct_inspect_b4_tm)
6282 	     IF (l_debug = 1) THEN
6283 		DEBUG('Calling Cleanup_Operation_Instance with ...',l_proc_name,9);
6284 		DEBUG('p_source_task_id    => ' || l_tempid_tab(i),l_proc_name,9);
6285 		DEBUG('p_activity_type_id  => ' || G_OP_ACTIVITY_INBOUND,l_proc_name,9);
6286 	     END IF;
6287 	     l_progress := '310';
6288 
6289 	     wms_atf_runtime_pub_apis.Cleanup_Operation_Instance
6290 	       (  p_source_task_id    => l_tempid_tab(i)
6291 		  ,p_activity_type_id  => G_OP_ACTIVITY_INBOUND
6292 		  ,x_return_status     => x_return_status
6293 		  ,x_msg_data          => x_msg_data
6294 		  ,x_msg_count         => x_msg_count
6295 		  ,x_error_code        => l_error_code
6296 		  );
6297 	     l_progress := '320';
6298 
6299 	     IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
6300 		IF (l_debug = 1) THEN
6301 		   DEBUG(l_error_code || ' Error in Cleanup_Operation_Instance ' ,l_proc_name,1);
6302 		END IF;
6303 		l_progress := '330';
6304 		x_return_status  := fnd_api.g_ret_sts_error;
6305 		/* Don't raise error in case of failure, instead set the ret status as error
6306 		and proceed with the next record */
6307 		  --RAISE fnd_api.g_exc_error;
6308 		  END IF;
6309 	  END IF;--IF (p_call_type = g_ct_inspect_b4_tm) THEN
6310 
6311 	  --BUG 5075410
6312 	  BEGIN
6313 	     UPDATE   mtl_txn_request_lines
6314 	       SET    wms_process_flag = 1
6315 	       WHERE  line_id IN (SELECT move_order_line_id
6316 				  FROM mtl_material_transactions_temp
6317 				  WHERE transaction_temp_id = l_tempid_tab(i))
6318 	       AND    wms_process_flag = 2;
6319 	  EXCEPTION
6320 	     WHEN OTHERS THEN
6321 		IF (l_debug = 1) THEN
6322 		   debug('No MOL updated. SQLERRM:'||Sqlerrm,l_proc_name,1);
6323 		END IF;
6324 	  END;
6325 
6326 	  IF (l_debug = 1) THEN
6327 	     debug('# of MOL with process_flag = 2 updated:'||SQL%rowcount,l_proc_name,1);
6328 	  END IF;
6329 	  --END BUG 5075410
6330        END LOOP;  -- All MMTTs loop
6331 
6332      ELSE
6333        -- Invalid inputs passed hence error out
6334        l_progress := '340';
6335        RAISE fnd_api.g_exc_error;
6336      END IF;
6337 
6338 
6339      IF c_mol_mmtt_cursor%ISOPEN THEN
6340        l_progress := '970';
6341        CLOSE c_mol_mmtt_cursor;
6342      END IF;
6343 
6344      IF c_tmp_mmtt_cursor%ISOPEN THEN
6345        l_progress := '980';
6346        CLOSE c_tmp_mmtt_cursor;
6347      END IF;
6348 
6349      IF c_all_mmtt_cursor%isopen THEN
6350 	CLOSE c_all_mmtt_cursor;
6351      END IF;
6352 
6353      l_progress := '985';
6354 
6355 
6356      l_progress := '990';
6357 
6358      -- Also need to clean up the TM variables.  This is needed because
6359      -- in an online transactions, the variables will not be reset
6360      inv_rcv_common_apis.rcv_clear_global;
6361 
6362    EXCEPTION
6363      WHEN fnd_api.g_exc_error THEN
6364        x_return_status  := fnd_api.g_ret_sts_error;
6365        fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
6366        DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
6367        DEBUG(SQLERRM,l_proc_name,1);
6368 
6369        IF c_mol_mmtt_cursor%ISOPEN THEN
6370          CLOSE c_mol_mmtt_cursor;
6371        END IF;
6372 
6373        IF c_tmp_mmtt_cursor%ISOPEN THEN
6374          CLOSE c_tmp_mmtt_cursor;
6375        END IF;
6376 
6377        IF c_all_mmtt_cursor%isopen THEN
6378 	  CLOSE c_all_mmtt_cursor;
6379        END IF;
6380 
6381      WHEN fnd_api.g_exc_unexpected_error THEN
6382        x_return_status  := fnd_api.g_ret_sts_unexp_error;
6383        fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
6384 
6385        DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
6386        DEBUG(SQLERRM,l_proc_name,1);
6387 
6388        IF c_mol_mmtt_cursor%ISOPEN THEN
6389          CLOSE c_mol_mmtt_cursor;
6390        END IF;
6391 
6392        IF c_tmp_mmtt_cursor%ISOPEN THEN
6393          CLOSE c_tmp_mmtt_cursor;
6394        END IF;
6395 
6396        IF c_all_mmtt_cursor%isopen THEN
6397 	  CLOSE c_all_mmtt_cursor;
6398        END IF;
6399      WHEN OTHERS THEN
6400        x_return_status  := fnd_api.g_ret_sts_unexp_error;
6401        fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => x_msg_count, p_data => x_msg_data);
6402        DEBUG(' Exception occured at l_progress = ' || l_progress || ' during ' || to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
6403        DEBUG(SQLERRM,l_proc_name,1);
6404 
6405        IF c_mol_mmtt_cursor%ISOPEN THEN
6406          CLOSE c_mol_mmtt_cursor;
6407        END IF;
6408 
6409        IF c_tmp_mmtt_cursor%ISOPEN THEN
6410          CLOSE c_tmp_mmtt_cursor;
6411        END IF;
6412 
6413        IF c_all_mmtt_cursor%isopen THEN
6414 	  CLOSE c_all_mmtt_cursor;
6415        END IF;
6416    END Cleanup_ATF;
6417 
6418 
6419    /**
6420    *  Nested LPN changes, This procedure explodes the given LPN
6421    *  and for each LPN checks the validity of the given LPN and returns
6422    *  @param   p_org_id   Current Organization
6423    *  @param   p_lpn_id   From LPN ID
6424    *  @param   p_mode
6425                Purpose of the parameter - This method can be called
6426                from both UI as well as grouping logic. If this method
6427                is called from grouping logic, check only for the given LPN
6428                and not for the child LPNs.
6429                0 - Check for the given LPN iteself and not for the child LPNs
6430                1 - Check for the given LPN as well as it child LPNs (All
6431                    putaway other than MANUAL LOAD.
6432                2 - Check for the given LPN as well as it child LPNs (for
6433                    the MANUAL DROP case)
6434    *  @param   x_ret      returns the following values
6435    *           0 - Success
6436    *           1 - Entire LPN needs Inspection.
6437    *           2 - Entire LPN is Invalid
6438    *           3 - Entire LPN is Invalid and cannot be putawayed.
6439    *           4 - Some child LPNs are invalid and cannot be putawayed.
6440    *           5 - Some child LPNs need inspection.
6441    *           6 - There is no contents in LPN or an inner LPN inside the LPN
6442    *           7 - LPN invalid because you cannot load the LPN due to sub
6443    *               transfer restrictions specified in the material status form
6444    *  @param   x_loaded_stauts
6445    *           0 - not loaded
6446    *           1 - loaded
6447    *  @param   x_return_status
6448    *  @param   x_msg_count
6449    *  @param   x_context
6450    *  @param   p_user_id
6451    **/
6452 
6453    PROCEDURE Check_LPN_Validity_Wrapper(
6454      p_org_id         IN             NUMBER
6455    , p_lpn_id         IN             NUMBER
6456    , p_user_id        IN             NUMBER
6457    , p_mode           IN             NUMBER
6458    , x_ret            OUT NOCOPY     NUMBER
6459    , x_lpn_context    OUT NOCOPY     NUMBER
6460    , x_loaded_status  OUT NOCOPY     VARCHAR2
6461    , x_return_status  OUT NOCOPY     VARCHAR2
6462    , x_msg_count      OUT NOCOPY     NUMBER
6463    , x_msg_data       OUT NOCOPY     VARCHAR2
6464    ) IS
6465      l_progress         NUMBER;
6466      l_lpn_id           NUMBER;
6467      l_debug            NUMBER           := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
6468      l_content_lpn      VARCHAR2(1)      := 'N';
6469      l_x_ret            NUMBER           :=0;
6470 
6471      l_x_msg_count      NUMBER;
6472      l_x_return_status  VARCHAR2(2);
6473      l_x_msg_data       VARCHAR2(2000);
6474      l_x_context        NUMBER;
6475      l_is_nested_lpn    VARCHAR2(1)      := 'N';
6476      l_lpn_has_contents VARCHAR2(1)      := 'N';
6477      l_lpn_sub          VARCHAR2(10);
6478      l_lpn_loc          NUMBER;
6479      l_drop_active      VARCHAR(1)       :=  'N';
6480      l_userid           NUMBER;
6481      l_username         VARCHAR2(100);
6482 	 l_invalid_cnt      NUMBER; --Bug#6920388
6483      l_lpn_context      NUMBER ; --Bug#6920388
6484 
6485      -- Flags to identify the LPN validity
6486      l_entire_lpn_empty BOOLEAN          := TRUE;
6487 
6488      l_child_lpn_state   VARCHAR2(1)      := NULL;  -- 'V' Valid, 'I' Inspect 'U' INVALID
6489      l_parent_lpn_state  VARCHAR2(1)      := NULL;  -- 'V' Valid, 'I' Inspect 'U' INVALID.
6490 	 l_mmtt_qty          NUMBER           := 0;     -- Added for bug 14723445
6491 
6492      CURSOR lpn_cursor IS
6493        SELECT     lpn_id
6494              FROM wms_license_plate_numbers
6495             WHERE lpn_id <> p_lpn_id
6496        START WITH lpn_id = p_lpn_id
6497        CONNECT BY parent_lpn_id = PRIOR lpn_id;
6498    BEGIN
6499 
6500      -- Intialize out variables.
6501      l_progress := 10;
6502      l_content_lpn := 'N';
6503      x_return_status := fnd_api.g_ret_sts_success;
6504      x_loaded_status := 'N';
6505      x_ret := 0;
6506 
6507      IF (l_debug = 1) THEN
6508        debug('Patchset J code - Chcek_LPN_Validity_Wrapper');
6509        debug('p_org_id==> '|| p_org_id);
6510        debug('p_lpn_id==> '|| p_lpn_id);
6511      END IF;
6512 
6513      -- Get LPN Context
6514      SELECT lpn_context,subinventory_code,locator_id
6515      INTO x_lpn_context,l_lpn_sub,l_lpn_loc
6516      FROM   wms_license_plate_numbers
6517      WHERE  lpn_id = p_lpn_id;
6518 
6519      --BUG 3473899: p_mode = 2 => it is coming from MANUAL_LOAD
6520      --check the material status, and see if a sub transfer is allowed
6521      --in the source LPN sub/loc.  If it is not, that you cannot load the
6522      --LPN
6523      IF (p_mode = 2 AND x_lpn_context = 1) THEN
6524 	IF (inv_material_status_grp.is_status_applicable('TRUE',
6525                         NULL,
6526                         2,--subtransfer
6527                         NULL,
6528                         NULL,
6529                         p_org_id,
6530                         NULL,
6531                         l_lpn_sub,
6532                         l_lpn_loc,
6533                         NULL,
6534                         NULL,
6535                         'Z') <> 'Y' OR
6536 	    inv_material_status_grp.is_status_applicable('TRUE',
6537                         NULL,
6538 			2,--Sub Transfer
6539                         NULL,
6540                         NULL,
6541                         p_org_id,
6542                         NULL,
6543                         l_lpn_sub,
6544                         l_lpn_loc,
6545                         NULL,
6546                         NULL,
6547                         'L') <> 'Y') THEN
6548 	   x_ret := 7;
6549 	   RETURN ;
6550 	END IF;
6551      END IF;
6552 
6553      IF (x_lpn_context = 3) THEN
6554 	--BUG 3625990: For receiving, instead of checking if the LPN is
6555 	--locked or not, check if it has any WOOI that has type 'DROP'
6556 	--and status 'ACTIVE'.  This is because in WMSTKPTB.CHECK_LPN_VALIDITY,
6557 	--we no longer lock the LPN if it is a Receiving LPN
6558 	l_drop_active := 'N';
6559         BEGIN
6560 	   SELECT DISTINCT 'Y'
6561 	     ,    wooi.last_updated_by
6562 	     INTO l_drop_active
6563 	     ,    l_userid
6564 	     FROM  mtl_txn_request_lines mtrl,
6565 	     mtl_material_transactions_temp mmtt,
6566 	     wms_op_operation_instances wooi ,
6567 	     (SELECT wlpn.lpn_id                 /*5723418*/
6568 		      FROM   wms_license_plate_numbers wlpn
6569                       START WITH wlpn.lpn_id = p_lpn_id
6570 		      CONNECT BY PRIOR wlpn.lpn_id = wlpn.parent_lpn_id) wlpn
6571 	     WHERE mtrl.lpn_id = wlpn.lpn_id
6572 	     AND   mmtt.move_order_line_id = mtrl.line_id
6573 	     AND   wooi.source_task_id = mmtt.transaction_temp_id
6574 	     AND   wooi.operation_status = 2
6575 	     AND   wooi.operation_type_id = 2
6576 	     AND   wooi.last_updated_by <> p_user_id;
6577 	EXCEPTION
6578 	   WHEN no_data_found THEN
6579 	      IF (l_debug = 1) THEN
6580 		 debug('No Operation Plan with status Drop Active');
6581 	      END IF;
6582 	      l_drop_active := 'N';
6583 	   WHEN too_many_rows THEN
6584 	      IF (l_debug = 1) THEN
6585 		 debug('More than 1 Operation Plan with status Drop Active');
6586 	      END IF;
6587 	      l_drop_active := 'Y';
6588 	      l_username := NULL;
6589 	      l_userid := NULL;
6590 	END;
6591 
6592 	IF (l_debug = 1) THEN
6593 	   debug(' Drop Active by another user? '||l_drop_active);
6594 	END IF;
6595 
6596 	IF (l_drop_active = 'Y') THEN
6597 	   IF (l_userid IS NOT NULL) THEN
6598               BEGIN
6599 		 SELECT user_name
6600 		   INTO l_username
6601 		   FROM fnd_user
6602 		   WHERE user_id = l_userid;
6603 	      EXCEPTION
6604 		 WHEN OTHERS THEN
6605 		    l_username := NULL;
6606 	      END;
6607 
6608 	      IF (l_username IS NOT NULL) THEN
6609 		 fnd_message.set_name('WMS','WMS_PUTAWAY_LPN_LOCKED');
6610 		 fnd_message.set_token('USER',l_username);
6611 		 fnd_msg_pub.ADD;
6612 		 fnd_message.set_name('WMS','WMS_CONT_INVALID_LPN');
6613 		 fnd_msg_pub.ADD;
6614 	      END IF;
6615 
6616 	   END IF;
6617 	   x_ret := 3; --return error
6618 	   x_return_status := fnd_api.g_ret_sts_success;
6619 	   RETURN;
6620 	END IF;--END IF (l_drop_active = 'Y') THEN
6621      END IF;--END IF (x_lpn_context = 3) THEN
6622 
6623      -- Check if the given lpn has contents.
6624 
6625      BEGIN
6626        SELECT 'Y','Y'
6627          INTO l_content_lpn,l_lpn_has_contents
6628          FROM wms_lpn_contents
6629         WHERE parent_lpn_id = p_lpn_id
6630           AND ROWNUM<2;
6631      EXCEPTION
6632        WHEN no_data_found THEN
6633          l_content_lpn := 'N';
6634      END;
6635 
6636      IF l_content_lpn='Y' THEN
6637 	wms_task_dispatch_put_away.check_lpn_validity
6638 	  (p_org_id=> p_org_id
6639 	   , p_lpn_id=> p_lpn_id
6640 	   , x_ret=> l_x_ret
6641 	   , x_return_status=> l_x_return_status
6642 	   , x_msg_count=> l_x_msg_count
6643 	   , x_msg_data=> l_x_msg_data
6644 	   , x_context=> l_x_context
6645 	   , p_user_id=> p_user_id
6646 	   );
6647 
6648         -- Check if the lPN is alredy  loaded
6649         BEGIN
6650 	  SELECT 'Y'  INTO x_loaded_status FROM dual where exists
6651                       (SELECT 1 from
6652                        wms_dispatched_tasks W,
6653 		       mtl_material_transactions_temp t,
6654 		       mtl_txn_request_lines M
6655                        WHERE m.lpn_id = p_lpn_id
6656 		       AND   m.organization_id = p_org_id
6657 		       AND   m.line_status <> 5
6658 		       AND   m.quantity-Nvl(m.quantity_delivered,0)>0
6659 		       AND   t.move_order_line_id = m.line_id
6660 		       AND   t.organization_id = p_org_id
6661 		       AND   w.organization_id = p_org_id
6662                        AND   t.transaction_temp_id = w.transaction_temp_id
6663                        AND   w.status = 4);
6664        EXCEPTION
6665          WHEN NO_DATA_FOUND THEN
6666 	   x_loaded_status := 'N';
6667        END;
6668 
6669 	    -- Start of code fix for bug 14723445
6670       IF x_loaded_status = 'Y' THEN
6671        BEGIN
6672         FOR x IN (SELECT Sum(primary_quantity) wlc_qty,inventory_item_id,organization_id
6673                   FROM wms_lpn_contents
6674                   WHERE parent_lpn_id = p_lpn_id AND organization_id = p_org_id
6675                  GROUP BY inventory_item_id,organization_id) LOOP
6676           l_mmtt_qty := 0;
6677           SELECT Sum(mmtt.primary_quantity) INTO l_mmtt_qty
6678            FROM wms_dispatched_tasks W,
6679 		            mtl_material_transactions_temp mmtt,
6680 		            mtl_txn_request_lines M
6681                WHERE m.lpn_id = p_lpn_id
6682 		       AND   m.organization_id = x.organization_id
6683 		       AND   m.line_status <> 5
6684 		       AND   m.quantity-Nvl(m.quantity_delivered,0)>0
6685 		       AND   mmtt.move_order_line_id = m.line_id
6686 		       AND   mmtt.organization_id = x.organization_id
6687 		       AND   w.organization_id = x.organization_id
6688            AND   mmtt.transaction_temp_id = w.transaction_temp_id
6689            AND mmtt.inventory_item_id = x.inventory_item_id
6690            AND   w.status = 4;
6691 
6692           IF NVL(l_mmtt_qty,0) <> x.wlc_qty THEN
6693           x_loaded_status := 'N';
6694 		  EXIT;
6695           END IF;
6696         END LOOP;
6697        EXCEPTION
6698          WHEN NO_DATA_FOUND THEN
6699 	        x_loaded_status := 'N';
6700        END;
6701       END IF;
6702         -- End of code fix for bug 14723445
6703 
6704        --bug 5590439
6705        -- Bug 5768339 ( Regression of 5590439)
6706         /*Bug#6920388. Commented this block because we are checking for
6707         loaded LPN below
6708 		begin
6709          if (x_lpn_context =1 and x_loaded_status = 'N' ) then
6710          -- SELECT 'Y' INTO x_loaded_status FROM dual where exists
6711    	    SELECT 'Z' INTO x_loaded_status FROM dual where exists        -- End of changes for the bug 5768339.
6712             (SELECT 1 FROM mtl_material_transactions_temp mmtt, wms_dispatched_tasks wdt
6713             WHERE mmtt.transaction_temp_id = wdt.transaction_temp_id
6714             AND mmtt.organization_id = wdt.organization_id
6715             AND wdt.status = 4
6716             AND wdt.organization_id = p_org_id
6717             AND (
6718 	    ((mmtt.lpn_id = p_lpn_id) AND (mmtt.transfer_lpn_id IS NOT NULL))
6719 	                    OR
6720             ((mmtt.content_lpn_id = p_lpn_id) AND (mmtt.transfer_lpn_id IS NOT NULL))
6721             ));
6722          end if;
6723        Exception
6724 	 when no_data_found then
6725 	   x_loaded_status := 'N';
6726        end; */
6727        --bug 5590439 end
6728 
6729 	 l_entire_lpn_empty := FALSE;
6730      END IF;--IF l_content_lpn='Y' AND l_drop_active = 'N' THEN
6731 
6732      IF(l_debug =1) THEN
6733         debug('  Loaded status of the LPN is '|| x_loaded_status );
6734      END IF;
6735 
6736 
6737      -- If return value is error then parent lpn state 'U' - INVALID
6738      -- else if return  value = 0 then parent lpn state 'V' - VALID;
6739      -- else if return value = 1  then parent lpn state 'I' - INSPECT;
6740      -- else paretn lpn statue -  'U'  - INVALID.
6741      IF (l_x_return_status = fnd_api.g_ret_sts_error  OR  l_x_return_status = fnd_api.g_ret_sts_unexp_error ) THEN
6742        l_parent_lpn_state := 'U';   -- invalid
6743      ELSIF (l_x_ret =0) THEN
6744          l_parent_lpn_state := 'V'; -- valid.
6745      ELSIF (l_x_ret =1) THEN
6746          l_parent_lpn_state := 'I'; -- Inspect required
6747      ELSIF (l_x_ret =4) THEN
6748          x_ret := 5;
6749          RETURN;
6750      ELSE
6751          l_parent_lpn_state := 'U';  -- Invalid
6752      END IF; -- for return state.
6753 
6754      IF(l_debug= 1) THEN
6755         debug('l_x_return_status ' || l_x_return_status);
6756         debug('l_x_ret ' || l_x_ret);
6757      END IF;
6758 
6759 
6760      -- If mode value is '0' then this method is called from grouping logic,
6761      -- In this case check for the given LPN itself and return.
6762 
6763      IF p_mode = 0 THEN
6764          -- In case of failures l_x_ret might be '0' set it to proper value;
6765          IF (l_x_return_status = fnd_api.g_ret_sts_error  OR
6766              l_x_return_status = fnd_api.g_ret_sts_unexp_error ) THEN
6767              l_x_ret := 3;
6768          END IF;
6769          x_ret := l_x_ret;
6770          RETURN;
6771      END IF;
6772 
6773          --Bug#6920388.Added IF block below.
6774 	BEGIN
6775 	  SELECT COUNT(1) INTO l_invalid_cnt
6776 	  FROM Mtl_material_transactions_temp mmtt,
6777                Wms_License_Plate_Numbers wlpn
6778 	  WHERE mmtt.organization_id=p_org_id
6779 	  AND mmtt.wms_task_type <> WMS_GLOBALS.g_wms_task_type_putaway
6780           AND wlpn.outermost_lpn_id = p_lpn_id
6781           AND wlpn.organization_id  = p_org_id
6782 	  AND mmtt.allocated_lpn_id = wlpn.lpn_id
6783           AND NOT EXISTS ( SELECT 1 FROM Wms_dispatched_tasks wdt
6784                             WHERE wdt.transaction_temp_id = mmtt.transaction_temp_id
6785                             AND   wdt.status >= G_TASK_STATUS_LOADED ) ;
6786 
6787 	  IF ( l_invalid_cnt > 0 )   THEN
6788 	      x_ret := 3;
6789               fnd_message.set_name('INV', 'INV_LPN_ALLOCATED');
6790 	      fnd_msg_pub.add ;
6791               IF (l_debug = 1) THEN
6792                 debug('check_lpn_validity: Error!!!, LPN is Allocated' );
6793               END IF;
6794               x_return_status := fnd_api.g_ret_sts_unexp_error;
6795 	      return;
6796           END IF;
6797 
6798           SELECT COUNT(1) INTO l_invalid_cnt
6799 	   FROM wms_loaded_quantities_v wlqv
6800 	   WHERE wlqv.organization_id = p_org_id
6801 	   AND NVL(wlqv.content_lpn_id , wlqv.lpn_id) = p_lpn_id ;
6802 
6803 	  IF ( l_invalid_cnt > 0 ) THEN
6804 	      x_ret := 3;
6805               fnd_message.set_name('WMS', 'WMS_LPN_LOADED_ERROR');
6806 	      fnd_msg_pub.add ;
6807               IF (l_debug = 1) THEN
6808                 debug('check_lpn_validity: Error!!!, LPN has loaded tasks ' );
6809               END IF;
6810               x_return_status := fnd_api.g_ret_sts_unexp_error;
6811 	      return;
6812           END IF;
6813         END;
6814 
6815      -- Check the validity of child LPNs
6816      IF (l_debug = 1)  THEN
6817         debug('Given LPN is validity checked, Now check if the child LPNs are valid');
6818      END IF;
6819 
6820      OPEN lpn_cursor;
6821 
6822      LOOP
6823        FETCH lpn_cursor INTO l_lpn_id;
6824        EXIT WHEN lpn_cursor%NOTFOUND;
6825        l_content_lpn      := 'N';
6826 
6827        IF(l_debug = 1) THEN
6828          debug('Checking validity of Child Lpn ' || l_lpn_id);
6829        END IF;
6830 
6831        -- Check lpn has contents
6832        BEGIN
6833          SELECT 'Y','Y'
6834            INTO l_content_lpn,l_lpn_has_contents
6835            FROM wms_lpn_contents
6836           WHERE parent_lpn_id = l_lpn_id
6837             AND ROWNUM<2;
6838        EXCEPTION
6839          WHEN no_data_found THEN
6840            l_content_lpn := 'N';
6841        END;
6842 
6843        IF l_debug = 1 THEN
6844          IF (l_content_lpn = 'N') THEN debug('lpn ' || l_lpn_id || 'has no contents'); END IF;
6845          IF (l_content_lpn = 'Y') THEN debug('lpn ' || l_lpn_id || 'has contents'); END IF;
6846        END IF;
6847 
6848        IF l_content_lpn ='Y' THEN
6849            --BUG 5130266 - No need to check for drop active here, because
6850            --the CONNECT BY PRIOR query on the outermost LPN would have
6851            --checked
6852            wms_task_dispatch_put_away.check_lpn_validity
6853              (p_org_id=> p_org_id
6854             , p_lpn_id=> l_lpn_id
6855             , x_ret=> l_x_ret
6856             , x_return_status=> l_x_return_status
6857             , x_msg_count=> l_x_msg_count
6858             , x_msg_data=> l_x_msg_data
6859             , x_context=> l_x_context
6860             , p_user_id=> p_user_id
6861 	   );
6862 
6863            IF(l_debug= 1) THEN
6864              debug('After checking validity for lpn '|| l_lpn_id);
6865              debug('Return status ' || l_x_return_status);
6866              debug('return value for the chld LPN ' || l_x_ret);
6867            END IF;
6868 
6869            -- If return value is error then child lpn state 'U' - INVALID
6870            -- else if return  value = 0 then child lpn state 'V' - VALID;
6871            -- else if return value = 1  then child lpn state 'I' - INSPECT;
6872            -- else paretn lpn statue -  'U'  - INVALID.
6873            IF (l_x_return_status = fnd_api.g_ret_sts_error  OR  l_x_return_status = fnd_api.g_ret_sts_unexp_error ) THEN
6874              l_child_lpn_state := 'U';   -- invalid
6875            ELSIF (l_x_ret =0) THEN
6876                l_child_lpn_state := 'V';    -- valid.
6877            ELSIF (l_x_ret =1) THEN
6878                l_child_lpn_state := 'I';  -- Inspect required
6879            ELSE
6880                l_child_lpn_state := 'U';  -- Invalid
6881            END IF; -- for return state.
6882 
6883            -- logic used.
6884 
6885            /*-------------------------------------------------------------------
6886             * Given lpn          child LPN        result
6887             *-------------------------------------------------------------------
6888             *  INVALID           VALID            oh.. oh.. We have a problem
6889             *                                     Break the loop, return 4
6890             *  INSP REQ          VALID            Break the loop, return 4
6891             *  INSP Req          Invalid          Break the loop, return 4
6892             *  VALID             INVALID          Break the loop, return 4
6893             *  INSP Req          INSP Req         Continue loop. return 1 if
6894             *                                     entire lpn req inspection.
6895             *  INVALID           INVALID          No problem continue looping,
6896             *                                     Until you find valid lpn.
6897             *  VAlID             VALID            No Problem continue looping
6898                                                   Until you find Invalid lpn.
6899             ------------------------------------------------------------------*/
6900 
6901             -- Conclusion: From above table if Given LPN and child LPN states
6902             -- are same continue/otherwise error out.
6903 
6904 
6905            IF (l_parent_lpn_state <> l_child_lpn_state) THEN
6906               x_ret := 4; RETURN;
6907            END IF;
6908 
6909            /*--------------------------------------------------------------------
6910             * If one child LPN is loaded then the entire LPN is loaded.
6911             * To know whether a lpn is loaded or not, that lpn should have
6912             * some cotents inside it. An lpn with some contents here after
6913             * will be called as content lpn.
6914             *
6915             * To make sure loaded check will be called only for one contentlpn
6916             * and not every lpn, make use of the flag l_entire_lpn_empty.
6917             *
6918             * First time when the flow comes here l_entire_lpn_empty will be false,
6919             * and from second time it will be true because we are updating
6920             * l_entire_lpn_empty flag in the end. This way we can make sure
6921             * loaded check will be called only once.
6922             ------------------------------------------------------------------------*/
6923 
6924            IF l_entire_lpn_empty THEN
6925               BEGIN
6926                 SELECT 'Y'  INTO x_loaded_status FROM dual where exists
6927                            (select 1 from
6928                             WMS_DISPATCHED_TASKS W, MTL_MATERIAL_TRANSACTIONS_TEMP T
6929                             WHERE T.lpn_id = l_lpn_id
6930 			    AND t.organization_id = p_org_id
6931 			    AND w.organization_id = p_org_id
6932                             AND T.transaction_temp_id = W.transaction_temp_id
6933                             AND W.STATUS = 4
6934                             AND W.TASK_TYPE = WMS_GLOBALS.g_wms_task_type_putaway); --Bug#6920388.Added putaway check
6935               EXCEPTION
6936                 WHEN NO_DATA_FOUND THEN
6937                   x_loaded_status := 'N';
6938               END;
6939            END IF;
6940            l_entire_lpn_empty := FALSE;
6941        END IF;
6942      END LOOP;
6943 
6944      -- Compare with childs state only atleast one child is processed.
6945      -- If child lpn state is null, then no child is so far processed.
6946      IF (l_parent_lpn_state <> nvl(l_child_lpn_state,l_parent_lpn_state)) THEN
6947         x_ret := 4;
6948      END IF;
6949 
6950      IF(l_parent_lpn_state = nvl(l_child_lpn_state,l_parent_lpn_state)) THEN
6951         IF l_parent_lpn_state = 'I' THEN
6952            x_ret:=1;
6953         ELSIF (l_parent_lpn_state = 'U') THEN
6954           x_ret := 3;
6955         ELSIF (l_parent_lpn_state = 'V') THEN
6956           x_ret :=0;
6957         END IF;
6958      END IF;
6959 
6960      IF (l_lpn_has_contents = 'N') THEN
6961         x_ret := 6;
6962      END IF;
6963 
6964      IF lpn_cursor%isopen THEN
6965        CLOSE lpn_cursor;
6966      END IF;
6967 
6968      IF ( x_ret in (0,1) and x_loaded_status = 'N' ) THEN
6969 	    Detach_Loaded_Lpns(p_lpn_id,p_org_id); --6920388
6970 	    SELECT lpn_context INTO l_lpn_context
6971 	    FROM Wms_license_plate_numbers wlpn
6972 	    WHERE wlpn.lpn_id=p_lpn_id
6973 	     AND wlpn.organization_id=p_org_id;
6974 	    IF l_lpn_context = WMS_CONTAINER_PUB.LPN_CONTEXT_PREGENERATED THEN
6975 	       debug('There is no LPN content available for this transaction.');
6976 	      fnd_message.set_name('WMS', 'WMS_LPN_UNAVAIL');
6977 	      fnd_msg_pub.ADD;
6978 	      x_return_status := fnd_api.g_ret_sts_unexp_error;
6979 	      x_ret := 3;
6980 	      RETURN;
6981 	    END IF;
6982      END IF;
6983      RETURN;
6984 
6985 
6986    EXCEPTION
6987      WHEN OTHERS THEN
6988        x_return_status := fnd_api.g_ret_sts_unexp_error;
6989        x_ret           := 3;
6990        debug(SQLCODE, 1);
6991 
6992        IF lpn_cursor%isopen THEN
6993          CLOSE lpn_cursor;
6994        END IF;
6995 
6996        IF SQLCODE IS NOT NULL THEN
6997          l_progress := 100;
6998          inv_mobile_helper_functions.sql_error('validate_nested_lpns', l_progress, SQLCODE);
6999        END IF;
7000    END check_lpn_validity_wrapper;
7001 
7002 
7003 
7004    /**
7005    *  Nested LPN changes, This procedure Loads the given FromLPN into ToLPN
7006    *  and calls suggestions_pub
7007    *  @param   p_org_id       Current Organization
7008    *  @param   p_sub_code     If tosub is receving then call transfer transaction
7009                               If to sub is inventory sub then call Pack Unpack API.
7010    *  @param   p_from_lpn_id  From LPN id
7011    *  @param   p_to_lpn_id    Transfer LPN id
7012    *  @param   p_mode
7013                1 - Load entire LPN
7014                If the parent_lpn for the given from lpn is not null then unpack the lpn from its parent
7015                If the to_lpn is not null then pack the lpn into tolpn.
7016                2 - Transfer All contents
7017                3 - Transfer parial quantity.
7018 
7019    *  @param   x_ret      returns the following values
7020    *           0 - Success
7021    *           1 - failure.
7022    *  @param   x_return_status
7023    *  @param   x_msg_count
7024    *  @param   x_context
7025    *  @param   p_user_id
7026    **/
7027 
7028    PROCEDURE suggestions_pub_wrapper(
7029      p_lpn_id               IN             NUMBER
7030    , p_org_id               IN             NUMBER
7031    , p_user_id              IN             NUMBER
7032    , p_eqp_ins              IN             VARCHAR2
7033    , p_status               IN             NUMBER := 3
7034    , p_check_for_crossdock  IN             VARCHAR2 := 'Y'
7035    , x_number_of_rows       OUT NOCOPY     NUMBER
7036    , x_return_status        OUT NOCOPY     VARCHAR2
7037    , x_msg_count            OUT NOCOPY     NUMBER
7038    , x_msg_data             OUT NOCOPY     VARCHAR2
7039    , x_crossdock            OUT NOCOPY     VARCHAR2
7040    ) IS
7041 
7042      l_lpn_id             NUMBER;
7043      l_number_of_rows     NUMBER := 0;
7044      l_x_crossdock        VARCHAR2(2) := 'Y';
7045      l_progress           NUMBER := 10;
7046      l_is_content_lpn     VARCHAR2(2) := 'N';
7047      l_debug           NUMBER         := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
7048      l_error_messages     VARCHAR2(1200);
7049      l_msg_count           NUMBER;
7050      l_msg_data           VARCHAR2(1200);
7051 	 l_wlc_without_mmtt             NUMBER := 0; --BUG12681832
7052      CURSOR lpn_cursor IS
7053            SELECT lpn_id
7054              FROM wms_license_plate_numbers
7055        START WITH lpn_id = p_lpn_id
7056        CONNECT BY parent_lpn_id = PRIOR lpn_id;
7057    BEGIN
7058 
7059      -- Intializat out variables.
7060      x_return_status := fnd_api.g_ret_sts_success;
7061 
7062      -- Explode the LPN and for each child LPN call suggestions_pub_wrapper.
7063      IF (l_debug = 1) THEN
7064        debug('Patchset J code');
7065        debug('  p_org_id==> '|| p_org_id);
7066        debug('  p_lpn_id==> '|| p_lpn_id);
7067        debug('  p_user_id==> '|| p_user_id);
7068      END IF;
7069 
7070      -- Initialize out variables
7071      x_return_status := fnd_api.g_ret_sts_success;
7072      x_crossdock:= 'N';
7073      x_number_of_rows := 0;
7074 
7075 	 SAVEPOINT before_suggestions_pub_call;
7076      OPEN lpn_cursor;
7077      LOOP
7078        FETCH lpn_cursor INTO l_lpn_id;
7079        EXIT WHEN lpn_cursor%notfound;
7080          -- Check if the given LPN has contents.
7081          BEGIN
7082              SELECT 'Y' INTO l_is_content_lpn
7083              FROM   wms_lpn_contents
7084              WHERE  parent_lpn_id = l_lpn_id
7085              AND    ROWNUM<2;
7086          EXCEPTION
7087            WHEN no_data_found  THEN l_is_content_lpn := 'N';
7088          END;
7089 
7090          -- Call suggestions pub for each of the LPN.
7091          IF (l_is_content_lpn = 'Y') THEN
7092 
7093            wms_Task_Dispatch_put_away.suggestions_pub(
7094               p_lpn_id               => l_lpn_id
7095              ,p_org_id               => p_org_id
7096              ,p_user_id              => p_user_id
7097              ,p_eqp_ins              => p_eqp_ins
7098              ,x_number_of_rows       => l_number_of_rows
7099              ,x_return_status        => x_return_status
7100              ,x_msg_count            => x_msg_count
7101              ,x_msg_data             => x_msg_data
7102              ,x_crossdock            => l_x_crossdock
7103              ,p_status               => p_status
7104              ,p_check_for_crossdock  => p_check_for_crossdock
7105              ,p_commit               => 'N');
7106 
7107 
7108          IF (l_debug=1) THEN
7109            debug('ret status after suggestion pug for :  ' ||l_lpn_id||' is '|| x_return_status);
7110          END IF;
7111 
7112 
7113          IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
7114 --	    IF x_msg_count = 0 THEN
7115 --	       FND_MESSAGE.SET_NAME('WMS','WMS_LOG_EXCEPTION_FAIL');
7116 --	       FND_MSG_PUB.ADD;
7117 --	    END IF;
7118 	    RAISE FND_API.g_exc_unexpected_error;
7119 
7120           ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
7121 --	    IF x_msg_count = 0 THEN
7122 --	    FND_MESSAGE.SET_NAME('WMS','WMS_LOG_EXCEPTION_FAIL');
7123 --	    FND_MSG_PUB.ADD;
7124 --	    END IF;
7125              RAISE FND_API.G_EXC_ERROR;
7126          END IF;
7127 
7128          IF l_x_crossdock = 'Y' THEN
7129             x_crossdock := 'Y';
7130          END IF;
7131          x_number_of_rows := l_number_of_rows+x_number_of_rows;
7132        END IF;
7133      END LOOP;
7134 
7135      IF l_debug = 1 THEN
7136         debug('End of loop: Final ret status: ' || x_return_status || ' no of rows: ' || x_number_of_rows);
7137      END IF;
7138 
7139 	--BUG12681832 Begin
7140 	IF (NOT WMS_RE_Custom_PUB.PutawayPartialDetailedLPN(p_lpn_id)) THEN
7141 		BEGIN
7142 		select count(1) INTO l_wlc_without_mmtt from
7143 			(SELECT lpn_id FROM  wms_license_plate_numbers
7144 					START  WITH lpn_id = p_lpn_id
7145 					CONNECT BY PRIOR lpn_id = parent_lpn_id ) wlpn ,
7146 			wms_lpn_contents wlc
7147 			where
7148 			wlc.parent_lpn_id = wlpn.lpn_id
7149 				and not exists (select 1 from mtl_material_transactions_temp mmtt
7150 								where mmtt.organization_id = wlc.organization_id
7151 								AND mmtt.inventory_item_id = wlc.inventory_item_id
7152 								AND (mmtt.lpn_id = wlc.parent_lpn_id or mmtt.content_lpn_id = wlc.parent_lpn_id));
7153 
7154 	    EXCEPTION
7155           WHEN NO_DATA_FOUND THEN
7156             l_wlc_without_mmtt := 0;
7157         END;
7158 
7159 		IF l_wlc_without_mmtt > 0 THEN
7160 			IF (l_debug = 1) THEN
7161 			debug('suggestions_pub: Before suggestions_csr ');
7162 			debug('suggestions_pub: There is a WLC for lpn_id  ' || p_lpn_id || ' that does not have an MMTT so preventing the putaway out');
7163 			END IF;
7164 			--fnd_msg_pub.initialize();
7165 			fnd_message.set_name('WMS', 'WMS_INVLID_LPN_MOVE');
7166 			fnd_msg_pub.ADD;
7167 			ROLLBACK TO before_suggestions_pub_call;
7168 			RAISE fnd_api.g_exc_error;
7169 		END IF;
7170 	END IF;
7171 	--BUG12681832 End
7172 
7173 
7174      IF x_number_of_rows = 0 THEN
7175 	fnd_msg_pub.count_and_get(p_count => l_msg_count, p_data =>
7176 				  l_msg_data);
7177 	IF l_debug = 1 THEN
7178 	   debug('l_msg_count: ' || l_msg_count);
7179 	   debug('l_msg_data: ' || l_msg_data);
7180 	END IF;
7181 
7182 	IF (l_msg_count IS NULL OR l_msg_count = 0) THEN
7183 	   -- use default message if there are no message on stack
7184 	   -- first reset message stack
7185      fnd_msg_pub.initialize();
7186 	   FND_MESSAGE.SET_NAME('WMS','WMS_ALLOCATE_FAIL');
7187 	   FND_MSG_PUB.ADD;
7188   ELSIF l_msg_count > 1 THEN
7189  	  FOR i IN 2 .. l_msg_count LOOP
7190  	    fnd_msg_pub.delete_msg(i);
7191  	  END LOOP;
7192 	END IF;
7193 	RAISE fnd_api.g_exc_error;
7194      END IF;
7195 
7196    EXCEPTION
7197      WHEN OTHERS THEN
7198        x_return_status := fnd_api.g_ret_sts_unexp_error;
7199        debug(SQLCODE, 1);
7200 
7201        IF lpn_cursor%isopen THEN
7202          CLOSE lpn_cursor;
7203        END IF;
7204 
7205        -- Get rid of the folloiwng, as it appends a junk message to the
7206        -- error message stack
7207        --       IF SQLCODE IS NOT NULL AND l_no_lines <> 1 THEN
7208        --         l_progress := 100;
7209        --	 inv_mobile_helper_functions.sql_error('suggestions_pub_wrapper', l_progress, SQLCODE);
7210        --       END IF;
7211    END suggestions_pub_wrapper;
7212 
7213    /**
7214    *  Nested LPN changes, This procedure packs the given from lpn into tolpn
7215    *  and calls suggestions_pub
7216    *  @param   p_org_id       Current Organization
7217    *  @param   p_sub_code     If tosub is receving then call transfer transaction
7218                               If to sub is inventory sub then call Pack Unpack API.
7219    *  @param   p_from_lpn_id  From LPN id
7220    *  @param   p_to_lpn_id    Transfer LPN id
7221    *  @param   p_mode
7222                1 - Pack from_lpn into to_lpn
7223                2 - unpack to_lpn from from_lpn
7224                for unpcaking lpn
7225                fromlpnid -> parentlpnid
7226                tolpnid   -> child lPNID
7227    *  @param   x_ret      returns the following values
7228    *           0 - Success
7229    *           1 - failure.
7230    **/
7231    PROCEDURE PackUnpack_lpn(
7232      p_from_lpn_id    IN             NUMBER
7233    , p_to_lpn_id      IN             NUMBER
7234    , p_org_id         IN             NUMBER
7235    , p_sub_code       IN             VARCHAR2
7236    , p_loc_id         IN             NUMBER
7237    , p_user_id        IN             NUMBER
7238    , p_project_id     IN             NUMBER  DEFAULT NULL
7239    , p_task_id        IN             NUMBER  DEFAULT NULL
7240    , p_trx_header_id  IN             NUMBER
7241    , p_mode           IN             NUMBER
7242    , p_batch_id       IN             NUMBER DEFAULT NULL
7243    , p_batch_seq      IN             NUMBER DEFAULT NULL
7244    , x_return_status  OUT NOCOPY     VARCHAR2
7245    , x_msg_count      OUT NOCOPY     NUMBER
7246    , x_msg_data       OUT NOCOPY     VARCHAR2
7247    ) IS
7248      l_progress    NUMBER;
7249      l_txn_temp_id NUMBER;
7250      l_debug       NUMBER         := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
7251      l_ret         NUMBER;
7252 
7253  BEGIN
7254    x_return_status := fnd_api.g_ret_sts_success;
7255    l_progress := 10;
7256 
7257    IF(l_debug =1) THEN
7258      debug('In Patchset J code: ','PackUnpackLpn');
7259    END IF;
7260 
7261 
7262    -- Pack from lpn into to lpn
7263    IF p_mode = 1 THEN
7264        l_ret := inv_trx_util_pub.insert_line_trx(
7265           p_trx_hdr_id           => p_trx_header_id
7266         , p_item_id              => '0'
7267         , p_org_id               => p_org_id
7268         , p_trx_action_id        => inv_globals.g_action_containerpack
7269         , p_subinv_code          => p_sub_code
7270         , p_locator_id           => p_loc_id
7271         , p_trx_type_id          => inv_globals.g_type_container_pack
7272         , p_trx_src_type_id      => inv_globals.g_sourcetype_inventory
7273         , p_trx_qty              => 1
7274         , p_pri_qty              => 1
7275         , p_uom                  => 'Ea'
7276         , p_user_id              => p_user_id
7277         , p_from_lpn_id          => NULL
7278         , p_cnt_lpn_id           => p_from_lpn_id
7279         , p_xfr_lpn_id           => p_to_lpn_id
7280         , x_trx_tmp_id           => l_txn_temp_id
7281         , x_proc_msg             => x_msg_data
7282         , p_project_id           => p_project_id
7283         , p_task_id              => p_task_id);
7284    END IF;
7285 
7286 
7287    -- UnPack to lpn from from lpn
7288    IF p_mode = 2 THEN
7289        l_ret := inv_trx_util_pub.insert_line_trx(
7290           p_trx_hdr_id           => p_trx_header_id
7291         , p_item_id              => '-1'
7292         , p_org_id               => p_org_id
7293         , p_trx_action_id        => inv_globals.g_action_containerunpack
7294         , p_subinv_code          => p_sub_code
7295         , p_locator_id           => p_loc_id
7296         , p_trx_type_id          => inv_globals.g_type_container_unpack
7297         , p_trx_src_type_id      => inv_globals.g_sourcetype_inventory
7298         , p_trx_qty              => 1
7299         , p_pri_qty              => 1
7300         , p_uom                  => 'Ea'
7301         , p_user_id              => p_user_id
7302         , p_from_lpn_id          => p_from_lpn_id
7303         , p_cnt_lpn_id           => p_to_lpn_id
7304         , p_xfr_lpn_id           => NULL
7305         , x_trx_tmp_id           => l_txn_temp_id
7306         , x_proc_msg             => x_msg_data
7307         , p_project_id           => p_project_id
7308         , p_task_id              => p_task_id);
7309    END IF;
7310 
7311    IF (l_txn_temp_id <=0 ) OR (l_ret<>0) THEN
7312      x_return_status := fnd_api.g_ret_sts_unexp_error;
7313    END IF;
7314 
7315    -- Update batch id and seq id
7316    IF p_batch_id IS NOT NULL AND p_batch_seq IS NOT NULL THEN
7317      UPDATE  mtl_material_transactions_temp
7318        SET   transaction_header_id = p_trx_header_id
7319            , transaction_batch_id  = p_batch_id
7320            , transaction_batch_seq = p_batch_seq
7321      WHERE   transaction_temp_id   = l_txn_temp_id;
7322    END IF; -- Update batch id
7323 
7324    IF l_debug = 1  THEN
7325      debug('return status' || l_ret);
7326      debug('New transaction created with transaction temp id ' || l_txn_temp_id,'packunpack_lpn');
7327    END IF;
7328 
7329  EXCEPTION
7330     WHEN OTHERS THEN
7331       x_return_status := fnd_api.g_ret_sts_unexp_error;
7332       debug(SQLCODE, 1);
7333 
7334       IF SQLCODE IS NOT NULL THEN
7335         l_progress := 100;
7336         inv_mobile_helper_functions.sql_error('Pack Unpack Lpn', l_progress, SQLCODE);
7337       END IF;
7338  END;
7339 
7340 
7341  PROCEDURE load_lpn(
7342      p_org_id         IN             NUMBER
7343    , p_sub_code       IN             VARCHAR2
7344    , p_loc_id         IN             NUMBER
7345    , p_from_lpn_id    IN             NUMBER
7346    , p_to_lpn_id      IN             NUMBER
7347    , p_mode           IN             NUMBER
7348    , p_user_id        IN             NUMBER
7349    , p_eqp_ins        IN             VARCHAR2
7350    , p_project_id     IN             NUMBER DEFAULT NULL
7351    , p_task_id        IN             NUMBER DEFAULT NULL
7352    , p_check_for_crossdock IN        VARCHAR2
7353    , x_return_status  OUT NOCOPY     VARCHAR2
7354    , x_msg_count      OUT NOCOPY     NUMBER
7355    , x_msg_data       OUT NOCOPY     VARCHAR2
7356    , x_crossdock      OUT NOCOPY     VARCHAR2
7357   ) IS
7358      l_progress          NUMBER;
7359      l_lpn_context       NUMBER;
7360      l_parent_lpn_id     NUMBER;
7361      l_trx_header_id     NUMBER;
7362      l_debug             NUMBER   := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
7363      l_x_num_of_rows     NUMBER   := 0;
7364      l_x_return_status   VARCHAR2(2);
7365      l_x_msg_count       NUMBER;
7366      l_x_msg_data        VARCHAR2(2000);
7367      l_ret               NUMBER;
7368      l_is_tm_call_needed BOOLEAN := FALSE;
7369      l_is_rcv_tm_call_needed BOOLEAN := FALSE;
7370      l_old_txn_mode_code VARCHAR2(10);
7371      l_group_id          NUMBER;
7372      l_to_sub            VARCHAR2(10);
7373      l_to_loc            NUMBER;
7374      l_to_context        NUMBER;
7375      l_sub_loc_changed   VARCHAR2(1);
7376      l_txn_temp_id NUMBER;
7377      TYPE number_tb_tp IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
7378      l_mmtt_ids          number_tb_tp;
7379      l_emp_id NUMBER;
7380      l_task_execute_rec wms_dispatched_tasks%ROWTYPE;
7381      l_error_code        NUMBER;
7382 
7383      l_consolidation_method_id NUMBER;
7384      l_drop_lpn_option NUMBER;
7385 
7386      l_msg_count                 NUMBER;
7387      l_msg_data                  VARCHAR2(10000);
7388      l_discrepancy       NUMBER;
7389  BEGIN
7390      l_progress := 10;
7391      x_return_status := fnd_api.g_ret_sts_success;
7392 
7393      IF (l_debug = 1) THEN
7394        debug('Patchset J code - Load LPN');
7395        debug('  p_org_id=======> '|| p_org_id);
7396        debug('  p_from_lpn_id==> '|| p_from_lpn_id);
7397        debug('  p_to_lpn_id====> '|| p_to_lpn_id);
7398        debug('  p_user_id======> '|| p_user_id);
7399        debug('  p_sub_code=====> '|| p_sub_code);
7400        debug('  p_loc_id=======> '|| p_loc_id);
7401      END IF;
7402 
7403      SELECT parent_lpn_id,lpn_context
7404        INTO l_parent_lpn_id,l_lpn_context
7405        FROM wms_license_plate_numbers
7406        WHERE lpn_id = p_from_lpn_id;
7407 
7408      IF (p_to_lpn_id IS NOT NULL AND p_to_lpn_id <> 0) THEN
7409   l_progress := 20;
7410   BEGIN
7411      SELECT lpn_context,subinventory_code,locator_id
7412        INTO l_to_context,l_to_sub,l_to_loc
7413        FROM wms_license_plate_numbers
7414        WHERE lpn_id = p_to_lpn_id;
7415   EXCEPTION
7416      WHEN OTHERS THEN
7417         l_to_context := -1;
7418         l_to_sub := NULL;
7419         l_to_loc := NULL;
7420   END;
7421   l_progress := 30;
7422      END IF;
7423 
7424      -- Set variable if sub/loc of toLPN is different from that of from LPN
7425      IF (Nvl(p_to_lpn_id,0) <> 0
7426    AND l_to_context IN (1,2,3)  -- No need to call TM if DFBU LPN
7427    AND (Nvl(l_to_sub,'@@@') <> Nvl(p_sub_code,'@@@')
7428         OR Nvl(l_to_loc,-1) <> Nvl(p_loc_id,-1))) THEN
7429   l_sub_loc_changed := 'Y';
7430       ELSE
7431   l_sub_loc_changed := 'N';
7432      END IF;
7433 
7434      IF (l_debug = 1) THEN
7435   debug('Sub/loc changed: ' || l_sub_loc_changed,9);
7436   debug('l_to_sub => ' || l_to_sub,9);
7437   debug('l_to_loc => ' || l_to_loc,9);
7438      END IF;
7439 
7440      -- Get Transaction header id.
7441 
7442      SELECT mtl_material_transactions_s.NEXTVAL
7443        INTO l_trx_header_id
7444        FROM dual;
7445 
7446 
7447      -- Pack Entire lpn witn LPN context Resides in inventory
7448      IF l_lpn_context = 1 AND p_mode = 1 THEN
7449 
7450        IF l_parent_lpn_id IS NOT NULL THEN
7451 
7452          IF(l_debug =1) THEN
7453            debug('Inventory LPN, Parent lpn is not null, Unpack '||p_from_lpn_id|| ' from ' ||l_parent_lpn_id);
7454          END IF;
7455 
7456          PackUnpack_LPN(
7457                  p_from_lpn_id   => l_parent_lpn_id
7458                , p_to_lpn_id     => p_from_lpn_id
7459                , p_org_id        => p_org_id
7460                , p_sub_code      => p_sub_code
7461                , p_loc_id        => p_loc_id
7462                , p_user_id       => p_user_id
7463                , p_project_id    => p_project_id
7464                , p_task_id       => p_task_id
7465                , p_trx_header_id => l_trx_header_id
7466          , p_mode          => 2
7467          , p_batch_id      => l_trx_header_id
7468          , p_batch_seq     => 1
7469                , x_return_status => l_x_return_status
7470                , x_msg_count     => l_x_msg_count
7471                , x_msg_data      => l_x_msg_data);
7472 
7473          l_is_tm_call_needed := TRUE;
7474        END IF;
7475 
7476        IF  (p_to_lpn_id IS NOT NULL) AND (p_to_lpn_id <> p_from_lpn_id) AND (p_to_lpn_id <> 0) THEN
7477 
7478          IF (l_debug = 1) THEN
7479            debug('Inventory LPN, To lpn is not null, Pack  '||p_from_lpn_id|| ' Into  ' ||p_to_lpn_id);
7480          END IF;
7481 
7482    IF (l_sub_loc_changed = 'Y') THEN
7483       l_ret := inv_trx_util_pub.insert_line_trx
7484         (
7485          p_trx_hdr_id           => l_trx_header_id
7486          , p_item_id              => '0'
7487          , p_org_id               => p_org_id
7488          , p_trx_action_id        => 2
7489          , p_subinv_code          => p_sub_code
7490          , p_tosubinv_code        => l_to_sub
7491          , p_locator_id           => p_loc_id
7492          , p_tolocator_id         => l_to_loc
7493          , p_trx_type_id          => 2
7494          , p_trx_src_type_id      => 13
7495          , p_trx_qty              => 1
7496          , p_pri_qty              => 1
7497          , p_uom                  => 'Ea'
7498          , p_user_id              => p_user_id
7499          , p_from_lpn_id          => NULL
7500          , p_cnt_lpn_id           => p_from_lpn_id
7501          , p_xfr_lpn_id           => NULL
7502          , x_trx_tmp_id           => l_txn_temp_id
7503          , x_proc_msg             => x_msg_data
7504          , p_project_id           => p_project_id
7505          , p_task_id              => p_task_id);
7506       IF (l_txn_temp_id <= 0 ) OR (l_ret<>0) THEN
7507          IF (l_debug = 1) THEN
7508       debug('Error inserting MMTT ID: ' ||
7509       l_txn_temp_id,9);
7510          END IF;
7511          RAISE fnd_api.g_exc_error;
7512       END IF;
7513 
7514       BEGIN
7515          UPDATE  mtl_material_transactions_temp
7516      SET   transaction_header_id = l_trx_header_id
7517      , transaction_batch_id  = l_trx_header_id
7518      , transaction_batch_seq = 2
7519      WHERE   transaction_temp_id   = l_txn_temp_id;
7520       EXCEPTION
7521          WHEN OTHERS THEN
7522       IF (l_debug = 1) THEN
7523          debug('Error updating MMTT with ID: ' ||
7524          l_txn_temp_id,9);
7525       END IF;
7526       RAISE fnd_api.g_exc_error;
7527       END;
7528 
7529       packunpack_lpn
7530         (
7531          p_from_lpn_id   => p_from_lpn_id
7532          , p_to_lpn_id     => p_to_lpn_id
7533          , p_org_id        => p_org_id
7534          , p_sub_code      => l_to_sub
7535          , p_loc_id        => l_to_loc
7536          , p_user_id       => p_user_id
7537          , p_project_id    => p_project_id
7538          , p_task_id       => p_task_id
7539          , p_trx_header_id => l_trx_header_id
7540          , p_mode          => 1 -- Pack
7541          , p_batch_id      => l_trx_header_id
7542          , p_batch_seq     => 3
7543          , x_return_status => l_x_return_status
7544          , x_msg_count     => l_x_msg_count
7545          , x_msg_data      => l_x_msg_data);
7546       ELSE -- If no sub/loc changed
7547       packunpack_lpn
7548         (
7549          p_from_lpn_id   => p_from_lpn_id
7550          , p_to_lpn_id     => p_to_lpn_id
7551          , p_org_id        => p_org_id
7552          , p_sub_code      => p_sub_code
7553          , p_loc_id        => p_loc_id
7554          , p_user_id       => p_user_id
7555          , p_project_id    => p_project_id
7556          , p_task_id       => p_task_id
7557          , p_trx_header_id => l_trx_header_id
7558          , p_mode          => 1 -- Pack
7559          , p_batch_id      => l_trx_header_id
7560          , p_batch_seq     => 2
7561          , x_return_status => l_x_return_status
7562          , x_msg_count     => l_x_msg_count
7563          , x_msg_data      => l_x_msg_data);
7564    END IF;
7565          l_is_tm_call_needed := TRUE;
7566        END IF;
7567 
7568      END IF; ---IF l_lpn_context = 1 AND p_mode = 1 THEN
7569 
7570      IF(l_debug = 1) THEN
7571         debug('After calling pack unpack ');
7572      END IF;
7573 
7574      -- Call pup if 1) it is WIP LPN, or if 2) it is RCV LPN and
7575      -- i) it is not doing any nesting
7576      -- ii) or if it is, the sub and loc is the same as the from LPN
7577      IF p_mode = 1
7578         AND ((l_lpn_context = 3 AND l_sub_loc_changed = 'N')
7579        OR l_lpn_context = 2) THEN
7580 
7581        -- Unpack the given LPN from its parent lpn
7582        IF l_parent_lpn_id IS NOT NULL THEN
7583           IF(l_debug = 1) THEN
7584              debug('Receiving LPN, Parent lpn is not null, Unpack '||p_from_lpn_id||' from ' ||l_parent_lpn_id);
7585           END IF;
7586           wms_container_pvt.packunpack_container
7587             (p_api_version       =>   1.0
7588             ,p_content_lpn_id   =>   p_from_lpn_id
7589             ,p_lpn_id           =>   l_parent_lpn_id
7590             ,p_operation        =>   2 -- Unpack
7591             ,p_organization_id  =>   p_org_id
7592             ,p_commit           =>   fnd_api.g_false
7593             ,x_return_status    =>   x_return_status
7594             ,x_msg_count        =>   x_msg_count
7595             ,x_msg_data         =>   x_msg_data);
7596 
7597     IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
7598        IF (l_debug = 1) THEN
7599     DEBUG('Error unpacking content_lpn = ' ||
7600           p_from_lpn_id || ' into lpn = ' ||
7601           l_parent_lpn_id,'load_lpn',9);
7602     debug('Error message:'||x_msg_data,9);
7603        END IF;
7604        RAISE fnd_api.g_exc_error;
7605     END IF;
7606        END IF; --IF l_parent_lpn_id IS NOT NULL THEN
7607 
7608        -- Pack the given LPN into ToLPN
7609        IF p_to_lpn_id IS NOT NULL AND p_to_lpn_id <> p_from_lpn_id AND (p_to_lpn_id <> 0) THEN
7610          IF(l_debug = 1) THEN
7611             debug('Receiving LPN, To lpn is not null, Pack  '||p_from_lpn_id|| ' Into  ' ||p_to_lpn_id);
7612          END IF;
7613 
7614    -- Only modify LPN sub/loc if it is WIP LPN, since the RCV case
7615    -- won't even reach this point
7616    IF (l_lpn_context=2 AND l_sub_loc_changed = 'Y') THEN
7617       wms_container_pvt.modify_lpn_wrapper
7618         ( p_api_version    =>  1.0
7619     ,x_return_status =>  x_return_status
7620     ,x_msg_count     =>  x_msg_count
7621     ,x_msg_data      =>  x_msg_data
7622     ,p_lpn_id        =>  p_from_lpn_id
7623     ,p_subinventory  =>  l_to_sub
7624     ,p_locator_id    =>  l_to_loc
7625     );
7626       IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
7627          IF (l_debug = 1) THEN
7628       debug('Error modifying lpn',
7629       'load_lpn', 9);
7630          END IF;
7631       END IF;
7632    END IF;
7633 
7634    wms_container_pvt.packunpack_container
7635            (p_api_version       =>   1.0
7636            ,p_content_lpn_id   =>   p_from_lpn_id
7637            ,p_lpn_id           =>   p_to_lpn_id
7638            ,p_operation        =>   1 --Pack
7639            ,p_organization_id  =>   p_org_id
7640            ,p_commit           =>   fnd_api.g_false
7641            ,x_return_status    =>   x_return_status
7642            ,x_msg_count        =>   x_msg_count
7643            ,x_msg_data         =>   x_msg_data);
7644     IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
7645        IF (l_debug = 1) THEN
7646     DEBUG('Error packing content_lpn = ' ||
7647           p_from_lpn_id || ' into lpn = ' ||
7648           p_to_lpn_id,'load_lpn',9);
7649     debug('Error message:'||x_msg_data,9);
7650        END IF;
7651        RAISE fnd_api.g_exc_error;
7652     END IF;
7653        END IF; -- IF p_to_lpn_id IS NOT NULL AND p_to_lpn_id <> p_from_lpn_id AND (p_to_lpn_id <> 0) THEN
7654 
7655      END IF; --IF p_mode = 1
7656 
7657      -- only call rcv tm if loading into an lpn with different sub/loc
7658      IF l_lpn_context = 3 AND l_sub_loc_changed = 'Y' THEN
7659   l_group_id := insert_rti
7660     (p_from_org  => p_org_id
7661      ,p_lpn_id   => p_from_lpn_id
7662      ,p_to_org  => p_org_id  -- same as from lpn
7663      ,p_to_sub  => l_to_sub
7664      ,p_to_loc  => l_to_loc
7665      ,p_xfer_lpn_id => p_from_lpn_id
7666      ,p_first_time => 1
7667      ,p_mobile_txn => 'Y'
7668      ,p_txn_mode_code =>  'ONLINE'
7669      ,x_return_status => x_return_status
7670      ,x_msg_count => x_msg_count
7671      ,x_msg_data => x_msg_data);
7672   IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
7673      IF (l_debug = 1) THEN
7674         debug('Error inserting RTI');
7675      END IF;
7676      RAISE fnd_api.g_exc_error;
7677   END IF;
7678 
7679   IF (l_debug = 1 ) then
7680      debug('RTI inserted with group_id:'|| l_group_id, 9);
7681   END IF;
7682 
7683   inv_rcv_integration_apis.insert_wlpni
7684     (p_api_version    =>  1.0
7685      ,x_return_status =>  x_return_status
7686      ,x_msg_count     =>  x_msg_count
7687      ,x_msg_data      =>  x_msg_data
7688      ,p_organization_id => p_org_id
7689      ,p_lpn_id          => p_from_lpn_id
7690      ,p_license_plate_number =>  NULL
7691      ,p_lpn_group_id    =>   l_group_id
7692      ,p_parent_lpn_id   =>   p_to_lpn_id
7693      );
7694   l_is_rcv_tm_call_needed := TRUE;
7695      END IF; -- If Into LPN has different sub/loc
7696 
7697      -- Call suggestions_pub_wrapper for the from lpn
7698      suggestions_pub_wrapper(
7699           p_lpn_id                    => p_from_lpn_id
7700         , p_org_id                    => p_org_id
7701         , p_user_id                   => p_user_id
7702         , p_eqp_ins                   => p_eqp_ins
7703         , p_status                    => 4 --loaded
7704         , p_check_for_crossdock       => p_check_for_crossdock
7705         , x_number_of_rows            => l_x_num_of_rows
7706         , x_return_status             => l_x_return_status
7707         , x_msg_count                 => l_x_msg_count
7708         , x_msg_data                  => l_x_msg_data
7709         , x_crossdock                 => x_crossdock
7710      );
7711 
7712      IF(l_debug = 1) THEN
7713        debug('x_return_status from suggestions_pub ' || l_x_return_status);
7714        debug('After creating suggestions for ' || p_from_lpn_id);
7715      END IF;
7716 
7717 
7718      IF l_x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
7719 --	IF (x_msg_count = 0) THEN
7720 --	   FND_MESSAGE.SET_NAME('WMS','WMS_LOG_EXCEPTION_FAIL');
7721 --	   FND_MSG_PUB.ADD;
7722 --	END IF;
7723 	RAISE FND_API.g_exc_unexpected_error;
7724       ELSIF l_x_return_status = FND_API.G_RET_STS_ERROR THEN
7725 --	IF (x_msg_count = 0 ) THEN
7726 --	   FND_MESSAGE.SET_NAME('WMS','WMS_LOG_EXCEPTION_FAIL');
7727 --	   FND_MSG_PUB.ADD;
7728 --	END IF;
7729         RAISE FND_API.G_EXC_ERROR;
7730      END IF;
7731 
7732      --BUG 4345714: If any line is not detailed, then fail.
7733      BEGIN
7734 	SELECT  /*+ ORDERED INDEX(MTRL MTL_TXN_REQUEST_LINES_N7) */  1
7735 	  INTO l_discrepancy
7736 	  FROM
7737 	      (SELECT wlpn.lpn_id                 /*5723418*/
7738 		FROM wms_license_plate_numbers wlpn
7739 		START WITH  wlpn.lpn_id = p_from_lpn_id
7740         	CONNECT BY PRIOR wlpn.lpn_id = wlpn.parent_lpn_id) wlpn,
7741               mtl_txn_request_lines mtrl -- Bug 14372071
7742 	  WHERE mtrl.lpn_id = wlpn.lpn_id
7743 	  AND mtrl.line_status = 7
7744 	  AND (mtrl.quantity-Nvl(mtrl.quantity_delivered,0)) <> Nvl(mtrl.quantity_detailed,0)
7745 	  AND mtrl.organization_id = p_org_id;
7746      EXCEPTION
7747 	WHEN too_many_rows THEN
7748 	   l_discrepancy := 1;
7749 	WHEN no_data_found THEN
7750 	   l_discrepancy := 0;
7751 	WHEN OTHERS THEN
7752 	   l_discrepancy := 0;
7753      END;
7754 
7755      IF (l_debug = 1) THEN
7756 	debug('l_discrepancy = ' || l_discrepancy, 'load_lpn', 9);
7757      END IF;
7758 
7759      IF l_discrepancy = 1 THEN
7760 	fnd_msg_pub.count_and_get(p_count => l_msg_count, p_data =>
7761 				  l_msg_data);
7762 	IF l_debug = 1 THEN
7763 	   debug('l_msg_count: ' || l_msg_count);
7764 	   debug('l_msg_data: ' || l_msg_data);
7765 	END IF;
7766 
7767 	IF (l_msg_count IS NULL OR l_msg_count = 0) THEN
7768 	   -- use default message if there are no message on stack
7769 	   -- first reset message stack
7770 	   fnd_msg_pub.initialize();
7771 	   FND_MESSAGE.SET_NAME('WMS','WMS_ALLOCATE_FAIL');
7772 	   FND_MSG_PUB.ADD;
7773 	 ELSIF l_msg_count > 1 THEN
7774 	   FOR i IN 2 .. l_msg_count LOOP
7775 	      fnd_msg_pub.delete_msg(i);
7776 	   END LOOP;
7777 	END IF;
7778 
7779 	-- 6962664
7780         debug('Suggested locator capacity should be reverted as putaway fails');
7781         debug('Before calling the revert_loc_suggested_capacity');
7782         wms_task_dispatch_put_away.revert_loc_suggested_capacity(
7783               x_return_status       => l_x_return_status
7784             , x_msg_count           => l_x_msg_count
7785             , x_msg_data            => l_x_msg_data
7786             , p_organization_id     => p_org_id
7787             , p_lpn_id              => p_from_lpn_id
7788             );
7789         debug('After calling the revert_loc_suggested_capacity');
7790         ROLLBACK;
7791 	-- 6962664
7792 
7793 	RAISE fnd_api.g_exc_error;
7794      END IF;
7795 
7796      --  Call TM for Pack/Unpack the given lpn
7797      IF (l_is_tm_call_needed = TRUE) THEN
7798         l_ret := inv_lpn_trx_pub.process_lpn_trx(
7799                  p_trx_hdr_id => l_trx_header_id,
7800                  p_commit => fnd_api.g_false,
7801                  x_proc_msg => x_msg_data,
7802                  p_atomic => fnd_api.g_true);
7803        -- Check return value and in case of failure raise exception
7804        IF (l_ret <> 0)  THEN
7805           IF l_debug = 1 THEN
7806             debug('TM call for pack unpack faluire');
7807             debug('Failure message ' || x_msg_data);
7808           END IF;
7809           FND_MESSAGE.SET_NAME('WMS','WMS_LOG_EXCEPTION_FAIL');
7810           RAISE FND_API.G_EXC_ERROR;
7811        END IF;
7812      END IF;
7813 
7814      IF (l_is_rcv_tm_call_needed = TRUE) THEN
7815 
7816 	--BUG 3359835: Must call activate_operation_instance
7817 	l_progress := 40;
7818 	SELECT mmtt.transaction_temp_id
7819 	  bulk collect INTO l_mmtt_ids
7820 	  FROM mtl_material_transactions_temp mmtt
7821 	  WHERE mmtt.organization_id = p_org_id
7822 	  AND mmtt.move_order_line_id IN
7823 	  ( SELECT mtrl.line_id
7824 	    FROM   mtl_txn_request_lines mtrl ,
7825 	    (SELECT wlpn.lpn_id   /* 5723418 */
7826 		     FROM wms_license_plate_numbers wlpn
7827 		     START WITH wlpn.lpn_id = p_from_lpn_id
7828 		     CONNECT BY PRIOR wlpn.lpn_id = wlpn.parent_lpn_id) wlpn
7829 	    WHERE mtrl.organization_id = p_org_id
7830 	    AND mtrl.lpn_id = wlpn.lpn_id );
7831 	l_progress := 50;
7832 
7833         BEGIN
7834 	   SELECT employee_id
7835 	     INTO l_emp_id
7836 	     FROM fnd_user
7837 	     WHERE user_id = p_user_id;
7838 	EXCEPTION
7839 	   WHEN OTHERS THEN
7840 	      IF (l_debug = 1) THEN
7841 		 DEBUG('There is no employee id tied to the user','load_lpn',9);
7842 	      END IF;
7843 	      fnd_message.set_name('WMS', 'WMS_NO_EMP_FOR_USR');
7844 	      fnd_msg_pub.ADD;
7845 	      RAISE fnd_api.g_exc_error;
7846 	END;
7847 
7848 	-- BEGIN DBI FIX
7849 	l_task_execute_rec.person_id := l_emp_id;
7850 	l_task_execute_rec.organization_id := p_org_id;
7851 	l_task_execute_rec.loaded_time := Sysdate;
7852 	l_task_execute_rec.user_task_type := -1;
7853 	-- END DBI FIX
7854 
7855 	FOR i IN 1 .. l_mmtt_ids.COUNT LOOP
7856 	   IF (l_debug = 1) THEN
7857 	      DEBUG('Calling activate_operation_instance','load_lpn',1);
7858 	      DEBUG(' ( p_source_task_id     => ' || l_mmtt_ids(i),'load_lpn',9);
7859 	      DEBUG('   ,p_activity_type_id  => 1','load_lpn',9);
7860 	      DEBUG('   ,p_task_execute_rec  => l_task_execute_rec','load_lpn',9);
7861 	      DEBUG('   ,p_operation_type_id => 1 )','load_lpn',9);
7862 	   END IF;
7863 
7864 	   --Moving the following line before the loop starts to stamp the
7865 	   --same time on all tasks for the given lpn - BEGIN DBI FIX
7866 	   --l_task_execute_rec.person_id := l_emp_id;
7867 	   --l_task_execute_rec.organization_id := p_org_id;
7868 	   --l_task_execute_rec.loaded_time := Sysdate;
7869 	   --l_task_execute_rec.user_task_type := -1;
7870 	   --END DBI FIX
7871 
7872 	   wms_atf_runtime_pub_apis.activate_operation_instance
7873 	     (  p_source_task_id  => l_mmtt_ids(i)
7874 		,p_activity_id => 1 -- Inbound
7875 		,p_task_execute_rec => l_task_execute_rec
7876 		,p_operation_type_id => 1 -- load
7877 		,x_return_status     => x_return_status
7878 		,x_msg_data          => x_msg_data
7879 		,x_msg_count         => x_msg_count
7880 		,x_error_code        => l_error_code
7881 		,x_consolidation_method_id => l_consolidation_method_id
7882 		,x_drop_lpn_option   => l_drop_lpn_option
7883 		);
7884 	   IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
7885 	      IF (l_debug = 1) THEN
7886 		 DEBUG('xxx: Error in activate_operation_instance','transfer_contents',9);
7887 	      END IF;
7888 	      RAISE fnd_api.g_exc_error;
7889 	   END IF;
7890 	END LOOP;
7891 
7892 	--End of BUG FIX 3359835
7893 
7894 	l_old_txn_mode_code := inv_rcv_common_apis.g_po_startup_value.transaction_mode;
7895 	inv_rcv_common_apis.g_po_startup_value.transaction_mode :=  'ONLINE';
7896 
7897   l_progress := 100;
7898 
7899         BEGIN
7900      inv_rcv_mobile_process_txn.rcv_process_receive_txn
7901        (x_return_status    =>  x_return_status
7902         ,x_msg_data        =>  x_msg_data
7903         );
7904   EXCEPTION
7905      WHEN OTHERS THEN
7906         inv_rcv_common_apis.g_po_startup_value.transaction_mode := l_old_txn_mode_code;
7907         IF (l_debug = 1) THEN
7908      DEBUG('xxx: Error - Rcv TM Failed','load_lpn',9);
7909         END IF;
7910         RAISE fnd_api.g_exc_error;
7911   END;
7912 
7913   IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
7914      IF (l_debug = 1) THEN
7915         DEBUG('xxx: Error - Rcv TM Failed','load_lpn',9);
7916      END IF;
7917      inv_rcv_common_apis.g_po_startup_value.transaction_mode := l_old_txn_mode_code;
7918      RAISE fnd_api.g_exc_error;
7919   END IF;
7920 
7921   l_progress := 150;
7922 
7923   inv_rcv_common_apis.g_po_startup_value.transaction_mode := l_old_txn_mode_code;
7924   inv_rcv_common_apis.rcv_clear_global;
7925      END IF;
7926 
7927      -- RCV TM would call complete_operation_instance
7928      IF (l_is_rcv_tm_call_needed <> TRUE) THEN
7929         BEGIN
7930      SELECT employee_id
7931        INTO l_emp_id
7932        FROM fnd_user
7933        WHERE user_id = p_user_id;
7934   EXCEPTION
7935      WHEN OTHERS THEN
7936         IF (l_debug = 1) THEN
7937      DEBUG('There is no employee id tied to the user','transfer_contents',9);
7938         END IF;
7939         fnd_message.set_name('WMS', 'WMS_NO_EMP_FOR_USR');
7940         fnd_msg_pub.ADD;
7941         RAISE fnd_api.g_exc_error;
7942   END;
7943   debug('Calling complete_atf_load for:'||p_from_lpn_id);
7944   DEBUG(' p_org_id    => '||p_org_id,     'load_lpn',4);
7945   DEBUG(' p_emp_id    => '||l_emp_id,     'load_lpn',4);
7946 
7947   Complete_ATF_Load (x_return_status  => x_return_status
7948          ,x_msg_count      => x_msg_count
7949          ,x_msg_data       => x_msg_data
7950          ,p_org_id         => p_org_id
7951          ,p_lpn_id         => p_from_lpn_id
7952          ,p_emp_id         => l_emp_id );
7953 
7954   -- Check for the return status
7955   IF x_return_status = fnd_api.g_ret_sts_success THEN
7956      -- Activate completed successfully.
7957      NULL;
7958    ELSIF x_return_status = fnd_api.g_ret_sts_error THEN
7959      RAISE fnd_api.g_exc_error;
7960    ELSIF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
7961      RAISE fnd_api.g_exc_unexpected_error;
7962   END IF;
7963      END IF;
7964 
7965      -- Before returning commit all changes to datbase.
7966      COMMIT;
7967 
7968    EXCEPTION
7969      WHEN OTHERS THEN
7970        x_return_status := fnd_api.g_ret_sts_unexp_error;
7971        debug(SQLCODE, 1);
7972 
7973        -- Get rid of the folloiwng, as it appends a junk message to the
7974        -- error message stack
7975 --       IF SQLCODE IS NOT NULL THEN
7976 --         l_progress := 100;
7977 --         debug('Exception in load_lpn : ' || SQLCODE);
7978 --         inv_mobile_helper_functions.sql_error('load_lpn', l_progress, SQLCODE);
7979 --       END IF;
7980  END;
7981 
7982 
7983   PROCEDURE update_mo(
7984         MoLineId NUMBER,
7985         ReferenceId NUMBER,
7986         Reference VARCHAR2,
7987         Reference_type_code NUMBER,
7988         lpn_id NUMBER,
7989         wms_process_flag NUMBER,
7990         inspect_status NUMBER)
7991  IS
7992  BEGIN
7993     UPDATE mtl_txn_request_lines
7994       SET Reference_id        = decode(ReferenceId,-9999,referenceId,NULL,NULL,referenceId),
7995           Reference           = decode(Reference,'-9999',reference,NULL,NULL,reference),
7996           Reference_type_code = decode(Reference_type_code,'-9999',reference_type_code,NULL,NULL,reference_type_code),
7997           lpn_id              = decode(lpn_id,-9999,lpn_id,NULL,NULL,lpn_id),
7998           wms_process_flag    = decode(wms_process_flag,-9999,wms_process_flag,NULL,NULL,wms_process_flag),
7999           inspection_status   = decode(inspect_status,-9999,inspect_status,NULL,null,inspect_status)
8000     WHERE line_id  = MoLineid;
8001  END;
8002 
8003 
8004  FUNCTION insert_rti(p_from_org IN NUMBER
8005           ,p_lpn_id IN NUMBER
8006           ,p_to_org IN NUMBER
8007           ,p_to_sub IN VARCHAR2
8008           ,p_to_loc IN NUMBER
8009           ,p_xfer_lpn_id IN NUMBER
8010           ,p_first_time IN NUMBER
8011           ,p_mobile_txn IN VARCHAR2
8012           ,p_txn_mode_code IN VARCHAR2
8013           ,x_return_status OUT nocopy VARCHAR2
8014           ,x_msg_count OUT nocopy NUMBER
8015           ,x_msg_data OUT nocopy VARCHAR2 )
8016    RETURN NUMBER
8017    IS
8018       l_rowid VARCHAR2(40);
8019       l_interface_transaction_id NUMBER;
8020       l_group_id NUMBER;
8021       l_sysdate DATE := Sysdate;
8022       l_user_id NUMBER := fnd_global.user_id;
8023       l_login_id NUMBER := fnd_global.login_id;
8024       l_to_sub VARCHAR2(10);
8025       l_to_loc NUMBER;
8026       l_lpn_id NUMBER;
8027       l_xfer_lpn_id NUMBER;
8028       l_from_org NUMBER;
8029       l_to_org NUMBER;
8030       l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
8031       l_progress VARCHAR(3) := '0';
8032       l_txn_mode_code VARCHAR2(10);
8033       l_error_code NUMBER;
8034 
8035       l_operating_unit_id MO_GLOB_ORG_ACCESS_TMP.ORGANIZATION_ID%TYPE;   --<R12 MOAC>
8036 
8037  BEGIN
8038     x_return_status := fnd_api.g_ret_sts_success;
8039 
8040     l_to_sub := p_to_sub;
8041     l_to_loc  := p_to_loc;
8042     l_lpn_id  := p_lpn_id;
8043     l_xfer_lpn_id  := p_xfer_lpn_id;
8044     l_from_org := p_from_org;
8045     l_to_org := p_to_org;
8046     l_txn_mode_code := p_txn_mode_code;
8047 
8048     IF (l_debug = 1) THEN
8049        DEBUG('Entering...', 'insert_rti', 9);
8050        debug(' (p_to_sub => ' || p_to_sub, 'insert_rti', 9);
8051        debug('  p_from_org => ' || p_from_org, 'insert_rti', 9);
8052        debug('  p_to_org => ' || p_to_org, 'insert_rti', 9);
8053        debug('  p_to_loc => ' || p_to_loc, 'insert_rti', 9);
8054        debug('  p_lpn_id => ' || p_lpn_id, 'insert_rti', 9);
8055        debug('  p_xfer_lpn_id   => ' || p_xfer_lpn_id, 'insert_rti', 9);
8056        debug('  p_first_time    => ' || p_first_time, 'insert_rti', 9);
8057        debug('  p_mobile_txn    => ' || p_mobile_txn, 'insert_rti', 9);
8058        debug('  p_txn_mode_code => ' || p_txn_mode_code||')', 'insert_rti', 9);
8059     END IF;
8060 
8061     IF (p_first_time = 1) THEN
8062        IF (l_debug = 1) THEN
8063     debug('Calling init_startup_values','insert_rti',9);
8064     debug('  p_org_id => '||l_from_org, 'insert_rti',9);
8065        END IF;
8066 
8067        BEGIN
8068     inv_rcv_common_apis.init_startup_values(l_from_org);
8069        EXCEPTION
8070     WHEN NO_DATA_FOUND THEN
8071        fnd_message.set_name('INV', 'INV_RCV_PARAM');
8072        fnd_msg_pub.ADD;
8073        RAISE fnd_api.g_exc_error;
8074        END;
8075 
8076        l_progress := '2';
8077 
8078        IF inv_rcv_common_apis.g_po_startup_value.sob_id IS NULL THEN
8079 	  --BUG 3435079: For performance reason, do not go against the view
8080 	  --org_organization_definitions.  instead, use
8081 	  --the query below
8082 	  SELECT TO_NUMBER(hoi.org_information1)
8083 	    INTO  inv_rcv_common_apis.g_po_startup_value.sob_id
8084 	    FROM hr_organization_information hoi
8085 	    WHERE hoi.organization_id = l_from_org
8086 	    AND (hoi.org_information_context || '') = 'Accounting Information';
8087 
8088        END IF;
8089 
8090        l_progress := '4';
8091 
8092        IF (l_debug = 1) THEN
8093 	  debug('init_startup_value exited.  Calling validate_trx_date'
8094 		,'insert_rti', 9);
8095        END IF;
8096 
8097        inv_rcv_common_apis.validate_trx_date
8098 	 (
8099 	  p_trx_date              => SYSDATE
8100 	  , p_organization_id     => l_from_org
8101 	  , p_sob_id              => inv_rcv_common_apis.g_po_startup_value.sob_id
8102 	  , x_return_status       => x_return_status
8103 	  , x_error_code          => l_error_code
8104 	  );
8105 
8106        IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
8107     IF (l_debug = 1)THEN
8108        debug('Date validation failed','insert_rti',9);
8109     END IF;
8110     RAISE fnd_api.g_exc_error;
8111        END IF;
8112 
8113        l_progress := '6';
8114 
8115     END IF; --IF (p_first_time = 1)
8116 
8117     SELECT rcv_transactions_interface_s.NEXTVAL
8118       INTO l_interface_transaction_id
8119       FROM dual;
8120 
8121     -- init_start_values should have called gen_txn_group_id
8122     l_group_id := inv_rcv_common_apis.g_rcv_global_var.interface_group_id;
8123 
8124     IF (l_debug = 1) THEN
8125        DEBUG('Calling rcv_trx_interface_insert_pkg.insert_row',
8126              'insert_rti', 9);
8127     END IF;
8128 
8129     SELECT org_information2
8130       INTO l_operating_unit_id
8131       FROM hr_organization_information
8132       WHERE organization_id = l_to_org
8133       AND org_information_context || '' ='Accounting Information';    --<R12 MOAC>
8134 
8135     l_progress := '10';
8136 
8137     rcv_trx_interface_insert_pkg.insert_row
8138       (
8139        x_rowid                      =>  l_rowid
8140        ,x_interface_transaction_id  =>  l_interface_transaction_id
8141        ,x_group_id                  =>  l_group_id
8142        ,x_last_update_date          =>  l_sysdate
8143        ,x_last_updated_by           =>  l_user_id
8144        ,x_creation_date             =>  l_sysdate
8145        ,x_created_by                =>  l_user_id
8146        ,x_last_update_login         =>  l_login_id
8147        ,x_transaction_type          =>  'TRANSFER'
8148        ,x_transaction_date          =>  l_sysdate
8149        ,x_processing_status_code    =>  'PENDING'
8150        ,x_processing_mode_code      =>  l_txn_mode_code
8151        ,x_processing_request_id     =>  NULL
8152        ,x_transaction_status_code   =>  'PENDING'
8153        ,x_category_id               =>  NULL
8154        ,x_quantity                  =>  0
8155        ,x_unit_of_measure           =>  'X'
8156        ,x_interface_source_code     =>  'RCV'
8157        ,x_interface_source_line_id  =>  NULL
8158        ,x_inv_transaction_id        =>  NULL
8159        ,x_item_id                   =>  NULL
8160       ,x_item_description          =>  NULL
8161       ,x_item_revision             =>  NULL
8162       ,x_uom_code                  =>  NULL
8163       ,x_employee_id               =>  l_user_id
8164       ,x_auto_transact_code        =>  NULL
8165       ,x_shipment_header_id        =>  NULL
8166       ,x_shipment_line_id          =>  NULL
8167       ,x_ship_to_location_id       =>  NULL
8168       ,x_primary_quantity          =>  NULL
8169       ,x_primary_unit_of_measure   =>  NULL
8170       ,x_receipt_source_code       =>  NULL
8171       ,x_vendor_id                 =>  NULL
8172       ,x_vendor_site_id            =>  NULL
8173       ,x_from_organization_id      =>  l_from_org
8174       ,x_to_organization_id        =>  l_to_org
8175       ,x_routing_header_id         =>  NULL
8176       ,x_routing_step_id           =>  NULL
8177       ,x_source_document_code      =>  NULL
8178       ,x_parent_transaction_id     =>  NULL
8179       ,x_po_header_id              =>  NULL
8180       ,x_po_revision_num           =>  NULL
8181       ,x_po_release_id             =>  NULL
8182       ,x_po_line_id                =>  NULL
8183       ,x_po_line_location_id       =>  NULL
8184       ,x_po_unit_price             =>  NULL
8185       ,x_currency_code             =>  NULL
8186       ,x_currency_conversion_type  =>  NULL
8187       ,x_currency_conversion_rate  =>  NULL
8188       ,x_currency_conversion_date  =>  NULL
8189       ,x_po_distribution_id        =>  NULL
8190       ,x_requisition_line_id       =>  NULL
8191       ,x_req_distribution_id       =>  NULL
8192       ,x_charge_account_id         =>  NULL
8193       ,x_substitute_unordered_code =>  NULL
8194       ,x_receipt_exception_flag    =>  NULL
8195       ,x_accrual_status_code       =>  NULL
8196       ,x_inspection_status_code    =>  NULL
8197       ,x_inspection_quality_code   =>  NULL
8198       ,x_destination_type_code     =>  'RECEIVING'
8199       ,x_deliver_to_person_id      =>  NULL
8200       ,x_location_id               =>  NULL
8201       ,x_deliver_to_location_id    =>  NULL
8202       ,x_subinventory              =>  l_to_sub
8203       ,x_locator_id                =>  l_to_loc
8204       ,x_wip_entity_id             =>  NULL
8205       ,x_wip_line_id               =>  NULL
8206       ,x_department_code           =>  NULL
8207       ,x_wip_repetitive_schedule_id=>  NULL
8208       ,x_wip_operation_seq_num     =>  NULL
8209       ,x_wip_resource_seq_num      =>  NULL
8210       ,x_bom_resource_id           =>  NULL
8211       ,x_shipment_num              =>  NULL
8212       ,x_freight_carrier_code      =>  NULL
8213       ,x_bill_of_lading            =>  NULL
8214       ,x_packing_slip              =>  NULL
8215       ,x_shipped_date              =>  NULL
8216       ,x_expected_receipt_date     =>  NULL
8217       ,x_actual_cost               =>  NULL
8218       ,x_transfer_cost             =>  NULL
8219       ,x_transportation_cost       =>  NULL
8220       ,x_transportation_account_id =>  NULL
8221       ,x_num_of_containers         =>  NULL
8222       ,x_waybill_airbill_num       =>  NULL
8223       ,x_vendor_item_num           =>  NULL
8224       ,x_vendor_lot_num            =>  NULL
8225       ,x_rma_reference             =>  NULL
8226       ,x_comments                  =>  NULL
8227       ,x_attribute_category        =>  NULL
8228       ,x_attribute1                =>  NULL
8229       ,x_attribute2                =>  NULL
8230       ,x_attribute3                =>  NULL
8231       ,x_attribute4                =>  NULL
8232       ,x_attribute5                =>  NULL
8233       ,x_attribute6                =>  NULL
8234       ,x_attribute7                =>  NULL
8235       ,x_attribute8                =>  NULL
8236       ,x_attribute9                =>  NULL
8237       ,x_attribute10               =>  NULL
8238       ,x_attribute11               =>  NULL
8239       ,x_attribute12               =>  NULL
8240       ,x_attribute13               =>  NULL
8241       ,x_attribute14               =>  NULL
8242       ,x_attribute15               =>  NULL
8243       ,x_ship_head_attribute_category =>  NULL
8244       ,x_ship_head_attribute1      =>  NULL
8245       ,x_ship_head_attribute2      =>  NULL
8246       ,x_ship_head_attribute3      =>  NULL
8247       ,x_ship_head_attribute4      =>  NULL
8248       ,x_ship_head_attribute5      =>  NULL
8249       ,x_ship_head_attribute6      =>  NULL
8250       ,x_ship_head_attribute7      =>  NULL
8251       ,x_ship_head_attribute8      =>  NULL
8252       ,x_ship_head_attribute9      =>  NULL
8253       ,x_ship_head_attribute10     =>  NULL
8254       ,x_ship_head_attribute11     =>  NULL
8255       ,x_ship_head_attribute12     =>  NULL
8256       ,x_ship_head_attribute13     =>  NULL
8257       ,x_ship_head_attribute14     =>  NULL
8258       ,x_ship_head_attribute15     =>  NULL
8259       ,x_ship_line_attribute_category =>  NULL
8260       ,x_ship_line_attribute1      =>  NULL
8261       ,x_ship_line_attribute2      =>  NULL
8262       ,x_ship_line_attribute3      =>  NULL
8263       ,x_ship_line_attribute4      =>  NULL
8264       ,x_ship_line_attribute5      =>  NULL
8265       ,x_ship_line_attribute6      =>  NULL
8266       ,x_ship_line_attribute7      =>  NULL
8267       ,x_ship_line_attribute8      =>  NULL
8268       ,x_ship_line_attribute9      =>  NULL
8269       ,x_ship_line_attribute10     =>  NULL
8270       ,x_ship_line_attribute11     =>  NULL
8271       ,x_ship_line_attribute12     =>  NULL
8272       ,x_ship_line_attribute13     =>  NULL
8273       ,x_ship_line_attribute14     =>  NULL
8274       ,x_ship_line_attribute15     =>  NULL
8275       ,x_ussgl_transaction_code    =>  NULL
8276       ,x_government_context        =>  NULL
8277       ,x_reason_id                 =>  NULL
8278       ,x_destination_context       =>  NULL
8279       ,x_source_doc_quantity       =>  NULL
8280       ,x_source_doc_unit_of_measure=>  NULL
8281       ,x_lot_number_cc             =>  NULL
8282       ,x_serial_number_cc          =>  NULL
8283       ,x_qa_collection_id          =>  NULL
8284       ,x_country_of_origin_code    =>  NULL
8285       ,x_oe_order_header_id        =>  NULL
8286       ,x_oe_order_line_id          =>  NULL
8287       ,x_customer_item_num         =>  NULL
8288       ,x_customer_id               =>  NULL
8289       ,x_customer_site_id          =>  NULL
8290       ,x_put_away_rule_id          =>  NULL
8291       ,x_put_away_strategy_id      =>  NULL
8292       ,x_lpn_id                    =>  l_lpn_id
8293       ,x_transfer_lpn_id           =>  l_xfer_lpn_id
8294       ,x_cost_group_id             =>  NULL
8295       ,x_mmtt_temp_id              =>  NULL
8296       ,x_mobile_txn                =>  p_mobile_txn
8297       ,x_transfer_cost_group_id    =>  NULL
8298       ,x_secondary_quantity        =>  NULL
8299       ,x_secondary_unit_of_measure =>  NULL
8300       ,p_org_id                    =>  l_operating_unit_id   --<R12 MOAC>
8301       );
8302 
8303     UPDATE
8304       rcv_transactions_interface
8305     SET
8306       validation_flag = 'Y'
8307       ,lpn_group_id = l_group_id
8308       WHERE ROWID = l_rowid;
8309 
8310     l_progress := '20';
8311 
8312     IF (l_debug = 1) THEN
8313        DEBUG('Quiting with group_id = '||l_group_id, 'insert_rti', 9);
8314     END IF;
8315 
8316     RETURN l_group_id;
8317  EXCEPTION
8318     WHEN OTHERS THEN
8319        x_return_status := fnd_api.g_ret_sts_error;
8320        IF (l_debug = 1) THEN
8321           debug('Error after progress = ' || l_progress,
8322                 'insert_rti',
8323                 9);
8324        END IF;
8325        IF SQLCODE IS NOT NULL THEN
8326           IF (l_debug = 1) THEN
8327              debug(SQLCODE, 'insert_rti', 9);
8328           END IF;
8329        END IF;
8330        RETURN -1;
8331  END insert_rti;
8332 
8333 
8334  PROCEDURE pre_process_lpn (
8335     x_return_status           OUT  NOCOPY VARCHAR2
8336   , x_msg_count               OUT  NOCOPY NUMBER
8337   , x_msg_data                OUT  NOCOPY VARCHAR2
8338   , p_from_lpn_id             IN  NUMBER
8339   , p_organization_id         IN NUMBER
8340   , p_subinventory_code       IN VARCHAR2
8341   , p_lpn_mode                IN NUMBER
8342   , p_locator_id              IN NUMBER
8343   , p_to_lpn_id               IN NUMBER
8344   , p_project_id              IN NUMBER   DEFAULT NULL
8345   , p_task_id                 IN NUMBER   DEFAULT NULL
8346   , p_user_id                 IN NUMBER
8347   , p_lpn_context             IN NUMBER
8348   , p_batch_id                IN NUMBER
8349   , p_batch_seq               IN NUMBER
8350   , p_pack_trans_id           IN NUMBER
8351   , x_batch_seq               OUT NOCOPY NUMBER --BUG 3544918
8352 ) IS
8353    l_debug             NUMBER   := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
8354    l_group_id          NUMBER;
8355    l_progress          NUMBER;
8356    l_ret               NUMBER;
8357    l_lpn_id            NUMBER;
8358    p_parent_lpn_id     NUMBER;
8359    l_lpn_sub           mtl_secondary_inventories.secondary_inventory_name%TYPE;
8360    l_lpn_loc           NUMBER;
8361    l_to_lpn_id         NUMBER;
8362 
8363 
8364    CURSOR lpn_cursor IS
8365    SELECT lpn_id
8366      FROM wms_license_plate_numbers
8367      WHERE parent_lpn_id = p_from_lpn_id;
8368 
8369    l_batch_seq NUMBER := p_batch_seq;
8370 
8371  BEGIN
8372 
8373    l_progress := 10;
8374 
8375    x_return_status := fnd_api.g_ret_sts_success;
8376 
8377    IF (l_debug = 1) THEN
8378      debug('Pre_Process_lpn : Patchset J code - Nested LPNs are supported');
8379      debug(' p_from_lpn_id: '||p_from_lpn_id);
8380      debug(' p_to_lpn_id: '||p_to_lpn_id);
8381    END IF;
8382 
8383    BEGIN
8384       -- Get Parent lpn id
8385       SELECT parent_lpn_id
8386 	,subinventory_code
8387 	,locator_id
8388 	INTO p_parent_lpn_id
8389 	,l_lpn_sub
8390 	,l_lpn_loc
8391 	FROM wms_license_plate_numbers
8392 	WHERE lpn_id = p_from_lpn_id;
8393    EXCEPTION
8394       WHEN OTHERS THEN
8395 	 IF (l_debug = 1) THEN
8396 	    debug('Error retrieving parent_lpn_id');
8397 	    debug('SQLCODE:'||SQLCODE||' SQLERRM'||Sqlerrm,9);
8398 	 END IF;
8399    END;
8400 
8401    -- Get LPN group Id
8402    IF p_lpn_context = 3 THEN
8403      IF inv_rcv_common_apis.g_rcv_global_var.interface_group_id IS NULL   THEN
8404        SELECT rcv_interface_groups_s.NEXTVAL
8405        INTO   l_group_id
8406        FROM   DUAL;
8407        inv_rcv_common_apis.g_rcv_global_var.interface_group_id := l_group_id;
8408      ELSE
8409        l_group_id := inv_rcv_common_apis.g_rcv_global_var.interface_group_id;
8410      END IF;
8411    END IF;
8412 
8413    -- Drop Entire LPN,
8414    IF p_lpn_mode = 2 THEN
8415 
8416      -- Process Resides in inventory LPN
8417      IF p_lpn_context = 1 AND p_parent_lpn_id IS NOT NULL THEN
8418 
8419        IF(l_debug =1) THEN
8420          debug('Inventory LPN, Parent lpn is not null, Unpack '||p_from_lpn_id|| ' from ' ||p_parent_lpn_id);
8421        END IF;
8422 
8423        -- Unpack the given LPN from its parent.
8424        PackUnpack_LPN(
8425                p_from_lpn_id   => p_parent_lpn_id
8426              , p_to_lpn_id     => p_from_lpn_id
8427              , p_org_id        => p_organization_id
8428              , p_sub_code      => l_lpn_sub
8429              , p_loc_id        => l_lpn_loc
8430              , p_user_id       => p_user_id
8431              , p_project_id    => p_project_id
8432              , p_task_id       => p_task_id
8433              , p_trx_header_id => p_pack_trans_id
8434              , p_mode          => 2 -- unpack
8435              , p_batch_id      => p_batch_id
8436              , p_batch_seq     => l_batch_seq
8437              , x_return_status => x_return_status
8438              , x_msg_count     => x_msg_count
8439              , x_msg_data      => x_msg_data);
8440 
8441        -- Check return value and in case of failure raise exception
8442        IF (l_ret <> 0)  THEN
8443          IF l_debug = 1 THEN
8444            debug('TM call for pack unpack faluire');
8445            debug('Failure message ' || x_msg_data);
8446          END IF;
8447          FND_MESSAGE.SET_NAME('WMS','WMS_LOG_EXCEPTION_FAIL');
8448          RAISE FND_API.G_EXC_ERROR;
8449        END IF;
8450 
8451        l_batch_seq := l_batch_seq + 1;
8452      END IF;
8453 
8454      -- Process Resides in Wip LPN
8455      IF p_lpn_context=2 AND  p_parent_lpn_id  IS NOT NULL THEN
8456 
8457        IF(l_debug = 1) THEN
8458           debug('Resides in WIP LPN, Parent lpn is not null, Unpack '||p_from_lpn_id||' from ' ||p_parent_lpn_id);
8459        END IF;
8460 
8461        -- unpack from LPN from its parent.
8462        wms_container_pvt.packunpack_container
8463          (p_api_version       =>   1.0
8464          ,p_content_lpn_id   =>   p_from_lpn_id
8465          ,p_lpn_id           =>   p_parent_lpn_id
8466          ,p_operation        =>   2 -- Unpack
8467          ,p_organization_id  =>   p_organization_id
8468          ,p_commit           =>   fnd_api.g_false
8469          ,x_return_status    =>   x_return_status
8470          ,x_msg_count        =>   x_msg_count
8471          ,x_msg_data         =>   x_msg_data);
8472 
8473      END IF;
8474 
8475      -- Process Resides in receiving LPN
8476      IF p_lpn_context = 3 THEN
8477 
8478          IF p_from_lpn_id = p_to_lpn_id  THEN
8479            l_to_lpn_id := NULL;
8480          ELSE
8481            l_to_lpn_id := p_to_lpn_id;
8482          END IF;
8483 
8484          inv_rcv_integration_apis.insert_wlpni(
8485            p_api_version           => 1.0
8486           ,x_return_status         => x_return_status
8487           ,x_msg_count             => x_msg_count
8488           ,x_msg_data              => x_msg_data
8489           ,p_organization_id       => p_organization_id
8490           ,p_lpn_id                => p_from_lpn_id
8491           ,p_license_plate_number  => NULL
8492           ,p_lpn_group_id          => l_group_id
8493           ,p_parent_lpn_id         => l_to_lpn_id);
8494 
8495       END IF;
8496    END IF;
8497 
8498    -- If p_lpn_mode is transfer all contents then do the following.
8499    IF p_lpn_mode = 1  THEN
8500 
8501        OPEN lpn_cursor;
8502        LOOP
8503          FETCH lpn_cursor INTO l_lpn_id;
8504          EXIT WHEN lpn_cursor%NOTFOUND;
8505 
8506          --UnPack child LPNs from the ginve LPN Resides in inventory
8507          IF p_lpn_context = 1  THEN
8508 
8509              IF(l_debug =1) THEN
8510                debug('Inventory LPN, Parent lpn is not null, Unpack '||l_lpn_id|| ' from ' ||p_from_lpn_id);
8511              END IF;
8512 
8513              PackUnpack_LPN(
8514                      p_from_lpn_id   => p_from_lpn_id
8515                    , p_to_lpn_id     => l_lpn_id --BUG 3544918
8516                    , p_org_id        => p_organization_id
8517                    , p_sub_code      => l_lpn_sub
8518                    , p_loc_id        => l_lpn_loc
8519                    , p_user_id       => p_user_id
8520                    , p_project_id    => p_project_id
8521                    , p_task_id       => p_task_id
8522                    , p_trx_header_id => p_pack_trans_id
8523                    , p_mode          => 2
8524 	           , p_batch_id      => p_batch_id
8525 	           , p_batch_seq     => l_batch_seq
8526                    , x_return_status => x_return_status
8527                    , x_msg_count     => x_msg_count
8528                    , x_msg_data      => x_msg_data);
8529 
8530              -- Check return value and in case of failure raise exception
8531              IF (l_ret <> 0)  THEN
8532                IF l_debug = 1 THEN
8533                  debug('TM call for pack unpack faluire');
8534                  debug('Failure message ' || x_msg_data);
8535                END IF;
8536                FND_MESSAGE.SET_NAME('WMS','WMS_LOG_EXCEPTION_FAIL');
8537                RAISE FND_API.G_EXC_ERROR;
8538              END IF; -- if l_ret<> 0
8539 
8540 	     l_batch_seq := l_batch_seq + 1;
8541 
8542          -- If the lpn context is resides in WIP and transaction mode is transfer all
8543          -- contents, then unpack each child LPN lpn from its parent
8544          ELSIF p_lpn_context = 2 THEN
8545 
8546                 IF(l_debug = 1) THEN
8547                    debug('WIP  LPN, Parent lpn is not null, Unpack '||l_lpn_id||' from ' ||p_from_lpn_id);
8548                 END IF;
8549 
8550                 wms_container_pvt.packunpack_container
8551                   (p_api_version       =>   1.0
8552                   ,p_content_lpn_id   =>   l_lpn_id      -- child LPN ID
8553                   ,p_lpn_id           =>   p_from_lpn_id -- Parent LPN ID
8554                   ,p_operation        =>   2 -- Unpack
8555                   ,p_organization_id  =>   p_organization_id
8556                   ,p_commit           =>   fnd_api.g_false
8557                   ,x_return_status    =>   x_return_status
8558                   ,x_msg_count        =>   x_msg_count
8559                   ,x_msg_data         =>   x_msg_data);
8560 
8561          ELSIF p_lpn_context = 3 THEN
8562 
8563                   -- Resides in Receving LPN, Unpack all child LPNs from their parents.
8564                   inv_rcv_integration_apis.insert_wlpni(
8565                     p_api_version           => 1.0
8566                    ,x_return_status         => x_return_status
8567                    ,x_msg_count             => x_msg_count
8568                    ,x_msg_data              => x_msg_data
8569                    ,p_organization_id       => p_organization_id
8570                    ,p_lpn_id                => l_lpn_id
8571                    ,p_license_plate_number  => NULL
8572                    ,p_lpn_group_id          => l_group_id
8573                    ,p_parent_lpn_id         => p_to_lpn_id);
8574          END IF;     -- If lpn context =1
8575        END LOOP;
8576 
8577        IF lpn_cursor%isopen THEN
8578          CLOSE lpn_cursor;
8579        END IF;
8580 
8581        x_batch_seq := l_batch_seq;
8582    END IF;
8583 
8584 
8585  EXCEPTION
8586 
8587    WHEN OTHERS THEN
8588      x_return_status := fnd_api.g_ret_sts_unexp_error;
8589      debug(SQLCODE, 1);
8590 
8591      IF lpn_cursor%isopen THEN
8592        CLOSE lpn_cursor;
8593      END IF;
8594 
8595      IF SQLCODE IS NOT NULL THEN
8596        l_progress := 100;
8597        inv_mobile_helper_functions.sql_error('pre_process_lpn', l_progress, SQLCODE);
8598      END IF;
8599 
8600  END pre_process_lpn;
8601 
8602  PROCEDURE post_process_lpn (
8603     x_return_status           OUT  NOCOPY VARCHAR2
8604   , x_msg_count               OUT  NOCOPY NUMBER
8605   , x_msg_data                OUT  NOCOPY VARCHAR2
8606   , p_from_lpn_id             IN NUMBER
8607   , p_organization_id         IN NUMBER
8608   , p_subinventory_code       IN VARCHAR2
8609   , p_lpn_mode                IN VARCHAR2
8610   , p_locator_id              IN NUMBER
8611   , p_to_lpn_id               IN NUMBER
8612   , p_project_id              IN NUMBER   DEFAULT NULL
8613   , p_task_id                 IN NUMBER   DEFAULT NULL
8614   , p_user_id                 IN NUMBER
8615   , p_lpn_context             IN NUMBER
8616   , p_pack_trans_id           IN NUMBER
8617   , p_batch_id                IN NUMBER
8618   , p_batch_seq               IN NUMBER
8619  ) IS
8620    l_debug             NUMBER   := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
8621    l_group_id          NUMBER;
8622    l_progress          NUMBER;
8623    l_ret               NUMBER;
8624    l_lpn_id            NUMBER;
8625 
8626    CURSOR lpn_cursor IS
8627    SELECT lpn_id
8628      FROM wms_license_plate_numbers
8629     WHERE parent_lpn_id = p_from_lpn_id;
8630 
8631    l_batch_seq NUMBER := p_batch_seq; --BUG 3544918
8632  BEGIN
8633 
8634    l_progress := 10;
8635 
8636    x_return_status := fnd_api.g_ret_sts_success;
8637 
8638    IF (l_debug = 1) THEN
8639      debug('Post_Process_lpn : Patchset J code - Nested LPNs are supported');
8640    END IF;
8641 
8642 
8643    -- Drop entire LPN
8644    IF(p_lpn_mode = 2 ) THEN
8645 
8646      IF  (p_lpn_context = 1  OR p_lpn_context = 2) AND (p_to_lpn_id IS NOT NULL) AND
8647          (p_to_lpn_id <> p_from_lpn_id) AND (p_to_lpn_id <> 0) THEN
8648 
8649        IF (l_debug = 1) THEN
8650          debug('Inventory/WP LPN, To lpn is not null, Pack  '||p_from_lpn_id|| ' Into  ' ||p_to_lpn_id);
8651        END IF;
8652 
8653        PackUnpack_LPN(
8654                p_from_lpn_id   => p_from_lpn_id
8655              , p_to_lpn_id     => p_to_lpn_id
8656              , p_org_id        => p_organization_id
8657              , p_sub_code      => p_subinventory_code
8658              , p_loc_id        => p_locator_id
8659              , p_user_id       => p_user_id
8660              , p_project_id    => p_project_id
8661              , p_task_id       => p_task_id
8662              , p_trx_header_id => p_pack_trans_id
8663              , p_mode          => 1 -- Pack
8664              , p_batch_id      => p_batch_id
8665              , p_batch_seq     => l_batch_seq
8666              , x_return_status => x_return_status
8667              , x_msg_count     => x_msg_count
8668              , x_msg_data      => x_msg_data);
8669 
8670      END IF;
8671    END IF;
8672 
8673    -- Transfer All contents
8674    IF p_lpn_mode = 1  THEN
8675 
8676      OPEN lpn_cursor;
8677      LOOP
8678        FETCH lpn_cursor INTO l_lpn_id;
8679        EXIT WHEN lpn_cursor%NOTFOUND;
8680 
8681        IF  (p_lpn_context = 1  OR p_lpn_context = 2) AND (p_to_lpn_id IS NOT NULL) AND
8682            (p_to_lpn_id <> p_from_lpn_id) AND (p_to_lpn_id <> 0) THEN
8683 
8684          IF (l_debug = 1) THEN
8685            debug('Inventory/WP LPN, To lpn is not null, Pack  '||l_lpn_id || ' Into  ' ||p_to_lpn_id);
8686          END IF;
8687 
8688          PackUnpack_LPN(
8689                  p_from_lpn_id   => l_lpn_id
8690                , p_to_lpn_id     => p_to_lpn_id
8691                , p_org_id        => p_organization_id
8692                , p_sub_code      => p_subinventory_code
8693                , p_loc_id        => p_locator_id
8694                , p_user_id       => p_user_id
8695                , p_project_id    => p_project_id
8696                , p_task_id       => p_task_id
8697                , p_trx_header_id => p_pack_trans_id
8698                , p_mode          => 1 -- Pack
8699                , p_batch_id      => p_batch_id
8700                , p_batch_seq     => l_batch_seq
8701                , x_return_status => x_return_status
8702                , x_msg_count     => x_msg_count
8703                , x_msg_data      => x_msg_data);
8704 	 l_batch_seq := l_batch_seq + 1;
8705        END IF;
8706      END LOOP;
8707 
8708    END IF;
8709 
8710    IF lpn_cursor%isopen THEN
8711      CLOSE lpn_cursor;
8712    END IF;
8713 
8714 
8715  EXCEPTION
8716 
8717    WHEN OTHERS THEN
8718      x_return_status := fnd_api.g_ret_sts_unexp_error;
8719      debug(SQLCODE, 1);
8720 
8721      IF lpn_cursor%isopen THEN
8722        CLOSE lpn_cursor;
8723      END IF;
8724 
8725      IF SQLCODE IS NOT NULL THEN
8726        l_progress := 100;
8727        inv_mobile_helper_functions.sql_error('post_process_lpn', l_progress, SQLCODE);
8728      END IF;
8729 
8730  END post_process_lpn;
8731 
8732  PROCEDURE transfer_contents
8733    (
8734     p_org_id           IN             NUMBER    DEFAULT NULL
8735     , p_sub_code       IN             VARCHAR2 DEFAULT NULL
8736     , p_loc_id         IN             NUMBER   DEFAULT NULL
8737     , p_from_lpn_id    IN             NUMBER
8738     , p_into_lpn_id    IN             NUMBER
8739     , p_operation      IN             VARCHAR2
8740     , p_mode           IN             NUMBER
8741     , p_user_id        IN             NUMBER
8742     , p_eqp_ins        IN             VARCHAR2
8743     , p_project_id     IN             NUMBER   DEFAULT NULL
8744     , p_task_id        IN             NUMBER   DEFAULT NULL
8745     , x_return_status  OUT NOCOPY     VARCHAR2
8746     , x_msg_count      OUT NOCOPY     NUMBER
8747     , x_msg_data       OUT NOCOPY     VARCHAR2 )
8748    IS
8749       TYPE number_tb_tp IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
8750       TYPE varchar30_tb_tp IS TABLE OF VARCHAR2(30) INDEX BY BINARY_INTEGER;
8751       l_from_context NUMBER;
8752       l_into_context NUMBER;
8753       l_is_loaded  VARCHAR2(1);
8754       l_number_of_rows NUMBER;
8755       l_crossdock  VARCHAR2(1); --??
8756       l_count NUMBER;
8757       l_org_id NUMBER;
8758       l_lpn_ids number_tb_tp;
8759       l_serial_numbers varchar30_tb_tp;
8760       l_txn_tmp_id_tb number_tb_tp;
8761       l_txn_tmp_id NUMBER;
8762       l_hdr_id NUMBER;
8763       l_sub_code VARCHAR2(10);
8764       l_loc_id NUMBER;
8765       l_to_sub VARCHAR2(10);
8766       l_to_loc NUMBER;
8767       l_ser_trx_id NUMBER;
8768       l_return_status NUMBER;
8769       l_task_execute_rec wms_dispatched_tasks%ROWTYPE;
8770       l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
8771       l_progress VARCHAR2(10) := '0';
8772       l_user_id NUMBER;
8773       l_emp_id NUMBER;
8774       l_error_code NUMBER;
8775       l_message VARCHAR2(1);
8776       l_prim_qty NUMBER;
8777       l_loose_contents NUMBER := 0;
8778       l_old_txn_mode_code VARCHAR2(10);
8779       l_inspection_flag NUMBER;
8780       l_load_flag  NUMBER;
8781       l_drop_flag NUMBER;
8782       l_load_prim_quantity NUMBER;
8783       l_inspect_prim_quantity NUMBER;
8784       l_drop_prim_quantity NUMBER;
8785       l_sub_loc_changed VARCHAR2(1);
8786       l_batch_seq NUMBER := 1;
8787       l_uom VARCHAR2(5);
8788 
8789       l_consolidation_method_id NUMBER;
8790       l_drop_lpn_option NUMBER;
8791 
8792       CURSOR wlpnc_cur IS
8793          SELECT
8794            wlpnc.inventory_item_id
8795            ,wlpnc.organization_id
8796            ,wlpnc.revision
8797            ,wlpnc.lot_number
8798            ,wlpnc.quantity
8799            ,wlpnc.uom_code
8800 	   ,wlpnc.secondary_quantity  -- Added for Bug 10301152
8801            ,wlpnc.secondary_uom_code  -- Added for Bug 10301152
8802            ,msi.serial_number_control_code
8803      ,msi.primary_uom_code
8804            FROM
8805            wms_lpn_contents wlpnc, mtl_system_items msi
8806            WHERE
8807            wlpnc.parent_lpn_id = p_from_lpn_id
8808            AND wlpnc.inventory_item_id = msi.inventory_item_id
8809      AND wlpnc.organization_id = msi.organization_id;
8810 
8811       l_msg_count                 NUMBER;
8812       l_msg_data                  VARCHAR2(10000);
8813 
8814  BEGIN
8815     IF (l_debug = 0) THEN
8816        DEBUG('Entering transfer_contents.pls', 'transfer_contents', 9);
8817        DEBUG('  ( p_from_lpn_id => ' || p_from_lpn_id,'transfer_contents',9);
8818        DEBUG('    ,p_into_lpn_id=> ' || p_into_lpn_id,'transfer_contents',9);
8819        DEBUG('    ,p_org_id     => ' || p_org_id,'transfer_contents',9);
8820        DEBUG('    ,p_operation  => ' || p_operation,'transfer_contents',9);
8821        DEBUG('    ,p_mode       => ' || p_mode,'transfer_contents',9);
8822        DEBUG('    ,p_user_id    => ' || p_user_id,'transfer_contents',9);
8823        DEBUG('    ,p_eqp_ins    => ' || p_eqp_ins || ')','transfer_contents',9);
8824     END IF;
8825     -- ignore operation/mode.
8826     -- assume 'XFER_CONTENTS' and 'LOAD' for now
8827 
8828     SAVEPOINT transfer_contents_pub;
8829 
8830     l_progress := '3';
8831 
8832     IF (p_user_id IS NULL) THEN
8833        l_user_id := fnd_global.user_id;
8834      ELSE
8835        l_user_id := p_user_id;
8836     END IF;
8837 
8838     l_progress := '5';
8839     BEGIN
8840        SELECT employee_id
8841          INTO l_emp_id
8842          FROM fnd_user
8843          WHERE user_id = l_user_id;
8844     EXCEPTION
8845        WHEN OTHERS THEN
8846           IF (l_debug = 1) THEN
8847              DEBUG('There is no employee id tied to the user','transfer_contents',9);
8848           END IF;
8849     fnd_message.set_name('WMS', 'WMS_NO_EMP_FOR_USR');
8850     fnd_msg_pub.ADD;
8851     RAISE fnd_api.g_exc_error;
8852    END;
8853 
8854    l_progress := '7';
8855 
8856    BEGIN
8857       SELECT
8858         lpn_context from_context
8859         ,organization_id organization_id
8860         ,subinventory_code
8861         ,locator_id
8862         INTO
8863         l_from_context
8864         ,l_org_id
8865         ,l_sub_code
8866         ,l_loc_id
8867         FROM
8868         wms_license_plate_numbers
8869         WHERE
8870         lpn_id = p_from_lpn_id;
8871    EXCEPTION
8872       WHEN OTHERS THEN
8873          IF (l_debug = 1) THEN
8874             DEBUG('from LPN not found in WMS_LICENSE_PLATE_NUMBERS',
8875                   'transfer_contents',9);
8876          END IF;
8877    fnd_message.set_name('WMS', 'WMS_CONT_INVALID_LPN');
8878    fnd_msg_pub.ADD;
8879          RAISE fnd_api.g_exc_error;
8880    END;
8881 
8882    l_progress := '9';
8883 
8884    BEGIN
8885       SELECT
8886         lpn_context
8887   ,subinventory_code
8888   ,locator_id
8889         INTO
8890         l_into_context
8891   ,l_to_sub
8892   ,l_to_loc
8893         FROM
8894         wms_license_plate_numbers
8895         WHERE
8896         lpn_id = p_into_lpn_id;
8897    EXCEPTION
8898       WHEN OTHERS THEN
8899          IF (l_debug = 1) THEN
8900             DEBUG('INTO LPN not found in WMS_LICENSE_PLATE_NUMBERS',
8901                   'transfer_contents',9);
8902          END IF;
8903    fnd_message.set_name('WMS', 'WMS_CONT_INVALID_LPN');
8904    fnd_msg_pub.ADD;
8905          RAISE fnd_api.g_exc_error;
8906    END;
8907 
8908    -- Set variable if sub/loc of toLPN is different from that of from LPN
8909    IF (Nvl(p_into_lpn_id,0) <> 0
8910        AND l_into_context IN (1,2,3)  -- No need to call TM if DFBU LPN
8911        AND (Nvl(l_to_sub,'@@@') <> Nvl(p_sub_code,'@@@')
8912       OR Nvl(l_to_loc,-1) <> Nvl(p_loc_id,-1))) THEN
8913       l_sub_loc_changed := 'Y';
8914     ELSE
8915       l_sub_loc_changed := 'N';
8916    END IF;
8917 
8918    l_progress := '10';
8919    IF (l_debug = 1) THEN
8920       DEBUG('from_content = ' || l_from_context ||
8921       ' into_context = ' || l_into_context ||
8922       ' from_sub_code = ' || l_sub_code ||
8923             ' from_loc_id = ' || l_loc_id
8924       || ' l_to_sub = '||l_to_sub ||
8925       ' l_to_loc = ' || l_to_loc ||
8926       ' l_sub_loc_changed = ' || l_sub_loc_changed
8927             ,'transfer_contents', 9);
8928    END IF;
8929 
8930    -- From LPN must be either in receiving, inventory, or WIP
8931    IF (l_from_context NOT IN (1, 2, 3)) THEN
8932       IF (l_debug = 1) THEN
8933          DEBUG('Invalid context for from_lpn_id',
8934                'transfer_contents', 9);
8935       END IF;
8936       fnd_message.set_name('WMS', 'WMS_CONT_INVALID_LPN_CONTEXT');
8937       fnd_msg_pub.ADD;
8938       RAISE fnd_api.g_exc_error;
8939    END IF;
8940 
8941    l_progress := '30';
8942 
8943    IF (l_debug = 1) THEN
8944       DEBUG('Calling suggestions_pub_wrapper','transfer_contents',9);
8945       DEBUG('  (p_lpn_id   => ' || p_from_lpn_id,'transfer_contents',9);
8946       DEBUG('   ,p_org_id  => ' || l_org_id,'transfer_contents',9);
8947       DEBUG('   ,p_user_id => ' || l_user_id,'transfer_contents',9);
8948       DEBUG('   ,p_eqp_ins => ' || p_eqp_ins || ')','transfer_contents',9);
8949    END IF;
8950 
8951    wms_putaway_utils.suggestions_pub_wrapper
8952      (p_lpn_id    =>   p_from_lpn_id
8953       ,p_org_id   =>   l_org_id
8954       ,p_user_id  =>   l_user_id
8955       ,p_eqp_ins  =>   p_eqp_ins
8956       ,x_number_of_rows => l_number_of_rows
8957       ,x_return_status =>  x_return_status
8958       ,x_msg_count     =>  x_msg_count
8959       ,x_msg_data      =>  x_msg_data
8960       ,x_crossdock     =>  l_crossdock
8961       );
8962 
8963    IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
8964       IF (l_debug = 1) THEN
8965          DEBUG('Error in suggestions_pub_wrapper','transfer_contents',9);
8966       END IF;
8967       RAISE fnd_api.g_exc_error;
8968    END IF;
8969 
8970    l_progress := '40';
8971 
8972    IF (l_debug = 1) THEN
8973       DEBUG('Suggestions_pub_wrapper returns sucessfully','transfer_contents',9);
8974    END IF;
8975 
8976 
8977    --BUG 4345714: Change the way quantities is validated from 11.5.10
8978    BEGIN
8979       SELECT /*+ ORDERED INDEX(MTRL MTL_TXN_REQUEST_LINES_N7) */ 1
8980 	INTO l_count
8981 	FROM
8982 	    (SELECT wlpn.lpn_id                /*5723418*/
8983 	      FROM wms_license_plate_numbers wlpn
8984 	      START WITH  wlpn.lpn_id = p_from_lpn_id
8985 	      CONNECT BY PRIOR wlpn.lpn_id = wlpn.parent_lpn_id ) wlpn,
8986             mtl_txn_request_lines mtrl -- Bug 14372071
8987 	WHERE mtrl.lpn_id = wlpn.lpn_id
8988 	AND mtrl.line_status = 7
8989 	AND (mtrl.quantity-Nvl(mtrl.quantity_delivered,0)) <> Nvl(mtrl.quantity_detailed,0)
8990 	AND mtrl.organization_id = l_org_id;
8991    EXCEPTION
8992       WHEN too_many_rows THEN
8993 	 l_count := 1;
8994       WHEN no_data_found THEN
8995 	 l_count := 0;
8996       WHEN OTHERS THEN
8997 	 l_count := 0;
8998    END;
8999 
9000 
9001    l_progress := '50';
9002 
9003    IF (l_count > 0) THEN
9004       fnd_msg_pub.count_and_get(p_count => l_msg_count, p_data =>
9005 				l_msg_data);
9006       IF l_debug = 1 THEN
9007 	 debug('l_msg_count: ' || l_msg_count);
9008 	 debug('l_msg_data: ' || l_msg_data);
9009       END IF;
9010 
9011       IF (l_msg_count IS NULL OR l_msg_count = 0) THEN
9012 	 -- use default message if there are no message on stack
9013 	 -- first reset message stack
9014 	 fnd_msg_pub.initialize();
9015 	 FND_MESSAGE.SET_NAME('WMS','WMS_ALLOCATE_FAIL');
9016 	 FND_MSG_PUB.ADD;
9017        ELSIF l_msg_count > 1 THEN
9018 	 FOR i IN 2 .. l_msg_count LOOP
9019 	    fnd_msg_pub.delete_msg(i);
9020 	 END LOOP;
9021       END IF;
9022       RAISE fnd_api.g_exc_error;
9023    END IF;
9024 
9025    l_progress := '60';
9026 
9027    -- Retrieve all the suggestion MMTT
9028    IF (l_from_context = 3) THEN
9029       -- Retrieve all the suggestion MMTT
9030       -- Distinguish from other dummy ones
9031       BEGIN
9032       -- Bug 5231114: Added the condition on transaction_source_type_id and
9033       -- transaction_action_id for the following combinations:13/12 and 4/27
9034          SELECT
9035            mmtt.transaction_temp_id
9036       BULK COLLECT INTO
9037            l_txn_tmp_id_tb
9038          FROM mtl_material_transactions_temp mmtt
9039          WHERE -- suggestion mmtts?
9040            ( ( mmtt.transaction_source_type_id = 1 AND
9041                mmtt.transaction_action_id = 27) OR
9042              ( mmtt.transaction_source_type_id = 7 AND
9043                mmtt.transaction_action_id = 12) OR
9044              ( mmtt.transaction_source_type_id = 12 AND
9045                mmtt.transaction_action_id = 27) OR
9046              ( mmtt.transaction_source_type_id = 13 AND
9047                mmtt.transaction_action_id = 12) OR
9048 	     ( mmtt.transaction_source_type_id = 4 AND
9049                mmtt.transaction_action_id = 27)) AND
9050 	   mmtt.organization_id = l_org_id AND
9051            mmtt.move_order_line_id --BUG 3435079: use org_id for performace reason
9052            IN ( SELECT mtrl.line_id
9053                 FROM   mtl_txn_request_lines mtrl,
9054                      ( SELECT wlpn.lpn_id     /*5723418*/
9055                         FROM wms_license_plate_numbers wlpn
9056                         START WITH wlpn.lpn_id = p_from_lpn_id
9057                         CONNECT BY PRIOR wlpn.lpn_id = wlpn.parent_lpn_id ) wlpn
9058                 WHERE  mtrl.organization_id = l_org_id
9059 		 --BUG 3435079: use org_id for performace reason
9060 		 AND mtrl.lpn_id = wlpn.lpn_id );
9061 
9062       EXCEPTION
9063          WHEN OTHERS THEN
9064             IF (l_debug = 1 ) THEN
9065                DEBUG('Unexpected Exception Raised in Bulk Select'
9066                      ,'transfer_contents', 9);
9067             END IF;
9068             RAISE fnd_api.g_exc_error;
9069       END;
9070 
9071       IF (l_debug = 1) THEN
9072 	 debug('# of MMTT returned: '||l_txn_tmp_id_tb.COUNT,'transfer_contents',9);
9073       END IF;
9074 
9075       IF (l_txn_tmp_id_tb.COUNT = 0) THEN
9076 	 IF (l_debug = 1) THEN
9077 	    debug('No MMTT returned!','transfer_contents',9);
9078 	 END IF;
9079 	 FND_MESSAGE.SET_NAME('WMS','WMS_ALLOCATE_FAIL');
9080 	 FND_MSG_PUB.ADD;
9081 	 RAISE fnd_api.g_exc_error;
9082       END IF;
9083     ELSE
9084       -- For inventory and wip, just query based on move_order_line_id
9085       BEGIN
9086          SELECT
9087            mmtt.transaction_temp_id
9088       BULK COLLECT INTO
9089            l_txn_tmp_id_tb
9090          FROM mtl_material_transactions_temp mmtt
9091          WHERE -- suggestion mmtts?
9092 	   mmtt.organization_id = l_org_id AND
9093            mmtt.move_order_line_id --BUG 3435079: use org_id for performace reason
9094            IN ( SELECT mtrl.line_id
9095                 FROM   mtl_txn_request_lines mtrl ,
9096 		(SELECT wlpn.lpn_id     /*5723418*/
9097                         FROM wms_license_plate_numbers wlpn
9098                         START WITH wlpn.lpn_id = p_from_lpn_id
9099                         CONNECT BY PRIOR wlpn.lpn_id = wlpn.parent_lpn_id ) wlpn
9100                 WHERE  mtrl.organization_id = l_org_id AND --BUG 3435079: use org_id for performace reason
9101 		mtrl.lpn_id =  wlpn.lpn_id  );
9102       EXCEPTION
9103          WHEN OTHERS THEN
9104             IF (l_debug = 1 ) THEN
9105                DEBUG('Unexpected Exception Raised in Bulk Select'
9106                      ,'transfer_contents', 9);
9107             END IF;
9108             RAISE fnd_api.g_exc_error;
9109       END;
9110    END IF; -- IF (l_from_context = 3)
9111 
9112    l_progress := '70';
9113 
9114    l_task_execute_rec.person_id := l_emp_id;
9115    l_task_execute_rec.organization_id := l_org_id;
9116    l_task_execute_rec.loaded_time := Sysdate;
9117    l_task_execute_rec.user_task_type := -1;
9118 
9119    -- Activate each MMTT's
9120    FOR i IN 1 .. l_txn_tmp_id_tb.COUNT LOOP
9121       l_progress := '73';
9122 
9123       wms_atf_runtime_pub_apis.validate_operation
9124   (x_return_status    =>   x_return_status
9125    ,x_msg_data         =>   x_msg_data
9126    ,x_msg_count        =>   x_msg_count
9127    ,x_error_code       =>   l_error_code
9128    ,x_inspection_flag  =>   l_inspection_flag
9129    ,x_load_flag        =>   l_load_flag
9130    ,x_drop_flag        =>   l_drop_flag
9131    ,x_load_prim_quantity => l_load_prim_quantity
9132    ,x_drop_prim_quantity => l_drop_prim_quantity
9133    ,x_inspect_prim_quantity => l_inspect_prim_quantity
9134    ,p_source_task_id   =>   l_txn_tmp_id_tb(i)
9135    ,p_move_order_line_id => NULL
9136    ,p_inventory_item_id =>  NULL
9137    ,p_lpn_id           =>   NULL
9138    ,p_activity_type_id =>   1 -- INBOUND
9139    ,p_organization_id  =>   l_org_id);
9140       IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
9141    IF (l_debug = 1) THEN
9142       debug('Validate_operation failed','transfer_contents',9);
9143    END IF;
9144    RAISE fnd_api.g_exc_error;
9145       END IF;
9146 
9147       l_progress := '75';
9148 
9149       IF (l_load_flag <> 3) THEN
9150    debug('MMTT:'||l_txn_tmp_id_tb(i)||' not full loaded','transfer_contents',9);
9151    RAISE fnd_api.g_exc_error;
9152       END IF;
9153 
9154       l_progress := '77';
9155 
9156       IF (l_debug = 1) THEN
9157          DEBUG('Calling activate_operation_instance','transfer_contents',9);
9158          DEBUG(' ( p_source_task_id     => ' || l_txn_tmp_id_tb(i),'transfer_contents',9);
9159          DEBUG('   ,p_activity_type_id  => 1','transfer_contents',9);
9160          DEBUG('   ,p_task_execute_rec  => l_task_execute_rec','transfer_contents',9);
9161          DEBUG('   ,p_operation_type_id => 1 )','transfer_contents',9);
9162       END IF;
9163 
9164       wms_atf_runtime_pub_apis.activate_operation_instance
9165   (  p_source_task_id  => l_txn_tmp_id_tb(i)
9166      ,p_activity_id => 1 -- Inbound
9167            ,p_task_execute_rec => l_task_execute_rec
9168            ,p_operation_type_id => 1 -- load
9169            ,x_return_status     => x_return_status
9170            ,x_msg_data          => x_msg_data
9171            ,x_msg_count         => x_msg_count
9172            ,x_error_code        => l_error_code
9173            ,x_consolidation_method_id => l_consolidation_method_id
9174            ,x_drop_lpn_option   => l_drop_lpn_option
9175            );
9176       IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
9177          IF (l_debug = 1) THEN
9178             DEBUG('xxx: Error in activate_operation_instance','transfer_contents',9);
9179          END IF;
9180    RAISE fnd_api.g_exc_error;
9181       END IF;
9182    END LOOP;
9183 
9184    l_progress := '80';
9185 
9186    IF (l_from_context = 3) THEN -- resides in receiving
9187       IF (l_debug = 1) THEN
9188          DEBUG('xxx: l_from_context = ' || l_from_context
9189                      || '. Resides in receiving','transfer_contents',9);
9190       END IF;
9191 
9192       l_progress := '90';
9193 
9194       BEGIN
9195          SELECT COUNT(lpn_content_id)
9196            INTO l_count
9197            FROM wms_lpn_contents
9198           WHERE parent_lpn_id = p_from_lpn_id;
9199       EXCEPTION
9200          WHEN no_data_found THEN
9201             l_count := 0;
9202       END;
9203 
9204       l_progress := '100';
9205 
9206       --loose item exists or the into lpn sub/loc is different than
9207       -- that of the from lpn
9208       IF (l_count > 0 OR l_sub_loc_changed = 'Y') THEN
9209          IF (l_debug = 1) THEN
9210             DEBUG('xxx: There are ' || l_count ||
9211                         ' entries of wlpnc.  Inserting rti...','transfer_contents',9);
9212             DEBUG('      (p_from_org => ' || l_org_id,'transfer_contents',9);
9213             DEBUG('       p_to_org   => ' || l_org_id,'transfer_contents',9);
9214             DEBUG('       p_to_sub   => ' || l_sub_code,'transfer_contents',9);
9215             DEBUG('       p_to_loc   => ' || l_loc_id,'transfer_contents',9);
9216             DEBUG('       p_lpn_id   => ' || p_from_lpn_id,'transfer_contents',9);
9217             DEBUG('       p_xfer_lpn_id => ' || p_into_lpn_id,'transfer_contents',9);
9218          END IF;
9219 
9220          l_progress := '110';
9221 
9222    IF (l_sub_loc_changed = 'Y') THEN
9223       l_return_status := insert_rti
9224         (p_from_org => l_org_id
9225          ,p_lpn_id => p_from_lpn_id
9226          ,p_to_org   => l_org_id
9227                ,p_to_sub => l_to_sub
9228                ,p_to_loc => l_to_loc
9229                ,p_xfer_lpn_id  => p_into_lpn_id
9230                ,p_first_time  => 1
9231                ,p_mobile_txn => 'Y'
9232                ,p_txn_mode_code => 'ONLINE'
9233                ,x_return_status =>  x_return_status
9234                ,x_msg_count     =>  x_msg_count
9235                ,x_msg_data      =>  x_msg_data
9236                );
9237     ELSE -- If no sub/loc changed, then into LPN must be DFBU
9238       -- So pass in sub/loc of the from LPN
9239       l_return_status := insert_rti
9240         (p_from_org => l_org_id
9241          ,p_lpn_id => p_from_lpn_id
9242          ,p_to_org   => l_org_id
9243                ,p_to_sub => l_sub_code
9244                ,p_to_loc => l_loc_id
9245                ,p_xfer_lpn_id  => p_into_lpn_id
9246                ,p_first_time  => 1
9247                ,p_mobile_txn => 'Y'
9248                ,p_txn_mode_code => 'ONLINE'
9249                ,x_return_status =>  x_return_status
9250                ,x_msg_count     =>  x_msg_count
9251                ,x_msg_data      =>  x_msg_data
9252                );
9253    END IF; -- IF (l_sub_loc_changed = 'Y') THEN
9254 
9255          IF (x_return_status <> fnd_api.g_ret_sts_success OR l_return_status = -1) THEN
9256             IF (l_debug = 1) THEN
9257                DEBUG('xxx: Error inserting_rti','transfer_contents',9);
9258             END IF;
9259             RAISE fnd_api.g_exc_error;
9260          END IF;
9261 
9262          l_progress := '120';
9263 
9264    -- Update WMS_PROCESS_FLAG of MOL to 2
9265    BEGIN
9266       UPDATE
9267         mtl_txn_request_lines
9268         SET
9269         wms_process_flag = 2
9270         WHERE
9271         lpn_id IN (SELECT wlpn.lpn_id
9272        FROM   wms_license_plate_numbers wlpn
9273        START WITH wlpn.lpn_id = p_from_lpn_id
9274        CONNECT BY PRIOR wlpn.lpn_id =
9275        wlpn.parent_lpn_id);
9276    EXCEPTION
9277       WHEN OTHERS THEN
9278          IF (l_debug = 1 ) THEN
9279       debug('Error updating MOL statuses','transfer_contents',9);
9280          END IF;
9281    END;
9282 
9283    --call rcv tm
9284          l_progress := '130';
9285          IF (l_debug = 1) THEN
9286             DEBUG('xxx: Calling rcv_process_receive_txn','transfer_contents',9);
9287          END IF;
9288 
9289    l_old_txn_mode_code := inv_rcv_common_apis.g_po_startup_value.transaction_mode;
9290    inv_rcv_common_apis.g_po_startup_value.transaction_mode := 'ONLINE';
9291 
9292    l_progress := '135';
9293 
9294    BEGIN
9295       inv_rcv_mobile_process_txn.rcv_process_receive_txn
9296         (x_return_status    =>  x_return_status
9297          ,x_msg_data        =>  x_msg_data
9298          );
9299    EXCEPTION
9300       WHEN OTHERS THEN
9301          inv_rcv_common_apis.g_po_startup_value.transaction_mode := l_old_txn_mode_code;
9302          IF (l_debug = 1) THEN
9303       DEBUG('xxx: Error - Rcv TM Failed','transfer_contents',9);
9304          END IF;
9305          RAISE fnd_api.g_exc_error;
9306    END;
9307 
9308          IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
9309             IF (l_debug = 1) THEN
9310                DEBUG('xxx: Error - Rcv TM Failed','transfer_contents',9);
9311             END IF;
9312       inv_rcv_common_apis.g_po_startup_value.transaction_mode := l_old_txn_mode_code;
9313             RAISE fnd_api.g_exc_error;
9314          END IF;
9315 
9316          l_progress := '140';
9317 
9318          inv_rcv_common_apis.g_po_startup_value.transaction_mode := l_old_txn_mode_code;
9319          inv_rcv_common_apis.rcv_clear_global;
9320 
9321        ELSE -- No loose items and no sub/loc change
9322          IF (l_debug = 1) THEN
9323             DEBUG('xxx: No loose items exist','transfer_contents',9);
9324          END IF;
9325 
9326          l_progress := '150';
9327 
9328          BEGIN
9329             SELECT lpn_id
9330               bulk collect INTO l_lpn_ids
9331               FROM wms_license_plate_numbers
9332               WHERE parent_lpn_id = p_from_lpn_id;
9333          EXCEPTION
9334             WHEN OTHERS THEN
9335                IF (l_debug = 1) THEN
9336                   DEBUG('xxx: Error getting nested lpns','transfer_contents',9);
9337                END IF;
9338          END;
9339 
9340          l_progress := '160';
9341 
9342          -- Call packunpack on each inner LPN
9343          FOR i IN 1 .. l_lpn_ids.COUNT LOOP
9344             wms_container_pvt.packunpack_container
9345               (p_api_version       =>   1.0
9346                ,p_content_lpn_id    =>  l_lpn_ids(i)
9347                ,p_lpn_id           =>   p_from_lpn_id
9348                ,p_operation        =>   2 /* Unpack */
9349                ,p_organization_id  =>   l_org_id
9350                ,x_return_status    =>   x_return_status
9351                ,x_msg_count        =>   x_msg_count
9352                ,x_msg_data         =>   x_msg_data
9353                );
9354             IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
9355                IF (l_debug = 1) THEN
9356                   DEBUG('xxx: Error unpacking content_lpn = ' ||
9357                               l_lpn_ids(i) || ' from lpn = ' ||
9358                               p_from_lpn_id,'transfer_contents',9);
9359                END IF;
9360                RAISE fnd_api.g_exc_error;
9361             END IF;
9362 
9363             wms_container_pvt.packunpack_container
9364               (p_api_version       =>   1.0
9365                ,p_content_lpn_id   =>   l_lpn_ids(i)
9366                ,p_lpn_id           =>   p_into_lpn_Id
9367                ,p_operation        =>   1 /* Pack */
9368                ,p_organization_id  =>   l_org_id
9369                ,x_return_status    =>   x_return_status
9370                ,x_msg_count        =>   x_msg_count
9371                ,x_msg_data         =>   x_msg_data
9372                );
9373             IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
9374                IF (l_debug = 1) THEN
9375                   DEBUG('xxx: Error packing content_lpn = ' ||
9376                               l_lpn_ids(i) || ' into lpn = ' ||
9377                               p_into_lpn_id,'transfer_contents',9);
9378                END IF;
9379                RAISE fnd_api.g_exc_error;
9380             END IF;
9381          END LOOP;  -- end nested lpn loop
9382 
9383          l_progress := '170';
9384 
9385          -- At the end, update the intoLPN context into 3 (RCV)
9386          wms_container_pub.modify_lpn_wrapper
9387            ( p_api_version    =>  1.0
9388              ,x_return_status =>  x_return_status
9389              ,x_msg_count     =>  x_msg_count
9390              ,x_msg_data      =>  x_msg_data
9391              ,p_lpn_id        =>  p_into_lpn_id
9392              ,p_lpn_context   =>  3 --RCV
9393        ,p_subinventory  =>  l_sub_code -- same as from LPN
9394        ,p_locator_id    =>  l_loc_id
9395              );
9396          IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
9397             IF (l_debug = 1) THEN
9398                debug('Error modifying lpn',
9399                      'transfer_contents', 9);
9400             END IF;
9401          END IF;
9402 
9403          -- Call complete operation instance
9404          FOR i IN 1 .. l_txn_tmp_id_tb.COUNT LOOP
9405             wms_atf_runtime_pub_apis.complete_operation_instance
9406               ( p_source_task_id  =>  l_txn_tmp_id_tb(i)
9407                 ,p_activity_id    =>  1 -- inbound
9408                 ,p_operation_type_id => 1 -- load
9409                 ,x_return_status  =>  x_return_status
9410                 ,x_msg_data       =>  x_msg_data
9411                 ,x_msg_count      =>  x_msg_count
9412                 ,x_error_code     =>  l_error_code
9413                 );
9414             IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
9415                IF (l_debug = 1) THEN
9416                   DEBUG('xxx: Error in complete_operation_instance on '
9417                         || ' MMTT = ' || l_txn_tmp_id_tb(i),'transfer_contents',9);
9418                END IF;
9419                RAISE fnd_api.g_exc_error;
9420             END IF;
9421 
9422       END LOOP;
9423 
9424       -- No need to update any MMTT/MOL, since there will none
9425       -- assoc with from_lpn
9426       END IF; -- end count > 0
9427     ELSIF (l_from_context = 2) THEN -- WIP LPN
9428 
9429       l_progress := '180';
9430 
9431       IF (l_debug = 1) THEN
9432          DEBUG('xxx: l_from_context = ' || l_from_context
9433                      || '. Resides in WIP','transfer_contents',9);
9434       END IF;
9435 
9436       -- Deal with loose item
9437       -- First deal with non-serial controlled items
9438       l_loose_contents := 0;
9439       FOR l_wlpnc_rec IN wlpnc_cur LOOP
9440          IF (l_loose_contents = 0) THEN
9441             l_loose_contents := 1;
9442          END IF;
9443          IF Nvl(l_wlpnc_rec.serial_number_control_code,1) IN (1, 6) THEN
9444             -- ??
9445             IF (l_debug = 1) THEN
9446                debug('calling pup on item:
9447                      '||l_wlpnc_rec.inventory_item_id,
9448                      'transfer_contents',9);
9449             END IF;
9450 
9451             wms_container_pvt.packunpack_container
9452               (p_api_version       =>   1.0
9453                ,p_lpn_id           =>   p_from_lpn_id
9454                ,p_operation        =>   2 -- UNPACK
9455                ,p_organization_id  =>   l_org_id
9456                ,p_content_item_id  =>   l_wlpnc_rec.inventory_item_id
9457                ,p_revision         =>   l_wlpnc_rec.revision
9458                ,p_lot_number       =>   l_wlpnc_rec.lot_number
9459                ,p_quantity         =>   l_wlpnc_rec.quantity
9460                ,p_uom              =>   l_wlpnc_rec.uom_code
9461                ,x_return_status    =>   x_return_status
9462                ,x_msg_count        =>   x_msg_count
9463                ,x_msg_data         =>   x_msg_data
9464                );
9465             IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
9466                IF (l_debug = 1) THEN
9467                   DEBUG('xxx: Error unpacking item_id = ' ||
9468                               l_wlpnc_rec.inventory_item_id || ' from lpn = ' ||
9469                               p_from_lpn_id,'transfer_contents',9);
9470                END IF;
9471                RAISE fnd_api.g_exc_error;
9472             END IF;
9473 
9474             wms_container_pvt.packunpack_container
9475               (p_api_version       =>   1.0
9476                ,p_lpn_id           =>   p_into_lpn_id
9477                ,p_operation        =>   1 -- PACK
9478                ,p_organization_id  =>   l_org_id
9479                ,p_content_item_id  =>   l_wlpnc_rec.inventory_item_id
9480                ,p_revision         =>   l_wlpnc_rec.revision
9481                ,p_lot_number       =>   l_wlpnc_rec.lot_number
9482                ,p_quantity         =>   l_wlpnc_rec.quantity
9483                ,p_uom              =>   l_wlpnc_rec.uom_code
9484                ,x_return_status    =>   x_return_status
9485                ,x_msg_count        =>   x_msg_count
9486                ,x_msg_data         =>   x_msg_data
9487                );
9488             IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
9489                IF (l_debug = 1) THEN
9490                   DEBUG('xxx: Error packing item_id = ' ||
9491                               l_wlpnc_rec.inventory_item_id || ' from lpn = ' ||
9492                               p_from_lpn_id,'transfer_contents',9);
9493                END IF;
9494                RAISE fnd_api.g_exc_error;
9495             END IF;
9496     ELSE -- If item is serial controlled
9497       -- Call packunpack on each serial number
9498       IF (l_serial_numbers.COUNT > 0) THEN
9499          l_serial_numbers.DELETE;
9500       END IF;
9501 
9502       BEGIN
9503                SELECT serial_number
9504                  bulk collect INTO l_serial_numbers
9505                  FROM mtl_serial_numbers
9506                  WHERE lpn_id = p_from_lpn_id
9507      AND inventory_item_id = l_wlpnc_rec.inventory_item_id
9508      AND Nvl(lot_number, '@@@') = Nvl(l_wlpnc_rec.lot_number, '@@@')
9509                  AND Nvl(revision, '@@@') = Nvl(l_wlpnc_rec.revision,'@@@');
9510             EXCEPTION
9511                WHEN OTHERS THEN
9512                   IF (l_debug = 1) THEN
9513                      debug('Error retrieving entries from MTL_SERIAL_NUMBERS',
9514                            'transfer_contents', 9);
9515                   END IF;
9516       RAISE fnd_api.g_exc_error;
9517             END;
9518 
9519       FOR i IN 1 .. l_serial_numbers.COUNT LOOP
9520          IF (l_debug = 1) THEN
9521       DEBUG('transfer_contents: unpacking serial number:'
9522       || l_serial_numbers(i),9);
9523          END IF;
9524          wms_container_pvt.packunpack_container
9525      (p_api_version       =>   1.0
9526       ,p_lpn_id           =>   p_from_lpn_id
9527       ,p_operation        =>   2 -- UNPACK
9528       ,p_organization_id  =>   l_org_id
9529       ,p_content_item_id  =>   l_wlpnc_rec.inventory_item_id
9530       ,p_revision         =>   l_wlpnc_rec.revision
9531       ,p_lot_number       =>   l_wlpnc_rec.lot_number
9532       ,p_from_serial_number => l_serial_numbers(i)
9533       ,p_to_serial_number =>   l_serial_numbers(i)
9534       ,p_uom              =>   l_wlpnc_rec.uom_code
9535       ,x_return_status    =>   x_return_status
9536       ,x_msg_count        =>   x_msg_count
9537       ,x_msg_data         =>   x_msg_data
9538       );
9539          IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
9540       IF (l_debug = 1) THEN
9541          DEBUG('xxx: Error unpacking serial_number = ' ||
9542          l_serial_numbers(i) || ' from lpn = ' ||
9543          p_from_lpn_id,'transfer_contents',9);
9544       END IF;
9545       RAISE fnd_api.g_exc_error;
9546          END IF;
9547          wms_container_pvt.packunpack_container
9548      (p_api_version       =>   1.0
9549       ,p_lpn_id           =>   p_into_lpn_id
9550       ,p_operation        =>   1 -- PACK
9551       ,p_organization_id  =>   l_org_id
9552       ,p_content_item_id  =>   l_wlpnc_rec.inventory_item_id
9553       ,p_revision         =>   l_wlpnc_rec.revision
9554       ,p_lot_number       =>   l_wlpnc_rec.lot_number
9555       ,p_from_serial_number => l_serial_numbers(i)
9556       ,p_to_serial_number =>   l_serial_numbers(i)
9557       ,p_uom              =>   l_wlpnc_rec.uom_code
9558       ,x_return_status    =>   x_return_status
9559       ,x_msg_count        =>   x_msg_count
9560       ,x_msg_data         =>   x_msg_data
9561       );
9562          IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
9563       IF (l_debug = 1) THEN
9564          DEBUG('xxx: Error packing serial_number = ' ||
9565          l_serial_numbers(i) || ' from lpn = ' ||
9566          p_from_lpn_id,'transfer_contents',9);
9567       END IF;
9568       RAISE fnd_api.g_exc_error;
9569          END IF;
9570       END LOOP; --  FOR i in 1 .. l_serial_numbers.COUNT LOOP
9571    END IF; -- IF Nvl(l_wlpnc_rec.serial_number_control_code,1) IN (1, 6) THEN
9572       END LOOP; --FOR l_wlpnc_rec IN wlpnc_cur LOOP
9573 
9574       l_progress := '200';
9575 
9576       -- Now deal with Nested LPN.  For now, there should be none
9577       BEGIN
9578          SELECT lpn_id
9579            bulk collect INTO l_lpn_ids
9580            FROM wms_license_plate_numbers
9581            WHERE parent_lpn_id = p_from_lpn_id;
9582       EXCEPTION
9583          WHEN OTHERS THEN
9584             IF (l_debug = 1) THEN
9585                DEBUG('xxx: Error getting nested lpns','transfer_contents',9);
9586             END IF;
9587       END;
9588 
9589       l_progress := '210';
9590 
9591       FOR i IN 1 .. l_lpn_ids.COUNT LOOP
9592          wms_container_pvt.packunpack_container
9593            (p_api_version       =>   1.0
9594             ,p_content_lpn_id    =>  l_lpn_ids(i)
9595             ,p_lpn_id           =>   p_from_lpn_id
9596             ,p_operation        =>   2 /* Unpack */
9597             ,p_organization_id  =>   l_org_id
9598             ,x_return_status    =>   x_return_status
9599             ,x_msg_count        =>   x_msg_count
9600             ,x_msg_data         =>   x_msg_data
9601             );
9602          IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
9603             IF (l_debug = 1) THEN
9604                DEBUG('xxx: Error unpacking lpn = ' ||
9605                            l_lpn_ids(i) || ' from lpn = ' ||
9606                            p_from_lpn_id,'transfer_contents',9);
9607             END IF;
9608             RAISE fnd_api.g_exc_error;
9609          END IF;
9610          wms_container_pvt.packunpack_container
9611            (p_api_version       =>   1.0
9612             ,p_content_lpn_id   =>   l_lpn_ids(i)
9613             ,p_lpn_id           =>   p_into_lpn_Id
9614             ,p_operation        =>   1 /* Pack */
9615             ,p_organization_id  =>   l_org_id
9616             ,x_return_status    =>   x_return_status
9617             ,x_msg_count        =>   x_msg_count
9618             ,x_msg_data         =>   x_msg_data
9619             );
9620          IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
9621             IF (l_debug = 1) THEN
9622                DEBUG('xxx: Error packing lpn = ' ||
9623                            l_lpn_ids(i) || ' into lpn = ' ||
9624                            p_into_lpn_id,'transfer_contents',9);
9625             END IF;
9626             RAISE fnd_api.g_exc_error;
9627          END IF;
9628       END LOOP;
9629 
9630       l_progress := '220';
9631 
9632       -- At the end, update the intoLPN context into 2 (WIP
9633       wms_container_pub.modify_lpn_wrapper
9634         ( p_api_version    =>  1.0
9635           ,x_return_status =>  x_return_status
9636           ,x_msg_count     =>  x_msg_count
9637           ,x_msg_data      =>  x_msg_data
9638           ,p_lpn_id        =>  p_into_lpn_id
9639           ,p_lpn_context   =>  2 --WIP
9640           );
9641       IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
9642          IF (l_debug = 1) THEN
9643             debug('Error modifying lpn context to wip',
9644                   'transfer_contents', 9);
9645          END IF;
9646       END IF;
9647 
9648       -- Call complete operation instance
9649       FOR i IN 1 .. l_txn_tmp_id_tb.COUNT LOOP
9650          wms_atf_runtime_pub_apis.complete_operation_instance
9651            ( p_source_task_id  =>  l_txn_tmp_id_tb(i)
9652              ,p_activity_id    =>  1 -- inbound
9653              ,p_operation_type_id => 1 -- load
9654              ,x_return_status  =>  x_return_status
9655              ,x_msg_data       =>  x_msg_data
9656              ,x_msg_count      =>  x_msg_count
9657              ,x_error_code     =>  l_error_code
9658              );
9659          IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
9660             IF (l_debug = 1) THEN
9661                DEBUG('xxx: Error in complete_operation_instance on '
9662                            || ' MMTT = ' || l_txn_tmp_id_tb(i),'transfer_contents',9);
9663             END IF;
9664             RAISE fnd_api.g_exc_error;
9665          END IF;
9666 
9667       END LOOP;
9668 
9669       -- Update MOL to new LPN id, only if there are loose contents
9670       -- associated with fromLPN
9671       IF (l_loose_contents = 1) THEN
9672          -- Update any MMTT assoc with the old LPN to the new LPN id
9673          IF (l_debug = 1) THEN
9674             -- ER 7307189 changes start
9675             debug('Updating MMTT...','transfer_contents',9);
9676             debug('Updating MMTT...','transfer_contents p_into_lpn_id'||p_into_lpn_id,9);
9677             debug('Updating MMTT...','transfer_contents p_from_lpn_id'||p_from_lpn_id,9);
9678             -- ER 7307189 changes end
9679          END IF;
9680 
9681 	 --BUG 3435079: The query doesn't seem to be picking up the lpn_id
9682 	 --index on MMTT.  So replace it with the following, and also
9683 	 --Move it BEFORE updating MOL
9684          UPDATE
9685            mtl_material_transactions_temp
9686            SET
9687            lpn_id = p_into_lpn_id
9688            WHERE
9689 	   lpn_id = p_from_lpn_id;
9690 
9691 
9692          BEGIN
9693             UPDATE
9694               mtl_txn_request_lines
9695               SET
9696               lpn_id = p_into_lpn_id
9697               WHERE
9698               lpn_id = p_from_lpn_id;
9699          EXCEPTION
9700             WHEN OTHERS THEN
9701                IF (l_debug =1 ) THEN
9702                   DEBUG('xxx: Error updating MOL','transfer_contents',9);
9703                END IF;
9704                RAISE fnd_api.g_exc_error;
9705          END;
9706 
9707          l_progress := '230';
9708 
9709       END IF;
9710 
9711     ELSE -- inventory LPN l_from_context = 1
9712 
9713       IF (l_debug = 1) THEN
9714          DEBUG('xxx: l_from_context = ' || l_from_context
9715                      || '. Resides in Inventory','transfer_contents',9);
9716       END IF;
9717 
9718       l_progress := '240';
9719 
9720       -- create MMTT records for each loose item, each inner LPN
9721       -- Group them into same header, and then call inventory TM
9722       SELECT mtl_material_transactions_s.NEXTVAL
9723         INTO   l_hdr_id
9724         FROM   dual;
9725 
9726       l_progress := '250';
9727 
9728       IF (l_sub_loc_changed = 'Y') THEN
9729    l_return_status := inv_trx_util_pub.insert_line_trx
9730      (
9731       p_trx_hdr_id           => l_hdr_id
9732       , p_item_id              => '0'
9733       , p_org_id               => l_org_id
9734       , p_trx_action_id        => 2
9735       , p_subinv_code          => l_sub_code
9736       , p_tosubinv_code        => l_to_sub
9737       , p_locator_id           => l_loc_id
9738       , p_tolocator_id         => l_to_loc
9739       , p_trx_type_id          => 2
9740       , p_trx_src_type_id      => 13
9741       , p_trx_qty              => 1
9742       , p_pri_qty              => 1
9743       , p_uom                  => 'Ea'
9744       , p_user_id              => l_user_id
9745       , p_from_lpn_id          => NULL
9746       , p_cnt_lpn_id           => p_from_lpn_id
9747       , p_xfr_lpn_id           => NULL
9748       , x_trx_tmp_id           => l_txn_tmp_id
9749       , x_proc_msg             => x_msg_data
9750       , p_project_id           => p_project_id
9751       , p_task_id              => p_task_id);
9752    IF (l_txn_tmp_id <= 0 ) OR (l_return_status<>0) THEN
9753       IF (l_debug = 1) THEN
9754          debug('Error inserting MMTT ID: ' ||
9755          l_txn_tmp_id,'transfer_contents',9);
9756       END IF;
9757       RAISE fnd_api.g_exc_error;
9758    END IF;
9759 
9760    BEGIN
9761       UPDATE  mtl_material_transactions_temp
9762         SET   transaction_header_id = l_hdr_id
9763         , transaction_batch_id  = l_hdr_id
9764         , transaction_batch_seq = l_batch_seq
9765         WHERE   transaction_temp_id   = l_txn_tmp_id;
9766    EXCEPTION
9767       WHEN OTHERS THEN
9768          IF (l_debug = 1) THEN
9769       debug('Error updating MMTT with ID: ' ||
9770       l_txn_tmp_id,9);
9771          END IF;
9772          RAISE fnd_api.g_exc_error;
9773    END;
9774 
9775    l_batch_seq := l_batch_seq + 1;
9776    -- update local variables for sub and loc
9777    l_sub_code := l_to_sub;
9778    l_loc_id := l_to_loc;
9779 
9780       END IF; --IF (l_sub_loc_changed = 'Y') THEN
9781 
9782       -- Deal with Nested LPN first
9783       BEGIN
9784          SELECT lpn_id
9785            bulk collect INTO l_lpn_ids
9786          FROM wms_license_plate_numbers
9787          WHERE parent_lpn_id = p_from_lpn_id;
9788       EXCEPTION
9789          WHEN OTHERS THEN
9790             IF (l_debug = 1) THEN
9791                DEBUG('xxx: Error getting nested lpns','transfer_contents',9);
9792             END IF;
9793       END;
9794 
9795       l_progress := '260';
9796 
9797       FOR i IN 1 .. l_lpn_ids.COUNT LOOP
9798          IF (l_debug = 1) THEN
9799             DEBUG('Inserting MMTT for lpn: ' || l_lpn_ids(i),
9800                   'transfer_contents',
9801                   9);
9802          END IF;
9803 
9804    l_return_Status := inv_trx_util_pub.insert_line_trx
9805      (
9806       p_trx_hdr_id             => l_hdr_id
9807       , p_item_id              => -1
9808       , p_org_id               => l_org_id
9809       , p_trx_action_id        => inv_globals.g_action_containersplit
9810       , p_subinv_code          => l_sub_code
9811       , p_locator_id           => l_loc_id
9812       , p_trx_type_id          => inv_globals.g_type_container_split
9813       , p_trx_src_type_id      => inv_globals.g_sourcetype_inventory
9814       , p_trx_qty              => 1
9815       , p_pri_qty              => 1
9816       , p_uom                  => 'X'
9817       , p_user_id              => l_user_id  --??
9818       , p_from_lpn_id          => p_from_lpn_id
9819       , p_cnt_lpn_id           => l_lpn_ids(i)
9820       , p_xfr_lpn_id           => p_into_lpn_id
9821       , x_trx_tmp_id           => l_txn_tmp_id
9822       , x_proc_msg             => x_msg_data );
9823 
9824    IF (l_return_status = -1) THEN
9825       IF (l_debug = 1) THEN
9826          debug('Error inserting MMTT', 'transfer_contents', 9);
9827       END IF;
9828       RAISE fnd_api.g_exc_error;
9829    END IF;
9830 
9831    BEGIN
9832       UPDATE  mtl_material_transactions_temp
9833         SET   transaction_header_id = l_hdr_id
9834         , transaction_batch_id  = l_hdr_id
9835         , transaction_batch_seq = l_batch_seq
9836         WHERE   transaction_temp_id   = l_txn_tmp_id;
9837    EXCEPTION
9838       WHEN OTHERS THEN
9839          IF (l_debug = 1) THEN
9840       debug('Error updating MMTT with ID: ' ||
9841       l_txn_tmp_id,9);
9842          END IF;
9843          RAISE fnd_api.g_exc_error;
9844    END;
9845 
9846    l_batch_seq := l_batch_seq + 1;
9847       END LOOP;
9848 
9849       l_progress := '270';
9850 
9851       -- Now deal with loose items
9852       l_loose_contents := 0;
9853       FOR l_wlpnc_rec IN wlpnc_cur LOOP
9854          IF (l_loose_contents = 0) THEN
9855             l_loose_contents := 1;
9856          END IF;
9857 
9858          IF (l_wlpnc_rec.uom_code <> l_wlpnc_rec.primary_uom_code) THEN
9859             l_prim_qty := inv_rcv_cache.convert_qty
9860 	                     (p_inventory_item_id => l_wlpnc_rec.inventory_item_id
9861 			      ,p_from_qty         => l_wlpnc_rec.quantity
9862 			      ,p_from_uom_code    => l_wlpnc_rec.uom_code
9863 			      ,p_to_uom_code      => l_wlpnc_rec.primary_uom_code);
9864           ELSE
9865             l_prim_qty := l_wlpnc_rec.quantity;
9866          END IF;
9867 
9868    IF (l_debug = 1) THEN
9869             debug('Inserting MMTT...', 'transfer_contents', 9);
9870          END IF;
9871          l_return_status := inv_trx_util_pub.insert_line_trx
9872            (
9873             p_trx_hdr_id             => l_hdr_id
9874             , p_item_id              => l_wlpnc_rec.inventory_item_id
9875             , p_revision             => l_wlpnc_rec.revision
9876             , p_org_id               => l_org_id
9877             , p_trx_action_id        => inv_globals.g_action_containersplit
9878             , p_subinv_code          => l_sub_code
9879             , p_locator_id           => l_loc_id
9880             , p_trx_type_id          => inv_globals.g_type_container_split
9881             , p_trx_src_type_id      => inv_globals.g_sourcetype_inventory
9882             , p_trx_qty              => l_wlpnc_rec.quantity
9883             , p_pri_qty              => l_prim_qty
9884             , p_uom                  => l_wlpnc_rec.uom_code
9885             , p_user_id              => l_user_id --??
9886             , p_from_lpn_id          => p_from_lpn_id
9887             , p_xfr_lpn_id           => p_into_lpn_id
9888             , x_trx_tmp_id           => l_txn_tmp_id
9889 	    , p_secondary_trx_qty    => l_wlpnc_rec.secondary_quantity -- Added for Bug 10301152
9890             , p_secondary_uom        => l_wlpnc_rec.secondary_uom_code -- Added for Bug 10301152
9891             , x_proc_msg             => x_msg_data);
9892    IF (l_return_status = -1) THEN
9893       IF (l_debug = 1) THEN
9894          debug('Error inserting MMTT', 'transfer_contents', 9);
9895       END IF;
9896       RAISE fnd_api.g_exc_error;
9897    END IF;
9898 
9899    BEGIN
9900       UPDATE  mtl_material_transactions_temp
9901         SET   transaction_header_id = l_hdr_id
9902         , transaction_batch_id  = l_hdr_id
9903         , transaction_batch_seq = l_batch_seq
9904         WHERE   transaction_temp_id   = l_txn_tmp_id;
9905    EXCEPTION
9906       WHEN OTHERS THEN
9907          IF (l_debug = 1) THEN
9908       debug('Error updating MMTT with ID: ' ||
9909       l_txn_tmp_id,9);
9910          END IF;
9911          RAISE fnd_api.g_exc_error;
9912    END;
9913 
9914    l_batch_seq := l_batch_seq + 1;
9915 
9916          IF (l_debug = 1) THEN
9917             debug('MMTT:' || l_txn_tmp_id || 'Inserted', 'transfer_contents', 9);
9918          END IF;
9919 
9920          IF (l_wlpnc_rec.lot_number IS NOT NULL) THEN
9921             l_return_status := inv_trx_util_pub.insert_lot_trx
9922               ( p_trx_tmp_id   => l_txn_tmp_id
9923                 ,p_user_id     => l_user_id
9924                 ,p_lot_number  => l_wlpnc_rec.lot_number
9925                 ,p_trx_qty     => l_wlpnc_rec.quantity
9926                 ,p_pri_qty     => l_prim_qty
9927                 ,x_ser_trx_id  => l_ser_trx_id
9928                 ,x_proc_msg    => x_msg_data
9929 		,p_secondary_qty => l_wlpnc_rec.secondary_quantity   -- Added for Bug 14671760
9930                 ,p_secondary_uom => l_wlpnc_rec.secondary_uom_code); -- Added for Bug 14671760
9931       IF (l_return_status = -1) THEN
9932          IF (l_debug = 1) THEN
9933       debug('Error inserting MTLT', 'transfer_contents', 9);
9934          END IF;
9935          RAISE fnd_api.g_exc_error;
9936       END IF;
9937          END IF;
9938 
9939          IF (Nvl(l_wlpnc_rec.serial_number_control_code,1)
9940              NOT IN (1, 6)) THEN -- Item is serial control
9941       IF (l_serial_numbers.COUNT > 0) THEN
9942          l_serial_numbers.DELETE;
9943       END IF;
9944 
9945             BEGIN
9946                SELECT serial_number
9947                  bulk collect INTO l_serial_numbers
9948                  FROM mtl_serial_numbers
9949                  WHERE lpn_id = p_from_lpn_id
9950      AND inventory_item_id = l_wlpnc_rec.inventory_item_id
9951      AND Nvl(lot_number, '@@@') = Nvl(l_wlpnc_rec.lot_number, '@@@')
9952                  AND Nvl(revision, '@@@') = Nvl(l_wlpnc_rec.revision,'@@@');
9953             EXCEPTION
9954                WHEN OTHERS THEN
9955                   IF (l_debug = 1) THEN
9956                      debug('Error retrieving entries from MTL_SERIAL_NUMBERS',
9957                            'transfer_contents', 9);
9958                   END IF;
9959       RAISE fnd_api.g_exc_error;
9960             END;
9961 
9962       IF (l_wlpnc_rec.lot_number IS NOT NULL) THEN
9963          l_txn_tmp_id := l_ser_trx_id;
9964       END IF;
9965 
9966             FOR i IN 1 .. l_serial_numbers.COUNT LOOP
9967          l_return_status := inv_trx_util_pub.insert_ser_trx
9968      (p_trx_tmp_id => l_txn_tmp_id
9969       ,p_user_id   => l_user_id
9970       ,p_fm_ser_num=> l_serial_numbers(i)
9971       ,p_to_ser_num=> l_serial_numbers(i)
9972       ,x_proc_msg  => x_msg_data );
9973          IF (l_return_status = -1) THEN
9974       IF (l_debug = 1) THEN
9975          debug('Error inserting MSNT', 'transfer_contents', 9);
9976       END IF;
9977       RAISE fnd_api.g_exc_error;
9978          END IF;
9979             END LOOP;
9980          END IF;
9981       END LOOP;
9982 
9983       IF (l_debug = 1) THEN
9984          DEBUG('xxx: Calling process_lpn_trx','transfer_contents',9);
9985          DEBUG('      (p_trx_hdr_id => ' || l_hdr_id,'transfer_contents',9);
9986       END IF;
9987 
9988       l_return_status :=inv_lpn_trx_pub.process_lpn_trx
9989         (p_trx_hdr_id         => l_hdr_id
9990    ,p_proc_mode         => 1
9991          ,p_commit            => fnd_api.g_false
9992          ,x_proc_msg          => x_msg_data);
9993 
9994    -- Call complete operation instance
9995    FOR i IN 1 .. l_txn_tmp_id_tb.COUNT LOOP
9996       wms_atf_runtime_pub_apis.complete_operation_instance
9997   ( p_source_task_id  =>  l_txn_tmp_id_tb(i)
9998     ,p_activity_id    =>  1 -- inbound
9999     ,p_operation_type_id => 1 -- load
10000     ,x_return_status  =>  x_return_status
10001     ,x_msg_data       =>  x_msg_data
10002     ,x_msg_count      =>  x_msg_count
10003     ,x_error_code     =>  l_error_code
10004     );
10005       IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
10006    IF (l_debug = 1) THEN
10007                DEBUG('xxx: Error in complete_operation_instance on '
10008          || ' MMTT = ' || l_txn_tmp_id_tb(i),'transfer_contents',9);
10009    END IF;
10010    RAISE fnd_api.g_exc_error;
10011       END IF;
10012 
10013    END LOOP;
10014 
10015       -- Update any MOL assoc with the old LPN to new LPN id
10016       IF (l_loose_contents = 1) THEN
10017    l_progress := '290';
10018    -- Update any MMTT assoc with the old LPN to the new LPN id
10019    IF (l_debug = 1) THEN
10020       -- ER 7307189 changes start
10021       debug('Updating MMTT...','transfer_contents11111',9);
10022       debug('Updating MMTT...','transfer_contents p_into_lpn_id'||p_into_lpn_id,9);
10023       debug('Updating MMTT...','transfer_contents p_from_lpn_id'||p_from_lpn_id,9);
10024       -- ER 7307189 changes end
10025 
10026    END IF;
10027 
10028    --BUG 3435079: The query doesn't seem to be picking up the lpn_id
10029    --index on MMTT.  So replace it with the following, and also
10030    --Move it BEFORE updating MOL
10031    UPDATE
10032      mtl_material_transactions_temp
10033      SET
10034      lpn_id = p_into_lpn_id
10035      WHERE
10036      lpn_id = p_from_lpn_id;
10037 
10038    IF (l_debug = 1) THEN
10039       debug('Updating MOL...','transfer_contents',9);
10040    END IF;
10041 
10042    BEGIN
10043       UPDATE
10044         mtl_txn_request_lines
10045         SET
10046         lpn_id = p_into_lpn_id
10047         WHERE
10048         lpn_id = p_from_lpn_id;
10049    EXCEPTION
10050       WHEN OTHERS THEN
10051          IF (l_debug = 1) THEN
10052 	    DEBUG('xxx: Error updating MOLs','transfer_contents',9);
10053          END IF;
10054          RAISE fnd_api.g_exc_error;
10055    END;
10056 
10057    l_progress := '300';
10058 
10059       END IF;
10060    END IF; -- end l_from_context check
10061 
10062    IF (l_debug = 1) THEN
10063       DEBUG('Exiting transfer_contents','transfer_contents', 9);
10064    END IF;
10065 
10066    COMMIT;
10067 
10068 EXCEPTION
10069    WHEN OTHERS THEN
10070       fnd_message.set_name('WMS', 'WMS_TASK_ERROR');
10071       fnd_msg_pub.add;
10072       x_return_status := fnd_api.g_ret_sts_unexp_error;
10073       IF (l_debug = 1) THEN
10074          debug('Exception occured after l_progress ='|| l_progress, 'transfer_contents', 9);
10075       END IF;
10076       IF (wlpnc_cur%isopen) THEN
10077 	 CLOSE wlpnc_cur;
10078       END IF;
10079       BEGIN
10080 	 ROLLBACK TO transfer_contents_pub;
10081       EXCEPTION
10082 	 WHEN OTHERS THEN
10083 	    IF (l_debug = 1) THEN
10084 	       debug('SQLCODE:'||SQLCODE||' SQLERRM'||Sqlerrm,9);
10085 	    END IF;
10086       END;
10087 END transfer_contents;
10088 
10089 
10090  /**
10091   *   This procedure is a wrapper for the complete_putaway API and will be
10092   *   called for a group of tasks. Accepts the current group Id or
10093   *   transaction_header_id and the values confirmed by the user on the page
10094   *   and processes the eligible tasks
10095   *   Processing Logic:
10096   *     -> Loop through each task/MMTT row given the group_id/header_id
10097   *     -> Consume the task quantity based on the confirmed quantity
10098   *     -> Create the lots and serials interface records (receiving LPN)
10099   *     -> Create the WLPNI records (receiving LPN) or
10100   *        call pack/unpack API to reflect nesting changes
10101   *     -> Call the complete_putaway API for the given task
10102   *     -> Call the receiving Transaction Manager (receiving LPN)
10103   *
10104   *  @param  x_return_status            Return Status Indicator
10105   *  @param  x_msg_count                Stacked messages counter
10106   *  @param  x_msg_data                 Stacked Messages
10107   *  @param  p_group_id                 ID of the current group of tasks
10108   *  @param  p_txn_header_id            header_id for the current group of tasks
10109   *  @param  p_drop_type                Drop Type Identifier.<br>
10110   *                                     ID - Item Drop<br>
10111   *                                     CD - Consolidated Drop<br>
10112   *                                     MD - Manual Drop<br>
10113   *                                     UD - User Drop<br>
10114   *  @param  p_lpn_mode                 Flag for LPN actions
10115   *                                     1 - Transfer Contents<br>
10116   *                                     2 - Drop Entire LPN<br>
10117   *                                     3 - Item Drop
10118   *  @param  p_lpn_id                   LPN being putaway
10119   *  @param  p_lpn_context               Context of the LPN being putaway
10120   *  @param  p_organization_id          Organization ID
10121   *  @param  p_user_id                  Logged in Employee ID
10122   *  @param  p_item_id                  Item for the current group (item drop)
10123   *  @param  p_revision                 Revision confirmed (item drop)
10124   *  @param  p_lot_number               Lot Number confirmed (item drop)
10125   *  @param  p_subinventory_code        Drop to Subinventory Code
10126   *  @param  p_locator_id               Drop to Locator ID
10127   *  @param  p_quantity                 Quantity Confirmed (item drop)
10128   *  @param  p_uom_code                 Unit of Measure confirmed (item drop)
10129   *  @param  p_entire_lpn               Flag to indicate if entire LPN is putaway
10130   *  @param  p_to_lpn_name              License Plate number of the Into LPN
10131   *  @param  p_to_lpn_id                LPN Id of the Into LPN
10132   *  @param  p_project_id               Project ID
10133   *  @param  p_task_id                  Task ID
10134   *  @param  p_reference                MOL reference
10135   *  @param  p_qty_reason_id            Reason ID for Quantity Discrepancy
10136   *  @param  p_loc_reason_id            Reason ID for locator discrepancy
10137   *  @param  p_process_serial_flag      Flag set if serials are confirmed in UI
10138   *  @param  p_msni_txn_interface_id    Transaction_interface_id of MSNI records
10139   *                                     created from the UI
10140   *  @param  p_product_transaction_id   Product_transaction_id of MTLI/MSNI
10141   *                                     populated if user confirms partial qty
10142   **/
10143   PROCEDURE Complete_Putaway_Wrapper(
10144       x_return_status           OUT  NOCOPY VARCHAR2
10145     , x_msg_count               OUT  NOCOPY NUMBER
10146     , x_msg_data                OUT  NOCOPY VARCHAR2
10147       /* Added for LMS project: Anupam Jain*/
10148     , x_lms_operation_plan_id   OUT  NOCOPY NUMBER
10149       /* LMS change end*/
10150     , p_group_id                IN          NUMBER
10151     , p_txn_header_id           IN          NUMBER
10152     , p_drop_type               IN          VARCHAR2
10153     , p_lpn_mode                IN          NUMBER
10154     , p_lpn_id                  IN          NUMBER
10155     , p_lpn_context             IN          NUMBER
10156     , p_organization_id         IN          NUMBER
10157     , p_user_id                 IN          NUMBER
10158     , p_item_id                 IN          NUMBER
10159     , p_revision                IN          VARCHAR2
10160     , p_lot_number              IN          VARCHAR2
10161     , p_subinventory_code       IN          VARCHAR2
10162     , p_locator_id              IN          NUMBER
10163     , p_quantity                IN          NUMBER
10164     , p_uom_code                IN          VARCHAR2
10165     , p_entire_lpn              IN          VARCHAR2
10166     , p_to_lpn_name             IN          VARCHAR2
10167     , p_to_lpn_id               IN          NUMBER
10168     , p_project_id              IN          NUMBER
10169     , p_task_id                 IN          NUMBER
10170     , p_reference               IN          VARCHAR2
10171     , p_qty_reason_id           IN          NUMBER
10172     , p_loc_reason_id           IN          NUMBER
10173     , p_process_serial_flag     IN          VARCHAR2
10174     , p_msni_txn_interface_id   IN          NUMBER
10175     , p_product_transaction_id  IN          NUMBER
10176     , p_secondary_quantity      IN          NUMBER --OPM Convergence
10177     , p_secondary_uom           IN          VARCHAR2 --OPM Convergence
10178     , p_lpn_initially_loaded    IN          VARCHAR2 ) IS
10179 
10180     --Cursor Declarations
10181 
10182     --Fetches the move order lines in the global temporary table for the current group
10183     CURSOR mol_csr IS
10184        SELECT  mtrl.line_id
10185 	       , mtrl.quantity
10186 	       , NVL(mtrl.quantity_detailed, 0)
10187 	       , NVL(mtrl.quantity_delivered, 0)
10188  	       , mtrl.uom_code
10189 	       , mtrl.secondary_quantity --OPM Convergence
10190 	       , NVL(mtrl.secondary_quantity_detailed, 0) --OPM Convergence
10191 	       , NVL(mtrl.secondary_quantity_delivered, 0) --OPM Convergence
10192 	       , mtrl.secondary_uom_code --OPM Convergence
10193 	 FROM mtl_txn_request_lines mtrl
10194 	 WHERE mtrl.line_id IN (SELECT  DISTINCT  gtmp.move_order_line_id
10195 			   FROM    wms_putaway_group_tasks_gtmp gtmp
10196 			   WHERE   gtmp.group_id = p_group_id
10197 			   AND     gtmp.transaction_header_id = p_txn_header_id
10198 			   AND     gtmp.row_type = g_row_tp_all_task)
10199          AND   mtrl.line_status <> 5 ; -- 9037915
10200 
10201     --R12: This cursor is used to find out the matching MOL for a
10202     --given group of serials with a particular inspection status and
10203     --lot number
10204     CURSOR serial_mol_csr(v_inspect_status NUMBER, v_lot_number VARCHAR2) IS
10205        SELECT  mtrl.line_id
10206 	       , mtrl.quantity
10207 	       , NVL(mtrl.quantity_detailed, 0)
10208 	       , NVL(mtrl.quantity_delivered, 0)
10209  	       , mtrl.uom_code
10210 	       , mtrl.secondary_quantity --OPM Convergence
10211 	       , NVL(mtrl.secondary_quantity_detailed, 0) --OPM Convergence
10212 	       , NVL(mtrl.secondary_quantity_delivered, 0) --OPM Convergence
10213 	       , mtrl.secondary_uom_code --OPM Convergence
10214 	 FROM mtl_txn_request_lines mtrl
10215 	 WHERE mtrl.line_id IN (SELECT  DISTINCT  gtmp.move_order_line_id
10216 			   FROM    wms_putaway_group_tasks_gtmp gtmp
10217 			   WHERE   gtmp.group_id = p_group_id
10218 			   AND     gtmp.transaction_header_id = p_txn_header_id
10219 			   AND     gtmp.row_type = g_row_tp_all_task)
10220 	 AND   Nvl(mtrl.lot_number,'&*_') = Nvl(v_lot_number,'&*_')
10221 	 AND   Nvl(mtrl.inspection_status,-1) = Nvl(v_inspect_status,-1);
10222 
10223     --Fetches the details of the current task from the global temp table
10224     --drop types 'Consolidated Drop' or 'Item Drop'
10225     CURSOR all_tasks_csr(v_disc VARCHAR2) IS
10226       SELECT  transaction_temp_id
10227             , lpn_id
10228             , move_order_line_id
10229             , inventory_item_id
10230             , revision
10231             , lot_number
10232             , transaction_quantity
10233             , transaction_uom
10234             , txn_source_id
10235             , backorder_delivery_detail
10236             , crossdock_type
10237             , wip_supply_type
10238             , secondary_quantity  -- OPM Convergence
10239             , secondary_uom       -- 9037915
10240 	    , inspection_status
10241 	    , primary_uom_code
10242       FROM    wms_putaway_group_tasks_gtmp
10243       WHERE   group_id = p_group_id
10244       AND     transaction_header_id = p_txn_header_id
10245       AND     row_type = G_ROW_TP_ALL_TASK
10246       AND     ( (v_disc = 'N') --no qty idsc
10247                 OR
10248                 (v_disc = 'Y' AND p_drop_type = G_DT_ITEM_DROP
10249                  AND process_flag = 'Y'  --item drop and qty disc
10250                 )
10251               );
10252 
10253     --Fetches the information for the current task for the drop types
10254     --'Manual Drop' or 'User Drop'
10255     CURSOR md_tasks_csr IS
10256       SELECT  mmtt.transaction_temp_id
10257             , mtrl.lpn_id
10258             , mtrl.line_id
10259             , mmtt.inventory_item_id
10260             , mmtt.revision
10261             , mtrl.lot_number
10262             , mmtt.transaction_quantity
10263             , mmtt.transaction_uom
10264             , mtrl.txn_source_id
10265             , mtrl.backorder_delivery_detail_id
10266             , mtrl.crossdock_type
10267             , mmtt.wip_supply_type
10268             , mmtt.secondary_transaction_quantity --OPM Convergence
10269             , mmtt.secondary_uom_code -- 9037915
10270 	    , mtrl.inspection_status
10271 	    , msi.primary_uom_code
10272       FROM    mtl_material_transactions_temp mmtt
10273             , mtl_txn_request_lines mtrl
10274 	    , mtl_txn_request_headers mtrh
10275 	    , mtl_system_items_kfv msi
10276 	    , (          /*5723418*/
10277                 SELECT  wlpn.lpn_id
10278                 FROM    wms_license_plate_numbers wlpn
10279                 START WITH wlpn.lpn_id = p_lpn_id
10280                 CONNECT BY PRIOR wlpn.lpn_id = wlpn.parent_lpn_id
10281               ) wlpn
10282       WHERE   mtrh.organization_id = p_organization_id
10283       AND     mtrh.move_order_type = 6
10284       AND     mtrl.header_id = mtrh.header_id
10285       AND     mtrl.line_id = mmtt.move_order_line_id
10286       AND     mtrl.line_status = 7
10287       AND     mtrl.lpn_id = wlpn.lpn_id
10288       AND     msi.inventory_item_id = mtrl.inventory_item_id
10289       AND     msi.organization_id = mtrl.organization_id;
10290 
10291     --R12: This cursor will return all serials entered by the user
10292     --in this transaction
10293     CURSOR rcv_serials_csr IS
10294        SELECT  msn.serial_number
10295 	 ,     'N'
10296 	 ,     msn.inspection_status
10297 	 ,     msn.lot_number
10298 	 FROM    mtl_serial_numbers_interface msni
10299 	 ,       mtl_serial_numbers msn
10300 	 ,       rcv_serials_supply rss
10301 	 WHERE   msni.transaction_interface_id = p_msni_txn_interface_id
10302 	 AND   msn.serial_number BETWEEN msni.fm_serial_number AND Nvl(msni.to_serial_number,  msni.fm_serial_number)
10303 	 AND   Length(msn.serial_number) = Length(msni.fm_serial_number)
10304 	 AND   msn.inventory_item_id = p_item_id
10305 	 AND   msn.current_organization_id = p_organization_id
10306 	 AND   msn.lpn_id = p_lpn_id
10307 	 AND   rss.serial_num = msn.serial_number
10308 	 AND   rss.supply_type_code = 'RECEIVING'
10309 	 ORDER BY msn.serial_number,msn.lot_number;
10310 
10311     --Fetches the serials that were consumed by the user in UI if user confirms
10312     --partial quantity for an Inventory/WIP LPN
10313     CURSOR  msnt_ser_csr(v_lpn_id  NUMBER
10314                        , v_item_id NUMBER) IS
10315       SELECT  serial_number
10316             , 'N'
10317       FROM    mtl_serial_numbers
10318       WHERE   inventory_item_id = v_item_id
10319       AND     lpn_id = v_lpn_id
10320       AND     (
10321                 (p_revision IS NOT NULL and revision = p_revision)
10322                 OR
10323                 (p_revision IS NULL)
10324               )
10325       AND     (
10326                 (p_lot_number IS NOT NULL and lot_number = p_lot_number)
10327                 OR
10328                 (p_lot_number IS NULL)
10329               )
10330       AND     group_mark_id = -4936;
10331 
10332     --Local variables
10333     l_quantity                NUMBER;   --Quantity passed to complete_putaway
10334     l_remaining_qty           NUMBER;   --Track remaining quantity to confirm
10335     l_sug_grp_quantity        NUMBER;   --Total suggested quantity for the group
10336     l_grp_prm_quantity        NUMBER;   --Total group quantity in primary UOM
10337     l_rem_grp_quantity        NUMBER;   --Remaining quantity to consume for MO splits
10338     l_prm_user_qty            NUMBER;   --Confirmed quantity in primary uom
10339     l_rem_user_qty            NUMBER;   --Remaining quantity to be confirmed
10340     l_mol_qty                 NUMBER;   --MOL quantity
10341     l_mol_qty_detailed        NUMBER;   --MOL total allocated quantity
10342     l_mol_qty_delivered       NUMBER;   --MOL transacted quantity
10343     l_new_txn_header_id       NUMBER;   --New value for transaction_header_id
10344     l_mol_uom_code            MTL_UNITS_OF_MEASURE_TL.uom_code%TYPE;   --MOL uom code
10345     l_tmp_qty1                NUMBER;   --MOL Qty - MOL Delivered Qty
10346     l_tmp_prm_qty             NUMBER;   --MOL Qty - Delivered Qty  in primary uom
10347     l_lpn_controlled_flag     NUMBER;   --LPN controlled flag for the drop sub
10348     l_qty_to_split            NUMBER;   --Quantity to split
10349     l_uom_con_sug_grp_qty     NUMBER;   --Suggested quantity converted to primary UOM???
10350     l_grp_uom_code            MTL_UNITS_OF_MEASURE_TL.uom_code%TYPE;
10351     l_uom_code                MTL_UNITS_OF_MEASURE_TL.uom_code%TYPE;
10352     l_primary_uom_code        MTL_UNITS_OF_MEASURE_TL.uom_code%TYPE;
10353     l_serialized_item         BOOLEAN;  --Flag to indicate if the item is serialized
10354     l_group_id                NUMBER;   -- Group ID
10355     l_product_transaction_id  NUMBER;   --Product transaction id for complete_putaway
10356     l_item_id                 NUMBER;   --Item ID for complete_putaway
10357     l_subinventory_code       mtl_secondary_inventories.secondary_inventory_name%TYPE;
10358     l_locator_id              NUMBER;   --Locator for complete_putaway
10359     l_lpn_id                  NUMBER;   --p_LPN_ID for complete_putaway???--check
10360     l_to_lpn_id               NUMBER;   --To LPN ID -- for pack/unpack
10361     l_to_lpn_name             wms_license_plate_numbers.license_plate_number%TYPE;
10362     l_revision                mtl_item_revisions.revision%TYPE;   --p_rev for complete_putaway
10363     l_lot_number              mtl_lot_numbers.lot_number%type;    --p_lot for complete_putaway
10364     l_mmtt_prm_qty             NUMBER;  --Lot quantity in primary uom
10365     l_lot_expiration_date     DATE;     --Lot expiration date
10366     l_lot_status_id           NUMBER;   --Lot Status
10367     l_cur_ser_number          mtl_serial_numbers.serial_number%TYPE;
10368     l_mmtt_temp_id            NUMBER;   --Variable to store p_temp_id of complete_putaway
10369     l_mmtt_temp_id_tbl        num_tab;  --Transaction_Temp_ids for the current group
10370     l_mmtt_item_id_tbl        num_tab;  --Item ID for the current group
10371     l_mmtt_rev_tbl            rev_tab;  --Revision for the current group
10372     l_mmtt_lot_tbl            lot_tab;  --Lot Number for the current group
10373     l_mmtt_qty_tbl            num_tab;  --transaction_quantity for the current group
10374     l_mmtt_uom_tbl            uom_tab;  --uom for the current group
10375     l_bk_del_det_tbl          num_tab;  --Backorder Delivery Detail to hold crossdock infor
10376     l_xdock_type_tbl          num_tab;  --Crossdock type for the current group
10377     l_wip_sup_typ_tbl         num_tab;  --Wip Supply Type for the current group
10378     l_mol_line_id             NUMBER;   --MO Line ID
10379     l_mol_lpn_id_tbl          num_tab;  --LPN_ID for the current task => p_lpn_id of complete_putaway
10380     l_mol_line_id_tbl         num_tab;  --MO Line ID
10381     l_parent_txn_id_tbl       num_tab;  --Parent Transaction Id - for rcv_serials_supply
10382     l_parent_txn_id           NUMBER;   --Parent Transaction Id - for rcv_serials_supply
10383     l_lot_control_code        NUMBER;   --Item Lot control code
10384     l_serial_control_code     NUMBER;   --Item Serial control code
10385     l_mmtt_count              NUMBER;   --No. of MMTTs for the current MOL
10386     l_ser_intf_id             NUMBER;   --Updated txn_interface_id for MSNI
10387     l_cur_task_ser_count      NUMBER;   --Count of serials consumed for the current task
10388     l_counter                 NUMBER;   --Count of serials consumed for the current task--bug12399417
10389     l_entire_lpn              VARCHAR2(1);    --Is entire LPN putaway
10390     l_process_flag            VARCHAR2(10);   --Indicator to pick tasks from global temp table
10391     l_disc                    VARCHAR2(10);   --Quantity discrepancy indicator
10392     l_serial_txn_temp_id      NUMBER;   --Serial temp id for lot and serial controlled item
10393     l_serial_temp_id          NUMBER;   --serial_transaction_temp_id - for MSNI
10394     l_temp_id_tbl             num_tab;  --Table of transaction_temp_id for the group
10395     l_ser_num_tbl             ser_num_tab;  --Table to hold serials in MSNI
10396     l_parent_ser_tbl          ser_num_tab;  --Table for serials for the parent serial
10397     l_ser_mark_tbl            ser_mark_tab; --Flag to indicate if serial is confirmed
10398     l_ser_qty                 NUMBER;       --Quantity for process_lot_serial API
10399     l_action                  NUMBER;       --Action code for the process_lot_serial API
10400     l_txn_ret                 NUMBER := 0;  --Return status of the Inventory TM
10401     l_msg_data                VARCHAR2(10000);
10402     l_del_count               NUMBER;       --No. of rows deleted in the temp table
10403     l_business_flow_code      NUMBER := 30; -- default to Inventory Putaway
10404     l_debug                   NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
10405     l_progress                VARCHAR2(10);       --Progress indicator
10406     l_proc_name               VARCHAR2(30) := 'complete_putaway_wrapper';
10407     l_mos_to_split_tbl        split_mo_tb_tp;
10408     l_mo_splt_tb              inv_rcv_integration_apis.mo_in_tb_tp;
10409     l_mo_splt_cnt             NUMBER;       --MO Split table counter
10410     l_rcv_commit              BOOLEAN := FALSE;
10411     l_lpn_mode                NUMBER;    -- Drop mode
10412     l_drop_sub_type           NUMBER;    -- Drop subinventory type
10413     l_xdock_to_wip            BOOLEAN;   -- Flag to indicate if the task is xdocked to WIP
10414     l_transaction_interface_id  NUMBER;  -- For interface recors
10415     l_dummy_temp_id           NUMBER;
10416     l_emp_id                  NUMBER;
10417     l_batch_seq               NUMBER;  --BUG 3306988
10418     l_lpn_serials_cnt         NUMBER := 0;  --Count of serials in the LPN
10419     l_skip_mmtt               BOOLEAN := FALSE; --
10420     l_error_code            NUMBER;
10421     l_drop_off_time           DATE := Sysdate; --DBI FIX
10422     l_grp_sec_quantity      NUMBER; --OPM Convergence
10423     l_grp_sec_uom           VARCHAR2(3); --OPM Convergence
10424     l_mol_sec_qty                 NUMBER;   --OPM Convergence
10425     l_mol_sec_qty_detailed        NUMBER;   --OPM Convergence
10426     l_mol_sec_qty_delivered       NUMBER;   --OPM Convergence
10427     l_mol_sec_uom_code       VARCHAR2(3); --OPM Convergence
10428     l_tmp_sec_qty            NUMBER; --OPM Convergence
10429     l_sec_user_qty NUMBER; --opm convergence
10430     l_mmtt_sec_qty_tbl            num_tab;  --OPM Convergence
10431     l_mmtt_sec_uom_tbl            uom_tab;  --OPM Convergence -- 9037915
10432     l_remaining_sec_qty     NUMBER; --OPM Convergence
10433     l_mmtt_sec_qty    NUMBER;--OPM Convergence
10434     l_secondary_quantity NUMBER; --OPM Convergence
10435     -- l_secondary_uom NUMBER; --OPM Convergence -- 9037915
10436     l_secondary_uom VARCHAR2(3) ; --OPM Convergence -- 9037915
10437     l_prim_qty_consumable     NUMBER; --BUG 5075410
10438 
10439     l_sec_qty_consumable     NUMBER; -- Bug#9037915
10440     l_sec_qty_to_split       NUMBER; -- Bug#9037915
10441 
10442 
10443     l_msni_inspect_status_tbl num_tab;
10444     TYPE varchar80_tab IS TABLE OF VARCHAR2(80) INDEX BY BINARY_INTEGER;
10445     l_msni_lot_tbl varchar80_tab;
10446     l_msni_qty_tbl num_tab;
10447     l_inspection_status_tbl num_tab;
10448     l_ser_inspect_status_tbl num_tab;
10449     l_ser_lot_num_tbl varchar80_tab;
10450     TYPE varchar3_tab IS TABLE OF VARCHAR2(3) INDEX BY BINARY_INTEGER;
10451     l_primary_uom_code_tbl varchar3_tab;
10452     l_result BOOLEAN;
10453     -- LMS specific variable
10454     l_lms_rec_count NUMBER       := 0;
10455     -- LMS code end
10456 
10457 
10458 BEGIN
10459     IF (l_debug = 1) THEN
10460       DEBUG('Entered complete_putaway_wrapper with the following params:', l_proc_name);
10461       DEBUG(' p_group_id               ==> '||p_group_id, l_proc_name);
10462       DEBUG(' p_txn_header_id          ==> '||p_txn_header_id,l_proc_name);
10463       DEBUG(' p_drop_type              ==> '||p_drop_type,l_proc_name);
10464       DEBUG(' p_drop_type              ==> '||p_drop_type,l_proc_name);
10465       DEBUG(' p_lpn_mode               ==> '||p_lpn_mode,l_proc_name);
10466       DEBUG(' p_lpn_id                 ==> '||p_lpn_id,l_proc_name);
10467       DEBUG(' p_organization_id        ==> '||p_organization_id,l_proc_name);
10468       DEBUG(' p_user_id                ==> '||p_user_id,l_proc_name);
10469       DEBUG(' p_item_id                ==> '||p_item_id,l_proc_name);
10470       DEBUG(' p_revision               ==> '||p_revision,l_proc_name);
10471       DEBUG(' p_lot_number             ==> '||p_lot_number,l_proc_name);
10472       DEBUG(' p_subinventory_code      ==> '||p_subinventory_code,l_proc_name);
10473       DEBUG(' p_locator_id             ==> '||p_locator_id,l_proc_name);
10474       DEBUG(' p_quantity               ==> '||p_quantity,l_proc_name);
10475       DEBUG(' p_uom_code               ==> '||p_uom_code,l_proc_name);
10476       DEBUG(' p_to_lpn_name            ==> '||p_to_lpn_name,l_proc_name);
10477       DEBUG(' p_to_lpn_id              ==> '||p_to_lpn_id,l_proc_name);
10478       DEBUG(' p_project_id             ==> '||p_project_id,l_proc_name);
10479       DEBUG(' p_task_id                ==> '||p_task_id,l_proc_name);
10480       DEBUG(' p_reference              ==> '||p_reference, l_proc_name);
10481       DEBUG(' p_qty_reason_id          ==> '||p_qty_reason_id,l_proc_name);
10482       DEBUG(' p_loc_reason_id          ==> '||p_loc_reason_id,l_proc_name);
10483       DEBUG(' p_process_serial_flag    ==> '||p_process_serial_flag,l_proc_name);
10484       DEBUG(' p_entire_lpn             ==> '||p_entire_lpn,l_proc_name);
10485       DEBUG(' p_msni_txn_interface_id  ==> '||p_msni_txn_interface_id,l_proc_name);
10486       DEBUG(' p_product_transaction_id ==> '||p_product_transaction_id,l_proc_name);
10487       DEBUG(' p_secondary_quantity     ==> '||p_secondary_quantity,l_proc_name);
10488       DEBUG(' p_secondary_uom          ==> '||p_secondary_uom,l_proc_name);
10489       DEBUG(' p_lpn_initially_loaded   ==> '||p_lpn_initially_loaded,l_proc_name);
10490     END IF;
10491 
10492     -- Initialize the return status
10493     x_return_status := FND_API.G_RET_STS_SUCCESS;
10494 
10495     -- Read the input parameters
10496     l_group_id               := p_group_id;
10497     l_product_transaction_id := p_product_transaction_id;
10498     l_serialized_item        := FALSE;
10499     l_mo_splt_cnt            := 0;
10500     l_lpn_serials_cnt        := 0;
10501     l_skip_mmtt              := FALSE;
10502 
10503     IF (l_debug = 1) THEN
10504       DEBUG('Entering complete_putaway_wrapper. Entry time : ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), l_proc_name);
10505     END IF;
10506 
10507     -- Create the save point
10508     SAVEPOINT complete_putaway_wrap_sp;
10509 
10510     -- Assign p_lpn_mode to l_lpn_mode
10511     l_lpn_mode := p_lpn_mode;
10512 
10513     -- For manual drops, since a load is not explicitly done first, we need
10514     -- to perform the activate and complete operation instance for load.
10515     -- We also then need to activate the drop operation.
10516     IF (p_drop_type = G_DT_MANUAL_DROP) THEN
10517       IF (l_debug = 1) THEN
10518         DEBUG('complete_putaway_wrapper: Activate/Complete operations for Manual Drop');
10519       END IF;
10520       l_progress := '5.1';
10521 
10522       BEGIN
10523         SELECT employee_id
10524         INTO   l_emp_id
10525         FROM   fnd_user
10526         WHERE  user_id = p_user_id;
10527       EXCEPTION
10528         WHEN NO_DATA_FOUND THEN
10529           IF (l_debug = 1) THEN
10530             DEBUG('complete_putaway_wrapper: There is no employee tied to the user');
10531           END IF;
10532           l_emp_id := NULL;
10533       END;
10534       IF (l_debug = 1) THEN
10535         DEBUG('complete_putaway_wrapper: Employee ID: ' || l_emp_id);
10536       END IF;
10537       l_progress := '5.2';
10538 
10539       BEGIN -- Simulate Load Operation
10540         IF (l_debug = 1) THEN
10541           DEBUG('complete_putaway_wrapper: Calling Complete_ATF_Load API');
10542         END IF;
10543         l_progress := '5.3';
10544 
10545         WMS_PUTAWAY_UTILS.Complete_ATF_Load
10546           (x_return_status  => x_return_status,
10547            x_msg_count      => x_msg_count,
10548            x_msg_data       => x_msg_data,
10549            p_org_id         => p_organization_id,
10550            p_lpn_id         => p_lpn_id,
10551            p_emp_id         => l_emp_id);
10552         IF (l_debug = 1) THEN
10553            DEBUG('complete_putaway_wrapper: Successfully called Complete_ATF_Load');
10554         END IF;
10555         l_progress := '5.4';
10556 
10557         -- Check for the return status
10558         IF (l_debug = 1) THEN
10559            debug('complete_putaway_wrapper: Return status of API: ' || x_return_status);
10560         END IF;
10561 
10562         IF x_return_status = fnd_api.g_ret_sts_success THEN
10563           -- Activate completed successfully.
10564           NULL;
10565         ELSIF x_return_status = fnd_api.g_ret_sts_error THEN
10566           RAISE fnd_api.g_exc_error;
10567         ELSIF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
10568           RAISE fnd_api.g_exc_unexpected_error;
10569         END IF;
10570         l_progress := '5.5';
10571       EXCEPTION
10572         WHEN OTHERS THEN
10573           RAISE fnd_api.g_exc_unexpected_error;
10574       END; -- Simulate Load Operation
10575 
10576       BEGIN -- Activate operation for Drop
10577         IF (l_debug = 1) THEN
10578           DEBUG('complete_putaway_wrapper: Calling ATF_For_Manual_Drop API');
10579         END IF;
10580         l_progress := '5.6';
10581 
10582         WMS_PUTAWAY_UTILS.ATF_For_Manual_Drop
10583           (x_return_status => x_return_status,
10584            x_msg_count     => x_msg_count,
10585            x_msg_data      => x_msg_data,
10586            p_call_type     => G_ATF_ACTIVATE_PLAN,
10587            p_org_id        => p_organization_id,
10588            p_lpn_id        => p_lpn_id,
10589            p_emp_id        => l_emp_id);
10590         IF (l_debug = 1) THEN
10591            DEBUG('complete_putaway_wrapper: Successfully called ATF_For_Manual_Drop');
10592         END IF;
10593 
10594         l_progress := '5.7';
10595         IF (l_debug = 1) THEN
10596           debug('complete_putaway_wrapper: Return status of API: ' || x_return_status);
10597         END IF;
10598 
10599         IF x_return_status = fnd_api.g_ret_sts_success THEN
10600           -- Activate op plan completed successfully.
10601           NULL;
10602         ELSIF x_return_status = fnd_api.g_ret_sts_error THEN
10603           RAISE fnd_api.g_exc_error;
10604         ELSIF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
10605           RAISE fnd_api.g_exc_unexpected_error;
10606         END IF;
10607         l_progress := '5.8';
10608       EXCEPTION
10609         WHEN OTHERS THEN
10610           RAISE fnd_api.g_exc_unexpected_error;
10611       END; -- Activate operation for Drop
10612     END IF; -- Manual Drop activate/complete operation instance
10613 
10614     --For the item drop scenario, first check if there was a quantity discrepancy
10615     --and set the flag appropriately
10616     IF (p_drop_type = G_DT_ITEM_DROP) THEN
10617       BEGIN
10618         l_progress := '10';
10619 
10620          --Get the primary_uom_code, lot_control_code and serial_control_code
10621          --for the item being dropped
10622         SELECT transaction_quantity
10623              , transaction_uom
10624              , primary_quantity
10625              , primary_uom_code
10626              , lot_control_code
10627              , serial_number_control_code
10628              , secondary_quantity --OPM Convergence
10629              , secondary_uom --OPM Convergence
10630         INTO   l_sug_grp_quantity
10631              , l_grp_uom_code
10632              , l_grp_prm_quantity
10633              , l_primary_uom_code
10634              , l_lot_control_code
10635              , l_serial_control_code
10636              , l_grp_sec_quantity
10637              , l_grp_sec_uom
10638         FROM   wms_putaway_group_tasks_gtmp
10639         WHERE  group_id = p_group_id
10640         AND    transaction_header_id = p_txn_header_id
10641         AND    row_type = G_ROW_TP_GROUP_TASK;
10642 
10643         IF (l_debug =1) THEN
10644           DEBUG('The suggested quantity for the group: ' || l_sug_grp_quantity, l_proc_name);
10645           DEBUG('lot code: ' || l_lot_control_code || ', serial code: ' || l_serial_control_code);
10646         END IF;
10647 
10648         l_progress := '20';
10649 
10650         --Convert the grp quantity into transaction uom
10651         IF l_grp_uom_code <> p_uom_code THEN
10652 	   l_uom_con_sug_grp_qty := inv_rcv_cache.convert_qty
10653 	                               (p_inventory_item_id => p_item_id
10654 					,p_from_qty         => l_sug_grp_quantity
10655 					,p_from_uom_code    => l_grp_uom_code
10656 					,p_to_uom_code      => p_uom_code);
10657 	 ELSE
10658           l_uom_con_sug_grp_qty := l_sug_grp_quantity;
10659         END IF;
10660 
10661         l_progress := '30';
10662 
10663         --Convert the confirmed quantity into primary uom
10664         IF (p_uom_code <> l_primary_uom_code) THEN
10665 	   l_prm_user_qty := inv_rcv_cache.convert_qty
10666 	                        (p_inventory_item_id => p_item_id
10667 				 ,p_from_qty         => p_quantity
10668 				 ,p_from_uom_code    => p_uom_code
10669 				 ,p_to_uom_code      => l_primary_uom_code);
10670         ELSE
10671           l_prm_user_qty := p_quantity;
10672         END IF;
10673         l_sec_user_qty := p_secondary_quantity; --OPM Convergence
10674         --Set the discrepancy flag if confirmed quantity does not match the
10675         --total quantity for the current group
10676         IF l_prm_user_qty <> l_grp_prm_quantity THEN
10677           l_disc := 'Y';
10678         ELSE
10679           l_disc := 'N';
10680         END IF;
10681 
10682       EXCEPTION
10683         WHEN OTHERS THEN
10684           IF (l_debug = 1) THEN
10685             DEBUG('Error fetching MMTTs for the current group. Cannot drop', l_proc_name, 1);
10686           END IF;
10687           fnd_message.set_name('WMS', 'WMS_TASK_ERROR');
10688           fnd_msg_pub.add;
10689           RAISE FND_API.G_EXC_ERROR;
10690       END;
10691     ELSE
10692       --For other drop types, the user would not be confirming quantity and uom
10693       --We would be transacting only the suggested quantities. Only sub and loc
10694       --would be user entered. So, set the quantity discrepancy flag as 'N'
10695       l_disc := 'N';
10696     END IF; --END IF check qty discrepancy for item drop
10697 
10698     l_progress := '40';
10699 
10700     IF (l_debug = 1) THEN
10701       DEBUG('complete_putaway_wrapper: Progress: ' || l_progress || '. Quantity Discrepancy flag: ' || l_disc, l_progress);
10702     END IF;
10703 
10704     --Move Order Splits
10705     --For an item drop scenario, we would need to split the move order line under
10706     --the following scenarios:
10707     --   a) User confirmed lesser than the suggested quantity in the UI
10708     --   b) Part of the MO Line is detailed to an MMTT record in another group
10709     --   c) Move Order line is not completely detailed
10710     --We would need to split the move orders only for an item drop since
10711     --the user would be transacting the entire quantity for other drop types
10712 
10713     --Decide on move order splits based on whether the item is serialized
10714     --This is needed since we will be matching the parent transactions with the
10715     --serials confirmed in the UI
10716     --The check for serialized item is needed only for a receiving LPN
10717     --We need this because for splitting move orders, we must first consume those
10718     --MOLS with serials of the parent txn
10719     --Eg. MOL1 has serials S1 - S5 with parent tranasction RT1 (rcv_serials_supply)
10720     --    MOL2 has serials S6 -S10 with parent transaction RT2
10721     --    User confirms S2, S6, S8 from the UI.
10722     --We should split MOL1 into MOL1 = 1 and create new line MOL3 for qty 4
10723     --We should split MOL2 into MOL2 = 2 and create new line MOL4 for qty 3
10724     --Later we would match the the serials S1, S6 and S8 against the parent txn
10725     --Even for a receiving LPN, if the user confirms the entire quantity, we
10726     --would be splitting the move order lines as if it were a non-serialized item
10727     --since the serials are anyway matched and only the quantity needs to be
10728     --matched.
10729     IF (p_drop_type = G_DT_ITEM_DROP) THEN
10730     --{
10731 
10732       -- BUG 5198628
10733       -- Get the serialized flag irrespective of l_disc
10734       IF (p_lpn_context = G_LPN_CONTEXT_RCV) THEN
10735           --Bug 5208888
10736           --For internal receipts, serials would be populated even if the serial control
10737           --code is dynamic at SO issue
10738           IF (l_serial_control_code = 6) THEN
10739             SELECT count(1)
10740             INTO   l_lpn_serials_cnt
10741             FROM   mtl_serial_numbers
10742             WHERE  lpn_id = p_lpn_id
10743             AND    inventory_item_id = p_item_id;
10744           END IF;
10745           IF ((l_serial_control_code IN (2, 5)) OR
10746               (l_serial_control_code = 6 AND p_reference = 'ORDER_LINE_ID') OR
10747               (l_serial_control_code = 6 AND p_reference = 'SHIPMENT_LINE_ID' AND
10748                l_lpn_serials_cnt > 0)) THEN
10749             l_serialized_item := TRUE;
10750           ELSE
10751             l_serialized_item := FALSE;
10752           END IF;
10753         ELSE
10754           l_serialized_item := FALSE;
10755         END IF;
10756    -- bug 5333789
10757    -- ELSE
10758    --   l_serialized_item := FALSE;
10759    -- END IF;
10760 
10761    -- IF (l_disc = 'Y' ) THEN
10762    -- bug 5333789
10763 
10764       --Initialize the remaining quantity in primary uom
10765       l_rem_user_qty := l_prm_user_qty;
10766 
10767       IF (l_debug = 1) THEN
10768       DEBUG('complete_putaway_wrapper: Progress: ' || l_progress || '. Item Drop scenario. Checking if MO Splits are required', l_proc_name);
10769       END IF;
10770 
10771       --The logic would be different based on whether the item is serial controlled or not
10772       -- IF (l_serialized_item) THEN bug 5333789
10773       IF (l_serialized_item and l_disc = 'Y') THEN
10774       --{
10775 	 IF (l_debug = 1) THEN
10776 	    DEBUG('complete_putaway_wrapper: Progress: ' || l_progress || '. Processing the MO lines for a serialized item', l_proc_name);
10777 	 END IF;
10778 
10779 	 l_progress := '50';
10780 
10781 	 --R12: Find out the serials entered by the user in the UI,
10782 	 --grouping by their inspection_status and lot_number
10783 	 SELECT count(1), Nvl(msn.inspection_status,-1), Nvl(msn.lot_number,'&*_')
10784 	 bulk collect INTO  l_msni_qty_tbl, l_msni_inspect_status_tbl, l_msni_lot_tbl
10785           FROM   mtl_serial_numbers_interface msni, mtl_serial_numbers msn
10786 	  WHERE  msni.transaction_interface_id = p_msni_txn_interface_id
10787 	   AND   msn.serial_number BETWEEN msni.fm_serial_number AND Nvl(msni.to_serial_number,msni.fm_serial_number)
10788 	   AND   Length(msn.serial_number) = Length(msni.fm_serial_number)
10789 	   GROUP BY msn.inspection_status, msn.lot_number;
10790 
10791 	 IF (l_debug = 1) THEN
10792 	    debug('Number of serial groups:'||l_msni_qty_tbl.COUNT,l_proc_name);
10793 	 END IF;
10794 
10795 	 --R12: Loop through each group of serials and find
10796 	 --the MOL to consume
10797 	 FOR i IN 1..l_msni_inspect_status_tbl.COUNT LOOP
10798 
10799 	    IF (l_debug = 1) THEN
10800 	       debug('Group ('||i||') Number:'||l_msni_qty_tbl(i)||' Inspect Status:'||
10801 		     l_msni_inspect_status_tbl(i)||' lot NUMBER:'
10802 		     ||l_msni_lot_tbl(i),l_proc_name);
10803 	    END IF;
10804 
10805 	    --Processing for a serialized item
10806 
10807 	    OPEN serial_mol_csr(l_msni_inspect_status_tbl(i),l_msni_lot_tbl(i));
10808 
10809 	    LOOP
10810 	       FETCH serial_mol_csr
10811 		 INTO l_mol_line_id
10812 		 , l_mol_qty
10813 		 , l_mol_qty_detailed
10814 		 , l_mol_qty_delivered
10815 		 , l_mol_uom_code
10816 		 , l_mol_sec_qty --OPM Convergence
10817 		 , l_mol_sec_qty_detailed --OPM Convergence
10818 		 , l_mol_sec_qty_delivered --OPM Convergence
10819 		 , l_mol_sec_uom_code;
10820 		 EXIT WHEN serial_mol_csr%NOTFOUND;
10821 
10822 	       l_progress := '60';
10823 
10824 	       --Have consumed the entire quantity confirmed and marked the temp table records
10825 	       IF l_rem_user_qty = 0 OR l_msni_qty_tbl(i) = 0 THEN
10826 		  EXIT;
10827 	       END IF;
10828 
10829 	       IF (l_debug = 1) THEN
10830 		  DEBUG('complete_putaway_wrapper: Working with mol: ' || l_mol_line_id ||
10831 			' qty: ' || l_mol_qty || ' qty det: ' || l_mol_qty_detailed || ' qty del: '
10832 			|| l_mol_qty_delivered || ' uom_code: ' || l_mol_uom_code, l_proc_name);
10833 	       END IF;
10834 
10835 	       --First get the difference between the quantity and quantity delivered
10836 	       l_tmp_qty1 := l_mol_qty - l_mol_qty_delivered;
10837 
10838 	       l_progress := '70';
10839 
10840 	       --Convert this into primary uom
10841 	       IF (l_mol_uom_code <> l_primary_uom_code) THEN
10842 		  l_tmp_prm_qty := inv_rcv_cache.convert_qty
10843 		                      (p_inventory_item_id => p_item_id
10844 				       ,p_from_qty         => l_tmp_qty1
10845 				       ,p_from_uom_code    => l_mol_uom_code
10846 				       ,p_to_uom_code      => l_primary_uom_code
10847 				       );
10848 		ELSE
10849 		  l_tmp_prm_qty := l_tmp_qty1;
10850 	       END IF;
10851 
10852 	       IF(l_debug = 1) THEN
10853 		  DEBUG('complete_putaway_wrapper: Progress: ' || l_progress ||
10854 			'. Available MO quantity for this group: ' || l_tmp_prm_qty, l_proc_name);
10855 	       END IF;
10856 
10857 	       l_progress := '80';
10858 
10859 	       --Now that we are ready with quantity calculations, mark the all tasks
10860 	       --in the current group for this move order line
10861 	       UPDATE wms_putaway_group_tasks_gtmp
10862 		 SET    process_flag = 'Y'
10863 		 WHERE  group_id = p_group_id
10864 		 AND    transaction_header_id = p_txn_header_id
10865 		 AND    move_order_line_id = l_mol_line_id;
10866 
10867 	       IF l_tmp_prm_qty <= l_msni_qty_tbl(i) THEN
10868 		  --Consumed the entire MOL qty. Still need to check other MOLs
10869 		  l_rem_user_qty := l_rem_user_qty - l_tmp_prm_qty;
10870 		  l_msni_qty_tbl(i) := l_msni_qty_tbl(i) - l_tmp_prm_qty;
10871 		ELSE
10872 		  --OK. The total available quantity is greater than the confirmed quantity
10873 		  --We have to split the move order line. So populate the table
10874 		  l_mo_splt_cnt := l_mo_splt_cnt + 1;
10875 
10876 		  --MOL.qty = 10, MOL.qty_delivered = 4. remaining user qty = 3
10877 		  --So we need to split the mol and create a mew one for 10-4-3 = 3
10878 		  l_qty_to_split := l_tmp_prm_qty - l_msni_qty_tbl(i);
10879 
10880 
10881 
10882 		  --First decrement the remaining quantity
10883 		  l_rem_user_qty := l_rem_user_qty - l_msni_qty_tbl(i);
10884 		  l_msni_qty_tbl(i) := 0;
10885 
10886 		  IF (l_debug = 1) THEN
10887 		     DEBUG('complete_putaway_wrapper: Need to split the move order line id: '
10888 			   || l_mol_line_id || ' with qty: ' || l_qty_to_split, l_proc_name);
10889 		  END IF;
10890 
10891 		  l_mos_to_split_tbl(l_mo_splt_cnt).line_id := l_mol_line_id;
10892 		  l_mos_to_split_tbl(l_mo_splt_cnt).prim_qty := l_qty_to_split;
10893 
10894 	       END IF;   --END IF compare the remaining qty with available qty
10895 	    END LOOP;   --END loop through each mol for the current group
10896 
10897 	    IF l_msni_qty_tbl(i) > 0 THEN
10898 	       IF(l_debug = 1) THEN
10899 		  DEBUG('complete_putaway_wrapper: Unable to match MOL for all serial IN the group! How?',l_proc_name);
10900 	       END IF;
10901 	       RAISE fnd_api.g_exc_error;
10902 	    END IF;
10903 	 END LOOP;
10904 
10905       --MO Splits for a non-serialized item
10906       --}
10907       ELSE
10908       --{
10909         l_progress := '90';
10910 
10911         --Processing for a non-serialized item
10912         IF (l_debug = 1) THEN
10913           DEBUG('complete_putaway_wrapper: Progress: ' || l_progress ||
10914             '. Processing the MO lines for a non-serialized item', l_proc_name);
10915         END IF;
10916 
10917         OPEN mol_csr;
10918         LOOP
10919 	  FETCH mol_csr
10920 	   INTO l_mol_line_id
10921 	      , l_mol_qty
10922               , l_mol_qty_detailed
10923               , l_mol_qty_delivered
10924               , l_mol_uom_code
10925               , l_mol_sec_qty --OPM Convergence
10926               , l_mol_sec_qty_detailed --OPM Convergence
10927               , l_mol_sec_qty_delivered --OPM Convergence
10928               , l_mol_sec_uom_code; --OPM Convergence
10929 	  EXIT WHEN mol_csr%NOTFOUND;
10930 
10931           --Have consumed the entire quantity confirmed and marked the temp table records
10932           IF l_rem_user_qty = 0 THEN
10933             EXIT;
10934           END IF;
10935 
10936 	  --BUG 5075410: MOL1 has 6, MMTT1 3 LOC1 MMTT2 3 LOC2
10937 	  --             MOL2 has 3, MMTT3 3 LOC1
10938 	  --System would suggest task for 6 to LOC1, 3 to LOC2
10939 	  --If the user enter 5 for the first task, then we cannot
10940 	  --just use the 5 from MOL1.  We must use 3 from MOL1 and
10941 	  --2 from MOL2, or vice versa.  So the following check is
10942 	  --needed to find out the quantity from the MOL that is
10943 	  --suggested for the current group of tasks
10944 	  BEGIN
10945 	     SELECT  SUM(mmtt.primary_quantity),
10946                      SUM(mmtt.secondary_transaction_quantity) -- 9037915
10947 	       INTO  l_prim_qty_consumable,
10948                      l_sec_qty_consumable
10949 	       FROM  mtl_material_transactions_temp mmtt
10950 	       ,     mtl_txn_request_lines mtrl
10951 	       WHERE mmtt.transaction_header_id = p_txn_header_id
10952 	       AND   mtrl.line_id = l_mol_line_id
10953 	       AND   mtrl.line_id = mmtt.move_order_line_id;
10954 	  EXCEPTION
10955 	     WHEN OTHERS THEN
10956 		IF (l_debug = 1) THEN
10957 		   debug('Error querying total consumable qty. SQLCODE:'||SQLCODE||' Sqlerrm:'||SQLERRM,l_proc_name,1);
10958 		END IF;
10959 	  END;
10960 
10961           IF (l_debug = 1) THEN
10962             DEBUG('complete_putaway_wrapper: Working with mol: ' || l_mol_line_id ||
10963               ' qty: ' || l_mol_qty || ' qty det: ' || l_mol_qty_detailed || ' qty del: '
10964               || l_mol_qty_delivered || ' uom_code: ' || l_mol_uom_code||
10965 	      ' consumable qty'||l_prim_qty_consumable, l_proc_name);
10966           END IF;
10967 
10968           --First get the difference between the quantity and quantity delivered
10969           l_tmp_qty1 := l_mol_qty - l_mol_qty_delivered;
10970 
10971           --Convert this into primary uom
10972           IF (l_mol_uom_code <> l_primary_uom_code) THEN
10973 	     l_tmp_prm_qty := inv_rcv_cache.convert_qty
10974 	                         (p_inventory_item_id => p_item_id
10975 				  ,p_from_qty         => l_tmp_qty1
10976 				  ,p_from_uom_code    => l_mol_uom_code
10977 				  ,p_to_uom_code      => l_primary_uom_code);
10978           ELSE
10979             l_tmp_prm_qty := l_tmp_qty1;
10980           END IF;
10981 
10982           /*OPM Convergence get the difference between the secondary quantity
10983             and the secondary quantity delivered. */
10984           l_tmp_sec_qty := l_mol_sec_qty - l_mol_sec_qty_delivered;
10985 
10986           l_progress := '100';
10987 
10988           IF(l_debug = 1) THEN
10989             DEBUG('complete_putaway_wrapper: Progress: ' || l_progress ||
10990              '. Available MO quantity for this group: ' || l_tmp_prm_qty ||
10991                 ' and Secondary qty ' || l_tmp_sec_qty , l_proc_name);
10992           END IF;
10993 
10994           --Now that we are ready with quantity calculations, mark the all tasks
10995           --in the current group for this move order line
10996           UPDATE wms_putaway_group_tasks_gtmp
10997           SET    process_flag = 'Y'
10998           WHERE  group_id = p_group_id
10999           AND    transaction_header_id = p_txn_header_id
11000           AND    move_order_line_id = l_mol_line_id;
11001 
11002 	  IF l_prim_qty_consumable <= l_rem_user_qty THEN
11003             l_rem_user_qty := l_rem_user_qty - l_prim_qty_consumable;
11004 
11005 	    IF (l_prim_qty_consumable < l_tmp_prm_qty) THEN
11006 	       l_qty_to_split := l_tmp_prm_qty - l_prim_qty_consumable;
11007 	       l_sec_qty_to_split := l_tmp_sec_qty - l_sec_qty_consumable;  -- 9037915
11008 	       IF (l_debug = 1) THEN
11009 		  DEBUG('complete_putaway_wrapper: Need to split the move order line id: '
11010 			|| l_mol_line_id || ' with qty: ' || l_qty_to_split || 'with Secondary Qty ' || l_sec_qty_to_split , l_proc_name);
11011 	       END IF;
11012 
11013 	       l_mo_splt_cnt := l_mo_splt_cnt + 1;
11014 	       l_mos_to_split_tbl(l_mo_splt_cnt).line_id  := l_mol_line_id;
11015 	       l_mos_to_split_tbl(l_mo_splt_cnt).prim_qty := l_qty_to_split;
11016 	       l_mos_to_split_tbl(l_mo_splt_cnt).sec_qty  := l_sec_qty_to_split;  -- 9037915
11017 	    END IF;
11018 	  ELSE
11019             --OK. The total available quantity is greater than the confirmed quantity
11020             --We have to split the move order line. So populate the table
11021             l_mo_splt_cnt := l_mo_splt_cnt + 1;
11022 
11023             --MOL.qty = 10, MOL.qty_delivered = 4. remaining user qty = 3
11024             --So we need to split the mol and create a mew one for 10-4-3 = 3
11025             l_qty_to_split := l_tmp_prm_qty - l_rem_user_qty;
11026             l_sec_qty_to_split := l_tmp_sec_qty - p_secondary_quantity ; -- 9037915
11027 
11028 
11029             IF (l_debug = 1) THEN
11030               DEBUG('complete_putaway_wrapper: Need to split the move order line id: '
11031                 || l_mol_line_id || ' with qty: ' || l_qty_to_split, l_proc_name);
11032             END IF;
11033 
11034             l_mos_to_split_tbl(l_mo_splt_cnt).line_id := l_mol_line_id;
11035             l_mos_to_split_tbl(l_mo_splt_cnt).prim_qty := l_qty_to_split;
11036             l_mos_to_split_tbl(l_mo_splt_cnt).sec_qty := l_sec_qty_to_split; -- 9037915
11037 
11038             --Non-serialized item. Only one MO line can be split
11039             l_rem_user_qty := 0;
11040           END IF;   --END IF compare remaining qty and MOL qty
11041         END LOOP;   --END loop through each mol for the current group
11042       --}
11043       END IF;   --END IF MO processing based on serialized flag
11044 
11045       IF (l_rem_user_qty > 0) THEN
11046 	 IF (l_debug = 1) THEN
11047 	    DEBUG('complete_putaway_wrapper: l_rem_user_qty > 0. Unable to find MOL for qty.  There must be some data corruption'||l_proc_name);
11048 	 END IF;
11049 	 RAISE fnd_api.g_exc_error;
11050       END IF;
11051 
11052       --Close the mol cursor
11053       IF mol_csr%ISOPEN THEN
11054         CLOSE mol_csr;
11055       END IF;
11056 
11057       IF serial_mol_csr%ISOPEN THEN
11058 	 CLOSE serial_mol_csr;
11059       END IF;
11060 
11061       --If at all any move order lines belonging to the current group have been
11062       --identified for splits, they would have been populated in the PL/SQL table
11063       --Loop through each and call the split_mo API to split the move order lines
11064       --Pass two additional parameters that are needed to sync up the
11065       --global temporary table with the move order lines/MMTTs so that when we
11066       --actually loop through the tasks in the group, they will reflect the changes
11067       IF (l_debug = 1) THEN
11068         DEBUG('complete_putaway_wrapper: Count of move order lines that need to be split: ' || l_mo_splt_cnt, l_proc_name);
11069       END IF;
11070 
11071       l_progress := '110';
11072 
11073       IF (l_mo_splt_cnt > 0) THEN
11074         FOR i IN 1 .. l_mo_splt_cnt LOOP
11075           l_qty_to_split := l_mos_to_split_tbl(i).prim_qty;
11076           l_sec_qty_to_split := l_mos_to_split_tbl(i).sec_qty; -- 9037915
11077 
11078           l_mo_splt_tb(1).prim_qty := l_qty_to_split;
11079           l_mo_splt_tb(1).sec_qty  := l_sec_qty_to_split ; -- 9037915
11080 
11081           --Call the Split_MO API
11082           IF (l_debug = 1) THEN
11083             debug('complete_putaway_wrapper: Progress: ' || l_progress ||
11084               '. Calling the split_mo API with primary qty: ' || l_qty_to_split ||
11085               ' and Secondary qty :' || l_sec_qty_to_split , 4);
11086           END IF;
11087 
11088           inv_rcv_integration_apis.split_mo(
11089               p_orig_mol_id           =>  l_mos_to_split_tbl(i).line_id
11090             , p_mo_splt_tb            =>  l_mo_splt_tb
11091             , p_updt_putaway_temp_tbl =>  FND_API.G_TRUE
11092             , p_txn_header_id         =>  p_txn_header_id
11093             , x_return_status         =>  x_return_status
11094             , x_msg_count             =>  x_msg_count
11095             , x_msg_data              =>  x_msg_data);
11096 
11097           IF (l_debug = 1) THEN
11098             DEBUG('complete_putaway_wrapper: split_mo API return status: ' || x_return_status, 4);
11099           END IF;
11100 
11101           IF x_return_status = fnd_api.g_ret_sts_error THEN
11102             RAISE fnd_api.g_exc_error;
11103           END IF;
11104 
11105           IF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
11106             RAISE fnd_api.g_exc_unexpected_error;
11107           END IF;
11108 
11109           IF (l_debug = 1) THEN
11110             DEBUG('complete_putaway_wrapper: new MOL ID: ' || l_mo_splt_tb(1).line_id, 4);
11111           END IF;
11112 
11113 	  --Bug 5075410: Cannot cleanup here because say 1 MOL
11114 	  --have two MMTT, then complete 1 of the task would cause
11115 	  --cleanup of the other MMTT, and transaction will fail
11116 	  --when user tries to complete the second task
11117         END LOOP;   --END Loop through each MOL identified for split
11118       END IF;   --END IF l_mo_splt_cnt > 0
11119     -- bug 5333789
11120     --}
11121     ELSE
11122     --{
11123         l_serialized_item := FALSE;
11124     --}
11125     -- bug 5333789
11126     END IF;    --END IF p_drop_type = G_DT_ITEM_DROP AND l_disc = 'Y'
11127 
11128     --Now that the MO splits are done, we have the temp table in sync and we
11129     --know the rows that we need to process.
11130 
11131     l_progress := '120';
11132 
11133     --Get the lpn_controlled_flag and subinventory_type attributes for the
11134     --destination subinventory
11135     BEGIN
11136       SELECT NVL(lpn_controlled_flag, 1)
11137            , NVL(subinventory_type, 1)
11138       INTO   l_lpn_controlled_flag
11139            , l_drop_sub_type
11140       FROM   mtl_secondary_inventories
11141       WHERE  organization_id = p_organization_id
11142       AND    secondary_inventory_name = p_subinventory_code;
11143     EXCEPTION
11144       WHEN OTHERS THEN
11145         l_lpn_controlled_flag := 2;
11146     END;
11147 
11148     IF (p_lpn_context IN (G_LPN_CONTEXT_INV, G_LPN_CONTEXT_WIP)) THEN
11149 
11150       SELECT mtl_material_transactions_s.NEXTVAL
11151       INTO   l_new_txn_header_id
11152       FROM   DUAL;
11153 
11154       --For an item drop of an Inventory/WIP LPN with quantity
11155       --discrepancy/partial lpn drop,
11156       --we would have marked the serials in UI. Have to first fetch all such
11157       --serials and then consume these for each task on a FIFO basis.
11158       --Basically, no matching is needed unlike receiving LPNs
11159        IF ((p_drop_type = G_DT_ITEM_DROP) AND (l_disc = 'Y' OR p_entire_lpn = 'N' OR (p_lpn_id=p_to_lpn_id and l_disc = 'N' and p_entire_lpn = 'Y')) AND (l_serial_control_code IN (2,5))) THEN--bug12399417
11160 
11161         l_progress := '130';
11162 
11163 	IF (l_debug = 1) THEN
11164 	   DEBUG('complete_putaway_wrapper: Progress: ' ||l_progress ||'. Process Serials', l_proc_name);
11165 	END IF;
11166 
11167         OPEN msnt_ser_csr(p_lpn_id, p_item_id);
11168         FETCH msnt_ser_csr
11169         BULK COLLECT INTO l_ser_num_tbl, l_ser_mark_tbl;
11170 
11171         IF (l_debug = 1) THEN
11172           DEBUG('complete_putaway_wrapper: Progress: ' || l_progress || '. Serial item partial qty. '
11173             || l_ser_num_tbl.COUNT || ' serials confirmed in UI', l_proc_name);
11174         END IF;
11175       END IF;
11176     ELSE
11177       --For a reveiving LPN, setting l_new_txn_header_id to the parameter.
11178       --This is not actually needed since for receiving LPNs, a new txn_hdr_id
11179       --would always be created for each MMTT we process.
11180       l_new_txn_header_id := p_txn_header_id;
11181 
11182       --R12: Open the rcv_serials_csr here.  It will be used later
11183       --when we try to match serials with the MMTT quantitty
11184       IF (p_drop_type = g_dt_item_drop AND
11185 	  l_disc = 'Y'AND
11186 	  ((l_serial_control_code > 1  AND l_serial_control_code <> 6) OR
11187 	   (l_serial_control_code = 6 AND p_reference = 'ORDER_LINE_ID') OR
11188 	   (l_serial_control_code = 6 AND p_reference = 'SHIPMENT_LINE_ID' AND l_lpn_serials_cnt > 0))) THEN
11189 
11190 	 OPEN rcv_serials_csr;
11191 	 FETCH rcv_serials_csr
11192 	 BULK COLLECT INTO l_ser_num_tbl, l_ser_mark_tbl,l_ser_inspect_status_tbl,l_ser_lot_num_tbl;
11193 
11194 	 DELETE FROM mtl_serial_numbers_interface
11195 	   WHERE transaction_interface_id = p_msni_txn_interface_id;
11196 
11197 	 IF (l_debug = 1) THEN
11198 	    debug('Number of MSNI deleted: '||SQL%rowcount,l_proc_name,9);
11199 	 END IF;
11200       END IF;
11201     END IF;   --END IF Inventory/WIP LPNs
11202 
11203     IF (msnt_ser_csr%ISOPEN) THEN
11204       CLOSE msnt_ser_csr;
11205     END IF;
11206 
11207     --Nested LPN changes
11208     pre_process_lpn(
11209           x_return_status           => x_return_status
11210         , x_msg_count               => x_msg_count
11211         , x_msg_data                => x_msg_data
11212         , p_from_lpn_id             => p_lpn_id
11213         , p_organization_id         => p_organization_id
11214         , p_subinventory_code       => p_subinventory_code
11215         , p_lpn_mode                => l_lpn_mode
11216         , p_locator_id              => p_locator_id
11217         , p_to_lpn_id               => p_to_lpn_id
11218         , p_project_id              => p_project_id
11219         , p_task_id                 => p_task_id
11220         , p_user_id                 => p_user_id
11221         , p_lpn_context             => p_lpn_context
11222         , p_batch_id                => l_new_txn_header_id
11223         , p_batch_seq               => 1
11224         , p_pack_trans_id           => l_new_txn_header_id
11225         , x_batch_seq               => l_batch_seq ); --BUG 3544918
11226     IF (l_debug = 1) THEN
11227       DEBUG('complete_putaway_wrapper: Progress: ' || l_progress || '. pre_process_lpn returns: ' || x_return_status, 4);
11228     END IF;
11229 
11230     IF x_return_status = fnd_api.g_ret_sts_error THEN
11231       RAISE fnd_api.g_exc_error;
11232     END IF;
11233 
11234     IF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
11235       RAISE fnd_api.g_exc_unexpected_error;
11236     END IF;
11237     -- Nested LPN changes end;
11238 
11239     --We should be calling complete_putaway for the eligible rows
11240     --Based on the drop type, open the appropriate cursors
11241     IF (p_drop_type IN (G_DT_CONSOLIDATED_DROP, G_DT_ITEM_DROP)) THEN
11242       OPEN all_tasks_csr(l_disc);
11243     ELSIF (p_drop_type IN (G_DT_USER_DROP, G_DT_MANUAL_DROP)) THEN
11244       IF (l_debug = 1) THEN
11245         DEBUG('complete_putaway_wrapper: Manual Drop case. Opening the md_tasks_csr to fetch the tasks', l_proc_name);
11246       END IF;
11247       OPEN md_tasks_csr;
11248     END IF;
11249 
11250     --Fetch all the tasks for the current group being putaway
11251     --LOOP
11252     IF (p_drop_type IN (G_DT_CONSOLIDATED_DROP, G_DT_ITEM_DROP)) THEN
11253       l_progress := 140;
11254       IF (l_debug = 1) THEN
11255         DEBUG('complete_putaway_wrapper: Fetching the all_tasks_csr...', l_proc_name);
11256       END IF;
11257       FETCH all_tasks_csr
11258       BULK COLLECT INTO
11259           l_temp_id_tbl
11260         , l_mol_lpn_id_tbl
11261         , l_mol_line_id_tbl
11262         , l_mmtt_item_id_tbl
11263         , l_mmtt_rev_tbl
11264         , l_mmtt_lot_tbl
11265         , l_mmtt_qty_tbl
11266         , l_mmtt_uom_tbl
11267         , l_parent_txn_id_tbl
11268         , l_bk_del_det_tbl
11269         , l_xdock_type_tbl
11270         , l_wip_sup_typ_tbl
11271         , l_mmtt_sec_qty_tbl
11272         , l_mmtt_sec_uom_tbl -- 9037915
11273         , l_inspection_status_tbl
11274         , l_primary_uom_code_tbl;
11275       --EXIT WHEN all_tasks_csr%NOTFOUND;
11276     ELSIF (p_drop_type IN (G_DT_USER_DROP, G_DT_MANUAL_DROP)) THEN
11277       l_progress := '150';
11278       IF (l_debug = 1) THEN
11279         DEBUG('complete_putaway_wrapper: Fetching the md_tasks_csr...', l_proc_name);
11280       END IF;
11281       FETCH md_tasks_csr
11282       BULK COLLECT INTO
11283           l_temp_id_tbl
11284         , l_mol_lpn_id_tbl
11285         , l_mol_line_id_tbl
11286         , l_mmtt_item_id_tbl
11287         , l_mmtt_rev_tbl
11288         , l_mmtt_lot_tbl
11289         , l_mmtt_qty_tbl
11290         , l_mmtt_uom_tbl
11291         , l_parent_txn_id_tbl
11292         , l_bk_del_det_tbl
11293         , l_xdock_type_tbl
11294         , l_wip_sup_typ_tbl
11295         , l_mmtt_sec_qty_tbl
11296         , l_mmtt_sec_uom_tbl -- 9037915
11297 	, l_inspection_status_tbl
11298         , l_primary_uom_code_tbl;
11299       --EXIT WHEN md_tasks_csr%NOTFOUND;
11300     END IF;
11301 
11302     --At this stage, I know the list of 'All Task' rows for the current group.
11303     --Will be fetching the details of each task, do the calculations and call
11304     --the complete_putaway for the current task
11305     IF (l_debug = 1) THEN
11306       DEBUG('complete_putaway_wrapper: Progress: ' || l_progress || 'No. of tasks for this group: ' || l_temp_id_tbl.COUNT, l_proc_name);
11307     END IF;
11308 
11309     IF (l_temp_id_tbl.COUNT = 0) THEN
11310       fnd_message.set_name('WMS', 'WMS_TASK_ERROR');
11311       fnd_msg_pub.ADD;
11312     END IF;
11313 
11314     IF (p_drop_type = G_DT_ITEM_DROP) THEN
11315       --First get the total drop quantity in transaction uom
11316       l_remaining_qty := p_quantity;
11317       l_remaining_sec_qty := p_secondary_quantity;
11318     END IF;
11319 
11320     FOR i IN 1 .. l_temp_id_tbl.COUNT
11321     LOOP
11322 
11323       l_mmtt_temp_id           := l_temp_id_tbl(i);
11324       l_item_id                := l_mmtt_item_id_tbl(i);
11325       l_product_transaction_id := p_product_transaction_id;
11326       --Initialize the count of serials to be consumed for the current task
11327       l_cur_task_ser_count := 0;
11328       l_counter:=0;--bug12399417
11329 
11330       --After inspecting a serialized item, there is one MO line for each serial inspected.
11331       --If the user confirms partial quantity, then we might be fetching a move order line
11332       --whose parent RT record might not correspond to the serial confirmed on the UI.
11333       -- eg: MO1   RT1   1 Ea  SN1, MO2   RT2  1 Ea  SN2, MO3   RT3   1 Ea SN3
11334       --If the user confirms the serials SN1 and SN3 on the UI, we should create deliver
11335       --RTIs only for RT1 and RT3. But we might have MMTT corresponding to MO2 fetched by
11336       --the tasks cursor. In which case, we would not be able to match the serials with the
11337       --parent transaction. So we should not call the complete_putaway API for this MMTT.
11338       --For this purpose have created the flag below which would always be FALSE
11339       --and will be set only if no serials can be matched for the current move order line
11340       l_skip_mmtt := FALSE;
11341 
11342       l_progress := '160';
11343       IF (l_debug = 1) THEN
11344         DEBUG('complete_putaway_wrapper: Progress: ' || l_progress || '. Am working on the task with temp_id: ' || l_mmtt_temp_id, l_proc_name);
11345       END IF;
11346 
11347       --Process the current task based on the drop type
11348       IF (p_drop_type = G_DT_ITEM_DROP) THEN
11349 
11350         --If I have consumed the entire drop quantity confirmed by the user
11351         --should be coming out of the processing loop
11352         IF (l_remaining_qty = 0) THEN
11353           IF (l_debug = 1) THEN
11354             DEBUG('complete_putaway_wrapper: Have consumed entire quantity confirmed by the user. No further processing', l_proc_name);
11355           END IF;
11356           EXIT;
11357         END IF;
11358 
11359         IF (l_debug =1) THEN
11360           DEBUG('complete_putaway_wrapper: Qty still to drop: ' || l_remaining_qty, l_proc_name);
11361         END IF;
11362 
11363         --Convert the current task quantity into the user confirmed UOM code
11364         IF (l_mmtt_uom_tbl(i) <> p_uom_code) THEN
11365 	   l_quantity := inv_rcv_cache.convert_qty
11366 	                   (p_inventory_item_id => l_mmtt_item_id_tbl(i)
11367 			    ,p_from_qty         => l_mmtt_qty_tbl(i)
11368 			    ,p_from_uom_code    => l_mmtt_uom_tbl(i)
11369 			    ,p_to_uom_code      => p_uom_code);
11370         ELSE
11371           l_quantity := l_mmtt_qty_tbl(i);
11372         END IF;
11373 
11374         IF (l_debug = 1) THEN
11375           DEBUG('Item Drop - Current Task quantity: ' || l_quantity, l_proc_name);
11376         END IF;
11377 
11378         l_progress := '170';
11379 
11380         IF (l_disc = 'Y' OR p_entire_lpn = 'N') THEN
11381           SELECT mtl_material_transactions_s.NEXTVAL
11382           INTO l_dummy_temp_id
11383           FROM dual;
11384         END IF;
11385 
11386         IF (l_quantity <= l_remaining_qty) THEN
11387           l_remaining_qty := l_remaining_qty - l_quantity;
11388         ELSE  --Is this possible?
11389           l_remaining_qty := l_quantity;
11390         END IF;
11391 
11392         --Set the rev, lot, sub and loc from the values confirmed in the UI
11393         l_lpn_id            := l_mol_lpn_id_tbl(i);
11394         l_entire_lpn        := p_entire_lpn;
11395         l_revision          := p_revision;
11396         l_lot_number        := p_lot_number;
11397         l_uom_code          := p_uom_code;
11398         l_subinventory_code := p_subinventory_code;
11399         l_locator_id        := p_locator_id;
11400 
11401         --If user scans the same LPN in case of quantity discrepancy, we need
11402         --to stamp the transfer_lpn_id and not content_lpn_id. This will
11403         --be done through the p_entire_lpn_flag. Reference - Bug #2310097
11404         IF (l_disc = 'Y' and l_entire_lpn = 'Y') THEN
11405           l_entire_lpn := 'N';
11406         END IF;
11407 
11408         --In case of a quantity discrepancy for a lot/serialized items,
11409         --have to create the interface records for lots and serials from
11410         --here itself and we would not be doing it from complete_putaway
11411         --More importantly, do it only if there is a quantity discrepancy
11412 	--Also create interface record if entire_lpn is 'N'
11413         IF ((l_disc = 'Y' OR p_entire_lpn = 'N')
11414     OR (p_lpn_id=p_to_lpn_id and l_disc = 'N' AND p_entire_lpn = 'Y' AND p_lpn_context <> 3 AND (l_serial_control_code > 1  AND l_serial_control_code <> 6)))THEN --bug12399417
11415          --Convert the task quantity into primary uom
11416           IF (l_mmtt_uom_tbl(i) <> l_primary_uom_code) THEN
11417 	     l_mmtt_prm_qty := inv_rcv_cache.convert_qty
11418 	                          (p_inventory_item_id => l_mmtt_item_id_tbl(i)
11419 				   ,p_from_qty         => l_mmtt_qty_tbl(i)
11420 				   ,p_from_uom_code    => l_mmtt_uom_tbl(i)
11421 				   ,p_to_uom_code      => l_primary_uom_code);
11422           ELSE
11423             l_mmtt_prm_qty := l_mmtt_qty_tbl(i);
11424           END IF;   --END IF UOM conversions for lot quantity
11425           l_mmtt_sec_qty := l_mmtt_sec_qty_tbl(i); --OPM Convergence       -- 9037915
11426           l_secondary_uom := l_mmtt_sec_uom_tbl(i);                        -- 9037915
11427 
11428           IF (p_lpn_context = G_LPN_CONTEXT_RCV) THEN
11429             --On inspecting a serialized item, one move order line is created for each
11430             --serial number. When the user confirms the serials on the UI, we generate
11431             --one single product_transaction_id for all such serials. However, when we
11432             --create the RTI records for these we have to associate only that serial
11433             --number corresponding to the parent transaction. So in such cases, we have to
11434             --generate a new value of product_transaction_id while updating the MSNI
11435             --ramarava
11436 			--Commenting the below portion coz as a part of change in 10262461 we decided to ask for Serial Validation even for qty 1
11437 			--This was done as in cases of item based putaway of serial items putaway was failing due to mismatched l_product_transaction_id value.
11438             /*IF (l_serialized_item AND l_mmtt_prm_qty = 1) THEN
11439               SELECT  rcv_transactions_interface_s.NEXTVAL
11440               INTO    l_product_transaction_id
11441               FROM    sys.dual;
11442             END IF;
11443             */
11444             --Insert MSNI for receiving LPN
11445             IF (l_lot_control_code > 1 AND p_lot_number IS NOT NULL) THEN
11446               l_progress := '180';
11447               IF (l_debug =1) THEN
11448                 DEBUG('complete_putaway_wrapper: Progress: ' || l_progress ||
11449                       ' Have to create MTLIs for prm qty ' || l_mmtt_prm_qty, l_proc_name);
11450               END IF;
11451 
11452               --Get the lot expiration date and lot status
11453               SELECT  expiration_date
11454                     , status_id
11455               INTO    l_lot_expiration_date
11456                     , l_lot_status_id
11457               FROM    mtl_lot_numbers
11458               WHERE   lot_number = p_lot_number
11459               AND     inventory_item_id = p_item_id
11460               AND     organization_id = p_organization_id;
11461 
11462               --Call the insert_mtli API to insert the lot_number in MTLI
11463               inv_rcv_integration_apis.insert_mtli(
11464                   p_api_version                 =>  1.0
11465                 , p_init_msg_lst                =>  FND_API.G_FALSE
11466                 , x_return_status               =>  x_return_status
11467                 , x_msg_count                   =>  x_msg_count
11468                 , x_msg_data                    =>  x_msg_data
11469                 , p_transaction_interface_id    =>  l_dummy_temp_id --l_mmtt_temp_id
11470                 , p_lot_number                  =>  p_lot_number
11471                 , p_transaction_quantity        =>  l_mmtt_qty_tbl(i)
11472                 , p_primary_quantity            =>  l_mmtt_prm_qty
11473                 , p_organization_id             =>  p_organization_id
11474                 , p_inventory_item_id           =>  p_item_id
11475                 , p_expiration_date             =>  l_lot_expiration_date
11476                 , p_status_id                   =>  l_lot_status_id
11477                 , x_serial_transaction_temp_id  =>  l_serial_temp_id
11478                 , p_product_transaction_id      =>  l_product_transaction_id
11479                 , p_product_code                =>  'RCV'
11480                 , p_att_exist                   =>  'Y'
11481                 , p_update_mln                  =>  'N'
11482                 , p_secondary_quantity          =>  l_mmtt_sec_qty); --OPM CONVERGENCE
11483 
11484                 l_secondary_quantity := l_mmtt_sec_qty ; --OPM Convergence -- 9037915
11485 
11486               IF x_return_status = FND_API.G_RET_STS_ERROR THEN
11487                 IF (l_debug = 1) THEN
11488                   DEBUG('complete_putaway_wrapper: Error at progress: ' || l_progress ||
11489                         ' insert_mtli returns g_exc_error', l_proc_name, 1);
11490                 END IF;
11491                 RAISE FND_API.G_EXC_ERROR;
11492               END IF;
11493 
11494               IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
11495                 IF (l_debug = 1) THEN
11496                   DEBUG('complete_putaway_wrapper: Error at progress: ' || l_progress ||
11497                         ' insert_mtli returns g_unexp_error', l_proc_name, 1);
11498                 END IF;
11499                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
11500               END IF;
11501 
11502               IF (l_debug = 1) THEN
11503                 DEBUG('complete_putaway_wrapper: Inserted MTLI intf_txn_id: ' || l_transaction_interface_id ||
11504                       ', ser_temp_id : ' || l_serial_temp_id || ' , prod_txn_id: ' || l_product_transaction_id);
11505               END IF;
11506             END IF;   --END Insert MTLI for receiving LPN
11507 
11508             --Match serial numbers for receiving LPN
11509             IF ((l_serial_control_code > 1  AND l_serial_control_code <> 6) OR
11510                 (l_serial_control_code = 6 AND p_reference = 'ORDER_LINE_ID') OR
11511                 (l_serial_control_code = 6 AND p_reference = 'SHIPMENT_LINE_ID' AND
11512                  l_lpn_serials_cnt > 0)) THEN
11513 
11514               l_progress := '190';
11515               IF (l_debug =1) THEN
11516                 DEBUG('complete_putaway_wrapper: Progress: ' || l_progress ||
11517                       ' Should match serials for the parent txn ' || l_parent_txn_id_tbl(i), l_proc_name);
11518               END IF;
11519 
11520               FOR j in 1 .. l_ser_num_tbl.COUNT LOOP
11521 
11522 		 IF ((l_ser_mark_tbl(j) = 'Y') OR
11523 		     (Nvl(l_ser_inspect_status_tbl(j),-1) <> Nvl(l_inspection_status_tbl(i),-1)) OR
11524 		     (Nvl(l_ser_lot_num_tbl(j),'&*_') <> Nvl(l_lot_number,'&*_'))) THEN
11525 		    NULL;
11526 		  ELSE
11527 		    --The MSNI records would have been created with a dummy interface_transaction_id.
11528 		    --On matching the serial, we need to update the transaction_interface_id
11529 		    --with the transaction_temp_id (for a serial and non-lot controlled item)
11530 		    --or with the serial_transaction_temp_id of the MTLI (for lot and serial controlled item)
11531 		    l_cur_ser_number := l_ser_num_tbl(j);
11532 		    DEBUG('complete_putaway_wrapper: cur_serial_number: ' || l_cur_ser_number);
11533 
11534 		    IF (l_lot_control_code > 1) THEN
11535 		       l_ser_intf_id := l_serial_temp_id;
11536 		     ELSE
11537 		       l_ser_intf_id := l_dummy_temp_id;--l_mmtt_temp_id;
11538 		    END IF;
11539 
11540 		    -- INSERT MSNI HERE
11541 		    l_result := wms_task_dispatch_put_away.insert_msni_helper
11542 		                         (p_txn_if_id         =>  l_ser_intf_id
11543 					  , p_serial_number   =>  l_cur_ser_number
11544 					  , p_org_id          =>  p_organization_id
11545 					  , p_item_id         =>  p_item_id
11546 					  , p_product_txn_id  =>  l_product_transaction_id
11547 					  );
11548 
11549 		    IF NOT l_result THEN
11550 		       IF (l_debug = 1) THEN
11551 			  debug('Failure while Inserting MSNI records - lot and serial controlled item',l_proc_name);
11552 		       END IF;
11553 		       RAISE fnd_api.g_exc_unexpected_error;
11554 		    END IF; -- END IF check l_result
11555 
11556 		    l_cur_task_ser_count := l_cur_task_ser_count + 1;
11557 		    l_ser_mark_tbl(j) := 'Y';
11558 
11559 		    IF (l_cur_task_ser_count = l_mmtt_prm_qty) THEN
11560 		       EXIT;
11561 		    END IF;
11562 
11563 		 END IF;
11564               END LOOP;   --END LOOP through serials for the parent transaction
11565 
11566 	      --On matching all the serials for the current task, exit out the loop
11567 	      IF (l_cur_task_ser_count = l_mmtt_prm_qty) THEN
11568 		 l_skip_mmtt := FALSE; -- Bug 3495726 issue 33
11569 	       ELSE --l_cur_task_ser_count < l_mmtt_prm_qty
11570 		 IF (l_debug = 1) THEN
11571 		    DEBUG('complete_putaway_wrapper: l_cur_task_ser_count < l_mmtt_prm_qty.  how???',l_proc_name);
11572 		 END IF;
11573 	      END IF;   --END IF check if all serials are consumed
11574 
11575 	      --We have now consumed all the serials for the current task after
11576               --matching them with the serials entered.
11577               --Compare the serials consumed with the task quantity in primary uom
11578               --In case of a mismatch, throw error
11579               l_progress := '200';
11580               IF (l_debug = 1) THEN
11581                 DEBUG('complete_putaway_wrapper: Progress: ' || l_progress || '. Have matched ' ||
11582                        l_cur_task_ser_count || ' as against the task qty: ' || l_mmtt_prm_qty);
11583               END IF;
11584               IF (l_cur_task_ser_count <> l_mmtt_prm_qty) THEN
11585                 NULL; --Do I throw the serial mismatch error here itself
11586               END IF;
11587             END IF;   --END IF Match serial numbers for receiving LPN
11588           --LPN context is Inventory/WIP
11589           ELSE
11590             --Create MSNT records for the serials confirmed from the UI
11591             l_progress := '210';
11592             IF (l_debug = 1) THEN
11593               DEBUG('complete_putaway_wrapper: Progress: ' || l_progress ||
11594                 '. INV/WIP LPN. Count of serials confirmed in the UI: ' || l_ser_num_tbl.COUNT, l_proc_name);
11595             END IF;
11596 
11597             FOR k IN 1 .. l_ser_num_tbl.COUNT LOOP
11598               --Process the serial only if the serial has not already been
11599               --marked
11600               IF l_ser_mark_tbl(k) <> 'Y' THEN
11601 
11602                 --For a serialized item, the user can enter the quantity
11603                 --ONLY in the item's primary UOM
11604                 IF l_remaining_qty = 0 THEN
11605 		  l_ser_qty := l_mmtt_prm_qty;
11606                 ELSIF l_remaining_qty < l_mmtt_prm_qty THEN
11607                   l_ser_qty := l_remaining_qty;
11608                 ELSE
11609                   l_ser_qty := l_mmtt_prm_qty;
11610                 END IF;
11611 
11612                 l_cur_ser_number := l_ser_num_tbl(k);
11613 
11614                 IF (l_lot_control_code > 1) THEN
11615                   l_action := 2;
11616                 ELSE
11617                   l_action := 3;
11618                 END IF;
11619 
11620                 --Call the API to create the MSNT records for the current serial number
11621                 wms_task_dispatch_gen.process_lot_serial(
11622                     p_org_id        =>  p_organization_id
11623                   , p_user_id       =>  p_user_id
11624                   , p_temp_id       =>  l_mmtt_temp_id
11625                   , p_item_id       =>  p_item_id
11626                   , p_qty           =>  l_ser_qty
11627                   , p_uom           =>  p_uom_code
11628                   , p_lot           =>  p_lot_number
11629                   , p_fm_serial     =>  l_cur_ser_number
11630                   , p_to_serial     =>  l_cur_ser_number
11631                   , p_action        =>  l_action
11632                   , x_return_status =>  x_return_status
11633                   , x_msg_count     =>  x_msg_count
11634                   , x_msg_data      =>  x_msg_data);
11635 
11636                 IF x_return_status = FND_API.G_RET_STS_ERROR THEN
11637                   IF (l_debug = 1) THEN
11638                     DEBUG('complete_putaway_wrapper: Error at progress: ' || l_progress ||
11639                       ' process_lot_serial API returns g_exc_error', l_proc_name, 1);
11640                   END IF;
11641                   RAISE FND_API.G_EXC_ERROR;
11642                 END IF;
11643 
11644                 IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
11645                   IF (l_debug = 1) THEN
11646                     DEBUG('complete_putaway_wrapper: Error at progress: ' || l_progress ||
11647                       ' process_lot_serial API returns g_unexp_error', l_proc_name, 1);
11648                   END IF;
11649                   RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
11650                 END IF;
11651 
11652                 --Mark the serial as processed so that it does not get picked up again
11653                 l_ser_mark_tbl(k) := 'Y';
11654                 /* bug12399417 */
11655 		l_counter:=l_counter+1;
11656 
11657 		IF (l_counter=l_mmtt_prm_qty) THEN
11658 		   EXIT;
11659 		END IF;
11660 	        /* bug12399417 */
11661               END IF;   --END IF l_ser_mark_tbl(k) <> 'Y' THEN
11662             END LOOP;   --END Loop through all serials confirmed in the UI
11663           END IF;   --END Do the lot and serial processing based on LPN context
11664 
11665         --Item based Drop, with no quantity discrepancy
11666         ELSE
11667           l_progress := '210';
11668           IF (l_debug = 1) THEN
11669             DEBUG('complete_putaway_wrapper: Progress: ' || l_progress ||
11670               '. Item based drop but no quantity discrepancy', l_proc_name);
11671           END IF;
11672           l_secondary_quantity := l_mmtt_sec_qty_tbl(i); --OPM Convergence -- 9037915
11673           l_secondary_uom := l_mmtt_sec_uom_tbl(i);                        -- 9037915
11674         END IF;   --END IF (l_disc = 'Y')
11675 
11676         --BUG 5208888
11677         IF (l_serialized_item AND p_lpn_context = 3) THEN
11678           IF (l_entire_lpn = 'Y' OR (NVL(p_to_lpn_id ,0) = 0 AND NVL(p_msni_txn_interface_id,0) = 0)) THEN           --bug 10404169 added p_to_lpn_id, --bug 12675494 added  p_msni_txn_interface_id
11679 		  -- Bug9318984 Modified the condition. OR NVL(l_lpn_controlled_flag,2) = 2
11680           -- Removed the change for above bug as a part of fix done in 10262461.
11681 		  -- Based on considering all scenarios like item based and LPN based drop in both LPN and non LPN controlled sub
11682 		  -- or for cases where locator capacity causes split etc. we decided to asked for serial confirmation and in non LPN controlled sub as well in some cases
11683 		  If (l_debug = 1) THEN
11684               DEBUG('Serials not entered from UI. Setting DISC flag to N so complete_putaway will insert the interface records',l_proc_name);
11685             END IF;
11686             l_disc := 'N';
11687 			l_skip_mmtt := FALSE;        --bug 10404169
11688           ELSE
11689             If (l_debug = 1) THEN
11690               DEBUG('Serials entered from UI. Setting DISC flag to Y so that complete_putaway will not insert the interface records',l_proc_name);
11691             END IF;
11692             l_disc := 'Y';
11693           END IF;
11694         END IF;
11695 
11696       --For other drop types (Consolidated Drop/Manual Drop/User Drop)
11697       ELSE
11698         l_progress := '220';
11699         IF (l_debug = 1) THEN
11700             DEBUG('complete_putaway_wrapper: Progress: ' || l_progress ||
11701               '. Drop type is CD/UD/MD. Consume MMTT directly', l_proc_name);
11702         END IF;
11703 
11704         --As the user does not confirm item, quantity, rev etc. we just need
11705         --read them from the MMTT and pass it on to complete_putaway. The
11706         --subinventory and locator must be set from the UI though
11707         l_lpn_id            := l_mol_lpn_id_tbl(i);
11708         l_quantity          := l_mmtt_qty_tbl(i);
11709         l_uom_code          := l_mmtt_uom_tbl(i);
11710         l_revision          := l_mmtt_rev_tbl(i);
11711         l_lot_number        := l_mmtt_lot_tbl(i);
11712         l_subinventory_code := p_subinventory_code;
11713         l_locator_id        := p_locator_id;
11714 
11715         l_secondary_quantity := l_mmtt_sec_qty_tbl(i); --OPM Convergence
11716         -- l_secondary_uom := p_secondary_uom;    --  9037915
11717         l_secondary_uom := l_mmtt_sec_uom_tbl(i); --  9037915
11718 
11719         If (l_debug = 1) THEN
11720           DEBUG('CD/MD : Current Task quantity: ' || l_quantity, l_proc_name);
11721         END IF;
11722 
11723         --Setting the p_entire_lpn_flag for complete_putaway
11724         --If there is only one MMTT for the current mol, then p_entire_flag
11725         --should be 'N' else it should be 'Y'
11726         SELECT count(1)
11727         INTO   l_mmtt_count
11728         FROM   mtl_material_transactions_temp
11729         WHERE  move_order_line_id = l_mol_line_id_tbl(i);
11730 
11731         IF l_mmtt_count > 1 THEN
11732           l_entire_lpn := 'N';
11733         ELSE
11734           l_entire_lpn := 'Y';
11735         END IF;
11736       END IF;   --END IF Process the current task based on the drop type
11737 
11738       --Do this stuff if the flag to skip the current task is not set
11739       IF l_skip_mmtt = FALSE THEN
11740 
11741         l_xdock_to_wip := FALSE;
11742         --Set the flag to check if the current task is crossdocked to a
11743         --WIP Issue job since for this case, we have to drop tht material as loose
11744         --So even if I have an entire nested LPN being crossdocked to a WIP issue
11745         --job, at the end of the drop, the entire nesting structure must be broken
11746         --Set this flag only if the destination sub is a storage sub since the user
11747         --might be performing a manual drop of a crossdocked line to receiving sub
11748         IF ((l_bk_del_det_tbl(i) IS NOT NULL) AND
11749             (NVL(l_xdock_type_tbl(i), 0) = 2) AND
11750             (NVL(l_wip_sup_typ_tbl(i),0) IN (1, 5)) AND
11751             (l_drop_sub_type = 1)) THEN
11752           l_xdock_to_wip := TRUE;
11753         END IF;
11754 
11755 
11756         /* Setting the p_to_lpn_id parameter for complete_putaway
11757          * If the drop sub is not LPN controlled, always pass to_lpn as NULL
11758          * Elsif the task is crossdocked to WIP issue job, pass to_lpn as NULL
11759          * Else
11760          *  If we are transacting the immediate contents of the from LPN Then
11761          *   IF Drop Mode =  Transfer contents then
11762          *     pass p_to_lpn_name (from parameter) to complete_putaway
11763          *   ELSE if Drop Mode = Drop Entire LPN then
11764          *     pass lpn_name of p_lpn_id itself (since we would create a new
11765          *       pack transaction for packing the from LPN into the "Into LPN")
11766          *   END IF;
11767          *  Else
11768          *   Read the LPN name and pass it to the complete_putaway_wrapper
11769          *  End If;
11770          * End IF;
11771          */
11772         IF l_lpn_controlled_flag = 2 THEN
11773           l_to_lpn_name := NULL;
11774         ELSIF l_xdock_to_wip = TRUE THEN
11775           l_to_lpn_name := NULL;
11776         ELSE
11777           IF ((l_mol_lpn_id_tbl(i) = p_lpn_id) AND (l_lpn_mode <> 2))  THEN
11778             l_to_lpn_name := p_to_lpn_name;
11779           ELSE
11780             --Replace this with LPN_NAME from global_temp table for this task
11781             SELECT license_plate_number
11782             INTO   l_to_lpn_name
11783             FROM   wms_license_plate_numbers
11784             WHERE  lpn_id = l_mol_lpn_id_tbl(i);
11785           END IF;   --END IF check MOL LPN and confirmed LPN
11786         END IF;   --END IF check lpn_controlled_flag for the sub
11787 
11788         --For INV/WIP LPNs, we would be calling the Inventory TM for the current
11789         --group. So, should update the MMTT with the new txn_header_id
11790         IF (p_lpn_context <> G_LPN_CONTEXT_RCV) THEN
11791           UPDATE  mtl_material_transactions_temp
11792           SET     transaction_header_id = l_new_txn_header_id
11793                 , transaction_batch_id  = l_new_txn_header_id
11794                 , transaction_batch_seq = l_batch_seq --BUG 3306988
11795           WHERE   transaction_temp_id = l_mmtt_temp_id;
11796   	      l_batch_seq := l_batch_seq + 1;
11797         END IF;
11798 
11799 	--Begin DBI FIX
11800 	--Need to update the drop_off_time on WDT for all the tasks
11801 	l_progress := '228';
11802 
11803 	BEGIN
11804 	   UPDATE wms_dispatched_tasks
11805 	     SET drop_off_time = l_drop_off_time
11806 	     WHERE transaction_temp_id = l_mmtt_temp_id;
11807 	EXCEPTION
11808 	   WHEN OTHERS THEN
11809 	      NULL;
11810 	END;
11811 	--END DBI FIX
11812 
11813         l_progress := '230';
11814         IF (l_debug = 1) THEN
11815             DEBUG('complete_putaway_wrapper: Progress: ' || l_progress, l_proc_name);
11816             DEBUG('Calling complete_putaway API with the following parameters: ', l_proc_name);
11817             DEBUG('p_lpn_id                  => ' || l_mol_lpn_id_tbl(i), l_proc_name);
11818             DEBUG('p_org_id                  => ' || p_organization_id, l_proc_name);
11819             DEBUG('p_temp_id                 => ' || l_mmtt_temp_id, l_proc_name);
11820             DEBUG('p_item_id                 => ' || l_item_id, l_proc_name);
11821             DEBUG('p_rev                     => ' || l_revision, l_proc_name);
11822             DEBUG('p_lot                     => ' || l_lot_number, l_proc_name);
11823             DEBUG('p_loc                     => ' || l_locator_id, l_proc_name);
11824             DEBUG('p_sub                     => ' || l_subinventory_code, l_proc_name);
11825             DEBUG('p_qty                     => ' || l_quantity, l_proc_name);
11826             DEBUG('p_uom                     => ' || l_uom_code, l_proc_name);
11827             DEBUG('p_user_id                 => ' || p_user_id, l_proc_name);
11828             DEBUG('p_disc                    => ' || l_disc, l_proc_name);
11829             DEBUG('p_entire_lpn              => ' || l_entire_lpn, l_proc_name);
11830             DEBUG('p_to_lpn                  => ' || l_to_lpn_name, l_proc_name);
11831             DEBUG('p_qty_reason_id           => ' || p_qty_reason_id, l_proc_name);
11832             DEBUG('p_loc_reason_id           => ' || p_loc_reason_id, l_proc_name);
11833             DEBUG('p_loc_reason_id           => ' || p_loc_reason_id, l_proc_name);
11834             DEBUG('p_process_serial_flag     => ' || p_process_serial_flag, l_proc_name);
11835             DEBUG('p_product_transaction_id  => ' || l_product_transaction_id, l_proc_name);
11836             DEBUG('p_new_txn_header_id       => ' || l_new_txn_header_id, l_proc_name);
11837             DEBUG('p_secondary_quantity      => ' || l_secondary_quantity, l_proc_name);
11838             DEBUG('p_secondary_uom           => ' || l_secondary_uom, l_proc_name);
11839         END IF;
11840 
11841         --Now that I have all the values calculated and set for this task,
11842         --call the complete_putaway API
11843         wms_task_dispatch_put_away.complete_putaway(
11844             p_lpn_id                  =>  l_mol_lpn_id_tbl(i)
11845           , p_org_id                  =>  p_organization_id
11846           , p_temp_id                 =>  l_mmtt_temp_id
11847           , p_item_id                 =>  l_item_id
11848           , p_rev                     =>  l_revision
11849           , p_lot                     =>  l_lot_number
11850           , p_loc                     =>  l_locator_id
11851           , p_sub                     =>  l_subinventory_code
11852           , p_qty                     =>  l_quantity
11853           , p_uom                     =>  l_uom_code
11854           , p_user_id                 =>  p_user_id
11855           , p_disc                    =>  l_disc
11856           , x_return_status           =>  x_return_status
11857           , x_msg_count               =>  x_msg_count
11858           , x_msg_data                =>  x_msg_data
11859           , p_entire_lpn              =>  l_entire_lpn
11860           , p_to_lpn                  =>  l_to_lpn_name
11861           , p_qty_reason_id           =>  p_qty_reason_id
11862           , p_loc_reason_id           =>  p_loc_reason_id
11863           , p_process_serial_flag     =>  p_process_serial_flag
11864           , p_commit                  =>  'N'
11865           , p_product_transaction_id  =>  l_product_transaction_id
11866           , p_lpn_mode                =>  l_lpn_mode
11867           , p_new_txn_header_id       =>  l_new_txn_header_id
11868           , p_secondary_quantity      =>  l_secondary_quantity  --OPM Convergence
11869           , p_secondary_uom           =>  l_secondary_uom --OPM Convergence
11870           , p_primary_uom             =>  l_primary_uom_code_tbl(i)
11871 	  );
11872 
11873         IF x_return_status = FND_API.G_RET_STS_ERROR THEN
11874           IF (l_debug = 1) THEN
11875             DEBUG('complete_putaway_wrapper: Error at progress: ' || l_progress || ' complete_putaway API returns g_exc_error', l_proc_name, 1);
11876           END IF;
11877           RAISE FND_API.G_EXC_ERROR;
11878         END IF;
11879 
11880         IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
11881           IF (l_debug = 1) THEN
11882             DEBUG('complete_putaway_wrapper: Error at progress: ' || l_progress || ' complete_putaway API returns g_unexp_error', l_proc_name, 1);
11883           END IF;
11884           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
11885         END IF;
11886 
11887         IF (l_debug = 1) THEN
11888           DEBUG('complete_putaway_wrapper: successful execution of complete_putaway for temp_id : ' || l_mmtt_temp_id, l_proc_name);
11889         END IF;
11890       ELSE
11891         IF (l_debug = 1) THEN
11892           DEBUG('complete_putaway_wrapper: skip_mmtt is TRUE, should not drop this task.' ||
11893                 ' Could be because the serial could not be matched with the task', l_proc_name);
11894         END IF;
11895         l_remaining_qty := l_remaining_qty + l_mmtt_prm_qty;
11896       END IF;  --END IF l_skip_mmtt = FALSE
11897     END LOOP;   --END For each MMTT in the group
11898     --END LOOP;   --END loop through all the tasks for the current group
11899 
11900     --Close the cursor opened above
11901     IF all_tasks_csr%ISOPEN THEN
11902       CLOSE all_tasks_csr;
11903     END IF;
11904 
11905     IF md_tasks_csr%ISOPEN THEN
11906       CLOSE md_tasks_csr;
11907     END IF;
11908 
11909     --So far so good. We're done with the RTIs/MMTTs for this group
11910     --Should now process all the MMTTs for the current group and call
11911     --the appropriate Transaction Managers
11912     IF (p_lpn_context = G_LPN_CONTEXT_RCV) THEN
11913       l_progress := '240';
11914 
11915       -- LMS code (start) (Anupam Jain)
11916       -- get the distinct OperationPlanId from MMTT for receiving LPNs
11917       -- logic is: return the operationplanid only if it is same for all the MMTTs
11918       -- for the given transaction_header_id
11919 
11920       IF (l_debug = 1) THEN
11921         debug('complete_putaway_wrapper: Before calling LMS Code for receiving LPNs', l_proc_name);
11922       END IF;
11923 
11924 
11925       SELECT count(DISTINCT operation_plan_id) INTO l_lms_rec_count
11926       FROM mtl_material_transactions_temp mmtt
11927       WHERE transaction_header_id = p_txn_header_id;
11928 
11929       IF (l_debug = 1) THEN
11930            debug('complete_putaway_wrapper: LMS Code, l_lms_rec_count: ' || l_lms_rec_count , l_proc_name);
11931       END IF;
11932 
11933       IF l_lms_rec_count = 1 THEN
11934           -- since same OperationPlanId stamped for all the MMTTs of the group
11935           -- return its value for LMS data capture
11936           SELECT operation_plan_id INTO x_lms_operation_plan_id
11937           FROM mtl_material_transactions_temp
11938           WHERE transaction_header_id = p_txn_header_id
11939           AND ROWNUM = 1;
11940 
11941           IF (l_debug = 1) THEN
11942            debug('complete_putaway_wrapper: LMS Code, p_lms_operation_plan_id: ' || x_lms_operation_plan_id , l_proc_name);
11943           END IF;
11944 
11945       END IF;
11946 
11947       IF (l_debug = 1) THEN
11948        debug('complete_putaway_wrapper: After calling LMS Code for receiving LPNs', l_proc_name);
11949       END IF;
11950 
11951       -- LMS Code (end)
11952 
11953       --Receiving LPN. Call Receiving Transaction Manager
11954       IF (l_debug = 1) THEN
11955         DEBUG('complete_putaway_wrapper: LPN Resides in receiving. Calling the receiving manager...');
11956       END IF;
11957 
11958       INV_RCV_MOBILE_PROCESS_TXN.rcv_process_receive_txn(
11959           x_return_status =>  x_return_status
11960         , x_msg_data      =>  l_msg_data);
11961 
11962       --Set the flag to indicate that data has been committed (would be used
11963       --to rollback to the appropriate savepoint)
11964       l_rcv_commit := TRUE;
11965 
11966       IF (l_debug = 1) THEN
11967         DEBUG('complete_putaway_wrapper: After calling the receiving manager');
11968         DEBUG('status returned by the receiving manager ' || x_return_status, 4);
11969         DEBUG('complete_putaway_wrapper: Rxing TM Txn Message: ' || l_msg_data, 4);
11970       END IF;
11971 
11972       IF x_return_status = FND_API.G_RET_STS_ERROR THEN
11973         IF (l_debug = 1) THEN
11974           DEBUG('complete_putaway_wrapper: Encountered g_exc_error while calling receiving manager;'|| to_char(sysdate, 'YYYY-MM-DD HH:DD:SS'), 4);
11975         END IF;
11976         fnd_message.set_name('WMS', 'WMS_TD_TXNMGR_ERROR');
11977         fnd_msg_pub.add;
11978         RAISE FND_API.G_EXC_ERROR;
11979       END IF;
11980 
11981       IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
11982         IF (l_debug = 1) THEN
11983           DEBUG('complete_putaway_wrapper: Encountered g_exc_unexp_error while calling receiving manager;'|| to_char(sysdate, 'YYYY-MM-DD HH:DD:SS'), 4);
11984         END IF;
11985         fnd_message.set_name('WMS', 'WMS_TD_TXNMGR_ERROR');
11986         fnd_msg_pub.add;
11987         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
11988       END IF;
11989 
11990       /*************** For bug 7428404 *****************/
11991          --Done with the TM calls. Go delete the processed records from the global temp rable
11992          --for the current group
11993           l_progress := 245;
11994 
11995           IF p_drop_type = G_DT_ITEM_DROP AND l_disc = 'Y' THEN
11996 
11997 
11998               Update_Tasks_In_Group(
11999                       x_return_status => x_return_status
12000                     , x_msg_count => x_msg_count
12001                     , x_msg_data =>  l_msg_data
12002                     , x_rec_count =>  l_del_count
12003                     , p_organization_id =>  p_organization_id
12004                     , p_drop_type => p_drop_type
12005                                 , p_lpn_id => p_lpn_id
12006                     , p_group_id => p_group_id
12007                     , p_emp_id => l_emp_id );
12008 
12009                IF (l_debug = 1) THEN
12010                  DEBUG('complete_putaway_wrapper: After calling Update_Tasks_In_Group');
12011                  DEBUG('status returned by Update_Tasks_In_Group ' || x_return_status, 4);
12012                  DEBUG('complete_putaway_wrapper: Txn Message: ' || l_msg_data, 4);
12013                END IF;
12014 
12015                IF x_return_status = FND_API.G_RET_STS_ERROR THEN
12016                  IF (l_debug = 1) THEN
12017                    DEBUG('complete_putaway_wrapper: Encountered g_exc_error while calling Update_Tasks_In_Group;'|| to_char(sysdate, 'YYYY-MM-DD HH:DD:SS'), 4);
12018                  END IF;
12019                  RAISE FND_API.G_EXC_ERROR;
12020                END IF;
12021 
12022                IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
12023                  IF (l_debug = 1) THEN
12024                    DEBUG('complete_putaway_wrapper: Encountered g_exc_unexp_error while calling Update_Tasks_In_Group;'|| to_char(sysdate, 'YYYY-MM-DD HH:DD:SS'), 4);
12025                  END IF;
12026                  RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
12027                END IF;
12028 
12029 
12030           ELSE
12031                l_del_count := Remove_Tasks_In_Group(
12032                                 p_del_type  =>  1
12033                               , p_group_id  =>  p_group_id);
12034           END IF;
12035 
12036 
12037     --If the LPN context is Inventory/WIP
12038     ELSE
12039       l_progress := '240';
12040       IF (p_lpn_context = G_LPN_CONTEXT_WIP) THEN
12041         --For WIP Putaway, use the new business flow code
12042         l_business_flow_code := 35;
12043       END IF;
12044 
12045       IF (l_debug = 1) THEN
12046         DEBUG('complete_putaway_wrapper: Progress: ' || l_progress || '. Calling post_process_lpn', l_proc_name);
12047       END IF;
12048 
12049       -- Nested LPN changes
12050       --Do not call the post_process_lpn if dest sub is NOT LPN controlled or passed to_lpn is NULL
12051       IF (l_lpn_controlled_flag = 2 OR p_to_lpn_name IS NULL) THEN
12052         IF l_debug = 1 THEN
12053           DEBUG('Dest sub is not LPN controlled, or passed TO LPN is NULL or Dest sub is not storage sub . No post_process_lpn calls.', l_proc_name);
12054           DEBUG('l_drop_sub_type : '||  l_drop_sub_type || ' to_lpn_name : ' || p_to_lpn_name || ' l_lpn_controlled_flag : ' || l_lpn_controlled_flag );
12055         END IF;
12056 
12057       ELSE
12058         IF l_debug = 1 THEN
12059           DEBUG('Calling post_process_lpn to handle the pack/unpacks.', l_proc_name);
12060         END IF;
12061 
12062         post_process_lpn(
12063              x_return_status          => x_return_status
12064            , x_msg_count              => x_msg_count
12065            , x_msg_data               => x_msg_data
12066            , p_from_lpn_id            => p_lpn_id
12067            , p_organization_id        => p_organization_id
12068            , p_subinventory_code      => p_subinventory_code
12069            , p_lpn_mode               => l_lpn_mode
12070            , p_locator_id             => p_locator_id
12071            , p_to_lpn_id              => p_to_lpn_id
12072            , p_project_id             => p_project_id
12073            , p_task_id                => p_task_id
12074            , p_user_id                => p_user_id
12075            , p_lpn_context            => p_lpn_context
12076            , p_pack_trans_id          => l_new_txn_header_id
12077            , p_batch_id               => l_new_txn_header_id
12078            , p_batch_seq              => l_batch_seq );
12079 
12080         IF (l_debug = 1) THEN
12081           DEBUG('complete_putaway_wrapper: post_process_lpn returns: ' || x_return_status, 4);
12082         END IF;
12083 
12084         IF x_return_status = fnd_api.g_ret_sts_error THEN
12085           RAISE fnd_api.g_exc_error;
12086         END IF;
12087 
12088         IF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
12089           RAISE fnd_api.g_exc_unexpected_error;
12090         END IF;
12091       END IF;
12092       -- Nested LPN changes end
12093 
12094       --Call the Inventory Transaction Manager
12095       IF (l_debug = 1) THEN
12096         DEBUG('complete_putaway_wrapper: INV or WIP LPN. Am calling the Inventory TM with txn_header_id ' || l_new_txn_header_id, l_proc_name);
12097       END IF;
12098 
12099       IF p_lpn_context = 2 THEN
12100         l_txn_ret := inv_lpn_trx_pub.process_lpn_trx(
12101             p_trx_hdr_id             => l_new_txn_header_id
12102           , p_commit                 => fnd_api.g_false
12103           , x_proc_msg               => x_msg_data
12104           , p_business_flow_code     => l_business_flow_code
12105           , p_proc_mode              => 1);
12106       ELSE
12107         l_txn_ret := inv_lpn_trx_pub.process_lpn_trx(
12108             p_trx_hdr_id             => l_new_txn_header_id
12109           , p_commit                 => fnd_api.g_false
12110           , x_proc_msg               => x_msg_data
12111           , p_business_flow_code     => l_business_flow_code);
12112       END IF;
12113 
12114       IF (l_debug = 1) THEN
12115         debug('complete_putaway_wrapper: After Calling Inventory TM', l_proc_name);
12116         debug('complete_putaway_wrapper: l_txn_ret: ' || l_txn_ret, l_proc_name);
12117         debug('complete_putaway_wrapper: Txn Message ' || x_msg_data, l_proc_name);
12118       END IF;
12119 
12120       IF (l_txn_ret <> 0) THEN
12121         fnd_message.set_name('WMS', 'WMS_TD_TXNMGR_ERROR');
12122         fnd_msg_pub.ADD;
12123         RAISE fnd_api.g_exc_unexpected_error;
12124       END IF;
12125 
12126 
12127     --Done with the TM calls. Go delete the records from the global temp rable
12128     --for the current group
12129     l_progress := '250';
12130     l_del_count := Remove_Tasks_In_Group(
12131                       p_del_type  =>  1
12132                     , p_group_id  =>  p_group_id);
12133 
12134     END IF;   --END IF call the managers based on lpn_context
12135 
12136      -- Call the API to clear the receiving global variables
12137      inv_rcv_common_apis.rcv_clear_global;
12138 
12139     --At last, I'm done for the current group. Can go ahead and commit my work!
12140     COMMIT;
12141 
12142     --BUG 3625990: Do not lock the LPN if it has context 3
12143     IF (p_lpn_context <> 3) THEN
12144        -- Lock all the remaining LPNs the user is currently working on
12145        -- We are re-locking again since the commit above would have relieved the lock.
12146        Lock_LPNs( x_return_status  => x_return_status
12147 		  ,x_msg_count      => x_msg_count
12148 		  ,x_msg_data       => x_msg_data );
12149 
12150        l_progress := '260';
12151 
12152        IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
12153 	  IF (l_debug = 1) THEN
12154 	     DEBUG(' Error in locking LPNs ' || x_msg_data ,l_proc_name,1);
12155 	  END IF;
12156 	  RAISE fnd_api.g_exc_error;
12157        END IF;
12158 
12159        IF (l_debug = 1) THEN
12160 	  DEBUG(':-)complete_putaway_wrapper exitted with success for group id ' || p_group_id || ' :-). Exit time: ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), l_proc_name);
12161        END IF;
12162     END IF;
12163 
12164   EXCEPTION
12165     WHEN fnd_api.g_exc_error THEN
12166       x_return_status := fnd_api.g_ret_sts_error;
12167        --Close the cursors
12168       IF mol_csr%ISOPEN THEN
12169         CLOSE mol_csr;
12170       END IF;
12171       IF all_tasks_csr%ISOPEN THEN
12172         CLOSE all_tasks_csr;
12173       END IF;
12174       IF md_tasks_csr%ISOPEN THEN
12175         CLOSE md_tasks_csr;
12176       END IF;
12177       IF rcv_serials_csr%ISOPEN THEN
12178         CLOSE rcv_serials_csr;
12179       END IF;
12180       IF msnt_ser_csr%ISOPEN THEN
12181         CLOSE msnt_ser_csr;
12182       END IF;
12183 
12184       IF NOT l_rcv_commit THEN
12185         ROLLBACK TO complete_putaway_wrap_sp;
12186       END IF;
12187 
12188       IF (l_debug = 1) THEN
12189         DEBUG('complete_putaway_wrapper: G_EXC_ERROR ocurred after l_progress = ' || l_progress, l_proc_name);
12190       END IF;
12191 
12192       --Get message count and data
12193       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
12194 
12195     WHEN fnd_api.g_exc_unexpected_error THEN
12196       x_return_status := fnd_api.g_ret_sts_unexp_error;
12197 
12198        --Close the cursors
12199       IF mol_csr%ISOPEN THEN
12200         CLOSE mol_csr;
12201       END IF;
12202       IF all_tasks_csr%ISOPEN THEN
12203         CLOSE all_tasks_csr;
12204       END IF;
12205       IF md_tasks_csr%ISOPEN THEN
12206         CLOSE md_tasks_csr;
12207       END IF;
12208       IF rcv_serials_csr%ISOPEN THEN
12209         CLOSE rcv_serials_csr;
12210       END IF;
12211       IF msnt_ser_csr%ISOPEN THEN
12212         CLOSE msnt_ser_csr;
12213       END IF;
12214 
12215       IF NOT l_rcv_commit THEN
12216         ROLLBACK TO complete_putaway_wrap_sp;
12217       END IF;
12218 
12219       IF (l_debug = 1) THEN
12220         DEBUG('complete_putaway_wrapper: G_EXC_UNEXPECTED_ERROR ocurred after l_progress = ' || l_progress, l_proc_name);
12221       END IF;
12222 
12223       --Get message count and data
12224       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
12225 
12226     WHEN OTHERS THEN
12227       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
12228 
12229       --Close the cursors
12230       IF mol_csr%ISOPEN THEN
12231         CLOSE mol_csr;
12232       END IF;
12233       IF all_tasks_csr%ISOPEN THEN
12234         CLOSE all_tasks_csr;
12235       END IF;
12236       IF md_tasks_csr%ISOPEN THEN
12237         CLOSE md_tasks_csr;
12238       END IF;
12239       IF rcv_serials_csr%ISOPEN THEN
12240         CLOSE rcv_serials_csr;
12241       END IF;
12242       IF msnt_ser_csr%ISOPEN THEN
12243         CLOSE msnt_ser_csr;
12244       END IF;
12245 
12246       IF NOT l_rcv_commit THEN
12247         ROLLBACK TO complete_putaway_wrap_sp;
12248       END IF;
12249 
12250       IF (l_debug = 1) THEN
12251         DEBUG('complete_putaway_wrapper: OTHER EXCEPTION ' || SQLERRM || ' ocurred after l_progress = ' || l_progress, l_proc_name);
12252       END IF;
12253 
12254       --Get message count and data
12255       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
12256       IF SQLCODE IS NOT NULL THEN
12257         inv_mobile_helper_functions.sql_error('wms_putaway_utils_pvt.complete_putaway_wrapper',l_progress, SQLCODE);
12258       END IF;
12259 
12260   END complete_putaway_wrapper;
12261 
12262 PROCEDURE validate_into_lpn
12263   (p_organization_id      IN    NUMBER             ,
12264    p_lpn_id               IN    NUMBER             ,
12265    p_employee_id          IN    NUMBER             ,
12266    p_into_lpn             IN    VARCHAR2           ,
12267    x_return_status        OUT   NOCOPY VARCHAR2    ,
12268    x_msg_count            OUT   NOCOPY NUMBER      ,
12269    x_msg_data             OUT   NOCOPY VARCHAR2    ,
12270    x_validation_passed    OUT   NOCOPY VARCHAR2    ,
12271    x_new_lpn_created      OUT   NOCOPY VARCHAR2    ,
12272    p_project_id           IN    NUMBER ,
12273    p_task_id              IN    NUMBER ,
12274    p_drop_type            IN    VARCHAR2 ,
12275    p_sub                  IN    VARCHAR2 ,
12276    p_loc                  IN    NUMBER ,
12277    p_crossdock_type       IN    VARCHAR2,
12278    p_consolidation_method_id   IN    NUMBER,
12279    p_backorder_delivery_detail_id IN NUMBER,
12280    p_suggested_into_lpn_id IN   NUMBER
12281    )
12282 IS
12283    l_api_name             CONSTANT VARCHAR2(30) := 'validate_into_lpn';
12284    l_progress             VARCHAR2(10);
12285    l_count                NUMBER;
12286    l_into_lpn_id          NUMBER;
12287    l_lpn_context          NUMBER;
12288    l_lpn_sub              VARCHAR2(10);
12289    l_lpn_loc_id           NUMBER;
12290    l_into_lpn_sub         VARCHAR2(10);
12291    l_into_lpn_loc_id      NUMBER;
12292    l_into_lpn_context     NUMBER;
12293    l_into_lpn_project_id  NUMBER;
12294    l_into_lpn_task_id     NUMBER;
12295    l_passed               VARCHAR2(1);
12296    l_item_count           NUMBER;
12297    l_lot_count            NUMBER;
12298    l_rev_count            NUMBER;
12299    l_qty_gtmp             NUMBER;
12300    l_qty_wlc              NUMBER;
12301    l_debug                NUMBER := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
12302    l_back_order_delivery_id NUMBER := -999;
12303    l_crossdock_type         NUMBER;
12304    l_wip_supply_type        NUMBER := -999;
12305    l_sub_validation_passed  NUMBER := 1;
12306    l_lpn_sub_type           NUMBER;
12307    l_lpn_controlled_flag    NUMBER;
12308    l_reservable_type        NUMBER;
12309    l_sub_type               NUMBER;
12310    l_allowed                VARCHAR2(1);--R12
12311    l_into_outermost_lpn_id  NUMBER;
12312    l_lpn_has_material       NUMBER;
12313    --BUG#11769042
12314    l_bo_d_frm_id 			NUMBER;
12315    l_bo_d_to_id 			NUMBER;
12316    v_line_rows  			WSH_UTIL_CORE.id_tab_type;
12317    v_grouping_rows       	WSH_UTIL_CORE.id_tab_type;
12318    l_return_status 			VARCHAR2(1);
12319    --BUG#11769042
12320 
12321 BEGIN
12322    IF (l_debug = 1) THEN
12323       debug('***Calling validate_into_lpn with the following parameters***','validate_into_lpn',9);
12324       debug('p_organization_id: => ' || p_organization_id,'validate_into_lpn',9);
12325       debug('p_lpn_id: ==========> ' || p_lpn_id,'validate_into_lpn',9);
12326       debug('p_employee_id: =====> ' || p_employee_id,'validate_into_lpn',9);
12327       debug('p_project_id: =====> ' || p_project_id,'validate_into_lpn',9);
12328       debug('p_task_id: =====> ' || p_task_id,'validate_into_lpn',9);
12329       debug('p_into_lpn: ========> ' || p_into_lpn,'validate_into_lpn',9);
12330       debug('p_drop_type: ========> ' || p_drop_type,'validate_into_lpn',9);
12331       debug('p_sub: ==============> ' || p_sub,'validate_into_lpn',9);
12332       debug('p_loc: ==============> ' || p_loc,'validate_into_lpn',9);
12333       debug('p_crossdock_type================>'||p_crossdock_type,'validate_into_lpn',9);
12334       debug('p_consolidation_method_id=======>'||p_consolidation_method_id,'validate_into_lpn',9);
12335       debug('p_backorder_delivery_detail_id==>'||p_backorder_delivery_detail_id,'validate_into_lpn',9);
12336       debug('p_suggested_into_lpn_id=========>'||p_suggested_into_lpn_id,'validate_into_lpn',9);
12337    END IF;
12338 
12339    -- Set the savepoint
12340    SAVEPOINT validate_lpn_sp;
12341    x_return_status := fnd_api.g_ret_sts_success;
12342 
12343    l_progress  := '10';
12344 
12345    -- Get the required LPN values
12346    SELECT lpn_context, subinventory_code, locator_id
12347      INTO l_lpn_context, l_lpn_sub, l_lpn_loc_id
12348      FROM wms_license_plate_numbers
12349      WHERE organization_id = p_organization_id
12350      AND lpn_id = p_lpn_id;
12351    l_progress := '20';
12352    IF (l_debug = 1) THEN
12353       debug('Retrieved LPN attributes','validate_into_lpn',9);
12354       debug('LPN Context: =======> ' || l_lpn_context,'validate_into_lpn',9);
12355       debug('Subinventory: ======> ' || l_lpn_sub,'validate_into_lpn',9);
12356       debug('Locator ID: ========> ' || l_lpn_loc_id,'validate_into_lpn',9);
12357    END IF;
12358 
12359    -- See if the Into LPN exists
12360    BEGIN
12361       SELECT 1, lpn_id, subinventory_code, locator_id, lpn_context,outermost_lpn_id
12362   INTO l_count, l_into_lpn_id, l_into_lpn_sub, l_into_lpn_loc_id, l_into_lpn_context,
12363        l_into_outermost_lpn_id
12364   FROM  wms_license_plate_numbers
12365   WHERE license_plate_number = p_into_lpn
12366   AND   organization_id      = p_organization_id
12367   FOR UPDATE OF lpn_id NOWAIT;
12368       -- lock so no other user can load into this lpn
12369    EXCEPTION
12370       WHEN NO_DATA_FOUND THEN
12371    l_count := 0;
12372    END;
12373    l_progress := '30';
12374    IF (l_debug = 1) THEN
12375       debug('Into LPN count: ' || l_count,'validate_into_lpn',9);
12376    END IF;
12377 
12378    --BUG 3354934
12379    IF (l_count <> 0 AND l_into_lpn_context = 5) THEN
12380       IF (l_debug = 1) THEN
12381 	 debug('Into LPN context is 5, so bypass all validation','validate_into_lpn',9);
12382       END IF;
12383       x_return_status := fnd_api.g_ret_sts_success;
12384       x_validation_passed := 'Y';
12385       x_new_lpn_created := 'N';
12386       --RETURN; --BUG#11769042
12387    END IF;
12388 
12389    IF (l_count = 0) THEN
12390       -- New LPN
12391       IF (l_debug = 1) THEN
12392    debug('LPN does not exist so create new LPN','validate_into_lpn',9);
12393       END IF;
12394 
12395       -- Create the LPN
12396       wms_container_pub.create_lpn
12397   ( p_api_version       => 1.0,
12398     p_init_msg_list     => fnd_api.g_false,
12399     p_commit            => fnd_api.g_false,
12400     x_return_status     => x_return_status,
12401     x_msg_count         => x_msg_count,
12402     x_msg_data          => x_msg_data,
12403     p_lpn               => p_into_lpn,
12404     p_organization_id   => p_organization_id,
12405     p_container_item_id => NULL,
12406     p_lot_number        => NULL,
12407     p_revision          => NULL,
12408     p_serial_number     => NULL,
12409 --    p_subinventory      => l_lpn_sub,
12410 --    p_locator_id        => l_lpn_loc_id,
12411     p_source            => 5, -- defined but not used
12412     p_cost_group_id     => NULL,
12413     x_lpn_id            => l_into_lpn_id
12414     );
12415       l_progress := '40';
12416       IF (l_debug = 1) THEN
12417    debug('Finished calling create LPN API: ' || l_into_lpn_id,'validate_into_lpn',9);
12418       END IF;
12419 
12420       -- Check the return status from the API call
12421       IF (x_return_status = fnd_api.g_ret_sts_success) THEN
12422          IF (l_debug = 1) THEN
12423             debug('Success returned from wms_container_pub.create_lpn API','validate_into_lpn',9);
12424          END IF;
12425              ELSE
12426          IF (l_debug = 1) THEN
12427             debug('Failure returned from wms_container_pub.create_lpn API','validate_into_lpn',9);
12428          END IF;
12429          FND_MESSAGE.SET_NAME('WMS', 'WMS_TD_CREATE_LPN_ERROR');
12430          FND_MSG_PUB.ADD;
12431          RAISE fnd_api.g_exc_error;
12432       END IF;
12433       l_progress := '60';
12434 
12435       -- Set the validation passed output parameter
12436       x_validation_passed := 'Y';
12437 
12438       -- Set the new LPN created output parameter
12439       x_new_lpn_created := 'Y';
12440 
12441       --BUG 3354934
12442       IF (l_debug = 1) THEN
12443 	 debug('LPN created.  Can bypass rest of validation','validate_into_lpn',9);
12444       END IF;
12445       x_return_status := fnd_api.g_ret_sts_success;
12446       RETURN;
12447 
12448     ELSE
12449       -- Existing LPN
12450       IF (l_debug =1 ) THEN
12451         debug('INTO LPN exists so validate it','validate_into_lpn',9);
12452         debug('Retrieved INTO LPN attributes','validate_into_lpn',9);
12453         debug('LPN ID: ============> ' || l_into_lpn_id,'validate_into_lpn',9);
12454         debug('LPN Context: =======> ' || l_into_lpn_context,'validate_into_lpn',9);
12455         debug('Subinventory: ======> ' || l_into_lpn_sub,'validate_into_lpn',9);
12456         debug('Locator ID: ========> ' || l_into_lpn_loc_id,'validate_into_lpn',9);
12457       END IF;
12458 
12459       -- Set the new LPN created output parameter
12460       x_new_lpn_created := 'N';
12461 
12462       l_count := 0;
12463 
12464       IF p_drop_type = 'LOAD'  THEN
12465 
12466         BEGIN
12467 	   -- Validate the LPN
12468 	   -- Bug 4652943
12469 	   -- For Performance Reason the count for WMS_LPN_CONTENTS
12470 	   -- is done outside.
12471 
12472 	   l_lpn_has_material := 0;
12473 
12474 	   select NVL(count(1),0)
12475 	     into l_lpn_has_material
12476 	     FROM wms_lpn_contents
12477 	     WHERE parent_lpn_id IN (SELECT wlpn1.lpn_id
12478 				     FROM   wms_license_plate_numbers wlpn1
12479 				     START WITH wlpn1.lpn_id = l_into_outermost_lpn_id
12480 				     CONNECT BY PRIOR wlpn1.lpn_id = wlpn1.parent_lpn_id);
12481 
12482 	  SELECT 1
12483             INTO l_count
12484             FROM dual
12485             WHERE EXISTS
12486            (SELECT 'INTO_LPN_EXISTS'
12487             FROM wms_license_plate_numbers wlpn
12488             WHERE wlpn.organization_id = p_organization_id
12489             AND wlpn.lpn_id <> p_lpn_id
12490             AND wlpn.lpn_id = l_into_lpn_id
12491             AND (wlpn.lpn_context = WMS_CONTAINER_PUB.LPN_CONTEXT_PREGENERATED
12492             OR (wlpn.lpn_context = l_lpn_context
12493            AND (
12494            ( l_lpn_has_material = 0
12495            --NOT EXISTS (SELECT 'LPN_HAS_MATERIAL'
12496            --FROM wms_lpn_contents
12497            --WHERE parent_lpn_id IN (SELECT wlpn1.lpn_id
12498            --      FROM   wms_license_plate_numbers wlpn1
12499            --      START WITH wlpn1.lpn_id = wlpn.outermost_lpn_id
12500            --      CONNECT BY PRIOR wlpn1.lpn_id = wlpn1.parent_lpn_id)
12501            --)
12502            )
12503           OR
12504           /** Bug:5650113
12505               While performing Manual Load by Item by Item, mmtt.lpn_id
12506               is stamped with from lpn_id not with into_lpn_id and
12507               because of this no rows is getting fetched for the query
12508               'LOADED_BY_SAME_USER'.
12509               So, modified mmmt.lpn_id to mtrl.lpn_id and introduced join
12510               with mmmt and mtrl table.
12511            */
12512           (EXISTS (SELECT 'LOADED_BY_SAME_USER'
12513              FROM  mtl_material_transactions_temp mmtt,
12514              wms_dispatched_tasks wdt,--5650113
12515              mtl_txn_request_lines mtrl--5650113
12516              WHERE mmtt.organization_id = p_organization_id
12517              AND mmtt.transaction_temp_id = wdt.transaction_temp_id
12518              AND wdt.organization_id = p_organization_id
12519              AND wdt.task_type = 2
12520              AND wdt.status = 4
12521              AND wdt.person_id = p_employee_id
12522              AND mtrl.line_id = mmtt.move_order_line_id--5650113
12523              AND mtrl.lpn_id IN (SELECT lpn_id--5650113
12524                FROM wms_license_plate_numbers
12525                START WITH lpn_id = wlpn.outermost_lpn_id
12526                CONNECT BY PRIOR lpn_id = parent_lpn_id
12527                )
12528              )
12529            )
12530           )
12531         )
12532       )
12533       AND inv_material_status_grp.is_status_applicable('TRUE',
12534                         NULL,
12535                         INV_GLOBALS.G_TYPE_CONTAINER_PACK,
12536                         NULL,
12537                         NULL,
12538                         p_organization_id,
12539                         NULL,
12540                         wlpn.subinventory_code,
12541                         wlpn.locator_id,
12542                         NULL,
12543                         NULL,
12544                         'Z') = 'Y'
12545            AND inv_material_status_grp.is_status_applicable('TRUE',
12546                         NULL,
12547                         INV_GLOBALS.G_TYPE_CONTAINER_PACK,
12548                         NULL,
12549                         NULL,
12550                         p_organization_id,
12551                         NULL,
12552                         wlpn.subinventory_code,
12553                         wlpn.locator_id,
12554                         NULL,
12555                         NULL,
12556                         'L') = 'Y');
12557             EXCEPTION
12558          WHEN NO_DATA_FOUND THEN
12559             l_count := 0;
12560             IF (l_debug = 1) THEN
12561                debug('No data found','validate_into_lpn',9);
12562             END IF;
12563             END;
12564             l_progress := '70';
12565             IF (l_debug = 1) THEN
12566          debug('Validated Into LPN count: ' || l_count,'validate_into_lpn',9);
12567             END IF;
12568 
12569             IF (l_count = 0) THEN
12570          x_validation_passed := 'N';
12571          IF (l_debug = 1) THEN
12572             debug('Into LPN validation failed!','validate_into_lpn',9);
12573          END IF;
12574    FND_MESSAGE.SET_NAME('WMS','WMS_NO_LPN_LOAD_ALLOWED');
12575    FND_MSG_PUB.ADD;
12576          RAISE fnd_api.g_exc_error;
12577              ELSE
12578          x_validation_passed := 'Y';
12579          IF (l_debug = 1) THEN
12580             debug('Into LPN validation passed!','validate_into_lpn',9);
12581          END IF;
12582             END IF;
12583          END IF;
12584 
12585        -- Validate Into lpn for drop
12586        IF (p_drop_type IN ('MANUAL_DROP', 'SYSTEM_DROP')) THEN
12587 
12588 	  --R12
12589 	  IF (p_crossdock_type IS NOT NULL AND
12590 	      p_backorder_delivery_detail_id IS NOT NULL AND
12591 	      l_into_lpn_context = 11) THEN
12592 	     IF (p_consolidation_method_id = 1) THEN
12593 
12594 		--If user override the LPN suggested by MDC
12595 		IF (p_suggested_into_lpn_id IS NOT NULL AND
12596 		    p_suggested_into_lpn_id <> l_into_lpn_id) THEN
12597 
12598 		   IF (l_debug = 1) THEN
12599 		      debug('Calling wms_mdc_pvt.validate_to_lpn','validate_into_lpn',9);
12600 		   END IF;
12601 
12602 		   wms_mdc_pvt.validate_to_lpn
12603 		     (p_from_lpn_id       => p_lpn_id
12604 		      ,p_to_lpn_id        => l_into_lpn_id
12605 		      ,p_is_from_to_delivery_same => 'U'
12606 		      ,x_allow_packing    => l_allowed
12607 		      ,x_return_status    => x_return_status
12608 		      ,x_msg_count        => x_msg_count
12609 		      ,x_msg_data         => x_msg_data
12610 		      );
12611 
12612 		   IF (x_return_status = 'U') THEN
12613 		      IF (l_debug = 1) THEN
12614 			 DEBUG(' Error in wms_mdc_pvt.validate_to_lpn','validate_into_lpn',9);
12615 		      END IF;
12616 		      RAISE fnd_api.g_exc_error;
12617 		    ELSE
12618 		      IF (l_allowed = 'Y') THEN
12619 			 IF (l_debug = 1) THEN
12620 			    debug(' validation passed!','validate_into_lpn',9);
12621 			 END IF;
12622 		       ELSE
12623 			 x_validation_passed := 'N';
12624 			 RETURN;
12625 		      END IF;
12626 		   END IF;
12627 		END IF;--IF (p_suggested_into_lpn_id IS NOT NULL AND p_suggested_into_lpn_id <> l_into_lpn_id) THEN
12628 
12629 	      ELSE --Non-MDC cases
12630 
12631 		IF (l_debug = 1) THEN
12632 		  debug('Checking for comingling','validate_into_lpn',9);
12633 		END IF;
12634 
12635 		-- Make sure that the Into LPN does not contain other deliveres
12636 --Commented for BUG#11769042
12637 	/*	BEGIN
12638 		   SELECT 'N'
12639 		     INTO l_allowed
12640 		     FROM wsh_delivery_assignments_v wda1
12641 		     ,    wsh_delivery_assignments_v wda2
12642 		     ,    wsh_delivery_details wdd
12643 		     WHERE wda1.delivery_detail_id = p_backorder_delivery_detail_id
12644 		     AND   wda2.delivery_detail_id = wdd.delivery_detail_id
12645 		     AND   wdd.lpn_id = l_into_lpn_id
12646 		     AND   Nvl(wda1.delivery_id,-1) <> Nvl(wda2.delivery_id,-2);
12647 		EXCEPTION
12648 		   WHEN too_many_rows THEN
12649 		      IF (l_debug = 1) THEN
12650 			 debug('too many rows','validate_into_lpn',9);
12651 		      END IF;
12652 		      l_allowed := 'N';
12653 		   WHEN no_data_found THEN
12654 		      IF (l_debug = 1) THEN
12655 			 debug('no data found','validate_into_lpn',9);
12656 		      END IF;
12657 		      l_allowed := 'Y';
12658 		   WHEN OTHERS THEN
12659 		      IF (l_debug = 1) THEN
12660 			 debug('others exception: '||Sqlerrm,'validate_into_lpn',9);
12661 		      END IF;
12662 		      l_allowed := 'N';
12663 		END;
12664 
12665 
12666 		IF (l_debug = 1) THEN
12667 		   debug('l_allowed:'||l_allowed,'validate_into_lpn',9);
12668 		END IF;
12669 */--Commented for BUG#11769042
12670 
12671                 /* Bug 5474915: Changed the error message from
12672                    WMS_COMMINGLE_EXISTS to WMS_LOOSE_TO_LPN.*/
12673 
12674 --Commented for BUG#11769042
12675 	/*	IF l_allowed = 'N' THEN
12676 		   x_return_status := fnd_api.g_ret_sts_error;
12677 		   x_validation_passed := 'N';
12678 		   fnd_message.set_name('WMS', 'WMS_LOOSE_TO_LPN');
12679 		   fnd_msg_pub.ADD;
12680 		   RETURN;
12681 		END IF;*/--Commented for BUG#11769042
12682 	     END IF;
12683 	     --x_validation_passed := 'Y';--Commented for BUG#11769042
12684 	     --RETURN;--Commented for BUG#11769042
12685 	  END IF;
12686 	  --R12 END
12687 
12688 	  --BUG#11769042 The Backorder Delivery Detail Id passed was incorrect
12689 	  -- So fetching the BO DD ID from GTMP table and the Delivery Detail in the Into LPN
12690     /*Validating for Correct BO dd ID from Gtmp*/
12691     BEGIN
12692        SELECT wpgt2.BACKORDER_DELIVERY_DETAIL
12693          INTO l_bo_d_frm_id
12694          FROM wms_putaway_group_tasks_gtmp  wpgt1 , wms_putaway_group_tasks_gtmp wpgt2
12695         WHERE wpgt1.backorder_delivery_detail = p_backorder_delivery_detail_id
12696           AND wpgt2.transaction_header_id  = wpgt1.transaction_header_id
12697           AND wpgt2.row_type = 'All Task'
12698           AND wpgt1.row_type = 'Group Task'
12699           AND ROWNUM = 1;
12700 
12701       v_line_rows(1) := l_bo_d_frm_id;
12702        IF (l_debug = 1) THEN
12703 			 debug('l_bo_d_frm_id: '||l_bo_d_frm_id,'validate_into_lpn',9);
12704 	   END IF;
12705 
12706      IF l_into_lpn_context = 11 THEN
12707 
12708      SELECT wda.delivery_detail_id
12709        INTO l_bo_d_to_id
12710        FROM wsh_delivery_assignments_v wda ,
12711             wsh_delivery_details wdd
12712       WHERE wdd.lpn_id = l_into_lpn_id
12713         AND wda.PARENT_DELIVERY_DETAIL_ID = wdd.delivery_detail_id
12714         AND ROWNUM = 1;
12715 
12716 
12717 		 IF (l_debug = 1) THEN
12718 			 debug('l_bo_d_to_id: '||l_bo_d_to_id,'validate_into_lpn',9);
12719 		 END IF;
12720 
12721      ELSE
12722      BEGIN
12723      SELECT MAX(mtrl.backorder_delivery_detail_id)
12724        INTO l_bo_d_to_id
12725        FROM mtl_txn_request_lines mtrl
12726       WHERE EXISTS  (SELECT 1
12727                        FROM rcv_transactions_interface rti, mtl_material_transactions_temp mmtt
12728                       WHERE rti.transfer_lpn_id = l_into_lpn_id
12729                         AND rti.mmtt_temp_id = mmtt.transaction_temp_id
12730                         AND mmtt.move_order_line_id = mtrl.line_id
12731 						AND mmtt.inventory_item_id = mtrl.inventory_item_id
12732                         AND mmtt.organization_id = mtrl.organization_id
12733                         AND TRANSACTION_STATUS_CODE <> 'ERROR');
12734 		 IF (l_debug = 1) THEN
12735 			 debug('l_bo_d_to_id: '||l_bo_d_to_id,'validate_into_lpn',9);
12736 		 END IF;
12737 
12738      EXCEPTION
12739        WHEN No_Data_Found  THEN
12740 			IF (l_debug = 1) THEN
12741 			 debug('In Exp l_bo_d_to_id : '||SQLERRM,'validate_into_lpn',9);
12742 			END IF;
12743       l_allowed := 'Y';
12744       x_return_status := fnd_api.g_ret_sts_success;
12745 	  x_validation_passed := 'Y';
12746       l_bo_d_to_id := NULL;
12747      END;
12748 
12749      END IF;
12750 
12751      v_line_rows(2) := l_bo_d_to_id;
12752 
12753     IF l_bo_d_to_id IS NOT NULL AND l_bo_d_frm_id IS NOT NULL THEN
12754 
12755 		wsh_delivery_details_grp.get_carton_grouping(p_line_rows => v_line_rows,
12756                                                      x_grouping_rows => v_grouping_rows,
12757                                                      x_return_status => l_return_status);
12758 		IF (l_debug = 1) THEN
12759 			 debug('l_return_status: '||l_return_status,'validate_into_lpn',9);
12760              debug('v_grouping_rows: '||v_grouping_rows.count,'validate_into_lpn',9);
12761 			 debug('v_grouping_rows(1): '||v_grouping_rows(1),'validate_into_lpn',9);
12762 			 debug('v_grouping_rows(2): '||v_grouping_rows(2),'validate_into_lpn',9);
12763 		END IF;
12764 
12765 		IF v_grouping_rows(1) <> v_grouping_rows(2) THEN
12766 			IF (l_debug = 1) THEN
12767 				 debug('Grouping does not match','validate_into_lpn',9);
12768 				 debug('Raise fnd_api.g_ret_sts_error','validate_into_lpn',9);
12769 			END IF;
12770 			l_allowed := 'N';
12771 			x_return_status := fnd_api.g_ret_sts_error;
12772 			x_validation_passed := 'N';
12773 			fnd_message.set_name('WMS', 'WMS_LOOSE_TO_LPN');
12774 			fnd_msg_pub.ADD;
12775 		   RETURN;
12776 		ELSE
12777 			IF (l_debug = 1) THEN
12778 				 debug('Grouping does match','validate_into_lpn',9);
12779 				 debug('Grouping does match v_grouping_rows(1): '||v_grouping_rows(1),'validate_into_lpn',9);
12780 				 debug('Grouping does match v_grouping_rows(2): '||v_grouping_rows(2),'validate_into_lpn',9);
12781 				 debug('l_allowed := Y','validate_into_lpn',9);
12782 			END IF;
12783 
12784 			l_allowed := 'Y';
12785 			x_return_status := fnd_api.g_ret_sts_success;
12786 			x_validation_passed := 'Y';
12787 		END IF;
12788 	ELSE
12789 		l_allowed := 'Y';
12790 		x_return_status := fnd_api.g_ret_sts_success;
12791 		x_validation_passed := 'Y';
12792     END IF;
12793 
12794     EXCEPTION
12795     WHEN OTHERS THEN
12796 		IF (l_debug = 1) THEN
12797 		  debug('In Exp: '||SQLERRM,'validate_into_lpn',9);
12798 		END IF;
12799 
12800 		l_allowed := 'Y';
12801 		x_return_status := fnd_api.g_ret_sts_success;
12802 		x_validation_passed := 'Y';
12803     END;
12804     /*Validating for Correct BO dd ID from Gtmp*/
12805 --BUG#11769042
12806          ---Check If the from LPN and ToLPN are same.
12807          IF p_lpn_id = l_into_lpn_id  THEN
12808 
12809            -- Check there are no child LPNs and there is only one item left for Item drop
12810            BEGIN
12811                select  count(DISTINCT inventory_item_id),count(DISTINCT lot_number),count(DISTINCT revision)
12812                  into  l_item_count,l_lot_count,l_rev_count
12813                  from  WMS_PUTAWAY_GROUP_TASKS_GTMP wpgtg
12814                 where  lpn_id = p_lpn_id
12815                   and  drop_type='ID'
12816 		 and  row_type = 'Group Task'
12817                   and  not exists
12818                      (select 1
12819                           from wms_license_plate_numbers wln
12820                        where wln.parent_lpn_id = p_lpn_id);
12821            EXCEPTION
12822              WHEN OTHERS THEN
12823                l_item_count :=2;
12824            END;
12825 
12826            IF (l_item_count > 1 OR l_lot_count > 1 OR l_rev_count > 1) THEN
12827              IF (l_debug = 1) THEN
12828                 debug('There are multiple items/lot/rev in the LPN or the LPN has child LPNS.!','validate_into_lpn',9);
12829              END IF;
12830 
12831               x_validation_passed := 'N';
12832               RETURN;
12833            END IF;
12834 
12835            -- check LPN is fully allocated.
12836            select sum(primary_quantity) INTO l_qty_gtmp
12837              from WMS_PUTAWAY_GROUP_TASKS_GTMP
12838       where lpn_id = p_lpn_id
12839               and row_type = 'Group Task'
12840               and drop_type = 'ID';
12841 
12842 		   select sum(primary_quantity) into l_qty_wlc  --12679402
12843              from wms_lpn_contents
12844             where parent_lpn_id = p_lpn_id;
12845 
12846            IF (l_qty_gtmp<>l_qty_wlc) THEN
12847              IF (l_debug = 1) THEN
12848                 debug(' quantity from global temp table ' || l_qty_gtmp);
12849                 debug(' quantity from wms license plate Numbers ' || l_qty_wlc);
12850                 debug('Given LPN is not fully allocated!','validate_into_lpn',9);
12851              END IF;
12852              x_validation_passed := 'N';
12853              RETURN;
12854            END IF;
12855      /** Bug 5684823:
12856           During Drop txn, if "from lpn" and "to lpn" are same, no need to
12857           verify the "to lpn" exists and it is in loaded state through the query
12858           "INTO_LPN_EXISTS". We can safely bypass it, as the "to lpn" will exist
12859           as "from lpn" exists and "from lpn" will be in loaded status irrespective
12860           of system generated drop or manual drop.
12861           So, bypassing the query 'INTO_LPN_EXISTS' and setting l_count to 1.
12862       */
12863       l_count := 1;
12864      ELSE--IF p_lpn_id = l_into_lpn_id  THEN
12865          BEGIN
12866             SELECT 1 INTO l_count
12867               FROM DUAL
12868              WHERE EXISTS (
12869                   SELECT 'INTO_LPN_EXISTS'
12870                     FROM wms_license_plate_numbers wlpn
12871                    WHERE wlpn.organization_id = p_organization_id
12872 		     AND wlpn.lpn_id = l_into_lpn_id
12873 	             AND (wlpn.lpn_context = 5 OR
12874 			  ((wlpn.lpn_context = 1 AND l_lpn_context IN (1,2) ) OR
12875 			   wlpn.lpn_context IN (1,3,11) AND l_lpn_context = 3)--BUG 3463634 --BUG#11769042(Adding LPN context 11)
12876 			  AND (p_lpn_id = wlpn.lpn_id OR --BUG 3368408
12877 			       (NOT EXISTS (SELECT 'LOADED'
12878 					    FROM  mtl_material_transactions_temp mmtt,
12879 					    wms_dispatched_tasks wdt
12880 					    WHERE mmtt.organization_id = p_organization_id
12881 					    AND mmtt.transaction_temp_id = wdt.transaction_temp_id
12882 					    AND wdt.organization_id = p_organization_id
12883 					    AND wdt.task_type = 2
12884 					    AND wdt.status = 4
12885 					    AND mmtt.lpn_id IN (SELECT wlpn2.lpn_id
12886 								FROM wms_license_plate_numbers wlpn2
12887 								START WITH wlpn2.lpn_id = wlpn.outermost_lpn_id
12888 								CONNECT BY PRIOR wlpn2.lpn_id = wlpn2.parent_lpn_id
12889 								)
12890 					    )
12891 				)
12892 			       )
12893 			  AND inv_material_status_grp.is_status_applicable
12894   			         ('TRUE',NULL,inv_globals.g_type_container_pack,
12895 				  NULL,NULL,p_organization_id,NULL,wlpn.subinventory_code,
12896 				  wlpn.locator_id,NULL,NULL,'Z') = 'Y'
12897 			  AND inv_material_status_grp.is_status_applicable
12898 	                         ('TRUE',NULL,inv_globals.g_type_container_pack,NULL,
12899 				  NULL,p_organization_id,NULL,wlpn.subinventory_code,
12900 				  wlpn.locator_id,NULL,NULL,'L') = 'Y'
12901 	                  )
12902 	           );
12903          EXCEPTION
12904          WHEN NO_DATA_FOUND THEN
12905             l_count := 0;
12906             IF (l_debug = 1) THEN
12907                debug('No data found for drop','validate_into_lpn',9);
12908             END IF;
12909          END; -- End for validation of drop
12910 
12911      END IF;--IF p_lpn_id = l_into_lpn_id  THEN
12912          l_progress := '70';
12913 
12914          IF (l_debug = 1) THEN
12915             debug('Validated Into LPN count for drop: ' || l_count,'validate_into_lpn',9);
12916          END IF;
12917 
12918          IF (l_count = 0) THEN
12919            x_validation_passed := 'N';
12920            IF (l_debug = 1) THEN
12921               debug('Into LPN validation failed! for drop! ','validate_into_lpn',9);
12922            END IF;
12923            RAISE fnd_api.g_exc_error;
12924          ELSE
12925            x_validation_passed := 'Y';
12926            IF (l_debug = 1) THEN
12927               debug('Into LPN validation passed for drop!','validate_into_lpn',9);
12928            END IF;
12929          END IF; -- end for l_count=0
12930 
12931 	 -- LPN Sub/LOC validation
12932    --Bug #4414782
12933 	 IF (l_into_lpn_context = 3 AND
12934 	     l_into_lpn_sub IS NULL AND
12935        p_sub IS NULL AND p_loc IS NULL) THEN
12936 	    --The user should be be allowed to nest a LPN with sub/loc
12937 	    --into a RCV LPN that has no sub/loc
12938 	    l_sub_validation_passed := 0;
12939 	  ELSIF  (p_drop_type = 'MANUAL_DROP' AND l_lpn_context <> l_into_lpn_context) THEN
12940 	    --If it is a Manual Drop, then the user should be able to drop
12941 	    --into a storage inventory if it is in RCV or WIP
12942 	    l_sub_validation_passed := 1;
12943 	  ELSE
12944 	    IF (l_into_lpn_sub IS NOT NULL AND l_into_lpn_loc_id IS NOT NULL) THEN
12945 	       IF (p_sub IS NOT NULL AND p_loc IS NOT NULL) THEN
12946 		  -- This is called from the PutawayDropPage
12947 		  -- So p_sub and p_loc must match the sub/loc of the
12948 		  -- into LPN
12949 		  IF (l_debug = 1) THEN
12950 		     debug('Validate sub for PutawayDrop scenario','validate_into_lpn',9);
12951 		  END IF;
12952 
12953 		  IF (p_lpn_id <> l_into_lpn_id AND --If user enter same into
12954 		                                    --lpn AS the from LPN,
12955 		                                    --We should allow him move it
12956 		                                    --to a different place
12957 		      (p_sub <> Nvl(l_into_lpn_sub, '@@@') OR
12958 		       p_loc <> Nvl(l_into_lpn_loc_id,-999))) THEN
12959 		     l_sub_validation_passed := 0;
12960 		   ELSE
12961 		     l_sub_validation_passed := 1;
12962 		  END IF;
12963 		ELSE
12964 		  IF (l_debug = 1) THEN
12965 		     debug('Calling get_crossdock_info','validate_into_lpn',9);
12966 		     debug('  p_lpn_id =======> '||p_lpn_id,'validate_into_lpn',9);
12967 		     debug('  p_organization_id ===> '||p_organization_id,'validate_into_lpn',9);
12968 		  END IF;
12969 		  wms_putaway_utils.get_crossdock_info
12970 		    (p_lpn_id          => p_lpn_id
12971 		     ,p_organization_id => p_organization_id
12972 		     ,x_back_order_delivery_id => l_back_order_delivery_id
12973 		     ,x_crossdock_type         => l_crossdock_type
12974 		     ,x_wip_supply_type        => l_wip_supply_type
12975 		     );
12976 		  IF (l_debug = 1) THEN
12977 		     debug('After calling get_crossdock_info','validate_into_lpn',9);
12978 		     debug('  l_crossdock_type =======> '||l_crossdock_type,'validate_into_lpn',9);
12979 		  END IF;
12980 
12981 	          BEGIN
12982 		     SELECT lpn_controlled_flag,reservable_type,Nvl(subinventory_type,1)
12983 		       INTO l_lpn_controlled_flag,l_reservable_type,l_lpn_sub_type
12984 		       FROM mtl_secondary_inventories
12985 		       WHERE organization_id = p_organization_id
12986 		       AND secondary_inventory_name = l_into_lpn_sub
12987 		       AND ((Nvl(subinventory_type,1)=1 AND
12988 			     NVL(disable_date, TRUNC(SYSDATE + 1)) > TRUNC(SYSDATE))
12989 			    OR
12990 			    (Nvl(subinventory_type,1)=2 AND
12991 			     (trunc(disable_date + (300*365)) >= trunc(SYSDATE)
12992 			      OR TO_CHAR(disable_date, 'YYYY/MM/DD') = '1700/01/01')))
12993 		       AND (((Nvl(subinventory_type,1)=1) AND
12994 			     inv_ui_item_sub_loc_lovs.validate_lpn_sub
12995 			     (p_organization_id,
12996 			      secondary_inventory_name,
12997 			      p_lpn_id) = 'Y') OR
12998 			    Nvl(subinventory_type,1)=2);
12999 		  EXCEPTION
13000 		     WHEN OTHERS THEN
13001 			l_lpn_sub_type := -999;
13002 			l_lpn_controlled_flag := -999;
13003 			l_reservable_type := -999;
13004 		  END;
13005 
13006 		  IF (p_drop_type <> 'MANUAL_DROP') THEN
13007 		     --If it is MANUAL_DROP, p_sub won't be passed
13008 	             BEGIN
13009 			SELECT Nvl(subinventory_type,1)
13010 			  INTO l_sub_type
13011 			  FROM mtl_secondary_inventories
13012 			  WHERE organization_id = p_organization_id
13013 			  AND secondary_inventory_name = p_sub
13014 			  AND ((Nvl(subinventory_type,1)=1 AND
13015 				NVL(disable_date, TRUNC(SYSDATE + 1)) > TRUNC(SYSDATE))
13016 			       OR
13017 			       (Nvl(subinventory_type,1)=2 AND
13018 				(trunc(disable_date + (300*365)) >= trunc(SYSDATE)
13019 				 OR TO_CHAR(disable_date,'YYYY/MM/DD') = '1700/01/01')));
13020 		     EXCEPTION
13021 			WHEN OTHERS THEN
13022 			   l_sub_type := -999;
13023 		     END;
13024 		  END IF;
13025 
13026 		  IF (l_debug = 1) THEN
13027 		     debug('l_lpn_sub_type: ' || l_lpn_sub_type,'validate_into_lpn',9);
13028 		     debug('l_lpn_controlled_flag: ' || l_lpn_controlled_flag ,'validate_into_lpn',9);
13029 		     debug('l_reservable_type: ' || l_reservable_type,'validate_into_lpn',9);
13030 		     debug('l_sub_type: ' || l_sub_type,'validate_into_lpn',9);
13031 		  END IF;
13032 
13033 
13034 		  IF l_crossdock_type = 0 THEN
13035 		     --No crossdocking
13036 		     IF p_drop_type = 'SYSTEM_DROP' THEN
13037 			--Directed Drop:
13038 			--In this case, lpn sub must be same as suggested sub
13039 			IF (p_lpn_id <> l_into_lpn_id) THEN --bug 5380737
13040 			  IF (l_sub_type = 1	AND l_lpn_sub_type = 1) OR
13041 			    (l_sub_type = 2 AND l_lpn_sub_type = 2) THEN
13042 			     l_sub_validation_passed := 1;
13043 			  ELSE
13044 			    l_sub_validation_passed := 0;
13045 			  END IF;
13046 			ELSE
13047 			  l_sub_validation_passed := 1;
13048 			END IF;
13049 		      ELSE
13050 			--Manual drop, all sub is OK
13051 			l_sub_validation_passed := 1;
13052 		     END IF; --IF p_drop_type = 'SYSTEM_DROP' THEN
13053 		   ELSIF l_crossdock_type = 1 THEN
13054 		     --SO xdock:
13055 		     IF (p_drop_type = 'MANUAL_DROP') THEN
13056 			--If fromLPN is a WIP lpn, then LPN sub
13057 			--must be storage sub, reservable and lpn-controlled
13058 			--Otherwise, it can be rcv sub and a valid storage sub
13059 			IF (l_lpn_context = 2
13060 			    AND l_lpn_sub_type = 1
13061 			    AND l_lpn_controlled_flag = 1
13062 			    AND l_reservable_type = 1) OR
13063 			  (l_lpn_context = 3
13064 			   AND (l_lpn_sub_type = 3 OR
13065 				(l_lpn_sub_type = 1
13066 				 AND l_lpn_controlled_flag = 1
13067 				 AND l_reservable_type = 1)
13068 				)
13069 			   ) THEN
13070 			   l_sub_validation_passed := 1;
13071 			 ELSE
13072 			   l_sub_validation_passed := 0;
13073 			END IF;
13074 		      ELSE
13075 			--If system suggested storage sub, then
13076 			--LPN sub must be valid storage sub
13077 			IF (p_lpn_id <> l_into_lpn_id) THEN --bug 5380737
13078 			  IF (l_sub_type = 1
13079 			     AND l_lpn_sub_type = 1
13080 			     AND l_lpn_controlled_flag = 1
13081 			     AND l_reservable_type = 1) OR
13082 			     (l_sub_type = 2
13083 			     AND l_lpn_sub_type = 2) THEN
13084 			    l_sub_validation_passed := 1;
13085 			  ELSE
13086 			    l_sub_validation_passed := 0;
13087 			  END IF;
13088 			ELSE
13089 			  l_sub_validation_passed := 1;
13090 			END IF;
13091 		     END IF; --IF (p_drop_type = 'MANUAL_DROP') THEN
13092 		   ELSIF l_crossdock_type = 2 THEN
13093 		     --WIP PULL
13094 		     IF (p_drop_type = 'MANUAL_LOAD') THEN
13095 			IF (l_lpn_context = 2) THEN
13096 			   --Destination sub is not lpn controlled.
13097 			   --So shouldn't be allowed to enter LPN
13098 			   l_sub_validation_passed := 0;
13099 			 ELSE
13100 			   IF (l_lpn_sub_type = 2) THEN
13101 			      l_sub_validation_passed := 1;
13102 			    ELSE
13103 			      l_sub_validation_passed := 0;
13104 			   END IF;
13105 			END IF;
13106 		      ELSE
13107 			--The user should not be allowed to enter
13108 			--LPN in inventory LPN
13109 			IF (p_lpn_id <> l_into_lpn_id) THEN --bug 5380737
13110 			  IF (l_sub_type = 2
13111 			      AND l_lpn_sub_type = 2) THEN
13112 			     l_sub_validation_passed := 1;
13113 			  ELSE
13114 			     l_sub_validation_passed := 0;
13115 			  END IF;
13116 			ELSE
13117 			  l_sub_validation_passed := 1;
13118 			END IF;
13119 		     END IF;
13120 		   ELSIF l_crossdock_type = 3 THEN
13121 		     -- WIP Push.  Into LPN field shouldn't be shown
13122 		     l_sub_validation_passed := 1;
13123 		   ELSE --l_crossdock_type = 4: SO XDOCK mixed with WIP PULL
13124 		     IF l_lpn_context = 2 THEN
13125 			l_sub_validation_passed := 0;
13126 		      ELSE
13127 			--System won't suggest inventory loc
13128 			IF (l_lpn_sub_type = 2) THEN
13129 			   l_sub_validation_passed := 1;
13130 			 ELSE
13131 			   l_sub_validation_passed := 0;
13132 			END IF;
13133 		     END IF; --IF l_lpn_context = 2 THEN
13134 		  END IF; --IF l_crossdock_type = 0 THEN
13135 	       END IF;--IF (p_sub IS NOT NULL AND p_loc IS NOT NULL) THEN
13136 	    END IF;--IF (p_drop_type = 'MANUAL_DROP' AND l_lpn_context <> l_into_lpn_context) THEN
13137 	 END IF;--IF (l_into_lpn_sub IS NOT NULL AND l_into_lpn_loc_id IS NOT NULL) THEN
13138 
13139 	 IF (l_debug = 1) THEN
13140 	    debug('l_sub_validation_passed ===> '||l_sub_validation_passed,'validate_into_lpn',9);
13141 	 END IF;
13142 
13143 	 IF (l_sub_validation_passed <> 1) THEN
13144 	    x_validation_passed := 'N';
13145 	 END IF;
13146 
13147        END IF; -- Enf for p_drop_type = 'MANUAL_DROP' or p_drop_type = 'SYSTEM_DROP
13148        -- End for validating drop
13149    END IF;
13150 
13151    /* Added l_into_lpn_context for bug 14509637 */
13152 -- if into LPN is an old one, need to check project/task ID
13153    IF ( (x_new_lpn_created = 'N') AND l_into_lpn_context <> 5 ) THEN
13154       IF (l_debug = 1) THEN
13155          debug('Validating project and task IDs...','validate_into_lpn', 9);
13156       END IF;
13157 
13158       BEGIN
13159   IF (l_lpn_context = 1) THEN
13160      SELECT
13161        project_id
13162        ,task_id
13163        INTO
13164        l_into_lpn_project_id
13165        ,l_into_lpn_task_id
13166        FROM
13167        mtl_item_locations
13168        WHERE
13169        inventory_location_id = l_into_lpn_loc_id;
13170    ELSE -- context is 2 or 3
13171      SELECT
13172        project_id
13173        ,task_id
13174        INTO
13175        l_into_lpn_project_id
13176        ,l_into_lpn_task_id
13177        FROM
13178        mtl_txn_request_lines mtrl, (SELECT lpn_id
13179                    FROM wms_license_plate_numbers
13180                    START WITH lpn_id = l_into_lpn_id
13181                    CONNECT BY PRIOR lpn_id = parent_lpn_id
13182                   ) wlpn2
13183       WHERE
13184       mtrl.lpn_id= wlpn2.lpn_id
13185       AND ROWNUM = 1;
13186       /* Modified clause lpn_id = 1 added in bug 7354300 back to lpn_id = l_into_lpn_id as it caused regression reported in bug 14356323 */
13187   END IF;
13188       EXCEPTION
13189    WHEN no_data_found THEN
13190       l_into_lpn_project_id := null;
13191       l_into_lpn_task_id := null;
13192    WHEN OTHERS THEN
13193       IF (l_debug = 1) THEN
13194          debug('unexpected exception in select project/task id',
13195          'validate_into_lpn', 9);
13196       END IF;
13197       END;
13198 
13199       IF (l_debug = 1) THEN
13200    debug('Into LPN Project Id = '||l_into_lpn_project_id
13201          ||' Task Id = ' || l_into_lpn_task_id,
13202          'validate_into_lpn', 9);
13203       END IF;
13204 
13205       IF NOT (Nvl(p_project_id, -9999) = Nvl(l_into_lpn_project_id, -9999) AND
13206         Nvl(p_task_id, -9999) = Nvl(l_into_lpn_task_id, -9999)) THEN
13207    IF (l_debug = 1) THEN
13208       debug('validate project/task ID failed','validate_into_lpn',9);
13209    END IF;
13210 
13211    x_validation_passed := 'N';
13212    RAISE fnd_api.g_exc_error;
13213       END IF; --NOT (Nvl(p_project_id, 9999) = Nvl(l_into_lpn_project_id, 9999)
13214               -- AND Nvl(p_task_id, 9999) = Nvl(l_into_lpn_task_id, 9999))
13215    END IF;  -- IF (x_new_lpn_created = 'N')
13216 
13217    -- Set the return status to success
13218    x_return_status := fnd_api.g_ret_sts_success;
13219    l_progress := '80';
13220 
13221    IF (l_debug = 1) THEN
13222       debug('***End of validate_into_lpn***','validate_into_lpn',9);
13223    END IF;
13224 
13225 EXCEPTION
13226    WHEN FND_API.G_EXC_ERROR THEN
13227       ROLLBACK TO validate_lpn_sp;
13228       x_return_status := fnd_api.g_ret_sts_error;
13229       x_validation_passed := 'N';
13230       x_new_lpn_created := 'N';
13231       fnd_msg_pub.count_and_get(p_count => x_msg_count,
13232         p_data  => x_msg_data);
13233       IF (l_debug = 1) THEN
13234      debug('Exiting validate_into_lpn - Execution error: ' ||
13235          l_progress ||' '|| TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),'validate_into_lpn',9);
13236       END IF;
13237 
13238    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
13239       ROLLBACK TO validate_lpn_sp;
13240       x_return_status := fnd_api.g_ret_sts_unexp_error;
13241       x_validation_passed := 'N';
13242       x_new_lpn_created := 'N';
13243       fnd_msg_pub.count_and_get(p_count => x_msg_count,
13244         p_data  => x_msg_data);
13245       IF (l_debug = 1) THEN
13246      debug('Exiting validate_into_lpn - Unexpected error: ' ||
13247          l_progress ||' '|| TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),'validate_into_lpn',9);
13248       END IF;
13249 
13250    WHEN OTHERS THEN
13251       ROLLBACK TO validate_lpn_sp;
13252       x_return_status := fnd_api.g_ret_sts_unexp_error;
13253       x_validation_passed := 'N';
13254       x_new_lpn_created := 'N';
13255       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
13256         fnd_msg_pub.add_exc_msg(g_pkg_name, l_api_name);
13257       END IF;
13258       fnd_msg_pub.count_and_get(p_count => x_msg_count,
13259         p_data  => x_msg_data);
13260       IF (l_debug = 1) THEN
13261      debug('Exiting validate_into_lpn - Others exception: ' ||
13262          l_progress ||' '|| TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),'validate_into_lpn',9);
13263       END IF;
13264 
13265 END validate_into_lpn;
13266 
13267 
13268 --Added new procedure for UNload ER:13797542
13269 
13270 /*
13271 This proceedure clean up the stuck Inbound lpns while putaway transaction.
13272 x_return_status =  U :Unload Task Failed because of Unknow Exception
13273 x_return_status =  S :LPN UnLoaded
13274 x_return_statuse = E  :Move order Doesn't exist for the LPN.
13275                       :Sub/Loc Mismatch between MTRL and WLPN
13276                       :Lot Number missing either in MTRL or WLC
13277 */
13278 
13279 PROCEDURE unload_task(
13280     x_return_status OUT NOCOPY VARCHAR2 ,
13281     x_message OUT NOCOPY       VARCHAR2 ,
13282     p_lpn_id IN NUMBER )
13283 IS
13284   l_proc_name        VARCHAR2(30) := 'Unload_task';
13285   l_progress         VARCHAR2(10) := '0';
13286   l_lpn_id           NUMBER;
13287   l_org_id           NUMBER ;
13288   l_count            NUMBER := 0;
13289   l_lot_control_code NUMBER := 0;
13290   l_error_code       NUMBER;
13291   l_msg_count        NUMBER := 0;
13292   l_tempid_tab num_tab;
13293   l_return_status VARCHAR2(10);
13294   l_user_id       NUMBER;
13295   l_cal_api       BOOLEAN;
13296   l_debug     NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'),0);
13297 
13298 
13299 
13300 
13301 CURSOR c_all_mmtt_cursor(l_org_id NUMBER)
13302 IS
13303   SELECT
13304     /*+ ORDERED USE_NL(mtrl mmtt) INDEX(MTRL MTL_TXN_REQUEST_LINES_N7) */
13305     mmtt.transaction_temp_id
13306   FROM
13307     (
13308       SELECT
13309         lpn_id,
13310         organization_id
13311       FROM
13312         wms_license_plate_numbers
13313         START WITH lpn_id       = p_lpn_id
13314         CONNECT BY PRIOR lpn_id = parent_lpn_id
13315     )
13316   wlpn,
13317   mtl_txn_request_lines mtrl,
13318   mtl_material_transactions_temp mmtt
13319 WHERE
13320   (
13321     mtrl.line_id = mmtt.move_order_line_id
13322   )
13323 AND wlpn.organization_id = l_org_id
13324 AND mtrl.lpn_id          = wlpn.lpn_id
13325 UNION --mmtt with out moveorder line_id stamped
13326 SELECT
13327   mmtt.transaction_temp_id
13328 FROM
13329   (
13330     SELECT
13331       lpn_id,
13332       organization_id
13333     FROM
13334       wms_license_plate_numbers
13335       START WITH lpn_id       = p_lpn_id
13336       CONNECT BY PRIOR lpn_id = parent_lpn_id
13337   )
13338   wlpn,
13339   mtl_material_transactions_temp mmtt
13340 WHERE
13341   (
13342     wlpn.lpn_id  = mmtt.lpn_id
13343   OR wlpn.lpn_id = mmtt.content_lpn_id
13344   )
13345 AND wlpn.organization_id = l_org_id ;
13346 CURSOR c_all_items (l_org_id NUMBER)
13347 IS
13348   SELECT
13349     parent_lpn_id lpn_id,
13350     inventory_item_id
13351   FROM
13352     wms_lpn_contents
13353   WHERE
13354     parent_lpn_id IN
13355     (
13356       SELECT
13357         lpn_id
13358       FROM
13359         wms_license_plate_numbers
13360         START WITH lpn_id       = p_lpn_id
13361         CONNECT BY PRIOR lpn_id = parent_lpn_id
13362     )
13363 AND organization_id = l_org_id ;
13364 CURSOR cur_mtrl_wlc_qty_mismatch(l_org_id NUMBER)
13365 IS
13366   SELECT
13367     1
13368   FROM
13369     (
13370       SELECT
13371         NVL(SUM(quantity-NVL(quantity_delivered,0)),0) mtrl_quantity,
13372         inventory_item_id,
13373         uom_code
13374       FROM
13375         mtl_txn_request_lines
13376       WHERE
13377         lpn_id IN
13378         (
13379           SELECT
13380             lpn_id
13381           FROM
13382             wms_license_plate_numbers
13383             START WITH lpn_id       = p_lpn_id
13384             CONNECT BY PRIOR lpn_id = parent_lpn_id
13385         )
13386     AND organization_id = l_org_id
13387     AND line_status     = 7
13388     GROUP BY
13389       inventory_item_id,
13390       uom_code
13391     )
13392     mtrl ,
13393     (
13394       SELECT
13395         NVL(SUM(quantity),0) wlc_quantity ,
13396         inventory_item_id,
13397         uom_code
13398       FROM
13399         wms_lpn_contents
13400       WHERE
13401         parent_lpn_id IN
13402         (
13403           SELECT
13404             lpn_id
13405           FROM
13406             wms_license_plate_numbers
13407             START WITH lpn_id       = p_lpn_id
13408             CONNECT BY PRIOR lpn_id = parent_lpn_id
13409         )
13410       AND organization_id = l_org_id
13411       GROUP BY
13412         inventory_item_id,
13413         uom_code
13414     )
13415     wlc
13416   WHERE
13417     mtrl.mtrl_quantity      <> wlc .wlc_quantity
13418   AND mtrl.inventory_item_id = wlc.inventory_item_id
13419   AND mtrl.uom_code          = wlc.uom_code
13420   AND rownum                 <2;
13421   CURSOR cur_wlc_without_mtrl(l_org_id NUMBER )
13422   IS
13423     SELECT
13424       1
13425     FROM
13426       wms_lpn_contents wlc
13427     WHERE
13428       parent_lpn_id IN
13429       (
13430         SELECT
13431           lpn_id
13432         FROM
13433           wms_license_plate_numbers
13434           START WITH lpn_id       = p_lpn_id
13435           CONNECT BY PRIOR lpn_id = parent_lpn_id
13436       )
13437   AND organization_id = l_org_id
13438   AND NOT EXISTS
13439     (
13440       SELECT
13441         1
13442       FROM
13443         mtl_txn_request_lines
13444       WHERE
13445         lpn_id              = wlc.parent_lpn_id
13446       AND organization_id   = wlc.organization_id
13447       AND inventory_item_id = wlc.inventory_item_id
13448       AND line_status       = 7
13449     )
13450   AND rownum <2 ;
13451   CURSOR cur_mtrl_wlpn_mismatch(l_org_id NUMBER)
13452   IS
13453     SELECT
13454       1
13455     FROM
13456       wms_license_plate_numbers wlpn,
13457       mtl_txn_request_lines mtrl
13458     WHERE
13459       wlpn.lpn_id = mtrl.lpn_id
13460     AND
13461       (
13462         NVL(wlpn.SUBINVENTORY_CODE,-1) <> NVL(mtrl.FROM_SUBINVENTORY_CODE,-1)
13463       OR NVL(wlpn.locator_id,      -1) <> NVL(mtrl.FROM_LOCATOR_ID,-1)
13464       )
13465     AND wlpn.lpn_id IN
13466       (
13467         SELECT
13468           lpn_id
13469         FROM
13470           wms_license_plate_numbers
13471           START WITH lpn_id       = p_lpn_id
13472           CONNECT BY PRIOR lpn_id = parent_lpn_id
13473       )
13474   AND wlpn.organization_id = l_org_id
13475   AND mtrl.line_status     = 7
13476   AND rownum               <2;
13477   CURSOR cur_mtrl_wlc_lot_mismatch(l_lpn_id NUMBER, l_item_id NUMBER ,l_org_id
13478                                             NUMBER )
13479   IS
13480     SELECT
13481       1
13482     FROM
13483       mtl_txn_request_lines mtrl,
13484       wms_lpn_contents wlc
13485     WHERE
13486       mtrl.lpn_id                 = wlc.parent_lpn_id
13487     AND mtrl.lot_number          IS NOT NULL
13488     AND wlc.lot_number           IS NOT NULL
13489     AND mtrl.inventory_item_id    = wlc.inventory_item_id
13490     AND NVL(mtrl.lot_number,'@') <> NVL(wlc.lot_number,'@')
13491     AND wlc.organization_id       = l_org_id
13492     AND wlc.parent_lpn_id         = l_lpn_id
13493     AND wlc.inventory_item_id     = l_item_id
13494     AND rownum                    <2 ;
13495 
13496 
13497 
13498     l_cur_mtrl_wlpn_mismatch cur_mtrl_wlpn_mismatch%ROWTYPE;
13499     l_cur_mtrl_wlc_lot_mismatch cur_mtrl_wlc_lot_mismatch%ROWTYPE;
13500     l_cur_wlc_without_mtrl cur_wlc_without_mtrl%ROWTYPE;
13501 	l_cur_mtrl_wlc_qty_mismatch cur_mtrl_wlc_qty_mismatch%ROWTYPE;
13502 
13503 
13504   BEGIN
13505 
13506     l_user_id := fnd_global.user_id;
13507     IF (l_debug = 1) THEN
13508       DEBUG(' Unload ER proceedure Enteredat at   ' || TO_CHAR(SYSDATE,
13509       'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
13510       DEBUG(' Unload LPN is   => '||p_lpn_id , 4);
13511       DEBUG(' Unload LPN user_id   => '||l_user_id , 4);
13512     END IF;
13513 
13514     l_return_status := fnd_api.g_ret_sts_success;
13515 
13516     l_progress      := 1 ;
13517 
13518 
13519       SELECT
13520         organization_id
13521       INTO
13522         l_org_id
13523       FROM
13524         wms_license_plate_numbers
13525       WHERE
13526         lpn_id = p_lpn_id ;
13527 
13528 
13529     OPEN cur_mtrl_wlc_qty_mismatch(l_org_id);
13530     FETCH
13531       cur_mtrl_wlc_qty_mismatch
13532     INTO
13533       l_cur_mtrl_wlc_qty_mismatch ;
13534 
13535     l_progress := 2 ;
13536 
13537       IF (cur_mtrl_wlc_qty_mismatch%FOUND) THEN
13538       fnd_message.set_name('WMS', 'WMS_MO_NOT_FOUND');
13539       fnd_msg_pub.add;
13540       RAISE fnd_api.g_exc_error;
13541 
13542       ELSE
13543       IF (l_debug = 1) THEN
13544         DEBUG(' Move order exists for wlc qty for lpn_id:   => '||p_lpn_id , 4);
13545       END IF;
13546 
13547 		 IF cur_mtrl_wlc_qty_mismatch%ISOPEN THEN
13548           CLOSE cur_mtrl_wlc_qty_mismatch;
13549          END IF;
13550 
13551      END IF;
13552 
13553     l_progress := 3 ;
13554 
13555     OPEN cur_wlc_without_mtrl(l_org_id) ;
13556     FETCH
13557       cur_wlc_without_mtrl
13558     INTO
13559       l_cur_wlc_without_mtrl ;
13560 
13561     IF (cur_wlc_without_mtrl%FOUND) THEN
13562       fnd_message.set_name('WMS', 'WMS_MO_NOT_FOUND');
13563       --fnd_message.set_token('LPN', l_lpn);
13564       fnd_msg_pub.add;
13565       RAISE fnd_api.g_exc_error;
13566       ELSE
13567       IF (l_debug = 1) THEN
13568         DEBUG(' Move order exis for the lpn_id.   => '||p_lpn_id , 4);
13569       END IF;
13570 
13571 	   IF cur_wlc_without_mtrl%ISOPEN THEN
13572           CLOSE cur_wlc_without_mtrl;
13573          END IF;
13574 
13575       END IF;
13576 
13577     OPEN cur_mtrl_wlpn_mismatch(l_org_id) ;
13578     FETCH
13579       cur_mtrl_wlpn_mismatch
13580     INTO
13581       l_cur_mtrl_wlpn_mismatch;
13582     l_progress := 4 ;
13583     IF cur_mtrl_wlpn_mismatch%FOUND THEN
13584       fnd_message.set_name('WMS', 'WMS_LPN_SUBLOC_MISMATCH');
13585       --fnd_message.set_token('LPN', l_lpn);
13586       fnd_msg_pub.add;
13587       RAISE fnd_api.g_exc_error;
13588       ELSE
13589       IF (l_debug = 1) THEN
13590         DEBUG(' There is no Sub-Inventory Mismatch between MTRL and WLC   => '||p_lpn_id , 4);
13591       END IF;
13592          IF cur_mtrl_wlpn_mismatch%ISOPEN THEN
13593           CLOSE cur_mtrl_wlpn_mismatch;
13594          END IF;
13595 
13596       END IF;
13597 
13598     FOR i IN c_all_items(l_org_id)
13599     LOOP
13600       IF ( inv_cache.set_item_rec(l_org_id,i.inventory_item_id)) THEN
13601         l_lot_control_code    := inv_cache.item_rec.lot_control_code ;
13602         IF (l_lot_control_code = 2 )THEN
13603           l_progress          := '5';
13604           IF (l_debug = 1) THEN
13605           DEBUG(' inside loop for lpn => '||p_lpn_id , 4);
13606           DEBUG(' inside loop for inventory_item_id  => '||i.inventory_item_id
13607           , 4);
13608           END IF;
13609           OPEN cur_mtrl_wlc_lot_mismatch(i.lpn_id,i.inventory_item_id,l_org_id) ;
13610           FETCH
13611             cur_mtrl_wlc_lot_mismatch
13612           INTO
13613             l_cur_mtrl_wlc_lot_mismatch ;
13614           IF (cur_mtrl_wlc_lot_mismatch%found) THEN
13615             fnd_message.set_name('WMS', 'WMS_CONT_INVALID_LOT');
13616             --fnd_message.set_token('LPN', l_lpn);
13617             fnd_msg_pub.add;
13618             RAISE fnd_api.g_exc_error;
13619 
13620             ELSE
13621             IF (l_debug = 1) THEN
13622               DEBUG(' There is no Lot Mismatch between MTRL and WLC   => '||p_lpn_id , 4);
13623             END IF;
13624 
13625 			 IF cur_mtrl_wlc_lot_mismatch%ISOPEN THEN
13626               CLOSE cur_mtrl_wlc_lot_mismatch;
13627              END IF;
13628 
13629           END IF ;
13630         END IF ;
13631       END IF ;
13632     END LOOP ;   --end loop c_all_items
13633 
13634 	   IF c_all_items%ISOPEN THEN
13635           CLOSE c_all_items;
13636          END IF;
13637 
13638     l_cal_api := FALSE;
13639 
13640 
13641     OPEN c_all_mmtt_cursor(l_org_id);
13642     l_progress := '6';
13643     LOOP -- c_all_mmtt_cursor loop
13644       EXIT
13645     WHEN c_all_mmtt_cursor%NOTFOUND;
13646       l_progress := '7';
13647       FETCH
13648         c_all_mmtt_cursor BULK COLLECT
13649       INTO
13650         l_tempid_tab LIMIT l_limit;
13651       l_progress := '8';
13652 
13653       l_progress := '9';
13654       -- Activate each MMTT's
13655       FOR i IN 1 .. l_tempid_tab.COUNT
13656       LOOP --All temp ids loop
13657 
13658        l_cal_api := TRUE;
13659          IF (l_debug = 1) THEN
13660           DEBUG('Calling cancel_operation_plan with ...',l_proc_name,9);
13661           DEBUG('p_source_task_id    => ' || l_tempid_tab(i),l_proc_name,9);
13662           DEBUG('p_activity_type_id  => ' || G_OP_ACTIVITY_INBOUND,l_proc_name,9);
13663         END IF;
13664 
13665 
13666         l_progress := '10';
13667         wms_atf_runtime_pub_apis.cancel_operation_plan ( p_source_task_id =>
13668         l_tempid_tab(i) ,p_activity_type_id => G_OP_ACTIVITY_INBOUND ,
13669         x_return_status => l_return_status ,x_msg_data => x_message ,
13670         x_msg_count => l_msg_count ,x_error_code => l_error_code );
13671         l_progress         := '310';
13672         IF (l_return_status = fnd_api.g_ret_sts_success) THEN
13673           --Updating WLC and Deleting RTI manually as we are not doing  it as part of
13674           -- cancel_operation_plan call
13675          UPDATE
13676            wms_lpn_contents
13677          SET
13678            source_name      = NULL,
13679            source_header_id =NULL,
13680            last_updated_by  = l_user_id,
13681           last_update_date = sysdate
13682         WHERE
13683           parent_lpn_id IN
13684         (
13685             SELECT
13686               lpn_id
13687             FROM
13688               wms_license_plate_numbers
13689               START WITH lpn_id       = p_lpn_id
13690               CONNECT BY PRIOR lpn_id = parent_lpn_id
13691         );
13692 
13693 	-- bug:15926098
13694 	DELETE FROM rcv_transactions_interface
13695         WHERE  lpn_id IN ( p_lpn_id )
13696                OR transfer_lpn_id IN ( p_lpn_id )
13697                   AND from_organization_id = l_org_id
13698                   AND transaction_type <> 'SHIP'
13699                   AND processing_status_code <> 'WSH_INTERFACED';
13700 
13701        UPDATE
13702           mtl_txn_request_lines
13703          SET
13704           quantity_delivered = 0,
13705           quantity_detailed  = 0,
13706           line_status        = 7,
13707           wms_process_flag   = 1
13708         WHERE
13709          line_id IN
13710          (
13711            SELECT
13712             line_id
13713            FROM
13714             mtl_txn_request_lines
13715            WHERE
13716            lpn_id IN
13717           (
13718            SELECT
13719             lpn_id
13720            FROM
13721             wms_license_plate_numbers
13722             START WITH lpn_id      IN (p_lpn_id)
13723             CONNECT BY PRIOR lpn_id = parent_lpn_id
13724         )
13725       AND organization_id                    = l_org_id
13726       AND line_status                        = 7
13727       AND quantity-NVL(quantity_delivered,0) > 0
13728     );
13729 
13730     -- End bug:15926098
13731 
13732 
13733           -- x_ret_value := 1;
13734           x_return_status := fnd_api.g_ret_sts_success;
13735         ELSE
13736         IF (l_debug = 1) THEN
13737           DEBUG(l_error_code || ' Error in cancel_operation_plane ' ,l_proc_name,1);
13738         END IF;
13739 
13740           l_progress := '11';
13741           --  x_ret_value := 1;
13742         END IF ;
13743       END LOOP; --All temp ids loop
13744     END LOOP;   -- c_all_mmtt_cursor loop
13745     IF c_all_mmtt_cursor%ISOPEN THEN
13746       CLOSE c_all_mmtt_cursor;
13747     END IF;
13748 
13749     IF ( (l_return_status <> fnd_api.g_ret_sts_success) OR l_cal_api = FALSE) THEN
13750       l_progress          := '12';
13751       IF (l_debug = 1) THEN
13752         DEBUG('Calling Manual Putaway Cleanup..',l_proc_name,9);
13753       END IF;
13754 
13755 
13756 
13757 --delete WOPI
13758   DELETE
13759   FROM
13760     wms_op_plan_instances
13761   WHERE
13762     source_task_id IN
13763     (
13764       SELECT
13765         parent_line_id
13766       FROM
13767         mtl_material_transactions_temp
13768       WHERE
13769         move_order_line_id IN
13770         (
13771           SELECT
13772             line_id
13773           FROM
13774             mtl_txn_request_lines
13775           WHERE
13776             lpn_id IN
13777             (
13778               SELECT
13779                 lpn_id
13780               FROM
13781                 wms_license_plate_numbers
13782                 START WITH lpn_id      IN (p_lpn_id)
13783                 CONNECT BY PRIOR lpn_id = parent_lpn_id
13784             )
13785           AND organization_id                    = l_org_id
13786           AND line_status                        = 7
13787           AND quantity-NVL(quantity_delivered,0) > 0
13788         )
13789   ) ;
13790 --delete WOOI
13791   DELETE
13792   FROM
13793     wms_op_operation_instances
13794   WHERE
13795     source_task_id IN
13796     (
13797       SELECT
13798         transaction_temp_id
13799       FROM
13800         mtl_material_transactions_temp
13801       WHERE
13802         move_order_line_id IN
13803         (
13804           SELECT
13805             line_id
13806           FROM
13807             mtl_txn_request_lines
13808           WHERE
13809             lpn_id IN
13810             (
13811               SELECT
13812                 lpn_id
13813               FROM
13814                 wms_license_plate_numbers
13815                 START WITH lpn_id      IN (p_lpn_id)
13816                 CONNECT BY PRIOR lpn_id = parent_lpn_id
13817             )
13818           AND organization_id                    = l_org_id
13819           AND line_status                        = 7
13820           AND quantity-NVL(quantity_delivered,0) > 0
13821         )
13822     ) ;
13823   --delete WDT
13824   DELETE
13825   FROM
13826     wms_dispatched_tasks
13827   WHERE
13828     transaction_temp_id IN
13829     (
13830       SELECT
13831         transaction_temp_id
13832       FROM
13833         mtl_material_transactions_temp
13834       WHERE
13835         move_order_line_id IN
13836         (
13837           SELECT
13838             line_id
13839           FROM
13840             mtl_txn_request_lines
13841           WHERE
13842             lpn_id IN
13843             (
13844               SELECT
13845                 lpn_id
13846               FROM
13847                 wms_license_plate_numbers
13848                 START WITH lpn_id      IN (p_lpn_id)
13849                 CONNECT BY PRIOR lpn_id = parent_lpn_id
13850             )
13851           AND organization_id                    = l_org_id
13852           AND line_status                        = 7
13853           AND quantity-NVL(quantity_delivered,0) > 0
13854         )
13855   );
13856 --delete MMTT
13857   DELETE
13858   FROM
13859     mtl_material_transactions_temp
13860   WHERE
13861     transaction_temp_id IN
13862     (
13863       SELECT
13864         parent_line_id
13865       FROM
13866         mtl_material_transactions_temp
13867       WHERE
13868         move_order_line_id IN
13869         (
13870           SELECT
13871             line_id
13872           FROM
13873             mtl_txn_request_lines
13874           WHERE
13875             lpn_id IN
13876             (
13877               SELECT
13878                 lpn_id
13879               FROM
13880                 wms_license_plate_numbers
13881                 START WITH lpn_id      IN (p_lpn_id)
13882                 CONNECT BY PRIOR lpn_id = parent_lpn_id
13883             )
13884           AND organization_id                    = l_org_id
13885           AND line_status                        = 7
13886           AND quantity-NVL(quantity_delivered,0) > 0
13887         )
13888     ) ;
13889 
13890 	--Delete MSNI
13891   DELETE
13892   FROM
13893     mtl_serial_numbers_interface
13894   WHERE
13895     product_transaction_id IN
13896     (
13897       SELECT
13898         interface_transaction_id
13899       FROM
13900         rcv_transactions_interface
13901       WHERE
13902         lpn_id           IN (p_lpn_id)
13903       OR transfer_lpn_id IN (p_lpn_id)
13904     );
13905   --Delete RSI
13906   DELETE
13907   FROM
13908     rcv_serials_interface
13909   WHERE
13910     interface_transaction_id IN
13911     (
13912       SELECT
13913         interface_transaction_id
13914       FROM
13915         rcv_transactions_interface
13916       WHERE
13917         lpn_id           IN (p_lpn_id)
13918       OR transfer_lpn_id IN (p_lpn_id)
13919     );
13920   --Delete MSNT
13921   DELETE
13922   FROM
13923     mtl_serial_numbers_temp
13924   WHERE
13925     transaction_temp_id IN
13926     (
13927 
13928        SELECT
13929         transaction_temp_id
13930       FROM
13931         mtl_material_transactions_temp
13932       WHERE
13933         move_order_line_id IN
13934         (
13935           SELECT
13936             line_id
13937           FROM
13938             mtl_txn_request_lines
13939           WHERE
13940             lpn_id IN
13941             (
13942               SELECT
13943                 lpn_id
13944               FROM
13945                 wms_license_plate_numbers
13946                 START WITH lpn_id      IN (p_lpn_id)
13947                 CONNECT BY PRIOR lpn_id = parent_lpn_id
13948             )
13949           AND organization_id                    = l_org_id
13950           AND line_status                        = 7
13951           AND quantity-NVL(quantity_delivered,0) > 0
13952         )
13953 
13954     );
13955 
13956 	--Delete MSNT based on Serial_transaction temp of MTLT
13957 
13958 	 DELETE
13959   FROM
13960     mtl_serial_numbers_temp
13961   WHERE
13962     transaction_temp_id IN
13963     (
13964       SELECT
13965         serial_transaction_temp_id
13966       FROM
13967         mtl_transaction_lots_temp
13968       WHERE
13969         transaction_temp_id IN
13970     (
13971 
13972         SELECT
13973         transaction_temp_id
13974       FROM
13975         mtl_material_transactions_temp
13976       WHERE
13977         move_order_line_id IN
13978         (
13979           SELECT
13980             line_id
13981           FROM
13982             mtl_txn_request_lines
13983           WHERE
13984             lpn_id IN
13985             (
13986               SELECT
13987                 lpn_id
13988               FROM
13989                 wms_license_plate_numbers
13990                 START WITH lpn_id      IN (p_lpn_id)
13991                 CONNECT BY PRIOR lpn_id = parent_lpn_id
13992             )
13993           AND organization_id                    = l_org_id
13994           AND line_status                        = 7
13995           AND quantity-NVL(quantity_delivered,0) > 0
13996         )
13997     )
13998 
13999     );
14000 
14001 	 --delete MTLI
14002   DELETE
14003   FROM
14004     mtl_transaction_lots_interface
14005   WHERE
14006     lot_number IN
14007     (
14008       SELECT
14009         lot_number
14010       FROM
14011         wms_lpn_contents
14012       WHERE
14013         parent_lpn_id IN (p_lpn_id)
14014     );
14015   --delete RLI
14016   DELETE
14017   FROM
14018     rcv_lots_interface
14019   WHERE
14020     interface_transaction_id IN
14021     (
14022       SELECT
14023         interface_transaction_id
14024       FROM
14025         rcv_transactions_interface
14026       WHERE
14027         lpn_id           IN (p_lpn_id)
14028       OR transfer_lpn_id IN (p_lpn_id)
14029     );
14030   --delete MTLT
14031   DELETE
14032   FROM
14033     mtl_transaction_lots_temp
14034   WHERE
14035     transaction_temp_id IN
14036     (
14037 
14038         SELECT
14039         transaction_temp_id
14040       FROM
14041         mtl_material_transactions_temp
14042       WHERE
14043         move_order_line_id IN
14044         (
14045           SELECT
14046             line_id
14047           FROM
14048             mtl_txn_request_lines
14049           WHERE
14050             lpn_id IN
14051             (
14052               SELECT
14053                 lpn_id
14054               FROM
14055                 wms_license_plate_numbers
14056                 START WITH lpn_id      IN (p_lpn_id)
14057                 CONNECT BY PRIOR lpn_id = parent_lpn_id
14058             )
14059           AND organization_id                    = l_org_id
14060           AND line_status                        = 7
14061           AND quantity-NVL(quantity_delivered,0) > 0
14062         )
14063     );
14064 
14065   --delete MMTT
14066   DELETE
14067   FROM
14068     mtl_material_transactions_temp
14069   WHERE
14070     transaction_temp_id IN
14071     (
14072       SELECT
14073         transaction_temp_id
14074       FROM
14075         mtl_material_transactions_temp
14076       WHERE
14077         move_order_line_id IN
14078         (
14079           SELECT
14080             line_id
14081           FROM
14082             mtl_txn_request_lines
14083           WHERE
14084             lpn_id IN
14085             (
14086               SELECT
14087                 lpn_id
14088               FROM
14089                 wms_license_plate_numbers
14090                 START WITH lpn_id      IN (p_lpn_id)
14091                 CONNECT BY PRIOR lpn_id = parent_lpn_id
14092             )
14093           AND organization_id                    = l_org_id
14094           AND line_status                        = 7
14095           AND quantity-NVL(quantity_delivered,0) > 0
14096         )
14097     ) ;
14098 
14099 
14100   --Skipping RTI of transaction type 'SHIP'
14101   DELETE
14102   FROM
14103     rcv_transactions_interface
14104   WHERE
14105     lpn_id                   IN (p_lpn_id)
14106   OR transfer_lpn_id         IN (p_lpn_id)
14107   AND from_organization_id    = l_org_id
14108   AND transaction_type       <> 'SHIP'
14109   AND PROCESSING_STATUS_CODE <> 'WSH_INTERFACED';
14110   --update MTRL
14111   UPDATE
14112     mtl_txn_request_lines
14113   SET
14114     quantity_delivered = 0,
14115     quantity_detailed  = 0,
14116     line_status        = 7,
14117     wms_process_flag   = 1
14118   WHERE
14119     line_id IN
14120     (
14121       SELECT
14122         line_id
14123       FROM
14124         mtl_txn_request_lines
14125       WHERE
14126         lpn_id IN
14127         (
14128           SELECT
14129             lpn_id
14130           FROM
14131             wms_license_plate_numbers
14132             START WITH lpn_id      IN (p_lpn_id)
14133             CONNECT BY PRIOR lpn_id = parent_lpn_id
14134         )
14135       AND organization_id                    = l_org_id
14136       AND line_status                        = 7
14137       AND quantity-NVL(quantity_delivered,0) > 0
14138     );
14139   UPDATE
14140     wms_lpn_contents
14141   SET
14142     source_header_id = NULL,
14143     source_name      = NULL
14144   WHERE
14145     parent_lpn_id IN (p_lpn_id);
14146   --update msn
14147   UPDATE
14148     mtl_serial_numbers
14149   SET
14150     group_mark_id = NULL,
14151     line_mark_id  = NULL
14152   WHERE
14153     serial_number IN
14154     (
14155       SELECT
14156         serial_number
14157       FROM
14158         mtl_serial_numbers
14159       WHERE
14160         lpn_id IN (p_lpn_id)
14161     );
14162 
14163 
14164  x_return_status := fnd_api.g_ret_sts_success;
14165 
14166  END IF ;
14167 
14168  COMMIT;
14169 
14170   EXCEPTION
14171   WHEN FND_API.G_EXC_ERROR THEN
14172   IF (l_debug = 1) THEN
14173     DEBUG('Expected Error at '||l_progress, l_proc_name,1);
14174   END IF;
14175 
14176     IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_error) THEN
14177       fnd_msg_pub.add_exc_msg(g_pkg_name, l_proc_name, SQLERRM);
14178     END IF;
14179     x_return_status := fnd_api.g_ret_sts_error;
14180 
14181         IF c_all_mmtt_cursor%ISOPEN THEN
14182       CLOSE c_all_mmtt_cursor;
14183     END IF;
14184     IF cur_mtrl_wlpn_mismatch%ISOPEN THEN
14185       CLOSE cur_mtrl_wlpn_mismatch;
14186     END IF;
14187   IF cur_mtrl_wlc_lot_mismatch%ISOPEN THEN
14188       CLOSE cur_mtrl_wlc_lot_mismatch;
14189     END IF;
14190   IF cur_wlc_without_mtrl%ISOPEN THEN
14191       CLOSE cur_wlc_without_mtrl;
14192     END IF;
14193     IF cur_mtrl_wlc_qty_mismatch%ISOPEN THEN
14194       CLOSE cur_mtrl_wlc_qty_mismatch;
14195     END IF;
14196 	 IF c_all_items%ISOPEN THEN
14197         CLOSE c_all_items;
14198      END IF;
14199 
14200 
14201   WHEN OTHERS THEN
14202 
14203   IF (l_debug = 1) THEN
14204     DEBUG('Error occured at l_progress  => '||l_progress , 4);
14205     DEBUG(' Exception occured  = ' || x_message || ' during ' || TO_CHAR(
14206     SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
14207     DEBUG(SQLERRM,l_proc_name,1);
14208   END IF;
14209     --x_ret_value := -1;
14210     x_return_status := fnd_api.g_ret_sts_unexp_error;
14211 
14212      IF c_all_mmtt_cursor%ISOPEN THEN
14213       CLOSE c_all_mmtt_cursor;
14214     END IF;
14215     IF cur_mtrl_wlpn_mismatch%ISOPEN THEN
14216       CLOSE cur_mtrl_wlpn_mismatch;
14217     END IF;
14218   IF cur_mtrl_wlc_lot_mismatch%ISOPEN THEN
14219       CLOSE cur_mtrl_wlc_lot_mismatch;
14220     END IF;
14221   IF cur_wlc_without_mtrl%ISOPEN THEN
14222       CLOSE cur_wlc_without_mtrl;
14223     END IF;
14224     IF cur_mtrl_wlc_qty_mismatch%ISOPEN THEN
14225       CLOSE cur_mtrl_wlc_qty_mismatch;
14226     END IF;
14227 	IF c_all_items%ISOPEN THEN
14228         CLOSE c_all_items;
14229      END IF;
14230 
14231 
14232  END unload_task ; --End of Unload ER Changes:13797542
14233 
14234 
14235 
14236 
14237 PROCEDURE get_crossdock_info
14238   (p_lpn_id                 IN NUMBER,
14239    p_organization_id        IN NUMBER,
14240    x_back_order_delivery_id OUT nocopy NUMBER,
14241    x_crossdock_type         OUT nocopy NUMBER,
14242    x_wip_supply_type        OUT nocopy NUMBER )
14243   IS
14244      l_back_order_delivery_id NUMBER := -1;
14245      l_crossdock_type         NUMBER := -1;
14246      l_wip_supply_type        NUMBER := -1;
14247      l_backorder_delivery_ids num_tab;
14248      l_crossdock_types         num_tab;
14249      l_wip_supply_types        num_tab;
14250      l_debug     NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'),
14251 				     0);
14252      l_progress VARCHAR(5) := '0';
14253 
14254 BEGIN
14255    IF (l_debug = 1) THEN
14256       debug('Entering get_crossdock_into:','get_crossdock_info',9);
14257       debug('  p_lpn_id ==========> ' || p_lpn_id, 'get_crossdock_info',9);
14258       debug('  p_organization_id =>'||p_organization_id,'get_crossdock_info',9);
14259    END IF;
14260 
14261    l_progress := '10';
14262 
14263    BEGIN
14264       SELECT
14265 	DISTINCT
14266 	Nvl(mol.backorder_delivery_detail_id,-1) back_order_delivery_detail_id,
14267 	Nvl(mol.crossdock_type,1) crossdock_type,
14268 	Nvl(mmtt.wip_supply_type,0) wip_supply_type
14269 	bulk collect INTO
14270 	l_backorder_delivery_ids,
14271 	l_crossdock_types,
14272 	l_wip_supply_types
14273 	FROM
14274 	mtl_txn_request_lines mol,
14275 	mtl_material_transactions_temp mmtt ,
14276 	(SELECT wlpn.lpn_id  /*5723418*/
14277 		FROM   wms_license_plate_numbers wlpn
14278 		START WITH wlpn.lpn_id = p_lpn_id
14279 		CONNECT BY PRIOR wlpn.lpn_id = wlpn.parent_lpn_id ) wlpn
14280 	WHERE 	mol.lpn_id = wlpn.lpn_id
14281 	AND mol.organization_id = p_organization_id
14282 	AND mol.line_id = mmtt.move_order_line_id
14283 	AND mmtt.organization_id = p_organization_id
14284 	ORDER BY wip_supply_type DESC;
14285    EXCEPTION
14286       WHEN OTHERS THEN
14287 	 IF (l_debug = 1) THEN
14288 	    debug('MMTT exists. Treat it as no crossdock','get_crossdock_info',9);
14289 	 END IF;
14290    END;
14291 
14292    l_progress := '20';
14293 
14294    l_crossdock_type := 0;
14295 
14296    FOR i IN 1 .. l_backorder_delivery_ids.COUNT LOOP
14297       IF l_wip_supply_types(i) = 1 THEN
14298 	 l_crossdock_type := 3; -- WIP PUSH overrides all other types
14299 	 EXIT;
14300       END IF;
14301 
14302       IF l_backorder_delivery_ids(i) <> -1 AND l_crossdock_types(i) = 1 THEN
14303 	 IF (l_crossdock_type = 0) THEN
14304 	    l_crossdock_type := 1; -- SO xdock.
14305 	  ELSIF (l_crossdock_type = 2) THEN
14306 	    l_crossdock_type := 4; -- Both SO xdock and WIP Pulled XDOCK
14307 	    -- No need to look further, since if there
14308 	    -- is a WIP push, it should have been already processed
14309 	    EXIT;
14310 	 END IF;
14311       END IF;
14312 
14313       IF l_backorder_delivery_ids(i) <> -1 AND l_crossdock_types(i) = 2 THEN
14314 	 -- If it comes here, then it must by a WIP PULL xdock
14315 	 IF (l_crossdock_type = 0) THEN
14316 	    l_crossdock_type := 2;
14317 	  ELSIF (l_crossdock_type = 1) THEN
14318 	    l_crossdock_type := 4; -- Both SO xdock and WIP Pulled XDOCK
14319 	    EXIT;
14320 	 END IF;
14321       END IF;
14322    END LOOP;
14323 
14324    l_progress := '30';
14325 
14326    x_back_order_delivery_id := -9999;
14327    x_crossdock_type := l_crossdock_type;
14328    x_wip_supply_type :=  -9999;
14329 
14330    IF (l_debug = 1) THEN
14331       debug('Exitting get_crossdock_into:','get_crossdock_info',9);
14332       debug('  x_backorder_delivery_detail_id ==========> ' || l_back_order_delivery_id, 'get_crossdock_info',9);
14333       debug('  x_crossdock_type =>'||l_crossdock_type,'get_crossdock_info',9);
14334       debug('  x_wip_supply_type =>'||l_wip_supply_type,'get_crossdock_info',9);
14335    END IF;
14336 EXCEPTION
14337    WHEN OTHERS THEN
14338       IF (l_debug = 1) THEN
14339 	 debug('Exception occured after progress' || l_progress,'get_crossdock_info',9);
14340       END IF;
14341 
14342       NULL;
14343 END get_crossdock_info;
14344 
14345 PROCEDURE update_loc_suggested_capacity(
14346     x_return_status    OUT NOCOPY     VARCHAR2
14347   , x_msg_count        OUT NOCOPY     NUMBER
14348   , x_msg_data         OUT NOCOPY     VARCHAR2
14349   , p_organization_id  IN             NUMBER
14350   , p_lpn_id           IN             NUMBER
14351   , p_location_id      IN             NUMBER
14352 ) IS
14353     l_api_name CONSTANT VARCHAR2(30) := 'update_loc_suggested_capacity';
14354     l_progress          VARCHAR2(10);
14355     l_item_id           NUMBER;
14356     l_locator_id        NUMBER;
14357     l_quantity          NUMBER;
14358     l_uom_code          VARCHAR2(3);
14359     l_debug             NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
14360 
14361     CURSOR l_suggestions_cursor IS
14362        --BUG 3435079: Use organization_id MTRL and MMTT
14363        --for performance reason
14364        SELECT mmtt.inventory_item_id
14365            , mmtt.locator_id
14366            , mmtt.transaction_quantity
14367            , mmtt.transaction_uom
14368       FROM   mtl_material_transactions_temp mmtt,
14369              mtl_txn_request_lines mtrl ,
14370 	     (SELECT lpn_id     /*5723418*/
14371                    FROM wms_license_plate_numbers
14372 		   START WITH lpn_id = p_lpn_id
14373 		   CONNECT BY PRIOR lpn_id = parent_lpn_id) wlpn
14374 	WHERE  mmtt.move_order_line_id = mtrl.line_id
14375 	AND  mmtt.organization_id = p_organization_id
14376 	AND  mtrl.organization_id = p_organization_id
14377         AND  mtrl.lpn_id  = wlpn.lpn_id
14378         AND  NVL(mmtt.wms_task_type, 0) <> -1;
14379 
14380   BEGIN
14381     IF (l_debug = 1) THEN
14382       debug('***Calling WMSPUTLB.update_loc_suggested_capacity***');
14383       debug('Org ID: => ' || p_organization_id);
14384       debug('LPN ID: => ' || p_lpn_id);
14385       debug('USER ENTERED LOCATOR ID: => ' || p_location_id);
14386     END IF;
14387 
14388     -- Set the savepoint
14389     SAVEPOINT update_capacity_sp;
14390     l_progress := '10';
14391     -- Loop through each suggested MMTT line for the LPN
14392     OPEN l_suggestions_cursor;
14393 
14394     LOOP
14395       FETCH l_suggestions_cursor INTO l_item_id, l_locator_id, l_quantity, l_uom_code;
14396       EXIT WHEN l_suggestions_cursor%NOTFOUND;
14397 
14398       IF (l_debug = 1) THEN
14399         debug('Current MMTT suggestion values:');
14400         debug('Inventory Item ID: => ' || l_item_id);
14401         debug('Locator ID: ========> ' || l_locator_id);
14402         debug('Transaction qty: ===> ' || l_quantity);
14403         debug('Transaction UOM: ===> ' || l_uom_code);
14404       END IF;
14405 
14406       l_progress := '20';
14407 
14408       IF (l_debug = 1) THEN
14409         debug('Call INV_LOC_WMS_UTILS.update_loc_suggested_capacity API');
14410       END IF;
14411 
14412       inv_loc_wms_utils.update_loc_suggested_capacity(
14413         x_return_status             => x_return_status
14414       , x_msg_count                 => x_msg_count
14415       , x_msg_data                  => x_msg_data
14416       , p_organization_id           => p_organization_id
14417       , p_inventory_location_id     => p_location_id
14418       , p_inventory_item_id         => l_item_id
14419       , p_primary_uom_flag          => 'N'
14420       , p_transaction_uom_code      => l_uom_code
14421       , p_quantity                  => l_quantity
14422       );
14423       l_progress := '30';
14424 
14425       IF (x_return_status = fnd_api.g_ret_sts_success) THEN
14426         IF (l_debug = 1) THEN
14427           debug('Success returned from update_loc_suggested_capacity API');
14428         END IF;
14429       ELSE
14430         IF (l_debug = 1) THEN
14431           debug('Failure returned from update_loc_suggested_capacity API');
14432         END IF;
14433         -- Bug 5393727: do not raise an exception if locator API returns an error
14434         -- RAISE fnd_api.g_exc_error;
14435       END IF;
14436 
14437       l_progress := '40';
14438     END LOOP;
14439 
14440     CLOSE l_suggestions_cursor;
14441     -- Set the output variable
14442     x_return_status := fnd_api.g_ret_sts_success;
14443     l_progress := '50';
14444 
14445     IF (l_debug = 1) THEN
14446       debug('***End of update_loc_suggested_capacity***');
14447     END IF;
14448   EXCEPTION
14449     WHEN fnd_api.g_exc_error THEN
14450       ROLLBACK TO update_capacity_sp;
14451       x_return_status := fnd_api.g_ret_sts_error;
14452       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
14453 
14454       IF (l_debug = 1) THEN
14455         debug(
14456           'Exiting update_loc_suggested_capacity - Execution error: ' || l_progress || ' '
14457           || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS')
14458         );
14459       END IF;
14460     WHEN fnd_api.g_exc_unexpected_error THEN
14461       ROLLBACK TO update_capacity_sp;
14462       x_return_status := fnd_api.g_ret_sts_unexp_error;
14463       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
14464 
14465       IF (l_debug = 1) THEN
14466         debug(
14467           'Exiting update_loc_suggested_capacity - Unexpected error: ' || l_progress || ' '
14468           || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS')
14469         );
14470       END IF;
14471     WHEN OTHERS THEN
14472       ROLLBACK TO update_capacity_sp;
14473       x_return_status := fnd_api.g_ret_sts_unexp_error;
14474 
14475       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
14476         fnd_msg_pub.add_exc_msg(g_pkg_name, l_api_name);
14477       END IF;
14478 
14479       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
14480 
14481       IF (l_debug = 1) THEN
14482         debug(
14483           'Exiting update_loc_suggested_capacity - Others exception: ' || l_progress || ' '
14484           || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS')
14485         );
14486       END IF;
14487 END update_loc_suggested_capacity;
14488 
14489 PROCEDURE validate_material_status
14490   (x_passed             OUT NOCOPY     VARCHAR2
14491    , p_organization_id  IN             NUMBER
14492    , p_sub              IN             VARCHAR2
14493    , p_loc              IN             NUMBER
14494    ) IS
14495       l_debug     NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
14496       l_proc_name VARCHAR2(30) := 'validate_material_status';
14497 BEGIN
14498    IF (l_debug = 1) THEN
14499       DEBUG(' Function Entered at ' ||
14500 	    to_char(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),l_proc_name,1);
14501       DEBUG(' p_organization_id   => '||p_organization_id,l_proc_name , 4);
14502       DEBUG(' p_sub => '||p_sub,l_proc_name, 4);
14503       DEBUG(' p_loc => '||p_loc,l_proc_name, 4);
14504    END IF;
14505 
14506    IF (inv_material_status_grp.is_status_applicable('TRUE',
14507                         NULL,
14508                         2,--subtransfer
14509                         NULL,
14510                         NULL,
14511                         p_organization_id,
14512                         NULL,
14513                         p_sub,
14514                         p_loc,
14515                         NULL,
14516                         NULL,
14517                         'Z') ='Y' AND
14518 	    inv_material_status_grp.is_status_applicable('TRUE',
14519                         NULL,
14520 			2,--Sub Transfer
14521                         NULL,
14522                         NULL,
14523                         p_organization_id,
14524                         NULL,
14525                         p_sub,
14526                         p_loc,
14527                         NULL,
14528                         NULL,
14529                         'L') = 'Y') THEN
14530       x_passed := 'Y';
14531     ELSE
14532       x_passed := 'N';
14533    END IF;
14534    IF (l_debug = 1) THEN
14535       DEBUG(' x_passed => '||x_passed,l_proc_name, 4);
14536    END IF;
14537 
14538 EXCEPTION
14539    WHEN OTHERS THEN
14540       x_passed := 'Y';
14541 END validate_material_status;
14542 
14543 --9695544-Changed the code to call shipping api only for SO lines.
14544 FUNCTION populate_grouping_rows
14545            (p_lpn_id    IN NUMBER)
14546          RETURN NUMBER IS
14547 
14548         --Bug#6968476.Added xdock type.SO xdk type can be NULL
14549 	CURSOR c_lpn_bo_dd_id (v_lpn_id NUMBER)IS
14550      	SELECT NVL(backorder_delivery_detail_id,-1) , nvl(crossdock_type,1)
14551      	FROM   mtl_txn_request_lines
14552      	WHERE  lpn_id = v_lpn_id
14553 	AND    line_status = 7 ; --9505772 need only open lines.
14554 
14555         l_count         NUMBER;
14556 	l_count_so      NUMBER := 0;
14557         l_count_non_so  NUMBER := 0;
14558         l_bo_dd_id      NUMBER := 0 ;
14559         l_return_status VARCHAR2(1);
14560         l_progress NUMBER;
14561 	l_debug     NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
14562 	l_proc_name VARCHAR2(30) := 'populate_grouping_rows';
14563 	l_xdock_type    NUMBER;             --Bug#6968476
14564         l_non_so_lines WSH_UTIL_CORE.id_tab_type; --9505772-This table will hold non-so lines
14565 BEGIN
14566 
14567 	g_line_rows.delete;  --This table will hold So lines
14568         g_grouping_rows.delete;
14569         l_non_so_lines.delete;
14570 
14571 	IF (l_debug = 1) THEN
14572 		debug('populate_grouping_rows: p_lpn_id='||p_lpn_id);
14573 	END IF;
14574 
14575 	l_progress := 100;
14576 
14577 	OPEN c_lpn_bo_dd_id(p_lpn_id);
14578 
14579 	l_progress := 200;
14580 
14581 	LOOP
14582 		FETCH c_lpn_bo_dd_id INTO l_bo_dd_id, l_xdock_type;
14583 		EXIT WHEN c_lpn_bo_dd_id%NOTFOUND;
14584 		IF (l_debug = 1) THEN
14585 		   debug('populate_grouping_rows: l_bo_dd_id='||l_bo_dd_id||',crossdock_type:'||l_xdock_type);
14586 		END IF;
14587 
14588                 /*Store SO and non-SO lines seperately */
14589 		IF ( l_xdock_type = 1 AND l_bo_dd_id > 0 ) THEN --Bug#6968476
14590                      l_count_so := l_count_so + 1 ;
14591                      g_line_rows(l_count_so) := l_bo_dd_id;
14592                 ELSE
14593                      l_count_non_so  := l_count_non_so +1;
14594                      l_non_so_lines(l_count_non_so) := l_bo_dd_id;
14595                 END IF;
14596 	END LOOP;
14597 
14598 	CLOSE c_lpn_bo_dd_id;
14599 
14600 	l_progress := 300;
14601 
14602 	IF (l_count_so > 0 ) THEN --We are dealing with SO  xdock lines here.
14603 	   IF (l_debug = 1) THEN
14604 	    debug('populate_grouping_rows: g_line_rows.count='||g_line_rows.count);
14605 	    debug('populate_grouping_rows: before calling wsh_delivery_details_grp.get_carton_grouping');
14606 	   END IF;
14607 
14608 	   l_progress := 400;
14609 
14610 	   wsh_delivery_details_grp.get_carton_grouping(p_line_rows => g_line_rows,
14611                                                      x_grouping_rows => g_grouping_rows,
14612                                                      x_return_status => l_return_status);
14613 
14614 	   l_progress := 500;
14615 
14616 	   IF (l_debug = 1) THEN
14617 		debug('populate_grouping_rows: after calling wsh_delivery_details_grp.get_carton_grouping');
14618 		debug('populate_grouping_rows: g_grouping_rows.count='||g_grouping_rows.COUNT);
14619            END IF;
14620 
14621            IF (l_count_non_so > 0) THEN --If there are any non-so lines, lets append them
14622              l_count := g_grouping_rows.count ;
14623              FOR i IN 1..l_count_non_so LOOP
14624                 g_grouping_rows(l_count+i) := l_non_so_lines(i);
14625 		g_line_rows(g_line_rows.COUNT+1) := l_non_so_lines(i);
14626              END LOOP;
14627           END IF;
14628 
14629        ELSE --Not even a single SO WDD line involved
14630           g_grouping_rows := l_non_so_lines;
14631 	  g_line_rows     := l_non_so_lines;
14632        END IF;
14633        RETURN g_grouping_rows.COUNT;
14634 END populate_grouping_rows;
14635 
14636 FUNCTION get_grouping_count
14637 	   (p_line_rows  IN WSH_UTIL_CORE.id_tab_type)
14638 	RETURN NUMBER IS
14639 
14640 	l_count NUMBER;
14641 	l_bo_dd_id NUMBER := -1;
14642 	l_duplicate NUMBER := 0;
14643 	l_progress NUMBER;
14644         l_debug     NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
14645         l_proc_name VARCHAR2(30) := 'get_grouping_count';
14646         l_line_rows WSH_UTIL_CORE.id_tab_type;
14647         l_temp      NUMBER;
14648 
14649 BEGIN
14650         l_line_rows := p_line_rows ; --9695544
14651 	IF (l_debug = 1) THEN
14652 		debug('get_grouping_count: Inside get_grouping_count');
14653 	END IF;
14654 
14655         --9695544-before checking duplicates we need to sort them */
14656         FOR I IN 1..l_line_rows.COUNT-1 LOOP
14657          FOR J IN I+1..l_line_rows.COUNT LOOP
14658             IF (l_line_rows(I) > l_line_rows(J)) THEN
14659               l_temp          := l_line_rows(I);
14660               l_line_rows(I)  :=  l_line_rows(J);
14661               l_line_rows(J)  :=  l_temp;
14662             END IF;
14663           END LOOP;
14664          END LOOP;
14665 
14666         FOR l_count IN 1..l_line_rows.COUNT LOOP
14667 		IF (l_debug = 1) THEN
14668 			debug('get_grouping_count: l_bo_dd_id='||l_bo_dd_id);
14669                 	debug('get_grouping_count: l_line_rows(l_count)='||l_line_rows(l_count));
14670 		END IF;
14671 
14672 		IF (l_bo_dd_id <> l_line_rows(l_count)) THEN
14673                         l_duplicate := l_duplicate + 1;
14674                 END IF;
14675 
14676                 l_bo_dd_id := l_line_rows(l_count);
14677 	END LOOP;
14678 
14679 	IF (l_debug = 1) THEN
14680 		debug('get_grouping_count: l_duplicate='||l_duplicate);
14681 	END IF;
14682 
14683 	RETURN l_duplicate;
14684 
14685 END get_grouping_count;
14686 
14687 FUNCTION get_grouping
14688 	   (p_bo_dd_id IN NUMBER)
14689 	RETURN NUMBER IS
14690 
14691 	l_index NUMBER;
14692 	i NUMBER;
14693 	l_progress NUMBER;
14694         l_debug     NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
14695         l_proc_name VARCHAR2(30) := 'get_grouping';
14696 
14697 BEGIN
14698 	IF (l_debug = 1) THEN
14699 		debug('get_grouping: p_bo_dd_id='||p_bo_dd_id);
14700 	END IF;
14701 
14702 	<<get_grouping_loop>>
14703 	FOR i IN 1..g_line_rows.COUNT LOOP
14704 		IF (l_debug = 1) THEN
14705 			debug('get_grouping: g_line_rows(i)='||g_line_rows(i));
14706 		END IF;
14707 		IF (g_line_rows(i) = p_bo_dd_id) THEN
14708 			l_index := i;
14709 			debug('get_grouping: l_index='||l_index);
14710 			EXIT get_grouping_loop;
14711 		END IF;
14712 	END LOOP;
14713 
14714 	IF (l_debug = 1) THEN
14715 	   debug('get_grouping: l_index='||l_index);
14716 	   IF l_index IS NOT NULL THEN
14717 	      debug('get_grouping: g_grouping_rows(l_index)='||g_grouping_rows(l_index));
14718 	   END IF;
14719 	END IF;
14720 
14721 	IF l_index IS NOT NULL THEN
14722 	   RETURN g_grouping_rows(l_index);
14723 	 ELSE
14724 	   RETURN p_bo_dd_id;
14725 	END IF;
14726 
14727 END get_grouping;
14728 
14729 --Added following procedure for bug 7143123
14730  /**
14731     *  Cleanup_LPN_Crossdock_Wrapper, This procedure explodes the given LPN
14732     *  and for each LPN checks whether crossdock move order exists and if so clean up the crossdock related data.
14733     *  @param   p_org_id   Current Organization
14734     *  @param   p_lpn_id   From LPN ID
14735     *  @param   x_return_status
14736     **/
14737 
14738     PROCEDURE Cleanup_LPN_Crossdock_Wrapper(
14739       p_org_id         IN             NUMBER
14740     , p_lpn_id         IN             NUMBER
14741     , x_return_status  OUT NOCOPY     VARCHAR2
14742     )
14743      IS
14744       l_progress         NUMBER;
14745       l_lpn_id           NUMBER;
14746       l_tempid           NUMBER;
14747       l_debug            NUMBER           := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
14748       l_content_lpn      VARCHAR2(1)      := 'N';
14749       l_lpn_context      NUMBER;
14750       l_lpn_has_contents VARCHAR2(1)      := 'N';
14751       l_x_msg_count      NUMBER;
14752       l_error_code       NUMBER;
14753       l_x_msg_data       FND_NEW_MESSAGES.MESSAGE_TEXT%TYPE; --VARCHAR2(2000);
14754       l_proc_name        VARCHAR2(100)    := 'Cleanup_LPN_Crossdock_Wrapper';
14755 
14756 
14757       CURSOR lpn_cursor IS
14758 	SELECT     lpn_id
14759 	      FROM wms_license_plate_numbers
14760 	START WITH lpn_id = p_lpn_id
14761 	CONNECT BY parent_lpn_id = PRIOR lpn_id;
14762 
14763      CURSOR c_mmtt_cursor(p_lpn number) IS
14764 	 SELECT mmtt.transaction_temp_id,
14765 	  mmtt.parent_line_id,
14766 		mmtt.operation_plan_id,
14767 		mol.line_id move_order_line_id,
14768 		mmtt.operation_seq_num,
14769 		mmtt.repetitive_line_id,
14770 		mmtt.primary_quantity,
14771 		mmtt.inventory_item_id,
14772 		mol.crossdock_type,
14773 		mol.backorder_delivery_detail_id
14774 	   FROM  mtl_material_transactions_temp mmtt,
14775 		 mtl_txn_request_lines mol
14776 	  WHERE  mmtt.move_order_line_id(+) = mol.line_id
14777 	     AND mmtt.wms_task_type=2
14778 	     AND mol.organization_id = mmtt.organization_id(+)
14779 	     AND mol.organization_id=p_org_id
14780 	     AND mol.lpn_id = p_lpn
14781 	     AND mol.LINE_STATUS <> 5
14782 	     AND EXISTS (SELECT 1 FROM mtl_txn_request_lines
14783 			  WHERE lpn_id = p_lpn
14784 			   AND BACKORDER_DELIVERY_DETAIL_ID IS NOT NULL);
14785 
14786        l_mmtt_rec c_mmtt_cursor%ROWTYPE;
14787        PRAGMA AUTONOMOUS_TRANSACTION;
14788     BEGIN
14789 
14790       -- Intialize out variables.
14791       l_progress := 10;
14792       l_content_lpn := 'N';
14793       x_return_status := fnd_api.g_ret_sts_success;
14794 
14795       IF (l_debug = 1) THEN
14796 	debug(l_progress||'Patchset J code - Cleanup_LPN_Crossdock_Wrapper');
14797 	debug(l_progress||'p_org_id==> '|| p_org_id);
14798 	debug(l_progress||'p_lpn_id==> '|| p_lpn_id);
14799       END IF;
14800 
14801       -- Get LPN Context
14802       SELECT lpn_context
14803       INTO l_lpn_context
14804       FROM   wms_license_plate_numbers
14805       WHERE  lpn_id = p_lpn_id;
14806 
14807       l_progress := 20;
14808 
14809       OPEN lpn_cursor;
14810        l_progress := 30;
14811       LOOP
14812 	FETCH lpn_cursor INTO l_lpn_id;
14813 	EXIT WHEN lpn_cursor%NOTFOUND;
14814 	l_content_lpn      := 'N';
14815 
14816 	IF(l_debug = 1) THEN
14817 	  debug(l_progress||'cleaning crossdock of Lpn ' || l_lpn_id);
14818 	END IF;
14819        l_progress := 40;
14820 	-- Check lpn has contents
14821 	BEGIN
14822 	  SELECT 'Y','Y'
14823 	    INTO l_content_lpn,l_lpn_has_contents
14824 	    FROM wms_lpn_contents
14825 	   WHERE parent_lpn_id = l_lpn_id
14826 	     AND ROWNUM<2;
14827 	EXCEPTION
14828 	  WHEN no_data_found THEN
14829 	    l_content_lpn := 'N';
14830 	END;
14831 
14832 	IF l_debug = 1 THEN
14833 	  IF (l_content_lpn = 'N') THEN debug(l_progress||'lpn ' || l_lpn_id || 'has no contents'); END IF;
14834 	  IF (l_content_lpn = 'Y') THEN debug(l_progress||'lpn ' || l_lpn_id || 'has contents'); END IF;
14835 	END IF;
14836 
14837 	IF l_content_lpn ='Y' THEN
14838 	   IF (l_lpn_context = 3) THEN
14839 	      OPEN  c_mmtt_cursor(l_lpn_id);
14840        l_progress := 50;
14841       LOOP
14842       FETCH c_mmtt_cursor INTO l_mmtt_rec;
14843       EXIT WHEN c_mmtt_cursor%NOTFOUND;
14844 
14845       IF (l_debug=1) THEN
14846 			   debug(l_progress||'Before calling revert_crossdock with following params:', l_proc_name,1);
14847 			   debug(l_progress||'transaction_temp_id => '|| l_mmtt_rec.transaction_temp_id, l_proc_name,1);
14848 			   debug(l_progress||'parent_line_id => '|| l_mmtt_rec.parent_line_id, l_proc_name,1);
14849 			   debug(l_progress||'p_move_order_line_id => '|| l_mmtt_rec.move_order_line_id, l_proc_name,1);
14850 			   debug(l_progress||'p_crossdock_type => '|| l_mmtt_rec.crossdock_type, l_proc_name,1);
14851 			   debug(l_progress||'p_backorder_delivery_detail_id => '|| l_mmtt_rec.backorder_delivery_detail_id, l_proc_name,1);
14852 			   debug(l_progress||'p_repetitive_line_id => '|| l_mmtt_rec.repetitive_line_id, l_proc_name,1);
14853 			   debug(l_progress||'p_operation_seq_number => '|| l_mmtt_rec.operation_seq_num, l_proc_name,1);
14854 			   debug(l_progress||'p_inventory_item_id => '|| l_mmtt_rec.inventory_item_id, l_proc_name,1);
14855 			   debug(l_progress||'p_primary_quantity => '|| l_mmtt_rec.primary_quantity, l_proc_name,1);
14856 		  END IF;
14857 
14858        l_progress := 60;
14859       IF l_mmtt_rec.transaction_temp_id IS NOT NULL THEN
14860       wms_atf_runtime_pub_apis.Cleanup_Operation_Instance
14861 		(  p_source_task_id    => l_mmtt_rec.transaction_temp_id
14862 		       , p_activity_type_id  => G_OP_ACTIVITY_INBOUND
14863 		       , x_return_status     => x_return_status
14864 		       , x_msg_data          => l_x_msg_data
14865 		       , x_msg_count         => l_x_msg_count
14866 		       , x_error_code        => l_error_code
14867 		      );
14868 
14869       IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
14870 		 IF (l_debug = 1) THEN
14871 		    debug(l_progress||l_error_code || ' Error in Cleanup_Operation_Instance ' ,l_proc_name,1);
14872 		 END IF;
14873 		 l_progress := 70;
14874 		 x_return_status  := fnd_api.g_ret_sts_error;
14875 		 /* Don't raise error in case of failure, instead set the ret status as error
14876 		 and proceed with the next record */
14877 		   END IF;
14878 
14879        l_progress := 80;
14880 
14881        FOR wooi IN (SELECT operation_instance_id
14882 			   FROM wms_op_operation_instances
14883 			  WHERE source_task_id = l_mmtt_rec.transaction_temp_id
14884 		   ) LOOP
14885 
14886 	 wms_op_runtime_pvt_apis.delete_operation_instance
14887 	   (  p_operation_instance_id  => wooi.operation_instance_id
14888 	    , x_return_status          => x_return_status
14889 			, x_msg_data               => l_x_msg_data
14890 			, x_msg_count              => l_x_msg_count
14891 	   );
14892 
14893 	   IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
14894 		 IF (l_debug = 1) THEN
14895 		    debug(l_progress||l_error_code || ' Error in delete_operation_instance ' ,l_proc_name,1);
14896 		 END IF;
14897 		 l_progress := 90;
14898 		 x_return_status  := fnd_api.g_ret_sts_error;
14899 		 /* Don't raise error in case of failure, instead set the ret status as error
14900 		 and proceed with the next record */
14901 		   END IF;
14902 
14903 
14904        END LOOP;
14905        l_progress := 100;
14906 
14907        FOR wopi IN (SELECT OP_PLAN_INSTANCE_ID
14908 			   FROM wms_op_plan_instances
14909 			  WHERE source_task_id = l_mmtt_rec.parent_line_id
14910 		    ) LOOP
14911 	 l_progress := 110;
14912 	 wms_op_runtime_pvt_apis.delete_plan_instance
14913 	   (  p_op_plan_instance_id  => wopi.OP_PLAN_INSTANCE_ID
14914 	    , x_return_status          => x_return_status
14915 			, x_msg_data               => l_x_msg_data
14916 			, x_msg_count              => l_x_msg_count
14917 	   );
14918 
14919 	   IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
14920 		 IF (l_debug = 1) THEN
14921 		    debug(l_progress||l_error_code || ' Error in delete_plan_instance ' ,l_proc_name,1);
14922 		 END IF;
14923 		 l_progress := 120;
14924 		 x_return_status  := fnd_api.g_ret_sts_error;
14925 		 /* Don't raise error in case of failure, instead set the ret status as error
14926 		 and proceed with the next record */
14927 		   END IF;
14928 
14929        END LOOP;
14930 
14931        l_progress := 130;
14932       inv_trx_util_pub.delete_transaction
14933 	  (  x_return_status        => x_return_status
14934 		       , x_msg_data             => l_x_msg_data
14935 		       , x_msg_count            => l_x_msg_count
14936 	   , p_transaction_temp_id  => l_mmtt_rec.transaction_temp_id
14937 	   , p_update_parent        => true
14938 	  );
14939 
14940       IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
14941 		 IF (l_debug = 1) THEN
14942 		    debug(l_progress||' Error in delete_transaction ' ,l_proc_name,1);
14943 		 END IF;
14944 		 l_progress := 140;
14945 		 x_return_status  := fnd_api.g_ret_sts_error;
14946 		 /* Don't raise error in case of failure, instead set the ret status as error
14947 		 and proceed with the next record */
14948 		   END IF;
14949        END IF;
14950 
14951       l_progress := 150;
14952 
14953 
14954        IF l_mmtt_rec.backorder_delivery_detail_id IS NOT NULL then
14955       WMS_OP_INBOUND_PVT.revert_crossdock
14956 			  (x_return_status                  => x_return_status
14957 			   , x_msg_count                    => l_x_msg_count
14958 			   , x_msg_data                     => l_x_msg_data
14959 			   , p_move_order_line_id           => l_mmtt_rec.move_order_line_id
14960 			   , p_crossdock_type               => l_mmtt_rec.crossdock_type
14961 			   , p_backorder_delivery_detail_id => l_mmtt_rec.backorder_delivery_detail_id
14962 			   , p_repetitive_line_id           => l_mmtt_rec.repetitive_line_id
14963 			   , p_operation_seq_number         => l_mmtt_rec.operation_seq_num
14964 			   , p_inventory_item_id            => l_mmtt_rec.inventory_item_id
14965 			   , p_primary_quantity             => l_mmtt_rec.primary_quantity
14966 			   );
14967 
14968 	 IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
14969 		 IF (l_debug = 1) THEN
14970 		    debug(l_progress||' Error in revert_crossdock ' ,l_proc_name,1);
14971 		 END IF;
14972 		 l_progress := 160;
14973 		 x_return_status  := fnd_api.g_ret_sts_error;
14974 		 /* Don't raise error in case of failure, instead set the ret status as error
14975 		 and proceed with the next record */
14976 		   END IF;
14977 
14978        END IF;
14979        l_progress := 170;
14980 
14981        UPDATE mtl_txn_request_lines SET quantity_detailed=NULL WHERE line_id=l_mmtt_rec.move_order_line_id;
14982 
14983        l_progress := 180;
14984 
14985       END LOOP;
14986 
14987       IF c_mmtt_cursor%isopen THEN
14988 	CLOSE c_mmtt_cursor;
14989       END IF;
14990       l_progress := 190;
14991 
14992 	    END IF;
14993 
14994 	    IF(l_debug= 1) THEN
14995 	      debug(l_progress||'After cleaning crossdock for lpn '|| l_lpn_id);
14996 	      debug(l_progress||'Return status ' || x_return_status);
14997 	    END IF;
14998 
14999 	l_progress := 200;
15000 	END IF;
15001       END LOOP;
15002 
15003       l_progress := 210;
15004       IF lpn_cursor%isopen THEN
15005 	CLOSE lpn_cursor;
15006       END IF;
15007 
15008       COMMIT;
15009       RETURN;
15010 
15011 
15012     EXCEPTION
15013       WHEN OTHERS THEN
15014 	ROLLBACK;
15015 	x_return_status := fnd_api.g_ret_sts_unexp_error;
15016 	debug(l_progress||SQLCODE, 1);
15017 
15018 	IF lpn_cursor%isopen THEN
15019 	  CLOSE lpn_cursor;
15020 	END IF;
15021 
15022 	IF c_mmtt_cursor%isopen THEN
15023 	CLOSE c_mmtt_cursor;
15024       END IF;
15025 
15026 	IF SQLCODE IS NOT NULL THEN
15027 	  l_progress := 220;
15028 	  inv_mobile_helper_functions.sql_error('Cleanup_LPN_Crossdock_Wrapper', l_progress, SQLCODE);
15029 	END IF;
15030     END Cleanup_LPN_Crossdock_Wrapper;
15031 
15032 END WMS_PUTAWAY_UTILS; --End of Package