DBA Data[Home] [Help]

PACKAGE BODY: APPS.WMS_ATF_RUNTIME_PUB_APIS

Source


1 PACKAGE BODY WMS_ATF_RUNTIME_PUB_APIS AS
2   /* $Header: WMSATFRB.pls 120.22.12020000.2 2012/09/22 12:20:32 ssrikaku ship $*/
3 
4   --
5   -- File        : $RCSFile: $
6   -- Content     : WMS_ATF_RUNTIME_PUB_APIS package Body
7   -- Description : WMS Operation Plan Run-time APIs
8   -- Notes       :
9   -- Modified    : 7/24/2003 lezhang created
10 
11   g_txn_type_so_stg_xfr    NUMBER       := inv_globals.g_type_transfer_order_stgxfr;
12   g_op_dest_sys_suggested  NUMBER       := wms_globals.g_op_dest_sys_suggested;
13   g_op_dest_api            NUMBER       := wms_globals.g_op_dest_api;
14   g_op_dest_pre_specified  NUMBER       := wms_globals.g_op_dest_pre_specified;
15   g_op_dest_rules_engine   NUMBER       := wms_globals.g_op_dest_rules_engine;
16   g_wms_task_type_pick     NUMBER       := wms_globals.g_wms_task_type_pick;
17   g_wms_task_type_stg_move NUMBER       := wms_globals.g_wms_task_type_stg_move;
18   g_wms_task_type_putaway  NUMBER       := wms_globals.g_wms_task_type_putaway;
19   g_wms_task_type_inspect  NUMBER       := wms_globals.g_wms_task_type_inspect;
20   g_op_drop_lpn_no_lpn     NUMBER       := wms_globals.g_op_drop_lpn_no_lpn;
21   g_op_drop_lpn_optional   NUMBER       := wms_globals.g_op_drop_lpn_optional;
22   g_ret_sts_success        VARCHAR2(1)  := fnd_api.g_ret_sts_success;
23   g_ret_sts_unexp_error    VARCHAR2(1)  := fnd_api.g_ret_sts_unexp_error;
24   g_ret_sts_error          VARCHAR2(1)  := fnd_api.g_ret_sts_error;
25   g_msg_lvl_unexp_error    NUMBER       := fnd_msg_pub.g_msg_lvl_unexp_error;
26   g_msg_lvl_error          NUMBER       := fnd_msg_pub.g_msg_lvl_error;
27   g_version_printed        BOOLEAN      := FALSE;
28   g_pkg_name               VARCHAR2(30) := 'WMS_ATF_RUNTIME_PUB_APIS';
29 
30   G_OP_TYPE_LOAD CONSTANT NUMBER := wms_globals.g_op_type_load;
31   G_OP_TYPE_DROP CONSTANT NUMBER:= wms_globals.G_OP_TYPE_DROP;
32   G_OP_TYPE_SORT CONSTANT NUMBER:= wms_globals.G_OP_TYPE_SORT;
33   G_OP_TYPE_CONSOLIDATE CONSTANT NUMBER:= wms_globals.G_OP_TYPE_CONSOLIDATE;
34   G_OP_TYPE_PACK CONSTANT NUMBER:= wms_globals.G_OP_TYPE_PACK;
35   G_OP_TYPE_LOAD_SHIP CONSTANT NUMBER:= wms_globals.G_OP_TYPE_LOAD_SHIP;
36   G_OP_TYPE_SHIP CONSTANT NUMBER:=  wms_globals.G_OP_TYPE_SHIP;
37   G_OP_TYPE_CYCLE_COUNT CONSTANT NUMBER :=  wms_globals.G_OP_TYPE_CYCLE_COUNT;
38   G_OP_TYPE_INSPECT CONSTANT NUMBER :=  wms_globals.G_OP_TYPE_INSPECT;
39   G_OP_TYPE_CROSSDOCK CONSTANT NUMBER:= wms_globals.G_OP_TYPE_CROSSDOCK;
40 
41   G_OP_ACTIVITY_INBOUND CONSTANT NUMBER:= wms_globals.G_OP_ACTIVITY_INBOUND;
42   g_task_status_dispatched CONSTANT NUMBER:= 3;
43   g_task_status_loaded CONSTANT NUMBER:= 4;
44   G_OP_INS_STAT_PENDING CONSTANT NUMBER := wms_globals.G_OP_INS_STAT_PENDING;
45   G_OP_INS_STAT_ACTIVE CONSTANT NUMBER := wms_globals.G_OP_INS_STAT_ACTIVE;
46   G_OP_INS_STAT_COMPLETED CONSTANT NUMBER := wms_globals.G_OP_INS_STAT_COMPLETED;
47   G_OP_INS_STAT_ABORTED   CONSTANT NUMBER := WMS_GLOBALS.G_OP_INS_STAT_ABORTED;
48   G_OP_INS_STAT_CANCELLED CONSTANT NUMBER := WMS_GLOBALS.G_OP_INS_STAT_CANCELLED;
49   G_OP_INS_STAT_IN_PROGRESS CONSTANT NUMBER := WMS_GLOBALS.G_OP_INS_STAT_IN_PROGRESS;
50   G_OP_DEST_SYS_SUGGESTED CONSTANT NUMBER := WMS_GLOBALS.G_OP_DEST_SYS_SUGGESTED;
51 
52 
53   PROCEDURE print_debug(p_err_msg IN VARCHAR2, p_module_name IN VARCHAR2, p_level IN NUMBER) IS
54   BEGIN
55     IF NOT g_version_printed THEN
56       inv_mobile_helper_functions.tracelog(p_err_msg => '$Header: WMSATFRB.pls 120.22.12020000.2 2012/09/22 12:20:32 ssrikaku ship $', p_module => g_pkg_name, p_level => 9);
57       g_version_printed  := TRUE;
58     END IF;
59 
60 --    dbms_output.put_line(p_module_name||'  :  '||p_err_msg);
61 
62     inv_mobile_helper_functions.tracelog(p_err_msg => p_err_msg, p_module => g_pkg_name || '.' ||p_module_name, p_level => p_level);
63   END print_debug;
64 
65 
66 
67  /**
68     *    INIT_OP_PLAN_INSTANCE:
69     * <p>For a given document record,this API initialises the Operation Plan Instance as well as the
70     *      Operation instance for the first plan detail.</p>
71     *  @param x_return_status  -Return Status
72     *  @param x_msg_data       -Returns Message Data
73     *  @param x_msg_count      -Returns the message count
74     *  @param x_error_code     -Returns Appropriate error code in case of any error.
75     *  @param p_source_task_id -Identifier of the document record.
76     *  @param p_activity_id    -Identifier of the Activity Type.
77     *
78    **/
79 
80    PROCEDURE INIT_OP_PLAN_INSTANCE(
81     x_return_status    OUT  NOCOPY    VARCHAR2
82   , x_msg_data         OUT  NOCOPY    fnd_new_messages.MESSAGE_TEXT%TYPE
83   , x_msg_count        OUT  NOCOPY    NUMBER
84   , x_error_code       OUT  NOCOPY    NUMBER
85   , p_source_task_id   IN             NUMBER
86   , p_activity_id      IN             NUMBER
87   ) IS
88 
89   CURSOR inbound_document_details IS
90      SELECT transaction_temp_id,
91             operation_plan_id,
92             inventory_item_id,
93             subinventory_code,
94             locator_id,
95             transfer_subinventory,
96             transfer_to_location,
97             organization_id,
98             wms_task_type,
99             primary_quantity
100        FROM mtl_material_transactions_temp
101      WHERE transaction_temp_id=p_source_task_id;
102 
103   CURSOR c_operation_plan(v_operation_plan_id NUMBER) IS
104      SELECT plan_type_id, crossdock_to_wip_flag
105       FROM WMS_OP_PLANS_B
106      WHERE operation_plan_id=v_operation_plan_id;
107 
108   CURSOR c_loc_selection_type(v_op_plan_id NUMBER) IS
109      SELECT loc_selection_criteria
110       FROM wms_op_plan_details
111      WHERE operation_sequence=(SELECT MAX(operation_sequence)
112                                FROM wms_op_plan_details
113                                WHERE operation_plan_id=v_op_plan_id)
114      AND operation_type IN (G_OP_TYPE_DROP,G_OP_TYPE_CROSSDOCK)
115      AND operation_plan_id=v_op_plan_id;
116 
117   CURSOR c_plan_detail(v_operation_plan_id NUMBER) IS
118      SELECT  operation_plan_detail_id
119            , operation_sequence
120            , is_in_inventory
121            , operation_type
122            , subsequent_op_plan_id
123     FROM WMS_OP_PLAN_DETAILS
124      WHERE operation_plan_id  = v_operation_plan_id
125      AND   operation_sequence = (SELECT MIN(operation_sequence)
126                                        FROM WMS_OP_PLAN_DETAILS
127                                       WHERE operation_plan_id=v_operation_plan_id);
128 
129   CURSOR c_plan_instance_exists IS
130      SELECT 1
131      FROM WMS_OP_PLAN_INSTANCES
132      WHERE source_task_id=p_source_task_id
133      AND activity_type_id=p_activity_id;
134 
135   CURSOR c_operation_instance_exists IS
136      SELECT 1
137      FROM WMS_OP_OPERATION_INSTANCES
138      WHERE source_task_id=p_source_task_id
139      AND activity_type_id=p_activity_id;
140 
141 
142                     l_debug     NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
143               l_module_name     VARCHAR2(30) := 'Init_OP_Plan_Instance';
144                  l_progress     NUMBER;
145 
146           l_inbound_doc_rec     inbound_document_details%ROWTYPE :=NULL;
147                  l_mmtt_rec     MTL_MATERIAL_TRANSACTIONS_TEMP%ROWTYPE:=NULL;
148        l_operation_plan_rec     c_operation_plan%ROWTYPE:=NULL;
149 l_operation_plan_detail_rec     c_plan_detail%ROWTYPE:=NULL;
150      l_op_plan_instance_rec     WMS_OP_PLAN_INSTANCES%ROWTYPE:=NULL;
151    l_operation_instance_rec     WMS_OP_OPERATION_INSTANCES%ROWTYPE :=NULL;
152         l_operation_plan_id     NUMBER;
153          l_operation_exists     NUMBER;
154               l_plan_exists     NUMBER;
155            l_source_task_id     NUMBER;
156           l_source_sub_code     VARCHAR2(10);
157         l_source_locator_id     NUMBER;
158      l_final_loc_sel_criter     NUMBER;
159      l_revert_loc_capacity     BOOLEAN :=FALSE;
160 
161   BEGIN
162 
163        IF (l_debug = 1) THEN
164          print_debug(' p_source_task_id   ==> ' || p_source_task_id, l_module_name,3);
165          print_debug(' p_activity_id ==> ' || p_activity_id, l_module_name,3);
166        END IF;
167 
168        SAVEPOINT init_plan_sp;
169 
170        x_return_status  := g_ret_sts_success;
171 
172        l_progress:=10;
173 
174        IF (p_source_task_id IS NULL OR p_activity_id IS NULL) THEN
175           IF (l_debug=1) THEN
176              print_debug('Source task Id is null',l_module_name,1);
177           END IF;
178 
179           /*Raise Invalid Arguement exception*/
180           --x_error_code:=1;
181           --RAISE FND_API.G_EXC_ERROR;
182           raise_application_error(INVALID_INPUT,'Invalid inputs passed'||p_source_task_id||' '||p_activity_id);
183 
184        END IF;
185 
186        IF p_activity_id NOT IN (G_OP_ACTIVITY_INBOUND) THEN
187 
188           /*Raise Invalid Arguement Exception*/
189           raise_application_error(INVALID_INPUT,'Invalid inputs passed'||p_source_task_id||' '||p_activity_id);
190 
191        END IF;
192 
193        /*Query document table(s) based on p_activity_id and P_Source_task_ID.*/
194 
195        IF (p_activity_id =G_OP_ACTIVITY_INBOUND) THEN
196 
197 
198            /*Fetching document Record for Inbound:from MMTT*/
199              IF (l_debug=1) THEN
200                   print_debug('Fetching document record for Inbound',l_module_name,9);
201              END IF;
202 
203              OPEN inbound_document_details;
204              FETCH inbound_document_details INTO l_inbound_doc_rec;
205 
206              IF (inbound_document_details%NOTFOUND) THEN
207                 IF (l_debug=1) THEN
208                    print_debug('Invalid document',l_module_name,3);
209                 END IF;
210 
211                 --x_error_code:=2;
212                 --RAISE FND_API.G_EXC_ERROR;
213                 CLOSE inbound_document_details;
214 
215                 raise_application_error(INVALID_DOC_ID,'Invalid Document');
216              END IF;
217 
218              CLOSE inbound_document_details;
219 
220              l_operation_plan_id:=l_inbound_doc_rec.operation_plan_id;
221              l_progress:=20;
222 
223              IF (l_debug=1) THEN
224                 print_debug('Operation Plan id'||l_operation_plan_id,l_module_name,9);
225              END IF;
226 
227              l_progress:=30;
228 
229        END IF;
230 
231        IF (l_operation_plan_id IS NULL) THEN
232           /*
233           * Null MMTT.Operation_Plan_ID is an error for this API. Not like activate_operation_instance
234           * or complete_operation_instance, in which an MMTT record without operation_plan_ID
235           * could have been loaded before J upgrade, init_plan_instance is always called after
236           * operation plan assignment.
237          */
238           l_progress:=40;
239 
240           IF (l_debug=1) THEN
241              print_debug('Operation Plan Id is null and unforgivable error',l_module_name,1);
242           END IF;
243 
244           --x_error_code:=3;
245           --RAISE FND_API.G_EXC_ERROR;
246           raise_application_error(INVALID_PLAN_ID, 'Invalid Plan');
247           /*Raise user defined exception to populated error code*/
248 
249        END IF;
250 
251        l_progress:=50;
252 
253        /* Query WMS_OP_PLANS_B for the operation_plan_id of this P_Source_task_ID;
254         * and query WMS_OP_PLAN_DETAILS (WOPD) for the first (smallest operation_sequence)
255         *  WOPD record for this plan for the operation_plan_id of this P_Source_task_ID;
256         *  Populate PL/SQL records for these two tables.
257         *  If Operation_Plan_ID is invalid then raise error exception.
258         */
259 
260           IF (l_debug=1) THEN
261             print_debug('Fetching the plan details for the operation plan stamped',l_module_name,9);
262           END IF;
263 
264           OPEN c_operation_plan(l_operation_plan_id);
265 
266           FETCH c_operation_plan INTO l_operation_plan_rec;
267 
268           IF (c_operation_plan%NOTFOUND) THEN
269 
270              /*Invalid operation Plan stamped:Hence raise user defined exception and populate error code*/
271              IF (l_debug=1) THEN
272                 print_debug('Invalid operation Plan stamped on document',l_module_name,3);
273              END IF;
274              --x_error_code:=4;
275              --RAISE FND_API.G_EXC_ERROR;
276              CLOSE c_operation_plan;
277              raise_application_error(INVALID_PLAN_ID, 'Invalid Plan');
278 
279           END IF;
280 
281           CLOSE c_operation_plan;
282 
283           l_progress:=60;
284 
285           OPEN c_plan_detail(l_operation_plan_id);
286 
287           FETCH c_plan_detail INTO l_operation_plan_detail_rec;
288 
289           IF (c_plan_detail%NOTFOUND) THEN
290 
291              /*Invalid operation Plan stamped:Hence raise user defined exception and populate error code*/
292              IF (l_debug=1) THEN
293                 print_debug('Invalid operation Plan stamped on document',l_module_name,3);
294              END IF;
295              --x_error_code:=4;
296              --RAISE FND_API.G_EXC_ERROR;
297              CLOSE c_plan_detail;
298              raise_application_error(INVALID_PLAN_ID,'Invalid Plan');
299 
300           END IF;
301 
302           CLOSE c_plan_detail;
303           l_progress:=70;
304 
305 
306            /*Query WMS_OP_PLAN_INSTANCES for P_Source_task_ID,
307              if the Source Task ID already has another Operation Plan Instance
308              associated with it raise unexpected error exception. Populate proper error code. */
309 
310             OPEN c_plan_instance_exists;
311 
312             FETCH c_plan_instance_exists INTO l_plan_exists;
313 
314             l_progress:=90;
315 
316             IF c_plan_instance_exists%FOUND THEN
317                IF (l_debug=1) THEN
318                  print_debug('Plan instance already exists for Plan instance and src task id',l_module_name,1);
319                END IF;
320 
321                --RAISE FND_API.G_EXC_ERROR;
322                CLOSE c_plan_instance_exists;
323 
324                raise_application_error(PLAN_INSTANCE_EXISTS,'Plan Instance already exists');
325 
326             END IF;
327 
328             CLOSE c_plan_instance_exists;
329 
330 
331        l_progress:=100;
332 
333        /* Query WMS_OP_OPERATION_INSTANCES for p_source_task_id. This should error out in case if
334         * the p_source_task_id is already an operation instance.If so raise unexpected error condition
335         */
336 
337        OPEN c_operation_instance_exists;
338 
339        FETCH c_operation_instance_exists INTO l_operation_exists;
340 
341        l_progress:=102;
342 
343        IF c_operation_instance_exists%FOUND THEN
344 
345           IF (l_debug=1) THEN
346              print_debug('The Source task id passed already exists as an operation instance',l_module_name,1);
347           END IF;
348 
349           CLOSE c_operation_instance_exists;
350 
351           RAISE_APPLICATION_ERROR(INVALID_DOC_ID,'Invalid Document Id');
352 
353 
354        END IF;
355 
356        CLOSE c_operation_instance_exists;
357 
358 
359        /*Create child document record */
360        IF (p_activity_id =G_OP_ACTIVITY_INBOUND) THEN
361 
362 
363           /*If the final determination method of the final drop is not System Suggested then the suggested
364             capacity of the locator needs to be reverted*/
365 
366 
367           OPEN c_loc_selection_type(l_operation_plan_id);
368 
369           FETCH c_loc_selection_type INTO l_final_loc_sel_criter;
370 
371           CLOSE c_loc_selection_type;
372 
373           IF (l_debug=1) THEN
374              print_debug('Locator Selection Type'||l_final_loc_sel_criter,l_module_name,9);
375           END IF;
376 
377           /*If Plan Type is inspect or the last drop in the in PLan does not have a determination type
378             of system suggested*/
379 
380           IF l_operation_plan_rec.plan_type_id=2 OR
381                             l_final_loc_sel_criter<>WMS_GLOBALS.G_OP_DEST_SYS_SUGGESTED THEN
382 
383              IF l_debug=1 THEN
384                print_debug('Locator Capacity needs to be reverted',l_module_name,9);
385              END IF;
386 
387              l_revert_loc_capacity:=TRUE;
388 
389           END IF;
390 
391 
392 
393           l_mmtt_rec.transaction_temp_id   := p_source_task_id;
394           l_mmtt_rec.inventory_item_id     := l_inbound_doc_rec.inventory_item_id;
395           l_mmtt_rec.subinventory_code     := l_inbound_doc_rec.subinventory_code;
396           l_mmtt_rec.locator_id            := l_inbound_doc_rec.locator_id;
397           l_mmtt_rec.transfer_subinventory := l_inbound_doc_rec.transfer_subinventory;
398           l_mmtt_rec.transfer_to_location  := l_inbound_doc_rec.transfer_to_location;
399           l_mmtt_rec.organization_id       := l_inbound_doc_rec.organization_id;
400           l_mmtt_rec.wms_task_type         := l_inbound_doc_rec.wms_task_type;
401           l_mmtt_rec.primary_quantity      := l_inbound_doc_rec.primary_quantity;
402 
403 	  IF l_operation_plan_detail_rec.operation_type = g_op_type_crossdock THEN
404 	     IF (l_debug=1) THEN
405 		print_debug('Crossdock operation is the only operation in this plan',l_module_name,4);
406 	     END IF;
407 	     IF(Nvl(l_operation_plan_rec.crossdock_to_wip_flag, 'N')='N')THEN
408 		l_operation_plan_detail_rec.subsequent_op_plan_id := Nvl(l_operation_plan_detail_rec.subsequent_op_plan_id, 1);
409 	      ELSE -- don't need to stamp subsequent_op_plan_id for WIP
410 		l_operation_plan_detail_rec.subsequent_op_plan_id := l_operation_plan_id;
411 	     END IF;
412 
413 	  END IF;
414 
415           /*Call document handler for creating child records*/
416           IF (l_debug=1) THEN
417              print_debug('Calling WMS_OP_INBOUND_PVT.INIT with the parameters',l_module_name,4);
418              print_debug('p_source_task_id          ==>'||p_source_task_id,l_module_name,4);
419              print_debug('p_operation_type          ==>'||l_operation_plan_detail_rec.operation_type,l_module_name,4);
420              print_debug('MMTT.transaction_temp_id  ==>'||p_source_task_id,l_module_name,4);
421              print_debug('MMTT.inventory_item_id    ==>'||l_inbound_doc_rec.inventory_item_id,l_module_name,4);
422              print_debug('MMTT.subinventory_code    ==>'||l_mmtt_rec.subinventory_code,l_module_name,4);
423              print_debug('MMTT.locator_id           ==>'||l_mmtt_rec.locator_id,l_module_name,4);
424              print_debug('MMTT.transfer_to_location ==>'||l_mmtt_rec.transfer_to_location,l_module_name,4);
425              print_debug('MMTT.transfer_subinventory==>'||l_mmtt_rec.transfer_subinventory,l_module_name,4);
426              print_debug('MMTT.organization_id      ==>'||l_mmtt_rec.organization_id,l_module_name,4);
427              print_debug('MMTT.wms_task_type        ==>'||l_mmtt_rec.wms_task_type,l_module_name,4);
428 	     print_debug('p_subsequent_op_plan_id   ==>'||l_operation_plan_detail_rec.subsequent_op_plan_id,l_module_name,4);
429           END IF;
430 
431 	  /*
432 	  {{
433 	    Operation plan only has one crossdock operation. Should verify from control board
434 	    that the parent task has inbound crossdock plan and child has outbound plan.
435 
436 	    }}
437 	  */
438 
439 
440           WMS_OP_INBOUND_PVT.INIT
441              (
442                    x_return_status    => x_return_status
443               ,        x_msg_count    => x_msg_count
444               ,         x_msg_data    => x_msg_data
445               ,       x_error_code    => x_error_code
446               ,   x_source_task_id    => l_source_task_id
447               ,   p_source_task_id    => p_source_task_id
448               ,     p_document_rec    => l_mmtt_rec
449               , p_operation_type_id   => l_operation_plan_detail_rec.operation_type
450               , p_revert_loc_capacity => l_revert_loc_capacity
451 	      , p_subsequent_op_plan_id => l_operation_plan_detail_rec.subsequent_op_plan_id
452               );
453 
454           l_progress:=110;
455 
456           IF (x_return_status=g_ret_sts_error) THEN
457              IF (l_debug=1) THEN
458                 print_debug('Error returned from doc handlers with err code'||x_error_code,l_module_name,9);
459              END IF;
460              RAISE FND_API.G_EXC_ERROR;
461 
462 
463           ELSIF (x_return_status<>g_ret_sts_success) THEN
464 
465            IF (l_debug=1) THEN
466               print_debug('unexpected error from doc handler',l_module_name,9);
467            END IF;
468            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
469 
470           END IF;
471 
472           IF (l_debug=1) THEN
473              print_debug('Child src task Id returned'||l_source_task_id,l_module_name,9);
474           END IF;
475 
476           l_progress:=110;
477 
478           /*   Create WMS_OP_PLAN_INSTANCES PL/SQL record with proper operation_plan_ID,
479                activity_type_ID, source_task_ID (P_source_task_ID), and original source/destination
480                data from source task record, with 'Pending' status.*/
481 
482          IF (l_debug=1) THEN
483             print_debug('Creating Plan Instance Record',l_module_name,9);
484          END IF;
485 
486          IF (l_inbound_doc_rec.transfer_to_location IS NULL) THEN
487             l_op_plan_instance_rec.orig_dest_sub_code   := l_inbound_doc_rec.subinventory_code;
488             l_op_plan_instance_rec.orig_dest_loc_id     := l_inbound_doc_rec.locator_id;
489             /*Add logic in Document handler to get source_sub_code and src_locator_id*/
490 
491          ELSE
492             l_op_plan_instance_rec.orig_source_sub_code := l_inbound_doc_rec.subinventory_code;
493             l_op_plan_instance_rec.orig_source_loc_id   := l_inbound_doc_rec.locator_id;
494             l_op_plan_instance_rec.orig_dest_sub_code   := l_inbound_doc_rec.transfer_subinventory;
495             l_op_plan_instance_rec.orig_dest_loc_id     := l_inbound_doc_rec.transfer_to_location;
496 
497          END IF;
498 
499        END IF;/*Activity Inbound*/
500 
501        SELECT  WMS_OP_INSTANCE_S.NEXTVAL
502           INTO l_op_plan_instance_rec.op_plan_instance_id
503        FROM dual;
504 
505        l_op_plan_instance_rec.operation_plan_id         := l_operation_plan_id;
506        l_op_plan_instance_rec.activity_type_id          := p_activity_id;
507        l_op_plan_instance_rec.plan_type_id              := l_operation_plan_rec.plan_type_id;
508        l_op_plan_instance_rec.source_task_id            := p_source_task_id;
509        l_op_plan_instance_rec.status                    := G_OP_INS_STAT_PENDING;
510        l_op_plan_instance_rec.organization_id           := l_inbound_doc_rec.organization_id;
511        l_op_plan_instance_rec.plan_execution_start_date := SYSDATE;
512 
513        l_progress:=120;
514 
515        IF (l_debug=1) THEN
516           print_debug('Call table handler to insert Plan instance',l_module_name,4);
517           print_debug('op_plan_instance_id       ==>'||l_op_plan_instance_rec.op_plan_instance_id,l_module_name,4);
518           print_debug('operation_plan_id         ==>'||l_op_plan_instance_rec.operation_plan_id,l_module_name,4);
519           print_debug('Activity_type_id          ==>'||l_op_plan_instance_rec.activity_type_id,l_module_name,4);
520           print_debug('Plan_type_id              ==>'||l_op_plan_instance_rec.Plan_type_id,l_module_name,4);
521           print_debug('source_task_id            ==>'||l_op_plan_instance_rec.source_task_id,l_module_name,4);
522           print_debug('Status                    ==>'||l_op_plan_instance_rec.status,l_module_name,4);
523           print_debug('Organization Id           ==>'||l_op_plan_instance_rec.organization_id,l_module_name,4);
524           print_debug('Plan Execution Start_date ==>'||l_op_plan_instance_rec.plan_execution_start_date,l_module_name,4);
525           print_debug('Orig_source_sub_code      ==>'||l_op_plan_instance_rec.orig_source_sub_code,l_module_name,4);
526           print_debug('Orig_source_loc_id        ==>'||l_op_plan_instance_rec.orig_source_loc_id,l_module_name,4);
527           print_debug('Orig_dest_sub_code        ==>'||l_op_plan_instance_rec.orig_dest_sub_code,l_module_name,4);
528           print_debug('Orig_dest_loc_id          ==>'||l_op_plan_instance_rec.orig_dest_loc_id,l_module_name,4);
529        END IF;
530 
531        /*Call WMS_OP_PLAN_INSTANCES table handler, WMS_OP_RUNTIME_PVT_APIS.INSERT_OPERATION_PLAN_INSTANCE,
532         to insert record into the WMS_OP_PLAN_INSTANCES table.*/
533 
534        WMS_OP_RUNTIME_PVT_APIS.INSERT_PLAN_INSTANCE(
535            x_return_status => x_return_status        ,
536            x_msg_count     => x_msg_count            ,
537            x_msg_data      => x_msg_data             ,
538            p_insert_rec    => l_op_plan_instance_rec );
539 
540        l_progress:=130;
541 
542        IF (l_debug=1) THEN
543           print_debug('Return Status from table handler',l_module_name,9);
544        END IF;
545 
546        IF (x_return_status=g_ret_sts_error) THEN
547          RAISE FND_API.G_EXC_ERROR;
548        ELSIF (x_return_status<>g_ret_sts_success) THEN
549           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
550        END IF;
551 
552        /*Create WMS_OP_OPERATION_INSTANCES (WOOI) PL/SQL record for the first operation.,
553          with proper operation_type_ID, source_task_ID (ID which is returned by the document hanlder),
554          etc. Status field as 'Pending'*/
555 
556         l_operation_instance_rec.operation_plan_detail_id := l_operation_plan_detail_rec.operation_plan_detail_id;
557         l_operation_instance_rec.op_plan_instance_id      := l_op_plan_instance_rec.op_plan_instance_id ;
558         l_operation_instance_rec.organization_id          := l_op_plan_instance_rec.organization_id;
559         l_operation_instance_rec.operation_status         := G_OP_INS_STAT_PENDING;
560         l_operation_instance_rec.operation_sequence       := l_operation_plan_detail_rec.operation_sequence;
561         l_operation_instance_rec.is_in_inventory          := Nvl(l_operation_plan_detail_rec.is_in_inventory, 'N');
562         l_operation_instance_rec.from_subinventory_code   := l_op_plan_instance_rec.orig_source_sub_code;
563         l_operation_instance_rec.from_locator_id          := l_op_plan_instance_rec.orig_source_loc_id;
564         l_operation_instance_rec.source_task_id           := l_source_task_id;
565 	     l_operation_instance_rec.activity_type_id         := p_activity_id;
566 
567         IF (l_operation_plan_detail_rec.operation_type=G_OP_TYPE_CROSSDOCK) THEN
568             l_operation_instance_rec.operation_type_id := G_OP_TYPE_LOAD;
569         ELSE
570             l_operation_instance_rec.operation_type_id    := l_operation_plan_detail_rec.operation_type;
571         END IF;
572 
573         l_progress:=140;
574 
575         IF (l_debug=1) THEN
576           print_debug('Call table handler to insert operation instance',l_module_name,4);
577           print_debug('operation_plan_detail_id ==>'||l_operation_instance_rec.operation_plan_detail_id,l_module_name,4);
578           print_debug('op_plan_instance_id      ==>'||l_operation_instance_rec.op_plan_instance_id,l_module_name,4);
579           print_debug('organization_id          ==>'||l_operation_instance_rec.organization_id,l_module_name,4);
580           print_debug('operation_status         ==>'||l_operation_instance_rec.operation_status,l_module_name,4);
581           print_debug('operation_sequence       ==>'||l_operation_instance_rec.operation_sequence,l_module_name,4);
582           print_debug('is_in_inventory          ==>'||l_operation_instance_rec.is_in_inventory,l_module_name,4);
583           print_debug('from_subinventory_code   ==>'||l_operation_instance_rec.from_subinventory_code,l_module_name,4);
584           print_debug('from_locator_id          ==>'||l_operation_instance_rec.from_locator_id,l_module_name,4);
585           print_debug('operation_type_id        ==>'||l_operation_instance_rec.operation_type_id,l_module_name,4);
586           print_debug('Source Task Id           ==>'||l_operation_instance_rec.source_task_id,l_module_name,4);
587         END IF;
588 
589        /*Call WMS_OP_OPERATION_INSTANCES table handler,  WMS_OP_RUNTIME_PVT_APIS.INSERT_OPERATION_INSTANCE,
590          to insert record into the WMS_OP_OPERATION_INSTANCES table.*/
591 
592         WMS_OP_RUNTIME_PVT_APIS.insert_operation_instance
593            ( x_return_status => x_return_status,
594              x_msg_count     => x_msg_count,
595              x_msg_data      => x_msg_data,
596              p_insert_rec    => l_operation_instance_rec
597            );
598 
599         l_progress:=150;
600 
601         IF (l_debug=1) THEN
602           print_debug('Return Status from table handler',l_module_name,9);
603         END IF;
604 
605         IF (x_return_status=g_ret_sts_error) THEN
606           RAISE FND_API.G_EXC_ERROR;
607         ELSIF (x_return_status<>g_ret_sts_success) THEN
608           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
609         END IF;
610 
611 
612 
613   EXCEPTION
614 
615        WHEN FND_API.G_EXC_ERROR THEN
616           IF (l_debug=1) THEN
617              print_debug('Expected Error Obtained at'||l_progress,l_module_name,1);
618           END IF;
619           IF (inbound_document_details%ISOPEN) THEN
620              CLOSE inbound_document_details;
621           END IF;
622 
623           IF (c_operation_plan%ISOPEN) THEN
624              CLOSE c_operation_plan;
625           END IF;
626 
627           IF (c_plan_detail%ISOPEN) THEN
628              CLOSE c_plan_detail;
629           END IF;
630 
631           IF (c_plan_instance_exists%ISOPEN) THEN
632              CLOSE c_plan_instance_exists;
633           END IF;
634 
635           IF (c_operation_instance_exists%ISOPEN) THEN
636              CLOSE c_operation_instance_exists;
637           END IF;
638 
639           ROLLBACK TO init_plan_sp;
640 
641           x_return_status:=g_ret_sts_error;
642 
643        WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
644           IF (l_debug=1) THEN
645              print_debug('UnExpected Error Obtained at'||l_progress||SQLERRM,l_module_name,1);
646           END IF;
647           IF (inbound_document_details%ISOPEN) THEN
648              CLOSE inbound_document_details;
649           END IF;
650 
651           IF (c_operation_plan%ISOPEN) THEN
652              CLOSE c_operation_plan;
653           END IF;
654 
655           IF (c_plan_detail%ISOPEN) THEN
656              CLOSE c_plan_detail;
657           END IF;
658 
659           IF (c_plan_instance_exists%ISOPEN) THEN
660              CLOSE c_plan_instance_exists;
661           END IF;
662 
663           IF (c_operation_instance_exists%ISOPEN) THEN
664              CLOSE c_operation_instance_exists;
665           END IF;
666 
667           ROLLBACK TO init_plan_sp;
668 
669           x_return_status:=g_ret_sts_unexp_error;
670 
671        WHEN OTHERS THEN
672 
673           print_debug(l_progress||' '||SQLERRM, l_module_name,1);
674 
675           IF fnd_msg_pub.check_msg_level(g_msg_lvl_unexp_error) THEN
676             fnd_msg_pub.add_exc_msg(g_pkg_name, l_module_name, SQLERRM);
677           END IF; /* fnd_msg.... */
678 
679           fnd_msg_pub.count_and_get(
680                                  p_count => x_msg_count,
681                                  p_data  => x_msg_data
682                                 );
683 
684           IF (SQLCODE<-20000) THEN
685             IF (l_debug=1) THEN
686               print_debug('This is a user defined exception',l_module_name,1);
687             END IF;
688 
689             x_error_code:=-(SQLCODE+20000);
690 
691             x_return_status:=g_ret_sts_error;
692 
693 
694           ELSE
695 
696             x_return_status := g_ret_sts_unexp_error;
697 
698           END IF;
699 
700           IF (inbound_document_details%ISOPEN) THEN
701              CLOSE inbound_document_details;
702           END IF;
703 
704           IF (c_operation_plan%ISOPEN) THEN
705              CLOSE c_operation_plan;
706           END IF;
707 
708           IF (c_plan_detail%ISOPEN) THEN
709              CLOSE c_plan_detail;
710           END IF;
711 
712           IF (c_plan_instance_exists%ISOPEN) THEN
713              CLOSE c_plan_instance_exists;
714           END IF;
715 
716           IF (c_operation_instance_exists%ISOPEN) THEN
717              CLOSE c_operation_instance_exists;
718           END IF;
719 
720           ROLLBACK TO init_plan_sp;
721 
722   END init_op_plan_instance;
723 
724 
725  /**
726   *   Determine_Attributes
727   *   <p>This private procedure returns the LPN, Locator, Subinventory
728   *   attributes for a given operation and activity. Today we do this
729   *   only for a Drop operation </p>
730   *  @param x_return_status    Return Status
731   *  @param x_msg_data         Returns the Message Data
732   *  @param x_msg_count        Returns the Error Message
733   *  @param x_error_code       Returns appropriate error code in case
734   *                            of an error.
735   *  @param x_attributes       Returns a record with the fetched attributes
736   *  @param p_source_task_id   Identifier of the source document record
737   *  @param p_activity_type_id Lookup code of the Activity type Id
738   **/
739    PROCEDURE  Determine_Attributes(
740               x_return_status    OUT  NOCOPY  VARCHAR2,
741               x_msg_data         OUT  NOCOPY  FND_NEW_MESSAGES.MESSAGE_TEXT%TYPE,
742               x_msg_count        OUT  NOCOPY  NUMBER,
743 	      x_error_code       OUT  NOCOPY  NUMBER,
744 	      x_consolidation_method_id   OUT NOCOPY  NUMBER,
745               x_drop_lpn_option  OUT  NOCOPY  NUMBER,	-- xdock
746               x_plan_attributes  OUT  NOCOPY  WMS_OP_INBOUND_PVT.DEST_PARAM_REC_TYPE,
747               p_source_task_id   IN           NUMBER,
748 	      p_activity_type_id IN           NUMBER,
749 	      p_inventory_item_id IN          NUMBER
750                                ) IS
751 
752       l_debug       NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'),0);
753       l_module_name VARCHAR2(30) := 'Determine_Attributes';
754       l_progress    VARCHAR2(30) := '0';
755 
756       l_attributes              WMS_OP_INBOUND_PVT.DEST_PARAM_REC_TYPE;
757 
758       l_sub_determination_method  NUMBER;
759       l_loc_determination_method  NUMBER;
760       l_lpn_determination_method  NUMBER;
761       l_zone_determination_method NUMBER;
762 
763 
764       l_return_status  VARCHAR2(1);
765       l_msg_data       VARCHAR2(2000);
766       l_msg_count      NUMBER;
767       l_zone_id        NUMBER;
768       l_valid_flag     VARCHAR2(1); -- dummy variable
769       l_validate_loc   BOOLEAN := FALSE;
770       l_mol_count      NUMBER; -- xdock
771       l_consolidation_method_id NUMBER; -- xdock
772       l_xd_loc_selection_criteria NUMBER; -- xdock
773       l_xd_loc_selection_api_id NUMBER; -- xdock
774       l_xd_drop_lpn_option NUMBER; -- xdock
775       l_operation_plan_name VARCHAR2(80);
776 
777       CURSOR c_parent_locator IS
778 	 SELECT Nvl(mmtt.transfer_subinventory, mmtt.subinventory_code) subinventory_code,
779 	   Nvl(mmtt.transfer_to_location, mmtt.locator_id)        locator_id
780 	   FROM   mtl_material_transactions_temp mmtt
781 	   WHERE  mmtt.transaction_temp_id =
782 	   (SELECT child_mmtt.parent_line_id
783 	    FROM mtl_material_transactions_temp child_mmtt
784 	    WHERE child_mmtt.transaction_temp_id = p_source_task_id);
785 
786       CURSOR c_plan_details IS
787       SELECT wopd.zone_selection_criteria  zone_selection_criteria,
788              wopd.pre_specified_zone_id    pre_specified_zone_id,
789              wopd.zone_selection_api_id    zone_selection_api_id,
790              wopd.sub_selection_criteria   sub_selection_criteria,
791              wopd.pre_specified_sub_code   pre_specified_sub_code,
792              wopd.sub_selection_api_id     sub_selection_api_id,
793              wopd.loc_selection_criteria   loc_selection_criteria,
794              wopd.pre_specified_loc_id     pre_specified_loc_id,
795              wopd.loc_selection_api_id     loc_selection_api_id,
796              wopd.lpn_selection_criteria   lpn_selection_criteria,
797 	     wopd.lpn_selection_api_id     lpn_selection_api_id,
798 	     wopd.operation_type           operation_type,
799              nvl(wopd.is_in_inventory,'N') is_in_inventory
800 	FROM   wms_op_operation_instances wooi,
801              wms_op_plan_details wopd
802       WHERE  wooi.activity_type_id = p_activity_type_id
803 	AND    wooi.source_task_id   = p_source_task_id
804 	AND    wooi.operation_status IN (g_op_ins_stat_pending,g_op_ins_stat_active)
805 	AND    wooi.operation_plan_detail_id = wopd.operation_plan_detail_id;
806 
807       l_plan_details c_plan_details%ROWTYPE;
808 
809       CURSOR c_mmtt_data IS
810 	 SELECT mol.project_id, mol.task_id,mmtt.organization_id, mmtt.operation_plan_id, mmtt.lpn_id,
811 	   Nvl(wopb.crossdock_to_wip_flag, 'N') crossdock_to_wip_flag
812 	   FROM mtl_material_transactions_temp mmtt,
813 	   mtl_txn_request_lines mol,
814 	   wms_op_plans_b wopb
815 	   WHERE mmtt.move_order_line_id = mol.line_id
816 	   AND mmtt.operation_plan_id = wopb.operation_plan_id
817 	   AND mmtt.transaction_temp_id = p_source_task_id;
818 
819       l_mmtt_data_rec c_mmtt_data%ROWTYPE;
820 
821       CURSOR c_item_attributes(v_org_id NUMBER) IS
822          SELECT nvl(msi.RESTRICT_SUBINVENTORIES_CODE,2) restrict_subinventories_code
823                ,nvl(msi.RESTRICT_LOCATORS_CODE,2) restrict_locators_code
824           FROM  mtl_system_items msi
825           WHERE msi.inventory_item_id = p_inventory_item_id
826           AND   msi.organization_id   = v_org_id;
827 
828       l_item_attributes c_item_attributes%ROWTYPE;
829 
830       CURSOR c_item_sub(v_org_id NUMBER, v_subinventory VARCHAR2) IS
831          SELECT 1
832          FROM mtl_item_sub_inventories
833          WHERE inventory_item_id   = p_inventory_item_id
834          AND   organization_id     = v_org_id
835          AND   secondary_inventory = v_subinventory;
836 
837       l_item_sub c_item_sub%ROWTYPE;
838 
839       CURSOR c_item_locator(v_org_id NUMBER,v_locator_id NUMBER) IS
840          SELECT 1
841          FROM mtl_secondary_locators
842          WHERE inventory_item_id = p_inventory_item_id
843          AND   secondary_locator = v_locator_id
844          AND   organization_id   = v_org_id;
845 
846       l_item_locator c_item_locator%ROWTYPE;
847 
848 
849    BEGIN
850 
851       IF (l_debug = 1) THEN
852 	 print_debug('Enter: ' , l_module_name, 1);
853 	 print_debug(' p_source_task_id => ' ||p_source_task_id, l_module_name, 1);
854 	 print_debug(' p_activity_type_id => ' ||p_activity_type_id , l_module_name, 1);
855 	 print_debug(' p_inventory_item_id => ' ||p_inventory_item_id, l_module_name, 1);
856 
857       END IF;
858 
859       x_return_status  := g_ret_sts_success;
860 
861       l_progress := '10';
862 
863       OPEN  c_plan_details;
864       FETCH c_plan_details INTO l_plan_details;
865       CLOSE c_plan_details;
866 
867       l_progress := '20';
868 
869       OPEN c_mmtt_data;
870       FETCH c_mmtt_data INTO l_mmtt_data_rec;
871       CLOSE c_mmtt_data;
872 
873       l_progress := '30';
874 
875       IF (l_debug = 1) THEN
876 	 print_debug('l_plan_details.zone_selection_criteria  => ' ||l_plan_details.zone_selection_criteria, l_module_name, 1);
877 	 print_debug('l_plan_details.pre_specified_zone_id  => ' ||l_plan_details.pre_specified_zone_id, l_module_name, 1);
878 	 print_debug('l_plan_details.zone_selection_api_id  => ' ||l_plan_details.zone_selection_api_id, l_module_name, 1);
879 	 print_debug('l_plan_details.sub_selection_criteria  => ' ||l_plan_details.sub_selection_criteria, l_module_name, 1);
880 	 print_debug('l_plan_details.pre_specified_sub_code  => ' ||l_plan_details.pre_specified_sub_code, l_module_name, 1);
881 	 print_debug('l_plan_details.sub_selection_api_id  => ' ||l_plan_details.sub_selection_api_id, l_module_name, 1);
882 	 print_debug('l_plan_details.loc_selection_criteria  => ' ||l_plan_details.loc_selection_criteria, l_module_name, 1);
883 	 print_debug('l_plan_details.pre_specified_loc_id  => ' ||l_plan_details.pre_specified_loc_id, l_module_name, 1);
884 	 print_debug('l_plan_details.loc_selection_api_id  => ' ||l_plan_details.loc_selection_api_id, l_module_name, 1);
885 	 print_debug('l_plan_details.lpn_selection_criteria  => ' ||l_plan_details.lpn_selection_criteria, l_module_name, 1);
886 	 print_debug('l_plan_details.lpn_selection_api_id  => ' ||l_plan_details.lpn_selection_api_id, l_module_name, 1);
887 	 print_debug('l_plan_details.operation_type  => ' ||l_plan_details.operation_type, l_module_name, 1);
888 	 print_debug('l_mmtt_data_rec.project_id => ' || l_mmtt_data_rec.project_id , l_module_name, 1);
889  	 print_debug('l_mmtt_data_rec.task_id => ' || l_mmtt_data_rec.task_id , l_module_name, 1);
890 	 print_debug('l_mmtt_data_rec.operation_plan_id => ' || l_mmtt_data_rec.operation_plan_id , l_module_name, 1);
891 	 print_debug('l_mmtt_data_rec.lpn_id => ' || l_mmtt_data_rec.lpn_id , l_module_name, 1);
892 	 print_debug('l_mmtt_data_rec.crossdock_to_wip_flag => ' || l_mmtt_data_rec.crossdock_to_wip_flag , l_module_name, 1);
893 
894      END IF;
895 
896       IF NVL(l_plan_details.sub_selection_criteria,0) =
897                wms_globals.G_OP_DEST_PRE_SPECIFIED
898       THEN
899 
900          l_attributes.sug_sub_code := l_plan_details.pre_specified_sub_code;
901 
902       ELSE
903 
904          l_attributes.sug_sub_code := NULL;
905 
906       END IF; /* l_plan_details.sub_sug...*/
907 
908      /**
909        * IF the selection criteria is Pre-Specified then fetch locator id
910        * from Plan
911        *
912        * If the selection criteria is System suggested or from Custom we
913        * call the wrapper to fetch the Locator Id
914        *
915        * If Rules suggested then this has to be the last operation and
916        * we get the values from Parent MMTT record
917        */
918 
919       IF NVL(l_plan_details.loc_selection_criteria,0) =
920                wms_globals.G_OP_DEST_PRE_SPECIFIED
921       THEN
922 	 l_attributes.sug_location_id := l_plan_details.pre_specified_loc_id;
923 
924     l_validate_loc  := TRUE;
925 
926       ELSIF NVL(l_plan_details.loc_selection_criteria,0) =
927                wms_globals.G_OP_DEST_API OR
928             NVL(l_plan_details.loc_selection_criteria,0) =
929                wms_globals.G_OP_DEST_CUSTOM_API
930       THEN
931 
932          /*
933           * The API should be validating its inputs so not validating
934           * for selection_api_id here
935           */
936 
937 	    IF (l_debug = 1) THEN
938 	       print_debug('Before calling wms_atf_dest_locator.get_dest_locator with following parameters: ' , l_module_name, 1);
939 	       print_debug('p_mode => ' || 1, l_module_name, 1);
940 	       print_debug('p_task_id => ' || p_source_task_id, l_module_name, 1);
941 	       print_debug('p_activity_type_id => ' || p_activity_type_id, l_module_name, 1);
942 	       print_debug('p_hook_call_id => ' || l_plan_details.loc_selection_api_id, l_module_name, 1);
943 	       print_debug('p_locator_id => ' || NULL, l_module_name, 1);
944 	       print_debug('p_item_id => ' || p_inventory_item_id, l_module_name, 1);
945 	    END IF;
946 
947 	    l_progress := '40';
948 
949 	    wms_atf_dest_locator.get_dest_locator
950 	    (
951 	      x_return_status         => l_return_status
952 	      ,  x_msg_count          => l_msg_count
953 	      ,  x_msg_data           => l_msg_data
954 	      ,  x_locator_id         => l_attributes.sug_location_id
955 	      ,  x_subinventory_code  => l_attributes.sug_sub_code
956 	      ,  x_zone_id            => l_zone_id
957 	      ,  x_loc_valid          => l_valid_flag
958 	      ,  p_mode               => 1
959 	      ,  p_task_id            => p_source_task_id
960 	      ,  p_activity_type_id   => p_activity_type_id
961 	      ,  p_hook_call_id       => l_plan_details.loc_selection_api_id
962 	      ,  p_locator_id         => NULL
963 	      ,  p_item_id            => p_inventory_item_id
964 	      ,  p_api_version        => NULL
965 	      ,  p_init_msg_list      => NULL
966 	      ,  p_commit             => NULL
967 	      );
968 
969 	    l_progress := '50';
970 
971 	    IF (l_debug = 1) THEN
972 	       print_debug('After calling wms_atf_dest_locator.get_dest_locator.' , l_module_name, 1);
973 	       print_debug('x_return_status => ' || l_return_status, l_module_name, 1);
974 	       print_debug('x_msg_count => ' || l_msg_count, l_module_name, 1);
975 	       print_debug('x_msg_data => ' || l_msg_data, l_module_name, 1);
976 	       print_debug('x_locator_id => ' || l_attributes.sug_location_id, l_module_name, 1);
977 	       print_debug('x_subinventory_code => ' || l_attributes.sug_sub_code, l_module_name, 1);
978 	       print_debug('x_zone_id => ' || l_zone_id, l_module_name, 1);
979 	       print_debug('x_loc_valid => ' || l_valid_flag, l_module_name, 1);
980 	    END IF;
981 
982 	    IF l_return_status <> g_ret_sts_success THEN
983 	       IF (l_debug = 1) THEN
984 		  print_debug('wms_atf_dest_locator.get_dest_locator failed:  l_return_status = '|| l_return_status, l_module_name, 1);
985 	       END IF;
986 	       --	       fnd_message.set_name('WMS', 'WMS_ATF_LOC_DETERM_FAILED');
987 	       --	       fnd_msg_pub.ADD;
988 
989 	       SELECT wopv.operation_plan_name
990 		 INTO l_operation_plan_name
991 		 FROM wms_op_plans_vl wopv,
992 		 mtl_material_transactions_temp mmtt
993 		 WHERE mmtt.transaction_temp_id = p_source_task_id
994 		 AND mmtt.operation_plan_id = wopv.operation_plan_id;
995 
996 	       fnd_message.set_name('WMS', 'DERIVE_DEST_SUGGESTIONS_FAILED');
997 	       fnd_message.set_token('OPERATION_PLAN_NAME', l_operation_plan_name);
998 	       fnd_msg_pub.ADD;
999 
1000 	       RAISE FND_API.G_EXC_ERROR;
1001 
1002 	    END IF;
1003 
1004 	    IF l_attributes.sug_location_id IS NULL THEN
1005 
1006 	       IF (l_debug = 1) THEN
1007 		  print_debug('wms_atf_dest_locator.get_dest_locator returns NULL locator. ', l_module_name, 1);
1008 	       END IF;
1009 	       --	       fnd_message.set_name('WMS', 'WMS_ATF_LOC_DETERM_FAILED');
1010 	       --	       fnd_msg_pub.ADD;
1011 
1012 	       SELECT wopv.operation_plan_name
1013 		 INTO l_operation_plan_name
1014 		 FROM wms_op_plans_vl wopv,
1015 		 mtl_material_transactions_temp mmtt
1016 		 WHERE mmtt.transaction_temp_id = p_source_task_id
1017 		 AND mmtt.operation_plan_id = wopv.operation_plan_id;
1018 
1019 	       fnd_message.set_name('WMS', 'DERIVE_DEST_SUGGESTIONS_FAILED');
1020 	       fnd_message.set_token('OPERATION_PLAN_NAME', l_operation_plan_name);
1021 	       fnd_msg_pub.ADD;
1022 
1023 	       RAISE FND_API.G_EXC_ERROR;
1024 
1025 	    END IF;
1026 
1027        l_validate_loc := TRUE;
1028 
1029        /*
1030        {{
1031 	 For corssdock operation, to determine drop off locator, should honor
1032 	 subsequent outbound operation plan.
1033 	 }}
1034        */
1035         ELSIF l_plan_details.operation_type = g_op_type_crossdock AND
1036               l_mmtt_data_rec.crossdock_to_wip_flag = 'N'
1037         THEN
1038 
1039 	 BEGIN
1040 	    SELECT  loc_selection_criteria
1041 	      ,     loc_selection_api_id
1042 	      ,     consolidation_method_id
1043 	      ,     Nvl(drop_lpn_option,2)
1044 	      INTO  l_xd_loc_selection_criteria
1045 	      ,     l_xd_loc_selection_api_id
1046 	      ,     l_consolidation_method_id
1047 	      ,     l_xd_drop_lpn_option
1048 	      FROM  wms_op_plan_details
1049 	      WHERE operation_plan_id = l_mmtt_data_rec.operation_plan_id
1050 	      AND   operation_type = 2;
1051 	 EXCEPTION
1052 	    WHEN OTHERS THEN
1053 	       IF (l_debug = 1) THEN
1054 		  print_debug('Error retrievine info from outbound plan',l_module_name,1);
1055 	       END IF;
1056 	       RAISE FND_API.G_EXC_ERROR;
1057 	 END ;
1058 
1059 	 IF (l_debug = 1) THEN
1060 	    print_debug('l_consolidation_method_id =>'||l_consolidation_method_id,l_module_name,4);
1061 	    print_debug('l_xd_loc_selection_criteria =>'||l_xd_loc_selection_criteria,l_module_name,4);
1062 	    print_debug('l_xd_loc_selection_api_id =>'||l_xd_loc_selection_api_id,l_module_name,4);
1063 	    print_debug('l_xd_drop_lpn_option =>'||l_xd_drop_lpn_option,l_module_name,4);
1064 	 END IF;
1065 
1066 	 IF (l_xd_loc_selection_criteria = wms_globals.g_op_dest_sys_suggested) THEN --4
1067 	    IF (l_debug = 1) THEN
1068 	       print_debug('Outbound plan should not have this value.',l_module_name,4);
1069 	    END IF;
1070 	    RAISE fnd_api.g_exc_error;
1071 	  ELSIF (l_xd_loc_selection_criteria = wms_globals.g_op_dest_api) THEN --2
1072 	    IF (l_xd_loc_selection_api_id = 1) THEN--Consolidation Lane
1073 
1074 	       --If it is consolidation based in consolidation loc, always treat
1075 	       --it as within delivery
1076 	       x_consolidation_method_id := 2;
1077 
1078 	       -- {{
1079 	       --   Subsequent outbound plan indicates drop to consolidation locator
1080 	       --   should suggest consolidation locator based on delivery consolidation
1081 	       -- }}
1082 	       IF (l_debug = 1) THEN
1083 		  print_debug('Calling WMS_OP_DEST_SYS_APIS.Get_CONS_Loc_For_Delivery.', l_module_name, 4);
1084 		  print_debug('p_call_mode = '||3, l_module_name, 4);
1085 		  print_debug('p_task_type = '||g_wms_task_type_pick, l_module_name, 4);
1086 		  print_debug('p_task_id = '||p_source_task_id, l_module_name, 4);
1087 	       END IF;
1088 
1089 	       l_progress := '50.10';
1090 
1091 	       wms_op_dest_sys_apis.Get_CONS_Loc_For_Delivery
1092 		 (x_return_status => l_return_status,
1093 		  x_message => l_msg_data,
1094 		  x_locator_id => l_attributes.sug_location_id,
1095 		  x_zone_id => l_zone_id,
1096 		  x_subinventory_code => l_attributes.sug_sub_code,
1097 		  p_call_mode => 3,  -- xdock locator selection
1098 		  p_task_type => g_wms_task_type_pick,  -- picking
1099 		  p_task_id => p_source_task_id,
1100 		  p_locator_id => NULL);
1101 
1102 	       IF (l_debug = 1) THEN
1103 		  print_debug('After calling wms_op_dest_sys_apis.Get_CONS_Loc_For_Delivery.', l_module_name, 4);
1104 		  print_debug('x_return_status = '||l_return_status, l_module_name, 4);
1105 		  print_debug('x_message = '||l_msg_data, l_module_name, 4);
1106 		  print_debug('x_locator_id = '||l_attributes.sug_location_id, l_module_name, 4);
1107 		  print_debug('x_subinventory_code = '||l_attributes.sug_sub_code, l_module_name, 4);
1108 		  print_debug('x_zone_id = '||l_zone_id, l_module_name, 4);
1109 	       END IF;
1110 
1111 	       --{{
1112 	       -- Warning messages, such as 'Consolidation locators full' should be passed back to the UI
1113 	       -- Should prompt the user with this message and option to proceed or not.
1114 	       -- 1. If user chooses yes, should proceed with the *warned* locator, e.g. the locator contains
1115 	       --    other delivery
1116 	       -- 2. If user choose no, should go back to menu and cleanup (rollback) the suggested loc/sub on MMTT
1117 	       --}}
1118 	       IF l_return_status <> g_ret_sts_success THEN
1119 		  IF l_return_status = 'W' THEN
1120 		     x_return_status := l_return_status;
1121 		     x_msg_data := l_msg_data;
1122 		   ELSE
1123 		     RAISE fnd_api.g_exc_error;
1124 		  END IF;
1125 
1126 	       END IF;
1127 	     ELSE --l_task_drop_loc_rec.loc_selection_api_id = 1 (Staging Lane)
1128 	       -- {{
1129 	       --   Subsequent outbound plan indicates drop to staging lane
1130 	       --   should suggest staging locator based on delivery consolidation
1131 	       -- }}
1132 
1133 
1134 	       l_progress := '50.20';
1135 
1136 	       --{{
1137 	       -- If there are multiple move order lines in this LPN, don't care about MDC
1138 	       -- always do consolidation within delivery
1139 	       -- And need to pass the approriate x_consolidation_method_id flag back to caller
1140 	       --}}
1141 
1142 	       SELECT COUNT(line_id)
1143 		 INTO l_mol_count
1144 		 FROM mtl_txn_request_lines
1145 		 WHERE lpn_id = l_mmtt_data_rec.lpn_id;
1146 
1147 	       IF (l_debug = 1) THEN
1148 		  print_debug('l_mol_count = '||l_mol_count, l_module_name , 4);
1149 		  print_debug('l_consolidation_method_id = '||l_consolidation_method_id, l_module_name , 4);
1150 	       END IF;
1151 
1152 	       IF (l_mol_count = 1
1153 		   AND l_consolidation_method_id = 1
1154 		   AND l_xd_drop_lpn_option = 2) THEN
1155 
1156 		  --Call MDC API
1157 		  x_consolidation_method_id := 1; -- across delivery
1158 
1159 		  --{{
1160 		  -- When MDC is enabled and when there is only one move order line in this LPN
1161 		  -- suggested sub/loc/LPN will be determined by MDC API
1162 		  --}}
1163 		  IF (l_debug = 1) THEN
1164 		     print_debug('Before calling wms_mdc_pvt.suggest_to_lpn with following parameters:', l_module_name, 4);
1165 		     print_debug('p_transfer_lpn_id : '||l_mmtt_data_rec.lpn_id , l_module_name, 4);
1166 		  END IF;
1167 
1168 		  wms_mdc_pvt.suggest_to_lpn
1169 		    (p_lpn_id => l_mmtt_data_rec.lpn_id,
1170 		     p_delivery_id => NULL,
1171 		     x_to_lpn_id =>l_attributes.cartonization_id,
1172 		     x_to_subinventory_code=>l_attributes.sug_sub_code,
1173 		     x_to_locator_id =>l_attributes.sug_location_id,
1174 		     x_return_status =>l_return_status,
1175 		     x_msg_count =>l_msg_count,
1176 		     x_msg_data =>l_msg_data);
1177 
1178 		  IF (l_debug = 1) THEN
1179 		     print_debug('After calling wms_mdc_pvt.suggest_to_lpn:', l_module_name, 4);
1180 		     print_debug('x_return_status : '|| x_return_status, l_module_name, 4);
1181 		     print_debug('x_to_lpn_id : '|| l_attributes.cartonization_id, l_module_name, 4);
1182 		     print_debug('x_to_subinventory_code : '||l_attributes.sug_sub_code , l_module_name, 4);
1183 		     print_debug('x_to_locator_id : '||l_attributes.sug_location_id , l_module_name, 4);
1184 		  END IF;
1185 
1186 		  IF l_return_status <> g_ret_sts_success THEN
1187 		     IF (l_debug = 1) THEN
1188 			print_debug('Failed calling wms_mdc_pvt.suggest_to_lpn.',l_module_name, 4);
1189 		     END IF;
1190 
1191 		     RAISE fnd_api.g_exc_error;
1192 		  END IF;
1193 	       END IF; --END IF (l_mol_count = 1)
1194 
1195 	       IF (l_consolidation_method_id = 2 OR
1196 		   l_xd_drop_lpn_option = 1 OR
1197 		   l_attributes.cartonization_id IS NULL) THEN
1198 
1199 		  IF (l_debug = 1) THEN
1200 		     print_debug('Calling WMS_OP_DEST_SYS_APIS.Get_Staging_Loc_For_Delivery.', l_module_name, 4);
1201 		     print_debug('p_call_mode = '||3, l_module_name, 4);
1202 		     print_debug('p_task_type = '||g_wms_task_type_pick, l_module_name, 4);
1203 		     print_debug('p_task_id = '||p_source_task_id, l_module_name, 4);
1204 		  END IF;
1205 
1206 		  x_consolidation_method_id := 2; -- within delivery
1207 
1208 		  wms_op_dest_sys_apis.Get_Staging_Loc_For_Delivery
1209 		    (x_return_status => l_return_status,
1210 		     x_message => l_msg_data,
1211 		     x_locator_id => l_attributes.sug_location_id,
1212 		     x_zone_id => l_zone_id,
1213 		     x_subinventory_code => l_attributes.sug_sub_code,
1214 		     p_call_mode => 3,  -- locator selection
1215 		     p_task_type => g_wms_task_type_pick,  -- picking
1216 		     p_task_id => p_source_task_id,
1217 		     p_locator_id => NULL);
1218 
1219 		  IF (l_debug = 1) THEN
1220 		     print_debug('After calling wms_op_dest_sys_apis.Get_Staging_Loc_For_Delivery.', l_module_name, 4);
1221 		     print_debug('x_return_status = '||l_return_status, l_module_name, 4);
1222 		     print_debug('x_message = '||l_msg_data, l_module_name, 4);
1223 		     print_debug('x_locator_id = '||l_attributes.sug_location_id, l_module_name, 4);
1224 		     print_debug('x_subinventory_code = '||l_attributes.sug_sub_code, l_module_name, 4);
1225 		     print_debug('x_zone_id = '||l_zone_id, l_module_name, 4);
1226 		  END IF;
1227 
1228 		  --{{
1229 		  -- Warning messages from get staging loc API should be passed back to the UI
1230 		  -- Should prompt the user with this message and option to proceed or not.
1231 		  -- 1. If user chooses yes, should proceed with the *warned* locator, e.g. the locator contains
1232 		  --    other delivery
1233 		  -- 2. If user choose no, should go back to menu and cleanup (rollback) the suggested loc/sub on MMTT
1234 		  --}}
1235 		  IF l_return_status <> g_ret_sts_success THEN
1236 		     IF l_return_status = 'W' THEN
1237 			x_return_status := l_return_status;
1238 			x_msg_data := l_msg_data;
1239 		      ELSE
1240 			RAISE fnd_api.g_exc_error;
1241 		     END IF;
1242 
1243 		  END IF;
1244 	       END IF; --END IF (l_consolidation_method_id = 2 OR
1245 	    END IF;--END IF (l_plan_details.loc_selection_api_id = 1) THEN
1246 	 END IF;--END IF (l_plan_details.loc_selection_criteria = wms_globals.g_op_dest_sys_suggested) THEN
1247 
1248 	 IF l_attributes.sug_location_id IS NULL OR
1249 	   l_attributes.sug_sub_code IS NULL THEN
1250 	    IF (l_debug = 1) THEN
1251 	       print_debug('MDC or staging suggestion api does not return anthing ',l_module_name, 4);
1252 	    END IF;
1253 
1254             OPEN  c_parent_locator;
1255             FETCH c_parent_locator INTO l_attributes.sug_sub_code,
1256                                         l_attributes.sug_location_id;
1257             CLOSE c_parent_locator;
1258 
1259 
1260 	 END IF;--END IF l_attributes.sug_location_id IS NULL OR
1261 
1262        ELSIF NVL(l_plan_details.loc_selection_criteria,0) =
1263 	 wms_globals.G_OP_DEST_SYS_SUGGESTED
1264 	 THEN
1265 
1266          IF p_activity_type_id = wms_globals.g_op_activity_inbound THEN
1267 
1268 	    l_progress := '60';
1269 
1270             OPEN  c_parent_locator;
1271             FETCH c_parent_locator INTO l_attributes.sug_sub_code,
1272                                         l_attributes.sug_location_id;
1273             CLOSE c_parent_locator;
1274 
1275             l_validate_loc := FALSE;
1276 
1277 	    l_progress := '70';
1278 
1279          ELSE
1280 
1281          /**
1282            * We should never come here, so raise Data Inconsistency Exception
1283            */
1284 	     IF (l_debug = 1) THEN
1285 		print_debug('Invalid activity '||p_activity_type_id,
1286 			    l_module_name, 1);
1287 	     END IF;
1288 
1289 
1290          END IF; /* p_activity_type_id ..*/
1291 
1292       ELSE
1293 
1294          /**
1295            * We should never come here, so raise Data Inconsistency Exception
1296            */
1297       	     IF (l_debug = 1) THEN
1298 		print_debug('Invalid Selection critiera value '||
1299 			    l_plan_details.loc_selection_criteria, l_module_name, 1);
1300 	     END IF;
1301 
1302            RAISE FND_API.G_EXC_ERROR;
1303 
1304       END IF; /* l_plan_details.sug_loca.... */
1305 
1306       IF l_validate_loc THEN
1307          /*  WE need to validate whether suggestions made
1308           * confirm to the ItemSub ItemLocator relations
1309           */
1310         /* Making this check for Inventory as in Patchset J we cannot define Item Sub Item Locator
1311          * relationships for REceving Subs and Locators. This might change in future release so
1312          * we would need to just comment this line
1313          */
1314         IF l_plan_details.is_in_inventory = 'Y' THEN
1315 
1316            print_debug('Validating the Item Sub,Item Locator relationships',l_module_name,1);
1317            print_debug('Fetch the item Attributes',l_module_name,1);
1318 
1319            OPEN c_item_attributes(l_mmtt_data_rec.organization_id);
1320 
1321            FETCH c_item_attributes INTO l_item_attributes;
1322 
1323            CLOSE c_item_attributes;
1324 
1325            IF l_debug =1 THEN
1326             print_debug('Item Attributes fetched are as follows',l_module_name,9);
1327             print_debug('Restrict SubInventory Code'||l_item_attributes.restrict_subinventories_code,l_module_name,9);
1328             print_debug('Restrict Locator Code'||l_item_attributes.restrict_locators_code,l_module_name,9);
1329            END IF;
1330 
1331            IF (l_item_attributes.restrict_subinventories_code =1) THEN
1332 
1333               /*The Item needs to be validated for  Item Sub restrictions*/
1334               IF l_debug =1 THEN
1335                  print_debug('Need to validate item Sub Restriction',l_module_name,9);
1336               END IF;
1337 
1338               OPEN c_item_sub(l_mmtt_data_rec.organization_id,l_attributes.sug_sub_code);
1339 
1340               FETCH c_item_sub INTO l_item_sub;
1341 
1342               IF c_item_sub%NOTFOUND THEN
1343 
1344                  /*Suggested locators does not satisfy Item Sub relationship.Raise exception*/
1345 
1346                  IF l_debug = 1 THEN
1347                     print_debug('Suggested Sub does not satisfy Item Sub relationship',l_module_name,9);
1348                  END IF;
1349 
1350                  fnd_message.set_name('WMS','WMS_ATF_ITEM_SUBLOC_FAIL');
1351                  fnd_msg_pub.ADD;
1352 
1353                  CLOSE c_item_sub;
1354 
1355                  RAISE fnd_api.g_exc_error;
1356 
1357               END IF;
1358 
1359               CLOSE c_item_sub;
1360 
1361 
1362            END IF;
1363 
1364            IF (l_item_attributes.restrict_locators_code = 1)  THEN
1365 
1366               IF l_debug = 1 THEN
1367                  print_debug('Suggested Locator needs to validated agaisnt item-loc relationships',l_module_name,9);
1368               END IF;
1369 
1370               OPEN c_item_locator(l_mmtt_data_rec.organization_id,l_attributes.sug_location_id);
1371 
1372               FETCH c_item_locator INTO l_item_locator;
1373 
1374               IF c_item_locator%NOTFOUND THEN
1375 
1376                  IF l_debug=1 THEN
1377                     print_debug('The suggested locator fails the Items locator validatation',l_module_name,9);
1378                  END IF;
1379 
1380                  fnd_message.set_name('WMS','WMS_ATF_ITEM_SUBLOC_FAIL');
1381                  fnd_msg_pub.ADD;
1382 
1383                  RAISE fnd_api.g_exc_error;
1384 
1385               END IF;
1386 
1387            END IF;
1388 
1389         END IF;
1390 
1391       END IF;
1392       -- Bug 3405713
1393       -- Call wms_op_dest_sys_apis.create_pjm_locator before LPN suggestion API
1394       -- Because locator_ID on LPN is for logical locator
1395       -- wms_op_dest_sys_apis.create_pjm_locator will return a logical locator if it exists,
1396       -- otherwise create a logical locator.
1397 
1398       IF l_mmtt_data_rec.project_id IS NOT NULL OR
1399 	l_mmtt_data_rec.task_id IS NOT NULL THEN
1400 
1401 	 l_progress := '74';
1402 
1403 	 IF (l_debug = 1) THEN
1404 	    print_debug('Before calling wms_op_dest_sys_apis.create_pjm_locator with following parameters: ' , l_module_name, 1);
1405 	    print_debug('x_locator_id => ' || l_attributes.sug_location_id , l_module_name, 1);
1406 	    print_debug('p_project_id => ' ||  l_mmtt_data_rec.project_id, l_module_name, 1);
1407 	    print_debug('p_task_id => ' ||  l_mmtt_data_rec.task_id, l_module_name, 1);
1408 
1409 	 END IF;
1410 
1411 	 wms_op_dest_sys_apis.create_pjm_locator
1412 	   (x_locator_id => l_attributes.sug_location_id,
1413 	    p_project_id => l_mmtt_data_rec.project_id,
1414 	    p_task_id    => l_mmtt_data_rec.task_id
1415 	    );
1416 
1417 	 IF (l_debug = 1) THEN
1418 	    print_debug('After calling wms_op_dest_sys_apis.create_pjm_locator : ' , l_module_name, 1);
1419 	    print_debug('l_attributes.sug_location_id => ' ||  l_attributes.sug_location_id, l_module_name, 1);
1420 
1421 	 END IF;
1422 	 l_progress := '78';
1423 
1424       END IF;
1425 
1426 
1427       IF NVL(l_plan_details.lpn_selection_criteria,0) =
1428                wms_globals.G_OP_DEST_API OR
1429             NVL(l_plan_details.lpn_selection_criteria,0) =
1430                wms_globals.G_OP_DEST_CUSTOM_API
1431       THEN
1432 
1433          /*
1434           * The API should be validating its inputs so not validating
1435           * for selection_api_id here
1436           */
1437 
1438 
1439 	    IF (l_debug = 1) THEN
1440 	       print_debug('Before calling  wms_atf_dest_lpn.get_dest_lpn with following parameters: ' , l_module_name, 4);
1441 	       print_debug('p_mode => ' || 1, l_module_name, 4);
1442 	       print_debug('p_task_id => ' || p_source_task_id, l_module_name, 4);
1443 	       print_debug('p_activity_type_id => ' || p_activity_type_id, l_module_name, 4);
1444 	       print_debug('p_hook_call_id => ' || l_plan_details.lpn_selection_api_id, l_module_name, 4);
1445 	       print_debug('p_lpn_id => ' || NULL, l_module_name, 4);
1446 	       print_debug('p_item_id => ' || p_inventory_item_id, l_module_name, 4);
1447 	    END IF;
1448 
1449 	    l_progress := '80';
1450 
1451 	    wms_atf_dest_lpn.get_dest_lpn
1452 	    (
1453 	      x_return_status         => l_return_status
1454 	      ,  x_msg_count          => l_msg_count
1455 	      ,  x_msg_data           => l_msg_data
1456 	      ,  x_lpn_id             => l_attributes.cartonization_id
1457 	      ,  x_valid_flag         => l_valid_flag
1458 	      ,  p_mode               => 1
1459 	      ,  p_task_id            => p_source_task_id
1460 	      ,  p_activity_type_id   => p_activity_type_id
1461 	      ,  p_hook_call_id       => l_plan_details.lpn_selection_api_id
1462 	      ,  p_lpn_id             => NULL
1463 	      ,  p_item_id            => p_inventory_item_id
1464 	      ,  p_subinventory_code  => l_attributes.sug_sub_code
1465 	      ,  p_locator_id         => l_attributes.sug_location_id
1466 	      ,  p_api_version        => NULL
1467 	      ,  p_init_msg_list      => NULL
1468 	      ,  p_commit             => NULL
1469 	      );
1470 
1471 	    l_progress := '90';
1472 
1473 	    IF (l_debug = 1) THEN
1474 	       print_debug('After calling  wms_atf_dest_lpn.get_dest_lpn.' , l_module_name, 4);
1475 	       print_debug('x_return_status => ' || l_return_status, l_module_name, 4);
1476 	       print_debug('x_msg_count => ' || l_msg_count, l_module_name, 4);
1477 	       print_debug('x_msg_data => ' || l_msg_data, l_module_name, 4);
1478 	       print_debug('x_lpn_valid => ' || l_attributes.cartonization_id, l_module_name, 4);
1479 	       print_debug('x_lpn_valid => ' || l_valid_flag, l_module_name, 4);
1480 	    END IF;
1481 
1482 	    IF l_return_status <> g_ret_sts_success THEN
1483 	       IF (l_debug = 1) THEN
1484 		  print_debug('wms_atf_dest_lpn.get_dest_lpn:  l_return_status = '|| l_return_status, l_module_name, 4);
1485 	       END IF;
1486 
1487 	       RAISE FND_API.G_EXC_ERROR;
1488 
1489 	    END IF;
1490 
1491       ELSIF l_plan_details.operation_type = g_op_type_crossdock AND
1492             l_mmtt_data_rec.crossdock_to_wip_flag = 'N'
1493       THEN
1494 	 --{{
1495 	 -- If current operation is crossdock, need to determine whether to suggest LPN based on
1496 	 -- subsequent outbound operation plan.
1497 	 -- Also pass back drop LPN option based on operation plan i.e. 1,3 yes(2), 2, 4 No(1)
1498 	 -- Putaway drop page should honor this with some tricky cases. check with gayu and mankumar
1499 	 --}}
1500 	 x_drop_lpn_option := l_xd_drop_lpn_option;
1501 
1502 	 IF l_xd_drop_lpn_option = 2 THEN
1503 
1504 	    --Call this API for:
1505 	    -- (Default) LPN based consolidation in staging lane within delivery
1506 	    -- LPN based consolidation in consolidation locator, within delivery in staging lane
1507 	    -- LPN based consolidation in consolidation locator, across deliveries in staging lane
1508 	    IF (x_consolidation_method_id = 2 OR l_xd_loc_selection_api_id = 1) THEN
1509 
1510 	       IF (l_debug = 1) THEN
1511 		  print_debug('Before calling wms_op_dest_sys_apis.Get_LPN_For_Delivery  with following parameters: ' , l_module_name, 4);
1512 		  print_debug('p_task_id   => ' || p_source_task_id, l_module_name, 4);
1513 		  print_debug('p_task_type => ' || g_wms_task_type_pick, l_module_name, 4);
1514 		  print_debug('p_sug_sub   => ' ||l_attributes.sug_sub_code,l_module_name,4);
1515 		  print_debug('p_sug_loc   => ' ||l_attributes.sug_location_id,l_module_name,4);
1516 	       END IF;
1517 
1518 	       l_progress := '90.10';
1519 
1520 	       wms_op_dest_sys_apis.Get_LPN_For_Delivery
1521 		 (
1522 		   x_return_status         => l_return_status
1523 		   ,  x_message            => l_msg_data
1524 		   ,  x_lpn_id             => l_attributes.cartonization_id
1525 		   ,  p_task_type          => g_wms_task_type_pick
1526 		   ,  p_task_id            => p_source_task_id
1527 		   ,  p_sug_sub            => l_attributes.sug_sub_code
1528 		   ,  p_sug_loc            => l_attributes.sug_location_id
1529 		   );
1530 
1531 	       l_progress := '90.20';
1532 
1533 	       IF (l_debug = 1) THEN
1534 		  print_debug('After calling  wms_op_dest_sys_apis.Get_LPN_For_Delivery.' , l_module_name, 4);
1535 		  print_debug('x_return_status => ' || l_return_status, l_module_name, 4);
1536 		  print_debug('x_msg_data => ' || l_msg_data, l_module_name, 4);
1537 		  print_debug('x_lpn_id => ' ||l_attributes.cartonization_id, l_module_name, 4);
1538 	       END IF;
1539 
1540 	       IF l_return_status <> g_ret_sts_success THEN
1541 		  IF (l_debug = 1) THEN
1542 		     print_debug('wms_atf_dest_lpn.get_dest_lpn:  l_return_status = '|| l_return_status, l_module_name, 4);
1543 		  END IF;
1544 
1545 		  RAISE FND_API.G_EXC_ERROR;
1546 
1547 	       END IF;
1548 
1549 	    END IF; -- (x_consolidation_method_id = 2) T
1550 
1551 	  ELSE
1552 	    x_drop_lpn_option := 1;
1553 	 END IF;
1554 
1555        ELSIF l_plan_details.lpn_selection_criteria IS NOT NULL THEN
1556 
1557          /**
1558            * We should never come here, so raise Data Inconsistency Exception
1559            */
1560    	     IF (l_debug = 1) THEN
1561 		print_debug('Invalid LPN Selection critiera value '||
1562 			    l_plan_details.lpn_selection_criteria, l_module_name, 1);
1563 	     END IF;
1564 
1565            RAISE FND_API.G_EXC_ERROR;
1566 
1567       END IF; /* lpn_selection...*/
1568 
1569 
1570       x_plan_attributes := l_attributes;
1571 
1572       --When x_return_status = 'W', x_msg_data is already
1573       --set to the translated string, so you do not want
1574       --to override it here
1575       IF (x_return_status <> 'W') THEN
1576 	 fnd_msg_pub.count_and_get(
1577 				    p_count => x_msg_count,
1578 				    p_data  => x_msg_data
1579 				    );
1580       END IF;
1581 
1582       --Moved this to the beginning because x_return_status could
1583       --have been updated to 'W', and this line will override the value
1584       --x_return_status  := g_ret_sts_success;
1585 
1586    EXCEPTION
1587 
1588          WHEN FND_API.G_EXC_ERROR THEN
1589             print_debug(l_progress||' '||SQLERRM, l_module_name,1);
1590 
1591             IF fnd_msg_pub.check_msg_level(g_msg_lvl_error) THEN
1592                fnd_msg_pub.add_exc_msg(g_pkg_name, l_module_name, SQLERRM);
1593             END IF; /* fnd_msg.... */
1594 
1595 	    IF (l_debug = 1) THEN
1596 	       print_debug('Expected exception ', l_module_name, 1);
1597 	    END IF;
1598             fnd_msg_pub.count_and_get(
1599                                        p_count => x_msg_count,
1600                                        p_data  => x_msg_data
1601                                       );
1602             x_error_code := SQLCODE;
1603             x_return_status := g_ret_sts_error;
1604 
1605 	    IF c_mmtt_data%isopen THEN
1606 	       CLOSE c_mmtt_data;
1607 	    END IF;
1608 
1609 	    IF c_plan_details%isopen THEN
1610 	       CLOSE c_plan_details;
1611 	    END IF;
1612 
1613 	    IF c_parent_locator%isopen THEN
1614 	       CLOSE c_parent_locator;
1615 	    END IF;
1616 
1617        IF c_item_attributes%ISOPEN THEN
1618           CLOSE c_item_attributes;
1619        END IF;
1620 
1621        IF c_item_sub%ISOPEN THEN
1622           CLOSE c_item_sub;
1623        END IF;
1624 
1625        IF c_item_locator%ISOPEN THEN
1626           CLOSE c_item_locator;
1627        END IF;
1628 
1629 
1630          WHEN OTHERS THEN
1631 
1632             print_debug(l_progress||' '||SQLERRM, l_module_name,1);
1633 
1634             IF fnd_msg_pub.check_msg_level(g_msg_lvl_unexp_error) THEN
1635                fnd_msg_pub.add_exc_msg(g_pkg_name, l_module_name, SQLERRM);
1636             END IF; /* fnd_msg.... */
1637 
1638 	    IF (l_debug = 1) THEN
1639 	       print_debug('Other exceptions ', l_module_name, 1);
1640 	    END IF;
1641 
1642             fnd_msg_pub.count_and_get(
1643                                        p_count => x_msg_count,
1644                                        p_data  => x_msg_data
1645                                       );
1646             x_error_code := SQLCODE;
1647             x_return_status := g_ret_sts_unexp_error;
1648 
1649 	    IF c_mmtt_data%isopen THEN
1650 	       CLOSE c_mmtt_data;
1651 	    END IF;
1652 
1653 	    IF c_plan_details%isopen THEN
1654 	       CLOSE c_plan_details;
1655 	    END IF;
1656 
1657 	    IF c_parent_locator%isopen THEN
1658 	       CLOSE c_parent_locator;
1659 	    END IF;
1660 
1661        IF c_item_attributes%ISOPEN THEN
1662           CLOSE c_item_attributes;
1663        END IF;
1664 
1665        IF c_item_sub%ISOPEN THEN
1666           CLOSE c_item_sub;
1667        END IF;
1668 
1669        IF c_item_locator%ISOPEN THEN
1670           CLOSE c_item_locator;
1671        END IF;
1672 
1673 
1674    END Determine_Attributes;
1675 
1676 /**
1677   * ACTIVATE_OPERATION_INSTANCE
1678   *   <p>For a given document record,this API activates the
1679   *      Operation Instance as well as the task
1680   *      associated with the current operation</p>
1681   *
1682   *  @param x_return_status    -Return Status
1683   *  @param x_msg_data         -Returns the Error message Status
1684   *  @param x_msg_count        -Returns the message count
1685   *  @param x_error_code       -Returns appropriate error code in case of any error.
1686   *  @param p_source_task_id   -Identifier of the document record.
1687   *  @param p_activity_id      -Identifier of the Activity Type.
1688   *  @param p_task_execute_rec -Input of WMS_DISPATCHED_TASKS to be created for the task.
1689   **/
1690 
1691   PROCEDURE ACTIVATE_OPERATION_INSTANCE(
1692        x_return_status     OUT  NOCOPY  VARCHAR2
1693       ,x_msg_data          OUT  NOCOPY  FND_NEW_MESSAGES.MESSAGE_TEXT%TYPE
1694       ,x_msg_count         OUT  NOCOPY  NUMBER
1695       ,x_error_code        OUT  NOCOPY  NUMBER
1696       ,x_drop_lpn_option   OUT  NOCOPY  NUMBER	-- xdock
1697       ,x_consolidation_method_id   OUT NOCOPY  NUMBER
1698       ,p_source_task_id    IN           NUMBER
1699       ,p_activity_id       IN           NUMBER
1700       ,p_operation_type_id IN           NUMBER
1701       ,p_task_execute_rec  IN           WMS_DISPATCHED_TASKS%ROWTYPE ) IS
1702 
1703 
1704        CURSOR c_wdt_details IS
1705          SELECT status
1706            FROM wms_dispatched_tasks
1707           WHERE transaction_temp_id=p_source_task_id
1708           AND   task_type=g_wms_task_type_putaway;
1709 
1710        CURSOR c_inbound_doc IS
1711 	  SELECT nvl(wopd.operation_plan_id, mmtt.operation_plan_id) operation_plan_id, -- get from wooi since mmtt may have subsequent plan ID
1712 	    mmtt.transaction_source_type_id,
1713 	    mmtt.transaction_action_id,
1714 	    mmtt.organization_id,
1715 	    mmtt.inventory_item_id,
1716 	    mmtt.parent_line_id,
1717 	    mmtt.primary_quantity,
1718 	    mmtt.move_order_line_id
1719 	    FROM mtl_material_transactions_temp mmtt,
1720 	    wms_op_operation_instances wooi,
1721 	    wms_op_plan_details wopd
1722 	    WHERE mmtt.transaction_temp_id=p_source_task_id
1723 	    AND mmtt.transaction_temp_id = wooi.source_task_id (+)
1724 	    AND wooi.operation_plan_detail_id = wopd.operation_plan_detail_id(+);
1725 
1726        CURSOR c_operation_instance_details IS
1727           SELECT operation_type_id,
1728                  operation_status,
1729                  op_plan_instance_id,
1730                  operation_plan_detail_id,
1731                  operation_instance_id
1732           FROM wms_op_operation_instances
1733           WHERE source_task_id=p_source_task_id
1734 	    AND operation_status IN (G_OP_INS_STAT_PENDING,G_OP_INS_STAT_ACTIVE)
1735 	    ORDER BY operation_sequence DESC;
1736 
1737        CURSOR c_operation_sequence(v_op_plan_instance_id NUMBER) IS
1738           SELECT count(op_plan_instance_id) count
1739           FROM wms_op_operation_instances
1740           WHERE op_plan_instance_id=v_op_plan_instance_id
1741           GROUP BY op_plan_instance_id;
1742 
1743        CURSOR c_effective_date(v_person_id NUMBER) IS
1744           SELECT effective_start_date,effective_end_date
1745           FROM per_all_people_f
1746           WHERE person_id = v_person_id
1747           AND SYSDATE BETWEEN effective_start_date AND effective_end_date;
1748 
1749 
1750        l_wdt_rec                    WMS_DISPATCHED_TASKS%ROWTYPE := NULL;
1751        l_wdt_details                c_wdt_details%ROWTYPE;
1752        l_inbound_doc                c_inbound_doc%ROWTYPE;
1753        l_mmtt_rec                   MTL_MATERIAL_TRANSACTIONS_TEMP%ROWTYPE :=NULL;
1754        l_opertn_instance_details    c_operation_instance_details%ROWTYPE := NULL;
1755        l_op_plan_instance_id        NUMBER;
1756        l_operation_count            NUMBER;
1757        l_wooi_rec                   wms_op_operation_instances%ROWTYPE := NULL;
1758        l_wopi_rec                   wms_op_plan_instances%ROWTYPE := NULL;
1759        l_sug_sub_code               VARCHAR2(10);
1760        l_sug_location_id            NUMBER;
1761        l_sug_to_lpn_id              NUMBER;
1762        l_dest_param_rec             WMS_OP_INBOUND_PVT.DEST_PARAM_REC_TYPE;
1763        l_operation_type_id          NUMBER;
1764 
1765        l_debug       NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
1766        l_module_name VARCHAR2(30) := 'Activate_Operation_Instance';
1767        l_progress    NUMBER;
1768 
1769        l_return_status       VARCHAR2(1);
1770        l_msg_count           NUMBER;
1771        l_msg_data            VARCHAR2(400);
1772 	   l_task_del_flag NUMBER := 0; -- 14284768
1773   BEGIN
1774 
1775       IF (l_debug = 1) THEN
1776           print_debug(' p_source_task_id ==> '||p_source_task_id ,l_module_name,3);
1777           print_debug(' p_activity_id    ==> '||p_activity_id,l_module_name,3);
1778           print_debug(' p_operation_type_id ==>'||p_operation_type_id,l_module_name,3);
1779 
1780       END IF;
1781 
1782       x_return_status  := g_ret_sts_success;
1783       l_progress:=10;
1784 
1785       SAVEPOINT activate_op_sp;
1786 
1787       l_operation_type_id := p_operation_type_id;
1788 
1789       IF (p_source_task_id IS NULL OR p_activity_id IS NULL) THEN
1790           IF (l_debug=1) THEN
1791              print_debug('Source task Id is null',l_module_name,1);
1792           END IF;
1793 
1794           /*Raise Invalid Arguement exception*/
1795           --x_error_code:=1;
1796           --RAISE FND_API.G_EXC_ERROR;
1797           raise_application_error(INVALID_INPUT,'Invalid inputs passed'||p_source_task_id||' '||p_activity_id);
1798 
1799        END IF;
1800 
1801        IF p_activity_id NOT IN (G_OP_ACTIVITY_INBOUND) THEN
1802 
1803         IF (l_debug=1) THEN
1804              print_debug('Invalid value of activity Id',l_module_name,1);
1805           END IF;
1806 
1807           /*Raise Invalid Arguement exception*/
1808           --x_error_code:=1;
1809           --RAISE FND_API.G_EXC_ERROR;
1810           raise_application_error(INVALID_INPUT,'Invalid inputs passed'||p_source_task_id||' '||p_activity_id);
1811        END IF;
1812 
1813        IF (p_activity_id =G_OP_ACTIVITY_INBOUND) THEN
1814 
1815           /* Create or update WDT
1816              This API can be called from 'Load', 'Drop', or 'Inspect' UI.
1817              Only when called from drop, there will be a WDT record with 'Loaded' status and
1818             'putaway' task type.For the other two cases, a new WDT record needs to be created.
1819           */
1820           l_progress:=20;
1821 
1822           OPEN c_wdt_details;
1823 
1824           FETCH c_wdt_details INTO l_wdt_details;
1825 
1826           IF (c_wdt_details%FOUND) THEN
1827 
1828              /*IF WDT.status = 'Dispatched' THEN
1829               *This is to carry over bug fix 2127361
1830               *Delete this WDT record*/
1831              IF (l_wdt_details.status=g_task_status_dispatched) THEN
1832 
1833 
1834                 DELETE FROM wms_dispatched_tasks
1835                   WHERE transaction_temp_id=p_source_task_id;
1836 
1837 				l_task_del_flag := 1; -- 14284768: set to 1 so as to call non autonomous insert for WDT
1838 
1839                 IF (l_debug=1) THEN
1840                   print_debug('Deleting the WDT record since in Dispatched status',l_module_name,9);
1841                 END IF;
1842 
1843              END IF;
1844 
1845           END IF;/*WDT record exists*/
1846           CLOSE c_wdt_details;
1847 
1848           l_progress:=23;
1849 
1850           /* Setting the operation Type Id*/
1851           IF (l_wdt_details.status=g_task_status_loaded) THEN
1852 
1853              l_operation_type_id :=G_OP_TYPE_DROP;
1854 
1855              IF (l_debug=1) THEN
1856                 print_debug('Operation is Drop',l_module_name,9);
1857              END IF;
1858 
1859           ELSIF ((l_wdt_details.status IS NULL OR l_wdt_details.status=g_task_status_dispatched) AND p_operation_type_id<>G_OP_TYPE_INSPECT) THEN
1860 
1861              IF (l_debug=1) THEN
1862                 print_debug('Operation is Load',l_module_name,9);
1863              END IF;
1864 
1865              l_operation_type_id := G_OP_TYPE_LOAD;
1866 
1867           ELSE
1868 
1869              l_operation_type_id := p_operation_type_id;
1870 
1871           END IF;
1872 
1873           l_progress:=25;
1874 
1875           IF (l_wdt_details.status IS NULL OR l_wdt_details.status=g_task_status_dispatched) THEN
1876 
1877              /*IF WDT record does not exist OR WDT.status = 'Dispatched' THEN
1878               *Create WDT PL/SQL record with P_Task_Execute_Rec,
1879               *populate P_Source_Task_ID  into transaction_temp_ID, status 'Dispatched'
1880               */
1881              l_progress:=27;
1882 
1883              l_wdt_rec := p_task_execute_rec;
1884 
1885              l_wdt_rec.transaction_temp_id  := p_source_task_id;
1886              l_wdt_rec.status               := g_task_status_dispatched;
1887 
1888              IF (l_wdt_rec.person_id IS NOT NULL OR l_wdt_rec.person_id <>-1) THEN
1889 
1890                 IF (l_debug=1) THEN
1891                   print_debug('Person id is not null ..hence fetching effective dates',l_module_name,4);
1892                 END IF;
1893 
1894                 OPEN c_effective_date(l_wdt_rec.person_id);
1895 
1896                 FETCH c_effective_date INTO l_wdt_rec.effective_start_date,l_wdt_rec.effective_end_date;
1897 
1898                 CLOSE c_effective_date;
1899 
1900                 IF (l_debug=1) THEN
1901                  print_debug('Effective Dates fetched for Person are '||l_wdt_rec.effective_start_date||l_wdt_rec.effective_end_date,l_module_name,4);
1902                 END IF;
1903 
1904              END IF;
1905 
1906              IF l_wdt_rec.effective_start_date IS NULL THEN
1907 
1908                l_wdt_rec.effective_start_date := SYSDATE;
1909                l_wdt_rec.effective_end_date   := SYSDATE;
1910 
1911              END IF;
1912 
1913              l_wdt_rec.dispatched_time      := SYSDATE;
1914 
1915              /*If P_Operation_Type_ID = 'Inspect' Then
1916               *  WDT.task_type = 'Inspect';
1917               *Else
1918               *  WDT.Task_Type = 'putaway';
1919               */
1920              IF (l_operation_type_id=G_OP_TYPE_INSPECT) THEN
1921 
1922                 l_wdt_rec.task_type := g_wms_task_type_inspect;
1923              ELSE
1924                 l_wdt_rec.task_type := g_wms_task_type_putaway;
1925              END IF;
1926 
1927           END IF;/*WDT not existing*/
1928 
1929           l_progress:=30;
1930 
1931 
1932         /* Check operation plan stamped or not.
1933          *  IF P_Activity_type_ID = Inbound THEN
1934          *  Query operation_plan_id from MMTT where transaction_temp_ID = P_Source_Task_ID
1935          *  If operation_plan_ID is NULL
1936          *   Return 'success' This handles the Non-ATF case
1937          */
1938           OPEN c_inbound_doc;
1939 
1940           FETCH c_inbound_doc INTO l_inbound_doc;
1941 
1942           IF (c_inbound_doc%NOTFOUND) THEN
1943              /* This is an Invalid document and hence raise exception*/
1944              IF (l_debug=1) THEN
1945                 print_debug('Invalid Document Record',l_module_name,1);
1946              END IF;
1947 
1948              CLOSE c_inbound_doc;
1949 
1950              RAISE FND_API.G_EXC_ERROR;
1951 
1952           END IF;
1953 
1954           CLOSE c_inbound_doc;
1955 
1956           l_progress:=40;
1957 
1958           IF (l_inbound_doc.operation_plan_id IS NULL) THEN
1959              /*Non ATF case..Return after inserting WDT*/
1960              IF (l_debug=1) THEN
1961                 print_debug('Operation Plan Id null on document record',l_module_name,9);
1962              END IF;
1963 
1964              l_progress:=50;
1965 
1966              IF (l_wdt_rec.transaction_temp_id IS NOT NULL) THEN   /*WDT record exists*/
1967 
1968                 l_wdt_rec.operation_plan_id  := l_inbound_doc.operation_plan_id;
1969                 l_wdt_rec.move_order_line_id := l_inbound_doc.move_order_line_id;
1970 
1971                 IF (l_debug=1) THEN
1972                    print_debug('Calling the table handler to insert WDT records',l_module_name,9);
1973                    print_debug('Calling insert_Dispatched_tasks with the following values for WDT record',l_module_name,4);
1974                    print_debug('transaction_temp_id  ==>'||l_wdt_rec.transaction_temp_id,l_module_name,4);
1975                    print_debug('user_task_type       ==>'||l_wdt_rec.user_task_type,l_module_name,4);
1976                    print_debug('person_id            ==>'||l_wdt_rec.person_id,l_module_name,4);
1977                    print_debug('status               ==>'||l_wdt_rec.status,l_module_name,4);
1978                    print_debug('effective_start_date ==>'||l_wdt_rec.effective_start_date,l_module_name,4);
1979                    print_debug('equipment_id         ==>'||l_wdt_rec.equipment_id,l_module_name,4);
1980                    print_debug('equipment_instance   ==>'||l_wdt_rec.equipment_instance,l_module_name,4);
1981                    print_debug('person_resource_id   ==>'||l_wdt_rec.person_resource_id,l_module_name,4);
1982                    print_debug('machine_resource_id  ==>'||l_wdt_rec.machine_resource_id,l_module_name,4);
1983                    print_debug('loaded_time          ==>'||l_wdt_rec.loaded_time,l_module_name,4);
1984                    print_debug('drop_off_time        ==>'||l_wdt_rec.drop_off_time,l_module_name,4);
1985                    print_debug('last_update_date     ==>'||l_wdt_rec.last_update_date,l_module_name,4);
1986                    print_debug('last_update_by       ==>'||l_wdt_rec.last_updated_by,l_module_name,4);
1987                    print_debug('created_by           ==>'||l_wdt_rec.created_by,l_module_name,4);
1988                    print_debug('creation_date        ==>'||l_wdt_rec.creation_date,l_module_name,4);
1989                    print_debug('priority             ==>'||l_wdt_rec.priority,l_module_name,4);
1990                    print_debug('task_group_id        ==>'||l_wdt_rec.task_group_id,l_module_name,4);
1991                    print_debug('device_id            ==>'||l_wdt_rec.device_id,l_module_name,4);
1992                    print_debug('device_invoked       ==>'||l_wdt_rec.device_invoked,l_module_name,4);
1993                    print_debug('device_request_id    ==>'||l_wdt_rec.device_request_id,l_module_name,4);
1994                    print_debug('move_order_line_id   ==>'||l_wdt_rec.move_order_line_id,l_module_name,4);
1995                    print_debug('task_type            ==>'||l_wdt_rec.task_type,l_module_name,4);
1996                    print_debug('Operation Plan Id    ==>'||l_wdt_rec.operation_plan_id,l_module_name,4);
1997                    print_debug('Move Order Line Id   ==>'||l_wdt_rec.move_order_line_id,l_module_name,4);
1998                    print_debug('organization_id      ==>'||l_wdt_rec.organization_id,l_module_name,4);
1999                 END IF;
2000 
2001                 l_progress:=60;
2002 
2003                 -- 14284768
2004                 IF l_task_del_flag = 1 THEN
2005 
2006                   WMS_OP_RUNTIME_PVT_APIS.Insert_Dispatched_tasks_nauto
2007                   ( p_wdt_rec        => l_wdt_rec,
2008                     p_source_task_id => p_source_task_id,
2009                     x_return_status  => x_return_status,
2010                     x_msg_count      => x_msg_count,
2011                     x_msg_data       => x_msg_data
2012                   );
2013 
2014                 ELSE
2015 
2016                   WMS_OP_RUNTIME_PVT_APIS.insert_dispatched_tasks
2017                   ( p_wdt_rec        => l_wdt_rec,
2018                     p_source_task_id => p_source_task_id,
2019                     x_return_status  => x_return_status,
2020                     x_msg_count      => x_msg_count,
2021                     x_msg_data       => x_msg_data
2022                   );
2023 
2024                 END IF;
2025                 -- 14284768
2026 
2027                 l_progress:=65;
2028 
2029                IF (l_debug=1) THEN
2030                  print_debug('return status afte calling table handler '||x_return_status,l_module_name,9);
2031                END IF;
2032 
2033                IF (x_return_status=g_ret_sts_error) THEN
2034 
2035                  IF (l_debug=1) THEN
2036                    print_debug('Expected error from table handler',l_module_name,1);
2037                  END IF;
2038 
2039                  RAISE FND_API.G_EXC_ERROR;
2040 
2041                ELSIF (x_return_status<>g_ret_sts_success) THEN
2042                  IF (l_debug=1) THEN
2043                    print_debug('Unexpected error fromt table hander',l_module_name,1);
2044                  END IF;
2045                  RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2046 
2047                END IF;
2048 
2049              END IF; /*WDT Record to be inserted*/
2050 
2051              RETURN;
2052           END IF;/*Non ATF Case*/
2053 
2054        END IF;/*Activity Inbound*/
2055 
2056       /* Query WMS_OP_OPERATION_INSTANCES where source_task_ID = P_Source_Task_ID
2057        * with highest operation_sequence; populate this record into PL/SQL record.
2058        * Raise unexpected error if it does not exist, and return appropriate error code.
2059        */
2060        l_progress:=70;
2061 
2062        OPEN c_operation_instance_details;
2063 
2064        FETCH c_operation_instance_details INTO l_opertn_instance_details;
2065 
2066        IF (c_operation_instance_details%NOTFOUND) THEN
2067 
2068           /* This is an invalid condition for Activiate
2069              The cursor would also take care of the following validation:
2070              If the status for this WOOI record is other than 'pending' or 'Active',
2071              raise unexpected error and return error code.*/
2072 
2073           IF (l_debug=1) THEN
2074              print_debug('No operation INstance exists:invalid condition',l_module_name,1);
2075           END IF;
2076 
2077           CLOSE c_operation_instance_details;
2078 
2079           RAISE FND_API.G_EXC_ERROR;
2080 
2081        END IF;
2082        CLOSE c_operation_instance_details;
2083 
2084        IF (l_debug=1) THEN
2085 	  print_debug('l_opertn_instance_details.operation_type_id= '||l_opertn_instance_details.operation_type_id,l_module_name,1);
2086 	  print_debug('l_opertn_instance_details.operation_status = '||l_opertn_instance_details.operation_status,l_module_name,1);
2087 	  print_debug('l_opertn_instance_details.op_plan_instance_id = '||l_opertn_instance_details.op_plan_instance_id,l_module_name,1);
2088 	  print_debug('l_opertn_instance_details.operation_plan_detail_id = '||l_opertn_instance_details.operation_plan_detail_id,l_module_name,1);
2089 	  print_debug('l_opertn_instance_details.operation_instance_id = '||l_opertn_instance_details.operation_instance_id,l_module_name,1);
2090 
2091        END IF;
2092 
2093 
2094        l_progress:=80;
2095 
2096        l_op_plan_instance_id:=l_opertn_instance_details.op_plan_instance_id;
2097 
2098        IF (p_activity_id=G_OP_ACTIVITY_INBOUND) THEN
2099 
2100 
2101          /*Calling the table handler to insert records in WDT*/
2102           IF (l_wdt_rec.transaction_temp_id IS NOT NULL) THEN   /*WDT record needs to be inserted*/
2103              l_progress:=90;
2104 
2105               l_wdt_rec.op_plan_instance_id := l_op_plan_instance_id;
2106               l_wdt_rec.operation_plan_id   := l_inbound_doc.operation_plan_id;
2107               l_wdt_rec.move_order_line_id  := l_inbound_doc.move_order_line_id;
2108 
2109               IF (l_debug=1) THEN
2110                  print_debug('Calling the table handler to insert WDT records',l_module_name,9);
2111                  print_debug('Calling insert_Dispatched_tasks with the following values for WDT record',l_module_name,4);
2112                  print_debug('transaction_temp_id  ==>'||l_wdt_rec.transaction_temp_id,l_module_name,4);
2113                  print_debug('user_task_type       ==>'||l_wdt_rec.user_task_type,l_module_name,4);
2114                  print_debug('person_id            ==>'||l_wdt_rec.person_id,l_module_name,4);
2115                  print_debug('status               ==>'||l_wdt_rec.status,l_module_name,4);
2116                  print_debug('effective_start_date ==>'||l_wdt_rec.effective_start_date,l_module_name,4);
2117                  print_debug('equipment_id         ==>'||l_wdt_rec.equipment_id,l_module_name,4);
2118                  print_debug('equipment_instance   ==>'||l_wdt_rec.equipment_instance,l_module_name,4);
2119                  print_debug('person_resource_id   ==>'||l_wdt_rec.person_resource_id,l_module_name,4);
2120                  print_debug('machine_resource_id  ==>'||l_wdt_rec.machine_resource_id,l_module_name,4);
2121                  print_debug('loaded_time          ==>'||l_wdt_rec.loaded_time,l_module_name,4);
2122                  print_debug('drop_off_time        ==>'||l_wdt_rec.drop_off_time,l_module_name,4);
2123                  print_debug('last_update_date     ==>'||l_wdt_rec.last_update_date,l_module_name,4);
2124                  print_debug('last_update_by       ==>'||l_wdt_rec.last_updated_by,l_module_name,4);
2125                  print_debug('created_by           ==>'||l_wdt_rec.created_by,l_module_name,4);
2126                  print_debug('creation_date        ==>'||l_wdt_rec.creation_date,l_module_name,4);
2127                  print_debug('priority             ==>'||l_wdt_rec.priority,l_module_name,4);
2128                  print_debug('task_group_id        ==>'||l_wdt_rec.task_group_id,l_module_name,4);
2129                  print_debug('device_id            ==>'||l_wdt_rec.device_id,l_module_name,4);
2130                  print_debug('device_invoked       ==>'||l_wdt_rec.device_invoked,l_module_name,4);
2131                  print_debug('device_request_id    ==>'||l_wdt_rec.device_request_id,l_module_name,4);
2132                  print_debug('move_order_line_id   ==>'||l_wdt_rec.move_order_line_id,l_module_name,4);
2133                  print_debug('task_type            ==>'||l_wdt_rec.task_type,l_module_name,4);
2134                  print_debug('Plan  Id             ==>'||l_wdt_rec.operation_plan_id,l_module_name,4);
2135                  print_debug('Plan Instance Id     ==>'||l_wdt_rec.op_plan_instance_id,l_module_name,4);
2136                  print_debug('Move Order Line Id   ==>'||l_wdt_rec.move_order_line_id,l_module_name,4);
2137               END IF;
2138 
2139               l_progress:=100;
2140 
2141               -- 14284768
2142               IF l_task_del_flag = 1 THEN
2143 
2144                 WMS_OP_RUNTIME_PVT_APIS.Insert_Dispatched_tasks_nauto
2145                 ( p_wdt_rec        => l_wdt_rec,
2146                   p_source_task_id => p_source_task_id,
2147                   x_return_status  => x_return_status,
2148                   x_msg_count      => x_msg_count,
2149                   x_msg_data       => x_msg_data
2150                 );
2151 
2152               ELSE
2153 
2154                 WMS_OP_RUNTIME_PVT_APIS.insert_dispatched_tasks
2155                 ( p_wdt_rec        => l_wdt_rec,
2156                   p_source_task_id => p_source_task_id,
2157                   x_return_status  => x_return_status,
2158                   x_msg_count      => x_msg_count,
2159                   x_msg_data       => x_msg_data
2160                 );
2161 
2162               END IF;
2163               -- 14284768
2164 
2165               l_progress:=110;
2166 
2167              IF (l_debug=1) THEN
2168                print_debug('return status afte calling table handler '||x_return_status,l_module_name,9);
2169              END IF;
2170 
2171              IF (x_return_status=g_ret_sts_error) THEN
2172 
2173                IF (l_debug=1) THEN
2174                  print_debug('Expected error from table handler',l_module_name,1);
2175                END IF;
2176 
2177                RAISE FND_API.G_EXC_ERROR;
2178 
2179              ELSIF (x_return_status<>g_ret_sts_success) THEN
2180                IF (l_debug=1) THEN
2181                  print_debug('Unexpected error fromt table hander',l_module_name,1);
2182                END IF;
2183                RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2184 
2185              END IF;
2186 
2187            END IF; /*WDT Record to be inserted*/
2188 
2189 
2190 
2191       /** If P_operation_Type_ID = 'Inspect' AND WOOI.operation_Type_ID = 'Load'
2192         *  This is the case where user inspects an LPN containing 2 mmtt lines:
2193         *  first with operation plan 'Inspect', second with operation plan 'load-drop-inspect'.
2194         *  User is allowed to inspect the full quantity. When inspect page calls activate_operation_instance
2195         *  for the second mmtt line, we should abort the operation plan.We first call abort_plan_instance
2196         *  to take care of plan/operation instances and null out MMTT.operation_plan_ID;
2197         *  then we call 'complete' document handler to take care of MMTT/MTRL etc.
2198         *  WMS_OP_RUNTIME_PUB.Abort_Plan_Instance
2199         *                      Return success;
2200         */
2201 
2202           IF (l_operation_type_id=G_OP_TYPE_INSPECT AND l_opertn_instance_details.operation_type_id=G_OP_TYPE_LOAD) THEN
2203 
2204              IF (l_debug=1) THEN
2205               print_debug('Aborting the Plan where the operation is load and user performs an inspect',l_module_name,9);
2206              END IF;
2207 
2208              Abort_Operation_Plan(  x_return_status    =>x_return_status
2209                                , x_msg_data         =>x_msg_data
2210                                , x_msg_count        =>x_msg_count
2211                                , x_error_code       =>x_error_code
2212                                , p_source_task_id   =>p_source_task_id
2213                                , p_activity_type_id =>p_activity_id);
2214              l_progress:=120;
2215 
2216            IF (l_debug=1) THEN
2217              print_debug('Return status of Abort_operation_plan'||x_return_status,l_module_name,9);
2218            END IF;
2219 
2220            IF (x_return_status=g_ret_sts_error) THEN
2221               IF (l_debug=1) THEN
2222                  print_debug('Error Obtained from Abort_operation_plan',l_module_name,1);
2223               END IF;
2224               RAISE FND_API.G_EXC_ERROR;
2225 
2226            ELSIF (x_return_status=g_ret_sts_unexp_error) THEN
2227 
2228              IF (l_debug=1) THEN
2229                print_debug('Error Obtained from Abort_operation_plan',l_module_name,1);
2230              END IF;
2231              RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2232           ELSE
2233              /*Return status from Abort is sucess.REturn from here*/
2234              RETURN;
2235 
2236           END IF;
2237 
2238        END IF;/*Operation Type Inspect*/
2239 
2240       END IF;/*Only incase of Inbound*/
2241 
2242       l_progress:=130;
2243 
2244       l_wooi_rec.operation_instance_id := l_opertn_instance_details.operation_instance_id;
2245 
2246       IF (l_opertn_instance_details.operation_status=G_OP_INS_STAT_PENDING) THEN
2247          IF (l_debug=1) THEN
2248             print_debug('Current Operation status is Pending,Update it to Active',l_module_name,9);
2249          END IF;
2250 
2251          /*If the status for this WOOI record is 'pending' update it to 'Active'*/
2252          l_wooi_rec.operation_status      := G_OP_INS_STAT_ACTIVE;
2253 
2254          l_progress:=140;
2255 
2256          IF (l_debug=1) THEN
2257             print_debug('the following values set',l_module_name,9);
2258             print_debug('status ==>'||l_wooi_rec.operation_status,l_module_name,9);
2259             print_debug('Operation Instance Id==>'||l_wooi_rec.operation_instance_id,l_module_name,9);
2260          END IF;
2261 
2262       END IF;/*Operation status is Pending*/
2263 
2264       l_progress:=150;
2265 
2266       /* If there is only one WOOI record for the same OP_PLAN_INSTANCE_ID,
2267        * it means it is the first step of this plan.
2268        * In this case, update WMS_OP_PLAN_INSTANCES status to 'Active' for this OP_PLAN_INSTANCE_ID.*/
2269 
2270       OPEN c_operation_sequence(l_op_plan_instance_id);
2271 
2272       FETCH c_operation_sequence INTO l_operation_count;
2273 
2274       CLOSE c_operation_sequence;
2275 
2276       l_progress:=160;
2277 
2278       IF (l_operation_count=1) THEN
2279          IF (l_debug=1) THEN
2280             print_debug('First operation in the plan,hence update plan status to In Progress',l_module_name,9);
2281          END IF;
2282 
2283          l_wopi_rec.op_plan_instance_id:=l_op_plan_instance_id;
2284          l_wopi_rec.status:=G_OP_INS_STAT_IN_PROGRESS;
2285 	 l_wopi_rec.plan_execution_start_date := Sysdate;
2286 
2287          l_progress:=170;
2288 
2289          IF (l_debug=1) THEN
2290             print_debug('Calling WMS_OP_RUNTIME_PVT_APIS.UPDATE_PLAN_INSTANCE to update the foll values',l_module_name,9);
2291             print_debug('Status'||l_wopi_rec.status,l_module_name,9);
2292             print_debug('Plan Instance Id'||l_wopi_rec.op_plan_instance_id,l_module_name,9);
2293 
2294          END IF;
2295 
2296          WMS_OP_RUNTIME_PVT_APIS.UPDATE_PLAN_INSTANCE
2297            ( p_update_rec    => l_wopi_rec
2298             ,x_return_status => x_return_status
2299             ,x_msg_count     => x_msg_count
2300             ,x_msg_data      => x_msg_data);
2301 
2302          l_progress:=180;
2303 
2304          IF (l_debug=1) THEN
2305              print_debug('Return status from table hanlder'||x_return_status,l_module_name,9);
2306           END IF;
2307 
2308           IF (x_return_status=g_ret_sts_error) THEN
2309              RAISE FND_API.G_EXC_ERROR;
2310           ELSIF (x_return_status<>g_ret_sts_success) THEN
2311              RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2312           END IF;
2313 
2314       END IF;/*Count=1*/
2315 
2316       /*IF P_Operation_Type_ID = 'Drop'
2317        * Derive destination methods.
2318        */
2319 
2320       IF ( l_operation_type_id=G_OP_TYPE_DROP AND p_activity_id=G_OP_ACTIVITY_INBOUND) THEN
2321 
2322          l_progress:=190;
2323 
2324          /*Calling Determine_attributes to get the suggested sub,locator and LPN based on the
2325            PLan Attributes*/
2326          IF (l_debug=1) THEN
2327             print_debug('Calling Determine_Attributes with the following parametemrs',l_module_name,9);
2328             print_debug('p_source_task_id  ==> '||p_source_task_id,l_module_name,4);
2329             print_debug('p_activity_id     ==> '||p_activity_id,l_module_name,4);
2330          END IF;
2331 
2332          Determine_Attributes(
2333               x_return_status    => x_return_status,
2334               x_msg_data         => x_msg_data,
2335               x_msg_count        => x_msg_count,
2336 	      x_error_code       => x_error_code,
2337 	      x_drop_lpn_option  => x_drop_lpn_option,
2338 	      x_plan_attributes  => l_dest_param_rec,
2339 	      x_consolidation_method_id => x_consolidation_method_id,
2340               p_source_task_id   => p_source_task_id,
2341 	      p_activity_type_id => p_activity_id,
2342 	      p_inventory_item_id => l_inbound_doc.inventory_item_id
2343 	      );
2344 
2345          l_progress:=200;
2346 
2347          IF (l_debug=1) THEN
2348             print_debug('Return status from Determine Attributes'||x_return_status,l_module_name,9);
2349 	    print_debug('x_drop_lpn_option = '||x_drop_lpn_option,l_module_name,9);
2350 
2351          END IF;
2352 
2353          IF (x_return_status=g_ret_sts_error) THEN
2354             IF (l_debug=1) THEN
2355                print_debug('Error obtained and hence raising exception',l_module_name,1);
2356             END IF;
2357             RAISE FND_API.G_EXC_ERROR;
2358 
2359          ELSIF (x_return_status=g_ret_sts_unexp_error) THEN
2360             IF (l_debug=1) THEN
2361                print_debug('Unexpected error obtained ',l_module_name,1);
2362             END IF;
2363             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2364 
2365          END IF;
2366 
2367          IF (l_debug=1) THEN
2368             print_debug('Values returned from Determine Attributes are',l_module_name,9);
2369             print_debug('suggested_sub_code ==>'||l_dest_param_rec.sug_sub_code,l_module_name,9);
2370             print_debug('Suggested Locator  ==>'||l_dest_param_rec.sug_location_id,l_module_name,9);
2371             print_debug('Suggested LPN      ==>'||l_dest_param_rec.cartonization_id,l_module_name,9);
2372          END IF;
2373 
2374 
2375          l_progress:=210;
2376 
2377          l_mmtt_rec.transaction_temp_id        := p_source_task_id;
2378          l_mmtt_rec.transaction_source_type_id := l_inbound_doc.transaction_source_type_id;
2379          l_mmtt_rec.transaction_action_id      := l_inbound_doc.transaction_action_id;
2380          l_mmtt_rec.organization_id            := l_inbound_doc.organization_id;
2381          l_mmtt_rec.parent_line_id             := l_inbound_doc.parent_line_id;
2382          l_mmtt_rec.primary_quantity           := l_inbound_doc.primary_quantity;
2383 	 l_mmtt_rec.inventory_item_id          := l_inbound_doc.inventory_item_id;
2384 
2385          IF (l_debug=1) THEN
2386             print_debug('Doc attributes being passed to the DOc handler',l_module_name,9);
2387             print_debug('Trx Src Type Id ==>'||l_mmtt_rec.transaction_source_type_id,l_module_name,9);
2388             print_debug('Trx Action Id   ==>'||l_mmtt_rec.transaction_action_id,l_module_name,9);
2389             print_debug('Organization Id ==>'||l_mmtt_rec.organization_id,l_module_name,9);
2390 	    print_debug('l_mmtt_rec.inventory_item_id  ==>'||l_mmtt_rec.inventory_item_id , l_module_name, 9);
2391          END IF;
2392 
2393          WMS_OP_INBOUND_PVT.ACTIVATE
2394             ( x_return_status    => l_return_status,--Use l_return_status instead
2395               x_msg_count        => l_msg_count,    --of x_return_status because
2396               x_msg_data         => l_msg_data,     --it will override the value
2397               x_error_code       => x_error_code,   --returned from determine_attributes
2398               p_source_task_id   => p_source_task_id,
2399               p_update_param_rec => l_dest_param_rec,
2400               p_document_rec     => l_mmtt_rec);
2401 
2402 
2403          l_progress:=220;
2404 
2405          IF (l_debug=1) THEN
2406              print_debug('Return status from document handler'||l_return_status,l_module_name,9);
2407          END IF;
2408 
2409          IF (l_return_status=g_ret_sts_error) THEN
2410              RAISE FND_API.G_EXC_ERROR;
2411          ELSIF (l_return_status<>g_ret_sts_success) THEN
2412              RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2413          END IF;
2414 
2415          l_progress:=230;
2416 
2417          l_wooi_rec.SUG_TO_SUB_CODE   := l_dest_param_rec.sug_sub_code;
2418          l_wooi_rec.SUG_TO_LOCATOR_ID := l_dest_param_rec.sug_location_id;
2419 	 /* comment out  by GXIAO 12/10/03*/
2420          --l_wooi_rec.LPN_ID            := l_dest_param_rec.cartonization_id;
2421       END IF;/*Activity Inbound and Drop Operation*/
2422 
2423       l_progress:=240;
2424 
2425       WMS_OP_RUNTIME_PVT_APIS.UPDATE_OPERATION_INSTANCE
2426             (  x_return_status => l_return_status --Use l_return_status instead
2427              , x_msg_count     => l_msg_count     --of x_return_status because
2428              , x_msg_data      => l_msg_data      --it will override the value
2429              , p_update_rec    => l_wooi_rec);    --returned from determine_attributes
2430 
2431       IF (l_debug=1) THEN
2432         print_debug('Return status from table hanlder'||l_return_status,l_module_name,9);
2433       END IF;
2434 
2435       IF (l_return_status=g_ret_sts_error) THEN
2436         RAISE FND_API.G_EXC_ERROR;
2437       ELSIF (l_return_status<>g_ret_sts_success) THEN
2438         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2439       END IF;
2440 
2441          l_progress:=250;
2442 
2443 
2444   EXCEPTION
2445 
2446      WHEN FND_API.G_EXC_ERROR THEN
2447            IF (l_debug=1) THEN
2448              print_debug('Expected Error at '||l_progress, l_module_name,1);
2449            END IF;
2450 
2451            IF fnd_msg_pub.check_msg_level(g_msg_lvl_error) THEN
2452               fnd_msg_pub.add_exc_msg(g_pkg_name, l_module_name, SQLERRM);
2453            END IF; /* fnd_msg.... */
2454 
2455            fnd_msg_pub.count_and_get(
2456                                       p_count => x_msg_count,
2457                                       p_data  => x_msg_data
2458                                      );
2459            --x_error_code := SQLCODE;
2460            x_return_status := g_ret_sts_error;
2461 
2462            IF (c_inbound_doc%ISOPEN) THEN
2463               CLOSE c_inbound_doc;
2464            END IF;
2465 
2466            IF (c_wdt_details%ISOPEN) THEN
2467               CLOSE c_wdt_details;
2468            END IF;
2469 
2470            IF (c_operation_instance_details%ISOPEN) THEN
2471               CLOSE c_operation_instance_details;
2472            END IF;
2473 
2474            IF (c_operation_sequence%ISOPEN) THEN
2475               CLOSE c_operation_sequence;
2476            END IF;
2477 
2478            IF (c_effective_date%ISOPEN) THEN
2479               CLOSE c_effective_date;
2480            END IF;
2481 
2482            ROLLBACK TO activate_op_sp;
2483 
2484         WHEN OTHERS THEN
2485            IF (l_debug=1) THEN
2486              print_debug('Unexpected error at'||l_progress||' '||SQLERRM, l_module_name,1);
2487            END IF;
2488 
2489 
2490            IF fnd_msg_pub.check_msg_level(g_msg_lvl_unexp_error) THEN
2491               fnd_msg_pub.add_exc_msg(g_pkg_name, l_module_name, SQLERRM);
2492            END IF; /* fnd_msg.... */
2493 
2494            fnd_msg_pub.count_and_get(
2495                                       p_count => x_msg_count,
2496                                       p_data  => x_msg_data
2497                                      );
2498            --x_error_code := SQLCODE;
2499            x_return_status := g_ret_sts_unexp_error;
2500            IF (c_inbound_doc%ISOPEN) THEN
2501               CLOSE c_inbound_doc;
2502            END IF;
2503 
2504            IF (c_wdt_details%ISOPEN) THEN
2505               CLOSE c_wdt_details;
2506            END IF;
2507 
2508            IF (c_operation_instance_details%ISOPEN) THEN
2509               CLOSE c_operation_instance_details;
2510            END IF;
2511 
2512            IF (c_operation_sequence%ISOPEN) THEN
2513               CLOSE c_operation_sequence;
2514            END IF;
2515 
2516            IF (c_effective_date%ISOPEN) THEN
2517               CLOSE c_effective_date;
2518            END IF;
2519 
2520 
2521            ROLLBACK TO activate_op_sp;
2522 
2523  END ACTIVATE_OPERATION_INSTANCE;
2524 
2525 
2526 
2527 
2528 /**  /**  Complete_Operation_instance
2529   *   <p>This procedure completes the current operation and  creates the next operation instance.
2530   *     If the operation is the last operation the plan is marked  as 'COMPLETED' and archived.</p>
2531   *
2532   *  @param x_return_status      -Return Status
2533   *  @param x_msg_data           -Returns the Error message Status
2534   *  @param x_msg_count          -Returns the message count
2535   *  @param x_error_code         -Returns appropriate error code in case of any error.
2536   *  @param p_source_task_id     -Identifier of the document record.
2537   *  @param p_activity_id        -Lookup Code for the Activity Type
2538   *  @param p_operation_type_id  -Input parameter containing the lookup code for the Operation
2539   *                               Type. Should be passed for inspect operation.
2540  **/
2541     PROCEDURE complete_operation_instance
2542     (
2543      x_return_status     OUT  NOCOPY  VARCHAR2
2544      ,x_msg_data          OUT  NOCOPY  FND_NEW_MESSAGES.MESSAGE_TEXT%TYPE
2545      ,x_msg_count         OUT  NOCOPY  NUMBER
2546      ,x_error_code        OUT  NOCOPY  NUMBER
2547      ,p_source_task_id    IN           NUMBER
2548      ,p_activity_id       IN           NUMBER
2549      ,p_operation_type_id IN           NUMBER )
2550     IS
2551 
2552        l_debug               NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
2553        l_module_name         VARCHAR2(30) := 'Complete_Operation_Instance';
2554        l_progress            NUMBER;
2555        l_return_status       VARCHAR2(1);
2556        l_msg_count           NUMBER;
2557        l_msg_data            VARCHAR2(400);
2558 
2559        l_wdt_task_id         NUMBER;
2560        l_wdt_status          NUMBER;
2561        l_operation_type_id   NUMBER;
2562        l_new_mmtt_id         NUMBER;
2563        l_atf_error_code      NUMBER;
2564        l_last_op_sequence    NUMBER;
2565 
2566        l_unforgivable_error_flag  VARCHAR2(1) := 'N';
2567        l_loc_sel_criteria_id      NUMBER;
2568        l_loc_sel_api_id           NUMBER;
2569 
2570        l_locator_id_dummy         NUMBER;
2571        l_subinventory_code_dummy  VARCHAR2(10);
2572        l_zone_id_dummy            NUMBER;
2573        l_valid_flag               VARCHAR2(1);
2574        l_lpn_controlled_flag        NUMBER;
2575 
2576        CURSOR c_inbound_document_details IS
2577 	  SELECT transaction_temp_id,
2578             operation_plan_id,
2579             inventory_item_id,
2580             subinventory_code,
2581             locator_id,
2582             transfer_subinventory,
2583             transfer_to_location,
2584             organization_id,
2585             Decode(wms_task_type, -1, g_wms_task_type_putaway, wms_task_type) wms_task_type,
2586 	    parent_line_id,
2587 	    transaction_action_id,
2588 	    transaction_source_type_id,
2589 	    transaction_type_id,
2590 	    move_order_line_id,
2591 	    transaction_uom,
2592 	    transaction_quantity,
2593 	    lpn_id,
2594 	    content_lpn_id,
2595 	    transfer_lpn_id,
2596 	    secondary_uom_code,                        --Bug# 7716519 : need to include sec_uom in cursor as this is used to update MTRl later
2597             secondary_transaction_quantity                   --Bug# 7716519 : need to include sec_qty in cursor as this is used to update MTRl later
2598 	    FROM mtl_material_transactions_temp
2599 	    WHERE transaction_temp_id=p_source_task_id;
2600 
2601        l_inbound_doc_rec  c_inbound_document_details%ROWTYPE;
2602        l_mmtt_rec mtl_material_transactions_temp%ROWTYPE;
2603 
2604        -- cursor to get the current active operation instance
2605        -- for the source task
2606 
2607        CURSOR c_wooi_data_rec IS
2608 	  SELECT wooi.operation_instance_id,
2609 	    wooi.operation_type_id,
2610 	    wooi.operation_plan_detail_id,
2611 	    wooi.op_plan_instance_id,
2612 	    wooi.operation_status,
2613 	    wooi.operation_sequence,
2614 	    wooi.sug_to_sub_code,
2615 	    wooi.sug_to_locator_id,
2616 	    wooi.organization_id,
2617 	    wopd.operation_plan_id
2618 	    FROM wms_op_operation_instances wooi,
2619 	    wms_op_plan_details wopd
2620 	    WHERE wooi.source_task_id = p_source_task_id
2621 	    AND wooi.activity_type_id = p_activity_id
2622 	    AND wooi.operation_plan_detail_id = wopd.operation_plan_detail_id
2623 	    ORDER BY wooi.operation_sequence DESC,
2624 	    wooi.operation_type_id DESC; -- this is added for crossdock, the last two operations will have the same operation_sequence. But we want to show drop first in this query
2625 --	    AND operation_status = g_op_ins_stat_active;
2626 
2627        l_wooi_data_rec c_wooi_data_rec%ROWTYPE;
2628        l_wooi_rec wms_op_operation_instances%ROWTYPE;
2629 
2630        -- cursor to get the plan detail for the next operation
2631        CURSOR c_next_plan_detail(v_operation_plan_id NUMBER,
2632 				 v_current_detail_sequence NUMBER) IS
2633 	  SELECT wopd1.operation_plan_detail_id
2634 	    , wopd1.operation_sequence
2635 	    , wopd1.is_in_inventory
2636 	    , wopd1.operation_type
2637 	    , Decode(Nvl(wopb.crossdock_to_wip_flag, 'N'),'N', Nvl(wopd1.subsequent_op_plan_id, 1),v_operation_plan_id) subsequent_op_plan_id-- added for planned xdocking
2638 	    FROM wms_op_plan_details wopd1,
2639 	    wms_op_plans_b wopb
2640 	    WHERE wopd1.operation_plan_id  = v_operation_plan_id
2641 	    AND wopb.operation_plan_id = v_operation_plan_id
2642 	    AND   wopd1.operation_sequence = (SELECT MIN(wopd2.operation_sequence)
2643 					      FROM wms_op_plan_details wopd2
2644 					      WHERE wopd2.operation_plan_id = v_operation_plan_id
2645 					      AND (wopd2.operation_sequence > v_current_detail_sequence  -- operations after the current
2646 						   OR (wopd2.operation_type = g_op_type_crossdock -- when current is a load operation for crossdock, sequence can be same as current operation
2647 						       AND wopd2.operation_sequence = v_current_detail_sequence)
2648 						   )
2649 					      );
2650 
2651        l_operation_plan_detail_rec c_next_plan_detail%ROWTYPE;
2652 
2653     BEGIN
2654 
2655        IF (l_debug = 1) THEN
2656 	  print_debug(' Entered. ',l_module_name,1);
2657 
2658 	  print_debug(' p_source_task_id==> '||p_source_task_id,l_module_name,3);
2659 	  print_debug(' p_activity_id==> '||p_activity_id,l_module_name,3);
2660 	  print_debug(' p_operation_type_id==> '||p_operation_type_id,l_module_name,3);
2661        END IF;
2662 
2663        x_return_status  := g_ret_sts_success;
2664        l_progress := 10;
2665        SAVEPOINT sp_complete_oprtn_instance;
2666 
2667 
2668        IF p_source_task_id IS NULL THEN
2669 	  IF (l_debug=1) THEN
2670 	     print_debug('Invalid input param. p_source_task_id Cannot be NULL.',l_module_name,4);
2671 	  END IF;
2672 	  RAISE FND_API.G_EXC_ERROR;
2673        END IF;
2674 
2675 
2676        IF p_activity_id IS NULL THEN
2677 	  IF (l_debug=1) THEN
2678 	     print_debug('Invalid input param. p_activity_id Cannot be NULL.',l_module_name,4);
2679 	  END IF;
2680 	  RAISE FND_API.G_EXC_ERROR;
2681        END IF;
2682 
2683        IF p_operation_type_id IS NOT NULL AND
2684 	 p_operation_type_id NOT IN (g_op_type_drop, g_op_type_load, g_op_type_inspect)THEN
2685 	  IF (l_debug=1) THEN
2686 	     print_debug('Invalid input param. p_operation_type_id can only be load, drop, inspect or NULL.',l_module_name,4);
2687 	  END IF;
2688 	  RAISE FND_API.G_EXC_ERROR;
2689        END IF;
2690 
2691        l_operation_type_id := p_operation_type_id;
2692 
2693        l_progress := 20;
2694 
2695        IF p_activity_id = g_op_activity_inbound THEN
2696 	  IF (l_debug=1) THEN
2697 	     print_debug('Inbound Activity.',l_module_name,4);
2698 	  END IF;
2699 
2700 	  OPEN c_inbound_document_details;
2701 
2702 	  FETCH c_inbound_document_details
2703 	    INTO l_inbound_doc_rec;
2704 
2705 	  IF c_inbound_document_details%notfound THEN
2706 	     IF (l_debug=1) THEN
2707 		print_debug('Invalid input param. p_source_task_id does not match to an MMTT record.',l_module_name,4);
2708 	     END IF;
2709 	     RAISE FND_API.G_EXC_ERROR;
2710 	  END IF;
2711 
2712 	  CLOSE c_inbound_document_details;
2713 
2714 
2715 	  l_mmtt_rec.transaction_temp_id := l_inbound_doc_rec.transaction_temp_id;
2716 	  l_mmtt_rec.operation_plan_id := l_inbound_doc_rec.operation_plan_id;
2717 	  l_mmtt_rec.inventory_item_id := l_inbound_doc_rec.inventory_item_id;
2718 	  l_mmtt_rec.subinventory_code := l_inbound_doc_rec.subinventory_code;
2719 	  l_mmtt_rec.locator_id := l_inbound_doc_rec.locator_id;
2720 	  l_mmtt_rec.transfer_subinventory := l_inbound_doc_rec.transfer_subinventory;
2721 	  l_mmtt_rec.transfer_to_location := l_inbound_doc_rec.transfer_to_location;
2722 	  l_mmtt_rec.organization_id := l_inbound_doc_rec.organization_id;
2723 	  l_mmtt_rec.wms_task_type := l_inbound_doc_rec.wms_task_type;
2724 	  l_mmtt_rec.parent_line_id := l_inbound_doc_rec.parent_line_id;
2725 	  l_mmtt_rec.transaction_action_id := l_inbound_doc_rec.transaction_action_id;
2726 	  l_mmtt_rec.transaction_source_type_id := l_inbound_doc_rec.transaction_source_type_id;
2727 	  l_mmtt_rec.transaction_type_id := l_inbound_doc_rec.transaction_type_id;
2728 	  l_mmtt_rec.move_order_line_id := l_inbound_doc_rec.move_order_line_id;
2729 	  l_mmtt_rec.transaction_uom := l_inbound_doc_rec.transaction_uom;
2730 	  l_mmtt_rec.transaction_quantity := l_inbound_doc_rec.transaction_quantity;
2731 	   -- Bug# 7716519, need to include sec_qty and sec_uom in mmtt_rec
2732 	  l_mmtt_rec.secondary_transaction_quantity := l_inbound_doc_rec.secondary_transaction_quantity;
2733 	  l_mmtt_rec.secondary_uom_code := l_inbound_doc_rec.secondary_uom_code;
2734 
2735 
2736 	  IF (l_debug=1) THEN
2737 	     print_debug('l_mmtt_rec.transaction_temp_id = '|| l_mmtt_rec.transaction_temp_id, l_module_name,4);
2738 	     print_debug('l_mmtt_rec.operation_plan_id = '|| l_mmtt_rec.operation_plan_id, l_module_name,4);
2739 	     print_debug('l_mmtt_rec.inventory_item_id = '|| l_mmtt_rec.inventory_item_id, l_module_name,4);
2740 	     print_debug('l_mmtt_rec.subinventory_code = '|| l_mmtt_rec.subinventory_code, l_module_name,4);
2741 	     print_debug('l_mmtt_rec.locator_id = '|| l_mmtt_rec.locator_id, l_module_name,4);
2742 	     print_debug('l_mmtt_rec.transfer_subinventory = '|| l_mmtt_rec.transfer_subinventory, l_module_name,4);
2743 	     print_debug('l_mmtt_rec.transfer_to_location  = '|| l_mmtt_rec.transfer_to_location , l_module_name,4);
2744 	     print_debug('l_mmtt_rec.organization_id = '|| l_mmtt_rec.organization_id, l_module_name,4);
2745 	     print_debug('l_mmtt_rec.wms_task_type = '|| l_mmtt_rec.wms_task_type, l_module_name,4);
2746 	     print_debug('l_mmtt_rec.parent_line_id = '|| l_mmtt_rec.parent_line_id, l_module_name,4);
2747 	     print_debug('l_mmtt_rec.transaction_action_id = '|| l_mmtt_rec.transaction_action_id, l_module_name,4);
2748 	     print_debug('l_mmtt_rec.transaction_source_type_id = '|| l_mmtt_rec.transaction_source_type_id, l_module_name,4);
2749 	     print_debug('l_mmtt_rec.transaction_type_id = '|| l_mmtt_rec.transaction_type_id, l_module_name,4);
2750 	     print_debug('l_mmtt_rec.move_order_line_id = '|| l_mmtt_rec.move_order_line_id, l_module_name,4);
2751 	     print_debug('l_mmtt_rec.transaction_uom = '|| l_mmtt_rec.transaction_uom, l_module_name,4);
2752 	     print_debug('l_mmtt_rec.transaction_quantity = '|| l_mmtt_rec.transaction_quantity, l_module_name,4);
2753 	     print_debug('l_inbound_doc_rec.lpn_id = '||l_inbound_doc_rec.lpn_id , l_module_name,4);
2754 	     print_debug('l_inbound_doc_rec.content_lpn_id = '||l_inbound_doc_rec.content_lpn_id , l_module_name,4);
2755 	     print_debug('l_inbound_doc_rec.transfer_lpn_id = '||l_inbound_doc_rec.transfer_lpn_id , l_module_name,4);
2756 
2757 	  END IF;
2758 
2759 
2760 	  l_progress := 23;
2761 
2762 	  IF l_mmtt_rec.operation_plan_id IS NOT NULL THEN
2763 	     -- fetch the current active operation instance for this task
2764 	     OPEN c_wooi_data_rec;
2765 
2766 	     FETCH c_wooi_data_rec INTO l_wooi_data_rec;
2767 	     IF c_wooi_data_rec%notfound THEN
2768 		IF (l_debug = 1) THEN
2769 		   print_debug('Operation instance record does not exist for this task.',l_module_name,4);
2770 		END IF;
2771 		fnd_message.set_name('WMS', 'WMS_ATF_NO_ACTIVE_PLAN');
2772 		fnd_msg_pub.ADD;
2773 		RAISE FND_API.G_EXC_ERROR;
2774 	     END IF;
2775 
2776 	     CLOSE c_wooi_data_rec;
2777 
2778 	     IF (l_debug = 1) THEN
2779 
2780 		print_debug('l_wooi_data_rec.operation_instance_id = '|| l_wooi_data_rec.operation_instance_id,l_module_name,4);
2781 		print_debug('l_wooi_data_rec.operation_type_id = '|| l_wooi_data_rec.operation_type_id,l_module_name,4);
2782 		print_debug('l_wooi_data_rec.operation_plan_detail_id = '|| l_wooi_data_rec.operation_plan_detail_id,l_module_name,4);
2783 		print_debug('l_wooi_data_rec.operation_plan_id = '|| l_wooi_data_rec.operation_plan_id,l_module_name,4);
2784 		print_debug('l_wooi_data_rec.op_plan_instance_id = '|| l_wooi_data_rec.op_plan_instance_id,l_module_name,4);
2785 		print_debug('l_wooi_data_rec.operation_status = '|| l_wooi_data_rec.operation_status,l_module_name,4);
2786 		print_debug('l_wooi_data_rec.operation_sequence = '|| l_wooi_data_rec.operation_sequence,l_module_name,4);
2787 		print_debug('l_wooi_data_rec.sug_to_sub_code = '|| l_wooi_data_rec.sug_to_sub_code,l_module_name,4);
2788 		print_debug('l_wooi_data_rec.sug_to_locator_id = '|| l_wooi_data_rec.sug_to_locator_id,l_module_name,4);
2789 	     END IF;
2790 
2791 	     IF l_wooi_data_rec.operation_status = g_op_ins_stat_pending THEN
2792 		-- Normally complete_operation_instance should not be called
2793 		-- when operation instance is pending.
2794 		-- It only occurs when complete_operation_instance is called
2795 		-- from packing workbenches receiving TM call.
2796 		-- In this case we raise an exception to by-pass all the logic below,
2797 		-- and return a special x_error_code
2798 		IF (l_debug = 1) THEN
2799 		   print_debug('Operation instance pending.',l_module_name,4);
2800 		END IF;
2801 		raise_application_error(COMPLETE_PENDING_OP,'Complete a pending operation instance');
2802 	      ELSIF l_wooi_data_rec.operation_status <> g_op_ins_stat_active THEN
2803 		IF (l_debug = 1) THEN
2804 		   print_debug('Invalid operation instance.',l_module_name,4);
2805 		END IF;
2806 		raise_application_error(INVALID_STATUS_FOR_OPERATION,'Invalid operation instance');
2807 	     END IF;
2808 
2809 	  END IF;
2810 
2811 
2812 	  l_progress := 25;
2813 
2814          BEGIN
2815 	    SELECT task_id, status
2816 	      INTO l_wdt_task_id, l_wdt_status
2817 	      FROM wms_dispatched_tasks
2818 	      WHERE transaction_temp_id = p_source_task_id
2819 	      AND task_type IN (g_wms_task_type_putaway, g_wms_task_type_inspect);
2820 
2821 	 EXCEPTION
2822 	    WHEN no_data_found THEN
2823 	       IF (l_debug=1) THEN
2824 		  print_debug('WDT does not exist for this MMTT.',l_module_name,4);
2825 	       END IF;
2826 	       RAISE FND_API.G_EXC_ERROR;
2827 
2828 	 END;
2829 
2830 	 l_progress := 30;
2831 
2832 	 IF (l_debug=1) THEN
2833 	    print_debug('l_wdt_task_id = '||l_wdt_task_id,l_module_name,4);
2834 	    print_debug('l_wdt_status = '||l_wdt_status,l_module_name,4);
2835 	 END IF;
2836 
2837 	 IF l_wdt_status NOT IN (g_task_status_loaded, g_task_status_dispatched) THEN
2838 	    IF (l_debug=1) THEN
2839 	       print_debug('Invalid WDT status.' ,l_module_name,4);
2840 	    END IF;
2841 	    fnd_message.set_name('WMS', 'WMS_ATF_INVALID_TASK_STATUS');
2842 	    fnd_msg_pub.ADD;
2843 	    RAISE FND_API.G_EXC_ERROR;
2844 	 END IF; -- IF l_wdt_status NOT IN (g_task_status_loaded, g_task_status_dispatched)
2845 
2846        END IF;  -- IF p_activity_id = g_op_activity_inbound
2847 
2848        l_progress := 30;
2849 
2850 
2851        IF l_wdt_status = g_task_status_dispatched THEN
2852 
2853 
2854 	  --This can happened for two operations today,
2855 	  --1.	Inspect
2856 	  --2.	Load
2857 
2858 	  --NOTE: Today the status of WDT is going to be 'Dispatched' for load and inspect
2859 	  --      (for drop it will be 'Loaded').
2860 	  --      But for Outbound/Warehousing tasks or when we have Inbound
2861 	  --      dispatched tasks then we CAN have a status of 'Active' for WDT records
2862 
2863 	  IF (l_debug=1) THEN
2864 	     print_debug('WDT status is dispatched.' ,l_module_name,4);
2865 	  END IF;
2866 
2867 
2868 	  IF p_operation_type_id = g_op_type_inspect THEN
2869 
2870 
2871 	     IF (l_debug=1) THEN
2872 		print_debug('Current operation is inspect.' ,l_module_name,4);
2873 		print_debug('Set l_operation_type_id to inspect.' ,l_module_name,4);
2874 
2875 		print_debug('Before calling wms_op_runtime_pvt_apis.archive_dispatched_tasks with following parameters' ,l_module_name,4);
2876 		print_debug('p_task_id => '|| l_wdt_task_id,l_module_name,4);
2877 		print_debug('p_source_task_id => '|| p_source_task_id,l_module_name,4);
2878 		print_debug('p_activity_type_id => '|| p_activity_id,l_module_name,4);
2879 	     END IF;
2880 
2881 	     l_operation_type_id := g_op_type_inspect;
2882 
2883 	     l_progress :=  40;
2884 
2885 	     wms_op_runtime_pvt_apis.archive_dispatched_tasks
2886 	       (
2887 		x_return_status         => l_return_status
2888 		, x_msg_count           => l_msg_count
2889 		, x_msg_data            => l_msg_data
2890 		, p_task_id             => l_wdt_task_id
2891 		, p_source_task_id      => p_source_task_id
2892 		, p_activity_type_id    => p_activity_id
2893 		, p_op_plan_instance_id => NULL
2894       , p_op_plan_status      => NULL
2895 		);
2896 
2897 	     l_progress :=  50;
2898 
2899 	     IF (l_debug=1) THEN
2900 		print_debug('After calling wms_op_runtime_pvt_apis.archive_dispatched_tasks .' ,l_module_name,4);
2901 		print_debug('x_return_status => '|| l_return_status,l_module_name,4);
2902 		print_debug('x_msg_count => '|| l_msg_count,l_module_name,4);
2903 		print_debug('x_msg_data => '|| l_msg_data,l_module_name,4);
2904 	     END IF;
2905 
2906 
2907 	     IF l_return_status <>FND_API.g_ret_sts_success THEN
2908 		IF (l_debug=1) THEN
2909 		   print_debug('wms_op_runtime_pvt_apis.archive_dispatched_tasks finished with error. l_return_status = ' || l_return_status,l_module_name,4);
2910 		END IF;
2911 		fnd_message.set_name('WMS', 'WMS_ATF_ARCHIVE_TASK_FAILURE');
2912 		fnd_msg_pub.ADD;
2913 		RAISE FND_API.G_EXC_ERROR;
2914 	     END IF;
2915 
2916 	   ELSIF p_operation_type_id IS NULL OR p_operation_type_id = g_op_type_load THEN
2917 	     IF (l_debug=1) THEN
2918 		print_debug('Current operation is load. Update WDT to loaded.' ,l_module_name,4);
2919 	     END IF;
2920 
2921 
2922 	     IF (l_debug=1) THEN
2923 		print_debug('Set l_operation_type_id to load.' ,l_module_name,4);
2924 	     END IF;
2925 
2926 	     l_operation_type_id := g_op_type_load;
2927 
2928 
2929 	     l_progress := 60;
2930 
2931 	     UPDATE wms_dispatched_tasks
2932 	       SET status = g_task_status_loaded,
2933 	       loaded_time = Sysdate,
2934 	       last_update_date = Sysdate,
2935 	       last_updated_by = fnd_global.user_id
2936 	       WHERE task_id = l_wdt_task_id;
2937 
2938 	     l_progress := 70;
2939 
2940 	   ELSE -- p_operation_type_id = g_op_type_inspect (not inspect or load or NULL)
2941 
2942 	     IF (l_debug=1) THEN
2943 		print_debug('Current operation ('|| p_operation_type_id ||') is not compatible with WDT status of dispatched.' ,l_module_name,4);
2944 	     END IF;
2945 	     fnd_message.set_name('WMS', 'WMS_ATF_INVALID_TASK_STATUS');
2946 	     fnd_msg_pub.ADD;
2947 	     RAISE FND_API.G_EXC_ERROR;
2948 
2949 	  END IF; -- IF p_operation_type_id = g_op_type_inspect
2950 
2951 
2952 
2953 	ELSIF l_wdt_status = g_task_status_loaded THEN
2954 	  --This can happened for only for one operation today, i.e. 'Drop'
2955 	  --Assumption here is that you always have a Load before a Drop, even for 'Single Step Drop'
2956 
2957 	  IF (l_debug=1) THEN
2958 	     print_debug('WDT status is loaded. Archive this WDT into WDTH.' ,l_module_name,4);
2959 	     print_debug('Set l_operation_type_id to Drop.' ,l_module_name,4);
2960 	  END IF;
2961 
2962 	  l_operation_type_id := g_op_type_drop;
2963 
2964 	  IF (l_debug=1) THEN
2965 
2966 	     print_debug('Drop - Before calling wms_op_runtime_pvt_apis.archive_dispatched_tasks with following parameters' ,l_module_name,4);
2967 	     print_debug('p_task_id => '|| l_wdt_task_id,l_module_name,4);
2968 	     print_debug('p_source_task_id => '|| p_source_task_id,l_module_name,4);
2969 	     print_debug('p_activity_type_id => '|| p_activity_id,l_module_name,4);
2970 	  END IF;
2971 
2972 	  l_progress :=  80;
2973 
2974 	  wms_op_runtime_pvt_apis.archive_dispatched_tasks
2975 	    (
2976 	     x_return_status         => l_return_status
2977 	     , x_msg_count           => l_msg_count
2978 	     , x_msg_data            => l_msg_data
2979 	     , p_task_id             => l_wdt_task_id
2980 	     , p_source_task_id      => p_source_task_id
2981 	     , p_activity_type_id    => p_activity_id
2982 	     , p_op_plan_instance_id => NULL
2983         , p_op_plan_status      => NULL
2984 	     );
2985 
2986 	  l_progress :=  90;
2987 
2988 	  IF (l_debug=1) THEN
2989 	     print_debug('Drop - After calling wms_op_runtime_pvt_apis.archive_dispatched_tasks .' ,l_module_name,4);
2990 	     print_debug('x_return_status => '|| l_return_status,l_module_name,4);
2991 	     print_debug('x_msg_count => '|| l_msg_count,l_module_name,4);
2992 	     print_debug('x_msg_data => '|| l_msg_data,l_module_name,4);
2993 	  END IF;
2994 
2995 
2996 	  IF l_return_status <>FND_API.g_ret_sts_success THEN
2997 	     IF (l_debug=1) THEN
2998 		print_debug('Drop - wms_op_runtime_pvt_apis.archive_dispatched_tasks finished with error. l_return_status = ' || l_return_status,l_module_name,4);
2999 	     END IF;
3000 	     fnd_message.set_name('WMS', 'WMS_ATF_ARCHIVE_TASK_FAILURE');
3001 	     fnd_msg_pub.ADD;
3002 	     RAISE FND_API.G_EXC_ERROR;
3003 	  END IF;
3004 
3005 	ELSE -- l_wdt_status = g_task_status_dispatched (not dispatched or loaded)
3006 
3007 	  IF (l_debug=1) THEN
3008 	     print_debug('Invalid WDT status.' ,l_module_name,4);
3009 	  END IF;
3010 	  fnd_message.set_name('WMS', 'WMS_ATF_INVALID_TASK_STATUS');
3011 	  fnd_msg_pub.ADD;
3012 	  RAISE FND_API.G_EXC_ERROR;
3013 
3014 
3015        END IF; -- IF l_wdt_status = g_task_status_dispatched
3016 
3017        -- Check operation plan stamped or not
3018        -- The assumption here is that we are looking at a pre-generated 11.5.9 MMTT record.
3019        -- This is possibly called either from user performing load, drop, or inspect operation.
3020        -- If 'Load', don't need to do anything to document records, simply return.
3021 
3022        -- If 'drop' or 'inspect', we need to update MOL and delete MMTT based on transaction type etc.
3023        -- This step basically becomes the last step of an operation plan.
3024        -- The 'Complete' inbound document handler should be able to handle this by passing 'p_last_operation_flag' = 'Y'.
3025 
3026        IF p_activity_id = g_op_activity_inbound THEN
3027 	  IF (l_debug=1) THEN
3028 	     print_debug('Inbound Activity.',l_module_name,4);
3029 	  END IF;
3030 
3031 	  IF l_mmtt_rec.operation_plan_id IS NULL THEN
3032 	     IF (l_debug=1) THEN
3033 		print_debug('Operation plan ID is null for this MMTT.',l_module_name,4);
3034 	     END IF;
3035 
3036 	     IF l_operation_type_id IN (g_op_type_drop, g_op_type_inspect) THEN
3037 
3038 		IF (l_debug=1) THEN
3039 		   print_debug('Current operation is DROP or INSPECT. Need to call COMPLETE document handler',l_module_name,4);
3040 		   print_debug('Operation_plan_ID NULL - Before calling wms_op_inbound_pvt.complete with following parameters:',l_module_name,4);
3041 		   print_debug('p_source_task_id => '|| p_source_task_id,l_module_name,4);
3042 		   print_debug('p_document_rec.transaction_temp_id => '|| l_mmtt_rec.transaction_temp_id,l_module_name,4);
3043 		   print_debug('p_operation_type_id => '|| l_operation_type_id,l_module_name,4);
3044 		   print_debug('p_next_operation_type_id => '|| '',l_module_name,4);
3045 		   print_debug('p_is_last_operation_flag => '|| 'Y',l_module_name,4);
3046 
3047 		END IF;
3048 
3049 		l_progress :=  100;
3050 
3051 		wms_op_inbound_pvt.complete
3052 		  (
3053 		   x_return_status              => l_return_status
3054 		   , x_msg_data                 => l_msg_data
3055 		   , x_msg_count                => l_msg_count
3056 		   , x_source_task_id           => l_new_mmtt_id
3057 		   , x_error_code               => l_atf_error_code
3058 		   , p_source_task_id           => p_source_task_id
3059 		   , p_document_rec             => l_mmtt_rec
3060 		   , p_operation_type_id        => l_operation_type_id
3061 		   , p_next_operation_type_id   => NULL
3062 		   , p_sug_to_sub_code		=> l_wooi_data_rec.sug_to_sub_code
3063 		   , p_sug_to_locator_id	=> l_wooi_data_rec.sug_to_locator_id
3064 		   , p_is_last_operation_flag   => 'Y'
3065 		   );
3066 
3067 		l_progress :=  102;
3068 
3069 		IF (l_debug=1) THEN
3070 		   print_debug('Inbound op plan null - After calling wms_op_inbound_pvt.complete. ', l_module_name,4);
3071 
3072 		   print_debug('x_return_status => '||l_return_status, l_module_name,4);
3073 		   print_debug('x_msg_data => '||l_msg_data, l_module_name,4);
3074 		   print_debug('x_msg_count => '||l_msg_count, l_module_name,4);
3075 		   print_debug('x_source_task_id => '||l_new_mmtt_id, l_module_name,4);
3076 		   print_debug('x_error_code => '||l_atf_error_code, l_module_name,4);
3077 		END IF;
3078       --bug 6924639
3079 		 IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3080         IF (l_debug = 1) THEN
3081           print_debug('Inbound op plan null - wms_op_inbound_pvt.complete finished with error. l_return_status = ' || l_return_status,l_module_name,4);
3082         END IF;
3083 
3084         fnd_message.set_name('WMS', 'WMS_WORK_FLOW_FAIL');
3085         fnd_msg_pub.ADD;
3086         RAISE fnd_api.g_exc_unexpected_error;
3087       ELSIF l_return_status = fnd_api.g_ret_sts_error THEN
3088         IF (l_debug = 1) THEN
3089           print_debug('Inbound op plan null - wms_op_inbound_pvt.complete finished with error. l_return_status = ' || l_return_status,l_module_name,4);
3090         END IF;
3091 
3092         fnd_message.set_name('WMS', 'WMS_WORK_FLOW_FAIL');
3093         fnd_msg_pub.ADD;
3094         RAISE fnd_api.g_exc_error;
3095       END IF;
3096      --bug 6924639
3097       /*
3098 		IF l_return_status <>FND_API.g_ret_sts_success THEN
3099 		   IF (l_debug=1) THEN
3100 		      print_debug('Inbound op plan null - wms_op_inbound_pvt.complete finished with error. l_return_status = ' || l_return_status,l_module_name,4);
3101 		   END IF;
3102 
3103 		   RAISE FND_API.G_EXC_ERROR;
3104 		END IF;
3105 		*/
3106 	     END IF; -- IF l_operation_type_id IN (g_op_type_drop, g_op_type_inspect)
3107 	     IF (l_debug=1) THEN
3108 		print_debug(' Exit execution. Because operation plan does not exist, no need to proceed with ATF related code.', l_module_name,4);
3109 		END IF;
3110 
3111 	     RETURN;  -- Return because operation plan does not exist, no need to proceed with ATF related code
3112 
3113 	  END IF; --  IF l_mmtt_rec.operation_plan_id IS NULL
3114 
3115 
3116 
3117        END IF;  -- IF p_activity_id = g_op_activity_inbound
3118 
3119 
3120 
3121        -- validate operation type on active operation instance
3122        -- against operation type passed in by the user
3123 
3124        IF l_operation_type_id <> l_wooi_data_rec.operation_type_id THEN
3125 	  IF (l_debug = 1) THEN
3126 	     print_debug('Operation type on operation instance : '||l_wooi_data_rec.operation_type_id||' does not match that passed in by user : '||l_operation_type_id, l_module_name,4);
3127 	  END IF;
3128 	  fnd_message.set_name('WMS', 'WMS_ATF_OPERATION_MISMATCH');
3129 	  fnd_msg_pub.ADD;
3130  	  RAISE FND_API.G_EXC_ERROR;
3131        END IF;
3132 
3133        IF (l_debug=1) THEN
3134 	  print_debug('Get MAX operation_sequence for the operation plan for detail ID: '||l_wooi_data_rec.operation_plan_detail_id,l_module_name,4);
3135        END IF;
3136 
3137        BEGIN
3138 	  l_progress := 104;
3139 
3140 	  SELECT MAX(wopd1.operation_sequence)
3141 	    INTO l_last_op_sequence
3142 	    FROM wms_op_plan_details wopd1
3143 	    WHERE wopd1.operation_plan_id =
3144 	    (SELECT wopd2.operation_plan_id
3145 	     FROM wms_op_plan_details wopd2
3146 	     WHERE wopd2.operation_plan_detail_id = l_wooi_data_rec.operation_plan_detail_id
3147 	     );
3148 
3149 	  l_progress := 106;
3150 
3151        EXCEPTION
3152 	  WHEN OTHERS THEN
3153 	     IF (l_debug=1) THEN
3154 		print_debug('Exception when getting max operation_sequence.' ,l_module_name,4);
3155 	     END IF;
3156 
3157 	     RAISE FND_API.G_EXC_ERROR;
3158        END;
3159 
3160 
3161        IF (l_debug=1) THEN
3162 	  print_debug('MAX operation_sequence (last step) is: '||l_last_op_sequence,l_module_name,4);
3163        END IF;
3164 
3165 
3166        -- Validate destination locator if this is not the last step of the plan
3167        IF p_activity_id = g_op_activity_inbound
3168 	 AND l_operation_type_id = g_op_type_drop
3169 	 AND l_wooi_data_rec.sug_to_locator_id IS NOT NULL
3170 	   AND l_last_op_sequence <> l_wooi_data_rec.operation_sequence
3171 	 THEN
3172 	  IF (l_debug=1) THEN
3173 	     print_debug('Inbound Activity and drop operation.',l_module_name,4);
3174 	  END IF;
3175 
3176 	  l_progress := 106.001;
3177 
3178 	  SELECT
3179 	    Nvl(lpn_controlled_flag, 2) -- 2 is non-lpn controlled
3180 	    INTO l_lpn_controlled_flag
3181 	    FROM mtl_secondary_inventories
3182 	    WHERE secondary_inventory_name = Nvl(l_mmtt_rec.transfer_subinventory,
3183 						 l_mmtt_rec.subinventory_code)
3184 	    AND organization_id = l_mmtt_rec.organization_id;
3185 
3186 	  l_progress := 106.002;
3187 
3188 	  IF (l_debug=1) THEN
3189 	     print_debug('LPN controlled code for sub '||Nvl(l_mmtt_rec.transfer_subinventory,l_mmtt_rec.subinventory_code)||' is '||l_lpn_controlled_flag,l_module_name,4);
3190 
3191 	  END IF;
3192 
3193 	  IF l_lpn_controlled_flag = 2 OR
3194 	    (l_inbound_doc_rec.content_lpn_id IS NULL AND
3195 	     l_inbound_doc_rec.transfer_lpn_id IS NULL) THEN
3196 	     IF (l_debug=1) THEN
3197 		print_debug('This sub is not LPN controlled, or user is dropping loose, this is an unforgivable error.',l_module_name,4);
3198 	     END IF;
3199 
3200 	     l_unforgivable_error_flag := 'Y';
3201 
3202 	  END IF;
3203 
3204 
3205 
3206 	  IF l_wooi_data_rec.sug_to_locator_id <> Nvl(l_mmtt_rec.transfer_to_location, l_mmtt_rec.locator_id)
3207 	    AND l_unforgivable_error_flag <> 'Y' THEN
3208 
3209 	     IF (l_debug=1) THEN
3210 		print_debug('Destination locator on operation instance : '||l_wooi_data_rec.sug_to_locator_id || ' does not match that on MMTT.' ,l_module_name,4);
3211 		print_debug('After user orverrides : '|| Nvl(l_mmtt_rec.transfer_to_location, l_mmtt_rec.locator_id)||' . Need to validate. ',l_module_name,4);
3212 	     END IF;
3213 
3214 	     l_progress := 108;
3215 
3216 	     SELECT
3217 	       loc_selection_criteria,
3218 	       loc_selection_api_id
3219 	       INTO
3220 	       l_loc_sel_criteria_id,
3221 	       l_loc_sel_api_id
3222 	       FROM wms_op_plan_details
3223 	       WHERE operation_plan_detail_id = l_wooi_data_rec.operation_plan_detail_id;
3224 	     l_progress := 110;
3225 
3226 	     IF (l_debug=1) THEN
3227 		print_debug('l_loc_sel_criteria_id = '||l_loc_sel_criteria_id,l_module_name,4);
3228 		print_debug('l_loc_sel_api_id = '||l_loc_sel_api_id,l_module_name,4);
3229 	     END IF;
3230 
3231 	     IF l_loc_sel_criteria_id = wms_globals.G_OP_DEST_PRE_SPECIFIED
3232 	       OR l_loc_sel_criteria_id = wms_globals.G_OP_DEST_SYS_SUGGESTED
3233 	       THEN
3234 
3235 		IF (l_debug=1) THEN
3236 		   print_debug('Unforgivable error: User overrides pre-specified or system-suggested locator, need to abort operation plan ',l_module_name,4);
3237 		END IF;
3238 		l_unforgivable_error_flag := 'Y';
3239 
3240 	      ELSIF l_loc_sel_criteria_id = wms_globals.G_OP_DEST_API
3241 		OR l_loc_sel_criteria_id = wms_globals.G_OP_DEST_CUSTOM_API
3242 		THEN
3243 
3244 		IF (l_debug=1) THEN
3245 		   print_debug('User overrides API derived locator, need to validate',l_module_name,4);
3246 
3247 		   print_debug('Before calling wms_atf_dest_locator.get_dest_locator with following parameters: ' , l_module_name, 4);
3248 		   print_debug('p_mode => ' || 2, l_module_name, 4);
3249 		   print_debug('p_task_id => ' || p_source_task_id, l_module_name, 4);
3250 		   print_debug('p_activity_type_id => ' || p_activity_id, l_module_name, 4);
3251 		   print_debug('p_hook_call_id => ' || l_loc_sel_api_id, l_module_name, 4);
3252 		   print_debug('p_locator_id => ' || Nvl(l_mmtt_rec.transfer_to_location, l_mmtt_rec.locator_id), l_module_name, 4);
3253 		   print_debug('p_item_id => ' || l_inbound_doc_rec.inventory_item_id, l_module_name, 4);
3254 		END IF;
3255 
3256 		wms_atf_dest_locator.get_dest_locator
3257 		  (
3258 		    x_return_status         => l_return_status
3259 		    ,  x_msg_count          => l_msg_count
3260 		    ,  x_msg_data           => l_msg_data
3261 		    ,  x_locator_id         => l_locator_id_dummy
3262 		    ,  x_subinventory_code  => l_subinventory_code_dummy
3263 		    ,  x_zone_id            => l_zone_id_dummy
3264 		    ,  x_loc_valid          => l_valid_flag
3265 		    ,  p_mode               => 2
3266 		    ,  p_task_id            => p_source_task_id
3267 		    ,  p_activity_type_id   => p_activity_id
3268 		    ,  p_hook_call_id       => l_loc_sel_api_id
3269 		    ,  p_locator_id         => Nvl(l_inbound_doc_rec.transfer_to_location, l_inbound_doc_rec.locator_id)
3270 		    ,  p_item_id            => l_inbound_doc_rec.inventory_item_id
3271 		    ,  p_api_version        => NULL
3272 		    ,  p_init_msg_list      => NULL
3273 		    ,  p_commit             => NULL
3274 		    );
3275 
3276 		IF (l_debug = 1) THEN
3277 		   print_debug('After calling wms_atf_dest_locator.get_dest_locator.' , l_module_name, 4);
3278 		   print_debug('x_return_status => ' || l_return_status, l_module_name, 4);
3279 		   print_debug('x_msg_count => ' || l_msg_count, l_module_name, 4);
3280 		   print_debug('x_msg_data => ' || l_msg_data, l_module_name, 4);
3281 		   print_debug('x_loc_valid => ' || l_valid_flag, l_module_name, 4);
3282 		END IF;
3283 
3284 		/*  When calling wms_atf_dest_locator.get_dest_locator for locator validation,
3285 		we do not check for x_return_status, because it will return E if it cannot find any locator
3286 
3287 		IF l_return_status <> g_ret_sts_success THEN
3288 		   IF (l_debug = 1) THEN
3289 		      print_debug('wms_atf_dest_locator.get_dest_locator failed:  l_return_status = '|| l_return_status, l_module_name, 4);
3290 		   END IF;
3291 
3292 		   RAISE FND_API.G_EXC_ERROR;
3293 
3294 		END IF;
3295 		  */
3296 		IF l_valid_flag = 'E' THEN
3297 		   IF (l_debug = 1) THEN
3298 		      print_debug('Invalid locator according to API, need to abort operation plan '|| l_return_status, l_module_name, 4);
3299 		   END IF;
3300 
3301 		   l_unforgivable_error_flag := 'Y';
3302 
3303 		END IF;
3304 
3305 	      ELSE
3306 
3307 		IF (l_debug=1) THEN
3308 		   print_debug('Invalid locator determination method.',l_module_name,4);
3309 		END IF;
3310 		fnd_message.set_name('WMS', 'WMS_ATF_LOC_DET_NOT_DEFINED');
3311 		fnd_msg_pub.ADD;
3312 		RAISE FND_API.G_EXC_ERROR;
3313 
3314 	     END IF;  -- IF NVL(l_loc_sel_criteria_id, 0) = wms_globals.G_OP_DEST_PRE_SPECIFIED
3315 
3316 	  END IF; -- IF l_wooi_data_rec.sug_to_locator_id <> Nvl(l_mmtt_rec.transfer_to_location
3317 
3318 	  IF l_unforgivable_error_flag = 'Y' THEN
3319 
3320 	     IF (l_debug=1) THEN
3321 		print_debug('Unforgivable error occured, abort operation plan, and complete the document execution',l_module_name,9);
3322 
3323 		print_debug('Before calling WMS_ATF_RUNTIME_PUB_APIS.abort_operation_plan with following parameters : ',l_module_name,9);
3324 		print_debug('p_source_task_id => '||p_source_task_id,l_module_name,9);
3325 		print_debug('p_activity_id => '||p_activity_id,l_module_name,9);
3326 	     END IF;
3327 
3328 	     wms_atf_runtime_pub_apis.abort_operation_plan
3329 	       (
3330 		x_return_status      => l_return_status
3331 		, x_msg_data         => l_msg_data
3332 		, x_msg_count        => l_msg_count
3333 		, x_error_code       => l_atf_error_code
3334 		, p_source_task_id   => p_source_task_id
3335 		, p_activity_type_id => p_activity_id);
3336 
3337 	     IF (l_debug=1) THEN
3338 		print_debug('After calling wms_atf_runtime_pub_apis.abort_operation_plan. ' ,l_module_name,9);
3339 		print_debug('x_return_status => '||l_return_status,l_module_name,9);
3340 		print_debug('x_msg_data => '||l_msg_data,l_module_name,9);
3341 		print_debug('x_msg_count => '||l_msg_count,l_module_name,9);
3342 		print_debug('x_error_code => '||l_atf_error_code,l_module_name,9);
3343 	     END IF;
3344 
3345 	     IF l_return_status <>FND_API.g_ret_sts_success THEN
3346 		IF (l_debug=1) THEN
3347 		   print_debug('wms_atf_runtime_pub_apis.abort_operation_plan finished with error. l_return_status = ' || l_return_status,l_module_name,4);
3348 		END IF;
3349 
3350 		RAISE FND_API.G_EXC_ERROR;
3351 	     END IF;
3352 
3353 
3354 	     IF (l_debug=1) THEN
3355 
3356 		print_debug('Unforgivable error - Before calling wms_op_inbound_pvt.complete with following parameters:',l_module_name,4);
3357 		print_debug('p_source_task_id => '|| p_source_task_id,l_module_name,4);
3358 		print_debug('p_document_rec.transaction_temp_id => '|| l_mmtt_rec.transaction_temp_id,l_module_name,4);
3359 		print_debug('p_operation_type_id => '|| l_operation_type_id,l_module_name,4);
3360 		print_debug('p_next_operation_type_id => '|| '',l_module_name,4);
3361 		print_debug('p_is_last_operation_flag => '|| 'Y',l_module_name,4);
3362 
3363 	     END IF;
3364 
3365 
3366 	     wms_op_inbound_pvt.complete
3367 	       (
3368 		x_return_status              => l_return_status
3369 		, x_msg_data                 => l_msg_data
3370 		, x_msg_count                => l_msg_count
3371 		, x_source_task_id           => l_new_mmtt_id
3372 		, x_error_code               => l_atf_error_code
3373 		, p_source_task_id           => p_source_task_id
3374 		, p_document_rec             => l_mmtt_rec
3375 		, p_operation_type_id        => l_operation_type_id
3376 		, p_next_operation_type_id   => NULL
3377 		, p_sug_to_sub_code	     => l_wooi_data_rec.sug_to_sub_code
3378 		, p_sug_to_locator_id	     => l_wooi_data_rec.sug_to_locator_id
3379 		, p_is_last_operation_flag   => 'Y'
3380 		);
3381 
3382 
3383 	     IF (l_debug=1) THEN
3384 		print_debug('Unforgivable Error - After calling wms_op_inbound_pvt.complete. ', l_module_name,4);
3385 
3386 		print_debug('x_return_status => '||l_return_status, l_module_name,4);
3387 		print_debug('x_msg_data => '||l_msg_data, l_module_name,4);
3388 		print_debug('x_msg_count => '||l_msg_count, l_module_name,4);
3389 		print_debug('x_source_task_id => '||l_new_mmtt_id, l_module_name,4);
3390 		print_debug('x_error_code => '||l_atf_error_code, l_module_name,4);
3391 	     END IF;
3392         --bug 6924639
3393          IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3394         IF (l_debug = 1) THEN
3395            print_debug('Unforgivable Error - wms_op_inbound_pvt.complete finished with error. l_return_status = ' || l_return_status,l_module_name,4);
3396         END IF;
3397 
3398         fnd_message.set_name('WMS', 'WMS_WORK_FLOW_FAIL');
3399         fnd_msg_pub.ADD;
3400         RAISE fnd_api.g_exc_unexpected_error;
3401       ELSIF l_return_status = fnd_api.g_ret_sts_error THEN
3402         IF (l_debug = 1) THEN
3403           print_debug('Unforgivable Error - wms_op_inbound_pvt.complete finished with error. l_return_status = ' || l_return_status,l_module_name,4);
3404         END IF;
3405 
3406         fnd_message.set_name('WMS', 'WMS_WORK_FLOW_FAIL');
3407         fnd_msg_pub.ADD;
3408         RAISE fnd_api.g_exc_error;
3409       END IF;
3410       --bug 6924639
3411 	     /*
3412 	     IF l_return_status <>FND_API.g_ret_sts_success THEN
3413 		IF (l_debug=1) THEN
3414 		   print_debug('Unforgivable Error - wms_op_inbound_pvt.complete finished with error. l_return_status = ' || l_return_status,l_module_name,4);
3415 		END IF;
3416 
3417 		RAISE FND_API.G_EXC_ERROR;
3418 	     END IF;
3419 	     */
3420 
3421 	     IF c_inbound_document_details%isopen THEN
3422 		CLOSE c_inbound_document_details;
3423 	     END IF;
3424 
3425 	     IF c_wooi_data_rec%isopen THEN
3426 		CLOSE c_wooi_data_rec;
3427 	     END IF;
3428 
3429 	     IF c_next_plan_detail%isopen THEN
3430 		CLOSE c_next_plan_detail;
3431 	     END IF;
3432 
3433 	     RETURN;  -- return for unforgivable error
3434 
3435 	  END IF;  -- IF l_unforgivable_error_flag = 'Y' THEN
3436 
3437 	  l_progress:=115;
3438 
3439 
3440        END IF; --  IF p_activity_id = g_op_activity_inbound
3441 
3442 
3443        -- Update the current operation instance to complete
3444        -- And populate other data for a completed operation instance:
3445        -- 'load from' sub/loc for load operation
3446        -- 'drop to' sub/loc for drop operation
3447        --
3448        -- For a receiving delivery the drop to sub/loc are populated in subinventory_code and locator_id
3449        -- while receiving transfer and inventory transfer they are populated in transfer_subinventory and transfer_to_location
3450        --
3451        -- It is not nessary to populate equipment, employee data, which should
3452        -- has been done in activate_operation_instance
3453 
3454        l_wooi_rec.operation_instance_id := l_wooi_data_rec.operation_instance_id;
3455        l_wooi_rec.operation_status := g_op_ins_stat_completed;
3456        l_wooi_rec.complete_time := Sysdate;
3457 
3458        IF (l_debug=1) THEN
3459 	  print_debug('Before updating WOOI: l_operation_type_id = ' ||l_operation_type_id,l_module_name,4);
3460        END if;
3461 
3462        IF l_operation_type_id = g_op_type_drop THEN
3463 	  l_wooi_rec.to_subinventory_code := Nvl(l_mmtt_rec.transfer_subinventory, l_mmtt_rec.subinventory_code);
3464 	  l_wooi_rec.to_locator_id := Nvl(l_mmtt_rec.transfer_to_location, l_mmtt_rec.locator_id);
3465 	ELSIF l_operation_type_id IN (g_op_type_inspect, g_op_type_load) THEN
3466 	  l_wooi_rec.from_subinventory_code := l_mmtt_rec.subinventory_code;
3467 	  l_wooi_rec.from_locator_id := l_mmtt_rec.locator_id;
3468        END IF;  -- IF l_operation_type_id = g_op_type_drop T
3469 
3470 
3471        IF (l_debug=1) THEN
3472 	  print_debug('Before calling wms_op_runtime_pvt_apis.update_operation_instance with following parameters: ',l_module_name,4);
3473 	  print_debug('l_wooi_rec.operation_instance_id => '|| l_wooi_rec.operation_instance_id,l_module_name,4);
3474 	  print_debug('l_wooi_rec.operation_status => '|| l_wooi_rec.operation_status,l_module_name,4);
3475 	  print_debug('l_wooi_rec.complete_time => '|| To_char(l_wooi_rec.complete_time),l_module_name,4);
3476 	  print_debug('l_wooi_rec.to_subinventory_code => '|| l_wooi_rec.to_subinventory_code,l_module_name,4);
3477 	  print_debug('l_wooi_rec.to_locator_id => '|| l_wooi_rec.to_locator_id,l_module_name,4);
3478 	  print_debug('l_wooi_rec.from_subinventory_code => '|| l_wooi_rec.from_subinventory_code,l_module_name,4);
3479 	  print_debug('l_wooi_rec.from_locator_id => '|| l_wooi_rec.from_locator_id,l_module_name,4);
3480        END IF;
3481 
3482        l_progress := 120;
3483 
3484        wms_op_runtime_pvt_apis.update_operation_instance
3485 	 (x_return_status     => l_return_status
3486 	  , x_msg_count       => l_msg_count
3487 	  , x_msg_data        => l_msg_data
3488 	  , p_update_rec      => l_wooi_rec);
3489 
3490        l_progress := 130;
3491 
3492        IF (l_debug=1) THEN
3493 	  print_debug('After calling wms_op_runtime_pvt_apis.update_operation_instance: ' ,l_module_name,4);
3494        	  print_debug('x_return_status => '|| l_return_status,l_module_name,4);
3495 	  print_debug('x_msg_count => '|| l_msg_count,l_module_name,4);
3496 	  print_debug('x_msg_data => '|| l_msg_data,l_module_name,4);
3497        END IF;
3498 
3499        IF l_return_status <>FND_API.g_ret_sts_success THEN
3500 	  IF (l_debug=1) THEN
3501 	     print_debug('wms_op_runtime_pvt_apis.update_operation_instance finished with error. l_return_status = ' || l_return_status,l_module_name,4);
3502 	  END IF;
3503 
3504 	  RAISE FND_API.G_EXC_ERROR;
3505        END IF;
3506 
3507 
3508        -- Handle situation where current operation is the last step in the plan.
3509 
3510        IF l_last_op_sequence = l_wooi_data_rec.operation_sequence  -- current operation sequence equals the max sequence in the plan
3511 	 AND l_operation_type_id <> g_op_type_load -- and this is not the load operation for a crossdock
3512 	 THEN
3513 
3514 	  -- this is the last operation in the plan
3515 	  IF (l_debug=1) THEN
3516 	     print_debug('Current operation is the last step of the operation plan.',l_module_name,4);
3517 	     print_debug('Before calling wms_op_runtime_pvt_apis.archive_dispatched_tasks with following parameters : ' , l_module_name,4);
3518 	     print_debug('p_task_id             => '||'', l_module_name,4);
3519 	     print_debug('p_source_task_id      => '||l_mmtt_rec.parent_line_id, l_module_name,4);
3520 	     print_debug('p_activity_type_id    => '||p_activity_id, l_module_name,4);
3521 	     print_debug('p_op_plan_instance_id => '||l_wooi_data_rec.op_plan_instance_id, l_module_name,4);
3522         print_debug('p_op_plan_status      => '||G_OP_INS_STAT_COMPLETED,l_module_name,4);
3523 	  END IF;
3524 
3525 	  -- Need to archive the parent MMTT record into WDTH
3526 	  wms_op_runtime_pvt_apis.archive_dispatched_tasks
3527 	    (
3528 	     x_return_status          => l_return_status
3529 	     , x_msg_count            => l_msg_count
3530 	     , x_msg_data             => l_msg_data
3531 	     , p_task_id              => NULL
3532 	     , p_source_task_id       => l_mmtt_rec.parent_line_id
3533 	     , p_activity_type_id     => p_activity_id
3534 	     , p_op_plan_instance_id  => l_wooi_data_rec.op_plan_instance_id
3535         , p_op_plan_status       => G_OP_INS_STAT_COMPLETED
3536 	     );
3537 
3538 	  IF (l_debug=1) THEN
3539 	     print_debug('After calling wms_op_runtime_pvt_apis.archive_dispatched_tasks.',l_module_name,4);
3540 	     print_debug('x_return_status => '|| l_return_status,l_module_name,4);
3541 	     print_debug('x_msg_count => '|| l_msg_count,l_module_name,4);
3542 	     print_debug('x_msg_data => '|| l_msg_data,l_module_name,4);
3543 
3544 	  END IF;
3545 
3546 	  IF l_return_status <>FND_API.g_ret_sts_success THEN
3547 	     IF (l_debug=1) THEN
3548 		print_debug('Last operation: archive parent MMTT - wms_op_runtime_pvt_apis.archive_dispatched_tasks finished with error. l_return_status = ' || l_return_status,l_module_name,4);
3549 	     END IF;
3550 	     fnd_message.set_name('WMS', 'WMS_ATF_ARCHIVE_TASK_FAILURE');
3551 	     fnd_msg_pub.ADD;
3552 	     RAISE FND_API.G_EXC_ERROR;
3553 	  END IF;
3554 
3555 	  IF (l_debug=1) THEN
3556 	     print_debug('Before calling wms_op_runtime_pvt_apis.complete_plan_instance with following parameters:', l_module_name,4);
3557 	     print_debug('p_op_plan_instance_id => '||l_wooi_data_rec.op_plan_instance_id, l_module_name,4);
3558 	  END IF;
3559 
3560 	  -- Complete the operation plan instance
3561 	  l_progress := 160;
3562 
3563 	  wms_op_runtime_pvt_apis.complete_plan_instance
3564 	    (x_return_status        => l_return_status,
3565 	     x_msg_data             => l_msg_data,
3566 	     x_msg_count            => l_msg_count,
3567 	     p_op_plan_instance_id  => l_wooi_data_rec.op_plan_instance_id
3568 	     );
3569 
3570 	  l_progress := 170;
3571 
3572 	  IF (l_debug=1) THEN
3573 	     print_debug('After calling wms_op_runtime_pvt_apis.complete_plan_instance. ', l_module_name,4);
3574 
3575 	     print_debug('x_return_status => '||l_return_status, l_module_name,4);
3576 	     print_debug('x_msg_data => '||l_msg_data, l_module_name,4);
3577 	     print_debug('x_msg_count => '||l_msg_count, l_module_name,4);
3578 	  END IF;
3579 
3580 	  IF l_return_status <>FND_API.g_ret_sts_success THEN
3581 	     IF (l_debug=1) THEN
3582 		print_debug('wms_op_runtime_pvt_apis.complete_plan_instance finished with error. l_return_status = ' || l_return_status,l_module_name,4);
3583 	     END IF;
3584 
3585 	     RAISE FND_API.G_EXC_ERROR;
3586 	  END IF;
3587 
3588 
3589 
3590 	  -- Complete the document
3591 
3592 	  IF p_activity_id = g_op_activity_inbound THEN
3593 
3594 
3595 	     IF (l_debug=1) THEN
3596 
3597 		print_debug('Last operation in plan and inbound - Before calling wms_op_inbound_pvt.complete with following parameters:',l_module_name,4);
3598 		print_debug('p_source_task_id => '|| p_source_task_id,l_module_name,4);
3599 		print_debug('p_document_rec.transaction_temp_id => '|| l_mmtt_rec.transaction_temp_id,l_module_name,4);
3600 		print_debug('p_operation_type_id => '|| l_operation_type_id,l_module_name,4);
3601 		print_debug('p_next_operation_type_id => '|| '',l_module_name,4);
3602 		print_debug('p_is_last_operation_flag => '|| 'Y',l_module_name,4);
3603 
3604 	     END IF;
3605 
3606 	     l_progress :=  180;
3607 
3608 	     wms_op_inbound_pvt.complete
3609 	       (
3610 		x_return_status              => l_return_status
3611 		, x_msg_data                 => l_msg_data
3612 		, x_msg_count                => l_msg_count
3613 		, x_source_task_id           => l_new_mmtt_id
3614 		, x_error_code               => l_atf_error_code
3615 		, p_source_task_id           => p_source_task_id
3616 		, p_document_rec             => l_mmtt_rec
3617 		, p_operation_type_id        => l_operation_type_id
3618 		, p_next_operation_type_id   => NULL
3619 		, p_sug_to_sub_code	     => l_wooi_data_rec.sug_to_sub_code
3620 		, p_sug_to_locator_id	     => l_wooi_data_rec.sug_to_locator_id
3621 		, p_is_last_operation_flag   => 'Y'
3622 		);
3623 
3624 	     l_progress :=  190;
3625 
3626 	     IF (l_debug=1) THEN
3627 		print_debug('Last operation in plan - After calling wms_op_inbound_pvt.complete. ', l_module_name,4);
3628 
3629 		print_debug('x_return_status => '||l_return_status, l_module_name,4);
3630 		print_debug('x_msg_data => '||l_msg_data, l_module_name,4);
3631 		print_debug('x_msg_count => '||l_msg_count, l_module_name,4);
3632 		print_debug('x_source_task_id => '||l_new_mmtt_id, l_module_name,4);
3633 		print_debug('x_error_code => '||l_atf_error_code, l_module_name,4);
3634 	     END IF;
3635         --bug 6924639
3636 	     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3637         IF (l_debug = 1) THEN
3638           print_debug('Inbound op plan null - wms_op_inbound_pvt.complete finished with error. l_return_status = ' || l_return_status,l_module_name,4);
3639         END IF;
3640 
3641         fnd_message.set_name('WMS', 'WMS_WORK_FLOW_FAIL');
3642         fnd_msg_pub.ADD;
3643         RAISE fnd_api.g_exc_unexpected_error;
3644       ELSIF l_return_status = fnd_api.g_ret_sts_error THEN
3645         IF (l_debug = 1) THEN
3646           print_debug('Inbound op plan null - wms_op_inbound_pvt.complete finished with error. l_return_status = ' || l_return_status,l_module_name,4);
3647         END IF;
3648 
3649         fnd_message.set_name('WMS', 'WMS_WORK_FLOW_FAIL');
3650         fnd_msg_pub.ADD;
3651         RAISE fnd_api.g_exc_error;
3652       END IF;
3653       --bug 6924639
3654         /*
3655 	     IF l_return_status <>FND_API.g_ret_sts_success THEN
3656 		IF (l_debug=1) THEN
3657 		   print_debug('Last operation in plan - wms_op_inbound_pvt.complete finished with error. l_return_status = ' || l_return_status,l_module_name,4);
3658 		END IF;
3659 
3660 		RAISE FND_API.G_EXC_ERROR;
3661 	     END IF;
3662 	  */
3663 	  END IF; -- IF p_activity_id = g_op_activity_inbound
3664 
3665 	ELSE -- this is NOT the last operation in the plan
3666 	  IF (l_debug=1) THEN
3667 	     print_debug('Current operation is NOT the last step of the operation plan.',l_module_name,4);
3668 	  END IF;
3669 
3670 	  -- query operation plan detail for the next operation
3671 	  OPEN c_next_plan_detail(l_wooi_data_rec.operation_plan_id,
3672 				  l_wooi_data_rec.operation_sequence);
3673 
3674 	  FETCH c_next_plan_detail INTO l_operation_plan_detail_rec;
3675 	  IF c_next_plan_detail%notfound THEN
3676 	     IF (l_debug=1) THEN
3677 		print_debug('Current operation does not have a next operation.' ,l_module_name,4);
3678 	     END IF;
3679 
3680 	     fnd_message.set_name('WMS', 'WMS_ATF_NO_NEXT_OPERATION');
3681 	     fnd_msg_pub.ADD;
3682 
3683 	     RAISE FND_API.G_EXC_ERROR;
3684 
3685 	  END IF;
3686 
3687 	  CLOSE c_next_plan_detail;
3688 
3689 	  -- construct operation instance record for the next operation in the plan
3690 
3691 	  l_wooi_rec := NULL;
3692 
3693 	  l_wooi_rec.operation_plan_detail_id := l_operation_plan_detail_rec.operation_plan_detail_id;
3694 	  l_wooi_rec.op_plan_instance_id      := l_wooi_data_rec.op_plan_instance_id ;
3695 	  l_wooi_rec.organization_id          := l_wooi_data_rec.organization_id;
3696 	  l_wooi_rec.operation_status         := g_op_ins_stat_pending;
3697 	  l_wooi_rec.operation_sequence       := l_operation_plan_detail_rec.operation_sequence; -- for crossdock, load and drop wooi will have the same sequence.
3698 	  l_wooi_rec.is_in_inventory          := Nvl(l_operation_plan_detail_rec.is_in_inventory, 'N');
3699 	  l_wooi_rec.from_subinventory_code   := Nvl(l_mmtt_rec.transfer_subinventory, l_mmtt_rec.subinventory_code);
3700 	  l_wooi_rec.from_locator_id          := Nvl(l_mmtt_rec.transfer_to_location, l_mmtt_rec.locator_id);
3701 	  l_wooi_rec.activity_type_id         := p_activity_id;
3702 	  -- Need to set next operation_type properly.
3703 	  -- Current operation 'Load' is always followed by a 'Drop'.
3704 	  -- Otherwise, If WOPD definition for next step is 'crossdock', need to create a 'Load' operation instance.
3705 	  -- Otherwise, use WOPD's operation type
3706 
3707 	  IF l_operation_type_id = g_op_type_load THEN
3708 	     l_wooi_rec.operation_type_id := g_op_type_drop;
3709 	   ELSIF l_operation_plan_detail_rec.operation_type = g_op_type_crossdock THEN
3710 	     l_wooi_rec.operation_type_id := g_op_type_load;
3711 	   ELSE -- other operation type
3712 	     l_wooi_rec.operation_type_id := l_operation_plan_detail_rec.operation_type;
3713 	  END IF; -- IF l_operation_type_id = g_op_type_load
3714 
3715 
3716 	  IF (l_debug=1) THEN
3717 	     print_debug('l_wooi_rec.operation_plan_detail_id = '||l_wooi_rec.operation_plan_detail_id,l_module_name,4);
3718 	     print_debug('l_wooi_rec.op_plan_instance_id = '||l_wooi_rec.op_plan_instance_id,l_module_name,4);
3719 	     print_debug('l_wooi_rec.organization_id = '||l_wooi_rec.organization_id,l_module_name,4);
3720 	     print_debug('l_wooi_rec.operation_status = '||l_wooi_rec.operation_status,l_module_name,4);
3721 	     print_debug('l_wooi_rec.operation_sequence = '||l_wooi_rec.operation_sequence,l_module_name,4);
3722 	     print_debug('l_wooi_rec.is_in_inventory = '||l_wooi_rec.is_in_inventory,l_module_name,4);
3723 	     print_debug('l_wooi_rec.from_subinventory_code = '||l_wooi_rec.from_subinventory_code,l_module_name,4);
3724 	     print_debug('l_wooi_rec.from_locator_id = '||l_wooi_rec.from_locator_id,l_module_name,4);
3725 	     print_debug('l_wooi_rec.operation_type_id = '||l_wooi_rec.operation_type_id,l_module_name,4);
3726 	     print_debug('l_wooi_rec.activity_type_id = '||l_wooi_rec.activity_type_id,l_module_name,4);
3727 	     print_debug('l_operation_plan_detail_rec.subsequent_op_plan_id        ==>'||l_operation_plan_detail_rec.subsequent_op_plan_id,l_module_name,4);
3728 
3729 	  END IF;
3730 
3731 	  -- update document table
3732 
3733 	  IF p_activity_id = g_op_activity_inbound THEN
3734 
3735 
3736 	     IF (l_debug=1) THEN
3737 		print_debug('NOT Last operation in plan and inbound - Before calling wms_op_inbound_pvt.complete with following parameters:',l_module_name,4);
3738 		print_debug('p_source_task_id => '|| p_source_task_id,l_module_name,4);
3739 		print_debug('p_document_rec.transaction_temp_id => '|| l_mmtt_rec.transaction_temp_id,l_module_name,4);
3740 		print_debug('p_operation_type_id => '|| l_operation_type_id,l_module_name,4);
3741 		print_debug('p_next_operation_type_id => '|| l_operation_plan_detail_rec.operation_type, l_module_name,4);
3742 		print_debug('p_is_last_operation_flag => '|| 'N',l_module_name,4);
3743 	     END IF;
3744 
3745 	     l_progress :=  220;
3746 	     /*{{
3747 		 When completing the drop operation right before the crossdock operation
3748 		 need to stamp subsequent outbound operation plan ID to the last task
3749 
3750 	     }}
3751 	       */
3752 
3753 	     wms_op_inbound_pvt.complete
3754 	       (
3755 		x_return_status              => l_return_status
3756 		, x_msg_data                 => l_msg_data
3757 		, x_msg_count                => l_msg_count
3758 		, x_source_task_id           => l_new_mmtt_id
3759 		, x_error_code               => l_atf_error_code
3760 		, p_source_task_id           => p_source_task_id
3761 		, p_document_rec             => l_mmtt_rec
3762 		, p_operation_type_id        => l_operation_type_id
3763 		, p_next_operation_type_id   => l_operation_plan_detail_rec.operation_type
3764 		, p_sug_to_sub_code	     => l_wooi_data_rec.sug_to_sub_code
3765 		, p_sug_to_locator_id	     => l_wooi_data_rec.sug_to_locator_id
3766 		, p_is_last_operation_flag   => 'N'
3767 		, p_subsequent_op_plan_id    => l_operation_plan_detail_rec.subsequent_op_plan_id
3768 		);
3769 
3770 	     l_progress :=  230;
3771 
3772 	     IF (l_debug=1) THEN
3773 		print_debug('NOT Last operation in plan - After calling wms_op_inbound_pvt.complete. ', l_module_name,4);
3774 
3775 		print_debug('x_return_status => '||l_return_status, l_module_name,4);
3776 		print_debug('x_msg_data => '||l_msg_data, l_module_name,4);
3777 		print_debug('x_msg_count => '||l_msg_count, l_module_name,4);
3778 		print_debug('x_source_task_id => '||l_new_mmtt_id, l_module_name,4);
3779 		print_debug('x_error_code => '||l_atf_error_code, l_module_name,4);
3780 	     END IF;
3781 	     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3782         IF (l_debug = 1) THEN
3783           print_debug('Inbound op plan null - wms_op_inbound_pvt.complete finished with error. l_return_status = ' || l_return_status,l_module_name,4);
3784         END IF;
3785 
3786         fnd_message.set_name('WMS', 'WMS_WORK_FLOW_FAIL');
3787         fnd_msg_pub.ADD;
3788         RAISE fnd_api.g_exc_unexpected_error;
3789       ELSIF l_return_status = fnd_api.g_ret_sts_error THEN
3790         IF (l_debug = 1) THEN
3791           print_debug('Inbound op plan null - wms_op_inbound_pvt.complete finished with error. l_return_status = ' || l_return_status,l_module_name,4);
3792         END IF;
3793 
3794         fnd_message.set_name('WMS', 'WMS_WORK_FLOW_FAIL');
3795         fnd_msg_pub.ADD;
3796         RAISE fnd_api.g_exc_error;
3797       END IF;
3798 
3799 
3800 	  END IF; -- IF p_activity_id = g_op_activity_inbound
3801 
3802 	  l_wooi_rec.source_task_id := Nvl(l_new_mmtt_id, p_source_task_id);
3803 
3804 	  IF (l_debug=1) THEN
3805 	     print_debug('l_wooi_rec.source_task_id = '||l_wooi_rec.source_task_id,l_module_name,4);
3806 	  END IF;
3807 
3808 	  -- create next operation instance
3809 
3810 	  IF (l_debug=1) THEN
3811 	     print_debug('Before calling wms_op_runtime_pvt_apis.insert_operation_instance:' ,l_module_name,4);
3812 	  END IF;
3813 
3814 	  wms_op_runtime_pvt_apis.insert_operation_instance
3815 	    (
3816 	     x_return_status => l_return_status        ,
3817 	     x_msg_count     => x_msg_count            ,
3818 	     x_msg_data      => x_msg_data             ,
3819 	     p_insert_rec    => l_wooi_rec );
3820 
3821 	  IF (l_debug=1) THEN
3822 	     print_debug('After calling wms_op_runtime_pvt_apis.insert_operation_instance:' ,l_module_name,4);
3823 	  END IF;
3824 
3825 	  IF l_return_status <>FND_API.g_ret_sts_success THEN
3826 	     IF (l_debug=1) THEN
3827 		print_debug('wms_op_runtime_pvt_apis.insert_operation_instance finished with error. l_return_status = ' || l_return_status,l_module_name,4);
3828 	     END IF;
3829 
3830 	     RAISE FND_API.G_EXC_ERROR;
3831 	  END IF;
3832 
3833 
3834        END IF;  -- IF l_last_op_sequence = l_wooi_data_rec.operation_sequence
3835 
3836 
3837        IF (l_debug = 1) THEN
3838 	  print_debug('x_return_status ==> '||x_return_status,l_module_name,3);
3839 	  print_debug('x_msg_data ==> '||x_msg_data,l_module_name,3);
3840 	  print_debug('x_msg_count ==> '||x_msg_count,l_module_name,3);
3841 	  print_debug('x_error_code ==> '||x_error_code,l_module_name,3);
3842 
3843 	  print_debug(' Before Exiting . ',l_module_name,3);
3844        END IF;
3845 
3846   EXCEPTION
3847 
3848      WHEN fnd_api.g_exc_error THEN
3849 	IF (l_debug=1) THEN
3850 	   print_debug('Error (fnd_api.g_exc_error) occured at'||l_progress,l_module_name,1);
3851 	END IF;
3852 	x_return_status:=FND_API.G_RET_STS_ERROR;
3853 	fnd_message.set_name('WMS', 'WMS_ATF_COMPLETE_OP_FAILURE');
3854 	fnd_msg_pub.ADD;
3855 --	fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
3856 	ROLLBACK TO sp_complete_oprtn_instance;
3857 
3858 	IF (SQLCODE<-20000) THEN
3859 	   IF (l_debug=1) THEN
3860               print_debug('This is a user defined exception',l_module_name,1);
3861 	   END IF;
3862 
3863 	   x_error_code:=-(SQLCODE+20000);
3864 	END IF;
3865 
3866 
3867 	IF c_inbound_document_details%isopen THEN
3868 	   CLOSE c_inbound_document_details;
3869 	END IF;
3870 
3871 	IF c_wooi_data_rec%isopen THEN
3872 	   CLOSE c_wooi_data_rec;
3873 	END IF;
3874 
3875 	IF c_next_plan_detail%isopen THEN
3876 	   CLOSE c_next_plan_detail;
3877 	END IF;
3878 
3879 
3880      WHEN fnd_api.g_exc_unexpected_error THEN
3881 
3882 	x_return_status:=FND_API.G_RET_STS_UNEXP_ERROR;
3883 	IF (l_debug=1) THEN
3884 	   print_debug('Unexpected Error (fnd_api.g_exc_unexpected_error) occured at '||l_progress,l_module_name,3);
3885 	END IF;
3886 	IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
3887 	   fnd_msg_pub.add_exc_msg(g_pkg_name, l_module_name);
3888 	END IF;
3889 	--	fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
3890 	fnd_message.set_name('WMS', 'WMS_ATF_COMPLETE_OP_FAILURE');
3891 	fnd_msg_pub.ADD;
3892 
3893 	IF (SQLCODE<-20000) THEN
3894 	   IF (l_debug=1) THEN
3895               print_debug('This is a user defined exception',l_module_name,1);
3896 	   END IF;
3897 
3898 	   x_error_code:=-(SQLCODE+20000);
3899 	END IF;
3900 
3901 	ROLLBACK TO sp_complete_oprtn_instance;
3902 
3903 	IF c_inbound_document_details%isopen THEN
3904 	   CLOSE c_inbound_document_details;
3905 	END IF;
3906 
3907 	IF c_wooi_data_rec%isopen THEN
3908 	   CLOSE c_wooi_data_rec;
3909 	END IF;
3910 
3911 	IF c_next_plan_detail%isopen THEN
3912 	   CLOSE c_next_plan_detail;
3913 	END IF;
3914 
3915      WHEN OTHERS THEN
3916 
3917 	x_return_status:=FND_API.G_RET_STS_UNEXP_ERROR;
3918 	IF (l_debug=1) THEN
3919 	   print_debug('Other Error occured at '||l_progress,l_module_name,1);
3920 	   IF SQLCODE IS NOT NULL AND SQLCODE >= -20000
3921 	     THEN
3922 	      print_debug('With SQL error : ' || SQLERRM(SQLCODE), l_module_name,1);
3923 	   END IF;
3924 	END IF;
3925 
3926 	IF (SQLCODE<-20000) THEN
3927 	   IF (l_debug=1) THEN
3928               print_debug('This is a user defined exception',l_module_name,1);
3929 	   END IF;
3930 
3931 	   x_error_code:=-(SQLCODE+20000);
3932 	END IF;
3933 
3934 	IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
3935 	   fnd_msg_pub.add_exc_msg(g_pkg_name, l_module_name);
3936 	END IF;
3937 	--	fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
3938 	fnd_message.set_name('WMS', 'WMS_ATF_COMPLETE_OP_FAILURE');
3939 	fnd_msg_pub.ADD;
3940 
3941 	ROLLBACK TO sp_complete_oprtn_instance;
3942 
3943 	IF c_inbound_document_details%isopen THEN
3944 	   CLOSE c_inbound_document_details;
3945 	END IF;
3946 
3947 	IF c_wooi_data_rec%isopen THEN
3948 	   CLOSE c_wooi_data_rec;
3949 	END IF;
3950 
3951 	IF c_next_plan_detail%isopen THEN
3952 	   CLOSE c_next_plan_detail;
3953 	END IF;
3954 
3955  END Complete_OPERATION_INSTANCE;
3956 
3957   /*
3958   *    Following is overloaded procedure
3959     *  without x_crossdock_prim_quantity and x_crossdock_flag.
3960     *  This is for the convenience of existing RCV call.
3961   */
3962 
3963  PROCEDURE validate_operation
3964     (
3965      x_return_status         OUT  NOCOPY  VARCHAR2 ,
3966      x_msg_data              OUT  NOCOPY  FND_NEW_MESSAGES.MESSAGE_TEXT%TYPE ,
3967      x_msg_count             OUT  NOCOPY  NUMBER,
3968      x_error_code            OUT  NOCOPY  NUMBER ,
3969      x_inspection_flag       OUT  NOCOPY  NUMBER ,
3970      x_load_flag             OUT  NOCOPY  NUMBER ,
3971      x_drop_flag             OUT  NOCOPY  NUMBER ,
3972      x_load_prim_quantity    OUT  NOCOPY  NUMBER ,
3973      x_drop_prim_quantity    OUT  NOCOPY  NUMBER ,
3974      x_inspect_prim_quantity OUT  NOCOPY  NUMBER ,
3975      p_source_task_id        IN           NUMBER ,
3976      p_move_order_line_id    IN           NUMBER ,
3977      p_inventory_item_id     IN           NUMBER ,
3978      p_LPN_ID                IN           NUMBER ,
3979      p_activity_type_id      IN           NUMBER ,
3980      p_organization_id       IN           NUMBER ,
3981      p_lot_number            IN           VARCHAR2 DEFAULT NULL ,
3982      p_revision              IN           VARCHAR2 DEFAULT NULL) IS
3983 
3984 	l_crossdock_flag NUMBER;
3985 	l_crossdock_prim_quantity NUMBER;
3986  BEGIN
3987 
3988     validate_operation
3989       (
3990        x_return_status         => x_return_status,
3991        x_msg_data              => x_msg_data,
3992        x_msg_count             => x_msg_count,
3993        x_error_code            => x_error_code,
3994        x_inspection_flag       => x_inspection_flag,
3995        x_load_flag             => x_load_flag,
3996        x_drop_flag             => x_drop_flag,
3997        x_crossdock_flag        => l_crossdock_flag,
3998        x_load_prim_quantity    => x_load_prim_quantity,
3999        x_drop_prim_quantity    => x_drop_prim_quantity,
4000        x_inspect_prim_quantity => x_inspect_prim_quantity,
4001        x_crossdock_prim_quantity => l_crossdock_prim_quantity,
4002        p_source_task_id        => p_source_task_id,
4003        p_move_order_line_id    => p_move_order_line_id,
4004        p_inventory_item_id     => p_inventory_item_id,
4005        p_LPN_ID                => p_lpn_id,
4006        p_activity_type_id      => p_activity_type_id,
4007        p_organization_id       => p_organization_id,
4008        p_lot_number            => p_lot_number,
4009        p_revision              => p_revision);
4010 
4011 
4012  END validate_operation;
4013 
4014 
4015 
4016 
4017 /**  Validate_Operation
4018   *   <p>For a given criteria (LPN, Item or task), this API returns certain information,
4019   *      operation type in particular, about the current pending operation.
4020   *      Based on the return value, it is caller procedure' s responsibility to determine
4021   *      whether user should be allowed to continue his/her operation.
4022   *  @param x_return_status        -Return Status
4023   *  @param x_msg_data             -Returns the Error message Data
4024   *  @param x_msg_count            -Returns the message count
4025   *  @param x_error_code           -Returns appropriate error code in case of any error.
4026   *  @param x_inspection_flag      -This parameter indicates the inspection requirement for the current pending
4027   *                                 operation instances for the given criteria. 1. No inspection is required;
4028   *                                  2. Part of the given criteria requires inspection;
4029   *                                  3. All operation instances for the given criteria require inspection
4030   *  @param x_drop_flag            -This parameter indicates the drop operation for the current pending
4031   *                                 operation instances for the given criteria.
4032   *                                 1. No drop is required; 2. Part of the given criteria requires drop;
4033   *                                 3. All operation instances for the given criteria require drop
4034   *  @param x_load_flag            -This parameter indicates the load operation for the current pending
4035   *                                 operation instances for the given criteria. 1. No load operation;
4036   *                                  2. Part of the given criteria requires load;
4037   *                                  3. All operation instances for the given criteria require load.
4038   *  @param x_load_prim_quantity   -If LPN/Item or move order line is passed, sum of the primary quantity from
4039   *                                 the document table for those document records whose pending operation is
4040   *                                 'Load'.
4041   *  @param x_drop_prim_Quantity   -If LPN/Item or move order line is passed, sum of the primary quantity from
4042   *                                 the document table for those document records whose pending operation is
4043   *                                 'Drop'.
4044   *  @param x_inspect_prim_Quantity-If LPN/Item or move order line is passed, sum of the primary quantity from
4045   *                                 the document table for those document records whose pending operation is
4046   *                                 'Inspect'.
4047   *  @param p_source_task_id       -Identifier of the document record.
4048   *  @param p_move_order_line_id   -Move Order line Id that has to be validated.
4049   *  @param p_inventory_item_id    -Inventory Item Id that has to be validated.
4050   *  @param p_LPN_ID               -LPN id of the LPN that has to be validated.
4051   *  @param p_activity_type_id     -Lookup containing the Activity to be performed.
4052   *  @param p_organization_id      -Organization Id
4053     **/
4054 
4055     PROCEDURE validate_operation
4056     (
4057      x_return_status         OUT  NOCOPY  VARCHAR2 ,
4058      x_msg_data              OUT  NOCOPY  FND_NEW_MESSAGES.MESSAGE_TEXT%TYPE ,
4059      x_msg_count             OUT  NOCOPY  NUMBER,
4060      x_error_code            OUT  NOCOPY  NUMBER ,
4061      x_inspection_flag       OUT  NOCOPY  NUMBER ,
4062      x_load_flag             OUT  NOCOPY  NUMBER ,
4063      x_drop_flag             OUT  NOCOPY  NUMBER ,
4064      x_crossdock_flag        OUT  NOCOPY  NUMBER ,
4065      x_load_prim_quantity    OUT  NOCOPY  NUMBER ,
4066      x_drop_prim_quantity    OUT  NOCOPY  NUMBER ,
4067      x_inspect_prim_quantity OUT  NOCOPY  NUMBER ,
4068      x_crossdock_prim_quantity OUT  NOCOPY  NUMBER ,
4069      p_source_task_id        IN           NUMBER ,
4070      p_move_order_line_id    IN           NUMBER ,
4071      p_inventory_item_id     IN           NUMBER ,
4072      p_LPN_ID                IN           NUMBER ,
4073      p_activity_type_id      IN           NUMBER ,
4074      p_organization_id       IN           NUMBER ,
4075      p_lot_number            IN           VARCHAR2 DEFAULT NULL,
4076      p_revision              IN           VARCHAR2 DEFAULT NULL) IS
4077 
4078     TYPE src_taskid_tab_type IS TABLE OF NUMBER
4079       INDEX BY BINARY_INTEGER;
4080 
4081     --3631633
4082     --Variable to hold license plate details
4083     l_subinventory_code WMS_LICENSE_PLATE_NUMBERS.SUBINVENTORY_CODE%TYPE;
4084     l_locator_id WMS_LICENSE_PLATE_NUMBERS.LOCATOR_ID%TYPE;
4085 
4086    CURSOR mol_details IS
4087       SELECT mol.inspection_status,
4088 	SUM(NVL(mmtt.primary_quantity, wms_task_dispatch_gen.get_primary_quantity(mol.inventory_item_id,mol.organization_id,(mol.quantity - Nvl(mol.quantity_delivered,0)),mol.uom_code))) quantity,
4089 	mmtt.operation_plan_id,mmtt.transaction_temp_id,mol.lpn_id,mol.inventory_item_id
4090       FROM mtl_txn_request_lines mol, mtl_material_transactions_temp mmtt
4091       WHERE       mol.line_id = mmtt.move_order_line_id(+)
4092       AND         mol.line_id = p_move_order_line_id
4093       AND mol.organization_id = p_organization_id
4094 	AND mol.organization_id = mmtt.organization_id(+)
4095 	AND mol.line_status <> 5  -- bug 4179991
4096       GROUP BY mol.inspection_status,mmtt.operation_plan_id,mmtt.transaction_temp_id, mol.lpn_id, mol.inventory_item_id;
4097 
4098    CURSOR lpn_details IS
4099       SELECT mol.inspection_status,mmtt.operation_plan_id,mmtt.transaction_temp_id
4100       FROM mtl_txn_request_lines mol, mtl_material_transactions_temp mmtt
4101       WHERE       mol.line_id = mmtt.move_order_line_id(+)
4102       AND          mol.lpn_id = p_lpn_id
4103 	AND mol.organization_id = p_organization_id
4104 	AND mol.line_status <> 5  -- bug 4179991
4105       AND mol.organization_id = mmtt.organization_id(+);
4106 
4107    CURSOR lpnitem_details IS
4108       SELECT mol.inspection_status,
4109 	SUM(NVL(mmtt.primary_quantity,
4110 		wms_task_dispatch_gen.get_primary_quantity(mol.inventory_item_id,mol.organization_id,(mol.quantity - Nvl(mol.quantity_delivered, 0)),mol.uom_code))) quantity,
4111 	mol.lpn_id,mmtt.operation_plan_id,mmtt.transaction_temp_id,mol.lot_number,mol.revision
4112       FROM mtl_txn_request_lines mol, mtl_material_transactions_temp mmtt
4113       WHERE          mol.lpn_id = p_lpn_id
4114       AND mol.inventory_item_id = p_inventory_item_id
4115       AND           mol.line_id = mmtt.move_order_line_id(+)
4116       AND   mol.organization_id = p_organization_id
4117 	AND   mol.organization_id = mmtt.organization_id(+)
4118 	AND mol.line_status <> 5  -- bug 4179991
4119       GROUP BY mol.lpn_id,mol.inspection_status,mmtt.operation_plan_id,mmtt.transaction_temp_id,mol.lot_number,mol.revision;
4120 
4121    CURSOR c_operation_instance(v_source_task_id NUMBER) IS
4122       SELECT wopi.operation_type_id,
4123 	wopd.operation_type wopd_op_type_id
4124 	FROM wms_op_operation_instances wopi,
4125 	wms_op_plan_details wopd
4126 	WHERE  wopi.source_task_id = v_source_task_id
4127 	AND wopi.operation_status  IN  (G_OP_INS_STAT_PENDING,G_OP_INS_STAT_ACTIVE)
4128 	AND wopi.operation_plan_detail_id = wopd.operation_plan_detail_id
4129 	ORDER BY wopi.operation_sequence DESC;
4130 
4131    CURSOR c_doc_detail IS
4132       SELECT mol.inspection_status,mmtt.operation_plan_id,mmtt.transaction_source_type_id,
4133 	NVL(mmtt.primary_quantity, wms_task_dispatch_gen.get_primary_quantity(mol.inventory_item_id,mol.organization_id,(mol.quantity - Nvl(mol.quantity_delivered,0)),mol.uom_code)) quantity
4134       FROM  mtl_material_transactions_temp mmtt , mtl_txn_request_lines mol
4135 	WHERE mmtt.transaction_temp_id = p_source_task_id
4136 	AND mol.line_status <> 5  -- bug 4179991
4137       AND   mmtt.move_order_line_id = mol.line_id;
4138 
4139    CURSOR c_quantity_details IS
4140     SELECT SUM(primary_transaction_quantity) quantity,lpn_id,inventory_item_id,lot_number,revision
4141     FROM mtl_onhand_quantities_detail
4142     WHERE lpn_id = p_lpn_id
4143     --3631633
4144     --Force to use the N5 index
4145     AND subinventory_code = l_subinventory_code
4146     AND locator_id = l_locator_id
4147     AND inventory_item_id =nvl(p_inventory_item_id,inventory_item_id)
4148     AND organization_id = p_organization_id
4149     GROUP BY lpn_id,inventory_item_id,lot_number,revision;
4150 
4151 
4152       l_debug              NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
4153       l_module_name        VARCHAR2(30) := 'Validate_Operation';
4154       l_progress           NUMBER       ;
4155 
4156       l_inspection_status  NUMBER:=-1;
4157       l_quantity           NUMBER:=0;
4158 	 /* l_issued_qty         NUMBER :=0;
4159       l_required_qty       NUMBER :=0;
4160       l_remaining_qty      NUMBER :=0;*/
4161       l_lpn_id             NUMBER;
4162       l_operation_plan_id  NUMBER;
4163       l_source_task_id     NUMBER;
4164       l_operation_type_id  NUMBER;
4165       l_wopd_op_type_id    NUMBER;
4166       l_operation_seq      NUMBER;
4167       l_tab_index          PLS_INTEGER;
4168 
4169       l_src_taskid_tab     src_taskid_tab_type;
4170       l_inventory_item_id  NUMBER;
4171 
4172       l_mmtt_pri_qty       NUMBER := 0;
4173 
4174       l_lpn_context        NUMBER;
4175 
4176       l_doc_detail         c_doc_detail%ROWTYPE;
4177 
4178 -- Increased lot size to 80 Char - Mercy Thomas - B4625329
4179       l_lot_number         VARCHAR2(80);
4180       l_revision           VARCHAR2(3);
4181 
4182       --Bug 4713903
4183       l_rev_control_code VARCHAR2(5):='FALSE';
4184       l_lot_control_code VARCHAR2(5):='FALSE';
4185       l_serial_control_code VARCHAR2(5):='FALSE';
4186       l_att NUMBER;
4187       l_qoh NUMBER;
4188       l_lpn_onhand NUMBER;
4189       l_return_msg VARCHAR2(2000);
4190       l_ret_val VARCHAR2(1);
4191 
4192  BEGIN
4193       IF (l_debug = 1) THEN
4194           print_DEBUG(' p_source_task_id     ==> '||p_source_task_id,l_module_name,3);
4195           print_DEBUG(' p_move_order_line_id ==> '||p_move_order_line_id,l_module_name,3);
4196           print_DEBUG(' p_inventory_item_id  ==> '||p_inventory_item_id ,l_module_name,3);
4197           print_DEBUG(' p_LPN_ID             ==> '||p_LPN_ID  ,l_module_name,3);
4198           print_DEBUG(' p_activity_type_id   ==> '||p_activity_type_id ,l_module_name,3);
4199           print_debug(' p_lot_number         ==> '||p_lot_number,l_module_name,3);
4200           print_debug(' p_revision           ==> '||p_revision,l_module_name,3);
4201       END IF;
4202 
4203 
4204       l_progress       :=10;
4205       /*Setting the default status of followinf output parameters*/
4206       x_return_status         := g_ret_sts_success;
4207       x_inspection_flag       := G_NO_INSPECTION;
4208       x_load_flag             := G_NO_LOAD;
4209       x_drop_flag             := G_NO_DROP;
4210       x_crossdock_flag        := g_no_crossdock;
4211       x_inspect_prim_quantity := 0;
4212       x_load_prim_quantity    := 0;
4213       x_drop_prim_quantity    := 0;
4214       x_crossdock_prim_quantity := 0;
4215 
4216       /*Validate inputs*/
4217       IF (p_source_task_id IS NULL) THEN
4218          IF (p_lpn_id IS NULL AND p_move_order_line_id IS NULL) THEN
4219             --x_error_code:=
4220             IF (l_debug=1) THEN
4221                print_debug('Invalid Argument set LPN: '||p_lpn_id||'Item :'||p_inventory_item_id||'MO Line Id'||p_move_order_line_id,l_module_name,1);
4222             END IF;
4223             RAISE_APPLICATION_ERROR(INVALID_INPUT,'Invalid Inputs passed');
4224          END IF;
4225       END IF;
4226 
4227       l_progress:=20;
4228 
4229        IF (p_activity_type_id=G_OP_ACTIVITY_INBOUND) THEN
4230 
4231          l_src_taskid_tab.DELETE;
4232          l_tab_index:=1;
4233 
4234          IF (p_source_task_id IS NULL) THEN
4235 
4236            /*Open the appropriate cursor based on the input*/
4237             IF (p_move_order_line_id IS NOT NULL) THEN
4238 
4239                l_progress:=30;
4240                OPEN mol_details;
4241                print_debug('MOL cursor openened',l_module_name,9);
4242 
4243             ELSIF (p_lpn_id IS NOT NULL AND p_inventory_item_id IS NOT NULL) THEN
4244 
4245                l_progress:=40;
4246                OPEN lpnitem_details;
4247                print_debug('LPN_ITEM cursor openened',l_module_name,9);
4248 
4249             ELSE
4250 
4251                l_progress:=50;
4252                OPEN lpn_details;
4253                print_debug('LPN cursor openened',l_module_name,9);
4254 
4255             END IF;
4256 
4257             LOOP
4258                IF (p_move_order_line_id IS NOT NULL) THEN
4259 
4260                     FETCH mol_details INTO l_inspection_status,l_quantity,l_operation_plan_id,l_source_task_id,l_lpn_id,l_inventory_item_id;
4261                     EXIT WHEN mol_details%NOTFOUND;
4262 
4263                     /*Validating to see if the values fetched tally with inputs*/
4264                     IF p_lpn_id IS NOT NULL AND p_lpn_id <>l_lpn_id THEN
4265 
4266                        IF l_debug=1 THEN
4267                          print_debug('LPN Value fetcched from MOLine: '||l_lpn_id,l_module_name,9);
4268                          print_debug('LPN Value passed as input'||p_lpn_id,l_module_name,9);
4269                        END IF;
4270 
4271                        RAISE_APPLICATION_ERROR(INVALID_INPUT,'Invalid Inputs passed');
4272                     END IF;
4273 
4274                     IF p_inventory_item_id IS NOT NULL AND p_inventory_item_id <>l_inventory_item_id  THEN
4275                        IF l_debug=1 THEN
4276                          print_debug('Item Value fetcched from MOLine: '||l_inventory_item_id,l_module_name,9);
4277                          print_debug('Item Value passed as input'||p_inventory_item_id,l_module_name,9);
4278                        END IF;
4279 
4280                        RAISE_APPLICATION_ERROR(INVALID_INPUT,'Invalid Inputs passed');
4281                     END IF;
4282 
4283                     l_progress:=60;
4284 
4285                ELSIF (p_lpn_id IS NOT NULL AND p_inventory_item_id IS NOT NULL) THEN
4286 
4287                     FETCH lpnitem_details INTO l_inspection_status,l_quantity,l_lpn_id,l_operation_plan_id,l_source_task_id,l_lot_number,l_revision;
4288                     EXIT WHEN lpnitem_details%NOTFOUND;
4289 
4290                     /*Checking if the Lot And REvision Controls passed match the input criteria*/
4291                     IF p_lot_number IS NOT NULL AND p_lot_number <> l_lot_number THEN
4292                        GOTO CONTINUE;
4293                     END IF;
4294 
4295                     IF p_revision IS NOT NULL AND p_revision <> l_revision THEN
4296                        GOTO CONTINUE;
4297                     END IF;
4298                     l_progress:=70;
4299 
4300                ELSE
4301 
4302                     FETCH lpn_details INTO l_inspection_status,l_operation_plan_id,l_source_task_id;
4303                     EXIT WHEN lpn_details%NOTFOUND;
4304                     l_progress:=80;
4305 
4306                END IF;
4307 
4308                IF (l_operation_plan_id IS NULL) THEN /*Case when no MMTTs exist*/
4309                     IF (l_inspection_status=1) THEN  /*Case when inspection required*/
4310 
4311                           IF (x_inspection_flag=G_NO_INSPECTION AND (x_load_flag<>G_NO_LOAD OR x_drop_flag<>G_NO_DROP)) THEN
4312                              x_inspection_flag:=G_PARTIAL_INSPECTION;
4313                           ELSE
4314                              x_inspection_flag:=G_FULL_INSPECTION;
4315                           END IF;
4316                           l_progress:=90;
4317 
4318                           IF (x_load_flag=G_FULL_LOAD) THEN
4319                              x_load_flag:=G_PARTIAL_LOAD;
4320                           END IF;
4321                           l_progress:=100;
4322 
4323                           x_inspect_prim_quantity:=x_inspect_prim_quantity+l_quantity;
4324 
4325                           IF (l_debug=1) THEN
4326                              print_debug('Inspection Flag set so far'||x_inspection_flag,l_module_name,9);
4327                              print_debug('Inspection Qty'||x_inspect_prim_quantity,l_module_name,9);
4328                           END IF;
4329 
4330                     ELSE
4331                           IF (x_load_flag=G_NO_LOAD AND (x_inspection_flag<>G_NO_INSPECTION OR x_drop_flag<>G_NO_DROP)) THEN
4332                              x_load_flag:=G_PARTIAL_LOAD;
4333                           ELSE
4334                              x_load_flag:=G_FULL_LOAD;
4335                           END IF;
4336                           l_progress:=110;
4337 
4338                           IF x_inspection_flag=G_FULL_INSPECTION THEN
4339                              x_inspection_flag:=G_PARTIAL_INSPECTION;
4340                           END IF;
4341 
4342                           x_load_prim_quantity:=x_load_prim_quantity+l_quantity;
4343                           l_progress:=120;
4344 
4345                           IF (l_debug=1) THEN
4346                              print_debug('Load Flag set so far'||x_inspection_flag,l_module_name,9);
4347                              print_debug('Load Qty'||x_inspect_prim_quantity,l_module_name,9);
4348                           END IF;
4349 
4350 
4351                     END IF;
4352                     l_quantity:=0;
4353                ELSE
4354                      l_src_taskid_tab(l_tab_index):= l_source_task_id;
4355 
4356                       l_tab_index:=l_tab_index+1;
4357 
4358                       l_progress:=130;
4359 
4360                       print_debug('Adding txn_temp_id to table'||l_source_task_id||l_tab_index,l_module_name,9);
4361 
4362                END IF;
4363 
4364             <<CONTINUE>>
4365                NULL;
4366 
4367             END LOOP;
4368 
4369             IF (mol_details%ISOPEN) THEN
4370                CLOSE mol_details;
4371             ELSIF (lpnitem_details%ISOPEN) THEN
4372                CLOSE lpnitem_details;
4373             ELSIF (lpn_details%ISOPEN) THEN
4374                CLOSE lpn_details;
4375             END IF;
4376 
4377             print_debug('Closed the open cursors',l_module_name,9);
4378             l_progress:=140;
4379 
4380             /*Incase none of the records are fetched from MTL_txn_request_lines for an inventory LPN
4381               returning with flag as FULL LOAD */
4382 
4383             IF p_lpn_id IS NOT NULL AND l_inspection_status=-1 THEN
4384 
4385                IF l_debug=1 THEN
4386                   print_debug('No move Order line exists for the LPN passed,returning status as Full Load reqd',l_module_name,9);
4387                END IF;
4388 
4389                 /* We would return a full Load required incase a Move Order Line is not obtained.
4390                  * Today we do no validation of a possible data corruption from Inbound:
4391                  * i.e. those cases where the LPN is in Rcv and doesnt have a move order line
4392                  * associated.
4393                  * Also if an LPN has a item which has no Move Order associated with it then
4394                  * we would still return as Full Load required.
4395                  */
4396                    x_inspection_flag := G_NO_INSPECTION;
4397                    x_load_flag       := G_FULL_LOAD;
4398                    x_drop_flag       := G_NO_DROP;
4399 
4400                    /*Since there is no Move Order existing for this LPN we need to query MOQD for
4401                     * on hand quantities.This is done only when both LPN and Item are passed as
4402                     * we never return qty only if LPN is passed.
4403                     */
4404                   IF p_inventory_item_id IS NOT NULL THEN
4405 
4406 		  --3631633
4407 		  --Select license plate number details
4408 		  IF p_lpn_id IS NOT NULL THEN
4409 			select subinventory_code,locator_id into l_subinventory_code,l_locator_id from WMS_LICENSE_PLATE_NUMBERS where lpn_id=p_lpn_id;
4410 		  END IF;
4411 
4412 		    --Bug 4730925 BEGIN
4413 		    --Get Item controls and then call quantity tree
4414 		    SELECT Decode(revision_qty_control_code,1,'FALSE','TRUE'),
4415 		      Decode(lot_control_code,1,'FALSE','TRUE'),
4416 		      Decode(serial_number_control_code,1,'FALSE',6,'FALSE','TRUE')
4417 		      INTO l_rev_control_code,
4418 		      l_lot_control_code,
4419 		      l_serial_control_code
4420 		      FROM mtl_system_items_kfv
4421 		      WHERE inventory_item_id = p_inventory_item_id
4422 		      AND organization_id = p_organization_id;
4423 
4424 		    IF p_lot_number IS NULL OR p_lot_number = '' THEN
4425 			l_lot_control_code := 'FALSE';
4426 		    END IF;
4427 		    --Quantity Tree to get the availability
4428 		    IF l_debug=1 THEN
4429                            print_debug(' p_lpn_id :'|| p_lpn_id ,l_module_name,9);
4430 			   print_debug(' p_organization_id :'|| p_organization_id ,l_module_name,9);
4431 			   print_debug(' p_inventory_item_id :'|| p_inventory_item_id ,l_module_name,9);
4432 			   print_debug(' p_revision :'|| p_revision ,l_module_name,9);
4433 			   print_debug(' l_locator_id :'|| l_locator_id ,l_module_name,9);
4434 			   print_debug(' l_subinventory_code :'|| l_subinventory_code ,l_module_name,9);
4435 			   print_debug(' p_lot_number :'|| p_lot_number ,l_module_name,9);
4436 			   print_debug(' l_rev_control_code :'|| l_rev_control_code ,l_module_name,9);
4437 			   print_debug(' l_serial_control_code :'|| l_serial_control_code ,l_module_name,9);
4438 			   print_debug(' l_lot_control_code :'|| l_lot_control_code ,l_module_name,9);
4439                     END IF;
4440 
4441 		    l_ret_val := inv_txn_validations.get_immediate_lpn_item_qty (
4442 										 p_lpn_id => p_lpn_id,
4443 										 p_organization_id => p_organization_id,
4444 										 p_source_type_id => -9999,
4445 										 p_inventory_item_id => p_inventory_item_id,
4446 										 p_revision => p_revision,
4447 										 p_locator_id => l_locator_id,
4448 										 p_subinventory_code => l_subinventory_code,
4449 										 p_lot_number => p_lot_number,
4450 										 p_is_revision_control => l_rev_control_code,
4451 										 p_is_serial_control => l_serial_control_code,
4452 										 p_is_lot_control => l_lot_control_code,
4453 										 x_transactable_qty => l_att,
4454 										 x_qoh => l_qoh,
4455 										 x_lpn_onhand => l_lpn_onhand,
4456 										 x_return_msg => l_return_msg);
4457 
4458 		    IF (l_ret_val = 'Y') THEN
4459 		       x_load_prim_quantity := x_load_prim_quantity + l_att;
4460 		     ELSE
4461 		       x_load_prim_quantity := 0;
4462 		    END IF;
4463 
4464 		   /*
4465 		   FOR l_quantity_details IN c_quantity_details LOOP
4466 
4467                       IF p_lot_number IS NOT NULL THEN
4468 
4469                          IF p_lot_number<>nvl(l_quantity_details.lot_number,'@#LOT#@') THEN
4470 		            --If lot numbers are not matching skip this record
4471                             IF l_debug=1 THEN
4472                              print_debug('Lot mumber from MOQD'||l_quantity_details.lot_number,l_module_name,9);
4473                             END IF;
4474                             GOTO NEXT_RECORD;
4475                           END IF;
4476                         END IF;
4477 
4478                         --Checking if the revisions match
4479                         IF p_revision IS NOT NULL THEN
4480                           IF p_revision<>nvl(l_quantity_details.revision,'-') THEN
4481                              IF l_debug=1 THEN
4482                               print_debug('Revsions are differet,skipping record',l_module_name,9);
4483                              END IF;
4484 
4485                              GOTO NEXT_RECORD;
4486                            END IF;
4487                         END IF;
4488 
4489                         x_load_prim_quantity := x_load_prim_quantity+l_quantity_details.quantity;
4490 
4491                         IF l_debug=1 THEN
4492                            print_debug('Qty fetched'||l_quantity_details.quantity,l_module_name,9);
4493                            print_debug('Load Qty'||x_load_prim_quantity,l_module_name,9);
4494                         END IF;
4495 
4496                         <<NEXT_RECORD>>
4497                            NULL;
4498                    END LOOP;
4499 		   Bug 4730925 END */
4500                   ELSE
4501                      x_load_prim_quantity :=0;
4502 
4503 
4504                   END IF;
4505 
4506                    IF l_debug=1 THEN
4507                     print_debug('Load Qty'||x_load_prim_quantity,l_module_name,9);
4508                    END IF;
4509 
4510                    x_inspect_prim_quantity :=0;
4511                    x_drop_prim_quantity    :=0;
4512 
4513             END IF;
4514 
4515 
4516          ELSE /*p_source_task_id not null*/
4517 
4518             IF l_debug=1 THEN
4519                      print_debug('Checking for non ATF case',l_module_name,9);
4520             END IF;
4521 
4522            OPEN c_doc_detail;
4523 
4524            FETCH c_doc_detail INTO l_doc_detail;
4525 
4526            IF c_doc_detail%NOTFOUND THEN
4527 
4528              IF l_debug=1 THEN
4529                print_debug('Invalid Input Doc Id passed',l_module_name,9);
4530              END IF;
4531 
4532              CLOSE c_doc_detail;
4533 
4534              RAISE_APPLICATION_ERROR(INVALID_INPUT,'Invalid Inputs passed');
4535 
4536            END IF;
4537 
4538            CLOSE c_doc_detail;
4539 
4540            IF l_doc_detail.operation_plan_id IS NULL THEN
4541 
4542              IF l_debug = 1 THEN
4543                print_debug('Operation Plan Id :Non-ATF Case',l_module_name,9);
4544                print_debug('Hence chekcing Move Order Line',l_module_name,9);
4545              END IF;
4546 
4547              IF (l_inspection_status=1) THEN  /*Case when inspection required*/
4548 
4549                IF (x_inspection_flag=G_NO_INSPECTION AND (x_load_flag<>G_NO_LOAD OR x_drop_flag<>G_NO_DROP)) THEN
4550 
4551                   x_inspection_flag:=G_PARTIAL_INSPECTION;
4552 
4553                ELSE
4554 
4555                   x_inspection_flag:=G_FULL_INSPECTION;
4556 
4557                END IF;
4558 
4559                l_progress:=201;
4560 
4561                IF (x_load_flag=G_FULL_LOAD) THEN
4562                   x_load_flag:=G_PARTIAL_LOAD;
4563                END IF;
4564 
4565                l_progress:=203;
4566 
4567                x_inspect_prim_quantity:=x_inspect_prim_quantity+l_doc_detail.quantity;
4568 
4569                IF (l_debug=1) THEN
4570                    print_debug('Inspection Flag set so far'||x_inspection_flag,l_module_name,9);
4571                    print_debug('Inspection Qty'||x_inspect_prim_quantity,l_module_name,9);
4572                END IF;
4573 
4574             ELSE
4575                IF (x_load_flag=G_NO_LOAD AND (x_inspection_flag<>G_NO_INSPECTION OR x_drop_flag<>G_NO_DROP)) THEN
4576                  x_load_flag:=G_PARTIAL_LOAD;
4577 
4578                ELSE
4579 
4580                  x_load_flag:=G_FULL_LOAD;
4581 
4582                END IF;
4583                l_progress:=205;
4584 
4585                IF x_inspection_flag=G_FULL_INSPECTION THEN
4586                   x_inspection_flag:=G_PARTIAL_INSPECTION;
4587 
4588                END IF;
4589 
4590                x_load_prim_quantity:=x_load_prim_quantity+l_doc_detail.quantity;
4591                l_progress:=120;
4592 
4593                IF (l_debug=1) THEN
4594                   print_debug('Load Flag set so far'||x_inspection_flag,l_module_name,9);
4595                   print_debug('Load Qty'||x_inspect_prim_quantity,l_module_name,9);
4596                END IF;
4597 
4598 
4599            END IF;
4600 
4601 
4602 		   SELECT primary_quantity
4603 		      INTO l_mmtt_pri_qty
4604 		      FROM mtl_material_transactions_temp
4605 		      WHERE transaction_temp_id = p_source_task_id;
4606 
4607 		    --code for the bug 11879482
4608 
4609 			--Crossdock flag is always getting set to Partial incase where Operation Plan exists.
4610 			--hence doing the same for the case where no Operation plan exists.
4611 
4612           /*  BEGIN
4613 
4614 
4615 
4616           SELECT quantity_issued,required_quantity  INTO l_issued_qty,l_required_qty FROM wip_requirement_operations
4617           WHERE wip_entity_id  = (SELECT demand_source_header_id FROM  mtl_material_transactions_temp WHERE transaction_temp_id = p_source_task_id ) ;
4618 
4619          EXCEPTION
4620 
4621          WHEN no_data_found THEN
4622 
4623          IF (l_debug=1) THEN
4624 		     print_debug('Demand source header id  does not exist for this MMTT.',l_module_name,4);
4625 	       END IF;
4626 	       RAISE FND_API.G_EXC_ERROR;
4627 
4628          WHEN OTHERS THEN
4629 	       IF (l_debug = 1) THEN
4630 		     print_debug('Error retrievine info from wip_requirement_operations',l_module_name,4);
4631 	       END IF;
4632 	       RAISE FND_API.G_EXC_ERROR;
4633 
4634 
4635           END;
4636 
4637 
4638           l_remaining_qty :=   l_required_qty -  l_issued_qty;
4639 
4640           print_debug('Log for mmt_qty'||l_mmtt_pri_qty,l_module_name,9);
4641           print_debug('Log for Issued qty'|| l_issued_qty,l_module_name,9);
4642           print_debug('Log for required qty'|| l_required_qty,l_module_name,9);
4643           print_debug('Log for remaining qty'|| l_remaining_qty,l_module_name,9);*/
4644 
4645           IF (l_doc_detail.transaction_source_type_id = 5) THEN
4646 
4647 
4648          -- IF ( l_mmtt_pri_qty <  l_remaining_qty)  THEN
4649 
4650            x_crossdock_flag := g_partial_crossdock;
4651 
4652         /*  ELSE
4653 
4654          x_crossdock_flag := g_full_crossdock;
4655 
4656          END IF;*/
4657 
4658          x_crossdock_prim_quantity := x_crossdock_prim_quantity + l_mmtt_pri_qty;
4659 
4660          print_debug('Log for crossdock set'|| x_crossdock_flag,l_module_name,9);
4661 
4662 
4663 
4664         END IF;
4665 
4666 
4667 
4668            ELSE
4669 
4670                l_src_taskid_tab(l_tab_index):=p_source_task_id;
4671                l_progress:=150;
4672                 print_debug('Adding p_source_task_id to pl/sql table'||l_src_taskid_tab(l_tab_index),l_module_name,9);
4673 
4674            END IF;
4675          END IF;
4676 
4677          IF (l_src_taskid_tab.COUNT>0) THEN
4678 
4679 
4680               FOR j IN l_src_taskid_tab.FIRST..l_src_taskid_tab.LAST LOOP
4681 
4682                  OPEN c_operation_instance(l_src_taskid_tab(j));
4683 
4684                  FETCH c_operation_instance INTO l_operation_type_id, l_wopd_op_type_id;
4685 
4686                  IF (c_operation_instance%NOTFOUND) THEN
4687 
4688                     IF (l_debug=1) THEN
4689                        print_debug('No operation instance exitsting'||l_progress,l_module_name,9);
4690                        RAISE_APPLICATION_ERROR(OPERATION_INSTANCE_NOT_EXISTS,'Operation Instance does not exist for source_task id'||l_src_taskid_tab(j));
4691                     END IF;
4692 
4693                  END IF;
4694 
4695                  CLOSE c_operation_instance;
4696 
4697 		 l_progress := 146;
4698 
4699 		 IF (p_activity_type_id=G_OP_ACTIVITY_INBOUND) THEN
4700 
4701 		    SELECT primary_quantity
4702 		      INTO l_mmtt_pri_qty
4703 		      FROM mtl_material_transactions_temp
4704 		      WHERE transaction_temp_id = l_src_taskid_tab(j);
4705 
4706 		 END IF;
4707 
4708        l_progress:=150;
4709 		 IF (l_debug=1) THEN
4710 		    print_debug('Operation Type Id in WOOI: '||l_operation_type_id,l_module_name,9);
4711 		    print_debug('Operation Type Id in WOPD: '||l_wopd_op_type_id,l_module_name,9);
4712 		    print_debug('l_mmtt_pri_qty = '||l_mmtt_pri_qty,l_module_name,9);
4713 		 END IF;
4714 
4715                  IF (l_operation_type_id=G_OP_TYPE_INSPECT) THEN
4716 
4717 		              x_inspect_prim_quantity := x_inspect_prim_quantity + l_mmtt_pri_qty;
4718 
4719                     IF (x_inspection_flag=G_NO_INSPECTION AND (x_load_flag<>G_NO_LOAD OR x_drop_flag<>G_NO_DROP)) THEN
4720 
4721                         x_inspection_flag:=G_PARTIAL_INSPECTION;
4722                     ELSE
4723                         x_inspection_flag:=G_FULL_INSPECTION;
4724 
4725                     END IF;
4726                     l_progress:=160;
4727 
4728 
4729                     IF (x_load_flag=G_FULL_LOAD) THEN
4730                        x_load_flag:=G_PARTIAL_LOAD;
4731                     END IF;
4732 
4733 
4734                     IF (x_drop_flag=G_FULL_DROP) THEN
4735                        x_drop_flag:=G_PARTIAL_DROP;
4736                     END IF;
4737                     l_progress:=170;
4738 
4739                     IF (l_debug=1) THEN
4740                         print_debug('Inspection Flag set so far'||x_inspection_flag,l_module_name,9);
4741                         print_debug('Load Flag set so far'||x_load_flag,l_module_name,9);
4742                         print_debug('Drop Flag set so far'||x_drop_flag,l_module_name,9);
4743                     END IF;
4744 
4745 
4746 		  ELSIF (l_operation_type_id=G_OP_TYPE_LOAD) THEN
4747 
4748 		    IF (l_wopd_op_type_id=g_op_type_crossdock) THEN
4749 		       IF (x_crossdock_flag=g_partial_crossdock) OR (x_crossdock_flag = g_no_crossdock AND
4750 			   (x_crossdock_prim_quantity < x_load_prim_quantity OR   -- there was some Non-crossdock load
4751 			    x_crossdock_prim_quantity < x_drop_prim_quantity))THEN  -- there was some Non-crossdock drop
4752 			  x_crossdock_flag := g_partial_crossdock;
4753 			ELSE
4754 			  x_crossdock_flag := g_full_crossdock;
4755 		       END IF;  -- IF (x_crossdock_flag = g_no_crossdock
4756 
4757 		       x_crossdock_prim_quantity := x_crossdock_prim_quantity + l_mmtt_pri_qty;
4758 
4759 		     ELSE  --IF (l_wopd_op_type_id=g_op_type_crossdock)
4760 
4761 		       IF (x_crossdock_flag = g_full_crossdock) THEN
4762 			  x_crossdock_flag := g_partial_crossdock;
4763 		       END IF; -- IF (x_crossdock_flag = g_full_crossdock)
4764 
4765 		    END IF;  -- IF (l_wopd_op_type_id=g_op_type_crossdock)
4766 
4767                     x_load_prim_quantity := x_load_prim_quantity + l_mmtt_pri_qty;
4768 
4769                     IF (x_load_flag=G_NO_LOAD AND (x_inspection_flag<>G_NO_INSPECTION OR x_drop_flag<>G_NO_DROP)) THEN
4770 
4771                         x_load_flag:=G_PARTIAL_LOAD;
4772                     ELSE
4773                         x_load_flag:=G_FULL_LOAD;
4774                     END IF;
4775 
4776                     IF (x_inspection_flag=G_FULL_INSPECTION) THEN
4777 
4778                        x_inspection_flag:=G_PARTIAL_INSPECTION;
4779                     END IF;
4780 
4781                     IF (x_drop_flag=G_FULL_DROP) THEN
4782 
4783                        x_drop_flag:=G_PARTIAL_DROP;
4784                     END IF;
4785                     l_progress:=180;
4786 
4787                     IF (l_debug=1) THEN
4788                         print_debug('Inspection Flag set so far'||x_inspection_flag,l_module_name,9);
4789                         print_debug('Load Flag set so far'||x_load_flag,l_module_name,9);
4790                         print_debug('Drop Flag set so far'||x_drop_flag,l_module_name,9);
4791                     END IF;
4792 
4793 
4794 		  ELSE
4795 
4796 		    IF (l_wopd_op_type_id=g_op_type_crossdock) THEN
4797 		       IF (x_crossdock_flag=g_partial_crossdock) OR(x_crossdock_flag = g_no_crossdock AND
4798 			   (x_crossdock_prim_quantity < x_load_prim_quantity OR   -- there was some Non-crossdock load
4799 			    x_crossdock_prim_quantity < x_drop_prim_quantity))THEN  -- there was some Non-crossdock drop
4800 			  x_crossdock_flag := g_partial_crossdock;
4801 			ELSE
4802 			  x_crossdock_flag := g_full_crossdock;
4803 		       END IF;  -- IF (x_crossdock_flag = g_no_crossdock
4804 
4805 		       x_crossdock_prim_quantity := x_crossdock_prim_quantity + l_mmtt_pri_qty;
4806 
4807 		     ELSE  --IF (l_wopd_op_type_id=g_op_type_crossdock)
4808 
4809 		       IF (x_crossdock_flag = g_full_crossdock) THEN
4810 			  x_crossdock_flag := g_partial_crossdock;
4811 		       END IF; -- IF (x_crossdock_flag = g_full_crossdock)
4812 
4813 		    END IF;  -- IF (l_wopd_op_type_id=g_op_type_crossdock)
4814 
4815 
4816 		    x_drop_prim_quantity := x_drop_prim_quantity + l_mmtt_pri_qty;
4817 
4818                     IF (x_drop_flag=G_NO_DROP AND (x_inspection_flag<>G_NO_LOAD OR x_drop_flag<>G_NO_INSPECTION)) THEN
4819 
4820                          x_drop_flag:=G_PARTIAL_DROP;
4821                     ELSE
4822                          x_drop_flag:=G_FULL_DROP;
4823                     END IF;
4824 
4825                     IF (x_inspection_flag=G_FULL_INSPECTION) THEN
4826 
4827                         x_inspection_flag:=G_PARTIAL_INSPECTION;
4828                     END IF;
4829 
4830                     IF (x_load_flag=G_FULL_LOAD) THEN
4831 
4832                         x_load_flag:=G_PARTIAL_LOAD;
4833                     END IF;
4834                     l_progress:=190;
4835 
4836                     IF (l_debug=1) THEN
4837                         print_debug('Inspection Flag set so far'||x_inspection_flag,l_module_name,9);
4838                         print_debug('Load Flag set so far'||x_load_flag,l_module_name,9);
4839                         print_debug('Drop Flag set so far'||x_drop_flag,l_module_name,9);
4840                     END IF;
4841 
4842                  END IF;
4843 
4844 
4845               END LOOP;
4846 
4847               print_debug('Inspection Flag'||x_inspection_flag,l_module_name,9);
4848               print_debug('Load Flag'||x_load_flag,l_module_name,9);
4849               print_debug('Drop Flag'||x_drop_flag,l_module_name,9);
4850               print_debug('Crossdock Flag '||x_crossdock_flag,l_module_name,9);
4851               print_debug('Crossdock Primary Qty '||x_crossdock_prim_quantity,l_module_name,9);
4852 
4853 
4854          END IF;/*l_srctaskid_tab.COUNT>0*/
4855 
4856 	 IF p_source_task_id IS NULL
4857 	   AND p_move_order_line_id IS NULL
4858 	     AND p_inventory_item_id IS NULL THEN
4859 
4860 	    x_load_prim_quantity := 0;
4861 	    x_drop_prim_quantity := 0;
4862 	    x_inspect_prim_quantity := 0;
4863 
4864 	 END IF;
4865 
4866 
4867          print_debug('Inspection Flag '||x_inspection_flag,l_module_name,9);
4868          print_debug('Load Flag '||x_load_flag,l_module_name,9);
4869          print_debug('Drop Flag '||x_drop_flag,l_module_name,9);
4870          print_debug('Load primary qty '||x_load_prim_quantity,l_module_name,9);
4871          print_debug('Drop primary qty '||x_drop_prim_quantity,l_module_name,9);
4872          print_debug('Inspect print_debug qty '||x_inspect_prim_quantity,l_module_name,9);
4873          print_debug('Crossdock Flag '||x_crossdock_flag,l_module_name,9);
4874          print_debug('Crossdock Primary Qty '||x_crossdock_prim_quantity,l_module_name,9);
4875 
4876        END IF;/*Activity Type =Inbound*/
4877 
4878  EXCEPTION
4879       WHEN FND_API.G_EXC_ERROR THEN
4880          x_return_status:=g_ret_sts_error;
4881          IF mol_details%ISOPEN THEN
4882                CLOSE mol_details;
4883          END IF;
4884          IF lpnitem_details%ISOPEN THEN
4885                CLOSE lpnitem_details;
4886          END IF;
4887          IF lpn_details%ISOPEN THEN
4888                CLOSE lpn_details;
4889          END IF;
4890          IF c_operation_instance%ISOPEN THEN
4891                 CLOSE c_operation_instance;
4892          END IF;
4893 
4894          IF c_doc_detail%ISOPEN THEN
4895              CLOSE c_doc_detail;
4896          END IF;
4897 
4898          IF c_quantity_details%ISOPEN THEN
4899             CLOSE c_quantity_details;
4900          END IF;
4901 
4902     /*Populate error message and error Code*/
4903 
4904       WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4905          x_return_status:=g_ret_sts_unexp_error;
4906 
4907          IF (l_debug=1) THEN
4908           print_debug('Unexpected error'||SQLERRM||l_progress,l_module_name,1);
4909          END IF;
4910 
4911          IF mol_details%ISOPEN THEN
4912                CLOSE mol_details;
4913          END IF;
4914          IF lpnitem_details%ISOPEN THEN
4915                CLOSE lpnitem_details;
4916          END IF;
4917          IF lpn_details%ISOPEN THEN
4918                CLOSE lpn_details;
4919          END IF;
4920 
4921          IF c_doc_detail%ISOPEN THEN
4922              CLOSE c_doc_detail;
4923          END IF;
4924 
4925          IF c_quantity_details%ISOPEN THEN
4926           CLOSE c_quantity_details;
4927          END IF;
4928 
4929       WHEN OTHERS THEN
4930          IF (l_debug=1) THEN
4931            print_debug(SQLERRM||l_progress, l_module_name,1);
4932          END IF;
4933 
4934          IF (SQLCODE<-20000) THEN
4935             IF (l_debug=1) THEN
4936               print_debug('This is a user defined exception',l_module_name,1);
4937             END IF;
4938 
4939             x_error_code:=-(SQLCODE+20000);
4940 
4941             x_return_status:=g_ret_sts_error;
4942 
4943 
4944           ELSE
4945 
4946             x_return_status := g_ret_sts_unexp_error;
4947 
4948           END IF;
4949 
4950 
4951          IF mol_details%ISOPEN THEN
4952                CLOSE mol_details;
4953          END IF;
4954          IF lpnitem_details%ISOPEN THEN
4955                CLOSE lpnitem_details;
4956          END IF;
4957          IF (lpn_details%ISOPEN) THEN
4958                CLOSE lpn_details;
4959          END IF;
4960 
4961          IF c_operation_instance%ISOPEN THEN
4962                CLOSE c_operation_instance;
4963          END IF;
4964 
4965          IF c_doc_detail%ISOPEN THEN
4966              CLOSE c_doc_detail;
4967          END IF;
4968 
4969          IF c_quantity_details%ISOPEN THEN
4970             CLOSE c_quantity_details;
4971          END IF;
4972 
4973 
4974  END Validate_Operation;
4975 
4976 /**
4977   *    Split_Operation_instance
4978   *   <p>This procedure splits the operation plan,parent document
4979   *   record accordingly.</p>
4980   *
4981   *  @param x_return_status     -Return Status
4982   *  @param x_msg_data          -Returns the Error message Data
4983   *  @param x_msg_count         -Returns the message count
4984   *  @param x_error_code        -Returns appropriate error code in case of an error .
4985   *  @param p_source_task_id    -Identifier of the source document record
4986   *  @param p_activity_type_id  - Lookup code of the Activity type Id
4987   *  @param p_new_Task_id_table -PL/SQL table of Numbers of the MMTT ids
4988   **/
4989  PROCEDURE   Split_Operation_Instance(
4990       x_return_status     OUT  NOCOPY  VARCHAR2 ,
4991       x_msg_data          OUT  NOCOPY  FND_NEW_MESSAGES.MESSAGE_TEXT%TYPE ,
4992       x_msg_count         OUT  NOCOPY  NUMBER,
4993       x_error_code        OUT  NOCOPY  NUMBER ,
4994       p_source_task_id    IN           NUMBER ,
4995       p_activity_type_id  IN           NUMBER ,
4996       p_new_task_id_table IN           task_id_table_type) IS
4997 
4998    CURSOR inbound_document_details(v_task_id NUMBER) IS
4999       SELECT operation_plan_id
5000 	, parent_line_id
5001 	, transaction_quantity
5002 	, primary_quantity
5003 	, transaction_temp_id
5004 	, inventory_item_id
5005 	, organization_id
5006 	, move_order_line_id
5007         , secondary_transaction_quantity -- 9037915
5008         , secondary_uom_code             -- 9037915
5009 	FROM mtl_material_transactions_temp
5010 	WHERE transaction_temp_id=v_task_id;
5011 
5012 
5013    CURSOR c_item_details(v_inventory_item_id NUMBER,v_organization_id NUMBER) IS
5014       SELECT nvl(lot_control_code,1)lot_control_code,nvl(serial_number_control_code,1) serial_number_control_code
5015 	FROM mtl_system_items_b
5016 	WHERE inventory_item_id = v_inventory_item_id
5017 	AND organization_id   = v_organization_id;
5018 
5019    CURSOR wopi_cursor(v_source_task_id NUMBER) IS
5020       SELECT *
5021 /*	  OP_PLAN_INSTANCE_ID,
5022           OPERATION_PLAN_ID,
5023           ACTIVITY_TYPE_ID,
5024           PLAN_TYPE_ID,
5025           SOURCE_TASK_ID,
5026           STATUS,
5027           PLAN_EXECUTION_START_DATE,
5028           PLAN_EXECUTION_END_DATE,
5029           ORGANIZATION_ID,
5030           ORIG_SOURCE_SUB_CODE,
5031           ORIG_SOURCE_LOC_ID,
5032           ORIG_DEST_SUB_CODE,
5033           ORIG_DEST_LOC_ID,
5034           LAST_UPDATE_DATE,
5035           LAST_UPDATED_BY,
5036           CREATION_DATE,
5037           CREATED_BY,
5038           LAST_UPDATE_LOGIN,
5039           ATTRIBUTE_CATEGORY,
5040           ATTRIBUTE1,
5041           ATTRIBUTE2,
5042           ATTRIBUTE3,
5043           ATTRIBUTE4,
5044           ATTRIBUTE5,
5045           ATTRIBUTE6,
5046           ATTRIBUTE7,
5047           ATTRIBUTE8,
5048           ATTRIBUTE9,
5049           ATTRIBUTE10,
5050           ATTRIBUTE11,
5051           ATTRIBUTE12,
5052           ATTRIBUTE13,
5053           ATTRIBUTE14,
5054           ATTRIBUTE15 */
5055 	FROM wms_op_plan_instances
5056 	WHERE source_task_id = v_source_task_id;
5057 
5058    CURSOR wooi_cursor(v_op_plan_instance_id NUMBER) IS
5059       SELECT *
5060 	/*
5061 	OPERATION_INSTANCE_ID,
5062 	OPERATION_PLAN_DETAIL_ID,
5063 	OPERATION_SEQUENCE,
5064 	OPERATION_TYPE_ID,
5065 	OP_PLAN_INSTANCE_ID,
5066 	OPERATION_STATUS,
5067 	ACTIVITY_TYPE_ID,
5068 	SOURCE_TASK_ID,
5069 	ACTIVATE_TIME,
5070 	COMPLETE_TIME,
5071 	SUG_TO_SUB_CODE,
5072 	SUG_TO_LOCATOR_ID,
5073 	FROM_SUBINVENTORY_CODE,
5074 	FROM_LOCATOR_ID,
5075 	TO_SUBINVENTORY_CODE,
5076 	TO_LOCATOR_ID,
5077 	IS_IN_INVENTORY,
5078 	ORGANIZATION_ID,
5079 	EMPLOYEE_ID,
5080 	EQUIPMENT_ID,
5081 	CREATED_BY,
5082 	LAST_UPDATE_DATE,
5083 	CREATION_DATE,
5084 	LAST_UPDATED_BY,
5085 	LAST_UPDATE_LOGIN,
5086 	ATTRIBUTE_CATEGORY,
5087 	ATTRIBUTE1,
5088 	ATTRIBUTE2,
5089 	ATTRIBUTE3,
5090 	ATTRIBUTE4,
5091 	ATTRIBUTE5,
5092 	ATTRIBUTE6,
5093 	ATTRIBUTE7,
5094 	ATTRIBUTE8,
5095 	ATTRIBUTE9,
5096 	ATTRIBUTE10,
5097 	ATTRIBUTE11,
5098 	ATTRIBUTE12,
5099 	ATTRIBUTE13,
5100 	ATTRIBUTE14,
5101 	ATTRIBUTE15
5102 	*/
5103 	FROM wms_op_operation_instances
5104 	WHERE op_plan_instance_id = v_op_plan_instance_id
5105        	ORDER BY operation_sequence DESC, operation_type_id DESC;
5106 	--Bug 5749206. Added another order by "operation_type_id"
5107 
5108    CURSOR wdth_cursor(v_op_plan_instance_id NUMBER) IS
5109       SELECT task_id,
5110 	transaction_id
5111 	FROM wms_dispatched_tasks_history
5112        WHERE op_plan_instance_id = v_op_plan_instance_id;
5113 
5114    l_inbound_doc_child_rec           inbound_document_details%ROWTYPE;
5115    l_inbound_doc_parent_rec          inbound_document_details%ROWTYPE;
5116    l_inbound_doc_child_split_rec     inbound_document_details%ROWTYPE;
5117    l_inbound_doc_parent_split_rec    inbound_document_details%ROWTYPE;
5118    l_item_details_rec                c_item_details%ROWTYPE;
5119    l_wopi_orig_rec                   wopi_cursor%ROWTYPE;
5120    l_wopi_split_rec                  wopi_cursor%ROWTYPE;
5121    l_wooi_orig_rec                   wooi_cursor%ROWTYPE;
5122    l_wooi_new_rec                    wooi_cursor%ROWTYPE;
5123    i                                 INTEGER;
5124    l_n_return_status                 NUMBER;
5125    l_lot_trx_id                      NUMBER;
5126    l_proc_msg                        VARCHAR2(500);
5127    l_progress                        NUMBER;
5128    l_debug                           NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
5129    l_module_name                     VARCHAR2(30) := 'Split_operation_instance';
5130 
5131 
5132    /* BEGIN GRACE11_02_2003 */
5133    l_next_task_id                    NUMBER;
5134    l_new_temp_id                     NUMBER;
5135    l_old_temp_id                     NUMBER;
5136    l_task_id                         NUMBER;
5137    l_wooi_index                      NUMBER;
5138    l_is_last_step_drop               BOOLEAN;
5139    l_num_wdts_for_mmtt               NUMBER;
5140    /* END GRACE11_02_2003 */
5141  BEGIN
5142 
5143       IF (l_debug = 1) THEN
5144           print_DEBUG(' p_source_task_id ==> '||p_source_task_id, l_module_name,3);
5145           print_DEBUG(' p_activity_type_id ==> '||p_activity_type_id, l_module_name,3);
5146       END IF;
5147       x_return_status  := g_ret_sts_success;
5148 
5149       l_progress := 10;
5150       SAVEPOINT split_op_instance_sp;
5151 
5152       IF (p_source_task_id IS NULL OR p_activity_type_id IS NULL) THEN
5153          IF (l_debug=1) THEN
5154             print_debug('Source task Id is null',l_module_name,1);
5155          END IF;
5156          /*Raise Invalid Arguement exception*/
5157          raise_application_error(INVALID_INPUT,'Invalid inputs passed'||p_source_task_id||' '||p_activity_type_id);
5158       END IF;
5159 
5160       /*Query document table(s) based on p_activity_id and P_Source_task_ID.*/
5161       IF (p_activity_type_id =G_OP_ACTIVITY_INBOUND) THEN
5162          /*Fetching document Record for Inbound:from MMTT*/
5163          IF (l_debug=1) THEN
5164             print_debug('Fetching document record for Inbound',l_module_name,9);
5165          END IF;
5166 
5167          OPEN inbound_document_details(p_source_task_id);
5168          FETCH inbound_document_details INTO l_inbound_doc_child_rec;
5169 
5170 	 l_progress := 20;
5171 
5172          IF (inbound_document_details%NOTFOUND) THEN
5173             IF (l_debug=1) THEN
5174                print_debug('Invalid document',l_module_name,3);
5175             END IF;
5176             raise_application_error(INVALID_DOC_ID,'Invalid Document');
5177          END IF;
5178 
5179          CLOSE inbound_document_details;
5180       END IF;
5181 
5182       IF (l_inbound_doc_child_rec.operation_plan_id IS NULL OR l_inbound_doc_child_rec.parent_line_id IS NULL) THEN
5183          IF (l_debug=1) THEN
5184             print_debug('OP Plan ID or parent OP Plan ID is null and unforgivable error',l_module_name, 1);
5185          END IF;
5186 
5187 	 --BUG 5075410: When splitting an MMTT, the WDT must also be
5188 	 --split. If not, the split qty will not be loaded after the split
5189 	 BEGIN
5190 	    SELECT COUNT(1) INTO l_num_wdts_for_mmtt
5191 	      FROM wms_dispatched_tasks
5192 	      WHERE transaction_temp_id = l_inbound_doc_child_rec.transaction_temp_id;
5193 	 EXCEPTION
5194 	    WHEN OTHERS THEN
5195 	       print_debug('Num of WDTs for orginal MMTT:'||l_num_wdts_for_mmtt,l_module_name,3);
5196 	 END;
5197 
5198 	 IF (l_debug = 1) THEN
5199 	    print_debug('Num of WDTs for orginal MMTT:'||l_num_wdts_for_mmtt,l_module_name,3);
5200 	 END IF;
5201 
5202 	 IF (l_num_wdts_for_mmtt > 0) THEN
5203 	    FOR i IN 1 ..  p_new_task_id_table.COUNT LOOP
5204 	       OPEN inbound_document_details(p_new_task_id_table(i));
5205 	       FETCH inbound_document_details INTO l_inbound_doc_child_split_rec;
5206 
5207 	       IF (inbound_document_details%NOTFOUND) THEN
5208 		  IF (l_debug=1) THEN
5209 		     print_debug('Invalid document',l_module_name,3);
5210 		  END IF;
5211 		  raise_application_error(INVALID_DOC_ID,'Invalid Document');
5212 	       END IF;
5213 
5214 	       CLOSE inbound_document_details;
5215 
5216 	       SELECT wms_dispatched_tasks_s.NEXTVAL
5217 		 INTO   l_next_task_id
5218 		 FROM   DUAL;
5219 
5220 	       IF (l_debug = 1) THEN
5221 		  print_debug('Inserting Duplicate WDT records using ID:'||l_next_task_id,l_module_name,3);
5222 	       END IF;
5223 
5224 	       INSERT INTO wms_dispatched_tasks
5225 		 (
5226 		  task_id
5227 		  , transaction_temp_id
5228 		  , organization_id
5229 		  , user_task_type
5230 		  , person_id
5231 		  , effective_start_date
5232 		  , effective_end_date
5233 		  , equipment_id
5234 		  , equipment_instance
5235 		  , person_resource_id
5236 		  , machine_resource_id
5237 		  , status
5238 		  , dispatched_time
5239 		  , loaded_time
5240 		  , drop_off_time
5241 		  , last_update_date
5242 		  , last_updated_by
5243 		  , creation_date
5244 		  , created_by
5245 		  , last_update_login
5246 		  , attribute_category
5247 		  , attribute1
5248 		  , attribute2
5249 		  , attribute3
5250 		  , attribute4
5251 		  , attribute5
5252 		  , attribute6
5253 		  , attribute7
5254 		  , attribute8
5255 		  , attribute9
5256 		  , attribute10
5257 		  , attribute11
5258 		  , attribute12
5259 		  , attribute13
5260 		  , attribute14
5261 		  , attribute15
5262 		  , task_type
5263 		  , priority
5264 		  , task_group_id
5265 		  , device_id
5266 		  , device_invoked
5267 		  , device_request_id
5268 		  , suggested_dest_subinventory
5269 		  , suggested_dest_locator_id
5270 		  , operation_plan_id
5271 		  , move_order_line_id
5272 		  , transfer_lpn_id
5273 		  , op_plan_instance_id
5274 		  , task_method
5275 		 )
5276 		 select l_next_task_id
5277 		 , l_inbound_doc_child_split_rec.transaction_temp_id
5278 		 , organization_id
5279 		 , user_task_type
5280 		 , person_id
5281 		 , effective_start_date
5282 		 , effective_end_date
5283 		 , equipment_id
5284 		 , equipment_instance
5285 		 , person_resource_id
5286 		 , machine_resource_id
5287 		 , status
5288 		 , dispatched_time
5289 		 , loaded_time
5290 		 , drop_off_time
5291 		 , SYSDATE
5292 		 , FND_GLOBAL.USER_ID
5293 		 , creation_date
5294 		 , created_by
5295 		 , fnd_global.USER_ID
5296 		 , attribute_category
5297 		 , attribute1
5298 		 , attribute2
5299 		 , attribute3
5300 		 , attribute4
5301 		 , attribute5
5302 		 , attribute6
5303 		 , attribute7
5304 		 , attribute8
5305 		 , attribute9
5306 		 , attribute10
5307 		 , attribute11
5308 		 , attribute12
5309 		 , attribute13
5310 		 , attribute14
5311 		 , attribute15
5312 		 , task_type
5313 		 , priority
5314 		 , task_group_id
5315 		 , device_id
5316 		 , device_invoked
5317 		 , device_request_id
5318 		 , suggested_dest_subinventory
5319 		 , suggested_dest_locator_id
5320 		 , operation_plan_id
5321 		 , l_inbound_doc_child_split_rec.move_order_line_id
5322 		 , transfer_lpn_id
5323 		 , l_wopi_split_rec.op_plan_instance_id
5324 		 , task_method
5325 		 FROM wms_dispatched_tasks
5326 		 WHERE transaction_temp_id = l_inbound_doc_child_rec.transaction_temp_id;
5327 	    END LOOP;
5328 	 END IF; --IF (l_num_wdts_for_mmtt > 1) THEN
5329 	 --END BUG 5075410: When splitting an MMTT, the WDT must also be
5330 
5331 	 RETURN;
5332          /*raise_application_error(INVALID_PLAN_ID, 'Invalid Plan');*/
5333          /*Raise user defined exception to populated error code*/
5334       END IF;
5335 
5336       /*Query MMTT based on parent_line_id*/
5337       OPEN inbound_document_details(l_inbound_doc_child_rec.parent_line_id);
5338       FETCH inbound_document_details INTO l_inbound_doc_parent_rec;
5339       l_progress := 30;
5340 
5341       IF (inbound_document_details%NOTFOUND) THEN
5342          IF (l_debug=1) THEN
5343             print_debug('Invalid document',l_module_name,3);
5344          END IF;
5345          raise_application_error(INVALID_DOC_ID,'Invalid Document');
5346       END IF;
5347 
5348       CLOSE inbound_document_details;
5349 
5350       /*update parent transaction quantity, PRIMARY_QUANTITY, create MTLT if appropriate*/
5351       l_inbound_doc_parent_rec.transaction_quantity := l_inbound_doc_child_rec.transaction_quantity;
5352       l_inbound_doc_parent_rec.primary_quantity := l_inbound_doc_child_rec.primary_quantity;
5353 	  l_inbound_doc_parent_rec.secondary_transaction_quantity := l_inbound_doc_child_rec.secondary_transaction_quantity; --13431255
5354 
5355       l_progress := 40;
5356       --call MMTT table handler to update records in the table
5357 	  --start of debug messages as part of 13431255
5358 	    IF (l_debug=1) THEN
5359 		print_debug('Transaction quantity: '||l_inbound_doc_child_rec.transaction_quantity,l_module_name,9);
5360         print_debug('Primary quantity: '||l_inbound_doc_child_rec.primary_quantity,l_module_name,9);
5361         print_debug('Secondary transaction quantity: '||l_inbound_doc_child_rec.secondary_transaction_quantity,l_module_name,9);
5362         END IF;
5363 	  --end of debug messages as part of 13431255
5364 
5365       UPDATE mtl_material_transactions_temp
5366 	SET transaction_quantity = l_inbound_doc_parent_rec.transaction_quantity,
5367 	    primary_quantity = l_inbound_doc_child_rec.primary_quantity,
5368 		secondary_transaction_quantity = l_inbound_doc_child_rec.secondary_transaction_quantity, --13431255
5369 	    last_update_date = SYSDATE
5370 	WHERE transaction_temp_id = l_inbound_doc_parent_rec.transaction_temp_id;
5371       l_progress := 50;
5372 
5373 
5374       /*If the item is a Lot Controlled Item, Query MTL_TRANSACTIONS_LOT_TEM (MTLT) based on
5375        P_Source_task_ID and poplate the PL/SQL record variable.*/
5376      /*Checking if the MTLTs exist for a Lot Controlled Item.If there are no records found throw an Invalid
5377       Document Error as the Assumption made is that rules will always create MTLTs for a lot controlled item
5378       --Have to finalise on this and will change if required*/
5379 
5380       OPEN c_item_details(l_inbound_doc_child_rec.INVENTORY_ITEM_ID,l_inbound_doc_child_rec.organization_id);
5381 
5382       FETCH c_item_details INTO l_item_details_rec;
5383 
5384       IF (c_item_details%NOTFOUND) THEN
5385 
5386          IF (l_debug=1) THEN
5387             print_debug('Item -Org combnation not found',l_module_name,1);
5388          END IF;
5389          RAISE FND_API.G_EXC_ERROR;
5390 
5391       END IF;
5392 
5393       CLOSE c_item_details;
5394 
5395       IF (l_item_details_rec.lot_control_code=2) THEN
5396 
5397           IF (l_debug=1) THEN
5398              print_debug('Item is lot Controlled',l_module_name,9);
5399           END IF;
5400 
5401 	  --delete MTLT where transaction_temp_id = l_inbound_doc_parent_rec.transaction_temp_id
5402 	  DELETE FROM mtl_transaction_lots_temp WHERE transaction_temp_id = l_inbound_doc_parent_rec.transaction_temp_id;
5403 
5404 	  --create PL/SQL records for MTLT based on MTLT linked to l_inbound_doc_child_rec.transaction_temp_id
5405 
5406 
5407 
5408 	     l_progress := 70;
5409 
5410 	     INSERT INTO mtl_transaction_lots_temp
5411                 (
5412                  transaction_temp_id
5413                , last_update_date
5414                , last_updated_by
5415                , creation_date
5416                , created_by
5417                , transaction_quantity
5418                , primary_quantity
5419                , secondary_quantity
5420                , secondary_unit_of_measure
5421                , lot_number
5422                , lot_expiration_date
5423                , serial_transaction_temp_id
5424                , description
5425                , vendor_name
5426                , supplier_lot_number
5427                , origination_date
5428                , date_code
5429                , grade_code
5430                , change_date
5431                , maturity_date
5432                , status_id
5433                , retest_date
5434                , age
5435                , item_size
5436                , color
5437                , volume
5438                , volume_uom
5439                , place_of_origin
5440                , best_by_date
5441                , LENGTH
5442                , length_uom
5443                , recycled_content
5444                , thickness
5445                , thickness_uom
5446                , width
5447                , width_uom
5448                , curl_wrinkle_fold
5449                , lot_attribute_category
5450                , c_attribute1
5451                , c_attribute2
5452                , c_attribute3
5453                , c_attribute4
5454                , c_attribute5
5455                , c_attribute6
5456                , c_attribute7
5457                , c_attribute8
5458                , c_attribute9
5459                , c_attribute10
5460                , c_attribute11
5461                , c_attribute12
5462                , c_attribute13
5463                , c_attribute14
5464                , c_attribute15
5465                , c_attribute16
5466                , c_attribute17
5467                , c_attribute18
5468                , c_attribute19
5469                , c_attribute20
5470                , d_attribute1
5471                , d_attribute2
5472                , d_attribute3
5473                , d_attribute4
5474                , d_attribute5
5475                , d_attribute6
5476                , d_attribute7
5477                , d_attribute8
5478                , d_attribute9
5479                , d_attribute10
5480                , n_attribute1
5481                , n_attribute2
5482                , n_attribute3
5483                , n_attribute4
5484                , n_attribute5
5485                , n_attribute6
5486                , n_attribute7
5487                , n_attribute8
5488                , n_attribute9
5489                , n_attribute10
5490                , vendor_id
5491                , territory_code
5492                 )
5493 	     SELECT l_inbound_doc_parent_rec.transaction_temp_id
5494 	       , SYSDATE
5495                , FND_GLOBAL.USER_ID
5496                , SYSDATE
5497                , FND_GLOBAL.USER_ID
5498                , l_inbound_doc_parent_rec.transaction_quantity
5499                , l_inbound_doc_parent_rec.primary_quantity
5500                , secondary_quantity
5501                , secondary_unit_of_measure
5502                , lot_number
5503                , lot_expiration_date
5504                , serial_transaction_temp_id
5505                , description
5506                , vendor_name
5507                , supplier_lot_number
5508                , origination_date
5509                , date_code
5510                , grade_code
5511                , change_date
5512                , maturity_date
5513                , status_id
5514                , retest_date
5515                , age
5516                , item_size
5517                , color
5518                , volume
5519                , volume_uom
5520                , place_of_origin
5521                , best_by_date
5522                , LENGTH
5523                , length_uom
5524                , recycled_content
5525                , thickness
5526                , thickness_uom
5527                , width
5528                , width_uom
5529                , curl_wrinkle_fold
5530                , lot_attribute_category
5531                , c_attribute1
5532                , c_attribute2
5533                , c_attribute3
5534                , c_attribute4
5535                , c_attribute5
5536                , c_attribute6
5537                , c_attribute7
5538                , c_attribute8
5539                , c_attribute9
5540                , c_attribute10
5541                , c_attribute11
5542                , c_attribute12
5543                , c_attribute13
5544                , c_attribute14
5545                , c_attribute15
5546                , c_attribute16
5547                , c_attribute17
5548                , c_attribute18
5549                , c_attribute19
5550                , c_attribute20
5551                , d_attribute1
5552                , d_attribute2
5553                , d_attribute3
5554                , d_attribute4
5555                , d_attribute5
5556                , d_attribute6
5557                , d_attribute7
5558                , d_attribute8
5559                , d_attribute9
5560                , d_attribute10
5561                , n_attribute1
5562                , n_attribute2
5563                , n_attribute3
5564                , n_attribute4
5565                , n_attribute5
5566                , n_attribute6
5567                , n_attribute7
5568                , n_attribute8
5569                , n_attribute9
5570                , n_attribute10
5571                , vendor_id
5572                , territory_code
5573 	       FROM mtl_transaction_lots_temp
5574 	       WHERE transaction_temp_id = l_inbound_doc_child_rec.transaction_temp_id;
5575 
5576       END IF;
5577 
5578       /* BEGIN GRACE11-02-2003
5579        * If there is a WDT record for the original child MMTT, we need to
5580        * duplicate WDT for each new child MMTT records.
5581        */
5582       SELECT COUNT(1) INTO l_num_wdts_for_mmtt
5583 	 FROM wms_dispatched_tasks
5584 	 WHERE transaction_temp_id = l_inbound_doc_child_rec.transaction_temp_id;
5585       /* END: GRACE11-02-2003 */
5586 
5587       /* split parent MMTT, OP_Plan_Instance, OP_Operation_Instance.
5588        * Update parent_line_id for new child task records
5589        */
5590       FOR i IN 1 ..  p_new_task_id_table.COUNT LOOP
5591 	 /*query document table based on new_p_task_id_table(i)*/
5592 	 OPEN inbound_document_details(p_new_task_id_table(i));
5593 	 FETCH inbound_document_details INTO l_inbound_doc_child_split_rec;
5594 	 l_progress := 100;
5595 	 IF (inbound_document_details%NOTFOUND) THEN
5596 	    IF (l_debug=1) THEN
5597 	       print_debug('Invalid document',l_module_name,3);
5598 	    END IF;
5599 	    raise_application_error(INVALID_DOC_ID,'Invalid Document');
5600 	 END IF;
5601 
5602 	 CLOSE inbound_document_details;
5603 
5604 	 /*create parent document record for new task record*/
5605 
5606 	 --copy l_inbound_doc_parent_rec into l_inbound_doc_parent_split_rec as new parent RECORD
5607 	 l_inbound_doc_parent_split_rec := l_inbound_doc_parent_rec;
5608 	 l_inbound_doc_parent_split_rec.transaction_quantity := l_inbound_doc_child_split_rec.transaction_quantity;
5609 	 l_inbound_doc_parent_split_rec.primary_quantity := l_inbound_doc_child_split_rec.primary_quantity;
5610 	 l_inbound_doc_parent_split_rec.secondary_transaction_quantity  := l_inbound_doc_child_split_rec.secondary_transaction_quantity ; -- 9037915
5611          l_inbound_doc_parent_split_rec.secondary_uom_code  := l_inbound_doc_child_split_rec.secondary_uom_code ;                         -- 9037915
5612 
5613 	 l_progress := 110;
5614 	 --call MMTT(MTLT) table handlers to insert the new parent RECORD l_inbound_doc_parent_split_rec
5615 	 inv_trx_util_pub.copy_insert_line_trx(
5616 					       x_return_status            => x_return_status
5617 					       , x_msg_data               => x_msg_data
5618 					       , x_msg_count              => x_msg_count
5619 					       , x_new_txn_temp_id        => l_inbound_doc_parent_split_rec.transaction_temp_id
5620 					       , p_transaction_temp_id    => l_inbound_doc_parent_rec.transaction_temp_id
5621 					       , p_txn_qty                => l_inbound_doc_parent_split_rec.transaction_quantity
5622                                                , p_primary_qty            => l_inbound_doc_parent_split_rec.primary_quantity
5623 					       , p_sec_txn_qty            => l_inbound_doc_parent_split_rec.secondary_transaction_quantity -- 9037915
5624 					       );
5625 
5626 	 IF (x_return_status=g_ret_sts_error) THEN
5627 	    RAISE FND_API.G_EXC_ERROR;
5628 	  ELSIF (x_return_status<>g_ret_sts_success) THEN
5629 	    RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5630 	 END IF;
5631 
5632 
5633 	 l_progress := 120;
5634 
5635 	 l_inbound_doc_child_split_rec.parent_line_id := l_inbound_doc_parent_split_rec.transaction_temp_id;
5636 	 --call MMTT table handlers to update the new child task record
5637 	 UPDATE mtl_material_transactions_temp
5638 	   SET parent_line_id = l_inbound_doc_parent_split_rec.transaction_temp_id
5639 	   WHERE transaction_temp_id = l_inbound_doc_child_split_rec.transaction_temp_id;
5640 	 l_progress := 140;
5641 
5642 	 --TODO: create records for MTLT based on MTLT records linked to l_inbound_doc_child_split_rec.transaction_temp_id
5643 	 --replace the transaction_temp_id with l_inbound_doc_parent_split_rec.transaction_temp_id, and insert into mtlt table
5644 
5645 
5646 	 OPEN c_item_details(l_inbound_doc_child_split_rec.INVENTORY_ITEM_ID,l_inbound_doc_child_split_rec.organization_id);
5647 
5648 	 FETCH c_item_details INTO l_item_details_rec;
5649 
5650 	 IF (c_item_details%NOTFOUND) THEN
5651 
5652 	    IF (l_debug=1) THEN
5653 	       print_debug('Item -Org combnation not found',l_module_name,1);
5654 	    END IF;
5655 	    RAISE FND_API.G_EXC_ERROR;
5656 
5657 	 END IF;
5658 
5659 	 CLOSE c_item_details;
5660 
5661 	 IF (l_item_details_rec.lot_control_code=2) THEN
5662 
5663 	    IF (l_debug=1) THEN
5664 	       print_debug('Item is lot Controlled',l_module_name,9);
5665 	    END IF;
5666 
5667 	    --create PL/SQL records for MTLT based on MTLT linked to l_inbound_doc_child_split_rec.transaction_temp_id
5668 
5669 
5670 	    l_progress := 70;
5671 
5672 	    INSERT INTO mtl_transaction_lots_temp
5673 	      (
5674 	       transaction_temp_id
5675                , last_update_date
5676                , last_updated_by
5677                , creation_date
5678                , created_by
5679                , transaction_quantity
5680                , primary_quantity
5681                , secondary_quantity
5682                , secondary_unit_of_measure
5683                , lot_number
5684                , lot_expiration_date
5685                , serial_transaction_temp_id
5686                , description
5687                , vendor_name
5688                , supplier_lot_number
5689                , origination_date
5690                , date_code
5691                , grade_code
5692                , change_date
5693                , maturity_date
5694                , status_id
5695                , retest_date
5696                , age
5697                , item_size
5698                , color
5699                , volume
5700                , volume_uom
5701                , place_of_origin
5702                , best_by_date
5703                , LENGTH
5704                , length_uom
5705                , recycled_content
5706                , thickness
5707                , thickness_uom
5708                , width
5709                , width_uom
5710                , curl_wrinkle_fold
5711                , lot_attribute_category
5712                , c_attribute1
5713                , c_attribute2
5714                , c_attribute3
5715                , c_attribute4
5716                , c_attribute5
5717                , c_attribute6
5718                , c_attribute7
5719                , c_attribute8
5720                , c_attribute9
5721                , c_attribute10
5722                , c_attribute11
5723                , c_attribute12
5724                , c_attribute13
5725                , c_attribute14
5726                , c_attribute15
5727                , c_attribute16
5728                , c_attribute17
5729                , c_attribute18
5730                , c_attribute19
5731                , c_attribute20
5732                , d_attribute1
5733                , d_attribute2
5734                , d_attribute3
5735                , d_attribute4
5736                , d_attribute5
5737                , d_attribute6
5738                , d_attribute7
5739                , d_attribute8
5740                , d_attribute9
5741                , d_attribute10
5742                , n_attribute1
5743                , n_attribute2
5744                , n_attribute3
5745                , n_attribute4
5746                , n_attribute5
5747                , n_attribute6
5748                , n_attribute7
5749                , n_attribute8
5750                , n_attribute9
5751                , n_attribute10
5752                , vendor_id
5753                , territory_code
5754                 )
5755 	      SELECT l_inbound_doc_parent_split_rec.transaction_temp_id
5756 	       , SYSDATE
5757 	       , FND_GLOBAL.USER_ID
5758                , SYSDATE
5759                , FND_GLOBAL.USER_ID
5760                , l_inbound_doc_parent_split_rec.transaction_quantity
5761                , l_inbound_doc_parent_split_rec.primary_quantity
5762                , l_inbound_doc_parent_split_rec.secondary_transaction_quantity -- 9037915
5763                , secondary_unit_of_measure
5764                , lot_number
5765                , lot_expiration_date
5766                , serial_transaction_temp_id
5767                , description
5768                , vendor_name
5769                , supplier_lot_number
5770                , origination_date
5771                , date_code
5772                , grade_code
5773                , change_date
5774                , maturity_date
5775                , status_id
5776                , retest_date
5777                , age
5778                , item_size
5779                , color
5780                , volume
5781                , volume_uom
5782                , place_of_origin
5783                , best_by_date
5784                , LENGTH
5785                , length_uom
5786                , recycled_content
5787                , thickness
5788                , thickness_uom
5789                , width
5790                , width_uom
5791                , curl_wrinkle_fold
5792                , lot_attribute_category
5793                , c_attribute1
5794                , c_attribute2
5795                , c_attribute3
5796                , c_attribute4
5797                , c_attribute5
5798                , c_attribute6
5799                , c_attribute7
5800                , c_attribute8
5801                , c_attribute9
5802                , c_attribute10
5803                , c_attribute11
5804                , c_attribute12
5805                , c_attribute13
5806                , c_attribute14
5807                , c_attribute15
5808                , c_attribute16
5809                , c_attribute17
5810                , c_attribute18
5811                , c_attribute19
5812                , c_attribute20
5813                , d_attribute1
5814                , d_attribute2
5815                , d_attribute3
5816                , d_attribute4
5817                , d_attribute5
5818                , d_attribute6
5819                , d_attribute7
5820                , d_attribute8
5821                , d_attribute9
5822                , d_attribute10
5823                , n_attribute1
5824                , n_attribute2
5825                , n_attribute3
5826                , n_attribute4
5827                , n_attribute5
5828                , n_attribute6
5829                , n_attribute7
5830                , n_attribute8
5831                , n_attribute9
5832                , n_attribute10
5833                , vendor_id
5834                , territory_code
5835 	      FROM mtl_transaction_lots_temp
5836 	      WHERE transaction_temp_id = l_inbound_doc_child_split_rec.transaction_temp_id;
5837 
5838 	 END IF;
5839 
5840 
5841 	 /*create op_plan_instances record for new task record*/
5842 	 OPEN wopi_cursor(l_inbound_doc_parent_rec.transaction_temp_id);
5843 	 FETCH wopi_cursor INTO l_wopi_orig_rec;
5844 	 l_progress := 150;
5845 	 IF (wopi_cursor%NOTFOUND) THEN
5846 	    -- NOTE: if no instances exist, the code below shouldn't be executed
5847 	    raise_application_error(PLAN_INSTANCE_NOT_EXITS,'Operation plan instance not exists');
5848 	 END IF;
5849 	 -- TODO: what if multiple instances exist?
5850 	 CLOSE wopi_cursor;
5851 
5852 	 --populate l_wopi_split_rec based on l_wopi_orig_rec
5853 	 l_wopi_split_rec := l_wopi_orig_rec;
5854 
5855 	 --l_wopi_split_rec.operation_plan_id := l_inbound_doc_parent_rec.operation_plan_id;
5856 	 l_wopi_split_rec.source_task_id := l_inbound_doc_parent_split_rec.transaction_temp_id;
5857 	 --l_wopi_source_task_idsplit_rec.status := l_wopi_orig_rec.status;
5858 
5859 	 l_progress := 160;
5860 	 --use sequence to generate op_plan_instance_id
5861 	 SELECT  WMS_OP_INSTANCE_S.NEXTVAL
5862 	   INTO l_wopi_split_rec.op_plan_instance_id
5863 	   FROM dual;
5864 	 l_progress := 170;
5865 	 --call wms_op_plan_instance table handler to insert l_wopi_split_rec INTO the table
5866 
5867 	 l_progress := 180;
5868 	 WMS_OP_RUNTIME_PVT_APIS.INSERT_PLAN_INSTANCE(
5869            x_return_status => x_return_status        ,
5870            x_msg_count     => x_msg_count            ,
5871            x_msg_data      => x_msg_data             ,
5872            p_insert_rec    => l_wopi_split_rec );
5873 
5874 	 IF (x_return_status=g_ret_sts_error) THEN
5875 	    RAISE FND_API.G_EXC_ERROR;
5876 	  ELSIF (x_return_status<>g_ret_sts_success) THEN
5877 	    RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5878 	 END IF;
5879 	 /* BEGIN:  GRACE11-02-2003
5880 	  * For each new WOPI that we are creating, we should copy all WDTH
5881 	  * records from the the original WOPI, and link these new WDTH
5882 	  * records to the new WOPI. The new WDTH will have same
5883 	  * transaction_quantity as l_inbound_doc_parent_split_rec. And we need to
5884 	  * update transaction_quantity of the original WDTHs to be same
5885 	  * as l_inbound_doc_parent_rec.
5886 	  */
5887 	 l_progress := 185;
5888 
5889 	  -- TODO: how is wms_dispatched_tasks_history linked to wms_op_plan_instances
5890 	  UPDATE wms_dispatched_tasks_history
5891 	    SET transaction_quantity = l_inbound_doc_parent_rec.transaction_quantity,
5892 		    secondary_transaction_quantity = l_inbound_doc_parent_rec.secondary_transaction_quantity, --13431255
5893 	        last_update_date = Sysdate    -- bug 3827507
5894 	    WHERE op_plan_instance_id =l_wopi_orig_rec.op_plan_instance_id;
5895 
5896 	  OPEN wdth_cursor(l_wopi_orig_rec.op_plan_instance_id);
5897 
5898 	  -- loop through wdth with the same op_plan_instance_id -lezhang
5899 	  LOOP
5900 	     FETCH wdth_cursor INTO l_task_id, l_old_temp_id;
5901 	     EXIT WHEN wdth_cursor%notfound;
5902 
5903 	     SELECT wms_dispatched_tasks_s.NEXTVAL
5904 	       INTO   l_next_task_id
5905 	       FROM   DUAL;
5906 
5907 	     SELECT mtl_material_transactions_s.NEXTVAL
5908 	       INTO l_new_temp_id
5909 	       FROM dual;
5910 	     -- TODO: how to construct new WDTHs from those linked to orig WOPI
5911 	     --INSERT INTO wms_dispatched_tasks_history ???;
5912 	     INSERT INTO wms_dispatched_tasks_history
5913 	       (
5914 		task_id
5915 		, transaction_id
5916 		, organization_id
5917 		, user_task_type
5918 		, person_id
5919 		, effective_start_date
5920 		, effective_end_date
5921 		, equipment_id
5922 		, equipment_instance
5923 		, person_resource_id
5924 		, machine_resource_id
5925 		, status
5926 		, dispatched_time
5927 		, loaded_time
5928 		, drop_off_time
5929 		, last_update_date
5930 		, last_updated_by
5931 		, creation_date
5932 		, created_by
5933 		, last_update_login
5934 		, attribute_category
5935 		, attribute1
5936 		, attribute2
5937 		, attribute3
5938 		, attribute4
5939 		, attribute5
5940 		, attribute6
5941 		, attribute7
5942 		, attribute8
5943 		, attribute9
5944 		, attribute10
5945 		, attribute11
5946 		, attribute12
5947 		, attribute13
5948 		, attribute14
5949 		, attribute15
5950 		, task_type
5951 		, priority
5952 		, task_group_id
5953 		, suggested_dest_subinventory
5954 		, suggested_dest_locator_id
5955 		, operation_plan_id
5956 		, move_order_line_id
5957 		, transfer_lpn_id
5958 		, transaction_batch_id
5959 		, transaction_batch_seq
5960 		, inventory_item_id
5961 		, revision
5962 		, transaction_quantity
5963 		, transaction_uom_code
5964 		, source_subinventory_code
5965 		, source_locator_id
5966 		, dest_subinventory_code
5967 	       , dest_locator_id
5968 	       , lpn_id
5969 	       , content_lpn_id
5970 	       , is_parent
5971 	       , parent_transaction_id
5972 	       , transfer_organization_id
5973 	       , source_document_id
5974 	       , op_plan_instance_id
5975 	       , task_method
5976 	       , transaction_type_id
5977 	       , transaction_source_type_id
5978 	       , transaction_action_id
5979 	       , transaction_temp_id  -- new link between wdth and we
5980                , secondary_transaction_quantity -- 9037915
5981                , secondary_transaction_uom_code -- 9037915
5982 	       )
5983 	       SELECT l_next_task_id
5984 	       , transaction_id
5985 	       , organization_id
5986 	       , user_task_type
5987 	       , person_id
5988 	       , effective_start_date
5989 	       , effective_end_date
5990 	       , equipment_id
5991 	       , equipment_instance
5992 	       , person_resource_id
5993 	       , machine_resource_id
5994 	       , status
5995 	       , dispatched_time
5996 	       , loaded_time
5997 	       , drop_off_time
5998 	       , SYSDATE
5999 	       , fnd_global.user_id
6000 	       , SYSDATE               -- bug 3827507
6001 	       , fnd_global.user_id    -- bug 3827507
6002 	       , fnd_global.user_id
6003 	       , attribute_category
6004 	       , attribute1
6005 	       , attribute2
6006 	       , attribute3
6007 	       , attribute4
6008 	       , attribute5
6009 	       , attribute6
6010 	       , attribute7
6011 	       , attribute8
6012 	       , attribute9
6013 	       , attribute10
6014 	       , attribute11
6015 	       , attribute12
6016 	       , attribute13
6017 	       , attribute14
6018 	       , attribute15
6019 	       , task_type
6020 	       , priority
6021 	       , task_group_id
6022 	       , suggested_dest_subinventory
6023 	       , suggested_dest_locator_id
6024 	       , operation_plan_id
6025 	       , l_inbound_doc_child_split_rec.move_order_line_id
6026 	       , transfer_lpn_id
6027 	       , transaction_batch_id
6028 	       , transaction_batch_seq
6029 	       , inventory_item_id
6030 	       , revision
6031 	       , l_inbound_doc_parent_split_rec.transaction_quantity
6032 	       , transaction_uom_code
6033 	       , source_subinventory_code
6034 	       , source_locator_id
6035 	       , dest_subinventory_code
6036 	       , dest_locator_id
6037 	       , lpn_id
6038 	       , content_lpn_id
6039 	       , is_parent
6040 	       , l_inbound_doc_parent_split_rec.transaction_temp_id
6041 	       , transfer_organization_id
6042 	       , source_document_id
6043 	       , l_wopi_split_rec.op_plan_instance_id
6044 	       , task_method
6045 	       , transaction_type_id
6046 	       , transaction_source_type_id
6047 	       , transaction_action_id
6048 	       , l_new_temp_id   -- new value to link wdth and we -- lezhang
6049                , l_inbound_doc_parent_split_rec.secondary_transaction_quantity -- 9037915
6050                , l_inbound_doc_parent_split_rec.secondary_uom_code             -- 9037915
6051 	       FROM   wms_dispatched_tasks_history
6052 	       WHERE  task_id = l_task_id;
6053 
6054 
6055 
6056 	     INSERT INTO wms_exceptions
6057 	       (
6058 		task_id
6059 		, sequence_number
6060 		, organization_id
6061 		, inventory_item_id
6062 		, person_id
6063 		, effective_start_date
6064 		, effective_end_date
6065 		, inventory_location_id
6066 		, reason_id
6067 		, discrepancy_type
6068 		, archive_flag
6069 		, subinventory_code
6070 		, lot_number
6071 		, revision
6072 		, last_update_date
6073 		, last_updated_by
6074 		, creation_date
6075 		, created_by
6076 		, last_update_login
6077 		, attribute_category
6078 		, attribute1
6079 		, attribute2
6080 		, attribute3
6081 		, attribute4
6082 		, attribute5
6083 		, attribute6
6084 		, attribute7
6085 		, attribute8
6086 		, attribute9
6087 		, attribute10
6088 		, attribute11
6089 		, attribute12
6090 		, attribute13
6091 		, attribute14
6092 		, attribute15
6093 		, transaction_header_id
6094 		, wms_task_type
6095 		, wf_item_key
6096 		, lpn_id
6097 		)
6098 	       SELECT
6099 	       l_new_temp_id -- new value to link wdth and we -- lezhang
6100 	       , sequence_number
6101 	       , organization_id
6102 	       , inventory_item_id
6103 	       , person_id
6104 	       , effective_start_date
6105 	       , effective_end_date
6106 	       , inventory_location_id
6107 	       , reason_id
6108 	       , discrepancy_type
6109 	       , archive_flag
6110 	       , subinventory_code
6111 	       , lot_number
6112 	       , revision
6113 	       , last_update_date
6114 	       , last_updated_by
6115 	       , creation_date
6116 	       , created_by
6117 	       , last_update_login
6118 	       , attribute_category
6119 	       , attribute1
6120 	       , attribute2
6121 	       , attribute3
6122 	       , attribute4
6123 	       , attribute5
6124 	       , attribute6
6125 	       , attribute7
6126 	       , attribute8
6127 	       , attribute9
6128 	       , attribute10
6129 	       , attribute11
6130 	       , attribute12
6131 	       , attribute13
6132 	       , attribute14
6133 	       , attribute15
6134 	       , transaction_header_id
6135 	       , wms_task_type
6136 	       , wf_item_key
6137 	       , lpn_id
6138 	       FROM wms_exceptions
6139 	       WHERE task_id = l_old_temp_id;
6140 
6141 	  END LOOP;
6142 
6143 	  CLOSE wdth_cursor;
6144 
6145 	  -- END: GRACE11-02-2003
6146 	  l_progress := 190;
6147 
6148 
6149 
6150 	 /*create op_operation_instances record for new task record*/
6151 	 -- NOTE: here assume that it is ok if no operation instances exist
6152 	 OPEN wooi_cursor(l_wopi_orig_rec.op_plan_instance_id);
6153 	 l_wooi_index := 0;
6154 	 l_is_last_step_drop := FALSE;
6155 	 LOOP
6156 	    FETCH wooi_cursor INTO l_wooi_orig_rec;
6157 	    l_progress := 200;
6158 	    EXIT WHEN wooi_cursor%NOTFOUND;
6159 	    --populate l_wooi_new_rec based on l_wooi_orig_rec
6160 	    l_wooi_index := l_wooi_index + 1;
6161 	    l_wooi_new_rec := l_wooi_orig_rec;
6162 
6163 	    /* BEGIN GRACE11_02_2003, note that 'ORDER BY operation_sequence DESC' is added to the cursor definition
6164 	     *  Now we update wooi.source_task_id if wooi.status =
6165 	     *  'Pending'. We need to change this to: update
6166 	     *  wooi.source_task_id for the last wooi, if the last step is
6167 	     *  load; and update wooi.source_task_id for the last TWO wooi
6168 	     *  records, if the last step is drop.
6169 	     * IF (l_wooi_orig_rec.operation_status = G_OP_INS_STAT_PENDING) THEN
6170 	     *   l_wooi_new_rec.source_task_id := p_new_task_id_table(i);
6171 	     * END IF;
6172 	     */
6173 	    IF (l_wooi_index = 1) THEN
6174 	       IF (l_wooi_orig_rec.OPERATION_TYPE_ID = 2) THEN
6175 		  l_is_last_step_drop := TRUE;
6176 	       END IF;
6177 	       IF (l_wooi_orig_rec.OPERATION_TYPE_ID = 1 OR l_wooi_orig_rec.OPERATION_TYPE_ID = 2 OR
6178               l_wooi_orig_rec.operation_type_id =G_OP_TYPE_INSPECT ) THEN
6179 		  l_wooi_new_rec.source_task_id := p_new_task_id_table(i);
6180 	       END IF;
6181 	    END IF;
6182 
6183 	    IF (l_wooi_index = 2) THEN
6184 	       IF (l_is_last_step_drop = TRUE) THEN
6185 		  l_wooi_new_rec.source_task_id := p_new_task_id_table(i);
6186 	       END IF;
6187 	    END IF;
6188 	    /* END GRACE11_02_2003 */
6189 
6190 	    l_wooi_new_rec.op_plan_instance_id := l_wopi_split_rec.op_plan_instance_id;
6191 	    l_progress := 210;
6192 	    --Call wms_op_operation_instances table handler to insert the record
6193 	    SELECT wms_op_instance_s.NEXTVAL
6194 	      INTO l_wooi_new_rec.operation_instance_id
6195 	      FROM dual;
6196 	    l_progress := 220;
6197 
6198 	    l_progress := 230;
6199 	    WMS_OP_RUNTIME_PVT_APIS.insert_operation_instance
6200 	      ( x_return_status => x_return_status,
6201 		x_msg_count     => x_msg_count,
6202 		x_msg_data      => x_msg_data,
6203 		p_insert_rec    => l_wooi_new_rec
6204 		);
6205 	    l_progress := 240;
6206 
6207 	    IF (l_debug=1) THEN
6208 	       print_debug('Return Status from table handler',l_module_name,9);
6209 	    END IF;
6210 
6211 	    IF (x_return_status=g_ret_sts_error) THEN
6212 	       RAISE FND_API.G_EXC_ERROR;
6213 	     ELSIF (x_return_status<>g_ret_sts_success) THEN
6214 	       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6215 	    END IF;
6216 
6217 	 END LOOP;
6218 	 CLOSE wooi_cursor;
6219 
6220 	 /* BEGIN GRACE11-02-2003
6221 	  * If there is a WDT record for the original child MMTT, we need to
6222 	  * duplicate WDT for each new child MMTT records.
6223 	  */
6224 	 IF (l_num_wdts_for_mmtt > 0) THEN
6225 	    -- TODO: how to duplicate WDT of original child MMTT (l_inbound_doc_child_rec)???
6226 	    --INSERT INTO wms_dispatched_tasks ???;
6227 	    SELECT wms_dispatched_tasks_s.NEXTVAL
6228 	      INTO   l_next_task_id
6229 	      FROM   DUAL;
6230 
6231 	    INSERT INTO wms_dispatched_tasks
6232 	      (
6233 	       task_id
6234 	       , transaction_temp_id
6235 	       , organization_id
6236 	       , user_task_type
6237 	       , person_id
6238 	       , effective_start_date
6239 	       , effective_end_date
6240 	       , equipment_id
6241 	       , equipment_instance
6242 	       , person_resource_id
6243 	       , machine_resource_id
6244 	       , status
6245 	       , dispatched_time
6246 	       , loaded_time
6247 	       , drop_off_time
6248 	       , last_update_date
6249 	       , last_updated_by
6250 	       , creation_date
6251 	       , created_by
6252 	       , last_update_login
6253 	       , attribute_category
6254 	       , attribute1
6255 	       , attribute2
6256 	       , attribute3
6257 	       , attribute4
6258 	       , attribute5
6259 	       , attribute6
6260 	       , attribute7
6261 	       , attribute8
6262 	       , attribute9
6263 	       , attribute10
6264 	       , attribute11
6265 	       , attribute12
6266 	       , attribute13
6267 	       , attribute14
6268 	       , attribute15
6269 	       , task_type
6270 	       , priority
6271 	       , task_group_id
6272 	       , device_id
6273 	       , device_invoked
6274 	       , device_request_id
6275 	       , suggested_dest_subinventory
6276 	      , suggested_dest_locator_id
6277 	      , operation_plan_id
6278 	      , move_order_line_id
6279 	      , transfer_lpn_id
6280 	      , op_plan_instance_id
6281 	      , task_method
6282 	      )
6283 	      select l_next_task_id
6284 	      , l_inbound_doc_child_split_rec.transaction_temp_id
6285 	      , organization_id
6286 	      , user_task_type
6287 	      , person_id
6288 	      , effective_start_date
6289 	      , effective_end_date
6290 	      , equipment_id
6291 	      , equipment_instance
6292 	      , person_resource_id
6293 	      , machine_resource_id
6294 	      , status
6295 	      , dispatched_time
6296 	      , loaded_time
6297 	      , drop_off_time
6298 	      , SYSDATE
6299 	      , FND_GLOBAL.USER_ID
6300 	      , creation_date
6301 	      , created_by
6302 	      , fnd_global.USER_ID
6303 	      , attribute_category
6304 	      , attribute1
6305 	      , attribute2
6306 	      , attribute3
6307 	      , attribute4
6308 	      , attribute5
6309 	      , attribute6
6310 	      , attribute7
6311 	      , attribute8
6312 	      , attribute9
6313 	      , attribute10
6314 	      , attribute11
6315 	      , attribute12
6316 	      , attribute13
6317 	      , attribute14
6318 	      , attribute15
6319 	      , task_type
6320 	      , priority
6321 	      , task_group_id
6322 	      , device_id
6323 	      , device_invoked
6324 	      , device_request_id
6325 	      , suggested_dest_subinventory
6326 	      , suggested_dest_locator_id
6327 	      , operation_plan_id
6328 	      , l_inbound_doc_child_split_rec.move_order_line_id
6329 	      , transfer_lpn_id
6330 	      , l_wopi_split_rec.op_plan_instance_id
6331 	      , task_method
6332 	      FROM wms_dispatched_tasks
6333 	      WHERE transaction_temp_id = l_inbound_doc_child_rec.transaction_temp_id;
6334 
6335 
6336 
6337 	 END IF;
6338 	 /* END: GRACE11-02-2003 */
6339 
6340       END LOOP;
6341  EXCEPTION
6342     WHEN FND_API.G_EXC_ERROR THEN
6343        IF (l_debug=1) THEN
6344 	  print_debug('Error (fnd_api.g_exc_error) occured at'||l_progress,l_module_name,1);
6345        END IF;
6346 
6347        IF (inbound_document_details%ISOPEN) THEN
6348 	  CLOSE inbound_document_details;
6349        END IF;
6350 
6351        IF (wopi_cursor%ISOPEN) THEN
6352 	  CLOSE wopi_cursor;
6353        END IF;
6354 
6355        IF (wooi_cursor%ISOPEN) THEN
6356 	  CLOSE wooi_cursor;
6357        END IF;
6358 
6359        IF (wdth_cursor%isopen) THEN
6360 	  CLOSE wdth_cursor;
6361        END IF;
6362 
6363        ROLLBACK TO split_op_instance_sp;
6364 
6365        x_return_status:=g_ret_sts_error;
6366 
6367     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
6368        IF (l_debug=1) THEN
6369 	  print_debug('Unexpected Error (fnd_api.g_exc_unexpected_error) occured at '||l_progress,l_module_name,1);
6370 	  --print_debug('UnExpected Error: '||SQLERRM,l_module_name,1);
6371        END IF;
6372 
6373        IF (inbound_document_details%ISOPEN) THEN
6374 	  CLOSE inbound_document_details;
6375        END IF;
6376 
6377        IF (wopi_cursor%ISOPEN) THEN
6378              CLOSE wopi_cursor;
6379        END IF;
6380 
6381        IF (wooi_cursor%ISOPEN) THEN
6382 	  CLOSE wooi_cursor;
6383        END IF;
6384 
6385        ROLLBACK TO split_op_instance_sp;
6386 
6387        x_return_status:=g_ret_sts_unexp_error;
6388 
6389     WHEN OTHERS THEN
6390        print_debug(SQLERRM, l_module_name, 1);
6391 
6392        IF fnd_msg_pub.check_msg_level(g_msg_lvl_unexp_error) THEN
6393 	  fnd_msg_pub.add_exc_msg(g_pkg_name, l_module_name, SQLERRM);
6394        END IF; /* fnd_msg.... */
6395 
6396        fnd_msg_pub.count_and_get(
6397 				  p_count => x_msg_count,
6398 				  p_data  => x_msg_data
6399 				  );
6400 
6401        IF (SQLCODE<-20000) THEN
6402 	  IF (l_debug=1) THEN
6403 	     print_debug('This is an user defined exception occured at '||l_progress,l_module_name,1);
6404 	  END IF;
6405 	  x_error_code:=-(SQLCODE+20000);
6406 	  x_return_status:=g_ret_sts_error;
6407 	ELSE
6408 	  x_return_status := g_ret_sts_unexp_error;
6409        END IF;
6410 
6411        IF (inbound_document_details%ISOPEN) THEN
6412 	  CLOSE inbound_document_details;
6413        END IF;
6414 
6415        IF (wopi_cursor%ISOPEN) THEN
6416 	  CLOSE wopi_cursor;
6417        END IF;
6418 
6419        IF (wooi_cursor%ISOPEN) THEN
6420 	  CLOSE wooi_cursor;
6421        END IF;
6422 
6423        IF (wdth_cursor%isopen) THEN
6424 	  CLOSE wdth_cursor;
6425        END IF;
6426 
6427        ROLLBACK TO split_op_instance_sp;
6428 
6429  END Split_Operation_instance;
6430 
6431 
6432 
6433 /**
6434   *   Cleanup_operation_instance
6435   *   <p>This procedure reverts the status of current operation
6436   *   instance whenever any processing exception occurs i.e. ,
6437   *   when user presses F2 or transaction manager errors
6438   *   out.</p>
6439   *
6440   *  @param x_return_status    Return Status
6441   *  @param x_msg_data         REturns the Message Data
6442   *  @param x_msg_count        Returns the Error Message
6443   *  @param x_error_code       Returns appropriate error code in case of an error .
6444   *  @param p_source_task_id   Identifier of the source document record
6445   *  @param p_activity_type_id Lookup code of the Activity type Id
6446   **/
6447  PROCEDURE   Cleanup_Operation_Instance(
6448       x_return_status     OUT  NOCOPY  VARCHAR2,
6449       x_msg_data          OUT  NOCOPY  FND_NEW_MESSAGES.MESSAGE_TEXT%TYPE,
6450       x_msg_count         OUT  NOCOPY  NUMBER,
6451       x_error_code        OUT  NOCOPY  NUMBER,
6452       p_source_task_id    IN           NUMBER,
6453       p_activity_type_id  IN           NUMBER )IS
6454 
6455 	 l_debug               NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
6456 	 l_module_name         VARCHAR2(30) := 'Cleanup_operation_instance';
6457 	 l_progress            NUMBER;
6458 	 l_return_status       VARCHAR2(1);
6459 	 l_msg_count           NUMBER;
6460 	 l_msg_data            VARCHAR2(400);
6461 
6462 	 l_wdt_task_id         NUMBER;
6463 	 l_wdt_status          NUMBER;
6464 	 l_first_op_sequence   NUMBER;
6465 
6466 	 CURSOR c_inbound_document_details IS
6467 	    SELECT transaction_temp_id,
6468 	      operation_plan_id,
6469 	      organization_id
6470 	      FROM mtl_material_transactions_temp
6471 	      WHERE transaction_temp_id=p_source_task_id;
6472 
6473 	 l_inbound_doc_rec  c_inbound_document_details%ROWTYPE;
6474 
6475 	 CURSOR c_wooi_data_rec IS
6476 	    SELECT operation_instance_id,
6477 	      operation_type_id,
6478 	      operation_plan_detail_id,
6479 	      op_plan_instance_id,
6480 	      operation_status,
6481 	      operation_sequence,
6482 	      sug_to_sub_code,
6483 	      sug_to_locator_id,
6484 	      organization_id
6485 	      FROM wms_op_operation_instances
6486 	      WHERE source_task_id = p_source_task_id
6487 	      AND activity_type_id = p_activity_type_id
6488 	      AND operation_status = g_op_ins_stat_active;
6489 
6490 	 l_wooi_data_rec c_wooi_data_rec%ROWTYPE:=NULL;
6491 	 l_wooi_rec wms_op_operation_instances%ROWTYPE:=NULL;
6492 
6493 	 l_wopi_update_rec wms_op_plan_instances%ROWTYPE:=NULL;
6494  BEGIN
6495 
6496     IF (l_debug = 1) THEN
6497        print_debug(' Entered. ',l_module_name,1);
6498        print_DEBUG(' p_source_task_id ==> '||p_source_task_id ,l_module_name,3);
6499        print_DEBUG(' p_activity_type_id ==> '||p_activity_type_id ,l_module_name,3);
6500     END IF;
6501     x_return_status  := g_ret_sts_success;
6502     l_progress := 10;
6503     SAVEPOINT sp_cleanup_oprtn_instance;
6504 
6505 
6506     IF p_source_task_id IS NULL THEN
6507        IF (l_debug=1) THEN
6508 	  print_debug('Invalid input param. p_source_task_id Cannot be NULL.',l_module_name,4);
6509        END IF;
6510        RAISE FND_API.G_EXC_ERROR;
6511     END IF;
6512 
6513 
6514     IF p_activity_type_id IS NULL THEN
6515        IF (l_debug=1) THEN
6516 	  print_debug('Invalid input param. p_activity_type_id Cannot be NULL.',l_module_name,4);
6517        END IF;
6518        RAISE FND_API.G_EXC_ERROR;
6519     END IF;
6520 
6521 
6522     l_progress := 20;
6523 
6524     BEGIN
6525        SELECT task_id, status
6526 	 INTO l_wdt_task_id, l_wdt_status
6527 	 FROM wms_dispatched_tasks
6528 	 WHERE transaction_temp_id = p_source_task_id
6529 	 AND task_type IN (g_wms_task_type_putaway, g_wms_task_type_inspect);
6530 
6531     EXCEPTION
6532        WHEN no_data_found THEN
6533 	  IF (l_debug=1) THEN
6534 	     print_debug('WDT does not exist for this MMTT.',l_module_name,4);
6535 	  END IF;
6536 	  RAISE FND_API.G_EXC_ERROR;
6537 
6538     END;
6539 
6540     l_progress := 30;
6541 
6542     IF (l_debug=1) THEN
6543        print_debug('l_wdt_task_id = '||l_wdt_task_id,l_module_name,4);
6544        print_debug('l_wdt_status = '||l_wdt_status,l_module_name,4);
6545     END IF;
6546 
6547     IF l_wdt_status NOT IN (g_task_status_loaded, g_task_status_dispatched) THEN
6548        IF (l_debug=1) THEN
6549 	  print_debug('Invalid WDT status.' ,l_module_name,4);
6550        END IF;
6551        fnd_message.set_name('WMS', 'WMS_ATF_INVALID_TASK_STATUS');
6552        fnd_msg_pub.ADD;
6553        RAISE FND_API.G_EXC_ERROR;
6554     END IF; -- IF l_wdt_status NOT IN (g_task_status_loaded, g_task_status_dispatched)
6555 
6556 
6557     IF l_wdt_status = g_task_status_dispatched THEN
6558 
6559        --This can happened for two operations today,
6560        --1.	Inspect
6561        --2.	Load
6562 
6563        --NOTE: Today the status of WDT is going to be 'Dispatched' for load and inspect
6564        --      (for drop it will be 'Loaded').
6565        --      But for Outbound/Warehousing tasks or when we have Inbound
6566        --      dispatched tasks then we CAN have a status of 'Active' for WDT records
6567 
6568        IF (l_debug=1) THEN
6569 	  print_debug('WDT status is dispatched, delete WDT record : task_id = ' || l_wdt_task_id,l_module_name,4);
6570        END IF;
6571 
6572        l_progress := 40;
6573 
6574        DELETE wms_dispatched_tasks
6575 	 WHERE task_id = l_wdt_task_id;
6576 
6577        l_progress := 50;
6578 
6579      ELSIF  l_wdt_status = g_task_status_loaded THEN
6580 
6581        IF (l_debug=1) THEN
6582 	  print_debug('WDT status is loaded, Do not need to cleanup anything for this WDT: task_id = ' || l_wdt_task_id,l_module_name,4);
6583        END IF;
6584 
6585 
6586     END IF;  --  IF l_wdt_status = g_task_status_dispatched
6587 
6588 
6589     l_progress := 60;
6590 
6591     OPEN c_inbound_document_details;
6592 
6593     FETCH c_inbound_document_details
6594       INTO l_inbound_doc_rec;
6595 
6596     IF c_inbound_document_details%notfound THEN
6597        IF (l_debug=1) THEN
6598 	  print_debug('Invalid input param. p_source_task_id does not match to an MMTT record.',l_module_name,4);
6599        END IF;
6600        RAISE FND_API.G_EXC_ERROR;
6601     END IF;
6602 
6603     CLOSE c_inbound_document_details;
6604 
6605     l_progress := 65;
6606 
6607 
6608 
6609     IF l_inbound_doc_rec.operation_plan_id IS NULL THEN
6610        IF (l_debug=1) THEN
6611 	  print_debug('Operation plan ID is null for this MMTT. Nothing more needs to be cleaned up.',l_module_name,4);
6612 	  print_debug('x_return_status ==> '||x_return_status,l_module_name,3);
6613 	  print_debug('x_msg_data ==> '||x_msg_data,l_module_name,3);
6614 	  print_debug('x_msg_count ==> '||x_msg_count,l_module_name,3);
6615 	  print_debug('x_error_code ==> '||x_error_code,l_module_name,3);
6616 
6617 	  print_debug(' Before Exiting . ',l_module_name,3);
6618 
6619        END IF;
6620 
6621        IF  l_wdt_status = g_task_status_loaded THEN
6622 
6623 	  IF (l_debug = 1) THEN
6624 	     print_debug('l_wdt_status = '||g_task_status_loaded||', Drop operation, call WMS_OP_INBOUND_PVT.cleanup with following parameters: ',l_module_name,4);
6625 	     print_debug('p_source_task_id => ' || p_source_task_id,l_module_name,4);
6626 	  END IF;
6627 
6628 	  l_progress := 72;
6629 
6630 	  -- This call will cleanup wms_task_type (-1 -> 2) and lpn columns
6631 
6632 	  wms_op_inbound_pvt.cleanup
6633 	    (
6634 	     x_return_status      => l_return_status
6635 	     , x_msg_data         => l_msg_data
6636 	     , x_msg_count        => l_msg_count
6637 	     , p_source_task_id   => p_source_task_id
6638 	     );
6639 
6640 	  l_progress := 74;
6641 
6642 	  IF (l_debug=1) THEN
6643 	     print_debug('After calling wms_op_inbound_pvt.cleanup.',l_module_name,4);
6644 	     print_debug('x_return_status => '|| l_return_status,l_module_name,4);
6645 	     print_debug('x_msg_count => '|| l_msg_count,l_module_name,4);
6646 	     print_debug('x_msg_data => '|| l_msg_data,l_module_name,4);
6647 	  END IF;
6648 
6649 	  IF l_return_status <>FND_API.g_ret_sts_success THEN
6650 	     IF (l_debug=1) THEN
6651 		print_debug('wms_op_inbound_pvt.cleanup finished with error. l_return_status = ' || l_return_status,l_module_name,4);
6652 	     END IF;
6653 
6654 	     RAISE FND_API.G_EXC_ERROR;
6655 	  END IF;
6656 
6657        END IF;
6658 
6659        RETURN;
6660 
6661     END IF;  -- IF l_mmtt_rec.operation_plan_id IS NULL
6662 
6663     l_progress := 80;
6664 
6665     -- fetch the current active operation instance for this task
6666     OPEN c_wooi_data_rec;
6667 
6668     FETCH c_wooi_data_rec INTO l_wooi_data_rec;
6669     IF c_wooi_data_rec%notfound THEN
6670        IF (l_debug = 1) THEN
6671 	  print_debug('Active operation instance record does not exist for this task.',l_module_name,4);
6672        END IF;
6673        fnd_message.set_name('WMS', 'WMS_ATF_NO_ACTIVE_PLAN');
6674        fnd_msg_pub.ADD;
6675        RAISE FND_API.G_EXC_ERROR;
6676     END IF;
6677 
6678     CLOSE c_wooi_data_rec;
6679 
6680     l_progress := 90;
6681 
6682     l_wooi_rec.operation_instance_id := l_wooi_data_rec.operation_instance_id;
6683     l_wooi_rec.operation_status := g_op_ins_stat_pending;
6684 
6685 
6686     IF p_activity_type_id = g_op_activity_inbound THEN
6687        IF (l_debug=1) THEN
6688 	  print_debug('Inbound Activity.',l_module_name,4);
6689        END IF;
6690 
6691 
6692        -- Firs need to update current active operation instance to pending
6693 
6694        IF (l_debug=1) THEN
6695 	  print_debug('Before calling wms_op_runtime_pvt_apis.update_operation_instance with following parameters: ',l_module_name,4);
6696 	  print_debug('l_wooi_rec.operation_instance_id => '|| l_wooi_rec.operation_instance_id,l_module_name,4);
6697 	  print_debug('l_wooi_rec.operation_status => '|| l_wooi_rec.operation_status,l_module_name,4);
6698        END IF;
6699 
6700        l_progress := 120;
6701 
6702        wms_op_runtime_pvt_apis.update_operation_instance
6703 	 (x_return_status     => l_return_status
6704 	  , x_msg_count       => l_msg_count
6705 	  , x_msg_data        => l_msg_data
6706 	  , p_update_rec      => l_wooi_rec);
6707 
6708        l_progress := 130;
6709 
6710        IF (l_debug=1) THEN
6711 	  print_debug('After calling wms_op_runtime_pvt_apis.update_operation_instance: ' ,l_module_name,4);
6712        	  print_debug('x_return_status => '|| l_return_status,l_module_name,4);
6713 	  print_debug('x_msg_count => '|| l_msg_count,l_module_name,4);
6714 	  print_debug('x_msg_data => '|| l_msg_data,l_module_name,4);
6715        END IF;
6716 
6717        IF l_return_status <>FND_API.g_ret_sts_success THEN
6718 	  IF (l_debug=1) THEN
6719 	     print_debug('wms_op_runtime_pvt_apis.update_operation_instance finished with error. l_return_status = ' || l_return_status,l_module_name,4);
6720 	  END IF;
6721 
6722 	  RAISE FND_API.G_EXC_ERROR;
6723        END IF;
6724 
6725        BEGIN
6726 	  -- If this the first operation within a plan,
6727 	  -- Need to revert plan instance to pending.
6728 
6729 	  l_progress := 130;
6730 
6731 	  SELECT MIN(wopd1.operation_sequence)
6732 	    INTO l_first_op_sequence
6733 	    FROM wms_op_plan_details wopd1
6734 	    WHERE wopd1.operation_plan_id =
6735 	    (SELECT wopd2.operation_plan_id
6736 	     FROM wms_op_plan_details wopd2
6737 	     WHERE wopd2.operation_plan_detail_id = l_wooi_data_rec.operation_plan_detail_id
6738 	     );
6739 
6740 	  l_progress := 140;
6741 
6742        EXCEPTION
6743 	  WHEN OTHERS THEN
6744 	     IF (l_debug=1) THEN
6745 		print_debug('Exception when getting min operation_sequence.' ,l_module_name,4);
6746 	     END IF;
6747 
6748 	     RAISE FND_API.G_EXC_ERROR;
6749        END;
6750 
6751 
6752        IF l_first_op_sequence = l_wooi_data_rec.operation_sequence AND
6753 	 l_wooi_data_rec.operation_type_id <> g_op_type_drop
6754 	 -- This check is necessary to make sure crossdock plan
6755 	 -- instance not incorrectly reverted to pending when cleaning up a drop
6756 	 -- since load/drop for a crossdock operation have the same operation_sequence
6757 	 THEN
6758 
6759 	  l_wopi_update_rec.status              := g_op_ins_stat_pending;
6760 	  l_wopi_update_rec.op_plan_instance_id := l_wooi_data_rec.op_plan_instance_id;
6761 
6762 	  IF (l_debug=1) THEN
6763 	     print_debug('Current operation is the first step of the operation plan.',l_module_name,4);
6764 
6765 	     print_debug('Calling WMS_OP_RUNTIME_PVT_APIS.Update_plan_instance with the following values to be updated',l_module_name,4);
6766 	     print_debug('l_wopi_update_rec.status    => '||l_wopi_update_rec.status,l_module_name,9);
6767 	     print_debug('l_wopi_update_rec.op_plan_instance_id => '||l_wopi_update_rec.op_plan_instance_id,l_module_name,9);
6768 	  END IF;
6769 
6770 	  l_progress := 150;
6771 
6772 	  WMS_OP_RUNTIME_PVT_APIS.Update_Plan_Instance
6773             (p_update_rec    => l_wopi_update_rec,
6774              x_return_status => l_return_status,
6775              x_msg_count     => x_msg_count,
6776              x_msg_data      => x_msg_data);
6777 
6778 	  l_progress := 160;
6779 
6780 	  IF (l_debug=1) THEN
6781 	     print_debug('After calling WMS_OP_RUNTIME_PVT_APIS.Update_plan_instance.',l_module_name,4);
6782 	     print_debug('x_return_status => '|| l_return_status,l_module_name,4);
6783 	     print_debug('x_msg_count => '|| l_msg_count,l_module_name,4);
6784 	     print_debug('x_msg_data => '|| l_msg_data,l_module_name,4);
6785 	  END IF;
6786 
6787 	  IF l_return_status <>FND_API.g_ret_sts_success THEN
6788 	     IF (l_debug=1) THEN
6789 		print_debug('wms_op_runtime_pvt_apis.Update_plan_instance finished with error. l_return_status = ' || l_return_status,l_module_name,4);
6790 	     END IF;
6791 
6792 	     RAISE FND_API.G_EXC_ERROR;
6793 	  END IF;
6794 
6795 
6796        END IF;  -- IF l_first_op_sequence = l_wooi_data_rec.operation_sequence
6797 
6798 
6799        IF l_wooi_data_rec.operation_type_id = g_op_type_drop THEN
6800 	  IF (l_debug = 1) THEN
6801 	     print_debug('Drop operation, call WMS_OP_INBOUND_PVT.cleanup with following parameters: ',l_module_name,4);
6802 	     print_debug('p_source_task_id => ' || p_source_task_id,l_module_name,4);
6803 	  END IF;
6804 
6805 	  l_progress := 170;
6806 
6807 	  wms_op_inbound_pvt.cleanup
6808 	    (
6809 	     x_return_status      => l_return_status
6810 	     , x_msg_data         => l_msg_data
6811 	     , x_msg_count        => l_msg_count
6812 	     , p_source_task_id   => p_source_task_id
6813 	     );
6814 
6815 	  l_progress := 180;
6816 
6817 	  IF (l_debug=1) THEN
6818 	     print_debug('After calling wms_op_inbound_pvt.cleanup.',l_module_name,4);
6819 	     print_debug('x_return_status => '|| l_return_status,l_module_name,4);
6820 	     print_debug('x_msg_count => '|| l_msg_count,l_module_name,4);
6821 	     print_debug('x_msg_data => '|| l_msg_data,l_module_name,4);
6822 	  END IF;
6823 
6824 	  IF l_return_status <>FND_API.g_ret_sts_success THEN
6825 	     IF (l_debug=1) THEN
6826 		print_debug('wms_op_inbound_pvt.cleanup finished with error. l_return_status = ' || l_return_status,l_module_name,4);
6827 	     END IF;
6828 
6829 	     RAISE FND_API.G_EXC_ERROR;
6830 	  END IF;
6831 
6832 
6833        END IF;  -- IF l_wooi_data_rec.operation_type_id = g_op_type_drop
6834 
6835 
6836     END IF; -- IF p_activity_type_id = g_op_activity_inbound
6837 
6838 
6839     IF (l_debug = 1) THEN
6840        print_debug('x_return_status ==> '||x_return_status,l_module_name,3);
6841        print_debug('x_msg_data ==> '||x_msg_data,l_module_name,3);
6842        print_debug('x_msg_count ==> '||x_msg_count,l_module_name,3);
6843        print_debug('x_error_code ==> '||x_error_code,l_module_name,3);
6844 
6845        print_debug(' Before Exiting . ',l_module_name,3);
6846     END IF;
6847 
6848  EXCEPTION
6849 
6850     WHEN fnd_api.g_exc_error THEN
6851        IF (l_debug=1) THEN
6852 	  print_debug('Error (fnd_api.g_exc_error) occured at'||l_progress,l_module_name,1);
6853        END IF;
6854        x_return_status:=FND_API.G_RET_STS_ERROR;
6855        ROLLBACK TO sp_cleanup_oprtn_instance;
6856 
6857        IF c_inbound_document_details%isopen THEN
6858 	  CLOSE c_inbound_document_details;
6859        END IF;
6860 
6861        IF c_wooi_data_rec%isopen THEN
6862 	  CLOSE c_wooi_data_rec;
6863        END IF;
6864        fnd_message.set_name('WMS', 'WMS_ATF_CLEANUP_FAILURE');
6865        fnd_msg_pub.ADD;
6866 
6867     WHEN fnd_api.g_exc_unexpected_error THEN
6868 
6869        x_return_status:=FND_API.G_RET_STS_UNEXP_ERROR;
6870        IF (l_debug=1) THEN
6871 	  print_debug('Unexpected Error (fnd_api.g_exc_unexpected_error) occured at '||l_progress,l_module_name,3);
6872        END IF;
6873        ROLLBACK TO sp_cleanup_oprtn_instance;
6874 
6875        IF c_inbound_document_details%isopen THEN
6876 	  CLOSE c_inbound_document_details;
6877        END IF;
6878 
6879        IF c_wooi_data_rec%isopen THEN
6880 	  CLOSE c_wooi_data_rec;
6881        END IF;
6882 
6883        fnd_message.set_name('WMS', 'WMS_ATF_CLEANUP_FAILURE');
6884        fnd_msg_pub.ADD;
6885 
6886     WHEN OTHERS THEN
6887 
6888        x_return_status:=FND_API.G_RET_STS_UNEXP_ERROR;
6889        IF (l_debug=1) THEN
6890 	  print_debug('Other Error occured at '||l_progress,l_module_name,1);
6891 	  IF SQLCODE IS NOT NULL THEN
6892 	     print_debug('With SQL error : ' || SQLERRM(SQLCODE), l_module_name,1);
6893 	  END IF;
6894        END IF;
6895        ROLLBACK TO sp_cleanup_oprtn_instance;
6896 
6897        IF c_inbound_document_details%isopen THEN
6898 	  CLOSE c_inbound_document_details;
6899        END IF;
6900 
6901        IF c_wooi_data_rec%isopen THEN
6902 	  CLOSE c_wooi_data_rec;
6903        END IF;
6904        fnd_message.set_name('WMS', 'WMS_ATF_CLEANUP_FAILURE');
6905        fnd_msg_pub.ADD;
6906 
6907  END Cleanup_Operation_instance;
6908 
6909 
6910 
6911  /**
6912   *   Cancel_operation_Plan
6913   *   <p>This procedure cancels the operation plan(s). It is called from control board</p>
6914   *
6915   *  @param x_return_status    Return Status
6916   *  @param x_msg_data         Returns the Message Data
6917   *  @param x_msg_count        Returns the Error Message
6918   *  @param x_error_code       Returns appropriate error code in case of an error .
6919   *  @param p_source_task_id   Identifier of the source document record
6920   *  @param p_activity_type_id Lookup code of the Activity type Id
6921   **/
6922    PROCEDURE  Cancel_Operation_Plan(
6923       x_return_status     OUT  NOCOPY  VARCHAR2,
6924       x_msg_data          OUT  NOCOPY  FND_NEW_MESSAGES.MESSAGE_TEXT%TYPE,
6925       x_msg_count         OUT  NOCOPY  NUMBER,
6926       x_error_code        OUT  NOCOPY  NUMBER,
6927       p_source_task_id    IN           NUMBER,
6928       p_activity_type_id  IN           NUMBER,
6929       p_retain_mmtt       IN   VARCHAR2 DEFAULT 'N',
6930       p_mmtt_error_code   IN   VARCHAR2 DEFAULT NULL,
6931       p_mmtt_error_explanation   IN   VARCHAR2 DEFAULT NULL
6932       ) IS
6933 
6934       CURSOR c_inbound_doc IS
6935        SELECT operation_plan_id,
6936               transaction_temp_id,
6937               parent_line_id
6938        FROM MTL_MATERIAL_TRANSACTIONS_TEMP
6939        WHERE transaction_temp_id = p_source_task_id;
6940 
6941       CURSOR c_operation_instance IS
6942          SELECT operation_instance_id,
6943                 operation_status,
6944                 op_plan_instance_id,
6945                 operation_type_id
6946          FROM WMS_OP_OPERATION_INSTANCES
6947          WHERE source_task_id = p_source_task_id
6948          AND activity_type_id = p_activity_type_id
6949 	   AND operation_status <> g_op_ins_stat_completed
6950 	   ORDER BY operation_sequence DESC;
6951 
6952 
6953        l_inbound_doc            c_inbound_doc%ROWTYPE;
6954        l_operation_instance_rec c_operation_instance%ROWTYPE;
6955        l_plan_status            NUMBER := -1;
6956        l_wooi_update_rec        WMS_OP_OPERATION_INSTANCES%ROWTYPE;
6957        l_wopi_update_rec        WMS_OP_PLAN_INSTANCES%ROWTYPE;
6958        l_parent_source_task_id  NUMBER;
6959 
6960 
6961        l_debug       NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
6962        l_module_name VARCHAR2(30) := 'Cancel_operation_Plan';
6963        l_progress    NUMBER;
6964 
6965    BEGIN
6966 
6967          IF (l_debug = 1) THEN
6968              print_DEBUG(' p_source_task_id ==> '||p_source_task_id ,l_module_name,3);
6969              print_DEBUG(' p_activity_type_id ==> '||p_activity_type_id ,l_module_name,3);
6970          END IF;
6971 
6972          SAVEPOINT cancel_op_plan_sp;
6973 
6974          x_return_status  := g_ret_sts_success;
6975          l_progress :=10;
6976 
6977          IF (p_source_task_id IS NULL OR p_activity_type_id IS NULL) THEN
6978 
6979             IF (l_debug=1) THEN
6980               print_debug('Invalid Input passed to Cancel_operation_plan',l_module_name,1);
6981             END IF;
6982 
6983             /*If the passed P_SOURCE_TASK_ID is null then raise Invalid Argument Error
6984              *and return with appropriate error code.
6985              */
6986 
6987             RAISE_APPLICATION_ERROR(INVALID_INPUT,'Invalid inputs passed'||p_source_task_id||' '||p_activity_type_id);
6988 
6989          END IF;
6990 
6991          l_progress:=20;
6992 
6993          /*Query the document tables based on p_source_task_id and populate the record variable.
6994            If p_activity_id=INBOUND then
6995            Query MMTT  where transaction_temp_id is equal p_source_task_id and
6996            populate the record variable.*/
6997          IF (p_activity_type_id = G_OP_ACTIVITY_INBOUND)  THEN
6998 
6999             OPEN c_inbound_doc;
7000 
7001             FETCH c_inbound_doc INTO l_inbound_doc;
7002 
7003             IF (c_inbound_doc%NOTFOUND) THEN
7004 
7005                CLOSE c_inbound_doc;
7006 
7007                IF (l_debug=1) THEN
7008                   print_debug('Document record does not exist',l_module_name,1);
7009                END IF;
7010 
7011                RAISE_APPLICATION_ERROR(INVALID_DOC_ID,'Invalid Document');
7012 
7013             END IF;
7014 
7015             CLOSE c_inbound_doc;
7016 
7017             l_progress:=30;
7018 
7019              /* Non ATF casse..Return Success*/
7020              IF l_inbound_doc.operation_plan_id IS NULL THEN
7021 
7022                 IF (l_debug=1) THEN
7023                   print_debug('Operation Plan Id null on MMTT:non ATF Case',l_module_name,9);
7024                 END IF;
7025 
7026                 RETURN;
7027 
7028              ELSE
7029                 l_parent_source_task_id:= l_inbound_doc.parent_line_id;
7030 
7031              END IF;/* Non ATF Case*/
7032 
7033          END IF; /*Activity is Inbound*/
7034 
7035          l_progress :=40;
7036 
7037          /*Populate the current Operation Instance (WOOI) record based
7038            on the p_source_task_id and the pending operation
7039           */
7040          OPEN c_operation_instance;
7041 
7042          FETCH c_operation_instance INTO l_operation_instance_rec;
7043 
7044          IF (c_operation_instance%NOTFOUND) THEN
7045 
7046             IF (l_debug=1) THEN
7047                print_debug('Operation Instance does not exists',l_module_name,9);
7048             END IF;
7049 
7050             CLOSE c_operation_instance;
7051 
7052             RAISE_APPLICATION_ERROR(OPERATION_INSTANCE_NOT_EXISTS,'Operation Instance does not exist for document record with Id '||p_source_task_id);
7053 
7054          END IF;
7055 
7056          CLOSE c_operation_instance;
7057 
7058          l_progress:=50;
7059 
7060 
7061          /* If the Operation Status is 'ACTIVE' then raise Invalid State Change Error,
7062           *  stating that you cannot change the status of a plan from 'ACTIVE' to 'CANCELLED'.
7063           */
7064 
7065          IF (l_operation_instance_rec.operation_status = G_OP_INS_STAT_ACTIVE)  THEN
7066 
7067             IF (l_debug=1) THEN
7068                print_debug('Invalid status for a cancel Operation',l_module_name,9);
7069             END IF;
7070 
7071             RAISE_APPLICATION_ERROR(INVALID_STATUS_FOR_OPERATION,'Cancellation cannot be performed on Active Record');
7072          END IF;
7073 
7074          l_progress:=60;
7075 
7076          /*If the current pending operation is 'DROP'
7077            Raise Error stating that cancellation cannot be performed
7078            for this operation and return appropriate err_code
7079            */
7080          IF (l_operation_instance_rec.operation_type_id =g_op_type_drop AND p_retain_mmtt = 'N') THEN
7081             IF (l_debug=1) THEN
7082                print_debug('Cancellation cannnot be performed on a Pending Drop operation',l_module_name,9);
7083             END IF;
7084 
7085             RAISE_APPLICATION_ERROR(INVALID_STATUS_FOR_OPERATION,'Cancellation cannot be performed on a Drop Operation');
7086 
7087          END IF;
7088 
7089          l_progress:=70;
7090 
7091 
7092          /*Mark the current Operation Plan (WOPI record) as 'Cancelled';
7093            call WMS_OP_RUNTIME_PVT_APIS.Update_operation_plan_instance to update
7094            the Operation Plan instance*/
7095          l_wopi_update_rec.status              := G_OP_INS_STAT_CANCELLED;
7096          l_wopi_update_rec.op_plan_instance_id := l_operation_instance_rec.op_plan_instance_id;
7097 
7098          l_progress := 100;
7099 
7100          IF (l_debug=1) THEN
7101             print_debug('Calling Update_plan_instance with the following values to be updated',l_module_name,9);
7102             print_debug('Plan status      => '||l_wopi_update_rec.status,l_module_name,9);
7103             print_debug('Plan Instance id => '||l_wopi_update_rec.op_plan_instance_id,l_module_name,9);
7104          END IF;
7105 
7106          WMS_OP_RUNTIME_PVT_APIS.Update_Plan_Instance
7107             (p_update_rec    => l_wopi_update_rec,
7108              x_return_status => x_return_status,
7109              x_msg_count     => x_msg_count,
7110              x_msg_data      => x_msg_data);
7111 
7112          l_progress:=110;
7113 
7114          IF (l_debug=1) THEN
7115            print_debug('Return status from table handler'||x_return_status,l_module_name,9);
7116          END IF;
7117 
7118          IF (x_return_status=g_ret_sts_error) THEN
7119            RAISE FND_API.G_EXC_ERROR;
7120 
7121          ELSIF (x_return_status<>g_ret_sts_success) THEN
7122            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7123 
7124          END IF;
7125 
7126          l_progress:=120;
7127 
7128        /*Mark the current pending Operation (WOOI record) as 'Cancelled';
7129         * call WMS_OP_RUNTIME_APIS.Update_operation_instance to update the Operation instance
7130         */
7131         l_wooi_update_rec.operation_status      := G_OP_INS_STAT_CANCELLED;
7132         l_wooi_update_rec.operation_instance_id := l_operation_instance_rec.operation_instance_id;
7133 
7134         IF (l_debug=1) THEN
7135           print_debug('Updating Operation Instance with the following values',l_module_name,9);
7136           print_debug('Operation status      ==> '||l_wooi_update_rec.operation_status,l_module_name,9);
7137           print_debug('Operation Instance Id ==> '||l_wooi_update_rec.operation_instance_id,l_module_name,9);
7138         END IF;
7139 
7140         l_progress:=130;
7141 
7142         WMS_OP_RUNTIME_PVT_APIS.Update_Operation_Instance
7143             (p_update_rec    => l_wooi_update_rec,
7144              x_return_status => x_return_status,
7145              x_msg_count     => x_msg_count,
7146              x_msg_data      => x_msg_data);
7147 
7148         IF (l_debug=1) THEN
7149             print_debug('Return status from table handler'||x_return_status,l_module_name,9);
7150         END IF;
7151 
7152         IF (x_return_status=g_ret_sts_error) THEN
7153 
7154           RAISE FND_API.G_EXC_ERROR;
7155 
7156         ELSIF (x_return_status<>g_ret_sts_success) THEN
7157 
7158           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7159 
7160         END IF;
7161 
7162 
7163        l_progress:=140;
7164        /*Call WMS_OP_RUNTIME_PVT_APIS.Archive_op_plan_instance to archive the
7165         Cancelled operation plan instance.
7166         */
7167        IF (l_debug=1) THEN
7168          print_debug('Archiving Plan Instance with Plan Instance Id'||l_operation_instance_rec.op_plan_instance_id,l_module_name,9);
7169        END IF;
7170 
7171        WMS_OP_RUNTIME_PVT_APIS.Archive_Plan_Instance
7172          (p_op_plan_instance_id => l_operation_instance_rec.op_plan_instance_id,
7173           x_return_status       => x_return_status,
7174           x_msg_count           => x_msg_count,
7175           x_msg_data            => x_msg_data);
7176 
7177        IF (l_debug=1) THEN
7178             print_debug('Return status from Archive Plan Instance'||x_return_status,l_module_name,9);
7179        END IF;
7180 
7181        l_progress:=150;
7182 
7183        IF (x_return_status=g_ret_sts_error) THEN
7184 
7185           RAISE FND_API.G_EXC_ERROR;
7186 
7187        ELSIF (x_return_status<>g_ret_sts_success) THEN
7188 
7189           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7190 
7191        END IF;
7192 
7193        /*For MMTT.parent_line_id call WMS_OP_RUNTIME_PVT_API.Archive_Dispatched_tasks to Archive WDT*/
7194        l_progress:=160;
7195 
7196        IF (l_debug=1) THEN
7197           print_debug('Calling Archive_Dispatched_tasks with the following parameters',l_module_name,4);
7198           print_debug('p_source_task_id      ==> '||l_parent_source_task_id,l_module_name,4);
7199           print_debug('p_activity_type_id    ==> '||p_activity_type_id,l_module_name,4);
7200           print_debug('p_op_plan_instance_id ==> '||l_operation_instance_rec.op_plan_instance_id,l_module_name,4);
7201           print_debug('p_op_plan_status      ==> '||G_OP_INS_STAT_CANCELLED,l_module_name,4);
7202        END IF;
7203 
7204        WMS_OP_RUNTIME_PVT_APIS.archive_dispatched_tasks(
7205         x_return_status        => x_return_status
7206       , x_msg_count            => x_msg_count
7207       , x_msg_data             => x_msg_data
7208       , p_task_id              => NULL
7209       , p_source_task_id       => l_parent_source_task_id
7210       , p_activity_type_id     => p_activity_type_id
7211       , p_op_plan_instance_id  => l_operation_instance_rec.op_plan_instance_id
7212       , p_op_plan_status       => G_OP_INS_STAT_CANCELLED);
7213 
7214       IF (l_debug=1) THEN
7215          print_debug('Return Status from Archive_dispatched_tasks is '||x_return_status,l_module_name,9);
7216       END IF;
7217 
7218       l_progress :=170;
7219 
7220       IF (x_return_status=g_ret_sts_error) THEN
7221 
7222           RAISE FND_API.G_EXC_ERROR;
7223 
7224       ELSIF (x_return_status<>g_ret_sts_success) THEN
7225 
7226           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7227 
7228       END IF;
7229 
7230       IF (p_activity_type_id = G_OP_ACTIVITY_INBOUND) THEN
7231 
7232 	 l_progress:=180;
7233 
7234 	 IF (l_debug=1) THEN
7235 	    print_debug('Inbound activity. ',l_module_name,4);
7236 	    print_debug('Before calling wms_op_inbound_pvt.cancel with following parameters: ' ,l_module_name,4);
7237 	    print_debug('p_source_task_id => '|| p_source_task_id,l_module_name,4);
7238 
7239 	 END IF;
7240 
7241 	 wms_op_inbound_pvt.cancel
7242 	   (
7243 	    x_return_status    => x_return_status,
7244 	    x_msg_data         => x_msg_data,
7245 	    x_msg_count        => x_msg_count,
7246 	    p_source_task_id   => p_source_task_id,
7247 	    p_retain_mmtt      => p_retain_mmtt,
7248 	    p_mmtt_error_code  => p_mmtt_error_code,
7249 	    p_mmtt_error_explanation => p_mmtt_error_explanation
7250 	    );
7251 
7252 	IF (l_debug=1) THEN
7253 	   print_debug('After calling wms_op_inbound_pvt.cancel.' ,l_module_name,4);
7254 	   print_debug('x_return_status => '|| x_return_status,l_module_name,4);
7255 	   print_debug('x_msg_data => '|| x_msg_data,l_module_name,4);
7256 	   print_debug('x_msg_count => '|| x_msg_count,l_module_name,4);
7257 	END IF;
7258 
7259 	IF x_return_status <>FND_API.g_ret_sts_success THEN
7260 	   IF (l_debug=1) THEN
7261 	      print_debug('wms_op_inbound_pvt.cancel finished with error. x_return_status = ' || x_return_status,l_module_name,4);
7262 	   END IF;
7263 
7264 	   RAISE FND_API.G_EXC_ERROR;
7265 	END IF;
7266 
7267          l_progress := 200;
7268       END IF; -- IF (p_activity_type_id = G_OP_ACTIVITY_INBOUND)
7269 
7270    EXCEPTION
7271 
7272         WHEN FND_API.G_EXC_ERROR THEN
7273 
7274           IF (l_debug=1) THEN
7275              print_debug('Expected Error Obtained at'||l_progress,l_module_name,1);
7276           END IF;
7277 
7278           IF c_inbound_doc%ISOPEN THEN
7279              CLOSE c_inbound_doc;
7280           END IF;
7281 
7282           IF c_operation_instance%ISOPEN THEN
7283              CLOSE c_operation_instance;
7284           END IF;
7285 
7286 
7287           ROLLBACK TO cancel_op_plan_sp;
7288 
7289           x_return_status:=g_ret_sts_error;
7290 
7291        WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
7292 
7293           IF (l_debug=1) THEN
7294              print_debug('UnExpected Error Obtained at'||l_progress||SQLERRM,l_module_name,1);
7295           END IF;
7296 
7297           IF (l_debug=1) THEN
7298              print_debug('Expected Error Obtained at'||l_progress,l_module_name,1);
7299           END IF;
7300 
7301           IF c_inbound_doc%ISOPEN THEN
7302              CLOSE c_inbound_doc;
7303           END IF;
7304 
7305           IF c_operation_instance%ISOPEN THEN
7306              CLOSE c_operation_instance;
7307           END IF;
7308 
7309 
7310           ROLLBACK TO cancel_op_plan_sp;
7311           x_return_status:=g_ret_sts_unexp_error;
7312 
7313        WHEN OTHERS THEN
7314 
7315           IF (l_debug=1) THEN
7316             print_debug(l_progress||' '||SQLERRM, l_module_name,1);
7317           END IF;
7318 
7319           IF fnd_msg_pub.check_msg_level(g_msg_lvl_unexp_error) THEN
7320             fnd_msg_pub.add_exc_msg(g_pkg_name, l_module_name, SQLERRM);
7321           END IF; /* fnd_msg.... */
7322 
7323           fnd_msg_pub.count_and_get(
7324                                  p_count => x_msg_count,
7325                                  p_data  => x_msg_data
7326                                 );
7327 
7328           IF (SQLCODE<-20000) THEN
7329             IF (l_debug=1) THEN
7330               print_debug('This is a user defined exception',l_module_name,1);
7331             END IF;
7332 
7333             x_error_code:=-(SQLCODE+20000);
7334 
7335             x_return_status:=g_ret_sts_error;
7336 
7337 
7338           ELSE
7339 
7340             x_return_status := g_ret_sts_unexp_error;
7341 
7342           END IF;
7343 
7344           IF c_inbound_doc%ISOPEN THEN
7345              CLOSE c_inbound_doc;
7346           END IF;
7347 
7348           IF c_operation_instance%ISOPEN THEN
7349              CLOSE c_operation_instance;
7350           END IF;
7351 
7352 
7353          ROLLBACK TO cancel_op_plan_sp;
7354 
7355    END Cancel_Operation_Plan;
7356 
7357 
7358 /**
7359   *   Abort_operation_Plan
7360   *   <p>This procedure aborts the operation plan(s). It is called from control board</p>
7361   *
7362   *  @param x_return_status    Return Status
7363   *  @param x_msg_data         Returns the Message Data
7364   *  @param x_msg_count        Returns the Error Message
7365   *  @param x_error_code       Returns appropriate error code in case of an error .
7366   *  @param p_source_task_id   Identifier of the source document record
7367   *  @param p_activity_type_id Lookup code of the Activity type Id
7368   **/
7369    PROCEDURE  Abort_Operation_Plan(
7370       x_return_status     OUT  NOCOPY  VARCHAR2,
7371       x_msg_data          OUT  NOCOPY  FND_NEW_MESSAGES.MESSAGE_TEXT%TYPE,
7372       x_msg_count         OUT  NOCOPY  NUMBER,
7373       x_error_code        OUT  NOCOPY  NUMBER,
7374       p_source_task_id    IN           NUMBER,
7375       p_activity_type_id  IN           NUMBER,
7376       p_for_manual_drop   IN  BOOLEAN DEFAULT FALSE
7377    ) IS
7378 
7379        CURSOR c_inbound_doc IS
7380         SELECT operation_plan_id,
7381 	  move_order_line_id,
7382 	  parent_line_id,
7383 	  transfer_to_location,
7384 	  locator_id,
7385 	  organization_id,
7386 	  inventory_item_id,
7387 	  primary_quantity
7388         FROM MTL_MATERIAL_TRANSACTIONS_TEMP
7389           WHERE transaction_temp_id = p_source_task_id;
7390 
7391        CURSOR c_operation_instance IS
7392           SELECT operation_instance_id,
7393                  operation_status,
7394                  op_plan_instance_id
7395           FROM WMS_OP_OPERATION_INSTANCES
7396           WHERE source_task_id = p_source_task_id
7397           AND operation_status <> G_OP_INS_STAT_COMPLETED
7398           AND activity_type_id = p_activity_type_id
7399 	  ORDER BY operation_sequence DESC;
7400 
7401        CURSOR c_plan_instance(v_op_plan_instance_id NUMBER) IS
7402           SELECT status,
7403 	    orig_dest_sub_code,
7404 	    orig_dest_loc_id
7405           FROM WMS_OP_PLAN_INSTANCES
7406           WHERE op_plan_instance_id=v_op_plan_instance_id;
7407 
7408 
7409        l_plan_orig_dest_sub    VARCHAR2(10);
7410        l_plan_orig_dest_loc_id NUMBER;
7411 
7412 
7413        l_inbound_doc           c_inbound_doc%ROWTYPE;
7414        l_operation_plan_id     NUMBER;
7415        l_operation_instance     c_operation_instance%ROWTYPE;
7416        l_plan_inst_status      NUMBER := -1;
7417        l_wooi_update_rec       WMS_OP_OPERATION_INSTANCES%ROWTYPE:=NULL;
7418        l_wopi_update_rec       WMS_OP_PLAN_INSTANCES%ROWTYPE:=NULL;
7419        l_parent_source_task_id NUMBER;
7420        l_document_rec          mtl_material_transactions_temp%ROWTYPE;
7421 
7422 
7423 
7424        l_debug       NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
7425        l_module_name VARCHAR2(30) := 'Abort_operation_Plan';
7426        l_return_status  VARCHAR2(1);
7427        l_msg_count NUMBER;
7428        l_msg_data VARCHAR2(400);
7429 
7430        l_progress    NUMBER       ;
7431 
7432    BEGIN
7433 
7434          IF (l_debug = 1) THEN
7435              print_DEBUG(' p_source_task_id ==> '||p_source_task_id ,l_module_name,3);
7436              print_DEBUG(' p_activity_type_id ==> '||p_activity_type_id ,l_module_name,3);
7437          END IF;
7438          SAVEPOINT abort_plan_sp;
7439 
7440          x_return_status  := g_ret_sts_success;
7441 
7442          l_progress := 10;
7443 
7444          /*If the passed P_SOURCE_TASK_ID is null then raise Invalid Argument
7445            Error and return with appropriate error code.*/
7446 
7447          IF (p_source_task_id IS NULL OR p_activity_type_id IS NULL) THEN
7448 
7449             IF (l_debug=1) THEN
7450                print_debug('Invalid inputs passed'||p_source_task_id,l_module_name,9);
7451             END IF;
7452 
7453             RAISE_APPLICATION_ERROR(INVALID_INPUT,'Invalid inputs passed'||p_source_task_id||' '||p_activity_type_id);
7454 
7455          END IF;
7456 
7457          l_progress:=20;
7458 
7459          /* Query the document tables based on p_source_task_id and
7460           * populate the record variable.
7461           * If p_activity_id=INBOUND then
7462           * Query MMTT (MTLT) where transaction_temp_id is equal p_source_task_id and
7463           * populate the record variable.*/
7464 
7465          IF p_activity_type_id=G_OP_ACTIVITY_INBOUND THEN
7466 
7467             OPEN c_inbound_doc;
7468 
7469             FETCH c_inbound_doc INTO l_inbound_doc;
7470 
7471             IF c_inbound_doc%NOTFOUND THEN
7472 
7473                /* If there is no document record is found corresponding to
7474                 * P_SOURCE_TASK_ID then raise Developer Error stating that
7475                 * document must exist and return with appropriate error code.*/
7476 
7477                IF (l_debug=1) THEN
7478                   print_debug('Document record not found for source_task_id'||p_source_task_id,l_module_name,1);
7479                END IF;
7480 
7481                CLOSE c_inbound_doc;
7482 
7483                RAISE_APPLICATION_ERROR(INVALID_DOC_ID,'Invalid Document Record');
7484              END IF;
7485 
7486              CLOSE c_inbound_doc;
7487 
7488              l_progress:=30;
7489 
7490            /*Handle case where MMTT.Operation_Plan_ID is NULL.
7491              If MMTT.Operation_Plan_ID is NULL
7492                This could happen when 'user drop ' a pre-11.5.10 MMTT record.
7493            */
7494 	          IF (l_inbound_doc.operation_plan_id IS NULL) THEN
7495               /* Non ATF Case*/
7496                IF (l_debug=1) THEN
7497                  print_debug('Operation Plan Id is null:non ATF case',l_module_name,9);
7498                END IF;
7499 
7500                RETURN;
7501 
7502              END IF; /*Non ATF case*/
7503 
7504              l_parent_source_task_id := l_inbound_doc.parent_line_id;
7505 
7506          END IF;/*Inbound*/
7507 
7508 
7509          l_progress :=40;
7510 
7511          /*Populate the current Operation Instance (WOOI) record based on
7512           * the p_source_task_id  and the pending operation
7513          */
7514 
7515          OPEN c_operation_instance;
7516 
7517          FETCH c_operation_instance INTO l_operation_instance;
7518 
7519          IF (c_operation_instance%NOTFOUND) THEN
7520 
7521             IF (l_debug=1) THEN
7522                print_debug('Operation instance does not exist for record',l_module_name,1);
7523             END IF;
7524 
7525             CLOSE c_operation_instance;
7526 
7527             RAISE_APPLICATION_ERROR(OPERATION_INSTANCE_NOT_EXISTS,'Operation Instance does not exist for document record with Id '||p_source_task_id);
7528          END IF;
7529 
7530          CLOSE c_operation_instance;
7531 
7532          l_progress:=50;
7533 
7534          /*Populate Operation Plan Instance (WOPI) into the Operation Plan Instance record
7535           * based on the Operation Plan Instance field*/
7536 
7537          OPEN c_plan_instance(l_operation_instance.op_plan_instance_id);
7538 
7539 	  FETCH c_plan_instance INTO
7540 	    l_plan_inst_status,
7541 	    l_plan_orig_dest_sub,
7542 	    l_plan_orig_dest_loc_id;
7543 
7544          CLOSE c_plan_instance;
7545 
7546          l_progress:=60;
7547 
7548          IF l_plan_inst_status IS NULL OR l_plan_inst_status=-1 THEN
7549 
7550             IF (l_debug=1) THEN
7551                print_debug('Invalid Operation Plan Instance',l_module_name,1);
7552             END IF;
7553 
7554             RAISE_APPLICATION_ERROR(INVALID_PLAN_INSTANCE,'Invalid Operation Plan Instance');
7555          END IF;
7556 
7557          l_progress:=70;
7558 
7559          /*Mark the current Operation Plan (WOPI record) as 'Aborted;
7560            call WMS_OP_RUNTIME_PVT_APIS.Update_operation_plan_instance to update
7561            the Operation Plan instance*/
7562          l_wopi_update_rec.status              := G_OP_INS_STAT_ABORTED;
7563          l_wopi_update_rec.op_plan_instance_id := l_operation_instance.op_plan_instance_id;
7564 
7565          l_progress := 80;
7566 
7567          IF (l_debug=1) THEN
7568             print_debug('Calling Update_plan_instance with the following values to be updated',l_module_name,9);
7569             print_debug('Plan status      => '||l_wopi_update_rec.status,l_module_name,9);
7570             print_debug('Plan Instance id => '||l_wopi_update_rec.op_plan_instance_id,l_module_name,9);
7571          END IF;
7572 
7573          WMS_OP_RUNTIME_PVT_APIS.Update_Plan_Instance
7574             (p_update_rec    => l_wopi_update_rec,
7575              x_return_status => x_return_status,
7576              x_msg_count     => x_msg_count,
7577              x_msg_data      => x_msg_data);
7578 
7579          l_progress:=90;
7580 
7581          IF (l_debug=1) THEN
7582            print_debug('Return status from table handler'||x_return_status,l_module_name,9);
7583          END IF;
7584 
7585          IF (x_return_status=g_ret_sts_error) THEN
7586            RAISE FND_API.G_EXC_ERROR;
7587 
7588          ELSIF (x_return_status<>g_ret_sts_success) THEN
7589            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7590 
7591          END IF;
7592 
7593          l_progress:=100;
7594 
7595        /*Mark the current pending Operation (WOOI record) as 'Aborted;
7596         * call WMS_OP_RUNTIME_APIS.Update_operation_instance to update the Operation instance
7597         */
7598         l_wooi_update_rec.operation_status      := G_OP_INS_STAT_ABORTED;
7599         l_wooi_update_rec.operation_instance_id := l_operation_instance.operation_instance_id;
7600 
7601         IF (l_debug=1) THEN
7602           print_debug('Updating Operation Instance with the following values',l_module_name,9);
7603           print_debug('Operation status      ==> '||l_wooi_update_rec.operation_status,l_module_name,9);
7604           print_debug('Operation Instance Id ==> '||l_wooi_update_rec.operation_instance_id,l_module_name,9);
7605         END IF;
7606 
7607         l_progress:=110;
7608 
7609         WMS_OP_RUNTIME_PVT_APIS.Update_Operation_Instance
7610             (p_update_rec    => l_wooi_update_rec,
7611              x_return_status => x_return_status,
7612              x_msg_count     => x_msg_count,
7613              x_msg_data      => x_msg_data);
7614 
7615         IF (l_debug=1) THEN
7616             print_debug('Return status from table handler'||x_return_status,l_module_name,9);
7617         END IF;
7618 
7619         IF (x_return_status=g_ret_sts_error) THEN
7620 
7621           RAISE FND_API.G_EXC_ERROR;
7622 
7623         ELSIF (x_return_status<>g_ret_sts_success) THEN
7624 
7625           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7626 
7627         END IF;
7628 
7629 
7630        l_progress:=120;
7631 
7632        /*Call WMS_OP_RUNTIME_PVT_APIS.Archive_op_plan_instance to archive the
7633         aborted operation plan instance.
7634         */
7635        IF (l_debug=1) THEN
7636          print_debug('Archiving Plan Instance with Plan Instance Id'||l_operation_instance.op_plan_instance_id,l_module_name,9);
7637        END IF;
7638 
7639        WMS_OP_RUNTIME_PVT_APIS.Archive_Plan_Instance
7640          (p_op_plan_instance_id => l_operation_instance.op_plan_instance_id,
7641           x_return_status       => x_return_status,
7642           x_msg_count           => x_msg_count,
7643           x_msg_data            => x_msg_data);
7644 
7645        IF (l_debug=1) THEN
7646             print_debug('Return status from Archive Plan Instance'||x_return_status,l_module_name,9);
7647        END IF;
7648 
7649        l_progress:=130;
7650 
7651        IF (x_return_status=g_ret_sts_error) THEN
7652 
7653           RAISE FND_API.G_EXC_ERROR;
7654 
7655        ELSIF (x_return_status<>g_ret_sts_success) THEN
7656 
7657           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7658 
7659        END IF;
7660 
7661        /*For MMTT.parent_line_id call WMS_OP_RUNTIME_PVT_API.Archive_Dispatched_tasks to Archive WDT*/
7662        l_progress:=140;
7663 
7664        IF (l_debug=1) THEN
7665           print_debug('Calling Archive_Dispatched_tasks with the following parameters',l_module_name,4);
7666           print_debug('p_source_task_id      ==> '||l_parent_source_task_id,l_module_name,4);
7667           print_debug('p_activity_type_id    ==> '||p_activity_type_id,l_module_name,4);
7668           print_debug('p_op_plan_instance_id ==> '||l_operation_instance.op_plan_instance_id,l_module_name,4);
7669           print_debug('p_op_plan_status      ==> '||G_OP_INS_STAT_ABORTED,l_module_name,4);
7670        END IF;
7671 
7672        WMS_OP_RUNTIME_PVT_APIS.archive_dispatched_tasks(
7673         x_return_status        => x_return_status
7674       , x_msg_count            => x_msg_count
7675       , x_msg_data             => x_msg_data
7676       , p_task_id              => NULL
7677       , p_source_task_id       => l_parent_source_task_id
7678       , p_activity_type_id     => p_activity_type_id
7679       , p_op_plan_instance_id  => l_operation_instance.op_plan_instance_id
7680       , p_op_plan_status       => G_OP_INS_STAT_ABORTED);
7681 
7682       IF (l_debug=1) THEN
7683          print_debug('Return Status from Archive_dispatched_tasks is '||x_return_status,l_module_name,9);
7684       END IF;
7685 
7686       l_progress :=150;
7687 
7688       IF (x_return_status=g_ret_sts_error) THEN
7689 
7690           RAISE FND_API.G_EXC_ERROR;
7691 
7692       ELSIF (x_return_status<>g_ret_sts_success) THEN
7693 
7694           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7695 
7696       END IF;
7697 
7698       IF (p_activity_type_id = G_OP_ACTIVITY_INBOUND) THEN
7699 
7700 
7701 	 l_progress:=160;
7702          /* null out the Operation Plan Id ,parent Line Id and delete the Parent transaction temp Id*/
7703          /*Call table hanlder to delete MMTT/MSNT/MTLT for the Parent MMTT*/
7704 
7705 /*	 INV_TRX_UTIL_PUB.Delete_transaction
7706            (x_return_status       => x_return_status,
7707             x_msg_data            => x_msg_data,
7708             x_msg_count           => x_msg_count,
7709             p_transaction_temp_id => l_parent_source_task_id,
7710             p_update_parent       => FALSE);
7711 
7712         IF (l_debug=1) THEN
7713             print_debug('Return status from Delete Transaction'||x_return_status,l_module_name,9);
7714         END IF;
7715 
7716         IF (x_return_status=g_ret_sts_error) THEN
7717 
7718           RAISE FND_API.G_EXC_ERROR;
7719 
7720         ELSIF (x_return_status<>g_ret_sts_success) THEN
7721 
7722           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7723 
7724         END IF;
7725 
7726         l_progress:=170;
7727 
7728         IF (l_debug=1) THEN
7729            print_debug('Updating parent Line id and Operation plan id for Src Document record',l_module_name,9);
7730 	   print_debug('Also update destination sub/loc Src Document record to that of the original task: l_plan_orig_dest_sub = '||l_plan_orig_dest_sub || '  l_plan_orig_dest_loc_id = ' || l_plan_orig_dest_loc_id,l_module_name,9);
7731 	   print_debug('l_inbound_doc.locator_id = '|| l_inbound_doc.locator_id,l_module_name,9);
7732     	   print_debug('l_inbound_doc.locator_id = '|| l_inbound_doc.locator_id,l_module_name,9);
7733     	   print_debug('l_inbound_doc.transfer_to_location = '|| l_inbound_doc.transfer_to_location,l_module_name,9);
7734 	END IF;
7735 
7736 	IF l_inbound_doc.locator_id IS NULL THEN
7737 	   UPDATE MTL_MATERIAL_TRANSACTIONS_TEMP
7738 	     SET operation_plan_id  = NULL ,
7739 	     parent_line_id     = NULL,
7740 	     subinventory_code  = l_plan_orig_dest_sub,
7741 	     locator_id         = l_plan_orig_dest_loc_id
7742 	     WHERE transaction_temp_id = p_source_task_id;
7743 	 ELSIF  l_inbound_doc.transfer_to_location IS NULL THEN
7744 	   UPDATE MTL_MATERIAL_TRANSACTIONS_TEMP
7745 	     SET operation_plan_id  = NULL ,
7746 	     parent_line_id     = NULL,
7747 	     transfer_subinventory  = l_plan_orig_dest_sub,
7748 	     transfer_to_location         = l_plan_orig_dest_loc_id
7749 	     WHERE transaction_temp_id = p_source_task_id;
7750 	END IF;
7751 
7752 
7753        l_progress := 180;
7754 	     */
7755 
7756 	     l_document_rec.transaction_temp_id := p_source_task_id;
7757 	   l_document_rec.parent_line_id := l_inbound_doc.parent_line_id;
7758 	   l_document_rec.locator_id := l_inbound_doc.locator_id;
7759 	   l_document_rec.transfer_to_location := l_inbound_doc.transfer_to_location;
7760 	   l_document_rec.organization_id := l_inbound_doc.organization_id;
7761 	   l_document_rec.inventory_item_id := l_inbound_doc.inventory_item_id;
7762 	   l_document_rec.primary_quantity := l_inbound_doc.primary_quantity;
7763 
7764 	   IF l_debug=1 THEN
7765 	      print_debug('Before calling wms_op_inbound_pvt.ABORT with following parameters:',l_module_name,4);
7766 	      print_debug('l_document_rec.transaction_temp_id => '|| l_document_rec.transaction_temp_id,l_module_name,4);
7767 	      print_debug('l_document_rec.parent_line_id => '||l_document_rec.parent_line_id,l_module_name,4);
7768 	      print_debug('l_document_rec.locator_id => '||l_document_rec.locator_id,l_module_name,4);
7769 	      print_debug('l_document_rec.transfer_to_location => '||l_document_rec.transfer_to_location,l_module_name,4);
7770 	      print_debug('p_plan_orig_sub_code => '||l_plan_orig_dest_sub,l_module_name,4);
7771 	      print_debug('p_plan_orig_loc_id => '||l_plan_orig_dest_loc_id,l_module_name,4);
7772 	      IF(p_for_manual_drop = TRUE) THEN
7773 		 print_debug('p_for_manual_drop => TRUE',l_module_name, 4);
7774 	       ELSE
7775 		 print_debug('p_for_manual_drop => FALSE',l_module_name,4);
7776 	      END IF;
7777 
7778 	   END IF;
7779 
7780 
7781 	   wms_op_inbound_pvt.ABORT
7782 	    (x_return_status         => l_return_status,
7783 	     x_msg_data              => l_msg_data,
7784 	     x_msg_count             => l_msg_count,
7785 	     p_document_rec          => l_document_rec,
7786 	     p_plan_orig_sub_code    => l_plan_orig_dest_sub,
7787 	     p_plan_orig_loc_id      => l_plan_orig_dest_loc_id,
7788 	     p_for_manual_drop       => p_for_manual_drop
7789 	     );
7790 
7791 	   IF l_debug=1 THEN
7792 	      print_debug('After calling wms_op_inbound_pvt.ABORT:',l_module_name,4);
7793 	      print_debug('x_return_status => '||l_return_status,l_module_name,4);
7794 	      print_debug('x_msg_data => '||l_msg_data,l_module_name,4);
7795 	      print_debug('x_msg_count => '||l_msg_count,l_module_name,4);
7796 	   END IF;
7797 
7798 	   IF l_return_status <> FND_API.g_ret_sts_success THEN
7799 	     IF (l_debug=1) THEN
7800 		print_debug(' wms_op_inbound_pvt.ABORT finished with error. l_return_status = ' || l_return_status,l_module_name,4);
7801 	     END IF;
7802 
7803 	     RAISE FND_API.G_EXC_ERROR;
7804 	  END IF;
7805 
7806        IF l_debug=1 THEN
7807           print_debug('Need to update the WDT /WDTH records nulling out operation Plan id',l_module_name,9);
7808           print_debug(' and also op_plan_instance_id ',l_module_name,9);
7809        END IF;
7810 
7811        -- Do not nul out operation_plan_id and op_plan_instance_id
7812        -- since DBI uses these columns for exception related KPI
7813        -- It is ok for WMS because without parent_transaction_id
7814        -- the aborted WDTH is still viewed as an independent task
7815        -- from control board.
7816        UPDATE wms_dispatched_tasks_history
7817 	 SET parent_transaction_id = NULL
7818 	 WHERE transaction_id = p_source_task_id;
7819 
7820 /*       UPDATE wms_dispatched_tasks
7821           SET operation_plan_id = NULL,
7822               op_plan_instance_id = NULL
7823        WHERE transaction_temp_id = p_source_task_id;
7824 
7825        l_progress := 190;
7826 
7827        IF SQL%ROWCOUNT=0 THEN
7828 
7829           IF l_debug =1 THEN
7830              print_debug('Could be an archived record,hence updating WDT',l_module_name,9);
7831           END IF;
7832 
7833           l_progress :=200;
7834 
7835           UPDATE wms_dispatched_tasks_history
7836              SET operation_plan_id     = NULL,
7837                  op_plan_instance_id   = NULL,
7838                  parent_transaction_id = NULL
7839           WHERE transaction_id = p_source_task_id;
7840 
7841 	END IF;
7842 */
7843 
7844       END IF;
7845 
7846       /*Need for calling a document handler or not..mostly not*/
7847 
7848    EXCEPTION
7849 
7850         WHEN FND_API.G_EXC_ERROR THEN
7851 
7852           IF (l_debug=1) THEN
7853              print_debug('Expected Error Obtained at'||l_progress,l_module_name,1);
7854           END IF;
7855 
7856           IF c_inbound_doc%ISOPEN THEN
7857              CLOSE c_inbound_doc;
7858           END IF;
7859 
7860           IF c_operation_instance%ISOPEN THEN
7861              CLOSE c_operation_instance;
7862           END IF;
7863 
7864           IF c_plan_instance%ISOPEN THEN
7865              CLOSE c_plan_instance;
7866           END IF;
7867 
7868           ROLLBACK TO abort_plan_sp;
7869 
7870           x_return_status:=g_ret_sts_error;
7871 
7872        WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
7873 
7874           IF (l_debug=1) THEN
7875              print_debug('UnExpected Error Obtained at'||l_progress||SQLERRM,l_module_name,1);
7876           END IF;
7877 
7878           IF (l_debug=1) THEN
7879              print_debug('Expected Error Obtained at'||l_progress,l_module_name,1);
7880           END IF;
7881 
7882           IF c_inbound_doc%ISOPEN THEN
7883              CLOSE c_inbound_doc;
7884           END IF;
7885 
7886           IF c_operation_instance%ISOPEN THEN
7887              CLOSE c_operation_instance;
7888           END IF;
7889 
7890           IF c_plan_instance%ISOPEN THEN
7891              CLOSE c_plan_instance;
7892           END IF;
7893 
7894           ROLLBACK TO abort_plan_sp;
7895           x_return_status:=g_ret_sts_unexp_error;
7896 
7897        WHEN OTHERS THEN
7898 
7899           IF (l_debug=1) THEN
7900             print_debug(l_progress||' '||SQLERRM, l_module_name,1);
7901           END IF;
7902 
7903           IF fnd_msg_pub.check_msg_level(g_msg_lvl_unexp_error) THEN
7904             fnd_msg_pub.add_exc_msg(g_pkg_name, l_module_name, SQLERRM);
7905           END IF; /* fnd_msg.... */
7906 
7907           fnd_msg_pub.count_and_get(
7908                                  p_count => x_msg_count,
7909                                  p_data  => x_msg_data
7910                                 );
7911 
7912           IF (SQLCODE<-20000) THEN
7913             IF (l_debug=1) THEN
7914               print_debug('This is a user defined exception',l_module_name,1);
7915             END IF;
7916 
7917             x_error_code:=-(SQLCODE+20000);
7918 
7919             x_return_status:=g_ret_sts_error;
7920 
7921 
7922           ELSE
7923 
7924             x_return_status := g_ret_sts_unexp_error;
7925 
7926           END IF;
7927 
7928           IF c_inbound_doc%ISOPEN THEN
7929              CLOSE c_inbound_doc;
7930           END IF;
7931 
7932           IF c_operation_instance%ISOPEN THEN
7933              CLOSE c_operation_instance;
7934           END IF;
7935 
7936           IF c_plan_instance%ISOPEN THEN
7937              CLOSE c_plan_instance;
7938           END IF;
7939 
7940 
7941          ROLLBACK TO abort_plan_sp;
7942 
7943 
7944    END Abort_Operation_Plan;
7945 
7946 
7947  /**
7948   *   Rollback_operation_Plan
7949   *   <p>This procedure rollsback the operation plan(s).This API is called for a Single Step Drop
7950   *      Operation and it rollsback to Load Pending.</p>
7951   *
7952   *  @param x_return_status    Return Status
7953   *  @param x_msg_data         Returns the Message Data
7954   *  @param x_msg_count        Returns the Error Message
7955   *  @param x_error_code       Returns appropriate error code in case of an error .
7956   *  @param p_source_task_id   Identifier of the source document record
7957   *  @param p_activity_type_id Lookup code of the Activity type Id
7958   **/
7959    PROCEDURE  Rollback_Operation_Plan(
7960       x_return_status     OUT  NOCOPY  VARCHAR2,
7961       x_msg_data          OUT  NOCOPY  FND_NEW_MESSAGES.MESSAGE_TEXT%TYPE,
7962       x_msg_count         OUT  NOCOPY  NUMBER,
7963       x_error_code        OUT  NOCOPY  NUMBER,
7964       p_source_task_id    IN           NUMBER,
7965       p_activity_type_id  IN           NUMBER ) IS
7966 
7967 
7968       CURSOR c_wdt_details IS
7969          SELECT status,
7970                 task_id
7971          FROM wms_dispatched_tasks
7972          WHERE transaction_temp_id = p_source_task_id;
7973 
7974       CURSOR c_inbound_document_details IS
7975          SELECT operation_plan_id
7976          FROM mtl_material_transactions_temp
7977          WHERE transaction_temp_id = p_source_task_id;
7978 
7979 
7980       CURSOR c_operation_instances IS
7981          SELECT operation_instance_id,
7982                 op_plan_instance_id,
7983                 operation_type_id,
7984                 operation_sequence,
7985                 operation_plan_detail_id,
7986                 operation_status
7987          FROM wms_op_operation_instances
7988          WHERE source_task_id = p_source_task_id
7989          AND activity_type_id = p_activity_type_id
7990          ORDER BY 4;
7991 
7992       CURSOR c_min_operation_seq (v_op_plan_id NUMBER) IS
7993          SELECT MIN(operation_sequence)
7994           FROM wms_op_plan_details
7995          WHERE operation_plan_id = v_op_plan_id;
7996 
7997 
7998 
7999 
8000        l_wdt_details          c_wdt_details%ROWTYPE;
8001        l_inbound_document     c_inbound_document_details%ROWTYPE;
8002        l_wooi_data_rec        c_operation_instances%ROWTYPE;
8003        l_min_opertn_sequence  NUMBER := -1;
8004        l_op_plan_instance_id  NUMBER;
8005        l_wooi_rec             wms_op_operation_instances%ROWTYPE;
8006        l_plan_detail_id       NUMBER;
8007        l_first_opertn_seq     NUMBER;
8008        l_wopi_rec             wms_op_plan_instances%ROWTYPE;
8009        l_op_plan_id           NUMBER;
8010 
8011        l_debug       NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
8012        l_module_name VARCHAR2(30) := 'Rollback_operation_Plan';
8013        l_progress    NUMBER       ;
8014 
8015    BEGIN
8016 
8017          IF (l_debug = 1) THEN
8018              print_DEBUG(' p_source_task_id ==> '||p_source_task_id ,l_module_name,3);
8019              print_DEBUG(' p_activity_type_id ==> '||p_activity_type_id ,l_module_name,3);
8020          END IF;
8021          x_return_status  := g_ret_sts_success;
8022 
8023          SAVEPOINT rollback_op_plan_sp;
8024          l_progress :=10;
8025 
8026          /*If the p_source_task_id is null or p_activity_type_id is null then raise invalid arguement error and
8027           * return by populating appropriate error code.
8028           */
8029          IF p_source_task_id IS NULL OR p_activity_type_id IS NULL THEN
8030 
8031             IF (l_debug=1) THEN
8032                print_debug('Invalid Inputs passed to Rollback Operation Plan '||p_source_task_id||' '||p_activity_type_id,l_module_name,9);
8033             END IF;
8034 
8035             RAISE_APPLICATION_ERROR(INVALID_INPUT,'Invalid inputs passed'||p_source_task_id||' '||p_activity_type_id);
8036 
8037          END IF;
8038 
8039          l_progress:=20;
8040 
8041 
8042          /* Update or remove WDT. Based on the p_source_task_id query WMS_DISPATCHED_TASKS and populate WDT record variable as follows:
8043           * a.	If no record is found in WDT for source_task_id then raise unexpected error and return after populating appropriate error code.
8044           * b.	If WDT.status = 'Dispatched' or 'Loaded' THEN
8045           *      a.	Remove WDT
8046           * End IF; (If Dispatched)
8047           */
8048 
8049          IF (p_activity_type_id =G_OP_ACTIVITY_INBOUND) THEN
8050 
8051             OPEN c_wdt_details;
8052 
8053             FETCH c_wdt_details INTO l_wdt_details;
8054 
8055             IF (c_wdt_details%NOTFOUND) THEN
8056 
8057               IF (l_debug=1) THEN
8058                  print_debug('No WDT record found for source task id '||p_source_task_id,l_module_name,1);
8059               END IF;
8060 
8061               CLOSE c_wdt_details;
8062 
8063               RAISE_APPLICATION_ERROR(TASK_NOT_EXISTS,'Task Record does not exist for source task Id :'||p_source_task_id);
8064 
8065             END IF;
8066 
8067             CLOSE c_wdt_details;
8068 
8069             l_progress:=30;
8070 
8071             IF (l_debug=1) THEN
8072                print_debug('Status of the task record fetched is '||l_wdt_details.status,l_module_name,9);
8073             END IF;
8074 
8075             IF l_wdt_details.status NOT IN (g_task_status_dispatched,g_task_status_loaded) THEN
8076 
8077                IF l_debug=1 THEN
8078                   print_debug('Invalid task record',l_module_name,1);
8079                END IF;
8080 
8081                RAISE_APPLICATION_ERROR(INVALID_TASK,'Invalid Task Record for task Id '||l_wdt_details.task_id||' and status '||l_wdt_details.status);
8082 
8083             END IF;
8084 
8085             l_progress := 40;
8086 
8087 
8088 	    -- Moved delete WDT to after WMS_OP_INBOUND_PVT.Cleanup
8089 
8090 
8091             /* Query the document tables based on p_source_taks_id and p_activity_id and populate the document record.
8092              *   If p_activity_id=INBOUND then
8093              *      Query MMTT (MTLT) where transaction_temp_id is equal p_source_task_id and populate the record variable.
8094              *   End If
8095              */
8096 
8097              OPEN c_inbound_document_details;
8098 
8099              FETCH c_inbound_document_details INTO l_inbound_document;
8100 
8101              IF c_inbound_document_details%NOTFOUND THEN
8102 
8103                 CLOSE c_inbound_document_details;
8104 
8105                 IF l_debug=1 THEN
8106 
8107                    print_debug('Inbound document record does not exist for source task Id :'||p_source_task_id,l_module_name,9);
8108 
8109                 END IF;
8110 
8111                 RAISE_APPLICATION_ERROR(INVALID_DOC_ID,'Invalid Document Record with document Id: '||p_source_task_id);
8112 
8113              END IF;
8114 
8115              CLOSE c_inbound_document_details;
8116 
8117              /*	If Operation_plan_id on MMTT record variable is null then return as Success
8118               *   Non ATF case
8119               */
8120              l_progress:=60;
8121 
8122              IF l_inbound_document.operation_plan_id IS NULL THEN
8123 
8124                 IF (l_debug=1) THEN
8125                    print_debug('Operation PLan Id null on document record:Non ATF case',l_module_name,9);
8126                 END IF;
8127 		/* for bug#5997558 */
8128 		IF p_activity_type_id =G_OP_ACTIVITY_INBOUND THEN
8129 
8130                 WMS_OP_INBOUND_PVT.Cleanup
8131                   (  x_return_status   => x_return_status
8132  	               , x_msg_data         => x_msg_data
8133  	               , x_msg_count        => x_msg_count
8134  	               , p_source_task_id   => p_source_task_id
8135  	              );
8136 
8137                IF l_debug=1 THEN
8138                   print_debug(' Return Status from Cleanup '||x_return_status,l_module_name,9);
8139                END IF;
8140 
8141 
8142                IF (x_return_status=g_ret_sts_error) THEN
8143                     RAISE FND_API.G_EXC_ERROR;
8144 
8145                ELSIF (x_return_status<>g_ret_sts_success) THEN
8146                     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
8147 
8148                END IF;
8149 	       end if ;
8150 
8151 	       /* End for bug#5997558 */
8152 
8153 		-- Before we return, we should delete WDT here
8154 		-- since now we delete WDT after calling WMS_OP_INBOUND_PVT.Cleanup at the end.
8155 
8156 		DELETE FROM WMS_DISPATCHED_TASKS
8157 		  WHERE transaction_temp_id = p_source_task_id
8158 		  AND task_id = l_wdt_details.task_id;
8159 
8160 		IF c_wdt_details%ISOPEN THEN
8161 		   CLOSE c_wdt_details;
8162 		END IF;
8163 
8164 		IF c_inbound_document_details%ISOPEN THEN
8165 		   CLOSE c_inbound_document_details;
8166 		END IF;
8167 
8168 		IF c_operation_instances%ISOPEN THEN
8169 		   CLOSE c_operation_instances;
8170 		END IF;
8171 
8172 		IF c_min_operation_seq%ISOPEN THEN
8173 		   CLOSE c_min_operation_seq;
8174 		END IF;
8175 
8176                 RETURN;
8177 
8178              END IF; /* non ATF case*/
8179 
8180              l_op_plan_id := l_inbound_document.operation_plan_id;
8181 
8182          END IF;/*Inbound*/
8183 
8184              /* Populate the Operation Instance (WOOI) record based on the
8185               * current Active operation for p_source_task_id.
8186               * Populate the Operation Plan Instance (WOPI) Record based on the Operation Plan
8187               * Instance of the operation Instance.
8188               */
8189              l_progress := 70;
8190 
8191              OPEN c_operation_instances;
8192 
8193              LOOP
8194 
8195               FETCH c_operation_instances INTO l_wooi_data_rec;
8196 
8197               EXIT WHEN c_operation_instances%NOTFOUND;
8198 
8199               IF l_wooi_data_rec.operation_type_id = G_OP_TYPE_LOAD AND l_wooi_data_rec.operation_status = G_OP_INS_STAT_COMPLETED THEN
8200 
8201                /* Call WMS_OP_RUNTIME_APIS_PVT.UPDATE_OPERATION_INSTANCE
8202                 * to update the Operation Instance populated in WOOI record
8203                 * tp statsu Pending
8204                 */
8205 
8206                  l_wooi_rec.operation_instance_id := l_wooi_data_rec.operation_instance_id;
8207                  l_wooi_rec.operation_status      := G_OP_INS_STAT_PENDING;
8208 
8209 
8210                 IF (l_debug=1) THEN
8211                    print_debug('Load Operation is Completed,hence updating the Operation Instance Record tp Pending',l_module_name,9);
8212                    print_debug('CAlling Update_Operation_instance with the Operation Instance Id as '||l_wooi_rec.operation_instance_id,l_module_name,9);
8213                    print_debug('Operation status as :'||l_wooi_rec.operation_status,l_module_name,9);
8214                 END IF;
8215 
8216                 l_progress := 80;
8217 
8218                 WMS_OP_RUNTIME_PVT_APIS.UPDATE_OPERATION_INSTANCE
8219                    (p_update_rec    => l_wooi_rec,
8220                     x_return_status => x_return_status,
8221                     x_msg_count     => x_msg_count,
8222                     x_msg_data      => x_msg_data);
8223 
8224                 IF (l_debug = 1) THEN
8225                    print_debug('Return Status from Update Operation Instance is '||x_return_status,l_module_name,9);
8226                 END IF;
8227 
8228                 IF (x_return_status=g_ret_sts_error) THEN
8229                    RAISE FND_API.G_EXC_ERROR;
8230 
8231                 ELSIF (x_return_status<>g_ret_sts_success) THEN
8232                   RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
8233 
8234                 END IF;
8235 
8236                 l_op_plan_instance_id := l_wooi_data_rec.op_plan_instance_id;
8237 
8238 
8239                 /* Fetching the minimum sequence of the Operation Plan Detail to check later if it is the
8240                  * first operation
8241                  */
8242 
8243                 l_min_opertn_sequence := l_wooi_data_rec.operation_sequence;
8244 
8245                 l_plan_detail_id := l_wooi_data_rec.operation_plan_detail_id;
8246 
8247 
8248               END IF;/* Operation is Load */
8249 
8250               l_progress := 90;
8251 
8252               IF l_wooi_data_rec.operation_type_id = G_OP_TYPE_DROP AND l_wooi_data_rec.operation_status IN (G_OP_INS_STAT_PENDING,G_OP_INS_STAT_ACTIVE)  THEN
8253 
8254                  IF (l_debug=1) THEN
8255                     print_debug('Operation is Drop,hence deleting the Operation Instance',l_module_name,9);
8256                  END IF;
8257 
8258                  /*Checking if operation Plan Instances are the same on both the Operation instance records
8259                   * Ideally this condition should never occur,checking for data corruption
8260                   */
8261 
8262                  IF l_op_plan_instance_id IS NOT NULL AND l_op_plan_instance_id <> l_wooi_data_rec.op_plan_instance_id THEN
8263 
8264                     IF (l_debug=1) THEN
8265                        print_debug('Invalid condition where the Operation plan Instances are different for the same source_task id',l_module_name,1);
8266                     END IF;
8267 
8268                     RAISE_APPLICATION_ERROR(DATA_INCONSISTENT,'Operation plan Instance different for the same Source task id in different Operation Instances');
8269 
8270                 END IF;
8271 
8272                 l_op_plan_instance_id := l_wooi_data_rec.op_plan_instance_id;
8273 
8274                 l_progress := 100;
8275 
8276                 /* Checking if the Operation Sequnce of Load is greater than that of Drop
8277                  * Ideally this case should never occur.Indication of data corruption
8278                  */
8279 
8280                 IF (l_min_opertn_sequence > l_wooi_data_rec.operation_sequence) THEN
8281 
8282                    IF (l_debug=1) THEN
8283                       print_debug('Operation sequence of Load greater than that of drop for the same source task id',l_module_name,9);
8284                    END IF;
8285 
8286                    RAISE_APPLICATION_ERROR(DATA_INCONSISTENT,'Operation Sequence of Load Operation greater than that of Drop');
8287                 END IF;
8288 
8289                 l_progress := 110;
8290 
8291                 IF (l_debug=1) THEN
8292                    print_debug('Calling Deleting Operation Instance with operation Instance Id'||l_wooi_data_rec.operation_instance_id,l_module_name,9);
8293                 END IF;
8294 
8295                 WMS_OP_RUNTIME_PVT_APIS.DELETE_OPERATION_INSTANCE
8296                    ( p_operation_instance_id => l_wooi_data_rec.operation_instance_id
8297                     ,x_return_status => x_return_status
8298                     ,x_msg_count     => x_msg_count
8299                     ,x_msg_data      => x_msg_data);
8300 
8301                 l_progress:=120;
8302 
8303                 IF l_debug=1 THEN
8304                    print_debug('Return Status from Delete Operation Instance '||x_return_status,l_module_name,9);
8305                 END IF;
8306 
8307                 IF (x_return_status=g_ret_sts_error) THEN
8308                    RAISE FND_API.G_EXC_ERROR;
8309 
8310                 ELSIF (x_return_status<>g_ret_sts_success) THEN
8311                   RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
8312 
8313                 END IF;
8314 
8315               END IF; /*Operation Type Drop*/
8316 
8317              END LOOP;
8318 
8319              l_progress:=130;
8320 
8321              /* If the above fetched operation is the first in the Plan then
8322               * Call WMS_OP_RUNTIME_APIS_PVT.UPDATE_PLAN_INSTANCE to set the plan status as Pending.
8323 				  */
8324               OPEN c_min_operation_seq(l_op_plan_id);
8325 
8326               FETCH c_min_operation_seq INTO l_first_opertn_seq;
8327 
8328               IF (c_min_operation_seq%NOTFOUND) THEN
8329 
8330                  IF (l_debug=1) THEN
8331                     print_debug('NO Operation Detail Record found',l_module_name,9);
8332                  END IF;
8333 
8334                  CLOSE c_min_operation_seq;
8335 
8336                  RAISE_APPLICATION_ERROR(INVALID_PLAN_ID,'Invalid Plan Id stamped on document record');
8337 
8338               END IF;
8339 
8340               CLOSE c_min_operation_seq;
8341 
8342               l_progress := 140;
8343 
8344               IF (l_first_opertn_seq = l_min_opertn_sequence) THEN
8345 
8346                  IF (l_debug=1) THEN
8347                     print_debug('It is the first Operation in the Plan,hence update status to pending',l_module_name,9);
8348                  END IF;
8349 
8350                  l_wopi_rec.op_plan_instance_id := l_op_plan_instance_id;
8351 
8352                  l_wopi_rec.status              := G_OP_INS_STAT_PENDING;
8353 
8354                  l_progress :=150;
8355 
8356                  IF l_debug=1 THEN
8357                     print_debug('Calling WMS_OP_RUNTIME_APIS_PVT.UPDATE_PLAN_INSTANCE with the following parameters',l_module_name,9);
8358                     print_debug('Op_plan_instance_id => '||l_wopi_rec.op_plan_instance_id,l_module_name,9);
8359                     print_debug('Status              => '||l_wopi_rec.status,l_module_name,9);
8360                  END IF;
8361 
8362                  WMS_OP_RUNTIME_PVT_APIS.UPDATE_PLAN_INSTANCE
8363                    (p_update_rec    => l_wopi_rec,
8364                     x_return_status => x_return_status,
8365                     x_msg_count     => x_msg_count,
8366                     x_msg_data      => x_msg_data);
8367 
8368                  IF (l_debug =1) THEN
8369                     print_debug('Return Status from Update Plan Instance'||x_return_status,l_module_name,9);
8370                  END IF;
8371 
8372                  IF (x_return_status=g_ret_sts_error) THEN
8373                    RAISE FND_API.G_EXC_ERROR;
8374 
8375                  ELSIF (x_return_status<>g_ret_sts_success) THEN
8376                    RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
8377 
8378                  END IF;
8379 
8380               END IF;/*First Operation in Plan*/
8381 
8382               /*If p_activity_id =INBOUND then
8383                *  Call WMS_OP_INBOUND_PVT.Cleanup
8384                *End If;
8385                */
8386               l_progress :=160;
8387              IF p_activity_type_id =G_OP_ACTIVITY_INBOUND THEN
8388 
8389                 WMS_OP_INBOUND_PVT.Cleanup
8390                   (  x_return_status   => x_return_status
8391  	               , x_msg_data         => x_msg_data
8392  	               , x_msg_count        => x_msg_count
8393  	               , p_source_task_id   => p_source_task_id
8394  	              );
8395 
8396                IF l_debug=1 THEN
8397                   print_debug(' Return Status from Cleanup '||x_return_status,l_module_name,9);
8398                END IF;
8399 
8400 
8401                IF (x_return_status=g_ret_sts_error) THEN
8402                     RAISE FND_API.G_EXC_ERROR;
8403 
8404                ELSIF (x_return_status<>g_ret_sts_success) THEN
8405                     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
8406 
8407                END IF;
8408 
8409 
8410 	       /* Deleting the WDT record as the status is being reverted to Load Pending */
8411 	       -- Moved delete WDT to after calling  WMS_OP_INBOUND_PVT.Cleanup
8412 	       -- because WMS_OP_INBOUND_PVT.Cleanup now calls putaway_cleanup, which assumes WDT still exists.
8413 
8414 	       DELETE FROM WMS_DISPATCHED_TASKS
8415 		 WHERE transaction_temp_id = p_source_task_id
8416 		 AND task_id = l_wdt_details.task_id;
8417 
8418 	       l_progress := 170;
8419 
8420              END IF;/*Activity is Inbound*/
8421 
8422    EXCEPTION
8423 
8424       WHEN FND_API.G_EXC_ERROR THEN
8425 
8426          IF (l_debug=1) THEN
8427             print_debug('Expected Error Obtained at'||l_progress,l_module_name,1);
8428          END IF;
8429 
8430          IF c_wdt_details%ISOPEN THEN
8431             CLOSE c_wdt_details;
8432          END IF;
8433 
8434          IF c_inbound_document_details%ISOPEN THEN
8435             CLOSE c_inbound_document_details;
8436          END IF;
8437 
8438          IF c_operation_instances%ISOPEN THEN
8439             CLOSE c_operation_instances;
8440          END IF;
8441 
8442          IF c_min_operation_seq%ISOPEN THEN
8443             CLOSE c_min_operation_seq;
8444          END IF;
8445 
8446          x_return_status := g_ret_sts_error;
8447 
8448          ROLLBACK TO rollback_op_plan_sp;
8449 
8450       WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8451 
8452          IF (l_debug=1) THEN
8453             print_debug('Expected Error Obtained at'||l_progress,l_module_name,1);
8454          END IF;
8455 
8456          IF c_wdt_details%ISOPEN THEN
8457             CLOSE c_wdt_details;
8458          END IF;
8459 
8460          IF c_inbound_document_details%ISOPEN THEN
8461             CLOSE c_inbound_document_details;
8462          END IF;
8463 
8464          IF c_operation_instances%ISOPEN THEN
8465             CLOSE c_operation_instances;
8466          END IF;
8467 
8468          IF c_min_operation_seq%ISOPEN THEN
8469             CLOSE c_min_operation_seq;
8470          END IF;
8471 
8472          x_return_status := g_ret_sts_unexp_error;
8473 
8474          ROLLBACK TO rollback_op_plan_sp;
8475 
8476       WHEN OTHERS THEN
8477 
8478             print_debug(SQLERRM, l_module_name,1);
8479             IF (l_debug=1) THEN
8480               print_debug(l_progress||' '||SQLERRM, l_module_name,1);
8481             END IF;
8482 
8483             IF fnd_msg_pub.check_msg_level(g_msg_lvl_unexp_error) THEN
8484               fnd_msg_pub.add_exc_msg(g_pkg_name, l_module_name, SQLERRM);
8485             END IF; /* fnd_msg.... */
8486 
8487             fnd_msg_pub.count_and_get(
8488                                    p_count => x_msg_count,
8489                                    p_data  => x_msg_data
8490                                   );
8491 
8492             IF (SQLCODE<-20000) THEN
8493               IF (l_debug=1) THEN
8494                 print_debug('This is a user defined exception',l_module_name,1);
8495               END IF;
8496 
8497               x_error_code:=-(SQLCODE+20000);
8498 
8499               x_return_status:=g_ret_sts_error;
8500 
8501 
8502             ELSE
8503 
8504               x_return_status := g_ret_sts_unexp_error;
8505 
8506             END IF;
8507 
8508             IF c_wdt_details%ISOPEN THEN
8509               CLOSE c_wdt_details;
8510            END IF;
8511 
8512            IF c_inbound_document_details%ISOPEN THEN
8513               CLOSE c_inbound_document_details;
8514            END IF;
8515 
8516            IF c_operation_instances%ISOPEN THEN
8517               CLOSE c_operation_instances;
8518            END IF;
8519 
8520            IF c_min_operation_seq%ISOPEN THEN
8521               CLOSE c_min_operation_seq;
8522            END IF;
8523 
8524 
8525             ROLLBACK TO rollback_op_plan_sp;
8526 
8527    END Rollback_Operation_Plan;
8528 
8529 
8530 /**
8531   *   Check_Plan_Status
8532   *   <p>This procedure returns the operation plan instance
8533   *   status for a given source task ID and activity.</p>
8534   *  @param x_return_status    Return Status
8535   *  @param x_msg_data         Returns the Message Data
8536   *  @param x_msg_count        Returns the Error Message
8537   *  @param x_error_code       Returns appropriate error code in case
8538   *                            of an error.
8539   *  @param x_plan_status      Returns the Status of the Plan
8540   *  @param p_source_task_id   Identifier of the source document record
8541   *  @param p_activity_type_id Lookup code of the Activity type Id
8542   **/
8543    PROCEDURE  Check_Plan_Status(
8544               x_return_status    OUT  NOCOPY  VARCHAR2,
8545               x_msg_data         OUT  NOCOPY  FND_NEW_MESSAGES.MESSAGE_TEXT%TYPE,
8546               x_msg_count        OUT  NOCOPY  NUMBER,
8547               x_error_code       OUT  NOCOPY  NUMBER,
8548               x_plan_status      OUT  NOCOPY  NUMBER,
8549               p_source_task_id   IN           NUMBER,
8550               p_activity_type_id IN           NUMBER
8551                                ) IS
8552 
8553       l_debug       NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'),0);
8554       l_module_name VARCHAR2(30) := 'Check_Plan_Status';
8555       l_progress    VARCHAR2(30);
8556 
8557       l_status      NUMBER := -1;
8558       l_plan_id     NUMBER := -1;
8559 
8560       CURSOR source_plan IS
8561       SELECT mmtt.operation_plan_id
8562       FROM   mtl_material_transactions_temp mmtt
8563       WHERE  mmtt.transaction_temp_id = p_source_task_id;
8564 
8565       CURSOR plan_status IS
8566       SELECT wopi.status
8567       FROM   wms_op_plan_instances wopi
8568       WHERE  wopi.activity_type_id = p_activity_type_id
8569       AND    wopi.source_task_id = p_source_task_id;
8570 
8571       CURSOR op_plan_status IS
8572        SELECT wopi.status
8573          FROM wms_op_plan_instances wopi
8574         WHERE wopi.op_plan_instance_id = (SELECT op_plan_instance_id
8575                                           FROM wms_op_operation_instances wooi
8576                                           WHERE wooi.source_task_id = p_source_task_id
8577                                           AND wooi.activity_type_id = p_activity_type_id
8578                                           AND ROWNUM =1);
8579 
8580 
8581    BEGIN
8582 
8583          l_progress := '10';
8584 
8585          IF (l_debug = 1) THEN
8586              print_debug(' p_source_task_id ==> '||p_source_task_id ,
8587                            l_module_name,3);
8588              print_debug(' p_activity_type_id ==> '||p_activity_type_id ,
8589                            l_module_name,3);
8590          END IF;
8591 
8592          l_progress := '20';
8593          /*
8594           * The activity type and the the source task cannot be null
8595           * Raise Illegal Argument Exception
8596           */
8597 
8598          IF (p_source_task_id IS NULL OR p_activity_type_id IS NULL) THEN
8599 
8600             /*RAISE fnd_api.g_exc_error;*/
8601             RAISE_APPLICATION_ERROR(INVALID_INPUT,'Invalid inputs passed'||p_source_task_id||p_activity_type_id);
8602 
8603          END IF; /* p_source_task_id IS...*/
8604 
8605          l_progress := '30';
8606 
8607          /*
8608           * If the activity type is Inbound then the source task type Id
8609           * corresponds to a MMTT record.
8610           *
8611           * This API is called from CREATE_SUGGESTIONS.. and it is possible
8612           * that is called for MMTT records that do not have an operation
8613           * plan
8614           * In such a case we should return a success and plan status
8615           * should be NULL
8616           *
8617           * If a plan exists then find out the plan status
8618           */
8619          IF p_activity_type_id = g_op_activity_inbound THEN
8620 
8621             OPEN  source_plan;
8622             FETCH source_plan INTO l_plan_id;
8623             CLOSE source_plan;
8624 
8625             l_progress := '40';
8626 
8627            /*
8628             * l_plan_id has a value of -1 if the cursor failed to fetch
8629             * any rows, this means that there is no MMTT corresponding
8630             * to the passed document Id,i.e. The document Id passed is
8631             * invalid.
8632             *
8633             * In this case Raise Illegal Argument Error.
8634             */
8635 
8636             IF l_plan_id = -1 THEN
8637 
8638                /*RAISE fnd_api.g_exc_error;*/
8639                RAISE_APPLICATION_ERROR(INVALID_DOC_ID,'Document Record does not exist for Source_task_id'||p_source_task_id);
8640 
8641             END IF; /* l_plan_id = -1 */
8642 
8643             l_progress := '50';
8644 
8645            /*
8646             * MMTT exists but plan id is NULL
8647             */
8648             IF l_plan_id IS NULL THEN
8649 
8650                l_status := NULL;
8651                GOTO success;
8652 
8653             END IF; /* l_plan_id IS NULL*/
8654 
8655          END IF; /* p_activity...*/
8656 
8657          l_progress := '60';
8658          /*
8659           * The assumption being made here is that the source task id
8660           * passed is the parent document id.
8661           */
8662 
8663          OPEN plan_status;
8664          FETCH plan_status INTO l_status;
8665          CLOSE plan_status;
8666 
8667          l_progress := '70';
8668 
8669          /*
8670           * l_status has a value of -1 if the cursor failed to fetch
8671           * any rows, this means that there is no plan corresponding
8672           * to the passed document Id,therefore need to check the
8673           * if the document Id is a part of Operation Instance,then
8674           * fetch plan status for the Operation Plan Instance correspomding
8675           * to the Operation
8676           * else
8677           *   Raise Expected Error.
8678           */
8679          IF l_status = -1 THEN
8680 
8681             OPEN op_plan_status;
8682 
8683             FETCH op_plan_status INTO l_status;
8684 
8685             CLOSE op_plan_status;
8686 
8687             IF l_status=-1 THEN
8688                /* Operation Instance does not exist*/
8689 
8690                RAISE_APPLICATION_ERROR(OPERATION_INSTANCE_NOT_EXISTS,'Operation instance does not exist for Source task Id'||p_source_task_id);
8691             END IF;
8692 
8693             l_progress :=75;
8694 
8695          END IF; /* l_status = -1 */
8696 
8697          l_progress := '80';
8698 
8699          <<SUCCESS>>
8700 
8701          x_return_status  := g_ret_sts_success;
8702          x_plan_status    := l_status;
8703          fnd_msg_pub.count_and_get(
8704                                     p_count => x_msg_count,
8705                                     p_data  => x_msg_data
8706                                   );
8707 
8708    EXCEPTION
8709 
8710          /*WHEN FND_API.G_EXC_ERROR THEN
8711             print_debug(l_progress||' '||SQLERRM, l_module_name,1);
8712 
8713             IF fnd_msg_pub.check_msg_level(g_msg_lvl_error) THEN
8714                fnd_msg_pub.add_exc_msg(g_pkg_name, l_module_name, SQLERRM);
8715             END IF; /* fnd_msg.... */
8716 
8717             /*fnd_msg_pub.count_and_get(
8718                                        p_count => x_msg_count,
8719                                        p_data  => x_msg_data
8720                                       );
8721             x_error_code := SQLCODE;
8722             x_return_status := g_ret_sts_error;*/
8723 
8724          WHEN OTHERS THEN
8725 
8726             print_debug(l_progress||' '||SQLERRM, l_module_name,1);
8727 
8728             IF fnd_msg_pub.check_msg_level(g_msg_lvl_unexp_error) THEN
8729                fnd_msg_pub.add_exc_msg(g_pkg_name, l_module_name, SQLERRM);
8730             END IF; /* fnd_msg.... */
8731 
8732             fnd_msg_pub.count_and_get(
8733                                        p_count => x_msg_count,
8734                                        p_data  => x_msg_data
8735                                       );
8736             IF (SQLCODE<-20000) THEN
8737                IF (l_debug=1) THEN
8738                  print_debug('This is a user defined exception',l_module_name,1);
8739                END IF;
8740 
8741                x_error_code:=-(SQLCODE+20000);
8742 
8743                x_return_status:=g_ret_sts_error;
8744 
8745 
8746             ELSE
8747 
8748                x_return_status := g_ret_sts_unexp_error;
8749             END IF;
8750 
8751    END Check_Plan_Status;
8752 
8753 
8754 
8755 
8756 END WMS_ATF_RUNTIME_PUB_APIS;