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.18 2008/04/29 14:02:58 abaid 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.18 2008/04/29 14:02:58 abaid 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   BEGIN
1773 
1774       IF (l_debug = 1) THEN
1775           print_debug(' p_source_task_id ==> '||p_source_task_id ,l_module_name,3);
1776           print_debug(' p_activity_id    ==> '||p_activity_id,l_module_name,3);
1777           print_debug(' p_operation_type_id ==>'||p_operation_type_id,l_module_name,3);
1778 
1779       END IF;
1780 
1781       x_return_status  := g_ret_sts_success;
1782       l_progress:=10;
1783 
1784       SAVEPOINT activate_op_sp;
1785 
1786       l_operation_type_id := p_operation_type_id;
1787 
1788       IF (p_source_task_id IS NULL OR p_activity_id IS NULL) THEN
1789           IF (l_debug=1) THEN
1790              print_debug('Source task Id is null',l_module_name,1);
1791           END IF;
1792 
1793           /*Raise Invalid Arguement exception*/
1794           --x_error_code:=1;
1795           --RAISE FND_API.G_EXC_ERROR;
1796           raise_application_error(INVALID_INPUT,'Invalid inputs passed'||p_source_task_id||' '||p_activity_id);
1797 
1798        END IF;
1799 
1800        IF p_activity_id NOT IN (G_OP_ACTIVITY_INBOUND) THEN
1801 
1802         IF (l_debug=1) THEN
1803              print_debug('Invalid value of activity Id',l_module_name,1);
1804           END IF;
1805 
1806           /*Raise Invalid Arguement exception*/
1807           --x_error_code:=1;
1808           --RAISE FND_API.G_EXC_ERROR;
1809           raise_application_error(INVALID_INPUT,'Invalid inputs passed'||p_source_task_id||' '||p_activity_id);
1810        END IF;
1811 
1812        IF (p_activity_id =G_OP_ACTIVITY_INBOUND) THEN
1813 
1814           /* Create or update WDT
1815              This API can be called from 'Load', 'Drop', or 'Inspect' UI.
1816              Only when called from drop, there will be a WDT record with 'Loaded' status and
1817             'putaway' task type.For the other two cases, a new WDT record needs to be created.
1818           */
1819           l_progress:=20;
1820 
1821           OPEN c_wdt_details;
1822 
1823           FETCH c_wdt_details INTO l_wdt_details;
1824 
1825           IF (c_wdt_details%FOUND) THEN
1826 
1827              /*IF WDT.status = 'Dispatched' THEN
1828               *This is to carry over bug fix 2127361
1829               *Delete this WDT record*/
1830              IF (l_wdt_details.status=g_task_status_dispatched) THEN
1831 
1832 
1833                 DELETE FROM wms_dispatched_tasks
1834                   WHERE transaction_temp_id=p_source_task_id;
1835 
1836                 IF (l_debug=1) THEN
1837                   print_debug('Deleting the WDT record since in Dispatched status',l_module_name,9);
1838                 END IF;
1839 
1840              END IF;
1841 
1842           END IF;/*WDT record exists*/
1843           CLOSE c_wdt_details;
1844 
1845           l_progress:=23;
1846 
1847           /* Setting the operation Type Id*/
1848           IF (l_wdt_details.status=g_task_status_loaded) THEN
1849 
1850              l_operation_type_id :=G_OP_TYPE_DROP;
1851 
1852              IF (l_debug=1) THEN
1853                 print_debug('Operation is Drop',l_module_name,9);
1854              END IF;
1855 
1856           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
1857 
1858              IF (l_debug=1) THEN
1859                 print_debug('Operation is Load',l_module_name,9);
1860              END IF;
1861 
1862              l_operation_type_id := G_OP_TYPE_LOAD;
1863 
1864           ELSE
1865 
1866              l_operation_type_id := p_operation_type_id;
1867 
1868           END IF;
1869 
1870           l_progress:=25;
1871 
1872           IF (l_wdt_details.status IS NULL OR l_wdt_details.status=g_task_status_dispatched) THEN
1873 
1874              /*IF WDT record does not exist OR WDT.status = 'Dispatched' THEN
1875               *Create WDT PL/SQL record with P_Task_Execute_Rec,
1876               *populate P_Source_Task_ID  into transaction_temp_ID, status 'Dispatched'
1877               */
1878              l_progress:=27;
1879 
1880              l_wdt_rec := p_task_execute_rec;
1881 
1882              l_wdt_rec.transaction_temp_id  := p_source_task_id;
1883              l_wdt_rec.status               := g_task_status_dispatched;
1884 
1885              IF (l_wdt_rec.person_id IS NOT NULL OR l_wdt_rec.person_id <>-1) THEN
1886 
1887                 IF (l_debug=1) THEN
1888                   print_debug('Person id is not null ..hence fetching effective dates',l_module_name,4);
1889                 END IF;
1890 
1891                 OPEN c_effective_date(l_wdt_rec.person_id);
1892 
1893                 FETCH c_effective_date INTO l_wdt_rec.effective_start_date,l_wdt_rec.effective_end_date;
1894 
1895                 CLOSE c_effective_date;
1896 
1897                 IF (l_debug=1) THEN
1898                  print_debug('Effective Dates fetched for Person are '||l_wdt_rec.effective_start_date||l_wdt_rec.effective_end_date,l_module_name,4);
1899                 END IF;
1900 
1901              END IF;
1902 
1903              IF l_wdt_rec.effective_start_date IS NULL THEN
1904 
1905                l_wdt_rec.effective_start_date := SYSDATE;
1906                l_wdt_rec.effective_end_date   := SYSDATE;
1907 
1908              END IF;
1909 
1910              l_wdt_rec.dispatched_time      := SYSDATE;
1911 
1912              /*If P_Operation_Type_ID = 'Inspect' Then
1913               *  WDT.task_type = 'Inspect';
1914               *Else
1915               *  WDT.Task_Type = 'putaway';
1916               */
1917              IF (l_operation_type_id=G_OP_TYPE_INSPECT) THEN
1918 
1919                 l_wdt_rec.task_type := g_wms_task_type_inspect;
1920              ELSE
1921                 l_wdt_rec.task_type := g_wms_task_type_putaway;
1922              END IF;
1923 
1924           END IF;/*WDT not existing*/
1925 
1926           l_progress:=30;
1927 
1928 
1929         /* Check operation plan stamped or not.
1930          *  IF P_Activity_type_ID = Inbound THEN
1931          *  Query operation_plan_id from MMTT where transaction_temp_ID = P_Source_Task_ID
1932          *  If operation_plan_ID is NULL
1933          *   Return 'success' This handles the Non-ATF case
1934          */
1935           OPEN c_inbound_doc;
1936 
1937           FETCH c_inbound_doc INTO l_inbound_doc;
1938 
1939           IF (c_inbound_doc%NOTFOUND) THEN
1940              /* This is an Invalid document and hence raise exception*/
1941              IF (l_debug=1) THEN
1942                 print_debug('Invalid Document Record',l_module_name,1);
1943              END IF;
1944 
1945              CLOSE c_inbound_doc;
1946 
1947              RAISE FND_API.G_EXC_ERROR;
1948 
1949           END IF;
1950 
1951           CLOSE c_inbound_doc;
1952 
1953           l_progress:=40;
1954 
1955           IF (l_inbound_doc.operation_plan_id IS NULL) THEN
1956              /*Non ATF case..Return after inserting WDT*/
1957              IF (l_debug=1) THEN
1958                 print_debug('Operation Plan Id null on document record',l_module_name,9);
1959              END IF;
1960 
1961              l_progress:=50;
1962 
1963              IF (l_wdt_rec.transaction_temp_id IS NOT NULL) THEN   /*WDT record exists*/
1964 
1965                 l_wdt_rec.operation_plan_id  := l_inbound_doc.operation_plan_id;
1966                 l_wdt_rec.move_order_line_id := l_inbound_doc.move_order_line_id;
1967 
1968                 IF (l_debug=1) THEN
1969                    print_debug('Calling the table handler to insert WDT records',l_module_name,9);
1970                    print_debug('Calling insert_Dispatched_tasks with the following values for WDT record',l_module_name,4);
1971                    print_debug('transaction_temp_id  ==>'||l_wdt_rec.transaction_temp_id,l_module_name,4);
1972                    print_debug('user_task_type       ==>'||l_wdt_rec.user_task_type,l_module_name,4);
1973                    print_debug('person_id            ==>'||l_wdt_rec.person_id,l_module_name,4);
1974                    print_debug('status               ==>'||l_wdt_rec.status,l_module_name,4);
1975                    print_debug('effective_start_date ==>'||l_wdt_rec.effective_start_date,l_module_name,4);
1976                    print_debug('equipment_id         ==>'||l_wdt_rec.equipment_id,l_module_name,4);
1977                    print_debug('equipment_instance   ==>'||l_wdt_rec.equipment_instance,l_module_name,4);
1978                    print_debug('person_resource_id   ==>'||l_wdt_rec.person_resource_id,l_module_name,4);
1979                    print_debug('machine_resource_id  ==>'||l_wdt_rec.machine_resource_id,l_module_name,4);
1980                    print_debug('loaded_time          ==>'||l_wdt_rec.loaded_time,l_module_name,4);
1981                    print_debug('drop_off_time        ==>'||l_wdt_rec.drop_off_time,l_module_name,4);
1982                    print_debug('last_update_date     ==>'||l_wdt_rec.last_update_date,l_module_name,4);
1983                    print_debug('last_update_by       ==>'||l_wdt_rec.last_updated_by,l_module_name,4);
1984                    print_debug('created_by           ==>'||l_wdt_rec.created_by,l_module_name,4);
1985                    print_debug('creation_date        ==>'||l_wdt_rec.creation_date,l_module_name,4);
1986                    print_debug('priority             ==>'||l_wdt_rec.priority,l_module_name,4);
1987                    print_debug('task_group_id        ==>'||l_wdt_rec.task_group_id,l_module_name,4);
1988                    print_debug('device_id            ==>'||l_wdt_rec.device_id,l_module_name,4);
1989                    print_debug('device_invoked       ==>'||l_wdt_rec.device_invoked,l_module_name,4);
1990                    print_debug('device_request_id    ==>'||l_wdt_rec.device_request_id,l_module_name,4);
1991                    print_debug('move_order_line_id   ==>'||l_wdt_rec.move_order_line_id,l_module_name,4);
1992                    print_debug('task_type            ==>'||l_wdt_rec.task_type,l_module_name,4);
1993                    print_debug('Operation Plan Id    ==>'||l_wdt_rec.operation_plan_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('organization_id      ==>'||l_wdt_rec.organization_id,l_module_name,4);
1996                 END IF;
1997 
1998                 l_progress:=60;
1999 
2000                 WMS_OP_RUNTIME_PVT_APIS.Insert_Dispatched_tasks
2001                 ( p_wdt_rec        => l_wdt_rec,
2002                   p_source_task_id => p_source_task_id,
2003                   x_return_status  => x_return_status,
2004                   x_msg_count      => x_msg_count,
2005                   x_msg_data       => x_msg_data
2006                  );
2007 
2008                 l_progress:=65;
2009 
2010                IF (l_debug=1) THEN
2011                  print_debug('return status afte calling table handler '||x_return_status,l_module_name,9);
2012                END IF;
2013 
2014                IF (x_return_status=g_ret_sts_error) THEN
2015 
2016                  IF (l_debug=1) THEN
2017                    print_debug('Expected error from table handler',l_module_name,1);
2018                  END IF;
2019 
2020                  RAISE FND_API.G_EXC_ERROR;
2021 
2022                ELSIF (x_return_status<>g_ret_sts_success) THEN
2023                  IF (l_debug=1) THEN
2024                    print_debug('Unexpected error fromt table hander',l_module_name,1);
2025                  END IF;
2026                  RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2027 
2028                END IF;
2029 
2030              END IF; /*WDT Record to be inserted*/
2031 
2032              RETURN;
2033           END IF;/*Non ATF Case*/
2034 
2035        END IF;/*Activity Inbound*/
2036 
2037       /* Query WMS_OP_OPERATION_INSTANCES where source_task_ID = P_Source_Task_ID
2038        * with highest operation_sequence; populate this record into PL/SQL record.
2039        * Raise unexpected error if it does not exist, and return appropriate error code.
2040        */
2041        l_progress:=70;
2042 
2043        OPEN c_operation_instance_details;
2044 
2045        FETCH c_operation_instance_details INTO l_opertn_instance_details;
2046 
2047        IF (c_operation_instance_details%NOTFOUND) THEN
2048 
2049           /* This is an invalid condition for Activiate
2050              The cursor would also take care of the following validation:
2051              If the status for this WOOI record is other than 'pending' or 'Active',
2052              raise unexpected error and return error code.*/
2053 
2054           IF (l_debug=1) THEN
2055              print_debug('No operation INstance exists:invalid condition',l_module_name,1);
2056           END IF;
2057 
2058           CLOSE c_operation_instance_details;
2059 
2060           RAISE FND_API.G_EXC_ERROR;
2061 
2062        END IF;
2063        CLOSE c_operation_instance_details;
2064 
2065        IF (l_debug=1) THEN
2066 	  print_debug('l_opertn_instance_details.operation_type_id= '||l_opertn_instance_details.operation_type_id,l_module_name,1);
2067 	  print_debug('l_opertn_instance_details.operation_status = '||l_opertn_instance_details.operation_status,l_module_name,1);
2068 	  print_debug('l_opertn_instance_details.op_plan_instance_id = '||l_opertn_instance_details.op_plan_instance_id,l_module_name,1);
2069 	  print_debug('l_opertn_instance_details.operation_plan_detail_id = '||l_opertn_instance_details.operation_plan_detail_id,l_module_name,1);
2070 	  print_debug('l_opertn_instance_details.operation_instance_id = '||l_opertn_instance_details.operation_instance_id,l_module_name,1);
2071 
2072        END IF;
2073 
2074 
2075        l_progress:=80;
2076 
2077        l_op_plan_instance_id:=l_opertn_instance_details.op_plan_instance_id;
2078 
2079        IF (p_activity_id=G_OP_ACTIVITY_INBOUND) THEN
2080 
2081 
2082          /*Calling the table handler to insert records in WDT*/
2083           IF (l_wdt_rec.transaction_temp_id IS NOT NULL) THEN   /*WDT record needs to be inserted*/
2084              l_progress:=90;
2085 
2086               l_wdt_rec.op_plan_instance_id := l_op_plan_instance_id;
2087               l_wdt_rec.operation_plan_id   := l_inbound_doc.operation_plan_id;
2088               l_wdt_rec.move_order_line_id  := l_inbound_doc.move_order_line_id;
2089 
2090               IF (l_debug=1) THEN
2091                  print_debug('Calling the table handler to insert WDT records',l_module_name,9);
2092                  print_debug('Calling insert_Dispatched_tasks with the following values for WDT record',l_module_name,4);
2093                  print_debug('transaction_temp_id  ==>'||l_wdt_rec.transaction_temp_id,l_module_name,4);
2094                  print_debug('user_task_type       ==>'||l_wdt_rec.user_task_type,l_module_name,4);
2095                  print_debug('person_id            ==>'||l_wdt_rec.person_id,l_module_name,4);
2096                  print_debug('status               ==>'||l_wdt_rec.status,l_module_name,4);
2097                  print_debug('effective_start_date ==>'||l_wdt_rec.effective_start_date,l_module_name,4);
2098                  print_debug('equipment_id         ==>'||l_wdt_rec.equipment_id,l_module_name,4);
2099                  print_debug('equipment_instance   ==>'||l_wdt_rec.equipment_instance,l_module_name,4);
2100                  print_debug('person_resource_id   ==>'||l_wdt_rec.person_resource_id,l_module_name,4);
2101                  print_debug('machine_resource_id  ==>'||l_wdt_rec.machine_resource_id,l_module_name,4);
2102                  print_debug('loaded_time          ==>'||l_wdt_rec.loaded_time,l_module_name,4);
2103                  print_debug('drop_off_time        ==>'||l_wdt_rec.drop_off_time,l_module_name,4);
2104                  print_debug('last_update_date     ==>'||l_wdt_rec.last_update_date,l_module_name,4);
2105                  print_debug('last_update_by       ==>'||l_wdt_rec.last_updated_by,l_module_name,4);
2106                  print_debug('created_by           ==>'||l_wdt_rec.created_by,l_module_name,4);
2107                  print_debug('creation_date        ==>'||l_wdt_rec.creation_date,l_module_name,4);
2108                  print_debug('priority             ==>'||l_wdt_rec.priority,l_module_name,4);
2109                  print_debug('task_group_id        ==>'||l_wdt_rec.task_group_id,l_module_name,4);
2110                  print_debug('device_id            ==>'||l_wdt_rec.device_id,l_module_name,4);
2111                  print_debug('device_invoked       ==>'||l_wdt_rec.device_invoked,l_module_name,4);
2112                  print_debug('device_request_id    ==>'||l_wdt_rec.device_request_id,l_module_name,4);
2113                  print_debug('move_order_line_id   ==>'||l_wdt_rec.move_order_line_id,l_module_name,4);
2114                  print_debug('task_type            ==>'||l_wdt_rec.task_type,l_module_name,4);
2115                  print_debug('Plan  Id             ==>'||l_wdt_rec.operation_plan_id,l_module_name,4);
2116                  print_debug('Plan Instance Id     ==>'||l_wdt_rec.op_plan_instance_id,l_module_name,4);
2117                  print_debug('Move Order Line Id   ==>'||l_wdt_rec.move_order_line_id,l_module_name,4);
2118               END IF;
2119 
2120               l_progress:=100;
2121 
2122               WMS_OP_RUNTIME_PVT_APIS.Insert_Dispatched_tasks
2123               ( p_wdt_rec        => l_wdt_rec,
2124                 p_source_task_id => p_source_task_id,
2125                 x_return_status  => x_return_status,
2126                 x_msg_count      => x_msg_count,
2127                 x_msg_data       => x_msg_data
2128                );
2129 
2130               l_progress:=110;
2131 
2132              IF (l_debug=1) THEN
2133                print_debug('return status afte calling table handler '||x_return_status,l_module_name,9);
2134              END IF;
2135 
2136              IF (x_return_status=g_ret_sts_error) THEN
2137 
2138                IF (l_debug=1) THEN
2139                  print_debug('Expected error from table handler',l_module_name,1);
2140                END IF;
2141 
2142                RAISE FND_API.G_EXC_ERROR;
2143 
2144              ELSIF (x_return_status<>g_ret_sts_success) THEN
2145                IF (l_debug=1) THEN
2146                  print_debug('Unexpected error fromt table hander',l_module_name,1);
2147                END IF;
2148                RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2149 
2150              END IF;
2151 
2152            END IF; /*WDT Record to be inserted*/
2153 
2154 
2155 
2156       /** If P_operation_Type_ID = 'Inspect' AND WOOI.operation_Type_ID = 'Load'
2157         *  This is the case where user inspects an LPN containing 2 mmtt lines:
2158         *  first with operation plan 'Inspect', second with operation plan 'load-drop-inspect'.
2159         *  User is allowed to inspect the full quantity. When inspect page calls activate_operation_instance
2160         *  for the second mmtt line, we should abort the operation plan.We first call abort_plan_instance
2161         *  to take care of plan/operation instances and null out MMTT.operation_plan_ID;
2162         *  then we call 'complete' document handler to take care of MMTT/MTRL etc.
2163         *  WMS_OP_RUNTIME_PUB.Abort_Plan_Instance
2164         *                      Return success;
2165         */
2166 
2167           IF (l_operation_type_id=G_OP_TYPE_INSPECT AND l_opertn_instance_details.operation_type_id=G_OP_TYPE_LOAD) THEN
2168 
2169              IF (l_debug=1) THEN
2170               print_debug('Aborting the Plan where the operation is load and user performs an inspect',l_module_name,9);
2171              END IF;
2172 
2173              Abort_Operation_Plan(  x_return_status    =>x_return_status
2174                                , x_msg_data         =>x_msg_data
2175                                , x_msg_count        =>x_msg_count
2176                                , x_error_code       =>x_error_code
2177                                , p_source_task_id   =>p_source_task_id
2178                                , p_activity_type_id =>p_activity_id);
2179              l_progress:=120;
2180 
2181            IF (l_debug=1) THEN
2182              print_debug('Return status of Abort_operation_plan'||x_return_status,l_module_name,9);
2183            END IF;
2184 
2185            IF (x_return_status=g_ret_sts_error) THEN
2186               IF (l_debug=1) THEN
2187                  print_debug('Error Obtained from Abort_operation_plan',l_module_name,1);
2188               END IF;
2189               RAISE FND_API.G_EXC_ERROR;
2190 
2191            ELSIF (x_return_status=g_ret_sts_unexp_error) THEN
2192 
2193              IF (l_debug=1) THEN
2194                print_debug('Error Obtained from Abort_operation_plan',l_module_name,1);
2195              END IF;
2196              RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2197           ELSE
2198              /*Return status from Abort is sucess.REturn from here*/
2199              RETURN;
2200 
2201           END IF;
2202 
2203        END IF;/*Operation Type Inspect*/
2204 
2205       END IF;/*Only incase of Inbound*/
2206 
2207       l_progress:=130;
2208 
2209       l_wooi_rec.operation_instance_id := l_opertn_instance_details.operation_instance_id;
2210 
2211       IF (l_opertn_instance_details.operation_status=G_OP_INS_STAT_PENDING) THEN
2212          IF (l_debug=1) THEN
2213             print_debug('Current Operation status is Pending,Update it to Active',l_module_name,9);
2214          END IF;
2215 
2216          /*If the status for this WOOI record is 'pending' update it to 'Active'*/
2217          l_wooi_rec.operation_status      := G_OP_INS_STAT_ACTIVE;
2218 
2219          l_progress:=140;
2220 
2221          IF (l_debug=1) THEN
2222             print_debug('the following values set',l_module_name,9);
2223             print_debug('status ==>'||l_wooi_rec.operation_status,l_module_name,9);
2224             print_debug('Operation Instance Id==>'||l_wooi_rec.operation_instance_id,l_module_name,9);
2225          END IF;
2226 
2227       END IF;/*Operation status is Pending*/
2228 
2229       l_progress:=150;
2230 
2231       /* If there is only one WOOI record for the same OP_PLAN_INSTANCE_ID,
2232        * it means it is the first step of this plan.
2233        * In this case, update WMS_OP_PLAN_INSTANCES status to 'Active' for this OP_PLAN_INSTANCE_ID.*/
2234 
2235       OPEN c_operation_sequence(l_op_plan_instance_id);
2236 
2237       FETCH c_operation_sequence INTO l_operation_count;
2238 
2239       CLOSE c_operation_sequence;
2240 
2241       l_progress:=160;
2242 
2243       IF (l_operation_count=1) THEN
2244          IF (l_debug=1) THEN
2245             print_debug('First operation in the plan,hence update plan status to In Progress',l_module_name,9);
2246          END IF;
2247 
2248          l_wopi_rec.op_plan_instance_id:=l_op_plan_instance_id;
2249          l_wopi_rec.status:=G_OP_INS_STAT_IN_PROGRESS;
2250 	 l_wopi_rec.plan_execution_start_date := Sysdate;
2251 
2252          l_progress:=170;
2253 
2254          IF (l_debug=1) THEN
2255             print_debug('Calling WMS_OP_RUNTIME_PVT_APIS.UPDATE_PLAN_INSTANCE to update the foll values',l_module_name,9);
2256             print_debug('Status'||l_wopi_rec.status,l_module_name,9);
2257             print_debug('Plan Instance Id'||l_wopi_rec.op_plan_instance_id,l_module_name,9);
2258 
2259          END IF;
2260 
2261          WMS_OP_RUNTIME_PVT_APIS.UPDATE_PLAN_INSTANCE
2262            ( p_update_rec    => l_wopi_rec
2263             ,x_return_status => x_return_status
2264             ,x_msg_count     => x_msg_count
2265             ,x_msg_data      => x_msg_data);
2266 
2267          l_progress:=180;
2268 
2269          IF (l_debug=1) THEN
2270              print_debug('Return status from table hanlder'||x_return_status,l_module_name,9);
2271           END IF;
2272 
2273           IF (x_return_status=g_ret_sts_error) THEN
2274              RAISE FND_API.G_EXC_ERROR;
2275           ELSIF (x_return_status<>g_ret_sts_success) THEN
2276              RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2277           END IF;
2278 
2279       END IF;/*Count=1*/
2280 
2281       /*IF P_Operation_Type_ID = 'Drop'
2282        * Derive destination methods.
2283        */
2284 
2285       IF ( l_operation_type_id=G_OP_TYPE_DROP AND p_activity_id=G_OP_ACTIVITY_INBOUND) THEN
2286 
2287          l_progress:=190;
2288 
2289          /*Calling Determine_attributes to get the suggested sub,locator and LPN based on the
2290            PLan Attributes*/
2291          IF (l_debug=1) THEN
2292             print_debug('Calling Determine_Attributes with the following parametemrs',l_module_name,9);
2293             print_debug('p_source_task_id  ==> '||p_source_task_id,l_module_name,4);
2294             print_debug('p_activity_id     ==> '||p_activity_id,l_module_name,4);
2295          END IF;
2296 
2297          Determine_Attributes(
2298               x_return_status    => x_return_status,
2299               x_msg_data         => x_msg_data,
2300               x_msg_count        => x_msg_count,
2301 	      x_error_code       => x_error_code,
2302 	      x_drop_lpn_option  => x_drop_lpn_option,
2303 	      x_plan_attributes  => l_dest_param_rec,
2304 	      x_consolidation_method_id => x_consolidation_method_id,
2305               p_source_task_id   => p_source_task_id,
2306 	      p_activity_type_id => p_activity_id,
2307 	      p_inventory_item_id => l_inbound_doc.inventory_item_id
2308 	      );
2309 
2310          l_progress:=200;
2311 
2312          IF (l_debug=1) THEN
2313             print_debug('Return status from Determine Attributes'||x_return_status,l_module_name,9);
2314 	    print_debug('x_drop_lpn_option = '||x_drop_lpn_option,l_module_name,9);
2315 
2316          END IF;
2317 
2318          IF (x_return_status=g_ret_sts_error) THEN
2319             IF (l_debug=1) THEN
2320                print_debug('Error obtained and hence raising exception',l_module_name,1);
2321             END IF;
2322             RAISE FND_API.G_EXC_ERROR;
2323 
2324          ELSIF (x_return_status=g_ret_sts_unexp_error) THEN
2325             IF (l_debug=1) THEN
2326                print_debug('Unexpected error obtained ',l_module_name,1);
2327             END IF;
2328             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2329 
2330          END IF;
2331 
2332          IF (l_debug=1) THEN
2333             print_debug('Values returned from Determine Attributes are',l_module_name,9);
2334             print_debug('suggested_sub_code ==>'||l_dest_param_rec.sug_sub_code,l_module_name,9);
2335             print_debug('Suggested Locator  ==>'||l_dest_param_rec.sug_location_id,l_module_name,9);
2336             print_debug('Suggested LPN      ==>'||l_dest_param_rec.cartonization_id,l_module_name,9);
2337          END IF;
2338 
2339 
2340          l_progress:=210;
2341 
2342          l_mmtt_rec.transaction_temp_id        := p_source_task_id;
2343          l_mmtt_rec.transaction_source_type_id := l_inbound_doc.transaction_source_type_id;
2344          l_mmtt_rec.transaction_action_id      := l_inbound_doc.transaction_action_id;
2345          l_mmtt_rec.organization_id            := l_inbound_doc.organization_id;
2346          l_mmtt_rec.parent_line_id             := l_inbound_doc.parent_line_id;
2347          l_mmtt_rec.primary_quantity           := l_inbound_doc.primary_quantity;
2348 	 l_mmtt_rec.inventory_item_id          := l_inbound_doc.inventory_item_id;
2349 
2350          IF (l_debug=1) THEN
2351             print_debug('Doc attributes being passed to the DOc handler',l_module_name,9);
2352             print_debug('Trx Src Type Id ==>'||l_mmtt_rec.transaction_source_type_id,l_module_name,9);
2353             print_debug('Trx Action Id   ==>'||l_mmtt_rec.transaction_action_id,l_module_name,9);
2354             print_debug('Organization Id ==>'||l_mmtt_rec.organization_id,l_module_name,9);
2355 	    print_debug('l_mmtt_rec.inventory_item_id  ==>'||l_mmtt_rec.inventory_item_id , l_module_name, 9);
2356          END IF;
2357 
2358          WMS_OP_INBOUND_PVT.ACTIVATE
2359             ( x_return_status    => l_return_status,--Use l_return_status instead
2360               x_msg_count        => l_msg_count,    --of x_return_status because
2361               x_msg_data         => l_msg_data,     --it will override the value
2362               x_error_code       => x_error_code,   --returned from determine_attributes
2363               p_source_task_id   => p_source_task_id,
2364               p_update_param_rec => l_dest_param_rec,
2365               p_document_rec     => l_mmtt_rec);
2366 
2367 
2368          l_progress:=220;
2369 
2370          IF (l_debug=1) THEN
2371              print_debug('Return status from document handler'||l_return_status,l_module_name,9);
2372          END IF;
2373 
2374          IF (l_return_status=g_ret_sts_error) THEN
2375              RAISE FND_API.G_EXC_ERROR;
2376          ELSIF (l_return_status<>g_ret_sts_success) THEN
2377              RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2378          END IF;
2379 
2380          l_progress:=230;
2381 
2382          l_wooi_rec.SUG_TO_SUB_CODE   := l_dest_param_rec.sug_sub_code;
2383          l_wooi_rec.SUG_TO_LOCATOR_ID := l_dest_param_rec.sug_location_id;
2384 	 /* comment out  by GXIAO 12/10/03*/
2385          --l_wooi_rec.LPN_ID            := l_dest_param_rec.cartonization_id;
2386       END IF;/*Activity Inbound and Drop Operation*/
2387 
2388       l_progress:=240;
2389 
2390       WMS_OP_RUNTIME_PVT_APIS.UPDATE_OPERATION_INSTANCE
2391             (  x_return_status => l_return_status --Use l_return_status instead
2392              , x_msg_count     => l_msg_count     --of x_return_status because
2393              , x_msg_data      => l_msg_data      --it will override the value
2394              , p_update_rec    => l_wooi_rec);    --returned from determine_attributes
2395 
2396       IF (l_debug=1) THEN
2397         print_debug('Return status from table hanlder'||l_return_status,l_module_name,9);
2398       END IF;
2399 
2400       IF (l_return_status=g_ret_sts_error) THEN
2401         RAISE FND_API.G_EXC_ERROR;
2402       ELSIF (l_return_status<>g_ret_sts_success) THEN
2403         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2404       END IF;
2405 
2406          l_progress:=250;
2407 
2408 
2409   EXCEPTION
2410 
2411      WHEN FND_API.G_EXC_ERROR THEN
2412            IF (l_debug=1) THEN
2413              print_debug('Expected Error at '||l_progress, l_module_name,1);
2414            END IF;
2415 
2416            IF fnd_msg_pub.check_msg_level(g_msg_lvl_error) THEN
2417               fnd_msg_pub.add_exc_msg(g_pkg_name, l_module_name, SQLERRM);
2418            END IF; /* fnd_msg.... */
2419 
2420            fnd_msg_pub.count_and_get(
2421                                       p_count => x_msg_count,
2422                                       p_data  => x_msg_data
2423                                      );
2424            --x_error_code := SQLCODE;
2425            x_return_status := g_ret_sts_error;
2426 
2427            IF (c_inbound_doc%ISOPEN) THEN
2428               CLOSE c_inbound_doc;
2429            END IF;
2430 
2431            IF (c_wdt_details%ISOPEN) THEN
2432               CLOSE c_wdt_details;
2433            END IF;
2434 
2435            IF (c_operation_instance_details%ISOPEN) THEN
2436               CLOSE c_operation_instance_details;
2437            END IF;
2438 
2439            IF (c_operation_sequence%ISOPEN) THEN
2440               CLOSE c_operation_sequence;
2441            END IF;
2442 
2443            IF (c_effective_date%ISOPEN) THEN
2444               CLOSE c_effective_date;
2445            END IF;
2446 
2447            ROLLBACK TO activate_op_sp;
2448 
2449         WHEN OTHERS THEN
2450            IF (l_debug=1) THEN
2451              print_debug('Unexpected error at'||l_progress||' '||SQLERRM, l_module_name,1);
2452            END IF;
2453 
2454 
2455            IF fnd_msg_pub.check_msg_level(g_msg_lvl_unexp_error) THEN
2456               fnd_msg_pub.add_exc_msg(g_pkg_name, l_module_name, SQLERRM);
2457            END IF; /* fnd_msg.... */
2458 
2459            fnd_msg_pub.count_and_get(
2460                                       p_count => x_msg_count,
2461                                       p_data  => x_msg_data
2462                                      );
2463            --x_error_code := SQLCODE;
2464            x_return_status := g_ret_sts_unexp_error;
2465            IF (c_inbound_doc%ISOPEN) THEN
2466               CLOSE c_inbound_doc;
2467            END IF;
2468 
2469            IF (c_wdt_details%ISOPEN) THEN
2470               CLOSE c_wdt_details;
2471            END IF;
2472 
2473            IF (c_operation_instance_details%ISOPEN) THEN
2474               CLOSE c_operation_instance_details;
2475            END IF;
2476 
2477            IF (c_operation_sequence%ISOPEN) THEN
2478               CLOSE c_operation_sequence;
2479            END IF;
2480 
2481            IF (c_effective_date%ISOPEN) THEN
2482               CLOSE c_effective_date;
2483            END IF;
2484 
2485 
2486            ROLLBACK TO activate_op_sp;
2487 
2488  END ACTIVATE_OPERATION_INSTANCE;
2489 
2490 
2491 
2492 
2493 /**  /**  Complete_Operation_instance
2494   *   <p>This procedure completes the current operation and  creates the next operation instance.
2495   *     If the operation is the last operation the plan is marked  as 'COMPLETED' and archived.</p>
2496   *
2497   *  @param x_return_status      -Return Status
2498   *  @param x_msg_data           -Returns the Error message Status
2499   *  @param x_msg_count          -Returns the message count
2500   *  @param x_error_code         -Returns appropriate error code in case of any error.
2501   *  @param p_source_task_id     -Identifier of the document record.
2502   *  @param p_activity_id        -Lookup Code for the Activity Type
2503   *  @param p_operation_type_id  -Input parameter containing the lookup code for the Operation
2504   *                               Type. Should be passed for inspect operation.
2505  **/
2506     PROCEDURE complete_operation_instance
2507     (
2508      x_return_status     OUT  NOCOPY  VARCHAR2
2509      ,x_msg_data          OUT  NOCOPY  FND_NEW_MESSAGES.MESSAGE_TEXT%TYPE
2510      ,x_msg_count         OUT  NOCOPY  NUMBER
2511      ,x_error_code        OUT  NOCOPY  NUMBER
2512      ,p_source_task_id    IN           NUMBER
2513      ,p_activity_id       IN           NUMBER
2514      ,p_operation_type_id IN           NUMBER )
2515     IS
2516 
2517        l_debug               NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
2518        l_module_name         VARCHAR2(30) := 'Complete_Operation_Instance';
2519        l_progress            NUMBER;
2520        l_return_status       VARCHAR2(1);
2521        l_msg_count           NUMBER;
2522        l_msg_data            VARCHAR2(400);
2523 
2524        l_wdt_task_id         NUMBER;
2525        l_wdt_status          NUMBER;
2526        l_operation_type_id   NUMBER;
2527        l_new_mmtt_id         NUMBER;
2528        l_atf_error_code      NUMBER;
2529        l_last_op_sequence    NUMBER;
2530 
2531        l_unforgivable_error_flag  VARCHAR2(1) := 'N';
2532        l_loc_sel_criteria_id      NUMBER;
2533        l_loc_sel_api_id           NUMBER;
2534 
2535        l_locator_id_dummy         NUMBER;
2536        l_subinventory_code_dummy  VARCHAR2(10);
2537        l_zone_id_dummy            NUMBER;
2538        l_valid_flag               VARCHAR2(1);
2539        l_lpn_controlled_flag        NUMBER;
2540 
2541        CURSOR c_inbound_document_details IS
2542 	  SELECT transaction_temp_id,
2543             operation_plan_id,
2544             inventory_item_id,
2545             subinventory_code,
2546             locator_id,
2547             transfer_subinventory,
2548             transfer_to_location,
2549             organization_id,
2550             Decode(wms_task_type, -1, g_wms_task_type_putaway, wms_task_type) wms_task_type,
2551 	    parent_line_id,
2552 	    transaction_action_id,
2553 	    transaction_source_type_id,
2554 	    transaction_type_id,
2555 	    move_order_line_id,
2556 	    transaction_uom,
2557 	    transaction_quantity,
2558 	    lpn_id,
2559 	    content_lpn_id,
2560 	    transfer_lpn_id
2561 	    FROM mtl_material_transactions_temp
2562 	    WHERE transaction_temp_id=p_source_task_id;
2563 
2564        l_inbound_doc_rec  c_inbound_document_details%ROWTYPE;
2565        l_mmtt_rec mtl_material_transactions_temp%ROWTYPE;
2566 
2567        -- cursor to get the current active operation instance
2568        -- for the source task
2569 
2570        CURSOR c_wooi_data_rec IS
2571 	  SELECT wooi.operation_instance_id,
2572 	    wooi.operation_type_id,
2573 	    wooi.operation_plan_detail_id,
2574 	    wooi.op_plan_instance_id,
2575 	    wooi.operation_status,
2576 	    wooi.operation_sequence,
2577 	    wooi.sug_to_sub_code,
2578 	    wooi.sug_to_locator_id,
2579 	    wooi.organization_id,
2580 	    wopd.operation_plan_id
2581 	    FROM wms_op_operation_instances wooi,
2582 	    wms_op_plan_details wopd
2583 	    WHERE wooi.source_task_id = p_source_task_id
2584 	    AND wooi.activity_type_id = p_activity_id
2585 	    AND wooi.operation_plan_detail_id = wopd.operation_plan_detail_id
2586 	    ORDER BY wooi.operation_sequence DESC,
2587 	    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
2588 --	    AND operation_status = g_op_ins_stat_active;
2589 
2590        l_wooi_data_rec c_wooi_data_rec%ROWTYPE;
2591        l_wooi_rec wms_op_operation_instances%ROWTYPE;
2592 
2593        -- cursor to get the plan detail for the next operation
2594        CURSOR c_next_plan_detail(v_operation_plan_id NUMBER,
2595 				 v_current_detail_sequence NUMBER) IS
2596 	  SELECT wopd1.operation_plan_detail_id
2597 	    , wopd1.operation_sequence
2598 	    , wopd1.is_in_inventory
2599 	    , wopd1.operation_type
2600 	    , 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
2601 	    FROM wms_op_plan_details wopd1,
2602 	    wms_op_plans_b wopb
2603 	    WHERE wopd1.operation_plan_id  = v_operation_plan_id
2604 	    AND wopb.operation_plan_id = v_operation_plan_id
2605 	    AND   wopd1.operation_sequence = (SELECT MIN(wopd2.operation_sequence)
2606 					      FROM wms_op_plan_details wopd2
2607 					      WHERE wopd2.operation_plan_id = v_operation_plan_id
2608 					      AND (wopd2.operation_sequence > v_current_detail_sequence  -- operations after the current
2609 						   OR (wopd2.operation_type = g_op_type_crossdock -- when current is a load operation for crossdock, sequence can be same as current operation
2610 						       AND wopd2.operation_sequence = v_current_detail_sequence)
2611 						   )
2612 					      );
2613 
2614        l_operation_plan_detail_rec c_next_plan_detail%ROWTYPE;
2615 
2616     BEGIN
2617 
2618        IF (l_debug = 1) THEN
2619 	  print_debug(' Entered. ',l_module_name,1);
2620 
2621 	  print_debug(' p_source_task_id==> '||p_source_task_id,l_module_name,3);
2622 	  print_debug(' p_activity_id==> '||p_activity_id,l_module_name,3);
2623 	  print_debug(' p_operation_type_id==> '||p_operation_type_id,l_module_name,3);
2624        END IF;
2625 
2626        x_return_status  := g_ret_sts_success;
2627        l_progress := 10;
2628        SAVEPOINT sp_complete_oprtn_instance;
2629 
2630 
2631        IF p_source_task_id IS NULL THEN
2632 	  IF (l_debug=1) THEN
2633 	     print_debug('Invalid input param. p_source_task_id Cannot be NULL.',l_module_name,4);
2634 	  END IF;
2635 	  RAISE FND_API.G_EXC_ERROR;
2636        END IF;
2637 
2638 
2639        IF p_activity_id IS NULL THEN
2640 	  IF (l_debug=1) THEN
2641 	     print_debug('Invalid input param. p_activity_id Cannot be NULL.',l_module_name,4);
2642 	  END IF;
2643 	  RAISE FND_API.G_EXC_ERROR;
2644        END IF;
2645 
2646        IF p_operation_type_id IS NOT NULL AND
2647 	 p_operation_type_id NOT IN (g_op_type_drop, g_op_type_load, g_op_type_inspect)THEN
2648 	  IF (l_debug=1) THEN
2649 	     print_debug('Invalid input param. p_operation_type_id can only be load, drop, inspect or NULL.',l_module_name,4);
2650 	  END IF;
2651 	  RAISE FND_API.G_EXC_ERROR;
2652        END IF;
2653 
2654        l_operation_type_id := p_operation_type_id;
2655 
2656        l_progress := 20;
2657 
2658        IF p_activity_id = g_op_activity_inbound THEN
2659 	  IF (l_debug=1) THEN
2660 	     print_debug('Inbound Activity.',l_module_name,4);
2661 	  END IF;
2662 
2663 	  OPEN c_inbound_document_details;
2664 
2665 	  FETCH c_inbound_document_details
2666 	    INTO l_inbound_doc_rec;
2667 
2668 	  IF c_inbound_document_details%notfound THEN
2669 	     IF (l_debug=1) THEN
2670 		print_debug('Invalid input param. p_source_task_id does not match to an MMTT record.',l_module_name,4);
2671 	     END IF;
2672 	     RAISE FND_API.G_EXC_ERROR;
2673 	  END IF;
2674 
2675 	  CLOSE c_inbound_document_details;
2676 
2677 
2678 	  l_mmtt_rec.transaction_temp_id := l_inbound_doc_rec.transaction_temp_id;
2679 	  l_mmtt_rec.operation_plan_id := l_inbound_doc_rec.operation_plan_id;
2680 	  l_mmtt_rec.inventory_item_id := l_inbound_doc_rec.inventory_item_id;
2681 	  l_mmtt_rec.subinventory_code := l_inbound_doc_rec.subinventory_code;
2682 	  l_mmtt_rec.locator_id := l_inbound_doc_rec.locator_id;
2683 	  l_mmtt_rec.transfer_subinventory := l_inbound_doc_rec.transfer_subinventory;
2684 	  l_mmtt_rec.transfer_to_location := l_inbound_doc_rec.transfer_to_location;
2685 	  l_mmtt_rec.organization_id := l_inbound_doc_rec.organization_id;
2686 	  l_mmtt_rec.wms_task_type := l_inbound_doc_rec.wms_task_type;
2687 	  l_mmtt_rec.parent_line_id := l_inbound_doc_rec.parent_line_id;
2688 	  l_mmtt_rec.transaction_action_id := l_inbound_doc_rec.transaction_action_id;
2689 	  l_mmtt_rec.transaction_source_type_id := l_inbound_doc_rec.transaction_source_type_id;
2690 	  l_mmtt_rec.transaction_type_id := l_inbound_doc_rec.transaction_type_id;
2691 	  l_mmtt_rec.move_order_line_id := l_inbound_doc_rec.move_order_line_id;
2692 	  l_mmtt_rec.transaction_uom := l_inbound_doc_rec.transaction_uom;
2693 	  l_mmtt_rec.transaction_quantity := l_inbound_doc_rec.transaction_quantity;
2694 
2695 
2696 	  IF (l_debug=1) THEN
2697 	     print_debug('l_mmtt_rec.transaction_temp_id = '|| l_mmtt_rec.transaction_temp_id, l_module_name,4);
2698 	     print_debug('l_mmtt_rec.operation_plan_id = '|| l_mmtt_rec.operation_plan_id, l_module_name,4);
2699 	     print_debug('l_mmtt_rec.inventory_item_id = '|| l_mmtt_rec.inventory_item_id, l_module_name,4);
2700 	     print_debug('l_mmtt_rec.subinventory_code = '|| l_mmtt_rec.subinventory_code, l_module_name,4);
2701 	     print_debug('l_mmtt_rec.locator_id = '|| l_mmtt_rec.locator_id, l_module_name,4);
2702 	     print_debug('l_mmtt_rec.transfer_subinventory = '|| l_mmtt_rec.transfer_subinventory, l_module_name,4);
2703 	     print_debug('l_mmtt_rec.transfer_to_location  = '|| l_mmtt_rec.transfer_to_location , l_module_name,4);
2704 	     print_debug('l_mmtt_rec.organization_id = '|| l_mmtt_rec.organization_id, l_module_name,4);
2705 	     print_debug('l_mmtt_rec.wms_task_type = '|| l_mmtt_rec.wms_task_type, l_module_name,4);
2706 	     print_debug('l_mmtt_rec.parent_line_id = '|| l_mmtt_rec.parent_line_id, l_module_name,4);
2707 	     print_debug('l_mmtt_rec.transaction_action_id = '|| l_mmtt_rec.transaction_action_id, l_module_name,4);
2708 	     print_debug('l_mmtt_rec.transaction_source_type_id = '|| l_mmtt_rec.transaction_source_type_id, l_module_name,4);
2709 	     print_debug('l_mmtt_rec.transaction_type_id = '|| l_mmtt_rec.transaction_type_id, l_module_name,4);
2710 	     print_debug('l_mmtt_rec.move_order_line_id = '|| l_mmtt_rec.move_order_line_id, l_module_name,4);
2711 	     print_debug('l_mmtt_rec.transaction_uom = '|| l_mmtt_rec.transaction_uom, l_module_name,4);
2712 	     print_debug('l_mmtt_rec.transaction_quantity = '|| l_mmtt_rec.transaction_quantity, l_module_name,4);
2713 	     print_debug('l_inbound_doc_rec.lpn_id = '||l_inbound_doc_rec.lpn_id , l_module_name,4);
2714 	     print_debug('l_inbound_doc_rec.content_lpn_id = '||l_inbound_doc_rec.content_lpn_id , l_module_name,4);
2715 	     print_debug('l_inbound_doc_rec.transfer_lpn_id = '||l_inbound_doc_rec.transfer_lpn_id , l_module_name,4);
2716 
2717 	  END IF;
2718 
2719 
2720 	  l_progress := 23;
2721 
2722 	  IF l_mmtt_rec.operation_plan_id IS NOT NULL THEN
2723 	     -- fetch the current active operation instance for this task
2724 	     OPEN c_wooi_data_rec;
2725 
2726 	     FETCH c_wooi_data_rec INTO l_wooi_data_rec;
2727 	     IF c_wooi_data_rec%notfound THEN
2728 		IF (l_debug = 1) THEN
2729 		   print_debug('Operation instance record does not exist for this task.',l_module_name,4);
2730 		END IF;
2731 		fnd_message.set_name('WMS', 'WMS_ATF_NO_ACTIVE_PLAN');
2732 		fnd_msg_pub.ADD;
2733 		RAISE FND_API.G_EXC_ERROR;
2734 	     END IF;
2735 
2736 	     CLOSE c_wooi_data_rec;
2737 
2738 	     IF (l_debug = 1) THEN
2739 
2740 		print_debug('l_wooi_data_rec.operation_instance_id = '|| l_wooi_data_rec.operation_instance_id,l_module_name,4);
2741 		print_debug('l_wooi_data_rec.operation_type_id = '|| l_wooi_data_rec.operation_type_id,l_module_name,4);
2742 		print_debug('l_wooi_data_rec.operation_plan_detail_id = '|| l_wooi_data_rec.operation_plan_detail_id,l_module_name,4);
2743 		print_debug('l_wooi_data_rec.operation_plan_id = '|| l_wooi_data_rec.operation_plan_id,l_module_name,4);
2744 		print_debug('l_wooi_data_rec.op_plan_instance_id = '|| l_wooi_data_rec.op_plan_instance_id,l_module_name,4);
2745 		print_debug('l_wooi_data_rec.operation_status = '|| l_wooi_data_rec.operation_status,l_module_name,4);
2746 		print_debug('l_wooi_data_rec.operation_sequence = '|| l_wooi_data_rec.operation_sequence,l_module_name,4);
2747 		print_debug('l_wooi_data_rec.sug_to_sub_code = '|| l_wooi_data_rec.sug_to_sub_code,l_module_name,4);
2748 		print_debug('l_wooi_data_rec.sug_to_locator_id = '|| l_wooi_data_rec.sug_to_locator_id,l_module_name,4);
2749 	     END IF;
2750 
2751 	     IF l_wooi_data_rec.operation_status = g_op_ins_stat_pending THEN
2752 		-- Normally complete_operation_instance should not be called
2753 		-- when operation instance is pending.
2754 		-- It only occurs when complete_operation_instance is called
2755 		-- from packing workbenches receiving TM call.
2756 		-- In this case we raise an exception to by-pass all the logic below,
2757 		-- and return a special x_error_code
2758 		IF (l_debug = 1) THEN
2759 		   print_debug('Operation instance pending.',l_module_name,4);
2760 		END IF;
2761 		raise_application_error(COMPLETE_PENDING_OP,'Complete a pending operation instance');
2762 	      ELSIF l_wooi_data_rec.operation_status <> g_op_ins_stat_active THEN
2763 		IF (l_debug = 1) THEN
2764 		   print_debug('Invalid operation instance.',l_module_name,4);
2765 		END IF;
2766 		raise_application_error(INVALID_STATUS_FOR_OPERATION,'Invalid operation instance');
2767 	     END IF;
2768 
2769 	  END IF;
2770 
2771 
2772 	  l_progress := 25;
2773 
2774          BEGIN
2775 	    SELECT task_id, status
2776 	      INTO l_wdt_task_id, l_wdt_status
2777 	      FROM wms_dispatched_tasks
2778 	      WHERE transaction_temp_id = p_source_task_id
2779 	      AND task_type IN (g_wms_task_type_putaway, g_wms_task_type_inspect);
2780 
2781 	 EXCEPTION
2782 	    WHEN no_data_found THEN
2783 	       IF (l_debug=1) THEN
2784 		  print_debug('WDT does not exist for this MMTT.',l_module_name,4);
2785 	       END IF;
2786 	       RAISE FND_API.G_EXC_ERROR;
2787 
2788 	 END;
2789 
2790 	 l_progress := 30;
2791 
2792 	 IF (l_debug=1) THEN
2793 	    print_debug('l_wdt_task_id = '||l_wdt_task_id,l_module_name,4);
2794 	    print_debug('l_wdt_status = '||l_wdt_status,l_module_name,4);
2795 	 END IF;
2796 
2797 	 IF l_wdt_status NOT IN (g_task_status_loaded, g_task_status_dispatched) THEN
2798 	    IF (l_debug=1) THEN
2799 	       print_debug('Invalid WDT status.' ,l_module_name,4);
2800 	    END IF;
2801 	    fnd_message.set_name('WMS', 'WMS_ATF_INVALID_TASK_STATUS');
2802 	    fnd_msg_pub.ADD;
2803 	    RAISE FND_API.G_EXC_ERROR;
2804 	 END IF; -- IF l_wdt_status NOT IN (g_task_status_loaded, g_task_status_dispatched)
2805 
2806        END IF;  -- IF p_activity_id = g_op_activity_inbound
2807 
2808        l_progress := 30;
2809 
2810 
2811        IF l_wdt_status = g_task_status_dispatched THEN
2812 
2813 
2814 	  --This can happened for two operations today,
2815 	  --1.	Inspect
2816 	  --2.	Load
2817 
2818 	  --NOTE: Today the status of WDT is going to be 'Dispatched' for load and inspect
2819 	  --      (for drop it will be 'Loaded').
2820 	  --      But for Outbound/Warehousing tasks or when we have Inbound
2821 	  --      dispatched tasks then we CAN have a status of 'Active' for WDT records
2822 
2823 	  IF (l_debug=1) THEN
2824 	     print_debug('WDT status is dispatched.' ,l_module_name,4);
2825 	  END IF;
2826 
2827 
2828 	  IF p_operation_type_id = g_op_type_inspect THEN
2829 
2830 
2831 	     IF (l_debug=1) THEN
2832 		print_debug('Current operation is inspect.' ,l_module_name,4);
2833 		print_debug('Set l_operation_type_id to inspect.' ,l_module_name,4);
2834 
2835 		print_debug('Before calling wms_op_runtime_pvt_apis.archive_dispatched_tasks with following parameters' ,l_module_name,4);
2836 		print_debug('p_task_id => '|| l_wdt_task_id,l_module_name,4);
2837 		print_debug('p_source_task_id => '|| p_source_task_id,l_module_name,4);
2838 		print_debug('p_activity_type_id => '|| p_activity_id,l_module_name,4);
2839 	     END IF;
2840 
2841 	     l_operation_type_id := g_op_type_inspect;
2842 
2843 	     l_progress :=  40;
2844 
2845 	     wms_op_runtime_pvt_apis.archive_dispatched_tasks
2846 	       (
2847 		x_return_status         => l_return_status
2848 		, x_msg_count           => l_msg_count
2849 		, x_msg_data            => l_msg_data
2850 		, p_task_id             => l_wdt_task_id
2851 		, p_source_task_id      => p_source_task_id
2852 		, p_activity_type_id    => p_activity_id
2853 		, p_op_plan_instance_id => NULL
2854       , p_op_plan_status      => NULL
2855 		);
2856 
2857 	     l_progress :=  50;
2858 
2859 	     IF (l_debug=1) THEN
2860 		print_debug('After calling wms_op_runtime_pvt_apis.archive_dispatched_tasks .' ,l_module_name,4);
2861 		print_debug('x_return_status => '|| l_return_status,l_module_name,4);
2862 		print_debug('x_msg_count => '|| l_msg_count,l_module_name,4);
2863 		print_debug('x_msg_data => '|| l_msg_data,l_module_name,4);
2864 	     END IF;
2865 
2866 
2867 	     IF l_return_status <>FND_API.g_ret_sts_success THEN
2868 		IF (l_debug=1) THEN
2869 		   print_debug('wms_op_runtime_pvt_apis.archive_dispatched_tasks finished with error. l_return_status = ' || l_return_status,l_module_name,4);
2870 		END IF;
2871 		fnd_message.set_name('WMS', 'WMS_ATF_ARCHIVE_TASK_FAILURE');
2872 		fnd_msg_pub.ADD;
2873 		RAISE FND_API.G_EXC_ERROR;
2874 	     END IF;
2875 
2876 	   ELSIF p_operation_type_id IS NULL OR p_operation_type_id = g_op_type_load THEN
2877 	     IF (l_debug=1) THEN
2878 		print_debug('Current operation is load. Update WDT to loaded.' ,l_module_name,4);
2879 	     END IF;
2880 
2881 
2882 	     IF (l_debug=1) THEN
2883 		print_debug('Set l_operation_type_id to load.' ,l_module_name,4);
2884 	     END IF;
2885 
2886 	     l_operation_type_id := g_op_type_load;
2887 
2888 
2889 	     l_progress := 60;
2890 
2891 	     UPDATE wms_dispatched_tasks
2892 	       SET status = g_task_status_loaded,
2893 	       loaded_time = Sysdate,
2894 	       last_update_date = Sysdate,
2895 	       last_updated_by = fnd_global.user_id
2896 	       WHERE task_id = l_wdt_task_id;
2897 
2898 	     l_progress := 70;
2899 
2900 	   ELSE -- p_operation_type_id = g_op_type_inspect (not inspect or load or NULL)
2901 
2902 	     IF (l_debug=1) THEN
2903 		print_debug('Current operation ('|| p_operation_type_id ||') is not compatible with WDT status of dispatched.' ,l_module_name,4);
2904 	     END IF;
2905 	     fnd_message.set_name('WMS', 'WMS_ATF_INVALID_TASK_STATUS');
2906 	     fnd_msg_pub.ADD;
2907 	     RAISE FND_API.G_EXC_ERROR;
2908 
2909 	  END IF; -- IF p_operation_type_id = g_op_type_inspect
2910 
2911 
2912 
2913 	ELSIF l_wdt_status = g_task_status_loaded THEN
2914 	  --This can happened for only for one operation today, i.e. 'Drop'
2915 	  --Assumption here is that you always have a Load before a Drop, even for 'Single Step Drop'
2916 
2917 	  IF (l_debug=1) THEN
2918 	     print_debug('WDT status is loaded. Archive this WDT into WDTH.' ,l_module_name,4);
2919 	     print_debug('Set l_operation_type_id to Drop.' ,l_module_name,4);
2920 	  END IF;
2921 
2922 	  l_operation_type_id := g_op_type_drop;
2923 
2924 	  IF (l_debug=1) THEN
2925 
2926 	     print_debug('Drop - Before calling wms_op_runtime_pvt_apis.archive_dispatched_tasks with following parameters' ,l_module_name,4);
2927 	     print_debug('p_task_id => '|| l_wdt_task_id,l_module_name,4);
2928 	     print_debug('p_source_task_id => '|| p_source_task_id,l_module_name,4);
2929 	     print_debug('p_activity_type_id => '|| p_activity_id,l_module_name,4);
2930 	  END IF;
2931 
2932 	  l_progress :=  80;
2933 
2934 	  wms_op_runtime_pvt_apis.archive_dispatched_tasks
2935 	    (
2936 	     x_return_status         => l_return_status
2937 	     , x_msg_count           => l_msg_count
2938 	     , x_msg_data            => l_msg_data
2939 	     , p_task_id             => l_wdt_task_id
2940 	     , p_source_task_id      => p_source_task_id
2941 	     , p_activity_type_id    => p_activity_id
2942 	     , p_op_plan_instance_id => NULL
2943         , p_op_plan_status      => NULL
2944 	     );
2945 
2946 	  l_progress :=  90;
2947 
2948 	  IF (l_debug=1) THEN
2949 	     print_debug('Drop - After calling wms_op_runtime_pvt_apis.archive_dispatched_tasks .' ,l_module_name,4);
2950 	     print_debug('x_return_status => '|| l_return_status,l_module_name,4);
2951 	     print_debug('x_msg_count => '|| l_msg_count,l_module_name,4);
2952 	     print_debug('x_msg_data => '|| l_msg_data,l_module_name,4);
2953 	  END IF;
2954 
2955 
2956 	  IF l_return_status <>FND_API.g_ret_sts_success THEN
2957 	     IF (l_debug=1) THEN
2958 		print_debug('Drop - wms_op_runtime_pvt_apis.archive_dispatched_tasks finished with error. l_return_status = ' || l_return_status,l_module_name,4);
2959 	     END IF;
2960 	     fnd_message.set_name('WMS', 'WMS_ATF_ARCHIVE_TASK_FAILURE');
2961 	     fnd_msg_pub.ADD;
2962 	     RAISE FND_API.G_EXC_ERROR;
2963 	  END IF;
2964 
2965 	ELSE -- l_wdt_status = g_task_status_dispatched (not dispatched or loaded)
2966 
2967 	  IF (l_debug=1) THEN
2968 	     print_debug('Invalid WDT status.' ,l_module_name,4);
2969 	  END IF;
2970 	  fnd_message.set_name('WMS', 'WMS_ATF_INVALID_TASK_STATUS');
2971 	  fnd_msg_pub.ADD;
2972 	  RAISE FND_API.G_EXC_ERROR;
2973 
2974 
2975        END IF; -- IF l_wdt_status = g_task_status_dispatched
2976 
2977        -- Check operation plan stamped or not
2978        -- The assumption here is that we are looking at a pre-generated 11.5.9 MMTT record.
2979        -- This is possibly called either from user performing load, drop, or inspect operation.
2980        -- If 'Load', don't need to do anything to document records, simply return.
2981 
2982        -- If 'drop' or 'inspect', we need to update MOL and delete MMTT based on transaction type etc.
2983        -- This step basically becomes the last step of an operation plan.
2984        -- The 'Complete' inbound document handler should be able to handle this by passing 'p_last_operation_flag' = 'Y'.
2985 
2986        IF p_activity_id = g_op_activity_inbound THEN
2987 	  IF (l_debug=1) THEN
2988 	     print_debug('Inbound Activity.',l_module_name,4);
2989 	  END IF;
2990 
2991 	  IF l_mmtt_rec.operation_plan_id IS NULL THEN
2992 	     IF (l_debug=1) THEN
2993 		print_debug('Operation plan ID is null for this MMTT.',l_module_name,4);
2994 	     END IF;
2995 
2996 	     IF l_operation_type_id IN (g_op_type_drop, g_op_type_inspect) THEN
2997 
2998 		IF (l_debug=1) THEN
2999 		   print_debug('Current operation is DROP or INSPECT. Need to call COMPLETE document handler',l_module_name,4);
3000 		   print_debug('Operation_plan_ID NULL - Before calling wms_op_inbound_pvt.complete with following parameters:',l_module_name,4);
3001 		   print_debug('p_source_task_id => '|| p_source_task_id,l_module_name,4);
3002 		   print_debug('p_document_rec.transaction_temp_id => '|| l_mmtt_rec.transaction_temp_id,l_module_name,4);
3003 		   print_debug('p_operation_type_id => '|| l_operation_type_id,l_module_name,4);
3004 		   print_debug('p_next_operation_type_id => '|| '',l_module_name,4);
3005 		   print_debug('p_is_last_operation_flag => '|| 'Y',l_module_name,4);
3006 
3007 		END IF;
3008 
3009 		l_progress :=  100;
3010 
3011 		wms_op_inbound_pvt.complete
3012 		  (
3013 		   x_return_status              => l_return_status
3014 		   , x_msg_data                 => l_msg_data
3015 		   , x_msg_count                => l_msg_count
3016 		   , x_source_task_id           => l_new_mmtt_id
3017 		   , x_error_code               => l_atf_error_code
3018 		   , p_source_task_id           => p_source_task_id
3019 		   , p_document_rec             => l_mmtt_rec
3020 		   , p_operation_type_id        => l_operation_type_id
3021 		   , p_next_operation_type_id   => NULL
3022 		   , p_sug_to_sub_code		=> l_wooi_data_rec.sug_to_sub_code
3023 		   , p_sug_to_locator_id	=> l_wooi_data_rec.sug_to_locator_id
3024 		   , p_is_last_operation_flag   => 'Y'
3025 		   );
3026 
3027 		l_progress :=  102;
3028 
3029 		IF (l_debug=1) THEN
3030 		   print_debug('Inbound op plan null - After calling wms_op_inbound_pvt.complete. ', l_module_name,4);
3031 
3032 		   print_debug('x_return_status => '||l_return_status, l_module_name,4);
3033 		   print_debug('x_msg_data => '||l_msg_data, l_module_name,4);
3034 		   print_debug('x_msg_count => '||l_msg_count, l_module_name,4);
3035 		   print_debug('x_source_task_id => '||l_new_mmtt_id, l_module_name,4);
3036 		   print_debug('x_error_code => '||l_atf_error_code, l_module_name,4);
3037 		END IF;
3038       --bug 6924639
3039 		 IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3040         IF (l_debug = 1) THEN
3041           print_debug('Inbound op plan null - wms_op_inbound_pvt.complete finished with error. l_return_status = ' || l_return_status,l_module_name,4);
3042         END IF;
3043 
3044         fnd_message.set_name('WMS', 'WMS_WORK_FLOW_FAIL');
3045         fnd_msg_pub.ADD;
3046         RAISE fnd_api.g_exc_unexpected_error;
3047       ELSIF l_return_status = fnd_api.g_ret_sts_error THEN
3048         IF (l_debug = 1) THEN
3049           print_debug('Inbound op plan null - wms_op_inbound_pvt.complete finished with error. l_return_status = ' || l_return_status,l_module_name,4);
3050         END IF;
3051 
3052         fnd_message.set_name('WMS', 'WMS_WORK_FLOW_FAIL');
3053         fnd_msg_pub.ADD;
3054         RAISE fnd_api.g_exc_error;
3055       END IF;
3056      --bug 6924639
3057       /*
3058 		IF l_return_status <>FND_API.g_ret_sts_success THEN
3059 		   IF (l_debug=1) THEN
3060 		      print_debug('Inbound op plan null - wms_op_inbound_pvt.complete finished with error. l_return_status = ' || l_return_status,l_module_name,4);
3061 		   END IF;
3062 
3063 		   RAISE FND_API.G_EXC_ERROR;
3064 		END IF;
3065 		*/
3066 	     END IF; -- IF l_operation_type_id IN (g_op_type_drop, g_op_type_inspect)
3067 	     IF (l_debug=1) THEN
3068 		print_debug(' Exit execution. Because operation plan does not exist, no need to proceed with ATF related code.', l_module_name,4);
3069 		END IF;
3070 
3071 	     RETURN;  -- Return because operation plan does not exist, no need to proceed with ATF related code
3072 
3073 	  END IF; --  IF l_mmtt_rec.operation_plan_id IS NULL
3074 
3075 
3076 
3077        END IF;  -- IF p_activity_id = g_op_activity_inbound
3078 
3079 
3080 
3081        -- validate operation type on active operation instance
3082        -- against operation type passed in by the user
3083 
3084        IF l_operation_type_id <> l_wooi_data_rec.operation_type_id THEN
3085 	  IF (l_debug = 1) THEN
3086 	     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);
3087 	  END IF;
3088 	  fnd_message.set_name('WMS', 'WMS_ATF_OPERATION_MISMATCH');
3089 	  fnd_msg_pub.ADD;
3090  	  RAISE FND_API.G_EXC_ERROR;
3091        END IF;
3092 
3093        IF (l_debug=1) THEN
3094 	  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);
3095        END IF;
3096 
3097        BEGIN
3098 	  l_progress := 104;
3099 
3100 	  SELECT MAX(wopd1.operation_sequence)
3101 	    INTO l_last_op_sequence
3102 	    FROM wms_op_plan_details wopd1
3103 	    WHERE wopd1.operation_plan_id =
3104 	    (SELECT wopd2.operation_plan_id
3105 	     FROM wms_op_plan_details wopd2
3106 	     WHERE wopd2.operation_plan_detail_id = l_wooi_data_rec.operation_plan_detail_id
3107 	     );
3108 
3109 	  l_progress := 106;
3110 
3111        EXCEPTION
3112 	  WHEN OTHERS THEN
3113 	     IF (l_debug=1) THEN
3114 		print_debug('Exception when getting max operation_sequence.' ,l_module_name,4);
3115 	     END IF;
3116 
3117 	     RAISE FND_API.G_EXC_ERROR;
3118        END;
3119 
3120 
3121        IF (l_debug=1) THEN
3122 	  print_debug('MAX operation_sequence (last step) is: '||l_last_op_sequence,l_module_name,4);
3123        END IF;
3124 
3125 
3126        -- Validate destination locator if this is not the last step of the plan
3127        IF p_activity_id = g_op_activity_inbound
3128 	 AND l_operation_type_id = g_op_type_drop
3129 	 AND l_wooi_data_rec.sug_to_locator_id IS NOT NULL
3130 	   AND l_last_op_sequence <> l_wooi_data_rec.operation_sequence
3131 	 THEN
3132 	  IF (l_debug=1) THEN
3133 	     print_debug('Inbound Activity and drop operation.',l_module_name,4);
3134 	  END IF;
3135 
3136 	  l_progress := 106.001;
3137 
3138 	  SELECT
3139 	    Nvl(lpn_controlled_flag, 2) -- 2 is non-lpn controlled
3140 	    INTO l_lpn_controlled_flag
3141 	    FROM mtl_secondary_inventories
3142 	    WHERE secondary_inventory_name = Nvl(l_mmtt_rec.transfer_subinventory,
3143 						 l_mmtt_rec.subinventory_code)
3144 	    AND organization_id = l_mmtt_rec.organization_id;
3145 
3146 	  l_progress := 106.002;
3147 
3148 	  IF (l_debug=1) THEN
3149 	     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);
3150 
3151 	  END IF;
3152 
3153 	  IF l_lpn_controlled_flag = 2 OR
3154 	    (l_inbound_doc_rec.content_lpn_id IS NULL AND
3155 	     l_inbound_doc_rec.transfer_lpn_id IS NULL) THEN
3156 	     IF (l_debug=1) THEN
3157 		print_debug('This sub is not LPN controlled, or user is dropping loose, this is an unforgivable error.',l_module_name,4);
3158 	     END IF;
3159 
3160 	     l_unforgivable_error_flag := 'Y';
3161 
3162 	  END IF;
3163 
3164 
3165 
3166 	  IF l_wooi_data_rec.sug_to_locator_id <> Nvl(l_mmtt_rec.transfer_to_location, l_mmtt_rec.locator_id)
3167 	    AND l_unforgivable_error_flag <> 'Y' THEN
3168 
3169 	     IF (l_debug=1) THEN
3170 		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);
3171 		print_debug('After user orverrides : '|| Nvl(l_mmtt_rec.transfer_to_location, l_mmtt_rec.locator_id)||' . Need to validate. ',l_module_name,4);
3172 	     END IF;
3173 
3174 	     l_progress := 108;
3175 
3176 	     SELECT
3177 	       loc_selection_criteria,
3178 	       loc_selection_api_id
3179 	       INTO
3180 	       l_loc_sel_criteria_id,
3181 	       l_loc_sel_api_id
3182 	       FROM wms_op_plan_details
3183 	       WHERE operation_plan_detail_id = l_wooi_data_rec.operation_plan_detail_id;
3184 	     l_progress := 110;
3185 
3186 	     IF (l_debug=1) THEN
3187 		print_debug('l_loc_sel_criteria_id = '||l_loc_sel_criteria_id,l_module_name,4);
3188 		print_debug('l_loc_sel_api_id = '||l_loc_sel_api_id,l_module_name,4);
3189 	     END IF;
3190 
3191 	     IF l_loc_sel_criteria_id = wms_globals.G_OP_DEST_PRE_SPECIFIED
3192 	       OR l_loc_sel_criteria_id = wms_globals.G_OP_DEST_SYS_SUGGESTED
3193 	       THEN
3194 
3195 		IF (l_debug=1) THEN
3196 		   print_debug('Unforgivable error: User overrides pre-specified or system-suggested locator, need to abort operation plan ',l_module_name,4);
3197 		END IF;
3198 		l_unforgivable_error_flag := 'Y';
3199 
3200 	      ELSIF l_loc_sel_criteria_id = wms_globals.G_OP_DEST_API
3201 		OR l_loc_sel_criteria_id = wms_globals.G_OP_DEST_CUSTOM_API
3202 		THEN
3203 
3204 		IF (l_debug=1) THEN
3205 		   print_debug('User overrides API derived locator, need to validate',l_module_name,4);
3206 
3207 		   print_debug('Before calling wms_atf_dest_locator.get_dest_locator with following parameters: ' , l_module_name, 4);
3208 		   print_debug('p_mode => ' || 2, l_module_name, 4);
3209 		   print_debug('p_task_id => ' || p_source_task_id, l_module_name, 4);
3210 		   print_debug('p_activity_type_id => ' || p_activity_id, l_module_name, 4);
3211 		   print_debug('p_hook_call_id => ' || l_loc_sel_api_id, l_module_name, 4);
3212 		   print_debug('p_locator_id => ' || Nvl(l_mmtt_rec.transfer_to_location, l_mmtt_rec.locator_id), l_module_name, 4);
3213 		   print_debug('p_item_id => ' || l_inbound_doc_rec.inventory_item_id, l_module_name, 4);
3214 		END IF;
3215 
3216 		wms_atf_dest_locator.get_dest_locator
3217 		  (
3218 		    x_return_status         => l_return_status
3219 		    ,  x_msg_count          => l_msg_count
3220 		    ,  x_msg_data           => l_msg_data
3221 		    ,  x_locator_id         => l_locator_id_dummy
3222 		    ,  x_subinventory_code  => l_subinventory_code_dummy
3223 		    ,  x_zone_id            => l_zone_id_dummy
3224 		    ,  x_loc_valid          => l_valid_flag
3225 		    ,  p_mode               => 2
3226 		    ,  p_task_id            => p_source_task_id
3227 		    ,  p_activity_type_id   => p_activity_id
3228 		    ,  p_hook_call_id       => l_loc_sel_api_id
3229 		    ,  p_locator_id         => Nvl(l_inbound_doc_rec.transfer_to_location, l_inbound_doc_rec.locator_id)
3230 		    ,  p_item_id            => l_inbound_doc_rec.inventory_item_id
3231 		    ,  p_api_version        => NULL
3232 		    ,  p_init_msg_list      => NULL
3233 		    ,  p_commit             => NULL
3234 		    );
3235 
3236 		IF (l_debug = 1) THEN
3237 		   print_debug('After calling wms_atf_dest_locator.get_dest_locator.' , l_module_name, 4);
3238 		   print_debug('x_return_status => ' || l_return_status, l_module_name, 4);
3239 		   print_debug('x_msg_count => ' || l_msg_count, l_module_name, 4);
3240 		   print_debug('x_msg_data => ' || l_msg_data, l_module_name, 4);
3241 		   print_debug('x_loc_valid => ' || l_valid_flag, l_module_name, 4);
3242 		END IF;
3243 
3244 		/*  When calling wms_atf_dest_locator.get_dest_locator for locator validation,
3245 		we do not check for x_return_status, because it will return E if it cannot find any locator
3246 
3247 		IF l_return_status <> g_ret_sts_success THEN
3248 		   IF (l_debug = 1) THEN
3249 		      print_debug('wms_atf_dest_locator.get_dest_locator failed:  l_return_status = '|| l_return_status, l_module_name, 4);
3250 		   END IF;
3251 
3252 		   RAISE FND_API.G_EXC_ERROR;
3253 
3254 		END IF;
3255 		  */
3256 		IF l_valid_flag = 'E' THEN
3257 		   IF (l_debug = 1) THEN
3258 		      print_debug('Invalid locator according to API, need to abort operation plan '|| l_return_status, l_module_name, 4);
3259 		   END IF;
3260 
3261 		   l_unforgivable_error_flag := 'Y';
3262 
3263 		END IF;
3264 
3265 	      ELSE
3266 
3267 		IF (l_debug=1) THEN
3268 		   print_debug('Invalid locator determination method.',l_module_name,4);
3269 		END IF;
3270 		fnd_message.set_name('WMS', 'WMS_ATF_LOC_DET_NOT_DEFINED');
3271 		fnd_msg_pub.ADD;
3272 		RAISE FND_API.G_EXC_ERROR;
3273 
3274 	     END IF;  -- IF NVL(l_loc_sel_criteria_id, 0) = wms_globals.G_OP_DEST_PRE_SPECIFIED
3275 
3276 	  END IF; -- IF l_wooi_data_rec.sug_to_locator_id <> Nvl(l_mmtt_rec.transfer_to_location
3277 
3278 	  IF l_unforgivable_error_flag = 'Y' THEN
3279 
3280 	     IF (l_debug=1) THEN
3281 		print_debug('Unforgivable error occured, abort operation plan, and complete the document execution',l_module_name,9);
3282 
3283 		print_debug('Before calling WMS_ATF_RUNTIME_PUB_APIS.abort_operation_plan with following parameters : ',l_module_name,9);
3284 		print_debug('p_source_task_id => '||p_source_task_id,l_module_name,9);
3285 		print_debug('p_activity_id => '||p_activity_id,l_module_name,9);
3286 	     END IF;
3287 
3288 	     wms_atf_runtime_pub_apis.abort_operation_plan
3289 	       (
3290 		x_return_status      => l_return_status
3291 		, x_msg_data         => l_msg_data
3292 		, x_msg_count        => l_msg_count
3293 		, x_error_code       => l_atf_error_code
3294 		, p_source_task_id   => p_source_task_id
3295 		, p_activity_type_id => p_activity_id);
3296 
3297 	     IF (l_debug=1) THEN
3298 		print_debug('After calling wms_atf_runtime_pub_apis.abort_operation_plan. ' ,l_module_name,9);
3299 		print_debug('x_return_status => '||l_return_status,l_module_name,9);
3300 		print_debug('x_msg_data => '||l_msg_data,l_module_name,9);
3301 		print_debug('x_msg_count => '||l_msg_count,l_module_name,9);
3302 		print_debug('x_error_code => '||l_atf_error_code,l_module_name,9);
3303 	     END IF;
3304 
3305 	     IF l_return_status <>FND_API.g_ret_sts_success THEN
3306 		IF (l_debug=1) THEN
3307 		   print_debug('wms_atf_runtime_pub_apis.abort_operation_plan finished with error. l_return_status = ' || l_return_status,l_module_name,4);
3308 		END IF;
3309 
3310 		RAISE FND_API.G_EXC_ERROR;
3311 	     END IF;
3312 
3313 
3314 	     IF (l_debug=1) THEN
3315 
3316 		print_debug('Unforgivable error - Before calling wms_op_inbound_pvt.complete with following parameters:',l_module_name,4);
3317 		print_debug('p_source_task_id => '|| p_source_task_id,l_module_name,4);
3318 		print_debug('p_document_rec.transaction_temp_id => '|| l_mmtt_rec.transaction_temp_id,l_module_name,4);
3319 		print_debug('p_operation_type_id => '|| l_operation_type_id,l_module_name,4);
3320 		print_debug('p_next_operation_type_id => '|| '',l_module_name,4);
3321 		print_debug('p_is_last_operation_flag => '|| 'Y',l_module_name,4);
3322 
3323 	     END IF;
3324 
3325 
3326 	     wms_op_inbound_pvt.complete
3327 	       (
3328 		x_return_status              => l_return_status
3329 		, x_msg_data                 => l_msg_data
3330 		, x_msg_count                => l_msg_count
3331 		, x_source_task_id           => l_new_mmtt_id
3332 		, x_error_code               => l_atf_error_code
3333 		, p_source_task_id           => p_source_task_id
3334 		, p_document_rec             => l_mmtt_rec
3335 		, p_operation_type_id        => l_operation_type_id
3336 		, p_next_operation_type_id   => NULL
3337 		, p_sug_to_sub_code	     => l_wooi_data_rec.sug_to_sub_code
3338 		, p_sug_to_locator_id	     => l_wooi_data_rec.sug_to_locator_id
3339 		, p_is_last_operation_flag   => 'Y'
3340 		);
3341 
3342 
3343 	     IF (l_debug=1) THEN
3344 		print_debug('Unforgivable Error - After calling wms_op_inbound_pvt.complete. ', l_module_name,4);
3345 
3346 		print_debug('x_return_status => '||l_return_status, l_module_name,4);
3347 		print_debug('x_msg_data => '||l_msg_data, l_module_name,4);
3348 		print_debug('x_msg_count => '||l_msg_count, l_module_name,4);
3349 		print_debug('x_source_task_id => '||l_new_mmtt_id, l_module_name,4);
3350 		print_debug('x_error_code => '||l_atf_error_code, l_module_name,4);
3351 	     END IF;
3352         --bug 6924639
3353          IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3354         IF (l_debug = 1) THEN
3355            print_debug('Unforgivable Error - wms_op_inbound_pvt.complete finished with error. l_return_status = ' || l_return_status,l_module_name,4);
3356         END IF;
3357 
3358         fnd_message.set_name('WMS', 'WMS_WORK_FLOW_FAIL');
3359         fnd_msg_pub.ADD;
3360         RAISE fnd_api.g_exc_unexpected_error;
3361       ELSIF l_return_status = fnd_api.g_ret_sts_error THEN
3362         IF (l_debug = 1) THEN
3363           print_debug('Unforgivable Error - wms_op_inbound_pvt.complete finished with error. l_return_status = ' || l_return_status,l_module_name,4);
3364         END IF;
3365 
3366         fnd_message.set_name('WMS', 'WMS_WORK_FLOW_FAIL');
3367         fnd_msg_pub.ADD;
3368         RAISE fnd_api.g_exc_error;
3369       END IF;
3370       --bug 6924639
3371 	     /*
3372 	     IF l_return_status <>FND_API.g_ret_sts_success THEN
3373 		IF (l_debug=1) THEN
3374 		   print_debug('Unforgivable Error - wms_op_inbound_pvt.complete finished with error. l_return_status = ' || l_return_status,l_module_name,4);
3375 		END IF;
3376 
3377 		RAISE FND_API.G_EXC_ERROR;
3378 	     END IF;
3379 	     */
3380 
3381 	     IF c_inbound_document_details%isopen THEN
3382 		CLOSE c_inbound_document_details;
3383 	     END IF;
3384 
3385 	     IF c_wooi_data_rec%isopen THEN
3386 		CLOSE c_wooi_data_rec;
3387 	     END IF;
3388 
3389 	     IF c_next_plan_detail%isopen THEN
3390 		CLOSE c_next_plan_detail;
3391 	     END IF;
3392 
3393 	     RETURN;  -- return for unforgivable error
3394 
3395 	  END IF;  -- IF l_unforgivable_error_flag = 'Y' THEN
3396 
3397 	  l_progress:=115;
3398 
3399 
3400        END IF; --  IF p_activity_id = g_op_activity_inbound
3401 
3402 
3403        -- Update the current operation instance to complete
3404        -- And populate other data for a completed operation instance:
3405        -- 'load from' sub/loc for load operation
3406        -- 'drop to' sub/loc for drop operation
3407        --
3408        -- For a receiving delivery the drop to sub/loc are populated in subinventory_code and locator_id
3409        -- while receiving transfer and inventory transfer they are populated in transfer_subinventory and transfer_to_location
3410        --
3411        -- It is not nessary to populate equipment, employee data, which should
3412        -- has been done in activate_operation_instance
3413 
3414        l_wooi_rec.operation_instance_id := l_wooi_data_rec.operation_instance_id;
3415        l_wooi_rec.operation_status := g_op_ins_stat_completed;
3416        l_wooi_rec.complete_time := Sysdate;
3417 
3418        IF (l_debug=1) THEN
3419 	  print_debug('Before updating WOOI: l_operation_type_id = ' ||l_operation_type_id,l_module_name,4);
3420        END if;
3421 
3422        IF l_operation_type_id = g_op_type_drop THEN
3423 	  l_wooi_rec.to_subinventory_code := Nvl(l_mmtt_rec.transfer_subinventory, l_mmtt_rec.subinventory_code);
3424 	  l_wooi_rec.to_locator_id := Nvl(l_mmtt_rec.transfer_to_location, l_mmtt_rec.locator_id);
3425 	ELSIF l_operation_type_id IN (g_op_type_inspect, g_op_type_load) THEN
3426 	  l_wooi_rec.from_subinventory_code := l_mmtt_rec.subinventory_code;
3427 	  l_wooi_rec.from_locator_id := l_mmtt_rec.locator_id;
3428        END IF;  -- IF l_operation_type_id = g_op_type_drop T
3429 
3430 
3431        IF (l_debug=1) THEN
3432 	  print_debug('Before calling wms_op_runtime_pvt_apis.update_operation_instance with following parameters: ',l_module_name,4);
3433 	  print_debug('l_wooi_rec.operation_instance_id => '|| l_wooi_rec.operation_instance_id,l_module_name,4);
3434 	  print_debug('l_wooi_rec.operation_status => '|| l_wooi_rec.operation_status,l_module_name,4);
3435 	  print_debug('l_wooi_rec.complete_time => '|| To_char(l_wooi_rec.complete_time),l_module_name,4);
3436 	  print_debug('l_wooi_rec.to_subinventory_code => '|| l_wooi_rec.to_subinventory_code,l_module_name,4);
3437 	  print_debug('l_wooi_rec.to_locator_id => '|| l_wooi_rec.to_locator_id,l_module_name,4);
3438 	  print_debug('l_wooi_rec.from_subinventory_code => '|| l_wooi_rec.from_subinventory_code,l_module_name,4);
3439 	  print_debug('l_wooi_rec.from_locator_id => '|| l_wooi_rec.from_locator_id,l_module_name,4);
3440        END IF;
3441 
3442        l_progress := 120;
3443 
3444        wms_op_runtime_pvt_apis.update_operation_instance
3445 	 (x_return_status     => l_return_status
3446 	  , x_msg_count       => l_msg_count
3447 	  , x_msg_data        => l_msg_data
3448 	  , p_update_rec      => l_wooi_rec);
3449 
3450        l_progress := 130;
3451 
3452        IF (l_debug=1) THEN
3453 	  print_debug('After calling wms_op_runtime_pvt_apis.update_operation_instance: ' ,l_module_name,4);
3454        	  print_debug('x_return_status => '|| l_return_status,l_module_name,4);
3455 	  print_debug('x_msg_count => '|| l_msg_count,l_module_name,4);
3456 	  print_debug('x_msg_data => '|| l_msg_data,l_module_name,4);
3457        END IF;
3458 
3459        IF l_return_status <>FND_API.g_ret_sts_success THEN
3460 	  IF (l_debug=1) THEN
3461 	     print_debug('wms_op_runtime_pvt_apis.update_operation_instance finished with error. l_return_status = ' || l_return_status,l_module_name,4);
3462 	  END IF;
3463 
3464 	  RAISE FND_API.G_EXC_ERROR;
3465        END IF;
3466 
3467 
3468        -- Handle situation where current operation is the last step in the plan.
3469 
3470        IF l_last_op_sequence = l_wooi_data_rec.operation_sequence  -- current operation sequence equals the max sequence in the plan
3471 	 AND l_operation_type_id <> g_op_type_load -- and this is not the load operation for a crossdock
3472 	 THEN
3473 
3474 	  -- this is the last operation in the plan
3475 	  IF (l_debug=1) THEN
3476 	     print_debug('Current operation is the last step of the operation plan.',l_module_name,4);
3477 	     print_debug('Before calling wms_op_runtime_pvt_apis.archive_dispatched_tasks with following parameters : ' , l_module_name,4);
3478 	     print_debug('p_task_id             => '||'', l_module_name,4);
3479 	     print_debug('p_source_task_id      => '||l_mmtt_rec.parent_line_id, l_module_name,4);
3480 	     print_debug('p_activity_type_id    => '||p_activity_id, l_module_name,4);
3481 	     print_debug('p_op_plan_instance_id => '||l_wooi_data_rec.op_plan_instance_id, l_module_name,4);
3482         print_debug('p_op_plan_status      => '||G_OP_INS_STAT_COMPLETED,l_module_name,4);
3483 	  END IF;
3484 
3485 	  -- Need to archive the parent MMTT record into WDTH
3486 	  wms_op_runtime_pvt_apis.archive_dispatched_tasks
3487 	    (
3488 	     x_return_status          => l_return_status
3489 	     , x_msg_count            => l_msg_count
3490 	     , x_msg_data             => l_msg_data
3491 	     , p_task_id              => NULL
3492 	     , p_source_task_id       => l_mmtt_rec.parent_line_id
3493 	     , p_activity_type_id     => p_activity_id
3494 	     , p_op_plan_instance_id  => l_wooi_data_rec.op_plan_instance_id
3495         , p_op_plan_status       => G_OP_INS_STAT_COMPLETED
3496 	     );
3497 
3498 	  IF (l_debug=1) THEN
3499 	     print_debug('After calling wms_op_runtime_pvt_apis.archive_dispatched_tasks.',l_module_name,4);
3500 	     print_debug('x_return_status => '|| l_return_status,l_module_name,4);
3501 	     print_debug('x_msg_count => '|| l_msg_count,l_module_name,4);
3502 	     print_debug('x_msg_data => '|| l_msg_data,l_module_name,4);
3503 
3504 	  END IF;
3505 
3506 	  IF l_return_status <>FND_API.g_ret_sts_success THEN
3507 	     IF (l_debug=1) THEN
3508 		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);
3509 	     END IF;
3510 	     fnd_message.set_name('WMS', 'WMS_ATF_ARCHIVE_TASK_FAILURE');
3511 	     fnd_msg_pub.ADD;
3512 	     RAISE FND_API.G_EXC_ERROR;
3513 	  END IF;
3514 
3515 	  IF (l_debug=1) THEN
3516 	     print_debug('Before calling wms_op_runtime_pvt_apis.complete_plan_instance with following parameters:', l_module_name,4);
3517 	     print_debug('p_op_plan_instance_id => '||l_wooi_data_rec.op_plan_instance_id, l_module_name,4);
3518 	  END IF;
3519 
3520 	  -- Complete the operation plan instance
3521 	  l_progress := 160;
3522 
3523 	  wms_op_runtime_pvt_apis.complete_plan_instance
3524 	    (x_return_status        => l_return_status,
3525 	     x_msg_data             => l_msg_data,
3526 	     x_msg_count            => l_msg_count,
3527 	     p_op_plan_instance_id  => l_wooi_data_rec.op_plan_instance_id
3528 	     );
3529 
3530 	  l_progress := 170;
3531 
3532 	  IF (l_debug=1) THEN
3533 	     print_debug('After calling wms_op_runtime_pvt_apis.complete_plan_instance. ', l_module_name,4);
3534 
3535 	     print_debug('x_return_status => '||l_return_status, l_module_name,4);
3536 	     print_debug('x_msg_data => '||l_msg_data, l_module_name,4);
3537 	     print_debug('x_msg_count => '||l_msg_count, l_module_name,4);
3538 	  END IF;
3539 
3540 	  IF l_return_status <>FND_API.g_ret_sts_success THEN
3541 	     IF (l_debug=1) THEN
3542 		print_debug('wms_op_runtime_pvt_apis.complete_plan_instance finished with error. l_return_status = ' || l_return_status,l_module_name,4);
3543 	     END IF;
3544 
3545 	     RAISE FND_API.G_EXC_ERROR;
3546 	  END IF;
3547 
3548 
3549 
3550 	  -- Complete the document
3551 
3552 	  IF p_activity_id = g_op_activity_inbound THEN
3553 
3554 
3555 	     IF (l_debug=1) THEN
3556 
3557 		print_debug('Last operation in plan and inbound - Before calling wms_op_inbound_pvt.complete with following parameters:',l_module_name,4);
3558 		print_debug('p_source_task_id => '|| p_source_task_id,l_module_name,4);
3559 		print_debug('p_document_rec.transaction_temp_id => '|| l_mmtt_rec.transaction_temp_id,l_module_name,4);
3560 		print_debug('p_operation_type_id => '|| l_operation_type_id,l_module_name,4);
3561 		print_debug('p_next_operation_type_id => '|| '',l_module_name,4);
3562 		print_debug('p_is_last_operation_flag => '|| 'Y',l_module_name,4);
3563 
3564 	     END IF;
3565 
3566 	     l_progress :=  180;
3567 
3568 	     wms_op_inbound_pvt.complete
3569 	       (
3570 		x_return_status              => l_return_status
3571 		, x_msg_data                 => l_msg_data
3572 		, x_msg_count                => l_msg_count
3573 		, x_source_task_id           => l_new_mmtt_id
3574 		, x_error_code               => l_atf_error_code
3575 		, p_source_task_id           => p_source_task_id
3576 		, p_document_rec             => l_mmtt_rec
3577 		, p_operation_type_id        => l_operation_type_id
3578 		, p_next_operation_type_id   => NULL
3579 		, p_sug_to_sub_code	     => l_wooi_data_rec.sug_to_sub_code
3580 		, p_sug_to_locator_id	     => l_wooi_data_rec.sug_to_locator_id
3581 		, p_is_last_operation_flag   => 'Y'
3582 		);
3583 
3584 	     l_progress :=  190;
3585 
3586 	     IF (l_debug=1) THEN
3587 		print_debug('Last operation in plan - After calling wms_op_inbound_pvt.complete. ', l_module_name,4);
3588 
3589 		print_debug('x_return_status => '||l_return_status, l_module_name,4);
3590 		print_debug('x_msg_data => '||l_msg_data, l_module_name,4);
3591 		print_debug('x_msg_count => '||l_msg_count, l_module_name,4);
3592 		print_debug('x_source_task_id => '||l_new_mmtt_id, l_module_name,4);
3593 		print_debug('x_error_code => '||l_atf_error_code, l_module_name,4);
3594 	     END IF;
3595         --bug 6924639
3596 	     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3597         IF (l_debug = 1) THEN
3598           print_debug('Inbound op plan null - wms_op_inbound_pvt.complete finished with error. l_return_status = ' || l_return_status,l_module_name,4);
3599         END IF;
3600 
3601         fnd_message.set_name('WMS', 'WMS_WORK_FLOW_FAIL');
3602         fnd_msg_pub.ADD;
3603         RAISE fnd_api.g_exc_unexpected_error;
3604       ELSIF l_return_status = fnd_api.g_ret_sts_error THEN
3605         IF (l_debug = 1) THEN
3606           print_debug('Inbound op plan null - wms_op_inbound_pvt.complete finished with error. l_return_status = ' || l_return_status,l_module_name,4);
3607         END IF;
3608 
3609         fnd_message.set_name('WMS', 'WMS_WORK_FLOW_FAIL');
3610         fnd_msg_pub.ADD;
3611         RAISE fnd_api.g_exc_error;
3612       END IF;
3613       --bug 6924639
3614         /*
3615 	     IF l_return_status <>FND_API.g_ret_sts_success THEN
3616 		IF (l_debug=1) THEN
3617 		   print_debug('Last operation in plan - wms_op_inbound_pvt.complete finished with error. l_return_status = ' || l_return_status,l_module_name,4);
3618 		END IF;
3619 
3620 		RAISE FND_API.G_EXC_ERROR;
3621 	     END IF;
3622 	  */
3623 	  END IF; -- IF p_activity_id = g_op_activity_inbound
3624 
3625 	ELSE -- this is NOT the last operation in the plan
3626 	  IF (l_debug=1) THEN
3627 	     print_debug('Current operation is NOT the last step of the operation plan.',l_module_name,4);
3628 	  END IF;
3629 
3630 	  -- query operation plan detail for the next operation
3631 	  OPEN c_next_plan_detail(l_wooi_data_rec.operation_plan_id,
3632 				  l_wooi_data_rec.operation_sequence);
3633 
3634 	  FETCH c_next_plan_detail INTO l_operation_plan_detail_rec;
3635 	  IF c_next_plan_detail%notfound THEN
3636 	     IF (l_debug=1) THEN
3637 		print_debug('Current operation does not have a next operation.' ,l_module_name,4);
3638 	     END IF;
3639 
3640 	     fnd_message.set_name('WMS', 'WMS_ATF_NO_NEXT_OPERATION');
3641 	     fnd_msg_pub.ADD;
3642 
3643 	     RAISE FND_API.G_EXC_ERROR;
3644 
3645 	  END IF;
3646 
3647 	  CLOSE c_next_plan_detail;
3648 
3649 	  -- construct operation instance record for the next operation in the plan
3650 
3651 	  l_wooi_rec := NULL;
3652 
3653 	  l_wooi_rec.operation_plan_detail_id := l_operation_plan_detail_rec.operation_plan_detail_id;
3654 	  l_wooi_rec.op_plan_instance_id      := l_wooi_data_rec.op_plan_instance_id ;
3655 	  l_wooi_rec.organization_id          := l_wooi_data_rec.organization_id;
3656 	  l_wooi_rec.operation_status         := g_op_ins_stat_pending;
3657 	  l_wooi_rec.operation_sequence       := l_operation_plan_detail_rec.operation_sequence; -- for crossdock, load and drop wooi will have the same sequence.
3658 	  l_wooi_rec.is_in_inventory          := Nvl(l_operation_plan_detail_rec.is_in_inventory, 'N');
3659 	  l_wooi_rec.from_subinventory_code   := Nvl(l_mmtt_rec.transfer_subinventory, l_mmtt_rec.subinventory_code);
3660 	  l_wooi_rec.from_locator_id          := Nvl(l_mmtt_rec.transfer_to_location, l_mmtt_rec.locator_id);
3661 	  l_wooi_rec.activity_type_id         := p_activity_id;
3662 	  -- Need to set next operation_type properly.
3663 	  -- Current operation 'Load' is always followed by a 'Drop'.
3664 	  -- Otherwise, If WOPD definition for next step is 'crossdock', need to create a 'Load' operation instance.
3665 	  -- Otherwise, use WOPD's operation type
3666 
3667 	  IF l_operation_type_id = g_op_type_load THEN
3668 	     l_wooi_rec.operation_type_id := g_op_type_drop;
3669 	   ELSIF l_operation_plan_detail_rec.operation_type = g_op_type_crossdock THEN
3670 	     l_wooi_rec.operation_type_id := g_op_type_load;
3671 	   ELSE -- other operation type
3672 	     l_wooi_rec.operation_type_id := l_operation_plan_detail_rec.operation_type;
3673 	  END IF; -- IF l_operation_type_id = g_op_type_load
3674 
3675 
3676 	  IF (l_debug=1) THEN
3677 	     print_debug('l_wooi_rec.operation_plan_detail_id = '||l_wooi_rec.operation_plan_detail_id,l_module_name,4);
3678 	     print_debug('l_wooi_rec.op_plan_instance_id = '||l_wooi_rec.op_plan_instance_id,l_module_name,4);
3679 	     print_debug('l_wooi_rec.organization_id = '||l_wooi_rec.organization_id,l_module_name,4);
3680 	     print_debug('l_wooi_rec.operation_status = '||l_wooi_rec.operation_status,l_module_name,4);
3681 	     print_debug('l_wooi_rec.operation_sequence = '||l_wooi_rec.operation_sequence,l_module_name,4);
3682 	     print_debug('l_wooi_rec.is_in_inventory = '||l_wooi_rec.is_in_inventory,l_module_name,4);
3683 	     print_debug('l_wooi_rec.from_subinventory_code = '||l_wooi_rec.from_subinventory_code,l_module_name,4);
3684 	     print_debug('l_wooi_rec.from_locator_id = '||l_wooi_rec.from_locator_id,l_module_name,4);
3685 	     print_debug('l_wooi_rec.operation_type_id = '||l_wooi_rec.operation_type_id,l_module_name,4);
3686 	     print_debug('l_wooi_rec.activity_type_id = '||l_wooi_rec.activity_type_id,l_module_name,4);
3687 	     print_debug('l_operation_plan_detail_rec.subsequent_op_plan_id        ==>'||l_operation_plan_detail_rec.subsequent_op_plan_id,l_module_name,4);
3688 
3689 	  END IF;
3690 
3691 	  -- update document table
3692 
3693 	  IF p_activity_id = g_op_activity_inbound THEN
3694 
3695 
3696 	     IF (l_debug=1) THEN
3697 		print_debug('NOT Last operation in plan and inbound - Before calling wms_op_inbound_pvt.complete with following parameters:',l_module_name,4);
3698 		print_debug('p_source_task_id => '|| p_source_task_id,l_module_name,4);
3699 		print_debug('p_document_rec.transaction_temp_id => '|| l_mmtt_rec.transaction_temp_id,l_module_name,4);
3700 		print_debug('p_operation_type_id => '|| l_operation_type_id,l_module_name,4);
3701 		print_debug('p_next_operation_type_id => '|| l_operation_plan_detail_rec.operation_type, l_module_name,4);
3702 		print_debug('p_is_last_operation_flag => '|| 'N',l_module_name,4);
3703 	     END IF;
3704 
3705 	     l_progress :=  220;
3706 	     /*{{
3707 		 When completing the drop operation right before the crossdock operation
3708 		 need to stamp subsequent outbound operation plan ID to the last task
3709 
3710 	     }}
3711 	       */
3712 
3713 	     wms_op_inbound_pvt.complete
3714 	       (
3715 		x_return_status              => l_return_status
3716 		, x_msg_data                 => l_msg_data
3717 		, x_msg_count                => l_msg_count
3718 		, x_source_task_id           => l_new_mmtt_id
3719 		, x_error_code               => l_atf_error_code
3720 		, p_source_task_id           => p_source_task_id
3721 		, p_document_rec             => l_mmtt_rec
3722 		, p_operation_type_id        => l_operation_type_id
3723 		, p_next_operation_type_id   => l_operation_plan_detail_rec.operation_type
3724 		, p_sug_to_sub_code	     => l_wooi_data_rec.sug_to_sub_code
3725 		, p_sug_to_locator_id	     => l_wooi_data_rec.sug_to_locator_id
3726 		, p_is_last_operation_flag   => 'N'
3727 		, p_subsequent_op_plan_id    => l_operation_plan_detail_rec.subsequent_op_plan_id
3728 		);
3729 
3730 	     l_progress :=  230;
3731 
3732 	     IF (l_debug=1) THEN
3733 		print_debug('NOT Last operation in plan - After calling wms_op_inbound_pvt.complete. ', l_module_name,4);
3734 
3735 		print_debug('x_return_status => '||l_return_status, l_module_name,4);
3736 		print_debug('x_msg_data => '||l_msg_data, l_module_name,4);
3737 		print_debug('x_msg_count => '||l_msg_count, l_module_name,4);
3738 		print_debug('x_source_task_id => '||l_new_mmtt_id, l_module_name,4);
3739 		print_debug('x_error_code => '||l_atf_error_code, l_module_name,4);
3740 	     END IF;
3741 	     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3742         IF (l_debug = 1) THEN
3743           print_debug('Inbound op plan null - wms_op_inbound_pvt.complete finished with error. l_return_status = ' || l_return_status,l_module_name,4);
3744         END IF;
3745 
3746         fnd_message.set_name('WMS', 'WMS_WORK_FLOW_FAIL');
3747         fnd_msg_pub.ADD;
3748         RAISE fnd_api.g_exc_unexpected_error;
3749       ELSIF l_return_status = fnd_api.g_ret_sts_error THEN
3750         IF (l_debug = 1) THEN
3751           print_debug('Inbound op plan null - wms_op_inbound_pvt.complete finished with error. l_return_status = ' || l_return_status,l_module_name,4);
3752         END IF;
3753 
3754         fnd_message.set_name('WMS', 'WMS_WORK_FLOW_FAIL');
3755         fnd_msg_pub.ADD;
3756         RAISE fnd_api.g_exc_error;
3757       END IF;
3758 
3759 
3760 	  END IF; -- IF p_activity_id = g_op_activity_inbound
3761 
3762 	  l_wooi_rec.source_task_id := Nvl(l_new_mmtt_id, p_source_task_id);
3763 
3764 	  IF (l_debug=1) THEN
3765 	     print_debug('l_wooi_rec.source_task_id = '||l_wooi_rec.source_task_id,l_module_name,4);
3766 	  END IF;
3767 
3768 	  -- create next operation instance
3769 
3770 	  IF (l_debug=1) THEN
3771 	     print_debug('Before calling wms_op_runtime_pvt_apis.insert_operation_instance:' ,l_module_name,4);
3772 	  END IF;
3773 
3774 	  wms_op_runtime_pvt_apis.insert_operation_instance
3775 	    (
3776 	     x_return_status => l_return_status        ,
3777 	     x_msg_count     => x_msg_count            ,
3778 	     x_msg_data      => x_msg_data             ,
3779 	     p_insert_rec    => l_wooi_rec );
3780 
3781 	  IF (l_debug=1) THEN
3782 	     print_debug('After calling wms_op_runtime_pvt_apis.insert_operation_instance:' ,l_module_name,4);
3783 	  END IF;
3784 
3785 	  IF l_return_status <>FND_API.g_ret_sts_success THEN
3786 	     IF (l_debug=1) THEN
3787 		print_debug('wms_op_runtime_pvt_apis.insert_operation_instance finished with error. l_return_status = ' || l_return_status,l_module_name,4);
3788 	     END IF;
3789 
3790 	     RAISE FND_API.G_EXC_ERROR;
3791 	  END IF;
3792 
3793 
3794        END IF;  -- IF l_last_op_sequence = l_wooi_data_rec.operation_sequence
3795 
3796 
3797        IF (l_debug = 1) THEN
3798 	  print_debug('x_return_status ==> '||x_return_status,l_module_name,3);
3799 	  print_debug('x_msg_data ==> '||x_msg_data,l_module_name,3);
3800 	  print_debug('x_msg_count ==> '||x_msg_count,l_module_name,3);
3801 	  print_debug('x_error_code ==> '||x_error_code,l_module_name,3);
3802 
3803 	  print_debug(' Before Exiting . ',l_module_name,3);
3804        END IF;
3805 
3806   EXCEPTION
3807 
3808      WHEN fnd_api.g_exc_error THEN
3809 	IF (l_debug=1) THEN
3810 	   print_debug('Error (fnd_api.g_exc_error) occured at'||l_progress,l_module_name,1);
3811 	END IF;
3812 	x_return_status:=FND_API.G_RET_STS_ERROR;
3813 	fnd_message.set_name('WMS', 'WMS_ATF_COMPLETE_OP_FAILURE');
3814 	fnd_msg_pub.ADD;
3815 --	fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
3816 	ROLLBACK TO sp_complete_oprtn_instance;
3817 
3818 	IF (SQLCODE<-20000) THEN
3819 	   IF (l_debug=1) THEN
3820               print_debug('This is a user defined exception',l_module_name,1);
3821 	   END IF;
3822 
3823 	   x_error_code:=-(SQLCODE+20000);
3824 	END IF;
3825 
3826 
3827 	IF c_inbound_document_details%isopen THEN
3828 	   CLOSE c_inbound_document_details;
3829 	END IF;
3830 
3831 	IF c_wooi_data_rec%isopen THEN
3832 	   CLOSE c_wooi_data_rec;
3833 	END IF;
3834 
3835 	IF c_next_plan_detail%isopen THEN
3836 	   CLOSE c_next_plan_detail;
3837 	END IF;
3838 
3839 
3840      WHEN fnd_api.g_exc_unexpected_error THEN
3841 
3842 	x_return_status:=FND_API.G_RET_STS_UNEXP_ERROR;
3843 	IF (l_debug=1) THEN
3844 	   print_debug('Unexpected Error (fnd_api.g_exc_unexpected_error) occured at '||l_progress,l_module_name,3);
3845 	END IF;
3846 	IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
3847 	   fnd_msg_pub.add_exc_msg(g_pkg_name, l_module_name);
3848 	END IF;
3849 	--	fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
3850 	fnd_message.set_name('WMS', 'WMS_ATF_COMPLETE_OP_FAILURE');
3851 	fnd_msg_pub.ADD;
3852 
3853 	IF (SQLCODE<-20000) THEN
3854 	   IF (l_debug=1) THEN
3855               print_debug('This is a user defined exception',l_module_name,1);
3856 	   END IF;
3857 
3858 	   x_error_code:=-(SQLCODE+20000);
3859 	END IF;
3860 
3861 	ROLLBACK TO sp_complete_oprtn_instance;
3862 
3863 	IF c_inbound_document_details%isopen THEN
3864 	   CLOSE c_inbound_document_details;
3865 	END IF;
3866 
3867 	IF c_wooi_data_rec%isopen THEN
3868 	   CLOSE c_wooi_data_rec;
3869 	END IF;
3870 
3871 	IF c_next_plan_detail%isopen THEN
3872 	   CLOSE c_next_plan_detail;
3873 	END IF;
3874 
3875      WHEN OTHERS THEN
3876 
3877 	x_return_status:=FND_API.G_RET_STS_UNEXP_ERROR;
3878 	IF (l_debug=1) THEN
3879 	   print_debug('Other Error occured at '||l_progress,l_module_name,1);
3880 	   IF SQLCODE IS NOT NULL AND SQLCODE >= -20000
3881 	     THEN
3882 	      print_debug('With SQL error : ' || SQLERRM(SQLCODE), l_module_name,1);
3883 	   END IF;
3884 	END IF;
3885 
3886 	IF (SQLCODE<-20000) THEN
3887 	   IF (l_debug=1) THEN
3888               print_debug('This is a user defined exception',l_module_name,1);
3889 	   END IF;
3890 
3891 	   x_error_code:=-(SQLCODE+20000);
3892 	END IF;
3893 
3894 	IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
3895 	   fnd_msg_pub.add_exc_msg(g_pkg_name, l_module_name);
3896 	END IF;
3897 	--	fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
3898 	fnd_message.set_name('WMS', 'WMS_ATF_COMPLETE_OP_FAILURE');
3899 	fnd_msg_pub.ADD;
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  END Complete_OPERATION_INSTANCE;
3916 
3917   /*
3918   *    Following is overloaded procedure
3919     *  without x_crossdock_prim_quantity and x_crossdock_flag.
3920     *  This is for the convenience of existing RCV call.
3921   */
3922 
3923  PROCEDURE validate_operation
3924     (
3925      x_return_status         OUT  NOCOPY  VARCHAR2 ,
3926      x_msg_data              OUT  NOCOPY  FND_NEW_MESSAGES.MESSAGE_TEXT%TYPE ,
3927      x_msg_count             OUT  NOCOPY  NUMBER,
3928      x_error_code            OUT  NOCOPY  NUMBER ,
3929      x_inspection_flag       OUT  NOCOPY  NUMBER ,
3930      x_load_flag             OUT  NOCOPY  NUMBER ,
3931      x_drop_flag             OUT  NOCOPY  NUMBER ,
3932      x_load_prim_quantity    OUT  NOCOPY  NUMBER ,
3933      x_drop_prim_quantity    OUT  NOCOPY  NUMBER ,
3934      x_inspect_prim_quantity OUT  NOCOPY  NUMBER ,
3935      p_source_task_id        IN           NUMBER ,
3936      p_move_order_line_id    IN           NUMBER ,
3937      p_inventory_item_id     IN           NUMBER ,
3938      p_LPN_ID                IN           NUMBER ,
3939      p_activity_type_id      IN           NUMBER ,
3940      p_organization_id       IN           NUMBER ,
3941      p_lot_number            IN           VARCHAR2 DEFAULT NULL ,
3942      p_revision              IN           VARCHAR2 DEFAULT NULL) IS
3943 
3944 	l_crossdock_flag NUMBER;
3945 	l_crossdock_prim_quantity NUMBER;
3946  BEGIN
3947 
3948     validate_operation
3949       (
3950        x_return_status         => x_return_status,
3951        x_msg_data              => x_msg_data,
3952        x_msg_count             => x_msg_count,
3953        x_error_code            => x_error_code,
3954        x_inspection_flag       => x_inspection_flag,
3955        x_load_flag             => x_load_flag,
3956        x_drop_flag             => x_drop_flag,
3957        x_crossdock_flag        => l_crossdock_flag,
3958        x_load_prim_quantity    => x_load_prim_quantity,
3959        x_drop_prim_quantity    => x_drop_prim_quantity,
3960        x_inspect_prim_quantity => x_inspect_prim_quantity,
3961        x_crossdock_prim_quantity => l_crossdock_prim_quantity,
3962        p_source_task_id        => p_source_task_id,
3963        p_move_order_line_id    => p_move_order_line_id,
3964        p_inventory_item_id     => p_inventory_item_id,
3965        p_LPN_ID                => p_lpn_id,
3966        p_activity_type_id      => p_activity_type_id,
3967        p_organization_id       => p_organization_id,
3968        p_lot_number            => p_lot_number,
3969        p_revision              => p_revision);
3970 
3971 
3972  END validate_operation;
3973 
3974 
3975 
3976 
3977 /**  Validate_Operation
3978   *   <p>For a given criteria (LPN, Item or task), this API returns certain information,
3979   *      operation type in particular, about the current pending operation.
3980   *      Based on the return value, it is caller procedure' s responsibility to determine
3981   *      whether user should be allowed to continue his/her operation.
3982   *  @param x_return_status        -Return Status
3983   *  @param x_msg_data             -Returns the Error message Data
3984   *  @param x_msg_count            -Returns the message count
3985   *  @param x_error_code           -Returns appropriate error code in case of any error.
3986   *  @param x_inspection_flag      -This parameter indicates the inspection requirement for the current pending
3987   *                                 operation instances for the given criteria. 1. No inspection is required;
3988   *                                  2. Part of the given criteria requires inspection;
3989   *                                  3. All operation instances for the given criteria require inspection
3990   *  @param x_drop_flag            -This parameter indicates the drop operation for the current pending
3991   *                                 operation instances for the given criteria.
3992   *                                 1. No drop is required; 2. Part of the given criteria requires drop;
3993   *                                 3. All operation instances for the given criteria require drop
3994   *  @param x_load_flag            -This parameter indicates the load operation for the current pending
3995   *                                 operation instances for the given criteria. 1. No load operation;
3996   *                                  2. Part of the given criteria requires load;
3997   *                                  3. All operation instances for the given criteria require load.
3998   *  @param x_load_prim_quantity   -If LPN/Item or move order line is passed, sum of the primary quantity from
3999   *                                 the document table for those document records whose pending operation is
4000   *                                 'Load'.
4001   *  @param x_drop_prim_Quantity   -If LPN/Item or move order line is passed, sum of the primary quantity from
4002   *                                 the document table for those document records whose pending operation is
4003   *                                 'Drop'.
4004   *  @param x_inspect_prim_Quantity-If LPN/Item or move order line is passed, sum of the primary quantity from
4005   *                                 the document table for those document records whose pending operation is
4006   *                                 'Inspect'.
4007   *  @param p_source_task_id       -Identifier of the document record.
4008   *  @param p_move_order_line_id   -Move Order line Id that has to be validated.
4009   *  @param p_inventory_item_id    -Inventory Item Id that has to be validated.
4010   *  @param p_LPN_ID               -LPN id of the LPN that has to be validated.
4011   *  @param p_activity_type_id     -Lookup containing the Activity to be performed.
4012   *  @param p_organization_id      -Organization Id
4013     **/
4014 
4015     PROCEDURE validate_operation
4016     (
4017      x_return_status         OUT  NOCOPY  VARCHAR2 ,
4018      x_msg_data              OUT  NOCOPY  FND_NEW_MESSAGES.MESSAGE_TEXT%TYPE ,
4019      x_msg_count             OUT  NOCOPY  NUMBER,
4020      x_error_code            OUT  NOCOPY  NUMBER ,
4021      x_inspection_flag       OUT  NOCOPY  NUMBER ,
4022      x_load_flag             OUT  NOCOPY  NUMBER ,
4023      x_drop_flag             OUT  NOCOPY  NUMBER ,
4024      x_crossdock_flag        OUT  NOCOPY  NUMBER ,
4025      x_load_prim_quantity    OUT  NOCOPY  NUMBER ,
4026      x_drop_prim_quantity    OUT  NOCOPY  NUMBER ,
4027      x_inspect_prim_quantity OUT  NOCOPY  NUMBER ,
4028      x_crossdock_prim_quantity OUT  NOCOPY  NUMBER ,
4029      p_source_task_id        IN           NUMBER ,
4030      p_move_order_line_id    IN           NUMBER ,
4031      p_inventory_item_id     IN           NUMBER ,
4032      p_LPN_ID                IN           NUMBER ,
4033      p_activity_type_id      IN           NUMBER ,
4034      p_organization_id       IN           NUMBER ,
4035      p_lot_number            IN           VARCHAR2 DEFAULT NULL,
4036      p_revision              IN           VARCHAR2 DEFAULT NULL) IS
4037 
4038     TYPE src_taskid_tab_type IS TABLE OF NUMBER
4039       INDEX BY BINARY_INTEGER;
4040 
4041     --3631633
4042     --Variable to hold license plate details
4043     l_subinventory_code WMS_LICENSE_PLATE_NUMBERS.SUBINVENTORY_CODE%TYPE;
4044     l_locator_id WMS_LICENSE_PLATE_NUMBERS.LOCATOR_ID%TYPE;
4045 
4046    CURSOR mol_details IS
4047       SELECT mol.inspection_status,
4048 	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,
4049 	mmtt.operation_plan_id,mmtt.transaction_temp_id,mol.lpn_id,mol.inventory_item_id
4050       FROM mtl_txn_request_lines mol, mtl_material_transactions_temp mmtt
4051       WHERE       mol.line_id = mmtt.move_order_line_id(+)
4052       AND         mol.line_id = p_move_order_line_id
4053       AND mol.organization_id = p_organization_id
4054 	AND mol.organization_id = mmtt.organization_id(+)
4055 	AND mol.line_status <> 5  -- bug 4179991
4056       GROUP BY mol.inspection_status,mmtt.operation_plan_id,mmtt.transaction_temp_id, mol.lpn_id, mol.inventory_item_id;
4057 
4058    CURSOR lpn_details IS
4059       SELECT mol.inspection_status,mmtt.operation_plan_id,mmtt.transaction_temp_id
4060       FROM mtl_txn_request_lines mol, mtl_material_transactions_temp mmtt
4061       WHERE       mol.line_id = mmtt.move_order_line_id(+)
4062       AND          mol.lpn_id = p_lpn_id
4063 	AND mol.organization_id = p_organization_id
4064 	AND mol.line_status <> 5  -- bug 4179991
4065       AND mol.organization_id = mmtt.organization_id(+);
4066 
4067    CURSOR lpnitem_details IS
4068       SELECT mol.inspection_status,
4069 	SUM(NVL(mmtt.primary_quantity,
4070 		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,
4071 	mol.lpn_id,mmtt.operation_plan_id,mmtt.transaction_temp_id,mol.lot_number,mol.revision
4072       FROM mtl_txn_request_lines mol, mtl_material_transactions_temp mmtt
4073       WHERE          mol.lpn_id = p_lpn_id
4074       AND mol.inventory_item_id = p_inventory_item_id
4075       AND           mol.line_id = mmtt.move_order_line_id(+)
4076       AND   mol.organization_id = p_organization_id
4077 	AND   mol.organization_id = mmtt.organization_id(+)
4078 	AND mol.line_status <> 5  -- bug 4179991
4079       GROUP BY mol.lpn_id,mol.inspection_status,mmtt.operation_plan_id,mmtt.transaction_temp_id,mol.lot_number,mol.revision;
4080 
4081    CURSOR c_operation_instance(v_source_task_id NUMBER) IS
4082       SELECT wopi.operation_type_id,
4083 	wopd.operation_type wopd_op_type_id
4084 	FROM wms_op_operation_instances wopi,
4085 	wms_op_plan_details wopd
4086 	WHERE  wopi.source_task_id = v_source_task_id
4087 	AND wopi.operation_status  IN  (G_OP_INS_STAT_PENDING,G_OP_INS_STAT_ACTIVE)
4088 	AND wopi.operation_plan_detail_id = wopd.operation_plan_detail_id
4089 	ORDER BY wopi.operation_sequence DESC;
4090 
4091    CURSOR c_doc_detail IS
4092       SELECT mol.inspection_status,mmtt.operation_plan_id,
4093 	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
4094       FROM  mtl_material_transactions_temp mmtt , mtl_txn_request_lines mol
4095 	WHERE mmtt.transaction_temp_id = p_source_task_id
4096 	AND mol.line_status <> 5  -- bug 4179991
4097       AND   mmtt.move_order_line_id = mol.line_id;
4098 
4099    CURSOR c_quantity_details IS
4100     SELECT SUM(primary_transaction_quantity) quantity,lpn_id,inventory_item_id,lot_number,revision
4101     FROM mtl_onhand_quantities_detail
4102     WHERE lpn_id = p_lpn_id
4103     --3631633
4104     --Force to use the N5 index
4105     AND subinventory_code = l_subinventory_code
4106     AND locator_id = l_locator_id
4107     AND inventory_item_id =nvl(p_inventory_item_id,inventory_item_id)
4108     AND organization_id = p_organization_id
4109     GROUP BY lpn_id,inventory_item_id,lot_number,revision;
4110 
4111 
4112       l_debug              NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
4113       l_module_name        VARCHAR2(30) := 'Validate_Operation';
4114       l_progress           NUMBER       ;
4115 
4116       l_inspection_status  NUMBER:=-1;
4117       l_quantity           NUMBER:=0;
4118       l_lpn_id             NUMBER;
4119       l_operation_plan_id  NUMBER;
4120       l_source_task_id     NUMBER;
4121       l_operation_type_id  NUMBER;
4122       l_wopd_op_type_id    NUMBER;
4123       l_operation_seq      NUMBER;
4124       l_tab_index          PLS_INTEGER;
4125 
4126       l_src_taskid_tab     src_taskid_tab_type;
4127       l_inventory_item_id  NUMBER;
4128 
4129       l_mmtt_pri_qty       NUMBER := 0;
4130 
4131       l_lpn_context        NUMBER;
4132 
4133       l_doc_detail         c_doc_detail%ROWTYPE;
4134 
4135 -- Increased lot size to 80 Char - Mercy Thomas - B4625329
4136       l_lot_number         VARCHAR2(80);
4137       l_revision           VARCHAR2(3);
4138 
4139       --Bug 4713903
4140       l_rev_control_code VARCHAR2(5):='FALSE';
4141       l_lot_control_code VARCHAR2(5):='FALSE';
4142       l_serial_control_code VARCHAR2(5):='FALSE';
4143       l_att NUMBER;
4144       l_qoh NUMBER;
4145       l_lpn_onhand NUMBER;
4146       l_return_msg VARCHAR2(2000);
4147       l_ret_val VARCHAR2(1);
4148 
4149  BEGIN
4150       IF (l_debug = 1) THEN
4151           print_DEBUG(' p_source_task_id     ==> '||p_source_task_id,l_module_name,3);
4152           print_DEBUG(' p_move_order_line_id ==> '||p_move_order_line_id,l_module_name,3);
4153           print_DEBUG(' p_inventory_item_id  ==> '||p_inventory_item_id ,l_module_name,3);
4154           print_DEBUG(' p_LPN_ID             ==> '||p_LPN_ID  ,l_module_name,3);
4155           print_DEBUG(' p_activity_type_id   ==> '||p_activity_type_id ,l_module_name,3);
4156           print_debug(' p_lot_number         ==> '||p_lot_number,l_module_name,3);
4157           print_debug(' p_revision           ==> '||p_revision,l_module_name,3);
4158       END IF;
4159 
4160 
4161       l_progress       :=10;
4162       /*Setting the default status of followinf output parameters*/
4163       x_return_status         := g_ret_sts_success;
4164       x_inspection_flag       := G_NO_INSPECTION;
4165       x_load_flag             := G_NO_LOAD;
4166       x_drop_flag             := G_NO_DROP;
4167       x_crossdock_flag        := g_no_crossdock;
4168       x_inspect_prim_quantity := 0;
4169       x_load_prim_quantity    := 0;
4170       x_drop_prim_quantity    := 0;
4171       x_crossdock_prim_quantity := 0;
4172 
4173       /*Validate inputs*/
4174       IF (p_source_task_id IS NULL) THEN
4175          IF (p_lpn_id IS NULL AND p_move_order_line_id IS NULL) THEN
4176             --x_error_code:=
4177             IF (l_debug=1) THEN
4178                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);
4179             END IF;
4180             RAISE_APPLICATION_ERROR(INVALID_INPUT,'Invalid Inputs passed');
4181          END IF;
4182       END IF;
4183 
4184       l_progress:=20;
4185 
4186        IF (p_activity_type_id=G_OP_ACTIVITY_INBOUND) THEN
4187 
4188          l_src_taskid_tab.DELETE;
4189          l_tab_index:=1;
4190 
4191          IF (p_source_task_id IS NULL) THEN
4192 
4193            /*Open the appropriate cursor based on the input*/
4194             IF (p_move_order_line_id IS NOT NULL) THEN
4195 
4196                l_progress:=30;
4197                OPEN mol_details;
4198                print_debug('MOL cursor openened',l_module_name,9);
4199 
4200             ELSIF (p_lpn_id IS NOT NULL AND p_inventory_item_id IS NOT NULL) THEN
4201 
4202                l_progress:=40;
4203                OPEN lpnitem_details;
4204                print_debug('LPN_ITEM cursor openened',l_module_name,9);
4205 
4206             ELSE
4207 
4208                l_progress:=50;
4209                OPEN lpn_details;
4210                print_debug('LPN cursor openened',l_module_name,9);
4211 
4212             END IF;
4213 
4214             LOOP
4215                IF (p_move_order_line_id IS NOT NULL) THEN
4216 
4217                     FETCH mol_details INTO l_inspection_status,l_quantity,l_operation_plan_id,l_source_task_id,l_lpn_id,l_inventory_item_id;
4218                     EXIT WHEN mol_details%NOTFOUND;
4219 
4220                     /*Validating to see if the values fetched tally with inputs*/
4221                     IF p_lpn_id IS NOT NULL AND p_lpn_id <>l_lpn_id THEN
4222 
4223                        IF l_debug=1 THEN
4224                          print_debug('LPN Value fetcched from MOLine: '||l_lpn_id,l_module_name,9);
4225                          print_debug('LPN Value passed as input'||p_lpn_id,l_module_name,9);
4226                        END IF;
4227 
4228                        RAISE_APPLICATION_ERROR(INVALID_INPUT,'Invalid Inputs passed');
4229                     END IF;
4230 
4231                     IF p_inventory_item_id IS NOT NULL AND p_inventory_item_id <>l_inventory_item_id  THEN
4232                        IF l_debug=1 THEN
4233                          print_debug('Item Value fetcched from MOLine: '||l_inventory_item_id,l_module_name,9);
4234                          print_debug('Item Value passed as input'||p_inventory_item_id,l_module_name,9);
4235                        END IF;
4236 
4237                        RAISE_APPLICATION_ERROR(INVALID_INPUT,'Invalid Inputs passed');
4238                     END IF;
4239 
4240                     l_progress:=60;
4241 
4242                ELSIF (p_lpn_id IS NOT NULL AND p_inventory_item_id IS NOT NULL) THEN
4243 
4244                     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;
4245                     EXIT WHEN lpnitem_details%NOTFOUND;
4246 
4247                     /*Checking if the Lot And REvision Controls passed match the input criteria*/
4248                     IF p_lot_number IS NOT NULL AND p_lot_number <> l_lot_number THEN
4249                        GOTO CONTINUE;
4250                     END IF;
4251 
4252                     IF p_revision IS NOT NULL AND p_revision <> l_revision THEN
4253                        GOTO CONTINUE;
4254                     END IF;
4255                     l_progress:=70;
4256 
4257                ELSE
4258 
4259                     FETCH lpn_details INTO l_inspection_status,l_operation_plan_id,l_source_task_id;
4260                     EXIT WHEN lpn_details%NOTFOUND;
4261                     l_progress:=80;
4262 
4263                END IF;
4264 
4265                IF (l_operation_plan_id IS NULL) THEN /*Case when no MMTTs exist*/
4266                     IF (l_inspection_status=1) THEN  /*Case when inspection required*/
4267 
4268                           IF (x_inspection_flag=G_NO_INSPECTION AND (x_load_flag<>G_NO_LOAD OR x_drop_flag<>G_NO_DROP)) THEN
4269                              x_inspection_flag:=G_PARTIAL_INSPECTION;
4270                           ELSE
4271                              x_inspection_flag:=G_FULL_INSPECTION;
4272                           END IF;
4273                           l_progress:=90;
4274 
4275                           IF (x_load_flag=G_FULL_LOAD) THEN
4276                              x_load_flag:=G_PARTIAL_LOAD;
4277                           END IF;
4278                           l_progress:=100;
4279 
4280                           x_inspect_prim_quantity:=x_inspect_prim_quantity+l_quantity;
4281 
4282                           IF (l_debug=1) THEN
4283                              print_debug('Inspection Flag set so far'||x_inspection_flag,l_module_name,9);
4284                              print_debug('Inspection Qty'||x_inspect_prim_quantity,l_module_name,9);
4285                           END IF;
4286 
4287                     ELSE
4288                           IF (x_load_flag=G_NO_LOAD AND (x_inspection_flag<>G_NO_INSPECTION OR x_drop_flag<>G_NO_DROP)) THEN
4289                              x_load_flag:=G_PARTIAL_LOAD;
4290                           ELSE
4291                              x_load_flag:=G_FULL_LOAD;
4292                           END IF;
4293                           l_progress:=110;
4294 
4295                           IF x_inspection_flag=G_FULL_INSPECTION THEN
4296                              x_inspection_flag:=G_PARTIAL_INSPECTION;
4297                           END IF;
4298 
4299                           x_load_prim_quantity:=x_load_prim_quantity+l_quantity;
4300                           l_progress:=120;
4301 
4302                           IF (l_debug=1) THEN
4303                              print_debug('Load Flag set so far'||x_inspection_flag,l_module_name,9);
4304                              print_debug('Load Qty'||x_inspect_prim_quantity,l_module_name,9);
4305                           END IF;
4306 
4307 
4308                     END IF;
4309                     l_quantity:=0;
4310                ELSE
4311                      l_src_taskid_tab(l_tab_index):= l_source_task_id;
4312 
4313                       l_tab_index:=l_tab_index+1;
4314 
4315                       l_progress:=130;
4316 
4317                       print_debug('Adding txn_temp_id to table'||l_source_task_id||l_tab_index,l_module_name,9);
4318 
4319                END IF;
4320 
4321             <<CONTINUE>>
4322                NULL;
4323 
4324             END LOOP;
4325 
4326             IF (mol_details%ISOPEN) THEN
4327                CLOSE mol_details;
4328             ELSIF (lpnitem_details%ISOPEN) THEN
4329                CLOSE lpnitem_details;
4330             ELSIF (lpn_details%ISOPEN) THEN
4331                CLOSE lpn_details;
4332             END IF;
4333 
4334             print_debug('Closed the open cursors',l_module_name,9);
4335             l_progress:=140;
4336 
4337             /*Incase none of the records are fetched from MTL_txn_request_lines for an inventory LPN
4338               returning with flag as FULL LOAD */
4339 
4340             IF p_lpn_id IS NOT NULL AND l_inspection_status=-1 THEN
4341 
4342                IF l_debug=1 THEN
4343                   print_debug('No move Order line exists for the LPN passed,returning status as Full Load reqd',l_module_name,9);
4344                END IF;
4345 
4346                 /* We would return a full Load required incase a Move Order Line is not obtained.
4347                  * Today we do no validation of a possible data corruption from Inbound:
4348                  * i.e. those cases where the LPN is in Rcv and doesnt have a move order line
4349                  * associated.
4350                  * Also if an LPN has a item which has no Move Order associated with it then
4351                  * we would still return as Full Load required.
4352                  */
4353                    x_inspection_flag := G_NO_INSPECTION;
4354                    x_load_flag       := G_FULL_LOAD;
4355                    x_drop_flag       := G_NO_DROP;
4356 
4357                    /*Since there is no Move Order existing for this LPN we need to query MOQD for
4358                     * on hand quantities.This is done only when both LPN and Item are passed as
4359                     * we never return qty only if LPN is passed.
4360                     */
4361                   IF p_inventory_item_id IS NOT NULL THEN
4362 
4363 		  --3631633
4364 		  --Select license plate number details
4365 		  IF p_lpn_id IS NOT NULL THEN
4366 			select subinventory_code,locator_id into l_subinventory_code,l_locator_id from WMS_LICENSE_PLATE_NUMBERS where lpn_id=p_lpn_id;
4367 		  END IF;
4368 
4369 		    --Bug 4730925 BEGIN
4370 		    --Get Item controls and then call quantity tree
4371 		    SELECT Decode(revision_qty_control_code,1,'FALSE','TRUE'),
4372 		      Decode(lot_control_code,1,'FALSE','TRUE'),
4373 		      Decode(serial_number_control_code,1,'FALSE',6,'FALSE','TRUE')
4374 		      INTO l_rev_control_code,
4375 		      l_lot_control_code,
4376 		      l_serial_control_code
4377 		      FROM mtl_system_items_kfv
4378 		      WHERE inventory_item_id = p_inventory_item_id
4379 		      AND organization_id = p_organization_id;
4380 
4381 		    IF p_lot_number IS NULL OR p_lot_number = '' THEN
4382 			l_lot_control_code := 'FALSE';
4383 		    END IF;
4384 		    --Quantity Tree to get the availability
4385 		    IF l_debug=1 THEN
4386                            print_debug(' p_lpn_id :'|| p_lpn_id ,l_module_name,9);
4387 			   print_debug(' p_organization_id :'|| p_organization_id ,l_module_name,9);
4388 			   print_debug(' p_inventory_item_id :'|| p_inventory_item_id ,l_module_name,9);
4389 			   print_debug(' p_revision :'|| p_revision ,l_module_name,9);
4390 			   print_debug(' l_locator_id :'|| l_locator_id ,l_module_name,9);
4391 			   print_debug(' l_subinventory_code :'|| l_subinventory_code ,l_module_name,9);
4392 			   print_debug(' p_lot_number :'|| p_lot_number ,l_module_name,9);
4393 			   print_debug(' l_rev_control_code :'|| l_rev_control_code ,l_module_name,9);
4394 			   print_debug(' l_serial_control_code :'|| l_serial_control_code ,l_module_name,9);
4395 			   print_debug(' l_lot_control_code :'|| l_lot_control_code ,l_module_name,9);
4396                     END IF;
4397 
4398 		    l_ret_val := inv_txn_validations.get_immediate_lpn_item_qty (
4399 										 p_lpn_id => p_lpn_id,
4400 										 p_organization_id => p_organization_id,
4401 										 p_source_type_id => -9999,
4402 										 p_inventory_item_id => p_inventory_item_id,
4403 										 p_revision => p_revision,
4404 										 p_locator_id => l_locator_id,
4405 										 p_subinventory_code => l_subinventory_code,
4406 										 p_lot_number => p_lot_number,
4407 										 p_is_revision_control => l_rev_control_code,
4408 										 p_is_serial_control => l_serial_control_code,
4409 										 p_is_lot_control => l_lot_control_code,
4410 										 x_transactable_qty => l_att,
4411 										 x_qoh => l_qoh,
4412 										 x_lpn_onhand => l_lpn_onhand,
4413 										 x_return_msg => l_return_msg);
4414 
4415 		    IF (l_ret_val = 'Y') THEN
4416 		       x_load_prim_quantity := x_load_prim_quantity + l_att;
4417 		     ELSE
4418 		       x_load_prim_quantity := 0;
4419 		    END IF;
4420 
4421 		   /*
4422 		   FOR l_quantity_details IN c_quantity_details LOOP
4423 
4424                       IF p_lot_number IS NOT NULL THEN
4425 
4426                          IF p_lot_number<>nvl(l_quantity_details.lot_number,'@#LOT#@') THEN
4427 		            --If lot numbers are not matching skip this record
4428                             IF l_debug=1 THEN
4429                              print_debug('Lot mumber from MOQD'||l_quantity_details.lot_number,l_module_name,9);
4430                             END IF;
4431                             GOTO NEXT_RECORD;
4432                           END IF;
4433                         END IF;
4434 
4435                         --Checking if the revisions match
4436                         IF p_revision IS NOT NULL THEN
4437                           IF p_revision<>nvl(l_quantity_details.revision,'-') THEN
4438                              IF l_debug=1 THEN
4439                               print_debug('Revsions are differet,skipping record',l_module_name,9);
4440                              END IF;
4441 
4442                              GOTO NEXT_RECORD;
4443                            END IF;
4444                         END IF;
4445 
4446                         x_load_prim_quantity := x_load_prim_quantity+l_quantity_details.quantity;
4447 
4448                         IF l_debug=1 THEN
4449                            print_debug('Qty fetched'||l_quantity_details.quantity,l_module_name,9);
4450                            print_debug('Load Qty'||x_load_prim_quantity,l_module_name,9);
4451                         END IF;
4452 
4453                         <<NEXT_RECORD>>
4454                            NULL;
4455                    END LOOP;
4456 		   Bug 4730925 END */
4457                   ELSE
4458                      x_load_prim_quantity :=0;
4459 
4460 
4461                   END IF;
4462 
4463                    IF l_debug=1 THEN
4464                     print_debug('Load Qty'||x_load_prim_quantity,l_module_name,9);
4465                    END IF;
4466 
4467                    x_inspect_prim_quantity :=0;
4468                    x_drop_prim_quantity    :=0;
4469 
4470             END IF;
4471 
4472 
4473          ELSE /*p_source_task_id not null*/
4474 
4475             IF l_debug=1 THEN
4476                      print_debug('Checking for non ATF case',l_module_name,9);
4477             END IF;
4478 
4479            OPEN c_doc_detail;
4480 
4481            FETCH c_doc_detail INTO l_doc_detail;
4482 
4483            IF c_doc_detail%NOTFOUND THEN
4484 
4485              IF l_debug=1 THEN
4486                print_debug('Invalid Input Doc Id passed',l_module_name,9);
4487              END IF;
4488 
4489              CLOSE c_doc_detail;
4490 
4491              RAISE_APPLICATION_ERROR(INVALID_INPUT,'Invalid Inputs passed');
4492 
4493            END IF;
4494 
4495            CLOSE c_doc_detail;
4496 
4497            IF l_doc_detail.operation_plan_id IS NULL THEN
4498 
4499              IF l_debug = 1 THEN
4500                print_debug('Operation Plan Id :Non-ATF Case',l_module_name,9);
4501                print_debug('Hence chekcing Move Order Line',l_module_name,9);
4502              END IF;
4503 
4504              IF (l_inspection_status=1) THEN  /*Case when inspection required*/
4505 
4506                IF (x_inspection_flag=G_NO_INSPECTION AND (x_load_flag<>G_NO_LOAD OR x_drop_flag<>G_NO_DROP)) THEN
4507 
4508                   x_inspection_flag:=G_PARTIAL_INSPECTION;
4509 
4510                ELSE
4511 
4512                   x_inspection_flag:=G_FULL_INSPECTION;
4513 
4514                END IF;
4515 
4516                l_progress:=201;
4517 
4518                IF (x_load_flag=G_FULL_LOAD) THEN
4519                   x_load_flag:=G_PARTIAL_LOAD;
4520                END IF;
4521 
4522                l_progress:=203;
4523 
4524                x_inspect_prim_quantity:=x_inspect_prim_quantity+l_doc_detail.quantity;
4525 
4526                IF (l_debug=1) THEN
4527                    print_debug('Inspection Flag set so far'||x_inspection_flag,l_module_name,9);
4528                    print_debug('Inspection Qty'||x_inspect_prim_quantity,l_module_name,9);
4529                END IF;
4530 
4531             ELSE
4532                IF (x_load_flag=G_NO_LOAD AND (x_inspection_flag<>G_NO_INSPECTION OR x_drop_flag<>G_NO_DROP)) THEN
4533                  x_load_flag:=G_PARTIAL_LOAD;
4534 
4535                ELSE
4536 
4537                  x_load_flag:=G_FULL_LOAD;
4538 
4539                END IF;
4540                l_progress:=205;
4541 
4542                IF x_inspection_flag=G_FULL_INSPECTION THEN
4543                   x_inspection_flag:=G_PARTIAL_INSPECTION;
4544 
4545                END IF;
4546 
4547                x_load_prim_quantity:=x_load_prim_quantity+l_doc_detail.quantity;
4548                l_progress:=120;
4549 
4550                IF (l_debug=1) THEN
4551                   print_debug('Load Flag set so far'||x_inspection_flag,l_module_name,9);
4552                   print_debug('Load Qty'||x_inspect_prim_quantity,l_module_name,9);
4553                END IF;
4554 
4555 
4556            END IF;
4557 
4558            ELSE
4559 
4560                l_src_taskid_tab(l_tab_index):=p_source_task_id;
4561                l_progress:=150;
4562                 print_debug('Adding p_source_task_id to pl/sql table'||l_src_taskid_tab(l_tab_index),l_module_name,9);
4563 
4564            END IF;
4565          END IF;
4566 
4567          IF (l_src_taskid_tab.COUNT>0) THEN
4568 
4569 
4570               FOR j IN l_src_taskid_tab.FIRST..l_src_taskid_tab.LAST LOOP
4571 
4572                  OPEN c_operation_instance(l_src_taskid_tab(j));
4573 
4574                  FETCH c_operation_instance INTO l_operation_type_id, l_wopd_op_type_id;
4575 
4576                  IF (c_operation_instance%NOTFOUND) THEN
4577 
4578                     IF (l_debug=1) THEN
4579                        print_debug('No operation instance exitsting'||l_progress,l_module_name,9);
4580                        RAISE_APPLICATION_ERROR(OPERATION_INSTANCE_NOT_EXISTS,'Operation Instance does not exist for source_task id'||l_src_taskid_tab(j));
4581                     END IF;
4582 
4583                  END IF;
4584 
4585                  CLOSE c_operation_instance;
4586 
4587 		 l_progress := 146;
4588 
4589 		 IF (p_activity_type_id=G_OP_ACTIVITY_INBOUND) THEN
4590 
4591 		    SELECT primary_quantity
4592 		      INTO l_mmtt_pri_qty
4593 		      FROM mtl_material_transactions_temp
4594 		      WHERE transaction_temp_id = l_src_taskid_tab(j);
4595 
4596 		 END IF;
4597 
4598        l_progress:=150;
4599 		 IF (l_debug=1) THEN
4600 		    print_debug('Operation Type Id in WOOI: '||l_operation_type_id,l_module_name,9);
4601 		    print_debug('Operation Type Id in WOPD: '||l_wopd_op_type_id,l_module_name,9);
4602 		    print_debug('l_mmtt_pri_qty = '||l_mmtt_pri_qty,l_module_name,9);
4603 		 END IF;
4604 
4605                  IF (l_operation_type_id=G_OP_TYPE_INSPECT) THEN
4606 
4607 		              x_inspect_prim_quantity := x_inspect_prim_quantity + l_mmtt_pri_qty;
4608 
4609                     IF (x_inspection_flag=G_NO_INSPECTION AND (x_load_flag<>G_NO_LOAD OR x_drop_flag<>G_NO_DROP)) THEN
4610 
4611                         x_inspection_flag:=G_PARTIAL_INSPECTION;
4612                     ELSE
4613                         x_inspection_flag:=G_FULL_INSPECTION;
4614 
4615                     END IF;
4616                     l_progress:=160;
4617 
4618 
4619                     IF (x_load_flag=G_FULL_LOAD) THEN
4620                        x_load_flag:=G_PARTIAL_LOAD;
4621                     END IF;
4622 
4623 
4624                     IF (x_drop_flag=G_FULL_DROP) THEN
4625                        x_drop_flag:=G_PARTIAL_DROP;
4626                     END IF;
4627                     l_progress:=170;
4628 
4629                     IF (l_debug=1) THEN
4630                         print_debug('Inspection Flag set so far'||x_inspection_flag,l_module_name,9);
4631                         print_debug('Load Flag set so far'||x_load_flag,l_module_name,9);
4632                         print_debug('Drop Flag set so far'||x_drop_flag,l_module_name,9);
4633                     END IF;
4634 
4635 
4636 		  ELSIF (l_operation_type_id=G_OP_TYPE_LOAD) THEN
4637 
4638 		    IF (l_wopd_op_type_id=g_op_type_crossdock) THEN
4639 		       IF (x_crossdock_flag=g_partial_crossdock) OR (x_crossdock_flag = g_no_crossdock AND
4640 			   (x_crossdock_prim_quantity < x_load_prim_quantity OR   -- there was some Non-crossdock load
4641 			    x_crossdock_prim_quantity < x_drop_prim_quantity))THEN  -- there was some Non-crossdock drop
4642 			  x_crossdock_flag := g_partial_crossdock;
4643 			ELSE
4644 			  x_crossdock_flag := g_full_crossdock;
4645 		       END IF;  -- IF (x_crossdock_flag = g_no_crossdock
4646 
4647 		       x_crossdock_prim_quantity := x_crossdock_prim_quantity + l_mmtt_pri_qty;
4648 
4649 		     ELSE  --IF (l_wopd_op_type_id=g_op_type_crossdock)
4650 
4651 		       IF (x_crossdock_flag = g_full_crossdock) THEN
4652 			  x_crossdock_flag := g_partial_crossdock;
4653 		       END IF; -- IF (x_crossdock_flag = g_full_crossdock)
4654 
4655 		    END IF;  -- IF (l_wopd_op_type_id=g_op_type_crossdock)
4656 
4657                     x_load_prim_quantity := x_load_prim_quantity + l_mmtt_pri_qty;
4658 
4659                     IF (x_load_flag=G_NO_LOAD AND (x_inspection_flag<>G_NO_INSPECTION OR x_drop_flag<>G_NO_DROP)) THEN
4660 
4661                         x_load_flag:=G_PARTIAL_LOAD;
4662                     ELSE
4663                         x_load_flag:=G_FULL_LOAD;
4664                     END IF;
4665 
4666                     IF (x_inspection_flag=G_FULL_INSPECTION) THEN
4667 
4668                        x_inspection_flag:=G_PARTIAL_INSPECTION;
4669                     END IF;
4670 
4671                     IF (x_drop_flag=G_FULL_DROP) THEN
4672 
4673                        x_drop_flag:=G_PARTIAL_DROP;
4674                     END IF;
4675                     l_progress:=180;
4676 
4677                     IF (l_debug=1) THEN
4678                         print_debug('Inspection Flag set so far'||x_inspection_flag,l_module_name,9);
4679                         print_debug('Load Flag set so far'||x_load_flag,l_module_name,9);
4680                         print_debug('Drop Flag set so far'||x_drop_flag,l_module_name,9);
4681                     END IF;
4682 
4683 
4684 		  ELSE
4685 
4686 		    IF (l_wopd_op_type_id=g_op_type_crossdock) THEN
4687 		       IF (x_crossdock_flag=g_partial_crossdock) OR(x_crossdock_flag = g_no_crossdock AND
4688 			   (x_crossdock_prim_quantity < x_load_prim_quantity OR   -- there was some Non-crossdock load
4689 			    x_crossdock_prim_quantity < x_drop_prim_quantity))THEN  -- there was some Non-crossdock drop
4690 			  x_crossdock_flag := g_partial_crossdock;
4691 			ELSE
4692 			  x_crossdock_flag := g_full_crossdock;
4693 		       END IF;  -- IF (x_crossdock_flag = g_no_crossdock
4694 
4695 		       x_crossdock_prim_quantity := x_crossdock_prim_quantity + l_mmtt_pri_qty;
4696 
4697 		     ELSE  --IF (l_wopd_op_type_id=g_op_type_crossdock)
4698 
4699 		       IF (x_crossdock_flag = g_full_crossdock) THEN
4700 			  x_crossdock_flag := g_partial_crossdock;
4701 		       END IF; -- IF (x_crossdock_flag = g_full_crossdock)
4702 
4703 		    END IF;  -- IF (l_wopd_op_type_id=g_op_type_crossdock)
4704 
4705 
4706 		    x_drop_prim_quantity := x_drop_prim_quantity + l_mmtt_pri_qty;
4707 
4708                     IF (x_drop_flag=G_NO_DROP AND (x_inspection_flag<>G_NO_LOAD OR x_drop_flag<>G_NO_INSPECTION)) THEN
4709 
4710                          x_drop_flag:=G_PARTIAL_DROP;
4711                     ELSE
4712                          x_drop_flag:=G_FULL_DROP;
4713                     END IF;
4714 
4715                     IF (x_inspection_flag=G_FULL_INSPECTION) THEN
4716 
4717                         x_inspection_flag:=G_PARTIAL_INSPECTION;
4718                     END IF;
4719 
4720                     IF (x_load_flag=G_FULL_LOAD) THEN
4721 
4722                         x_load_flag:=G_PARTIAL_LOAD;
4723                     END IF;
4724                     l_progress:=190;
4725 
4726                     IF (l_debug=1) THEN
4727                         print_debug('Inspection Flag set so far'||x_inspection_flag,l_module_name,9);
4728                         print_debug('Load Flag set so far'||x_load_flag,l_module_name,9);
4729                         print_debug('Drop Flag set so far'||x_drop_flag,l_module_name,9);
4730                     END IF;
4731 
4732                  END IF;
4733 
4734 
4735               END LOOP;
4736 
4737               print_debug('Inspection Flag'||x_inspection_flag,l_module_name,9);
4738               print_debug('Load Flag'||x_load_flag,l_module_name,9);
4739               print_debug('Drop Flag'||x_drop_flag,l_module_name,9);
4740               print_debug('Crossdock Flag '||x_crossdock_flag,l_module_name,9);
4741               print_debug('Crossdock Primary Qty '||x_crossdock_prim_quantity,l_module_name,9);
4742 
4743 
4744          END IF;/*l_srctaskid_tab.COUNT>0*/
4745 
4746 	 IF p_source_task_id IS NULL
4747 	   AND p_move_order_line_id IS NULL
4748 	     AND p_inventory_item_id IS NULL THEN
4749 
4750 	    x_load_prim_quantity := 0;
4751 	    x_drop_prim_quantity := 0;
4752 	    x_inspect_prim_quantity := 0;
4753 
4754 	 END IF;
4755 
4756 
4757          print_debug('Inspection Flag '||x_inspection_flag,l_module_name,9);
4758          print_debug('Load Flag '||x_load_flag,l_module_name,9);
4759          print_debug('Drop Flag '||x_drop_flag,l_module_name,9);
4760          print_debug('Load primary qty '||x_load_prim_quantity,l_module_name,9);
4761          print_debug('Drop primary qty '||x_drop_prim_quantity,l_module_name,9);
4762          print_debug('Inspect print_debug qty '||x_inspect_prim_quantity,l_module_name,9);
4763          print_debug('Crossdock Flag '||x_crossdock_flag,l_module_name,9);
4764          print_debug('Crossdock Primary Qty '||x_crossdock_prim_quantity,l_module_name,9);
4765 
4766        END IF;/*Activity Type =Inbound*/
4767 
4768  EXCEPTION
4769       WHEN FND_API.G_EXC_ERROR THEN
4770          x_return_status:=g_ret_sts_error;
4771          IF mol_details%ISOPEN THEN
4772                CLOSE mol_details;
4773          END IF;
4774          IF lpnitem_details%ISOPEN THEN
4775                CLOSE lpnitem_details;
4776          END IF;
4777          IF lpn_details%ISOPEN THEN
4778                CLOSE lpn_details;
4779          END IF;
4780          IF c_operation_instance%ISOPEN THEN
4781                 CLOSE c_operation_instance;
4782          END IF;
4783 
4784          IF c_doc_detail%ISOPEN THEN
4785              CLOSE c_doc_detail;
4786          END IF;
4787 
4788          IF c_quantity_details%ISOPEN THEN
4789             CLOSE c_quantity_details;
4790          END IF;
4791 
4792     /*Populate error message and error Code*/
4793 
4794       WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4795          x_return_status:=g_ret_sts_unexp_error;
4796 
4797          IF (l_debug=1) THEN
4798           print_debug('Unexpected error'||SQLERRM||l_progress,l_module_name,1);
4799          END IF;
4800 
4801          IF mol_details%ISOPEN THEN
4802                CLOSE mol_details;
4803          END IF;
4804          IF lpnitem_details%ISOPEN THEN
4805                CLOSE lpnitem_details;
4806          END IF;
4807          IF lpn_details%ISOPEN THEN
4808                CLOSE lpn_details;
4809          END IF;
4810 
4811          IF c_doc_detail%ISOPEN THEN
4812              CLOSE c_doc_detail;
4813          END IF;
4814 
4815          IF c_quantity_details%ISOPEN THEN
4816           CLOSE c_quantity_details;
4817          END IF;
4818 
4819       WHEN OTHERS THEN
4820          IF (l_debug=1) THEN
4821            print_debug(SQLERRM||l_progress, l_module_name,1);
4822          END IF;
4823 
4824          IF (SQLCODE<-20000) THEN
4825             IF (l_debug=1) THEN
4826               print_debug('This is a user defined exception',l_module_name,1);
4827             END IF;
4828 
4829             x_error_code:=-(SQLCODE+20000);
4830 
4831             x_return_status:=g_ret_sts_error;
4832 
4833 
4834           ELSE
4835 
4836             x_return_status := g_ret_sts_unexp_error;
4837 
4838           END IF;
4839 
4840 
4841          IF mol_details%ISOPEN THEN
4842                CLOSE mol_details;
4843          END IF;
4844          IF lpnitem_details%ISOPEN THEN
4845                CLOSE lpnitem_details;
4846          END IF;
4847          IF (lpn_details%ISOPEN) THEN
4848                CLOSE lpn_details;
4849          END IF;
4850 
4851          IF c_operation_instance%ISOPEN THEN
4852                CLOSE c_operation_instance;
4853          END IF;
4854 
4855          IF c_doc_detail%ISOPEN THEN
4856              CLOSE c_doc_detail;
4857          END IF;
4858 
4859          IF c_quantity_details%ISOPEN THEN
4860             CLOSE c_quantity_details;
4861          END IF;
4862 
4863 
4864  END Validate_Operation;
4865 
4866 /**
4867   *    Split_Operation_instance
4868   *   <p>This procedure splits the operation plan,parent document
4869   *   record accordingly.</p>
4870   *
4871   *  @param x_return_status     -Return Status
4872   *  @param x_msg_data          -Returns the Error message Data
4873   *  @param x_msg_count         -Returns the message count
4874   *  @param x_error_code        -Returns appropriate error code in case of an error .
4875   *  @param p_source_task_id    -Identifier of the source document record
4876   *  @param p_activity_type_id  - Lookup code of the Activity type Id
4877   *  @param p_new_Task_id_table -PL/SQL table of Numbers of the MMTT ids
4878   **/
4879  PROCEDURE   Split_Operation_Instance(
4880       x_return_status     OUT  NOCOPY  VARCHAR2 ,
4881       x_msg_data          OUT  NOCOPY  FND_NEW_MESSAGES.MESSAGE_TEXT%TYPE ,
4882       x_msg_count         OUT  NOCOPY  NUMBER,
4883       x_error_code        OUT  NOCOPY  NUMBER ,
4884       p_source_task_id    IN           NUMBER ,
4885       p_activity_type_id  IN           NUMBER ,
4886       p_new_task_id_table IN           task_id_table_type) IS
4887 
4888    CURSOR inbound_document_details(v_task_id NUMBER) IS
4889       SELECT operation_plan_id
4890 	, parent_line_id
4891 	, transaction_quantity
4892 	, primary_quantity
4893 	, transaction_temp_id
4894 	, inventory_item_id
4895 	, organization_id
4896 	, move_order_line_id
4897 	FROM mtl_material_transactions_temp
4898 	WHERE transaction_temp_id=v_task_id;
4899 
4900 
4901    CURSOR c_item_details(v_inventory_item_id NUMBER,v_organization_id NUMBER) IS
4902       SELECT nvl(lot_control_code,1)lot_control_code,nvl(serial_number_control_code,1) serial_number_control_code
4903 	FROM mtl_system_items_b
4904 	WHERE inventory_item_id = v_inventory_item_id
4905 	AND organization_id   = v_organization_id;
4906 
4907    CURSOR wopi_cursor(v_source_task_id NUMBER) IS
4908       SELECT *
4909 /*	  OP_PLAN_INSTANCE_ID,
4910           OPERATION_PLAN_ID,
4911           ACTIVITY_TYPE_ID,
4912           PLAN_TYPE_ID,
4913           SOURCE_TASK_ID,
4914           STATUS,
4915           PLAN_EXECUTION_START_DATE,
4916           PLAN_EXECUTION_END_DATE,
4917           ORGANIZATION_ID,
4918           ORIG_SOURCE_SUB_CODE,
4919           ORIG_SOURCE_LOC_ID,
4920           ORIG_DEST_SUB_CODE,
4921           ORIG_DEST_LOC_ID,
4922           LAST_UPDATE_DATE,
4923           LAST_UPDATED_BY,
4924           CREATION_DATE,
4925           CREATED_BY,
4926           LAST_UPDATE_LOGIN,
4927           ATTRIBUTE_CATEGORY,
4928           ATTRIBUTE1,
4929           ATTRIBUTE2,
4930           ATTRIBUTE3,
4931           ATTRIBUTE4,
4932           ATTRIBUTE5,
4933           ATTRIBUTE6,
4934           ATTRIBUTE7,
4935           ATTRIBUTE8,
4936           ATTRIBUTE9,
4937           ATTRIBUTE10,
4938           ATTRIBUTE11,
4939           ATTRIBUTE12,
4940           ATTRIBUTE13,
4941           ATTRIBUTE14,
4942           ATTRIBUTE15 */
4943 	FROM wms_op_plan_instances
4944 	WHERE source_task_id = v_source_task_id;
4945 
4946    CURSOR wooi_cursor(v_op_plan_instance_id NUMBER) IS
4947       SELECT *
4948 	/*
4949 	OPERATION_INSTANCE_ID,
4950 	OPERATION_PLAN_DETAIL_ID,
4951 	OPERATION_SEQUENCE,
4952 	OPERATION_TYPE_ID,
4953 	OP_PLAN_INSTANCE_ID,
4954 	OPERATION_STATUS,
4955 	ACTIVITY_TYPE_ID,
4956 	SOURCE_TASK_ID,
4957 	ACTIVATE_TIME,
4958 	COMPLETE_TIME,
4959 	SUG_TO_SUB_CODE,
4960 	SUG_TO_LOCATOR_ID,
4961 	FROM_SUBINVENTORY_CODE,
4962 	FROM_LOCATOR_ID,
4963 	TO_SUBINVENTORY_CODE,
4964 	TO_LOCATOR_ID,
4965 	IS_IN_INVENTORY,
4966 	ORGANIZATION_ID,
4967 	EMPLOYEE_ID,
4968 	EQUIPMENT_ID,
4969 	CREATED_BY,
4970 	LAST_UPDATE_DATE,
4971 	CREATION_DATE,
4972 	LAST_UPDATED_BY,
4973 	LAST_UPDATE_LOGIN,
4974 	ATTRIBUTE_CATEGORY,
4975 	ATTRIBUTE1,
4976 	ATTRIBUTE2,
4977 	ATTRIBUTE3,
4978 	ATTRIBUTE4,
4979 	ATTRIBUTE5,
4980 	ATTRIBUTE6,
4981 	ATTRIBUTE7,
4982 	ATTRIBUTE8,
4983 	ATTRIBUTE9,
4984 	ATTRIBUTE10,
4985 	ATTRIBUTE11,
4986 	ATTRIBUTE12,
4987 	ATTRIBUTE13,
4988 	ATTRIBUTE14,
4989 	ATTRIBUTE15
4990 	*/
4991 	FROM wms_op_operation_instances
4992 	WHERE op_plan_instance_id = v_op_plan_instance_id
4993        	ORDER BY operation_sequence DESC, operation_type_id DESC;
4994 	--Bug 5749206. Added another order by "operation_type_id"
4995 
4996    CURSOR wdth_cursor(v_op_plan_instance_id NUMBER) IS
4997       SELECT task_id,
4998 	transaction_id
4999 	FROM wms_dispatched_tasks_history
5000        WHERE op_plan_instance_id = v_op_plan_instance_id;
5001 
5002    l_inbound_doc_child_rec           inbound_document_details%ROWTYPE;
5003    l_inbound_doc_parent_rec          inbound_document_details%ROWTYPE;
5004    l_inbound_doc_child_split_rec     inbound_document_details%ROWTYPE;
5005    l_inbound_doc_parent_split_rec    inbound_document_details%ROWTYPE;
5006    l_item_details_rec                c_item_details%ROWTYPE;
5007    l_wopi_orig_rec                   wopi_cursor%ROWTYPE;
5008    l_wopi_split_rec                  wopi_cursor%ROWTYPE;
5009    l_wooi_orig_rec                   wooi_cursor%ROWTYPE;
5010    l_wooi_new_rec                    wooi_cursor%ROWTYPE;
5011    i                                 INTEGER;
5012    l_n_return_status                 NUMBER;
5013    l_lot_trx_id                      NUMBER;
5014    l_proc_msg                        VARCHAR2(500);
5015    l_progress                        NUMBER;
5016    l_debug                           NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
5017    l_module_name                     VARCHAR2(30) := 'Split_operation_instance';
5018 
5019 
5020    /* BEGIN GRACE11_02_2003 */
5021    l_next_task_id                    NUMBER;
5022    l_new_temp_id                     NUMBER;
5023    l_old_temp_id                     NUMBER;
5024    l_task_id                         NUMBER;
5025    l_wooi_index                      NUMBER;
5026    l_is_last_step_drop               BOOLEAN;
5027    l_num_wdts_for_mmtt               NUMBER;
5028    /* END GRACE11_02_2003 */
5029  BEGIN
5030 
5031       IF (l_debug = 1) THEN
5032           print_DEBUG(' p_source_task_id ==> '||p_source_task_id, l_module_name,3);
5033           print_DEBUG(' p_activity_type_id ==> '||p_activity_type_id, l_module_name,3);
5034       END IF;
5035       x_return_status  := g_ret_sts_success;
5036 
5037       l_progress := 10;
5038       SAVEPOINT split_op_instance_sp;
5039 
5040       IF (p_source_task_id IS NULL OR p_activity_type_id IS NULL) THEN
5041          IF (l_debug=1) THEN
5042             print_debug('Source task Id is null',l_module_name,1);
5043          END IF;
5044          /*Raise Invalid Arguement exception*/
5045          raise_application_error(INVALID_INPUT,'Invalid inputs passed'||p_source_task_id||' '||p_activity_type_id);
5046       END IF;
5047 
5048       /*Query document table(s) based on p_activity_id and P_Source_task_ID.*/
5049       IF (p_activity_type_id =G_OP_ACTIVITY_INBOUND) THEN
5050          /*Fetching document Record for Inbound:from MMTT*/
5051          IF (l_debug=1) THEN
5052             print_debug('Fetching document record for Inbound',l_module_name,9);
5053          END IF;
5054 
5055          OPEN inbound_document_details(p_source_task_id);
5056          FETCH inbound_document_details INTO l_inbound_doc_child_rec;
5057 
5058 	 l_progress := 20;
5059 
5060          IF (inbound_document_details%NOTFOUND) THEN
5061             IF (l_debug=1) THEN
5062                print_debug('Invalid document',l_module_name,3);
5063             END IF;
5064             raise_application_error(INVALID_DOC_ID,'Invalid Document');
5065          END IF;
5066 
5067          CLOSE inbound_document_details;
5068       END IF;
5069 
5070       IF (l_inbound_doc_child_rec.operation_plan_id IS NULL OR l_inbound_doc_child_rec.parent_line_id IS NULL) THEN
5071          IF (l_debug=1) THEN
5072             print_debug('OP Plan ID or parent OP Plan ID is null and unforgivable error',l_module_name, 1);
5073          END IF;
5074 
5075 	 --BUG 5075410: When splitting an MMTT, the WDT must also be
5076 	 --split. If not, the split qty will not be loaded after the split
5077 	 BEGIN
5078 	    SELECT COUNT(1) INTO l_num_wdts_for_mmtt
5079 	      FROM wms_dispatched_tasks
5080 	      WHERE transaction_temp_id = l_inbound_doc_child_rec.transaction_temp_id;
5081 	 EXCEPTION
5082 	    WHEN OTHERS THEN
5083 	       print_debug('Num of WDTs for orginal MMTT:'||l_num_wdts_for_mmtt,l_module_name,3);
5084 	 END;
5085 
5086 	 IF (l_debug = 1) THEN
5087 	    print_debug('Num of WDTs for orginal MMTT:'||l_num_wdts_for_mmtt,l_module_name,3);
5088 	 END IF;
5089 
5090 	 IF (l_num_wdts_for_mmtt > 0) THEN
5091 	    FOR i IN 1 ..  p_new_task_id_table.COUNT LOOP
5092 	       OPEN inbound_document_details(p_new_task_id_table(i));
5093 	       FETCH inbound_document_details INTO l_inbound_doc_child_split_rec;
5094 
5095 	       IF (inbound_document_details%NOTFOUND) THEN
5096 		  IF (l_debug=1) THEN
5097 		     print_debug('Invalid document',l_module_name,3);
5098 		  END IF;
5099 		  raise_application_error(INVALID_DOC_ID,'Invalid Document');
5100 	       END IF;
5101 
5102 	       CLOSE inbound_document_details;
5103 
5104 	       SELECT wms_dispatched_tasks_s.NEXTVAL
5105 		 INTO   l_next_task_id
5106 		 FROM   DUAL;
5107 
5108 	       IF (l_debug = 1) THEN
5109 		  print_debug('Inserting Duplicate WDT records using ID:'||l_next_task_id,l_module_name,3);
5110 	       END IF;
5111 
5112 	       INSERT INTO wms_dispatched_tasks
5113 		 (
5114 		  task_id
5115 		  , transaction_temp_id
5116 		  , organization_id
5117 		  , user_task_type
5118 		  , person_id
5119 		  , effective_start_date
5120 		  , effective_end_date
5121 		  , equipment_id
5122 		  , equipment_instance
5123 		  , person_resource_id
5124 		  , machine_resource_id
5125 		  , status
5126 		  , dispatched_time
5127 		  , loaded_time
5128 		  , drop_off_time
5129 		  , last_update_date
5130 		  , last_updated_by
5131 		  , creation_date
5132 		  , created_by
5133 		  , last_update_login
5134 		  , attribute_category
5135 		  , attribute1
5136 		  , attribute2
5137 		  , attribute3
5138 		  , attribute4
5139 		  , attribute5
5140 		  , attribute6
5141 		  , attribute7
5142 		  , attribute8
5143 		  , attribute9
5144 		  , attribute10
5145 		  , attribute11
5146 		  , attribute12
5147 		  , attribute13
5148 		  , attribute14
5149 		  , attribute15
5150 		  , task_type
5151 		  , priority
5152 		  , task_group_id
5153 		  , device_id
5154 		  , device_invoked
5155 		  , device_request_id
5156 		  , suggested_dest_subinventory
5157 		  , suggested_dest_locator_id
5158 		  , operation_plan_id
5159 		  , move_order_line_id
5160 		  , transfer_lpn_id
5161 		  , op_plan_instance_id
5162 		  , task_method
5163 		 )
5164 		 select l_next_task_id
5165 		 , l_inbound_doc_child_split_rec.transaction_temp_id
5166 		 , organization_id
5167 		 , user_task_type
5168 		 , person_id
5169 		 , effective_start_date
5170 		 , effective_end_date
5171 		 , equipment_id
5172 		 , equipment_instance
5173 		 , person_resource_id
5174 		 , machine_resource_id
5175 		 , status
5176 		 , dispatched_time
5177 		 , loaded_time
5178 		 , drop_off_time
5179 		 , SYSDATE
5180 		 , FND_GLOBAL.USER_ID
5181 		 , creation_date
5182 		 , created_by
5183 		 , fnd_global.USER_ID
5184 		 , attribute_category
5185 		 , attribute1
5186 		 , attribute2
5187 		 , attribute3
5188 		 , attribute4
5189 		 , attribute5
5190 		 , attribute6
5191 		 , attribute7
5192 		 , attribute8
5193 		 , attribute9
5194 		 , attribute10
5195 		 , attribute11
5196 		 , attribute12
5197 		 , attribute13
5198 		 , attribute14
5199 		 , attribute15
5200 		 , task_type
5201 		 , priority
5202 		 , task_group_id
5203 		 , device_id
5204 		 , device_invoked
5205 		 , device_request_id
5206 		 , suggested_dest_subinventory
5207 		 , suggested_dest_locator_id
5208 		 , operation_plan_id
5209 		 , l_inbound_doc_child_split_rec.move_order_line_id
5210 		 , transfer_lpn_id
5211 		 , l_wopi_split_rec.op_plan_instance_id
5212 		 , task_method
5213 		 FROM wms_dispatched_tasks
5214 		 WHERE transaction_temp_id = l_inbound_doc_child_rec.transaction_temp_id;
5215 	    END LOOP;
5216 	 END IF; --IF (l_num_wdts_for_mmtt > 1) THEN
5217 	 --END BUG 5075410: When splitting an MMTT, the WDT must also be
5218 
5219 	 RETURN;
5220          /*raise_application_error(INVALID_PLAN_ID, 'Invalid Plan');*/
5221          /*Raise user defined exception to populated error code*/
5222       END IF;
5223 
5224       /*Query MMTT based on parent_line_id*/
5225       OPEN inbound_document_details(l_inbound_doc_child_rec.parent_line_id);
5226       FETCH inbound_document_details INTO l_inbound_doc_parent_rec;
5227       l_progress := 30;
5228 
5229       IF (inbound_document_details%NOTFOUND) THEN
5230          IF (l_debug=1) THEN
5231             print_debug('Invalid document',l_module_name,3);
5232          END IF;
5233          raise_application_error(INVALID_DOC_ID,'Invalid Document');
5234       END IF;
5235 
5236       CLOSE inbound_document_details;
5237 
5238       /*update parent transaction quantity, PRIMARY_QUANTITY, create MTLT if appropriate*/
5239       l_inbound_doc_parent_rec.transaction_quantity := l_inbound_doc_child_rec.transaction_quantity;
5240       l_inbound_doc_parent_rec.primary_quantity := l_inbound_doc_child_rec.primary_quantity;
5241 
5242       l_progress := 40;
5243       --call MMTT table handler to update records in the table
5244 
5245       UPDATE mtl_material_transactions_temp
5246 	SET transaction_quantity = l_inbound_doc_parent_rec.transaction_quantity,
5247 	    primary_quantity = l_inbound_doc_child_rec.primary_quantity,
5248 	    last_update_date = SYSDATE
5249 	WHERE transaction_temp_id = l_inbound_doc_parent_rec.transaction_temp_id;
5250       l_progress := 50;
5251 
5252 
5253       /*If the item is a Lot Controlled Item, Query MTL_TRANSACTIONS_LOT_TEM (MTLT) based on
5254        P_Source_task_ID and poplate the PL/SQL record variable.*/
5255      /*Checking if the MTLTs exist for a Lot Controlled Item.If there are no records found throw an Invalid
5256       Document Error as the Assumption made is that rules will always create MTLTs for a lot controlled item
5257       --Have to finalise on this and will change if required*/
5258 
5259       OPEN c_item_details(l_inbound_doc_child_rec.INVENTORY_ITEM_ID,l_inbound_doc_child_rec.organization_id);
5260 
5261       FETCH c_item_details INTO l_item_details_rec;
5262 
5263       IF (c_item_details%NOTFOUND) THEN
5264 
5265          IF (l_debug=1) THEN
5266             print_debug('Item -Org combnation not found',l_module_name,1);
5267          END IF;
5268          RAISE FND_API.G_EXC_ERROR;
5269 
5270       END IF;
5271 
5272       CLOSE c_item_details;
5273 
5274       IF (l_item_details_rec.lot_control_code=2) THEN
5275 
5276           IF (l_debug=1) THEN
5277              print_debug('Item is lot Controlled',l_module_name,9);
5278           END IF;
5279 
5280 	  --delete MTLT where transaction_temp_id = l_inbound_doc_parent_rec.transaction_temp_id
5281 	  DELETE FROM mtl_transaction_lots_temp WHERE transaction_temp_id = l_inbound_doc_parent_rec.transaction_temp_id;
5282 
5283 	  --create PL/SQL records for MTLT based on MTLT linked to l_inbound_doc_child_rec.transaction_temp_id
5284 
5285 
5286 
5287 	     l_progress := 70;
5288 
5289 	     INSERT INTO mtl_transaction_lots_temp
5290                 (
5291                  transaction_temp_id
5292                , last_update_date
5293                , last_updated_by
5294                , creation_date
5295                , created_by
5296                , transaction_quantity
5297                , primary_quantity
5298                , secondary_quantity
5299                , secondary_unit_of_measure
5300                , lot_number
5301                , lot_expiration_date
5302                , serial_transaction_temp_id
5303                , description
5304                , vendor_name
5305                , supplier_lot_number
5306                , origination_date
5307                , date_code
5308                , grade_code
5309                , change_date
5310                , maturity_date
5311                , status_id
5312                , retest_date
5313                , age
5314                , item_size
5315                , color
5316                , volume
5317                , volume_uom
5318                , place_of_origin
5319                , best_by_date
5320                , LENGTH
5321                , length_uom
5322                , recycled_content
5323                , thickness
5324                , thickness_uom
5325                , width
5326                , width_uom
5327                , curl_wrinkle_fold
5328                , lot_attribute_category
5329                , c_attribute1
5330                , c_attribute2
5331                , c_attribute3
5332                , c_attribute4
5333                , c_attribute5
5334                , c_attribute6
5335                , c_attribute7
5336                , c_attribute8
5337                , c_attribute9
5338                , c_attribute10
5339                , c_attribute11
5340                , c_attribute12
5341                , c_attribute13
5342                , c_attribute14
5343                , c_attribute15
5344                , c_attribute16
5345                , c_attribute17
5346                , c_attribute18
5347                , c_attribute19
5348                , c_attribute20
5349                , d_attribute1
5350                , d_attribute2
5351                , d_attribute3
5352                , d_attribute4
5353                , d_attribute5
5354                , d_attribute6
5355                , d_attribute7
5356                , d_attribute8
5357                , d_attribute9
5358                , d_attribute10
5359                , n_attribute1
5360                , n_attribute2
5361                , n_attribute3
5362                , n_attribute4
5363                , n_attribute5
5364                , n_attribute6
5365                , n_attribute7
5366                , n_attribute8
5367                , n_attribute9
5368                , n_attribute10
5369                , vendor_id
5370                , territory_code
5371                 )
5372 	     SELECT l_inbound_doc_parent_rec.transaction_temp_id
5373 	       , SYSDATE
5374                , FND_GLOBAL.USER_ID
5375                , SYSDATE
5376                , FND_GLOBAL.USER_ID
5377                , l_inbound_doc_parent_rec.transaction_quantity
5378                , l_inbound_doc_parent_rec.primary_quantity
5379                , secondary_quantity
5380                , secondary_unit_of_measure
5381                , lot_number
5382                , lot_expiration_date
5383                , serial_transaction_temp_id
5384                , description
5385                , vendor_name
5386                , supplier_lot_number
5387                , origination_date
5388                , date_code
5389                , grade_code
5390                , change_date
5391                , maturity_date
5392                , status_id
5393                , retest_date
5394                , age
5395                , item_size
5396                , color
5397                , volume
5398                , volume_uom
5399                , place_of_origin
5400                , best_by_date
5401                , LENGTH
5402                , length_uom
5403                , recycled_content
5404                , thickness
5405                , thickness_uom
5406                , width
5407                , width_uom
5408                , curl_wrinkle_fold
5409                , lot_attribute_category
5410                , c_attribute1
5411                , c_attribute2
5412                , c_attribute3
5413                , c_attribute4
5414                , c_attribute5
5415                , c_attribute6
5416                , c_attribute7
5417                , c_attribute8
5418                , c_attribute9
5419                , c_attribute10
5420                , c_attribute11
5421                , c_attribute12
5422                , c_attribute13
5423                , c_attribute14
5424                , c_attribute15
5425                , c_attribute16
5426                , c_attribute17
5427                , c_attribute18
5428                , c_attribute19
5429                , c_attribute20
5430                , d_attribute1
5431                , d_attribute2
5432                , d_attribute3
5433                , d_attribute4
5434                , d_attribute5
5435                , d_attribute6
5436                , d_attribute7
5437                , d_attribute8
5438                , d_attribute9
5439                , d_attribute10
5440                , n_attribute1
5441                , n_attribute2
5442                , n_attribute3
5443                , n_attribute4
5444                , n_attribute5
5445                , n_attribute6
5446                , n_attribute7
5447                , n_attribute8
5448                , n_attribute9
5449                , n_attribute10
5450                , vendor_id
5451                , territory_code
5452 	       FROM mtl_transaction_lots_temp
5453 	       WHERE transaction_temp_id = l_inbound_doc_child_rec.transaction_temp_id;
5454 
5455       END IF;
5456 
5457       /* BEGIN GRACE11-02-2003
5458        * If there is a WDT record for the original child MMTT, we need to
5459        * duplicate WDT for each new child MMTT records.
5460        */
5461       SELECT COUNT(1) INTO l_num_wdts_for_mmtt
5462 	 FROM wms_dispatched_tasks
5463 	 WHERE transaction_temp_id = l_inbound_doc_child_rec.transaction_temp_id;
5464       /* END: GRACE11-02-2003 */
5465 
5466       /* split parent MMTT, OP_Plan_Instance, OP_Operation_Instance.
5467        * Update parent_line_id for new child task records
5468        */
5469       FOR i IN 1 ..  p_new_task_id_table.COUNT LOOP
5470 	 /*query document table based on new_p_task_id_table(i)*/
5471 	 OPEN inbound_document_details(p_new_task_id_table(i));
5472 	 FETCH inbound_document_details INTO l_inbound_doc_child_split_rec;
5473 	 l_progress := 100;
5474 	 IF (inbound_document_details%NOTFOUND) THEN
5475 	    IF (l_debug=1) THEN
5476 	       print_debug('Invalid document',l_module_name,3);
5477 	    END IF;
5478 	    raise_application_error(INVALID_DOC_ID,'Invalid Document');
5479 	 END IF;
5480 
5481 	 CLOSE inbound_document_details;
5482 
5483 	 /*create parent document record for new task record*/
5484 
5485 	 --copy l_inbound_doc_parent_rec into l_inbound_doc_parent_split_rec as new parent RECORD
5486 	 l_inbound_doc_parent_split_rec := l_inbound_doc_parent_rec;
5487 	 l_inbound_doc_parent_split_rec.transaction_quantity := l_inbound_doc_child_split_rec.transaction_quantity;
5488 	 l_inbound_doc_parent_split_rec.primary_quantity := l_inbound_doc_child_split_rec.primary_quantity;
5489 	 l_progress := 110;
5490 	 --call MMTT(MTLT) table handlers to insert the new parent RECORD l_inbound_doc_parent_split_rec
5491 	 inv_trx_util_pub.copy_insert_line_trx(
5492 					       x_return_status            => x_return_status
5493 					       , x_msg_data               => x_msg_data
5494 					       , x_msg_count              => x_msg_count
5495 					       , x_new_txn_temp_id        => l_inbound_doc_parent_split_rec.transaction_temp_id
5496 					       , p_transaction_temp_id    => l_inbound_doc_parent_rec.transaction_temp_id
5497 					       , p_txn_qty                => l_inbound_doc_parent_split_rec.transaction_quantity
5498                                                , p_primary_qty            => l_inbound_doc_parent_split_rec.primary_quantity
5499 					       );
5500 
5501 	 IF (x_return_status=g_ret_sts_error) THEN
5502 	    RAISE FND_API.G_EXC_ERROR;
5503 	  ELSIF (x_return_status<>g_ret_sts_success) THEN
5504 	    RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5505 	 END IF;
5506 
5507 
5508 	 l_progress := 120;
5509 
5510 	 l_inbound_doc_child_split_rec.parent_line_id := l_inbound_doc_parent_split_rec.transaction_temp_id;
5511 	 --call MMTT table handlers to update the new child task record
5512 	 UPDATE mtl_material_transactions_temp
5513 	   SET parent_line_id = l_inbound_doc_parent_split_rec.transaction_temp_id
5514 	   WHERE transaction_temp_id = l_inbound_doc_child_split_rec.transaction_temp_id;
5515 	 l_progress := 140;
5516 
5517 	 --TODO: create records for MTLT based on MTLT records linked to l_inbound_doc_child_split_rec.transaction_temp_id
5518 	 --replace the transaction_temp_id with l_inbound_doc_parent_split_rec.transaction_temp_id, and insert into mtlt table
5519 
5520 
5521 	 OPEN c_item_details(l_inbound_doc_child_split_rec.INVENTORY_ITEM_ID,l_inbound_doc_child_split_rec.organization_id);
5522 
5523 	 FETCH c_item_details INTO l_item_details_rec;
5524 
5525 	 IF (c_item_details%NOTFOUND) THEN
5526 
5527 	    IF (l_debug=1) THEN
5528 	       print_debug('Item -Org combnation not found',l_module_name,1);
5529 	    END IF;
5530 	    RAISE FND_API.G_EXC_ERROR;
5531 
5532 	 END IF;
5533 
5534 	 CLOSE c_item_details;
5535 
5536 	 IF (l_item_details_rec.lot_control_code=2) THEN
5537 
5538 	    IF (l_debug=1) THEN
5539 	       print_debug('Item is lot Controlled',l_module_name,9);
5540 	    END IF;
5541 
5542 	    --create PL/SQL records for MTLT based on MTLT linked to l_inbound_doc_child_split_rec.transaction_temp_id
5543 
5544 
5545 	    l_progress := 70;
5546 
5547 	    INSERT INTO mtl_transaction_lots_temp
5548 	      (
5549 	       transaction_temp_id
5550                , last_update_date
5551                , last_updated_by
5552                , creation_date
5553                , created_by
5554                , transaction_quantity
5555                , primary_quantity
5556                , secondary_quantity
5557                , secondary_unit_of_measure
5558                , lot_number
5559                , lot_expiration_date
5560                , serial_transaction_temp_id
5561                , description
5562                , vendor_name
5563                , supplier_lot_number
5564                , origination_date
5565                , date_code
5566                , grade_code
5567                , change_date
5568                , maturity_date
5569                , status_id
5570                , retest_date
5571                , age
5572                , item_size
5573                , color
5574                , volume
5575                , volume_uom
5576                , place_of_origin
5577                , best_by_date
5578                , LENGTH
5579                , length_uom
5580                , recycled_content
5581                , thickness
5582                , thickness_uom
5583                , width
5584                , width_uom
5585                , curl_wrinkle_fold
5586                , lot_attribute_category
5587                , c_attribute1
5588                , c_attribute2
5589                , c_attribute3
5590                , c_attribute4
5591                , c_attribute5
5592                , c_attribute6
5593                , c_attribute7
5594                , c_attribute8
5595                , c_attribute9
5596                , c_attribute10
5597                , c_attribute11
5598                , c_attribute12
5599                , c_attribute13
5600                , c_attribute14
5601                , c_attribute15
5602                , c_attribute16
5603                , c_attribute17
5604                , c_attribute18
5605                , c_attribute19
5606                , c_attribute20
5607                , d_attribute1
5608                , d_attribute2
5609                , d_attribute3
5610                , d_attribute4
5611                , d_attribute5
5612                , d_attribute6
5613                , d_attribute7
5614                , d_attribute8
5615                , d_attribute9
5616                , d_attribute10
5617                , n_attribute1
5618                , n_attribute2
5619                , n_attribute3
5620                , n_attribute4
5621                , n_attribute5
5622                , n_attribute6
5623                , n_attribute7
5624                , n_attribute8
5625                , n_attribute9
5626                , n_attribute10
5627                , vendor_id
5628                , territory_code
5629                 )
5630 	      SELECT l_inbound_doc_parent_split_rec.transaction_temp_id
5631 	       , SYSDATE
5632 	       , FND_GLOBAL.USER_ID
5633                , SYSDATE
5634                , FND_GLOBAL.USER_ID
5635                , l_inbound_doc_parent_split_rec.transaction_quantity
5636                , l_inbound_doc_parent_split_rec.primary_quantity
5637                , secondary_quantity
5638                , secondary_unit_of_measure
5639                , lot_number
5640                , lot_expiration_date
5641                , serial_transaction_temp_id
5642                , description
5643                , vendor_name
5644                , supplier_lot_number
5645                , origination_date
5646                , date_code
5647                , grade_code
5648                , change_date
5649                , maturity_date
5650                , status_id
5651                , retest_date
5652                , age
5653                , item_size
5654                , color
5655                , volume
5656                , volume_uom
5657                , place_of_origin
5658                , best_by_date
5659                , LENGTH
5660                , length_uom
5661                , recycled_content
5662                , thickness
5663                , thickness_uom
5664                , width
5665                , width_uom
5666                , curl_wrinkle_fold
5667                , lot_attribute_category
5668                , c_attribute1
5669                , c_attribute2
5670                , c_attribute3
5671                , c_attribute4
5672                , c_attribute5
5673                , c_attribute6
5674                , c_attribute7
5675                , c_attribute8
5676                , c_attribute9
5677                , c_attribute10
5678                , c_attribute11
5679                , c_attribute12
5680                , c_attribute13
5681                , c_attribute14
5682                , c_attribute15
5683                , c_attribute16
5684                , c_attribute17
5685                , c_attribute18
5686                , c_attribute19
5687                , c_attribute20
5688                , d_attribute1
5689                , d_attribute2
5690                , d_attribute3
5691                , d_attribute4
5692                , d_attribute5
5693                , d_attribute6
5694                , d_attribute7
5695                , d_attribute8
5696                , d_attribute9
5697                , d_attribute10
5698                , n_attribute1
5699                , n_attribute2
5700                , n_attribute3
5701                , n_attribute4
5702                , n_attribute5
5703                , n_attribute6
5704                , n_attribute7
5705                , n_attribute8
5706                , n_attribute9
5707                , n_attribute10
5708                , vendor_id
5709                , territory_code
5710 	      FROM mtl_transaction_lots_temp
5711 	      WHERE transaction_temp_id = l_inbound_doc_child_split_rec.transaction_temp_id;
5712 
5713 	 END IF;
5714 
5715 
5716 	 /*create op_plan_instances record for new task record*/
5717 	 OPEN wopi_cursor(l_inbound_doc_parent_rec.transaction_temp_id);
5718 	 FETCH wopi_cursor INTO l_wopi_orig_rec;
5719 	 l_progress := 150;
5720 	 IF (wopi_cursor%NOTFOUND) THEN
5721 	    -- NOTE: if no instances exist, the code below shouldn't be executed
5722 	    raise_application_error(PLAN_INSTANCE_NOT_EXITS,'Operation plan instance not exists');
5723 	 END IF;
5724 	 -- TODO: what if multiple instances exist?
5725 	 CLOSE wopi_cursor;
5726 
5727 	 --populate l_wopi_split_rec based on l_wopi_orig_rec
5728 	 l_wopi_split_rec := l_wopi_orig_rec;
5729 
5730 	 --l_wopi_split_rec.operation_plan_id := l_inbound_doc_parent_rec.operation_plan_id;
5731 	 l_wopi_split_rec.source_task_id := l_inbound_doc_parent_split_rec.transaction_temp_id;
5732 	 --l_wopi_source_task_idsplit_rec.status := l_wopi_orig_rec.status;
5733 
5734 	 l_progress := 160;
5735 	 --use sequence to generate op_plan_instance_id
5736 	 SELECT  WMS_OP_INSTANCE_S.NEXTVAL
5737 	   INTO l_wopi_split_rec.op_plan_instance_id
5738 	   FROM dual;
5739 	 l_progress := 170;
5740 	 --call wms_op_plan_instance table handler to insert l_wopi_split_rec INTO the table
5741 
5742 	 l_progress := 180;
5743 	 WMS_OP_RUNTIME_PVT_APIS.INSERT_PLAN_INSTANCE(
5744            x_return_status => x_return_status        ,
5745            x_msg_count     => x_msg_count            ,
5746            x_msg_data      => x_msg_data             ,
5747            p_insert_rec    => l_wopi_split_rec );
5748 
5749 	 IF (x_return_status=g_ret_sts_error) THEN
5750 	    RAISE FND_API.G_EXC_ERROR;
5751 	  ELSIF (x_return_status<>g_ret_sts_success) THEN
5752 	    RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5753 	 END IF;
5754 	 /* BEGIN:  GRACE11-02-2003
5755 	  * For each new WOPI that we are creating, we should copy all WDTH
5756 	  * records from the the original WOPI, and link these new WDTH
5757 	  * records to the new WOPI. The new WDTH will have same
5758 	  * transaction_quantity as l_inbound_doc_parent_split_rec. And we need to
5759 	  * update transaction_quantity of the original WDTHs to be same
5760 	  * as l_inbound_doc_parent_rec.
5761 	  */
5762 	 l_progress := 185;
5763 
5764 	  -- TODO: how is wms_dispatched_tasks_history linked to wms_op_plan_instances
5765 	  UPDATE wms_dispatched_tasks_history
5766 	    SET transaction_quantity = l_inbound_doc_parent_rec.transaction_quantity,
5767 	        last_update_date = Sysdate    -- bug 3827507
5768 	    WHERE op_plan_instance_id =l_wopi_orig_rec.op_plan_instance_id;
5769 
5770 	  OPEN wdth_cursor(l_wopi_orig_rec.op_plan_instance_id);
5771 
5772 	  -- loop through wdth with the same op_plan_instance_id -lezhang
5773 	  LOOP
5774 	     FETCH wdth_cursor INTO l_task_id, l_old_temp_id;
5775 	     EXIT WHEN wdth_cursor%notfound;
5776 
5777 	     SELECT wms_dispatched_tasks_s.NEXTVAL
5778 	       INTO   l_next_task_id
5779 	       FROM   DUAL;
5780 
5781 	     SELECT mtl_material_transactions_s.NEXTVAL
5782 	       INTO l_new_temp_id
5783 	       FROM dual;
5784 	     -- TODO: how to construct new WDTHs from those linked to orig WOPI
5785 	     --INSERT INTO wms_dispatched_tasks_history ???;
5786 	     INSERT INTO wms_dispatched_tasks_history
5787 	       (
5788 		task_id
5789 		, transaction_id
5790 		, organization_id
5791 		, user_task_type
5792 		, person_id
5793 		, effective_start_date
5794 		, effective_end_date
5795 		, equipment_id
5796 		, equipment_instance
5797 		, person_resource_id
5798 		, machine_resource_id
5799 		, status
5800 		, dispatched_time
5801 		, loaded_time
5802 		, drop_off_time
5803 		, last_update_date
5804 		, last_updated_by
5805 		, creation_date
5806 		, created_by
5807 		, last_update_login
5808 		, attribute_category
5809 		, attribute1
5810 		, attribute2
5811 		, attribute3
5812 		, attribute4
5813 		, attribute5
5814 		, attribute6
5815 		, attribute7
5816 		, attribute8
5817 		, attribute9
5818 		, attribute10
5819 		, attribute11
5820 		, attribute12
5821 		, attribute13
5822 		, attribute14
5823 		, attribute15
5824 		, task_type
5825 		, priority
5826 		, task_group_id
5827 		, suggested_dest_subinventory
5828 		, suggested_dest_locator_id
5829 		, operation_plan_id
5830 		, move_order_line_id
5831 		, transfer_lpn_id
5832 		, transaction_batch_id
5833 		, transaction_batch_seq
5834 		, inventory_item_id
5835 		, revision
5836 		, transaction_quantity
5837 		, transaction_uom_code
5838 		, source_subinventory_code
5839 		, source_locator_id
5840 		, dest_subinventory_code
5841 	       , dest_locator_id
5842 	       , lpn_id
5843 	       , content_lpn_id
5844 	       , is_parent
5845 	       , parent_transaction_id
5846 	       , transfer_organization_id
5847 	       , source_document_id
5848 	       , op_plan_instance_id
5849 	       , task_method
5850 	       , transaction_type_id
5851 	       , transaction_source_type_id
5852 	       , transaction_action_id
5853 	       , transaction_temp_id  -- new link between wdth and we
5854 	       )
5855 	       SELECT l_next_task_id
5856 	       , transaction_id
5857 	       , organization_id
5858 	       , user_task_type
5859 	       , person_id
5860 	       , effective_start_date
5861 	       , effective_end_date
5862 	       , equipment_id
5863 	       , equipment_instance
5864 	       , person_resource_id
5865 	       , machine_resource_id
5866 	       , status
5867 	       , dispatched_time
5868 	       , loaded_time
5869 	       , drop_off_time
5870 	       , SYSDATE
5871 	       , fnd_global.user_id
5872 	       , SYSDATE               -- bug 3827507
5873 	       , fnd_global.user_id    -- bug 3827507
5874 	       , fnd_global.user_id
5875 	       , attribute_category
5876 	       , attribute1
5877 	       , attribute2
5878 	       , attribute3
5879 	       , attribute4
5880 	       , attribute5
5881 	       , attribute6
5882 	       , attribute7
5883 	       , attribute8
5884 	       , attribute9
5885 	       , attribute10
5886 	       , attribute11
5887 	       , attribute12
5888 	       , attribute13
5889 	       , attribute14
5890 	       , attribute15
5891 	       , task_type
5892 	       , priority
5893 	       , task_group_id
5894 	       , suggested_dest_subinventory
5895 	       , suggested_dest_locator_id
5896 	       , operation_plan_id
5897 	       , l_inbound_doc_child_split_rec.move_order_line_id
5898 	       , transfer_lpn_id
5899 	       , transaction_batch_id
5900 	       , transaction_batch_seq
5901 	       , inventory_item_id
5902 	       , revision
5903 	       , l_inbound_doc_parent_split_rec.transaction_quantity
5904 	       , transaction_uom_code
5905 	       , source_subinventory_code
5906 	       , source_locator_id
5907 	       , dest_subinventory_code
5908 	       , dest_locator_id
5909 	       , lpn_id
5910 	       , content_lpn_id
5911 	       , is_parent
5912 	       , l_inbound_doc_parent_split_rec.transaction_temp_id
5913 	       , transfer_organization_id
5914 	       , source_document_id
5915 	       , l_wopi_split_rec.op_plan_instance_id
5916 	       , task_method
5917 	       , transaction_type_id
5918 	       , transaction_source_type_id
5919 	       , transaction_action_id
5920 	       , l_new_temp_id   -- new value to link wdth and we -- lezhang
5921 	       FROM   wms_dispatched_tasks_history
5922 	       WHERE  task_id = l_task_id;
5923 
5924 
5925 
5926 	     INSERT INTO wms_exceptions
5927 	       (
5928 		task_id
5929 		, sequence_number
5930 		, organization_id
5931 		, inventory_item_id
5932 		, person_id
5933 		, effective_start_date
5934 		, effective_end_date
5935 		, inventory_location_id
5936 		, reason_id
5937 		, discrepancy_type
5938 		, archive_flag
5939 		, subinventory_code
5940 		, lot_number
5941 		, revision
5942 		, last_update_date
5943 		, last_updated_by
5944 		, creation_date
5945 		, created_by
5946 		, last_update_login
5947 		, attribute_category
5948 		, attribute1
5949 		, attribute2
5950 		, attribute3
5951 		, attribute4
5952 		, attribute5
5953 		, attribute6
5954 		, attribute7
5955 		, attribute8
5956 		, attribute9
5957 		, attribute10
5958 		, attribute11
5959 		, attribute12
5960 		, attribute13
5961 		, attribute14
5962 		, attribute15
5963 		, transaction_header_id
5964 		, wms_task_type
5965 		, wf_item_key
5966 		, lpn_id
5967 		)
5968 	       SELECT
5969 	       l_new_temp_id -- new value to link wdth and we -- lezhang
5970 	       , sequence_number
5971 	       , organization_id
5972 	       , inventory_item_id
5973 	       , person_id
5974 	       , effective_start_date
5975 	       , effective_end_date
5976 	       , inventory_location_id
5977 	       , reason_id
5978 	       , discrepancy_type
5979 	       , archive_flag
5980 	       , subinventory_code
5981 	       , lot_number
5982 	       , revision
5983 	       , last_update_date
5984 	       , last_updated_by
5985 	       , creation_date
5986 	       , created_by
5987 	       , last_update_login
5988 	       , attribute_category
5989 	       , attribute1
5990 	       , attribute2
5991 	       , attribute3
5992 	       , attribute4
5993 	       , attribute5
5994 	       , attribute6
5995 	       , attribute7
5996 	       , attribute8
5997 	       , attribute9
5998 	       , attribute10
5999 	       , attribute11
6000 	       , attribute12
6001 	       , attribute13
6002 	       , attribute14
6003 	       , attribute15
6004 	       , transaction_header_id
6005 	       , wms_task_type
6006 	       , wf_item_key
6007 	       , lpn_id
6008 	       FROM wms_exceptions
6009 	       WHERE task_id = l_old_temp_id;
6010 
6011 	  END LOOP;
6012 
6013 	  CLOSE wdth_cursor;
6014 
6015 	  -- END: GRACE11-02-2003
6016 	  l_progress := 190;
6017 
6018 
6019 
6020 	 /*create op_operation_instances record for new task record*/
6021 	 -- NOTE: here assume that it is ok if no operation instances exist
6022 	 OPEN wooi_cursor(l_wopi_orig_rec.op_plan_instance_id);
6023 	 l_wooi_index := 0;
6024 	 l_is_last_step_drop := FALSE;
6025 	 LOOP
6026 	    FETCH wooi_cursor INTO l_wooi_orig_rec;
6027 	    l_progress := 200;
6028 	    EXIT WHEN wooi_cursor%NOTFOUND;
6029 	    --populate l_wooi_new_rec based on l_wooi_orig_rec
6030 	    l_wooi_index := l_wooi_index + 1;
6031 	    l_wooi_new_rec := l_wooi_orig_rec;
6032 
6033 	    /* BEGIN GRACE11_02_2003, note that 'ORDER BY operation_sequence DESC' is added to the cursor definition
6034 	     *  Now we update wooi.source_task_id if wooi.status =
6035 	     *  'Pending'. We need to change this to: update
6036 	     *  wooi.source_task_id for the last wooi, if the last step is
6037 	     *  load; and update wooi.source_task_id for the last TWO wooi
6038 	     *  records, if the last step is drop.
6039 	     * IF (l_wooi_orig_rec.operation_status = G_OP_INS_STAT_PENDING) THEN
6040 	     *   l_wooi_new_rec.source_task_id := p_new_task_id_table(i);
6041 	     * END IF;
6042 	     */
6043 	    IF (l_wooi_index = 1) THEN
6044 	       IF (l_wooi_orig_rec.OPERATION_TYPE_ID = 2) THEN
6045 		  l_is_last_step_drop := TRUE;
6046 	       END IF;
6047 	       IF (l_wooi_orig_rec.OPERATION_TYPE_ID = 1 OR l_wooi_orig_rec.OPERATION_TYPE_ID = 2 OR
6048               l_wooi_orig_rec.operation_type_id =G_OP_TYPE_INSPECT ) THEN
6049 		  l_wooi_new_rec.source_task_id := p_new_task_id_table(i);
6050 	       END IF;
6051 	    END IF;
6052 
6053 	    IF (l_wooi_index = 2) THEN
6054 	       IF (l_is_last_step_drop = TRUE) THEN
6055 		  l_wooi_new_rec.source_task_id := p_new_task_id_table(i);
6056 	       END IF;
6057 	    END IF;
6058 	    /* END GRACE11_02_2003 */
6059 
6060 	    l_wooi_new_rec.op_plan_instance_id := l_wopi_split_rec.op_plan_instance_id;
6061 	    l_progress := 210;
6062 	    --Call wms_op_operation_instances table handler to insert the record
6063 	    SELECT wms_op_instance_s.NEXTVAL
6064 	      INTO l_wooi_new_rec.operation_instance_id
6065 	      FROM dual;
6066 	    l_progress := 220;
6067 
6068 	    l_progress := 230;
6069 	    WMS_OP_RUNTIME_PVT_APIS.insert_operation_instance
6070 	      ( x_return_status => x_return_status,
6071 		x_msg_count     => x_msg_count,
6072 		x_msg_data      => x_msg_data,
6073 		p_insert_rec    => l_wooi_new_rec
6074 		);
6075 	    l_progress := 240;
6076 
6077 	    IF (l_debug=1) THEN
6078 	       print_debug('Return Status from table handler',l_module_name,9);
6079 	    END IF;
6080 
6081 	    IF (x_return_status=g_ret_sts_error) THEN
6082 	       RAISE FND_API.G_EXC_ERROR;
6083 	     ELSIF (x_return_status<>g_ret_sts_success) THEN
6084 	       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6085 	    END IF;
6086 
6087 	 END LOOP;
6088 	 CLOSE wooi_cursor;
6089 
6090 	 /* BEGIN GRACE11-02-2003
6091 	  * If there is a WDT record for the original child MMTT, we need to
6092 	  * duplicate WDT for each new child MMTT records.
6093 	  */
6094 	 IF (l_num_wdts_for_mmtt > 0) THEN
6095 	    -- TODO: how to duplicate WDT of original child MMTT (l_inbound_doc_child_rec)???
6096 	    --INSERT INTO wms_dispatched_tasks ???;
6097 	    SELECT wms_dispatched_tasks_s.NEXTVAL
6098 	      INTO   l_next_task_id
6099 	      FROM   DUAL;
6100 
6101 	    INSERT INTO wms_dispatched_tasks
6102 	      (
6103 	       task_id
6104 	       , transaction_temp_id
6105 	       , organization_id
6106 	       , user_task_type
6107 	       , person_id
6108 	       , effective_start_date
6109 	       , effective_end_date
6110 	       , equipment_id
6111 	       , equipment_instance
6112 	       , person_resource_id
6113 	       , machine_resource_id
6114 	       , status
6115 	       , dispatched_time
6116 	       , loaded_time
6117 	       , drop_off_time
6118 	       , last_update_date
6119 	       , last_updated_by
6120 	       , creation_date
6121 	       , created_by
6122 	       , last_update_login
6123 	       , attribute_category
6124 	       , attribute1
6125 	       , attribute2
6126 	       , attribute3
6127 	       , attribute4
6128 	       , attribute5
6129 	       , attribute6
6130 	       , attribute7
6131 	       , attribute8
6132 	       , attribute9
6133 	       , attribute10
6134 	       , attribute11
6135 	       , attribute12
6136 	       , attribute13
6137 	       , attribute14
6138 	       , attribute15
6139 	       , task_type
6140 	       , priority
6141 	       , task_group_id
6142 	       , device_id
6143 	       , device_invoked
6144 	       , device_request_id
6145 	       , suggested_dest_subinventory
6146 	      , suggested_dest_locator_id
6147 	      , operation_plan_id
6148 	      , move_order_line_id
6149 	      , transfer_lpn_id
6150 	      , op_plan_instance_id
6151 	      , task_method
6152 	      )
6153 	      select l_next_task_id
6154 	      , l_inbound_doc_child_split_rec.transaction_temp_id
6155 	      , organization_id
6156 	      , user_task_type
6157 	      , person_id
6158 	      , effective_start_date
6159 	      , effective_end_date
6160 	      , equipment_id
6161 	      , equipment_instance
6162 	      , person_resource_id
6163 	      , machine_resource_id
6164 	      , status
6165 	      , dispatched_time
6166 	      , loaded_time
6167 	      , drop_off_time
6168 	      , SYSDATE
6169 	      , FND_GLOBAL.USER_ID
6170 	      , creation_date
6171 	      , created_by
6172 	      , fnd_global.USER_ID
6173 	      , attribute_category
6174 	      , attribute1
6175 	      , attribute2
6176 	      , attribute3
6177 	      , attribute4
6178 	      , attribute5
6179 	      , attribute6
6180 	      , attribute7
6181 	      , attribute8
6182 	      , attribute9
6183 	      , attribute10
6184 	      , attribute11
6185 	      , attribute12
6186 	      , attribute13
6187 	      , attribute14
6188 	      , attribute15
6189 	      , task_type
6190 	      , priority
6191 	      , task_group_id
6192 	      , device_id
6193 	      , device_invoked
6194 	      , device_request_id
6195 	      , suggested_dest_subinventory
6196 	      , suggested_dest_locator_id
6197 	      , operation_plan_id
6198 	      , l_inbound_doc_child_split_rec.move_order_line_id
6199 	      , transfer_lpn_id
6200 	      , l_wopi_split_rec.op_plan_instance_id
6201 	      , task_method
6202 	      FROM wms_dispatched_tasks
6203 	      WHERE transaction_temp_id = l_inbound_doc_child_rec.transaction_temp_id;
6204 
6205 
6206 
6207 	 END IF;
6208 	 /* END: GRACE11-02-2003 */
6209 
6210       END LOOP;
6211  EXCEPTION
6212     WHEN FND_API.G_EXC_ERROR THEN
6213        IF (l_debug=1) THEN
6214 	  print_debug('Error (fnd_api.g_exc_error) occured at'||l_progress,l_module_name,1);
6215        END IF;
6216 
6217        IF (inbound_document_details%ISOPEN) THEN
6218 	  CLOSE inbound_document_details;
6219        END IF;
6220 
6221        IF (wopi_cursor%ISOPEN) THEN
6222 	  CLOSE wopi_cursor;
6223        END IF;
6224 
6225        IF (wooi_cursor%ISOPEN) THEN
6226 	  CLOSE wooi_cursor;
6227        END IF;
6228 
6229        IF (wdth_cursor%isopen) THEN
6230 	  CLOSE wdth_cursor;
6231        END IF;
6232 
6233        ROLLBACK TO split_op_instance_sp;
6234 
6235        x_return_status:=g_ret_sts_error;
6236 
6237     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
6238        IF (l_debug=1) THEN
6239 	  print_debug('Unexpected Error (fnd_api.g_exc_unexpected_error) occured at '||l_progress,l_module_name,1);
6240 	  --print_debug('UnExpected Error: '||SQLERRM,l_module_name,1);
6241        END IF;
6242 
6243        IF (inbound_document_details%ISOPEN) THEN
6244 	  CLOSE inbound_document_details;
6245        END IF;
6246 
6247        IF (wopi_cursor%ISOPEN) THEN
6248              CLOSE wopi_cursor;
6249        END IF;
6250 
6251        IF (wooi_cursor%ISOPEN) THEN
6252 	  CLOSE wooi_cursor;
6253        END IF;
6254 
6255        ROLLBACK TO split_op_instance_sp;
6256 
6257        x_return_status:=g_ret_sts_unexp_error;
6258 
6259     WHEN OTHERS THEN
6260        print_debug(SQLERRM, l_module_name, 1);
6261 
6262        IF fnd_msg_pub.check_msg_level(g_msg_lvl_unexp_error) THEN
6263 	  fnd_msg_pub.add_exc_msg(g_pkg_name, l_module_name, SQLERRM);
6264        END IF; /* fnd_msg.... */
6265 
6266        fnd_msg_pub.count_and_get(
6267 				  p_count => x_msg_count,
6268 				  p_data  => x_msg_data
6269 				  );
6270 
6271        IF (SQLCODE<-20000) THEN
6272 	  IF (l_debug=1) THEN
6273 	     print_debug('This is an user defined exception occured at '||l_progress,l_module_name,1);
6274 	  END IF;
6275 	  x_error_code:=-(SQLCODE+20000);
6276 	  x_return_status:=g_ret_sts_error;
6277 	ELSE
6278 	  x_return_status := g_ret_sts_unexp_error;
6279        END IF;
6280 
6281        IF (inbound_document_details%ISOPEN) THEN
6282 	  CLOSE inbound_document_details;
6283        END IF;
6284 
6285        IF (wopi_cursor%ISOPEN) THEN
6286 	  CLOSE wopi_cursor;
6287        END IF;
6288 
6289        IF (wooi_cursor%ISOPEN) THEN
6290 	  CLOSE wooi_cursor;
6291        END IF;
6292 
6293        IF (wdth_cursor%isopen) THEN
6294 	  CLOSE wdth_cursor;
6295        END IF;
6296 
6297        ROLLBACK TO split_op_instance_sp;
6298 
6299  END Split_Operation_instance;
6300 
6301 
6302 
6303 /**
6304   *   Cleanup_operation_instance
6305   *   <p>This procedure reverts the status of current operation
6306   *   instance whenever any processing exception occurs i.e. ,
6307   *   when user presses F2 or transaction manager errors
6308   *   out.</p>
6309   *
6310   *  @param x_return_status    Return Status
6311   *  @param x_msg_data         REturns the Message Data
6312   *  @param x_msg_count        Returns the Error Message
6313   *  @param x_error_code       Returns appropriate error code in case of an error .
6314   *  @param p_source_task_id   Identifier of the source document record
6315   *  @param p_activity_type_id Lookup code of the Activity type Id
6316   **/
6317  PROCEDURE   Cleanup_Operation_Instance(
6318       x_return_status     OUT  NOCOPY  VARCHAR2,
6319       x_msg_data          OUT  NOCOPY  FND_NEW_MESSAGES.MESSAGE_TEXT%TYPE,
6320       x_msg_count         OUT  NOCOPY  NUMBER,
6321       x_error_code        OUT  NOCOPY  NUMBER,
6322       p_source_task_id    IN           NUMBER,
6323       p_activity_type_id  IN           NUMBER )IS
6324 
6325 	 l_debug               NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
6326 	 l_module_name         VARCHAR2(30) := 'Cleanup_operation_instance';
6327 	 l_progress            NUMBER;
6328 	 l_return_status       VARCHAR2(1);
6329 	 l_msg_count           NUMBER;
6330 	 l_msg_data            VARCHAR2(400);
6331 
6332 	 l_wdt_task_id         NUMBER;
6333 	 l_wdt_status          NUMBER;
6334 	 l_first_op_sequence   NUMBER;
6335 
6336 	 CURSOR c_inbound_document_details IS
6337 	    SELECT transaction_temp_id,
6338 	      operation_plan_id,
6339 	      organization_id
6340 	      FROM mtl_material_transactions_temp
6341 	      WHERE transaction_temp_id=p_source_task_id;
6342 
6343 	 l_inbound_doc_rec  c_inbound_document_details%ROWTYPE;
6344 
6345 	 CURSOR c_wooi_data_rec IS
6346 	    SELECT operation_instance_id,
6347 	      operation_type_id,
6348 	      operation_plan_detail_id,
6349 	      op_plan_instance_id,
6350 	      operation_status,
6351 	      operation_sequence,
6352 	      sug_to_sub_code,
6353 	      sug_to_locator_id,
6354 	      organization_id
6355 	      FROM wms_op_operation_instances
6356 	      WHERE source_task_id = p_source_task_id
6357 	      AND activity_type_id = p_activity_type_id
6358 	      AND operation_status = g_op_ins_stat_active;
6359 
6360 	 l_wooi_data_rec c_wooi_data_rec%ROWTYPE:=NULL;
6361 	 l_wooi_rec wms_op_operation_instances%ROWTYPE:=NULL;
6362 
6363 	 l_wopi_update_rec wms_op_plan_instances%ROWTYPE:=NULL;
6364  BEGIN
6365 
6366     IF (l_debug = 1) THEN
6367        print_debug(' Entered. ',l_module_name,1);
6368        print_DEBUG(' p_source_task_id ==> '||p_source_task_id ,l_module_name,3);
6369        print_DEBUG(' p_activity_type_id ==> '||p_activity_type_id ,l_module_name,3);
6370     END IF;
6371     x_return_status  := g_ret_sts_success;
6372     l_progress := 10;
6373     SAVEPOINT sp_cleanup_oprtn_instance;
6374 
6375 
6376     IF p_source_task_id IS NULL THEN
6377        IF (l_debug=1) THEN
6378 	  print_debug('Invalid input param. p_source_task_id Cannot be NULL.',l_module_name,4);
6379        END IF;
6380        RAISE FND_API.G_EXC_ERROR;
6381     END IF;
6382 
6383 
6384     IF p_activity_type_id IS NULL THEN
6385        IF (l_debug=1) THEN
6386 	  print_debug('Invalid input param. p_activity_type_id Cannot be NULL.',l_module_name,4);
6387        END IF;
6388        RAISE FND_API.G_EXC_ERROR;
6389     END IF;
6390 
6391 
6392     l_progress := 20;
6393 
6394     BEGIN
6395        SELECT task_id, status
6396 	 INTO l_wdt_task_id, l_wdt_status
6397 	 FROM wms_dispatched_tasks
6398 	 WHERE transaction_temp_id = p_source_task_id
6399 	 AND task_type IN (g_wms_task_type_putaway, g_wms_task_type_inspect);
6400 
6401     EXCEPTION
6402        WHEN no_data_found THEN
6403 	  IF (l_debug=1) THEN
6404 	     print_debug('WDT does not exist for this MMTT.',l_module_name,4);
6405 	  END IF;
6406 	  RAISE FND_API.G_EXC_ERROR;
6407 
6408     END;
6409 
6410     l_progress := 30;
6411 
6412     IF (l_debug=1) THEN
6413        print_debug('l_wdt_task_id = '||l_wdt_task_id,l_module_name,4);
6414        print_debug('l_wdt_status = '||l_wdt_status,l_module_name,4);
6415     END IF;
6416 
6417     IF l_wdt_status NOT IN (g_task_status_loaded, g_task_status_dispatched) THEN
6418        IF (l_debug=1) THEN
6419 	  print_debug('Invalid WDT status.' ,l_module_name,4);
6420        END IF;
6421        fnd_message.set_name('WMS', 'WMS_ATF_INVALID_TASK_STATUS');
6422        fnd_msg_pub.ADD;
6423        RAISE FND_API.G_EXC_ERROR;
6424     END IF; -- IF l_wdt_status NOT IN (g_task_status_loaded, g_task_status_dispatched)
6425 
6426 
6427     IF l_wdt_status = g_task_status_dispatched THEN
6428 
6429        --This can happened for two operations today,
6430        --1.	Inspect
6431        --2.	Load
6432 
6433        --NOTE: Today the status of WDT is going to be 'Dispatched' for load and inspect
6434        --      (for drop it will be 'Loaded').
6435        --      But for Outbound/Warehousing tasks or when we have Inbound
6436        --      dispatched tasks then we CAN have a status of 'Active' for WDT records
6437 
6438        IF (l_debug=1) THEN
6439 	  print_debug('WDT status is dispatched, delete WDT record : task_id = ' || l_wdt_task_id,l_module_name,4);
6440        END IF;
6441 
6442        l_progress := 40;
6443 
6444        DELETE wms_dispatched_tasks
6445 	 WHERE task_id = l_wdt_task_id;
6446 
6447        l_progress := 50;
6448 
6449      ELSIF  l_wdt_status = g_task_status_loaded THEN
6450 
6451        IF (l_debug=1) THEN
6452 	  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);
6453        END IF;
6454 
6455 
6456     END IF;  --  IF l_wdt_status = g_task_status_dispatched
6457 
6458 
6459     l_progress := 60;
6460 
6461     OPEN c_inbound_document_details;
6462 
6463     FETCH c_inbound_document_details
6464       INTO l_inbound_doc_rec;
6465 
6466     IF c_inbound_document_details%notfound THEN
6467        IF (l_debug=1) THEN
6468 	  print_debug('Invalid input param. p_source_task_id does not match to an MMTT record.',l_module_name,4);
6469        END IF;
6470        RAISE FND_API.G_EXC_ERROR;
6471     END IF;
6472 
6473     CLOSE c_inbound_document_details;
6474 
6475     l_progress := 65;
6476 
6477 
6478 
6479     IF l_inbound_doc_rec.operation_plan_id IS NULL THEN
6480        IF (l_debug=1) THEN
6481 	  print_debug('Operation plan ID is null for this MMTT. Nothing more needs to be cleaned up.',l_module_name,4);
6482 	  print_debug('x_return_status ==> '||x_return_status,l_module_name,3);
6483 	  print_debug('x_msg_data ==> '||x_msg_data,l_module_name,3);
6484 	  print_debug('x_msg_count ==> '||x_msg_count,l_module_name,3);
6485 	  print_debug('x_error_code ==> '||x_error_code,l_module_name,3);
6486 
6487 	  print_debug(' Before Exiting . ',l_module_name,3);
6488 
6489        END IF;
6490 
6491        IF  l_wdt_status = g_task_status_loaded THEN
6492 
6493 	  IF (l_debug = 1) THEN
6494 	     print_debug('l_wdt_status = '||g_task_status_loaded||', Drop operation, call WMS_OP_INBOUND_PVT.cleanup with following parameters: ',l_module_name,4);
6495 	     print_debug('p_source_task_id => ' || p_source_task_id,l_module_name,4);
6496 	  END IF;
6497 
6498 	  l_progress := 72;
6499 
6500 	  -- This call will cleanup wms_task_type (-1 -> 2) and lpn columns
6501 
6502 	  wms_op_inbound_pvt.cleanup
6503 	    (
6504 	     x_return_status      => l_return_status
6505 	     , x_msg_data         => l_msg_data
6506 	     , x_msg_count        => l_msg_count
6507 	     , p_source_task_id   => p_source_task_id
6508 	     );
6509 
6510 	  l_progress := 74;
6511 
6512 	  IF (l_debug=1) THEN
6513 	     print_debug('After calling wms_op_inbound_pvt.cleanup.',l_module_name,4);
6514 	     print_debug('x_return_status => '|| l_return_status,l_module_name,4);
6515 	     print_debug('x_msg_count => '|| l_msg_count,l_module_name,4);
6516 	     print_debug('x_msg_data => '|| l_msg_data,l_module_name,4);
6517 	  END IF;
6518 
6519 	  IF l_return_status <>FND_API.g_ret_sts_success THEN
6520 	     IF (l_debug=1) THEN
6521 		print_debug('wms_op_inbound_pvt.cleanup finished with error. l_return_status = ' || l_return_status,l_module_name,4);
6522 	     END IF;
6523 
6524 	     RAISE FND_API.G_EXC_ERROR;
6525 	  END IF;
6526 
6527        END IF;
6528 
6529        RETURN;
6530 
6531     END IF;  -- IF l_mmtt_rec.operation_plan_id IS NULL
6532 
6533     l_progress := 80;
6534 
6535     -- fetch the current active operation instance for this task
6536     OPEN c_wooi_data_rec;
6537 
6538     FETCH c_wooi_data_rec INTO l_wooi_data_rec;
6539     IF c_wooi_data_rec%notfound THEN
6540        IF (l_debug = 1) THEN
6541 	  print_debug('Active operation instance record does not exist for this task.',l_module_name,4);
6542        END IF;
6543        fnd_message.set_name('WMS', 'WMS_ATF_NO_ACTIVE_PLAN');
6544        fnd_msg_pub.ADD;
6545        RAISE FND_API.G_EXC_ERROR;
6546     END IF;
6547 
6548     CLOSE c_wooi_data_rec;
6549 
6550     l_progress := 90;
6551 
6552     l_wooi_rec.operation_instance_id := l_wooi_data_rec.operation_instance_id;
6553     l_wooi_rec.operation_status := g_op_ins_stat_pending;
6554 
6555 
6556     IF p_activity_type_id = g_op_activity_inbound THEN
6557        IF (l_debug=1) THEN
6558 	  print_debug('Inbound Activity.',l_module_name,4);
6559        END IF;
6560 
6561 
6562        -- Firs need to update current active operation instance to pending
6563 
6564        IF (l_debug=1) THEN
6565 	  print_debug('Before calling wms_op_runtime_pvt_apis.update_operation_instance with following parameters: ',l_module_name,4);
6566 	  print_debug('l_wooi_rec.operation_instance_id => '|| l_wooi_rec.operation_instance_id,l_module_name,4);
6567 	  print_debug('l_wooi_rec.operation_status => '|| l_wooi_rec.operation_status,l_module_name,4);
6568        END IF;
6569 
6570        l_progress := 120;
6571 
6572        wms_op_runtime_pvt_apis.update_operation_instance
6573 	 (x_return_status     => l_return_status
6574 	  , x_msg_count       => l_msg_count
6575 	  , x_msg_data        => l_msg_data
6576 	  , p_update_rec      => l_wooi_rec);
6577 
6578        l_progress := 130;
6579 
6580        IF (l_debug=1) THEN
6581 	  print_debug('After calling wms_op_runtime_pvt_apis.update_operation_instance: ' ,l_module_name,4);
6582        	  print_debug('x_return_status => '|| l_return_status,l_module_name,4);
6583 	  print_debug('x_msg_count => '|| l_msg_count,l_module_name,4);
6584 	  print_debug('x_msg_data => '|| l_msg_data,l_module_name,4);
6585        END IF;
6586 
6587        IF l_return_status <>FND_API.g_ret_sts_success THEN
6588 	  IF (l_debug=1) THEN
6589 	     print_debug('wms_op_runtime_pvt_apis.update_operation_instance finished with error. l_return_status = ' || l_return_status,l_module_name,4);
6590 	  END IF;
6591 
6592 	  RAISE FND_API.G_EXC_ERROR;
6593        END IF;
6594 
6595        BEGIN
6596 	  -- If this the first operation within a plan,
6597 	  -- Need to revert plan instance to pending.
6598 
6599 	  l_progress := 130;
6600 
6601 	  SELECT MIN(wopd1.operation_sequence)
6602 	    INTO l_first_op_sequence
6603 	    FROM wms_op_plan_details wopd1
6604 	    WHERE wopd1.operation_plan_id =
6605 	    (SELECT wopd2.operation_plan_id
6606 	     FROM wms_op_plan_details wopd2
6607 	     WHERE wopd2.operation_plan_detail_id = l_wooi_data_rec.operation_plan_detail_id
6608 	     );
6609 
6610 	  l_progress := 140;
6611 
6612        EXCEPTION
6613 	  WHEN OTHERS THEN
6614 	     IF (l_debug=1) THEN
6615 		print_debug('Exception when getting min operation_sequence.' ,l_module_name,4);
6616 	     END IF;
6617 
6618 	     RAISE FND_API.G_EXC_ERROR;
6619        END;
6620 
6621 
6622        IF l_first_op_sequence = l_wooi_data_rec.operation_sequence AND
6623 	 l_wooi_data_rec.operation_type_id <> g_op_type_drop
6624 	 -- This check is necessary to make sure crossdock plan
6625 	 -- instance not incorrectly reverted to pending when cleaning up a drop
6626 	 -- since load/drop for a crossdock operation have the same operation_sequence
6627 	 THEN
6628 
6629 	  l_wopi_update_rec.status              := g_op_ins_stat_pending;
6630 	  l_wopi_update_rec.op_plan_instance_id := l_wooi_data_rec.op_plan_instance_id;
6631 
6632 	  IF (l_debug=1) THEN
6633 	     print_debug('Current operation is the first step of the operation plan.',l_module_name,4);
6634 
6635 	     print_debug('Calling WMS_OP_RUNTIME_PVT_APIS.Update_plan_instance with the following values to be updated',l_module_name,4);
6636 	     print_debug('l_wopi_update_rec.status    => '||l_wopi_update_rec.status,l_module_name,9);
6637 	     print_debug('l_wopi_update_rec.op_plan_instance_id => '||l_wopi_update_rec.op_plan_instance_id,l_module_name,9);
6638 	  END IF;
6639 
6640 	  l_progress := 150;
6641 
6642 	  WMS_OP_RUNTIME_PVT_APIS.Update_Plan_Instance
6643             (p_update_rec    => l_wopi_update_rec,
6644              x_return_status => l_return_status,
6645              x_msg_count     => x_msg_count,
6646              x_msg_data      => x_msg_data);
6647 
6648 	  l_progress := 160;
6649 
6650 	  IF (l_debug=1) THEN
6651 	     print_debug('After calling WMS_OP_RUNTIME_PVT_APIS.Update_plan_instance.',l_module_name,4);
6652 	     print_debug('x_return_status => '|| l_return_status,l_module_name,4);
6653 	     print_debug('x_msg_count => '|| l_msg_count,l_module_name,4);
6654 	     print_debug('x_msg_data => '|| l_msg_data,l_module_name,4);
6655 	  END IF;
6656 
6657 	  IF l_return_status <>FND_API.g_ret_sts_success THEN
6658 	     IF (l_debug=1) THEN
6659 		print_debug('wms_op_runtime_pvt_apis.Update_plan_instance finished with error. l_return_status = ' || l_return_status,l_module_name,4);
6660 	     END IF;
6661 
6662 	     RAISE FND_API.G_EXC_ERROR;
6663 	  END IF;
6664 
6665 
6666        END IF;  -- IF l_first_op_sequence = l_wooi_data_rec.operation_sequence
6667 
6668 
6669        IF l_wooi_data_rec.operation_type_id = g_op_type_drop THEN
6670 	  IF (l_debug = 1) THEN
6671 	     print_debug('Drop operation, call WMS_OP_INBOUND_PVT.cleanup with following parameters: ',l_module_name,4);
6672 	     print_debug('p_source_task_id => ' || p_source_task_id,l_module_name,4);
6673 	  END IF;
6674 
6675 	  l_progress := 170;
6676 
6677 	  wms_op_inbound_pvt.cleanup
6678 	    (
6679 	     x_return_status      => l_return_status
6680 	     , x_msg_data         => l_msg_data
6681 	     , x_msg_count        => l_msg_count
6682 	     , p_source_task_id   => p_source_task_id
6683 	     );
6684 
6685 	  l_progress := 180;
6686 
6687 	  IF (l_debug=1) THEN
6688 	     print_debug('After calling wms_op_inbound_pvt.cleanup.',l_module_name,4);
6689 	     print_debug('x_return_status => '|| l_return_status,l_module_name,4);
6690 	     print_debug('x_msg_count => '|| l_msg_count,l_module_name,4);
6691 	     print_debug('x_msg_data => '|| l_msg_data,l_module_name,4);
6692 	  END IF;
6693 
6694 	  IF l_return_status <>FND_API.g_ret_sts_success THEN
6695 	     IF (l_debug=1) THEN
6696 		print_debug('wms_op_inbound_pvt.cleanup finished with error. l_return_status = ' || l_return_status,l_module_name,4);
6697 	     END IF;
6698 
6699 	     RAISE FND_API.G_EXC_ERROR;
6700 	  END IF;
6701 
6702 
6703        END IF;  -- IF l_wooi_data_rec.operation_type_id = g_op_type_drop
6704 
6705 
6706     END IF; -- IF p_activity_type_id = g_op_activity_inbound
6707 
6708 
6709     IF (l_debug = 1) THEN
6710        print_debug('x_return_status ==> '||x_return_status,l_module_name,3);
6711        print_debug('x_msg_data ==> '||x_msg_data,l_module_name,3);
6712        print_debug('x_msg_count ==> '||x_msg_count,l_module_name,3);
6713        print_debug('x_error_code ==> '||x_error_code,l_module_name,3);
6714 
6715        print_debug(' Before Exiting . ',l_module_name,3);
6716     END IF;
6717 
6718  EXCEPTION
6719 
6720     WHEN fnd_api.g_exc_error THEN
6721        IF (l_debug=1) THEN
6722 	  print_debug('Error (fnd_api.g_exc_error) occured at'||l_progress,l_module_name,1);
6723        END IF;
6724        x_return_status:=FND_API.G_RET_STS_ERROR;
6725        ROLLBACK TO sp_cleanup_oprtn_instance;
6726 
6727        IF c_inbound_document_details%isopen THEN
6728 	  CLOSE c_inbound_document_details;
6729        END IF;
6730 
6731        IF c_wooi_data_rec%isopen THEN
6732 	  CLOSE c_wooi_data_rec;
6733        END IF;
6734        fnd_message.set_name('WMS', 'WMS_ATF_CLEANUP_FAILURE');
6735        fnd_msg_pub.ADD;
6736 
6737     WHEN fnd_api.g_exc_unexpected_error THEN
6738 
6739        x_return_status:=FND_API.G_RET_STS_UNEXP_ERROR;
6740        IF (l_debug=1) THEN
6741 	  print_debug('Unexpected Error (fnd_api.g_exc_unexpected_error) occured at '||l_progress,l_module_name,3);
6742        END IF;
6743        ROLLBACK TO sp_cleanup_oprtn_instance;
6744 
6745        IF c_inbound_document_details%isopen THEN
6746 	  CLOSE c_inbound_document_details;
6747        END IF;
6748 
6749        IF c_wooi_data_rec%isopen THEN
6750 	  CLOSE c_wooi_data_rec;
6751        END IF;
6752 
6753        fnd_message.set_name('WMS', 'WMS_ATF_CLEANUP_FAILURE');
6754        fnd_msg_pub.ADD;
6755 
6756     WHEN OTHERS THEN
6757 
6758        x_return_status:=FND_API.G_RET_STS_UNEXP_ERROR;
6759        IF (l_debug=1) THEN
6760 	  print_debug('Other Error occured at '||l_progress,l_module_name,1);
6761 	  IF SQLCODE IS NOT NULL THEN
6762 	     print_debug('With SQL error : ' || SQLERRM(SQLCODE), l_module_name,1);
6763 	  END IF;
6764        END IF;
6765        ROLLBACK TO sp_cleanup_oprtn_instance;
6766 
6767        IF c_inbound_document_details%isopen THEN
6768 	  CLOSE c_inbound_document_details;
6769        END IF;
6770 
6771        IF c_wooi_data_rec%isopen THEN
6772 	  CLOSE c_wooi_data_rec;
6773        END IF;
6774        fnd_message.set_name('WMS', 'WMS_ATF_CLEANUP_FAILURE');
6775        fnd_msg_pub.ADD;
6776 
6777  END Cleanup_Operation_instance;
6778 
6779 
6780 
6781  /**
6782   *   Cancel_operation_Plan
6783   *   <p>This procedure cancels the operation plan(s). It is called from control board</p>
6784   *
6785   *  @param x_return_status    Return Status
6786   *  @param x_msg_data         Returns the Message Data
6787   *  @param x_msg_count        Returns the Error Message
6788   *  @param x_error_code       Returns appropriate error code in case of an error .
6789   *  @param p_source_task_id   Identifier of the source document record
6790   *  @param p_activity_type_id Lookup code of the Activity type Id
6791   **/
6792    PROCEDURE  Cancel_Operation_Plan(
6793       x_return_status     OUT  NOCOPY  VARCHAR2,
6794       x_msg_data          OUT  NOCOPY  FND_NEW_MESSAGES.MESSAGE_TEXT%TYPE,
6795       x_msg_count         OUT  NOCOPY  NUMBER,
6796       x_error_code        OUT  NOCOPY  NUMBER,
6797       p_source_task_id    IN           NUMBER,
6798       p_activity_type_id  IN           NUMBER,
6799       p_retain_mmtt       IN   VARCHAR2 DEFAULT 'N',
6800       p_mmtt_error_code   IN   VARCHAR2 DEFAULT NULL,
6801       p_mmtt_error_explanation   IN   VARCHAR2 DEFAULT NULL
6802       ) IS
6803 
6804       CURSOR c_inbound_doc IS
6805        SELECT operation_plan_id,
6806               transaction_temp_id,
6807               parent_line_id
6808        FROM MTL_MATERIAL_TRANSACTIONS_TEMP
6809        WHERE transaction_temp_id = p_source_task_id;
6810 
6811       CURSOR c_operation_instance IS
6812          SELECT operation_instance_id,
6813                 operation_status,
6814                 op_plan_instance_id,
6815                 operation_type_id
6816          FROM WMS_OP_OPERATION_INSTANCES
6817          WHERE source_task_id = p_source_task_id
6818          AND activity_type_id = p_activity_type_id
6819 	   AND operation_status <> g_op_ins_stat_completed
6820 	   ORDER BY operation_sequence DESC;
6821 
6822 
6823        l_inbound_doc            c_inbound_doc%ROWTYPE;
6824        l_operation_instance_rec c_operation_instance%ROWTYPE;
6825        l_plan_status            NUMBER := -1;
6826        l_wooi_update_rec        WMS_OP_OPERATION_INSTANCES%ROWTYPE;
6827        l_wopi_update_rec        WMS_OP_PLAN_INSTANCES%ROWTYPE;
6828        l_parent_source_task_id  NUMBER;
6829 
6830 
6831        l_debug       NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
6832        l_module_name VARCHAR2(30) := 'Cancel_operation_Plan';
6833        l_progress    NUMBER;
6834 
6835    BEGIN
6836 
6837          IF (l_debug = 1) THEN
6838              print_DEBUG(' p_source_task_id ==> '||p_source_task_id ,l_module_name,3);
6839              print_DEBUG(' p_activity_type_id ==> '||p_activity_type_id ,l_module_name,3);
6840          END IF;
6841 
6842          SAVEPOINT cancel_op_plan_sp;
6843 
6844          x_return_status  := g_ret_sts_success;
6845          l_progress :=10;
6846 
6847          IF (p_source_task_id IS NULL OR p_activity_type_id IS NULL) THEN
6848 
6849             IF (l_debug=1) THEN
6850               print_debug('Invalid Input passed to Cancel_operation_plan',l_module_name,1);
6851             END IF;
6852 
6853             /*If the passed P_SOURCE_TASK_ID is null then raise Invalid Argument Error
6854              *and return with appropriate error code.
6855              */
6856 
6857             RAISE_APPLICATION_ERROR(INVALID_INPUT,'Invalid inputs passed'||p_source_task_id||' '||p_activity_type_id);
6858 
6859          END IF;
6860 
6861          l_progress:=20;
6862 
6863          /*Query the document tables based on p_source_task_id and populate the record variable.
6864            If p_activity_id=INBOUND then
6865            Query MMTT  where transaction_temp_id is equal p_source_task_id and
6866            populate the record variable.*/
6867          IF (p_activity_type_id = G_OP_ACTIVITY_INBOUND)  THEN
6868 
6869             OPEN c_inbound_doc;
6870 
6871             FETCH c_inbound_doc INTO l_inbound_doc;
6872 
6873             IF (c_inbound_doc%NOTFOUND) THEN
6874 
6875                CLOSE c_inbound_doc;
6876 
6877                IF (l_debug=1) THEN
6878                   print_debug('Document record does not exist',l_module_name,1);
6879                END IF;
6880 
6881                RAISE_APPLICATION_ERROR(INVALID_DOC_ID,'Invalid Document');
6882 
6883             END IF;
6884 
6885             CLOSE c_inbound_doc;
6886 
6887             l_progress:=30;
6888 
6889              /* Non ATF casse..Return Success*/
6890              IF l_inbound_doc.operation_plan_id IS NULL THEN
6891 
6892                 IF (l_debug=1) THEN
6893                   print_debug('Operation Plan Id null on MMTT:non ATF Case',l_module_name,9);
6894                 END IF;
6895 
6896                 RETURN;
6897 
6898              ELSE
6899                 l_parent_source_task_id:= l_inbound_doc.parent_line_id;
6900 
6901              END IF;/* Non ATF Case*/
6902 
6903          END IF; /*Activity is Inbound*/
6904 
6905          l_progress :=40;
6906 
6907          /*Populate the current Operation Instance (WOOI) record based
6908            on the p_source_task_id and the pending operation
6909           */
6910          OPEN c_operation_instance;
6911 
6912          FETCH c_operation_instance INTO l_operation_instance_rec;
6913 
6914          IF (c_operation_instance%NOTFOUND) THEN
6915 
6916             IF (l_debug=1) THEN
6917                print_debug('Operation Instance does not exists',l_module_name,9);
6918             END IF;
6919 
6920             CLOSE c_operation_instance;
6921 
6922             RAISE_APPLICATION_ERROR(OPERATION_INSTANCE_NOT_EXISTS,'Operation Instance does not exist for document record with Id '||p_source_task_id);
6923 
6924          END IF;
6925 
6926          CLOSE c_operation_instance;
6927 
6928          l_progress:=50;
6929 
6930 
6931          /* If the Operation Status is 'ACTIVE' then raise Invalid State Change Error,
6932           *  stating that you cannot change the status of a plan from 'ACTIVE' to 'CANCELLED'.
6933           */
6934 
6935          IF (l_operation_instance_rec.operation_status = G_OP_INS_STAT_ACTIVE)  THEN
6936 
6937             IF (l_debug=1) THEN
6938                print_debug('Invalid status for a cancel Operation',l_module_name,9);
6939             END IF;
6940 
6941             RAISE_APPLICATION_ERROR(INVALID_STATUS_FOR_OPERATION,'Cancellation cannot be performed on Active Record');
6942          END IF;
6943 
6944          l_progress:=60;
6945 
6946          /*If the current pending operation is 'DROP'
6947            Raise Error stating that cancellation cannot be performed
6948            for this operation and return appropriate err_code
6949            */
6950          IF (l_operation_instance_rec.operation_type_id =g_op_type_drop AND p_retain_mmtt = 'N') THEN
6951             IF (l_debug=1) THEN
6952                print_debug('Cancellation cannnot be performed on a Pending Drop operation',l_module_name,9);
6953             END IF;
6954 
6955             RAISE_APPLICATION_ERROR(INVALID_STATUS_FOR_OPERATION,'Cancellation cannot be performed on a Drop Operation');
6956 
6957          END IF;
6958 
6959          l_progress:=70;
6960 
6961 
6962          /*Mark the current Operation Plan (WOPI record) as 'Cancelled';
6963            call WMS_OP_RUNTIME_PVT_APIS.Update_operation_plan_instance to update
6964            the Operation Plan instance*/
6965          l_wopi_update_rec.status              := G_OP_INS_STAT_CANCELLED;
6966          l_wopi_update_rec.op_plan_instance_id := l_operation_instance_rec.op_plan_instance_id;
6967 
6968          l_progress := 100;
6969 
6970          IF (l_debug=1) THEN
6971             print_debug('Calling Update_plan_instance with the following values to be updated',l_module_name,9);
6972             print_debug('Plan status      => '||l_wopi_update_rec.status,l_module_name,9);
6973             print_debug('Plan Instance id => '||l_wopi_update_rec.op_plan_instance_id,l_module_name,9);
6974          END IF;
6975 
6976          WMS_OP_RUNTIME_PVT_APIS.Update_Plan_Instance
6977             (p_update_rec    => l_wopi_update_rec,
6978              x_return_status => x_return_status,
6979              x_msg_count     => x_msg_count,
6980              x_msg_data      => x_msg_data);
6981 
6982          l_progress:=110;
6983 
6984          IF (l_debug=1) THEN
6985            print_debug('Return status from table handler'||x_return_status,l_module_name,9);
6986          END IF;
6987 
6988          IF (x_return_status=g_ret_sts_error) THEN
6989            RAISE FND_API.G_EXC_ERROR;
6990 
6991          ELSIF (x_return_status<>g_ret_sts_success) THEN
6992            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6993 
6994          END IF;
6995 
6996          l_progress:=120;
6997 
6998        /*Mark the current pending Operation (WOOI record) as 'Cancelled';
6999         * call WMS_OP_RUNTIME_APIS.Update_operation_instance to update the Operation instance
7000         */
7001         l_wooi_update_rec.operation_status      := G_OP_INS_STAT_CANCELLED;
7002         l_wooi_update_rec.operation_instance_id := l_operation_instance_rec.operation_instance_id;
7003 
7004         IF (l_debug=1) THEN
7005           print_debug('Updating Operation Instance with the following values',l_module_name,9);
7006           print_debug('Operation status      ==> '||l_wooi_update_rec.operation_status,l_module_name,9);
7007           print_debug('Operation Instance Id ==> '||l_wooi_update_rec.operation_instance_id,l_module_name,9);
7008         END IF;
7009 
7010         l_progress:=130;
7011 
7012         WMS_OP_RUNTIME_PVT_APIS.Update_Operation_Instance
7013             (p_update_rec    => l_wooi_update_rec,
7014              x_return_status => x_return_status,
7015              x_msg_count     => x_msg_count,
7016              x_msg_data      => x_msg_data);
7017 
7018         IF (l_debug=1) THEN
7019             print_debug('Return status from table handler'||x_return_status,l_module_name,9);
7020         END IF;
7021 
7022         IF (x_return_status=g_ret_sts_error) THEN
7023 
7024           RAISE FND_API.G_EXC_ERROR;
7025 
7026         ELSIF (x_return_status<>g_ret_sts_success) THEN
7027 
7028           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7029 
7030         END IF;
7031 
7032 
7033        l_progress:=140;
7034        /*Call WMS_OP_RUNTIME_PVT_APIS.Archive_op_plan_instance to archive the
7035         Cancelled operation plan instance.
7036         */
7037        IF (l_debug=1) THEN
7038          print_debug('Archiving Plan Instance with Plan Instance Id'||l_operation_instance_rec.op_plan_instance_id,l_module_name,9);
7039        END IF;
7040 
7041        WMS_OP_RUNTIME_PVT_APIS.Archive_Plan_Instance
7042          (p_op_plan_instance_id => l_operation_instance_rec.op_plan_instance_id,
7043           x_return_status       => x_return_status,
7044           x_msg_count           => x_msg_count,
7045           x_msg_data            => x_msg_data);
7046 
7047        IF (l_debug=1) THEN
7048             print_debug('Return status from Archive Plan Instance'||x_return_status,l_module_name,9);
7049        END IF;
7050 
7051        l_progress:=150;
7052 
7053        IF (x_return_status=g_ret_sts_error) THEN
7054 
7055           RAISE FND_API.G_EXC_ERROR;
7056 
7057        ELSIF (x_return_status<>g_ret_sts_success) THEN
7058 
7059           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7060 
7061        END IF;
7062 
7063        /*For MMTT.parent_line_id call WMS_OP_RUNTIME_PVT_API.Archive_Dispatched_tasks to Archive WDT*/
7064        l_progress:=160;
7065 
7066        IF (l_debug=1) THEN
7067           print_debug('Calling Archive_Dispatched_tasks with the following parameters',l_module_name,4);
7068           print_debug('p_source_task_id      ==> '||l_parent_source_task_id,l_module_name,4);
7069           print_debug('p_activity_type_id    ==> '||p_activity_type_id,l_module_name,4);
7070           print_debug('p_op_plan_instance_id ==> '||l_operation_instance_rec.op_plan_instance_id,l_module_name,4);
7071           print_debug('p_op_plan_status      ==> '||G_OP_INS_STAT_CANCELLED,l_module_name,4);
7072        END IF;
7073 
7074        WMS_OP_RUNTIME_PVT_APIS.archive_dispatched_tasks(
7075         x_return_status        => x_return_status
7076       , x_msg_count            => x_msg_count
7077       , x_msg_data             => x_msg_data
7078       , p_task_id              => NULL
7079       , p_source_task_id       => l_parent_source_task_id
7080       , p_activity_type_id     => p_activity_type_id
7081       , p_op_plan_instance_id  => l_operation_instance_rec.op_plan_instance_id
7082       , p_op_plan_status       => G_OP_INS_STAT_CANCELLED);
7083 
7084       IF (l_debug=1) THEN
7085          print_debug('Return Status from Archive_dispatched_tasks is '||x_return_status,l_module_name,9);
7086       END IF;
7087 
7088       l_progress :=170;
7089 
7090       IF (x_return_status=g_ret_sts_error) THEN
7091 
7092           RAISE FND_API.G_EXC_ERROR;
7093 
7094       ELSIF (x_return_status<>g_ret_sts_success) THEN
7095 
7096           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7097 
7098       END IF;
7099 
7100       IF (p_activity_type_id = G_OP_ACTIVITY_INBOUND) THEN
7101 
7102 	 l_progress:=180;
7103 
7104 	 IF (l_debug=1) THEN
7105 	    print_debug('Inbound activity. ',l_module_name,4);
7106 	    print_debug('Before calling wms_op_inbound_pvt.cancel with following parameters: ' ,l_module_name,4);
7107 	    print_debug('p_source_task_id => '|| p_source_task_id,l_module_name,4);
7108 
7109 	 END IF;
7110 
7111 	 wms_op_inbound_pvt.cancel
7112 	   (
7113 	    x_return_status    => x_return_status,
7114 	    x_msg_data         => x_msg_data,
7115 	    x_msg_count        => x_msg_count,
7116 	    p_source_task_id   => p_source_task_id,
7117 	    p_retain_mmtt      => p_retain_mmtt,
7118 	    p_mmtt_error_code  => p_mmtt_error_code,
7119 	    p_mmtt_error_explanation => p_mmtt_error_explanation
7120 	    );
7121 
7122 	IF (l_debug=1) THEN
7123 	   print_debug('After calling wms_op_inbound_pvt.cancel.' ,l_module_name,4);
7124 	   print_debug('x_return_status => '|| x_return_status,l_module_name,4);
7125 	   print_debug('x_msg_data => '|| x_msg_data,l_module_name,4);
7126 	   print_debug('x_msg_count => '|| x_msg_count,l_module_name,4);
7127 	END IF;
7128 
7129 	IF x_return_status <>FND_API.g_ret_sts_success THEN
7130 	   IF (l_debug=1) THEN
7131 	      print_debug('wms_op_inbound_pvt.cancel finished with error. x_return_status = ' || x_return_status,l_module_name,4);
7132 	   END IF;
7133 
7134 	   RAISE FND_API.G_EXC_ERROR;
7135 	END IF;
7136 
7137          l_progress := 200;
7138       END IF; -- IF (p_activity_type_id = G_OP_ACTIVITY_INBOUND)
7139 
7140    EXCEPTION
7141 
7142         WHEN FND_API.G_EXC_ERROR THEN
7143 
7144           IF (l_debug=1) THEN
7145              print_debug('Expected Error Obtained at'||l_progress,l_module_name,1);
7146           END IF;
7147 
7148           IF c_inbound_doc%ISOPEN THEN
7149              CLOSE c_inbound_doc;
7150           END IF;
7151 
7152           IF c_operation_instance%ISOPEN THEN
7153              CLOSE c_operation_instance;
7154           END IF;
7155 
7156 
7157           ROLLBACK TO cancel_op_plan_sp;
7158 
7159           x_return_status:=g_ret_sts_error;
7160 
7161        WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
7162 
7163           IF (l_debug=1) THEN
7164              print_debug('UnExpected Error Obtained at'||l_progress||SQLERRM,l_module_name,1);
7165           END IF;
7166 
7167           IF (l_debug=1) THEN
7168              print_debug('Expected Error Obtained at'||l_progress,l_module_name,1);
7169           END IF;
7170 
7171           IF c_inbound_doc%ISOPEN THEN
7172              CLOSE c_inbound_doc;
7173           END IF;
7174 
7175           IF c_operation_instance%ISOPEN THEN
7176              CLOSE c_operation_instance;
7177           END IF;
7178 
7179 
7180           ROLLBACK TO cancel_op_plan_sp;
7181           x_return_status:=g_ret_sts_unexp_error;
7182 
7183        WHEN OTHERS THEN
7184 
7185           IF (l_debug=1) THEN
7186             print_debug(l_progress||' '||SQLERRM, l_module_name,1);
7187           END IF;
7188 
7189           IF fnd_msg_pub.check_msg_level(g_msg_lvl_unexp_error) THEN
7190             fnd_msg_pub.add_exc_msg(g_pkg_name, l_module_name, SQLERRM);
7191           END IF; /* fnd_msg.... */
7192 
7193           fnd_msg_pub.count_and_get(
7194                                  p_count => x_msg_count,
7195                                  p_data  => x_msg_data
7196                                 );
7197 
7198           IF (SQLCODE<-20000) THEN
7199             IF (l_debug=1) THEN
7200               print_debug('This is a user defined exception',l_module_name,1);
7201             END IF;
7202 
7203             x_error_code:=-(SQLCODE+20000);
7204 
7205             x_return_status:=g_ret_sts_error;
7206 
7207 
7208           ELSE
7209 
7210             x_return_status := g_ret_sts_unexp_error;
7211 
7212           END IF;
7213 
7214           IF c_inbound_doc%ISOPEN THEN
7215              CLOSE c_inbound_doc;
7216           END IF;
7217 
7218           IF c_operation_instance%ISOPEN THEN
7219              CLOSE c_operation_instance;
7220           END IF;
7221 
7222 
7223          ROLLBACK TO cancel_op_plan_sp;
7224 
7225    END Cancel_Operation_Plan;
7226 
7227 
7228 /**
7229   *   Abort_operation_Plan
7230   *   <p>This procedure aborts the operation plan(s). It is called from control board</p>
7231   *
7232   *  @param x_return_status    Return Status
7233   *  @param x_msg_data         Returns the Message Data
7234   *  @param x_msg_count        Returns the Error Message
7235   *  @param x_error_code       Returns appropriate error code in case of an error .
7236   *  @param p_source_task_id   Identifier of the source document record
7237   *  @param p_activity_type_id Lookup code of the Activity type Id
7238   **/
7239    PROCEDURE  Abort_Operation_Plan(
7240       x_return_status     OUT  NOCOPY  VARCHAR2,
7241       x_msg_data          OUT  NOCOPY  FND_NEW_MESSAGES.MESSAGE_TEXT%TYPE,
7242       x_msg_count         OUT  NOCOPY  NUMBER,
7243       x_error_code        OUT  NOCOPY  NUMBER,
7244       p_source_task_id    IN           NUMBER,
7245       p_activity_type_id  IN           NUMBER,
7246       p_for_manual_drop   IN  BOOLEAN DEFAULT FALSE
7247    ) IS
7248 
7249        CURSOR c_inbound_doc IS
7250         SELECT operation_plan_id,
7251 	  move_order_line_id,
7252 	  parent_line_id,
7253 	  transfer_to_location,
7254 	  locator_id,
7255 	  organization_id,
7256 	  inventory_item_id,
7257 	  primary_quantity
7258         FROM MTL_MATERIAL_TRANSACTIONS_TEMP
7259           WHERE transaction_temp_id = p_source_task_id;
7260 
7261        CURSOR c_operation_instance IS
7262           SELECT operation_instance_id,
7263                  operation_status,
7264                  op_plan_instance_id
7265           FROM WMS_OP_OPERATION_INSTANCES
7266           WHERE source_task_id = p_source_task_id
7267           AND operation_status <> G_OP_INS_STAT_COMPLETED
7268           AND activity_type_id = p_activity_type_id
7269 	  ORDER BY operation_sequence DESC;
7270 
7271        CURSOR c_plan_instance(v_op_plan_instance_id NUMBER) IS
7272           SELECT status,
7273 	    orig_dest_sub_code,
7274 	    orig_dest_loc_id
7275           FROM WMS_OP_PLAN_INSTANCES
7276           WHERE op_plan_instance_id=v_op_plan_instance_id;
7277 
7278 
7279        l_plan_orig_dest_sub    VARCHAR2(10);
7280        l_plan_orig_dest_loc_id NUMBER;
7281 
7282 
7283        l_inbound_doc           c_inbound_doc%ROWTYPE;
7284        l_operation_plan_id     NUMBER;
7285        l_operation_instance     c_operation_instance%ROWTYPE;
7286        l_plan_inst_status      NUMBER := -1;
7287        l_wooi_update_rec       WMS_OP_OPERATION_INSTANCES%ROWTYPE:=NULL;
7288        l_wopi_update_rec       WMS_OP_PLAN_INSTANCES%ROWTYPE:=NULL;
7289        l_parent_source_task_id NUMBER;
7290        l_document_rec          mtl_material_transactions_temp%ROWTYPE;
7291 
7292 
7293 
7294        l_debug       NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
7295        l_module_name VARCHAR2(30) := 'Abort_operation_Plan';
7296        l_return_status  VARCHAR2(1);
7297        l_msg_count NUMBER;
7298        l_msg_data VARCHAR2(400);
7299 
7300        l_progress    NUMBER       ;
7301 
7302    BEGIN
7303 
7304          IF (l_debug = 1) THEN
7305              print_DEBUG(' p_source_task_id ==> '||p_source_task_id ,l_module_name,3);
7306              print_DEBUG(' p_activity_type_id ==> '||p_activity_type_id ,l_module_name,3);
7307          END IF;
7308          SAVEPOINT abort_plan_sp;
7309 
7310          x_return_status  := g_ret_sts_success;
7311 
7312          l_progress := 10;
7313 
7314          /*If the passed P_SOURCE_TASK_ID is null then raise Invalid Argument
7315            Error and return with appropriate error code.*/
7316 
7317          IF (p_source_task_id IS NULL OR p_activity_type_id IS NULL) THEN
7318 
7319             IF (l_debug=1) THEN
7320                print_debug('Invalid inputs passed'||p_source_task_id,l_module_name,9);
7321             END IF;
7322 
7323             RAISE_APPLICATION_ERROR(INVALID_INPUT,'Invalid inputs passed'||p_source_task_id||' '||p_activity_type_id);
7324 
7325          END IF;
7326 
7327          l_progress:=20;
7328 
7329          /* Query the document tables based on p_source_task_id and
7330           * populate the record variable.
7331           * If p_activity_id=INBOUND then
7332           * Query MMTT (MTLT) where transaction_temp_id is equal p_source_task_id and
7333           * populate the record variable.*/
7334 
7335          IF p_activity_type_id=G_OP_ACTIVITY_INBOUND THEN
7336 
7337             OPEN c_inbound_doc;
7338 
7339             FETCH c_inbound_doc INTO l_inbound_doc;
7340 
7341             IF c_inbound_doc%NOTFOUND THEN
7342 
7343                /* If there is no document record is found corresponding to
7344                 * P_SOURCE_TASK_ID then raise Developer Error stating that
7345                 * document must exist and return with appropriate error code.*/
7346 
7347                IF (l_debug=1) THEN
7348                   print_debug('Document record not found for source_task_id'||p_source_task_id,l_module_name,1);
7349                END IF;
7350 
7351                CLOSE c_inbound_doc;
7352 
7353                RAISE_APPLICATION_ERROR(INVALID_DOC_ID,'Invalid Document Record');
7354              END IF;
7355 
7356              CLOSE c_inbound_doc;
7357 
7358              l_progress:=30;
7359 
7360            /*Handle case where MMTT.Operation_Plan_ID is NULL.
7361              If MMTT.Operation_Plan_ID is NULL
7362                This could happen when 'user drop ' a pre-11.5.10 MMTT record.
7363            */
7364 	          IF (l_inbound_doc.operation_plan_id IS NULL) THEN
7365               /* Non ATF Case*/
7366                IF (l_debug=1) THEN
7367                  print_debug('Operation Plan Id is null:non ATF case',l_module_name,9);
7368                END IF;
7369 
7370                RETURN;
7371 
7372              END IF; /*Non ATF case*/
7373 
7374              l_parent_source_task_id := l_inbound_doc.parent_line_id;
7375 
7376          END IF;/*Inbound*/
7377 
7378 
7379          l_progress :=40;
7380 
7381          /*Populate the current Operation Instance (WOOI) record based on
7382           * the p_source_task_id  and the pending operation
7383          */
7384 
7385          OPEN c_operation_instance;
7386 
7387          FETCH c_operation_instance INTO l_operation_instance;
7388 
7389          IF (c_operation_instance%NOTFOUND) THEN
7390 
7391             IF (l_debug=1) THEN
7392                print_debug('Operation instance does not exist for record',l_module_name,1);
7393             END IF;
7394 
7395             CLOSE c_operation_instance;
7396 
7397             RAISE_APPLICATION_ERROR(OPERATION_INSTANCE_NOT_EXISTS,'Operation Instance does not exist for document record with Id '||p_source_task_id);
7398          END IF;
7399 
7400          CLOSE c_operation_instance;
7401 
7402          l_progress:=50;
7403 
7404          /*Populate Operation Plan Instance (WOPI) into the Operation Plan Instance record
7405           * based on the Operation Plan Instance field*/
7406 
7407          OPEN c_plan_instance(l_operation_instance.op_plan_instance_id);
7408 
7409 	  FETCH c_plan_instance INTO
7410 	    l_plan_inst_status,
7411 	    l_plan_orig_dest_sub,
7412 	    l_plan_orig_dest_loc_id;
7413 
7414          CLOSE c_plan_instance;
7415 
7416          l_progress:=60;
7417 
7418          IF l_plan_inst_status IS NULL OR l_plan_inst_status=-1 THEN
7419 
7420             IF (l_debug=1) THEN
7421                print_debug('Invalid Operation Plan Instance',l_module_name,1);
7422             END IF;
7423 
7424             RAISE_APPLICATION_ERROR(INVALID_PLAN_INSTANCE,'Invalid Operation Plan Instance');
7425          END IF;
7426 
7427          l_progress:=70;
7428 
7429          /*Mark the current Operation Plan (WOPI record) as 'Aborted;
7430            call WMS_OP_RUNTIME_PVT_APIS.Update_operation_plan_instance to update
7431            the Operation Plan instance*/
7432          l_wopi_update_rec.status              := G_OP_INS_STAT_ABORTED;
7433          l_wopi_update_rec.op_plan_instance_id := l_operation_instance.op_plan_instance_id;
7434 
7435          l_progress := 80;
7436 
7437          IF (l_debug=1) THEN
7438             print_debug('Calling Update_plan_instance with the following values to be updated',l_module_name,9);
7439             print_debug('Plan status      => '||l_wopi_update_rec.status,l_module_name,9);
7440             print_debug('Plan Instance id => '||l_wopi_update_rec.op_plan_instance_id,l_module_name,9);
7441          END IF;
7442 
7443          WMS_OP_RUNTIME_PVT_APIS.Update_Plan_Instance
7444             (p_update_rec    => l_wopi_update_rec,
7445              x_return_status => x_return_status,
7446              x_msg_count     => x_msg_count,
7447              x_msg_data      => x_msg_data);
7448 
7449          l_progress:=90;
7450 
7451          IF (l_debug=1) THEN
7452            print_debug('Return status from table handler'||x_return_status,l_module_name,9);
7453          END IF;
7454 
7455          IF (x_return_status=g_ret_sts_error) THEN
7456            RAISE FND_API.G_EXC_ERROR;
7457 
7458          ELSIF (x_return_status<>g_ret_sts_success) THEN
7459            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7460 
7461          END IF;
7462 
7463          l_progress:=100;
7464 
7465        /*Mark the current pending Operation (WOOI record) as 'Aborted;
7466         * call WMS_OP_RUNTIME_APIS.Update_operation_instance to update the Operation instance
7467         */
7468         l_wooi_update_rec.operation_status      := G_OP_INS_STAT_ABORTED;
7469         l_wooi_update_rec.operation_instance_id := l_operation_instance.operation_instance_id;
7470 
7471         IF (l_debug=1) THEN
7472           print_debug('Updating Operation Instance with the following values',l_module_name,9);
7473           print_debug('Operation status      ==> '||l_wooi_update_rec.operation_status,l_module_name,9);
7474           print_debug('Operation Instance Id ==> '||l_wooi_update_rec.operation_instance_id,l_module_name,9);
7475         END IF;
7476 
7477         l_progress:=110;
7478 
7479         WMS_OP_RUNTIME_PVT_APIS.Update_Operation_Instance
7480             (p_update_rec    => l_wooi_update_rec,
7481              x_return_status => x_return_status,
7482              x_msg_count     => x_msg_count,
7483              x_msg_data      => x_msg_data);
7484 
7485         IF (l_debug=1) THEN
7486             print_debug('Return status from table handler'||x_return_status,l_module_name,9);
7487         END IF;
7488 
7489         IF (x_return_status=g_ret_sts_error) THEN
7490 
7491           RAISE FND_API.G_EXC_ERROR;
7492 
7493         ELSIF (x_return_status<>g_ret_sts_success) THEN
7494 
7495           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7496 
7497         END IF;
7498 
7499 
7500        l_progress:=120;
7501 
7502        /*Call WMS_OP_RUNTIME_PVT_APIS.Archive_op_plan_instance to archive the
7503         aborted operation plan instance.
7504         */
7505        IF (l_debug=1) THEN
7506          print_debug('Archiving Plan Instance with Plan Instance Id'||l_operation_instance.op_plan_instance_id,l_module_name,9);
7507        END IF;
7508 
7509        WMS_OP_RUNTIME_PVT_APIS.Archive_Plan_Instance
7510          (p_op_plan_instance_id => l_operation_instance.op_plan_instance_id,
7511           x_return_status       => x_return_status,
7512           x_msg_count           => x_msg_count,
7513           x_msg_data            => x_msg_data);
7514 
7515        IF (l_debug=1) THEN
7516             print_debug('Return status from Archive Plan Instance'||x_return_status,l_module_name,9);
7517        END IF;
7518 
7519        l_progress:=130;
7520 
7521        IF (x_return_status=g_ret_sts_error) THEN
7522 
7523           RAISE FND_API.G_EXC_ERROR;
7524 
7525        ELSIF (x_return_status<>g_ret_sts_success) THEN
7526 
7527           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7528 
7529        END IF;
7530 
7531        /*For MMTT.parent_line_id call WMS_OP_RUNTIME_PVT_API.Archive_Dispatched_tasks to Archive WDT*/
7532        l_progress:=140;
7533 
7534        IF (l_debug=1) THEN
7535           print_debug('Calling Archive_Dispatched_tasks with the following parameters',l_module_name,4);
7536           print_debug('p_source_task_id      ==> '||l_parent_source_task_id,l_module_name,4);
7537           print_debug('p_activity_type_id    ==> '||p_activity_type_id,l_module_name,4);
7538           print_debug('p_op_plan_instance_id ==> '||l_operation_instance.op_plan_instance_id,l_module_name,4);
7539           print_debug('p_op_plan_status      ==> '||G_OP_INS_STAT_ABORTED,l_module_name,4);
7540        END IF;
7541 
7542        WMS_OP_RUNTIME_PVT_APIS.archive_dispatched_tasks(
7543         x_return_status        => x_return_status
7544       , x_msg_count            => x_msg_count
7545       , x_msg_data             => x_msg_data
7546       , p_task_id              => NULL
7547       , p_source_task_id       => l_parent_source_task_id
7548       , p_activity_type_id     => p_activity_type_id
7549       , p_op_plan_instance_id  => l_operation_instance.op_plan_instance_id
7550       , p_op_plan_status       => G_OP_INS_STAT_ABORTED);
7551 
7552       IF (l_debug=1) THEN
7553          print_debug('Return Status from Archive_dispatched_tasks is '||x_return_status,l_module_name,9);
7554       END IF;
7555 
7556       l_progress :=150;
7557 
7558       IF (x_return_status=g_ret_sts_error) THEN
7559 
7560           RAISE FND_API.G_EXC_ERROR;
7561 
7562       ELSIF (x_return_status<>g_ret_sts_success) THEN
7563 
7564           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7565 
7566       END IF;
7567 
7568       IF (p_activity_type_id = G_OP_ACTIVITY_INBOUND) THEN
7569 
7570 
7571 	 l_progress:=160;
7572          /* null out the Operation Plan Id ,parent Line Id and delete the Parent transaction temp Id*/
7573          /*Call table hanlder to delete MMTT/MSNT/MTLT for the Parent MMTT*/
7574 
7575 /*	 INV_TRX_UTIL_PUB.Delete_transaction
7576            (x_return_status       => x_return_status,
7577             x_msg_data            => x_msg_data,
7578             x_msg_count           => x_msg_count,
7579             p_transaction_temp_id => l_parent_source_task_id,
7580             p_update_parent       => FALSE);
7581 
7582         IF (l_debug=1) THEN
7583             print_debug('Return status from Delete Transaction'||x_return_status,l_module_name,9);
7584         END IF;
7585 
7586         IF (x_return_status=g_ret_sts_error) THEN
7587 
7588           RAISE FND_API.G_EXC_ERROR;
7589 
7590         ELSIF (x_return_status<>g_ret_sts_success) THEN
7591 
7592           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7593 
7594         END IF;
7595 
7596         l_progress:=170;
7597 
7598         IF (l_debug=1) THEN
7599            print_debug('Updating parent Line id and Operation plan id for Src Document record',l_module_name,9);
7600 	   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);
7601 	   print_debug('l_inbound_doc.locator_id = '|| l_inbound_doc.locator_id,l_module_name,9);
7602     	   print_debug('l_inbound_doc.locator_id = '|| l_inbound_doc.locator_id,l_module_name,9);
7603     	   print_debug('l_inbound_doc.transfer_to_location = '|| l_inbound_doc.transfer_to_location,l_module_name,9);
7604 	END IF;
7605 
7606 	IF l_inbound_doc.locator_id IS NULL THEN
7607 	   UPDATE MTL_MATERIAL_TRANSACTIONS_TEMP
7608 	     SET operation_plan_id  = NULL ,
7609 	     parent_line_id     = NULL,
7610 	     subinventory_code  = l_plan_orig_dest_sub,
7611 	     locator_id         = l_plan_orig_dest_loc_id
7612 	     WHERE transaction_temp_id = p_source_task_id;
7613 	 ELSIF  l_inbound_doc.transfer_to_location IS NULL THEN
7614 	   UPDATE MTL_MATERIAL_TRANSACTIONS_TEMP
7615 	     SET operation_plan_id  = NULL ,
7616 	     parent_line_id     = NULL,
7617 	     transfer_subinventory  = l_plan_orig_dest_sub,
7618 	     transfer_to_location         = l_plan_orig_dest_loc_id
7619 	     WHERE transaction_temp_id = p_source_task_id;
7620 	END IF;
7621 
7622 
7623        l_progress := 180;
7624 	     */
7625 
7626 	     l_document_rec.transaction_temp_id := p_source_task_id;
7627 	   l_document_rec.parent_line_id := l_inbound_doc.parent_line_id;
7628 	   l_document_rec.locator_id := l_inbound_doc.locator_id;
7629 	   l_document_rec.transfer_to_location := l_inbound_doc.transfer_to_location;
7630 	   l_document_rec.organization_id := l_inbound_doc.organization_id;
7631 	   l_document_rec.inventory_item_id := l_inbound_doc.inventory_item_id;
7632 	   l_document_rec.primary_quantity := l_inbound_doc.primary_quantity;
7633 
7634 	   IF l_debug=1 THEN
7635 	      print_debug('Before calling wms_op_inbound_pvt.ABORT with following parameters:',l_module_name,4);
7636 	      print_debug('l_document_rec.transaction_temp_id => '|| l_document_rec.transaction_temp_id,l_module_name,4);
7637 	      print_debug('l_document_rec.parent_line_id => '||l_document_rec.parent_line_id,l_module_name,4);
7638 	      print_debug('l_document_rec.locator_id => '||l_document_rec.locator_id,l_module_name,4);
7639 	      print_debug('l_document_rec.transfer_to_location => '||l_document_rec.transfer_to_location,l_module_name,4);
7640 	      print_debug('p_plan_orig_sub_code => '||l_plan_orig_dest_sub,l_module_name,4);
7641 	      print_debug('p_plan_orig_loc_id => '||l_plan_orig_dest_loc_id,l_module_name,4);
7642 	      IF(p_for_manual_drop = TRUE) THEN
7643 		 print_debug('p_for_manual_drop => TRUE',l_module_name, 4);
7644 	       ELSE
7645 		 print_debug('p_for_manual_drop => FALSE',l_module_name,4);
7646 	      END IF;
7647 
7648 	   END IF;
7649 
7650 
7651 	   wms_op_inbound_pvt.ABORT
7652 	    (x_return_status         => l_return_status,
7653 	     x_msg_data              => l_msg_data,
7654 	     x_msg_count             => l_msg_count,
7655 	     p_document_rec          => l_document_rec,
7656 	     p_plan_orig_sub_code    => l_plan_orig_dest_sub,
7657 	     p_plan_orig_loc_id      => l_plan_orig_dest_loc_id,
7658 	     p_for_manual_drop       => p_for_manual_drop
7659 	     );
7660 
7661 	   IF l_debug=1 THEN
7662 	      print_debug('After calling wms_op_inbound_pvt.ABORT:',l_module_name,4);
7663 	      print_debug('x_return_status => '||l_return_status,l_module_name,4);
7664 	      print_debug('x_msg_data => '||l_msg_data,l_module_name,4);
7665 	      print_debug('x_msg_count => '||l_msg_count,l_module_name,4);
7666 	   END IF;
7667 
7668 	   IF l_return_status <> FND_API.g_ret_sts_success THEN
7669 	     IF (l_debug=1) THEN
7670 		print_debug(' wms_op_inbound_pvt.ABORT finished with error. l_return_status = ' || l_return_status,l_module_name,4);
7671 	     END IF;
7672 
7673 	     RAISE FND_API.G_EXC_ERROR;
7674 	  END IF;
7675 
7676        IF l_debug=1 THEN
7677           print_debug('Need to update the WDT /WDTH records nulling out operation Plan id',l_module_name,9);
7678           print_debug(' and also op_plan_instance_id ',l_module_name,9);
7679        END IF;
7680 
7681        -- Do not nul out operation_plan_id and op_plan_instance_id
7682        -- since DBI uses these columns for exception related KPI
7683        -- It is ok for WMS because without parent_transaction_id
7684        -- the aborted WDTH is still viewed as an independent task
7685        -- from control board.
7686        UPDATE wms_dispatched_tasks_history
7687 	 SET parent_transaction_id = NULL
7688 	 WHERE transaction_id = p_source_task_id;
7689 
7690 /*       UPDATE wms_dispatched_tasks
7691           SET operation_plan_id = NULL,
7692               op_plan_instance_id = NULL
7693        WHERE transaction_temp_id = p_source_task_id;
7694 
7695        l_progress := 190;
7696 
7697        IF SQL%ROWCOUNT=0 THEN
7698 
7699           IF l_debug =1 THEN
7700              print_debug('Could be an archived record,hence updating WDT',l_module_name,9);
7701           END IF;
7702 
7703           l_progress :=200;
7704 
7705           UPDATE wms_dispatched_tasks_history
7706              SET operation_plan_id     = NULL,
7707                  op_plan_instance_id   = NULL,
7708                  parent_transaction_id = NULL
7709           WHERE transaction_id = p_source_task_id;
7710 
7711 	END IF;
7712 */
7713 
7714       END IF;
7715 
7716       /*Need for calling a document handler or not..mostly not*/
7717 
7718    EXCEPTION
7719 
7720         WHEN FND_API.G_EXC_ERROR THEN
7721 
7722           IF (l_debug=1) THEN
7723              print_debug('Expected Error Obtained at'||l_progress,l_module_name,1);
7724           END IF;
7725 
7726           IF c_inbound_doc%ISOPEN THEN
7727              CLOSE c_inbound_doc;
7728           END IF;
7729 
7730           IF c_operation_instance%ISOPEN THEN
7731              CLOSE c_operation_instance;
7732           END IF;
7733 
7734           IF c_plan_instance%ISOPEN THEN
7735              CLOSE c_plan_instance;
7736           END IF;
7737 
7738           ROLLBACK TO abort_plan_sp;
7739 
7740           x_return_status:=g_ret_sts_error;
7741 
7742        WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
7743 
7744           IF (l_debug=1) THEN
7745              print_debug('UnExpected Error Obtained at'||l_progress||SQLERRM,l_module_name,1);
7746           END IF;
7747 
7748           IF (l_debug=1) THEN
7749              print_debug('Expected Error Obtained at'||l_progress,l_module_name,1);
7750           END IF;
7751 
7752           IF c_inbound_doc%ISOPEN THEN
7753              CLOSE c_inbound_doc;
7754           END IF;
7755 
7756           IF c_operation_instance%ISOPEN THEN
7757              CLOSE c_operation_instance;
7758           END IF;
7759 
7760           IF c_plan_instance%ISOPEN THEN
7761              CLOSE c_plan_instance;
7762           END IF;
7763 
7764           ROLLBACK TO abort_plan_sp;
7765           x_return_status:=g_ret_sts_unexp_error;
7766 
7767        WHEN OTHERS THEN
7768 
7769           IF (l_debug=1) THEN
7770             print_debug(l_progress||' '||SQLERRM, l_module_name,1);
7771           END IF;
7772 
7773           IF fnd_msg_pub.check_msg_level(g_msg_lvl_unexp_error) THEN
7774             fnd_msg_pub.add_exc_msg(g_pkg_name, l_module_name, SQLERRM);
7775           END IF; /* fnd_msg.... */
7776 
7777           fnd_msg_pub.count_and_get(
7778                                  p_count => x_msg_count,
7779                                  p_data  => x_msg_data
7780                                 );
7781 
7782           IF (SQLCODE<-20000) THEN
7783             IF (l_debug=1) THEN
7784               print_debug('This is a user defined exception',l_module_name,1);
7785             END IF;
7786 
7787             x_error_code:=-(SQLCODE+20000);
7788 
7789             x_return_status:=g_ret_sts_error;
7790 
7791 
7792           ELSE
7793 
7794             x_return_status := g_ret_sts_unexp_error;
7795 
7796           END IF;
7797 
7798           IF c_inbound_doc%ISOPEN THEN
7799              CLOSE c_inbound_doc;
7800           END IF;
7801 
7802           IF c_operation_instance%ISOPEN THEN
7803              CLOSE c_operation_instance;
7804           END IF;
7805 
7806           IF c_plan_instance%ISOPEN THEN
7807              CLOSE c_plan_instance;
7808           END IF;
7809 
7810 
7811          ROLLBACK TO abort_plan_sp;
7812 
7813 
7814    END Abort_Operation_Plan;
7815 
7816 
7817  /**
7818   *   Rollback_operation_Plan
7819   *   <p>This procedure rollsback the operation plan(s).This API is called for a Single Step Drop
7820   *      Operation and it rollsback to Load Pending.</p>
7821   *
7822   *  @param x_return_status    Return Status
7823   *  @param x_msg_data         Returns the Message Data
7824   *  @param x_msg_count        Returns the Error Message
7825   *  @param x_error_code       Returns appropriate error code in case of an error .
7826   *  @param p_source_task_id   Identifier of the source document record
7827   *  @param p_activity_type_id Lookup code of the Activity type Id
7828   **/
7829    PROCEDURE  Rollback_Operation_Plan(
7830       x_return_status     OUT  NOCOPY  VARCHAR2,
7831       x_msg_data          OUT  NOCOPY  FND_NEW_MESSAGES.MESSAGE_TEXT%TYPE,
7832       x_msg_count         OUT  NOCOPY  NUMBER,
7833       x_error_code        OUT  NOCOPY  NUMBER,
7834       p_source_task_id    IN           NUMBER,
7835       p_activity_type_id  IN           NUMBER ) IS
7836 
7837 
7838       CURSOR c_wdt_details IS
7839          SELECT status,
7840                 task_id
7841          FROM wms_dispatched_tasks
7842          WHERE transaction_temp_id = p_source_task_id;
7843 
7844       CURSOR c_inbound_document_details IS
7845          SELECT operation_plan_id
7846          FROM mtl_material_transactions_temp
7847          WHERE transaction_temp_id = p_source_task_id;
7848 
7849 
7850       CURSOR c_operation_instances IS
7851          SELECT operation_instance_id,
7852                 op_plan_instance_id,
7853                 operation_type_id,
7854                 operation_sequence,
7855                 operation_plan_detail_id,
7856                 operation_status
7857          FROM wms_op_operation_instances
7858          WHERE source_task_id = p_source_task_id
7859          AND activity_type_id = p_activity_type_id
7860          ORDER BY 4;
7861 
7862       CURSOR c_min_operation_seq (v_op_plan_id NUMBER) IS
7863          SELECT MIN(operation_sequence)
7864           FROM wms_op_plan_details
7865          WHERE operation_plan_id = v_op_plan_id;
7866 
7867 
7868 
7869 
7870        l_wdt_details          c_wdt_details%ROWTYPE;
7871        l_inbound_document     c_inbound_document_details%ROWTYPE;
7872        l_wooi_data_rec        c_operation_instances%ROWTYPE;
7873        l_min_opertn_sequence  NUMBER := -1;
7874        l_op_plan_instance_id  NUMBER;
7875        l_wooi_rec             wms_op_operation_instances%ROWTYPE;
7876        l_plan_detail_id       NUMBER;
7877        l_first_opertn_seq     NUMBER;
7878        l_wopi_rec             wms_op_plan_instances%ROWTYPE;
7879        l_op_plan_id           NUMBER;
7880 
7881        l_debug       NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
7882        l_module_name VARCHAR2(30) := 'Rollback_operation_Plan';
7883        l_progress    NUMBER       ;
7884 
7885    BEGIN
7886 
7887          IF (l_debug = 1) THEN
7888              print_DEBUG(' p_source_task_id ==> '||p_source_task_id ,l_module_name,3);
7889              print_DEBUG(' p_activity_type_id ==> '||p_activity_type_id ,l_module_name,3);
7890          END IF;
7891          x_return_status  := g_ret_sts_success;
7892 
7893          SAVEPOINT rollback_op_plan_sp;
7894          l_progress :=10;
7895 
7896          /*If the p_source_task_id is null or p_activity_type_id is null then raise invalid arguement error and
7897           * return by populating appropriate error code.
7898           */
7899          IF p_source_task_id IS NULL OR p_activity_type_id IS NULL THEN
7900 
7901             IF (l_debug=1) THEN
7902                print_debug('Invalid Inputs passed to Rollback Operation Plan '||p_source_task_id||' '||p_activity_type_id,l_module_name,9);
7903             END IF;
7904 
7905             RAISE_APPLICATION_ERROR(INVALID_INPUT,'Invalid inputs passed'||p_source_task_id||' '||p_activity_type_id);
7906 
7907          END IF;
7908 
7909          l_progress:=20;
7910 
7911 
7912          /* Update or remove WDT. Based on the p_source_task_id query WMS_DISPATCHED_TASKS and populate WDT record variable as follows:
7913           * a.	If no record is found in WDT for source_task_id then raise unexpected error and return after populating appropriate error code.
7914           * b.	If WDT.status = 'Dispatched' or 'Loaded' THEN
7915           *      a.	Remove WDT
7916           * End IF; (If Dispatched)
7917           */
7918 
7919          IF (p_activity_type_id =G_OP_ACTIVITY_INBOUND) THEN
7920 
7921             OPEN c_wdt_details;
7922 
7923             FETCH c_wdt_details INTO l_wdt_details;
7924 
7925             IF (c_wdt_details%NOTFOUND) THEN
7926 
7927               IF (l_debug=1) THEN
7928                  print_debug('No WDT record found for source task id '||p_source_task_id,l_module_name,1);
7929               END IF;
7930 
7931               CLOSE c_wdt_details;
7932 
7933               RAISE_APPLICATION_ERROR(TASK_NOT_EXISTS,'Task Record does not exist for source task Id :'||p_source_task_id);
7934 
7935             END IF;
7936 
7937             CLOSE c_wdt_details;
7938 
7939             l_progress:=30;
7940 
7941             IF (l_debug=1) THEN
7942                print_debug('Status of the task record fetched is '||l_wdt_details.status,l_module_name,9);
7943             END IF;
7944 
7945             IF l_wdt_details.status NOT IN (g_task_status_dispatched,g_task_status_loaded) THEN
7946 
7947                IF l_debug=1 THEN
7948                   print_debug('Invalid task record',l_module_name,1);
7949                END IF;
7950 
7951                RAISE_APPLICATION_ERROR(INVALID_TASK,'Invalid Task Record for task Id '||l_wdt_details.task_id||' and status '||l_wdt_details.status);
7952 
7953             END IF;
7954 
7955             l_progress := 40;
7956 
7957 
7958 	    -- Moved delete WDT to after WMS_OP_INBOUND_PVT.Cleanup
7959 
7960 
7961             /* Query the document tables based on p_source_taks_id and p_activity_id and populate the document record.
7962              *   If p_activity_id=INBOUND then
7963              *      Query MMTT (MTLT) where transaction_temp_id is equal p_source_task_id and populate the record variable.
7964              *   End If
7965              */
7966 
7967              OPEN c_inbound_document_details;
7968 
7969              FETCH c_inbound_document_details INTO l_inbound_document;
7970 
7971              IF c_inbound_document_details%NOTFOUND THEN
7972 
7973                 CLOSE c_inbound_document_details;
7974 
7975                 IF l_debug=1 THEN
7976 
7977                    print_debug('Inbound document record does not exist for source task Id :'||p_source_task_id,l_module_name,9);
7978 
7979                 END IF;
7980 
7981                 RAISE_APPLICATION_ERROR(INVALID_DOC_ID,'Invalid Document Record with document Id: '||p_source_task_id);
7982 
7983              END IF;
7984 
7985              CLOSE c_inbound_document_details;
7986 
7987              /*	If Operation_plan_id on MMTT record variable is null then return as Success
7988               *   Non ATF case
7989               */
7990              l_progress:=60;
7991 
7992              IF l_inbound_document.operation_plan_id IS NULL THEN
7993 
7994                 IF (l_debug=1) THEN
7995                    print_debug('Operation PLan Id null on document record:Non ATF case',l_module_name,9);
7996                 END IF;
7997 		/* for bug#5997558 */
7998 		IF p_activity_type_id =G_OP_ACTIVITY_INBOUND THEN
7999 
8000                 WMS_OP_INBOUND_PVT.Cleanup
8001                   (  x_return_status   => x_return_status
8002  	               , x_msg_data         => x_msg_data
8003  	               , x_msg_count        => x_msg_count
8004  	               , p_source_task_id   => p_source_task_id
8005  	              );
8006 
8007                IF l_debug=1 THEN
8008                   print_debug(' Return Status from Cleanup '||x_return_status,l_module_name,9);
8009                END IF;
8010 
8011 
8012                IF (x_return_status=g_ret_sts_error) THEN
8013                     RAISE FND_API.G_EXC_ERROR;
8014 
8015                ELSIF (x_return_status<>g_ret_sts_success) THEN
8016                     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
8017 
8018                END IF;
8019 	       end if ;
8020 
8021 	       /* End for bug#5997558 */
8022 
8023 		-- Before we return, we should delete WDT here
8024 		-- since now we delete WDT after calling WMS_OP_INBOUND_PVT.Cleanup at the end.
8025 
8026 		DELETE FROM WMS_DISPATCHED_TASKS
8027 		  WHERE transaction_temp_id = p_source_task_id
8028 		  AND task_id = l_wdt_details.task_id;
8029 
8030 		IF c_wdt_details%ISOPEN THEN
8031 		   CLOSE c_wdt_details;
8032 		END IF;
8033 
8034 		IF c_inbound_document_details%ISOPEN THEN
8035 		   CLOSE c_inbound_document_details;
8036 		END IF;
8037 
8038 		IF c_operation_instances%ISOPEN THEN
8039 		   CLOSE c_operation_instances;
8040 		END IF;
8041 
8042 		IF c_min_operation_seq%ISOPEN THEN
8043 		   CLOSE c_min_operation_seq;
8044 		END IF;
8045 
8046                 RETURN;
8047 
8048              END IF; /* non ATF case*/
8049 
8050              l_op_plan_id := l_inbound_document.operation_plan_id;
8051 
8052          END IF;/*Inbound*/
8053 
8054              /* Populate the Operation Instance (WOOI) record based on the
8055               * current Active operation for p_source_task_id.
8056               * Populate the Operation Plan Instance (WOPI) Record based on the Operation Plan
8057               * Instance of the operation Instance.
8058               */
8059              l_progress := 70;
8060 
8061              OPEN c_operation_instances;
8062 
8063              LOOP
8064 
8065               FETCH c_operation_instances INTO l_wooi_data_rec;
8066 
8067               EXIT WHEN c_operation_instances%NOTFOUND;
8068 
8069               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
8070 
8071                /* Call WMS_OP_RUNTIME_APIS_PVT.UPDATE_OPERATION_INSTANCE
8072                 * to update the Operation Instance populated in WOOI record
8073                 * tp statsu Pending
8074                 */
8075 
8076                  l_wooi_rec.operation_instance_id := l_wooi_data_rec.operation_instance_id;
8077                  l_wooi_rec.operation_status      := G_OP_INS_STAT_PENDING;
8078 
8079 
8080                 IF (l_debug=1) THEN
8081                    print_debug('Load Operation is Completed,hence updating the Operation Instance Record tp Pending',l_module_name,9);
8082                    print_debug('CAlling Update_Operation_instance with the Operation Instance Id as '||l_wooi_rec.operation_instance_id,l_module_name,9);
8083                    print_debug('Operation status as :'||l_wooi_rec.operation_status,l_module_name,9);
8084                 END IF;
8085 
8086                 l_progress := 80;
8087 
8088                 WMS_OP_RUNTIME_PVT_APIS.UPDATE_OPERATION_INSTANCE
8089                    (p_update_rec    => l_wooi_rec,
8090                     x_return_status => x_return_status,
8091                     x_msg_count     => x_msg_count,
8092                     x_msg_data      => x_msg_data);
8093 
8094                 IF (l_debug = 1) THEN
8095                    print_debug('Return Status from Update Operation Instance is '||x_return_status,l_module_name,9);
8096                 END IF;
8097 
8098                 IF (x_return_status=g_ret_sts_error) THEN
8099                    RAISE FND_API.G_EXC_ERROR;
8100 
8101                 ELSIF (x_return_status<>g_ret_sts_success) THEN
8102                   RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
8103 
8104                 END IF;
8105 
8106                 l_op_plan_instance_id := l_wooi_data_rec.op_plan_instance_id;
8107 
8108 
8109                 /* Fetching the minimum sequence of the Operation Plan Detail to check later if it is the
8110                  * first operation
8111                  */
8112 
8113                 l_min_opertn_sequence := l_wooi_data_rec.operation_sequence;
8114 
8115                 l_plan_detail_id := l_wooi_data_rec.operation_plan_detail_id;
8116 
8117 
8118               END IF;/* Operation is Load */
8119 
8120               l_progress := 90;
8121 
8122               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
8123 
8124                  IF (l_debug=1) THEN
8125                     print_debug('Operation is Drop,hence deleting the Operation Instance',l_module_name,9);
8126                  END IF;
8127 
8128                  /*Checking if operation Plan Instances are the same on both the Operation instance records
8129                   * Ideally this condition should never occur,checking for data corruption
8130                   */
8131 
8132                  IF l_op_plan_instance_id IS NOT NULL AND l_op_plan_instance_id <> l_wooi_data_rec.op_plan_instance_id THEN
8133 
8134                     IF (l_debug=1) THEN
8135                        print_debug('Invalid condition where the Operation plan Instances are different for the same source_task id',l_module_name,1);
8136                     END IF;
8137 
8138                     RAISE_APPLICATION_ERROR(DATA_INCONSISTENT,'Operation plan Instance different for the same Source task id in different Operation Instances');
8139 
8140                 END IF;
8141 
8142                 l_op_plan_instance_id := l_wooi_data_rec.op_plan_instance_id;
8143 
8144                 l_progress := 100;
8145 
8146                 /* Checking if the Operation Sequnce of Load is greater than that of Drop
8147                  * Ideally this case should never occur.Indication of data corruption
8148                  */
8149 
8150                 IF (l_min_opertn_sequence > l_wooi_data_rec.operation_sequence) THEN
8151 
8152                    IF (l_debug=1) THEN
8153                       print_debug('Operation sequence of Load greater than that of drop for the same source task id',l_module_name,9);
8154                    END IF;
8155 
8156                    RAISE_APPLICATION_ERROR(DATA_INCONSISTENT,'Operation Sequence of Load Operation greater than that of Drop');
8157                 END IF;
8158 
8159                 l_progress := 110;
8160 
8161                 IF (l_debug=1) THEN
8162                    print_debug('Calling Deleting Operation Instance with operation Instance Id'||l_wooi_data_rec.operation_instance_id,l_module_name,9);
8163                 END IF;
8164 
8165                 WMS_OP_RUNTIME_PVT_APIS.DELETE_OPERATION_INSTANCE
8166                    ( p_operation_instance_id => l_wooi_data_rec.operation_instance_id
8167                     ,x_return_status => x_return_status
8168                     ,x_msg_count     => x_msg_count
8169                     ,x_msg_data      => x_msg_data);
8170 
8171                 l_progress:=120;
8172 
8173                 IF l_debug=1 THEN
8174                    print_debug('Return Status from Delete Operation Instance '||x_return_status,l_module_name,9);
8175                 END IF;
8176 
8177                 IF (x_return_status=g_ret_sts_error) THEN
8178                    RAISE FND_API.G_EXC_ERROR;
8179 
8180                 ELSIF (x_return_status<>g_ret_sts_success) THEN
8181                   RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
8182 
8183                 END IF;
8184 
8185               END IF; /*Operation Type Drop*/
8186 
8187              END LOOP;
8188 
8189              l_progress:=130;
8190 
8191              /* If the above fetched operation is the first in the Plan then
8192               * Call WMS_OP_RUNTIME_APIS_PVT.UPDATE_PLAN_INSTANCE to set the plan status as Pending.
8193 				  */
8194               OPEN c_min_operation_seq(l_op_plan_id);
8195 
8196               FETCH c_min_operation_seq INTO l_first_opertn_seq;
8197 
8198               IF (c_min_operation_seq%NOTFOUND) THEN
8199 
8200                  IF (l_debug=1) THEN
8201                     print_debug('NO Operation Detail Record found',l_module_name,9);
8202                  END IF;
8203 
8204                  CLOSE c_min_operation_seq;
8205 
8206                  RAISE_APPLICATION_ERROR(INVALID_PLAN_ID,'Invalid Plan Id stamped on document record');
8207 
8208               END IF;
8209 
8210               CLOSE c_min_operation_seq;
8211 
8212               l_progress := 140;
8213 
8214               IF (l_first_opertn_seq = l_min_opertn_sequence) THEN
8215 
8216                  IF (l_debug=1) THEN
8217                     print_debug('It is the first Operation in the Plan,hence update status to pending',l_module_name,9);
8218                  END IF;
8219 
8220                  l_wopi_rec.op_plan_instance_id := l_op_plan_instance_id;
8221 
8222                  l_wopi_rec.status              := G_OP_INS_STAT_PENDING;
8223 
8224                  l_progress :=150;
8225 
8226                  IF l_debug=1 THEN
8227                     print_debug('Calling WMS_OP_RUNTIME_APIS_PVT.UPDATE_PLAN_INSTANCE with the following parameters',l_module_name,9);
8228                     print_debug('Op_plan_instance_id => '||l_wopi_rec.op_plan_instance_id,l_module_name,9);
8229                     print_debug('Status              => '||l_wopi_rec.status,l_module_name,9);
8230                  END IF;
8231 
8232                  WMS_OP_RUNTIME_PVT_APIS.UPDATE_PLAN_INSTANCE
8233                    (p_update_rec    => l_wopi_rec,
8234                     x_return_status => x_return_status,
8235                     x_msg_count     => x_msg_count,
8236                     x_msg_data      => x_msg_data);
8237 
8238                  IF (l_debug =1) THEN
8239                     print_debug('Return Status from Update Plan Instance'||x_return_status,l_module_name,9);
8240                  END IF;
8241 
8242                  IF (x_return_status=g_ret_sts_error) THEN
8243                    RAISE FND_API.G_EXC_ERROR;
8244 
8245                  ELSIF (x_return_status<>g_ret_sts_success) THEN
8246                    RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
8247 
8248                  END IF;
8249 
8250               END IF;/*First Operation in Plan*/
8251 
8252               /*If p_activity_id =INBOUND then
8253                *  Call WMS_OP_INBOUND_PVT.Cleanup
8254                *End If;
8255                */
8256               l_progress :=160;
8257              IF p_activity_type_id =G_OP_ACTIVITY_INBOUND THEN
8258 
8259                 WMS_OP_INBOUND_PVT.Cleanup
8260                   (  x_return_status   => x_return_status
8261  	               , x_msg_data         => x_msg_data
8262  	               , x_msg_count        => x_msg_count
8263  	               , p_source_task_id   => p_source_task_id
8264  	              );
8265 
8266                IF l_debug=1 THEN
8267                   print_debug(' Return Status from Cleanup '||x_return_status,l_module_name,9);
8268                END IF;
8269 
8270 
8271                IF (x_return_status=g_ret_sts_error) THEN
8272                     RAISE FND_API.G_EXC_ERROR;
8273 
8274                ELSIF (x_return_status<>g_ret_sts_success) THEN
8275                     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
8276 
8277                END IF;
8278 
8279 
8280 	       /* Deleting the WDT record as the status is being reverted to Load Pending */
8281 	       -- Moved delete WDT to after calling  WMS_OP_INBOUND_PVT.Cleanup
8282 	       -- because WMS_OP_INBOUND_PVT.Cleanup now calls putaway_cleanup, which assumes WDT still exists.
8283 
8284 	       DELETE FROM WMS_DISPATCHED_TASKS
8285 		 WHERE transaction_temp_id = p_source_task_id
8286 		 AND task_id = l_wdt_details.task_id;
8287 
8288 	       l_progress := 170;
8289 
8290              END IF;/*Activity is Inbound*/
8291 
8292    EXCEPTION
8293 
8294       WHEN FND_API.G_EXC_ERROR THEN
8295 
8296          IF (l_debug=1) THEN
8297             print_debug('Expected Error Obtained at'||l_progress,l_module_name,1);
8298          END IF;
8299 
8300          IF c_wdt_details%ISOPEN THEN
8301             CLOSE c_wdt_details;
8302          END IF;
8303 
8304          IF c_inbound_document_details%ISOPEN THEN
8305             CLOSE c_inbound_document_details;
8306          END IF;
8307 
8308          IF c_operation_instances%ISOPEN THEN
8309             CLOSE c_operation_instances;
8310          END IF;
8311 
8312          IF c_min_operation_seq%ISOPEN THEN
8313             CLOSE c_min_operation_seq;
8314          END IF;
8315 
8316          x_return_status := g_ret_sts_error;
8317 
8318          ROLLBACK TO rollback_op_plan_sp;
8319 
8320       WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8321 
8322          IF (l_debug=1) THEN
8323             print_debug('Expected Error Obtained at'||l_progress,l_module_name,1);
8324          END IF;
8325 
8326          IF c_wdt_details%ISOPEN THEN
8327             CLOSE c_wdt_details;
8328          END IF;
8329 
8330          IF c_inbound_document_details%ISOPEN THEN
8331             CLOSE c_inbound_document_details;
8332          END IF;
8333 
8334          IF c_operation_instances%ISOPEN THEN
8335             CLOSE c_operation_instances;
8336          END IF;
8337 
8338          IF c_min_operation_seq%ISOPEN THEN
8339             CLOSE c_min_operation_seq;
8340          END IF;
8341 
8342          x_return_status := g_ret_sts_unexp_error;
8343 
8344          ROLLBACK TO rollback_op_plan_sp;
8345 
8346       WHEN OTHERS THEN
8347 
8348             print_debug(SQLERRM, l_module_name,1);
8349             IF (l_debug=1) THEN
8350               print_debug(l_progress||' '||SQLERRM, l_module_name,1);
8351             END IF;
8352 
8353             IF fnd_msg_pub.check_msg_level(g_msg_lvl_unexp_error) THEN
8354               fnd_msg_pub.add_exc_msg(g_pkg_name, l_module_name, SQLERRM);
8355             END IF; /* fnd_msg.... */
8356 
8357             fnd_msg_pub.count_and_get(
8358                                    p_count => x_msg_count,
8359                                    p_data  => x_msg_data
8360                                   );
8361 
8362             IF (SQLCODE<-20000) THEN
8363               IF (l_debug=1) THEN
8364                 print_debug('This is a user defined exception',l_module_name,1);
8365               END IF;
8366 
8367               x_error_code:=-(SQLCODE+20000);
8368 
8369               x_return_status:=g_ret_sts_error;
8370 
8371 
8372             ELSE
8373 
8374               x_return_status := g_ret_sts_unexp_error;
8375 
8376             END IF;
8377 
8378             IF c_wdt_details%ISOPEN THEN
8379               CLOSE c_wdt_details;
8380            END IF;
8381 
8382            IF c_inbound_document_details%ISOPEN THEN
8383               CLOSE c_inbound_document_details;
8384            END IF;
8385 
8386            IF c_operation_instances%ISOPEN THEN
8387               CLOSE c_operation_instances;
8388            END IF;
8389 
8390            IF c_min_operation_seq%ISOPEN THEN
8391               CLOSE c_min_operation_seq;
8392            END IF;
8393 
8394 
8395             ROLLBACK TO rollback_op_plan_sp;
8396 
8397    END Rollback_Operation_Plan;
8398 
8399 
8400 /**
8401   *   Check_Plan_Status
8402   *   <p>This procedure returns the operation plan instance
8403   *   status for a given source task ID and activity.</p>
8404   *  @param x_return_status    Return Status
8405   *  @param x_msg_data         Returns the Message Data
8406   *  @param x_msg_count        Returns the Error Message
8407   *  @param x_error_code       Returns appropriate error code in case
8408   *                            of an error.
8409   *  @param x_plan_status      Returns the Status of the Plan
8410   *  @param p_source_task_id   Identifier of the source document record
8411   *  @param p_activity_type_id Lookup code of the Activity type Id
8412   **/
8413    PROCEDURE  Check_Plan_Status(
8414               x_return_status    OUT  NOCOPY  VARCHAR2,
8415               x_msg_data         OUT  NOCOPY  FND_NEW_MESSAGES.MESSAGE_TEXT%TYPE,
8416               x_msg_count        OUT  NOCOPY  NUMBER,
8417               x_error_code       OUT  NOCOPY  NUMBER,
8418               x_plan_status      OUT  NOCOPY  NUMBER,
8419               p_source_task_id   IN           NUMBER,
8420               p_activity_type_id IN           NUMBER
8421                                ) IS
8422 
8423       l_debug       NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'),0);
8424       l_module_name VARCHAR2(30) := 'Check_Plan_Status';
8425       l_progress    VARCHAR2(30);
8426 
8427       l_status      NUMBER := -1;
8428       l_plan_id     NUMBER := -1;
8429 
8430       CURSOR source_plan IS
8431       SELECT mmtt.operation_plan_id
8432       FROM   mtl_material_transactions_temp mmtt
8433       WHERE  mmtt.transaction_temp_id = p_source_task_id;
8434 
8435       CURSOR plan_status IS
8436       SELECT wopi.status
8437       FROM   wms_op_plan_instances wopi
8438       WHERE  wopi.activity_type_id = p_activity_type_id
8439       AND    wopi.source_task_id = p_source_task_id;
8440 
8441       CURSOR op_plan_status IS
8442        SELECT wopi.status
8443          FROM wms_op_plan_instances wopi
8444         WHERE wopi.op_plan_instance_id = (SELECT op_plan_instance_id
8445                                           FROM wms_op_operation_instances wooi
8446                                           WHERE wooi.source_task_id = p_source_task_id
8447                                           AND wooi.activity_type_id = p_activity_type_id
8448                                           AND ROWNUM =1);
8449 
8450 
8451    BEGIN
8452 
8453          l_progress := '10';
8454 
8455          IF (l_debug = 1) THEN
8456              print_debug(' p_source_task_id ==> '||p_source_task_id ,
8457                            l_module_name,3);
8458              print_debug(' p_activity_type_id ==> '||p_activity_type_id ,
8459                            l_module_name,3);
8460          END IF;
8461 
8462          l_progress := '20';
8463          /*
8464           * The activity type and the the source task cannot be null
8465           * Raise Illegal Argument Exception
8466           */
8467 
8468          IF (p_source_task_id IS NULL OR p_activity_type_id IS NULL) THEN
8469 
8470             /*RAISE fnd_api.g_exc_error;*/
8471             RAISE_APPLICATION_ERROR(INVALID_INPUT,'Invalid inputs passed'||p_source_task_id||p_activity_type_id);
8472 
8473          END IF; /* p_source_task_id IS...*/
8474 
8475          l_progress := '30';
8476 
8477          /*
8478           * If the activity type is Inbound then the source task type Id
8479           * corresponds to a MMTT record.
8480           *
8481           * This API is called from CREATE_SUGGESTIONS.. and it is possible
8482           * that is called for MMTT records that do not have an operation
8483           * plan
8484           * In such a case we should return a success and plan status
8485           * should be NULL
8486           *
8487           * If a plan exists then find out the plan status
8488           */
8489          IF p_activity_type_id = g_op_activity_inbound THEN
8490 
8491             OPEN  source_plan;
8492             FETCH source_plan INTO l_plan_id;
8493             CLOSE source_plan;
8494 
8495             l_progress := '40';
8496 
8497            /*
8498             * l_plan_id has a value of -1 if the cursor failed to fetch
8499             * any rows, this means that there is no MMTT corresponding
8500             * to the passed document Id,i.e. The document Id passed is
8501             * invalid.
8502             *
8503             * In this case Raise Illegal Argument Error.
8504             */
8505 
8506             IF l_plan_id = -1 THEN
8507 
8508                /*RAISE fnd_api.g_exc_error;*/
8509                RAISE_APPLICATION_ERROR(INVALID_DOC_ID,'Document Record does not exist for Source_task_id'||p_source_task_id);
8510 
8511             END IF; /* l_plan_id = -1 */
8512 
8513             l_progress := '50';
8514 
8515            /*
8516             * MMTT exists but plan id is NULL
8517             */
8518             IF l_plan_id IS NULL THEN
8519 
8520                l_status := NULL;
8521                GOTO success;
8522 
8523             END IF; /* l_plan_id IS NULL*/
8524 
8525          END IF; /* p_activity...*/
8526 
8527          l_progress := '60';
8528          /*
8529           * The assumption being made here is that the source task id
8530           * passed is the parent document id.
8531           */
8532 
8533          OPEN plan_status;
8534          FETCH plan_status INTO l_status;
8535          CLOSE plan_status;
8536 
8537          l_progress := '70';
8538 
8539          /*
8540           * l_status has a value of -1 if the cursor failed to fetch
8541           * any rows, this means that there is no plan corresponding
8542           * to the passed document Id,therefore need to check the
8543           * if the document Id is a part of Operation Instance,then
8544           * fetch plan status for the Operation Plan Instance correspomding
8545           * to the Operation
8546           * else
8547           *   Raise Expected Error.
8548           */
8549          IF l_status = -1 THEN
8550 
8551             OPEN op_plan_status;
8552 
8553             FETCH op_plan_status INTO l_status;
8554 
8555             CLOSE op_plan_status;
8556 
8557             IF l_status=-1 THEN
8558                /* Operation Instance does not exist*/
8559 
8560                RAISE_APPLICATION_ERROR(OPERATION_INSTANCE_NOT_EXISTS,'Operation instance does not exist for Source task Id'||p_source_task_id);
8561             END IF;
8562 
8563             l_progress :=75;
8564 
8565          END IF; /* l_status = -1 */
8566 
8567          l_progress := '80';
8568 
8569          <<SUCCESS>>
8570 
8571          x_return_status  := g_ret_sts_success;
8572          x_plan_status    := l_status;
8573          fnd_msg_pub.count_and_get(
8574                                     p_count => x_msg_count,
8575                                     p_data  => x_msg_data
8576                                   );
8577 
8578    EXCEPTION
8579 
8580          /*WHEN FND_API.G_EXC_ERROR THEN
8581             print_debug(l_progress||' '||SQLERRM, l_module_name,1);
8582 
8583             IF fnd_msg_pub.check_msg_level(g_msg_lvl_error) THEN
8584                fnd_msg_pub.add_exc_msg(g_pkg_name, l_module_name, SQLERRM);
8585             END IF; /* fnd_msg.... */
8586 
8587             /*fnd_msg_pub.count_and_get(
8588                                        p_count => x_msg_count,
8589                                        p_data  => x_msg_data
8590                                       );
8591             x_error_code := SQLCODE;
8592             x_return_status := g_ret_sts_error;*/
8593 
8594          WHEN OTHERS THEN
8595 
8596             print_debug(l_progress||' '||SQLERRM, l_module_name,1);
8597 
8598             IF fnd_msg_pub.check_msg_level(g_msg_lvl_unexp_error) THEN
8599                fnd_msg_pub.add_exc_msg(g_pkg_name, l_module_name, SQLERRM);
8600             END IF; /* fnd_msg.... */
8601 
8602             fnd_msg_pub.count_and_get(
8603                                        p_count => x_msg_count,
8604                                        p_data  => x_msg_data
8605                                       );
8606             IF (SQLCODE<-20000) THEN
8607                IF (l_debug=1) THEN
8608                  print_debug('This is a user defined exception',l_module_name,1);
8609                END IF;
8610 
8611                x_error_code:=-(SQLCODE+20000);
8612 
8613                x_return_status:=g_ret_sts_error;
8614 
8615 
8616             ELSE
8617 
8618                x_return_status := g_ret_sts_unexp_error;
8619             END IF;
8620 
8621    END Check_Plan_Status;
8622 
8623 
8624 
8625 
8626 END WMS_ATF_RUNTIME_PUB_APIS;