DBA Data[Home] [Help]

PACKAGE BODY: APPS.MSC_REL_PLAN_PUB

Source


1 PACKAGE BODY MSC_REL_PLAN_PUB AS
2 /* $Header: MSCPRELB.pls 120.89.12020000.7 2012/11/29 05:21:25 qiangwan ship $ */
3 
4 --  Start of Comments
5 --  API name 	MSC_Release_Plan_SC
6 --  Type 	Public
7 --  Procedure
8 --
9 --  Pre-reqs
10 --
11 --  Parameters
12 --
13 --  Version 	Current version = 1.0
14 --  		Initial version = 1.0
15 --
16 --  Notes
17 --
18 --     OVERVIEW:
19 --     This procedure populates the WIP and purchasing interface tables with
20 --     rows for creating and rescheduling jobs, purchase orders, and repetitive
21 --     schedules
22 --
23 --     ARGUMENTS:
24 --     arg_plan_id:	   The plan identifier
25 --     arg_org_id:         The current organization id
26 --     arg_sr_instance     The source instance id of the org
27 --     arg_compile_desig:  The current plan name
28 --     arg_user_id:        The user
29 --     arg_po_group_by:    How to group attributes together for po mass load
30 --     arg_wip_group_id:   How to group records in wip
31 --     lv_launch_process: Which process to launch
32 --     lv_calendar_code:    Calendar code for current organization
33 --     lv_exception_set_id: Exception set id for current organization
34 --
35 --     RETURNS:            Nothing
36 --
37 
38 
39    G_INS_DISCRETE               CONSTANT NUMBER := 1;
40    G_INS_PROCESS                CONSTANT NUMBER := 2;
41    G_INS_OTHER                  CONSTANT NUMBER := 3;
42    G_INS_MIXED                  CONSTANT NUMBER := 4;
43    G_INS_EXCHANGE               CONSTANT NUMBER := 5;
44 
45    gv_auto_release              NUMBER;
46    v_curr_instance_type         NUMBER;
47    v_msc_released_only_by_user  NUMBER;
48    v_batch_id_populated         NUMBER := 1; --1 - populated ; 2 - not populated in msc_supplies
49    v_plan_type          NUMBER;      -- For RP Release
50    v_rp_plan            NUMBER := 0 ;
51    v_user_id			NUMBER;
52    v_time_stamp         NUMBER := 0;
53    v_unconstrained_plan     number;
54    v_rp_time            NUMBER := 0; -- For RP Release
55 
56    JOB_CANCELLED          CONSTANT INTEGER := 7;
57 
58    NULL_DBLINK                  CONSTANT VARCHAR2(1):= ' ';
59 
60    LT_RESOURCE_INSTANCE CONSTANT NUMBER := 8;  -- dsr
61  	-- SUBST_CHANGE CONSTANT NUMBER := 3; -- dsr
62  	LT_RESOURCE_INST_USAGE CONSTANT NUMBER := 9; -- dsr
63  	LT_CHARGE_TYPE CONSTANT NUMBER := 10; -- dsr
64  	EAM_RESCHEDULE_WORK_ORDER CONSTANT NUMBER := 21; -- dsr
65  	EAM CONSTANT NUMBER :=1;
66  	CMRO CONSTANT NUMBER :=2;
67  	WORK_ORDER_SUB_COMPONENT CONSTANT NUMBER := 206;
68    l_sql_stmt          VARCHAR2(20000);
69 /* DWK */ -- Function added for DRP Release
70 FUNCTION Decode_Sales_Order_Number(p_order_number_string in VARCHAR2)
71                                    return NUMBER IS
72 
73 p_order_number  NUMBER := 0;
74 l_end           number;
75 l_counter       number;
76 l_max_counter   number;
77 l_char          varchar2(1);
78 l_num_char      number;
79 
80 BEGIN
81 
82     BEGIN
83         p_order_number := to_number (p_order_number_string);
84         return p_order_number;
85     EXCEPTION
86         WHEN others then
87            null;
88     END;
89 
90     p_order_number:= null;
91     l_end := 0;
92     l_counter := 0;
93     l_max_counter := length(p_order_number_string);
94 
95     if (l_max_counter = 0) then
96         return null;
97     end if;
98 
99     while (l_end <> 1) loop
100         l_counter := l_counter + 1;
101         if (l_counter > l_max_counter ) then
102             l_end := 1;
103             exit;
104         end if;
105         l_char := substr (p_order_number_string, l_counter, 1);
106         BEGIN
107             l_num_char := to_number (l_char);
108         EXCEPTION
109             WHEN OTHERS then
110                 l_end := 1;
111         END;
112     end loop;
113 
114     if (l_counter > l_max_counter) then
115         BEGIN
116            p_order_number := to_number (p_order_number_string);
117         EXCEPTION
118             WHEN others then
119                 return null;
120         END;
121     elsif (l_counter = 1) then
122         return null;
123     else
124         BEGIN
125             p_order_number := to_number (substr (p_order_number_string, 1,l_counter -1));
126         EXCEPTION
127             WHEN others then
128                 return null;
129         END;
130     end if;
131 
132     return p_order_number;
133 
134 EXCEPTION
135     WHEN others THEN
136 
137         return null;
138 END;
139 
140    -- This procesure prints out debug information
141    --commenting out the procedure bug 8761596
142   /* PROCEDURE print_debug_info(
143      p_debug_info IN VARCHAR2
144    )IS
145    BEGIN
146         FND_FILE.PUT_LINE(FND_FILE.LOG, p_debug_info);
147        --dbms_output.put_line(p_debug_info);
148    EXCEPTION
149    WHEN OTHERS THEN
150       RAISE;
151    END print_debug_info;*/
152 
153 
154 
155 FUNCTION GET_CAL_DATE ( lv_inst_id in number
156                         ,lv_calendar_date in date
157                         ,lv_calendar_code in varchar2) return date
158 is
159 lv_date date;
160 BEGIN
161     SELECT  cal4.calendar_date
162     into    lv_date
163     FROM    msc_calendar_dates cal3,
164             msc_calendar_dates cal4
165     WHERE   cal3.sr_instance_id = lv_inst_id
166     AND     cal3.exception_set_id = -1
167     AND     cal3.calendar_date = lv_calendar_date
168     AND     cal3.calendar_code  = lv_calendar_code
169     AND     cal4.sr_instance_id = cal3.sr_instance_id
170     AND     cal4.calendar_code = cal3.calendar_code
171     and     cal4.exception_set_id = cal3.exception_set_id
172     and     cal4.seq_num = nvl(cal3.seq_num,cal3.prior_seq_num);
173 
174     return lv_date;
175 
176     EXCEPTION
177        when no_data_found then
178        return null;
179        when others  then
180        return null;
181 
182 END GET_CAL_DATE;
183 FUNCTION GET_COPRODUCT_QTY ( inst_id in number
184                             , pln_id in number
185                             ,disp_id in number
186                             ,bill_seq_id in number) return number
187 is
188 lv_total number;
189 BEGIN
190 
191     select sum(coprod.new_order_quantity)
192     into lv_total
193     from  msc_supplies coprod, msc_bom_components mbc
194     where coprod.sr_instance_id = inst_id
195     and   coprod.plan_id  = pln_id
196     and   mbc.sr_instance_id = coprod.sr_instance_id
197     and   mbc.plan_id = coprod.plan_id
198     and   mbc.inventory_item_id = coprod.inventory_item_id
199     and   mbc.organization_id = coprod.organization_id
200     and   mbc.bill_sequence_id = bill_seq_id
201     and   NOT (mbc.usage_quantity < 0 and mbc.component_type <> 10)
202     and   coprod.order_type = 17
203     and   coprod.disposition_id = disp_id;
204 
205     select nvl(lv_total,0) into   lv_total from dual;
206 
207     return lv_total;
208 
209     EXCEPTION
210        when no_data_found then lv_total := 0;
211        return lv_total;
212        when others then lv_total := 0;
213        return lv_total;
214 
215 END GET_COPRODUCT_QTY;
216 
217 FUNCTION GET_REV_CUM_YIELD ( inst_id in number
218                             , pln_id in number
219                             ,process_seq_id in number
220                             ,trans_id in number
221                             ,org_id in number) return number
222 is
223 lv_yield number;
224 BEGIN
225 
226     select nvl(mrr.REVERSE_CUMULATIVE_YIELD,1)
227     into lv_yield
228     from  msc_process_effectivity mpe,
229           msc_routings mr,
230           msc_resource_requirements mrr
231     where mpe.sr_instance_id = inst_id
232     and   mpe.plan_id  = pln_id
233     and   mpe.process_sequence_id = process_seq_id
234     and   mr.routing_sequence_id = mpe.routing_sequence_id
235     and   mr.plan_id = mpe.plan_id
236     and   mr.sr_instance_id = mpe.sr_instance_id
237     and   mrr.plan_id = mr.plan_id
238     and   mrr.routing_sequence_id = mr.routing_sequence_id
239     and   mrr.sr_instance_id = mr.sr_instance_id
240     and   mrr.OPERATION_SEQ_NUM = mr.FIRST_OP_SEQ_NUM
241     and   mrr.supply_id = trans_id
242     and   mrr.organization_id = org_id
243     and   mrr.rowid=(select min(rowid) from msc_resource_requirements
244                      where routing_sequence_id = mr.routing_sequence_id
245                      and   sr_instance_id = inst_id
246                      and   plan_id = pln_id
247                      and   supply_id = trans_id
248                      and   organization_id = org_id
249                      and   operation_seq_num = mr.first_op_seq_num
250                      and   parent_id = 2);
251 
252     return lv_yield;
253 
254 EXCEPTION
255        when no_data_found then lv_yield := 1;
256        return lv_yield;
257        when others then lv_yield := 1;
258        return lv_yield;
259 
260 END GET_REV_CUM_YIELD;
261 
262 FUNCTION GET_REV_CUM_YIELD_DISC( inst_id        IN NUMBER
263                                  ,pln_id         IN NUMBER
264                                  ,process_seq_id IN NUMBER
265                                  ,trans_id       IN NUMBER
266                                  ,org_id         IN NUMBER
267                                  ,org_type       IN NUMBER
268                                   )
269 RETURN number
270  IS
271  lv_yield_dis number;
272 
273  BEGIN
274 
275    lv_yield_dis := 0;
276 
277   /* For  bug: 2559388 - added this If cond to return RCY=1 (for OPM, there is no RCY concept ,so return 1
278     and for the OPM orgs organization_type = 2 in msc_trading_partners */
279 
280    IF (org_type = 2) THEN
281         lv_yield_dis := 1;
282         RETURN lv_yield_dis;
283    END IF;
284 
285      select nvl(mrr.REVERSE_CUMULATIVE_YIELD,1)
286      into lv_yield_dis
287      from  msc_process_effectivity mpe,
288            msc_routings mr,
289            msc_resource_requirements mrr
290      where mpe.sr_instance_id = inst_id
291      and   mpe.plan_id  = pln_id
292      and   mpe.process_sequence_id = process_seq_id
293      and   mr.routing_sequence_id = mpe.routing_sequence_id
294      and   mr.plan_id = mpe.plan_id
295      and   mr.sr_instance_id = mpe.sr_instance_id
296      and   mrr.plan_id = mr.plan_id
297      and   mrr.routing_sequence_id = mr.routing_sequence_id
298      and   mrr.sr_instance_id = mr.sr_instance_id
299      and   mrr.supply_id = trans_id
300      and   mrr.organization_id = org_id
301      and   mrr.OPERATION_SEQ_NUM = (select min(OPERATION_SEQ_NUM)
302                                from msc_resource_requirements
303                               where   routing_sequence_id = mr.routing_sequence_id
304                                 and   sr_instance_id = inst_id
305                                 and   plan_id = pln_id
306                                 and   supply_id = trans_id
307                                 and   organization_id = org_id
308                                 and   parent_id = 2)
309      and mrr.parent_id = 2
310      and rownum = 1;
311 
312    RETURN lv_yield_dis;
313 
314  EXCEPTION
315         WHEN OTHERS THEN
316             lv_yield_dis := 1;
317 
318    RETURN lv_yield_dis;
319 
320 END GET_REV_CUM_YIELD_DISC;
321 
322 
323 FUNCTION GET_REV_CUM_YIELD_DISC_COMP( inst_id         IN NUMBER
324                                       ,pln_id         IN NUMBER
325                                       ,process_seq_id IN NUMBER
326                                       ,trans_id       IN NUMBER
327                                       ,org_id         IN NUMBER
328                                       ,org_type       IN NUMBER
329                                       ,op_seq_num     IN NUMBER
330                                   )
331 RETURN number
332  IS
333  lv_yield_dis number;
334 
335  BEGIN
336 
337    lv_yield_dis := 0;
338 
339   /* For  bug: 2559388 - added this If cond to return RCY=1 (for OPM, there is no RCY concept ,so return 1
340     and for the OPM orgs organization_type = 2 in msc_trading_partners */
341 
342    IF (org_type = 2) THEN
343         lv_yield_dis := 1;
344         RETURN lv_yield_dis;
345    END IF;
346 
347      select nvl(mrr.REVERSE_CUMULATIVE_YIELD,1)
348      into lv_yield_dis
349      from  msc_process_effectivity mpe,
350            msc_routings mr,
351            msc_resource_requirements mrr
352      where mpe.sr_instance_id = inst_id
353      and   mpe.plan_id  = pln_id
354      and   mpe.process_sequence_id = process_seq_id
355      and   mr.routing_sequence_id = mpe.routing_sequence_id
356      and   mr.plan_id = mpe.plan_id
357      and   mr.sr_instance_id = mpe.sr_instance_id
358      and   mrr.plan_id = mr.plan_id
359      and   mrr.routing_sequence_id = mr.routing_sequence_id
360      and   mrr.sr_instance_id = mr.sr_instance_id
361      and   mrr.supply_id = trans_id
362      and   mrr.organization_id = org_id
363      and   mrr.OPERATION_SEQ_NUM = op_seq_num
364      and mrr.parent_id = 2
365      and rownum = 1;
366 
367    RETURN lv_yield_dis;
368 
369  EXCEPTION
370         WHEN OTHERS THEN
371             lv_yield_dis := 1;
372 
373    RETURN lv_yield_dis;
374 
375 END GET_REV_CUM_YIELD_DISC_COMP;
376 
377 FUNCTION GET_USAGE_QUANTITY ( p_plan_id     IN NUMBER
378                               ,p_inst_id     IN NUMBER
379                               ,p_org_id     IN NUMBER
380                               ,p_using_assy_id     IN NUMBER
381                               ,p_comp_seq_id  IN NUMBER) RETURN NUMBER
382 IS
383 lv_USAGE_QUANTITY NUMBER;
384 BEGIN
385 
386     SELECT mbc.USAGE_QUANTITY
387     INTO   lv_USAGE_QUANTITY
388     FROM   MSC_BOM_COMPONENTS mbc
389     WHERE mbc.plan_id = p_plan_id
390     AND  mbc.sr_instance_id = p_inst_id
391     AND mbc.organization_id = p_org_id
392     AND mbc.using_assembly_id = p_using_assy_id
393     AND mbc.component_sequence_id  =  p_comp_seq_id;
394 
395     RETURN lv_USAGE_QUANTITY;
396 
397 EXCEPTION
398        WHEN OTHERS THEN  return NULL;
399 
400 END GET_USAGE_QUANTITY;
401 
402 FUNCTION GET_WIP_SUPPLY_TYPE ( p_plan_id     IN NUMBER
403                               ,p_inst_id     IN NUMBER
404                               ,p_process_seq_id IN NUMBER
405                               ,p_item_id      IN NUMBER
406                               ,p_comp_item_id IN NUMBER
407                               ,p_org_id      IN NUMBER) RETURN NUMBER
408 IS
409 lv_wip_sup_type NUMBER;
410 BEGIN
411 
412     SELECT mbc.WIP_SUPPLY_TYPE
413     INTO   lv_wip_sup_type
414     FROM   MSC_BOM_COMPONENTS mbc,
415            msc_process_effectivity mpe
416    where mpe.sr_instance_id = p_inst_id
417     and   mpe.plan_id  = p_plan_id
418     and   mpe.process_sequence_id = p_process_seq_id
419     AND mpe.organization_id = p_org_id
420     AND mpe.item_id = p_item_id
421     and    mbc.plan_id =  mpe.plan_id
422     AND mbc.sr_instance_id = mpe.sr_instance_id
423     AND mbc.bill_sequence_id = mpe.bill_sequence_id
424     AND mbc.inventory_item_id =  p_comp_item_id
425     AND (mpe.disable_date is NULL OR
426             trunc(mpe.disable_date) >= trunc(SYSDATE) )
427     AND (mbc.disable_date is NULL OR
428             trunc(mbc.disable_date) >= trunc(SYSDATE) )
429     and rownum = 1;
430     RETURN lv_wip_sup_type;
431 
432 EXCEPTION
433        WHEN OTHERS THEN  return NULL;
434 
435 END GET_WIP_SUPPLY_TYPE;
436 
437 /* added these 2 functions for getting the sr_tp_id and tp_site_code
438   for releasing across the instances */
439 FUNCTION GET_MODELED_SR_TP_ID (pMODELED_SUPPLIER_ID    IN NUMBER,
440                                pSR_INSTANCE_ID         IN NUMBER)
441     RETURN NUMBER
442 IS
443 lv_mod_sup_sr_tp_id NUMBER;
444 BEGIN
445 
446     SELECT SR_TP_ID
447     INTO   lv_mod_sup_sr_tp_id
448     FROM   MSC_TP_ID_LID
449     WHERE  TP_ID = pMODELED_SUPPLIER_ID
450       AND  SR_INSTANCE_ID = pSR_INSTANCE_ID
451       AND  PARTNER_TYPE = 1;
452 
453     RETURN lv_mod_sup_sr_tp_id;
454 
455 EXCEPTION
456     WHEN OTHERS THEN
457           RETURN NULL;
458 
459 END GET_MODELED_SR_TP_ID;
460 
461 FUNCTION GET_MODELED_TP_SITE_CODE (pMODELED_SUPPLIER_ID       IN NUMBER,
462                                    pMODELED_SUPPLIER_SITE_ID  IN NUMBER,
463                                    pSR_INSTANCE_ID            IN NUMBER)
464    RETURN VARCHAR2
465 IS
466 lv_mod_sup_site_code VARCHAR2(30);
467 BEGIN
468 
469     SELECT TP_SITE_CODE
470     INTO   lv_mod_sup_site_code
471     FROM   MSC_TRADING_PARTNER_SITES
472     WHERE  PARTNER_ID = pMODELED_SUPPLIER_ID
473       AND  PARTNER_SITE_ID = pMODELED_SUPPLIER_SITE_ID
474       AND  SR_INSTANCE_ID = pSR_INSTANCE_ID
475       AND  PARTNER_TYPE = 1;
476 
477     RETURN lv_mod_sup_site_code;
478 
479 EXCEPTION
480     WHEN OTHERS THEN
481           RETURN NULL;
482 
483 END GET_MODELED_TP_SITE_CODE;
484 
485 /*Procedure has been overloaded since this will be called differently by ASCP
486  * engine and RP engine
487  The procedure called by ASCP currently has 6 new parameters .
488 We are retaining the old signature for RP engine
489 */
490 
491 PROCEDURE MSC_RELEASE_PLAN_SC
492 ( arg_plan_id			IN      NUMBER
493 , arg_log_org_id 		IN 	NUMBER
494 , arg_log_sr_instance           IN      NUMBER
495 , arg_org_id 			IN 	NUMBER
496 , arg_sr_instance               IN      NUMBER
497 , arg_compile_desig 		IN 	VARCHAR2
498 , arg_user_id 			IN 	NUMBER
499 , arg_po_group_by 		IN 	NUMBER
500 , arg_po_batch_number 		IN 	NUMBER
501 , arg_wip_group_id 		IN 	NUMBER
502 , arg_loaded_jobs 		IN OUT  NOCOPY NumTblTyp
503 , arg_loaded_reqs 		IN OUT  NOCOPY  NumTblTyp
504 , arg_loaded_scheds 		IN OUT  NOCOPY NumTblTyp
505 , arg_resched_jobs 		IN OUT  NOCOPY NumTblTyp
506 , arg_resched_reqs 		IN OUT  NOCOPY NumTblTyp
507 , arg_wip_req_id  		IN OUT  NOCOPY NumTblTyp
508 , arg_req_load_id 		IN OUT  NOCOPY  NumTblTyp
509 , arg_req_resched_id 		IN OUT  NOCOPY  NumTblTyp
510 , arg_released_instance         IN OUT  NOCOPY  NumTblTyp
511 , arg_mode                      IN      VARCHAR2
512 , arg_transaction_id            IN      NUMBER
513 , arg_loaded_lot_jobs           IN OUT  NOCOPY  NumTblTyp
514 , arg_resched_lot_jobs          IN OUT  NOCOPY  NumTblTyp
515 , arg_osfm_req_id               IN OUT  NOCOPY  NumTblTyp
516  -- the following 2 parameters added for dsr
517 , arg_resched_eam_jobs           IN OUT  NOCOPY  NumTblTyp
518 , arg_eam_req_id                 IN OUT  NOCOPY  NumTblTyp
519 --arg_eam_req_id will return the request id for the rescheduled EAM jobs
520 -- the following 2 parameters added for DRP Release
521 , arg_loaded_int_reqs               IN OUT  NOCOPY  NumTblTyp
522 , arg_resched_int_reqs              IN OUT  NOCOPY NumTblTyp
523 , arg_int_req_load_id               IN OUT  NOCOPY  NumTblTyp
524 , arg_int_req_resched_id            IN OUT  NOCOPY  NumTblTyp
525 , arg_loaded_int_repair_orders      IN OUT  NOCOPY  NumTblTyp -- bug 6021045
526 , arg_int_repair_orders_id          IN OUT  NOCOPY  NumTblTyp -- bug 6021045
527 , arg_loaded_ext_repair_orders      IN OUT  NOCOPY  NumTblTyp -- bug 6051361
528 , arg_ext_repair_orders_id          IN OUT  NOCOPY  NumTblTyp -- bug 6051361
529 ) IS
530 
531 lv_loaded_eam_jobs       NumTblTyp := NumTblTyp(0);
532 lv_loaded_eam_req_id     NumTblTyp := NumTblTyp(0);
533 lv_resched_cmro_jobs     NumTblTyp := NumTblTyp(0);
534 lv_resched_cmro_req_id   NumTblTyp := NumTblTyp(0);
535 lv_loaded_cmro_jobs      NumTblTyp := NumTblTyp(0);
536 lv_loaded_cmro_req_id    NumTblTyp := NumTblTyp(0);
537 BEGIN
538 
539 /* call the overloaded MSC_RELEASE_PLAN_SC with default parameters for the 6 new
540  * parameters added for USAF
541 lv_loaded_eam_jobs       NumTblTyp;
542 lv_loaded_eam_req_id     NumTblTyp;
543 lv_resched_cmro_jobs     NumTblTyp;
544 lv_resched_cmro_req_id   NumTblTyp;
545 lv_loaded_cmro_jobs      NumTblTyp;
546 lv_loaded_cmro_req_id    NumTblTyp;
547                 */
548 
549 MSC_RELEASE_PLAN_SC
550      ( arg_plan_id
551       , arg_log_org_id
552       , arg_log_sr_instance
553       , arg_org_id
554       , arg_sr_instance
555       , arg_compile_desig
556       , arg_user_id
557       , arg_po_group_by
558       , arg_po_batch_number
559       , arg_wip_group_id
560       , arg_loaded_jobs
561       , arg_loaded_reqs
562       , arg_loaded_scheds
563       , arg_resched_jobs
564       , arg_resched_reqs
565       , arg_wip_req_id
566       , arg_req_load_id
567       , arg_req_resched_id
568       , arg_released_instance
569       , arg_mode
570       , arg_transaction_id
571       , arg_loaded_lot_jobs
572       , arg_resched_lot_jobs
573       , arg_osfm_req_id
574       , arg_resched_eam_jobs
575       , arg_eam_req_id
576       , arg_loaded_int_reqs
577       , arg_resched_int_reqs
578       , arg_int_req_load_id
579       , arg_int_req_resched_id
580       , arg_loaded_int_repair_orders
581       , arg_int_repair_orders_id
582       , arg_loaded_ext_repair_orders
583       , arg_ext_repair_orders_id
584       , lv_loaded_eam_jobs
585       , lv_loaded_eam_req_id
586       , lv_resched_cmro_jobs
587       , lv_resched_cmro_req_id
588       , lv_loaded_cmro_jobs
589       , lv_loaded_cmro_req_id
590       );
591 
592 /* end of call*/
593 
594 END MSC_RELEASE_PLAN_SC ;
595 
596 
597 /*End of overloaded procedure */
598 
599 /***********************************************************
600 PROCEDURE:       MSC_RELEASE_PLAN_SC
601 
602 *************************************************************/
603 PROCEDURE MSC_RELEASE_PLAN_SC
604 ( arg_plan_id			IN      NUMBER
605 , arg_log_org_id 		IN 	NUMBER
606 , arg_log_sr_instance           IN      NUMBER
607 , arg_org_id 			IN 	NUMBER
608 , arg_sr_instance               IN      NUMBER
609 , arg_compile_desig 		IN 	VARCHAR2
610 , arg_user_id 			IN 	NUMBER
611 , arg_po_group_by 		IN 	NUMBER
612 , arg_po_batch_number 		IN 	NUMBER
613 , arg_wip_group_id 		IN 	NUMBER
614 , arg_loaded_jobs 		IN OUT  NOCOPY NumTblTyp
615 , arg_loaded_reqs 		IN OUT  NOCOPY  NumTblTyp
616 , arg_loaded_scheds 		IN OUT  NOCOPY NumTblTyp
617 , arg_resched_jobs 		IN OUT  NOCOPY NumTblTyp
618 , arg_resched_reqs 		IN OUT  NOCOPY NumTblTyp
619 , arg_wip_req_id  		IN OUT  NOCOPY NumTblTyp
620 , arg_req_load_id 		IN OUT  NOCOPY  NumTblTyp
621 , arg_req_resched_id 		IN OUT  NOCOPY  NumTblTyp
622 , arg_released_instance         IN OUT  NOCOPY  NumTblTyp
623 , arg_mode                      IN      VARCHAR2
624 , arg_transaction_id            IN      NUMBER
625 , arg_loaded_lot_jobs           IN OUT  NOCOPY  NumTblTyp
626 , arg_resched_lot_jobs          IN OUT  NOCOPY  NumTblTyp
627 , arg_osfm_req_id               IN OUT  NOCOPY  NumTblTyp
628  -- the following 2 parameters added for dsr
629 , arg_resched_eam_jobs           IN OUT  NOCOPY  NumTblTyp
630 , arg_eam_req_id                 IN OUT  NOCOPY  NumTblTyp
631 --arg_eam_req_id will return the request id for the rescheduled EAM jobs
632 -- the following 2 parameters added for DRP Release
633 , arg_loaded_int_reqs               IN OUT  NOCOPY  NumTblTyp
634 , arg_resched_int_reqs              IN OUT  NOCOPY NumTblTyp
635 , arg_int_req_load_id               IN OUT  NOCOPY  NumTblTyp
636 , arg_int_req_resched_id            IN OUT  NOCOPY  NumTblTyp
637 , arg_loaded_int_repair_orders      IN OUT  NOCOPY  NumTblTyp -- bug 6021045
638 , arg_int_repair_orders_id          IN OUT  NOCOPY  NumTblTyp -- bug 6021045
639 , arg_loaded_ext_repair_orders      IN OUT  NOCOPY  NumTblTyp -- bug 6051361
640 , arg_ext_repair_orders_id          IN OUT  NOCOPY  NumTblTyp -- bug 6051361
641 --The following 6 parameters are added for the USAF project -eAM/cMRO integration
642 , arg_loaded_eam_jobs               IN OUT  NOCOPY  NumTblTyp
643 , arg_loaded_eam_req_id             IN OUT  NOCOPY  NumTblTyp
644 , arg_resched_cmro_jobs             IN OUT  NOCOPY  NumTblTyp
645 , arg_resched_cmro_req_id           IN OUT  NOCOPY  NumTblTyp
646 , arg_loaded_cmro_jobs              IN OUT  NOCOPY  NumTblTyp
647 , arg_loaded_cmro_req_id            IN OUT  NOCOPY  NumTblTyp
648 ) IS
649 
650 CURSOR c_Instance IS
651 SELECT apps1.instance_id,
652        apps1.instance_code,
653        apps1.apps_ver,
654        apps1.instance_type,
655        DECODE(apps1.m2a_dblink,NULL,' ', '@' || m2a_dblink),
656        DECODE(apps1.a2m_dblink,NULL,NULL_DBLINK,a2m_dblink),
657        LENGTH( apps1.instance_code)+2
658 FROM   msc_apps_instances apps1,
659        ( SELECT distinct
660                 sr_instance_id
661            FROM msc_plan_organizations_v plan_org
662           WHERE plan_org.plan_id = arg_plan_id
663             AND plan_org.organization_id = arg_org_id
664             AND plan_org.owning_sr_instance = arg_sr_instance
665             AND plan_org.sr_instance_id =
666                          decode(arg_log_sr_instance,
667                                 arg_sr_instance, plan_org.sr_instance_id,
668                                 arg_log_sr_instance)) ins
669 WHERE apps1.instance_id = ins.sr_instance_id;
670 
671   l_sr_instance_id    NUMBER;
672   l_instance_code     VARCHAR2(3);
673   l_apps_ver          VARCHAR2(10);
674   l_dblink            VARCHAR2(128);
675   l_a2m_dblink        VARCHAR2(128);
676 
677   l_user_name         VARCHAR2(100):= NULL;
678   l_resp_name         VARCHAR2(100):= NULL;
679   l_application_name  VARCHAR2(240):= NULL;
680 
681   l_user_id           NUMBER;
682   l_resp_id           NUMBER;
683   l_application_id    NUMBER;
684 
685 --  l_sql_stmt          VARCHAR2(4000);
686 
687   l_loaded_jobs       NUMBER;
688   l_loaded_reqs       NUMBER;
689   l_loaded_scheds     NUMBER;
690   l_resched_jobs      NUMBER;
691 
692   L_RESCHED_EAM_JOBS	   NUMBER; -- dsr
693   L_EAM_REQ_ID 		   NUMBER; -- dsr
694 
695   l_resched_reqs      NUMBER;
696   l_wip_req_id        NUMBER;
697   l_req_load_id       NUMBER;
698   l_req_resched_id    NUMBER;
699 
700   lv_count            NUMBER:= 0;
701 
702 
703   l_loaded_lot_jobs   NUMBER;
704   l_resched_lot_jobs  NUMBER;
705   l_osfm_req_id       NUMBER;
706 
707   lv_error_buf        VARCHAR2(2000);
708   lv_ret_code         NUMBER;
709 
710   l_load_int_jobs          number;
711   l_resched_int_jobs       number;
712   l_int_req_load_id        number;
713   l_int_req_resched_id     number;
714 
715   l_loaded_int_repair_orders number ;-- bug 6021045
716   l_int_repair_orders_id number ;-- bug 6021045
717 
718   l_loaded_ext_repair_orders number ;-- bug 6051361
719   l_ext_repair_orders_id number ;-- bug 6051361
720 
721   l_loaded_eam_jobs number;
722   l_loaded_eam_req_id number;
723   l_resched_cmro_jobs    number;
724   l_resched_cmro_req_id  number;
725   l_loaded_cmro_jobs     number;
726   l_loaded_cmro_req_id   number;
727   l_wip_group_id      NUMBER;
728   l_po_batch_number   NUMBER;
729   l_concurrent_program_name VARCHAR2(2000);
730 BEGIN
731     BEGIN
732        select fcp.concurrent_program_name
733          INTO l_concurrent_program_name
734          from fnd_concurrent_requests fcr
735              ,fnd_concurrent_programs fcp
736         where request_id = fnd_profile.value('CONC_REQUEST_ID')
737           AND fcr.concurrent_program_id = fcp.concurrent_program_id;
738 
739     IF(l_concurrent_program_name = 'MSCAPO') THEN
740 --        arg_mode := NULL;
741         gv_auto_release := 1;
742     ELSE
743         gv_auto_release := 0;
744     END IF;
745 
746     EXCEPTION WHEN NO_DATA_FOUND
747     THEN
748      gv_auto_release := 0;
749 
750     END;
751 
752    SELECT
753      DECODE(NVL(fnd_profile.value('MSC_RELEASED_BY_USER_ONLY') ,'N'), 'Y',1 ,2)
754      INTO  v_msc_released_only_by_user
755      FROM  DUAL;
756      v_batch_id_populated := 1;
757      IF (g_batch_id = g_prev_batch_id)  OR g_batch_id = -1 THEN
758            begin
759                -- populating batch_id from destination side seq.
760                Execute immediate 'select  mrp_workbench_query_s.nextval
761                                  FROM DUAL '
762                             into MSC_Rel_Plan_PUB.g_batch_id;
763                v_batch_id_populated := 2;
764            exception when others then
765             fnd_file.put_line(FND_FILE.LOG, sqlerrm);
766            end;
767      END IF;
768      g_prev_batch_id := g_batch_id;
769    SELECT
770        FND_GLOBAL.USER_ID,
771        FND_GLOBAL.USER_NAME,
772        FND_GLOBAL.RESP_NAME,
773        FND_GLOBAL.APPLICATION_NAME
774      INTO v_user_id,
775           l_user_name,
776            l_resp_name,
777            l_application_name
778      FROM  dual;
779 
780 
781      SELECT APPLICATION_ID
782      INTO l_application_id
783      FROM FND_APPLICATION_VL
784      WHERE APPLICATION_NAME = l_application_name;
785 
786   -------- Release the planned order one instance by one instance.
787 
788   OPEN c_Instance;
789   LOOP
790     FETCH c_Instance
791      INTO l_sr_instance_id,
792           l_instance_code,
793           l_apps_ver,
794           v_curr_instance_type,
795           l_dblink,
796           l_a2m_dblink,
797           v_instance_code_length;
798 
799     EXIT WHEN c_Instance%NOTFOUND;
800 
801     ---------- Initialize the remote process
802     ---------- If the instance is discrete or mixed type
803     ----------    AND the instance is at a remote database
804 
805    arg_loaded_jobs.extend(1);
806    arg_loaded_reqs.extend(1);
807    arg_loaded_scheds.extend(1);
808    arg_resched_jobs.extend(1);
809    arg_resched_reqs.extend(1);
810    arg_wip_req_id.extend(1);
811    arg_req_load_id.extend(1);
812    arg_req_resched_id.extend(1);
813    arg_released_instance.extend(1);
814    arg_loaded_lot_jobs.extend(1);
815    arg_resched_lot_jobs.extend(1);
816    arg_osfm_req_id.extend(1);
817 
818    arg_loaded_int_reqs.extend(1);
819    arg_resched_int_reqs.extend(1);
820    arg_int_req_load_id.extend(1);
821    arg_int_req_resched_id.extend(1);
822 
823    arg_loaded_int_repair_orders.extend(1); -- bug 6021045
824    arg_int_repair_orders_id.extend(1); --bug 6021045
825 
826    arg_loaded_ext_repair_orders.extend(1); -- bug 6051361
827    arg_ext_repair_orders_id.extend(1); --bug 6051361
828    /*USAF*/
829    arg_loaded_eam_jobs.extend(1);
830    arg_loaded_eam_req_id.extend(1);
831    arg_resched_eam_jobs.extend(1);
832    arg_eam_req_id.extend(1);
833    arg_resched_cmro_jobs.extend(1);
834    arg_resched_cmro_req_id.extend(1);
835    arg_loaded_cmro_jobs.extend(1);
836    arg_loaded_cmro_req_id.extend(1);
837    lv_count:= lv_count+1;
838 
839    arg_released_instance(lv_count):= l_sr_instance_id;
840 
841    -- initialize the Applications Environment --
842   IF v_curr_instance_type IN ( G_INS_DISCRETE, G_INS_PROCESS, G_INS_MIXED, G_INS_OTHER ) THEN /*E1-Bug 8628506  */
843    IF (l_apps_ver >= 3 OR v_curr_instance_type = G_INS_OTHER )THEN /* E1-Bug 8628506 */
844       l_sql_stmt:=
845        'BEGIN'
846      ||'  MRP_AP_REL_PLAN_PUB.INITIALIZE'||l_dblink
847                          ||'( :l_user_name,'
848                          ||'  :l_resp_name,'
849                          ||'  :l_application_name,'
850                          ||'  :l_sr_instance_id,'
851                          ||'  :l_instance_code,'
852                          ||'  :l_a2m_dblink,'
853                          ||'  :l_wip_group_id,'
854                          ||'  :l_po_batch_number,'
855                          ||'  :l_application_id);'
856      ||'END;';
857 
858        EXECUTE IMMEDIATE l_sql_stmt
859                    USING IN l_user_name,
860                          IN l_resp_name,
861                          IN l_application_name,
862                          IN l_sr_instance_id,
863                          IN l_instance_code,
864                          IN l_a2m_dblink,
865                          OUT l_wip_group_id,
866                          OUT l_po_batch_number,
867                          IN l_application_id;
868     ELSE
869          l_sql_stmt:=
870             'BEGIN'
871           ||'  MRP_AP_REL_PLAN_PUB.INITIALIZE'||l_dblink
872                               ||'( :l_user_name,'
873                               ||'  :l_resp_name,'
874                               ||'  :l_application_name);'
875           ||'END;';
876 
877          EXECUTE IMMEDIATE l_sql_stmt
878                         USING IN l_user_name,
879                               IN l_resp_name,
880                               IN l_application_name;
881 
882     END IF;
883 
884     END IF;
885 
886        l_loaded_jobs   := 0;
887        l_loaded_reqs   := 0;
888        l_loaded_scheds := 0;
889        l_resched_jobs  := 0;
890        l_loaded_lot_jobs := 0;
891        l_resched_lot_jobs := 0;
892        L_RESCHED_EAM_JOBS := 0; -- dsr
893        l_load_int_jobs   :=0;
894        l_resched_int_jobs :=0;
895 
896        l_loaded_int_repair_orders :=0  ;-- bug 6021045
897        l_loaded_ext_repair_orders :=0  ;-- bug 6051361
898 
899        l_loaded_eam_jobs :=0;
900        l_resched_cmro_jobs :=0;
901     -- load the msc interface tables, submit the request --
902        LOAD_MSC_INTERFACE
903                 (arg_dblink  => l_dblink,
904                  arg_plan_id => arg_plan_id,
905                  arg_log_org_id => arg_log_org_id,
906                  arg_org_instance => l_sr_instance_id,
907                  arg_owning_org_id => arg_org_id,
908                  arg_owning_instance => arg_sr_instance,
909                  arg_compile_desig => arg_compile_desig,
910                  arg_user_id => arg_user_id,
911                  arg_po_group_by => arg_po_group_by,
912                  arg_po_batch_number => l_po_batch_number,
913                  arg_wip_group_id => l_wip_group_id,
914     ----------------------------------------------- Number of Loaded Orders
915                  arg_loaded_jobs   => l_loaded_jobs,
916                  arg_loaded_lot_jobs => l_loaded_lot_jobs,
917                  arg_resched_lot_jobs => l_resched_lot_jobs,
918                  arg_loaded_reqs   => l_loaded_reqs,
919                  arg_loaded_scheds => l_loaded_scheds,
920                  arg_resched_jobs  => l_resched_jobs,
921     ----------------------------------------------- Request IDs
922                  arg_resched_reqs =>   l_resched_reqs,
923                  arg_wip_req_id =>     l_wip_req_id,
924                  arg_osfm_req_id =>    l_osfm_req_id,
925                  arg_req_load_id =>    l_req_load_id,
926                  arg_req_resched_id => l_req_resched_id,
927     -------------------------------------------------------------
928                  arg_mode => arg_mode,
929                  arg_transaction_id => arg_transaction_id,
930                  l_apps_ver   =>  l_apps_ver,
931                  -- the following 2 lines added for dsr
932 	        		 arg_resched_eam_jobs => l_resched_eam_jobs,
933 							 arg_eam_req_id =>    l_eam_req_id,
934     -------------------------------------------------------------
935                  arg_loaded_int_reqs  => l_load_int_jobs,
936                  arg_resched_int_reqs => l_resched_int_jobs,
937                  arg_int_req_load_id  => l_int_req_load_id,
938                  arg_int_req_resched_id => l_int_req_resched_id,
939     -------------------------------------------------------------
940                arg_loaded_int_repair_orders=> l_loaded_int_repair_orders,   -- bug 6021045
941                arg_int_repair_orders_id=> l_int_repair_orders_id,           -- bug 6021045
942                arg_loaded_ext_repair_orders=> l_loaded_ext_repair_orders,   -- bug 6051361
943                arg_ext_repair_orders_id=> l_ext_repair_orders_id,            -- bug 6051361
944     ---------------------------------------------------------------
945     /*USAF*/
946               arg_loaded_eam_jobs=> l_loaded_eam_jobs,
947               arg_loaded_eam_req_id=> l_loaded_eam_req_id,
948               arg_resched_cmro_jobs=> l_resched_cmro_jobs,
949               arg_resched_cmro_req_id=> l_resched_cmro_req_id,
950               arg_loaded_cmro_jobs=> l_loaded_cmro_jobs,
951               arg_loaded_cmro_req_id=> l_loaded_cmro_req_id
952 		 );
953 
954 
955 
956    COMMIT WORK;
957 
958    arg_loaded_jobs(lv_count)   :=  l_loaded_jobs;
959    arg_loaded_reqs(lv_count)   :=  l_loaded_reqs;
960    arg_loaded_scheds(lv_count) :=  l_loaded_scheds;
961    arg_resched_jobs(lv_count)  :=  l_resched_jobs;
962 
963    arg_loaded_lot_jobs(lv_count) := l_loaded_lot_jobs;
964    arg_resched_lot_jobs(lv_count) := l_resched_lot_jobs;
965 
966    arg_resched_reqs(lv_count)  :=  l_resched_reqs;
967    arg_wip_req_id(lv_count)    :=  l_wip_req_id;
968 
969    arg_osfm_req_id(lv_count)    :=  l_osfm_req_id;
970    arg_req_load_id(lv_count)   :=  l_req_load_id;
971    arg_req_resched_id(lv_count):=  l_req_resched_id;
972 
973     -- dsr
974    arg_resched_eam_jobs(lv_count) := l_resched_eam_jobs;
975    arg_eam_req_id(lv_count)    :=  l_eam_req_id;
976 
977    -- DRP Release
978    arg_loaded_int_reqs(lv_count) := l_load_int_jobs;
979    arg_resched_int_reqs(lv_count) := l_resched_int_jobs;
980    arg_int_req_load_id(lv_count) := l_int_req_load_id;
981    arg_int_req_resched_id(lv_count) :=  l_int_req_resched_id;
982 
983    --IRO release
984     arg_loaded_int_repair_orders(lv_count):= l_loaded_int_repair_orders;    -- bug 6021045
985     arg_int_repair_orders_id(lv_count) := l_int_repair_orders_id;-- bug 6021045
986 
987    --ERO release
988     arg_loaded_ext_repair_orders(lv_count):= l_loaded_ext_repair_orders;    -- bug 6051361
989     arg_ext_repair_orders_id(lv_count) := l_ext_repair_orders_id;-- bug 6051361
990 
991     /*USAF*/
992     arg_loaded_eam_jobs(lv_count) := l_loaded_eam_jobs;
993     arg_loaded_eam_req_id(lv_count) := l_loaded_eam_req_id;
994     arg_loaded_cmro_jobs(lv_count) := l_loaded_cmro_jobs;
995     arg_loaded_cmro_req_id(lv_count) := l_loaded_cmro_req_id;
996     arg_resched_cmro_jobs(lv_count) := l_resched_cmro_jobs;
997     arg_resched_cmro_req_id(lv_count) := l_resched_cmro_req_id;
998 
999    END LOOP;
1000 
1001    CLOSE c_Instance;
1002 
1003    arg_loaded_jobs.trim(1);
1004    arg_loaded_lot_jobs.trim(1);
1005    arg_loaded_reqs.trim(1);
1006    arg_loaded_scheds.trim(1);
1007    arg_resched_jobs.trim(1);
1008    arg_resched_lot_jobs.trim(1);
1009    arg_resched_reqs.trim(1);
1010    arg_wip_req_id.trim(1);
1011    arg_osfm_req_id.trim(1);
1012    arg_req_load_id.trim(1);
1013    arg_req_resched_id.trim(1);
1014    arg_released_instance.trim(1);
1015    arg_loaded_int_reqs.trim(1);
1016    arg_resched_int_reqs.trim(1);
1017    arg_int_req_load_id.trim(1);
1018    arg_int_req_resched_id.trim(1);
1019    arg_loaded_int_repair_orders.trim(1);-- bug 6021045
1020    arg_int_repair_orders_id.trim(1);-- bug 6021045
1021    arg_loaded_ext_repair_orders.trim(1);-- bug 6051361
1022    arg_ext_repair_orders_id.trim(1);-- bug 6051361
1023    /*USAF*/
1024    arg_loaded_eam_jobs.trim(1);
1025    arg_loaded_eam_req_id.trim(1);
1026    arg_resched_eam_jobs.trim(1);
1027    arg_eam_req_id.trim(1);
1028 
1029 EXCEPTION
1030 
1031    WHEN OTHERS THEN
1032       IF c_Instance%ISOPEN THEN CLOSE c_Instance; END IF;
1033 
1034       RAISE;
1035 
1036 END MSC_Release_Plan_Sc;
1037 
1038 /* for bug: 2428319,
1039   OVER-LOADED the PROCEDURE MSC_RELEASE_PLAN_SC for backward compatibility, so that the old versions of UI package MSC_EXP_WF
1040   and the MSCFNORD does not get INVALID after the application of Collections patch
1041   This may also happens because ATP has some UI pre-req patch which applies MSCFNORD on the source instance
1042   and does not get compiled
1043   This is done only for Compilation purpose and this procedure will not be called by UI for the
1044   functionality of Releasing planned orders
1045 */
1046 PROCEDURE MSC_RELEASE_PLAN_SC
1047 ( arg_plan_id			IN      NUMBER
1048 , arg_log_org_id 		IN 	NUMBER
1049 , arg_log_sr_instance           IN      NUMBER
1050 , arg_org_id 			IN 	NUMBER
1051 , arg_sr_instance               IN      NUMBER
1052 , arg_compile_desig 		IN 	VARCHAR2
1053 , arg_user_id 			IN 	NUMBER
1054 , arg_po_group_by 		IN 	NUMBER
1055 , arg_po_batch_number 		IN 	NUMBER
1056 , arg_wip_group_id 		IN 	NUMBER
1057 , arg_loaded_jobs 		IN OUT 	NOCOPY  NumTblTyp
1058 , arg_loaded_reqs 		IN OUT  NOCOPY  NumTblTyp
1059 , arg_loaded_scheds 		IN OUT  NOCOPY  NumTblTyp
1060 , arg_resched_jobs 		IN OUT  NOCOPY  NumTblTyp
1061 , arg_resched_reqs 		IN OUT  NOCOPY  NumTblTyp
1062 , arg_wip_req_id  		IN OUT  NOCOPY  NumTblTyp
1063 , arg_req_load_id 		IN OUT  NOCOPY  NumTblTyp
1064 , arg_req_resched_id 		IN OUT  NOCOPY  NumTblTyp
1065 , arg_released_instance         IN OUT  NOCOPY  NumTblTyp
1066 , arg_mode                      IN      VARCHAR2
1067 , arg_transaction_id            IN      NUMBER
1068 , arg_loaded_int_reqs               IN OUT  NOCOPY  NumTblTyp
1069 , arg_resched_int_reqs              IN OUT  NOCOPY  NumTblTyp
1070 , arg_int_req_load_id               IN OUT  NOCOPY  NumTblTyp
1071 , arg_int_req_resched_id            IN OUT  NOCOPY  NumTblTyp
1072 , arg_loaded_int_repair_orders      IN OUT  NOCOPY  NumTblTyp -- bug 6021045
1073 , arg_int_repair_orders_id          IN OUT  NOCOPY  NumTblTyp -- bug 6021045
1074 , arg_loaded_ext_repair_orders      IN OUT  NOCOPY  NumTblTyp -- bug 6051361
1075 , arg_ext_repair_orders_id          IN OUT  NOCOPY  NumTblTyp -- bug 6051361
1076 --The following 6 parameters are added for the' USAF project -eAM/cMRO integration
1077 , arg_loaded_eam_jobs               IN OUT  NOCOPY  NumTblTyp
1078 , arg_loaded_eam_req_id 	        IN OUT  NOCOPY  NumTblTyp
1079 , arg_resched_cmro_jobs             IN OUT  NOCOPY  NumTblTyp
1080 , arg_resched_cmro_req_id 	        IN OUT  NOCOPY  NumTblTyp
1081 , arg_loaded_cmro_jobs              IN OUT  NOCOPY  NumTblTyp
1082 , arg_loaded_cmro_req_id 	        IN OUT  NOCOPY  NumTblTyp
1083 ) IS
1084 
1085 CURSOR c_Instance IS
1086 SELECT apps1.instance_id,
1087        apps1.apps_ver,
1088        apps1.instance_type,
1089        DECODE(apps1.m2a_dblink,NULL,' ', '@' || m2a_dblink),
1090        LENGTH( apps1.instance_code)+2
1091 FROM   msc_apps_instances apps1,
1092        ( SELECT distinct
1093                 sr_instance_id
1094            FROM msc_plan_organizations_v plan_org
1095           WHERE plan_org.plan_id = arg_plan_id
1096             AND plan_org.organization_id = arg_org_id
1097             AND plan_org.owning_sr_instance = arg_sr_instance
1098             AND plan_org.sr_instance_id =
1099                          decode(arg_log_sr_instance,
1100                                 arg_sr_instance, plan_org.sr_instance_id,
1101                                 arg_log_sr_instance)) ins
1102 WHERE apps1.instance_id = ins.sr_instance_id;
1103 
1104   l_sr_instance_id    NUMBER;
1105   l_apps_ver          VARCHAR2(10);
1106   l_dblink            VARCHAR2(128);
1107 
1108   l_user_name         VARCHAR2(100):= NULL;
1109   l_resp_name         VARCHAR2(100):= NULL;
1110   l_application_name  VARCHAR2(240):= NULL;
1111 
1112   l_user_id           NUMBER;
1113   l_resp_id           NUMBER;
1114   l_application_id    NUMBER;
1115 
1116 --  l_sql_stmt          VARCHAR2(4000);
1117 
1118   l_loaded_jobs       NUMBER;
1119   l_loaded_reqs       NUMBER;
1120   l_loaded_scheds     NUMBER;
1121   l_resched_jobs      NUMBER;
1122 
1123   l_resched_reqs      NUMBER;
1124   l_wip_req_id        NUMBER;
1125   l_req_load_id       NUMBER;
1126   l_req_resched_id    NUMBER;
1127 
1128   lv_count            NUMBER:= 0;
1129 
1130  arg_loaded_lot_jobs  NumTblTyp;
1131  arg_resched_lot_jobs NumTblTyp;
1132  arg_osfm_req_id      NumTblTyp;
1133 
1134   l_loaded_lot_jobs   NUMBER;
1135   l_resched_lot_jobs  NUMBER;
1136   l_osfm_req_id       NUMBER;
1137 
1138   l_wip_group_id      NUMBER;
1139   l_po_batch_number   NUMBER;
1140 
1141   L_RESCHED_EAM_JOBS	   NUMBER; -- dsr
1142   L_EAM_REQ_ID 		   NUMBER; -- dsr
1143 
1144   lv_error_buf        VARCHAR2(2000);
1145   lv_ret_code	      NUMBER;
1146 
1147   l_load_int_jobs          number;
1148   l_resched_int_jobs       number;
1149   l_int_req_load_id        number;
1150   l_int_req_resched_id     number;
1151 
1152   l_loaded_int_repair_orders number ;-- bug 6021045
1153   l_int_repair_orders_id number ;-- bug 6021045
1154 
1155   l_loaded_ext_repair_orders number ;-- bug 6051361
1156   l_ext_repair_orders_id number ;-- bug 6051361
1157 
1158   l_loaded_eam_jobs number ;
1159   l_loaded_eam_req_id number;
1160   l_resched_cmro_jobs    number;
1161   l_resched_cmro_req_id  number;
1162   l_loaded_cmro_jobs     number;
1163   l_loaded_cmro_req_id   number;
1164 BEGIN
1165 
1166 
1167    SELECT
1168      DECODE(NVL(fnd_profile.value('MSC_RELEASED_BY_USER_ONLY') ,'N'), 'Y',1 ,2)
1169      INTO  v_msc_released_only_by_user
1170      FROM  DUAL;
1171      v_batch_id_populated := 1;
1172      IF (g_batch_id = g_prev_batch_id)  OR g_batch_id = -1 THEN
1173            begin
1174                -- populating batch_id from destination side seq.
1175                Execute immediate 'select  mrp_workbench_query_s.nextval
1176                                  FROM DUAL '
1177                             into MSC_Rel_Plan_PUB.g_batch_id;
1178                v_batch_id_populated := 2;
1179            exception when others then
1180             fnd_file.put_line(FND_FILE.LOG, sqlerrm);
1181            end;
1182      END IF;
1183      g_prev_batch_id := g_batch_id;
1184    SELECT
1185        FND_GLOBAL.USER_ID,
1186        FND_GLOBAL.USER_NAME,
1187        FND_GLOBAL.RESP_NAME,
1188        FND_GLOBAL.APPLICATION_NAME
1189      INTO v_user_id,
1190           l_user_name,
1191            l_resp_name,
1192            l_application_name
1193      FROM  dual;
1194 
1195   -------- Release the planned order one instance by one instance.
1196 
1197   OPEN c_Instance;
1198   LOOP
1199     FETCH c_Instance
1200      INTO l_sr_instance_id,
1201           l_apps_ver,
1202           v_curr_instance_type,
1203           l_dblink,
1204           v_instance_code_length;
1205 
1206     EXIT WHEN c_Instance%NOTFOUND;
1207 
1208     ---------- Initialize the remote process
1209     ---------- If the instance is discrete or mixed type
1210     ----------    AND the instance is at a remote database
1211 
1212    arg_loaded_jobs.extend(1);
1213    arg_loaded_reqs.extend(1);
1214    arg_loaded_scheds.extend(1);
1215    arg_resched_jobs.extend(1);
1216    arg_resched_reqs.extend(1);
1217    arg_wip_req_id.extend(1);
1218    arg_req_load_id.extend(1);
1219    arg_req_resched_id.extend(1);
1220    arg_released_instance.extend(1);
1221    arg_loaded_lot_jobs.extend(1);
1222    arg_resched_lot_jobs.extend(1);
1223    arg_osfm_req_id.extend(1);
1224 
1225    arg_loaded_int_reqs.extend(1);
1226    arg_resched_int_reqs.extend(1);
1227    arg_int_req_load_id.extend(1);
1228    arg_int_req_resched_id.extend(1);
1229 
1230    arg_loaded_int_repair_orders.extend(1); -- bug 6021045
1231    arg_int_repair_orders_id.extend(1); --bug 6021045
1232    arg_loaded_ext_repair_orders.extend(1); -- bug 6051361
1233    arg_ext_repair_orders_id.extend(1); --bug 6051361
1234    --USAF
1235    arg_loaded_eam_jobs.extend(1);
1236    arg_loaded_eam_req_id.extend(1);
1237    arg_resched_cmro_jobs.extend(1);
1238    arg_resched_cmro_req_id.extend(1);
1239    arg_loaded_cmro_jobs.extend(1);
1240    arg_loaded_cmro_req_id.extend(1);
1241    lv_count:= lv_count+1;
1242 
1243    arg_released_instance(lv_count):= l_sr_instance_id;
1244 
1245    -- initialize the Applications Environment --
1246    IF v_curr_instance_type IN ( G_INS_DISCRETE, G_INS_PROCESS, G_INS_MIXED) THEN
1247 
1248     l_sql_stmt:=
1249        'BEGIN'
1250      ||'  MRP_AP_REL_PLAN_PUB.INITIALIZE'||l_dblink
1251                          ||'( :l_user_name,'
1252                          ||'  :l_resp_name,'
1253                          ||'  :l_application_name,'
1254                          ||'  :l_wip_group_id,'
1255                          ||'  :l_po_batch_number);'
1256      ||'END;';
1257 
1258        EXECUTE IMMEDIATE l_sql_stmt
1259                    USING IN l_user_name,
1260                          IN l_resp_name,
1261                          IN l_application_name,
1262                          OUT l_wip_group_id,
1263                          OUT l_po_batch_number;
1264 
1265    END IF;
1266 
1267        l_loaded_jobs   := 0;
1268        l_loaded_reqs   := 0;
1269        l_loaded_scheds := 0;
1270        l_resched_jobs  := 0;
1271        l_loaded_lot_jobs := 0;
1272        l_resched_lot_jobs := 0;
1273        l_load_int_jobs   :=0;
1274        l_resched_int_jobs :=0;
1275 
1276        l_loaded_int_repair_orders:= 0 ;--bug 6021045
1277        l_loaded_ext_repair_orders:= 0 ;--bug 6051361
1278 
1279        --USAF
1280        l_loaded_eam_jobs :=0;
1281        l_resched_cmro_jobs := 0;
1282        l_loaded_cmro_jobs := 0;
1283 
1284     -- load the msc interface tables, submit the request --
1285 
1286        LOAD_MSC_INTERFACE
1287                 (arg_dblink  => l_dblink,
1288                  arg_plan_id => arg_plan_id,
1289                  arg_log_org_id => arg_log_org_id,
1290                  arg_org_instance => l_sr_instance_id,
1291                  arg_owning_org_id => arg_org_id,
1292                  arg_owning_instance => arg_sr_instance,
1293                  arg_compile_desig => arg_compile_desig,
1294                  arg_user_id => arg_user_id,
1295                  arg_po_group_by => arg_po_group_by,
1296                  arg_po_batch_number => l_po_batch_number,
1297                  arg_wip_group_id => l_wip_group_id,
1298     ----------------------------------------------- Number of Loaded Orders
1299                  arg_loaded_jobs   => l_loaded_jobs,
1300                  arg_loaded_lot_jobs => l_loaded_lot_jobs,
1301                  arg_resched_lot_jobs => l_resched_lot_jobs,
1302                  arg_loaded_reqs   => l_loaded_reqs,
1303                  arg_loaded_scheds => l_loaded_scheds,
1304                  arg_resched_jobs  => l_resched_jobs,
1305     ----------------------------------------------- Request IDs
1306                  arg_resched_reqs =>   l_resched_reqs,
1307                  arg_wip_req_id =>     l_wip_req_id,
1308                  arg_osfm_req_id =>    l_osfm_req_id,
1309                  arg_req_load_id =>    l_req_load_id,
1310                  arg_req_resched_id => l_req_resched_id,
1311     -------------------------------------------------------------
1312                  arg_mode => arg_mode,
1313                  arg_transaction_id => arg_transaction_id,
1314                  l_apps_ver   =>  l_apps_ver,
1315                  -- the following 2 lines added for dsr
1316 		 arg_resched_eam_jobs => l_resched_eam_jobs, -- dsr: should we put NULL here?
1317 		 arg_eam_req_id => l_eam_req_id, -- dsr ?
1318     -------------------------------------------------------------
1319                  arg_loaded_int_reqs  => l_load_int_jobs,
1320                  arg_resched_int_reqs => l_resched_int_jobs,
1321                  arg_int_req_load_id  => l_int_req_load_id,
1322                  arg_int_req_resched_id => l_int_req_resched_id,
1323     -------------------------------------------------------------
1324                 arg_loaded_int_repair_orders=> l_loaded_int_repair_orders,   -- bug 6021045
1325                 arg_int_repair_orders_id=> l_int_repair_orders_id,   -- bug 6021045
1326                 arg_loaded_ext_repair_orders=> l_loaded_ext_repair_orders,   -- bug 6051361
1327                 arg_ext_repair_orders_id=> l_ext_repair_orders_id,   -- bug 6051361
1328     ---------------------------------------------------------------
1329     /*USAF*/
1330               arg_loaded_eam_jobs => l_loaded_eam_jobs,
1331               arg_loaded_eam_req_id => l_loaded_eam_req_id,
1332               arg_resched_cmro_jobs => l_resched_cmro_jobs,
1333               arg_resched_cmro_req_id => l_resched_cmro_req_id,
1334               arg_loaded_cmro_jobs => l_loaded_cmro_jobs,
1335               arg_loaded_cmro_req_id=>l_loaded_cmro_req_id
1336 		 );
1337 
1338 
1339 
1340    COMMIT WORK;
1341 
1342    arg_loaded_jobs(lv_count)   :=  l_loaded_jobs;
1343    arg_loaded_reqs(lv_count)   :=  l_loaded_reqs;
1344    arg_loaded_scheds(lv_count) :=  l_loaded_scheds;
1345    arg_resched_jobs(lv_count)  :=  l_resched_jobs;
1346 
1347    arg_loaded_lot_jobs(lv_count) := l_loaded_lot_jobs;
1348    arg_resched_lot_jobs(lv_count) := l_resched_lot_jobs;
1349 
1350    arg_resched_reqs(lv_count)  :=  l_resched_reqs;
1351    arg_wip_req_id(lv_count)    :=  l_wip_req_id;
1352 
1353    arg_osfm_req_id(lv_count)    :=  l_osfm_req_id;
1354    arg_req_load_id(lv_count)   :=  l_req_load_id;
1355    arg_req_resched_id(lv_count):=  l_req_resched_id;
1356 
1357    arg_loaded_int_reqs(lv_count) := l_load_int_jobs;
1358    arg_resched_int_reqs(lv_count) := l_resched_int_jobs;
1359    arg_int_req_load_id(lv_count) := l_int_req_load_id;
1360    arg_int_req_resched_id(lv_count) :=  l_int_req_resched_id;
1361 
1362     --IRO release
1363     arg_loaded_int_repair_orders(lv_count):= l_loaded_int_repair_orders;    -- bug 6021045
1364     arg_int_repair_orders_id(lv_count) := l_int_repair_orders_id;-- bug 6021045
1365 
1366     --ERO release
1367     arg_loaded_ext_repair_orders(lv_count):= l_loaded_ext_repair_orders;    -- bug 6051361
1368     arg_ext_repair_orders_id(lv_count) := l_ext_repair_orders_id;-- bug 6051361
1369 
1370     /*USAF*/
1371     arg_loaded_eam_jobs(lv_count) := l_loaded_eam_jobs;
1372     arg_loaded_eam_req_id(lv_count) := l_loaded_eam_req_id;
1373     arg_resched_cmro_jobs(lv_count)   := l_resched_cmro_jobs;
1374     arg_resched_cmro_req_id(lv_count) := l_resched_cmro_req_id;
1375     arg_loaded_cmro_jobs(lv_count)    := l_loaded_cmro_jobs;
1376     arg_loaded_cmro_req_id(lv_count)  := l_loaded_cmro_req_id;
1377 
1378    END LOOP;
1379 
1380    CLOSE c_Instance;
1381 
1382    arg_loaded_jobs.trim(1);
1383    arg_loaded_reqs.trim(1);
1384    arg_loaded_scheds.trim(1);
1385    arg_resched_jobs.trim(1);
1386    arg_resched_reqs.trim(1);
1387    arg_wip_req_id.trim(1);
1388    arg_req_load_id.trim(1);
1389    arg_req_resched_id.trim(1);
1390    arg_released_instance.trim(1);
1391    arg_loaded_int_reqs.trim(1);
1392    arg_resched_int_reqs.trim(1);
1393    arg_int_req_load_id.trim(1);
1394    arg_int_req_resched_id.trim(1);
1395 
1396     --IRO release
1397     arg_loaded_int_repair_orders.trim(1);    -- bug 6021045
1398     arg_int_repair_orders_id.trim(1);-- bug 6021045
1399 
1400     --ERO release
1401     arg_loaded_ext_repair_orders.trim(1);    -- bug 6051361
1402     arg_ext_repair_orders_id.trim(1);-- bug 6051361
1403 
1404        /*USAF*/
1405    arg_loaded_eam_jobs.trim(1);
1406    arg_loaded_eam_req_id.trim(1);
1407    arg_resched_cmro_jobs.trim(1);
1408    arg_resched_cmro_req_id.trim(1);
1409    arg_loaded_cmro_jobs.trim(1);
1410    arg_loaded_cmro_req_id.trim(1);
1411 
1412 EXCEPTION
1413 
1414    WHEN OTHERS THEN
1415       IF c_Instance%ISOPEN THEN CLOSE c_Instance; END IF;
1416 
1417       RAISE;
1418 
1419 END MSC_Release_Plan_Sc;
1420 
1421 /***********************************************************
1422 PROCEDURE:        LOAD_MSC_INTERFACE
1423 
1424 *************************************************************/
1425 PROCEDURE LOAD_MSC_INTERFACE
1426 ( arg_dblink                    IN      VARCHAR2
1427 , arg_plan_id			IN      NUMBER
1428 , arg_log_org_id 		IN 	NUMBER
1429 , arg_org_instance              IN      NUMBER
1430 , arg_owning_org_id 	        IN	NUMBER
1431 , arg_owning_instance           IN      NUMBER
1432 , arg_compile_desig 		IN 	VARCHAR2
1433 , arg_user_id 			IN 	NUMBER
1434 , arg_po_group_by 		IN 	NUMBER
1435 , arg_po_batch_number 		IN 	NUMBER
1436 , arg_wip_group_id 		IN 	NUMBER
1437 , arg_loaded_jobs 		IN OUT 	NOCOPY  NUMBER
1438 , arg_loaded_lot_jobs           IN OUT  NOCOPY  NUMBER
1439 , arg_resched_lot_jobs          IN OUT  NOCOPY  NUMBER
1440 , arg_loaded_reqs 		IN OUT  NOCOPY  NUMBER
1441 , arg_loaded_scheds 		IN OUT  NOCOPY  NUMBER
1442 , arg_resched_jobs 		IN OUT  NOCOPY  NUMBER
1443 , arg_resched_reqs 		IN OUT  NOCOPY  NUMBER
1444 , arg_wip_req_id 		IN OUT  NOCOPY  NUMBER
1445 , arg_osfm_req_id               IN OUT  NOCOPY  NUMBER
1446 , arg_req_load_id 		IN OUT  NOCOPY  NUMBER
1447 , arg_req_resched_id 		IN OUT  NOCOPY  NUMBER
1448 , arg_mode                      IN      VARCHAR2
1449 , arg_transaction_id            IN      NUMBER
1450 , l_apps_ver                    IN VARCHAR2
1451 -- dsr
1452 , arg_resched_eam_jobs          IN OUT  NOCOPY  NUMBER
1453 , arg_eam_req_id                IN OUT  NOCOPY  NUMBER
1454 , arg_loaded_int_reqs               IN OUT  NOCOPY  Number
1455 , arg_resched_int_reqs              IN OUT  NOCOPY  Number
1456 , arg_int_req_load_id               IN OUT  NOCOPY  Number
1457 , arg_int_req_resched_id            IN OUT  NOCOPY  Number
1458 , arg_loaded_int_repair_orders      IN OUT  NOCOPY  Number -- bug 6021045
1459 , arg_int_repair_orders_id          IN OUT  NOCOPY  Number -- bug 6021045
1460 , arg_loaded_ext_repair_orders      IN OUT  NOCOPY  Number -- bug 6051361
1461 , arg_ext_repair_orders_id          IN OUT  NOCOPY  Number -- bug 6051361
1462 /*USAF*/
1463 , arg_loaded_eam_jobs               IN OUT NOCOPY NUMBER
1464 , arg_loaded_eam_req_id             IN OUT NOCOPY NUMBER
1465 , arg_resched_cmro_jobs             IN OUT  NOCOPY NUMBER
1466 , arg_resched_cmro_req_id           IN OUT  NOCOPY NUMBER
1467 , arg_loaded_cmro_jobs               IN OUT NOCOPY NUMBER
1468 , arg_loaded_cmro_req_id             IN OUT NOCOPY NUMBER
1469 ) IS
1470    TYPE CurTyp is ref cursor;
1471 
1472    CURSOR c_plan_type(p_plan_id number) IS
1473       select plan_type
1474         from msc_plans a
1475         where
1476         plan_id = p_plan_id;
1477 
1478    CURSOR c_plan_completion_date(p_plan_id number) IS
1479       select plan_completion_date
1480         from msc_plans
1481         where plan_id = p_plan_id;
1482 
1483    CURSOR  c_rp_rel_ord is
1484    select s.plan_id,
1485               s.transaction_id,
1486               s.implement_quantity,
1487               s.implement_date,
1488               p.plan_version,
1489               s.last_update_date,
1490               s.last_updated_by,
1491               s.creation_date,
1492               s.created_by,
1493               s.last_update_login,
1494               decode(s.release_status,
1495                      11,21,
1496                      12,22,
1497                      13,23,
1498                      s.release_status) rp_release_status
1499             FROM msc_supplies s, msc_plans p
1500             WHERE p.plan_id = s.plan_id
1501             AND s.plan_id = arg_plan_id
1502             AND batch_id = g_batch_id;
1503 
1504     VERSION                 CONSTANT CHAR(200) :=
1505     '$Header: MSCPRELB.pls 120.89.12020000.7 2012/11/29 05:21:25 qiangwan ship $';
1506 
1507     lv_launch_process      INTEGER;
1508     lv_handle              VARCHAR2(200);
1509     lv_output              NUMBER;
1510     lv_error_stmt          VARCHAR2(2000) := NULL;
1511 
1512     lv_sql_stmt            VARCHAR2(4000);
1513     lv_temp		    NUMBER;
1514 
1515     lv_wf                   NUMBER;
1516     lv_wf_load_type         NUMBER;
1517 
1518     retcode                varchar2(30);
1519 
1520     /* DWK */
1521     l_new_iso_num          number;
1522     l_reschedule_iso_num   number;
1523 
1524     l_new_iro_num number ; -- bug 6021045
1525     l_new_ero_num number ; -- bug 6051361
1526 
1527     c_batch_number  CurTyp;
1528     l_batch_number1  NUMBER;
1529     l_batch_number2  NUMBER;
1530 
1531     l_batch_number3 NUMBER;-- bug 6021045
1532     l_batch_number4 NUMBER;-- bug 6051361
1533     l_request_id    NUMBER;
1534 
1535     l_plan_completion_date DATE;
1536     l_plan_type     NUMBER;
1537     lv_error_buf        VARCHAR2(2000);
1538     lv_ret_code       NUMBER;
1539     l_timestamp_profile_val VARCHAR2(10);
1540 
1541     l_user_id            NUMBER;
1542     l_user_name          VARCHAR2(2000);
1543     l_resp_name          VARCHAR2(2000);
1544     l_application_name   VARCHAR2(2000);
1545     l_application_id     NUMBER;
1546     lv_inst_type        NUMBER;
1547     rp_release_status   NUMBER;
1548 BEGIN
1549     -- if mode is NULL then it means that this procedure is called from PWB
1550     -- where we need to do batch processing
1551     -- If mode is WF, then we need to do this work only for the
1552     -- transaction_id that is passed in
1553     -- If the mode is WF_BATCH, this will only process the planned orders
1554     -- which need to be released, it won't process the supplies which needs
1555     -- to be rescheduled
1556 
1557     dbms_lock.allocate_unique(arg_compile_desig||
1558                                   to_char(arg_owning_org_id),lv_handle);
1559 
1560     lv_output := dbms_lock.request(lv_handle, 6, 32767, TRUE);
1561 
1562 /*
1563     IF l_plan_type = 8 AND l_apps_ver = MSC_UTIL. G_APPS115  THEN
1564 			 RAISE_APPLICATION_ERROR(-20000,'SRP release not supported to 11510 source',TRUE);
1565     END IF;
1566 */
1567     if(lv_output <> 0) then
1568         FND_MESSAGE.SET_NAME('MRP', 'GEN-LOCK-WARNING');
1569         FND_MESSAGE.SET_TOKEN('EVENT', 'RELEASE PLANNED ORDERS');
1570         lv_error_stmt := FND_MESSAGE.GET;
1571         raise_application_error(-20000, lv_error_stmt);
1572     end if;
1573 
1574     OPEN c_plan_type(arg_plan_id);
1575     FETCH c_plan_type INTO l_plan_type;
1576     CLOSE c_plan_type;
1577 
1578     OPEN c_plan_completion_date(arg_plan_id);
1579     FETCH c_plan_completion_date into l_plan_completion_date;
1580     CLOSE c_plan_completion_date;
1581 
1582     select instance_type into lv_inst_type from msc_apps_instances where instance_id = arg_org_instance;
1583 
1584     v_plan_type  := l_plan_type;  -- For RP Release
1585     -- Get the hour uom code
1586     -- Get the profile MRP_PURCHASING_BY_REVISION is set
1587 
1588     if ( v_plan_type > 100) then
1589        v_rp_plan := 1;
1590     else
1591        v_rp_plan := 0;
1592     end if;
1593 
1594     lv_sql_stmt:=
1595        'BEGIN'
1596      ||' :v_hour_uom := FND_PROFILE.VALUE'||arg_dblink
1597                                    ||'(''BOM:HOUR_UOM_CODE'');'
1598      ||' :v_purchasing_by_rev := FND_PROFILE.VALUE'||arg_dblink
1599                                    ||'(''MRP_PURCHASING_BY_REVISION'');'
1600      ||'END;';
1601 
1602     EXECUTE IMMEDIATE lv_sql_stmt
1603             USING OUT v_hour_uom,
1604                   OUT v_purchasing_by_rev;
1605 
1606     lv_wf:= SYS_NO;
1607     -- Get the Load Type if it's 'WF' mode.
1608     IF arg_mode = 'WF' THEN
1609        lv_wf:= SYS_YES;
1610        BEGIN
1611           SELECT load_type
1612             INTO lv_wf_load_type
1613             FROM MSC_SUPPLIES s
1614            WHERE s.plan_id = arg_plan_id
1615              AND s.transaction_id = arg_transaction_id;
1616        EXCEPTION
1617           WHEN NO_DATA_FOUND THEN NULL;
1618           WHEN OTHERS THEN RAISE;
1619        END;
1620     END IF;
1621 
1622     IF arg_mode = 'WF_BATCH' then
1623        lv_wf := 3;
1624     END IF;
1625 
1626    /* start bug 6021045 */
1627     /* For releasing IRO (internal repair order )*/
1628     IF ( lv_wf_load_type = IRO_LOAD   OR
1629          lv_wf= SYS_NO   OR
1630          lv_wf =3)  AND l_apps_ver > MSC_UTIL.G_APPS115 THEN
1631 
1632           lv_sql_stmt := ' SELECT mrp_workbench_query_s.nextval from dual'|| arg_dblink;
1633           OPEN c_batch_number for lv_sql_stmt;
1634           FETCH c_batch_number INTO l_batch_number3;
1635           CLOSE c_batch_number;
1636 
1637 
1638 
1639           OPEN c_plan_type(arg_plan_id);
1640           FETCH c_plan_type INTO l_plan_type;
1641           CLOSE c_plan_type;
1642 
1643            IF l_plan_type = 8 THEN
1644 
1645                IF(MSC_Rel_Plan_PUB.G_SPP_SPLIT_YN = 'Y') THEN
1646                Release_IRO_2(
1647                                   p_dblink  => arg_dblink,
1648                                   p_arg_iro_batch_number => l_batch_number3,
1649                                   p_arg_owning_instance => arg_owning_instance,
1650                                   p_arg_po_group_by     => arg_po_group_by,
1651                                   p_arg_plan_id         => arg_plan_id,
1652                                   p_arg_log_org_id      => arg_log_org_id,
1653                                   p_arg_owning_org_id  => arg_owning_org_id,
1654                                   p_arg_org_instance   => arg_org_instance,
1655                                   p_arg_mode           => arg_mode,
1656                                   p_arg_transaction_id  => arg_transaction_id,
1657                                   p_arg_loaded_int_repair_orders  =>l_new_iro_num,
1658                                   p_load_type     => IRO_LOAD);
1659                  ELSE
1660                      Release_IRO(
1661                                   p_dblink  => arg_dblink,
1662                                   p_arg_iro_batch_number => l_batch_number3,
1663                                   p_arg_owning_instance => arg_owning_instance,
1664                                   p_arg_po_group_by     => arg_po_group_by,
1665                                   p_arg_plan_id         => arg_plan_id,
1666                                   p_arg_log_org_id      => arg_log_org_id,
1667                                   p_arg_owning_org_id  => arg_owning_org_id,
1668                                   p_arg_org_instance   => arg_org_instance,
1669                                   p_arg_mode           => arg_mode,
1670                                   p_arg_transaction_id  => arg_transaction_id,
1671                                   p_arg_loaded_int_repair_orders  =>l_new_iro_num,
1672                                   p_load_type     => IRO_LOAD);
1673                   END IF;
1674 
1675                 arg_loaded_int_repair_orders:= l_new_iro_num;
1676 
1677 
1678                      l_user_id := FND_GLOBAL.USER_ID;
1679                      l_user_name := FND_GLOBAL.USER_NAME;
1680                      l_resp_name := FND_GLOBAL.RESP_NAME;
1681                      l_application_name := FND_GLOBAL.APPLICATION_NAME;
1682 
1683 
1684                   SELECT APPLICATION_ID
1685                      INTO l_application_id
1686                      FROM FND_APPLICATION_VL
1687                      WHERE APPLICATION_NAME = l_application_name;
1688 
1689                  IF ( arg_loaded_int_repair_orders > 0) THEN
1690                        lv_sql_stmt:= ' BEGIN '
1691                           ||' MSC_SRP_RELEASE.MSC_RELEASE_IRO'||arg_dblink
1692                           ||'(:l_user_name,:l_resp_name,:l_application_name,:l_application_id,:l_batch_number, 256, '
1693                           ||' :arg_int_repair_orders_id);' ||
1694                         ' END;';
1695                     EXECUTE IMMEDIATE lv_sql_stmt
1696                     USING
1697                         IN l_user_name,
1698                         IN l_resp_name,
1699                         IN l_application_name,IN l_application_id,
1700                         IN l_batch_number3,
1701                         IN OUT arg_int_repair_orders_id;
1702 
1703                       COMMIT WORK;
1704                   END IF;
1705 
1706 
1707      END IF ;
1708 
1709     END IF ;
1710   /* end bug bug 6021045 */
1711 
1712    /* Start of Bug 6051361 */
1713   IF (lv_wf_load_type = ERO_LOAD   OR
1714          lv_wf= SYS_NO   OR
1715         lv_wf =3)  AND l_apps_ver > MSC_UTIL.G_APPS115 THEN
1716 
1717           lv_sql_stmt := ' SELECT mrp_workbench_query_s.nextval from dual'|| arg_dblink;
1718           OPEN c_batch_number for lv_sql_stmt;
1719           FETCH c_batch_number INTO l_batch_number4;
1720           CLOSE c_batch_number;
1721 
1722 
1723 
1724           OPEN c_plan_type(arg_plan_id);
1725           FETCH c_plan_type INTO l_plan_type;
1726           CLOSE c_plan_type;
1727 
1728        --execute immediate ' SELECT mrp_workbench_query_s@arg_dblink.nextval  INTO l_batch_number4  from dual'|| arg_dblink;
1729 
1730        --SELECT msc_plans.mrp_plan_type INTO l_plan_type from MSC_PLANS
1731        --WHERE PLAN_ID =  arg_plan_id ;
1732 
1733        IF l_plan_type = 8 THEN
1734 
1735                             IF(MSC_Rel_Plan_PUB.G_SPP_SPLIT_YN = 'Y') THEN
1736 
1737                                 Release_ERO_2(
1738                                   p_dblink  => arg_dblink,
1739                                   p_arg_ero_batch_number => l_batch_number4,
1740                                   p_arg_owning_instance => arg_owning_instance,
1741                                   p_arg_po_group_by     => arg_po_group_by,
1742                                   p_arg_plan_id         => arg_plan_id,
1743                                   p_arg_log_org_id      => arg_log_org_id,
1744                                   p_arg_owning_org_id  => arg_owning_org_id,
1745                                   p_arg_org_instance   => arg_org_instance,
1746                                   p_arg_mode           => arg_mode,
1747                                   p_arg_transaction_id  => arg_transaction_id,
1748                                   p_arg_loaded_ext_repair_orders  =>l_new_ero_num,
1749                                   p_load_type     =>  ERO_LOAD);
1750 
1751                               ELSE
1752 
1753                                   Release_ERO(
1754                                   p_dblink  => arg_dblink,
1755                                   p_arg_ero_batch_number => l_batch_number4,
1756                                   p_arg_owning_instance => arg_owning_instance,
1757                                   p_arg_po_group_by     => arg_po_group_by,
1758                                   p_arg_plan_id         => arg_plan_id,
1759                                   p_arg_log_org_id      => arg_log_org_id,
1760                                   p_arg_owning_org_id  => arg_owning_org_id,
1761                                   p_arg_org_instance   => arg_org_instance,
1762                                   p_arg_mode           => arg_mode,
1763                                   p_arg_transaction_id  => arg_transaction_id,
1764                                   p_arg_loaded_ext_repair_orders  =>l_new_ero_num,
1765                                   p_load_type     =>  ERO_LOAD);
1766 
1767           END IF;
1768        END IF ;
1769     arg_loaded_ext_repair_orders:= l_new_ero_num ;
1770 
1771               l_user_id := FND_GLOBAL.USER_ID;
1772               l_user_name := FND_GLOBAL.USER_NAME;
1773               l_resp_name := FND_GLOBAL.RESP_NAME;
1774               l_application_name := FND_GLOBAL.APPLICATION_NAME;
1775 
1776              select nvl(application_id,724)
1777               into l_application_id
1778                  from fnd_application_vl
1779                  where application_name =l_application_name;
1780 
1781     IF (arg_loaded_ext_repair_orders > 0) THEN
1782 
1783                  Lv_sql_stmt :='BEGIN  '
1784 			|| ' MRP_CL_FUNCTION.SUBMIT_CR'||arg_dblink
1785 		  ||'(:l_user_name,:l_resp_name,:l_application_name,:l_application_id,:l_batch_id ,:l_conc_req_short_name ,:l_conc_request_desc,'
1786   		||':l_owning_appl_short_name,  128,'
1787       ||' :l_request_id );'
1788       ||' END; ';
1789 
1790 	    --Execute Immediate lv_sql_stmt using  l
1791 
1792    	 EXECUTE IMMEDIATE lv_sql_stmt
1793                   USING
1794                   IN l_user_name,
1795                   IN l_resp_name,
1796                   IN l_application_name,IN l_application_id,
1797                   IN l_batch_number4,'MSCRLERO' ,'Release ERO To Source','MSC',
1798                   IN OUT l_request_id;
1799     END IF;
1800 
1801   END IF ;
1802 COMMIT WORK;
1803 
1804    /*end of Bug 6051361 */
1805 /* DWK */
1806     /* For Creating Internal Sales Orders and
1807            Rescheduling Internal Sales Orders */
1808 
1809     --- ISO/IR LOAD/Reschedule ---
1810     IF ( lv_wf_load_type = DRP_REQ_LOAD   OR
1811          lv_wf_load_type = DRP_REQ_RESCHED OR
1812    --      lv_wf_load_type = ASCP_IREQ_RESCHED OR		 -- IR/ISO resch Proj
1813          lv_wf= SYS_NO   OR
1814          lv_wf =3) THEN
1815 
1816        /* In case arg_mode is NULL then lv_wf = SYS_NO, and there will be
1817           no transaction id nor lv_wf_load_type.  Therefore, we need to call
1818           the POPULATE_ISO_IN_SOURCE twice. One for new iso, another for re-sche
1819           of ISO with hardcoded load_type. */
1820 
1821        lv_sql_stmt := ' SELECT mrp_workbench_query_s.nextval from dual'|| arg_dblink;
1822 
1823        OPEN c_batch_number for lv_sql_stmt;
1824        FETCH c_batch_number INTO l_batch_number1;
1825        CLOSE c_batch_number;
1826 
1827        OPEN c_batch_number for lv_sql_stmt;
1828        FETCH c_batch_number INTO l_batch_number2;
1829        CLOSE c_batch_number;
1830 
1831        OPEN c_plan_type(arg_plan_id);
1832        FETCH c_plan_type INTO l_plan_type;
1833        CLOSE c_plan_type;
1834 
1835      IF ((l_plan_type = 5) OR (l_plan_type = 8)) THEN
1836        IF  (lv_wf = SYS_NO) and (lv_wf_load_type IS NULL) THEN
1837          /* For releasing planned arrival as int req/iso */
1838 
1839               POPULATE_ISO_IN_SOURCE(
1840                                   l_dblink  => arg_dblink,
1841                                   l_arg_po_batch_number => l_batch_number1 ,
1842                                   l_arg_owning_instance => arg_owning_instance,
1843                                   l_arg_po_group_by     => arg_po_group_by,
1844                                   l_arg_plan_id         => arg_plan_id,
1845                                   l_arg_log_org_id      => arg_log_org_id,
1846                                   l_arg_owning_org_id   => arg_owning_org_id,
1847                                   l_arg_org_instance    => arg_org_instance,
1848                                   l_arg_mode            => arg_mode,
1849                                   l_arg_transaction_id  => arg_transaction_id,
1850                                   arg_loaded_int_reqs   => l_new_iso_num,
1851                                   arg_resched_int_reqs  => l_reschedule_iso_num,
1852                                   p_load_type           => DRP_REQ_LOAD);
1853                                arg_loaded_int_reqs := l_new_iso_num;
1854 
1855                   IF (arg_loaded_int_reqs > 0) THEN
1856                   COMMIT WORK;
1857 
1858 		             if lv_inst_type <> G_INS_OTHER then
1859                   lv_sql_stmt:= ' BEGIN '
1860                           ||' MRP_CREATE_SCHEDULE_ISO.MSC_RELEASE_ISO'||arg_dblink
1861                           ||'(:l_batch_number, 32, '
1862                           ||' :arg_int_req_load_id,:arg_int_req_resched_id);' ||
1863                         ' END;';
1864                    EXECUTE IMMEDIATE lv_sql_stmt
1865                     USING IN l_batch_number1,
1866                         IN OUT arg_int_req_load_id, IN OUT lv_temp;
1867 		   end if;
1868 		   END IF;/* 9107066*/
1869 
1870               /* for rescheduling int reqs by updating iso */
1871 
1872               POPULATE_ISO_IN_SOURCE(
1873                                   l_dblink  => arg_dblink,
1874                                   l_arg_po_batch_number => l_batch_number2 ,
1875                                   l_arg_owning_instance => arg_owning_instance,
1876                                   l_arg_po_group_by     => arg_po_group_by,
1877                                   l_arg_plan_id         => arg_plan_id,
1878                                   l_arg_log_org_id      => arg_log_org_id,
1879                                   l_arg_owning_org_id   => arg_owning_org_id,
1880                                   l_arg_org_instance    => arg_org_instance,
1881                                   l_arg_mode            => arg_mode,
1882                                   l_arg_transaction_id  => arg_transaction_id,
1883                                   arg_loaded_int_reqs   => l_new_iso_num,
1884                                   arg_resched_int_reqs  => l_reschedule_iso_num,
1885                                   p_load_type           => DRP_REQ_RESCHED);
1886                                   arg_resched_int_reqs := l_reschedule_iso_num;
1887 
1888                     IF (arg_resched_int_reqs > 0) THEN
1889                     COMMIT WORK;
1890                     if lv_inst_type <> G_INS_OTHER then
1891                     lv_wf_load_type := DRP_REQ_RESCHED;
1892                     lv_sql_stmt:= ' BEGIN '
1893                           ||' MRP_CREATE_SCHEDULE_ISO.MSC_RELEASE_ISO'||arg_dblink
1894                           ||'(:l_batch_number, 64, '
1895                           ||' :arg_int_req_load_id,:arg_int_req_resched_id);' ||
1896                         ' END;';
1897                     EXECUTE IMMEDIATE lv_sql_stmt
1898                      USING IN l_batch_number2,
1899                         IN OUT lv_temp, IN OUT arg_int_req_resched_id;
1900                      end if;
1901                     END IF;
1902 
1903 
1904        ELSE
1905 
1906           /* for lv_wf = WF. creates ireq/iso if lv_wf_type =  DRP_REQ_LOAD
1907              or reschedules iso if lv_wf_type = DRP_REQ_RESCHED */
1908 
1909                     POPULATE_ISO_IN_SOURCE(
1910                                   l_dblink  => arg_dblink,
1911                                   l_arg_po_batch_number => l_batch_number1 ,
1912                                   l_arg_owning_instance => arg_owning_instance,
1913                                   l_arg_po_group_by     => arg_po_group_by,
1914                                   l_arg_plan_id         => arg_plan_id,
1915                                   l_arg_log_org_id      => arg_log_org_id,
1916                                   l_arg_owning_org_id   => arg_owning_org_id,
1917                                   l_arg_org_instance    => arg_org_instance,
1918                                   l_arg_mode            => arg_mode,
1919                                   l_arg_transaction_id  => arg_transaction_id,
1920                                   arg_loaded_int_reqs   => arg_loaded_int_reqs,
1921                                   arg_resched_int_reqs  => arg_resched_int_reqs,
1922                                   p_load_type           => lv_wf_load_type);
1923 
1924                  IF (arg_loaded_int_reqs > 0 OR arg_resched_int_reqs > 0) THEN
1925                  COMMIT WORK;
1926 
1927 		          if lv_inst_type <> G_INS_OTHER then
1928                  lv_sql_stmt:= ' BEGIN '
1929                           ||' MRP_CREATE_SCHEDULE_ISO.MSC_RELEASE_ISO'||arg_dblink
1930                           ||'(:l_batch_number, :lv_wf_load_type, '
1931                           ||' :arg_int_req_load_id,:arg_int_req_resched_id);' ||
1932                         ' END;';
1933                   EXECUTE IMMEDIATE lv_sql_stmt
1934                      USING IN l_batch_number1, IN lv_wf_load_type,
1935                         IN OUT arg_int_req_load_id, IN OUT arg_int_req_resched_id;
1936                 end if;
1937                 END IF; /* 9107066*/
1938 
1939 
1940 	   END IF;
1941        -- IR/ISO resch Proj Start
1942 	  ELSIF ((l_plan_type in (1,2,3, 101, 102, 103))  -- 9072267
1943              AND
1944              (l_apps_ver >= MSC_UTIL.G_APPS121)) THEN
1945 		-- Call Need to reschedule ISOs that have corresponding IRs in
1946 		-- the same plan. Here we need to call the same OM routine as
1947 		-- the DRP code for a new load_type ASCP_IREQ_RESCHED
1948         -- BUG 9072267. Need to check for RP plan types
1949 
1950 		IF  (lv_wf = SYS_NO) and (lv_wf_load_type IS NULL) THEN
1951 
1952 		   POPULATE_ISO_IN_SOURCE(
1953                                   l_dblink  => arg_dblink,
1954                                   l_arg_po_batch_number => l_batch_number2 ,
1955                                   l_arg_owning_instance => arg_owning_instance,
1956                                   l_arg_po_group_by     => arg_po_group_by,
1957                                   l_arg_plan_id         => arg_plan_id,
1958                                   l_arg_log_org_id      => arg_log_org_id,
1959                                   l_arg_owning_org_id   => arg_owning_org_id,
1960                                   l_arg_org_instance    => arg_org_instance,
1961                                   l_arg_mode            => arg_mode,
1962                                   l_arg_transaction_id  => arg_transaction_id,
1963                                   arg_loaded_int_reqs   => l_new_iso_num,
1964                                   arg_resched_int_reqs  => l_reschedule_iso_num,
1965                                   p_load_type           => DRP_REQ_RESCHED);
1966 
1967 		   arg_resched_int_reqs := l_reschedule_iso_num;
1968 
1969 		   IF (arg_resched_int_reqs > 0) THEN
1970 			  COMMIT WORK;
1971 			  lv_wf_load_type := DRP_REQ_RESCHED;
1972 			  -- Here while calling the Source API, we still pass the
1973 			  -- load_type as DRP_REQ_RESCHED (64)
1974 
1975 			  lv_sql_stmt:= ' BEGIN '
1976                           ||' MRP_CREATE_SCHEDULE_ISO.MSC_RELEASE_ISO'||arg_dblink
1977                           ||'(:l_batch_number, 64, '
1978                           ||' :arg_int_req_load_id,:arg_int_req_resched_id);' ||
1979                         ' END;';
1980 
1981 			  EXECUTE IMMEDIATE lv_sql_stmt
1982 				USING IN l_batch_number2,
1983 				IN OUT lv_temp, IN OUT arg_int_req_resched_id;
1984 
1985 		   END IF;
1986 		 ELSIF (lv_wf_load_type = DRP_REQ_RESCHED ) THEN
1987 
1988 		   POPULATE_ISO_IN_SOURCE(
1989                                   l_dblink  => arg_dblink,
1990                                   l_arg_po_batch_number => l_batch_number1 ,
1991                                   l_arg_owning_instance => arg_owning_instance,
1992                                   l_arg_po_group_by     => arg_po_group_by,
1993                                   l_arg_plan_id         => arg_plan_id,
1994                                   l_arg_log_org_id      => arg_log_org_id,
1995                                   l_arg_owning_org_id   => arg_owning_org_id,
1996                                   l_arg_org_instance    => arg_org_instance,
1997                                   l_arg_mode            => arg_mode,
1998                                   l_arg_transaction_id  => arg_transaction_id,
1999                                   arg_loaded_int_reqs   => arg_loaded_int_reqs,
2000                                   arg_resched_int_reqs  => arg_resched_int_reqs,
2001                                   p_load_type           => lv_wf_load_type);
2002 
2003 
2004 		   IF (arg_loaded_int_reqs > 0 OR arg_resched_int_reqs > 0) THEN
2005 
2006 			  COMMIT WORK;
2007 			  lv_wf_load_type := DRP_REQ_RESCHED;
2008 
2009 			  lv_sql_stmt:= ' BEGIN '
2010                           ||' MRP_CREATE_SCHEDULE_ISO.MSC_RELEASE_ISO'||arg_dblink
2011                           ||'(:l_batch_number, :lv_wf_load_type, '
2012                           ||' :arg_int_req_load_id,:arg_int_req_resched_id);' ||
2013                         ' END;';
2014 
2015 			  EXECUTE IMMEDIATE lv_sql_stmt
2016 				USING IN l_batch_number1, IN lv_wf_load_type,
2017 				IN OUT arg_int_req_load_id, IN OUT arg_int_req_resched_id;
2018 
2019 		   END IF;
2020 
2021     -- IR/ISO resch Proj End
2022      END IF;
2023     END IF;
2024   END IF;
2025 -- Timestamp for RP Release
2026    v_rp_time := 86399/86400;
2027    SELECT  DECODE(NVL(fnd_profile.value('MSC_PO_DEFAULT_TIME_STAMP') ,'1'), '1','N', 'Y')
2028     INTO    l_timestamp_profile_val
2029     FROM    DUAL;
2030 
2031     select decode((mp.daily_material_constraints+ mp.daily_resource_constraints+
2032                   mp.weekly_material_constraints+mp.weekly_resource_constraints+
2033                   mp.period_material_constraints+mp.period_resource_constraints),12,
2034                   1,0) into v_unconstrained_plan
2035     from msc_plans mp
2036     where plan_id = arg_plan_id;
2037 
2038     if((l_timestamp_profile_val = 'Y') AND (v_unconstrained_plan = 1)) then
2039     v_time_stamp := 86399/86400;
2040     else
2041     v_time_stamp := 0;
2042     end if;
2043 
2044     --- WIP_DIS_MASS_LOAD ---
2045     IF lv_wf= SYS_NO OR
2046        lv_wf_load_type= WIP_DIS_MASS_LOAD OR
2047        lv_wf =3 THEN
2048 
2049        arg_loaded_jobs:= load_wip_discrete_jobs
2050                              ( arg_plan_id,
2051                                arg_log_org_id,
2052                                arg_org_instance,
2053                                arg_owning_org_id,
2054                                arg_owning_instance,
2055                                arg_user_id,
2056                                arg_wip_group_id,
2057                                arg_mode,
2058                                arg_transaction_id,
2059                                l_apps_ver );
2060 
2061        arg_loaded_lot_jobs:= load_osfm_lot_jobs
2062                              ( arg_plan_id,
2063                                arg_log_org_id,
2064                                arg_org_instance,
2065                                arg_owning_org_id,
2066                                arg_owning_instance,
2067                                arg_user_id,
2068                                arg_wip_group_id,
2069                                arg_mode,
2070                                arg_transaction_id,
2071                                l_apps_ver );
2072 
2073 
2074     END IF;
2075 
2076     --- WIP_DIS_MASS_RESCHEDULE ---
2077     IF lv_wf= SYS_NO OR
2078        lv_wf_load_type= WIP_DIS_MASS_RESCHEDULE or
2079    --  lv_wf_load_type= EAM_RESCHEDULE_WORK_ORDER OR -- 21 bug# 4524589
2080        lv_wf_load_type = 6 THEN
2081 
2082        arg_resched_jobs:= reschedule_wip_discrete_jobs
2083                              ( arg_plan_id,
2084                                arg_log_org_id,
2085                                arg_org_instance,
2086                                arg_owning_org_id,
2087                                arg_owning_instance,
2088                                arg_user_id,
2089                                arg_wip_group_id,
2090                                arg_mode,
2091                                arg_transaction_id,
2092                                l_apps_ver,
2093 			       WIP_DIS_MASS_RESCHEDULE
2094 			      );
2095 
2096        arg_resched_lot_jobs := reschedule_osfm_lot_jobs
2097                              ( arg_plan_id,
2098                                arg_log_org_id,
2099                                arg_org_instance,
2100                                arg_owning_org_id,
2101                                arg_owning_instance,
2102                                arg_user_id,
2103                                arg_wip_group_id,
2104                                arg_mode,
2105                                arg_transaction_id );
2106 
2107         -- dsr: added the following new api call
2108     /*  arg_resched_eam_jobs:= reschedule_wip_discrete_jobs
2109                              ( arg_plan_id,
2110                                arg_log_org_id,
2111                                arg_org_instance,
2112                                arg_owning_org_id,
2113                                arg_owning_instance,
2114                                arg_user_id,
2115                                arg_wip_group_id,
2116                                arg_mode,
2117                                arg_transaction_id,
2118                                l_apps_ver ,
2119 			       21 -- dsr arg_load_type eam rescheduled wo
2120 			     );    */
2121 
2122     END IF;
2123 
2124     --- WIP_REP_MASS_LOAD ---
2125     IF lv_wf= SYS_NO OR
2126        lv_wf_load_type= WIP_REP_MASS_LOAD OR
2127        lv_wf = 3 THEN
2128 
2129        arg_loaded_scheds:= load_repetitive_schedules
2130                              ( arg_plan_id,
2131                                arg_log_org_id,
2132                                arg_org_instance,
2133                                arg_owning_org_id,
2134                                arg_owning_instance,
2135                                arg_user_id,
2136                                arg_wip_group_id,
2137                                arg_mode,
2138                                arg_transaction_id );
2139     END IF;
2140 
2141     -- For RP Release , we need to set the timestamp as 23:59:59
2142     -- And for this if any of the WIP jobs created or rescheduled, then
2143     -- We call the procedure to set the timestamp
2144     if (( v_plan_type  = 101) OR (v_plan_type = 102) OR (v_plan_type = 103))
2145     then
2146       if (  nvl(arg_loaded_jobs,0)
2147           + nvl(arg_loaded_lot_jobs,0)
2148           + nvl(arg_resched_jobs,0)
2149           + nvl(arg_resched_lot_jobs,0)) > 0
2150       then
2151            SET_RP_TIMESTAMP_WIP( arg_wip_group_id);
2152       end if;
2153     end if;
2154 
2155     --- PO_MASS_LOAD ---
2156 
2157 
2158     IF lv_wf= SYS_NO OR
2159        lv_wf_load_type= PO_MASS_LOAD OR
2160        lv_wf = 3 THEN
2161 
2162 
2163        arg_loaded_reqs:= load_po_requisitions
2164                              ( arg_plan_id,
2165                                arg_log_org_id,
2166                                arg_org_instance,
2167                                arg_owning_org_id,
2168                                arg_owning_instance,
2169                                arg_user_id,
2170                                arg_po_group_by,
2171                                arg_po_batch_number,
2172                                arg_mode,
2173                                arg_transaction_id );
2174     END IF;
2175 
2176     --- PO_MASS_RESCHEDULE ---
2177     IF lv_wf= SYS_NO OR
2178 	  (lv_wf_load_type= PO_MASS_RESCHEDULE OR
2179 	   (lv_wf_load_type = DRP_REQ_RESCHED and
2180 		l_apps_ver <= MSC_UTIL.G_APPS120)) THEN   -- IR/ISO resch Proj
2181 
2182        arg_resched_reqs:= reschedule_po
2183                              ( arg_plan_id,
2184                                arg_log_org_id,
2185                                arg_org_instance,
2186                                arg_owning_org_id,
2187                                arg_owning_instance,
2188                                arg_user_id,
2189                                arg_po_group_by,
2190                                arg_mode,
2191                                arg_transaction_id );
2192     END IF;
2193     -- For RP Release
2194     if (( v_plan_type  = 101) OR (v_plan_type = 102) OR (v_plan_type = 103))
2195     then
2196       if (  nvl(arg_loaded_reqs,0)
2197           + nvl(arg_resched_reqs,0)
2198          ) > 0
2199       then
2200            SET_RP_TIMESTAMP_PO (arg_po_batch_number);
2201       end if;
2202     end if;
2203 
2204      /*USAF*/
2205       --- CMRO_EAM_MASS_LOAD ---
2206     IF ( lv_wf_load_type = CMRO_EAM_MASS_LOAD OR lv_wf= SYS_NO OR lv_wf =3) AND
2207         l_apps_ver >= MSC_UTIL.G_EAM_CMRO_SUP_VER THEN
2208 
2209           OPEN c_plan_type(arg_plan_id);
2210           FETCH c_plan_type INTO l_plan_type;
2211           CLOSE c_plan_type;
2212 
2213            IF l_plan_type = 1 THEN
2214                IF(MSC_UTIL.G_CMRO_EAM_INT_ENABLED = 'Y') THEN
2215 
2216                  load_eam_cmro_jobs( p_arg_plan_id => arg_plan_id,
2217                                      p_arg_log_org_id => arg_log_org_id,
2218                                      p_arg_org_instance => arg_org_instance,
2219                                      p_arg_owning_org_id => arg_owning_org_id,
2220                                      p_arg_owning_instance => arg_owning_instance,
2221                                      p_arg_user_id => arg_user_id,
2222                                      p_arg_wip_group_id => arg_wip_group_id,
2223                                      p_arg_mode => arg_mode,
2224                                      p_arg_transaction_id => arg_transaction_id,
2225                                      p_l_apps_ver => l_apps_ver,
2226                                      p_loaded_eam_jobs => arg_loaded_eam_jobs,
2227                                      p_loaded_cmro_jobs => arg_loaded_cmro_jobs );
2228 
2229                  arg_resched_eam_jobs := reschedule_eam_jobs
2230                                                   ( arg_plan_id
2231                                                         , arg_log_org_id
2232                                                         , arg_org_instance
2233                                                         , arg_owning_org_id
2234                                                         , arg_owning_instance
2235                                                         , arg_user_id
2236                                                         , arg_wip_group_id
2237                                                         , arg_mode
2238                                                         , arg_transaction_id
2239                                                         , l_apps_ver
2240                                                         , CMRO_EAM_RESCHED
2241                                                         ) ;
2242                  arg_resched_cmro_jobs := reschedule_cmro_jobs
2243                                                   ( arg_plan_id
2244                                                         , arg_log_org_id
2245                                                         , arg_org_instance
2246                                                         , arg_owning_org_id
2247                                                         , arg_owning_instance
2248                                                         , arg_user_id
2249                                                         , arg_wip_group_id
2250                                                         , arg_mode
2251                                                         , arg_transaction_id
2252                                                         , l_apps_ver
2253                                                         , CMRO_EAM_RESCHED
2254                                                         ) ;
2255 
2256            END IF;
2257      END IF;
2258  END IF;
2259     MSC_RELEASE_HOOK.EXTEND_RELEASE
2260                  (lv_error_buf,
2261                   lv_ret_code,
2262                  arg_dblink  => arg_dblink,
2263                  arg_plan_id => arg_plan_id,
2264                  arg_log_org_id => arg_log_org_id,
2265                  arg_org_instance => arg_org_instance,
2266                  arg_owning_org_id => arg_owning_org_id,
2267                  arg_owning_instance => arg_owning_instance,
2268                  arg_compile_desig => arg_compile_desig,
2269                  arg_user_id => arg_user_id,
2270                  arg_po_group_by => arg_po_group_by,
2271                  arg_po_batch_number => arg_po_batch_number,
2272                  arg_wip_group_id => arg_wip_group_id,
2273     ----------------------------------------------- Number of Loaded Orders
2274                  arg_loaded_jobs   => arg_loaded_jobs,
2275                  arg_loaded_lot_jobs => arg_loaded_lot_jobs,
2276                  arg_resched_lot_jobs => arg_loaded_lot_jobs,
2277                  arg_loaded_reqs   => arg_loaded_reqs,
2278                  arg_loaded_scheds => arg_loaded_scheds,
2279                  arg_resched_jobs  => arg_resched_jobs,
2280                  arg_int_repair_orders=>arg_loaded_int_repair_orders,
2281                  arg_ext_repair_orders=>arg_loaded_ext_repair_orders,
2282                  --arg_loaded_eam_jobs=>arg_loaded_eam_jobs,
2283     ----------------------------------------------- Request IDs
2284                  arg_resched_reqs =>   arg_resched_reqs,
2285                  arg_wip_req_id =>     arg_wip_req_id,
2286                  arg_osfm_req_id =>    arg_osfm_req_id,
2287                  arg_req_load_id =>    arg_req_load_id,
2288                  arg_req_resched_id => arg_req_resched_id,
2289                  arg_int_repair_Order_id=>arg_int_repair_orders_id,
2290                  arg_ext_repair_Order_id=>arg_ext_repair_orders_id,
2291                  --arg_loaded_eam_req_id=>arg_loaded_eam_req_id,
2292     -------------------------------------------------------------
2293                  arg_mode => arg_mode,
2294                  arg_transaction_id => arg_transaction_id,
2295                  l_apps_ver   =>  l_apps_ver);
2296 
2297 
2298        IF lv_ret_code=-1 THEN /* custom hook returned error*/
2299         	 FND_MESSAGE.SET_NAME('MSC','MSC_ERROR_REL_CUSTOM_HOOK');
2300      			 RAISE_APPLICATION_ERROR(-20000,FND_MESSAGE.GET||lv_error_buf,TRUE);
2301    			END IF;
2302 
2303 
2304 commit;
2305 
2306     -- call (remote) procedures to submit the concuncurrent request --
2307 
2308  IF v_curr_instance_type IN ( G_INS_DISCRETE, G_INS_PROCESS, G_INS_MIXED) THEN
2309 
2310     IF arg_loaded_jobs+arg_resched_jobs+arg_loaded_scheds > 0  THEN
2311 
2312         lv_sql_stmt:=
2313          'BEGIN'
2314         ||' MRP_AP_REL_PLAN_PUB.LD_WIP_JOB_SCHEDULE_INTERFACE'||arg_dblink
2315                   ||'( :arg_wip_req_id );'
2316         ||' END;';
2317 
2318         EXECUTE IMMEDIATE lv_sql_stmt
2319                 USING OUT arg_wip_req_id;
2320 
2321       END IF;
2322 
2323 
2324       IF arg_loaded_lot_jobs + nvl(arg_resched_lot_jobs,0) > 0 then
2325 
2326         lv_sql_stmt:=
2327          'BEGIN'
2328         ||' MRP_AP_REL_PLAN_PUB.LD_LOT_JOB_SCHEDULE_INTERFACE'||arg_dblink
2329                   ||'( :arg_osfm_req_id );'
2330         ||' END;';
2331 
2332         EXECUTE IMMEDIATE lv_sql_stmt
2333                 USING OUT arg_osfm_req_id;
2334 
2335 
2336        END IF;
2337 
2338       /*USAF*/ /*We call the same procedure for loaded EAM and CMRO jobs
2339      and not for reschedules */
2340       IF (nvl(arg_loaded_eam_jobs,0) +
2341           nvl(arg_loaded_cmro_jobs,0)) > 0
2342 
2343          then
2344 
2345         lv_sql_stmt:=
2346          'BEGIN'
2347         ||' MRP_AP_REL_PLAN_PUB.LD_EAM_CMRO_SCHEDULE_INTERFACE'||arg_dblink
2348                   ||'( :arg_loaded_eam_req_id ,'
2349                   ||' :arg_loaded_cmro_req_id,'
2350                   ||' :l_plan_completion_date    );'
2351 -- need to also pass  plan_completion_date
2352         ||' END;';
2353 
2354         EXECUTE IMMEDIATE lv_sql_stmt
2355                 USING OUT arg_loaded_eam_req_id,
2356                       OUT arg_loaded_cmro_req_id,
2357                       IN  l_plan_completion_date
2358                          ;
2359 
2360 
2361        END IF;
2362 
2363        /*For reschedule of eam jobs for USAF */
2364        IF  nvl(arg_resched_eam_jobs,0) > 0 then
2365 
2366         lv_sql_stmt:=
2367          'BEGIN'
2368         ||' MRP_AP_REL_PLAN_PUB.LD_EAM_RESCHEDULE_INTERFACE'||arg_dblink
2369                   ||'( :arg_eam_req_id );'
2370 
2371 
2372         ||' END;';
2373 
2374         EXECUTE IMMEDIATE lv_sql_stmt
2375                 USING OUT arg_eam_req_id;
2376 
2377        END IF;
2378 
2379               /*For reschedule of cmro jobs for USAF */
2380        IF  nvl(arg_resched_cmro_jobs,0) > 0 then
2381 
2382         lv_sql_stmt:=
2383          'BEGIN'
2384         ||' MRP_AP_REL_PLAN_PUB.LD_CMRO_RESCHEDULE_INTERFACE'||arg_dblink
2385                   ||'( :arg_resched_cmro_req_id );'
2386 
2387 
2388         ||' END;';
2389 
2390         EXECUTE IMMEDIATE lv_sql_stmt
2391                 USING OUT arg_resched_cmro_req_id;
2392 
2393        END IF;
2394 
2395        -- dsr: added the following lines for eam
2396      /* IF nvl(arg_resched_eam_jobs,0) > 0 then
2397 
2398         lv_sql_stmt:=
2399          'BEGIN'
2400         ||' MRP_AP_REL_PLAN_PUB.LD_EAM_RESCHEDULE_JOBS'||arg_dblink
2401                   ||'(:arg_eam_req_id );'
2402         ||' END;';
2403 
2404         EXECUTE IMMEDIATE lv_sql_stmt
2405                 USING OUT arg_eam_req_id;
2406 
2407        END IF; */
2408 
2409 /*         IF fnd_profile.value('MSC_RETAIN_RELEASED_DATA') ='N' THEN
2410              DELETE msc_wip_job_schedule_interface
2411              WHERE sr_instance_id= arg_org_instance;
2412 
2413           DELETE MSC_WIP_JOB_DTLS_INTERFACE
2414            WHERE sr_instance_id= arg_org_instance;
2415         END IF;
2416 */
2417     IF arg_loaded_reqs > 0 THEN
2418       DECLARE po_group_by_name VARCHAR2(10);
2419       BEGIN
2420         IF arg_po_group_by = 1 THEN
2421           po_group_by_name := 'ALL';
2422         ELSIF arg_po_group_by = 2 THEN
2423           po_group_by_name := 'ITEM';
2424         ELSIF arg_po_group_by = 3 THEN
2425           po_group_by_name := 'BUYER';
2426         ELSIF arg_po_group_by = 4 THEN
2427           po_group_by_name := 'PLANNER';
2428         ELSIF arg_po_group_by = 5 THEN
2429           po_group_by_name := 'VENDOR';
2430         ELSIF arg_po_group_by = 6 THEN
2431           po_group_by_name := 'ONE-EACH';
2432         ELSIF arg_po_group_by = 7 THEN
2433           po_group_by_name := 'CATEGORY';
2434         ELSIF arg_po_group_by = 8 THEN
2435           po_group_by_name := 'LOCATION';
2436         END IF;
2437 
2438         lv_sql_stmt:=
2439            'BEGIN'
2440          ||' MRP_AP_REL_PLAN_PUB.LD_PO_REQUISITIONS_INTERFACE'||arg_dblink
2441                   ||'( :po_group_by_name,'
2442                   ||'  :arg_req_load_id );'
2443          ||' END;';
2444 
2445          EXECUTE IMMEDIATE lv_sql_stmt
2446                  USING  IN po_group_by_name,
2447                        OUT arg_req_load_id;
2448           IF fnd_profile.value('MSC_RETAIN_RELEASED_DATA') ='N' THEN
2449             DELETE MSC_PO_REQUISITIONS_INTERFACE
2450             WHERE sr_instance_id= arg_org_instance;
2451         END IF;
2452       END;
2453     END IF;
2454 
2455     IF arg_resched_reqs > 0 THEN
2456 
2457        /*
2458        OPEN c_plan_type(arg_plan_id);
2459        FETCH c_plan_type INTO l_plan_type;
2460        CLOSE c_plan_type;
2461 
2462        IF l_plan_type = 5 THEN
2463           lv_sql_stmt :=
2464             'BEGIN ' ||
2465             'MRP_PO_RESCHEDULE.LAUNCH_RESCHEDULE_PO'||arg_dblink||
2466             '(:arg_req_resched_id); ' ||
2467             'END;';
2468          ELSE
2469          */
2470           lv_sql_stmt:=
2471             'BEGIN'
2472             ||' MRP_AP_REL_PLAN_PUB.LD_PO_RESCHEDULE_INTERFACE'||arg_dblink
2473             ||'( :arg_req_resched_id);'
2474             ||' END;';
2475       -- END IF;
2476 
2477           EXECUTE IMMEDIATE lv_sql_stmt
2478                   USING OUT arg_req_resched_id;
2479 
2480 /*             IF fnd_profile.value('MSC_RETAIN_RELEASED_DATA') ='N' THEN
2481                 DELETE MSC_PO_RESCHEDULE_INTERFACE
2482                  WHERE sr_instance_id= arg_org_instance;
2483             END IF;
2484 */
2485     END IF;
2486 
2487  ELSIF v_curr_instance_type in (G_INS_OTHER, G_INS_EXCHANGE) THEN
2488    IF fnd_profile.value('MSC_RETAIN_RELEASED_DATA') ='N' THEN
2489       lv_sql_stmt :=
2490             ' BEGIN'
2491           ||' MSC_A2A_XML_WF.LEGACY_RELEASE (:p_arg_org_instance);'
2492           ||' END;';
2493 
2494       EXECUTE IMMEDIATE lv_sql_stmt USING  arg_org_instance;
2495    END IF;
2496  END IF; -- v_curr_instance_type
2497 
2498     --- Update the released orders.
2499 
2500     IF  arg_loaded_jobs > 0   OR
2501         arg_resched_jobs > 0  OR
2502         arg_loaded_lot_jobs > 0   OR
2503         arg_resched_lot_jobs > 0  OR
2504         arg_loaded_scheds > 0 OR
2505         arg_loaded_reqs > 0   OR
2506         arg_resched_reqs > 0  OR
2507         arg_loaded_int_reqs > 0 OR
2508         arg_resched_int_reqs > 0
2509         OR arg_loaded_int_repair_orders >0
2510         OR arg_loaded_ext_repair_orders >0
2511         OR arg_loaded_eam_jobs > 0
2512         OR arg_resched_eam_jobs > 0  THEN
2513 
2514         -- For RP Auto Release populate the table
2515         -- MSC_RP_RELEASED_ORDERS
2516         IF ( v_rp_plan  = 1) THEN
2517           BEGIN
2518           FOR c_rec IN c_rp_rel_ord
2519           LOOP
2520            UPDATE  msc_rp_released_orders
2521             SET    plan_id = c_rec.plan_id,
2522                    transaction_id = c_rec.transaction_id,
2523                    implemented_quanity = NVL(implemented_quanity, 0) +
2524                                          c_rec.implement_quantity,
2525                    implemented_date = c_rec.implement_date,
2526                    plan_version  = c_rec.plan_version,
2527                    last_update_date = c_rec.last_update_date,
2528                    last_updated_by= c_rec.last_updated_by,
2529                    creation_date=c_rec.creation_date,
2530                    created_by=c_rec.created_by,
2531                    last_update_login=c_rec.last_update_login,
2532                    release_status = c_rec.rp_release_status
2533             WHERE plan_id=arg_plan_id
2534             AND transaction_id=c_rec.transaction_id;
2535 
2536             IF SQL%NOTFOUND THEN
2537 
2538               INSERT INTO msc_rp_released_orders
2539                 (plan_id,
2540                 transaction_id,
2541                 implemented_quanity,
2542                 implemented_date,
2543                 plan_version,
2544                 last_update_date,
2545                 last_updated_by,
2546                 creation_date,
2547                 created_by,
2548                 last_update_login,
2549                 release_status )
2550               VALUES
2551                 (arg_plan_id
2552                  , c_rec.transaction_id
2553                  , c_rec.implement_quantity
2554                  , c_rec.implement_date
2555                  , c_rec.plan_version
2556                  , c_rec.last_update_date
2557                  , c_rec.last_updated_by
2558                  , c_rec.creation_date
2559                  , c_rec.created_by
2560                  , c_rec.last_update_login
2561                  , c_rec.rp_release_status
2562                   );
2563             END IF;
2564           END LOOP;
2565           EXCEPTION
2566             WHEN OTHERS THEN
2567             /* Error handling here */
2568 
2569             raise;
2570             RETURN;
2571           END;
2572         END IF;
2573         IF lv_wf= SYS_YES THEN
2574 
2575            UPDATE MSC_SUPPLIES
2576               SET implement_demand_class = NULL,
2577                   implement_date = NULL,
2578                   implement_quantity = NULL,
2579                   implement_firm = NULL,
2580                   implement_wip_class_code = NULL,
2581                   implement_job_name = NULL,
2582                   implement_status_code = NULL,
2583                   implement_location_id = NULL,
2584                   implement_source_org_id = NULL,
2585                   implement_supplier_id = NULL,
2586                   implement_supplier_site_id = NULL,
2587                   implement_project_id = NULL,
2588                   implement_task_id = NULL,
2589                   release_status = decode(sign(v_plan_type-100),1,
2590                                    decode(release_status,11,21,
2591                                                          12,22,
2592                                                          13,23,
2593                                                          release_status),
2594                                    NULL), -- For RP Release
2595                   load_type = NULL,
2596                   implement_as = NULL,
2597                   implement_unit_number = NULL,
2598                   implement_schedule_group_id = NULL,
2599                   implement_build_sequence = NULL,
2600                   implement_line_id = NULL,
2601                   implement_alternate_bom = NULL,
2602                   implement_dock_date = NULL,
2603                   implement_ship_date = NULL,
2604                   implement_employee_id = NULL,
2605                   implement_alternate_routing = NULL,
2606                   implemented_quantity = nvl(implemented_quantity, 0) + nvl(quantity_in_process,0),
2607                   quantity_in_process = 0,
2608                   implement_ship_method = NULL
2609             WHERE transaction_id= arg_transaction_id
2610               AND plan_id= arg_plan_id
2611 	      AND release_errors IS NULL
2612           and  batch_id = g_batch_id ;
2613 
2614         ELSE
2615 
2616            UPDATE MSC_SUPPLIES
2617               SET implement_demand_class = NULL,
2618                   implement_date = NULL,
2619                   implement_quantity = NULL,
2620                   implement_firm = NULL,
2621                   implement_wip_class_code = NULL,
2622                   implement_job_name = NULL,
2623                   implement_status_code = NULL,
2624                   implement_location_id = NULL,
2625                   implement_source_org_id = NULL,
2626                   implement_supplier_id = NULL,
2627                   implement_supplier_site_id = NULL,
2628                   implement_project_id = NULL,
2629                   implement_task_id = NULL,
2630                   release_status = decode(sign(v_plan_type-100),1,
2631                                    decode(release_status,11,21,
2632                                                          12,22,
2633                                                          13,23,
2634                                                          release_status),
2635                                    NULL), -- For RP Release
2636                   load_type = NULL,
2637                   implement_as = NULL,
2638                   implement_unit_number = NULL,
2639                   implement_schedule_group_id = NULL,
2640                   implement_build_sequence = NULL,
2641                   implement_line_id = NULL,
2642                   implement_alternate_bom = NULL,
2643                   implement_dock_date = NULL,
2644                   implement_ship_date = NULL,
2645                   implement_employee_id = NULL,
2646                   implement_alternate_routing = NULL,
2647                   implemented_quantity = nvl(implemented_quantity, 0) + nvl(quantity_in_process,0),
2648                   quantity_in_process = 0,
2649                   implement_ship_method = NULL,
2650                   releasable = decode (new_order_quantity,
2651                                       nvl(implemented_quantity, 0) +
2652                                       nvl(quantity_in_process,0), releasable,
2653                                       NULL),
2654                   batch_id = decode (new_order_quantity,
2655                                       nvl(implemented_quantity, 0) +
2656                                       nvl(quantity_in_process,0), batch_id,
2657                                       NULL)
2658             WHERE batch_id = g_batch_id
2659               and sr_instance_id = arg_org_instance;
2660         END IF;  -- lv_wf
2661 
2662        IF fnd_profile.value('MSC_RETAIN_RELEASED_DATA') ='N' THEN
2663           DELETE msc_wip_job_schedule_interface
2664             WHERE sr_instance_id= arg_org_instance
2665             AND   NVL(GROUP_ID,-1) = NVL(arg_wip_group_id, -1);
2666 
2667          DELETE MSC_WIP_JOB_DTLS_INTERFACE
2668           WHERE sr_instance_id= arg_org_instance
2669           AND   NVL(GROUP_ID,-1) = NVL(arg_wip_group_id, -1);
2670 
2671 
2672           DELETE MSC_PO_REQUISITIONS_INTERFACE
2673           WHERE sr_instance_id= arg_org_instance
2674           AND   NVL(BATCH_ID, -1) = NVL(arg_po_batch_number, -1);
2675 
2676           DELETE MSC_PO_RESCHEDULE_INTERFACE
2677           WHERE sr_instance_id= arg_org_instance
2678            and batch_id = g_batch_id;
2679       END IF;
2680 
2681     END IF;
2682 
2683 EXCEPTION WHEN OTHERS THEN
2684 rollback; /* Rollback whatever was released */
2685 
2686           DELETE msc_wip_job_schedule_interface
2687           WHERE sr_instance_id= arg_org_instance
2688 	  AND   NVL(GROUP_ID,-1) = NVL(arg_wip_group_id, -1);
2689 
2690           DELETE MSC_WIP_JOB_DTLS_INTERFACE
2691           WHERE sr_instance_id= arg_org_instance
2692 	  AND   NVL(GROUP_ID,-1) = NVL(arg_wip_group_id, -1);
2693 
2694 
2695           DELETE MSC_PO_REQUISITIONS_INTERFACE
2696           WHERE sr_instance_id= arg_org_instance
2697           AND   NVL(BATCH_ID, -1) = NVL(arg_po_batch_number, -1);
2698 
2699 
2700           DELETE MSC_PO_RESCHEDULE_INTERFACE
2701           WHERE sr_instance_id= arg_org_instance
2702           and batch_id = g_batch_id;
2703           /*
2704           IF g_batch_id <> -1 THEN
2705             update msc_supplies set batch_id = NULL where batch_id = g_batch_id;
2706           END IF;
2707           */
2708  commit;
2709  raise;
2710 
2711 END LOAD_MSC_INTERFACE;
2712 
2713 /* DWK
2714 
2715 
2716  PROCEDURE   :  POPULATE_ISO_IN_SOURCE
2717 
2718  -------------------------------------------------------------*/
2719 PROCEDURE POPULATE_ISO_IN_SOURCE(
2720                                   l_dblink              IN  varchar2,
2721                                   l_arg_po_batch_number IN  number,
2722                                   l_arg_owning_instance IN  number,
2723                                   l_arg_po_group_by     IN  number,
2724                                   l_arg_plan_id         IN  number,
2725                                   l_arg_log_org_id      IN  number,
2726                                   l_arg_owning_org_id   IN  number,
2727                                   l_arg_org_instance    IN  number,
2728                                   l_arg_mode            IN  varchar2,
2729                                   l_arg_transaction_id  IN  number,
2730                                   arg_loaded_int_reqs   IN OUT NOCOPY number,
2731                                   arg_resched_int_reqs  IN OUT NOCOPY number,
2732                                   p_load_type           IN  number) IS
2733 
2734 TYPE CurTyp is ref cursor;
2735 
2736 c_row_count  CurTyp;
2737 
2738  CURSOR c_plan_type(p_plan_id number) IS
2739       select plan_type
2740         from msc_plans a
2741         where
2742         plan_id = p_plan_id;
2743 
2744 l_sql_stmt   VARCHAR2(20000):= NULL;
2745 l_sql_cur    varchar2(3000) := NULL;
2746 
2747 l_before_row_count  number := 0;
2748 l_after_row_count   number := 0;
2749 
2750 l_plan_type NUMBER :=0;
2751 BEGIN
2752 
2753    l_sql_cur := ' SELECT count(*) from MRP_ORG_TRANSFER_RELEASE'|| l_dblink ||
2754                 ' WHERE batch_id = :l_arg_po_batch_number' ;
2755 
2756 
2757    OPEN c_row_count for l_sql_cur USING l_arg_po_batch_number;
2758    FETCH c_row_count INTO l_before_row_count;
2759    CLOSE c_row_count;
2760 
2761 
2762 
2763    OPEN c_plan_type(l_arg_plan_id);
2764                     FETCH c_plan_type INTO l_plan_type;
2765    CLOSE c_plan_type;
2766 
2767 
2768 
2769    IF p_load_type = DRP_REQ_LOAD THEN
2770 
2771       l_sql_stmt := ' INSERT INTO MRP_ORG_TRANSFER_RELEASE' || l_dblink ||
2772                  '  ( batch_id, '||
2773                  '    Item_ID, ' ||
2774                  '    SRC_Organization_ID, ' ||
2775                  '    SR_Instance_ID, ' ||
2776                  '    To_Organization_ID, ' ||
2777                  '    To_Sr_Instance_ID, ' ||
2778                  '    Quantity, ' ||
2779                  '    Need_By_Date, ' ||
2780                  '    Ship_date, ' ||
2781                  '    Deliver_to_location_id, ' ||
2782                  '    Deliver_to_requestor_id, ' ||
2783                  '    Preparer_id, ' ||
2784                  '    Uom_code, ' ||
2785                  '    Charge_account_id, ' ||
2786                  '    To_Operating_unit, ' ||
2787                  '    SRC_Operating_unit, ' ||
2788                  '    Load_Type, ' ||
2789                  '    Sales_Order_Line_ID, ' ||
2790                  '    Sales_Order_Number, ' ||
2791                  '    End_item_number, ' ||
2792                  '    firm_demand_flag, ' ||
2793                  '    transaction_id, ' ||
2794                  '    plan_type ,' ||
2795                  '    part_condition ,' || -- bug 9031352
2796                  '    ship_method )  ' ||
2797                  ' (SELECT /*+ FIRST_ROWS */ ' ||
2798                  '    :l_arg_po_batch_number,'||      -- batch id
2799                  '    msi.sr_inventory_item_id, ' ||  -- item id in the source
2800                  '    s.implement_source_org_id,' ||  -- source organization id
2801                  '    s.sr_instance_id,         ' ||  -- source instance id
2802                  '    s.organization_id,        ' ||  -- destination org id
2803                  '    :l_org_instance_id,       ' ||  -- destination instance id
2804                  '    s.implement_quantity,     ' ||  -- quantity
2805 	               '    s.implement_dock_date,    ' ||  -- arrival date in dst org
2806                  '    s.implement_ship_date,    ' ||  -- ship date
2807                  '    s.implement_location_id,  ' ||  -- destination location id
2808                  '    s.implement_employee_id,  ' ||  -- Deliver to requestor id
2809                  '    s.implement_employee_id,  ' ||  -- preparer id
2810                  '    msi.uom_code,             ' ||   -- uom
2811                  '    decode(mp.organization_type, ' || -- Account id
2812                  '           1, nvl(mpp.material_account, '||
2813                  '                 decode( msi1.inventory_asset_flag, ' ||
2814                  '                        ''Y'', mp.material_account, ' ||
2815                  '                 nvl(msi1.expense_account,mp.expense_account))),' ||
2816                  '           -1), '||
2817                   '    mp2.operating_unit,  ' ||        -- src_operating unit
2818                   '    mp.operating_unit, ' ||        -- to_operating_unit
2819                   '   :p_load_type, ' ||             -- load type
2820                   '    null, ' || -- Sales Order Line ID
2821                   '    s.transaction_id, ' || -- Sales Order Number
2822                   '    s.implement_unit_number, ' || --end item number
2823                   '    DECODE(s.implement_firm, 1, ''Y'',''N''), '||
2824                   '    s.transaction_id, ' || -- transaction_id of msc_supplies
2825                   l_plan_type ||',' ||
2826                   '    decode(s.item_type_value,1,''G'',2,''B'',''G''), ' || -- part condition of msc_supplies -- bug 9031352
2827                   '    nvl(s.implement_ship_method,s.ship_method) ' ||
2828                   ' FROM    msc_projects mpp, '||
2829                   '         msc_trading_partners mp, ' ||
2830                   '         msc_trading_partners mp2,' ||
2831                   '         msc_system_items    msi, ' ||
2832                   '         msc_system_items    msi1,' ||
2833                   '         msc_supplies        s, '   ||
2834                   '         msc_plan_organizations_v orgs ' ||
2835                   ' WHERE   mpp.organization_id (+)= s.organization_id ' ||
2836                   '      AND mpp.project_id (+)= nvl(s.implement_project_id,-23453) ' ||
2837                   '      AND     mpp.plan_id (+)= s.plan_id ' ||
2838                   '      AND     mpp.sr_instance_id(+)= s.sr_instance_id ' ||
2839                   '      AND     mp.sr_tp_id  = s.source_organization_id ' ||
2840                   '      AND     mp.sr_instance_id = s.sr_instance_id ' ||
2841                   '      AND     mp.partner_type= 3 ' ||
2842                   '      AND     mp2.sr_tp_id  = msi.organization_id ' ||
2843                   '      AND     mp2.sr_instance_id = msi.sr_instance_id ' ||
2844                   '      AND     mp2.partner_type= 3 ' ||
2845                   '      AND     msi.inventory_item_id = s.inventory_item_id ' ||
2846                   '      AND     msi.plan_id = s.plan_id ' ||
2847                   '      AND     msi.organization_id = s.organization_id ' ||
2848                   '      AND     msi.sr_instance_id = s.sr_instance_id '   ||
2849                   '      AND     msi1.plan_id = -1 ' ||
2850                   '      AND     msi1.sr_instance_id = msi.sr_instance_id ' ||
2851                   '      AND     msi1.organization_id = msi.organization_id ' ||
2852                   '      AND     msi1.inventory_item_id = msi.inventory_item_id ' ||
2853                   '      AND     s.release_errors is NULL ' ||
2854                   '      AND     s.implement_quantity > 0 ' ||
2855                   '      AND     s.organization_id = orgs.planned_organization ' ||
2856                   '      AND     s.sr_instance_id = orgs.sr_instance_id ' ||
2857                   '      AND     s.plan_id = orgs.plan_id ' ||
2858                   '      AND     orgs.organization_id = :l_arg_owning_org_id ' ||
2859                   '      AND     orgs.owning_sr_instance = :l_arg_owning_instance ' ||
2860                   '      AND     orgs.plan_id = :l_arg_plan_id  ' ||
2861                   '      AND     orgs.sr_instance_id  = :l_arg_org_instance ' ||
2862                   '      AND    (s.releasable = ' || RELEASABLE || ' or s.releasable is null ) ';
2863 
2864                 IF  v_batch_id_populated = 2 THEN
2865                     l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id is NULL ';
2866                 ELSE
2867                     l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id = ' || g_batch_id || ' ' ;
2868                 END IF;
2869 
2870                 IF l_arg_log_org_id <> l_arg_owning_org_id THEN
2871                         l_sql_stmt :=  l_sql_stmt ||'      AND     orgs.planned_organization = ' || l_arg_log_org_id || ' ' ;
2872                 END IF;
2873 
2874                 IF l_arg_mode IS NULL THEN
2875                     l_sql_stmt :=  l_sql_stmt ||'      AND s.load_type  in ( '|| DRP_REQ_LOAD||','|| IRO_LOAD  ||')'||  ' ';
2876                 ELSIF l_arg_mode = 'WF' THEN
2877                     l_sql_stmt :=  l_sql_stmt ||'      AND s.transaction_id = ' || l_arg_transaction_id  || '  ' ;
2878                 END IF;
2879 
2880                 IF v_msc_released_only_by_user = 1 THEN
2881                     l_sql_stmt :=  l_sql_stmt || ' AND s.last_updated_by = ' || v_user_id || ' ' ;
2882                 END IF;
2883 
2884                 l_sql_stmt :=  l_sql_stmt || ' ) ';
2885 
2886 
2887 
2888                 IF l_arg_mode IS NULL OR l_arg_mode = 'WF' THEN
2889                    EXECUTE IMMEDIATE l_sql_stmt USING l_arg_po_batch_number,
2890                                                       l_arg_org_instance,
2891                                                       p_load_type,
2892                                                       l_arg_owning_org_id,
2893                                                       l_arg_owning_instance,
2894                                                       l_arg_plan_id,
2895                                                       l_arg_org_instance;
2896 
2897                 END IF;
2898 
2899 
2900 
2901     ELSE
2902 
2903       l_sql_stmt := ' INSERT INTO MRP_ORG_TRANSFER_RELEASE' || l_dblink ||
2904                  '  ( batch_id, '||
2905                  '    Item_ID, ' ||
2906                  '    SRC_Organization_ID, ' ||
2907                  '    SR_Instance_ID, ' ||
2908                  '    To_Organization_ID, ' ||
2909                  '    To_Sr_Instance_ID, ' ||
2910                  '    Quantity, ' ||
2911                  '    Need_By_Date, ' ||
2912                  '    Ship_date, ' ||
2913                  '    Deliver_to_location_id, ' ||
2914                  '    Deliver_to_requestor_id, ' ||
2915                  '    Preparer_id, ' ||
2916                  '    Uom_code, ' ||
2917                  '    Charge_account_id, ' ||
2918                  '    To_Operating_unit, ' ||
2919                  '    SRC_Operating_unit, ' ||
2920                  '    Load_Type, ' ||
2921                  '    Sales_Order_Line_ID, ' ||
2922                  '    Sales_Order_Number, ' ||
2923                  '    End_item_number, '||
2924                  '    firm_demand_flag, ' ||
2925                  '    transaction_id, ' ||
2926                  '    plan_type ,' ||
2927                  '    ship_method )  ' ||
2928                  ' (SELECT /*+ FIRST_ROWS */ ' ||
2929                  '    :l_arg_po_batch_number,'||      -- batch id
2930                  '    msi.sr_inventory_item_id, ' ||  -- item id in the source
2931                  '    s.implement_source_org_id,' ||  -- source organization id
2932                  '    s.sr_instance_id,         ' ||  -- source instance id
2933                  '    s.organization_id,        ' ||  -- destination org id
2934                  '    :l_org_instance_id,       ' ||  -- destination instance id
2935                  '    s.implement_quantity,     ' ||  -- quantity
2936 	         '    s.implement_dock_date,    ' ||  -- arrival date in dst org
2937                  '    s.implement_ship_date,    ' ||  -- ship date
2938                  '    s.implement_location_id,  ' ||  -- destination location id
2939                  '    s.implement_employee_id,  ' ||  -- Deliver to requestor id
2940                  '    s.implement_employee_id,  ' ||  -- preparer id
2941                  '    msi.uom_code,             ' ||   -- uom
2942                  '    decode(mp.organization_type, ' || -- Account id
2943                  '           1, nvl(mpp.material_account, '||
2944                  '                 decode( msi1.inventory_asset_flag, ' ||
2945                  '                        ''Y'', mp.material_account, ' ||
2946                  '                 nvl(msi1.expense_account,mp.expense_account))),' ||
2947                  '           -1), '||
2948                   '    mp2.operating_unit,  ' ||        -- to_operating unit
2949                   '    mp.operating_unit, ' ||        -- src_operating_unit
2950                   '   :p_load_type, ' ||             -- load type
2951                   '    d.sales_order_line_id,' ||    -- sales order line_id
2952 /*                  '    to_number(substr(d.order_number,1,instr(d.order_number,ltrim(d.order_number,''0123456789''))-1)) ,' || --'    msc_rel_plan_pub.Decode_Sales_Order_Number' || l_dblink || '(d.order_number),'        || -- xxx dsting */
2953                   '    decode(instr(d.order_number,ltrim(d.order_number,''0123456789'')), null,to_number(d.order_number), to_number(substr(d.order_number,1,instr(d.order_number,ltrim(d.order_number,''0123456789''))-1))), '||     -- 9849059
2954                   '    s.implement_unit_number, ' ||  -- end item number
2955                   '    DECODE(s.implement_firm, 1, ''Y'', ''N''), '||
2956                   '    s.transaction_id, ' || -- transaction_id of msc_supplies
2957                   l_plan_type ||','||
2958                   '    nvl(s.implement_ship_method,s.ship_method) ' ||
2959                   ' FROM    msc_projects mpp, '||
2960                   '         msc_trading_partners mp, ' ||
2961                   '         msc_trading_partners mp2,' ||
2962                   '         msc_system_items    msi, ' ||
2963                   '         msc_system_items    msi1,' ||
2964                   '         msc_supplies        s,'    ||
2965                   '         msc_demands         d,'    ||
2966                   '         msc_plan_organizations_v orgs ' ||
2967                   ' WHERE   mpp.organization_id (+)= s.organization_id   ' ||
2968                   '       AND mpp.project_id (+)= nvl(s.implement_project_id,-23453) ' ||
2969                   '       AND     mpp.plan_id (+)= s.plan_id   ' ||
2970                   '       AND     mpp.sr_instance_id(+)= s.sr_instance_id  ' ||
2971                   '      AND     mp.sr_tp_id  = s.source_organization_id  ' ||
2972                   '      AND     mp.sr_instance_id = s.sr_instance_id ' ||
2973                   '      AND     mp.partner_type= 3 ' ||
2974                   '      AND     mp2.sr_tp_id  = msi.organization_id ' ||
2975                   '      AND     mp2.sr_instance_id = msi.sr_instance_id ' ||
2976                   '      AND     mp2.partner_type= 3 ' ||
2977                   '      AND     msi.inventory_item_id = s.inventory_item_id ' ||
2978                   '      AND     msi.plan_id = s.plan_id ' ||
2979                   '      AND     msi.organization_id = s.organization_id ' ||
2980                   '      AND     msi.sr_instance_id = s.sr_instance_id '   ||
2981                   '      AND     msi1.plan_id = -1 ' ||
2982                   '      AND     msi1.sr_instance_id = msi.sr_instance_id ' ||
2983                   '      AND     msi1.organization_id = msi.organization_id ' ||
2984                   '      AND     msi1.inventory_item_id = msi.inventory_item_id ' ||
2985                   '      AND     s.release_errors is NULL ' ||
2986                   '      AND     s.implement_quantity >= 0 ' ||
2987                   '      AND     s.organization_id = orgs.planned_organization ' ||
2988                   '      AND     s.sr_instance_id = orgs.sr_instance_id ' ||
2989                   '      AND     s.plan_id = orgs.plan_id ' ||
2990                   '      AND     orgs.organization_id = :l_arg_owning_org_id ' ||
2991                   '      AND     orgs.owning_sr_instance = :l_arg_owning_instance ' ||
2992                   '      AND     orgs.plan_id = :l_arg_plan_id  ' ||
2993                   '      AND     orgs.sr_instance_id  = :l_arg_org_instance ' ||
2994                   '      AND     d.plan_id = s.plan_id ' ||
2995                   '      AND     d.sr_instance_id  = s.sr_instance_id ' ||
2996                   '      AND     d.disposition_id  = s.transaction_id ' ||
2997                   '      AND     d.origination_type = 30 ' ||
2998                   -- Bug 8717740
2999                   '      and     d.inventory_item_id = s.inventory_item_id ' ||
3000                   '      and    (s.releasable = ' || RELEASABLE || ' or s.releasable is null ) ';
3001 
3002                 IF  v_batch_id_populated = 2 THEN
3003                     l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id is NULL ';
3004                 ELSE
3005                     l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id = ' || g_batch_id || ' ' ;
3006                 END IF;
3007 
3008                 IF l_arg_log_org_id <> l_arg_owning_org_id THEN
3009                         l_sql_stmt :=  l_sql_stmt ||'      AND     orgs.planned_organization = ' || l_arg_log_org_id || ' ' ;
3010                 END IF;
3011 
3012                 IF l_arg_mode IS NULL THEN
3013                     l_sql_stmt :=  l_sql_stmt ||'      AND s.load_type  ='|| p_load_type ||  ' and s.load_type IS NOT NULL ';
3014                 ELSIF l_arg_mode = 'WF' THEN
3015                     l_sql_stmt :=  l_sql_stmt ||'      AND s.transaction_id = ' || l_arg_transaction_id  || '  ' ;
3016                 END IF;
3017 
3018                 IF v_msc_released_only_by_user = 1 THEN
3019                     l_sql_stmt :=  l_sql_stmt || ' AND s.last_updated_by = ' || v_user_id || ' ' ;
3020                 END IF;
3021 
3022                 l_sql_stmt :=  l_sql_stmt || ' ) ';
3023 
3024                IF l_arg_mode IS NULL OR l_arg_mode = 'WF' THEN
3025 
3026                    EXECUTE IMMEDIATE l_sql_stmt USING l_arg_po_batch_number,
3027                                                       l_arg_org_instance,
3028                                                       p_load_type,
3029                                                       l_arg_owning_org_id,
3030                                                       l_arg_owning_instance,
3031                                                       l_arg_plan_id,
3032                                                       l_arg_org_instance;
3033 
3034                 END IF;
3035 
3036    END IF;
3037 
3038    update mrp_org_transfer_release motr
3039                  set earliest_ship_date = (select max(ms.new_schedule_date)
3040                                            from    msc_supplies ms, msc_single_lvl_peg mslp
3041                                            where   ms.plan_id=l_arg_plan_id
3042                                            and     ms.transaction_id = mslp.child_id
3043                                            and     ms.plan_id= mslp.plan_id
3044                                            and     mslp.parent_id = motr.transaction_id
3045                                            and     mslp.pegging_type=1)
3046                   where motr.batch_id=l_arg_po_batch_number;
3047 
3048 
3049    OPEN c_row_count for l_sql_cur USING l_arg_po_batch_number;
3050    FETCH c_row_count INTO l_after_row_count;
3051    CLOSE c_row_count;
3052 
3053    IF p_load_type = DRP_REQ_LOAD THEN
3054 
3055       arg_loaded_int_reqs := l_after_row_count - l_before_row_count;
3056       arg_resched_int_reqs := null;
3057    ELSE
3058       arg_loaded_int_reqs := null;
3059       arg_resched_int_reqs := l_after_row_count - l_before_row_count;
3060 
3061    END IF;
3062 
3063   IF l_arg_mode = 'WF' THEN
3064         update msc_supplies
3065            set releasable = RELEASE_ATTEMPTED
3066                ,batch_id   = nvl(batch_id,g_batch_id)
3067          where plan_id = l_arg_plan_id
3068            and transaction_id= l_arg_transaction_id;
3069    ELSE
3070        execute immediate '  update msc_supplies
3071                                set releasable = ' || RELEASE_ATTEMPTED || '
3072                                   ,batch_id   = nvl(batch_id,' ||g_batch_id ||')
3073                              where plan_id = ' || l_arg_plan_id || '
3074                                and transaction_id in
3075                                     (select transaction_id from MRP_ORG_TRANSFER_RELEASE' || l_dblink || '
3076                                       where batch_id = ' || l_arg_po_batch_number  || ')';
3077    END IF;
3078 
3079 END POPULATE_ISO_IN_SOURCE;
3080 
3081 
3082 
3083 /*------------------------------------------------------------
3084 
3085  PROCEDURE   :  POPULATE_ISO_IN_SOURCE_2
3086 
3087  -------------------------------------------------------------*/
3088 PROCEDURE POPULATE_ISO_IN_SOURCE_2(
3089                                   l_dblink              IN  varchar2,
3090                                   l_arg_po_batch_number IN  number,
3091                                   l_arg_owning_instance IN  number,
3092                                   l_arg_po_group_by     IN  number,
3093                                   l_arg_plan_id         IN  number,
3094                                   l_arg_log_org_id      IN  number,
3095                                   l_arg_owning_org_id   IN  number,
3096                                   l_arg_org_instance    IN  number,
3097                                   l_arg_mode            IN  varchar2,
3098                                   l_arg_transaction_id  IN  number,
3099                                   arg_loaded_int_reqs   IN OUT NOCOPY number,
3100                                   arg_resched_int_reqs  IN OUT NOCOPY number,
3101                                   p_load_type           IN  number) IS
3102 
3103 TYPE CurTyp is ref cursor;
3104 
3105 c_row_count  CurTyp;
3106 
3107  CURSOR c_plan_type(p_plan_id number) IS
3108       select plan_type
3109       from msc_plans a
3110       where
3111       plan_id = p_plan_id;
3112 
3113 l_sql_stmt   VARCHAR2(20000):= NULL;
3114 l_sql_cur    varchar2(3000) := NULL;
3115 
3116 --l_before_row_count  number := 0;
3117 --l_after_row_count   number := 0;
3118 
3119 l_plan_type NUMBER :=0;
3120 
3121 BEGIN
3122 
3123    OPEN c_plan_type(l_arg_plan_id);
3124    FETCH c_plan_type INTO l_plan_type;
3125    CLOSE c_plan_type;
3126 
3127    IF p_load_type = DRP_REQ_LOAD THEN
3128 
3129       l_sql_stmt := ' INSERT INTO MRP_ORG_TRANSFER_RELEASE' || l_dblink ||
3130                  '  ( batch_id, '||
3131                  '    Item_ID, ' ||
3132                  '    SRC_Organization_ID, ' ||
3133                  '    SR_Instance_ID, ' ||
3134                  '    To_Organization_ID, ' ||
3135                  '    To_Sr_Instance_ID, ' ||
3136                  '    Quantity, ' ||
3137                  '    Need_By_Date, ' ||
3138                  '    Ship_date, ' ||
3139                  '    Deliver_to_location_id, ' ||
3140                  '    Deliver_to_requestor_id, ' ||
3141                  '    Preparer_id, ' ||
3142                  '    Uom_code, ' ||
3143                  '    Charge_account_id, ' ||
3144                  '    To_Operating_unit, ' ||
3145                  '    SRC_Operating_unit, ' ||
3146                  '    Load_Type, ' ||
3147                  '    Sales_Order_Line_ID, ' ||
3148                  '    Sales_Order_Number, ' ||
3149                  '    End_item_number, ' ||
3150                  '    firm_demand_flag, ' ||
3151                  '    transaction_id, ' ||
3152                  '    plan_type ,' ||
3153                  '    part_condition ,' ||             -- bug 9031352
3154                  '    ship_method )  ' ||
3155                  '( SELECT /*+ FIRST_ROWS */  '||
3156                  '    :l_arg_po_batch_number,'||      -- batch id
3157                  '    msi.sr_inventory_item_id, ' ||  -- item id in the source
3158                  '    s.implement_source_org_id,' ||  -- source organization id
3159                  '    s.sr_instance_id,         ' ||  -- source instance id
3160                  '    s.organization_id,        ' ||  -- destination org id
3161                  '    :l_org_instance_id,       ' ||  -- destination instance id
3162                  '    s.implement_quantity,     ' ||  -- quantity
3163 	               '    s.implement_dock_date,    ' ||  -- arrival date in dst org
3164                  '    nvl(s.implement_ship_date,nvl(s.implement_dock_date,s.implement_date)),    ' ||  -- ship date
3165                  '    s.implement_location_id,  ' ||  -- destination location id
3166                  '    s.implement_employee_id,  ' ||  -- Deliver to requestor id
3167                  '    s.implement_employee_id,  ' ||  -- preparer id
3168                  '    msi.uom_code,             ' ||   -- uom
3169                  '    decode(mp.organization_type, ' || -- Account id
3170                  '           1, nvl(mpp.material_account, '||
3171                  '                 decode( msi1.inventory_asset_flag, ' ||
3172                  '                        ''Y'', mp.material_account, ' ||
3173                  '                 nvl(msi1.expense_account,mp.expense_account))),' ||
3174                  '           -1), '||
3175                   '    mp.operating_unit,  ' ||        -- src_operating unit
3176                   '    mp2.operating_unit, ' ||        -- to_operating_unit
3177                   '   :p_load_type, ' ||             -- load type
3178                   '    null, ' || -- Sales Order Line ID
3179                   '    s.transaction_id, ' || -- Sales Order Number
3180                   '    s.implement_unit_number, ' || --end item number
3181                   '    DECODE(s.implement_firm, 1, ''Y'',''N''), '||
3182                   '    s.transaction_id, ' || -- transaction_id of msc_supplies
3183                    l_plan_type ||',' ||
3184                   '    decode(s.item_type_value,1,''G'',2,''B'',''G''), ' || -- part condition of msc_supplies  -- bug 9031352
3185                   '    nvl(s.implement_ship_method,s.ship_method) ' ||
3186                   ' FROM    msc_projects mpp, ' ||
3187                       ' msc_trading_partners mp, ' ||
3188                       ' msc_trading_partners mp2, ' ||
3189                        'msc_system_items    msi, '||
3190                        'msc_system_items    msi1,'||
3191                        'msc_part_supplies        s, '||
3192                        'msc_plan_organizations_v orgs '||
3193                     'WHERE   mpp.organization_id (+)= s.organization_id '||
3194                         'AND mpp.project_id (+)= nvl(s.implement_project_id,-23453) ' ||
3195                         'AND     mpp.plan_id (+)= s.plan_id '||
3196                         'AND     mpp.sr_instance_id(+)= s.sr_instance_id '||
3197                         'AND     mp.sr_tp_id  = s.source_organization_id '||
3198                         'AND     mp.sr_instance_id = s.sr_instance_id '||
3199                         'AND     mp.partner_type= 3 '||
3200                         'AND     mp2.sr_tp_id  = msi.organization_id '||
3201                         'AND     mp2.sr_instance_id = msi.sr_instance_id '||
3202                         'AND     mp2.partner_type= 3 '||
3203                         'AND     msi.inventory_item_id = s.inventory_item_id '||
3204                         'AND     msi.plan_id = s.plan_id '||
3205                         'AND     msi.organization_id = s.organization_id '||
3206                         'AND     msi.sr_instance_id = s.sr_instance_id '||
3207                         'AND     msi1.plan_id = -1 '||
3208                         'AND     msi1.sr_instance_id = msi.sr_instance_id '||
3209                         'AND     msi1.organization_id = msi.organization_id '||
3210                         'AND     msi1.inventory_item_id = msi.inventory_item_id '||
3211                         --'AND     s.release_errors is NULL '||
3212                         'AND    (s.releasable = ' || RELEASABLE || 'or s.releasable is null ) '||
3213                         --'AND     s.implement_quantity > 0 '||
3214                         'AND     s.organization_id = orgs.planned_organization '||
3215                         'AND     s.sr_instance_id = orgs.sr_instance_id '||
3216                         'AND     s.plan_id = orgs.plan_id '||
3217                         'AND     orgs.organization_id = :l_arg_owning_org_id '||
3218                         'AND     orgs.owning_sr_instance = :l_arg_owning_instance '||
3219                         'AND     orgs.plan_id = :l_arg_plan_id  '||
3220                         'AND     orgs.sr_instance_id  = :l_arg_org_instance ';
3221 
3222 
3223                 IF  v_batch_id_populated = 2 THEN
3224                     l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id is NULL ';
3225                 ELSE
3226                     l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id = ' || g_batch_id || ' ' ;
3227                 END IF;
3228 
3229                 IF l_arg_log_org_id <> l_arg_owning_org_id THEN
3230                         l_sql_stmt :=  l_sql_stmt ||'      AND     orgs.planned_organization = ' || l_arg_log_org_id || ' ' ;
3231                 END IF;
3232 
3233                 IF l_arg_mode IS NULL THEN
3234                     l_sql_stmt :=  l_sql_stmt ||'      AND s.load_type  in ( '|| DRP_REQ_LOAD||','|| IRO_LOAD  ||')'||  ' ';
3235                 ELSIF l_arg_mode = 'WF' THEN
3236                     l_sql_stmt :=  l_sql_stmt ||'      AND s.transaction_id = ' || l_arg_transaction_id  || '  ' ;
3237                 END IF;
3238 
3239                /* IF v_msc_released_only_by_user = 1 THEN
3240                     l_sql_stmt :=  l_sql_stmt || ' AND s.last_updated_by = ' || v_user_id || ' ' ;
3241                 END IF;*/
3242 
3243                 l_sql_stmt :=  l_sql_stmt || ' ) ';
3244 
3245 
3246 
3247                 IF l_arg_mode IS NULL OR l_arg_mode = 'WF' THEN
3248                    EXECUTE IMMEDIATE l_sql_stmt USING l_arg_po_batch_number,
3249                                                       l_arg_org_instance,
3250                                                       p_load_type,
3251                                                       l_arg_owning_org_id,
3252                                                       l_arg_owning_instance,
3253                                                       l_arg_plan_id,
3254                                                       l_arg_org_instance;
3255                   arg_loaded_int_reqs := SQL%ROWCOUNT;
3256 
3257                 END IF;
3258 
3259    END IF;
3260 
3261   /* update mrp_org_transfer_release motr
3262                  set earliest_ship_date = (select max(ms.new_schedule_date)
3263                                            from    msc_part_supplies ms, msc_single_lvl_peg mslp
3264                                            where   ms.plan_id=l_arg_plan_id
3265                                            and     ms.transaction_id = mslp.child_id
3266                                            and     ms.plan_id= mslp.plan_id
3267                                            and     mslp.parent_id = motr.transaction_id
3268                                            and     mslp.pegging_type=1)
3269                   where motr.batch_id=l_arg_po_batch_number;*/
3270 
3271    arg_resched_int_reqs := null;
3272 
3273 
3274   IF l_arg_mode = 'WF' THEN
3275         update msc_supplies
3276          set releasable = RELEASE_ATTEMPTED,
3277                batch_id   = nvl(batch_id,g_batch_id)
3278          where plan_id = l_arg_plan_id
3279          and transaction_id= l_arg_transaction_id;
3280 
3281     /*     update msc_part_supplies
3282          set releasable = RELEASE_ATTEMPTED,
3283                batch_id   = nvl(batch_id,g_batch_id)
3284          where plan_id = l_arg_plan_id
3285          and transaction_id= l_arg_transaction_id;       */
3286    ELSE
3287        execute immediate '  update msc_supplies
3288                                set releasable = ' || RELEASE_ATTEMPTED || ',
3289                                    batch_id   = nvl(batch_id,' ||g_batch_id ||')
3290                              where plan_id = ' || l_arg_plan_id || '
3291                              and transaction_id in (select orig_transaction_id from msc_part_supplies where transaction_id in
3292                                     (select transaction_id from MRP_ORG_TRANSFER_RELEASE' || l_dblink || '
3293                                       where batch_id = ' || l_arg_po_batch_number  || '))';
3294 
3295         execute immediate '  update msc_part_supplies
3296                                set releasable = ' || RELEASE_ATTEMPTED || ',
3297                                    batch_id   = nvl(batch_id,' ||g_batch_id ||')
3298                              where plan_id = ' || l_arg_plan_id || '
3299                              and transaction_id in
3300                                     (select transaction_id from MRP_ORG_TRANSFER_RELEASE' || l_dblink || '
3301                                       where batch_id = ' || l_arg_po_batch_number  || ')';
3302    END IF;
3303 
3304 END POPULATE_ISO_IN_SOURCE_2;
3305 
3306 
3307 PROCEDURE RELEASE_IRO(
3308     p_dblink IN VARCHAR2,
3309     p_arg_iro_batch_number IN NUMBER,
3310     p_arg_owning_instance IN NUMBER,
3311     p_arg_po_group_by IN NUMBER,
3312     p_arg_plan_id IN NUMBER,
3313     p_arg_log_org_id IN NUMBER,
3314     p_arg_owning_org_id IN NUMBER,
3315     p_arg_org_instance IN NUMBER,
3316     p_arg_mode IN VARCHAR2,
3317     p_arg_transaction_id IN NUMBER,
3318     p_arg_loaded_int_repair_orders IN OUT nocopy NUMBER,
3319     p_load_type IN NUMBER)
3320 IS
3321 --l_sql_cur VARCHAR2(3000) := NULL;
3322 l_sql_qry VARCHAR2(3000) := NULL;
3323 l_sql_qry2 varchar2(3000) := NULL;
3324 p_new_iso_num NUMBER;
3325 p_where_clause varchar2(3000):= NULL;
3326 p_total_rows  NUMBER;
3327 p_succ_rows  NUMBER;
3328 p_error_rows  NUMBER;
3329 p_reschedule_iso_num NUMBER;
3330 
3331 /* CURSOR c1(v_arg_mode VARCHAR2,
3332     v_arg_org_instance NUMBER,
3333     v_load_type NUMBER,
3334     v_arg_transaction_id NUMBER,
3335     v_arg_plan_id NUMBER) IS
3336 */
3337 
3338     type numlisttyp IS TABLE OF NUMBER;
3339     type datelisttyp IS TABLE OF DATE;
3340     type varlisttyp IS TABLE OF VARCHAR2(30);
3341 
3342     lv_transaction_id_lst numlisttyp;
3343     lv_quantity_lst numlisttyp;
3344     lv_in_req_quantity_lst numlisttyp;
3345     lv_load_type_lst  numlisttyp;
3346     lv_out_req_quantity_lst numlisttyp;
3347     lv_in_req_transaction_id_lst numlisttyp;
3348     lv_out_req_transaction_id_lst numlisttyp;
3349     lv_inventory_item_id_lst numlisttyp;
3350     lv_uom_code_lst varlisttyp;
3351     lv_organization_id_lst numlisttyp;
3352     lv_promise_date_lst datelisttyp;
3353     lv_implement_location_id  numlisttyp;
3354     lv_implement_source_org_id numlisttyp;
3355 
3356 
3357 
3358     --lv_implement_quantity number;
3359 BEGIN
3360   /* 1. get the row count for the new batch  if in table MRP_IRO_RELEASE */
3361   /*
3362   l_sql_cur := 'SELECT count(*) from MRP_IRO_RELEASE' || p_dblink ||' WHERE batch_id = :p_arg_iro_batch_number';
3363   OPEN c_row_count FOR l_sql_cur USING p_arg_iro_batch_number;
3364   FETCH c_row_count
3365   INTO l_before_row_count;
3366   CLOSE c_row_count;
3367   */
3368    p_arg_loaded_int_repair_orders:=0;
3369 
3370    IF p_arg_mode IS NULL THEN
3371      p_where_clause:= ' AND s.load_type = '||p_load_type ;
3372 
3373    ELSIF p_arg_mode = 'WF' THEN
3374        p_where_clause:= ' AND s.transaction_id = '||p_arg_transaction_id;
3375 
3376   ELSE
3377       -- log_msg('no IROs to release....');
3378        return ;
3379     END IF;
3380 
3381   /* 3. bulk collect all the records returned by record  by above cursor into table types record */
3382 l_sql_qry2:=
3383 'with mfg0 AS  ( select transaction_id,plan_id,organization_id ,sr_instance_id ,demand_id ,
3384                 sum(allocated_quantity) allocated_quantity
3385                 from msc_full_pegging
3386                 where sr_instance_id = '||p_arg_org_instance||
3387                 '  and plan_id = '|| p_arg_plan_id ||
3388                 ' group by transaction_id,plan_id,organization_id ,sr_instance_id ,demand_id
3389       )
3390   SELECT /*+ LEADING(s d) */ s1.transaction_id,
3391     s1.new_order_quantity quantity,
3392     s2.new_order_quantity in_req_quantity,
3393     s.new_order_quantity  out_req_quantity,
3394     Decode(s2.order_type,51,s2.transaction_id, null)  in_req_transaction_id,
3395     s.transaction_id out_req_transaction_id,
3396     --L_arg_io_batch_number Batch_id,
3397     msi1.sr_inventory_item_id inventory_item_id,
3398     msi1.uom_code,
3399     s1.organization_id,
3400     s1.new_schedule_date promise_date,
3401     s.implement_location_id,
3402     s.Load_Type,
3403     s.implement_source_org_id
3404  FROM msc_supplies s,
3405     msc_supplies s1,
3406     msc_supplies s2,
3407     msc_system_items msi1,
3408     msc_demands d,
3409     msc_demands d1,
3410     msc_demands d2,
3411     MFG0 mfg,
3412     mfg0 mfg1
3413   WHERE s.release_errors IS NULL
3414    AND s.sr_instance_id = '||p_arg_org_instance||
3415  ' AND s.plan_id = '|| p_arg_plan_id ||
3416  ' AND s.transaction_id = d.disposition_id
3417    AND s.sr_instance_id = d.sr_instance_id
3418    AND s.plan_id = d.plan_id
3419    AND d.origination_type = 1 --- Planned order demand
3420    AND d.demand_id = mfg.demand_id
3421    AND d.plan_id = mfg.plan_id
3422    AND d.sr_instance_id = mfg.sr_instance_id
3423    AND d.organization_id = mfg.organization_id
3424    AND mfg.transaction_id = s1.transaction_id
3425    AND mfg.plan_id = s1.plan_id
3426    AND s1.sr_instance_id = msi1.sr_instance_id
3427    AND s1.plan_id = msi1.plan_id
3428    AND s1.inventory_item_id = msi1.inventory_item_id
3429    AND s1.organization_id = msi1.organization_id
3430    AND s1.transaction_id = d1.disposition_id
3431    AND s1.sr_instance_id = d1.sr_instance_id
3432    AND s1.plan_id = d1.plan_id
3433    AND d1.origination_type = 78 -- planned defective part demand
3434    AND d1.demand_id = mfg1.demand_id(+)
3435    AND d1.plan_id = mfg1.plan_id(+)
3436    AND d1.sr_instance_id = mfg1.sr_instance_id(+)
3437    AND D1.organization_id = mfg1.organization_id (+)
3438    AND mfg1.transaction_id = s2.transaction_id(+)
3439    AND mfg1.plan_id = s2.plan_id(+)
3440    AND s2.transaction_id = d2.disposition_id(+)
3441    AND s2.sr_instance_id = d2.sr_instance_id(+)
3442    AND s2.plan_id = d2.plan_id(+)
3443    AND d2.origination_type(+) = 1
3444    and (s.releasable = ' || RELEASABLE  || ' or s.releasable is null )';
3445 
3446     IF  v_batch_id_populated = 2 THEN
3447 
3448         l_sql_qry2 :=  l_sql_qry2 || ' AND  s.batch_id is NULL ';
3449     ELSE
3450 
3451         l_sql_qry2 :=  l_sql_qry2 || ' AND  s.batch_id = ' || g_batch_id || ' ' ;
3452     END IF;
3453 
3454     l_sql_qry2:= l_sql_qry2  ||p_where_clause;
3455 
3456 
3457 
3458  IF ((p_arg_mode IS NULL) OR (p_arg_mode = 'WF')) THEN
3459 
3460   -- bulk collect l_sql_qry2 into lv_IRO_list;
3461   EXECUTE IMMEDIATE l_sql_qry2
3462           bulk collect INTO
3463     lv_transaction_id_lst,
3464     lv_quantity_lst,
3465     lv_in_req_quantity_lst,
3466     lv_out_req_quantity_lst,
3467     lv_in_req_transaction_id_lst,
3468     lv_out_req_transaction_id_lst,
3469     lv_inventory_item_id_lst,
3470     lv_uom_code_lst,
3471     lv_organization_id_lst,
3472     lv_promise_date_lst,
3473     lv_implement_location_id,
3474     lv_load_type_lst,
3475     lv_implement_source_org_id
3476  ;
3477 
3478  END IF;
3479 
3480    p_where_clause :=null ;
3481 
3482 
3483   /* 4.1 iterate through  the record in table type and update the load type for transaction_id corresponding to in_req_transaction_id to  32 */
3484      for i IN 1 .. lv_in_req_transaction_id_lst.count LOOP
3485 
3486           /*4.2 calling the api to update implement_* column */
3487         IF (lv_in_req_transaction_id_lst(i) is not NULL) THEN
3488 
3489           p_where_clause := ' transaction_id = ' || lv_in_req_transaction_id_lst(i) || ' and plan_id = ' || p_arg_plan_id;
3490 
3491              MSC_SELECT_ALL_FOR_RELEASE_PUB.Update_Implement_Attrib(p_where_clause ,
3492                                           NULL,
3493                                           NULL,
3494                                           NULL,
3495                                           NULL,
3496                                           NULL,
3497                                           p_total_rows ,
3498                                           p_succ_rows ,
3499                                           p_error_rows
3500                                           );
3501              /* debuf msg
3502                 select --implement_quantity,
3503                 new_order_quantity, release_errors
3504                 into lv_implement_quantity, lv_release_errors from msc_supplies
3505                 where plan_id =  p_arg_plan_id
3506                 and   transaction_id =  lv_in_req_transaction_id_lst(i);
3507               */
3508 
3509               UPDATE msc_supplies
3510               SET load_type = 32,
3511                  release_status = 2,
3512                  implement_quantity = new_order_quantity,  --to be removed if above api works fine
3513                  release_errors = NULL,
3514                 batch_id=decode(v_batch_id_populated,2,batch_id,g_batch_id)
3515               WHERE transaction_id = lv_in_req_transaction_id_lst(i)
3516                 and plan_id =p_arg_plan_id ;
3517 
3518          END IF;
3519     END LOOP;
3520 
3521   /* 5. bulk insert the data in the table type to mrp_iro_release table in the source */
3522   FOR i IN 1 .. lv_transaction_id_lst.COUNT
3523   LOOP
3524 
3525     l_sql_qry := 'insert into mrp_iro_release' || p_dblink || '(Transaction_id,
3526                           Quantity,
3527                           in_req_quantity,
3528                           out_req_quantity,
3529                           In_req_transaction_id,
3530                           Out_req_transaction_id,
3531                           Batch_id,
3532                           Inventory_item_id,
3533                           Uom_code,
3534                           Organization_id,
3535                           Promise_date,
3536                           Load_Type,
3537                           deliver_to_location_id,
3538                           src_organization_id)
3539                   values (:lv_Transaction_id,
3540                           :lv_Quantity,
3541                           :lv_in_req_quantity,
3542                           :lv_out_req_quantity,
3543                           :lv_In_req_transaction_id,
3544                           :lv_Out_req_transaction_id,
3545                           :p_arg_iro_batch_number,
3546                           :lv_Inventory_item,
3547                           :lv_uom_code ,
3548                           :lv_organization_id,
3549                           :lv_Promise_date,
3550                           :Load_Type,
3551                           :implement_location_id,
3552                           :implement_source_org_id)';
3553 
3554     EXECUTE IMMEDIATE l_sql_qry USING lv_transaction_id_lst(i),
3555       lv_quantity_lst(i),
3556       lv_in_req_quantity_lst(i),
3557       lv_out_req_quantity_lst(i),
3558       lv_in_req_transaction_id_lst(i),
3559       lv_out_req_transaction_id_lst(i),
3560       p_arg_iro_batch_number,
3561       lv_inventory_item_id_lst(i),
3562       lv_uom_code_lst(i),
3563       lv_organization_id_lst(i),
3564       lv_promise_date_lst(i),
3565       lv_load_type_lst(i),
3566       lv_implement_location_id(i),
3567       lv_implement_source_org_id(i);
3568 
3569     p_arg_loaded_int_repair_orders := p_arg_loaded_int_repair_orders + 1 ;
3570 
3571         update msc_supplies
3572            set releasable = RELEASE_ATTEMPTED
3573                ,batch_id   = nvl(batch_id,g_batch_id)
3574          where plan_id =p_arg_plan_id
3575            and transaction_id= lv_transaction_id_lst(i);
3576 
3577   END LOOP;
3578 
3579   /*
3580   OPEN c_row_count FOR l_sql_cur USING p_arg_iro_batch_number;
3581   FETCH c_row_count
3582   INTO l_after_row_count;
3583   CLOSE c_row_count;
3584   */
3585 
3586   --p_arg_loaded_int_repair_orders:= l_after_row_count -l_before_row_count;
3587   /* 6. call populate_iso_in_source to populate mrp_org_transfer_release_table in the source */
3588 
3589   IF p_arg_loaded_int_repair_orders > 0 THEN
3590 
3591       populate_iso_in_source(l_dblink => p_dblink,
3592        l_arg_po_batch_number => p_arg_iro_batch_number,
3593        l_arg_owning_instance => p_arg_owning_instance,
3594        l_arg_po_group_by => p_arg_po_group_by,
3595        l_arg_plan_id => p_arg_plan_id,
3596        l_arg_log_org_id => p_arg_log_org_id,
3597        l_arg_owning_org_id => p_arg_owning_org_id,
3598        l_arg_org_instance => p_arg_org_instance,
3599        l_arg_mode => p_arg_mode,
3600        l_arg_transaction_id => p_arg_transaction_id,
3601        arg_loaded_int_reqs => p_new_iso_num,
3602        arg_resched_int_reqs => p_reschedule_iso_num,
3603        p_load_type => drp_req_load);
3604   END IF;
3605 
3606   l_sql_qry := NULL;
3607 
3608   /* 7. update the load type to null which had been updated to 32   previously */
3609    for i IN 1 .. lv_in_req_transaction_id_lst.count LOOP
3610 
3611     If (lv_in_req_transaction_id_lst(i) is NULL) then
3612 
3613         l_sql_qry :=     'UPDATE msc_supplies '
3614                           ||'SET load_type = NULL
3615                           WHERE plan_id= :p_arg_plan_id AND transaction_id = '||lv_out_req_transaction_id_lst(i) ;
3616 
3617      Else
3618 
3619         l_sql_qry :=     'UPDATE msc_supplies '
3620                           ||'SET load_type = NULL WHERE plan_id= :p_arg_plan_id AND transaction_id in ('
3621                           || lv_in_req_transaction_id_lst(i)||','||lv_out_req_transaction_id_lst(i)||')' ;
3622     ENd if;
3623 
3624         execute immediate l_sql_qry
3625         USING p_arg_plan_id;
3626 
3627 
3628 
3629    END LOOP;
3630   /*
3631    forall i IN 1 .. lv_out_req_transaction_id_lst.count
3632         UPDATE msc_supplies
3633         SET load_type = NULL
3634         WHERE transaction_id = lv_out_req_transaction_id_lst(i)
3635         and plan_id =p_arg_plan_id ;
3636    */
3637 
3638   Exception
3639   When Others Then
3640 
3641    raise;
3642 END release_iro;
3643 
3644 
3645 PROCEDURE RELEASE_IRO_2(
3646     p_dblink IN VARCHAR2,
3647     p_arg_iro_batch_number IN NUMBER,
3648     p_arg_owning_instance IN NUMBER,
3649     p_arg_po_group_by IN NUMBER,
3650     p_arg_plan_id IN NUMBER,
3651     p_arg_log_org_id IN NUMBER,
3652     p_arg_owning_org_id IN NUMBER,
3653     p_arg_org_instance IN NUMBER,
3654     p_arg_mode IN VARCHAR2,
3655     p_arg_transaction_id IN NUMBER,
3656     p_arg_loaded_int_repair_orders IN OUT nocopy NUMBER,
3657     p_load_type IN NUMBER)
3658 IS
3659 --l_sql_cur VARCHAR2(3000) := NULL;
3660 l_sql_qry VARCHAR2(3000) := NULL;
3661 l_sql_qry2 varchar2(3000) := NULL;
3662 p_new_iso_num NUMBER;
3663 p_where_clause varchar2(3000):= NULL;
3664 p_total_rows  NUMBER;
3665 p_succ_rows  NUMBER;
3666 p_error_rows  NUMBER;
3667 p_reschedule_iso_num NUMBER;
3668 
3669 /* CURSOR c1(v_arg_mode VARCHAR2,
3670     v_arg_org_instance NUMBER,
3671     v_load_type NUMBER,
3672     v_arg_transaction_id NUMBER,
3673     v_arg_plan_id NUMBER) IS
3674 */
3675 
3676     type numlisttyp IS TABLE OF NUMBER;
3677     type datelisttyp IS TABLE OF DATE;
3678     type varlisttyp IS TABLE OF VARCHAR2(30);
3679 
3680     lv_transaction_id_lst numlisttyp;
3681     lv_quantity_lst numlisttyp;
3682     lv_in_req_quantity_lst numlisttyp;
3683     lv_load_type_lst  numlisttyp;
3684     lv_out_req_quantity_lst numlisttyp;
3685     lv_in_req_transaction_id_lst numlisttyp;
3686     lv_out_req_transaction_id_lst numlisttyp;
3687     lv_inventory_item_id_lst numlisttyp;
3688     lv_uom_code_lst varlisttyp;
3689     lv_organization_id_lst numlisttyp;
3690     lv_promise_date_lst datelisttyp;
3691     lv_implement_location_id  numlisttyp;
3692     lv_implement_source_org_id numlisttyp;
3693     lv_orig_transaction_id_lst numlisttyp;
3694 
3695     --lv_implement_quantity number;
3696 BEGIN
3697   /* 1. get the row count for the new batch  if in table MRP_IRO_RELEASE */
3698 
3699    p_arg_loaded_int_repair_orders:=0;
3700 
3701    IF p_arg_mode IS NULL THEN
3702      p_where_clause:= ' AND k.load_type = '||p_load_type ;
3703 
3704    ELSIF p_arg_mode = 'WF' THEN
3705        p_where_clause:= ' AND k.transaction_id = '||p_arg_transaction_id;
3706 
3707   ELSE
3708       -- log_msg('no IROs to release....');
3709        return ;
3710     END IF;
3711 
3712   /* 3. bulk collect all the records returned by record  by above cursor into table types record */
3713 l_sql_qry2:=
3714 
3715 'with mfg0 AS
3716     ( select transaction_id,plan_id,organization_id ,sr_instance_id ,demand_id ,
3717                 sum(allocated_quantity) allocated_quantity
3718              from msc_part_pegging
3719              where sr_instance_id = '||p_arg_org_instance|| '
3720              and plan_id = '|| p_arg_plan_id ||'
3721              group by transaction_id,plan_id,organization_id ,sr_instance_id ,demand_id
3722       )
3723  SELECT /*+ LEADING(s d) */
3724     s1.transaction_id,
3725     s1.new_order_quantity quantity,
3726     s2.new_order_quantity in_req_quantity,
3727     s.new_order_quantity  out_req_quantity,
3728     Decode(s2.order_type,51,s2.transaction_id, null)  in_req_transaction_id,
3729     s.transaction_id out_req_transaction_id,
3730     --L_arg_io_batch_number Batch_id,
3731     msi1.sr_inventory_item_id inventory_item_id,
3732     msi1.uom_code,
3733     s1.organization_id,
3734     s1.new_schedule_date promise_date,
3735     s.implement_location_id,
3736     k.Load_Type,
3737     s.implement_source_org_id,
3738     k.transaction_id
3739 FROM
3740 msc_supplies k,
3741 msc_part_supplies s,
3742 msc_part_demands d,
3743 mfg0 mfg,
3744 msc_part_supplies s1,
3745 msc_part_demands d1,
3746 mfg0 mfg1,
3747 msc_part_supplies s2,
3748 msc_part_demands d2,
3749 msc_system_items msi1
3750 WHERE
3751    k.release_errors IS NULL
3752    AND (k.releasable = ' || RELEASABLE || ' or k.releasable is null )
3753    AND k.sr_instance_id = '||p_arg_org_instance|| '
3754    AND k.plan_id = '|| p_arg_plan_id ||'  --K.batchidid filter and k.transctionid/k.load_typefilter added dynamically
3755    AND k.TRANSACTION_ID = s.ORIG_TRANSACTION_ID
3756    AND s.transaction_id = d.disposition_id
3757    AND s.sr_instance_id = d.sr_instance_id
3758    AND s.plan_id = d.plan_id
3759    AND d.origination_type = 1 --- Planned order demand
3760    AND d.demand_id = mfg.demand_id
3761    AND d.plan_id = mfg.plan_id
3762    AND d.sr_instance_id = mfg.sr_instance_id
3763    AND d.organization_id = mfg.organization_id
3764    AND mfg.transaction_id = s1.transaction_id
3765    AND mfg.plan_id = s1.plan_id
3766    AND s1.plan_id = msi1.plan_id
3767    AND s1.sr_instance_id = msi1.sr_instance_id
3768    AND s1.organization_id = msi1.organization_id
3769    AND s1.inventory_item_id = msi1.inventory_item_id
3770    AND s1.transaction_id = d1.disposition_id
3771    AND s1.sr_instance_id = d1.sr_instance_id
3772    AND s1.plan_id = d1.plan_id
3773    AND d1.origination_type = 78 -- planned defective part demand
3774    AND d1.plan_id = mfg1.plan_id (+)
3775    AND d1.sr_instance_id = mfg1.sr_instance_id (+)
3776    AND d1.organization_id = mfg1.organization_id (+)
3777    AND d1.demand_id = mfg1.demand_id (+)
3778    AND mfg1.transaction_id = s2.transaction_id(+)
3779    AND mfg1.plan_id = s2.plan_id(+)
3780    AND s2.transaction_id = d2.disposition_id(+)
3781    AND s2.sr_instance_id = d2.sr_instance_id(+)
3782    AND s2.plan_id = d2.plan_id(+)
3783    AND d2.origination_type(+) = 1';
3784 
3785     IF  v_batch_id_populated = 2 THEN
3786         l_sql_qry2 :=  l_sql_qry2 || ' AND  k.batch_id is NULL ';
3787     ELSE
3788         l_sql_qry2 :=  l_sql_qry2 || ' AND  k.batch_id = ' || g_batch_id || ' ' ;
3789     END IF;
3790 
3791     l_sql_qry2:= l_sql_qry2  ||p_where_clause;
3792 
3793 
3794 
3795  IF ((p_arg_mode IS NULL) OR (p_arg_mode = 'WF')) THEN
3796   -- bulk collect l_sql_qry2 into lv_IRO_list;
3797   EXECUTE IMMEDIATE l_sql_qry2
3798     bulk collect INTO
3799     lv_transaction_id_lst,
3800     lv_quantity_lst,
3801     lv_in_req_quantity_lst,
3802     lv_out_req_quantity_lst,
3803     lv_in_req_transaction_id_lst,
3804     lv_out_req_transaction_id_lst,
3805     lv_inventory_item_id_lst,
3806     lv_uom_code_lst,
3807     lv_organization_id_lst,
3808     lv_promise_date_lst,
3809     lv_implement_location_id,
3810     lv_load_type_lst,
3811     lv_implement_source_org_id,
3812     lv_orig_transaction_id_lst
3813  ;
3814  END IF;
3815 
3816    p_where_clause :=null ;
3817 
3818 
3819   /* 4.1 iterate through  the record in table type and update the load type for transaction_id corresponding to in_req_transaction_id to  32 */
3820      for i IN 1 .. lv_orig_transaction_id_lst.count LOOP
3821 
3822 
3823               UPDATE msc_supplies
3824               SET --load_type = 32,
3825                  release_status = 2,
3826                 -- implement_quantity = new_order_quantity,  --to be removed if above api works fine
3827                  --release_errors = NULL,
3828                 batch_id=decode(v_batch_id_populated,2,batch_id,g_batch_id)
3829               WHERE transaction_id = lv_orig_transaction_id_lst(i)
3830                 and plan_id =p_arg_plan_id ;
3831       END LOOP;
3832 
3833      for i IN 1 .. lv_in_req_transaction_id_lst.count LOOP
3834 
3835          UPDATE msc_part_supplies
3836               SET load_type = 32,
3837                 --release_status = 2,
3838                 --implement_quantity = new_order_quantity,  --to be removed if above api works fine
3839                 release_errors = NULL,
3840                 batch_id=decode(v_batch_id_populated,2,batch_id,g_batch_id)
3841               WHERE transaction_id in ( lv_in_req_transaction_id_lst(i),lv_out_req_transaction_id_lst(i))
3842               and plan_id =p_arg_plan_id ;
3843 
3844 
3845     END LOOP;
3846 
3847 
3848   /* 5. bulk insert the data in the table type to mrp_iro_release table in the source */
3849   FOR i IN 1 .. lv_transaction_id_lst.COUNT
3850   LOOP
3851 
3852     l_sql_qry := 'insert into mrp_iro_release' || p_dblink || '(Transaction_id,
3853                           Quantity,
3854                           in_req_quantity,
3855                           out_req_quantity,
3856                           In_req_transaction_id,
3857                           Out_req_transaction_id,
3858                           Batch_id,
3859                           Inventory_item_id,
3860                           Uom_code,
3861                           Organization_id,
3862                           Promise_date,
3863                           Load_Type,
3864                           deliver_to_location_id,
3865                           src_organization_id)
3866                   values (:lv_Transaction_id,
3867                           :lv_Quantity,
3868                           :lv_in_req_quantity,
3869                           :lv_out_req_quantity,
3870                           :lv_In_req_transaction_id,
3871                           :lv_Out_req_transaction_id,
3872                           :p_arg_iro_batch_number,
3873                           :lv_Inventory_item,
3874                           :lv_uom_code ,
3875                           :lv_organization_id,
3876                           :lv_Promise_date,
3877                           :Load_Type,
3878                           :implement_location_id,
3879                           :implement_source_org_id)';
3880 
3881     EXECUTE IMMEDIATE l_sql_qry USING
3882       lv_transaction_id_lst(i),
3883       lv_quantity_lst(i),
3884       lv_in_req_quantity_lst(i),
3885       lv_out_req_quantity_lst(i),
3886       lv_in_req_transaction_id_lst(i),
3887       lv_out_req_transaction_id_lst(i),
3888       p_arg_iro_batch_number,
3889       lv_inventory_item_id_lst(i),
3890       lv_uom_code_lst(i),
3891       lv_organization_id_lst(i),
3892       lv_promise_date_lst(i),
3893       lv_load_type_lst(i),
3894       lv_implement_location_id(i),
3895       lv_implement_source_org_id(i);
3896 
3897 
3898     p_arg_loaded_int_repair_orders := p_arg_loaded_int_repair_orders + 1 ;
3899 
3900         update msc_supplies  -- change the variable name to lv_orig_transaction_id
3901            set releasable = RELEASE_ATTEMPTED,
3902                batch_id   = nvl(batch_id,g_batch_id)
3903            where plan_id = p_arg_plan_id
3904            and transaction_id= lv_orig_transaction_id_lst(i);
3905 
3906         /*    update msc_part_supplies
3907            set releasable = RELEASE_ATTEMPTED,
3908                batch_id   = nvl(batch_id,g_batch_id)
3909            where plan_id = p_arg_plan_id
3910            and transaction_id= lv_transaction_id_lst(i);*/
3911 
3912     END LOOP;
3913 
3914      /*
3915   OPEN c_row_count FOR l_sql_cur USING p_arg_iro_batch_number;
3916   FETCH c_row_count
3917   INTO l_after_row_count;
3918   CLOSE c_row_count;
3919   */
3920 
3921 
3922   --p_arg_loaded_int_repair_orders:= l_after_row_count -l_before_row_count;
3923 
3924 
3925   /* 6. call POPULATE_ISO_IN_SOURCE_2 to populate mrp_org_transfer_release_table in the source */
3926 
3927   IF p_arg_loaded_int_repair_orders > 0 THEN
3928 
3929       populate_iso_in_source_2(l_dblink => p_dblink,
3930        l_arg_po_batch_number => p_arg_iro_batch_number,
3931        l_arg_owning_instance => p_arg_owning_instance,
3932        l_arg_po_group_by => p_arg_po_group_by,
3933        l_arg_plan_id => p_arg_plan_id,
3934        l_arg_log_org_id => p_arg_log_org_id,
3935        l_arg_owning_org_id => p_arg_owning_org_id,
3936        l_arg_org_instance => p_arg_org_instance,
3937        l_arg_mode => p_arg_mode,
3938        l_arg_transaction_id => p_arg_transaction_id,
3939        arg_loaded_int_reqs => p_new_iso_num,
3940        arg_resched_int_reqs => p_reschedule_iso_num,
3941        p_load_type => drp_req_load);
3942   END IF;
3943 
3944   l_sql_qry := NULL;
3945 
3946   Exception
3947   When Others Then
3948   Raise;
3949 END release_iro_2;
3950 
3951 PROCEDURE Release_Ero(
3952     p_dblink IN VARCHAR2,
3953     p_arg_ero_batch_number IN number,
3954     p_arg_owning_instance IN NUMBER,
3955     p_arg_po_group_by IN NUMBER,
3956     p_arg_plan_id IN NUMBER,
3957     p_arg_log_org_id IN NUMBER,
3958     p_arg_owning_org_id IN NUMBER,
3959     p_arg_org_instance IN NUMBER,
3960     p_arg_mode IN VARCHAR2,
3961     p_arg_transaction_id IN NUMBER,
3962     p_arg_loaded_ext_repair_orders IN OUT nocopy NUMBER,
3963     p_load_type IN NUMBER)
3964 IS
3965 
3966 l_sql_qry VARCHAR2(8000) := NULL;
3967 l_sql_cnt_qry VARCHAR2(500) := NULL;
3968 p_count NUMBER;
3969 BEGIN
3970 
3971   l_sql_qry := '
3972     Insert into MRP_ERO_RELEASE'||p_dblink||
3973     '  (TRANSACTION_ID,
3974     REPAIR_SUPPLIER_ID ,
3975     REPAIR_SUPPLIER_ORG_ID ,
3976     REPAIR_PROGRAM ,
3977     BATCH_ID ,
3978     DESTINATION_ORG_ID  ,
3979     SOURCE_ORG_ID ,
3980     INVENTORY_ITEM_ID ,
3981     PROMISE_DATE  ,
3982     QUANTITY  ,
3983     DEFECTIVE_ITEM_ID,
3984     DEFECTIVE_ITEM_QTY)
3985 Select
3986     s.transaction_id ,
3987     mtp1.sr_tp_id   repair_supplier_id ,
3988     s1.organization_id  repair_supplier_org_id ,
3989     msi.repair_program  repair_program,
3990      '||p_arg_ero_batch_number||'  ,
3991     s.organization_id  destination_organization_id ,
3992     d2.organization_id source_organization_id ,
3993     msi.sr_inventory_item_id repair_to_item_id ,
3994     nvl(s.implement_dock_date , nvl(s.new_dock_date, sysdate ) ) need_by_date,
3995     s.implement_quantity quantity,
3996     msi1.sr_inventory_item_id  defective_item_id,
3997     s2.new_order_quantity defective_part_quantity
3998 FROM msc_supplies s,
3999      Msc_supplies s1,
4000      Msc_supplies s2 ,
4001      msc_system_items msi,
4002      msc_system_items msi1,
4003      msc_trading_partners mtp,
4004      msc_trading_partners mtp1,
4005      msc_demands d  ,
4006      msc_demands d1,
4007      msc_demands d2,
4008     (  select distinct transaction_id,plan_id,organization_id ,sr_instance_id ,demand_id
4009              from msc_full_pegging
4010              where sr_instance_id = '||p_arg_org_instance||'
4011              and plan_id ='||p_arg_plan_id||'
4012       ) mfg,
4013      (  select  transaction_id,plan_id,organization_id ,sr_instance_id ,demand_id ,
4014                  sum(allocated_quantity) allocated_quantity
4015                 from msc_full_pegging
4016                 where sr_instance_id = '||p_arg_org_instance||'
4017                 and plan_id ='||p_arg_plan_id||'
4018                 group by transaction_id,plan_id,organization_id ,sr_instance_id ,demand_id
4019       ) mfg1
4020 WHERE s.release_errors is NULL
4021     AND (s.releasable = ' || RELEASABLE || ' or s.releasable is null )
4022     AND   s.implement_quantity > 0
4023     AND   s.sr_instance_id = '||p_arg_org_instance||
4024 '    AND   s.plan_id = '||p_arg_plan_id||'
4025     AND  ((:l_arg_mode is null and s.load_type = :p_load_type  and s.load_type IS NOT NULL) OR (:l_arg_mode = ''WF'' and s.transaction_id =:l_arg_transaction_id))
4026     AND   msi.inventory_item_id = s.inventory_item_id
4027     AND   msi.plan_id = s.plan_id
4028     AND   msi.organization_id = s.organization_id
4029     AND   msi.sr_instance_id = s.sr_instance_id
4030     AND   s.transaction_id =d.disposition_id
4031     AND   s.sr_instance_id = d.sr_instance_id
4032     And   s.plan_id =d.plan_id
4033     And   d.origination_type= 1 --- Planned order demand
4034     And   d.demand_id  = mfg.demand_id
4035     And   d.plan_id  = mfg.plan_id
4036     And   d.sr_instance_id =mfg.sr_instance_id
4037     And   d.organization_id=  mfg.organization_id
4038     And   mfg.transaction_id =s1.transaction_id
4039     And   mfg.plan_id=s1.plan_id
4040     AND   mtp.sr_tp_id  = s1.organization_id
4041     AND   mtp.sr_instance_id = s1.sr_instance_id
4042     AND   mtp.partner_type= 3
4043     And   mtp.modeled_supplier_id = mtp1.partner_id
4044     AND   s1.transaction_id =d1.disposition_id
4045     AND   s1.sr_instance_id = d1.sr_instance_id
4046     And   s1.plan_id =d1.plan_id
4047     And   d1.origination_type= 78 -- planned defective part demand
4048     And   d1.demand_id  = mfg1.demand_id
4049     And   d1.plan_id  = mfg1.plan_id
4050     And   d1.sr_instance_id =mfg1.sr_instance_id
4051     And   mfg1.organization_id = mfg1.organization_id
4052     And   mfg1.transaction_id = s2.transaction_id
4053     And   mfg1.plan_id =s2.plan_id
4054     AND   s2.transaction_id =d2.disposition_id
4055     AND   s2.sr_instance_id = d2.sr_instance_id
4056     And   s2.plan_id =d2.plan_id
4057     /* And   d2.origination_type= 1 -- planned defective out bound shipment */
4058     And   d2.origination_type in (1,30) -- defective ISO/pl out bound shipment (BUG 13258605)
4059     And   d2.sr_instance_id = msi1.sr_instance_id
4060     And   d2.plan_id = msi1.plan_id
4061     And   d2.inventory_item_id = msi1.inventory_item_id
4062     And   d2.organization_id = msi1.organization_id'  ;
4063 
4064     IF  v_batch_id_populated = 2 THEN
4065         l_sql_qry :=  l_sql_qry || ' AND  s.batch_id is NULL ';
4066     ELSE
4067         l_sql_qry :=  l_sql_qry || ' AND  s.batch_id = ' || g_batch_id || ' ' ;
4068     END IF;
4069 
4070  Execute immediate l_sql_qry
4071   USING
4072       p_arg_mode,
4073       p_load_type,
4074       p_arg_mode,
4075       p_arg_transaction_id;
4076 
4077    l_sql_cnt_qry := 'select count(*)
4078    from mrp_ero_release'||p_dblink|| '  where batch_id = '||p_arg_ero_batch_number;
4079 
4080    Execute immediate l_sql_cnt_qry
4081    INTO p_count;
4082 
4083    IF p_arg_mode = 'WF' THEN
4084         update msc_supplies
4085            set releasable = RELEASE_ATTEMPTED
4086                ,batch_id   = nvl(batch_id,g_batch_id)
4087          where plan_id = p_arg_plan_id
4088            and transaction_id= p_arg_transaction_id;
4089    ELSE
4090         execute immediate 'update msc_supplies
4091                                set releasable = ' || RELEASE_ATTEMPTED || '
4092                                ,batch_id   = nvl(batch_id,' ||g_batch_id || ')
4093                              where plan_id = ' || p_arg_plan_id || '
4094                                and transaction_id in
4095                                     (select transaction_id from mrp_ero_release' ||p_dblink||'
4096                                         where batch_id = ' || p_arg_ero_batch_number || ')';
4097    END IF;
4098 
4099   p_arg_loaded_ext_repair_orders := p_count;
4100 
4101   Exception
4102   When Others Then
4103   raise;
4104 
4105 END Release_Ero;
4106 
4107 PROCEDURE Release_Ero_2(
4108     p_dblink IN VARCHAR2,
4109     p_arg_ero_batch_number IN number,
4110     p_arg_owning_instance IN NUMBER,
4111     p_arg_po_group_by IN NUMBER,
4112     p_arg_plan_id IN NUMBER,
4113     p_arg_log_org_id IN NUMBER,
4114     p_arg_owning_org_id IN NUMBER,
4115     p_arg_org_instance IN NUMBER,
4116     p_arg_mode IN VARCHAR2,
4117     p_arg_transaction_id IN NUMBER,
4118     p_arg_loaded_ext_repair_orders IN OUT nocopy NUMBER,
4119     p_load_type IN NUMBER)
4120 IS
4121 l_sql_qry VARCHAR2(8000) := NULL;
4122 l_sql_cnt_qry VARCHAR2(500) := NULL;
4123 p_count NUMBER;
4124 BEGIN
4125 
4126   l_sql_qry := '
4127     Insert into MRP_ERO_RELEASE'||p_dblink||
4128     '  (TRANSACTION_ID,
4129     REPAIR_SUPPLIER_ID ,
4130     REPAIR_SUPPLIER_ORG_ID ,
4131     REPAIR_PROGRAM ,
4132     BATCH_ID ,
4133     DESTINATION_ORG_ID  ,
4134     SOURCE_ORG_ID ,
4135     INVENTORY_ITEM_ID ,
4136     PROMISE_DATE  ,
4137     QUANTITY  ,
4138     DEFECTIVE_ITEM_ID,
4139     DEFECTIVE_ITEM_QTY)
4140 Select
4141     s.transaction_id ,
4142     mtp1.sr_tp_id   repair_supplier_id ,
4143     s1.organization_id  repair_supplier_org_id ,
4144     msi.repair_program  repair_program,
4145      '||p_arg_ero_batch_number||'  ,
4146     s.organization_id  destination_organization_id ,
4147     d2.organization_id source_organization_id ,
4148     msi.sr_inventory_item_id repair_to_item_id ,
4149     nvl(s.implement_dock_date , nvl(s.new_dock_date, sysdate ) ) need_by_date,
4150     s.implement_quantity quantity,
4151     msi1.sr_inventory_item_id  defective_item_id,
4152     s2.new_order_quantity defective_part_quantity
4153 FROM
4154      msc_supplies k,
4155      msc_system_items msi,
4156      msc_part_supplies s,
4157      msc_part_demands d,
4158      Msc_part_supplies s1,
4159      msc_trading_partners mtp,
4160      msc_trading_partners mtp1,
4161      msc_part_demands d1,
4162      Msc_part_supplies s2,
4163      msc_part_demands d2,
4164      msc_system_items msi1,
4165     (select distinct transaction_id,plan_id,organization_id ,sr_instance_id ,demand_id
4166              from msc_part_pegging
4167              where sr_instance_id = '||p_arg_org_instance||'
4168              and plan_id ='||p_arg_plan_id||'
4169       ) mfg,
4170      (  select  transaction_id,plan_id,organization_id ,sr_instance_id ,demand_id ,
4171                  sum(allocated_quantity) allocated_quantity
4172                 from msc_part_pegging
4173                 where sr_instance_id = '||p_arg_org_instance||'
4174                 and plan_id ='||p_arg_plan_id||'
4175                 group by transaction_id,plan_id,organization_id ,sr_instance_id ,demand_id
4176       ) mfg1
4177 WHERE k.release_errors is NULL
4178     AND (k.releasable = ' || RELEASABLE || ' or k.releasable is null )
4179     AND   k.implement_quantity > 0
4180     AND   k.sr_instance_id = '||p_arg_org_instance||
4181 '   AND   k.plan_id = '||p_arg_plan_id||'
4182     AND  ((:l_arg_mode is null and k.load_type = :p_load_type and k.load_type IS NOT NULL) OR (:l_arg_mode = ''WF'' and k.transaction_id =:l_arg_transaction_id))
4183     AND k.TRANSACTION_ID = s.ORIG_TRANSACTION_ID
4184     AND   msi.inventory_item_id = s.inventory_item_id
4185     AND   msi.plan_id = s.plan_id
4186     AND   msi.organization_id = s.organization_id
4187     AND   msi.sr_instance_id = s.sr_instance_id
4188     AND   s.transaction_id =d.disposition_id
4189     AND   s.sr_instance_id = d.sr_instance_id
4190     And   s.plan_id =d.plan_id
4191     And   d.origination_type= 1 --- Planned order demand
4192     And   d.demand_id  = mfg.demand_id
4193     And   d.plan_id  = mfg.plan_id
4194     And   d.sr_instance_id =mfg.sr_instance_id
4195     And   d.organization_id=  mfg.organization_id
4196     And   mfg.transaction_id =s1.transaction_id
4197     And   mfg.plan_id=s1.plan_id
4198     AND   mtp.sr_tp_id  = s1.organization_id
4199     AND   mtp.sr_instance_id = s1.sr_instance_id
4200     AND   mtp.partner_type= 3
4201     And   mtp.modeled_supplier_id = mtp1.partner_id
4202     AND   s1.transaction_id =d1.disposition_id
4203     AND   s1.sr_instance_id = d1.sr_instance_id
4204     And   s1.plan_id =d1.plan_id
4205     And   d1.origination_type= 78 -- planned defective part demand
4206     And   d1.demand_id  = mfg1.demand_id
4207     And   d1.plan_id  = mfg1.plan_id
4208     And   d1.sr_instance_id =mfg1.sr_instance_id
4209     And   mfg1.organization_id = s2.organization_id
4210     And   mfg1.transaction_id = s2.transaction_id
4211     And   mfg1.plan_id =s2.plan_id
4212     AND   s2.transaction_id =d2.disposition_id
4213     AND   s2.sr_instance_id = d2.sr_instance_id
4214     And   s2.plan_id =d2.plan_id
4215     /* And   d2.origination_type= 1 -- planned defective out bound shipment */
4216     And   d2.origination_type in (1,30) -- defective ISO/pl out bound shipment (BUG 13258605)
4217     And   d2.sr_instance_id = msi1.sr_instance_id
4218     And   d2.plan_id = msi1.plan_id
4219     And   d2.inventory_item_id = msi1.inventory_item_id
4220     And   d2.organization_id = msi1.organization_id ' ;
4221 
4222     IF  v_batch_id_populated = 2 THEN
4223         l_sql_qry :=  l_sql_qry || ' AND  k.batch_id is NULL ';
4224     ELSE
4225         l_sql_qry :=  l_sql_qry || ' AND  k.batch_id = ' || g_batch_id || ' ' ;
4226     END IF;
4227 
4228  Execute immediate l_sql_qry
4229   USING
4230       p_arg_mode,
4231       p_load_type,
4232       p_arg_mode,
4233       p_arg_transaction_id;
4234 
4235  p_count := SQL%ROWCOUNT;
4236 
4237    IF p_arg_mode = 'WF' THEN
4238         update msc_supplies
4239            set releasable = RELEASE_ATTEMPTED
4240                ,batch_id   = nvl(batch_id,g_batch_id)
4241          where plan_id = p_arg_plan_id
4242            and transaction_id= p_arg_transaction_id;
4243 
4244            update msc_part_supplies
4245            set releasable = RELEASE_ATTEMPTED
4246                ,batch_id = g_batch_id
4247          where plan_id = p_arg_plan_id
4248            and orig_transaction_id= p_arg_transaction_id;
4249    ELSE
4250         execute immediate 'update msc_supplies
4251                                set releasable = ' || RELEASE_ATTEMPTED || '
4252                                ,batch_id   = nvl(batch_id,' ||g_batch_id || ')
4253                              where plan_id = ' || p_arg_plan_id || '
4254                                and transaction_id in (select orig_transaction_id from msc_part_supplies where transaction_id in
4255                                     (select transaction_id from mrp_ero_release' ||p_dblink||'
4256                                         where batch_id = ' || p_arg_ero_batch_number || '))';
4257 
4258         execute immediate 'update msc_part_supplies
4259                                set releasable = ' || RELEASE_ATTEMPTED || '
4260                                ,batch_id   =' || g_batch_id || '
4261                              where plan_id = ' || p_arg_plan_id || '
4262                                and transaction_id in
4263                                     (select transaction_id from mrp_ero_release' ||p_dblink||'
4264                                         where batch_id = ' || p_arg_ero_batch_number || ')';
4265    END IF;
4266 
4267    p_arg_loaded_ext_repair_orders := p_count;
4268 
4269 
4270   Exception
4271   When Others Then
4272   raise;
4273 
4274 END Release_Ero_2;
4275 
4276 
4277 FUNCTION load_osfm_lot_jobs
4278 ( arg_plan_id			IN      NUMBER
4279 , arg_log_org_id 		IN 	NUMBER
4280 , arg_org_instance              IN      NUMBER
4281 , arg_owning_org_id 		IN 	NUMBER
4282 , arg_owning_instance           IN      NUMBER
4283 , arg_user_id 			IN 	NUMBER
4284 , arg_wip_group_id              IN      NUMBER
4285 , arg_mode                      IN      VARCHAR2
4286 , arg_transaction_id            IN      NUMBER,
4287   l_apps_ver                    IN      VARCHAR2
4288 )RETURN NUMBER
4289 IS
4290    lv_loaded_jobs NUMBER := 0;
4291 
4292    TYPE NumTab  IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
4293 
4294 
4295    lv_transaction_id      NumTab;
4296    lv_instance_id         NumTab;
4297    lv_org_id              NumTab;
4298    lv_plan_id             NumTab;
4299    lv_details_enabled     NumTab;
4300    lv_agg_details         NumTab;
4301    lv_job_count           NUMBER;
4302    lv_release_details     NUMBER;
4303    lv_inflate_wip         NUMBER;
4304 
4305 
4306 BEGIN
4307 
4308   SELECT decode(nvl(FND_PROFILE.value('MSC_RELEASE_DTLS_REVDATE'),'Y'),'N',2,1),
4309 	DECODE(NVL(fnd_profile.value('MSC_INFLATE_WIP') ,'N'), 'N',2 ,1)
4310    	INTO lv_release_details,lv_inflate_wip
4311    	FROM dual;
4312 
4313 
4314   /* we release the lot based job details, only if it doesn't use aggregate resources */
4315 
4316  /* Details will NOT be released for
4317      a. Unconstrained Plan
4318      b. if the new_wip_start_date is null
4319      c. if the implement quantity or date is different then the planned quantity date
4320      d. if the revision date is different then the new_wip_start_date
4321         and the profile option setting : MSC_RELEASE_DTLS_REVDATE  = 'N'
4322      e. Non - Daily Bucketed Plans
4323       f.bug 4655420-Alternate BOM/Routing is changed during release.
4324   */
4325   /* Ignore the profile option
4326       MSC: Release WIP Dtls if Order Date different then BOM Revision Date.
4327      in case of RP plan
4328   */
4329    if (v_rp_plan = 1 ) then
4330      lv_release_details := 1;
4331    end if;
4332 l_sql_stmt := '   SELECT s.transaction_id,
4333    	  s.sr_instance_id,
4334    	  decode( mp.daily_material_constraints+ mp.daily_resource_constraints+
4335                   mp.weekly_material_constraints+ mp.weekly_resource_constraints+
4336                   mp.period_material_constraints+ mp.period_resource_constraints,12,2,
4337                decode(mpb.bucket_type,1,
4338                DECODE( s.implement_quantity, s.new_order_quantity,
4339                      DECODE( s.implement_date, s.new_schedule_date,
4340                       DECODE(NVL(s.implement_alternate_bom, ''-23453''),
4341                      			 NVL(s.alternate_bom_designator, ''-23453''),
4342                 				DECODE(NVL(s.implement_alternate_routing, ''-23453''),
4343                        			NVL(s.alternate_routing_designator, ''-23453''),
4344                         DECODE(trunc(msc_calendar.date_offset
4345                               (s.organization_id,
4346                                s.sr_instance_id,
4347                                1, --daily bucket
4348                                s.need_by_date ,
4349                                ceil(nvl(msi.fixed_lead_time,0) +
4350                  nvl(msi.variable_lead_time,0) * s.implement_quantity)*-1 )),
4351                      trunc(s.new_wip_start_date),1,
4352                                    decode(' || lv_release_details || ' ,2,2,1)),
4353                            2),
4354                          2),
4355                        2),
4356                      2),
4357                  2)),
4358          s.organization_id,
4359          s.plan_id
4360      FROM msc_supplies s,
4361           msc_plan_organizations_v orgs,
4362           msc_plan_buckets mpb,
4363           msc_system_items msi,
4364           msc_plans mp
4365     WHERE  mp.plan_id = :arg_plan_id
4366     AND   s.release_errors is NULL
4367     AND   nvl(s.cfm_routing_flag,0) = 3
4368     AND   s.implement_quantity > 0
4369     AND   s.organization_id = orgs.planned_organization
4370     AND   s.sr_instance_id = orgs.sr_instance_id
4371     AND   s.plan_id = :arg_plan_id
4372     AND   orgs.plan_id = :arg_plan_id
4373     AND   orgs.organization_id = :arg_owning_org_id
4374     AND   orgs.owning_sr_instance = :arg_owning_instance
4375     AND   orgs.sr_instance_id = :arg_org_instance ' ;
4376 
4377     IF arg_log_org_id <> arg_owning_org_id THEN
4378             l_sql_stmt :=  l_sql_stmt ||'      AND     orgs.planned_organization = ' || arg_log_org_id || ' ' ;
4379     END IF;
4380 
4381     IF arg_mode IS NULL THEN
4382         l_sql_stmt :=  l_sql_stmt ||'  AND s.load_type = 5 ';
4383         IF v_msc_released_only_by_user = 1 THEN
4384             l_sql_stmt :=  l_sql_stmt || ' AND s.last_updated_by = ' || v_user_id || ' ' ;
4385         END IF;
4386     ELSIF arg_mode = 'WF_BATCH' THEN
4387         l_sql_stmt :=  l_sql_stmt ||'  AND s.load_type = 5 ';
4388     ELSIF arg_mode = 'WF' THEN
4389         l_sql_stmt :=  l_sql_stmt ||'      AND s.transaction_id = ' || arg_transaction_id  || '  ' ;
4390     END IF;
4391 
4392         l_sql_stmt :=  l_sql_stmt || ' AND   s.plan_id = mpb.plan_id
4393     AND  (nvl(s.implement_date,s.new_schedule_date) between mpb.bkt_start_date and mpb.bkt_end_date)
4394     AND   s.new_wip_start_date IS NOT NULL
4395     AND   msi.inventory_item_id = s.inventory_item_id
4396     AND   msi.plan_id = s.plan_id
4397     AND   msi.organization_id = s.organization_id
4398     AND   msi.sr_instance_id = s.sr_instance_id
4399     and (s.releasable = ' || RELEASABLE || ' or s.releasable is null ) ';
4400 
4401     IF  v_batch_id_populated = 2 THEN
4402         l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id is NULL ';
4403     ELSE
4404         l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id = ' || g_batch_id || ' ' ;
4405     END IF;
4406     l_sql_stmt :=  l_sql_stmt || '
4407 UNION
4408   SELECT s.transaction_id,
4409           s.sr_instance_id,
4410           2 /* Details not enabled for Manual Planned orders */,
4411           s.organization_id,
4412           s.plan_id
4413      FROM msc_supplies s,
4414           msc_plan_organizations_v orgs
4415     WHERE s.release_errors is NULL
4416     AND   nvl(s.cfm_routing_flag,0) = 3
4417     AND   s.implement_quantity > 0
4418     AND   s.organization_id = orgs.planned_organization
4419     AND   s.sr_instance_id = orgs.sr_instance_id
4420     AND   s.plan_id = :arg_plan_id
4421     AND   orgs.plan_id = :arg_plan_id
4422     AND   orgs.organization_id = :arg_owning_org_id
4423     AND   orgs.owning_sr_instance = :arg_owning_instance
4424     AND   orgs.sr_instance_id = :arg_org_instance
4425     and   s.new_wip_start_date IS NULL
4426     and (s.releasable = ' || RELEASABLE || ' or s.releasable is null )' ;
4427 
4428     IF  v_batch_id_populated = 2 THEN
4429         l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id is NULL ';
4430     ELSE
4431         l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id = ' || g_batch_id || ' ' ;
4432     END IF;
4433 
4434     IF arg_log_org_id <> arg_owning_org_id THEN
4435             l_sql_stmt :=  l_sql_stmt ||'      AND     orgs.planned_organization = ' || arg_log_org_id || ' ' ;
4436     END IF;
4437 
4438     IF arg_mode IS NULL THEN
4439         l_sql_stmt :=  l_sql_stmt ||'  AND s.load_type = 5 ';
4440         IF v_msc_released_only_by_user = 1 THEN
4441             l_sql_stmt :=  l_sql_stmt || ' AND s.last_updated_by = ' || v_user_id || ' ' ;
4442         END IF;
4443     ELSIF arg_mode = 'WF_BATCH' THEN
4444         l_sql_stmt :=  l_sql_stmt ||'  AND s.load_type = 5 ';
4445     ELSIF arg_mode = 'WF' THEN
4446         l_sql_stmt :=  l_sql_stmt ||'      AND s.transaction_id = ' || arg_transaction_id  || '  ' ;
4447     END IF;
4448 
4449     lv_job_count:= 0;
4450     IF arg_mode IS NULL OR arg_mode = 'WF_BATCH' OR arg_mode = 'WF' THEN
4451         EXECUTE IMMEDIATE l_sql_stmt
4452               BULK COLLECT     INTO   lv_transaction_id,
4453                                       lv_instance_id,
4454                                       lv_details_enabled,
4455                                       lv_org_id,
4456                                       lv_plan_id
4457                                     USING  arg_plan_id
4458                                             ,arg_plan_id
4459                                             ,arg_plan_id
4460                                             ,arg_owning_org_id
4461                                             ,arg_owning_instance
4462                                             ,arg_org_instance
4463                                             ,arg_plan_id
4464                                             ,arg_plan_id
4465                                             ,arg_owning_org_id
4466                                             ,arg_owning_instance
4467                                             ,arg_org_instance;
4468         lv_job_count:= SQL%ROWCOUNT;
4469     END IF;
4470 
4471 
4472 
4473     -- -----------------------------------------------------------------------
4474     -- Perform the wip discrete job mass load
4475     -- -----------------------------------------------------------------------
4476        /* Due to we only give PLANNED components, BILL_RTG_EXPLOSION_FLAG
4477           is set to 'Y'.  */
4478 
4479          FOR k in 1..lv_job_count
4480        Loop
4481                 Begin
4482                   SELECT 2
4483                   Into lv_agg_details(k)
4484                   FROM msc_department_resources deptres,
4485                        msc_resource_requirements resreq
4486                  WHERE resreq.sr_instance_id= lv_instance_id(k)
4487                    AND resreq.supply_id = lv_transaction_id(k)
4488                    AND resreq.organization_id= lv_org_id(k)
4489                    AND resreq.plan_id   = lv_plan_id(k)
4490                    AND resreq.parent_id   = 2
4491                    AND deptres.plan_id  = resreq.plan_id
4492                    AND deptres.sr_instance_id= resreq.sr_instance_id
4493                    AND deptres.resource_id= resreq.resource_id
4494                    AND deptres.department_id= resreq.department_id
4495                    AND deptres.organization_id= resreq.organization_id
4496                    AND deptres.aggregate_resource_flag= 1
4497                    AND rownum=1;
4498                   Exception
4499                   When no_data_found
4500                   then
4501                   lv_agg_details(k) := 1;
4502                   End;
4503 
4504        End Loop;
4505 
4506 
4507 
4508     FORALL j IN 1..lv_job_count
4509     INSERT INTO msc_wip_job_schedule_interface
4510             (last_update_date,
4511              cfm_routing_flag,
4512             last_updated_by,
4513             last_update_login,
4514             creation_date,
4515             created_by,
4516             group_id,
4517             source_code,
4518             source_line_id,
4519             organization_id,
4520             organization_type,
4521             load_type,
4522             status_type,
4523             first_unit_start_date,
4524             last_unit_completion_date,
4525             bom_revision_date,
4526             routing_revision_date,
4527             primary_item_id,
4528             class_code,
4529             job_name,
4530             firm_planned_flag,
4531             start_quantity,
4532 	    net_quantity,
4533             demand_class,
4534             project_id,
4535             task_id,
4536 	    schedule_group_id,
4537        	    build_sequence,
4538 	    line_id,
4539 	    alternate_bom_designator,
4540 	    alternate_routing_designator,
4541 	    end_item_unit_number,
4542 	    process_phase,
4543 	    process_status,
4544             bom_reference_id,
4545             routing_reference_id,
4546             BILL_RTG_EXPLOSION_FLAG,
4547             HEADER_ID,
4548             uom_code, --Outbound Changes for XML
4549             SR_INSTANCE_ID
4550             -- dsr: 2 new columns
4551             , schedule_priority
4552             , requested_completion_date
4553             )
4554     SELECT  SYSDATE,
4555             nvl(s.cfm_routing_flag,0),
4556             arg_user_id,
4557             s.last_update_login,
4558             SYSDATE,
4559             arg_user_id,
4560             arg_wip_group_id,
4561             'MSC',
4562             s.transaction_id,
4563             msi.organization_id,
4564             tp.organization_type,
4565             5,
4566             s.implement_status_code,
4567             decode(lv_details_enabled(j),1,new_wip_start_date,null),
4568             decode(v_unconstrained_plan,0,s.implement_date,1,trunc(s.implement_date)+v_time_stamp),
4569             s.new_wip_start_date,
4570             s.new_wip_start_date,
4571             msi.sr_inventory_item_id,
4572             s.implement_wip_class_code,
4573             s.implement_job_name,
4574             s.implement_firm,
4575             decode(s.implement_quantity,s.new_order_quantity,
4576                                           nvl(s.wip_start_quantity,s.implement_quantity),
4577                                         s.implement_quantity),
4578 	    s.implement_quantity,
4579             s.implement_demand_class,
4580             s.implement_project_id,
4581             s.implement_task_id,
4582 	    s.implement_schedule_group_id,
4583 	    s.implement_build_sequence,
4584        	    s.implement_line_id,
4585 	    s.implement_alternate_bom,
4586 	    s.implement_alternate_routing,
4587  	    s.implement_unit_number,
4588 	    2,
4589 	    1,
4590             DECODE( tp.organization_type,
4591                     2, mpe.bill_sequence_id,
4592                     NULL),
4593             DECODE( tp.organization_type,
4594                     2, mpe.routing_sequence_id,
4595                     NULL),
4596             'Y',
4597             s.transaction_id,
4598             nvl(s.implement_uom_code,msi.uom_code),
4599             s.sr_instance_id
4600              -- dsr: 2 new columns
4601             , s.schedule_priority
4602             , nvl(s.requested_completion_date, s.need_by_date)
4603       FROM  msc_trading_partners    tp,
4604             msc_parameters          param,
4605             msc_system_items        msi,
4606             msc_process_effectivity mpe,
4607             msc_supplies            s
4608     WHERE   tp.sr_tp_id= msi.organization_id
4609     AND     tp.sr_instance_id= msi.sr_instance_id
4610     AND     tp.partner_type=3
4611     AND     param.organization_id = msi.organization_id
4612     AND     param.sr_instance_id  = msi.sr_instance_id
4613     AND     msi.inventory_item_id = s.inventory_item_id
4614     AND     msi.plan_id = s.plan_id
4615     AND     msi.organization_id = s.organization_id
4616     AND     msi.sr_instance_id = s.sr_instance_id
4617     AND     mpe.plan_id(+)= s.plan_id
4618     AND     mpe.process_sequence_id(+)= s.process_seq_id
4619     AND     s.transaction_id= lv_transaction_id(j)
4620     AND     s.sr_instance_id= lv_instance_id(j)
4621     AND     s.plan_id= arg_plan_id
4622     AND     nvl(s.cfm_routing_flag,0) = 3;
4623 
4624 
4625     IF SQL%ROWCOUNT > 0
4626     THEN
4627         lv_loaded_jobs := SQL%ROWCOUNT;
4628 
4629     ELSE
4630         lv_loaded_jobs := 0;
4631 
4632     END IF;
4633 
4634         -- ------------------------------------------------------------------------
4635     -- Perform the lot-based job mass load for the details
4636     -- -----------------------------------------------------------------------
4637 
4638     /* lot-based job details are released only when the source profile WSM: Create Lot Based Job Routing is Yes
4639     and org planning parameter is primary */
4640 
4641 
4642     /* OPERATION NETWORKS */
4643 
4644 
4645     FORALL j IN 1..lv_job_count
4646     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
4647     (last_update_date,
4648      last_updated_by,
4649      last_update_login,
4650      creation_date,
4651      created_by,
4652      group_id,
4653      parent_header_id,
4654      SUBSTITUTION_TYPE,
4655      LOAD_TYPE,
4656      process_phase,
4657      process_status,
4658      OPERATION_SEQ_NUM,
4659      NEXT_ROUTING_OP_SEQ_NUM,
4660      cfm_routing_flag,
4661      SR_INSTANCE_ID)
4662   (SELECT  SYSDATE,
4663             arg_user_id,
4664             s.last_update_login,
4665             SYSDATE,
4666             arg_user_id,
4667             arg_wip_group_id,
4668             s.transaction_id,
4669             4,
4670             5,
4671             1,
4672             1,
4673             nwk.from_op_seq_num,
4674             nwk.to_op_seq_num,
4675             3,
4676             s.sr_instance_id
4677    From msc_supplies s,
4678    msc_operation_networks nwk,
4679    msc_apps_instances ins,
4680    msc_parameters param
4681    Where    nwk.plan_id = s.plan_id
4682     AND     nwk.sr_instance_id = s.sr_instance_id
4683     AND     nwk.routing_sequence_id = s.routing_sequence_id
4684     AND     nwk.transition_type = 1
4685     AND     s.transaction_id= lv_transaction_id(j)
4686     AND     s.sr_instance_id= lv_instance_id(j)
4687     AND     s.plan_id= arg_plan_id
4688     AND     lv_details_enabled(j)= 1
4689     AND     lv_agg_details(j) = 1
4690     AND     ins.instance_id = lv_instance_id(j)
4691     AND     nvl(ins.lbj_details,2) = 1
4692     AND     param.organization_id = s.organization_id
4693     AND     param.sr_instance_id = s.sr_instance_id
4694     AND     param.network_scheduling_method = 1);
4695 
4696 
4697     /* Operations */
4698 
4699 FORALL j IN 1..lv_job_count
4700     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
4701     (last_update_date,
4702      last_updated_by,
4703      last_update_login,
4704      creation_date,
4705      created_by,
4706      group_id,
4707      parent_header_id,
4708      SUBSTITUTION_TYPE,
4709      LOAD_TYPE,
4710      process_phase,
4711      process_status,
4712      OPERATION_SEQ_NUM,
4713      first_unit_start_date,
4714      last_unit_completion_date,
4715      cfm_routing_flag,
4716      SR_INSTANCE_ID,
4717      scheduled_quantity)
4718   (SELECT  SYSDATE,
4719             arg_user_id,
4720             s.last_update_login,
4721             SYSDATE,
4722             arg_user_id,
4723             arg_wip_group_id,
4724             s.transaction_id,
4725             4,
4726             3,
4727             1,
4728             1,
4729             res.operation_seq_num,
4730             min(res.START_DATE),
4731             max(res.END_DATE),
4732             3,
4733             s.sr_instance_id,
4734             max(nvl(res.CUMMULATIVE_QUANTITY,0))
4735    From msc_supplies s,
4736    msc_resource_requirements res,
4737    msc_apps_instances ins,
4738    msc_parameters param
4739    Where    res.plan_id = s.plan_id
4740     AND     res.sr_instance_id = s.sr_instance_id
4741     AND     s.transaction_id = res.supply_id
4742     AND     res.parent_id = 2
4743     -- AND     res.resource_id <> -1 	Bug#3432607
4744     -- AND     res.department_id <> -1
4745     AND     s.transaction_id= lv_transaction_id(j)
4746     AND     s.sr_instance_id= lv_instance_id(j)
4747     AND     s.plan_id= arg_plan_id
4748     AND     lv_details_enabled(j)= 1
4749     AND     lv_agg_details(j) = 1
4750     AND     ins.instance_id = lv_instance_id(j)
4751     AND     nvl(ins.lbj_details,2) = 1
4752     AND     param.organization_id = s.organization_id
4753     AND     param.sr_instance_id = s.sr_instance_id
4754     AND     param.network_scheduling_method = 1
4755     GROUP BY
4756             s.last_update_login,
4757             s.transaction_id,
4758             res.OPERATION_SEQ_NUM,
4759             s.sr_instance_id);
4760          --   res.CUMMULATIVE_QUANTITY);
4761 
4762     /* Resources */
4763 FORALL j IN 1..lv_job_count
4764     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
4765     (last_update_date,
4766      last_updated_by,
4767      last_update_login,
4768      creation_date,
4769      created_by,
4770      group_id,
4771      parent_header_id,
4772      SUBSTITUTION_TYPE,
4773      LOAD_TYPE,
4774      process_phase,
4775      process_status,
4776      operation_seq_num,
4777      -- dsr resource_seq_num,
4778      resource_id_new,
4779      start_date,
4780      completion_date,
4781      alternate_num,
4782      cfm_routing_flag,
4783      SR_INSTANCE_ID
4784     -- dsr: add following columns
4785      , firm_flag
4786      , setup_id
4787      , group_sequence_id
4788      , group_sequence_number
4789      , batch_id
4790      , maximum_assigned_units
4791      , parent_seq_num
4792      , resource_seq_num
4793      , schedule_seq_num
4794      , assigned_units
4795      , usage_rate_or_amount
4796      , scheduled_flag
4797 	 )
4798   (SELECT  SYSDATE,
4799             arg_user_id,
4800             s.last_update_login,
4801             SYSDATE,
4802             arg_user_id,
4803             arg_wip_group_id,
4804             s.transaction_id,
4805             decode(res.parent_seq_num, null,4,2),
4806             1,
4807             1,
4808             1,
4809             res.operation_seq_num,
4810            -- res.resource_seq_num,
4811             res.resource_id,
4812             nvl(res.firm_start_date,res.START_DATE),
4813             nvl(res.firm_end_date,res.END_DATE),
4814             nvl(res.alternate_num,0),
4815             3,
4816             s.sr_instance_id
4817             -- dsr: add following columns
4818             , res.firm_flag
4819 	    , res.setup_id
4820 	    , res.group_sequence_id
4821 	   , res.group_sequence_number
4822            , res.batch_number
4823 	   , res.maximum_assigned_units
4824 	  , res.parent_seq_num
4825 	  , res.orig_resource_seq_num
4826 	  , res.resource_seq_num
4827 	  , res.assigned_units
4828 	  , decode(res.parent_seq_num, null, (res.RESOURCE_HOURS/decode(res.basis_type,2,1,
4829                     nvl(res.cummulative_quantity,
4830                              decode(msi.rounding_control_type,1,
4831                ROUND(s.new_order_quantity/nvl(res.REVERSE_CUMULATIVE_YIELD,1) ,6),
4832                     (s.new_order_quantity/nvl(res.REVERSE_CUMULATIVE_YIELD,1))
4833                             ) ) ))* decode(mdr.efficiency,NULL,100,0,100,mdr.efficiency)/100 * decode(mdr.utilization,NULL,100,0,100,mdr.utilization)/100, res.RESOURCE_HOURS)
4834            ,  decode(nvl(res.schedule_flag,1),-23453,1,1,1,res.schedule_flag)
4835    From msc_supplies s,
4836    msc_resource_requirements res,
4837    msc_apps_instances ins,
4838    msc_parameters param,
4839    msc_department_resources mdr,
4840    msc_system_items        msi
4841    Where    res.plan_id = s.plan_id
4842     AND     res.sr_instance_id = s.sr_instance_id
4843     AND     s.transaction_id = res.supply_id
4844     AND     res.parent_id = 2
4845     AND     res.resource_id <> -1
4846     AND     res.department_id <> -1
4847     AND     s.transaction_id= lv_transaction_id(j)
4848     AND     s.sr_instance_id= lv_instance_id(j)
4849     AND     s.plan_id= arg_plan_id
4850     AND     lv_details_enabled(j)= 1
4851     AND     lv_agg_details(j) = 1
4852     AND     ins.instance_id = lv_instance_id(j)
4853     AND     nvl(ins.lbj_details,2) = 1
4854     AND     param.organization_id = s.organization_id
4855     AND     param.sr_instance_id = s.sr_instance_id
4856     AND     res.plan_id = mdr.plan_id
4857     AND     res.organization_id =mdr.organization_id
4858     AND     res.sr_instance_id = mdr.sr_instance_id
4859     AND     res.resource_id = mdr.resource_id
4860     AND     res.department_id=mdr.department_id
4861     AND     msi.inventory_item_id = s.inventory_item_id
4862     AND     msi.plan_id = s.plan_id
4863     AND     msi.organization_id = s.organization_id
4864     AND     msi.sr_instance_id = s.sr_instance_id
4865     AND     param.network_scheduling_method = 1);
4866 
4867     /*Components*/
4868 
4869     FORALL j IN 1..lv_job_count
4870     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
4871     (last_update_date,
4872      last_updated_by,
4873      last_update_login,
4874      creation_date,
4875      created_by,
4876      group_id,
4877      parent_header_id,
4878      SUBSTITUTION_TYPE,
4879      LOAD_TYPE,
4880      process_phase,
4881      process_status,
4882      operation_seq_num,
4883      inventory_item_id_new,
4884      primary_component_id,
4885      source_phantom_id,
4886      component_seq_id,
4887      mrp_net_flag,
4888      date_required,
4889      mps_date_required,
4890      basis_type,
4891      quantity_per_assembly,
4892      required_quantity,
4893      mps_required_quantity,
4894      cfm_routing_flag,
4895      SR_INSTANCE_ID)
4896   (SELECT  SYSDATE,
4897             arg_user_id,
4898             s.last_update_login,
4899             SYSDATE,
4900             arg_user_id,
4901             arg_wip_group_id,
4902             s.transaction_id,
4903             4,
4904             2,
4905             1,
4906             1,
4907             nvl(md.op_seq_num,1),
4908             icomp.sr_inventory_item_id,
4909             icomp1.sr_inventory_item_id,
4910             icomp2.sr_inventory_item_id,
4911             md.COMP_SEQ_ID,
4912             1,
4913             md.USING_ASSEMBLY_DEMAND_DATE,
4914             md.USING_ASSEMBLY_DEMAND_DATE,
4915             decode(md.component_scaling_type,1,NULL,md.component_scaling_type),
4916             md.quantity_per_assembly,
4917             md.USING_REQUIREMENT_QUANTITY,
4918             md.USING_REQUIREMENT_QUANTITY,
4919             3,
4920             s.sr_instance_id
4921    From msc_supplies s,
4922    msc_demands md,
4923    msc_system_items icomp,
4924    msc_system_items icomp1,
4925    msc_system_items icomp2,
4926    msc_apps_instances ins,
4927    msc_parameters param
4928    Where   /* not exists (select 'x'
4929                         from msc_exception_details excp
4930                         where excp.plan_id = s.plan_id
4931                         and excp.number1 = s.transaction_id
4932                         and excp.inventory_item_id = s.inventory_item_id
4933                         and excp.organization_id = s.organization_id
4934                         and excp.sr_instance_id = s.sr_instance_id
4935                         and excp.exception_type = 33
4936                         and excp.number2 = md.inventory_item_id)*/  /* not needed as inv_old need not be populated*/
4937     	    icomp.inventory_item_id= md.inventory_item_id
4938     AND     icomp.organization_id= md.organization_id
4939     AND     icomp.sr_instance_id= md.sr_instance_id
4940     AND     icomp.plan_id= md.plan_id
4941     AND     nvl(icomp.wip_supply_type,0) <> 6
4942     AND     icomp1.inventory_item_id= md.primary_component_id
4943     AND     icomp1.organization_id= md.organization_id
4944     AND     icomp1.sr_instance_id= md.sr_instance_id
4945     AND     icomp1.plan_id= md.plan_id
4946     AND     icomp2.inventory_item_id(+)= md.source_phantom_id
4947     AND     icomp2.organization_id(+)= md.organization_id
4948     AND     icomp2.sr_instance_id(+)= md.sr_instance_id
4949     AND     icomp2.plan_id(+)= md.plan_id
4950     AND     md.plan_id = s.plan_id
4951     AND     md.sr_instance_id = s.sr_instance_id
4952     AND     md.disposition_id= s.transaction_id
4953     AND     md.origination_type = 1
4954     AND     s.transaction_id= lv_transaction_id(j)
4955     AND     s.sr_instance_id= lv_instance_id(j)
4956     AND     s.plan_id= arg_plan_id
4957     AND     lv_details_enabled(j)= 1
4958     AND     lv_agg_details(j) = 1
4959     AND     ins.instance_id = lv_instance_id(j)
4960     AND     nvl(ins.lbj_details,2) = 1
4961     AND     param.organization_id = s.organization_id
4962     AND     param.sr_instance_id = s.sr_instance_id
4963     AND     param.network_scheduling_method = 1);
4964 
4965 
4966  /* Resource Usage */
4967  FORALL j IN 1..lv_job_count
4968     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
4969     (last_update_date,
4970      last_updated_by,
4971      last_update_login,
4972      creation_date,
4973      created_by,
4974      group_id,
4975      parent_header_id,
4976      SUBSTITUTION_TYPE,
4977      LOAD_TYPE,
4978      process_phase,
4979      process_status,
4980      operation_seq_num,
4981      -- dsr resource_seq_num,
4982      resource_id_new,
4983      assigned_units,
4984      alternate_num,
4985      start_date,
4986      completion_date,
4987      cfm_routing_flag,
4988      SR_INSTANCE_ID
4989      -- dsr: add folllowing columns
4990      , resource_seq_num
4991      , schedule_seq_num
4992      , parent_seq_num
4993      )
4994   (SELECT  SYSDATE,
4995             arg_user_id,
4996             s.last_update_login,
4997             SYSDATE,
4998             arg_user_id,
4999             arg_wip_group_id,
5000             s.transaction_id,
5001             4,
5002             4,
5003             1,
5004             1,
5005             res.operation_seq_num,
5006             -- dsr res.resource_seq_num,
5007             res.resource_id,
5008             res.assigned_units,
5009             nvl(res.alternate_num,0),
5010             nvl(res.firm_start_date,res.START_DATE),
5011             nvl(res.firm_end_date,res.END_DATE),
5012             3,
5013             s.sr_instance_id
5014              -- dsr: add folllowing columns
5015 	    , res.orig_resource_seq_num
5016 	    , res.resource_seq_num
5017 	    , res.parent_seq_num
5018    From msc_supplies s,
5019    msc_resource_requirements res,
5020    msc_apps_instances ins,
5021    msc_parameters param
5022    Where    res.plan_id = s.plan_id
5023     AND     res.sr_instance_id = s.sr_instance_id
5024     AND     s.transaction_id = res.supply_id
5025     AND     res.parent_id = 1
5026     AND     res.resource_id <> -1
5027     AND     res.department_id <> -1
5028     AND     s.transaction_id= lv_transaction_id(j)
5029     AND     s.sr_instance_id= lv_instance_id(j)
5030     AND     s.plan_id= arg_plan_id
5031     AND     lv_details_enabled(j)= 1
5032     AND     lv_agg_details(j) = 1
5033     AND     ins.instance_id = lv_instance_id(j)
5034     AND     nvl(ins.lbj_details,2) = 1
5035     AND     param.organization_id = s.organization_id
5036     AND     param.sr_instance_id = s.sr_instance_id
5037     AND     param.network_scheduling_method = 1);
5038 
5039     -- dsr begin: Operation Resource Instances
5040     FORALL j IN 1..lv_job_count
5041     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
5042           ( last_update_date,
5043             last_updated_by,
5044             last_update_login,
5045             creation_date,
5046             created_by,
5047             organization_type,
5048             organization_id,
5049             group_id,
5050             parent_header_id,
5051             operation_seq_num,
5052            -- resource_seq_num, rawasthi
5053             RESOURCE_INSTANCE_ID,
5054             start_date,
5055             completion_date,
5056             SUBSTITUTION_TYPE,
5057             LOAD_TYPE,
5058             process_phase,
5059             process_status,
5060             SR_INSTANCE_ID,
5061             operation_seq_id,
5062             FIRM_FLAG,
5063             resource_hours,
5064             department_id,
5065 	    SERIAL_NUMBER,
5066             group_sequence_id,
5067             group_sequence_number,
5068             batch_id
5069            , resource_seq_num
5070            , schedule_seq_num
5071            , parent_seq_num
5072            , cfm_routing_flag
5073            , resource_id_new
5074            , assigned_units
5075           )
5076     SELECT
5077            SYSDATE,
5078            arg_user_id,
5079            s.last_update_login,
5080            SYSDATE,
5081            arg_user_id,
5082            tp.organization_type,
5083            s.organization_id,
5084            arg_wip_group_id,
5085            s.transaction_id,
5086            resreq.OPERATION_SEQ_NUM,
5087         -- resreq.RESOURCE_SEQ_NUM,
5088            dep_res_inst.RES_INSTANCE_ID,
5089            nvl(resreq.firm_start_date,res_instreq.START_DATE),
5090            nvl(resreq.firm_end_date,res_instreq.END_DATE),
5091            SUBST_ADD, -- jguo SUBST_CHANGE,
5092            LT_RESOURCE_INSTANCE,
5093            2,
5094            1,
5095            s.sr_instance_id,
5096            resreq.operation_sequence_id,
5097            resreq.firm_flag,
5098            res_instreq.resource_instance_hours,
5099            resreq.department_id,
5100            dep_res_inst.serial_number,
5101            resreq.group_sequence_id,
5102            resreq.group_sequence_number,
5103            res_instreq.batch_number, ---- sbala res_instreq.res_inst_batch_id
5104            resreq.orig_resource_seq_num
5105 	  , resreq.resource_seq_num
5106 	  , resreq.parent_seq_num
5107 	  , 3
5108 	  , resreq.resource_id
5109 	  , 1
5110     FROM
5111            msc_trading_partners   tp,
5112            msc_resource_requirements resreq,
5113            msc_resource_instance_reqs res_instreq, -- changed from design doc
5114            msc_dept_res_instances dep_res_inst,
5115            msc_supplies            s,
5116            msc_apps_instances ins,
5117            msc_parameters param
5118     WHERE
5119          tp.sr_tp_id=s.organization_id
5120  AND     tp.sr_instance_id= s.sr_instance_id
5121  AND     tp.partner_type=3
5122  AND     resreq.sr_instance_id= s.sr_instance_id
5123  AND     resreq.organization_id= s.organization_id
5124  AND     resreq.supply_id = s.transaction_id
5125  AND     resreq.plan_id   = s.plan_id
5126 --    AND     resreq.transaction_id = res_instreq.res_inst_transaction_id
5127  AND     resreq.sr_instance_id = res_instreq.sr_instance_id
5128  AND     resreq.plan_id = res_instreq.plan_id
5129  AND     resreq.resource_seq_num = res_instreq.resource_seq_num
5130  AND     resreq.operation_seq_num = res_instreq.operation_seq_num
5131  AND     resreq.resource_id = res_instreq.resource_id
5132  AND     resreq.supply_id = res_instreq.supply_id
5133  AND     resreq.parent_id = res_instreq.parent_id  --rawasthi
5134  AND     resreq.start_date = res_instreq.start_date
5135  AND     resreq.parent_id   = 2
5136  AND     resreq.resource_id <> -1
5137  AND     resreq.department_id <> -1
5138 --    AND     res_instreq.res_instance_id = dep_res_inst.dept_resource_inst_id
5139  AND    res_instreq.plan_id = dep_res_inst.plan_id
5140  AND    res_instreq.sr_instance_id = dep_res_inst.sr_instance_id
5141  AND    res_instreq.department_id = dep_res_inst.department_id
5142  AND    res_instreq.resource_id = dep_res_inst.resource_id
5143 /* anuj: serail number and resource_instance id joins */
5144  AND    res_instreq.serial_number = dep_res_inst.serial_number
5145  AND    res_instreq.res_instance_id = dep_res_inst.res_instance_id
5146  AND    s.transaction_id= lv_transaction_id(j)
5147  AND    s.sr_instance_id= lv_instance_id(j)
5148  AND    s.plan_id= arg_plan_id
5149  AND    lv_details_enabled(j)= 1
5150  AND    lv_agg_details(j) = 1
5151  AND    ins.instance_id = lv_instance_id(j)
5152  AND    nvl(ins.lbj_details,2) = 1
5153  AND    param.organization_id = s.organization_id
5154  AND    param.sr_instance_id = s.sr_instance_id
5155  AND    param.network_scheduling_method = 1
5156 	;
5157 
5158 
5159   /*print_debug_info( 'Operation Resource Instances: rows inserted into MSC_WIP_JOB_DTLS_INTERFACE = '
5160   						|| SQL%ROWCOUNT
5161 						); */
5162 
5163    -- dsr: RESOURCE INSTANCE USAGES
5164 
5165 FORALL j IN 1..lv_job_count
5166     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
5167           ( last_update_date,
5168             last_updated_by,
5169             last_update_login,
5170             creation_date,
5171             created_by,
5172             organization_type,
5173             organization_id,
5174             group_id,
5175             parent_header_id,
5176             operation_seq_num,
5177            -- resource_seq_num, rawasthi
5178             RESOURCE_INSTANCE_ID,
5179             start_date,
5180             completion_date,
5181             SUBSTITUTION_TYPE,
5182             LOAD_TYPE,
5183             process_phase,
5184             process_status,
5185             SR_INSTANCE_ID,
5186             operation_seq_id,
5187             FIRM_FLAG,
5188             resource_hours,
5189             department_id,
5190             serial_number,
5191             resource_seq_num,
5192             schedule_seq_num,
5193             parent_seq_num,
5194             cfm_routing_flag,
5195             resource_id_new,
5196             assigned_units
5197      )
5198     SELECT
5199             SYSDATE,
5200             arg_user_id,
5201             s.last_update_login,
5202             SYSDATE,
5203             arg_user_id,
5204             tp.organization_type,
5205             s.organization_id,
5206             arg_wip_group_id,
5207             s.transaction_id,
5208             resreq.OPERATION_SEQ_NUM,
5209          -- resreq.RESOURCE_SEQ_NUM,
5210             dep_res_inst.RES_INSTANCE_ID,
5211             nvl(resreq.firm_start_date,res_instreq.START_DATE),
5212             nvl(resreq.firm_end_date,res_instreq.END_DATE),
5213             SUBST_ADD, -- jguo SUBST_CHANGE,
5214             LT_RESOURCE_INST_USAGE,
5215             2,
5216             1,
5217             s.sr_instance_id,
5218             resreq.operation_sequence_id,
5219             resreq.firm_flag,
5220             res_instreq.resource_instance_hours,
5221             resreq.department_id,
5222             dep_res_inst.serial_number,
5223             resreq.orig_resource_seq_num
5224 	  , resreq.resource_seq_num
5225 	  , resreq.parent_seq_num
5226 	  , 3
5227 	  , resreq.resource_id
5228 	  , 1
5229      FROM
5230             msc_trading_partners   tp,
5231             msc_resource_requirements resreq,
5232             msc_resource_instance_reqs res_instreq,
5233             msc_dept_res_instances dep_res_inst,
5234             msc_supplies            s,
5235             msc_apps_instances ins,
5236   	    msc_parameters param
5237     WHERE
5238             tp.sr_tp_id=s.organization_id
5239     AND     tp.sr_instance_id= s.sr_instance_id
5240     AND     tp.partner_type=3
5241     AND     resreq.sr_instance_id= s.sr_instance_id
5242     AND     resreq.organization_id= s.organization_id
5243     AND     resreq.supply_id = s.transaction_id
5244     AND     resreq.plan_id   = s.plan_id
5245 --    AND     resreq.transaction_id = res_instreq.res_inst_transaction_id
5246     AND     resreq.sr_instance_id = res_instreq.sr_instance_id
5247     AND     resreq.plan_id = res_instreq.plan_id
5248     AND     resreq.resource_seq_num = res_instreq.resource_seq_num
5249     AND     resreq.operation_seq_num = res_instreq.operation_seq_num
5250     AND     resreq.resource_id = res_instreq.resource_id
5251     AND     resreq.supply_id = res_instreq.supply_id
5252     AND     resreq.parent_id = res_instreq.parent_id  --rawasthi
5253     AND     resreq.start_date = res_instreq.start_date
5254     AND     resreq.parent_id   = 1
5255     AND     resreq.resource_id <> -1
5256     AND     resreq.department_id <> -1
5257 --    AND     res_instreq.res_instance_id = dep_res_inst.dept_resource_inst_id
5258     AND     res_instreq.department_id = dep_res_inst.department_id
5259     AND     res_instreq.resource_id = dep_res_inst.resource_id
5260     AND     res_instreq.plan_id = dep_res_inst.plan_id
5261     AND     res_instreq.sr_instance_id = dep_res_inst.sr_instance_id
5262 /* anuj: serail number and resource_instance id joins */
5263     AND     res_instreq.serial_number = dep_res_inst.serial_number
5264     AND     res_instreq.res_instance_id = dep_res_inst.res_instance_id
5265     AND     s.transaction_id= lv_transaction_id(j)
5266     AND     s.sr_instance_id= lv_instance_id(j)
5267     AND     s.plan_id= arg_plan_id
5268     AND     lv_details_enabled(j)= 1
5269     AND     lv_agg_details(j) = 1
5270     AND     ins.instance_id = lv_instance_id(j)
5271     AND     nvl(ins.lbj_details,2) = 1
5272     AND     param.organization_id = s.organization_id
5273     AND     param.sr_instance_id = s.sr_instance_id
5274     AND     param.network_scheduling_method = 1
5275 	;
5276 
5277 /*  print_debug_info( 'Resource Instance Usage: rows inserted into MSC_WIP_JOB_DTLS_INTERFACE = '
5278   						|| SQL%ROWCOUNT
5279 	 				); */
5280 
5281 	-- dsr end
5282     FORALL j IN 1..lv_job_count
5283         update msc_supplies
5284            set releasable = RELEASE_ATTEMPTED
5285                ,batch_id   = nvl(batch_id,g_batch_id)
5286          where plan_id = arg_plan_id
5287            and transaction_id= lv_transaction_id(j);
5288 
5289     return lv_loaded_jobs;
5290 
5291 END load_osfm_lot_jobs;
5292 
5293 
5294 
5295 FUNCTION reschedule_osfm_lot_jobs
5296 ( arg_plan_id			IN      NUMBER
5297 , arg_log_org_id 		IN 	NUMBER
5298 , arg_org_instance              IN      NUMBER
5299 , arg_owning_org_id 		IN 	NUMBER
5300 , arg_owning_instance           IN      NUMBER
5301 , arg_user_id 			IN 	NUMBER
5302 , arg_wip_group_id 		IN 	NUMBER
5303 , arg_mode                      IN      VARCHAR2
5304 , arg_transaction_id            IN      NUMBER
5305 )RETURN NUMBER
5306 IS
5307 
5308 TYPE NumTab  IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
5309 
5310    lv_resched_jobs    NUMBER;
5311    lv_transaction_id  NumTab;
5312    lv_instance_id     NumTab;
5313    lv_details_enabled NumTab;
5314    lv_org_id	      NumTab;
5315    lv_plan_id         NumTab;
5316    lv_agg_details     NumTab;
5317 
5318 BEGIN
5319 
5320 	/* Details will not be released
5321 		for Non - Daily Bucketed Plans
5322 		if the implement quantity or date is different then the planned quantity date
5323 		if the Lot-based job uses aggregate resources
5324 		if the job has faulty network*/
5325 
5326 	l_sql_stmt :=  ' SELECT s.transaction_id,
5327           s.sr_instance_id,
5328           decode( mp.daily_material_constraints+ mp.daily_resource_constraints+
5329                   mp.weekly_material_constraints+ mp.weekly_resource_constraints+
5330                   mp.period_material_constraints+ mp.period_resource_constraints,12,
5331                2,
5332               DECODE(nvl(s.wsm_faulty_network,2),2,Decode(mpb.bucket_type,1,DECODE( s.implement_quantity,
5333                   s.new_order_quantity, DECODE( s.implement_date,
5334                                                 s.new_schedule_date, 1,
5335                                                 2),
5336                   2),2),2)),
5337            s.organization_id,
5338            s.plan_id
5339      FROM msc_plans mp,
5340           msc_supplies s,
5341           msc_plan_organizations_v orgs,
5342           msc_plan_buckets mpb
5343     WHERE mp.plan_id = :arg_plan_id
5344     AND   s.release_errors is NULL
5345     AND   s.implement_quantity > 0
5346     AND   s.organization_id = orgs.planned_organization
5347     AND   s.sr_instance_id = orgs.sr_instance_id
5348     AND   s.plan_id = :arg_plan_id
5349     AND   orgs.plan_id = :arg_plan_id
5350     AND   orgs.organization_id = :arg_owning_org_id
5351     AND   orgs.owning_sr_instance = :arg_owning_instance
5352     AND   orgs.sr_instance_id = :arg_org_instance
5353     and  (s.releasable = ' || RELEASABLE || ' or s.releasable is null )';
5354 
5355     IF  v_batch_id_populated = 2 THEN
5356         l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id is NULL ';
5357     ELSE
5358         l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id = ' || g_batch_id || ' ' ;
5359     END IF;
5360 
5361     IF arg_log_org_id <> arg_owning_org_id THEN
5362             l_sql_stmt :=  l_sql_stmt ||'      AND     orgs.planned_organization = ' || arg_log_org_id || ' ' ;
5363     END IF;
5364 
5365     IF arg_mode IS NULL THEN
5366         l_sql_stmt :=  l_sql_stmt ||'  AND s.load_type = 6 ';
5367         IF v_msc_released_only_by_user = 1 THEN
5368             l_sql_stmt :=  l_sql_stmt || ' AND s.last_updated_by = ' || v_user_id || ' ' ;
5369         END IF;
5370     ELSIF arg_mode = 'WF_BATCH' THEN
5371         l_sql_stmt :=  l_sql_stmt ||'  AND s.load_type = 6 ';
5372     ELSIF arg_mode = 'WF' THEN
5373         l_sql_stmt :=  l_sql_stmt ||'      AND s.transaction_id = ' || arg_transaction_id  || '  ' ;
5374     END IF;
5375 
5376     l_sql_stmt :=  l_sql_stmt || ' and   s.plan_id = mpb.plan_id
5377     AND ( (DECODE(s.disposition_status_type ,
5378                   2,DECODE(SIGN( mp.curr_start_date-s.new_schedule_date),
5379                            1,mp.curr_start_date,
5380                            DECODE(SIGN(s.new_schedule_date-mp.curr_cutoff_date),
5381                                  1,mp.curr_cutoff_date,
5382                                  s.new_schedule_date)),
5383                   s.new_schedule_date ) BETWEEN mpb.bkt_start_date
5384                                         AND mpb.bkt_end_date)) ';
5385 
5386     lv_resched_jobs:= 0;
5387 
5388     IF arg_mode IS NULL OR arg_mode = 'WF_BATCH' OR arg_mode = 'WF' THEN
5389         EXECUTE IMMEDIATE l_sql_stmt
5390                BULK COLLECT      INTO lv_transaction_id,
5391                                       lv_instance_id,
5392                                       lv_details_enabled,
5393                                       lv_org_id,
5394                                       lv_plan_id
5395                                      USING  arg_plan_id
5396                                             ,arg_plan_id
5397                                             ,arg_plan_id
5398                                             ,arg_owning_org_id
5399                                             ,arg_owning_instance
5400                                             ,arg_org_instance ;
5401         lv_resched_jobs:= SQL%ROWCOUNT;
5402     END IF;
5403 
5404 
5405 
5406 
5407     FOR k in 1..lv_resched_jobs
5408        Loop
5409                 Begin
5410                   SELECT 2
5411                   Into lv_agg_details(k)
5412                   FROM msc_department_resources deptres,
5413                        msc_resource_requirements resreq
5414                  WHERE resreq.sr_instance_id= lv_instance_id(k)
5415                    AND resreq.supply_id = lv_transaction_id(k)
5416                    AND resreq.organization_id= lv_org_id(k)
5417                    AND resreq.plan_id   = lv_plan_id(k)
5418                    AND resreq.parent_id   = 2
5419                    AND deptres.plan_id  = resreq.plan_id
5420                    AND deptres.sr_instance_id= resreq.sr_instance_id
5421                    AND deptres.resource_id= resreq.resource_id
5422                    AND deptres.department_id= resreq.department_id
5423                    AND deptres.organization_id= resreq.organization_id
5424                    AND deptres.aggregate_resource_flag= 1
5425                    AND rownum=1;
5426                   Exception
5427                   When no_data_found
5428                   then
5429                   lv_agg_details(k) := 1;
5430                   End;
5431 
5432        End Loop;
5433 
5434     -- ------------------------------------------------------------------------
5435     -- Perform the lot based job reschedule
5436     -- ------------------------------------------------------------------------
5437     FORALL j in 1..lv_resched_jobs
5438     INSERT INTO msc_wip_job_schedule_interface
5439             (last_update_date,
5440             last_updated_by,
5441             cfm_routing_flag,
5442             last_update_login,
5443             creation_date,
5444             created_by,
5445             group_id,
5446             source_code,
5447             organization_id,
5448             organization_type,
5449             status_type,
5450             load_type,
5451             first_unit_start_date,
5452             last_unit_completion_date,
5453             bom_revision_date,
5454             routing_revision_date,
5455             job_name,
5456             firm_planned_flag,
5457             start_quantity,   /* bug 1229891: net_quantity */
5458             net_quantity,
5459             wip_entity_id,
5460             demand_class,
5461             project_id,
5462             task_id,
5463 	    schedule_group_id,
5464 	    build_sequence,
5465             line_id,
5466             alternate_bom_designator,
5467 	    alternate_routing_designator,
5468 	    end_item_unit_number,
5469             process_phase,
5470 	    process_status,
5471             BILL_RTG_EXPLOSION_FLAG,
5472             HEADER_ID,
5473             uom_code, --Outbound Changes for XML
5474             SR_INSTANCE_ID,
5475             PRIMARY_ITEM_ID,
5476             source_line_id --Outbound Changes for XML
5477 	   -- dsr: added 2 new columns
5478 	   , schedule_priority
5479 	   , requested_completion_date
5480 	  )
5481     SELECT  SYSDATE,
5482             arg_user_id,
5483             s.cfm_routing_flag,
5484             s.last_update_login,
5485             SYSDATE,
5486             arg_user_id,
5487             arg_wip_group_id,
5488             'MSC',
5489             msi.organization_id,
5490             tp.organization_type,
5491             DECODE(NVL(s.implement_status_code, s.wip_status_code),
5492                    JOB_CANCELLED,JOB_CANCELLED,NULL),
5493             6,
5494             decode(lv_details_enabled(j),1,new_wip_start_date,null),
5495             decode(v_unconstrained_plan,0,s.implement_date,1,trunc(s.implement_date)+v_time_stamp),
5496             NULL,
5497             NULL,
5498             s.implement_job_name,
5499             s.implement_firm,
5500             DECODE( tp.organization_type,
5501                     1, DECODE(s.new_order_quantity,
5502                               s.implement_quantity, TO_NUMBER(NULL),
5503                         ((s.new_order_quantity + NVL(s.qty_completed, 0) +
5504                           NVL(s.qty_scrapped, 0)) -
5505                          (s.new_order_quantity - s.implement_quantity))),
5506                     NULL),
5507             DECODE( tp.organization_type,
5508                     2, DECODE(s.new_order_quantity,
5509                               s.implement_quantity, TO_NUMBER(NULL),
5510                         ((s.new_order_quantity + NVL(s.qty_completed, 0) +
5511                           NVL(s.qty_scrapped, 0)) -
5512                          (s.new_order_quantity - s.implement_quantity))),
5513                     s.implement_quantity),
5514             s.disposition_id,
5515             s.implement_demand_class,
5516             s.implement_project_id,
5517             s.implement_task_id,
5518 	    s.implement_schedule_group_id,
5519             s.implement_build_sequence,
5520             s.implement_line_id,
5521        	    s.implement_alternate_bom,
5522 	    s.implement_alternate_routing,
5523 	    s.implement_unit_number,
5524             2,
5525 	    1,
5526             'Y',
5527             s.transaction_id,
5528             nvl(s.implement_uom_code,msi.uom_code),
5529             s.sr_instance_id,
5530             msi.sr_inventory_item_id,
5531             s.transaction_id --Outbound Changes for XML
5532             -- dsr: added 2 new columns
5533 	   , s.schedule_priority
5534 	   , s.requested_completion_date
5535     FROM    msc_trading_partners tp,
5536             msc_parameters param,
5537             msc_system_items msi,
5538             msc_supplies     s,
5539             msc_plan_organizations_v orgs
5540     WHERE   tp.sr_tp_id= msi.organization_id
5541     AND     tp.sr_instance_id= msi.sr_instance_id
5542     AND     tp.partner_type=3
5543     AND     param.organization_id = msi.organization_id
5544     AND    param.sr_instance_id = msi.sr_instance_id
5545     AND    msi.inventory_item_id = s.inventory_item_id
5546     AND    msi.plan_id = s.plan_id
5547     AND    msi.organization_id = s.organization_id
5548     AND    msi.sr_instance_id  = s.sr_instance_id
5549     AND    s.release_errors is NULL
5550     AND    s.organization_id = orgs.planned_organization
5551     AND    s.sr_instance_id = orgs.sr_instance_id
5552     AND    s.plan_id = orgs.plan_id
5553     AND    orgs.organization_id = arg_owning_org_id
5554     AND    orgs.owning_sr_instance = arg_owning_instance
5555     AND    orgs.plan_id = arg_plan_id
5556     AND    orgs.planned_organization = decode(arg_log_org_id,
5557                                          arg_owning_org_id, orgs.planned_organization,
5558                                           arg_log_org_id)
5559     AND    orgs.sr_instance_id = arg_org_instance
5560     AND     ((arg_mode is null and s.load_type = 6 AND
5561               s.last_updated_by = decode(v_msc_released_only_by_user,1,v_user_id,s.last_updated_by)) or
5562                 (arg_mode = 'WF' and s.transaction_id = arg_transaction_id))
5563     AND    nvl(s.cfm_routing_flag,0) = 3
5564     AND    s.transaction_id = lv_transaction_id(j)
5565     AND    s.sr_instance_id  = lv_instance_id(j)
5566     AND    s.plan_id = lv_plan_id(j);
5567 
5568 
5569 
5570     -- ------------------------------------------------------------------------
5571     -- Perform the lot-based job mass load for the details
5572     -- -----------------------------------------------------------------------
5573 
5574     /* lot-based job details are released only when the source profile WSM: Create Lot Based Job Routing is Yes
5575     and org planning parameter is primary */
5576 
5577 
5578     /* OPERATION NETWORKS */
5579 
5580 
5581     FORALL j IN 1..lv_resched_jobs
5582     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
5583     (last_update_date,
5584      last_updated_by,
5585      last_update_login,
5586      creation_date,
5587      created_by,
5588      group_id,
5589      parent_header_id,
5590      SUBSTITUTION_TYPE,
5591      LOAD_TYPE,
5592      process_phase,
5593      process_status,
5594      job_op_seq_num,
5595      operation_seq_num,
5596      next_routing_op_seq_num,
5597      cfm_routing_flag,
5598      SR_INSTANCE_ID)
5599   (SELECT  SYSDATE,
5600             arg_user_id,
5601             s.last_update_login,
5602             SYSDATE,
5603             arg_user_id,
5604             arg_wip_group_id,
5605             s.transaction_id,
5606             4,
5607             5,
5608             1,
5609             1,
5610             decode(s.JUMP_OP_SEQ_NUM, null, decode(nwk.from_op_seq_num, s.OPERATION_SEQ_NUM, s.JOB_OP_SEQ_NUM, null), nwk.from_op_seq_num, s.JOB_OP_SEQ_NUM,null),
5611             decode(nwk.from_op_seq_num, 50000 ,null,nwk.from_op_seq_num),
5612             nwk.to_op_seq_num,
5613             3,
5614             s.sr_instance_id
5615    From msc_supplies s,
5616    msc_job_operation_networks nwk,
5617    msc_apps_instances ins,
5618    msc_parameters param
5619    Where    nwk.plan_id = s.plan_id
5620     AND     nwk.sr_instance_id = s.sr_instance_id
5621     AND     nwk.transaction_id = s.transaction_id
5622     AND     nwk.recommended = 'Y'
5623     AND     s.transaction_id= lv_transaction_id(j)
5624     AND     s.sr_instance_id= lv_instance_id(j)
5625     AND     s.plan_id= arg_plan_id
5626     AND     lv_details_enabled(j)= 1
5627     AND     lv_agg_details(j) = 1
5628     AND     ins.instance_id = lv_instance_id(j)
5629     AND     nvl(ins.lbj_details,2) = 1
5630     AND     param.organization_id = s.organization_id
5631     AND     param.sr_instance_id = s.sr_instance_id
5632     AND     param.network_scheduling_method = 1);
5633 
5634 
5635     /* Operations */
5636 
5637 FORALL j IN 1..lv_resched_jobs
5638     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
5639     (last_update_date,
5640      last_updated_by,
5641      last_update_login,
5642      creation_date,
5643      created_by,
5644      group_id,
5645      parent_header_id,
5646      SUBSTITUTION_TYPE,
5647      LOAD_TYPE,
5648      process_phase,
5649      process_status,
5650      job_op_seq_num,
5651      OPERATION_SEQ_NUM,
5652      first_unit_start_date,
5653      last_unit_completion_date,
5654      cfm_routing_flag,
5655      SR_INSTANCE_ID,
5656      scheduled_quantity)
5657   (SELECT  SYSDATE,
5658             arg_user_id,
5659             s.last_update_login,
5660             SYSDATE,
5661             arg_user_id,
5662             arg_wip_group_id,
5663             s.transaction_id,
5664             4,
5665             3,
5666             1,
5667             1,
5668             decode(s.JUMP_OP_SEQ_NUM, null, decode(res.OPERATION_SEQ_NUM, s.OPERATION_SEQ_NUM, s.JOB_OP_SEQ_NUM, null), res.OPERATION_SEQ_NUM, s.JOB_OP_SEQ_NUM,null),
5669             decode(res.operation_seq_num, 50000,null,res.operation_seq_num),
5670             min(res.START_DATE),
5671             max(res.END_DATE),
5672             3,
5673             s.sr_instance_id,
5674             max(nvl(res.CUMMULATIVE_QUANTITY,0))
5675    From msc_supplies s,
5676    msc_resource_requirements res,
5677    msc_apps_instances ins,
5678    msc_parameters param
5679    Where    res.plan_id = s.plan_id
5680     AND     res.sr_instance_id = s.sr_instance_id
5681     AND     s.transaction_id = res.supply_id
5682     AND     res.parent_id = 2
5683     -- AND     res.resource_id <> -1   --Bug#3432607
5684     -- AND     res.department_id <> -1
5685     AND     s.transaction_id= lv_transaction_id(j)
5686     AND     s.sr_instance_id= lv_instance_id(j)
5687     AND     s.plan_id= arg_plan_id
5688     AND     lv_details_enabled(j)= 1
5689     AND     lv_agg_details(j) = 1
5690     AND     ins.instance_id = lv_instance_id(j)
5691     AND     nvl(ins.lbj_details,2) = 1
5692     AND     param.organization_id = s.organization_id
5693     AND     param.sr_instance_id = s.sr_instance_id
5694     AND     param.network_scheduling_method = 1
5695     GROUP BY
5696             s.last_update_login,
5697             s.transaction_id,
5698             res.OPERATION_SEQ_NUM,
5699             s.sr_instance_id,
5700            -- res.CUMMULATIVE_QUANTITY,
5701             s.OPERATION_SEQ_NUM,
5702             s.JUMP_OP_SEQ_NUM,
5703             s.JOB_OP_SEQ_NUM);
5704 
5705     /* Resources */
5706 FORALL j IN 1..lv_resched_jobs
5707     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
5708     (last_update_date,
5709      last_updated_by,
5710      last_update_login,
5711      creation_date,
5712      created_by,
5713      group_id,
5714      parent_header_id,
5715      SUBSTITUTION_TYPE,
5716      LOAD_TYPE,
5717      process_phase,
5718      process_status,
5719      job_op_seq_num,
5720      operation_seq_num,
5721      -- dsr resource_seq_num,
5722      resource_id_new,
5723      start_date,
5724      completion_date,
5725      alternate_num,
5726      cfm_routing_flag,
5727      SR_INSTANCE_ID
5728     -- dsr: add following columns
5729     , firm_flag
5730     , setup_id
5731     , group_sequence_id
5732     , group_sequence_number
5733     , batch_id
5734    , maximum_assigned_units
5735    , parent_seq_num
5736    , resource_seq_num
5737    , schedule_seq_num
5738    , assigned_units
5739    , usage_rate_or_amount
5740    , scheduled_flag
5741 )
5742   (SELECT  SYSDATE,
5743             arg_user_id,
5744             s.last_update_login,
5745             SYSDATE,
5746             arg_user_id,
5747             arg_wip_group_id,
5748             s.transaction_id,
5749             decode(res.parent_seq_num, null,4,2),
5750             1,
5751             1,
5752             1,
5753             decode(s.JUMP_OP_SEQ_NUM, null, decode(res.OPERATION_SEQ_NUM, s.OPERATION_SEQ_NUM, s.JOB_OP_SEQ_NUM, null), res.OPERATION_SEQ_NUM, s.JOB_OP_SEQ_NUM,null),
5754             decode(res.operation_seq_num, 50000,null,res.operation_seq_num),
5755             -- res.resource_seq_num,
5756             res.resource_id,
5757             nvl(res.firm_start_date,res.START_DATE),
5758             nvl(res.firm_end_date,res.END_DATE),
5759             nvl(res.alternate_num,0),
5760             3,
5761             s.sr_instance_id
5762             -- dsr: add following columns
5763             , res.firm_flag
5764 	    , res.setup_id
5765 	    , res.group_sequence_id
5766 	    , res.group_sequence_number
5767             , res.batch_number
5768 	    , res.maximum_assigned_units
5769 	    , res.parent_seq_num
5770 	    , res.orig_resource_seq_num
5771 	    , res.resource_seq_num
5772 	    , res.assigned_units
5773 	    , decode(res.parent_seq_num, null, (res.RESOURCE_HOURS/decode(res.basis_type,2,1,
5774                     nvl(res.cummulative_quantity,
5775                              decode(msi.rounding_control_type,1,
5776                ROUND(s.new_order_quantity/nvl(res.REVERSE_CUMULATIVE_YIELD,1) ,6),
5777                     (s.new_order_quantity/nvl(res.REVERSE_CUMULATIVE_YIELD,1))
5778                             ) ) ))* decode(mdr.efficiency,NULL,100,0,100,mdr.efficiency)/100 * decode(mdr.utilization,NULL,100,0,100,mdr.utilization)/100, res.RESOURCE_HOURS)
5779             ,  decode(nvl(res.schedule_flag,1),-23453,1,1,1,res.schedule_flag)
5780    From msc_supplies s,
5781    msc_resource_requirements res,
5782    msc_apps_instances ins,
5783    msc_parameters param,
5784    msc_department_resources mdr,
5785    msc_system_items msi
5786    Where    res.plan_id = s.plan_id
5787     AND     res.sr_instance_id = s.sr_instance_id
5788     AND     s.transaction_id = res.supply_id
5789     AND     res.parent_id = 2
5790     AND     res.resource_id <> -1
5791     AND     res.department_id <> -1
5792     AND     s.transaction_id= lv_transaction_id(j)
5793     AND     s.sr_instance_id= lv_instance_id(j)
5794     AND     s.plan_id= arg_plan_id
5795     AND     lv_details_enabled(j)= 1
5796     AND     lv_agg_details(j) = 1
5797     AND     ins.instance_id = lv_instance_id(j)
5798     AND     nvl(ins.lbj_details,2) = 1
5799     AND     param.organization_id = s.organization_id
5800     AND     param.sr_instance_id = s.sr_instance_id
5801     AND     res.plan_id = mdr.plan_id
5802     AND     res.organization_id =mdr.organization_id
5803     AND     res.sr_instance_id = mdr.sr_instance_id
5804     AND     res.resource_id = mdr.resource_id
5805     AND     res.department_id=mdr.department_id
5806     AND     msi.inventory_item_id = s.inventory_item_id
5807     AND     msi.plan_id = s.plan_id
5808     AND     msi.organization_id = s.organization_id
5809     AND     msi.sr_instance_id = s.sr_instance_id
5810     AND     param.network_scheduling_method = 1);
5811 
5812     /*Components*/
5813 
5814     FORALL j IN 1..lv_resched_jobs
5815     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
5816     (last_update_date,
5817      last_updated_by,
5818      last_update_login,
5819      creation_date,
5820      created_by,
5821      group_id,
5822      parent_header_id,
5823      SUBSTITUTION_TYPE,
5824      LOAD_TYPE,
5825      process_phase,
5826      process_status,
5827      job_op_seq_num,
5828      operation_seq_num,
5829      inventory_item_id_new,
5830      primary_component_id,
5831      source_phantom_id,
5832      component_seq_id,
5833      mrp_net_flag,
5834      date_required,
5835      mps_date_required,
5836      basis_type,
5837      quantity_per_assembly,
5838      required_quantity,
5839      mps_required_quantity,
5840      cfm_routing_flag,
5841      SR_INSTANCE_ID)
5842   (SELECT  SYSDATE,
5843             arg_user_id,
5844             s.last_update_login,
5845             SYSDATE,
5846             arg_user_id,
5847             arg_wip_group_id,
5848             s.transaction_id,
5849             4,
5850             2,
5851             1,
5852             1,
5853             decode(s.JUMP_OP_SEQ_NUM, null, decode(md.op_seq_num, s.OPERATION_SEQ_NUM, s.JOB_OP_SEQ_NUM, null), md.op_seq_num, s.JOB_OP_SEQ_NUM,null),
5854             decode(md.op_seq_num, 50000,null,md.op_seq_num),
5855             icomp.sr_inventory_item_id,
5856             icomp1.sr_inventory_item_id,
5857             icomp2.sr_inventory_item_id,
5858             md.COMP_SEQ_ID,
5859             1,
5860             md.USING_ASSEMBLY_DEMAND_DATE,
5861             md.USING_ASSEMBLY_DEMAND_DATE,
5862             decode(md.component_scaling_type,1,NULL,md.component_scaling_type),
5863             md.quantity_per_assembly,
5864             md.USING_REQUIREMENT_QUANTITY,
5865             md.USING_REQUIREMENT_QUANTITY,
5866             3,
5867             s.sr_instance_id
5868    From msc_supplies s,
5869    msc_demands md,
5870    msc_system_items icomp,
5871    msc_system_items icomp1,
5872    msc_system_items icomp2,
5873    msc_apps_instances ins,
5874    msc_parameters param
5875    Where   /* not exists (select 'x'
5876                         from msc_exception_details excp
5877                         where excp.plan_id = s.plan_id
5878                         and excp.number1 = s.transaction_id
5879                         and excp.inventory_item_id = s.inventory_item_id
5880                         and excp.organization_id = s.organization_id
5881                         and excp.sr_instance_id = s.sr_instance_id
5882                         and excp.exception_type = 33
5883                         and excp.number2 = md.inventory_item_id)*/  /* not needed as inv_old need not be populated*/
5884     	    icomp.inventory_item_id= md.inventory_item_id
5885     AND     icomp.organization_id= md.organization_id
5886     AND     icomp.sr_instance_id= md.sr_instance_id
5887     AND     icomp.plan_id= md.plan_id
5888     AND     nvl(icomp.wip_supply_type,0) <> 6
5889     AND     icomp1.inventory_item_id= md.primary_component_id
5890     AND     icomp1.organization_id= md.organization_id
5891     AND     icomp1.sr_instance_id= md.sr_instance_id
5892     AND     icomp1.plan_id= md.plan_id
5893     AND     icomp2.inventory_item_id(+)= md.source_phantom_id
5894     AND     icomp2.organization_id(+)= md.organization_id
5895     AND     icomp2.sr_instance_id(+)= md.sr_instance_id
5896     AND     icomp2.plan_id(+)= md.plan_id
5897     AND     md.plan_id = s.plan_id
5898     AND     md.sr_instance_id = s.sr_instance_id
5899     AND     md.disposition_id= s.transaction_id
5900     AND     md.origination_type = 1
5901     AND     s.transaction_id= lv_transaction_id(j)
5902     AND     s.sr_instance_id= lv_instance_id(j)
5903     AND     s.plan_id= arg_plan_id
5904     AND     lv_details_enabled(j)= 1
5905     AND     lv_agg_details(j) = 1
5906     AND     ins.instance_id = lv_instance_id(j)
5907     AND     nvl(ins.lbj_details,2) = 1
5908     AND     param.organization_id = s.organization_id
5909     AND     param.sr_instance_id = s.sr_instance_id
5910     AND     param.network_scheduling_method = 1);
5911 
5912 
5913  /* Resource Usage */
5914  FORALL j IN 1..lv_resched_jobs
5915     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
5916     (last_update_date,
5917      last_updated_by,
5918      last_update_login,
5919      creation_date,
5920      created_by,
5921      group_id,
5922      parent_header_id,
5923      SUBSTITUTION_TYPE,
5924      LOAD_TYPE,
5925      process_phase,
5926      process_status,
5927      job_op_seq_num,
5928      operation_seq_num,
5929      -- dsr resource_seq_num,
5930      resource_id_new,
5931      assigned_units,
5932      alternate_num,
5933      start_date,
5934      completion_date,
5935      cfm_routing_flag,
5936      SR_INSTANCE_ID
5937      , resource_seq_num -- dsr
5938      , schedule_seq_num
5939      , parent_seq_num
5940     )
5941   (SELECT  SYSDATE,
5942             arg_user_id,
5943             s.last_update_login,
5944             SYSDATE,
5945             arg_user_id,
5946             arg_wip_group_id,
5947             s.transaction_id,
5948             4,
5949             4,
5950             1,
5951             1,
5952             decode(s.JUMP_OP_SEQ_NUM, null, decode(res.operation_seq_num, s.OPERATION_SEQ_NUM, s.JOB_OP_SEQ_NUM, null), res.operation_seq_num, s.JOB_OP_SEQ_NUM,null),
5953             decode(res.operation_seq_num, 50000,null,res.operation_seq_num),
5954            --  res.resource_seq_num,
5955             res.resource_id,
5956             res.assigned_units,
5957             nvl(res.alternate_num,0),
5958             nvl(res.firm_start_date,res.START_DATE),
5959             nvl(res.firm_end_date,res.END_DATE),
5960             3,
5961             s.sr_instance_id,
5962              -- dsr
5963 	    res.orig_resource_seq_num,
5964 	    res.resource_seq_num,
5965 	    res.parent_seq_num
5966        From msc_supplies s,
5967            msc_resource_requirements res,
5968            msc_apps_instances ins,
5969            msc_parameters param
5970    Where    res.plan_id = s.plan_id
5971     AND     res.sr_instance_id = s.sr_instance_id
5972     AND     s.transaction_id = res.supply_id
5973     AND     res.parent_id = 1
5974     AND     res.resource_id <> -1
5975     AND     res.department_id <> -1
5976     AND     s.transaction_id= lv_transaction_id(j)
5977     AND     s.sr_instance_id= lv_instance_id(j)
5978     AND     s.plan_id= arg_plan_id
5979     AND     lv_details_enabled(j)= 1
5980     AND     lv_agg_details(j) = 1
5981     AND     ins.instance_id = lv_instance_id(j)
5982     AND     nvl(ins.lbj_details,2) = 1
5983     AND     param.organization_id = s.organization_id
5984     AND     param.sr_instance_id = s.sr_instance_id
5985     AND     param.network_scheduling_method = 1);
5986 
5987     -- dsr begin: Operation Resource Instances
5988 
5989     FORALL j IN 1..lv_resched_jobs
5990     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
5991           ( last_update_date,
5992             last_updated_by,
5993             last_update_login,
5994             creation_date,
5995             created_by,
5996             organization_type,
5997             organization_id,
5998             group_id,
5999             parent_header_id,
6000             job_op_seq_num,
6001             operation_seq_num,
6002        --   resource_seq_num, dsr rawasthi
6003             RESOURCE_INSTANCE_ID,
6004             start_date,
6005             completion_date,
6006             SUBSTITUTION_TYPE,
6007             LOAD_TYPE,
6008             process_phase,
6009             process_status,
6010             SR_INSTANCE_ID,
6011             operation_seq_id,
6012             FIRM_FLAG,
6013             resource_hours,
6014             department_id,
6015 	    SERIAL_NUMBER,
6016             group_sequence_id,
6017             group_sequence_number,
6018             batch_id
6019             , resource_seq_num -- dsr
6020             , schedule_seq_num
6021             , parent_seq_num
6022             , cfm_routing_flag
6023             , resource_id_new
6024             , assigned_units
6025          )
6026     SELECT
6027             SYSDATE,
6028             arg_user_id,
6029             s.last_update_login,
6030             SYSDATE,
6031             arg_user_id,
6032             tp.organization_type,
6033             s.organization_id,
6034             arg_wip_group_id,
6035             s.transaction_id,
6036             decode(s.JUMP_OP_SEQ_NUM, null, decode(resreq.operation_seq_num, s.OPERATION_SEQ_NUM, s.JOB_OP_SEQ_NUM, null), resreq.operation_seq_num, s.JOB_OP_SEQ_NUM,null),
6037             decode(resreq.operation_seq_num, 50000,null,resreq.operation_seq_num),
6038          -- resreq.OPERATION_SEQ_NUM,
6039          -- resreq.RESOURCE_SEQ_NUM,
6040             dep_res_inst.RES_INSTANCE_ID,
6041             nvl(resreq.firm_start_date,res_instreq.START_DATE),
6042             nvl(resreq.firm_end_date,res_instreq.END_DATE),
6043             SUBST_ADD, -- jguo SUBST_CHANGE,
6044             LT_RESOURCE_INSTANCE,
6045             2,
6046             1,
6047             s.sr_instance_id,
6048             resreq.operation_sequence_id,
6049             resreq.firm_flag,
6050             res_instreq.resource_instance_hours,
6051             resreq.department_id,
6052             dep_res_inst.serial_number,
6053             resreq.group_sequence_id,
6054             resreq.group_sequence_number,
6055             res_instreq.batch_number,
6056 	    ---- sbala res_instreq.res_inst_batch_id
6057             resreq.orig_resource_seq_num,
6058 	    resreq.resource_seq_num,
6059 	    resreq.parent_seq_num,
6060 	    3,
6061 	    resreq.resource_id,
6062 	    1
6063       FROM  msc_trading_partners   tp,
6064             msc_resource_requirements resreq,
6065             msc_resource_instance_reqs res_instreq, -- changed from design doc
6066             msc_dept_res_instances dep_res_inst,
6067             msc_supplies            s,
6068             msc_apps_instances ins,
6069             msc_parameters param
6070     WHERE
6071             tp.sr_tp_id=s.organization_id
6072     AND     tp.sr_instance_id= s.sr_instance_id
6073     AND     tp.partner_type=3
6074     AND     resreq.sr_instance_id= s.sr_instance_id
6075     AND     resreq.organization_id= s.organization_id
6076     AND     resreq.supply_id = s.transaction_id
6077     AND     resreq.plan_id   = s.plan_id
6078 --    AND     resreq.transaction_id = res_instreq.res_inst_transaction_id
6079     AND     resreq.sr_instance_id = res_instreq.sr_instance_id
6080     AND     resreq.plan_id = res_instreq.plan_id
6081     AND     resreq.resource_seq_num = res_instreq.resource_seq_num
6082     AND     resreq.operation_seq_num = res_instreq.operation_seq_num
6083     AND     resreq.resource_id = res_instreq.resource_id
6084     AND     resreq.supply_id = res_instreq.supply_id
6085     AND     resreq.parent_id = res_instreq.parent_id  --rawasthi
6086     AND     resreq.start_date = res_instreq.start_date
6087     AND     resreq.parent_id   = 2
6088     AND     resreq.resource_id <> -1
6089     AND     resreq.department_id <> -1
6090 --    AND     res_instreq.res_instance_id = dep_res_inst.dept_resource_inst_id
6091     AND     res_instreq.plan_id = dep_res_inst.plan_id
6092     AND     res_instreq.sr_instance_id = dep_res_inst.sr_instance_id
6093     AND     res_instreq.department_id = dep_res_inst.department_id
6094     AND     res_instreq.resource_id = dep_res_inst.resource_id
6095 /* anuj: serail number and resource_instance id joins */
6096     AND     res_instreq.serial_number = dep_res_inst.serial_number
6097     AND     res_instreq.res_instance_id = dep_res_inst.res_instance_id
6098     AND     s.transaction_id= lv_transaction_id(j)
6099     AND     s.sr_instance_id= lv_instance_id(j)
6100     AND     s.plan_id= arg_plan_id
6101     AND     lv_details_enabled(j)= 1
6102     AND     lv_agg_details(j) = 1
6103     AND     ins.instance_id = lv_instance_id(j)
6104     AND     nvl(ins.lbj_details,2) = 1
6105     AND     param.organization_id = s.organization_id
6106     AND     param.sr_instance_id = s.sr_instance_id
6107     AND     param.network_scheduling_method = 1
6108 	;
6109 
6110 /*  print_debug_info('Resource Instance: rows inserted into msc_wip_job_dtls_interface = '
6111   					|| sql%rowcount); */
6112 
6113 FORALL j IN 1..lv_resched_jobs
6114     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
6115           ( last_update_date,
6116             last_updated_by,
6117             last_update_login,
6118             creation_date,
6119             created_by,
6120             organization_type,
6121             organization_id,
6122             group_id,
6123             parent_header_id,
6124             job_op_seq_num,
6125             operation_seq_num,
6126         --    resource_seq_num,
6127             RESOURCE_INSTANCE_ID,
6128             start_date,
6129             completion_date,
6130             SUBSTITUTION_TYPE,
6131             LOAD_TYPE,
6132             process_phase,
6133             process_status,
6134             SR_INSTANCE_ID,
6135             operation_seq_id,
6136             FIRM_FLAG,
6137             resource_hours,
6138             department_id,
6139             serial_number
6140             , resource_seq_num -- dsr
6141             , schedule_seq_num
6142             , parent_seq_num
6143             , cfm_routing_flag
6144             , resource_id_new
6145             , assigned_units
6146  )
6147     SELECT
6148             SYSDATE,
6149             arg_user_id,
6150             s.last_update_login,
6151             SYSDATE,
6152             arg_user_id,
6153             tp.organization_type,
6154             s.organization_id,
6155             arg_wip_group_id,
6156             s.transaction_id,
6157             decode(s.JUMP_OP_SEQ_NUM, null, decode(resreq.operation_seq_num, s.OPERATION_SEQ_NUM, s.JOB_OP_SEQ_NUM, null), resreq.operation_seq_num, s.JOB_OP_SEQ_NUM,null),
6158             decode(resreq.operation_seq_num, 50000,null,resreq.operation_seq_num),
6159          -- resreq.OPERATION_SEQ_NUM,
6160          -- resreq.RESOURCE_SEQ_NUM,
6161             dep_res_inst.RES_INSTANCE_ID,
6162             nvl(resreq.firm_start_date,res_instreq.START_DATE),
6163             nvl(resreq.firm_end_date,res_instreq.END_DATE),
6164             SUBST_ADD, -- jguo SUBST_CHANGE,
6165             LT_RESOURCE_INST_USAGE,
6166             2,
6167             1,
6168             s.sr_instance_id,
6169             resreq.operation_sequence_id,
6170             resreq.firm_flag,
6171             res_instreq.resource_instance_hours,
6172             resreq.department_id,
6173             dep_res_inst.serial_number,
6174             resreq.orig_resource_seq_num
6175 	    , resreq.resource_seq_num
6176 	    , resreq.parent_seq_num
6177 	    , 3
6178 	    , resreq.resource_id
6179 	    , 1
6180     FROM
6181             msc_trading_partners   tp,
6182             msc_resource_requirements resreq,
6183             msc_resource_instance_reqs res_instreq,
6184             msc_dept_res_instances dep_res_inst,
6185             msc_supplies            s,
6186             msc_apps_instances ins,
6187             msc_parameters param
6188     WHERE
6189             tp.sr_tp_id=s.organization_id
6190       AND   tp.sr_instance_id= s.sr_instance_id
6191       AND   tp.partner_type=3
6192       AND   resreq.sr_instance_id= s.sr_instance_id
6193       AND   resreq.organization_id= s.organization_id
6194       AND   resreq.supply_id = s.transaction_id
6195       AND   resreq.plan_id   = s.plan_id
6196 --    AND   resreq.transaction_id = res_instreq.res_inst_transaction_id
6197       AND   resreq.sr_instance_id = res_instreq.sr_instance_id
6198       AND   resreq.plan_id = res_instreq.plan_id
6199       AND   resreq.resource_seq_num = res_instreq.resource_seq_num
6200       AND   resreq.operation_seq_num = res_instreq.operation_seq_num
6201       AND   resreq.resource_id = res_instreq.resource_id
6202       AND   resreq.supply_id = res_instreq.supply_id
6203       AND   resreq.parent_id = res_instreq.parent_id  --rawasthi
6204       AND   resreq.start_date = res_instreq.start_date
6205       AND   resreq.parent_id   = 1
6206       AND   resreq.resource_id <> -1
6207       AND   resreq.department_id <> -1
6208 --    AND     res_instreq.res_instance_id = dep_res_inst.dept_resource_inst_id
6209       AND   res_instreq.department_id = dep_res_inst.department_id
6210       AND   res_instreq.resource_id = dep_res_inst.resource_id
6211       AND   res_instreq.plan_id = dep_res_inst.plan_id
6212       AND   res_instreq.sr_instance_id = dep_res_inst.sr_instance_id
6213 /* anuj: serail number and resource_instance id joins */
6214       AND   res_instreq.serial_number = dep_res_inst.serial_number
6215       AND   res_instreq.res_instance_id = dep_res_inst.res_instance_id
6216       AND   s.transaction_id= lv_transaction_id(j)
6217       AND   s.sr_instance_id= lv_instance_id(j)
6218       AND   s.plan_id= arg_plan_id
6219       AND   lv_details_enabled(j)= 1
6220       AND   lv_agg_details(j) = 1
6221       AND   ins.instance_id = lv_instance_id(j)
6222       AND   nvl(ins.lbj_details,2) = 1
6223       AND   param.organization_id = s.organization_id
6224       AND   param.sr_instance_id = s.sr_instance_id
6225       AND   param.network_scheduling_method = 1
6226 	;
6227 
6228 /*  print_debug_info('Resource Instance Usage: rows inserted into msc_wip_job_dtls_interface = '
6229   					|| sql%rowcount); */
6230 
6231 
6232     FORALL j IN 1..lv_resched_jobs
6233         update msc_supplies
6234            set releasable = RELEASE_ATTEMPTED
6235                ,batch_id   = nvl(batch_id,g_batch_id)
6236          where plan_id = arg_plan_id
6237            and transaction_id= lv_transaction_id(j);
6238 
6239 	-- dsr end
6240     RETURN lv_resched_jobs;
6241 
6242 END reschedule_osfm_lot_jobs;
6243 
6244 
6245 FUNCTION load_wip_discrete_jobs
6246 ( arg_plan_id			IN      NUMBER
6247 , arg_log_org_id 		IN 	NUMBER
6248 , arg_org_instance              IN      NUMBER
6249 , arg_owning_org_id 		IN 	NUMBER
6250 , arg_owning_instance           IN      NUMBER
6251 , arg_user_id 			IN 	NUMBER
6252 , arg_wip_group_id              IN      NUMBER
6253 , arg_mode                      IN      VARCHAR2
6254 , arg_transaction_id            IN      NUMBER,
6255   l_apps_ver                    IN      VARCHAR2
6256 )RETURN NUMBER
6257 IS
6258    lv_loaded_jobs NUMBER;
6259 
6260    TYPE NumTab  IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
6261  --TYPE RIDTab  IS TABLE OF ROWID  INDEX BY BINARY_INTEGER;
6262 
6263    lv_transaction_id          NumTab;
6264    lv_instance_id             NumTab;
6265    lv_details_enabled         NumTab;
6266    lv_Agg_details             NumTab;
6267    Lv_org_id                  Numtab;
6268    lv_plan_id                 NumTab;
6269    lv_job_count               NUMBER;
6270    lv_release_details         NUMBER;
6271    lv_inflate_wip             NUMBER;
6272    lv_round_primary_item      NumTab;
6273 
6274 -- BUG 9085180
6275 
6276    lv_wip_start_qty_in         NumTab;
6277    lv_wip_start_qty_out        NumTab;
6278    lv_new_order_qty            NumTab;
6279    lv_implement_qty            NumTab;
6280    lv_qty_scrapped             NumTab;
6281    lv_qty_completed            NumTab;
6282    lv_net_qty                  NumTab;
6283 
6284    orig_net_qty                NUMBER;
6285 
6286 
6287 CURSOR c_release_dtls(p_transaction_id number,
6288                       p_plan_id  number) IS
6289 SELECT COUNT(1) FROM (
6290 SELECT count(1)
6291 FROM msc_supplies
6292 WHERE implement_alternate_bom is not null
6293   and transaction_id = p_transaction_id
6294   AND plan_id = p_plan_id
6295   and rownum < 2
6296 UNION
6297 SELECT count(1)
6298 from msc_exception_details excp
6299 where excp.plan_id = p_plan_id
6300       and number1 = p_transaction_id
6301       and excp.exception_type = 33
6302       and rownum < 2);
6303 
6304 
6305 CURSOR c_plan_type(p_plan_id number) IS
6306 select plan_type
6307 from msc_plans a
6308 where
6309 plan_id = p_plan_id;
6310 
6311 l_sub_comp_count NUMBER;
6312 l_count   NUMBER := 0;
6313 l_plan_type  number := 0;
6314 
6315 BEGIN
6316 
6317    SELECT decode(nvl(FND_PROFILE.value('MSC_RELEASE_DTLS_REVDATE'),'Y'),'N',2,1),
6318    DECODE(NVL(fnd_profile.value('MSC_INFLATE_WIP') ,'N'), 'N',2 ,1)
6319    INTO lv_release_details,lv_inflate_wip
6320    FROM dual;
6321 
6322   /* we release the discrete job, only if it doesn't use aggregate resources */
6323   /* bug 1252659 fix, replace rowid by
6324          (transaction_id,sr_instance_id,plan_id) */
6325 
6326   /* Details will NOT be released for
6327      a. Unconstrained Plan
6328      b. if the new_wip_start_date is null
6329      c. if the implement quantity or date is different then the planned quantity date
6330      d. if the revision date is different then the new_wip_start_date
6331         and the profile option setting : MSC_RELEASE_DTLS_REVDATE  = 'N'
6332      e. Non - Daily Bucketed Plans
6333      f.bug 4655420-Alternate BOM/Routing is changed during release.
6334   */
6335   /* Ignore the profile option
6336       MSC: Release WIP Dtls if Order Date different then BOM Revision Date.
6337      in case of RP plan
6338   */
6339    if (v_rp_plan = 1 ) then
6340      lv_release_details := 1;
6341    end if;
6342 
6343     -- dsting get plan type
6344     OPEN c_plan_type(arg_plan_id);
6345     FETCH c_plan_type INTO l_plan_type;
6346     CLOSE c_plan_type;
6347 
6348    l_sql_stmt := ' SELECT s.transaction_id,
6349           s.sr_instance_id,
6350            Decode(' ||l_plan_type ||', 5, -- dsting for drp look at alternate bom/subst exception to determine whether or not to release dtls
6351                   Decode(implement_alternate_bom, NULL,
6352                          (SELECT Decode(COUNT(1), 1, 1, 2)
6353                             FROM msc_exception_details e
6354                            WHERE e.plan_id = :arg_plan_id
6355                              AND e.number1 = s.transaction_id
6356                              AND e.exception_type = 33), 1),
6357                   decode( mp.daily_material_constraints+ mp.daily_resource_constraints+
6358                   mp.weekly_material_constraints+ mp.weekly_resource_constraints+
6359                   mp.period_material_constraints+ mp.period_resource_constraints,12,2,
6360                decode(mpb.bucket_type,1,
6361                  Decode(greatest(abs(s.implement_quantity -
6362                                            s.new_order_quantity),
6363                                        0.000001),
6364                               0.000001,
6365                      DECODE( s.implement_date, s.new_schedule_date,
6366                      	DECODE(NVL(s.implement_alternate_bom, ''-23453''),
6367                      			 NVL(s.alternate_bom_designator, ''-23453''),
6368                					 DECODE(NVL(s.implement_alternate_routing, ''-23453''),
6369                       			 NVL(s.alternate_routing_designator, ''-23453''),
6370                         DECODE(trunc(msc_calendar.date_offset
6371                               (s.organization_id,
6372                                s.sr_instance_id,
6373                                1, --daily bucket
6374                                s.need_by_date ,
6375                                ceil(nvl(msi.fixed_lead_time,0) +
6376                  nvl(msi.variable_lead_time,0) * s.implement_quantity)*-1 )),
6377                      trunc(s.new_wip_start_date),1,
6378                                    decode(' || lv_release_details || ',2,2,1)),
6379                            2),
6380                          2),
6381                        2),
6382                      2),
6383                  2))),
6384          s.organization_id,
6385          s.plan_id,
6386          decode( ' || l_apps_ver ||' ,
6387                  6,msi.rounding_control_type,
6388                  5,msi.rounding_control_type,
6389                  4,msi.rounding_control_type,
6390                  3,msi.rounding_control_type,
6391                  2),  -- BUG 13624336 (FP of BUG 13500698)
6392          NVL(s.wip_start_quantity, 0),
6393          NVL(s.new_order_quantity, 0),
6394          NVL(s.implement_quantity, 0)
6395 --         NVL(s.qty_scrapped, 0),
6396 --         NVL(s.qty_completed, 0)
6397      FROM msc_supplies s,
6398           msc_plan_organizations_v orgs,
6399           msc_plan_buckets mpb,
6400           msc_system_items msi,
6401           msc_plans mp
6402     WHERE mp.plan_id = :arg_plan_id
6403     AND   s.release_errors is NULL
6404     AND   s.implement_quantity > 0
6405     AND   s.organization_id = orgs.planned_organization
6406     AND   s.sr_instance_id = orgs.sr_instance_id
6407     AND   s.plan_id = orgs.plan_id
6408     AND   msi.inventory_item_id = s.inventory_item_id
6409     AND   msi.plan_id = s.plan_id
6410     AND   msi.organization_id = s.organization_id
6411     AND   msi.sr_instance_id = s.sr_instance_id
6412     AND   orgs.plan_id = mp.plan_id
6413     AND   orgs.organization_id = :arg_owning_org_id
6414     AND   orgs.owning_sr_instance = :arg_owning_instance
6415     AND   orgs.sr_instance_id = :arg_org_instance
6416     and   s.plan_id = mpb.plan_id
6417     and  (nvl(s.implement_date,s.new_schedule_date) between mpb.bkt_start_date and mpb.bkt_end_date)
6418     and   s.new_wip_start_date IS NOT NULL
6419     and (s.releasable = ' || RELEASABLE || ' or s.releasable is null )';
6420 
6421     IF  v_batch_id_populated = 2 THEN
6422         l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id is NULL ';
6423     ELSE
6424         l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id = ' || g_batch_id || ' ' ;
6425     END IF;
6426 
6427     IF arg_log_org_id <> arg_owning_org_id THEN
6428             l_sql_stmt :=  l_sql_stmt ||'      AND     orgs.planned_organization = ' || arg_log_org_id || ' ' ;
6429     END IF;
6430 
6431     IF arg_mode IS NULL THEN
6432         l_sql_stmt :=  l_sql_stmt ||'  AND s.load_type = ' || WIP_DIS_MASS_LOAD || ' ';
6433         IF v_msc_released_only_by_user = 1 THEN
6434             l_sql_stmt :=  l_sql_stmt || ' AND s.last_updated_by = ' || v_user_id || ' ' ;
6435         END IF;
6436     ELSIF arg_mode = 'WF_BATCH' THEN
6437         l_sql_stmt :=  l_sql_stmt ||'  AND s.load_type = ' || WIP_DIS_MASS_LOAD || ' ';
6438     ELSIF arg_mode = 'WF' THEN
6439         l_sql_stmt :=  l_sql_stmt ||'      AND s.transaction_id = ' || arg_transaction_id  || '  ' ;
6440     END IF;
6441 
6442 l_sql_stmt :=  l_sql_stmt ||'
6443 UNION
6444   SELECT s.transaction_id,
6445           s.sr_instance_id,
6446           2 /* Details not enabled for Manual Planned orders */,
6447           s.organization_id,
6448           s.plan_id,
6449           2,  /* setting rounding control to 2 ,since details are not released and this flag is used in details*/
6450          NVL(s.wip_start_quantity, 0),
6451          NVL(s.new_order_quantity, 0),
6452          NVL(s.implement_quantity, 0)
6453 --         NVL(s.qty_scrapped, 0),
6454 --         NVL(s.qty_completed, 0)
6455      FROM msc_supplies s,
6456           msc_plan_organizations_v orgs
6457     WHERE s.release_errors is NULL
6458     AND   s.implement_quantity > 0
6459     AND   s.organization_id = orgs.planned_organization
6460     AND   s.sr_instance_id = orgs.sr_instance_id
6461     AND   s.plan_id = :arg_plan_id
6462     AND   orgs.plan_id = :arg_plan_id
6463     AND   orgs.organization_id = :arg_owning_org_id
6464     AND   orgs.owning_sr_instance = :arg_owning_instance
6465     AND   orgs.sr_instance_id = :arg_org_instance
6466     and   s.new_wip_start_date IS NULL
6467     and (s.releasable = ' || RELEASABLE || ' or s.releasable is null ) ';
6468 
6469     IF  v_batch_id_populated = 2 THEN
6470         l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id is NULL ';
6471     ELSE
6472         l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id = ' || g_batch_id || ' ' ;
6473     END IF;
6474 
6475     IF arg_log_org_id <> arg_owning_org_id THEN
6476             l_sql_stmt :=  l_sql_stmt ||'      AND     orgs.planned_organization = ' || arg_log_org_id || ' ' ;
6477     END IF;
6478 
6479 
6480     IF arg_mode IS NULL THEN
6481         l_sql_stmt :=  l_sql_stmt ||'  AND s.load_type = ' || WIP_DIS_MASS_LOAD || ' ';
6482         IF v_msc_released_only_by_user = 1 THEN
6483             l_sql_stmt :=  l_sql_stmt || ' AND s.last_updated_by = ' || v_user_id || ' ' ;
6484         END IF;
6485     ELSIF arg_mode = 'WF_BATCH' THEN
6486         l_sql_stmt :=  l_sql_stmt ||'  AND s.load_type = ' || WIP_DIS_MASS_LOAD || ' ';
6487     ELSIF arg_mode = 'WF' THEN
6488         l_sql_stmt :=  l_sql_stmt ||'      AND s.transaction_id = ' || arg_transaction_id  || '  ' ;
6489     END IF;
6490 
6491     lv_job_count:= 0;
6492     IF arg_mode IS NULL OR arg_mode = 'WF_BATCH' OR arg_mode = 'WF' THEN
6493         EXECUTE IMMEDIATE l_sql_stmt
6494         BULK COLLECT
6495                                              INTO lv_transaction_id,
6496                                                   lv_instance_id,
6497                                                   lv_details_enabled,
6498                                                   lv_org_id,
6499                                                   lv_plan_id,
6500                                                   lv_round_primary_item,
6501                                                   lv_wip_start_qty_in,
6502                                                   lv_new_order_qty,
6503                                                   lv_implement_qty
6504 --                                                  lv_qty_scrapped,
6505 --                                                  lv_qty_completed
6506                                     USING  arg_plan_id
6507                                             ,arg_plan_id
6508                                             ,arg_owning_org_id
6509                                             ,arg_owning_instance
6510                                             ,arg_org_instance
6511                                             ,arg_plan_id
6512                                             ,arg_plan_id
6513                                             ,arg_owning_org_id
6514                                             ,arg_owning_instance
6515                                             ,arg_org_instance  ;
6516         lv_job_count:= SQL%ROWCOUNT;
6517     END IF;
6518 
6519     -- -----------------------------------------------------------------------
6520     -- Perform the wip discrete job mass load
6521     -- -----------------------------------------------------------------------
6522        /* Due to we only give PLANNED components, BILL_RTG_EXPLOSION_FLAG
6523           is set to 'Y'.  */
6524 
6525     --DBMS_OUTPUT.PUT_LINE('LOAD_JOB');
6526 
6527     FOR k in 1..lv_job_count
6528        Loop
6529                 Begin
6530                   SELECT 2
6531                   Into lv_agg_details(k)
6532                   FROM msc_department_resources deptres,
6533                        msc_resource_requirements resreq
6534                  WHERE resreq.sr_instance_id= lv_instance_id(k)
6535                    AND resreq.supply_id = lv_transaction_id(k)
6536                    AND resreq.organization_id= lv_org_id(k)
6537                    AND resreq.plan_id   = lv_plan_id(k)
6538                    AND resreq.parent_id   = 2
6539                    AND deptres.plan_id  = resreq.plan_id
6540                    AND deptres.sr_instance_id= resreq.sr_instance_id
6541                    AND deptres.resource_id= resreq.resource_id
6542                    AND deptres.department_id= resreq.department_id
6543                    AND deptres.organization_id= resreq.organization_id
6544                    AND deptres.aggregate_resource_flag= 1
6545                    AND rownum=1;
6546                   Exception
6547                   When no_data_found
6548                   then
6549                   lv_agg_details(k) := 1;
6550                   End;
6551                   -- BUG 9085180
6552                   -- Compute the lv_wip_start_qty_out and lv_net_qty.
6553 
6554                   IF ((lv_details_enabled(k) = 1) OR
6555                       (abs(lv_implement_qty(k) - lv_new_order_qty(k))
6556                                                       <= 0.000001)) THEN
6557 
6558                      -- There is no change in date/quantity at the time of release
6559                      -- and we are populating the details, so we will pass the
6560                      -- WIP start and net quantity.
6561                      -- In this case, lv_new_order_qty = lv_implement_qty
6562 
6563                      lv_wip_start_qty_out(k) := NVL(lv_wip_start_qty_in(k),
6564                                                     lv_new_order_qty (k));
6565                      lv_net_qty(k) := lv_new_order_qty (k) ;
6566 
6567                   ELSE
6568 
6569                      -- The user changes the implement quantity at the time of release.
6570                      -- We might need a factor (reverse cum yield) here to
6571                      -- inflate the start quantity.
6572 
6573                      lv_net_qty(k) := lv_implement_qty (k) ;
6574 
6575                      orig_net_qty := lv_new_order_qty (k) ;
6576                      IF (orig_net_qty <= 0.000001) THEN
6577                          lv_wip_start_qty_out(k) := lv_implement_qty (k) ;
6578                      ELSE
6579                          lv_wip_start_qty_out(k) := lv_net_qty(k) *
6580                                       (lv_wip_start_qty_in(k) / orig_net_qty);
6581                      END IF;  --9952844
6582                   END IF;
6583 
6584 
6585        End Loop;
6586 
6587 
6588     FORALL j IN 1..lv_job_count
6589     INSERT INTO msc_wip_job_schedule_interface
6590             (last_update_date,
6591             last_updated_by,
6592             last_update_login,
6593             creation_date,
6594             created_by,
6595             group_id,
6596             source_code,
6597             source_line_id,
6598             organization_id,
6599             organization_type,
6600             load_type,
6601             status_type,
6602             first_unit_start_date,
6603             last_unit_completion_date,
6604             bom_revision_date,
6605             routing_revision_date,
6606             primary_item_id,
6607             class_code,
6608             job_name,
6609             firm_planned_flag,
6610             start_quantity,
6611 	    net_quantity,
6612             demand_class,
6613             project_id,
6614             task_id,
6615 	    schedule_group_id,
6616        	    build_sequence,
6617 	    line_id,
6618 	    alternate_bom_designator,
6619 	    alternate_routing_designator,
6620 	    end_item_unit_number,
6621 	    process_phase,
6622 	    process_status,
6623             bom_reference_id,
6624             routing_reference_id,
6625             BILL_RTG_EXPLOSION_FLAG,
6626             HEADER_ID,
6627             uom_code, --Outbound Changes for XML
6628             SR_INSTANCE_ID
6629             -- dsr
6630             , schedule_priority
6631             , requested_completion_date
6632             )
6633     SELECT  SYSDATE,
6634             arg_user_id,
6635             s.last_update_login,
6636             decode(tp.organization_type,2,s.creation_date,SYSDATE),
6637             arg_user_id,
6638             arg_wip_group_id,
6639             'MSC',
6640             s.transaction_id,
6641             msi.organization_id,
6642             tp.organization_type,
6643             1,
6644             decode(tp.organization_type,2,1,s.implement_status_code),
6645             DECODE( lv_details_enabled(j),
6646                     2,DECODE( tp.organization_type,
6647                               2, s.new_wip_start_date,
6648                               NULL),
6649                     s.new_wip_start_date),
6650             /* BUG 14378841 (FP of BUG 14358365)
6651              * Ferring Enhancement - Post Processing Lead time for Make Items
6652              * We will pass the implement dock date as the completion date
6653              * on the work order.
6654              */
6655             /*decode(v_unconstrained_plan,
6656                      0,s.implement_date,
6657                      1,trunc(s.implement_date)+v_time_stamp), */
6658               decode(v_unconstrained_plan,
6659                        0, nvl(s.implement_dock_date,
6660                           nvl(s.new_dock_date, s.implement_date)),
6661                        1, trunc(nvl(s.implement_dock_date,
6662                                 nvl(s.new_dock_date, s.implement_date))
6663                               + v_time_stamp)),
6664  /* Added to code to release the greatest of sysdate OR the BOM/Routing revision date */
6665             decode( lv_details_enabled(j),1, -- If details and daily buckets
6666                     GREATEST(trunc(sysdate),
6667                              decode(v_rp_plan,
6668                                     1,trunc(s.new_wip_start_date),
6669                                     trunc(msc_calendar.date_offset(
6670                                           s.organization_id,
6671                                           s.sr_instance_id,
6672                                           1, --daily bucket
6673                                           s.need_by_date ,
6674                                           ceil(nvl(msi.fixed_lead_time,0) +
6675                                           nvl(msi.variable_lead_time,0) * s.implement_quantity)*-1 ) ) )
6676                             )+(1439/1440)
6677                     ,NULL),
6678             decode( lv_details_enabled(j),1, -- If details and daily buckets
6679                     GREATEST(trunc(sysdate),trunc( msc_calendar.date_offset
6680                          (s.organization_id,
6681                           s.sr_instance_id,
6682                           1, --daily bucket
6683                           s.need_by_date ,
6684                          ceil(nvl(msi.fixed_lead_time,0) +
6685                           nvl(msi.variable_lead_time,0) * s.implement_quantity)*-1 ) )
6686                             )+(1439/1440)
6687                     ,NULL),
6688             msi.sr_inventory_item_id,
6689             s.implement_wip_class_code,
6690             s.implement_job_name,
6691             s.implement_firm,
6692 /*
6693             decode(msi.rounding_control_type,1,
6694                ROUND(s.implement_quantity/GET_REV_CUM_YIELD_DISC(s.sr_instance_id, s.plan_id
6695                    ,s.process_seq_id,s.transaction_id,s.organization_id,tp.organization_type)),
6696                (s.implement_quantity/GET_REV_CUM_YIELD_DISC(s.sr_instance_id, s.plan_id
6697                    ,s.process_seq_id,s.transaction_id,s.organization_id,tp.organization_type))
6698                    ),*/
6699 /* Bug 4540170 - PLANNED ORDERS RELEASED FROM ASCP DO NOT CREATE BATCH WITH CORRECT QTYS */
6700 /*        Decode(greatest(abs(s.implement_quantity -
6701                                            s.new_order_quantity),
6702                         0.000001),
6703                0.000001, nvl(s.wip_start_quantity,s.implement_quantity),
6704                                               s.implement_quantity), */
6705         decode(tp.organization_type,
6706                1,lv_wip_start_qty_out(j),
6707                lv_net_qty(j)),
6708         lv_net_qty(j),
6709 --	    s.implement_quantity,
6710             s.implement_demand_class,
6711             s.implement_project_id,
6712             s.implement_task_id,
6713 	    s.implement_schedule_group_id,
6714 	    s.implement_build_sequence,
6715        	    s.implement_line_id,
6716             DECODE( tp.organization_type, -- biogen enhancement -part2
6717                     2, to_char(mb.bill_sequence_id), -- for bug 14775879
6718                     s.implement_alternate_bom),      -- FP of bug 14770153
6719             DECODE( tp.organization_type,
6720                     2, to_char(mb.bill_sequence_id),
6721                     s.implement_alternate_routing),
6722             -- commented for biogen enhancement -part2
6723 	  /* substr(s.implement_alternate_bom,1, -- biogen enhancement-part1
6724              instr(s.implement_alternate_bom,'/')-1),
6725 	     substr(s.implement_alternate_routing,1,
6726              instr(s.implement_alternate_routing,'/')-1), */
6727  	    s.implement_unit_number,
6728 	    2,
6729 	    1,
6730             DECODE( tp.organization_type,
6731                     2, mpe.bill_sequence_id,
6732                     NULL),
6733             DECODE( tp.organization_type,
6734                     2, mpe.routing_sequence_id,
6735                     NULL),
6736             'Y',
6737             s.transaction_id,
6738             nvl(s.implement_uom_code,msi.uom_code),
6739             s.sr_instance_id
6740             -- dsr
6741             , s.schedule_priority
6742             , nvl(s.requested_completion_date, s.need_by_date)
6743       FROM  msc_trading_partners    tp,
6744             msc_parameters          param,
6745             msc_system_items        msi,
6746             msc_process_effectivity mpe,
6747             msc_supplies            s,
6748             msc_boms                mb
6749     WHERE   tp.sr_tp_id= msi.organization_id
6750     AND     tp.sr_instance_id= msi.sr_instance_id
6751     AND     tp.partner_type=3
6752     AND     param.organization_id = msi.organization_id
6753     AND     param.sr_instance_id  = msi.sr_instance_id
6754     AND     msi.inventory_item_id = s.inventory_item_id
6755     AND     msi.plan_id = s.plan_id
6756     AND     msi.organization_id = s.organization_id
6757     AND     msi.sr_instance_id = s.sr_instance_id
6758     AND     mpe.plan_id(+)= s.plan_id
6759     AND     mpe.process_sequence_id(+)= s.process_seq_id
6760     -- Added outer join on msc_boms as per columns in MSC_BOMS_N1 index,
6761     -- for biogen enhancement - part2
6762     AND     mb.plan_id(+) = s.plan_id
6763     AND     mb.organization_id(+) = s.organization_id
6764     AND     mb.sr_instance_id(+) = s.sr_instance_id
6765     AND     mb.assembly_item_id(+) = s.inventory_item_id
6766     AND     mb.alternate_bom_designator(+) = s.implement_alternate_bom
6767     AND     s.transaction_id= lv_transaction_id(j)
6768     AND     s.sr_instance_id= lv_instance_id(j)
6769     AND     s.plan_id= arg_plan_id;
6770 
6771     IF SQL%ROWCOUNT > 0
6772     THEN
6773         lv_loaded_jobs := SQL%ROWCOUNT;
6774 
6775     ELSE
6776         lv_loaded_jobs := 0;
6777 
6778     END IF;
6779 
6780 
6781 
6782 
6783 
6784     -- ------------------------------------------------------------------------
6785     -- Perform the wip discrete job mass load for the details
6786     -- ------------------------------------------------------------------------
6787 
6788     /* the details are populated, only if the implement date and quantity
6789        are the same as the planned date and quantity */
6790 
6791     /* If the current plan is DRP plan then check the following conditions
6792        to determin whether we need to release the details or not
6793        1) For planned orders that do not use
6794           substitute components/alternateboms, only the
6795           header level information is released */
6796 
6797     l_count := 1;
6798 
6799 /*
6800     IF l_plan_type = 5 THEN   -- DRP plan
6801        --OPEN  c_release_dtls(arg_transaction_id, arg_plan_id);
6802        --FETCH c_release_dtls INTO l_count;
6803        --CLOSE c_release_dtls;
6804 
6805        -- dsting redo the details enabled flag for things with subst comp
6806        for i IN 1..lv_transaction_id.count loop
6807           SELECT count(1)
6808             INTO l_sub_comp_count
6809             from msc_exception_details excp
6810             where excp.plan_id = arg_plan_id
6811             and number1 = lv_transaction_id(i)
6812             and excp.exception_type = 33
6813             and rownum < 2;
6814 
6815           IF l_sub_comp_count > 0 THEN
6816              lv_details_enabled(i) := 1;
6817           END IF;
6818        END LOOP;
6819     END IF;
6820 */
6821     /* If it is not a drp plan then l_count = 1 so details will be released */
6822     IF l_count = 1 THEN  /* Release details as well */
6823        /* the details are populated, only if the implement date and quantity
6824           are the same as the planned date and quantity */
6825 
6826        /* OPERATIONS */
6827        FORALL j IN 1..lv_job_count
6828        INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
6829            (last_update_date,
6830             last_updated_by,
6831             last_update_login,
6832             creation_date,
6833             created_by,
6834             organization_type,
6835             organization_id,
6836             group_id,
6837             parent_header_id,
6838             operation_seq_num,
6839             department_id,
6840             description,
6841             standard_operation_id,
6842             first_unit_start_date,
6843             first_unit_completion_date,
6844             last_unit_start_date,
6845             last_unit_completion_date,
6846             minimum_transfer_quantity,
6847             count_point_type,
6848             backflush_flag,
6849             SUBSTITUTION_TYPE,
6850             LOAD_TYPE,
6851 	    process_phase,
6852 	    process_status,
6853             operation_seq_id, --Outbound changes for XML
6854             SR_INSTANCE_ID)
6855     SELECT  SYSDATE,
6856             arg_user_id,
6857             s.last_update_login,
6858             SYSDATE,
6859             arg_user_id,
6860             tp.organization_type,
6861             s.organization_id,
6862             arg_wip_group_id,
6863             s.transaction_id,
6864             resreq.OPERATION_SEQ_NUM,
6865             NULL,            --department_id,
6866             NULL,   --description,
6867             NULL,   --standard_operation_id,
6868             min(resreq.START_DATE),   --first_unit_start_date,
6869             min(resreq.START_DATE),   --first_unit_completion_date,
6870             max(resreq.END_DATE),     --last_unit_start_date,
6871             max(resreq.END_DATE),     --last_unit_completion_date,
6872             NULL,   --minimum_transfer_quantity,
6873             NULL,   --count_point_type,
6874             NULL,   --backflush_flag,
6875             SUBST_CHANGE,
6876             LT_OPERATION,
6877             2,
6878             1,
6879             resreq.operation_sequence_id, --Outbound changes for XML
6880             s.sr_instance_id
6881       FROM  msc_trading_partners   tp,
6882             msc_resource_requirements resreq,
6883             msc_parameters          param,
6884             msc_system_items        msi,
6885             msc_supplies            s
6886     WHERE   tp.sr_tp_id= msi.organization_id
6887     AND     tp.sr_instance_id= msi.sr_instance_id
6888     AND     tp.partner_type=3
6889     AND     resreq.sr_instance_id= s.sr_instance_id
6890     AND     resreq.organization_id= s.organization_id
6891     AND     resreq.supply_id = s.transaction_id
6892     AND     resreq.plan_id   = s.plan_id
6893     AND     resreq.parent_id   = 2
6894     AND     resreq.resource_id <> -1
6895     AND     resreq.department_id <> -1
6896     AND     param.organization_id = msi.organization_id
6897     AND     param.sr_instance_id  = msi.sr_instance_id
6898     AND     msi.inventory_item_id = s.inventory_item_id
6899     AND     msi.plan_id = s.plan_id
6900     AND     msi.organization_id = s.organization_id
6901     AND     msi.sr_instance_id = s.sr_instance_id
6902     AND     s.transaction_id= lv_transaction_id(j)
6903     AND     s.sr_instance_id= lv_instance_id(j)
6904     AND     s.plan_id= arg_plan_id
6905     AND     lv_details_enabled(j)= 1
6906     AND     lv_agg_details(j) = 1
6907     GROUP BY
6908             SYSDATE,
6909             arg_user_id,
6910             tp.organization_type,
6911             s.organization_id,
6912             s.last_update_login,
6913             SYSDATE,
6914             arg_user_id,
6915             arg_wip_group_id,
6916             s.transaction_id,
6917             resreq.OPERATION_SEQ_NUM,
6918             NULL,            --department_id,
6919             LT_OPERATION,    --load_type,
6920             NULL,            --description,
6921             NULL,            --standard_operation_id,
6922             NULL,            --minimum_transfer_quantity,
6923             NULL,            --count_point_type,
6924             NULL,            --backflush_flag,
6925             resreq.operation_sequence_id,
6926             s.sr_instance_id;
6927 
6928 
6929     --DBMS_OUTPUT.PUT_LINE('OPERATION_RESOURCE');
6930  /* for bug: 2479630, modified the expression that is passed in the column: usage_rate_or_amount,
6931    to divide the resource_hours by cum. qty (which is equal to new_order_qty/cum. yield) and if that is null
6932     than apply the CY on new_order_qty. This logic to use cummulative_qty is added to remove the calc. errors */
6933 
6934     /* OPERATION RESOURCES */
6935     FORALL j IN 1..lv_job_count
6936     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
6937           ( last_update_date,
6938             last_updated_by,
6939             last_update_login,
6940             creation_date,
6941             created_by,
6942             organization_type,
6943             organization_id,
6944             group_id,
6945             parent_header_id,
6946             operation_seq_num,
6947            -- resource_seq_num, rawasthi
6948             alternate_num,
6949             resource_id_old,
6950             resource_id_new,
6951             usage_rate_or_amount,
6952             scheduled_flag,
6953             applied_resource_units,   --
6954             applied_resource_value,   --
6955             uom_code,
6956             basis_type,     --
6957             activity_id,    --
6958             autocharge_type,     --
6959             standard_rate_flag,  --
6960             start_date,
6961             completion_date,
6962             assigned_units,
6963             SUBSTITUTION_TYPE,
6964             LOAD_TYPE,
6965 	    process_phase,
6966 	    process_status,
6967             description,
6968             SR_INSTANCE_ID,
6969             operation_seq_id, --Outbound changes for XML
6970             FIRM_FLAG,
6971             resource_hours,
6972             department_id
6973 	-- added the following for dsr
6974 	   , setup_id
6975 	   , group_sequence_id
6976 	   , group_sequence_number
6977            , batch_id
6978 	   , maximum_assigned_units
6979 	   , parent_seq_num
6980 	   , resource_seq_num
6981            , schedule_seq_num
6982 	 )
6983     SELECT  SYSDATE,
6984             arg_user_id,
6985             s.last_update_login,
6986             SYSDATE,
6987             arg_user_id,
6988             tp.organization_type,
6989             s.organization_id,
6990             arg_wip_group_id,
6991             s.transaction_id,
6992             resreq.OPERATION_SEQ_NUM,
6993            -- resreq.RESOURCE_SEQ_NUM,
6994             resreq.ALTERNATE_NUM,
6995             resreq.RESOURCE_ID,
6996             resreq.RESOURCE_ID,
6997              /* for OPM orgs (tp.organization_type =2) we don't consider lv_inflate_wip */
6998             /* decode(resreq.parent_seq_num, null, decode(decode(tp.organization_type,2,2,1),1,(resreq.RESOURCE_HOURS/decode(resreq.basis_type,2,1,
6999                     nvl(resreq.cummulative_quantity,
7000                              decode(msi.rounding_control_type,1,
7001                ROUND(s.new_order_quantity/decode(tp.organization_type,2,1,nvl(resreq.REVERSE_CUMULATIVE_YIELD,1)) ,6),
7002                     (s.new_order_quantity/decode(tp.organization_type,2,1,nvl(resreq.REVERSE_CUMULATIVE_YIELD,1)) )
7003                             ) ) ))* decode(mdr.efficiency,NULL,100,0,100,mdr.efficiency)/100 * decode(mdr.utilization,NULL,100,0,100,mdr.utilization)/100,
7004                                     resreq.RESOURCE_HOURS/decode(resreq.basis_type,2,1,
7005                     nvl(resreq.cummulative_quantity,
7006                              decode(msi.rounding_control_type,1,
7007                ROUND(s.new_order_quantity/decode(tp.organization_type,2,1,nvl(resreq.REVERSE_CUMULATIVE_YIELD,1)) ,6),
7008                     (s.new_order_quantity/decode(tp.organization_type,2,1,nvl(resreq.REVERSE_CUMULATIVE_YIELD,1)) )
7009                             ) ) )), resreq.usage_rate),  */
7010             resreq.usage_rate,
7011             decode(nvl(resreq.schedule_flag,1),-23453,1,1,1,resreq.schedule_flag),
7012             NULL,
7013             NULL,
7014             v_hour_uom,
7015             resreq.basis_type,
7016             NULL,
7017             NULL,
7018             NULL,
7019             nvl(resreq.firm_start_date,resreq.START_DATE),
7020             nvl(resreq.firm_end_date,resreq.END_DATE),
7021             resreq.ASSIGNED_UNITS,
7022             decode(resreq.parent_seq_num,null,SUBST_CHANGE,SUBST_ADD),
7023             -- SUBST_CHANGE,
7024             LT_RESOURCE,
7025             2,
7026             1,
7027             NULL,
7028             s.sr_instance_id,
7029             resreq.operation_sequence_id, --Outbound changes for XML
7030             -- dsr decode(nvl(resreq.firm_flag,0),0,2,1),
7031             NVL(resreq.firm_flag, 0), -- if null, then default to not firm (0)
7032             resreq.resource_hours,
7033             resreq.department_id
7034             -- added the following for dsr
7035 	    , resreq.setup_id
7036 	    , resreq.group_sequence_id
7037 	    , resreq.group_sequence_number
7038      	    , resreq.batch_number
7039 	    , resreq.maximum_assigned_units
7040 	    , resreq.parent_seq_num
7041 	    , resreq.orig_resource_seq_num
7042             , resreq.resource_seq_num
7043       FROM  msc_trading_partners   tp,
7044             msc_resource_requirements resreq,
7045             msc_parameters          param,
7046             msc_system_items        msi,
7047             msc_supplies            s,
7048             msc_department_resources mdr
7049     WHERE   tp.sr_tp_id= msi.organization_id
7050     AND     tp.sr_instance_id= msi.sr_instance_id
7051     AND     tp.partner_type=3
7052     AND     resreq.sr_instance_id= s.sr_instance_id
7053     AND     resreq.organization_id= s.organization_id
7054     AND     resreq.supply_id = s.transaction_id
7055     AND     resreq.plan_id   = s.plan_id
7056     AND     resreq.parent_id   = 2
7057     AND     resreq.resource_id <> -1
7058     AND     resreq.department_id <> -1
7059     AND     param.organization_id = msi.organization_id
7060     AND     param.sr_instance_id  = msi.sr_instance_id
7061     AND     msi.inventory_item_id = s.inventory_item_id
7062     AND     msi.plan_id = s.plan_id
7063     AND     msi.organization_id = s.organization_id
7064     AND     msi.sr_instance_id = s.sr_instance_id
7065     AND     s.transaction_id= lv_transaction_id(j)
7066     AND     s.sr_instance_id= lv_instance_id(j)
7067     AND     s.plan_id= arg_plan_id
7068     AND     lv_details_enabled(j)= 1
7069     AND     lv_agg_details(j) = 1
7070     AND     resreq.plan_id = mdr.plan_id
7071     AND     resreq.organization_id =mdr.organization_id
7072     AND     resreq.sr_instance_id = mdr.sr_instance_id
7073     AND     resreq.resource_id = mdr.resource_id
7074     AND     resreq.department_id=mdr.department_id;
7075 
7076 
7077     --DBMS_OUTPUT.PUT_LINE('LOAD COMPONENTS');
7078 
7079  /* for bug: 2378484, added code to consider the  cum yield in the calc of qty_per_assembly */
7080     /* UPDATE EXISTING COMPONENTS                      *
7081      |    We should set inventory_item_id_new to NULL  |
7082      *                                                 */
7083     FORALL j IN 1..lv_job_count
7084     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
7085             (last_update_date,
7086             last_updated_by,
7087             last_update_login,
7088             creation_date,
7089             created_by,
7090             organization_type,
7091             organization_id,
7092             group_id,
7093             parent_header_id,
7094             operation_seq_num,
7095             inventory_item_id_old,
7096             inventory_item_id_new,
7097             basis_type,
7098             quantity_per_assembly,
7099             component_yield_factor,
7100             department_id,
7101             wip_supply_type,
7102             date_required,
7103             required_quantity,
7104             quantity_issued,
7105             supply_subinventory,
7106             supply_locator_id,
7107             mrp_net_flag,
7108             mps_required_quantity,
7109             mps_date_required,
7110             SUBSTITUTION_TYPE,
7111             LOAD_TYPE,
7112 	    process_phase,
7113 	    process_status,
7114             description,
7115 --            operation_seq_id, --Outbound changes for XML
7116             uom_code, --Outbound Changes for XML
7117             SR_INSTANCE_ID)
7118     SELECT  SYSDATE,
7119             arg_user_id,
7120             s.last_update_login,
7121             SYSDATE,
7122             arg_user_id,
7123             tp.organization_type,
7124             s.organization_id,
7125             arg_wip_group_id,
7126             s.transaction_id,
7127             nvl(md.op_seq_num,1),
7128             icomp.sr_inventory_item_id,
7129             decode(l_apps_ver,
7130                    '6', null,
7131                    '5', null,
7132                    '4', null,
7133                    '3', null,
7134                    icomp.sr_inventory_item_id), -- BUG 13624336 (FP of BUG 13500698)
7135             decode(md.component_scaling_type,1,NULL,md.component_scaling_type),
7136 /*
7137             decode(lv_round_primary_item(j),1,
7138                ROUND(md.USING_REQUIREMENT_QUANTITY/ROUND(s.implement_quantity/GET_REV_CUM_YIELD_DISC_COMP(s.sr_instance_id
7139                         ,s.plan_id,s.process_seq_id,s.transaction_id,s.organization_id,tp.organization_type,md.op_seq_num),6),6),
7140                ROUND(md.USING_REQUIREMENT_QUANTITY/(s.implement_quantity/GET_REV_CUM_YIELD_DISC_COMP(s.sr_instance_id
7141                         ,s.plan_id,s.process_seq_id,s.transaction_id,s.organization_id,tp.organization_type,md.op_seq_num)),6)
7142                   ),*/
7143 --            round(md.USING_REQUIREMENT_QUANTITY/s.wip_start_quantity,6),
7144             TO_NUMBER(NULL),       --Quantity_per
7145             md.component_yield_factor,
7146             TO_NUMBER(NULL),       --Department_ID
7147 /*
7148             NVL(GET_WIP_SUPPLY_TYPE(s.plan_id, s.sr_instance_id,s.process_seq_id,
7149                                     s.inventory_item_id,md.inventory_item_id,s.organization_id),
7150                     icomp.wip_supply_type),*/
7151             md.wip_supply_type,
7152             md.USING_ASSEMBLY_DEMAND_DATE,
7153             md.USING_REQUIREMENT_QUANTITY,
7154             0,
7155             TO_CHAR(NULL),     -- Sub Inventory
7156             TO_NUMBER(NULL),   -- Locator ID
7157             1,                 -- MRP_NET_FLAG
7158             md.USING_REQUIREMENT_QUANTITY,
7159             md.USING_ASSEMBLY_DEMAND_DATE,
7160             SUBST_CHANGE,
7161             LT_COMPONENT,
7162             2,
7163             1,
7164             TO_CHAR(NULL),
7165 --            md.operation_seq_id,
7166             nvl(s.implement_uom_code,msi.uom_code),
7167             s.sr_instance_id
7168       FROM  msc_trading_partners    tp,
7169             msc_system_items        icomp,
7170             msc_demands             md,
7171             msc_parameters          param,
7172             msc_system_items        msi,
7173             msc_supplies            s
7174     WHERE   /*
7175             ( md.SUBST_ITEM_FLAG <> 1
7176               OR md.SUBST_ITEM_FLAG IS NULL) */
7177             not exists (select 'x'
7178                         from msc_exception_details excp
7179                         where excp.plan_id = msi.plan_id
7180                         and excp.number1 = s.transaction_id
7181                         and excp.inventory_item_id = msi.inventory_item_id
7182                         and excp.organization_id = msi.organization_id
7183                         and excp.sr_instance_id = msi.sr_instance_id
7184                         and excp.exception_type = 33
7185                         and excp.number2 = md.inventory_item_id)
7186     AND     tp.sr_tp_id= msi.organization_id
7187     AND     tp.sr_instance_id= msi.sr_instance_id
7188     AND     tp.partner_type=3
7189     AND     icomp.inventory_item_id= md.inventory_item_id
7190     AND     icomp.organization_id= md.organization_id
7191     AND     icomp.sr_instance_id= md.sr_instance_id
7192     AND     icomp.plan_id= md.plan_id
7193     AND     nvl(icomp.wip_supply_type,0) <> 6
7194     AND     md.sr_instance_Id= s.sr_instance_Id
7195     AND     md.disposition_id= s.transaction_id
7196     AND     md.plan_id= s.plan_id
7197     AND     md.origination_type IN (1,47)
7198     AND     param.organization_id = msi.organization_id
7199     AND     param.sr_instance_id  = msi.sr_instance_id
7200     AND     msi.inventory_item_id = s.inventory_item_id
7201     AND     msi.plan_id = s.plan_id
7202     AND     msi.organization_id = s.organization_id
7203     AND     msi.sr_instance_id = s.sr_instance_id
7204     AND     s.transaction_id= lv_transaction_id(j)
7205     AND     s.sr_instance_id= lv_instance_id(j)
7206     AND     s.plan_id= arg_plan_id
7207     AND     lv_details_enabled(j)= 1
7208     AND     lv_agg_details(j) = 1;
7209 
7210     --Loading Co-products/by-products for OPM orgs
7211     FORALL j IN 1..lv_job_count
7212     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
7213             (last_update_date,
7214             last_updated_by,
7215             last_update_login,
7216             creation_date,
7217             created_by,
7218             organization_type,
7219             organization_id,
7220             group_id,
7221             parent_header_id,
7222             operation_seq_num,
7223             inventory_item_id_old,
7224             inventory_item_id_new,
7225             quantity_per_assembly,
7226             department_id,
7227             wip_supply_type,
7228             date_required,
7229             required_quantity,
7230             quantity_issued,
7231             supply_subinventory,
7232             supply_locator_id,
7233             mrp_net_flag,
7234             mps_required_quantity,
7235             mps_date_required,
7236             SUBSTITUTION_TYPE,
7237             LOAD_TYPE,
7238 	    process_phase,
7239 	    process_status,
7240             description,
7241             uom_code, --Outbound Changes for XML
7242             SR_INSTANCE_ID)
7243     SELECT  SYSDATE,
7244             arg_user_id,
7245             s.last_update_login,
7246             SYSDATE,
7247             arg_user_id,
7248             tp.organization_type,
7249             s.organization_id,
7250             arg_wip_group_id,
7251             s.transaction_id,
7252             nvl(co.operation_seq_num,1),
7253             icomp.sr_inventory_item_id,
7254             decode(l_apps_ver,
7255                    '6',null,
7256                    '5',null,
7257                    '4',null,
7258                    '3',null,
7259                    icomp.sr_inventory_item_id), -- BUG 13624336 (FP of BUG 13500698)
7260             TO_NUMBER(NULL),       --Quantity_per
7261             TO_NUMBER(NULL),       --Department_ID
7262             co.wip_supply_type,
7263             co.NEW_SCHEDULE_DATE,
7264             co.NEW_ORDER_QUANTITY,
7265             0,
7266             TO_CHAR(NULL),     -- Sub Inventory
7267             TO_NUMBER(NULL),   -- Locator ID
7268             1,                 -- MRP_NET_FLAG
7269             co.NEW_ORDER_QUANTITY,
7270             co.NEW_SCHEDULE_DATE,
7271             SUBST_CHANGE,
7272             LT_COMPONENT,
7273             2,
7274             1,
7275             TO_CHAR(NULL),
7276             nvl(s.implement_uom_code,msi.uom_code),
7277             s.sr_instance_id
7278       FROM  msc_trading_partners    tp,
7279             msc_system_items        icomp,
7280             msc_supplies            co,
7281             msc_parameters          param,
7282             msc_system_items        msi,
7283             msc_supplies            s
7284     WHERE  /* not exists (select 'x'
7285                         from msc_exception_details excp
7286                         where excp.plan_id = msi.plan_id
7287                         and excp.number1 = s.transaction_id
7288                         and excp.inventory_item_id = msi.inventory_item_id
7289                         and excp.organization_id = msi.organization_id
7290                         and excp.sr_instance_id = msi.sr_instance_id
7291                         and excp.exception_type = 33
7292                         and excp.number2 = co.inventory_item_id)
7293     AND */
7294             tp.sr_tp_id             = msi.organization_id
7295     AND     tp.sr_instance_id       = msi.sr_instance_id
7296     AND     tp.partner_type         = 3
7297     AND     icomp.inventory_item_id = co.inventory_item_id
7298     AND     icomp.organization_id   = co.organization_id
7299     AND     icomp.sr_instance_id    = co.sr_instance_id
7300     AND     icomp.plan_id           = co.plan_id
7301     AND     co.sr_instance_Id       = s.sr_instance_Id
7302     AND     co.disposition_id       = s.transaction_id
7303     AND     co.plan_id              = s.plan_id
7304     AND     co.order_type           = 17        --Co-product /by-product
7305     AND     param.organization_id   = msi.organization_id
7306     AND     param.sr_instance_id    = msi.sr_instance_id
7307     AND     msi.inventory_item_id   = s.inventory_item_id
7308     AND     msi.plan_id             = s.plan_id
7309     AND     msi.organization_id     = s.organization_id
7310     AND     msi.sr_instance_id      = s.sr_instance_id
7311     AND     s.transaction_id        = lv_transaction_id(j)
7312     AND     s.sr_instance_id        = lv_instance_id(j)
7313     AND     s.plan_id               = arg_plan_id
7314     AND     lv_details_enabled(j)   = 1
7315     AND     lv_agg_details(j)       = 1
7316     AND     tp.organization_type    = 2;
7317 
7318     --DBMS_OUTPUT.PUT_LINE('LOAD SUBSTITUTE COMPONENTS');
7319     /* SUBSTITUTE EXISTING COMPONENTS */
7320     FORALL j IN 1..lv_job_count
7321     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
7322             (last_update_date,
7323             last_updated_by,
7324             last_update_login,
7325             creation_date,
7326             created_by,
7327             organization_type,
7328             group_id,
7329             parent_header_id,
7330             operation_seq_num,
7331             inventory_item_id_old,
7332             inventory_item_id_new,
7333             quantity_per_assembly,
7334             department_id,
7335             wip_supply_type,
7336             date_required,
7337             required_quantity,
7338             quantity_issued,
7339             supply_subinventory,
7340             supply_locator_id,
7341             mrp_net_flag,
7342             mps_required_quantity,
7343             mps_date_required,
7344             SUBSTITUTION_TYPE,
7345             LOAD_TYPE,
7346 	    process_phase,
7347 	    process_status,
7348             description,
7349 --            operation_seq_id, --Outbound changes for XML
7350             uom_code, --Outbound Changes for XML
7351             SR_INSTANCE_ID)
7352     SELECT  SYSDATE,
7353             arg_user_id,
7354             s.last_update_login,
7355             SYSDATE,
7356             arg_user_id,
7357             tp.organization_type,
7358             arg_wip_group_id,
7359             s.transaction_id,
7360             nvl(md.op_seq_num,1),
7361             sr_item.sr_inventory_item_id,
7362             icomp.sr_inventory_item_id,
7363 /*
7364             decode(lv_round_primary_item(j),1,
7365                      ROUND(md.USING_REQUIREMENT_QUANTITY/ROUND(s.implement_quantity/GET_REV_CUM_YIELD_DISC_COMP(s.sr_instance_id
7366                          ,s.plan_id,s.process_seq_id,s.transaction_id,s.organization_id,tp.organization_type,md.op_seq_num),6),6),
7367                      ROUND(md.USING_REQUIREMENT_QUANTITY/(s.implement_quantity/GET_REV_CUM_YIELD_DISC_COMP(s.sr_instance_id
7368                          ,s.plan_id,s.process_seq_id,s.transaction_id,s.organization_id,tp.organization_type,md.op_seq_num)),6)
7369                   ),*/
7370             bsub.usage_quantity,
7371             TO_NUMBER(NULL),       --Department_ID
7372 /*
7373             NVL(GET_WIP_SUPPLY_TYPE(s.plan_id, s.sr_instance_id,s.process_seq_id,
7374                                     s.inventory_item_id,md.inventory_item_id,s.organization_id),
7375                     icomp.wip_supply_type),*/
7376             nvl(md.wip_supply_type,icomp.wip_supply_type),
7377             md.USING_ASSEMBLY_DEMAND_DATE,
7378             md.USING_REQUIREMENT_QUANTITY,
7379             0,
7380             TO_CHAR(NULL),     -- Sub Inventory
7381             TO_NUMBER(NULL),   -- Locator ID
7382             1,
7383             md.USING_REQUIREMENT_QUANTITY,
7384             md.USING_ASSEMBLY_DEMAND_DATE,
7385             SUBST_CHANGE,
7386             LT_COMPONENT,
7387             2,
7388             1,
7389             TO_CHAR(NULL),
7390 --            md.operation_seq_id,
7391             nvl(s.implement_uom_code,msi.uom_code),
7392             s.sr_instance_id
7393       FROM  msc_trading_partners    tp,
7394             msc_item_id_lid         sr_item,
7395             msc_bom_components      subcomp,
7396             msc_component_substitutes bsub,
7397 /*            msc_bom_components      bcomp, */
7398             msc_boms                bom,
7399             msc_system_items        icomp,
7400             msc_demands             md,
7401             msc_parameters          param,
7402             msc_system_items        msi,
7403             msc_supplies            s
7404     WHERE   tp.sr_tp_id= msi.organization_id
7405     AND     tp.sr_instance_id= msi.sr_instance_id
7406     AND     tp.partner_type=3
7407     AND     sr_item.inventory_item_id= subcomp.inventory_item_id
7408     AND     sr_item.sr_instance_id= subcomp.sr_instance_id
7409     AND    subcomp.plan_id               = bsub.plan_id
7410     AND     subcomp.bill_sequence_id      = bsub.bill_sequence_id
7411     AND     subcomp.COMPONENT_SEQUENCE_ID = bsub.COMPONENT_SEQUENCE_ID
7412     AND     bsub.substitute_item_id = md.inventory_item_id
7413     AND     bsub.organization_id    = md.organization_id
7414     AND     bsub.plan_id            = md.plan_id
7415     AND     bsub.COMPONENT_SEQUENCE_ID= md.comp_seq_id
7416    /* Bug 3636332  AND     bsub.bill_sequence_id   = bom.bill_sequence_id */
7417   --------------------------------------------------------------
7418 /*    AND     md.SUBST_ITEM_FLAG=1 */
7419   --------------------------------------------------------------
7420     AND     ( bom.ALTERNATE_BOM_DESIGNATOR= s.ALTERNATE_BOM_DESIGNATOR
7421               OR ( bom.ALTERNATE_BOM_DESIGNATOR IS NULL
7422                    AND s.ALTERNATE_BOM_DESIGNATOR IS NULL))
7423     AND     bom.assembly_item_id=  s.inventory_item_id
7424     AND     bom.organization_id=   s.organization_id
7425     AND     bom.sr_instance_id=    s.sr_instance_id
7426     AND     bom.plan_id=           s.plan_id
7427     AND     icomp.inventory_item_id= md.inventory_item_id
7428     AND     icomp.organization_id= md.organization_id
7429     AND     icomp.sr_instance_id= md.sr_instance_id
7430     AND     icomp.plan_id= md.plan_id
7431     AND     md.sr_instance_Id= s.sr_instance_Id
7432     AND     md.disposition_id= s.transaction_id
7433     AND     md.plan_id= s.plan_id
7434     AND     md.origination_type in (1,47)
7435     AND     param.organization_id = msi.organization_id
7436     AND     param.sr_instance_id  = msi.sr_instance_id
7437     AND     msi.inventory_item_id = s.inventory_item_id
7438     AND     msi.plan_id = s.plan_id
7439     AND     msi.organization_id = s.organization_id
7440     AND     msi.sr_instance_id = s.sr_instance_id
7441     AND     s.transaction_id= lv_transaction_id(j)
7442     AND     s.sr_instance_id= lv_instance_id(j)
7443     AND     s.plan_id= arg_plan_id
7444     AND     lv_details_enabled(j)= 1
7445     AND     lv_agg_details(j) = 1;
7446 
7447     --DBMS_OUTPUT.PUT_LINE('RESOURCE_USAGE');
7448 
7449 
7450     -- RESOURCE USAGE
7451     FORALL j IN 1..lv_job_count
7452     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
7453           ( last_update_date,
7454             last_updated_by,
7455             last_update_login,
7456             creation_date,
7457             created_by,
7458             organization_type,
7459             organization_id,
7460             group_id,
7461             parent_header_id,
7462             operation_seq_num,
7463             -- resource_seq_num,
7464             alternate_num,
7465             resource_id_old,
7466             resource_id_new,
7467             usage_rate_or_amount,
7468             scheduled_flag,
7469             applied_resource_units,   --
7470             applied_resource_value,   --
7471             uom_code,
7472             basis_type,     --
7473             activity_id,    --
7474             autocharge_type,     --
7475             standard_rate_flag,  --
7476             start_date,
7477             completion_date,
7478             assigned_units,
7479             SUBSTITUTION_TYPE,
7480             LOAD_TYPE,
7481 	    process_phase,
7482 	    process_status,
7483             description,
7484             SR_INSTANCE_ID,
7485             operation_seq_id, --Outbound changes for XML
7486             resource_seq_num,
7487             schedule_seq_num,
7488             -- dsr FIRM_FLAG,
7489             department_id,
7490             resource_hours,
7491             parent_seq_num)
7492     SELECT  SYSDATE,
7493             arg_user_id,
7494             s.last_update_login,
7495             SYSDATE,
7496             arg_user_id,
7497             tp.organization_type,
7498             s.organization_id,
7499             arg_wip_group_id,
7500             s.transaction_id,
7501             resreq.OPERATION_SEQ_NUM,
7502            -- resreq.RESOURCE_SEQ_NUM,
7503             resreq.ALTERNATE_NUM,
7504             resreq.RESOURCE_ID,
7505             resreq.RESOURCE_ID,
7506            /* for OPM orgs (tp.organization_type =2) we don't consider lv_inflate_wip */
7507            /* decode(resreq.parent_seq_num, null, decode(decode(tp.organization_type,2,2,lv_inflate_wip),1,(resreq.RESOURCE_HOURS/decode(resreq.basis_type,2,1,
7508                     nvl(decode(resreq.cummulative_quantity,0,1,resreq.cummulative_quantity),
7509                              decode(msi.rounding_control_type,1,
7510                ROUND(s.new_order_quantity/decode(tp.organization_type,2,1,nvl(resreq.REVERSE_CUMULATIVE_YIELD,1)) ,6),
7511                     (s.new_order_quantity/decode(tp.organization_type,2,1,nvl(resreq.REVERSE_CUMULATIVE_YIELD,1)) )
7512                             ) ) ))* decode(mdr.efficiency,NULL,100,0,100,mdr.efficiency)/100 * decode(mdr.utilization,NULL,100,0,100,mdr.utilization)/100,
7513                                     resreq.RESOURCE_HOURS/decode(resreq.basis_type,2,1,
7514                     nvl(decode(resreq.cummulative_quantity,0,1,resreq.cummulative_quantity),
7515                              decode(msi.rounding_control_type,1,
7516                ROUND(s.new_order_quantity/decode(tp.organization_type,2,1,nvl(resreq.REVERSE_CUMULATIVE_YIELD,1)) ,6),
7517                     (s.new_order_quantity/decode(tp.organization_type,2,1,nvl(resreq.REVERSE_CUMULATIVE_YIELD,1)) )
7518                             ) ) )), resreq.usage_rate),  */
7519             resreq.usage_rate,
7520             decode(nvl(resreq.schedule_flag,1),-23453,1,1,1,resreq.schedule_flag),
7521             NULL,
7522             NULL,
7523             v_hour_uom,
7524             resreq.basis_type,
7525             NULL,
7526             NULL,
7527             NULL,
7528             nvl(resreq.firm_start_date,resreq.START_DATE),
7529             nvl(resreq.firm_end_date,resreq.END_DATE),
7530             resreq.ASSIGNED_UNITS,
7531             SUBST_CHANGE,
7532             LT_RESOURCE_USAGE,
7533             2,
7534             1,
7535             NULL,
7536             s.sr_instance_id,
7537             resreq.operation_sequence_id, --Outbound changes for XML
7538             resreq.orig_resource_seq_num,
7539             resreq.resource_seq_num,
7540             -- dsr decode(nvl(resreq.firm_flag,0),0,2,1),
7541             resreq.department_id,
7542             resreq.resource_hours,
7543             resreq.parent_seq_num
7544       FROM  msc_trading_partners   tp,
7545             msc_resource_requirements resreq,
7546             msc_parameters          param,
7547             msc_system_items        msi,
7548             msc_supplies            s,
7549             msc_department_resources mdr
7550     WHERE   tp.sr_tp_id= msi.organization_id
7551     AND     tp.sr_instance_id= msi.sr_instance_id
7552     AND     tp.partner_type=3
7553     AND     resreq.sr_instance_id= s.sr_instance_id
7554     AND     resreq.organization_id= s.organization_id
7555     AND     resreq.supply_id = s.transaction_id
7556     AND     resreq.plan_id   = s.plan_id
7557     AND     resreq.parent_id   = 1
7558     AND     resreq.resource_id <> -1
7559     AND     resreq.department_id <> -1
7560     AND     param.organization_id = msi.organization_id
7561     AND     param.sr_instance_id  = msi.sr_instance_id
7562     AND     msi.inventory_item_id = s.inventory_item_id
7563     AND     msi.plan_id = s.plan_id
7564     AND     msi.organization_id = s.organization_id
7565     AND     msi.sr_instance_id = s.sr_instance_id
7566     AND     s.transaction_id= lv_transaction_id(j)
7567     AND     s.sr_instance_id= lv_instance_id(j)
7568     AND     s.plan_id= arg_plan_id
7569     AND     lv_details_enabled(j)= 1
7570     AND     lv_agg_details(j) = 1
7571     AND     resreq.plan_id = mdr.plan_id
7572     AND     resreq.organization_id =mdr.organization_id
7573     AND     resreq.sr_instance_id = mdr.sr_instance_id
7574     AND     resreq.resource_id = mdr.resource_id
7575     AND     resreq.department_id=mdr.department_id ;
7576    -- AND     tp.organization_type = 2;
7577 
7578    -- dsr starts here
7579 --    print_debug_info('OPERATION RESOURCE_INSTANCES');
7580 	-- OPERATION RESOURCE_INSTANCES
7581 
7582     FORALL j IN 1..lv_job_count
7583     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
7584           ( last_update_date,
7585             last_updated_by,
7586             last_update_login,
7587             creation_date,
7588             created_by,
7589             organization_type,
7590             organization_id,
7591             group_id,
7592             parent_header_id,
7593             operation_seq_num,
7594             -- resource_seq_num,
7595             resource_id_old, --rawasthi
7596             resource_id_new, -- xx resource_id, ???
7597             RESOURCE_INSTANCE_ID,
7598             start_date,
7599             completion_date,
7600             SUBSTITUTION_TYPE,
7601             LOAD_TYPE,
7602             process_phase,
7603             process_status,
7604             SR_INSTANCE_ID,
7605             operation_seq_id,
7606             -- FIRM_FLAG,
7607             resource_hours,
7608             department_id,
7609 	    -- setup_id,
7610 	    SERIAL_NUMBER,
7611             group_sequence_id,
7612             group_sequence_number,
7613             batch_id,
7614             resource_seq_num,
7615             schedule_seq_num
7616             , assigned_units -- jguo
7617             , parent_seq_num
7618  )
7619     SELECT
7620            SYSDATE,
7621            arg_user_id,
7622            s.last_update_login,
7623            SYSDATE,
7624            arg_user_id,
7625            tp.organization_type,
7626            s.organization_id,
7627            arg_wip_group_id,
7628            s.transaction_id,
7629            resreq.OPERATION_SEQ_NUM,
7630           -- resreq.RESOURCE_SEQ_NUM,
7631            resreq.resource_id,
7632            resreq.resource_id,
7633            dep_res_inst.RES_INSTANCE_ID,  -- jguo dep_res_inst.dept_RESOURCE_INST_ID,
7634            nvl(resreq.firm_start_date,res_instreq.START_DATE),
7635            nvl(resreq.firm_end_date,res_instreq.END_DATE),
7636            SUBST_ADD, -- jguo SUBST_CHANGE,
7637            LT_RESOURCE_INSTANCE,
7638            2,
7639            1,
7640            s.sr_instance_id,
7641            resreq.operation_sequence_id,
7642         -- res_instreq.firm_flag,
7643            resreq.resource_hours,
7644            resreq.department_id,
7645           -- resreq.department_id / 2, rawasthi
7646         -- res_instreq.setup_id,
7647            dep_res_inst.serial_number,
7648            resreq.group_sequence_id,
7649            resreq.group_sequence_number,
7650            res_instreq.batch_number,
7651  ---- sbala res_instreq.res_inst_batch_id
7652            resreq.orig_resource_seq_num,
7653            resreq.resource_seq_num
7654         -- resreq.alternate_num,
7655          , 1 -- jguo
7656          , resreq.parent_seq_num
7657     FROM
7658           msc_trading_partners   tp,
7659           msc_resource_requirements resreq,
7660           msc_resource_instance_reqs res_instreq,
7661           msc_dept_res_instances dep_res_inst,
7662           msc_supplies            s
7663     WHERE
7664          tp.sr_tp_id=s.organization_id
7665  AND     tp.sr_instance_id= s.sr_instance_id
7666  AND     tp.partner_type=3
7667  AND     resreq.sr_instance_id= s.sr_instance_id
7668  AND     resreq.organization_id= s.organization_id
7669  AND     resreq.supply_id = s.transaction_id
7670  AND     resreq.plan_id   = s.plan_id
7671 -- dsr AND     resreq.transaction_id = res_instreq.res_inst_transaction_id
7672  AND     resreq.resource_seq_num = res_instreq.resource_seq_num
7673  AND     resreq.operation_seq_num = res_instreq.operation_seq_num
7674  AND     resreq.resource_id = res_instreq.resource_id
7675  AND     resreq.supply_id = res_instreq.supply_id
7676  AND     resreq.sr_instance_id = res_instreq.sr_instance_id
7677  AND     resreq.plan_id = res_instreq.plan_id
7678  AND     resreq.parent_id = res_instreq.parent_id  --rawasthi
7679  AND     resreq.start_date = res_instreq.start_date
7680  AND     resreq.parent_id   = 2
7681  AND     resreq.resource_id <> -1
7682  AND     resreq.department_id <> -1
7683 -- dsr   AND     res_instreq.res_instance_id = dep_res_inst.dept_resource_inst_id
7684  AND      res_instreq.plan_id = dep_res_inst.plan_id
7685  AND      res_instreq.sr_instance_id = dep_res_inst.sr_instance_id
7686  AND      res_instreq.department_id = dep_res_inst.department_id
7687  AND      res_instreq.resource_id = dep_res_inst.resource_id
7688 /*anuj_review: join condition for serial number and res_instance_id */
7689  AND      res_instreq.serial_number = dep_res_inst.serial_number
7690  AND      res_instreq.res_instance_id = dep_res_inst.res_instance_id
7691  AND      s.transaction_id= lv_transaction_id(j)
7692  AND      s.sr_instance_id= lv_instance_id(j)
7693  AND      s.plan_id= arg_plan_id
7694  AND      lv_details_enabled(j)= 1
7695  AND      lv_agg_details(j) = 1
7696     ;
7697 
7698 --   print_debug_info('load_wip_discrete_jobs# rows inserted into MSC_WIP_JOB_DTLS_INTERFACE = ' || SQL%ROWCOUNT);
7699     -- RESOURCE INSTANCE USAGES
7700   -- print_debug_info('RESOURCE INSTANCE USAGES ');
7701 
7702 	FORALL j IN 1..lv_job_count
7703     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
7704           ( last_update_date,
7705             last_updated_by,
7706             last_update_login,
7707             creation_date,
7708             created_by,
7709             organization_type,
7710             organization_id,
7711             group_id,
7712             parent_header_id,
7713             operation_seq_num,
7714             -- resource_seq_num,
7715             resource_id_old,
7716 	    resource_id_new, -- xx RESOURCE_ID, ???
7717             RESOURCE_INSTANCE_ID,
7718             start_date,
7719             completion_date,
7720             SUBSTITUTION_TYPE,
7721             LOAD_TYPE,
7722             process_phase,
7723             process_status,
7724             SR_INSTANCE_ID,
7725             operation_seq_id,
7726             FIRM_FLAG,
7727             resource_hours,
7728             department_id,
7729             serial_number,
7730             resource_seq_num,
7731             schedule_seq_num
7732          -- alternate_num
7733            , assigned_units -- jguo
7734            ,parent_seq_num
7735  )
7736     SELECT
7737            SYSDATE,
7738            arg_user_id,
7739            s.last_update_login,
7740            SYSDATE,
7741            arg_user_id,
7742            tp.organization_type,
7743            s.organization_id,
7744            arg_wip_group_id,
7745            s.transaction_id,
7746            resreq.OPERATION_SEQ_NUM,
7747           -- resreq.RESOURCE_SEQ_NUM,
7748            resreq.RESOURCE_ID,
7749            resreq.RESOURCE_ID,
7750            dep_res_inst.RES_INSTANCE_ID,  -- jguo dep_res_inst.dept_RESOURCE_INST_ID,
7751            nvl(resreq.firm_start_date,res_instreq.START_DATE),
7752            nvl(resreq.firm_end_date,res_instreq.END_DATE),
7753            SUBST_ADD, -- jguo SUBST_CHANGE,
7754            LT_RESOURCE_INST_USAGE,
7755            2,
7756            1,
7757            s.sr_instance_id,
7758            resreq.operation_sequence_id,
7759            resreq.firm_flag,
7760            res_instreq.resource_instance_hours,
7761            resreq.department_id,
7762          --  resreq.department_id / 2, rawasthi
7763            dep_res_inst.serial_number,
7764            resreq.orig_resource_seq_num,
7765            resreq.resource_seq_num
7766         -- resreq.alternate_num
7767           ,1 -- jguo
7768           , resreq.parent_seq_num
7769   FROM
7770            msc_trading_partners   tp,
7771            msc_resource_requirements resreq,
7772            msc_resource_instance_reqs res_instreq, -- ??? msc_res_inst_requirements res_instreq,
7773            msc_dept_res_instances dep_res_inst,
7774            msc_supplies            s
7775     WHERE
7776             tp.sr_tp_id=s.organization_id
7777     AND     tp.sr_instance_id= s.sr_instance_id
7778     AND     tp.partner_type=3
7779     AND     resreq.sr_instance_id= s.sr_instance_id
7780     AND     resreq.organization_id= s.organization_id
7781     AND     resreq.supply_id = s.transaction_id
7782     AND     resreq.plan_id   = s.plan_id
7783 -- dsr   AND     resreq.transaction_id = res_instreq.res_inst_transaction_id
7784     AND     resreq.resource_seq_num = res_instreq.resource_seq_num
7785     AND     resreq.operation_seq_num = res_instreq.operation_seq_num
7786     AND     resreq.resource_id = res_instreq.resource_id
7787     AND     resreq.supply_id = res_instreq.supply_id
7788     AND     resreq.sr_instance_id = res_instreq.sr_instance_id
7789     AND     resreq.plan_id = res_instreq.plan_id
7790     AND     resreq.parent_id = res_instreq.parent_id  --rawasthi
7791     AND     resreq.start_date = res_instreq.start_date
7792     AND     resreq.parent_id   = 1
7793     AND     resreq.resource_id <> -1
7794     AND     resreq.department_id <> -1
7795 -- dsr   AND     res_instreq.res_instance_id = dep_res_inst.dept_resource_inst_id
7796     AND     res_instreq.department_id = dep_res_inst.department_id
7797     AND     res_instreq.resource_id = dep_res_inst.resource_id
7798     AND     res_instreq.plan_id = dep_res_inst.plan_id
7799     AND     res_instreq.sr_instance_id = dep_res_inst.sr_instance_id
7800 /* anuj: serail number and resource_instance id joins */
7801     AND     res_instreq.serial_number = dep_res_inst.serial_number
7802     AND     res_instreq.res_instance_id = dep_res_inst.res_instance_id
7803     AND     s.transaction_id= lv_transaction_id(j)
7804     AND     s.sr_instance_id= lv_instance_id(j)
7805     AND     s.plan_id= arg_plan_id
7806     AND     lv_details_enabled(j)= 1
7807     AND     lv_agg_details(j) = 1
7808     ;
7809 
7810 --   print_debug_info('load_wip_discrete_jobs# rows inserted into MSC_WIP_JOB_DTLS_INTERFACE = ' || SQL%ROWCOUNT);
7811 
7812 -- Resource Charges
7813 --   print_debug_info('Resource Charges ');
7814 
7815 FORALL j IN 1..lv_job_count
7816     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
7817           ( last_update_date,
7818             last_updated_by,
7819             last_update_login,
7820             creation_date,
7821             created_by,
7822             organization_type,
7823             organization_id,
7824             group_id,
7825             parent_header_id,
7826             operation_seq_num,
7827             resource_seq_num,
7828 	    alternate_num,
7829             start_date,
7830             completion_date,
7831 	    required_quantity,
7832 	    charge_number,
7833             SR_INSTANCE_ID,
7834             SUBSTITUTION_TYPE,
7835             LOAD_TYPE,
7836             process_phase,
7837             process_status,
7838             resource_id_old,
7839             resource_id_new,
7840             schedule_seq_num
7841 )
7842     SELECT
7843             SYSDATE,
7844             arg_user_id,
7845             s.last_update_login,
7846             SYSDATE,
7847             arg_user_id,
7848             tp.organization_type,
7849             s.organization_id,
7850             arg_wip_group_id,
7851             s.transaction_id,
7852             resreq.OPERATION_SEQ_NUM,
7853             resreq.ORIG_RESOURCE_SEQ_NUM,
7854             resreq.alternate_num,
7855             Chg.charge_start_datetime,
7856             Chg.charge_end_datetime,
7857             chg.charge_quantity, -- Chg.Planned_Charge_Quantity,
7858             Chg.charge_number,
7859             s.sr_instance_id,
7860             SUBST_ADD, -- jguo SUBST_CHANGE, -- 3
7861             LT_CHARGE_TYPE, --8
7862             2,
7863             1,
7864             resreq.RESOURCE_ID,
7865             resreq.RESOURCE_ID,
7866             resreq.resource_seq_num
7867      FROM
7868            msc_trading_partners   tp,
7869            msc_resource_requirements resreq,
7870            msc_supplies            s,
7871            msc_resource_charges chg
7872     WHERE
7873             tp.sr_tp_id=s.organization_id
7874     AND     tp.sr_instance_id= s.sr_instance_id
7875     AND     tp.partner_type=3
7876     AND     resreq.sr_instance_id= s.sr_instance_id
7877     AND     resreq.organization_id= s.organization_id
7878     AND     resreq.supply_id = s.transaction_id
7879     AND     resreq.plan_id   = s.plan_id
7880     AND     resreq.transaction_id = chg.res_transaction_id -- chg.transaction_id
7881     AND     resreq.plan_id = chg.plan_id
7882     AND     resreq.sr_instance_id = chg.sr_instance_id
7883     AND     resreq.parent_id   = 2
7884     AND     resreq.resource_id <> -1
7885     AND     resreq.department_id <> -1
7886     AND     s.transaction_id= lv_transaction_id(j)
7887     AND     s.sr_instance_id= lv_instance_id(j)
7888     AND     s.plan_id= arg_plan_id
7889     AND     lv_details_enabled(j)= 1
7890     AND     lv_agg_details(j) = 1
7891 	;
7892     END IF;  /* End of l_count = 1 */
7893     -- dsr ends here
7894 
7895     FORALL j IN 1..lv_job_count
7896         update msc_supplies
7897            set releasable = RELEASE_ATTEMPTED
7898                ,batch_id   = nvl(batch_id,g_batch_id)
7899          where plan_id = arg_plan_id
7900            and transaction_id= lv_transaction_id(j);
7901 
7902     RETURN lv_loaded_jobs;
7903 
7904 END load_wip_discrete_jobs;
7905 
7906 FUNCTION reschedule_wip_discrete_jobs
7907 ( arg_plan_id			IN      NUMBER
7908 , arg_log_org_id 		IN 	NUMBER
7909 , arg_org_instance              IN      NUMBER
7910 , arg_owning_org_id 		IN 	NUMBER
7911 , arg_owning_instance           IN      NUMBER
7912 , arg_user_id 			IN 	NUMBER
7913 , arg_wip_group_id 		IN 	NUMBER
7914 , arg_mode                      IN      VARCHAR2
7915 , arg_transaction_id            IN      NUMBER
7916 , l_apps_ver                    IN      VARCHAR2
7917 , arg_load_type                 IN      NUMBER DEFAULT NULL -- dsr
7918 )RETURN NUMBER IS
7919 
7920    lv_resched_jobs NUMBER;
7921 
7922 
7923    TYPE NumTab  IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
7924  --TYPE RIDTab  IS TABLE OF ROWID  INDEX BY BINARY_INTEGER;
7925 
7926    lv_transaction_id           NumTab;
7927    lv_instance_id              NumTab;
7928    lv_details_enabled          NumTab;
7929    lv_job_count                NUMBER;
7930    lv_plan_id                  NumTab;
7931    lv_org_id                   Numtab;
7932    lv_agg_details              NumTab;
7933 
7934 -- BUG 9085180
7935 
7936    lv_wip_start_qty_in         NumTab;
7937    lv_wip_start_qty_out        NumTab;
7938    lv_new_order_qty            NumTab;
7939    lv_implement_qty            NumTab;
7940    lv_qty_scrapped             NumTab;
7941    lv_qty_completed            NumTab;
7942    lv_net_qty                  NumTab;
7943    lv_explode_in_wip           NUMBER; -- BUG 10399650
7944 
7945    orig_net_qty                NUMBER;
7946 
7947 CURSOR c_release_dtls(p_transaction_id number,
7948                       p_plan_id  number) IS
7949 SELECT count(1)
7950 FROM msc_supplies
7951 WHERE implement_alternate_bom is not null
7952       and transaction_id = p_transaction_id
7953       and rownum < 2
7954 UNION
7955 SELECT count(1)
7956 from msc_exception_details excp
7957 where excp.plan_id = p_plan_id
7958       and number1 = p_transaction_id
7959       and excp.exception_type = 33
7960       and rownum < 2;
7961 
7962 
7963 CURSOR c_plan_type(p_plan_id number) IS
7964 select plan_type
7965 from msc_plans a
7966 where
7967 plan_id = p_plan_id;
7968 
7969 l_count   NUMBER := 0;
7970 l_plan_type  number := 0;
7971 
7972 
7973 BEGIN
7974 
7975   -- Bug 10399650
7976     SELECT
7977      DECODE(NVL(fnd_profile.value('MSC_EXPLODE_IN_WIP') ,'Y'), 'Y',1 ,2)
7978      INTO  lv_explode_in_wip
7979      FROM  DUAL;
7980   -- we release the discrete job, only if it doesn't use aggregate resources
7981   -- bug 1252659 fix, replace rowid by
7982   --       (transaction_id,sr_instance_id,plan_id)
7983 
7984    l_sql_stmt := ' SELECT s.transaction_id,
7985           s.sr_instance_id,
7986           decode((mp.daily_material_constraints
7987                   + mp.daily_resource_constraints
7988                   + mp.weekly_material_constraints
7989                   + mp.weekly_resource_constraints
7990                   + mp.period_material_constraints
7991                   + mp.period_resource_constraints),
7992                   12, 2,
7993                DECODE(mpb.bucket_type,1,
7994                          Decode(greatest(abs(s.implement_quantity -
7995                                            s.new_order_quantity),
7996                                        0.000001),
7997                                  0.000001,
7998                                  DECODE( s.implement_date,
7999                                          s.new_schedule_date, 1,
8000                                          2),
8001                                 2),
8002                       2)),
8003            s.organization_id,
8004            s.plan_id,
8005            NVL(s.wip_start_quantity, 0) + NVL(s.qty_completed, 0) + NVL(s.qty_scrapped, 0),
8006            NVL(s.new_order_quantity, 0),
8007            NVL(s.implement_quantity, 0),
8008            NVL(s.qty_scrapped, 0),
8009            NVL(s.qty_completed, 0)
8010     FROM msc_supplies s,
8011           msc_plans mp,
8012           msc_plan_organizations_v orgs,
8013           msc_plan_buckets mpb
8014     WHERE s.release_errors is NULL
8015     AND   s.implement_quantity > 0
8016     AND   s.organization_id = orgs.planned_organization
8017     AND   s.sr_instance_id = orgs.sr_instance_id
8018     AND   s.plan_id = :arg_plan_id
8019     AND   orgs.plan_id = :arg_plan_id
8020     AND   orgs.organization_id = :arg_owning_org_id
8021     AND   orgs.owning_sr_instance = :arg_owning_instance
8022     AND   orgs.sr_instance_id = :arg_org_instance
8023     and   s.plan_id = mpb.plan_id
8024     and   s.plan_id = mp.plan_id
8025     AND ( (DECODE(s.disposition_status_type ,
8026                   2,DECODE(SIGN( mp.curr_start_date-s.new_schedule_date),
8027                            1,mp.curr_start_date,
8028                            DECODE(SIGN(s.new_schedule_date-mp.curr_cutoff_date),
8029                                  1,mp.curr_cutoff_date,
8030                                  s.new_schedule_date)),
8031                   s.new_schedule_date ) BETWEEN mpb.bkt_start_date
8032                                         AND mpb.bkt_end_date))
8033     and (s.releasable = ' || RELEASABLE || ' or s.releasable is null )';
8034 
8035     IF  v_batch_id_populated = 2 THEN
8036         l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id is NULL ';
8037     ELSE
8038         l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id = ' || g_batch_id || ' ' ;
8039     END IF;
8040 
8041     IF arg_log_org_id <> arg_owning_org_id THEN
8042             l_sql_stmt :=  l_sql_stmt ||'      AND     orgs.planned_organization = ' || arg_log_org_id || ' ' ;
8043     END IF;
8044 
8045     IF arg_mode IS NULL THEN
8046         l_sql_stmt :=  l_sql_stmt ||'      AND s.load_type = ' || arg_load_type || ' ';
8047         IF v_msc_released_only_by_user = 1 THEN
8048             l_sql_stmt :=  l_sql_stmt || ' AND s.last_updated_by = ' || v_user_id || ' ' ;
8049         END IF;
8050     ELSIF arg_mode = 'WF_BATCH' THEN
8051         l_sql_stmt :=  l_sql_stmt ||'      AND s.load_type = ' || arg_load_type || ' ';
8052     ELSIF arg_mode = 'WF' THEN
8053         l_sql_stmt :=  l_sql_stmt ||'      AND s.transaction_id = ' || arg_transaction_id  || '  ' ;
8054     END IF;
8055 
8056     lv_job_count:= 0;
8057     IF arg_mode IS NULL OR arg_mode = 'WF_BATCH' OR arg_mode = 'WF' THEN
8058         EXECUTE IMMEDIATE l_sql_stmt
8059              BULK COLLECT            INTO lv_transaction_id,
8060                                           lv_instance_id,
8061                                           lv_details_enabled,
8062                                           lv_org_id,
8063                                           lv_plan_id,
8064                                           lv_wip_start_qty_in,
8065                                           lv_new_order_qty,
8066                                           lv_implement_qty,
8067                                           lv_qty_scrapped,
8068                                           lv_qty_completed
8069                                         USING  arg_plan_id
8070                                             ,arg_plan_id
8071                                             ,arg_owning_org_id
8072                                             ,arg_owning_instance
8073                                             ,arg_org_instance    ;
8074         lv_job_count:= SQL%ROWCOUNT;
8075     END IF;
8076 
8077 
8078     -- -----------------------------------------------------------------------
8079     -- Perform the wip discrete job mass load
8080     -- -----------------------------------------------------------------------
8081     --    Due to we only give PLANNED components, BILL_RTG_EXPLOSION_FLAG
8082     --    is set to 'Y'.
8083     --   Bug 10399650 , We will modify the value of BILL_RTG_EXPLOSION_FLAG
8084     --   based on the profile option
8085     --   "MSC: Explode in WIP when Details are not released"
8086     --DBMS_OUTPUT.PUT_LINE('LOAD_JOB');
8087 
8088     FOR k in 1..lv_job_count
8089        Loop
8090                 Begin
8091                   SELECT 2
8092                   Into lv_agg_details(k)
8093                   FROM msc_department_resources deptres,
8094                        msc_resource_requirements resreq
8095                  WHERE resreq.sr_instance_id= lv_instance_id(k)
8096                    AND resreq.supply_id = lv_transaction_id(k)
8097                    AND resreq.organization_id= lv_org_id(k)
8098                    AND resreq.plan_id   = lv_plan_id(k)
8099                    AND resreq.parent_id   = 2
8100                    AND deptres.plan_id  = resreq.plan_id
8101                    AND deptres.sr_instance_id= resreq.sr_instance_id
8102                    AND deptres.resource_id= resreq.resource_id
8103                    AND deptres.department_id= resreq.department_id
8104                    AND deptres.organization_id= resreq.organization_id
8105                    AND deptres.aggregate_resource_flag= 1
8106                    AND rownum=1;
8107                   Exception
8108                   When no_data_found
8109                   then
8110                   lv_agg_details(k) := 1;
8111                   End;
8112 
8113         -- BUG 9085180
8114         -- Compute the lv_wip_start_qty_out and lv_net_qty.
8115 
8116         IF (lv_details_enabled(k) = 1) THEN
8117 
8118            -- There is no change in date/quantity at the time of release
8119            -- and we are populating the details, so we will pass the
8120            -- WIP start and net quantity.
8121            -- In this case, lv_new_order_qty = lv_implement_qty
8122 
8123            lv_wip_start_qty_out(k) := lv_wip_start_qty_in(k);
8124            lv_net_qty(k) := lv_new_order_qty (k) +
8125                             lv_qty_scrapped(k) + lv_qty_completed (k);
8126 
8127         ELSIF (abs(lv_implement_qty(k) - lv_new_order_qty(k)) <= 0.000001) THEN
8128 
8129            -- There is no change in quantity at the time of release
8130            -- This is a proper date change, since we are not passing the
8131            -- details, we will not populate the start and net quantity
8132            -- in the WIP interface table.
8133            -- For unconstrained plan, should we pass qty?
8134 
8135            lv_wip_start_qty_out(k) :=  NULL;
8136            lv_net_qty(k) := NULL;
8137 
8138         ELSE
8139 
8140            -- The user changes the implement quantity at the time of release.
8141            -- We might need a factor (reverse cum yield) here to
8142            -- inflate the start quantity.
8143 
8144            lv_net_qty(k) := lv_implement_qty (k) +
8145                             lv_qty_scrapped(k) + lv_qty_completed (k);
8146 
8147            orig_net_qty := (lv_new_order_qty (k) +
8148                             lv_qty_scrapped(k) +
8149                             lv_qty_completed (k));
8150            IF (orig_net_qty <= 0.000001) THEN
8151                orig_net_qty := 1;
8152            END IF;
8153 
8154            lv_wip_start_qty_out(k) := lv_net_qty(k) *
8155                                       (lv_wip_start_qty_in(k) / orig_net_qty);
8156 
8157         END IF;
8158 
8159        End Loop;
8160 
8161     FORALL j IN 1..lv_job_count
8162     INSERT INTO msc_wip_job_schedule_interface
8163             (last_update_date,
8164             last_updated_by,
8165             last_update_login,
8166             creation_date,
8167             created_by,
8168             group_id,
8169             source_code,
8170             organization_id,
8171             organization_type,
8172             status_type,
8173             load_type,
8174             last_unit_completion_date,
8175             first_unit_start_date,
8176             bom_revision_date,
8177             routing_revision_date,
8178             job_name,
8179             firm_planned_flag,
8180             start_quantity,   -- bug 1229891: net_quantity
8181             net_quantity,
8182             wip_entity_id,
8183             demand_class,
8184             project_id,
8185             task_id,
8186 	    schedule_group_id,
8187 	    build_sequence,
8188             line_id,
8189             alternate_bom_designator,
8190 	    alternate_routing_designator,
8191 	    end_item_unit_number,
8192             process_phase,
8193 	    process_status,
8194             BILL_RTG_EXPLOSION_FLAG,
8195             HEADER_ID,
8196             SR_INSTANCE_ID,
8197             uom_code, --Outbound Changes for XML
8198             PRIMARY_ITEM_ID,
8199             source_line_id --Outbound Changes for XML
8200             , schedule_priority -- dsr
8201             , requested_completion_date -- dsr
8202             )
8203     SELECT  SYSDATE,
8204             arg_user_id,
8205             s.last_update_login,
8206             decode(tp.organization_type,2,s.creation_date,SYSDATE),
8207             arg_user_id,
8208             arg_wip_group_id,
8209             'MSC',
8210             msi.organization_id,
8211             tp.organization_type,
8212             DECODE(NVL(s.implement_status_code, s.wip_status_code),
8213                    JOB_CANCELLED,JOB_CANCELLED,NULL),
8214             decode(arg_load_type, WIP_DIS_MASS_RESCHEDULE, 3, 21), /* dsr 3 */
8215             /* BUG 14378841 (FP of BUG 14358365)
8216              * Ferring Enhancement - Post Processing Lead time for Make Items
8217              * We will pass the implement dock date as the completion date
8218              * on the work order.
8219              */
8220             /* decode(v_unconstrained_plan,
8221                       0,s.implement_date,
8222                       1,trunc(s.implement_date)+v_time_stamp), */
8223             decode(v_unconstrained_plan,
8224                    0, nvl(s.implement_dock_date,
8225                           nvl(s.new_dock_date, s.implement_date)),
8226                    1, trunc(nvl(s.implement_dock_date,
8227                                 nvl(s.new_dock_date, s.implement_date))
8228                             + v_time_stamp)),
8229             s.new_wip_Start_Date,
8230             NULL,
8231             NULL,
8232             s.implement_job_name,
8233             s.implement_firm,
8234             DECODE( tp.organization_type,
8235                     1,lv_wip_start_qty_out(j) ,
8236                     NULL),
8237             lv_net_qty(j),
8238             s.disposition_id,
8239             s.implement_demand_class,
8240             s.implement_project_id,
8241             s.implement_task_id,
8242 	    s.implement_schedule_group_id,
8243             s.implement_build_sequence,
8244             s.implement_line_id,
8245        	    DECODE( tp.organization_type, -- biogen enhancement -part2
8246                     2, to_char(mb.bill_sequence_id), -- for bug 14775879
8247                     s.implement_alternate_bom),      -- FP of bug 14770153
8248             DECODE( tp.organization_type,
8249                     2, to_char(mb.bill_sequence_id),
8250                     s.implement_alternate_routing),
8251              -- commented for biogen enhancement -part2
8252        	   /* substr(s.implement_alternate_bom,1, -- biogen enhancement-part1
8253               instr(s.implement_alternate_bom,'/')-1),
8254 	      substr(s.implement_alternate_routing,1,
8255               instr(s.implement_alternate_routing,'/')-1), */
8256 	    s.implement_unit_number,
8257             2,
8258 	    1,
8259             decode(lv_details_enabled(j),
8260                1, 'Y',
8261                decode(lv_explode_in_wip,
8262                1,'Y',
8263                'N')),  --bug 10399650
8264             s.transaction_id,
8265             s.sr_instance_id,
8266             nvl(s.implement_uom_code,msi.uom_code),
8267             msi.sr_inventory_item_id,
8268             s.transaction_id --Outbound Changes for XML
8269             , s.schedule_priority -- dsr
8270             , s.requested_completion_date -- dsr
8271     FROM    msc_trading_partners tp,
8272             msc_parameters param,
8273             msc_system_items msi,
8274             msc_supplies     s,
8275             msc_plan_organizations_v orgs,
8276             msc_boms     mb
8277     WHERE   tp.sr_tp_id= msi.organization_id
8278     AND     tp.sr_instance_id= msi.sr_instance_id
8279     AND     tp.partner_type=3
8280     AND     param.organization_id = msi.organization_id
8281     AND    param.sr_instance_id = msi.sr_instance_id
8282     AND    msi.inventory_item_id = s.inventory_item_id
8283     AND    msi.plan_id = s.plan_id
8284     AND    msi.organization_id = s.organization_id
8285     AND    msi.sr_instance_id  = s.sr_instance_id
8286     AND    s.release_errors is NULL
8287     AND    s.organization_id = orgs.planned_organization
8288     AND    s.sr_instance_id = orgs.sr_instance_id
8289     AND    s.plan_id = orgs.plan_id
8290     -- Added outer join on msc_boms as per columns in MSC_BOMS_N1 index,
8291     -- for biogen enhancement - part2
8292     AND    mb.plan_id(+) = s.plan_id
8293     AND    mb.organization_id(+) = s.organization_id
8294     AND    mb.sr_instance_id(+) = s.sr_instance_id
8295     AND    mb.assembly_item_id(+) = s.inventory_item_id
8296     AND    mb.alternate_bom_designator(+) = s.implement_alternate_bom
8297     AND    orgs.organization_id = arg_owning_org_id
8298     AND    orgs.owning_sr_instance = arg_owning_instance
8299     AND    orgs.plan_id = arg_plan_id
8300     AND    orgs.planned_organization = decode(arg_log_org_id,
8301                             arg_owning_org_id, orgs.planned_organization,
8302                             arg_log_org_id)
8303      AND    orgs.sr_instance_id = arg_org_instance
8304     --AND     ((arg_mode is null and s.load_type = WIP_DIS_MASS_RESCHEDULE) or
8305       AND     ((arg_mode is null and s.load_type = arg_load_type) or
8306                 (arg_mode = 'WF' and s.transaction_id = arg_transaction_id))
8307     AND     s.transaction_id= lv_transaction_id(j)
8308     AND     s.sr_instance_id= lv_instance_id(j);
8309 
8310     IF SQL%ROWCOUNT > 0
8311     THEN
8312         lv_resched_jobs := SQL%ROWCOUNT;
8313 
8314     ELSE
8315         lv_resched_jobs := 0;
8316 
8317     END IF;
8318 
8319     -- ------------------------------------------------------------------------
8320     -- Perform the wip discrete job mass resched for the details
8321     -- ------------------------------------------------------------------------
8322 
8323     -- the details are populated, only if the implement date and quantity
8324     -- are the same as the planned date and quantity
8325 
8326     /* If the current plan is DRP plan then check the following conditions
8327        to determin whether we need to release the details or not
8328        1) For planned orders that do not use
8329           substitute components/alternateboms, only the
8330           header level information is released */
8331 
8332     l_count := 1;
8333 
8334     OPEN c_plan_type(arg_plan_id);
8335     FETCH c_plan_type INTO l_plan_type;
8336     CLOSE c_plan_type;
8337 
8338     IF l_plan_type = 5 THEN   -- DRP plan
8339        OPEN  c_release_dtls(arg_transaction_id, arg_plan_id);
8340        FETCH c_release_dtls INTO l_count;
8341        CLOSE c_release_dtls;
8342     END IF;
8343 
8344     /* If it is not a drp plan then l_count = 1 by default so details will be released */
8345     IF l_count = 1 THEN  /* Release details as well */
8346        -- OPERATIONS
8347        FORALL j IN 1..lv_job_count
8348        INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
8349            (last_update_date,
8350             last_updated_by,
8351             last_update_login,
8352             creation_date,
8353             created_by,
8354             organization_type,
8355             organization_id,
8356             group_id,
8357             parent_header_id,
8358             operation_seq_num,
8359             department_id,
8360             description,
8361             standard_operation_id,
8362             first_unit_start_date,
8363             first_unit_completion_date,
8364             last_unit_start_date,
8365             last_unit_completion_date,
8366             minimum_transfer_quantity,
8367             count_point_type,
8368             backflush_flag,
8369             SUBSTITUTION_TYPE,
8370             LOAD_TYPE,
8371 	    process_phase,
8372 	    process_status,
8373             SR_INSTANCE_ID,
8374             operation_seq_id, --Outbound Changes for XML
8375             WIP_ENTITY_ID
8376             , eam_flag -- dsr
8377             )
8378     SELECT  SYSDATE,
8379             arg_user_id,
8380             s.last_update_login,
8381             SYSDATE,
8382             arg_user_id,
8383             tp.organization_type,
8384             s.organization_id,
8385             arg_wip_group_id,
8386             s.transaction_id,
8387             resreq.OPERATION_SEQ_NUM,
8388             NULL,            --department_id,
8389             NULL,   --description,
8390             NULL,   --standard_operation_id,
8391             min(resreq.START_DATE),   --first_unit_start_date,
8392             min(resreq.START_DATE),   --first_unit_completion_date,
8393             max(resreq.END_DATE),     --last_unit_start_date,
8394             max(resreq.END_DATE),     --last_unit_completion_date,
8395             NULL,   --minimum_transfer_quantity,
8396             NULL,   --count_point_type,
8397             NULL,   --backflush_flag,
8398             SUBST_CHANGE,
8399             LT_OPERATION,
8400             2,
8401             1,
8402             s.sr_instance_id,
8403             resreq.operation_sequence_id, --Outbound Changes for XML
8404             s.disposition_id
8405             , decode(arg_load_type, EAM_RESCHEDULE_WORK_ORDER, SYS_YES, SYS_NO) -- dsr
8406       FROM  msc_trading_partners   tp,
8407             msc_resource_requirements resreq,
8408             msc_parameters          param,
8409             msc_system_items        msi,
8410             msc_supplies            s
8411     WHERE   tp.sr_tp_id= msi.organization_id
8412     AND     tp.sr_instance_id= msi.sr_instance_id
8413     AND     tp.partner_type=3
8414     AND     resreq.sr_instance_id= s.sr_instance_id
8415     AND     resreq.organization_id= s.organization_id
8416     AND     resreq.supply_id = s.transaction_id
8417     AND     resreq.plan_id   = s.plan_id
8418     AND     resreq.parent_id   = 2
8419     AND     param.organization_id = msi.organization_id
8420     AND     param.sr_instance_id  = msi.sr_instance_id
8421     AND     msi.inventory_item_id = s.inventory_item_id
8422     AND     msi.plan_id = s.plan_id
8423     AND     msi.organization_id = s.organization_id
8424     AND     msi.sr_instance_id = s.sr_instance_id
8425     AND     s.transaction_id= lv_transaction_id(j)
8426     AND     s.sr_instance_id= lv_instance_id(j)
8427     AND     s.plan_id= arg_plan_id
8428     AND     lv_details_enabled(j)= 1
8429     AND     lv_agg_details(j) = 1
8430     GROUP BY
8431             SYSDATE,
8432             arg_user_id,
8433             tp.organization_type,
8434             s.organization_id,
8435             s.last_update_login,
8436             SYSDATE,
8437             arg_user_id,
8438             arg_wip_group_id,
8439             s.transaction_id,
8440             resreq.OPERATION_SEQ_NUM,
8441             NULL,            --department_id,
8442             LT_OPERATION,    --load_type,
8443             NULL,            --description,
8444             NULL,            --standard_operation_id,
8445             NULL,            --minimum_transfer_quantity,
8446             NULL,            --count_point_type,
8447             NULL,            --backflush_flag,
8448             s.sr_instance_id,
8449             resreq.operation_sequence_id,
8450             s.disposition_id;
8451 
8452 
8453     --DBMS_OUTPUT.PUT_LINE('OPERATION_RESOURCE');
8454 
8455     -- OPERATION RESOURCES
8456     FORALL j IN 1..lv_job_count
8457     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
8458           ( last_update_date,
8459             last_updated_by,
8460             last_update_login,
8461             creation_date,
8462             created_by,
8463             organization_type,
8464             organization_id,
8465             group_id,
8466             parent_header_id,
8467             operation_seq_num,
8468             -- resource_seq_num,
8469             alternate_num,
8470             resource_id_old,
8471             resource_id_new,
8472             usage_rate_or_amount,
8473             scheduled_flag,
8474             applied_resource_units,   --
8475             applied_resource_value,   --
8476             uom_code,
8477             basis_type,     --
8478             activity_id,    --
8479             autocharge_type,     --
8480             standard_rate_flag,  --
8481             start_date,
8482             completion_date,
8483             assigned_units,
8484             SUBSTITUTION_TYPE,
8485             LOAD_TYPE,
8486 	    process_phase,
8487 	    process_status,
8488             description,
8489             SR_INSTANCE_ID,
8490             operation_seq_id, -- Outbound Changes for XML
8491             wip_entity_id,
8492             resource_hours,
8493             department_id
8494             -- dsr: following 9 columns
8495             , firm_flag
8496             , setup_id
8497             , group_sequence_id
8498             , group_sequence_number
8499             , batch_id
8500             , maximum_assigned_units
8501             , parent_seq_num
8502             , eam_flag
8503             , resource_seq_num
8504             , schedule_seq_num
8505 	  )
8506     SELECT  SYSDATE,
8507             arg_user_id,
8508             s.last_update_login,
8509             SYSDATE,
8510             arg_user_id,
8511             tp.organization_type,
8512             s.organization_id,
8513             arg_wip_group_id,
8514             s.transaction_id,
8515             resreq.OPERATION_SEQ_NUM,
8516             -- resreq.RESOURCE_SEQ_NUM,
8517             resreq.ALTERNATE_NUM,
8518             resreq.RESOURCE_ID,
8519             resreq.RESOURCE_ID,
8520            decode(resreq.parent_seq_num, null,
8521                     decode(l_apps_ver,
8522                           '6',to_number(null),
8523                           '5',to_number(null), -- BUG 13624336 (FP of BUG 13500698)
8524                           '4',to_number(null),
8525                           '3',TO_NUMBER(NULL),
8526                            resreq.RESOURCE_HOURS/decode(resreq.basis_type,2,1,
8527                            decode(s.new_order_quantity,0,1,s.new_order_quantity))), resreq.usage_rate),
8528             decode(nvl(resreq.schedule_flag,1),-23453,1,1,1,resreq.schedule_flag),
8529             NULL,
8530             NULL,
8531             v_hour_uom,
8532             resreq.basis_type,
8533             NULL,
8534             NULL,
8535             NULL,
8536             nvl(resreq.firm_start_date,resreq.START_DATE),
8537             nvl(resreq.firm_end_date,resreq.END_DATE),
8538             -- decode(l_apps_ver,'3',TO_NUMBER(NULL),resreq.ASSIGNED_UNITS),
8539             resreq.ASSIGNED_UNITS,
8540             decode(resreq.parent_seq_num,null,SUBST_CHANGE,SUBST_ADD),
8541             -- SUBST_CHANGE,
8542             LT_RESOURCE,
8543             2,
8544             1,
8545             NULL,
8546             s.sr_instance_id,
8547             resreq.operation_sequence_id, -- Outbound Changes for XML
8548             s.disposition_id,
8549             resreq.resource_hours,
8550             resreq.department_id
8551              -- dsr: following 9 columns
8552             , resreq.firm_flag
8553             , resreq.setup_id
8554             , resreq.group_sequence_id
8555             , resreq.group_sequence_number
8556             , resreq.batch_number
8557             , resreq.maximum_assigned_units
8558             , resreq.parent_seq_num
8559             , DECODE(arg_load_type, EAM_RESCHEDULE_WORK_ORDER, SYS_YES, SYS_NO) -- dsr: eam_flag
8560             , resreq.orig_resource_seq_num
8561             , resreq.resource_seq_num
8562       FROM  msc_trading_partners   tp,
8563             msc_resource_requirements resreq,
8564             msc_parameters          param,
8565             msc_system_items        msi,
8566             msc_supplies            s
8567     WHERE   tp.sr_tp_id= msi.organization_id
8568     AND     tp.sr_instance_id= msi.sr_instance_id
8569     AND     tp.partner_type=3
8570     AND     resreq.sr_instance_id= s.sr_instance_id
8571     AND     resreq.organization_id= s.organization_id
8572     AND     resreq.supply_id = s.transaction_id
8573     AND     resreq.plan_id   = s.plan_id
8574     AND     resreq.parent_id   = 2
8575     AND     param.organization_id = msi.organization_id
8576     AND     param.sr_instance_id  = msi.sr_instance_id
8577     AND     msi.inventory_item_id = s.inventory_item_id
8578     AND     msi.plan_id = s.plan_id
8579     AND     msi.organization_id = s.organization_id
8580     AND     msi.sr_instance_id = s.sr_instance_id
8581     AND     s.transaction_id= lv_transaction_id(j)
8582     AND     s.sr_instance_id= lv_instance_id(j)
8583     AND     s.plan_id= arg_plan_id
8584     AND     lv_details_enabled(j)= 1
8585     AND     lv_agg_details(j) = 1 ;
8586 
8587 
8588     --DBMS_OUTPUT.PUT_LINE('LOAD COMPONENTS');
8589 
8590     /* UPDATE EXISTING COMPONENTS                      *
8591      |    We should set inventory_item_id_new to NULL  |
8592      *                                                 */
8593     FORALL j IN 1..lv_job_count
8594     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
8595             (last_update_date,
8596             last_updated_by,
8597             last_update_login,
8598             creation_date,
8599             created_by,
8600             organization_type,
8601             organization_id,
8602             group_id,
8603             parent_header_id,
8604             operation_seq_num,
8605             inventory_item_id_old,
8606             inventory_item_id_new,
8607             basis_type,
8608             quantity_per_assembly,
8609             component_yield_factor,
8610             department_id,
8611             wip_supply_type,
8612             date_required,
8613             required_quantity,
8614             quantity_issued,
8615             supply_subinventory,
8616             supply_locator_id,
8617             mrp_net_flag,
8618             mps_required_quantity,
8619             mps_date_required,
8620             SUBSTITUTION_TYPE,
8621             LOAD_TYPE,
8622 	    process_phase,
8623 	    process_status,
8624             description,
8625             SR_INSTANCE_ID,
8626 --            operation_seq_id, -- Outbound Changes for XML
8627             uom_code, --Outbound Changes for XML
8628             wip_entity_id,
8629              eam_flag -- dsr
8630             )
8631     SELECT  SYSDATE,
8632             arg_user_id,
8633             s.last_update_login,
8634             SYSDATE,
8635             arg_user_id,
8636             tp.organization_type,
8637             s.organization_id,
8638             arg_wip_group_id,
8639             s.transaction_id,
8640             nvl(md.op_seq_num,1),
8641             icomp.sr_inventory_item_id,
8642             decode(l_apps_ver,
8643                    '6',to_number(null),
8644                    '5',to_number(null),
8645                    '4',to_number(null),
8646                    '3',null,
8647                    icomp.sr_inventory_item_id), -- BUG 13624336 (FP of BUG 13500698)
8648             decode(md.component_scaling_type,1,NULL,md.component_scaling_type),
8649             decode(l_apps_ver,
8650                    '6',TO_NUMBER(NULL),
8651                    '5',TO_NUMBER(NULL),
8652                    '4',TO_NUMBER(NULL),
8653                    '3',TO_NUMBER(NULL),
8654                    (md.USING_REQUIREMENT_QUANTITY/s.implement_quantity)), -- BUG 13624336(FP of BUG 13500698)
8655             md.component_yield_factor,
8656             TO_NUMBER(NULL),       --Department_ID
8657             NVL(GET_WIP_SUPPLY_TYPE(s.plan_id, s.sr_instance_id,s.process_seq_id,
8658                                     s.inventory_item_id,md.inventory_item_id,s.organization_id),
8659                     icomp.wip_supply_type),
8660             md.USING_ASSEMBLY_DEMAND_DATE,
8661             decode(l_apps_ver,
8662                    '6',TO_NUMBER(NULL),
8663                    '5',TO_NUMBER(NULL),
8664                    '4',TO_NUMBER(NULL),
8665                    '3',TO_NUMBER(NULL),
8666                    md.USING_REQUIREMENT_QUANTITY), -- BUG 13624336 (FP of BUG 13500698)
8667             TO_NUMBER(NULL),  --quantity_issued
8668             TO_CHAR(NULL),     -- Sub Inventory
8669             TO_NUMBER(NULL),   -- Locator ID
8670             1,                 -- MRP_NET_FLAG
8671             decode(l_apps_ver,
8672                    '6',to_number(null),
8673                    '5',to_number(null),
8674                    '4',to_number(null),
8675                    '3',TO_NUMBER(NULL),
8676                    md.USING_REQUIREMENT_QUANTITY),  -- BUG 13624336 (FP of BUG 13500698)
8677             md.USING_ASSEMBLY_DEMAND_DATE,
8678             SUBST_CHANGE,
8679             LT_COMPONENT,
8680             2,
8681             1,
8682             TO_CHAR(NULL),
8683             s.sr_instance_id,
8684 --            md.operation_seq_id,
8685             nvl(s.implement_uom_code, msi.uom_code),
8686             s.disposition_id
8687             , DECODE(arg_load_type, EAM_RESCHEDULE_WORK_ORDER, SYS_YES, SYS_NO) -- dsr: eam_flag
8688       FROM  msc_trading_partners    tp,
8689             msc_system_items        icomp,
8690             msc_demands             md,
8691             msc_parameters          param,
8692             msc_system_items        msi,
8693             msc_supplies            s
8694     WHERE   /*
8695             ( md.SUBST_ITEM_FLAG <> 1
8696               OR md.SUBST_ITEM_FLAG IS NULL) */
8697             not exists (select 'x'
8698                         from msc_exception_details excp
8699                         where excp.plan_id = msi.plan_id
8700                         and excp.number1 = s.transaction_id
8701                         and excp.inventory_item_id = msi.inventory_item_id
8702                         and excp.organization_id = msi.organization_id
8703                         and excp.sr_instance_id = msi.sr_instance_id
8704                         and excp.exception_type = 33
8705                         and excp.number2 = md.inventory_item_id)
8706     AND     tp.sr_tp_id= msi.organization_id
8707     AND     tp.sr_instance_id= msi.sr_instance_id
8708     AND     tp.partner_type=3
8709     AND     icomp.inventory_item_id= md.inventory_item_id
8710     AND     icomp.organization_id= md.organization_id
8711     AND     icomp.sr_instance_id= md.sr_instance_id
8712     AND     icomp.plan_id= md.plan_id
8713     AND     nvl(icomp.wip_supply_type,0) <> 6
8714     AND     md.sr_instance_id= s.sr_instance_id
8715     AND     md.disposition_id= s.transaction_id
8716     AND     md.plan_id= s.plan_id
8717     AND     param.organization_id = msi.organization_id
8718     AND     param.sr_instance_id  = msi.sr_instance_id
8719     AND     msi.inventory_item_id = s.inventory_item_id
8720     AND     msi.plan_id = s.plan_id
8721     AND     msi.organization_id = s.organization_id
8722     AND     msi.sr_instance_id = s.sr_instance_id
8723     AND     s.transaction_id= lv_transaction_id(j)
8724     AND     s.sr_instance_id= lv_instance_id(j)
8725     AND     s.plan_id= arg_plan_id
8726     AND     lv_details_enabled(j)= 1
8727     AND     lv_agg_details(j) = 1;
8728 
8729 
8730     --Load Co-product/by-product component details
8731 
8732     FORALL j IN 1..lv_job_count
8733     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
8734             (last_update_date,
8735             last_updated_by,
8736             last_update_login,
8737             creation_date,
8738             created_by,
8739             organization_type,
8740             organization_id,
8741             group_id,
8742             parent_header_id,
8743             operation_seq_num,
8744             inventory_item_id_old,
8745             inventory_item_id_new,
8746             quantity_per_assembly,
8747             department_id,
8748             wip_supply_type,
8749             date_required,
8750             required_quantity,
8751             quantity_issued,
8752             supply_subinventory,
8753             supply_locator_id,
8754             mrp_net_flag,
8755             mps_required_quantity,
8756             mps_date_required,
8757             SUBSTITUTION_TYPE,
8758             LOAD_TYPE,
8759 	    process_phase,
8760 	    process_status,
8761             description,
8762             SR_INSTANCE_ID,
8763 --            operation_seq_id, -- Outbound Changes for XML
8764             uom_code, --Outbound Changes for XML
8765             wip_entity_id
8766             , eam_flag -- dsr
8767             )
8768     SELECT  SYSDATE,
8769             arg_user_id,
8770             s.last_update_login,
8771             SYSDATE,
8772             arg_user_id,
8773             tp.organization_type,
8774             s.organization_id,
8775             arg_wip_group_id,
8776             s.transaction_id,
8777             nvl(co.operation_seq_num,1),
8778             icomp.sr_inventory_item_id,
8779             decode(l_apps_ver,
8780                    '6',to_number(null),
8781                    '5',to_number(null),
8782                    '4',to_number(null),
8783                    '3',null,
8784                    icomp.sr_inventory_item_id),   -- BUG 13624336 (FP of BUG 13500698)
8785             decode(l_apps_ver,
8786                    '6',to_number(null),
8787                    '5',to_number(null),
8788                    '4',to_number(null),
8789                    '3',TO_NUMBER(NULL),
8790                    (co.new_order_quantity/s.implement_quantity)), -- BUG 13624336 (FP of BUG 13500698)
8791             TO_NUMBER(NULL),       --Department_ID
8792             NVL(GET_WIP_SUPPLY_TYPE(s.plan_id, s.sr_instance_id,s.process_seq_id,
8793                                     s.inventory_item_id,co.inventory_item_id,s.organization_id),
8794                     icomp.wip_supply_type),
8795             co.new_schedule_date,
8796             decode(l_apps_ver,
8797                    '6',to_number(null),
8798                    '5',to_number(null),
8799                    '4',to_number(null),
8800                    '3',TO_NUMBER(NULL),
8801                    co.new_order_quantity),  -- BUG 13624336 (FP of BUG 13500698)
8802             TO_NUMBER(NULL),  --quantity_issued
8803             TO_CHAR(NULL),     -- Sub Inventory
8804             TO_NUMBER(NULL),   -- Locator ID
8805             1,                 -- MRP_NET_FLAG
8806             decode(l_apps_ver,
8807                    '6',to_number(null),
8808                    '5',to_number(null),
8809                    '4',to_number(null),
8810                    '3',TO_NUMBER(NULL),
8811                    co.new_order_quantity),  -- BUG 13624336 (FP of BUG 13500698)
8812             co.new_schedule_date,
8813             SUBST_CHANGE,
8814             LT_COMPONENT,
8815             2,
8816             1,
8817             TO_CHAR(NULL),
8818             s.sr_instance_id,
8819             nvl(s.implement_uom_code, msi.uom_code),
8820             s.disposition_id
8821              , DECODE(arg_load_type, EAM_RESCHEDULE_WORK_ORDER, SYS_YES, SYS_NO) -- dsr: eam_flag
8822       FROM  msc_trading_partners    tp,
8823             msc_system_items        icomp,
8824             msc_supplies            co,
8825             msc_parameters          param,
8826             msc_system_items        msi,
8827             msc_supplies            s
8828     WHERE   /*not exists (select 'x'
8829                         from msc_exception_details excp
8830                         where excp.plan_id = msi.plan_id
8831                         and excp.number1 = s.transaction_id
8832                         and excp.inventory_item_id = msi.inventory_item_id
8833                         and excp.organization_id = msi.organization_id
8834                         and excp.sr_instance_id = msi.sr_instance_id
8835                         and excp.exception_type = 33
8836                         and excp.number2 = co.inventory_item_id)
8837     AND     */
8838             tp.sr_tp_id             = msi.organization_id
8839     AND     tp.sr_instance_id       = msi.sr_instance_id
8840     AND     tp.partner_type         = 3
8841     AND     icomp.inventory_item_id = co.inventory_item_id
8842     AND     icomp.organization_id   = co.organization_id
8843     AND     icomp.sr_instance_id    = co.sr_instance_id
8844     AND     icomp.plan_id           = co.plan_id
8845     AND     co.sr_instance_id       = s.sr_instance_id
8846     AND     co.disposition_id       = s.transaction_id
8847     AND     co.plan_id              = s.plan_id
8848     AND     co.order_type           = 14 -- Discrete Job Co-products/by-products.
8849     AND     param.organization_id   = msi.organization_id
8850     AND     param.sr_instance_id    = msi.sr_instance_id
8851     AND     msi.inventory_item_id   = s.inventory_item_id
8852     AND     msi.plan_id             = s.plan_id
8853     AND     msi.organization_id     = s.organization_id
8854     AND     msi.sr_instance_id      = s.sr_instance_id
8855     AND     s.transaction_id        = lv_transaction_id(j)
8856     AND     s.sr_instance_id        = lv_instance_id(j)
8857     AND     s.plan_id               = arg_plan_id
8858     AND     lv_details_enabled(j)   = 1
8859     AND     lv_agg_details(j)       = 1
8860     AND     tp.organization_type    = 2;
8861 
8862 
8863     --DBMS_OUTPUT.PUT_LINE('RESOURCE_USAGE');
8864 
8865     -- RESOURCE USAGE
8866     FORALL j IN 1..lv_job_count
8867     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
8868           ( last_update_date,
8869             last_updated_by,
8870             last_update_login,
8871             creation_date,
8872             created_by,
8873             organization_type,
8874             organization_id,
8875             group_id,
8876             parent_header_id,
8877             operation_seq_num,
8878             -- resource_seq_num,
8879             alternate_num,
8880             resource_id_old,
8881             resource_id_new,
8882             usage_rate_or_amount,
8883             scheduled_flag,
8884             applied_resource_units,   --
8885             applied_resource_value,   --
8886             uom_code,
8887             basis_type,     --
8888             activity_id,    --
8889             autocharge_type,     --
8890             standard_rate_flag,  --
8891             start_date,
8892             completion_date,
8893             assigned_units,
8894             SUBSTITUTION_TYPE,
8895             LOAD_TYPE,
8896 	    process_phase,
8897 	    process_status,
8898             description,
8899             SR_INSTANCE_ID,
8900             operation_seq_id, -- Outbound Changes for XML
8901             wip_entity_id,
8902             department_id,
8903             resource_hours
8904             , eam_flag -- dsr
8905             , resource_seq_num
8906             , schedule_seq_num
8907             ,parent_seq_num
8908             )
8909     SELECT  SYSDATE,
8910             arg_user_id,
8911             s.last_update_login,
8912             SYSDATE,
8913             arg_user_id,
8914             tp.organization_type,
8915             s.organization_id,
8916             arg_wip_group_id,
8917             s.transaction_id,
8918             resreq.OPERATION_SEQ_NUM,
8919            -- resreq.RESOURCE_SEQ_NUM,
8920             resreq.ALTERNATE_NUM,
8921             resreq.RESOURCE_ID,
8922             resreq.RESOURCE_ID,
8923             decode(resreq.parent_seq_num, null,
8924                   decode(l_apps_ver,
8925                           '6',to_number(null),
8926                           '5',to_number(null), -- BUG 13624336 (FP of BUG 13500698)
8927                           '4',to_number(null),
8928                           '3',TO_NUMBER(NULL),
8929                            resreq.RESOURCE_HOURS/decode(resreq.basis_type,2,1,
8930                            decode(s.new_order_quantity,0,1,s.new_order_quantity))), resreq.usage_rate),
8931             decode(nvl(resreq.schedule_flag,1),-23453,1,1,1,resreq.schedule_flag),
8932             NULL,
8933             NULL,
8934             v_hour_uom,
8935             resreq.basis_type,
8936             NULL,
8937             NULL,
8938             NULL,
8939             nvl(resreq.firm_start_date,resreq.START_DATE),
8940             nvl(resreq.firm_end_date,resreq.END_DATE),
8941             -- decode(l_apps_ver,'3',TO_NUMBER(NULL),resreq.ASSIGNED_UNITS),
8942             resreq.ASSIGNED_UNITS,
8943             SUBST_CHANGE,
8944             LT_RESOURCE_USAGE,
8945             2,
8946             1,
8947             NULL,
8948             s.sr_instance_id,
8949             resreq.operation_sequence_id, -- Outbound Changes for XML
8950             s.disposition_id,
8951             resreq.department_id,
8952             resreq.resource_hours
8953             , DECODE(arg_load_type, EAM_RESCHEDULE_WORK_ORDER, SYS_YES, SYS_NO) -- dsr: eam_flag
8954             , resreq.orig_resource_seq_num
8955             , resreq.resource_seq_num
8956             ,resreq.parent_seq_num
8957       FROM  msc_trading_partners   tp,
8958             msc_resource_requirements resreq,
8959             msc_parameters          param,
8960             msc_system_items        msi,
8961             msc_supplies            s
8962     WHERE   tp.sr_tp_id= msi.organization_id
8963     AND     tp.sr_instance_id= msi.sr_instance_id
8964     AND     tp.partner_type=3
8965     AND     resreq.sr_instance_id= s.sr_instance_id
8966     AND     resreq.organization_id= s.organization_id
8967     AND     resreq.supply_id = s.transaction_id
8968     AND     resreq.plan_id   = s.plan_id
8969     AND     resreq.parent_id   = 1
8970     AND     param.organization_id = msi.organization_id
8971     AND     param.sr_instance_id  = msi.sr_instance_id
8972     AND     msi.inventory_item_id = s.inventory_item_id
8973     AND     msi.plan_id = s.plan_id
8974     AND     msi.organization_id = s.organization_id
8975     AND     msi.sr_instance_id = s.sr_instance_id
8976     AND     s.transaction_id= lv_transaction_id(j)
8977     AND     s.sr_instance_id= lv_instance_id(j)
8978     AND     s.plan_id= arg_plan_id
8979     AND     lv_details_enabled(j)= 1
8980     AND     lv_agg_details(j) = 1
8981      AND     tp.organization_type IN (1, 2);  -- 1 - discrete wip org; 2 - opm org
8982 
8983     -- dsr starts here
8984 --    print_debug_info('OPERATION RESOURCE_INSTANCES');
8985 	-- OPERATION RESOURCE_INSTANCES
8986 
8987     FORALL j IN 1..lv_job_count
8988     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
8989           ( last_update_date,
8990             last_updated_by,
8991             last_update_login,
8992             creation_date,
8993             created_by,
8994             organization_type,
8995             organization_id,
8996             group_id,
8997             parent_header_id,
8998             operation_seq_num,
8999            -- resource_seq_num,
9000             resource_id_old, -- rawasthi
9001 	    resource_id_new, -- xx resource_id, ???
9002             RESOURCE_INSTANCE_ID,
9003             start_date,
9004             completion_date,
9005             SUBSTITUTION_TYPE,
9006             LOAD_TYPE,
9007             process_phase,
9008             process_status,
9009             SR_INSTANCE_ID,
9010             operation_seq_id,
9011             -- FIRM_FLAG,
9012             resource_hours,
9013             department_id,
9014 	    -- setup_id,
9015 	    SERIAL_NUMBER,
9016             group_sequence_id,
9017             group_sequence_number,
9018             batch_id,
9019             resource_seq_num
9020             , schedule_seq_num
9021             , wip_entity_id -- for reschedule
9022             , eam_flag -- dsr
9023             , parent_seq_num
9024  )
9025     SELECT
9026           SYSDATE,
9027           arg_user_id,
9028           s.last_update_login,
9029           SYSDATE,
9030           arg_user_id,
9031           tp.organization_type,
9032           s.organization_id,
9033           arg_wip_group_id,
9034           s.transaction_id,
9035           resreq.OPERATION_SEQ_NUM,
9036        --   resreq.RESOURCE_SEQ_NUM,
9037           resreq.resource_id,
9038           resreq.resource_id,
9039   -- jguo dep_res_inst.RESOURCE_INST_ID,
9040           dep_res_inst.RES_INSTANCE_ID ,
9041           nvl(resreq.firm_start_date,res_instreq.START_DATE),
9042           nvl(resreq.firm_end_date,res_instreq.END_DATE),
9043           SUBST_ADD, -- jguo SUBST_CHANGE,
9044           LT_RESOURCE_INSTANCE,
9045           2,
9046           1,
9047           s.sr_instance_id,
9048           resreq.operation_sequence_id,
9049        -- res_instreq.firm_flag,
9050           resreq.resource_hours,
9051           resreq.department_id,
9052          -- resreq.department_id / 2, rawasthi
9053        -- res_instreq.setup_id,
9054           dep_res_inst.serial_number,
9055           resreq.group_sequence_id,
9056           resreq.group_sequence_number,
9057           res_instreq.batch_number,    --- sbala res_instreq.res_inst_batch_id,
9058           resreq.orig_resource_seq_num,
9059           resreq.resource_seq_num
9060       --  resreq.alternate_num,
9061         , s.disposition_id -- for reschedule
9062         , DECODE(arg_load_type, EAM_RESCHEDULE_WORK_ORDER, SYS_YES, SYS_NO) -- dsr: eam_flag
9063         , resreq.parent_seq_num
9064    FROM
9065           msc_trading_partners   tp,
9066           msc_resource_requirements resreq,
9067           msc_resource_instance_reqs res_instreq,
9068           msc_dept_res_instances dep_res_inst,
9069           msc_supplies            s
9070     WHERE
9071          tp.sr_tp_id=s.organization_id
9072  AND     tp.sr_instance_id= s.sr_instance_id
9073  AND     tp.partner_type=3
9074  AND     resreq.sr_instance_id= s.sr_instance_id
9075  AND     resreq.organization_id= s.organization_id
9076  AND     resreq.supply_id = s.transaction_id
9077  AND     resreq.plan_id   = s.plan_id
9078 -- dsr AND     resreq.transaction_id = res_instreq.res_inst_transaction_id
9079  AND     resreq.resource_seq_num = res_instreq.resource_seq_num
9080  AND     resreq.operation_seq_num = res_instreq.operation_seq_num
9081  AND     resreq.resource_id = res_instreq.resource_id
9082  AND     resreq.supply_id = res_instreq.supply_id
9083  AND     resreq.sr_instance_id = res_instreq.sr_instance_id
9084  AND     resreq.plan_id = res_instreq.plan_id
9085  AND     resreq.parent_id = res_instreq.parent_id  --rawasthi
9086  AND     resreq.start_date = res_instreq.start_date
9087  AND     resreq.parent_id   = 2
9088  AND     resreq.resource_id <> -1
9089  AND     resreq.department_id <> -1
9090 -- dsr   AND     res_instreq.res_instance_id = dep_res_inst.dept_resource_inst_id
9091  AND     res_instreq.plan_id = dep_res_inst.plan_id
9092  AND     res_instreq.sr_instance_id = dep_res_inst.sr_instance_id
9093  AND     res_instreq.department_id = dep_res_inst.department_id
9094  AND     res_instreq.resource_id = dep_res_inst.resource_id
9095 /* anuj: serail number and resource_instance id joins */
9096  AND     res_instreq.serial_number = dep_res_inst.serial_number
9097  AND     res_instreq.res_instance_id = dep_res_inst.res_instance_id
9098  AND     s.transaction_id= lv_transaction_id(j)
9099  AND     s.sr_instance_id= lv_instance_id(j)
9100  AND     s.plan_id= arg_plan_id
9101  AND     lv_details_enabled(j)= 1
9102  AND     lv_agg_details(j) = 1
9103     ;
9104 
9105 --print_debug_info('reschedule_wip_discrete_jobs: 888 sql%rowcount = '|| SQL%ROWCOUNT);
9106 
9107 
9108     -- RESOURCE INSTANCE USAGES
9109 --print_debug_info('RESOURCE INSTANCE USAGES');
9110 	FORALL j IN 1..lv_job_count
9111     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
9112           ( last_update_date,
9113             last_updated_by,
9114             last_update_login,
9115             creation_date,
9116             created_by,
9117             organization_type,
9118             organization_id,
9119             group_id,
9120             parent_header_id,
9121             operation_seq_num,
9122            -- resource_seq_num,
9123             resource_id_old, -- rawasthi
9124 	    resource_id_new, -- xx RESOURCE_ID, ???
9125             RESOURCE_INSTANCE_ID,
9126             start_date,
9127             completion_date,
9128             SUBSTITUTION_TYPE,
9129             LOAD_TYPE,
9130             process_phase,
9131             process_status,
9132             SR_INSTANCE_ID,
9133             operation_seq_id,
9134             FIRM_FLAG,
9135             resource_hours,
9136             department_id,
9137             serial_number,
9138             resource_seq_num,
9139             schedule_seq_num
9140          -- alternate_num
9141      	  , wip_entity_id -- for reschedule
9142 	  , eam_flag -- dsr
9143 	  , assigned_units -- jguo
9144 	  ,parent_seq_num
9145  )
9146     SELECT
9147            SYSDATE,
9148            arg_user_id,
9149            s.last_update_login,
9150            SYSDATE,
9151            arg_user_id,
9152            tp.organization_type,
9153            s.organization_id,
9154            arg_wip_group_id,
9155            s.transaction_id,
9156            resreq.OPERATION_SEQ_NUM,
9157        --    resreq.RESOURCE_SEQ_NUM,
9158            resreq.RESOURCE_ID,
9159            resreq.RESOURCE_ID,
9160   -- jguo  dep_res_inst.RESOURCE_INST_ID,
9161            dep_res_inst.RES_INSTANCE_ID ,
9162            nvl(resreq.firm_start_date,res_instreq.START_DATE),
9163            nvl(resreq.firm_end_date,res_instreq.END_DATE),
9164            SUBST_ADD, -- jguo SUBST_CHANGE,
9165            LT_RESOURCE_INST_USAGE,
9166            2,
9167            1,
9168            s.sr_instance_id,
9169            resreq.operation_sequence_id,
9170            resreq.firm_flag,
9171            res_instreq.resource_instance_hours,
9172            resreq.department_id,
9173           -- resreq.department_id / 2, rawasthi
9174            dep_res_inst.serial_number,
9175            resreq.orig_resource_seq_num,
9176            resreq.resource_seq_num
9177         -- resreq.alternate_num
9178          , s.disposition_id -- for reschedule
9179          , DECODE(arg_load_type, EAM_RESCHEDULE_WORK_ORDER, SYS_YES, SYS_NO) -- dsr: eam_flag
9180          , 1 -- jguo
9181          ,resreq.parent_seq_num
9182     FROM
9183            msc_trading_partners   tp,
9184            msc_resource_requirements resreq,
9185            msc_resource_instance_reqs res_instreq, -- ??? msc_res_inst_requirements res_instreq,
9186            msc_dept_res_instances dep_res_inst,
9187            msc_supplies            s
9188     WHERE
9189             tp.sr_tp_id=s.organization_id
9190     AND     tp.sr_instance_id= s.sr_instance_id
9191     AND     tp.partner_type=3
9192     AND     resreq.sr_instance_id= s.sr_instance_id
9193     AND     resreq.organization_id= s.organization_id
9194     AND     resreq.supply_id = s.transaction_id
9195     AND     resreq.plan_id   = s.plan_id
9196 -- dsr   AND     resreq.transaction_id = res_instreq.res_inst_transaction_id
9197     AND     resreq.resource_seq_num = res_instreq.resource_seq_num
9198     AND     resreq.operation_seq_num = res_instreq.operation_seq_num
9199     AND     resreq.resource_id = res_instreq.resource_id
9200     AND     resreq.supply_id = res_instreq.supply_id
9201     AND     resreq.sr_instance_id = res_instreq.sr_instance_id
9202     AND     resreq.plan_id = res_instreq.plan_id
9203     AND     resreq.parent_id = res_instreq.parent_id  --rawasthi
9204     AND     resreq.start_date = res_instreq.start_date
9205     AND     resreq.parent_id   = 1
9206     AND     resreq.resource_id <> -1
9207     AND     resreq.department_id <> -1
9208 -- dsr   AND     res_instreq.res_instance_id = dep_res_inst.dept_resource_inst_id
9209     AND     res_instreq.department_id = dep_res_inst.department_id
9210     AND     res_instreq.resource_id = dep_res_inst.resource_id
9211     AND     res_instreq.plan_id = dep_res_inst.plan_id
9212     AND     res_instreq.sr_instance_id = dep_res_inst.sr_instance_id
9213 /* anuj: serail number and resource_instance id joins */
9214     AND     res_instreq.serial_number = dep_res_inst.serial_number
9215     AND     res_instreq.res_instance_id = dep_res_inst.res_instance_id
9216     AND     s.transaction_id= lv_transaction_id(j)
9217     AND     s.sr_instance_id= lv_instance_id(j)
9218     AND     s.plan_id= arg_plan_id
9219     AND     lv_details_enabled(j)= 1
9220     AND     lv_agg_details(j) = 1
9221     ;
9222 
9223 --print_debug_info('reschedule_wip_discrete_jobs: 999 sql%rowcount = '|| SQL%ROWCOUNT);
9224 
9225 
9226 -- Resource Charges
9227 
9228 --print_debug_info('RESOURCE CHARGES');
9229 
9230 FORALL j IN 1..lv_job_count
9231     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
9232           ( last_update_date,
9233             last_updated_by,
9234             last_update_login,
9235             creation_date,
9236             created_by,
9237             organization_type,
9238             organization_id,
9239             group_id,
9240             parent_header_id,
9241             operation_seq_num,
9242             resource_seq_num,
9243 	    alternate_num,
9244             start_date,
9245             completion_date,
9246 	    required_quantity,
9247 	    charge_number,
9248             SR_INSTANCE_ID,
9249             SUBSTITUTION_TYPE,
9250             LOAD_TYPE,
9251             process_phase,
9252             process_status
9253           , wip_entity_id -- for reschedule
9254           , eam_flag -- dsr
9255           , resource_id_old
9256           , resource_id_new
9257           , schedule_seq_num
9258        )
9259     SELECT
9260             SYSDATE,
9261             arg_user_id,
9262             s.last_update_login,
9263             SYSDATE,
9264             arg_user_id,
9265             tp.organization_type,
9266             s.organization_id,
9267             arg_wip_group_id,
9268             s.transaction_id,
9269             resreq.OPERATION_SEQ_NUM,
9270             resreq.ORIG_RESOURCE_SEQ_NUM,
9271             resreq.alternate_num,
9272             Chg.charge_start_datetime,
9273             Chg.charge_end_datetime,
9274             chg.charge_quantity, -- Chg.Planned_Charge_Quantity,
9275             Chg.charge_number,
9276             s.sr_instance_id,
9277             SUBST_CHANGE, -- 3
9278             LT_CHARGE_TYPE, --8
9279             2,
9280             1
9281           , s.disposition_id -- for reschedule
9282           , DECODE(arg_load_type, EAM_RESCHEDULE_WORK_ORDER, SYS_YES, SYS_NO) -- dsr: eam_flag
9283           , resreq.RESOURCE_ID
9284           ,  resreq.RESOURCE_ID
9285           ,  resreq.resource_seq_num
9286      FROM
9287              msc_trading_partners   tp,
9288              msc_resource_requirements resreq,
9289              msc_supplies            s,
9290              msc_resource_charges chg
9291     WHERE
9292             tp.sr_tp_id=s.organization_id
9293     AND     tp.sr_instance_id= s.sr_instance_id
9294     AND     tp.partner_type=3
9295     AND     resreq.sr_instance_id= s.sr_instance_id
9296     AND     resreq.organization_id= s.organization_id
9297     AND     resreq.supply_id = s.transaction_id
9298     AND     resreq.plan_id   = s.plan_id
9299     AND     resreq.transaction_id = chg.res_transaction_id -- chg.transaction_id
9300     AND     resreq.plan_id = chg.plan_id
9301     AND     resreq.sr_instance_id = chg.sr_instance_id
9302     AND     resreq.parent_id   = 2
9303     AND     resreq.resource_id <> -1
9304     AND     resreq.department_id <> -1
9305     AND     s.transaction_id= lv_transaction_id(j)
9306     AND     s.sr_instance_id= lv_instance_id(j)
9307     AND     s.plan_id= arg_plan_id
9308     AND     lv_details_enabled(j)= 1
9309     AND     lv_agg_details(j) = 1
9310 	;
9311 
9312 --print_debug_info('reschedule_wip_discrete_jobs: aaa sql%rowcount = '|| SQL%ROWCOUNT);
9313 
9314 -- dsr ends here
9315    END IF; /* End of l_count = 1 */
9316 
9317     FORALL j IN 1..lv_job_count
9318        update msc_supplies
9319            set releasable = RELEASE_ATTEMPTED
9320                ,batch_id   = nvl(batch_id,g_batch_id)
9321          where plan_id = arg_plan_id
9322            and transaction_id= lv_transaction_id(j);
9323 
9324     RETURN lv_resched_jobs;
9325 
9326 END reschedule_wip_discrete_jobs;
9327 
9328 FUNCTION load_repetitive_schedules
9329 ( arg_plan_id			IN      NUMBER
9330 , arg_log_org_id 		IN 	NUMBER
9331 , arg_org_instance              IN      NUMBER
9332 , arg_owning_org_id 		IN 	NUMBER
9333 , arg_owning_instance           IN      NUMBER
9334 , arg_user_id 			IN 	NUMBER
9335 , arg_wip_group_id              IN      NUMBER
9336 , arg_mode                      IN      VARCHAR2
9337 , arg_transaction_id            IN      NUMBER
9338 )RETURN NUMBER
9339 
9340 IS
9341    lv_loaded_scheds    NUMBER;
9342 BEGIN
9343 
9344     -- ------------------------------------------------------------------------
9345     -- Perform the wip repetitive schedule mass load
9346     -- ------------------------------------------------------------------------
9347     l_sql_stmt := ' INSERT INTO msc_wip_job_schedule_interface
9348             (last_update_date,
9349             last_updated_by,
9350             last_update_login,
9351             creation_date,
9352             created_by,
9353             group_id,
9354             source_code,
9355             source_line_id,
9356             organization_id,
9357             organization_type,
9358             load_type,
9359             last_unit_completion_date,
9360             bom_revision_date,
9361             routing_revision_date,
9362             processing_work_days,
9363             daily_production_rate,
9364             line_id,
9365             primary_item_id,
9366             firm_planned_flag,
9367             demand_class,
9368 	    process_phase,
9369 	    process_status,
9370             bom_reference_id,
9371             routing_reference_id,
9372             BILL_RTG_EXPLOSION_FLAG,
9373             HEADER_ID,
9374             uom_code, --Outbound Changes for XML
9375             SR_INSTANCE_ID)
9376        SELECT SYSDATE,
9377             :arg_user_id,
9378             s.last_update_login,
9379             SYSDATE,
9380             :arg_user_id,
9381             :arg_wip_group_id,
9382             ''MSC'',
9383             s.transaction_id,
9384             msi.organization_id,
9385             tp.organization_type,
9386             2,
9387             s.implement_date,
9388 			NULL,
9389 			NULL,
9390             s.implement_processing_days,
9391             s.implement_daily_rate,
9392             s.implement_line_id,
9393             msi.sr_inventory_item_id,
9394             s.implement_firm,
9395             s.implement_demand_class,
9396             2,
9397             1,
9398             DECODE( tp.organization_type,
9399                     2, mpe.bill_sequence_id,
9400                     NULL),
9401             DECODE( tp.organization_type,
9402                     2, mpe.routing_sequence_id,
9403                     NULL),
9404             ''Y'',
9405             s.transaction_id,
9406             nvl(s.implement_uom_code,msi.uom_code),
9407             s.sr_instance_id
9408     FROM    msc_trading_partners tp,
9409             msc_parameters param,
9410             msc_system_items msi,
9411             msc_process_effectivity mpe,
9412             msc_supplies     s,
9413             msc_plan_organizations_v orgs
9414     WHERE   tp.sr_tp_id= msi.organization_id
9415     AND     tp.sr_instance_id= msi.sr_instance_id
9416     AND     tp.partner_type=3
9417     AND     param.organization_id = msi.organization_id
9418     AND     param.sr_instance_id = msi.sr_instance_id
9419     AND     msi.plan_id = s.plan_id
9420     AND     msi.organization_id = s.organization_id
9421     AND     msi.sr_instance_id = s.sr_instance_id
9422     AND     msi.inventory_item_id = s.inventory_item_id
9423     AND     mpe.plan_id(+)= s.plan_id
9424     AND     mpe.process_sequence_id(+)= s.process_seq_id
9425     AND	    s.release_errors is NULL
9426     AND     s.implement_daily_rate > 0
9427     AND     s.organization_id = orgs.planned_organization
9428     AND     s.sr_instance_id = orgs.sr_instance_id
9429     AND     s.plan_id = orgs.plan_id
9430     AND     orgs.organization_id = :arg_owning_org_id
9431     AND     orgs.owning_sr_instance = :arg_owning_instance
9432     AND     orgs.plan_id = :arg_plan_id
9433     AND     orgs.sr_instance_id = :arg_org_instance
9434     and    (s.releasable = ' || RELEASABLE || ' or s.releasable is null )' ;
9435 
9436     IF  v_batch_id_populated = 2 THEN
9437         l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id is NULL ';
9438     ELSE
9439         l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id = ' || g_batch_id || ' ' ;
9440     END IF;
9441 
9442     IF arg_log_org_id <> arg_owning_org_id THEN
9443         l_sql_stmt :=  l_sql_stmt ||'      AND     orgs.planned_organization = ' || arg_log_org_id || ' ' ;
9444     END IF;
9445 
9446     IF arg_mode IS NULL THEN
9447         l_sql_stmt :=  l_sql_stmt ||'  AND s.load_type = ' || WIP_REP_MASS_LOAD || ' ';
9448         IF v_msc_released_only_by_user = 1 THEN
9449             l_sql_stmt :=  l_sql_stmt || ' AND s.last_updated_by = ' || v_user_id || ' ' ;
9450         END IF;
9451     ELSIF arg_mode = 'WF_BATCH' THEN
9452         l_sql_stmt :=  l_sql_stmt ||'  AND s.load_type =  ' || WIP_REP_MASS_LOAD || ' ';
9453     ELSIF arg_mode = 'WF' THEN
9454         l_sql_stmt :=  l_sql_stmt ||'      AND s.transaction_id = ' || arg_transaction_id  || '  ' ;
9455     END IF;
9456 
9457     lv_loaded_scheds:= 0;
9458     IF arg_mode IS NULL OR arg_mode = 'WF_BATCH' OR arg_mode = 'WF' THEN
9459         EXECUTE IMMEDIATE l_sql_stmt USING  arg_user_id
9460                                            ,arg_user_id
9461                                            ,arg_wip_group_id
9462                                            ,arg_owning_org_id
9463                                            ,arg_owning_instance
9464                                            ,arg_plan_id
9465                                            ,arg_org_instance;
9466         lv_loaded_scheds:= SQL%ROWCOUNT;
9467     END IF;
9468 
9469 
9470    IF arg_mode = 'WF' THEN
9471         update msc_supplies
9472            set releasable = RELEASE_ATTEMPTED
9473                ,batch_id   = nvl(batch_id,g_batch_id)
9474          where plan_id = arg_plan_id
9475            and transaction_id= arg_transaction_id;
9476    ELSE
9477         update msc_supplies
9478            set releasable = RELEASE_ATTEMPTED
9479                ,batch_id   = nvl(batch_id,g_batch_id)
9480          where plan_id = arg_plan_id
9481            and transaction_id in (select source_line_id from msc_wip_job_schedule_interface
9482                                    where group_id = arg_wip_group_id);
9483    END IF;
9484 
9485     RETURN lv_loaded_scheds;
9486 
9487 END load_repetitive_schedules;
9488 
9489 FUNCTION load_po_requisitions
9490 ( arg_plan_id			IN      NUMBER
9491 , arg_log_org_id 		IN 	NUMBER
9492 , arg_org_instance              IN      NUMBER
9493 , arg_owning_org_id 		IN 	NUMBER
9494 , arg_owning_instance           IN      NUMBER
9495 , arg_user_id 			IN 	NUMBER
9496 , arg_po_group_by 		IN 	NUMBER
9497 , arg_po_batch_number 		IN 	NUMBER
9498 , arg_mode                      IN      VARCHAR2
9499 , arg_transaction_id            IN      NUMBER
9500 )RETURN NUMBER
9501 IS
9502    lv_loaded_reqs    NUMBER;
9503    lv_errbuf         VARCHAR2(2048);
9504    ln_retcode        NUMBER;
9505    lv_custom         EXCEPTION;
9506 BEGIN
9507 
9508     -- ------------------------------------------------------------------------
9509     -- Perform the po mass load
9510     -- ------------------------------------------------------------------------
9511     --  Check if the profile MRP_PURCHASING_BY_REVISION is set
9512     --  NOTE: We always pass 'VENDOR' as the group by parameter to the req
9513     -- import program.  PO will only look at this parameter if it has failed
9514     -- to find a value in the group code.
9515 
9516 
9517 
9518     INSERT INTO msc_po_requisitions_interface /*+ FIRST_ROWS */
9519             (
9520             /*line_type_id, Amount or Quantity based */
9521 	    last_updated_by,
9522             last_update_date,
9523             last_update_login,
9524             creation_date,
9525             created_by,
9526             item_id,
9527             quantity,
9528             need_by_date,
9529             interface_source_code,
9530             deliver_to_location_id,
9531             deliver_to_requestor_id,
9532             destination_type_code,
9533             preparer_id,
9534             source_type_code,
9535             authorization_status,
9536             uom_code,
9537             batch_id,
9538             charge_account_id,
9539             group_code,
9540             item_revision,
9541             destination_organization_id,
9542             autosource_flag,
9543             org_id,
9544             source_organization_id,
9545             suggested_vendor_id,
9546             /*suggested_vendor_site_id,  -- For Outbound Changes for XML*/
9547             suggested_vendor_site,
9548             project_id,
9549             task_id,
9550 	    end_item_unit_number,
9551             project_accounting_context,
9552             source_line_id, -- Outbound Changes for XML
9553             SR_INSTANCE_ID)
9554      SELECT /*+ FIRST_ROWS */
9555             /*1, Quantity based */
9556             s.last_updated_by,
9557             SYSDATE,
9558             s.last_update_login,
9559             SYSDATE,
9560             s.created_by,
9561             msi.sr_inventory_item_id,
9562             s.implement_quantity,
9563             /*nvl(get_cal_date(s.sr_instance_id,
9564                              cal2.calendar_date,
9565                              mis.delivery_calendar_code), cal2.calendar_date),*/
9566 
9567 	    --implement_dock_date becomes null for manual po
9568 	    nvl(s.implement_dock_date,
9569 		nvl(s.new_dock_date,
9570 			trunc(sysdate)
9571 		)
9572 	    ),
9573 	    'MSC',
9574             s.implement_location_id,
9575             s.implement_employee_id,
9576             'INVENTORY',
9577             s.implement_employee_id,
9578             DECODE(s.implement_supplier_id,
9579                    NULL, DECODE(s.implement_source_org_id,
9580                                   NULL,to_char(NULL),
9581                                   decode(mp1.MODELED_SUPPLIER_ID,
9582                                           NULL,'INVENTORY',
9583                                           DECODE(mp1.subcontracting_source_org,
9584                                           1,'INVENTORY','VENDOR')
9585                                           )),
9586                    'VENDOR'), -- PO wants us to pass null now -- spob
9587             'APPROVED',
9588             msi.uom_code, --mr.implement_uom_code,
9589             arg_po_batch_number,
9590             decode(mp.organization_type,1,nvl(mpp.material_account,
9591                       decode( msi1.inventory_asset_flag,
9592                               'Y', mp.material_account,
9593 		              nvl(msi1.expense_account, mp.expense_account))),-1),
9594             decode(arg_po_group_by,
9595                 REQ_GRP_ALL_ON_ONE, 'ALL-ON-ONE',
9596                 REQ_GRP_ITEM, to_char(s.inventory_item_id),
9597                 REQ_GRP_BUYER, nvl(to_char(msi.buyer_id),NULL),
9598                 REQ_GRP_PLANNER, nvl(msi.planner_code,'PLANNER'),
9599                 REQ_GRP_VENDOR,  NULL,
9600                 REQ_GRP_ONE_EACH, to_char(-100),
9601                 REQ_GRP_CATEGORY,nvl(to_char(msi.sr_category_id),NULL),
9602                 REQ_GRP_LOCATION,NULL,
9603                 NULL),
9604             DECODE( v_purchasing_by_rev,
9605                     NULL, DECODE( msi.REVISION_QTY_CONTROL_CODE,
9606 			          NOT_UNDER_REV_CONTROL, NULL,
9607                                   msi.revision),
9608 	            PURCHASING_BY_REV, msi.revision,
9609 	            NOT_PURCHASING_BY_REV, NULL),
9610             s.organization_id,
9611             'P',
9612             mp.operating_unit,
9613     /* for single instance if the organization is modelled then the source_sr_instane_id = sr_instance_id
9614        in msc_supplies so we can safely use mp1 for both cases.
9615        For cross-instance releases get the modeled supplier infor */
9616             decode(s.implement_source_org_id,
9617                    NULL, to_number(NULL),
9618                    decode(mp1.MODELED_SUPPLIER_ID,
9619                           NULL,s.implement_source_org_id,
9620                           decode(mp1.subcontracting_source_org,
9621                                  1 , s.implement_source_org_id,
9622                                  to_number(NULL)))),
9623             decode(s.implement_source_org_id,
9624                    NULL ,supplier.sr_tp_id,
9625                    decode(mp1.MODELED_SUPPLIER_ID,
9626                           NULL,to_number(NULL),
9627                           decode(mp1.subcontracting_source_org,
9628                                  1, to_number(NULL),
9629                                  GET_MODELED_SR_TP_ID(mp1.MODELED_SUPPLIER_ID,
9630                                                       s.sr_instance_id)))),
9631             decode(s.implement_source_org_id,
9632                    NULL ,mtps.tp_site_code,
9633                    decode(mp1.MODELED_SUPPLIER_ID,
9634                           NULL,to_char(NULL),
9635                           decode(mp1.subcontracting_source_org,
9636                                  1, to_char(NULL),
9637                                  GET_MODELED_TP_SITE_CODE(
9638                                   mp1.MODELED_SUPPLIER_ID,
9639                                   mp1.MODELED_SUPPLIER_SITE_ID,s.sr_instance_id)
9640                                ))),
9641             s.implement_project_id,
9642             s.implement_task_id,
9643   	    s.implement_unit_number,
9644 	    DECODE(s.implement_project_id, NULL, 'N', 'Y'),
9645             s.transaction_id,-- For Outbound Changes for XML
9646             s.sr_instance_id
9647     FROM    msc_projects mpp,
9648             --msc_calendar_dates cal1,
9649             --msc_calendar_dates cal2,
9650             msc_tp_id_lid        supplier,
9651             msc_trading_partner_sites mtps,
9652             msc_trading_partners mp, -- mtl_parameters      mp,
9653             msc_trading_partners mp1,
9654             msc_system_items    msi,
9655             msc_system_items    msi1,
9656             msc_supplies        s,
9657             msc_item_suppliers mis,
9658             msc_plan_organizations_v orgs
9659     WHERE   supplier.tp_id(+)=
9660                 nvl(s.implement_supplier_id, s.source_supplier_id)
9661     AND     supplier.partner_type(+)=1
9662     AND     supplier.sr_instance_id(+)= s.sr_instance_id
9663     AND     mtps.partner_id(+) = nvl(s.implement_supplier_id, s.source_supplier_id)
9664     AND     mtps.partner_type(+) = 1
9665     AND     mtps.partner_site_id(+) = nvl(s.implement_supplier_site_id, s.source_supplier_site_id)
9666     AND     mis.sr_instance_id (+) = s.sr_instance_id
9667     AND     mis.plan_id (+) = s.plan_id
9668     AND     mis.inventory_item_id (+) = s.inventory_item_id
9669     AND     mis.organization_id (+) = s.organization_id
9670     AND     mis.using_organization_id (+) = -1
9671     AND    mis.supplier_id (+) = nvl(s.implement_supplier_id, s.source_supplier_id)
9672     AND     mis.supplier_site_id (+) =  nvl(s.implement_supplier_site_id, s.source_supplier_site_id)
9673     AND     mpp.organization_id (+)= s.organization_id
9674     AND	    mpp.project_id (+)= nvl(s.implement_project_id, -23453)
9675     AND     mpp.plan_id (+)= s.plan_id
9676     AND     mpp.sr_instance_id(+)= s.sr_instance_id
9677     --AND     cal1.sr_instance_id= mp.sr_instance_id
9678     --AND	    cal1.calendar_code = mp.calendar_code
9679     --AND     cal1.exception_set_id = mp.calendar_exception_set_id
9680     --AND     cal1.calendar_date = trunc(s.implement_date)
9681     --AND     cal2.sr_instance_id = cal1.sr_instance_id
9682     -- AND     cal2.calendar_code = cal1.calendar_code
9683     --AND     cal2.exception_set_id = cal1.exception_set_id
9684     --AND     cal2.seq_num = GREATEST(1,NVL(cal1.seq_num, cal1.prior_seq_num) -     NVL(msi.postprocessing_lead_time, 0))
9685     AND     mp.sr_tp_id  = msi.organization_id
9686     AND     mp.sr_instance_id = msi.sr_instance_id
9687     AND     mp.partner_type= 3
9688 AND mp1.sr_tp_id (+)= s.SOURCE_ORGANIZATION_ID
9689 AND mp1.sr_instance_id (+)= s.SOURCE_SR_INSTANCE_ID
9690 AND mp1.partner_type(+)= 3
9691     AND     msi.inventory_item_id = s.inventory_item_id
9692     AND     msi.plan_id = s.plan_id
9693     AND     msi.organization_id = s.organization_id
9694     AND     msi.sr_instance_id = s.sr_instance_id
9695     AND     msi1.plan_id = -1
9696     AND     msi1.sr_instance_id = msi.sr_instance_id
9697     AND     msi1.organization_id = msi.organization_id
9698     AND     msi1.inventory_item_id = msi.inventory_item_id
9699     AND	    s.release_errors is NULL
9700     AND     s.implement_quantity > 0
9701     AND     s.organization_id = orgs.planned_organization
9702     AND     s.sr_instance_id = orgs.sr_instance_id
9703     AND     s.plan_id = orgs.plan_id
9704     AND     orgs.organization_id = arg_owning_org_id
9705     AND     orgs.owning_sr_instance = arg_owning_instance
9706     AND     orgs.plan_id = arg_plan_id
9707     /* AND     orgs.planned_organization = decode(arg_log_org_id,
9708                     arg_owning_org_id, orgs.planned_organization,
9709                     arg_log_org_id)  */   --11865146
9710     AND (arg_log_org_id = arg_owning_org_id
9711          OR
9712          orgs.planned_organization = arg_log_org_id)
9713     AND     orgs.sr_instance_id  = arg_org_instance
9714     AND     (arg_mode is null and s.load_type = PO_MASS_LOAD and s.load_type IS NOT NULL)
9715     AND     s.last_updated_by = decode(v_msc_released_only_by_user,1,v_user_id,s.last_updated_by)
9716     and (s.releasable = RELEASABLE or s.releasable is null )
9717     and  (s.batch_id = g_batch_id or (v_batch_id_populated = 2 and s.batch_id is null) )
9718     UNION ALL
9719     SELECT /*+ FIRST_ROWS */
9720             /*1,  Quantity based */
9721             s.last_updated_by,
9722             SYSDATE,
9723             s.last_update_login,
9724             SYSDATE,
9725             s.created_by,
9726             msi.sr_inventory_item_id,
9727             s.implement_quantity,
9728            /* nvl(get_cal_date(s.sr_instance_id,
9729                              cal2.calendar_date,
9730                              mis.delivery_calendar_code), cal2.calendar_date),*/
9731 
9732 	    --implement_dock_date becomes null for manual po
9733 	    nvl(s.implement_dock_date,
9734 		nvl(s.new_dock_date,
9735 			trunc(sysdate)
9736 		)
9737 	    ),
9738 
9739             'MSC',
9740             s.implement_location_id,
9741             s.implement_employee_id,
9742             'INVENTORY',
9743             s.implement_employee_id,
9744             DECODE(s.implement_supplier_id,
9745                    NULL, DECODE(s.implement_source_org_id,
9746                                   NULL,to_char(NULL),
9747                                   decode(mp1.MODELED_SUPPLIER_ID,
9748                                           NULL,'INVENTORY', 'VENDOR') ),
9749                    'VENDOR'), -- PO wants us to pass null now -- spob
9750             'APPROVED',
9751             msi.uom_code, --mr.implement_uom_code,
9752             arg_po_batch_number,
9753             decode(mp.organization_type,1,nvl(mpp.material_account,
9754                       decode( msi1.inventory_asset_flag,
9755                               'Y', mp.material_account,
9756 		              nvl(msi1.expense_account, mp.expense_account))),-1),
9757             decode(arg_po_group_by,
9758                 REQ_GRP_ALL_ON_ONE, 'ALL-ON-ONE',
9759                 REQ_GRP_ITEM, to_char(s.inventory_item_id),
9760                 REQ_GRP_BUYER, nvl(to_char(msi.buyer_id),NULL),
9761                 REQ_GRP_PLANNER, nvl(msi.planner_code,'PLANNER'),
9762                 REQ_GRP_VENDOR,  NULL,
9763                 REQ_GRP_ONE_EACH, to_char(-100),
9764                 REQ_GRP_CATEGORY,nvl(to_char(msi.sr_category_id),NULL),
9765                 REQ_GRP_LOCATION,NULL,
9766                 NULL),
9767             DECODE( v_purchasing_by_rev,
9768                     NULL, DECODE( msi.REVISION_QTY_CONTROL_CODE,
9769 			          NOT_UNDER_REV_CONTROL, NULL,
9770                                   msi.revision),
9771 	            PURCHASING_BY_REV, msi.revision,
9772 	            NOT_PURCHASING_BY_REV, NULL),
9773             s.organization_id,
9774             'P',
9775             mp.operating_unit,
9776             decode(s.implement_source_org_id, NULL, to_number(NULL),
9777                         decode(mp1.MODELED_SUPPLIER_ID, NULL,s.implement_source_org_id, to_number(NULL) ) ),
9778             decode(s.implement_source_org_id, NULL ,supplier.sr_tp_id,
9779                         decode(mp1.MODELED_SUPPLIER_ID, NULL,to_number(NULL),
9780                                      GET_MODELED_SR_TP_ID(mp1.MODELED_SUPPLIER_ID,s.sr_instance_id)
9781                                )),
9782             decode(s.implement_source_org_id, NULL ,mtps.tp_site_code,
9783                         decode(mp1.MODELED_SUPPLIER_ID, NULL,to_char(NULL),
9784                                      GET_MODELED_TP_SITE_CODE(mp1.MODELED_SUPPLIER_ID,
9785                                                               mp1.MODELED_SUPPLIER_SITE_ID,s.sr_instance_id)
9786                                )),
9787             s.implement_project_id,
9788             s.implement_task_id,
9789   	    s.implement_unit_number,
9790 	    DECODE(s.implement_project_id, NULL, 'N', 'Y'),
9791             s.transaction_id, -- Outbound Changes for XML
9792             s.sr_instance_id
9793     FROM    msc_projects mpp,
9794            -- msc_calendar_dates cal1,
9795            -- msc_calendar_dates cal2,
9796             msc_tp_id_lid        supplier,
9797             msc_trading_partner_sites mtps,
9798             msc_trading_partners mp, -- mtl_parameters      mp,
9799             msc_trading_partners mp1,
9800             msc_system_items    msi,
9801             msc_system_items    msi1,
9802             msc_item_suppliers mis,
9803             msc_supplies        s,
9804             msc_plan_organizations_v orgs
9805     WHERE   supplier.tp_id(+)=
9806                 nvl(s.implement_supplier_id, s.source_supplier_id)
9807     AND     supplier.partner_type(+)=1
9808     AND     supplier.sr_instance_id(+)= s.sr_instance_id
9809     AND     mtps.partner_id(+) = nvl(s.implement_supplier_id, s.source_supplier_id)
9810     AND     mtps.partner_type(+) = 1
9811     AND     mtps.partner_site_id(+) = nvl(s.implement_supplier_site_id, s.source_supplier_site_id)
9812     AND     mis.sr_instance_id (+) = s.sr_instance_id
9813     AND     mis.plan_id (+) = s.plan_id
9814     AND     mis.inventory_item_id (+) = s.inventory_item_id
9815     AND     mis.organization_id (+) = s.organization_id
9816     AND     mis.using_organization_id (+) = -1
9817     AND    mis.supplier_id (+) = nvl(s.implement_supplier_id, s.source_supplier_id)
9818     AND     mis.supplier_site_id (+) =  nvl(s.implement_supplier_site_id, s.source_supplier_site_id)
9819     AND     mpp.organization_id (+)= s.organization_id
9820     AND	    mpp.project_id (+)= nvl(s.implement_project_id, -23453)
9821     AND     mpp.plan_id (+)= s.plan_id
9822     AND     mpp.sr_instance_id(+)= s.sr_instance_id
9823    -- AND     cal1.sr_instance_id= mp.sr_instance_id
9824    -- AND	    cal1.calendar_code = mp.calendar_code
9825     --AND     cal1.exception_set_id = mp.calendar_exception_set_id
9826    -- AND     cal1.calendar_date = trunc(s.implement_date)
9827    -- AND     cal2.sr_instance_id = cal1.sr_instance_id
9828    -- AND     cal2.calendar_code = cal1.calendar_code
9829    -- AND     cal2.exception_set_id = cal1.exception_set_id
9830    -- AND     cal2.seq_num = GREATEST(1,NVL(cal1.seq_num, cal1.prior_seq_num) -     NVL(msi.postprocessing_lead_time, 0))
9831     AND     mp.sr_tp_id  = msi.organization_id
9832     AND     mp.sr_instance_id = msi.sr_instance_id
9833     AND     mp.partner_type= 3
9834 AND mp1.sr_tp_id (+)= s.SOURCE_ORGANIZATION_ID
9835 AND mp1.sr_instance_id (+)= s.SOURCE_SR_INSTANCE_ID
9836 AND mp1.partner_type(+)= 3
9837     AND     msi.inventory_item_id = s.inventory_item_id
9838     AND     msi.plan_id = s.plan_id
9839     AND     msi.organization_id = s.organization_id
9840     AND     msi.sr_instance_id = s.sr_instance_id
9841     AND     msi1.plan_id = -1
9842     AND     msi1.sr_instance_id = msi.sr_instance_id
9843     AND     msi1.organization_id = msi.organization_id
9844     AND     msi1.inventory_item_id = msi.inventory_item_id
9845     AND	    s.release_errors is NULL
9846     AND     s.implement_quantity > 0
9847     AND     s.organization_id = orgs.planned_organization
9848     AND     s.sr_instance_id = orgs.sr_instance_id
9849     AND     s.plan_id = orgs.plan_id
9850     AND     orgs.organization_id = arg_owning_org_id
9851     AND     orgs.owning_sr_instance = arg_owning_instance
9852     AND     orgs.plan_id = arg_plan_id
9853     /* AND     orgs.planned_organization = decode(arg_log_org_id,
9854                     arg_owning_org_id, orgs.planned_organization,
9855                     arg_log_org_id)  */   --11865146
9856     AND (arg_log_org_id = arg_owning_org_id
9857          OR
9858          orgs.planned_organization = arg_log_org_id)
9859     AND     orgs.sr_instance_id  = arg_org_instance
9860     AND     (arg_mode = 'WF_BATCH' and s.load_type = PO_MASS_LOAD and s.load_type IS NOT NULL)
9861     and (s.releasable = RELEASABLE or s.releasable is null )
9862     and  (s.batch_id = g_batch_id or (v_batch_id_populated = 2 and s.batch_id is null) )
9863     UNION ALL
9864     SELECT /*+ FIRST_ROWS */
9865             /*1,  Quantity based */
9866             s.last_updated_by,
9867             SYSDATE,
9868             s.last_update_login,
9869             SYSDATE,
9870             s.created_by,
9871             msi.sr_inventory_item_id,
9872             s.implement_quantity,
9873             /*nvl(get_cal_date(s.sr_instance_id,
9874                              cal2.calendar_date,
9875                              mis.delivery_calendar_code), cal2.calendar_date),*/
9876 
9877 	    --implement_dock_date becomes null for manual po
9878 	    nvl(s.implement_dock_date,
9879 		nvl(s.new_dock_date,
9880 			trunc(sysdate)
9881 		)
9882 	    ),
9883 
9884 	    'MSC',
9885             s.implement_location_id,
9886             s.implement_employee_id,
9887             'INVENTORY',
9888             s.implement_employee_id,
9889             DECODE(s.implement_supplier_id,
9890                    NULL, DECODE(s.implement_source_org_id,
9891                                   NULL,to_char(NULL),
9892                                   decode(mp1.MODELED_SUPPLIER_ID,
9893                                           NULL,'INVENTORY', 'VENDOR') ),
9894                    'VENDOR'), -- PO wants us to pass null now -- spob
9895             'APPROVED',
9896             msi.uom_code, --mr.implement_uom_code,
9897             arg_po_batch_number,
9898             decode(mp.organization_type,1,nvl(mpp.material_account,
9899                       decode( msi1.inventory_asset_flag,
9900                               'Y', mp.material_account,
9901 		              nvl(msi1.expense_account, mp.expense_account))),-1),
9902             decode(arg_po_group_by,
9903                 REQ_GRP_ALL_ON_ONE, 'ALL-ON-ONE',
9904                 REQ_GRP_ITEM, to_char(s.inventory_item_id),
9905                 REQ_GRP_BUYER, nvl(to_char(msi.buyer_id),NULL),
9906                 REQ_GRP_PLANNER, nvl(msi.planner_code,'PLANNER'),
9907                 REQ_GRP_VENDOR,  NULL,
9908                 REQ_GRP_ONE_EACH, to_char(-100),
9909                 REQ_GRP_CATEGORY,nvl(to_char(msi.sr_category_id),NULL),
9910                 REQ_GRP_LOCATION,NULL,
9911                 NULL),
9912             DECODE( v_purchasing_by_rev,
9913                     NULL, DECODE( msi.REVISION_QTY_CONTROL_CODE,
9914 			          NOT_UNDER_REV_CONTROL, NULL,
9915                                   msi.revision),
9916 	            PURCHASING_BY_REV, msi.revision,
9917 	            NOT_PURCHASING_BY_REV, NULL),
9918             s.organization_id,
9919             'P',
9920             mp.operating_unit,
9921             decode(s.implement_source_org_id, NULL, to_number(NULL),
9922                         decode(mp1.MODELED_SUPPLIER_ID, NULL,s.implement_source_org_id, to_number(NULL) ) ),
9923             decode(s.implement_source_org_id, NULL ,supplier.sr_tp_id,
9924                         decode(mp1.MODELED_SUPPLIER_ID, NULL,to_number(NULL),
9925                                      GET_MODELED_SR_TP_ID(mp1.MODELED_SUPPLIER_ID,s.sr_instance_id)
9926                                )),
9927             decode(s.implement_source_org_id, NULL ,mtps.tp_site_code,
9928                         decode(mp1.MODELED_SUPPLIER_ID, NULL,to_char(NULL),
9929                                      GET_MODELED_TP_SITE_CODE(mp1.MODELED_SUPPLIER_ID,
9930                                                               mp1.MODELED_SUPPLIER_SITE_ID,s.sr_instance_id)
9931                                )),
9932             s.implement_project_id,
9933             s.implement_task_id,
9934   	    s.implement_unit_number,
9935 	    DECODE(s.implement_project_id, NULL, 'N', 'Y'),
9936             s.transaction_id, --outbound changes for XML
9937             s.sr_instance_id
9938     FROM    msc_projects mpp,
9939            -- msc_calendar_dates cal1,
9940            -- msc_calendar_dates cal2,
9941             msc_tp_id_lid        supplier,
9942             msc_trading_partner_sites mtps,
9943             msc_trading_partners mp, -- mtl_parameters      mp,
9944             msc_trading_partners mp1,
9945             msc_system_items    msi,
9946             msc_system_items    msi1,
9947             msc_item_suppliers mis,
9948             msc_supplies        s,
9949             msc_plan_organizations_v orgs
9950     WHERE   supplier.tp_id(+)=
9951                 nvl(s.implement_supplier_id, s.source_supplier_id)
9952     AND     supplier.partner_type(+)=1
9953     AND     supplier.sr_instance_id(+)= s.sr_instance_id
9954     AND     mtps.partner_id(+) = nvl(s.implement_supplier_id, s.source_supplier_id)
9955     AND     mtps.partner_type(+) = 1
9956     AND     mtps.partner_site_id(+) = nvl(s.implement_supplier_site_id, s.source_supplier_site_id)
9957     AND     mis.sr_instance_id (+) = s.sr_instance_id
9958     AND     mis.plan_id (+) = s.plan_id
9959     AND     mis.inventory_item_id (+) = s.inventory_item_id
9960     AND     mis.organization_id (+) = s.organization_id
9961     AND     mis.using_organization_id (+) = -1
9962     AND    mis.supplier_id (+) = nvl(s.implement_supplier_id, s.source_supplier_id)
9963     AND     mis.supplier_site_id (+) =  nvl(s.implement_supplier_site_id, s.source_supplier_site_id)
9964     AND     mpp.organization_id (+)= s.organization_id
9965     AND	    mpp.project_id (+)= nvl(s.implement_project_id, -23453)
9966     AND     mpp.plan_id (+)= s.plan_id
9967     AND     mpp.sr_instance_id(+)= s.sr_instance_id
9968     --AND     cal1.sr_instance_id= mp.sr_instance_id
9969     --AND	    cal1.calendar_code = mp.calendar_code
9970     --AND     cal1.exception_set_id = mp.calendar_exception_set_id
9971     --AND     cal1.calendar_date = trunc(s.implement_date)
9972     --AND     cal2.sr_instance_id = cal1.sr_instance_id
9973    -- AND     cal2.calendar_code = cal1.calendar_code
9974     --AND     cal2.exception_set_id = cal1.exception_set_id
9975     --AND     cal2.seq_num = GREATEST(1,NVL(cal1.seq_num, cal1.prior_seq_num) -                NVL(msi.postprocessing_lead_time, 0))
9976     AND     mp.sr_tp_id  = msi.organization_id
9977     AND     mp.sr_instance_id = msi.sr_instance_id
9978     AND     mp.partner_type= 3
9979 AND mp1.sr_tp_id (+)= s.SOURCE_ORGANIZATION_ID
9980 AND mp1.sr_instance_id (+)= s.SOURCE_SR_INSTANCE_ID
9981 AND mp1.partner_type(+)= 3
9982     AND     msi.inventory_item_id = s.inventory_item_id
9983     AND     msi.plan_id = s.plan_id
9984     AND     msi.organization_id = s.organization_id
9985     AND     msi.sr_instance_id = s.sr_instance_id
9986     AND     msi1.plan_id = -1
9987     AND     msi1.sr_instance_id = msi.sr_instance_id
9988     AND     msi1.organization_id = msi.organization_id
9989     AND     msi1.inventory_item_id = msi.inventory_item_id
9990     AND	    s.release_errors is NULL
9991     AND     s.implement_quantity > 0
9992     AND     s.organization_id = orgs.planned_organization
9993     AND     s.sr_instance_id = orgs.sr_instance_id
9994     AND     s.plan_id = orgs.plan_id
9995     AND     orgs.organization_id = arg_owning_org_id
9996     AND     orgs.owning_sr_instance = arg_owning_instance
9997     AND     orgs.plan_id = arg_plan_id
9998     /* AND     orgs.planned_organization = decode(arg_log_org_id,
9999                     arg_owning_org_id, orgs.planned_organization,
10000                     arg_log_org_id)  */   --11865146
10001     AND (arg_log_org_id = arg_owning_org_id
10002          OR
10003          orgs.planned_organization = arg_log_org_id)
10004     AND     orgs.sr_instance_id  = arg_org_instance
10005     AND     (arg_mode = 'WF' and s.transaction_id = arg_transaction_id)
10006     and (s.releasable = RELEASABLE or s.releasable is null )
10007     and  (s.batch_id = g_batch_id or (v_batch_id_populated = 2 and s.batch_id is null) );
10008 
10009     IF SQL%ROWCOUNT > 0 THEN
10010         lv_loaded_reqs := SQL%ROWCOUNT;
10011     ELSE
10012         lv_loaded_reqs := 0;
10013     END IF;
10014 
10015     update msc_po_requisitions_interface
10016     set group_code =  to_char(msc_po_requisitions_int_s.nextval)
10017     where group_code = '-100'
10018       and batch_id = arg_po_batch_number;
10019 
10020     msc_cl_cleanse.cleanse_release(lv_errbuf,
10021                                    ln_retcode,
10022                                    PO_MASS_LOAD,
10023                                    arg_org_instance,
10024                                    arg_po_batch_number
10025                                    );
10026     IF ln_retcode = 2 THEN --error
10027       raise lv_custom;
10028     END IF;
10029 
10030    IF arg_mode = 'WF' THEN
10031         update msc_supplies
10032            set releasable = RELEASE_ATTEMPTED
10033                ,batch_id   = nvl(batch_id,g_batch_id)
10034          where plan_id = arg_plan_id
10035            and transaction_id= arg_transaction_id;
10036    ELSE
10037         update msc_supplies
10038            set releasable = RELEASE_ATTEMPTED
10039                ,batch_id   = nvl(batch_id,g_batch_id)
10040          where plan_id = arg_plan_id
10041            and transaction_id in (select source_line_id from msc_po_requisitions_interface
10042                                    where batch_id = arg_po_batch_number);
10043    END IF;
10044 
10045     return lv_loaded_reqs;
10046 
10047 END load_po_requisitions;
10048 
10049 FUNCTION reschedule_po
10050 ( arg_plan_id			IN      NUMBER
10051 , arg_log_org_id 		IN 	NUMBER
10052 , arg_org_instance              IN      NUMBER
10053 , arg_owning_org_id 		IN 	NUMBER
10054 , arg_owning_instance           IN      NUMBER
10055 , arg_user_id 			IN 	NUMBER
10056 , arg_po_batch_number 		IN 	NUMBER
10057 , arg_mode                      IN      VARCHAR2
10058 , arg_transaction_id            IN      NUMBER
10059 )RETURN NUMBER
10060 IS
10061    lv_resched_reqs NUMBER;
10062 BEGIN
10063     -- ------------------------------------------------------------------------
10064     -- Perform the po mass reschedule
10065     -- ------------------------------------------------------------------------
10066 
10067     INSERT INTO msc_po_reschedule_interface
10068            (process_id,
10069             quantity,
10070             need_by_date,
10071             line_id,
10072             last_update_date,
10073             last_updated_by,
10074             creation_date,
10075             created_by,
10076             purchase_order_id, -- outbound Changes for XML
10077             po_number,         -- outbound Changes for XML
10078             source_line_id,    -- outbound Changes for XML
10079             uom,               -- outbound Changes for XML
10080             SR_INSTANCE_ID,
10081             batch_id)
10082     SELECT  NULL,
10083             s.implement_quantity,
10084             --cal2.calendar_date,
10085 	    s.implement_dock_date,
10086             s.po_line_id,
10087             SYSDATE,
10088             arg_user_id,
10089             SYSDATE,
10090             arg_user_id,
10091             s.disposition_id,
10092             s.order_number,
10093             s.transaction_id,
10094             nvl(s.implement_uom_code,msi.uom_code),
10095             s.sr_instance_id,
10096             g_batch_id
10097     FROM    --msc_calendar_dates cal1,
10098             --msc_calendar_dates cal2,
10099             msc_trading_partners mp,
10100             msc_system_items msi,
10101             msc_supplies s,
10102             msc_apps_instances mai,  -- IR/ISO resch Proj
10103             msc_plan_organizations_v orgs
10104     WHERE   --cal1.sr_instance_id= mp.sr_instance_id
10105     --AND     cal1.calendar_code = mp.calendar_code
10106     --AND     cal1.exception_set_id = mp.calendar_exception_set_id
10107     --AND     cal1.calendar_date = trunc(s.implement_date)
10108    -- AND     cal2.sr_instance_id= cal1.sr_instance_id
10109     --AND     cal2.calendar_code = cal1.calendar_code
10110     --AND     cal2.exception_set_id = cal1.exception_set_id
10111     --AND     cal2.seq_num = GREATEST(1,NVL(cal1.seq_num, cal1.prior_seq_num) -                NVL(msi.postprocessing_lead_time, 0))
10112          mp.sr_tp_id = msi.organization_id
10113     AND     mp.sr_instance_id = msi.sr_instance_id
10114     AND     mp.partner_type= 3
10115     AND     msi.inventory_item_id = s.inventory_item_id
10116     AND     msi.plan_id = s.plan_id
10117     AND     msi.organization_id = s.organization_id
10118     AND     msi.sr_instance_id = s.sr_instance_id
10119     AND	    s.release_errors is NULL
10120     AND     s.po_line_id IS NOT NULL
10121     AND     s.organization_id = orgs.planned_organization
10122     AND     s.sr_instance_id = orgs.sr_instance_id
10123     AND     s.plan_id = orgs.plan_id
10124     AND     orgs.organization_id = arg_owning_org_id
10125     AND     orgs.owning_sr_instance = arg_owning_instance
10126     AND     orgs.plan_id = arg_plan_id
10127     AND     orgs.planned_organization = decode(arg_log_org_id,
10128                     arg_owning_org_id, orgs.planned_organization,
10129                     arg_log_org_id)
10130     AND     orgs.sr_instance_id = arg_org_instance
10131     AND     s.sr_instance_id = mai.instance_id
10132     AND     ((arg_mode is null and s.load_type = PO_MASS_RESCHEDULE and s.load_type IS NOT NULL)
10133 			 OR     (arg_mode is null
10134 					 and s.load_type = DRP_REQ_RESCHED
10135 					 and mai.APPS_VER <= MSC_UTIL.G_APPS120
10136 					 and s.load_type IS NOT NULL))  -- IR/ISO resch Proj
10137     AND      s.last_updated_by = decode(v_msc_released_only_by_user,1,v_user_id,s.last_updated_by)
10138     and (s.releasable = RELEASABLE or s.releasable is null )
10139     and  (s.batch_id = g_batch_id or (v_batch_id_populated = 2 and s.batch_id is null) )
10140     UNION ALL
10141     SELECT NULL,
10142             s.implement_quantity,
10143            -- cal2.calendar_date,
10144 	    s.implement_dock_date,
10145             s.po_line_id,
10146             SYSDATE,
10147             arg_user_id,
10148             SYSDATE,
10149             arg_user_id,
10150             s.disposition_id,
10151             s.order_number,
10152             s.transaction_id,
10153             nvl(s.implement_uom_code,msi.uom_code),
10154             s.sr_instance_id,
10155             g_batch_id
10156     FROM    --msc_calendar_dates cal1,
10157             --msc_calendar_dates cal2,
10158             msc_trading_partners mp,
10159             msc_system_items msi,
10160             msc_supplies s,
10161             msc_plan_organizations_v orgs
10162     WHERE   --cal1.sr_instance_id= mp.sr_instance_id
10163     --AND     cal1.calendar_code = mp.calendar_code
10164    -- AND     cal1.exception_set_id = mp.calendar_exception_set_id
10165    -- AND     cal1.calendar_date = trunc(s.implement_date)
10166    -- AND     cal2.sr_instance_id= cal1.sr_instance_id
10167     --AND     cal2.calendar_code = cal1.calendar_code
10168    -- AND     cal2.exception_set_id = cal1.exception_set_id
10169     --AND     cal2.seq_num = GREATEST(1,NVL(cal1.seq_num, cal1.prior_seq_num) -      NVL(msi.postprocessing_lead_time, 0))
10170          mp.sr_tp_id = msi.organization_id
10171     AND     mp.sr_instance_id = msi.sr_instance_id
10172     AND     mp.partner_type= 3
10173     AND     msi.inventory_item_id = s.inventory_item_id
10174     AND     msi.plan_id = s.plan_id
10175     AND     msi.organization_id = s.organization_id
10176     AND     msi.sr_instance_id = s.sr_instance_id
10177     AND	    s.release_errors is NULL
10178     AND     s.po_line_id IS NOT NULL
10179     AND     s.organization_id = orgs.planned_organization
10180     AND     s.sr_instance_id = orgs.sr_instance_id
10181     AND     s.plan_id = orgs.plan_id
10182     AND     orgs.organization_id = arg_owning_org_id
10183     AND     orgs.owning_sr_instance = arg_owning_instance
10184     AND     orgs.plan_id = arg_plan_id
10185     AND     orgs.planned_organization = decode(arg_log_org_id,
10186                     arg_owning_org_id, orgs.planned_organization,
10187                     arg_log_org_id)
10188     AND     orgs.sr_instance_id = arg_org_instance
10189     AND     (arg_mode = 'WF' and s.transaction_id = arg_transaction_id)
10190     and (s.releasable = RELEASABLE or s.releasable is null )
10191     and  (s.batch_id = g_batch_id or (v_batch_id_populated = 2 and s.batch_id is null) );
10192 
10193     IF SQL%ROWCOUNT > 0  THEN
10194         lv_resched_reqs := SQL%ROWCOUNT;
10195     ELSE
10196         lv_resched_reqs := 0;
10197     END IF;
10198 
10199    IF arg_mode = 'WF' THEN
10200         update msc_supplies
10201            set releasable = RELEASE_ATTEMPTED
10202                ,batch_id   = nvl(batch_id,g_batch_id)
10203          where plan_id = arg_plan_id
10204            and transaction_id= arg_transaction_id;
10205    ELSE
10206         IF v_batch_id_populated = 1 THEN
10207             update msc_supplies
10208                set releasable = RELEASE_ATTEMPTED
10209              where batch_id = g_batch_id;
10210         ELSE
10211             update msc_supplies
10212                set releasable = RELEASE_ATTEMPTED
10213                    ,batch_id   = g_batch_id
10214              where plan_id = arg_plan_id
10215                and transaction_id in (select source_line_id from msc_po_reschedule_interface);
10216         END IF;
10217    END IF;
10218 
10219     RETURN lv_resched_reqs;
10220 
10221 END reschedule_po;
10222 
10223 FUNCTION reschedule_po_wf
10224 ( arg_dblink                    IN      VARCHAR2
10225 , arg_plan_id			IN      NUMBER
10226 , arg_log_org_id 		IN 	NUMBER
10227 , arg_org_instance              IN      NUMBER
10228 , arg_owning_org_id 		IN 	NUMBER
10229 , arg_owning_instance           IN      NUMBER
10230 , arg_user_id 			IN 	NUMBER
10231 , arg_mode                      IN      VARCHAR2
10232 , arg_transaction_id            IN      NUMBER
10233 )RETURN NUMBER
10234 IS
10235 
10236     lv_return_code		number;
10237     lv_old_need_by_date		date;
10238     lv_new_need_by_date		date;
10239     lv_po_header_id		number;
10240     lv_po_line_id		number;
10241     lv_po_number			varchar2(60);
10242 
10243     lv_sql_stmt                 VARCHAR2(2000);
10244 BEGIN
10245 
10246     -- ------------------------------------------------------------------------
10247     -- Perform PO reschedule : Workflow version
10248     -- ------------------------------------------------------------------------
10249 
10250 
10251 
10252       BEGIN
10253 
10254         SELECT  s.old_schedule_date,
10255                 --cal2.calendar_date,
10256 		s.implement_dock_date,
10257                 s.disposition_id,         --purchase_order_id,
10258                 s.po_line_id,
10259                 s.order_number            --po_number
10260         INTO    lv_old_need_by_date,
10261                 lv_new_need_by_date,
10262                 lv_po_header_id,
10263                 lv_po_line_id,
10264                 lv_po_number
10265         FROM    --msc_calendar_dates cal1,
10266                 --msc_calendar_dates cal2,
10267                 msc_trading_partners mp,
10268                 msc_system_items msi,
10269                 msc_supplies s,
10270                 msc_plan_organizations_v orgs
10271         WHERE   --cal1.sr_instance_id = mp.sr_instance_id
10272         --AND     cal1.calendar_code = mp.calendar_code
10273         --AND     cal1.exception_set_id = mp.calendar_exception_set_id
10274         --AND     cal1.calendar_date = trunc(NVL(s.implement_date,s.new_schedule_date))
10275        -- AND     cal2.sr_instance_id = cal1.sr_instance_id
10276         --AND     cal2.calendar_code = cal1.calendar_code
10277         --AND     cal2.exception_set_id = cal1.exception_set_id
10278        -- AND     cal2.seq_num = GREATEST(1,NVL(cal1.seq_num, cal1.prior_seq_num) -                  NVL(msi.postprocessing_lead_time, 0))
10279              mp.sr_tp_id = msi.organization_id
10280         AND     mp.sr_instance_id = msi.sr_instance_id
10281         AND     mp.partner_type= 3
10282         AND     msi.inventory_item_id = s.inventory_item_id
10283         AND     msi.plan_id = s.plan_id
10284         AND     msi.organization_id = s.organization_id
10285         AND     msi.sr_instance_id = s.sr_instance_id
10286         AND     s.release_errors is NULL
10287         AND     s.po_line_id IS NOT NULL
10288         AND     s.order_type = 1
10289         AND     s.organization_id = orgs.planned_organization
10290         AND     s.plan_id = orgs.plan_id
10291         AND     orgs.organization_id = arg_owning_org_id
10292         AND     orgs.owning_sr_instance = arg_owning_instance
10293         AND     orgs.plan_id = arg_plan_id
10294         AND     orgs.planned_organization = decode(arg_log_org_id,
10295                         arg_owning_org_id, orgs.planned_organization,
10296                         arg_log_org_id)
10297         AND     orgs.sr_instance_id = arg_org_instance
10298         AND     s.transaction_id = arg_transaction_id
10299         and (s.releasable = RELEASABLE or s.releasable is null )
10300         and  (s.batch_id = g_batch_id or (v_batch_id_populated = 2 and s.batch_id is null) );
10301 
10302       EXCEPTION
10303         WHEN NO_DATA_FOUND THEN
10304           null;
10305       END;
10306 
10307 
10308         lv_return_code:= 0;
10309 
10310       IF lv_po_line_id IS NOT NULL THEN
10311 
10312         lv_sql_stmt:=
10313            'BEGIN'
10314         ||'  IF po_reschedule_pkg.reschedule'||arg_dblink||'('
10315                                           ||'   :lv_old_need_by_date,'
10316                                           ||'   :lv_new_need_by_date,'
10317                                           ||'   :lv_po_header_id,'
10318                                           ||'   :lv_po_line_id,'
10319                                           ||'   :lv_po_number) THEN'
10320         ||'     :lv_return_code:= 1;'
10321         ||'  END IF;'
10322         ||' END;';
10323 
10324         EXECUTE IMMEDIATE lv_sql_stmt
10325                 USING  IN lv_old_need_by_date,
10326                        IN lv_new_need_by_date,
10327                        IN lv_po_header_id,
10328                        IN lv_po_line_id,
10329                        IN lv_po_number,
10330                        OUT lv_return_code;
10331        END IF;
10332 
10333         update msc_supplies
10334            set releasable = RELEASE_ATTEMPTED
10335                ,batch_id   = nvl(batch_id,g_batch_id)
10336          where plan_id = arg_plan_id
10337            and transaction_id= arg_transaction_id;
10338 
10339        RETURN lv_return_code;
10340 
10341 END reschedule_po_wf;
10342 
10343 /*USAF
10344 This procedure will handle the release of Planned Repair Work orders
10345 as eam/cmro jobs */
10346 PROCEDURE load_eam_cmro_jobs
10347 ( p_arg_plan_id			IN      NUMBER
10348 , p_arg_log_org_id 		IN 	NUMBER
10349 , p_arg_org_instance              IN      NUMBER
10350 , p_arg_owning_org_id 		IN 	NUMBER
10351 , p_arg_owning_instance           IN      NUMBER
10352 , p_arg_user_id 			IN 	NUMBER
10353 , p_arg_wip_group_id              IN      NUMBER
10354 , p_arg_mode                      IN      VARCHAR2
10355 , p_arg_transaction_id            IN      NUMBER
10356 , p_l_apps_ver                    IN      VARCHAR2
10357 , p_loaded_eam_jobs IN OUT NOCOPY NUMBER
10358 , p_loaded_cmro_jobs IN OUT NOCOPY NUMBER
10359 )
10360 IS
10361    lv_loaded_jobs NUMBER;
10362 
10363    TYPE NumTab  IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
10364 
10365    lv_transaction_id          NumTab;
10366    lv_instance_id             NumTab;
10367    lv_details_enabled         NumTab;
10368    lv_Agg_details             NumTab;
10369    Lv_org_id                  Numtab;
10370    lv_plan_id                 NumTab;
10371    lv_job_count               NUMBER;
10372    lv_release_details         NUMBER;
10373    lv_inflate_wip             NUMBER;
10374    lv_round_primary_item      NumTab;
10375 
10376    lv_wip_start_qty_in         NumTab;
10377    lv_wip_start_qty_out        NumTab;
10378    lv_new_order_qty            NumTab;
10379    lv_implement_qty            NumTab;
10380    lv_qty_scrapped             NumTab;
10381    lv_qty_completed            NumTab;
10382    lv_net_qty                  NumTab;
10383    lv_repair_module            NumTab;
10384    orig_net_qty                NUMBER;
10385 CURSOR c_count(p_arg_wip_group_id number ) is
10386 select count(*) loaded_jobs
10387 ,maintenance_object_source
10388 from msc_wip_job_schedule_interface
10389 where group_id =  p_arg_wip_group_id*-1
10390 and load_type = CMRO_EAM_MASS_LOAD
10391 group by maintenance_object_source;
10392 
10393 CURSOR c_plan_type(p_plan_id number) IS
10394 select plan_type
10395 from msc_plans a
10396 where
10397 plan_id = p_plan_id;
10398 
10399 l_sub_comp_count NUMBER;
10400 l_count   NUMBER := 0;
10401 l_plan_type  number := 0;
10402 
10403 BEGIN
10404 
10405     --  get plan type
10406     OPEN c_plan_type(p_arg_plan_id);
10407     FETCH c_plan_type INTO l_plan_type;
10408     CLOSE c_plan_type;
10409 
10410     IF(gv_auto_release = 1) THEN
10411       -- Auto release, we need to delete the records in the
10412        -- source table AHL_REQ_SUPPLY_SCHEDULE for the items
10413        -- planned in the plan. Hence, we are inserting a record
10414        -- into msc_wip_job_schedule_interface for all items in the
10415        -- plan with a start_quantity of -23453.
10416 
10417 
10418     INSERT INTO MSC_WIP_JOB_SCHEDULE_INTERFACE
10419              (
10420             last_update_date,
10421             last_updated_by,
10422             creation_date,
10423             created_by,
10424             group_id,
10425             source_code,
10426             organization_id,
10427             load_type,
10428             start_quantity)
10429       SELECT
10430             SYSDATE,
10431             p_arg_user_id,
10432             SYSDATE,
10433             p_arg_user_id,
10434             p_arg_wip_group_id*-1,
10435             'MSC',
10436             msi.organization_id,
10437             257,
10438             -23453
10439         from msc_system_items msi
10440             ,msc_item_attributes mia
10441       where msi.inventory_item_id = mia.inventory_item_id
10442         and msi.organization_id = mia.organization_id
10443         and mia.repair_module = CMRO
10444         and msi.plan_id =  p_arg_plan_id;
10445 
10446     END IF;
10447 
10448    l_sql_stmt := ' SELECT s.transaction_id,
10449                    s.sr_instance_id,
10450                    s.organization_id,
10451          s.plan_id,
10452          msa.repair_module,
10453          decode( ' || p_l_apps_ver ||' ,4,msi.rounding_control_type,3,msi.rounding_control_type,2),
10454          NVL(s.wip_start_quantity, 0),
10455          NVL(s.new_order_quantity, 0),
10456          NVL(s.implement_quantity, 0)
10457      FROM msc_supplies s,
10458           msc_plan_organizations_v orgs,
10459           msc_plan_buckets mpb,
10460           msc_system_items msi,
10461           msc_plans mp,
10462           msc_item_attributes msa
10463     WHERE mp.plan_id = :p_arg_plan_id
10464     AND   s.release_errors is NULL
10465     AND   s.implement_quantity > 0
10466     AND   s.organization_id = orgs.planned_organization
10467     AND   s.sr_instance_id = orgs.sr_instance_id
10468     AND   s.plan_id = orgs.plan_id
10469     AND   msi.inventory_item_id = s.inventory_item_id
10470     AND   msi.plan_id = s.plan_id
10471     AND   msi.organization_id = s.organization_id
10472     AND   msi.sr_instance_id = s.sr_instance_id
10473     AND   msi.inventory_item_id = msa.inventory_item_id
10474     AND   msi.organization_id = msa.organization_id
10475     --AND   msi.plan_id = msa.plan_id
10476     AND   msi.sr_instance_id = msa.sr_instance_id
10477     AND   msa.simulation_set_id = mp.item_simulation_set_id
10478     AND   msa.plan_id = -1
10479     AND   msa.sr_instance_id = mp.sr_instance_id
10480 --    and   msa.repair_module in '||'''EAM'''||'
10481     and   msa.repair_module in ('||EAM|| ','||CMRO||')'||'
10482     AND   orgs.plan_id = mp.plan_id
10483     AND   orgs.organization_id = :p_arg_owning_org_id
10484     AND   orgs.owning_sr_instance = :p_arg_owning_instance
10485     AND   orgs.sr_instance_id = :p_arg_org_instance
10486     and   s.plan_id = mpb.plan_id
10487     and  (nvl(s.implement_date,s.new_schedule_date) between mpb.bkt_start_date and mpb.bkt_end_date)
10488     and   s.new_wip_start_date IS NOT NULL
10489     and (s.releasable = ' || RELEASABLE || ' or s.releasable is null )';
10490 
10491     IF  v_batch_id_populated = 2 THEN
10492         l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id is NULL ';
10493     ELSE
10494         l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id = ' || g_batch_id || ' ' ;
10495     END IF;
10496 
10497     IF p_arg_log_org_id <> p_arg_owning_org_id THEN
10498             l_sql_stmt :=  l_sql_stmt ||'      AND     orgs.planned_organization = ' || p_arg_log_org_id || ' ' ;
10499     END IF;
10500 
10501     IF p_arg_mode IS NULL THEN
10502         l_sql_stmt :=  l_sql_stmt ||'  AND s.load_type = ' || CMRO_EAM_MASS_LOAD || ' ';
10503         IF v_msc_released_only_by_user = 1 THEN
10504             l_sql_stmt :=  l_sql_stmt || ' AND s.last_updated_by = ' || v_user_id || ' ' ;
10505         END IF;
10506     ELSIF p_arg_mode = 'WF_BATCH' THEN
10507         l_sql_stmt :=  l_sql_stmt ||'  AND s.load_type = ' || CMRO_EAM_MASS_LOAD || ' ';
10508     ELSIF p_arg_mode = 'WF' THEN
10509         l_sql_stmt :=  l_sql_stmt ||'      AND s.transaction_id = ' || p_arg_transaction_id  || '  ' ;
10510     END IF;
10511 
10512     lv_job_count:= 0;
10513     IF p_arg_mode IS NULL OR p_arg_mode = 'WF_BATCH' OR p_arg_mode = 'WF' THEN
10514         EXECUTE IMMEDIATE l_sql_stmt
10515         BULK COLLECT
10516                                 INTO lv_transaction_id,
10517                                             lv_instance_id,
10518                                             lv_org_id,
10519                                             lv_plan_id,
10520                                             lv_repair_module,
10521                                             lv_round_primary_item,
10522                                             lv_wip_start_qty_in,
10523                                             lv_new_order_qty,
10524                                             lv_implement_qty
10525                                    USING  p_arg_plan_id
10526                                             ,p_arg_owning_org_id
10527                                             ,p_arg_owning_instance
10528                                             ,p_arg_org_instance;
10529 
10530         lv_job_count:= SQL%ROWCOUNT;
10531     END IF;
10532 
10533     FOR k in 1..lv_job_count
10534        Loop
10535 
10536 
10537            -- The user changes the implement quantity at the time of release.
10538            -- We might need a factor (reverse cum yield) here to
10539            -- inflate the start quantity.
10540 
10541            lv_net_qty(k) := lv_implement_qty (k) ;
10542 
10543            orig_net_qty := lv_new_order_qty (k) ;
10544            IF (orig_net_qty <= 0.000001) THEN
10545                orig_net_qty := 1;
10546            END IF;
10547 
10548            lv_wip_start_qty_out(k) := lv_net_qty(k) *
10549                             (lv_wip_start_qty_in(k) / orig_net_qty);
10550 
10551       End loop;
10552 
10553     -- -----------------------------------------------------------------------
10554     --Insert into the interface table for both eam and cmro new work orders
10555     -- -----------------------------------------------------------------------
10556 
10557 
10558     FORALL j IN 1..lv_job_count
10559     INSERT INTO msc_wip_job_schedule_interface
10560             (last_update_date,
10561             last_updated_by,
10562             last_update_login,
10563             creation_date,
10564             created_by,
10565             group_id,
10566             source_code,
10567             source_line_id,
10568             organization_id,
10569             organization_type,
10570             load_type,
10571             maintenance_object_source,
10572             status_type,
10573             first_unit_start_date,
10574             last_unit_completion_date,
10575             primary_item_id,
10576             class_code,
10577             job_name,
10578             firm_planned_flag,
10579             start_quantity,
10580 	          net_quantity,
10581             demand_class,
10582             project_id,
10583             task_id,
10584 	          schedule_group_id,
10585        	    build_sequence,
10586 	          line_id,
10587 	          alternate_bom_designator,
10588 	          alternate_routing_designator,
10589 	          end_item_unit_number,
10590 	          process_phase,
10591 	          process_status,
10592             bom_reference_id,
10593             routing_reference_id,
10594             BILL_RTG_EXPLOSION_FLAG,
10595             HEADER_ID,
10596             uom_code,
10597             SR_INSTANCE_ID
10598             , schedule_priority
10599             , requested_completion_date
10600             )
10601     SELECT  SYSDATE,
10602             p_arg_user_id,
10603             s.last_update_login,
10604             decode(tp.organization_type,2,s.creation_date,SYSDATE),
10605             p_arg_user_id,
10606             p_arg_wip_group_id*-1,   --We multiply the group_id by -1 for
10607                                      --order_type = 79. Since all the records in
10608                                      --this select are order_type = 79, we do
10609                                      -- not need a decode here
10610             'MSC',
10611             s.transaction_id,
10612             msi.organization_id,
10613             tp.organization_type,
10614             CMRO_EAM_MASS_LOAD,
10615             lv_repair_module(j), --s.maintenance_object_source,
10616             decode(tp.organization_type,2,1,s.implement_status_code),
10617             s.new_wip_start_date ,
10618             s.implement_date,
10619             msi.sr_inventory_item_id,
10620             s.implement_wip_class_code,
10621             s.implement_job_name,
10622             s.implement_firm,
10623             decode(tp.organization_type,
10624                 1,lv_wip_start_qty_out(j),
10625                NULL),
10626            lv_net_qty(j),
10627 --	    s.implement_quantity,
10628             s.implement_demand_class,
10629             s.implement_project_id,
10630             s.implement_task_id,
10631 	          s.implement_schedule_group_id,
10632 	          s.implement_build_sequence,
10633        	    s.implement_line_id,
10634 	          s.implement_alternate_bom,
10635 	          s.implement_alternate_routing,
10636  	          s.implement_unit_number,
10637 	          2,
10638 	          1,
10639             DECODE( tp.organization_type,
10640                     2, mpe.bill_sequence_id,
10641                     NULL),
10642             DECODE( tp.organization_type,
10643                     2, mpe.routing_sequence_id,
10644                     NULL),
10645             'Y',
10646             s.transaction_id,
10647             nvl(s.implement_uom_code,msi.uom_code),
10648             s.sr_instance_id
10649             , s.schedule_priority
10650             , nvl(s.requested_completion_date, s.need_by_date)
10651       FROM  msc_trading_partners    tp,
10652             msc_parameters          param,
10653             msc_system_items        msi,
10654             msc_process_effectivity mpe,
10655             msc_supplies            s
10656     WHERE   tp.sr_tp_id= msi.organization_id
10657     AND     tp.sr_instance_id= msi.sr_instance_id
10658     AND     tp.partner_type=3
10659     AND     param.organization_id = msi.organization_id
10660     AND     param.sr_instance_id  = msi.sr_instance_id
10661     AND     msi.inventory_item_id = s.inventory_item_id
10662     AND     msi.plan_id = s.plan_id
10663     AND     msi.organization_id = s.organization_id
10664     AND     msi.sr_instance_id = s.sr_instance_id
10665     AND     mpe.plan_id(+)= s.plan_id
10666     AND     mpe.process_sequence_id(+)= s.process_seq_id
10667     AND     s.transaction_id= lv_transaction_id(j)
10668     AND     s.sr_instance_id= lv_instance_id(j)
10669     AND     s.plan_id= p_arg_plan_id;
10670 
10671     IF SQL%ROWCOUNT > 0
10672     THEN
10673         lv_loaded_jobs := SQL%ROWCOUNT;
10674 
10675     ELSE
10676         lv_loaded_jobs := 0;
10677 
10678     END IF;
10679 
10680 
10681     p_loaded_eam_jobs :=0;
10682     p_loaded_cmro_jobs :=0;
10683       FOR c_rec in c_count(p_arg_wip_group_id) LOOP
10684 
10685          IF (c_rec.maintenance_object_source = 1) THEN
10686           p_loaded_eam_jobs :=  c_rec.loaded_jobs;
10687 
10688          ELSIF (c_rec.maintenance_object_source = 2) THEN
10689           p_loaded_cmro_jobs :=  c_rec.loaded_jobs;
10690 
10691          END IF;
10692       END LOOP ;
10693 
10694 
10695 IF c_count%ISOPEN THEN CLOSE c_count; END IF;
10696 
10697 	 FORALL j IN 1..lv_job_count
10698         update msc_supplies
10699            set releasable = RELEASE_ATTEMPTED
10700                ,batch_id   = nvl(batch_id,g_batch_id)
10701         where plan_id = p_arg_plan_id
10702            and transaction_id= lv_transaction_id(j);
10703 
10704 END load_eam_cmro_jobs;
10705 
10706 /*USAF
10707 This procedure will handle the reschedule recommendations of eam jobs */
10708 FUNCTION reschedule_eam_jobs
10709 ( arg_plan_id			IN      NUMBER
10710 , arg_log_org_id 		IN 	NUMBER
10711 , arg_org_instance              IN      NUMBER
10712 , arg_owning_org_id 		IN 	NUMBER
10713 , arg_owning_instance           IN      NUMBER
10714 , arg_user_id 			IN 	NUMBER
10715 , arg_wip_group_id              IN      NUMBER
10716 , arg_mode                      IN      VARCHAR2
10717 , arg_transaction_id            IN      NUMBER
10718 , l_apps_ver                    IN      VARCHAR2
10719 , arg_load_type                 IN      NUMBER DEFAULT NULL
10720 )RETURN NUMBER IS
10721 
10722  lv_resched_jobs NUMBER;
10723 
10724 
10725   TYPE NumTab  IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
10726  --TYPE RIDTab  IS TABLE OF ROWID  INDEX BY BINARY_INTEGER;
10727 
10728    lv_transaction_id           NumTab;
10729    lv_instance_id              NumTab;
10730    lv_details_enabled          NumTab;
10731    lv_job_count                NUMBER;
10732    lv_plan_id                  NumTab;
10733    lv_org_id                   Numtab;
10734    lv_agg_details              NumTab;
10735 
10736 -- BUG 9085180
10737 
10738    lv_wip_start_qty_in         NumTab;
10739    lv_wip_start_qty_out        NumTab;
10740    lv_new_order_qty            NumTab;
10741    lv_implement_qty            NumTab;
10742    lv_qty_scrapped             NumTab;
10743    lv_qty_completed            NumTab;
10744    lv_net_qty                  NumTab;
10745 
10746    orig_net_qty                NUMBER;
10747 
10748 CURSOR c_release_dtls(p_transaction_id number,
10749                       p_plan_id  number) IS
10750 SELECT count(1)
10751 FROM msc_supplies
10752 WHERE implement_alternate_bom is not null
10753       and transaction_id = p_transaction_id
10754       and rownum < 2
10755 UNION
10756 SELECT count(1)
10757 from msc_exception_details excp
10758 where excp.plan_id = p_plan_id
10759       and number1 = p_transaction_id
10760       and excp.exception_type = 33
10761       and rownum < 2;
10762 
10763 
10764 CURSOR c_plan_type(p_plan_id number) IS
10765 select plan_type
10766 from msc_plans a
10767 where
10768 plan_id = p_plan_id;
10769 
10770 l_count   NUMBER := 0;
10771 l_plan_type  number := 0;
10772 
10773 
10774 BEGIN
10775 
10776   -- we release the discrete job, only if it doesn't use aggregate resources
10777   -- bug 1252659 fix, replace rowid by
10778   --       (transaction_id,sr_instance_id,plan_id)
10779 
10780    l_sql_stmt := ' SELECT s.transaction_id,
10781           s.sr_instance_id,
10782           decode((mp.daily_material_constraints
10783                   + mp.daily_resource_constraints
10784                   + mp.weekly_material_constraints
10785                   + mp.weekly_resource_constraints
10786                   + mp.period_material_constraints
10787                   + mp.period_resource_constraints),
10788                   12, 2,
10789                DECODE(mpb.bucket_type,1,
10790                          Decode(greatest(abs(s.implement_quantity -
10791                                            s.new_order_quantity),
10792                                        0.000001),
10793                                  0.000001,
10794                                  DECODE( s.implement_date,
10795                                          s.new_schedule_date, 1,
10796                                          2),
10797                                 2),
10798                       2)),
10799            s.organization_id,
10800            s.plan_id,
10801            NVL(s.wip_start_quantity, 0),
10802            NVL(s.new_order_quantity, 0),
10803            NVL(s.implement_quantity, 0),
10804            NVL(s.qty_scrapped, 0),
10805            NVL(s.qty_completed, 0)
10806     FROM msc_supplies s,
10807           msc_plans mp,
10808           msc_plan_organizations_v orgs,
10809           msc_plan_buckets mpb
10810     WHERE s.release_errors is NULL
10811     AND   s.implement_quantity > 0
10812     AND   s.organization_id = orgs.planned_organization
10813     AND   s.sr_instance_id = orgs.sr_instance_id
10814     AND   s.plan_id = :arg_plan_id
10815     AND   orgs.plan_id = :arg_plan_id
10816     AND   orgs.organization_id = :arg_owning_org_id
10817     AND   orgs.owning_sr_instance = :arg_owning_instance
10818     AND   orgs.sr_instance_id = :arg_org_instance
10819     and   s.plan_id = mpb.plan_id
10820     and   s.plan_id = mp.plan_id
10821     AND ( (DECODE(s.disposition_status_type ,
10822                   2,DECODE(SIGN( mp.curr_start_date-s.new_schedule_date),
10823                            1,mp.curr_start_date,
10824                            DECODE(SIGN(s.new_schedule_date-mp.curr_cutoff_date),
10825                                  1,mp.curr_cutoff_date,
10826                                  s.new_schedule_date)),
10827                   s.new_schedule_date ) BETWEEN mpb.bkt_start_date
10828                                         AND mpb.bkt_end_date))
10829     and (s.releasable = ' || RELEASABLE || ' or s.releasable is null )
10830     and s.maintenance_object_source =1 ';
10831 
10832     IF  v_batch_id_populated = 2 THEN
10833         l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id is NULL ';
10834     ELSE
10835         l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id = ' || g_batch_id || ' ' ;
10836     END IF;
10837 
10838     IF arg_log_org_id <> arg_owning_org_id THEN
10839             l_sql_stmt :=  l_sql_stmt ||'      AND     orgs.planned_organization = ' || arg_log_org_id || ' ' ;
10840     END IF;
10841 
10842     IF arg_mode IS NULL THEN
10843         l_sql_stmt :=  l_sql_stmt ||'      AND s.load_type = ' || arg_load_type || ' ';
10844         IF v_msc_released_only_by_user = 1 THEN
10845             l_sql_stmt :=  l_sql_stmt || ' AND s.last_updated_by = ' || v_user_id || ' ' ;
10846         END IF;
10847     ELSIF arg_mode = 'WF_BATCH' THEN
10848         l_sql_stmt :=  l_sql_stmt ||'      AND s.load_type = ' || arg_load_type || ' ';
10849     ELSIF arg_mode = 'WF' THEN
10850         l_sql_stmt :=  l_sql_stmt ||'      AND s.transaction_id = ' || arg_transaction_id  || '  ' ;
10851     END IF;
10852 
10853     lv_job_count:= 0;
10854     IF arg_mode IS NULL OR arg_mode = 'WF_BATCH' OR arg_mode = 'WF' THEN
10855         EXECUTE IMMEDIATE l_sql_stmt
10856              BULK COLLECT            INTO lv_transaction_id,
10857                                           lv_instance_id,
10858                                           lv_details_enabled,
10859                                           lv_org_id,
10860                                           lv_plan_id,
10861                                           lv_wip_start_qty_in,
10862                                           lv_new_order_qty,
10863                                           lv_implement_qty,
10864                                           lv_qty_scrapped,
10865                                           lv_qty_completed
10866                                         USING  arg_plan_id
10867                                             ,arg_plan_id
10868                                             ,arg_owning_org_id
10869                                             ,arg_owning_instance
10870                                             ,arg_org_instance    ;
10871         lv_job_count:= SQL%ROWCOUNT;
10872     END IF;
10873 
10874 
10875     -- -----------------------------------------------------------------------
10876     -- Perform the wip discrete job mass load
10877     -- -----------------------------------------------------------------------
10878     --    Due to we only give PLANNED components, BILL_RTG_EXPLOSION_FLAG
10879     --    is set to 'Y'.
10880 
10881     --DBMS_OUTPUT.PUT_LINE('LOAD_JOB');
10882 
10883     FOR k in 1..lv_job_count
10884        Loop
10885                 Begin
10886                   SELECT 2
10887                   Into lv_agg_details(k)
10888                   FROM msc_department_resources deptres,
10889                        msc_resource_requirements resreq
10890                  WHERE resreq.sr_instance_id= lv_instance_id(k)
10891                    AND resreq.supply_id = lv_transaction_id(k)
10892                    AND resreq.organization_id= lv_org_id(k)
10893                    AND resreq.plan_id   = lv_plan_id(k)
10894                    AND resreq.parent_id   = 2
10895                    AND deptres.plan_id  = resreq.plan_id
10896                    AND deptres.sr_instance_id= resreq.sr_instance_id
10897                    AND deptres.resource_id= resreq.resource_id
10898                    AND deptres.department_id= resreq.department_id
10899                    AND deptres.organization_id= resreq.organization_id
10900                    AND deptres.aggregate_resource_flag= 1
10901                    AND rownum=1;
10902                   Exception
10903                   When no_data_found
10904                   then
10905                   lv_agg_details(k) := 1;
10906                   End;
10907 
10908         -- BUG 9085180
10909         -- Compute the lv_wip_start_qty_out and lv_net_qty.
10910 
10911         IF (lv_details_enabled(k) = 1) THEN
10912 
10913            -- There is no change in date/quantity at the time of release
10914            -- and we are populating the details, so we will pass the
10915            -- WIP start and net quantity.
10916            -- In this case, lv_new_order_qty = lv_implement_qty
10917 
10918            lv_wip_start_qty_out(k) := lv_wip_start_qty_in(k);
10919            lv_net_qty(k) := lv_new_order_qty (k) +
10920                             lv_qty_scrapped(k) + lv_qty_completed (k);
10921 
10922         ELSIF (abs(lv_implement_qty(k) - lv_new_order_qty(k)) <= 0.000001) THEN
10923 
10924            -- There is no change in quantity at the time of release
10925            -- This is a proper date change, since we are not passing the
10926            -- details, we will not populate the start and net quantity
10927            -- in the WIP interface table.
10928            -- For unconstrained plan, should we pass qty?
10929 
10930            lv_wip_start_qty_out(k) :=  NULL;
10931            lv_net_qty(k) := NULL;
10932 
10933         ELSE
10934 
10935            -- The user changes the implement quantity at the time of release.
10936            -- We might need a factor (reverse cum yield) here to
10937            -- inflate the start quantity.
10938 
10939            lv_net_qty(k) := lv_implement_qty (k) +
10940                             lv_qty_scrapped(k) + lv_qty_completed (k);
10941 
10942            orig_net_qty := (lv_new_order_qty (k) +
10943                             lv_qty_scrapped(k) +
10944                             lv_qty_completed (k));
10945            IF (orig_net_qty <= 0.000001) THEN
10946                orig_net_qty := 1;
10947            END IF;
10948 
10949            lv_wip_start_qty_out(k) := lv_net_qty(k) *
10950                                       (lv_wip_start_qty_in(k) / orig_net_qty);
10951 
10952         END IF;
10953 
10954        End Loop;
10955 
10956     FORALL j IN 1..lv_job_count
10957     INSERT INTO msc_wip_job_schedule_interface
10958             (last_update_date,
10959             last_updated_by,
10960             last_update_login,
10961             creation_date,
10962             created_by,
10963             group_id,
10964             source_code,
10965             organization_id,
10966             organization_type,
10967             status_type,
10968             load_type,
10969             maintenance_object_source,
10970             last_unit_completion_date,
10971             first_unit_start_date,
10972             bom_revision_date,
10973             routing_revision_date,
10974             job_name,
10975             firm_planned_flag,
10976             start_quantity,   -- bug 1229891: net_quantity
10977             net_quantity,
10978             wip_entity_id,
10979             demand_class,
10980             project_id,
10981             task_id,
10982 	          schedule_group_id,
10983 	          build_sequence,
10984             line_id,
10985             alternate_bom_designator,
10986 	          alternate_routing_designator,
10987 	          end_item_unit_number,
10988             process_phase,
10989 	          process_status,
10990             BILL_RTG_EXPLOSION_FLAG,
10991             HEADER_ID,
10992             SR_INSTANCE_ID,
10993             uom_code, --Outbound Changes for XML
10994             PRIMARY_ITEM_ID,
10995             source_line_id --Outbound Changes for XML
10996             , schedule_priority -- dsr
10997             , requested_completion_date -- dsr
10998             )
10999     SELECT  SYSDATE,
11000             arg_user_id,
11001             s.last_update_login,
11002             decode(tp.organization_type,2,s.creation_date,SYSDATE),
11003             arg_user_id,
11004             arg_wip_group_id,
11005             'MSC',
11006             msi.organization_id,
11007             tp.organization_type,
11008             DECODE(NVL(s.implement_status_code, s.wip_status_code),
11009                    JOB_CANCELLED,JOB_CANCELLED,NULL),
11010             arg_load_type,
11011             1,
11012             s.implement_date,
11013             s.new_wip_Start_Date,
11014             NULL,
11015             NULL,
11016             s.implement_job_name,
11017             s.implement_firm,
11018             DECODE( tp.organization_type,
11019                     1,lv_wip_start_qty_out(j) ,
11020                     NULL),
11021             lv_net_qty(j),
11022             s.disposition_id,
11023             s.implement_demand_class,
11024             s.implement_project_id,
11025             s.implement_task_id,
11026 	          s.implement_schedule_group_id,
11027             s.implement_build_sequence,
11028             s.implement_line_id,
11029        	    s.implement_alternate_bom,
11030 	          s.implement_alternate_routing,
11031 	          s.implement_unit_number,
11032             2,
11033 	          1,
11034             'Y',
11035             s.transaction_id,
11036             s.sr_instance_id,
11037             nvl(s.implement_uom_code,msi.uom_code),
11038             msi.sr_inventory_item_id,
11039             s.transaction_id
11040             , s.schedule_priority
11041             , s.requested_completion_date
11042     FROM    msc_trading_partners tp,
11043             msc_parameters param,
11044             msc_system_items msi,
11045             msc_supplies     s,
11046             msc_plan_organizations_v orgs
11047     WHERE   tp.sr_tp_id= msi.organization_id
11048     AND     tp.sr_instance_id= msi.sr_instance_id
11049     AND     tp.partner_type=3
11050     AND     param.organization_id = msi.organization_id
11051     AND    param.sr_instance_id = msi.sr_instance_id
11052     AND    msi.inventory_item_id = s.inventory_item_id
11053     AND    msi.plan_id = s.plan_id
11054     AND    msi.organization_id = s.organization_id
11055     AND    msi.sr_instance_id  = s.sr_instance_id
11056     AND    s.release_errors is NULL
11057     AND    s.organization_id = orgs.planned_organization
11058     AND    s.sr_instance_id = orgs.sr_instance_id
11059     AND    s.plan_id = orgs.plan_id
11060     AND    orgs.organization_id = arg_owning_org_id
11061     AND    orgs.owning_sr_instance = arg_owning_instance
11062     AND    orgs.plan_id = arg_plan_id
11063     AND    orgs.planned_organization = decode(arg_log_org_id,
11064                                          arg_owning_org_id, orgs.planned_organization,
11065                                           arg_log_org_id)
11066      AND    orgs.sr_instance_id = arg_org_instance
11067       AND     ((arg_mode is null and s.load_type = arg_load_type) or
11068                 (arg_mode = 'WF' and s.transaction_id = arg_transaction_id))
11069     AND     s.transaction_id= lv_transaction_id(j)
11070     AND     s.sr_instance_id= lv_instance_id(j);
11071 
11072     IF SQL%ROWCOUNT > 0
11073     THEN
11074         lv_resched_jobs := SQL%ROWCOUNT;
11075 
11076     ELSE
11077         lv_resched_jobs := 0;
11078 
11079     END IF;
11080 
11081     -- ------------------------------------------------------------------------
11082     -- Perform the wip discrete job mass resched for the details
11083     -- ------------------------------------------------------------------------
11084 
11085     -- the details are populated, only if the implement date and quantity
11086     -- are the same as the planned date and quantity
11087 
11088     /* If the current plan is DRP plan then check the following conditions
11089        to determin whether we need to release the details or not
11090        1) For planned orders that do not use
11091           substitute components/alternateboms, only the
11092           header level information is released */
11093 
11094     l_count := 1;
11095 
11096     /* If it is not a drp plan then l_count = 1 by default so details will be released */
11097     IF l_count = 1 THEN  /* Release details as well */
11098        -- OPERATIONS
11099        FORALL j IN 1..lv_job_count
11100        INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
11101            (last_update_date,
11102             last_updated_by,
11103             last_update_login,
11104             creation_date,
11105             created_by,
11106             organization_type,
11107             organization_id,
11108             group_id,
11109             parent_header_id,
11110             operation_seq_num,
11111             department_id,
11112             description,
11113             standard_operation_id,
11114             first_unit_start_date,
11115             first_unit_completion_date,
11116             last_unit_start_date,
11117             last_unit_completion_date,
11118             minimum_transfer_quantity,
11119             count_point_type,
11120             backflush_flag,
11121             SUBSTITUTION_TYPE,
11122             LOAD_TYPE,
11123             --maintenance_object_source,
11124 	          process_phase,
11125 	          process_status,
11126             SR_INSTANCE_ID,
11127             operation_seq_id, --Outbound Changes for XML
11128             WIP_ENTITY_ID
11129             , eam_flag -- dsr
11130             )
11131     SELECT  SYSDATE,
11132             arg_user_id,
11133             s.last_update_login,
11134             SYSDATE,
11135             arg_user_id,
11136             tp.organization_type,
11137             s.organization_id,
11138             arg_wip_group_id,
11139             s.transaction_id,
11140             resreq.OPERATION_SEQ_NUM,
11141             NULL,            --department_id,
11142             NULL,   --description,
11143             NULL,   --standard_operation_id,
11144             min(resreq.START_DATE),   --first_unit_start_date,
11145             min(resreq.START_DATE),   --first_unit_completion_date,
11146             max(resreq.END_DATE),     --last_unit_start_date,
11147             max(resreq.END_DATE),     --last_unit_completion_date,
11148             NULL,   --minimum_transfer_quantity,
11149             NULL,   --count_point_type,
11150             NULL,   --backflush_flag,
11151             SUBST_CHANGE,
11152             LT_OPERATION,
11153             --1,
11154             2,
11155             1,
11156             s.sr_instance_id,
11157             resreq.operation_sequence_id, --Outbound Changes for XML
11158             s.disposition_id
11159             , decode(arg_load_type, EAM_RESCHEDULE_WORK_ORDER, SYS_YES, SYS_NO) -- dsr
11160       FROM  msc_trading_partners   tp,
11161             msc_resource_requirements resreq,
11162             msc_parameters          param,
11163             msc_system_items        msi,
11164             msc_supplies            s
11165     WHERE   tp.sr_tp_id= msi.organization_id
11166     AND     tp.sr_instance_id= msi.sr_instance_id
11167     AND     tp.partner_type=3
11168     AND     resreq.sr_instance_id= s.sr_instance_id
11169     AND     resreq.organization_id= s.organization_id
11170     AND     resreq.supply_id = s.transaction_id
11171     AND     resreq.plan_id   = s.plan_id
11172     AND     resreq.parent_id   = 2
11173     AND     param.organization_id = msi.organization_id
11174     AND     param.sr_instance_id  = msi.sr_instance_id
11175     AND     msi.inventory_item_id = s.inventory_item_id
11176     AND     msi.plan_id = s.plan_id
11177     AND     msi.organization_id = s.organization_id
11178     AND     msi.sr_instance_id = s.sr_instance_id
11179     AND     s.transaction_id= lv_transaction_id(j)
11180     AND     s.sr_instance_id= lv_instance_id(j)
11181     AND     s.plan_id= arg_plan_id
11182     AND     lv_details_enabled(j)= 1
11183     AND     lv_agg_details(j) = 1
11184     GROUP BY
11185             SYSDATE,
11186             arg_user_id,
11187             tp.organization_type,
11188             s.organization_id,
11189             s.last_update_login,
11190             SYSDATE,
11191             arg_user_id,
11192             arg_wip_group_id,
11193             s.transaction_id,
11194             resreq.OPERATION_SEQ_NUM,
11195             NULL,            --department_id,
11196             LT_OPERATION,    --load_type,
11197             NULL,            --description,
11198             NULL,            --standard_operation_id,
11199             NULL,            --minimum_transfer_quantity,
11200             NULL,            --count_point_type,
11201             NULL,            --backflush_flag,
11202             s.sr_instance_id,
11203             resreq.operation_sequence_id,
11204             s.disposition_id;
11205 
11206 
11207     --DBMS_OUTPUT.PUT_LINE('OPERATION_RESOURCE');
11208 
11209     -- OPERATION RESOURCES
11210     FORALL j IN 1..lv_job_count
11211     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
11212           ( last_update_date,
11213             last_updated_by,
11214             last_update_login,
11215             creation_date,
11216             created_by,
11217             organization_type,
11218             organization_id,
11219             group_id,
11220             parent_header_id,
11221             operation_seq_num,
11222             -- resource_seq_num,
11223             alternate_num,
11224             resource_id_old,
11225             resource_id_new,
11226             usage_rate_or_amount,
11227             scheduled_flag,
11228             applied_resource_units,   --
11229             applied_resource_value,   --
11230             uom_code,
11231             basis_type,     --
11232             activity_id,    --
11233             autocharge_type,     --
11234             standard_rate_flag,  --
11235             start_date,
11236             completion_date,
11237             assigned_units,
11238             SUBSTITUTION_TYPE,
11239             LOAD_TYPE,
11240             --maintenance_object_source,
11241 	          process_phase,
11242 	          process_status,
11243             description,
11244             SR_INSTANCE_ID,
11245             operation_seq_id, -- Outbound Changes for XML
11246             wip_entity_id,
11247             resource_hours,
11248             department_id
11249             -- dsr: following 9 columns
11250             , firm_flag
11251             , setup_id
11252             , group_sequence_id
11253             , group_sequence_number
11254             , batch_id
11255             , maximum_assigned_units
11256             , parent_seq_num
11257             , eam_flag
11258             , resource_seq_num
11259             , schedule_seq_num
11260 	  )
11261     SELECT  SYSDATE,
11262             arg_user_id,
11263             s.last_update_login,
11264             SYSDATE,
11265             arg_user_id,
11266             tp.organization_type,
11267             s.organization_id,
11268             arg_wip_group_id,
11269             s.transaction_id,
11270             resreq.OPERATION_SEQ_NUM,
11271             -- resreq.RESOURCE_SEQ_NUM,
11272             resreq.ALTERNATE_NUM,
11273             resreq.RESOURCE_ID,
11274             resreq.RESOURCE_ID,
11275             decode(resreq.parent_seq_num, null, decode(l_apps_ver,'4',to_number(null),'3',TO_NUMBER(NULL),resreq.RESOURCE_HOURS/decode(resreq.basis_type,2,1,decode(s.new_order_quantity,0,1,s.new_order_quantity))), resreq.usage_rate),
11276             decode(nvl(resreq.schedule_flag,1),-23453,1,1,1,resreq.schedule_flag),
11277             NULL,
11278             NULL,
11279             NULL, -- v_hour_uom, bug 14752020
11280             resreq.basis_type,
11281             NULL,
11282             NULL,
11283             NULL,
11284             nvl(resreq.firm_start_date,resreq.START_DATE),
11285             nvl(resreq.firm_end_date,resreq.END_DATE),
11286             -- decode(l_apps_ver,'3',TO_NUMBER(NULL),resreq.ASSIGNED_UNITS),
11287             resreq.ASSIGNED_UNITS,
11288             decode(resreq.parent_seq_num,null,SUBST_CHANGE,SUBST_ADD),
11289             -- SUBST_CHANGE,
11290             LT_RESOURCE,
11291             --1,
11292             2,
11293             1,
11294             NULL,
11295             s.sr_instance_id,
11296             resreq.operation_sequence_id, -- Outbound Changes for XML
11297             s.disposition_id,
11298             resreq.resource_hours,
11299             resreq.department_id
11300              -- dsr: following 9 columns
11301             , resreq.firm_flag
11302             , resreq.setup_id
11303             , resreq.group_sequence_id
11304             , resreq.group_sequence_number
11305             , resreq.batch_number
11306             , resreq.maximum_assigned_units
11307             , resreq.parent_seq_num
11308             , DECODE(arg_load_type, EAM_RESCHEDULE_WORK_ORDER, SYS_YES, SYS_NO) -- dsr: eam_flag
11309             , resreq.orig_resource_seq_num
11310             , resreq.resource_seq_num
11311       FROM  msc_trading_partners   tp,
11312             msc_resource_requirements resreq,
11313             msc_parameters          param,
11314             msc_system_items        msi,
11315             msc_supplies            s
11316     WHERE   tp.sr_tp_id= msi.organization_id
11317     AND     tp.sr_instance_id= msi.sr_instance_id
11318     AND     tp.partner_type=3
11319     AND     resreq.sr_instance_id= s.sr_instance_id
11320     AND     resreq.organization_id= s.organization_id
11321     AND     resreq.supply_id = s.transaction_id
11322     AND     resreq.plan_id   = s.plan_id
11323     AND     resreq.parent_id   = 2
11324     AND     param.organization_id = msi.organization_id
11325     AND     param.sr_instance_id  = msi.sr_instance_id
11326     AND     msi.inventory_item_id = s.inventory_item_id
11327     AND     msi.plan_id = s.plan_id
11328     AND     msi.organization_id = s.organization_id
11329     AND     msi.sr_instance_id = s.sr_instance_id
11330     AND     s.transaction_id= lv_transaction_id(j)
11331     AND     s.sr_instance_id= lv_instance_id(j)
11332     AND     s.plan_id= arg_plan_id
11333     AND     lv_details_enabled(j)= 1
11334     AND     lv_agg_details(j) = 1 ;
11335 
11336 
11337     /* UPDATE EXISTING COMPONENTS                      *
11338      |    We should set inventory_item_id_new to NULL  |
11339      *                                                 */
11340     FORALL j IN 1..lv_job_count
11341     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
11342             (last_update_date,
11343             last_updated_by,
11344             last_update_login,
11345             creation_date,
11346             created_by,
11347             organization_type,
11348             organization_id,
11349             group_id,
11350             parent_header_id,
11351             operation_seq_num,
11352             inventory_item_id_old,
11353             inventory_item_id_new,
11354             basis_type,
11355             quantity_per_assembly,
11356             component_yield_factor,
11357             department_id,
11358             wip_supply_type,
11359             date_required,
11360             required_quantity,
11361             quantity_issued,
11362             supply_subinventory,
11363             supply_locator_id,
11364             mrp_net_flag,
11365             mps_required_quantity,
11366             mps_date_required,
11367             SUBSTITUTION_TYPE,
11368             LOAD_TYPE,
11369             --maintenance_object_source,
11370 	          process_phase,
11371 	          process_status,
11372             description,
11373             SR_INSTANCE_ID,
11374             uom_code, --Outbound Changes for XML
11375             wip_entity_id,
11376              eam_flag -- dsr
11377             )
11378     SELECT  SYSDATE,
11379             arg_user_id,
11380             s.last_update_login,
11381             SYSDATE,
11382             arg_user_id,
11383             tp.organization_type,
11384             s.organization_id,
11385             arg_wip_group_id,
11386             s.transaction_id,
11387             nvl(md.op_seq_num,1),
11388             icomp.sr_inventory_item_id,
11389             decode(l_apps_ver,'4',to_number(null),'3',null,icomp.sr_inventory_item_id),
11390             decode(md.component_scaling_type,1,NULL,md.component_scaling_type),
11391             decode(l_apps_ver,'4',TO_NUMBER(NULL),'3',TO_NUMBER(NULL),(md.USING_REQUIREMENT_QUANTITY/s.implement_quantity)),
11392             md.component_yield_factor,
11393             TO_NUMBER(NULL),       --Department_ID
11394             NVL(GET_WIP_SUPPLY_TYPE(s.plan_id, s.sr_instance_id,s.process_seq_id,
11395                                     s.inventory_item_id,md.inventory_item_id,s.organization_id),
11396                     icomp.wip_supply_type),
11397             md.USING_ASSEMBLY_DEMAND_DATE,
11398             decode(l_apps_ver,'4',TO_NUMBER(NULL),'3',TO_NUMBER(NULL),md.USING_REQUIREMENT_QUANTITY),
11399             TO_NUMBER(NULL),  --quantity_issued
11400             TO_CHAR(NULL),     -- Sub Inventory
11401             TO_NUMBER(NULL),   -- Locator ID
11402             1,                 -- MRP_NET_FLAG
11403             decode(l_apps_ver,'4',to_number(null),'3',TO_NUMBER(NULL),md.USING_REQUIREMENT_QUANTITY),
11404             md.USING_ASSEMBLY_DEMAND_DATE,
11405             SUBST_CHANGE,
11406             LT_COMPONENT,
11407             --1,
11408             2,
11409             1,
11410             TO_CHAR(NULL),
11411             s.sr_instance_id,
11412             nvl(s.implement_uom_code, msi.uom_code),
11413             s.disposition_id
11414             , DECODE(arg_load_type, EAM_RESCHEDULE_WORK_ORDER, SYS_YES, SYS_NO) -- dsr: eam_flag
11415       FROM  msc_trading_partners    tp,
11416             msc_system_items        icomp,
11417             msc_demands             md,
11418             msc_parameters          param,
11419             msc_system_items        msi,
11420             msc_supplies            s
11421     WHERE   /*
11422             ( md.SUBST_ITEM_FLAG <> 1
11423               OR md.SUBST_ITEM_FLAG IS NULL) */
11424             not exists (select 'x'
11425                         from msc_exception_details excp
11426                         where excp.plan_id = msi.plan_id
11427                         and excp.number1 = s.transaction_id
11428                         and excp.inventory_item_id = msi.inventory_item_id
11429                         and excp.organization_id = msi.organization_id
11430                         and excp.sr_instance_id = msi.sr_instance_id
11431                         and excp.exception_type = 33
11432                         and excp.number2 = md.inventory_item_id)
11433     AND     tp.sr_tp_id= msi.organization_id
11434     AND     tp.sr_instance_id= msi.sr_instance_id
11435     AND     tp.partner_type=3
11436     AND     icomp.inventory_item_id= md.inventory_item_id
11437     AND     icomp.organization_id= md.organization_id
11438     AND     icomp.sr_instance_id= md.sr_instance_id
11439     AND     icomp.plan_id= md.plan_id
11440     AND     nvl(icomp.wip_supply_type,0) <> 6
11441     AND     md.sr_instance_id= s.sr_instance_id
11442     AND     md.disposition_id= s.transaction_id
11443     AND     md.plan_id= s.plan_id
11444     AND     param.organization_id = msi.organization_id
11445     AND     param.sr_instance_id  = msi.sr_instance_id
11446     AND     msi.inventory_item_id = s.inventory_item_id
11447     AND     msi.plan_id = s.plan_id
11448     AND     msi.organization_id = s.organization_id
11449     AND     msi.sr_instance_id = s.sr_instance_id
11450     AND     s.transaction_id= lv_transaction_id(j)
11451     AND     s.sr_instance_id= lv_instance_id(j)
11452     AND     s.plan_id= arg_plan_id
11453     AND     lv_details_enabled(j)= 1
11454     AND     lv_agg_details(j) = 1;
11455 
11456 
11457     --Load Co-product/by-product component details
11458 
11459     FORALL j IN 1..lv_job_count
11460     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
11461             (last_update_date,
11462             last_updated_by,
11463             last_update_login,
11464             creation_date,
11465             created_by,
11466             organization_type,
11467             organization_id,
11468             group_id,
11469             parent_header_id,
11470             operation_seq_num,
11471             inventory_item_id_old,
11472             inventory_item_id_new,
11473             quantity_per_assembly,
11474             department_id,
11475             wip_supply_type,
11476             date_required,
11477             required_quantity,
11478             quantity_issued,
11479             supply_subinventory,
11480             supply_locator_id,
11481             mrp_net_flag,
11482             mps_required_quantity,
11483             mps_date_required,
11484             SUBSTITUTION_TYPE,
11485             LOAD_TYPE,
11486             --maintenance_object_source,
11487 	          process_phase,
11488 	          process_status,
11489             description,
11490             SR_INSTANCE_ID,
11491 --            operation_seq_id, -- Outbound Changes for XML
11492             uom_code, --Outbound Changes for XML
11493             wip_entity_id
11494             , eam_flag -- dsr
11495             )
11496     SELECT  SYSDATE,
11497             arg_user_id,
11498             s.last_update_login,
11499             SYSDATE,
11500             arg_user_id,
11501             tp.organization_type,
11502             s.organization_id,
11503             arg_wip_group_id,
11504             s.transaction_id,
11505             nvl(co.operation_seq_num,1),
11506             icomp.sr_inventory_item_id,
11507             decode(l_apps_ver,'4',to_number(null),'3',null,icomp.sr_inventory_item_id),
11508             decode(l_apps_ver,'4',to_number(null),'3',TO_NUMBER(NULL),(co.new_order_quantity/s.implement_quantity)),
11509             TO_NUMBER(NULL),       --Department_ID
11510             NVL(GET_WIP_SUPPLY_TYPE(s.plan_id, s.sr_instance_id,s.process_seq_id,
11511                                     s.inventory_item_id,co.inventory_item_id,s.organization_id),
11512                     icomp.wip_supply_type),
11513             co.new_schedule_date,
11514             decode(l_apps_ver,'4',to_number(null),'3',TO_NUMBER(NULL),co.new_order_quantity),
11515             TO_NUMBER(NULL),  --quantity_issued
11516             TO_CHAR(NULL),     -- Sub Inventory
11517             TO_NUMBER(NULL),   -- Locator ID
11518             1,                 -- MRP_NET_FLAG
11519             decode(l_apps_ver,'4',to_number(null),'3',TO_NUMBER(NULL),co.new_order_quantity),
11520             co.new_schedule_date,
11521             SUBST_CHANGE,
11522             LT_COMPONENT,
11523             --1,
11524             2,
11525             1,
11526             TO_CHAR(NULL),
11527             s.sr_instance_id,
11528             nvl(s.implement_uom_code, msi.uom_code),
11529             s.disposition_id
11530              , DECODE(arg_load_type, EAM_RESCHEDULE_WORK_ORDER, SYS_YES, SYS_NO) -- dsr: eam_flag
11531       FROM  msc_trading_partners    tp,
11532             msc_system_items        icomp,
11533             msc_supplies            co,
11534             msc_parameters          param,
11535             msc_system_items        msi,
11536             msc_supplies            s
11537     WHERE   /*not exists (select 'x'
11538                         from msc_exception_details excp
11539                         where excp.plan_id = msi.plan_id
11540                         and excp.number1 = s.transaction_id
11541                         and excp.inventory_item_id = msi.inventory_item_id
11542                         and excp.organization_id = msi.organization_id
11543                         and excp.sr_instance_id = msi.sr_instance_id
11544                         and excp.exception_type = 33
11545                         and excp.number2 = co.inventory_item_id)
11546     AND     */
11547             tp.sr_tp_id             = msi.organization_id
11548     AND     tp.sr_instance_id       = msi.sr_instance_id
11549     AND     tp.partner_type         = 3
11550     AND     icomp.inventory_item_id = co.inventory_item_id
11551     AND     icomp.organization_id   = co.organization_id
11552     AND     icomp.sr_instance_id    = co.sr_instance_id
11553     AND     icomp.plan_id           = co.plan_id
11554     AND     co.sr_instance_id       = s.sr_instance_id
11555     AND     co.disposition_id       = s.transaction_id
11556     AND     co.plan_id              = s.plan_id
11557     AND     co.order_type           = 14 -- Discrete Job Co-products/by-products.
11558     AND     param.organization_id   = msi.organization_id
11559     AND     param.sr_instance_id    = msi.sr_instance_id
11560     AND     msi.inventory_item_id   = s.inventory_item_id
11561     AND     msi.plan_id             = s.plan_id
11562     AND     msi.organization_id     = s.organization_id
11563     AND     msi.sr_instance_id      = s.sr_instance_id
11564     AND     s.transaction_id        = lv_transaction_id(j)
11565     AND     s.sr_instance_id        = lv_instance_id(j)
11566     AND     s.plan_id               = arg_plan_id
11567     AND     lv_details_enabled(j)   = 1
11568     AND     lv_agg_details(j)       = 1
11569     AND     tp.organization_type    = 2;
11570 
11571 
11572     -- RESOURCE USAGE
11573     FORALL j IN 1..lv_job_count
11574     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
11575           ( last_update_date,
11576             last_updated_by,
11577             last_update_login,
11578             creation_date,
11579             created_by,
11580             organization_type,
11581             organization_id,
11582             group_id,
11583             parent_header_id,
11584             operation_seq_num,
11585             -- resource_seq_num,
11586             alternate_num,
11587             resource_id_old,
11588             resource_id_new,
11589             usage_rate_or_amount,
11590             scheduled_flag,
11591             applied_resource_units,   --
11592             applied_resource_value,   --
11593             uom_code,
11594             basis_type,     --
11595             activity_id,    --
11596             autocharge_type,     --
11597             standard_rate_flag,  --
11598             start_date,
11599             completion_date,
11600             assigned_units,
11601             SUBSTITUTION_TYPE,
11602             LOAD_TYPE,
11603             --maintenance_object_source,
11604 	    process_phase,
11605 	    process_status,
11606             description,
11607             SR_INSTANCE_ID,
11608             operation_seq_id, -- Outbound Changes for XML
11609             wip_entity_id,
11610             department_id,
11611             resource_hours
11612             , eam_flag -- dsr
11613             , resource_seq_num
11614             , schedule_seq_num
11615             ,parent_seq_num
11616             )
11617     SELECT  SYSDATE,
11618             arg_user_id,
11619             s.last_update_login,
11620             SYSDATE,
11621             arg_user_id,
11622             tp.organization_type,
11623             s.organization_id,
11624             arg_wip_group_id,
11625             s.transaction_id,
11626             resreq.OPERATION_SEQ_NUM,
11627            -- resreq.RESOURCE_SEQ_NUM,
11628             resreq.ALTERNATE_NUM,
11629             resreq.RESOURCE_ID,
11630             resreq.RESOURCE_ID,
11631             decode(resreq.parent_seq_num, null, decode(l_apps_ver,'4',to_number(null),'3',TO_NUMBER(NULL),resreq.RESOURCE_HOURS/decode(resreq.basis_type,2,1,decode(s.new_order_quantity,0,1,s.new_order_quantity))), resreq.usage_rate),
11632             decode(nvl(resreq.schedule_flag,1),-23453,1,1,1,resreq.schedule_flag),
11633             NULL,
11634             NULL,
11635             v_hour_uom,
11636             resreq.basis_type,
11637             NULL,
11638             NULL,
11639             NULL,
11640             nvl(resreq.firm_start_date,resreq.START_DATE),
11641             nvl(resreq.firm_end_date,resreq.END_DATE),
11642             -- decode(l_apps_ver,'3',TO_NUMBER(NULL),resreq.ASSIGNED_UNITS),
11643             resreq.ASSIGNED_UNITS,
11644             SUBST_CHANGE,
11645             LT_RESOURCE_USAGE,
11646             --1,
11647             2,
11648             1,
11649             NULL,
11650             s.sr_instance_id,
11651             resreq.operation_sequence_id, -- Outbound Changes for XML
11652             s.disposition_id,
11653             resreq.department_id,
11654             resreq.resource_hours
11655             , DECODE(arg_load_type, EAM_RESCHEDULE_WORK_ORDER, SYS_YES, SYS_NO) -- dsr: eam_flag
11656             , resreq.orig_resource_seq_num
11657             , resreq.resource_seq_num
11658             ,resreq.parent_seq_num
11659       FROM  msc_trading_partners   tp,
11660             msc_resource_requirements resreq,
11661             msc_parameters          param,
11662             msc_system_items        msi,
11663             msc_supplies            s
11664     WHERE   tp.sr_tp_id= msi.organization_id
11665     AND     tp.sr_instance_id= msi.sr_instance_id
11666     AND     tp.partner_type=3
11667     AND     resreq.sr_instance_id= s.sr_instance_id
11668     AND     resreq.organization_id= s.organization_id
11669     AND     resreq.supply_id = s.transaction_id
11670     AND     resreq.plan_id   = s.plan_id
11671     AND     resreq.parent_id   = 1
11672     AND     param.organization_id = msi.organization_id
11673     AND     param.sr_instance_id  = msi.sr_instance_id
11674     AND     msi.inventory_item_id = s.inventory_item_id
11675     AND     msi.plan_id = s.plan_id
11676     AND     msi.organization_id = s.organization_id
11677     AND     msi.sr_instance_id = s.sr_instance_id
11678     AND     s.transaction_id= lv_transaction_id(j)
11679     AND     s.sr_instance_id= lv_instance_id(j)
11680     AND     s.plan_id= arg_plan_id
11681     AND     lv_details_enabled(j)= 1
11682     AND     lv_agg_details(j) = 1
11683      AND     tp.organization_type IN (1, 2);  -- 1 - discrete wip org; 2 - opm org
11684 
11685     -- dsr starts here
11686 --    print_debug_info('OPERATION RESOURCE_INSTANCES');
11687 	-- OPERATION RESOURCE_INSTANCES
11688 
11689     FORALL j IN 1..lv_job_count
11690     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
11691           ( last_update_date,
11692             last_updated_by,
11693             last_update_login,
11694             creation_date,
11695             created_by,
11696             organization_type,
11697             organization_id,
11698             group_id,
11699             parent_header_id,
11700             operation_seq_num,
11701            -- resource_seq_num,
11702             resource_id_old, -- rawasthi
11703 	    resource_id_new, -- xx resource_id, ???
11704             RESOURCE_INSTANCE_ID,
11705             start_date,
11706             completion_date,
11707             SUBSTITUTION_TYPE,
11708             LOAD_TYPE,
11709             --maintenance_object_source,
11710             process_phase,
11711             process_status,
11712             SR_INSTANCE_ID,
11713             operation_seq_id,
11714             -- FIRM_FLAG,
11715             resource_hours,
11716             department_id,
11717 	    -- setup_id,
11718 	    SERIAL_NUMBER,
11719             group_sequence_id,
11720             group_sequence_number,
11721             batch_id,
11722             resource_seq_num
11723             , schedule_seq_num
11724             , wip_entity_id -- for reschedule
11725             , eam_flag -- dsr
11726             , parent_seq_num
11727  )
11728     SELECT
11729           SYSDATE,
11730           arg_user_id,
11731           s.last_update_login,
11732           SYSDATE,
11733           arg_user_id,
11734           tp.organization_type,
11735           s.organization_id,
11736           arg_wip_group_id,
11737           s.transaction_id,
11738           resreq.OPERATION_SEQ_NUM,
11739        --   resreq.RESOURCE_SEQ_NUM,
11740           resreq.resource_id,
11741           resreq.resource_id,
11742   -- jguo dep_res_inst.RESOURCE_INST_ID,
11743           dep_res_inst.RES_INSTANCE_ID ,
11744           nvl(resreq.firm_start_date,res_instreq.START_DATE),
11745           nvl(resreq.firm_end_date,res_instreq.END_DATE),
11746           SUBST_ADD, -- jguo SUBST_CHANGE,
11747           LT_RESOURCE_INSTANCE,
11748           --1,
11749           2,
11750           1,
11751           s.sr_instance_id,
11752           resreq.operation_sequence_id,
11753        -- res_instreq.firm_flag,
11754           resreq.resource_hours,
11755           resreq.department_id,
11756          -- resreq.department_id / 2, rawasthi
11757        -- res_instreq.setup_id,
11758           dep_res_inst.serial_number,
11759           resreq.group_sequence_id,
11760           resreq.group_sequence_number,
11761           res_instreq.batch_number,    --- sbala res_instreq.res_inst_batch_id,
11762           resreq.orig_resource_seq_num,
11763           resreq.resource_seq_num
11764       --  resreq.alternate_num,
11765         , s.disposition_id -- for reschedule
11766         , DECODE(arg_load_type, EAM_RESCHEDULE_WORK_ORDER, SYS_YES, SYS_NO) -- dsr: eam_flag
11767         , resreq.parent_seq_num
11768    FROM
11769           msc_trading_partners   tp,
11770           msc_resource_requirements resreq,
11771           msc_resource_instance_reqs res_instreq,
11772           msc_dept_res_instances dep_res_inst,
11773           msc_supplies            s
11774     WHERE
11775          tp.sr_tp_id=s.organization_id
11776  AND     tp.sr_instance_id= s.sr_instance_id
11777  AND     tp.partner_type=3
11778  AND     resreq.sr_instance_id= s.sr_instance_id
11779  AND     resreq.organization_id= s.organization_id
11780  AND     resreq.supply_id = s.transaction_id
11781  AND     resreq.plan_id   = s.plan_id
11782 -- dsr AND     resreq.transaction_id = res_instreq.res_inst_transaction_id
11783  AND     resreq.resource_seq_num = res_instreq.resource_seq_num
11784  AND     resreq.operation_seq_num = res_instreq.operation_seq_num
11785  AND     resreq.resource_id = res_instreq.resource_id
11786  AND     resreq.supply_id = res_instreq.supply_id
11787  AND     resreq.sr_instance_id = res_instreq.sr_instance_id
11788  AND     resreq.plan_id = res_instreq.plan_id
11789  AND     resreq.parent_id = res_instreq.parent_id  --rawasthi
11790  AND     resreq.start_date = res_instreq.start_date
11791  AND     resreq.parent_id   = 2
11792  AND     resreq.resource_id <> -1
11793  AND     resreq.department_id <> -1
11794 -- dsr   AND     res_instreq.res_instance_id = dep_res_inst.dept_resource_inst_id
11795  AND     res_instreq.plan_id = dep_res_inst.plan_id
11796  AND     res_instreq.sr_instance_id = dep_res_inst.sr_instance_id
11797  AND     res_instreq.department_id = dep_res_inst.department_id
11798  AND     res_instreq.resource_id = dep_res_inst.resource_id
11799 /* anuj: serail number and resource_instance id joins */
11800  AND     res_instreq.serial_number = dep_res_inst.serial_number
11801  AND     res_instreq.res_instance_id = dep_res_inst.res_instance_id
11802  AND     s.transaction_id= lv_transaction_id(j)
11803  AND     s.sr_instance_id= lv_instance_id(j)
11804  AND     s.plan_id= arg_plan_id
11805  AND     lv_details_enabled(j)= 1
11806  AND     lv_agg_details(j) = 1
11807     ;
11808 
11809 --print_debug_info('reschedule_wip_discrete_jobs: 888 sql%rowcount = '|| SQL%ROWCOUNT);
11810 
11811 
11812     -- RESOURCE INSTANCE USAGES
11813 --print_debug_info('RESOURCE INSTANCE USAGES');
11814 	FORALL j IN 1..lv_job_count
11815     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
11816           ( last_update_date,
11817             last_updated_by,
11818             last_update_login,
11819             creation_date,
11820             created_by,
11821             organization_type,
11822             organization_id,
11823             group_id,
11824             parent_header_id,
11825             operation_seq_num,
11826            -- resource_seq_num,
11827             resource_id_old, -- rawasthi
11828 	    resource_id_new, -- xx RESOURCE_ID, ???
11829             RESOURCE_INSTANCE_ID,
11830             start_date,
11831             completion_date,
11832             SUBSTITUTION_TYPE,
11833             LOAD_TYPE,
11834             --maintenance_object_source,
11835             process_phase,
11836             process_status,
11837             SR_INSTANCE_ID,
11838             operation_seq_id,
11839             FIRM_FLAG,
11840             resource_hours,
11841             department_id,
11842             serial_number,
11843             resource_seq_num,
11844             schedule_seq_num
11845          -- alternate_num
11846      	  , wip_entity_id -- for reschedule
11847 	  , eam_flag -- dsr
11848 	  , assigned_units -- jguo
11849 	  ,parent_seq_num
11850  )
11851     SELECT
11852            SYSDATE,
11853            arg_user_id,
11854            s.last_update_login,
11855            SYSDATE,
11856            arg_user_id,
11857            tp.organization_type,
11858            s.organization_id,
11859            arg_wip_group_id,
11860            s.transaction_id,
11861            resreq.OPERATION_SEQ_NUM,
11862        --    resreq.RESOURCE_SEQ_NUM,
11863            resreq.RESOURCE_ID,
11864            resreq.RESOURCE_ID,
11865   -- jguo  dep_res_inst.RESOURCE_INST_ID,
11866            dep_res_inst.RES_INSTANCE_ID ,
11867            nvl(resreq.firm_start_date,res_instreq.START_DATE),
11868            nvl(resreq.firm_end_date,res_instreq.END_DATE),
11869            SUBST_ADD, -- jguo SUBST_CHANGE,
11870            LT_RESOURCE_INST_USAGE,
11871            --1,
11872            2,
11873            1,
11874            s.sr_instance_id,
11875            resreq.operation_sequence_id,
11876            resreq.firm_flag,
11877            res_instreq.resource_instance_hours,
11878            resreq.department_id,
11879           -- resreq.department_id / 2, rawasthi
11880            dep_res_inst.serial_number,
11881            resreq.orig_resource_seq_num,
11882            resreq.resource_seq_num
11883         -- resreq.alternate_num
11884          , s.disposition_id -- for reschedule
11885          , DECODE(arg_load_type, EAM_RESCHEDULE_WORK_ORDER, SYS_YES, SYS_NO) -- dsr: eam_flag
11886          , 1 -- jguo
11887          ,resreq.parent_seq_num
11888     FROM
11889            msc_trading_partners   tp,
11890            msc_resource_requirements resreq,
11891            msc_resource_instance_reqs res_instreq, -- ??? msc_res_inst_requirements res_instreq,
11892            msc_dept_res_instances dep_res_inst,
11893            msc_supplies            s
11894     WHERE
11895             tp.sr_tp_id=s.organization_id
11896     AND     tp.sr_instance_id= s.sr_instance_id
11897     AND     tp.partner_type=3
11898     AND     resreq.sr_instance_id= s.sr_instance_id
11899     AND     resreq.organization_id= s.organization_id
11900     AND     resreq.supply_id = s.transaction_id
11901     AND     resreq.plan_id   = s.plan_id
11902 -- dsr   AND     resreq.transaction_id = res_instreq.res_inst_transaction_id
11903     AND     resreq.resource_seq_num = res_instreq.resource_seq_num
11904     AND     resreq.operation_seq_num = res_instreq.operation_seq_num
11905     AND     resreq.resource_id = res_instreq.resource_id
11906     AND     resreq.supply_id = res_instreq.supply_id
11907     AND     resreq.sr_instance_id = res_instreq.sr_instance_id
11908     AND     resreq.plan_id = res_instreq.plan_id
11909     AND     resreq.parent_id = res_instreq.parent_id  --rawasthi
11910     AND     resreq.start_date = res_instreq.start_date
11911     AND     resreq.parent_id   = 1
11912     AND     resreq.resource_id <> -1
11913     AND     resreq.department_id <> -1
11914 -- dsr   AND     res_instreq.res_instance_id = dep_res_inst.dept_resource_inst_id
11915     AND     res_instreq.department_id = dep_res_inst.department_id
11916     AND     res_instreq.resource_id = dep_res_inst.resource_id
11917     AND     res_instreq.plan_id = dep_res_inst.plan_id
11918     AND     res_instreq.sr_instance_id = dep_res_inst.sr_instance_id
11919 /* anuj: serail number and resource_instance id joins */
11920     AND     res_instreq.serial_number = dep_res_inst.serial_number
11921     AND     res_instreq.res_instance_id = dep_res_inst.res_instance_id
11922     AND     s.transaction_id= lv_transaction_id(j)
11923     AND     s.sr_instance_id= lv_instance_id(j)
11924     AND     s.plan_id= arg_plan_id
11925     AND     lv_details_enabled(j)= 1
11926     AND     lv_agg_details(j) = 1
11927     ;
11928 
11929 --print_debug_info('reschedule_wip_discrete_jobs: 999 sql%rowcount = '|| SQL%ROWCOUNT);
11930 
11931 
11932 -- Resource Charges
11933 
11934 --print_debug_info('RESOURCE CHARGES');
11935 
11936 FORALL j IN 1..lv_job_count
11937     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
11938           ( last_update_date,
11939             last_updated_by,
11940             last_update_login,
11941             creation_date,
11942             created_by,
11943             organization_type,
11944             organization_id,
11945             group_id,
11946             parent_header_id,
11947             operation_seq_num,
11948             resource_seq_num,
11949 	    alternate_num,
11950             start_date,
11951             completion_date,
11952 	    required_quantity,
11953 	    charge_number,
11954             SR_INSTANCE_ID,
11955             SUBSTITUTION_TYPE,
11956             LOAD_TYPE,
11957             --maintenance_object_source,
11958             process_phase,
11959             process_status
11960           , wip_entity_id -- for reschedule
11961           , eam_flag -- dsr
11962           , resource_id_old
11963           , resource_id_new
11964           , schedule_seq_num
11965        )
11966     SELECT
11967             SYSDATE,
11968             arg_user_id,
11969             s.last_update_login,
11970             SYSDATE,
11971             arg_user_id,
11972             tp.organization_type,
11973             s.organization_id,
11974             arg_wip_group_id,
11975             s.transaction_id,
11976             resreq.OPERATION_SEQ_NUM,
11977             resreq.ORIG_RESOURCE_SEQ_NUM,
11978             resreq.alternate_num,
11979             Chg.charge_start_datetime,
11980             Chg.charge_end_datetime,
11981             chg.charge_quantity, -- Chg.Planned_Charge_Quantity,
11982             Chg.charge_number,
11983             s.sr_instance_id,
11984             SUBST_CHANGE, -- 3
11985             LT_CHARGE_TYPE, --8
11986             --1,
11987             2,
11988             1
11989           , s.disposition_id -- for reschedule
11990           , DECODE(arg_load_type, EAM_RESCHEDULE_WORK_ORDER, SYS_YES, SYS_NO) -- dsr: eam_flag
11991           , resreq.RESOURCE_ID
11992           ,  resreq.RESOURCE_ID
11993           ,  resreq.resource_seq_num
11994      FROM
11995              msc_trading_partners   tp,
11996              msc_resource_requirements resreq,
11997              msc_supplies            s,
11998              msc_resource_charges chg
11999     WHERE
12000             tp.sr_tp_id=s.organization_id
12001     AND     tp.sr_instance_id= s.sr_instance_id
12002     AND     tp.partner_type=3
12003     AND     resreq.sr_instance_id= s.sr_instance_id
12004     AND     resreq.organization_id= s.organization_id
12005     AND     resreq.supply_id = s.transaction_id
12006     AND     resreq.plan_id   = s.plan_id
12007     AND     resreq.transaction_id = chg.res_transaction_id -- chg.transaction_id
12008     AND     resreq.plan_id = chg.plan_id
12009     AND     resreq.sr_instance_id = chg.sr_instance_id
12010     AND     resreq.parent_id   = 2
12011     AND     resreq.resource_id <> -1
12012     AND     resreq.department_id <> -1
12013     AND     s.transaction_id= lv_transaction_id(j)
12014     AND     s.sr_instance_id= lv_instance_id(j)
12015     AND     s.plan_id= arg_plan_id
12016     AND     lv_details_enabled(j)= 1
12017     AND     lv_agg_details(j) = 1
12018 	;
12019 -- dsr ends here
12020    END IF; /* End of l_count = 1 */
12021 
12022     FORALL j IN 1..lv_job_count
12023        update msc_supplies
12024            set releasable = RELEASE_ATTEMPTED
12025                ,batch_id   = nvl(batch_id,g_batch_id)
12026          where plan_id = arg_plan_id
12027            and transaction_id= lv_transaction_id(j);
12028 
12029     RETURN lv_resched_jobs;
12030 
12031 
12032 
12033 END reschedule_eam_jobs;
12034 
12035 /* USAF
12036 This procedure will handle the reschedule recommendations of cmro jobs */
12037 FUNCTION reschedule_cmro_jobs
12038 ( arg_plan_id			IN      NUMBER
12039 , arg_log_org_id 		IN 	NUMBER
12040 , arg_org_instance              IN      NUMBER
12041 , arg_owning_org_id 		IN 	NUMBER
12042 , arg_owning_instance           IN      NUMBER
12043 , arg_user_id 			IN 	NUMBER
12044 , arg_wip_group_id              IN      NUMBER
12045 , arg_mode                      IN      VARCHAR2
12046 , arg_transaction_id            IN      NUMBER
12047 , l_apps_ver                    IN      VARCHAR2
12048 , arg_load_type                 IN      NUMBER DEFAULT NULL
12049 )RETURN NUMBER IS
12050 
12051  lv_resched_jobs NUMBER;
12052 
12053 
12054   TYPE NumTab  IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
12055  --TYPE RIDTab  IS TABLE OF ROWID  INDEX BY BINARY_INTEGER;
12056 
12057    lv_transaction_id           NumTab;
12058    lv_instance_id              NumTab;
12059    lv_details_enabled          NumTab;
12060    lv_job_count                NUMBER;
12061    lv_plan_id                  NumTab;
12062    lv_org_id                   Numtab;
12063    lv_agg_details              NumTab;
12064 
12065 
12066    lv_wip_start_qty_in         NumTab;
12067    lv_wip_start_qty_out        NumTab;
12068    lv_new_order_qty            NumTab;
12069    lv_implement_qty            NumTab;
12070    lv_qty_scrapped             NumTab;
12071    lv_qty_completed            NumTab;
12072    lv_net_qty                  NumTab;
12073 
12074    orig_net_qty                NUMBER;
12075    --l_sql_stmt                  VARCHAR2(5000);
12076 
12077 CURSOR c_release_dtls(p_transaction_id number,
12078                       p_plan_id  number) IS
12079 SELECT count(1)
12080 FROM msc_supplies
12081 WHERE implement_alternate_bom is not null
12082       and transaction_id = p_transaction_id
12083       and rownum < 2
12084 UNION
12085 SELECT count(1)
12086 from msc_exception_details excp
12087 where excp.plan_id = p_plan_id
12088       and number1 = p_transaction_id
12089       and excp.exception_type = 33
12090       and rownum < 2;
12091 
12092 
12093 CURSOR c_plan_type(p_plan_id number) IS
12094 select plan_type
12095 from msc_plans a
12096 where
12097 plan_id = p_plan_id;
12098 
12099 l_count   NUMBER := 0;
12100 l_plan_type  number := 0;
12101 
12102 
12103 BEGIN
12104 
12105   -- we release the discrete job, only if it doesn't use aggregate resources
12106   -- bug 1252659 fix, replace rowid by
12107   --       (transaction_id,sr_instance_id,plan_id)
12108 
12109    l_sql_stmt := ' SELECT s.transaction_id,
12110           s.sr_instance_id,
12111           decode((mp.daily_material_constraints
12112                   + mp.daily_resource_constraints
12113                   + mp.weekly_material_constraints
12114                   + mp.weekly_resource_constraints
12115                   + mp.period_material_constraints
12116                   + mp.period_resource_constraints),
12117                   12, 2,
12118                DECODE(mpb.bucket_type,1,
12119                          Decode(greatest(abs(s.implement_quantity -
12120                                            s.new_order_quantity),
12121                                        0.000001),
12122                                  0.000001,
12123                                  DECODE( s.implement_date,
12124                                          s.new_schedule_date, 1,
12125                                          2),
12126                                 2),
12127                       2)),
12128            s.organization_id,
12129            s.plan_id,
12130            NVL(s.wip_start_quantity, 0),
12131            NVL(s.new_order_quantity, 0),
12132            NVL(s.implement_quantity, 0),
12133            NVL(s.qty_scrapped, 0),
12134            NVL(s.qty_completed, 0)
12135     FROM msc_supplies s,
12136           msc_plans mp,
12137           msc_plan_organizations_v orgs,
12138           msc_plan_buckets mpb
12139     WHERE s.release_errors is NULL
12140     AND   s.implement_quantity > 0
12141     AND   s.organization_id = orgs.planned_organization
12142     AND   s.sr_instance_id = orgs.sr_instance_id
12143     AND   s.plan_id = :arg_plan_id
12144     AND   orgs.plan_id = :arg_plan_id
12145     AND   orgs.organization_id = :arg_owning_org_id
12146     AND   orgs.owning_sr_instance = :arg_owning_instance
12147     AND   orgs.sr_instance_id = :arg_org_instance
12148     and   s.plan_id = mpb.plan_id
12149     and   s.plan_id = mp.plan_id
12150     AND ( (DECODE(s.disposition_status_type ,
12151                   2,DECODE(SIGN( mp.curr_start_date-s.new_schedule_date),
12152                            1,mp.curr_start_date,
12153                            DECODE(SIGN(s.new_schedule_date-mp.curr_cutoff_date),
12154                                  1,mp.curr_cutoff_date,
12155                                  s.new_schedule_date)),
12156                   s.new_schedule_date ) BETWEEN mpb.bkt_start_date
12157                                         AND mpb.bkt_end_date))
12158     and (s.releasable = ' || RELEASABLE || ' or s.releasable is null )
12159     and s.maintenance_object_source =2 ';
12160 
12161     IF  v_batch_id_populated = 2 THEN
12162         l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id is NULL ';
12163     ELSE
12164         l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id = ' || g_batch_id || ' ' ;
12165     END IF;
12166 
12167     IF arg_log_org_id <> arg_owning_org_id THEN
12168             l_sql_stmt :=  l_sql_stmt ||'      AND     orgs.planned_organization = ' || arg_log_org_id || ' ' ;
12169     END IF;
12170 
12171     IF arg_mode IS NULL THEN
12172         l_sql_stmt :=  l_sql_stmt ||'      AND s.load_type = ' || arg_load_type || ' ';
12173         IF v_msc_released_only_by_user = 1 THEN
12174             l_sql_stmt :=  l_sql_stmt || ' AND s.last_updated_by = ' || v_user_id || ' ' ;
12175         END IF;
12176     ELSIF arg_mode = 'WF_BATCH' THEN
12177         l_sql_stmt :=  l_sql_stmt ||'      AND s.load_type = ' || arg_load_type || ' ';
12178     ELSIF arg_mode = 'WF' THEN
12179         l_sql_stmt :=  l_sql_stmt ||'      AND s.transaction_id = ' || arg_transaction_id  || '  ' ;
12180     END IF;
12181 
12182     lv_job_count:= 0;
12183     IF arg_mode IS NULL OR arg_mode = 'WF_BATCH' OR arg_mode = 'WF' THEN
12184         EXECUTE IMMEDIATE l_sql_stmt
12185              BULK COLLECT            INTO lv_transaction_id,
12186                                           lv_instance_id,
12187                                           lv_details_enabled,
12188                                           lv_org_id,
12189                                           lv_plan_id,
12190                                           lv_wip_start_qty_in,
12191                                           lv_new_order_qty,
12192                                           lv_implement_qty,
12193                                           lv_qty_scrapped,
12194                                           lv_qty_completed
12195                                         USING  arg_plan_id
12196                                             ,arg_plan_id
12197                                             ,arg_owning_org_id
12198                                             ,arg_owning_instance
12199                                             ,arg_org_instance    ;
12200         lv_job_count:= SQL%ROWCOUNT;
12201     END IF;
12202 
12203 
12204     -- -----------------------------------------------------------------------
12205     -- Perform the wip discrete job mass load
12206     -- -----------------------------------------------------------------------
12207     --    Due to we only give PLANNED components, BILL_RTG_EXPLOSION_FLAG
12208     --    is set to 'Y'.
12209 
12210     --DBMS_OUTPUT.PUT_LINE('LOAD_JOB');
12211 
12212     FOR k in 1..lv_job_count
12213        Loop
12214                 Begin
12215                   SELECT 2
12216                   Into lv_agg_details(k)
12217                   FROM msc_department_resources deptres,
12218                        msc_resource_requirements resreq
12219                  WHERE resreq.sr_instance_id= lv_instance_id(k)
12220                    AND resreq.supply_id = lv_transaction_id(k)
12221                    AND resreq.organization_id= lv_org_id(k)
12222                    AND resreq.plan_id   = lv_plan_id(k)
12223                    AND resreq.parent_id   = 2
12224                    AND deptres.plan_id  = resreq.plan_id
12225                    AND deptres.sr_instance_id= resreq.sr_instance_id
12226                    AND deptres.resource_id= resreq.resource_id
12227                    AND deptres.department_id= resreq.department_id
12228                    AND deptres.organization_id= resreq.organization_id
12229                    AND deptres.aggregate_resource_flag= 1
12230                    AND rownum=1;
12231                   Exception
12232                   When no_data_found
12233                   then
12234                   lv_agg_details(k) := 1;
12235                   End;
12236 
12237         -- BUG 9085180
12238         -- Compute the lv_wip_start_qty_out and lv_net_qty.
12239 
12240         IF (lv_details_enabled(k) = 1) THEN
12241 
12242            -- There is no change in date/quantity at the time of release
12243            -- and we are populating the details, so we will pass the
12244            -- WIP start and net quantity.
12245            -- In this case, lv_new_order_qty = lv_implement_qty
12246 
12247            lv_wip_start_qty_out(k) := lv_wip_start_qty_in(k);
12248            lv_net_qty(k) := lv_new_order_qty (k) +
12249                             lv_qty_scrapped(k) + lv_qty_completed (k);
12250 
12251         ELSIF (abs(lv_implement_qty(k) - lv_new_order_qty(k)) <= 0.000001) THEN
12252 
12253            -- There is no change in quantity at the time of release
12254            -- This is a proper date change, since we are not passing the
12255            -- details, we will not populate the start and net quantity
12256            -- in the WIP interface table.
12257            -- For unconstrained plan, should we pass qty?
12258 
12259            lv_wip_start_qty_out(k) :=  NULL;
12260            lv_net_qty(k) := NULL;
12261 
12262         ELSE
12263 
12264            -- The user changes the implement quantity at the time of release.
12265            -- We might need a factor (reverse cum yield) here to
12266            -- inflate the start quantity.
12267 
12268            lv_net_qty(k) := lv_implement_qty (k) +
12269                             lv_qty_scrapped(k) + lv_qty_completed (k);
12270 
12271            orig_net_qty := (lv_new_order_qty (k) +
12272                             lv_qty_scrapped(k) +
12273                             lv_qty_completed (k));
12274            IF (orig_net_qty <= 0.000001) THEN
12275                orig_net_qty := 1;
12276            END IF;
12277 
12278            lv_wip_start_qty_out(k) := lv_net_qty(k) *
12279                                       (lv_wip_start_qty_in(k) / orig_net_qty);
12280 
12281         END IF;
12282 
12283        End Loop;
12284 
12285     FORALL j IN 1..lv_job_count
12286     INSERT INTO msc_wip_job_schedule_interface
12287             (last_update_date,
12288             last_updated_by,
12289             last_update_login,
12290             creation_date,
12291             created_by,
12292             group_id,
12293             source_code,
12294             organization_id,
12295             organization_type,
12296             status_type,
12297             load_type,
12298             maintenance_object_source,
12299             last_unit_completion_date,
12300             first_unit_start_date,
12301             bom_revision_date,
12302             routing_revision_date,
12303             job_name,
12304             firm_planned_flag,
12305             start_quantity,   -- bug 1229891: net_quantity
12306             net_quantity,
12307             wip_entity_id,
12308             demand_class,
12309             project_id,
12310             task_id,
12311 	          schedule_group_id,
12312 	          build_sequence,
12313             line_id,
12314             alternate_bom_designator,
12315 	          alternate_routing_designator,
12316 	          end_item_unit_number,
12317             process_phase,
12318 	          process_status,
12319             BILL_RTG_EXPLOSION_FLAG,
12320             HEADER_ID,
12321             SR_INSTANCE_ID,
12322             uom_code, --Outbound Changes for XML
12323             PRIMARY_ITEM_ID,
12324             source_line_id --Outbound Changes for XML
12325             , schedule_priority -- dsr
12326             , requested_completion_date -- dsr
12327             )
12328     SELECT  SYSDATE,
12329             arg_user_id,
12330             s.last_update_login,
12331             decode(tp.organization_type,2,s.creation_date,SYSDATE),
12332             arg_user_id,
12333             arg_wip_group_id*-1,
12334             'MSC',
12335             msi.organization_id,
12336             tp.organization_type,
12337             DECODE(NVL(s.implement_status_code, s.wip_status_code),
12338                    JOB_CANCELLED,JOB_CANCELLED,NULL),
12339             arg_load_type,
12340             2,
12341             s.implement_date,
12342             s.new_wip_Start_Date,
12343             NULL,
12344             NULL,
12345             s.implement_job_name,
12346             s.implement_firm,
12347             DECODE( tp.organization_type,
12348                     1,lv_wip_start_qty_out(j) ,
12349                     NULL),
12350             lv_net_qty(j),
12351             s.disposition_id,
12352             s.implement_demand_class,
12353             s.implement_project_id,
12354             s.implement_task_id,
12355 	          s.implement_schedule_group_id,
12356             s.implement_build_sequence,
12357             s.implement_line_id,
12358        	    s.implement_alternate_bom,
12359 	          s.implement_alternate_routing,
12360 	          s.implement_unit_number,
12361             2,
12362 	          1,
12363             'Y',
12364             s.transaction_id,
12365             s.sr_instance_id,
12366             nvl(s.implement_uom_code,msi.uom_code),
12367             msi.sr_inventory_item_id,
12368             s.transaction_id
12369             , s.schedule_priority
12370             , s.requested_completion_date
12371     FROM    msc_trading_partners tp,
12372             msc_parameters param,
12373             msc_system_items msi,
12374             msc_supplies     s,
12375             msc_plan_organizations_v orgs
12376     WHERE   tp.sr_tp_id= msi.organization_id
12377     AND     tp.sr_instance_id= msi.sr_instance_id
12378     AND     tp.partner_type=3
12379     AND     param.organization_id = msi.organization_id
12380     AND    param.sr_instance_id = msi.sr_instance_id
12381     AND    msi.inventory_item_id = s.inventory_item_id
12382     AND    msi.plan_id = s.plan_id
12383     AND    msi.organization_id = s.organization_id
12384     AND    msi.sr_instance_id  = s.sr_instance_id
12385     AND    s.release_errors is NULL
12386     AND    s.organization_id = orgs.planned_organization
12387     AND    s.sr_instance_id = orgs.sr_instance_id
12388     AND    s.plan_id = orgs.plan_id
12389     AND    orgs.organization_id = arg_owning_org_id
12390     AND    orgs.owning_sr_instance = arg_owning_instance
12391     AND    orgs.plan_id = arg_plan_id
12392     AND    orgs.planned_organization = decode(arg_log_org_id,
12393                                          arg_owning_org_id, orgs.planned_organization,
12394                                           arg_log_org_id)
12395      AND    orgs.sr_instance_id = arg_org_instance
12396       AND     ((arg_mode is null and s.load_type = arg_load_type) or
12397                 (arg_mode = 'WF' and s.transaction_id = arg_transaction_id))
12398     AND     s.transaction_id= lv_transaction_id(j)
12399     AND     s.sr_instance_id= lv_instance_id(j);
12400 
12401     IF SQL%ROWCOUNT > 0
12402     THEN
12403         lv_resched_jobs := SQL%ROWCOUNT;
12404 
12405     ELSE
12406         lv_resched_jobs := 0;
12407 
12408     END IF;
12409 
12410     -- ------------------------------------------------------------------------
12411     -- Perform the wip discrete job mass resched for the details
12412     -- ------------------------------------------------------------------------
12413 
12414     -- the details are populated, only if the implement date and quantity
12415     -- are the same as the planned date and quantity
12416 
12417     /* If the current plan is DRP plan then check the following conditions
12418        to determin whether we need to release the details or not
12419        1) For planned orders that do not use
12420           substitute components/alternateboms, only the
12421           header level information is released */
12422 
12423     l_count := 1;
12424 
12425     /* If it is not a drp plan then l_count = 1 by default so details will be released */
12426     IF l_count = 1 THEN  /* Release details as well */
12427        -- OPERATIONS
12428        FORALL j IN 1..lv_job_count
12429        INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
12430            (last_update_date,
12431             last_updated_by,
12432             last_update_login,
12433             creation_date,
12434             created_by,
12435             organization_type,
12436             organization_id,
12437             group_id,
12438             parent_header_id,
12439             operation_seq_num,
12440             department_id,
12441             description,
12442             standard_operation_id,
12443             first_unit_start_date,
12444             first_unit_completion_date,
12445             last_unit_start_date,
12446             last_unit_completion_date,
12447             minimum_transfer_quantity,
12448             count_point_type,
12449             backflush_flag,
12450             SUBSTITUTION_TYPE,
12451             LOAD_TYPE,
12452             --maintenance_object_source,
12453 	          process_phase,
12454 	          process_status,
12455             SR_INSTANCE_ID,
12456             operation_seq_id, --Outbound Changes for XML
12457             WIP_ENTITY_ID
12458             , eam_flag -- dsr
12459             )
12460     SELECT  SYSDATE,
12461             arg_user_id,
12462             s.last_update_login,
12463             SYSDATE,
12464             arg_user_id,
12465             tp.organization_type,
12466             s.organization_id,
12467             arg_wip_group_id*-1,
12468             s.transaction_id,
12469             resreq.OPERATION_SEQ_NUM,
12470             NULL,            --department_id,
12471             NULL,   --description,
12472             NULL,   --standard_operation_id,
12473             min(resreq.START_DATE),   --first_unit_start_date,
12474             min(resreq.START_DATE),   --first_unit_completion_date,
12475             max(resreq.END_DATE),     --last_unit_start_date,
12476             max(resreq.END_DATE),     --last_unit_completion_date,
12477             NULL,   --minimum_transfer_quantity,
12478             NULL,   --count_point_type,
12479             NULL,   --backflush_flag,
12480             SUBST_CHANGE,
12481             LT_OPERATION,
12482             --1,
12483             2,
12484             1,
12485             s.sr_instance_id,
12486             resreq.operation_sequence_id, --Outbound Changes for XML
12487             s.disposition_id
12488             , decode(arg_load_type, EAM_RESCHEDULE_WORK_ORDER, SYS_YES, SYS_NO) -- dsr
12489       FROM  msc_trading_partners   tp,
12490             msc_resource_requirements resreq,
12491             msc_parameters          param,
12492             msc_system_items        msi,
12493             msc_supplies            s
12494     WHERE   tp.sr_tp_id= msi.organization_id
12495     AND     tp.sr_instance_id= msi.sr_instance_id
12496     AND     tp.partner_type=3
12497     AND     resreq.sr_instance_id= s.sr_instance_id
12498     AND     resreq.organization_id= s.organization_id
12499     AND     resreq.supply_id = s.transaction_id
12500     AND     resreq.plan_id   = s.plan_id
12501     AND     resreq.parent_id   = 2
12502     AND     param.organization_id = msi.organization_id
12503     AND     param.sr_instance_id  = msi.sr_instance_id
12504     AND     msi.inventory_item_id = s.inventory_item_id
12505     AND     msi.plan_id = s.plan_id
12506     AND     msi.organization_id = s.organization_id
12507     AND     msi.sr_instance_id = s.sr_instance_id
12508     AND     s.transaction_id= lv_transaction_id(j)
12509     AND     s.sr_instance_id= lv_instance_id(j)
12510     AND     s.plan_id= arg_plan_id
12511     --AND     lv_details_enabled(j)= 1
12512     --AND     lv_agg_details(j) = 1
12513     GROUP BY
12514             SYSDATE,
12515             arg_user_id,
12516             tp.organization_type,
12517             s.organization_id,
12518             s.last_update_login,
12519             SYSDATE,
12520             arg_user_id,
12521             arg_wip_group_id*-1,
12522             s.transaction_id,
12523             resreq.OPERATION_SEQ_NUM,
12524             NULL,            --department_id,
12525             LT_OPERATION,    --load_type,
12526             NULL,            --description,
12527             NULL,            --standard_operation_id,
12528             NULL,            --minimum_transfer_quantity,
12529             NULL,            --count_point_type,
12530             NULL,            --backflush_flag,
12531             s.sr_instance_id,
12532             resreq.operation_sequence_id,
12533             s.disposition_id;
12534 
12535 
12536     --DBMS_OUTPUT.PUT_LINE('OPERATION_RESOURCE');
12537 
12538     -- OPERATION RESOURCES
12539     FORALL j IN 1..lv_job_count
12540     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
12541           ( last_update_date,
12542             last_updated_by,
12543             last_update_login,
12544             creation_date,
12545             created_by,
12546             organization_type,
12547             organization_id,
12548             group_id,
12549             parent_header_id,
12550             operation_seq_num,
12551             -- resource_seq_num,
12552             alternate_num,
12553             resource_id_old,
12554             resource_id_new,
12555             usage_rate_or_amount,
12556             scheduled_flag,
12557             applied_resource_units,   --
12558             applied_resource_value,   --
12559             uom_code,
12560             basis_type,     --
12561             activity_id,    --
12562             autocharge_type,     --
12563             standard_rate_flag,  --
12564             start_date,
12565             completion_date,
12566             assigned_units,
12567             SUBSTITUTION_TYPE,
12568             LOAD_TYPE,
12569             --maintenance_object_source,
12570 	          process_phase,
12571 	          process_status,
12572             description,
12573             SR_INSTANCE_ID,
12574             operation_seq_id, -- Outbound Changes for XML
12575             wip_entity_id,
12576             resource_hours,
12577             department_id
12578             -- dsr: following 9 columns
12579             , firm_flag
12580             , setup_id
12581             , group_sequence_id
12582             , group_sequence_number
12583             , batch_id
12584             , maximum_assigned_units
12585             , parent_seq_num
12586             , eam_flag
12587             , resource_seq_num
12588             , schedule_seq_num
12589 	  )
12590     SELECT  SYSDATE,
12591             arg_user_id,
12592             s.last_update_login,
12593             SYSDATE,
12594             arg_user_id,
12595             tp.organization_type,
12596             s.organization_id,
12597             arg_wip_group_id*-1,
12598             s.transaction_id,
12599             resreq.OPERATION_SEQ_NUM,
12600             -- resreq.RESOURCE_SEQ_NUM,
12601             resreq.ALTERNATE_NUM,
12602             resreq.RESOURCE_ID,
12603             resreq.RESOURCE_ID,
12604             decode(resreq.parent_seq_num, null, decode(l_apps_ver,'4',to_number(null),'3',TO_NUMBER(NULL),resreq.RESOURCE_HOURS/decode(resreq.basis_type,2,1,decode(s.new_order_quantity,0,1,s.new_order_quantity))), resreq.usage_rate),
12605             decode(nvl(resreq.schedule_flag,1),-23453,1,1,1,resreq.schedule_flag),
12606             NULL,
12607             NULL,
12608             v_hour_uom,
12609             resreq.basis_type,
12610             NULL,
12611             NULL,
12612             NULL,
12613             nvl(resreq.firm_start_date,resreq.START_DATE),
12614             nvl(resreq.firm_end_date,resreq.END_DATE),
12615             -- decode(l_apps_ver,'3',TO_NUMBER(NULL),resreq.ASSIGNED_UNITS),
12616             resreq.ASSIGNED_UNITS,
12617             decode(resreq.parent_seq_num,null,SUBST_CHANGE,SUBST_ADD),
12618             -- SUBST_CHANGE,
12619             LT_RESOURCE,
12620             --1,
12621             2,
12622             1,
12623             NULL,
12624             s.sr_instance_id,
12625             resreq.operation_sequence_id, -- Outbound Changes for XML
12626             s.disposition_id,
12627             resreq.resource_hours,
12628             resreq.department_id
12629              -- dsr: following 9 columns
12630             , resreq.firm_flag
12631             , resreq.setup_id
12632             , resreq.group_sequence_id
12633             , resreq.group_sequence_number
12634             , resreq.batch_number
12635             , resreq.maximum_assigned_units
12636             , resreq.parent_seq_num
12637             , DECODE(arg_load_type, EAM_RESCHEDULE_WORK_ORDER, SYS_YES, SYS_NO) -- dsr: eam_flag
12638             , resreq.orig_resource_seq_num
12639             , resreq.resource_seq_num
12640       FROM  msc_trading_partners   tp,
12641             msc_resource_requirements resreq,
12642             msc_parameters          param,
12643             msc_system_items        msi,
12644             msc_supplies            s
12645     WHERE   tp.sr_tp_id= msi.organization_id
12646     AND     tp.sr_instance_id= msi.sr_instance_id
12647     AND     tp.partner_type=3
12648     AND     resreq.sr_instance_id= s.sr_instance_id
12649     AND     resreq.organization_id= s.organization_id
12650     AND     resreq.supply_id = s.transaction_id
12651     AND     resreq.plan_id   = s.plan_id
12652     AND     resreq.parent_id   = 2
12653     AND     param.organization_id = msi.organization_id
12654     AND     param.sr_instance_id  = msi.sr_instance_id
12655     AND     msi.inventory_item_id = s.inventory_item_id
12656     AND     msi.plan_id = s.plan_id
12657     AND     msi.organization_id = s.organization_id
12658     AND     msi.sr_instance_id = s.sr_instance_id
12659     AND     s.transaction_id= lv_transaction_id(j)
12660     AND     s.sr_instance_id= lv_instance_id(j)
12661     AND     s.plan_id= arg_plan_id
12662     --AND     lv_details_enabled(j)= 1
12663     --AND     lv_agg_details(j) = 1
12664     ;
12665 
12666 
12667     /* UPDATE EXISTING COMPONENTS                      *
12668      |    We should set inventory_item_id_new to NULL  |
12669      *                                                 */
12670     FORALL j IN 1..lv_job_count
12671     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
12672             (last_update_date,
12673             last_updated_by,
12674             last_update_login,
12675             creation_date,
12676             created_by,
12677             organization_type,
12678             organization_id,
12679             group_id,
12680             parent_header_id,
12681             operation_seq_num,
12682             inventory_item_id_old,
12683             inventory_item_id_new,
12684             basis_type,
12685             quantity_per_assembly,
12686             component_yield_factor,
12687             department_id,
12688             wip_supply_type,
12689             date_required,
12690             required_quantity,
12691             quantity_issued,
12692             supply_subinventory,
12693             supply_locator_id,
12694             mrp_net_flag,
12695             mps_required_quantity,
12696             mps_date_required,
12697             SUBSTITUTION_TYPE,
12698             LOAD_TYPE,
12699             --maintenance_object_source,
12700 	          process_phase,
12701 	          process_status,
12702             description,
12703             SR_INSTANCE_ID,
12704             uom_code, --Outbound Changes for XML
12705             wip_entity_id,
12706              eam_flag -- dsr
12707             )
12708     SELECT  SYSDATE,
12709             arg_user_id,
12710             s.last_update_login,
12711             SYSDATE,
12712             arg_user_id,
12713             tp.organization_type,
12714             s.organization_id,
12715             arg_wip_group_id*-1,
12716             s.transaction_id,
12717             nvl(md.op_seq_num,1),
12718             icomp.sr_inventory_item_id,
12719             decode(l_apps_ver,'4',to_number(null),'3',null,icomp.sr_inventory_item_id),
12720             decode(md.component_scaling_type,1,NULL,md.component_scaling_type),
12721             decode(l_apps_ver,'4',TO_NUMBER(NULL),'3',TO_NUMBER(NULL),(md.USING_REQUIREMENT_QUANTITY/s.implement_quantity)),
12722             md.component_yield_factor,
12723             TO_NUMBER(NULL),       --Department_ID
12724             NVL(GET_WIP_SUPPLY_TYPE(s.plan_id, s.sr_instance_id,s.process_seq_id,
12725                                     s.inventory_item_id,md.inventory_item_id,s.organization_id),
12726                     icomp.wip_supply_type),
12727             md.USING_ASSEMBLY_DEMAND_DATE,
12728             decode(l_apps_ver,'4',TO_NUMBER(NULL),'3',TO_NUMBER(NULL),md.USING_REQUIREMENT_QUANTITY),
12729             TO_NUMBER(NULL),  --quantity_issued
12730             TO_CHAR(NULL),     -- Sub Inventory
12731             TO_NUMBER(NULL),   -- Locator ID
12732             1,                 -- MRP_NET_FLAG
12733             decode(l_apps_ver,'4',to_number(null),'3',TO_NUMBER(NULL),md.USING_REQUIREMENT_QUANTITY),
12734             md.USING_ASSEMBLY_DEMAND_DATE,
12735             SUBST_CHANGE,
12736             LT_COMPONENT,
12737             --1,
12738             2,
12739             1,
12740             TO_CHAR(NULL),
12741             s.sr_instance_id,
12742             nvl(s.implement_uom_code, msi.uom_code),
12743             s.disposition_id
12744             , DECODE(arg_load_type, EAM_RESCHEDULE_WORK_ORDER, SYS_YES, SYS_NO) -- dsr: eam_flag
12745       FROM  msc_trading_partners    tp,
12746             msc_system_items        icomp,
12747             msc_demands             md,
12748             msc_parameters          param,
12749             msc_system_items        msi,
12750             msc_supplies            s
12751     WHERE   /*
12752             ( md.SUBST_ITEM_FLAG <> 1
12753               OR md.SUBST_ITEM_FLAG IS NULL)
12754             not exists (select 'x'
12755                         from msc_exception_details excp
12756                         where excp.plan_id = msi.plan_id
12757                         and excp.number1 = s.transaction_id
12758                         and excp.inventory_item_id = msi.inventory_item_id
12759                         and excp.organization_id = msi.organization_id
12760                         and excp.sr_instance_id = msi.sr_instance_id
12761                         and excp.exception_type = 33
12762                         and excp.number2 = md.inventory_item_id)*/
12763          tp.sr_tp_id= msi.organization_id
12764     AND     tp.sr_instance_id= msi.sr_instance_id
12765     AND     tp.partner_type=3
12766     AND     icomp.inventory_item_id= md.inventory_item_id
12767     AND     icomp.organization_id= md.organization_id
12768     AND     icomp.sr_instance_id= md.sr_instance_id
12769     AND     icomp.plan_id= md.plan_id
12770     AND     nvl(icomp.wip_supply_type,0) <> 6
12771     AND     md.sr_instance_id= s.sr_instance_id
12772     AND     md.disposition_id= s.transaction_id
12773     AND     md.plan_id= s.plan_id
12774     AND     md.original_item_id is null
12775     AND     param.organization_id = msi.organization_id
12776     AND     param.sr_instance_id  = msi.sr_instance_id
12777     AND     msi.inventory_item_id = s.inventory_item_id
12778     AND     msi.plan_id = s.plan_id
12779     AND     msi.organization_id = s.organization_id
12780     AND     msi.sr_instance_id = s.sr_instance_id
12781     AND     s.transaction_id= lv_transaction_id(j)
12782     AND     s.sr_instance_id= lv_instance_id(j)
12783     AND     s.plan_id= arg_plan_id;
12784     --AND     lv_details_enabled(j)= 1
12785     --AND     lv_agg_details(j) = 1;
12786 
12787 
12788     --Load Co-product/by-product component details
12789 
12790     FORALL j IN 1..lv_job_count
12791     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
12792             (last_update_date,
12793             last_updated_by,
12794             last_update_login,
12795             creation_date,
12796             created_by,
12797             organization_type,
12798             organization_id,
12799             group_id,
12800             parent_header_id,
12801             operation_seq_num,
12802             inventory_item_id_old,
12803             inventory_item_id_new,
12804             quantity_per_assembly,
12805             department_id,
12806             wip_supply_type,
12807             date_required,
12808             required_quantity,
12809             quantity_issued,
12810             supply_subinventory,
12811             supply_locator_id,
12812             mrp_net_flag,
12813             mps_required_quantity,
12814             mps_date_required,
12815             SUBSTITUTION_TYPE,
12816             LOAD_TYPE,
12817             --maintenance_object_source,
12818 	          process_phase,
12819 	          process_status,
12820             description,
12821             SR_INSTANCE_ID,
12822 --            operation_seq_id, -- Outbound Changes for XML
12823             uom_code, --Outbound Changes for XML
12824             wip_entity_id
12825             , eam_flag -- dsr
12826             )
12827     SELECT  SYSDATE,
12828             arg_user_id,
12829             s.last_update_login,
12830             SYSDATE,
12831             arg_user_id,
12832             tp.organization_type,
12833             s.organization_id,
12834             arg_wip_group_id*-1,
12835             s.transaction_id,
12836             nvl(co.operation_seq_num,1),
12837             icomp.sr_inventory_item_id,
12838             decode(l_apps_ver,'4',to_number(null),'3',null,icomp.sr_inventory_item_id),
12839             decode(l_apps_ver,'4',to_number(null),'3',TO_NUMBER(NULL),(co.new_order_quantity/s.implement_quantity)),
12840             TO_NUMBER(NULL),       --Department_ID
12841             NVL(GET_WIP_SUPPLY_TYPE(s.plan_id, s.sr_instance_id,s.process_seq_id,
12842                                     s.inventory_item_id,co.inventory_item_id,s.organization_id),
12843                     icomp.wip_supply_type),
12844             co.new_schedule_date,
12845             decode(l_apps_ver,'4',to_number(null),'3',TO_NUMBER(NULL),co.new_order_quantity),
12846             TO_NUMBER(NULL),  --quantity_issued
12847             TO_CHAR(NULL),     -- Sub Inventory
12848             TO_NUMBER(NULL),   -- Locator ID
12849             1,                 -- MRP_NET_FLAG
12850             decode(l_apps_ver,'4',to_number(null),'3',TO_NUMBER(NULL),co.new_order_quantity),
12851             co.new_schedule_date,
12852             SUBST_CHANGE,
12853             LT_COMPONENT,
12854             --1,
12855             2,
12856             1,
12857             TO_CHAR(NULL),
12858             s.sr_instance_id,
12859             nvl(s.implement_uom_code, msi.uom_code),
12860             s.disposition_id
12861              , DECODE(arg_load_type, EAM_RESCHEDULE_WORK_ORDER, SYS_YES, SYS_NO) -- dsr: eam_flag
12862       FROM  msc_trading_partners    tp,
12863             msc_system_items        icomp,
12864             msc_supplies            co,
12865             msc_parameters          param,
12866             msc_system_items        msi,
12867             msc_supplies            s
12868     WHERE   /*not exists (select 'x'
12869                         from msc_exception_details excp
12870                         where excp.plan_id = msi.plan_id
12871                         and excp.number1 = s.transaction_id
12872                         and excp.inventory_item_id = msi.inventory_item_id
12873                         and excp.organization_id = msi.organization_id
12874                         and excp.sr_instance_id = msi.sr_instance_id
12875                         and excp.exception_type = 33
12876                         and excp.number2 = co.inventory_item_id)
12877     AND     */
12878             tp.sr_tp_id             = msi.organization_id
12879     AND     tp.sr_instance_id       = msi.sr_instance_id
12880     AND     tp.partner_type         = 3
12881     AND     icomp.inventory_item_id = co.inventory_item_id
12882     AND     icomp.organization_id   = co.organization_id
12883     AND     icomp.sr_instance_id    = co.sr_instance_id
12884     AND     icomp.plan_id           = co.plan_id
12885     AND     co.sr_instance_id       = s.sr_instance_id
12886     AND     co.disposition_id       = s.transaction_id
12887     AND     co.plan_id              = s.plan_id
12888     AND     co.order_type           = 14 -- Discrete Job Co-products/by-products.
12889     AND     param.organization_id   = msi.organization_id
12890     AND     param.sr_instance_id    = msi.sr_instance_id
12891     AND     msi.inventory_item_id   = s.inventory_item_id
12892     AND     msi.plan_id             = s.plan_id
12893     AND     msi.organization_id     = s.organization_id
12894     AND     msi.sr_instance_id      = s.sr_instance_id
12895     AND     s.transaction_id        = lv_transaction_id(j)
12896     AND     s.sr_instance_id        = lv_instance_id(j)
12897     AND     s.plan_id               = arg_plan_id
12898     --AND     lv_details_enabled(j)   = 1
12899     --AND     lv_agg_details(j)       = 1
12900     AND     tp.organization_type    = 2;
12901 
12902 
12903     -- RESOURCE USAGE
12904     FORALL j IN 1..lv_job_count
12905     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
12906           ( last_update_date,
12907             last_updated_by,
12908             last_update_login,
12909             creation_date,
12910             created_by,
12911             organization_type,
12912             organization_id,
12913             group_id,
12914             parent_header_id,
12915             operation_seq_num,
12916             -- resource_seq_num,
12917             alternate_num,
12918             resource_id_old,
12919             resource_id_new,
12920             usage_rate_or_amount,
12921             scheduled_flag,
12922             applied_resource_units,   --
12923             applied_resource_value,   --
12924             uom_code,
12925             basis_type,     --
12926             activity_id,    --
12927             autocharge_type,     --
12928             standard_rate_flag,  --
12929             start_date,
12930             completion_date,
12931             assigned_units,
12932             SUBSTITUTION_TYPE,
12933             LOAD_TYPE,
12934             --maintenance_object_source,
12935 	    process_phase,
12936 	    process_status,
12937             description,
12938             SR_INSTANCE_ID,
12939             operation_seq_id, -- Outbound Changes for XML
12940             wip_entity_id,
12941             department_id,
12942             resource_hours
12943             , eam_flag -- dsr
12944             , resource_seq_num
12945             , schedule_seq_num
12946             ,parent_seq_num
12947             )
12948     SELECT  SYSDATE,
12949             arg_user_id,
12950             s.last_update_login,
12951             SYSDATE,
12952             arg_user_id,
12953             tp.organization_type,
12954             s.organization_id,
12955             arg_wip_group_id*-1,
12956             s.transaction_id,
12957             resreq.OPERATION_SEQ_NUM,
12958            -- resreq.RESOURCE_SEQ_NUM,
12959             resreq.ALTERNATE_NUM,
12960             resreq.RESOURCE_ID,
12961             resreq.RESOURCE_ID,
12962             decode(resreq.parent_seq_num, null, decode(l_apps_ver,'4',to_number(null),'3',TO_NUMBER(NULL),resreq.RESOURCE_HOURS/decode(resreq.basis_type,2,1,decode(s.new_order_quantity,0,1,s.new_order_quantity))), resreq.usage_rate),
12963             decode(nvl(resreq.schedule_flag,1),-23453,1,1,1,resreq.schedule_flag),
12964             NULL,
12965             NULL,
12966             v_hour_uom,
12967             resreq.basis_type,
12968             NULL,
12969             NULL,
12970             NULL,
12971             nvl(resreq.firm_start_date,resreq.START_DATE),
12972             nvl(resreq.firm_end_date,resreq.END_DATE),
12973             -- decode(l_apps_ver,'3',TO_NUMBER(NULL),resreq.ASSIGNED_UNITS),
12974             resreq.ASSIGNED_UNITS,
12975             SUBST_CHANGE,
12976             LT_RESOURCE_USAGE,
12977             --1,
12978             2,
12979             1,
12980             NULL,
12981             s.sr_instance_id,
12982             resreq.operation_sequence_id, -- Outbound Changes for XML
12983             s.disposition_id,
12984             resreq.department_id,
12985             resreq.resource_hours
12986             , DECODE(arg_load_type, EAM_RESCHEDULE_WORK_ORDER, SYS_YES, SYS_NO) -- dsr: eam_flag
12987             , resreq.orig_resource_seq_num
12988             , resreq.resource_seq_num
12989             ,resreq.parent_seq_num
12990       FROM  msc_trading_partners   tp,
12991             msc_resource_requirements resreq,
12992             msc_parameters          param,
12993             msc_system_items        msi,
12994             msc_supplies            s
12995     WHERE   tp.sr_tp_id= msi.organization_id
12996     AND     tp.sr_instance_id= msi.sr_instance_id
12997     AND     tp.partner_type=3
12998     AND     resreq.sr_instance_id= s.sr_instance_id
12999     AND     resreq.organization_id= s.organization_id
13000     AND     resreq.supply_id = s.transaction_id
13001     AND     resreq.plan_id   = s.plan_id
13002     AND     resreq.parent_id   = 1
13003     AND     param.organization_id = msi.organization_id
13004     AND     param.sr_instance_id  = msi.sr_instance_id
13005     AND     msi.inventory_item_id = s.inventory_item_id
13006     AND     msi.plan_id = s.plan_id
13007     AND     msi.organization_id = s.organization_id
13008     AND     msi.sr_instance_id = s.sr_instance_id
13009     AND     s.transaction_id= lv_transaction_id(j)
13010     AND     s.sr_instance_id= lv_instance_id(j)
13011     AND     s.plan_id= arg_plan_id
13012     --AND     lv_details_enabled(j)= 1
13013     --AND     lv_agg_details(j) = 1
13014      AND     tp.organization_type IN (1, 2);  -- 1 - discrete wip org; 2 - opm org
13015 
13016     -- dsr starts here
13017 --    print_debug_info('OPERATION RESOURCE_INSTANCES');
13018 	-- OPERATION RESOURCE_INSTANCES
13019 
13020     FORALL j IN 1..lv_job_count
13021     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
13022           ( last_update_date,
13023             last_updated_by,
13024             last_update_login,
13025             creation_date,
13026             created_by,
13027             organization_type,
13028             organization_id,
13029             group_id,
13030             parent_header_id,
13031             operation_seq_num,
13032            -- resource_seq_num,
13033             resource_id_old, -- rawasthi
13034 	          resource_id_new, -- xx resource_id, ???
13035             RESOURCE_INSTANCE_ID,
13036             start_date,
13037             completion_date,
13038             SUBSTITUTION_TYPE,
13039             LOAD_TYPE,
13040             --maintenance_object_source,
13041             process_phase,
13042             process_status,
13043             SR_INSTANCE_ID,
13044             operation_seq_id,
13045             -- FIRM_FLAG,
13046             resource_hours,
13047             department_id,
13048 	          -- setup_id,
13049 	          SERIAL_NUMBER,
13050             group_sequence_id,
13051             group_sequence_number,
13052             batch_id,
13053             resource_seq_num
13054             , schedule_seq_num
13055             , wip_entity_id -- for reschedule
13056             , eam_flag -- dsr
13057             , parent_seq_num
13058  )
13059     SELECT
13060           SYSDATE,
13061           arg_user_id,
13062           s.last_update_login,
13063           SYSDATE,
13064           arg_user_id,
13065           tp.organization_type,
13066           s.organization_id,
13067           arg_wip_group_id*-1,
13068           s.transaction_id,
13069           resreq.OPERATION_SEQ_NUM,
13070        --   resreq.RESOURCE_SEQ_NUM,
13071           resreq.resource_id,
13072           resreq.resource_id,
13073   -- jguo dep_res_inst.RESOURCE_INST_ID,
13074           dep_res_inst.RES_INSTANCE_ID ,
13075           nvl(resreq.firm_start_date,res_instreq.START_DATE),
13076           nvl(resreq.firm_end_date,res_instreq.END_DATE),
13077           SUBST_ADD, -- jguo SUBST_CHANGE,
13078           LT_RESOURCE_INSTANCE,
13079           --1,
13080           2,
13081           1,
13082           s.sr_instance_id,
13083           resreq.operation_sequence_id,
13084        -- res_instreq.firm_flag,
13085           resreq.resource_hours,
13086           resreq.department_id,
13087          -- resreq.department_id / 2, rawasthi
13088        -- res_instreq.setup_id,
13089           dep_res_inst.serial_number,
13090           resreq.group_sequence_id,
13091           resreq.group_sequence_number,
13092           res_instreq.batch_number,    --- sbala res_instreq.res_inst_batch_id,
13093           resreq.orig_resource_seq_num,
13094           resreq.resource_seq_num
13095       --  resreq.alternate_num,
13096         , s.disposition_id -- for reschedule
13097         , DECODE(arg_load_type, EAM_RESCHEDULE_WORK_ORDER, SYS_YES, SYS_NO) -- dsr: eam_flag
13098         , resreq.parent_seq_num
13099    FROM
13100           msc_trading_partners   tp,
13101           msc_resource_requirements resreq,
13102           msc_resource_instance_reqs res_instreq,
13103           msc_dept_res_instances dep_res_inst,
13104           msc_supplies            s
13105     WHERE
13106          tp.sr_tp_id=s.organization_id
13107  AND     tp.sr_instance_id= s.sr_instance_id
13108  AND     tp.partner_type=3
13109  AND     resreq.sr_instance_id= s.sr_instance_id
13110  AND     resreq.organization_id= s.organization_id
13111  AND     resreq.supply_id = s.transaction_id
13112  AND     resreq.plan_id   = s.plan_id
13113 -- dsr AND     resreq.transaction_id = res_instreq.res_inst_transaction_id
13114  AND     resreq.resource_seq_num = res_instreq.resource_seq_num
13115  AND     resreq.operation_seq_num = res_instreq.operation_seq_num
13116  AND     resreq.resource_id = res_instreq.resource_id
13117  AND     resreq.supply_id = res_instreq.supply_id
13118  AND     resreq.sr_instance_id = res_instreq.sr_instance_id
13119  AND     resreq.plan_id = res_instreq.plan_id
13120  AND     resreq.parent_id = res_instreq.parent_id  --rawasthi
13121  AND     resreq.start_date = res_instreq.start_date
13122  AND     resreq.parent_id   = 2
13123  AND     resreq.resource_id <> -1
13124  AND     resreq.department_id <> -1
13125 -- dsr   AND     res_instreq.res_instance_id = dep_res_inst.dept_resource_inst_id
13126  AND     res_instreq.plan_id = dep_res_inst.plan_id
13127  AND     res_instreq.sr_instance_id = dep_res_inst.sr_instance_id
13128  AND     res_instreq.department_id = dep_res_inst.department_id
13129  AND     res_instreq.resource_id = dep_res_inst.resource_id
13130 /* anuj: serail number and resource_instance id joins */
13131  AND     res_instreq.serial_number = dep_res_inst.serial_number
13132  AND     res_instreq.res_instance_id = dep_res_inst.res_instance_id
13133  AND     s.transaction_id= lv_transaction_id(j)
13134  AND     s.sr_instance_id= lv_instance_id(j)
13135  AND     s.plan_id= arg_plan_id
13136  --AND     lv_details_enabled(j)= 1
13137  --AND     lv_agg_details(j) = 1
13138     ;
13139 
13140 --print_debug_info('reschedule_wip_discrete_jobs: 888 sql%rowcount = '|| SQL%ROWCOUNT);
13141 
13142 
13143     -- RESOURCE INSTANCE USAGES
13144 --print_debug_info('RESOURCE INSTANCE USAGES');
13145 	FORALL j IN 1..lv_job_count
13146     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
13147           ( last_update_date,
13148             last_updated_by,
13149             last_update_login,
13150             creation_date,
13151             created_by,
13152             organization_type,
13153             organization_id,
13154             group_id,
13155             parent_header_id,
13156             operation_seq_num,
13157            -- resource_seq_num,
13158             resource_id_old, -- rawasthi
13159 	    resource_id_new, -- xx RESOURCE_ID, ???
13160             RESOURCE_INSTANCE_ID,
13161             start_date,
13162             completion_date,
13163             SUBSTITUTION_TYPE,
13164             LOAD_TYPE,
13165             --maintenance_object_source,
13166             process_phase,
13167             process_status,
13168             SR_INSTANCE_ID,
13169             operation_seq_id,
13170             FIRM_FLAG,
13171             resource_hours,
13172             department_id,
13173             serial_number,
13174             resource_seq_num,
13175             schedule_seq_num
13176          -- alternate_num
13177      	  , wip_entity_id -- for reschedule
13178 	  , eam_flag -- dsr
13179 	  , assigned_units -- jguo
13180 	  ,parent_seq_num
13181  )
13182     SELECT
13183            SYSDATE,
13184            arg_user_id,
13185            s.last_update_login,
13186            SYSDATE,
13187            arg_user_id,
13188            tp.organization_type,
13189            s.organization_id,
13190            arg_wip_group_id*-1,
13191            s.transaction_id,
13192            resreq.OPERATION_SEQ_NUM,
13193        --    resreq.RESOURCE_SEQ_NUM,
13194            resreq.RESOURCE_ID,
13195            resreq.RESOURCE_ID,
13196   -- jguo  dep_res_inst.RESOURCE_INST_ID,
13197            dep_res_inst.RES_INSTANCE_ID ,
13198            nvl(resreq.firm_start_date,res_instreq.START_DATE),
13199            nvl(resreq.firm_end_date,res_instreq.END_DATE),
13200            SUBST_ADD, -- jguo SUBST_CHANGE,
13201            LT_RESOURCE_INST_USAGE,
13202            --1,
13203            2,
13204            1,
13205            s.sr_instance_id,
13206            resreq.operation_sequence_id,
13207            resreq.firm_flag,
13208            res_instreq.resource_instance_hours,
13209            resreq.department_id,
13210           -- resreq.department_id / 2, rawasthi
13211            dep_res_inst.serial_number,
13212            resreq.orig_resource_seq_num,
13213            resreq.resource_seq_num
13214         -- resreq.alternate_num
13215          , s.disposition_id -- for reschedule
13216          , DECODE(arg_load_type, EAM_RESCHEDULE_WORK_ORDER, SYS_YES, SYS_NO) -- dsr: eam_flag
13217          , 1 -- jguo
13218          ,resreq.parent_seq_num
13219     FROM
13220            msc_trading_partners   tp,
13221            msc_resource_requirements resreq,
13222            msc_resource_instance_reqs res_instreq, -- ??? msc_res_inst_requirements res_instreq,
13223            msc_dept_res_instances dep_res_inst,
13224            msc_supplies            s
13225     WHERE
13226             tp.sr_tp_id=s.organization_id
13227     AND     tp.sr_instance_id= s.sr_instance_id
13228     AND     tp.partner_type=3
13229     AND     resreq.sr_instance_id= s.sr_instance_id
13230     AND     resreq.organization_id= s.organization_id
13231     AND     resreq.supply_id = s.transaction_id
13232     AND     resreq.plan_id   = s.plan_id
13233 -- dsr   AND     resreq.transaction_id = res_instreq.res_inst_transaction_id
13234     AND     resreq.resource_seq_num = res_instreq.resource_seq_num
13235     AND     resreq.operation_seq_num = res_instreq.operation_seq_num
13236     AND     resreq.resource_id = res_instreq.resource_id
13237     AND     resreq.supply_id = res_instreq.supply_id
13238     AND     resreq.sr_instance_id = res_instreq.sr_instance_id
13239     AND     resreq.plan_id = res_instreq.plan_id
13240     AND     resreq.parent_id = res_instreq.parent_id  --rawasthi
13241     AND     resreq.start_date = res_instreq.start_date
13242     AND     resreq.parent_id   = 1
13243     AND     resreq.resource_id <> -1
13244     AND     resreq.department_id <> -1
13245 -- dsr   AND     res_instreq.res_instance_id = dep_res_inst.dept_resource_inst_id
13246     AND     res_instreq.department_id = dep_res_inst.department_id
13247     AND     res_instreq.resource_id = dep_res_inst.resource_id
13248     AND     res_instreq.plan_id = dep_res_inst.plan_id
13249     AND     res_instreq.sr_instance_id = dep_res_inst.sr_instance_id
13250 /* anuj: serail number and resource_instance id joins */
13251     AND     res_instreq.serial_number = dep_res_inst.serial_number
13252     AND     res_instreq.res_instance_id = dep_res_inst.res_instance_id
13253     AND     s.transaction_id= lv_transaction_id(j)
13254     AND     s.sr_instance_id= lv_instance_id(j)
13255     AND     s.plan_id= arg_plan_id
13256     --AND     lv_details_enabled(j)= 1
13257     --AND     lv_agg_details(j) = 1
13258     ;
13259 
13260 --print_debug_info('reschedule_wip_discrete_jobs: 999 sql%rowcount = '|| SQL%ROWCOUNT);
13261 
13262 
13263 -- Resource Charges
13264 
13265 --print_debug_info('RESOURCE CHARGES');
13266 
13267 FORALL j IN 1..lv_job_count
13268     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
13269           ( last_update_date,
13270             last_updated_by,
13271             last_update_login,
13272             creation_date,
13273             created_by,
13274             organization_type,
13275             organization_id,
13276             group_id,
13277             parent_header_id,
13278             operation_seq_num,
13279             resource_seq_num,
13280 	    alternate_num,
13281             start_date,
13282             completion_date,
13283 	    required_quantity,
13284 	    charge_number,
13285             SR_INSTANCE_ID,
13286             SUBSTITUTION_TYPE,
13287             LOAD_TYPE,
13288             --maintenance_object_source,
13289             process_phase,
13290             process_status
13291           , wip_entity_id -- for reschedule
13292           , eam_flag -- dsr
13293           , resource_id_old
13294           , resource_id_new
13295           , schedule_seq_num
13296        )
13297     SELECT
13298             SYSDATE,
13299             arg_user_id,
13300             s.last_update_login,
13301             SYSDATE,
13302             arg_user_id,
13303             tp.organization_type,
13304             s.organization_id,
13305             arg_wip_group_id*-1,
13306             s.transaction_id,
13307             resreq.OPERATION_SEQ_NUM,
13308             resreq.ORIG_RESOURCE_SEQ_NUM,
13309             resreq.alternate_num,
13310             Chg.charge_start_datetime,
13311             Chg.charge_end_datetime,
13312             chg.charge_quantity, -- Chg.Planned_Charge_Quantity,
13313             Chg.charge_number,
13314             s.sr_instance_id,
13315             SUBST_CHANGE, -- 3
13316             LT_CHARGE_TYPE, --8
13317             --1,
13318             2,
13319             1
13320           , s.disposition_id -- for reschedule
13321           , DECODE(arg_load_type, EAM_RESCHEDULE_WORK_ORDER, SYS_YES, SYS_NO) -- dsr: eam_flag
13322           , resreq.RESOURCE_ID
13323           ,  resreq.RESOURCE_ID
13324           ,  resreq.resource_seq_num
13325      FROM
13326              msc_trading_partners   tp,
13327              msc_resource_requirements resreq,
13328              msc_supplies            s,
13329              msc_resource_charges chg
13330     WHERE
13331             tp.sr_tp_id=s.organization_id
13332     AND     tp.sr_instance_id= s.sr_instance_id
13333     AND     tp.partner_type=3
13334     AND     resreq.sr_instance_id= s.sr_instance_id
13335     AND     resreq.organization_id= s.organization_id
13336     AND     resreq.supply_id = s.transaction_id
13337     AND     resreq.plan_id   = s.plan_id
13338     AND     resreq.transaction_id = chg.res_transaction_id -- chg.transaction_id
13339     AND     resreq.plan_id = chg.plan_id
13340     AND     resreq.sr_instance_id = chg.sr_instance_id
13341     AND     resreq.parent_id   = 2
13342     AND     resreq.resource_id <> -1
13343     AND     resreq.department_id <> -1
13344     AND     s.transaction_id= lv_transaction_id(j)
13345     AND     s.sr_instance_id= lv_instance_id(j)
13346     AND     s.plan_id= arg_plan_id
13347     --AND     lv_details_enabled(j)= 1
13348     --AND     lv_agg_details(j) = 1
13349 	;
13350 
13351 	/*Code to insert the subsitute component--new for USAF */
13352 
13353 	FORALL j IN 1..lv_job_count
13354     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
13355             (last_update_date,
13356             last_updated_by,
13357             last_update_login,
13358             creation_date,
13359             created_by,
13360             organization_type,
13361             organization_id,
13362             group_id,
13363             parent_header_id,
13364             operation_seq_num,
13365             inventory_item_id_old,
13366             inventory_item_id_new,
13367             basis_type,
13368             quantity_per_assembly,
13369             component_yield_factor,
13370             department_id,
13371             wip_supply_type,
13372             date_required,
13373             required_quantity,
13374             quantity_issued,
13375             supply_subinventory,
13376             supply_locator_id,
13377             mrp_net_flag,
13378             mps_required_quantity,
13379             mps_date_required,
13380             SUBSTITUTION_TYPE,
13381             LOAD_TYPE,
13382             --maintenance_object_source,
13383 	          process_phase,
13384 	          process_status,
13385             description,
13386             SR_INSTANCE_ID,
13387             uom_code, --Outbound Changes for XML
13388             wip_entity_id,
13389              eam_flag -- dsr
13390             )
13391     SELECT  SYSDATE,
13392             arg_user_id,
13393             ms.last_update_login,
13394             SYSDATE,
13395             arg_user_id,
13396             tp.organization_type,
13397             ms.organization_id,
13398             arg_wip_group_id*-1,
13399             ms.transaction_id,
13400             nvl(md.op_seq_num,1),
13401             icompold.sr_inventory_item_id, --inventory_item_id_old,
13402             icompnew.sr_inventory_item_id, -- inventory_item_id_new
13403             decode(md.component_scaling_type,1,NULL,md.component_scaling_type),
13404             decode(l_apps_ver,'4',TO_NUMBER(NULL),'3',TO_NUMBER(NULL),(md.USING_REQUIREMENT_QUANTITY/ms.implement_quantity)),
13405             md.component_yield_factor,
13406             TO_NUMBER(NULL),       --Department_ID
13407             NVL(GET_WIP_SUPPLY_TYPE(ms.plan_id, ms.sr_instance_id,ms.process_seq_id,
13408                                     ms.inventory_item_id,md.inventory_item_id,ms.organization_id),1),--chk this ???
13409                     --icompnew.wip_supply_type),
13410             md.USING_ASSEMBLY_DEMAND_DATE,
13411             decode(l_apps_ver,'4',TO_NUMBER(NULL),'3',TO_NUMBER(NULL),md.USING_REQUIREMENT_QUANTITY),
13412             TO_NUMBER(NULL),  --quantity_issued
13413             TO_CHAR(NULL),     -- Sub Inventory
13414             TO_NUMBER(NULL),   -- Locator ID
13415             1,                 -- MRP_NET_FLAG
13416             decode(l_apps_ver,'4',to_number(null),'3',TO_NUMBER(NULL),md.USING_REQUIREMENT_QUANTITY),
13417             md.USING_ASSEMBLY_DEMAND_DATE,
13418             SUBST_CHANGE,
13419             LT_SUB_COMPONENT,
13420             --1,
13421             2,
13422             1,
13423             TO_CHAR(NULL),
13424             ms.sr_instance_id,
13425             nvl(ms.implement_uom_code, msi.uom_code),
13426             ms.disposition_id
13427             , DECODE(arg_load_type, EAM_RESCHEDULE_WORK_ORDER, SYS_YES, SYS_NO) -- dsr: eam_flag
13428       FROM  msc_trading_partners    tp,
13429             msc_system_items        icompold,
13430             msc_system_items        icompnew,
13431             msc_demands             md,
13432             msc_parameters          param,
13433             msc_system_items        msi,
13434             msc_supplies            ms,
13435             msc_exception_details   med
13436     WHERE   med.exception_type = WORK_ORDER_SUB_COMPONENT
13437     AND     med.plan_id = arg_plan_id
13438     and     med.number1 = ms.transaction_id
13439     and     med.inventory_item_id = ms.inventory_item_id
13440     and     med.organization_id = ms.organization_id
13441     and     med.sr_instance_id = ms.sr_instance_id
13442     --and     med.date1 = ms.new_schedule_date
13443     and     ms.order_type = 70
13444     and     ms.maintenance_object_source = 2
13445     and     md.plan_id = ms.plan_id
13446     and     md.disposition_id = ms.transaction_id
13447     and     md.original_item_id IS NOT NULL
13448     and     md.original_item_id <> md.inventory_item_id
13449     AND     tp.sr_tp_id= msi.organization_id
13450     AND     tp.sr_instance_id= msi.sr_instance_id
13451     AND     tp.partner_type=3
13452     AND     param.organization_id = msi.organization_id
13453     AND     param.sr_instance_id  = msi.sr_instance_id
13454     AND     msi.inventory_item_id = ms.inventory_item_id
13455     AND     msi.plan_id = ms.plan_id
13456     AND     msi.organization_id = ms.organization_id
13457     AND     msi.sr_instance_id = ms.sr_instance_id
13458     AND     icompold.inventory_item_id = md.original_item_id
13459     AND     icompold.plan_id = md.plan_id
13460     AND     icompold.organization_id = md.organization_id
13461     AND     icompold.sr_instance_id = md.sr_instance_id
13462     AND     icompnew.inventory_item_id = md.inventory_item_id
13463     AND     icompnew.plan_id = md.plan_id
13464     AND     icompnew.organization_id = md.organization_id
13465     AND     icompnew.sr_instance_id = md.sr_instance_id
13466     AND     ms.transaction_id= lv_transaction_id(j)
13467     AND     ms.sr_instance_id= lv_instance_id(j)
13468     AND     ms.plan_id= arg_plan_id;
13469 
13470 -- dsr ends here
13471    END IF; /* End of l_count = 1 */
13472 
13473     FORALL j IN 1..lv_job_count
13474        update msc_supplies
13475            set releasable = RELEASE_ATTEMPTED
13476                ,batch_id   = nvl(batch_id,g_batch_id)
13477          where plan_id = arg_plan_id
13478            and transaction_id= lv_transaction_id(j);
13479 
13480     RETURN lv_resched_jobs;
13481 
13482 
13483 
13484 END reschedule_cmro_jobs;
13485 
13486 PROCEDURE SET_RP_TIMESTAMP_WIP ( p_group_id IN number)
13487 IS
13488 BEGIN
13489     -- First Update the header records;
13490   begin
13491 
13492      UPDATE MSC_WIP_JOB_SCHEDULE_INTERFACE
13493      SET
13494      first_unit_start_date =
13495                       trunc(first_unit_start_date) + v_rp_time,
13496      last_unit_completion_date =
13497                       trunc(last_unit_completion_date) + v_rp_time,
13498      requested_completion_date =
13499                       trunc(requested_completion_date) + v_rp_time
13500      WHERE group_id = p_group_id;
13501   exception
13502      when others
13503      then
13504        NULL;
13505   end;
13506    -- Then Update the details records
13507   begin
13508      UPDATE MSC_WIP_JOB_DTLS_INTERFACE
13509      SET
13510      start_date =
13511                       trunc(start_date) + v_rp_time,
13512      completion_date =
13513                       trunc(completion_date) + v_rp_time,
13514      first_unit_start_date =
13515                       trunc(first_unit_start_date) + v_rp_time,
13516      first_unit_completion_date =
13517                       trunc(first_unit_completion_date) + v_rp_time,
13518      last_unit_start_date =
13519                       trunc(last_unit_start_date) + v_rp_time,
13520      last_unit_completion_date =
13521                       trunc(last_unit_completion_date) + v_rp_time,
13522      date_required =
13523                       trunc(date_required) + v_rp_time
13524      WHERE group_id= p_group_id;
13525   exception
13526      when others
13527      then
13528        NULL;
13529   end;
13530 
13531 END  SET_RP_TIMESTAMP_WIP;
13532 
13533 PROCEDURE SET_RP_TIMESTAMP_PO (p_arg_batch_id IN number)
13534 IS
13535 BEGIN
13536     -- Update PO Interface tables
13537   begin
13538      UPDATE MSC_PO_REQUISITIONS_INTERFACE
13539      SET
13540      need_by_date = trunc(need_by_date) + v_rp_time
13541     WHERE batch_id = p_arg_batch_id;
13542   exception
13543      when others
13544      then
13545        NULL;
13546   end;
13547     -- Update PO_RESCHEDULE if there is any
13548   begin
13549      UPDATE MSC_PO_RESCHEDULE_INTERFACE
13550      SET
13551      need_by_date = trunc(need_by_date) + v_rp_time
13552     WHERE batch_id = g_batch_id;
13553   exception
13554      when others
13555      then
13556        NULL;
13557   end;
13558 END SET_RP_TIMESTAMP_PO;
13559 
13560 END MSC_REL_PLAN_PUB;