DBA Data[Home] [Help]

PACKAGE BODY: APPS.WMS_WAVE_PLANNING_PVT

Source


1 PACKAGE BODY wms_wave_planning_pvt as
2   /* $Header: WMSWVPVB.pls 120.57.12020000.10 2013/03/25 06:46:55 ssrikaku ship $ */
3 
4  l_seq_val NUMBER;
5  g_mmtt_table         num_t := num_t(); --11870443
6 
7 
8  TYPE troLineRecTyp IS RECORD (
9       delivery_detail_id    NUMBER,
10       inventory_item_id     NUMBER,
11       from_sub       VARCHAR2(10),
12       from_locator             NUMBER,
13       lot_number               VARCHAR2(32),
14       revision                 VARCHAR2(3),
15       source_header_id     NUMBER,
16       requested_quantity     NUMBER,
17       source_line_id     NUMBER,
18       requested_quantity_uom   VARCHAR2(3),
19       SRC_REQUESTED_QUANTITY  NUMBER,
20       SRC_REQUESTED_QUANTITY_UOM VARCHAR2(3),
21       requested_quantity2     NUMBER,
22       requested_quantity_uom2 VARCHAR2(3),
23       ship_set_id       NUMBER,
24       ship_model_complete_flag VARCHAR2(1),
25       top_model_line_id     NUMBER,
26       date_scheduled      DATE,
27       project_id    NUMBER,
28       task_id NUMBER,
29       unit_number   NUMBER,
30       preferred_grade     VARCHAR2(150),
31       secondary_quantity     NUMBER,
32       top_model_quantity    NUMBER
33 );
34 
35 
36 TYPE troLineRecTabTyp IS TABLE OF troLineRecTyp INDEX BY BINARY_INTEGER;
37 g_troline_table troLineRecTabTyp;
38 
39 TYPE mtrlTblTyp  is table of mtl_txn_request_lines%rowtype index by binary_integer;
40 g_mtrl_tbl  mtrlTblTyp;
41 
42   TYPE psrTyp IS RECORD(
43     attribute      NUMBER,
44     attribute_name VARCHAR2(30),
45     priority       NUMBER,
46     sort_order     VARCHAR2(4));
47 
48   l_source_code_tb             char_tab;
49   l_source_header_id_tb        num_tab;
50   l_source_line_id_tb          num_tab;
51   l_source_header_number_tb    char1_tab;
52   l_source_line_number_tb      char1_tab;
53   l_source_header_type_id_tb   num_tab;
54   l_source_document_type_id_tb num_tab;
55   l_delivery_Detail_id_tb      num_tab;
56   l_organization_id_tb         num_tab;
57   l_item_id_tb                 num_tab;
58   l_delivery_id_tb             num_tab;
59   l_requested_quantity_tb      num_tab;
60   l_requested_quantity_uom_tb  uom_tab;
61   l_requested_quantity2_tb     num_tab;
62   l_requested_quantity_uom2_tb uom_tab;
63   l_demand_header_id_tb        num_tab;
64   l_net_weight_tb              num_tab;
65   l_volume_tb                  num_tab;
66   l_net_value                  num_tab;
67 
68   TYPE psrTabTyp IS TABLE OF psrTyp INDEX BY BINARY_INTEGER;
69   --
70   g_newline             CONSTANT VARCHAR2(10)  := fnd_global.newline;
71   -- PACKAGE CONSTANTS
72   --
73 
74   -- Indicate what attributes are used in Pick Sequence Rules
75   C_INVOICE_VALUE     CONSTANT BINARY_INTEGER := 1;
76   C_ORDER_NUMBER      CONSTANT BINARY_INTEGER := 2;
77   C_SCHEDULE_DATE     CONSTANT BINARY_INTEGER := 3;
78   C_TRIP_STOP_DATE    CONSTANT BINARY_INTEGER := 4;
79   C_SHIPMENT_PRIORITY CONSTANT BINARY_INTEGER := 5;
80 
81   -- Indicate status of functions
82   -- SUCCESS CONSTANT BINARY_INTEGER := 6;
83   --  DONE    CONSTANT BINARY_INTEGER := 7;
84   --  FAILURE CONSTANT BINARY_INTEGER := 8;
85 
86   --
87   -- PACKAGE VARIABLES
88   --
89   --g_initialized         BOOLEAN := FALSE;
90   g_ordered_psr psrTabTyp;
91   --  g_use_order_ps        VARCHAR2(1) := 'Y';
92   g_total_pick_criteria NUMBER;
93   g_primary_psr         VARCHAR2(30);
94   g_request_data varchar2(30);
95 
96   -- Track local PL/SQL table information
97   g_del_current_line NUMBER := 1;
98   g_rel_current_line NUMBER := 1;
99   first_line         relRecTyp;
100   MAX_LINES          NUMBER := 52;
101   -- Return status of procedures
102   g_return_status VARCHAR2(1);
103   ajith           varchar2(4000);
104   -- ajith1          varchar2(4000);
105   -- ajith2          varchar2(4000);
106   -- Column variables for DBMS_SQL package for mapping to values
107   -- selected from cursors
108   v_source_code              varchar2(30);
109   v_header_id                NUMBER;
110   v_line_id                  NUMBER;
111   v_header_number            varchar2(150);
112   v_line_number              varchar2(150);
113   v_header_type_name         varchar2(240);
114   v_header_type_id           NUMBER;
115   v_document_type_id         NUMBER;
116   v_delivery_detail_id       NUMBER;
117   v_released_status          varchar2(1);
118   v_org_id                   NUMBER;
119   v_inventory_item_id        NUMBER;
120   v_requested_quantity       NUMBER;
121   v_requested_quantity_uom   varchar2(3);
122   v_move_order_line_id       NUMBER;
123   v_ship_model_complete_flag varchar2(1);
124   v_top_model_id             NUMBER;
125   v_ship_from_location_id    NUMBER;
126   v_ship_to_location_id      NUMBER;
127   v_ship_method_code         varchar2(30);
128   v_shipment_priority_code   varchar2(30);
129   v_ship_set_id              NUMBER;
130   v_date_scheduled           date;
131   v_planned_departure_date   date;
132   v_delivery_id              NUMBER;
133   v_customer_id              NUMBER;
134   v_carrier_id               NUMBER;
135   v_preferred_grade          NUMBER;
136   v_requested_quantity2      NUMBER;
137   v_requested_quantity_uom2  varchar2(3);
138   v_project_id               NUMBER;
139   v_task_id                  NUMBER;
140   v_subinventory             varchar2(30);
141   v_weight_uom_code          varchar2(5);
142   v_net_weight               NUMBER;
143   v_volume_uom_code          varchar2(5);
144   v_volume                   NUMBER;
145   v_cursorID                 INTEGER;
146   v_ignore                   INTEGER;
147 --g_enforce_ship_set_and_smc VARCHAR2(1);
148   -- new_wave_type WSH_PICKING_BATCHES_PUB.Batch_Info_Rec;
149 
150   l_is_unreleased           BOOLEAN;
151   l_is_pending              BOOLEAN;
152   l_is_queued               BOOLEAN;
153   l_is_dispatched           BOOLEAN;
154   l_is_active               BOOLEAN;
155   l_is_loaded               BOOLEAN;
156   l_is_completed            BOOLEAN;
157   l_include_inbound         BOOLEAN;
158   l_include_outbound        BOOLEAN;
159   l_include_crossdock       BOOLEAN;
160   l_include_manufacturing   BOOLEAN;
161   l_include_warehousing     BOOLEAN;
162   l_include_sales_orders    BOOLEAN;
163   l_include_internal_orders BOOLEAN;
164   l_include_replenishment   BOOLEAN;
165   l_include_mo_transfer     BOOLEAN;
166   l_include_mo_issue        BOOLEAN;
167   l_include_lpn_putaway     BOOLEAN;
168   l_include_staging_move    BOOLEAN;
169   l_include_cycle_count     BOOLEAN;
170   l_is_pending_plan         BOOLEAN;
171   l_is_inprogress_plan      BOOLEAN;
172   l_is_completed_plan       BOOLEAN;
173   l_is_cancelled_plan       BOOLEAN;
174   l_is_aborted_plan         BOOLEAN;
175   l_query_independent_tasks BOOLEAN;
176   l_query_planned_tasks     BOOLEAN;
177 
178   l_organization_id            NUMBER;
179   l_subinventory               VARCHAR2(240);
180   l_locator_id                 NUMBER;
181   l_to_subinventory            VARCHAR2(240);
182   l_to_locator_id              NUMBER;
183   l_inventory_item_id          NUMBER;
184   l_category_set_id            NUMBER;
185   l_item_category_id           NUMBER;
186   l_employee_id                NUMBER;
187   l_equipment_type_id          NUMBER;
188   l_equipment                  VARCHAR2(240);
189   l_user_task_type_id          NUMBER;
190   l_from_task_quantity         NUMBER;
191   l_to_task_quantity           NUMBER;
192   l_from_task_priority         NUMBER;
193   l_to_task_priority           NUMBER;
194   l_from_creation_date         DATE;
195   l_to_creation_date           DATE;
196   l_from_purchase_order        VARCHAR2(240);
197   l_from_po_header_id          NUMBER;
198   l_to_purchase_order          VARCHAR2(240);
199   l_to_po_header_id            NUMBER;
200   l_from_rma                   VARCHAR2(240);
201   l_from_rma_header_id         NUMBER;
202   l_to_rma                     VARCHAR2(240);
203   l_to_rma_header_id           NUMBER;
204   l_from_requisition           VARCHAR2(240);
205   l_from_requisition_header_id NUMBER;
206   l_to_requisition             VARCHAR2(240);
207   l_to_requisition_header_id   NUMBER;
208   l_from_shipment              VARCHAR2(240);
209   l_to_shipment                VARCHAR2(240);
210   l_from_sales_order_id        NUMBER;
211   l_to_sales_order_id          NUMBER;
212   l_from_pick_slip             NUMBER;
213   l_to_pick_slip               NUMBER;
214   l_customer_id                NUMBER;
215   l_customer_category          VARCHAR2(240);
216   l_delivery_id                NUMBER;
217   l_carrier_id                 NUMBER;
218   l_ship_method_code           VARCHAR2(240);
219   l_trip_id                    NUMBER;
220   l_shipment_priority          VARCHAR2(240);
221   l_from_shipment_date         DATE;
222   l_to_shipment_date           DATE;
223   l_ship_to_state              VARCHAR2(240);
224   l_ship_to_country            VARCHAR2(240);
225   l_ship_to_postal_code        VARCHAR2(240);
226   l_from_lines_in_sales_order  NUMBER;
227   l_to_lines_in_sales_order    NUMBER;
228   l_manufacturing_type         VARCHAR2(240);
229   l_from_job                   VARCHAR2(240);
230   l_to_job                     VARCHAR2(240);
231   l_assembly_id                NUMBER;
232   l_from_start_date            DATE;
233   l_to_start_date              DATE;
234   l_from_line                  VARCHAR2(240);
235   l_to_line                    VARCHAR2(240);
236   l_department_id              NUMBER;
237   l_from_replenishment_mo      VARCHAR2(240);
238   l_to_replenishment_mo        VARCHAR2(240);
239   l_from_transfer_issue_mo     VARCHAR2(240);
240   l_to_transfer_issue_mo       VARCHAR2(240);
241   l_cycle_count_name           VARCHAR2(240);
242   l_op_plan_activity_id        NUMBER;
243   l_op_plan_type_id            NUMBER;
244   l_op_plan_id                 NUMBER;
245 
246   -- l_action_description   VARCHAR2(1000);
247   l_tasks_total          NUMBER;
248   l_action_type          VARCHAR2(10);
249   l_status               VARCHAR2(60);
250   l_status_code          NUMBER;
251   l_priority_type        VARCHAR2(10);
252   l_priority             NUMBER;
253   l_clear_priority       VARCHAR2(100);
254   l_assign_type          VARCHAR2(100);
255   l_employee             VARCHAR2(100);
256   l_user_task_type       VARCHAR2(100);
257   l_effective_start_date DATE;
258   l_effective_end_date   DATE;
259   l_person_resource_id   NUMBER;
260   l_person_resource_code VARCHAR2(100);
261   l_override_emp_check   BOOLEAN;
262   l_temp_query           BOOLEAN;
263   l_temp_action          BOOLEAN;
264   l_wave_header_id       number;
265   g_update_wdd      VARCHAR2(1) := 'N';
266 
267 
268   PROCEDURE print_debug(p_err_msg VARCHAR2, p_debug number) IS
269 
270   BEGIN
271 
272     IF p_debug = 1 THEN
273       inv_mobile_helper_functions.tracelog(p_err_msg => p_err_msg,
274                                            p_module  => 'WMS_WAVE_PLANNING_PVT',
275                                            p_level   => 4);
276     END IF;
277 
278   END print_debug;
279 
280   PROCEDURE print_form_messages(p_err_msg VARCHAR2) is
281 
282     l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
283   begin
284 
285     print_debug(' ' || p_err_msg, l_Debug);
286 
287   end print_form_messages;
288 
289 
290   /*
291 
292   This procedure is used to set the global variables from Forms and Concurrent Program
293   */
294   procedure set_global_variable(order_type_id          in number,
295                                 order_header_id        in number,
296                                 backorders_flag        in varchar2,
297                                 include_planned_lines  in varchar2,
298                                 customer_id            in number,
299                                 inventory_item_id      in number,
300                                 shipment_priority_code in varchar2,
301                                 ship_method_code       in varchar2,
302                                 ship_to_loc_id         in number,
303                                 project_id             in number,
304                                 task_id                in number,
305                                 delivery_id            in number,
306                                 trip_id                in number,
307                                 trip_stop_id           in number,
308                                 pick_seq_rule_id       in number,
309                                 pick_grouping_rule_id  number,
310                                 scheduled_days         in number,
311                                 scheduled_hrs          in number,
312                                 dock_days              in number,
313                                 dock_hours             in number,
314                                 customer_class_id      in VARCHAR2,
315                                 carrier_id             in number,
316                                 category_Set_id        in number,
317                                 add_lines              in varchar2 default 'N') IS
318     l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
319   begin
320 
321     g_add_lines := add_lines;
322 
323     if order_type_id is null then
324       g_order_type_id := 0;
325     else
326       g_order_type_id := order_type_id;
327     end if;
328     if order_header_id is null then
329       g_order_header_id := 0;
330     else
331       g_order_header_id := order_header_id;
332     end if;
333     --g_backorders_flag := backorders_flag;
334     g_backorders_flag := 'I';
335 
336     g_include_planned_lines := include_planned_lines;
337 
338     if customer_id is null then
339       g_customer_id := 0;
340     else
341       g_customer_id := customer_id;
342     end if;
343 
344     if customer_class_id is null then
345       g_customer_class_id := null;
346     else
347       --  g_customer_class_id := '''' || customer_class_id || '''';
348       g_customer_class_id := customer_class_id;
349     end if;
350 
351     -- g_existing_rsvs_only_flag := reservations_flag;
352     if inventory_item_id is null then
353       g_inventory_item_id := 0;
354     else
355       g_inventory_item_id := inventory_item_id;
356     end if;
357     if shipment_priority_code is not null then
358       g_shipment_priority := shipment_priority_code;
359     end if;
360     if ship_method_code is not null then
361       -- g_ship_method_code := '''' || ship_method_code || '''';
362       g_ship_method_code := ship_method_code;
363       print_debug('g_ship_method_code' || g_ship_method_code, l_debug);
364     end if;
365     /*
366     if ship_set_number is null then
367       g_ship_set_number := 0;
368     else
369       g_ship_set_number := ship_set_number;
370     end if;
371     */
372     g_ship_to_loc_id := SHIP_TO_LOC_ID;
373     if project_id is null then
374       g_project_id := 0;
375     else
376       g_project_id := project_id;
377     end if;
378     if task_id is null then
379       g_task_id := 0;
380     else
381       g_task_id := task_id;
382     end if;
383     --g_doc_set_id := document_Set_id;
384 
385     if delivery_id is null then
386       g_DELIVERY_ID := 0;
387     else
388       g_DELIVERY_ID := delivery_id;
389     end if;
390 
391     if trip_id is null then
392       g_trip_id := 0;
393     else
394       g_trip_id := trip_id;
395     end if;
396     if trip_stop_id is null then
397       g_trip_Stop_id := 0;
398     else
399       g_trip_stop_id := trip_stop_id;
400     end if;
401 
402     if category_Set_id is null then
403 
404       g_item_category_id := 0;
405 
406     else
407       g_item_category_id := category_Set_id;
408 
409     end if;
410     if scheduled_days is null and scheduled_hrs is null then
411       g_from_sched_ship_date := NULL; --????????????????
412 
413     else
414       --   if nvl(scheduled_days, 0) <= -1 or (nvl(scheduled_hrs, 0) / 24) <= -1 then
415       if nvl(scheduled_days, 0) < 0 or (nvl(scheduled_hrs, 0) / 24) < 0 then
416 
417         g_from_sched_ship_date := sysdate + nvl(scheduled_days, 0) +
418                                   nvl(scheduled_hrs, 0) / 24; --????????????????
419         g_to_sched_ship_date   := sysdate;
420 
421       else
422 
423         g_from_sched_ship_date := sysdate; --????????????????
424         g_to_sched_ship_date   := sysdate + nvl(scheduled_days, 0) +
425                                   nvl(scheduled_hrs, 0) / 24;
426       end if;
427     end if;
428 
429     if dock_days is null and dock_hours is null then
430       g_from_dock_appoint_date := NULL; --????????????????
431 
432     else
433 
434       --   if nvl(dock_hours, 0) <= -1 or (nvl(dock_days, 0) / 24) <= -1 then
435       if nvl(dock_hours, 0) < 0 or (nvl(dock_days, 0) / 24) < 0 then
436         g_from_dock_appoint_date := sysdate + nvl(dock_days, 0) +
437                                     nvl(dock_hours, 0) / 24; --????????????????
438         g_to_dock_appoint_date   := sysdate;
439       else
440 
441         g_from_dock_appoint_date := sysdate; --????????????????
442         g_to_dock_appoint_date   := sysdate + nvl(dock_days, 0) +
443                                     nvl(dock_hours, 0) / 24;
444       end if;
445     end if;
446 
447     --g_from_sched_ship_date   := NULL; --????????????????
448     -- g_from_dock_appoint_date := NULL;
449     g_pick_seq_rule_id      := pick_seq_rule_id;
450     g_pick_grouping_rule_id := pick_grouping_rule_id;
451 
452     if carrier_id is null then
453       g_carrier_id := 0;
454     else
455       g_carrier_id := carrier_id;
456     end if;
457 
458     print_debug('In Set Global Variables', l_Debug);
459 
460     print_debug('Order Type Id ' || g_order_type_id, l_debug);
461     --print_debug('g_ship_set_number ' || g_ship_set_number, l_debug);
462     print_debug('g_task_id ' || g_task_id, l_debug);
463     print_debug('(g_project_id ' || g_project_id, l_debug);
464     print_debug('g_trip_id ' || g_trip_id, l_debug);
465     print_debug('Delivery id ' || g_DELIVERY_ID, l_debug);
466     print_debug('Customer Class Id ' || g_customer_class_id, l_debug);
467     print_debug('Inventory Item Id ' || g_inventory_item_id, l_debug);
468     print_debug('scheduled_days ' || scheduled_days, l_debug);
469     print_debug('scheduled_hrs ' || scheduled_hrs, l_debug);
470     print_debug('g_from_sched_ship_date ' || g_from_sched_ship_date,
471                 l_debug);
472 
473     print_debug('g_to_sched_ship_date ' || g_to_sched_ship_date, l_debug);
474     print_debug('g_from_dock_appoint_date ' || g_from_dock_appoint_date,
475                 l_debug);
476     print_debug('g_to_dock_appoint_date ' || g_to_dock_appoint_date,
477                 l_debug);
478 
479     print_debug('Pick Sequence Rule Id ' || g_pick_seq_rule_id || ' : ' ||
480                 g_pick_grouping_rule_id,
481                 l_debug);
482     print_debug('g_include_planned_lines ' || g_include_planned_lines,
483                 l_debug);
484 
485   EXCEPTION
486     when others THEN
487       print_debug('Error : ' || SQLCODE || ' : ' || SQLERRM, l_debug);
488   END set_global_variable;
489 
490   PROCEDURE submit_WP_conc_request(p_wave_header_id in number,
491                                    p_org_id         in number,
492                                    x_request_id     OUT NOCOPY number) IS
493 
494     l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
495   begin
496 
497     --Calling the Concurrent Program that does the following
498     --Calls the Dynamic SQL
499     -- Creates the batch record
500     --Inserts into Lines Table
501     -- Calls the Pick Release process
502     --Should clear out All Global Variable ???????????????
503     x_request_id := FND_REQUEST.SUBMIT_REQUEST(application => 'WMS',
504                                                program     => 'WMS_WAVE_PLANNING',
505                                                description => 'Wave Planning Initiate process', -- Need to change to Upper Cae?????
506                                                start_time  => NULL,
507                                                argument1   => NULL,
508                                                argument2   => p_wave_header_id,
509                                                argument3   => p_org_id);
510 
511     commit;
512   exception
513     WHEN OTHERS THEN
514       print_debug('Error in submit Wave Planning Concurrent Request: ' ||
515                   SQLCODE || ' : ' || SQLERRM,
516                   l_debug);
517   end submit_WP_conc_request;
518 
519   FUNCTION Outstanding_Order_Value(p_header_id IN BINARY_INTEGER,
520                                    p_line_id   IN BINARY_INTEGER)
521     RETURN BINARY_INTEGER IS
522 
523     l_order_value BINARY_INTEGER;
524 
525   BEGIN
526 
527     --
528     SELECT SUM(NVL(L.ORDERED_QUANTITY, 0) * NVL(L.UNIT_SELLING_PRICE, 0))
529       INTO l_order_value
530       FROM OE_ORDER_HEADERS_ALL H, OE_ORDER_LINES_ALL L
531      WHERE H.HEADER_ID = p_header_id
532        and l.line_id = p_line_id
533        AND L.HEADER_ID = H.HEADER_ID;
534 
535     --
536     RETURN l_order_value;
537 
538   EXCEPTION
539     WHEN OTHERS THEN
540       --
541       RETURN 0;
542 
543     --
544 
545   END Outstanding_Order_Value;
546 
547   procedure delete_wave_header(p_wave_header_id in number)
548 
549    is
550 
551   begin
552 
553     delete from wms_wp_wave_headers_vl
554      where wave_header_id = p_wave_header_id;
555 
556     commit;
557 
558   end delete_wave_header;
559 
560   PROCEDURE launch_concurrent_CP(errbuf               OUT NOCOPY VARCHAR2,
561                                  retcode              OUT NOCOPY NUMBER,
562                                  p_wave_template_name in varchar2,
563                                  p_wave_header_id     in number,
564                                  p_org_id             in number) is
565 
566     x_msg_count              number;
567     x_return_status          VARCHAR2(100);
568     x_msg_data               VARCHAR2(500);
569     p_BATCH_ID               NUMBER;
570     P_REQUEST_ID             NUMBER;
571     l_debug                  NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'),
572                                            0);
573     p_order_type_id          number;
574     p_order_header_id        number;
575     p_backorders_flag        varchar2(1);
576     p_include_planned_lines  varchar2(1);
577     p_customer_id            number;
578     p_inventory_item_id      number;
579     p_shipment_priority_code varchar2(100);
580     p_ship_method_code       varchar2(100);
581     p_ship_to_loc_id         number;
582     p_project_id             number;
583     p_task_id                number;
584     p_delivery_id            number;
585     p_category_id            number;
586     p_trip_id                number;
587     p_trip_stop_id           number;
588     p_pick_seq_rule_id       number;
589     p_pick_grouping_rule_id  number;
590     p_scheduled_days         number;
591     p_scheduled_hrs          number;
592     p_dock_days              number;
593     p_dock_hours             number;
594     p_customer_class_id      varchar2(100);
595     p_planning_criteria_id   number;
596     p_carrier_id             NUMBER;
597     parent_request_id        number;
598     l_plan_req_status        boolean;
599     l_dummy                  boolean;
600     l_plan_completion_status VARCHAR2(100); -- Bug 12733479
601     l_plan_phase             VARCHAR2(100);
602     l_plan_status            VARCHAR2(100);
603     l_plan_dev_phase         VARCHAR2(100);
604     l_plan_dev_status        VARCHAR2(100);
605     l_plan_message           VARCHAR2(500);
606     l_completion_status      VARCHAR2(100);
607     l_rel_phase              VARCHAR2(100);
608     l_rel_status             VARCHAR2(100);
609     l_rel_dev_phase          VARCHAR2(100);
610     l_rel_dev_status         VARCHAR2(100);
611     l_rel_message            VARCHAR2(500);
612     l_mode                   VARCHAR2(4) := null;
613     l_request_data           VARCHAR2(100);
614     p_plan_wave              varchar2(1);
615     p_release_wave           varchar2(1);
616     v_wave_header_id         NUMBER;
617     plan_request_id          NUMBER;
618 
619     wave_creation_exception exception;
620     wave_line_exception exception;
621 
622   begin
623 
624     savepoint create_wave_lines_sp;
625 
626     v_wave_header_id := p_wave_header_id;
627 
628     l_mode         := NULL;
629     l_request_data := FND_CONC_GLOBAL.Request_Data;
630     if l_request_data is NULL then
631 
632       if p_wave_template_name is not null then
633         --If Concurrent program is Scheduled and template name is given
634         --Create Wave records for Wave Headers and Advanced Criteria table
635         wms_wave_planning_pvt.insert_wave_record(v_WAVE_HEADER_ID);
636         --insert into Advanced Criteria
637 
638         if x_return_status <> 'S' then
639           raise wave_Creation_exception;
640         end if;
641 
642       END IF;
643 
644       select order_type_id,
645              from_order_header_id,
646              backorders_flag,
647              include_planned_lines,
648              customer_id,
649              inventory_item_id,
650              ship_priority,
651              ship_method_code,
652              SHIP_TO_LOCATION_ID,
653              project_id,
654              task_id,
655              delivery_id,
656              trip_id,
657              trip_stop_id,
658              pick_seq_rule_id,
659              pick_grouping_rule_id,
660              scheduled_days,
661              scheduled_hrs,
662              dock_appointment_days,
663              dock_appointment_hours,
664              customer_class_id,
665              planning_criteria_id,
666              initiate_wave_planning,
667              RELEASE_IMMEDIATELY,
668              carrier_id,
669              category_id
670         into p_order_type_id,
671              p_order_header_id,
672              p_backorders_flag,
673              p_include_planned_lines,
674              p_customer_id,
675              p_inventory_item_id,
676              p_shipment_priority_code,
677              p_ship_method_code,
678              p_ship_to_loc_id,
679              p_project_id,
680              p_task_id,
681              p_delivery_id,
682              p_trip_id,
683              p_trip_stop_id,
684              p_pick_seq_rule_id,
685              p_pick_grouping_rule_id,
686              p_scheduled_days,
687              p_scheduled_hrs,
688              p_dock_days,
689              p_dock_hours,
690              p_customer_class_id,
691              p_planning_criteria_id,
692              p_plan_wave,
693              p_release_wave,
694              p_carrier_id,
695              p_category_id
696         from wms_wp_wave_headers_vl
697        where wave_header_id = v_wave_header_id;
698 
699       set_global_variable(p_order_type_id,
700                           p_order_header_id,
701                           p_backorders_flag,
702                           nvl(p_include_planned_lines, 'Y'),
703                           p_customer_id,
704                           p_inventory_item_id,
705                           p_shipment_priority_code,
706                           p_ship_method_code,
707                           p_ship_to_loc_id,
708                           p_project_id,
709                           p_task_id,
710                           p_delivery_id,
711                           p_trip_id,
712                           p_trip_stop_id,
713                           p_pick_seq_rule_id,
714                           p_pick_grouping_rule_id,
715                           p_scheduled_days,
716                           p_scheduled_hrs,
717                           p_dock_days,
718                           p_dock_hours,
719                           p_customer_class_id,
720                           p_carrier_id,
721                           p_category_id);
722 
723       update wms_wp_wave_headers_vl
724          set request_id = fnd_global.conc_request_id
725        where wave_header_id = v_wave_header_id;
726 
727       --Call the Dynamic SQL
728       get_dynamic_sql(v_wave_header_id, p_org_id, x_return_status);
729 
730       print_debug('l_request_data:' || l_request_data, l_debug);
731 
732       if x_return_status = 'S' then
733 
734         if p_plan_wave = 'Y' then
735 
736           print_debug('Calling Plan Wave Concurrent program:', l_debug);
737           parent_request_id := fnd_global.conc_request_id;
738 
739 
740           -- Bug 12733479 change. Commenting out the following call.
741           --FND_CONC_GLOBAL.Set_Req_Globals(Conc_Status  => 'PAUSED',
742           --                                Request_Data => 'Child');
743 
744           p_request_id := FND_REQUEST.SUBMIT_REQUEST(application => 'WMS',
745                                                      program     => 'PLAN_WAVE_CP',
746                                                      description => 'Wave Planning Plan Wave',
747                                                      start_time  => NULL,
748                                                      --sub_request => TRUE, Bug 12733479
749                                                      argument1   => v_wave_header_id,
750                                                      argument2   => p_planning_criteria_id);
751           print_debug('Calling Plan Wave Concurrent program with Request id:' ||
752                       p_request_id,
753                       l_debug);
754 
755           plan_request_id := p_request_id;
756 
757           commit; -- Bug 12733479
758 
759           IF p_request_id = 0 then
760              print_debug('Request submission failed for Plan Wave.', l_debug);
761              RAISE fnd_api.g_exc_unexpected_error;
762           ELSE
763              -- Bug 12733479
764              l_dummy := FND_CONCURRENT.WAIT_FOR_REQUEST(request_id => plan_request_id,
765                                                         interval => 5,
766                                                         phase => l_plan_phase,
767                                                         status => l_plan_status,
768                                                         dev_phase => l_plan_dev_phase,
769                                                         dev_status => l_plan_dev_status,
770                                                         message => l_plan_message);
771 
772              -- Bug 12733479 debug message
773              print_debug('After waiting for Plan Wave request.', l_debug);
774 
775              l_dummy := FND_CONCURRENT.GET_REQUEST_STATUS(plan_request_id,
776                                                           '',
777                                                           '',
778                                                           l_plan_phase,
779                                                           l_plan_status,
780                                                           l_plan_dev_phase,
781                                                           l_plan_dev_status,
782                                                           l_plan_message);
783 
784              -- Bug 12733479 debug message
785              print_debug('l_plan_dev_status: ' || l_plan_dev_status, l_debug);
786 
787              IF l_plan_dev_status = 'WARNING' THEN
788                 l_plan_completion_status := 'WARNING';
789 
790              ELSIF l_plan_dev_status <> 'NORMAL' THEN
791                 -- Bug 12733479 debug message
792                 print_debug('Plan wave request returned with ERROR. Setting the completion status of Wave Planning Initiate process CP to ERROR and exiting.', l_debug);
793                 l_plan_completion_status := 'ERROR';
794 
795                 l_dummy := FND_CONCURRENT.SET_COMPLETION_STATUS(l_plan_completion_status, '');
796 
797                 -- Bug 12733479
798                 GOTO END_LAUNCH_CONC_CP;
799 
800              ELSE
801                 l_plan_completion_status := 'NORMAL';
802 
803                 -- Bug 12733479 adding NVL condition
804                 IF NVL(p_release_wave,'A') <> 'Y' then
805                    -- Bug 12733479 debug message
806                    print_debug('Release wave is not checked.', l_debug);
807                    l_dummy := FND_CONCURRENT.SET_COMPLETION_STATUS(l_plan_completion_status, '');
808                 END IF;
809 
810              END IF; -- IF l_plan_dev_status
811 
812           END IF; -- IF p_request_id = 0
813 
814         end if; -- if p_plan_wave = 'Y'
815 
816         if p_release_wave = 'Y' THEN
817           -- Bug 12733479 change. Commenting out the following call.
818           --FND_CONC_GLOBAL.Set_Req_Globals(Conc_Status  => 'PAUSED',
819           --                               Request_Data => 'Child');
820 
821           p_request_id := FND_REQUEST.SUBMIT_REQUEST(application => 'WMS',
822                                                      program     => 'RELEASE_BATCH_CP',
823                                                      description => 'Wave Planning Release Wave',
824                                                      start_time  => NULL,
825                                                      --sub_request => TRUE, Bug 12733479
826                                                      argument1   => V_wave_header_id);
827 
828           print_debug('Calling Release Wave Concurrent program with Request id: ' || p_request_id, l_debug);
829           commit; -- Bug 12733479
830 
831           IF p_request_id = 0 then
832              print_debug('Request submission failed for Release Wave.', l_debug);
833              RAISE fnd_api.g_exc_unexpected_error;
834           ELSE
835              -- Bug 12733479
836              l_dummy := FND_CONCURRENT.WAIT_FOR_REQUEST(request_id => p_request_id,
837                                                         interval => 5,
838                                                         phase => l_rel_phase,
839                                                         status => l_rel_status,
840                                                         dev_phase => l_rel_dev_phase,
841                                                         dev_status => l_rel_dev_status,
842                                                         message => l_rel_message);
843 
844 
845              -- Bug 12733479 debug message
846              print_debug('After waiting for Release Wave request.', l_debug);
847 
848              l_dummy := FND_CONCURRENT.GET_REQUEST_STATUS(p_request_id,
849                                                           '',
850                                                           '',
851                                                           l_rel_phase,
852                                                           l_rel_status,
853                                                           l_rel_dev_phase,
854                                                           l_rel_dev_status,
855                                                           l_rel_message);
856 
857 
858              IF l_rel_dev_status = 'WARNING' THEN
859                 l_completion_status := 'WARNING';
860 
861              ELSIF l_rel_dev_status <> 'NORMAL' THEN
862                 l_completion_status := 'ERROR';
863 
864              ELSE
865                 l_completion_status := 'NORMAL';
866 
867                 -- Bug 12733479
868                 IF NVL(l_plan_completion_status,'A') = 'WARNING' THEN
869                    l_completion_status := 'WARNING';
870                 END IF;
871 
872              END IF;
873 
874              l_dummy := FND_CONCURRENT.SET_COMPLETION_STATUS(l_completion_status, '');
875 
876           END IF; -- if p_request_id = 0
877 
878         end if; -- if p_release_wave = 'Y'
879 
880       else
881         raise wave_line_exception;
882 
883       end if; -- if x_return_status = 'S'
884     end if; -- if l_request_data IS NULL
885 
886     <<END_LAUNCH_CONC_CP>> -- Bug 12733479
887     commit;
888 
889   exception
890     WHEN wave_creation_exception then
891 
892       rollback to create_wave_lines_sp;
893       RAISE fnd_api.g_exc_unexpected_error;
894 
895     WHEN wave_line_exception then
896 
897       rollback to create_wave_lines_sp;
898 
899       delete_wave_header(v_wave_header_id);
900       RAISE fnd_api.g_exc_unexpected_error;
901 
902     --  l_completion_status := 'ERROR';
903 
904     /*  l_dummy := FND_CONCURRENT.SET_COMPLETION_STATUS(l_completion_status,
905     '');*/
906 
907     WHEN OTHERS THEN
908       print_debug('Error in Launch Concurrent CP: ' || SQLCODE || ' : ' ||
909                   SQLERRM,
910                   l_debug);
911       --  l_completion_status := 'ERROR';
912       /*  l_dummy             := FND_CONCURRENT.SET_COMPLETION_STATUS(l_completion_status,
913 
914       '');*/
915 
916       RAISE fnd_api.g_exc_unexpected_error;
917   end launch_concurrent_CP;
918 
919   PROCEDURE Plan_Wave_CP(errbuf                 OUT NOCOPY VARCHAR2,
920                          retcode                OUT NOCOPY NUMBER,
921                          p_wave_header_id       in number,
922                          p_planning_criteria_id in number)
923 
924    is
925     l_debug         NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
926     x_return_status VARCHAR2(10);
927   begin
928 
929    g_request_data := FND_CONC_GLOBAL.Request_Data;
930    print_Debug('request data  '|| g_request_data, l_debug);
931 
932 if  g_request_data is null then
933     savepoint plan_wave_concurrent_sp;
934 
935     print_Debug('In Plan Wave Cp', l_debug);
936 
937     print_Debug('Calling Plan Wave API', l_debug);
938     plan_wave(p_wave_header_id, p_planning_criteria_id, x_return_Status);
939 
940     if x_return_Status <> 'S' then
941 
942       RAISE fnd_api.g_exc_unexpected_error;
943 
944     end if;
945 
946 end if;
947 
948   exception
949 
950     when others THEN
951 
952       print_debug('Error in Plan Wave CP: ' || SQLCODE || ' : ' || SQLERRM,
953                   l_debug);
954       rollback to plan_wave_concurrent_sp;
955       RAISE fnd_api.g_exc_unexpected_error;
956   end plan_wave_cp;
957 
958   procedure insert_wave_record(p_wave_header_id in OUT NOCOPY number) is
959     l_debug          NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
960     l_wave_header_id number;
961   begin
962 
963     select WMS_WP_WAVE_HEADERS_S.NEXTVAL into l_wave_header_id from dual;
964 
965   INSERT INTO wms_wp_wave_headers_vl
966       (wave_header_id,
967        WAVE_NAME,
968        WAVE_DESCRIPTION,
969        start_time,  -- start time changes
970        WAVE_SOURCE,
971        WAVE_STATUS,
972        TYPE_ID,
973        BATCH_ID,
974        SHIP_TO_LOCATION_ID,
975        CUSTOMER_CLASS_ID,
976        pull_replenishment_flag,
977        INITIATE_WAVE_PLANNING,
978        RELEASE_IMMEDIATELY,
979        TABLE_NAME,
980        ADVANCED_CRITERIA,
981        CREATED_BY,
982        CREATION_DATE,
983        LAST_UPDATED_BY,
984        LAST_UPDATE_DATE,
985        LAST_UPDATE_LOGIN,
986        ORGANIZATION_ID,
987        PICK_SEQ_RULE_ID,
988        PICK_GROUPING_RULE_ID,
989        TRIP_ID,
990        TRIP_STOP_ID,
991        SHIP_METHOD_CODE,
992        SHIPMENT_PRIORITY_CODE,
993        CARRIER_ID,
994        DELIVERY_ID,
995        FROM_ORDER_HEADER_ID,
996        ORDER_TYPE_ID,
997        CUSTOMER_ID,
998        TASK_ID,
999        PROJECT_ID,
1000        CATEGORY_SET_ID,
1001        CATEGORY_ID,
1002        INVENTORY_ITEM_ID,
1003        BACKORDERS_FLAG,
1004        INCLUDE_PLANNED_LINES,
1005        TASK_PLANNING_FLAG,
1006        APPEND_DELIVERIES,
1007        AUTO_CREATE_DELIVERY,
1008        AUTO_CREATE_DELIVERY_CRITERIA,
1009        TASK_PRIORITY,
1010        DEFAULT_STAGE_SUBINVENTORY,
1011        DEFAULT_STAGE_LOCATOR_ID,
1012        DEFAULT_ALLOCATION_METHOD,
1013        WAVE_FIRMED_FLAG,
1014        WAVE_COMPLETION_TIME,
1015        ORDER_NAME,
1016        CUSTOMER,
1017        ORDER_TYPE,
1018        CUSTOMER_CLASS,
1019        SHIP_METHOD,
1020        CARRIER,
1021        SHIP_PRIORITY,
1022        DELIVERY,
1023        TRIP,
1024        TRIP_STOP,
1025        ITEM,
1026        ITEM_CATEGORY,
1027        PROJECT_NAME,
1028        TASK_NAME,
1029        SCHEDULED_DAYS,
1030        SCHEDULED_HRS,
1031        DOCK_APPOINTMENT_DAYS,
1032        DOCK_APPOINTMENT_HOURS,
1033        PICK_SLIP_GROUP,
1034        RELEASE_SEQ_RULE,
1035        STAGING_SUBINVENTORY,
1036        STAGING_LOCATOR,
1037        CROSS_DOCK_CRITERIA,
1038        PLANNING_CRITERIA,
1039        PLANNING_CRITERIA_ID,
1040        pick_subinventory)
1041       SELECT l_wave_header_id,
1042              WAVE_NAME || '-' || l_wave_header_id,
1043              WAVE_DESCRIPTION,
1044              start_time, -- start time changes
1045              WAVE_SOURCE,
1046              WAVE_STATUS, --Need to Confirm??????
1047              'W',
1048              BATCH_ID,
1049              SHIP_TO_LOCATION_ID,
1050              CUSTOMER_CLASS_ID,
1051              pull_replenishment_flag,
1052              INITIATE_WAVE_PLANNING,
1053              RELEASE_IMMEDIATELY,
1054              TABLE_NAME,
1055              ADVANCED_CRITERIA,
1056              CREATED_BY,
1057              CREATION_DATE,
1058              fnd_global.user_id,
1059              sysdate,
1060              fnd_global.login_id,
1061              ORGANIZATION_ID,
1062              PICK_SEQ_RULE_ID,
1063              PICK_GROUPING_RULE_ID,
1064              TRIP_ID,
1065              TRIP_STOP_ID,
1066              SHIP_METHOD_CODE,
1067              SHIPMENT_PRIORITY_CODE,
1068              CARRIER_ID,
1069              DELIVERY_ID,
1070              FROM_ORDER_HEADER_ID,
1071              ORDER_TYPE_ID,
1072              CUSTOMER_ID,
1073              TASK_ID,
1074              PROJECT_ID,
1075              CATEGORY_SET_ID,
1076              CATEGORY_ID,
1077              INVENTORY_ITEM_ID,
1078              BACKORDERS_FLAG,
1079              INCLUDE_PLANNED_LINES,
1080              TASK_PLANNING_FLAG,
1081              APPEND_DELIVERIES,
1082              AUTO_CREATE_DELIVERY,
1083              AUTO_CREATE_DELIVERY_CRITERIA,
1084              TASK_PRIORITY,
1085              DEFAULT_STAGE_SUBINVENTORY,
1086              DEFAULT_STAGE_LOCATOR_ID,
1087              DEFAULT_ALLOCATION_METHOD,
1088              WAVE_FIRMED_FLAG,
1089              WAVE_COMPLETION_TIME,
1090              ORDER_NAME,
1091              CUSTOMER,
1092              ORDER_TYPE,
1093              CUSTOMER_CLASS,
1094              SHIP_METHOD,
1095              CARRIER,
1096              SHIP_PRIORITY,
1097              DELIVERY,
1098              TRIP,
1099              TRIP_STOP,
1100              ITEM,
1101              ITEM_CATEGORY,
1102              PROJECT_NAME,
1103              TASK_NAME,
1104              SCHEDULED_DAYS,
1105              SCHEDULED_HRS,
1106              DOCK_APPOINTMENT_DAYS,
1107              DOCK_APPOINTMENT_HOURS,
1108              PICK_SLIP_GROUP,
1109              RELEASE_SEQ_RULE,
1110              STAGING_SUBINVENTORY,
1111              STAGING_LOCATOR,
1112              CROSS_DOCK_CRITERIA,
1113              PLANNING_CRITERIA,
1114              PLANNING_CRITERIA_ID,
1115              pick_subinventory
1116         FROM wms_wp_wave_headers_vl
1117        WHERE wave_header_id = p_wave_header_id;
1118 
1119     INSERT INTO wms_wp_Advanced_Criteria
1120       (RULE_ID,
1121        RULE_WAVE_HEADER_ID,
1122        SEQUENCE_NUMBER,
1123        LAST_UPDATED_BY,
1124        LAST_UPDATE_DATE,
1125        CREATED_BY,
1126        CREATION_DATE,
1127        LAST_UPDATE_LOGIN,
1128        LOGICAL_OPERATOR_CODE,
1129        LOGICAL_OPERATOR_MEANING,
1130        BRACKET_OPEN,
1131        OBJECT_ID,
1132        OBJECT_NAME,
1133        OBJECT_DESCRIPTION,
1134        PARAMETER_ID,
1135        PARAMETER_NAME,
1136        PARAMETER_DESCRIPTION,
1137        OPERATOR_CODE,
1138        OPERATOR_MEANING,
1139        OPERATOR_DESCRIPTION,
1140        OPERAND_VALUE,
1141        OPERAND_CHAR,
1142        BRACKET_CLOSE)
1143       SELECT wms_wp_advanced_criteria_s.NEXTVAL,
1144              l_wave_header_id,
1145              SEQUENCE_NUMBER,
1146              fnd_global.user_id,
1147              sysdate,
1148              CREATED_BY,
1149              CREATION_DATE,
1150              fnd_global.login_id,
1151              LOGICAL_OPERATOR_CODE,
1152              LOGICAL_OPERATOR_MEANING,
1153              BRACKET_OPEN,
1154              OBJECT_ID,
1155              OBJECT_NAME,
1156              OBJECT_DESCRIPTION,
1157              PARAMETER_ID,
1158              PARAMETER_NAME,
1159              PARAMETER_DESCRIPTION,
1160              OPERATOR_CODE,
1161              OPERATOR_MEANING,
1162              OPERATOR_DESCRIPTION,
1163              OPERAND_VALUE,
1164              OPERAND_CHAR,
1165              BRACKET_CLOSE
1166         FROM wms_wp_Advanced_Criteria
1167        WHERE rule_wave_header_id = p_wave_header_id;
1168 
1169     select WMS_WP_WAVE_HEADERS_S.CURRVAL into p_wave_header_id from dual;
1170 
1171   exception
1172     when others then
1173       select WMS_WP_WAVE_HEADERS_S.CURRVAL into p_wave_header_id from dual;
1174       print_debug('Error in Insert Wave Record: ' || SQLCODE || ' : ' ||
1175                   SQLERRM,
1176                   l_debug);
1177   end insert_wave_record;
1178 
1179   PROCEDURE Call_Plan_Wave_CP(p_wave_header_id       in number,
1180                               p_planning_criteria_id in number,
1181                               p_request_id           OUT NOCOPY number)
1182 
1183    is
1184     l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
1185   begin
1186 
1187     print_Debug('In Call Plan Wave', l_debug);
1188     p_request_id := FND_REQUEST.SUBMIT_REQUEST(application => 'WMS',
1189                                                program     => 'PLAN_WAVE_CP',
1190                                                description => 'Wave Planning Plan Wave',
1191                                                start_time  => NULL,
1192                                                argument1   => p_wave_header_id,
1193                                                argument2   => p_planning_criteria_id);
1194   exception
1195     when others then
1196       print_debug('Error in Call Plan Wave Cp: ' || SQLCODE || ' : ' ||
1197                   SQLERRM,
1198                   l_debug);
1199   end call_Plan_Wave_CP;
1200 
1201    procedure labor_resource_planning(x_labor_time_tbl labor_time_tbl,
1202                                     tbl_type         number,
1203                                     plan_type        varchar2,
1204                                     plan_id          number,
1205                                     wave_id          number,
1206                                     ORGANIZATION_ID number) is
1207     l_planning_criteria_id NUMBER := plan_id;
1208     l_wave_header_id       NUMBER := wave_id;
1209     l_organization_id      NUMBER;
1210     cap_det                NUMBER;
1211 
1212     ----------  SET UP DETAILS --------------------
1213     TYPE labor_setup_record IS RECORD(
1214       resource_name            VARCHAR2(100),
1215       resource_type            NUMBER,
1216       source                   VARCHAR2(100),
1217       destination              VARCHAR2(100),
1218       unit_of_measure          VARCHAR2(10),
1219       item_category_id         NUMBER,
1220       operation_plan_id        NUMBER,
1221       transaction_time         NUMBER,
1222       travel_time              NUMBER,
1223       outbound_processing_time NUMBER);
1224 
1225     TYPE labor_setup_tbl IS TABLE OF labor_setup_record INDEX BY BINARY_INTEGER;
1226 
1227     x_labor_setup_tbl  labor_setup_tbl; -- Gonna contain entire set up
1228     x_labor_suited_tbl labor_setup_tbl; -- Gonna contain only suited resource detail for the current task
1229 
1230     --x_tasks_tbl labor_time_tbl; -- Gonna contain details of the planned tasks
1231     ----------  TASK DETAILS ----------------------
1232 
1233     ----- cursor to get the details of all the resources from wms_wp_labor_planning table ----
1234 
1235     /*  cursor get the details of resource. First fetch persons (resouce type = 2) last fetch machines (resource type = 1)*/
1236 
1237     CURSOR c_get_labor_details(p_plan_id NUMBER) IS
1238       SELECT wlp.resource_type resource_name,
1239              wlp.source_subinventory,
1240              wlp.destination_subinventory,
1241              wlp.pick_uom pick_uom,
1242              wlp.transaction_time,
1243              wlp.travel_time,
1244              wlp.processing_overhead_duration,
1245              wlp.category_id,
1246              Decode(plan_type, 'A', -1, 'R', wlp.operation_plan_id) operation_plan_id,
1247              br.resource_type resource_type
1248         FROM wms_wp_labor_planning       wlp,
1249              bom_department_resources    bdr,
1250              bom_Resources               br,
1251              wms_wp_planning_Criteria_vl wpl
1252        WHERE wlp.planning_criteria_id = p_plan_id
1253          AND wpl.planning_criteria_id = p_plan_id
1254          AND br.resource_code = wlp.resource_type
1255          AND br.resource_id = bdr.resource_id
1256          AND bdr.department_id = wpl.department_id
1257        ORDER BY br.resource_type DESC;
1258 
1259     --- cursor to get the distinct resources ----
1260 
1261     CURSOR c_get_distinct_resource(p_plan_id NUMBER) IS
1262       SELECT DISTINCT resource_type
1263         FROM wms_wp_labor_planning
1264        WHERE planning_criteria_id = p_plan_id;
1265 
1266     position                 NUMBER := 0;
1267     current_load             NUMBER := 0;
1268     resource_type            NUMBER := 0;
1269     number_of_tasks          NUMBER := 0;
1270     total_capacity           NUMBER := 0;
1271     l_std_oprn_id            VARCHAR2(1) := 'N';
1272     l_item_cat_id            VARCHAR2(1) := 'N';
1273     suit_copy                NUMBER := 0;
1274     l_available_capacity     NUMBER := 0;
1275     l_qty_able_to_do         NUMBER := 0;
1276     l_total_quantity_to_do   NUMBER;
1277     l_total_machine_quantity NUMBER;
1278     l_suit_count             NUMBER;
1279     l_planned_load_task      NUMBER;
1280     l_planned_temp_load      NUMBER;
1281     l_complete_task          NUMBER; -- This will be added to planned tasks. It will become zero after assigned to 1st resource, if
1282     -- the work load is shared among 2 or more resources.
1283     l_debug            NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'),
1284                                      0);
1285     l_complete_machine number;
1286     l_resource_okay    VARCHAR2(1);
1287     l_dept_id          number;
1288     l_utilization      number;
1289     l_efficiency       number;
1290 
1291   BEGIN
1292       print_debug('In ORGANIZATION_ID API '||ORGANIZATION_ID, l_debug);
1293 
1294     /*
1295     1.Take a task from x_labor_time_tbl
1296     2.From x_labor_setup_tbl copy the records into x_labor_suited_tbl which can do the work
1297     3.If no resource is person then task can't be assigned to any one
1298     4.If person is there assign the load to him.
1299     5.If no enough capacity then try for another resource from x_labor_suited_tbl
1300     6.If no resource found then overload the current person.
1301     7.After assigning the work to person, see if machine is available to do and assign same load to it.
1302     */
1303     /* tbl_type = '1' ==> inventory movements table
1304     tbl_type = '2' ==> crossdock movements table
1305     plan_type = 'A' ==> availability check planning
1306     plan_type = 'R' ==> rules based planning*/
1307 
1308     l_organization_id := ORGANIZATION_ID;
1309     begin
1310       print_debug('In labor_resource_planning API', l_debug);
1311       select department_id
1312         into l_dept_id
1313         from wms_wp_planning_criteria_vl
1314        where planning_criteria_id = l_planning_criteria_id;
1315     exception
1316       when no_data_found then
1317         l_dept_id := -1;
1318     end;
1319 
1320     /* labor planning api is going to call this procedure twice;
1321     when it calls for inventory movements only we need to update resource capacity table.
1322     After doing labor resource planning for inventory movements, labor planning api will call this again for xdock movements;
1323     then resource capacity should have old values which are obtained after labor resource planning for inventory movements
1324     This is bcoz labor resource planning should be done for inventory + xdock movements*/
1325 
1326     -- populating x_resource_capacity_tbl --
1327     if tbl_type = 1 then
1328       position := 0;
1329       FOR l_cap_det IN c_get_distinct_resource(l_planning_criteria_id) LOOP
1330         x_resource_capacity_tbl(position).resource_name := l_cap_det.resource_type;
1331         wms_wave_planning_pvt.get_current_work_load(l_cap_det.resource_type,
1332                                                     l_planning_criteria_id,
1333                                                     l_wave_header_id,
1334                                                     current_load,
1335                                                     resource_type,
1336                                                     number_of_tasks,
1337                                                     total_capacity);
1338 
1339         x_resource_capacity_tbl(position).current_load := current_load;
1340         x_resource_capacity_tbl(position).resource_type := resource_type;
1341         x_resource_capacity_tbl(position).actual_tasks := number_of_tasks;
1342         x_resource_capacity_tbl(position).total_capacity := total_capacity;
1343         x_resource_capacity_tbl(position).available_capacity := total_capacity -
1344                                                                 current_load;
1345 
1346         position := position + 1;
1347       END LOOP;
1348     end if;
1349 
1350     -- populating x_labor_setup_tbl --
1351     position := 0;
1352     FOR l_setup IN c_get_labor_details(l_planning_criteria_id) LOOP
1353       --x_labor_setup_tbl --
1354       x_labor_setup_tbl(position).resource_name := l_setup.resource_name;
1355       x_labor_setup_tbl(position).source := l_setup.source_subinventory;
1356       x_labor_setup_tbl(position).destination := l_setup.destination_subinventory;
1357       x_labor_setup_tbl(position).unit_of_measure := l_setup.pick_uom;
1358       x_labor_setup_tbl(position).item_category_id := l_setup.category_id;
1359       x_labor_setup_tbl(position).operation_plan_id := l_setup.operation_plan_id;
1360       print_debug('operation plan id assignment = ' ||
1361                   x_labor_setup_tbl(position).item_category_id,
1362                   l_debug);
1363       x_labor_setup_tbl(position).transaction_time := l_setup.transaction_time;
1364       x_labor_setup_tbl(position).travel_time := l_setup.travel_time;
1365       x_labor_setup_tbl(position).outbound_processing_time := l_setup.processing_overhead_duration;
1366       x_labor_setup_tbl(position).resource_type := l_setup.resource_type;
1367 
1368       position := position + 1;
1369     END LOOP;
1370 
1371     -- load calculation starts --
1372     if x_labor_time_tbl.count > 0 then
1373       FOR task IN x_labor_time_tbl.first .. x_labor_time_tbl.last LOOP
1374         -- loop for every task
1375         print_debug('task is from ' || x_labor_time_tbl(TASK)
1376                     .picking_subinventory || ' to' ||
1377                     x_labor_time_tbl(TASK)
1378                     .destination_subinventory || ' and quantity moved is ' ||
1379                     x_labor_time_tbl(TASK).demand_qty_picking_uom,
1380                     l_debug);
1381         print_debug('From  : ' || x_labor_time_tbl(TASK)
1382                     .picking_subinventory,
1383                     l_debug);
1384         print_debug('To  : ' || x_labor_time_tbl(TASK)
1385                     .destination_subinventory,
1386                     l_debug);
1387         print_debug('UOM  : ' || x_labor_time_tbl(TASK).picking_uom,
1388                     l_debug);
1389         print_debug('qty  : ' || x_labor_time_tbl(TASK)
1390                     .demand_qty_picking_uom,
1391                     l_debug);
1392         print_debug('item_id  : ' || x_labor_time_tbl(TASK)
1393                     .INVENTORY_ITEM_ID,
1394                     l_debug);
1395         print_debug('oprn_id  : ' || x_labor_time_tbl(TASK)
1396                     .operation_plan_id,
1397                     l_debug);
1398         print_debug('std_oprn_id  : ' || x_labor_time_tbl(TASK)
1399                     .standard_operation_id,
1400                     l_debug);
1401 
1402         suit_copy          := 0; -- variable to copy into suited table.
1403         l_complete_task    := 1;
1404         l_complete_machine := 1;
1405         FOR setup IN x_labor_setup_tbl.first .. x_labor_setup_tbl.last LOOP
1406           -- loop the set up table and get the eligible resources to complete the task
1407           l_std_oprn_id := 'N';
1408           l_item_cat_id := 'N';
1409           IF x_labor_setup_tbl(setup).source = x_labor_time_tbl(TASK)
1410           .picking_subinventory AND x_labor_setup_tbl(setup)
1411           .destination = x_labor_time_tbl(TASK)
1412           .destination_subinventory AND x_labor_setup_tbl(setup)
1413           .unit_of_measure = x_labor_time_tbl(TASK)
1414           .picking_uom AND
1415              ((x_labor_setup_tbl(setup)
1416               .operation_plan_id = x_labor_time_tbl(TASK).operation_plan_id) or
1417               (x_labor_setup_tbl(setup)
1418               .source = 'Not Applicable' and x_labor_time_tbl(TASK)
1419               .operation_plan_id = -1) or
1420               (plan_type = 'R' and x_labor_setup_tbl(setup).operation_plan_id is null)) THEN
1421              print_debug('resource1',l_debug);
1422             IF ((x_labor_time_tbl(task)
1423                .standard_operation_id = -1 and plan_type = 'A') or
1424                (x_labor_setup_tbl(setup)
1425                .source = 'Not Applicable' and x_labor_time_tbl(TASK)
1426                .standard_operation_id = -1)) THEN
1427               l_std_oprn_id := 'Y';
1428               print_debug('resource2',l_debug);
1429             ELSE
1430               begin
1431                 SELECT 'Y'
1432                   INTO l_std_oprn_id
1433                   FROM dual
1434                  WHERE x_labor_time_tbl(task)
1435                 .standard_operation_id IN
1436                        (SELECT standard_operation_id
1437                           FROM bom_std_op_resources_v
1438                          WHERE resource_code = x_labor_setup_tbl(setup)
1439                         .resource_name);
1440                  print_debug('resource3',l_debug);
1441               exception
1442                 when no_data_found THEN
1443                   l_std_oprn_id := 'N';
1444               end;
1445             END IF;
1446 
1447             IF l_std_oprn_id = 'Y' THEN
1448               -- This resource is capable of doing the particular task
1449               -- assign value Y to l_item_cat_id, if the setup matches.
1450               BEGIN
1451               print_debug('resource4',l_debug);
1452 
1453                 SELECT 'Y'
1454                   INTO l_item_cat_id
1455                   FROM dual
1456                  WHERE x_labor_setup_tbl(setup)
1457                 .item_category_id is null
1458                     or (x_labor_setup_tbl(setup)
1459                         .item_category_id IN
1460                         (SELECT category_id
1461                            FROM mtl_category_set_valid_cats_v a
1462                           WHERE a.category_id IN
1463                                 (SELECT DISTINCT b.category_id
1464                                    FROM mtl_item_categories b
1465                                   WHERE a.category_id = b.category_id
1466                                     and b.inventory_item_id =
1467                                         x_labor_time_tbl(TASK)
1468                                  .inventory_item_id
1469                                     AND b.organization_id = l_organization_id)));
1470 
1471               exception
1472                 when no_data_found THEN
1473                   l_item_cat_id := 'N';
1474               end;
1475 
1476               IF l_item_cat_id = 'Y' THEN
1477               print_debug('resource5',l_debug);
1478                 x_labor_suited_tbl(suit_copy).resource_name := x_labor_setup_tbl(setup)
1479                                                               .resource_name;
1480                 x_labor_suited_tbl(suit_copy).source := x_labor_setup_tbl(setup)
1481                                                        .source;
1482                 x_labor_suited_tbl(suit_copy).destination := x_labor_setup_tbl(setup)
1483                                                             .destination;
1484                 x_labor_suited_tbl(suit_copy).unit_of_measure := x_labor_setup_tbl(setup)
1485                                                                 .unit_of_measure;
1486                 x_labor_suited_tbl(suit_copy).item_category_id := x_labor_setup_tbl(setup)
1487                                                                  .item_category_id;
1488                 x_labor_suited_tbl(suit_copy).operation_plan_id := x_labor_setup_tbl(setup)
1489                                                                   .operation_plan_id;
1490                 x_labor_suited_tbl(suit_copy).transaction_time := x_labor_setup_tbl(setup)
1491                                                                  .transaction_time;
1492                 x_labor_suited_tbl(suit_copy).travel_time := x_labor_setup_tbl(setup)
1493                                                             .travel_time;
1494                 x_labor_suited_tbl(suit_copy).outbound_processing_time := x_labor_setup_tbl(setup)
1495                                                                          .outbound_processing_time;
1496                 x_labor_suited_tbl(suit_copy).resource_type := x_labor_setup_tbl(setup)
1497                                                               .resource_type;
1498                 suit_copy := suit_copy + 1;
1499               END IF;
1500             END IF;
1501           END IF;
1502         END LOOP;
1503 
1504         l_total_quantity_to_do   := x_labor_time_tbl(TASK)
1505                                    .demand_qty_picking_uom;
1506         l_total_machine_quantity := l_total_quantity_to_do;
1507         l_suit_count             := x_labor_suited_tbl.Count - 1;
1508         l_planned_load_task      := 0;
1509         print_debug('************************ x_labor_suited_tbl.Count ***************************',
1510                     l_debug);
1511         print_debug('x_labor_suited_tbl.Count ' ||
1512                     x_labor_suited_tbl.Count,
1513                     l_debug);
1514         IF x_labor_suited_tbl.Count > 0 THEN
1515           FOR suit IN x_labor_suited_tbl.first .. x_labor_suited_tbl.last LOOP
1516             print_debug('current resource is ' || x_labor_suited_tbl(suit)
1517                         .resource_name,
1518                         l_debug);
1519             print_debug('current resource type is ' ||
1520                         x_labor_suited_tbl(suit).resource_type,
1521                         l_debug);
1522             print_debug('======================================================',
1523                         l_debug);
1524             IF l_total_quantity_to_do > 0 THEN
1525               IF x_labor_suited_tbl(suit).resource_type = 2 THEN
1526                 -- ==> person resource
1527                 -- Find the position in x_resource_capacity_tbl.
1528 
1529                 FOR l_cap_det IN x_resource_capacity_tbl.first .. x_resource_capacity_tbl.last LOOP
1530                   IF x_resource_capacity_tbl(l_cap_det)
1531                   .resource_name = x_labor_suited_tbl(suit).resource_name THEN
1532                     cap_det := l_cap_det;
1533                     EXIT;
1534                   END IF;
1535                 END LOOP;
1536                 print_debug(' quanity = ' || l_total_quantity_to_do,
1537                             l_debug);
1538                 print_debug(' available_capacity = ' ||
1539                             x_resource_capacity_tbl(cap_det)
1540                             .available_capacity,
1541                             l_debug);
1542                 print_debug(' travel_time = ' || x_labor_suited_tbl(suit)
1543                             .travel_time,
1544                             l_debug);
1545                 print_debug(' outbound_processing_time = ' ||
1546                             x_labor_suited_tbl(suit)
1547                             .outbound_processing_time,
1548                             l_debug);
1549                 print_debug(' transaction_time = ' ||
1550                             x_labor_suited_tbl(suit).transaction_time,
1551                             l_debug);
1552 
1553                 select nvl(utilization, 100) / 100,
1554                        nvl(efficiency, 100) / 100
1555                   into l_utilization, l_efficiency
1556                   from bom_department_resources_v bdrv
1557                  where department_id = l_dept_id
1558                    and resource_code = x_resource_capacity_tbl(cap_det)
1559                 .resource_name;
1560 
1561                 l_available_capacity := (x_resource_capacity_tbl(cap_det)
1562                                         .available_capacity * l_efficiency *
1563                                          l_utilization) -
1564                                         (x_labor_suited_tbl(suit)
1565                                         .travel_time +
1566                                          x_labor_suited_tbl(suit)
1567                                         .outbound_processing_time);
1568                 l_qty_able_to_do     := Floor(l_available_capacity /
1569                                               x_labor_suited_tbl(suit)
1570                                               .transaction_time);
1571                 print_debug(' l_qty_able_to_do = ' || l_qty_able_to_do,
1572                             l_debug);
1573 
1574                 IF l_qty_able_to_do >= l_total_quantity_to_do THEN
1575                   -- Here the resource is able to do the task completely so no problem.
1576                   print_debug('Resource can do completely', l_debug);
1577                   x_resource_capacity_tbl(cap_det).planned_tasks := x_resource_capacity_tbl(cap_det)
1578                                                                    .planned_tasks +
1579                                                                     l_complete_task;
1580                   l_complete_task := 0;
1581                   l_planned_temp_load := round((l_total_quantity_to_do *
1582                                          x_labor_suited_tbl(suit)
1583                                          .transaction_time +
1584                                           x_labor_suited_tbl(suit)
1585                                          .travel_time + Nvl(x_labor_suited_tbl(suit)
1586                                                             .outbound_processing_time,
1587                                                             0)) /
1588                                          (l_efficiency * l_utilization));
1589                   x_resource_capacity_tbl(cap_det).planned_load := x_resource_capacity_tbl(cap_det)
1590                                                                   .planned_load +
1591                                                                    l_planned_temp_load;
1592                   l_total_quantity_to_do := 0;
1593                   l_planned_load_task := l_planned_load_task +
1594                                          l_planned_temp_load;
1595                   x_resource_capacity_tbl(cap_det).available_capacity := x_resource_capacity_tbl(cap_det)
1596                                                                         .available_capacity -
1597                                                                          l_planned_temp_load;
1598                   print_debug(' l_planned_temp_load = ' ||
1599                               l_planned_temp_load,
1600                               l_debug);
1601 
1602                   print_debug('resource = '||x_resource_capacity_tbl(cap_det).resource_name,l_debug);
1603                   print_debug('available = '||x_resource_capacity_tbl(cap_det).available_capacity,l_debug);
1604                   print_debug('planned = '||x_resource_capacity_tbl(cap_det).planned_load,l_debug);
1605                 ELSIF l_qty_able_to_do < l_total_quantity_to_do THEN
1606                   -- Here the resource can do only part of the task.
1607                   print_debug('Resource can do partially', l_debug);
1608                   l_resource_okay := 'N';
1609                   IF suit < l_suit_count THEN
1610                     IF x_labor_suited_tbl(suit + 1).resource_type = 2 THEN
1611                       l_resource_okay := 'Y';
1612                       print_debug('l_resource_okay = ' || l_resource_okay,
1613                                   l_debug);
1614                     ELSE
1615                       l_resource_okay := 'N';
1616                       print_debug('l_resource_okay = ' || l_resource_okay,
1617                                   l_debug);
1618                     END IF;
1619                   END IF;
1620 
1621                   IF l_resource_okay = 'Y' THEN
1622                     IF x_resource_capacity_tbl(cap_det)
1623                     .available_capacity > 0 THEN
1624                       x_resource_capacity_tbl(cap_det).planned_tasks := x_resource_capacity_tbl(cap_det)
1625                                                                        .planned_tasks +
1626                                                                         l_complete_task;
1627                       l_complete_task := 0;
1628                       IF l_qty_able_to_do > 0 THEN
1629                         l_planned_temp_load := round((l_qty_able_to_do *
1630                                                x_labor_suited_tbl(suit)
1631                                                .transaction_time +
1632                                                 x_labor_suited_tbl(suit)
1633                                                .travel_time +
1634                                                 Nvl(x_labor_suited_tbl(suit)
1635                                                     .outbound_processing_time,
1636                                                     0)) / (l_efficiency *
1637                                                l_utilization));
1638                         x_resource_capacity_tbl(cap_det).planned_load := x_resource_capacity_tbl(cap_det)
1639                                                                         .planned_load +
1640                                                                          l_planned_temp_load;
1641                         l_total_quantity_to_do := l_total_quantity_to_do -
1642                                                   l_qty_able_to_do;
1643                         l_planned_load_task := l_planned_load_task +
1644                                                l_planned_temp_load;
1645                         x_resource_capacity_tbl(cap_det).available_capacity := x_resource_capacity_tbl(cap_det)
1646                                                                               .available_capacity -
1647                                                                               l_planned_temp_load;
1648                         print_debug(' l_planned_temp_load = ' ||
1649                                     l_planned_temp_load,
1650                                     l_debug);
1651                   print_debug('resource = '||x_resource_capacity_tbl(cap_det).resource_name,l_debug);
1652                   print_debug('available = '||x_resource_capacity_tbl(cap_det).available_capacity,l_debug);
1653                   print_debug('planned = '||x_resource_capacity_tbl(cap_det).planned_load,l_debug);
1654 
1655                       ELSE
1656                         print_debug('Resource cannot handle even 1 qty',
1657                                     l_debug);
1658                       END IF;
1659                     END IF;
1660                   ELSE
1661                     -- There is no other person resource who can do this task. So assign the load to this guy only.
1662                     print_debug('Resource should do every thing', l_debug);
1663                     x_resource_capacity_tbl(cap_det).planned_tasks := x_resource_capacity_tbl(cap_det)
1664                                                                      .planned_tasks +
1665                                                                       l_complete_task;
1666                     l_complete_task := 0;
1667                     l_planned_temp_load := round((l_total_quantity_to_do *
1668                                            x_labor_suited_tbl(suit)
1669                                            .transaction_time +
1670                                             x_labor_suited_tbl(suit)
1671                                            .travel_time +
1672                                             Nvl(x_labor_suited_tbl(suit)
1673                                                 .outbound_processing_time,
1674                                                 0)) /
1675                                            (l_efficiency * l_utilization));
1676                     x_resource_capacity_tbl(cap_det).planned_load := x_resource_capacity_tbl(cap_det)
1677                                                                     .planned_load +
1678                                                                      l_planned_temp_load;
1679                     l_total_quantity_to_do := 0;
1680                     l_planned_load_task := l_planned_load_task +
1681                                            l_planned_temp_load;
1682                     x_resource_capacity_tbl(cap_det).available_capacity := x_resource_capacity_tbl(cap_det)
1683                                                                           .available_capacity -
1684                                                                            l_planned_temp_load;
1685                     print_debug(' l_planned_temp_load = ' ||
1686                                 l_planned_temp_load,
1687                                 l_debug);
1688                   print_debug('resource = '||x_resource_capacity_tbl(cap_det).resource_name,l_debug);
1689                   print_debug('available = '||x_resource_capacity_tbl(cap_det).available_capacity,l_debug);
1690                   print_debug('planned = '||x_resource_capacity_tbl(cap_det).planned_load,l_debug);
1691 
1692                   END IF;
1693 
1694                 END IF;
1695               END IF;
1696             END IF;
1697 
1698             IF l_total_quantity_to_do = 0 AND l_planned_load_task > 0 THEN
1699               cap_det := -1;
1700               print_debug('in machine calculation', l_debug);
1701               -- now assign the total load to the machinery.
1702               IF x_labor_suited_tbl(suit).resource_type = 1 THEN
1703                 print_debug('machine is ' || x_labor_suited_tbl(suit)
1704                             .resource_name,
1705                             l_debug);
1706                 FOR l_cap_det IN x_resource_capacity_tbl.first .. x_resource_capacity_tbl.last LOOP
1707                   IF x_resource_capacity_tbl(l_cap_det)
1708                   .resource_name = x_labor_suited_tbl(suit).resource_name THEN
1709                     cap_det := l_cap_det;
1710                     EXIT;
1711                   END IF;
1712                 END LOOP;
1713 
1714                 IF x_resource_capacity_tbl(cap_det)
1715                 .available_capacity > l_planned_load_task THEN
1716                   x_resource_capacity_tbl(cap_det).planned_load := x_resource_capacity_tbl(cap_det)
1717                                                                   .planned_load +
1718                                                                    l_planned_load_task;
1719                   x_resource_capacity_tbl(cap_det).planned_tasks := x_resource_capacity_tbl(cap_det)
1720                                                                    .planned_tasks +
1721                                                                     l_complete_machine;
1722                   x_resource_capacity_tbl(cap_det).available_capacity := x_resource_capacity_tbl(cap_det)
1723                                                                         .available_capacity -
1724                                                                          l_planned_load_task;
1725                   l_planned_load_task := 0;
1726                   l_complete_machine := 0;
1727                 ELSIF x_resource_capacity_tbl(cap_det)
1728                 .available_capacity < l_planned_load_task THEN
1729                   IF l_suit_count > suit THEN
1730                     print_debug('more machine available', l_debug);
1731                     IF x_resource_capacity_tbl(cap_det)
1732                     .available_capacity > 0 THEN
1733                       x_resource_capacity_tbl(cap_det).planned_load := x_resource_capacity_tbl(cap_det)
1734                                                                       .planned_load +
1735                                                                        x_resource_capacity_tbl(cap_det)
1736                                                                       .available_capacity;
1737                       l_planned_load_task := l_planned_load_task -
1738                                              x_resource_capacity_tbl(cap_det)
1739                                             .available_capacity;
1740                       x_resource_capacity_tbl(cap_det).available_capacity := 0;
1741                       x_resource_capacity_tbl(cap_det).planned_tasks := x_resource_capacity_tbl(cap_det)
1742                                                                        .planned_tasks +
1743                                                                         l_complete_machine;
1744                       l_complete_machine := 0;
1745                     END IF;
1746                   ELSE
1747                     print_debug('final machine', l_debug);
1748                     print_debug('x_resource_capacity_tbl(cap_det).resource_name = ' ||
1749                                 x_resource_capacity_tbl(cap_det)
1750                                 .resource_name,
1751                                 l_debug);
1752                     x_resource_capacity_tbl(cap_det).planned_load := x_resource_capacity_tbl(cap_det)
1753                                                                     .planned_load +
1754                                                                      l_planned_load_task;
1755                     x_resource_capacity_tbl(cap_det).available_capacity := x_resource_capacity_tbl(cap_det)
1756                                                                           .available_capacity -
1757                                                                            l_planned_load_task;
1758                     print_debug('x_resource_capacity_tbl(cap_det).planned_load = ' ||
1759                                 x_resource_capacity_tbl(cap_det)
1760                                 .planned_load,
1761                                 l_debug);
1762                     l_planned_load_task := 0;
1763                     x_resource_capacity_tbl(cap_det).planned_tasks := x_resource_capacity_tbl(cap_det)
1764                                                                      .planned_tasks +
1765                                                                       l_complete_machine;
1766                     l_complete_machine := 0;
1767                   END IF;
1768                 END IF;
1769               END IF;
1770             END IF;
1771 
1772           END LOOP;
1773 
1774         END IF;
1775 
1776       END LOOP;
1777     end if;
1778 
1779     for m2 in x_resource_capacity_tbl.FIRST .. x_resource_capacity_tbl.LAST LOOP
1780 
1781       print_debug('************************ Resource Details after planning, ***************************',
1782                   l_debug);
1783       print_debug('x_resource_capacity_tbl(m2).resource_name ' ||
1784                   x_resource_capacity_tbl(m2).resource_name,
1785                   l_debug);
1786       print_debug('x_resource_capacity_tbl(m2).resource_type ' ||
1787                   x_resource_capacity_tbl(m2).resource_type,
1788                   l_debug);
1789       print_debug('x_resource_capacity_tbl(m2).total_capacity ' ||
1790                   x_resource_capacity_tbl(m2).total_capacity,
1791                   l_debug);
1792       print_debug('x_resource_capacity_tbl(m2).available_capacity ' ||
1793                   x_resource_capacity_tbl(m2).available_capacity,
1794                   l_debug);
1795       print_debug('x_resource_capacity_tbl(m2).planned_load ' ||
1796                   x_resource_capacity_tbl(m2).planned_load,
1797                   l_debug);
1798       print_debug('x_resource_capacity_tbl(m2).current_load ' ||
1799                   x_resource_capacity_tbl(m2).current_load,
1800                   l_debug);
1801       print_debug('x_resource_capacity_tbl(m2).planned_tasks ' ||
1802                   x_resource_capacity_tbl(m2).planned_tasks,
1803                   l_debug);
1804       print_debug('x_resource_capacity_tbl(m2).actual_tasks ' ||
1805                   x_resource_capacity_tbl(m2).actual_tasks,
1806                   l_debug);
1807 
1808     end loop;
1809 
1810   end;
1811 
1812 
1813 
1814 
1815   procedure rules_labor_planning(p_planning_criteria_id in number,
1816                                p_wave_header_id       in number,
1817                                p_move_order_hdr_id    in number,
1818                                p_org_id number,
1819                                x_return_status        out nocopy varchar2)
1820 
1821  is
1822 
1823   --  p_move_order_header_id number := 3013761;
1824 
1825   l_labor_setup_mode varchar2(1);
1826   l_tbl_count number;
1827   l_tbl_start number;
1828 
1829   cursor c_parent_mmtts_zone is
1830     SELECT DISTINCT mmtt1.transaction_temp_id,
1831                     mmtt1.standard_operation_id,
1832                     mmtt1.parent_line_id,
1833                     mmtt1.transaction_quantity,
1834                     wz1.zone_name from_zone,
1835                     Decode(mmtt1.transfer_to_location,
1836                            NULL,
1837                            NULL,
1838                            wz2.zone_name) to_zone,
1839                     mmtt1.transaction_uom,
1840                     mmtt1.operation_plan_id,
1841                     mmtt1.inventory_item_id
1842       FROM mtl_material_transactions_temp mmtt1,
1843            mtl_material_transactions_temp mmtt2,
1844            wms_zone_locators              wzl1,
1845            wms_zone_locators              wzl2,
1846            wms_zones_vl                   wz1,
1847            wms_zones_vl                   wz2
1848      WHERE mmtt2.move_order_header_id = p_move_order_hdr_id
1849        and mmtt1.locator_id = wzl1.inventory_location_id
1850        and (mmtt1.transfer_to_location = wzl2.inventory_location_id OR
1851            mmtt1.transfer_to_location IS NULL)
1852        and mmtt1.organization_id = wzl1.organization_id
1853        and mmtt1.organization_id = wzl2.organization_id
1854        and wz1.zone_id = wzl1.zone_id
1855        and wz2.zone_id = wzl2.zone_id
1856        and wz1.zone_type = 'L'
1857        and wz2.zone_type = 'L'
1858        AND (mmtt1.transaction_temp_id = mmtt2.transaction_temp_id OR
1859            mmtt1.transaction_temp_id = mmtt2.parent_line_id)
1860        AND (mmtt2.parent_line_id IS NULL OR
1861            mmtt1.transaction_temp_id = mmtt1.parent_line_id);
1862 
1863   p_parent_line_id        number;
1864   p_standard_operation_id number;
1865   p_transaction_qty       number;
1866 
1867   cursor c_child_mmtts_zone IS
1868     select distinct transaction_temp_id,
1869                     wz1.zone_name from_zone,
1870                     wz2.zone_name to_zone,
1871                     transaction_uom,
1872                     operation_plan_id,
1873                     inventory_item_id,
1874                     dropping_order,
1875                     transaction_quantity
1876       from mtl_material_transactions_temp mmtt,
1877            MTL_SECONDARY_INVENTORIES      MSI,
1878            wms_zone_locators              wzl1,
1879            wms_zone_locators              wzl2,
1880            wms_zones_vl                   wz1,
1881            wms_zones_vl                   wz2
1882      where move_order_header_id = p_move_order_hdr_id
1883           --AND mmtt.transfer_subinventory =msi.SECONDARY_INVENTORY_NAME
1884        AND mmtt.organization_id = msi.organization_id
1885        and mmtt.locator_id = wzl1.inventory_location_id
1886        and mmtt.transfer_to_location = wzl2.inventory_location_id
1887        and mmtt.organization_id = wzl1.organization_id
1888        and mmtt.organization_id = wzl2.organization_id
1889        and wz1.zone_id = wzl1.zone_id
1890        and wz2.zone_id = wzl2.zone_id
1891        and wz1.zone_type = 'L'
1892        and wz2.zone_type = 'L'
1893        and wzl2.subinventory_code = msi.SECONDARY_INVENTORY_NAME
1894        and parent_line_id = p_parent_line_id
1895      order by dropping_order;
1896 
1897   cursor c_parent_mmtts is
1898     SELECT DISTINCT mmtt1.transaction_temp_id,
1899                     mmtt1.standard_operation_id,
1900                     mmtt1.parent_line_id,
1901                     mmtt1.transaction_quantity,
1902                     mmtt1.subinventory_code,
1903                     mmtt1.transfer_subinventory,
1904                     mmtt1.transaction_uom,
1905                     mmtt1.operation_plan_id,
1906                     mmtt1.inventory_item_id
1907       FROM mtl_material_transactions_temp mmtt1,
1908            mtl_material_transactions_temp mmtt2
1909      WHERE mmtt2.move_order_header_id = p_move_order_hdr_id
1910        AND (mmtt1.transaction_temp_id = mmtt2.transaction_temp_id OR
1911            mmtt1.transaction_temp_id = mmtt2.parent_line_id)
1912        AND (mmtt2.parent_line_id IS NULL OR
1913            mmtt1.transaction_temp_id = mmtt1.parent_line_id);
1914 
1915   cursor c_child_mmtts IS
1916     select distinct transaction_temp_id,
1917                     subinventory_code,
1918                     transfer_subinventory,
1919                     transaction_uom,
1920                     operation_plan_id,
1921                     inventory_item_id,
1922                     dropping_order,
1923                     transaction_quantity
1924       from mtl_material_transactions_temp mmtt,
1925            MTL_SECONDARY_INVENTORIES      MSI
1926      where move_order_header_id = p_move_order_hdr_id
1927        AND mmtt.transfer_subinventory = msi.SECONDARY_INVENTORY_NAME
1928        AND mmtt.organization_id = msi.organization_id
1929        and parent_line_id = p_parent_line_id
1930      order by dropping_order;
1931 
1932   n1         number := 0;
1933   prev_index number;
1934   j          number := 0;
1935   --i number := 1;
1936   l_dest_subinventory varchar2(30);
1937   l_allocation_method varchar2(1);
1938 
1939   x_labor_time_tbl           labor_time_tbl;
1940   c_labor_time_tbl           labor_time_tbl;
1941   l_debug                    NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'),
1942                                            0);
1943   v_destination_subinventory VARCHAR2(30);
1944   v_zone_name varchar2(100);
1945 begin
1946 
1947   select labor_setup_mode, allocation_method, destination_subinventory
1948     into l_labor_setup_mode,
1949          l_allocation_method,
1950          v_destination_subinventory
1951     from wms_wp_planning_criteria_vl
1952    where PLANNING_CRITERIA_ID = p_planning_criteria_id;
1953 
1954 if v_destination_subinventory is null THEN
1955 
1956  SELECT staging_subinventory
1957     INTO   v_destination_subinventory
1958     FROM wms_wp_wave_headers_vl
1959    WHERE wave_header_id = p_wave_header_id;
1960 
1961  end if;
1962 
1963   if l_allocation_method in ('N','I','X') and p_move_order_hdr_id <> -1then
1964 
1965    if l_labor_setup_mode = 'S' then
1966 
1967     for l_parent in c_parent_mmtts loop
1968 
1969       p_parent_line_id        := l_parent.parent_line_id;
1970       p_standard_operation_id := l_parent.standard_operation_id;
1971       p_transaction_qty       := l_parent.transaction_quantity;
1972 
1973       if p_parent_line_id is not null then
1974 
1975         j := 0;
1976         for l_child in c_child_mmtts loop
1977 
1978           if j > 0 then
1979             -- From the second time onwards
1980 
1981             if l_dest_subinventory = l_child.transfer_subinventory then
1982 
1983               x_labor_time_tbl(prev_index).picking_subinventory := l_child.subinventory_code;
1984               x_labor_time_tbl(prev_index).destination_SUBINVENTORY := l_child.transfer_subinventory;
1985               x_labor_time_tbl(prev_index).demand_qty_picking_uom := x_labor_time_tbl(prev_index)
1986                                                                     .demand_qty_picking_uom +
1987                                                                      l_child.transaction_quantity;
1988 
1989               prev_index := n1;
1990 
1991             else
1992 
1993               n1 := n1 + 1;
1994 
1995               x_labor_time_tbl(n1).picking_subinventory := l_dest_subinventory;
1996               x_labor_time_tbl(n1).destination_SUBINVENTORY := l_child.transfer_subinventory;
1997               x_labor_time_tbl(n1).picking_uom := l_child.transaction_uom;
1998               x_labor_time_tbl(n1).demand_qty_picking_uom := l_child.transaction_quantity;
1999               x_labor_time_tbl(n1).operation_plan_id := l_child.operation_plan_id;
2000               x_labor_time_tbl(n1).inventory_item_id := l_child.inventory_item_id;
2001               x_labor_time_tbl(n1).standard_operation_id := p_standard_operation_id;
2002 
2003               prev_index := n1;
2004 
2005             end if;
2006 
2007           else
2008 
2009             n1 := n1 + 1;
2010 
2011             x_labor_time_tbl(n1).picking_subinventory := l_child.subinventory_code;
2012             x_labor_time_tbl(n1).destination_SUBINVENTORY := l_child.transfer_subinventory;
2013             x_labor_time_tbl(n1).picking_uom := l_child.transaction_uom;
2014             x_labor_time_tbl(n1).demand_qty_picking_uom := l_child.transaction_quantity;
2015             x_labor_time_tbl(n1).operation_plan_id := l_child.operation_plan_id;
2016             x_labor_time_tbl(n1).inventory_item_id := l_child.inventory_item_id;
2017             x_labor_time_tbl(n1).standard_operation_id := p_standard_operation_id;
2018 
2019             prev_index := n1;
2020             --p_transaction_qty := p_transaction_qty - l_child.transaction_quantity;
2021 
2022           end if;
2023 
2024           l_dest_subinventory := l_child.transfer_subinventory;
2025 
2026           j := j + 1;
2027 
2028         end loop;
2029 
2030       else
2031 
2032         n1 := n1 + 1;
2033 
2034         x_labor_time_tbl(n1).picking_subinventory := l_parent.subinventory_code;
2035         x_labor_time_tbl(n1).destination_SUBINVENTORY := l_parent.transfer_subinventory;
2036         x_labor_time_tbl(n1).picking_uom := l_parent.transaction_uom;
2037         x_labor_time_tbl(n1).demand_qty_picking_uom := l_parent.transaction_quantity;
2038         x_labor_time_tbl(n1).operation_plan_id := l_parent.operation_plan_id;
2039         x_labor_time_tbl(n1).inventory_item_id := l_parent.inventory_item_id;
2040         x_labor_time_tbl(n1).standard_operation_id := p_standard_operation_id;
2041 
2042         prev_index := n1;
2043 
2044       end if;
2045 
2046     end loop;
2047 
2048   else
2049 
2050     for l_parent in c_parent_mmtts_zone loop
2051 
2052       p_parent_line_id        := l_parent.parent_line_id;
2053       p_standard_operation_id := l_parent.standard_operation_id;
2054       p_transaction_qty       := l_parent.transaction_quantity;
2055 
2056       if p_parent_line_id is not null then
2057 
2058         j := 0;
2059         for l_child in c_child_mmtts_zone loop
2060 
2061           if j > 0 then
2062             -- From the second time onwards
2063 
2064             if l_dest_subinventory = l_child.to_zone then
2065 
2066               x_labor_time_tbl(prev_index).picking_subinventory := l_child.from_zone;
2067               x_labor_time_tbl(prev_index).destination_SUBINVENTORY := l_child.to_zone;
2068               x_labor_time_tbl(prev_index).demand_qty_picking_uom := x_labor_time_tbl(prev_index)
2069                                                                     .demand_qty_picking_uom +
2070                                                                      l_child.transaction_quantity;
2071 
2072               prev_index := n1;
2073 
2074             else
2075 
2076               n1 := n1 + 1;
2077 
2078               x_labor_time_tbl(n1).picking_subinventory := l_dest_subinventory;
2079               x_labor_time_tbl(n1).destination_SUBINVENTORY := l_child.to_zone;
2080               x_labor_time_tbl(n1).picking_uom := l_child.transaction_uom;
2081               x_labor_time_tbl(n1).demand_qty_picking_uom := l_child.transaction_quantity;
2082               x_labor_time_tbl(n1).operation_plan_id := l_child.operation_plan_id;
2083               x_labor_time_tbl(n1).inventory_item_id := l_child.inventory_item_id;
2084               x_labor_time_tbl(n1).standard_operation_id := p_standard_operation_id;
2085 
2086               prev_index := n1;
2087 
2088             end if;
2089 
2090           else
2091 
2092             n1 := n1 + 1;
2093 
2094             x_labor_time_tbl(n1).picking_subinventory := l_child.from_zone;
2095             x_labor_time_tbl(n1).destination_SUBINVENTORY := l_child.to_zone;
2096             x_labor_time_tbl(n1).picking_uom := l_child.transaction_uom;
2097             x_labor_time_tbl(n1).demand_qty_picking_uom := l_child.transaction_quantity;
2098             x_labor_time_tbl(n1).operation_plan_id := l_child.operation_plan_id;
2099             x_labor_time_tbl(n1).inventory_item_id := l_child.inventory_item_id;
2100             x_labor_time_tbl(n1).standard_operation_id := p_standard_operation_id;
2101 
2102             prev_index := n1;
2103             --p_transaction_qty := p_transaction_qty - l_child.transaction_quantity;
2104 
2105           end if;
2106 
2107           l_dest_subinventory := l_child.to_zone;
2108 
2109           j := j + 1;
2110 
2111         end loop;
2112 
2113       else
2114 
2115         n1 := n1 + 1;
2116 
2117         x_labor_time_tbl(n1).picking_subinventory := l_parent.from_zone;
2118         x_labor_time_tbl(n1).destination_SUBINVENTORY := l_parent.to_zone;
2119         x_labor_time_tbl(n1).picking_uom := l_parent.transaction_uom;
2120         x_labor_time_tbl(n1).demand_qty_picking_uom := l_parent.transaction_quantity;
2121         x_labor_time_tbl(n1).operation_plan_id := l_parent.operation_plan_id;
2122         x_labor_time_tbl(n1).inventory_item_id := l_parent.inventory_item_id;
2123         x_labor_time_tbl(n1).standard_operation_id := p_standard_operation_id;
2124 
2125         prev_index := n1;
2126 
2127       end if;
2128 
2129     end loop;
2130 
2131   end if;
2132 
2133   if x_labor_time_tbl.count > 0 then
2134 
2135     for i IN x_labor_time_tbl.FIRST .. x_labor_time_tbl.LAST loop
2136 
2137       print_debug('x_labor_time_tbl(i).source ' || x_labor_time_tbl(i)
2138                   .picking_subinventory,
2139                   l_debug);
2140       print_debug('x_labor_time_tbl(i).destination ' ||
2141                   x_labor_time_tbl(i).destination_SUBINVENTORY,
2142                   l_debug);
2143       print_debug('x_labor_time_tbl(i).unit_of_measure ' ||
2144                   x_labor_time_tbl(i).picking_uom,
2145                   l_debug);
2146       print_debug('x_labor_time_tbl(i).demand_qty_picking_uom ' ||
2147                   x_labor_time_tbl(i).demand_qty_picking_uom,
2148                   l_debug);
2149       print_debug('x_labor_time_tbl(i).operation_plan_id ' ||
2150                   x_labor_time_tbl(i).operation_plan_id,
2151                   l_debug);
2152       print_debug('x_labor_time_tbl(i).inventory_item_id ' ||
2153                   x_labor_time_tbl(i).inventory_item_id,
2154                   l_debug);
2155       print_debug('x_labor_time_tbl(i).standard_operation_id ' ||
2156                   x_labor_time_tbl(i).standard_operation_id,
2157                   l_debug);
2158 
2159     end loop;
2160 
2161   end if;
2162 
2163   labor_resource_planning(x_labor_time_tbl,
2164                           1,
2165                           'R',
2166                           p_planning_criteria_id,
2167                           p_wave_header_id,
2168                           p_org_id);
2169 
2170 end if;
2171 
2172 if l_allocation_method in('C', 'N', 'X') and p_move_order_hdr_id = -1 then
2173 
2174 print_debug('Getting the crossdock movements --- >  ', l_debug);
2175 
2176 print_debug('Bulk Labor Planning Enabled ', l_debug);
2177 
2178  if l_labor_setup_mode = 'S' then
2179 SELECT 'Not Applicable',v_destination_subinventory,
2180  Min(wwl.crossdock_uom),
2181  Sum(wwl.crossdock_quantity),
2182   -1, wdd.inventory_item_id,
2183   -1 bulk collect into
2184   c_labor_time_tbl
2185   FROM wms_wp_wave_lines wwl, wsh_delivery_details wdd --11775489 get from wdd...FIXED
2186   WHERE wwl.wave_header_id = p_wave_header_id
2187     and wwl.organization_id =  wdd.organization_id
2188 	and wwl.delivery_detail_id = wdd.delivery_detail_id
2189   GROUP BY wdd.inventory_item_id
2190    ORDER BY wdd.inventory_item_id;
2191 
2192 else  --Zone
2193 
2194 
2195 	SELECT distinct zone_name into v_zone_name from wms_zones_vl wz, wms_zone_locators wzl WHERE
2196 	wz.zone_id=wzl.zone_id
2197 	and wz.zone_type ='L'
2198 	and wzl.subinventory_code=v_destination_subinventory;
2199 
2200 	SELECT 'Not Applicable', v_zone_name,
2201  Min(wwl.crossdock_uom),
2202  Sum(wwl.crossdock_quantity),
2203   -1, wdd.inventory_item_id,
2204   -1 bulk collect into
2205   c_labor_time_tbl
2206   FROM wms_wp_wave_lines wwl, wsh_delivery_details wdd --11775489 get from wdd...FIXED
2207   WHERE wwl.wave_header_id = p_wave_header_id
2208     and wwl.delivery_detail_id = wdd.delivery_detail_id
2209 	and wwl.organization_id = wdd.organization_id
2210   GROUP BY wdd.inventory_item_id
2211    ORDER BY wdd.inventory_item_id;
2212 
2213 
2214 end if;
2215 
2216 if l_allocation_method = 'C' then labor_resource_planning(c_labor_time_tbl,
2217                                                           1,
2218                                                           'R',
2219                                                           p_planning_criteria_id,
2220                                                           p_wave_header_id,
2221                                                           p_org_id);
2222 
2223 elsif l_allocation_method in('N', 'X') then
2224 
2225 labor_resource_planning(c_labor_time_tbl,
2226                         2,
2227                         'R',
2228                         p_planning_criteria_id,
2229                         p_wave_header_id,
2230                         p_org_id);
2231 
2232 end if;
2233 
2234 end if;
2235 
2236 
2237  DELETE FROM wms_wp_labor_Statistics
2238    WHERE wave_header_id = p_wave_header_id;
2239 
2240 l_tbl_count := x_resource_capacity_tbl.count;
2241 
2242   if x_resource_capacity_tbl.count > 0 then
2243    --forall i in 0 .. l_tbl_count     -- dbchange1
2244    for i in x_resource_capacity_tbl.first .. x_resource_capacity_tbl.last loop
2245       insert into wms_wp_labor_statistics
2246         (wave_header_id,
2247          resource_name,
2248          planned_wave_load,
2249          total_capacity,
2250          actual_workload,
2251          available_capacity,
2252          NUMBER_OF_ACTUAL_TASKS,
2253          NUMBER_OF_PLANNED_TASKS)
2254       values
2255         (p_wave_header_id,
2256          x_resource_capacity_tbl(i).resource_name,
2257          x_resource_capacity_tbl(i).planned_load,
2258          x_resource_capacity_tbl(i).total_Capacity,
2259          x_resource_capacity_tbl(i).current_load,
2260          x_resource_capacity_tbl(i).available_capacity,
2261          x_resource_capacity_tbl(i).actual_tasks,
2262          x_resource_capacity_tbl(i).planned_tasks);
2263          end loop;
2264   end if;
2265 
2266   -- x_labor_stats_tbl.delete;
2267  -- x_labor_time_tbl.delete;
2268  -- c_labor_time_tbl.delete;
2269 --  x_resource_capacity_tbl.delete;
2270   -- x_labor_dtl_tbl.delete;
2271   -- x_machine_dtl_tbl.delete;
2272   --  x_labor_stats_tbl_tmp.delete;
2273 
2274   commit;
2275   x_return_status := 'S';
2276 
2277   exception
2278   when others then
2279 
2280       print_debug('Rules Labor Planning : ' || SQLCODE || ' : ' ||
2281                   SQLERRM,
2282                   l_debug);
2283    x_return_status := 'E';
2284 end rules_labor_planning;
2285 
2286 
2287   procedure call_planned_crossdock(p_wave_header_id       in number,
2288                                    p_planning_criteria_id in number)
2289 
2290    is
2291     l_api_version_number NUMBER := 1.0;
2292     l_commit             VARCHAR2(1) := FND_API.G_FALSE;
2293 
2294     -- X-dock, declare package level global variables
2295     g_allocation_method  WSH_PICKING_BATCHES.ALLOCATION_METHOD%TYPE;
2296     g_xdock_delivery_ids WSH_UTIL_CORE.Id_Tab_Type; -- used for X-dock only
2297     g_xdock_detail_ids   WSH_PICK_LIST.DelDetTabTyp; -- used for X-dock only
2298 
2299     --xdock wsh_pr_criteria.relRecTabTyp;
2300 
2301     xdock           WSH_PR_CRITERIA.relRecTabTyp;
2302     m3              number := 0;
2303     l_return_status VARCHAR2(1);
2304     l_msg_data      VARCHAR2(120);
2305     l_msg_count     NUMBER;
2306   l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
2307     cursor c_crossdock_demand_lines is
2308       select wdd.source_header_number,
2309              wdd.source_line_id,
2310              wdd.customer_id,
2311              wdd.source_header_id,
2312              wwl.delivery_detail_id,
2313              wdd.organization_id,
2314              wdd.inventory_item_id,
2315              (wdd.requested_quantity - nvl(wwr.allocated_quantity, 0)) requested_quantity,
2316              wdd.requested_quantity_uom,
2317              inv_salesorder.get_salesorder_for_oeheader(wdd.source_header_id) demand_source_header_id
2318         from wms_wp_wave_headers_vl       wwh,
2319              wms_wp_wave_lines            wwl, --11775489 get from wdd... FIXED
2320              wms_wp_planning_criteria_vl  wwp,
2321              wsh_delivery_details         wdd,
2322              wms_wp_rules_simulation wwr
2323        where wwh.wave_header_id = p_wave_header_id
2324          and wwp.planning_criteria_id = p_planning_criteria_id
2325          and wwh.planning_criteria_id = wwp.planning_criteria_id
2326          and wwh.wave_header_id = wwl.wave_header_id
2327          and wwr.delivery_detail_id = wwl.delivery_detail_id
2328          and wwr.wave_header_id=wwl.wave_header_id
2329          and wwl.delivery_detail_id = wdd.delivery_Detail_id
2330 		 and wdd.organization_id = wwl.organization_id
2331        and wdd.released_status in ('R','B'); --- For Hot Order Changes
2332          --- For Hot Order Changes;;
2333 
2334   begin
2335 
2336     g_planning_criteria_id := p_planning_criteria_id;
2337     for l_crossdock in c_crossdock_demand_lines loop
2338 
2339 if  l_crossdock.requested_quantity >0 then
2340  xdock(m3).requested_quantity := l_crossdock.requested_quantity;
2341 print_debug('In Crossdock Requested Quantity is '||l_crossdock.requested_quantity,l_debug);
2342 
2343       xdock(m3).source_line_id := l_crossdock.source_line_id;
2344       xdock(m3).source_header_id := l_crossdock.source_header_id;
2345       xdock(m3).organization_id := l_crossdock.organization_id;
2346       xdock(m3).inventory_item_id := l_crossdock.inventory_item_id;
2347       xdock(m3).delivery_detail_id := l_crossdock.delivery_detail_id;
2348 
2349 
2350       xdock(m3).demand_source_header_id := l_crossdock.demand_source_header_id;
2351       xdock(m3).source_header_number := l_crossdock.source_header_number;
2352       -- xdock(m3).line_number :=l_crossdock.line_number;
2353       xdock(m3).customer_id := l_crossdock.customer_id;
2354       xdock(m3).requested_quantity_uom := l_crossdock.requested_quantity_uom;
2355       m3 := m3 + 1;
2356     end if;
2357     end loop;
2358 
2359 
2360     WMS_Xdock_Pegging_Pub.Planned_Cross_Dock(p_api_version         => l_api_version_number,
2361                                              p_init_msg_list       => fnd_api.g_false,
2362                                              p_commit              => l_commit,
2363                                              p_batch_id            => null,
2364                                              p_wsh_release_table   => xdock,
2365                                              p_trolin_delivery_ids => g_xdock_delivery_ids,
2366                                              p_del_detail_id       => g_xdock_detail_ids,
2367                                              x_return_status       => l_return_status,
2368                                              x_msg_count           => l_msg_count,
2369                                              x_msg_data            => l_msg_data,
2370                                              p_simulation_mode     => 'Y');
2371 
2372   end call_planned_crossdock;
2373 
2374 
2375   PROCEDURE initialize(p_wave_id NUMBER, p_organization_id NUMBER, x_return_status out nocopy varchar2) IS
2376 
2377 
2378     l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
2379 
2380     CURSOR fetch_wdd(p_wave_id number) is
2381       select wdd.delivery_detail_id,
2382              wdd.inventory_item_id,
2383              subinventory,
2384              locator_id,
2385              lot_number,
2386              revision,
2387              wdd.source_header_id,
2388              wwr.requested_quantity - Nvl(wwr.crossdocked_quantity, 0),
2389              wdd.source_line_id,
2390              wdd.REQUESTED_QUANTITY_UOM,
2391              SRC_REQUESTED_QUANTITY,
2392              SRC_REQUESTED_QUANTITY_UOM,
2393              wwr.requested_quantity2 - Nvl(wwr.crossdocked_quantity2, 0),
2394              wdd.requested_quantity_uom2,
2395              ship_set_id,
2396              SHIP_MODEL_COMPLETE_FLAG,
2397              top_model_line_id,
2398              date_scheduled,
2399              project_id,
2400              task_id,
2401              unit_NUMBER,
2402              preferred_grade,
2403              NULL,
2404              NULL
2405         FROM wsh_delivery_details         wdd,
2406              wms_wp_wave_lines            wwl, --11775489 added org_id join...FIXED
2407              wms_wp_rules_simulation wwr
2408        WHERE wdd.released_status in ('R', 'B')
2409          AND wwr.delivery_detail_id = wdd.delivery_detail_id
2410          and wwr.wave_header_id=wwl.wave_header_id
2411          and wdd.delivery_detail_id = wwl.delivery_detail_id
2412 		 and wdd.organization_id = wwl.organization_id
2413          and wwl.wave_header_id = p_wave_id
2414          and wwr.wave_header_id=p_wave_id
2415        ORDER BY wave_line_id;
2416 
2417   BEGIN
2418     print_debug('In Initialize p_wave_id: '||p_wave_id||' '||'p_organization_id: '||p_organization_id, l_debug);
2419 
2420 
2421 
2422 
2423     SELECT enforce_ship_set_and_smc,
2424            default_stage_subinventory,
2425            default_stage_locator_id
2426       INTO g_enforce_ship_set_and_smc,
2427            g_default_stage_subinventory,
2428            g_default_stage_locator_id
2429       FROM WSH_SHIPPING_PARAMETERS
2430      WHERE ORGANIZATION_ID = p_organization_id;
2431 
2432 
2433     --g_from_subinventory := nvl(Pick from subinventory given in Planning criteria, g_pick_subinventory);
2434     g_from_subinventory := nvl(g_from_subinventory_plan, g_pick_subinventory);
2435     --g_from_locator := NULL since in wave planning currently no provision for default pick from locator
2436     g_from_locator    := NULL;
2437     g_to_subinventory := g_staging_subinventory;
2438   --  print_debug('initialize 2',l_debug);
2439   --g_to_locator  := staging locator given at wave header
2440 
2441 
2442     OPEN fetch_wdd(p_wave_id);
2443     fetch fetch_wdd bulk collect
2444       into g_troline_table;
2445 
2446     x_return_status := FND_API.G_RET_STS_SUCCESS;
2447 
2448    EXCEPTION
2449     WHEN No_Data_Found THEN
2450       print_debug('No Data Found Exception in initialize',l_debug);
2451       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2452     WHEN OTHERS THEN
2453       print_debug('OTHER Exception in initialize',l_debug);
2454       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2455 
2456   END initialize;
2457 
2458 
2459 
2460    PROCEDURE create_move_order(p_organization_id           number,
2461                               p_wave_id NUMBER,
2462                               x_mo_header_id   OUT NOCOPY NUMBER,
2463                               x_request_number OUT NOCOPY NUMBER,
2464                               x_return_status out nocopy varchar2) IS
2465 
2466     l_standalone_mode          VARCHAR2(1) := nvl(fnd_profile.VALUE('WMS_DEPLOYMENT_MODE'), 'I');
2467     l_line_id                  NUMBER;
2468     l_result                   number;
2469     l_requested_quantity       number;
2470     l_result2                  number;
2471     l_requested_quantity2      number;
2472     l_demand_source_header_id  NUMBER;
2473     l_primary_uom_code         varchar2(3);
2474     l_ship_set_id              NUMBER;
2475     l_top_model_line_id        NUMBER;
2476     l_ship_model_complete_flag VARCHAR2(1);
2477     l_last_top_model_line_id   NUMBER;
2478     l_last_model_quantity      NUMBER;
2479     l_last_ship_set_id         NUMBER;
2480     l_ordered_quantity         NUMBER;
2481     l_order_quantity_uom       VARCHAR2(1);
2482     l_request_number number;
2483     l_mo_header_id   number;
2484     l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
2485    -- l_count number := 0;
2486    l_tbl_count number;
2487    l_tbl_first number;
2488    l_tbl_last number;
2489    l_mtrl_count NUMBER := 0;
2490    l_wdd_count NUMBER := 0;
2491 
2492    l_attr_tab            wsh_delivery_autocreate.grp_attr_tab_type;
2493    l_action_rec          wsh_delivery_autocreate.action_rec_type;
2494    l_target_rec          wsh_delivery_autocreate.grp_attr_rec_type;
2495    l_group_info          wsh_delivery_autocreate.grp_attr_tab_type;
2496    l_matched_entities    wsh_util_core.id_tab_type;
2497    l_out_rec             wsh_delivery_autocreate.out_rec_type;
2498 
2499    TYPE group_match_seq_rec_type IS RECORD(
2500       delivery_detail_id NUMBER,
2501       match_group_id     NUMBER,
2502       delivery_group_id  NUMBER);
2503 
2504     TYPE group_match_seq_tab_type IS TABLE OF group_match_seq_rec_type INDEX BY BINARY_INTEGER;
2505    l_group_match_seq_tbl group_match_seq_tab_type;
2506 
2507    TYPE num_tbl_type IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
2508    l_carton_grouping_tbl  num_tbl_type;
2509 
2510    l_match_found    BOOLEAN;
2511    l_return_status varchar2(10);
2512   BEGIN
2513     print_debug('In Create Move Order', l_debug);
2514     SAVEPOINT create_move_order;
2515     --x_return_status := fnd_api.g_ret_sts_success;
2516 
2517     SELECT MTL_TXN_REQUEST_HEADERS_S.NEXTVAL
2518       into l_request_number
2519       FROM SYS.DUAL;
2520 
2521     l_mo_header_id := INV_TRANSFER_ORDER_PVT.get_next_header_id;
2522 
2523     INSERT INTO mtl_txn_request_headers
2524       (HEADER_ID,
2525        REQUEST_NUMBER,
2526        TRANSACTION_TYPE_ID,
2527        MOVE_ORDER_TYPE,
2528        ORGANIZATION_ID,
2529        DESCRIPTION,
2530        DATE_REQUIRED,
2531        FROM_SUBINVENTORY_CODE,
2532        TO_SUBINVENTORY_CODE,
2533        TO_ACCOUNT_ID,
2534        HEADER_STATUS,
2535        STATUS_DATE,
2536        LAST_UPDATED_BY,
2537        LAST_UPDATE_LOGIN,
2538        LAST_UPDATE_DATE,
2539        CREATED_BY,
2540        CREATION_DATE,
2541        REQUEST_ID,
2542        PROGRAM_APPLICATION_ID,
2543        PROGRAM_ID,
2544        PROGRAM_UPDATE_DATE,
2545        GROUPING_RULE_ID,
2546        ATTRIBUTE1,
2547        ATTRIBUTE2,
2548        ATTRIBUTE3,
2549        ATTRIBUTE4,
2550        ATTRIBUTE5,
2551        ATTRIBUTE6,
2552        ATTRIBUTE7,
2553        ATTRIBUTE8,
2554        ATTRIBUTE9,
2555        ATTRIBUTE10,
2556        ATTRIBUTE11,
2557        ATTRIBUTE12,
2558        ATTRIBUTE13,
2559        ATTRIBUTE14,
2560        ATTRIBUTE15,
2561        ATTRIBUTE_CATEGORY,
2562        SHIP_TO_LOCATION_ID,
2563        FREIGHT_CODE,
2564        SHIPMENT_METHOD,
2565        AUTO_RECEIPT_FLAG,
2566        REFERENCE_ID,
2567        REFERENCE_DETAIL_ID,
2568        ASSIGNMENT_ID)
2569     VALUES
2570       (l_mo_header_id,
2571        l_request_number,
2572        NULL,
2573        INV_GLOBALS.G_MOVE_ORDER_PICK_WAVE,
2574        p_organization_id,
2575        NULL,
2576        sysdate,
2577        NULL,
2578        NULL,
2579        NULL,
2580        INV_Globals.G_TO_STATUS_PREAPPROVED,
2581        NULL,
2582        FND_GLOBAL.USER_ID,
2583        FND_GLOBAL.CONC_LOGIN_ID,
2584        SYSDATE,
2585        FND_GLOBAL.USER_ID,
2586        SYSDATE,
2587        NULL,
2588        NULL,
2589        NULL,
2590        NULL,
2591        g_PICK_GROUPING_RULE_ID,
2592        NULL,
2593        NULL,
2594        NULL,
2595        NULL,
2596        NULL,
2597        NULL,
2598        NULL,
2599        NULL,
2600        NULL,
2601        NULL,
2602        NULL,
2603        NULL,
2604        NULL,
2605        NULL,
2606        NULL,
2607        NULL,
2608        NULL,
2609        NULL,
2610        NULL,
2611        NULL,
2612        NULL,
2613        NULL,
2614        NULL);
2615 
2616     print_debug('Created Move Order: header_id '||l_mo_header_id||' request_number '||l_request_number, l_debug);
2617 
2618 
2619     FOR i IN g_troline_table.first .. g_troline_table.last LOOP
2620 		IF(g_troline_table.exists(i)) THEN --BUG14197871
2621 			l_attr_tab(i).entity_id := g_troline_table(i).delivery_detail_id;
2622 			l_attr_tab(i).entity_type := 'DELIVERY_DETAIL';
2623 		END IF;
2624     END LOOP;
2625 
2626     l_action_rec.action               := 'MATCH_GROUPS';
2627     l_action_rec.group_by_header_flag := 'N';
2628 
2629     print_debug('Calling Find Matching Groups ', l_debug);
2630 
2631     wsh_Delivery_autocreate.Find_Matching_Groups(p_attr_tab         => l_attr_tab,
2632                                                      p_action_rec       => l_action_rec,
2633                                                      p_target_rec       => l_target_rec,
2634                                                      p_group_tab        => l_group_info,
2635                                                      x_matched_entities => l_matched_entities,
2636                                                      x_out_rec          => l_out_rec,
2637                                                      x_return_status    => l_return_status);
2638 
2639     IF l_return_status <> fnd_api.g_ret_sts_success THEN
2640           print_debug('Error status from Find_Matching_Groups: ' || l_return_status, l_debug);
2641           RAISE fnd_api.g_exc_unexpected_error;
2642     END IF;
2643 
2644     for i in 1 .. l_attr_tab.count loop
2645 
2646           l_match_found := FALSE;
2647 
2648           IF l_group_match_seq_tbl.count > 0 THEN
2649             --{
2650             FOR k in l_group_match_seq_tbl.FIRST .. l_group_match_seq_tbl.LAST LOOP
2651               --{
2652               IF l_attr_tab(i).group_id = l_group_match_seq_tbl(k).match_group_id THEN
2653                 --{
2654                 l_group_match_seq_tbl(i).delivery_group_id := l_group_match_seq_tbl(k).delivery_group_id;
2655                 l_match_found := TRUE;
2656                 EXIT;
2657                 --}
2658               End IF;
2659               --}
2660             END LOOP;
2661             --}
2662           END IF;
2663 
2664           IF NOT l_match_found THEN
2665 
2666             l_group_match_seq_tbl(i).match_group_id := l_attr_tab(i).group_id;
2667             select WSH_DELIVERY_GROUP_S.nextval
2668               into l_group_match_seq_tbl(i).delivery_group_id
2669               from dual;
2670 
2671           End IF;
2672 
2673           print_debug('CARTON GROUPING ID : ' || l_group_match_seq_tbl(i).delivery_group_id, l_debug);
2674 
2675           l_carton_grouping_tbl(i) := l_group_match_seq_tbl(i).delivery_group_id;
2676 
2677         end loop;
2678 
2679 
2680    for i in g_troline_table.FIRST .. g_troline_table.LAST loop
2681 
2682     	SELECT MTL_TXN_REQUEST_LINES_S.NEXTVAL INTO l_seq_val FROM dual;     -- Changed
2683       l_line_id := l_seq_val;
2684 
2685       g_mtrl_tbl(i).LINE_ID := l_line_id;
2686       g_mtrl_tbl(i).HEADER_ID := l_mo_header_id;
2687 
2688       g_mtrl_tbl(i).LINE_NUMBER := i;
2689       g_mtrl_tbl(i).ORGANIZATION_ID := p_organization_id;
2690       g_mtrl_tbl(i).INVENTORY_ITEM_ID := g_troline_table(i).inventory_item_id;
2691       g_mtrl_tbl(i).REVISION := g_troline_table(i).revision;
2692 
2693 
2694       -- calculate g_mtrl_tbl.from_subinventory_code,from_locator_id
2695       IF ((g_troline_table(i).from_sub is NOT NULL) AND
2696          (g_from_subinventory IS NULL)) THEN
2697         g_mtrl_tbl(i).from_subinventory_code := g_troline_table(i).from_sub;
2698         -- Standalone project Changes : Begin
2699         IF (l_standalone_mode = 'D') THEN
2700           g_mtrl_tbl(i).from_locator_id := g_troline_table(i).from_locator;
2701         END IF;
2702       ELSE
2703         g_mtrl_tbl(i).from_subinventory_code := g_from_subinventory;
2704         -- Standalone project Changes
2705         -- wdd's loc id should be considered when released with pick from sub and no pick from loc
2706         IF (l_standalone_mode = 'D' AND g_from_locator IS NULL) THEN
2707           IF (g_mtrl_tbl(i).from_subinventory_code = g_from_subinventory) THEN
2708             g_mtrl_tbl(i).from_locator_id := g_troline_table(i)
2709                                             .from_locator;
2710           ELSE
2711             g_mtrl_tbl(i).from_locator_id := NULL;
2712           END IF;
2713         ELSE
2714           g_mtrl_tbl(i).from_locator_id := g_from_locator;
2715         END IF;
2716       END IF;
2717 
2718 
2719 
2720       -- calculate g_mtrl_tbl.revision,lot_number
2721       -- Standalone project Changes
2722       IF (l_standalone_mode = 'D') THEN
2723         g_mtrl_tbl(i).revision := g_troline_table(i).revision;
2724         g_mtrl_tbl(i).lot_number := g_troline_table(i).lot_number;
2725       END IF;
2726 
2727       g_mtrl_tbl(i).to_subinventory_code := g_to_subinventory;
2728       g_mtrl_tbl(i).to_locator_id := g_to_locator;
2729 
2730       g_mtrl_tbl(i).TO_ACCOUNT_ID := null;
2731       g_mtrl_tbl(i).SERIAL_NUMBER_START := null;
2732       g_mtrl_tbl(i).SERIAL_NUMBER_END := null;
2733       g_mtrl_tbl(i).UOM_CODE := g_troline_table(i).requested_quantity_uom;
2734 
2735       -- calculate g_mtrl_tbl(i).quantity
2736       l_result              := g_troline_table(i).requested_quantity;
2737       l_requested_quantity  := g_troline_table(i).requested_quantity;
2738       l_result2             := g_troline_table(i).requested_quantity2;
2739       l_requested_quantity2 := nvl(g_troline_table(i).requested_quantity2,
2740                                    0);
2741 
2742 
2743       l_demand_source_header_id := INV_SALESORDER.GET_SALESORDER_FOR_OEHEADER(g_troline_table(i).source_header_id);
2744 
2745       IF (g_from_subinventory is not null) THEN
2746 
2747 
2748         WSH_PICK_LIST.Calculate_Reservations(p_demand_source_header_id => l_demand_source_header_id,
2749                                              p_demand_source_line_id   => g_troline_table(i)
2750                                                                          .source_line_id,
2751                                              p_requested_quantity      => g_troline_table(i)
2752                                                                          .requested_quantity,
2753                                              -- Bug 4775539
2754                                              p_requested_quantity_uom     => g_troline_table(i)
2755                                                                             .requested_quantity_uom,
2756                                              p_src_requested_quantity_uom => g_troline_table(i)
2757                                                                             .src_requested_quantity_uom,
2758                                              p_src_requested_quantity     => g_troline_table(i)
2759                                                                             .src_requested_quantity,
2760                                              p_inv_item_id                => g_troline_table(i)
2761                                                                             .inventory_item_id,
2762                                              --  p_requested_quantity2        => g_troline_table(i).requested_quantity2,
2763                                              x_result  => l_result,
2764                                              x_result2 => l_result2);
2765 
2766 
2767       END IF;
2768 
2769       g_troline_table(i).requested_quantity := l_result;
2770       g_troline_table(i).SECONDARY_QUANTITY := to_number(nvl(l_result2, 0));
2771       g_mtrl_tbl(i).quantity := round(g_troline_table(i).requested_quantity,
2772                                       5);
2773 
2774       g_mtrl_tbl(i).QUANTITY_DELIVERED := NULL;
2775       g_mtrl_tbl(i).QUANTITY_DETAILED := null;
2776       g_mtrl_tbl(i).DATE_REQUIRED := g_troline_table(i).date_scheduled;
2777       g_mtrl_tbl(i).REASON_ID := null;
2778       g_mtrl_tbl(i).REFERENCE := null;
2779       g_mtrl_tbl(i).REFERENCE_TYPE_CODE := null;
2780       g_mtrl_tbl(i).REFERENCE_ID := null;
2781       g_mtrl_tbl(i).PROJECT_ID := g_troline_table(i).project_id;
2782       g_mtrl_tbl(i).TASK_ID := g_troline_table(i).task_id;
2783       g_mtrl_tbl(i).TRANSACTION_HEADER_ID := null;
2784       g_mtrl_tbl(i).LINE_STATUS := INV_Globals.G_TO_STATUS_PREAPPROVED;
2785       g_mtrl_tbl(i).STATUS_DATE := SYSDATE;
2786       g_mtrl_tbl(i).LAST_UPDATED_BY := FND_GLOBAL.USER_ID;
2787       g_mtrl_tbl(i).LAST_UPDATE_LOGIN := FND_GLOBAL.CONC_LOGIN_ID;
2788       g_mtrl_tbl(i).LAST_UPDATE_DATE := SYSDATE;
2789       g_mtrl_tbl(i).CREATED_BY := FND_GLOBAL.USER_ID;
2790       g_mtrl_tbl(i).CREATION_DATE := SYSDATE;
2791       g_mtrl_tbl(i).REQUEST_ID := null;
2792       g_mtrl_tbl(i).PROGRAM_APPLICATION_ID := null;
2793       g_mtrl_tbl(i).PROGRAM_ID := null;
2794       g_mtrl_tbl(i).PROGRAM_UPDATE_DATE := null;
2795       g_mtrl_tbl(i).ATTRIBUTE1 := null;
2796       g_mtrl_tbl(i).ATTRIBUTE2 := null;
2797       g_mtrl_tbl(i).ATTRIBUTE3 := null;
2798       g_mtrl_tbl(i).ATTRIBUTE4 := null;
2799       g_mtrl_tbl(i).ATTRIBUTE5 := null;
2800       g_mtrl_tbl(i).ATTRIBUTE6 := null;
2801       g_mtrl_tbl(i).ATTRIBUTE7 := null;
2802       g_mtrl_tbl(i).ATTRIBUTE8 := null;
2803       g_mtrl_tbl(i).ATTRIBUTE9 := null;
2804       g_mtrl_tbl(i).ATTRIBUTE10 := null;
2805       g_mtrl_tbl(i).ATTRIBUTE11 := null;
2806       g_mtrl_tbl(i).ATTRIBUTE12 := null;
2807       g_mtrl_tbl(i).ATTRIBUTE13 := null;
2808       g_mtrl_tbl(i).ATTRIBUTE14 := null;
2809       g_mtrl_tbl(i).ATTRIBUTE15 := null;
2810       g_mtrl_tbl(i).ATTRIBUTE_CATEGORY := null;
2811 
2812       g_mtrl_tbl(i).TXN_SOURCE_ID := l_demand_source_header_id;
2813       g_mtrl_tbl(i).TXN_SOURCE_LINE_ID := g_troline_table(i).source_line_id;
2814       g_mtrl_tbl(i).TXN_SOURCE_LINE_DETAIL_ID := g_troline_table(i).delivery_detail_id;
2815 
2816       g_mtrl_tbl(i).TRANSACTION_TYPE_ID := INV_GLOBALS.G_TYPE_TRANSFER_ORDER_STGXFR;
2817       g_mtrl_tbl(i).TRANSACTION_SOURCE_TYPE_ID := 2;
2818 
2819       -- calculate g_mtrl_tbl(i).PRIMARY_QUANTITY
2820       SELECT primary_uom_code
2821         INTO l_primary_uom_code
2822         FROM mtl_system_items
2823        WHERE organization_id = g_mtrl_tbl(i)
2824       .organization_id
2825          AND inventory_item_id = g_mtrl_tbl(i).inventory_item_id;
2826 
2827       IF l_primary_uom_code = g_mtrl_tbl(i).uom_code THEN
2828         g_mtrl_tbl(i).primary_quantity := g_mtrl_tbl(i).quantity;
2829       ELSE
2830         g_mtrl_tbl(i).primary_quantity := inv_convert.inv_um_convert(item_id       => g_mtrl_tbl(i)
2831                                                                                      .inventory_item_id,
2832                                                                      PRECISION     => NULL,
2833                                                                      from_quantity => g_mtrl_tbl(i)
2834                                                                                      .quantity,
2835                                                                      from_unit     => g_mtrl_tbl(i)
2836                                                                                      .uom_code,
2837                                                                      to_unit       => l_primary_uom_code,
2838                                                                      from_name     => NULL,
2839                                                                      to_name       => NULL);
2840       END IF;
2841 
2842 
2843     --	   SELECT WSH_DELIVERY_GROUP_S.NEXTVAL INTO l_seq_val FROM dual;     -- Changed
2844 
2845 
2846       g_mtrl_tbl(i).TO_ORGANIZATION_ID := null;
2847       g_mtrl_tbl(i).PUT_AWAY_STRATEGY_ID := null;
2848       g_mtrl_tbl(i).PICK_STRATEGY_ID := null;
2849       g_mtrl_tbl(i).SHIP_TO_LOCATION_ID := null;
2850       g_mtrl_tbl(i).UNIT_NUMBER := g_troline_table(i).unit_number;
2851       g_mtrl_tbl(i).REFERENCE_DETAIL_ID := null;
2852       g_mtrl_tbl(i).ASSIGNMENT_ID := null;
2853       g_mtrl_tbl(i).FROM_COST_GROUP_ID := null;
2854       g_mtrl_tbl(i).TO_COST_GROUP_ID := null;
2855       g_mtrl_tbl(i).LPN_ID := null;
2856       g_mtrl_tbl(i).TO_LPN_ID := null;
2857       g_mtrl_tbl(i).PICK_SLIP_NUMBER := null;
2858       g_mtrl_tbl(i).PICK_SLIP_DATE := null;
2859       g_mtrl_tbl(i).FROM_SUBINVENTORY_ID := null;
2860       g_mtrl_tbl(i).TO_SUBINVENTORY_ID := null;
2861       g_mtrl_tbl(i).INSPECTION_STATUS := null;
2862       g_mtrl_tbl(i).PICK_METHODOLOGY_ID := null;
2863       g_mtrl_tbl(i).CONTAINER_ITEM_ID := null;
2864       --g_mtrl_tbl(i).CARTON_GROUPING_ID := l_seq_val;
2865       g_mtrl_tbl(i).CARTON_GROUPING_ID := l_carton_grouping_tbl(i);
2866       g_mtrl_tbl(i).BACKORDER_DELIVERY_DETAIL_ID := null;
2867       g_mtrl_tbl(i).WMS_PROCESS_FLAG := null;
2868       g_mtrl_tbl(i).SHIP_SET_ID := g_troline_table(i).ship_set_id;
2869 
2870       -- calculate g_mtrl_tbl(i).SHIP_MODEL_ID and g_mtrl_tbl(i).MODEL_QUANTITY
2871       l_ship_set_id              := g_troline_table(i).ship_set_id;
2872       l_ship_model_complete_flag := g_troline_table(i).SHIP_MODEL_COMPLETE_FLAG;
2873       l_top_model_line_id        := g_troline_table(i).top_model_line_id;
2874 
2875       IF (g_enforce_ship_set_and_smc = 'Y') THEN
2876         IF (l_ship_set_id IS NOT NULL) THEN
2877           -- Ignore SMC if SS is Specified
2878           l_ship_model_complete_flag := NULL;
2879           l_top_model_line_id        := NULL;
2880         ELSE
2881           IF (NVL(l_ship_model_complete_flag, 'N') = 'N') THEN
2882             -- Ignore top_model_line_id if SMC is not set to Y
2883             l_top_model_line_id := NULL;
2884           END IF;
2885         END IF;
2886 
2887         IF (((l_last_ship_set_id IS NOT NULL) AND
2888            (l_last_ship_set_id <> NVL(l_ship_set_id, -99))) OR
2889            ((l_last_top_model_line_id IS NOT NULL) AND
2890            (l_last_top_model_line_id <> NVL(l_top_model_line_id, -99)))) THEN
2891           l_last_top_model_line_id := NULL;
2892           l_last_model_quantity    := NULL;
2893         END IF;
2894       ELSE
2895         l_ship_set_id              := NULL;
2896         l_top_model_line_id        := NULL;
2897         l_ship_model_complete_flag := 'N';
2898       END IF; -- g_enforce_ship_set_and_smc
2899 
2900       IF ((l_last_ship_set_id IS NULL) AND
2901          (l_last_top_model_line_id IS NULL)) THEN
2902         l_last_model_quantity := NULL;
2903       END IF;
2904 
2905       IF ((g_troline_table(i).ship_set_id is NULL) AND
2906          (l_top_model_line_id is NOT NULL)) THEN
2907         g_troline_table(i).top_model_line_id := l_top_model_line_id;
2908         IF (l_top_model_line_id <> NVL(l_last_top_model_line_id, -99)) THEN
2909           SELECT ORDERED_QUANTITY, ORDER_QUANTITY_UOM
2910             into l_ordered_quantity, l_order_quantity_uom
2911             FROM OE_ORDER_LINES_ALL
2912            WHERE LINE_ID = l_top_model_line_id;
2913 
2914           g_troline_table(i).top_model_quantity := l_ordered_quantity;
2915           l_last_model_quantity := l_ordered_quantity;
2916         ELSE
2917           g_troline_table(i).top_model_quantity := l_last_model_quantity;
2918         END IF;
2919       ELSE
2920         g_troline_table(i).top_model_line_id := NULL;
2921         g_troline_table(i).top_model_quantity := NULL;
2922       END IF;
2923 
2924       IF (g_enforce_ship_set_and_smc = 'Y') THEN
2925         l_last_ship_set_id       := l_ship_set_id;
2926         l_last_top_model_line_id := l_top_model_line_id;
2927       END IF;
2928 
2929       g_mtrl_tbl(i).ship_model_id := g_troline_table(i).top_model_line_id;
2930       g_mtrl_tbl(i).model_quantity := g_troline_table(i).top_model_quantity;
2931 
2932       g_mtrl_tbl(i).CROSSDOCK_TYPE := null;
2933       g_mtrl_tbl(i).REQUIRED_QUANTITY := null;
2934       g_mtrl_tbl(i).GRADE_CODE := g_troline_table(i).preferred_grade;
2935       g_mtrl_tbl(i).SECONDARY_QUANTITY := g_troline_table(i).SECONDARY_QUANTITY;
2936       g_mtrl_tbl(i).SECONDARY_QUANTITY_DELIVERED := null;
2937       g_mtrl_tbl(i).SECONDARY_QUANTITY_DETAILED := null;
2938       g_mtrl_tbl(i).SECONDARY_REQUIRED_QUANTITY := null;
2939       g_mtrl_tbl(i).SECONDARY_UOM_CODE := g_troline_table(i).requested_quantity_uom2;
2940       g_mtrl_tbl(i).WIP_ENTITY_ID := null;
2941       g_mtrl_tbl(i).REPETITIVE_SCHEDULE_ID := null;
2942       g_mtrl_tbl(i).OPERATION_SEQ_NUM := null;
2943       g_mtrl_tbl(i).WIP_SUPPLY_TYPE := null;
2944     end loop;
2945 
2946    print_debug('CREATE MO LINE END OF LOOP', l_debug);
2947    print_debug('g_mtrl_tbl.first: '||g_mtrl_tbl.first||' g_mtrl_tbl.last: '||g_mtrl_tbl.last||' g_mtrl_tbl.count: '||g_mtrl_tbl.count,l_debug);
2948 
2949     -- for each WDD in the current wave insert record in MTRL with following values:
2950     Forall i in g_mtrl_tbl.first .. g_mtrl_tbl.last
2951       insert into mtl_txn_request_lines values g_mtrl_tbl (i);
2952 
2953 l_mtrl_count := SQL%ROWCOUNT;
2954       print_debug( l_mtrl_count||' move order lines inserted INTO MTRL', l_debug);
2955 
2956 
2957 
2958 --l_tbl_first := g_mtrl_tbl.first;
2959 --l_tbl_last := g_mtrl_tbl.last;
2960 
2961  --  FORALL i in l_tbl_first .. l_tbl_last
2962  --dbchange2
2963  FOR i in g_mtrl_tbl.first .. g_mtrl_tbl.last LOOP
2964 
2965       UPDATE wsh_delivery_details
2966       SET move_order_line_id = g_mtrl_tbl(i).line_id
2967       WHERE delivery_detail_id = g_mtrl_tbl(i).TXN_SOURCE_LINE_DETAIL_ID
2968       AND move_order_line_id IS NULL;
2969 
2970       l_wdd_count := l_wdd_count +SQL%ROWCOUNT;
2971 
2972 	  IF(SQL%ROWCOUNT = 0) THEN
2973 		print_debug( l_wdd_count||' has not been modified as delivery detail' ||g_mtrl_tbl(i).TXN_SOURCE_LINE_DETAIL_ID ||'already had move order on it', l_debug);
2974 	  END IF;
2975 end loop;
2976 
2977 --l_wdd_count := SQL%ROWCOUNT;
2978 
2979     print_debug( l_wdd_count||' WDDs updated with move_order_line_Id', l_debug);
2980 
2981  print_debug( 'g_mtrl_tbl.Count is '||g_mtrl_tbl.Count, l_debug);
2982 
2983     IF l_wdd_count <> g_mtrl_tbl.Count THEN
2984       print_debug('Could not update all WDD lines', l_debug);
2985       RAISE fnd_api.g_exc_unexpected_error;
2986     ELSE
2987       g_update_wdd  := 'Y';
2988     END IF;
2989 
2990    x_mo_header_id   := l_mo_header_id;
2991    x_request_number := l_request_number;
2992 
2993    COMMIT;
2994    x_return_status := FND_API.G_RET_STS_SUCCESS;
2995    print_debug('Exiting Create Move Order', l_debug);
2996 
2997   EXCEPTION
2998     WHEN OTHERS THEN
2999       print_debug('Error in create_move_order',l_debug);
3000       ROLLBACK TO create_move_order;
3001       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3002 
3003 END create_move_order;
3004 
3005 
3006 
3007 
3008 PROCEDURE process_reservations(
3009     p_wave_id IN NUMBER,
3010     p_action  IN VARCHAR2,
3011     x_return_status OUT NOCOPY VARCHAR2 )
3012 IS
3013 TYPE num_tab IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
3014 TYPE uom_tab IS TABLE OF VARCHAR2(3) INDEX BY BINARY_INTEGER;
3015 TYPE date_tab IS TABLE OF DATE INDEX BY BINARY_INTEGER;
3016 TYPE char_tab IS TABLE OF VARCHAR2(30) INDEX BY BINARY_INTEGER;
3017 TYPE char1_tab IS TABLE OF VARCHAR2(1) INDEX BY BINARY_INTEGER;
3018 TYPE char150_tab IS TABLE OF VARCHAR2(150) INDEX BY BINARY_INTEGER;
3019 TYPE char80_tab IS TABLE OF VARCHAR2(80) INDEX BY BINARY_INTEGER;
3020 
3021   lt_reservation_id num_tab;
3022   lt_requirement_date date_tab;
3023   lt_organization_id num_tab;
3024   lt_inventory_item_id num_tab;
3025   lt_demand_source_type_id num_tab;
3026   lt_demand_source_name char_tab;
3027   lt_demand_source_header_id num_tab;
3028   lt_demand_source_line_id num_tab;
3029   lt_demand_source_delivery num_tab;
3030   lt_primary_uom_code uom_tab;
3031   lt_primary_uom_id num_tab;
3032   lt_reservation_uom_code uom_tab ;
3033   lt_reservation_uom_id num_tab;
3034   lt_reservation_quantity num_tab;
3035   lt_prim_res_qty num_tab;
3036   lt_autodetail_group_id num_tab;
3037   lt_external_source_code char_tab;
3038   lt_external_source_line_id num_tab;
3039   lt_supply_source_type_id num_tab;
3040   lt_supply_source_header_id num_tab;
3041   lt_supply_source_line_id num_tab;
3042   lt_supply_source_line_detail num_tab;
3043   lt_supply_source_name char_tab ;
3044   lt_revision uom_tab;
3045   lt_subinventory_code char_tab;
3046   lt_subinventory_id num_tab;
3047   lt_locator_id num_tab;
3048   lt_lot_number char80_tab;
3049   lt_lot_number_id num_tab ;
3050   lt_serial_number char_tab;
3051   lt_serial_number_id num_tab ;
3052   lt_partial_quantities_allowed num_tab;
3053   lt_auto_detailed num_tab;
3054   lt_pick_slip_number num_tab ;
3055   lt_lpn_id num_tab;
3056   lt_last_update_date date_tab;
3057   lt_last_updated_by num_tab;
3058   lt_creation_date date_tab;
3059   lt_created_by num_tab;
3060   lt_last_update_login num_tab;
3061   lt_request_id num_tab;
3062   lt_program_application_id num_tab;
3063   lt_program_id num_tab;
3064   lt_program_update_date date_tab;
3065   lt_attribute_category char_tab;
3066   lt_attribute1 char150_tab;
3067   lt_attribute2 char150_tab;
3068   lt_attribute3 char150_tab;
3069   lt_attribute4 char150_tab;
3070   lt_attribute5 char150_tab;
3071   lt_attribute6 char150_tab;
3072   lt_attribute7 char150_tab;
3073   lt_attribute8 char150_tab ;
3074   lt_attribute9 char150_tab;
3075   lt_attribute10 char150_tab;
3076   lt_attribute11 char150_tab;
3077   lt_attribute12 char150_tab;
3078   lt_attribute13 char150_tab;
3079   lt_attribute14 char150_tab;
3080   lt_attribute15 char150_tab;
3081   lt_ship_ready_flag num_tab;
3082   lt_n_column1 num_tab;
3083   lt_detailed_quantity num_tab;
3084   lt_cost_group_id num_tab;
3085   lt_container_lpn_id num_tab;
3086   lt_staged_flag char1_tab ;
3087   lt_secondary_detailed_quantity num_tab;
3088   lt_sec_res_qty num_tab;
3089   lt_secondary_uom_code uom_tab;
3090   lt_secondary_uom_id num_tab;
3091   lt_crossdock_flag char1_tab;
3092   lt_crossdock_criteria_id num_tab;
3093   lt_demand_source_line_detail num_tab;
3094   lt_serial_reservation_quantity num_tab;
3095   lt_supply_receipt_date date_tab;
3096   lt_demand_ship_date date_tab ;
3097   lt_exception_code char150_tab;
3098   lt_orig_supply_source_type_id num_tab;
3099   lt_orig_sup_src_hdr_id num_tab;
3100   lt_orig_supply_source_line_id num_tab;
3101   lt_orig_sup_src_line_det num_tab ;
3102   lt_orig_demand_source_type_id num_tab;
3103   lt_orig_dd_src_hdr_id num_tab;
3104   lt_orig_demand_source_line_id num_tab;
3105   lt_orig_dd_src_line_det num_tab;
3106   lt_project_id num_tab;
3107   lt_task_id num_tab;
3108 
3109   CURSOR c_reservations (p_wave_id NUMBER)
3110   IS
3111     SELECT reservation_id ,
3112       requirement_date,
3113       organization_id,
3114       inventory_item_id,
3115       demand_source_type_id,
3116       demand_source_name,
3117       demand_source_header_id,
3118       demand_source_line_id,
3119       demand_source_delivery,
3120       primary_uom_code,
3121       primary_uom_id,
3122       reservation_uom_code ,
3123       reservation_uom_id,
3124       reservation_quantity,
3125       primary_reservation_quantity,
3126       autodetail_group_id,
3127       external_source_code,
3128       external_source_line_id,
3129       supply_source_type_id,
3130       supply_source_header_id,
3131       supply_source_line_id,
3132       supply_source_line_detail,
3133       supply_source_name ,
3134       revision,
3135       subinventory_code,
3136       subinventory_id,
3137       locator_id,
3138       lot_number ,
3139       lot_number_id ,
3140       serial_number,
3141       serial_number_id ,
3142       partial_quantities_allowed ,
3143       auto_detailed ,
3144       pick_slip_number ,
3145       lpn_id ,
3146       last_update_date,
3147       last_updated_by,
3148       creation_date ,
3149       created_by,
3150       last_update_login ,
3151       request_id ,
3152       program_application_id,
3153       program_id ,
3154       program_update_date,
3155       attribute_category,
3156       attribute1 ,
3157       attribute2 ,
3158       attribute3 ,
3159       attribute4 ,
3160       attribute5 ,
3161       attribute6 ,
3162       attribute7 ,
3163       attribute8 ,
3164       attribute9 ,
3165       attribute10 ,
3166       attribute11 ,
3167       attribute12 ,
3168       attribute13 ,
3169       attribute14 ,
3170       attribute15 ,
3171       ship_ready_flag,
3172       n_column1,
3173       detailed_quantity,
3174       cost_group_id ,
3175       container_lpn_id ,
3176       staged_flag ,
3177       secondary_detailed_quantity ,
3178       secondary_reservation_quantity,
3179       secondary_uom_code,
3180       secondary_uom_id,
3181       crossdock_flag,
3182       crossdock_criteria_id,
3183       demand_source_line_detail,
3184       serial_reservation_quantity,
3185       supply_receipt_date,
3186       demand_ship_date ,
3187       exception_code,
3188       orig_supply_source_type_id,
3189       orig_supply_source_header_id,
3190       orig_supply_source_line_id,
3191       orig_supply_source_line_detail ,
3192       orig_demand_source_type_id,
3193       orig_demand_source_header_id,
3194       orig_demand_source_line_id,
3195       orig_demand_source_line_detail,
3196       project_id,
3197       task_id
3198     FROM mtl_reservations
3199     WHERE demand_source_line_id IN
3200       (SELECT DISTINCT wdd.source_line_id
3201       FROM WMS_WP_WAVE_LINES wwl,
3202         wsh_delivery_details wdd--11775489 get from wdd... FIXED
3203       WHERE wwl.wave_header_id   = p_wave_id
3204       AND wwl.delivery_detail_id = wdd.delivery_detail_id
3205       AND wdd.organization_id    = wwl.organization_id
3206       );
3207 
3208 TYPE rsTblTyp IS TABLE OF mtl_reservations%rowtype INDEX BY binary_integer;
3209 
3210   l_rs_tbl rsTblTyp;
3211   l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
3212 BEGIN
3213 
3214   IF p_action = 'S' THEN
3215     OPEN c_reservations(p_wave_id);
3216     FETCH c_reservations BULK COLLECT
3217     INTO lt_reservation_id ,
3218       lt_requirement_date ,
3219       lt_organization_id ,
3220       lt_inventory_item_id ,
3221       lt_demand_source_type_id ,
3222       lt_demand_source_name ,
3223       lt_demand_source_header_id ,
3224       lt_demand_source_line_id ,
3225       lt_demand_source_delivery ,
3226       lt_primary_uom_code ,
3227       lt_primary_uom_id ,
3228       lt_reservation_uom_code ,
3229       lt_reservation_uom_id ,
3230       lt_reservation_quantity ,
3231       lt_prim_res_qty ,
3232       lt_autodetail_group_id ,
3233       lt_external_source_code ,
3234       lt_external_source_line_id ,
3235       lt_supply_source_type_id ,
3236       lt_supply_source_header_id ,
3237       lt_supply_source_line_id ,
3238       lt_supply_source_line_detail ,
3239       lt_supply_source_name ,
3240       lt_revision ,
3241       lt_subinventory_code ,
3242       lt_subinventory_id ,
3243       lt_locator_id ,
3244       lt_lot_number ,
3245       lt_lot_number_id ,
3246       lt_serial_number ,
3247       lt_serial_number_id ,
3248       lt_partial_quantities_allowed ,
3249       lt_auto_detailed ,
3250       lt_pick_slip_number ,
3251       lt_lpn_id ,
3252       lt_last_update_date ,
3253       lt_last_updated_by ,
3254       lt_creation_date ,
3255       lt_created_by ,
3256       lt_last_update_login ,
3257       lt_request_id ,
3258       lt_program_application_id ,
3259       lt_program_id ,
3260       lt_program_update_date ,
3261       lt_attribute_category ,
3262       lt_attribute1 ,
3263       lt_attribute2 ,
3264       lt_attribute3 ,
3265       lt_attribute4 ,
3266       lt_attribute5 ,
3267       lt_attribute6 ,
3268       lt_attribute7 ,
3269       lt_attribute8 ,
3270       lt_attribute9 ,
3271       lt_attribute10 ,
3272       lt_attribute11 ,
3273       lt_attribute12 ,
3274       lt_attribute13 ,
3275       lt_attribute14 ,
3276       lt_attribute15 ,
3277       lt_ship_ready_flag ,
3278       lt_n_column1 ,
3279       lt_detailed_quantity ,
3280       lt_cost_group_id ,
3281       lt_container_lpn_id ,
3282       lt_staged_flag ,
3283       lt_secondary_detailed_quantity ,
3284       lt_sec_res_qty ,
3285       lt_secondary_uom_code ,
3286       lt_secondary_uom_id ,
3287       lt_crossdock_flag ,
3288       lt_crossdock_criteria_id ,
3289       lt_demand_source_line_detail,
3290       lt_serial_reservation_quantity ,
3291       lt_supply_receipt_date ,
3292       lt_demand_ship_date ,
3293       lt_exception_code ,
3294       lt_orig_supply_source_type_id ,
3295       lt_orig_sup_src_hdr_id ,
3296       lt_orig_supply_source_line_id ,
3297       lt_orig_sup_src_line_det ,
3298       lt_orig_demand_source_type_id ,
3299       lt_orig_dd_src_hdr_id ,
3300       lt_orig_demand_source_line_id ,
3301       lt_orig_dd_src_line_det ,
3302       lt_project_id ,
3303       lt_task_id;
3304 
3305     print_Debug('Total no of reservation records for all line in current wave '||c_reservations%ROWCOUNT,l_debug);
3306 
3307     IF c_reservations%ROWCOUNT > 0 THEN
3308       FORALL i IN lt_reservation_id.first .. lt_reservation_id.last
3309       INSERT
3310       INTO wms_wp_reservations_gtmp
3311         (
3312           reservation_id ,
3313           requirement_date,
3314           organization_id,
3315           inventory_item_id,
3316           demand_source_type_id,
3317           demand_source_name,
3318           demand_source_header_id,
3319           demand_source_line_id,
3320           demand_source_delivery,
3321           primary_uom_code,
3322           primary_uom_id,
3323           reservation_uom_code ,
3324           reservation_uom_id,
3325           reservation_quantity,
3326           primary_reservation_quantity,
3327           autodetail_group_id,
3328           external_source_code,
3329           external_source_line_id,
3330           supply_source_type_id,
3331           supply_source_header_id,
3332           supply_source_line_id,
3333           supply_source_line_detail,
3334           supply_source_name ,
3335           revision,
3336           subinventory_code,
3337           subinventory_id,
3338           locator_id,
3339           lot_number ,
3340           lot_number_id ,
3341           serial_number,
3342           serial_number_id ,
3343           partial_quantities_allowed ,
3344           auto_detailed ,
3345           pick_slip_number ,
3346           lpn_id ,
3347           last_update_date,
3348           last_updated_by,
3349           creation_date ,
3350           created_by,
3351           last_update_login ,
3352           request_id ,
3353           program_application_id,
3354           program_id ,
3355           program_update_date,
3356           attribute_category,
3357           attribute1 ,
3358           attribute2 ,
3359           attribute3 ,
3360           attribute4 ,
3361           attribute5 ,
3362           attribute6 ,
3363           attribute7 ,
3364           attribute8 ,
3365           attribute9 ,
3366           attribute10 ,
3367           attribute11 ,
3368           attribute12 ,
3369           attribute13 ,
3370           attribute14 ,
3371           attribute15 ,
3372           ship_ready_flag,
3373           n_column1,
3374           detailed_quantity,
3375           cost_group_id ,
3376           container_lpn_id ,
3377           staged_flag ,
3378           secondary_detailed_quantity ,
3379           secondary_reservation_quantity,
3380           secondary_uom_code,
3381           secondary_uom_id,
3382           crossdock_flag,
3383           crossdock_criteria_id,
3384           demand_source_line_detail,
3385           serial_reservation_quantity,
3386           supply_receipt_date,
3387           demand_ship_date ,
3388           exception_code,
3389           orig_supply_source_type_id,
3390           orig_supply_source_header_id,
3391           orig_supply_source_line_id,
3392           orig_supply_source_line_detail ,
3393           orig_demand_source_type_id,
3394           orig_demand_source_header_id,
3395           orig_demand_source_line_id,
3396           orig_demand_source_line_detail,
3397           project_id,
3398           task_id
3399         )
3400         VALUES
3401         (
3402           lt_reservation_id(i) ,
3403           lt_requirement_date(i) ,
3404           lt_organization_id(i) ,
3405           lt_inventory_item_id(i) ,
3406           lt_demand_source_type_id(i) ,
3407           lt_demand_source_name(i) ,
3408           lt_demand_source_header_id(i) ,
3409           lt_demand_source_line_id(i) ,
3410           lt_demand_source_delivery(i) ,
3411           lt_primary_uom_code(i) ,
3412           lt_primary_uom_id(i) ,
3413           lt_reservation_uom_code(i) ,
3414           lt_reservation_uom_id(i) ,
3415           lt_reservation_quantity(i) ,
3416           lt_prim_res_qty(i) ,
3417           lt_autodetail_group_id(i) ,
3418           lt_external_source_code(i) ,
3419           lt_external_source_line_id(i) ,
3420           lt_supply_source_type_id(i) ,
3421           lt_supply_source_header_id(i) ,
3422           lt_supply_source_line_id(i) ,
3423           lt_supply_source_line_detail(i) ,
3424           lt_supply_source_name(i) ,
3425           lt_revision(i) ,
3426           lt_subinventory_code(i) ,
3427           lt_subinventory_id(i) ,
3428           lt_locator_id(i) ,
3429           lt_lot_number(i) ,
3430           lt_lot_number_id(i) ,
3431           lt_serial_number(i) ,
3432           lt_serial_number_id(i) ,
3433           lt_partial_quantities_allowed(i) ,
3434           lt_auto_detailed(i) ,
3435           lt_pick_slip_number(i) ,
3436           lt_lpn_id(i) ,
3437           lt_last_update_date(i) ,
3438           lt_last_updated_by(i) ,
3439           lt_creation_date(i) ,
3440           lt_created_by(i) ,
3441           lt_last_update_login(i) ,
3442           lt_request_id(i) ,
3443           lt_program_application_id(i) ,
3444           lt_program_id(i) ,
3445           lt_program_update_date(i) ,
3446           lt_attribute_category(i) ,
3447           lt_attribute1(i) ,
3448           lt_attribute2(i) ,
3449           lt_attribute3(i) ,
3450           lt_attribute4(i) ,
3451           lt_attribute5(i) ,
3452           lt_attribute6(i) ,
3453           lt_attribute7(i) ,
3454           lt_attribute8(i) ,
3455           lt_attribute9(i) ,
3456           lt_attribute10(i) ,
3457           lt_attribute11(i) ,
3458           lt_attribute12(i) ,
3459           lt_attribute13(i) ,
3460           lt_attribute14(i) ,
3461           lt_attribute15(i) ,
3462           lt_ship_ready_flag(i) ,
3463           lt_n_column1(i) ,
3464           lt_detailed_quantity(i) ,
3465           lt_cost_group_id(i) ,
3466           lt_container_lpn_id(i) ,
3467           lt_staged_flag(i) ,
3468           lt_secondary_detailed_quantity(i) ,
3469           lt_sec_res_qty(i) ,
3470           lt_secondary_uom_code(i) ,
3471           lt_secondary_uom_id(i) ,
3472           lt_crossdock_flag(i) ,
3473           lt_crossdock_criteria_id(i) ,
3474           lt_demand_source_line_detail(i),
3475           lt_serial_reservation_quantity(i) ,
3476           lt_supply_receipt_date(i) ,
3477           lt_demand_ship_date(i) ,
3478           lt_exception_code(i) ,
3479           lt_orig_supply_source_type_id(i) ,
3480           lt_orig_sup_src_hdr_id(i) ,
3481           lt_orig_supply_source_line_id(i) ,
3482           lt_orig_sup_src_line_det(i) ,
3483           lt_orig_demand_source_type_id(i) ,
3484           lt_orig_dd_src_hdr_id(i) ,
3485           lt_orig_demand_source_line_id(i) ,
3486           lt_orig_dd_src_line_det(i) ,
3487           lt_project_id(i) ,
3488           lt_task_id(i)
3489         );
3490 
3491     ELSE
3492       print_Debug('No reservation records exists for any line in current wave ',l_debug);
3493     END IF;
3494 
3495     CLOSE c_reservations;
3496 
3497   ELSIF p_action = 'R' THEN
3498 
3499     DELETE
3500     FROM mtl_reservations
3501     WHERE demand_source_line_id IN
3502       (SELECT DISTINCT wdd.source_line_id
3503       FROM WMS_WP_WAVE_LINES wwl,
3504         wsh_delivery_details wdd--11775489 get from wdd... FIXED
3505       WHERE wave_header_id       = p_wave_id
3506       AND wwl.delivery_detail_id = wdd.delivery_detail_id
3507       AND wdd.organization_id    = wwl.organization_id
3508       );
3509 
3510     INSERT
3511     INTO mtl_reservations
3512       (
3513         reservation_id ,
3514         requirement_date,
3515         organization_id,
3516         inventory_item_id,
3517         demand_source_type_id,
3518         demand_source_name,
3519         demand_source_header_id,
3520         demand_source_line_id,
3521         demand_source_delivery,
3522         primary_uom_code,
3523         primary_uom_id,
3524         reservation_uom_code ,
3525         reservation_uom_id,
3526         reservation_quantity,
3527         primary_reservation_quantity,
3528         autodetail_group_id,
3529         external_source_code,
3530         external_source_line_id,
3531         supply_source_type_id,
3532         supply_source_header_id,
3533         supply_source_line_id,
3534         supply_source_line_detail,
3535         supply_source_name ,
3536         revision,
3537         subinventory_code,
3538         subinventory_id,
3539         locator_id,
3540         lot_number ,
3541         lot_number_id ,
3542         serial_number,
3543         serial_number_id ,
3544         partial_quantities_allowed ,
3545         auto_detailed ,
3546         pick_slip_number ,
3547         lpn_id ,
3548         last_update_date,
3549         last_updated_by,
3550         creation_date ,
3551         created_by,
3552         last_update_login ,
3553         request_id ,
3554         program_application_id,
3555         program_id ,
3556         program_update_date,
3557         attribute_category,
3558         attribute1 ,
3559         attribute2 ,
3560         attribute3 ,
3561         attribute4 ,
3562         attribute5 ,
3563         attribute6 ,
3564         attribute7 ,
3565         attribute8 ,
3566         attribute9 ,
3567         attribute10 ,
3568         attribute11 ,
3569         attribute12 ,
3570         attribute13 ,
3571         attribute14 ,
3572         attribute15 ,
3573         ship_ready_flag,
3574         n_column1,
3575         detailed_quantity,
3576         cost_group_id ,
3577         container_lpn_id ,
3578         staged_flag ,
3579         secondary_detailed_quantity ,
3580         secondary_reservation_quantity,
3581         secondary_uom_code,
3582         secondary_uom_id,
3583         crossdock_flag,
3584         crossdock_criteria_id,
3585         demand_source_line_detail,
3586         serial_reservation_quantity,
3587         supply_receipt_date,
3588         demand_ship_date ,
3589         exception_code,
3590         orig_supply_source_type_id,
3591         orig_supply_source_header_id,
3592         orig_supply_source_line_id,
3593         orig_supply_source_line_detail ,
3594         orig_demand_source_type_id,
3595         orig_demand_source_header_id,
3596         orig_demand_source_line_id,
3597         orig_demand_source_line_detail,
3598         project_id,
3599         task_id
3600       )
3601     SELECT reservation_id ,
3602       requirement_date,
3603       organization_id,
3604       inventory_item_id,
3605       demand_source_type_id,
3606       demand_source_name,
3607       demand_source_header_id,
3608       demand_source_line_id,
3609       demand_source_delivery,
3610       primary_uom_code,
3611       primary_uom_id,
3612       reservation_uom_code ,
3613       reservation_uom_id,
3614       reservation_quantity,
3615       primary_reservation_quantity,
3616       autodetail_group_id,
3617       external_source_code,
3618       external_source_line_id,
3619       supply_source_type_id,
3620       supply_source_header_id,
3621       supply_source_line_id,
3622       supply_source_line_detail,
3623       supply_source_name ,
3624       revision,
3625       subinventory_code,
3626       subinventory_id,
3627       locator_id,
3628       lot_number ,
3629       lot_number_id ,
3630       serial_number,
3631       serial_number_id ,
3632       partial_quantities_allowed ,
3633       auto_detailed ,
3634       pick_slip_number ,
3635       lpn_id ,
3636       last_update_date,
3637       last_updated_by,
3638       creation_date ,
3639       created_by,
3640       last_update_login ,
3641       request_id ,
3642       program_application_id,
3643       program_id ,
3644       program_update_date,
3645       attribute_category,
3646       attribute1 ,
3647       attribute2 ,
3648       attribute3 ,
3649       attribute4 ,
3650       attribute5 ,
3651       attribute6 ,
3652       attribute7 ,
3653       attribute8 ,
3654       attribute9 ,
3655       attribute10 ,
3656       attribute11 ,
3657       attribute12 ,
3658       attribute13 ,
3659       attribute14 ,
3660       attribute15 ,
3661       ship_ready_flag,
3662       n_column1,
3663       detailed_quantity,
3664       cost_group_id ,
3665       container_lpn_id ,
3666       staged_flag ,
3667       secondary_detailed_quantity ,
3668       secondary_reservation_quantity,
3669       secondary_uom_code,
3670       secondary_uom_id,
3671       crossdock_flag,
3672       crossdock_criteria_id,
3673       demand_source_line_detail,
3674       serial_reservation_quantity,
3675       supply_receipt_date,
3676       demand_ship_date ,
3677       exception_code,
3678       orig_supply_source_type_id,
3679       orig_supply_source_header_id,
3680       orig_supply_source_line_id,
3681       orig_supply_source_line_detail ,
3682       orig_demand_source_type_id,
3683       orig_demand_source_header_id,
3684       orig_demand_source_line_id,
3685       orig_demand_source_line_detail,
3686       project_id,
3687       task_id
3688     FROM wms_wp_reservations_gtmp;
3689   END IF;
3690 
3691   COMMIT;
3692   x_return_status := FND_API.G_RET_STS_SUCCESS;
3693 
3694 EXCEPTION
3695 
3696 WHEN OTHERS THEN
3697   print_debug('Error in process_reservation. Action is '||p_action, l_debug);
3698   x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3699 
3700 END process_reservations;
3701 
3702 
3703 
3704 PROCEDURE spawn_workers
3705     (p_organization_id    number,
3706      p_wave_id            number,
3707      p_mo_header_id       number,
3708      p_mode               VARCHAR2,
3709      p_num_workers        number,
3710      x_return_status  OUT NOCOPY VARCHAR2
3711     ) IS
3712    -- l_api_name     VARCHAR2(30) := 'spawn_workers';
3713     l_msg_count    NUMBER;
3714     l_msg_data     VARCHAR2(2000);
3715 
3716     l_sub_request  BOOLEAN      := TRUE;
3717     l_request_id   NUMBER;
3718     l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
3719 
3720     l_phase      VARCHAR2(100);
3721     l_status     VARCHAR2(100);
3722     l_dev_phase  VARCHAR2(100);
3723     l_dev_status VARCHAR2(100);
3724     l_message    VARCHAR2(500);
3725      l_mode                   VARCHAR2(4) := null;
3726     l_request_data           VARCHAR2(100);
3727 
3728   --  l_result     boolean;
3729 
3730 
3731     l_dummy             boolean;
3732     l_completion_status VARCHAR2(100);
3733 
3734   --     l_mode                   VARCHAR2(4) := null;
3735   --     l_request_data           VARCHAR2(100);
3736   --     parent_request_id number;
3737 
3738 
3739   BEGIN
3740 
3741     l_mode         := NULL;
3742     l_request_data := FND_CONC_GLOBAL.Request_Data;
3743     if l_request_data is NULL then
3744 
3745 
3746     PRINT_DEBUG('In spawn_workers ',l_debug);
3747     x_return_status := fnd_api.g_ret_sts_success;
3748 
3749  --   l_sub_request := TRUE;
3750  --   print_debug ('Sub request is TRUE.', l_debug);
3751 
3752     FOR i IN 1..p_num_workers LOOP -- {
3753                   print_debug ('Submitting worker #: ' || i, l_debug);
3754 
3755        l_request_id :=
3756           FND_REQUEST.Submit_Request( application => 'WMS'
3757                                     , program     => 'WMSWPRBS'
3758                                   --  , description => ''
3759                                   --  , start_time  => ''
3760                                 --    , sub_request => l_sub_request
3761                                     , argument1   => p_organization_id
3762                                     , argument2   => p_wave_id
3763                                     , argument3   => p_mo_header_id
3764                                     , argument4   => p_mode
3765                                     , argument5   => i     -- Worker ID
3766                                     );
3767 
3768  COMMIT;
3769         IF l_request_id = 0 THEN
3770                  print_debug( 'Request submission failed for worker ' || i, l_debug);
3771                  RAISE fnd_api.g_exc_unexpected_error;
3772         ELSE
3773                  print_debug( 'Request ' || l_request_id ||' submitted successfully' , l_debug);
3774         END IF;
3775     END LOOP; --}
3776 
3777 
3778 
3779 
3780   --  IF l_sub_request THEN
3781 
3782           print_debug ('Setting Parent Request to pause' , l_debug);
3783 
3784        FND_CONC_GLOBAL.Set_Req_Globals( Conc_Status  => 'PAUSED'
3785                                       , Request_Data => p_organization_id   ||':'||
3786                                                         p_wave_id ||':'||
3787                                                         p_mo_header_id ||':'||
3788                                                         p_mode);
3789   --  END IF;
3790 end if;
3791 
3792   EXCEPTION
3793     WHEN OTHERS THEN
3794       print_debug ('Other error: ' || SQLERRM, l_debug);
3795       x_return_status := fnd_api.g_ret_sts_unexp_error;
3796 
3797 END spawn_workers;
3798 
3799 PROCEDURE fetch_next_wpr_rec
3800       (  p_organization_id    IN number
3801        , p_wave_id            IN number
3802        , p_mo_header_id       IN NUMBER
3803        , p_mode               IN VARCHAR2
3804        , x_inventory_item_id  OUT NOCOPY number
3805        , x_return_status      OUT NOCOPY VARCHAR2
3806        ) IS
3807 
3808  --   l_api_name       VARCHAR2(30) := 'fetch_next_wpr_rec';
3809     l_msg_count      NUMBER;
3810     l_msg_data       VARCHAR2(2000);
3811 
3812     l_inventory_item_id  number;
3813     l_return_status  VARCHAR2(1);
3814     l_row_id         ROWID;
3815     done             BOOLEAN := FALSE;
3816     l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
3817 
3818     record_locked_exc      EXCEPTION;
3819     PRAGMA EXCEPTION_INIT  (record_locked_exc, -54);
3820 
3821   BEGIN
3822     x_return_status := fnd_api.g_ret_sts_success;
3823 
3824     SAVEPOINT fetch_wpr_sp;
3825 
3826     print_debug( 'Entered fetch_next_wpr_rec with parameters: '                       || g_newline ||
3827                     'p_organization_id => '            || TO_CHAR(p_organization_id)  || g_newline ||
3828                     'p_wave_id => '                    || TO_CHAR(p_wave_id)          || g_newline ||
3829                     'p_mo_header_id => '               || TO_CHAR(p_mo_header_id)     || g_newline ||
3830                     'p_mode => '                       || TO_CHAR(p_mode)
3831                     ,l_debug);
3832 
3833   IF p_mode = 'RBP-SS' THEN
3834     begin
3835           UPDATE wms_pr_workers
3836           SET processed_flag = 'Y'
3837           WHERE organization_id  = p_organization_id
3838           AND batch_id          = p_wave_id
3839           AND mo_header_id     = p_mo_header_id
3840           AND processed_flag = 'N'
3841           AND worker_mode   = 'RBP'
3842           AND transaction_batch_id IS NULL;
3843 
3844           IF SQL%ROWCOUNT = 0 THEN
3845            print_debug ('No more records', l_debug);
3846            x_return_status := 'N';
3847            RETURN;
3848           END IF;
3849 
3850           l_inventory_item_id := NULL;
3851           COMMIT;
3852 
3853     END;
3854   ELSIF p_mode = 'RBP' THEN
3855     LOOP --{
3856        EXIT WHEN done;
3857        BEGIN
3858            SELECT rowid INTO l_row_id
3859            FROM wms_pr_workers
3860            WHERE organization_id  = p_organization_id
3861              AND batch_id          = p_wave_id
3862              AND mo_header_id     = p_mo_header_id
3863              AND processed_flag = 'N'
3864              AND worker_mode   = 'RBP'
3865              AND transaction_batch_id IS NOT NULL
3866              AND rownum < 2
3867              FOR UPDATE NOWAIT;
3868 
3869 
3870 
3871           done := TRUE;
3872 
3873           UPDATE wms_pr_workers
3874              SET processed_flag = 'Y'
3875            WHERE rowid = l_row_id
3876           RETURNING transaction_batch_id  -- inventory_item_id
3877                INTO l_inventory_item_id;
3878 
3879           COMMIT;
3880 
3881 
3882          print_debug
3883              ( 'Successfully locked a WPR row: ' || g_newline ||
3884              ' l_inventory_item_id: '            || TO_CHAR(l_inventory_item_id)
3885              , l_debug
3886              );
3887 
3888        EXCEPTION
3889           WHEN record_locked_exc THEN
3890              print_debug ('Record locked', l_debug);
3891              done := FALSE;
3892           WHEN NO_DATA_FOUND THEN
3893              print_debug ('No more records', l_debug);
3894              x_return_status := 'N';
3895              done := TRUE;
3896           WHEN OTHERS THEN
3897              print_debug ('Other error: ' || SQLERRM, l_debug);
3898              done := TRUE;
3899              RAISE fnd_api.g_exc_unexpected_error;
3900        END;
3901 
3902     END LOOP; --}
3903   ELSE
3904     print_debug ('Invalid Mode ', l_debug);
3905     RAISE fnd_api.g_exc_unexpected_error;
3906 
3907   END IF;
3908 
3909     IF x_return_status = fnd_api.g_ret_sts_success THEN
3910        x_inventory_item_id := l_inventory_item_id;
3911     END IF;
3912 
3913   EXCEPTION
3914     WHEN OTHERS THEN
3915       ROLLBACK TO fetch_wpr_sp;
3916       x_return_status := fnd_api.g_ret_sts_unexp_error;
3917       print_debug ('Other error: ' || SQLERRM, l_debug);
3918 
3919 END fetch_next_wpr_rec;
3920 
3921 
3922 PROCEDURE allocation (errbuf              OUT   NOCOPY   VARCHAR2,
3923                       retcode             OUT   NOCOPY   NUMBER,
3924                       p_organization_id IN NUMBER,
3925                       p_wave_id        IN NUMBER,
3926                       p_mo_header_id    IN NUMBER,
3927                       p_mode            IN VARCHAR2,
3928                       p_worker_id       IN NUMBER DEFAULT NULL
3929                      ) IS
3930 
3931 l_print_mode VARCHAR2(1); --used for print pick slip which is not required
3932 l_grouping_rule_id NUMBER; -- get its value from shipping parameter however it is used for print pick slip which is not required
3933 l_detail_rec_count NUMBER;
3934 l_mo_line_tbl        inv_move_order_pub.Trolin_Tbl_Type;
3935 l_mo_line            INV_Move_Order_PUB.TROLIN_REC_TYPE;
3936 
3937 l_return_status          VARCHAR2(3);
3938 l_msg_count          number;
3939 l_msg_data           varchar2(1000);
3940 l_conc_ret_status    BOOLEAN;
3941 l_error_message      VARCHAR2(2000);
3942 
3943 l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
3944 
3945 
3946 v_inventory_item_id   NUMBER;
3947 
3948 l_line_index NUMBER;
3949 l_start_index     NUMBER;
3950 l_cur_txn_source_line_id NUMBER;
3951 l_quantity                  NUMBER;
3952 l_quantity_delivered NUMBER;
3953 l_cur_ship_set_id NUMBER := NULL;
3954 l_cur_ship_model_id      NUMBER := NULL;
3955 l_cur_txn_source_qty     NUMBER;
3956 l_lower_tolerance NUMBER;
3957 l_transaction_quantity  NUMBER;
3958 l_model_reloop           BOOLEAN := FALSE;
3959 l_set_index       NUMBER;
3960 l_cur_txn_source_req_qty NUMBER;
3961 l_txn_source_line_uom    VARCHAR2(3);
3962 l_new_model_quantity     NUMBER;
3963 l_set_txn_source_line_id NUMBER := NULL;
3964 l_set_new_req_qty        NUMBER;
3965 l_set_txn_source_req_qty NUMBER;
3966 l_new_line_quantity      NUMBER;
3967 l_return_value        BOOLEAN := TRUE;
3968 l_primary_uom VARCHAR2(3);
3969 l_reservable_type          NUMBER;
3970 
3971 
3972 CURSOR c1 IS
3973 SELECT
3974 attribute1, attribute10, attribute11, attribute12, attribute13,
3975 attribute14, attribute15, attribute2, attribute3, attribute4,
3976 attribute5, attribute6, attribute7, attribute8, attribute9,
3977 attribute_category, created_by, creation_date, date_required, from_locator_id,
3978 from_subinventory_code, from_subinventory_id, header_id, inventory_item_id, last_updated_by,
3979 last_update_date, last_update_login, line_id, line_number, line_status,
3980 lot_number, organization_id, program_application_id, program_id, program_update_date,
3981 project_id, quantity, quantity_delivered, quantity_detailed, reason_id,
3982 reference, reference_id, reference_type_code, request_id, revision,
3983 serial_number_end, serial_number_start, status_date, task_id, to_account_id,
3984 to_locator_id, to_subinventory_code, to_subinventory_id, transaction_header_id, transaction_type_id,
3985 txn_source_id, txn_source_line_id, txn_source_line_detail_id, transaction_source_type_id, primary_quantity,
3986 to_organization_id, pick_strategy_id, put_away_strategy_id, uom_code, unit_number,
3987 ship_to_location_id, from_cost_group_id, to_cost_group_id, lpn_id, to_lpn_id,
3988 pick_methodology_id, container_item_id, carton_grouping_id, FND_API.G_MISS_CHAR, FND_API.G_MISS_CHAR,
3989 FND_API.G_MISS_CHAR, inspection_status, wms_process_flag, pick_slip_number, pick_slip_date,
3990 ship_set_id, ship_model_id, model_quantity, required_quantity, secondary_quantity,
3991 secondary_uom_code, secondary_quantity_detailed, secondary_quantity_delivered, grade_code, secondary_required_quantity
3992 FROM mtl_txn_request_lines mtrl, wms_wp_rules_simulation wwr
3993 WHERE mtrl.TXN_SOURCE_LINE_DETAIL_ID = wwr.delivery_detail_id
3994 AND p_mode = 'RBP'
3995 and wwr.wave_header_id=p_wave_id
3996 AND mtrl.inventory_item_id = v_inventory_item_id
3997 
3998 UNION
3999 SELECT
4000 mtrl.attribute1, mtrl.attribute10, mtrl.attribute11, mtrl.attribute12, mtrl.attribute13,
4001 mtrl.attribute14, mtrl.attribute15, mtrl.attribute2, mtrl.attribute3, mtrl.attribute4,
4002 mtrl.attribute5, mtrl.attribute6, mtrl.attribute7, mtrl.attribute8, mtrl.attribute9,
4003 mtrl.attribute_category, mtrl.created_by, mtrl.creation_date, mtrl.date_required, mtrl.from_locator_id,
4004 mtrl.from_subinventory_code, mtrl.from_subinventory_id, mtrl.header_id, mtrl.inventory_item_id, mtrl.last_updated_by,
4005 mtrl.last_update_date, mtrl.last_update_login, mtrl.line_id, mtrl.line_number, mtrl.line_status,
4006 mtrl.lot_number, mtrl.organization_id, mtrl.program_application_id, mtrl.program_id, mtrl.program_update_date,
4007 mtrl.project_id, mtrl.quantity, mtrl.quantity_delivered, mtrl.quantity_detailed, mtrl.reason_id,
4008 mtrl.reference, mtrl.reference_id, mtrl.reference_type_code, mtrl.request_id, mtrl.revision,
4009 mtrl.serial_number_end, mtrl.serial_number_start, mtrl.status_date, mtrl.task_id, mtrl.to_account_id,
4010 mtrl.to_locator_id, mtrl.to_subinventory_code, mtrl.to_subinventory_id, mtrl.transaction_header_id, mtrl.transaction_type_id,
4011 mtrl.txn_source_id, mtrl.txn_source_line_id, mtrl.txn_source_line_detail_id, mtrl.transaction_source_type_id, mtrl.primary_quantity,
4012 mtrl.to_organization_id, mtrl.pick_strategy_id, mtrl.put_away_strategy_id, mtrl.uom_code, mtrl.unit_number,
4013 mtrl.ship_to_location_id, mtrl.from_cost_group_id, mtrl.to_cost_group_id, mtrl.lpn_id, mtrl.to_lpn_id,
4014 mtrl.pick_methodology_id, mtrl.container_item_id, mtrl.carton_grouping_id, FND_API.G_MISS_CHAR, FND_API.G_MISS_CHAR,
4015 FND_API.G_MISS_CHAR, mtrl.inspection_status, mtrl.wms_process_flag, mtrl.pick_slip_number, mtrl.pick_slip_date,
4016 mtrl.ship_set_id, mtrl.ship_model_id, mtrl.model_quantity, mtrl.required_quantity, mtrl.secondary_quantity,
4017 mtrl.secondary_uom_code, mtrl.secondary_quantity_detailed, mtrl.secondary_quantity_delivered, mtrl.grade_code, mtrl.secondary_required_quantity
4018 FROM mtl_txn_request_lines mtrl, wms_wp_rules_simulation wwr, wsh_delivery_details wdd
4019 WHERE mtrl.TXN_SOURCE_LINE_DETAIL_ID = wwr.delivery_detail_id
4020 AND wwr.delivery_detail_id = wdd.delivery_detail_id
4021 AND p_mode = 'RBP-SS'
4022 and wwr.wave_header_id=p_wave_id
4023 AND (wdd.SHIP_SET_ID IS NOT NULL OR Nvl(wdd.SHIP_MODEL_COMPLETE_FLAG, 'N') = 'Y')
4024 ORDER BY line_id
4025 ;
4026 
4027 
4028 
4029 BEGIN
4030 
4031 print_debug('Enter Allocation ',l_debug);
4032 retcode := 0;
4033 
4034 
4035        print_debug( 'Entered Allocation with parameters: '                           || g_newline ||
4036                     'p_organization_id => '        || TO_CHAR(p_organization_id)     || g_newline ||
4037                     'p_wave_id => '                || TO_CHAR(p_wave_id)             || g_newline ||
4038                     'p_mo_header_id => '           || TO_CHAR(p_mo_header_id)        || g_newline ||
4039                     'p_mode => '                   || TO_CHAR(p_mode)                || g_newline ||
4040                     'p_worker_id => '              || TO_CHAR(p_worker_id)
4041                     ,l_debug);
4042 
4043 --x_return_status := fnd_api.g_ret_sts_success;
4044 
4045    LOOP --{
4046        l_return_status := fnd_api.g_ret_sts_success;
4047        fetch_next_wpr_rec
4048        ( p_organization_id    => p_organization_id
4049        , p_wave_id            => p_wave_id
4050        , p_mo_header_id       => p_mo_header_id
4051        , p_mode               => p_mode
4052        , x_inventory_item_id  => v_inventory_item_id
4053        , x_return_status      => l_return_status
4054        );
4055 
4056        print_debug('Return status after fetch_next_wpr_rec '||l_return_status, l_debug);
4057        IF l_return_status = 'N' THEN
4058           print_debug ( 'No more records in WPR', l_debug );
4059           EXIT;
4060        ELSIF l_return_status <> fnd_api.g_ret_sts_success THEN
4061           print_debug('Error status from fetch_next_wpr_rec: ' || l_return_status, l_debug);
4062           RAISE fnd_api.g_exc_unexpected_error;
4063        END IF;
4064 
4065        OPEN c1;
4066        FETCH c1 BULK COLLECT INTO l_mo_line_tbl;
4067        print_debug('l_mo_line_tbl.count '||l_mo_line_tbl.Count, l_debug);
4068 
4069        l_line_index      := l_mo_line_tbl.FIRST;
4070        --FOR i IN l_mo_line_tbl.first..l_mo_line_tbl.last LOOP
4071        LOOP
4072         l_mo_line := l_mo_line_tbl(l_line_index);
4073         IF p_mode = 'RBP-SS' THEN
4074           IF l_mo_line.ship_set_id IS NOT NULL AND
4075                     (l_cur_ship_set_id IS NULL OR l_cur_ship_set_id <> l_mo_line.ship_set_id) THEN
4076             SAVEPOINT SHIPSET;
4077 	          l_cur_ship_set_id := l_mo_line.ship_set_id;
4078             l_start_index     := l_line_index;
4079             --l_start_process   := l_processed_row_count;
4080 	          print_debug('Start Shipset :' || l_cur_ship_set_id, l_debug);
4081 	        ELSIF l_cur_ship_set_id IS NOT NULL AND l_mo_line.ship_set_id IS NULL THEN
4082             print_debug('End of Shipset :' || l_cur_ship_set_id, l_debug);
4083 	          l_cur_ship_set_id := NULL;
4084           END IF;
4085 
4086           IF l_mo_line.ship_model_id IS NOT NULL AND
4087 	                  (l_cur_ship_model_id IS NULL OR l_cur_ship_model_id <> l_mo_line.ship_model_id) THEN
4088             SAVEPOINT SHIPMODEL;
4089 	          l_cur_ship_model_id := l_mo_line.ship_model_id;
4090             l_start_index     := l_line_index;
4091             --l_start_process   := l_processed_row_count;
4092 	          print_debug('Start Ship Model :' || l_cur_ship_model_id, l_debug);
4093           ELSIF l_cur_ship_model_id IS NOT NULL AND l_mo_line.ship_model_id IS NULL THEN
4094             print_debug('End of Ship Model :' || l_cur_ship_model_id, l_debug);
4095             l_cur_ship_model_id := NULL;
4096           END IF;
4097 
4098           IF l_cur_txn_source_line_id IS NULL OR l_mo_line.txn_source_line_id <> l_cur_txn_source_line_id THEN
4099             l_cur_txn_source_line_id := l_mo_line.txn_source_line_id;
4100             l_cur_txn_source_qty     := 0;
4101             --l_cur_txn_source_qty2 := 0;
4102             print_debug('Set Current Txn Src Line:' || l_cur_txn_source_line_id, l_debug);
4103           END IF;
4104         END IF;
4105 
4106        BEGIN
4107          print_debug('Calling process_line for Move Order line id '||l_mo_line.line_id, l_debug);
4108 
4109          IF l_mo_line.quantity > 0 THEN
4110             SAVEPOINT allocation;
4111 
4112             l_return_value := INV_CACHE.set_item_rec(l_mo_line.organization_id, l_mo_line.inventory_item_id);
4113             IF NOT l_return_value THEN
4114               print_debug('Error setting item cache', 'Inv_Pick_Release_PVT.Process_Line');
4115               raise fnd_api.g_exc_unexpected_error;
4116             End If;
4117             l_reservable_type:= INV_CACHE.item_rec.reservable_type;
4118 
4119             IF l_reservable_type = 2 THEN
4120               print_debug('In wave simulation mode. Update the allocation table with complete quantity fulfilled', l_debug);
4121               UPDATE wms_wp_rules_simulation
4122               SET ALLOCATED_QUANTITY = l_mo_line.quantity,
4123               ALLOCATED_QUANTITY2 = l_mo_line.secondary_quantity
4124               WHERE DELIVERY_DETAIL_ID = l_mo_line.txn_source_line_detail_id
4125              and  wave_header_id=p_wave_id;
4126 
4127             ELSE
4128               INV_Pick_Release_PVT.Process_Line(
4129 	                p_api_version	      => 1.0
4130 	              , p_init_msg_list	    => fnd_api.g_false
4131 	              , p_commit		        => fnd_api.g_false
4132 	              , x_return_status     => l_return_status
4133    	            , x_msg_count         => l_msg_count
4134    	            , x_msg_data          => l_msg_data
4135    	            , p_mo_line_rec       => l_mo_line
4136 	              , p_grouping_rule_id	=> g_PICK_GROUPING_RULE_ID -- set in initialize
4137 	           -- , p_allow_partial_pick => l_allow_partial_pick   -- default value or TRUE if not passed
4138 	              , p_print_mode        => l_print_mode            -- pass null value since for simulation mode it is not required
4139 	              , x_detail_rec_count	=> l_detail_rec_count
4140                 , p_plan_tasks        => TRUE
4141                 , p_wave_simulation_mode  => 'Y'
4142                 );
4143               print_debug('Allocation: return status after INV_Pick_Release_PVT.Process_Line for line_id '||l_mo_line.line_id||': '||l_return_status, l_debug);
4144               /*IF l_return_status <> fnd_api.g_ret_sts_success THEN
4145                   ROLLBACK;
4146               END IF;
4147               */
4148             END IF;
4149           ELSE
4150             print_debug('Requested quantity is zero', l_debug);
4151           END IF;
4152 
4153        EXCEPTION
4154        WHEN others THEN
4155           print_debug('Error in allocation for line_id '||l_mo_line.line_id, l_debug);
4156 
4157           ROLLBACK TO allocation;
4158        END;
4159 
4160        IF p_mode = 'RBP' then
4161         COMMIT;
4162        END IF;
4163 
4164        IF p_MODE ='RBP-SS' THEN
4165           l_quantity              := l_mo_line.quantity;
4166           l_quantity_delivered    := l_mo_line.quantity_delivered;
4167           l_lower_tolerance       := inv_pick_release_pvt.g_min_tolerance;
4168           l_transaction_quantity  := nvl(l_mo_line.quantity_detailed, 0) - nvl(l_mo_line.quantity_delivered, 0);
4169 
4170           IF (l_transaction_quantity < (l_quantity - NVL(l_quantity_delivered,0) - l_lower_tolerance)) THEN
4171                IF l_cur_ship_set_id IS NOT NULL THEN
4172                     ROLLBACK to SHIPSET;
4173                     l_set_index   := l_start_index;
4174                     --l_set_process := l_start_process;
4175                     --loop through all move order lines for this ship set
4176                     LOOP
4177                       l_mo_line := l_mo_line_tbl(l_set_index);
4178                       update mtl_txn_request_lines
4179                       set quantity                = 0,
4180                       quantity_detailed           = 0,
4181                       secondary_quantity          = decode(secondary_quantity, fnd_api.g_miss_num, NULL, 0),
4182                       secondary_quantity_detailed = decode(secondary_quantity_detailed, fnd_api.g_miss_num, NULL, 0),
4183                       line_status                 = 5,
4184                       status_date                 = sysdate
4185                       where line_id = l_mo_line.line_id;
4186 
4187                       EXIT WHEN l_mo_line_tbl.LAST = l_set_index;
4188                       l_set_index := l_mo_line_tbl.NEXT(l_set_index);
4189                       if nvl(l_mo_line_tbl(l_set_index).ship_set_id, -1) <> l_cur_ship_set_id then
4190                           l_set_index := l_mo_line_tbl.PRIOR(l_set_index);
4191                           EXIT;
4192                       end if;
4193 
4194                       --If next line is for same ship set, update output table
4195                       --l_set_process := l_set_process + 1;
4196                     END loop;
4197 
4198                     l_line_index          := l_set_index;
4199                     l_cur_ship_set_id     := NULL;
4200                     --l_processed_row_count := l_set_process;
4201                     l_detail_rec_count    := 0;
4202                     print_debug('Finished processing all lines in shipset', l_debug);
4203 
4204                ELSIF l_cur_ship_model_id IS NOT NULL THEN
4205                     ROLLBACK to SHIPMODEL;
4206                     l_set_index   := l_start_index;
4207                     --l_set_process := l_start_process;
4208                     print_debug('OE Line: ' || l_cur_txn_source_line_id, l_debug);
4209 
4210                     BEGIN
4211                       SELECT ordered_quantity, order_quantity_uom
4212                       INTO l_cur_txn_source_req_qty, l_txn_source_line_uom
4213                       FROM OE_ORDER_LINES_ALL
4214                       WHERE line_id = l_cur_txn_source_line_id;
4215                     EXCEPTION
4216                       WHEN NO_DATA_FOUND then
4217                           print_debug('No Order Line Quantity found', l_debug);
4218                           ROLLBACK;
4219                           RAISE fnd_api.g_exc_unexpected_error;
4220                     END;
4221 
4222                     BEGIN
4223                        SELECT primary_uom_code
4224                        INTO l_primary_uom
4225                        FROM mtl_system_items
4226                        WHERE organization_id = l_mo_line.organization_id
4227                        AND inventory_item_id = l_mo_line.inventory_item_id;
4228                     EXCEPTION
4229                        WHEN no_data_found THEN
4230                           ROLLBACK;
4231                           print_debug('No Item Info found', l_debug);
4232                           RAISE fnd_api.g_exc_unexpected_error;
4233                     END;
4234 
4235                     IF l_txn_source_line_uom <> l_primary_uom THEN
4236                           l_cur_txn_source_req_qty := inv_convert.inv_um_convert(l_mo_line.inventory_item_id,
4237                                                                      NULL,
4238                                                                      l_cur_txn_source_req_qty,
4239                                                                      l_txn_source_line_uom,
4240                                                                      l_primary_uom,
4241                                                                      NULL,
4242                                                                      NULL);
4243                     END IF;
4244 
4245                     l_new_model_quantity := floor(l_cur_txn_source_qty * l_mo_line.model_quantity
4246                                                   / l_cur_txn_source_req_qty);
4247                     print_debug('New model qty ' || l_new_model_quantity, l_debug);
4248 
4249                     LOOP
4250                       l_mo_line := l_mo_line_tbl(l_set_index);
4251                       print_debug('SHIPMODEL-Current mo line:' || l_mo_line.line_id, l_debug);
4252 
4253                       IF l_set_txn_source_line_Id IS NULL OR
4254                                     l_set_txn_source_line_id <> l_mo_line.txn_source_line_id THEN
4255                           l_set_txn_source_line_id := l_mo_line.txn_source_line_id;
4256                           print_debug('OE Line: ' || l_set_txn_source_line_id, l_debug);
4257 
4258                           IF l_set_txn_source_line_id = l_cur_txn_source_line_id Then
4259                               l_set_txn_source_req_qty := l_cur_txn_source_req_qty;
4260                           ELSE
4261                               BEGIN
4262                                 SELECT ordered_quantity, order_quantity_uom
4263                                 INTO l_set_txn_source_req_qty, l_txn_source_line_uom
4264                                 FROM OE_ORDER_LINES_ALL
4265                                 WHERE line_id = l_set_txn_source_line_id;
4266                               EXCEPTION
4267                                 WHEN NO_DATA_FOUND then
4268                                   print_debug('No Order Line Quantity found', l_debug);
4269                                   ROLLBACK;
4270                                   RAISE fnd_api.g_exc_unexpected_error;
4271                               END;
4272 
4273                               if l_txn_source_line_uom <> l_primary_uom then
4274                                   l_cur_txn_source_req_qty := inv_convert.inv_um_convert(l_mo_line.inventory_item_id,
4275                                                                            NULL,
4276                                                                            l_cur_txn_source_req_qty,
4277                                                                            l_txn_source_line_uom,
4278                                                                            l_primary_uom,
4279                                                                            NULL,
4280                                                                            NULL);
4281                               end if;
4282                           END IF;
4283 
4284                           l_set_new_req_qty := l_set_txn_source_req_qty * l_new_model_quantity
4285                                                 / l_mo_line.model_quantity;
4286 
4287                           print_debug('New req qty: ' || l_set_new_req_qty, l_debug);
4288                       END IF;
4289 
4290                       -- set new move order line quantity
4291                       IF l_set_new_req_qty >= l_mo_line.quantity THEN
4292                           l_new_line_quantity := l_mo_line.quantity;
4293                       ELSE
4294                           l_new_line_quantity := l_set_new_req_qty;
4295                           print_debug('New line qty: ' || l_new_line_quantity, l_debug);
4296                           l_return_value := INV_CACHE.set_wdd_rec(l_mo_line.line_id);
4297                           If NOT l_return_value Then
4298                               print_debug('Error setting cache for delivery line',l_debug);
4299                               RAISE fnd_api.g_exc_unexpected_error;
4300                           End If;
4301                       END IF;
4302 
4303                       l_set_new_req_qty := l_set_new_req_qty - l_new_line_quantity;
4304 
4305                       -- Update mo line with new quantity and model quantity;
4306                       --If mo line quantity is 0, close the move order line
4307                       IF l_new_line_quantity = 0 THEN
4308                           update mtl_txn_request_lines
4309                           set quantity          = 0,
4310                           quantity_detailed = 0,
4311                           line_status       = 5,
4312                           status_date       = sysdate,
4313                           model_quantity    = l_new_model_quantity
4314                           where line_id = l_mo_line.line_id;
4315 
4316                           l_mo_line_tbl(l_set_index).quantity_detailed := 0;
4317                           l_mo_line_tbl(l_set_index).line_status := 5;
4318                       ELSE
4319                           update mtl_txn_request_lines
4320                           set quantity          = l_new_line_quantity,
4321                           quantity_detailed = NULL,
4322                           model_quantity    = l_new_model_quantity
4323                           where line_id = l_mo_line.line_id;
4324 
4325                           l_mo_line_tbl(l_set_index).quantity_detailed := NULL;
4326                       END IF;
4327 
4328                       l_mo_line_tbl(l_set_index).quantity := l_new_line_quantity;
4329 
4330                       EXIT WHEN l_mo_line_tbl.LAST = l_set_index;
4331                       l_set_index := l_mo_line_tbl.NEXT(l_set_index);
4332                       if nvl(l_mo_line_tbl(l_set_index).ship_model_id, -99) <> l_cur_ship_model_id then
4333                           l_set_index := l_mo_line_tbl.PRIOR(l_set_index);
4334                           EXIT;
4335                       end if;
4336 
4337                     END LOOP;
4338 
4339                     l_cur_ship_model_id := NULL;
4340                     l_cur_txn_source_qty := 0;
4341                     --l_cur_txn_source_qty2    := 0;
4342                     l_cur_txn_source_line_id := NULL;
4343 
4344                     IF l_new_model_quantity = 0 THEN
4345                           l_line_index          := l_set_index;
4346                           --l_processed_row_count := l_set_process;
4347                           l_detail_rec_count    := 0;
4348 
4349                           print_debug('Backordered all lines with this Ship Model Id', l_debug);
4350 
4351                     ELSE
4352 
4353                           l_line_index          := l_start_index;
4354                           --l_processed_row_count := l_start_process;
4355                           l_model_reloop        := TRUE;
4356                     END IF;
4357 
4358                END IF;
4359           END if;
4360        END IF;
4361 
4362        EXIT WHEN l_line_index = l_mo_line_tbl.last AND(l_model_reloop <> TRUE);
4363        IF (l_model_reloop <> TRUE) THEN
4364             l_line_index := l_mo_line_tbl.NEXT(l_line_index);
4365        ELSE
4366             --Don't increment the line index and turn off the reloop variable
4367             l_model_reloop := FALSE;
4368        END IF;
4369 
4370        END LOOP;
4371        CLOSE c1;
4372    END loop;
4373 
4374    COMMIT;
4375 
4376 EXCEPTION
4377   WHEN OTHERS THEN
4378       l_error_message := SQLERRM;
4379       print_debug ('Error in allocation '||l_error_message, l_debug);
4380       l_conc_ret_status := fnd_concurrent.set_completion_status('ERROR', l_error_message);
4381       retcode := 2;
4382       errbuf  := l_error_message;
4383 
4384 END allocation;
4385 PROCEDURE init_allocation(p_organization_id  number,
4386                            p_wave_id      number,
4387                            p_mo_header_id number,
4388                            x_return_status  out nocopy varchar2
4389                            )IS
4390  l_num_workers   NUMBER;
4391  l_tot_worker_records NUMBER := 0;
4392  l_tot_smc_records NUMBER;
4393  l_tbl_count number;
4394  --l_select_clause  VARCHAR2(1000);
4395  --l_from_clause   VARCHAR2(1000);
4396  --l_where_clause   VARCHAR2(1000);
4397  --l_groupby_clause VARCHAR2(1000);
4398  --l_final_query varchar2(1000);
4399 
4400  CURSOR c1 IS
4401      SELECT WDD.ORGANIZATION_ID, WDD.INVENTORY_ITEM_ID, COUNT(wwl.wave_line_id)
4402      FROM wms_wp_wave_lines wwl,
4403           wsh_delivery_details wdd	 --11775489 get from wdd... FIXED
4404      WHERE wwl.organization_id = p_organization_id
4405      and wwl.wave_header_id = p_wave_id
4406 	 and wwl.delivery_detail_id = wdd.delivery_detail_id
4407 	 and wdd.organization_id = wwl.organization_id
4408      GROUP BY WDD.ORGANIZATION_ID, WDD.INVENTORY_ITEM_ID;
4409 
4410  CURSOR c2 IS
4411      SELECT Wdd.ORGANIZATION_ID, DECODE(Wdd.SHIP_SET_ID,NULL,DECODE(Wdd.SHIP_MODEL_COMPLETE_FLAG,'Y',NULL,Wdd.INVENTORY_ITEM_ID),NULL), COUNT(*)
4412      FROM wms_wp_rules_simulation wwl, wsh_delivery_details wdd
4413      WHERE wdd.organization_id = p_organization_id
4414      AND wwl.delivery_detail_id = wdd.delivery_detail_id
4415      and wwl.wave_header_id=p_wave_id
4416      GROUP BY Wdd.ORGANIZATION_ID, DECODE(Wdd.SHIP_SET_ID,NULL,DECODE(Wdd.SHIP_MODEL_COMPLETE_FLAG,'Y',NULL,Wdd.INVENTORY_ITEM_ID),NULL);
4417 
4418 
4419 
4420  TYPE cTblorg IS TABLE OF number INDEX BY BINARY_INTEGER;
4421  	 TYPE cTblitem IS TABLE OF number INDEX BY BINARY_INTEGER;
4422  	 	 TYPE cTblcount IS TABLE OF number INDEX BY BINARY_INTEGER;
4423 
4424  	 	 l_cTblorg cTblorg;
4425  	 	 	l_cTblitem cTblitem;
4426  	 	 	l_cTblcount cTblcount;
4427 
4428  l_msg_data  VARCHAR2(1000);
4429  l_retcode NUMBER;
4430 
4431  l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
4432  begin
4433   print_debug('In init_allocation',l_debug);
4434   x_return_status := FND_API.G_RET_STS_SUCCESS;
4435 
4436 
4437    IF g_enforce_ship_set_and_smc = 'N' THEN
4438       OPEN c1;
4439       FETCH c1 BULK COLLECT INTO l_cTblorg,l_cTblitem,l_cTblcount;
4440    ELSE
4441       OPEN c2;
4442       FETCH c2 BULK COLLECT INTO l_cTblorg,l_cTblitem,l_cTblcount;
4443    END IF;
4444 
4445 --l_tbl_count := l_cTbl.count;
4446 
4447  --
4448   --FORall i IN l_cTbl.first..l_cTbl.last dbchange3
4449  FORALL i IN l_cTblorg.first..l_cTblorg.last
4450    INSERT INTO wms_pr_workers
4451            (batch_id,
4452             worker_mode,
4453             processed_flag,
4454             organization_id,
4455             mo_header_id,
4456             transaction_batch_id,
4457             detailed_count
4458            )
4459    VALUES (p_wave_id,
4460            'RBP',
4461            'N',
4462            l_cTblorg(i),
4463            p_mo_header_id,
4464            l_cTblitem(i),
4465            l_cTblcount(i)
4466           );
4467 
4468          -- l_tot_worker_records := l_tot_worker_records + SQL%ROWCOUNT;
4469 
4470 
4471     l_tot_worker_records := SQL%ROWCOUNT;
4472     print_debug(l_tot_worker_records ||' no of rows inserted into wms_pr_workers', l_debug);
4473 
4474   SELECT Count(*) INTO l_tot_smc_records
4475   FROM wms_pr_workers
4476   WHERE organization_id=p_organization_id
4477   AND batch_id=p_wave_id
4478   and mo_header_id=p_mo_header_id
4479   AND worker_mode='RBP'
4480   AND processed_flag='N'
4481   AND transaction_batch_id IS NULL;
4482 
4483   IF l_tot_smc_records > 1 THEN
4484     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4485     RETURN;
4486   ELSIF l_tot_smc_records = 1 THEN
4487   --SS/SMC batch, lock record and call allocation directly in parent thread
4488 
4489            allocation(errbuf            => l_msg_data,
4490                       retcode           => l_retcode,
4491                       p_organization_id => p_organization_id,
4492                       p_wave_id         => p_wave_id,
4493                       p_mo_header_id    => p_mo_header_id,
4494                       p_mode            => 'RBP-SS'
4495                      );
4496 
4497            print_debug('return status after init_allocation '|| l_retcode, l_debug);
4498            IF l_retcode <> 0 THEN
4499               RAISE fnd_api.g_exc_unexpected_error;
4500            END IF;
4501 
4502   END IF;
4503 
4504     l_tot_worker_records := l_tot_worker_records - l_tot_smc_records;
4505 
4506     l_num_workers := NVL(fnd_profile.value('WSH_PR_NUM_WORKERS'),3);
4507     IF l_num_workers < 2 THEN
4508       l_num_workers := 3;
4509     END IF;
4510 
4511     l_num_workers := LEAST(l_tot_worker_records, l_num_workers);
4512 
4513     spawn_workers(p_organization_id => p_organization_id,
4514                   p_wave_id         => p_wave_id,
4515                   p_mo_header_id    => p_mo_header_id,
4516                   p_mode            => 'RBP',
4517                   p_num_workers     => l_num_workers,
4518                   x_return_status   => x_return_status);
4519 
4520     print_debug('return status after spawn_workers '||x_return_status, l_debug);
4521     IF x_return_status <> fnd_api.g_ret_sts_success THEN
4522       RAISE fnd_api.g_exc_unexpected_error;
4523     END IF;
4524 
4525   --  COMMIT;
4526  EXCEPTION
4527   WHEN OTHERS THEN
4528     print_debug ('Error in init_allocation '||SQLERRM, l_debug);
4529     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4530 
4531  END init_allocation;
4532 
4533 
4534 
4535 PROCEDURE post_allocation_processing
4536             (P_ORGANIZATION_ID      NUMBER,
4537              p_move_order_Number    NUMBER,
4538              x_request_id       out nocopy NUMBER,
4539              x_return_status    OUT NOCOPY varchar2) IS
4540 --PROCEDURE post_allocation_processing(P_ORGANIZATION_ID IN NUMBER, p_move_order_Number NUMBER) IS
4541     l_phase      VARCHAR2(100);
4542     l_status     VARCHAR2(100);
4543     l_dev_phase  VARCHAR2(100);
4544     l_dev_status VARCHAR2(100);
4545     l_message    VARCHAR2(500);
4546     l_batch_id   varchar2(30);
4547     l_result     boolean;
4548 
4549     l_request_id        NUMBER;
4550     l_dummy             boolean;
4551     l_completion_status VARCHAR2(100);
4552 
4553        l_mode                   VARCHAR2(4) := null;
4554        l_request_data           VARCHAR2(100);
4555        parent_request_id number;
4556        l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
4557 
4558 
4559   BEGIN
4560 
4561    x_return_status := fnd_api.g_ret_sts_success;
4562 
4563     l_request_id := FND_REQUEST.SUBMIT_REQUEST(application => 'WMS',
4564                                                program     => 'WMSPALOC',
4565                                                description => 'WMS post-allocation processing',
4566                                                start_time  => NULL,
4567                                             --   sub_request => TRUE,
4568                                                argument1   => p_move_order_Number,
4569                                                argument2   => P_ORGANIZATION_ID,
4570                                                argument3   => 1,
4571                                                argument4   => 2,
4572                                                argument5   => 1,
4573                                                argument6   => 1,
4574                                                argument7   => 2,
4575                                                argument8   => 2,
4576                                                argument9   => 1,
4577                                                argument10  => 2,
4578                                                argument11  => 'Y');
4579 
4580                     -- argument3 operation_plan     1
4581                     -- argument5 consolidate tasks  1
4582                     -- argument6 assign task type   1
4583                     -- argument9 plan tasks         1
4584 
4585      x_request_id := l_request_id;
4586 
4587                      commit;
4588 
4589 
4590      l_dummy := FND_CONCURRENT.WAIT_FOR_REQUEST(request_id => l_request_id,
4591                 interval => 5,
4592   --              max_wait => 120,
4593                 phase => l_phase,
4594                 status => l_status,
4595                 dev_phase => l_dev_phase,
4596                 dev_status => l_dev_status,
4597                 message => l_message);
4598 
4599 
4600 
4601 
4602     l_dummy := FND_CONCURRENT.GET_REQUEST_STATUS(l_request_id,
4603                                                  '',
4604                                                  '',
4605                                                  l_phase,
4606                                                  l_status,
4607                                                  l_dev_phase,
4608                                                  l_dev_status,
4609                                                  l_message);
4610 
4611     print_debug('Rule Based simulation: Waiting for Post Allocation request '||l_request_id, l_debug);
4612     print_debug('l_phase '||l_phase||' l_status '||l_status||' l_dev_phase '||l_dev_phase||' l_dev_status '||l_dev_status,l_debug);
4613 
4614     IF l_dev_status = 'WARNING' THEN
4615       l_completion_status := 'WARNING';
4616       x_return_status  := FND_API.G_RET_STS_UNEXP_ERROR;
4617     ELSIF l_dev_status <> 'NORMAL' THEN
4618       l_completion_status := 'ERROR';
4619       x_return_status  := FND_API.G_RET_STS_UNEXP_ERROR;
4620     else
4621       l_completion_status := 'NORMAL';
4622       l_dummy             := FND_CONCURRENT.SET_COMPLETION_STATUS(l_completion_status, '');
4623     END IF;
4624 
4625   EXCEPTION
4626     WHEN OTHERS THEN
4627       print_debug('Error in post_allocation_processing', l_debug);
4628       x_return_status  := FND_API.G_RET_STS_UNEXP_ERROR;
4629   END post_allocation_processing;
4630 
4631 
4632   PROCEDURE fulfillment_labor_planning(p_planning_criteria_id in number,
4633                                      p_wave_header_id       in number,
4634                                      p_move_order_hrd_id    in number,
4635                                      P_ORGANIZATION_ID      in number,
4636                                      x_return_status        out nocopy varchar2) IS
4637 
4638   CURSOR c1 IS
4639     select mtrl.TXN_SOURCE_LINE_DETAIL_ID delivery_detail_id,
4640            sum(transaction_quantity) l_quantity,
4641            sum(secondary_transaction_quantity) l_quantity2
4642       from wms_wp_rules_simulation   wwr,
4643            mtl_material_transactions_temp mmtt,
4644            mtl_txn_request_lines          mtrl
4645      where wwr.delivery_detail_id = mtrl.TXN_SOURCE_LINE_DETAIL_ID
4646      and wwr.wave_header_id=p_wave_header_id
4647        AND mtrl.line_Id = mmtt.move_order_line_id
4648        and (mmtt.parent_line_id IS NULL OR
4649            mmtt.transaction_temp_id <> mmtt.parent_line_id)
4650      group by mtrl.TXN_SOURCE_LINE_DETAIL_ID;
4651 
4652   TYPE wp_rules_rec IS RECORD(
4653     delivery_detail_id number,
4654     allocated_qty      number,
4655     allocated_qty2     number);
4656 
4657   TYPE wp_rules_tbl IS TABLE OF wp_rules_rec INDEX BY BINARY_INTEGER;
4658 
4659   x_wp_rules_tbl wp_rules_tbl;
4660 
4661   l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
4662 
4663   l_enable_labor_planning varchar2(1);
4664   v_labor_count           number;
4665 
4666 BEGIN
4667 
4668   print_debug('In fulfillment_labor_planning ', l_debug);
4669   x_return_status := fnd_api.g_ret_sts_success;
4670 
4671   open c1;
4672 
4673   fetch c1 bulk collect
4674     into x_wp_rules_tbl;
4675   print_debug('x_wp_rules_tbl.count: ' || x_wp_rules_tbl.count, l_debug);
4676 
4677   CLOSE c1;
4678 
4679   /*  FOR i IN x_wp_rules_tbl.first..x_wp_rules_tbl.last LOOP
4680     print_debug('i is '||i,l_debug);
4681       print_debug('delivery_detail_id '||x_wp_rules_tbl(i).delivery_detail_id||'  l_quantity '||x_wp_rules_tbl(i).allocated_qty, l_debug);
4682     END LOOP;
4683   */
4684 
4685  -- forall i in indices of x_wp_rules_tbl
4686  if x_wp_rules_tbl.count >0 then
4687 
4688 
4689  for i in x_wp_rules_tbl.FIRST .. x_wp_rules_tbl.LAST LOOP
4690 
4691  	begin
4692     update wms_wp_rules_simulation
4693        set allocated_quantity  = x_wp_rules_tbl(i).allocated_qty,
4694            allocated_quantity2 = x_wp_rules_tbl(i).allocated_qty2
4695      where delivery_detail_id = x_wp_rules_tbl(i).delivery_detail_id
4696      and wave_header_id=p_wave_header_id;
4697 
4698      EXCEPTION
4699      	when others then
4700      		null;
4701      	end;
4702    end loop;
4703 
4704 end if;
4705 
4706 
4707   print_debug('In fulfillment_labor_planning 4', l_debug);
4708 
4709   ----call labor planning api-----
4710 
4711   select enable_labor_planning
4712     into l_enable_labor_planning
4713     from wms_wp_planning_Criteria_vl
4714    where planning_criteria_id = p_planning_criteria_id;
4715 
4716   print_debug('Checking if Labor Planning Set up or Department Setup  is done ',
4717               l_debug);
4718   select count(1)
4719     into v_labor_count
4720     from wms_wp_labor_planning
4721    where planning_Criteria_id = p_planning_Criteria_id;
4722 
4723   if v_labor_count > 0 and l_enable_labor_planning = 'Y' then
4724 
4725     savepoint labor_planning_sp;
4726 
4727     print_debug('Call Labor Planning API', l_debug);
4728     rules_labor_planning(p_planning_criteria_id,
4729                          p_wave_header_id,
4730                          p_move_order_hrd_id,
4731                          P_ORGANIZATION_ID,
4732                          x_return_status);
4733 
4734     if x_return_status <> 'S' then
4735 
4736       rollback to labor_planning_sp;
4737     else
4738       commit;
4739     end if;
4740 
4741   else
4742 
4743     print_debug('Setup is not available for Labor Planning. Please check the Labor Planning  setup',
4744                 l_debug);
4745     delete from wms_wp_labor_statistics
4746      where wave_header_id = p_wave_header_id;
4747     commit;
4748   end if;
4749 
4750 
4751 
4752 EXCEPTION
4753   WHEN No_Data_Found THEN
4754     print_debug('No Data Found Exception in fulfillment_labor_planning',
4755                 l_debug);
4756     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4757 
4758   WHEN OTHERS THEN
4759     print_debug('Other Exception in fulfillment_labor_planning', l_debug);
4760     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4761 
4762 END fulfillment_labor_planning;
4763 
4764 
4765 
4766 
4767 PROCEDURE clean_up(p_organization_id NUMBER, p_wave_id NUMBER, p_mo_header_id NUMBER, x_return_status OUT NOCOPY varchar2) IS
4768 
4769 
4770  /* TYPE delMMTTRec IS RECORD
4771         (transaction_temp_id  NUMBER,
4772          transaction_header_id  NUMBER,
4773          ITEM_LOT_CONTROL_CODE  NUMBER,
4774          ITEM_SERIAL_CONTROL_CODE number
4775         );
4776 
4777   TYPE delMMTTTab IS TABLE OF delMMTTRec INDEX BY BINARY_INTEGER;
4778   l_del_mmtt delMMTTTab; */
4779 
4780 TYPE delMMTTtempid IS TABLE OF number INDEX BY BINARY_INTEGER;
4781 TYPE delMMTThdrid IS TABLE OF number INDEX BY BINARY_INTEGER;
4782 	TYPE delMMTTlotctrl IS TABLE OF number INDEX BY BINARY_INTEGER;
4783 		TYPE delMMTTserialctrl IS TABLE OF number INDEX BY BINARY_INTEGER;
4784 
4785 
4786 l_delMMTTtempid delMMTTtempid;
4787 l_delMMTThdrid delMMTThdrid;
4788 l_delMMTTlotctrl delMMTTlotctrl;
4789 l_delMMTTserialctrl delMMTTserialctrl;
4790 
4791   CURSOR c_del_mmtt IS
4792   SELECT DISTINCT mmtt1.transaction_temp_id, mmtt1.transaction_header_id, mmtt1.ITEM_LOT_CONTROL_CODE, mmtt1.ITEM_SERIAL_CONTROL_CODE
4793   FROM mtl_material_transactions_temp mmtt1, mtl_material_transactions_temp mmtt2
4794   WHERE mmtt2.move_order_header_id=p_mo_header_id
4795     AND (mmtt1.transaction_temp_id=mmtt2.transaction_temp_id OR mmtt1.transaction_temp_id=mmtt2.parent_line_id);
4796 
4797 
4798   l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
4799   l_tbl_count number;
4800   l_row_count NUMBER := 0;
4801 
4802 
4803   BEGIN
4804   x_return_status := fnd_api.g_ret_sts_success;
4805 
4806 
4807    BEGIN
4808     IF g_update_wdd = 'Y' THEN
4809       UPDATE wsh_delivery_details
4810       SET move_order_line_id=NULL
4811       WHERE delivery_detail_id IN (SELECT delivery_Detail_id
4812                                    FROM wms_wp_rules_simulation where wave_header_id=p_wave_id);
4813 
4814       print_debug(SQL%ROWCOUNT || ' Rows updated from WDD ',l_debug);
4815       g_update_wdd := 'N';
4816       COMMIT;
4817     END IF;
4818     EXCEPTION
4819       WHEN OTHERS THEN
4820         print_debug('Could not update WDD', l_debug);
4821     END;
4822 
4823 
4824    DELETE FROM wms_pr_workers
4825    WHERE batch_id = p_wave_id
4826    AND worker_mode = 'RBP'
4827    AND organization_id = p_organization_id
4828    AND mo_header_id = p_mo_header_id;
4829 
4830    print_debug(SQL%ROWCOUNT || ' Rows deleted from wms_pr_workers ',l_debug);
4831 
4832 
4833    DELETE FROM wms_wp_reservations_gtmp;
4834 
4835 
4836  open c_del_mmtt;
4837  FETCH c_del_mmtt BULK COLLECT INTO l_delMMTTtempid,l_delMMTThdrid,l_delMMTTlotctrl,l_delMMTTserialctrl;
4838 
4839  print_debug(' Number of rows in l_del_mmtt cursor is '||l_delMMTTtempid.count,l_debug);
4840 
4841 l_tbl_count := l_delMMTTtempid.Count;
4842  IF l_delMMTTtempid.Count >0 THEN
4843  FORALL i IN l_delMMTTtempid.FIRST ..l_delMMTTtempid.LAST
4844  --FOR i IN 1 ..l_tbl_count LOOP dbchange4
4845     UPDATE mtl_serial_numbers
4846     SET group_mark_id = NULL, line_mark_id = NULL, lot_line_mark_id = NULL
4847     WHERE l_delMMTTserialctrl(i) IN (2, 5)
4848       AND ((l_delMMTTlotctrl(i) <> 2 AND (group_mark_id = l_delMMTTtempid(i) OR group_mark_id = l_delMMTThdrid(i)))
4849             OR (l_delMMTTlotctrl(i) = 2
4850                 AND (group_mark_id  IN (SELECT serial_transaction_temp_id
4851                                        FROM mtl_transaction_lots_temp
4852                                        WHERE transaction_temp_id = l_delMMTTtempid(i))
4853                      OR group_mark_id = l_delMMTThdrid(i))));
4854 
4855 
4856     print_debug(SQL%ROWCOUNT || ' Rows updated from MSN ',l_debug);
4857 
4858 
4859                   --dbchange5
4860 
4861  --FORALL i IN 1 ..l_tbl_count
4862 
4863 FORALL i IN l_delMMTTtempid.FIRST ..l_delMMTTtempid.LAST
4864     DELETE FROM mtl_serial_numbers_temp
4865     WHERE  l_delMMTTserialctrl(i) IN (2, 5)
4866       AND  ((l_delMMTTlotctrl(i) <> 2
4867                 AND  transaction_temp_id = l_delMMTTtempid(i))
4868             OR (l_delMMTTlotctrl(i) = 2
4869                 AND transaction_temp_id IN (SELECT SERIAL_TRANSACTION_TEMP_ID
4870                                             FROM mtl_transaction_lots_temp
4871                                             WHERE transaction_temp_id = l_delMMTTtempid(i))));
4872 
4873   print_debug(SQL%ROWCOUNT|| ' Rows deleted from MSNT ',l_debug);
4874 
4875 
4876    --FORALL i IN 1 ..l_tbl_count   dbchange6
4877 
4878 FORALL i IN l_delMMTTtempid.FIRST ..l_delMMTTtempid.LAST
4879     DELETE FROM mtl_transaction_lots_temp
4880     WHERE  l_delMMTTlotctrl(i) = 2
4881       AND  transaction_temp_id = l_delMMTTtempid(i);
4882 
4883   print_debug(SQL%ROWCOUNT || ' Rows deleted from MTLT ',l_debug);
4884 
4885  --FORALL i IN 1 ..l_tbl_count  dbchange7
4886 
4887 FORALL i IN l_delMMTTtempid.FIRST ..l_delMMTTtempid.LAST
4888     DELETE FROM mtl_material_transactions_temp
4889     WHERE transaction_temp_id = l_delMMTTtempid(i);
4890 
4891 
4892 
4893   print_debug(SQL%ROWCOUNT || ' Rows deleted from MMTT ',l_debug);
4894 
4895  END IF;
4896 
4897   DELETE FROM mtl_txn_request_Lines WHERE header_id=p_mo_header_id;
4898 
4899   print_debug(SQL%ROWCOUNT || ' Rows deleted from MTRL ', l_debug);
4900 
4901   DELETE FROM mtl_txn_request_headers WHERE header_id=p_mo_header_id;
4902 
4903   print_debug(SQL%ROWCOUNT || ' Rows deleted from MTRH ', l_debug);
4904 
4905   process_reservations(p_wave_id =>  p_wave_id
4906                   , p_action      =>  'R'
4907                   , x_return_status  => x_return_status
4908                   );
4909 
4910 
4911 --delete from wms_wp_rules_simulation;
4912   COMMIT;
4913 
4914  EXCEPTION
4915   WHEN OTHERS THEN
4916     print_debug('Error in clean_up', l_debug);
4917     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4918 
4919  END clean_up;
4920 
4921 
4922   PROCEDURE rule_based_simulation(p_planning_criteria_id in number,
4923                                 p_wave_id              in number,
4924                                 p_organization_id      IN number) IS
4925 
4926   l_mo_header_id      number;
4927   l_debug             NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
4928   l_phase             VARCHAR2(100);
4929   l_status            VARCHAR2(100);
4930   l_dev_phase         VARCHAR2(100);
4931   l_dev_status        VARCHAR2(100);
4932   l_message           VARCHAR2(500);
4933   l_result            boolean;
4934   l_request_number    number;
4935   l_request_id        number;
4936   l_request_data      varchar2(30);
4937   l_completion_status varchar2(30);
4938   l_dummy             BOOLEAN;
4939   l_return_status     VARCHAR2(3);
4940   l_num_workers       NUMBER;
4941 
4942   cleanup_not_require_exception  EXCEPTION;
4943 
4944 BEGIN
4945   print_debug('In Rule Based simulation ', l_debug);
4946   l_return_status := FND_API.G_RET_STS_SUCCESS;
4947 
4948   initialize(p_wave_id, p_organization_id, l_return_status);
4949   print_debug('return status after initialize ' ||
4950               l_return_status,
4951               l_debug);
4952   IF l_return_status <> fnd_api.g_ret_sts_success THEN
4953     RAISE cleanup_not_require_exception;
4954   END IF;
4955 
4956   print_debug('Rule Based simulation: calling create_move_order', l_debug);
4957     create_move_order(p_organization_id, p_wave_id, l_mo_header_id, l_request_number, l_return_status);
4958   print_debug('return status after create_move_order ' ||
4959               l_return_status,
4960               l_debug);
4961   IF l_return_status <> fnd_api.g_ret_sts_success THEN
4962     RAISE cleanup_not_require_exception;
4963   END IF;
4964 
4965 
4966   process_reservations(p_wave_id       => p_wave_id,
4967                        p_action        => 'S',
4968                        x_return_status => l_return_status);
4969   print_debug('return status after process_reservations ' ||
4970               l_return_status,
4971               l_debug);
4972   IF l_return_status <> fnd_api.g_ret_sts_success THEN
4973     RAISE fnd_api.g_exc_unexpected_error;
4974   END IF;
4975 
4976 
4977   init_allocation(p_organization_id,
4978                   p_wave_id,
4979                   l_mo_header_id,
4980                   l_return_status);
4981 
4982     print_debug('return status after init_allocation '|| l_return_status, l_debug);
4983     IF l_return_status <> fnd_api.g_ret_sts_success THEN
4984       RAISE fnd_api.g_exc_unexpected_error;
4985     END IF;
4986 
4987 
4988   print_debug('Rule Based simulation: calling post_allocation_processing',
4989               l_debug);
4990 
4991   post_allocation_processing(P_ORGANIZATION_ID,
4992                              l_request_number,
4993                              l_request_id,
4994                              l_return_status);
4995   --post_allocation_processing(P_ORGANIZATION_ID, l_request_number);
4996   print_debug('Rule Based simulation: return status after post_allocation_processing ' ||
4997               l_return_status,
4998               l_debug);
4999   IF l_return_status <> fnd_api.g_ret_sts_success THEN
5000     RAISE fnd_api.g_exc_unexpected_error;
5001   END IF;
5002 
5003   BEGIN
5004     print_debug('Update WDD', l_debug);
5005     UPDATE wsh_delivery_details
5006     SET move_order_line_id=NULL
5007     WHERE delivery_detail_id IN (SELECT delivery_Detail_id
5008                                  FROM wms_wp_rules_simulation where wave_header_id=p_wave_id);
5009 
5010     g_update_wdd := 'N';
5011   EXCEPTION
5012     WHEN OTHERS THEN
5013       print_debug('Could not update WDD. need to update it again during clean_up', l_debug);
5014   END;
5015 
5016   print_debug('Rule Based simulation: calling fulfillment_labor_planning',
5017               l_debug);
5018   fulfillment_labor_planning(p_planning_criteria_id,
5019                              p_wave_id,
5020                              l_mo_header_id,
5021                              p_organization_id,
5022                              l_return_status);
5023   print_debug('Rule Based simulation: return status after fulfillment_labor_planning ' ||
5024               l_return_status,
5025               l_debug);
5026   IF l_return_status <> fnd_api.g_ret_sts_success THEN
5027     RAISE fnd_api.g_exc_unexpected_error;
5028   END IF;
5029 
5030   print_debug('Rule Based simulation: calling clean up', l_debug);
5031   clean_up(p_organization_id, p_wave_id, l_mo_header_id, l_return_status);
5032   print_debug('Rule Based simulation: return status after clean_up ' ||
5033               l_return_status,
5034               l_debug);
5035   IF l_return_status <> fnd_api.g_ret_sts_success THEN
5036     RAISE fnd_api.g_exc_unexpected_error;
5037   END IF;
5038 
5039   print_debug('Rule Based simulation: exiting', l_debug);
5040 
5041   EXCEPTION
5042     WHEN cleanup_not_require_exception THEN
5043       print_debug('Error in rule_based_simulation, No need to call clean_up', l_debug);
5044       RAISE fnd_api.g_exc_unexpected_error;
5045 
5046     WHEN OTHERS THEN
5047       print_debug('Error in rule_based_simulation. Need to call clean_up', l_debug);
5048       clean_up(p_organization_id, p_wave_id, l_mo_header_id, l_return_status);
5049       RAISE fnd_api.g_exc_unexpected_error;
5050   END rule_based_simulation;
5051 
5052 
5053 
5054  procedure Plan_Wave(p_wave_header_id       in number,
5055                     p_planning_criteria_id in number,
5056                     x_return_status        OUT NOCOPY varchar2) is
5057 
5058   l_dummy             boolean;
5059   l_completion_status VARCHAR2(100);
5060 
5061   cursor c_item is
5062     SELECT DISTINCT wdd.inventory_item_id, wwl.organization_id
5063       FROM wms_wp_wave_lines wwl, wsh_delivery_details wdd--11775489 get from wdd...FIXED
5064      WHERE wwl.wave_header_id = p_wave_header_id
5065 	   and wwl.delivery_detail_id = wdd.delivery_detail_id
5066 	   and wwl.organization_id = wdd.organization_id
5067        and nvl(remove_from_wave_flag, 'N') <> 'Y';
5068 
5069   L_ORG_ID  NUMBER;
5070   L_ITEM_ID NUMBER;
5071   l_tbl_count number;
5072 
5073   cursor c_plan_lines is
5074     select wdd.source_header_number,
5075            wwl.delivery_detail_id,
5076            wdd.organization_id,
5077            wdd.inventory_item_id,
5078            wdd.requested_quantity,
5079            wdd.requested_quantity_uom,
5080            wwh.planning_criteria_id,
5081            wdd.ship_set_id,
5082            wdd.source_line_id,
5083            wdd.top_model_line_id,
5084            --  wdd.source_header_id,
5085            inv_salesorder.get_salesorder_for_oeheader(wdd.source_header_id) demand_source_header_id,
5086            --   wdd.source_header_type_id,
5087            decode(wdd.source_document_type_id, 10, 8, 2) demand_source_type_id,
5088            wwl.wave_line_id
5089       from wms_wp_wave_headers_vl      wwh,
5090            wms_wp_wave_lines           wwl, --11775489 get from wdd... FIXED...may have performance impact...
5091            wms_wp_planning_criteria_vl wwp,
5092            wsh_delivery_details        wdd
5093      where wwh.wave_header_id = p_wave_header_id
5094        and wwl.organization_id = l_org_id
5095        and wdd.inventory_item_id = l_item_id
5096        and wwp.planning_criteria_id = p_planning_criteria_id
5097        and wwh.planning_criteria_id = wwp.planning_criteria_id
5098        and wwh.wave_header_id = wwl.wave_header_id
5099        and wwl.delivery_detail_id = wdd.delivery_Detail_id
5100 	   and wdd.organization_id = wwl.organization_id
5101        and wdd.released_status in ('R','B') --- For Hot Order Changes
5102        and nvl(remove_from_wave_flag, 'N') <> 'Y';
5103 
5104 
5105   TYPE SUFFICIENT_QTY_LINES IS RECORD(
5106     source_header_number    varchar2(30),
5107     DELIVERY_DETAIL_ID      NUMBER,
5108     ORGANIZATION_ID         NUMBER,
5109     INVENTORY_ITEM_ID       NUMBER,
5110     REQUESTED_QUANTITY      NUMBER,
5111     requested_quantity_uom  VARCHAR2(3),
5112     SOURCE_LINE_ID          NUMBER,
5113     ship_set_id             number,
5114     demand_source_header_id number,
5115     demand_source_type_id   number,
5116     model_id                number);
5117 
5118   TYPE INSUFFICIENT_QTY_LINES IS RECORD(
5119     source_header_number    varchar2(30),
5120     DELIVERY_DETAIL_ID      NUMBER,
5121     ORGANIZATION_ID         NUMBER,
5122     INVENTORY_ITEM_ID       NUMBER,
5123     REQUESTED_QUANTITY      NUMBER,
5124     requested_quantity_uom  VARCHAR2(3),
5125     SOURCE_LINE_ID          NUMBER,
5126     ship_set_id             number,
5127     demand_source_header_id number,
5128     demand_source_type_id   number,
5129     model_id                number);
5130 
5131 
5132   m1            number;
5133   v_labor_count number;
5134   v_shipset_no  number;
5135   v_model_no    number;
5136 
5137   TYPE DELIVERY_DETAIL_TBL IS TABLE OF number INDEX BY BINARY_INTEGER;
5138   TYPE SUFFICIENT_QTY_LINES_TBL IS TABLE OF SUFFICIENT_QTY_LINES INDEX BY BINARY_INTEGER;
5139   TYPE INSUFFICIENT_QTY_LINES_TBL IS TABLE OF SUFFICIENT_QTY_LINES INDEX BY BINARY_INTEGER;
5140   TYPE insufficient_order_number IS TABLE OF number INDEX BY BINARY_INTEGER;
5141 
5142   l_insufficient_DD_TBL       DELIVERY_DETAIL_TBL; -- For storing insufficient delivery detail ids
5143   l_insufficient_order_number insufficient_order_number;
5144 
5145   cursor c_shipset is
5146     SELECT WDD.DELIVERY_DETAIL_ID
5147       FROM WMS_WP_WAVE_LINES WWL, WSH_DELIVERY_DETAILS WDD --11775489 added org_id join... FIXED
5148      WHERE WWL.DELIVERY_DETAIL_ID = WDD.DELIVERY_DETAIL_ID
5149 	   and WWL.organization_id = wdd.organization_id
5150        AND WWL.WAVE_HEADER_ID = p_wave_header_id
5151        AND WDD.SHIP_SET_ID = v_shipset_no
5152         and wdd.released_status in ('R','B') --- For Hot Order Changes
5153        and v_shipset_no is not null;
5154 
5155   cursor c_model is
5156     SELECT WDD.DELIVERY_DETAIL_ID
5157       FROM WMS_WP_WAVE_LINES WWL, WSH_DELIVERY_DETAILS WDD --11775489 added org_id join... FIXED
5158      WHERE WWL.DELIVERY_DETAIL_ID = WDD.DELIVERY_DETAIL_ID
5159 	   and WWL.organization_id = wdd.organization_id
5160        AND WWL.WAVE_HEADER_ID = p_wave_header_id
5161        AND WDD.top_model_line_id = v_model_no
5162         and wdd.released_status in ('R','B') --- For Hot Order Changes
5163        and v_model_no is not null;
5164 
5165   cursor c_credit_check is
5166     SELECT wwl.wave_line_id, wdd.source_line_id, wdd.source_header_id
5167       FROM WMS_WP_WAVE_LINES WWL, --11775489 get from wdd... FIXED... check below comment
5168 	       wsh_delivery_details wdd
5169      WHERE WWL.WAVE_HEADER_ID = p_wave_header_id
5170 	   and wwl.delivery_detail_id  = wdd.delivery_detail_id
5171 	   and wdd.organization_id = wwl.organization_id
5172    --   and wdd.released_status in ('R','B') --- For Hot Order Changes
5173        and nvl(wwl.remove_from_wave_flag, 'N') <> 'Y';
5174 
5175   L_DEMAND_QTY       NUMBER;
5176   l_is_revision_ctrl BOOLEAN := FALSE;
5177   l_is_lot_ctrl      BOOLEAN := FALSE;
5178   l_is_serial_ctrl   BOOLEAN := FALSE;
5179   l_qoh              NUMBER;
5180   l_rqoh             NUMBER;
5181   l_qr               NUMBER;
5182   l_qs               NUMBER;
5183   l_atr              NUMBER;
5184   l_att              NUMBER;
5185 
5186   l_return_status          VARCHAR2(3) := fnd_api.g_ret_sts_success;
5187   j                        number := 0;
5188   k                        number := 0;
5189   v_backorder_flag         varchar2(1);
5190   v_reject_line_flag       varchar2(1);
5191   v_reject_shipset_flag    varchar2(1);
5192   v_reject_model_flag      varchar2(1);
5193   v_reject_order_flag      varchar2(1);
5194   v_reserve_flag           varchar2(1);
5195   v_create_delivery_flag   varchar2(1);
5196   v_credit_check_hold_flag varchar2(1);
5197 
5198   p_delivery_detail_id         number;
5199   l_TabOfDelDets               WSH_UTIL_CORE.ID_TAB_TYPE;
5200   l_del_rows                   WSH_UTIL_CORE.ID_TAB_TYPE;
5201   X_INSUFFICIENT_QTY_LINES_TBL INSUFFICIENT_QTY_LINES_TBL;
5202   X_SUFFICIENT_QTY_LINES_TBL   SUFFICIENT_QTY_LINES_TBL;
5203   X_DELIVERY_DETAIL_TBL        DELIVERY_DETAIL_TBL;
5204 
5205 
5206 
5207 
5208   TYPE CONSOL_LINES_wave_hdr_id IS TABLE OF number INDEX BY BINARY_INTEGER;
5209   	TYPE CONSOL_LINES_wave_line_id IS TABLE OF number INDEX BY BINARY_INTEGER;
5210   		TYPE CONSOL_LINES_fill_rate IS TABLE OF number INDEX BY BINARY_INTEGER;
5211 
5212   X_CONSOL_LINES_wave_hdr_id   CONSOL_LINES_wave_hdr_id;
5213   X_CONSOL_LINES_wave_line_id CONSOL_LINES_wave_line_id;
5214   X_CONSOL_LINES_fill_rate CONSOL_LINES_fill_rate;
5215 
5216   m                    NUMBER := 0;
5217   l_debug              NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'),
5218                                      0);
5219   l_msg_count          number;
5220   l_msg_data           varchar2(1000);
5221   v_Delivery_Detail_id NUMBER;
5222   v_sqlerrm            NUMBER;
5223   v_sqlmsg             VARCHAR2(1000);
5224 
5225   l_pick_subinventory varchar2(100);
5226   l_reserved_qty      number;
5227   l_effective_atr     number;
5228 
5229   --For credit check hold
5230   l_hold_result VARCHAR2(10);
5231   TYPE credit_check_tbl IS TABLE OF number INDEX BY BINARY_INTEGER;
5232   l_credit_check_tbl credit_check_tbl;
5233   l_hold_index       number;
5234 
5235   --For backorder part
5236   l_out_rows            WSH_UTIL_CORE.ID_TAB_TYPE;
5237   g_backorder_deliv_tab WSH_UTIL_CORE.ID_TAB_TYPE;
5238   g_backorder_qty_tab   WSH_UTIL_CORE.ID_TAB_TYPE;
5239   g_dummy_table         WSH_UTIL_CORE.ID_TAB_TYPE;
5240   n1                    number := 1;
5241 
5242   --For Reservations
5243   l_rsv_temp_rec                inv_reservation_global.mtl_reservation_rec_type;
5244   l_rsv_temp_rec_2              inv_reservation_global.mtl_reservation_rec_type;
5245   l_src_sub                     VARCHAR2(10);
5246   l_order_count                 NUMBER;
5247   l_demand_header_id            NUMBER;
5248   L_demand_line_id              NUMBER;
5249   L_demand_line_detail_id       NUMBER;
5250   L_demand_quantity             NUMBER;
5251   L_demand_quantity_in_repl_uom NUMBER;
5252   L_demand_uom_code             VARCHAR2(3);
5253   L_demand_type_id              NUMBER;
5254   L_sequence_id                 NUMBER;
5255   l_expected_ship_date          date;
5256   l_repl_level                  NUMBER;
5257   l_repl_type                   NUMBER;
5258   l_repl_UOM_code               VARCHAR2(3);
5259   l_Repl_Lot_Size               NUMBER;
5260   l_rsv_tbl_tmp                 inv_reservation_global.mtl_reservation_tbl_type;
5261   l_rsv_rec                     inv_reservation_global.mtl_reservation_rec_type;
5262   l_serial_number               inv_reservation_global.serial_number_tbl_type;
5263   l_to_serial_number            inv_reservation_global.serial_number_tbl_type;
5264   l_quantity_reserved           NUMBER;
5265   l_quantity_reserved2          NUMBER;
5266   l_rsv_id                      NUMBER;
5267   l_error_code                  NUMBER;
5268   l_mtl_reservation_count       NUMBER;
5269   n                             number := 1;
5270   l_other_wdd_qty               number := 0;
5271   l_temp_value                  number;
5272   l_enable_labor_planning       varchar2(1);
5273   l_planning_method             varchar2(1);
5274   l_crossdock_criteria          varchar2(100);
5275   l_crossdock_criteria_id       number;
5276   l_allocation_method           varchar2(100);
5277   l_crossdock_qty               number := 0;
5278   l_allocated_qty               number := 0;
5279   l_organization_id             number;
5280   l_table_start number;
5281 
5282   -- For Storing Values in Global Temp Table -- Rules SImulation
5283   cursor c_populate_temp IS
5284     select wwl.delivery_Detail_id
5285       from wms_wp_wave_lines wwl, wsh_delivery_details wdd
5286      where wwl.wave_header_id = p_wave_header_id
5287 	   and wwl.delivery_detail_id = wdd.delivery_detail_id --11775489 get from wdd... FIXED
5288 	   and wwl.organization_id = wdd.organization_id;
5289 
5290   TYPE wp_crossdock_rec IS RECORD(
5291     delivery_detail_id number,
5292     crossdock_qty      number);
5293 
5294   TYPE wp_crossdock_tbl IS TABLE OF wp_crossdock_rec INDEX BY BINARY_INTEGER;
5295 
5296   x_wp_crossdock_tbl wp_crossdock_tbl;
5297 
5298   l_start_time  DATE; -- start time changes
5299   l_target_date DATE; -- start time changes
5300   --  l_organization_id NUMBER; -- start time changes
5301   start_date_exception EXCEPTION; -- start time changes
5302 l_wave_status varchar2(30);
5303 l_total_qty number;
5304 l_pull_replenishment varchar2(1);
5305 
5306 begin
5307 
5308   -- start time changes start
5309   print_debug('CHECKING IF START TIME IS BETWEEN SYSDATE OR NOT', l_debug);
5310 
5311 
5312 
5313 
5314   SELECT start_time, organization_id,pick_seq_rule_id,
5315   pick_grouping_rule_id,pick_subinventory,staging_subinventory,
5316   DEFAULT_STAGE_LOCATOR_ID,wave_status,pull_replenishment_flag
5317     INTO l_start_time, l_organization_id,
5318     g_pick_seq_rule_id,
5319            g_pick_grouping_rule_id,
5320            g_pick_subinventory,
5321            g_staging_subinventory,
5322            g_to_locator,
5323            l_wave_status,
5324            l_pull_replenishment
5325     FROM wms_wp_wave_headers_vl
5326    WHERE wave_header_id = p_wave_header_id;
5327 
5328 print_debug('Ajith after', l_debug);
5329     print_Debug('start time = ' ||
5330               To_Char(l_start_time, 'DD:MON:YYYY HH24:MI:SS'),
5331               l_debug);
5332 
5333   select enable_labor_planning
5334     into l_enable_labor_planning
5335     from wms_wp_planning_Criteria_vl
5336    where planning_criteria_id = p_planning_criteria_id;
5337 
5338    print_debug('Validating the Start time ', l_debug);
5339 
5340       print_debug('Labor Enabled Flag is  '||l_enable_labor_planning, l_debug);
5341 
5342 
5343    if l_enable_labor_planning = 'Y' then
5344 
5345   SELECT MAX(nvl(wdab.end_time,
5346                  (nvl(wts.planned_departure_date,
5347                       nvl(wnd.latest_pickup_date, wdd.date_scheduled)))))
5348     into l_target_date
5349     FROM wms_wp_wave_lines        wwl, --11775489 get from wdd... FIXED...check performance impact
5350          wsh_delivery_details     wdd,
5351          wsh_delivery_assignments wda,
5352          wsh_new_deliveries       wnd,
5353          wsh_delivery_legs        wdl,
5354          wsh_trip_stops           wts,
5355          wms_dock_appointments_b  wdab
5356    WHERE wwl.organization_id = l_organization_id
5357      AND wwl.wave_header_id = p_wave_header_id
5358      AND wwl.delivery_detail_id = wdd.delivery_detail_id
5359      AND wdd.delivery_detail_id = wda.delivery_detail_id
5360 	 and wdd.organization_id = wwl.organization_id
5361       --and wdd.released_status in ('R','B') --- For Hot Order Changes
5362      AND wda.delivery_id = wnd.delivery_id(+)
5363      AND wnd.delivery_id = wdl.delivery_id(+)
5364      AND wdl.pick_up_stop_id = wts.stop_id(+)
5365      AND wts.stop_id = wdab.trip_stop(+);
5366 
5367   print_Debug('l_target_date = ' ||
5368               To_Char(l_target_date, 'DD:MON:YYYY HH24:MI:SS'),
5369               l_debug);
5370   print_Debug('SYSDATE = ' || to_char(SYSDATE, 'DD:MON:YYYY HH24:MI:SS'),
5371               l_debug);
5372 
5373      IF l_target_date < l_start_time THEN
5374      RAISE start_date_exception;
5375      END IF;
5376 end if;
5377   -- start time changes end
5378 
5379   -- Bulk Populate the Global Temp Table
5380 
5381 
5382   insert into wms_wp_rules_simulation
5383     (wave_header_id,delivery_Detail_id, requested_quantity, requested_quantity2)
5384     select wwl.wave_header_id,wwl.delivery_Detail_id, wdd.requested_quantity, wdd.requested_quantity2
5385       from wms_wp_wave_lines wwl, wsh_delivery_details wdd --11775489 get from wdd... FIXED
5386      where wave_header_id = p_wave_header_id
5387      and wdd.released_status in ('R','B') --- For Hot Order Changes;
5388      and wdd.delivery_Detail_id = wwl.delivery_Detail_id
5389 	 and wdd.organization_id = wwl.organization_id
5390      and nvl(remove_from_wave_flag,'N')<>'Y';
5391 
5392   update wms_wp_wave_lines
5393      set crossdock_quantity = 0
5394    where wave_header_id = p_wave_header_id;
5395   --select
5396   -- Checking if Pick Subinventory is mentioned in Planning Criteria
5397 
5398   print_debug('In Plan Wave API', l_debug);
5399 
5400   select Backorder_flag,
5401          Reject_order_line_flag,
5402          Reject_all_lines_shipset_flag,
5403          Reject_all_lines_model_flag,
5404          Reject_Order_flag,
5405          Reserve_stock_flag,
5406          auto_create_deliveries_flag,
5407          credit_check_hold_flag,
5408          picking_subinventory,
5409          enable_labor_planning,
5410          planning_method,
5411          crossdock_criteria,
5412          crossdock_criteria_id,
5413          allocation_method
5414     into v_backorder_flag,
5415          v_reject_line_flag,
5416          v_reject_shipset_flag,
5417          v_reject_model_flag,
5418          v_reject_order_flag,
5419          v_reserve_flag,
5420          v_create_delivery_flag,
5421          v_credit_check_hold_flag,
5422          l_pick_subinventory,
5423          l_enable_labor_planning,
5424          l_planning_method,
5425          l_crossdock_criteria,
5426          l_crossdock_criteria_id,
5427          l_allocation_method
5428     from wms_wp_planning_criteria_vl
5429    where planning_criteria_id = p_planning_criteria_id;
5430 
5431   print_debug('pick_subinventory in Plan Wave CP is:' ||
5432               l_pick_subinventory,
5433               l_debug);
5434 
5435 g_from_subinventory_plan := l_pick_subinventory;
5436 
5437   -- Plan Wave plans what needs to be done for the wave lines if there is sufficient stock and if sufficient stock is not available.
5438   -- Get the consolidated demand for the item.
5439   -- Get the ATR for the item(org level)
5440   -- Get the wave lines that has available quantity and get the lines that has insufficient quantity
5441   -- Based on the parameters passed, we will be planning what needs to be done
5442 
5443   --Find out the total demand quantity for an item.
5444   ---   OPEN c_req_qty_for_item;
5445   --    FETCH c_req_qty_for_item BULK COLLECT INTO  CONSOL_ITEM_TBL, ORG_ID_TBL,CONSOL_ITEM_REQ_QTY_TBL;
5446   --    CLOSE c_req_qty_for_item;
5447 
5448   -- Find the total lines for the wave_header_id
5449   /*
5450   OPEN c_plan_lines;
5451   FETCH c_plan_lines BULK COLLECT INTO CONSOL_LINES_TBL;
5452   CLOSE c_plan_lines;   */
5453 
5454   print_debug('Before c_item_rec loop ', l_debug);
5455 
5456   print_debug('Checking whether it is availability to check planning or Rule Based Planning ',
5457               l_debug);
5458 
5459   if l_planning_method = 'R' then
5460 
5461     print_debug('Rule Based Planning ', l_debug);
5462 
5463     if l_allocation_method = 'C' then
5464 
5465       call_planned_crossdock(p_wave_header_id, p_planning_criteria_id);
5466 
5467     elsif l_allocation_method = 'I' THEN
5468       print_debug('xxx calling Rule Based simulation ', l_debug);
5469 
5470       rule_based_simulation(p_planning_criteria_id,
5471                             p_wave_header_id,
5472                             l_organization_id);
5473 
5474     elsif l_allocation_method = 'N' then
5475 
5476       rule_based_simulation(p_planning_criteria_id,
5477                             p_wave_header_id,
5478                             l_organization_id);
5479 
5480       call_planned_crossdock(p_wave_header_id, p_planning_criteria_id);
5481 
5482     elsif l_allocation_method = 'X' then
5483 
5484       call_planned_crossdock(p_wave_header_id, p_planning_criteria_id);
5485 
5486       rule_based_simulation(p_planning_criteria_id,
5487                             p_wave_header_id,
5488                             l_organization_id);
5489 
5490     end if;
5491 
5492   else
5493 
5494     print_debug('Availabiltity Based Planning ', l_debug);
5495 
5496     if l_allocation_method in ('C', 'X') then
5497 
5498       -- Call Crossdocking
5499 
5500       call_planned_crossdock(p_wave_header_id, p_planning_criteria_id);
5501 
5502   elsif l_allocation_method = 'N' then
5503 
5504   	print_debug('Prioritize Inventory --> Checking how much can come from inventory', l_debug);
5505 
5506   	 FOR c_item_rec IN c_item LOOP
5507 
5508     l_item_id := c_item_rec.inventory_item_id;
5509     l_org_id  := c_item_rec.organization_id;
5510     print_debug('l_item_id :' || l_item_id, l_debug);
5511     print_debug('l_org_id :' || l_org_id, l_debug);
5512 
5513           print_debug('Planning Method is Availability to Check and Allocation Method is not Crossdock Only --> Getting ATR ',
5514                   l_debug);
5515 
5516       --Find out the total atr for the item
5517       IF inv_cache.set_item_rec(L_ORG_ID, L_item_id) THEN
5518 
5519 
5520         IF inv_cache.item_rec.serial_number_control_code NOT IN (1, 6) THEN
5521           l_is_serial_ctrl := FALSE;
5522         ELSE
5523           l_is_serial_ctrl := TRUE;
5524         END IF;
5525 
5526       ELSE
5527 
5528         print_debug('Error: Item detail not found', l_debug);
5529         RAISE no_data_found;
5530       END IF;
5531 
5532       inv_quantity_tree_pub.query_quantities(p_api_version_number      => 1.0,
5533                                              p_init_msg_lst            => fnd_api.g_false,
5534                                              x_return_status           => l_return_status,
5535                                              x_msg_count               => l_msg_count,
5536                                              x_msg_data                => l_msg_data,
5537                                              p_organization_id         => l_org_id,
5538                                              p_inventory_item_id       => l_item_id,
5539                                              p_tree_mode               => inv_quantity_tree_pub.g_transaction_mode,
5540                                              p_is_revision_control     => l_is_revision_ctrl,
5541                                              p_is_lot_control          => l_is_lot_ctrl,
5542                                              p_is_serial_control       => l_is_serial_ctrl,
5543                                              p_demand_source_type_id   => -9999 --should not be null
5544                                             ,
5545                                              p_demand_source_header_id => -9999 --should not be null
5546                                             ,
5547                                              p_demand_source_line_id   => -9999,
5548                                              p_revision                => NULL,
5549                                              p_lot_number              => NULL,
5550                                              p_subinventory_code       => l_pick_subinventory,
5551                                              p_locator_id              => NULL,
5552                                              x_qoh                     => l_qoh,
5553                                              x_rqoh                    => l_rqoh,
5554                                              x_qr                      => l_qr,
5555                                              x_qs                      => l_qs,
5556                                              x_att                     => l_att,
5557                                              x_atr                     => l_atr);
5558 
5559       print_debug('x_qoh :' || l_qoh, l_debug);
5560       print_debug('x_rqoh :' || l_rqoh, l_debug);
5561       print_debug('x_qr :' || l_qr, l_debug);
5562       print_debug('x_qs :' || l_qs, l_debug);
5563       print_debug('x_att :' || l_att, l_debug);
5564       print_debug('x_atr :' || l_atr, l_debug);
5565 
5566 
5567 
5568   	FOR c_rec IN c_plan_lines LOOP
5569 
5570       EXIT WHEN c_plan_lines%NOTFOUND;
5571 
5572     --   begin
5573 
5574           -- Since Qty Tree would have also subttracted qty for current demand
5575           -- lines under consideration for which there is existing
5576           -- reservation as well, I need to add them back
5577           -- to see real picture of the atr for demand line under consideration
5578 
5579           ------------------
5580 
5581           SELECT nvl(sum(reservation_quantity), 0)
5582             INTO l_reserved_qty
5583             FROM mtl_Reservations
5584            WHERE demand_source_line_id = c_rec.SOURCE_LINE_ID
5585              and organization_id = L_ORG_ID
5586              and inventory_item_id = L_ITEM_ID
5587              and (subinventory_code = l_pick_subinventory or
5588                  l_pick_subinventory is null);
5589           SELECT Nvl(SUM(wdd.requested_quantity), 0)
5590             INTO l_other_wdd_qty
5591             FROM wsh_delivery_details wdd
5592            WHERE wdd.organization_id = l_org_id
5593              AND wdd.inventory_item_id = l_item_id
5594 			 and wdd.released_status in ('B', 'E', 'F', 'K', 'R','S') --11822962 only consider wdds where requested material is against a possible reservation
5595              and wdd.delivery_Detail_id not in
5596                  (select wwl.delivery_detail_id
5597                     from wms_wp_wave_lines wwl, wsh_delivery_details wdd2 --11775489 get from wdd... FIXED
5598                    where wwl.wave_header_id = p_wave_header_id
5599 				     and wwl.delivery_detail_id = wdd2.delivery_Detail_id
5600 					 and wwl.organization_id = wdd2.organization_id
5601                         --  and nvl(remove_from_wave_flag, 'N') <> 'Y'   --- Removed by Ajith in Phase III as  planning it second time is giving an issue
5602                      and wdd2.source_line_id = wdd.source_line_id);
5603 
5604           print_debug('Reserved Qty for other dds that do not belong to the wave in source line id  :' ||
5605                       c_rec.SOURCE_LINE_ID || ' is ' || l_other_wdd_qty,
5606                       l_debug);
5607 
5608           IF (l_reserved_qty - l_other_wdd_qty) >= 0 THEN
5609             l_temp_value := (l_reserved_qty - l_other_wdd_qty);
5610           ELSE
5611             l_temp_value := 0;
5612           END IF;
5613 
5614           l_reserved_qty := l_temp_value;
5615 
5616           print_debug('Reserved Qty for source line id  :' ||
5617                       c_rec.SOURCE_LINE_ID || ' is ' || l_reserved_qty,
5618                       l_debug);
5619 
5620                                 print_debug('l_atr is   :' ||l_atr,
5621                       l_debug);
5622 
5623                          if (l_atr + l_reserved_qty) >= c_rec.requested_quantity then
5624 
5625                 update wms_wp_rules_simulation
5626                    set allocated_quantity =  c_rec.requested_quantity
5627                  where delivery_detail_id = c_rec.delivery_Detail_id
5628                  and wave_header_id=p_wave_header_id;
5629                 print_debug('Prioritize Inventory --> Quantity picked from Inventory is  '||c_rec.requested_quantity,
5630                             l_debug);
5631 
5632 
5633                 l_atr := l_atr - c_rec.REQUESTED_QUANTITY;
5634 
5635               elsif (l_atr + l_reserved_qty) < c_rec.requested_quantity then
5636 
5637 
5638 
5639                   update wms_wp_rules_simulation
5640                      set allocated_quantity =
5641                                                 (l_atr + l_reserved_qty)
5642                    where delivery_detail_id = c_rec.delivery_Detail_id
5643                     and wave_header_id=p_wave_header_id;
5644 
5645    print_debug('Prioritize Inventory --> Quantity picked from Inventory is  '||
5646                                                 (l_atr + l_reserved_qty),
5647                             l_debug);
5648 
5649 
5650                 l_atr :=0;
5651 
5652 
5653               end if;
5654 
5655 
5656 
5657 
5658 
5659       if l_atr < 0 then
5660 
5661         l_atr := 0;
5662 
5663       end if;
5664 
5665     end loop;
5666 
5667   end loop;
5668 
5669          print_debug('Prioritize Inventory -->Calling Crossdocking', l_debug);
5670     call_planned_crossdock(p_wave_header_id, p_planning_criteria_id);
5671     end if;
5672 
5673 
5674   end if;
5675 
5676   print_debug('Entering the Item Loop ', l_debug);
5677   FOR c_item_rec IN c_item LOOP
5678 
5679     l_item_id := c_item_rec.inventory_item_id;
5680     l_org_id  := c_item_rec.organization_id;
5681     print_debug('l_item_id :' || l_item_id, l_debug);
5682     print_debug('l_org_id :' || l_org_id, l_debug);
5683 
5684     -- Get the Allocation Method and check if it is crossdock only then the following code can be skipped..
5685 
5686     if (l_planning_method = 'A' and l_allocation_method <> 'C') then
5687 
5688       print_debug('Planning Method is Availability to Check and Allocation Method is not Crossdock Only --> Getting ATR ',
5689                   l_debug);
5690 
5691       --Find out the total atr for the item
5692       IF inv_cache.set_item_rec(L_ORG_ID, L_item_id) THEN
5693 
5694       /*  IF inv_cache.item_rec.revision_qty_control_code = 2 THEN
5695           l_is_revision_ctrl := TRUE;
5696         ELSE
5697           l_is_revision_ctrl := FALSE;
5698         END IF;
5699 
5700         IF inv_cache.item_rec.lot_control_code = 2 THEN
5701           l_is_lot_ctrl := TRUE;
5702         ELSE
5703           l_is_lot_ctrl := FALSE;
5704         END IF;*/
5705 
5706         IF inv_cache.item_rec.serial_number_control_code NOT IN (1, 6) THEN
5707           l_is_serial_ctrl := FALSE;
5708         ELSE
5709           l_is_serial_ctrl := TRUE;
5710         END IF;
5711 
5712       ELSE
5713 
5714         print_debug('Error: Item detail not found', l_debug);
5715         RAISE no_data_found;
5716       END IF;
5717 
5718       inv_quantity_tree_pub.query_quantities(p_api_version_number      => 1.0,
5719                                              p_init_msg_lst            => fnd_api.g_false,
5720                                              x_return_status           => l_return_status,
5721                                              x_msg_count               => l_msg_count,
5722                                              x_msg_data                => l_msg_data,
5723                                              p_organization_id         => l_org_id,
5724                                              p_inventory_item_id       => l_item_id,
5725                                              p_tree_mode               => inv_quantity_tree_pub.g_transaction_mode,
5726                                              p_is_revision_control     => l_is_revision_ctrl,
5727                                              p_is_lot_control          => l_is_lot_ctrl,
5728                                              p_is_serial_control       => l_is_serial_ctrl,
5729                                              p_demand_source_type_id   => -9999 --should not be null
5730                                             ,
5731                                              p_demand_source_header_id => -9999 --should not be null
5732                                             ,
5733                                              p_demand_source_line_id   => -9999,
5734                                              p_revision                => NULL,
5735                                              p_lot_number              => NULL,
5736                                              p_subinventory_code       => l_pick_subinventory,
5737                                              p_locator_id              => NULL,
5738                                              x_qoh                     => l_qoh,
5739                                              x_rqoh                    => l_rqoh,
5740                                              x_qr                      => l_qr,
5741                                              x_qs                      => l_qs,
5742                                              x_att                     => l_att,
5743                                              x_atr                     => l_atr);
5744 
5745       print_debug('x_qoh :' || l_qoh, l_debug);
5746       print_debug('x_rqoh :' || l_rqoh, l_debug);
5747       print_debug('x_qr :' || l_qr, l_debug);
5748       print_debug('x_qs :' || l_qs, l_debug);
5749       print_debug('x_att :' || l_att, l_debug);
5750       print_debug('x_atr :' || l_atr, l_debug);
5751 
5752 
5753           -- Ajith Replenishment Simulation for availability check
5754 
5755       -- Get the Sourcing subinventory for the pick subinventory
5756 
5757       -- Need to check if entire atr can be fulfilled from this subinventory or should it be replenished.
5758 
5759       if l_pick_subinventory is not null and l_pull_replenishment = 'Y' and get_source_subinventory(l_item_id,l_pick_subinventory) is not null THEN
5760 
5761 
5762       	   print_debug('Checking if Replenishment is needed in Availability check', l_debug);
5763       select sum(wdd.requested_quantity) into l_total_qty
5764 	  from wms_wp_wave_lines wwl, wsh_delivery_details wdd
5765 	  where wave_header_id = p_wave_header_id --11775489 get from wdd... FIXED
5766 	  and wwl.delivery_detail_id  = wdd.delivery_detail_id
5767 	  and wdd.organization_id = wwl.organization_id
5768       and wdd.inventory_item_id = l_item_id and wdd.organization_id=l_org_id;
5769 
5770      print_debug('total demand quantity for the item is ' || l_total_qty, l_debug);
5771 
5772       if l_total_qty < l_atr THEN
5773 
5774       	 print_debug('No Replenishment is needed', l_debug);
5775 
5776     ELSE
5777 
5778     	l_atr := l_atr + get_att_for_subinventory(get_source_subinventory(l_item_id,l_pick_subinventory),
5779                                     l_item_id,
5780                                     l_org_id);
5781 
5782 
5783     	 print_debug('New ATT for the item is ' || l_atr, l_debug);
5784 
5785     end if;
5786 
5787 
5788 
5789 
5790     end if;
5791 
5792 
5793     ELSE
5794 
5795       l_atr := 0;
5796 
5797     end if; --- For Crossdock only
5798     FOR c_rec IN c_plan_lines LOOP
5799 
5800       EXIT WHEN c_plan_lines%NOTFOUND;
5801 
5802       if (l_planning_method = 'A' and l_allocation_method <> 'C') then
5803 
5804         print_debug('Planning Method is Availability to Check and Allocation Method is not Crossdock Only  --> Getting Reserved Qty ',
5805                     l_debug);
5806 
5807         begin
5808 
5809           -- Since Qty Tree would have also subttracted qty for current demand
5810           -- lines under consideration for which there is existing
5811           -- reservation as well, I need to add them back
5812           -- to see real picture of the atr for demand line under consideration
5813 
5814           ------------------
5815 
5816           SELECT nvl(sum(reservation_quantity), 0)
5817             INTO l_reserved_qty
5818             FROM mtl_Reservations
5819            WHERE demand_source_line_id = c_rec.SOURCE_LINE_ID
5820              and organization_id = L_ORG_ID
5821              and inventory_item_id = L_ITEM_ID
5822              and (subinventory_code = l_pick_subinventory or
5823                  l_pick_subinventory is null);
5824           SELECT Nvl(SUM(wdd.requested_quantity), 0)
5825             INTO l_other_wdd_qty
5826             FROM wsh_delivery_details wdd
5827            WHERE wdd.organization_id = l_org_id
5828              AND wdd.inventory_item_id = l_item_id
5829 			 and wdd.released_status in ('B', 'E', 'F', 'K', 'R','S') --11822962 only consider wdds where requested material is against a possible reservation
5830              and wdd.delivery_Detail_id not in
5831                  (select wwl.delivery_detail_id
5832                     from wms_wp_wave_lines wwl, wsh_delivery_details wdd2 --11775489 get from wdd... FIXED
5833                    where wwl.wave_header_id = p_wave_header_id
5834 				     and wwl.delivery_detail_id = wdd2.delivery_Detail_id
5835 					 and wwl.organization_id = wdd2.organization_id
5836                         --  and nvl(remove_from_wave_flag, 'N') <> 'Y'   --- Removed by Ajith in Phase III as  planning it second time is giving an issue
5837                      and wdd2.source_line_id = wdd.source_line_id);
5838 
5839           print_debug('Reserved Qty for other dds that do not belong to the wave in source line id  :' ||
5840                       c_rec.SOURCE_LINE_ID || ' is ' || l_other_wdd_qty,
5841                       l_debug);
5842 
5843           IF (l_reserved_qty - l_other_wdd_qty) >= 0 THEN
5844             l_temp_value := (l_reserved_qty - l_other_wdd_qty);
5845           ELSE
5846             l_temp_value := 0;
5847           END IF;
5848 
5849           l_reserved_qty := l_temp_value;
5850 
5851           print_debug('Reserved Qty for source line id  :' ||
5852                       c_rec.SOURCE_LINE_ID || ' is ' || l_reserved_qty,
5853                       l_debug);
5854 
5855           l_crossdock_qty := 0; ---In Case of Inventory only
5856           if l_allocation_method <> 'I' then
5857             select crossdocked_quantity
5858               into l_crossdock_qty
5859               from wms_wp_rules_simulation
5860              where delivery_Detail_id = c_rec.delivery_Detail_id
5861               and wave_header_id=p_wave_header_id;
5862 
5863             print_debug('Crossdocked Quantity for delivery_Detail_id   :' ||
5864                         c_rec.delivery_Detail_id || ' is ' ||
5865                         l_crossdock_qty,
5866                         l_debug);
5867 
5868             if l_allocation_method = 'N' then
5869 
5870               print_debug('Prioritize Inventory', l_debug);
5871 
5872               if (l_atr + l_reserved_qty) >= c_rec.requested_quantity then
5873 
5874                 update wms_wp_rules_simulation
5875                    set crossdocked_quantity = 0
5876                  where delivery_detail_id = c_rec.delivery_Detail_id
5877                   and wave_header_id=p_wave_header_id;
5878                 print_debug('Prioritize Inventory --> Quantity picked from Crossdocking is 0 ',
5879                             l_debug);
5880 
5881                 l_crossdock_qty := 0;
5882               else
5883 
5884                 if l_crossdock_qty > 0 then
5885 
5886                   update wms_wp_rules_simulation
5887                      set crossdocked_quantity = c_rec.requested_quantity -
5888                                                 (l_atr + l_reserved_qty)
5889                    where delivery_detail_id = c_rec.delivery_Detail_id
5890                     and wave_header_id=p_wave_header_id;
5891 
5892                   print_debug('Prioritize Inventory --> Quantity picked from Crossdocking is  ' ||
5893                               to_number(c_rec.requested_quantity -
5894                                         (l_atr + l_reserved_qty)),
5895                               l_debug);
5896                   l_crossdock_qty := c_rec.requested_quantity -
5897                                      (l_atr + l_reserved_qty);
5898                 end if;
5899               end if;
5900 
5901             end if;
5902           end if;
5903         exception
5904 
5905           when no_data_found then
5906             l_reserved_qty  := 0;
5907             l_crossdock_qty := 0;
5908         end;
5909 
5910         l_atr := l_atr + l_reserved_qty + l_crossdock_qty;
5911 
5912         print_debug('Effective ATR is  :' || l_atr, l_debug);
5913 
5914       Elsif (l_allocation_method = 'C') THEN
5915         -- For Crossdock only in Rule Based or Normal Planning
5916 
5917         print_debug('Allocation Method is Crossdock Only  --> Getting Crossdock Qty ',
5918                     l_debug);
5919 
5920         select crossdocked_quantity
5921           into l_crossdock_qty
5922           from wms_wp_rules_simulation
5923          where delivery_Detail_id = c_rec.delivery_Detail_id
5924           and wave_header_id=p_wave_header_id;
5925 
5926         print_debug('Crossdocked Quantity for delivery_Detail_id   :' ||
5927                     c_rec.delivery_Detail_id || ' is ' || l_crossdock_qty,
5928                     l_debug);
5929 
5930         l_atr := l_crossdock_qty;
5931 
5932       elsif (l_planning_method = 'R' and l_allocation_method <> 'C') then
5933 
5934         print_debug('Planning Method is Rule based Planning and Allocation Method is not Crossdock Only  --> Getting Allocated Qty and Crossdocked Qty ',
5935                     l_debug);
5936 
5937         select crossdocked_quantity, allocated_quantity
5938           into l_crossdock_qty, l_allocated_qty
5939           from wms_wp_rules_simulation
5940          where delivery_Detail_id = c_rec.delivery_Detail_id
5941           and wave_header_id=p_wave_header_id;
5942 
5943         print_debug('Crossdocked Quantity for delivery_Detail_id   :' ||
5944                     c_rec.delivery_Detail_id || ' is ' || l_crossdock_qty,
5945                     l_debug);
5946         print_debug('Allocated Quantity for delivery_Detail_id   :' ||
5947                     c_rec.delivery_Detail_id || ' is ' || l_allocated_qty,
5948                     l_debug);
5949 
5950         l_atr := l_crossdock_qty + l_allocated_qty;
5951 
5952       end if; -- Crossdock only
5953 
5954       print_debug('Effective Final ATR   is :' || l_atr, l_debug);
5955 
5956       if l_atr >= c_rec.REQUESTED_QUANTITY then
5957 
5958         PRINT_DEBUG('Effective ATR is greater than the Requested Quantity',
5959                     l_debug);
5960 
5961         X_CONSOL_LINES_wave_hdr_id(m) := p_wave_header_id;
5962        X_CONSOL_LINES_wave_line_id(m) := c_rec.wave_line_id;
5963         X_CONSOL_LINES_fill_rate(m) := 100;
5964 
5965         X_SUFFICIENT_QTY_LINES_TBL(j).DELIVERY_DETAIL_ID := c_rec
5966                                                            .DELIVERY_DETAIL_ID;
5967         X_SUFFICIENT_QTY_LINES_TBL(j).SOURCE_LINE_ID := c_rec
5968                                                        .SOURCE_LINE_ID;
5969         X_SUFFICIENT_QTY_LINES_TBL(j).source_header_number := c_rec
5970                                                              .source_header_number;
5971         X_SUFFICIENT_QTY_LINES_TBL(j).ORGANIZATION_ID := c_rec
5972                                                         .ORGANIZATION_ID;
5973         X_SUFFICIENT_QTY_LINES_TBL(j).INVENTORY_ITEM_ID := c_rec
5974                                                           .INVENTORY_ITEM_ID;
5975         X_SUFFICIENT_QTY_LINES_TBL(j).REQUESTED_QUANTITY := c_rec
5976                                                            .REQUESTED_QUANTITY;
5977         X_SUFFICIENT_QTY_LINES_TBL(j).ship_set_id := c_rec.ship_set_id;
5978         X_SUFFICIENT_QTY_LINES_TBL(j).model_id := c_rec.top_model_line_id;
5979         X_SUFFICIENT_QTY_LINES_TBL(j).requested_quantity_uom := c_rec
5980                                                                .requested_quantity_uom;
5981         X_SUFFICIENT_QTY_LINES_TBL(j).demand_source_header_id := c_rec
5982                                                                 .demand_source_header_id;
5983         X_SUFFICIENT_QTY_LINES_TBL(j).demand_source_type_id := c_rec
5984                                                               .demand_source_type_id;
5985 
5986         -- For Autocreate delivery
5987         if v_create_delivery_flag = 'Y' then
5988           l_TabOfDelDets(j + 1) := X_SUFFICIENT_QTY_LINES_TBL(j)
5989                                   .DELIVERY_DETAIL_ID;
5990         end if;
5991         if v_reserve_flag = 'Y' then
5992           -- If atr is greater than the req qty plan for lines with Sufficient stock
5993           -- *********Planning Action for Lines with Sufficient Stock *********
5994 
5995           --Reserve Stock for Order Line
5996           --Create High Level Reservation
5997 
5998           begin
5999 
6000             -- Create Org Level reservation for every demand line, if it does not exist
6001 
6002             -- Check if an Org level reservation exists for corresponding order line
6003             -- Clear out old values
6004             l_rsv_temp_rec := l_rsv_temp_rec_2;
6005 
6006             -- Assign all new values
6007             l_rsv_temp_rec.organization_id   := X_SUFFICIENT_QTY_LINES_TBL(j)
6008                                                .ORGANIZATION_ID;
6009             l_rsv_temp_rec.inventory_item_id := X_SUFFICIENT_QTY_LINES_TBL(j)
6010                                                .INVENTORY_item_id;
6011 
6012             l_rsv_temp_rec.DEMAND_SOURCE_TYPE_ID := X_SUFFICIENT_QTY_LINES_TBL(j)
6013                                                    .demand_source_type_id;
6014 
6015             l_rsv_temp_rec.DEMAND_SOURCE_HEADER_ID := X_SUFFICIENT_QTY_LINES_TBL(j)
6016                                                      .demand_source_header_id;
6017             l_rsv_temp_rec.DEMAND_SOURCE_LINE_ID   := X_SUFFICIENT_QTY_LINES_TBL(j)
6018                                                      .source_line_id;
6019 
6020             l_return_status := fnd_api.g_ret_sts_success;
6021             inv_reservation_pub.query_reservation(p_api_version_number        => 1.0,
6022                                                   x_return_status             => l_return_status,
6023                                                   x_msg_count                 => l_msg_count,
6024                                                   x_msg_data                  => l_msg_data,
6025                                                   p_query_input               => l_rsv_temp_rec,
6026                                                   x_mtl_reservation_tbl       => l_rsv_tbl_tmp,
6027                                                   x_mtl_reservation_tbl_count => l_mtl_reservation_count,
6028                                                   x_error_code                => l_error_code);
6029 
6030             IF l_RETURN_status = fnd_api.g_ret_sts_success THEN
6031 
6032               PRINT_DEBUG('Number of reservations found: ' ||
6033                           l_mtl_reservation_count,
6034                           l_debug);
6035 
6036               --
6037               IF l_mtl_reservation_count = 0 then
6038                 -- Create high-level reservation
6039 
6040                 PRINT_DEBUG('Creating reservation>>>', l_debug);
6041 
6042                 -- Set the values for the reservation record to be created
6043 
6044                 l_rsv_rec.reservation_id          := NULL;
6045                 l_rsv_rec.requirement_date        := sysdate; --l_demand_expected_time;
6046                 l_rsv_rec.organization_id         := X_SUFFICIENT_QTY_LINES_TBL(j)
6047                                                     .ORGANIZATION_ID;
6048                 l_rsv_rec.inventory_item_id       := X_SUFFICIENT_QTY_LINES_TBL(j)
6049                                                     .inventory_item_id;
6050                 l_rsv_rec.demand_source_name      := NULL;
6051                 l_rsv_rec.demand_source_type_id   := X_SUFFICIENT_QTY_LINES_TBL(j)
6052                                                     .demand_source_type_id;
6053                 l_rsv_rec.demand_source_header_id := X_SUFFICIENT_QTY_LINES_TBL(j)
6054                                                     .demand_source_header_id;
6055                 -- here l_demand_so_header_id is inv_salesorder.get_salesorder_for_oeheader(wdd.source_header_id)
6056                 l_rsv_rec.demand_source_line_id        := X_SUFFICIENT_QTY_LINES_TBL(j)
6057                                                          .source_line_id;
6058                 l_rsv_rec.orig_demand_source_type_id   := X_SUFFICIENT_QTY_LINES_TBL(j)
6059                                                          .demand_source_type_id;
6060                 l_rsv_rec.orig_demand_source_header_id := X_SUFFICIENT_QTY_LINES_TBL(j)
6061                                                          .demand_source_header_id;
6062                 l_rsv_rec.orig_demand_source_line_id   := X_SUFFICIENT_QTY_LINES_TBL(j)
6063                                                          .source_line_id;
6064 
6065                 -- For now supply is only from Inventory supply_source_type_id = 13
6066                 l_rsv_rec.demand_source_line_detail      := NULL;
6067                 l_rsv_rec.orig_demand_source_line_detail := NULL;
6068 
6069                 l_rsv_rec.demand_source_delivery         := NULL;
6070                 l_rsv_rec.primary_uom_code               := X_SUFFICIENT_QTY_LINES_TBL(j)
6071                                                            .requested_quantity_uom;
6072                 l_rsv_rec.primary_uom_id                 := NULL;
6073                 l_rsv_rec.secondary_uom_code             := null;
6074                 l_rsv_rec.secondary_uom_id               := NULL;
6075                 l_rsv_rec.reservation_uom_code           := X_SUFFICIENT_QTY_LINES_TBL(j)
6076                                                            .requested_quantity_uom; --l_supply_uom_code;
6077                 l_rsv_rec.reservation_uom_id             := NULL;
6078                 l_rsv_rec.reservation_quantity           := X_SUFFICIENT_QTY_LINES_TBL(j)
6079                                                            .requested_quantity; --Should
6080                 l_rsv_rec.primary_reservation_quantity   := X_SUFFICIENT_QTY_LINES_TBL(j)
6081                                                            .requested_quantity;
6082                 l_rsv_rec.secondary_reservation_quantity := null;
6083                 l_rsv_rec.detailed_quantity              := NULL;
6084                 l_rsv_rec.secondary_detailed_quantity    := NULL;
6085                 l_rsv_rec.autodetail_group_id            := NULL;
6086                 l_rsv_rec.external_source_code           := 'OE'; -- Mark the external source --Ajith???????????????
6087                 l_rsv_rec.external_source_line_id        := NULL;
6088                 l_rsv_rec.supply_source_type_id          := 13;
6089                 l_rsv_rec.orig_supply_source_type_id     := 13;
6090                 l_rsv_rec.supply_source_name             := NULL;
6091 
6092                 l_rsv_rec.supply_source_header_id        := NULL;
6093                 l_rsv_rec.supply_source_line_id          := NULL;
6094                 l_rsv_rec.supply_source_line_detail      := NULL;
6095                 l_rsv_rec.orig_supply_source_header_id   := NULL;
6096                 l_rsv_rec.orig_supply_source_line_id     := NULL;
6097                 l_rsv_rec.orig_supply_source_line_detail := NULL;
6098 
6099                 l_rsv_rec.revision := NULL;
6100                 --  l_rsv_rec.subinventory_code  := l_subinventory_code;
6101                 l_rsv_rec.subinventory_code  := NULL;
6102                 l_rsv_rec.subinventory_id    := NULL;
6103                 l_rsv_rec.locator_id         := NULL;
6104                 l_rsv_rec.lot_number         := NULL;
6105                 l_rsv_rec.lot_number_id      := NULL;
6106                 l_rsv_rec.pick_slip_number   := NULL;
6107                 l_rsv_rec.lpn_id             := NULL;
6108                 l_rsv_rec.attribute_category := NULL;
6109                 l_rsv_rec.attribute1         := NULL;
6110                 l_rsv_rec.attribute2         := NULL;
6111                 l_rsv_rec.attribute3         := NULL;
6112                 l_rsv_rec.attribute4         := NULL;
6113                 l_rsv_rec.attribute5         := NULL;
6114                 l_rsv_rec.attribute6         := NULL;
6115                 l_rsv_rec.attribute7         := NULL;
6116                 l_rsv_rec.attribute8         := NULL;
6117                 l_rsv_rec.attribute9         := NULL;
6118                 l_rsv_rec.attribute10        := NULL;
6119                 l_rsv_rec.attribute11        := NULL;
6120                 l_rsv_rec.attribute12        := NULL;
6121                 l_rsv_rec.attribute13        := NULL;
6122                 l_rsv_rec.attribute14        := NULL;
6123                 l_rsv_rec.attribute15        := NULL;
6124                 l_rsv_rec.ship_ready_flag    := NULL;
6125                 l_rsv_rec.staged_flag        := NULL;
6126 
6127                 l_rsv_rec.crossdock_flag        := NULL;
6128                 l_rsv_rec.crossdock_criteria_id := NULL;
6129 
6130                 l_rsv_rec.serial_reservation_quantity := NULL;
6131                 --   l_rsv_rec.supply_receipt_date         := l_supply_expected_time; --????????Ajith
6132                 --   l_rsv_rec.demand_ship_date            := l_demand_expected_time; --?????????Ajith
6133                 l_rsv_rec.supply_receipt_date := sysdate;
6134                 l_rsv_rec.demand_ship_date    := sysdate;
6135                 l_rsv_rec.project_id          := NULL;
6136                 l_rsv_rec.task_id             := NULL;
6137                 l_rsv_rec.serial_number       := NULL;
6138 
6139                 print_debug('Call the create_reservation API to create the replenishemnt reservation',
6140                             l_debug);
6141 
6142                 INV_RESERVATION_PVT.create_reservation(p_api_version_number          => 1.0,
6143                                                        p_init_msg_lst                => fnd_api.g_false,
6144                                                        x_return_status               => l_return_status,
6145                                                        x_msg_count                   => l_msg_count,
6146                                                        x_msg_data                    => l_msg_data,
6147                                                        p_rsv_rec                     => l_rsv_rec,
6148                                                        p_serial_number               => l_serial_number,
6149                                                        x_serial_number               => l_to_serial_number,
6150                                                        p_partial_reservation_flag    => fnd_api.g_false,
6151                                                        p_force_reservation_flag      => fnd_api.g_false,
6152                                                        p_validation_flag             => fnd_api.g_true,
6153                                                        x_quantity_reserved           => l_quantity_reserved,
6154                                                        x_secondary_quantity_reserved => l_quantity_reserved2,
6155                                                        x_reservation_id              => l_rsv_id);
6156 
6157               END IF;
6158 
6159               IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
6160 
6161                 print_debug('Error returned from INV create_reservation API: ' ||
6162                             l_return_status,
6163                             l_debug);
6164 
6165                 -- Raise an exception.  The caller will do the rollback, cleanups,
6166                 RAISE FND_API.G_EXC_ERROR;
6167 
6168               END IF;
6169 
6170             ELSE
6171 
6172               PRINT_DEBUG('Error: ' || l_msg_data, l_debug);
6173 
6174             END IF;
6175 
6176           EXCEPTION
6177             WHEN OTHERS THEN
6178               l_return_status := fnd_api.g_ret_sts_error;
6179               fnd_msg_pub.count_and_get(p_count => l_msg_count,
6180                                         p_data  => l_msg_data);
6181 
6182               print_debug('Exiting Create_RSV - Execution error: ' ||
6183                           TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS') || ' ' ||
6184                           l_msg_data,
6185                           l_debug);
6186 
6187           end;
6188         end if;
6189 
6190         j := j + 1;
6191 
6192       else
6193 
6194         PRINT_DEBUG('Effective ATR is less than the Requested Quantity',
6195                     l_debug);
6196 
6197         X_CONSOL_LINES_wave_hdr_id(m) := p_wave_header_id;
6198        X_CONSOL_LINES_wave_line_id(m) := c_rec.wave_line_id;
6199 
6200         IF L_ATR > 0 THEN
6201           X_CONSOL_LINES_fill_rate(m) := round((l_atr * 100) /
6202                                                            c_rec.REQUESTED_QUANTITY);
6203 
6204           --    l_atr := l_atr - c_rec.REQUESTED_QUANTITY;
6205 
6206         ELSE
6207           X_CONSOL_LINES_fill_rate(m) := 0;
6208 
6209           -- l_atr := l_atr - c_rec.REQUESTED_QUANTITY;
6210         end if;
6211 
6212         X_INSUFFICIENT_QTY_LINES_TBL(k).DELIVERY_DETAIL_ID := c_rec
6213                                                              .DELIVERY_DETAIL_ID;
6214         X_INSUFFICIENT_QTY_LINES_TBL(k).SOURCE_LINE_ID := c_rec
6215                                                          .SOURCE_LINE_ID;
6216         X_INSUFFICIENT_QTY_LINES_TBL(k).source_header_number := c_rec
6217                                                                .source_header_number;
6218         X_INSUFFICIENT_QTY_LINES_TBL(k).ORGANIZATION_ID := c_rec
6219                                                           .ORGANIZATION_ID;
6220         X_INSUFFICIENT_QTY_LINES_TBL(k).INVENTORY_ITEM_ID := c_rec
6221                                                             .INVENTORY_ITEM_ID;
6222         X_INSUFFICIENT_QTY_LINES_TBL(k).REQUESTED_QUANTITY := c_rec
6223                                                              .REQUESTED_QUANTITY;
6224         X_INSUFFICIENT_QTY_LINES_TBL(k).requested_quantity_uom := c_rec
6225                                                                  .requested_quantity_uom;
6226         X_INSUFFICIENT_QTY_LINES_TBL(k).ship_set_id := c_rec.ship_set_id;
6227         X_INSUFFICIENT_QTY_LINES_TBL(k).model_id := c_rec.top_model_line_id;
6228 
6229         if v_backorder_flag = 'Y' then
6230           --Backorder Line
6231 
6232           print_debug('In Backorder Line. Populating the G_backorder_qty_tab and G_backorder_deliv_tab',
6233                       l_debug);
6234 
6235           G_backorder_qty_tab(k + 1) := X_INSUFFICIENT_QTY_LINES_TBL(k)
6236                                        .REQUESTED_QUANTITY;
6237           G_backorder_deliv_tab(k + 1) := X_INSUFFICIENT_QTY_LINES_TBL(k)
6238                                          .DELIVERY_DETAIL_ID;
6239           g_dummy_table(k + 1) := null;
6240           print_debug('Delivery Detail Id ' || ' = ' ||
6241                       G_backorder_deliv_tab(k + 1),
6242                       l_debug);
6243           print_debug('Back Order Qty  ' || ' = ' ||
6244                       G_backorder_qty_tab(k + 1),
6245                       l_debug);
6246 
6247         end if;
6248 
6249         if v_reject_order_flag = 'Y' then
6250 
6251           l_insufficient_order_number(k) := X_INSUFFICIENT_QTY_LINES_TBL(k)
6252                                                         .source_header_number;
6253 
6254         end if;
6255 
6256         --  Storing all the insufficient delivery detail ids in l_insufficient_DD_TBL
6257 
6258         l_insufficient_DD_TBL(k) := X_INSUFFICIENT_QTY_LINES_TBL(k)
6259                                                       .DELIVERY_DETAIL_ID;
6260 
6261         k := k + 1;
6262 
6263       end if;
6264 
6265       --  end if;
6266       m := m + 1;
6267 
6268       if l_allocation_method <> 'X' and l_planning_method = 'A' then
6269 
6270         l_atr := l_atr - c_rec.REQUESTED_QUANTITY;
6271 
6272       end if;
6273       if l_atr < 0 then
6274 
6275         l_atr := 0;
6276 
6277       end if;
6278 
6279     end loop;
6280 
6281   end loop;
6282 
6283   --Planning Action for Credit Check Hold
6284 
6285   if v_credit_check_hold_flag = 'Y' then
6286 
6287     begin
6288       print_debug('In Credit Check Hold In Plan Wave API', l_debug);
6289       l_hold_index := 0;
6290 
6291       for l_credit_check in c_credit_check loop
6292 
6293         print_debug('Calling OE_Holds_pub.check_holds from plan_wave API',
6294                     l_debug);
6295 
6296         OE_HOLDS_PUB.Check_Holds(p_api_version   => 1.0,
6297                                  p_header_id     => l_credit_check.source_header_id,
6298                                  p_line_id       => l_credit_check.source_line_id,
6299                                  p_hold_id       => 1,
6300                                  p_entity_code   => 'O',
6301                                  p_entity_id     => NULL,
6302                                  x_result_out    => l_hold_result,
6303                                  x_msg_count     => l_msg_count,
6304                                  x_msg_data      => l_msg_data,
6305                                  x_return_status => l_return_status);
6306 
6307         if l_return_status = 'S' and l_hold_result = FND_API.G_TRUE then
6308           print_debug('Credit Check is on hold for Wave Line Id ' ||
6309                       l_credit_check.wave_line_id || ' in Wave header id ' ||
6310                       p_wave_header_id,
6311                       l_debug);
6312           -- put wave line id in credit_check pl/sql table
6313           l_credit_check_tbl(l_hold_index) := l_credit_check.wave_line_id;
6314           l_hold_index := l_hold_index + 1;
6315 
6316         end if;
6317 
6318         IF l_return_status <> fnd_api.g_ret_sts_success THEN
6319           RAISE fnd_api.g_exc_unexpected_error;
6320         END IF;
6321 
6322       end loop;
6323 
6324 --    l_tbl_count := l_credit_check_tbl.LAST;
6325   --  l_table_start := l_credit_check_tbl.FIRST;
6326 
6327      forall c1 in l_credit_check_tbl.FIRST ..l_credit_check_tbl.LAST
6328  -- for c1 in l_table_start ..l_tbl_count loop dbchange8
6329         update wms_wp_wave_lines
6330            set remove_from_wave_flag = 'Y',
6331                message               = 'Line removed from wave due to credit check hold'
6332          where wave_line_id = l_credit_check_tbl(c1)
6333            and wave_header_id = p_wave_header_id
6334            and nvl(remove_from_wave_flag, 'N') <> 'Y';
6335 
6336 
6337       l_credit_check_tbl.delete;
6338     exception
6339 
6340       when others then
6341         print_debug('Exception in Credit Check Hold In Plan Wave API',
6342                     l_debug);
6343         l_return_status := 'E';
6344         l_credit_check_tbl.delete;
6345     end;
6346     x_return_status := l_return_status;
6347   end if;
6348 
6349   -- ******* Planning Action for Lines with Insufficient Stock*********
6350   if X_INSUFFICIENT_QTY_LINES_TBL.count > 0 then
6351 
6352     if v_backorder_flag = 'Y' then
6353       --Backorder Line
6354 
6355       BEGIN
6356 
6357         print_debug('Calling Shipping API to backorder all  demand lines when sufficient qty is not available',
6358                     l_debug);
6359 
6360         WSH_SHIP_CONFIRM_ACTIONS2.backorder(p_detail_ids     => G_backorder_deliv_tab,
6361                                             p_bo_qtys        => G_backorder_qty_tab,
6362                                             p_req_qtys       => G_backorder_qty_tab,
6363                                             p_bo_qtys2       => G_dummy_table,
6364                                             p_overpick_qtys  => G_dummy_table,
6365                                             p_overpick_qtys2 => G_dummy_table,
6366                                             p_bo_mode        => 'UNRESERVE',
6367                                             p_bo_source      => 'PICK',
6368                                             x_out_rows       => l_out_rows,
6369                                             x_return_status  => l_return_status);
6370 
6371         print_debug('After call to Backorder API Return Status :' ||
6372                     l_return_status,
6373                     l_debug);
6374 
6375         IF l_return_status <> fnd_api.g_ret_sts_success THEN
6376           RAISE fnd_api.g_exc_unexpected_error;
6377 
6378         END IF;
6379 
6380         --Delete all entries in the pl/sql table
6381         G_backorder_deliv_tab.DELETE;
6382         G_backorder_qty_tab.DELETE;
6383         G_dummy_table.DELETE;
6384 
6385         x_return_status := l_return_status;
6386       EXCEPTION
6387         WHEN OTHERS THEN
6388 
6389           print_debug('Error in Backorder_wdd_for_repl: ' || sqlcode || ',' ||
6390                       sqlerrm,
6391                       l_debug);
6392 
6393           --Delete all entries in the pl/sql table
6394           G_backorder_deliv_tab.DELETE;
6395           G_backorder_qty_tab.DELETE;
6396           G_dummy_table.DELETE;
6397           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6398       END;
6399 
6400     end if;
6401 
6402     -- Reject Order Line from the Wave
6403 
6404     if v_reject_line_flag = 'Y' then
6405 
6406       print_debug('In Reject Line', l_debug);
6407 
6408       -- We will be doing bulk update  at the end for this
6409 
6410     end if;
6411 
6412     for i in X_INSUFFICIENT_QTY_LINES_TBL.FIRST .. X_INSUFFICIENT_QTY_LINES_TBL.LAST LOOP
6413 
6414       if v_reject_shipset_flag = 'Y' then
6415         -- Reject All Lines in Ship Set from Wave
6416 
6417         v_shipset_no := X_INSUFFICIENT_QTY_LINES_TBL(i).SHIP_SET_ID;
6418         print_debug('Plan Wave Ship Set with Ship Set Number  ' ||
6419                     v_shipset_no,
6420                     l_debug);
6421         OPEN c_shipset;
6422         FETCH c_shipset BULK COLLECT
6423           INTO X_DELIVERY_DETAIL_TBL;
6424         CLOSE c_shipset;
6425 
6426         -- Find all the lines that has the same Ship Set No
6427 
6428           -- l_tbl_count := X_DELIVERY_DETAIL_TBL.count;
6429 
6430 
6431 
6432         if X_DELIVERY_DETAIL_TBL.count > 0 then
6433           --forall l in l_table_start .. l_tbl_count   dbchange9
6434           forall l in X_DELIVERY_DETAIL_TBL.FIRST .. X_DELIVERY_DETAIL_TBL.LAST
6435             update wms_wp_wave_lines
6436                set remove_from_wave_flag = 'Y',
6437                    message               = 'Line removed from wave due to insufficient quantity'
6438              where delivery_Detail_id = X_DELIVERY_DETAIL_TBL(l)
6439                and wave_header_id = p_wave_header_id;
6440 
6441 
6442           X_DELIVERY_DETAIL_TBL.delete;
6443         end if;
6444       end if;
6445 
6446       if v_reject_model_flag = 'Y' then
6447         -- Reject all lines for the Model from Wave
6448 
6449         --Get the Model number for the lines from WDD based on delivery_detail_id
6450         --Assuming model to be top_model_line_id --????NEED to confirm with Satish
6451 
6452         v_model_no := X_INSUFFICIENT_QTY_LINES_TBL(i).model_id;
6453         print_debug('In Reject Model : Model No  ' || v_model_no, l_debug);
6454         OPEN c_model;
6455         FETCH c_model BULK COLLECT
6456           INTO X_DELIVERY_DETAIL_TBL;
6457         CLOSE c_model;
6458 
6459 
6460 
6461         if X_DELIVERY_DETAIL_TBL.count > 0 then
6462           -- Find all the lines that has the same Model No
6463       -- forall l in l_table_start .. l_tbl_count    dbchange10
6464          forall l in X_DELIVERY_DETAIL_TBL.FIRST .. X_DELIVERY_DETAIL_TBL.LAST
6465             update wms_wp_wave_lines
6466                set remove_from_wave_flag = 'Y',
6467                    message               = 'Line removed from wave due to insufficient quantity'
6468              where delivery_Detail_id = X_DELIVERY_DETAIL_TBL(l)
6469                and wave_header_id = p_wave_header_id;
6470 
6471           X_DELIVERY_DETAIL_TBL.delete;
6472         end if;
6473       end if;
6474     end loop;
6475 
6476     if v_reject_order_flag = 'Y' then
6477       print_debug('In Reject Order', l_debug);
6478       -- Reject Entire Order from Wave
6479      -- forall i3 in l_table_start .. l_tbl_count dbchange11
6480      forall i3 in l_insufficient_order_number.FIRST .. l_insufficient_order_number.LAST
6481         update wms_wp_wave_lines --11775489 get from wdd... FIXED
6482            set remove_from_wave_flag = 'Y',
6483                message               = 'Line removed from wave due to insufficient quantity'
6484          where wave_line_id in (select wwl.wave_line_id
6485 		                        from wms_wp_wave_lines wwl, wsh_delivery_details wdd
6486 								where wwl.wave_header_id = p_wave_header_id
6487 								  and nvl(wwl.remove_from_wave_flag, 'N') <> 'Y'
6488 								  and wwl.delivery_detail_id = wdd.delivery_detail_id
6489 								  and wwl.organization_id = wdd.organization_id
6490 								  and wdd.source_header_number = l_insufficient_order_number(i3));
6491       l_insufficient_order_number.delete;
6492     end if;
6493 
6494     if v_reject_line_flag = 'Y' or v_backorder_flag = 'Y' then
6495 
6496     -- l_tbl_count := l_insufficient_DD_TBL.count;
6497 
6498 
6499 
6500      --forall i2 in l_table_start .. l_tbl_count dbchange12
6501     forall i2 in l_insufficient_DD_TBL.FIRST .. l_insufficient_DD_TBL.LAST
6502         update wms_wp_wave_lines
6503            set remove_from_wave_flag = 'Y',
6504                message               = 'Line removed from wave due to insufficient quantity'
6505          where delivery_Detail_id = l_insufficient_DD_TBL(i2)
6506            and wave_header_id = p_wave_header_id
6507            and nvl(remove_from_wave_flag, 'N') <> 'Y';
6508 
6509       l_insufficient_DD_TBL.delete;
6510 
6511     end if;
6512   END IF;
6513 
6514   if X_SUFFICIENT_QTY_LINES_TBL.count > 0 then
6515 
6516     --Auto create Deliveries for the delivery ids.
6517 
6518     if v_create_delivery_flag = 'Y' then
6519 
6520       begin
6521 
6522         WSH_DELIVERY_DETAILS_PUB.Autocreate_Deliveries(
6523                                                        -- Standard parameters
6524                                                        p_api_version_number => 1.0,
6525                                                        p_init_msg_list      => FND_API.G_FALSE,
6526                                                        p_commit             => FND_API.G_FALSE,
6527                                                        x_return_status      => l_return_status,
6528                                                        x_msg_count          => l_msg_count,
6529                                                        x_msg_data           => l_msg_data,
6530                                                        p_line_rows          => l_TabOfDelDets,
6531                                                        x_del_rows           => l_del_rows);
6532 
6533         IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
6534 
6535           print_debug('Error returned from Auto Create Deliveries API: ' ||
6536                       l_return_status,
6537                       l_debug);
6538 
6539           -- Raise an exception.  The caller will do the rollback, cleanups,
6540           RAISE FND_API.G_EXC_ERROR;
6541         END IF;
6542       exception
6543         when others then
6544 
6545           print_debug('Error returned from Auto Create Deliveries API: ' ||
6546                       l_return_status,
6547                       l_debug);
6548 
6549       end;
6550 
6551     end if;
6552   end if;
6553 
6554   --Calculate the Planned Fill Rate for the Wave
6555   /*  WMS_WAVE_PLANNING_PVT.get_line_fill_rate(x_return_status,
6556                                            p_wave_header_id);
6557   print_debug('Status after Call to Get the Fill Rate' ||
6558               x_return_status,
6559               l_debug); */
6560 
6561   if l_allocation_method in ('C', 'N', 'X') then
6562 
6563     -- Update the crossdock qty in Lines Table
6564 
6565     select delivery_detail_id, crossdocked_quantity bulk collect
6566       into x_wp_crossdock_tbl
6567       from wms_wp_rules_simulation
6568        where wave_header_id=p_wave_header_id;
6569 
6570    -- forall i in indices of x_wp_crossdock_tbl
6571    if x_wp_crossdock_tbl.count > 0 then
6572       for i in  x_wp_crossdock_tbl.FIRST .. x_wp_crossdock_tbl.LAST loop
6573       update wms_wp_wave_lines
6574          set crossdock_quantity = x_wp_crossdock_tbl(i).crossdock_qty
6575        where delivery_detail_id = x_wp_crossdock_tbl(i).delivery_detail_id
6576          and wave_header_id = p_wave_header_id;
6577   end loop;
6578 end if;
6579   end if;
6580 
6581 
6582 
6583 
6584   if X_CONSOL_LINES_wave_hdr_id.count > 0 then
6585    -- forall m2 in l_table_start .. l_tbl_count dbchange13
6586       forall m2 in X_CONSOL_LINES_wave_hdr_id.FIRST .. X_CONSOL_LINES_wave_hdr_id.LAST
6587       update wms_wp_wave_lines
6588          set planned_fill_rate = X_CONSOL_LINES_fill_rate(m2)
6589        where wave_line_id = X_CONSOL_LINES_wave_line_id(m2);
6590 
6591   end if;
6592 
6593 if l_wave_status <> 'Released' then
6594   print_debug('Before Calling API to Update Wave Status: ', l_debug);
6595   update_wave_header_status(x_return_status, p_wave_header_id, 'Planned');
6596 end if;
6597   -- Need to check whether the set up is done else we wont invoke Labor Planning
6598 
6599   print_debug('Return Status after Plan Wave is ' || x_return_status,
6600               l_debug);
6601 
6602   commit;
6603 
6604   print_debug('Checking if Labor Planning Set up or Department Setup  is done ',
6605               l_debug);
6606   select count(1)
6607     into v_labor_count
6608     from wms_wp_labor_planning
6609    where planning_Criteria_id = p_planning_Criteria_id;
6610 
6611   if v_labor_count > 0 and l_enable_labor_planning = 'Y' then
6612 
6613    savepoint labor_planning_sp;
6614 
6615     if l_planning_method = 'A' then
6616 
6617       print_debug('Setup is available for Labor Planning. Calling Availability Check Labor Planning API ',
6618                   l_debug);
6619 
6620       WMS_WAVE_PLANNING_PVT.labor_planning(p_wave_header_id,
6621                                            p_planning_criteria_id,
6622                                            x_return_status);
6623 
6624     elsif l_planning_method = 'R' and
6625           l_allocation_method in ('C', 'N', 'X') then
6626       print_debug('Setup is available for Labor Planning. Calling Rule Based  Labor Planning API ',
6627                   l_debug);
6628 
6629       rules_labor_planning(p_planning_criteria_id,
6630                            p_wave_header_id,
6631                            -1,
6632                            l_organization_id,
6633                            x_return_status);
6634 
6635   end if;
6636 
6637     if x_return_status <> 'S' then
6638 
6639       rollback to labor_planning_sp;
6640     else
6641       commit;
6642     end if;
6643 
6644 
6645   else
6646 
6647     print_debug('Setup is not available for Labor Planning. Please check the Labor Planning  setup',
6648                 l_debug);
6649     delete from wms_wp_labor_statistics
6650      where wave_header_id = p_wave_header_id;
6651     commit;
6652   end if;
6653 
6654   print_debug('Return Status after Plan Wave is ' || x_return_status,
6655               l_debug);
6656 
6657  delete from wms_wp_rules_simulation where wave_header_id=p_wave_header_id;
6658  commit;
6659 
6660 exception
6661 
6662   WHEN start_date_exception THEN
6663     print_debug('START DATE PROBLEM. SO DID NOT PLAN', l_debug);
6664 
6665   when others THEN
6666     print_debug('Error in Plan Wave API: ' || SQLCODE || ' : ' || SQLERRM,
6667                 l_debug);
6668                  delete from wms_wp_rules_simulation where wave_header_id=p_wave_header_id;
6669                  commit;
6670     x_return_status := FND_API.G_RET_STS_ERROR;
6671 
6672 end Plan_Wave;
6673 
6674 
6675   -- start time changes
6676  -- copy entire get_available_capacity
6677 
6678 /*function get_resource_capacity(p_wave_start_time  number,
6679                                p_wave_end_time    number,
6680                                p_shift_start_time number,
6681                                p_shift_end_time   number,
6682                                p_date_diff        number) return number is
6683   l_debug              NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'),
6684                                      0);
6685   wst                  number := p_wave_start_time;
6686   wet                  number := p_wave_end_time;
6687   sst                  number := p_shift_start_time;
6688   shet                 number := p_shift_end_time;
6689   l_date_diff          number := p_date_diff;
6690   l_available_capacity number := 0;
6691   l_start_day_time NUMBER := 0;
6692   l_end_day_time NUMBER := 0;
6693 begin
6694 
6695   print_debug('In get_resource_capacity function',l_debug);
6696   if (l_date_diff) >= 1 then
6697     -- utilization on the first day
6698     -- NO NEED TO CHECK AGAIN. THIS PART IS OKAY.
6699     if wst > sst THEN
6700       IF shet > wst THEN
6701         l_start_day_time := shet - wst;
6702         print_debug('l_start_day_time(1) := ' || l_start_day_time, l_debug);
6703       ELSE
6704         l_start_day_time := 0;
6705         print_debug('l_start_day_time(1) := ' || l_start_day_time, l_debug);
6706       END IF;
6707     elsif wst <= sst then
6708       l_start_day_time := shet - sst;
6709       print_debug('l_start_day_time(2) := ' || l_start_day_time, l_debug);
6710     end if;
6711     -- utilization on the last day
6712     -- NO NEED TO CHECK AGAIN. THIS PART IS OKAY.
6713     if wet >= shet then
6714       l_end_day_time := shet - sst;
6715       print_debug('l_end_day_time(1) := ' || l_end_day_time, l_debug);
6716     elsif wet < shet THEN
6717       IF wet > sst THEN
6718         l_end_day_time := wet - sst;
6719         print_debug('l_end_day_time(2) := ' || l_end_day_time, l_debug);
6720       ELSE
6721         l_end_day_time := 0;
6722         print_debug('l_end_day_time(2) := ' || l_end_day_time, l_debug);
6723       END IF;
6724     end if;
6725 
6726     l_available_capacity := l_available_capacity + l_end_day_time +
6727                             l_start_day_time +
6728                             ((l_date_diff) - 1) * (shet - sst);
6729     print_debug('l_daily_capacity := ' ||
6730                 ((l_date_diff) - 1) * (shet - sst),
6731                 l_debug);
6732     print_debug('l_available_capacity := ' || l_available_capacity,
6733                 l_debug);
6734 
6735   elsif (l_date_diff) = 0 THEN
6736     IF (wst BETWEEN sst AND shet) OR (wet BETWEEN sst AND shet) or
6737        (wst < sst and wet > shet) then
6738       IF wst > sst THEN
6739         IF wet >= shet THEN
6740           l_available_capacity := (shet - wst);
6741           print_debug('l_available_capacity1 := ' || l_available_capacity,
6742                       l_debug);
6743         ELSIF wet < shet THEN
6744           l_available_capacity := (wet - wst);
6745           print_debug('l_available_capacity2 := ' || l_available_capacity,
6746                       l_debug);
6747         END IF;
6748       ELSIF wst <= sst THEN
6749         IF wet >= shet THEN
6750           l_available_capacity := (shet - sst);
6751           print_debug('l_available_capacity3 := ' || l_available_capacity,
6752                       l_debug);
6753         ELSIF wet < shet THEN
6754           l_available_capacity := (wet - sst);
6755           print_debug('l_available_capacity4 := ' || l_available_capacity,
6756                       l_debug);
6757         END IF;
6758       END IF;
6759     END IF;
6760   end if;
6761   print_debug('End of get_resource_capacity function',l_debug);
6762   return l_available_capacity;
6763 
6764 exception
6765   when others then
6766     print_debug('Error in get_resource_capacity function', l_debug);
6767     return - 1;
6768 end;
6769 */
6770  function get_resource_capacity(p_wave_start_time  number,
6771                                p_wave_end_time    number,
6772                                p_shift_start_time number,
6773                                p_shift_end_time   number,
6774                                p_date_diff        number) return number is
6775   l_debug              NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'),
6776                                      0);
6777   wst                  number := p_wave_start_time;
6778   wet                  number := p_wave_end_time;
6779   sst                  number := p_shift_start_time;
6780   shet                 number := p_shift_end_time;
6781   l_date_diff          number := p_date_diff;
6782   l_available_capacity number := 0;
6783   l_start_day_time NUMBER := 0;
6784   l_end_day_time NUMBER := 0;
6785 begin
6786 
6787   print_debug('In get_resource_capacity function',l_debug);
6788   if (l_date_diff) >= 1 then
6789     -- utilization on the first day
6790     -- NO NEED TO CHECK AGAIN. THIS PART IS OKAY.
6791     if shet > sst then
6792     if wst > sst THEN
6793       IF shet > wst THEN
6794         l_start_day_time := shet - wst;
6795         print_debug('l_start_day_time(1) := ' || l_start_day_time, l_debug);
6796       ELSE
6797         l_start_day_time := 0;
6798         print_debug('l_start_day_time(1) := ' || l_start_day_time, l_debug);
6799       END IF;
6800     elsif wst <= sst then
6801       l_start_day_time := shet - sst;
6802       print_debug('l_start_day_time(2) := ' || l_start_day_time, l_debug);
6803     end if;
6804     -- utilization on the last day
6805     -- NO NEED TO CHECK AGAIN. THIS PART IS OKAY.
6806     if wet >= shet then
6807       l_end_day_time := shet - sst;
6808       print_debug('l_end_day_time(1) := ' || l_end_day_time, l_debug);
6809     elsif wet < shet THEN
6810       IF wet > sst THEN
6811         l_end_day_time := wet - sst;
6812         print_debug('l_end_day_time(2) := ' || l_end_day_time, l_debug);
6813       ELSE
6814         l_end_day_time := 0;
6815         print_debug('l_end_day_time(2) := ' || l_end_day_time, l_debug);
6816       END IF;
6817     end if;
6818 
6819     l_available_capacity := l_available_capacity + l_end_day_time +
6820                             l_start_day_time +
6821                             ((l_date_diff) - 1) * (shet - sst);
6822 
6823     elsif shet < sst then
6824     -- start day calculation
6825       if wst > sst then
6826          l_start_day_time := 1440 - wst;
6827       elsif wst < sst then
6828          l_start_day_time := 1440 - sst;
6829       end if;
6830 
6831     -- end day calculation
6832       if wet > shet then
6833         l_end_day_time := shet;
6834       elsif wet < shet then
6835 	l_end_day_time := wet;
6836       end if;
6837 
6838     l_available_capacity := l_available_capacity + l_end_day_time +
6839                             l_start_day_time +
6840                             ((l_date_diff) - 1) * ((1440 - sst) + shet);
6841     end if;
6842     print_debug('l_daily_capacity := ' ||
6843                 ((l_date_diff) - 1) * (shet - sst),
6844                 l_debug);
6845     print_debug('l_available_capacity := ' || l_available_capacity,
6846                 l_debug);
6847 
6848   elsif (l_date_diff) = 0 THEN
6849     IF (wst BETWEEN sst AND shet) OR (wet BETWEEN sst AND shet) or
6850        (wst < sst and wet > shet) then
6851       IF wst > sst THEN
6852         IF wet >= shet THEN
6853           l_available_capacity := (shet - wst);
6854           print_debug('l_available_capacity1 := ' || l_available_capacity,
6855                       l_debug);
6856         ELSIF wet < shet THEN
6857           l_available_capacity := (wet - wst);
6858           print_debug('l_available_capacity2 := ' || l_available_capacity,
6859                       l_debug);
6860         END IF;
6861       ELSIF wst <= sst THEN
6862         IF wet >= shet THEN
6863           l_available_capacity := (shet - sst);
6864           print_debug('l_available_capacity3 := ' || l_available_capacity,
6865                       l_debug);
6866         ELSIF wet < shet THEN
6867           l_available_capacity := (wet - sst);
6868           print_debug('l_available_capacity4 := ' || l_available_capacity,
6869                       l_debug);
6870         END IF;
6871       END IF;
6872     END IF;
6873   end if;
6874   print_debug('End of get_resource_capacity function',l_debug);
6875   return l_available_capacity;
6876 
6877 exception
6878   when others then
6879     print_debug('Error in get_resource_capacity function', l_debug);
6880     return - 1;
6881 end;
6882 
6883 function get_available_capacity(p_resource_id     number,
6884                                 p_dept_id         number,
6885                                 p_start_date      date, -- start time changes
6886                                 p_target_date     date,
6887                                 p_24hrs           number,
6888                                 p_org_id          number,
6889                                 l_unit_of_measure in varchar2)
6890   return NUMBER is
6891 
6892   l_instance_count     number := 0;
6893   l_available_capacity number := 0;
6894   l_day_diff           NUMBER;
6895   l_temp_capacity      number := 0;
6896   l_debug              NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'),
6897                                      0);
6898 
6899   wst              number := round((p_start_date - trunc(p_start_date)) * 24 * 60);
6900   wet              number := round((p_target_date - trunc(p_target_date)) * 24 * 60);
6901   shet             number := 0;
6902   sst              number := 0;
6903   l_date_diff      number := 0;
6904   l_start_day_time number := 0;
6905   l_end_day_time   number := 0;
6906   resource_capacity_exception exception;
6907   l_conv_rate NUMBER;
6908 
6909   cursor c_shift is
6910     SELECT distinct round(bst.to_time / 60) end_time,
6911                     round(bst.from_time / 60) start_time,
6912                     nvl(brs.CAPACITY_UNITS, 0) cap_units,
6913                     brs.shift_num shift_num
6914       from bom_resource_shifts brs,
6915            mtl_parameters      mp,
6916            bom_shift_dates     bsd,
6917            bom_shift_times     bst
6918      where brs.department_id = p_dept_id
6919        and brs.resource_id = p_resource_id
6920        and mp.organization_id = p_org_id
6921        and mp.calendar_code = bsd.calendar_code
6922        and mp.calendar_exception_set_id = bsd.exception_set_id
6923        and brs.shift_num = bsd.shift_num
6924        --and bsd.shift_date BETWEEN Trunc(SYSDATE) AND Trunc(SYSDATE + 1)
6925        and bsd.seq_num is not null
6926        and bst.shift_num = bsd.shift_num
6927        and bst.calendar_code = bsd.calendar_code
6928      order BY brs.shift_num;
6929 
6930   type shift_details_record is record(
6931     end_time   number,
6932     start_time number,
6933     cap_units  number,
6934     shift_num  number);
6935   TYPE shift_tbl IS TABLE OF shift_details_record INDEX BY BINARY_INTEGER;
6936   l_shift_tbl shift_tbl;
6937 
6938 BEGIN
6939   print_debug('p_start_date := ' ||
6940               To_Char(p_start_date, 'DD-MON-RRRR HH24:MI:SS'),
6941               l_debug);
6942   print_debug('p_target_date := ' ||
6943               To_Char(p_target_date, 'DD-MON-RRRR HH24:MI:SS'),
6944               l_debug);
6945 
6946   print_debug('wst := ' || wst, l_debug);
6947   print_debug('wet := ' || wet, l_debug);
6948 
6949   l_date_diff := trunc(p_target_date) - trunc(p_start_date);
6950 
6951   print_debug('l_date_diff := ' || l_date_diff, l_debug);
6952 
6953   if (p_24hrs = 1) then
6954     print_debug('P_24HRS = 1 ===> available 24 hrs', l_debug);
6955     select count(*)
6956       into l_instance_count
6957       from BOM_DEPT_RES_INSTANCES
6958      WHERE resource_id = p_resource_id
6959        and department_id = p_dept_id;
6960 
6961     l_temp_capacity := wms_wave_planning_pvt.get_resource_capacity(wst,
6962                                                                    wet,
6963                                                                    0,
6964                                                                    1440,
6965                                                                    l_date_diff);
6966 
6967     if l_temp_capacity = -1 then
6968       raise resource_capacity_exception;
6969     end if;
6970 
6971     l_available_capacity := l_instance_count * l_temp_capacity; -- in minutes
6972 
6973   elsif (p_24hrs = 2) then
6974     print_debug('P_24HRS = 2 ===> available in shifts', l_debug);
6975     l_available_capacity := 0;
6976 
6977     open c_shift;
6978     fetch c_shift BULK collect
6979       into l_shift_tbl;
6980 
6981     for i in l_shift_tbl.first .. l_shift_tbl.last loop
6982 
6983       shet := l_shift_tbl(i).end_time;
6984       sst  := l_shift_tbl(i).start_time;
6985       print_debug('shet := ' || shet,l_debug);
6986       print_debug('sst := ' || sst,l_debug);
6987 
6988       l_temp_capacity := wms_wave_planning_pvt.get_resource_capacity(wst,
6989                                                                      wet,
6990                                                                      sst,
6991                                                                      shet,
6992                                                                      l_date_diff);
6993       if l_temp_capacity = -1 then
6994         raise resource_capacity_exception;
6995       end if;
6996 
6997       l_available_capacity := l_available_capacity +
6998                               (l_temp_capacity * l_shift_tbl(i).cap_units);
6999 
7000     end loop;
7001 
7002   end if;
7003   print_debug('l_available_capacity before conversion := ' || l_available_capacity, l_debug);
7004   l_conv_rate := round(wms_wave_planning_pvt.get_conversion_rate(null,
7005 
7006                                                                  'MIN',
7007 
7008                                                                  l_unit_of_measure));
7009 
7010 print_debug('Conversion rat ='||l_conv_rate,l_debug);
7011 
7012   l_available_capacity := l_conv_rate *  l_available_capacity;
7013   print_debug('l_available_capacity after conversion := ' || l_available_capacity, l_debug);
7014 
7015   print_debug('End of get_available_capacity function',l_debug);
7016 
7017   return l_available_capacity;
7018 
7019 EXCEPTION
7020   WHEN resource_capacity_exception THEN
7021     print_debug('Error from get_resource_capacity function',l_debug);
7022     return - 1;
7023   WHEN OTHERS THEN
7024     print_debug('Unknown Error in get_available_capacity function',l_debug);
7025     return - 1;
7026 
7027 end get_available_capacity;
7028 
7029 
7030 
7031    procedure get_current_work_load(p_resource             in varchar2,
7032                                 p_planning_criteria_id in number,
7033                                 p_wave_header_id       in number,
7034                                 x_current_workload     out nocopy number,
7035                                 x_resource_type        out nocopy NUMBER,
7036                                 x_number_of_tasks      OUT nocopy NUMBER,
7037                                 x_total_capacity       out nocopy NUMBER) IS
7038   -- start time changes
7039 
7040   cursor c_resource_details is
7041     select source_subinventory,
7042            destination_subinventory,
7043            pick_uom,
7044            transaction_time,
7045            travel_time,
7046            processing_overhead_duration
7047       from wms_wp_labor_planning
7048      where planning_criteria_id = p_planning_criteria_id
7049        and resource_type = p_resource;
7050 
7051   l_unit_of_measure      varchar2(10);
7052   l_uom                  varchar2(10);
7053   l_capacity_units       number := 0;
7054   l_current_workload     number := 0;
7055   l_transaction_quantity number := 0;
7056   l_resource_type        number := 0; -- 1 => machine  2 => person
7057   l_load                 number := 0;
7058   l_utilization          number;
7059   l_efficiency           number;
7060   l_number_of_tasks      NUMBER := 0;
7061   l_tasks                NUMBER := 0;
7062   l_resource_id          number := 0;
7063   l_dept_id              number := 0;
7064   l_target_date          date;
7065   l_org_id               number;
7066   l_24hrs                number;
7067   l_debug                NUMBER := nvl(fnd_profile.VALUE('INV_DEBUG_TRACE'),
7068                                        0);
7069   l_total_capacity       NUMBER;
7070   l_LABOR_SETUP_MODE     varchar2(1); -- ssk
7071   l_start_date           DATE; -- start time changes
7072   st_exception EXCEPTION; -- start time changes
7073 
7074 begin
7075 -- ssk
7076 select LABOR_SETUP_MODE into l_LABOR_SETUP_MODE from wms_wp_planning_criteria_vl
7077 where PLANNING_CRITERIA_ID = p_planning_criteria_id;
7078 -- ssk
7079   select department_id
7080     into l_dept_id
7081     from wms_wp_planning_criteria_vl
7082    where planning_criteria_id = p_planning_criteria_id;
7083   print_debug('In get_current_work_load procedure', l_debug);
7084   select (nvl(utilization, 100) / 100),
7085          (nvl(efficiency, 100) / 100),
7086          capacity_units,
7087          unit_of_measure,
7088          resource_id,
7089          ORGANIZATION_ID,
7090          AVAILABLE_24_HOURS_FLAG
7091     into l_utilization,
7092          l_efficiency,
7093          l_capacity_units,
7094          l_unit_of_measure,
7095          l_resource_id,
7096          l_org_id,
7097          l_24hrs
7098     from bom_department_resources_v
7099    where department_id = l_dept_id
7100      and resource_code = p_resource;
7101 
7102   l_current_workload := 0;
7103 
7104   for l_resource_details in c_resource_details loop
7105   if l_labor_setup_mode = 'S' then
7106     begin
7107       select sum(mmtt.transaction_quantity), Count(*), br.resource_type
7108         into l_transaction_quantity, l_number_of_tasks, l_resource_type
7109         from bom_resources                  br,
7110              mtl_material_transactions_temp mmtt,
7111              wms_dispatched_tasks           wdt
7112        where mmtt.subinventory_code =
7113              l_resource_details.source_subinventory
7114          and mmtt.transfer_subinventory =
7115              l_resource_details.destination_subinventory
7116          and mmtt.transaction_uom = l_resource_details.pick_uom
7117          and mmtt.transaction_temp_id = wdt.transaction_temp_id(+)
7118          and wdt.person_resource_id = br.resource_id(+)
7119          and br.resource_code = p_resource
7120        group by br.resource_type;
7121     exception
7122       when no_data_found then
7123         l_transaction_quantity := 0;
7124         SELECT resource_type
7125           INTO l_resource_type
7126           FROM bom_resources
7127          WHERE resource_code = p_resource
7128            AND resource_id IN
7129                (SELECT DISTINCT resource_id
7130                   FROM BOM_DEPARTMENT_RESOURCES
7131                  WHERE department_id IN
7132                        (SELECT DISTINCT department_id
7133                           FROM wms_wp_planning_criteria_vl
7134                          WHERE planning_criteria_id = p_planning_criteria_id));
7135     end;
7136     elsif l_labor_setup_mode = 'Z' then
7137     begin
7138       select sum(mmtt.transaction_quantity), Count(*), br.resource_type
7139         into l_transaction_quantity, l_number_of_tasks, l_resource_type
7140         from bom_resources                  br,
7141              mtl_material_transactions_temp mmtt,
7142              wms_dispatched_tasks           wdt,
7143 	     wms_zone_locators              wzl1,
7144            wms_zone_locators              wzl2,
7145            wms_zones_vl                   wz1,
7146            wms_zones_vl                   wz2
7147        where wz1.zone_name =
7148              l_resource_details.source_subinventory
7149          and wz2.zone_name =
7150              l_resource_details.destination_subinventory
7151 	and mmtt.locator_id = wzl1.inventory_location_id
7152        and mmtt.transfer_to_location = wzl2.inventory_location_id
7153        and mmtt.organization_id = wzl1.organization_id
7154        and mmtt.organization_id = wzl2.organization_id
7155        and wz1.zone_id = wzl1.zone_id
7156        and wz2.zone_id = wzl2.zone_id
7157        and wz1.zone_type = 'L'
7158        and wz2.zone_type = 'L'
7159          and mmtt.transaction_uom = l_resource_details.pick_uom
7160          and mmtt.transaction_temp_id = wdt.transaction_temp_id(+)
7161          and wdt.person_resource_id = br.resource_id(+)
7162          and br.resource_code = p_resource
7163        group by br.resource_type;
7164     exception
7165       when no_data_found then
7166         l_transaction_quantity := 0;
7167         SELECT resource_type
7168           INTO l_resource_type
7169           FROM bom_resources
7170          WHERE resource_code = p_resource
7171            AND resource_id IN
7172                (SELECT DISTINCT resource_id
7173                   FROM BOM_DEPARTMENT_RESOURCES
7174                  WHERE department_id IN
7175                        (SELECT DISTINCT department_id
7176                           FROM wms_wp_planning_criteria_vl
7177                          WHERE planning_criteria_id = p_planning_criteria_id));
7178                          END;
7179     end if;
7180     l_tasks := l_tasks + l_number_of_tasks;
7181     print_debug('current load = ' || l_current_workload, l_debug);
7182     print_debug('l_transaction_quantity = ' || l_transaction_quantity,
7183                 l_debug);
7184     IF (l_transaction_quantity > 0) then
7185       l_load             := (l_transaction_quantity *
7186                             nvl(l_resource_details.transaction_time,0)) +
7187                             nvl(l_resource_details.travel_time,0) + nvl(l_resource_details.processing_overhead_duration,0);
7188       l_current_workload := l_current_workload + l_load;
7189       print_debug('current load = ' || l_current_workload, l_debug);
7190     END IF;
7191   end loop;
7192 
7193   select time_uom
7194     into l_uom
7195     from wms_wp_planning_criteria_vl
7196    where planning_Criteria_id = p_planning_criteria_id;
7197 
7198   x_current_workload := round((l_current_workload) /
7199                               (l_utilization * l_efficiency));
7200   x_resource_type    := l_resource_type;
7201   x_number_of_tasks  := l_tasks;
7202   SELECT MAX(nvl(wdab.end_time,
7203                  (nvl(wts.planned_departure_date,
7204                       nvl(wnd.latest_pickup_date, wdd.date_scheduled)))))
7205     into l_target_date
7206     FROM wms_wp_wave_lines        wwl, --11775489 get from wdd... FIXED check performance impact
7207          wsh_delivery_details     wdd,
7208          wsh_delivery_assignments wda,
7209          wsh_new_deliveries       wnd,
7210          wsh_delivery_legs        wdl,
7211          wsh_trip_stops           wts,
7212          wms_dock_appointments_b  wdab
7213    WHERE wwl.organization_id = l_org_id
7214      AND wwl.wave_header_id = p_wave_header_id
7215      AND wwl.delivery_detail_id = wdd.delivery_detail_id
7216 	 and wdd.organization_id = wwl.organization_id
7217      AND wdd.delivery_detail_id = wda.delivery_detail_id
7218      AND wda.delivery_id = wnd.delivery_id(+)
7219      AND wnd.delivery_id = wdl.delivery_id(+)
7220      AND wdl.pick_up_stop_id = wts.stop_id(+)
7221      AND wts.stop_id = wdab.trip_stop(+);
7222 
7223   -- start time changes start
7224   SELECT start_time
7225     INTO l_start_date
7226     FROM wms_wp_wave_headers_vl
7227    WHERE wave_header_id = p_wave_header_id;
7228   print_debug('start_time = ' || l_start_date, l_debug);
7229 
7230   -- start time changes end
7231 
7232   l_total_capacity := get_available_capacity(l_resource_id,
7233                                              l_dept_id,
7234                                              l_start_date, -- start time changes
7235                                              l_target_date,
7236                                              l_24hrs,
7237                                              l_org_id,
7238                                              l_unit_of_measure);
7239 
7240   x_total_capacity := round(round(l_total_capacity *
7241 
7242                                   wms_wave_planning_pvt.get_conversion_rate(null,
7243 
7244                                                                             l_unit_of_measure,
7245 
7246                                                                             l_uom)));
7247 
7248   print_debug('resource is ' || p_resource, l_debug);
7249   print_debug('current_workload = ' || x_current_workload, l_debug);
7250   print_debug('resource_type = ' || x_resource_type, l_debug);
7251   print_debug('no of tasks = ' || x_number_of_tasks, l_debug);
7252   print_debug('x_total_capacity = ' || x_total_capacity, l_debug);
7253 
7254 EXCEPTION
7255   when others then
7256     print_debug('Error in get_current_workload function' || ':' || SQLCODE || ':' ||
7257                 SQLERRM,
7258                 l_debug);
7259 end get_current_work_load;
7260 
7261 
7262   /*FUNCTION get_next_resource(j                          NUMBER,
7263                              p_source_subinventory      VARCHAR2,
7264                              p_destination_subinventory VARCHAR2,
7265                              p_uom                      VARCHAR2,
7266                              p_resource_type            NUMBER) RETURN NUMBER IS
7267     i               number := j + 1;
7268     l_found         NUMBER := 0;
7269     l_next_resource NUMBER := -1;
7270     l_debug         NUMBER := nvl(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
7271   BEGIN
7272     if p_resource_type = 2 then
7273       while i < x_labor_dtl_tbl.count loop
7274         if x_labor_dtl_tbl(i).source_subinventory = p_source_subinventory AND
7275            x_labor_dtl_tbl(i)
7276         .destination_subinventory = p_destination_subinventory AND
7277            x_labor_dtl_tbl(i).pick_uom = p_uom AND x_labor_dtl_tbl(i)
7278         .resource_type = p_resource_type then
7279           l_found         := 1;
7280           l_next_resource := i;
7281           exit;
7282         ELSE
7283           i := i + 1;
7284         end if;
7285       end loop;
7286 
7287     elsif p_resource_type = 1 then
7288       while i < x_machine_dtl_tbl.count loop
7289         if x_machine_dtl_tbl(i).source_subinventory = p_source_subinventory AND
7290            x_machine_dtl_tbl(i)
7291         .destination_subinventory = p_destination_subinventory AND
7292            x_machine_dtl_tbl(i).pick_uom = p_uom AND x_machine_dtl_tbl(i)
7293         .resource_type = p_resource_type then
7294           l_found         := 1;
7295           l_next_resource := i;
7296           exit;
7297         ELSE
7298           i := i + 1;
7299         end if;
7300       end loop;
7301     end if;
7302 
7303     if l_found = 1 then
7304       return l_next_resource;
7305     ELSE
7306       return - 1;
7307     end if;
7308 
7309   EXCEPTION
7310     WHEN OTHERS THEN
7311       print_debug('Error in get_next_resource function ' || ':' || SQLCODE || ':' ||
7312                   SQLERRM,
7313                   l_debug);
7314   END;
7315 
7316   procedure sync_machine_person_time(p_planned_load             number,
7317                                      p_planned_tasks            number,
7318                                      p_source_subinventory      varchar2,
7319                                      p_destination_subinventory varchar2,
7320                                      p_uom                      varchar2) is
7321 
7322     l_debug              NUMBER := nvl(fnd_profile.VALUE('INV_DEBUG_TRACE'),
7323                                        0);
7324     l_planned_load       number := p_planned_load;
7325     l_planned_tasks      number := p_planned_tasks;
7326     l_resource_type      number := 1; -- hard coded. should not be changed
7327     l_completed          number := 0;
7328     l_temp_planned_tasks number := 0;
7329     j                    number := 0;
7330     j_temp               number := 0;
7331     i                    number := 0;
7332 
7333   begin
7334     print_debug('In sync_machine_person_time api', l_debug);
7335     print_debug('planned load passed to this api is ' || p_planned_load,
7336                 l_debug);
7337     print_debug('planned tasks passed to this api are ' || p_planned_tasks,
7338                 l_debug);
7339     print_debug('number of machines available are ' ||
7340                 x_machine_dtl_tbl.count,
7341                 l_debug);
7342 
7343     if x_machine_dtl_tbl.count > 0 then
7344       -- only if machines are available enter the below loops.
7345       while i < x_machine_dtl_tbl.count and l_completed <> 1 loop
7346         -- i should be incremented manually
7347         if x_machine_dtl_tbl(i).source_subinventory = p_source_subinventory and
7348            x_machine_dtl_tbl(i)
7349         .destination_subinventory = p_destination_subinventory and
7350            x_machine_dtl_tbl(i).pick_uom = p_uom then
7351 
7352           print_debug('The machine is ' || x_machine_dtl_tbl(i)
7353                       .resource_name,
7354                       l_debug);
7355           j := 0;
7356 
7357           while j < x_labor_stats_tbl.count loop
7358             -- j should be incremented manually
7359             if x_machine_dtl_tbl(i).resource_name = x_labor_stats_tbl(j)
7360             .resource_name then
7361 
7362               if x_labor_stats_tbl(j)
7363               .available_capacity > 0 and x_labor_stats_tbl(j)
7364               .available_capacity >= l_planned_load then
7365                 print_debug('This machine resource can take the entire load',
7366                             l_debug);
7367                 print_debug('available capacity for the resource is ' ||
7368                             x_labor_stats_tbl(j).available_capacity,
7369                             l_debug);
7370                 x_labor_stats_tbl(j).planned_wave_load := x_labor_stats_tbl(j)
7371                                                          .planned_wave_load +
7372                                                           l_planned_load;
7373                 x_labor_stats_tbl(j).number_of_planned_tasks := x_labor_stats_tbl(j)
7374                                                                .number_of_planned_tasks + 1;
7375                 x_labor_stats_tbl(j).available_capacity := x_labor_stats_tbl(j)
7376                                                           .available_capacity -
7377                                                            l_planned_load;
7378                 print_debug('available capacity after load assignmentfor the resource is ' ||
7379                             x_labor_stats_tbl(j).available_capacity,
7380                             l_debug);
7381                 print_debug('Planned tasks for the resource is ' ||
7382                             x_labor_stats_tbl(j).number_of_planned_tasks,
7383                             l_debug);
7384 
7385                 l_completed := 1;
7386                 j           := x_labor_stats_tbl.count + 5;
7387                 exit;
7388 
7389               elsif x_labor_stats_tbl(j)
7390               .available_capacity > 0 and x_labor_stats_tbl(j)
7391               .available_capacity < l_planned_load then
7392                 print_debug('This machine resource can take the partial load',
7393                             l_debug);
7394                 i := get_next_resource(i,
7395                                        p_source_subinventory,
7396                                        p_destination_subinventory,
7397                                        p_uom,
7398                                        l_resource_type);
7399 
7400                 if i = -1 then
7401                   print_debug('available capacity for the resource is ' ||
7402                               x_labor_stats_tbl(j).available_capacity,
7403                               l_debug);
7404                   x_labor_stats_tbl(j).planned_wave_load := x_labor_stats_tbl(j)
7405                                                            .planned_wave_load +
7406                                                             l_planned_load;
7407                   x_labor_stats_tbl(j).number_of_planned_tasks := x_labor_stats_tbl(j)
7408                                                                  .number_of_planned_tasks + 1;
7409                   x_labor_stats_tbl(j).available_capacity := x_labor_stats_tbl(j)
7410                                                             .available_capacity -
7411                                                              l_planned_load;
7412                   print_debug('available capacity after load assignmentfor the resource is ' ||
7413                               x_labor_stats_tbl(j).available_capacity,
7414                               l_debug);
7415                   print_debug('Planned tasks for the resource is ' ||
7416                               x_labor_stats_tbl(j).number_of_planned_tasks,
7417                               l_debug);
7418 
7419                   l_completed := 1;
7420                   j           := x_labor_stats_tbl.count + 5;
7421                   i           := x_machine_dtl_tbl.count + 5;
7422                   exit;
7423                 else
7424                   print_debug('available capacity for the resource is ' ||
7425                               x_labor_stats_tbl(j).available_capacity,
7426                               l_debug);
7427                   x_labor_stats_tbl(j).planned_wave_load := x_labor_stats_tbl(j)
7428                                                            .planned_wave_load +
7429                                                             x_labor_stats_tbl(j)
7430                                                            .available_capacity;
7431                   x_labor_stats_tbl(j).number_of_planned_tasks := x_labor_stats_tbl(j)
7432                                                                  .number_of_planned_tasks + 1;
7433                   l_planned_load := l_planned_load - x_labor_stats_tbl(j)
7434                                    .available_capacity;
7435                   x_labor_stats_tbl(j).available_capacity := 0;
7436                   print_debug('available capacity after load assignmentfor the resource is ' ||
7437                               x_labor_stats_tbl(j).available_capacity,
7438                               l_debug);
7439                   print_debug('Planned tasks for the resource is ' ||
7440                               x_labor_stats_tbl(j).number_of_planned_tasks,
7441                               l_debug);
7442 
7443                   j := x_labor_stats_tbl.count + 5;
7444                 end if;
7445               elsif x_labor_stats_tbl(j).available_capacity <= 0 then
7446                 print_debug('This machine resource can take the partial load',
7447                             l_debug);
7448                 i := get_next_resource(i,
7449                                        p_source_subinventory,
7450                                        p_destination_subinventory,
7451                                        p_uom,
7452                                        l_resource_type);
7453 
7454                 if i = -1 then
7455                   print_debug('available capacity for the resource is ' ||
7456                               x_labor_stats_tbl(j).available_capacity,
7457                               l_debug);
7458                   x_labor_stats_tbl(j).planned_wave_load := x_labor_stats_tbl(j)
7459                                                            .planned_wave_load +
7460                                                             l_planned_load;
7461                   x_labor_stats_tbl(j).number_of_planned_tasks := x_labor_stats_tbl(j)
7462                                                                  .number_of_planned_tasks + 1;
7463                   x_labor_stats_tbl(j).available_capacity := x_labor_stats_tbl(j)
7464                                                             .available_capacity -
7465                                                              l_planned_load;
7466                   print_debug('available capacity after load assignmentfor the resource is ' ||
7467                               x_labor_stats_tbl(j).available_capacity,
7468                               l_debug);
7469                   print_debug('Planned tasks for the resource is ' ||
7470                               x_labor_stats_tbl(j).number_of_planned_tasks,
7471                               l_debug);
7472 
7473                   l_completed := 1;
7474                   j           := x_labor_stats_tbl.count + 5;
7475                   i           := x_machine_dtl_tbl.count + 5;
7476                   exit;
7477                 else
7478                   j := x_labor_stats_tbl.count + 5;
7479                 end if;
7480               end if;
7481 
7482             else
7483               j := j + 1;
7484             end if;
7485           end loop;
7486         else
7487           i := i + 1;
7488         end if;
7489       end loop;
7490 
7491     end if;
7492 
7493   exception
7494     when others then
7495       print_debug('error in sync_machine_person_time api ' || ':' ||
7496                   SQLCODE || ':' || SQLERRM,
7497                   l_debug);
7498   end;
7499 */
7500 
7501 
7502   /*
7503   API to Calculate Labor Planning and Resource Allocation.
7504 
7505 
7506   */
7507  procedure labor_planning(p_wave_header_id       in number,
7508                          p_planning_criteria_id in number,
7509                          x_return_status        OUT NOCOPY varchar2) is
7510 
7511   v_replenishment_required   varchar2(1) := 'N';
7512   v_picking_subinventory     varchar2(4000);
7513   v_destination_subinventory varchar2(4000);
7514   L_ORG_ID                   NUMBER;
7515   L_ITEM_ID                  NUMBER;
7516   tbl_index                  number := 0;
7517   l_att                      number;
7518   l_bulk_planning            varchar2(1);
7519   n                          number := 0;
7520   v_department_id            number;
7521   v_time_uom                 VARCHAR2(10);
7522   l_allocation_method        varchar2(20);
7523 
7524 
7525 
7526   cursor c_labor_lines is
7527     select DISTINCT wwl.organization_id,
7528                     wdd.inventory_item_id,
7529                     wwh.pull_replenishment_flag pull_replenishment_flag,
7530                     wwl.wave_line_id,
7531                     (wdd.requested_quantity - wwl.crossdock_quantity) requested_quantity
7532       from WMS_WP_WAVE_HEADERS_B wwh, wms_wp_wave_lines wwl,wsh_delivery_details wdd --11775489 get from wdd... FIXED check performance impact
7533      where wwh.wave_header_id = p_wave_header_id
7534        and wwh.wave_header_id = wwl.wave_header_id
7535        AND Nvl(wwl.remove_from_Wave_flag, 'N') <> 'Y'
7536        AND wdd.inventory_item_id = l_item_id
7537        and wwl.organization_id = l_org_id
7538           and wwl.delivery_detail_id=wdd.delivery_detail_id
7539 		  and wdd.organization_id = wwl.organization_id
7540      and wdd.released_status in ('R','B') --- For Hot Order Changes
7541      ORDER BY inventory_item_id, wave_line_id;
7542 
7543   cursor c_item is
7544     SELECT DISTINCT wdd.inventory_item_id, wwl.organization_id
7545       FROM wms_wp_wave_lines wwl,wsh_delivery_Details wdd --11775489 get from wdd... FIXED
7546      WHERE wave_header_id = p_wave_header_id
7547      and wwl.delivery_detail_id=wdd.delivery_detail_id
7548 	 and wdd.organization_id = wwl.organization_id
7549      and wdd.released_status in ('R','B') --- For Hot Order Changes
7550        AND Nvl(remove_from_Wave_flag, 'N') <> 'Y';
7551 
7552   cursor c_subinventories is
7553     select DISTINCT MSI.SECONDARY_INVENTORY_NAME,
7554                     moqd.SUBINVENTORY_CODE,
7555                     wms_wave_planning_pvt.get_source_subinventory(MSIB.INVENTORY_ITEM_id,
7556                                                                   MSI.SECONDARY_INVENTORY_NAME) source_subinventory,
7557                     MSI.PICK_UOM_CODE,
7558                     wms_wave_planning_pvt.get_conversion_rate(MSIB.INVENTORY_ITEM_id,
7559                                                               MSI.PICK_UOM_CODE,
7560                                                               MSIB.PRIMARY_UOM_CODE) AS CONVERSION_RATE,
7561                     MSI.PICKING_ORDER
7562       from MTL_SECONDARY_INVENTORIES    MSI,
7563            MTL_SYSTEM_ITEMS_B           MSIB,
7564            mtl_onhand_quantities_Detail moqd
7565      WHERE MSIB.organization_id = l_org_id
7566        and MSIB.INVENTORY_ITEM_ID = l_item_id
7567        AND MSI.PICK_UOM_CODE IS NOT NULL
7568        AND (MSI.SECONDARY_INVENTORY_NAME = v_picking_subinventory or
7569            v_picking_subinventory is null)
7570        AND wms_wave_planning_pvt.get_conversion_rate(MSIB.INVENTORY_ITEM_id,
7571                                                      MSI.PICK_UOM_CODE,
7572                                                      MSIB.PRIMARY_UOM_CODE) > 0
7573        AND moqd.INVENTORY_ITEM_id = MSIB.INVENTORY_ITEM_id
7574        AND moqd.ORGANIZATION_ID = MSIB.ORGANIZATION_ID
7575        AND moqd.SUBINVENTORY_CODE = msi.SECONDARY_INVENTORY_NAME
7576        AND moqd.ORGANIZATION_ID = msi.ORGANIZATION_ID
7577      ORDER BY CONVERSION_RATE DESC, MSI.PICKING_ORDER;
7578 
7579   cursor c_labor_time is
7580     SELECT source_subinventory,
7581            destination_subinventory,
7582            pick_uom,
7583            transaction_time,
7584            travel_time,
7585            resource_type
7586       FROM wms_wp_labor_planning
7587      WHERE planning_criteria_id = p_planning_criteria_id
7588      group by source_subinventory,
7589               destination_subinventory,
7590               pick_uom,
7591               transaction_time,
7592               travel_time,
7593               resource_type; -- group by added by
7594 
7595   cursor c_labor_resource is
7596     SELECT distinct resource_type
7597       FROM wms_wp_labor_planning
7598      WHERE planning_criteria_id = p_planning_criteria_id;
7599 
7600   /*  cursor c_labor_resource is
7601   SELECT distinct wwlp.resource_type
7602     FROM wms_wp_labor_planning wwlp,BOM_DEPARTMENT_RESOURCES_V bdr,wms_wp_planning_criteria_vl wwp
7603    WHERE wwlp.planning_criteria_id =  p_planning_criteria_id
7604    AND wwp.planning_criteria_id=wwlp.planning_criteria_id
7605    and wwp.department_id=v_department_id
7606    and wwp.department_id=bdr.department_id
7607    and wwlp.resource_type=bdr.resource_code
7608    AND (bdr.AVAILABLE_24_HOURS_FLAG=2
7609    OR (SELECT Count(1) FROM BOM_DEPT_RES_INSTANCES WHERE resource_id= bdr.resource_id AND department_id=bdr.department_id) > 0);*/
7610 
7611   TYPE ITEM_TBL IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
7612 
7613   x_item_tbl ITEM_TBL;
7614 
7615   L_DEMAND_QTY NUMBER;
7616 
7617   l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
7618 
7619   l_return_status VARCHAR2(3) := fnd_api.g_ret_sts_success;
7620 
7621   v_total_labor_time number := 0;
7622 
7623   travel_time_idx number;
7624   n2              number := 0;
7625   /*  CURSOR c_dept is
7626   SELECT RESOURCE_code,
7627          (nvl(utilization, 100) / 100) utilization,
7628          (nvl(efficiency, 100) / 100) efficiency,
7629          unit_of_measure,
7630          capacity_units
7631     FROM BOM_DEPARTMENT_RESOURCES_V
7632    WHERE department_id = v_department_id; */
7633 
7634   --  declare variables
7635   x_current_load            number := 0;
7636   x_resource_type           number := 0;
7637   x_number_of_tasks         NUMBER := 0;
7638   x_total_capacity          NUMBER := 0;
7639   l_dd_qty                  NUMBER := 0;
7640   l_person_found            NUMBER := 0;
7641   l_avail_capacity          NUMBER := 0;
7642   l_utilization             NUMBER := 0;
7643   l_efficiency              NUMBER := 0;
7644   l_number_of_capable_units NUMBER := 0;
7645   l_accept_load             NUMBER := 0;
7646   j                         NUMBER := 0;
7647   l_completed               NUMBER := 0;
7648   j_temp                    NUMBER := 0;
7649   l_resource_type           NUMBER := 2; -- Never ever change this declaration. Problematic
7650   lab_dtl                   NUMBER := 0;
7651   l_planned_load            NUMBER := 0;
7652   k                         NUMBER := 0;
7653   --  declared variables
7654   l_other_wdd_qty    number := 0;
7655   l_temp_value       number;
7656   l_reserved_qty     number;
7657   l_crossdock_qty    number;
7658   l_current_position NUMBER := 0;
7659   l_tbl_count number;
7660 begin
7661 
7662   print_debug('Labor Planning API entered -------> ', l_debug);
7663 
7664   select wwp.picking_subinventory,
7665          wwp.destination_subinventory,
7666          bulk_labor_planning_flag,
7667          department_id,
7668          time_uom,
7669          allocation_method
7670     into v_picking_subinventory,
7671          v_destination_subinventory,
7672          l_bulk_planning,
7673          v_department_id,
7674          v_time_uom,
7675          l_allocation_method
7676     from wms_wp_planning_criteria_vl wwp
7677    where planning_Criteria_id = p_planning_criteria_id;
7678 
7679   print_debug('The Picking Subinventory specified is ' ||
7680               v_picking_subinventory,
7681               l_debug);
7682   print_debug('The Destination Subinventory is ' ||
7683               v_destination_subinventory,
7684               l_debug);
7685 
7686   -- Getting the distinct resource in the Labor Planning table and populating it into the pl/sql record
7687 
7688   /*  for l_resource in c_labor_resource loop
7689 
7690     x_labor_stats_tbl(n2).resource_name := l_resource.resource_type;
7691     --  addition starts
7692     wms_wave_planning_pvt.get_current_work_load(l_resource.resource_type,
7693                                                 p_planning_criteria_id,
7694                                                 p_wave_header_id,
7695                                                 x_current_load,
7696                                                 x_resource_type,
7697                                                 x_number_of_tasks,
7698                                                 x_total_capacity);
7699     x_labor_stats_tbl(n2).current_workload := x_current_load;
7700     x_labor_stats_tbl(n2).resource_type := x_resource_type;
7701     x_labor_stats_tbl(n2).number_of_tasks := x_number_of_tasks;
7702     x_labor_stats_tbl(n2).total_capacity := x_total_capacity;
7703     x_labor_stats_tbl(n2).available_capacity := x_total_capacity -
7704                                                 x_current_load;
7705 
7706     print_debug('x_labor_stats_tbl(n2).resource_name = ' ||
7707                 x_labor_stats_tbl(n2).resource_name,
7708                 l_debug);
7709     print_debug('x_labor_stats_tbl(n2).current_workload = ' ||
7710                 x_labor_stats_tbl(n2).current_workload,
7711                 l_debug);
7712     print_debug('x_labor_stats_tbl(n2).resource_type = ' ||
7713                 x_labor_stats_tbl(n2).resource_type,
7714                 l_debug);
7715     print_debug('x_labor_stats_tbl(n2).number_of_tasks = ' ||
7716                 x_labor_stats_tbl(n2).number_of_tasks,
7717                 l_debug);
7718     print_debug('x_labor_stats_tbl(n2).total_capacity = ' ||
7719                 x_labor_stats_tbl(n2).total_capacity,
7720                 l_debug);
7721     print_debug('x_labor_stats_tbl(n2).available_capacity = ' ||
7722                 x_labor_stats_tbl(n2).available_capacity,
7723                 l_debug);
7724 
7725     --  addition stops
7726 
7727     n2 := n2 + 1;
7728 
7729   end loop; */
7730 
7731   if l_allocation_method <> 'C' then
7732     for c_item_rec in c_item loop
7733 
7734       l_item_id    := c_item_rec.inventory_item_id;
7735       l_org_id     := c_item_rec.organization_id;
7736       L_DEMAND_QTY := 0;
7737 
7738       print_debug('The Item processed is ' || l_item_id, l_debug);
7739 
7740       l_att := get_att_for_subinventory(null, l_item_id, l_org_id);
7741       print_debug('l_att for item ' || l_item_id ||
7742                   '- In labor Planning API is' || l_att,
7743                   l_debug);
7744       begin
7745 
7746         -- Since Qty Tree would have also subttracted qty for current demand
7747         -- lines under consideration for which there is existing
7748         -- reservation as well, I need to add them back
7749         -- to see real picture of the atr for demand line under consideration
7750 
7751         ------------------
7752         SELECT nvl(sum(reservation_quantity), 0)
7753           INTO l_reserved_qty
7754           FROM mtl_Reservations
7755          WHERE demand_source_line_id in
7756                (select wdd.source_line_id
7757                   from wms_wp_wave_lines wwl, wsh_delivery_details wdd--11775489 get from wdd... FIXED
7758                  where wwl.wave_header_id = p_wave_header_id
7759 				   and wwl.delivery_detail_id  = wdd.delivery_detail_id
7760 				   and wdd.organization_id = wwl.organization_id
7761                    and nvl(wwl.remove_from_wave_flag, 'N') <> 'Y')
7762            and organization_id = L_ORG_ID
7763            and inventory_item_id = L_ITEM_ID
7764            and (subinventory_code = v_picking_subinventory or
7765                v_picking_subinventory is null);
7766 
7767         SELECT Nvl(SUM(wdd.requested_quantity), 0)
7768           INTO l_other_wdd_qty
7769           FROM wsh_delivery_details wdd
7770          WHERE wdd.organization_id = l_org_id
7771            AND wdd.inventory_item_id = l_item_id
7772 		   and wdd.released_status in ('B', 'E', 'F', 'K', 'R','S') --11822962 only consider wdds where requested material is against a possible reservation
7773            and wdd.delivery_Detail_id not in
7774                (select delivery_detail_id
7775                   from wms_wp_wave_lines
7776                  where wave_header_id = p_wave_header_id
7777                    and nvl(remove_from_wave_flag, 'N') <> 'Y');
7778 
7779         print_debug('Reserved Qty for other dds that do not belong to the wave   :' ||
7780                     ' is ' || l_other_wdd_qty,
7781                     l_debug);
7782 
7783         IF (l_reserved_qty - l_other_wdd_qty) >= 0 THEN
7784           l_temp_value := (l_reserved_qty - l_other_wdd_qty);
7785         ELSE
7786           l_temp_value := 0;
7787         END IF;
7788 
7789         l_reserved_qty := l_temp_value;
7790 
7791         l_att := l_att + l_reserved_qty;
7792 
7793         print_debug('Reserved Qty is ' || l_reserved_qty, l_debug);
7794 
7795       exception
7796 
7797         when no_data_found then
7798           l_reserved_qty := 0;
7799       end;
7800 
7801       if nvl(l_bulk_planning, 'N') = 'Y' then
7802 
7803         print_debug('Bulk Labor Planning Enabled ', l_debug);
7804         select sum(wdd.requested_quantity), sum(wwl.crossdock_quantity)
7805           into l_demand_qty, l_crossdock_qty
7806           from wms_wp_wave_lines wwl,wsh_delivery_details wdd --11775489 get from wdd... FIXED
7807          where wwl.wave_header_id = p_wave_header_id
7808            and nvl(remove_from_wave_flag, 'N') <> 'Y'
7809               and wwl.delivery_detail_id=wdd.delivery_detail_id
7810 			  and wdd.organization_id = wwl.organization_id
7811      and wdd.released_status in ('R','B') --- For Hot Order Changes
7812            and wdd.inventory_item_id = l_item_id;
7813 
7814         print_debug('The  Crossdock quantity in Labor Planning API is ' ||
7815                     l_crossdock_qty,
7816                     l_debug);
7817 
7818         if l_allocation_method in ('N', 'X') then
7819 
7820           l_demand_qty := l_demand_qty - l_crossdock_qty;
7821 
7822         end if;
7823 
7824         select pull_replenishment_flag
7825           into v_replenishment_required
7826           from wms_wp_wave_headers_vl
7827          where wave_header_id = p_wave_header_id;
7828 
7829         if l_att >= L_DEMAND_QTY then
7830 
7831           print_debug('ATT greater than demand qty - In labor Planning API',
7832                       l_debug);
7833 
7834         else
7835 
7836           print_debug('ATT less than demand qty - In labor Planning API',
7837                       l_debug);
7838 
7839           l_demand_qty := l_att;
7840 
7841           print_debug('The new demand quantity is ' || l_demand_qty,
7842                       l_debug);
7843 
7844         end if;
7845         print_debug('The  demand quantity in Labor Planning API is ' ||
7846                     l_demand_qty,
7847                     l_debug);
7848         -- Store all the values in cursor c_subinventories to the labor pl/sql record
7849 
7850         for l_rec in c_subinventories loop
7851 
7852           x_labor_plan_tbl(n).picking_subinventory := l_rec.SECONDARY_INVENTORY_NAME;
7853           x_labor_plan_tbl(n).source_subinventory := l_rec.source_subINVENTORY;
7854           x_labor_plan_tbl(n).picking_uom := l_rec.PICK_UOM_CODE;
7855           x_labor_plan_tbl(n).conversion_rate := l_rec.CONVERSION_RATE;
7856           x_labor_plan_tbl(n).att := get_att_for_subinventory(p_sub     => l_rec.SECONDARY_INVENTORY_NAME,
7857                                                               p_item_id => l_item_id,
7858                                                               p_org_id  => l_org_id);
7859 
7860           print_debug('The Picking Subinventory is ' ||
7861                       x_labor_plan_tbl(n).picking_subinventory,
7862                       l_debug);
7863           print_debug('The Source Subinventory is ' || x_labor_plan_tbl(n)
7864                       .source_subinventory,
7865                       l_debug);
7866           print_debug('The Picking UOM  is ' || x_labor_plan_tbl(n)
7867                       .picking_uom,
7868                       l_debug);
7869           print_debug('The att for Subinventory  ' || x_labor_plan_tbl(n)
7870                       .picking_subinventory || ' is ' ||
7871                       x_labor_plan_tbl(n).att,
7872                       l_debug);
7873           n := n + 1;
7874 
7875         end loop;
7876         -- Represent the total demand quantity as integral multiple of the qty conversion to primary UOM.
7877         -- Update the labor_record pl/sql table for normal and Replenishment Cases
7878         -- v_destination_subinventory := 'STA';
7879         update_bulk_labor_record(x_labor_plan_tbl           => x_labor_plan_tbl,
7880                                  v_replenishment_required   => v_replenishment_required,
7881                                  v_destination_subinventory => v_destination_subinventory,
7882                                  p_demand_qty               => l_demand_qty,
7883                                  x_return_status            => l_return_status);
7884 
7885         x_labor_plan_tbl.delete;
7886         --Reinitializing n to 0 again;
7887         n := 0;
7888 
7889       else
7890         -- Get the Subinventory information where item is available
7891         print_debug('Bulk Labor Planning Not Enabled ', l_debug);
7892         print_debug('Doing Labor planning for All Wdds one by one ',
7893                     l_debug);
7894 
7895         tbl_index := 0;
7896         for l_rec in c_subinventories loop
7897 
7898           x_labor_plan_tbl(tbl_index).picking_subinventory := l_rec.SECONDARY_INVENTORY_NAME;
7899           x_labor_plan_tbl(tbl_index).source_subinventory := l_rec.source_subINVENTORY;
7900           x_labor_plan_tbl(tbl_index).picking_uom := l_rec.PICK_UOM_CODE;
7901           x_labor_plan_tbl(tbl_index).conversion_rate := l_rec.CONVERSION_RATE;
7902           x_labor_plan_tbl(tbl_index).att := get_att_for_subinventory(p_sub     => l_rec.SECONDARY_INVENTORY_NAME,
7903                                                                       p_item_id => l_item_id,
7904                                                                       p_org_id  => l_org_id);
7905 
7906           -- Ideal Pick Scenario Record
7907           ideal_labor_plan_tbl(tbl_index).picking_subinventory := l_rec.SECONDARY_INVENTORY_NAME;
7908           ideal_labor_plan_tbl(tbl_index).source_subinventory := l_rec.source_subINVENTORY;
7909           ideal_labor_plan_tbl(tbl_index).picking_uom := l_rec.PICK_UOM_CODE;
7910           ideal_labor_plan_tbl(tbl_index).conversion_rate := l_rec.CONVERSION_RATE;
7911 
7912           print_debug('The Picking Subinventory is ' ||
7913                       x_labor_plan_tbl(tbl_index).picking_subinventory,
7914                       l_debug);
7915           print_debug('The Source Subinventory is ' ||
7916                       x_labor_plan_tbl(tbl_index).source_subinventory,
7917                       l_debug);
7918           print_debug('The Picking UOM  is ' ||
7919                       x_labor_plan_tbl(tbl_index).picking_uom,
7920                       l_debug);
7921           print_debug('The att for Subinventory  ' ||
7922                       x_labor_plan_tbl(tbl_index)
7923                       .picking_subinventory || ' is ' ||
7924                       x_labor_plan_tbl(tbl_index).att,
7925                       l_debug);
7926           tbl_index := tbl_index + 1;
7927 
7928         end loop;
7929         FOR c_rec IN c_labor_lines LOOP
7930 
7931           print_debug('The Requested Quantity for wave line id ' ||
7932                       c_rec.wave_line_id || 'is ' ||
7933                       c_rec.requested_quantity,
7934                       l_debug);
7935           v_replenishment_required := c_Rec.pull_replenishment_flag;
7936 
7937           L_DEMAND_QTY := L_DEMAND_QTY + c_rec.requested_quantity;
7938 
7939           if L_DEMAND_QTY > l_att then
7940 
7941             print_debug('This line cannot be processed as the total demand qty exceeds the org level ATT',
7942                         l_debug);
7943             exit;
7944 
7945           else
7946 
7947             print_debug('Process wave line id and represent it as integral multiple of the conversion rates of subinventories where item is available',
7948                         l_debug);
7949 
7950             --  Get the ideal pick scenario based on the requested qty and representing it as integral multiple.
7951 
7952             get_ideal_pick_scenario(p_requested_qty => c_rec.requested_quantity,
7953                                     x_return_status => l_return_status);
7954 
7955           end if;
7956 
7957         END LOOP; -- End loop for all items in that loop;
7958 
7959         --We have two tables one with ideal record and one has the subinventory list with ATT
7960 
7961         -- Need to synchronize ideal pl/sql table based on ATT.
7962 
7963         synchronize_labor_plan_tables(x_return_status            => l_return_status,
7964                                       p_replenishment_required   => v_replenishment_required,
7965                                       v_Destination_subinventory => v_Destination_subinventory);
7966 
7967         -- Here we need to enter the Picking to Staging Subinventory and Picking UOM  in the Time Tbl for that item.
7968 
7969         ideal_labor_plan_tbl.delete;
7970         x_labor_plan_tbl.delete;
7971 
7972       end if; -- End if for Bulk Labor Planning
7973 
7974       print_debug('Current Position is ' || l_current_position, l_debug);
7975 
7976       if x_labor_time_tbl.count > 0 then
7977 
7978         for m4 in l_current_position .. x_labor_time_tbl.LAST loop
7979 
7980           x_labor_time_tbl(m4).inventory_item_id := l_item_id;
7981           x_labor_time_tbl(m4).standard_operation_id := -1;
7982           x_labor_time_tbl(m4).operation_plan_id := -1;
7983 
7984         end loop;
7985 
7986       end if;
7987 
7988       l_current_position := x_labor_time_tbl.count - 1;
7989 
7990     end loop; -- End loop for Item
7991 
7992     labor_resource_planning(x_labor_time_tbl,
7993                             1,
7994                             'A',
7995                             p_planning_criteria_id,
7996                             p_wave_header_id,
7997                             l_org_id);
7998 
7999   end if;
8000 
8001   if l_allocation_method in ('C', 'X', 'N') then
8002 
8003     print_debug('Getting the crossdock movements --- >  ', l_debug);
8004 
8005     if nvl(l_bulk_planning, 'N') = 'Y' then
8006 
8007       print_debug('Bulk Labor Planning Enabled ', l_debug);
8008       SELECT 'Not Applicable',
8009              v_destination_subinventory,
8010              Min(wdd.requested_quantity_uom), --11775489 select from wdd
8011              Sum(crossdock_quantity),
8012              -1,
8013              wdd.inventory_item_id,
8014              -1 bulk collect
8015         into c_labor_time_tbl
8016         FROM wms_wp_wave_lines wwl,wsh_delivery_details wdd --11775489 get from wdd...
8017        WHERE wave_header_id = p_wave_header_id
8018           and wwl.delivery_detail_id=wdd.delivery_detail_id
8019 		  and wdd.organization_id = wwl.organization_id
8020      and wdd.released_status in ('R','B') --- For Hot Order Changes
8021        GROUP BY wdd.inventory_item_id
8022        ORDER BY wdd.inventory_item_id;
8023 
8024     else
8025       SELECT 'Not Applicable',
8026              v_destination_subinventory,
8027              wwl.requested_quantity_uom, --11775489 select from wdd
8028              crossdock_quantity,
8029              -1,
8030              wdd.inventory_item_id,
8031              -1 bulk collect
8032         into c_labor_time_tbl
8033         FROM wms_wp_wave_lines wwl,wsh_delivery_details wdd --11775489 get from wdd...
8034        WHERE wave_header_id = p_wave_header_id
8035           and wwl.delivery_detail_id=wdd.delivery_detail_id
8036 		  and wdd.organization_id = wwl.organization_id
8037      and wdd.released_status in ('R','B') --- For Hot Order Changes
8038        ORDER BY inventory_item_id;
8039 
8040     end if;
8041 
8042     if l_allocation_method = 'C' then
8043 
8044     	 SELECT organization_id INTO l_org_id FROM wms_Wp_Wave_headers_vl WHERE wave_header_id=p_wave_header_id;
8045 
8046       labor_resource_planning(c_labor_time_tbl,
8047                               1,
8048                               'A',
8049                               p_planning_criteria_id,
8050                               p_wave_header_id,
8051                               l_org_id);
8052 
8053     elsif l_allocation_method in ('N', 'X') then
8054 
8055       labor_resource_planning(c_labor_time_tbl,
8056                               2,
8057                               'A',
8058                               p_planning_criteria_id,
8059                               p_wave_header_id,
8060                               l_org_id);
8061 
8062     end if;
8063 
8064   end if;
8065 
8066   --  end if;
8067 
8068   print_debug('Labor Movement Required from following  pick subinv to dest subinv ',
8069               l_debug);
8070 
8071   print_debug('Time Uom is  ' || v_time_uom, l_debug);
8072 
8073 
8074   DELETE FROM wms_wp_labor_Statistics
8075    WHERE wave_header_id = p_wave_header_id;
8076 
8077 l_tbl_count := x_resource_capacity_tbl.count;
8078 
8079   if x_resource_capacity_tbl.count > 0 then
8080      --forall i in x_resource_capacity_tbl.first .. x_resource_capacity_tbl.last
8081      for i in x_resource_capacity_tbl.first .. x_resource_capacity_tbl.last LOOP --dbchange14
8082 
8083       insert into wms_wp_labor_statistics
8084         (wave_header_id,
8085          resource_name,
8086          planned_wave_load,
8087          total_capacity,
8088          actual_workload,
8089          available_capacity,
8090          NUMBER_OF_ACTUAL_TASKS,
8091          NUMBER_OF_PLANNED_TASKS)
8092       values
8093         (p_wave_header_id,
8094          x_resource_capacity_tbl(i).resource_name,
8095          x_resource_capacity_tbl(i).planned_load,
8096          x_resource_capacity_tbl(i).total_Capacity,
8097          x_resource_capacity_tbl(i).current_load,
8098          x_resource_capacity_tbl(i).available_capacity,
8099          x_resource_capacity_tbl(i).actual_tasks,
8100          x_resource_capacity_tbl(i).planned_tasks);
8101        end loop;
8102   end if;
8103 
8104   -- x_labor_stats_tbl.delete;
8105   x_labor_time_tbl.delete;
8106   c_labor_time_tbl.delete;
8107   x_resource_capacity_tbl.delete;
8108   -- x_labor_dtl_tbl.delete;
8109   -- x_machine_dtl_tbl.delete;
8110   --  x_labor_stats_tbl_tmp.delete;
8111 
8112   commit;
8113   x_return_status := 'S';
8114 exception
8115   when others then
8116     print_debug('Error in Labor Planning API: ' || SQLCODE || ' : ' ||
8117                 SQLERRM,
8118                 l_debug);
8119     x_return_status := 'E';
8120 end labor_planning;
8121 
8122   procedure get_ideal_pick_scenario(p_requested_qty in number,
8123                                     x_return_status OUT NOCOPY varchar2)
8124 
8125    is
8126 
8127     l_requested_qty number := p_requested_qty;
8128     l_debug         NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
8129     divider         number;
8130   begin
8131 
8132     print_debug('Entered ideal pick scenario: '||ideal_labor_plan_tbl.count, l_debug);
8133 
8134 
8135 if ideal_labor_plan_tbl.count >0 then
8136 
8137     FOR i IN ideal_labor_plan_tbl.FIRST .. ideal_labor_plan_tbl.LAST LOOP
8138 
8139 
8140     print_debug('Inside for loop ', l_debug);
8141 
8142       divider := Floor(l_requested_qty / ideal_labor_plan_tbl(i)
8143                        .conversion_rate);
8144 
8145     print_debug('After divider ', l_debug);
8146 
8147 
8148       ideal_labor_plan_tbl(i).demand_qty_picking_uom := ideal_labor_plan_tbl(i)
8149                                                        .demand_qty_picking_uom +
8150                                                         divider;
8151 
8152       print_debug('Picking Subinventory is  ' || ideal_labor_plan_tbl(i)
8153                   .picking_subinventory,
8154                   l_debug);
8155 
8156       print_debug('Conversion rate is ' || ideal_labor_plan_tbl(i)
8157                   .conversion_rate,
8158                   l_debug);
8159 
8160       print_debug('Demand qty in Picking UOM is ' ||
8161                   ideal_labor_plan_tbl(i).demand_qty_picking_uom,
8162                   l_debug);
8163 
8164       print_debug(' Picking UOM is ' || ideal_labor_plan_tbl(i)
8165                   .picking_uom,
8166                   l_debug);
8167 
8168       l_requested_qty := l_requested_qty -
8169                          divider * ideal_labor_plan_tbl(i).conversion_rate;
8170 
8171     END LOOP;
8172 
8173   end if;
8174 
8175   exception
8176     when others then
8177       print_debug('Error in get ideal pick scenario: ' || SQLCODE || ' : ' ||
8178                   SQLERRM,
8179                   l_debug);
8180 
8181   end get_ideal_pick_Scenario;
8182 
8183   procedure synchronize_labor_plan_tables(x_return_status            OUT NOCOPY varchar2,
8184                                           p_replenishment_Required   in varchar2,
8185                                           v_Destination_subinventory in varchar2) is
8186 
8187     m             number;
8188     l             number;
8189     sum_qty       NUMBER := 0;
8190     sum_att       NUMBER := 0;
8191     repl_quantity NUMBER := 0;
8192     qty_moved     NUMBER := 0;
8193     jump_Count    NUMBER := 0;
8194     m2            NUMBER;
8195     m1            NUMBER;
8196 
8197     l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
8198   begin
8199 
8200     print_debug('Entered synchronize labor plan tables', l_debug);
8201 
8202     m := ideal_labor_plan_tbl.count - 1;
8203     print_debug('m is ' || m, l_debug);
8204     l := x_labor_plan_tbl.count - 1;
8205 
8206     print_debug('x labor count  is ' || l, l_debug);
8207 
8208     WHILE (m >= 0) loop
8209 
8210       -- First find out the sum of the conversion rate and subinventory
8211 
8212       print_debug('Current conversion rate is ' || ideal_labor_plan_tbl(m)
8213                   .conversion_rate,
8214                   l_debug);
8215 
8216       -- Find the sum of qty in this conversion rate  and sum of att as well
8217 
8218       m1        := m - 1;
8219       sum_qty   := ideal_labor_plan_tbl(m)
8220                   .demand_qty_picking_uom * ideal_labor_plan_tbl(m)
8221                   .conversion_rate;
8222       sum_att   := x_labor_plan_tbl(m).att;
8223       qty_moved := 0;
8224 
8225       WHILE (m1 > 0) loop
8226 
8227         EXIT WHEN ideal_labor_plan_tbl(m1) .conversion_rate > ideal_labor_plan_tbl(m) .conversion_rate;
8228 
8229         sum_qty := sum_qty + ideal_labor_plan_tbl(m1)
8230                   .demand_qty_picking_uom * ideal_labor_plan_tbl(m1)
8231                   .conversion_rate;
8232         sum_att := sum_att + x_labor_plan_tbl(m1).att;
8233 
8234         m1 := m1 - 1;
8235       END LOOP;
8236 
8237       -- Dbms_Output.put_line('m1 is '||m1+1);
8238 
8239       print_debug('Sum qty is ' || sum_qty, l_debug);
8240 
8241       print_debug('Sum att is ' || sum_att, l_debug);
8242 
8243       IF sum_att >= sum_qty then
8244 
8245         -- We need to move for subinventories  of conversion rate 1
8246 
8247         jump_Count := 0;
8248         WHILE (m1 < m) LOOP
8249           IF (ideal_labor_plan_tbl(m1 + 1)
8250              .demand_qty_picking_uom * ideal_labor_plan_tbl(m1 + 1)
8251              .conversion_rate) <= x_labor_plan_tbl(m1 + 1)
8252           .att AND ideal_labor_plan_tbl(m1 + 1)
8253           .demand_qty_picking_uom <> 0 THEN
8254 
8255             qty_moved := qty_moved +
8256                          (ideal_labor_plan_tbl(m1 + 1)
8257                          .demand_qty_picking_uom *
8258                           ideal_labor_plan_tbl(m1 + 1).conversion_rate);
8259 
8260             x_labor_time_tbl(n1).demand_qty_picking_uom := ideal_labor_plan_tbl(m1 + 1)
8261                                                           .demand_qty_picking_uom;
8262             x_labor_time_tbl(n1).picking_uom := ideal_labor_plan_tbl(m1 + 1)
8263                                                .picking_uom;
8264             x_labor_time_tbl(n1).picking_subinventory := ideal_labor_plan_tbl(m1 + 1)
8265                                                         .picking_subinventory;
8266             x_labor_time_tbl(n1).destination_subinventory := v_destination_subinventory;
8267 
8268             print_debug('Entered if part where demand qty in picking uom > 0 and  it is less than att',
8269                         l_debug);
8270             print_debug('n1 is ' || n1, l_debug);
8271             n1 := n1 + 1;
8272 
8273           ELSIF ideal_labor_plan_tbl(m1 + 1)
8274           .demand_qty_picking_uom <> 0 AND
8275                 (ideal_labor_plan_tbl(m1 + 1)
8276                  .demand_qty_picking_uom * ideal_labor_plan_tbl(m1 + 1)
8277                  .conversion_rate) > x_labor_plan_tbl(m1 + 1).att then
8278 
8279             x_labor_time_tbl(n1).demand_qty_picking_uom := floor(x_labor_plan_tbl(m1 + 1)
8280                                                                  .att /
8281                                                                  ideal_labor_plan_tbl(m1 + 1)
8282                                                                  .conversion_rate);
8283             x_labor_time_tbl(n1).picking_uom := ideal_labor_plan_tbl(m1 + 1)
8284                                                .picking_uom;
8285             x_labor_time_tbl(n1).picking_subinventory := ideal_labor_plan_tbl(m1 + 1)
8286                                                         .picking_subinventory;
8287             x_labor_time_tbl(n1).destination_subinventory := v_destination_subinventory;
8288             print_debug('Entered if part where demand qty in picking uom <> 0 and  it is greater than att',
8289                         l_debug);
8290             print_debug('n1 is ' || n1, l_debug);
8291             n1 := n1 + 1;
8292 
8293             qty_moved := qty_moved + x_labor_plan_tbl(m1 + 1).att;
8294 
8295           ELSE
8296             print_debug('Entered if part where demand qty in picking uom  =  0',
8297                         l_debug);
8298             print_debug('sum_qty is ' || sum_qty, l_debug);
8299             print_debug('qty_moved is ' || qty_moved, l_debug);
8300 
8301             IF x_labor_plan_tbl(m1 + 1)
8302             .att >= (sum_qty - qty_moved) AND (sum_qty - qty_moved) > 0 THEN
8303 
8304               x_labor_time_tbl(n1).demand_qty_picking_uom := floor((sum_qty -
8305                                                                    qty_moved) /
8306                                                                    ideal_labor_plan_tbl(m1 + 1)
8307                                                                    .conversion_rate);
8308               print_debug('Qty to be moved in demand qty picking uom is  ' ||
8309                           x_labor_time_tbl(n1).demand_qty_picking_uom,
8310                           l_debug);
8311 
8312               x_labor_time_tbl(n1).picking_uom := ideal_labor_plan_tbl(m1 + 1)
8313                                                  .picking_uom;
8314               x_labor_time_tbl(n1).picking_subinventory := ideal_labor_plan_tbl(m1 + 1)
8315                                                           .picking_subinventory;
8316               print_debug('Picking Sub inventory is  ' ||
8317                           x_labor_time_tbl(n1).picking_subinventory,
8318                           l_debug);
8319               print_debug('Picking uom is  ' || x_labor_time_tbl(n1)
8320                           .picking_uom,
8321                           l_debug);
8322               x_labor_time_tbl(n1).destination_subinventory := v_destination_subinventory;
8323               print_debug('n1 is ' || n1, l_debug);
8324               print_debug('Entered if part where demand qty in picking uom  =  0 and  it is less than att',
8325                           l_debug);
8326               n1 := n1 + 1;
8327 
8328               qty_moved := qty_moved + (sum_qty - qty_moved);
8329 
8330             ELSIF x_labor_plan_tbl(m1 + 1).att < (sum_qty - qty_moved) then
8331 
8332               qty_moved := qty_moved + x_labor_plan_tbl(m1 + 1).att;
8333 
8334               x_labor_time_tbl(n1).demand_qty_picking_uom := floor(x_labor_plan_tbl(m1 + 1)
8335                                                                    .att /
8336                                                                    ideal_labor_plan_tbl(m1 + 1)
8337                                                                    .conversion_rate);
8338               x_labor_time_tbl(n1).picking_uom := ideal_labor_plan_tbl(m1 + 1)
8339                                                  .picking_uom;
8340               x_labor_time_tbl(n1).picking_subinventory := ideal_labor_plan_tbl(m1 + 1)
8341                                                           .picking_subinventory;
8342               x_labor_time_tbl(n1).destination_subinventory := v_destination_subinventory;
8343               print_debug('n1 in else2 is ' || n1, l_debug);
8344               n1 := n1 + 1;
8345 
8346             END IF;
8347           END IF;
8348 
8349           jump_Count := jump_Count + 1;
8350           m1         := m1 + 1;
8351 
8352         END LOOP;
8353 
8354         print_debug('jump_Count is ' || jump_Count, l_debug);
8355 
8356       ELSE
8357 
8358         --Quantity is less
8359 
8360         -- Need to check if replenishment s required
8361 
8362         IF p_replenishment_Required = 'Y' THEN
8363 
8364           print_debug('Replenishment Required ', l_debug);
8365           m2 := m1 + 1;
8366           -- Here we need to check how much needs to be moved
8367 
8368           repl_quantity := ceil((sum_qty - sum_att) /
8369                                 x_labor_plan_tbl(m2 - 1).conversion_rate);
8370           print_debug('Replenishment qty is ' || repl_quantity, l_debug);
8371           -- Need to check if the source subinventory is m1+1 and att is available
8372 
8373           IF x_labor_plan_tbl(m2 - 1)
8374           .att >= repl_quantity and x_labor_plan_tbl(m2 - 1)
8375           .picking_subinventory = x_labor_plan_tbl(m2)
8376           .source_subinventory then
8377 
8378             x_labor_plan_tbl(m2 - 1).att := x_labor_plan_tbl(m2 - 1)
8379                                            .att - (repl_quantity *
8380                                                    x_labor_plan_tbl(m2 - 1)
8381                                                    .conversion_rate);
8382             x_labor_plan_tbl(m2).att := x_labor_plan_tbl(m2)
8383                                        .att + (repl_quantity *
8384                                                x_labor_plan_tbl(m2 - 1)
8385                                                .conversion_rate);
8386 
8387             print_Debug('New ATT for  ' || x_labor_plan_tbl(m2 - 1)
8388                         .picking_subinventory || ' is ' ||
8389                         x_labor_plan_tbl(m2 - 1).att,
8390                         l_debug);
8391             print_Debug('New ATT for ' || x_labor_plan_tbl(m2)
8392                         .picking_subinventory || ' is ' ||
8393                         x_labor_plan_tbl(m2).att,
8394                         l_debug);
8395 
8396             x_labor_time_tbl(n1).demand_qty_picking_uom := repl_quantity;
8397             x_labor_time_tbl(n1).picking_uom := ideal_labor_plan_tbl(m2 - 1)
8398                                                .picking_uom;
8399             x_labor_time_tbl(n1).picking_subinventory := x_labor_plan_tbl(m2)
8400                                                         .source_subinventory;
8401             x_labor_time_tbl(n1).destination_subinventory := ideal_labor_plan_tbl(m2)
8402                                                             .picking_subinventory;
8403 
8404             n1 := n1 + 1;
8405 
8406             jump_Count := 0;
8407             WHILE (m1 < m) loop
8408               IF x_labor_plan_tbl(m1 + 1).att <= (ideal_labor_plan_tbl(m1 + 1)
8409                          .demand_qty_picking_uom *
8410                          ideal_labor_plan_tbl(m1 + 1)
8411                          .conversion_rate) THEN
8412 
8413                 qty_moved := qty_moved + x_labor_plan_tbl(m1 + 1).att;
8414 
8415                 x_labor_time_tbl(n1).demand_qty_picking_uom := (x_labor_plan_tbl(m1 + 1)
8416                                                                .att /
8417                                                                 x_labor_plan_tbl(m1 + 1)
8418                                                                .conversion_rate);
8419                 x_labor_time_tbl(n1).picking_uom := ideal_labor_plan_tbl(m1 + 1)
8420                                                    .picking_uom;
8421                 x_labor_time_tbl(n1).picking_subinventory := ideal_labor_plan_tbl(m1 + 1)
8422                                                             .picking_subinventory;
8423                 x_labor_time_tbl(n1).destination_subinventory := v_destination_subinventory;
8424 
8425                 print_debug('n1 is ' || n1, l_debug);
8426                 n1 := n1 + 1;
8427               ELSE
8428 
8429                 IF qty_moved < sum_qty THEN
8430                   IF (sum_qty - qty_moved) <= x_labor_plan_tbl(m1 + 1).att then
8431 
8432                     x_labor_time_tbl(n1).demand_qty_picking_uom := To_Number((sum_qty -
8433                                                                              qty_moved) /
8434                                                                              x_labor_plan_tbl(m1 + 1)
8435                                                                              .conversion_rate);
8436                     x_labor_time_tbl(n1).picking_uom := ideal_labor_plan_tbl(m1 + 1)
8437                                                        .picking_uom;
8438                     x_labor_time_tbl(n1).picking_subinventory := ideal_labor_plan_tbl(m1 + 1)
8439                                                                 .picking_subinventory;
8440                     x_labor_time_tbl(n1).destination_subinventory := v_destination_subinventory;
8441 
8442                     qty_moved := qty_moved + To_Number(sum_qty - qty_moved);
8443 
8444                     print_debug('n1 is ' || n1, l_debug);
8445                     n1 := n1 + 1;
8446 
8447                   ELSE
8448                     x_labor_time_tbl(n1).demand_qty_picking_uom := x_labor_plan_tbl(m1 + 1)
8449                                                                   .att /
8450                                                                    x_labor_plan_tbl(m1 + 1)
8451                                                                   .conversion_rate;
8452                     x_labor_time_tbl(n1).picking_uom := ideal_labor_plan_tbl(m1 + 1)
8453                                                        .picking_uom;
8454                     x_labor_time_tbl(n1).picking_subinventory := ideal_labor_plan_tbl(m1 + 1)
8455                                                                 .picking_subinventory;
8456                     x_labor_time_tbl(n1).destination_subinventory := v_destination_subinventory;
8457 
8458                     print_debug('n1 is ' || n1, l_debug);
8459                     n1 := n1 + 1;
8460 
8461                     qty_moved := qty_moved + x_labor_plan_tbl(m1 + 1).att;
8462 
8463                   END IF;
8464 
8465                 END IF;
8466               END IF;
8467 
8468               jump_Count := jump_Count + 1;
8469               m1         := m1 + 1;
8470 
8471             END LOOP;
8472 
8473           else
8474 
8475             -- Source subinventory is not the defined or not correct or ATT is not available in Source Subinventory
8476 
8477             print_debug('Source subinventory is not defined or ATT is not available in Source subinventory',
8478                         l_debug);
8479 
8480             jump_Count := 0;
8481             WHILE (m1 < m) loop
8482               jump_Count := jump_Count + 1;
8483               m1         := m1 + 1;
8484 
8485               x_labor_time_tbl(n1).demand_qty_picking_uom := floor(x_labor_plan_tbl(m1)
8486                                                                    .att /
8487                                                                    x_labor_plan_tbl(m1)
8488                                                                    .conversion_rate);
8489               x_labor_time_tbl(n1).picking_uom := ideal_labor_plan_tbl(m1)
8490                                                  .picking_uom;
8491               x_labor_time_tbl(n1).picking_subinventory := ideal_labor_plan_tbl(m1)
8492                                                           .picking_subinventory;
8493               x_labor_time_tbl(n1).destination_subinventory := v_destination_subinventory;
8494 
8495               print_debug('n1 is ' || n1, l_debug);
8496               n1 := n1 + 1;
8497 
8498             END LOOP;
8499 
8500           END IF;
8501 
8502         ELSE
8503 
8504           -- Replenishment not required
8505           -- If replenishment is not required we just move whatever is available
8506 
8507           print_debug('Qty not available ', l_debug);
8508           jump_Count := 0;
8509           WHILE (m1 < m) loop
8510             jump_Count := jump_Count + 1;
8511             m1         := m1 + 1;
8512 
8513             x_labor_time_tbl(n1).demand_qty_picking_uom := floor(x_labor_plan_tbl(m1)
8514                                                                  .att /
8515                                                                  x_labor_plan_tbl(m1)
8516                                                                  .conversion_rate);
8517             x_labor_time_tbl(n1).picking_uom := ideal_labor_plan_tbl(m1)
8518                                                .picking_uom;
8519             x_labor_time_tbl(n1).picking_subinventory := ideal_labor_plan_tbl(m1)
8520                                                         .picking_subinventory;
8521             x_labor_time_tbl(n1).destination_subinventory := v_destination_subinventory;
8522 
8523             print_debug('n1 is ' || n1, l_debug);
8524             n1 := n1 + 1;
8525 
8526           END LOOP;
8527 
8528         END IF;
8529 
8530       END IF;
8531       m := m - jump_Count;
8532     END LOOP;
8533 
8534   exception
8535     when others then
8536       print_debug('Error in get synchronize labor plan tables: ' ||
8537                   SQLCODE || ' : ' || SQLERRM,
8538                   l_debug);
8539 
8540   end synchronize_labor_plan_tables;
8541 
8542   FUNCTION get_conversion_rate(p_item_id       IN NUMBER,
8543                                p_from_uom_code IN VARCHAR2,
8544                                p_to_uom_code   IN VARCHAR2) RETURN NUMBER IS
8545     l_conversion_rate NUMBER;
8546     l_debug           NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
8547 
8548   BEGIN
8549     IF (p_from_uom_code = p_to_uom_code) THEN
8550       -- No conversion necessary
8551       l_conversion_rate := 1;
8552     else
8553 
8554       inv_convert.inv_um_conversion(from_unit => p_from_uom_code,
8555                                     to_unit   => p_to_uom_code,
8556                                     item_id   => p_item_id,
8557                                     uom_rate  => l_conversion_rate);
8558 
8559     end if;
8560     RETURN l_conversion_rate;
8561 
8562   EXCEPTION
8563     WHEN OTHERS THEN
8564       IF l_debug = 1 THEN
8565         print_debug('Exception in get_conversion_rate: ' || sqlcode || ', ' ||
8566                     sqlerrm,
8567                     l_debug);
8568       END IF;
8569       -- If an exception occurs, return a negative value.
8570       -- The calling program should interpret this as an exception in retrieving
8571       -- the UOM conversion rate.
8572       RETURN - 999;
8573   END get_conversion_rate;
8574 
8575   FUNCTION get_att_for_subinventory(p_sub     IN VARCHAR2,
8576                                     p_item_id IN NUMBER,
8577                                     p_org_id  IN NUMBER) RETURN NUMBER IS
8578 
8579     l_is_revision_ctrl BOOLEAN := FALSE;
8580     l_is_lot_ctrl      BOOLEAN := FALSE;
8581     l_is_serial_ctrl   BOOLEAN := FALSE;
8582     l_qoh              NUMBER;
8583     l_rqoh             NUMBER;
8584     l_qr               NUMBER;
8585     l_qs               NUMBER;
8586     l_atr              NUMBER;
8587     l_att              NUMBER;
8588     l_msg_count        number;
8589     l_return_status    VARCHAR2(3) := fnd_api.g_ret_sts_success;
8590     l_msg_data         varchar2(100);
8591     l_debug            NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'),
8592                                      0);
8593   begin
8594 
8595     --Find out the total atr for the item
8596     IF inv_cache.set_item_rec(p_ORG_ID, p_item_id) THEN
8597 
8598      /* IF inv_cache.item_rec.revision_qty_control_code = 2 THEN
8599         l_is_revision_ctrl := TRUE;
8600       ELSE
8601         l_is_revision_ctrl := FALSE;
8602       END IF;
8603 
8604       IF inv_cache.item_rec.lot_control_code = 2 THEN
8605         l_is_lot_ctrl := TRUE;
8606       ELSE
8607         l_is_lot_ctrl := FALSE;
8608       END IF; */
8609 
8610       IF inv_cache.item_rec.serial_number_control_code NOT IN (1, 6) THEN
8611         l_is_serial_ctrl := FALSE;
8612       ELSE
8613         l_is_serial_ctrl := TRUE;
8614       END IF;
8615 
8616     ELSE
8617       RAISE no_data_found;
8618     END IF;
8619     print_debug('In Get ATT for subinventory API', l_debug);
8620 
8621     inv_quantity_tree_pub.query_quantities(p_api_version_number      => 1.0,
8622                                            p_init_msg_lst            => fnd_api.g_false,
8623                                            x_return_status           => l_return_status,
8624                                            x_msg_count               => l_msg_count,
8625                                            x_msg_data                => l_msg_data,
8626                                            p_organization_id         => p_org_id,
8627                                            p_inventory_item_id       => p_item_id,
8628                                            p_tree_mode               => inv_quantity_tree_pub.g_transaction_mode,
8629                                            p_is_revision_control     => l_is_revision_ctrl,
8630                                            p_is_lot_control          => l_is_lot_ctrl,
8631                                            p_is_serial_control       => l_is_serial_ctrl,
8632                                            p_demand_source_type_id   => -9999 --should not be null
8633                                           ,
8634                                            p_demand_source_header_id => -9999 --should not be null
8635                                           ,
8636                                            p_demand_source_line_id   => -9999,
8637                                            p_revision                => NULL,
8638                                            p_lot_number              => NULL,
8639                                            p_subinventory_code       => p_sub,
8640                                            p_locator_id              => NULL,
8641                                            x_qoh                     => l_qoh,
8642                                            x_rqoh                    => l_rqoh,
8643                                            x_qr                      => l_qr,
8644                                            x_qs                      => l_qs,
8645                                            x_att                     => l_att,
8646                                            x_atr                     => l_atr);
8647 
8648     print_debug('ATR for Item ' || p_item_id || 'in subinventory ' ||
8649                 p_sub || ' is :' || l_atr,
8650                 l_debug);
8651     IF l_return_status <> fnd_api.g_ret_sts_success THEN
8652       l_atr := 0;
8653     END IF;
8654 
8655     RETURN l_atr;
8656   END get_att_for_subinventory;
8657 
8658   procedure update_bulk_labor_record(x_labor_plan_tbl           in OUT NOCOPY labor_plan_tbl,
8659                                      v_replenishment_required   in varchar2,
8660                                      v_destination_subinventory in varchar2,
8661                                      p_demand_qty               in OUT NOCOPY number,
8662                                      x_return_status            OUT NOCOPY varchar2)
8663 
8664    is
8665 
8666     i                       NUMBER := 0;
8667     divider                 INTEGER;
8668     REMinder                INTEGER;
8669     j                       number := 0;
8670     v_att                   NUMBER;
8671     n                       number := 0;
8672     sum_qty                 NUMBER := 0;
8673     current_conversion_rate NUMBER;
8674     next_conversion_rate    NUMBER;
8675     low_picking_order       NUMBER;
8676 
8677     l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
8678   BEGIN
8679 
8680     print_debug('Entered Bulk Labor Record API ', l_debug);
8681 
8682     v_att := x_labor_plan_tbl(0).att;
8683 
8684     print_debug(' v_att is ' || v_att, l_debug);
8685 
8686     WHILE (p_demand_qty >= x_labor_plan_tbl(x_labor_plan_tbl.Count - 1)
8687           .conversion_rate AND j <= x_labor_plan_tbl.Count - 1) loop
8688 
8689       IF (p_demand_qty >= v_att) AND
8690          (v_att >= x_labor_plan_tbl(j).conversion_rate) THEN
8691 
8692         divider := Floor(v_att / x_labor_plan_tbl(j).conversion_rate);
8693         print_debug('Qty in picking uom is ' || divider, l_debug);
8694 
8695         x_labor_plan_tbl(j).demand_qty_picking_uom := divider;
8696         x_labor_plan_tbl(j).demand_quantity := v_att;
8697         -- x_labor_plan_tbl(j).demand_quantity := v_att;
8698         -- p_demand_qty := p_demand_qty - v_att;
8699         p_demand_qty := p_demand_qty -
8700                         (divider * x_labor_plan_tbl(j).conversion_rate);
8701 
8702         print_debug('Demand Qty is ' || v_att, l_debug);
8703         print_debug('Conversion rate is ' || x_labor_plan_tbl(j)
8704                     .conversion_rate,
8705                     l_debug);
8706         print_debug('The Picking Subinventory is ' || x_labor_plan_tbl(j)
8707                     .picking_subinventory,
8708                     l_debug);
8709         print_debug('The Source Subinventory is ' || x_labor_plan_tbl(j)
8710                     .source_subinventory,
8711                     l_debug);
8712         v_att := 0;
8713 
8714       ELSIF (v_att >= x_labor_plan_tbl(j).conversion_rate) then
8715         IF Mod(v_att, x_labor_plan_tbl(j).conversion_rate) = 0 then
8716           v_att := v_att - x_labor_plan_tbl(j).conversion_rate;
8717         ELSE
8718           v_att := v_att - Mod(v_att, x_labor_plan_tbl(j).conversion_rate);
8719         END IF;
8720       ELSE
8721         -- For Replenishment cases
8722         -- Dbms_Output.put_line('v_final is '||x_labor_plan_tbl(j+1).att);
8723         --  Dbms_Output.put_line('qty is '||qty);
8724         if v_replenishment_required = 'Y' then
8725 
8726           print_debug('Replenishment is Enabled', l_debug);
8727           IF j < x_labor_plan_tbl.Count - 1 then
8728 
8729             current_conversion_rate := x_labor_plan_tbl(j).conversion_rate;
8730 
8731             print_debug('Current conversion_rate is' ||
8732                         current_conversion_rate,
8733                         l_debug);
8734 
8735             FOR k IN j + 1 .. x_labor_plan_tbl.Count - 1 loop
8736 
8737               next_conversion_rate := x_labor_plan_tbl(k).conversion_rate;
8738               print_debug('next_conversion_rate is' ||
8739                           next_conversion_rate,
8740                           l_debug);
8741               low_picking_order := k;
8742               EXIT WHEN x_labor_plan_tbl(k) .conversion_rate < x_labor_plan_tbl(j) .conversion_rate;
8743 
8744             END LOOP;
8745 
8746             sum_qty := 0;
8747 
8748             FOR i IN j + 1 .. x_labor_plan_tbl.Count - 1 loop
8749 
8750               IF x_labor_plan_tbl(i).conversion_rate = next_conversion_rate then
8751 
8752                 sum_qty := sum_qty + x_labor_plan_tbl(i).att;
8753 
8754                 -- Find the subinventory first picking order in this group
8755                 -- It shd be value of i which is j+1
8756 
8757               end IF;
8758 
8759             END LOOP;
8760 
8761             print_debug('sum_qty is' || sum_qty, l_debug);
8762             print_debug('low_picking_order is' || low_picking_order,
8763                         l_debug);
8764 
8765             IF p_demand_qty > sum_qty THEN
8766 
8767               IF (x_labor_plan_tbl(j)
8768                  .att - (x_labor_plan_tbl(j)
8769                   .demand_qty_picking_uom * x_labor_plan_tbl(j)
8770                   .conversion_rate)) >= x_labor_plan_tbl(j)
8771               .conversion_rate and x_labor_plan_tbl(low_picking_order)
8772               .source_subinventory = x_labor_plan_tbl(j)
8773               .picking_subinventory then
8774 
8775                 x_labor_plan_tbl(low_picking_order).att := x_labor_plan_tbl(low_picking_order)
8776                                                           .att +
8777                                                            x_labor_plan_tbl(j)
8778                                                           .conversion_rate;
8779                 x_labor_plan_tbl(j).att := x_labor_plan_tbl(j)
8780                                           .att - x_labor_plan_tbl(j)
8781                                           .conversion_rate;
8782 
8783                 x_labor_time_tbl(n1).demand_qty_picking_uom := 1; -- As maximum of 1 plt or 1 cs will be transferred
8784                 x_labor_time_tbl(n1).picking_uom := x_labor_plan_tbl(j)
8785                                                    .picking_uom;
8786                 x_labor_time_tbl(n1).picking_subinventory := x_labor_plan_tbl(j)
8787                                                             .picking_subinventory;
8788                 x_labor_time_tbl(n1).destination_subinventory := x_labor_plan_tbl(low_picking_order)
8789                                                                 .picking_subinventory;
8790 
8791                 print_debug('New ATT for Subinventory ' ||
8792                             x_labor_plan_tbl(low_picking_order)
8793                             .picking_subinventory || ' is ' ||
8794                             x_labor_plan_tbl(low_picking_order).att,
8795                             l_debug);
8796 
8797                 print_debug('New ATT for Source Subinventory ' ||
8798                             x_labor_plan_tbl(j)
8799                             .picking_subinventory || ' is ' ||
8800                             x_labor_plan_tbl(j).att,
8801                             l_debug);
8802 
8803                 n1 := n1 + 1;
8804                 -- Dbms_Output.put_line('att j+1 is '||x_labor_plan_tbl(j+1).att);
8805                 --Dbms_Output.put_line('qty1 is '||x_labor_plan_tbl(j+1).conversion_rate);
8806                 -- Dbms_Output.put_line('j is '||j);
8807 
8808               END IF;
8809             END IF;
8810           end if;
8811         end if;
8812         j := j + 1;
8813         IF j <= x_labor_plan_tbl.Count - 1 then
8814 
8815           v_att := x_labor_plan_tbl(j).att;
8816           print_debug('Demand Qty is ' || v_att, l_debug);
8817         END IF;
8818       END IF;
8819 
8820     END LOOP;
8821 
8822     FOR i IN x_labor_plan_tbl.FIRST .. x_labor_plan_tbl.LAST LOOP
8823       /*
8824         print_debug('total integral value is ' || x_labor_plan_tbl(i)
8825                     .demand_qty_picking_uom,
8826                     l_debug);
8827         print_debug('Demand qty is ' || x_labor_plan_tbl(i).demand_quantity,
8828                     l_debug);
8829         print_debug('att is ' || x_labor_plan_tbl(i).att, l_debug);
8830       */
8831       if x_labor_plan_tbl(i).demand_qty_picking_uom <> 0 then
8832         x_labor_time_tbl(n1).demand_qty_picking_uom := x_labor_plan_tbl(i)
8833                                                       .demand_qty_picking_uom;
8834         x_labor_time_tbl(n1).picking_uom := x_labor_plan_tbl(i).picking_uom;
8835         x_labor_time_tbl(n1).picking_subinventory := x_labor_plan_tbl(i)
8836                                                     .picking_subinventory;
8837         x_labor_time_tbl(n1).destination_subinventory := v_destination_subinventory;
8838         print_debug('n1 is ' || n1, l_debug);
8839         n1 := n1 + 1;
8840       end if;
8841     END LOOP;
8842   exception
8843     when others then
8844       print_debug('Error in Update Labor record: ' || SQLCODE || ' : ' ||
8845                   SQLERRM,
8846                   l_debug);
8847 
8848   end update_bulk_labor_record;
8849 
8850   function get_source_subinventory(p_item_id      in number,
8851                                    p_subinventory in varchar2)
8852     return varchar2 is
8853     v_source_subinventory varchar2(100);
8854 
8855   begin
8856 
8857     select source_subinventory
8858       into v_source_subinventory
8859       from MTL_ITEM_SUB_INVENTORIES
8860      where inventory_item_id = p_item_id
8861        and SECONDARY_INVENTORY = p_subinventory;
8862 
8863     return v_source_subinventory;
8864 
8865   exception
8866     when no_data_found then
8867       return null;
8868 
8869   end get_source_subinventory;
8870 
8871   PROCEDURE Release_Batch_CP(errbuf           OUT NOCOPY VARCHAR2,
8872                              retcode          OUT NOCOPY NUMBER,
8873                              p_wave_header_id in number)
8874 
8875    is
8876 
8877     x_msg_count     number;
8878     x_return_status VARCHAR2(100);
8879     x_msg_data      VARCHAR2(500);
8880     p_BATCH_ID      NUMBER;
8881     P_REQUEST_ID    NUMBER;
8882     l_debug         NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
8883     l_phase         VARCHAR2(100);
8884     l_status        VARCHAR2(100);
8885     l_dev_phase     VARCHAR2(100);
8886     l_dev_status    VARCHAR2(100);
8887     l_message       VARCHAR2(500);
8888     l_result        boolean;
8889   begin
8890 
8891     print_debug('Entered Release Batch CP - Call Create Batch Record',
8892                 l_debug);
8893 
8894     create_batch_record(x_return_status, p_wave_header_id);
8895 
8896     print_debug('Status after Call to create batch record' ||
8897                 x_return_status,
8898                 l_debug);
8899 
8900     if x_return_status = 'S' then
8901 
8902       savepoint release_lines_concurrent_sp;
8903 
8904       UPDATE wms_wp_wave_lines
8905          SET message               = 'This line has been Firmed in Wave ' ||
8906                                      p_wave_header_id,
8907              remove_from_Wave_flag = 'Y'
8908        WHERE delivery_detail_id IN
8909              (SELECT wwl.delivery_detail_id
8910                 FROM wms_wp_wave_lines wwl, wsh_delivery_details wdd
8911                WHERE wwl.wave_header_id = p_wave_header_id
8912 			     and wwl.delivery_detail_id = wdd.delivery_detail_id --11775489 get from wdd... FIXED
8913 				 and wwl.organization_id = wdd.organization_id
8914                  and nvl(remove_from_wave_flag, 'N') <> 'Y')
8915          and wave_header_id <> p_wave_header_id;
8916 
8917       update wms_wp_wave_headers_vl
8918          set wave_firmed_flag = 'Y'
8919        where wave_header_id = p_wave_header_id;
8920 
8921       --  commit;
8922 
8923       WSH_PICKING_BATCHES_GRP.release_wms_wave(p_release_mode        => 'CONCURRENT',
8924                                                p_pick_wave_header_id => p_wave_header_id,
8925                                                x_request_id          => p_request_id,
8926                                                X_RETURN_STATUS       => X_RETURN_STATUS,
8927                                                X_MSG_COUNT           => X_MSG_COUNT,
8928                                                X_MSG_DATA            => X_MSG_DATA,
8929                                                P_BATCH_REC           => new_wave_type, --???????g_wave_release_attribute_Rec
8930                                                X_BATCH_ID            => p_BATCH_ID);
8931 
8932 	 print_debug('Value of p batch id is' || p_batch_id, l_debug);
8933 
8934       /* update wms_wp_wave_headers_vl
8935         set request_id = p_request_id
8936       where wave_header_id = p_wave_header_id;*/
8937 
8938       --Making our concurrent program wait till the pick release concurrent program is complete
8939       l_result := FND_CONCURRENT.WAIT_FOR_REQUEST(request_id => p_request_id,
8940                                                   phase      => l_phase,
8941                                                   status     => l_status,
8942                                                   dev_phase  => l_dev_phase,
8943                                                   dev_status => l_dev_status,
8944                                                   message    => l_message);
8945 
8946       l_result := FND_CONCURRENT.SET_COMPLETION_STATUS(status  => l_status,
8947                                                        message => '');
8948 
8949 
8950 
8951       if x_return_status = 'S' then
8952         -- To take the Warning Status
8953         print_debug('Updating Wave Header Status after Calling Release Wave Concurrently',
8954                     l_debug);
8955         update_wave_header_status(x_return_status,
8956                                   p_wave_header_id,
8957                                   'Released',
8958 								  p_batch_id);
8959 
8960 
8961         get_actual_fill_rate(x_return_status, p_wave_header_id);
8962 
8963         print_debug('Status after Call to Get the Actual Fill Rate' ||
8964                     x_return_status,
8965                     l_debug);
8966 
8967         commit;
8968 
8969 elsif x_return_status = 'W'  then
8970 	  print_debug('Updating Wave Header Status after Calling Release Wave Concurrently',
8971                     l_debug);
8972         update_wave_header_status(x_return_status,
8973                                   p_wave_header_id,
8974                                   'Released(Warning)',
8975 								  p_batch_id);
8976 
8977 
8978         get_actual_fill_rate(x_return_status, p_wave_header_id);
8979 
8980         print_debug('Status after Call to Get the Actual Fill Rate' ||
8981                     x_return_status,
8982                     l_debug);
8983 
8984         commit;
8985 
8986       else
8987 
8988         rollback to release_lines_concurrent_sp;
8989       end if;
8990 
8991     end if;
8992 
8993   end Release_Batch_CP;
8994 
8995 
8996   function check_min_equip_Capacity(l_mmtt_tbl in num_t)
8997     return boolean is
8998 
8999     g_newline CONSTANT VARCHAR2(10) := fnd_global.newline;
9000     l_debug            NUMBER := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),
9001                                      0);
9002     g_bulk_fetch_limit NUMBER := 1000;
9003 
9004     g_hash_base  NUMBER := 1;
9005     g_hash_size  NUMBER := POWER(2, 25);
9006     sum_capacity number := 0;
9007     sum_qty      number := 0;
9008 
9009     l_item_vol             NUMBER; --item unit volume
9010     l_item_v_uom           VARCHAR2(3); --item unit volume UOM
9011     l_equip_v_uom          VARCHAR2(3); -- equipment volume UOM
9012     l_equip_vol            NUMBER; --equipment volume capacity
9013     l_eqp_capacity         NUMBER;
9014     l_minimum_fill_percent number;
9015     l_eq_it_v_uom_ratio    NUMBER := 1; --conversion ratio between equipment volume capacity and item unit volume UOM
9016     l_txn_pri_uom_ratio    NUMBER; --conversion rate between transaction uom and item primary UOM
9017     l_txn_uom_code         VARCHAR2(3); --transaction uom_code
9018     l_item_id              NUMBER;
9019     l_init_qty             NUMBER;
9020     l_item_prim_uom_code   VARCHAR2(3); --primary uom_code
9021     l_min_cap              NUMBER; --minimum equipment capacity for a task
9022     l_min_cap_temp         NUMBER;
9023     l_new_qty              NUMBER;
9024     p_task_id              number;
9025     l_organization_id      number;
9026 
9027     CURSOR c_eqp_capacity(p_task_id NUMBER) IS
9028       SELECT distinct res_equip.inventory_item_id
9029         FROM mtl_material_transactions_temp mmtt,
9030              bom_resource_equipments        res_equip,
9031              bom_resources                  res,
9032              bom_std_op_resources           tt_x_res
9033        WHERE mmtt.transaction_temp_id = p_task_id
9034          AND mmtt.standard_operation_id = tt_x_res.standard_operation_id
9035          AND tt_x_res.resource_id = res.resource_id
9036          AND res.resource_type = 1
9037          AND res_equip.resource_id = tt_x_res.resource_id;
9038 
9039   begin
9040 
9041     for i in l_mmtt_tbl.FIRST .. l_mmtt_tbl.LAST loop
9042 
9043       SELECT mmtt.transaction_uom,
9044              mmtt.inventory_item_id,
9045              mmtt.transaction_quantity,
9046              item.primary_uom_code,
9047              item.organization_id
9048         INTO l_txn_uom_code,
9049              l_item_id,
9050              l_init_qty,
9051              l_item_prim_uom_code,
9052              l_organization_id
9053         FROM mtl_material_transactions_temp mmtt,
9054              mtl_item_locations             mil,
9055              mtl_secondary_inventories      msi,
9056              mtl_system_items               item
9057        WHERE mmtt.transaction_temp_id = l_mmtt_tbl(i)
9058          AND mmtt.locator_id = mil.inventory_location_id(+)
9059          AND mmtt.organization_id = mil.organization_id(+)
9060          AND mmtt.subinventory_code = msi.secondary_inventory_name
9061          AND mmtt.organization_id = msi.organization_id
9062          AND mmtt.inventory_item_id = item.inventory_item_id
9063          AND mmtt.organization_id = item.organization_id;
9064 
9065       l_min_cap := -9999;
9066 
9067       sum_qty := sum_qty + l_init_qty;
9068 
9069       IF (l_txn_uom_code IS NULL OR l_item_id IS NULL OR l_init_qty IS NULL OR
9070          l_item_prim_uom_code IS NULL) THEN
9071 
9072         print_debug('Necessary UOM information is missing for task: ' ||
9073                     l_mmtt_tbl(i),
9074                     l_Debug);
9075         RETURN TRUE;
9076       END IF;
9077 
9078       IF l_txn_uom_code = l_item_prim_uom_code THEN
9079         -- {
9080         l_txn_pri_uom_ratio := 1;
9081       ELSE
9082 
9083         --Compute conversion ratio between transaction UOM and item primary UOM
9084         inv_convert.inv_um_conversion(from_unit => l_txn_uom_code,
9085                                       to_unit   => l_item_prim_uom_code,
9086                                       item_id   => l_item_id,
9087                                       uom_rate  => l_txn_pri_uom_ratio);
9088       END IF; -- }
9089 
9090       IF (l_txn_pri_uom_ratio = -99999) THEN
9091         -- { uom conversion failure
9092         print_debug('txn/item uom ratio calculation failed for task: ' ||
9093                     l_mmtt_tbl(i),
9094                     l_debug);
9095         RETURN TRUE;
9096       END IF; -- }
9097 
9098       print_debug('UOM conversion data:' ||
9099                   TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),
9100                   l_debug);
9101       print_debug('l_txn_pri_uom_ratio => ' || l_txn_pri_uom_ratio,
9102                   l_debug);
9103 
9104       IF (inv_cache.set_item_rec(l_organization_id, l_item_id)) THEN
9105         -- {
9106         l_item_v_uom := inv_cache.item_rec.volume_uom_code;
9107         l_item_vol   := inv_cache.item_rec.unit_volume;
9108       END IF;
9109 
9110       IF l_item_v_uom IS NOT NULL THEN
9111         -- {
9112         OPEN c_eqp_capacity(l_mmtt_tbl(i));
9113         LOOP
9114           -- {  --Equipment loop
9115           FETCH c_eqp_capacity
9116             INTO l_eqp_capacity;
9117           EXIT WHEN c_eqp_capacity%NOTFOUND;
9118 
9119           IF (inv_cache.set_item_rec(l_organization_id, l_eqp_capacity)) THEN
9120             -- {
9121             l_equip_v_uom          := inv_cache.item_rec.volume_uom_code;
9122             l_equip_vol            := inv_cache.item_rec.internal_volume;
9123             l_minimum_fill_percent := inv_cache.item_rec.minimum_fill_percent;
9124           END IF; -- }
9125 
9126           IF (l_equip_vol IS NOT NULL AND l_item_vol IS NOT NULL) THEN
9127             -- {
9128             l_eq_it_v_uom_ratio := -9999;
9129             l_min_cap_temp      := -9999;
9130           END IF; -- }
9131 
9132           IF (l_item_v_uom IS NOT NULL AND l_equip_v_uom IS NOT NULL) THEN
9133             -- {
9134             IF l_equip_v_uom = l_item_v_uom THEN
9135               -- {
9136               l_eq_it_v_uom_ratio := 1;
9137             ELSE
9138               inv_convert.inv_um_conversion(from_unit => l_equip_v_uom,
9139                                             to_unit   => l_item_v_uom,
9140                                             item_id   => 0,
9141                                             uom_rate  => l_eq_it_v_uom_ratio);
9142             END IF; -- }
9143           END IF; -- }
9144 
9145           print_debug('l_equip_vol = ' || l_equip_vol, l_debug);
9146           print_debug('l_item_vol = ' || l_item_vol, l_debug);
9147           print_debug('l_eq_it_v_uom_ratio = ' || l_eq_it_v_uom_ratio,
9148                       l_debug);
9149 
9150           IF l_eq_it_v_uom_ratio <> -9999 THEN
9151             l_min_cap_temp := TRUNC((l_equip_vol * l_eq_it_v_uom_ratio *
9152                                     (l_minimum_fill_percent / 100)) /
9153                                     l_item_vol);
9154             IF (l_min_cap_temp = 0) THEN
9155               -- {
9156               l_min_cap_temp := (l_equip_vol * l_eq_it_v_uom_ratio) /
9157                                 l_item_vol;
9158             end if;
9159           ELSE
9160             print_debug('Both eqp/item volume and eqp/item weight uom ratio calculation failed' ||
9161                         TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),
9162                         l_debug);
9163 
9164           END IF; -- }
9165 
9166           print_debug('l_min_cap_temp = ' || l_min_cap_temp, l_debug);
9167           print_debug('l_min_cap = ' || l_min_cap, l_debug);
9168 
9169           IF ((l_min_cap_temp <> -9999 AND l_min_cap_temp < l_min_cap) OR
9170              l_min_cap = -9999) THEN
9171             -- {
9172             l_min_cap := l_min_cap_temp;
9173 
9174             --       END IF ;  -- }
9175           ELSE
9176             print_debug('Eqp/it vol is not defined: ' ||
9177                         TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),
9178                         l_debug);
9179             RETURN TRUE;
9180           END IF; -- }
9181 
9182         END LOOP; -- }   End of Equipment loop
9183 
9184         CLOSE c_eqp_capacity;
9185 
9186       end if;
9187 
9188       --sum_capacity := l_min_cap + sum_capacity;
9189       sum_capacity := l_min_cap;
9190 
9191       IF (l_min_cap = -9999) THEN
9192         -- {
9193         print_debug('invalid capacity for all equipment' ||
9194                     TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),
9195                     l_Debug);
9196         RETURN TRUE;
9197       END IF; -- }
9198 
9199     end loop;
9200 
9201     print_debug('Sum of Capacity for all tasks in user task type is  ' ||
9202                 sum_capacity,
9203                 l_Debug);
9204     print_debug('Sum of transaction qty  for all tasks in user task type is  ' ||
9205                 sum_qty,
9206                 l_Debug);
9207 
9208     if sum_qty >= sum_capacity then
9209 
9210       return true;
9211 
9212     else
9213 
9214       return false;
9215 
9216     end if;
9217 
9218   exception
9219 
9220     when others then
9221       print_debug('Error in Get Minimum Equipment Capacity : ' || SQLCODE ||
9222                   ' : ' || SQLERRM,
9223                   l_debug);
9224       return false;
9225   end check_min_equip_Capacity;
9226 
9227   function get_tasks_exist(p_consol_locator_id in number,
9228                            p_delivery_id       in number)
9229 
9230    return boolean
9231 
9232    IS
9233 
9234     l_mmtt_count number;
9235     l_debug      NUMBER := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'), 0);
9236   begin
9237 
9238     SELECT count(distinct mmtt.transaction_temp_id)
9239       into l_mmtt_count
9240       FROM mtl_material_transactions_temp mmtt,
9241            wsh_Delivery_details           wdd,
9242            WSH_NEW_DELIVERIES             WND,
9243            WSH_DELIVERY_ASSIGNMENTS       WDA
9244      WHERE wdd.source_line_id = mmtt.trx_source_line_id
9245        AND WND.DELIVERY_ID(+) = WDA.DELIVERY_ID
9246        AND WDA.DELIVERY_DETAIL_ID = WDD.DELIVERY_DETAIL_ID
9247        and mmtt.transfer_to_location = p_consol_locator_id
9248        and mmtt.wms_task_status <> 8
9249        and wnd.delivery_id <> p_delivery_id
9250        and wnd.delivery_id is not null;
9251 
9252     print_debug('Count of Tasks that are already  been worked upon in the Consolidation Locator' ||
9253                 l_mmtt_count,
9254                 l_Debug);
9255 
9256     if l_mmtt_count > 0 then
9257 
9258       return false;
9259 
9260     else
9261 
9262       return true;
9263 
9264     end if;
9265 
9266   exception
9267 
9268     when others then
9269 
9270       return false;
9271 
9272   end get_tasks_exist;
9273 
9274   function get_tasks_exist(p_consol_locator_id in number)
9275 
9276    return boolean
9277 
9278    IS
9279     l_mmtt_count number;
9280     l_debug      NUMBER := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'), 0);
9281   begin
9282 
9283     SELECT count(distinct mmtt.transaction_temp_id)
9284       into l_mmtt_count
9285       FROM mtl_material_transactions_temp mmtt,
9286            wsh_Delivery_details           wdd,
9287            WSH_NEW_DELIVERIES             WND,
9288            WSH_DELIVERY_ASSIGNMENTS       WDA
9289      WHERE wdd.source_line_id = mmtt.trx_source_line_id
9290        AND WND.DELIVERY_ID(+) = WDA.DELIVERY_ID
9291        AND WDA.DELIVERY_DETAIL_ID = WDD.DELIVERY_DETAIL_ID
9292        and mmtt.wms_task_status <> 8
9293        and mmtt.transfer_to_location = p_consol_locator_id
9294        and wnd.delivery_id is not null;
9295 
9296     print_debug('Count of Tasks that are already  been worked upon in the Consolidation Locator' ||
9297                 l_mmtt_count,
9298                 l_Debug);
9299 
9300     if l_mmtt_count > 0 then
9301 
9302       return false;
9303 
9304     else
9305 
9306       return true;
9307 
9308     end if;
9309 
9310   exception
9311 
9312     when others then
9313 
9314       return false;
9315 
9316   end get_tasks_exist;
9317 
9318   PROCEDURE Task_Release_CP(errbuf            OUT NOCOPY VARCHAR2,
9319                             retcode           OUT NOCOPY NUMBER,
9320                             p_organization_id in number,
9321                             p_query_name      in varchar2,
9322                             p_task_release_id in number) is
9323 
9324     l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
9325 
9326     CURSOR c_saved_queries(p_query_name VARCHAR2) IS
9327       select field_name,
9328              --ltrim(rtrim(field_value)) field_value,
9329              field_value,
9330              organization_id,
9331              query_type
9332         from wms_saved_queries
9333        where query_name = p_query_name
9334          and (query_type = 'TASK_PLANNING' or
9335              query_type = 'TEMP_TASK_PLANNING')
9336          FOR UPDATE NOWAIT;
9337 
9338     cursor c_wave_temp is
9339       select transaction_temp_id,
9340              status,
9341              status_id,
9342              task_type,
9343              task_type_id,
9344              source_header
9345         from wms_waveplan_tasks_temp;
9346 
9347     cursor c_mmtt is
9348       select distinct transaction_temp_id from wms_waveplan_tasks_temp;
9349 
9350     rec_saved_queries c_saved_queries%rowtype;
9351 
9352     l_field_name_table      wms_wave_planning_pvt.field_name_table_type;
9353     l_field_value_table     wms_wave_planning_pvt.field_value_table_type;
9354     l_organization_id_table wms_wave_planning_pvt.organization_id_table_type;
9355     l_query_type_table      wms_wave_planning_pvt.query_type_table_type;
9356 
9357     l_return_status VARCHAR2(1);
9358     l_msg_data      VARCHAR2(120);
9359     l_msg_count     NUMBER;
9360     l_save_count    NUMBER;
9361     l_return_msg    VARCHAR2(120);
9362     l_record_count  NUMBER;
9363 
9364     l_query_name     varchar2(100);
9365     l_return_message VARCHAR2(4000);
9366     -- l_count          number := 0;
9367     -- Replenishment Complete declaration section
9368     p_carton_grouping_id number;
9369     --l_mmtt_table         num_t := num_t();--11870443 make this global, rename to g_mmtt_table
9370     l_mmtt_table2        number_table_type;
9371     l_final_mmtt_table   number_table_type;
9372     no_of_tasks          number := 0;
9373     --   no_of_rc_tasks       number := 0;
9374     --p_batch_id           number;
9375 
9376     CURSOR c_carton_grouping_id IS
9377       SELECT DISTINCT carton_grouping_id
9378         FROM wms_waveplan_tasks_temp wwtt,
9379              mtl_txn_request_lines   mtrl,
9380              wsh_Delivery_details    wdd
9381        WHERE wwtt.move_order_line_id = mtrl.line_id
9382          and wdd.source_line_id = wwtt.transaction_source_line_id
9383          and wwtt.transaction_temp_id in
9384              (select transaction_temp_id
9385                 from wms_wp_tp_mmtt
9386                where indicator_flag = 'C')
9387          and wwtt.status_id = 8; --- Ajith????????????
9388     --  order by wdd.shipment_priority_code;
9389 
9390     CURSOR c_rc_wdds is
9391       SELECT distinct wdd.delivery_detail_id
9392         FROM mtl_material_transactions_temp mmtt,
9393              mtl_txn_request_lines          mtrl,
9394              wsh_Delivery_details           wdd
9395        WHERE mmtt.move_order_line_id = mtrl.line_id
9396          AND wdd.source_line_id = mmtt.trx_source_line_id
9397          AND Nvl(wdd.REPLENISHMENT_STATUS, 'C') = 'C'
9398          AND mtrl.carton_grouping_id = p_carton_grouping_id;
9399 
9400 	--11870443 modified this cursor for performance
9401 	--use union all in inner query and add outer query instead of using or condition
9402     CURSOR c_rr_wdds is
9403 		select DISTINCT (DELIVERY_DETAIL_ID) from
9404 		(
9405 		SELECT WDD.DELIVERY_DETAIL_ID DELIVERY_DETAIL_ID
9406 		FROM WSH_DELIVERY_DETAILS WDD
9407 		WHERE WDD.REPLENISHMENT_STATUS = 'R'
9408 		 AND (WDD.BATCH_ID IN
9409 			   ( SELECT WDD.BATCH_ID --bug14534180
9410 				 FROM WSH_DELIVERY_DETAILS WDD,
9411 					  MTL_TXN_REQUEST_LINES MTRL,
9412 					  MTL_MATERIAL_TRANSACTIONS_TEMP MMTT
9413 				 WHERE MMTT.MOVE_ORDER_LINE_ID = MTRL.LINE_ID
9414 					   AND WDD.SOURCE_LINE_ID = MMTT.TRX_SOURCE_LINE_ID
9415 					   AND MTRL.CARTON_GROUPING_ID = p_carton_grouping_id))
9416 		UNION ALL
9417 		SELECT WDD.DELIVERY_DETAIL_ID DELIVERY_DETAIL_ID
9418 		FROM WSH_DELIVERY_DETAILS WDD
9419 		WHERE WDD.REPLENISHMENT_STATUS = 'R'
9420 		 AND  (WDD.BATCH_ID IN
9421 			   ( SELECT SELECTED_BATCH_ID
9422 				 FROM WSH_PICKING_BATCHES WPB,
9423 					  MTL_TXN_REQUEST_LINES MTRL,
9424 					  WSH_DELIVERY_DETAILS WDD2,
9425 					  MTL_MATERIAL_TRANSACTIONS_TEMP MMTT
9426 				 WHERE MMTT.MOVE_ORDER_LINE_ID = MTRL.LINE_ID
9427 					   AND WDD2.SOURCE_LINE_ID = MMTT.TRX_SOURCE_LINE_ID
9428 					   AND WPB.BATCH_ID = WDD2.BATCH_ID
9429 					   AND MTRL.CARTON_GROUPING_ID = p_carton_grouping_id))
9430 		);
9431 
9432     CURSOR c_rc_tasks is
9433       SELECT wwtt.transaction_temp_id
9434         FROM wms_waveplan_tasks_temp wwtt, mtl_txn_request_lines mtrl
9435        WHERE wwtt.move_order_line_id = mtrl.line_id
9436          and wwtt.transaction_temp_id in
9437              (select transaction_temp_id
9438                 from wms_wp_tp_mmtt
9439                where indicator_flag = 'C')
9440          AND mtrl.carton_grouping_id = p_carton_grouping_id;
9441 
9442     -- Minimum Equipment Capacity
9443 
9444     CURSOR c_task_type is
9445       SELECT DISTINCT wwtt.user_task_type
9446         FROM wms_waveplan_tasks_temp wwtt;
9447     /*
9448     p_user_task_type varchar2(4);
9449 
9450              CURSOR c_tasktype_mmtt(p_user_task_type) is
9451           SELECT wwtt.transaction_temp_id transaction_temp_id
9452             FROM wms_waveplan_tasks_temp  wwtt
9453             WHERE user_task_type=p_user_task_type;
9454 
9455     */
9456     -- Reverse Trip Stop
9457 
9458     l_Stop_id number;
9459     l_trip_id number;
9460     j         number;
9461 
9462     CURSOR c_get_trip is
9463       SELECT distinct wts.trip_id trip_id
9464         FROM wms_waveplan_tasks_temp  wwtt,
9465              wsh_Delivery_details     wdd,
9466              WSH_NEW_DELIVERIES       WND,
9467              WSH_DELIVERY_ASSIGNMENTS WDA,
9468              WSH_TRIP_STOPS           WTS,
9469              WSH_DELIVERY_LEGS        WLG
9470        WHERE WND.DELIVERY_ID(+) = WDA.DELIVERY_ID
9471          AND wdd.source_line_id = wwtt.transaction_source_line_id
9472          AND WDA.DELIVERY_DETAIL_ID = WDD.DELIVERY_DETAIL_ID
9473          AND WTS.STOP_ID(+) = WLG.DROP_OFF_STOP_ID
9474          AND WLG.DELIVERY_ID(+) = WND.DELIVERY_ID
9475          and wwtt.transaction_temp_id in
9476              (select transaction_temp_id
9477                 from wms_wp_tp_mmtt
9478                where indicator_flag = 'F') --- Ajith????????????
9479          and wwtt.status_id = 8
9480          and wts.trip_id is not null; --  Get All Trips for tasks  in UnReleased Status
9481 
9482     CURSOR c_get_trip_Stop is
9483       SELECT distinct wts.stop_id stop_id, wts.STOP_SEQUENCE_NUMBER
9484         FROM mtl_material_transactions_temp mmtt,
9485              wsh_Delivery_details           wdd,
9486              WSH_NEW_DELIVERIES             WND,
9487              WSH_DELIVERY_ASSIGNMENTS       WDA,
9488              WSH_TRIP_STOPS                 WTS,
9489              WSH_DELIVERY_LEGS              WLG
9490        WHERE WND.DELIVERY_ID(+) = WDA.DELIVERY_ID
9491          and wts.trip_id = l_trip_id
9492          AND wdd.source_line_id = mmtt.trx_source_line_id
9493          AND WDA.DELIVERY_DETAIL_ID = WDD.DELIVERY_DETAIL_ID
9494          AND WTS.STOP_ID(+) = WLG.DROP_OFF_STOP_ID
9495          AND WLG.DELIVERY_ID(+) = WND.DELIVERY_ID
9496        ORDER BY wts.STOP_SEQUENCE_NUMBER desc;
9497 
9498     CURSOR c_get_mmtt is
9499       SELECT distinct mmtt.transaction_temp_id transaction_temp_id
9500         FROM mtl_material_transactions_temp mmtt,
9501              wsh_Delivery_details           wdd,
9502              WSH_NEW_DELIVERIES             WND,
9503              WSH_DELIVERY_ASSIGNMENTS       WDA,
9504              WSH_TRIP_STOPS                 WTS,
9505              WSH_DELIVERY_LEGS              WLG
9506        WHERE wts.stop_id = l_stop_id
9507          AND wdd.source_line_id = mmtt.trx_source_line_id
9508          AND WND.DELIVERY_ID(+) = WDA.DELIVERY_ID
9509          AND WDA.DELIVERY_DETAIL_ID = WDD.DELIVERY_DETAIL_ID
9510          AND WTS.STOP_ID(+) = WLG.DROP_OFF_STOP_ID
9511          AND WLG.DELIVERY_ID(+) = WND.DELIVERY_ID
9512       /*   and wwtt.transaction_temp_id in
9513                          (select transaction_temp_id
9514                             from wms_wp_tp_mmtt
9515                            where indicator_flag = 'F') --- Ajith???????????? */
9516       --    AND wwtt.status_id <> 6
9517       --  ORDER BY wts.STOP_SEQUENCE_NUMBER desc
9518       union
9519       SELECT distinct mmt.transaction_id transaction_temp_id
9520         FROM mtl_material_transactions mmt,
9521              wsh_Delivery_details      wdd,
9522              WSH_NEW_DELIVERIES        WND,
9523              WSH_DELIVERY_ASSIGNMENTS  WDA,
9524              WSH_TRIP_STOPS            WTS,
9525              WSH_DELIVERY_LEGS         WLG
9526        WHERE wts.stop_id = l_stop_id
9527          AND wdd.source_line_id = mmt.trx_source_line_id
9528          AND WND.DELIVERY_ID(+) = WDA.DELIVERY_ID
9529          AND WDA.DELIVERY_DETAIL_ID = WDD.DELIVERY_DETAIL_ID
9530          AND WTS.STOP_ID(+) = WLG.DROP_OFF_STOP_ID
9531          AND WLG.DELIVERY_ID(+) = WND.DELIVERY_ID
9532       /*   and wwtt.transaction_temp_id in
9533                          (select transaction_temp_id
9534                             from wms_wp_tp_mmtt
9535                            where indicator_flag = 'F') --- Ajith???????????? */
9536       --    AND wwtt.status_id <> 6
9537       --   ORDER BY wts.STOP_SEQUENCE_NUMBER desc
9538       ;
9539 
9540     -- Consolidation Locator
9541 
9542     CURSOR c_consol_locator IS
9543       select distinct wwtt.to_locator_id, wwtt.to_locator
9544         from wms_waveplan_tasks_temp wwtt, mtl_item_locations_kfv mil
9545        where wwtt.to_locator_id = mil.inventory_location_id
9546          and wwtt.to_organization_id = mil.organization_id
9547          and mil.inventory_location_type in (4, 5)
9548          and wwtt.transaction_temp_id in
9549              (select transaction_temp_id
9550                 from wms_wp_tp_mmtt
9551                where indicator_flag = 'T') --- Ajith????????????
9552          and wwtt.status_id = 8; -- We get only the Un Released Lines Delivery.
9553 
9554     p_consol_locator_id number;
9555 
9556     p_delivery_id number;
9557 
9558     cursor c_delivery is
9559       select distinct wnd.delivery_id
9560         from wsh_delivery_details     wdd,
9561              wsh_new_deliveries       wnd,
9562              wsh_delivery_details     wdd2,
9563              wsh_delivery_assignments wda
9564        where wdd2.
9565        lpn_id in (select distinct lpn_id
9566                           from mtl_onhand_quantities_detail moqd
9567                          where locator_id = p_consol_locator_id)
9568          and WND.DELIVERY_ID(+) = WDA.DELIVERY_ID
9569          AND WDA.DELIVERY_DETAIL_ID = WDD.DELIVERY_DETAIL_ID
9570          AND wda.parent_delivery_detail_id = wdd2.delivery_detail_id(+);
9571 
9572     -- Get all WDDS that are in Released to Warehouse and Staged status
9573     cursor c_wdd_delivery is
9574       select wdd.delivery_detail_id
9575         from wsh_delivery_details     wdd,
9576              wsh_delivery_assignments wda,
9577              wsh_new_deliveries       wnd
9578        where wnd.delivery_id = p_delivery_id
9579          and WND.DELIVERY_ID(+) = WDA.DELIVERY_ID
9580          AND WDA.DELIVERY_DETAIL_ID = WDD.DELIVERY_DETAIL_ID
9581          and wdd.released_status in ('Y', 'S');
9582 
9583     cursor c_wdd_Staged is
9584       select wdd.delivery_detail_id
9585         from wsh_delivery_details     wdd,
9586              wsh_delivery_assignments wda,
9587              wsh_new_deliveries       wnd
9588        where wnd.delivery_id = p_delivery_id
9589          and WND.DELIVERY_ID(+) = WDA.DELIVERY_ID
9590          AND WDA.DELIVERY_DETAIL_ID = WDD.DELIVERY_DETAIL_ID
9591          and wdd.released_status in ('Y');
9592 
9593     l_wdd_count number;
9594 
9595     l_wdd_staged_count number;
9596 
9597     CURSOR c_delivery_mmtt IS
9598       SELECT DISTINCT WND.DELIVERY_ID delivery_id
9599         FROM wms_waveplan_tasks_temp  wwtt,
9600              wsh_Delivery_details     wdd,
9601              WSH_NEW_DELIVERIES       WND,
9602              WSH_DELIVERY_ASSIGNMENTS WDA
9603        WHERE wdd.source_line_id = wwtt.transaction_source_line_id
9604          and wwtt.transaction_temp_id in
9605              (select transaction_temp_id
9606                 from wms_wp_tp_mmtt
9607                where indicator_flag = 'T') --- Ajith????????????
9608          AND WND.DELIVERY_ID(+) = WDA.DELIVERY_ID
9609          AND WDA.DELIVERY_DETAIL_ID = WDD.DELIVERY_DETAIL_ID
9610          and wwtt.status_id = 8 -- We get only the Un Released Lines Delivery.
9611          and wwtt.to_locator_id = p_consol_locator_id
9612        order by WND.DELIVERY_ID;
9613 
9614     p_delivery_mmtt_id number;
9615 
9616     -- We get all the tasks for the delivery.
9617     CURSOR c_get_mmtt_delivery(p_delivery_mmtt_id in number) is
9618       SELECT distinct wwtt.transaction_temp_id transaction_temp_id
9619         FROM wms_waveplan_tasks_temp  wwtt,
9620              wsh_Delivery_details     wdd,
9621              WSH_NEW_DELIVERIES       WND,
9622              WSH_DELIVERY_ASSIGNMENTS WDA
9623        WHERE WND.DELIVERY_ID = p_delivery_mmtt_id
9624          and wdd.source_line_id = wwtt.transaction_source_line_id
9625          and wwtt.transaction_temp_id in
9626              (select transaction_temp_id
9627                 from wms_wp_tp_mmtt
9628                where indicator_flag = 'T')
9629          AND WND.DELIVERY_ID(+) = WDA.DELIVERY_ID
9630          AND WDA.DELIVERY_DETAIL_ID = WDD.DELIVERY_DETAIL_ID;
9631 
9632     l_unreleased_count number := 0;
9633 
9634     l_delivery_count number;
9635     -- l_packed_count           number;
9636     l_completed_task_count  number;
9637     l_total_completed_count number := 0;
9638     v_task_count            number := 0;
9639     v_final_completed_flag  varchar2(1) := 'Y';
9640     --  v_completed_flag         varchar2(1);
9641     l_wdd_table              number_table_type;
9642     l_wdd_table1             number_table_type;
9643     l_wdd_table2             number_table_type;
9644     l_wdd_table3             number_table_type;
9645     l_replenishment_complete VARCHAR2(1);
9646     l_fill_capacity          VARCHAR2(1);
9647     l_reverse_trip_Stop      VARCHAR2(1);
9648     l_consol_locator         VARCHAR2(1);
9649     l_custom_flag            VARCHAR2(1);
9650     l_trip_tolerance         number;
9651     l_dummy_delivery         number;
9652     --  no_of_tasks_delivery     number := 0;
9653     l_replen_tolerance       number;
9654     l_cons_locator_tolerance number;
9655     l_custom_plan_tolerance  number;
9656     l_release_delivery       varchar2(1) := 'N';
9657     l_table_count            number;
9658     --no_of_packed_tasks       number := 0;
9659     -- process_next_delivery    varchar2(1) := 'Y';
9660     -- l_first_time             varchar2(1) := 'Y';
9661     --   k2                       number := 0;
9662     l_sum_qty number := 0;
9663 
9664     --Catronization
9665     l_cartonization_required VARCHAR2(1);
9666     --  l_move_order_line_id     varchar2(4000);
9667 
9668 
9669 
9670     --  cartonization_profile        VARCHAR2(1)   := 'Y';
9671     v_cart_value             NUMBER;
9672     l_cartonize_sales_orders VARCHAR2(1) := NULL;
9673 
9674     cursor c_carton_lines is
9675       select wdd.delivery_Detail_id, mmtt.move_order_line_id
9676       -- into p_line_rows(l1), p_move_order_line_tbl(l1)
9677         from wsh_delivery_details wdd, mtl_material_transactions_temp mmtt
9678        where wdd.Source_line_id = mmtt.trx_source_line_id
9679          AND Nvl(wdd.REPLENISHMENT_STATUS, 'C') = 'C'
9680          and mmtt.transaction_temp_id in
9681              (select * from table(wms_wave_planning_pvt.list_mmtt_release)); --11870443
9682 
9683     /*    cursor c_move_order_header is
9684     select distinct HEADER_ID
9685       from mtl_txn_request_lines
9686      where to_char(line_id) in
9687            (SELECT TRIM(SUBSTR(txt,
9688                                INSTR(txt, ',', 1, level) + 1,
9689                                INSTR(txt, ',', 1, level + 1) -
9690                                INSTR(txt, ',', 1, level) - 1)) AS token
9691               FROM (SELECT ',' || l_move_order_line_id || ',' AS txt
9692                       FROM dual)
9693             CONNECT BY level <=
9694                        LENGTH(txt) - LENGTH(REPLACE(txt, ',', '')) - 1);*/
9695 
9696     l_attr_tab            wsh_delivery_autocreate.grp_attr_tab_type;
9697     l_action_rec          wsh_delivery_autocreate.action_rec_type;
9698     l_target_rec          wsh_delivery_autocreate.grp_attr_rec_type;
9699     l_group_info          wsh_delivery_autocreate.grp_attr_tab_type;
9700     l_matched_entities    wsh_util_core.id_tab_type;
9701     l_out_rec             wsh_delivery_autocreate.out_rec_type;
9702     p_line_rows           wsh_util_core.id_tab_type;
9703     p_carton_grouping_tbl inv_move_order_pub.num_tbl_type;
9704     p_move_order_line_tbl inv_move_order_pub.num_tbl_type;
9705     l_match_found         boolean;
9706     x_return_status       varchar2(1);
9707 
9708     -- For Creating Move Order Header
9709 
9710     l_trohdr_rec       INV_Move_Order_PUB.Trohdr_Rec_Type;
9711     l_trohdr_val_rec   INV_Move_Order_PUB.Trohdr_Val_Rec_Type;
9712     l_x_trohdr_rec     INV_Move_Order_PUB.Trohdr_Rec_Type;
9713     l_x_trohdr_val_rec INV_Move_Order_PUB.Trohdr_Val_Rec_Type;
9714     l_commit           VARCHAR2(1) := FND_API.G_TRUE;
9715 
9716     TYPE group_match_seq_rec_type IS RECORD(
9717       delivery_detail_id NUMBER,
9718       match_group_id     NUMBER,
9719       delivery_group_id  NUMBER);
9720 
9721     TYPE group_match_seq_tab_type IS TABLE OF group_match_seq_rec_type INDEX BY BINARY_INTEGER;
9722 
9723     l_group_match_seq_tbl group_match_seq_tab_type;
9724 
9725     --  p_move_order_hdr_tbl num_tab;
9726 
9727     l_repl_tasks number := 0;
9728 
9729     l_replcom_tasks number := 0;
9730     l_temp          number;
9731     line_count      number := 1;
9732   begin
9733 
9734     l_record_count            := 0;
9735     l_is_unreleased           := FALSE;
9736     l_is_pending              := FALSE;
9737     l_is_queued               := FALSE;
9738     l_is_dispatched           := FALSE;
9739     l_is_active               := FALSE;
9740     l_is_loaded               := FALSE;
9741     l_is_completed            := FALSE;
9742     l_include_inbound         := FALSE;
9743     l_include_outbound        := FALSE;
9744     l_include_crossdock       := FALSE;
9745     l_include_manufacturing   := FALSE;
9746     l_include_warehousing     := FALSE;
9747     l_include_sales_orders    := FALSE;
9748     l_include_internal_orders := FALSE;
9749     l_include_replenishment   := FALSE;
9750     l_include_mo_transfer     := FALSE;
9751     l_include_mo_issue        := FALSE;
9752     l_include_lpn_putaway     := FALSE;
9753     l_include_staging_move    := FALSE;
9754     l_include_cycle_count     := FALSE;
9755     l_is_pending_plan         := FALSE;
9756     l_is_inprogress_plan      := FALSE;
9757     l_is_completed_plan       := FALSE;
9758     l_is_cancelled_plan       := FALSE;
9759     l_is_aborted_plan         := FALSE;
9760     l_query_independent_tasks := FALSE;
9761     l_query_planned_tasks     := FALSE;
9762 
9763     OPEN c_saved_queries(p_query_name);
9764 
9765     FETCH c_saved_queries BULK COLLECT
9766       INTO l_field_name_table, l_field_value_table, l_organization_id_table, l_query_type_table;
9767 
9768     -- If no records founds for the given query name
9769     -- then close the cursor and return informing invalid query name.
9770 
9771     print_DEBUG('c_saved_queries%ROWCOUNT = ' || c_saved_queries%ROWCOUNT,
9772                 l_debug);
9773 
9774     IF c_saved_queries%ROWCOUNT = 0 THEN
9775       CLOSE c_saved_queries;
9776       print_DEBUG('No data found for query name = ' || p_query_name,
9777                   l_debug);
9778       --x_rowcount      := 0;
9779       --  x_return_status := fnd_api.g_ret_sts_success;
9780       --  x_return_message:= 'No data found for query name = ' || p_query_name ;
9781 
9782       RETURN;
9783     END IF;
9784 
9785     CLOSE c_saved_queries;
9786 
9787     print_DEBUG('field_name_table.count ' || l_field_name_table.count,
9788                 l_debug);
9789 
9790     print_DEBUG('Bulk collect from c_saved_queries successful and closed c_saved_queries cursor',
9791                 l_debug);
9792 
9793     print_DEBUG('Calling SET_QUERY_TASKS_PARAMETERS', l_Debug);
9794 
9795     SET_QUERY_TASKS_PARAMETERS(p_field_name_table      => l_field_name_table,
9796                                p_field_value_table     => l_field_value_table,
9797                                p_organization_id_table => l_organization_id_table,
9798                                p_query_type_table      => l_query_type_table,
9799                                x_return_status         => l_return_status,
9800                                x_return_message        => l_return_message);
9801 
9802     print_DEBUG('SET_QUERY_TASKS_PARAMETERS return status = ' ||
9803                 l_return_status,
9804                 l_debug);
9805     print_DEBUG('SET_QUERY_TASKS_PARAMETERS return message = ' ||
9806                 l_return_message,
9807                 l_debug);
9808 
9809     IF l_return_status = fnd_api.g_ret_sts_error OR
9810        l_return_status = fnd_api.g_ret_sts_unexp_error THEN
9811       print_DEBUG(' Error in SET_QUERY_TASKS_PARAMETERS ', l_debug);
9812       return;
9813     END IF;
9814 
9815     select replen_completed_flag,
9816            nvl(replen_tolerance, 0),
9817            min_equip_capacity_flag,
9818            rev_trip_stop_flag,
9819            nvl(trip_stop_tolerance, 0),
9820            cons_locator_flag,
9821            nvl(cons_locator_tolerance, 0),
9822            custom_task_plan_flag,
9823            nvl(custom_plan_tolerance, 0)
9824       into l_replenishment_complete,
9825            l_replen_tolerance,
9826            l_fill_capacity,
9827            l_reverse_trip_Stop,
9828            l_trip_tolerance,
9829            l_consol_locator,
9830            l_cons_locator_tolerance,
9831            l_custom_flag,
9832            l_custom_plan_tolerance
9833       from wms_task_Release_vl
9834      where criteria_id = p_task_release_id;
9835 
9836     if l_reverse_trip_Stop = 'Y' or l_consol_locator = 'Y' or
9837        l_replenishment_complete = 'Y' THEN
9838 
9839       -- We will be querying only for UnReleased Tasks
9840 
9841       l_is_queued             := FALSE;
9842       l_is_dispatched         := FALSE;
9843       l_is_active             := FALSE;
9844       l_is_loaded             := FALSE;
9845       l_is_completed          := FALSE;
9846       l_is_pending            := FALSE;
9847       l_include_inbound       := FALSE;
9848       l_include_crossdock     := FALSE;
9849       l_include_manufacturing := FALSE;
9850       l_include_warehousing   := FALSE;
9851 
9852     else
9853       --    l_is_queued             := TRUE;
9854       --  l_is_dispatched         := TRUE;
9855       --  l_is_active             := TRUE;
9856       --  l_is_loaded             := TRUE;
9857       --  l_is_completed          := FALSE;
9858       l_include_inbound       := FALSE;
9859       l_include_crossdock     := FALSE;
9860       l_include_manufacturing := FALSE;
9861       l_include_warehousing   := FALSE;
9862 
9863     end if;
9864 
9865     --get the values related to cartonization from org parameters
9866     BEGIN
9867       SELECT NVL(cartonization_flag, -1), NVL(cartonize_sales_orders, 'Y')
9868         INTO v_cart_value, l_cartonize_sales_orders
9869         FROM mtl_parameters
9870        WHERE organization_id = p_organization_id;
9871     EXCEPTION
9872       WHEN OTHERS THEN
9873         v_cart_value := NULL;
9874     END;
9875 
9876     if v_cart_value = 4 AND l_cartonize_sales_orders = 'Y' THEN
9877       --Always Cartonize for Task Release process
9878       l_cartonization_required := 'Y';
9879     elsif v_cart_value = 5 AND l_cartonize_sales_orders = 'Y' THEN
9880       --Cartonize for Task Release at subinventory level
9881       l_cartonization_required := 'Y';
9882     else
9883 
9884       l_cartonization_required := 'N';
9885 
9886     end if;
9887 
9888     delete from wms_waveplan_tasks_temp;
9889 
9890     wms_waveplan_tasks_pvt.query_tasks(p_add                        => NULL,
9891                                        p_organization_id            => l_organization_id,
9892                                        p_subinventory_code          => l_subinventory,
9893                                        p_locator_id                 => l_locator_id,
9894                                        p_to_subinventory_code       => l_to_subinventory,
9895                                        p_to_locator_id              => l_to_locator_id,
9896                                        p_inventory_item_id          => l_inventory_item_id,
9897                                        p_category_set_id            => l_category_set_id,
9898                                        p_item_category_id           => l_item_category_id,
9899                                        p_person_id                  => l_employee_id,
9900                                        p_person_resource_id         => l_person_resource_id,
9901                                        p_equipment_type_id          => l_equipment_type_id,
9902                                        p_machine_instance           => l_equipment,
9903                                        p_user_task_type_id          => l_user_task_type_id,
9904                                        p_from_task_quantity         => l_from_task_quantity,
9905                                        p_to_task_quantity           => l_to_task_quantity,
9906                                        p_from_task_priority         => l_from_task_priority,
9907                                        p_to_task_priority           => l_to_task_priority,
9908                                        p_from_creation_date         => l_from_creation_date,
9909                                        p_to_creation_date           => l_to_creation_date,
9910                                        p_is_unreleased              => l_is_unreleased,
9911                                        p_is_pending                 => l_is_pending,
9912                                        p_is_queued                  => l_is_queued,
9913                                        p_is_dispatched              => l_is_dispatched,
9914                                        p_is_active                  => l_is_active,
9915                                        p_is_loaded                  => l_is_loaded,
9916                                        p_is_completed               => l_is_completed,
9917                                        p_include_inbound            => l_include_inbound,
9918                                        p_include_outbound           => l_include_outbound,
9919                                        p_include_crossdock          => l_include_crossdock,
9920                                        p_include_manufacturing      => l_include_manufacturing,
9921                                        p_include_warehousing        => l_include_warehousing,
9922                                        p_from_purchase_order        => l_from_purchase_order,
9923                                        p_from_po_header_id          => l_from_po_header_id,
9924                                        p_to_purchase_order          => l_to_purchase_order,
9925                                        p_to_po_header_id            => l_to_po_header_id,
9926                                        p_from_rma                   => l_from_rma,
9927                                        p_from_rma_header_id         => l_from_rma_header_id,
9928                                        p_to_rma                     => l_to_rma,
9929                                        p_to_rma_header_id           => l_to_rma_header_id,
9930                                        p_from_requisition           => l_from_requisition,
9931                                        p_from_requisition_header_id => l_from_requisition_header_id,
9932                                        p_to_requisition             => l_to_requisition,
9933                                        p_to_requisition_header_id   => l_to_requisition_header_id,
9934                                        p_from_shipment_number       => l_from_shipment,
9935                                        p_to_shipment_number         => l_to_shipment,
9936                                        p_from_sales_order_id        => l_from_sales_order_id,
9937                                        p_to_sales_order_id          => l_to_sales_order_id,
9938                                        p_from_pick_slip_number      => l_from_pick_slip,
9939                                        p_to_pick_slip_number        => l_to_pick_slip,
9940                                        p_customer_id                => l_customer_id,
9941                                        p_customer_category          => l_customer_category,
9942                                        p_delivery_id                => l_delivery_id,
9943                                        p_carrier_id                 => l_carrier_id,
9944                                        p_ship_method                => l_ship_method_code,
9945                                        p_trip_id                    => l_trip_id,
9946                                        p_shipment_priority          => l_shipment_priority,
9947                                        p_from_shipment_date         => l_from_shipment_date,
9948                                        p_to_shipment_date           => l_to_shipment_date,
9949                                        p_ship_to_state              => l_ship_to_state,
9950                                        p_ship_to_country            => l_ship_to_country,
9951                                        p_ship_to_postal_code        => l_ship_to_postal_code,
9952                                        p_from_number_of_order_lines => l_from_lines_in_sales_order,
9953                                        p_to_number_of_order_lines   => l_to_lines_in_sales_order,
9954                                        p_manufacturing_type         => l_manufacturing_type,
9955                                        p_from_job                   => l_from_job,
9956                                        p_to_job                     => l_to_job,
9957                                        p_assembly_id                => l_assembly_id,
9958                                        p_from_start_date            => l_from_start_date,
9959                                        p_to_start_date              => l_to_start_date,
9960                                        p_from_line                  => l_from_line,
9961                                        p_to_line                    => l_to_line,
9962                                        p_department_id              => l_department_id,
9963                                        p_include_sales_orders       => l_include_sales_orders,
9964                                        p_include_internal_orders    => l_include_internal_orders,
9965                                        p_include_replenishment      => l_include_replenishment,
9966                                        p_from_replenishment_mo      => l_from_replenishment_mo,
9967                                        p_to_replenishment_mo        => l_to_replenishment_mo,
9968                                        p_include_mo_transfer        => l_include_mo_transfer,
9969                                        p_include_mo_issue           => l_include_mo_issue,
9970                                        p_from_transfer_issue_mo     => l_from_transfer_issue_mo,
9971                                        p_to_transfer_issue_mo       => l_to_transfer_issue_mo,
9972                                        p_include_lpn_putaway        => l_include_lpn_putaway,
9973                                        p_include_staging_move       => l_include_staging_move,
9974                                        p_include_cycle_count        => l_include_cycle_count,
9975                                        p_cycle_count_name           => l_cycle_count_name,
9976                                        x_return_status              => l_return_status,
9977                                        x_msg_data                   => l_msg_data,
9978                                        x_msg_count                  => l_msg_count,
9979                                        x_record_count               => l_record_count,
9980                                        p_query_independent_tasks    => l_query_independent_tasks,
9981                                        p_query_planned_tasks        => l_query_planned_tasks,
9982                                        p_is_pending_plan            => l_is_pending_plan,
9983                                        p_is_inprogress_plan         => l_is_inprogress_plan,
9984                                        p_is_completed_plan          => l_is_completed_plan,
9985                                        p_is_cancelled_plan          => l_is_cancelled_plan,
9986                                        p_is_aborted_plan            => l_is_aborted_plan,
9987                                        p_activity_id                => l_op_plan_activity_id,
9988                                        p_plan_type_id               => l_op_plan_type_id,
9989                                        p_op_plan_id                 => l_op_plan_id,
9990                                        p_wave_header_id             => l_wave_header_id);
9991 
9992     print_DEBUG('WMS_WAVEPLAN_TASKS_PVT.QUERY_TASKS x_return_status => ' ||
9993                 l_return_status,
9994                 l_debug);
9995     print_DEBUG('WMS_WAVEPLAN_TASKS_PVT.QUERY_TASKS x_msg_data => ' ||
9996                 l_msg_data,
9997                 l_debug);
9998     print_DEBUG('WMS_WAVEPLAN_TASKS_PVT.QUERY_TASKS x_msg_count => ' ||
9999                 l_msg_count,
10000                 l_debug);
10001     print_DEBUG('WMS_WAVEPLAN_TASKS_PVT.QUERY_TASKS x_record_count => ' ||
10002                 l_record_count,
10003                 l_debug);
10004 
10005     IF l_return_status = fnd_api.g_ret_sts_error THEN
10006       print_DEBUG(' Error in WMS_WAVEPLAN_TASKS_PVT.QUERY_TASKS ', l_debug);
10007       --return;
10008       RAISE fnd_api.g_exc_error;
10009     END IF;
10010 
10011     open c_mmtt;
10012 
10013     fetch c_mmtt bulk collect
10014       into l_final_mmtt_table;
10015 
10016     close c_mmtt;
10017 
10018     print_DEBUG(' Total Number of Tasks is  ' || l_final_mmtt_table.count,
10019                 l_debug);
10020 
10021     forall m in l_final_mmtt_table.FIRST .. l_final_mmtt_table.LAST
10022       insert into wms_wp_tp_mmtt values (l_final_mmtt_table(m), 'N');
10023 
10024     /*
10025       for l_rec in c_wave_temp loop
10026 
10027         print_debug('Transaction Temp Id is' || l_rec.transaction_temp_id,
10028                     l_debug);
10029         print_debug('Status is' || l_rec.status, l_debug);
10030         print_debug('Task Type is' || l_rec.task_type, l_debug);
10031         print_debug('Order Number is' || l_rec.source_header, l_debug);
10032 
10033       end loop;
10034     */
10035     IF l_custom_flag = 'Y' THEN
10036       print_debug('Custom condition is enabled. Calling wms_wp_custom_apis_pub.task_release_cust ',
10037                   l_debug);
10038       wms_wp_custom_apis_pub.task_release_cust(p_organization_id       => p_organization_id,
10039                                                p_custom_plan_tolerance => l_custom_plan_tolerance,
10040                                                --  p_cartonization_required      => p_cartonization,
10041                                                p_final_mmtt_table => g_mmtt_table, -- Need to make it an in/ out parameter.Ajith?????
10042                                                x_return_status    => l_return_status,
10043                                                x_msg_count        => l_msg_count,
10044                                                x_msg_data         => l_msg_data);
10045 
10046       IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
10047         print_debug('Error returned from task_release_cust in Task_Release_CP ',
10048                     l_debug);
10049         RAISE FND_API.G_EXC_ERROR;
10050       ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
10051         print_debug('Unexpected errror from task_release_cust in Task_Release_CP ',
10052                     l_debug);
10053         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
10054       END IF;
10055 
10056       x_return_status := l_return_status;
10057 
10058     end if;
10059 
10060     --ELSE
10061 
10062     if l_fill_capacity = 'Y' then
10063 
10064       print_debug('Checking Minimum Equipment Fill Capacity', l_debug);
10065 
10066       begin
10067 
10068         -- Looping through the different User Task Types
10069         for l_user_task_type in c_task_type loop
10070 
10071           print_debug('Looping through User Task Type ' ||
10072                       l_user_task_type.user_task_type,
10073                       l_debug);
10074 
10075           -- Need to find out all the tasks for that User task type
10076 			g_mmtt_table.delete; --11870443 change to global g_mmtt_table 11870443
10077           select transaction_temp_id bulk collect
10078             into g_mmtt_table --11870443 change to global g_mmtt_table 11870443
10079             from wms_waveplan_tasks_temp
10080            where user_Task_type = l_user_task_type.user_task_type;
10081 
10082           print_debug('Calling Minimum Equipment Capacity API to get the minimum equipment
10083                 Capacity ',
10084                       l_debug);
10085 
10086           select sum(transaction_quantity)
10087             into l_sum_qty
10088             from wms_waveplan_tasks_temp
10089            where user_Task_type = l_user_task_type.user_task_type;
10090 
10091           print_debug('Sum of Transaction Qty for User task type ' ||
10092                       l_user_task_type.user_task_type || ' is  ' ||
10093                       l_sum_qty,
10094                       l_debug);
10095 
10096           if check_min_equip_Capacity(g_mmtt_table) then --11870443 change to global g_mmtt_table 11870443
10097 
10098             print_debug(' All Tasks in User Task type ' ||
10099                         l_user_task_type.user_task_type ||
10100                         ' has fulfilled the minimum equipment
10101                 Capacity ',
10102                         l_debug);
10103 
10104             forall i in g_mmtt_table.FIRST .. g_mmtt_table.LAST --11870443 change to global g_mmtt_table 11870443
10105               update wms_wp_tp_mmtt
10106                  set indicator_flag = 'F'
10107                where transaction_temp_id = g_mmtt_table(i); --11870443 change to global g_mmtt_table 11870443
10108             --   and indicator_flag <> 'Y';
10109 
10110             --  l_mmtt_table1.DELETE;
10111 
10112           else
10113             print_debug(' All Tasks in User Task type ' ||
10114                         l_user_task_type.user_task_type ||
10115                         ' has not fulfilled the minimum equipment
10116                 Capacity ',
10117                         l_debug);
10118             g_mmtt_table.DELETE; --11870443 change to global g_mmtt_table 11870443
10119           end if;
10120 
10121         end loop;
10122 
10123 
10124       exception
10125         WHEN OTHERS THEN
10126           print_debug('Error in Task Planning Minimum Fill Capacity: ' ||
10127                       SQLCODE || ' : ' || SQLERRM,
10128                       l_debug);
10129 
10130           RAISE fnd_api.g_exc_error;
10131       end;
10132 
10133     end if;
10134 
10135     if l_reverse_trip_Stop = 'Y' then
10136 
10137       if l_fill_capacity = 'Y' then
10138 
10139         /*  forall i in l_mmtt_table.FIRST .. l_mmtt_table.LAST
10140         update wms_wp_tp_mmtt
10141            set indicator_flag = 'Y'
10142          where transaction_temp_id = l_mmtt_table(i); */
10143         g_mmtt_table.DELETE; --11870443 change to global g_mmtt_table 11870443
10144       else
10145 
10146         forall i in l_final_mmtt_table.FIRST .. l_final_mmtt_table.LAST
10147           update wms_wp_tp_mmtt
10148              set indicator_flag = 'F'
10149            where transaction_temp_id = l_final_mmtt_table(i);
10150 
10151       end if;
10152 
10153       print_debug('Checking Reverse Trip Stop Tasks', l_debug);
10154 
10155       begin
10156         j := 0;
10157 
10158         for l_trip in c_get_trip loop
10159           l_trip_id := l_trip.trip_id;
10160           print_debug(' Trip Id is ' || l_trip_id, l_debug);
10161 
10162           v_final_completed_flag := 'Y';
10163 
10164           for l_stop in c_get_trip_Stop loop
10165 
10166             l_Stop_id               := l_stop.stop_id;
10167             v_task_count            := 0;
10168             l_total_completed_count := 0;
10169 
10170             print_debug(' Trip Stop Id is ' || l_stop_id, l_debug);
10171 
10172             for l_task in c_get_mmtt loop
10173               if v_final_completed_flag = 'Y' then
10174                 -- Need to check if the task is completed
10175 
10176                 v_task_count := v_task_count + 1;
10177 
10178                 select count(1)
10179                   into l_completed_task_count
10180                   from mtl_material_transactions
10181                  where transaction_id = l_task.transaction_temp_id;
10182 
10183                 if l_completed_task_count = 1 then
10184                   print_debug(' transaction id ' ||
10185                               l_task.transaction_temp_id ||
10186                               ' is completed',
10187                               l_debug);
10188 
10189                   l_total_completed_count := l_total_completed_count + 1;
10190                   -- So need to check if all the tasks are completed or not
10191                 else
10192                   print_debug(' transaction temp id not completed is ' ||
10193                               l_task.transaction_temp_id,
10194                               l_debug);
10195                   -- Need to find out whether the Tasks belong to the Query
10196                   begin
10197                     select 1
10198                       into l_unreleased_count
10199                       from wms_wp_tp_mmtt
10200                      where indicator_flag = 'F'
10201                        and transaction_temp_id = l_task.transaction_temp_id;
10202 
10203                   exception
10204                     when others then
10205                       print_debug(' transaction temp id not available in Control Board Query is  ' ||
10206                                   l_task.transaction_temp_id,
10207                                   l_debug);
10208 
10209                       l_unreleased_count := 0;
10210                   end;
10211 
10212                   if l_unreleased_count = 1 then
10213                     print_debug(' transaction temp id  in Control Board Query is ' ||
10214                                 l_task.transaction_temp_id,
10215                                 l_debug);
10216                     g_mmtt_table(j) := l_task.transaction_temp_id; --11870443 change to global g_mmtt_table 11870443
10217                     j := j + 1;
10218 
10219                   end if;
10220                 end if;
10221 
10222               end if;
10223             end loop;
10224 
10225             -- FInal Completion Status
10226 
10227             print_debug(' l_total_completed_count is ' ||
10228                         l_total_completed_count,
10229                         l_debug);
10230 
10231             print_debug(' v_task_count is ' || v_task_count, l_debug);
10232 
10233             if v_task_count > 0 then
10234               if l_total_completed_count >=
10235                  ((100 - l_trip_tolerance) / 100) * v_task_count then
10236                 print_debug(' All Tasks for the trip stop are Completed ',
10237                             l_debug);
10238                 v_final_completed_flag := 'Y';
10239               else
10240                 print_debug(' All Tasks for the trip stop are not Completed So not releasing the tasks for the next trip stop ',
10241                             l_debug);
10242                 v_final_completed_flag := 'N';
10243               end if;
10244             end if;
10245           end loop;
10246         end loop;
10247 
10248         forall i in g_mmtt_table.FIRST .. g_mmtt_table.LAST --11870443 change to global g_mmtt_table 11870443
10249           update wms_wp_tp_mmtt
10250              set indicator_flag = 'T'
10251            where transaction_temp_id = g_mmtt_table(i); --11870443 change to global g_mmtt_table 11870443
10252         --  and indicator_flag <> 'Y';
10253         --  l_mmtt_table1.DELETE;
10254 
10255       exception
10256         WHEN OTHERS THEN
10257           print_debug('Error in Task Release Reverse Trip Stop: ' ||
10258                       SQLCODE || ' : ' || SQLERRM,
10259                       l_debug);
10260           RAISE fnd_api.g_exc_error;
10261       end;
10262 
10263     end if;
10264 
10265     -- Checking Consolidation Locator group by delivery
10266     -- We release tasks only when the tasks pertaining to the prior delivery is packed.
10267     if l_consol_locator = 'Y' then
10268 
10269       begin
10270 
10271         if (l_fill_capacity = 'Y' and l_reverse_trip_Stop = 'Y') or
10272            l_reverse_trip_Stop = 'Y' then
10273 
10274           g_mmtt_table.delete; --11870443 change to global g_mmtt_table 11870443
10275 
10276         elsif (l_fill_capacity = 'Y' and l_reverse_trip_Stop <> 'Y') then
10277 
10278           forall i in g_mmtt_table.FIRST .. g_mmtt_table.LAST --11870443 change to global g_mmtt_table 11870443
10279             update wms_wp_tp_mmtt
10280                set indicator_flag = 'T'
10281              where transaction_temp_id = g_mmtt_table(i); --11870443 change to global g_mmtt_table 11870443
10282 
10283         else
10284 
10285           forall i in l_final_mmtt_table.FIRST .. l_final_mmtt_table.LAST
10286             update wms_wp_tp_mmtt
10287                set indicator_flag = 'T'
10288              where transaction_temp_id = l_final_mmtt_table(i);
10289 
10290         end if;
10291 
10292         print_debug('Checking Consolidation Locator Tasks', l_debug);
10293 
10294         for l_consol_locator in c_consol_locator loop
10295 
10296           p_consol_locator_id := l_consol_locator.to_locator_id;
10297 
10298           print_debug(' Consolidation Locator   is ' ||
10299                       l_consol_locator.to_locator,
10300                       l_debug);
10301 
10302           open c_delivery;
10303           loop
10304             fetch c_delivery
10305               into l_dummy_delivery;
10306             exit;
10307           end loop;
10308 
10309           l_delivery_count := c_delivery%ROWCOUNT;
10310 
10311           close c_delivery;
10312 
10313           if l_delivery_count = 0 then
10314 
10315             -- Then we need to release all the tasks for the first delivery.
10316 
10317             if get_tasks_exist(p_consol_locator_id) then
10318               l_release_delivery := 'Y';
10319             end if;
10320 
10321           else
10322 
10323             for l_delivery in c_delivery loop
10324 
10325               p_delivery_id := l_delivery.delivery_id;
10326 
10327               print_debug(' Processing Delivery  in Consolidation Locator ' ||
10328                           p_delivery_id,
10329                           l_debug);
10330 
10331               open c_wdd_delivery;
10332 
10333               fetch c_wdd_delivery bulk collect
10334                 into l_wdd_table2;
10335 
10336               close c_wdd_delivery;
10337 
10338               ---- Getting all WDDS in Replenishment requested Status
10339 
10340               open c_wdd_staged;
10341 
10342               fetch c_wdd_staged bulk collect
10343                 into l_wdd_table3;
10344 
10345               close c_wdd_staged;
10346 
10347               l_wdd_count := l_wdd_table2.count; ---> set1
10348 
10349               l_wdd_staged_count := l_wdd_table3.count; ---> set2
10350 
10351               print_debug(' Count of Total Wdds in the delivery is  ' ||
10352                           l_wdd_count,
10353                           l_debug);
10354 
10355               print_debug(' Count of Total Staged  WDDs  in the  delivery is  ' ||
10356                           l_wdd_staged_count,
10357                           l_debug);
10358 
10359               print_debug('Consolidation Locator Tolerance is ' ||
10360                           l_cons_locator_tolerance,
10361                           l_debug);
10362 
10363               if l_wdd_count > 0 then
10364                 if ((l_wdd_staged_count * 100) / (l_wdd_count)) >=
10365                    (100 - l_cons_locator_tolerance) then
10366 
10367                   print_debug(100 - l_cons_locator_tolerance ||
10368                               ' % of wdds in the delivery are Staged ',
10369                               l_debug);
10370 
10371                   if get_tasks_exist(p_consol_locator_id, p_delivery_id) then
10372                     l_release_delivery := 'Y';
10373                     exit; --Exiting the loop as no need to process the next delivery.
10374                   end if;
10375                   --   l_release_delivery := 'Y'; -- Releasing next delivery
10376 
10377                 else
10378 
10379                   print_debug(100 - l_cons_locator_tolerance ||
10380                               ' % of wdds in the delivery are not Staged . So not processing the next delivery',
10381                               l_debug);
10382 
10383                   l_release_delivery := 'N';
10384 
10385                 end if;
10386               end if;
10387 
10388               l_wdd_count        := 0;
10389               l_wdd_staged_count := 0;
10390 
10391             end loop;
10392 
10393           end if;
10394 
10395           if l_release_delivery = 'Y' then
10396 
10397             print_debug('Releasing tasks for the First Delivery ', l_debug);
10398 
10399             for l_delivery1 in c_delivery_mmtt loop
10400 
10401               p_delivery_mmtt_id := l_delivery1.delivery_id;
10402 
10403               print_debug(' delivery id whose tasks are  to be released is ' ||
10404                           p_delivery_mmtt_id,
10405                           l_debug);
10406 
10407               open c_get_mmtt_delivery(p_delivery_mmtt_id);
10408 
10409               fetch c_get_mmtt_delivery bulk collect
10410                 into l_mmtt_table2;
10411 
10412               close c_get_mmtt_delivery;
10413 
10414               forall i in l_mmtt_table2.FIRST .. l_mmtt_table2.LAST
10415                 update wms_wp_tp_mmtt
10416                    set indicator_flag = 'C'
10417                  where transaction_temp_id = l_mmtt_table2(i);
10418 
10419               --    l_mmtt_table.delete;
10420 
10421               exit; -- This should run only for one delivery
10422 
10423             end loop;
10424 
10425             l_table_count := g_mmtt_table.COUNT; --11870443 change to global g_mmtt_table 11870443
10426 
10427             print_debug(' l_table_count is ' || l_table_count, l_debug);
10428 
10429             for i in l_mmtt_table2.FIRST .. l_mmtt_table2.LAST loop
10430 
10431               g_mmtt_table(i + l_table_count) := l_mmtt_table2(i); --11870443 change to global g_mmtt_table 11870443
10432 
10433             end loop;
10434 
10435             l_mmtt_table2.delete;
10436 
10437           end if;
10438 
10439         end loop;
10440 
10441       exception
10442         WHEN OTHERS THEN
10443           print_debug('Error in Task Release Consolidation Locator : ' ||
10444                       SQLCODE || ' : ' || SQLERRM,
10445                       l_debug);
10446           RAISE fnd_api.g_exc_error;
10447       end;
10448 
10449     end if;
10450 
10451     IF l_custom_flag = 'Y' THEN
10452 
10453       print_DEBUG('Before pre cartonization starts after customization.',
10454                   l_debug);
10455     else
10456       print_DEBUG('Updating the g_mmtt_table from the global temp table.',
10457                   l_debug);
10458 
10459       if (l_fill_capacity = 'Y' or l_reverse_trip_Stop = 'Y' or
10460          l_consol_locator = 'Y') then
10461 
10462         forall i in g_mmtt_table.FIRST .. g_mmtt_table.LAST --11870443 change to global g_mmtt_table 11870443
10463           update wms_wp_tp_mmtt
10464              set indicator_flag = 'C'
10465            where transaction_temp_id = g_mmtt_table(i); --11870443 change to global g_mmtt_table 11870443
10466 
10467       else
10468 
10469         forall i in l_final_mmtt_table.FIRST .. l_final_mmtt_table.LAST
10470           update wms_wp_tp_mmtt
10471              set indicator_flag = 'C'
10472            where transaction_temp_id = l_final_mmtt_table(i);
10473 
10474         select transaction_temp_id bulk collect
10475           into g_mmtt_table --11870443 change to global g_mmtt_table 11870443
10476           from wms_wp_tp_mmtt
10477          where indicator_flag = 'C';
10478 
10479       end if;
10480 
10481       --   l_mmtt_table.delete;
10482 
10483     end if;
10484 
10485     if l_cartonization_required = 'Y' or l_replenishment_complete = 'Y' then
10486 
10487       --<<Pre cartonize code here >>
10488 
10489       print_DEBUG('Pre Cartonization code starts here .', l_debug);
10490 
10491       if g_mmtt_table.count > 0 then ---11870443 change to global g_mmtt_table 11870443
10492 
10493 		print_DEBUG('g_mmtt_table.count '||g_mmtt_table.count, l_debug);
10494         open c_carton_lines;
10495 
10496         fetch c_carton_lines bulk collect
10497           into p_line_rows, p_move_order_line_tbl;
10498 
10499         close c_carton_lines;
10500 
10501         print_DEBUG('Before Update mtrl carton grouping id to null .',
10502                     l_debug);
10503 
10504         forall l2 in 1 .. p_move_order_line_tbl.count
10505           update mtl_txn_request_lines
10506              set carton_grouping_id = null
10507            where line_id = p_move_order_line_tbl(l2);
10508 
10509         print_DEBUG('After Update mtrl .', l_debug);
10510         FOR i IN 1 .. p_line_rows.count LOOP
10511           print_debug('**** PROCESSING DELIVERY DETAIL ID ' ||
10512                       P_LINE_ROWS(I) || ' ****',
10513                       l_debug);
10514           l_attr_tab(i).entity_id := p_line_rows(i);
10515           l_attr_tab(i).entity_type := 'DELIVERY_DETAIL';
10516         END LOOP;
10517 
10518         l_action_rec.action               := 'MATCH_GROUPS';
10519         l_action_rec.group_by_header_flag := 'N'; -- Need to confirm with Satish????????
10520 
10521         --    l_action_rec.check_single_grp := 'Y';
10522 
10523         print_debug('Calling Find Matching Groups ', l_debug);
10524         /*
10525         wsh_Delivery_autocreate.Find_Matching_Groups(p_attr_tab IN OUT NOCOPY grp_attr_tab_type,
10526                          p_action_rec IN action_rec_type,
10527                          p_target_rec IN grp_attr_rec_type,
10528                          p_group_tab IN OUT NOCOPY grp_attr_tab_type,
10529                          x_matched_entities OUT NOCOPY wsh_util_core.id_tab_type,
10530                          x_out_rec out NOCOPY out_rec_type,
10531                          x_return_status out NOCOPY varchar2); */
10532         -- l_group_info.organization_id := p_organization_id;
10533         wsh_Delivery_autocreate.Find_Matching_Groups(p_attr_tab         => l_attr_tab,
10534                                                      p_action_rec       => l_action_rec,
10535                                                      p_target_rec       => l_target_rec,
10536                                                      p_group_tab        => l_group_info,
10537                                                      x_matched_entities => l_matched_entities,
10538                                                      x_out_rec          => l_out_rec,
10539                                                      x_return_status    => x_return_status);
10540 
10541         print_debug('Return status from  Find Matching Groups ' ||
10542                     x_return_status,
10543                     l_debug);
10544 
10545         /*  l_group_match_seq_tbl.delete;
10546 
10547           --{
10548           for i in 1 .. l_attr_tab.count loop
10549 
10550             l_match_found := FALSE;
10551             --  print_debug('l_match_found '||l_match_found,l_debug);
10552 
10553             IF l_group_match_seq_tbl.count > 0 THEN
10554               --{
10555               FOR k in l_group_match_seq_tbl.FIRST .. l_group_match_seq_tbl.LAST LOOP
10556                 --{
10557 
10558                 print_debug(' l_attr_tab(i).group_id ' || l_attr_tab(i)
10559                             .group_id,
10560                             l_debug);
10561 
10562                 IF l_attr_tab(i).group_id = l_group_match_seq_tbl(k)
10563                 .match_group_id THEN
10564                   --{
10565                   l_group_match_seq_tbl(i).delivery_group_id := l_group_match_seq_tbl(k)
10566                                                                .delivery_group_id;
10567                   print_debug('  l_group_match_seq_tbl(i).delivery_group_id  ' ||
10568                               l_group_match_seq_tbl(i).delivery_group_id,
10569                               l_debug);
10570 
10571                   l_match_found := TRUE;
10572                   EXIT;
10573                   --}
10574                 End IF;
10575                 --}
10576               END LOOP;
10577               --}
10578             END IF;
10579 
10580             IF NOT l_match_found THEN
10581               --{
10582 
10583               l_group_match_seq_tbl(i).match_group_id := l_attr_tab(i)
10584                                                         .group_id;
10585 
10586               print_debug(' 123 l_attr_tab(i).group_id ' || l_attr_tab(i)
10587                           .group_id,
10588                           l_debug);
10589               print_debug('123 l_group_match_seq_tbl(k)
10590               .match_group_id ' ||
10591                           l_group_match_seq_tbl(i).match_group_id,
10592                           l_debug);
10593 
10594               select WSH_DELIVERY_GROUP_S.nextval
10595                 into l_group_match_seq_tbl(i) .delivery_group_id
10596                 from dual;
10597               --}
10598             End IF;
10599 
10600             --   l_group_match_seq_tbl(i) .delivery_group_id := l_temp;
10601             print_debug('CARTON GROUPING ID : ' || l_group_match_seq_tbl(i)
10602                         .delivery_group_id,
10603                         l_debug);
10604 
10605             p_carton_grouping_tbl(i) := l_group_match_seq_tbl(i)
10606                                        .delivery_group_id;
10607             --}
10608 
10609           end loop;
10610         */
10611 
10612         for i in 1 .. l_attr_tab.count loop
10613 
10614           l_match_found := FALSE;
10615 
10616           IF l_group_match_seq_tbl.count > 0 THEN
10617             --{
10618             FOR k in l_group_match_seq_tbl.FIRST .. l_group_match_seq_tbl.LAST LOOP
10619               --{
10620               IF l_attr_tab(i).group_id = l_group_match_seq_tbl(k)
10621               .match_group_id THEN
10622                 --{
10623                 l_group_match_seq_tbl(i).delivery_group_id := l_group_match_seq_tbl(k)
10624                                                              .delivery_group_id;
10625                 l_match_found := TRUE;
10626                 EXIT;
10627                 --}
10628               End IF;
10629               --}
10630             END LOOP;
10631             --}
10632           END IF;
10633 
10634           IF NOT l_match_found THEN
10635             --{
10636             l_group_match_seq_tbl(i).match_group_id := l_attr_tab(i)
10637                                                       .group_id;
10638             select WSH_DELIVERY_GROUP_S.nextval
10639               into l_group_match_seq_tbl(i) .delivery_group_id
10640               from dual;
10641             --}
10642           End IF;
10643 
10644           print_debug('CARTON GROUPING ID : ' || l_group_match_seq_tbl(i)
10645                       .delivery_group_id,
10646                       l_debug);
10647 
10648           p_carton_grouping_tbl(i) := l_group_match_seq_tbl(i)
10649                                      .delivery_group_id;
10650 
10651         end loop;
10652 
10653         print_debug('Calling the  inv_move_order_pub.stamp_cart_id  : ',
10654                     l_debug);
10655 
10656         inv_move_order_pub.stamp_cart_id(p_validation_level    => 1,
10657                                          p_carton_grouping_tbl => p_carton_grouping_tbl,
10658                                          p_move_order_line_tbl => p_move_order_line_tbl);
10659 
10660         print_debug(' After calling  inv_move_order_pub.stamp_cart_id -- > Now we Release the Tasks   ',
10661                     l_debug);
10662 
10663         print_debug('Pre cartonization code ends here ---->   ', l_debug);
10664 
10665         -- end if;
10666 
10667         if l_replenishment_complete = 'Y' then
10668 
10669           print_debug('Inside Replenishment Completed Check ---->   ',
10670                       l_debug);
10671 
10672           -- Process the lines
10673           begin
10674             print_debug('Checking Replenishment Complete Tasks', l_debug);
10675 
10676             g_mmtt_table.delete; --11870443 change to global g_mmtt_table 11870443
10677             for l_carton in c_carton_grouping_id loop
10678 
10679               p_carton_grouping_id := l_carton.carton_grouping_id;
10680 
10681               --  p_batch_id := l_carton.batch_id;
10682 
10683               print_debug(' carton_grouping_id is ' ||
10684                           p_carton_grouping_id,
10685                           l_debug);
10686 
10687               ---- Getting all WDDS in Replenishment Completed or Released to Warehouse Status
10688 
10689               open c_rc_wdds;
10690 
10691               fetch c_rc_wdds bulk collect
10692                 into l_wdd_table;
10693 
10694               close c_rc_wdds;
10695 
10696               ---- Getting all WDDS in Replenishment requested Status
10697 
10698               open c_rr_wdds;
10699 
10700               fetch c_rr_wdds bulk collect
10701                 into l_wdd_table1;
10702 
10703               close c_rr_wdds;
10704 
10705               no_of_tasks := l_wdd_table.count; ---> set1
10706 
10707               l_repl_tasks := l_wdd_table1.count; ---> set2
10708 
10709               print_debug(' Count of Replenishment Completed Wdds is  ' ||
10710                           no_of_tasks,
10711                           l_debug);
10712 
10713               print_debug(' Count of Replenishment Requested WDDs is  ' ||
10714                           l_repl_tasks,
10715                           l_debug);
10716 
10717               if no_of_tasks > 0 then
10718                 if ((no_of_tasks * 100) / (no_of_tasks + l_repl_tasks)) >=
10719                    (100 - l_replen_tolerance) then
10720 
10721                   print_debug(100 - l_replen_tolerance ||
10722                               ' % of wdds are Replenishment Completed or Released to Warehouse ',
10723                               l_debug);
10724 
10725                   open c_rc_tasks;
10726 
10727                   fetch c_rc_tasks bulk collect
10728                     into g_mmtt_table;
10729 
10730                   close c_rc_tasks;
10731 
10732                   forall i in g_mmtt_table.FIRST .. g_mmtt_table.LAST --11870443 change to global g_mmtt_table 11870443
10733                     update wms_wp_tp_mmtt
10734                        set indicator_flag = 'L'
10735                      where transaction_temp_id = g_mmtt_table(i); --11870443 change to global g_mmtt_table 11870443
10736 
10737                   g_mmtt_table.delete; --11870443 change to global g_mmtt_table 11870443
10738 
10739                   print_debug(' Releasing the Tasks for the Carton Grouping id ' ||
10740                               p_carton_grouping_id,
10741                               l_debug);
10742 
10743                 else
10744                   print_debug(' Not Releasing the Tasks for the Carton Grouping id ' ||
10745                               p_carton_grouping_id || ' as ' ||
10746                               to_char(100 - l_replen_tolerance) ||
10747                               ' % of wdds are not Replenishment Completed or Released to Warehouse ',
10748                               l_debug);
10749 
10750                 end if;
10751               end if;
10752               no_of_tasks  := 0;
10753               l_repl_tasks := 0;
10754             end loop;
10755 
10756             select transaction_temp_id bulk collect
10757               into g_mmtt_table --11870443 change to global g_mmtt_table 11870443
10758               from wms_wp_tp_mmtt
10759              where indicator_flag = 'L';
10760 
10761           exception
10762             WHEN OTHERS THEN
10763               print_debug('Error in Task Planning Replenishment Complete part: ' ||
10764                           SQLCODE || ' : ' || SQLERRM,
10765                           l_debug);
10766               RAISE fnd_api.g_exc_error;
10767 
10768           end;
10769 
10770         end if;
10771 
10772         if l_cartonization_required = 'Y' then
10773 
10774           if p_move_order_line_tbl.count > 0 then
10775 
10776             print_debug('Inside Call to Cartonize ---->   ', l_debug);
10777 
10778             print_debug('Assigning Values to Header Record ---->   ',
10779                         l_debug);
10780 
10781             l_trohdr_rec.created_by          := fnd_global.user_id;
10782             l_trohdr_rec.creation_date       := sysdate;
10783             l_trohdr_rec.header_status       := INV_Globals.g_to_status_preapproved;
10784             l_trohdr_rec.last_updated_by     := fnd_global.user_id;
10785             l_trohdr_rec.last_update_date    := sysdate;
10786             l_trohdr_rec.last_update_login   := fnd_global.user_id;
10787             l_trohdr_rec.organization_id     := p_organization_id; -- assigned inside the loop
10788             l_trohdr_rec.status_date         := sysdate;
10789             l_trohdr_rec.move_order_type     := INV_GLOBALS.G_MOVE_ORDER_PICK_WAVE;
10790             l_trohdr_rec.transaction_type_id := INV_GLOBALS.G_TYPE_TRANSFER_ORDER_STGXFR;
10791             l_trohdr_rec.operation           := INV_GLOBALS.G_OPR_CREATE;
10792             l_trohdr_rec.db_flag             := FND_API.G_TRUE;
10793 
10794             print_debug('CALLING INV_Move_Order_PUB.Create_Move_Order_Header',
10795                         l_debug);
10796 
10797             INV_Move_Order_PUB.Create_Move_Order_Header(p_api_version_number => 1.0,
10798                                                         p_init_msg_list      => FND_API.G_FALSE,
10799                                                         p_return_values      => FND_API.G_TRUE,
10800                                                         p_commit             => l_commit,
10801                                                         x_return_status      => x_return_status,
10802                                                         x_msg_count          => l_msg_count,
10803                                                         x_msg_data           => l_msg_data,
10804                                                         p_trohdr_rec         => l_trohdr_rec,
10805                                                         p_trohdr_val_rec     => l_trohdr_val_rec,
10806                                                         x_trohdr_rec         => l_x_trohdr_rec,
10807                                                         x_trohdr_val_rec     => l_x_trohdr_val_rec,
10808                                                         p_validation_flag    => inv_move_order_pub.g_validation_yes);
10809 
10810             IF l_return_status = FND_API.G_RET_STS_ERROR THEN
10811 
10812               print_debug('Creating MO Header failed with unexpected error returning message: ' ||
10813                           l_msg_data,
10814                           l_debug);
10815 
10816               RAISE fnd_api.g_exc_unexpected_error;
10817               -- If cant create a common MOH, do no repl stuff
10818             ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
10819 
10820               print_debug('Creating MO Header failed with expected error returning message: ' ||
10821                           l_msg_data,
10822                           l_debug);
10823               --    RAISE fnd_api.g_exc_error;
10824               -- If cant create a common MOH, do not cartonize
10825             ELSE
10826 
10827               print_debug('Creating MO Header returned success with MO Header Id: ' ||
10828                           l_x_trohdr_rec.header_id,
10829                           l_debug);
10830 
10831             END IF;
10832 
10833             print_debug('Before Stamping the move order Header Ids for Move order Lines: ',
10834                         l_debug);
10835 
10836             -- Calling API to create Move Order Header API
10837 
10838             -- Stamping Move Order Header Id for all the Lines
10839 
10840             for i2 in p_move_order_line_tbl.FIRST .. p_move_order_line_tbl.LAST loop
10841 
10842               update mtl_txn_request_lines
10843                  set header_id   = l_x_trohdr_rec.header_id,
10844                      line_number = line_count
10845                where line_id = p_move_order_line_tbl(i2);
10846               line_count := line_count + 1;
10847             end loop;
10848 
10849             forall i3 in p_move_order_line_tbl.FIRST .. p_move_order_line_tbl.LAST
10850               update mtl_material_transactions_temp
10851                  set move_order_header_id = l_x_trohdr_rec.header_id
10852                where move_order_line_id = p_move_order_line_tbl(i3);
10853 
10854             /*
10855                loop
10856                 print_debug(' Move Order Line Id is   ' ||
10857                             p_move_order_line_tbl(i2),
10858                             l_debug);
10859 
10860                 l_move_order_line_id := l_move_order_line_id ||
10861                                         to_char(p_move_order_line_tbl(i2)) || ',';
10862               end loop;
10863 
10864               l_move_order_line_id := l_move_order_line_id || '0'; -- Just to make sure no extra comma is present in the query
10865 
10866               print_debug(' Move Order Line Id char  is   ' ||
10867                           l_move_order_line_id,
10868                           l_debug);
10869 
10870               open c_move_order_header;
10871 
10872               fetch c_move_order_header bulk collect
10873                 into p_move_order_hdr_tbl;
10874 
10875               close c_move_order_header;
10876             */
10877           end if;
10878 
10879           /*
10880           select HEADER_ID
10881             into p_move_order_hdr_tbl(i2)
10882             from mtl_txn_request_lines
10883            where line_id = p_move_order_line_tbl(i2); */
10884 
10885           -- end loop;
10886 
10887           /* if p_move_order_hdr_tbl.count > 0 then
10888           for i3 in p_move_order_hdr_tbl.FIRST .. p_move_order_hdr_tbl.LAST loop
10889 
10890             print_debug(' Move Order header  Id is   ' ||
10891                         p_move_order_hdr_tbl(i3),
10892                         l_debug);
10893 
10894             print_debug(' Calling Cartonize API to cartonize the move order headers  ',
10895                         l_debug);
10896                         */
10897 
10898           wms_postalloc_pvt.cartonize(p_org_id               => p_organization_id,
10899                                       p_move_order_header_id => l_x_trohdr_rec.header_id,
10900                                       p_caller               => 'TRP',
10901                                       x_return_status        => x_return_status);
10902 
10903           print_DEBUG('Return Status from Cartonize API => ' ||
10904                       x_return_status,
10905                       l_debug);
10906           --   end loop;
10907 
10908         end if;
10909 
10910       end if;
10911 
10912     end if;
10913     --END IF;
10914 
10915     if g_mmtt_table.count > 0 then --11870443 change to global g_mmtt_table 11870443
10916       for l1 in g_mmtt_table.FIRST .. g_mmtt_table.LAST loop
10917 
10918         print_DEBUG(' Transaction temp ids selected for Releasing .' ||
10919                     g_mmtt_table(l1),
10920                     l_debug);
10921 
10922       end loop;
10923 
10924     end if;
10925 
10926     forall i IN g_mmtt_table.FIRST .. g_mmtt_table.LAST --11870443 change to global g_mmtt_table 11870443
10927       UPDATE mtl_material_transactions_temp
10928          SET wms_task_status = 1
10929        WHERE transaction_temp_id = g_mmtt_table(i) --11870443 change to global g_mmtt_table 11870443
10930          and wms_task_status = 8;
10931 
10932     commit;
10933 
10934     -- Clearing the tables.
10935     l_field_name_table.delete;
10936     l_field_value_table.delete;
10937     l_query_type_table.delete;
10938     g_mmtt_table.delete;
10939 
10940     print_DEBUG('Cleared pl/sql tables l_query_type_table, l_field_name_table and l_field_value_table.',
10941                 l_debug);
10942 
10943   exception
10944     WHEN OTHERS THEN
10945       print_debug('Error in Task Release CP: ' || SQLCODE || ' : ' ||
10946                   SQLERRM,
10947                   l_debug);
10948 
10949       RAISE fnd_api.g_exc_unexpected_error;
10950   end Task_Release_CP;
10951 
10952   PROCEDURE SET_QUERY_TASKS_PARAMETERS(p_field_name_table      IN wms_wave_planning_pvt.field_name_table_type,
10953                                        p_field_value_table     IN wms_wave_planning_pvt.field_value_table_type,
10954                                        p_organization_id_table IN wms_wave_planning_pvt.organization_id_table_type,
10955                                        p_query_type_table      IN wms_wave_planning_pvt.query_type_table_type,
10956                                        x_return_status         OUT NOCOPY VARCHAR2,
10957                                        x_return_message        OUT NOCOPY VARCHAR2) IS
10958     i       number;
10959     l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
10960   BEGIN
10961     print_DEBUG('Inside SET_QUERY_TASKS_PARAMETERS', l_debug);
10962 
10963     print_DEBUG('p_field_name_table.count ' || p_field_name_table.count,
10964                 l_debug);
10965 
10966     IF p_field_name_table.count <> 0 THEN
10967       FOR i in p_field_name_table.first .. p_field_name_table.last LOOP
10968         IF (p_field_name_table(i) = 'FIND_TASKS.UNRELEASED' AND
10969            p_field_value_table(i) = 'Y') THEN
10970           l_is_unreleased := TRUE;
10971         END IF;
10972 
10973         IF (p_field_name_table(i) = 'FIND_TASKS.PENDING' AND
10974            p_field_value_table(i) = 'Y') THEN
10975           l_is_pending := TRUE;
10976         END IF;
10977 
10978         IF (p_field_name_table(i) = 'FIND_TASKS.QUEUED' AND
10979            p_field_value_table(i) = 'Y') THEN
10980           l_is_queued := TRUE;
10981         END IF;
10982 
10983         IF (p_field_name_table(i) = 'FIND_TASKS.DISPATCHED' AND
10984            p_field_value_table(i) = 'Y') THEN
10985           l_is_dispatched := TRUE;
10986         END IF;
10987 
10988         IF (p_field_name_table(i) = 'FIND_TASKS.ACTIVE' AND
10989            p_field_value_table(i) = 'Y') THEN
10990           l_is_active := TRUE;
10991         END IF;
10992 
10993         IF (p_field_name_table(i) = 'FIND_TASKS.LOADED' AND
10994            p_field_value_table(i) = 'Y') THEN
10995           l_is_loaded := TRUE;
10996         END IF;
10997 
10998         IF (p_field_name_table(i) = 'FIND_TASKS.COMPLETED' AND
10999            p_field_value_table(i) = 'Y') THEN
11000           l_is_completed := TRUE;
11001         END IF;
11002 
11003         IF (p_field_name_table(i) = 'FIND_TASKS.INBOUND' AND
11004            p_field_value_table(i) = 'Y') THEN
11005           l_include_inbound := TRUE;
11006         END IF;
11007 
11008         IF (p_field_name_table(i) = 'FIND_TASKS.OUTBOUND' AND
11009            p_field_value_table(i) = 'Y') THEN
11010           l_include_outbound := TRUE;
11011         END IF;
11012 
11013         IF (p_field_name_table(i) = 'FIND_TASKS.CROSSDOCK' AND
11014            p_field_value_table(i) = 'Y') THEN
11015           l_include_crossdock := TRUE;
11016         END IF;
11017 
11018         IF (p_field_name_table(i) = 'FIND_TASKS.MANUFACTURING' AND
11019            p_field_value_table(i) = 'Y') THEN
11020           l_include_manufacturing := TRUE;
11021         END IF;
11022 
11023         IF (p_field_name_table(i) = 'FIND_TASKS.WAREHOUSING' AND
11024            p_field_value_table(i) = 'Y') THEN
11025           l_include_warehousing := TRUE;
11026         END IF;
11027 
11028         IF (p_field_name_table(i) = 'FIND_TASKS.ORDER_TYPE' AND
11029            p_field_value_table(i) = 'S') THEN
11030           l_include_sales_orders := TRUE;
11031         ELSIF (p_field_name_table(i) = 'FIND_TASKS.ORDER_TYPE' AND
11032               p_field_value_table(i) = 'I') THEN
11033           l_include_internal_orders := TRUE;
11034         ELSIF (p_field_name_table(i) = 'FIND_TASKS.ORDER_TYPE' AND
11035               p_field_value_table(i) = 'B') THEN
11036           l_include_sales_orders    := TRUE;
11037           l_include_internal_orders := TRUE;
11038         END IF;
11039 
11040         IF (p_field_name_table(i) = 'FIND_TASKS.REPLENISHMENT_TASKS' AND
11041            p_field_value_table(i) = 'Y') THEN
11042           l_include_replenishment := TRUE;
11043         END IF;
11044 
11045         IF (p_field_name_table(i) = 'FIND_TASKS.MO_TRANSFER_TASKS' AND
11046            p_field_value_table(i) = 'Y') THEN
11047           l_include_mo_transfer := TRUE;
11048         END IF;
11049 
11050         IF (p_field_name_table(i) = 'FIND_TASKS.MO_ISSUE_TASKS' AND
11051            p_field_value_table(i) = 'Y') THEN
11052           l_include_mo_issue := TRUE;
11053         END IF;
11054 
11055         IF (p_field_name_table(i) = 'FIND_TASKS.LPN_PUTAWAY_TASKS' AND
11056            p_field_value_table(i) = 'Y') THEN
11057           l_include_lpn_putaway := TRUE;
11058         END IF;
11059 
11060         IF (p_field_name_table(i) = 'FIND_TASKS.STAGING_MOVE' AND
11061            p_field_value_table(i) = 'Y') THEN
11062           l_include_staging_move := TRUE;
11063         END IF;
11064 
11065         IF (p_field_name_table(i) = 'FIND_TASKS.CYCLE_COUNT_TASKS' AND
11066            p_field_value_table(i) = 'Y') THEN
11067           l_include_cycle_count := TRUE;
11068         END IF;
11069 
11070         IF (p_field_name_table(i) = 'FIND_TASKS.PLAN_PENDING' AND
11071            p_field_value_table(i) = 'Y') THEN
11072           l_is_pending_plan := TRUE;
11073         END IF;
11074 
11075         IF (p_field_name_table(i) = 'FIND_TASKS.PLAN_IN_PROGRESS' AND
11076            p_field_value_table(i) = 'Y') THEN
11077           l_is_inprogress_plan := TRUE;
11078         END IF;
11079 
11080         IF (p_field_name_table(i) = 'FIND_TASKS.PLAN_COMPLETED' AND
11081            p_field_value_table(i) = 'Y') THEN
11082           l_is_completed_plan := TRUE;
11083         END IF;
11084 
11085         IF (p_field_name_table(i) = 'FIND_TASKS.PLAN_CANCELLED' AND
11086            p_field_value_table(i) = 'Y') THEN
11087           l_is_cancelled_plan := TRUE;
11088         END IF;
11089 
11090         IF (p_field_name_table(i) = 'FIND_TASKS.PLAN_ABORTED' AND
11091            p_field_value_table(i) = 'Y') THEN
11092           l_is_aborted_plan := TRUE;
11093         END IF;
11094 
11095         IF (p_field_name_table(i) = 'FIND_TASKS.PLANNED_TASKS' AND
11096            p_field_value_table(i) = 'Y') THEN
11097           l_query_planned_tasks := TRUE;
11098         END IF;
11099 
11100         IF (p_field_name_table(i) = 'FIND_TASKS.INDEPENDENT_TASKS' AND
11101            p_field_value_table(i) = 'Y') THEN
11102           l_query_independent_tasks := TRUE;
11103         END IF;
11104 
11105         IF p_field_name_table(i) = 'FIND_TASKS.SUBINVENTORY' THEN
11106           l_subinventory := p_field_value_table(i);
11107         ELSIF p_field_name_table(i) = 'FIND_TASKS.LOCATOR_ID' THEN
11108           l_locator_id := p_field_value_table(i);
11109         ELSIF p_field_name_table(i) = 'FIND_TASKS.TO_SUBINVENTORY' THEN
11110           l_to_subinventory := p_field_value_table(i);
11111         ELSIF p_field_name_table(i) = 'FIND_TASKS.TO_LOCATOR_ID' THEN
11112           l_to_locator_id := p_field_value_table(i);
11113         ELSIF p_field_name_table(i) = 'FIND_TASKS.INVENTORY_ITEM_ID' THEN
11114           l_inventory_item_id := p_field_value_table(i);
11115         ELSIF p_field_name_table(i) = 'FIND_TASKS.CATEGORY_SET_ID' THEN
11116           l_category_set_id := p_field_value_table(i);
11117         ELSIF p_field_name_table(i) = 'FIND_TASKS.ITEM_CATEGORY_ID' THEN
11118           l_item_category_id := p_field_value_table(i);
11119         ELSIF p_field_name_table(i) = 'FIND_TASKS.EMPLOYEE_ID' THEN
11120           l_employee_id := p_field_value_table(i);
11121         ELSIF p_field_name_table(i) = 'FIND_TASKS.PERSON_RESOURCE_ID' THEN
11122           l_person_resource_id := p_field_value_table(i);
11123         ELSIF p_field_name_table(i) = 'FIND_TASKS.EQUIPMENT_TYPE_ID' THEN
11124           l_equipment_type_id := p_field_value_table(i);
11125         ELSIF p_field_name_table(i) = 'FIND_TASKS.EQUIPMENT' THEN
11126           l_equipment := p_field_value_table(i);
11127         ELSIF p_field_name_table(i) = 'FIND_TASKS.USER_TASK_TYPE_ID' THEN
11128           l_user_task_type_id := p_field_value_table(i);
11129         ELSIF p_field_name_table(i) = 'FIND_TASKS.FROM_TASK_QUANTITY' THEN
11130           l_from_task_quantity := p_field_value_table(i);
11131         ELSIF p_field_name_table(i) = 'FIND_TASKS.TO_TASK_QUANTITY' THEN
11132           l_to_task_quantity := p_field_value_table(i);
11133         ELSIF p_field_name_table(i) = 'FIND_TASKS.FROM_TASK_PRIORITY' THEN
11134           l_from_task_priority := p_field_value_table(i);
11135         ELSIF p_field_name_table(i) = 'FIND_TASKS.TO_TASK_PRIORITY' THEN
11136           l_to_task_priority := p_field_value_table(i);
11137         ELSIF p_field_name_table(i) = 'FIND_TASKS.FROM_CREATION_DATE' THEN
11138           l_from_creation_date := FND_DATE.CHARDT_TO_DATE(p_field_value_table(i), 1);
11139         ELSIF p_field_name_table(i) = 'FIND_TASKS.TO_CREATION_DATE' THEN
11140           l_to_creation_date := FND_DATE.CHARDT_TO_DATE(p_field_value_table(i), 1);
11141         ELSIF p_field_name_table(i) = 'FIND_TASKS.FROM_PURCHASE_ORDER' THEN
11142           l_from_purchase_order := p_field_value_table(i);
11143         ELSIF p_field_name_table(i) = 'FIND_TASKS.FROM_PO_HEADER_ID' THEN
11144           l_from_po_header_id := p_field_value_table(i);
11145         ELSIF p_field_name_table(i) = 'FIND_TASKS.TO_PURCHASE_ORDER' THEN
11146           l_to_purchase_order := p_field_value_table(i);
11147         ELSIF p_field_name_table(i) = 'FIND_TASKS.TO_PO_HEADER_ID' THEN
11148           l_to_po_header_id := p_field_value_table(i);
11149         ELSIF p_field_name_table(i) = 'FIND_TASKS.FROM_RMA' THEN
11150           l_from_rma := p_field_value_table(i);
11151         ELSIF p_field_name_table(i) = 'FIND_TASKS.FROM_RMA_HEADER_ID' THEN
11152           l_from_rma_header_id := p_field_value_table(i);
11153         ELSIF p_field_name_table(i) = 'FIND_TASKS.TO_RMA' THEN
11154           l_to_rma := p_field_value_table(i);
11155         ELSIF p_field_name_table(i) = 'FIND_TASKS.TO_RMA_HEADER_ID' THEN
11156           l_to_rma_header_id := p_field_value_table(i);
11157         ELSIF p_field_name_table(i) = 'FIND_TASKS.FROM_REQUISITION' THEN
11158           l_from_requisition := p_field_value_table(i);
11159         ELSIF p_field_name_table(i) =
11160               'FIND_TASKS.FROM_REQUISITION_HEADER_ID' THEN
11161           l_from_requisition_header_id := p_field_value_table(i);
11162         ELSIF p_field_name_table(i) = 'FIND_TASKS.TO_REQUISITION' THEN
11163           l_to_requisition := p_field_value_table(i);
11164         ELSIF p_field_name_table(i) = 'FIND_TASKS.TO_REQUISITION_HEADER_ID' THEN
11165           l_to_requisition_header_id := p_field_value_table(i);
11166         ELSIF p_field_name_table(i) = 'FIND_TASKS.FROM_SHIPMENT' THEN
11167           l_from_shipment := p_field_value_table(i);
11168         ELSIF p_field_name_table(i) = 'FIND_TASKS.TO_SHIPMENT' THEN
11169           l_to_shipment := p_field_value_table(i);
11170         ELSIF p_field_name_table(i) = 'FIND_TASKS.FROM_SALES_ORDER_ID' THEN
11171           l_from_sales_order_id := p_field_value_table(i);
11172         ELSIF p_field_name_table(i) = 'FIND_TASKS.TO_SALES_ORDER_ID' THEN
11173           l_to_sales_order_id := p_field_value_table(i);
11174         ELSIF p_field_name_table(i) = 'FIND_TASKS.FROM_PICK_SLIP' THEN
11175           l_from_pick_slip := p_field_value_table(i);
11176         ELSIF p_field_name_table(i) = 'FIND_TASKS.TO_PICK_SLIP' THEN
11177           l_to_pick_slip := p_field_value_table(i);
11178         ELSIF p_field_name_table(i) = 'FIND_TASKS.CUSTOMER_ID' THEN
11179           l_customer_id := p_field_value_table(i);
11180         ELSIF p_field_name_table(i) = 'FIND_TASKS.CUSTOMER_CATEGORY' THEN
11181           l_customer_category := p_field_value_table(i);
11182         ELSIF p_field_name_table(i) = 'FIND_TASKS.DELIVERY_ID' THEN
11183           l_delivery_id := p_field_value_table(i);
11184         ELSIF p_field_name_table(i) = 'FIND_TASKS.CARRIER_ID' THEN
11185           l_carrier_id := p_field_value_table(i);
11186         ELSIF p_field_name_table(i) = 'FIND_TASKS.SHIP_METHOD_CODE' THEN
11187           l_ship_method_code := p_field_value_table(i);
11188         ELSIF p_field_name_table(i) = 'FIND_TASKS.TRIP_ID' THEN
11189           l_trip_id := p_field_value_table(i);
11190         ELSIF p_field_name_table(i) = 'FIND_TASKS.SHIPMENT_PRIORITY' THEN
11191           l_shipment_priority := p_field_value_table(i);
11192         ELSIF p_field_name_table(i) = 'FIND_TASKS.FROM_SHIPMENT_DATE' THEN
11193           l_from_shipment_date := FND_DATE.CHARDT_TO_DATE(p_field_value_table(i), 1);
11194         ELSIF p_field_name_table(i) = 'FIND_TASKS.TO_SHIPMENT_DATE' THEN
11195           l_to_shipment_date := FND_DATE.CHARDT_TO_DATE(p_field_value_table(i), 1);
11196         ELSIF p_field_name_table(i) = 'FIND_TASKS.SHIP_TO_STATE' THEN
11197           l_ship_to_state := p_field_value_table(i);
11198         ELSIF p_field_name_table(i) = 'FIND_TASKS.SHIP_TO_COUNTRY' THEN
11199           l_ship_to_country := p_field_value_table(i);
11200         ELSIF p_field_name_table(i) = 'FIND_TASKS.SHIP_TO_POSTAL_CODE' THEN
11201           l_ship_to_postal_code := p_field_value_table(i);
11202         ELSIF p_field_name_table(i) =
11203               'FIND_TASKS.FROM_LINES_IN_SALES_ORDER' THEN
11204           l_from_lines_in_sales_order := p_field_value_table(i);
11205         ELSIF p_field_name_table(i) = 'FIND_TASKS.TO_LINES_IN_SALES_ORDER' THEN
11206           l_to_lines_in_sales_order := p_field_value_table(i);
11207         ELSIF p_field_name_table(i) = 'FIND_TASKS.MANUFACTURING_TYPE' THEN
11208           l_manufacturing_type := p_field_value_table(i);
11209         ELSIF p_field_name_table(i) = 'FIND_TASKS.FROM_JOB' THEN
11210           l_from_job := p_field_value_table(i);
11211         ELSIF p_field_name_table(i) = 'FIND_TASKS.TO_JOB' THEN
11212           l_to_job := p_field_value_table(i);
11213         ELSIF p_field_name_table(i) = 'FIND_TASKS.ASSEMBLY_ID' THEN
11214           l_assembly_id := p_field_value_table(i);
11215         ELSIF p_field_name_table(i) = 'FIND_TASKS.FROM_START_DATE' THEN
11216           l_from_start_date := FND_DATE.CHARDT_TO_DATE(p_field_value_table(i), 1);
11217         ELSIF p_field_name_table(i) = 'FIND_TASKS.TO_START_DATE' THEN
11218           l_to_start_date := FND_DATE.CHARDT_TO_DATE(p_field_value_table(i), 1);
11219         ELSIF p_field_name_table(i) = 'FIND_TASKS.FROM_LINE' THEN
11220           l_from_line := p_field_value_table(i);
11221         ELSIF p_field_name_table(i) = 'FIND_TASKS.TO_LINE' THEN
11222           l_to_line := p_field_value_table(i);
11223         ELSIF p_field_name_table(i) = 'FIND_TASKS.DEPARTMENT_ID' THEN
11224           l_department_id := p_field_value_table(i);
11225         ELSIF p_field_name_table(i) = 'FIND_TASKS.FROM_REPLENISHMENT_MO' THEN
11226           l_from_replenishment_mo := p_field_value_table(i);
11227         ELSIF p_field_name_table(i) = 'FIND_TASKS.TO_REPLENISHMENT_MO' THEN
11228           l_to_replenishment_mo := p_field_value_table(i);
11229         ELSIF p_field_name_table(i) = 'FIND_TASKS.FROM_TRANSFER_ISSUE_MO' THEN
11230           l_from_transfer_issue_mo := p_field_value_table(i);
11231         ELSIF p_field_name_table(i) = 'FIND_TASKS.TO_TRANSFER_ISSUE_MO' THEN
11232           l_to_transfer_issue_mo := p_field_value_table(i);
11233         ELSIF p_field_name_table(i) = 'FIND_TASKS.CYCLE_COUNT_NAME' THEN
11234           l_cycle_count_name := p_field_value_table(i);
11235         ELSIF p_field_name_table(i) = 'FIND_TASKS.OP_PLAN_ACTIVITY_ID' THEN
11236           l_op_plan_activity_id := p_field_value_table(i);
11237         ELSIF p_field_name_table(i) = 'FIND_TASKS.OP_PLAN_TYPE_ID' THEN
11238           l_op_plan_type_id := p_field_value_table(i);
11239         ELSIF p_field_name_table(i) = 'FIND_TASKS.OP_PLAN_ID' THEN
11240           l_op_plan_id := p_field_value_table(i);
11241         ELSIF p_field_name_table(i) = 'FIND_TASKS.WAVE_HEADER_ID' THEN
11242           l_wave_header_id := p_field_value_table(i);
11243           print_debug('l wave header id is: ' || l_wave_header_id, l_debug);
11244         END IF;
11245 
11246       END LOOP;
11247     END IF;
11248 
11249     i                 := 1;
11250     l_organization_id := p_organization_id_table(i);
11251 
11252     IF p_query_type_table(i) = 'TEMP_TASK_PLANNING' THEN
11253       l_temp_query := TRUE;
11254     ELSE
11255       l_temp_query := FALSE;
11256     END IF;
11257 
11258     x_return_status := FND_API.G_RET_STS_SUCCESS;
11259 
11260     --DEBUG( 'Exiting SET_QUERY_TASKS_PARAMETERS');
11261 
11262   EXCEPTION
11263     WHEN OTHERS THEN
11264       x_return_status  := FND_API.G_RET_STS_UNEXP_ERROR;
11265       x_return_message := 'Unexpected error has occured in WMS_TASK_ACTION_PVT.SET_QUERY_TASKS_PARAMETERS. ' ||
11266                           'Oracle error message is ' || SQLERRM;
11267       --DEBUG( 'Unexpected error has occured. Oracle error message is '
11268     --  || SQLERRM, 'WMS_TASK_ACTION_PVT.SET_QUERY_TASKS_PARAMETERS - other error');
11269 
11270   END SET_QUERY_TASKS_PARAMETERS;
11271 
11272   function get_net_value(p_wave_header_id in number) return varchar2 is
11273 
11274     cursor c_net_value is
11275       SELECT Sum(outstanding_order_value(wdd.source_header_id,wdd.source_line_id)) net_value,
11276              wdd.source_header_number,
11277              max(ooh.TRANSACTIONAL_CURR_CODE) TRANSACTIONAL_CURR_CODE,
11278              max(ooh.conversion_rate) conversion_rate,
11279              max(ooh.conversion_type_code) conversion_type_code,
11280              max(ooh.conversion_rate_Date) conversion_rate_Date
11281              --max(org_id) org_id 12371804 remove this
11282         FROM wms_wp_Wave_lines wwl, oe_order_headers_all ooh, wsh_delivery_details wdd --11775489 get from wdd... FIXED...check performance problem
11283        WHERE wwl.wave_header_id = p_wave_header_id
11284 	     and wwl.delivery_detail_id  = wdd.delivery_detail_id
11285 		 and wdd.organization_id = wwl.organization_id
11286          AND wdd.source_header_number = ooh.order_number
11287          AND Nvl(wwl.remove_From_Wave_flag, 'N') <> 'Y'
11288        GROUP BY wdd.source_header_number;
11289 
11290     l_total_value number := 0;
11291 
11292     l_currency_code varchar2(10);
11293 
11294     l_set_of_books_id number;
11295 
11296     G_SOB_CURRENCY varchar2(10);
11297     l_debug        NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
11298 
11299   begin
11300 
11301     --12371804 get currency code once instead of fetching each time inside of loop
11302 	--this is because a wave can only have orders from one org
11303     select currency_code
11304 	into G_SOB_CURRENCY
11305 	from oe_gl_sets_of_books_v ogsob, org_organization_definitions ood, wms_wp_wave_headers_b wwh
11306 	where wwh.wave_header_id = p_wave_header_id
11307 	  and wwh.organization_id = ood.organization_id
11308 	  and ood.set_of_books_id = ogsob.set_of_books_id;
11309     for l_value in c_net_value loop
11310 
11311 
11312       l_total_value := l_total_value +
11313                        gl_currency_api.convert_amount(x_from_currency   => l_value.TRANSACTIONAL_CURR_CODE,
11314                                                       x_to_currency     => G_SOB_CURRENCY,
11315                                                       x_conversion_date => l_value.conversion_rate_Date,
11316                                                       x_conversion_type => l_value.conversion_rate,
11317                                                       x_amount          => l_value.net_value);
11318 
11319     end loop;
11320 
11321     return l_total_value || ' ' || G_SOB_CURRENCY;
11322 
11323   exception
11324     when others then
11325 
11326       print_debug('Error in Get Net Value API : ' || SQLCODE || ' : ' ||
11327                   SQLERRM,
11328                   l_debug);
11329 
11330       return null;
11331 
11332   end get_net_value;
11333 
11334   PROCEDURE SET_ACTION_TASKS_PARAMETERS(p_field_name_table  IN wms_wave_planning_pvt.field_name_table_type,
11335                                         p_field_value_table IN wms_wave_planning_pvt.field_value_table_type,
11336                                         p_query_type_table  IN wms_wave_planning_pvt.query_type_table_type,
11337                                         x_return_status     OUT NOCOPY VARCHAR2,
11338                                         x_return_message    OUT NOCOPY VARCHAR2) IS
11339     i       number;
11340     l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
11341   BEGIN
11342     print_DEBUG('Inside SET_ACTION_TASKS_PARAMETERS', l_debug);
11343 
11344     IF p_field_name_table.count <> 0 THEN
11345       FOR i IN p_field_name_table.first .. p_field_name_table.last LOOP
11346         IF p_field_name_table(i) = 'MANAGE_TASKS.ACTION_TYPE' THEN
11347           l_action_type := p_field_value_table(i);
11348         ELSIF p_field_name_table(i) = 'MANAGE_TASKS.STATUS' THEN
11349           l_status := p_field_value_table(i);
11350         ELSIF p_field_name_table(i) = 'MANAGE_TASKS.STATUS_CODE' THEN
11351           l_status_code := p_field_value_table(i);
11352         ELSIF p_field_name_table(i) = 'MANAGE_TASKS.PRIORITY_TYPE' THEN
11353           l_priority_type := p_field_value_table(i);
11354         ELSIF p_field_name_table(i) = 'MANAGE_TASKS.PRIORITY' THEN
11355           l_priority := p_field_value_table(i);
11356         ELSIF p_field_name_table(i) = 'MANAGE_TASKS.CLEAR_PRIORITY' THEN
11357           l_clear_priority := p_field_value_table(i);
11358         ELSIF p_field_name_table(i) = 'MANAGE_TASKS.ASSIGN_TYPE' THEN
11359           l_assign_type := p_field_value_table(i);
11360         ELSIF p_field_name_table(i) = 'MANAGE_TASKS.EMPLOYEE' THEN
11361           l_employee := p_field_value_table(i);
11362         ELSIF p_field_name_table(i) = 'MANAGE_TASKS.EMPLOYEE_ID' THEN
11363           l_employee_id := p_field_value_table(i);
11364         ELSIF p_field_name_table(i) = 'MANAGE_TASKS.USER_TASK_TYPE' THEN
11365           l_user_task_type := p_field_value_table(i);
11366         ELSIF p_field_name_table(i) = 'MANAGE_TASKS.USER_TASK_TYPE_ID' THEN
11367           l_user_task_type_id := p_field_value_table(i);
11368         ELSIF p_field_name_table(i) = 'MANAGE_TASKS.EFFECTIVE_START_DATE' THEN
11369           l_effective_start_date := FND_DATE.CHARDT_TO_DATE(p_field_value_table(i), 1);
11370         ELSIF p_field_name_table(i) = 'MANAGE_TASKS.EFFECTIVE_END_DATE' THEN
11371           l_effective_end_date := FND_DATE.CHARDT_TO_DATE(p_field_value_table(i), 1);
11372         ELSIF p_field_name_table(i) = 'MANAGE_TASKS.PERSON_RESOURCE_ID' THEN
11373           l_person_resource_id := p_field_value_table(i);
11374         ELSIF p_field_name_table(i) = 'MANAGE_TASKS.PERSON_RESOURCE_CODE' THEN
11375           l_person_resource_code := p_field_value_table(i);
11376         END IF;
11377 
11378         IF ((p_field_name_table(i) = 'MANAGE_TASKS.OVERRIDE_EMP_CHECK') and
11379            (p_field_value_table(i) = 'Y')) THEN
11380           l_override_emp_check := TRUE;
11381         END IF;
11382 
11383       END LOOP;
11384     END IF;
11385     i := 1;
11386     IF p_query_type_table(i) = 'TEMP_TASK_ACTION' THEN
11387       l_temp_action := TRUE;
11388     ELSE
11389       l_temp_action := FALSE;
11390     END IF;
11391 
11392     x_return_status := FND_API.G_RET_STS_SUCCESS;
11393 
11394     print_DEBUG('Exiting SET_ACTION_TASKS_PARAMETERS', l_debug);
11395 
11396   EXCEPTION
11397     WHEN OTHERS THEN
11398       x_return_status  := FND_API.G_RET_STS_UNEXP_ERROR;
11399       x_return_message := 'Unexpected error has occured in WMS_TASK_ACTION_PVT.SET_ACTION_TASKS_PARAMETERS. ' ||
11400                           'Oracle error message is ' || SQLERRM;
11401       print_DEBUG('Unexpected error has occured. Oracle error message is ' ||
11402                   SQLERRM ||
11403                   ' in WMS_TASK_ACTION_PVT.SET_ACTION_TASKS_PARAMETERS - other error',
11404                   l_debug);
11405   END SET_ACTION_TASKS_PARAMETERS;
11406 
11407    PROCEDURE insert_wave_header(x_return_status   OUT nocopy VARCHAR2,
11408                                p_wave_header_rec in wms_wp_wave_headers_vl%ROWTYPE) is
11409     l_debug     NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
11410     p_WAVE_NAME VARCHAR2(4000);
11411   begin
11412 
11413     -- delete from  wms_wp_wave_headers_vl where wave_header_id = p_wave_header_id ;
11414     --check if the wave header already exists.
11415     -- For appending wave name with wave header id
11416 
11417     savepoint wave_header_creation;
11418 
11419     if p_wave_header_rec.WAVE_NAME is null then
11420 
11421       p_WAVE_NAME := p_wave_header_rec.WAVE_NAME || ' ' ||
11422                      p_wave_header_rec.WAVE_HEADER_ID;
11423     else
11424 
11425       p_WAVE_NAME := p_wave_header_rec.WAVE_NAME || '-' ||
11426                      p_wave_header_rec.WAVE_HEADER_ID;
11427 
11428     end if;
11429     print_debug('Concurrent Request Id' || fnd_global.conc_request_id,
11430                 l_debug);
11431     insert into wms_wp_wave_headers_vl
11432       (WAVE_HEADER_ID,
11433        WAVE_NAME,
11434        WAVE_DESCRIPTION,
11435        start_time,   -- start time changes
11436        WAVE_SOURCE,
11437        WAVE_STATUS,
11438        TYPE_ID,
11439        BATCH_ID,
11440        SHIP_TO_LOCATION_ID,
11441        CUSTOMER_CLASS_ID,
11442        pull_replenishment_flag,
11443        INITIATE_WAVE_PLANNING,
11444        RELEASE_IMMEDIATELY,
11445        TABLE_NAME,
11446        ADVANCED_CRITERIA,
11447        CREATED_BY,
11448        CREATION_DATE,
11449        LAST_UPDATED_BY,
11450        LAST_UPDATE_DATE,
11451        LAST_UPDATE_LOGIN,
11452        ORGANIZATION_ID,
11453        PICK_SEQ_RULE_ID,
11454        PICK_GROUPING_RULE_ID,
11455        TRIP_ID,
11456        TRIP_STOP_ID,
11457        SHIP_METHOD_CODE,
11458        SHIPMENT_PRIORITY_CODE,
11459        CARRIER_ID,
11460        DELIVERY_ID,
11461        FROM_ORDER_HEADER_ID,
11462        ORDER_TYPE_ID,
11463        CUSTOMER_ID,
11464        TASK_ID,
11465        PROJECT_ID,
11466        CATEGORY_SET_ID,
11467        CATEGORY_ID,
11468        INVENTORY_ITEM_ID,
11469        BACKORDERS_FLAG,
11470        INCLUDE_PLANNED_LINES,
11471        TASK_PLANNING_FLAG,
11472        APPEND_DELIVERIES,
11473        AUTO_CREATE_DELIVERY,
11474        AUTO_CREATE_DELIVERY_CRITERIA,
11475        TASK_PRIORITY,
11476        DEFAULT_STAGE_SUBINVENTORY,
11477        DEFAULT_STAGE_LOCATOR_ID,
11478        DEFAULT_ALLOCATION_METHOD,
11479        ORDER_NAME,
11480        CUSTOMER,
11481        ORDER_TYPE,
11482        CUSTOMER_CLASS,
11483        SHIP_METHOD,
11484        CARRIER,
11485        SHIP_PRIORITY,
11486        DELIVERY,
11487        TRIP,
11488        TRIP_STOP,
11489        ITEM,
11490        ITEM_CATEGORY,
11491        PROJECT_NAME,
11492        TASK_NAME,
11493        SCHEDULED_DAYS,
11494        SCHEDULED_HRS,
11495        DOCK_APPOINTMENT_DAYS,
11496        DOCK_APPOINTMENT_HOURS,
11497        PICK_SLIP_GROUP,
11498        RELEASE_SEQ_RULE,
11499        STAGING_SUBINVENTORY,
11500        STAGING_LOCATOR,
11501        CROSS_DOCK_CRITERIA,
11502        PICK_SUBINVENTORY,
11503        PLANNING_CRITERIA,
11504        PLANNING_CRITERIA_ID,
11505        REQUEST_ID,
11506        WAVE_FIRMED_FLAG)
11507     values
11508       (p_wave_header_rec.WAVE_HEADER_ID,
11509        p_WAVE_NAME,
11510        p_wave_header_rec.WAVE_DESCRIPTION,
11511        p_wave_header_rec.start_time, -- start time changes
11512        p_wave_header_rec.WAVE_SOURCE,
11513        p_wave_header_rec.WAVE_STATUS,
11514        p_wave_header_rec.TYPE_ID,
11515        p_wave_header_rec.BATCH_ID,
11516        p_wave_header_rec.SHIP_TO_LOCATION_ID,
11517        p_wave_header_rec.CUSTOMER_CLASS_ID,
11518        p_wave_header_rec.pull_replenishment_flag,
11519        p_wave_header_rec.INITIATE_WAVE_PLANNING,
11520        p_wave_header_rec.RELEASE_IMMEDIATELY,
11521        p_wave_header_rec.TABLE_NAME,
11522        p_wave_header_rec.ADVANCED_CRITERIA,
11523        p_wave_header_rec.CREATED_BY,
11524        p_wave_header_rec.CREATION_DATE,
11525        p_wave_header_rec.LAST_UPDATED_BY,
11526        p_wave_header_rec.LAST_UPDATE_DATE,
11527        p_wave_header_rec.LAST_UPDATE_LOGIN,
11528        p_wave_header_rec.ORGANIZATION_ID,
11529        p_wave_header_rec.PICK_SEQ_RULE_ID,
11530        p_wave_header_rec.PICK_GROUPING_RULE_ID,
11531        p_wave_header_rec.TRIP_ID,
11532        p_wave_header_rec.TRIP_STOP_ID,
11533        p_wave_header_rec.SHIP_METHOD_CODE,
11534        p_wave_header_rec.SHIPMENT_PRIORITY_CODE,
11535        p_wave_header_rec.CARRIER_ID,
11536        p_wave_header_rec.DELIVERY_ID,
11537        p_wave_header_rec.FROM_ORDER_HEADER_ID,
11538        p_wave_header_rec.ORDER_TYPE_ID,
11539        p_wave_header_rec.CUSTOMER_ID,
11540        p_wave_header_rec.TASK_ID,
11541        p_wave_header_rec.PROJECT_ID,
11542        p_wave_header_rec.CATEGORY_SET_ID,
11543        p_wave_header_rec.CATEGORY_ID,
11544        p_wave_header_rec.INVENTORY_ITEM_ID,
11545        p_wave_header_rec.BACKORDERS_FLAG,
11546        p_wave_header_rec.INCLUDE_PLANNED_LINES,
11547        p_wave_header_rec.TASK_PLANNING_FLAG,
11548        p_wave_header_rec.APPEND_DELIVERIES,
11549        p_wave_header_rec.AUTO_CREATE_DELIVERY,
11550        p_wave_header_rec.AUTO_CREATE_DELIVERY_CRITERIA,
11551        p_wave_header_rec.TASK_PRIORITY,
11552        p_wave_header_rec.DEFAULT_STAGE_SUBINVENTORY,
11553        p_wave_header_rec.DEFAULT_STAGE_LOCATOR_ID,
11554        p_wave_header_rec.DEFAULT_ALLOCATION_METHOD,
11555        p_wave_header_rec.ORDER_NAME,
11556        p_wave_header_rec.CUSTOMER,
11557        p_wave_header_rec.ORDER_TYPE,
11558        p_wave_header_rec.CUSTOMER_CLASS,
11559        p_wave_header_rec.SHIP_METHOD,
11560        p_wave_header_rec.CARRIER,
11561        p_wave_header_rec.SHIP_PRIORITY,
11562        p_wave_header_rec.DELIVERY,
11563        p_wave_header_rec.TRIP,
11564        p_wave_header_rec.TRIP_STOP,
11565        p_wave_header_rec.ITEM,
11566        p_wave_header_rec.ITEM_CATEGORY,
11567        p_wave_header_rec.PROJECT_NAME,
11568        p_wave_header_rec.TASK_NAME,
11569        p_wave_header_rec.SCHEDULED_DAYS,
11570        p_wave_header_rec.SCHEDULED_HRS,
11571        p_wave_header_rec.DOCK_APPOINTMENT_DAYS,
11572        p_wave_header_rec.DOCK_APPOINTMENT_HOURS,
11573        p_wave_header_rec.PICK_SLIP_GROUP,
11574        p_wave_header_rec.RELEASE_SEQ_RULE,
11575        p_wave_header_rec.STAGING_SUBINVENTORY,
11576        p_wave_header_rec.STAGING_LOCATOR,
11577        p_wave_header_rec.CROSS_DOCK_CRITERIA,
11578        p_wave_header_rec.PICK_SUBINVENTORY,
11579        p_wave_header_rec.PLANNING_CRITERIA,
11580        p_wave_header_rec.PLANNING_CRITERIA_ID,
11581        fnd_global.conc_request_id,
11582        p_wave_header_rec.WAVE_FIRMED_FLAG);
11583 
11584     x_return_status := 'S';
11585     COMMIT;
11586 
11587   EXCEPTION
11588     when others then
11589       x_return_status := 'E';
11590       print_debug('Error in insert wave header API : ' || SQLCODE || ' : ' ||
11591                   SQLERRM,
11592                   l_debug);
11593   END insert_wave_header;
11594 
11595   PROCEDURE RELEASE_ONLINE(X_RETURN_STATUS  OUT NOCOPY VARCHAR2,
11596                            P_WAVE_HEADER_ID IN NUMBER) IS
11597     x_msg_count  varchar2(100);
11598     x_msg_data   varchar2(100);
11599     p_batch_id   number;
11600     P_REQUEST_ID NUMBER;
11601     l_debug      NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
11602   BEGIN
11603 
11604     create_batch_record(x_return_status, p_wave_header_id);
11605 
11606     if x_return_status = 'S' then
11607 
11608       WSH_PICKING_BATCHES_GRP.release_wms_wave(p_release_mode        => 'ONLINE',
11609                                                p_pick_wave_header_id => p_wave_header_id,
11610                                                x_request_id          => p_request_id,
11611                                                X_RETURN_STATUS       => X_RETURN_STATUS,
11612                                                X_MSG_COUNT           => X_MSG_COUNT,
11613                                                X_MSG_DATA            => X_MSG_DATA,
11614                                                P_BATCH_REC           => new_wave_type,
11615                                                X_BATCH_ID            => p_BATCH_ID);
11616 
11617 		 print_debug('Value of p batch id is' || p_batch_id, l_debug);
11618 
11619       if x_return_status = 'S' then
11620         print_debug('Updating Wave Header Status after Calling Release Wave from Workbench',
11621                     l_debug);
11622         update_wave_header_status(x_return_status,
11623                                   p_wave_header_id,
11624                                   'Released',
11625 								  p_batch_id);
11626 
11627 
11628         get_actual_fill_rate(x_return_status, p_wave_header_id);
11629 
11630         print_debug('Status after Call to Get the Actual Fill Rate' ||
11631                     x_return_status,
11632                     l_debug);
11633         commit;
11634 
11635       elsif  x_return_status = 'W' then
11636 
11637       	print_debug('Updating Wave Header Status after Calling Release Wave from Workbench',
11638                     l_debug);
11639         update_wave_header_status(x_return_status,
11640                                   p_wave_header_id,
11641                                   'Released(Warning)',
11642 								  p_batch_id);
11643 
11644 
11645         get_actual_fill_rate(x_return_status, p_wave_header_id);
11646 
11647         print_debug('Status after Call to Get the Actual Fill Rate' ||
11648                     x_return_status,
11649                     l_debug);
11650         commit;
11651       end if;
11652 
11653     end if;
11654   EXCEPTION
11655 
11656     WHEN OTHERS THEN
11657 
11658       print_debug('Error in Release Online API : ' || SQLCODE || ' : ' ||
11659                   SQLERRM,
11660                   l_debug);
11661 
11662   END RELEASE_ONLINE;
11663 
11664   procedure create_batch_record(x_return_status  OUT nocopy varchar2,
11665                                 p_wave_header_id in number)
11666 
11667    is
11668 
11669     p_FROM_ORDER_HEADER_ID       number;
11670     p_BACKORDERS_FLAG            varchar2(1);
11671     p_DOCUMENT_SET_ID            number;
11672     p_EXISTING_RSVS_ONLY_FLAG    varchar2(1);
11673     p_SHIPMENT_PRIORITY_CODE     varchar2(100);
11674     p_SHIP_METHOD_CODE           varchar2(100);
11675     p_CUSTOMER_ID                number;
11676     p_SHIP_SET_NUMBER            number;
11677     p_INVENTORY_ITEM_ID          number;
11678     p_ORDER_TYPE_ID              number;
11679     p_SHIP_TO_LOCATION_ID        number;
11680     p_TRIP_ID                    number;
11681     p_DELIVERY_ID                number;
11682     p_INCLUDE_PLANNED_LINES      varchar2(1);
11683     p_PICK_GROUPING_RULE_ID      number;
11684     p_PICK_SEQ_RULE_ID           number;
11685     p_AUTO_CREATE_DELIVERY       varchar2(1);
11686     p_TRIP_STOP_ID               number;
11687     p_DEFAULT_STAGE_SUBINVENTORY varchar2(100);
11688     p_DEFAULT_STAGE_LOCATOR_ID   number;
11689     p_PROJECT_ID                 number;
11690     p_TASK_ID                    number;
11691     p_ORGANIZATION_ID            number;
11692     p_TASK_PLANNING_FLAG         varchar2(1);
11693     p_CATEGORY_SET_ID            number;
11694     p_CATEGORY_ID                number;
11695     p_ac_DELIVERY_CRITERIA       varchar2(100);
11696     p_TASK_PRIORITY              number;
11697     p_DEFAULT_ALLOCATION_METHOD  varchar2(100);
11698     p_REPLENISHMENT_ONLY         varchar2(1);
11699     p_INITIATE_WAVE_PLANNING     varchar2(1);
11700     p_RELEASE_IMMEDIATELY        varchar2(1);
11701     p_pick_subinventory          varchar2(400);
11702     p_crossdock_criteria_name    varchar2(400);
11703 	p_append_flag                varchar2(1); -- 13361256
11704     l_debug                      NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'),
11705                                                0);
11706   begin
11707 
11708     select FROM_ORDER_HEADER_ID,
11709            BACKORDERS_FLAG,
11710            SHIP_PRIORITY,
11711            SHIP_METHOD_CODE,
11712            CUSTOMER_ID,
11713            INVENTORY_ITEM_ID,
11714            ORDER_TYPE_ID,
11715            SHIP_TO_LOCATION_ID,
11716            TRIP_ID,
11717            DELIVERY_ID,
11718            INCLUDE_PLANNED_LINES,
11719            PICK_GROUPING_RULE_ID,
11720            PICK_SEQ_RULE_ID,
11721            AUTO_CREATE_DELIVERY,
11722            TRIP_STOP_ID,
11723            staging_subinventory,
11724            DEFAULT_STAGE_LOCATOR_ID,
11725            PROJECT_ID,
11726            TASK_ID,
11727            ORGANIZATION_ID,
11728            TASK_PLANNING_FLAG,
11729            CATEGORY_SET_ID,
11730            CATEGORY_ID,
11731            AUTO_CREATE_DELIVERY_CRITERIA,
11732            TASK_PRIORITY,
11733            DEFAULT_ALLOCATION_METHOD,
11734            pull_replenishment_flag,
11735            INITIATE_WAVE_PLANNING,
11736            RELEASE_IMMEDIATELY,
11737            pick_subinventory,
11738            cross_dock_criteria,
11739 		   append_deliveries -- ssk
11740       into p_FROM_ORDER_HEADER_ID,
11741            p_BACKORDERS_FLAG,
11742            p_SHIPMENT_PRIORITY_CODE,
11743            p_SHIP_METHOD_CODE,
11744            p_CUSTOMER_ID,
11745            p_INVENTORY_ITEM_ID,
11746            p_ORDER_TYPE_ID,
11747            p_SHIP_TO_LOCATION_ID,
11748            p_TRIP_ID,
11749            p_DELIVERY_ID,
11750            p_INCLUDE_PLANNED_LINES,
11751            p_PICK_GROUPING_RULE_ID,
11752            p_PICK_SEQ_RULE_ID,
11753            p_AUTO_CREATE_DELIVERY,
11754            p_TRIP_STOP_ID,
11755            p_DEFAULT_STAGE_SUBINVENTORY,
11756            p_DEFAULT_STAGE_LOCATOR_ID,
11757            p_PROJECT_ID,
11758            p_TASK_ID,
11759            p_ORGANIZATION_ID,
11760            p_TASK_PLANNING_FLAG,
11761            p_CATEGORY_SET_ID,
11762            p_CATEGORY_ID,
11763            p_ac_DELIVERY_CRITERIA,
11764            p_TASK_PRIORITY,
11765            p_DEFAULT_ALLOCATION_METHOD,
11766            p_REPLENISHMENT_ONLY,
11767            p_INITIATE_WAVE_PLANNING,
11768            p_RELEASE_IMMEDIATELY,
11769            p_pick_subinventory,
11770            p_crossdock_criteria_name,
11771 		   p_append_flag --  ssk
11772       from wms_wp_wave_headers_vl
11773      where wave_header_id = p_wave_header_id;
11774 
11775     /* Leave out parameters other than in Wave Options tab
11776     if p_FROM_ORDER_HEADER_ID is not null then
11777       new_wave_type.Order_Header_Id := p_FROM_ORDER_HEADER_ID;
11778     end if;
11779 
11780       if p_SHIPMENT_PRIORITY_CODE is not null then
11781       new_wave_type.Shipment_Priority_Code := p_SHIPMENT_PRIORITY_CODE;
11782     end if;
11783     if p_SHIP_METHOD_CODE is not null then
11784       new_wave_type.Ship_Method_Code := p_SHIP_METHOD_CODE;
11785     end if;
11786     if p_CUSTOMER_ID is not null then
11787       new_wave_type.Customer_Id := p_CUSTOMER_ID;
11788     end if;
11789 
11790     if p_INVENTORY_ITEM_ID is not null then
11791       new_wave_type.Inventory_Item_Id := p_INVENTORY_ITEM_ID;
11792     end if;
11793     if p_ORDER_TYPE_ID is not null then
11794       new_wave_type.Order_Type_Id := p_ORDER_TYPE_ID;
11795     end if;
11796 
11797 
11798     if p_TRIP_ID is not null then
11799       new_wave_type.Trip_Id := p_TRIP_ID;
11800     end if;
11801     if p_DELIVERY_ID is not null then
11802       new_wave_type.Delivery_Id := p_DELIVERY_ID;
11803     end if;
11804 
11805       if p_CATEGORY_SET_ID is not null then
11806       new_wave_type.Category_Set_ID := p_CATEGORY_SET_ID;
11807     end if;
11808     if p_CATEGORY_ID is not null then
11809       new_wave_type.Category_ID := p_CATEGORY_ID;
11810     end if;
11811          if p_TRIP_STOP_ID is not null then
11812       new_wave_type.Trip_Stop_Id := p_TRIP_STOP_ID;
11813     end if;
11814       if p_PROJECT_ID is not null then
11815       new_wave_type.Project_Id := p_PROJECT_ID;
11816     end if;
11817     */
11818 	-- 13361256
11819 	begin
11820 	  select decode(MO_PICK_CONFIRM_REQUIRED,1,'N',2,'Y')
11821 	  into new_wave_type.auto_pick_confirm_flag
11822 	  from mtl_parameters
11823 	  where organization_id = p_ORGANIZATION_ID;
11824 	  print_debug('new_wave_type.auto_pick_confirm_flag '||new_wave_type.auto_pick_confirm_flag, l_debug);
11825 	exception
11826 	  when others then
11827 	   print_debug('SQLERRM '||SQLERRM, l_debug);
11828 	   print_debug('SQLCODE '||SQLCODE, l_debug);
11829 	end;
11830 
11831 	if new_wave_type.auto_pick_confirm_flag = 'N' then
11832 	  new_wave_type.Autopack_Flag := 'N';
11833 	end if;
11834 
11835 	if p_append_flag is not null then
11836 	  new_wave_type.append_flag := p_append_flag;
11837 	end if;
11838     -- 13361256
11839     if p_crossdock_criteria_name is not null then
11840       new_wave_type.crossdock_criteria_name := p_crossdock_criteria_name;
11841     end if;
11842 
11843     if p_INCLUDE_PLANNED_LINES is not null then
11844       --Include Newly Added Columns
11845       new_wave_type.Include_Planned_Lines := p_INCLUDE_PLANNED_LINES;
11846     end if;
11847     if p_BACKORDERS_FLAG is not null then
11848       new_wave_type.Backorders_Only_Flag := p_BACKORDERS_FLAG;
11849     end if;
11850     if p_PICK_GROUPING_RULE_ID is not null then
11851       new_wave_type.Pick_Grouping_Rule_Id := p_PICK_GROUPING_RULE_ID;
11852     end if;
11853     if p_PICK_SEQ_RULE_ID is not null then
11854       new_wave_type.Pick_Sequence_Rule_Id := p_PICK_SEQ_RULE_ID;
11855     end if;
11856     if p_AUTO_CREATE_DELIVERY is not null then
11857       new_wave_type.Autocreate_Delivery_Flag := p_AUTO_CREATE_DELIVERY;
11858     end if;
11859     if p_SHIP_TO_LOCATION_ID is not null then
11860       --Need to modify the code for From and To Requested Date and From and To Scheduled Ship Date
11861       new_wave_type.Ship_To_Location_Id := p_SHIP_TO_LOCATION_ID;
11862     end if;
11863     if p_DEFAULT_STAGE_SUBINVENTORY is not null then
11864       new_wave_type. Default_Stage_Subinventory := p_DEFAULT_STAGE_SUBINVENTORY;
11865     end if;
11866     if p_DEFAULT_STAGE_LOCATOR_ID is not null then
11867       new_wave_type.Default_Stage_Locator_Id := p_DEFAULT_STAGE_LOCATOR_ID;
11868     end if;
11869     if p_SHIP_SET_NUMBER is not null then
11870       new_wave_type.Ship_Set_Number := p_SHIP_SET_NUMBER;
11871     end if;
11872     if p_ORGANIZATION_ID is not null then
11873       new_wave_type.Organization_Id := p_ORGANIZATION_ID;
11874     end if;
11875     if p_TASK_PLANNING_FLAG is not null then
11876       new_wave_type.Task_Planning_Flag := p_TASK_PLANNING_FLAG;
11877     end if;
11878     if p_DOCUMENT_SET_ID is not null then
11879       new_wave_type.document_set_id := p_DOCUMENT_SET_ID;
11880     end if;
11881     if p_REPLENISHMENT_ONLY is not null then
11882       new_wave_type.dynamic_replenishment_flag := p_REPLENISHMENT_ONLY;
11883     end if;
11884 
11885     if p_pick_subinventory is not null then
11886       new_wave_type.PICK_FROM_SUBINVENTORY := p_pick_subinventory;
11887     end if;
11888 
11889     if p_ac_DELIVERY_CRITERIA is not null then
11890       new_wave_type.ac_Delivery_Criteria := p_ac_DELIVERY_CRITERIA;
11891     end if;
11892     if p_TASK_PRIORITY is not null then
11893       new_wave_type.task_priority := p_TASK_PRIORITY;
11894     end if;
11895     if p_DEFAULT_ALLOCATION_METHOD is not null then
11896       new_wave_type.allocation_method := p_DEFAULT_ALLOCATION_METHOD;
11897     end if;
11898     --Few columns in the PL/SQL record does not contain corresponding match in the headers table.
11899     --Will add Attributes in the end. As of now not adding that
11900     x_return_status := 'S';
11901     print_debug('Exited Create Batch Record API ', l_debug);
11902   exception
11903 
11904     when others then
11905       x_return_status := 'E';
11906       print_debug('Error in create batch record API : ' || SQLCODE ||
11907                   ' : ' || SQLERRM,
11908                   l_debug);
11909   end create_batch_record;
11910 
11911   --
11912   -- Name
11913   --   PROCEDURE Init_Rules
11914   --
11915   -- Purpose
11916   --   Retrieves sequencing information based on sequence rule
11917   --   Retrieves group by based on grouping rule
11918   --
11919   -- Input Parameter
11920   --   p_pick_seq_rule_id       - pick sequence rule
11921   --   p_pick_grouping_rule_id  - pick grouping rule
11922   --
11923   -- Output Parameters
11924   --   x_api_status      - Success, Error, Unexpected Error
11925   --
11926   PROCEDURE Init_Rules(p_pick_seq_rule_id      IN NUMBER,
11927                        p_pick_grouping_rule_id IN NUMBER,
11928                        x_api_status            OUT NOCOPY VARCHAR2) IS
11929     -- cursor to fetch pick sequence rule info
11930     CURSOR pick_seq_rule(v_psr_id IN NUMBER) IS
11931       SELECT NAME,
11932              NVL(ORDER_ID_PRIORITY, -1),
11933              DECODE(ORDER_ID_SORT, 'A', 'ASC', 'D', 'DESC', ''),
11934              NVL(INVOICE_VALUE_PRIORITY, -1),
11935              DECODE(INVOICE_VALUE_SORT, 'A', 'ASC', 'D', 'DESC', ''),
11936              NVL(SCHEDULE_DATE_PRIORITY, -1),
11937              DECODE(SCHEDULE_DATE_SORT, 'A', 'ASC', 'D', 'DESC', ''),
11938              NVL(SHIPMENT_PRI_PRIORITY, -1),
11939              DECODE(SHIPMENT_PRI_SORT, 'A', 'ASC', 'D', 'DESC', ''),
11940              NVL(TRIP_STOP_DATE_PRIORITY, -1),
11941              DECODE(TRIP_STOP_DATE_SORT, 'A', 'ASC', 'D', 'DESC', '')
11942         FROM WSH_PICK_SEQUENCE_RULES
11943        WHERE PICK_SEQUENCE_RULE_ID = v_psr_id
11944          AND SYSDATE BETWEEN TRUNC(NVL(START_DATE_ACTIVE, SYSDATE)) AND
11945              NVL(END_DATE_ACTIVE, TRUNC(SYSDATE) + 1);
11946 
11947     -- cursor to determine if pick slip rule contains order number
11948     CURSOR order_ps_group(v_pgr_id IN NUMBER) IS
11949       SELECT NVL(ORDER_NUMBER_FLAG, 'N')
11950         FROM WSH_PICK_GROUPING_RULES
11951        WHERE PICK_GROUPING_RULE_ID = v_pgr_id
11952          AND SYSDATE BETWEEN TRUNC(NVL(START_DATE_ACTIVE, SYSDATE)) AND
11953              NVL(END_DATE_ACTIVE, TRUNC(SYSDATE) + 1);
11954 
11955     l_pick_seq_rule_name      VARCHAR2(30);
11956     l_invoice_value_priority  NUMBER;
11957     l_order_number_priority   NUMBER;
11958     l_schedule_date_priority  NUMBER;
11959     l_trip_stop_date_priority NUMBER;
11960     l_shipment_pri_priority   NUMBER;
11961     l_invoice_value_sort      VARCHAR2(4);
11962     l_order_number_sort       VARCHAR2(4);
11963     l_schedule_date_sort      VARCHAR2(4);
11964     l_trip_stop_date_sort     VARCHAR2(4);
11965     l_shipment_pri_sort       VARCHAR2(4);
11966     i                         NUMBER;
11967     j                         NUMBER;
11968     l_temp_psr                psrTyp;
11969     l_cs                      NUMBER;
11970     l_debug                   NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'),
11971                                             0);
11972 
11973   BEGIN
11974     print_debug('Fetching pick sequence rule information for the batch',
11975                 l_debug);
11976     -- fetch pick sequence rule parameters
11977     OPEN pick_seq_rule(p_pick_seq_rule_id);
11978     FETCH pick_seq_rule
11979       INTO l_pick_seq_rule_name, l_order_number_priority,
11980        l_order_number_sort, l_invoice_value_priority,
11981         l_invoice_value_sort, l_schedule_date_priority,
11982          l_schedule_date_sort, l_shipment_pri_priority,
11983          l_shipment_pri_sort, l_trip_stop_date_priority,
11984           l_trip_stop_date_sort;
11985 
11986     -- handle pick sequence rule does not exist
11987     IF pick_seq_rule%NOTFOUND THEN
11988       print_debug('Pick sequence rule ID ' || to_char(g_pick_seq_rule_id) ||
11989                   ' does not exist.',
11990                   l_debug);
11991       x_api_status := FND_API.G_RET_STS_UNEXP_ERROR;
11992       RETURN;
11993     END IF;
11994     IF pick_seq_rule%ISOPEN THEN
11995       CLOSE pick_seq_rule;
11996     END IF;
11997 
11998     -- initialize the pick sequence rule parameters
11999     i := 1;
12000     IF (l_invoice_value_priority <> -1) THEN
12001       g_ordered_psr(i).attribute := C_INVOICE_VALUE;
12002       g_ordered_psr(i).attribute_name := 'INVOICE_VALUE';
12003       -- initialize the invoice_value_flag to be used as part
12004       -- of building the select statement
12005       /*??   g_invoice_value_flag := 'Y'; */
12006       g_ordered_psr(i).priority := l_invoice_value_priority;
12007       g_ordered_psr(i).sort_order := l_invoice_value_sort;
12008       i := i + 1;
12009     END IF;
12010     IF (l_order_number_priority <> -1) THEN
12011       g_ordered_psr(i).attribute := C_ORDER_NUMBER;
12012       g_ordered_psr(i).attribute_name := 'ORDER_NUMBER';
12013       g_ordered_psr(i).priority := l_order_number_priority;
12014       g_ordered_psr(i).sort_order := l_order_number_sort;
12015       i := i + 1;
12016     END IF;
12017     IF (l_schedule_date_priority <> -1) THEN
12018       g_ordered_psr(i).attribute := C_SCHEDULE_DATE;
12019       g_ordered_psr(i).attribute_name := 'SCHEDULE_DATE';
12020       g_ordered_psr(i).priority := l_schedule_date_priority;
12021       g_ordered_psr(i).sort_order := l_schedule_date_sort;
12022       i := i + 1;
12023     END IF;
12024     IF (l_trip_stop_date_priority <> -1) THEN
12025       g_ordered_psr(i).attribute := C_TRIP_STOP_DATE;
12026       g_ordered_psr(i).attribute_name := 'TRIP_STOP_DATE';
12027       g_ordered_psr(i).priority := l_trip_stop_date_priority;
12028       g_ordered_psr(i).sort_order := l_trip_stop_date_sort;
12029       i := i + 1;
12030     END IF;
12031     IF (l_shipment_pri_priority <> -1) THEN
12032       g_ordered_psr(i).attribute := C_SHIPMENT_PRIORITY;
12033       g_ordered_psr(i).attribute_name := 'SHIPMENT_PRIORITY';
12034       g_ordered_psr(i).priority := l_shipment_pri_priority;
12035       g_ordered_psr(i).sort_order := l_shipment_pri_sort;
12036       i := i + 1;
12037     END IF;
12038     g_total_pick_criteria := i - 1;
12039 
12040     -- sort the table for pick sequence rule according to priority
12041     FOR i IN 1 .. g_total_pick_criteria LOOP
12042       FOR j IN i + 1 .. g_total_pick_criteria LOOP
12043         IF (g_ordered_psr(j).priority < g_ordered_psr(i).priority) THEN
12044           l_temp_psr := g_ordered_psr(j);
12045           g_ordered_psr(j) := g_ordered_psr(i);
12046           g_ordered_psr(i) := l_temp_psr;
12047         END IF;
12048       END LOOP;
12049     END LOOP;
12050 
12051     -- determine the most significant pick sequence rule attribute
12052     g_primary_psr := g_ordered_psr(1).attribute_name;
12053     print_debug('Primary pick rule is ' || g_primary_psr, l_debug);
12054 
12055     -- print pick sequence rule information for debugging purposes
12056     FOR i IN 1 .. g_total_pick_criteria LOOP
12057       print_debug('attribute = ' || g_ordered_psr(i)
12058                   .attribute_name || ' ' || 'priority = ' ||
12059                   to_char(g_ordered_psr(i).priority) || ' ' || 'sort = ' ||
12060                   g_ordered_psr(i).sort_order,
12061                   l_debug);
12062     END LOOP;
12063     --Need to remove this part
12064     /*
12065       print_debug('Determining if order number is in grouping rule...',
12066                   l_debug);
12067       OPEN order_ps_group(p_pick_grouping_rule_id);
12068       FETCH order_ps_group
12069         INTO g_use_order_ps;
12070       IF order_ps_group%NOTFOUND THEN
12071         g_use_order_ps := 'N';
12072       END IF;
12073       IF order_ps_group%ISOPEN THEN
12074         CLOSE order_ps_group;
12075       END IF;
12076     */
12077     x_api_status := fnd_api.g_ret_sts_success;
12078 
12079   EXCEPTION
12080     -- handle other errors
12081     WHEN OTHERS THEN
12082       IF pick_seq_rule%ISOPEN THEN
12083         CLOSE pick_seq_rule;
12084       END IF;
12085       IF order_ps_group%ISOPEN THEN
12086         CLOSE order_ps_group;
12087       END IF;
12088       print_debug('Unexpected error in Init_Rules', l_debug);
12089       x_api_status := FND_API.G_RET_STS_ERROR;
12090   END Init_Rules;
12091 
12092   procedure launch_online(x_return_status     OUT nocopy VARCHAR2,
12093                           p_wave_header_id    IN NUMBER,
12094                           v_orgid             in number,
12095                           p_release_immediate in varchar2,
12096                           p_plan_wave         in varchar2,
12097                           p_request_id        OUT NOCOPY number,
12098                           p_add_lines         in varchar2 default 'N') IS
12099 
12100     l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
12101     --x_return_status varchar2(20);
12102     x_msg_count            varchar2(100);
12103     x_msg_data             varchar2(100);
12104     p_batch_id             number;
12105     p_planning_criteria_id number;
12106     wave_line_exception exception;
12107   begin
12108     print_debug('In Launch Online', l_debug);
12109 
12110     savepoint create_wave_lines_online_sp;
12111     --Get Dynamic SQL and insert into Lines Table
12112     get_dynamic_sql(p_wave_header_id, v_orgid, x_return_status);
12113 
12114     print_debug('Status after call to get Dynamic SQL ' || x_return_status,
12115                 l_debug);
12116 
12117     if x_return_status = 'S' then
12118 
12119       if p_plan_wave = 'Y' then
12120 
12121         select planning_criteria_id
12122           into p_planning_criteria_id
12123           from wms_wp_wave_headers_vl
12124          where wave_header_id = p_wave_header_id;
12125 
12126         print_debug('In Launch Online Plan Wave with Planning Criteria Id ' ||
12127                     p_planning_criteria_id,
12128                     l_debug);
12129         WMS_WAVE_PLANNING_PVT.Plan_Wave(p_wave_header_id,
12130                                         p_planning_criteria_id,
12131                                         x_return_status);
12132       end if;
12133 
12134     else
12135 
12136       if p_Add_lines = 'Y' then
12137         rollback to create_wave_lines_online_sp;
12138         RAISE fnd_api.g_exc_unexpected_error;
12139       else
12140         raise wave_line_exception;
12141       end if;
12142     end if;
12143     if x_return_status = 'S' then
12144       print_debug('In Launch Online Release Immediately ' ||
12145                   p_release_immediate,
12146                   l_debug);
12147       if p_release_immediate = 'Y' then
12148         create_batch_record(x_return_status, p_wave_header_id);
12149 
12150         if x_return_status = 'S' then
12151 
12152           -- Need to firm the wave as releasing it will firm the wave
12153           savepoint release_wave_lines_sp;
12154 
12155           UPDATE wms_wp_wave_lines
12156              SET message               = 'This line has been Firmed in Wave ' ||
12157                                          p_wave_header_id,
12158                  remove_from_Wave_flag = 'Y'
12159            WHERE delivery_detail_id IN
12160                  (SELECT wwl.delivery_detail_id
12161                 FROM wms_wp_wave_lines wwl, wsh_delivery_details wdd --11775489 get from wdd... FIXED
12162                WHERE wwl.wave_header_id = p_wave_header_id
12163 			     and wwl.delivery_detail_id = wdd.delivery_detail_id
12164 				 and wwl.organization_id = wdd.organization_id
12165                  and nvl(remove_from_wave_flag, 'N') <> 'Y')
12166              and wave_header_id <> p_wave_header_id;
12167 
12168           update wms_wp_wave_headers_vl
12169              set wave_firmed_flag = 'Y'
12170            where wave_header_id = p_wave_header_id;
12171 
12172           print_debug('In Launch Online Before Calling Release Wave',
12173                       l_debug);
12174 
12175           WSH_PICKING_BATCHES_GRP.release_wms_wave(p_release_mode        => 'ONLINE',
12176                                                    p_pick_wave_header_id => p_wave_header_id,
12177                                                    x_request_id          => p_request_id,
12178                                                    X_RETURN_STATUS       => X_RETURN_STATUS,
12179                                                    X_MSG_COUNT           => X_MSG_COUNT,
12180                                                    X_MSG_DATA            => X_MSG_DATA,
12181                                                    P_BATCH_REC           => new_wave_type,
12182                                                    X_BATCH_ID            => p_BATCH_ID);
12183 
12184 			print_debug('Value of p batch id is' || p_batch_id, l_debug);
12185 
12186         end if;
12187         if x_return_status = 'S' then
12188           print_debug('Updating Wave Header Status after Calling Release Wave',
12189                       l_debug);
12190           update_wave_header_status(x_return_status,
12191                                     p_wave_header_id,
12192                                     'Released',
12193 									p_batch_id);
12194 
12195 
12196           --Update the Actual Fill Rate
12197           get_actual_fill_rate(x_return_status, p_wave_header_id);
12198 
12199           print_debug('Status after Call to Get the Actual Fill Rate' ||
12200                       x_return_status,
12201                       l_debug);
12202 
12203       elsif  x_return_status = 'W' then
12204  print_debug('Updating Wave Header Status after Calling Release Wave',
12205                       l_debug);
12206           update_wave_header_status(x_return_status,
12207                                     p_wave_header_id,
12208                                     'Released(Warning)',
12209 									p_batch_id);
12210 
12211 
12212           --Update the Actual Fill Rate
12213           get_actual_fill_rate(x_return_status, p_wave_header_id);
12214 
12215           print_debug('Status after Call to Get the Actual Fill Rate' ||
12216                       x_return_status,
12217                       l_debug);
12218 
12219 
12220 else
12221           rollback to release_wave_lines_sp;
12222           RAISE fnd_api.g_exc_unexpected_error;
12223         end if;
12224       end if;
12225 
12226     end if;
12227 
12228     commit;
12229 
12230   EXCEPTION
12231     WHEN wave_line_exception then
12232 
12233       rollback to create_wave_lines_online_sp;
12234 
12235       delete_wave_header(p_wave_header_id);
12236       RAISE fnd_api.g_exc_unexpected_error;
12237 
12238     WHEN OTHERS THEN
12239       print_debug('Error in Launch Online : ' || SQLCODE || ' : ' ||
12240                   SQLERRM,
12241                   l_debug);
12242   end launch_online;
12243 
12244   procedure get_dynamic_sql(p_wave_header_id in number,
12245                             org_id           in number,
12246                             x_return_status  OUT NOCOPY varchar2) is
12247     l_debug           NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
12248     l_organization_id number := org_id;
12249     l_return_status   varchar2(30);
12250     l_done_flag       varchar2(10);
12251     v_advanced_sql    varchar2(4000);
12252     v_wave_sql        varchar2(4000);
12253 
12254     l_to_sub_tmp            VARCHAR2(10);
12255     l_to_loc_tmp            NUMBER;
12256     l_pick_seq_rule_id      NUMBER;
12257     l_pick_grouping_rule_id NUMBER;
12258     l_ps_mode               VARCHAR2(1);
12259     l_use_header_flag       VARCHAR2(1);
12260     l_firmed_flag           varchar2(1);
12261     l_count                 number;
12262 
12263     l_api_is_implemented     BOOLEAN;
12264     l_custom_line_tbl        line_tbl_typ;
12265     l_custom_line_action_tbl action_tbl_typ;
12266     l_replenishment_status   VARCHAR2(1);
12267     l_released_status        VARCHAR2(1);
12268     l_msg_count              varchar2(100);
12269     l_msg_data               varchar2(100);
12270     l_tbl_count number;
12271 
12272     CURSOR get_default_params(v_org_id IN NUMBER) IS
12273       SELECT NVL(PRINT_PICK_SLIP_MODE, 'E'),
12274              NVL(AUTOCREATE_DEL_ORDERS_FLAG, 'Y'),
12275              DEFAULT_STAGE_SUBINVENTORY,
12276              DEFAULT_STAGE_LOCATOR_ID,
12277              PICK_SEQUENCE_RULE_ID,
12278              PICK_GROUPING_RULE_ID
12279         FROM WSH_SHIPPING_PARAMETERS
12280        WHERE ORGANIZATION_ID = org_id;
12281 
12282     cursor c_get_wave_parameters is
12283       SELECT restrictions.PARAMETER_ID,
12284              restrictions.OPERATOR_MEANING,
12285              restrictions.OPERAND_VALUE
12286         FROM WMS_WP_ADVANCED_CRITERIA restrictions,
12287              wms_wp_wave_headers_vl   headers
12288        WHERE restrictions.RULE_WAVE_HEADER_ID = headers.wave_header_id
12289          and restrictions.RULE_WAVE_HEADER_ID = P_WAVE_HEADER_ID
12290          AND PARAMETER_ID IN (6002, 6003, 6004, 6005, 6006);
12291 
12292     cursor c_get_wave_line_count is
12293       SELECT restrictions.PARAMETER_ID,
12294              restrictions.OPERATOR_MEANING,
12295              restrictions.OPERAND_VALUE
12296         FROM WMS_WP_ADVANCED_CRITERIA restrictions,
12297              wms_wp_wave_headers_vl   headers
12298        WHERE restrictions.RULE_WAVE_HEADER_ID = headers.wave_header_id
12299          and restrictions.RULE_WAVE_HEADER_ID = P_WAVE_HEADER_ID
12300          AND PARAMETER_ID = 6001;
12301 
12302   begin
12303 
12304     print_debug('Entered Get Dynamic SQL ' || l_return_status, l_debug);
12305 
12306     select advanced_criteria, nvl(wave_firmed_flag, 'N')
12307       into v_advanced_sql, l_firmed_flag
12308       from wms_wp_wave_headers_vl
12309      where wave_header_id = p_wave_header_id;
12310 
12311     OPEN get_default_params(org_id);
12312     FETCH get_default_params
12313       INTO l_ps_mode, l_use_header_flag, l_to_sub_tmp, l_to_loc_tmp, l_pick_seq_rule_id, l_pick_grouping_rule_id;
12314 
12315     IF (l_pick_grouping_rule_id IS NOT NULL AND g_pick_seq_rule_id IS NULL) THEN
12316       -- This case shd not happen....as release sequence rule is mandatory field in UI
12317       WMS_WAVE_PLANNING_PVT.Init_Rules(l_pick_seq_rule_id,
12318                                        l_pick_grouping_rule_id,
12319                                        l_return_status);
12320 
12321     else
12322 
12323       WMS_WAVE_PLANNING_PVT.Init_Rules(g_pick_seq_rule_id,
12324                                        g_pick_grouping_rule_id,
12325                                        l_return_status);
12326 
12327     end if;
12328 
12329     WMS_WAVE_PLANNING_PVT.Init_Cursor(l_organization_id,
12330                                       v_advanced_sql,
12331                                       P_WAVE_HEADER_ID,
12332                                       l_return_status);
12333 
12334     print_debug('init cursor status ' || l_return_status, l_debug);
12335 
12336     IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
12337       print_debug('Error occurred in Init_Cursor', l_debug);
12338       x_return_status := 'E';
12339       --  RETURN;
12340     elsif l_return_status = FND_API.G_RET_STS_SUCCESS then
12341       print_Debug('before get Lines ' || l_return_status, l_Debug);
12342 
12343 	  --12371394 put the get_lines call and the insert to wwl inside a loop
12344       l_done_flag := FND_API.G_FALSE;
12345 	  LOOP
12346 	  EXIT WHEN l_done_flag = FND_API.G_TRUE;
12347       WMS_WAVE_PLANNING_PVT.Get_Lines(l_done_flag, l_return_status);
12348 
12349       IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
12350         print_debug('Error occurred in Get Lines', l_debug);
12351         -- RETURN;
12352 		l_done_flag := FND_API.G_TRUE; --12371394
12353         x_return_status := 'E';
12354       else
12355         FORALL i IN 1 .. release_table.Count
12356           insert into wms_wp_wave_lines
12357             (WAVE_HEADER_ID,
12358              WAVE_LINE_ID,
12359              WAVE_LINE_SOURCE,
12360              WAVE_LINE_STATUS,
12361              CREATED_BY,
12362              CREATION_DATE,
12363              LAST_UPDATED_BY,
12364              LAST_UPDATE_DATE,
12365              LAST_UPDATE_LOGIN,
12366              DELIVERY_DETAIL_ID,
12367              ORGANIZATION_ID,
12368              DEMAND_SOURCE_HEADER_ID,
12369 			 crossdock_uom)--11775489 this column added
12370           values
12371             (p_wave_header_id,
12372              WMS_WP_WAVE_LINES_S.NEXTVAL,
12373              'OE',
12374              'Created',
12375              fnd_global.user_id,
12376              sysdate,
12377              fnd_global.user_id,
12378              sysdate,
12379              fnd_global.login_id,
12380              l_delivery_Detail_id_tb(i),
12381              l_organization_id_tb(i),
12382              inv_salesorder.get_salesorder_for_oeheader(l_source_header_id_tb(i)), --???????? Demand Source Header Id
12383 			 l_requested_quantity_uom_tb(i));--11775489 this column added
12384 
12385         --   commit;
12386 
12387 	  end if;  --12371394 now we end the if statement here so the filtering of wwwl happens only once instead of once per loop iteration
12388 	  end loop;--12371394 end the loop here.
12389 
12390         -- Honor the Wave Aggregate parameters
12391 
12392         for c_rec in c_get_wave_parameters loop
12393 
12394           exit when c_get_wave_parameters%NOTFOUND;
12395 
12396           if c_rec.operator_meaning in ('>', '>=', '=') then
12397 
12398             print_debug('Entered Wave Parameter with >, >= ,= operators  ',
12399                         l_debug);
12400             print_debug('If Criteria is not satified Wave Lines wont be created. ',
12401                         l_debug);
12402 
12403             if c_rec.parameter_id in (6002) then
12404 
12405               -- Total Weight
12406               select sum(wdd.net_weight)
12407                 into l_count
12408                 from wms_wp_wave_lines wwl, wsh_delivery_Details wdd --11775489 get from wdd... FIXED
12409                where wwl.wave_header_id = p_wave_header_id
12410 			     and wwl.delivery_detail_id  = wdd.delivery_detail_id
12411 				 and wdd.organization_id = wwl.organization_id;
12412 
12413               print_debug('Entered Wave Parameter with >, >= ,= operators  --- >  Total Weight ',
12414                           l_debug);
12415 
12416               print_debug(' Total Weight  in Lines in the Wave is ' ||
12417                           l_count,
12418                           l_debug);
12419 
12420               print_debug(' Total Weight given in the Criteria is  ' ||
12421                           C_REC.OPERAND_VALUE,
12422                           l_debug);
12423 
12424               if c_rec.operator_meaning = '=' then
12425 
12426                 if l_count = C_REC.OPERAND_VALUE then
12427 
12428                   print_debug('Criteria is satisfied. So not deleting any lines ',
12429                               l_debug);
12430                 else
12431                   delete from wms_wp_wave_lines
12432                    where wave_header_id = p_wave_header_id;
12433 
12434                 end if;
12435               elsif c_rec.operator_meaning = '>=' then
12436 
12437                 if l_count >= C_REC.OPERAND_VALUE then
12438                   print_debug('Criteria is satisfied. So not deleting any lines ',
12439                               l_debug);
12440                 else
12441                   delete from wms_wp_wave_lines
12442                    where wave_header_id = p_wave_header_id;
12443 
12444                 end if;
12445               elsif c_rec.operator_meaning = '>' then
12446 
12447                 if l_count > C_REC.OPERAND_VALUE then
12448                   print_debug('Criteria is satisfied. So not deleting any lines ',
12449                               l_debug);
12450                 else
12451                   delete from wms_wp_wave_lines
12452                    where wave_header_id = p_wave_header_id;
12453 
12454                 end if;
12455 
12456               end if;
12457 
12458             elsif c_rec.parameter_id in (6003) then
12459               -- Total Volume
12460               select sum(wdd.volume)
12461                 into l_count
12462                 from wms_wp_wave_lines wwl, wsh_delivery_details wdd--11775489 get from wdd... FIXED
12463                where wwl.wave_header_id = p_wave_header_id
12464 			     and wwl.delivery_detail_id  = wdd.delivery_detail_id
12465 				 and wdd.organization_id = wwl.organization_id;
12466 
12467               print_debug('Entered Wave Parameter with >, >= ,= operators  --- >  Total Volume ',
12468                           l_debug);
12469 
12470               print_debug(' Total Volume in Lines in the Wave is ' ||
12471                           l_count,
12472                           l_debug);
12473 
12474               print_debug(' Total Volume given in the Criteria is  ' ||
12475                           C_REC.OPERAND_VALUE,
12476                           l_debug);
12477 
12478               if c_rec.operator_meaning = '=' then
12479 
12480                 if l_count = C_REC.OPERAND_VALUE then
12481                   print_debug('Criteria is satisfied. So not deleting any lines ',
12482                               l_debug);
12483                 else
12484                   delete from wms_wp_wave_lines
12485                    where wave_header_id = p_wave_header_id;
12486 
12487                 end if;
12488               elsif c_rec.operator_meaning = '>=' then
12489 
12490                 if l_count >= C_REC.OPERAND_VALUE then
12491                   print_debug('Criteria is satisfied. So not deleting any lines ',
12492                               l_debug);
12493                 else
12494                   delete from wms_wp_wave_lines
12495                    where wave_header_id = p_wave_header_id;
12496 
12497                 end if;
12498               elsif c_rec.operator_meaning = '>' then
12499 
12500                 if l_count > C_REC.OPERAND_VALUE then
12501                   print_debug('Criteria is satisfied. So not deleting any lines ',
12502                               l_debug);
12503                 else
12504                   delete from wms_wp_wave_lines
12505                    where wave_header_id = p_wave_header_id;
12506 
12507                 end if;
12508 
12509               end if;
12510 
12511             elsif c_rec.parameter_id in (6004) then
12512               -- Total Value
12513               select sum(outstanding_order_value(wdd.source_header_id, wdd.source_line_id))
12514                 into l_count
12515                 from wms_wp_wave_lines wwl, wsh_delivery_details wdd --11775489 get from wdd... FIXED
12516                where wave_header_id = p_wave_header_id
12517 			     and wwl.delivery_detail_id  = wdd.delivery_detail_id
12518 				 and wdd.organization_id = wwl.organization_id;
12519 
12520               print_debug('Entered Wave Parameter with >, >= ,= operators  --- >  Total Value ',
12521                           l_debug);
12522 
12523               print_debug(' Total Value  in Lines in the Wave is ' ||
12524                           l_count,
12525                           l_debug);
12526 
12527               print_debug(' Total Value given in the Criteria is  ' ||
12528                           C_REC.OPERAND_VALUE,
12529                           l_debug);
12530 
12531               if c_rec.operator_meaning = '=' then
12532 
12533                 if l_count = C_REC.OPERAND_VALUE then
12534                   print_debug('Criteria is satisfied. So not deleting any lines ',
12535                               l_debug);
12536                 else
12537                   delete from wms_wp_wave_lines
12538                    where wave_header_id = p_wave_header_id;
12539 
12540                 end if;
12541               elsif c_rec.operator_meaning = '>=' then
12542 
12543                 if l_count >= C_REC.OPERAND_VALUE then
12544                   print_debug('Criteria is satisfied. So not deleting any lines ',
12545                               l_debug);
12546                 else
12547                   delete from wms_wp_wave_lines
12548                    where wave_header_id = p_wave_header_id;
12549 
12550                 end if;
12551               elsif c_rec.operator_meaning = '>' then
12552 
12553                 if l_count > C_REC.OPERAND_VALUE then
12554                   print_debug('Criteria is satisfied. So not deleting any lines ',
12555                               l_debug);
12556                 else
12557                   delete from wms_wp_wave_lines
12558                    where wave_header_id = p_wave_header_id;
12559 
12560                 end if;
12561 
12562               end if;
12563 
12564             elsif c_rec.parameter_id in (6005) then
12565               --Delivery Count
12566               select count(distinct wda.delivery_id)
12567                 into l_count
12568                 from wms_wp_wave_lines wwl, wsh_delivery_assignments wda, wsh_delivery_details wdd --11775489 get from wdd... FIXED
12569                where wwl.wave_header_id = p_wave_header_id
12570 			     and wwl.delivery_detail_id  = wdd.delivery_detail_id
12571 				 and wwl.organization_id = wdd.organization_id
12572 				 and wda.delivery_detail_id = wdd.delivery_detail_id;
12573 
12574               print_debug('Entered Wave Parameter with >, >= ,= operators  --- > Delivery count ',
12575                           l_debug);
12576 
12577               print_debug(' Delivery count  in Lines in the Wave is ' ||
12578                           l_count,
12579                           l_debug);
12580 
12581               print_debug(' Delivery count  given in the Criteria is  ' ||
12582                           C_REC.OPERAND_VALUE,
12583                           l_debug);
12584 
12585               if c_rec.operator_meaning = '=' then
12586 
12587                 if l_count = C_REC.OPERAND_VALUE then
12588                   print_debug('Criteria is satisfied. So not deleting any lines ',
12589                               l_debug);
12590                 else
12591                   delete from wms_wp_wave_lines
12592                    where wave_header_id = p_wave_header_id;
12593 
12594                 end if;
12595               elsif c_rec.operator_meaning = '>=' then
12596 
12597                 if l_count >= C_REC.OPERAND_VALUE then
12598                   print_debug('Criteria is satisfied. So not deleting any lines ',
12599                               l_debug);
12600                 else
12601                   delete from wms_wp_wave_lines
12602                    where wave_header_id = p_wave_header_id;
12603 
12604                 end if;
12605               elsif c_rec.operator_meaning = '>' then
12606 
12607                 if l_count > C_REC.OPERAND_VALUE then
12608                   print_debug('Criteria is satisfied. So not deleting any lines ',
12609                               l_debug);
12610                 else
12611                   delete from wms_wp_wave_lines
12612                    where wave_header_id = p_wave_header_id;
12613 
12614                 end if;
12615 
12616               end if;
12617             elsif c_rec.parameter_id in (6006) then
12618               -- Item Count
12619               select count(distinct wdd.inventory_item_id)
12620                 into l_count
12621                 from wms_wp_wave_lines wwl, wsh_delivery_Details wdd --11775489 get from wdd... FIXED
12622                where wwl.wave_header_id = p_wave_header_id
12623 			     and wwl.delivery_detail_id  = wdd.delivery_detail_id
12624 				 and wdd.organization_id = wwl.organization_id;
12625 
12626               print_debug('Entered Wave Parameter with >, >= ,= operators  --- > Item count ',
12627                           l_debug);
12628 
12629               print_debug(' Item count  in Lines in the Wave is ' ||
12630                           l_count,
12631                           l_debug);
12632 
12633               print_debug(' Item count  given in the Criteria is  ' ||
12634                           C_REC.OPERAND_VALUE,
12635                           l_debug);
12636 
12637               if c_rec.operator_meaning = '=' then
12638 
12639                 if l_count = C_REC.OPERAND_VALUE then
12640                   print_debug('Criteria is satisfied. So not deleting any lines ',
12641                               l_debug);
12642                 else
12643                   delete from wms_wp_wave_lines
12644                    where wave_header_id = p_wave_header_id;
12645 
12646                 end if;
12647               elsif c_rec.operator_meaning = '>=' then
12648 
12649                 if l_count >= C_REC.OPERAND_VALUE then
12650                   print_debug('Criteria is satisfied. So not deleting any lines ',
12651                               l_debug);
12652                 else
12653                   delete from wms_wp_wave_lines
12654                    where wave_header_id = p_wave_header_id;
12655 
12656                 end if;
12657               elsif c_rec.operator_meaning = '>' then
12658 
12659                 if l_count > C_REC.OPERAND_VALUE then
12660                   print_debug('Criteria is satisfied. So not deleting any lines ',
12661                               l_debug);
12662                 else
12663                   delete from wms_wp_wave_lines
12664                    where wave_header_id = p_wave_header_id;
12665 
12666                 end if;
12667 
12668               end if;
12669 
12670             end if;
12671 
12672             --       end if;
12673 
12674           else
12675             -- This is for criteria that is '<', '<=' where u need to delete the extra lines
12676             if c_rec.parameter_id in (6002, 6003, 6004) then
12677 
12678               if C_REC.OPERATOR_MEANING = '<' then
12679 
12680                 C_REC.OPERATOR_MEANING := '>=';
12681 
12682               elsif C_REC.OPERATOR_MEANING = '<=' then
12683 
12684                 C_REC.OPERATOR_MEANING := '>';
12685 
12686               end if;
12687             end if;
12688 
12689             if c_rec.parameter_id = 6002 then
12690 
12691               -- Total Weight
12692 --11775489 get from wdd... FIXED
12693               EXECUTE IMMEDIATE 'delete from wms_wp_wave_lines where wave_line_id in
12694          (select wave_line_id from
12695              (select x.wave_line_id, x.Cum_total_weight
12696               from
12697                 (
12698                    select wl.wave_line_id , SUM(wdd.net_weight) OVER(ORDER BY wave_line_id) Cum_total_weight from wms_wp_wave_lines wl, wsh_delivery_details wdd
12699                     where wl.wave_header_id = :wave_header_id and wdd.delivery_detail_id = wwl.delivery_detail_id and wdd.organization_id = wwl.organization_id
12700                 ) x
12701                  where Cum_total_weight ' ||
12702                                 C_REC.OPERATOR_MEANING ||
12703                                 C_REC.OPERAND_VALUE || '))'
12704                 using p_wave_header_id;
12705 
12706               print_debug('No of Lines deleted based on condition Total Weight is  ' ||
12707                           SQL%ROWCOUNT,
12708                           l_debug);
12709 
12710             elsif c_rec.parameter_id = 6003 then
12711 
12712               -- Total Volume
12713 			  --11775489 get from wdd... FIXED
12714               EXECUTE IMMEDIATE 'delete from wms_wp_wave_lines where wave_line_id in
12715          (select wave_line_id from
12716              (select x.wave_line_id, x.Cum_total_volume
12717               from
12718                 (
12719                    select wl.wave_line_id , SUM(wdd.volume) OVER(ORDER BY wave_line_id) Cum_total_volume from wms_wp_wave_lines wl, wsh_delivery_details wdd
12720                     where wl.wave_header_id = :wave_header_id and wwl.delivery_detail_id  = wdd.delivery_detail_id and wdd.organization_id = wwl.organization_id
12721                 ) x
12722                  where Cum_total_volume ' ||
12723                                 C_REC.OPERATOR_MEANING ||
12724                                 C_REC.OPERAND_VALUE || '))'
12725                 using p_wave_header_id;
12726 
12727               print_debug('No of Lines deleted based on condition Total Volume is  ' ||
12728                           SQL%ROWCOUNT,
12729                           l_debug);
12730 
12731             elsif c_rec.parameter_id = 6004 then
12732 
12733               -- Total Value
12734 
12735               EXECUTE IMMEDIATE 'delete from wms_wp_wave_lines where wave_line_id in
12736          (select wave_line_id from
12737              (select x.wave_line_id, x.Cum_value
12738               from
12739                 (
12740                    select wl.wave_line_id , OUTSTANDING_ORDER_VALUE(wdd.source_header_id, wdd.source_line_id) OVER(ORDER BY wave_line_id) Cum_value from wms_wp_wave_lines wl, wsh_delivery_details
12741                     where wave_header_id = :wave_header_id and wwl.delivery_detail_id  = wdd.delivery_detail_id and wdd.organization_id = wwl.organization_id
12742                 ) x
12743                  where Cum_value' ||
12744                                 C_REC.OPERATOR_MEANING ||
12745                                 C_REC.OPERAND_VALUE || '))'
12746                 using p_wave_header_id;
12747 
12748               print_debug('No of Lines deleted based on condition Total Value is  ' ||
12749                           SQL%ROWCOUNT,
12750                           l_debug);
12751 
12752             elsif c_rec.parameter_id = 6005 then
12753 
12754               --Delivery Count
12755 			  --11775489 get from wdd... check for error?
12756               EXECUTE IMMEDIATE 'delete from wms_wp_wave_lines where wave_line_id in
12757 			  (select wave_line_id from wms_wp_wave_lines wwl, wsh_delivery_assignments wda, wsh_delivery_details wdd
12758 			  where wwl.delivery_detail_id = wdd.delivery_detail_id and wwl.organization_id = wdd.organization_id and wdd.delivery_detail_id = wda.delivery_detail_id and wda.delivery_id not in
12759 (
12760 select delivery_id from
12761 (
12762 select  min(wwwl.WAVE_LINE_ID)  min_line_id, wnd.delivery_id
12763 from WMS_WP_WAVE_LINES wwwl, wsh_delivery_details wdd,
12764 WSH_NEW_DELIVERIES wnd,  WSH_DELIVERY_ASSIGNMENTS WDA
12765 where wave_header_id = :p_wave_header_id
12766 and  wnd.DELIVERY_ID(+) = WDA.DELIVERY_ID
12767 AND   WDA.DELIVERY_DETAIL_ID = WDD.DELIVERY_DETAIL_ID
12768 AND wwwl.delivery_Detail_id = WDD.DELIVERY_DETAIL_ID
12769 and wdd.organization_id = wwwl.organization_id
12770 AND wnd.delivery_id IS NOT null
12771                                group by wnd.delivery_id
12772                               order by min_line_id)' ||
12773                                 ' where rownum ' || C_REC.OPERATOR_MEANING ||
12774                                 C_REC.OPERAND_VALUE || '))' ||
12775                                 ' and wave_header_id = :wave_header_id and delivery_id is not null '
12776                 using p_wave_header_id, p_wave_header_id;
12777 
12778               print_debug('No of Lines deleted based on condition Delivery Count is  ' ||
12779                           SQL%ROWCOUNT,
12780                           l_debug);
12781 
12782             elsif c_rec.parameter_id = 6006 then
12783 
12784               --Item Count
12785 			  --11775489 get from wdd... FIXED
12786               EXECUTE IMMEDIATE 'delete from wms_wp_wave_lines where wave_line_id IN
12787 (SELECT wwl.wave_line_id FROM wms_wp_wave_lines wwl, wsh_delivery_details wdd
12788 WHERE wwl.organization_id = wdd.organization_id AND wwl.delivery_detail_id = wdd.delivery_detail_id AND wdd.inventory_item_id not in
12789 (
12790 
12791 select inventory_item_id from
12792 (
12793 select  min(wwwl.WAVE_LINE_ID)  min_line_id, wdd.inventory_item_id
12794 from WMS_WP_WAVE_LINES wwwl, wsh_delivery_details wdd
12795 where wwwl.wave_header_id = :wave_header_id and wwwl.delivery_detail_id  = wdd.delivery_detail_id and wdd.organization_id = wwwl.organization_id
12796                                group by wdd.inventory_item_id
12797                               order by min_line_id) ' ||
12798                                 ' where rownum ' || C_REC.OPERATOR_MEANING ||
12799                                 C_REC.OPERAND_VALUE || '))' ||
12800                                 ' and wave_header_id = :wave_header_id'
12801                 using p_wave_header_id, p_wave_header_id;
12802 
12803               print_debug('No of Lines deleted based on condition Item Count is  ' ||
12804                           SQL%ROWCOUNT,
12805                           l_debug);
12806 
12807             end if;
12808           end if;
12809 
12810         end loop;
12811 
12812         for c_rec1 in c_get_wave_line_count loop
12813           exit when c_get_wave_line_count%NOTFOUND;
12814           if c_rec1.operator_meaning in ('>', '>=', '=') then
12815 
12816             -- Line Count
12817             select count(1)
12818               into l_count
12819               from wms_wp_wave_lines
12820              where wave_header_id = p_wave_header_id;
12821 
12822             print_debug('Entered Wave Parameter with >, >= ,= operators  --- > Line count ',
12823                         l_debug);
12824 
12825             print_debug(' Line count  in Lines in the Wave is ' || l_count,
12826                         l_debug);
12827 
12828             print_debug(' Line count  given in the Criteria is  ' ||
12829                         C_REC1.OPERAND_VALUE,
12830                         l_debug);
12831 
12832             if c_rec1.operator_meaning = '=' then
12833 
12834               if l_count = C_REC1.OPERAND_VALUE then
12835                 print_debug('Criteria is satisfied. So not deleting any lines ',
12836                             l_debug);
12837               else
12838                 delete from wms_wp_wave_lines
12839                  where wave_header_id = p_wave_header_id;
12840 
12841               end if;
12842             elsif c_rec1.operator_meaning = '>=' then
12843 
12844               if l_count >= C_REC1.OPERAND_VALUE then
12845                 print_debug('Criteria is satisfied. So not deleting any lines ',
12846                             l_debug);
12847               else
12848                 delete from wms_wp_wave_lines
12849                  where wave_header_id = p_wave_header_id;
12850 
12851               end if;
12852             elsif c_rec1.operator_meaning = '>' then
12853 
12854               if l_count > C_REC1.OPERAND_VALUE then
12855                 print_debug('Criteria is satisfied. So not deleting any lines ',
12856                             l_debug);
12857               else
12858                 delete from wms_wp_wave_lines
12859                  where wave_header_id = p_wave_header_id;
12860 
12861               end if;
12862 
12863             end if;
12864           else
12865 
12866             -- Line Count
12867             /*
12868             v_wave_sql := 'delete from wms_wp_wave_lines where rownum ' ||
12869                           C_REC1.OPERATOR_MEANING || C_REC1.OPERAND_VALUE ||
12870                           ' and wave_header_id = ' || p_wave_header_id;
12871             */
12872             EXECUTE IMMEDIATE 'delete from wms_wp_wave_lines where wave_line_id not in
12873           (
12874           select wave_line_id from wms_wp_wave_lines where wave_header_id = :wave_header_id and rownum' ||
12875                               C_REC1.OPERATOR_MEANING ||
12876                               C_REC1.OPERAND_VALUE || ' )' ||
12877                               ' and wave_header_id = :wave_header_id'
12878               using p_wave_header_id, p_wave_header_id;
12879 
12880             print_debug('No of Lines deleted based on condition Line Count is  ' ||
12881                         SQL%ROWCOUNT,
12882                         l_debug);
12883           end if;
12884         end loop;
12885 
12886         print_debug('Firmed Flag is ' || l_firmed_flag, l_debug);
12887 
12888         if l_firmed_flag = 'Y' then
12889           UPDATE wms_wp_wave_lines
12890              SET message               = 'This line has been Firmed in Wave ' ||
12891                                          p_wave_header_id,
12892                  remove_from_Wave_flag = 'Y'
12893            WHERE delivery_detail_id IN
12894                  (SELECT wwl.delivery_detail_id --11775489 get from wdd... FIXED
12895                     FROM wms_wp_wave_lines wwl, wsh_delivery_details wdd
12896                    WHERE wave_header_id = p_wave_header_id
12897 				     and wwl.delivery_detail_id = wdd.delivery_detail_id
12898 					 and wwl.organization_id = wdd.organization_id
12899                      and nvl(remove_from_wave_flag, 'N') <> 'Y')
12900              and wave_header_id <> p_wave_header_id;
12901         end if;
12902 
12903       wms_wp_custom_apis_pub.create_wave_lines_cust(p_wave_header_id         => p_wave_header_id,
12904                                                     x_api_is_implemented     => l_api_is_implemented,
12905                                                     x_custom_line_tbl        => l_custom_line_tbl,
12906                                                     x_custom_line_action_tbl => l_custom_line_action_tbl,
12907                                                     x_return_status          => l_return_status,
12908                                                     x_msg_count              => l_msg_count,
12909                                                     x_msg_data               => l_msg_data);
12910 
12911       IF l_api_is_implemented AND
12912          l_return_status = fnd_api.g_ret_sts_success THEN
12913         print_debug('Custom API Implemented for create wave lines ',
12914                     l_debug);
12915                     l_tbl_count := l_custom_line_tbl.count;
12916 
12917        -- FORall i IN 0 .. l_tbl_count dbchange15
12918        FOR i IN l_custom_line_tbl.FIRST .. l_custom_line_tbl.LAST LOOP
12919           DELETE FROM wms_wp_wave_lines
12920            WHERE DELIVERY_DETAIL_ID = l_custom_line_tbl(i).DELIVERY_DETAIL_ID
12921              AND l_custom_line_action_tbl(i) = 'REMOVE'
12922              AND wave_header_id = p_wave_header_id;
12923            end loop;
12924 
12925         print_debug('Deleted ' || SQL%ROWCOUNT ||
12926                     ' lines from wms_wp_wave_lines using custom API returned lines',
12927                     l_debug);
12928 
12929         FOR i IN l_custom_line_action_tbl.first .. l_custom_line_action_tbl.last loop
12930           IF l_custom_line_action_tbl(i) = 'ADD' THEN
12931             SELECT released_status, Nvl(replenishment_status, 'C')
12932               INTO l_released_status, l_replenishment_status
12933               FROM wsh_delivery_details
12934              WHERE delivery_detail_id = l_custom_line_tbl(i)
12935             .delivery_detail_id;
12936 
12937             IF (l_released_status = 'R' OR l_released_status = 'B') and
12938                l_replenishment_status = 'C' THEN
12939               DECLARE
12940                 a number;
12941               begin
12942                 SELECT (1)
12943                   INTO a
12944                   FROM dual
12945                  WHERE l_custom_line_tbl(i)
12946                 .delivery_detail_id IN
12947                        (SELECT distinct wwl.delivery_detail_id
12948                           FROM WMS_WP_WAVE_LINES      wwl,
12949                                wms_wp_wave_headers_vl wwh
12950                           WHERE wwl.wave_header_id = p_wave_header_id -- Bug 13448469 (Replaced 367 with p_wave_header_id)
12951                             OR (wwl.wave_header_id = wwh.wave_header_id and
12952                                nvl(wwh.wave_firmed_flag, 'N') = 'Y' and
12953                                wwh.wave_status <> 'Cancelled' and
12954                                nvl(wwl.remove_from_wave_flag, 'N') <> 'Y'));
12955 
12956                 print_debug('Not inserting detail line ' ||
12957                             l_custom_line_tbl(i)
12958                             .delivery_detail_id ||
12959                             ' returned by custom API as line already added to the current wave or it is firmed in other wave ',
12960                             l_debug);
12961 
12962                 l_custom_line_tbl.delete(i);
12963                 l_custom_line_action_tbl.delete(i);
12964               EXCEPTION
12965                 WHEN No_Data_Found THEN
12966                   print_debug('Detail line ' || l_custom_line_tbl(i)
12967                               .delivery_detail_id ||
12968                               ' from custom API will be inserted into lines table',
12969                               l_debug);
12970               END;
12971             ELSE
12972               print_debug('Not inserting detail line ' ||
12973                           l_custom_line_tbl(i)
12974                           .delivery_detail_id ||
12975                           ' returned by custom API as replenishment is not completed yet',
12976                           l_debug);
12977               l_custom_line_tbl.delete(i);
12978               l_custom_line_action_tbl.delete(i);
12979             END IF;
12980           ELSE
12981             -- l_custom_line_action_tbl(i)='REMOVE' or some other value
12982             l_custom_line_tbl.delete(i);
12983             l_custom_line_action_tbl.delete(i);
12984           END IF;
12985         END LOOP;
12986 
12987         FORALL i IN l_custom_line_tbl.first .. l_custom_line_tbl.last
12988           INSERT INTO wms_wp_wave_lines VALUES l_custom_line_tbl (i);
12989 
12990         print_debug('Inserted ' || SQL%ROWCOUNT ||
12991                     ' detail lines returned by custom API into wms_wp_wave_lines ',
12992                     l_debug);
12993 
12994       END IF; -- custom API implemented
12995 
12996     end if;
12997 
12998     x_return_status := l_return_status;
12999 
13000   EXCEPTION
13001     WHEN OTHERS THEN
13002       print_debug('Unexpected error occurred in Get Dynamic SQL', l_debug);
13003       print_debug('Error : ' || SQLCODE || ' : ' || SQLERRM, l_debug);
13004       x_return_status := 'E';
13005   end get_dynamic_sql;
13006 
13007   PROCEDURE Init_Cursor(p_organization_id IN NUMBER,
13008                         v_advanced_sql    in varchar2,
13009                         V_WAVE_HEADER_ID  in NUMBER,
13010                         x_api_status      OUT NOCOPY VARCHAR2) IS
13011     l_debug              NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'),
13012                                        0);
13013     l_cs                 NUMBER;
13014     l_strlen             NUMBER;
13015     i                    NUMBER;
13016     g_advanced_sql       varchar2(4000);
13017     G_WHERE_SQL          VARCHAR2(32000);
13018     V_CURSOR_FLAG        VARCHAR2(1);
13019     ajith                varchar2(400);
13020     v_single_line_flag   varchar2(1) := 'N';
13021     v_delivery_flag      varchar2(1) := 'N';
13022     v_quantity_flag      varchar2(1) := 'N';
13023     v_ordercount_flag    varchar2(1) := 'N';
13024     v_item_flag          varchar2(1) := 'N';
13025     v_customerclass_flag varchar2(1) := 'N';
13026 
13027     -- Phase II Changes
13028     v_carrier_order_flag varchar2(1) := 'N';
13029     v_order_line_flag    varchar2(1) := 'N';
13030     v_trip_flag          varchar2(1) := 'N';
13031     v_cat_flag           varchar2(1) := 'N';
13032 
13033     CURSOR C_ADVANCED_CONDITION IS
13034       SELECT PARAMETER_ID
13035         FROM WMS_WP_ADVANCED_CRITERIA
13036        WHERE RULE_WAVE_HEADER_ID = V_WAVE_HEADER_ID
13037          AND PARAMETER_ID IN
13038              (1006, 2001, 2002, 3002, 5001, 5002, 5003, 5004, 5005, 5006, 3001, 1011, 1004, 2006, 2004, 2005, 4002);
13039   BEGIN
13040     --
13041     -- Make sure the first_line record has header_id set to -1
13042     -- This is used to determine whether the Get_Lines
13043     -- is called for the first time or not. The first_line is set
13044     -- as a dummy line
13045 
13046     print_debug('Entered Init Cursor ', l_debug);
13047     first_line.source_header_id := -1;
13048     g_del_current_line          := 1;
13049     g_advanced_sql              := v_advanced_sql;
13050     V_CURSOR_FLAG               := 'Y';
13051 
13052   SELECT  enforce_ship_set_and_smc
13053   INTO  g_enforce_ship_set_and_smc
13054     FROM   WSH_SHIPPING_PARAMETERS WSP
13055     WHERE    WSP.ORGANIZATION_ID = p_organization_id;
13056 
13057 
13058 
13059     G_WHERE_SQL := 'WHERE ';
13060     -- Selection for unreleased lines
13061     g_Unreleased_SQL := 'SELECT distinct WDD.SOURCE_CODE,' ||
13062                         '       WDD.SOURCE_HEADER_ID,' ||
13063                         '       WDD.SOURCE_LINE_ID,' ||
13064                         '       WDD.SOURCE_HEADER_NUMBER,' ||
13065                         '       WDD.SOURCE_LINE_NUMBER,' ||
13066                         '       WDD.SOURCE_HEADER_TYPE_NAME,' ||
13067                         '       WDD.SOURCE_HEADER_TYPE_ID,' ||
13068                         '       WDD.SOURCE_DOCUMENT_TYPE_ID,' ||
13069                         '       WDD.DELIVERY_DETAIL_ID,' ||
13070                         '       WDD.RELEASED_STATUS,' ||
13071                         '       WDD.ORGANIZATION_ID,' ||
13072                         '       WDD.INVENTORY_ITEM_ID,' ||
13073                         '       WDD.REQUESTED_QUANTITY,' ||
13074                         '       WDD.REQUESTED_QUANTITY_UOM,' ||
13075                         '       WDD.MOVE_ORDER_LINE_ID,' ||
13076                         '       WDD.SHIP_MODEL_COMPLETE_FLAG,' ||
13077                         '       WDD.TOP_MODEL_LINE_ID,' ||
13078                         '       WDD.SHIP_FROM_LOCATION_ID,' ||
13079                         '       WDD.SHIP_TO_LOCATION_ID,' ||
13080                         '       WDD.SHIP_METHOD_CODE,' ||
13081                         '       WDD.SHIPMENT_PRIORITY_CODE,' ||
13082                         '       WDD.SHIP_SET_ID,' ||
13083                         '       WDD.DATE_SCHEDULED,' ||
13084                         '       WTS.PLANNED_DEPARTURE_DATE,' ||
13085                         '       WDA.DELIVERY_ID,' ||
13086                         '       WDD.CUSTOMER_ID,' ||
13087                         '       WDD.CARRIER_ID,' ||
13088                         '       WDD.PREFERRED_GRADE,' ||
13089                         '       WDD.SRC_REQUESTED_QUANTITY2,' ||
13090                         '       WDD.SRC_REQUESTED_QUANTITY_UOM2,' ||
13091                         '       WDD.PROJECT_ID,' || '       WDD.TASK_ID,' ||
13092                         '       WDD.SUBINVENTORY,' ||
13093                         '       WDD.WEIGHT_UOM_CODE,' ||
13094                         '       WDD.NET_WEIGHT,' || --????? Is it Net Weight or Gross Weight
13095                         '       WDD.VOLUME_UOM_CODE,' ||
13096                         '       WDD.VOLUME '; -- Ajith Changed Need to Check
13097     g_Unreleased_SQL := g_Unreleased_SQL ||
13098                         ' FROM  WSH_DELIVERY_DETAILS WDD,' ||
13099                         '       WSH_NEW_DELIVERIES WDE,' ||
13100                         '       WSH_DELIVERY_ASSIGNMENTS WDA,' ||
13101                         '       WSH_DELIVERY_LEGS WLG,' ||
13102                         '       WSH_TRIP_STOPS WTS, ' ||
13103                         '       WMS_DOCK_APPOINTMENTS_B WDO ';
13104 
13105     FOR CUR_REC IN C_ADVANCED_CONDITION LOOP
13106 
13107       IF CUR_REC.PARAMETER_ID = 2002 THEN
13108         if v_quantity_flag = 'N' then
13109           g_Unreleased_SQL := g_Unreleased_SQL ||
13110                               ',(SELECT SUM(WDD1.SRC_REQUESTED_QUANTITY) QUANTITY, WDD1.SOURCE_LINE_ID FROM WSH_DELIVERY_DETAILS WDD1
13111                            GROUP BY WDD1.SOURCE_LINE_ID) SALES_ORDER_LINE ';
13112 
13113           g_WHERE_SQL := g_WHERE_SQL ||
13114                          ' WDD.SOURCE_LINE_ID=SALES_ORDER_LINE.SOURCE_LINE_ID  AND ';
13115 
13116         end if;
13117         v_quantity_flag := 'Y';
13118         --   g_WHERE_SQL := g_WHERE_SQL || ' '||g_advanced_sql;
13119 
13120       ELSIF CUR_REC.PARAMETER_ID = 3002 THEN
13121         if v_ordercount_flag = 'N' then
13122           g_Unreleased_SQL := g_Unreleased_SQL ||
13123                               ',(SELECT COUNT(distinct SOURCE_HEADER_NUMBER) ORDER_COUNT , WDE.DELIVERY_ID DELIVERY_ID FROM WSH_DELIVERY_DETAILS WDD ,WSH_NEW_DELIVERIES WDE, WSH_DELIVERY_ASSIGNMENTS WDA
13124                                                 WHERE  WDE.DELIVERY_ID = WDA.DELIVERY_ID AND   WDA.DELIVERY_DETAIL_ID = WDD.DELIVERY_DETAIL_ID
13125                                                  GROUP BY WDE.DELIVERY_ID) DELIVERY ';
13126           g_WHERE_SQL      := g_WHERE_SQL ||
13127                               ' WDE.DELIVERY_ID=DELIVERY.DELIVERY_ID  AND ';
13128           -- g_WHERE_SQL := g_WHERE_SQL || ' '||g_advanced_sql;
13129         end if;
13130         v_ordercount_flag := 'Y';
13131         /*
13132         ELSIF CUR_REC.PARAMETER_ID = 1005 THEN
13133           --SINGLE LINE ORDER
13134           g_WHERE_SQL        := g_WHERE_SQL || g_ADVANCED_SQL ||
13135                                 ' select 1  FROM wsh_delivery_details wdd1 where wdd1.source_header_number = wdd.source_header_number GROUP BY SOURCE_HEADER_NUMBER HAVING Count(SOURCE_HEADER_NUMBER)=1)';
13136           v_single_line_flag := 'Y';
13137         */
13138       elsif cur_rec.parameter_id IN (5001, 5002, 5003, 5004, 2001, 5005) then
13139         --ITEM
13140         if v_item_flag = 'N' then
13141           g_Unreleased_SQL := g_Unreleased_SQL ||
13142                               ',MTL_SYSTEM_ITEMS_B MSIB ';
13143           g_WHERE_SQL      := g_WHERE_SQL ||
13144                               ' WDD.INVENTORY_ITEM_ID=MSIB.INVENTORY_ITEM_ID  AND ';
13145         end if;
13146         v_item_flag := 'Y';
13147         -- g_WHERE_SQL := g_WHERE_SQL || ' '||g_advanced_sql;
13148       elsif cur_rec.parameter_id IN (4002) then
13149         --TRIP Carrier
13150         if v_trip_flag = 'N' then
13151           g_Unreleased_SQL := g_Unreleased_SQL || ',WSH_TRIPS WT ';
13152           g_WHERE_SQL      := g_WHERE_SQL ||
13153                               ' WTS.TRIP_ID=WT.TRIP_ID  AND ';
13154         end if;
13155         v_trip_flag := 'Y';
13156 
13157       elsif cur_rec.parameter_id IN (5006) then
13158         --Item Category
13159         if v_cat_flag = 'N' then
13160           g_Unreleased_SQL := g_Unreleased_SQL ||
13161                               ',MTL_ITEM_CATEGORIES MIC ';
13162           g_WHERE_SQL      := g_WHERE_SQL ||
13163                               ' MIC.INVENTORY_ITEM_ID=WDD.INVENTORY_ITEM_ID  AND ';
13164         end if;
13165         v_cat_flag := 'Y';
13166 
13167       elsif cur_rec.parameter_id IN (1011, 1004) then
13168         -- Ship Method, Carrier, Scheduled Ship Date
13169         if v_carrier_order_flag = 'N' then
13170           g_Unreleased_SQL := g_Unreleased_SQL ||
13171                               ',oe_order_headers_all OOHA ';
13172           g_WHERE_SQL      := g_WHERE_SQL ||
13173                               ' WDD.source_header_id=OOHA.header_id  AND ';
13174         end if;
13175         v_carrier_order_flag := 'Y';
13176         -- g_WHERE_SQL := g_WHERE_SQL || ' '||g_advanced_sql;
13177 
13178       elsif cur_rec.parameter_id IN (2004, 2005, 2006) then
13179         -- Ship Method, Carrier, Scheduled Ship Date
13180         if v_order_line_flag = 'N' then
13181           g_Unreleased_SQL := g_Unreleased_SQL ||
13182                               ',oe_order_lines_all OOLA ';
13183           g_WHERE_SQL      := g_WHERE_SQL ||
13184                               ' WDD.source_header_id=OOLA.header_id  AND WDD.SOURCE_LINE_ID=OOLA.LINE_ID and ';
13185         end if;
13186         v_order_line_flag := 'Y';
13187         -- g_WHERE_SQL := g_WHERE_SQL || ' '||g_advanced_sql;
13188 
13189       ELSIF CUR_REC.PARAMETER_ID = 1006 THEN
13190         --CUSTOMER CATEGORY
13191         if v_customerclass_flag = 'N' then
13192           g_Unreleased_SQL := g_Unreleased_SQL ||
13193                               ',(select  cust_Acct.customer_class_code customer_class_code,
13194           cust_acct.cust_account_id cust_account_id from
13195  hz_parties PARTY
13196 , hz_cust_accounts CUST_ACCT
13197 where party.party_id = cust_acct.party_id
13198 and cust_acct.status = ''A'') CUSTOMER_CLASS  ';
13199 
13200           g_WHERE_SQL := g_WHERE_SQL ||
13201                          ' CUSTOMER_CLASS.cust_account_id = WDD.CUSTOMER_ID AND ';
13202         end if;
13203         v_customerclass_flag := 'Y';
13204       elsif CUR_REC.PARAMETER_ID = 3001 then
13205 
13206         --Putting g_Delivery_id as -1 if delivery id is chosen in advanced criteria tab
13207 
13208         g_DELIVERY_ID := -1;
13209 
13210       end if;
13211 
13212       IF C_ADVANCED_CONDITION%ROWCOUNT > 0 THEN
13213 
13214         V_CURSOR_FLAG := 'N';
13215       END IF;
13216 
13217     END LOOP;
13218     if v_single_line_flag <> 'Y' then
13219       IF V_CURSOR_FLAG = 'Y' THEN
13220         g_WHERE_SQL := g_WHERE_SQL || G_ADVANCED_SQL;
13221 
13222       else
13223 
13224         g_WHERE_SQL := g_WHERE_SQL || ' ' || g_advanced_sql;
13225       END IF;
13226     end if;
13227 
13228     if g_advanced_sql is null then
13229       g_WHERE_SQL := g_WHERE_SQL || ' 1=1 ';
13230     end if;
13231     g_Unreleased_SQL := g_Unreleased_SQL || G_WHERE_SQL ||
13232                         ' AND   WDD.RELEASED_STATUS IN (''R'',''B'',''X'') ' ||
13233                         ' AND   nvl(WDD.replenishment_status,''C'') = ''C''' ||
13234                         ' AND   WDD.DATE_SCHEDULED IS NOT NULL' ||
13235                         ' AND   WTS.STOP_ID(+) = WLG.PICK_UP_STOP_ID ' ||
13236                         ' AND   WLG.DELIVERY_ID(+) = WDE.DELIVERY_ID ' ||
13237                         ' AND   WDE.DELIVERY_ID(+) = WDA.DELIVERY_ID ' ||
13238                         ' AND   WDA.DELIVERY_DETAIL_ID = WDD.DELIVERY_DETAIL_ID' ||
13239                         ' AND   WTS.STOP_ID = WDO.TRIP_STOP(+) ' || --Added for Dock Assignments
13240                         ' AND   NVL(WDD.REQUESTED_QUANTITY,0) > 0' ||
13241                         ' AND   WDD.SOURCE_CODE = ''OE''' ||
13242                         ' AND  WDD.DELIVERY_DETAIL_ID NOT IN' || --11775489 get from wdd... FIXED
13243                         '(SELECT distinct wwl.delivery_detail_id  FROM WMS_WP_WAVE_LINES wwl ,wms_wp_wave_headers_vl wwh, wsh_delivery_details wdd
13244                          where wwl.wave_header_id=wwh.wave_header_id and wwl.delivery_detail_id = wdd.delivery_detail_id
13245 						 and wwl.organization_id = wdd.organization_id and (nvl(wwh.wave_firmed_flag,''N'') = ''Y''  and wwh.wave_status <> ''Cancelled''  and nvl(wwl.remove_from_wave_flag,''N'') <>''Y''))';
13246     -- WHERE WAVE_HEADER_ID = :X_WAVE_HEADER_ID)';
13247 
13248     -- The above condition is added to check if the lines are present in Wave Lines table
13249     -- Following conditions use bind variables
13250     -- The columns which have indexes are included in the SQL statement
13251     -- conditionally.  This is done so that the index will be used
13252 
13253     if nvl(g_Add_lines, 'N') = 'Y' then
13254       g_Unreleased_SQL := g_Unreleased_SQL ||
13255                           ' AND  WDD.DELIVERY_DETAIL_ID NOT IN' ||
13256                           '(select distinct delivery_detail_id from wms_wp_Wave_lines where wave_header_id=:X_wave_header_id)';
13257 
13258     end if;
13259 
13260     g_cond_SQL := '';
13261 
13262     IF (g_customer_class_id is not null) THEN
13263       g_cond_SQL := g_Cond_SQL || ' AND WDD.CUSTOMER_ID IN  (select cust_acct.cust_account_id
13264 from hz_parties PARTY
13265 , hz_cust_accounts CUST_ACCT
13266 where party.party_id = cust_acct.party_id
13267 and cust_acct.status = ''A''' ||
13268                     ' AND cust_Acct.customer_class_code = :X_CUSTOMER_CLASS_ID) ' || '';
13269       print_debug('g_cond_sql  after customer class code :' || g_cond_sql,
13270                   l_debug);
13271     END IF;
13272 
13273     -- For Item Category
13274     IF (g_item_category_id <> 0) THEN
13275       g_cond_SQL := g_Cond_SQL || ' and wdd.inventory_item_id in (select distinct inventory_item_id
13276 from mtl_item_categories mic
13277 where mic.category_id =:X_category_id) ' || '';
13278     END IF;
13279 
13280     -- Ajith Changed this part
13281     IF (g_order_header_id <> 0) THEN
13282       g_Cond_SQL := g_Cond_SQL ||
13283                     ' AND WDD.SOURCE_HEADER_ID = :X_header_id ' || '';
13284       /*
13285           ELSIF (g_order_header_id <> 0 AND g_order_line_id <> 0) THEN
13286       g_Cond_SQL := g_Cond_SQL ||
13287       ' AND WDD.SOURCE_HEADER_ID + 0 = :X_header_id' || '';*/
13288     END IF;
13289     /*  IF (g_order_line_id <> 0) THEN
13290         g_Cond_SQL := g_Cond_SQL ||
13291     ' AND WDD.SOURCE_LINE_ID = :X_order_line_id' || '';
13292     END IF;*/
13293     IF (g_customer_id <> 0) THEN
13294       g_Cond_SQL := g_Cond_SQL || ' AND WDD.CUSTOMER_ID = :X_customer_id' || '';
13295     END IF;
13296     IF (g_carrier_id is not null) THEN
13297       g_Cond_SQL := g_Cond_SQL ||
13298                     ' AND (WDD.CARRIER_ID = :X_carrier_id OR :X_carrier_id = 0)' || '';
13299     END IF;
13300     IF (g_ship_from_loc_id <> -1) THEN
13301       g_Cond_SQL := g_Cond_SQL ||
13302                     ' AND WDD.SHIP_FROM_LOCATION_ID = :X_ship_from_loc_id' || '';
13303     END IF;
13304     IF (g_ship_to_loc_id <> 0) THEN
13305       g_Cond_SQL := g_Cond_SQL ||
13306                     ' AND WDD.SHIP_TO_LOCATION_ID = :X_ship_to_loc_id' || '';
13307     END IF;
13308     g_Cond_SQL := g_Cond_SQL ||
13309                   ' AND (WDD.SOURCE_HEADER_TYPE_ID = :X_order_type_id OR :X_order_type_id = 0) ' || '' ||
13310                  --    ' AND (WDD.SHIP_SET_ID = :X_ship_set_id OR :X_ship_set_id = 0) ' || '' ||
13311                   ' AND (WDD.TASK_ID = :X_task_id OR :X_task_id = 0) ' || '' ||
13312                   ' AND (WDD.PROJECT_ID = :X_project_id OR :X_project_id = 0) ' || '' ||
13313                   ' AND (WDD.ORGANIZATION_ID = :X_org_id' || '' ||
13314                   '      OR  :X_org_id IS NULL) ' || '' ||
13315                   ' AND (WDD.SHIP_METHOD_CODE =  :X_ship_method_code ' || '' ||
13316                   '      OR  :X_ship_method_code IS NULL) ' || '' ||
13317                   ' AND (WDD.SHIPMENT_PRIORITY_CODE = :X_shipment_priority ' || '' ||
13318                   '     OR :X_shipment_priority IS NULL) ' || '' ||
13319                   ' AND (WDD.DATE_REQUESTED >=' ||
13320                   '         :X_from_request_date OR :X_from_request_date IS NULL) ' || '' ||
13321                   ' AND (WDD.DATE_REQUESTED <=' ||
13322                   '   :X_to_request_date OR :X_to_request_date IS NULL) ' || '' ||
13323                   ' AND (WDD.DATE_SCHEDULED >= :X_from_sched_ship_date OR :X_from_sched_ship_date IS NULL) ' || '' ||
13324                   ' AND (WDD.DATE_SCHEDULED <=' ||
13325                   '   :X_to_sched_ship_date OR :X_to_sched_ship_date IS NULL) ' || '' ||
13326                   ' AND (WDD.INVENTORY_ITEM_ID  = :X_inventory_item_id ' ||
13327                   '   OR :X_inventory_item_id = 0) ' || '' ||
13328                   ' AND (NVL(WDE.DELIVERY_ID, -99) = -99 ' ||
13329                   '   OR :X_include_planned_lines <> ''N'' ' ||
13330                   '   OR (:X_trip_id <> 0 and :X_include_planned_lines <> ''N'') OR (:X_delivery_id <> 0 and :X_include_planned_lines <> ''N'') OR (WDE.DELIVERY_ID IS NOT NULL and :X_include_planned_lines <> ''N''))';
13331     /*
13332                       if g_delivery_id <> -1 then
13333               g_Cond_SQL := g_Cond_SQL || ' AND (NVL(WDE.DELIVERY_ID, -99) = -99 ' ||
13334                       '   OR :X_include_planned_lines <> ''N'' ' ||
13335                       '         OR :X_trip_id <> 0 OR :X_delivery_id <> 0)';
13336                       elsif g_delivery_id = -1then
13337                        g_Cond_SQL := g_Cond_SQL || ' AND (NVL(WDE.DELIVERY_ID, -99) = -99 ' ||
13338                       '   OR :X_include_planned_lines <> ''N'' ' ||
13339                       '         OR :X_trip_id <> 0 OR :X_delivery_id = -1)';
13340                       end if;
13341 
13342     */
13343     -- Handling trips and deliveries
13344     IF (g_delivery_id <> 0) and (g_delivery_id <> -1) THEN
13345       g_Cond_SQL := g_Cond_SQL || ' AND WDE.DELIVERY_ID = :X_delivery_id' || '';
13346     END IF;
13347     IF (g_trip_id <> 0) THEN
13348       g_Cond_SQL := g_Cond_SQL || ' AND WTS.TRIP_ID = :X_trip_id ' || '' ||
13349                     ' AND ( WTS.STOP_ID = :X_trip_stop_id' ||
13350                     '       OR  :X_trip_stop_id = 0) ' || '';
13351 
13352     end if;
13353 
13354     if (g_from_dock_appoint_date is not null) then
13355       g_Cond_SQL := g_Cond_SQL ||
13356                     ' AND (WDO.START_TIME >= sysdate or :X_from_dock_appoint_date IS NULL )' ||
13357                     ' AND (WDO.END_TIME <= :X_to_dock_appoint_date OR :X_to_dock_appoint_date IS NULL)';
13358     end if;
13359 
13360     -- Determine the order by clause
13361     g_orderby_SQL := 'ORDER BY ';
13362 
13363  FOR i IN 1 .. g_total_pick_criteria LOOP
13364       IF (g_ordered_psr(i).attribute = C_INVOICE_VALUE) THEN
13365 
13366         g_orderby_SQL := g_orderby_SQL ||
13367                          ' WSH_PICK_CUSTOM.OUTSTANDING_ORDER_VALUE(WDD.SOURCE_HEADER_ID) ' ||
13368                          g_ordered_psr(i)
13369                         .sort_order || ' , SOURCE_HEADER_ID ASC ,';
13370 
13371       ELSIF (g_ordered_psr(i).attribute = C_ORDER_NUMBER) THEN
13372         g_orderby_SQL := g_orderby_SQL || ' SOURCE_HEADER_ID ' ||
13373                          g_ordered_psr(i).sort_order || ', ';
13374       ELSIF (g_ordered_psr(i).attribute = C_SCHEDULE_DATE) THEN
13375         g_orderby_SQL := g_orderby_SQL || ' DATE_SCHEDULED ' ||
13376                          g_ordered_psr(i).sort_order || ', ';
13377       ELSIF (g_ordered_psr(i).attribute = C_TRIP_STOP_DATE) THEN
13378         g_orderby_SQL := g_orderby_SQL || 'PLANNED_DEPARTURE_DATE ' ||
13379                          g_ordered_psr(i).sort_order || ', ';
13380       ELSIF (g_ordered_psr(i).attribute = C_SHIPMENT_PRIORITY) THEN
13381         g_orderby_SQL := g_orderby_SQL || 'SHIPMENT_PRIORITY_CODE ' ||
13382                          g_ordered_psr(i).sort_order || ', ';
13383       END IF;
13384     END LOOP;
13385 
13386 if g_enforce_ship_set_and_smc = 'Y' then
13387 
13388 		           g_orderby_SQL := g_orderby_SQL || ' NVL(WDD.SHIP_SET_ID,999999999), ';
13389 
13390 	        -- Consider SMC only if SS is not specified
13391 	   g_orderby_SQL := g_orderby_SQL || ' DECODE(NVL(WDD.SHIP_SET_ID,-999999999), -999999999, WDD.SHIP_MODEL_COMPLETE_FLAG,NULL) DESC, ';
13392 
13393 	        -- This is necessary to push the non-transactable lines ahead in SS/SMC
13394 		g_orderby_SQL := g_orderby_SQL ||' RELEASED_STATUS DESC, ';
13395 
13396 	        -- Consider SMC only if SS is not specified
13397 		g_orderby_SQL := g_orderby_SQL || ' DECODE(NVL(WDD.SHIP_SET_ID,-999999999), -999999999,WDD.TOP_MODEL_LINE_ID,NULL), ';
13398 			g_orderby_SQL := g_orderby_SQL || ' WDD.INVENTORY_ITEM_ID, ';
13399 
13400 
13401 end if;
13402 
13403     g_orderby_SQL := g_orderby_SQL || ' WDD.SOURCE_LINE_ID, ' ||
13404                      ' SHIP_FROM_LOCATION_ID, ' || ' SHIP_METHOD_CODE';
13405     -- Parse cursor
13406     v_CursorID := DBMS_SQL.Open_Cursor;
13407     print_debug('Parse cursor', l_debug);
13408 
13409     print_debug('Printing Dynamic SQL', l_debug);
13410     print_debug('g_unreleased_sql :' || g_unreleased_sql, l_debug);
13411 
13412     print_debug('g_where_sql :' || g_where_sql, l_debug);
13413     print_debug('g_cond_sql  :' || g_cond_sql, l_debug);
13414     print_debug('g_orderby_sql  :' || g_orderby_sql, l_debug);
13415 
13416     DBMS_SQL.Parse(v_CursorID,
13417                    '( ' || g_Unreleased_SQL || g_Cond_SQL || ' ) ' ||
13418                    g_orderby_SQL,
13419                    DBMS_SQL.v7);
13420 
13421     print_debug('Column definition for cursor', l_debug);
13422     DBMS_SQL.Define_Column(v_CursorID, 1, v_source_code, 30);
13423     DBMS_SQL.Define_Column(v_CursorID, 2, v_header_id);
13424     DBMS_SQL.Define_Column(v_CursorID, 3, v_line_id);
13425     DBMS_SQL.Define_Column(v_CursorID, 4, v_header_number, 150);
13426     DBMS_SQL.Define_Column(v_CursorID, 5, v_line_number, 150);
13427     DBMS_SQL.Define_Column(v_CursorID, 6, v_header_type_name, 240);
13428     DBMS_SQL.Define_Column(v_CursorID, 7, v_header_type_id);
13429     DBMS_SQL.Define_Column(v_CursorID, 8, v_document_type_id);
13430     DBMS_SQL.Define_Column(v_CursorID, 9, v_delivery_detail_id);
13431     DBMS_SQL.Define_Column(v_CursorID, 10, v_released_status, 1);
13432     DBMS_SQL.Define_Column(v_CursorID, 11, v_org_id);
13433     DBMS_SQL.Define_Column(v_CursorID, 12, v_inventory_item_id);
13434     DBMS_SQL.Define_Column(v_CursorID, 13, v_requested_quantity);
13435     DBMS_SQL.Define_Column(v_CursorID, 14, v_requested_quantity_uom, 3);
13436     DBMS_SQL.Define_Column(v_CursorID, 15, v_move_order_line_id);
13437     DBMS_SQL.Define_Column(v_CursorID, 16, v_ship_model_complete_flag, 1);
13438     DBMS_SQL.Define_Column(v_CursorID, 17, v_top_model_id);
13439     DBMS_SQL.Define_Column(v_CursorID, 18, v_ship_from_location_id);
13440     DBMS_SQL.Define_Column(v_CursorID, 19, v_ship_to_location_id);
13441     DBMS_SQL.Define_Column(v_CursorID, 20, v_ship_method_code, 30);
13442     DBMS_SQL.Define_Column(v_CursorID, 21, v_shipment_priority_code, 30);
13443     DBMS_SQL.Define_Column(v_CursorID, 22, v_ship_set_id);
13444     DBMS_SQL.Define_Column(v_CursorID, 23, v_date_scheduled);
13445     DBMS_SQL.Define_Column(v_CursorID, 24, v_planned_departure_date);
13446     DBMS_SQL.Define_Column(v_CursorID, 25, v_delivery_id);
13447     DBMS_SQL.Define_Column(v_CursorID, 26, v_customer_id);
13448     DBMS_SQL.Define_Column(v_CursorID, 27, v_carrier_id);
13449     DBMS_SQL.Define_Column(v_CursorID, 28, v_preferred_grade);
13450     DBMS_SQL.Define_Column(v_CursorID, 29, v_requested_quantity2);
13451     DBMS_SQL.Define_Column(v_CursorID, 30, v_requested_quantity_uom2, 3);
13452     DBMS_SQL.Define_Column(v_CursorID, 31, v_project_id);
13453     DBMS_SQL.Define_Column(v_CursorID, 32, v_task_id);
13454     DBMS_SQL.Define_Column(v_CursorID, 33, v_subinventory, 30);
13455     DBMS_SQL.Define_Column(v_CursorID, 34, v_weight_uom_code, 5);
13456     DBMS_SQL.Define_Column(v_CursorID, 35, v_net_weight);
13457     DBMS_SQL.Define_Column(v_CursorID, 36, v_volume_uom_code, 5);
13458     DBMS_SQL.Define_Column(v_CursorID, 37, v_volume);
13459     --  Bind release criteria values
13460 
13461     print_debug('Bind cursor', L_debug);
13462 
13463     /* IF (g_del_line_id <> 0 AND g_del_lines_list IS NULL) THEN
13464         -- DBMS_SQL.BIND_VARIABLE(v_CursorID,':X_del_detail_id',g_del_line_id);
13465     END IF;*/
13466     IF (g_customer_class_id is not null) THEN
13467       DBMS_SQL.BIND_VARIABLE(v_CursorID,
13468                              ':X_CUSTOMER_CLASS_ID',
13469                              g_customer_class_id);
13470     END IF;
13471 
13472     IF (g_order_header_id <> 0) THEN
13473       DBMS_SQL.BIND_VARIABLE(v_CursorID, ':X_header_id', g_order_header_id);
13474     END IF;
13475     /*   IF (g_order_line_id <> 0) THEN
13476     DBMS_SQL.BIND_VARIABLE(v_CursorID,':X_order_line_id',g_order_line_id);
13477       END IF;*/
13478     IF (g_customer_id <> 0) THEN
13479       DBMS_SQL.BIND_VARIABLE(v_CursorID, ':X_customer_id', g_customer_id);
13480     END IF;
13481     IF (g_item_category_id <> 0) THEN
13482       DBMS_SQL.BIND_VARIABLE(v_CursorID,
13483                              ':X_category_id',
13484                              g_item_category_id);
13485     END IF;
13486     IF (g_ship_from_loc_id <> -1) THEN
13487       DBMS_SQL.BIND_VARIABLE(v_CursorID,
13488                              ':X_ship_from_loc_id',
13489                              g_ship_from_loc_id);
13490     END IF;
13491     IF (g_ship_to_loc_id <> 0) THEN
13492       DBMS_SQL.BIND_VARIABLE(v_CursorID,
13493                              ':X_ship_to_loc_id',
13494                              g_ship_to_loc_id);
13495     END IF;
13496     --  DBMS_SQL.BIND_VARIABLE(v_CursorID,':X_backorders_flag',g_backorders_flag);
13497     DBMS_SQL.BIND_VARIABLE(v_CursorID, ':X_order_type_id', g_order_type_id);
13498     --   DBMS_SQL.BIND_VARIABLE(v_CursorID, ':X_ship_set_id', g_ship_set_number);
13499     DBMS_SQL.BIND_VARIABLE(v_CursorID, ':X_task_id', g_task_id);
13500     DBMS_SQL.BIND_VARIABLE(v_CursorID, ':X_project_id', g_project_id);
13501     DBMS_SQL.BIND_VARIABLE(v_CursorID, ':X_org_id', p_organization_id);
13502     DBMS_SQL.BIND_VARIABLE(v_CursorID,
13503                            ':X_ship_method_code',
13504                            g_ship_method_code);
13505     DBMS_SQL.BIND_VARIABLE(v_CursorID,
13506                            ':X_shipment_priority',
13507                            g_shipment_priority);
13508     DBMS_SQL.BIND_VARIABLE(v_CursorID,
13509                            ':X_from_request_date',
13510                            g_from_request_date);
13511     DBMS_SQL.BIND_VARIABLE(v_CursorID,
13512                            ':X_to_request_date',
13513                            g_to_request_date);
13514     DBMS_SQL.BIND_VARIABLE(v_CursorID,
13515                            ':X_from_sched_ship_date',
13516                            g_from_sched_ship_date);
13517     DBMS_SQL.BIND_VARIABLE(v_CursorID,
13518                            ':X_to_sched_ship_date',
13519                            g_to_sched_ship_date);
13520     DBMS_SQL.BIND_VARIABLE(v_CursorID,
13521                            ':X_inventory_item_id',
13522                            g_inventory_item_id);
13523     DBMS_SQL.BIND_VARIABLE(v_CursorID, ':X_trip_id', g_trip_id);
13524     DBMS_SQL.BIND_VARIABLE(v_CursorID, ':X_delivery_id', g_delivery_id);
13525     DBMS_SQL.BIND_VARIABLE(v_CursorID,
13526                            ':X_include_planned_lines',
13527                            g_include_planned_lines);
13528     IF (g_trip_id <> 0) THEN
13529       DBMS_SQL.BIND_VARIABLE(v_CursorID, ':X_trip_stop_id', g_trip_stop_id);
13530     end if;
13531 
13532     if (g_from_dock_appoint_date is not null) then
13533       DBMS_SQL.BIND_VARIABLE(v_CursorID,
13534                              ':X_to_dock_appoint_date',
13535                              g_to_dock_appoint_date);
13536       DBMS_SQL.BIND_VARIABLE(v_CursorID,
13537                              ':X_from_dock_appoint_date',
13538                              g_from_dock_appoint_date);
13539     END IF;
13540 
13541     if nvl(g_add_lines, 'N') = 'Y' then
13542       DBMS_SQL.BIND_VARIABLE(v_CursorID,
13543                              ':X_WAVE_HEADER_ID',
13544                              V_WAVE_HEADER_ID);
13545     end if;
13546     DBMS_SQL.BIND_VARIABLE(v_CursorID, ':X_CARRIER_ID', g_CARRIER_ID);
13547     -- Execute the cursor
13548     print_debug('Executing cursor', l_debug);
13549 
13550 	  IF DBMS_SQL.Is_Open(v_cursorID) THEN
13551        print_debug('The DBMS SQL is_open before the execute with rows for v_cursorID ' , l_debug);
13552       END IF;
13553     v_ignore := DBMS_SQL.Execute(v_CursorID);
13554 	  IF DBMS_SQL.Is_Open(v_cursorID) THEN
13555        print_debug('The DBMS SQL is_open after the execute with rows for v_cursorID '  , l_debug);
13556       END IF;
13557     x_api_status := fnd_api.g_ret_sts_success;
13558   EXCEPTION
13559     WHEN OTHERS THEN
13560       print_debug('Error : ' || SQLCODE || ' : ' || SQLERRM, l_debug);
13561       x_api_status := FND_API.G_RET_STS_ERROR;
13562   END Init_Cursor;
13563 
13564   --
13565   -- Name
13566   --   PROCEDURE Get_Lines
13567   --
13568   -- Purpose
13569   --   This routine returns information about the lines that
13570   --   are eligible for release. It fetches rows from the cursor
13571   --   for unreleased and backordered lines, and inserts the each
13572   --   row in the release_table based on the release sequence rule.
13573   --   It controls the number of lines to be fetched. The default
13574   --   value is set at 50. It also indicates whether there are any
13575   --   more lines to be retrieved.
13576   --
13577   -- Output Parameters
13578   --    x_done_flag   - whether all lines have been fetched
13579   --    x_api_status  - FND_API.G_RET_STS_SUCCESS or
13580   --                    FND_API.G_RET_STS_ERROR or
13581   --                    FND_API.G_RET_STS_ERROR
13582   --
13583   PROCEDURE Get_Lines(x_done_flag  OUT NOCOPY VARCHAR2,
13584                       x_api_status OUT NOCOPY VARCHAR2) IS
13585     l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
13586   BEGIN
13587     print_debug('In Get_Lines', l_debug);
13588     -- handle uninitialized package errors here
13589     /*
13590     IF g_initialized = FALSE THEN
13591       print_debug('The package must be initialized before use',l_debug);
13592       x_api_status := FND_API.G_RET_STS_ERROR;
13593       RETURN;
13594       END IF;
13595       */
13596     g_rel_current_line := 1;
13597     -- Clear the table
13598     --Ajith Need to modify this.
13599     IF release_table.count <> 0 THEN
13600       release_table.delete;
13601       l_source_code_tb.delete;
13602       l_source_header_id_tb.delete;
13603       l_source_line_id_tb.delete;
13604       l_source_header_number_tb.delete;
13605       l_source_line_number_tb.delete;
13606       l_source_header_type_id_tb.delete;
13607       l_source_document_type_id_tb.delete;
13608       l_delivery_Detail_id_tb.delete;
13609       l_organization_id_tb.delete;
13610       l_item_id_tb.delete;
13611       l_requested_quantity_tb.delete;
13612       l_requested_quantity_uom_tb.delete;
13613       l_requested_quantity2_tb.delete;
13614       l_requested_quantity_uom2_tb.delete;
13615       l_demand_header_id_tb.delete;
13616       l_net_weight_tb.delete;
13617       l_volume_tb.delete;
13618       l_net_value.delete;
13619 
13620     END IF;
13621     -- Set flag to indicate that fetching is not completed for
13622     -- an organization
13623     x_done_flag := FND_API.G_FALSE;
13624     -- If called after the first time, place the last row fetched in previous
13625     -- call as the first row, since it was not returned in the previous call
13626     print_debug('Fetching Customer specified lines', l_debug);
13627     MAX_LINES := 1000;
13628     print_debug('MAX_LINES is ' || to_char(MAX_LINES), l_debug);
13629     -- Loop and fetch up to MAX_LINES
13630 	  IF DBMS_SQL.Is_Open(v_cursorID) THEN
13631        print_debug('The DBMS SQL is_open with rows for v_cursorID '  , l_debug);
13632       END IF;
13633     LOOP
13634       IF g_rel_current_line < MAX_LINES THEN
13635         IF DBMS_SQL.Fetch_Rows(v_cursorID) = 0 THEN
13636           print_debug('Fetched all lines for organization', l_debug);
13637           x_done_flag := FND_API.G_TRUE;
13638           DBMS_SQL.Close_Cursor(v_cursorID);
13639           -- Reinitialize the first line marker since we have
13640           -- fetched all rows
13641           first_line.source_header_id := -1;
13642           print_debug('After Fetching all ROWs ', l_debug);
13643 
13644           EXIT;
13645         ELSE
13646           print_debug('--------------------', l_debug);
13647           print_debug('Current line is ' || to_char(g_rel_current_line),
13648                       l_debug);
13649           -- Save fetched record into release table
13650           Insert_RL_Row(g_return_status);
13651 
13652           IF (g_return_status = FND_API.G_RET_STS_ERROR) OR
13653              (g_return_status = FND_API.G_RET_STS_ERROR) THEN
13654             print_debug('Error occurred in Insert_RL_Row', l_debug);
13655             x_api_status := g_return_status;
13656             RETURN;
13657           END IF;
13658           print_debug('Row Description:', l_debug);
13659           print_debug('order_header_id = ' ||
13660                       release_table(g_rel_current_line)
13661                       .source_header_id || ' order_line_id = ' ||
13662                       release_table(g_rel_current_line)
13663                       .source_line_id || ' delivery_detail_id = ' ||
13664                       release_table(g_rel_current_line).delivery_detail_id,
13665                       l_debug);
13666           g_rel_current_line := g_rel_current_line + 1;
13667         END IF;
13668       ELSE
13669         x_done_flag        := FND_API.G_FALSE;
13670         EXIT;
13671       END IF;
13672     END LOOP;
13673     x_api_status := fnd_api.g_ret_sts_success;
13674   EXCEPTION
13675     WHEN OTHERS THEN
13676 
13677       print_debug('Unexpected error occurred in Get_Lines', l_debug);
13678       print_debug('Error : ' || SQLCODE || ' : ' || SQLERRM, l_debug);
13679 
13680       IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
13681         FND_MSG_PUB.Add_Exc_Msg('WSH_PR_CRITERIA', 'Get_Lines');
13682       END IF;
13683       IF DBMS_SQL.Is_Open(v_cursorID) THEN
13684         DBMS_SQL.Close_Cursor(v_cursorID);
13685       END IF;
13686       x_done_flag  := FND_API.G_RET_STS_ERROR;
13687       x_api_status := FND_API.G_RET_STS_ERROR;
13688   END Get_Lines;
13689 
13690   PROCEDURE Insert_RL_Row(x_api_status OUT NOCOPY VARCHAR2) IS
13691     l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
13692   BEGIN
13693     print_debug('Map output columns', l_debug);
13694     DBMS_SQL.Column_Value(v_CursorID, 1, v_source_code);
13695     DBMS_SQL.Column_Value(v_CursorID, 2, v_header_id);
13696     DBMS_SQL.Column_Value(v_CursorID, 3, v_line_id);
13697     DBMS_SQL.Column_Value(v_CursorID, 4, v_header_number);
13698     DBMS_SQL.Column_Value(v_CursorID, 5, v_line_number);
13699     DBMS_SQL.Column_Value(v_CursorID, 6, v_header_type_name);
13700     DBMS_SQL.Column_Value(v_CursorID, 7, v_header_type_id);
13701     DBMS_SQL.Column_Value(v_CursorID, 8, v_document_type_id);
13702     DBMS_SQL.Column_Value(v_CursorID, 9, v_delivery_detail_id);
13703     DBMS_SQL.Column_Value(v_CursorID, 10, v_released_status);
13704     DBMS_SQL.Column_Value(v_CursorID, 11, v_org_id);
13705     DBMS_SQL.Column_Value(v_CursorID, 12, v_inventory_item_id);
13706     DBMS_SQL.Column_Value(v_CursorID, 13, v_requested_quantity);
13707     DBMS_SQL.Column_Value(v_CursorID, 14, v_requested_quantity_uom);
13708     DBMS_SQL.Column_Value(v_CursorID, 15, v_move_order_line_id);
13709     DBMS_SQL.Column_Value(v_CursorID, 16, v_ship_model_complete_flag);
13710     DBMS_SQL.Column_Value(v_CursorID, 17, v_top_model_id);
13711     DBMS_SQL.Column_Value(v_CursorID, 18, v_ship_from_location_id);
13712     DBMS_SQL.Column_Value(v_CursorID, 19, v_ship_to_location_id);
13713     DBMS_SQL.Column_Value(v_CursorID, 20, v_ship_method_code);
13714     DBMS_SQL.Column_Value(v_CursorID, 21, v_shipment_priority_code);
13715     DBMS_SQL.Column_Value(v_CursorID, 22, v_ship_set_id);
13716     DBMS_SQL.Column_Value(v_CursorID, 23, v_date_scheduled);
13717     DBMS_SQL.Column_Value(v_CursorID, 24, v_planned_departure_date);
13718     DBMS_SQL.Column_Value(v_CursorID, 25, v_delivery_id);
13719     DBMS_SQL.Column_Value(v_CursorID, 26, v_customer_id);
13720     DBMS_SQL.Column_Value(v_CursorID, 27, v_carrier_id);
13721     DBMS_SQL.Column_Value(v_CursorID, 28, v_preferred_grade);
13722     DBMS_SQL.Column_Value(v_CursorID, 29, v_requested_quantity2);
13723     DBMS_SQL.Column_Value(v_CursorID, 30, v_requested_quantity_uom2);
13724     DBMS_SQL.Column_Value(v_CursorID, 31, v_project_id);
13725     DBMS_SQL.Column_Value(v_CursorID, 32, v_task_id);
13726     DBMS_SQL.Column_Value(v_CursorID, 33, v_subinventory);
13727     DBMS_SQL.Column_Value(v_CursorID, 34, v_weight_uom_code);
13728     DBMS_SQL.Column_Value(v_CursorID, 35, v_net_weight);
13729     DBMS_SQL.Column_Value(v_CursorID, 36, v_volume_uom_code);
13730 
13731     DBMS_SQL.Column_Value(v_CursorID, 37, v_volume);
13732     print_debug('Insert into table', l_debug);
13733     release_table(g_rel_current_line).source_line_id := v_line_id;
13734     release_table(g_rel_current_line).source_header_id := v_header_id;
13735     release_table(g_rel_current_line).organization_id := v_org_id;
13736     release_table(g_rel_current_line).inventory_item_id := v_inventory_item_id;
13737     release_table(g_rel_current_line).move_order_line_id := v_move_order_line_id;
13738     release_table(g_rel_current_line).source_code := v_source_code;
13739     release_table(g_rel_current_line).SOURCE_HEADER_NUMBER := v_HEADER_NUMBER;
13740     release_table(g_rel_current_line).SOURCE_LINE_NUMBER := v_LINE_NUMBER;
13741     release_table(g_rel_current_line).SOURCE_HEADER_TYPE_NAME := v_HEADER_TYPE_NAME;
13742     release_table(g_rel_current_line).SOURCE_HEADER_TYPE_ID := v_HEADER_TYPE_ID;
13743     release_table(g_rel_current_line).SOURCE_DOCUMENT_TYPE_ID := v_DOCUMENT_TYPE_ID;
13744     release_table(g_rel_current_line).delivery_detail_id := v_delivery_detail_id;
13745     release_table(g_rel_current_line).requested_quantity := v_requested_quantity;
13746     release_table(g_rel_current_line).requested_quantity_uom := v_requested_quantity_uom;
13747     release_table(g_rel_current_line).SHIP_MODEL_COMPLETE_FLAG := v_SHIP_MODEL_COMPLETE_FLAG;
13748     release_table(g_rel_current_line).top_model_line_id := v_top_model_id;
13749     release_table(g_rel_current_line).ship_from_location_id := v_ship_from_location_id;
13750     release_table(g_rel_current_line).ship_to_location_id := v_ship_to_location_id;
13751     release_table(g_rel_current_line).ship_method_code := v_ship_method_code;
13752     release_table(g_rel_current_line).shipment_priority_code := v_shipment_priority_code;
13753     release_table(g_rel_current_line).ship_set_id := v_ship_set_id;
13754     release_table(g_rel_current_line).date_scheduled := v_date_scheduled;
13755     release_table(g_rel_current_line).planned_departure_date := v_planned_departure_date;
13756     release_table(g_rel_current_line).delivery_id := v_delivery_id;
13757     release_table(g_rel_current_line).customer_id := v_customer_id;
13758     release_table(g_rel_current_line).carrier_id := v_carrier_id;
13759     release_table(g_rel_current_line).preferred_grade := v_preferred_grade;
13760     release_table(g_rel_current_line).requested_quantity := v_requested_quantity;
13761     release_table(g_rel_current_line).requested_quantity_uom := v_requested_quantity_uom;
13762     release_table(g_rel_current_line).project_id := v_project_id;
13763     release_table(g_rel_current_line).task_id := v_task_id;
13764     release_table(g_rel_current_line).FROM_SUBINVENTORY_CODE := v_subinventory;
13765     release_table(g_rel_current_line).net_weight_uom_code := v_weight_uom_code;
13766     release_table(g_rel_current_line).net_weight := v_net_weight;
13767     release_table(g_rel_current_line).volume_uom_code := v_volume_uom_code;
13768     release_table(g_rel_current_line).volume := v_volume;
13769 
13770     -- For Bulk Processing
13771 
13772     l_source_code_tb(g_rel_current_line) := v_source_code;
13773 
13774     l_source_header_id_tb(g_rel_current_line) := v_header_id;
13775     l_source_line_id_tb(g_rel_current_line) := v_line_id;
13776     l_source_header_number_tb(g_rel_current_line) := v_HEADER_NUMBER;
13777     l_source_line_number_tb(g_rel_current_line) := v_LINE_NUMBER;
13778     l_source_header_type_id_tb(g_rel_current_line) := v_HEADER_TYPE_ID;
13779     l_source_document_type_id_tb(g_rel_current_line) := v_DOCUMENT_TYPE_ID;
13780     l_delivery_Detail_id_tb(g_rel_current_line) := v_delivery_detail_id;
13781     l_delivery_id_tb(g_rel_current_line) := v_delivery_id;
13782     l_organization_id_tb(g_rel_current_line) := v_org_id;
13783     l_item_id_tb(g_rel_current_line) := v_inventory_item_id;
13784     l_requested_quantity_tb(g_rel_current_line) := v_requested_quantity;
13785     l_requested_quantity_uom_tb(g_rel_current_line) := v_requested_quantity_uom;
13786     l_requested_quantity2_tb(g_rel_current_line) := v_requested_quantity2;
13787     l_requested_quantity_uom2_tb(g_rel_current_line) := v_requested_quantity_uom2;
13788     -- l_demand_header_id_tb(g_rel_current_line);
13789     l_net_weight_tb(g_rel_current_line) := v_net_weight;
13790     l_volume_tb(g_rel_current_line) := v_volume;
13791     --  l_net_value(g_rel_current_line);
13792 
13793     x_api_status := fnd_api.g_ret_sts_success;
13794   EXCEPTION
13795     WHEN OTHERS THEN
13796       print_debug('Unexpected error in Insert_RL_Row', l_debug);
13797       print_debug('Error : ' || SQLCODE || ' : ' || SQLERRM, l_debug);
13798       x_api_status := FND_API.G_RET_STS_ERROR;
13799   END Insert_RL_Row;
13800 
13801   --Procedure to Calculate the Planned Fill Rate
13802   procedure get_line_fill_rate(x_return_status  OUT NOCOPY varchar2,
13803                                p_wave_header_id in number) is
13804     cursor c_lines(p_item_id in number, p_org_id in number) is
13805       select wwl.wave_header_id,
13806              wwl.wave_line_id,
13807              wdd.source_header_number,
13808              wdd.source_line_id,
13809              wwl.delivery_detail_id,
13810              wwl.organization_id,
13811              wdd.inventory_item_id,
13812              wdd.requested_quantity
13813         from wms_wp_wave_lines wwl, wsh_delivery_details wdd --11775489 get from wdd... FIXED...check performance
13814        where wwl.wave_header_id = p_wave_header_id
13815          and wdd.inventory_item_id = p_item_id
13816          and wwl.organization_id = p_org_id
13817 		 and wwl.delivery_detail_id  = wdd.delivery_detail_id
13818 		 and wdd.organization_id = wwl.organization_id
13819          and nvl(wwl.remove_from_wave_flag, 'N') = 'N'
13820        ORDER BY wave_header_id, inventory_item_id, wave_line_id;
13821 
13822     cursor c_item is
13823       SELECT DISTINCT wdd.inventory_item_id, wwl.organization_id
13824         FROM wms_wp_wave_lines wwl, wsh_delivery_Details wdd --11775489 get from wdd... FIXED
13825        WHERE wwl.wave_header_id = p_wave_header_id
13826 	     and wwl.delivery_detail_id  = wdd.delivery_detail_id
13827 		 and wdd.organization_id = wwl.organization_id;
13828 
13829     TYPE CONSOL_LINES_REC IS RECORD(
13830       wave_header_id    NUMBER,
13831       wave_line_id      number,
13832       planned_fill_rate number);
13833 
13834     TYPE CONSOL_LINES_TBL IS TABLE OF CONSOL_LINES_REC INDEX BY BINARY_INTEGER;
13835     L_ORG_ID           NUMBER;
13836     L_ITEM_ID          NUMBER;
13837     L_DEMAND_QTY       NUMBER;
13838     l_is_revision_ctrl BOOLEAN := FALSE;
13839     l_is_lot_ctrl      BOOLEAN := FALSE;
13840     l_is_serial_ctrl   BOOLEAN := FALSE;
13841     l_qoh              NUMBER;
13842     l_rqoh             NUMBER;
13843     l_qr               NUMBER;
13844     l_qs               NUMBER;
13845     l_atr              NUMBER;
13846     l_att              NUMBER;
13847     l_reserved_qty     number;
13848 
13849     X_CONSOL_LINES_TBL  CONSOL_LINES_TBL;
13850     l_debug             NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'),
13851                                       0);
13852     m                   NUMBER := 0;
13853     l_msg_count         number;
13854     l_msg_data          varchar2(100);
13855     l_pick_subinventory varchar2(100);
13856     l_other_wdd_qty     number := 0;
13857     l_temp_value        number;
13858 
13859   begin
13860 
13861     -- Checking if Pick Subinventory is mentioned in Planning Criteria
13862     begin
13863       select picking_subinventory
13864         into l_pick_subinventory
13865         from wms_wp_planning_criteria_vl wwp, wms_wp_wave_headers_vl wwh
13866        where wave_header_id = p_wave_header_id
13867          and wwh.planning_Criteria_id = wwp.planning_Criteria_id;
13868       print_debug('pick_subinventory :' || l_pick_subinventory, l_debug);
13869     exception
13870       when no_data_found then
13871         null;
13872     end;
13873 
13874     --Consolidating every item and org combination and then traversing through all the lines in the wave
13875     for c_item_rec in c_item loop
13876       l_item_id := c_item_rec.inventory_item_id;
13877       l_org_id  := c_item_rec.organization_id;
13878       print_debug('l_item_id :' || l_item_id, l_debug);
13879       print_debug('l_org_id :' || l_org_id, l_debug);
13880 
13881       --Find out the total atr for the item
13882       IF inv_cache.set_item_rec(L_ORG_ID, L_item_id) THEN
13883 
13884      /*   IF inv_cache.item_rec.revision_qty_control_code = 2 THEN
13885           l_is_revision_ctrl := TRUE;
13886         ELSE
13887           l_is_revision_ctrl := FALSE;
13888         END IF;
13889 
13890         IF inv_cache.item_rec.lot_control_code = 2 THEN
13891           l_is_lot_ctrl := TRUE;
13892         ELSE
13893           l_is_lot_ctrl := FALSE;
13894         END IF; */
13895 
13896         IF inv_cache.item_rec.serial_number_control_code NOT IN (1, 6) THEN
13897           l_is_serial_ctrl := FALSE;
13898         ELSE
13899           l_is_serial_ctrl := TRUE;
13900         END IF;
13901 
13902       ELSE
13903 
13904         print_debug('Error: Item detail not found', l_debug);
13905 
13906         RAISE no_data_found;
13907       END IF;
13908 
13909       inv_quantity_tree_pub.query_quantities(p_api_version_number      => 1.0,
13910                                              p_init_msg_lst            => fnd_api.g_false,
13911                                              x_return_status           => x_return_status,
13912                                              x_msg_count               => l_msg_count,
13913                                              x_msg_data                => l_msg_data,
13914                                              p_organization_id         => l_org_id,
13915                                              p_inventory_item_id       => l_item_id,
13916                                              p_tree_mode               => inv_quantity_tree_pub.g_transaction_mode,
13917                                              p_is_revision_control     => l_is_revision_ctrl,
13918                                              p_is_lot_control          => l_is_lot_ctrl,
13919                                              p_is_serial_control       => l_is_serial_ctrl,
13920                                              p_demand_source_type_id   => -9999 --should not be null
13921                                             ,
13922                                              p_demand_source_header_id => -9999 --should not be null
13923                                             ,
13924                                              p_demand_source_line_id   => -9999,
13925                                              p_revision                => NULL,
13926                                              p_lot_number              => NULL,
13927                                              p_subinventory_code       => l_pick_subinventory,
13928                                              p_locator_id              => NULL,
13929                                              x_qoh                     => l_qoh,
13930                                              x_rqoh                    => l_rqoh,
13931                                              x_qr                      => l_qr,
13932                                              x_qs                      => l_qs,
13933                                              x_att                     => l_att,
13934                                              x_atr                     => l_atr);
13935 
13936       print_debug('x_qoh :' || l_qoh, l_debug);
13937       print_debug('x_rqoh :' || l_rqoh, l_debug);
13938       print_debug('x_qr :' || l_qr, l_debug);
13939       print_debug('x_qs :' || l_qs, l_debug);
13940       print_debug('x_att :' || l_att, l_debug);
13941       print_debug('x_atr :' || l_atr, l_debug);
13942       FOR c_rec IN c_lines(l_item_id, l_org_id) LOOP
13943 
13944         EXIT WHEN c_lines%NOTFOUND;
13945         begin
13946 
13947           -- Since Qty Tree would have also subttracted qty for current demand
13948           -- lines under consideration for which there is existing
13949           -- reservation as well, I need to add them back
13950           -- to see real picture of the atr for demand line under consideration
13951 
13952           ------------------
13953 
13954           SELECT nvl(sum(reservation_quantity), 0)
13955             INTO l_reserved_qty
13956             FROM mtl_Reservations
13957            WHERE demand_source_line_id = c_rec.SOURCE_LINE_ID
13958              and organization_id = L_ORG_ID
13959              and inventory_item_id = L_ITEM_ID
13960              and (subinventory_code = l_pick_subinventory or
13961                  l_pick_subinventory is null);
13962           SELECT Nvl(SUM(wdd.requested_quantity), 0)
13963             INTO l_other_wdd_qty
13964             FROM wsh_delivery_details wdd
13965            WHERE wdd.organization_id = l_org_id
13966              AND wdd.inventory_item_id = l_item_id
13967 			 and wdd.released_status in ('B', 'E', 'F', 'K', 'R','S') --11822962 only consider wdds where requested material is against a possible reservation
13968              and wdd.delivery_Detail_id not in
13969                  (select wwl.delivery_detail_id
13970                     from wms_wp_wave_lines wwl, wsh_delivery_Details wdd2 --11775489 get from wdd... FIXED
13971                    where wwl.wave_header_id = p_wave_header_id
13972                      and nvl(wwl.remove_from_wave_flag, 'N') <> 'Y'
13973                      and wdd2.source_line_id = wdd.source_line_id
13974 					 and wwl.delivery_detail_id  = wdd2.delivery_detail_id
13975 					 and wdd2.organization_id = wwl.organization_id);
13976 
13977           print_debug('Reserved Qty for other dds that do not belong to the wave in source line id  :' ||
13978                       c_rec.SOURCE_LINE_ID || ' is ' || l_other_wdd_qty,
13979                       l_debug);
13980 
13981           IF (l_reserved_qty - l_other_wdd_qty) >= 0 THEN
13982             l_temp_value := (l_reserved_qty - l_other_wdd_qty);
13983           ELSE
13984             l_temp_value := 0;
13985           END IF;
13986 
13987           l_reserved_qty := l_temp_value;
13988 
13989           print_debug('Reserved Qty for source line id  :' ||
13990                       c_rec.SOURCE_LINE_ID || ' is ' || l_reserved_qty,
13991                       l_debug);
13992 
13993         exception
13994 
13995           when no_data_found then
13996             l_reserved_qty := 0;
13997         end;
13998 
13999         l_atr := l_atr + l_reserved_qty;
14000 
14001         print_debug('Effective ATR is  :' || l_atr, l_debug);
14002 
14003         if l_atr >= c_rec.REQUESTED_QUANTITY then
14004           X_CONSOL_LINES_TBL(m).wave_header_id := p_wave_header_id;
14005           X_CONSOL_LINES_TBL(m).wave_line_id := c_rec.wave_line_id;
14006           X_CONSOL_LINES_TBL(m).planned_fill_rate := 100;
14007 
14008           l_atr := l_atr - c_rec.REQUESTED_QUANTITY;
14009         else
14010 
14011           X_CONSOL_LINES_TBL(m).wave_header_id := p_wave_header_id;
14012           X_CONSOL_LINES_TBL(m).wave_line_id := c_rec.wave_line_id;
14013 
14014           IF L_ATR > 0 THEN
14015             X_CONSOL_LINES_TBL(m).planned_fill_rate := round((l_atr * 100) /
14016                                                              c_rec.REQUESTED_QUANTITY);
14017 
14018             l_atr := l_atr - c_rec.REQUESTED_QUANTITY;
14019 
14020           ELSE
14021             X_CONSOL_LINES_TBL(m).planned_fill_rate := 0;
14022 
14023             -- l_atr := l_atr - c_rec.REQUESTED_QUANTITY;
14024           end if;
14025         end if;
14026 
14027         --Updating the Lines Table with planned_fill_rate and actual _fill_rate
14028         print_debug('Wave Line Id:' || X_CONSOL_LINES_TBL(m).wave_line_id,
14029                     l_debug);
14030 
14031         print_debug('Planned Fill Rate:' || X_CONSOL_LINES_TBL(m)
14032                     .planned_fill_rate,
14033                     l_debug);
14034 
14035         update wms_wp_wave_lines
14036            set planned_fill_rate = X_CONSOL_LINES_TBL(m).planned_fill_rate
14037          where wave_line_id = X_CONSOL_LINES_TBL(m).wave_line_id;
14038 
14039         m := m + 1;
14040       END LOOP;
14041 
14042     end loop;
14043     x_return_status := fnd_api.g_ret_sts_success;
14044     -- commit;
14045   exception
14046 
14047     when others then
14048       print_debug('Error in Planned Fill Rate: ' || SQLCODE || ' : ' ||
14049                   SQLERRM,
14050                   l_debug);
14051       x_return_status := FND_API.G_RET_STS_ERROR;
14052 
14053   end get_line_fill_rate;
14054 
14055   --bug11775489 restructure to call update_line_actual_fill_rate for each line
14056   procedure get_actual_fill_rate(x_return_status  OUT NOCOPY varchar2,
14057                                  p_wave_header_id in number)
14058 
14059    is
14060     cursor c_lines is
14061 	select wwl.wave_line_id
14062 	from wms_wp_wave_lines wwl
14063 	where wave_header_id = p_wave_header_id
14064 	and nvl(remove_from_wave_flag, 'N') <> 'Y'; --11775489 added filter
14065 	l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
14066 
14067   begin
14068     x_return_status := fnd_api.g_ret_sts_success;
14069     for l_line in c_lines loop
14070 	  update_line_actual_fill_rate(l_line.wave_line_id);
14071     end loop;
14072     x_return_status := fnd_api.g_ret_sts_success;
14073     --   commit;
14074   exception
14075     when others then
14076       print_debug('Error in Actual Fill Rate: ' || SQLCODE || ' : ' ||
14077                   SQLERRM,
14078                   l_debug);
14079       x_return_status := FND_API.G_RET_STS_ERROR;
14080 
14081   end get_actual_fill_rate;
14082 
14083   function get_order_weight(p_source_header_id in number,
14084                             p_orgid            in number) return number
14085 
14086    is
14087 
14088     cursor c_uom is
14089       select nvl(net_weight, 0) net_weight, weight_uom_code
14090         from wsh_delivery_details
14091        where source_header_id = p_source_header_id;
14092 
14093     v_weight_uom varchar2(20);
14094     total_weight number := 0;
14095     l_debug      NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
14096   begin
14097 
14098     SELECT uom_code
14099       into v_weight_uom
14100       FROM mtl_units_of_measure_vl
14101      WHERE uom_class = (SELECT weight_uom_class
14102                           FROM wsh_shipping_parameters
14103                          WHERE organization_id = p_orgid)
14104        AND base_uom_flag = 'Y';
14105 
14106     for l_uom in c_uom loop
14107 
14108       total_weight := total_weight + wms_wave_planning_pvt.get_conversion_rate(null,
14109                                                                                l_uom.weight_uom_code,
14110                                                                                v_weight_uom) *
14111                       l_uom.net_weight;
14112 
14113     end loop;
14114     --  print_debug('Total Weight for Order id :'||p_source_header_id ||' is '|| total_weight, l_debug);
14115 
14116     return total_weight;
14117 
14118   exception
14119 
14120     when others then
14121       print_debug('Error in get_order_weight : ' || SQLCODE || ' : ' ||
14122                   SQLERRM,
14123                   l_debug);
14124       return 0;
14125 
14126   end get_order_weight;
14127 
14128   function get_order_volume(p_source_header_id in number,
14129                             p_orgid            in number) return number
14130 
14131    is
14132 
14133     cursor c_uom is
14134       select nvl(volume, 0) volume, volume_uom_code
14135         from wsh_delivery_details
14136        where source_header_id = p_source_header_id;
14137 
14138     v_volume_uom varchar2(20);
14139     total_volume number := 0;
14140     l_debug      NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
14141   begin
14142 
14143     SELECT uom_code
14144       into v_volume_uom
14145       FROM mtl_units_of_measure_vl
14146      WHERE uom_class = (SELECT volume_uom_class
14147                           FROM wsh_shipping_parameters
14148                          WHERE organization_id = p_orgid)
14149        AND base_uom_flag = 'Y';
14150 
14151     for l_uom in c_uom loop
14152 
14153       total_volume := total_volume + wms_wave_planning_pvt.get_conversion_rate(null,
14154                                                                                l_uom.volume_uom_code,
14155                                                                                v_volume_uom) *
14156                       l_uom.volume;
14157 
14158     end loop;
14159     --  print_debug('Total Volume for Order id :'||p_source_header_id ||' is '|| total_volume, l_debug);
14160 
14161     return total_volume;
14162 
14163   exception
14164 
14165     when others then
14166       print_debug('Error in get_order_volume : ' || SQLCODE || ' : ' ||
14167                   SQLERRM,
14168                   l_debug);
14169       return 0;
14170 
14171   end get_order_volume;
14172 
14173   procedure get_net_weight_volume(p_wave_header_id in number,
14174                                   p_orgid          in number,
14175                                   x_weight         OUT NOCOPY varchar2,
14176                                   x_volume         OUT NOCOPY varchar2)
14177 
14178    is
14179 
14180     cursor c_uom is
14181       select nvl(net_weight, 0) net_weight,
14182              weight_uom_code,
14183              nvl(volume, 0) volume,
14184              volume_uom_code
14185         from wsh_delivery_details
14186        where delivery_detail_id in
14187              (SELECT wwl.delivery_detail_id --11775489 get from wdd... FIXED
14188                 FROM wms_wp_wave_lines wwl, wsh_delivery_details wdd
14189                WHERE wwl.wave_header_id = p_wave_header_id
14190 			     and wwl.delivery_detail_id = wdd.delivery_detail_id
14191 				 and wwl.organization_id = wdd.organization_id
14192                  and nvl(remove_from_wave_flag, 'N') <> 'Y');
14193 
14194     v_weight_uom varchar2(20);
14195     v_volume_uom varchar2(20);
14196 
14197     total_weight number := 0;
14198     total_volume number := 0;
14199     l_debug      NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
14200   begin
14201 
14202     SELECT uom_code
14203       into v_weight_uom
14204       FROM mtl_units_of_measure_vl
14205      WHERE uom_class = (SELECT weight_uom_class
14206                           FROM wsh_shipping_parameters
14207                          WHERE organization_id = p_orgid)
14208        AND base_uom_flag = 'Y';
14209 
14210     SELECT uom_code
14211       into v_volume_uom
14212       FROM mtl_units_of_measure_vl
14213      WHERE uom_class = (SELECT volume_uom_class
14214                           FROM wsh_shipping_parameters
14215                          WHERE organization_id = p_orgid)
14216        AND base_uom_flag = 'Y';
14217 
14218     for l_uom in c_uom loop
14219 
14220       -- dbms_output.put_line( l_uom.weight_uom_code);
14221       total_weight := total_weight + wms_wave_planning_pvt.get_conversion_rate(null,
14222                                                                                l_uom.weight_uom_code,
14223                                                                                v_weight_uom) *
14224                       l_uom.net_weight;
14225 
14226       total_volume := total_volume + wms_wave_planning_pvt.get_conversion_rate(null,
14227                                                                                l_uom.volume_uom_code,
14228                                                                                v_volume_uom) *
14229                       l_uom.volume;
14230 
14231     end loop;
14232 
14233     --dbms_output.put_line(v_weight_uom);
14234     --dbms_output.put_line(v_volume_uom);
14235     --dbms_output.put_line(total_weight);
14236     --dbms_output.put_line(total_volume);
14237 
14238     print_debug('Total Weight is : ' || total_weight, l_debug);
14239 
14240     if nvl(total_weight, 0) > 0 then
14241 
14242       x_weight := total_weight || ' ' || v_weight_uom;
14243     else
14244       x_weight := '';
14245     end if;
14246     print_debug('Total Volume  is : ' || total_weight, l_debug);
14247 
14248     if nvl(total_volume, 0) > 0 then
14249 
14250       x_volume := round(total_volume, 2) || ' ' || v_volume_uom;
14251     else
14252 
14253       x_volume := '';
14254     end if;
14255 
14256   exception
14257 
14258     when others then
14259       print_debug('Error in get net weight volume : ' || SQLCODE || ' : ' ||
14260                   SQLERRM,
14261                   l_debug);
14262 
14263   end get_net_weight_volume;
14264 
14265   procedure update_wave_header_status(x_return_status  OUT NOCOPY varchar2,
14266                                       p_wave_header_id in number,
14267                                       Status           in varchar2,
14268 									  p_batch_id in number default null) IS
14269 
14270 
14271 
14272 
14273 
14274     l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
14275   begin
14276 
14277   IF(p_batch_id is not NULL) THEN
14278       print_debug('Update Wave Header Status 2: ' || Status ||'the val of batch'||p_batch_id, l_debug);
14279     update wms_wp_wave_headers_vl
14280        set wave_status = Status,
14281 	    batch_id = p_batch_id
14282      where wave_header_id = p_wave_header_id;
14283 
14284   ELSE
14285     print_debug('Update Wave Header Status 3: ' || Status ||'the val of batch'||p_batch_id, l_debug);
14286     update wms_wp_wave_headers_vl
14287        set wave_status = Status
14288      where wave_header_id = p_wave_header_id;
14289 
14290   END IF; --end if for batch_id != NULL
14291 
14292 
14293     x_return_status := 'S';
14294   exception
14295 
14296     when OTHERS then
14297       print_debug('Error in Update Wave Header Status: ' || SQLCODE ||
14298                   ' : ' || SQLERRM,
14299                   l_debug);
14300       x_return_status := FND_API.G_RET_STS_ERROR;
14301   end update_wave_header_status;
14302 
14303   PROCEDURE update_task(p_transaction_temp_id   IN WMS_WAVE_PLANNING_PVT.transaction_temp_table_type,
14304                         p_task_type_id          IN WMS_WAVE_PLANNING_PVT.task_type_id_table_type,
14305                         p_employee              IN VARCHAR2,
14306                         p_employee_id           IN NUMBER,
14307                         p_user_task_type        IN VARCHAR2,
14308                         p_user_task_type_id     IN NUMBER,
14309                         p_effective_start_date  IN DATE,
14310                         p_effective_end_date    IN DATE,
14311                         p_person_resource_id    IN NUMBER,
14312                         p_person_resource_code  IN VARCHAR2,
14313                         p_force_employee_change IN BOOLEAN,
14314                         p_to_status             IN VARCHAR2,
14315                         p_to_status_id          IN NUMBER,
14316                         p_priority_type         IN varchar2 DEFAULT 'S', --mitgupta
14317                         p_priority              IN NUMBER,
14318                         p_clear_priority        IN VARCHAR2,
14319                         x_result                OUT NOCOPY WMS_WAVE_PLANNING_PVT.result_table_type,
14320                         x_message               OUT NOCOPY WMS_WAVE_PLANNING_PVT.message_table_type,
14321                         x_task_id               OUT NOCOPY WMS_WAVE_PLANNING_PVT.task_id_table_type,
14322                         x_return_status         OUT NOCOPY VARCHAR2,
14323                         x_return_msg            OUT NOCOPY VARCHAR2,
14324                         x_msg_count             OUT NOCOPY NUMBER) IS
14325     l_task_id NUMBER;
14326     l_index   NUMBER;
14327     l_debug   NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
14328 
14329     TYPE status_table_type IS TABLE OF Varchar2(100);
14330 
14331     l_transaction_temp_ids      WMS_WAVE_PLANNING_PVT.transaction_temp_table_type;
14332     l_task_type_ids             WMS_WAVE_PLANNING_PVT.task_type_id_table_type;
14333     l_statuses                  status_table_type;
14334     l_transaction_temp_ids_temp WMS_WAVE_PLANNING_PVT.transaction_temp_table_type;
14335     l_device_id                 NUMBER;
14336     l_messages                  WMS_WAVE_PLANNING_PVT.message_table_type;
14337     l_message                   WMS_WP_TASKS_GTMP.error%TYPE;
14338     g_cannot_update_putaway          WMS_WP_TASKS_GTMP.error%TYPE;
14339     g_task_updated                   WMS_WP_TASKS_GTMP.error%TYPE;
14340 
14341   BEGIN
14342     print_debug('XXXX Update Wave Header Status: ', l_debug);
14343 
14344 --g_cannot_update_putaway := NULL;
14345 
14346      IF g_cannot_update_putaway IS NULL
14347       THEN
14348          fnd_message.set_name ('WMS', 'WMS_CANNOT_UPDATE_PUTAWAY_TASK');
14349          g_cannot_update_putaway := fnd_message.get;
14350 
14351          fnd_message.set_name ('WMS', 'WMS_TASK_UPDATED');
14352          g_task_updated := fnd_message.get;
14353 
14354       END IF;
14355 
14356       FORALL i IN p_transaction_temp_id.FIRST .. p_transaction_temp_id.LAST
14357          UPDATE wms_wp_tasks_gtmp
14358             SET RESULT = 'X'
14359           WHERE transaction_temp_id = p_transaction_temp_id (i)
14360             AND task_type_id = p_task_type_id (i);
14361 
14362       -- Validations
14363 
14364       -- 1  Pick
14365       -- 2  Putaway
14366       -- 3  Cycle Count
14367       -- 4  Replenish
14368       -- 5  Move Order Transfer
14369       -- 6  Move Order Issue
14370       -- 7  Staging Move
14371 
14372       -- Cannot update putaway tasks or staging moves
14373       UPDATE wms_wp_tasks_gtmp
14374          SET RESULT = 'E',
14375              error =
14376                 DECODE (task_type_id,
14377                         2, g_cannot_update_putaway
14378                        )
14379        WHERE RESULT = 'X' AND task_type_id IN (2);
14380 
14381     g_task_updated := fnd_message.get;
14382     -- Validations
14383 
14384     -- 1  Pick
14385     -- 2  Putaway
14386     -- 3  Cycle Count
14387     -- 4  Replenish
14388     -- 5  Move Order Transfer
14389     -- 6  Move Order Issue
14390     -- 7  Staging Move
14391  /*   FORALL i IN p_transaction_temp_id.FIRST .. p_transaction_temp_id.LAST
14392       UPDATE wms_wp_tasks_gtmp
14393          SET RESULT = 'X'
14394        WHERE transaction_temp_id = p_transaction_temp_id(i)
14395          AND task_type_id = p_task_type_id(i);*/
14396 
14397     -- Invalid status changes
14398     IF p_to_status_id IS NOT NULL THEN
14399       SELECT transaction_temp_id, task_type_id, status BULK COLLECT
14400         INTO l_transaction_temp_ids, l_task_type_ids, l_statuses
14401         FROM wms_wp_tasks_gtmp
14402        WHERE RESULT = 'X'
14403          AND NOT ((status_id = 8 AND p_to_status_id IN (1, 2)) -- Unreleased to pending or queued
14404               OR (status_id = 1 AND p_to_status_id IN (2, 8)) -- Pending to queued or unreleased
14405               OR (status_id = 2 AND p_to_status_id IN (1, 8)) -- Queued to pending or unreleased
14406               OR (status_id = 9 AND p_to_status_id IN (1, 8)) -- R12:Active to Pending or unreleased
14407               OR (status_id = 3 AND p_to_status_id IN (1, 8)) -- R12:Dispatched to Pending or unreleased
14408               OR (status_id = p_to_status_id)); -- No Status Change
14409       IF l_transaction_temp_ids.COUNT > 0 THEN
14410         FOR i IN l_transaction_temp_ids.FIRST .. l_transaction_temp_ids.LAST LOOP
14411           fnd_message.set_name('WMS', 'WMS_CANNOT_UPDATE_STATUS');
14412           fnd_message.set_token('FROM_STATUS', l_statuses(i));
14413           fnd_message.set_token('TO_STATUS', p_to_status);
14414           l_messages(i) := fnd_message.get;
14415         END LOOP;
14416 
14417         FORALL i IN l_transaction_temp_ids.FIRST .. l_transaction_temp_ids.LAST
14418           UPDATE wms_wp_tasks_gtmp
14419              SET RESULT = 'E', error = l_messages(i)
14420            WHERE transaction_temp_id = l_transaction_temp_ids(i)
14421              AND task_type_id = l_task_type_ids(i);
14422       END IF;
14423 
14424       IF p_to_status_id IN (1, 8) THEN
14425         SELECT wwtt.transaction_temp_id, wwtt.task_type_id, wwtt.status BULK COLLECT
14426           INTO l_transaction_temp_ids, l_task_type_ids, l_statuses
14427           FROM wms_wp_tasks_gtmp              wwtt,
14428                mtl_material_transactions_temp mmtt,
14429                WMS_DISPATCHED_TASKS           wdt
14430          WHERE wwtt.transaction_temp_id = wdt.transaction_temp_id
14431            AND wwtt.transaction_temp_id = mmtt.transaction_temp_id
14432            AND wwtt.RESULT = 'X'
14433            AND wwtt.status_id = 3
14434            AND EXISTS
14435          (SELECT 1
14436                   FROM WMS_DISPATCHED_TASKS wdt2
14437                  WHERE wdt2.person_id = wwtt.person_id
14438                    AND wdt2.status = 9
14439                    AND wdt2.task_method IS NOT NULL
14440                    AND wdt2.transaction_temp_id IN
14441                        (SELECT transaction_temp_id
14442                           FROM mtl_material_transactions_temp mmtt1
14443                          WHERE DECODE(wdt.TASK_METHOD,
14444                                       'CARTON',
14445                                       mmtt1.cartonization_id,
14446                                       'PICK_SLIP',
14447                                       mmtt1.pick_slip_number,
14448                                       'DISCRETE',
14449                                       mmtt1.pick_slip_number,
14450                                       mmtt1.transaction_source_id) =
14451                                DECODE(wdt.TASK_METHOD,
14452                                       'CARTON',
14453                                       mmtt.cartonization_id,
14454                                       'PICK_SLIP',
14455                                       mmtt.pick_slip_number,
14456                                       'DISCRETE',
14457                                       mmtt.pick_slip_number,
14458                                       mmtt.transaction_source_id)));
14459 
14460         IF l_transaction_temp_ids.COUNT > 0 THEN
14461           FOR i IN l_transaction_temp_ids.FIRST .. l_transaction_temp_ids.LAST LOOP
14462             fnd_message.set_name('WMS', 'WMS_GROUP_TASKS_CANNOT_UPDATE');
14463             l_messages(i) := fnd_message.get;
14464           END LOOP;
14465 
14466           FORALL i IN l_transaction_temp_ids.FIRST .. l_transaction_temp_ids.LAST
14467             UPDATE wms_wp_tasks_gtmp
14468                SET RESULT = 'E',
14469                    error  = 'This group of tasks is currently being worked, cannot change status'
14470              WHERE transaction_temp_id = l_transaction_temp_ids(i)
14471                AND task_type_id = l_task_type_ids(i);
14472         END IF;
14473 
14474         -- if the original task status is Active, check if the user whom the task is assigned is
14475         -- logged on to the system
14476         -- if the original task status is Active, check if the user whom the task is assigned is
14477         -- logged on to the system
14478         SELECT transaction_temp_id, task_type_id, status BULK COLLECT
14479           INTO l_transaction_temp_ids, l_task_type_ids, l_statuses
14480           FROM wms_wp_tasks_gtmp wwtt
14481          WHERE RESULT = 'X'
14482            AND status_id = 9
14483            AND EXISTS
14484          (SELECT 1
14485                   FROM MTL_MOBILE_LOGIN_HIST MMLH, WMS_DISPATCHED_TASKS WDT
14486                  WHERE WDT.TRANSACTION_TEMP_ID = WWTT.TRANSACTION_TEMP_ID
14487                    AND MMLH.USER_ID = WDT.LAST_UPDATED_BY
14488                    AND MMLH.LOGOFF_DATE IS NULL
14489                    AND MMLH.EVENT_MESSAGE IS NULL);
14490 
14491         IF l_transaction_temp_ids.COUNT > 0 THEN
14492           FORALL i IN l_transaction_temp_ids.FIRST .. l_transaction_temp_ids.LAST
14493             UPDATE wms_wp_tasks_gtmp
14494                SET RESULT = 'E',
14495                    error  = 'This task is currently being worked, cannot change status'
14496              WHERE transaction_temp_id = l_transaction_temp_ids(i)
14497                AND task_type_id = l_task_type_ids(i);
14498         END IF;
14499 
14500       END IF;
14501 
14502     END IF;
14503 
14504     -- Employee eligibility validation
14505     IF p_employee_id IS NOT NULL AND NOT p_force_employee_change THEN
14506       fnd_message.set_name('WMS', 'WMS_CANNOT_UPDATE_EMPLOYEE');
14507       fnd_message.set_token('EMPLOYEE', p_employee);
14508       l_message := fnd_message.get;
14509 
14510       UPDATE wms_wp_tasks_gtmp wwtt
14511          SET RESULT = 'E', error = l_message
14512        WHERE RESULT = 'X'
14513          AND NOT EXISTS
14514        (SELECT 1
14515                 FROM bom_std_op_resources bsor, bom_resource_employees bre
14516                WHERE wwtt.user_task_type_id = bsor.standard_operation_id
14517                  AND bsor.resource_id = bre.resource_id
14518                  AND bre.person_id = p_employee_id);
14519 
14520       SELECT transaction_temp_id BULK COLLECT
14521         INTO l_transaction_temp_ids_temp
14522         FROM wms_wp_tasks_gtmp
14523        WHERE RESULT = 'X';
14524 
14525       --j Develop
14526       IF (l_transaction_temp_ids_temp.COUNT > 0) THEN
14527         FOR i IN l_transaction_temp_ids_temp.FIRST .. l_transaction_temp_ids_temp.LAST LOOP
14528           IF (getforcesignonflagvalue(l_transaction_temp_ids_temp(i),
14529                                       l_device_id) = 'Y') THEN
14530             fnd_message.set_name('WMS', 'WMS_CANNOT_UPDATE_EMPLOYEE');
14531             fnd_message.set_token('EMPLOYEE', p_employee);
14532             l_message := fnd_message.get;
14533             if l_debug = 1 then
14534               print_DEBUG('l_device_id : ' || l_device_id, l_debug);
14535               print_DEBUG(' p_employee_id : ' || p_employee_id, l_debug);
14536             end if;
14537 
14538             UPDATE wms_wp_tasks_gtmp wwtt
14539                SET RESULT = 'E', error = l_message
14540              WHERE transaction_temp_id = l_transaction_temp_ids_temp(i)
14541                AND NOT EXISTS
14542              (SELECT 1
14543                       FROM wms_device_assignment_temp
14544                      WHERE device_id = l_device_id
14545                        AND employee_id = p_employee_id);
14546           END IF;
14547         END LOOP;
14548       END IF;
14549       -- End  of J Develop
14550     END IF;
14551 
14552     UPDATE wms_wp_tasks_gtmp wwtt
14553        SET person_resource_id = (SELECT bre.resource_id
14554                                    FROM bom_std_op_resources   bsor,
14555                                         bom_resource_employees bre
14556                                   WHERE wwtt.user_task_type_id =
14557                                         bsor.standard_operation_id
14558                                     AND bsor.resource_id = bre.resource_id
14559                                     AND bre.person_id = wwtt.person_id
14560                                     AND ROWNUM < 2)
14561      WHERE RESULT = 'X';
14562 
14563     IF p_user_task_type_id IS NOT NULL THEN
14564       -- R12: Can update User Task Type if task is dispatched, active  IF
14565       -- Dispatched or Active tasks are in the process of getting updated to pending or Unreleased
14566       IF p_to_status_id IS NOT NULL THEN
14567         UPDATE wms_wp_tasks_gtmp wwtt
14568            SET RESULT = 'E', error = l_message
14569          WHERE RESULT = 'X'
14570            AND (status_id NOT IN (1, 2, 3, 8, 9) AND
14571                p_to_status_id IN (1, 8))
14572         RETURNING transaction_temp_id, task_type_id, status BULK COLLECT INTO l_transaction_temp_ids, l_task_type_ids, l_statuses;
14573       ELSE
14574         -- R12: Cannot update User Task Type if task is dispatched, active or loaded IF
14575         -- Dispatched or Active tasks are NOT in the process of getting updated to pending or Unreleased
14576         UPDATE wms_wp_tasks_gtmp wwtt
14577            SET RESULT = 'E', error = l_message
14578          WHERE RESULT = 'X'
14579            AND status_id NOT IN (1, 8)
14580         RETURNING transaction_temp_id, task_type_id, status BULK COLLECT INTO l_transaction_temp_ids, l_task_type_ids, l_statuses;
14581       END IF;
14582 
14583       IF l_transaction_temp_ids.COUNT > 0 THEN
14584         FOR i IN l_transaction_temp_ids.FIRST .. l_transaction_temp_ids.LAST LOOP
14585           fnd_message.set_name('WMS', 'WMS_CANNOT_UPDATE_USER_TASK_TYPE');
14586           fnd_message.set_token('STATUS', l_statuses(i));
14587           l_messages(i) := fnd_message.get;
14588         END LOOP;
14589 
14590         FORALL i IN l_transaction_temp_ids.FIRST .. l_transaction_temp_ids.LAST
14591           UPDATE wms_wp_tasks_gtmp
14592              SET RESULT = 'E', error = l_messages(i)
14593            WHERE transaction_temp_id = l_transaction_temp_ids(i)
14594              AND task_type_id = l_task_type_ids(i);
14595       END IF;
14596     END IF;
14597     -- Ajith Needs to check the following code ??????????
14598     --mitgupta add p_priority_type
14599     IF (p_priority_type IS NOT NULL AND p_priority IS NOT NULL) OR
14600        p_clear_priority = 'Y' THEN
14601       -- R12: Can update priority if task is dispatched, active  IF
14602       -- Dispatched or Active tasks are in the process of getting updated to pending or Unreleased
14603       IF p_to_status_id IS NOT NULL THEN
14604         UPDATE wms_wp_tasks_gtmp wwtt
14605            SET RESULT = 'E', error = l_message
14606          WHERE RESULT = 'X'
14607            AND (status_id NOT IN (1, 2, 3, 8, 9) AND
14608                p_to_status_id IN (1, 8))
14609         RETURNING transaction_temp_id, task_type_id, status BULK COLLECT INTO l_transaction_temp_ids, l_task_type_ids, l_statuses;
14610       ELSE
14611         -- R12: Cannot update priority if task is dispatched, active or loaded IF
14612         -- Dispatched or Active tasks are NOT in the process of getting updated to pending or Unreleased
14613         UPDATE wms_wp_tasks_gtmp wwtt
14614            SET RESULT = 'E', error = l_message
14615          WHERE RESULT = 'X'
14616            AND status_id NOT IN (1, 2, 8)
14617         RETURNING transaction_temp_id, task_type_id, status BULK COLLECT INTO l_transaction_temp_ids, l_task_type_ids, l_statuses;
14618       END IF;
14619 
14620       IF l_transaction_temp_ids.COUNT > 0 THEN
14621         FOR i IN l_transaction_temp_ids.FIRST .. l_transaction_temp_ids.LAST LOOP
14622           fnd_message.set_name('WMS', 'WMS_CANNOT_UPDATE_PRIORITY');
14623           fnd_message.set_token('STATUS', l_statuses(i));
14624           l_messages(i) := fnd_message.get;
14625         END LOOP;
14626 
14627         FORALL i IN l_transaction_temp_ids.FIRST .. l_transaction_temp_ids.LAST
14628           UPDATE wms_wp_tasks_gtmp
14629              SET RESULT = 'E', error = l_messages(i)
14630            WHERE transaction_temp_id = l_transaction_temp_ids(i)
14631              AND task_type_id = l_task_type_ids(i);
14632       END IF;
14633     END IF;
14634 
14635     -- If changing status to Queued
14636     IF p_to_status_id = 2 THEN
14637       UPDATE wms_wp_tasks_gtmp
14638          SET task_id   = NVL(task_id, wms_dispatched_tasks_s.NEXTVAL),
14639              status    = p_to_status,
14640              status_id = p_to_status_id,
14641              priority  = DECODE(p_clear_priority,
14642                                 'Y',
14643                                 NULL,
14644                                 DECODE(p_priority_type,
14645                                        'I',
14646                                        NVL(priority, 0) + p_priority, -- R12: Increment priority
14647                                        'D',
14648                                        DECODE(SIGN(NVL(priority, 0) -
14649                                                    p_priority), -- R12: Decrement priority
14650                                               -1,
14651                                               0,
14652                                               +1,
14653                                               NVL(priority, 0) - p_priority,
14654                                               0,
14655                                               0),
14656                                        'S',
14657                                        NVL(p_priority, priority), -- R12: Set       priority
14658                                        priority)),
14659 
14660              person               = p_employee,
14661              person_id            = p_employee_id,
14662              effective_start_date = p_effective_start_date,
14663              effective_end_date   = p_effective_end_date,
14664              person_resource_code = p_person_resource_code,
14665              person_resource_id   = p_person_resource_id,
14666              RESULT               = 'S',
14667              error                = g_task_updated,
14668              is_modified          = 'Y'
14669        WHERE RESULT = 'X';
14670     ELSE
14671 
14672       if l_debug = 1 then
14673         print_DEBUG('Else Part  ______________________: ', l_debug);
14674       end if;
14675       UPDATE wms_wp_tasks_gtmp
14676          SET task_id           = DECODE(p_to_status_id,
14677                                         1,
14678                                         NULL,
14679                                         8,
14680                                         NULL,
14681                                         task_id),
14682              status            = NVL(p_to_status, status),
14683              status_id         = NVL(p_to_status_id, status_id),
14684              user_task_type    = NVL(p_user_task_type, user_task_type),
14685              user_task_type_id = NVL(p_user_task_type_id, user_task_type_id),
14686 
14687              priority = DECODE(p_clear_priority,
14688                                'Y',
14689                                NULL,
14690                                DECODE(p_priority_type,
14691                                       'I',
14692                                       NVL(priority, 0) + p_priority, -- R12: Increment priority
14693                                       'D',
14694                                       DECODE(SIGN(NVL(priority, 0) -
14695                                                   p_priority), -- R12: Decrement priority
14696                                              -1,
14697                                              0,
14698                                              +1,
14699                                              NVL(priority, 0) - p_priority,
14700                                              0,
14701                                              0),
14702                                       'S',
14703                                       NVL(p_priority, priority), -- R12: Set Constant priority value
14704                                       priority)),
14705 
14706              person               = DECODE(p_to_status_id,
14707                                            1,
14708                                            NULL,
14709                                            8,
14710                                            NULL,
14711                                            person),
14712              person_id            = DECODE(p_to_status_id,
14713                                            1,
14714                                            NULL,
14715                                            8,
14716                                            NULL,
14717                                            person_id),
14718              effective_start_date = DECODE(p_to_status_id,
14719                                            1,
14720                                            NULL,
14721                                            8,
14722                                            NULL,
14723                                            effective_start_date),
14724              effective_end_date   = DECODE(p_to_status_id,
14725                                            1,
14726                                            NULL,
14727                                            8,
14728                                            NULL,
14729                                            effective_end_date),
14730              person_resource_code = DECODE(p_to_status_id,
14731                                            1,
14732                                            NULL,
14733                                            8,
14734                                            NULL,
14735                                            person_resource_code),
14736              person_resource_id   = DECODE(p_person_resource_id,
14737                                            1,
14738                                            NULL,
14739                                            8,
14740                                            NULL,
14741                                            person_resource_id),
14742              RESULT               = 'S',
14743              error                = g_task_updated,
14744              is_modified          = 'Y'
14745        WHERE RESULT = 'X';
14746 
14747       if l_debug = 1 then
14748         print_DEBUG('Else Part : ________________2', l_debug);
14749       end if;
14750     END IF;
14751 
14752     FORALL i IN p_transaction_temp_id.FIRST .. p_transaction_temp_id.LAST
14753       UPDATE wms_wp_tasks_gtmp
14754          SET RESULT = RESULT
14755        WHERE transaction_temp_id = p_transaction_temp_id(i)
14756          AND task_type_id = p_task_type_id(i)
14757       RETURNING task_id, RESULT, error BULK COLLECT INTO x_task_id, x_result, x_message;
14758     x_return_status := 'S';
14759   EXCEPTION
14760     WHEN OTHERS THEN
14761       x_return_status := 'U';
14762       x_return_msg    := SQLERRM;
14763       print_DEBUG('Sql Error: ' || SQLERRM, l_debug);
14764   END update_task;
14765 
14766   PROCEDURE save_tasks(p_commit        BOOLEAN,
14767                        p_user_id       NUMBER,
14768                        p_login_id      NUMBER,
14769                        x_save_count    OUT NOCOPY NUMBER,
14770                        x_return_status OUT NOCOPY VARCHAR2,
14771                        x_msg_data      OUT NOCOPY VARCHAR2,
14772                        x_msg_count     OUT NOCOPY NUMBER) IS
14773     TYPE transaction_temp_id_table_type IS TABLE OF mtl_material_transactions_temp.transaction_temp_id%TYPE INDEX BY BINARY_INTEGER;
14774 
14775     TYPE wms_task_status_table_type IS TABLE OF mtl_material_transactions_temp.wms_task_status%TYPE INDEX BY BINARY_INTEGER;
14776 
14777     TYPE task_priority_table_type IS TABLE OF mtl_material_transactions_temp.task_priority%TYPE INDEX BY BINARY_INTEGER;
14778 
14779     TYPE person_id_table_type IS TABLE OF wms_dispatched_tasks.person_id%TYPE INDEX BY BINARY_INTEGER;
14780 
14781     TYPE person_resource_id_table_type IS TABLE OF wms_dispatched_tasks.person_resource_id%TYPE /* Bug 5630187 */
14782     INDEX BY BINARY_INTEGER;
14783 
14784     TYPE effective_start_date IS TABLE OF wms_dispatched_tasks.effective_start_date%TYPE INDEX BY BINARY_INTEGER;
14785 
14786     TYPE effective_end_date IS TABLE OF wms_dispatched_tasks.effective_end_date%TYPE INDEX BY BINARY_INTEGER;
14787 
14788     TYPE task_type_id IS TABLE OF wms_waveplan_tasks_temp.task_type_id%TYPE INDEX BY BINARY_INTEGER;
14789 
14790     TYPE user_task_type_id IS TABLE OF wms_waveplan_tasks_temp.user_task_type_id%TYPE -- R12: Update User Task Type Id
14791     INDEX BY BINARY_INTEGER;
14792 
14793     l_transaction_temp_id_table  transaction_temp_id_table_type;
14794     l_wms_task_status_table      wms_task_status_table_type;
14795     l_task_priority_table        task_priority_table_type;
14796     l_person_id_table            person_id_table_type;
14797     l_person_resource_id_table   person_resource_id_table_type;
14798     l_effective_start_date_table effective_start_date;
14799     l_effective_end_date_table   effective_end_date;
14800     l_task_type_id               task_type_id;
14801     l_user_task_type_id          user_task_type_id;
14802     l_error_message              VARCHAR2(120);
14803     l_update_date                DATE;
14804     l_non_cycle_count_number     NUMBER := 0;
14805     l_cycle_count_number         NUMBER := 0;
14806     l_children_task_count        NUMBER := 0;
14807     l_debug                      NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'),
14808                                                0);
14809 
14810     CURSOR cur_wwtt IS
14811       SELECT transaction_temp_id,
14812              task_type_id,
14813              mmtt_last_updated_by,
14814              mmtt_last_update_date,
14815              wdt_last_updated_by,
14816              wdt_last_update_date,
14817              person_id,
14818              person_id_original,
14819              person_resource_id,
14820              effective_start_date,
14821              effective_end_date,
14822              status_id,
14823              status_id_original,
14824              priority,
14825              priority_original,
14826              user_task_type_id,
14827              num_of_child_tasks
14828         FROM wms_wp_tasks_gtmp wwtt
14829        WHERE wwtt.is_modified = 'Y';
14830 
14831     --Patchset J: Bulk picking
14832     --Get the transaction_temp_id of the children tasks
14833     CURSOR bulk_children_tasks_cur(trx_temp_id NUMBER) IS
14834       SELECT transaction_temp_id
14835         FROM mtl_material_transactions_temp
14836        WHERE parent_line_id = trx_temp_id
14837          AND transaction_temp_id <> trx_temp_id
14838          FOR UPDATE nowait;
14839     --end bulk picking
14840 
14841     i NUMBER;
14842     record_locked EXCEPTION;
14843     PRAGMA EXCEPTION_INIT(record_locked, -54);
14844 
14845   BEGIN
14846     x_return_status := 'S';
14847     x_save_count    := 0;
14848     SAVEPOINT save_tasks;
14849 
14850     i := 1;
14851 
14852     FOR rec_wwtt IN cur_wwtt LOOP
14853       BEGIN
14854 
14855         print_DEBUG('TRANSACTION_TEMP_ID: ' ||
14856                     rec_wwtt.transaction_temp_id,
14857                     l_debug);
14858 
14859         print_DEBUG('STATUS_ID: ' || rec_wwtt.status_id, l_debug);
14860         print_DEBUG('PERSON_ID: ' || rec_wwtt.person_id, l_debug);
14861         print_DEBUG('PRIORITY: ' || rec_wwtt.priority, l_debug);
14862 
14863         IF rec_wwtt.task_type_id IN (2, 8) AND rec_wwtt.status_id = 12 THEN
14864           if l_debug = 1 then
14865             print_DEBUG('Cancelled Plan', l_debug);
14866           end if;
14867 
14868           x_save_count := x_save_count + 1;
14869           if l_debug = 1 then
14870             print_DEBUG('no of records saved are' || x_save_count, l_debug);
14871           end if;
14872 
14873           IF g_plan_cancelled IS NULL THEN
14874             fnd_message.set_name('WMS', 'WMS_PLAN_CANCELLED');
14875             g_plan_cancelled := fnd_message.get;
14876           END IF;
14877 
14878           UPDATE wms_wp_tasks_gtmp
14879              SET is_modified = 'N', RESULT = 'S', error = g_plan_cancelled
14880            WHERE transaction_temp_id = rec_wwtt.transaction_temp_id;
14881 
14882         END IF;
14883 
14884         if l_debug = 1 then
14885           print_DEBUG('In the wwtt for loop', l_debug);
14886         end if;
14887 
14888         IF rec_wwtt.task_type_id <> 3 THEN
14889           SELECT mmtt.transaction_temp_id
14890             INTO l_transaction_temp_id_table(i)
14891             FROM mtl_material_transactions_temp mmtt,
14892                  wms_dispatched_tasks           wdt
14893            WHERE mmtt.transaction_temp_id = rec_wwtt.transaction_temp_id
14894              AND mmtt.transaction_temp_id = wdt.transaction_temp_id(+)
14895              AND mmtt.wms_task_type = wdt.task_type(+)
14896              AND DECODE(wdt.status,
14897                         NULL,
14898                         NVL(mmtt.wms_task_status, 1),
14899                         wdt.status) = NVL(rec_wwtt.status_id_original, -1)
14900              AND NVL(mmtt.task_priority, -1) =
14901                  NVL(rec_wwtt.priority_original, -1)
14902              AND NVL(wdt.person_id, -1) =
14903                  NVL(rec_wwtt.person_id_original, -1)
14904              AND mmtt.last_updated_by = rec_wwtt.mmtt_last_updated_by
14905              AND mmtt.last_update_date = rec_wwtt.mmtt_last_update_date
14906              AND NVL(wdt.last_updated_by, -1) =
14907                  NVL(rec_wwtt.wdt_last_updated_by, -1)
14908              AND (wdt.last_update_date = rec_wwtt.wdt_last_update_date OR
14909                  (wdt.last_update_date IS NULL AND
14910                  rec_wwtt.wdt_last_update_date IS NULL))
14911              FOR UPDATE NOWAIT;
14912 
14913           l_non_cycle_count_number := l_non_cycle_count_number + 1;
14914 
14915         END IF;
14916 
14917         l_wms_task_status_table(i) := rec_wwtt.status_id;
14918         l_task_priority_table(i) := rec_wwtt.priority;
14919         l_person_id_table(i) := rec_wwtt.person_id;
14920         l_person_resource_id_table(i) := rec_wwtt.person_resource_id;
14921         l_effective_start_date_table(i) := sysdate;
14922         l_effective_end_date_table(i) := sysdate;
14923         l_task_type_id(i) := rec_wwtt.task_type_id;
14924         l_user_task_type_id(i) := rec_wwtt.user_task_type_id;
14925         i := i + 1;
14926 
14927         --If updating a bulk tasks, update the children tasks also.
14928         --Condition to check if it's a bulk task is with the
14929         --num_of_child_task column.
14930         --Bulk task should always have children tasks
14931         IF rec_wwtt.num_of_child_tasks IS NOT NULL AND
14932            rec_wwtt.num_of_child_tasks > 0 THEN
14933           FOR bulk_children IN bulk_children_tasks_cur(l_transaction_temp_id_table(i - 1)) LOOP
14934             l_transaction_temp_id_table(i) := bulk_children.transaction_temp_id;
14935 
14936             l_wms_task_status_table(i) := rec_wwtt.status_id;
14937             l_task_priority_table(i) := rec_wwtt.priority;
14938             l_person_id_table(i) := rec_wwtt.person_id;
14939             l_person_resource_id_table(i) := rec_wwtt.person_resource_id;
14940             l_effective_start_date_table(i) := sysdate; -- rec_wwtt.effective_start_date; --bug#6409956
14941             l_effective_end_date_table(i) := sysdate; --rec_wwtt.effective_end_date; --bug#6409956
14942             l_task_type_id(i) := rec_wwtt.task_type_id;
14943             l_user_task_type_id(i) := rec_wwtt.user_task_type_id; -- R12: Update User Task Type Id
14944             i := i + 1;
14945             l_children_task_count := l_children_task_count + 1;
14946           END LOOP;
14947         END IF;
14948         --/bulk picking
14949       EXCEPTION
14950         WHEN record_locked THEN
14951           NULL;
14952         WHEN NO_DATA_FOUND THEN
14953           NULL;
14954       END;
14955     END LOOP;
14956 
14957     x_save_count := x_save_count + l_transaction_temp_id_table.COUNT -
14958                     l_children_task_count;
14959 
14960     if l_debug = 1 then
14961       print_DEBUG('Save Count is ' || x_save_count, l_debug);
14962     end if;
14963 
14964     -- IF l_transaction_temp_id_table.COUNT > 0 THEN
14965     IF x_save_count > 0 THEN
14966       if l_debug = 1 then
14967         print_DEBUG('Save Count is ' || x_save_count, l_debug);
14968       end if;
14969 
14970       IF l_non_cycle_count_number > 0 THEN
14971         FORALL i IN l_transaction_temp_id_table.FIRST .. l_transaction_temp_id_table.LAST
14972           UPDATE mtl_material_transactions_temp
14973              SET wms_task_status       = DECODE(l_wms_task_status_table(i),
14974                                                 8,
14975                                                 8,
14976                                                 1,
14977                                                 1,
14978                                                 NULL),
14979                  task_priority         = l_task_priority_table(i),
14980                  last_update_date      = SYSDATE,
14981                  last_updated_by       = p_user_id,
14982                  last_update_login     = p_login_id,
14983                  standard_operation_id = l_user_task_type_id(i)
14984            WHERE transaction_temp_id = l_transaction_temp_id_table(i) -- R12: Update User Task Type Id
14985              AND l_task_type_id(i) <> 3;
14986         if l_debug = 1 then
14987           print_DEBUG('No of records updated are-777 ' || SQL%ROWCOUNT,
14988                       l_debug);
14989         end if;
14990       END IF;
14991 
14992       -- Delete WDT line for tasks that were queued but now are pending or unreleased
14993       DELETE wms_dispatched_tasks wdt
14994        WHERE wdt.status IN (2, 3, 9) -- R12: Delete the Active or Dispatched tasks which were updated to pending/Unreleased
14995          AND wdt.transaction_temp_id IN
14996              (SELECT transaction_temp_id
14997                 FROM wms_wp_tasks_gtmp wwtt
14998                WHERE wwtt.status_id IN (1, 8)
14999                  AND wwtt.is_modified = 'Y');
15000       if l_debug = 1 then
15001         print_DEBUG('No of records deleted are-555 ' || SQL%ROWCOUNT,
15002                     l_debug);
15003       end if;
15004 
15005       l_update_date := SYSDATE;
15006       if l_debug = 1 then
15007         print_DEBUG('inserting into WDT ' || x_save_count, l_debug);
15008       end if;
15009 
15010       -- Insert into WDT tasks that have become queued from pending or unreleased
15011       INSERT INTO wms_dispatched_tasks
15012         (task_id,
15013          transaction_temp_id,
15014          organization_id,
15015          user_task_type,
15016          person_id,
15017          effective_start_date,
15018          effective_end_date,
15019          person_resource_id,
15020          machine_resource_id,
15021          status,
15022          dispatched_time,
15023          last_update_date,
15024          last_updated_by,
15025          creation_date,
15026          created_by,
15027          last_update_login,
15028          task_type,
15029          priority,
15030          move_order_line_id,
15031          operation_plan_id,
15032          transfer_lpn_id)
15033         (SELECT wwtt.task_id,
15034                 wwtt.transaction_temp_id,
15035                 wwtt.organization_id,
15036                 NVL(wwtt.user_task_type_id, 0),
15037                 wwtt.person_id,
15038                 sysdate,
15039                 sysdate,
15040                 wwtt.person_resource_id,
15041                 NULL,
15042                 2, -- Queued
15043                 NULL,
15044                 l_update_date,
15045                 p_user_id,
15046                 l_update_date,
15047                 p_user_id,
15048                 p_login_id,
15049                 wwtt.task_type_id,
15050                 wwtt.priority,
15051                 wwtt.move_order_line_id,
15052                 wwtt.operation_plan_id,
15053                 wwtt.to_lpn_id
15054            FROM wms_wp_tasks_gtmp wwtt
15055           WHERE wwtt.status_id = 2
15056             AND wwtt.status_id_original IN (1, 8)
15057             AND wwtt.is_modified = 'Y'
15058             AND NOT EXISTS
15059           (SELECT 1
15060                    FROM wms_dispatched_tasks wdt
15061                   WHERE wdt.transaction_temp_id = wwtt.transaction_temp_id));
15062       if l_debug = 1 then
15063         print_DEBUG('No of records inserted are-444 ' || SQL%ROWCOUNT,
15064                     l_debug);
15065       end if;
15066       --  forall i IN l_transaction_temp_id_table.first..l_transaction_temp_id_table.last
15067       FORALL i IN 1 .. l_transaction_temp_id_table.COUNT
15068         UPDATE wms_dispatched_tasks
15069            SET person_id            = l_person_id_table(i),
15070                person_resource_id   = l_person_resource_id_table(i),
15071                effective_start_date = l_effective_start_date_table(i),
15072                effective_end_date   = l_effective_end_date_table(i),
15073                priority             = l_task_priority_table(i),
15074                last_update_date     = l_update_date,
15075                last_updated_by      = p_user_id,
15076                last_update_login    = p_login_id
15077          WHERE transaction_temp_id = l_transaction_temp_id_table(i);
15078       if l_debug = 1 then
15079         print_DEBUG('No of records updated are-333 ' || SQL%ROWCOUNT,
15080                     l_debug);
15081         print_DEBUG('Commiting ', l_debug);
15082       end if;
15083 
15084       IF g_task_saved IS NULL THEN
15085         fnd_message.set_name('WMS', 'WMS_TASK_SAVED');
15086         g_task_saved := fnd_message.get;
15087       END IF;
15088 
15089       --  forall i IN l_transaction_temp_id_table.first..l_transaction_temp_id_table.last
15090       FORALL i IN 1 .. l_transaction_temp_id_table.COUNT
15091         UPDATE wms_wp_tasks_gtmp
15092            SET RESULT                = 'S',
15093                error                 = g_task_saved,
15094                is_modified           = 'N',
15095                person_id_original    = l_person_id_table(i),
15096                status_id_original    = l_wms_task_status_table(i),
15097                priority_original     = l_task_priority_table(i),
15098                mmtt_last_updated_by  = p_user_id,
15099                mmtt_last_update_date = l_update_date,
15100                wdt_last_updated_by   = DECODE(l_wms_task_status_table(i),
15101                                               1,
15102                                               NULL,
15103                                               8,
15104                                               NULL,
15105                                               p_user_id),
15106                wdt_last_update_date  = TO_DATE(DECODE(l_wms_task_status_table(i),
15107                                                       1,
15108                                                       NULL,
15109                                                       8,
15110                                                       NULL,
15111                                                       TO_CHAR(l_update_date,
15112                                                               'DD-MON-YY HH24:MI:SS')),
15113                                                'DD-MON-YY HH24:MI:SS')
15114          WHERE transaction_temp_id = l_transaction_temp_id_table(i);
15115       if l_debug = 1 then
15116         print_DEBUG('No of records updated are-222 ' || SQL%ROWCOUNT,
15117                     l_debug);
15118       end if;
15119     END IF;
15120 
15121     fnd_message.set_name('FND', 'FORM_RECORD_CHANGED');
15122     l_error_message := fnd_message.get;
15123 
15124     UPDATE wms_wp_tasks_gtmp
15125        SET RESULT = 'E', error = l_error_message
15126      WHERE is_modified = 'Y';
15127 
15128     if l_debug = 1 then
15129       print_DEBUG('No of records updated are-111 ' || SQL%ROWCOUNT,
15130                   l_debug);
15131     end if;
15132 
15133     /* Bug 5507934 */
15134     IF p_commit THEN
15135       if l_debug = 1 then
15136         print_DEBUG('Commiting the record', l_debug);
15137       end if;
15138       COMMIT;
15139     END IF;
15140     /* End of 5507934 */
15141   EXCEPTION
15142     WHEN OTHERS THEN
15143       ROLLBACK TO save_tasks;
15144       x_return_status := 'U';
15145       x_msg_data      := SQLERRM;
15146       print_DEBUG('SQL error: ' || SQLERRM, l_debug);
15147   END save_tasks;
15148 
15149   FUNCTION getforcesignonflagvalue(p_transaction_temp_id IN mtl_material_transactions_temp.transaction_temp_id%TYPE,
15150                                    p_device_id           OUT NOCOPY NUMBER)
15151     RETURN VARCHAR2 IS
15152     l_force_sign_on wms_devices_b.force_sign_on_flag%TYPE;
15153   BEGIN
15154     SELECT wms_task_dispatch_device.get_eligible_device(mmtt.organization_id,
15155                                                         mmtt.subinventory_code,
15156                                                         mmtt.locator_id)
15157       INTO p_device_id
15158       FROM mtl_material_transactions_temp mmtt
15159      WHERE mmtt.transaction_temp_id = p_transaction_temp_id;
15160 
15161     SELECT force_sign_on_flag
15162       INTO l_force_sign_on
15163       FROM wms_devices_b
15164      WHERE device_id = p_device_id;
15165 
15166     RETURN l_force_sign_on;
15167   EXCEPTION
15168     WHEN OTHERS THEN
15169       RETURN 'N';
15170   END getforcesignonflagvalue;
15171 
15172   PROCEDURE set_num_of_child_tasks IS
15173     l_debug    NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
15174     l_progress NUMBER;
15175 
15176     TYPE num_of_child_tasks_type IS TABLE OF wms_wp_tasks_gtmp.num_of_child_tasks%TYPE;
15177     l_num_of_child_tasks_tbl num_of_child_tasks_type;
15178     l_parent_temp_ids_tbl    wms_wave_planning_pvt.transaction_temp_table_type;
15179   BEGIN
15180     IF l_debug = 1 THEN
15181       print_DEBUG('set_num_of_child_tasks entered ' ||
15182                   TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),
15183                   l_debug);
15184     END IF;
15185 
15186     l_progress := 10;
15187 
15188     --MMTT used to get children task.
15189     --wwtt used to get the parent task.
15190     SELECT COUNT(1), wwtt.transaction_temp_id BULK COLLECT
15191       INTO l_num_of_child_tasks_tbl, l_parent_temp_ids_tbl
15192       FROM wms_wp_tasks_gtmp wwtt, mtl_material_transactions_temp mmtt
15193      WHERE wwtt.transaction_temp_id = mmtt.parent_line_id
15194        AND wwtt.transaction_temp_id <> mmtt.transaction_temp_id
15195      GROUP BY wwtt.transaction_temp_id;
15196 
15197     l_progress := 20;
15198 
15199     IF l_num_of_child_tasks_tbl.COUNT > 0 THEN
15200       l_progress := 30;
15201       FORALL i IN l_num_of_child_tasks_tbl.FIRST .. l_num_of_child_tasks_tbl.LAST
15202         UPDATE wms_wp_tasks_gtmp wwtt
15203            SET wwtt.num_of_child_tasks = l_num_of_child_tasks_tbl(i)
15204          WHERE wwtt.transaction_temp_id = l_parent_temp_ids_tbl(i);
15205       l_progress := 40;
15206     END IF;
15207 
15208     l_progress := 50;
15209 
15210     IF l_debug = 1 THEN
15211       print_DEBUG('set_num_of_child_tasks exited ' ||
15212                   TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),
15213                   l_debug);
15214     END IF;
15215   EXCEPTION
15216     WHEN NO_DATA_FOUND THEN
15217       --no parent bulk task
15218       IF l_debug = 1 THEN
15219         print_DEBUG('set_num_of_child_tasks no_data_found after l_progress ' ||
15220                     l_progress,
15221                     l_debug);
15222 
15223         IF l_progress = 10 THEN
15224           print_DEBUG('set_num_of_child_tasks exited normally with no parent ' ||
15225                       'task found ' ||
15226                       TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'),
15227                       l_debug);
15228         END IF;
15229       END IF;
15230     WHEN OTHERS THEN
15231       IF l_debug = 1 THEN
15232         print_DEBUG('set_num_of_child_tasks OTHERS exception after l_progress ' ||
15233                     l_progress,
15234                     l_debug);
15235       END IF;
15236   END set_num_of_child_tasks;
15237 
15238   --Get Completion Status
15239   --This Procedure is being called by Wave Planning Track Completion Status Concurrent Program.
15240   --E -  Replenishment Requested
15241   --F -- Replenishment Completed
15242   /*We are mark Completed ...after all WDDs are staged
15243   then we mark Wave Closed...after all WDDs are shipped  (We are introducing one more status Wave Status = 'Closed')
15244   */
15245 
15246   PROCEDURE Update_Completion_Status_CP(errbuf  OUT NOCOPY VARCHAR2,
15247                                         retcode OUT NOCOPY NUMBER)
15248 
15249    is
15250     --Cursor to find whether the wave lines in Released Status are staged.
15251 
15252     cursor c_waves is
15253       select wwh.wave_header_id
15254         from wms_wp_wave_headers_vl wwh,
15255              (select wave_header_id, count(1) count_lines
15256                 from wms_wp_wwb_lines_v
15257                where nvl(remove_from_wave_flag, 'N') = 'N'
15258                group by wave_header_id) wwlv,
15259              (select count(1) count_completed_lines, wave_header_id
15260                 from wms_wp_wwb_lines_v
15261                where line_progress_id >= 8
15262                  and nvl(remove_from_wave_flag, 'N') = 'N'
15263                group by wave_header_id) wwlv1
15264        where wwh.wave_header_id = wwlv.wave_header_id
15265          and wwh.wave_status = 'Released'
15266          and wwlv.count_lines = wwlv1.count_completed_lines
15267          and wwlv.wave_header_id = wwlv1.wave_header_id;
15268 
15269 
15270     x_return_status varchar2(10);
15271     l_debug         NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
15272 
15273   begin
15274 
15275     savepoint check_completed_status_sp;
15276 
15277     -- Updating the Wave Status if all the lines in that Wave are Completed.
15278 
15279     for l_waves in c_waves loop
15280 
15281       print_debug('Completion Status for Wave : ' ||
15282                   l_waves.wave_header_id,
15283                   l_debug);
15284 
15285       update wms_wp_wave_headers_vl
15286          set wave_status = 'Completed', wave_Completion_time = sysdate -- should we change the column name to Wave Completion Date
15287        where wave_header_id = l_waves.wave_header_id;
15288 
15289       update wms_wp_wave_lines
15290          set pick_fill_rate = 100
15291        where wave_header_id = l_waves.wave_header_id;
15292 
15293       -- Need to check whether the wave can be closed or not
15294       print_debug('Making Pick Fill Rate For the Wave : ' ||
15295                   l_waves.wave_header_id || ' as 100',
15296                   l_debug);
15297       --  commit;
15298 
15299     end loop;
15300 
15301     -- Call to Calculate Pick Fill Rate for Released but not completed Lines
15302 
15303     wms_wave_planning_pvt.get_pick_fill_rate(x_return_status);
15304 
15305     if x_return_Status = 'S' then
15306       print_debug('Checking whether the wave can be closed. Call Check_wave_closed_status API : ',
15307                   l_debug);
15308 
15309       wms_wave_planning_pvt.Check_wave_closed_status(x_return_status);
15310     else
15311 
15312       RAISE fnd_api.g_exc_error;
15313 
15314     end if;
15315 
15316     if x_return_Status <> 'S' then
15317 
15318       RAISE fnd_api.g_exc_error;
15319 
15320     end if;
15321 
15322     commit;
15323 
15324   exception
15325 
15326     when others then
15327 
15328       print_debug('Error in update completion status : ' || SQLCODE ||
15329                   ' : ' || SQLERRM,
15330                   l_debug);
15331 
15332       rollback to check_completed_status_sp;
15333 
15334       RAISE fnd_api.g_exc_unexpected_error;
15335   end Update_Completion_Status_CP;
15336 
15337   procedure Check_wave_closed_status(x_return_status OUT NOCOPY varchar2)
15338 
15339    is
15340     -- Cursor change to the base table and compare wave line count with lines in wdd status shipped in a wave.  Ajith?????
15341     cursor c_completed_waves is
15342       select wwh.wave_header_id
15343         from wms_wp_wave_headers_vl wwh,
15344              (select wave_header_id, count(1) count_lines
15345                 from wms_wp_wwb_lines_v
15346                where nvl(remove_from_wave_flag, 'N') = 'N'
15347                group by wave_header_id) wwlv,
15348              (select count(1) count_completed_lines, wave_header_id
15349                 from wms_wp_wwb_lines_v
15350                where line_progress_id = 11
15351                  and nvl(remove_from_wave_flag, 'N') = 'N'
15352                group by wave_header_id) wwlv1
15353        where wwh.wave_header_id = wwlv.wave_header_id
15354          and wwh.wave_status in ('Completed', 'Released') -- Adding released status if direct ship is done lines will be in shipped directly
15355          and wwlv.count_lines = wwlv1.count_completed_lines
15356          and wwlv.wave_header_id = wwlv1.wave_header_id;
15357 
15358     l_debug           NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
15359     l_wave_header_tbl num_tab;
15360     i                 number := 0;
15361   begin
15362     -- Here do Bulk Update Ajith?????
15363 
15364     for l_waves in c_completed_waves loop
15365 
15366       print_debug('Closed Status for Wave : ' || l_waves.wave_header_id,
15367                   l_debug);
15368 
15369       l_wave_header_tbl(i) := l_waves.wave_header_id;
15370       i := i + 1;
15371 
15372     end loop;
15373 
15374     --  commit;
15375     if l_wave_header_tbl.count > 0 then
15376       forall k in l_wave_header_tbl.FIRST .. l_wave_header_tbl.LAST
15377         update wms_wp_wave_headers_vl
15378            set wave_status = 'Closed' -- should we change the column name to Wave Completion Date
15379          where wave_header_id = l_wave_header_tbl(k);
15380 
15381     end if;
15382 
15383     x_return_status := 'S';
15384 
15385   exception
15386 
15387     when others then
15388 
15389       x_return_status := 'E';
15390 
15391       print_debug('Error in check wave closed status : ' || SQLCODE ||
15392                   ' : ' || SQLERRM,
15393                   l_debug);
15394 
15395   end Check_wave_closed_status;
15396 
15397   --Get_pick_fill_rate
15398 --11775489 restructure to call new update_line_pick_fill_rate for each line
15399   procedure get_pick_fill_rate(x_return_status OUT NOCOPY varchar2)
15400 
15401 
15402    is
15403    l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
15404     -- Pick Fill Rate for Backordered / Replenishment Requested Status is 0.
15405     -- We calculate the Pick Fill Rate for Released lines by (sum(completed transaction qty) / requested qty ) *100.
15406 
15407     -- Transaction Qty should be converted to primary uom
15408     cursor c_lines is
15409 	select wwl.wave_line_id
15410 	from wms_wp_wave_lines wwl, --11775489 added org_id join...
15411          wms_wp_wave_headers_vl wwh,
15412 		 wsh_delivery_details wdd
15413        where wwh.wave_header_id = wwl.wave_header_id
15414          and (wwh.wave_status = 'Released')
15415          and nvl(wwl.remove_from_wave_flag, 'N') = 'N'
15416 		 and wdd.delivery_detail_id = wwl.delivery_detail_id
15417 		 and wdd.organization_id = wwl.organization_id
15418          and wdd.released_status in ('B', 'E', 'Y');
15419   begin
15420     for l_line in c_lines loop
15421       update_line_pick_fill_rate(l_line.wave_line_id);
15422     end loop;
15423 
15424     x_return_status := 'S';
15425   EXCEPTION
15426     WHEN OTHERS THEN
15427       print_debug('Error : ' || SQLCODE || ' : ' || SQLERRM, l_debug);
15428       x_return_status := FND_API.G_RET_STS_ERROR;
15429 
15430 end get_pick_fill_rate;
15431 
15432   function get_loaded_status(p_Delivery_Detail_id in number) return number
15433 
15434    is
15435     v_loaded number := 0;
15436     v_total  number := 0;
15437     v_status number := 0;
15438     l_debug  NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
15439 
15440   begin
15441 	--bug12322720 changed this query to join through move_order_line_id instead of trx_source_line_id
15442 	--also modified to join with wdt
15443     SELECT Sum(Decode(Nvl(wdt.status,0), 4, 1, 0)) loaded,
15444            Count(1) total_tasked
15445 	  into v_loaded, v_total
15446       FROM wsh_delivery_details wdd, mtl_material_transactions_temp mmtt, wms_dispatched_tasks wdt
15447       WHERE wdd.delivery_detail_id = p_Delivery_Detail_id
15448 	  AND wdd.move_order_line_id = mmtt.move_order_line_id
15449       AND wdt.transaction_temp_id (+) = mmtt.TRANSACTION_temp_id;
15450 
15451     if v_loaded < v_total and v_loaded >= 1 then
15452 
15453       v_status := 1; -- Partially Loaded
15454 
15455     elsif v_loaded = v_total then
15456       v_status := 2; -- Loaded
15457 
15458     end if;
15459 
15460     return v_status;
15461 
15462   exception
15463 
15464     when others then
15465 
15466       print_debug('Error in get Loaded Status ' || SQLCODE || SQLERRM,
15467                   l_Debug);
15468 
15469       return - 1;
15470 
15471   end get_loaded_status;
15472 
15473   PROCEDURE catch_wave_exceptions_cp(errbuf                         OUT nocopy VARCHAR2,
15474                                      retcode                        OUT nocopy NUMBER,
15475                                      p_exception_name               IN VARCHAR2,
15476                                      p_organization_id              IN NUMBER,
15477                                      p_wave                         IN NUMBER,
15478                                      p_exception_entity             IN VARCHAR2,
15479                                      p_progress_stage               IN VARCHAR2,
15480                                      p_completion_threshold         IN NUMBER,
15481                                      p_high_sev_exception_threshold IN NUMBER,
15482                                      p_low_sev_exception_threshold  IN NUMBER,
15483                                      p_take_corrective_measures     IN VARCHAR2,
15484                                      p_release_back_ordered_lines   IN VARCHAR2,
15485                                      p_action_name                  IN VARCHAR2) IS
15486 
15487     l_total_lines            NUMBER;
15488     l_perfect_lines          NUMBER;
15489     l_progress_stage         NUMBER;
15490     l_temp_previous_order_id NUMBER DEFAULT NULL;
15491     l_temp_previous_line_id  NUMBER DEFAULT NULL;
15492     l_exception_id           NUMBER := -1;
15493     l_previous_order_id      NUMBER;
15494     l_previous_line_id       NUMBER;
15495     l_debug                  NUMBER := nvl(fnd_profile.VALUE('INV_DEBUG_TRACE'),
15496                                            0);
15497 
15498     l_api_is_implemented BOOLEAN;
15499     l_return_status      varchar2(30);
15500     l_msg_count          varchar2(100);
15501     l_msg_data           varchar2(100);
15502 
15503     -- cursor c_get_waves fetches wave if wave id is given else all the waves in released status.
15504     -- cursor also fetches the date scheduled for that particular wave.
15505     CURSOR c_get_waves IS
15506       SELECT DISTINCT wwl.wave_header_id,
15507                       MIN(nvl(wdab.end_time,
15508                               (nvl(wts.planned_arrival_date,
15509                                    nvl(wnd.earliest_pickup_date,
15510                                        wdd.date_scheduled))))) date_scheduled
15511         FROM wms_wp_wave_lines wwl, --11775489 added org_id join...
15512              wsh_delivery_details wdd,
15513              (SELECT wave_header_id wave_id
15514                 FROM wms_wp_wave_headers_vl
15515                WHERE ((wave_header_id = p_wave) AND
15516                      LOWER(wave_status) in ('released', 'completed'))
15517                   OR (p_wave IS NULL AND
15518                      LOWER(wave_status) in ('released', 'completed'))) wwh,
15519              wsh_delivery_assignments wda,
15520              wsh_delivery_legs wdl,
15521              wsh_new_deliveries wnd,
15522              wsh_trip_stops wts,
15523              wms_dock_appointments_b wdab
15524        WHERE wwl.wave_header_id = wwh.wave_id
15525          AND wwl.delivery_detail_id = wdd.delivery_detail_id
15526 		 and wwl.organization_id = wdd.organization_id
15527          AND wdd.delivery_detail_id = wda.delivery_detail_id
15528          AND wda.delivery_id = wnd.delivery_id(+)
15529          AND wnd.delivery_id = wdl.delivery_id(+)
15530          AND wdl.pick_up_stop_id = wts.stop_id(+)
15531          AND wts.stop_id = wdab.trip_stop(+)
15532          AND wdd.organization_id = p_organization_id
15533 		 and nvl(wwl.remove_from_Wave_flag, 'N') <> 'Y' --11775489 filter out lines no longer in wave
15534        GROUP BY wwl.wave_header_id;
15535 
15536     -- cursor def for getting trips and corresponding date scheduled of a particular wave.
15537     CURSOR c_get_trips(p_wave_id NUMBER) IS
15538       SELECT DISTINCT wts.trip_id,
15539                       nvl(wdab.end_time, wts.planned_arrival_date) date_scheduled
15540         FROM wms_wp_wave_lines        wwl, --11775489 added org_id join...
15541              wsh_delivery_details     wdd,
15542              wsh_delivery_assignments wda,
15543              wsh_delivery_legs        wdl,
15544              wsh_new_deliveries       wnd,
15545              wsh_trip_stops           wts,
15546              wms_dock_appointments_b  wdab
15547        WHERE wwl.wave_header_id = p_wave_id
15548          AND wwl.delivery_detail_id = wdd.delivery_detail_id
15549 		 and wwl.organization_id = wdd.organization_id
15550          AND wdd.delivery_detail_id = wda.delivery_detail_id
15551          AND wda.delivery_id = wnd.delivery_id(+)
15552          AND wnd.delivery_id = wdl.delivery_id(+)
15553          AND wdl.pick_up_stop_id = wts.stop_id(+)
15554          AND wts.trip_id IS NOT NULL
15555          AND wts.stop_id = wdab.trip_stop(+)
15556 		 and nvl(wwl.remove_from_Wave_flag, 'N') <> 'Y' --11775489 filter out lines no longer in wave
15557          AND wdd.organization_id = p_organization_id;
15558 
15559     -- cursor def for getting deliveries and corresponding date scheduled of a particular wave.
15560     CURSOR c_get_deliveries(p_wave_id NUMBER) IS
15561       SELECT DISTINCT wts.trip_id,
15562                       wda.delivery_id,
15563                       nvl(wdab.end_time,
15564                           (nvl(wts.planned_arrival_date,
15565                                wnd.earliest_pickup_date))) date_scheduled
15566         FROM wms_wp_wave_lines        wwl, --11775489 added org_id join...
15567              wsh_delivery_details     wdd,
15568              wsh_delivery_assignments wda,
15569              wsh_delivery_legs        wdl,
15570              wsh_new_deliveries       wnd,
15571              wsh_trip_stops           wts,
15572              wms_dock_appointments_b  wdab
15573        WHERE wwl.wave_header_id = p_wave_id
15574          AND wwl.delivery_detail_id = wdd.delivery_detail_id
15575 		 and wwl.organization_id = wdd.organization_id
15576          AND wdd.delivery_detail_id = wda.delivery_detail_id
15577          AND wda.delivery_id IS NOT NULL
15578          AND wda.delivery_id = wnd.delivery_id(+)
15579          AND wnd.delivery_id = wdl.delivery_id(+)
15580          AND wdl.pick_up_stop_id = wts.stop_id(+)
15581          AND wts.stop_id = wdab.trip_stop(+)
15582 		 and nvl(wwl.remove_from_Wave_flag, 'N') <> 'Y' --11775489 filter out lines no longer in wave
15583          AND wdd.organization_id = p_organization_id;
15584 
15585     -- cursor def for getting orders and corresponding date scheduled that belong to particular wave.
15586     CURSOR c_get_so(p_wave_id NUMBER) IS
15587       SELECT DISTINCT wts.trip_id,
15588                       wda.delivery_id,
15589                       shi.source_header_id,
15590                       shi.date_scheduled,
15591                       wdd.source_header_number
15592         FROM (SELECT wdd.source_header_id,
15593                      MIN(nvl(wdab.end_time,
15594                              (nvl(wts.planned_arrival_date,
15595                                   nvl(wnd.earliest_pickup_date,
15596                                       wdd.date_scheduled))))) date_scheduled
15597                 FROM wms_wp_wave_lines        wwl, --11775489 added org_id join...
15598                      wsh_delivery_details     wdd,
15599                      wsh_delivery_assignments wda,
15600                      wsh_delivery_legs        wdl,
15601                      wsh_new_deliveries       wnd,
15602                      wsh_trip_stops           wts,
15603                      wms_dock_appointments_b  wdab
15604                WHERE wwl.wave_header_id = p_wave_id
15605                  AND wwl.delivery_detail_id = wdd.delivery_detail_id
15606 				 and wwl.organization_id = wdd.organization_id
15607                  AND wdd.delivery_detail_id = wda.delivery_detail_id
15608                  AND wda.delivery_id = wnd.delivery_id(+)
15609                  AND wnd.delivery_id = wdl.delivery_id(+)
15610                  AND wdl.pick_up_stop_id = wts.stop_id(+)
15611                  AND wts.stop_id = wdab.trip_stop(+)
15612                  AND wdd.organization_id = p_organization_id
15613 				 and nvl(wwl.remove_from_Wave_flag, 'N') <> 'Y' --11775489 filter out lines no longer in wave
15614                GROUP BY wdd.source_header_id) shi,
15615              wsh_delivery_details wdd,
15616              wsh_delivery_assignments wda,
15617              wsh_delivery_legs wdl,
15618              wsh_new_deliveries wnd,
15619              wsh_trip_stops wts
15620        WHERE wdd.source_header_id = shi.source_header_id
15621          AND wdd.delivery_detail_id = wda.delivery_detail_id
15622          AND wda.delivery_id = wnd.delivery_id(+)
15623          AND wnd.delivery_id = wdl.delivery_id(+)
15624          AND wdl.pick_up_stop_id = wts.stop_id(+)
15625        ORDER BY shi.source_header_id;
15626     -- Don't remove this order by.. Usefull for check_so procedure
15627 
15628     -- cursor def for getting all the lines and corresponding date scheduled in a particular wave.
15629     CURSOR c_get_lines(p_wave_id NUMBER) IS
15630       SELECT DISTINCT wts.trip_id,
15631                       wda.delivery_id,
15632                       shi.source_line_id,
15633                       shi.date_scheduled,
15634                       wdd.source_header_number
15635         FROM (SELECT wdd.source_line_id,
15636                      MIN(nvl(wdab.end_time,
15637                              (nvl(wts.planned_arrival_date,
15638                                   nvl(wnd.earliest_pickup_date,
15639                                       wdd.date_scheduled))))) date_scheduled
15640                 FROM wms_wp_wave_lines        wwl, --11775489 added org_id join...
15641                      wsh_delivery_details     wdd,
15642                      wsh_delivery_assignments wda,
15643                      wsh_delivery_legs        wdl,
15644                      wsh_new_deliveries       wnd,
15645                      wsh_trip_stops           wts,
15646                      wms_dock_appointments_b  wdab
15647                WHERE wwl.wave_header_id = p_wave_id
15648                  AND wwl.delivery_detail_id = wdd.delivery_detail_id
15649 				 and wwl.organization_id = wdd.organization_id
15650                  AND wdd.delivery_detail_id = wda.delivery_detail_id
15651                  AND wda.delivery_id = wnd.delivery_id(+)
15652                  AND wnd.delivery_id = wdl.delivery_id(+)
15653                  AND wdl.pick_up_stop_id = wts.stop_id(+)
15654                  AND wts.stop_id = wdab.trip_stop(+)
15655                  AND wdd.organization_id = p_organization_id
15656 				 and nvl(wwl.remove_from_Wave_flag, 'N') <> 'Y' --11775489 filter out lines no longer in wave
15657                GROUP BY wdd.source_line_id) shi,
15658              wms_wp_wave_lines wwl,--11775489 added org_id join...
15659              wsh_delivery_details wdd,
15660              wsh_delivery_assignments wda,
15661              wsh_delivery_legs wdl,
15662              wsh_new_deliveries wnd,
15663              wsh_trip_stops wts
15664        WHERE wdd.source_line_id = shi.source_line_id
15665          AND wdd.delivery_detail_id = wwl.delivery_detail_id
15666 		 and wwl.organization_id = wdd.organization_id
15667          AND wdd.delivery_detail_id = wda.delivery_detail_id
15668          AND wda.delivery_id = wnd.delivery_id(+)
15669          AND wnd.delivery_id = wdl.delivery_id(+)
15670          AND wdl.pick_up_stop_id = wts.stop_id(+)
15671 		 and nvl(wwl.remove_from_Wave_flag, 'N') <> 'Y' --11775489 filter out lines no longer in wave
15672        order by shi.source_line_id;
15673 
15674 
15675     l_progress  NUMBER;
15676     l_exception VARCHAR2(1) := 'N';
15677 
15678   BEGIN
15679     wms_wp_custom_apis_pub.Get_wave_exceptions_cust(x_api_is_implemented           => l_api_is_implemented,
15680                                                     p_exception_name               => p_exception_name,
15681                                                     p_organization_id              => p_organization_id,
15682                                                     p_wave                         => p_wave,
15683                                                     p_exception_entity             => p_exception_entity,
15684                                                     p_progress_stage               => p_progress_stage,
15685                                                     p_completion_threshold         => p_completion_threshold,
15686                                                     p_high_sev_exception_threshold => p_high_sev_exception_threshold,
15687                                                     p_low_sev_exception_threshold  => p_low_sev_exception_threshold,
15688                                                     p_take_corrective_measures     => p_take_corrective_measures,
15689                                                     p_release_back_ordered_lines   => p_release_back_ordered_lines,
15690                                                     p_action_name                  => p_action_name,
15691                                                     x_return_status                => l_return_status,
15692                                                     x_msg_count                    => l_msg_count,
15693                                                     x_msg_data                     => l_msg_data);
15694 
15695     IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
15696       print_debug('Error returned from Get_wave_exceptions_cust in api catch_wave_exceptions_cp ',
15697                   l_debug);
15698       RAISE FND_API.G_EXC_ERROR;
15699     ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
15700       print_debug('Unexpected errror from Get_wave_exceptions_cust api in catch_wave_exceptions_cp ',
15701                   l_debug);
15702       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
15703     END IF;
15704 
15705     IF l_api_is_implemented THEN
15706       print_debug('Custom API Implemented for create exceptions ', l_debug);
15707       /*print_debug('take_corrective_measure is ' || p_take_corrective_measures, l_debug);
15708       IF (p_take_corrective_measures = 'Yes') THEN
15709               -- call api to take corrective measures
15710               take_corrective_measures(l_exception_id,
15711                                        p_wave_id,
15712                                        p_entity,
15713                                        p_entity_value,
15714                                        p_release_back_ordered_lines,
15715                                        p_action_name,
15716                                        p_organization_id);
15717 
15718       END IF;
15719       */
15720     else
15721 
15722       IF (p_progress_stage = 'Ready to Release') THEN
15723         l_progress_stage := 1;
15724       ELSIF (p_progress_stage = 'Backordered') THEN
15725         l_progress_stage := 2;
15726       ELSIF (p_progress_stage = 'Crossdock Planned') THEN
15727         l_progress_stage := 3;
15728       ELSIF (p_progress_stage = 'Replenishment Planned') THEN
15729         l_progress_stage := 4;
15730       ELSIF (p_progress_stage = 'Tasked') THEN
15731         l_progress_stage := 5;
15732       ELSIF (p_progress_stage = 'Picked') THEN
15733         l_progress_stage := 7; -- skipping 6 here, as it is used for partially loaded in wms_wp_wwb_lines_v
15734       ELSIF (p_progress_stage = 'Packed') THEN
15735         l_progress_stage := 9;
15736       ELSIF (p_progress_stage = 'Staged') THEN
15737         l_progress_stage := 8;
15738       ELSIF (p_progress_stage = 'Loaded to Dock') THEN
15739         l_progress_stage := 10;
15740       END IF;
15741 
15742       print_debug('Entity is ' || p_exception_entity, l_debug);
15743       print_debug('p_progress Stage is ' || p_progress_stage, l_debug);
15744       print_debug('l_Progress Stage is ' || l_progress_stage, l_debug);
15745       FOR l_get_waves IN c_get_waves LOOP
15746 
15747         IF (p_exception_entity = 'Wave') THEN
15748           print_debug('Entity: Wave; Wave id: ' ||
15749                       l_get_waves.wave_header_id,
15750                       l_debug);
15751           print_debug('Entity: Wave; Date_Scheduled: ' ||
15752                       l_get_waves.date_scheduled,
15753                       l_debug);
15754 
15755           l_exception := 'N';
15756 
15757           BEGIN
15758 
15759             -- used to find if any record exists in the wms_wave_exceptions table for this wave.
15760             -- if it exists fetches the corresponding exception_id else fetches -1.
15761             l_exception_id := -1;
15762             SELECT exception_id
15763               INTO l_exception_id
15764               FROM wms_wp_wave_exceptions_vl
15765              WHERE wave_header_id = l_get_waves.wave_header_id
15766                AND exception_entity = p_exception_entity
15767                AND exception_stage = p_progress_stage
15768                AND LOWER(status) <> 'closed';
15769 
15770           EXCEPTION
15771             WHEN no_data_found THEN
15772               l_exception_id := -1;
15773           END;
15774 
15775           print_debug('l_exception_id :=' || l_exception_id, l_debug);
15776 
15777           BEGIN
15778 
15779             SELECT Count(*),
15780                    Sum(CASE
15781                          WHEN line_progress_id >= l_progress_stage THEN
15782                           1
15783                          ELSE
15784                           0
15785                        END)
15786               INTO l_total_lines, l_perfect_lines
15787               FROM wms_wp_wwb_lines_v
15788              WHERE wave_header_id = l_get_waves.wave_header_id
15789                AND organization_id = p_organization_id
15790                and nvl(remove_from_Wave_flag, 'N') <> 'Y';
15791 
15792           EXCEPTION
15793 
15794             when others then
15795               l_exception := 'Y';
15796 
15797           END;
15798 
15799           if (l_exception = 'N') then
15800 
15801             actionable_exceptions(l_perfect_lines,
15802                                   l_total_lines,
15803                                   p_completion_threshold,
15804                                   p_low_sev_exception_threshold,
15805                                   p_high_sev_exception_threshold,
15806                                   l_get_waves.date_scheduled,
15807                                   l_exception_id,
15808                                   p_exception_name,
15809                                   l_get_waves.wave_header_id,
15810                                   NULL,
15811                                   NULL,
15812                                   NULL,
15813                                   NULL,
15814                                   NULL,
15815                                   l_previous_order_id,
15816                                   NULL,
15817                                   l_previous_line_id,
15818                                   p_exception_entity,
15819                                   l_get_waves.wave_header_id,
15820                                   p_progress_stage,
15821                                   p_take_corrective_measures,
15822                                   p_release_back_ordered_lines,
15823                                   p_action_name,
15824                                   p_organization_id);
15825 
15826           end if;
15827 
15828         ELSIF (p_exception_entity = 'Trip') THEN
15829           FOR l_get_trips IN c_get_trips(l_get_waves.wave_header_id) LOOP
15830             print_debug('wave_header_id ' || l_get_waves.wave_header_id,
15831                         l_debug);
15832             print_debug('trip_id ' || l_get_trips.trip_id, l_debug);
15833             print_debug('date_scheduled ' || l_get_trips.date_scheduled,
15834                         l_debug);
15835 
15836             l_exception := 'N';
15837 
15838             BEGIN
15839 
15840               -- used to find if any record exists in the wms_wave_exceptions table for this trip.
15841               -- if it exists fetches the corresponding exception_id else fetches -1.
15842               l_exception_id := -1;
15843               SELECT exception_id
15844                 INTO l_exception_id
15845                 FROM wms_wp_wave_exceptions_vl
15846                WHERE wave_header_id = l_get_waves.wave_header_id
15847                  AND exception_entity = p_exception_entity
15848                  AND exception_stage = p_progress_stage
15849                  AND trip_id = l_get_trips.trip_id
15850                  AND LOWER(status) <> 'closed';
15851 
15852             EXCEPTION
15853               WHEN no_data_found THEN
15854                 l_exception_id := -1;
15855             END;
15856 
15857             print_debug('l_exception_id :=' || l_exception_id, l_debug);
15858 
15859             BEGIN
15860 
15861               SELECT Count(*),
15862                      Sum(CASE
15863                            WHEN line_progress_id >= l_progress_stage THEN
15864                             1
15865                            ELSE
15866                             0
15867                          END)
15868                 INTO l_total_lines, l_perfect_lines
15869                 FROM wms_wp_wwb_lines_v
15870                WHERE wave_header_id = l_get_waves.wave_header_id
15871                  AND TRIP = l_get_trips.trip_id
15872                  AND organization_id = p_organization_id
15873                  and nvl(remove_from_Wave_flag, 'N') <> 'Y';
15874 
15875             EXCEPTION
15876 
15877               when others then
15878                 l_exception := 'Y';
15879 
15880             END;
15881 
15882             if (l_exception = 'N') then
15883               actionable_exceptions(l_perfect_lines,
15884                                     l_total_lines,
15885                                     p_completion_threshold,
15886                                     p_low_sev_exception_threshold,
15887                                     p_high_sev_exception_threshold,
15888                                     l_get_trips.date_scheduled,
15889                                     l_exception_id,
15890                                     p_exception_name,
15891                                     l_get_waves.wave_header_id,
15892                                     l_get_trips.trip_id,
15893                                     NULL,
15894                                     NULL,
15895                                     NULL,
15896                                     NULL,
15897                                     l_previous_order_id,
15898                                     NULL,
15899                                     l_previous_line_id,
15900                                     p_exception_entity,
15901                                     l_get_trips.trip_id,
15902                                     p_progress_stage,
15903                                     p_take_corrective_measures,
15904                                     p_release_back_ordered_lines,
15905                                     p_action_name,
15906                                     p_organization_id);
15907 
15908             end if;
15909           END LOOP;
15910 
15911         ELSIF (p_exception_entity = 'Delivery') THEN
15912           FOR l_get_deliveries IN c_get_deliveries(l_get_waves.wave_header_id) LOOP
15913             print_debug('wave_header_id ' || l_get_waves.wave_header_id,
15914                         l_debug);
15915             print_debug('trip_id ' || l_get_deliveries.trip_id, l_debug);
15916             print_debug('delivery_id ' || l_get_deliveries.delivery_id,
15917                         l_debug);
15918             print_debug('date_scheduled ' ||
15919                         l_get_deliveries.date_scheduled,
15920                         l_debug);
15921 
15922             l_exception := 'N';
15923 
15924             BEGIN
15925 
15926               -- used to find if any record exists in the wms_wave_exceptions table for this delivery.
15927               -- if it exists fetches the corresponding exception_id else fetches -1.
15928               l_exception_id := -1;
15929               SELECT exception_id
15930                 INTO l_exception_id
15931                 FROM wms_wp_wave_exceptions_vl
15932                WHERE wave_header_id = l_get_waves.wave_header_id
15933                  AND exception_entity = p_exception_entity
15934                  AND exception_stage = p_progress_stage
15935                  AND delivery_id = l_get_deliveries.delivery_id
15936                  AND LOWER(status) <> 'closed';
15937 
15938             EXCEPTION
15939               WHEN no_data_found THEN
15940                 l_exception_id := -1;
15941             END;
15942 
15943             print_debug('l_exception_id :=' || l_exception_id, l_debug);
15944 
15945             BEGIN
15946 
15947               SELECT Count(*),
15948                      Sum(CASE
15949                            WHEN line_progress_id >= l_progress_stage THEN
15950                             1
15951                            ELSE
15952                             0
15953                          END)
15954                 INTO l_total_lines, l_perfect_lines
15955                 FROM wms_wp_wwb_lines_v
15956                WHERE wave_header_id = l_get_waves.wave_header_id
15957                  AND delivery_id = l_get_deliveries.delivery_id
15958                  AND organization_id = p_organization_id
15959                  and nvl(remove_from_Wave_flag, 'N') <> 'Y';
15960 
15961             EXCEPTION
15962 
15963               when others then
15964                 l_exception := 'Y';
15965 
15966             END;
15967 
15968             if (l_exception = 'N') then
15969 
15970               actionable_exceptions(l_perfect_lines,
15971                                     l_total_lines,
15972                                     p_completion_threshold,
15973                                     p_low_sev_exception_threshold,
15974                                     p_high_sev_exception_threshold,
15975                                     l_get_deliveries.date_scheduled,
15976                                     l_exception_id,
15977                                     p_exception_name,
15978                                     l_get_waves.wave_header_id,
15979                                     l_get_deliveries.trip_id,
15980                                     l_get_deliveries.delivery_id,
15981                                     NULL,
15982                                     NULL,
15983                                     NULL,
15984                                     l_previous_order_id,
15985                                     NULL,
15986                                     l_previous_line_id,
15987                                     p_exception_entity,
15988                                     l_get_deliveries.delivery_id,
15989                                     p_progress_stage,
15990                                     p_take_corrective_measures,
15991                                     p_release_back_ordered_lines,
15992                                     p_action_name,
15993                                     p_organization_id);
15994 
15995             end if;
15996           END LOOP;
15997 
15998         ELSIF (p_exception_entity = 'Order') THEN
15999           FOR l_get_so IN c_get_so(l_get_waves.wave_header_id) LOOP
16000             print_debug('wave_header_id ' || l_get_waves.wave_header_id,
16001                         l_debug);
16002             print_debug('trip_id ' || l_get_so.trip_id, l_debug);
16003             print_debug('delivery_id ' || l_get_so.delivery_id, l_debug);
16004             print_debug('Order number ' || l_get_so.source_header_number,
16005                         l_debug);
16006             print_debug('date_scheduled ' || l_get_so.date_scheduled,
16007                         l_debug);
16008 
16009             l_exception := 'N';
16010 
16011             BEGIN
16012 
16013               -- used to find if any record exists in the wms_wave_exceptions table for this Order.
16014               -- if it exists fetches the corresponding exception_id else fetches -1.
16015               l_exception_id := -1;
16016               SELECT exception_id
16017                 INTO l_exception_id
16018                 FROM wms_wp_wave_exceptions_vl
16019                WHERE wave_header_id = l_get_waves.wave_header_id
16020                  AND exception_entity = p_exception_entity
16021                  AND exception_stage = p_progress_stage
16022                  AND order_number = l_get_so.source_header_number
16023                  AND LOWER(status) <> 'closed';
16024 
16025             EXCEPTION
16026               WHEN no_data_found THEN
16027                 l_exception_id := -1;
16028             END;
16029 
16030             print_debug('l_exception_id :=' || l_exception_id, l_debug);
16031 
16032             BEGIN
16033 
16034               SELECT Count(*),
16035                      Sum(CASE
16036                            WHEN line_progress_id >= l_progress_stage THEN
16037                             1
16038                            ELSE
16039                             0
16040                          END)
16041                 INTO l_total_lines, l_perfect_lines
16042                 FROM wms_wp_wwb_lines_v
16043                WHERE wave_header_id = l_get_waves.wave_header_id
16044                  AND source_header_id = l_get_so.source_header_id
16045                  AND organization_id = p_organization_id
16046                  and nvl(remove_from_Wave_flag, 'N') <> 'Y';
16047 
16048             EXCEPTION
16049 
16050               when others then
16051 
16052                 l_exception := 'Y';
16053 
16054             END;
16055 
16056             if (l_exception = 'N') then
16057 
16058               l_previous_order_id := l_temp_previous_order_id;
16059 
16060               actionable_exceptions(l_perfect_lines,
16061                                     l_total_lines,
16062                                     p_completion_threshold,
16063                                     p_low_sev_exception_threshold,
16064                                     p_high_sev_exception_threshold,
16065                                     l_get_so.date_scheduled,
16066                                     l_exception_id,
16067                                     p_exception_name,
16068                                     l_get_waves.wave_header_id,
16069                                     l_get_so.trip_id,
16070                                     l_get_so.delivery_id,
16071                                     l_get_so.source_header_number,
16072                                     NULL,
16073                                     l_get_so.source_header_id,
16074                                     l_previous_order_id,
16075                                     NULL,
16076                                     l_previous_line_id,
16077                                     p_exception_entity,
16078                                     l_get_so.source_header_number,
16079                                     p_progress_stage,
16080                                     p_take_corrective_measures,
16081                                     p_release_back_ordered_lines,
16082                                     p_action_name,
16083                                     p_organization_id);
16084 
16085               l_temp_previous_order_id := l_previous_order_id;
16086 
16087             end if;
16088           END LOOP;
16089 
16090         ELSIF (p_exception_entity = 'Order Line') THEN
16091           FOR l_get_lines IN c_get_lines(l_get_waves.wave_header_id) LOOP
16092             print_debug('wave_header_id ' || l_get_waves.wave_header_id,
16093                         l_debug);
16094             print_debug('trip_id ' || l_get_lines.trip_id, l_debug);
16095             print_debug('delivery_id ' || l_get_lines.delivery_id, l_debug);
16096             print_debug('order number ' ||
16097                         l_get_lines.source_header_number,
16098                         l_debug);
16099             print_debug('line id ' || l_get_lines.source_line_id, l_debug);
16100             print_debug('date_scheduled ' || l_get_lines.date_scheduled,
16101                         l_debug);
16102 
16103             l_exception := 'N';
16104 
16105             BEGIN
16106 
16107               -- used to find if any record exists in the wms_wave_exceptions table for this trip.
16108               -- if it exists fetches the corresponding exception_id else fetches -1.
16109               l_exception_id := -1;
16110               SELECT exception_id
16111                 INTO l_exception_id
16112                 FROM wms_wp_wave_exceptions_vl
16113                WHERE wave_header_id = l_get_waves.wave_header_id
16114                  AND exception_entity = p_exception_entity
16115                  AND exception_stage = p_progress_stage
16116                  AND ORDER_LINE_ID = l_get_lines.source_line_id
16117                  AND LOWER(status) <> 'closed';
16118 
16119             EXCEPTION
16120               WHEN no_data_found THEN
16121                 l_exception_id := -1;
16122             END;
16123 
16124             print_debug('l_exception_id :=' || l_exception_id, l_debug);
16125 
16126             BEGIN
16127 
16128               SELECT Count(*),
16129                      Sum(CASE
16130                            WHEN line_progress_id >= l_progress_stage THEN
16131                             1
16132                            ELSE
16133                             0
16134                          END)
16135                 INTO l_total_lines, l_perfect_lines
16136                 FROM wms_wp_wwb_lines_v
16137                WHERE wave_header_id = l_get_waves.wave_header_id
16138                  AND source_line_id = l_get_lines.source_line_id
16139                  AND organization_id = p_organization_id
16140                  and nvl(remove_from_Wave_flag, 'N') <> 'Y';
16141 
16142             EXCEPTION
16143 
16144               when others then
16145                 l_exception := 'Y';
16146 
16147             END;
16148 
16149             if (l_exception = 'N') then
16150               actionable_exceptions(l_perfect_lines,
16151                                     l_total_lines,
16152                                     p_completion_threshold,
16153                                     p_low_sev_exception_threshold,
16154                                     p_high_sev_exception_threshold,
16155                                     l_get_lines.date_scheduled,
16156                                     l_exception_id,
16157                                     p_exception_name,
16158                                     l_get_waves.wave_header_id,
16159                                     l_get_lines.trip_id,
16160                                     l_get_lines.delivery_id,
16161                                     l_get_lines.source_header_number,
16162                                     l_get_lines.source_line_id,
16163                                     NULL,
16164                                     l_previous_order_id,
16165                                     l_get_lines.source_line_id,
16166                                     l_previous_line_id,
16167                                     p_exception_entity,
16168                                     l_get_lines.source_line_id,
16169                                     p_progress_stage,
16170                                     p_take_corrective_measures,
16171                                     p_release_back_ordered_lines,
16172                                     p_action_name,
16173                                     p_organization_id);
16174 
16175             end if;
16176           END LOOP;
16177         END IF;
16178 
16179       END LOOP;
16180     end if;
16181 
16182   EXCEPTION
16183     WHEN others THEN
16184       print_debug('Unknown exception in Exceptions CP ' || SQLCODE || ':' ||
16185                   sqlerrm,
16186                   l_debug);
16187       RAISE fnd_api.g_exc_unexpected_error;
16188   END catch_wave_exceptions_cp;
16189 
16190   PROCEDURE insert_purge_exceptions(p_exception_name       VARCHAR2,
16191                                     p_exception_entity     VARCHAR2,
16192                                     p_exception_level      VARCHAR2,
16193                                     p_completion_threshold NUMBER,
16194                                     p_progress_stage       VARCHAR2,
16195                                     p_exception_threshold  NUMBER,
16196                                     p_wave_id              NUMBER,
16197                                     p_trip_id              NUMBER,
16198                                     p_delivery_id          NUMBER,
16199                                     p_order_number         NUMBER,
16200                                     p_order_line_id        NUMBER,
16201                                     x_return_status        OUT NOCOPY VARCHAR2) IS
16202 
16203     l_currval  NUMBER;
16204     l_msg      VARCHAR2(1000);
16205     l_addl_msg VARCHAR2(1000);
16206     l_debug    NUMBER := nvl(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
16207     l_null     NUMBER := null;
16208 
16209   BEGIN
16210 
16211     -- Procedure inserts a record into the exceptions table
16212     print_debug('In insert_purge_exceptions procedure', l_debug);
16213 
16214     l_msg      := p_exception_level || ' exception for ' ||
16215                   p_exception_entity;
16216     l_addl_msg := '(' || p_completion_threshold || ' % lines are not ' ||
16217                   p_progress_stage || ' within ';
16218     l_addl_msg := l_addl_msg || p_exception_threshold ||
16219                   ' hours of shipment )';
16220     l_msg      := l_msg || l_addl_msg;
16221 
16222     print_debug('message for exception is : ' || l_msg, l_debug);
16223 
16224      SELECT wms_WP_WAVE_exceptions_s.NEXTVAL INTO l_seq_val FROM dual;     -- Changed
16225 
16226     WMS_WP_WAVE_EXCEPTIONS_PKG.INSERT_ROW(
16227                                           -- X_ROWID => l_null, --
16228                                           X_EXCEPTION_ID          => l_seq_val,
16229                                           X_EXCEPTION_ENTITY      => p_exception_entity,
16230                                           X_EXCEPTION_STAGE       => p_progress_stage,
16231                                           X_EXCEPTION_LEVEL       => p_exception_level,
16232                                           X_EXCEPTION_MSG         => l_msg,
16233                                           X_WAVE_HEADER_ID        => p_wave_id,
16234                                           X_TRIP_ID               => p_trip_id,
16235                                           X_DELIVERY_ID           => p_delivery_id,
16236                                           X_ORDER_NUMBER          => p_order_number,
16237                                           X_ORDER_LINE_ID         => p_order_line_id,
16238                                           X_STATUS                => 'Active',
16239                                           X_READY_TO_RELEASE      => null,
16240                                           X_BACKORDERED           => null,
16241                                           X_CROSSDOCK_PLANNED     => null,
16242                                           X_REPLENISHMENT_PLANNED => null,
16243                                           X_TASKED                => null,
16244                                           X_PICKED                => null,
16245                                           X_PACKED                => null,
16246                                           X_STAGED                => null,
16247                                           X_LOADED_TO_DOCK        => null,
16248                                           X_SHIPPED               => null,
16249                                           X_CONCURRENT_REQUEST_ID => fnd_global.conc_request_id,
16250                                           X_PROGRAM_ID            => fnd_global.conc_program_id, --
16251                                           X_EXCEPTION_NAME        => p_exception_name,
16252                                           X_CREATION_DATE         => sysdate,
16253                                           X_CREATED_BY            => fnd_global.user_id,
16254                                           X_LAST_UPDATE_DATE      => sysdate,
16255                                           X_LAST_UPDATED_BY       => fnd_global.user_id,
16256                                           X_LAST_UPDATE_LOGIN     => fnd_global.conc_login_id);
16257 
16258     /*  INSERT INTO wms_wp_wave_exceptions_vl
16259       (exception_id,
16260        exception_name,
16261        exception_entity,
16262        exception_stage,
16263        exception_level,
16264        exception_msg,
16265        wave_header_id,
16266        trip_id,
16267        delivery_id,
16268        order_number,
16269        order_line_id,
16270        status,
16271        concurrent_request_id,
16272        program_id,
16273        created_by,
16274        creation_date,
16275        last_update_date,
16276        last_updated_by,
16277        last_update_login)
16278     VALUES
16279       (wms_WP_WAVE_exceptions_s.nextval,
16280        p_exception_name,
16281        p_exception_entity,
16282        p_progress_stage,
16283        p_exception_level,
16284        l_msg,
16285        p_wave_id,
16286        p_trip_id,
16287        p_delivery_id,
16288        p_order_number,
16289        p_order_line_id,
16290        'Active',
16291        fnd_global.conc_request_id,
16292        fnd_global.conc_program_id,
16293        fnd_global.user_id,
16294        sysdate,
16295        sysdate,
16296        fnd_global.user_id,
16297        fnd_global.conc_login_id);*/
16298 
16299     print_debug('exception id for current exception :=' ||
16300                 l_seq_val,    -- Changed
16301                 l_debug);
16302 
16303     -- DELETE NEED NOT BE USED, AS CARE IS TAKEN IN ACTIONABLE PROCEDURE.
16304 
16305     /*
16306     DELETE FROM wms_wave_exceptions
16307      WHERE wave_header_id = p_wave_id
16308        AND ((p_exception_entity = 'Wave' AND
16309            (wave_header_id = p_wave_id AND trip_id IS NULL AND
16310            delivery_id IS NULL AND order_number IS NULL AND
16311            order_line_id IS NULL)) OR
16312            (p_exception_entity = 'Trip' AND trip_id = p_trip_id AND
16313            delivery_id IS NULL AND order_number IS NULL AND
16314            order_line_id IS NULL) OR
16315            (p_exception_entity = 'Delivery' AND
16316            delivery_id = p_delivery_id AND order_number IS NULL AND
16317            order_line_id IS NULL) OR
16318            (p_exception_entity = 'Order' AND order_number = p_order_number AND
16319            order_line_id IS NULL) OR (p_exception_entity = 'Order Line' AND
16320            order_line_id = p_order_line_id))
16321        AND concurrent_request_id != fnd_global.conc_request_id
16322        AND exception_id != (l_currval);
16323 
16324     COMMIT; */
16325 
16326   EXCEPTION
16327     WHEN others THEN
16328       print_debug('Exception in insert_purge_exceptions' || SQLCODE || ':' ||
16329                   sqlerrm,
16330                   l_debug);
16331       x_return_status := fnd_api.g_ret_sts_error;
16332   END insert_purge_exceptions;
16333 
16334   PROCEDURE check_so(p_current_order    NUMBER,
16335                      p_current_delivery NUMBER,
16336                      p_current_trip     NUMBER,
16337                      p_organization_id  NUMBER) IS
16338 
16339     l_date              DATE := sysdate;
16340     l_previous_delivery NUMBER DEFAULT NULL;
16341     l_previous_trip     NUMBER DEFAULT NULL;
16342     l_exception_id      NUMBER DEFAULT NULL;
16343     l_debug             NUMBER := nvl(fnd_profile.VALUE('INV_DEBUG_TRACE'),
16344                                       0);
16345 
16346     -- My information purpose
16347 
16348     /*In this procedure, we need not take care of purging the exceptions that were logged in
16349     previously for the same entity as this procedure is called only if the
16350     previous order in this current concurrent request is same as that of the current one
16351     and it would have taken care of purging the exceptions.*/
16352 
16353   BEGIN
16354 
16355     -- This procedure is used to avoid multiple entries into wms_wave_exception table for a single order
16356     -- If the Order has lines which are attached to different trips and deliveries then for each different
16357     -- combination of that order, delivery and trip, there is a chance of an entry. This procedure eliminates it.
16358 
16359     print_debug('In check_so procedure', l_debug);
16360     SELECT exception_id, delivery_id, trip_id
16361       INTO l_exception_id, l_previous_delivery, l_previous_trip
16362       FROM wms_wp_wave_exceptions_vl
16363      WHERE concurrent_request_id = fnd_global.conc_request_id
16364        AND order_number = p_current_order;
16365 
16366     print_debug('exception_id fetched =' || l_exception_id, l_debug);
16367 
16368     -- If deliveries and trips ares same no issues.
16369     IF (nvl(l_previous_delivery, -999) = nvl(p_current_delivery, -999) AND
16370        nvl(l_previous_trip, -999) = nvl(p_current_trip, -999)) THEN
16371       print_debug('Nothing to update or insert : Entity = Order', l_debug);
16372     ELSE
16373       -- If deliveries are same, obviously trip is same. So that condition is ruled out.
16374       -- If trips are same, deliveries may not be equal. If that is the case, null out only delivery as trip is same.
16375       -- if both trips and deliveries are different null out both delivery and trip.
16376 
16377       IF (nvl(l_previous_trip, -999) = nvl(p_current_trip, -999) AND
16378          nvl(l_previous_delivery, -999) <> nvl(p_current_delivery, -999)) THEN
16379 
16380         update wms_wp_wave_exceptions_b
16381            set delivery_id       = null,
16382                last_update_login = fnd_global.conc_login_id,
16383                last_updated_by   = fnd_global.user_id,
16384                last_update_date  = l_date
16385          where exception_id = l_exception_id;
16386 
16387         update WMS_WP_WAVE_EXCEPTIONS_TL
16388            set LAST_UPDATE_DATE  = l_date,
16389                LAST_UPDATED_BY   = fnd_global.user_id,
16390                LAST_UPDATE_LOGIN = fnd_global.conc_login_id,
16391                SOURCE_LANG       = userenv('LANG')
16392          where EXCEPTION_ID = l_exception_id
16393            and userenv('LANG') in (LANGUAGE, SOURCE_LANG);
16394 
16395         /*        UPDATE wms_wp_wave_exceptions
16396           SET delivery_id = NULL
16397         WHERE exception_id = l_exception_id;*/
16398 
16399       ELSIF (nvl(l_previous_trip, -999) <> nvl(p_current_trip, -999) AND
16400             nvl(l_previous_delivery, -999) <>
16401             nvl(p_current_delivery, -999)) THEN
16402 
16403         update wms_wp_wave_exceptions_b
16404            set trip_id           = null,
16405                delivery_id       = null,
16406                last_update_login = fnd_global.conc_login_id,
16407                last_updated_by   = fnd_global.user_id,
16408                last_update_date  = l_date
16409          where exception_id = l_exception_id;
16410 
16411         update WMS_WP_WAVE_EXCEPTIONS_TL
16412            set LAST_UPDATE_DATE  = l_date,
16413                LAST_UPDATED_BY   = fnd_global.user_id,
16414                LAST_UPDATE_LOGIN = fnd_global.conc_login_id,
16415                SOURCE_LANG       = userenv('LANG')
16416          where EXCEPTION_ID = l_exception_id
16417            and userenv('LANG') in (LANGUAGE, SOURCE_LANG);
16418         /*
16419         UPDATE wms_wp_wave_exceptions
16420            SET trip_id = NULL, delivery_id = NULL
16421          WHERE exception_id = l_exception_id;*/
16422 
16423       END IF;
16424 
16425     END IF;
16426 
16427   EXCEPTION
16428     -- My information purpose.
16429 
16430     /* Ideally no_data_found and too_many_rows exceptions should not raise.
16431     no_data_found: Atleast exception_id should be fetched in select query
16432     too_many_rows: For that particular concurrent request,if the order number
16433     comes for the second time we are calling this procedure and this will retain only
16434     one record for that.*/
16435     WHEN no_data_found THEN
16436       print_debug('No data was fetched from check_so procedure query',
16437                   l_debug);
16438     WHEN too_many_rows THEN
16439       print_debug('too many rows fetched from check_so procedure query',
16440                   l_debug);
16441     WHEN others THEN
16442       print_debug('Unknown exception raised in check_so procedure',
16443                   l_debug);
16444   END check_so;
16445 
16446   -- below procedure is similar to check_so but here it takes care of lines.
16447 
16448   PROCEDURE check_line(p_current_line     NUMBER,
16449                        p_current_delivery NUMBER,
16450                        p_current_trip     NUMBER,
16451                        p_organization_id  NUMBER) IS
16452 
16453     l_date              DATE := sysdate;
16454     l_previous_delivery NUMBER DEFAULT NULL;
16455     l_previous_trip     NUMBER DEFAULT NULL;
16456     l_exception_id      NUMBER DEFAULT NULL;
16457     l_debug             NUMBER := nvl(fnd_profile.VALUE('INV_DEBUG_TRACE'),
16458                                       0);
16459 
16460   BEGIN
16461 
16462     print_debug('In check_line procedure', l_debug);
16463     SELECT exception_id, delivery_id, trip_id
16464       INTO l_exception_id, l_previous_delivery, l_previous_trip
16465       FROM wms_wp_wave_exceptions_vl
16466      WHERE concurrent_request_id = fnd_global.conc_request_id
16467        AND ORDER_LINE_ID = p_current_line;
16468 
16469     print_debug('exception_id fetched =' || l_exception_id, l_debug);
16470 
16471     IF (nvl(l_previous_delivery, -999) = nvl(p_current_delivery, -999) AND
16472        nvl(l_previous_trip, -999) = nvl(p_current_trip, -999)) THEN
16473       print_debug('Nothing to update or insert : Entity = Line', l_debug);
16474     ELSE
16475       IF (nvl(l_previous_trip, -999) = nvl(p_current_trip, -999) AND
16476          nvl(l_previous_delivery, -999) <> nvl(p_current_delivery, -999)) THEN
16477 
16478         update wms_wp_wave_exceptions_b
16479            set delivery_id       = null,
16480                last_update_login = fnd_global.conc_login_id,
16481                last_updated_by   = fnd_global.user_id,
16482                last_update_date  = l_date
16483          where exception_id = l_exception_id;
16484 
16485         update WMS_WP_WAVE_EXCEPTIONS_TL
16486            set LAST_UPDATE_DATE  = l_date,
16487                LAST_UPDATED_BY   = fnd_global.user_id,
16488                LAST_UPDATE_LOGIN = fnd_global.conc_login_id,
16489                SOURCE_LANG       = userenv('LANG')
16490          where EXCEPTION_ID = l_exception_id
16491            and userenv('LANG') in (LANGUAGE, SOURCE_LANG);
16492 
16493       ELSIF (nvl(l_previous_trip, -999) <> nvl(p_current_trip, -999) AND
16494             nvl(l_previous_delivery, -999) <>
16495             nvl(p_current_delivery, -999)) THEN
16496 
16497         update wms_wp_wave_exceptions_b
16498            set trip_id           = null,
16499                delivery_id       = null,
16500                last_update_login = fnd_global.conc_login_id,
16501                last_updated_by   = fnd_global.user_id,
16502                last_update_date  = l_date
16503          where exception_id = l_exception_id;
16504 
16505         update WMS_WP_WAVE_EXCEPTIONS_TL
16506            set LAST_UPDATE_DATE  = l_date,
16507                LAST_UPDATED_BY   = fnd_global.user_id,
16508                LAST_UPDATE_LOGIN = fnd_global.conc_login_id,
16509                SOURCE_LANG       = userenv('LANG')
16510          where EXCEPTION_ID = l_exception_id
16511            and userenv('LANG') in (LANGUAGE, SOURCE_LANG);
16512 
16513       END IF;
16514     END IF;
16515   EXCEPTION
16516     WHEN no_data_found THEN
16517       print_debug('No data was fetched from check_line procedure query',
16518                   l_debug);
16519     WHEN too_many_rows THEN
16520       print_debug('too many rows fetched from check_line procedure query',
16521                   l_debug);
16522     WHEN others THEN
16523       print_debug('Unknown exception raised in check_line procedure',
16524                   l_debug);
16525   END check_line;
16526 
16527   PROCEDURE actionable_exceptions(p_perfect_lines                NUMBER,
16528                                   p_total_lines                  NUMBER,
16529                                   p_completion_threshold         NUMBER,
16530                                   p_low_sev_exception_threshold  NUMBER,
16531                                   p_high_sev_exception_threshold NUMBER,
16532                                   p_date_scheduled               DATE,
16533                                   p_exception_id                 NUMBER,
16534                                   p_exception_name               VARCHAR2,
16535                                   p_wave_id                      NUMBER,
16536                                   p_trip_id                      NUMBER,
16537                                   p_delivery_id                  NUMBER,
16538                                   p_order_number                 NUMBER,
16539                                   p_order_line_id                NUMBER,
16540                                   p_current_order_id             NUMBER,
16541                                   p_previous_order_id            IN OUT NOCOPY NUMBER,
16542                                   p_current_line_id              NUMBER,
16543                                   p_previous_line_id             IN OUT NOCOPY NUMBER,
16544                                   p_entity                       VARCHAR2,
16545                                   p_entity_value                 NUMBER,
16546                                   p_progress_stage               VARCHAR2,
16547                                   p_take_corrective_measures     VARCHAR2,
16548                                   p_release_back_ordered_lines   VARCHAR2,
16549                                   p_action_name                  VARCHAR2,
16550                                   p_organization_id              NUMBER) IS
16551 
16552     l_level                VARCHAR2(25) DEFAULT NULL;
16553     l_diff_min             NUMBER;
16554     l_readytorelease       VARCHAR2(25);
16555     l_backordered          VARCHAR2(25);
16556     l_crossdocked          VARCHAR2(25);
16557     l_replenishment        VARCHAR2(25);
16558     l_tasked               VARCHAR2(25);
16559     l_picked               VARCHAR2(25);
16560     l_packed               VARCHAR2(25);
16561     l_staged               VARCHAR2(25);
16562     l_loadedtodock         VARCHAR2(25);
16563     l_shipped              VARCHAR2(25);
16564     x_return_status        VARCHAR2(10) := 'S';
16565     l_completion_threshold NUMBER;
16566     l_update_exception_id  NUMBER;
16567     l_msg                  VARCHAR2(400);
16568     l_addl_msg             VARCHAR2(100);
16569 
16570     l_low_min  NUMBER := p_low_sev_exception_threshold * 60;
16571     l_high_min NUMBER := p_high_sev_exception_threshold * 60;
16572     l_debug    NUMBER := nvl(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
16573 
16574   BEGIN
16575 
16576     print_debug('In actionable_exceptions procedure', l_debug);
16577     l_diff_min := (p_date_scheduled - sysdate) * 24 * 60;
16578     l_level    := NULL;
16579 
16580     print_debug('take_corrective_measure is=' ||
16581                 p_take_corrective_measures,
16582                 l_debug);
16583 
16584     IF ((p_perfect_lines / p_total_lines) * 100 < p_completion_threshold) THEN
16585 
16586       print_debug('Actual % lines in this particular stage is less than user given completion threshold',
16587                   l_debug);
16588 
16589       print_debug('actual percentage := ' ||
16590                   (p_perfect_lines / p_total_lines),
16591                   l_debug);
16592 
16593       IF (l_diff_min <= l_low_min AND l_diff_min > l_high_min) THEN
16594         l_level := 'Low';
16595         -- raise low severity exception
16596       ELSIF (l_diff_min <= l_high_min) THEN
16597         l_level := 'High';
16598       END IF;
16599 
16600     END IF;
16601 
16602     print_debug('exception_level is =' || l_level, l_debug);
16603 
16604     IF (p_exception_id <> -1) THEN
16605 
16606       print_debug('p_exception_id <> -1 :=' || p_exception_id, l_debug);
16607 
16608       IF (l_level = 'Low' OR l_level = 'High') THEN
16609 
16610         print_debug('p_current_order_id = ' || p_current_order_id, l_debug);
16611         print_debug('p_previous_order_id = ' || p_previous_order_id,
16612                     l_debug);
16613 
16614         IF (p_entity = 'Order' AND
16615            nvl(p_current_order_id, -99) = nvl(p_previous_order_id, -999)) THEN
16616           -- If it is for the first time with entity order, p_previous_order_id will be null
16617           print_debug('call being made to check_so', l_debug);
16618           check_so(p_order_number,
16619                    p_delivery_id,
16620                    p_trip_id,
16621                    p_organization_id);
16622 
16623           -- we need not take any other action as the required action was taken for this particular order
16624           -- in previous run itself.
16625         ELSIF (p_entity = 'Order Line' AND
16626               nvl(p_current_line_id, -99) = nvl(p_previous_line_id, -999)) THEN
16627           -- if it is for the first time with entiry order line, p_previous_line_id will be null
16628           print_debug('call being made to check_line', l_debug);
16629           check_line(p_order_line_id,
16630                      p_delivery_id,
16631                      p_trip_id,
16632                      p_organization_id);
16633 
16634           -- we need not take any other action as the required action was taken for this particular order line id
16635           -- in previous run itself.
16636         ELSE
16637 
16638           IF (l_level = 'Low') THEN
16639             l_completion_threshold := p_low_sev_exception_threshold;
16640           ELSIF (l_level = 'High') THEN
16641             l_completion_threshold := p_high_sev_exception_threshold;
16642           END IF;
16643 
16644           l_msg      := l_level || ' exception for ' || p_entity;
16645           l_addl_msg := '(' || p_completion_threshold ||
16646                         ' % lines are not ' || p_progress_stage ||
16647                         ' within ';
16648           l_addl_msg := l_addl_msg || l_completion_threshold ||
16649                         ' hours of shipment )';
16650           l_msg      := l_msg || l_addl_msg;
16651 
16652           SELECT to_char(ROUND((nvl(SUM(decode(line_progress_id, 1, 1, 0)),
16653                                     0) /
16654                                decode(COUNT(1), 0, -1, COUNT(-1))) * 100,
16655                                2)) || '%',
16656                  to_char(ROUND((nvl(SUM(decode(line_progress_id, 2, 1, 0)),
16657                                     0) /
16658                                decode(COUNT(1), 0, -1, COUNT(-1))) * 100,
16659                                2)) || '%',
16660                  to_char(ROUND((nvl(SUM(decode(line_progress_id, 3, 1, 0)),
16661                                     0) /
16662                                decode(COUNT(1), 0, -1, COUNT(-1))) * 100,
16663                                2)) || '%',
16664                  to_char(ROUND((nvl(SUM(decode(line_progress_id, 4, 1, 0)),
16665                                     0) /
16666                                decode(COUNT(1), 0, -1, COUNT(-1))) * 100,
16667                                2)) || '%',
16668                  to_char(ROUND((nvl(SUM(decode(line_progress_id, 5, 1, 0)),
16669                                     0) /
16670                                decode(COUNT(1), 0, -1, COUNT(-1))) * 100,
16671                                2)) || '%',
16672                  to_char(ROUND((nvl(SUM(decode(line_progress_id, 7, 1, 0)),
16673                                     0) /
16674                                decode(COUNT(1), 0, -1, COUNT(-1))) * 100,
16675                                2)) || '%',
16676                  to_char(ROUND((nvl(SUM(decode(line_progress_id, 8, 1, 0)),
16677                                     0) /
16678                                decode(COUNT(1), 0, -1, COUNT(-1))) * 100,
16679                                2)) || '%',
16680                  to_char(ROUND((nvl(SUM(decode(line_progress_id, 9, 1, 0)),
16681                                     0) /
16682                                decode(COUNT(1), 0, -1, COUNT(-1))) * 100,
16683                                2)) || '%',
16684                  to_char(ROUND((nvl(SUM(decode(line_progress_id, 10, 1, 0)),
16685                                     0) /
16686                                decode(COUNT(1), 0, -1, COUNT(-1))) * 100,
16687                                2)) || '%',
16688                  to_char(ROUND((nvl(SUM(decode(line_progress_id, 11, 1, 0)),
16689                                     0) /
16690                                decode(COUNT(1), 0, -1, COUNT(-1))) * 100,
16691                                2)) || '%'
16692             INTO l_readytorelease,
16693                  l_backordered,
16694                  l_crossdocked,
16695                  l_replenishment,
16696                  l_tasked,
16697                  l_picked,
16698                  l_staged,
16699                  l_packed,
16700                  l_loadedtodock,
16701                  l_shipped
16702             FROM wms_wp_wwb_lines_v
16703            WHERE wave_header_id = p_wave_id
16704              AND ((p_entity = 'Wave' AND wave_header_id = p_wave_id) OR
16705                  (p_entity = 'Trip' AND trip = p_entity_value) OR
16706                  (p_entity = 'Delivery' AND delivery_id = p_entity_value) OR
16707                  (p_entity = 'Order' AND order_number = p_entity_value) OR
16708                  (p_entity = 'Order Line' AND
16709                  source_line_id = p_entity_value));
16710 
16711           UPDATE wms_wp_wave_exceptions_b
16712              SET exception_level       = l_level,
16713                  exception_msg         = l_msg,
16714                  ready_to_release      = l_readytorelease,
16715                  backordered           = l_backordered,
16716                  crossdock_planned     = l_crossdocked,
16717                  replenishment_planned = l_replenishment,
16718                  tasked                = l_tasked,
16719                  picked                = l_picked,
16720                  packed                = l_packed,
16721                  staged                = l_staged,
16722                  loaded_to_dock        = l_loadedtodock,
16723                  shipped               = l_shipped,
16724                  concurrent_request_id = fnd_global.conc_request_id,
16725                  program_id            = fnd_global.conc_program_id,
16726                  last_updated_by       = fnd_global.user_id,
16727                  last_update_login     = fnd_global.conc_login_id,
16728                  last_update_date      = SYSDATE
16729 
16730            WHERE exception_id = p_exception_id;
16731 
16732           update WMS_WP_WAVE_EXCEPTIONS_TL
16733              set LAST_UPDATE_DATE  = SYSDATE,
16734                  LAST_UPDATED_BY   = fnd_global.user_id,
16735                  LAST_UPDATE_LOGIN = fnd_global.conc_login_id,
16736                  SOURCE_LANG       = userenv('LANG')
16737            where EXCEPTION_ID = p_exception_id
16738              and userenv('LANG') in (LANGUAGE, SOURCE_LANG);
16739 
16740           IF (p_take_corrective_measures = 'Yes') THEN
16741             -- call api to take corrective measures
16742             take_corrective_measures(p_exception_id,
16743                                      p_wave_id,
16744                                      p_entity,
16745                                      p_entity_value,
16746                                      p_release_back_ordered_lines,
16747                                      p_action_name,
16748                                      p_organization_id);
16749 
16750           END IF;
16751 
16752           /*   elsif (p_take_corrective_measures = 'No') then
16753           -- If the record has status "action taken" we should not change it
16754           -- If the record is active, we need not change it
16755           -- So no action to be taken in this in this "elsif" part.*/
16756         END IF;
16757 
16758       ELSIF (l_level IS NULL) THEN
16759 
16760         -- confirm the below if condition.
16761         -- It may be like, first time when the order is encountered and exception rouse
16762         -- but some corrective measures were taken and as result exception is no more there
16763         -- by the time the order is encountered the second time.
16764         -- That time it should be closed. ?????????
16765         IF (p_entity = 'Order' AND
16766            nvl(p_current_order_id, -99) = nvl(p_previous_order_id, -999)) THEN
16767 
16768           print_debug('required action is already taken for the same order
16769       the last time when this order was evaluated',
16770                       l_debug);
16771 
16772         ELSIF (p_entity = 'Order Line' AND
16773               nvl(p_current_line_id, -99) = nvl(p_previous_line_id, -999)) THEN
16774           print_debug('required action is already taken for the same order line id
16775       the last time when this order line id was evaluated',
16776                       l_debug);
16777 
16778         ELSE
16779 
16780           UPDATE wms_wp_wave_exceptions_b
16781              SET status            = 'Closed',
16782                  last_update_date  = sysdate,
16783                  last_updated_by   = fnd_global.user_id,
16784                  last_update_login = fnd_global.login_id
16785            WHERE exception_id = p_exception_id;
16786 
16787           update WMS_WP_WAVE_EXCEPTIONS_TL
16788              set LAST_UPDATE_DATE  = SYSDATE,
16789                  LAST_UPDATED_BY   = fnd_global.user_id,
16790                  LAST_UPDATE_LOGIN = fnd_global.conc_login_id,
16791                  SOURCE_LANG       = userenv('LANG')
16792            where EXCEPTION_ID = p_exception_id
16793              and userenv('LANG') in (LANGUAGE, SOURCE_LANG);
16794 
16795         END IF;
16796 
16797       END IF;
16798 
16799       IF p_entity = 'Order' THEN
16800         p_previous_order_id := p_current_order_id;
16801       END IF;
16802 
16803       if p_entity = 'Order Line' then
16804         p_previous_line_id := p_current_line_id;
16805       end if;
16806 
16807     ELSIF (p_exception_id = -1 AND l_level IS NOT NULL) THEN
16808 
16809       print_debug('in p_exception_id = -1', l_debug);
16810 
16811       IF (l_level = 'Low') THEN
16812         l_completion_threshold := p_low_sev_exception_threshold;
16813       ELSIF (l_level = 'High') THEN
16814         l_completion_threshold := p_high_sev_exception_threshold;
16815       END IF;
16816 
16817       insert_purge_exceptions(p_exception_name,
16818                               p_entity,
16819                               l_level,
16820                               p_completion_threshold,
16821                               p_progress_stage,
16822                               l_completion_threshold,
16823                               p_wave_id,
16824                               p_trip_id,
16825                               p_delivery_id,
16826                               p_order_number,
16827                               p_order_line_id,
16828                               x_return_status);
16829 
16830       IF (x_return_status <> 'S') THEN
16831         print_debug('Error returned from insert_purge_exceptions ',
16832                     l_debug);
16833         RAISE fnd_api.g_exc_unexpected_error;
16834       END IF;
16835 
16836       IF (p_entity = 'Order') THEN
16837         p_previous_order_id := p_current_order_id;
16838       END IF;
16839 
16840       if p_entity = ('Order Line') THEN
16841         p_previous_line_id := p_current_line_id;
16842       end if;
16843 
16844       SELECT to_char(ROUND((nvl(SUM(decode(line_progress_id, 1, 1, 0)), 0) /
16845                            decode(COUNT(1), 0, -1, COUNT(-1))) * 100,
16846                            2)) || '%',
16847              to_char(ROUND((nvl(SUM(decode(line_progress_id, 2, 1, 0)), 0) /
16848                            decode(COUNT(1), 0, -1, COUNT(-1))) * 100,
16849                            2)) || '%',
16850              to_char(ROUND((nvl(SUM(decode(line_progress_id, 3, 1, 0)), 0) /
16851                            decode(COUNT(1), 0, -1, COUNT(-1))) * 100,
16852                            2)) || '%',
16853              to_char(ROUND((nvl(SUM(decode(line_progress_id, 4, 1, 0)), 0) /
16854                            decode(COUNT(1), 0, -1, COUNT(-1))) * 100,
16855                            2)) || '%',
16856              to_char(ROUND((nvl(SUM(decode(line_progress_id, 5, 1, 0)), 0) /
16857                            decode(COUNT(1), 0, -1, COUNT(-1))) * 100,
16858                            2)) || '%',
16859              to_char(ROUND((nvl(SUM(decode(line_progress_id, 7, 1, 0)), 0) /
16860                            decode(COUNT(1), 0, -1, COUNT(-1))) * 100,
16861                            2)) || '%',
16862              to_char(ROUND((nvl(SUM(decode(line_progress_id, 8, 1, 0)), 0) /
16863                            decode(COUNT(1), 0, -1, COUNT(-1))) * 100,
16864                            2)) || '%',
16865              to_char(ROUND((nvl(SUM(decode(line_progress_id, 9, 1, 0)), 0) /
16866                            decode(COUNT(1), 0, -1, COUNT(-1))) * 100,
16867                            2)) || '%',
16868              to_char(ROUND((nvl(SUM(decode(line_progress_id, 10, 1, 0)), 0) /
16869                            decode(COUNT(1), 0, -1, COUNT(-1))) * 100,
16870                            2)) || '%',
16871              to_char(ROUND((nvl(SUM(decode(line_progress_id, 11, 1, 0)), 0) /
16872                            decode(COUNT(1), 0, -1, COUNT(-1))) * 100,
16873                            2)) || '%'
16874         INTO l_readytorelease,
16875              l_backordered,
16876              l_crossdocked,
16877              l_replenishment,
16878              l_tasked,
16879              l_picked,
16880              l_staged,
16881              l_packed,
16882              l_loadedtodock,
16883              l_shipped
16884         FROM wms_wp_wwb_lines_v
16885        WHERE wave_header_id = p_wave_id
16886          AND ((p_entity = 'Wave' AND wave_header_id = p_wave_id) OR
16887              (p_entity = 'Trip' AND trip = p_entity_value) OR
16888              (p_entity = 'Delivery' AND delivery_id = p_entity_value) OR
16889              (p_entity = 'Order' AND order_number = p_entity_value) OR
16890              (p_entity = 'Order Line' AND source_line_id = p_entity_value));
16891 
16892             SELECT wms_WP_WAVE_exceptions_s.currval INTO l_seq_val FROM dual;      -- Changed
16893       l_update_exception_id := l_seq_val;              -- Changed
16894 
16895       UPDATE wms_wp_wave_exceptions_b
16896          SET ready_to_release      = l_readytorelease,
16897              backordered           = l_backordered,
16898              crossdock_planned     = l_crossdocked,
16899              replenishment_planned = l_replenishment,
16900              tasked                = l_tasked,
16901              picked                = l_picked,
16902              packed                = l_packed,
16903              staged                = l_staged,
16904              loaded_to_dock        = l_loadedtodock,
16905              shipped               = l_shipped,
16906              concurrent_request_id = fnd_global.conc_request_id,
16907              program_id            = fnd_global.conc_program_id,
16908              last_update_date      = sysdate,
16909              last_updated_by       = fnd_global.user_id,
16910              last_update_login     = fnd_global.conc_login_id
16911        WHERE exception_id = l_update_exception_id;
16912 
16913       update WMS_WP_WAVE_EXCEPTIONS_TL
16914          set LAST_UPDATE_DATE  = SYSDATE,
16915              LAST_UPDATED_BY   = fnd_global.user_id,
16916              LAST_UPDATE_LOGIN = fnd_global.conc_login_id,
16917              SOURCE_LANG       = userenv('LANG')
16918        where EXCEPTION_ID = l_update_exception_id
16919          and userenv('LANG') in (LANGUAGE, SOURCE_LANG);
16920 
16921       IF (p_take_corrective_measures = 'Yes') THEN
16922         -- call api to take corrective measures
16923         SELECT wms_WP_WAVE_exceptions_s.CURRVAL INTO l_seq_val FROM dual;  -- Changed
16924         take_corrective_measures(l_seq_val,                                          -- Changed
16925                                  p_wave_id,
16926                                  p_entity,
16927                                  p_entity_value,
16928                                  p_release_back_ordered_lines,
16929                                  p_action_name,
16930                                  p_organization_id);
16931 
16932         /*   elsif (p_take_corrective_measures = 'No') then
16933         -- If the record has status "action taken" we should not change it
16934         -- If the record is active, we need not change it
16935         -- So no action to be taken in this in this "elsif" part.*/
16936       END IF;
16937 
16938     END IF;
16939 
16940   END actionable_exceptions;
16941 
16942   PROCEDURE apply_saved_action( --p_action_name  IN    VARCHAR2,
16943                                p_wave_id       NUMBER,
16944                                p_entity        varchar2,
16945                                p_entity_value  NUMBER,
16946                                p_action_name   VARCHAR2,
16947                                x_return_status OUT nocopy VARCHAR2) IS
16948 
16949     l_transaction_temp_id_table WMS_WAVE_PLANNING_PVT.transaction_temp_table_type;
16950     l_task_type_id_table        WMS_WAVE_PLANNING_PVT.task_type_id_table_type;
16951     row_in                      NUMBER := 0;
16952     l_select_query              VARCHAR2(4000);
16953     l_from_query                VARCHAR2(4000);
16954     l_where_query               VARCHAR2(4000);
16955     l_final_query               VARCHAR2(4000) := NULL;
16956     type cur_typ IS ref CURSOR;
16957     c               cur_typ;
16958     l_task_id_table wms_wave_planning_pvt.task_id_table_type;
16959     --x_return_status VARCHAR2(1);
16960 
16961     l_debug          NUMBER := nvl(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
16962     l_updated_record NUMBER := 0;
16963 
16964     l_msg_data      VARCHAR2(120);
16965     l_msg_count     NUMBER;
16966     l_save_count    NUMBER;
16967     l_return_status VARCHAR2(120);
16968     l_result_table  wms_wave_planning_pvt.result_table_type;
16969     l_message_table wms_wave_planning_pvt.message_table_type;
16970 
16971     CURSOR c_saved_actions(p_action_name varchar2) IS
16972       select field_name, field_value, query_type
16973         from wms_saved_queries
16974        where query_name = p_action_name
16975          and (query_type = 'TASK_ACTION' or query_type = 'TEMP_TASK_ACTION')
16976          FOR UPDATE NOWAIT;
16977 
16978     CURSOR c_query_type(p_action_name varchar2) IS
16979       select distinct query_type
16980         from wms_saved_queries
16981        where query_name = p_action_name;
16982 
16983     rec_saved_actions c_saved_actions%rowtype;
16984 
16985     l_field_name_table      wms_wave_planning_pvt.field_name_table_type;
16986     l_field_value_table     wms_wave_planning_pvt.field_value_table_type;
16987     l_organization_id_table wms_wave_planning_pvt.organization_id_table_type;
16988     l_query_type_table      wms_wave_planning_pvt.query_type_table_type;
16989 
16990   BEGIN
16991 
16992     -- Clearing the tables.
16993     l_field_name_table.delete;
16994     l_field_value_table.delete;
16995     l_query_type_table.delete;
16996 
16997     print_DEBUG('Cleared pl/sql tables l_query_type_table, l_field_name_table and l_field_value_table.',
16998                 l_debug);
16999 
17000     print_DEBUG('Opening c_saved_actions cursor', l_debug);
17001 
17002     OPEN c_saved_actions(p_action_name);
17003     FETCH c_saved_actions BULK COLLECT
17004       INTO l_field_name_table, l_field_value_table, l_query_type_table;
17005 
17006     -- If no records founds for the given query name
17007     -- then close the cursor and return informing invalid query name.
17008 
17009     print_DEBUG('c_saved_actions%ROWCOUNT = ' || c_saved_actions%ROWCOUNT,
17010                 l_debug);
17011 
17012     --x_rowcount := c_saved_actions%ROWCOUNT;
17013 
17014     IF c_saved_actions%ROWCOUNT = 0 THEN
17015       CLOSE c_saved_actions;
17016 
17017       print_DEBUG('No data found for action name. ' || p_action_name,
17018                   l_debug);
17019       --x_rowcount  := 0;
17020       x_return_status := fnd_api.g_ret_sts_success;
17021       --x_return_message:= 'No data found for action name. ' || p_action_name;
17022 
17023       FOR rec_query_type IN c_query_type(p_action_name) LOOP
17024         IF rec_query_type.query_type = 'TEMP_TASK_ACTION' THEN
17025           l_temp_action := TRUE;
17026         ELSE
17027           l_temp_action := FALSE;
17028         END IF;
17029       END LOOP;
17030 
17031       RETURN;
17032     END IF;
17033 
17034     CLOSE c_saved_actions;
17035 
17036     print_DEBUG('Bulk collect successful and closed c_saved_actions cursor',
17037                 l_debug);
17038 
17039     print_DEBUG('Calling SET_ACTION_TASKS_PARAMETERS', l_debug);
17040 
17041     SET_ACTION_TASKS_PARAMETERS(p_field_name_table  => l_field_name_table,
17042                                 p_field_value_table => l_field_value_table,
17043                                 p_query_type_table  => l_query_type_table,
17044                                 x_return_status     => l_return_status,
17045                                 x_return_message    => l_msg_data);
17046 
17047     print_DEBUG('SET_ACTION_TASKS_PARAMETERS return status = ' ||
17048                 l_return_status,
17049                 l_debug);
17050     print_DEBUG('SET_ACTION_TASKS_PARAMETERS return message = ' ||
17051                 l_msg_data,
17052                 l_debug);
17053 
17054     -- If set_action_tasks_parameters returns error then log message and return.
17055     IF l_return_status = fnd_api.g_ret_sts_error OR
17056        l_return_status = fnd_api.g_ret_sts_unexp_error THEN
17057       print_DEBUG(' Error in SET_ACTION_TASKS_PARAMETERS ', l_debug);
17058       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
17059       return;
17060     END IF;
17061 
17062     -------------------------------------------------------------------------------------------------------------
17063 --11775489 get from wdd... FIXED changed to move_order_line_id similar to other bug
17064     l_select_query := 'select mmtt.TRANSACTION_TEMP_ID, mmtt.wms_task_type';
17065     l_from_query   := ' from wms_wp_wave_lines wwl,wsh_delivery_details wdd,
17066                                    mtl_material_transactions_temp mmtt';
17067     l_where_query  := ' where wwl.wave_header_id = :wave_id
17068                                   and wdd.delivery_detail_id = wwl.delivery_detail_id
17069 								  and wdd.organization_id = wwl.organization_id
17070                                   and wdd.move_order_line_id  = mmtt.move_order_line_id';
17071     --     Change the status from unreleased to pending for  those mmtts  that belong to wdds
17072     --     that belong to this particular entity value and mmtts are in 'Unreleased status
17073 
17074     IF (p_entity = 'Wave') THEN
17075       print_debug('Nothing to Do here' || l_final_query, l_debug);
17076     ELSIF (p_entity = 'Trip') THEN
17077       l_from_query  := l_from_query ||
17078                        ' ,wsh_delivery_assignments wda, wsh_new_deliveries wnd,
17079                                         wsh_delivery_legs wdl, wsh_trip_stops wts';
17080       l_where_query := l_where_query ||
17081                        ' and wdd.delivery_detail_id = wda.delivery_detail_id
17082                                           and wda.delivery_id = wnd.delivery_id
17083             and wnd.delivery_id = wdl.delivery_id
17084             and wdl.pick_up_stop_id = wts.stop_id
17085             and wts.trip_id = :entity_value ';
17086     ELSIF (p_entity = 'Delivery') THEN
17087       l_from_query  := l_from_query || ' , wsh_delivery_assignments wda';
17088       l_where_query := l_where_query ||
17089                        ' and wdd.delivery_detail_id = wda.delivery_detail_id
17090                                            and wda.delivery_id = :entity_value ';
17091     ELSIF (p_entity = 'Order') THEN
17092       l_where_query := l_where_query ||
17093                        ' and wdd.source_header_id = :entity_value';
17094 
17095       print_debug('take measures, pending tasks, task priority of order',
17096                   l_debug);
17097     ELSIF (p_entity = 'Order Line') THEN
17098       l_where_query := l_where_query ||
17099                        ' and wdd.source_line_id = :entity_value ';
17100     END IF;
17101 
17102     l_final_query := l_select_query || l_from_query || l_where_query;
17103     print_debug('final query:  ' || l_final_query, l_debug);
17104 
17105     l_transaction_temp_id_table.DELETE;
17106     l_task_type_id_table.DELETE;
17107 
17108     IF (p_entity = 'Wave') THEN
17109 
17110       OPEN c FOR l_final_query
17111         USING p_wave_id;
17112       LOOP
17113         FETCH c
17114           INTO l_transaction_temp_id_table(row_in), l_task_type_id_table(row_in);
17115         row_in := row_in + 1;
17116         EXIT WHEN c % NOTFOUND;
17117       END LOOP;
17118 
17119       CLOSE c;
17120       row_in := 0;
17121     ELSE
17122 
17123       OPEN c FOR l_final_query
17124         USING p_wave_id, p_entity_value;
17125       LOOP
17126         FETCH c
17127           INTO l_transaction_temp_id_table(row_in), l_task_type_id_table(row_in);
17128         row_in := row_in + 1;
17129         EXIT WHEN c % NOTFOUND;
17130       END LOOP;
17131 
17132       CLOSE c;
17133       row_in := 0;
17134     END IF;
17135 
17136     print_debug('XXX 1', l_debug);
17137 
17138     IF (l_transaction_temp_id_table.count = 0 or
17139        l_task_type_id_table.count = 0) THEN
17140       RETURN;
17141     END IF;
17142 
17143     --populate wms_wp_tasks_gtmp using view wms_wp_wwb_tasks_v and transaction_temp_id in l_transaction_temp_id
17144     FORALL ttemp_id IN l_transaction_temp_id_table.FIRST .. l_transaction_temp_id_table.LAST
17145       insert into wms_wp_tasks_gtmp
17146         select task_id,
17147                to_lpn,
17148                to_lpn_id,
17149                transaction_temp_id,
17150                transaction_type_id,
17151                transaction_action_id,
17152                transaction_source_type_id,
17153                transaction_source_id,
17154                transaction_source_line_id,
17155                organization_id,
17156                organization_code,
17157                to_organization_id,
17158                to_organization_code,
17159                to_subinventory,
17160                to_locator_id,
17161                to_locator,
17162                user_task_type_id,
17163                user_task_type,
17164                person_id,
17165                person_id_original,
17166                person,
17167                effective_start_date,
17168                effective_end_date,
17169                person_resource_id,
17170                person_resource_code,
17171                status_id,
17172                status_id_original,
17173                status,
17174                mmtt_last_update_date,
17175                mmtt_last_updated_by,
17176                wdt_last_update_date,
17177                wdt_last_updated_by,
17178                priority,
17179                priority_original,
17180                task_type_id,
17181                task_type,
17182                is_modified,
17183                plan_task,
17184                result,
17185                error,
17186                source_header,
17187                line_number,
17188                item,
17189                item_description,
17190                revision,
17191                subinventory,
17192                locator,
17193                secondary_quantity,
17194                secondary_uom,
17195                operation_plan_id,
17196                operation_plan,
17197                operation_sequence,
17198                operation_plan_instance,
17199                ship_method,
17200                shipment_date,
17201                shipment_priority,
17202                department,
17203                child_tasks,
17204                num_of_child_tasks,
17205                allocated_lpn,
17206                cartonized_lpn,
17207                container_item,
17208                content_lpn,
17209                dispatched_time,
17210                equipment,
17211                from_lpn,
17212                parent_line_id,
17213                move_order_line_id,
17214                pick_slip_number,
17215                picked_lpn,
17216                transaction_uom,
17217                transaction_quantity
17218           from wms_wp_wwb_tasks_v
17219          where wave_header_id = p_wave_id
17220            AND transaction_temp_id = l_transaction_temp_id_table(ttemp_id);
17221 
17222     /* Bug 5485730 - The employee details should be null if the status is being updated
17223     to Pending or Unreleased */
17224     IF l_action_type = 'U' then
17225 
17226       IF l_status_code IN (1, 8) THEN
17227         l_employee             := NULL;
17228         l_employee_id          := NULL;
17229         l_user_task_type       := NULL;
17230         l_user_task_type_id    := NULL;
17231         l_effective_start_date := NULL;
17232         l_effective_end_date   := NULL;
17233         l_person_resource_id   := NULL;
17234         l_person_resource_code := NULL;
17235       END IF;
17236       /* End of Bug 5485730 */
17237       --call update task
17238       print_DEBUG('Calling wms_wave_planning_pvt.UPDATE_TASK', l_debug);
17239       print_DEBUG('Following are the input parameters', l_debug);
17240       print_DEBUG('p_employee    => ' || l_employee, l_debug);
17241       print_DEBUG('p_employee_id    => ' || l_employee_id, l_debug);
17242       print_DEBUG('p_user_task_type  => ' || l_user_task_type, l_debug);
17243       print_DEBUG('p_user_task_type_id  => ' || l_user_task_type_id,
17244                   l_debug);
17245       print_DEBUG('p_effective_start_date  => ' || l_effective_start_date,
17246                   l_debug);
17247       print_DEBUG('p_effective_end_date  => ' || l_effective_end_date,
17248                   l_debug);
17249       print_DEBUG('p_person_resource_id  => ' || l_person_resource_id,
17250                   l_debug);
17251       print_DEBUG('p_person_resource_code  => ' || l_person_resource_code,
17252                   l_debug);
17253       --print_DEBUG( 'p_person_resource_code  => ' || l_override_emp_check, l_debug);
17254       print_DEBUG('p_to_status    => ' || l_status, l_debug);
17255       print_DEBUG('p_to_status_id    => ' || l_status_code, l_debug);
17256       print_DEBUG('p_update_priority_type  => ' || l_priority_type,
17257                   l_debug);
17258       print_DEBUG('p_update_priority  => ' || l_priority, l_debug);
17259       print_DEBUG('p_clear_priority  => ' || l_clear_priority, l_debug);
17260 
17261       IF l_override_emp_check = TRUE THEN
17262         print_DEBUG('p_force_employee_change  => TRUE', l_debug);
17263       ELSE
17264         print_DEBUG('p_force_employee_change  => FALSE', l_debug);
17265       END IF;
17266 
17267       update_task(p_transaction_temp_id   => l_transaction_temp_id_table,
17268                   p_task_type_id          => l_task_type_id_table,
17269                   p_employee              => l_employee,
17270                   p_employee_id           => l_employee_id,
17271                   p_user_task_type        => l_user_task_type,
17272                   p_user_task_type_id     => l_user_task_type_id,
17273                   p_effective_start_date  => l_effective_start_date,
17274                   p_effective_end_date    => l_effective_end_date,
17275                   p_person_resource_id    => l_person_resource_id,
17276                   p_person_resource_code  => l_person_resource_code,
17277                   p_force_employee_change => l_override_emp_check,
17278                   p_to_status             => l_status,
17279                   p_to_status_id          => l_status_code,
17280                   p_priority_type         => l_priority_type,
17281                   p_priority              => l_priority,
17282                   p_clear_priority        => l_clear_priority,
17283                   x_result                => l_result_table,
17284                   x_message               => l_message_table,
17285                   x_task_id               => l_task_id_table,
17286                   x_return_status         => l_return_status,
17287                   x_return_msg            => l_msg_data,
17288                   x_msg_count             => l_msg_count);
17289 
17290       print_DEBUG('update_task l_return_status ' || l_return_status,
17291                   l_debug);
17292 
17293       IF l_return_status = fnd_api.g_ret_sts_error OR
17294          l_return_status = fnd_api.g_ret_sts_unexp_error THEN
17295         print_DEBUG('Error in update_task ', l_debug);
17296         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
17297         return;
17298       END IF;
17299 
17300       print_DEBUG('Records Updated by update_task ' ||
17301                   l_result_table.count,
17302                   l_debug);
17303       l_updated_record := l_result_table.Count;
17304 
17305     ELSIF l_action_type = 'C' THEN
17306       print_DEBUG('Cancel Task is not applicable ', l_debug);
17307       l_updated_record := 0;
17308     ELSE
17309       print_DEBUG('Invalid Action ', l_debug);
17310       l_updated_record := 0;
17311     END IF;
17312 
17313     IF l_updated_record > 0 then
17314       print_DEBUG('Calling wms_waveplan_tasks_pvt.save_tasks ', l_debug);
17315       print_DEBUG('Input Parameters passed', l_debug);
17316       print_DEBUG('p_commit  => TRUE', l_debug);
17317       print_DEBUG('p_user_id  => ' || fnd_global.user_id, l_debug);
17318       print_DEBUG('p_login_id  => ' || fnd_global.login_id, l_debug);
17319 
17320       save_tasks(p_commit        => TRUE,
17321                  p_user_id       => fnd_global.user_id,
17322                  p_login_id      => fnd_global.login_id,
17323                  x_save_count    => l_save_count,
17324                  x_return_status => l_return_status,
17325                  x_msg_data      => l_msg_data,
17326                  x_msg_count     => l_msg_count);
17327 
17328       print_DEBUG('SAVE_TASKS return Status = ' || l_return_status,
17329                   l_debug);
17330       print_DEBUG('SAVE_TASKS l_save_count = ' || l_save_count, l_debug);
17331       print_DEBUG('SAVE_TASKS l_msg_data = ' || l_msg_data, l_debug);
17332       print_DEBUG('SAVE_TASKS l_msg_count = ' || l_msg_count, l_debug);
17333 
17334       IF l_return_status = fnd_api.g_ret_sts_error OR
17335          l_return_status = fnd_api.g_ret_sts_unexp_error THEN
17336         print_DEBUG('Error in save_task ', l_debug);
17337         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
17338         return;
17339       END IF;
17340     END IF;
17341 
17342     l_transaction_temp_id_table.DELETE;
17343     l_task_type_id_table.DELETE;
17344 
17345     print_debug('Exiting apply_saved_action ', l_debug);
17346 
17347   EXCEPTION
17348     WHEN others THEN
17349       print_debug('exception in apply_saved_action ', l_debug);
17350   END apply_saved_action;
17351 
17352   PROCEDURE take_corrective_measures(p_exception_id               NUMBER,
17353                                      p_wave_id                    NUMBER,
17354                                      p_entity                     VARCHAR2,
17355                                      p_entity_value               NUMBER,
17356                                      p_release_back_ordered_lines VARCHAR2,
17357                                      p_action_name                VARCHAR2,
17358                                      p_organization_id            NUMBER) IS
17359 
17360     type wdd_table IS TABLE OF NUMBER INDEX BY binary_integer;
17361     l_wdd_table    wdd_table;
17362     row_in         NUMBER := 0;
17363     l_select_query VARCHAR2(4000);
17364     l_from_query   VARCHAR2(4000);
17365     l_where_query  VARCHAR2(4000);
17366     l_where_rpt    VARCHAR2(4000);
17367     l_where_cptp   VARCHAR2(4000);
17368     type cur_typ IS ref CURSOR;
17369     c               cur_typ;
17370     l_final_query   VARCHAR2(4000) := NULL;
17371     l_debug         NUMBER := nvl(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
17372     x_msg_count     VARCHAR2(100);
17373     x_msg_data      VARCHAR2(100);
17374     p_batch_id      NUMBER;
17375     p_request_id    NUMBER;
17376     x_return_status VARCHAR2(1);
17377     l_phase         VARCHAR2(100);
17378     l_status        VARCHAR2(100);
17379     l_dev_phase     VARCHAR2(100);
17380     l_dev_status    VARCHAR2(100);
17381     l_message       VARCHAR2(500);
17382     l_result        boolean;
17383 
17384     l_entity_value number;
17385     --p_action_name VARCHAR2(100) := 'p';
17386 
17387   BEGIN
17388 
17389     print_debug('in take_corrective_measures API', l_debug);
17390 
17391     if (p_entity = 'Order') then
17392       begin
17393         select distinct (source_header_id)
17394           into l_entity_value
17395           from wsh_delivery_details
17396          where source_header_number = p_entity_value
17397            and organization_id = p_organization_id;
17398 
17399       exception
17400         when no_data_found then
17401           print_debug('no data found exception in take corrective measures',
17402                       l_debug);
17403         when others then
17404           print_debug('other exception in take corrective measures',
17405                       l_debug);
17406       end;
17407     else
17408 
17409       l_entity_value := p_entity_value;
17410     end if;
17411 --11775489 added org_id join...
17412     IF (p_release_back_ordered_lines = 'Yes') THEN
17413       l_select_query := 'select wwl.delivery_detail_id';
17414       l_from_query   := ' from wms_wp_wave_lines wwl,wsh_delivery_details wdd';
17415       l_where_query  := ' where wwl.wave_header_id = :wave_id
17416                                   and wdd.delivery_detail_id = wwl.delivery_detail_id
17417 								  and wdd.organizatin_id = wwl.organization_id
17418                                   and wdd.released_status = ''B''
17419                                   and wdd.replenishment_status is null';
17420       --not in (''R'',''C'')';
17421       -- Select only backordered wdds (EXCLUDE 'RR" and 'RC' status ) for this particular entity value
17422 
17423       IF (p_entity = 'Wave') THEN
17424         print_debug('No more changes in any string', l_debug);
17425       ELSIF (p_entity = 'Trip') THEN
17426         l_from_query  := l_from_query ||
17427                          ' , wsh_delivery_assignments wda, wsh_new_deliveries wnd,
17428                       wsh_delivery_legs wdl,wsh_trip_stops wts';
17429         l_where_query := l_where_query ||
17430                          ' and wda.delivery_detail_id = wdd.delivery_detail_id
17431                        and wda.delivery_id = wnd.delivery_id(+)
17432                        and wnd.delivery_id = wdl.delivery_id(+)
17433                        and wdl.pick_up_stop_id = wts.stop_id(+)
17434                        and wts.trip_id = :entity_value';
17435       ELSIF (p_entity = 'Delivery') THEN
17436         l_from_query  := l_from_query || ' ,wsh_delivery_assignments wda';
17437         l_where_query := l_where_query ||
17438                          ' and wda.delivery_detail_id = wdd.delivery_detail_id
17439                        and wda.delivery_id = :entity_value';
17440       ELSIF (p_entity = 'Order') THEN
17441         l_where_query := l_where_query ||
17442                          ' and wdd.source_header_id = :entity_value'; --11775489 get information from wdd
17443       ELSIF (p_entity = 'Order Line') THEN
17444         l_where_query := l_where_query ||
17445                          ' and wdd.source_line_id = :entity_value'; --11775489 get information from wdd
17446       END IF;
17447 
17448       l_final_query := l_select_query || l_from_query || l_where_query;
17449 
17450       print_debug('Query for ' || p_entity || ':' || l_final_query,
17451                   l_debug);
17452       print_debug('The Wave Header Id is ' || p_wave_id, l_debug);
17453 
17454       IF (p_entity = 'Wave') THEN
17455 
17456         OPEN c FOR l_final_query
17457           USING p_wave_id;
17458         LOOP
17459           FETCH c
17460             INTO l_wdd_table(row_in);
17461           row_in := row_in + 1;
17462           EXIT WHEN c % NOTFOUND;
17463         END LOOP;
17464 
17465         CLOSE c;
17466         row_in := 0;
17467       ELSE
17468 
17469         OPEN c FOR l_final_query
17470           USING p_wave_id, l_entity_value;
17471         LOOP
17472           FETCH c
17473             INTO l_wdd_table(row_in);
17474           row_in := row_in + 1;
17475           EXIT WHEN c % NOTFOUND;
17476         END LOOP;
17477 
17478         CLOSE c;
17479         row_in := 0;
17480       END IF;
17481 
17482       print_debug('Setting the Re Release flag to Y ', l_debug);
17483 
17484       forall i IN l_wdd_table.FIRST .. l_wdd_table.LAST
17485 
17486         UPDATE wms_wp_wave_lines
17487            SET re_release_flag = 'Y'
17488          WHERE delivery_detail_id = l_wdd_table(i);
17489       COMMIT;
17490 
17491       -- Call backorder API with the pls table as input.
17492       wms_wave_planning_pvt.create_batch_record(x_return_status, p_wave_id);
17493 
17494       IF x_return_status = 'S' THEN
17495 
17496         wsh_picking_batches_grp.release_wms_wave(p_release_mode        => 'CONCURRENT',
17497                                                  p_pick_wave_header_id => p_wave_id,
17498                                                  x_request_id          => p_request_id, -- wat rqst id
17499                                                  x_return_status       => x_return_status,
17500                                                  x_msg_count           => x_msg_count,
17501                                                  x_msg_data            => x_msg_data,
17502                                                  p_batch_rec           => new_wave_type, -- wat wave type
17503                                                  x_batch_id            => p_batch_id);
17504         -- wat batch id
17505 
17506 		print_debug('Value of p batch id is' || p_batch_id, l_debug);
17507 
17508         l_result := fnd_concurrent.wait_for_request(request_id => p_request_id,
17509                                                     phase      => l_phase,
17510                                                     status     => l_status,
17511                                                     dev_phase  => l_dev_phase,
17512                                                     dev_status => l_dev_status,
17513                                                     message    => l_message);
17514 
17515         l_result := fnd_concurrent.set_completion_status(status  => l_status,
17516                                                          message => '');
17517 
17518         IF x_return_status = 'S' THEN
17519 
17520           get_actual_fill_rate(x_return_status, p_wave_id);
17521 
17522           print_debug('Status after Call to Get the Actual Fill Rate' ||
17523                       x_return_status,
17524                       l_debug);
17525 
17526         END IF;
17527 
17528       END IF;
17529 
17530       -- After calling the api, we need to delete the records in the plsql table
17531       -- NEED TO UNCOMMENT THE ABOVE PART, WHEN INCLUDED IN AJITH'S MAIN API
17532       l_wdd_table.DELETE;
17533     END IF;
17534 
17535 
17536 
17537     IF p_action_name IS NOT NULL THEN
17538 
17539       apply_saved_action(p_wave_id       => p_wave_id,
17540                          p_entity        => p_entity,
17541                          p_entity_value  => p_entity_value,
17542                          p_action_name   => p_action_name,
17543                          x_return_status => x_return_status);
17544 
17545       IF x_return_status = fnd_api.g_ret_sts_error OR
17546          x_return_status = fnd_api.g_ret_sts_unexp_error THEN
17547         print_DEBUG('Error in apply_saved_action ', l_debug);
17548         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
17549         return;
17550       END IF;
17551 
17552     END IF;
17553 
17554     IF (p_release_back_ordered_lines = 'Yes' OR p_action_name IS NOT NULL) THEN
17555 
17556       UPDATE wms_wp_wave_exceptions_b
17557          SET status = 'Action Taken'
17558        WHERE exception_id = p_exception_id;
17559 
17560       update WMS_WP_WAVE_EXCEPTIONS_TL
17561          set LAST_UPDATE_DATE  = SYSDATE,
17562              LAST_UPDATED_BY   = fnd_global.user_id,
17563              LAST_UPDATE_LOGIN = fnd_global.conc_login_id,
17564              SOURCE_LANG       = userenv('LANG')
17565        where EXCEPTION_ID = p_exception_id
17566          and userenv('LANG') in (LANGUAGE, SOURCE_LANG);
17567 
17568     END IF;
17569 
17570     print_debug('p_exception_id = ' || p_exception_id, l_debug);
17571 
17572     print_debug('after update of exceptions table', l_debug);
17573 
17574     -- Temporaryily added
17575 
17576   END;
17577 
17578       -- cancel task changes start
17579 
17580 PROCEDURE cancel_mmtt(p_txn_temp_id NUMBER, x_result OUT nocopy VARCHAR2) IS
17581 
17582     x_return_status   VARCHAR2(100);
17583     x_msg_data        VARCHAR2(100);
17584     x_msg_count       NUMBER;
17585     l_txn_temp_id     NUMBER;
17586     l_mol_id          NUMBER;
17587     l_moh_id          NUMBER;
17588     l_res_id          NUMBER;
17589     l_txn_qty         NUMBER;
17590     l_prim_qty        NUMBER;
17591     l_sec_qty         NUMBER;
17592     l_org_id          NUMBER;
17593     l_from_sub        VARCHAR2(10);
17594     l_from_loc        NUMBER;
17595     l_no_mmtt         NUMBER;
17596     l_mo_type         NUMBER;
17597     l_sec_uom         VARCHAR2(3);
17598     l_qty             NUMBER;
17599     l_qty_del         NUMBER;
17600     l_qty_det         NUMBER;
17601     l_sec_mol_qty     NUMBER;
17602     l_sec_qty_del     NUMBER;
17603     l_sec_qty_det     NUMBER;
17604     l_qty_backorder   NUMBER;
17605 
17606     l_shipping_attr wsh_interface.changedattributetabtype;
17607 
17608   BEGIN
17609 
17610     l_txn_temp_id := p_txn_temp_id;
17611 
17612       SELECT mtrl.move_order_line_id,
17613              mtrl.move_order_header_id,
17614              mtrl.reservation_id,
17615              mtrl.transaction_quantity,
17616              mtrl.primary_quantity,
17617              mtrl.secondary_transaction_quantity,
17618              mtrl.organization_id,
17619              mtrh.move_order_type
17620         INTO l_mol_id,
17621              l_moh_id,
17622              l_res_id,
17623              l_txn_qty,
17624              l_prim_qty,
17625              l_sec_qty,
17626              l_org_id,
17627 	     l_mo_type
17628         FROM mtl_material_transactions_temp mtrl,
17629          mtl_txn_request_headers mtrh
17630        WHERE mtrl.transaction_temp_id = l_txn_temp_id
17631         AND mtrh.header_id = mtrl.move_order_header_id;
17632 
17633       -- move order type :   2:Replenishment    3:Pick Wave    6:Crossdocking
17634 
17635       IF l_mo_type = 6 THEN
17636         -- cross dock task
17637 
17638         BEGIN
17639 
17640           wms_cross_dock_pvt.cancel_crossdock_task(p_transaction_temp_id => l_txn_temp_id,
17641                                                    x_return_status       => x_return_status,
17642                                                    x_msg_data            => x_msg_data,
17643                                                    x_msg_count           => x_msg_count);
17644 
17645           IF x_return_status = 'S' THEN
17646             x_result := 'S';
17647           ELSE
17648 	    x_result := 'E';
17649           END IF;
17650 
17651         EXCEPTION
17652 	 WHEN OTHERS THEN
17653 	 x_result := 'E';
17654 
17655         END;
17656       ELSIF l_mo_type = 3 THEN
17657         -- pick release task
17658        BEGIN
17659         SELECT COUNT(*)
17660           INTO l_no_mmtt
17661           FROM mtl_material_transactions_temp
17662          WHERE move_order_line_id = l_mol_id;
17663 
17664 
17665         SELECT delivery_detail_id,
17666                oe_header_id,
17667                oe_line_id,
17668                released_status
17669           INTO l_shipping_attr(1).delivery_detail_id,
17670                l_shipping_attr(1).source_header_id,
17671                l_shipping_attr(1).source_line_id,
17672                l_shipping_attr(1).released_status
17673           FROM wsh_inv_delivery_details_v
17674          WHERE move_order_line_id = l_mol_id
17675            AND move_order_line_id IS NOT NULL
17676            AND released_status = 'S';
17677 
17678         SELECT from_subinventory_code,
17679                from_locator_id,
17680                secondary_uom_code,
17681                quantity,
17682                quantity_delivered,
17683                quantity_detailed,
17684                secondary_quantity,
17685                secondary_quantity_delivered,
17686                secondary_quantity_detailed
17687           INTO l_from_sub,
17688                l_from_loc,
17689                l_sec_uom,
17690                l_qty,
17691                l_qty_del,
17692                l_qty_det,
17693                l_sec_mol_qty,
17694                l_sec_qty_del,
17695                l_sec_qty_det
17696           FROM mtl_txn_request_lines
17697          WHERE line_id = l_mol_id;
17698 
17699 
17700         /* The below call deletes mtlt, msnt and mmtt. Takes care of everything*/
17701 
17702         inv_mo_backorder_pvt.delete_details(x_return_status        => x_return_status,
17703                                             x_msg_data             => x_msg_data,
17704                                             x_msg_count            => x_msg_count,
17705                                             p_transaction_temp_id  => l_txn_temp_id,
17706                                             p_move_order_line_id   => l_mol_id,
17707                                             p_reservation_id       => l_res_id,
17708                                             p_transaction_quantity => l_txn_qty,
17709                                             p_primary_trx_qty      => l_prim_qty,
17710                                             p_secondary_trx_qty    => l_sec_qty);
17711 
17712         IF l_no_mmtt = 1 THEN
17713           -- only 1 mmtt is there so close the mol
17714 
17715           IF l_sec_uom IS NULL THEN
17716 
17717             UPDATE mtl_txn_request_lines
17718                SET line_status       = 5,
17719                    quantity_detailed = (quantity_detailed - l_txn_qty),
17720                    quantity          = (quantity - l_txn_qty)
17721              WHERE line_id = l_mol_id;
17722           ELSE
17723 
17724             UPDATE mtl_txn_request_lines
17725                SET line_status                 = 5,
17726                    quantity_detailed           = (quantity_detailed -
17727                                                  l_txn_qty),
17728                    secondary_quantity_detailed = (secondary_quantity_detailed -
17729                                                  l_sec_qty),
17730                    quantity                    = (quantity - l_txn_qty),
17731                    secondary_quantity          = (secondary_quantity -
17732                                                  l_sec_qty),
17733                    status_date                 = sysdate
17734              WHERE line_id = l_mol_id;
17735           END IF;
17736 
17737         ELSE
17738           -- more than 1 mmtt is there so should not close mol
17739 
17740           IF l_sec_uom IS NULL THEN
17741 
17742             UPDATE mtl_txn_request_lines
17743                SET quantity_detailed = (quantity_detailed - l_txn_qty),
17744                    quantity          = (quantity - l_txn_qty)
17745              WHERE line_id = l_mol_id;
17746           ELSE
17747 
17748             UPDATE mtl_txn_request_lines
17749                SET quantity_detailed           = (quantity_detailed -
17750                                                  l_txn_qty),
17751                    secondary_quantity_detailed = (secondary_quantity_detailed -
17752                                                  l_sec_qty),
17753                    quantity                    = (quantity - l_txn_qty),
17754                    secondary_quantity          = (secondary_quantity -
17755                                                  l_sec_qty),
17756                    status_date                 = sysdate
17757              WHERE line_id = l_mol_id;
17758           END IF;
17759 
17760         END IF;
17761 
17762         -- To deal with case of over picking
17763 
17764         /* Conditions checked here are:
17765               If more qty is already picked then obviously current transaction quantiy
17766         will be more than qty to backorder which is calculated*/ -- Check with Satish or Ajith
17767 
17768         l_qty_backorder := l_qty - l_qty_del;
17769 
17770         IF l_txn_qty > l_qty_backorder THEN
17771           l_txn_qty := l_qty_backorder;
17772           l_sec_qty := l_sec_qty - l_sec_qty_del;
17773         END IF;
17774 
17775         l_shipping_attr(1).ship_from_org_id := l_org_id;
17776         l_shipping_attr(1).action_flag := 'B';
17777         l_shipping_attr(1).cycle_count_quantity := l_txn_qty;
17778         l_shipping_attr(1).cycle_count_quantity2 := l_sec_qty;
17779         l_shipping_attr(1).subinventory := l_from_sub;
17780         l_shipping_attr(1).locator_id := l_from_loc;
17781 
17782         /* The below call splits the wdd or backorders the line accordingly*/
17783 
17784         wsh_interface.update_shipping_attributes(p_source_code        => 'INV',
17785                                                  p_changed_attributes => l_shipping_attr,
17786                                                  x_return_status      => x_return_status);
17787 
17788         IF x_return_status = 'S' THEN
17789           x_result := 'S';
17790         ELSE
17791 	  x_result := 'E';
17792         END IF;
17793 
17794 	EXCEPTION
17795 	WHEN OTHERS THEN
17796 	x_result := 'E';
17797 
17798 	END;
17799 
17800 
17801       ELSIF l_mo_type = 2 THEN
17802         -- replenishment task
17803         -- Replenishment task is not eligible for cancelling
17804         -- print a message that the task is ineligible for cancellation
17805         -- pass the value of txn temp id to the calling procedure
17806 	     x_result := 'R';
17807       END IF;
17808 
17809   EXCEPTION
17810 
17811   WHEN OTHERS THEN
17812   -- print some debug message and return fail
17813   x_result := 'E';
17814 
17815   END cancel_mmtt;
17816 
17817 PROCEDURE update_wave_lines_tbl (p_mo_line_table		IN  INV_Move_Order_PUB.TROLIN_TBL_TYPE
17818 								,x_return_status        OUT	NOCOPY VARCHAR2) IS
17819 
17820 								l_debug NUMBER := 		NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
17821 								TYPE num_tab IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
17822 								j number := 0; -- 14781148
17823 								l_mo_line_table 		num_tab; --bug 10256506 change db issue where forall cannot reference specific columns of pl/sql table
17824 
17825 
17826 BEGIN
17827 	print_debug('Inside the call to update_wave_lines_tbl at place 1',l_debug);
17828 
17829 	--bug 10256506 copy p_mo_line_table(i).txn_source_line_detail_id to local l_mo_line_table, then do bulk delete using l_mo_line_table
17830 	if p_mo_line_table.count > 0 then  -- 14781148
17831 		for i in p_mo_line_table.first .. p_mo_line_table.last loop
17832 		 if p_mo_line_table.exists(i) then -- 14781148
17833 		   l_mo_line_table(j) := p_mo_line_table(i).txn_source_line_detail_id;
17834 		   j := j+ 1; -- 14781148
17835 		 end if; -- 14781148
17836 		end loop;
17837 	end if; -- 14781148
17838 
17839 	IF l_mo_line_table.COUNT > 0 THEN
17840 	FORALL i IN l_mo_line_table.FIRST .. l_mo_line_table.LAST
17841 		DELETE FROM wms_wp_wave_lines wwwl
17842 		WHERE wwwl.delivery_detail_id = l_mo_line_table(i)
17843 		AND exists (select 1 from wms_wp_wave_headers_b wwwh where wwwh.wave_firmed_flag = 'N' and wwwl.wave_header_id = wwwh.wave_header_id)
17844 		AND nvl(wwwl.remove_from_wave_flag , 'N')='N';
17845 
17846 
17847 	print_debug('Inside the call to update_wave_lines_tbl at place 2',l_debug);
17848 	END IF;
17849 
17850 	print_debug('Before returning from update_wave_lines_tbl at place 3',l_debug);
17851 	x_return_status := fnd_api.g_ret_sts_success;
17852 
17853 EXCEPTION -- 14781148
17854   WHEN OTHERS THEN
17855     print_debug('Error in update_wave_lines_tbl',l_debug);
17856     print_debug('SQLCODE == '||SQLCODE,l_debug);
17857     print_debug('SQLERRM == '||SQLERRM,l_debug);
17858     x_return_status := fnd_api.G_RET_STS_ERROR;
17859 END update_wave_lines_tbl;
17860 
17861   -- cancel task changes end
17862 
17863 
17864 FUNCTION list_mmtt_release
17865   return num_t pipelined
17866   is
17867   l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
17868   begin
17869 	for i in g_mmtt_table.first..g_mmtt_table.last loop
17870 	  if g_mmtt_table.exists(i) then
17871 	    pipe row(g_mmtt_table(i));
17872 	  end if;
17873 	end loop
17874 	return;
17875   exception
17876     when others then
17877 	  print_debug('Unknown exception occurred in list_mmtt_release: '||SQLERRM,l_debug);
17878   end;
17879 
17880    PROCEDURE insert_global_temp(p_wave_header_id IN number)
17881  IS
17882  TYPE lookup_meaning_table IS TABLE OF mfg_lookups.meaning%TYPE INDEX BY BINARY_INTEGER;
17883  type lookup_meaning_codes IS TABLE OF mfg_lookups.lookup_code%TYPE INDEX BY BINARY_INTEGER;
17884  l_plan_task_types lookup_meaning_table; --12693583
17885  l_plan_task_types_temp lookup_meaning_table; --12693583
17886  l_plan_task_type_codes lookup_meaning_codes; --12693583
17887 
17888  BEGIN
17889 
17890      --12693583...store table for task types string translation
17891 	  select lookup_code,  REPLACE (meaning, '''', '''''')  bulk collect into l_plan_task_type_codes, l_plan_task_types_temp
17892 	  from mfg_lookups
17893 	  where lookup_type = 'WMS_PLAN_TASK_TYPES'
17894 	  order by lookup_code;
17895 
17896 	 for i in l_plan_task_type_codes.first .. l_plan_task_type_codes.last loop
17897 		l_plan_task_types(l_plan_task_type_codes(i)):=l_plan_task_types_temp(i);
17898 	 end loop;
17899 
17900 
17901 
17902     delete wms_wp_orders_gtmp;
17903 	delete wms_wp_lines_gtmp;
17904 	delete from wms_wp_exceptions_gtmp;
17905 	delete from wms_wp_tasks_gtmp;
17906 	delete from wms_wp_labor_gtmp;
17907 
17908 	--Orders GTMP
17909 	--bug12371804 query from base tables instead of from wms_wp_wwb_orders_v for performance
17910 	--inner most select finds all lines associated with wave; next outer select groups by order number; outermost select adds in order-level information
17911 	insert into wms_wp_orders_gtmp (order_number,
17912      customer,
17913      planned_fill_rate,
17914      release_fill_rate,
17915      pick_fill_rate,
17916      order_type,
17917      customer_id,
17918      cust_po_number,
17919      ordered_date,
17920      order_request_date,
17921      order_progress)
17922 	select /*+ use_nl(CUST_ACCT) index(CUST_ACCT HZ_CUST_ACCOUNTS_U1) */ ooh.order_number,
17923 	HP.PARTY_NAME CUSTOMER,
17924 	wl.planned_fill_rate,
17925 	wl.release_fill_rate,
17926 	wl.pick_fill_rate,
17927 	ott.name,
17928 	ooh.sold_to_org_id,
17929 	ooh.CUST_PO_NUMBER,
17930 	ooh.ordered_date,
17931 	ooh.request_date order_request_date,
17932 	wl.order_progress
17933 	from oe_order_headers_all ooh,
17934 	HZ_PARTIES HP,
17935 	hz_cust_accounts CUST_ACCT,
17936 	oe_transaction_types_tl ott,
17937 	(select wwlv.source_header_id,
17938 	sum(wwlv.requested_quantity*wwlv.planned_fill_rate)/decode(sum(wwlv.requested_quantity),0,1,sum(wwlv.requested_quantity)) planned_fill_rate,
17939 	sum(wwlv.requested_quantity*wwlv.release_fill_rate)/decode(sum(wwlv.requested_quantity),0,1,sum(wwlv.requested_quantity)) release_fill_rate,
17940 	sum(wwlv.requested_quantity*wwlv.pick_fill_rate)/decode(sum(wwlv.pick_fill_rate),0,1,sum(wwlv.pick_fill_rate)) pick_fill_rate,
17941 	decode(count(DISTINCT wwlv.line_progress_id),1,'','Partially ')||Decode(max(wwlv.line_progress_id),1,'Ready to Release',2,'Backordered',3,'Crossdock Planned',4,'Replenishment Created',5,'Tasked',6,'Picked',
17942 	7,'Picked',9,'In Packing',8,'Staged',10,'Loaded to Dock',11,'Shipped') ORDER_PROGRESS
17943 	from (SELECT wwl.wave_line_id,
17944 	 wdd.requested_quantity, --11775489 select from wdd instead...
17945 	wdd.source_header_id source_header_id,
17946 	Nvl(WWl.planned_FILL_RATE, 0) planned_fill_rate,
17947 	Nvl(wwl.release_fill_rate,0) release_fill_rate,
17948 	Nvl(wwl.pick_fill_rate,0) pick_fill_rate,
17949 	Decode(wdd.released_status,
17950 	'S',Decode(wms_Wave_planning_pvt.get_loaded_status(wdd.delivery_Detail_id),1,6,2,7,Decode(Nvl(wdd.move_order_line_id,-99),-99,3,5)),
17951 	'C',11,
17952 	'B',Decode(wdd.replenishment_status,'R',4,2),
17953 	'R',1,
17954 	'K',3,
17955 	'Y',Decode(wlpn.lpn_context,11,Decode(mil.inventory_location_type,2,8,4,9,5,9),9,10)) LINE_PROGRESS_ID,
17956 	wwl.wave_header_id
17957 	FROM
17958 	WSH_DELIVERY_DETAILS WDD,
17959 	WMS_WP_WAVE_LINES WWL,
17960 	WMS_LICENSE_PLATE_NUMBERS WLPN,
17961 	WSH_DELIVERY_ASSIGNMENTS WDA,
17962 	mtl_item_locations_kfv mil,
17963 	wsh_Delivery_details wdd2
17964 	 WHERE WDA.DELIVERY_DETAIL_ID = WDD.DELIVERY_DETAIL_ID
17965 	AND wda.parent_delivery_detail_id = wdd2.delivery_detail_id(+)
17966 	AND wdd.locator_id = mil.inventory_location_id(+)
17967 	and WWL.DELIVERY_DETAIL_ID = WDD.DELIVERY_DETAIL_ID
17968 	and wwl.organization_id = wdd.organization_id --11775489 get from wdd... FIXED
17969 	AND wlpn.lpn_id(+) = wdd2.lpn_id
17970 	AND wwl.wave_header_id = p_wave_header_id) wwlv
17971 	group by wwlv.source_header_id) wl
17972 	where wl.source_header_id = ooh.header_id
17973 	and HP.party_id = cust_acct.party_id
17974 	and cust_acct.status = 'A'
17975 	and cust_acct.cust_account_id= ooh.SOLD_TO_ORG_ID
17976 	and ott.transaction_type_id = ooh.order_type_id
17977 	and ott.LANGUAGE = userenv('LANG');
17978 
17979 
17980 	--Lines GTMP
17981 	--12371804 query from base tables instead of wms_wp_wwb_lines_v
17982 	insert into wms_wp_lines_gtmp
17983 	(DELIVER_TO,
17984 	DELIVERY,
17985 	EXCEPTION_ID,
17986 	ITEM,
17987 	LINE_STATUS,
17988 	LINE_PROGRESS,
17989 	ORDER_NUMBER,
17990 	SOURCE_LINE_NUMBER,
17991 	REQUESTED_QTY,
17992 	PLANNED_FILL_RATE,
17993 	RELEASE_FILL_RATE,
17994 	PICK_FILL_RATE,
17995 	SHIP_TO,
17996 	SHIPPED_QTY,
17997 	DATE_SCHEDULED,
17998 	BATCH,
17999 	CARRIER,
18000 	CONTACT,
18001 	CUSTOMER_MODEL_SERIAL_NUMBER,
18002 	DATE_REQUESTED,
18003 	DELIVER_TO_CONTACT,
18004 	DELIVER_TO_CUSTOMER,
18005 	DELIVERED_QTY,
18006 	EARLIEST_DELIVERY_DATE,
18007 	EARLIEST_SHIP_DATE,
18008 	FOB,
18009 	FREIGHT_TERMS,
18010 	GRADE,
18011 	GROSS_WEIGHT,
18012 	HAZARD_CLASS,
18013 	IGNORE_FOR_PLANNING,
18014 	INSPECTION_STATUS,
18015 	INTERMEDIATE_SHIP_TO,
18016 	INTERMEDIATE_SHIP_TO_CONTACT,
18017 	ITEM_DESCRIPTION,
18018 	LATEST_DELIVERY_DATE,
18019 	LOADING_SEQUENCE,
18020 	LOCATOR,
18021 	LOT,
18022 	MAX_LOAD_WEIGHT,
18023 	MAX_VOLUME,
18024 	MIN_FILL_PERCENT,
18025 	MODE_OF_TRANSPORT,
18026 	NET_WEIGHT,
18027 	ORDER_TYPE,
18028 	ORG_NAME,
18029 	PACKING_INSTRUCTIONS,
18030 	REQUESTED_QTY_UOM,
18031 	REVISION,
18032 	SEAL_CODE,
18033 	SECONDARY_DELIVERED_QTY,
18034 	SECONDARY_REQUESTED_QTY,
18035 	SECONDARY_REQUESTED_QTY_UOM,
18036 	SECONDARY_SHIPPED_QTY,
18037 	SECONDARY_SRC_REQ_QTY,
18038 	SECONDARY_SRC_REQ_QTY_UOM,
18039 	SERVICE_LEVEL,
18040 	SHIP_METHOD,
18041 	SHIP_MODEL_COMPLETE,
18042 	SHIP_SET,
18043 	SHIP_TO_CONTACT,
18044 	SHIP_TO_CUSTOMER,
18045 	SHIPMENT_PRIORITY,
18046 	SHIPPING_INSTRUCTIONS,
18047 	SOURCE_REQUESTED_QTY,
18048 	SOURCE_REQUESTED_QTY_UOM,
18049 	TARE_WEIGHT,
18050 	TASK_NUMBER,
18051 	TRACKING_NUMBER,
18052 	UNIT_NUMBER,
18053 	VOLUME,
18054 	VOLUME_UOM,
18055 	WEIGHT_UOM,
18056 	CUSTOMER,
18057 	PROJECT,
18058 	CUSTOMER_CLASS,
18059 	TRIP,
18060 	REMOVE_FROM_WAVE_FLAG,
18061 	WAVE_FIRMED_FLAG,
18062 	MESSAGE)
18063 	select /*+ use_nl(MSIV) */
18064 	(select ui_location_code from wsh_locations where wsh_location_id=OOL.DELIVER_TO_ORG_ID) DELIVER_TO,
18065 	WND.NAME DELIVERY,
18066 	null,
18067 	MSIV.concatenated_segments ITEM, --12642410
18068 	(select meaning from FND_LOOKUP_VALUES WHERE lookup_type = 'PICK_STATUS' AND LANGUAGE = userenv('LANG') and lookup_code = wdd.released_status) line_status, --12693583
18069 	Decode(wdd.released_status,
18070 	'S',Decode(wms_Wave_planning_pvt.get_loaded_status(wdd.delivery_Detail_id),1,'Partially Picked',2,'Picked',Decode(Nvl(wdd.move_order_line_id,-99),-99,'Crossdock Planned','Tasked')),
18071 	'C','Shipped',
18072 	'B',Decode(wdd.replenishment_status,'R','Replenishment Created','Backordered'),
18073 	'R','Ready to Release',
18074 	'Y',Decode(wlpn.lpn_context,11,Decode(mil.inventory_location_type,2,'Staged',4,'In Packing',5,'In Packing'),9,'Loaded to Dock')) LINE_PROGRESS,
18075 	WDD.SOURCE_HEADER_NUMBER ORDER_NUMBER,
18076 	WDD.SOURCE_LINE_NUMBER,
18077 	WDD.REQUESTED_QUANTITY REQUESTED_QTY,
18078 	WWl.planned_FILL_RATE,
18079 	wwl.release_fill_rate,
18080 	wwl.pick_fill_rate,
18081 	(select ui_location_code from wsh_locations where wsh_location_id=WDD.SHIP_TO_LOCATION_ID) SHIP_TO,
18082 	WDD.SHIPPED_QUANTITY SHIPPED_QTY,
18083 	WDD.DATE_SCHEDULED,
18084 	WDD.BATCH_ID BATCH,
18085 	WC.FREIGHT_CODE CARRIER,
18086 	(select ui_location_code from wsh_locations where wsh_location_id=OOL.SHIP_TO_CONTACT_ID) Line_Ship_To_Contact,
18087 	OOL.CUST_MODEL_SERIAL_NUMBER,
18088 	OOL.REQUEST_DATE Line_request_date,
18089 	(select ui_location_code from wsh_locations where wsh_location_id=OOL.DELIVER_TO_CONTACT_ID) DELIVER_TO_CONTACT,
18090 	  (select ui_location_code from wsh_locations where
18091 	   wsh_location_id=WDD.DELIVER_TO_LOCATION_ID) DELIVER_TO_LOCATION,
18092 	WDD.DELIVERED_QUANTITY,
18093 	WDD.EARLIEST_DROPOFF_DATE,
18094 	to_date(OOL.EARLIEST_SHIP_DATE),
18095 	WDD.FOB_CODE,
18096 	WDD.FREIGHT_TERMS_CODE,
18097 	WDD.PREFERRED_GRADE,
18098 	WDD.GROSS_WEIGHT,
18099 	WDD.HAZARD_CLASS_ID,
18100 	WDD.IGNORE_FOR_PLANNING,
18101 	WDD.INSPECTION_FLAG,
18102 	(select ui_location_code from wsh_locations where wsh_location_id=WDD.INTMED_SHIP_TO_LOCATION_ID),
18103 	(select ui_location_code from wsh_locations where wsh_location_id=WDD.INTMED_SHIP_TO_CONTACT_ID),
18104 	WDD.ITEM_DESCRIPTION,
18105 	WDD.LATEST_DROPOFF_DATE,
18106 	WDD.LOAD_SEQ_NUMBER,
18107 	mil.concatenated_segments,
18108 	WDD.LOT_NUMBER,
18109 	WDD.MAXIMUM_LOAD_WEIGHT,
18110 	WDD.MAXIMUM_VOLUME,
18111 	WDD.MINIMUM_FILL_PERCENT,
18112 	WDD.MODE_OF_TRANSPORT,
18113 	WDD.NET_WEIGHT,
18114 	WDD.SOURCE_HEADER_TYPE_NAME "ORDER TYPE",
18115 	MP.ORGANIZATION_CODE,
18116 	WDD.PACKING_INSTRUCTIONS,
18117 	WDD.REQUESTED_QUANTITY_UOM,
18118 	WDD.REVISION,
18119 	WDD.SEAL_CODE,
18120 	WDD.DELIVERED_QUANTITY2,
18121 	WDD.REQUESTED_QUANTITY2,
18122 	WDD.REQUESTED_QUANTITY_UOM2,
18123 	WDD.SHIPPED_QUANTITY2,
18124 	WDD.SRC_REQUESTED_QUANTITY2,
18125 	WDD.SRC_REQUESTED_QUANTITY_UOM2,
18126 	WDD.SERVICE_LEVEL,
18127 	WDD.SHIP_METHOD_CODE SHIP_METHOD,
18128 	WDD.SHIP_MODEL_COMPLETE_FLAG,
18129 	WDD.SHIP_SET_ID,
18130 	(select ui_location_code from wsh_locations where wsh_location_id=WDD.SHIP_TO_CONTACT_ID),
18131 	(SELECT p.party_name FROM hz_party_sites ps, hz_parties p WHERE p.party_id = ps.party_id AND ps.location_id = wdd.ship_to_location_id AND party_type = 'ORGANIZATION' and rownum=1) SHIP_TO_CUSTOMER,
18132 	WDD.SHIPMENT_PRIORITY_CODE SHIPMENT_PRIORITY,
18133 	WDD.SHIPPING_INSTRUCTIONS,
18134 	WDD.SRC_REQUESTED_QUANTITY,
18135 	WDD.SRC_REQUESTED_QUANTITY_UOM,
18136 	WLPN.TARE_WEIGHT,
18137 	WDD.TASK_ID TASK_NUMBER,
18138 	WDD.TRACKING_NUMBER,
18139 	WDD.UNIT_NUMBER,
18140 	WDD.VOLUME,
18141 	WDD.VOLUME_UOM_CODE,
18142 	WDD.WEIGHT_UOM_CODE,
18143 	HP.PARTY_NAME CUSTOMER,
18144 	WDD.PROJECT_ID PROJECT,
18145 	cust_Acct.customer_class_code CUSTOMER_CLASS,
18146 	WTS.TRIP_ID TRIP,
18147 	wwl.remove_from_wave_flag,
18148 	wwh.wave_firmed_flag,
18149 	wwl.message
18150 	from
18151 	WSH_DELIVERY_DETAILS WDD,
18152 	HZ_PARTIES HP,
18153 	hz_cust_accounts CUST_ACCT,
18154 	OE_ORDER_LINES_ALL OOL,
18155 	wms_wp_wave_headers_b WWH,
18156 	WMS_WP_WAVE_LINES WWL,
18157 	WSH_NEW_DELIVERIES WND,
18158 	MTL_SYSTEM_ITEMS_KFV MSIV,
18159 	WSH_CARRIERS WC,
18160 	MTL_PARAMETERS MP,
18161 	WMS_LICENSE_PLATE_NUMBERS WLPN,
18162 	WSH_DELIVERY_ASSIGNMENTS WDA,
18163 	WSH_TRIP_STOPS WTS,
18164 	WSH_DELIVERY_LEGS WLG,
18165 	mtl_item_locations_kfv mil,
18166 	wsh_Delivery_details wdd2
18167 	 WHERE
18168 	HP.party_id = cust_acct.party_id
18169 	and cust_acct.status = 'A'
18170 	and cust_acct.cust_account_id= wdd.customer_id
18171 	AND WDD.SOURCE_LINE_ID = OOL.LINE_ID
18172 	AND WDD.ORGANIZATION_ID = MP.ORGANIZATION_ID
18173 	AND WDD.INVENTORY_ITEM_ID = MSIV.INVENTORY_ITEM_ID
18174 	AND WND.DELIVERY_ID(+) = WDA.DELIVERY_ID
18175 	AND WDA.DELIVERY_DETAIL_ID = WDD.DELIVERY_DETAIL_ID
18176 	AND WDD.CARRIER_ID = WC.CARRIER_ID(+)
18177 	AND wda.parent_delivery_detail_id = wdd2.delivery_detail_id(+)
18178 	AND wdd.locator_id = mil.inventory_location_id(+)
18179 	AND WWH.WAVE_HEADER_ID = WWL.WAVE_HEADER_ID
18180 	and WWL.DELIVERY_DETAIL_ID = WDD.DELIVERY_DETAIL_ID
18181 	and wwl.organization_id = wdd.organization_id --11775489 get from wdd... FIXED
18182 	AND   WTS.STOP_ID(+) = WLG.PICK_UP_STOP_ID
18183 	AND   WLG.DELIVERY_ID(+) = WND.DELIVERY_ID
18184 	AND wlpn.lpn_id(+) = wdd2.lpn_id
18185 	AND WDD.ORGANIZATION_ID = MSIV.ORGANIZATION_ID --12371804
18186 	AND wwl.wave_header_id = p_wave_header_id;
18187 
18188 
18189 
18190 
18191 	--12371804 query from base tables instead of from wms_wp_wwb_tasks_v
18192 	insert into wms_wp_tasks_gtmp(
18193 	TASK_ID,
18194 	TO_LPN,
18195 	TO_LPN_ID,
18196 	TRANSACTION_TEMP_ID,
18197 	TRANSACTION_TYPE_ID,
18198 	TRANSACTION_ACTION_ID,
18199 	TRANSACTION_SOURCE_TYPE_ID,
18200 	TRANSACTION_SOURCE_ID,
18201 	TRANSACTION_SOURCE_LINE_ID,
18202 	ORGANIZATION_ID,
18203 	ORGANIZATION_CODE,
18204 	TO_ORGANIZATION_ID,
18205 	TO_ORGANIZATION_CODE,
18206 	TO_SUBINVENTORY,
18207 	TO_LOCATOR_ID,
18208 	TO_LOCATOR,
18209 	USER_TASK_TYPE_ID,
18210 	USER_TASK_TYPE,
18211 	PERSON_ID,
18212 	PERSON_ID_ORIGINAL,
18213 	PERSON,
18214 	EFFECTIVE_START_DATE,
18215 	EFFECTIVE_END_DATE,
18216 	PERSON_RESOURCE_ID,
18217 	PERSON_RESOURCE_CODE,
18218 	STATUS_ID,
18219 	STATUS_ID_ORIGINAL,
18220 	STATUS,
18221 	MMTT_LAST_UPDATE_DATE,
18222 	MMTT_LAST_UPDATED_BY,
18223 	WDT_LAST_UPDATE_DATE,
18224 	WDT_LAST_UPDATED_BY,
18225 	PRIORITY,
18226 	PRIORITY_ORIGINAL,
18227 	TASK_TYPE_ID,
18228 	TASK_TYPE,
18229 	IS_MODIFIED,
18230 	PLAN_TASK,
18231 	RESULT,
18232 	ERROR,
18233 	SOURCE_HEADER,
18234 	LINE_NUMBER,
18235 	ITEM,
18236 	ITEM_DESCRIPTION,
18237 	REVISION,
18238 	SUBINVENTORY,
18239 	LOCATOR,
18240 	SECONDARY_QUANTITY,
18241 	SECONDARY_UOM,
18242 	OPERATION_PLAN_ID,
18243 	OPERATION_PLAN,
18244 	OPERATION_SEQUENCE,
18245 	OPERATION_PLAN_INSTANCE,
18246 	SHIP_METHOD,
18247 	SHIPMENT_DATE,
18248 	SHIPMENT_PRIORITY,
18249 	DEPARTMENT,
18250 	CHILD_TASKS,
18251 	NUM_OF_CHILD_TASKS,
18252 	ALLOCATED_LPN,
18253 	CARTONIZED_LPN,
18254 	CONTAINER_ITEM,
18255 	CONTENT_LPN,
18256 	DISPATCHED_TIME,
18257 	EQUIPMENT,
18258 	FROM_LPN,
18259 	PARENT_LINE_ID,
18260 	MOVE_ORDER_LINE_ID,
18261 	PICK_SLIP_NUMBER,
18262 	PICKED_LPN,
18263 	TRANSACTION_UOM,
18264 	TRANSACTION_QUANTITY)
18265 SELECT /*+ use_nl(WWL,WDD,mmtt) index(mmtt) */ DISTINCT WDT.TASK_ID,
18266 					(SELECT LICENSE_PLATE_NUMBER
18267 					   FROM WMS_LICENSE_PLATE_NUMBERS
18268 					  WHERE LPN_ID = MMTT.TRANSFER_LPN_ID) TO_LPN,
18269 					MMTT.TRANSFER_LPN_ID TO_LPN_ID,
18270 					mmtt.transaction_temp_id,
18271 					mmtt.transaction_type_id,
18272 					mmtt.transaction_action_id,
18273 					mmtt.transaction_source_type_id,
18274 					mmtt.transaction_source_id,
18275 					mmtt.trx_source_line_id,
18276 					mmtt.organization_id,
18277 					(SELECT organization_code
18278 					   FROM mtl_parameters
18279 					  WHERE organization_id = mmtt.organization_id) organization_code,
18280 					mmtt.transfer_organization to_organization_id,
18281 					(SELECT organization_code
18282 					   FROM mtl_parameters
18283 					  WHERE organization_id = mmtt.transfer_organization) to_organization_code,
18284 					mmtt.transfer_subinventory to_subinventory,
18285 					mmtt.transfer_to_location to_locator_id,
18286 				(SELECT  mil.concatenated_segments --bug12642410
18287                    FROM MTL_ITEM_LOCATIONS_kfv mil
18288                   WHERE mmtt.transfer_to_location = mil.inventory_location_id
18289                     AND mmtt.organization_id = mil.organization_id) to_locator,
18290 					mmtt.standard_operation_id user_task_type_id,
18291 					bso.operation_code,
18292 					wdt.person_id person_id,
18293 					wdt.person_id person_id_original,
18294 					pap.full_name person,
18295 					wdt.effective_start_date,
18296 					wdt.effective_end_date,
18297 					wdt.person_resource_id,
18298 					br1.resource_code,
18299 					decode(wdt.status,
18300 						   null,
18301 						   nvl(mmtt.wms_task_status, 1),
18302 						   wdt.status) status_id,
18303 					decode(wdt.status,
18304 						   null,
18305 						   nvl(mmtt.wms_task_status, 1),
18306 						   wdt.status) status_id_original,
18307 	(select meaning from mfg_lookups where lookup_type = 'WMS_TASK_STATUS' and lookup_code = decode(wdt.status,null, nvl(mmtt.wms_task_status,1), wdt.status)) STATUS, --12693583
18308 					mmtt.last_update_date mmtt_last_update_date,
18309 					mmtt.last_updated_by mmtt_last_update_by,
18310 					wdt.last_update_date wdt_last_update_date,
18311 					wdt.last_updated_by wdt_last_update_by,
18312 					mmtt.task_priority,
18313 					mmtt.task_priority priority_original,
18314 					mmtt.wms_task_type task_type_id,
18315 					 (select meaning from mfg_lookups where lookup_type = 'WMS_TASK_TYPES' and lookup_code =mmtt.wms_task_type) task_type, --12693583
18316 					'N' is_modified,
18317 					decode(mmtt.parent_line_id,
18318 						   null,
18319 						   decode(mmtt.operation_plan_id,
18320 								  null,
18321 								  l_plan_task_types(1),
18322 								  decode(mmtt.transaction_action_id,
18323 										 28,
18324 										 l_plan_task_types(1),
18325 										 decode(mmtt.transaction_source_type_id,
18326 												5,
18327 												l_plan_task_types(1),
18328 												13,
18329 												decode(mmtt.transaction_type_id,
18330 													   51,
18331 													   l_plan_task_types(1),
18332 													   l_plan_task_types(3))))),
18333 						   mmtt.transaction_temp_id,
18334 						   l_plan_task_types(4),
18335 						   l_plan_task_types(2)) plan_task, --12693583
18336 					' ' result,
18337 					' ' error,
18338 					WDD.SOURCE_HEADER_NUMBER source_header,
18339 					WDD.SOURCE_LINE_NUMBER line_number,
18340 					MSIV.concatenated_segments item, --bug12642410
18341 					MSIV.DESCRIPTION item_description,
18342 					MMTT.REVISION,
18343 					MMTT.SUBINVENTORY_CODE subinventory,
18344 					(mil.concatenated_segments) locator,
18345 				MMTT.SECONDARY_TRANSACTION_QUANTITY,
18346 					MMTT.SECONDARY_UOM_CODE,
18347 					MMTT.OPERATION_PLAN_ID,
18348 					WOPV.OPERATION_PLAN_NAME,
18349 					WOOI.OPERATION_SEQUENCE,
18350 					WOOI.OP_PLAN_INSTANCE_ID,
18351 					WDD.SHIP_METHOD_CODE SHIP_METHOD,
18352 					WDD.DATE_SCHEDULED shipment_date,
18353 					WDD.SHIPMENT_PRIORITY_CODE shipment_priority,
18354 					MMTT.DEPARTMENT_CODE,
18355 					'N' CHILD_TASKS,
18356 					null num_of_child_tasks,
18357 					(SELECT LICENSE_PLATE_NUMBER
18358 					   FROM WMS_LICENSE_PLATE_NUMBERS
18359 					  WHERE LPN_ID = MMTT.ALLOCATED_LPN_ID) ALLOCATED_LPN,
18360 					(SELECT LICENSE_PLATE_NUMBER
18361 					   FROM WMS_LICENSE_PLATE_NUMBERS
18362 					  WHERE LPN_ID = MMTT.CARTONIZATION_ID) CARTONIZED_LPN,
18363 				   (SELECT concatenated_segments FROM mtl_system_items_kfv WHERE inventory_item_id= MMTT.CONTAINER_ITEM_ID AND organization_id=mmtt.organization_id),
18364 					(SELECT LICENSE_PLATE_NUMBER
18365 					   FROM WMS_LICENSE_PLATE_NUMBERS
18366 					  WHERE LPN_ID = MMTT.CONTENT_LPN_ID) CONTENT_LPN,
18367 					WDT.DISPATCHED_TIME,
18368 					WDT.EQUIPMENT_ID,
18369 					(SELECT LICENSE_PLATE_NUMBER
18370 					   FROM WMS_LICENSE_PLATE_NUMBERS
18371 					  WHERE LPN_ID = MMTT.LPN_ID) FROM_LPN,
18372 					MMTT.PARENT_LINE_ID,
18373 					mmtt.move_order_line_id,
18374 					MMTT.PICK_SLIP_NUMBER,
18375 					(SELECT LICENSE_PLATE_NUMBER
18376 					   FROM WMS_LICENSE_PLATE_NUMBERS
18377 					  WHERE LPN_ID = MMTT.CONTENT_LPN_ID) PICKED_LPN,
18378 					mmtt.transaction_uom,
18379 					mmtt.transaction_quantity
18380 	  FROM mtl_material_transactions_temp mmtt,
18381 		   WSH_DELIVERY_DETAILS           WDD,
18382 		   MTL_ITEM_LOCATIONS_kfv             mil, --bug12642410
18383 		   mtl_system_items_kfv           msiv,
18384 		   wms_op_plans_vl                wopv,
18385 		   per_all_people_f               pap,
18386 		   wms_op_operation_instances     wooi,
18387 		   WMS_WP_WAVE_LINES              WWL,
18388 		   wms_dispatched_tasks           wdt,
18389 		   bom_standard_operations        bso,
18390 		   bom_resources                  br1
18391 	 where mmtt.transaction_temp_id = wdt.transaction_temp_id(+)
18392 	   AND wdt.person_id = pap.person_id(+)
18393 	   AND wdt.effective_start_date >= pap.effective_start_date(+)
18394 	   AND wdt.effective_end_date <= pap.effective_end_date(+)
18395 	   AND mmtt.organization_id = msiv.organization_id
18396 	   AND mmtt.inventory_item_id = msiv.inventory_item_id
18397 	   and mmtt.transaction_action_id = 28
18398 	   AND mmtt.organization_id = mil.organization_id(+)
18399 	   AND mmtt.locator_id = mil.inventory_location_id(+)
18400 	   AND WDD.source_line_id = MMTT.trx_source_line_id
18401 	   AND wdd.move_order_line_id=mmtt.move_order_line_id
18402 	   AND mmtt.transaction_temp_id = wooi.source_task_id(+)
18403 	   AND mmtt.operation_plan_id = wopv.operation_plan_id(+)
18404 	   AND WWL.delivery_detail_id = WDD.delivery_detail_id
18405 	   and wwl.organization_id = wdd.organization_id
18406 	   and wwl.wave_header_id = p_wave_header_id
18407 	   and nvl(wwl.remove_from_wave_flag,'N') <> 'Y'
18408 	   AND mmtt.standard_operation_id = bso.standard_operation_id(+)
18409 	   AND mmtt.organization_id = bso.organization_id(+)
18410 	   AND wdt.person_resource_id = br1.resource_id(+)
18411 	   AND mmtt.parent_line_id is null --12736677 no longer display bulk tasks (parent or child)
18412 	union
18413 	--12736677 now include separate select for the bulk tasks
18414 	SELECT /*+ use_nl(WWL,WDD,mmtt) index(mmtt) */ DISTINCT WDT.TASK_ID,
18415 					(SELECT LICENSE_PLATE_NUMBER
18416 					   FROM WMS_LICENSE_PLATE_NUMBERS
18417 					  WHERE LPN_ID = MMTT.TRANSFER_LPN_ID) TO_LPN,
18418 					MMTT.TRANSFER_LPN_ID TO_LPN_ID,
18419 					mmtt.transaction_temp_id,
18420 					mmtt.transaction_type_id,
18421 					mmtt.transaction_action_id,
18422 					mmtt.transaction_source_type_id,
18423 					mmtt.transaction_source_id,
18424 					mmtt.trx_source_line_id,
18425 					mmtt.organization_id,
18426 					(SELECT organization_code
18427 					   FROM mtl_parameters
18428 					  WHERE organization_id = mmtt.organization_id) organization_code,
18429 					mmtt.transfer_organization to_organization_id,
18430 					(SELECT organization_code
18431 					   FROM mtl_parameters
18432 					  WHERE organization_id = mmtt.transfer_organization) to_organization_code,
18433 					mmtt.transfer_subinventory to_subinventory,
18434 					mmtt.transfer_to_location to_locator_id,
18435 				(SELECT  mil.concatenated_segments --bug12642410
18436                    FROM MTL_ITEM_LOCATIONS_kfv mil
18437                   WHERE mmtt.transfer_to_location = mil.inventory_location_id
18438                     AND mmtt.organization_id = mil.organization_id) to_locator,
18439 					mmtt.standard_operation_id user_task_type_id,
18440 					bso.operation_code,
18441 					wdt.person_id person_id,
18442 					wdt.person_id person_id_original,
18443 					pap.full_name person,
18444 					wdt.effective_start_date,
18445 					wdt.effective_end_date,
18446 					wdt.person_resource_id,
18447 					br1.resource_code,
18448 					decode(wdt.status,
18449 						   null,
18450 						   nvl(mmtt.wms_task_status, 1),
18451 						   wdt.status) status_id,
18452 					decode(wdt.status,
18453 						   null,
18454 						   nvl(mmtt.wms_task_status, 1),
18455 						   wdt.status) status_id_original,
18456 	(select meaning from mfg_lookups where lookup_type = 'WMS_TASK_STATUS' and lookup_code = decode(wdt.status,null, nvl(mmtt.wms_task_status,1), wdt.status)) STATUS, --12693583
18457 					mmtt.last_update_date mmtt_last_update_date,
18458 					mmtt.last_updated_by mmtt_last_update_by,
18459 					wdt.last_update_date wdt_last_update_date,
18460 					wdt.last_updated_by wdt_last_update_by,
18461 					mmtt.task_priority,
18462 					mmtt.task_priority priority_original,
18463 					mmtt.wms_task_type task_type_id,
18464 					 (select meaning from mfg_lookups where lookup_type = 'WMS_TASK_TYPES' and lookup_code =mmtt.wms_task_type) task_type, --12693583
18465 					'N' is_modified,
18466 					l_plan_task_types(4) plan_task, --12693583, 12736677
18467 					' ' result,
18468 					' ' error,
18469 					null source_header,
18470 					null line_number,
18471 					MSIV.concatenated_segments item, --bug12642410
18472 					MSIV.DESCRIPTION item_description,
18473 					MMTT.REVISION,
18474 					MMTT.SUBINVENTORY_CODE subinventory,
18475 					(mil.concatenated_segments) locator,
18476 				MMTT.SECONDARY_TRANSACTION_QUANTITY,
18477 					MMTT.SECONDARY_UOM_CODE,
18478 					MMTT.OPERATION_PLAN_ID,
18479 					WOPV.OPERATION_PLAN_NAME,
18480 					WOOI.OPERATION_SEQUENCE,
18481 					WOOI.OP_PLAN_INSTANCE_ID,
18482 					null SHIP_METHOD,
18483 					null shipment_date,
18484 					null shipment_priority,
18485 					MMTT.DEPARTMENT_CODE,
18486 					'N' CHILD_TASKS,
18487 					(select count(1) from mtl_material_transactions_temp where parent_line_id = mmtt.transaction_temp_id and transaction_temp_id <> mmtt.transaction_temp_id) num_of_child_tasks,
18488 					(SELECT LICENSE_PLATE_NUMBER
18489 					   FROM WMS_LICENSE_PLATE_NUMBERS
18490 					  WHERE LPN_ID = MMTT.ALLOCATED_LPN_ID) ALLOCATED_LPN,
18491 					(SELECT LICENSE_PLATE_NUMBER
18492 					   FROM WMS_LICENSE_PLATE_NUMBERS
18493 					  WHERE LPN_ID = MMTT.CARTONIZATION_ID) CARTONIZED_LPN,
18494 				   (SELECT concatenated_segments FROM mtl_system_items_kfv WHERE inventory_item_id= MMTT.CONTAINER_ITEM_ID AND organization_id=mmtt.organization_id),
18495 					(SELECT LICENSE_PLATE_NUMBER
18496 					   FROM WMS_LICENSE_PLATE_NUMBERS
18497 					  WHERE LPN_ID = MMTT.CONTENT_LPN_ID) CONTENT_LPN,
18498 					WDT.DISPATCHED_TIME,
18499 					WDT.EQUIPMENT_ID,
18500 					(SELECT LICENSE_PLATE_NUMBER
18501 					   FROM WMS_LICENSE_PLATE_NUMBERS
18502 					  WHERE LPN_ID = MMTT.LPN_ID) FROM_LPN,
18503 					MMTT.PARENT_LINE_ID,
18504 					mmtt.move_order_line_id,
18505 					MMTT.PICK_SLIP_NUMBER,
18506 					(SELECT LICENSE_PLATE_NUMBER
18507 					   FROM WMS_LICENSE_PLATE_NUMBERS
18508 					  WHERE LPN_ID = MMTT.CONTENT_LPN_ID) PICKED_LPN,
18509 					mmtt.transaction_uom,
18510 					mmtt.transaction_quantity
18511 	  FROM mtl_material_transactions_temp mmtt,
18512 	       mtl_material_transactions_temp mmtt2, --12736677
18513 		   WSH_DELIVERY_DETAILS           WDD,
18514 		   MTL_ITEM_LOCATIONS_kfv             mil, --bug12642410
18515 		   mtl_system_items_kfv           msiv,
18516 		   wms_op_plans_vl                wopv,
18517 		   per_all_people_f               pap,
18518 		   wms_op_operation_instances     wooi,
18519 		   WMS_WP_WAVE_LINES              WWL,
18520 		   wms_dispatched_tasks           wdt,
18521 		   bom_standard_operations        bso,
18522 		   bom_resources                  br1
18523 	 where mmtt.transaction_temp_id = wdt.transaction_temp_id(+)
18524 	   AND wdt.person_id = pap.person_id(+)
18525 	   AND wdt.effective_start_date >= pap.effective_start_date(+)
18526 	   AND wdt.effective_end_date <= pap.effective_end_date(+)
18527 	   AND mmtt.organization_id = msiv.organization_id
18528 	   AND mmtt.inventory_item_id = msiv.inventory_item_id
18529 	   and mmtt.transaction_action_id = 28
18530 	   AND mmtt.organization_id = mil.organization_id(+)
18531 	   AND mmtt.locator_id = mil.inventory_location_id(+)
18532 	   AND WDD.source_line_id = mmtt2.trx_source_line_id --12736677
18533 	   AND wdd.move_order_line_id=mmtt2.move_order_line_id --12736677
18534 	   and mmtt.transaction_temp_id = mmtt2.parent_line_id --12736677
18535 	   and mmtt.transaction_temp_id = mmtt.parent_line_id --12736677
18536 	   AND mmtt.transaction_temp_id = wooi.source_task_id(+)
18537 	   AND mmtt.operation_plan_id = wopv.operation_plan_id(+)
18538 	   AND WWL.delivery_detail_id = WDD.delivery_detail_id
18539 	   and wwl.organization_id = wdd.organization_id
18540 	   and wwl.wave_header_id = p_wave_header_id
18541 	   and nvl(wwl.remove_from_wave_flag,'N') <> 'Y'
18542 	   AND mmtt.standard_operation_id = bso.standard_operation_id(+)
18543 	   AND mmtt.organization_id = bso.organization_id(+)
18544 	   AND wdt.person_resource_id = br1.resource_id(+)
18545 	union
18546 	SELECT DISTINCT WDT.TASK_ID,
18547 					(SELECT LICENSE_PLATE_NUMBER
18548 					   FROM WMS_LICENSE_PLATE_NUMBERS
18549 					  WHERE LPN_ID = MMTT.TRANSFER_LPN_ID) TO_LPN,
18550 					MMTT.TRANSFER_LPN_ID TO_LPN_ID,
18551 					mmtt.transaction_temp_id,
18552 					mmtt.transaction_type_id,
18553 					mmtt.transaction_action_id,
18554 					mmtt.transaction_source_type_id,
18555 					mmtt.transaction_source_id,
18556 					mmtt.trx_source_line_id,
18557 					mmtt.organization_id,
18558 					(SELECT organization_code
18559 					   FROM mtl_parameters
18560 					  WHERE organization_id = mmtt.organization_id) organization_code,
18561 					mmtt.transfer_organization to_organization_id,
18562 					(SELECT organization_code
18563 					   FROM mtl_parameters
18564 					  WHERE organization_id = mmtt.transfer_organization) to_organization_code,
18565 					mmtt.transfer_subinventory to_subinventory,
18566 					mmtt.transfer_to_location to_locator_id,
18567 			(select mil.concatenated_segments --bug12642410
18568                    FROM MTL_ITEM_LOCATIONS_kfv mil
18569                   WHERE mmtt.transfer_to_location = mil.inventory_location_id
18570                     AND mmtt.organization_id = mil.organization_id) to_locator,
18571 					mmtt.standard_operation_id user_task_type_id,
18572 					bso.operation_code,
18573 					wdt.person_id person_id,
18574 					wdt.person_id person_id_original,
18575 					pap.full_name person,
18576 					wdt.effective_start_date,
18577 					wdt.effective_end_date,
18578 					wdt.person_resource_id,
18579 					br1.resource_code,
18580 					decode(wdt.status,
18581 						   null,
18582 						   nvl(mmtt.wms_task_status, 1),
18583 						   wdt.status) status_id,
18584 					decode(wdt.status,
18585 						   null,
18586 						   nvl(mmtt.wms_task_status, 1),
18587 						   wdt.status) status_id_original,
18588 	(select meaning from mfg_lookups where lookup_type = 'WMS_TASK_STATUS' and lookup_code = decode(wdt.status,null, nvl(mmtt.wms_task_status,1), wdt.status)) STATUS, --12693583
18589 					mmtt.last_update_date mmtt_last_update_date,
18590 					mmtt.last_updated_by mmtt_last_update_by,
18591 					wdt.last_update_date wdt_last_update_date,
18592 					wdt.last_updated_by wdt_last_update_by,
18593 					mmtt.task_priority,
18594 					mmtt.task_priority priority_original,
18595 					mmtt.wms_task_type task_type_id,
18596 (select meaning from mfg_lookups where lookup_type = 'WMS_TASK_TYPES' and lookup_code =mmtt.wms_task_type) task_type, --12693583
18597 					'N' is_modified,
18598 					decode(mmtt.parent_line_id,
18599 						   null,
18600 						   decode(mmtt.operation_plan_id,
18601 								  null,
18602 								  l_plan_task_types(1),
18603 								  decode(mmtt.transaction_action_id,
18604 										 28,
18605 										 l_plan_task_types(1),
18606 										 decode(mmtt.transaction_source_type_id,
18607 												5,
18608 												l_plan_task_types(1),
18609 												13,
18610 												decode(mmtt.transaction_type_id,
18611 													   51,
18612 													   l_plan_task_types(1),
18613 													   l_plan_task_types(3))))),
18614 						   mmtt.transaction_temp_id,
18615 						   l_plan_task_types(4),
18616 						   l_plan_task_types(2)) plan_task, --12693583
18617 				   ' ' result,
18618 					' ' error,
18619 					to_char(mtrh.request_number) source_header,
18620 					to_char(mtrl.line_number) line_number,
18621 					MSIV.concatenated_segments, --bug12642410
18622 					MSIV.DESCRIPTION,
18623 					MMTT.REVISION,
18624 					MMTT.SUBINVENTORY_CODE,
18625 					(mil.concatenated_segments) locator, --bug12642410
18626 					MMTT.SECONDARY_TRANSACTION_QUANTITY,
18627 					MMTT.SECONDARY_UOM_CODE,
18628 					MMTT.OPERATION_PLAN_ID,
18629 					WOPV.OPERATION_PLAN_NAME,
18630 					WOOI.OPERATION_SEQUENCE,
18631 					WOOI.OP_PLAN_INSTANCE_ID,
18632 					null SHIP_METHOD,
18633 					null shipment_date,
18634 					null shipment_priority,
18635 					MMTT.DEPARTMENT_CODE,
18636 					DECODE(PARENT_TRANSACTION_TEMP_ID, NULL, 'N', 'Y') CHILD_TASKS,
18637 					null num_of_child_tasks,
18638 					(SELECT LICENSE_PLATE_NUMBER
18639 					   FROM WMS_LICENSE_PLATE_NUMBERS
18640 					  WHERE LPN_ID = MMTT.ALLOCATED_LPN_ID) ALLOCATED_LPN,
18641 					(SELECT LICENSE_PLATE_NUMBER
18642 					   FROM WMS_LICENSE_PLATE_NUMBERS
18643 					  WHERE LPN_ID = MMTT.CARTONIZATION_ID) CARTONIZED_LPN,
18644                  (SELECT concatenated_segments FROM mtl_system_items_kfv WHERE inventory_item_id= MMTT.CONTAINER_ITEM_ID AND organization_id=mmtt.organization_id), --bug12642410
18645 					(SELECT LICENSE_PLATE_NUMBER
18646 					   FROM WMS_LICENSE_PLATE_NUMBERS
18647 					  WHERE LPN_ID = MMTT.CONTENT_LPN_ID) CONTENT_LPN,
18648 					WDT.DISPATCHED_TIME,
18649 					WDT.EQUIPMENT_ID,
18650 					(SELECT LICENSE_PLATE_NUMBER
18651 					   FROM WMS_LICENSE_PLATE_NUMBERS
18652 					  WHERE LPN_ID = MMTT.LPN_ID) FROM_LPN,
18653 					MMTT.PARENT_LINE_ID,
18654 					mmtt.move_order_line_id,
18655 					MMTT.PICK_SLIP_NUMBER,
18656 					(SELECT LICENSE_PLATE_NUMBER
18657 					   FROM WMS_LICENSE_PLATE_NUMBERS
18658 					  WHERE LPN_ID = MMTT.CONTENT_LPN_ID) PICKED_LPN,
18659 					mmtt.transaction_uom,
18660 					mmtt.transaction_quantity
18661 	  FROM mtl_material_transactions_temp mmtt,
18662 		   MTL_ITEM_LOCATIONS_kfv             mil, --bug12642410
18663 		   mtl_system_items_kfv           msiv,
18664 		   wms_op_plans_vl                wopv,
18665 		   per_all_people_f               pap,
18666 		   wms_op_operation_instances     wooi,
18667 		   WMS_WP_WAVE_LINES              WWL,
18668 		   wms_dispatched_tasks           wdt,
18669 		   bom_standard_operations        bso,
18670 		   bom_resources                  br1,
18671 		   mtl_txn_request_headers        mtrh,
18672 		   mtl_txn_request_lines          mtrl,
18673 		   wms_replenishment_Details wrd
18674 	 where mmtt.transaction_temp_id = wdt.transaction_temp_id(+)
18675 	   and mtrl.header_id = mtrh.header_id
18676 	   --and mtrl.line_id = mmtt.trx_source_line_id changed to move_order_line_id below
18677 	   and mtrl.line_id = mmtt.move_order_line_id
18678 	   and mmtt.transaction_source_type_id = 4
18679 		  and (mtrh.move_order_type = 2)
18680 	   and (mmtt.transaction_action_id =2)
18681 	   AND wdt.person_id = pap.person_id(+)
18682 	   AND wdt.effective_start_date >= pap.effective_start_date(+)
18683 	   AND wdt.effective_end_date <= pap.effective_end_date(+)
18684 	   AND mmtt.organization_id = msiv.organization_id
18685 	   AND mmtt.inventory_item_id = msiv.inventory_item_id
18686 	   AND mmtt.organization_id = mil.organization_id
18687 	   AND mmtt.locator_id = mil.inventory_location_id
18688 	   AND mmtt.transaction_temp_id = wooi.source_task_id(+)
18689 	   AND mmtt.operation_plan_id = wopv.operation_plan_id(+)
18690 	   and wrd.organization_id = wrd.organization_id
18691 	   and wwl.wave_header_id = p_wave_header_id
18692 	   AND wwl.demand_source_header_id=wrd.demand_header_id
18693 		and nvl(wwl.remove_from_wave_flag,'N') <> 'Y'
18694 	   AND wrd.source_header_id=mtrh.header_id
18695 	   AND mmtt.standard_operation_id = bso.standard_operation_id(+)
18696 	   AND mmtt.organization_id = bso.organization_id(+)
18697 	   AND wdt.person_resource_id = br1.resource_id(+)
18698 	union
18699 	SELECT DISTINCT wdth.TASK_ID,
18700 					(SELECT LICENSE_PLATE_NUMBER
18701 					   FROM WMS_LICENSE_PLATE_NUMBERS
18702 					  WHERE LPN_ID = mmt.TRANSFER_LPN_ID) TO_LPN,
18703 					mmt.TRANSFER_LPN_ID TO_LPN_ID,
18704 					mmt.transaction_id,
18705 					mmt.transaction_type_id,
18706 					mmt.transaction_action_id,
18707 					mmt.transaction_source_type_id,
18708 					mmt.transaction_source_id,
18709 					mmt.trx_source_line_id,
18710 					mmt.organization_id,
18711 					(SELECT organization_code
18712 					   FROM mtl_parameters
18713 					  WHERE organization_id = mmt.organization_id) organization_code,
18714 					mmt.transfer_organization_id to_organization_id,
18715 					(SELECT organization_code
18716 					   FROM mtl_parameters
18717 					  WHERE organization_id = mmt.transfer_organization_id) to_organization_code,
18718 					mmt.transfer_subinventory to_subinventory,
18719 					mmt.transfer_locator_id to_locator_id,
18720 			(SELECT  mil.concatenated_segments --bug12642410
18721 
18722                    FROM MTL_ITEM_LOCATIONS_kfv mil
18723                   WHERE mmt.transfer_locator_id = mil.inventory_location_id
18724                     AND mmt.organization_id = mil.organization_id) to_locator,
18725 					wdth.user_task_type user_task_type_id,
18726 					bso.operation_code,
18727 					wdth.person_id person_id,
18728 					wdth.person_id person_id_original,
18729 					pap.full_name person,
18730 					wdth.effective_start_date,
18731 					wdth.effective_end_date,
18732 					wdth.person_resource_id,
18733 					br1.resource_code,
18734 					6 status_id,
18735 					6 status_id_original,
18736 					(SELECT meaning
18737 					   FROM mfg_lookups
18738 					  WHERE lookup_type = 'WMS_TASK_STATUS'
18739 						AND lookup_code = 6) STATUS,
18740 					to_date(null)mmtt_last_update_date,
18741 					NULL mmtt_last_updated_by,
18742 					to_date(null) wdt_last_update_date,
18743 					NULL wdt_last_updated_by,
18744 					wdth.priority,
18745 					wdth.priority priority_original,
18746 					wdth.task_type task_type_id,
18747 (select meaning from mfg_lookups where lookup_type = 'WMS_TASK_TYPES' and lookup_code =wdth.task_type) task_type, --12693583
18748 					'N' is_modified,
18749 					decode(wdth.is_parent,
18750 						   'Y',
18751 						   decode(wdth.transaction_action_id,
18752 								  28,
18753 								  l_plan_task_types(4),
18754 								  decode(wdth.transaction_source_type_id,
18755 										 5,
18756 										 decode(wdth.transaction_type_id,
18757 												35,
18758 												l_plan_task_types(4),
18759 												l_plan_task_types(3)),
18760 										 13,
18761 										 decode(wdth.transaction_type_id,
18762 												51,
18763 												l_plan_task_types(4),
18764 												l_plan_task_types(3)),
18765 										 l_plan_task_types(3))),
18766 						   decode(wdth.parent_transaction_id,
18767 								  null,
18768 								  l_plan_task_types(1),
18769 								  decode(wdth.transaction_action_id,
18770 										 28,
18771 										 l_plan_task_types(5),
18772 										 decode(wdth.transaction_source_type_id,
18773 												5,
18774 												decode(wdth.transaction_type_id,
18775 													   35,
18776 													   l_plan_task_types(5),
18777 													   l_plan_task_types(2)),
18778 												13,
18779 												decode(wdth.transaction_type_id,
18780 													   51,
18781 													   l_plan_task_types(5),
18782 													   l_plan_task_types(2)),
18783 												l_plan_task_types(2))))) plan_task, --12693583
18784 					' ' result,
18785 					' ' error,
18786 					WDD.SOURCE_HEADER_NUMBER source_header,
18787 					WDD.SOURCE_LINE_NUMBER line_number,
18788 					MSIV.concatenated_segments, --bug12642410
18789 					MSIV.DESCRIPTION,
18790 					mmt.REVISION,
18791 					mmt.subinventory_code,
18792 		(mil.concatenated_segments) locator, --bug12642410
18793 					abs(mmt.SECONDARY_TRANSACTION_QUANTITY), -- 13360870
18794 					mmt.SECONDARY_UOM_CODE,
18795 					WDTH.OPERATION_PLAN_ID,
18796 					WOPV.OPERATION_PLAN_NAME,
18797 					NULL operation_sequence,
18798 					WDTH.OP_PLAN_INSTANCE_ID,
18799 					WDD.SHIP_METHOD_CODE SHIP_METHOD,
18800 					WDD.DATE_SCHEDULED shipment_date,
18801 					WDD.SHIPMENT_PRIORITY_CODE shipment_priority,
18802 					To_Char(mmt.DEPARTMENT_id),
18803 					DECODE(mmt.PARENT_TRANSACTION_ID, NULL, 'N', 'Y') CHILD_TASKS,
18804 					0 num_of_child_tasks,
18805 					' ' ALLOCATED_LPN,
18806 					' ' CARTONIZED_LPN,
18807 					' ' CONTAINER_ITEM_ID,
18808 					(SELECT LICENSE_PLATE_NUMBER
18809 					   FROM WMS_LICENSE_PLATE_NUMBERS
18810 					  WHERE LPN_ID = mmt.CONTENT_LPN_ID) CONTENT_LPN,
18811 					wdth.DISPATCHED_TIME,
18812 					wdth.EQUIPMENT_ID,
18813 					(SELECT LICENSE_PLATE_NUMBER
18814 					   FROM WMS_LICENSE_PLATE_NUMBERS
18815 					  WHERE LPN_ID = mmt.LPN_ID) FROM_LPN,
18816 					wdth.parent_transaction_id,
18817 					mmt.move_order_line_id,
18818 					mmt.PICK_SLIP_NUMBER,
18819 					(SELECT LICENSE_PLATE_NUMBER
18820 					   FROM WMS_LICENSE_PLATE_NUMBERS
18821 					  WHERE LPN_ID = mmt.CONTENT_LPN_ID) PICKED_LPN,
18822 					mmt.transaction_uom,
18823 					abs(mmt.transaction_quantity)
18824 	  FROM mtl_material_transactions    mmt,
18825 		   WSH_DELIVERY_DETAILS         WDD,
18826 		   MTL_ITEM_LOCATIONS_kfv           mil, --bug12642410
18827 		   mtl_system_items_kfv         msiv,
18828 		   wms_op_plans_vl              wopv,
18829 		   per_all_people_f             pap,
18830 		   wms_op_operation_instances   wooi,
18831 		   WMS_WP_WAVE_LINES            WWL,
18832 		   wms_dispatched_tasks_history wdth,
18833 		   bom_standard_operations      bso,
18834 		   bom_resources                br1
18835 	 where mmt.transaction_set_id = wdth.transaction_id
18836 	  AND decode(mmt.transfer_transaction_id, null, 0, mmt.transaction_quantity) <= 0
18837 	   AND nvl(mmt.transaction_batch_id, -1) =
18838 		   decode(wdth.task_type,
18839 				  2,
18840 				  nvl(mmt.transaction_batch_id, -1),
18841 				  3,
18842 				  nvl(mmt.transaction_batch_id, -1),
18843 				  decode(wdth.transaction_batch_id,
18844 						 -999,
18845 						 nvl(mmt.transaction_batch_id, -1),
18846 						 nvl(wdth.transaction_batch_id,
18847 							 nvl(mmt.transaction_batch_id, -1))))
18848 	   AND nvl(mmt.transaction_batch_seq, -1) =
18849 		   decode(wdth.task_type,
18850 				  2,
18851 				  nvl(mmt.transaction_batch_seq, -1),
18852 				  3,
18853 				  nvl(mmt.transaction_batch_seq, -1),
18854 				  decode(wdth.transaction_batch_seq,
18855 						 -999,
18856 						 nvl(mmt.transaction_batch_seq, -1),
18857 						 nvl(wdth.transaction_batch_seq,
18858 							 nvl(mmt.transaction_batch_seq, -1))))
18859 	   AND wdth.person_id = pap.person_id(+)
18860 	   AND wdth.effective_start_date >= pap.effective_start_date(+)
18861 	   AND wdth.effective_end_date <= pap.effective_end_date(+)
18862 	   AND mmt.organization_id = msiv.organization_id
18863 	   AND mmt.inventory_item_id = msiv.inventory_item_id
18864 	   AND mmt.organization_id = mil.organization_id(+)
18865 	   AND mmt.locator_id = mil.inventory_location_id(+)
18866 	    -- commented for bug 14293167
18867 		/* after performing the shipping transaction in wave workbench, the wdd.source_line_id was being updated
18868 		for the split line and following condition was not getting satisfied*/
18869 	   -- AND WDD.source_line_id = mmt.trx_source_line_id
18870 	   and WDD.move_order_line_id = mmt.move_order_line_id
18871 	   AND wdth.transaction_temp_id = wooi.source_task_id(+)
18872 	   AND wdth.operation_plan_id = wopv.operation_plan_id(+)
18873 	   AND WWL.delivery_detail_id = WDD.delivery_detail_id
18874 	   and wwl.organization_id = wdd.organization_id
18875 	   and wwl.wave_header_id = p_wave_header_id
18876 		and nvl(wwl.remove_from_wave_flag,'N') <> 'Y'
18877 	   AND wdth.user_task_type = bso.standard_operation_id(+)
18878 	   AND wdth.organization_id = bso.organization_id(+)
18879 	   and mmt.transaction_action_id = 28
18880 	   AND wdth.person_resource_id = br1.resource_id(+)
18881 	   union -- For Crossdocked Tasks
18882 	   SELECT DISTINCT WDT.TASK_ID,
18883 					(SELECT LICENSE_PLATE_NUMBER
18884 					   FROM WMS_LICENSE_PLATE_NUMBERS
18885 					  WHERE LPN_ID = MMTT.TRANSFER_LPN_ID) TO_LPN,
18886 					MMTT.TRANSFER_LPN_ID TO_LPN_ID,
18887 					mmtt.transaction_temp_id,
18888 					mmtt.transaction_type_id,
18889 					mmtt.transaction_action_id,
18890 					mmtt.transaction_source_type_id,
18891 					mmtt.transaction_source_id,
18892 					mmtt.trx_source_line_id,
18893 					mmtt.organization_id,
18894 					(SELECT organization_code
18895 					   FROM mtl_parameters
18896 					  WHERE organization_id = mmtt.organization_id) organization_code,
18897 					mmtt.transfer_organization to_organization_id,
18898 					(SELECT organization_code
18899 					   FROM mtl_parameters
18900 					  WHERE organization_id = mmtt.transfer_organization) to_organization_code,
18901 					mmtt.transfer_subinventory to_subinventory,
18902 					mmtt.transfer_to_location to_locator_id,
18903 			(SELECT  mil.concatenated_segments --bug12642410
18904 
18905                    FROM MTL_ITEM_LOCATIONS_kfv mil
18906                   WHERE mmtt.transfer_to_location = mil.inventory_location_id
18907                     AND mmtt.organization_id = mil.organization_id) to_locator,
18908 					mmtt.standard_operation_id user_task_type_id,
18909 					bso.operation_code,
18910 					wdt.person_id person_id,
18911 					wdt.person_id person_id_original,
18912 					pap.full_name person,
18913 					wdt.effective_start_date,
18914 					wdt.effective_end_date,
18915 					wdt.person_resource_id,
18916 					br1.resource_code,
18917 					decode(wdt.status,
18918 						   null,
18919 						   nvl(mmtt.wms_task_status, 1),
18920 						   wdt.status) status_id,
18921 					decode(wdt.status,
18922 						   null,
18923 						   nvl(mmtt.wms_task_status, 1),
18924 						   wdt.status) status_id_original,
18925 	(select meaning from mfg_lookups where lookup_type = 'WMS_TASK_STATUS' and lookup_code = decode(wdt.status,null, nvl(mmtt.wms_task_status,1), wdt.status)) STATUS, --12693583
18926 					mmtt.last_update_date mmtt_last_update_date,
18927 					mmtt.last_updated_by mmtt_last_update_by,
18928 					wdt.last_update_date wdt_last_update_date,
18929 					wdt.last_updated_by wdt_last_update_by,
18930 					mmtt.task_priority,
18931 					mmtt.task_priority priority_original,
18932 					mmtt.wms_task_type task_type_id,
18933 (select meaning from mfg_lookups where lookup_type = 'WMS_TASK_TYPES' and lookup_code =mmtt.wms_task_type) task_type, --12693583
18934 					'N' is_modified,
18935 					decode(mmtt.parent_line_id,
18936 						   null,
18937 						   decode(mmtt.operation_plan_id,
18938 								  null,
18939 								  l_plan_task_types(1),
18940 								  decode(mmtt.transaction_action_id,
18941 										 28,
18942 										 l_plan_task_types(1),
18943 										 decode(mmtt.transaction_source_type_id,
18944 												5,
18945 												l_plan_task_types(1),
18946 												13,
18947 												decode(mmtt.transaction_type_id,
18948 													   51,
18949 													   l_plan_task_types(1),
18950 													   l_plan_task_types(3))))),
18951 						   mmtt.transaction_temp_id,
18952 						   l_plan_task_types(4),
18953 						   l_plan_task_types(2)) plan_task,
18954 					' ' result,
18955 					' ' error,
18956 					WDD.SOURCE_HEADER_NUMBER source_header,
18957 					WDD.SOURCE_LINE_NUMBER line_number,
18958                 MSIV.concatenated_segments item, --bug12642410
18959 					MSIV.DESCRIPTION item_description,
18960 					MMTT.REVISION,
18961 					MMTT.SUBINVENTORY_CODE subinventory,
18962 					(mil.concatenated_segments) locator,
18963 				MMTT.SECONDARY_TRANSACTION_QUANTITY,
18964 					MMTT.SECONDARY_UOM_CODE,
18965 					MMTT.OPERATION_PLAN_ID,
18966 					WOPV.OPERATION_PLAN_NAME,
18967 					WOOI.OPERATION_SEQUENCE,
18968 					WOOI.OP_PLAN_INSTANCE_ID,
18969 					WDD.SHIP_METHOD_CODE SHIP_METHOD,
18970 					WDD.DATE_SCHEDULED shipment_date,
18971 					WDD.SHIPMENT_PRIORITY_CODE shipment_priority,
18972 					MMTT.DEPARTMENT_CODE,
18973 					DECODE(PARENT_TRANSACTION_TEMP_ID, NULL, 'N', 'Y') CHILD_TASKS,
18974 					null num_of_child_tasks,
18975 					(SELECT LICENSE_PLATE_NUMBER
18976 					   FROM WMS_LICENSE_PLATE_NUMBERS
18977 					  WHERE LPN_ID = MMTT.ALLOCATED_LPN_ID) ALLOCATED_LPN,
18978 					(SELECT LICENSE_PLATE_NUMBER
18979 					   FROM WMS_LICENSE_PLATE_NUMBERS
18980 					  WHERE LPN_ID = MMTT.CARTONIZATION_ID) CARTONIZED_LPN,
18981                (SELECT concatenated_segments FROM mtl_system_items_kfv WHERE inventory_item_id= MMTT.CONTAINER_ITEM_ID AND organization_id=mmtt.organization_id), --bug12642410
18982 					(SELECT LICENSE_PLATE_NUMBER
18983 					   FROM WMS_LICENSE_PLATE_NUMBERS
18984 					  WHERE LPN_ID = MMTT.CONTENT_LPN_ID) CONTENT_LPN,
18985 					WDT.DISPATCHED_TIME,
18986 					WDT.EQUIPMENT_ID,
18987 					(SELECT LICENSE_PLATE_NUMBER
18988 					   FROM WMS_LICENSE_PLATE_NUMBERS
18989 					  WHERE LPN_ID = MMTT.LPN_ID) FROM_LPN,
18990 					MMTT.PARENT_LINE_ID,
18991 					mmtt.move_order_line_id,
18992 					MMTT.PICK_SLIP_NUMBER,
18993 					(SELECT LICENSE_PLATE_NUMBER
18994 					   FROM WMS_LICENSE_PLATE_NUMBERS
18995 					  WHERE LPN_ID = MMTT.CONTENT_LPN_ID) PICKED_LPN,
18996 					mmtt.transaction_uom,
18997 					mmtt.transaction_quantity
18998 	  FROM mtl_material_transactions_temp mmtt,
18999 		   WSH_DELIVERY_DETAILS           WDD,
19000 		   MTL_ITEM_LOCATIONS_kfv             mil, --bug12642410
19001 		   mtl_system_items_kfv           msiv,
19002 		   wms_op_plans_vl                wopv,
19003 		   per_all_people_f               pap,
19004 		   wms_op_operation_instances     wooi,
19005 		   WMS_WP_WAVE_LINES              WWL,
19006 		   wms_dispatched_tasks           wdt,
19007 		   bom_standard_operations        bso,
19008 		   bom_resources                  br1,
19009 		   mtl_txn_request_lines mtrl
19010 	 where mmtt.transaction_temp_id = wdt.transaction_temp_id(+)
19011 	   AND wdt.person_id = pap.person_id(+)
19012 	   AND wdt.effective_start_date >= pap.effective_start_date(+)
19013 	   AND wdt.effective_end_date <= pap.effective_end_date(+)
19014 	   AND mmtt.organization_id = msiv.organization_id
19015 	   AND mmtt.inventory_item_id = msiv.inventory_item_id
19016 	   and mmtt.transaction_action_id = 27
19017 	   AND mmtt.organization_id = mil.organization_id(+)
19018 	   AND mmtt.locator_id = mil.inventory_location_id(+)
19019 	--   AND WDD.source_line_id = MMTT.trx_source_line_id
19020 	and mmtt.move_order_line_id=mtrl.line_id
19021 	   AND mmtt.transaction_temp_id = wooi.source_task_id(+)
19022 	   AND mmtt.operation_plan_id = wopv.operation_plan_id(+)
19023 	   AND WWL.delivery_detail_id = WDD.delivery_detail_id
19024 	   and wwl.organization_id = wdd.organization_id
19025 	   and wwl.wave_header_id = p_wave_header_id
19026 		and nvl(wwl.remove_from_wave_flag,'N') <> 'Y'
19027 	   AND mtrl.BACKORDER_DELIVERY_DETAIL_ID =  wdd.delivery_detail_id
19028 	   AND mmtt.standard_operation_id = bso.standard_operation_id(+)
19029 	   AND mmtt.organization_id = bso.organization_id(+)
19030 	   AND wdt.person_resource_id = br1.resource_id(+)
19031 	   union -- Crossdocked Tasks completed
19032 	  SELECT DISTINCT wdth.TASK_ID,
19033 					(SELECT LICENSE_PLATE_NUMBER
19034 					   FROM WMS_LICENSE_PLATE_NUMBERS
19035 					  WHERE LPN_ID = wdth.TRANSFER_LPN_ID) TO_LPN,
19036 					wdth.TRANSFER_LPN_ID TO_LPN_ID,
19037 					wdth.transaction_id,
19038 					wdth.transaction_type_id,
19039 					wdth.transaction_action_id,
19040 					wdth.transaction_source_type_id,
19041 				   to_number(null) transaction_source_id,
19042 				   to_number(null) trx_source_line_id,
19043 					wdth.organization_id,
19044 					(SELECT organization_code
19045 					   FROM mtl_parameters
19046 					  WHERE organization_id = wdth.organization_id) organization_code,
19047 					wdth.transfer_organization_id to_organization_id,
19048 					(SELECT organization_code
19049 					   FROM mtl_parameters
19050 					  WHERE organization_id = wdth.transfer_organization_id) to_organization_code,
19051 					wdth.dest_subinventory_code to_subinventory,
19052 					wdth.dest_locator_id to_locator_id,
19053 			(SELECT  mil.concatenated_segments --bug12642410
19054 
19055                    FROM MTL_ITEM_LOCATIONS_kfv mil
19056                   WHERE wdth.dest_locator_id = mil.inventory_location_id
19057                     AND wdth.organization_id = mil.organization_id) to_locator,
19058 					wdth.user_task_type user_task_type_id,
19059 					bso.operation_code,
19060 					wdth.person_id person_id,
19061 					wdth.person_id person_id_original,
19062 					pap.full_name person,
19063 					wdth.effective_start_date,
19064 					wdth.effective_end_date,
19065 					wdth.person_resource_id,
19066 					br1.resource_code,
19067 					6 status_id,
19068 					6 status_id_original,
19069 					(SELECT meaning
19070 					   FROM mfg_lookups
19071 					  WHERE lookup_type = 'WMS_TASK_STATUS'
19072 						AND lookup_code = 6) STATUS,
19073 					to_date(null)mmtt_last_update_date,
19074 					NULL mmtt_last_updated_by,
19075 					to_date(null) wdt_last_update_date,
19076 					NULL wdt_last_updated_by,
19077 					wdth.priority,
19078 					wdth.priority priority_original,
19079 					wdth.task_type task_type_id,
19080 					 (select meaning from mfg_lookups where lookup_type = 'WMS_TASK_TYPES' and lookup_code =wdth.task_type) task_type, --12693583
19081 					'N' is_modified,
19082 					decode(wdth.is_parent,
19083 						   'Y',
19084 						   decode(wdth.transaction_action_id,
19085 								  28,
19086 								  l_plan_task_types(4),
19087 								  decode(wdth.transaction_source_type_id,
19088 										 5,
19089 										 decode(wdth.transaction_type_id,
19090 												35,
19091 												l_plan_task_types(4),
19092 												l_plan_task_types(3)),
19093 										 13,
19094 										 decode(wdth.transaction_type_id,
19095 												51,
19096 												l_plan_task_types(4),
19097 												l_plan_task_types(3)),
19098 										 l_plan_task_types(3))),
19099 						   decode(wdth.parent_transaction_id,
19100 								  null,
19101 								  l_plan_task_types(1),
19102 								  decode(wdth.transaction_action_id,
19103 										 28,
19104 										 l_plan_task_types(5),
19105 										 decode(wdth.transaction_source_type_id,
19106 												5,
19107 												decode(wdth.transaction_type_id,
19108 													   35,
19109 													   l_plan_task_types(5),
19110 													   l_plan_task_types(2)),
19111 												13,
19112 												decode(wdth.transaction_type_id,
19113 													   51,
19114 													   l_plan_task_types(5),
19115 													   l_plan_task_types(2)),
19116 												l_plan_task_types(2))))) plan_task, --12693583
19117 					' ' result,
19118 					' ' error,
19119 					WDD.SOURCE_HEADER_NUMBER source_header,
19120 					WDD.SOURCE_LINE_NUMBER line_number,
19121 					MSIV.concatenated_segments, --bug12642410
19122 					MSIV.DESCRIPTION,
19123 					wdth.REVISION,
19124 					wdth.source_subinventory_code,
19125 			(mil.concatenated_segments) locator, --bug12642410
19126 					wdth.SECONDARY_TRANSACTION_QUANTITY,
19127 					wdth.SECONDARY_TRANSACTION_UOM_CODE,
19128 					WDTH.OPERATION_PLAN_ID,
19129 					WOPV.OPERATION_PLAN_NAME,
19130 					NULL operation_sequence,
19131 					WDTH.OP_PLAN_INSTANCE_ID,
19132 					WDD.SHIP_METHOD_CODE SHIP_METHOD,
19133 					WDD.DATE_SCHEDULED shipment_date,
19134 					WDD.SHIPMENT_PRIORITY_CODE shipment_priority,
19135 					  to_char(null),
19136 					DECODE(wdth.PARENT_TRANSACTION_ID, NULL, 'N', 'Y') CHILD_TASKS,
19137 					0 num_of_child_tasks,
19138 					' ' ALLOCATED_LPN,
19139 					' ' CARTONIZED_LPN,
19140 					' ' CONTAINER_ITEM_ID,
19141 					(SELECT LICENSE_PLATE_NUMBER
19142 					   FROM WMS_LICENSE_PLATE_NUMBERS
19143 					  WHERE LPN_ID = wdth.CONTENT_LPN_ID) CONTENT_LPN,
19144 					wdth.DISPATCHED_TIME,
19145 					wdth.EQUIPMENT_ID,
19146 					(SELECT LICENSE_PLATE_NUMBER
19147 					   FROM WMS_LICENSE_PLATE_NUMBERS
19148 					  WHERE LPN_ID = wdth.LPN_ID) FROM_LPN,
19149 					wdth.parent_transaction_id,
19150 					wdth.move_order_line_id,
19151 					to_number(null),
19152 					(SELECT LICENSE_PLATE_NUMBER
19153 					   FROM WMS_LICENSE_PLATE_NUMBERS
19154 					  WHERE LPN_ID = wdth.CONTENT_LPN_ID) PICKED_LPN,
19155 					wdth.transaction_uom_code,
19156 					abs(wdth.transaction_quantity)
19157 	  FROM
19158 		   WSH_DELIVERY_DETAILS         WDD,
19159 		   MTL_ITEM_LOCATIONS_kfv           mil, --bug12642410
19160 		   mtl_system_items_kfv         msiv,
19161 		   wms_op_plans_vl              wopv,
19162 		   per_all_people_f             pap,
19163 		   wms_op_operation_instances   wooi,
19164 		   WMS_WP_WAVE_LINES            WWL,
19165 		   wms_dispatched_tasks_history wdth,
19166 		   bom_standard_operations      bso,
19167 		   bom_resources                br1,
19168 		   mtl_txn_request_lines mtrl,
19169 		   mtl_txn_request_headers mtrh
19170 	 where 1=1
19171 	   AND wdth.person_id = pap.person_id(+)
19172 	   AND wdth.effective_start_date >= pap.effective_start_date(+)
19173 	   AND wdth.effective_end_date <= pap.effective_end_date(+)
19174 	   AND wdth.organization_id = msiv.organization_id
19175 	   AND wdth.inventory_item_id = msiv.inventory_item_id
19176 	   AND wdth.organization_id = mil.organization_id(+)
19177 	   AND wdth.source_locator_id = mil.inventory_location_id(+)
19178 	   AND wdth.organization_id = mtrl.organization_id
19179 	   AND wdth.inventory_item_id = mtrl.inventory_item_id
19180 	  AND wdth.move_order_line_id = mtrl.line_id
19181 	and wdth.is_parent = 'N'
19182 	AND mtrl.BACKORDER_DELIVERY_DETAIL_ID =  wdd.delivery_detail_id
19183 	and mtrh.header_id=mtrl.header_id
19184 	   AND wdth.transaction_temp_id = wooi.source_task_id(+)
19185 	   AND wdth.operation_plan_id = wopv.operation_plan_id(+)
19186 	   AND WWL.delivery_detail_id = WDD.delivery_detail_id
19187 	   and wwl.organization_id = wdd.organization_id
19188 	   and wwl.wave_header_id = p_wave_header_id
19189 		and nvl(wwl.remove_from_wave_flag,'N') <> 'Y'
19190 	   AND wdth.user_task_type = bso.standard_operation_id(+)
19191 	   AND wdth.organization_id = bso.organization_id(+)
19192 	   AND wdth.person_resource_id = br1.resource_id(+)
19193 		 and wdth.task_type IN (2)
19194 				  and (
19195 				  (wdth.transaction_source_type_id = 1
19196 				  and wdth.transaction_action_id = 27) OR
19197 				  (wdth.transaction_source_type_id = 12
19198 				  and wdth.transaction_action_id = 27) OR
19199 				  (wdth.transaction_source_type_id = 7
19200 				  and wdth.transaction_action_id = 12) OR
19201 				  (wdth.transaction_source_type_id = 13
19202 				  and wdth.transaction_action_id = 12) OR
19203 				  (wdth.transaction_source_type_id = 4
19204 				  and wdth.transaction_action_id = 2) OR
19205 			  (wdth.transaction_source_type_id = 4
19206 				  and wdth.transaction_action_id = 27)
19207 				 );
19208 
19209 	WMS_WAVE_PLANNING_PVT.set_num_of_child_tasks;
19210 
19211 	insert into wms_wp_exceptions_gtmp_v(
19212 	EXCEPTION_NAME,
19213 	EXCEPTION_ID,
19214 	LOGGING_ENTITY,
19215 	ENTITY_VALUE,
19216 	SEVERITY_LEVEL,
19217 	EXCEPTION_MESSAGE,
19218 	CREATION_DATE,
19219 	STATUS,
19220 	READY_TO_RELEASE,
19221 	BACKORDERED,
19222 	CROSSDOCK_PLANNED,
19223 	REPLENISHMENT_PLANNED,
19224 	TASKED,
19225 	PICKED,
19226 	PACKED,
19227 	STAGED,
19228 	LOADED_TO_DOCK,
19229 	SHIPPED)
19230 	select exception_name,exception_id,exception_entity,
19231 	Decode(exception_entity,'Order Line',order_line_id,'Wave',wave_header_id,'Trip',trip_id,'Delivery',delivery_id,'Order', order_number) entity_value, -- to show entity
19232 	exception_level,
19233 	exception_msg,creation_date,
19234 				 status,
19235 				 ready_to_release,
19236 				 backordered,
19237 				 crossdock_planned,
19238 				 replenishment_planned,
19239 				 tasked,
19240 				 picked,
19241 				 packed,
19242 				 staged,
19243 				 loaded_to_dock,
19244 				 shipped
19245 	from wms_wp_wave_exceptions_vl where wave_header_id = p_wave_header_id
19246 	and lower(status) <> 'closed';
19247 
19248 	insert into wms_wp_labor_gtmp_v(
19249 	RESOURCE_NAME,
19250 	NUMBER_OF_ACTUAL_TASKS,
19251 	NUMBER_OF_PLANNED_TASKS,
19252 	ACTUAL_WORKLOAD,
19253 	PLANNED_WAVE_LOAD,
19254 	AVAILABLE_CAPACITY,
19255 	TOTAL_CAPACITY,
19256 	TIME_UOM)
19257 	select     wls.RESOURCE_NAME,
19258 	wls.number_of_actual_tasks,
19259 	wls.number_of_planned_tasks,
19260 	wls.actual_workload,
19261 	wls.planned_wave_load,
19262 	wls.available_capacity,
19263 	wls.total_capacity,
19264 	wpl.time_uom
19265 	from wms_wp_labor_statistics wls,
19266 	wms_wp_planning_criteria_vl wpl,
19267 	wms_wp_wave_headers_vl wwl
19268 	WHERE wwl.wave_header_id = p_wave_header_id
19269 	and wls.wave_header_id =  wwl.wave_header_id
19270 	AND wpl.planning_criteria_id = wwl.planning_criteria_id;
19271 
19272  END insert_global_temp;
19273 
19274  --update all wave lines matching this delivery detial with correct pick fill rate
19275  --bug 11775489  added
19276 procedure update_line_pick_fill_rate(p_wave_line_id in number)
19277 is
19278   l_pick_fill_rate number;
19279   l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
19280 begin
19281 	  print_debug('Calculating Pick Fill Rate for wave line : ' || p_wave_line_id,
19282                   l_debug);
19283 	select
19284 	   Decode(wdd.released_status,
19285                     'Y',
19286                     (((get_conversion_rate(wdd.inventory_item_id,
19287                                            wwtv.transaction_uom,
19288                                            wdd.requested_quantity_uom) *
19289                     wwtv.transaction_quantity) / wdd.requested_quantity) * 100),
19290                     'B',
19291                     0,
19292                     'E',
19293                     0)
19294 	into l_pick_fill_rate
19295 	from wms_wp_wave_lines wwl,
19296 	     wsh_delivery_details wdd,
19297 		 wms_wp_wwb_tasks_v wwtv,
19298 		 wms_wp_wave_headers_vl wwh
19299 	where wwl.wave_line_id = p_wave_line_id
19300 	  and wwl.delivery_detail_id = wdd.delivery_detail_id
19301 	  and wwl.organization_id = wdd.organization_id
19302 	  and wwtv.transaction_source_line_id(+) = wdd.source_line_id
19303 	  and wwh.wave_header_id = wwl.wave_header_id
19304 	  and (wwh.wave_status = 'Released')
19305       and nvl(wwl.remove_from_wave_flag, 'N') = 'N'
19306       and wdd.released_status in ('B', 'E', 'Y');
19307 
19308 
19309     update wms_wp_wave_lines
19310 	set pick_fill_rate = l_pick_fill_rate
19311 	where wave_line_id = p_wave_line_id;
19312 
19313 exception
19314   when others then
19315           print_debug('Error in update_line_pick_fill_rate for line id '||p_wave_line_id||' : ' || SQLCODE || ' : ' || SQLERRM, l_debug);
19316 end update_line_pick_fill_rate;
19317 
19318 --bug 11775489 added
19319 procedure update_line_actual_fill_rate(p_wave_line_id in number)
19320 is
19321   l_crossdocked varchar2(3):='N';
19322   l_allocated_quantity number;
19323   l_fill_rate number;
19324   l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
19325 begin
19326 
19327   --line is in crossdocked status if wdd.released status is 'K' or wdd.move_order_line_id is null
19328   begin
19329     select 'Y' into l_crossdocked from dual where exists
19330     (select 1 from wsh_delivery_details wdd, wms_wp_wave_lines wwl
19331     where wwl.wave_line_id = p_wave_line_id
19332       and wwl.delivery_detail_id = wdd.delivery_detail_id
19333 	  and wwl.organization_id = wdd.organization_id
19334 	  and (wdd.released_status = 'K' or (wdd.move_order_line_id  is null and wdd.released_status = 'S')));
19335   exception
19336     when no_data_found then
19337 	  l_crossdocked:='N';
19338   end;
19339 
19340 
19341   --if crossdocked status then set fill rate to be 100, otherwise calculate...
19342   if l_crossdocked = 'Y' then
19343     print_debug(' Line is in Crossdocked Status. In that case we mark the Release Fill Rate for that Line as 100%',
19344                     l_debug);
19345 	l_fill_rate := 100;
19346 
19347   else
19348     select sum((get_conversion_rate(wdd.inventory_item_id,
19349                                              mmtt.transaction_uom,
19350                                              wdd.requested_quantity_uom) *
19351                         Nvl(MMTT.TRANSACTION_QUANTITY, 0)))
19352 	into l_allocated_quantity
19353 	from wsh_delivery_details wdd,
19354 	     mtl_material_transactions_temp mmtt,
19355 	     wms_wp_wave_lines wwl
19356     where wdd.move_order_line_id = mmtt.move_order_line_id
19357 	  and wdd.delivery_detail_id = wwl.delivery_detail_id
19358 	  and wdd.organization_id = wwl.organization_id
19359 	  and wwl.wave_line_id = p_wave_line_id;
19360 
19361 	select round(decode(wdd.released_status, 'B', 0, l_allocated_quantity)*100/wdd.requested_quantity)
19362 	into l_fill_rate
19363 	from wsh_delivery_details wdd,
19364 	     wms_wp_wave_lines wwl
19365     where wwl.wave_line_id = p_wave_line_id
19366 	  and wwl.organization_id = wdd.organization_id
19367 	  and wwl.delivery_detail_id = wdd.delivery_detail_id;
19368   end if;
19369 
19370   update wms_wp_wave_lines
19371   set release_fill_rate = l_fill_rate
19372   where wave_line_id = p_wave_line_id;
19373 
19374 
19375 exception
19376   when others then
19377           print_debug('Error in update_line_actual_fill_rate for line_id '||p_wave_line_id||' : ' || SQLCODE || ' : ' || SQLERRM, l_debug);
19378 end update_line_actual_fill_rate;
19379 
19380 procedure launch_wave_endeca(P_WAVE_NAME                  IN VARCHAR2,
19381                              P_WAVE_DESCRIPTION           IN VARCHAR2,
19382                              P_start_time                 IN DATE, -- start time changes
19383                              P_SHIP_TO_LOCATION_ID        IN NUMBER,
19384                              P_CUSTOMER_CLASS_ID          IN VARCHAR2,
19385                              P_pull_replenishment_flag    IN VARCHAR2,
19386                              P_RELEASE_IMMEDIATELY        IN VARCHAR2,
19387                              P_ORGANIZATION_ID            IN NUMBER,
19388                              P_PICK_SEQ_RULE_ID           IN NUMBER,
19389                              P_PICK_GROUPING_RULE_ID      IN NUMBER,
19390                              P_TRIP_ID                    IN NUMBER,
19391                              P_TRIP_STOP_ID               IN NUMBER,
19392                              P_SHIP_METHOD_CODE           IN VARCHAR2,
19393                              P_SHIPMENT_PRIORITY_CODE     IN VARCHAR2,
19394                              P_CARRIER_ID                 IN NUMBER,
19395                              P_DELIVERY_ID                IN NUMBER,
19396                              P_FROM_ORDER_HEADER_ID       IN NUMBER,
19397                              P_ORDER_TYPE_ID              IN NUMBER,
19398                              P_CUSTOMER_ID                IN NUMBER,
19399                              P_CATEGORY_SET_ID            IN NUMBER,
19400                              P_CATEGORY_ID                IN NUMBER,
19401                              P_INVENTORY_ITEM_ID          IN NUMBER,
19402                              P_INCLUDE_PLANNED_LINES      IN VARCHAR2,
19403                              P_TASK_PLANNING_FLAG         IN VARCHAR2,
19404                              P_APPEND_DELIVERIES          IN VARCHAR2,
19405                              P_AUTO_CREATE_DELIVERY       IN VARCHAR2,
19406                              P_AUTODELIVERY_CRITERIA      IN VARCHAR2,
19407                              P_TASK_PRIORITY              IN NUMBER,
19408                              P_DEFAULT_STAGE_SUBINVENTORY IN VARCHAR2,
19409                              P_DEFAULT_STAGE_LOCATOR_ID   IN NUMBER,
19410                              P_DEFAULT_ALLOCATION_METHOD  IN VARCHAR2,
19411                              P_WAVE_FIRMED_FLAG           IN VARCHAR2,
19412                              P_ORDER_NAME                 IN VARCHAR2,
19413                              P_CUSTOMER                   IN VARCHAR2,
19414                              P_ORDER_TYPE                 IN VARCHAR2,
19415                              P_CUSTOMER_CLASS             IN VARCHAR2,
19416                              P_SHIP_METHOD                IN VARCHAR2,
19417                              P_CARRIER                    IN VARCHAR2,
19418                              P_SHIP_PRIORITY              IN VARCHAR2,
19419                              P_DELIVERY                   IN VARCHAR2,
19420                              P_TRIP                       IN VARCHAR2,
19421                              P_TRIP_STOP                  IN VARCHAR2,
19422                              P_ITEM                       IN VARCHAR2,
19423                              P_ITEM_CATEGORY              IN VARCHAR2,
19424                              P_SCHEDULED_DAYS             IN NUMBER,
19425                              P_SCHEDULED_HRS              IN NUMBER,
19426                              P_DOCK_APPOINTMENT_DAYS      IN NUMBER,
19427                              P_DOCK_APPOINTMENT_HOURS     IN NUMBER,
19428                              P_PICK_SLIP_GROUP            IN VARCHAR2,
19429                              P_RELEASE_SEQ_RULE           IN VARCHAR2,
19430                              P_STAGING_SUBINVENTORY       IN VARCHAR2,
19431                              P_STAGING_LOCATOR            IN VARCHAR2,
19432                              P_CROSS_DOCK_CRITERIA        IN VARCHAR2,
19433                              P_pick_subinventory          IN VARCHAR2,
19434                              x_return_status              OUT NOCOPY VARCHAR2,
19435                              x_wave_header_id             OUT NOCOPY NUMBER)
19436 
19437  IS
19438 
19439   l_debug          NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
19440   l_wave_header_id number;
19441   l_request_id     number;
19442 BEGIN
19443   select WMS_WP_WAVE_HEADERS_S.NEXTVAL into l_wave_header_id from dual;
19444 
19445   print_debug('Enter  LAUNCH_WAVE_ENDECA ' || ' : ' || l_wave_header_id,
19446               l_debug);
19447 
19448   x_return_status  := 'S';
19449   x_wave_header_id := l_wave_header_id;
19450   INSERT INTO wms_wp_wave_headers_vl
19451     (wave_header_id,
19452      WAVE_NAME,
19453      WAVE_DESCRIPTION,
19454      start_time, -- start time changes
19455      WAVE_SOURCE,
19456      WAVE_STATUS,
19457      TYPE_ID,
19458      BATCH_ID,
19459      SHIP_TO_LOCATION_ID,
19460      CUSTOMER_CLASS_ID,
19461      pull_replenishment_flag,
19462      INITIATE_WAVE_PLANNING,
19463      RELEASE_IMMEDIATELY,
19464      TABLE_NAME,
19465      ADVANCED_CRITERIA,
19466      CREATED_BY,
19467      CREATION_DATE,
19468      LAST_UPDATED_BY,
19469      LAST_UPDATE_DATE,
19470      LAST_UPDATE_LOGIN,
19471      ORGANIZATION_ID,
19472      PICK_SEQ_RULE_ID,
19473      PICK_GROUPING_RULE_ID,
19474      TRIP_ID,
19475      TRIP_STOP_ID,
19476      SHIP_METHOD_CODE,
19477      SHIPMENT_PRIORITY_CODE,
19478      CARRIER_ID,
19479      DELIVERY_ID,
19480      FROM_ORDER_HEADER_ID,
19481      ORDER_TYPE_ID,
19482      CUSTOMER_ID,
19483      TASK_ID,
19484      PROJECT_ID,
19485      CATEGORY_SET_ID,
19486      CATEGORY_ID,
19487      INVENTORY_ITEM_ID,
19488      BACKORDERS_FLAG,
19489      INCLUDE_PLANNED_LINES,
19490      TASK_PLANNING_FLAG,
19491      APPEND_DELIVERIES,
19492      AUTO_CREATE_DELIVERY,
19493      AUTO_CREATE_DELIVERY_CRITERIA,
19494      TASK_PRIORITY,
19495      DEFAULT_STAGE_SUBINVENTORY,
19496      DEFAULT_STAGE_LOCATOR_ID,
19497      DEFAULT_ALLOCATION_METHOD,
19498      WAVE_FIRMED_FLAG,
19499      WAVE_COMPLETION_TIME,
19500      ORDER_NAME,
19501      CUSTOMER,
19502      ORDER_TYPE,
19503      CUSTOMER_CLASS,
19504      SHIP_METHOD,
19505      CARRIER,
19506      SHIP_PRIORITY,
19507      DELIVERY,
19508      TRIP,
19509      TRIP_STOP,
19510      ITEM,
19511      ITEM_CATEGORY,
19512      PROJECT_NAME,
19513      TASK_NAME,
19514      SCHEDULED_DAYS,
19515      SCHEDULED_HRS,
19516      DOCK_APPOINTMENT_DAYS,
19517      DOCK_APPOINTMENT_HOURS,
19518      PICK_SLIP_GROUP,
19519      RELEASE_SEQ_RULE,
19520      STAGING_SUBINVENTORY,
19521      STAGING_LOCATOR,
19522      CROSS_DOCK_CRITERIA,
19523      PLANNING_CRITERIA,
19524      PLANNING_CRITERIA_ID,
19525      pick_subinventory)
19526   VALUES
19527     (l_wave_header_id,
19528      P_WAVE_NAME || '-' || l_wave_header_id,
19529      P_WAVE_DESCRIPTION,
19530      P_start_time, -- start time changes
19531      'OE',
19532      'Created', --Need to Confirm??????
19533      'W',
19534      null,
19535      P_SHIP_TO_LOCATION_ID,
19536      P_CUSTOMER_CLASS_ID,
19537      P_pull_replenishment_flag,
19538      'N',
19539      P_RELEASE_IMMEDIATELY,
19540      null,
19541      null,
19542      fnd_global.user_id,
19543      sysdate,
19544      fnd_global.user_id,
19545      sysdate,
19546      fnd_global.login_id,
19547      P_ORGANIZATION_ID,
19548      P_PICK_SEQ_RULE_ID,
19549      P_PICK_GROUPING_RULE_ID,
19550      P_TRIP_ID,
19551      P_TRIP_STOP_ID,
19552      P_SHIP_METHOD_CODE,
19553      P_SHIPMENT_PRIORITY_CODE,
19554      P_CARRIER_ID,
19555      P_DELIVERY_ID,
19556      P_FROM_ORDER_HEADER_ID,
19557      P_ORDER_TYPE_ID,
19558      P_CUSTOMER_ID,
19559      null,
19560      null,
19561      P_CATEGORY_SET_ID,
19562      P_CATEGORY_ID,
19563      P_INVENTORY_ITEM_ID,
19564      null,
19565      P_INCLUDE_PLANNED_LINES,
19566      P_TASK_PLANNING_FLAG,
19567      P_APPEND_DELIVERIES,
19568      P_AUTO_CREATE_DELIVERY,
19569      P_AUTODELIVERY_CRITERIA,
19570      P_TASK_PRIORITY,
19571      P_DEFAULT_STAGE_SUBINVENTORY,
19572      P_DEFAULT_STAGE_LOCATOR_ID,
19573      P_DEFAULT_ALLOCATION_METHOD,
19574      P_WAVE_FIRMED_FLAG,
19575      null,
19576      P_ORDER_NAME,
19577      P_CUSTOMER,
19578      P_ORDER_TYPE,
19579      P_CUSTOMER_CLASS,
19580      P_SHIP_METHOD,
19581      P_CARRIER,
19582      P_SHIP_PRIORITY,
19583      P_DELIVERY,
19584      P_TRIP,
19585      P_TRIP_STOP,
19586      P_ITEM,
19587      P_ITEM_CATEGORY,
19588      null,
19589      null,
19590      P_SCHEDULED_DAYS,
19591      P_SCHEDULED_HRS,
19592      P_DOCK_APPOINTMENT_DAYS,
19593      P_DOCK_APPOINTMENT_HOURS,
19594      P_PICK_SLIP_GROUP,
19595      P_RELEASE_SEQ_RULE,
19596      P_STAGING_SUBINVENTORY,
19597      P_STAGING_LOCATOR,
19598      P_CROSS_DOCK_CRITERIA,
19599      null,
19600      null,
19601      P_pick_subinventory);
19602 
19603   print_debug('Inserted Wave Record  LAUNCH_WAVE_ENDECA ' || ' : ' ||
19604               SQL%ROWCOUNT,
19605               l_debug);
19606 
19607   set_global_variable(p_order_type_id,
19608                       P_FROM_ORDER_HEADER_ID,
19609                       null,
19610                       nvl(p_include_planned_lines, 'Y'),
19611                       p_customer_id,
19612                       p_inventory_item_id,
19613                       p_shipment_priority_code,
19614                       p_ship_method_code,
19615                       P_SHIP_TO_LOCATION_ID,
19616                       null,
19617                       null,
19618                       p_delivery_id,
19619                       p_trip_id,
19620                       p_trip_stop_id,
19621                       p_pick_seq_rule_id,
19622                       p_pick_grouping_rule_id,
19623                       p_scheduled_days,
19624                       p_scheduled_hrs,
19625                       P_DOCK_APPOINTMENT_DAYS,
19626                       P_DOCK_APPOINTMENT_HOURS,
19627                       p_customer_class_id,
19628                       p_carrier_id,
19629                       p_category_id);
19630 
19631   print_debug('After Setting Global Variables  ', l_debug);
19632 
19633   COMMIT;
19634 
19635   launch_online(x_return_status     => x_return_status,
19636                 p_wave_header_id    => l_wave_header_id,
19637                 v_orgid             => p_organization_id,
19638                 p_release_immediate => P_RELEASE_IMMEDIATELY,
19639                 p_plan_wave         => 'N',
19640                 p_request_id        => l_request_id,
19641                 p_add_lines         => 'N');
19642 
19643   x_return_status := 'S';
19644 
19645 EXCEPTION
19646 
19647   WHEN OTHERS THEN
19648     print_debug('Error in LAUNCH_WAVE_ENDECA ' || ' : ' || SQLCODE ||
19649                 ' : ' || SQLERRM,
19650                 l_debug);
19651     x_return_status := 'E';
19652 
19653 END LAUNCH_WAVE_ENDECA;
19654 
19655 PROCEDURE release_wave_endeca(p_wave_header_id IN NUMBER,
19656                               x_request_id     OUT NOCOPY NUMBER,
19657                               x_return_status  OUT NOCOPY VARCHAR2,
19658                               x_message        OUT NOCOPY VARCHAR2)
19659 
19660  IS
19661 
19662 BEGIN
19663 
19664   update wms_wp_wave_headers_vl
19665      set release_immediately = 'Y'
19666    WHERE WAVE_HEADER_ID = P_WAVE_HEADER_ID;
19667 
19668   /*UPDATE wms_wp_wave_lines
19669   SET message = 'This line has been Firmed in Wave '||p_wave_header_id,
19670   remove_from_Wave_flag = 'Y'
19671   WHERE delivery_detail_id IN
19672   (SELECT delivery_detail_id FROM wms_wp_wave_lines WHERE wave_header_id = p_wave_header_id
19673   and nvl(remove_from_wave_flag,'N')<>'Y')
19674   and wave_header_id != p_wave_header_id;
19675 
19676   update wms_wp_wave_headers_vl
19677   set wave_firmed_flag = 'Y'
19678   where wave_header_id = p_wave_header_id;
19679                                                */
19680   commit;
19681 
19682   x_request_id := FND_REQUEST.SUBMIT_REQUEST(application => 'WMS',
19683                                              program     => 'RELEASE_BATCH_CP',
19684                                              description => 'Wave Planning Release Wave',
19685                                              start_time  => NULL,
19686                                              argument1   => p_wave_header_id);
19687 
19688   commit;
19689 
19690   if (x_request_id > 0) THEN
19691 
19692     x_message       := 'WMS_WP_RELEASE_PROCESS';
19693     x_return_status := 'S';
19694 
19695   ELSE
19696 
19697     x_message       := 'WSH_PR_REVOKED';
19698     x_return_status := 'E';
19699 
19700   end if;
19701 
19702 EXCEPTION
19703 
19704   WHEN OTHERS THEN
19705     x_return_status := 'E';
19706     x_message       := 'Error in Release Wave Endeca';
19707     x_request_id    := 0;
19708 
19709 END RELEASE_WAVE_ENDECA;
19710 
19711 PROCEDURE remove_lines_endeca(p_wave_header_id IN NUMBER,
19712                               p_order_number   IN VARCHAR2,
19713                               p_line_number    IN VARCHAR2,
19714                               x_return_status  OUT NOCOPY VARCHAR2) IS
19715 
19716 BEGIN
19717 
19718   delete from wms_wp_wave_lines
19719    where wave_line_id in
19720          (select wave_line_id
19721             from wms_wp_wave_lines wwl, wsh_delivery_details wdd
19722            where wwl.wave_header_id = p_wave_header_id
19723              and wdd.delivery_detail_id = wwl.delivery_Detail_id
19724              and wdd.organization_id = wwl.organization_id
19725              and wdd.source_line_number = p_line_number
19726              and wdd.source_header_number = p_order_number);
19727 
19728   COMMIT;
19729 
19730   x_return_status := 'S';
19731 
19732 EXCEPTION
19733 
19734   WHEN OTHERS THEN
19735     x_return_status := 'E';
19736 
19737 END remove_lines_endeca;
19738 
19739 end WMS_WAVE_PLANNING_PVT;
19740