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.41.12010000.13 2008/12/23 11:21:01 vsiyer 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    v_curr_instance_type         NUMBER;
46    v_msc_released_only_by_user  NUMBER;
47    v_batch_id_populated         NUMBER := 1; --1 - populated ; 2 - not populated in msc_supplies
48    v_user_id			NUMBER;
49 
50    JOB_CANCELLED          CONSTANT INTEGER := 7;
51 
52    NULL_DBLINK                  CONSTANT VARCHAR2(1):= ' ';
53 
54    LT_RESOURCE_INSTANCE CONSTANT NUMBER := 8;  -- dsr
55  	-- SUBST_CHANGE CONSTANT NUMBER := 3; -- dsr
56  	LT_RESOURCE_INST_USAGE CONSTANT NUMBER := 9; -- dsr
57  	LT_CHARGE_TYPE CONSTANT NUMBER := 10; -- dsr
58  	EAM_RESCHEDULE_WORK_ORDER CONSTANT NUMBER := 21; -- dsr
59 
60    l_sql_stmt          VARCHAR2(20000);
61 
62 /* DWK */ -- Function added for DRP Release
63 FUNCTION Decode_Sales_Order_Number(p_order_number_string in VARCHAR2)
64                                    return NUMBER IS
65 
66 p_order_number  NUMBER := 0;
67 l_end           number;
68 l_counter       number;
69 l_max_counter   number;
70 l_char          varchar2(1);
71 l_num_char      number;
72 
73 BEGIN
74 
75     BEGIN
76         p_order_number := to_number (p_order_number_string);
77         return p_order_number;
78     EXCEPTION
79         WHEN others then
80            null;
81     END;
82 
83     p_order_number:= null;
84     l_end := 0;
85     l_counter := 0;
86     l_max_counter := length(p_order_number_string);
87 
88     if (l_max_counter = 0) then
89         return null;
90     end if;
91 
92     while (l_end <> 1) loop
93         l_counter := l_counter + 1;
94         if (l_counter > l_max_counter ) then
95             l_end := 1;
96             exit;
97         end if;
98         l_char := substr (p_order_number_string, l_counter, 1);
99         BEGIN
100             l_num_char := to_number (l_char);
101         EXCEPTION
102             WHEN OTHERS then
103                 l_end := 1;
104         END;
105     end loop;
106 
107     if (l_counter > l_max_counter) then
108         BEGIN
109            p_order_number := to_number (p_order_number_string);
110         EXCEPTION
111             WHEN others then
112                 return null;
113         END;
114     elsif (l_counter = 1) then
115         return null;
116     else
117         BEGIN
118             p_order_number := to_number (substr (p_order_number_string, 1,l_counter -1));
119         EXCEPTION
120             WHEN others then
121                 return null;
122         END;
123     end if;
124 
125     return p_order_number;
126 
127 EXCEPTION
128     WHEN others THEN
129 
130         return null;
131 END;
132 
133    -- This procesure prints out debug information
134    PROCEDURE print_debug_info(
135      p_debug_info IN VARCHAR2
136    )IS
137    BEGIN
138        -- FND_FILE.PUT_LINE(FND_FILE.LOG, p_debug_info);
139        dbms_output.put_line(p_debug_info);
140    EXCEPTION
141    WHEN OTHERS THEN
142       RAISE;
143    END print_debug_info;
144 
145 
146 
147 FUNCTION GET_CAL_DATE ( lv_inst_id in number
148                         ,lv_calendar_date in date
149                         ,lv_calendar_code in varchar2) return date
150 is
151 lv_date date;
152 BEGIN
153     SELECT  cal4.calendar_date
154     into    lv_date
155     FROM    msc_calendar_dates cal3,
156             msc_calendar_dates cal4
157     WHERE   cal3.sr_instance_id = lv_inst_id
158     AND     cal3.exception_set_id = -1
159     AND     cal3.calendar_date = lv_calendar_date
160     AND     cal3.calendar_code  = lv_calendar_code
161     AND     cal4.sr_instance_id = cal3.sr_instance_id
162     AND     cal4.calendar_code = cal3.calendar_code
163     and     cal4.exception_set_id = cal3.exception_set_id
164     and     cal4.seq_num = nvl(cal3.seq_num,cal3.prior_seq_num);
165 
166     return lv_date;
167 
168     EXCEPTION
169        when no_data_found then
170        return null;
171        when others  then
172        return null;
173 
174 END GET_CAL_DATE;
175 FUNCTION GET_COPRODUCT_QTY ( inst_id in number
176                             , pln_id in number
177                             ,disp_id in number
178                             ,bill_seq_id in number) return number
179 is
180 lv_total number;
181 BEGIN
182 
183     select sum(coprod.new_order_quantity)
184     into lv_total
185     from  msc_supplies coprod, msc_bom_components mbc
186     where coprod.sr_instance_id = inst_id
187     and   coprod.plan_id  = pln_id
188     and   mbc.sr_instance_id = coprod.sr_instance_id
189     and   mbc.plan_id = coprod.plan_id
190     and   mbc.inventory_item_id = coprod.inventory_item_id
191     and   mbc.organization_id = coprod.organization_id
192     and   mbc.bill_sequence_id = bill_seq_id
193     and   NOT (mbc.usage_quantity < 0 and mbc.component_type <> 10)
194     and   coprod.order_type = 17
195     and   coprod.disposition_id = disp_id;
196 
197     select nvl(lv_total,0) into   lv_total from dual;
198 
199     return lv_total;
200 
201     EXCEPTION
202        when no_data_found then lv_total := 0;
203        return lv_total;
204        when others then lv_total := 0;
205        return lv_total;
206 
207 END GET_COPRODUCT_QTY;
208 
209 FUNCTION GET_REV_CUM_YIELD ( inst_id in number
210                             , pln_id in number
211                             ,process_seq_id in number
212                             ,trans_id in number
213                             ,org_id in number) return number
214 is
215 lv_yield number;
216 BEGIN
217 
218     select nvl(mrr.REVERSE_CUMULATIVE_YIELD,1)
219     into lv_yield
220     from  msc_process_effectivity mpe,
221           msc_routings mr,
222           msc_resource_requirements mrr
223     where mpe.sr_instance_id = inst_id
224     and   mpe.plan_id  = pln_id
225     and   mpe.process_sequence_id = process_seq_id
226     and   mr.routing_sequence_id = mpe.routing_sequence_id
227     and   mr.plan_id = mpe.plan_id
228     and   mr.sr_instance_id = mpe.sr_instance_id
229     and   mrr.plan_id = mr.plan_id
230     and   mrr.routing_sequence_id = mr.routing_sequence_id
231     and   mrr.sr_instance_id = mr.sr_instance_id
232     and   mrr.OPERATION_SEQ_NUM = mr.FIRST_OP_SEQ_NUM
233     and   mrr.supply_id = trans_id
234     and   mrr.organization_id = org_id
235     and   mrr.rowid=(select min(rowid) from msc_resource_requirements
236                      where routing_sequence_id = mr.routing_sequence_id
237                      and   sr_instance_id = inst_id
238                      and   plan_id = pln_id
239                      and   supply_id = trans_id
240                      and   organization_id = org_id
241                      and   operation_seq_num = mr.first_op_seq_num
242                      and   parent_id = 2);
243 
244     return lv_yield;
245 
246 EXCEPTION
247        when no_data_found then lv_yield := 1;
248        return lv_yield;
249        when others then lv_yield := 1;
250        return lv_yield;
251 
252 END GET_REV_CUM_YIELD;
253 
254 FUNCTION GET_REV_CUM_YIELD_DISC( inst_id        IN NUMBER
255                                  ,pln_id         IN NUMBER
256                                  ,process_seq_id IN NUMBER
257                                  ,trans_id       IN NUMBER
258                                  ,org_id         IN NUMBER
259                                  ,org_type       IN NUMBER
260                                   )
261 RETURN number
262  IS
263  lv_yield_dis number;
264 
265  BEGIN
266 
267    lv_yield_dis := 0;
268 
269   /* For  bug: 2559388 - added this If cond to return RCY=1 (for OPM, there is no RCY concept ,so return 1
270     and for the OPM orgs organization_type = 2 in msc_trading_partners */
271 
272    IF (org_type = 2) THEN
273         lv_yield_dis := 1;
274         RETURN lv_yield_dis;
275    END IF;
276 
277      select nvl(mrr.REVERSE_CUMULATIVE_YIELD,1)
278      into lv_yield_dis
279      from  msc_process_effectivity mpe,
280            msc_routings mr,
281            msc_resource_requirements mrr
282      where mpe.sr_instance_id = inst_id
283      and   mpe.plan_id  = pln_id
284      and   mpe.process_sequence_id = process_seq_id
285      and   mr.routing_sequence_id = mpe.routing_sequence_id
286      and   mr.plan_id = mpe.plan_id
287      and   mr.sr_instance_id = mpe.sr_instance_id
288      and   mrr.plan_id = mr.plan_id
289      and   mrr.routing_sequence_id = mr.routing_sequence_id
290      and   mrr.sr_instance_id = mr.sr_instance_id
291      and   mrr.supply_id = trans_id
292      and   mrr.organization_id = org_id
293      and   mrr.OPERATION_SEQ_NUM = (select min(OPERATION_SEQ_NUM)
294                                from msc_resource_requirements
295                               where   routing_sequence_id = mr.routing_sequence_id
296                                 and   sr_instance_id = inst_id
297                                 and   plan_id = pln_id
298                                 and   supply_id = trans_id
299                                 and   organization_id = org_id
300                                 and   parent_id = 2)
301      and mrr.parent_id = 2
302      and rownum = 1;
303 
304    RETURN lv_yield_dis;
305 
306  EXCEPTION
307         WHEN OTHERS THEN
308             lv_yield_dis := 1;
309 
310    RETURN lv_yield_dis;
311 
312 END GET_REV_CUM_YIELD_DISC;
313 
314 
315 FUNCTION GET_REV_CUM_YIELD_DISC_COMP( inst_id         IN NUMBER
316                                       ,pln_id         IN NUMBER
317                                       ,process_seq_id IN NUMBER
318                                       ,trans_id       IN NUMBER
319                                       ,org_id         IN NUMBER
320                                       ,org_type       IN NUMBER
321                                       ,op_seq_num     IN NUMBER
322                                   )
323 RETURN number
324  IS
325  lv_yield_dis number;
326 
327  BEGIN
328 
329    lv_yield_dis := 0;
330 
331   /* For  bug: 2559388 - added this If cond to return RCY=1 (for OPM, there is no RCY concept ,so return 1
332     and for the OPM orgs organization_type = 2 in msc_trading_partners */
333 
334    IF (org_type = 2) THEN
335         lv_yield_dis := 1;
336         RETURN lv_yield_dis;
337    END IF;
338 
339      select nvl(mrr.REVERSE_CUMULATIVE_YIELD,1)
340      into lv_yield_dis
341      from  msc_process_effectivity mpe,
342            msc_routings mr,
343            msc_resource_requirements mrr
344      where mpe.sr_instance_id = inst_id
345      and   mpe.plan_id  = pln_id
346      and   mpe.process_sequence_id = process_seq_id
347      and   mr.routing_sequence_id = mpe.routing_sequence_id
348      and   mr.plan_id = mpe.plan_id
349      and   mr.sr_instance_id = mpe.sr_instance_id
350      and   mrr.plan_id = mr.plan_id
351      and   mrr.routing_sequence_id = mr.routing_sequence_id
352      and   mrr.sr_instance_id = mr.sr_instance_id
353      and   mrr.supply_id = trans_id
354      and   mrr.organization_id = org_id
355      and   mrr.OPERATION_SEQ_NUM = op_seq_num
356      and mrr.parent_id = 2
357      and rownum = 1;
358 
359    RETURN lv_yield_dis;
360 
361  EXCEPTION
362         WHEN OTHERS THEN
363             lv_yield_dis := 1;
364 
365    RETURN lv_yield_dis;
366 
367 END GET_REV_CUM_YIELD_DISC_COMP;
368 
369 FUNCTION GET_USAGE_QUANTITY ( p_plan_id     IN NUMBER
370                               ,p_inst_id     IN NUMBER
371                               ,p_org_id     IN NUMBER
372                               ,p_using_assy_id     IN NUMBER
373                               ,p_comp_seq_id  IN NUMBER) RETURN NUMBER
374 IS
375 lv_USAGE_QUANTITY NUMBER;
376 BEGIN
377 
378     SELECT mbc.USAGE_QUANTITY
379     INTO   lv_USAGE_QUANTITY
380     FROM   MSC_BOM_COMPONENTS mbc
381     WHERE mbc.plan_id = p_plan_id
382     AND  mbc.sr_instance_id = p_inst_id
383     AND mbc.organization_id = p_org_id
384     AND mbc.using_assembly_id = p_using_assy_id
385     AND mbc.component_sequence_id  =  p_comp_seq_id;
386 
387     RETURN lv_USAGE_QUANTITY;
388 
389 EXCEPTION
390        WHEN OTHERS THEN  return NULL;
391 
392 END GET_USAGE_QUANTITY;
393 
394 FUNCTION GET_WIP_SUPPLY_TYPE ( p_plan_id     IN NUMBER
395                               ,p_inst_id     IN NUMBER
396                               ,p_process_seq_id IN NUMBER
397                               ,p_item_id      IN NUMBER
398                               ,p_comp_item_id IN NUMBER
399                               ,p_org_id      IN NUMBER) RETURN NUMBER
400 IS
401 lv_wip_sup_type NUMBER;
402 BEGIN
403 
404     SELECT mbc.WIP_SUPPLY_TYPE
405     INTO   lv_wip_sup_type
406     FROM   MSC_BOM_COMPONENTS mbc,
407            msc_process_effectivity mpe
408    where mpe.sr_instance_id = p_inst_id
409     and   mpe.plan_id  = p_plan_id
410     and   mpe.process_sequence_id = p_process_seq_id
411     AND mpe.organization_id = p_org_id
412     AND mpe.item_id = p_item_id
413     and    mbc.plan_id =  mpe.plan_id
414     AND mbc.sr_instance_id = mpe.sr_instance_id
415     AND mbc.bill_sequence_id = mpe.bill_sequence_id
416     AND mbc.inventory_item_id =  p_comp_item_id
417     AND (mpe.disable_date is NULL OR
418             trunc(mpe.disable_date) >= trunc(SYSDATE) )
419     AND (mbc.disable_date is NULL OR
420             trunc(mbc.disable_date) >= trunc(SYSDATE) )
421     and rownum = 1;
422     RETURN lv_wip_sup_type;
423 
424 EXCEPTION
425        WHEN OTHERS THEN  return NULL;
426 
427 END GET_WIP_SUPPLY_TYPE;
428 
429 /* added these 2 functions for getting the sr_tp_id and tp_site_code
430   for releasing across the instances */
431 FUNCTION GET_MODELED_SR_TP_ID (pMODELED_SUPPLIER_ID    IN NUMBER,
432                                pSR_INSTANCE_ID         IN NUMBER)
433     RETURN NUMBER
434 IS
435 lv_mod_sup_sr_tp_id NUMBER;
436 BEGIN
437 
438     SELECT SR_TP_ID
439     INTO   lv_mod_sup_sr_tp_id
440     FROM   MSC_TP_ID_LID
441     WHERE  TP_ID = pMODELED_SUPPLIER_ID
442       AND  SR_INSTANCE_ID = pSR_INSTANCE_ID
443       AND  PARTNER_TYPE = 1;
444 
445     RETURN lv_mod_sup_sr_tp_id;
446 
447 EXCEPTION
448     WHEN OTHERS THEN
449           RETURN NULL;
450 
451 END GET_MODELED_SR_TP_ID;
452 
453 FUNCTION GET_MODELED_TP_SITE_CODE (pMODELED_SUPPLIER_ID       IN NUMBER,
454                                    pMODELED_SUPPLIER_SITE_ID  IN NUMBER,
455                                    pSR_INSTANCE_ID            IN NUMBER)
456    RETURN VARCHAR2
457 IS
458 lv_mod_sup_site_code VARCHAR2(30);
459 BEGIN
460 
461     SELECT TP_SITE_CODE
462     INTO   lv_mod_sup_site_code
463     FROM   MSC_TRADING_PARTNER_SITES
464     WHERE  PARTNER_ID = pMODELED_SUPPLIER_ID
465       AND  PARTNER_SITE_ID = pMODELED_SUPPLIER_SITE_ID
466       AND  SR_INSTANCE_ID = pSR_INSTANCE_ID
467       AND  PARTNER_TYPE = 1;
468 
469     RETURN lv_mod_sup_site_code;
470 
471 EXCEPTION
472     WHEN OTHERS THEN
473           RETURN NULL;
474 
475 END GET_MODELED_TP_SITE_CODE;
476 
477 
478 /***********************************************************
479 PROCEDURE:       MSC_RELEASE_PLAN_SC
480 
481 *************************************************************/
482 PROCEDURE MSC_RELEASE_PLAN_SC
483 ( arg_plan_id			IN      NUMBER
484 , arg_log_org_id 		IN 	NUMBER
485 , arg_log_sr_instance           IN      NUMBER
486 , arg_org_id 			IN 	NUMBER
487 , arg_sr_instance               IN      NUMBER
488 , arg_compile_desig 		IN 	VARCHAR2
489 , arg_user_id 			IN 	NUMBER
490 , arg_po_group_by 		IN 	NUMBER
491 , arg_po_batch_number 		IN 	NUMBER
492 , arg_wip_group_id 		IN 	NUMBER
493 , arg_loaded_jobs 		IN OUT  NOCOPY NumTblTyp
494 , arg_loaded_reqs 		IN OUT  NOCOPY  NumTblTyp
495 , arg_loaded_scheds 		IN OUT  NOCOPY NumTblTyp
496 , arg_resched_jobs 		IN OUT  NOCOPY NumTblTyp
497 , arg_resched_reqs 		IN OUT  NOCOPY NumTblTyp
498 , arg_wip_req_id  		IN OUT  NOCOPY NumTblTyp
499 , arg_req_load_id 		IN OUT  NOCOPY  NumTblTyp
500 , arg_req_resched_id 		IN OUT  NOCOPY  NumTblTyp
501 , arg_released_instance         IN OUT  NOCOPY  NumTblTyp
502 , arg_mode                      IN      VARCHAR2
503 , arg_transaction_id            IN      NUMBER
504 , arg_loaded_lot_jobs           IN OUT  NOCOPY  NumTblTyp
505 , arg_resched_lot_jobs          IN OUT  NOCOPY  NumTblTyp
506 , arg_osfm_req_id               IN OUT  NOCOPY  NumTblTyp
507  -- the following 2 parameters added for dsr
508 , arg_resched_eam_jobs          IN OUT  NOCOPY  NumTblTyp
509 , arg_eam_req_id 	        IN OUT  NOCOPY  NumTblTyp
510 -- the following 2 parameters added for DRP Release
511 , arg_loaded_int_reqs               IN OUT  NOCOPY  NumTblTyp
512 , arg_resched_int_reqs              IN OUT  NOCOPY NumTblTyp
513 , arg_int_req_load_id               IN OUT  NOCOPY  NumTblTyp
514 , arg_int_req_resched_id            IN OUT  NOCOPY  NumTblTyp
515 , arg_loaded_int_repair_orders      IN OUT  NOCOPY  NumTblTyp -- bug 6021045
516 , arg_int_repair_orders_id          IN OUT  NOCOPY  NumTblTyp -- bug 6021045
517 , arg_loaded_ext_repair_orders      IN OUT  NOCOPY  NumTblTyp -- bug 6051361
518 , arg_ext_repair_orders_id          IN OUT  NOCOPY  NumTblTyp -- bug 6051361
519 ) IS
520 
521 CURSOR c_Instance IS
522 SELECT apps.instance_id,
523        apps.instance_code,
524        apps.apps_ver,
525        apps.instance_type,
526        DECODE(apps.m2a_dblink,NULL,' ', '@' || m2a_dblink),
527        DECODE(apps.a2m_dblink,NULL,NULL_DBLINK,a2m_dblink),
528        LENGTH( apps.instance_code)+2
529 FROM   msc_apps_instances apps,
530        ( SELECT distinct
531                 sr_instance_id
532            FROM msc_plan_organizations_v plan_org
533           WHERE plan_org.plan_id = arg_plan_id
534             AND plan_org.organization_id = arg_org_id
535             AND plan_org.owning_sr_instance = arg_sr_instance
536             AND plan_org.sr_instance_id =
537                          decode(arg_log_sr_instance,
538                                 arg_sr_instance, plan_org.sr_instance_id,
539                                 arg_log_sr_instance)) ins
540 WHERE apps.instance_id = ins.sr_instance_id;
541 
542   l_sr_instance_id    NUMBER;
543   l_instance_code     VARCHAR2(3);
544   l_apps_ver          VARCHAR2(10);
545   l_dblink            VARCHAR2(128);
546   l_a2m_dblink        VARCHAR2(128);
547 
548   l_user_name         VARCHAR2(100):= NULL;
549   l_resp_name         VARCHAR2(100):= NULL;
550   l_application_name  VARCHAR2(240):= NULL;
551 
552   l_user_id           NUMBER;
553   l_resp_id           NUMBER;
554   l_application_id    NUMBER;
555 
556 --  l_sql_stmt          VARCHAR2(4000);
557 
558   l_loaded_jobs       NUMBER;
559   l_loaded_reqs       NUMBER;
560   l_loaded_scheds     NUMBER;
561   l_resched_jobs      NUMBER;
562 
563   L_RESCHED_EAM_JOBS	   NUMBER; -- dsr
564   L_EAM_REQ_ID 		   NUMBER; -- dsr
565 
566   l_resched_reqs      NUMBER;
567   l_wip_req_id        NUMBER;
568   l_req_load_id       NUMBER;
569   l_req_resched_id    NUMBER;
570 
571   lv_count            NUMBER:= 0;
572 
573 
574   l_loaded_lot_jobs   NUMBER;
575   l_resched_lot_jobs  NUMBER;
576   l_osfm_req_id       NUMBER;
577 
578   lv_error_buf        VARCHAR2(2000);
579   lv_ret_code         NUMBER;
580 
581   l_load_int_jobs          number;
582   l_resched_int_jobs       number;
583   l_int_req_load_id        number;
584   l_int_req_resched_id     number;
585 
586   l_loaded_int_repair_orders number ;-- bug 6021045
587   l_int_repair_orders_id number ;-- bug 6021045
588 
589   l_loaded_ext_repair_orders number ;-- bug 6051361
590   l_ext_repair_orders_id number ;-- bug 6051361
591 
592   l_wip_group_id      NUMBER;
593   l_po_batch_number   NUMBER;
594 
595 BEGIN
596 
597 
598    SELECT
599      DECODE(NVL(fnd_profile.value('MSC_RELEASED_BY_USER_ONLY') ,'N'), 'Y',1 ,2)
600      INTO  v_msc_released_only_by_user
601      FROM  DUAL;
602      v_batch_id_populated := 1;
603      IF (g_batch_id = g_prev_batch_id)  OR g_batch_id = -1 THEN
604            begin
605                -- populating batch_id from destination side seq.
606                Execute immediate 'select  mrp_workbench_query_s.nextval
607                                  FROM DUAL '
608                             into MSC_Rel_Plan_PUB.g_batch_id;
609                v_batch_id_populated := 2;
610            exception when others then
611             fnd_file.put_line(FND_FILE.LOG, sqlerrm);
612            end;
613      END IF;
614      g_prev_batch_id := g_batch_id;
615    SELECT
616        FND_GLOBAL.USER_ID,
617        FND_GLOBAL.USER_NAME,
618        FND_GLOBAL.RESP_NAME,
619        FND_GLOBAL.APPLICATION_NAME
620      INTO v_user_id,
621           l_user_name,
622            l_resp_name,
623            l_application_name
624      FROM  dual;
625 
626 
627      SELECT APPLICATION_ID
628      INTO l_application_id
629      FROM FND_APPLICATION_VL
630      WHERE APPLICATION_NAME = l_application_name;
631 
632   -------- Release the planned order one instance by one instance.
633 
634   OPEN c_Instance;
635   LOOP
636     FETCH c_Instance
637      INTO l_sr_instance_id,
638           l_instance_code,
639           l_apps_ver,
640           v_curr_instance_type,
641           l_dblink,
642           l_a2m_dblink,
643           v_instance_code_length;
644 
645     EXIT WHEN c_Instance%NOTFOUND;
646 
647     ---------- Initialize the remote process
648     ---------- If the instance is discrete or mixed type
649     ----------    AND the instance is at a remote database
650 
651    arg_loaded_jobs.extend(1);
652    arg_loaded_reqs.extend(1);
653    arg_loaded_scheds.extend(1);
654    arg_resched_jobs.extend(1);
655    arg_resched_reqs.extend(1);
656    arg_wip_req_id.extend(1);
657    arg_req_load_id.extend(1);
658    arg_req_resched_id.extend(1);
659    arg_released_instance.extend(1);
660    arg_loaded_lot_jobs.extend(1);
661    arg_resched_lot_jobs.extend(1);
662    arg_osfm_req_id.extend(1);
663 
664    arg_loaded_int_reqs.extend(1);
665    arg_resched_int_reqs.extend(1);
666    arg_int_req_load_id.extend(1);
667    arg_int_req_resched_id.extend(1);
668 
669    arg_loaded_int_repair_orders.extend(1); -- bug 6021045
670    arg_int_repair_orders_id.extend(1); --bug 6021045
671 
672    arg_loaded_ext_repair_orders.extend(1); -- bug 6051361
673    arg_ext_repair_orders_id.extend(1); --bug 6051361
674 
675    lv_count:= lv_count+1;
676 
677    arg_released_instance(lv_count):= l_sr_instance_id;
678 
679    -- initialize the Applications Environment --
680    IF v_curr_instance_type IN ( G_INS_DISCRETE, G_INS_PROCESS, G_INS_MIXED) THEN
681    IF l_apps_ver >= 3 THEN
682       l_sql_stmt:=
683        'BEGIN'
684      ||'  MRP_AP_REL_PLAN_PUB.INITIALIZE'||l_dblink
685                          ||'( :l_user_name,'
686                          ||'  :l_resp_name,'
687                          ||'  :l_application_name,'
688                          ||'  :l_sr_instance_id,'
689                          ||'  :l_instance_code,'
690                          ||'  :l_a2m_dblink,'
691                          ||'  :l_wip_group_id,'
692                          ||'  :l_po_batch_number,'
693                          ||'  :l_application_id);'
694      ||'END;';
695 
696        EXECUTE IMMEDIATE l_sql_stmt
697                    USING IN l_user_name,
698                          IN l_resp_name,
699                          IN l_application_name,
700                          IN l_sr_instance_id,
701                          IN l_instance_code,
702                          IN l_a2m_dblink,
703                          OUT l_wip_group_id,
704                          OUT l_po_batch_number,
705                          IN l_application_id;
706     ELSE
707          l_sql_stmt:=
708             'BEGIN'
709           ||'  MRP_AP_REL_PLAN_PUB.INITIALIZE'||l_dblink
710                               ||'( :l_user_name,'
711                               ||'  :l_resp_name,'
712                               ||'  :l_application_name);'
713           ||'END;';
714 
715          EXECUTE IMMEDIATE l_sql_stmt
716                         USING IN l_user_name,
717                               IN l_resp_name,
718                               IN l_application_name;
719 
720     END IF;
721 
722     END IF;
723 
724        l_loaded_jobs   := 0;
725        l_loaded_reqs   := 0;
726        l_loaded_scheds := 0;
727        l_resched_jobs  := 0;
728        l_loaded_lot_jobs := 0;
729        l_resched_lot_jobs := 0;
730        L_RESCHED_EAM_JOBS := 0; -- dsr
731        l_load_int_jobs   :=0;
732        l_resched_int_jobs :=0;
733 
734        l_loaded_int_repair_orders :=0  ;-- bug 6021045
735        l_loaded_ext_repair_orders :=0  ;-- bug 6051361
736 
737 
738     -- load the msc interface tables, submit the request --
739        LOAD_MSC_INTERFACE
740                 (arg_dblink  => l_dblink,
741                  arg_plan_id => arg_plan_id,
742                  arg_log_org_id => arg_log_org_id,
743                  arg_org_instance => l_sr_instance_id,
744                  arg_owning_org_id => arg_org_id,
745                  arg_owning_instance => arg_sr_instance,
746                  arg_compile_desig => arg_compile_desig,
747                  arg_user_id => arg_user_id,
748                  arg_po_group_by => arg_po_group_by,
749                  arg_po_batch_number => l_po_batch_number,
750                  arg_wip_group_id => l_wip_group_id,
751     ----------------------------------------------- Number of Loaded Orders
752                  arg_loaded_jobs   => l_loaded_jobs,
753                  arg_loaded_lot_jobs => l_loaded_lot_jobs,
754                  arg_resched_lot_jobs => l_resched_lot_jobs,
755                  arg_loaded_reqs   => l_loaded_reqs,
756                  arg_loaded_scheds => l_loaded_scheds,
757                  arg_resched_jobs  => l_resched_jobs,
758     ----------------------------------------------- Request IDs
759                  arg_resched_reqs =>   l_resched_reqs,
760                  arg_wip_req_id =>     l_wip_req_id,
761                  arg_osfm_req_id =>    l_osfm_req_id,
762                  arg_req_load_id =>    l_req_load_id,
763                  arg_req_resched_id => l_req_resched_id,
764     -------------------------------------------------------------
765                  arg_mode => arg_mode,
766                  arg_transaction_id => arg_transaction_id,
767                  l_apps_ver   =>  l_apps_ver,
768                  -- the following 2 lines added for dsr
769 	        		 arg_resched_eam_jobs => l_resched_eam_jobs,
770 							 arg_eam_req_id =>    l_eam_req_id,
771     -------------------------------------------------------------
772                  arg_loaded_int_reqs  => l_load_int_jobs,
773                  arg_resched_int_reqs => l_resched_int_jobs,
774                  arg_int_req_load_id  => l_int_req_load_id,
775                  arg_int_req_resched_id => l_int_req_resched_id,
776     -------------------------------------------------------------
777                arg_loaded_int_repair_orders=> l_loaded_int_repair_orders,   -- bug 6021045
778                arg_int_repair_orders_id=> l_int_repair_orders_id,           -- bug 6021045
779                arg_loaded_ext_repair_orders=> l_loaded_ext_repair_orders,   -- bug 6051361
780                arg_ext_repair_orders_id=> l_ext_repair_orders_id            -- bug 6051361
781 		 );
782 
783 
784 
785    COMMIT WORK;
786 
787    arg_loaded_jobs(lv_count)   :=  l_loaded_jobs;
788    arg_loaded_reqs(lv_count)   :=  l_loaded_reqs;
789    arg_loaded_scheds(lv_count) :=  l_loaded_scheds;
790    arg_resched_jobs(lv_count)  :=  l_resched_jobs;
791 
792    arg_loaded_lot_jobs(lv_count) := l_loaded_lot_jobs;
793    arg_resched_lot_jobs(lv_count) := l_resched_lot_jobs;
794 
795    arg_resched_reqs(lv_count)  :=  l_resched_reqs;
796    arg_wip_req_id(lv_count)    :=  l_wip_req_id;
797 
798    arg_osfm_req_id(lv_count)    :=  l_osfm_req_id;
799    arg_req_load_id(lv_count)   :=  l_req_load_id;
800    arg_req_resched_id(lv_count):=  l_req_resched_id;
801 
802     -- dsr
803    arg_resched_eam_jobs(lv_count) := l_resched_eam_jobs;
804    arg_eam_req_id(lv_count)    :=  l_eam_req_id;
805 
806    -- DRP Release
807    arg_loaded_int_reqs(lv_count) := l_load_int_jobs;
808    arg_resched_int_reqs(lv_count) := l_resched_int_jobs;
809    arg_int_req_load_id(lv_count) := l_int_req_load_id;
810    arg_int_req_resched_id(lv_count) :=  l_int_req_resched_id;
811 
812    --IRO release
813     arg_loaded_int_repair_orders(lv_count):= l_loaded_int_repair_orders;    -- bug 6021045
814     arg_int_repair_orders_id(lv_count) := l_int_repair_orders_id;-- bug 6021045
815 
816    --ERO release
817     arg_loaded_ext_repair_orders(lv_count):= l_loaded_ext_repair_orders;    -- bug 6051361
818     arg_ext_repair_orders_id(lv_count) := l_ext_repair_orders_id;-- bug 6051361
819 
820    END LOOP;
821 
822    CLOSE c_Instance;
823 
824    arg_loaded_jobs.trim(1);
825    arg_loaded_lot_jobs.trim(1);
826    arg_loaded_reqs.trim(1);
827    arg_loaded_scheds.trim(1);
828    arg_resched_jobs.trim(1);
829    arg_resched_lot_jobs.trim(1);
830    arg_resched_reqs.trim(1);
831    arg_wip_req_id.trim(1);
832    arg_osfm_req_id.trim(1);
833    arg_req_load_id.trim(1);
834    arg_req_resched_id.trim(1);
835    arg_released_instance.trim(1);
836    arg_loaded_int_reqs.trim(1);
837    arg_resched_int_reqs.trim(1);
838    arg_int_req_load_id.trim(1);
839    arg_int_req_resched_id.trim(1);
840    arg_loaded_int_repair_orders.trim(1);-- bug 6021045
841    arg_int_repair_orders_id.trim(1);-- bug 6021045
842    arg_loaded_ext_repair_orders.trim(1);-- bug 6051361
843    arg_ext_repair_orders_id.trim(1);-- bug 6051361
844 
845 EXCEPTION
846 
847    WHEN OTHERS THEN
848       IF c_Instance%ISOPEN THEN CLOSE c_Instance; END IF;
849 
850       RAISE;
851 
852 END MSC_Release_Plan_Sc;
853 
854 /* for bug: 2428319,
855   OVER-LOADED the PROCEDURE MSC_RELEASE_PLAN_SC for backward compatibility, so that the old versions of UI package MSC_EXP_WF
856   and the MSCFNORD does not get INVALID after the application of Collections patch
857   This may also happens because ATP has some UI pre-req patch which applies MSCFNORD on the source instance
858   and does not get compiled
859   This is done only for Compilation purpose and this procedure will not be called by UI for the
860   functionality of Releasing planned orders
861 */
862 PROCEDURE MSC_RELEASE_PLAN_SC
863 ( arg_plan_id			IN      NUMBER
864 , arg_log_org_id 		IN 	NUMBER
865 , arg_log_sr_instance           IN      NUMBER
866 , arg_org_id 			IN 	NUMBER
867 , arg_sr_instance               IN      NUMBER
868 , arg_compile_desig 		IN 	VARCHAR2
869 , arg_user_id 			IN 	NUMBER
870 , arg_po_group_by 		IN 	NUMBER
871 , arg_po_batch_number 		IN 	NUMBER
872 , arg_wip_group_id 		IN 	NUMBER
873 , arg_loaded_jobs 		IN OUT 	NOCOPY  NumTblTyp
874 , arg_loaded_reqs 		IN OUT  NOCOPY  NumTblTyp
875 , arg_loaded_scheds 		IN OUT  NOCOPY  NumTblTyp
876 , arg_resched_jobs 		IN OUT  NOCOPY  NumTblTyp
877 , arg_resched_reqs 		IN OUT  NOCOPY  NumTblTyp
878 , arg_wip_req_id  		IN OUT  NOCOPY  NumTblTyp
879 , arg_req_load_id 		IN OUT  NOCOPY  NumTblTyp
880 , arg_req_resched_id 		IN OUT  NOCOPY  NumTblTyp
881 , arg_released_instance         IN OUT  NOCOPY  NumTblTyp
882 , arg_mode                      IN      VARCHAR2
883 , arg_transaction_id            IN      NUMBER
884 , arg_loaded_int_reqs               IN OUT  NOCOPY  NumTblTyp
885 , arg_resched_int_reqs              IN OUT  NOCOPY  NumTblTyp
886 , arg_int_req_load_id               IN OUT  NOCOPY  NumTblTyp
887 , arg_int_req_resched_id            IN OUT  NOCOPY  NumTblTyp
888 , arg_loaded_int_repair_orders      IN OUT  NOCOPY  NumTblTyp -- bug 6021045
889 , arg_int_repair_orders_id          IN OUT  NOCOPY  NumTblTyp -- bug 6021045
890 , arg_loaded_ext_repair_orders      IN OUT  NOCOPY  NumTblTyp -- bug 6051361
891 , arg_ext_repair_orders_id          IN OUT  NOCOPY  NumTblTyp -- bug 6051361
892 ) IS
893 
894 CURSOR c_Instance IS
895 SELECT apps.instance_id,
896        apps.apps_ver,
897        apps.instance_type,
898        DECODE(apps.m2a_dblink,NULL,' ', '@' || m2a_dblink),
899        LENGTH( apps.instance_code)+2
900 FROM   msc_apps_instances apps,
901        ( SELECT distinct
902                 sr_instance_id
903            FROM msc_plan_organizations_v plan_org
904           WHERE plan_org.plan_id = arg_plan_id
905             AND plan_org.organization_id = arg_org_id
906             AND plan_org.owning_sr_instance = arg_sr_instance
907             AND plan_org.sr_instance_id =
908                          decode(arg_log_sr_instance,
909                                 arg_sr_instance, plan_org.sr_instance_id,
910                                 arg_log_sr_instance)) ins
911 WHERE apps.instance_id = ins.sr_instance_id;
912 
913   l_sr_instance_id    NUMBER;
914   l_apps_ver          VARCHAR2(10);
915   l_dblink            VARCHAR2(128);
916 
917   l_user_name         VARCHAR2(100):= NULL;
918   l_resp_name         VARCHAR2(100):= NULL;
919   l_application_name  VARCHAR2(240):= NULL;
920 
921   l_user_id           NUMBER;
922   l_resp_id           NUMBER;
923   l_application_id    NUMBER;
924 
925 --  l_sql_stmt          VARCHAR2(4000);
926 
927   l_loaded_jobs       NUMBER;
928   l_loaded_reqs       NUMBER;
929   l_loaded_scheds     NUMBER;
930   l_resched_jobs      NUMBER;
931 
932   l_resched_reqs      NUMBER;
933   l_wip_req_id        NUMBER;
934   l_req_load_id       NUMBER;
935   l_req_resched_id    NUMBER;
936 
937   lv_count            NUMBER:= 0;
938 
939  arg_loaded_lot_jobs  NumTblTyp;
940  arg_resched_lot_jobs NumTblTyp;
941  arg_osfm_req_id      NumTblTyp;
942 
943   l_loaded_lot_jobs   NUMBER;
944   l_resched_lot_jobs  NUMBER;
945   l_osfm_req_id       NUMBER;
946 
947   l_wip_group_id      NUMBER;
948   l_po_batch_number   NUMBER;
949 
950   L_RESCHED_EAM_JOBS	   NUMBER; -- dsr
951   L_EAM_REQ_ID 		   NUMBER; -- dsr
952 
953   lv_error_buf        VARCHAR2(2000);
954   lv_ret_code	      NUMBER;
955 
956   l_load_int_jobs          number;
957   l_resched_int_jobs       number;
958   l_int_req_load_id        number;
959   l_int_req_resched_id     number;
960 
961   l_loaded_int_repair_orders number ;-- bug 6021045
962   l_int_repair_orders_id number ;-- bug 6021045
963 
964   l_loaded_ext_repair_orders number ;-- bug 6051361
965   l_ext_repair_orders_id number ;-- bug 6051361
966 
967 BEGIN
968 
969 
970    SELECT
971      DECODE(NVL(fnd_profile.value('MSC_RELEASED_BY_USER_ONLY') ,'N'), 'Y',1 ,2)
972      INTO  v_msc_released_only_by_user
973      FROM  DUAL;
974      v_batch_id_populated := 1;
975      IF (g_batch_id = g_prev_batch_id)  OR g_batch_id = -1 THEN
976            begin
977                -- populating batch_id from destination side seq.
978                Execute immediate 'select  mrp_workbench_query_s.nextval
979                                  FROM DUAL '
980                             into MSC_Rel_Plan_PUB.g_batch_id;
981                v_batch_id_populated := 2;
982            exception when others then
983             fnd_file.put_line(FND_FILE.LOG, sqlerrm);
984            end;
985      END IF;
986      g_prev_batch_id := g_batch_id;
987    SELECT
988        FND_GLOBAL.USER_ID,
989        FND_GLOBAL.USER_NAME,
990        FND_GLOBAL.RESP_NAME,
991        FND_GLOBAL.APPLICATION_NAME
992      INTO v_user_id,
993           l_user_name,
994            l_resp_name,
995            l_application_name
996      FROM  dual;
997 
998   -------- Release the planned order one instance by one instance.
999 
1000   OPEN c_Instance;
1001   LOOP
1002     FETCH c_Instance
1003      INTO l_sr_instance_id,
1004           l_apps_ver,
1005           v_curr_instance_type,
1006           l_dblink,
1007           v_instance_code_length;
1008 
1009     EXIT WHEN c_Instance%NOTFOUND;
1010 
1011     ---------- Initialize the remote process
1012     ---------- If the instance is discrete or mixed type
1013     ----------    AND the instance is at a remote database
1014 
1015    arg_loaded_jobs.extend(1);
1016    arg_loaded_reqs.extend(1);
1017    arg_loaded_scheds.extend(1);
1018    arg_resched_jobs.extend(1);
1019    arg_resched_reqs.extend(1);
1020    arg_wip_req_id.extend(1);
1021    arg_req_load_id.extend(1);
1022    arg_req_resched_id.extend(1);
1023    arg_released_instance.extend(1);
1024    arg_loaded_lot_jobs.extend(1);
1025    arg_resched_lot_jobs.extend(1);
1026    arg_osfm_req_id.extend(1);
1027 
1028    arg_loaded_int_reqs.extend(1);
1029    arg_resched_int_reqs.extend(1);
1030    arg_int_req_load_id.extend(1);
1031    arg_int_req_resched_id.extend(1);
1032 
1033    arg_loaded_int_repair_orders.extend(1); -- bug 6021045
1034    arg_int_repair_orders_id.extend(1); --bug 6021045
1035    arg_loaded_ext_repair_orders.extend(1); -- bug 6051361
1036    arg_ext_repair_orders_id.extend(1); --bug 6051361
1037 
1038    lv_count:= lv_count+1;
1039 
1040    arg_released_instance(lv_count):= l_sr_instance_id;
1041 
1042    -- initialize the Applications Environment --
1043    IF v_curr_instance_type IN ( G_INS_DISCRETE, G_INS_PROCESS, G_INS_MIXED) THEN
1044 
1045     l_sql_stmt:=
1046        'BEGIN'
1047      ||'  MRP_AP_REL_PLAN_PUB.INITIALIZE'||l_dblink
1048                          ||'( :l_user_name,'
1049                          ||'  :l_resp_name,'
1050                          ||'  :l_application_name,'
1051                          ||'  :l_wip_group_id,'
1052                          ||'  :l_po_batch_number);'
1053      ||'END;';
1054 
1055        EXECUTE IMMEDIATE l_sql_stmt
1056                    USING IN l_user_name,
1057                          IN l_resp_name,
1058                          IN l_application_name,
1059                          OUT l_wip_group_id,
1060                          OUT l_po_batch_number;
1061 
1062    END IF;
1063 
1064        l_loaded_jobs   := 0;
1065        l_loaded_reqs   := 0;
1066        l_loaded_scheds := 0;
1067        l_resched_jobs  := 0;
1068        l_loaded_lot_jobs := 0;
1069        l_resched_lot_jobs := 0;
1070        l_load_int_jobs   :=0;
1071        l_resched_int_jobs :=0;
1072 
1073        l_loaded_int_repair_orders:= 0 ;--bug 6021045
1074        l_loaded_ext_repair_orders:= 0 ;--bug 6051361
1075 
1076     -- load the msc interface tables, submit the request --
1077 
1078        LOAD_MSC_INTERFACE
1079                 (arg_dblink  => l_dblink,
1080                  arg_plan_id => arg_plan_id,
1081                  arg_log_org_id => arg_log_org_id,
1082                  arg_org_instance => l_sr_instance_id,
1083                  arg_owning_org_id => arg_org_id,
1084                  arg_owning_instance => arg_sr_instance,
1085                  arg_compile_desig => arg_compile_desig,
1086                  arg_user_id => arg_user_id,
1087                  arg_po_group_by => arg_po_group_by,
1088                  arg_po_batch_number => l_po_batch_number,
1089                  arg_wip_group_id => l_wip_group_id,
1090     ----------------------------------------------- Number of Loaded Orders
1091                  arg_loaded_jobs   => l_loaded_jobs,
1092                  arg_loaded_lot_jobs => l_loaded_lot_jobs,
1093                  arg_resched_lot_jobs => l_resched_lot_jobs,
1094                  arg_loaded_reqs   => l_loaded_reqs,
1095                  arg_loaded_scheds => l_loaded_scheds,
1096                  arg_resched_jobs  => l_resched_jobs,
1097     ----------------------------------------------- Request IDs
1098                  arg_resched_reqs =>   l_resched_reqs,
1099                  arg_wip_req_id =>     l_wip_req_id,
1100                  arg_osfm_req_id =>    l_osfm_req_id,
1101                  arg_req_load_id =>    l_req_load_id,
1102                  arg_req_resched_id => l_req_resched_id,
1103     -------------------------------------------------------------
1104                  arg_mode => arg_mode,
1105                  arg_transaction_id => arg_transaction_id,
1106                  l_apps_ver   =>  l_apps_ver,
1107                  -- the following 2 lines added for dsr
1108 		 arg_resched_eam_jobs => l_resched_eam_jobs, -- dsr: should we put NULL here?
1109 		 arg_eam_req_id => l_eam_req_id, -- dsr ?
1110     -------------------------------------------------------------
1111                  arg_loaded_int_reqs  => l_load_int_jobs,
1112                  arg_resched_int_reqs => l_resched_int_jobs,
1113                  arg_int_req_load_id  => l_int_req_load_id,
1114                  arg_int_req_resched_id => l_int_req_resched_id,
1115     -------------------------------------------------------------
1116                 arg_loaded_int_repair_orders=> l_loaded_int_repair_orders,   -- bug 6021045
1117                 arg_int_repair_orders_id=> l_int_repair_orders_id,   -- bug 6021045
1118                 arg_loaded_ext_repair_orders=> l_loaded_ext_repair_orders,   -- bug 6051361
1119                 arg_ext_repair_orders_id=> l_ext_repair_orders_id   -- bug 6051361
1120 		 );
1121 
1122 
1123 
1124    COMMIT WORK;
1125 
1126    arg_loaded_jobs(lv_count)   :=  l_loaded_jobs;
1127    arg_loaded_reqs(lv_count)   :=  l_loaded_reqs;
1128    arg_loaded_scheds(lv_count) :=  l_loaded_scheds;
1129    arg_resched_jobs(lv_count)  :=  l_resched_jobs;
1130 
1131    arg_loaded_lot_jobs(lv_count) := l_loaded_lot_jobs;
1132    arg_resched_lot_jobs(lv_count) := l_resched_lot_jobs;
1133 
1134    arg_resched_reqs(lv_count)  :=  l_resched_reqs;
1135    arg_wip_req_id(lv_count)    :=  l_wip_req_id;
1136 
1137    arg_osfm_req_id(lv_count)    :=  l_osfm_req_id;
1138    arg_req_load_id(lv_count)   :=  l_req_load_id;
1139    arg_req_resched_id(lv_count):=  l_req_resched_id;
1140 
1141    arg_loaded_int_reqs(lv_count) := l_load_int_jobs;
1142    arg_resched_int_reqs(lv_count) := l_resched_int_jobs;
1143    arg_int_req_load_id(lv_count) := l_int_req_load_id;
1144    arg_int_req_resched_id(lv_count) :=  l_int_req_resched_id;
1145 
1146     --IRO release
1147     arg_loaded_int_repair_orders(lv_count):= l_loaded_int_repair_orders;    -- bug 6021045
1148     arg_int_repair_orders_id(lv_count) := l_int_repair_orders_id;-- bug 6021045
1149 
1150     --ERO release
1151     arg_loaded_ext_repair_orders(lv_count):= l_loaded_ext_repair_orders;    -- bug 6051361
1152     arg_ext_repair_orders_id(lv_count) := l_ext_repair_orders_id;-- bug 6051361
1153 
1154    END LOOP;
1155 
1156    CLOSE c_Instance;
1157 
1158    arg_loaded_jobs.trim(1);
1159    arg_loaded_reqs.trim(1);
1160    arg_loaded_scheds.trim(1);
1161    arg_resched_jobs.trim(1);
1162    arg_resched_reqs.trim(1);
1163    arg_wip_req_id.trim(1);
1164    arg_req_load_id.trim(1);
1165    arg_req_resched_id.trim(1);
1166    arg_released_instance.trim(1);
1167    arg_loaded_int_reqs.trim(1);
1168    arg_resched_int_reqs.trim(1);
1169    arg_int_req_load_id.trim(1);
1170    arg_int_req_resched_id.trim(1);
1171 
1172     --IRO release
1173     arg_loaded_int_repair_orders.trim(1);    -- bug 6021045
1174     arg_int_repair_orders_id.trim(1);-- bug 6021045
1175 
1176     --ERO release
1177     arg_loaded_ext_repair_orders.trim(1);    -- bug 6051361
1178     arg_ext_repair_orders_id.trim(1);-- bug 6051361
1179 
1180 EXCEPTION
1181 
1182    WHEN OTHERS THEN
1183       IF c_Instance%ISOPEN THEN CLOSE c_Instance; END IF;
1184 
1185       RAISE;
1186 
1187 END MSC_Release_Plan_Sc;
1188 
1189 /***********************************************************
1190 PROCEDURE:        LOAD_MSC_INTERFACE
1191 
1192 *************************************************************/
1193 PROCEDURE LOAD_MSC_INTERFACE
1194 ( arg_dblink                    IN      VARCHAR2
1195 , arg_plan_id			IN      NUMBER
1196 , arg_log_org_id 		IN 	NUMBER
1197 , arg_org_instance              IN      NUMBER
1198 , arg_owning_org_id 	        IN	NUMBER
1199 , arg_owning_instance           IN      NUMBER
1200 , arg_compile_desig 		IN 	VARCHAR2
1201 , arg_user_id 			IN 	NUMBER
1202 , arg_po_group_by 		IN 	NUMBER
1203 , arg_po_batch_number 		IN 	NUMBER
1204 , arg_wip_group_id 		IN 	NUMBER
1205 , arg_loaded_jobs 		IN OUT 	NOCOPY  NUMBER
1206 , arg_loaded_lot_jobs           IN OUT  NOCOPY  NUMBER
1207 , arg_resched_lot_jobs          IN OUT  NOCOPY  NUMBER
1208 , arg_loaded_reqs 		IN OUT  NOCOPY  NUMBER
1209 , arg_loaded_scheds 		IN OUT  NOCOPY  NUMBER
1210 , arg_resched_jobs 		IN OUT  NOCOPY  NUMBER
1211 , arg_resched_reqs 		IN OUT  NOCOPY  NUMBER
1212 , arg_wip_req_id 		IN OUT  NOCOPY  NUMBER
1213 , arg_osfm_req_id               IN OUT  NOCOPY  NUMBER
1214 , arg_req_load_id 		IN OUT  NOCOPY  NUMBER
1215 , arg_req_resched_id 		IN OUT  NOCOPY  NUMBER
1216 , arg_mode                      IN      VARCHAR2
1217 , arg_transaction_id            IN      NUMBER
1218 , l_apps_ver                    IN VARCHAR2
1219 -- dsr
1220 , arg_resched_eam_jobs          IN OUT  NOCOPY  NUMBER
1221 , arg_eam_req_id                IN OUT  NOCOPY  NUMBER
1222 , arg_loaded_int_reqs               IN OUT  NOCOPY  Number
1223 , arg_resched_int_reqs              IN OUT  NOCOPY  Number
1224 , arg_int_req_load_id               IN OUT  NOCOPY  Number
1225 , arg_int_req_resched_id            IN OUT  NOCOPY  Number
1226 , arg_loaded_int_repair_orders      IN OUT  NOCOPY  Number -- bug 6021045
1227 , arg_int_repair_orders_id          IN OUT  NOCOPY  Number -- bug 6021045
1228 , arg_loaded_ext_repair_orders      IN OUT  NOCOPY  Number -- bug 6051361
1229 , arg_ext_repair_orders_id          IN OUT  NOCOPY  Number -- bug 6051361
1230 ) IS
1231    TYPE CurTyp is ref cursor;
1232 
1233    CURSOR c_plan_type(p_plan_id number) IS
1234       select plan_type
1235         from msc_plans a
1236         where
1237         plan_id = p_plan_id;
1238 
1239     VERSION                 CONSTANT CHAR(80) :=
1240     '$Header: MSCPRELB.pls 120.41.12010000.13 2008/12/23 11:21:01 vsiyer ship $';
1241 
1242     lv_launch_process      INTEGER;
1243     lv_handle              VARCHAR2(200);
1244     lv_output              NUMBER;
1245     lv_error_stmt          VARCHAR2(2000) := NULL;
1246 
1247     lv_sql_stmt            VARCHAR2(4000);
1248     lv_temp		    NUMBER;
1249 
1250     lv_wf                   NUMBER;
1251     lv_wf_load_type         NUMBER;
1252 
1253     retcode                varchar2(30);
1254 
1255     /* DWK */
1256     l_new_iso_num          number;
1257     l_reschedule_iso_num   number;
1258 
1259     l_new_iro_num number ; -- bug 6021045
1260     l_new_ero_num number ; -- bug 6051361
1261 
1262     c_batch_number  CurTyp;
1263     l_batch_number1  NUMBER;
1264     l_batch_number2  NUMBER;
1265 
1266     l_batch_number3 NUMBER;-- bug 6021045
1267     l_batch_number4 NUMBER;-- bug 6051361
1268     l_request_id    NUMBER;
1269 
1270     l_plan_type     NUMBER;
1271     lv_error_buf        VARCHAR2(2000);
1272     lv_ret_code       NUMBER;
1273 
1274     l_user_id            NUMBER;
1275     l_user_name          VARCHAR2(2000);
1276     l_resp_name          VARCHAR2(2000);
1277     l_application_name   VARCHAR2(2000);
1278     l_application_id     NUMBER;
1279 BEGIN
1280     -- if mode is NULL then it means that this procedure is called from PWB
1281     -- where we need to do batch processing
1282     -- If mode is WF, then we need to do this work only for the
1283     -- transaction_id that is passed in
1284     -- If the mode is WF_BATCH, this will only process the planned orders
1285     -- which need to be released, it won't process the supplies which needs
1286     -- to be rescheduled
1287 
1288     dbms_lock.allocate_unique(arg_compile_desig||
1289                                   to_char(arg_owning_org_id),lv_handle);
1290 
1291     lv_output := dbms_lock.request(lv_handle, 6, 32767, TRUE);
1292 
1293 /*
1294     IF l_plan_type = 8 AND l_apps_ver = MSC_UTIL. G_APPS115  THEN
1295 			 RAISE_APPLICATION_ERROR(-20000,'SRP release not supported to 11510 source',TRUE);
1296     END IF;
1297 */
1298     if(lv_output <> 0) then
1299         FND_MESSAGE.SET_NAME('MRP', 'GEN-LOCK-WARNING');
1300         FND_MESSAGE.SET_TOKEN('EVENT', 'RELEASE PLANNED ORDERS');
1301         lv_error_stmt := FND_MESSAGE.GET;
1302         raise_application_error(-20000, lv_error_stmt);
1303     end if;
1304 
1305     -- Get the hour uom code
1306     -- Get the profile MRP_PURCHASING_BY_REVISION is set
1307 
1308     lv_sql_stmt:=
1309        'BEGIN'
1310      ||' :v_hour_uom := FND_PROFILE.VALUE'||arg_dblink
1311                                    ||'(''BOM:HOUR_UOM_CODE'');'
1312      ||' :v_purchasing_by_rev := FND_PROFILE.VALUE'||arg_dblink
1313                                    ||'(''MRP_PURCHASING_BY_REVISION'');'
1314      ||'END;';
1315 
1316     EXECUTE IMMEDIATE lv_sql_stmt
1317             USING OUT v_hour_uom,
1318                   OUT v_purchasing_by_rev;
1319 
1320     lv_wf:= SYS_NO;
1321     -- Get the Load Type if it's 'WF' mode.
1322     IF arg_mode = 'WF' THEN
1323        lv_wf:= SYS_YES;
1324        BEGIN
1325           SELECT load_type
1326             INTO lv_wf_load_type
1327             FROM MSC_SUPPLIES s
1328            WHERE s.plan_id = arg_plan_id
1329              AND s.transaction_id = arg_transaction_id;
1330        EXCEPTION
1331           WHEN NO_DATA_FOUND THEN NULL;
1332           WHEN OTHERS THEN RAISE;
1333        END;
1334     END IF;
1335 
1336     IF arg_mode = 'WF_BATCH' then
1337        lv_wf := 3;
1338     END IF;
1339 
1340    /* start bug 6021045 */
1341     /* For releasing IRO (internal repair order )*/
1342     IF ( lv_wf_load_type = IRO_LOAD   OR
1343          lv_wf= SYS_NO   OR
1344          lv_wf =3)  AND l_apps_ver > MSC_UTIL.G_APPS115 THEN
1345 
1346           lv_sql_stmt := ' SELECT mrp_workbench_query_s.nextval from dual'|| arg_dblink;
1347           OPEN c_batch_number for lv_sql_stmt;
1348           FETCH c_batch_number INTO l_batch_number3;
1349           CLOSE c_batch_number;
1350 
1351 
1352 
1353           OPEN c_plan_type(arg_plan_id);
1354           FETCH c_plan_type INTO l_plan_type;
1355           CLOSE c_plan_type;
1356 
1357 
1358            IF l_plan_type = 8 THEN
1359 
1360                IF(MSC_Rel_Plan_PUB.G_SPP_SPLIT_YN = 'Y') THEN
1361                Release_IRO_2(
1362                                   p_dblink  => arg_dblink,
1363                                   p_arg_iro_batch_number => l_batch_number3,
1364                                   p_arg_owning_instance => arg_owning_instance,
1365                                   p_arg_po_group_by     => arg_po_group_by,
1366                                   p_arg_plan_id         => arg_plan_id,
1367                                   p_arg_log_org_id      => arg_log_org_id,
1368                                   p_arg_owning_org_id  => arg_owning_org_id,
1369                                   p_arg_org_instance   => arg_org_instance,
1370                                   p_arg_mode           => arg_mode,
1371                                   p_arg_transaction_id  => arg_transaction_id,
1372                                   p_arg_loaded_int_repair_orders  =>l_new_iro_num,
1373                                   p_load_type     => IRO_LOAD);
1374                  ELSE
1375                      Release_IRO(
1376                                   p_dblink  => arg_dblink,
1377                                   p_arg_iro_batch_number => l_batch_number3,
1378                                   p_arg_owning_instance => arg_owning_instance,
1379                                   p_arg_po_group_by     => arg_po_group_by,
1380                                   p_arg_plan_id         => arg_plan_id,
1381                                   p_arg_log_org_id      => arg_log_org_id,
1382                                   p_arg_owning_org_id  => arg_owning_org_id,
1383                                   p_arg_org_instance   => arg_org_instance,
1384                                   p_arg_mode           => arg_mode,
1385                                   p_arg_transaction_id  => arg_transaction_id,
1386                                   p_arg_loaded_int_repair_orders  =>l_new_iro_num,
1387                                   p_load_type     => IRO_LOAD);
1388                   END IF;
1389 
1390                 arg_loaded_int_repair_orders:= l_new_iro_num;
1391 
1392 
1393                      l_user_id := FND_GLOBAL.USER_ID;
1394                      l_user_name := FND_GLOBAL.USER_NAME;
1395                      l_resp_name := FND_GLOBAL.RESP_NAME;
1396                      l_application_name := FND_GLOBAL.APPLICATION_NAME;
1397 
1398 
1399                   SELECT APPLICATION_ID
1400                      INTO l_application_id
1401                      FROM FND_APPLICATION_VL
1402                      WHERE APPLICATION_NAME = l_application_name;
1403 
1404                  IF ( arg_loaded_int_repair_orders > 0) THEN
1405                        lv_sql_stmt:= ' BEGIN '
1406                           ||' MSC_SRP_RELEASE.MSC_RELEASE_IRO'||arg_dblink
1407                           ||'(:l_user_name,:l_resp_name,:l_application_name,:l_application_id,:l_batch_number, 256, '
1408                           ||' :arg_int_repair_orders_id);' ||
1409                         ' END;';
1410                     EXECUTE IMMEDIATE lv_sql_stmt
1411                     USING
1412                         IN l_user_name,
1413                         IN l_resp_name,
1414                         IN l_application_name,IN l_application_id,
1415                         IN l_batch_number3,
1416                         IN OUT arg_int_repair_orders_id;
1417 
1418                       COMMIT WORK;
1419                   END IF;
1420 
1421 
1422      END IF ;
1423 
1424     END IF ;
1425   /* end bug bug 6021045 */
1426 
1427    /* Start of Bug 6051361 */
1428   IF (lv_wf_load_type = ERO_LOAD   OR
1429          lv_wf= SYS_NO   OR
1430         lv_wf =3)  AND l_apps_ver > MSC_UTIL.G_APPS115 THEN
1431 
1432           lv_sql_stmt := ' SELECT mrp_workbench_query_s.nextval from dual'|| arg_dblink;
1433           OPEN c_batch_number for lv_sql_stmt;
1434           FETCH c_batch_number INTO l_batch_number4;
1435           CLOSE c_batch_number;
1436 
1437 
1438 
1439           OPEN c_plan_type(arg_plan_id);
1440           FETCH c_plan_type INTO l_plan_type;
1441           CLOSE c_plan_type;
1442 
1443        --execute immediate ' SELECT mrp_workbench_query_s@arg_dblink.nextval  INTO l_batch_number4  from dual'|| arg_dblink;
1444 
1445        --SELECT msc_plans.mrp_plan_type INTO l_plan_type from MSC_PLANS
1446        --WHERE PLAN_ID =  arg_plan_id ;
1447 
1448        IF l_plan_type = 8 THEN
1449 
1450                             IF(MSC_Rel_Plan_PUB.G_SPP_SPLIT_YN = 'Y') THEN
1451 
1452                                 Release_ERO_2(
1453                                   p_dblink  => arg_dblink,
1454                                   p_arg_ero_batch_number => l_batch_number4,
1455                                   p_arg_owning_instance => arg_owning_instance,
1456                                   p_arg_po_group_by     => arg_po_group_by,
1457                                   p_arg_plan_id         => arg_plan_id,
1458                                   p_arg_log_org_id      => arg_log_org_id,
1459                                   p_arg_owning_org_id  => arg_owning_org_id,
1460                                   p_arg_org_instance   => arg_org_instance,
1461                                   p_arg_mode           => arg_mode,
1462                                   p_arg_transaction_id  => arg_transaction_id,
1463                                   p_arg_loaded_ext_repair_orders  =>l_new_ero_num,
1464                                   p_load_type     =>  ERO_LOAD);
1465 
1466                               ELSE
1467 
1468                                   Release_ERO(
1469                                   p_dblink  => arg_dblink,
1470                                   p_arg_ero_batch_number => l_batch_number4,
1471                                   p_arg_owning_instance => arg_owning_instance,
1472                                   p_arg_po_group_by     => arg_po_group_by,
1473                                   p_arg_plan_id         => arg_plan_id,
1474                                   p_arg_log_org_id      => arg_log_org_id,
1475                                   p_arg_owning_org_id  => arg_owning_org_id,
1476                                   p_arg_org_instance   => arg_org_instance,
1477                                   p_arg_mode           => arg_mode,
1478                                   p_arg_transaction_id  => arg_transaction_id,
1479                                   p_arg_loaded_ext_repair_orders  =>l_new_ero_num,
1480                                   p_load_type     =>  ERO_LOAD);
1481 
1482           END IF;
1483        END IF ;
1484     arg_loaded_ext_repair_orders:= l_new_ero_num ;
1485 
1486               l_user_id := FND_GLOBAL.USER_ID;
1487               l_user_name := FND_GLOBAL.USER_NAME;
1488               l_resp_name := FND_GLOBAL.RESP_NAME;
1489               l_application_name := FND_GLOBAL.APPLICATION_NAME;
1490 
1491              select nvl(application_id,724)
1492               into l_application_id
1493                  from fnd_application_vl
1494                  where application_name =l_application_name;
1495 
1496     IF (arg_loaded_ext_repair_orders > 0) THEN
1497 
1498                  Lv_sql_stmt :='BEGIN  '
1499 			|| ' MRP_CL_FUNCTION.SUBMIT_CR'||arg_dblink
1500 		  ||'(:l_user_name,:l_resp_name,:l_application_name,:l_application_id,:l_batch_id ,:l_conc_req_short_name ,:l_conc_request_desc,'
1501   		||':l_owning_appl_short_name,  128,'
1502       ||' :l_request_id );'
1503       ||' END; ';
1504 
1505 	    --Execute Immediate lv_sql_stmt using  l
1506 
1507    	 EXECUTE IMMEDIATE lv_sql_stmt
1508                   USING
1509                   IN l_user_name,
1510                   IN l_resp_name,
1511                   IN l_application_name,IN l_application_id,
1512                   IN l_batch_number4,'MSCRLERO' ,'Release ERO To Source','MSC',
1513                   IN OUT l_request_id;
1514     END IF;
1515 
1516   END IF ;
1517 COMMIT WORK;
1518 
1519    /*end of Bug 6051361 */
1520 /* DWK */
1521     /* For Creating Internal Sales Orders and
1522            Rescheduling Internal Sales Orders */
1523 
1524     --- ISO/IR LOAD/Reschedule ---
1525     IF ( lv_wf_load_type = DRP_REQ_LOAD   OR
1526          lv_wf_load_type = DRP_REQ_RESCHED OR
1527    --      lv_wf_load_type = ASCP_IREQ_RESCHED OR		 -- IR/ISO resch Proj
1528          lv_wf= SYS_NO   OR
1529          lv_wf =3) THEN
1530 
1531        /* In case arg_mode is NULL then lv_wf = SYS_NO, and there will be
1532           no transaction id nor lv_wf_load_type.  Therefore, we need to call
1533           the populate_iso_in_source twice. One for new iso, another for re-sche
1534           of ISO with hardcoded load_type. */
1535 
1536        lv_sql_stmt := ' SELECT mrp_workbench_query_s.nextval from dual'|| arg_dblink;
1537 
1538        OPEN c_batch_number for lv_sql_stmt;
1539        FETCH c_batch_number INTO l_batch_number1;
1540        CLOSE c_batch_number;
1541 
1542        OPEN c_batch_number for lv_sql_stmt;
1543        FETCH c_batch_number INTO l_batch_number2;
1544        CLOSE c_batch_number;
1545 
1546        OPEN c_plan_type(arg_plan_id);
1547        FETCH c_plan_type INTO l_plan_type;
1548        CLOSE c_plan_type;
1549 
1550      IF ((l_plan_type = 5) OR (l_plan_type = 8)) THEN
1551        IF  (lv_wf = SYS_NO) and (lv_wf_load_type IS NULL) THEN
1552          /* For releasing planned arrival as int req/iso */
1553 
1554               POPULATE_ISO_IN_SOURCE(
1555                                   l_dblink  => arg_dblink,
1556                                   l_arg_po_batch_number => l_batch_number1 ,
1557                                   l_arg_owning_instance => arg_owning_instance,
1558                                   l_arg_po_group_by     => arg_po_group_by,
1559                                   l_arg_plan_id         => arg_plan_id,
1560                                   l_arg_log_org_id      => arg_log_org_id,
1561                                   l_arg_owning_org_id   => arg_owning_org_id,
1562                                   l_arg_org_instance    => arg_org_instance,
1563                                   l_arg_mode            => arg_mode,
1564                                   l_arg_transaction_id  => arg_transaction_id,
1565                                   arg_loaded_int_reqs   => l_new_iso_num,
1566                                   arg_resched_int_reqs  => l_reschedule_iso_num,
1567                                   p_load_type           => DRP_REQ_LOAD);
1568                                arg_loaded_int_reqs := l_new_iso_num;
1569 
1570                   IF (arg_loaded_int_reqs > 0) THEN
1571                   COMMIT WORK;
1572                   lv_sql_stmt:= ' BEGIN '
1573                           ||' MRP_CREATE_SCHEDULE_ISO.MSC_RELEASE_ISO'||arg_dblink
1574                           ||'(:l_batch_number, 32, '
1575                           ||' :arg_int_req_load_id,:arg_int_req_resched_id);' ||
1576                         ' END;';
1577                    EXECUTE IMMEDIATE lv_sql_stmt
1578                     USING IN l_batch_number1,
1579                         IN OUT arg_int_req_load_id, IN OUT lv_temp;
1580                    END IF;
1581 
1582               /* for rescheduling int reqs by updating iso */
1583 
1584               POPULATE_ISO_IN_SOURCE(
1585                                   l_dblink  => arg_dblink,
1586                                   l_arg_po_batch_number => l_batch_number2 ,
1587                                   l_arg_owning_instance => arg_owning_instance,
1588                                   l_arg_po_group_by     => arg_po_group_by,
1589                                   l_arg_plan_id         => arg_plan_id,
1590                                   l_arg_log_org_id      => arg_log_org_id,
1591                                   l_arg_owning_org_id   => arg_owning_org_id,
1592                                   l_arg_org_instance    => arg_org_instance,
1593                                   l_arg_mode            => arg_mode,
1594                                   l_arg_transaction_id  => arg_transaction_id,
1595                                   arg_loaded_int_reqs   => l_new_iso_num,
1596                                   arg_resched_int_reqs  => l_reschedule_iso_num,
1597                                   p_load_type           => DRP_REQ_RESCHED);
1598                                   arg_resched_int_reqs := l_reschedule_iso_num;
1599 
1600                     IF (arg_resched_int_reqs > 0) THEN
1601                     COMMIT WORK;
1602                     lv_wf_load_type := DRP_REQ_RESCHED;
1603                     lv_sql_stmt:= ' BEGIN '
1604                           ||' MRP_CREATE_SCHEDULE_ISO.MSC_RELEASE_ISO'||arg_dblink
1605                           ||'(:l_batch_number, 64, '
1606                           ||' :arg_int_req_load_id,:arg_int_req_resched_id);' ||
1607                         ' END;';
1608                     EXECUTE IMMEDIATE lv_sql_stmt
1609                      USING IN l_batch_number2,
1610                         IN OUT lv_temp, IN OUT arg_int_req_resched_id;
1611                     END IF;
1612 
1613 
1614        ELSE
1615 
1616           /* for lv_wf = WF. creates ireq/iso if lv_wf_type =  DRP_REQ_LOAD
1617              or reschedules iso if lv_wf_type = DRP_REQ_RESCHED */
1618 
1619                     POPULATE_ISO_IN_SOURCE(
1620                                   l_dblink  => arg_dblink,
1621                                   l_arg_po_batch_number => l_batch_number1 ,
1622                                   l_arg_owning_instance => arg_owning_instance,
1623                                   l_arg_po_group_by     => arg_po_group_by,
1624                                   l_arg_plan_id         => arg_plan_id,
1625                                   l_arg_log_org_id      => arg_log_org_id,
1626                                   l_arg_owning_org_id   => arg_owning_org_id,
1627                                   l_arg_org_instance    => arg_org_instance,
1628                                   l_arg_mode            => arg_mode,
1629                                   l_arg_transaction_id  => arg_transaction_id,
1630                                   arg_loaded_int_reqs   => arg_loaded_int_reqs,
1631                                   arg_resched_int_reqs  => arg_resched_int_reqs,
1632                                   p_load_type           => lv_wf_load_type);
1633 
1634                  IF (arg_loaded_int_reqs > 0 OR arg_resched_int_reqs > 0) THEN
1635                  COMMIT WORK;
1636                  lv_sql_stmt:= ' BEGIN '
1637                           ||' MRP_CREATE_SCHEDULE_ISO.MSC_RELEASE_ISO'||arg_dblink
1638                           ||'(:l_batch_number, :lv_wf_load_type, '
1639                           ||' :arg_int_req_load_id,:arg_int_req_resched_id);' ||
1640                         ' END;';
1641                   EXECUTE IMMEDIATE lv_sql_stmt
1642                      USING IN l_batch_number1, IN lv_wf_load_type,
1643                         IN OUT arg_int_req_load_id, IN OUT arg_int_req_resched_id;
1644                   END IF;
1645 
1646 
1647 	   END IF;
1648        -- IR/ISO resch Proj Start
1649 	  ELSIF ((l_plan_type in (1,2,3)) AND (l_apps_ver >= MSC_UTIL.G_APPS121)) THEN
1650 		-- Call Need to reschedule ISOs that have corresponding IRs in
1651 		-- the same plan. Here we need to call the same OM routine as
1652 		-- the DRP code for a new load_type ASCP_IREQ_RESCHED
1653 
1654 		IF  (lv_wf = SYS_NO) and (lv_wf_load_type IS NULL) THEN
1655 
1656 		   POPULATE_ISO_IN_SOURCE(
1657                                   l_dblink  => arg_dblink,
1658                                   l_arg_po_batch_number => l_batch_number2 ,
1659                                   l_arg_owning_instance => arg_owning_instance,
1660                                   l_arg_po_group_by     => arg_po_group_by,
1661                                   l_arg_plan_id         => arg_plan_id,
1662                                   l_arg_log_org_id      => arg_log_org_id,
1663                                   l_arg_owning_org_id   => arg_owning_org_id,
1664                                   l_arg_org_instance    => arg_org_instance,
1665                                   l_arg_mode            => arg_mode,
1666                                   l_arg_transaction_id  => arg_transaction_id,
1667                                   arg_loaded_int_reqs   => l_new_iso_num,
1668                                   arg_resched_int_reqs  => l_reschedule_iso_num,
1669                                   p_load_type           => DRP_REQ_RESCHED);
1670 
1671 		   arg_resched_int_reqs := l_reschedule_iso_num;
1672 
1673 		   IF (arg_resched_int_reqs > 0) THEN
1674 			  COMMIT WORK;
1675 			  lv_wf_load_type := DRP_REQ_RESCHED;
1676 			  -- Here while calling the Source API, we still pass the
1677 			  -- load_type as DRP_REQ_RESCHED (64)
1678 
1679 			  lv_sql_stmt:= ' BEGIN '
1680                           ||' MRP_CREATE_SCHEDULE_ISO.MSC_RELEASE_ISO'||arg_dblink
1681                           ||'(:l_batch_number, 64, '
1682                           ||' :arg_int_req_load_id,:arg_int_req_resched_id);' ||
1683                         ' END;';
1684 
1685 			  EXECUTE IMMEDIATE lv_sql_stmt
1686 				USING IN l_batch_number2,
1687 				IN OUT lv_temp, IN OUT arg_int_req_resched_id;
1688 
1689 		   END IF;
1690 		 ELSIF (lv_wf_load_type = DRP_REQ_RESCHED ) THEN
1691 
1692 		   POPULATE_ISO_IN_SOURCE(
1693                                   l_dblink  => arg_dblink,
1694                                   l_arg_po_batch_number => l_batch_number1 ,
1695                                   l_arg_owning_instance => arg_owning_instance,
1696                                   l_arg_po_group_by     => arg_po_group_by,
1697                                   l_arg_plan_id         => arg_plan_id,
1698                                   l_arg_log_org_id      => arg_log_org_id,
1699                                   l_arg_owning_org_id   => arg_owning_org_id,
1700                                   l_arg_org_instance    => arg_org_instance,
1701                                   l_arg_mode            => arg_mode,
1702                                   l_arg_transaction_id  => arg_transaction_id,
1703                                   arg_loaded_int_reqs   => arg_loaded_int_reqs,
1704                                   arg_resched_int_reqs  => arg_resched_int_reqs,
1705                                   p_load_type           => lv_wf_load_type);
1706 
1707 
1708 		   IF (arg_loaded_int_reqs > 0 OR arg_resched_int_reqs > 0) THEN
1709 
1710 			  COMMIT WORK;
1711 			  lv_wf_load_type := DRP_REQ_RESCHED;
1712 
1713 			  lv_sql_stmt:= ' BEGIN '
1714                           ||' MRP_CREATE_SCHEDULE_ISO.MSC_RELEASE_ISO'||arg_dblink
1715                           ||'(:l_batch_number, :lv_wf_load_type, '
1716                           ||' :arg_int_req_load_id,:arg_int_req_resched_id);' ||
1717                         ' END;';
1718 
1719 			  EXECUTE IMMEDIATE lv_sql_stmt
1720 				USING IN l_batch_number1, IN lv_wf_load_type,
1721 				IN OUT arg_int_req_load_id, IN OUT arg_int_req_resched_id;
1722 
1723 		   END IF;
1724 
1725     -- IR/ISO resch Proj End
1726      END IF;
1727     END IF;
1728   END IF;
1729 
1730 
1731     --- WIP_DIS_MASS_LOAD ---
1732     IF lv_wf= SYS_NO OR
1733        lv_wf_load_type= WIP_DIS_MASS_LOAD OR
1734        lv_wf =3 THEN
1735 
1736        arg_loaded_jobs:= load_wip_discrete_jobs
1737                              ( arg_plan_id,
1738                                arg_log_org_id,
1739                                arg_org_instance,
1740                                arg_owning_org_id,
1741                                arg_owning_instance,
1742                                arg_user_id,
1743                                arg_wip_group_id,
1744                                arg_mode,
1745                                arg_transaction_id,
1746                                l_apps_ver );
1747 
1748        arg_loaded_lot_jobs:= load_osfm_lot_jobs
1749                              ( arg_plan_id,
1750                                arg_log_org_id,
1751                                arg_org_instance,
1752                                arg_owning_org_id,
1753                                arg_owning_instance,
1754                                arg_user_id,
1755                                arg_wip_group_id,
1756                                arg_mode,
1757                                arg_transaction_id,
1758                                l_apps_ver );
1759 
1760 
1761     END IF;
1762 
1763     --- WIP_DIS_MASS_RESCHEDULE ---
1764     IF lv_wf= SYS_NO OR
1765        lv_wf_load_type= WIP_DIS_MASS_RESCHEDULE or
1766    --  lv_wf_load_type= EAM_RESCHEDULE_WORK_ORDER OR -- 21 bug# 4524589
1767        lv_wf_load_type = 6 THEN
1768 
1769        arg_resched_jobs:= reschedule_wip_discrete_jobs
1770                              ( arg_plan_id,
1771                                arg_log_org_id,
1772                                arg_org_instance,
1773                                arg_owning_org_id,
1774                                arg_owning_instance,
1775                                arg_user_id,
1776                                arg_wip_group_id,
1777                                arg_mode,
1778                                arg_transaction_id,
1779                                l_apps_ver,
1780 			       WIP_DIS_MASS_RESCHEDULE
1781 			      );
1782 
1783        arg_resched_lot_jobs := reschedule_osfm_lot_jobs
1784                              ( arg_plan_id,
1785                                arg_log_org_id,
1786                                arg_org_instance,
1787                                arg_owning_org_id,
1788                                arg_owning_instance,
1789                                arg_user_id,
1790                                arg_wip_group_id,
1791                                arg_mode,
1792                                arg_transaction_id );
1793 
1794         -- dsr: added the following new api call
1795     /*  arg_resched_eam_jobs:= reschedule_wip_discrete_jobs
1796                              ( arg_plan_id,
1797                                arg_log_org_id,
1798                                arg_org_instance,
1799                                arg_owning_org_id,
1800                                arg_owning_instance,
1801                                arg_user_id,
1802                                arg_wip_group_id,
1803                                arg_mode,
1804                                arg_transaction_id,
1805                                l_apps_ver ,
1806 			       21 -- dsr arg_load_type eam rescheduled wo
1807 			     );    */
1808 
1809     END IF;
1810 
1811     --- WIP_REP_MASS_LOAD ---
1812     IF lv_wf= SYS_NO OR
1813        lv_wf_load_type= WIP_REP_MASS_LOAD OR
1814        lv_wf = 3 THEN
1815 
1816        arg_loaded_scheds:= load_repetitive_schedules
1817                              ( arg_plan_id,
1818                                arg_log_org_id,
1819                                arg_org_instance,
1820                                arg_owning_org_id,
1821                                arg_owning_instance,
1822                                arg_user_id,
1823                                arg_wip_group_id,
1824                                arg_mode,
1825                                arg_transaction_id );
1826     END IF;
1827 
1828     --- PO_MASS_LOAD ---
1829 
1830 
1831     IF lv_wf= SYS_NO OR
1832        lv_wf_load_type= PO_MASS_LOAD OR
1833        lv_wf = 3 THEN
1834 
1835 
1836        arg_loaded_reqs:= load_po_requisitions
1837                              ( arg_plan_id,
1838                                arg_log_org_id,
1839                                arg_org_instance,
1840                                arg_owning_org_id,
1841                                arg_owning_instance,
1842                                arg_user_id,
1843                                arg_po_group_by,
1844                                arg_po_batch_number,
1845                                arg_mode,
1846                                arg_transaction_id );
1847     END IF;
1848 
1849     --- PO_MASS_RESCHEDULE ---
1850     IF lv_wf= SYS_NO OR
1851 	  (lv_wf_load_type= PO_MASS_RESCHEDULE OR
1852 	   (lv_wf_load_type = DRP_REQ_RESCHED and
1853 		l_apps_ver <= MSC_UTIL.G_APPS120)) THEN   -- IR/ISO resch Proj
1854 
1855        arg_resched_reqs:= reschedule_po
1856                              ( arg_plan_id,
1857                                arg_log_org_id,
1858                                arg_org_instance,
1859                                arg_owning_org_id,
1860                                arg_owning_instance,
1861                                arg_user_id,
1862                                arg_po_group_by,
1863                                arg_mode,
1864                                arg_transaction_id );
1865     END IF;
1866 
1867 
1868     MSC_RELEASE_HOOK.EXTEND_RELEASE
1869                  (lv_error_buf,
1870                   lv_ret_code,
1871                  arg_dblink  => arg_dblink,
1872                  arg_plan_id => arg_plan_id,
1873                  arg_log_org_id => arg_log_org_id,
1874                  arg_org_instance => arg_org_instance,
1875                  arg_owning_org_id => arg_owning_org_id,
1876                  arg_owning_instance => arg_owning_instance,
1877                  arg_compile_desig => arg_compile_desig,
1878                  arg_user_id => arg_user_id,
1879                  arg_po_group_by => arg_po_group_by,
1880                  arg_po_batch_number => arg_po_batch_number,
1881                  arg_wip_group_id => arg_wip_group_id,
1882     ----------------------------------------------- Number of Loaded Orders
1883                  arg_loaded_jobs   => arg_loaded_jobs,
1884                  arg_loaded_lot_jobs => arg_loaded_lot_jobs,
1885                  arg_resched_lot_jobs => arg_loaded_lot_jobs,
1886                  arg_loaded_reqs   => arg_loaded_reqs,
1887                  arg_loaded_scheds => arg_loaded_scheds,
1888                  arg_resched_jobs  => arg_resched_jobs,
1889                  arg_int_repair_orders=>arg_loaded_int_repair_orders,
1890                  arg_ext_repair_orders=>arg_loaded_ext_repair_orders,
1891     ----------------------------------------------- Request IDs
1892                  arg_resched_reqs =>   arg_resched_reqs,
1893                  arg_wip_req_id =>     arg_wip_req_id,
1894                  arg_osfm_req_id =>    arg_osfm_req_id,
1895                  arg_req_load_id =>    arg_req_load_id,
1896                  arg_req_resched_id => arg_req_resched_id,
1897                  arg_int_repair_Order_id=>arg_int_repair_orders_id,
1898                  arg_ext_repair_Order_id=>arg_ext_repair_orders_id,
1899     -------------------------------------------------------------
1900                  arg_mode => arg_mode,
1901                  arg_transaction_id => arg_transaction_id,
1902                  l_apps_ver   =>  l_apps_ver);
1903 
1904 
1905        IF lv_ret_code=-1 THEN /* custom hook returned error*/
1906         	 FND_MESSAGE.SET_NAME('MSC','MSC_ERROR_REL_CUSTOM_HOOK');
1907      			 RAISE_APPLICATION_ERROR(-20000,FND_MESSAGE.GET||lv_error_buf,TRUE);
1908    			END IF;
1909 
1910 
1911 commit;
1912 
1913     -- call (remote) procedures to submit the concuncurrent request --
1914 
1915  IF v_curr_instance_type IN ( G_INS_DISCRETE, G_INS_PROCESS, G_INS_MIXED) THEN
1916 
1917     IF arg_loaded_jobs+arg_resched_jobs+arg_loaded_scheds > 0  THEN
1918 
1919         lv_sql_stmt:=
1920          'BEGIN'
1921         ||' MRP_AP_REL_PLAN_PUB.LD_WIP_JOB_SCHEDULE_INTERFACE'||arg_dblink
1922                   ||'( :arg_wip_req_id );'
1923         ||' END;';
1924 
1925         EXECUTE IMMEDIATE lv_sql_stmt
1926                 USING OUT arg_wip_req_id;
1927 
1928       END IF;
1929 
1930 
1931       IF arg_loaded_lot_jobs + nvl(arg_resched_lot_jobs,0) > 0 then
1932 
1933         lv_sql_stmt:=
1934          'BEGIN'
1935         ||' MRP_AP_REL_PLAN_PUB.LD_LOT_JOB_SCHEDULE_INTERFACE'||arg_dblink
1936                   ||'( :arg_osfm_req_id );'
1937         ||' END;';
1938 
1939         EXECUTE IMMEDIATE lv_sql_stmt
1940                 USING OUT arg_osfm_req_id;
1941 
1942 
1943        END IF;
1944 
1945        -- dsr: added the following lines for eam
1946      /* IF nvl(arg_resched_eam_jobs,0) > 0 then
1947 
1948         lv_sql_stmt:=
1949          'BEGIN'
1950         ||' MRP_AP_REL_PLAN_PUB.LD_EAM_RESCHEDULE_JOBS'||arg_dblink
1951                   ||'(:arg_eam_req_id );'
1952         ||' END;';
1953 
1954         EXECUTE IMMEDIATE lv_sql_stmt
1955                 USING OUT arg_eam_req_id;
1956 
1957        END IF; */
1958 
1959 
1960    /*     DELETE msc_wip_job_schedule_interface
1961          WHERE sr_instance_id= arg_org_instance;
1962 
1963         DELETE MSC_WIP_JOB_DTLS_INTERFACE
1964          WHERE sr_instance_id= arg_org_instance;
1965   */
1966     IF arg_loaded_reqs > 0 THEN
1967       DECLARE po_group_by_name VARCHAR2(10);
1968       BEGIN
1969         IF arg_po_group_by = 1 THEN
1970           po_group_by_name := 'ALL';
1971         ELSIF arg_po_group_by = 2 THEN
1972           po_group_by_name := 'ITEM';
1973         ELSIF arg_po_group_by = 3 THEN
1974           po_group_by_name := 'BUYER';
1975         ELSIF arg_po_group_by = 4 THEN
1976           po_group_by_name := 'PLANNER';
1977         ELSIF arg_po_group_by = 5 THEN
1978           po_group_by_name := 'VENDOR';
1979         ELSIF arg_po_group_by = 6 THEN
1980           po_group_by_name := 'ONE-EACH';
1981         ELSIF arg_po_group_by = 7 THEN
1982           po_group_by_name := 'CATEGORY';
1983         ELSIF arg_po_group_by = 8 THEN
1984           po_group_by_name := 'LOCATION';
1985         END IF;
1986 
1987         lv_sql_stmt:=
1988            'BEGIN'
1989          ||' MRP_AP_REL_PLAN_PUB.LD_PO_REQUISITIONS_INTERFACE'||arg_dblink
1990                   ||'( :po_group_by_name,'
1991                   ||'  :arg_req_load_id );'
1992          ||' END;';
1993 
1994          EXECUTE IMMEDIATE lv_sql_stmt
1995                  USING  IN po_group_by_name,
1996                        OUT arg_req_load_id;
1997    /*     DELETE MSC_PO_REQUISITIONS_INTERFACE
1998          WHERE sr_instance_id= arg_org_instance;
1999 */
2000       END;
2001     END IF;
2002 
2003     IF arg_resched_reqs > 0 THEN
2004 
2005        /*
2006        OPEN c_plan_type(arg_plan_id);
2007        FETCH c_plan_type INTO l_plan_type;
2008        CLOSE c_plan_type;
2009 
2010        IF l_plan_type = 5 THEN
2011           lv_sql_stmt :=
2012             'BEGIN ' ||
2013             'MRP_PO_RESCHEDULE.LAUNCH_RESCHEDULE_PO'||arg_dblink||
2014             '(:arg_req_resched_id); ' ||
2015             'END;';
2016          ELSE
2017          */
2018           lv_sql_stmt:=
2019             'BEGIN'
2020             ||' MRP_AP_REL_PLAN_PUB.LD_PO_RESCHEDULE_INTERFACE'||arg_dblink
2021             ||'( :arg_req_resched_id);'
2022             ||' END;';
2023       -- END IF;
2024 
2025           EXECUTE IMMEDIATE lv_sql_stmt
2026                   USING OUT arg_req_resched_id;
2027 
2028 
2029  /*         DELETE MSC_PO_RESCHEDULE_INTERFACE
2030            WHERE sr_instance_id= arg_org_instance;
2031 */
2032     END IF;
2033 
2034  ELSIF v_curr_instance_type in (G_INS_OTHER, G_INS_EXCHANGE) THEN
2035 
2036       lv_sql_stmt :=
2037             ' BEGIN'
2038           ||' MSC_A2A_XML_WF.LEGACY_RELEASE (:p_arg_org_instance);'
2039           ||' END;';
2040 
2041       EXECUTE IMMEDIATE lv_sql_stmt USING  arg_org_instance;
2042 
2043  END IF; -- v_curr_instance_type
2044 
2045     --- Update the released orders.
2046 
2047     IF  arg_loaded_jobs > 0   OR
2048         arg_resched_jobs > 0  OR
2049         arg_loaded_lot_jobs > 0   OR
2050         arg_resched_lot_jobs > 0  OR
2051         arg_loaded_scheds > 0 OR
2052         arg_loaded_reqs > 0   OR
2053         arg_resched_reqs > 0  OR
2054         arg_loaded_int_reqs > 0 OR
2055         arg_resched_int_reqs > 0
2056         OR arg_loaded_int_repair_orders >0
2057         OR arg_loaded_ext_repair_orders >0 THEN
2058 
2059 
2060         IF lv_wf= SYS_YES THEN
2061 
2062            UPDATE MSC_SUPPLIES
2063               SET implement_demand_class = NULL,
2064                   implement_date = NULL,
2065                   implement_quantity = NULL,
2066                   implement_firm = NULL,
2067                   implement_wip_class_code = NULL,
2068                   implement_job_name = NULL,
2069                   implement_status_code = NULL,
2070                   implement_location_id = NULL,
2071                   implement_source_org_id = NULL,
2072                   implement_supplier_id = NULL,
2073                   implement_supplier_site_id = NULL,
2074                   implement_project_id = NULL,
2075                   implement_task_id = NULL,
2076                   release_status = NULL,
2077                   load_type = NULL,
2078                   implement_as = NULL,
2079                   implement_unit_number = NULL,
2080                   implement_schedule_group_id = NULL,
2081                   implement_build_sequence = NULL,
2082                   implement_line_id = NULL,
2083                   implement_alternate_bom = NULL,
2084                   implement_dock_date = NULL,
2085                   implement_ship_date = NULL,
2086                   implement_employee_id = NULL,
2087                   implement_alternate_routing = NULL,
2088                   implemented_quantity = nvl(implemented_quantity, 0) + nvl(quantity_in_process,0),
2089                   quantity_in_process = 0,
2090                   implement_ship_method = NULL
2091             WHERE transaction_id= arg_transaction_id
2092               AND plan_id= arg_plan_id
2093 	      AND release_errors IS NULL
2094           and  batch_id = g_batch_id ;
2095 
2096         ELSE
2097 
2098            UPDATE MSC_SUPPLIES
2099               SET implement_demand_class = NULL,
2100                   implement_date = NULL,
2101                   implement_quantity = NULL,
2102                   implement_firm = NULL,
2103                   implement_wip_class_code = NULL,
2104                   implement_job_name = NULL,
2105                   implement_status_code = NULL,
2106                   implement_location_id = NULL,
2107                   implement_source_org_id = NULL,
2108                   implement_supplier_id = NULL,
2109                   implement_supplier_site_id = NULL,
2110                   implement_project_id = NULL,
2111                   implement_task_id = NULL,
2112                   release_status = NULL,
2113                   load_type = NULL,
2114                   implement_as = NULL,
2115                   implement_unit_number = NULL,
2116                   implement_schedule_group_id = NULL,
2117                   implement_build_sequence = NULL,
2118                   implement_line_id = NULL,
2119                   implement_alternate_bom = NULL,
2120                   implement_dock_date = NULL,
2121                   implement_ship_date = NULL,
2122                   implement_employee_id = NULL,
2123                   implement_alternate_routing = NULL,
2124                   implemented_quantity = nvl(implemented_quantity, 0) + nvl(quantity_in_process,0),
2125                   quantity_in_process = 0,
2126                   implement_ship_method = NULL
2127             WHERE batch_id = g_batch_id
2128               and sr_instance_id = arg_org_instance;
2129         END IF;  -- lv_wf
2130 
2131           DELETE msc_wip_job_schedule_interface
2132           WHERE sr_instance_id= arg_org_instance
2133           AND   NVL(GROUP_ID,-1) = NVL(arg_wip_group_id, -1);
2134 
2135           DELETE MSC_WIP_JOB_DTLS_INTERFACE
2136           WHERE sr_instance_id= arg_org_instance
2137           AND   NVL(GROUP_ID,-1) = NVL(arg_wip_group_id, -1);
2138 
2139 
2140           DELETE MSC_PO_REQUISITIONS_INTERFACE
2141           WHERE sr_instance_id= arg_org_instance
2142           AND   NVL(BATCH_ID, -1) = NVL(arg_po_batch_number, -1);
2143 
2144           DELETE MSC_PO_RESCHEDULE_INTERFACE
2145           WHERE sr_instance_id= arg_org_instance
2146             and batch_id = g_batch_id;
2147 
2148     END IF;
2149 
2150 EXCEPTION WHEN OTHERS THEN
2151 rollback; /* Rollback whatever was released */
2152 
2153           DELETE msc_wip_job_schedule_interface
2154           WHERE sr_instance_id= arg_org_instance
2155 	  AND   NVL(GROUP_ID,-1) = NVL(arg_wip_group_id, -1);
2156 
2157           DELETE MSC_WIP_JOB_DTLS_INTERFACE
2158           WHERE sr_instance_id= arg_org_instance
2159 	  AND   NVL(GROUP_ID,-1) = NVL(arg_wip_group_id, -1);
2160 
2161 
2162           DELETE MSC_PO_REQUISITIONS_INTERFACE
2163           WHERE sr_instance_id= arg_org_instance
2164           AND   NVL(BATCH_ID, -1) = NVL(arg_po_batch_number, -1);
2165 
2166 
2167           DELETE MSC_PO_RESCHEDULE_INTERFACE
2168           WHERE sr_instance_id= arg_org_instance
2169           and batch_id = g_batch_id;
2170           /*
2171           IF g_batch_id <> -1 THEN
2172             update msc_supplies set batch_id = NULL where batch_id = g_batch_id;
2173           END IF;
2174           */
2175  commit;
2176  raise;
2177 
2178 END LOAD_MSC_INTERFACE;
2179 
2180 /* DWK
2181 
2182 
2183  PROCEDURE   :  POPULATE_ISO_IN_SOURCE
2184 
2185  -------------------------------------------------------------*/
2186 PROCEDURE POPULATE_ISO_IN_SOURCE(
2187                                   l_dblink              IN  varchar2,
2188                                   l_arg_po_batch_number IN  number,
2189                                   l_arg_owning_instance IN  number,
2190                                   l_arg_po_group_by     IN  number,
2191                                   l_arg_plan_id         IN  number,
2192                                   l_arg_log_org_id      IN  number,
2193                                   l_arg_owning_org_id   IN  number,
2194                                   l_arg_org_instance    IN  number,
2195                                   l_arg_mode            IN  varchar2,
2196                                   l_arg_transaction_id  IN  number,
2197                                   arg_loaded_int_reqs   IN OUT NOCOPY number,
2198                                   arg_resched_int_reqs  IN OUT NOCOPY number,
2199                                   p_load_type           IN  number) IS
2200 
2201 TYPE CurTyp is ref cursor;
2202 
2203 c_row_count  CurTyp;
2204 
2205  CURSOR c_plan_type(p_plan_id number) IS
2206       select plan_type
2207         from msc_plans a
2208         where
2209         plan_id = p_plan_id;
2210 
2211 l_sql_stmt   VARCHAR2(20000):= NULL;
2212 l_sql_cur    varchar2(3000) := NULL;
2213 
2214 l_before_row_count  number := 0;
2215 l_after_row_count   number := 0;
2216 
2217 l_plan_type NUMBER :=0;
2218 BEGIN
2219 
2220    l_sql_cur := ' SELECT count(*) from MRP_ORG_TRANSFER_RELEASE'|| l_dblink ||
2221                 ' WHERE batch_id = :l_arg_po_batch_number' ;
2222 
2223 
2224    OPEN c_row_count for l_sql_cur USING l_arg_po_batch_number;
2225    FETCH c_row_count INTO l_before_row_count;
2226    CLOSE c_row_count;
2227 
2228 
2229 
2230    OPEN c_plan_type(l_arg_plan_id);
2231                     FETCH c_plan_type INTO l_plan_type;
2232    CLOSE c_plan_type;
2233 
2234 
2235 
2236    IF p_load_type = DRP_REQ_LOAD THEN
2237 
2238       l_sql_stmt := ' INSERT INTO MRP_ORG_TRANSFER_RELEASE' || l_dblink ||
2239                  '  ( batch_id, '||
2240                  '    Item_ID, ' ||
2241                  '    SRC_Organization_ID, ' ||
2242                  '    SR_Instance_ID, ' ||
2243                  '    To_Organization_ID, ' ||
2244                  '    To_Sr_Instance_ID, ' ||
2245                  '    Quantity, ' ||
2246                  '    Need_By_Date, ' ||
2247                  '    Ship_date, ' ||
2248                  '    Deliver_to_location_id, ' ||
2249                  '    Deliver_to_requestor_id, ' ||
2250                  '    Preparer_id, ' ||
2251                  '    Uom_code, ' ||
2252                  '    Charge_account_id, ' ||
2253                  '    To_Operating_unit, ' ||
2254                  '    SRC_Operating_unit, ' ||
2255                  '    Load_Type, ' ||
2256                  '    Sales_Order_Line_ID, ' ||
2257                  '    Sales_Order_Number, ' ||
2258                  '    End_item_number, ' ||
2259                  '    firm_demand_flag, ' ||
2260                  '    transaction_id, ' ||
2261                  '    plan_type ,' ||
2262                  '    ship_method )  ' ||
2263                  ' (SELECT /*+ FIRST_ROWS */ ' ||
2264                  '    :l_arg_po_batch_number,'||      -- batch id
2265                  '    msi.sr_inventory_item_id, ' ||  -- item id in the source
2266                  '    s.implement_source_org_id,' ||  -- source organization id
2267                  '    s.sr_instance_id,         ' ||  -- source instance id
2268                  '    s.organization_id,        ' ||  -- destination org id
2269                  '    :l_org_instance_id,       ' ||  -- destination instance id
2270                  '    s.implement_quantity,     ' ||  -- quantity
2271 	               '    s.implement_dock_date,    ' ||  -- arrival date in dst org
2272                  '    s.implement_ship_date,    ' ||  -- ship date
2273                  '    s.implement_location_id,  ' ||  -- destination location id
2274                  '    s.implement_employee_id,  ' ||  -- Deliver to requestor id
2275                  '    s.implement_employee_id,  ' ||  -- preparer id
2276                  '    msi.uom_code,             ' ||   -- uom
2277                  '    decode(mp.organization_type, ' || -- Account id
2278                  '           1, nvl(mpp.material_account, '||
2279                  '                 decode( msi1.inventory_asset_flag, ' ||
2280                  '                        ''Y'', mp.material_account, ' ||
2281                  '                 nvl(msi1.expense_account,mp.expense_account))),' ||
2282                  '           -1), '||
2283                   '    mp2.operating_unit,  ' ||        -- src_operating unit
2284                   '    mp.operating_unit, ' ||        -- to_operating_unit
2285                   '   :p_load_type, ' ||             -- load type
2286                   '    null, ' || -- Sales Order Line ID
2287                   '    s.transaction_id, ' || -- Sales Order Number
2288                   '    s.implement_unit_number, ' || --end item number
2289                   '    DECODE(s.implement_firm, 1, ''Y'',''N''), '||
2290                   '    s.transaction_id, ' || -- transaction_id of msc_supplies
2291                   l_plan_type ||',' ||
2292                   '    nvl(s.implement_ship_method,s.ship_method) ' ||
2293                   ' FROM    msc_projects mpp, '||
2294                   '         msc_trading_partners mp, ' ||
2295                   '         msc_trading_partners mp2,' ||
2296                   '         msc_system_items    msi, ' ||
2297                   '         msc_system_items    msi1,' ||
2298                   '         msc_supplies        s, '   ||
2299                   '         msc_plan_organizations_v orgs ' ||
2300                   ' WHERE   mpp.organization_id (+)= s.organization_id ' ||
2301                   '      AND mpp.project_id (+)= nvl(s.implement_project_id,-23453) ' ||
2302                   '      AND     mpp.plan_id (+)= s.plan_id ' ||
2303                   '      AND     mpp.sr_instance_id(+)= s.sr_instance_id ' ||
2304                   '      AND     mp.sr_tp_id  = s.source_organization_id ' ||
2305                   '      AND     mp.sr_instance_id = s.sr_instance_id ' ||
2306                   '      AND     mp.partner_type= 3 ' ||
2307                   '      AND     mp2.sr_tp_id  = msi.organization_id ' ||
2308                   '      AND     mp2.sr_instance_id = msi.sr_instance_id ' ||
2309                   '      AND     mp2.partner_type= 3 ' ||
2310                   '      AND     msi.inventory_item_id = s.inventory_item_id ' ||
2311                   '      AND     msi.plan_id = s.plan_id ' ||
2312                   '      AND     msi.organization_id = s.organization_id ' ||
2313                   '      AND     msi.sr_instance_id = s.sr_instance_id '   ||
2314                   '      AND     msi1.plan_id = -1 ' ||
2315                   '      AND     msi1.sr_instance_id = msi.sr_instance_id ' ||
2316                   '      AND     msi1.organization_id = msi.organization_id ' ||
2317                   '      AND     msi1.inventory_item_id = msi.inventory_item_id ' ||
2318                   '      AND     s.release_errors is NULL ' ||
2319                   '      AND     s.implement_quantity > 0 ' ||
2320                   '      AND     s.organization_id = orgs.planned_organization ' ||
2321                   '      AND     s.sr_instance_id = orgs.sr_instance_id ' ||
2322                   '      AND     s.plan_id = orgs.plan_id ' ||
2323                   '      AND     orgs.organization_id = :l_arg_owning_org_id ' ||
2324                   '      AND     orgs.owning_sr_instance = :l_arg_owning_instance ' ||
2325                   '      AND     orgs.plan_id = :l_arg_plan_id  ' ||
2326                   '      AND     orgs.sr_instance_id  = :l_arg_org_instance ' ||
2327                   '      AND    (s.releasable = ' || RELEASABLE || ' or s.releasable is null ) ';
2328 
2329                 IF  v_batch_id_populated = 2 THEN
2330                     l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id is NULL ';
2331                 ELSE
2332                     l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id = ' || g_batch_id || ' ' ;
2333                 END IF;
2334 
2335                 IF l_arg_log_org_id <> l_arg_owning_org_id THEN
2336                         l_sql_stmt :=  l_sql_stmt ||'      AND     orgs.planned_organization = ' || l_arg_log_org_id || ' ' ;
2337                 END IF;
2338 
2339                 IF l_arg_mode IS NULL THEN
2340                     l_sql_stmt :=  l_sql_stmt ||'      AND s.load_type  in ( '|| DRP_REQ_LOAD||','|| IRO_LOAD  ||')'||  ' ';
2341                 ELSIF l_arg_mode = 'WF' THEN
2342                     l_sql_stmt :=  l_sql_stmt ||'      AND s.transaction_id = ' || l_arg_transaction_id  || '  ' ;
2343                 END IF;
2344 
2345                 IF v_msc_released_only_by_user = 1 THEN
2346                     l_sql_stmt :=  l_sql_stmt || ' AND s.last_updated_by = ' || v_user_id || ' ' ;
2347                 END IF;
2348 
2349                 l_sql_stmt :=  l_sql_stmt || ' ) ';
2350 
2351 
2352 
2353                 IF l_arg_mode IS NULL OR l_arg_mode = 'WF' THEN
2354                    EXECUTE IMMEDIATE l_sql_stmt USING l_arg_po_batch_number,
2355                                                       l_arg_org_instance,
2356                                                       p_load_type,
2357                                                       l_arg_owning_org_id,
2358                                                       l_arg_owning_instance,
2359                                                       l_arg_plan_id,
2360                                                       l_arg_org_instance;
2361 
2362                 END IF;
2363 
2364 
2365 
2366     ELSE
2367 
2368       l_sql_stmt := ' INSERT INTO MRP_ORG_TRANSFER_RELEASE' || l_dblink ||
2369                  '  ( batch_id, '||
2370                  '    Item_ID, ' ||
2371                  '    SRC_Organization_ID, ' ||
2372                  '    SR_Instance_ID, ' ||
2373                  '    To_Organization_ID, ' ||
2374                  '    To_Sr_Instance_ID, ' ||
2375                  '    Quantity, ' ||
2376                  '    Need_By_Date, ' ||
2377                  '    Ship_date, ' ||
2378                  '    Deliver_to_location_id, ' ||
2379                  '    Deliver_to_requestor_id, ' ||
2380                  '    Preparer_id, ' ||
2381                  '    Uom_code, ' ||
2382                  '    Charge_account_id, ' ||
2383                  '    To_Operating_unit, ' ||
2384                  '    SRC_Operating_unit, ' ||
2385                  '    Load_Type, ' ||
2386                  '    Sales_Order_Line_ID, ' ||
2387                  '    Sales_Order_Number, ' ||
2388                  '    End_item_number, '||
2389                  '    firm_demand_flag, ' ||
2390                  '    transaction_id, ' ||
2391                  '    plan_type ,' ||
2392                  '    ship_method )  ' ||
2393                  ' (SELECT /*+ FIRST_ROWS */ ' ||
2394                  '    :l_arg_po_batch_number,'||      -- batch id
2395                  '    msi.sr_inventory_item_id, ' ||  -- item id in the source
2396                  '    s.implement_source_org_id,' ||  -- source organization id
2397                  '    s.sr_instance_id,         ' ||  -- source instance id
2398                  '    s.organization_id,        ' ||  -- destination org id
2399                  '    :l_org_instance_id,       ' ||  -- destination instance id
2400                  '    s.implement_quantity,     ' ||  -- quantity
2401 	         '    s.implement_dock_date,    ' ||  -- arrival date in dst org
2402                  '    s.implement_ship_date,    ' ||  -- ship date
2403                  '    s.implement_location_id,  ' ||  -- destination location id
2404                  '    s.implement_employee_id,  ' ||  -- Deliver to requestor id
2405                  '    s.implement_employee_id,  ' ||  -- preparer id
2406                  '    msi.uom_code,             ' ||   -- uom
2407                  '    decode(mp.organization_type, ' || -- Account id
2408                  '           1, nvl(mpp.material_account, '||
2409                  '                 decode( msi1.inventory_asset_flag, ' ||
2410                  '                        ''Y'', mp.material_account, ' ||
2411                  '                 nvl(msi1.expense_account,mp.expense_account))),' ||
2412                  '           -1), '||
2413                   '    mp.operating_unit,  ' ||        -- src_operating unit
2414                   '    mp2.operating_unit, ' ||        -- to_operating_unit
2415                   '   :p_load_type, ' ||             -- load type
2416                   '    d.sales_order_line_id,' ||    -- sales order line_id
2417                   '    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
2418                   '    s.implement_unit_number, ' ||  -- end item number
2419                   '    DECODE(s.implement_firm, 1, ''Y'', ''N''), '||
2420                   '    s.transaction_id, ' || -- transaction_id of msc_supplies
2421                   l_plan_type ||','||
2422                   '    nvl(s.implement_ship_method,s.ship_method) ' ||
2423                   ' FROM    msc_projects mpp, '||
2424                   '         msc_trading_partners mp, ' ||
2425                   '         msc_trading_partners mp2,' ||
2426                   '         msc_system_items    msi, ' ||
2427                   '         msc_system_items    msi1,' ||
2428                   '         msc_supplies        s,'    ||
2429                   '         msc_demands         d,'    ||
2430                   '         msc_plan_organizations_v orgs ' ||
2431                   ' WHERE   mpp.organization_id (+)= s.organization_id   ' ||
2432                   '       AND mpp.project_id (+)= nvl(s.implement_project_id,-23453) ' ||
2433                   '       AND     mpp.plan_id (+)= s.plan_id   ' ||
2434                   '       AND     mpp.sr_instance_id(+)= s.sr_instance_id  ' ||
2435                   '      AND     mp.sr_tp_id  = s.source_organization_id  ' ||
2436                   '      AND     mp.sr_instance_id = s.sr_instance_id ' ||
2437                   '      AND     mp.partner_type= 3 ' ||
2438                   '      AND     mp2.sr_tp_id  = msi.organization_id ' ||
2439                   '      AND     mp2.sr_instance_id = msi.sr_instance_id ' ||
2440                   '      AND     mp2.partner_type= 3 ' ||
2441                   '      AND     msi.inventory_item_id = s.inventory_item_id ' ||
2442                   '      AND     msi.plan_id = s.plan_id ' ||
2443                   '      AND     msi.organization_id = s.organization_id ' ||
2444                   '      AND     msi.sr_instance_id = s.sr_instance_id '   ||
2445                   '      AND     msi1.plan_id = -1 ' ||
2446                   '      AND     msi1.sr_instance_id = msi.sr_instance_id ' ||
2447                   '      AND     msi1.organization_id = msi.organization_id ' ||
2448                   '      AND     msi1.inventory_item_id = msi.inventory_item_id ' ||
2449                   '      AND     s.release_errors is NULL ' ||
2450                   '      AND     s.implement_quantity >= 0 ' ||
2451                   '      AND     s.organization_id = orgs.planned_organization ' ||
2452                   '      AND     s.sr_instance_id = orgs.sr_instance_id ' ||
2453                   '      AND     s.plan_id = orgs.plan_id ' ||
2454                   '      AND     orgs.organization_id = :l_arg_owning_org_id ' ||
2455                   '      AND     orgs.owning_sr_instance = :l_arg_owning_instance ' ||
2456                   '      AND     orgs.plan_id = :l_arg_plan_id  ' ||
2457                   '      AND     orgs.sr_instance_id  = :l_arg_org_instance ' ||
2458                   '      AND     d.plan_id = s.plan_id ' ||
2459                   '      AND     d.sr_instance_id  = s.sr_instance_id ' ||
2460                   '      AND     d.disposition_id  = s.transaction_id ' ||
2461                   '      AND     d.origination_type = 30 ' ||
2462                   '      and    (s.releasable = ' || RELEASABLE || ' or s.releasable is null ) ';
2463 
2464                 IF  v_batch_id_populated = 2 THEN
2465                     l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id is NULL ';
2466                 ELSE
2467                     l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id = ' || g_batch_id || ' ' ;
2468                 END IF;
2469 
2470                 IF l_arg_log_org_id <> l_arg_owning_org_id THEN
2471                         l_sql_stmt :=  l_sql_stmt ||'      AND     orgs.planned_organization = ' || l_arg_log_org_id || ' ' ;
2472                 END IF;
2473 
2474                 IF l_arg_mode IS NULL THEN
2475                     l_sql_stmt :=  l_sql_stmt ||'      AND s.load_type  ='|| p_load_type ||  ' and s.load_type IS NOT NULL ';
2476                 ELSIF l_arg_mode = 'WF' THEN
2477                     l_sql_stmt :=  l_sql_stmt ||'      AND s.transaction_id = ' || l_arg_transaction_id  || '  ' ;
2478                 END IF;
2479 
2480                 IF v_msc_released_only_by_user = 1 THEN
2481                     l_sql_stmt :=  l_sql_stmt || ' AND s.last_updated_by = ' || v_user_id || ' ' ;
2482                 END IF;
2483 
2484                 l_sql_stmt :=  l_sql_stmt || ' ) ';
2485 
2486                IF l_arg_mode IS NULL OR l_arg_mode = 'WF' THEN
2487 
2488                    EXECUTE IMMEDIATE l_sql_stmt USING l_arg_po_batch_number,
2489                                                       l_arg_org_instance,
2490                                                       p_load_type,
2491                                                       l_arg_owning_org_id,
2492                                                       l_arg_owning_instance,
2493                                                       l_arg_plan_id,
2494                                                       l_arg_org_instance;
2495 
2496                 END IF;
2497 
2498    END IF;
2499 
2500    update mrp_org_transfer_release motr
2501                  set earliest_ship_date = (select max(ms.new_schedule_date)
2502                                            from    msc_supplies ms, msc_single_lvl_peg mslp
2503                                            where   ms.plan_id=l_arg_plan_id
2504                                            and     ms.transaction_id = mslp.child_id
2505                                            and     ms.plan_id= mslp.plan_id
2506                                            and     mslp.parent_id = motr.transaction_id
2507                                            and     mslp.pegging_type=1)
2508                   where motr.batch_id=l_arg_po_batch_number;
2509 
2510 
2511    OPEN c_row_count for l_sql_cur USING l_arg_po_batch_number;
2512    FETCH c_row_count INTO l_after_row_count;
2513    CLOSE c_row_count;
2514 
2515    IF p_load_type = DRP_REQ_LOAD THEN
2516 
2517       arg_loaded_int_reqs := l_after_row_count - l_before_row_count;
2518       arg_resched_int_reqs := null;
2519    ELSE
2520       arg_loaded_int_reqs := null;
2521       arg_resched_int_reqs := l_after_row_count - l_before_row_count;
2522 
2523    END IF;
2524 
2525   IF l_arg_mode = 'WF' THEN
2526         update msc_supplies
2527            set releasable = RELEASE_ATTEMPTED
2528                ,batch_id   = nvl(batch_id,g_batch_id)
2529          where plan_id = l_arg_plan_id
2530            and transaction_id= l_arg_transaction_id;
2531    ELSE
2532        execute immediate '  update msc_supplies
2533                                set releasable = ' || RELEASE_ATTEMPTED || '
2534                                   ,batch_id   = nvl(batch_id,' ||g_batch_id ||')
2535                              where plan_id = ' || l_arg_plan_id || '
2536                                and transaction_id in
2537                                     (select transaction_id from MRP_ORG_TRANSFER_RELEASE' || l_dblink || '
2538                                       where batch_id = ' || l_arg_po_batch_number  || ')';
2539    END IF;
2540 
2541 END POPULATE_ISO_IN_SOURCE;
2542 
2543 
2544 
2545 /*------------------------------------------------------------
2546 
2547  PROCEDURE   :  POPULATE_ISO_IN_SOURCE_2
2548 
2549  -------------------------------------------------------------*/
2550 PROCEDURE POPULATE_ISO_IN_SOURCE_2(
2551                                   l_dblink              IN  varchar2,
2552                                   l_arg_po_batch_number IN  number,
2553                                   l_arg_owning_instance IN  number,
2554                                   l_arg_po_group_by     IN  number,
2555                                   l_arg_plan_id         IN  number,
2556                                   l_arg_log_org_id      IN  number,
2557                                   l_arg_owning_org_id   IN  number,
2558                                   l_arg_org_instance    IN  number,
2559                                   l_arg_mode            IN  varchar2,
2560                                   l_arg_transaction_id  IN  number,
2561                                   arg_loaded_int_reqs   IN OUT NOCOPY number,
2562                                   arg_resched_int_reqs  IN OUT NOCOPY number,
2563                                   p_load_type           IN  number) IS
2564 
2565 TYPE CurTyp is ref cursor;
2566 
2567 c_row_count  CurTyp;
2568 
2569  CURSOR c_plan_type(p_plan_id number) IS
2570       select plan_type
2571       from msc_plans a
2572       where
2573       plan_id = p_plan_id;
2574 
2575 l_sql_stmt   VARCHAR2(20000):= NULL;
2576 l_sql_cur    varchar2(3000) := NULL;
2577 
2578 --l_before_row_count  number := 0;
2579 --l_after_row_count   number := 0;
2580 
2581 l_plan_type NUMBER :=0;
2582 
2583 BEGIN
2584 
2585    OPEN c_plan_type(l_arg_plan_id);
2586    FETCH c_plan_type INTO l_plan_type;
2587    CLOSE c_plan_type;
2588 
2589    IF p_load_type = DRP_REQ_LOAD THEN
2590 
2591       l_sql_stmt := ' INSERT INTO MRP_ORG_TRANSFER_RELEASE' || l_dblink ||
2592                  '  ( batch_id, '||
2593                  '    Item_ID, ' ||
2594                  '    SRC_Organization_ID, ' ||
2595                  '    SR_Instance_ID, ' ||
2596                  '    To_Organization_ID, ' ||
2597                  '    To_Sr_Instance_ID, ' ||
2598                  '    Quantity, ' ||
2599                  '    Need_By_Date, ' ||
2600                  '    Ship_date, ' ||
2601                  '    Deliver_to_location_id, ' ||
2602                  '    Deliver_to_requestor_id, ' ||
2603                  '    Preparer_id, ' ||
2604                  '    Uom_code, ' ||
2605                  '    Charge_account_id, ' ||
2606                  '    To_Operating_unit, ' ||
2607                  '    SRC_Operating_unit, ' ||
2608                  '    Load_Type, ' ||
2609                  '    Sales_Order_Line_ID, ' ||
2610                  '    Sales_Order_Number, ' ||
2611                  '    End_item_number, ' ||
2612                  '    firm_demand_flag, ' ||
2613                  '    transaction_id, ' ||
2614                  '    plan_type ,' ||
2615                  '    ship_method )  ' ||
2616                  '( SELECT /*+ FIRST_ROWS */  '||
2617                  '    :l_arg_po_batch_number,'||      -- batch id
2618                  '    msi.sr_inventory_item_id, ' ||  -- item id in the source
2619                  '    s.implement_source_org_id,' ||  -- source organization id
2620                  '    s.sr_instance_id,         ' ||  -- source instance id
2621                  '    s.organization_id,        ' ||  -- destination org id
2622                  '    :l_org_instance_id,       ' ||  -- destination instance id
2623                  '    s.implement_quantity,     ' ||  -- quantity
2624 	               '    s.implement_dock_date,    ' ||  -- arrival date in dst org
2625                  '    nvl(s.implement_ship_date,nvl(s.implement_dock_date,s.implement_date)),    ' ||  -- ship date
2626                  '    s.implement_location_id,  ' ||  -- destination location id
2627                  '    s.implement_employee_id,  ' ||  -- Deliver to requestor id
2628                  '    s.implement_employee_id,  ' ||  -- preparer id
2629                  '    msi.uom_code,             ' ||   -- uom
2630                  '    decode(mp.organization_type, ' || -- Account id
2631                  '           1, nvl(mpp.material_account, '||
2632                  '                 decode( msi1.inventory_asset_flag, ' ||
2633                  '                        ''Y'', mp.material_account, ' ||
2634                  '                 nvl(msi1.expense_account,mp.expense_account))),' ||
2635                  '           -1), '||
2636                   '    mp.operating_unit,  ' ||        -- src_operating unit
2637                   '    mp2.operating_unit, ' ||        -- to_operating_unit
2638                   '   :p_load_type, ' ||             -- load type
2639                   '    null, ' || -- Sales Order Line ID
2640                   '    s.transaction_id, ' || -- Sales Order Number
2641                   '    s.implement_unit_number, ' || --end item number
2642                   '    DECODE(s.implement_firm, 1, ''Y'',''N''), '||
2643                   '    s.transaction_id, ' || -- transaction_id of msc_supplies
2644                    l_plan_type ||',' ||
2645                   '    nvl(s.implement_ship_method,s.ship_method) ' ||
2646                   ' FROM    msc_projects mpp, ' ||
2647                       ' msc_trading_partners mp, ' ||
2648                       ' msc_trading_partners mp2, ' ||
2649                        'msc_system_items    msi, '||
2650                        'msc_system_items    msi1,'||
2651                        'msc_part_supplies        s, '||
2652                        'msc_plan_organizations_v orgs '||
2653                     'WHERE   mpp.organization_id (+)= s.organization_id '||
2654                         'AND mpp.project_id (+)= nvl(s.implement_project_id,-23453) ' ||
2655                         'AND     mpp.plan_id (+)= s.plan_id '||
2656                         'AND     mpp.sr_instance_id(+)= s.sr_instance_id '||
2657                         'AND     mp.sr_tp_id  = s.source_organization_id '||
2658                         'AND     mp.sr_instance_id = s.sr_instance_id '||
2659                         'AND     mp.partner_type= 3 '||
2660                         'AND     mp2.sr_tp_id  = msi.organization_id '||
2661                         'AND     mp2.sr_instance_id = msi.sr_instance_id '||
2662                         'AND     mp2.partner_type= 3 '||
2663                         'AND     msi.inventory_item_id = s.inventory_item_id '||
2664                         'AND     msi.plan_id = s.plan_id '||
2665                         'AND     msi.organization_id = s.organization_id '||
2666                         'AND     msi.sr_instance_id = s.sr_instance_id '||
2667                         'AND     msi1.plan_id = -1 '||
2668                         'AND     msi1.sr_instance_id = msi.sr_instance_id '||
2669                         'AND     msi1.organization_id = msi.organization_id '||
2670                         'AND     msi1.inventory_item_id = msi.inventory_item_id '||
2671                         --'AND     s.release_errors is NULL '||
2672                         'AND    (s.releasable = ' || RELEASABLE || 'or s.releasable is null ) '||
2673                         --'AND     s.implement_quantity > 0 '||
2674                         'AND     s.organization_id = orgs.planned_organization '||
2675                         'AND     s.sr_instance_id = orgs.sr_instance_id '||
2676                         'AND     s.plan_id = orgs.plan_id '||
2677                         'AND     orgs.organization_id = :l_arg_owning_org_id '||
2678                         'AND     orgs.owning_sr_instance = :l_arg_owning_instance '||
2679                         'AND     orgs.plan_id = :l_arg_plan_id  '||
2680                         'AND     orgs.sr_instance_id  = :l_arg_org_instance ';
2681 
2682 
2683                 IF  v_batch_id_populated = 2 THEN
2684                     l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id is NULL ';
2685                 ELSE
2686                     l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id = ' || g_batch_id || ' ' ;
2687                 END IF;
2688 
2689                 IF l_arg_log_org_id <> l_arg_owning_org_id THEN
2690                         l_sql_stmt :=  l_sql_stmt ||'      AND     orgs.planned_organization = ' || l_arg_log_org_id || ' ' ;
2691                 END IF;
2692 
2693                 IF l_arg_mode IS NULL THEN
2694                     l_sql_stmt :=  l_sql_stmt ||'      AND s.load_type  in ( '|| DRP_REQ_LOAD||','|| IRO_LOAD  ||')'||  ' ';
2695                 ELSIF l_arg_mode = 'WF' THEN
2696                     l_sql_stmt :=  l_sql_stmt ||'      AND s.transaction_id = ' || l_arg_transaction_id  || '  ' ;
2697                 END IF;
2698 
2699                /* IF v_msc_released_only_by_user = 1 THEN
2700                     l_sql_stmt :=  l_sql_stmt || ' AND s.last_updated_by = ' || v_user_id || ' ' ;
2701                 END IF;*/
2702 
2703                 l_sql_stmt :=  l_sql_stmt || ' ) ';
2704 
2705 
2706 
2707                 IF l_arg_mode IS NULL OR l_arg_mode = 'WF' THEN
2708                    EXECUTE IMMEDIATE l_sql_stmt USING l_arg_po_batch_number,
2709                                                       l_arg_org_instance,
2710                                                       p_load_type,
2711                                                       l_arg_owning_org_id,
2712                                                       l_arg_owning_instance,
2713                                                       l_arg_plan_id,
2714                                                       l_arg_org_instance;
2715                   arg_loaded_int_reqs := SQL%ROWCOUNT;
2716 
2717                 END IF;
2718 
2719    END IF;
2720 
2721   /* update mrp_org_transfer_release motr
2722                  set earliest_ship_date = (select max(ms.new_schedule_date)
2723                                            from    msc_part_supplies ms, msc_single_lvl_peg mslp
2724                                            where   ms.plan_id=l_arg_plan_id
2725                                            and     ms.transaction_id = mslp.child_id
2726                                            and     ms.plan_id= mslp.plan_id
2727                                            and     mslp.parent_id = motr.transaction_id
2728                                            and     mslp.pegging_type=1)
2729                   where motr.batch_id=l_arg_po_batch_number;*/
2730 
2731    arg_resched_int_reqs := null;
2732 
2733 
2734   IF l_arg_mode = 'WF' THEN
2735         update msc_supplies
2736          set releasable = RELEASE_ATTEMPTED,
2737                batch_id   = nvl(batch_id,g_batch_id)
2738          where plan_id = l_arg_plan_id
2739          and transaction_id= l_arg_transaction_id;
2740 
2741     /*     update msc_part_supplies
2742          set releasable = RELEASE_ATTEMPTED,
2743                batch_id   = nvl(batch_id,g_batch_id)
2744          where plan_id = l_arg_plan_id
2745          and transaction_id= l_arg_transaction_id;       */
2746    ELSE
2747        execute immediate '  update msc_supplies
2748                                set releasable = ' || RELEASE_ATTEMPTED || ',
2749                                    batch_id   = nvl(batch_id,' ||g_batch_id ||')
2750                              where plan_id = ' || l_arg_plan_id || '
2751                              and transaction_id in (select orig_transaction_id from msc_part_supplies where transaction_id in
2752                                     (select transaction_id from MRP_ORG_TRANSFER_RELEASE' || l_dblink || '
2753                                       where batch_id = ' || l_arg_po_batch_number  || '))';
2754 
2755         execute immediate '  update msc_part_supplies
2756                                set releasable = ' || RELEASE_ATTEMPTED || ',
2757                                    batch_id   = nvl(batch_id,' ||g_batch_id ||')
2758                              where plan_id = ' || l_arg_plan_id || '
2759                              and transaction_id in
2760                                     (select transaction_id from MRP_ORG_TRANSFER_RELEASE' || l_dblink || '
2761                                       where batch_id = ' || l_arg_po_batch_number  || ')';
2762    END IF;
2763 
2764 END POPULATE_ISO_IN_SOURCE_2;
2765 
2766 
2767 PROCEDURE RELEASE_IRO(
2768     p_dblink IN VARCHAR2,
2769     p_arg_iro_batch_number IN NUMBER,
2770     p_arg_owning_instance IN NUMBER,
2771     p_arg_po_group_by IN NUMBER,
2772     p_arg_plan_id IN NUMBER,
2773     p_arg_log_org_id IN NUMBER,
2774     p_arg_owning_org_id IN NUMBER,
2775     p_arg_org_instance IN NUMBER,
2776     p_arg_mode IN VARCHAR2,
2777     p_arg_transaction_id IN NUMBER,
2778     p_arg_loaded_int_repair_orders IN OUT nocopy NUMBER,
2779     p_load_type IN NUMBER)
2780 IS
2781 --l_sql_cur VARCHAR2(3000) := NULL;
2782 l_sql_qry VARCHAR2(3000) := NULL;
2783 l_sql_qry2 varchar2(3000) := NULL;
2784 p_new_iso_num NUMBER;
2785 p_where_clause varchar2(3000):= NULL;
2786 p_total_rows  NUMBER;
2787 p_succ_rows  NUMBER;
2788 p_error_rows  NUMBER;
2789 p_reschedule_iso_num NUMBER;
2790 
2791 /* CURSOR c1(v_arg_mode VARCHAR2,
2792     v_arg_org_instance NUMBER,
2793     v_load_type NUMBER,
2794     v_arg_transaction_id NUMBER,
2795     v_arg_plan_id NUMBER) IS
2796 */
2797 
2798     type numlisttyp IS TABLE OF NUMBER;
2799     type datelisttyp IS TABLE OF DATE;
2800     type varlisttyp IS TABLE OF VARCHAR2(30);
2801 
2802     lv_transaction_id_lst numlisttyp;
2803     lv_quantity_lst numlisttyp;
2804     lv_in_req_quantity_lst numlisttyp;
2805     lv_load_type_lst  numlisttyp;
2806     lv_out_req_quantity_lst numlisttyp;
2807     lv_in_req_transaction_id_lst numlisttyp;
2808     lv_out_req_transaction_id_lst numlisttyp;
2809     lv_inventory_item_id_lst numlisttyp;
2810     lv_uom_code_lst varlisttyp;
2811     lv_organization_id_lst numlisttyp;
2812     lv_promise_date_lst datelisttyp;
2813     lv_implement_location_id  numlisttyp;
2814     lv_implement_source_org_id numlisttyp;
2815 
2816 
2817 
2818     --lv_implement_quantity number;
2819 BEGIN
2820   /* 1. get the row count for the new batch  if in table MRP_IRO_RELEASE */
2821   /*
2822   l_sql_cur := 'SELECT count(*) from MRP_IRO_RELEASE' || p_dblink ||' WHERE batch_id = :p_arg_iro_batch_number';
2823   OPEN c_row_count FOR l_sql_cur USING p_arg_iro_batch_number;
2824   FETCH c_row_count
2825   INTO l_before_row_count;
2826   CLOSE c_row_count;
2827   */
2828    p_arg_loaded_int_repair_orders:=0;
2829 
2830    IF p_arg_mode IS NULL THEN
2831      p_where_clause:= ' AND s.load_type = '||p_load_type ;
2832 
2833    ELSIF p_arg_mode = 'WF' THEN
2834        p_where_clause:= ' AND s.transaction_id = '||p_arg_transaction_id;
2835 
2836   ELSE
2837       -- log_msg('no IROs to release....');
2838        return ;
2839     END IF;
2840 
2841   /* 3. bulk collect all the records returned by record  by above cursor into table types record */
2842 l_sql_qry2:=
2843 'with mfg0 AS  ( select transaction_id,plan_id,organization_id ,sr_instance_id ,demand_id ,
2844                 sum(allocated_quantity) allocated_quantity
2845                 from msc_full_pegging
2846                 where sr_instance_id = '||p_arg_org_instance||
2847                 '  and plan_id = '|| p_arg_plan_id ||
2848                 ' group by transaction_id,plan_id,organization_id ,sr_instance_id ,demand_id
2849       )
2850   SELECT /*+ LEADING(s d) */ s1.transaction_id,
2851     s1.new_order_quantity quantity,
2852     s2.new_order_quantity in_req_quantity,
2853     s.new_order_quantity  out_req_quantity,
2854     Decode(s2.order_type,51,s2.transaction_id, null)  in_req_transaction_id,
2855     s.transaction_id out_req_transaction_id,
2856     --L_arg_io_batch_number Batch_id,
2857     msi1.sr_inventory_item_id inventory_item_id,
2858     msi1.uom_code,
2859     s1.organization_id,
2860     s1.new_schedule_date promise_date,
2861     s.implement_location_id,
2862     s.Load_Type,
2863     s.implement_source_org_id
2864  FROM msc_supplies s,
2865     msc_supplies s1,
2866     msc_supplies s2,
2867     msc_system_items msi1,
2868     msc_demands d,
2869     msc_demands d1,
2870     msc_demands d2,
2871     MFG0 mfg,
2872     mfg0 mfg1
2873   WHERE s.release_errors IS NULL
2874    AND s.sr_instance_id = '||p_arg_org_instance||
2875  ' AND s.plan_id = '|| p_arg_plan_id ||
2876  ' AND s.transaction_id = d.disposition_id
2877    AND s.sr_instance_id = d.sr_instance_id
2878    AND s.plan_id = d.plan_id
2879    AND d.origination_type = 1 --- Planned order demand
2880    AND d.demand_id = mfg.demand_id
2881    AND d.plan_id = mfg.plan_id
2882    AND d.sr_instance_id = mfg.sr_instance_id
2883    AND d.organization_id = mfg.organization_id
2884    AND mfg.transaction_id = s1.transaction_id
2885    AND mfg.plan_id = s1.plan_id
2886    AND s1.sr_instance_id = msi1.sr_instance_id
2887    AND s1.plan_id = msi1.plan_id
2888    AND s1.inventory_item_id = msi1.inventory_item_id
2889    AND s1.organization_id = msi1.organization_id
2890    AND s1.transaction_id = d1.disposition_id
2891    AND s1.sr_instance_id = d1.sr_instance_id
2892    AND s1.plan_id = d1.plan_id
2893    AND d1.origination_type = 78 -- planned defective part demand
2894    AND d1.demand_id = mfg1.demand_id(+)
2895    AND d1.plan_id = mfg1.plan_id(+)
2896    AND d1.sr_instance_id = mfg1.sr_instance_id(+)
2897    AND D1.organization_id = mfg1.organization_id (+)
2898    AND mfg1.transaction_id = s2.transaction_id(+)
2899    AND mfg1.plan_id = s2.plan_id(+)
2900    AND s2.transaction_id = d2.disposition_id(+)
2901    AND s2.sr_instance_id = d2.sr_instance_id(+)
2902    AND s2.plan_id = d2.plan_id(+)
2903    AND d2.origination_type(+) = 1
2904    and (s.releasable = ' || RELEASABLE  || ' or s.releasable is null )';
2905 
2906     IF  v_batch_id_populated = 2 THEN
2907 
2908         l_sql_qry2 :=  l_sql_qry2 || ' AND  s.batch_id is NULL ';
2909     ELSE
2910 
2911         l_sql_qry2 :=  l_sql_qry2 || ' AND  s.batch_id = ' || g_batch_id || ' ' ;
2912     END IF;
2913 
2914     l_sql_qry2:= l_sql_qry2  ||p_where_clause;
2915 
2916 
2917 
2918  IF ((p_arg_mode IS NULL) OR (p_arg_mode = 'WF')) THEN
2919 
2920   -- bulk collect l_sql_qry2 into lv_IRO_list;
2921   EXECUTE IMMEDIATE l_sql_qry2
2922           bulk collect INTO
2923     lv_transaction_id_lst,
2924     lv_quantity_lst,
2925     lv_in_req_quantity_lst,
2926     lv_out_req_quantity_lst,
2927     lv_in_req_transaction_id_lst,
2928     lv_out_req_transaction_id_lst,
2929     lv_inventory_item_id_lst,
2930     lv_uom_code_lst,
2931     lv_organization_id_lst,
2932     lv_promise_date_lst,
2933     lv_implement_location_id,
2934     lv_load_type_lst,
2935     lv_implement_source_org_id
2936  ;
2937 
2938  END IF;
2939 
2940    p_where_clause :=null ;
2941 
2942 
2943   /* 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 */
2944      for i IN 1 .. lv_in_req_transaction_id_lst.count LOOP
2945 
2946           /*4.2 calling the api to update implement_* column */
2947         IF (lv_in_req_transaction_id_lst(i) is not NULL) THEN
2948 
2949           p_where_clause := ' transaction_id = ' || lv_in_req_transaction_id_lst(i);
2950 
2951              MSC_SELECT_ALL_FOR_RELEASE_PUB.Update_Implement_Attrib(p_where_clause ,
2952                                           NULL,
2953                                           NULL,
2954                                           NULL,
2955                                           NULL,
2956                                           NULL,
2957                                           p_total_rows ,
2958                                           p_succ_rows ,
2959                                           p_error_rows
2960                                           );
2961              /* debuf msg
2962                 select --implement_quantity,
2963                 new_order_quantity, release_errors
2964                 into lv_implement_quantity, lv_release_errors from msc_supplies
2965                 where plan_id =  p_arg_plan_id
2966                 and   transaction_id =  lv_in_req_transaction_id_lst(i);
2967               */
2968 
2969               UPDATE msc_supplies
2970               SET load_type = 32,
2971                  release_status = 2,
2972                  implement_quantity = new_order_quantity,  --to be removed if above api works fine
2973                  release_errors = NULL,
2974                 batch_id=decode(v_batch_id_populated,2,batch_id,g_batch_id)
2975               WHERE transaction_id = lv_in_req_transaction_id_lst(i)
2976                 and plan_id =p_arg_plan_id ;
2977 
2978          END IF;
2979     END LOOP;
2980 
2981   /* 5. bulk insert the data in the table type to mrp_iro_release table in the source */
2982   FOR i IN 1 .. lv_transaction_id_lst.COUNT
2983   LOOP
2984 
2985     l_sql_qry := 'insert into mrp_iro_release' || p_dblink || '(Transaction_id,
2986                           Quantity,
2987                           in_req_quantity,
2988                           out_req_quantity,
2989                           In_req_transaction_id,
2990                           Out_req_transaction_id,
2991                           Batch_id,
2992                           Inventory_item_id,
2993                           Uom_code,
2994                           Organization_id,
2995                           Promise_date,
2996                           Load_Type,
2997                           deliver_to_location_id,
2998                           src_organization_id)
2999                   values (:lv_Transaction_id,
3000                           :lv_Quantity,
3001                           :lv_in_req_quantity,
3002                           :lv_out_req_quantity,
3003                           :lv_In_req_transaction_id,
3004                           :lv_Out_req_transaction_id,
3005                           :p_arg_iro_batch_number,
3006                           :lv_Inventory_item,
3007                           :lv_uom_code ,
3008                           :lv_organization_id,
3009                           :lv_Promise_date,
3010                           :Load_Type,
3011                           :implement_location_id,
3012                           :implement_source_org_id)';
3013 
3014     EXECUTE IMMEDIATE l_sql_qry USING lv_transaction_id_lst(i),
3015       lv_quantity_lst(i),
3016       lv_in_req_quantity_lst(i),
3017       lv_out_req_quantity_lst(i),
3018       lv_in_req_transaction_id_lst(i),
3019       lv_out_req_transaction_id_lst(i),
3020       p_arg_iro_batch_number,
3021       lv_inventory_item_id_lst(i),
3022       lv_uom_code_lst(i),
3023       lv_organization_id_lst(i),
3024       lv_promise_date_lst(i),
3025       lv_load_type_lst(i),
3026       lv_implement_location_id(i),
3027       lv_implement_source_org_id(i);
3028 
3029     p_arg_loaded_int_repair_orders := p_arg_loaded_int_repair_orders + 1 ;
3030 
3031         update msc_supplies
3032            set releasable = RELEASE_ATTEMPTED
3033                ,batch_id   = nvl(batch_id,g_batch_id)
3034          where plan_id =p_arg_plan_id
3035            and transaction_id= lv_transaction_id_lst(i);
3036 
3037   END LOOP;
3038 
3039   /*
3040   OPEN c_row_count FOR l_sql_cur USING p_arg_iro_batch_number;
3041   FETCH c_row_count
3042   INTO l_after_row_count;
3043   CLOSE c_row_count;
3044   */
3045 
3046   --p_arg_loaded_int_repair_orders:= l_after_row_count -l_before_row_count;
3047   /* 6. call populate_iso_in_source to populate mrp_org_transfer_release_table in the source */
3048 
3049   IF p_arg_loaded_int_repair_orders > 0 THEN
3050 
3051       populate_iso_in_source(l_dblink => p_dblink,
3052        l_arg_po_batch_number => p_arg_iro_batch_number,
3053        l_arg_owning_instance => p_arg_owning_instance,
3054        l_arg_po_group_by => p_arg_po_group_by,
3055        l_arg_plan_id => p_arg_plan_id,
3056        l_arg_log_org_id => p_arg_log_org_id,
3057        l_arg_owning_org_id => p_arg_owning_org_id,
3058        l_arg_org_instance => p_arg_org_instance,
3059        l_arg_mode => p_arg_mode,
3060        l_arg_transaction_id => p_arg_transaction_id,
3061        arg_loaded_int_reqs => p_new_iso_num,
3062        arg_resched_int_reqs => p_reschedule_iso_num,
3063        p_load_type => drp_req_load);
3064   END IF;
3065 
3066   l_sql_qry := NULL;
3067 
3068   /* 7. update the load type to null which had been updated to 32   previously */
3069    for i IN 1 .. lv_in_req_transaction_id_lst.count LOOP
3070 
3071     If (lv_in_req_transaction_id_lst(i) is NULL) then
3072 
3073         l_sql_qry :=     'UPDATE msc_supplies '
3074                           ||'SET load_type = NULL
3075                           WHERE plan_id= :p_arg_plan_id AND transaction_id = '||lv_out_req_transaction_id_lst(i) ;
3076 
3077      Else
3078 
3079         l_sql_qry :=     'UPDATE msc_supplies '
3080                           ||'SET load_type = NULL WHERE plan_id= :p_arg_plan_id AND transaction_id in ('
3081                           || lv_in_req_transaction_id_lst(i)||','||lv_out_req_transaction_id_lst(i)||')' ;
3082     ENd if;
3083 
3084         execute immediate l_sql_qry
3085         USING p_arg_plan_id;
3086 
3087 
3088 
3089    END LOOP;
3090   /*
3091    forall i IN 1 .. lv_out_req_transaction_id_lst.count
3092         UPDATE msc_supplies
3093         SET load_type = NULL
3094         WHERE transaction_id = lv_out_req_transaction_id_lst(i)
3095         and plan_id =p_arg_plan_id ;
3096    */
3097 
3098   Exception
3099   When Others Then
3100 
3101    raise;
3102 END release_iro;
3103 
3104 
3105 PROCEDURE RELEASE_IRO_2(
3106     p_dblink IN VARCHAR2,
3107     p_arg_iro_batch_number IN NUMBER,
3108     p_arg_owning_instance IN NUMBER,
3109     p_arg_po_group_by IN NUMBER,
3110     p_arg_plan_id IN NUMBER,
3111     p_arg_log_org_id IN NUMBER,
3112     p_arg_owning_org_id IN NUMBER,
3113     p_arg_org_instance IN NUMBER,
3114     p_arg_mode IN VARCHAR2,
3115     p_arg_transaction_id IN NUMBER,
3116     p_arg_loaded_int_repair_orders IN OUT nocopy NUMBER,
3117     p_load_type IN NUMBER)
3118 IS
3119 --l_sql_cur VARCHAR2(3000) := NULL;
3120 l_sql_qry VARCHAR2(3000) := NULL;
3121 l_sql_qry2 varchar2(3000) := NULL;
3122 p_new_iso_num NUMBER;
3123 p_where_clause varchar2(3000):= NULL;
3124 p_total_rows  NUMBER;
3125 p_succ_rows  NUMBER;
3126 p_error_rows  NUMBER;
3127 p_reschedule_iso_num NUMBER;
3128 
3129 /* CURSOR c1(v_arg_mode VARCHAR2,
3130     v_arg_org_instance NUMBER,
3131     v_load_type NUMBER,
3132     v_arg_transaction_id NUMBER,
3133     v_arg_plan_id NUMBER) IS
3134 */
3135 
3136     type numlisttyp IS TABLE OF NUMBER;
3137     type datelisttyp IS TABLE OF DATE;
3138     type varlisttyp IS TABLE OF VARCHAR2(30);
3139 
3140     lv_transaction_id_lst numlisttyp;
3141     lv_quantity_lst numlisttyp;
3142     lv_in_req_quantity_lst numlisttyp;
3143     lv_load_type_lst  numlisttyp;
3144     lv_out_req_quantity_lst numlisttyp;
3145     lv_in_req_transaction_id_lst numlisttyp;
3146     lv_out_req_transaction_id_lst numlisttyp;
3147     lv_inventory_item_id_lst numlisttyp;
3148     lv_uom_code_lst varlisttyp;
3149     lv_organization_id_lst numlisttyp;
3150     lv_promise_date_lst datelisttyp;
3151     lv_implement_location_id  numlisttyp;
3152     lv_implement_source_org_id numlisttyp;
3153     lv_orig_transaction_id_lst numlisttyp;
3154 
3155     --lv_implement_quantity number;
3156 BEGIN
3157   /* 1. get the row count for the new batch  if in table MRP_IRO_RELEASE */
3158 
3159    p_arg_loaded_int_repair_orders:=0;
3160 
3161    IF p_arg_mode IS NULL THEN
3162      p_where_clause:= ' AND k.load_type = '||p_load_type ;
3163 
3164    ELSIF p_arg_mode = 'WF' THEN
3165        p_where_clause:= ' AND k.transaction_id = '||p_arg_transaction_id;
3166 
3167   ELSE
3168       -- log_msg('no IROs to release....');
3169        return ;
3170     END IF;
3171 
3172   /* 3. bulk collect all the records returned by record  by above cursor into table types record */
3173 l_sql_qry2:=
3174 
3175 'with mfg0 AS
3176     ( select transaction_id,plan_id,organization_id ,sr_instance_id ,demand_id ,
3177                 sum(allocated_quantity) allocated_quantity
3178              from msc_part_pegging
3179              where sr_instance_id = '||p_arg_org_instance|| '
3180              and plan_id = '|| p_arg_plan_id ||'
3181              group by transaction_id,plan_id,organization_id ,sr_instance_id ,demand_id
3182       )
3183  SELECT /*+ LEADING(s d) */
3184     s1.transaction_id,
3185     s1.new_order_quantity quantity,
3186     s2.new_order_quantity in_req_quantity,
3187     s.new_order_quantity  out_req_quantity,
3188     Decode(s2.order_type,51,s2.transaction_id, null)  in_req_transaction_id,
3189     s.transaction_id out_req_transaction_id,
3190     --L_arg_io_batch_number Batch_id,
3191     msi1.sr_inventory_item_id inventory_item_id,
3192     msi1.uom_code,
3193     s1.organization_id,
3194     s1.new_schedule_date promise_date,
3195     s.implement_location_id,
3196     k.Load_Type,
3197     s.implement_source_org_id,
3198     k.transaction_id
3199 FROM
3200 msc_supplies k,
3201 msc_part_supplies s,
3202 msc_part_demands d,
3203 mfg0 mfg,
3204 msc_part_supplies s1,
3205 msc_part_demands d1,
3206 mfg0 mfg1,
3207 msc_part_supplies s2,
3208 msc_part_demands d2,
3209 msc_system_items msi1
3210 WHERE
3211    k.release_errors IS NULL
3212    AND (k.releasable = ' || RELEASABLE || ' or k.releasable is null )
3213    AND k.sr_instance_id = '||p_arg_org_instance|| '
3214    AND k.plan_id = '|| p_arg_plan_id ||'  --K.batchidid filter and k.transctionid/k.load_typefilter added dynamically
3215    AND k.TRANSACTION_ID = s.ORIG_TRANSACTION_ID
3216    AND s.transaction_id = d.disposition_id
3217    AND s.sr_instance_id = d.sr_instance_id
3218    AND s.plan_id = d.plan_id
3219    AND d.origination_type = 1 --- Planned order demand
3220    AND d.demand_id = mfg.demand_id
3221    AND d.plan_id = mfg.plan_id
3222    AND d.sr_instance_id = mfg.sr_instance_id
3223    AND d.organization_id = mfg.organization_id
3224    AND mfg.transaction_id = s1.transaction_id
3225    AND mfg.plan_id = s1.plan_id
3226    AND s1.plan_id = msi1.plan_id
3227    AND s1.sr_instance_id = msi1.sr_instance_id
3228    AND s1.organization_id = msi1.organization_id
3229    AND s1.inventory_item_id = msi1.inventory_item_id
3230    AND s1.transaction_id = d1.disposition_id
3231    AND s1.sr_instance_id = d1.sr_instance_id
3232    AND s1.plan_id = d1.plan_id
3233    AND d1.origination_type = 78 -- planned defective part demand
3234    AND d1.plan_id = mfg1.plan_id (+)
3235    AND d1.sr_instance_id = mfg1.sr_instance_id (+)
3236    AND d1.organization_id = mfg1.organization_id (+)
3237    AND d1.demand_id = mfg1.demand_id (+)
3238    AND mfg1.transaction_id = s2.transaction_id(+)
3239    AND mfg1.plan_id = s2.plan_id(+)
3240    AND s2.transaction_id = d2.disposition_id(+)
3241    AND s2.sr_instance_id = d2.sr_instance_id(+)
3242    AND s2.plan_id = d2.plan_id(+)
3243    AND d2.origination_type(+) = 1';
3244 
3245     IF  v_batch_id_populated = 2 THEN
3246         l_sql_qry2 :=  l_sql_qry2 || ' AND  k.batch_id is NULL ';
3247     ELSE
3248         l_sql_qry2 :=  l_sql_qry2 || ' AND  k.batch_id = ' || g_batch_id || ' ' ;
3249     END IF;
3250 
3251     l_sql_qry2:= l_sql_qry2  ||p_where_clause;
3252 
3253 
3254 
3255  IF ((p_arg_mode IS NULL) OR (p_arg_mode = 'WF')) THEN
3256   -- bulk collect l_sql_qry2 into lv_IRO_list;
3257   EXECUTE IMMEDIATE l_sql_qry2
3258     bulk collect INTO
3259     lv_transaction_id_lst,
3260     lv_quantity_lst,
3261     lv_in_req_quantity_lst,
3262     lv_out_req_quantity_lst,
3263     lv_in_req_transaction_id_lst,
3264     lv_out_req_transaction_id_lst,
3265     lv_inventory_item_id_lst,
3266     lv_uom_code_lst,
3267     lv_organization_id_lst,
3268     lv_promise_date_lst,
3269     lv_implement_location_id,
3270     lv_load_type_lst,
3271     lv_implement_source_org_id,
3272     lv_orig_transaction_id_lst
3273  ;
3274  END IF;
3275 
3276    p_where_clause :=null ;
3277 
3278 
3279   /* 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 */
3280      for i IN 1 .. lv_orig_transaction_id_lst.count LOOP
3281 
3282 
3283               UPDATE msc_supplies
3284               SET --load_type = 32,
3285                  release_status = 2,
3286                 -- implement_quantity = new_order_quantity,  --to be removed if above api works fine
3287                  --release_errors = NULL,
3288                 batch_id=decode(v_batch_id_populated,2,batch_id,g_batch_id)
3289               WHERE transaction_id = lv_orig_transaction_id_lst(i)
3290                 and plan_id =p_arg_plan_id ;
3291       END LOOP;
3292 
3293      for i IN 1 .. lv_in_req_transaction_id_lst.count LOOP
3294 
3295          UPDATE msc_part_supplies
3296               SET load_type = 32,
3297                 --release_status = 2,
3298                 --implement_quantity = new_order_quantity,  --to be removed if above api works fine
3299                 release_errors = NULL,
3300                 batch_id=decode(v_batch_id_populated,2,batch_id,g_batch_id)
3301               WHERE transaction_id in ( lv_in_req_transaction_id_lst(i),lv_out_req_transaction_id_lst(i))
3302               and plan_id =p_arg_plan_id ;
3303 
3304 
3305     END LOOP;
3306 
3307 
3308   /* 5. bulk insert the data in the table type to mrp_iro_release table in the source */
3309   FOR i IN 1 .. lv_transaction_id_lst.COUNT
3310   LOOP
3311 
3312     l_sql_qry := 'insert into mrp_iro_release' || p_dblink || '(Transaction_id,
3313                           Quantity,
3314                           in_req_quantity,
3315                           out_req_quantity,
3316                           In_req_transaction_id,
3317                           Out_req_transaction_id,
3318                           Batch_id,
3319                           Inventory_item_id,
3320                           Uom_code,
3321                           Organization_id,
3322                           Promise_date,
3323                           Load_Type,
3324                           deliver_to_location_id,
3325                           src_organization_id)
3326                   values (:lv_Transaction_id,
3327                           :lv_Quantity,
3328                           :lv_in_req_quantity,
3329                           :lv_out_req_quantity,
3330                           :lv_In_req_transaction_id,
3331                           :lv_Out_req_transaction_id,
3332                           :p_arg_iro_batch_number,
3333                           :lv_Inventory_item,
3334                           :lv_uom_code ,
3335                           :lv_organization_id,
3336                           :lv_Promise_date,
3337                           :Load_Type,
3338                           :implement_location_id,
3339                           :implement_source_org_id)';
3340 
3341     EXECUTE IMMEDIATE l_sql_qry USING
3342       lv_transaction_id_lst(i),
3343       lv_quantity_lst(i),
3344       lv_in_req_quantity_lst(i),
3345       lv_out_req_quantity_lst(i),
3346       lv_in_req_transaction_id_lst(i),
3347       lv_out_req_transaction_id_lst(i),
3348       p_arg_iro_batch_number,
3349       lv_inventory_item_id_lst(i),
3350       lv_uom_code_lst(i),
3351       lv_organization_id_lst(i),
3352       lv_promise_date_lst(i),
3353       lv_load_type_lst(i),
3354       lv_implement_location_id(i),
3355       lv_implement_source_org_id(i);
3356 
3357 
3358     p_arg_loaded_int_repair_orders := p_arg_loaded_int_repair_orders + 1 ;
3359 
3360         update msc_supplies  -- change the variable name to lv_orig_transaction_id
3361            set releasable = RELEASE_ATTEMPTED,
3362                batch_id   = nvl(batch_id,g_batch_id)
3363            where plan_id = p_arg_plan_id
3364            and transaction_id= lv_orig_transaction_id_lst(i);
3365 
3366         /*    update msc_part_supplies
3367            set releasable = RELEASE_ATTEMPTED,
3368                batch_id   = nvl(batch_id,g_batch_id)
3369            where plan_id = p_arg_plan_id
3370            and transaction_id= lv_transaction_id_lst(i);*/
3371 
3372     END LOOP;
3373 
3374      /*
3375   OPEN c_row_count FOR l_sql_cur USING p_arg_iro_batch_number;
3376   FETCH c_row_count
3377   INTO l_after_row_count;
3378   CLOSE c_row_count;
3379   */
3380 
3381 
3382   --p_arg_loaded_int_repair_orders:= l_after_row_count -l_before_row_count;
3383 
3384 
3385   /* 6. call POPULATE_ISO_IN_SOURCE_2 to populate mrp_org_transfer_release_table in the source */
3386 
3387   IF p_arg_loaded_int_repair_orders > 0 THEN
3388 
3389       populate_iso_in_source_2(l_dblink => p_dblink,
3390        l_arg_po_batch_number => p_arg_iro_batch_number,
3391        l_arg_owning_instance => p_arg_owning_instance,
3392        l_arg_po_group_by => p_arg_po_group_by,
3393        l_arg_plan_id => p_arg_plan_id,
3394        l_arg_log_org_id => p_arg_log_org_id,
3395        l_arg_owning_org_id => p_arg_owning_org_id,
3396        l_arg_org_instance => p_arg_org_instance,
3397        l_arg_mode => p_arg_mode,
3398        l_arg_transaction_id => p_arg_transaction_id,
3399        arg_loaded_int_reqs => p_new_iso_num,
3400        arg_resched_int_reqs => p_reschedule_iso_num,
3401        p_load_type => drp_req_load);
3402   END IF;
3403 
3404   l_sql_qry := NULL;
3405 
3406   Exception
3407   When Others Then
3408   Raise;
3409 END release_iro_2;
3410 
3411 PROCEDURE Release_Ero(
3412     p_dblink IN VARCHAR2,
3413     p_arg_ero_batch_number IN number,
3414     p_arg_owning_instance IN NUMBER,
3415     p_arg_po_group_by IN NUMBER,
3416     p_arg_plan_id IN NUMBER,
3417     p_arg_log_org_id IN NUMBER,
3418     p_arg_owning_org_id IN NUMBER,
3419     p_arg_org_instance IN NUMBER,
3420     p_arg_mode IN VARCHAR2,
3421     p_arg_transaction_id IN NUMBER,
3422     p_arg_loaded_ext_repair_orders IN OUT nocopy NUMBER,
3423     p_load_type IN NUMBER)
3424 IS
3425 
3426 l_sql_qry VARCHAR2(8000) := NULL;
3427 l_sql_cnt_qry VARCHAR2(500) := NULL;
3428 p_count NUMBER;
3429 BEGIN
3430 
3431   l_sql_qry := '
3432     Insert into MRP_ERO_RELEASE'||p_dblink||
3433     '  (TRANSACTION_ID,
3434     REPAIR_SUPPLIER_ID ,
3435     REPAIR_SUPPLIER_ORG_ID ,
3436     REPAIR_PROGRAM ,
3437     BATCH_ID ,
3438     DESTINATION_ORG_ID  ,
3439     SOURCE_ORG_ID ,
3440     INVENTORY_ITEM_ID ,
3441     PROMISE_DATE  ,
3442     QUANTITY  ,
3443     DEFECTIVE_ITEM_ID,
3444     DEFECTIVE_ITEM_QTY)
3445 Select
3446     s.transaction_id ,
3447     mtp1.sr_tp_id   repair_supplier_id ,
3448     s1.organization_id  repair_supplier_org_id ,
3449     msi.repair_program  repair_program,
3450      '||p_arg_ero_batch_number||'  ,
3451     s.organization_id  destination_organization_id ,
3452     d2.organization_id source_organization_id ,
3453     msi.sr_inventory_item_id repair_to_item_id ,
3454     nvl(s.implement_dock_date , nvl(s.new_dock_date, sysdate ) ) need_by_date,
3455     s.implement_quantity quantity,
3456     msi1.sr_inventory_item_id  defective_item_id,
3457     s2.new_order_quantity defective_part_quantity
3458 FROM msc_supplies s,
3459      Msc_supplies s1,
3460      Msc_supplies s2 ,
3461      msc_system_items msi,
3462      msc_system_items msi1,
3463      msc_trading_partners mtp,
3464      msc_trading_partners mtp1,
3465      msc_demands d  ,
3466      msc_demands d1,
3467      msc_demands d2,
3468     (  select distinct transaction_id,plan_id,organization_id ,sr_instance_id ,demand_id
3469              from msc_full_pegging
3470              where sr_instance_id = '||p_arg_org_instance||'
3471              and plan_id ='||p_arg_plan_id||'
3472       ) mfg,
3473      (  select  transaction_id,plan_id,organization_id ,sr_instance_id ,demand_id ,
3474                  sum(allocated_quantity) allocated_quantity
3475                 from msc_full_pegging
3476                 where sr_instance_id = '||p_arg_org_instance||'
3477                 and plan_id ='||p_arg_plan_id||'
3478                 group by transaction_id,plan_id,organization_id ,sr_instance_id ,demand_id
3479       ) mfg1
3480 WHERE s.release_errors is NULL
3481     AND (s.releasable = ' || RELEASABLE || ' or s.releasable is null )
3482     AND   s.implement_quantity > 0
3483     AND   s.sr_instance_id = '||p_arg_org_instance||
3484 '    AND   s.plan_id = '||p_arg_plan_id||'
3485     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))
3486     AND   msi.inventory_item_id = s.inventory_item_id
3487     AND   msi.plan_id = s.plan_id
3488     AND   msi.organization_id = s.organization_id
3489     AND   msi.sr_instance_id = s.sr_instance_id
3490     AND   s.transaction_id =d.disposition_id
3491     AND   s.sr_instance_id = d.sr_instance_id
3492     And   s.plan_id =d.plan_id
3493     And   d.origination_type= 1 --- Planned order demand
3494     And   d.demand_id  = mfg.demand_id
3495     And   d.plan_id  = mfg.plan_id
3496     And   d.sr_instance_id =mfg.sr_instance_id
3497     And   d.organization_id=  mfg.organization_id
3498     And   mfg.transaction_id =s1.transaction_id
3499     And   mfg.plan_id=s1.plan_id
3500     AND   mtp.sr_tp_id  = s1.organization_id
3501     AND   mtp.sr_instance_id = s1.sr_instance_id
3502     AND   mtp.partner_type= 3
3503     And   mtp.modeled_supplier_id = mtp1.partner_id
3504     AND   s1.transaction_id =d1.disposition_id
3505     AND   s1.sr_instance_id = d1.sr_instance_id
3506     And   s1.plan_id =d1.plan_id
3507     And   d1.origination_type= 78 -- planned defective part demand
3508     And   d1.demand_id  = mfg1.demand_id
3509     And   d1.plan_id  = mfg1.plan_id
3510     And   d1.sr_instance_id =mfg1.sr_instance_id
3511     And   mfg1.organization_id = mfg1.organization_id
3512     And   mfg1.transaction_id = s2.transaction_id
3513     And   mfg1.plan_id =s2.plan_id
3514     AND   s2.transaction_id =d2.disposition_id
3515     AND   s2.sr_instance_id = d2.sr_instance_id
3516     And   s2.plan_id =d2.plan_id
3517     And   d2.origination_type= 1 -- planned defective out bound shipment
3518     And   d2.sr_instance_id = msi1.sr_instance_id
3519     And   d2.plan_id = msi1.plan_id
3520     And   d2.inventory_item_id = msi1.inventory_item_id
3521     And   d2.organization_id = msi1.organization_id'  ;
3522 
3523     IF  v_batch_id_populated = 2 THEN
3524         l_sql_qry :=  l_sql_qry || ' AND  s.batch_id is NULL ';
3525     ELSE
3526         l_sql_qry :=  l_sql_qry || ' AND  s.batch_id = ' || g_batch_id || ' ' ;
3527     END IF;
3528 
3529  Execute immediate l_sql_qry
3530   USING
3531       p_arg_mode,
3532       p_load_type,
3533       p_arg_mode,
3534       p_arg_transaction_id;
3535 
3536    l_sql_cnt_qry := 'select count(*)
3537    from mrp_ero_release'||p_dblink|| '  where batch_id = '||p_arg_ero_batch_number;
3538 
3539    Execute immediate l_sql_cnt_qry
3540    INTO p_count;
3541 
3542    IF p_arg_mode = 'WF' THEN
3543         update msc_supplies
3544            set releasable = RELEASE_ATTEMPTED
3545                ,batch_id   = nvl(batch_id,g_batch_id)
3546          where plan_id = p_arg_plan_id
3547            and transaction_id= p_arg_transaction_id;
3548    ELSE
3549         execute immediate 'update msc_supplies
3550                                set releasable = ' || RELEASE_ATTEMPTED || '
3551                                ,batch_id   = nvl(batch_id,' ||g_batch_id || ')
3552                              where plan_id = ' || p_arg_plan_id || '
3553                                and transaction_id in
3554                                     (select transaction_id from mrp_ero_release' ||p_dblink||'
3555                                         where batch_id = ' || p_arg_ero_batch_number || ')';
3556    END IF;
3557 
3558   p_arg_loaded_ext_repair_orders := p_count;
3559 
3560   Exception
3561   When Others Then
3562   raise;
3563 
3564 END Release_Ero;
3565 
3566 PROCEDURE Release_Ero_2(
3567     p_dblink IN VARCHAR2,
3568     p_arg_ero_batch_number IN number,
3569     p_arg_owning_instance IN NUMBER,
3570     p_arg_po_group_by IN NUMBER,
3571     p_arg_plan_id IN NUMBER,
3572     p_arg_log_org_id IN NUMBER,
3573     p_arg_owning_org_id IN NUMBER,
3574     p_arg_org_instance IN NUMBER,
3575     p_arg_mode IN VARCHAR2,
3576     p_arg_transaction_id IN NUMBER,
3577     p_arg_loaded_ext_repair_orders IN OUT nocopy NUMBER,
3578     p_load_type IN NUMBER)
3579 IS
3580 l_sql_qry VARCHAR2(8000) := NULL;
3581 l_sql_cnt_qry VARCHAR2(500) := NULL;
3582 p_count NUMBER;
3583 BEGIN
3584 
3585   l_sql_qry := '
3586     Insert into MRP_ERO_RELEASE'||p_dblink||
3587     '  (TRANSACTION_ID,
3588     REPAIR_SUPPLIER_ID ,
3589     REPAIR_SUPPLIER_ORG_ID ,
3590     REPAIR_PROGRAM ,
3591     BATCH_ID ,
3592     DESTINATION_ORG_ID  ,
3593     SOURCE_ORG_ID ,
3594     INVENTORY_ITEM_ID ,
3595     PROMISE_DATE  ,
3596     QUANTITY  ,
3597     DEFECTIVE_ITEM_ID,
3598     DEFECTIVE_ITEM_QTY)
3599 Select
3600     s.transaction_id ,
3601     mtp1.sr_tp_id   repair_supplier_id ,
3602     s1.organization_id  repair_supplier_org_id ,
3603     msi.repair_program  repair_program,
3604      '||p_arg_ero_batch_number||'  ,
3605     s.organization_id  destination_organization_id ,
3606     d2.organization_id source_organization_id ,
3607     msi.sr_inventory_item_id repair_to_item_id ,
3608     nvl(s.implement_dock_date , nvl(s.new_dock_date, sysdate ) ) need_by_date,
3609     s.implement_quantity quantity,
3610     msi1.sr_inventory_item_id  defective_item_id,
3611     s2.new_order_quantity defective_part_quantity
3612 FROM
3613      msc_supplies k,
3614      msc_system_items msi,
3615      msc_part_supplies s,
3616      msc_part_demands d,
3617      Msc_part_supplies s1,
3618      msc_trading_partners mtp,
3619      msc_trading_partners mtp1,
3620      msc_part_demands d1,
3621      Msc_part_supplies s2,
3622      msc_part_demands d2,
3623      msc_system_items msi1,
3624     (select distinct transaction_id,plan_id,organization_id ,sr_instance_id ,demand_id
3625              from msc_part_pegging
3626              where sr_instance_id = '||p_arg_org_instance||'
3627              and plan_id ='||p_arg_plan_id||'
3628       ) mfg,
3629      (  select  transaction_id,plan_id,organization_id ,sr_instance_id ,demand_id ,
3630                  sum(allocated_quantity) allocated_quantity
3631                 from msc_part_pegging
3632                 where sr_instance_id = '||p_arg_org_instance||'
3633                 and plan_id ='||p_arg_plan_id||'
3634                 group by transaction_id,plan_id,organization_id ,sr_instance_id ,demand_id
3635       ) mfg1
3636 WHERE k.release_errors is NULL
3637     AND (k.releasable = ' || RELEASABLE || ' or k.releasable is null )
3638     AND   k.implement_quantity > 0
3639     AND   k.sr_instance_id = '||p_arg_org_instance||
3640 '   AND   k.plan_id = '||p_arg_plan_id||'
3641     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))
3642     AND k.TRANSACTION_ID = s.ORIG_TRANSACTION_ID
3643     AND   msi.inventory_item_id = s.inventory_item_id
3644     AND   msi.plan_id = s.plan_id
3645     AND   msi.organization_id = s.organization_id
3646     AND   msi.sr_instance_id = s.sr_instance_id
3647     AND   s.transaction_id =d.disposition_id
3648     AND   s.sr_instance_id = d.sr_instance_id
3649     And   s.plan_id =d.plan_id
3650     And   d.origination_type= 1 --- Planned order demand
3651     And   d.demand_id  = mfg.demand_id
3652     And   d.plan_id  = mfg.plan_id
3653     And   d.sr_instance_id =mfg.sr_instance_id
3654     And   d.organization_id=  mfg.organization_id
3655     And   mfg.transaction_id =s1.transaction_id
3656     And   mfg.plan_id=s1.plan_id
3657     AND   mtp.sr_tp_id  = s1.organization_id
3658     AND   mtp.sr_instance_id = s1.sr_instance_id
3659     AND   mtp.partner_type= 3
3660     And   mtp.modeled_supplier_id = mtp1.partner_id
3661     AND   s1.transaction_id =d1.disposition_id
3662     AND   s1.sr_instance_id = d1.sr_instance_id
3663     And   s1.plan_id =d1.plan_id
3664     And   d1.origination_type= 78 -- planned defective part demand
3665     And   d1.demand_id  = mfg1.demand_id
3666     And   d1.plan_id  = mfg1.plan_id
3667     And   d1.sr_instance_id =mfg1.sr_instance_id
3668     And   mfg1.organization_id = s2.organization_id
3669     And   mfg1.transaction_id = s2.transaction_id
3670     And   mfg1.plan_id =s2.plan_id
3671     AND   s2.transaction_id =d2.disposition_id
3672     AND   s2.sr_instance_id = d2.sr_instance_id
3673     And   s2.plan_id =d2.plan_id
3674     And   d2.origination_type= 1 -- planned defective out bound shipment
3675     And   d2.sr_instance_id = msi1.sr_instance_id
3676     And   d2.plan_id = msi1.plan_id
3677     And   d2.inventory_item_id = msi1.inventory_item_id
3678     And   d2.organization_id = msi1.organization_id ' ;
3679 
3680     IF  v_batch_id_populated = 2 THEN
3681         l_sql_qry :=  l_sql_qry || ' AND  k.batch_id is NULL ';
3682     ELSE
3683         l_sql_qry :=  l_sql_qry || ' AND  k.batch_id = ' || g_batch_id || ' ' ;
3684     END IF;
3685 
3686  Execute immediate l_sql_qry
3687   USING
3688       p_arg_mode,
3689       p_load_type,
3690       p_arg_mode,
3691       p_arg_transaction_id;
3692 
3693  p_count := SQL%ROWCOUNT;
3694 
3695    IF p_arg_mode = 'WF' THEN
3696         update msc_supplies
3697            set releasable = RELEASE_ATTEMPTED
3698                ,batch_id   = nvl(batch_id,g_batch_id)
3699          where plan_id = p_arg_plan_id
3700            and transaction_id= p_arg_transaction_id;
3701 
3702            update msc_part_supplies
3703            set releasable = RELEASE_ATTEMPTED
3704                ,batch_id = g_batch_id
3705          where plan_id = p_arg_plan_id
3706            and orig_transaction_id= p_arg_transaction_id;
3707    ELSE
3708         execute immediate 'update msc_supplies
3709                                set releasable = ' || RELEASE_ATTEMPTED || '
3710                                ,batch_id   = nvl(batch_id,' ||g_batch_id || ')
3711                              where plan_id = ' || p_arg_plan_id || '
3712                                and transaction_id in (select orig_transaction_id from msc_part_supplies where transaction_id in
3713                                     (select transaction_id from mrp_ero_release' ||p_dblink||'
3714                                         where batch_id = ' || p_arg_ero_batch_number || '))';
3715 
3716         execute immediate 'update msc_part_supplies
3717                                set releasable = ' || RELEASE_ATTEMPTED || '
3718                                ,batch_id   =' || g_batch_id || '
3719                              where plan_id = ' || p_arg_plan_id || '
3720                                and transaction_id in
3721                                     (select transaction_id from mrp_ero_release' ||p_dblink||'
3722                                         where batch_id = ' || p_arg_ero_batch_number || ')';
3723    END IF;
3724 
3725    p_arg_loaded_ext_repair_orders := p_count;
3726 
3727 
3728   Exception
3729   When Others Then
3730   raise;
3731 
3732 END Release_Ero_2;
3733 
3734 
3735 FUNCTION load_osfm_lot_jobs
3736 ( arg_plan_id			IN      NUMBER
3737 , arg_log_org_id 		IN 	NUMBER
3738 , arg_org_instance              IN      NUMBER
3739 , arg_owning_org_id 		IN 	NUMBER
3740 , arg_owning_instance           IN      NUMBER
3741 , arg_user_id 			IN 	NUMBER
3742 , arg_wip_group_id              IN      NUMBER
3743 , arg_mode                      IN      VARCHAR2
3744 , arg_transaction_id            IN      NUMBER,
3745   l_apps_ver                    IN      VARCHAR2
3746 )RETURN NUMBER
3747 IS
3748    lv_loaded_jobs NUMBER := 0;
3749 
3750    TYPE NumTab  IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
3751 
3752 
3753    lv_transaction_id      NumTab;
3754    lv_instance_id         NumTab;
3755    lv_org_id              NumTab;
3756    lv_plan_id             NumTab;
3757    lv_details_enabled     NumTab;
3758    lv_agg_details         NumTab;
3759    lv_job_count           NUMBER;
3760    lv_release_details     NUMBER;
3761    lv_inflate_wip         NUMBER;
3762 
3763 
3764 BEGIN
3765 
3766   SELECT decode(nvl(FND_PROFILE.value('MSC_RELEASE_DTLS_REVDATE'),'Y'),'N',2,1),
3767 	DECODE(NVL(fnd_profile.value('MSC_INFLATE_WIP') ,'N'), 'N',2 ,1)
3768    	INTO lv_release_details,lv_inflate_wip
3769    	FROM dual;
3770 
3771 
3772   /* we release the lot based job details, only if it doesn't use aggregate resources */
3773 
3774  /* Details will NOT be released for
3775      a. Unconstrained Plan
3776      b. if the new_wip_start_date is null
3777      c. if the implement quantity or date is different then the planned quantity date
3778      d. if the revision date is different then the new_wip_start_date
3779         and the profile option setting : MSC_RELEASE_DTLS_REVDATE  = 'N'
3780      e. Non - Daily Bucketed Plans
3781       f.bug 4655420-Alternate BOM/Routing is changed during release.
3782   */
3783 l_sql_stmt := '   SELECT s.transaction_id,
3784    	  s.sr_instance_id,
3785    	  decode( mp.daily_material_constraints+ mp.daily_resource_constraints+
3786                   mp.weekly_material_constraints+ mp.weekly_resource_constraints+
3787                   mp.period_material_constraints+ mp.period_resource_constraints,12,2,
3788                decode(mpb.bucket_type,1,
3789                DECODE( s.implement_quantity, s.new_order_quantity,
3790                      DECODE( s.implement_date, s.new_schedule_date,
3791                       DECODE(NVL(s.implement_alternate_bom, ''-23453''),
3792                      			 NVL(s.alternate_bom_designator, ''-23453''),
3793                 				DECODE(NVL(s.implement_alternate_routing, ''-23453''),
3794                        			NVL(s.alternate_routing_designator, ''-23453''),
3795                         DECODE(trunc(msc_calendar.date_offset
3796                               (s.organization_id,
3797                                s.sr_instance_id,
3798                                1, --daily bucket
3799                                s.need_by_date ,
3800                                ceil(nvl(msi.fixed_lead_time,0) +
3801                  nvl(msi.variable_lead_time,0) * s.implement_quantity)*-1 )),
3802                      trunc(s.new_wip_start_date),1,
3803                                    decode(' || lv_release_details || ' ,2,2,1)),
3804                            2),
3805                          2),
3806                        2),
3807                      2),
3808                  2)),
3809          s.organization_id,
3810          s.plan_id
3811      FROM msc_supplies s,
3812           msc_plan_organizations_v orgs,
3813           msc_plan_buckets mpb,
3814           msc_system_items msi,
3815           msc_plans mp
3816     WHERE  mp.plan_id = :arg_plan_id
3817     AND   s.release_errors is NULL
3818     AND   nvl(s.cfm_routing_flag,0) = 3
3819     AND   s.implement_quantity > 0
3820     AND   s.organization_id = orgs.planned_organization
3821     AND   s.sr_instance_id = orgs.sr_instance_id
3822     AND   s.plan_id = :arg_plan_id
3823     AND   orgs.plan_id = :arg_plan_id
3824     AND   orgs.organization_id = :arg_owning_org_id
3825     AND   orgs.owning_sr_instance = :arg_owning_instance
3826     AND   orgs.sr_instance_id = :arg_org_instance ' ;
3827 
3828     IF arg_log_org_id <> arg_owning_org_id THEN
3829             l_sql_stmt :=  l_sql_stmt ||'      AND     orgs.planned_organization = ' || arg_log_org_id || ' ' ;
3830     END IF;
3831 
3832     IF arg_mode IS NULL THEN
3833         l_sql_stmt :=  l_sql_stmt ||'  AND s.load_type = 5 ';
3834         IF v_msc_released_only_by_user = 1 THEN
3835             l_sql_stmt :=  l_sql_stmt || ' AND s.last_updated_by = ' || v_user_id || ' ' ;
3836         END IF;
3837     ELSIF arg_mode = 'WF_BATCH' THEN
3838         l_sql_stmt :=  l_sql_stmt ||'  AND s.load_type = 5 ';
3839     ELSIF arg_mode = 'WF' THEN
3840         l_sql_stmt :=  l_sql_stmt ||'      AND s.transaction_id = ' || arg_transaction_id  || '  ' ;
3841     END IF;
3842 
3843         l_sql_stmt :=  l_sql_stmt || ' AND   s.plan_id = mpb.plan_id
3844     AND  (nvl(s.implement_date,s.new_schedule_date) between mpb.bkt_start_date and mpb.bkt_end_date)
3845     AND   s.new_wip_start_date IS NOT NULL
3846     AND   msi.inventory_item_id = s.inventory_item_id
3847     AND   msi.plan_id = s.plan_id
3848     AND   msi.organization_id = s.organization_id
3849     AND   msi.sr_instance_id = s.sr_instance_id
3850     and (s.releasable = ' || RELEASABLE || ' or s.releasable is null ) ';
3851 
3852     IF  v_batch_id_populated = 2 THEN
3853         l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id is NULL ';
3854     ELSE
3855         l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id = ' || g_batch_id || ' ' ;
3856     END IF;
3857     l_sql_stmt :=  l_sql_stmt || '
3858 UNION
3859   SELECT s.transaction_id,
3860           s.sr_instance_id,
3861           2 /* Details not enabled for Manual Planned orders */,
3862           s.organization_id,
3863           s.plan_id
3864      FROM msc_supplies s,
3865           msc_plan_organizations_v orgs
3866     WHERE s.release_errors is NULL
3867     AND   nvl(s.cfm_routing_flag,0) = 3
3868     AND   s.implement_quantity > 0
3869     AND   s.organization_id = orgs.planned_organization
3870     AND   s.sr_instance_id = orgs.sr_instance_id
3871     AND   s.plan_id = :arg_plan_id
3872     AND   orgs.plan_id = :arg_plan_id
3873     AND   orgs.organization_id = :arg_owning_org_id
3874     AND   orgs.owning_sr_instance = :arg_owning_instance
3875     AND   orgs.sr_instance_id = :arg_org_instance
3876     and   s.new_wip_start_date IS NULL
3877     and (s.releasable = ' || RELEASABLE || ' or s.releasable is null )' ;
3878 
3879     IF  v_batch_id_populated = 2 THEN
3880         l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id is NULL ';
3881     ELSE
3882         l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id = ' || g_batch_id || ' ' ;
3883     END IF;
3884 
3885     IF arg_log_org_id <> arg_owning_org_id THEN
3886             l_sql_stmt :=  l_sql_stmt ||'      AND     orgs.planned_organization = ' || arg_log_org_id || ' ' ;
3887     END IF;
3888 
3889     IF arg_mode IS NULL THEN
3890         l_sql_stmt :=  l_sql_stmt ||'  AND s.load_type = 5 ';
3891         IF v_msc_released_only_by_user = 1 THEN
3892             l_sql_stmt :=  l_sql_stmt || ' AND s.last_updated_by = ' || v_user_id || ' ' ;
3893         END IF;
3894     ELSIF arg_mode = 'WF_BATCH' THEN
3895         l_sql_stmt :=  l_sql_stmt ||'  AND s.load_type = 5 ';
3896     ELSIF arg_mode = 'WF' THEN
3897         l_sql_stmt :=  l_sql_stmt ||'      AND s.transaction_id = ' || arg_transaction_id  || '  ' ;
3898     END IF;
3899 
3900     lv_job_count:= 0;
3901     IF arg_mode IS NULL OR arg_mode = 'WF_BATCH' OR arg_mode = 'WF' THEN
3902         EXECUTE IMMEDIATE l_sql_stmt
3903               BULK COLLECT     INTO   lv_transaction_id,
3904                                       lv_instance_id,
3905                                       lv_details_enabled,
3906                                       lv_org_id,
3907                                       lv_plan_id
3908                                     USING  arg_plan_id
3909                                             ,arg_plan_id
3910                                             ,arg_plan_id
3911                                             ,arg_owning_org_id
3912                                             ,arg_owning_instance
3913                                             ,arg_org_instance
3914                                             ,arg_plan_id
3915                                             ,arg_plan_id
3916                                             ,arg_owning_org_id
3917                                             ,arg_owning_instance
3918                                             ,arg_org_instance;
3919         lv_job_count:= SQL%ROWCOUNT;
3920     END IF;
3921 
3922 
3923 
3924     -- -----------------------------------------------------------------------
3925     -- Perform the wip discrete job mass load
3926     -- -----------------------------------------------------------------------
3927        /* Due to we only give PLANNED components, BILL_RTG_EXPLOSION_FLAG
3928           is set to 'Y'.  */
3929 
3930          FOR k in 1..lv_job_count
3931        Loop
3932                 Begin
3933                   SELECT 2
3934                   Into lv_agg_details(k)
3935                   FROM msc_department_resources deptres,
3936                        msc_resource_requirements resreq
3937                  WHERE resreq.sr_instance_id= lv_instance_id(k)
3938                    AND resreq.supply_id = lv_transaction_id(k)
3939                    AND resreq.organization_id= lv_org_id(k)
3940                    AND resreq.plan_id   = lv_plan_id(k)
3941                    AND resreq.parent_id   = 2
3942                    AND deptres.plan_id  = resreq.plan_id
3943                    AND deptres.sr_instance_id= resreq.sr_instance_id
3944                    AND deptres.resource_id= resreq.resource_id
3945                    AND deptres.department_id= resreq.department_id
3946                    AND deptres.organization_id= resreq.organization_id
3947                    AND deptres.aggregate_resource_flag= 1
3948                    AND rownum=1;
3949                   Exception
3950                   When no_data_found
3951                   then
3952                   lv_agg_details(k) := 1;
3953                   End;
3954 
3955        End Loop;
3956 
3957 
3958 
3959     FORALL j IN 1..lv_job_count
3960     INSERT INTO msc_wip_job_schedule_interface
3961             (last_update_date,
3962              cfm_routing_flag,
3963             last_updated_by,
3964             last_update_login,
3965             creation_date,
3966             created_by,
3967             group_id,
3968             source_code,
3969             source_line_id,
3970             organization_id,
3971             organization_type,
3972             load_type,
3973             status_type,
3974             first_unit_start_date,
3975             last_unit_completion_date,
3976             bom_revision_date,
3977             routing_revision_date,
3978             primary_item_id,
3979             class_code,
3980             job_name,
3981             firm_planned_flag,
3982             start_quantity,
3983 	    net_quantity,
3984             demand_class,
3985             project_id,
3986             task_id,
3987 	    schedule_group_id,
3988        	    build_sequence,
3989 	    line_id,
3990 	    alternate_bom_designator,
3991 	    alternate_routing_designator,
3992 	    end_item_unit_number,
3993 	    process_phase,
3994 	    process_status,
3995             bom_reference_id,
3996             routing_reference_id,
3997             BILL_RTG_EXPLOSION_FLAG,
3998             HEADER_ID,
3999             uom_code, --Outbound Changes for XML
4000             SR_INSTANCE_ID
4001             -- dsr: 2 new columns
4002             , schedule_priority
4003             , requested_completion_date
4004             )
4005     SELECT  SYSDATE,
4006             nvl(s.cfm_routing_flag,0),
4007             arg_user_id,
4008             s.last_update_login,
4009             SYSDATE,
4010             arg_user_id,
4011             arg_wip_group_id,
4012             'MSC',
4013             s.transaction_id,
4014             msi.organization_id,
4015             tp.organization_type,
4016             5,
4017             s.implement_status_code,
4018             decode(lv_details_enabled(j),1,new_wip_start_date,null),
4019             s.implement_date,
4020             s.new_wip_start_date,
4021             s.new_wip_start_date,
4022             msi.sr_inventory_item_id,
4023             s.implement_wip_class_code,
4024             s.implement_job_name,
4025             s.implement_firm,
4026             decode(s.implement_quantity,s.new_order_quantity,
4027                                           nvl(s.wip_start_quantity,s.implement_quantity),
4028                                         s.implement_quantity),
4029 	    s.implement_quantity,
4030             s.implement_demand_class,
4031             s.implement_project_id,
4032             s.implement_task_id,
4033 	    s.implement_schedule_group_id,
4034 	    s.implement_build_sequence,
4035        	    s.implement_line_id,
4036 	    s.implement_alternate_bom,
4037 	    s.implement_alternate_routing,
4038  	    s.implement_unit_number,
4039 	    2,
4040 	    1,
4041             DECODE( tp.organization_type,
4042                     2, mpe.bill_sequence_id,
4043                     NULL),
4044             DECODE( tp.organization_type,
4045                     2, mpe.routing_sequence_id,
4046                     NULL),
4047             'Y',
4048             s.transaction_id,
4049             nvl(s.implement_uom_code,msi.uom_code),
4050             s.sr_instance_id
4051              -- dsr: 2 new columns
4052             , s.schedule_priority
4053             , nvl(s.requested_completion_date, s.need_by_date)
4054       FROM  msc_trading_partners    tp,
4055             msc_parameters          param,
4056             msc_system_items        msi,
4057             msc_process_effectivity mpe,
4058             msc_supplies            s
4059     WHERE   tp.sr_tp_id= msi.organization_id
4060     AND     tp.sr_instance_id= msi.sr_instance_id
4061     AND     tp.partner_type=3
4062     AND     param.organization_id = msi.organization_id
4063     AND     param.sr_instance_id  = msi.sr_instance_id
4064     AND     msi.inventory_item_id = s.inventory_item_id
4065     AND     msi.plan_id = s.plan_id
4066     AND     msi.organization_id = s.organization_id
4067     AND     msi.sr_instance_id = s.sr_instance_id
4068     AND     mpe.plan_id(+)= s.plan_id
4069     AND     mpe.process_sequence_id(+)= s.process_seq_id
4070     AND     s.transaction_id= lv_transaction_id(j)
4071     AND     s.sr_instance_id= lv_instance_id(j)
4072     AND     s.plan_id= arg_plan_id
4073     AND     nvl(s.cfm_routing_flag,0) = 3;
4074 
4075 
4076     IF SQL%ROWCOUNT > 0
4077     THEN
4078         lv_loaded_jobs := SQL%ROWCOUNT;
4079 
4080     ELSE
4081         lv_loaded_jobs := 0;
4082 
4083     END IF;
4084 
4085         -- ------------------------------------------------------------------------
4086     -- Perform the lot-based job mass load for the details
4087     -- -----------------------------------------------------------------------
4088 
4089     /* lot-based job details are released only when the source profile WSM: Create Lot Based Job Routing is Yes
4090     and org planning parameter is primary */
4091 
4092 
4093     /* OPERATION NETWORKS */
4094 
4095 
4096     FORALL j IN 1..lv_job_count
4097     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
4098     (last_update_date,
4099      last_updated_by,
4100      last_update_login,
4101      creation_date,
4102      created_by,
4103      group_id,
4104      parent_header_id,
4105      SUBSTITUTION_TYPE,
4106      LOAD_TYPE,
4107      process_phase,
4108      process_status,
4109      OPERATION_SEQ_NUM,
4110      NEXT_ROUTING_OP_SEQ_NUM,
4111      cfm_routing_flag,
4112      SR_INSTANCE_ID)
4113   (SELECT  SYSDATE,
4114             arg_user_id,
4115             s.last_update_login,
4116             SYSDATE,
4117             arg_user_id,
4118             arg_wip_group_id,
4119             s.transaction_id,
4120             4,
4121             5,
4122             1,
4123             1,
4124             nwk.from_op_seq_num,
4125             nwk.to_op_seq_num,
4126             3,
4127             s.sr_instance_id
4128    From msc_supplies s,
4129    msc_operation_networks nwk,
4130    msc_apps_instances ins,
4131    msc_parameters param
4132    Where    nwk.plan_id = s.plan_id
4133     AND     nwk.sr_instance_id = s.sr_instance_id
4134     AND     nwk.routing_sequence_id = s.routing_sequence_id
4135     AND     nwk.transition_type = 1
4136     AND     s.transaction_id= lv_transaction_id(j)
4137     AND     s.sr_instance_id= lv_instance_id(j)
4138     AND     s.plan_id= arg_plan_id
4139     AND     lv_details_enabled(j)= 1
4140     AND     lv_agg_details(j) = 1
4141     AND     ins.instance_id = lv_instance_id(j)
4142     AND     nvl(ins.lbj_details,2) = 1
4143     AND     param.organization_id = s.organization_id
4144     AND     param.sr_instance_id = s.sr_instance_id
4145     AND     param.network_scheduling_method = 1);
4146 
4147 
4148     /* Operations */
4149 
4150 FORALL j IN 1..lv_job_count
4151     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
4152     (last_update_date,
4153      last_updated_by,
4154      last_update_login,
4155      creation_date,
4156      created_by,
4157      group_id,
4158      parent_header_id,
4159      SUBSTITUTION_TYPE,
4160      LOAD_TYPE,
4161      process_phase,
4162      process_status,
4163      OPERATION_SEQ_NUM,
4164      first_unit_start_date,
4165      last_unit_completion_date,
4166      cfm_routing_flag,
4167      SR_INSTANCE_ID,
4168      scheduled_quantity)
4169   (SELECT  SYSDATE,
4170             arg_user_id,
4171             s.last_update_login,
4172             SYSDATE,
4173             arg_user_id,
4174             arg_wip_group_id,
4175             s.transaction_id,
4176             4,
4177             3,
4178             1,
4179             1,
4180             res.operation_seq_num,
4181             min(res.START_DATE),
4182             max(res.END_DATE),
4183             3,
4184             s.sr_instance_id,
4185             max(nvl(res.CUMMULATIVE_QUANTITY,0))
4186    From msc_supplies s,
4187    msc_resource_requirements res,
4188    msc_apps_instances ins,
4189    msc_parameters param
4190    Where    res.plan_id = s.plan_id
4191     AND     res.sr_instance_id = s.sr_instance_id
4192     AND     s.transaction_id = res.supply_id
4193     AND     res.parent_id = 2
4194     -- AND     res.resource_id <> -1 	Bug#3432607
4195     -- AND     res.department_id <> -1
4196     AND     s.transaction_id= lv_transaction_id(j)
4197     AND     s.sr_instance_id= lv_instance_id(j)
4198     AND     s.plan_id= arg_plan_id
4199     AND     lv_details_enabled(j)= 1
4200     AND     lv_agg_details(j) = 1
4201     AND     ins.instance_id = lv_instance_id(j)
4202     AND     nvl(ins.lbj_details,2) = 1
4203     AND     param.organization_id = s.organization_id
4204     AND     param.sr_instance_id = s.sr_instance_id
4205     AND     param.network_scheduling_method = 1
4206     GROUP BY
4207             s.last_update_login,
4208             s.transaction_id,
4209             res.OPERATION_SEQ_NUM,
4210             s.sr_instance_id);
4211          --   res.CUMMULATIVE_QUANTITY);
4212 
4213     /* Resources */
4214 FORALL j IN 1..lv_job_count
4215     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
4216     (last_update_date,
4217      last_updated_by,
4218      last_update_login,
4219      creation_date,
4220      created_by,
4221      group_id,
4222      parent_header_id,
4223      SUBSTITUTION_TYPE,
4224      LOAD_TYPE,
4225      process_phase,
4226      process_status,
4227      operation_seq_num,
4228      -- dsr resource_seq_num,
4229      resource_id_new,
4230      start_date,
4231      completion_date,
4232      alternate_num,
4233      cfm_routing_flag,
4234      SR_INSTANCE_ID
4235     -- dsr: add following columns
4236      , firm_flag
4237      , setup_id
4238      , group_sequence_id
4239      , group_sequence_number
4240      , batch_id
4241      , maximum_assigned_units
4242      , parent_seq_num
4243      , resource_seq_num
4244      , schedule_seq_num
4245      , assigned_units
4246      , usage_rate_or_amount
4247      , scheduled_flag
4248 	 )
4249   (SELECT  SYSDATE,
4250             arg_user_id,
4251             s.last_update_login,
4252             SYSDATE,
4253             arg_user_id,
4254             arg_wip_group_id,
4255             s.transaction_id,
4256             decode(res.parent_seq_num, null,4,2),
4257             1,
4258             1,
4259             1,
4260             res.operation_seq_num,
4261            -- res.resource_seq_num,
4262             res.resource_id,
4263             nvl(res.firm_start_date,res.START_DATE),
4264             nvl(res.firm_end_date,res.END_DATE),
4265             nvl(res.alternate_num,0),
4266             3,
4267             s.sr_instance_id
4268             -- dsr: add following columns
4269             , res.firm_flag
4270 	    , res.setup_id
4271 	    , res.group_sequence_id
4272 	   , res.group_sequence_number
4273            , res.batch_number
4274 	   , res.maximum_assigned_units
4275 	  , res.parent_seq_num
4276 	  , res.orig_resource_seq_num
4277 	  , res.resource_seq_num
4278 	  , res.assigned_units
4279 	  , decode(res.parent_seq_num, null, (res.RESOURCE_HOURS/decode(res.basis_type,2,1,
4280                     nvl(res.cummulative_quantity,
4281                              decode(msi.rounding_control_type,1,
4282                ROUND(s.new_order_quantity/nvl(res.REVERSE_CUMULATIVE_YIELD,1) ,6),
4283                     (s.new_order_quantity/nvl(res.REVERSE_CUMULATIVE_YIELD,1))
4284                             ) ) ))* decode(mdr.efficiency,NULL,100,0,100,mdr.efficiency)/100 * decode(mdr.utilization,NULL,100,0,100,mdr.utilization)/100, res.RESOURCE_HOURS)
4285            ,  decode(nvl(res.schedule_flag,1),-23453,1,1,1,res.schedule_flag)
4286    From msc_supplies s,
4287    msc_resource_requirements res,
4288    msc_apps_instances ins,
4289    msc_parameters param,
4290    msc_department_resources mdr,
4291    msc_system_items        msi
4292    Where    res.plan_id = s.plan_id
4293     AND     res.sr_instance_id = s.sr_instance_id
4294     AND     s.transaction_id = res.supply_id
4295     AND     res.parent_id = 2
4296     AND     res.resource_id <> -1
4297     AND     res.department_id <> -1
4298     AND     s.transaction_id= lv_transaction_id(j)
4299     AND     s.sr_instance_id= lv_instance_id(j)
4300     AND     s.plan_id= arg_plan_id
4301     AND     lv_details_enabled(j)= 1
4302     AND     lv_agg_details(j) = 1
4303     AND     ins.instance_id = lv_instance_id(j)
4304     AND     nvl(ins.lbj_details,2) = 1
4305     AND     param.organization_id = s.organization_id
4306     AND     param.sr_instance_id = s.sr_instance_id
4307     AND     res.plan_id = mdr.plan_id
4308     AND     res.organization_id =mdr.organization_id
4309     AND     res.sr_instance_id = mdr.sr_instance_id
4310     AND     res.resource_id = mdr.resource_id
4311     AND     res.department_id=mdr.department_id
4312     AND     msi.inventory_item_id = s.inventory_item_id
4313     AND     msi.plan_id = s.plan_id
4314     AND     msi.organization_id = s.organization_id
4315     AND     msi.sr_instance_id = s.sr_instance_id
4316     AND     param.network_scheduling_method = 1);
4317 
4318     /*Components*/
4319 
4320     FORALL j IN 1..lv_job_count
4321     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
4322     (last_update_date,
4323      last_updated_by,
4324      last_update_login,
4325      creation_date,
4326      created_by,
4327      group_id,
4328      parent_header_id,
4329      SUBSTITUTION_TYPE,
4330      LOAD_TYPE,
4331      process_phase,
4332      process_status,
4333      operation_seq_num,
4334      inventory_item_id_new,
4335      primary_component_id,
4336      source_phantom_id,
4337      component_seq_id,
4338      mrp_net_flag,
4339      date_required,
4340      mps_date_required,
4341      basis_type,
4342      quantity_per_assembly,
4343      required_quantity,
4344      mps_required_quantity,
4345      cfm_routing_flag,
4346      SR_INSTANCE_ID)
4347   (SELECT  SYSDATE,
4348             arg_user_id,
4349             s.last_update_login,
4350             SYSDATE,
4351             arg_user_id,
4352             arg_wip_group_id,
4353             s.transaction_id,
4354             4,
4355             2,
4356             1,
4357             1,
4358             nvl(md.op_seq_num,1),
4359             icomp.sr_inventory_item_id,
4360             icomp1.sr_inventory_item_id,
4361             icomp2.sr_inventory_item_id,
4362             md.COMP_SEQ_ID,
4363             1,
4364             md.USING_ASSEMBLY_DEMAND_DATE,
4365             md.USING_ASSEMBLY_DEMAND_DATE,
4366             decode(md.component_scaling_type,1,NULL,md.component_scaling_type),
4367             md.quantity_per_assembly,
4368             md.USING_REQUIREMENT_QUANTITY,
4369             md.USING_REQUIREMENT_QUANTITY,
4370             3,
4371             s.sr_instance_id
4372    From msc_supplies s,
4373    msc_demands md,
4374    msc_system_items icomp,
4375    msc_system_items icomp1,
4376    msc_system_items icomp2,
4377    msc_apps_instances ins,
4378    msc_parameters param
4379    Where   /* not exists (select 'x'
4380                         from msc_exception_details excp
4381                         where excp.plan_id = s.plan_id
4382                         and excp.number1 = s.transaction_id
4383                         and excp.inventory_item_id = s.inventory_item_id
4384                         and excp.organization_id = s.organization_id
4385                         and excp.sr_instance_id = s.sr_instance_id
4386                         and excp.exception_type = 33
4387                         and excp.number2 = md.inventory_item_id)*/  /* not needed as inv_old need not be populated*/
4388     	    icomp.inventory_item_id= md.inventory_item_id
4389     AND     icomp.organization_id= md.organization_id
4390     AND     icomp.sr_instance_id= md.sr_instance_id
4391     AND     icomp.plan_id= md.plan_id
4392     AND     nvl(icomp.wip_supply_type,0) <> 6
4393     AND     icomp1.inventory_item_id= md.primary_component_id
4394     AND     icomp1.organization_id= md.organization_id
4395     AND     icomp1.sr_instance_id= md.sr_instance_id
4396     AND     icomp1.plan_id= md.plan_id
4397     AND     icomp2.inventory_item_id(+)= md.source_phantom_id
4398     AND     icomp2.organization_id(+)= md.organization_id
4399     AND     icomp2.sr_instance_id(+)= md.sr_instance_id
4400     AND     icomp2.plan_id(+)= md.plan_id
4401     AND     md.plan_id = s.plan_id
4402     AND     md.sr_instance_id = s.sr_instance_id
4403     AND     md.disposition_id= s.transaction_id
4404     AND     md.origination_type = 1
4405     AND     s.transaction_id= lv_transaction_id(j)
4406     AND     s.sr_instance_id= lv_instance_id(j)
4407     AND     s.plan_id= arg_plan_id
4408     AND     lv_details_enabled(j)= 1
4409     AND     lv_agg_details(j) = 1
4410     AND     ins.instance_id = lv_instance_id(j)
4411     AND     nvl(ins.lbj_details,2) = 1
4412     AND     param.organization_id = s.organization_id
4413     AND     param.sr_instance_id = s.sr_instance_id
4414     AND     param.network_scheduling_method = 1);
4415 
4416 
4417  /* Resource Usage */
4418  FORALL j IN 1..lv_job_count
4419     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
4420     (last_update_date,
4421      last_updated_by,
4422      last_update_login,
4423      creation_date,
4424      created_by,
4425      group_id,
4426      parent_header_id,
4427      SUBSTITUTION_TYPE,
4428      LOAD_TYPE,
4429      process_phase,
4430      process_status,
4431      operation_seq_num,
4432      -- dsr resource_seq_num,
4433      resource_id_new,
4434      assigned_units,
4435      alternate_num,
4436      start_date,
4437      completion_date,
4438      cfm_routing_flag,
4439      SR_INSTANCE_ID
4440      -- dsr: add folllowing columns
4441      , resource_seq_num
4442      , schedule_seq_num
4443      , parent_seq_num
4444      )
4445   (SELECT  SYSDATE,
4446             arg_user_id,
4447             s.last_update_login,
4448             SYSDATE,
4449             arg_user_id,
4450             arg_wip_group_id,
4451             s.transaction_id,
4452             4,
4453             4,
4454             1,
4455             1,
4456             res.operation_seq_num,
4457             -- dsr res.resource_seq_num,
4458             res.resource_id,
4459             res.assigned_units,
4460             nvl(res.alternate_num,0),
4461             nvl(res.firm_start_date,res.START_DATE),
4462             nvl(res.firm_end_date,res.END_DATE),
4463             3,
4464             s.sr_instance_id
4465              -- dsr: add folllowing columns
4466 	    , res.orig_resource_seq_num
4467 	    , res.resource_seq_num
4468 	    , res.parent_seq_num
4469    From msc_supplies s,
4470    msc_resource_requirements res,
4471    msc_apps_instances ins,
4472    msc_parameters param
4473    Where    res.plan_id = s.plan_id
4474     AND     res.sr_instance_id = s.sr_instance_id
4475     AND     s.transaction_id = res.supply_id
4476     AND     res.parent_id = 1
4477     AND     res.resource_id <> -1
4478     AND     res.department_id <> -1
4479     AND     s.transaction_id= lv_transaction_id(j)
4480     AND     s.sr_instance_id= lv_instance_id(j)
4481     AND     s.plan_id= arg_plan_id
4482     AND     lv_details_enabled(j)= 1
4483     AND     lv_agg_details(j) = 1
4484     AND     ins.instance_id = lv_instance_id(j)
4485     AND     nvl(ins.lbj_details,2) = 1
4486     AND     param.organization_id = s.organization_id
4487     AND     param.sr_instance_id = s.sr_instance_id
4488     AND     param.network_scheduling_method = 1);
4489 
4490     -- dsr begin: Operation Resource Instances
4491     FORALL j IN 1..lv_job_count
4492     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
4493           ( last_update_date,
4494             last_updated_by,
4495             last_update_login,
4496             creation_date,
4497             created_by,
4498             organization_type,
4499             organization_id,
4500             group_id,
4501             parent_header_id,
4502             operation_seq_num,
4503            -- resource_seq_num, rawasthi
4504             RESOURCE_INSTANCE_ID,
4505             start_date,
4506             completion_date,
4507             SUBSTITUTION_TYPE,
4508             LOAD_TYPE,
4509             process_phase,
4510             process_status,
4511             SR_INSTANCE_ID,
4512             operation_seq_id,
4513             FIRM_FLAG,
4514             resource_hours,
4515             department_id,
4516 	    SERIAL_NUMBER,
4517             group_sequence_id,
4518             group_sequence_number,
4519             batch_id
4520            , resource_seq_num
4521            , schedule_seq_num
4522            , parent_seq_num
4523            , cfm_routing_flag
4524            , resource_id_new
4525            , assigned_units
4526           )
4527     SELECT
4528            SYSDATE,
4529            arg_user_id,
4530            s.last_update_login,
4531            SYSDATE,
4532            arg_user_id,
4533            tp.organization_type,
4534            s.organization_id,
4535            arg_wip_group_id,
4536            s.transaction_id,
4537            resreq.OPERATION_SEQ_NUM,
4538         -- resreq.RESOURCE_SEQ_NUM,
4539            dep_res_inst.RES_INSTANCE_ID,
4540            nvl(resreq.firm_start_date,res_instreq.START_DATE),
4541            nvl(resreq.firm_end_date,res_instreq.END_DATE),
4542            SUBST_ADD, -- jguo SUBST_CHANGE,
4543            LT_RESOURCE_INSTANCE,
4544            2,
4545            1,
4546            s.sr_instance_id,
4547            resreq.operation_sequence_id,
4548            resreq.firm_flag,
4549            res_instreq.resource_instance_hours,
4550            resreq.department_id,
4551            dep_res_inst.serial_number,
4552            resreq.group_sequence_id,
4553            resreq.group_sequence_number,
4554            res_instreq.batch_number, ---- sbala res_instreq.res_inst_batch_id
4555            resreq.orig_resource_seq_num
4556 	  , resreq.resource_seq_num
4557 	  , resreq.parent_seq_num
4558 	  , 3
4559 	  , resreq.resource_id
4560 	  , 1
4561     FROM
4562            msc_trading_partners   tp,
4563            msc_resource_requirements resreq,
4564            msc_resource_instance_reqs res_instreq, -- changed from design doc
4565            msc_dept_res_instances dep_res_inst,
4566            msc_supplies            s,
4567            msc_apps_instances ins,
4568            msc_parameters param
4569     WHERE
4570          tp.sr_tp_id=s.organization_id
4571  AND     tp.sr_instance_id= s.sr_instance_id
4572  AND     tp.partner_type=3
4573  AND     resreq.sr_instance_id= s.sr_instance_id
4574  AND     resreq.organization_id= s.organization_id
4575  AND     resreq.supply_id = s.transaction_id
4576  AND     resreq.plan_id   = s.plan_id
4577 --    AND     resreq.transaction_id = res_instreq.res_inst_transaction_id
4578  AND     resreq.sr_instance_id = res_instreq.sr_instance_id
4579  AND     resreq.plan_id = res_instreq.plan_id
4580  AND     resreq.resource_seq_num = res_instreq.resource_seq_num
4581  AND     resreq.operation_seq_num = res_instreq.operation_seq_num
4582  AND     resreq.resource_id = res_instreq.resource_id
4583  AND     resreq.supply_id = res_instreq.supply_id
4584  AND     resreq.parent_id = res_instreq.parent_id  --rawasthi
4585  AND     resreq.start_date = res_instreq.start_date
4586  AND     resreq.parent_id   = 2
4587  AND     resreq.resource_id <> -1
4588  AND     resreq.department_id <> -1
4589 --    AND     res_instreq.res_instance_id = dep_res_inst.dept_resource_inst_id
4590  AND    res_instreq.plan_id = dep_res_inst.plan_id
4591  AND    res_instreq.sr_instance_id = dep_res_inst.sr_instance_id
4592  AND    res_instreq.department_id = dep_res_inst.department_id
4593  AND    res_instreq.resource_id = dep_res_inst.resource_id
4594 /* anuj: serail number and resource_instance id joins */
4595  AND    res_instreq.serial_number = dep_res_inst.serial_number
4596  AND    res_instreq.res_instance_id = dep_res_inst.res_instance_id
4597  AND    s.transaction_id= lv_transaction_id(j)
4598  AND    s.sr_instance_id= lv_instance_id(j)
4599  AND    s.plan_id= arg_plan_id
4600  AND    lv_details_enabled(j)= 1
4601  AND    lv_agg_details(j) = 1
4602  AND    ins.instance_id = lv_instance_id(j)
4603  AND    nvl(ins.lbj_details,2) = 1
4604  AND    param.organization_id = s.organization_id
4605  AND    param.sr_instance_id = s.sr_instance_id
4606  AND    param.network_scheduling_method = 1
4607 	;
4608 
4609 
4610   print_debug_info( 'Operation Resource Instances: rows inserted into MSC_WIP_JOB_DTLS_INTERFACE = '
4611   						|| SQL%ROWCOUNT
4612 						);
4613 
4614    -- dsr: RESOURCE INSTANCE USAGES
4615 
4616 FORALL j IN 1..lv_job_count
4617     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
4618           ( last_update_date,
4619             last_updated_by,
4620             last_update_login,
4621             creation_date,
4622             created_by,
4623             organization_type,
4624             organization_id,
4625             group_id,
4626             parent_header_id,
4627             operation_seq_num,
4628            -- resource_seq_num, rawasthi
4629             RESOURCE_INSTANCE_ID,
4630             start_date,
4631             completion_date,
4632             SUBSTITUTION_TYPE,
4633             LOAD_TYPE,
4634             process_phase,
4635             process_status,
4636             SR_INSTANCE_ID,
4637             operation_seq_id,
4638             FIRM_FLAG,
4639             resource_hours,
4640             department_id,
4641             serial_number,
4642             resource_seq_num,
4643             schedule_seq_num,
4644             parent_seq_num,
4645             cfm_routing_flag,
4646             resource_id_new,
4647             assigned_units
4648      )
4649     SELECT
4650             SYSDATE,
4651             arg_user_id,
4652             s.last_update_login,
4653             SYSDATE,
4654             arg_user_id,
4655             tp.organization_type,
4656             s.organization_id,
4657             arg_wip_group_id,
4658             s.transaction_id,
4659             resreq.OPERATION_SEQ_NUM,
4660          -- resreq.RESOURCE_SEQ_NUM,
4661             dep_res_inst.RES_INSTANCE_ID,
4662             nvl(resreq.firm_start_date,res_instreq.START_DATE),
4663             nvl(resreq.firm_end_date,res_instreq.END_DATE),
4664             SUBST_ADD, -- jguo SUBST_CHANGE,
4665             LT_RESOURCE_INST_USAGE,
4666             2,
4667             1,
4668             s.sr_instance_id,
4669             resreq.operation_sequence_id,
4670             resreq.firm_flag,
4671             res_instreq.resource_instance_hours,
4672             resreq.department_id,
4673             dep_res_inst.serial_number,
4674             resreq.orig_resource_seq_num
4675 	  , resreq.resource_seq_num
4676 	  , resreq.parent_seq_num
4677 	  , 3
4678 	  , resreq.resource_id
4679 	  , 1
4680      FROM
4681             msc_trading_partners   tp,
4682             msc_resource_requirements resreq,
4683             msc_resource_instance_reqs res_instreq,
4684             msc_dept_res_instances dep_res_inst,
4685             msc_supplies            s,
4686             msc_apps_instances ins,
4687   	    msc_parameters param
4688     WHERE
4689             tp.sr_tp_id=s.organization_id
4690     AND     tp.sr_instance_id= s.sr_instance_id
4691     AND     tp.partner_type=3
4692     AND     resreq.sr_instance_id= s.sr_instance_id
4693     AND     resreq.organization_id= s.organization_id
4694     AND     resreq.supply_id = s.transaction_id
4695     AND     resreq.plan_id   = s.plan_id
4696 --    AND     resreq.transaction_id = res_instreq.res_inst_transaction_id
4697     AND     resreq.sr_instance_id = res_instreq.sr_instance_id
4698     AND     resreq.plan_id = res_instreq.plan_id
4699     AND     resreq.resource_seq_num = res_instreq.resource_seq_num
4700     AND     resreq.operation_seq_num = res_instreq.operation_seq_num
4701     AND     resreq.resource_id = res_instreq.resource_id
4702     AND     resreq.supply_id = res_instreq.supply_id
4703     AND     resreq.parent_id = res_instreq.parent_id  --rawasthi
4704     AND     resreq.start_date = res_instreq.start_date
4705     AND     resreq.parent_id   = 1
4706     AND     resreq.resource_id <> -1
4707     AND     resreq.department_id <> -1
4708 --    AND     res_instreq.res_instance_id = dep_res_inst.dept_resource_inst_id
4709     AND     res_instreq.department_id = dep_res_inst.department_id
4710     AND     res_instreq.resource_id = dep_res_inst.resource_id
4711     AND     res_instreq.plan_id = dep_res_inst.plan_id
4712     AND     res_instreq.sr_instance_id = dep_res_inst.sr_instance_id
4713 /* anuj: serail number and resource_instance id joins */
4714     AND     res_instreq.serial_number = dep_res_inst.serial_number
4715     AND     res_instreq.res_instance_id = dep_res_inst.res_instance_id
4716     AND     s.transaction_id= lv_transaction_id(j)
4717     AND     s.sr_instance_id= lv_instance_id(j)
4718     AND     s.plan_id= arg_plan_id
4719     AND     lv_details_enabled(j)= 1
4720     AND     lv_agg_details(j) = 1
4721     AND     ins.instance_id = lv_instance_id(j)
4722     AND     nvl(ins.lbj_details,2) = 1
4723     AND     param.organization_id = s.organization_id
4724     AND     param.sr_instance_id = s.sr_instance_id
4725     AND     param.network_scheduling_method = 1
4726 	;
4727 
4728   print_debug_info( 'Resource Instance Usage: rows inserted into MSC_WIP_JOB_DTLS_INTERFACE = '
4729   						|| SQL%ROWCOUNT
4730 						);
4731 
4732 	-- dsr end
4733     FORALL j IN 1..lv_job_count
4734         update msc_supplies
4735            set releasable = RELEASE_ATTEMPTED
4736                ,batch_id   = nvl(batch_id,g_batch_id)
4737          where plan_id = arg_plan_id
4738            and transaction_id= lv_transaction_id(j);
4739 
4740     return lv_loaded_jobs;
4741 
4742 END load_osfm_lot_jobs;
4743 
4744 
4745 
4746 FUNCTION reschedule_osfm_lot_jobs
4747 ( arg_plan_id			IN      NUMBER
4748 , arg_log_org_id 		IN 	NUMBER
4749 , arg_org_instance              IN      NUMBER
4750 , arg_owning_org_id 		IN 	NUMBER
4751 , arg_owning_instance           IN      NUMBER
4752 , arg_user_id 			IN 	NUMBER
4753 , arg_wip_group_id 		IN 	NUMBER
4754 , arg_mode                      IN      VARCHAR2
4755 , arg_transaction_id            IN      NUMBER
4756 )RETURN NUMBER
4757 IS
4758 
4759 TYPE NumTab  IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
4760 
4761    lv_resched_jobs    NUMBER;
4762    lv_transaction_id  NumTab;
4763    lv_instance_id     NumTab;
4764    lv_details_enabled NumTab;
4765    lv_org_id	      NumTab;
4766    lv_plan_id         NumTab;
4767    lv_agg_details     NumTab;
4768 
4769 BEGIN
4770 
4771 	/* Details will not be released
4772 		for Non - Daily Bucketed Plans
4773 		if the implement quantity or date is different then the planned quantity date
4774 		if the Lot-based job uses aggregate resources
4775 		if the job has faulty network*/
4776 
4777 	l_sql_stmt :=  ' SELECT s.transaction_id,
4778           s.sr_instance_id,
4779           decode( mp.daily_material_constraints+ mp.daily_resource_constraints+
4780                   mp.weekly_material_constraints+ mp.weekly_resource_constraints+
4781                   mp.period_material_constraints+ mp.period_resource_constraints,12,
4782                2,
4783               DECODE(nvl(s.wsm_faulty_network,2),2,Decode(mpb.bucket_type,1,DECODE( s.implement_quantity,
4784                   s.new_order_quantity, DECODE( s.implement_date,
4785                                                 s.new_schedule_date, 1,
4786                                                 2),
4787                   2),2),2)),
4788            s.organization_id,
4789            s.plan_id
4790      FROM msc_plans mp,
4791           msc_supplies s,
4792           msc_plan_organizations_v orgs,
4793           msc_plan_buckets mpb
4794     WHERE mp.plan_id = :arg_plan_id
4795     AND   s.release_errors is NULL
4796     AND   s.implement_quantity > 0
4797     AND   s.organization_id = orgs.planned_organization
4798     AND   s.sr_instance_id = orgs.sr_instance_id
4799     AND   s.plan_id = :arg_plan_id
4800     AND   orgs.plan_id = :arg_plan_id
4801     AND   orgs.organization_id = :arg_owning_org_id
4802     AND   orgs.owning_sr_instance = :arg_owning_instance
4803     AND   orgs.sr_instance_id = :arg_org_instance
4804     and  (s.releasable = ' || RELEASABLE || ' or s.releasable is null )';
4805 
4806     IF  v_batch_id_populated = 2 THEN
4807         l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id is NULL ';
4808     ELSE
4809         l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id = ' || g_batch_id || ' ' ;
4810     END IF;
4811 
4812     IF arg_log_org_id <> arg_owning_org_id THEN
4813             l_sql_stmt :=  l_sql_stmt ||'      AND     orgs.planned_organization = ' || arg_log_org_id || ' ' ;
4814     END IF;
4815 
4816     IF arg_mode IS NULL THEN
4817         l_sql_stmt :=  l_sql_stmt ||'  AND s.load_type = 6 ';
4818         IF v_msc_released_only_by_user = 1 THEN
4819             l_sql_stmt :=  l_sql_stmt || ' AND s.last_updated_by = ' || v_user_id || ' ' ;
4820         END IF;
4821     ELSIF arg_mode = 'WF_BATCH' THEN
4822         l_sql_stmt :=  l_sql_stmt ||'  AND s.load_type = 6 ';
4823     ELSIF arg_mode = 'WF' THEN
4824         l_sql_stmt :=  l_sql_stmt ||'      AND s.transaction_id = ' || arg_transaction_id  || '  ' ;
4825     END IF;
4826 
4827     l_sql_stmt :=  l_sql_stmt || ' and   s.plan_id = mpb.plan_id
4828     AND  (s.new_schedule_date between mpb.bkt_start_date and mpb.bkt_end_date) ';
4829 
4830     lv_resched_jobs:= 0;
4831 
4832     IF arg_mode IS NULL OR arg_mode = 'WF_BATCH' OR arg_mode = 'WF' THEN
4833         EXECUTE IMMEDIATE l_sql_stmt
4834                BULK COLLECT      INTO lv_transaction_id,
4835                                       lv_instance_id,
4836                                       lv_details_enabled,
4837                                       lv_org_id,
4838                                       lv_plan_id
4839                                      USING  arg_plan_id
4840                                             ,arg_plan_id
4841                                             ,arg_plan_id
4842                                             ,arg_owning_org_id
4843                                             ,arg_owning_instance
4844                                             ,arg_org_instance ;
4845         lv_resched_jobs:= SQL%ROWCOUNT;
4846     END IF;
4847 
4848 
4849 
4850 
4851     FOR k in 1..lv_resched_jobs
4852        Loop
4853                 Begin
4854                   SELECT 2
4855                   Into lv_agg_details(k)
4856                   FROM msc_department_resources deptres,
4857                        msc_resource_requirements resreq
4858                  WHERE resreq.sr_instance_id= lv_instance_id(k)
4859                    AND resreq.supply_id = lv_transaction_id(k)
4860                    AND resreq.organization_id= lv_org_id(k)
4861                    AND resreq.plan_id   = lv_plan_id(k)
4862                    AND resreq.parent_id   = 2
4863                    AND deptres.plan_id  = resreq.plan_id
4864                    AND deptres.sr_instance_id= resreq.sr_instance_id
4865                    AND deptres.resource_id= resreq.resource_id
4866                    AND deptres.department_id= resreq.department_id
4867                    AND deptres.organization_id= resreq.organization_id
4868                    AND deptres.aggregate_resource_flag= 1
4869                    AND rownum=1;
4870                   Exception
4871                   When no_data_found
4872                   then
4873                   lv_agg_details(k) := 1;
4874                   End;
4875 
4876        End Loop;
4877 
4878     -- ------------------------------------------------------------------------
4879     -- Perform the lot based job reschedule
4880     -- ------------------------------------------------------------------------
4881     FORALL j in 1..lv_resched_jobs
4882     INSERT INTO msc_wip_job_schedule_interface
4883             (last_update_date,
4884             last_updated_by,
4885             cfm_routing_flag,
4886             last_update_login,
4887             creation_date,
4888             created_by,
4889             group_id,
4890             source_code,
4891             organization_id,
4892             organization_type,
4893             status_type,
4894             load_type,
4895             first_unit_start_date,
4896             last_unit_completion_date,
4897             bom_revision_date,
4898             routing_revision_date,
4899             job_name,
4900             firm_planned_flag,
4901             start_quantity,   /* bug 1229891: net_quantity */
4902             net_quantity,
4903             wip_entity_id,
4904             demand_class,
4905             project_id,
4906             task_id,
4907 	    schedule_group_id,
4908 	    build_sequence,
4909             line_id,
4910             alternate_bom_designator,
4911 	    alternate_routing_designator,
4912 	    end_item_unit_number,
4913             process_phase,
4914 	    process_status,
4915             BILL_RTG_EXPLOSION_FLAG,
4916             HEADER_ID,
4917             uom_code, --Outbound Changes for XML
4918             SR_INSTANCE_ID,
4919             PRIMARY_ITEM_ID,
4920             source_line_id --Outbound Changes for XML
4921 	   -- dsr: added 2 new columns
4922 	   , schedule_priority
4923 	   , requested_completion_date
4924 	  )
4925     SELECT  SYSDATE,
4926             arg_user_id,
4927             s.cfm_routing_flag,
4928             s.last_update_login,
4929             SYSDATE,
4930             arg_user_id,
4931             arg_wip_group_id,
4932             'MSC',
4933             msi.organization_id,
4934             tp.organization_type,
4935             DECODE(NVL(s.implement_status_code, s.wip_status_code),
4936                    JOB_CANCELLED,JOB_CANCELLED,NULL),
4937             6,
4938             decode(lv_details_enabled(j),1,new_wip_start_date,null),
4939             s.implement_date,
4940             NULL,
4941             NULL,
4942             s.implement_job_name,
4943             s.implement_firm,
4944             DECODE( tp.organization_type,
4945                     1, DECODE(s.new_order_quantity,
4946                               s.implement_quantity, TO_NUMBER(NULL),
4947                         ((s.new_order_quantity + NVL(s.qty_completed, 0) +
4948                           NVL(s.qty_scrapped, 0)) -
4949                          (s.new_order_quantity - s.implement_quantity))),
4950                     NULL),
4951             DECODE( tp.organization_type,
4952                     2, DECODE(s.new_order_quantity,
4953                               s.implement_quantity, TO_NUMBER(NULL),
4954                         ((s.new_order_quantity + NVL(s.qty_completed, 0) +
4955                           NVL(s.qty_scrapped, 0)) -
4956                          (s.new_order_quantity - s.implement_quantity))),
4957                     s.implement_quantity),
4958             s.disposition_id,
4959             s.implement_demand_class,
4960             s.implement_project_id,
4961             s.implement_task_id,
4962 	    s.implement_schedule_group_id,
4963             s.implement_build_sequence,
4964             s.implement_line_id,
4965        	    s.implement_alternate_bom,
4966 	    s.implement_alternate_routing,
4967 	    s.implement_unit_number,
4968             2,
4969 	    1,
4970             'Y',
4971             s.transaction_id,
4972             nvl(s.implement_uom_code,msi.uom_code),
4973             s.sr_instance_id,
4974             msi.sr_inventory_item_id,
4975             s.transaction_id --Outbound Changes for XML
4976             -- dsr: added 2 new columns
4977 	   , s.schedule_priority
4978 	   , s.requested_completion_date
4979     FROM    msc_trading_partners tp,
4980             msc_parameters param,
4981             msc_system_items msi,
4982             msc_supplies     s,
4983             msc_plan_organizations_v orgs
4984     WHERE   tp.sr_tp_id= msi.organization_id
4985     AND     tp.sr_instance_id= msi.sr_instance_id
4986     AND     tp.partner_type=3
4987     AND     param.organization_id = msi.organization_id
4988     AND    param.sr_instance_id = msi.sr_instance_id
4989     AND    msi.inventory_item_id = s.inventory_item_id
4990     AND    msi.plan_id = s.plan_id
4991     AND    msi.organization_id = s.organization_id
4992     AND    msi.sr_instance_id  = s.sr_instance_id
4993     AND    s.release_errors is NULL
4994     AND    s.organization_id = orgs.planned_organization
4995     AND    s.sr_instance_id = orgs.sr_instance_id
4996     AND    s.plan_id = orgs.plan_id
4997     AND    orgs.organization_id = arg_owning_org_id
4998     AND    orgs.owning_sr_instance = arg_owning_instance
4999     AND    orgs.plan_id = arg_plan_id
5000     AND    orgs.planned_organization = decode(arg_log_org_id,
5001                                          arg_owning_org_id, orgs.planned_organization,
5002                                           arg_log_org_id)
5003     AND    orgs.sr_instance_id = arg_org_instance
5004     AND     ((arg_mode is null and s.load_type = 6 AND
5005               s.last_updated_by = decode(v_msc_released_only_by_user,1,v_user_id,s.last_updated_by)) or
5006                 (arg_mode = 'WF' and s.transaction_id = arg_transaction_id))
5007     AND    nvl(s.cfm_routing_flag,0) = 3
5008     AND    s.transaction_id = lv_transaction_id(j)
5009     AND    s.sr_instance_id  = lv_instance_id(j)
5010     AND    s.plan_id = lv_plan_id(j);
5011 
5012 
5013 
5014     -- ------------------------------------------------------------------------
5015     -- Perform the lot-based job mass load for the details
5016     -- -----------------------------------------------------------------------
5017 
5018     /* lot-based job details are released only when the source profile WSM: Create Lot Based Job Routing is Yes
5019     and org planning parameter is primary */
5020 
5021 
5022     /* OPERATION NETWORKS */
5023 
5024 
5025     FORALL j IN 1..lv_resched_jobs
5026     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
5027     (last_update_date,
5028      last_updated_by,
5029      last_update_login,
5030      creation_date,
5031      created_by,
5032      group_id,
5033      parent_header_id,
5034      SUBSTITUTION_TYPE,
5035      LOAD_TYPE,
5036      process_phase,
5037      process_status,
5038      job_op_seq_num,
5039      operation_seq_num,
5040      next_routing_op_seq_num,
5041      cfm_routing_flag,
5042      SR_INSTANCE_ID)
5043   (SELECT  SYSDATE,
5044             arg_user_id,
5045             s.last_update_login,
5046             SYSDATE,
5047             arg_user_id,
5048             arg_wip_group_id,
5049             s.transaction_id,
5050             4,
5051             5,
5052             1,
5053             1,
5054             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),
5055             decode(nwk.from_op_seq_num, 50000 ,null,nwk.from_op_seq_num),
5056             nwk.to_op_seq_num,
5057             3,
5058             s.sr_instance_id
5059    From msc_supplies s,
5060    msc_job_operation_networks nwk,
5061    msc_apps_instances ins,
5062    msc_parameters param
5063    Where    nwk.plan_id = s.plan_id
5064     AND     nwk.sr_instance_id = s.sr_instance_id
5065     AND     nwk.transaction_id = s.transaction_id
5066     AND     nwk.recommended = 'Y'
5067     AND     s.transaction_id= lv_transaction_id(j)
5068     AND     s.sr_instance_id= lv_instance_id(j)
5069     AND     s.plan_id= arg_plan_id
5070     AND     lv_details_enabled(j)= 1
5071     AND     lv_agg_details(j) = 1
5072     AND     ins.instance_id = lv_instance_id(j)
5073     AND     nvl(ins.lbj_details,2) = 1
5074     AND     param.organization_id = s.organization_id
5075     AND     param.sr_instance_id = s.sr_instance_id
5076     AND     param.network_scheduling_method = 1);
5077 
5078 
5079     /* Operations */
5080 
5081 FORALL j IN 1..lv_resched_jobs
5082     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
5083     (last_update_date,
5084      last_updated_by,
5085      last_update_login,
5086      creation_date,
5087      created_by,
5088      group_id,
5089      parent_header_id,
5090      SUBSTITUTION_TYPE,
5091      LOAD_TYPE,
5092      process_phase,
5093      process_status,
5094      job_op_seq_num,
5095      OPERATION_SEQ_NUM,
5096      first_unit_start_date,
5097      last_unit_completion_date,
5098      cfm_routing_flag,
5099      SR_INSTANCE_ID,
5100      scheduled_quantity)
5101   (SELECT  SYSDATE,
5102             arg_user_id,
5103             s.last_update_login,
5104             SYSDATE,
5105             arg_user_id,
5106             arg_wip_group_id,
5107             s.transaction_id,
5108             4,
5109             3,
5110             1,
5111             1,
5112             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),
5113             decode(res.operation_seq_num, 50000,null,res.operation_seq_num),
5114             min(res.START_DATE),
5115             max(res.END_DATE),
5116             3,
5117             s.sr_instance_id,
5118             max(nvl(res.CUMMULATIVE_QUANTITY,0))
5119    From msc_supplies s,
5120    msc_resource_requirements res,
5121    msc_apps_instances ins,
5122    msc_parameters param
5123    Where    res.plan_id = s.plan_id
5124     AND     res.sr_instance_id = s.sr_instance_id
5125     AND     s.transaction_id = res.supply_id
5126     AND     res.parent_id = 2
5127     -- AND     res.resource_id <> -1   --Bug#3432607
5128     -- AND     res.department_id <> -1
5129     AND     s.transaction_id= lv_transaction_id(j)
5130     AND     s.sr_instance_id= lv_instance_id(j)
5131     AND     s.plan_id= arg_plan_id
5132     AND     lv_details_enabled(j)= 1
5133     AND     lv_agg_details(j) = 1
5134     AND     ins.instance_id = lv_instance_id(j)
5135     AND     nvl(ins.lbj_details,2) = 1
5136     AND     param.organization_id = s.organization_id
5137     AND     param.sr_instance_id = s.sr_instance_id
5138     AND     param.network_scheduling_method = 1
5139     GROUP BY
5140             s.last_update_login,
5141             s.transaction_id,
5142             res.OPERATION_SEQ_NUM,
5143             s.sr_instance_id,
5144            -- res.CUMMULATIVE_QUANTITY,
5145             s.OPERATION_SEQ_NUM,
5146             s.JUMP_OP_SEQ_NUM,
5147             s.JOB_OP_SEQ_NUM);
5148 
5149     /* Resources */
5150 FORALL j IN 1..lv_resched_jobs
5151     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
5152     (last_update_date,
5153      last_updated_by,
5154      last_update_login,
5155      creation_date,
5156      created_by,
5157      group_id,
5158      parent_header_id,
5159      SUBSTITUTION_TYPE,
5160      LOAD_TYPE,
5161      process_phase,
5162      process_status,
5163      job_op_seq_num,
5164      operation_seq_num,
5165      -- dsr resource_seq_num,
5166      resource_id_new,
5167      start_date,
5168      completion_date,
5169      alternate_num,
5170      cfm_routing_flag,
5171      SR_INSTANCE_ID
5172     -- dsr: add following columns
5173     , firm_flag
5174     , setup_id
5175     , group_sequence_id
5176     , group_sequence_number
5177     , batch_id
5178    , maximum_assigned_units
5179    , parent_seq_num
5180    , resource_seq_num
5181    , schedule_seq_num
5182    , assigned_units
5183    , usage_rate_or_amount
5184    , scheduled_flag
5185 )
5186   (SELECT  SYSDATE,
5187             arg_user_id,
5188             s.last_update_login,
5189             SYSDATE,
5190             arg_user_id,
5191             arg_wip_group_id,
5192             s.transaction_id,
5193             decode(res.parent_seq_num, null,4,2),
5194             1,
5195             1,
5196             1,
5197             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),
5198             decode(res.operation_seq_num, 50000,null,res.operation_seq_num),
5199             -- res.resource_seq_num,
5200             res.resource_id,
5201             nvl(res.firm_start_date,res.START_DATE),
5202             nvl(res.firm_end_date,res.END_DATE),
5203             nvl(res.alternate_num,0),
5204             3,
5205             s.sr_instance_id
5206             -- dsr: add following columns
5207             , res.firm_flag
5208 	    , res.setup_id
5209 	    , res.group_sequence_id
5210 	    , res.group_sequence_number
5211             , res.batch_number
5212 	    , res.maximum_assigned_units
5213 	    , res.parent_seq_num
5214 	    , res.orig_resource_seq_num
5215 	    , res.resource_seq_num
5216 	    , res.assigned_units
5217 	    , decode(res.parent_seq_num, null, (res.RESOURCE_HOURS/decode(res.basis_type,2,1,
5218                     nvl(res.cummulative_quantity,
5219                              decode(msi.rounding_control_type,1,
5220                ROUND(s.new_order_quantity/nvl(res.REVERSE_CUMULATIVE_YIELD,1) ,6),
5221                     (s.new_order_quantity/nvl(res.REVERSE_CUMULATIVE_YIELD,1))
5222                             ) ) ))* decode(mdr.efficiency,NULL,100,0,100,mdr.efficiency)/100 * decode(mdr.utilization,NULL,100,0,100,mdr.utilization)/100, res.RESOURCE_HOURS)
5223             ,  decode(nvl(res.schedule_flag,1),-23453,1,1,1,res.schedule_flag)
5224    From msc_supplies s,
5225    msc_resource_requirements res,
5226    msc_apps_instances ins,
5227    msc_parameters param,
5228    msc_department_resources mdr,
5229    msc_system_items msi
5230    Where    res.plan_id = s.plan_id
5231     AND     res.sr_instance_id = s.sr_instance_id
5232     AND     s.transaction_id = res.supply_id
5233     AND     res.parent_id = 2
5234     AND     res.resource_id <> -1
5235     AND     res.department_id <> -1
5236     AND     s.transaction_id= lv_transaction_id(j)
5237     AND     s.sr_instance_id= lv_instance_id(j)
5238     AND     s.plan_id= arg_plan_id
5239     AND     lv_details_enabled(j)= 1
5240     AND     lv_agg_details(j) = 1
5241     AND     ins.instance_id = lv_instance_id(j)
5242     AND     nvl(ins.lbj_details,2) = 1
5243     AND     param.organization_id = s.organization_id
5244     AND     param.sr_instance_id = s.sr_instance_id
5245     AND     res.plan_id = mdr.plan_id
5246     AND     res.organization_id =mdr.organization_id
5247     AND     res.sr_instance_id = mdr.sr_instance_id
5248     AND     res.resource_id = mdr.resource_id
5249     AND     res.department_id=mdr.department_id
5250     AND     msi.inventory_item_id = s.inventory_item_id
5251     AND     msi.plan_id = s.plan_id
5252     AND     msi.organization_id = s.organization_id
5253     AND     msi.sr_instance_id = s.sr_instance_id
5254     AND     param.network_scheduling_method = 1);
5255 
5256     /*Components*/
5257 
5258     FORALL j IN 1..lv_resched_jobs
5259     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
5260     (last_update_date,
5261      last_updated_by,
5262      last_update_login,
5263      creation_date,
5264      created_by,
5265      group_id,
5266      parent_header_id,
5267      SUBSTITUTION_TYPE,
5268      LOAD_TYPE,
5269      process_phase,
5270      process_status,
5271      job_op_seq_num,
5272      operation_seq_num,
5273      inventory_item_id_new,
5274      primary_component_id,
5275      source_phantom_id,
5276      component_seq_id,
5277      mrp_net_flag,
5278      date_required,
5279      mps_date_required,
5280      basis_type,
5281      quantity_per_assembly,
5282      required_quantity,
5283      mps_required_quantity,
5284      cfm_routing_flag,
5285      SR_INSTANCE_ID)
5286   (SELECT  SYSDATE,
5287             arg_user_id,
5288             s.last_update_login,
5289             SYSDATE,
5290             arg_user_id,
5291             arg_wip_group_id,
5292             s.transaction_id,
5293             4,
5294             2,
5295             1,
5296             1,
5297             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),
5298             decode(md.op_seq_num, 50000,null,md.op_seq_num),
5299             icomp.sr_inventory_item_id,
5300             icomp1.sr_inventory_item_id,
5301             icomp2.sr_inventory_item_id,
5302             md.COMP_SEQ_ID,
5303             1,
5304             md.USING_ASSEMBLY_DEMAND_DATE,
5305             md.USING_ASSEMBLY_DEMAND_DATE,
5306             decode(md.component_scaling_type,1,NULL,md.component_scaling_type),
5307             md.quantity_per_assembly,
5308             md.USING_REQUIREMENT_QUANTITY,
5309             md.USING_REQUIREMENT_QUANTITY,
5310             3,
5311             s.sr_instance_id
5312    From msc_supplies s,
5313    msc_demands md,
5314    msc_system_items icomp,
5315    msc_system_items icomp1,
5316    msc_system_items icomp2,
5317    msc_apps_instances ins,
5318    msc_parameters param
5319    Where   /* not exists (select 'x'
5320                         from msc_exception_details excp
5321                         where excp.plan_id = s.plan_id
5322                         and excp.number1 = s.transaction_id
5323                         and excp.inventory_item_id = s.inventory_item_id
5324                         and excp.organization_id = s.organization_id
5325                         and excp.sr_instance_id = s.sr_instance_id
5326                         and excp.exception_type = 33
5327                         and excp.number2 = md.inventory_item_id)*/  /* not needed as inv_old need not be populated*/
5328     	    icomp.inventory_item_id= md.inventory_item_id
5329     AND     icomp.organization_id= md.organization_id
5330     AND     icomp.sr_instance_id= md.sr_instance_id
5331     AND     icomp.plan_id= md.plan_id
5332     AND     nvl(icomp.wip_supply_type,0) <> 6
5333     AND     icomp1.inventory_item_id= md.primary_component_id
5334     AND     icomp1.organization_id= md.organization_id
5335     AND     icomp1.sr_instance_id= md.sr_instance_id
5336     AND     icomp1.plan_id= md.plan_id
5337     AND     icomp2.inventory_item_id(+)= md.source_phantom_id
5338     AND     icomp2.organization_id(+)= md.organization_id
5339     AND     icomp2.sr_instance_id(+)= md.sr_instance_id
5340     AND     icomp2.plan_id(+)= md.plan_id
5341     AND     md.plan_id = s.plan_id
5342     AND     md.sr_instance_id = s.sr_instance_id
5343     AND     md.disposition_id= s.transaction_id
5344     AND     md.origination_type = 1
5345     AND     s.transaction_id= lv_transaction_id(j)
5346     AND     s.sr_instance_id= lv_instance_id(j)
5347     AND     s.plan_id= arg_plan_id
5348     AND     lv_details_enabled(j)= 1
5349     AND     lv_agg_details(j) = 1
5350     AND     ins.instance_id = lv_instance_id(j)
5351     AND     nvl(ins.lbj_details,2) = 1
5352     AND     param.organization_id = s.organization_id
5353     AND     param.sr_instance_id = s.sr_instance_id
5354     AND     param.network_scheduling_method = 1);
5355 
5356 
5357  /* Resource Usage */
5358  FORALL j IN 1..lv_resched_jobs
5359     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
5360     (last_update_date,
5361      last_updated_by,
5362      last_update_login,
5363      creation_date,
5364      created_by,
5365      group_id,
5366      parent_header_id,
5367      SUBSTITUTION_TYPE,
5368      LOAD_TYPE,
5369      process_phase,
5370      process_status,
5371      job_op_seq_num,
5372      operation_seq_num,
5373      -- dsr resource_seq_num,
5374      resource_id_new,
5375      assigned_units,
5376      alternate_num,
5377      start_date,
5378      completion_date,
5379      cfm_routing_flag,
5380      SR_INSTANCE_ID
5381      , resource_seq_num -- dsr
5382      , schedule_seq_num
5383      , parent_seq_num
5384     )
5385   (SELECT  SYSDATE,
5386             arg_user_id,
5387             s.last_update_login,
5388             SYSDATE,
5389             arg_user_id,
5390             arg_wip_group_id,
5391             s.transaction_id,
5392             4,
5393             4,
5394             1,
5395             1,
5396             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),
5397             decode(res.operation_seq_num, 50000,null,res.operation_seq_num),
5398            --  res.resource_seq_num,
5399             res.resource_id,
5400             res.assigned_units,
5401             nvl(res.alternate_num,0),
5402             nvl(res.firm_start_date,res.START_DATE),
5403             nvl(res.firm_end_date,res.END_DATE),
5404             3,
5405             s.sr_instance_id,
5406              -- dsr
5407 	    res.orig_resource_seq_num,
5408 	    res.resource_seq_num,
5409 	    res.parent_seq_num
5410        From msc_supplies s,
5411            msc_resource_requirements res,
5412            msc_apps_instances ins,
5413            msc_parameters param
5414    Where    res.plan_id = s.plan_id
5415     AND     res.sr_instance_id = s.sr_instance_id
5416     AND     s.transaction_id = res.supply_id
5417     AND     res.parent_id = 1
5418     AND     res.resource_id <> -1
5419     AND     res.department_id <> -1
5420     AND     s.transaction_id= lv_transaction_id(j)
5421     AND     s.sr_instance_id= lv_instance_id(j)
5422     AND     s.plan_id= arg_plan_id
5423     AND     lv_details_enabled(j)= 1
5424     AND     lv_agg_details(j) = 1
5425     AND     ins.instance_id = lv_instance_id(j)
5426     AND     nvl(ins.lbj_details,2) = 1
5427     AND     param.organization_id = s.organization_id
5428     AND     param.sr_instance_id = s.sr_instance_id
5429     AND     param.network_scheduling_method = 1);
5430 
5431     -- dsr begin: Operation Resource Instances
5432 
5433     FORALL j IN 1..lv_resched_jobs
5434     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
5435           ( last_update_date,
5436             last_updated_by,
5437             last_update_login,
5438             creation_date,
5439             created_by,
5440             organization_type,
5441             organization_id,
5442             group_id,
5443             parent_header_id,
5444             job_op_seq_num,
5445             operation_seq_num,
5446        --   resource_seq_num, dsr rawasthi
5447             RESOURCE_INSTANCE_ID,
5448             start_date,
5449             completion_date,
5450             SUBSTITUTION_TYPE,
5451             LOAD_TYPE,
5452             process_phase,
5453             process_status,
5454             SR_INSTANCE_ID,
5455             operation_seq_id,
5456             FIRM_FLAG,
5457             resource_hours,
5458             department_id,
5459 	    SERIAL_NUMBER,
5460             group_sequence_id,
5461             group_sequence_number,
5462             batch_id
5463             , resource_seq_num -- dsr
5464             , schedule_seq_num
5465             , parent_seq_num
5466             , cfm_routing_flag
5467             , resource_id_new
5468             , assigned_units
5469          )
5470     SELECT
5471             SYSDATE,
5472             arg_user_id,
5473             s.last_update_login,
5474             SYSDATE,
5475             arg_user_id,
5476             tp.organization_type,
5477             s.organization_id,
5478             arg_wip_group_id,
5479             s.transaction_id,
5480             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),
5481             decode(resreq.operation_seq_num, 50000,null,resreq.operation_seq_num),
5482          -- resreq.OPERATION_SEQ_NUM,
5483          -- resreq.RESOURCE_SEQ_NUM,
5484             dep_res_inst.RES_INSTANCE_ID,
5485             nvl(resreq.firm_start_date,res_instreq.START_DATE),
5486             nvl(resreq.firm_end_date,res_instreq.END_DATE),
5487             SUBST_ADD, -- jguo SUBST_CHANGE,
5488             LT_RESOURCE_INSTANCE,
5489             2,
5490             1,
5491             s.sr_instance_id,
5492             resreq.operation_sequence_id,
5493             resreq.firm_flag,
5494             res_instreq.resource_instance_hours,
5495             resreq.department_id,
5496             dep_res_inst.serial_number,
5497             resreq.group_sequence_id,
5498             resreq.group_sequence_number,
5499             res_instreq.batch_number,
5500 	    ---- sbala res_instreq.res_inst_batch_id
5501             resreq.orig_resource_seq_num,
5502 	    resreq.resource_seq_num,
5503 	    resreq.parent_seq_num,
5504 	    3,
5505 	    resreq.resource_id,
5506 	    1
5507       FROM  msc_trading_partners   tp,
5508             msc_resource_requirements resreq,
5509             msc_resource_instance_reqs res_instreq, -- changed from design doc
5510             msc_dept_res_instances dep_res_inst,
5511             msc_supplies            s,
5512             msc_apps_instances ins,
5513             msc_parameters param
5514     WHERE
5515             tp.sr_tp_id=s.organization_id
5516     AND     tp.sr_instance_id= s.sr_instance_id
5517     AND     tp.partner_type=3
5518     AND     resreq.sr_instance_id= s.sr_instance_id
5519     AND     resreq.organization_id= s.organization_id
5520     AND     resreq.supply_id = s.transaction_id
5521     AND     resreq.plan_id   = s.plan_id
5522 --    AND     resreq.transaction_id = res_instreq.res_inst_transaction_id
5523     AND     resreq.sr_instance_id = res_instreq.sr_instance_id
5524     AND     resreq.plan_id = res_instreq.plan_id
5525     AND     resreq.resource_seq_num = res_instreq.resource_seq_num
5526     AND     resreq.operation_seq_num = res_instreq.operation_seq_num
5527     AND     resreq.resource_id = res_instreq.resource_id
5528     AND     resreq.supply_id = res_instreq.supply_id
5529     AND     resreq.parent_id = res_instreq.parent_id  --rawasthi
5530     AND     resreq.start_date = res_instreq.start_date
5531     AND     resreq.parent_id   = 2
5532     AND     resreq.resource_id <> -1
5533     AND     resreq.department_id <> -1
5534 --    AND     res_instreq.res_instance_id = dep_res_inst.dept_resource_inst_id
5535     AND     res_instreq.plan_id = dep_res_inst.plan_id
5536     AND     res_instreq.sr_instance_id = dep_res_inst.sr_instance_id
5537     AND     res_instreq.department_id = dep_res_inst.department_id
5538     AND     res_instreq.resource_id = dep_res_inst.resource_id
5539 /* anuj: serail number and resource_instance id joins */
5540     AND     res_instreq.serial_number = dep_res_inst.serial_number
5541     AND     res_instreq.res_instance_id = dep_res_inst.res_instance_id
5542     AND     s.transaction_id= lv_transaction_id(j)
5543     AND     s.sr_instance_id= lv_instance_id(j)
5544     AND     s.plan_id= arg_plan_id
5545     AND     lv_details_enabled(j)= 1
5546     AND     lv_agg_details(j) = 1
5547     AND     ins.instance_id = lv_instance_id(j)
5548     AND     nvl(ins.lbj_details,2) = 1
5549     AND     param.organization_id = s.organization_id
5550     AND     param.sr_instance_id = s.sr_instance_id
5551     AND     param.network_scheduling_method = 1
5552 	;
5553 
5554   print_debug_info('Resource Instance: rows inserted into msc_wip_job_dtls_interface = '
5555   					|| sql%rowcount);
5556 
5557 FORALL j IN 1..lv_resched_jobs
5558     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
5559           ( last_update_date,
5560             last_updated_by,
5561             last_update_login,
5562             creation_date,
5563             created_by,
5564             organization_type,
5565             organization_id,
5566             group_id,
5567             parent_header_id,
5568             job_op_seq_num,
5569             operation_seq_num,
5570         --    resource_seq_num,
5571             RESOURCE_INSTANCE_ID,
5572             start_date,
5573             completion_date,
5574             SUBSTITUTION_TYPE,
5575             LOAD_TYPE,
5576             process_phase,
5577             process_status,
5578             SR_INSTANCE_ID,
5579             operation_seq_id,
5580             FIRM_FLAG,
5581             resource_hours,
5582             department_id,
5583             serial_number
5584             , resource_seq_num -- dsr
5585             , schedule_seq_num
5586             , parent_seq_num
5587             , cfm_routing_flag
5588             , resource_id_new
5589             , assigned_units
5590  )
5591     SELECT
5592             SYSDATE,
5593             arg_user_id,
5594             s.last_update_login,
5595             SYSDATE,
5596             arg_user_id,
5597             tp.organization_type,
5598             s.organization_id,
5599             arg_wip_group_id,
5600             s.transaction_id,
5601             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),
5602             decode(resreq.operation_seq_num, 50000,null,resreq.operation_seq_num),
5603          -- resreq.OPERATION_SEQ_NUM,
5604          -- resreq.RESOURCE_SEQ_NUM,
5605             dep_res_inst.RES_INSTANCE_ID,
5606             nvl(resreq.firm_start_date,res_instreq.START_DATE),
5607             nvl(resreq.firm_end_date,res_instreq.END_DATE),
5608             SUBST_ADD, -- jguo SUBST_CHANGE,
5609             LT_RESOURCE_INST_USAGE,
5610             2,
5611             1,
5612             s.sr_instance_id,
5613             resreq.operation_sequence_id,
5614             resreq.firm_flag,
5615             res_instreq.resource_instance_hours,
5616             resreq.department_id,
5617             dep_res_inst.serial_number,
5618             resreq.orig_resource_seq_num
5619 	    , resreq.resource_seq_num
5620 	    , resreq.parent_seq_num
5621 	    , 3
5622 	    , resreq.resource_id
5623 	    , 1
5624     FROM
5625             msc_trading_partners   tp,
5626             msc_resource_requirements resreq,
5627             msc_resource_instance_reqs res_instreq,
5628             msc_dept_res_instances dep_res_inst,
5629             msc_supplies            s,
5630             msc_apps_instances ins,
5631             msc_parameters param
5632     WHERE
5633             tp.sr_tp_id=s.organization_id
5634       AND   tp.sr_instance_id= s.sr_instance_id
5635       AND   tp.partner_type=3
5636       AND   resreq.sr_instance_id= s.sr_instance_id
5637       AND   resreq.organization_id= s.organization_id
5638       AND   resreq.supply_id = s.transaction_id
5639       AND   resreq.plan_id   = s.plan_id
5640 --    AND   resreq.transaction_id = res_instreq.res_inst_transaction_id
5641       AND   resreq.sr_instance_id = res_instreq.sr_instance_id
5642       AND   resreq.plan_id = res_instreq.plan_id
5643       AND   resreq.resource_seq_num = res_instreq.resource_seq_num
5644       AND   resreq.operation_seq_num = res_instreq.operation_seq_num
5645       AND   resreq.resource_id = res_instreq.resource_id
5646       AND   resreq.supply_id = res_instreq.supply_id
5647       AND   resreq.parent_id = res_instreq.parent_id  --rawasthi
5648       AND   resreq.start_date = res_instreq.start_date
5649       AND   resreq.parent_id   = 1
5650       AND   resreq.resource_id <> -1
5651       AND   resreq.department_id <> -1
5652 --    AND     res_instreq.res_instance_id = dep_res_inst.dept_resource_inst_id
5653       AND   res_instreq.department_id = dep_res_inst.department_id
5654       AND   res_instreq.resource_id = dep_res_inst.resource_id
5655       AND   res_instreq.plan_id = dep_res_inst.plan_id
5656       AND   res_instreq.sr_instance_id = dep_res_inst.sr_instance_id
5657 /* anuj: serail number and resource_instance id joins */
5658       AND   res_instreq.serial_number = dep_res_inst.serial_number
5659       AND   res_instreq.res_instance_id = dep_res_inst.res_instance_id
5660       AND   s.transaction_id= lv_transaction_id(j)
5661       AND   s.sr_instance_id= lv_instance_id(j)
5662       AND   s.plan_id= arg_plan_id
5663       AND   lv_details_enabled(j)= 1
5664       AND   lv_agg_details(j) = 1
5665       AND   ins.instance_id = lv_instance_id(j)
5666       AND   nvl(ins.lbj_details,2) = 1
5667       AND   param.organization_id = s.organization_id
5668       AND   param.sr_instance_id = s.sr_instance_id
5669       AND   param.network_scheduling_method = 1
5670 	;
5671 
5672   print_debug_info('Resource Instance Usage: rows inserted into msc_wip_job_dtls_interface = '
5673   					|| sql%rowcount);
5674 
5675 
5676     FORALL j IN 1..lv_resched_jobs
5677         update msc_supplies
5678            set releasable = RELEASE_ATTEMPTED
5679                ,batch_id   = nvl(batch_id,g_batch_id)
5680          where plan_id = arg_plan_id
5681            and transaction_id= lv_transaction_id(j);
5682 
5683 	-- dsr end
5684     RETURN lv_resched_jobs;
5685 
5686 END reschedule_osfm_lot_jobs;
5687 
5688 
5689 FUNCTION load_wip_discrete_jobs
5690 ( arg_plan_id			IN      NUMBER
5691 , arg_log_org_id 		IN 	NUMBER
5692 , arg_org_instance              IN      NUMBER
5693 , arg_owning_org_id 		IN 	NUMBER
5694 , arg_owning_instance           IN      NUMBER
5695 , arg_user_id 			IN 	NUMBER
5696 , arg_wip_group_id              IN      NUMBER
5697 , arg_mode                      IN      VARCHAR2
5698 , arg_transaction_id            IN      NUMBER,
5699   l_apps_ver                    IN      VARCHAR2
5700 )RETURN NUMBER
5701 IS
5702    lv_loaded_jobs NUMBER;
5703 
5704    TYPE NumTab  IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
5705  --TYPE RIDTab  IS TABLE OF ROWID  INDEX BY BINARY_INTEGER;
5706 
5707    lv_transaction_id          NumTab;
5708    lv_instance_id             NumTab;
5709    lv_details_enabled         NumTab;
5710    lv_Agg_details             NumTab;
5711    Lv_org_id                  Numtab;
5712    lv_plan_id                 NumTab;
5713    lv_job_count               NUMBER;
5714    lv_release_details         NUMBER;
5715    lv_inflate_wip             NUMBER;
5716    lv_round_primary_item      NumTab;
5717 
5718 CURSOR c_release_dtls(p_transaction_id number,
5719                       p_plan_id  number) IS
5720 SELECT COUNT(1) FROM (
5721 SELECT count(1)
5722 FROM msc_supplies
5723 WHERE implement_alternate_bom is not null
5724   and transaction_id = p_transaction_id
5725   AND plan_id = p_plan_id
5726   and rownum < 2
5727 UNION
5728 SELECT count(1)
5729 from msc_exception_details excp
5730 where excp.plan_id = p_plan_id
5731       and number1 = p_transaction_id
5732       and excp.exception_type = 33
5733       and rownum < 2);
5734 
5735 
5736 CURSOR c_plan_type(p_plan_id number) IS
5737 select plan_type
5738 from msc_plans a
5739 where
5740 plan_id = p_plan_id;
5741 
5742 l_sub_comp_count NUMBER;
5743 l_count   NUMBER := 0;
5744 l_plan_type  number := 0;
5745 
5746 BEGIN
5747 
5748    SELECT decode(nvl(FND_PROFILE.value('MSC_RELEASE_DTLS_REVDATE'),'Y'),'N',2,1),
5749    DECODE(NVL(fnd_profile.value('MSC_INFLATE_WIP') ,'N'), 'N',2 ,1)
5750    INTO lv_release_details,lv_inflate_wip
5751    FROM dual;
5752 
5753   /* we release the discrete job, only if it doesn't use aggregate resources */
5754   /* bug 1252659 fix, replace rowid by
5755          (transaction_id,sr_instance_id,plan_id) */
5756 
5757   /* Details will NOT be released for
5758      a. Unconstrained Plan
5759      b. if the new_wip_start_date is null
5760      c. if the implement quantity or date is different then the planned quantity date
5761      d. if the revision date is different then the new_wip_start_date
5762         and the profile option setting : MSC_RELEASE_DTLS_REVDATE  = 'N'
5763      e. Non - Daily Bucketed Plans
5764      f.bug 4655420-Alternate BOM/Routing is changed during release.
5765   */
5766 
5767     -- dsting get plan type
5768     OPEN c_plan_type(arg_plan_id);
5769     FETCH c_plan_type INTO l_plan_type;
5770     CLOSE c_plan_type;
5771 
5772    l_sql_stmt := ' SELECT s.transaction_id,
5773           s.sr_instance_id,
5774            Decode(' ||l_plan_type ||', 5, -- dsting for drp look at alternate bom/subst exception to determine whether or not to release dtls
5775                   Decode(implement_alternate_bom, NULL,
5776                          (SELECT Decode(COUNT(1), 1, 1, 2)
5777                             FROM msc_exception_details e
5778                            WHERE e.plan_id = :arg_plan_id
5779                              AND e.number1 = s.transaction_id
5780                              AND e.exception_type = 33), 1),
5781                   decode( mp.daily_material_constraints+ mp.daily_resource_constraints+
5782                   mp.weekly_material_constraints+ mp.weekly_resource_constraints+
5783                   mp.period_material_constraints+ mp.period_resource_constraints,12,2,
5784                decode(mpb.bucket_type,1,
5785                DECODE( s.implement_quantity, s.new_order_quantity,
5786                      DECODE( s.implement_date, s.new_schedule_date,
5787                      	DECODE(NVL(s.implement_alternate_bom, ''-23453''),
5788                      			 NVL(s.alternate_bom_designator, ''-23453''),
5789                					 DECODE(NVL(s.implement_alternate_routing, ''-23453''),
5790                       			 NVL(s.alternate_routing_designator, ''-23453''),
5791                         DECODE(trunc(msc_calendar.date_offset
5792                               (s.organization_id,
5793                                s.sr_instance_id,
5794                                1, --daily bucket
5795                                s.need_by_date ,
5796                                ceil(nvl(msi.fixed_lead_time,0) +
5797                  nvl(msi.variable_lead_time,0) * s.implement_quantity)*-1 )),
5798                      trunc(s.new_wip_start_date),1,
5799                                    decode(' || lv_release_details || ',2,2,1)),
5800                            2),
5801                          2),
5802                        2),
5803                      2),
5804                  2))),
5805          s.organization_id,
5806          s.plan_id,
5807          decode( ' || l_apps_ver ||' ,4,msi.rounding_control_type,3,msi.rounding_control_type,2)
5808      FROM msc_supplies s,
5809           msc_plan_organizations_v orgs,
5810           msc_plan_buckets mpb,
5811           msc_system_items msi,
5812           msc_plans mp
5813     WHERE mp.plan_id = :arg_plan_id
5814     AND   s.release_errors is NULL
5815     AND   s.implement_quantity > 0
5816     AND   s.organization_id = orgs.planned_organization
5817     AND   s.sr_instance_id = orgs.sr_instance_id
5818     AND   s.plan_id = orgs.plan_id
5819     AND   msi.inventory_item_id = s.inventory_item_id
5820     AND   msi.plan_id = s.plan_id
5821     AND   msi.organization_id = s.organization_id
5822     AND   msi.sr_instance_id = s.sr_instance_id
5823     AND   orgs.plan_id = mp.plan_id
5824     AND   orgs.organization_id = :arg_owning_org_id
5825     AND   orgs.owning_sr_instance = :arg_owning_instance
5826     AND   orgs.sr_instance_id = :arg_org_instance
5827     and   s.plan_id = mpb.plan_id
5828     and  (nvl(s.implement_date,s.new_schedule_date) between mpb.bkt_start_date and mpb.bkt_end_date)
5829     and   s.new_wip_start_date IS NOT NULL
5830     and (s.releasable = ' || RELEASABLE || ' or s.releasable is null )';
5831 
5832     IF  v_batch_id_populated = 2 THEN
5833         l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id is NULL ';
5834     ELSE
5835         l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id = ' || g_batch_id || ' ' ;
5836     END IF;
5837 
5838     IF arg_log_org_id <> arg_owning_org_id THEN
5839             l_sql_stmt :=  l_sql_stmt ||'      AND     orgs.planned_organization = ' || arg_log_org_id || ' ' ;
5840     END IF;
5841 
5842     IF arg_mode IS NULL THEN
5843         l_sql_stmt :=  l_sql_stmt ||'  AND s.load_type = ' || WIP_DIS_MASS_LOAD || ' ';
5844         IF v_msc_released_only_by_user = 1 THEN
5845             l_sql_stmt :=  l_sql_stmt || ' AND s.last_updated_by = ' || v_user_id || ' ' ;
5846         END IF;
5847     ELSIF arg_mode = 'WF_BATCH' THEN
5848         l_sql_stmt :=  l_sql_stmt ||'  AND s.load_type = ' || WIP_DIS_MASS_LOAD || ' ';
5849     ELSIF arg_mode = 'WF' THEN
5850         l_sql_stmt :=  l_sql_stmt ||'      AND s.transaction_id = ' || arg_transaction_id  || '  ' ;
5851     END IF;
5852 
5853 l_sql_stmt :=  l_sql_stmt ||'
5854 UNION
5855   SELECT s.transaction_id,
5856           s.sr_instance_id,
5857           2 /* Details not enabled for Manual Planned orders */,
5858           s.organization_id,
5859           s.plan_id,
5860           2  /* setting rounding control to 2 ,since details are not released and this flag is used in details*/
5861      FROM msc_supplies s,
5862           msc_plan_organizations_v orgs
5863     WHERE s.release_errors is NULL
5864     AND   s.implement_quantity > 0
5865     AND   s.organization_id = orgs.planned_organization
5866     AND   s.sr_instance_id = orgs.sr_instance_id
5867     AND   s.plan_id = :arg_plan_id
5868     AND   orgs.plan_id = :arg_plan_id
5869     AND   orgs.organization_id = :arg_owning_org_id
5870     AND   orgs.owning_sr_instance = :arg_owning_instance
5871     AND   orgs.sr_instance_id = :arg_org_instance
5872     and   s.new_wip_start_date IS NULL
5873     and (s.releasable = ' || RELEASABLE || ' or s.releasable is null ) ';
5874 
5875     IF  v_batch_id_populated = 2 THEN
5876         l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id is NULL ';
5877     ELSE
5878         l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id = ' || g_batch_id || ' ' ;
5879     END IF;
5880 
5881     IF arg_log_org_id <> arg_owning_org_id THEN
5882             l_sql_stmt :=  l_sql_stmt ||'      AND     orgs.planned_organization = ' || arg_log_org_id || ' ' ;
5883     END IF;
5884 
5885 
5886     IF arg_mode IS NULL THEN
5887         l_sql_stmt :=  l_sql_stmt ||'  AND s.load_type = ' || WIP_DIS_MASS_LOAD || ' ';
5888         IF v_msc_released_only_by_user = 1 THEN
5889             l_sql_stmt :=  l_sql_stmt || ' AND s.last_updated_by = ' || v_user_id || ' ' ;
5890         END IF;
5891     ELSIF arg_mode = 'WF_BATCH' THEN
5892         l_sql_stmt :=  l_sql_stmt ||'  AND s.load_type = ' || WIP_DIS_MASS_LOAD || ' ';
5893     ELSIF arg_mode = 'WF' THEN
5894         l_sql_stmt :=  l_sql_stmt ||'      AND s.transaction_id = ' || arg_transaction_id  || '  ' ;
5895     END IF;
5896 
5897     lv_job_count:= 0;
5898     IF arg_mode IS NULL OR arg_mode = 'WF_BATCH' OR arg_mode = 'WF' THEN
5899         EXECUTE IMMEDIATE l_sql_stmt
5900         BULK COLLECT
5901                                              INTO lv_transaction_id,
5902                                                   lv_instance_id,
5903                                                   lv_details_enabled,
5904                                                   lv_org_id,
5905                                                   lv_plan_id,
5906                                                   lv_round_primary_item
5907                                     USING  arg_plan_id
5908                                             ,arg_plan_id
5909                                             ,arg_owning_org_id
5910                                             ,arg_owning_instance
5911                                             ,arg_org_instance
5912                                             ,arg_plan_id
5913                                             ,arg_plan_id
5914                                             ,arg_owning_org_id
5915                                             ,arg_owning_instance
5916                                             ,arg_org_instance  ;
5917         lv_job_count:= SQL%ROWCOUNT;
5918     END IF;
5919 
5920     -- -----------------------------------------------------------------------
5921     -- Perform the wip discrete job mass load
5922     -- -----------------------------------------------------------------------
5923        /* Due to we only give PLANNED components, BILL_RTG_EXPLOSION_FLAG
5924           is set to 'Y'.  */
5925 
5926     --DBMS_OUTPUT.PUT_LINE('LOAD_JOB');
5927 
5928     FOR k in 1..lv_job_count
5929        Loop
5930                 Begin
5931                   SELECT 2
5932                   Into lv_agg_details(k)
5933                   FROM msc_department_resources deptres,
5934                        msc_resource_requirements resreq
5935                  WHERE resreq.sr_instance_id= lv_instance_id(k)
5936                    AND resreq.supply_id = lv_transaction_id(k)
5937                    AND resreq.organization_id= lv_org_id(k)
5938                    AND resreq.plan_id   = lv_plan_id(k)
5939                    AND resreq.parent_id   = 2
5940                    AND deptres.plan_id  = resreq.plan_id
5941                    AND deptres.sr_instance_id= resreq.sr_instance_id
5942                    AND deptres.resource_id= resreq.resource_id
5943                    AND deptres.department_id= resreq.department_id
5944                    AND deptres.organization_id= resreq.organization_id
5945                    AND deptres.aggregate_resource_flag= 1
5946                    AND rownum=1;
5947                   Exception
5948                   When no_data_found
5949                   then
5950                   lv_agg_details(k) := 1;
5951                   End;
5952 
5953        End Loop;
5954 
5955 
5956     FORALL j IN 1..lv_job_count
5957     INSERT INTO msc_wip_job_schedule_interface
5958             (last_update_date,
5959             last_updated_by,
5960             last_update_login,
5961             creation_date,
5962             created_by,
5963             group_id,
5964             source_code,
5965             source_line_id,
5966             organization_id,
5967             organization_type,
5968             load_type,
5969             status_type,
5970             first_unit_start_date,
5971             last_unit_completion_date,
5972             bom_revision_date,
5973             routing_revision_date,
5974             primary_item_id,
5975             class_code,
5976             job_name,
5977             firm_planned_flag,
5978             start_quantity,
5979 	    net_quantity,
5980             demand_class,
5981             project_id,
5982             task_id,
5983 	    schedule_group_id,
5984        	    build_sequence,
5985 	    line_id,
5986 	    alternate_bom_designator,
5987 	    alternate_routing_designator,
5988 	    end_item_unit_number,
5989 	    process_phase,
5990 	    process_status,
5991             bom_reference_id,
5992             routing_reference_id,
5993             BILL_RTG_EXPLOSION_FLAG,
5994             HEADER_ID,
5995             uom_code, --Outbound Changes for XML
5996             SR_INSTANCE_ID
5997             -- dsr
5998             , schedule_priority
5999             , requested_completion_date
6000             )
6001     SELECT  SYSDATE,
6002             arg_user_id,
6003             s.last_update_login,
6004             decode(tp.organization_type,2,s.creation_date,SYSDATE),
6005             arg_user_id,
6006             arg_wip_group_id,
6007             'MSC',
6008             s.transaction_id,
6009             msi.organization_id,
6010             tp.organization_type,
6011             1,
6012             decode(tp.organization_type,2,1,s.implement_status_code),
6013             DECODE( lv_details_enabled(j),
6014                     2,DECODE( tp.organization_type,
6015                               2, s.new_wip_start_date,
6016                               NULL),
6017                     s.new_wip_start_date),
6018             s.implement_date,
6019  /* Added to code to release the greatest of sysdate OR the BOM/Routing revision date */
6020             decode( lv_details_enabled(j),1, -- If details and daily buckets
6021                     GREATEST(trunc(sysdate),trunc(msc_calendar.date_offset
6022                          (s.organization_id,
6023                           s.sr_instance_id,
6024                           1, --daily bucket
6025                           s.need_by_date ,
6026                          ceil(nvl(msi.fixed_lead_time,0) +
6027                           nvl(msi.variable_lead_time,0) * s.implement_quantity)*-1 ) )
6028                             )+(1439/1440)
6029                     ,NULL),
6030             decode( lv_details_enabled(j),1, -- If details and daily buckets
6031                     GREATEST(trunc(sysdate),trunc( msc_calendar.date_offset
6032                          (s.organization_id,
6033                           s.sr_instance_id,
6034                           1, --daily bucket
6035                           s.need_by_date ,
6036                          ceil(nvl(msi.fixed_lead_time,0) +
6037                           nvl(msi.variable_lead_time,0) * s.implement_quantity)*-1 ) )
6038                             )+(1439/1440)
6039                     ,NULL),
6040             msi.sr_inventory_item_id,
6041             s.implement_wip_class_code,
6042             s.implement_job_name,
6043             s.implement_firm,
6044 /*
6045             decode(msi.rounding_control_type,1,
6046                ROUND(s.implement_quantity/GET_REV_CUM_YIELD_DISC(s.sr_instance_id, s.plan_id
6047                    ,s.process_seq_id,s.transaction_id,s.organization_id,tp.organization_type)),
6048                (s.implement_quantity/GET_REV_CUM_YIELD_DISC(s.sr_instance_id, s.plan_id
6049                    ,s.process_seq_id,s.transaction_id,s.organization_id,tp.organization_type))
6050                    ),*/
6051 /* Bug 4540170 - PLANNED ORDERS RELEASED FROM ASCP DO NOT CREATE BATCH WITH CORRECT QTYS */
6052             decode(tp.organization_type,2,s.implement_quantity,     -- 4540170
6053                      decode(s.implement_quantity,s.new_order_quantity,
6054                                               nvl(s.wip_start_quantity,s.implement_quantity),
6055                                               s.implement_quantity)
6056             ),
6057 	    s.implement_quantity,
6058             s.implement_demand_class,
6059             s.implement_project_id,
6060             s.implement_task_id,
6061 	    s.implement_schedule_group_id,
6062 	    s.implement_build_sequence,
6063        	    s.implement_line_id,
6064 	    s.implement_alternate_bom,
6065 	    s.implement_alternate_routing,
6066  	    s.implement_unit_number,
6067 	    2,
6068 	    1,
6069             DECODE( tp.organization_type,
6070                     2, mpe.bill_sequence_id,
6071                     NULL),
6072             DECODE( tp.organization_type,
6073                     2, mpe.routing_sequence_id,
6074                     NULL),
6075             'Y',
6076             s.transaction_id,
6077             nvl(s.implement_uom_code,msi.uom_code),
6078             s.sr_instance_id
6079             -- dsr
6080             , s.schedule_priority
6081             , nvl(s.requested_completion_date, s.need_by_date)
6082       FROM  msc_trading_partners    tp,
6083             msc_parameters          param,
6084             msc_system_items        msi,
6085             msc_process_effectivity mpe,
6086             msc_supplies            s
6087     WHERE   tp.sr_tp_id= msi.organization_id
6088     AND     tp.sr_instance_id= msi.sr_instance_id
6089     AND     tp.partner_type=3
6090     AND     param.organization_id = msi.organization_id
6091     AND     param.sr_instance_id  = msi.sr_instance_id
6092     AND     msi.inventory_item_id = s.inventory_item_id
6093     AND     msi.plan_id = s.plan_id
6094     AND     msi.organization_id = s.organization_id
6095     AND     msi.sr_instance_id = s.sr_instance_id
6096     AND     mpe.plan_id(+)= s.plan_id
6097     AND     mpe.process_sequence_id(+)= s.process_seq_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 
6102     IF SQL%ROWCOUNT > 0
6103     THEN
6104         lv_loaded_jobs := SQL%ROWCOUNT;
6105 
6106     ELSE
6107         lv_loaded_jobs := 0;
6108 
6109     END IF;
6110 
6111 
6112 
6113 
6114 
6115     -- ------------------------------------------------------------------------
6116     -- Perform the wip discrete job mass load for the details
6117     -- ------------------------------------------------------------------------
6118 
6119     /* the details are populated, only if the implement date and quantity
6120        are the same as the planned date and quantity */
6121 
6122     /* If the current plan is DRP plan then check the following conditions
6123        to determin whether we need to release the details or not
6124        1) For planned orders that do not use
6125           substitute components/alternateboms, only the
6126           header level information is released */
6127 
6128     l_count := 1;
6129 
6130 /*
6131     IF l_plan_type = 5 THEN   -- DRP plan
6132        --OPEN  c_release_dtls(arg_transaction_id, arg_plan_id);
6133        --FETCH c_release_dtls INTO l_count;
6134        --CLOSE c_release_dtls;
6135 
6136        -- dsting redo the details enabled flag for things with subst comp
6137        for i IN 1..lv_transaction_id.count loop
6138           SELECT count(1)
6139             INTO l_sub_comp_count
6140             from msc_exception_details excp
6141             where excp.plan_id = arg_plan_id
6142             and number1 = lv_transaction_id(i)
6143             and excp.exception_type = 33
6144             and rownum < 2;
6145 
6146           IF l_sub_comp_count > 0 THEN
6147              lv_details_enabled(i) := 1;
6148           END IF;
6149        END LOOP;
6150     END IF;
6151 */
6152     /* If it is not a drp plan then l_count = 1 so details will be released */
6153     IF l_count = 1 THEN  /* Release details as well */
6154        /* the details are populated, only if the implement date and quantity
6155           are the same as the planned date and quantity */
6156 
6157        /* OPERATIONS */
6158        FORALL j IN 1..lv_job_count
6159        INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
6160            (last_update_date,
6161             last_updated_by,
6162             last_update_login,
6163             creation_date,
6164             created_by,
6165             organization_type,
6166             organization_id,
6167             group_id,
6168             parent_header_id,
6169             operation_seq_num,
6170             department_id,
6171             description,
6172             standard_operation_id,
6173             first_unit_start_date,
6174             first_unit_completion_date,
6175             last_unit_start_date,
6176             last_unit_completion_date,
6177             minimum_transfer_quantity,
6178             count_point_type,
6179             backflush_flag,
6180             SUBSTITUTION_TYPE,
6181             LOAD_TYPE,
6182 	    process_phase,
6183 	    process_status,
6184             operation_seq_id, --Outbound changes for XML
6185             SR_INSTANCE_ID)
6186     SELECT  SYSDATE,
6187             arg_user_id,
6188             s.last_update_login,
6189             SYSDATE,
6190             arg_user_id,
6191             tp.organization_type,
6192             s.organization_id,
6193             arg_wip_group_id,
6194             s.transaction_id,
6195             resreq.OPERATION_SEQ_NUM,
6196             NULL,            --department_id,
6197             NULL,   --description,
6198             NULL,   --standard_operation_id,
6199             min(resreq.START_DATE),   --first_unit_start_date,
6200             min(resreq.START_DATE),   --first_unit_completion_date,
6201             max(resreq.END_DATE),     --last_unit_start_date,
6202             max(resreq.END_DATE),     --last_unit_completion_date,
6203             NULL,   --minimum_transfer_quantity,
6204             NULL,   --count_point_type,
6205             NULL,   --backflush_flag,
6206             SUBST_CHANGE,
6207             LT_OPERATION,
6208             2,
6209             1,
6210             resreq.operation_sequence_id, --Outbound changes for XML
6211             s.sr_instance_id
6212       FROM  msc_trading_partners   tp,
6213             msc_resource_requirements resreq,
6214             msc_parameters          param,
6215             msc_system_items        msi,
6216             msc_supplies            s
6217     WHERE   tp.sr_tp_id= msi.organization_id
6218     AND     tp.sr_instance_id= msi.sr_instance_id
6219     AND     tp.partner_type=3
6220     AND     resreq.sr_instance_id= s.sr_instance_id
6221     AND     resreq.organization_id= s.organization_id
6222     AND     resreq.supply_id = s.transaction_id
6223     AND     resreq.plan_id   = s.plan_id
6224     AND     resreq.parent_id   = 2
6225     AND     resreq.resource_id <> -1
6226     AND     resreq.department_id <> -1
6227     AND     param.organization_id = msi.organization_id
6228     AND     param.sr_instance_id  = msi.sr_instance_id
6229     AND     msi.inventory_item_id = s.inventory_item_id
6230     AND     msi.plan_id = s.plan_id
6231     AND     msi.organization_id = s.organization_id
6232     AND     msi.sr_instance_id = s.sr_instance_id
6233     AND     s.transaction_id= lv_transaction_id(j)
6234     AND     s.sr_instance_id= lv_instance_id(j)
6235     AND     s.plan_id= arg_plan_id
6236     AND     lv_details_enabled(j)= 1
6237     AND     lv_agg_details(j) = 1
6238     GROUP BY
6239             SYSDATE,
6240             arg_user_id,
6241             tp.organization_type,
6242             s.organization_id,
6243             s.last_update_login,
6244             SYSDATE,
6245             arg_user_id,
6246             arg_wip_group_id,
6247             s.transaction_id,
6248             resreq.OPERATION_SEQ_NUM,
6249             NULL,            --department_id,
6250             LT_OPERATION,    --load_type,
6251             NULL,            --description,
6252             NULL,            --standard_operation_id,
6253             NULL,            --minimum_transfer_quantity,
6254             NULL,            --count_point_type,
6255             NULL,            --backflush_flag,
6256             resreq.operation_sequence_id,
6257             s.sr_instance_id;
6258 
6259 
6260     --DBMS_OUTPUT.PUT_LINE('OPERATION_RESOURCE');
6261  /* for bug: 2479630, modified the expression that is passed in the column: usage_rate_or_amount,
6262    to divide the resource_hours by cum. qty (which is equal to new_order_qty/cum. yield) and if that is null
6263     than apply the CY on new_order_qty. This logic to use cummulative_qty is added to remove the calc. errors */
6264 
6265     /* OPERATION RESOURCES */
6266     FORALL j IN 1..lv_job_count
6267     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
6268           ( last_update_date,
6269             last_updated_by,
6270             last_update_login,
6271             creation_date,
6272             created_by,
6273             organization_type,
6274             organization_id,
6275             group_id,
6276             parent_header_id,
6277             operation_seq_num,
6278            -- resource_seq_num, rawasthi
6279             alternate_num,
6280             resource_id_old,
6281             resource_id_new,
6282             usage_rate_or_amount,
6283             scheduled_flag,
6284             applied_resource_units,   --
6285             applied_resource_value,   --
6286             uom_code,
6287             basis_type,     --
6288             activity_id,    --
6289             autocharge_type,     --
6290             standard_rate_flag,  --
6291             start_date,
6292             completion_date,
6293             assigned_units,
6294             SUBSTITUTION_TYPE,
6295             LOAD_TYPE,
6296 	    process_phase,
6297 	    process_status,
6298             description,
6299             SR_INSTANCE_ID,
6300             operation_seq_id, --Outbound changes for XML
6301             FIRM_FLAG,
6302             resource_hours,
6303             department_id
6304 	-- added the following for dsr
6305 	   , setup_id
6306 	   , group_sequence_id
6307 	   , group_sequence_number
6308            , batch_id
6309 	   , maximum_assigned_units
6310 	   , parent_seq_num
6311 	   , resource_seq_num
6312            , schedule_seq_num
6313 	 )
6314     SELECT  SYSDATE,
6315             arg_user_id,
6316             s.last_update_login,
6317             SYSDATE,
6318             arg_user_id,
6319             tp.organization_type,
6320             s.organization_id,
6321             arg_wip_group_id,
6322             s.transaction_id,
6323             resreq.OPERATION_SEQ_NUM,
6324            -- resreq.RESOURCE_SEQ_NUM,
6325             resreq.ALTERNATE_NUM,
6326             resreq.RESOURCE_ID,
6327             resreq.RESOURCE_ID,
6328              /* for OPM orgs (tp.organization_type =2) we don't consider lv_inflate_wip */
6329             /* decode(resreq.parent_seq_num, null, decode(decode(tp.organization_type,2,2,1),1,(resreq.RESOURCE_HOURS/decode(resreq.basis_type,2,1,
6330                     nvl(resreq.cummulative_quantity,
6331                              decode(msi.rounding_control_type,1,
6332                ROUND(s.new_order_quantity/decode(tp.organization_type,2,1,nvl(resreq.REVERSE_CUMULATIVE_YIELD,1)) ,6),
6333                     (s.new_order_quantity/decode(tp.organization_type,2,1,nvl(resreq.REVERSE_CUMULATIVE_YIELD,1)) )
6334                             ) ) ))* decode(mdr.efficiency,NULL,100,0,100,mdr.efficiency)/100 * decode(mdr.utilization,NULL,100,0,100,mdr.utilization)/100,
6335                                     resreq.RESOURCE_HOURS/decode(resreq.basis_type,2,1,
6336                     nvl(resreq.cummulative_quantity,
6337                              decode(msi.rounding_control_type,1,
6338                ROUND(s.new_order_quantity/decode(tp.organization_type,2,1,nvl(resreq.REVERSE_CUMULATIVE_YIELD,1)) ,6),
6339                     (s.new_order_quantity/decode(tp.organization_type,2,1,nvl(resreq.REVERSE_CUMULATIVE_YIELD,1)) )
6340                             ) ) )), resreq.usage_rate),  */
6341             resreq.usage_rate,
6342             decode(nvl(resreq.schedule_flag,1),-23453,1,1,1,resreq.schedule_flag),
6343             NULL,
6344             NULL,
6345             v_hour_uom,
6346             resreq.basis_type,
6347             NULL,
6348             NULL,
6349             NULL,
6350             nvl(resreq.firm_start_date,resreq.START_DATE),
6351             nvl(resreq.firm_end_date,resreq.END_DATE),
6352             resreq.ASSIGNED_UNITS,
6353             decode(resreq.parent_seq_num,null,SUBST_CHANGE,SUBST_ADD),
6354             -- SUBST_CHANGE,
6355             LT_RESOURCE,
6356             2,
6357             1,
6358             NULL,
6359             s.sr_instance_id,
6360             resreq.operation_sequence_id, --Outbound changes for XML
6361             -- dsr decode(nvl(resreq.firm_flag,0),0,2,1),
6362             NVL(resreq.firm_flag, 0), -- if null, then default to not firm (0)
6363             resreq.resource_hours,
6364             resreq.department_id
6365             -- added the following for dsr
6366 	    , resreq.setup_id
6367 	    , resreq.group_sequence_id
6368 	    , resreq.group_sequence_number
6369      	    , resreq.batch_number
6370 	    , resreq.maximum_assigned_units
6371 	    , resreq.parent_seq_num
6372 	    , resreq.orig_resource_seq_num
6373             , resreq.resource_seq_num
6374       FROM  msc_trading_partners   tp,
6375             msc_resource_requirements resreq,
6376             msc_parameters          param,
6377             msc_system_items        msi,
6378             msc_supplies            s,
6379             msc_department_resources mdr
6380     WHERE   tp.sr_tp_id= msi.organization_id
6381     AND     tp.sr_instance_id= msi.sr_instance_id
6382     AND     tp.partner_type=3
6383     AND     resreq.sr_instance_id= s.sr_instance_id
6384     AND     resreq.organization_id= s.organization_id
6385     AND     resreq.supply_id = s.transaction_id
6386     AND     resreq.plan_id   = s.plan_id
6387     AND     resreq.parent_id   = 2
6388     AND     resreq.resource_id <> -1
6389     AND     resreq.department_id <> -1
6390     AND     param.organization_id = msi.organization_id
6391     AND     param.sr_instance_id  = msi.sr_instance_id
6392     AND     msi.inventory_item_id = s.inventory_item_id
6393     AND     msi.plan_id = s.plan_id
6394     AND     msi.organization_id = s.organization_id
6395     AND     msi.sr_instance_id = s.sr_instance_id
6396     AND     s.transaction_id= lv_transaction_id(j)
6397     AND     s.sr_instance_id= lv_instance_id(j)
6398     AND     s.plan_id= arg_plan_id
6399     AND     lv_details_enabled(j)= 1
6400     AND     lv_agg_details(j) = 1
6401     AND     resreq.plan_id = mdr.plan_id
6402     AND     resreq.organization_id =mdr.organization_id
6403     AND     resreq.sr_instance_id = mdr.sr_instance_id
6404     AND     resreq.resource_id = mdr.resource_id
6405     AND     resreq.department_id=mdr.department_id;
6406 
6407 
6408     --DBMS_OUTPUT.PUT_LINE('LOAD COMPONENTS');
6409 
6410  /* for bug: 2378484, added code to consider the  cum yield in the calc of qty_per_assembly */
6411     /* UPDATE EXISTING COMPONENTS                      *
6412      |    We should set inventory_item_id_new to NULL  |
6413      *                                                 */
6414     FORALL j IN 1..lv_job_count
6415     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
6416             (last_update_date,
6417             last_updated_by,
6418             last_update_login,
6419             creation_date,
6420             created_by,
6421             organization_type,
6422             organization_id,
6423             group_id,
6424             parent_header_id,
6425             operation_seq_num,
6426             inventory_item_id_old,
6427             inventory_item_id_new,
6428             basis_type,
6429             quantity_per_assembly,
6430             component_yield_factor,
6431             department_id,
6432             wip_supply_type,
6433             date_required,
6434             required_quantity,
6435             quantity_issued,
6436             supply_subinventory,
6437             supply_locator_id,
6438             mrp_net_flag,
6439             mps_required_quantity,
6440             mps_date_required,
6441             SUBSTITUTION_TYPE,
6442             LOAD_TYPE,
6443 	    process_phase,
6444 	    process_status,
6445             description,
6446 --            operation_seq_id, --Outbound changes for XML
6447             uom_code, --Outbound Changes for XML
6448             SR_INSTANCE_ID)
6449     SELECT  SYSDATE,
6450             arg_user_id,
6451             s.last_update_login,
6452             SYSDATE,
6453             arg_user_id,
6454             tp.organization_type,
6455             s.organization_id,
6456             arg_wip_group_id,
6457             s.transaction_id,
6458             nvl(md.op_seq_num,1),
6459             icomp.sr_inventory_item_id,
6460             decode(l_apps_ver,'4', null, '3',null,icomp.sr_inventory_item_id),
6461             decode(md.component_scaling_type,1,NULL,md.component_scaling_type),
6462 /*
6463             decode(lv_round_primary_item(j),1,
6464                ROUND(md.USING_REQUIREMENT_QUANTITY/ROUND(s.implement_quantity/GET_REV_CUM_YIELD_DISC_COMP(s.sr_instance_id
6465                         ,s.plan_id,s.process_seq_id,s.transaction_id,s.organization_id,tp.organization_type,md.op_seq_num),6),6),
6466                ROUND(md.USING_REQUIREMENT_QUANTITY/(s.implement_quantity/GET_REV_CUM_YIELD_DISC_COMP(s.sr_instance_id
6467                         ,s.plan_id,s.process_seq_id,s.transaction_id,s.organization_id,tp.organization_type,md.op_seq_num)),6)
6468                   ),*/
6469 --            round(md.USING_REQUIREMENT_QUANTITY/s.wip_start_quantity,6),
6470             TO_NUMBER(NULL),       --Quantity_per
6471             md.component_yield_factor,
6472             TO_NUMBER(NULL),       --Department_ID
6473 /*
6474             NVL(GET_WIP_SUPPLY_TYPE(s.plan_id, s.sr_instance_id,s.process_seq_id,
6475                                     s.inventory_item_id,md.inventory_item_id,s.organization_id),
6476                     icomp.wip_supply_type),*/
6477             md.wip_supply_type,
6478             md.USING_ASSEMBLY_DEMAND_DATE,
6479             md.USING_REQUIREMENT_QUANTITY,
6480             0,
6481             TO_CHAR(NULL),     -- Sub Inventory
6482             TO_NUMBER(NULL),   -- Locator ID
6483             1,                 -- MRP_NET_FLAG
6484             md.USING_REQUIREMENT_QUANTITY,
6485             md.USING_ASSEMBLY_DEMAND_DATE,
6486             SUBST_CHANGE,
6487             LT_COMPONENT,
6488             2,
6489             1,
6490             TO_CHAR(NULL),
6491 --            md.operation_seq_id,
6492             nvl(s.implement_uom_code,msi.uom_code),
6493             s.sr_instance_id
6494       FROM  msc_trading_partners    tp,
6495             msc_system_items        icomp,
6496             msc_demands             md,
6497             msc_parameters          param,
6498             msc_system_items        msi,
6499             msc_supplies            s
6500     WHERE   /*
6501             ( md.SUBST_ITEM_FLAG <> 1
6502               OR md.SUBST_ITEM_FLAG IS NULL) */
6503             not exists (select 'x'
6504                         from msc_exception_details excp
6505                         where excp.plan_id = msi.plan_id
6506                         and excp.number1 = s.transaction_id
6507                         and excp.inventory_item_id = msi.inventory_item_id
6508                         and excp.organization_id = msi.organization_id
6509                         and excp.sr_instance_id = msi.sr_instance_id
6510                         and excp.exception_type = 33
6511                         and excp.number2 = md.inventory_item_id)
6512     AND     tp.sr_tp_id= msi.organization_id
6513     AND     tp.sr_instance_id= msi.sr_instance_id
6514     AND     tp.partner_type=3
6515     AND     icomp.inventory_item_id= md.inventory_item_id
6516     AND     icomp.organization_id= md.organization_id
6517     AND     icomp.sr_instance_id= md.sr_instance_id
6518     AND     icomp.plan_id= md.plan_id
6519     AND     nvl(icomp.wip_supply_type,0) <> 6
6520     AND     md.sr_instance_Id= s.sr_instance_Id
6521     AND     md.disposition_id= s.transaction_id
6522     AND     md.plan_id= s.plan_id
6523     AND     md.origination_type IN (1,47)
6524     AND     param.organization_id = msi.organization_id
6525     AND     param.sr_instance_id  = msi.sr_instance_id
6526     AND     msi.inventory_item_id = s.inventory_item_id
6527     AND     msi.plan_id = s.plan_id
6528     AND     msi.organization_id = s.organization_id
6529     AND     msi.sr_instance_id = s.sr_instance_id
6530     AND     s.transaction_id= lv_transaction_id(j)
6531     AND     s.sr_instance_id= lv_instance_id(j)
6532     AND     s.plan_id= arg_plan_id
6533     AND     lv_details_enabled(j)= 1
6534     AND     lv_agg_details(j) = 1;
6535 
6536     --Loading Co-products/by-products for OPM orgs
6537     FORALL j IN 1..lv_job_count
6538     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
6539             (last_update_date,
6540             last_updated_by,
6541             last_update_login,
6542             creation_date,
6543             created_by,
6544             organization_type,
6545             organization_id,
6546             group_id,
6547             parent_header_id,
6548             operation_seq_num,
6549             inventory_item_id_old,
6550             inventory_item_id_new,
6551             quantity_per_assembly,
6552             department_id,
6553             wip_supply_type,
6554             date_required,
6555             required_quantity,
6556             quantity_issued,
6557             supply_subinventory,
6558             supply_locator_id,
6559             mrp_net_flag,
6560             mps_required_quantity,
6561             mps_date_required,
6562             SUBSTITUTION_TYPE,
6563             LOAD_TYPE,
6564 	    process_phase,
6565 	    process_status,
6566             description,
6567             uom_code, --Outbound Changes for XML
6568             SR_INSTANCE_ID)
6569     SELECT  SYSDATE,
6570             arg_user_id,
6571             s.last_update_login,
6572             SYSDATE,
6573             arg_user_id,
6574             tp.organization_type,
6575             s.organization_id,
6576             arg_wip_group_id,
6577             s.transaction_id,
6578             nvl(co.operation_seq_num,1),
6579             icomp.sr_inventory_item_id,
6580             decode(l_apps_ver,'4',null,'3',null,icomp.sr_inventory_item_id),
6581             TO_NUMBER(NULL),       --Quantity_per
6582             TO_NUMBER(NULL),       --Department_ID
6583             co.wip_supply_type,
6584             co.NEW_SCHEDULE_DATE,
6585             co.NEW_ORDER_QUANTITY,
6586             0,
6587             TO_CHAR(NULL),     -- Sub Inventory
6588             TO_NUMBER(NULL),   -- Locator ID
6589             1,                 -- MRP_NET_FLAG
6590             co.NEW_ORDER_QUANTITY,
6591             co.NEW_SCHEDULE_DATE,
6592             SUBST_CHANGE,
6593             LT_COMPONENT,
6594             2,
6595             1,
6596             TO_CHAR(NULL),
6597             nvl(s.implement_uom_code,msi.uom_code),
6598             s.sr_instance_id
6599       FROM  msc_trading_partners    tp,
6600             msc_system_items        icomp,
6601             msc_supplies            co,
6602             msc_parameters          param,
6603             msc_system_items        msi,
6604             msc_supplies            s
6605     WHERE  /* not exists (select 'x'
6606                         from msc_exception_details excp
6607                         where excp.plan_id = msi.plan_id
6608                         and excp.number1 = s.transaction_id
6609                         and excp.inventory_item_id = msi.inventory_item_id
6610                         and excp.organization_id = msi.organization_id
6611                         and excp.sr_instance_id = msi.sr_instance_id
6612                         and excp.exception_type = 33
6613                         and excp.number2 = co.inventory_item_id)
6614     AND */
6615             tp.sr_tp_id             = msi.organization_id
6616     AND     tp.sr_instance_id       = msi.sr_instance_id
6617     AND     tp.partner_type         = 3
6618     AND     icomp.inventory_item_id = co.inventory_item_id
6619     AND     icomp.organization_id   = co.organization_id
6620     AND     icomp.sr_instance_id    = co.sr_instance_id
6621     AND     icomp.plan_id           = co.plan_id
6622     AND     co.sr_instance_Id       = s.sr_instance_Id
6623     AND     co.disposition_id       = s.transaction_id
6624     AND     co.plan_id              = s.plan_id
6625     AND     co.order_type           = 17        --Co-product /by-product
6626     AND     param.organization_id   = msi.organization_id
6627     AND     param.sr_instance_id    = msi.sr_instance_id
6628     AND     msi.inventory_item_id   = s.inventory_item_id
6629     AND     msi.plan_id             = s.plan_id
6630     AND     msi.organization_id     = s.organization_id
6631     AND     msi.sr_instance_id      = s.sr_instance_id
6632     AND     s.transaction_id        = lv_transaction_id(j)
6633     AND     s.sr_instance_id        = lv_instance_id(j)
6634     AND     s.plan_id               = arg_plan_id
6635     AND     lv_details_enabled(j)   = 1
6636     AND     lv_agg_details(j)       = 1
6637     AND     tp.organization_type    = 2;
6638 
6639     --DBMS_OUTPUT.PUT_LINE('LOAD SUBSTITUTE COMPONENTS');
6640     /* SUBSTITUTE EXISTING COMPONENTS */
6641     FORALL j IN 1..lv_job_count
6642     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
6643             (last_update_date,
6644             last_updated_by,
6645             last_update_login,
6646             creation_date,
6647             created_by,
6648             organization_type,
6649             group_id,
6650             parent_header_id,
6651             operation_seq_num,
6652             inventory_item_id_old,
6653             inventory_item_id_new,
6654             quantity_per_assembly,
6655             department_id,
6656             wip_supply_type,
6657             date_required,
6658             required_quantity,
6659             quantity_issued,
6660             supply_subinventory,
6661             supply_locator_id,
6662             mrp_net_flag,
6663             mps_required_quantity,
6664             mps_date_required,
6665             SUBSTITUTION_TYPE,
6666             LOAD_TYPE,
6667 	    process_phase,
6668 	    process_status,
6669             description,
6670 --            operation_seq_id, --Outbound changes for XML
6671             uom_code, --Outbound Changes for XML
6672             SR_INSTANCE_ID)
6673     SELECT  SYSDATE,
6674             arg_user_id,
6675             s.last_update_login,
6676             SYSDATE,
6677             arg_user_id,
6678             tp.organization_type,
6679             arg_wip_group_id,
6680             s.transaction_id,
6681             nvl(md.op_seq_num,1),
6682             sr_item.sr_inventory_item_id,
6683             icomp.sr_inventory_item_id,
6684 /*
6685             decode(lv_round_primary_item(j),1,
6686                      ROUND(md.USING_REQUIREMENT_QUANTITY/ROUND(s.implement_quantity/GET_REV_CUM_YIELD_DISC_COMP(s.sr_instance_id
6687                          ,s.plan_id,s.process_seq_id,s.transaction_id,s.organization_id,tp.organization_type,md.op_seq_num),6),6),
6688                      ROUND(md.USING_REQUIREMENT_QUANTITY/(s.implement_quantity/GET_REV_CUM_YIELD_DISC_COMP(s.sr_instance_id
6689                          ,s.plan_id,s.process_seq_id,s.transaction_id,s.organization_id,tp.organization_type,md.op_seq_num)),6)
6690                   ),*/
6691             bsub.usage_quantity,
6692             TO_NUMBER(NULL),       --Department_ID
6693 /*
6694             NVL(GET_WIP_SUPPLY_TYPE(s.plan_id, s.sr_instance_id,s.process_seq_id,
6695                                     s.inventory_item_id,md.inventory_item_id,s.organization_id),
6696                     icomp.wip_supply_type),*/
6697             nvl(md.wip_supply_type,icomp.wip_supply_type),
6698             md.USING_ASSEMBLY_DEMAND_DATE,
6699             md.USING_REQUIREMENT_QUANTITY,
6700             0,
6701             TO_CHAR(NULL),     -- Sub Inventory
6702             TO_NUMBER(NULL),   -- Locator ID
6703             1,
6704             md.USING_REQUIREMENT_QUANTITY,
6705             md.USING_ASSEMBLY_DEMAND_DATE,
6706             SUBST_CHANGE,
6707             LT_COMPONENT,
6708             2,
6709             1,
6710             TO_CHAR(NULL),
6711 --            md.operation_seq_id,
6712             nvl(s.implement_uom_code,msi.uom_code),
6713             s.sr_instance_id
6714       FROM  msc_trading_partners    tp,
6715             msc_item_id_lid         sr_item,
6716             msc_bom_components      subcomp,
6717             msc_component_substitutes bsub,
6718 /*            msc_bom_components      bcomp, */
6719             msc_boms                bom,
6720             msc_system_items        icomp,
6721             msc_demands             md,
6722             msc_parameters          param,
6723             msc_system_items        msi,
6724             msc_supplies            s
6725     WHERE   tp.sr_tp_id= msi.organization_id
6726     AND     tp.sr_instance_id= msi.sr_instance_id
6727     AND     tp.partner_type=3
6728     AND     sr_item.inventory_item_id= subcomp.inventory_item_id
6729     AND     sr_item.sr_instance_id= subcomp.sr_instance_id
6730     AND    subcomp.plan_id               = bsub.plan_id
6731     AND     subcomp.bill_sequence_id      = bsub.bill_sequence_id
6732     AND     subcomp.COMPONENT_SEQUENCE_ID = bsub.COMPONENT_SEQUENCE_ID
6733     AND     bsub.substitute_item_id = md.inventory_item_id
6734     AND     bsub.organization_id    = md.organization_id
6735     AND     bsub.plan_id            = md.plan_id
6736     AND     bsub.COMPONENT_SEQUENCE_ID= md.comp_seq_id
6737    /* Bug 3636332  AND     bsub.bill_sequence_id   = bom.bill_sequence_id */
6738   --------------------------------------------------------------
6739 /*    AND     md.SUBST_ITEM_FLAG=1 */
6740   --------------------------------------------------------------
6741     AND     ( bom.ALTERNATE_BOM_DESIGNATOR= s.ALTERNATE_BOM_DESIGNATOR
6742               OR ( bom.ALTERNATE_BOM_DESIGNATOR IS NULL
6743                    AND s.ALTERNATE_BOM_DESIGNATOR IS NULL))
6744     AND     bom.assembly_item_id=  s.inventory_item_id
6745     AND     bom.organization_id=   s.organization_id
6746     AND     bom.sr_instance_id=    s.sr_instance_id
6747     AND     bom.plan_id=           s.plan_id
6748     AND     icomp.inventory_item_id= md.inventory_item_id
6749     AND     icomp.organization_id= md.organization_id
6750     AND     icomp.sr_instance_id= md.sr_instance_id
6751     AND     icomp.plan_id= md.plan_id
6752     AND     md.sr_instance_Id= s.sr_instance_Id
6753     AND     md.disposition_id= s.transaction_id
6754     AND     md.plan_id= s.plan_id
6755     AND     md.origination_type in (1,47)
6756     AND     param.organization_id = msi.organization_id
6757     AND     param.sr_instance_id  = msi.sr_instance_id
6758     AND     msi.inventory_item_id = s.inventory_item_id
6759     AND     msi.plan_id = s.plan_id
6760     AND     msi.organization_id = s.organization_id
6761     AND     msi.sr_instance_id = s.sr_instance_id
6762     AND     s.transaction_id= lv_transaction_id(j)
6763     AND     s.sr_instance_id= lv_instance_id(j)
6764     AND     s.plan_id= arg_plan_id
6765     AND     lv_details_enabled(j)= 1
6766     AND     lv_agg_details(j) = 1;
6767 
6768     --DBMS_OUTPUT.PUT_LINE('RESOURCE_USAGE');
6769 
6770 
6771     -- RESOURCE USAGE
6772     FORALL j IN 1..lv_job_count
6773     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
6774           ( last_update_date,
6775             last_updated_by,
6776             last_update_login,
6777             creation_date,
6778             created_by,
6779             organization_type,
6780             organization_id,
6781             group_id,
6782             parent_header_id,
6783             operation_seq_num,
6784             -- resource_seq_num,
6785             alternate_num,
6786             resource_id_old,
6787             resource_id_new,
6788             usage_rate_or_amount,
6789             scheduled_flag,
6790             applied_resource_units,   --
6791             applied_resource_value,   --
6792             uom_code,
6793             basis_type,     --
6794             activity_id,    --
6795             autocharge_type,     --
6796             standard_rate_flag,  --
6797             start_date,
6798             completion_date,
6799             assigned_units,
6800             SUBSTITUTION_TYPE,
6801             LOAD_TYPE,
6802 	    process_phase,
6803 	    process_status,
6804             description,
6805             SR_INSTANCE_ID,
6806             operation_seq_id, --Outbound changes for XML
6807             resource_seq_num,
6808             schedule_seq_num,
6809             -- dsr FIRM_FLAG,
6810             department_id,
6811             resource_hours,
6812             parent_seq_num)
6813     SELECT  SYSDATE,
6814             arg_user_id,
6815             s.last_update_login,
6816             SYSDATE,
6817             arg_user_id,
6818             tp.organization_type,
6819             s.organization_id,
6820             arg_wip_group_id,
6821             s.transaction_id,
6822             resreq.OPERATION_SEQ_NUM,
6823            -- resreq.RESOURCE_SEQ_NUM,
6824             resreq.ALTERNATE_NUM,
6825             resreq.RESOURCE_ID,
6826             resreq.RESOURCE_ID,
6827            /* for OPM orgs (tp.organization_type =2) we don't consider lv_inflate_wip */
6828            /* 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,
6829                     nvl(decode(resreq.cummulative_quantity,0,1,resreq.cummulative_quantity),
6830                              decode(msi.rounding_control_type,1,
6831                ROUND(s.new_order_quantity/decode(tp.organization_type,2,1,nvl(resreq.REVERSE_CUMULATIVE_YIELD,1)) ,6),
6832                     (s.new_order_quantity/decode(tp.organization_type,2,1,nvl(resreq.REVERSE_CUMULATIVE_YIELD,1)) )
6833                             ) ) ))* decode(mdr.efficiency,NULL,100,0,100,mdr.efficiency)/100 * decode(mdr.utilization,NULL,100,0,100,mdr.utilization)/100,
6834                                     resreq.RESOURCE_HOURS/decode(resreq.basis_type,2,1,
6835                     nvl(decode(resreq.cummulative_quantity,0,1,resreq.cummulative_quantity),
6836                              decode(msi.rounding_control_type,1,
6837                ROUND(s.new_order_quantity/decode(tp.organization_type,2,1,nvl(resreq.REVERSE_CUMULATIVE_YIELD,1)) ,6),
6838                     (s.new_order_quantity/decode(tp.organization_type,2,1,nvl(resreq.REVERSE_CUMULATIVE_YIELD,1)) )
6839                             ) ) )), resreq.usage_rate),  */
6840             resreq.usage_rate,
6841             decode(nvl(resreq.schedule_flag,1),-23453,1,1,1,resreq.schedule_flag),
6842             NULL,
6843             NULL,
6844             v_hour_uom,
6845             resreq.basis_type,
6846             NULL,
6847             NULL,
6848             NULL,
6849             nvl(resreq.firm_start_date,resreq.START_DATE),
6850             nvl(resreq.firm_end_date,resreq.END_DATE),
6851             resreq.ASSIGNED_UNITS,
6852             SUBST_CHANGE,
6853             LT_RESOURCE_USAGE,
6854             2,
6855             1,
6856             NULL,
6857             s.sr_instance_id,
6858             resreq.operation_sequence_id, --Outbound changes for XML
6859             resreq.orig_resource_seq_num,
6860             resreq.resource_seq_num,
6861             -- dsr decode(nvl(resreq.firm_flag,0),0,2,1),
6862             resreq.department_id,
6863             resreq.resource_hours,
6864             resreq.parent_seq_num
6865       FROM  msc_trading_partners   tp,
6866             msc_resource_requirements resreq,
6867             msc_parameters          param,
6868             msc_system_items        msi,
6869             msc_supplies            s,
6870             msc_department_resources mdr
6871     WHERE   tp.sr_tp_id= msi.organization_id
6872     AND     tp.sr_instance_id= msi.sr_instance_id
6873     AND     tp.partner_type=3
6874     AND     resreq.sr_instance_id= s.sr_instance_id
6875     AND     resreq.organization_id= s.organization_id
6876     AND     resreq.supply_id = s.transaction_id
6877     AND     resreq.plan_id   = s.plan_id
6878     AND     resreq.parent_id   = 1
6879     AND     resreq.resource_id <> -1
6880     AND     resreq.department_id <> -1
6881     AND     param.organization_id = msi.organization_id
6882     AND     param.sr_instance_id  = msi.sr_instance_id
6883     AND     msi.inventory_item_id = s.inventory_item_id
6884     AND     msi.plan_id = s.plan_id
6885     AND     msi.organization_id = s.organization_id
6886     AND     msi.sr_instance_id = s.sr_instance_id
6887     AND     s.transaction_id= lv_transaction_id(j)
6888     AND     s.sr_instance_id= lv_instance_id(j)
6889     AND     s.plan_id= arg_plan_id
6890     AND     lv_details_enabled(j)= 1
6891     AND     lv_agg_details(j) = 1
6892     AND     resreq.plan_id = mdr.plan_id
6893     AND     resreq.organization_id =mdr.organization_id
6894     AND     resreq.sr_instance_id = mdr.sr_instance_id
6895     AND     resreq.resource_id = mdr.resource_id
6896     AND     resreq.department_id=mdr.department_id ;
6897    -- AND     tp.organization_type = 2;
6898 
6899    -- dsr starts here
6900     print_debug_info('OPERATION RESOURCE_INSTANCES');
6901 	-- OPERATION RESOURCE_INSTANCES
6902 
6903     FORALL j IN 1..lv_job_count
6904     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
6905           ( last_update_date,
6906             last_updated_by,
6907             last_update_login,
6908             creation_date,
6909             created_by,
6910             organization_type,
6911             organization_id,
6912             group_id,
6913             parent_header_id,
6914             operation_seq_num,
6915             -- resource_seq_num,
6916             resource_id_old, --rawasthi
6917             resource_id_new, -- xx resource_id, ???
6918             RESOURCE_INSTANCE_ID,
6919             start_date,
6920             completion_date,
6921             SUBSTITUTION_TYPE,
6922             LOAD_TYPE,
6923             process_phase,
6924             process_status,
6925             SR_INSTANCE_ID,
6926             operation_seq_id,
6927             -- FIRM_FLAG,
6928             resource_hours,
6929             department_id,
6930 	    -- setup_id,
6931 	    SERIAL_NUMBER,
6932             group_sequence_id,
6933             group_sequence_number,
6934             batch_id,
6935             resource_seq_num,
6936             schedule_seq_num
6937             , assigned_units -- jguo
6938             , parent_seq_num
6939  )
6940     SELECT
6941            SYSDATE,
6942            arg_user_id,
6943            s.last_update_login,
6944            SYSDATE,
6945            arg_user_id,
6946            tp.organization_type,
6947            s.organization_id,
6948            arg_wip_group_id,
6949            s.transaction_id,
6950            resreq.OPERATION_SEQ_NUM,
6951           -- resreq.RESOURCE_SEQ_NUM,
6952            resreq.resource_id,
6953            resreq.resource_id,
6954            dep_res_inst.RES_INSTANCE_ID,  -- jguo dep_res_inst.dept_RESOURCE_INST_ID,
6955            nvl(resreq.firm_start_date,res_instreq.START_DATE),
6956            nvl(resreq.firm_end_date,res_instreq.END_DATE),
6957            SUBST_ADD, -- jguo SUBST_CHANGE,
6958            LT_RESOURCE_INSTANCE,
6959            2,
6960            1,
6961            s.sr_instance_id,
6962            resreq.operation_sequence_id,
6963         -- res_instreq.firm_flag,
6964            resreq.resource_hours,
6965            resreq.department_id,
6966           -- resreq.department_id / 2, rawasthi
6967         -- res_instreq.setup_id,
6968            dep_res_inst.serial_number,
6969            resreq.group_sequence_id,
6970            resreq.group_sequence_number,
6971            res_instreq.batch_number,
6972  ---- sbala res_instreq.res_inst_batch_id
6973            resreq.orig_resource_seq_num,
6974            resreq.resource_seq_num
6975         -- resreq.alternate_num,
6976          , 1 -- jguo
6977          , resreq.parent_seq_num
6978     FROM
6979           msc_trading_partners   tp,
6980           msc_resource_requirements resreq,
6981           msc_resource_instance_reqs res_instreq,
6982           msc_dept_res_instances dep_res_inst,
6983           msc_supplies            s
6984     WHERE
6985          tp.sr_tp_id=s.organization_id
6986  AND     tp.sr_instance_id= s.sr_instance_id
6987  AND     tp.partner_type=3
6988  AND     resreq.sr_instance_id= s.sr_instance_id
6989  AND     resreq.organization_id= s.organization_id
6990  AND     resreq.supply_id = s.transaction_id
6991  AND     resreq.plan_id   = s.plan_id
6992 -- dsr AND     resreq.transaction_id = res_instreq.res_inst_transaction_id
6993  AND     resreq.resource_seq_num = res_instreq.resource_seq_num
6994  AND     resreq.operation_seq_num = res_instreq.operation_seq_num
6995  AND     resreq.resource_id = res_instreq.resource_id
6996  AND     resreq.supply_id = res_instreq.supply_id
6997  AND     resreq.sr_instance_id = res_instreq.sr_instance_id
6998  AND     resreq.plan_id = res_instreq.plan_id
6999  AND     resreq.parent_id = res_instreq.parent_id  --rawasthi
7000  AND     resreq.start_date = res_instreq.start_date
7001  AND     resreq.parent_id   = 2
7002  AND     resreq.resource_id <> -1
7003  AND     resreq.department_id <> -1
7004 -- dsr   AND     res_instreq.res_instance_id = dep_res_inst.dept_resource_inst_id
7005  AND      res_instreq.plan_id = dep_res_inst.plan_id
7006  AND      res_instreq.sr_instance_id = dep_res_inst.sr_instance_id
7007  AND      res_instreq.department_id = dep_res_inst.department_id
7008  AND      res_instreq.resource_id = dep_res_inst.resource_id
7009 /*anuj_review: join condition for serial number and res_instance_id */
7010  AND      res_instreq.serial_number = dep_res_inst.serial_number
7011  AND      res_instreq.res_instance_id = dep_res_inst.res_instance_id
7012  AND      s.transaction_id= lv_transaction_id(j)
7013  AND      s.sr_instance_id= lv_instance_id(j)
7014  AND      s.plan_id= arg_plan_id
7015  AND      lv_details_enabled(j)= 1
7016  AND      lv_agg_details(j) = 1
7017     ;
7018 
7019    print_debug_info('load_wip_discrete_jobs# rows inserted into MSC_WIP_JOB_DTLS_INTERFACE = ' || SQL%ROWCOUNT);
7020     -- RESOURCE INSTANCE USAGES
7021    print_debug_info('RESOURCE INSTANCE USAGES ');
7022 
7023 	FORALL j IN 1..lv_job_count
7024     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
7025           ( last_update_date,
7026             last_updated_by,
7027             last_update_login,
7028             creation_date,
7029             created_by,
7030             organization_type,
7031             organization_id,
7032             group_id,
7033             parent_header_id,
7034             operation_seq_num,
7035             -- resource_seq_num,
7036             resource_id_old,
7037 	    resource_id_new, -- xx RESOURCE_ID, ???
7038             RESOURCE_INSTANCE_ID,
7039             start_date,
7040             completion_date,
7041             SUBSTITUTION_TYPE,
7042             LOAD_TYPE,
7043             process_phase,
7044             process_status,
7045             SR_INSTANCE_ID,
7046             operation_seq_id,
7047             FIRM_FLAG,
7048             resource_hours,
7049             department_id,
7050             serial_number,
7051             resource_seq_num,
7052             schedule_seq_num
7053          -- alternate_num
7054            , assigned_units -- jguo
7055            ,parent_seq_num
7056  )
7057     SELECT
7058            SYSDATE,
7059            arg_user_id,
7060            s.last_update_login,
7061            SYSDATE,
7062            arg_user_id,
7063            tp.organization_type,
7064            s.organization_id,
7065            arg_wip_group_id,
7066            s.transaction_id,
7067            resreq.OPERATION_SEQ_NUM,
7068           -- resreq.RESOURCE_SEQ_NUM,
7069            resreq.RESOURCE_ID,
7070            resreq.RESOURCE_ID,
7071            dep_res_inst.RES_INSTANCE_ID,  -- jguo dep_res_inst.dept_RESOURCE_INST_ID,
7072            nvl(resreq.firm_start_date,res_instreq.START_DATE),
7073            nvl(resreq.firm_end_date,res_instreq.END_DATE),
7074            SUBST_ADD, -- jguo SUBST_CHANGE,
7075            LT_RESOURCE_INST_USAGE,
7076            2,
7077            1,
7078            s.sr_instance_id,
7079            resreq.operation_sequence_id,
7080            resreq.firm_flag,
7081            res_instreq.resource_instance_hours,
7082            resreq.department_id,
7083          --  resreq.department_id / 2, rawasthi
7084            dep_res_inst.serial_number,
7085            resreq.orig_resource_seq_num,
7086            resreq.resource_seq_num
7087         -- resreq.alternate_num
7088           ,1 -- jguo
7089           , resreq.parent_seq_num
7090   FROM
7091            msc_trading_partners   tp,
7092            msc_resource_requirements resreq,
7093            msc_resource_instance_reqs res_instreq, -- ??? msc_res_inst_requirements res_instreq,
7094            msc_dept_res_instances dep_res_inst,
7095            msc_supplies            s
7096     WHERE
7097             tp.sr_tp_id=s.organization_id
7098     AND     tp.sr_instance_id= s.sr_instance_id
7099     AND     tp.partner_type=3
7100     AND     resreq.sr_instance_id= s.sr_instance_id
7101     AND     resreq.organization_id= s.organization_id
7102     AND     resreq.supply_id = s.transaction_id
7103     AND     resreq.plan_id   = s.plan_id
7104 -- dsr   AND     resreq.transaction_id = res_instreq.res_inst_transaction_id
7105     AND     resreq.resource_seq_num = res_instreq.resource_seq_num
7106     AND     resreq.operation_seq_num = res_instreq.operation_seq_num
7107     AND     resreq.resource_id = res_instreq.resource_id
7108     AND     resreq.supply_id = res_instreq.supply_id
7109     AND     resreq.sr_instance_id = res_instreq.sr_instance_id
7110     AND     resreq.plan_id = res_instreq.plan_id
7111     AND     resreq.parent_id = res_instreq.parent_id  --rawasthi
7112     AND     resreq.start_date = res_instreq.start_date
7113     AND     resreq.parent_id   = 1
7114     AND     resreq.resource_id <> -1
7115     AND     resreq.department_id <> -1
7116 -- dsr   AND     res_instreq.res_instance_id = dep_res_inst.dept_resource_inst_id
7117     AND     res_instreq.department_id = dep_res_inst.department_id
7118     AND     res_instreq.resource_id = dep_res_inst.resource_id
7119     AND     res_instreq.plan_id = dep_res_inst.plan_id
7120     AND     res_instreq.sr_instance_id = dep_res_inst.sr_instance_id
7121 /* anuj: serail number and resource_instance id joins */
7122     AND     res_instreq.serial_number = dep_res_inst.serial_number
7123     AND     res_instreq.res_instance_id = dep_res_inst.res_instance_id
7124     AND     s.transaction_id= lv_transaction_id(j)
7125     AND     s.sr_instance_id= lv_instance_id(j)
7126     AND     s.plan_id= arg_plan_id
7127     AND     lv_details_enabled(j)= 1
7128     AND     lv_agg_details(j) = 1
7129     ;
7130 
7131    print_debug_info('load_wip_discrete_jobs# rows inserted into MSC_WIP_JOB_DTLS_INTERFACE = ' || SQL%ROWCOUNT);
7132 
7133 -- Resource Charges
7134    print_debug_info('Resource Charges ');
7135 
7136 FORALL j IN 1..lv_job_count
7137     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
7138           ( last_update_date,
7139             last_updated_by,
7140             last_update_login,
7141             creation_date,
7142             created_by,
7143             organization_type,
7144             organization_id,
7145             group_id,
7146             parent_header_id,
7147             operation_seq_num,
7148             resource_seq_num,
7149 	    alternate_num,
7150             start_date,
7151             completion_date,
7152 	    required_quantity,
7153 	    charge_number,
7154             SR_INSTANCE_ID,
7155             SUBSTITUTION_TYPE,
7156             LOAD_TYPE,
7157             process_phase,
7158             process_status,
7159             resource_id_old,
7160             resource_id_new,
7161             schedule_seq_num
7162 )
7163     SELECT
7164             SYSDATE,
7165             arg_user_id,
7166             s.last_update_login,
7167             SYSDATE,
7168             arg_user_id,
7169             tp.organization_type,
7170             s.organization_id,
7171             arg_wip_group_id,
7172             s.transaction_id,
7173             resreq.OPERATION_SEQ_NUM,
7174             resreq.ORIG_RESOURCE_SEQ_NUM,
7175             resreq.alternate_num,
7176             Chg.charge_start_datetime,
7177             Chg.charge_end_datetime,
7178             chg.charge_quantity, -- Chg.Planned_Charge_Quantity,
7179             Chg.charge_number,
7180             s.sr_instance_id,
7181             SUBST_ADD, -- jguo SUBST_CHANGE, -- 3
7182             LT_CHARGE_TYPE, --8
7183             2,
7184             1,
7185             resreq.RESOURCE_ID,
7186             resreq.RESOURCE_ID,
7187             resreq.resource_seq_num
7188      FROM
7189            msc_trading_partners   tp,
7190            msc_resource_requirements resreq,
7191            msc_supplies            s,
7192            msc_resource_charges chg
7193     WHERE
7194             tp.sr_tp_id=s.organization_id
7195     AND     tp.sr_instance_id= s.sr_instance_id
7196     AND     tp.partner_type=3
7197     AND     resreq.sr_instance_id= s.sr_instance_id
7198     AND     resreq.organization_id= s.organization_id
7199     AND     resreq.supply_id = s.transaction_id
7200     AND     resreq.plan_id   = s.plan_id
7201     AND     resreq.transaction_id = chg.res_transaction_id -- chg.transaction_id
7202     AND     resreq.plan_id = chg.plan_id
7203     AND     resreq.sr_instance_id = chg.sr_instance_id
7204     AND     resreq.parent_id   = 2
7205     AND     resreq.resource_id <> -1
7206     AND     resreq.department_id <> -1
7207     AND     s.transaction_id= lv_transaction_id(j)
7208     AND     s.sr_instance_id= lv_instance_id(j)
7209     AND     s.plan_id= arg_plan_id
7210     AND     lv_details_enabled(j)= 1
7211     AND     lv_agg_details(j) = 1
7212 	;
7213     END IF;  /* End of l_count = 1 */
7214     -- dsr ends here
7215 
7216     FORALL j IN 1..lv_job_count
7217         update msc_supplies
7218            set releasable = RELEASE_ATTEMPTED
7219                ,batch_id   = nvl(batch_id,g_batch_id)
7220          where plan_id = arg_plan_id
7221            and transaction_id= lv_transaction_id(j);
7222 
7223     RETURN lv_loaded_jobs;
7224 
7225 END load_wip_discrete_jobs;
7226 
7227 FUNCTION reschedule_wip_discrete_jobs
7228 ( arg_plan_id			IN      NUMBER
7229 , arg_log_org_id 		IN 	NUMBER
7230 , arg_org_instance              IN      NUMBER
7231 , arg_owning_org_id 		IN 	NUMBER
7232 , arg_owning_instance           IN      NUMBER
7233 , arg_user_id 			IN 	NUMBER
7234 , arg_wip_group_id 		IN 	NUMBER
7235 , arg_mode                      IN      VARCHAR2
7236 , arg_transaction_id            IN      NUMBER
7237 , l_apps_ver                    IN      VARCHAR2
7238 , arg_load_type                 IN      NUMBER DEFAULT NULL -- dsr
7239 )RETURN NUMBER
7240 IS
7241 
7242    lv_resched_jobs NUMBER;
7243 
7244 
7245    TYPE NumTab  IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
7246  --TYPE RIDTab  IS TABLE OF ROWID  INDEX BY BINARY_INTEGER;
7247 
7248    lv_transaction_id           NumTab;
7249    lv_instance_id              NumTab;
7250    lv_details_enabled          NumTab;
7251    lv_job_count                NUMBER;
7252    lv_plan_id                  NumTab;
7253    lv_org_id                   Numtab;
7254    lv_agg_details              NumTab;
7255 
7256 CURSOR c_release_dtls(p_transaction_id number,
7257                       p_plan_id  number) IS
7258 SELECT count(1)
7259 FROM msc_supplies
7260 WHERE implement_alternate_bom is not null
7261       and transaction_id = p_transaction_id
7262       and rownum < 2
7263 UNION
7264 SELECT count(1)
7265 from msc_exception_details excp
7266 where excp.plan_id = p_plan_id
7267       and number1 = p_transaction_id
7268       and excp.exception_type = 33
7269       and rownum < 2;
7270 
7271 
7272 CURSOR c_plan_type(p_plan_id number) IS
7273 select plan_type
7274 from msc_plans a
7275 where
7276 plan_id = p_plan_id;
7277 
7278 l_count   NUMBER := 0;
7279 l_plan_type  number := 0;
7280 
7281 
7282 BEGIN
7283 
7284   -- we release the discrete job, only if it doesn't use aggregate resources
7285   -- bug 1252659 fix, replace rowid by
7286   --       (transaction_id,sr_instance_id,plan_id)
7287 
7288    l_sql_stmt := ' SELECT s.transaction_id,
7289           s.sr_instance_id,
7290            decode(mp.daily_material_constraints+ mp.daily_resource_constraints+
7291                   mp.weekly_material_constraints+ mp.weekly_resource_constraints+
7292                  mp.period_material_constraints+ mp.period_resource_constraints,12, 2,
7293                DECODE(mpb.bucket_type,1,DECODE( s.implement_quantity,
7294                   s.new_order_quantity, DECODE( s.implement_date,
7295                                                 s.new_schedule_date, 1,
7296                                                 2),
7297                   2),2)),
7298            s.organization_id,
7299            s.plan_id
7300     FROM msc_supplies s,
7301           msc_plans mp,
7302           msc_plan_organizations_v orgs,
7303           msc_plan_buckets mpb
7304     WHERE s.release_errors is NULL
7305     AND   s.implement_quantity > 0
7306     AND   s.organization_id = orgs.planned_organization
7307     AND   s.sr_instance_id = orgs.sr_instance_id
7308     AND   s.plan_id = :arg_plan_id
7309     AND   orgs.plan_id = :arg_plan_id
7310     AND   orgs.organization_id = :arg_owning_org_id
7311     AND   orgs.owning_sr_instance = :arg_owning_instance
7312     AND   orgs.sr_instance_id = :arg_org_instance
7313     and   s.plan_id = mpb.plan_id
7314     and   s.plan_id = mp.plan_id
7315     AND ( (DECODE(s.disposition_status_type ,
7316                   2,DECODE(SIGN( mp.curr_start_date-s.new_schedule_date),
7317                            1,mp.curr_start_date,
7318                            DECODE(SIGN(s.new_schedule_date-mp.curr_cutoff_date),
7319                                  1,mp.curr_cutoff_date,
7320                                  s.new_schedule_date)),
7321                   s.new_schedule_date ) BETWEEN mpb.bkt_start_date
7322                                         AND mpb.bkt_end_date))
7323     and (s.releasable = ' || RELEASABLE || ' or s.releasable is null )';
7324 
7325     IF  v_batch_id_populated = 2 THEN
7326         l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id is NULL ';
7327     ELSE
7328         l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id = ' || g_batch_id || ' ' ;
7329     END IF;
7330 
7331     IF arg_log_org_id <> arg_owning_org_id THEN
7332             l_sql_stmt :=  l_sql_stmt ||'      AND     orgs.planned_organization = ' || arg_log_org_id || ' ' ;
7333     END IF;
7334 
7335     IF arg_mode IS NULL THEN
7336         l_sql_stmt :=  l_sql_stmt ||'      AND s.load_type = ' || arg_load_type || ' ';
7337         IF v_msc_released_only_by_user = 1 THEN
7338             l_sql_stmt :=  l_sql_stmt || ' AND s.last_updated_by = ' || v_user_id || ' ' ;
7339         END IF;
7340     ELSIF arg_mode = 'WF_BATCH' THEN
7341         l_sql_stmt :=  l_sql_stmt ||'      AND s.load_type = ' || arg_load_type || ' ';
7342     ELSIF arg_mode = 'WF' THEN
7343         l_sql_stmt :=  l_sql_stmt ||'      AND s.transaction_id = ' || arg_transaction_id  || '  ' ;
7344     END IF;
7345 
7346     lv_job_count:= 0;
7347     IF arg_mode IS NULL OR arg_mode = 'WF_BATCH' OR arg_mode = 'WF' THEN
7348         EXECUTE IMMEDIATE l_sql_stmt
7349              BULK COLLECT            INTO lv_transaction_id,
7350                                           lv_instance_id,
7351                                           lv_details_enabled,
7352                                           lv_org_id,
7353                                           lv_plan_id
7354                                         USING  arg_plan_id
7355                                             ,arg_plan_id
7356                                             ,arg_owning_org_id
7357                                             ,arg_owning_instance
7358                                             ,arg_org_instance    ;
7359         lv_job_count:= SQL%ROWCOUNT;
7360     END IF;
7361 
7362 
7363     -- -----------------------------------------------------------------------
7364     -- Perform the wip discrete job mass load
7365     -- -----------------------------------------------------------------------
7366     --    Due to we only give PLANNED components, BILL_RTG_EXPLOSION_FLAG
7367     --    is set to 'Y'.
7368 
7369     --DBMS_OUTPUT.PUT_LINE('LOAD_JOB');
7370 
7371     FOR k in 1..lv_job_count
7372        Loop
7373                 Begin
7374                   SELECT 2
7375                   Into lv_agg_details(k)
7376                   FROM msc_department_resources deptres,
7377                        msc_resource_requirements resreq
7378                  WHERE resreq.sr_instance_id= lv_instance_id(k)
7379                    AND resreq.supply_id = lv_transaction_id(k)
7380                    AND resreq.organization_id= lv_org_id(k)
7381                    AND resreq.plan_id   = lv_plan_id(k)
7382                    AND resreq.parent_id   = 2
7383                    AND deptres.plan_id  = resreq.plan_id
7384                    AND deptres.sr_instance_id= resreq.sr_instance_id
7385                    AND deptres.resource_id= resreq.resource_id
7386                    AND deptres.department_id= resreq.department_id
7387                    AND deptres.organization_id= resreq.organization_id
7388                    AND deptres.aggregate_resource_flag= 1
7389                    AND rownum=1;
7390                   Exception
7391                   When no_data_found
7392                   then
7393                   lv_agg_details(k) := 1;
7394                   End;
7395 
7396        End Loop;
7397 
7398     FORALL j IN 1..lv_job_count
7399     INSERT INTO msc_wip_job_schedule_interface
7400             (last_update_date,
7401             last_updated_by,
7402             last_update_login,
7403             creation_date,
7404             created_by,
7405             group_id,
7406             source_code,
7407             organization_id,
7408             organization_type,
7409             status_type,
7410             load_type,
7411             last_unit_completion_date,
7412             first_unit_start_date,
7413             bom_revision_date,
7414             routing_revision_date,
7415             job_name,
7416             firm_planned_flag,
7417             start_quantity,   -- bug 1229891: net_quantity
7418             net_quantity,
7419             wip_entity_id,
7420             demand_class,
7421             project_id,
7422             task_id,
7423 	    schedule_group_id,
7424 	    build_sequence,
7425             line_id,
7426             alternate_bom_designator,
7427 	    alternate_routing_designator,
7428 	    end_item_unit_number,
7429             process_phase,
7430 	    process_status,
7431             BILL_RTG_EXPLOSION_FLAG,
7432             HEADER_ID,
7433             SR_INSTANCE_ID,
7434             uom_code, --Outbound Changes for XML
7435             PRIMARY_ITEM_ID,
7436             source_line_id --Outbound Changes for XML
7437             , schedule_priority -- dsr
7438             , requested_completion_date -- dsr
7439             )
7440     SELECT  SYSDATE,
7441             arg_user_id,
7442             s.last_update_login,
7443             decode(tp.organization_type,2,s.creation_date,SYSDATE),
7444             arg_user_id,
7445             arg_wip_group_id,
7446             'MSC',
7447             msi.organization_id,
7448             tp.organization_type,
7449             DECODE(NVL(s.implement_status_code, s.wip_status_code),
7450                    JOB_CANCELLED,JOB_CANCELLED,NULL),
7451             decode(arg_load_type, WIP_DIS_MASS_RESCHEDULE, 3, 21), /* dsr 3 */
7452             s.implement_date,
7453             s.new_wip_Start_Date,
7454             NULL,
7455             NULL,
7456             s.implement_job_name,
7457             s.implement_firm,
7458             DECODE( tp.organization_type,
7459                     1, DECODE(s.new_order_quantity,
7460                               s.implement_quantity, TO_NUMBER(NULL),
7461                         ((s.new_order_quantity + NVL(s.qty_completed, 0) +
7462                           NVL(s.qty_scrapped, 0)) -
7463                          (s.new_order_quantity - s.implement_quantity))),
7464                     NULL),
7465                 DECODE(s.new_order_quantity,
7466                               s.implement_quantity, TO_NUMBER(NULL),
7467                         ((s.new_order_quantity + NVL(s.qty_completed, 0) +
7468                           NVL(s.qty_scrapped, 0)) -
7469                          (s.new_order_quantity - s.implement_quantity))),
7470             s.disposition_id,
7471             s.implement_demand_class,
7472             s.implement_project_id,
7473             s.implement_task_id,
7474 	    s.implement_schedule_group_id,
7475             s.implement_build_sequence,
7476             s.implement_line_id,
7477        	    s.implement_alternate_bom,
7478 	    s.implement_alternate_routing,
7479 	    s.implement_unit_number,
7480             2,
7481 	    1,
7482             'Y',
7483             s.transaction_id,
7484             s.sr_instance_id,
7485             nvl(s.implement_uom_code,msi.uom_code),
7486             msi.sr_inventory_item_id,
7487             s.transaction_id --Outbound Changes for XML
7488             , s.schedule_priority -- dsr
7489             , s.requested_completion_date -- dsr
7490     FROM    msc_trading_partners tp,
7491             msc_parameters param,
7492             msc_system_items msi,
7493             msc_supplies     s,
7494             msc_plan_organizations_v orgs
7495     WHERE   tp.sr_tp_id= msi.organization_id
7496     AND     tp.sr_instance_id= msi.sr_instance_id
7497     AND     tp.partner_type=3
7498     AND     param.organization_id = msi.organization_id
7499     AND    param.sr_instance_id = msi.sr_instance_id
7500     AND    msi.inventory_item_id = s.inventory_item_id
7501     AND    msi.plan_id = s.plan_id
7502     AND    msi.organization_id = s.organization_id
7503     AND    msi.sr_instance_id  = s.sr_instance_id
7504     AND    s.release_errors is NULL
7505     AND    s.organization_id = orgs.planned_organization
7506     AND    s.sr_instance_id = orgs.sr_instance_id
7507     AND    s.plan_id = orgs.plan_id
7508     AND    orgs.organization_id = arg_owning_org_id
7509     AND    orgs.owning_sr_instance = arg_owning_instance
7510     AND    orgs.plan_id = arg_plan_id
7511     AND    orgs.planned_organization = decode(arg_log_org_id,
7512                                          arg_owning_org_id, orgs.planned_organization,
7513                                           arg_log_org_id)
7514      AND    orgs.sr_instance_id = arg_org_instance
7515     --AND     ((arg_mode is null and s.load_type = WIP_DIS_MASS_RESCHEDULE) or
7516       AND     ((arg_mode is null and s.load_type = arg_load_type) or
7517                 (arg_mode = 'WF' and s.transaction_id = arg_transaction_id))
7518     AND     s.transaction_id= lv_transaction_id(j)
7519     AND     s.sr_instance_id= lv_instance_id(j);
7520 
7521     IF SQL%ROWCOUNT > 0
7522     THEN
7523         lv_resched_jobs := SQL%ROWCOUNT;
7524 
7525     ELSE
7526         lv_resched_jobs := 0;
7527 
7528     END IF;
7529 
7530     -- ------------------------------------------------------------------------
7531     -- Perform the wip discrete job mass resched for the details
7532     -- ------------------------------------------------------------------------
7533 
7534     -- the details are populated, only if the implement date and quantity
7535     -- are the same as the planned date and quantity
7536 
7537     /* If the current plan is DRP plan then check the following conditions
7538        to determin whether we need to release the details or not
7539        1) For planned orders that do not use
7540           substitute components/alternateboms, only the
7541           header level information is released */
7542 
7543     l_count := 1;
7544 
7545     OPEN c_plan_type(arg_plan_id);
7546     FETCH c_plan_type INTO l_plan_type;
7547     CLOSE c_plan_type;
7548 
7549     IF l_plan_type = 5 THEN   -- DRP plan
7550        OPEN  c_release_dtls(arg_transaction_id, arg_plan_id);
7551        FETCH c_release_dtls INTO l_count;
7552        CLOSE c_release_dtls;
7553     END IF;
7554 
7555     /* If it is not a drp plan then l_count = 1 by default so details will be released */
7556     IF l_count = 1 THEN  /* Release details as well */
7557        -- OPERATIONS
7558        FORALL j IN 1..lv_job_count
7559        INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
7560            (last_update_date,
7561             last_updated_by,
7562             last_update_login,
7563             creation_date,
7564             created_by,
7565             organization_type,
7566             organization_id,
7567             group_id,
7568             parent_header_id,
7569             operation_seq_num,
7570             department_id,
7571             description,
7572             standard_operation_id,
7573             first_unit_start_date,
7574             first_unit_completion_date,
7575             last_unit_start_date,
7576             last_unit_completion_date,
7577             minimum_transfer_quantity,
7578             count_point_type,
7579             backflush_flag,
7580             SUBSTITUTION_TYPE,
7581             LOAD_TYPE,
7582 	    process_phase,
7583 	    process_status,
7584             SR_INSTANCE_ID,
7585             operation_seq_id, --Outbound Changes for XML
7586             WIP_ENTITY_ID
7587             , eam_flag -- dsr
7588             )
7589     SELECT  SYSDATE,
7590             arg_user_id,
7591             s.last_update_login,
7592             SYSDATE,
7593             arg_user_id,
7594             tp.organization_type,
7595             s.organization_id,
7596             arg_wip_group_id,
7597             s.transaction_id,
7598             resreq.OPERATION_SEQ_NUM,
7599             NULL,            --department_id,
7600             NULL,   --description,
7601             NULL,   --standard_operation_id,
7602             min(resreq.START_DATE),   --first_unit_start_date,
7603             min(resreq.START_DATE),   --first_unit_completion_date,
7604             max(resreq.END_DATE),     --last_unit_start_date,
7605             max(resreq.END_DATE),     --last_unit_completion_date,
7606             NULL,   --minimum_transfer_quantity,
7607             NULL,   --count_point_type,
7608             NULL,   --backflush_flag,
7609             SUBST_CHANGE,
7610             LT_OPERATION,
7611             2,
7612             1,
7613             s.sr_instance_id,
7614             resreq.operation_sequence_id, --Outbound Changes for XML
7615             s.disposition_id
7616             , decode(arg_load_type, EAM_RESCHEDULE_WORK_ORDER, SYS_YES, SYS_NO) -- dsr
7617       FROM  msc_trading_partners   tp,
7618             msc_resource_requirements resreq,
7619             msc_parameters          param,
7620             msc_system_items        msi,
7621             msc_supplies            s
7622     WHERE   tp.sr_tp_id= msi.organization_id
7623     AND     tp.sr_instance_id= msi.sr_instance_id
7624     AND     tp.partner_type=3
7625     AND     resreq.sr_instance_id= s.sr_instance_id
7626     AND     resreq.organization_id= s.organization_id
7627     AND     resreq.supply_id = s.transaction_id
7628     AND     resreq.plan_id   = s.plan_id
7629     AND     resreq.parent_id   = 2
7630     AND     param.organization_id = msi.organization_id
7631     AND     param.sr_instance_id  = msi.sr_instance_id
7632     AND     msi.inventory_item_id = s.inventory_item_id
7633     AND     msi.plan_id = s.plan_id
7634     AND     msi.organization_id = s.organization_id
7635     AND     msi.sr_instance_id = s.sr_instance_id
7636     AND     s.transaction_id= lv_transaction_id(j)
7637     AND     s.sr_instance_id= lv_instance_id(j)
7638     AND     s.plan_id= arg_plan_id
7639     AND     lv_details_enabled(j)= 1
7640     AND     lv_agg_details(j) = 1
7641     GROUP BY
7642             SYSDATE,
7643             arg_user_id,
7644             tp.organization_type,
7645             s.organization_id,
7646             s.last_update_login,
7647             SYSDATE,
7648             arg_user_id,
7649             arg_wip_group_id,
7650             s.transaction_id,
7651             resreq.OPERATION_SEQ_NUM,
7652             NULL,            --department_id,
7653             LT_OPERATION,    --load_type,
7654             NULL,            --description,
7655             NULL,            --standard_operation_id,
7656             NULL,            --minimum_transfer_quantity,
7657             NULL,            --count_point_type,
7658             NULL,            --backflush_flag,
7659             s.sr_instance_id,
7660             resreq.operation_sequence_id,
7661             s.disposition_id;
7662 
7663 
7664     --DBMS_OUTPUT.PUT_LINE('OPERATION_RESOURCE');
7665 
7666     -- OPERATION RESOURCES
7667     FORALL j IN 1..lv_job_count
7668     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
7669           ( last_update_date,
7670             last_updated_by,
7671             last_update_login,
7672             creation_date,
7673             created_by,
7674             organization_type,
7675             organization_id,
7676             group_id,
7677             parent_header_id,
7678             operation_seq_num,
7679             -- resource_seq_num,
7680             alternate_num,
7681             resource_id_old,
7682             resource_id_new,
7683             usage_rate_or_amount,
7684             scheduled_flag,
7685             applied_resource_units,   --
7686             applied_resource_value,   --
7687             uom_code,
7688             basis_type,     --
7689             activity_id,    --
7690             autocharge_type,     --
7691             standard_rate_flag,  --
7692             start_date,
7693             completion_date,
7694             assigned_units,
7695             SUBSTITUTION_TYPE,
7696             LOAD_TYPE,
7697 	    process_phase,
7698 	    process_status,
7699             description,
7700             SR_INSTANCE_ID,
7701             operation_seq_id, -- Outbound Changes for XML
7702             wip_entity_id,
7703             resource_hours,
7704             department_id
7705             -- dsr: following 9 columns
7706             , firm_flag
7707             , setup_id
7708             , group_sequence_id
7709             , group_sequence_number
7710             , batch_id
7711             , maximum_assigned_units
7712             , parent_seq_num
7713             , eam_flag
7714             , resource_seq_num
7715             , schedule_seq_num
7716 	  )
7717     SELECT  SYSDATE,
7718             arg_user_id,
7719             s.last_update_login,
7720             SYSDATE,
7721             arg_user_id,
7722             tp.organization_type,
7723             s.organization_id,
7724             arg_wip_group_id,
7725             s.transaction_id,
7726             resreq.OPERATION_SEQ_NUM,
7727             -- resreq.RESOURCE_SEQ_NUM,
7728             resreq.ALTERNATE_NUM,
7729             resreq.RESOURCE_ID,
7730             resreq.RESOURCE_ID,
7731             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),
7732             decode(nvl(resreq.schedule_flag,1),-23453,1,1,1,resreq.schedule_flag),
7733             NULL,
7734             NULL,
7735             v_hour_uom,
7736             resreq.basis_type,
7737             NULL,
7738             NULL,
7739             NULL,
7740             nvl(resreq.firm_start_date,resreq.START_DATE),
7741             nvl(resreq.firm_end_date,resreq.END_DATE),
7742             -- decode(l_apps_ver,'3',TO_NUMBER(NULL),resreq.ASSIGNED_UNITS),
7743             resreq.ASSIGNED_UNITS,
7744             decode(resreq.parent_seq_num,null,SUBST_CHANGE,SUBST_ADD),
7745             -- SUBST_CHANGE,
7746             LT_RESOURCE,
7747             2,
7748             1,
7749             NULL,
7750             s.sr_instance_id,
7751             resreq.operation_sequence_id, -- Outbound Changes for XML
7752             s.disposition_id,
7753             resreq.resource_hours,
7754             resreq.department_id
7755              -- dsr: following 9 columns
7756             , resreq.firm_flag
7757             , resreq.setup_id
7758             , resreq.group_sequence_id
7759             , resreq.group_sequence_number
7760             , resreq.batch_number
7761             , resreq.maximum_assigned_units
7762             , resreq.parent_seq_num
7763             , DECODE(arg_load_type, EAM_RESCHEDULE_WORK_ORDER, SYS_YES, SYS_NO) -- dsr: eam_flag
7764             , resreq.orig_resource_seq_num
7765             , resreq.resource_seq_num
7766       FROM  msc_trading_partners   tp,
7767             msc_resource_requirements resreq,
7768             msc_parameters          param,
7769             msc_system_items        msi,
7770             msc_supplies            s
7771     WHERE   tp.sr_tp_id= msi.organization_id
7772     AND     tp.sr_instance_id= msi.sr_instance_id
7773     AND     tp.partner_type=3
7774     AND     resreq.sr_instance_id= s.sr_instance_id
7775     AND     resreq.organization_id= s.organization_id
7776     AND     resreq.supply_id = s.transaction_id
7777     AND     resreq.plan_id   = s.plan_id
7778     AND     resreq.parent_id   = 2
7779     AND     param.organization_id = msi.organization_id
7780     AND     param.sr_instance_id  = msi.sr_instance_id
7781     AND     msi.inventory_item_id = s.inventory_item_id
7782     AND     msi.plan_id = s.plan_id
7783     AND     msi.organization_id = s.organization_id
7784     AND     msi.sr_instance_id = s.sr_instance_id
7785     AND     s.transaction_id= lv_transaction_id(j)
7786     AND     s.sr_instance_id= lv_instance_id(j)
7787     AND     s.plan_id= arg_plan_id
7788     AND     lv_details_enabled(j)= 1
7789     AND     lv_agg_details(j) = 1 ;
7790 
7791 
7792     --DBMS_OUTPUT.PUT_LINE('LOAD COMPONENTS');
7793 
7794     /* UPDATE EXISTING COMPONENTS                      *
7795      |    We should set inventory_item_id_new to NULL  |
7796      *                                                 */
7797     FORALL j IN 1..lv_job_count
7798     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
7799             (last_update_date,
7800             last_updated_by,
7801             last_update_login,
7802             creation_date,
7803             created_by,
7804             organization_type,
7805             organization_id,
7806             group_id,
7807             parent_header_id,
7808             operation_seq_num,
7809             inventory_item_id_old,
7810             inventory_item_id_new,
7811             basis_type,
7812             quantity_per_assembly,
7813             component_yield_factor,
7814             department_id,
7815             wip_supply_type,
7816             date_required,
7817             required_quantity,
7818             quantity_issued,
7819             supply_subinventory,
7820             supply_locator_id,
7821             mrp_net_flag,
7822             mps_required_quantity,
7823             mps_date_required,
7824             SUBSTITUTION_TYPE,
7825             LOAD_TYPE,
7826 	    process_phase,
7827 	    process_status,
7828             description,
7829             SR_INSTANCE_ID,
7830 --            operation_seq_id, -- Outbound Changes for XML
7831             uom_code, --Outbound Changes for XML
7832             wip_entity_id,
7833              eam_flag -- dsr
7834             )
7835     SELECT  SYSDATE,
7836             arg_user_id,
7837             s.last_update_login,
7838             SYSDATE,
7839             arg_user_id,
7840             tp.organization_type,
7841             s.organization_id,
7842             arg_wip_group_id,
7843             s.transaction_id,
7844             nvl(md.op_seq_num,1),
7845             icomp.sr_inventory_item_id,
7846             decode(l_apps_ver,'4',to_number(null),'3',null,icomp.sr_inventory_item_id),
7847             decode(md.component_scaling_type,1,NULL,md.component_scaling_type),
7848             decode(l_apps_ver,'4',TO_NUMBER(NULL),'3',TO_NUMBER(NULL),(md.USING_REQUIREMENT_QUANTITY/s.implement_quantity)),
7849             md.component_yield_factor,
7850             TO_NUMBER(NULL),       --Department_ID
7851             NVL(GET_WIP_SUPPLY_TYPE(s.plan_id, s.sr_instance_id,s.process_seq_id,
7852                                     s.inventory_item_id,md.inventory_item_id,s.organization_id),
7853                     icomp.wip_supply_type),
7854             md.USING_ASSEMBLY_DEMAND_DATE,
7855             decode(l_apps_ver,'4',TO_NUMBER(NULL),'3',TO_NUMBER(NULL),md.USING_REQUIREMENT_QUANTITY),
7856             TO_NUMBER(NULL),  --quantity_issued
7857             TO_CHAR(NULL),     -- Sub Inventory
7858             TO_NUMBER(NULL),   -- Locator ID
7859             1,                 -- MRP_NET_FLAG
7860             decode(l_apps_ver,'4',to_number(null),'3',TO_NUMBER(NULL),md.USING_REQUIREMENT_QUANTITY),
7861             md.USING_ASSEMBLY_DEMAND_DATE,
7862             SUBST_CHANGE,
7863             LT_COMPONENT,
7864             2,
7865             1,
7866             TO_CHAR(NULL),
7867             s.sr_instance_id,
7868 --            md.operation_seq_id,
7869             nvl(s.implement_uom_code, msi.uom_code),
7870             s.disposition_id
7871             , DECODE(arg_load_type, EAM_RESCHEDULE_WORK_ORDER, SYS_YES, SYS_NO) -- dsr: eam_flag
7872       FROM  msc_trading_partners    tp,
7873             msc_system_items        icomp,
7874             msc_demands             md,
7875             msc_parameters          param,
7876             msc_system_items        msi,
7877             msc_supplies            s
7878     WHERE   /*
7879             ( md.SUBST_ITEM_FLAG <> 1
7880               OR md.SUBST_ITEM_FLAG IS NULL) */
7881             not exists (select 'x'
7882                         from msc_exception_details excp
7883                         where excp.plan_id = msi.plan_id
7884                         and excp.number1 = s.transaction_id
7885                         and excp.inventory_item_id = msi.inventory_item_id
7886                         and excp.organization_id = msi.organization_id
7887                         and excp.sr_instance_id = msi.sr_instance_id
7888                         and excp.exception_type = 33
7889                         and excp.number2 = md.inventory_item_id)
7890     AND     tp.sr_tp_id= msi.organization_id
7891     AND     tp.sr_instance_id= msi.sr_instance_id
7892     AND     tp.partner_type=3
7893     AND     icomp.inventory_item_id= md.inventory_item_id
7894     AND     icomp.organization_id= md.organization_id
7895     AND     icomp.sr_instance_id= md.sr_instance_id
7896     AND     icomp.plan_id= md.plan_id
7897     AND     nvl(icomp.wip_supply_type,0) <> 6
7898     AND     md.sr_instance_id= s.sr_instance_id
7899     AND     md.disposition_id= s.transaction_id
7900     AND     md.plan_id= s.plan_id
7901     AND     param.organization_id = msi.organization_id
7902     AND     param.sr_instance_id  = msi.sr_instance_id
7903     AND     msi.inventory_item_id = s.inventory_item_id
7904     AND     msi.plan_id = s.plan_id
7905     AND     msi.organization_id = s.organization_id
7906     AND     msi.sr_instance_id = s.sr_instance_id
7907     AND     s.transaction_id= lv_transaction_id(j)
7908     AND     s.sr_instance_id= lv_instance_id(j)
7909     AND     s.plan_id= arg_plan_id
7910     AND     lv_details_enabled(j)= 1
7911     AND     lv_agg_details(j) = 1;
7912 
7913 
7914     --Load Co-product/by-product component details
7915 
7916     FORALL j IN 1..lv_job_count
7917     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
7918             (last_update_date,
7919             last_updated_by,
7920             last_update_login,
7921             creation_date,
7922             created_by,
7923             organization_type,
7924             organization_id,
7925             group_id,
7926             parent_header_id,
7927             operation_seq_num,
7928             inventory_item_id_old,
7929             inventory_item_id_new,
7930             quantity_per_assembly,
7931             department_id,
7932             wip_supply_type,
7933             date_required,
7934             required_quantity,
7935             quantity_issued,
7936             supply_subinventory,
7937             supply_locator_id,
7938             mrp_net_flag,
7939             mps_required_quantity,
7940             mps_date_required,
7941             SUBSTITUTION_TYPE,
7942             LOAD_TYPE,
7943 	    process_phase,
7944 	    process_status,
7945             description,
7946             SR_INSTANCE_ID,
7947 --            operation_seq_id, -- Outbound Changes for XML
7948             uom_code, --Outbound Changes for XML
7949             wip_entity_id
7950             , eam_flag -- dsr
7951             )
7952     SELECT  SYSDATE,
7953             arg_user_id,
7954             s.last_update_login,
7955             SYSDATE,
7956             arg_user_id,
7957             tp.organization_type,
7958             s.organization_id,
7959             arg_wip_group_id,
7960             s.transaction_id,
7961             nvl(co.operation_seq_num,1),
7962             icomp.sr_inventory_item_id,
7963             decode(l_apps_ver,'4',to_number(null),'3',null,icomp.sr_inventory_item_id),
7964             decode(l_apps_ver,'4',to_number(null),'3',TO_NUMBER(NULL),(co.new_order_quantity/s.implement_quantity)),
7965             TO_NUMBER(NULL),       --Department_ID
7966             NVL(GET_WIP_SUPPLY_TYPE(s.plan_id, s.sr_instance_id,s.process_seq_id,
7967                                     s.inventory_item_id,co.inventory_item_id,s.organization_id),
7968                     icomp.wip_supply_type),
7969             co.new_schedule_date,
7970             decode(l_apps_ver,'4',to_number(null),'3',TO_NUMBER(NULL),co.new_order_quantity),
7971             TO_NUMBER(NULL),  --quantity_issued
7972             TO_CHAR(NULL),     -- Sub Inventory
7973             TO_NUMBER(NULL),   -- Locator ID
7974             1,                 -- MRP_NET_FLAG
7975             decode(l_apps_ver,'4',to_number(null),'3',TO_NUMBER(NULL),co.new_order_quantity),
7976             co.new_schedule_date,
7977             SUBST_CHANGE,
7978             LT_COMPONENT,
7979             2,
7980             1,
7981             TO_CHAR(NULL),
7982             s.sr_instance_id,
7983             nvl(s.implement_uom_code, msi.uom_code),
7984             s.disposition_id
7985              , DECODE(arg_load_type, EAM_RESCHEDULE_WORK_ORDER, SYS_YES, SYS_NO) -- dsr: eam_flag
7986       FROM  msc_trading_partners    tp,
7987             msc_system_items        icomp,
7988             msc_supplies            co,
7989             msc_parameters          param,
7990             msc_system_items        msi,
7991             msc_supplies            s
7992     WHERE   /*not exists (select 'x'
7993                         from msc_exception_details excp
7994                         where excp.plan_id = msi.plan_id
7995                         and excp.number1 = s.transaction_id
7996                         and excp.inventory_item_id = msi.inventory_item_id
7997                         and excp.organization_id = msi.organization_id
7998                         and excp.sr_instance_id = msi.sr_instance_id
7999                         and excp.exception_type = 33
8000                         and excp.number2 = co.inventory_item_id)
8001     AND     */
8002             tp.sr_tp_id             = msi.organization_id
8003     AND     tp.sr_instance_id       = msi.sr_instance_id
8004     AND     tp.partner_type         = 3
8005     AND     icomp.inventory_item_id = co.inventory_item_id
8006     AND     icomp.organization_id   = co.organization_id
8007     AND     icomp.sr_instance_id    = co.sr_instance_id
8008     AND     icomp.plan_id           = co.plan_id
8009     AND     co.sr_instance_id       = s.sr_instance_id
8010     AND     co.disposition_id       = s.transaction_id
8011     AND     co.plan_id              = s.plan_id
8012     AND     co.order_type           = 14 -- Discrete Job Co-products/by-products.
8013     AND     param.organization_id   = msi.organization_id
8014     AND     param.sr_instance_id    = msi.sr_instance_id
8015     AND     msi.inventory_item_id   = s.inventory_item_id
8016     AND     msi.plan_id             = s.plan_id
8017     AND     msi.organization_id     = s.organization_id
8018     AND     msi.sr_instance_id      = s.sr_instance_id
8019     AND     s.transaction_id        = lv_transaction_id(j)
8020     AND     s.sr_instance_id        = lv_instance_id(j)
8021     AND     s.plan_id               = arg_plan_id
8022     AND     lv_details_enabled(j)   = 1
8023     AND     lv_agg_details(j)       = 1
8024     AND     tp.organization_type    = 2;
8025 
8026 
8027     --DBMS_OUTPUT.PUT_LINE('RESOURCE_USAGE');
8028 
8029     -- RESOURCE USAGE
8030     FORALL j IN 1..lv_job_count
8031     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
8032           ( last_update_date,
8033             last_updated_by,
8034             last_update_login,
8035             creation_date,
8036             created_by,
8037             organization_type,
8038             organization_id,
8039             group_id,
8040             parent_header_id,
8041             operation_seq_num,
8042             -- resource_seq_num,
8043             alternate_num,
8044             resource_id_old,
8045             resource_id_new,
8046             usage_rate_or_amount,
8047             scheduled_flag,
8048             applied_resource_units,   --
8049             applied_resource_value,   --
8050             uom_code,
8051             basis_type,     --
8052             activity_id,    --
8053             autocharge_type,     --
8054             standard_rate_flag,  --
8055             start_date,
8056             completion_date,
8057             assigned_units,
8058             SUBSTITUTION_TYPE,
8059             LOAD_TYPE,
8060 	    process_phase,
8061 	    process_status,
8062             description,
8063             SR_INSTANCE_ID,
8064             operation_seq_id, -- Outbound Changes for XML
8065             wip_entity_id,
8066             department_id,
8067             resource_hours
8068             , eam_flag -- dsr
8069             , resource_seq_num
8070             , schedule_seq_num
8071             ,parent_seq_num
8072             )
8073     SELECT  SYSDATE,
8074             arg_user_id,
8075             s.last_update_login,
8076             SYSDATE,
8077             arg_user_id,
8078             tp.organization_type,
8079             s.organization_id,
8080             arg_wip_group_id,
8081             s.transaction_id,
8082             resreq.OPERATION_SEQ_NUM,
8083            -- resreq.RESOURCE_SEQ_NUM,
8084             resreq.ALTERNATE_NUM,
8085             resreq.RESOURCE_ID,
8086             resreq.RESOURCE_ID,
8087             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),
8088             decode(nvl(resreq.schedule_flag,1),-23453,1,1,1,resreq.schedule_flag),
8089             NULL,
8090             NULL,
8091             v_hour_uom,
8092             resreq.basis_type,
8093             NULL,
8094             NULL,
8095             NULL,
8096             nvl(resreq.firm_start_date,resreq.START_DATE),
8097             nvl(resreq.firm_end_date,resreq.END_DATE),
8098             -- decode(l_apps_ver,'3',TO_NUMBER(NULL),resreq.ASSIGNED_UNITS),
8099             resreq.ASSIGNED_UNITS,
8100             SUBST_CHANGE,
8101             LT_RESOURCE_USAGE,
8102             2,
8103             1,
8104             NULL,
8105             s.sr_instance_id,
8106             resreq.operation_sequence_id, -- Outbound Changes for XML
8107             s.disposition_id,
8108             resreq.department_id,
8109             resreq.resource_hours
8110             , DECODE(arg_load_type, EAM_RESCHEDULE_WORK_ORDER, SYS_YES, SYS_NO) -- dsr: eam_flag
8111             , resreq.orig_resource_seq_num
8112             , resreq.resource_seq_num
8113             ,resreq.parent_seq_num
8114       FROM  msc_trading_partners   tp,
8115             msc_resource_requirements resreq,
8116             msc_parameters          param,
8117             msc_system_items        msi,
8118             msc_supplies            s
8119     WHERE   tp.sr_tp_id= msi.organization_id
8120     AND     tp.sr_instance_id= msi.sr_instance_id
8121     AND     tp.partner_type=3
8122     AND     resreq.sr_instance_id= s.sr_instance_id
8123     AND     resreq.organization_id= s.organization_id
8124     AND     resreq.supply_id = s.transaction_id
8125     AND     resreq.plan_id   = s.plan_id
8126     AND     resreq.parent_id   = 1
8127     AND     param.organization_id = msi.organization_id
8128     AND     param.sr_instance_id  = msi.sr_instance_id
8129     AND     msi.inventory_item_id = s.inventory_item_id
8130     AND     msi.plan_id = s.plan_id
8131     AND     msi.organization_id = s.organization_id
8132     AND     msi.sr_instance_id = s.sr_instance_id
8133     AND     s.transaction_id= lv_transaction_id(j)
8134     AND     s.sr_instance_id= lv_instance_id(j)
8135     AND     s.plan_id= arg_plan_id
8136     AND     lv_details_enabled(j)= 1
8137     AND     lv_agg_details(j) = 1
8138      AND     tp.organization_type IN (1, 2);  -- 1 - discrete wip org; 2 - opm org
8139 
8140     -- dsr starts here
8141     print_debug_info('OPERATION RESOURCE_INSTANCES');
8142 	-- OPERATION RESOURCE_INSTANCES
8143 
8144     FORALL j IN 1..lv_job_count
8145     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
8146           ( last_update_date,
8147             last_updated_by,
8148             last_update_login,
8149             creation_date,
8150             created_by,
8151             organization_type,
8152             organization_id,
8153             group_id,
8154             parent_header_id,
8155             operation_seq_num,
8156            -- resource_seq_num,
8157             resource_id_old, -- rawasthi
8158 	    resource_id_new, -- xx resource_id, ???
8159             RESOURCE_INSTANCE_ID,
8160             start_date,
8161             completion_date,
8162             SUBSTITUTION_TYPE,
8163             LOAD_TYPE,
8164             process_phase,
8165             process_status,
8166             SR_INSTANCE_ID,
8167             operation_seq_id,
8168             -- FIRM_FLAG,
8169             resource_hours,
8170             department_id,
8171 	    -- setup_id,
8172 	    SERIAL_NUMBER,
8173             group_sequence_id,
8174             group_sequence_number,
8175             batch_id,
8176             resource_seq_num
8177             , schedule_seq_num
8178             , wip_entity_id -- for reschedule
8179             , eam_flag -- dsr
8180             , parent_seq_num
8181  )
8182     SELECT
8183           SYSDATE,
8184           arg_user_id,
8185           s.last_update_login,
8186           SYSDATE,
8187           arg_user_id,
8188           tp.organization_type,
8189           s.organization_id,
8190           arg_wip_group_id,
8191           s.transaction_id,
8192           resreq.OPERATION_SEQ_NUM,
8193        --   resreq.RESOURCE_SEQ_NUM,
8194           resreq.resource_id,
8195           resreq.resource_id,
8196   -- jguo dep_res_inst.RESOURCE_INST_ID,
8197           dep_res_inst.RES_INSTANCE_ID ,
8198           nvl(resreq.firm_start_date,res_instreq.START_DATE),
8199           nvl(resreq.firm_end_date,res_instreq.END_DATE),
8200           SUBST_ADD, -- jguo SUBST_CHANGE,
8201           LT_RESOURCE_INSTANCE,
8202           2,
8203           1,
8204           s.sr_instance_id,
8205           resreq.operation_sequence_id,
8206        -- res_instreq.firm_flag,
8207           resreq.resource_hours,
8208           resreq.department_id,
8209          -- resreq.department_id / 2, rawasthi
8210        -- res_instreq.setup_id,
8211           dep_res_inst.serial_number,
8212           resreq.group_sequence_id,
8213           resreq.group_sequence_number,
8214           res_instreq.batch_number,    --- sbala res_instreq.res_inst_batch_id,
8215           resreq.orig_resource_seq_num,
8216           resreq.resource_seq_num
8217       --  resreq.alternate_num,
8218         , s.disposition_id -- for reschedule
8219         , DECODE(arg_load_type, EAM_RESCHEDULE_WORK_ORDER, SYS_YES, SYS_NO) -- dsr: eam_flag
8220         , resreq.parent_seq_num
8221    FROM
8222           msc_trading_partners   tp,
8223           msc_resource_requirements resreq,
8224           msc_resource_instance_reqs res_instreq,
8225           msc_dept_res_instances dep_res_inst,
8226           msc_supplies            s
8227     WHERE
8228          tp.sr_tp_id=s.organization_id
8229  AND     tp.sr_instance_id= s.sr_instance_id
8230  AND     tp.partner_type=3
8231  AND     resreq.sr_instance_id= s.sr_instance_id
8232  AND     resreq.organization_id= s.organization_id
8233  AND     resreq.supply_id = s.transaction_id
8234  AND     resreq.plan_id   = s.plan_id
8235 -- dsr AND     resreq.transaction_id = res_instreq.res_inst_transaction_id
8236  AND     resreq.resource_seq_num = res_instreq.resource_seq_num
8237  AND     resreq.operation_seq_num = res_instreq.operation_seq_num
8238  AND     resreq.resource_id = res_instreq.resource_id
8239  AND     resreq.supply_id = res_instreq.supply_id
8240  AND     resreq.sr_instance_id = res_instreq.sr_instance_id
8241  AND     resreq.plan_id = res_instreq.plan_id
8242  AND     resreq.parent_id = res_instreq.parent_id  --rawasthi
8243  AND     resreq.start_date = res_instreq.start_date
8244  AND     resreq.parent_id   = 2
8245  AND     resreq.resource_id <> -1
8246  AND     resreq.department_id <> -1
8247 -- dsr   AND     res_instreq.res_instance_id = dep_res_inst.dept_resource_inst_id
8248  AND     res_instreq.plan_id = dep_res_inst.plan_id
8249  AND     res_instreq.sr_instance_id = dep_res_inst.sr_instance_id
8250  AND     res_instreq.department_id = dep_res_inst.department_id
8251  AND     res_instreq.resource_id = dep_res_inst.resource_id
8252 /* anuj: serail number and resource_instance id joins */
8253  AND     res_instreq.serial_number = dep_res_inst.serial_number
8254  AND     res_instreq.res_instance_id = dep_res_inst.res_instance_id
8255  AND     s.transaction_id= lv_transaction_id(j)
8256  AND     s.sr_instance_id= lv_instance_id(j)
8257  AND     s.plan_id= arg_plan_id
8258  AND     lv_details_enabled(j)= 1
8259  AND     lv_agg_details(j) = 1
8260     ;
8261 
8262 print_debug_info('reschedule_wip_discrete_jobs: 888 sql%rowcount = '|| SQL%ROWCOUNT);
8263 
8264 
8265     -- RESOURCE INSTANCE USAGES
8266 print_debug_info('RESOURCE INSTANCE USAGES');
8267 	FORALL j IN 1..lv_job_count
8268     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
8269           ( last_update_date,
8270             last_updated_by,
8271             last_update_login,
8272             creation_date,
8273             created_by,
8274             organization_type,
8275             organization_id,
8276             group_id,
8277             parent_header_id,
8278             operation_seq_num,
8279            -- resource_seq_num,
8280             resource_id_old, -- rawasthi
8281 	    resource_id_new, -- xx RESOURCE_ID, ???
8282             RESOURCE_INSTANCE_ID,
8283             start_date,
8284             completion_date,
8285             SUBSTITUTION_TYPE,
8286             LOAD_TYPE,
8287             process_phase,
8288             process_status,
8289             SR_INSTANCE_ID,
8290             operation_seq_id,
8291             FIRM_FLAG,
8292             resource_hours,
8293             department_id,
8294             serial_number,
8295             resource_seq_num,
8296             schedule_seq_num
8297          -- alternate_num
8298      	  , wip_entity_id -- for reschedule
8299 	  , eam_flag -- dsr
8300 	  , assigned_units -- jguo
8301 	  ,parent_seq_num
8302  )
8303     SELECT
8304            SYSDATE,
8305            arg_user_id,
8306            s.last_update_login,
8307            SYSDATE,
8308            arg_user_id,
8309            tp.organization_type,
8310            s.organization_id,
8311            arg_wip_group_id,
8312            s.transaction_id,
8313            resreq.OPERATION_SEQ_NUM,
8314        --    resreq.RESOURCE_SEQ_NUM,
8315            resreq.RESOURCE_ID,
8316            resreq.RESOURCE_ID,
8317   -- jguo  dep_res_inst.RESOURCE_INST_ID,
8318            dep_res_inst.RES_INSTANCE_ID ,
8319            nvl(resreq.firm_start_date,res_instreq.START_DATE),
8320            nvl(resreq.firm_end_date,res_instreq.END_DATE),
8321            SUBST_ADD, -- jguo SUBST_CHANGE,
8322            LT_RESOURCE_INST_USAGE,
8323            2,
8324            1,
8325            s.sr_instance_id,
8326            resreq.operation_sequence_id,
8327            resreq.firm_flag,
8328            res_instreq.resource_instance_hours,
8329            resreq.department_id,
8330           -- resreq.department_id / 2, rawasthi
8331            dep_res_inst.serial_number,
8332            resreq.orig_resource_seq_num,
8333            resreq.resource_seq_num
8334         -- resreq.alternate_num
8335          , s.disposition_id -- for reschedule
8336          , DECODE(arg_load_type, EAM_RESCHEDULE_WORK_ORDER, SYS_YES, SYS_NO) -- dsr: eam_flag
8337          , 1 -- jguo
8338          ,resreq.parent_seq_num
8339     FROM
8340            msc_trading_partners   tp,
8341            msc_resource_requirements resreq,
8342            msc_resource_instance_reqs res_instreq, -- ??? msc_res_inst_requirements res_instreq,
8343            msc_dept_res_instances dep_res_inst,
8344            msc_supplies            s
8345     WHERE
8346             tp.sr_tp_id=s.organization_id
8347     AND     tp.sr_instance_id= s.sr_instance_id
8348     AND     tp.partner_type=3
8349     AND     resreq.sr_instance_id= s.sr_instance_id
8350     AND     resreq.organization_id= s.organization_id
8351     AND     resreq.supply_id = s.transaction_id
8352     AND     resreq.plan_id   = s.plan_id
8353 -- dsr   AND     resreq.transaction_id = res_instreq.res_inst_transaction_id
8354     AND     resreq.resource_seq_num = res_instreq.resource_seq_num
8355     AND     resreq.operation_seq_num = res_instreq.operation_seq_num
8356     AND     resreq.resource_id = res_instreq.resource_id
8357     AND     resreq.supply_id = res_instreq.supply_id
8358     AND     resreq.sr_instance_id = res_instreq.sr_instance_id
8359     AND     resreq.plan_id = res_instreq.plan_id
8360     AND     resreq.parent_id = res_instreq.parent_id  --rawasthi
8361     AND     resreq.start_date = res_instreq.start_date
8362     AND     resreq.parent_id   = 1
8363     AND     resreq.resource_id <> -1
8364     AND     resreq.department_id <> -1
8365 -- dsr   AND     res_instreq.res_instance_id = dep_res_inst.dept_resource_inst_id
8366     AND     res_instreq.department_id = dep_res_inst.department_id
8367     AND     res_instreq.resource_id = dep_res_inst.resource_id
8368     AND     res_instreq.plan_id = dep_res_inst.plan_id
8369     AND     res_instreq.sr_instance_id = dep_res_inst.sr_instance_id
8370 /* anuj: serail number and resource_instance id joins */
8371     AND     res_instreq.serial_number = dep_res_inst.serial_number
8372     AND     res_instreq.res_instance_id = dep_res_inst.res_instance_id
8373     AND     s.transaction_id= lv_transaction_id(j)
8374     AND     s.sr_instance_id= lv_instance_id(j)
8375     AND     s.plan_id= arg_plan_id
8376     AND     lv_details_enabled(j)= 1
8377     AND     lv_agg_details(j) = 1
8378     ;
8379 
8380 print_debug_info('reschedule_wip_discrete_jobs: 999 sql%rowcount = '|| SQL%ROWCOUNT);
8381 
8382 
8383 -- Resource Charges
8384 
8385 print_debug_info('RESOURCE CHARGES');
8386 
8387 FORALL j IN 1..lv_job_count
8388     INSERT INTO MSC_WIP_JOB_DTLS_INTERFACE
8389           ( last_update_date,
8390             last_updated_by,
8391             last_update_login,
8392             creation_date,
8393             created_by,
8394             organization_type,
8395             organization_id,
8396             group_id,
8397             parent_header_id,
8398             operation_seq_num,
8399             resource_seq_num,
8400 	    alternate_num,
8401             start_date,
8402             completion_date,
8403 	    required_quantity,
8404 	    charge_number,
8405             SR_INSTANCE_ID,
8406             SUBSTITUTION_TYPE,
8407             LOAD_TYPE,
8408             process_phase,
8409             process_status
8410           , wip_entity_id -- for reschedule
8411           , eam_flag -- dsr
8412           , resource_id_old
8413           , resource_id_new
8414           , schedule_seq_num
8415        )
8416     SELECT
8417             SYSDATE,
8418             arg_user_id,
8419             s.last_update_login,
8420             SYSDATE,
8421             arg_user_id,
8422             tp.organization_type,
8423             s.organization_id,
8424             arg_wip_group_id,
8425             s.transaction_id,
8426             resreq.OPERATION_SEQ_NUM,
8427             resreq.ORIG_RESOURCE_SEQ_NUM,
8428             resreq.alternate_num,
8429             Chg.charge_start_datetime,
8430             Chg.charge_end_datetime,
8431             chg.charge_quantity, -- Chg.Planned_Charge_Quantity,
8432             Chg.charge_number,
8433             s.sr_instance_id,
8434             SUBST_CHANGE, -- 3
8435             LT_CHARGE_TYPE, --8
8436             2,
8437             1
8438           , s.disposition_id -- for reschedule
8439           , DECODE(arg_load_type, EAM_RESCHEDULE_WORK_ORDER, SYS_YES, SYS_NO) -- dsr: eam_flag
8440           , resreq.RESOURCE_ID
8441           ,  resreq.RESOURCE_ID
8442           ,  resreq.resource_seq_num
8443      FROM
8444              msc_trading_partners   tp,
8445              msc_resource_requirements resreq,
8446              msc_supplies            s,
8447              msc_resource_charges chg
8448     WHERE
8449             tp.sr_tp_id=s.organization_id
8450     AND     tp.sr_instance_id= s.sr_instance_id
8451     AND     tp.partner_type=3
8452     AND     resreq.sr_instance_id= s.sr_instance_id
8453     AND     resreq.organization_id= s.organization_id
8454     AND     resreq.supply_id = s.transaction_id
8455     AND     resreq.plan_id   = s.plan_id
8456     AND     resreq.transaction_id = chg.res_transaction_id -- chg.transaction_id
8457     AND     resreq.plan_id = chg.plan_id
8458     AND     resreq.sr_instance_id = chg.sr_instance_id
8459     AND     resreq.parent_id   = 2
8460     AND     resreq.resource_id <> -1
8461     AND     resreq.department_id <> -1
8462     AND     s.transaction_id= lv_transaction_id(j)
8463     AND     s.sr_instance_id= lv_instance_id(j)
8464     AND     s.plan_id= arg_plan_id
8465     AND     lv_details_enabled(j)= 1
8466     AND     lv_agg_details(j) = 1
8467 	;
8468 
8469 print_debug_info('reschedule_wip_discrete_jobs: aaa sql%rowcount = '|| SQL%ROWCOUNT);
8470 
8471 -- dsr ends here
8472    END IF; /* End of l_count = 1 */
8473 
8474     FORALL j IN 1..lv_job_count
8475        update msc_supplies
8476            set releasable = RELEASE_ATTEMPTED
8477                ,batch_id   = nvl(batch_id,g_batch_id)
8478          where plan_id = arg_plan_id
8479            and transaction_id= lv_transaction_id(j);
8480 
8481     RETURN lv_resched_jobs;
8482 
8483 END reschedule_wip_discrete_jobs;
8484 
8485 FUNCTION load_repetitive_schedules
8486 ( arg_plan_id			IN      NUMBER
8487 , arg_log_org_id 		IN 	NUMBER
8488 , arg_org_instance              IN      NUMBER
8489 , arg_owning_org_id 		IN 	NUMBER
8490 , arg_owning_instance           IN      NUMBER
8491 , arg_user_id 			IN 	NUMBER
8492 , arg_wip_group_id              IN      NUMBER
8493 , arg_mode                      IN      VARCHAR2
8494 , arg_transaction_id            IN      NUMBER
8495 )RETURN NUMBER
8496 
8497 IS
8498    lv_loaded_scheds    NUMBER;
8499 BEGIN
8500 
8501     -- ------------------------------------------------------------------------
8502     -- Perform the wip repetitive schedule mass load
8503     -- ------------------------------------------------------------------------
8504     l_sql_stmt := ' INSERT INTO msc_wip_job_schedule_interface
8505             (last_update_date,
8506             last_updated_by,
8507             last_update_login,
8508             creation_date,
8509             created_by,
8510             group_id,
8511             source_code,
8512             source_line_id,
8513             organization_id,
8514             organization_type,
8515             load_type,
8516             last_unit_completion_date,
8517             bom_revision_date,
8518             routing_revision_date,
8519             processing_work_days,
8520             daily_production_rate,
8521             line_id,
8522             primary_item_id,
8523             firm_planned_flag,
8524             demand_class,
8525 	    process_phase,
8526 	    process_status,
8527             bom_reference_id,
8528             routing_reference_id,
8529             BILL_RTG_EXPLOSION_FLAG,
8530             HEADER_ID,
8531             uom_code, --Outbound Changes for XML
8532             SR_INSTANCE_ID)
8533        SELECT SYSDATE,
8534             :arg_user_id,
8535             s.last_update_login,
8536             SYSDATE,
8537             :arg_user_id,
8538             :arg_wip_group_id,
8539             ''MSC'',
8540             s.transaction_id,
8541             msi.organization_id,
8542             tp.organization_type,
8543             2,
8544             s.implement_date,
8545 			NULL,
8546 			NULL,
8547             s.implement_processing_days,
8548             s.implement_daily_rate,
8549             s.implement_line_id,
8550             msi.sr_inventory_item_id,
8551             s.implement_firm,
8552             s.implement_demand_class,
8553             2,
8554             1,
8555             DECODE( tp.organization_type,
8556                     2, mpe.bill_sequence_id,
8557                     NULL),
8558             DECODE( tp.organization_type,
8559                     2, mpe.routing_sequence_id,
8560                     NULL),
8561             ''Y'',
8562             s.transaction_id,
8563             nvl(s.implement_uom_code,msi.uom_code),
8564             s.sr_instance_id
8565     FROM    msc_trading_partners tp,
8566             msc_parameters param,
8567             msc_system_items msi,
8568             msc_process_effectivity mpe,
8569             msc_supplies     s,
8570             msc_plan_organizations_v orgs
8571     WHERE   tp.sr_tp_id= msi.organization_id
8572     AND     tp.sr_instance_id= msi.sr_instance_id
8573     AND     tp.partner_type=3
8574     AND     param.organization_id = msi.organization_id
8575     AND     param.sr_instance_id = msi.sr_instance_id
8576     AND     msi.plan_id = s.plan_id
8577     AND     msi.organization_id = s.organization_id
8578     AND     msi.sr_instance_id = s.sr_instance_id
8579     AND     msi.inventory_item_id = s.inventory_item_id
8580     AND     mpe.plan_id(+)= s.plan_id
8581     AND     mpe.process_sequence_id(+)= s.process_seq_id
8582     AND	    s.release_errors is NULL
8583     AND     s.implement_daily_rate > 0
8584     AND     s.organization_id = orgs.planned_organization
8585     AND     s.sr_instance_id = orgs.sr_instance_id
8586     AND     s.plan_id = orgs.plan_id
8587     AND     orgs.organization_id = :arg_owning_org_id
8588     AND     orgs.owning_sr_instance = :arg_owning_instance
8589     AND     orgs.plan_id = :arg_plan_id
8590     AND     orgs.sr_instance_id = :arg_org_instance
8591     and    (s.releasable = ' || RELEASABLE || ' or s.releasable is null )' ;
8592 
8593     IF  v_batch_id_populated = 2 THEN
8594         l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id is NULL ';
8595     ELSE
8596         l_sql_stmt :=  l_sql_stmt || ' AND  s.batch_id = ' || g_batch_id || ' ' ;
8597     END IF;
8598 
8599     IF arg_log_org_id <> arg_owning_org_id THEN
8600         l_sql_stmt :=  l_sql_stmt ||'      AND     orgs.planned_organization = ' || arg_log_org_id || ' ' ;
8601     END IF;
8602 
8603     IF arg_mode IS NULL THEN
8604         l_sql_stmt :=  l_sql_stmt ||'  AND s.load_type = ' || WIP_REP_MASS_LOAD || ' ';
8605         IF v_msc_released_only_by_user = 1 THEN
8606             l_sql_stmt :=  l_sql_stmt || ' AND s.last_updated_by = ' || v_user_id || ' ' ;
8607         END IF;
8608     ELSIF arg_mode = 'WF_BATCH' THEN
8609         l_sql_stmt :=  l_sql_stmt ||'  AND s.load_type =  ' || WIP_REP_MASS_LOAD || ' ';
8610     ELSIF arg_mode = 'WF' THEN
8611         l_sql_stmt :=  l_sql_stmt ||'      AND s.transaction_id = ' || arg_transaction_id  || '  ' ;
8612     END IF;
8613 
8614     lv_loaded_scheds:= 0;
8615     IF arg_mode IS NULL OR arg_mode = 'WF_BATCH' OR arg_mode = 'WF' THEN
8616         EXECUTE IMMEDIATE l_sql_stmt USING  arg_user_id
8617                                            ,arg_user_id
8618                                            ,arg_wip_group_id
8619                                            ,arg_owning_org_id
8620                                            ,arg_owning_instance
8621                                            ,arg_plan_id
8622                                            ,arg_org_instance;
8623         lv_loaded_scheds:= SQL%ROWCOUNT;
8624     END IF;
8625 
8626 
8627    IF arg_mode = 'WF' THEN
8628         update msc_supplies
8629            set releasable = RELEASE_ATTEMPTED
8630                ,batch_id   = nvl(batch_id,g_batch_id)
8631          where plan_id = arg_plan_id
8632            and transaction_id= arg_transaction_id;
8633    ELSE
8634         update msc_supplies
8635            set releasable = RELEASE_ATTEMPTED
8636                ,batch_id   = nvl(batch_id,g_batch_id)
8637          where plan_id = arg_plan_id
8638            and transaction_id in (select source_line_id from msc_wip_job_schedule_interface
8639                                    where group_id = arg_wip_group_id);
8640    END IF;
8641 
8642     RETURN lv_loaded_scheds;
8643 
8644 END load_repetitive_schedules;
8645 
8646 FUNCTION load_po_requisitions
8647 ( arg_plan_id			IN      NUMBER
8648 , arg_log_org_id 		IN 	NUMBER
8649 , arg_org_instance              IN      NUMBER
8650 , arg_owning_org_id 		IN 	NUMBER
8651 , arg_owning_instance           IN      NUMBER
8652 , arg_user_id 			IN 	NUMBER
8653 , arg_po_group_by 		IN 	NUMBER
8654 , arg_po_batch_number 		IN 	NUMBER
8655 , arg_mode                      IN      VARCHAR2
8656 , arg_transaction_id            IN      NUMBER
8657 )RETURN NUMBER
8658 IS
8659    lv_loaded_reqs    NUMBER;
8660    lv_errbuf         VARCHAR2(2048);
8661    ln_retcode        NUMBER;
8662    lv_custom         EXCEPTION;
8663 BEGIN
8664 
8665     -- ------------------------------------------------------------------------
8666     -- Perform the po mass load
8667     -- ------------------------------------------------------------------------
8668     --  Check if the profile MRP_PURCHASING_BY_REVISION is set
8669     --  NOTE: We always pass 'VENDOR' as the group by parameter to the req
8670     -- import program.  PO will only look at this parameter if it has failed
8671     -- to find a value in the group code.
8672 
8673 
8674 
8675     INSERT INTO msc_po_requisitions_interface /*+ FIRST_ROWS */
8676             (
8677             /*line_type_id, Amount or Quantity based */
8678 	    last_updated_by,
8679             last_update_date,
8680             last_update_login,
8681             creation_date,
8682             created_by,
8683             item_id,
8684             quantity,
8685             need_by_date,
8686             interface_source_code,
8687             deliver_to_location_id,
8688             deliver_to_requestor_id,
8689             destination_type_code,
8690             preparer_id,
8691             source_type_code,
8692             authorization_status,
8693             uom_code,
8694             batch_id,
8695             charge_account_id,
8696             group_code,
8697             item_revision,
8698             destination_organization_id,
8699             autosource_flag,
8700             org_id,
8701             source_organization_id,
8702             suggested_vendor_id,
8703             /*suggested_vendor_site_id,  -- For Outbound Changes for XML*/
8704             suggested_vendor_site,
8705             project_id,
8706             task_id,
8707 	    end_item_unit_number,
8708             project_accounting_context,
8709             source_line_id, -- Outbound Changes for XML
8710             SR_INSTANCE_ID)
8711      SELECT /*+ FIRST_ROWS */
8712             /*1, Quantity based */
8713             s.last_updated_by,
8714             SYSDATE,
8715             s.last_update_login,
8716             SYSDATE,
8717             s.created_by,
8718             msi.sr_inventory_item_id,
8719             s.implement_quantity,
8720             /*nvl(get_cal_date(s.sr_instance_id,
8721                              cal2.calendar_date,
8722                              mis.delivery_calendar_code), cal2.calendar_date),*/
8723 
8724 	    --implement_dock_date becomes null for manual po
8725 	    nvl(s.implement_dock_date,
8726 		nvl(s.new_dock_date,
8727 			trunc(sysdate)
8728 		)
8729 	    ),
8730 	    'MSC',
8731             s.implement_location_id,
8732             s.implement_employee_id,
8733             'INVENTORY',
8734             s.implement_employee_id,
8735             DECODE(s.implement_supplier_id,
8736                    NULL, DECODE(s.implement_source_org_id,
8737                                   NULL,to_char(NULL),
8738                                   decode(mp1.MODELED_SUPPLIER_ID,
8739                                           NULL,'INVENTORY', 'VENDOR') ),
8740                    'VENDOR'), -- PO wants us to pass null now -- spob
8741             'APPROVED',
8742             msi.uom_code, --mr.implement_uom_code,
8743             arg_po_batch_number,
8744             decode(mp.organization_type,1,nvl(mpp.material_account,
8745                       decode( msi1.inventory_asset_flag,
8746                               'Y', mp.material_account,
8747 		              nvl(msi1.expense_account, mp.expense_account))),-1),
8748             decode(arg_po_group_by,
8749                 REQ_GRP_ALL_ON_ONE, 'ALL-ON-ONE',
8750                 REQ_GRP_ITEM, to_char(s.inventory_item_id),
8751                 REQ_GRP_BUYER, nvl(to_char(msi.buyer_id),NULL),
8752                 REQ_GRP_PLANNER, nvl(msi.planner_code,'PLANNER'),
8753                 REQ_GRP_VENDOR,  NULL,
8754                 REQ_GRP_ONE_EACH, to_char(-100),
8755                 REQ_GRP_CATEGORY,nvl(to_char(msi.sr_category_id),NULL),
8756                 REQ_GRP_LOCATION,NULL,
8757                 NULL),
8758             DECODE( v_purchasing_by_rev,
8759                     NULL, DECODE( msi.REVISION_QTY_CONTROL_CODE,
8760 			          NOT_UNDER_REV_CONTROL, NULL,
8761                                   msi.revision),
8762 	            PURCHASING_BY_REV, msi.revision,
8763 	            NOT_PURCHASING_BY_REV, NULL),
8764             s.organization_id,
8765             'P',
8766             mp.operating_unit,
8767     /* for single instance if the organization is modelled then the source_sr_instane_id = sr_instance_id
8768        in msc_supplies so we can safely use mp1 for both cases.
8769        For cross-instance releases get the modeled supplier infor */
8770             decode(s.implement_source_org_id, NULL, to_number(NULL),
8771                         decode(mp1.MODELED_SUPPLIER_ID, NULL,s.implement_source_org_id, to_number(NULL) ) ),
8772             decode(s.implement_source_org_id, NULL ,supplier.sr_tp_id,
8773                         decode(mp1.MODELED_SUPPLIER_ID, NULL,to_number(NULL),
8774                                      GET_MODELED_SR_TP_ID(mp1.MODELED_SUPPLIER_ID,s.sr_instance_id)
8775                                )),
8776             decode(s.implement_source_org_id, NULL ,mtps.tp_site_code,
8777                         decode(mp1.MODELED_SUPPLIER_ID, NULL,to_char(NULL),
8778                                      GET_MODELED_TP_SITE_CODE(mp1.MODELED_SUPPLIER_ID,
8779                                                               mp1.MODELED_SUPPLIER_SITE_ID,s.sr_instance_id)
8780                                )),
8781             s.implement_project_id,
8782             s.implement_task_id,
8783   	    s.implement_unit_number,
8784 	    DECODE(s.implement_project_id, NULL, 'N', 'Y'),
8785             s.transaction_id,-- For Outbound Changes for XML
8786             s.sr_instance_id
8787     FROM    msc_projects mpp,
8788             --msc_calendar_dates cal1,
8789             --msc_calendar_dates cal2,
8790             msc_tp_id_lid        supplier,
8791             msc_trading_partner_sites mtps,
8792             msc_trading_partners mp, -- mtl_parameters      mp,
8793             msc_trading_partners mp1,
8794             msc_system_items    msi,
8795             msc_system_items    msi1,
8796             msc_supplies        s,
8797             msc_item_suppliers mis,
8798             msc_plan_organizations_v orgs
8799     WHERE   supplier.tp_id(+)=
8800                 nvl(s.implement_supplier_id, s.source_supplier_id)
8801     AND     supplier.partner_type(+)=1
8802     AND     supplier.sr_instance_id(+)= s.sr_instance_id
8803     AND     mtps.partner_id(+) = nvl(s.implement_supplier_id, s.source_supplier_id)
8804     AND     mtps.partner_type(+) = 1
8805     AND     mtps.partner_site_id(+) = nvl(s.implement_supplier_site_id, s.source_supplier_site_id)
8806     AND     mis.sr_instance_id (+) = s.sr_instance_id
8807     AND     mis.plan_id (+) = s.plan_id
8808     AND     mis.inventory_item_id (+) = s.inventory_item_id
8809     AND     mis.organization_id (+) = s.organization_id
8810     AND     mis.using_organization_id (+) = -1
8811     AND    mis.supplier_id (+) = nvl(s.implement_supplier_id, s.source_supplier_id)
8812     AND     mis.supplier_site_id (+) =  nvl(s.implement_supplier_site_id, s.source_supplier_site_id)
8813     AND     mpp.organization_id (+)= s.organization_id
8814     AND	    mpp.project_id (+)= nvl(s.implement_project_id, -23453)
8815     AND     mpp.plan_id (+)= s.plan_id
8816     AND     mpp.sr_instance_id(+)= s.sr_instance_id
8817     --AND     cal1.sr_instance_id= mp.sr_instance_id
8818     --AND	    cal1.calendar_code = mp.calendar_code
8819     --AND     cal1.exception_set_id = mp.calendar_exception_set_id
8820     --AND     cal1.calendar_date = trunc(s.implement_date)
8821     --AND     cal2.sr_instance_id = cal1.sr_instance_id
8822     -- AND     cal2.calendar_code = cal1.calendar_code
8823     --AND     cal2.exception_set_id = cal1.exception_set_id
8824     --AND     cal2.seq_num = GREATEST(1,NVL(cal1.seq_num, cal1.prior_seq_num) -     NVL(msi.postprocessing_lead_time, 0))
8825     AND     mp.sr_tp_id  = msi.organization_id
8826     AND     mp.sr_instance_id = msi.sr_instance_id
8827     AND     mp.partner_type= 3
8828 AND mp1.sr_tp_id (+)= s.SOURCE_ORGANIZATION_ID
8829 AND mp1.sr_instance_id (+)= s.SOURCE_SR_INSTANCE_ID
8830 AND mp1.partner_type(+)= 3
8831     AND     msi.inventory_item_id = s.inventory_item_id
8832     AND     msi.plan_id = s.plan_id
8833     AND     msi.organization_id = s.organization_id
8834     AND     msi.sr_instance_id = s.sr_instance_id
8835     AND     msi1.plan_id = -1
8836     AND     msi1.sr_instance_id = msi.sr_instance_id
8837     AND     msi1.organization_id = msi.organization_id
8838     AND     msi1.inventory_item_id = msi.inventory_item_id
8839     AND	    s.release_errors is NULL
8840     AND     s.implement_quantity > 0
8841     AND     s.organization_id = orgs.planned_organization
8842     AND     s.sr_instance_id = orgs.sr_instance_id
8843     AND     s.plan_id = orgs.plan_id
8844     AND     orgs.organization_id = arg_owning_org_id
8845     AND     orgs.owning_sr_instance = arg_owning_instance
8846     AND     orgs.plan_id = arg_plan_id
8847     AND     orgs.planned_organization = decode(arg_log_org_id,
8848                     arg_owning_org_id, orgs.planned_organization,
8849                     arg_log_org_id)
8850     AND     orgs.sr_instance_id  = arg_org_instance
8851     AND     (arg_mode is null and s.load_type = PO_MASS_LOAD and s.load_type IS NOT NULL)
8852     AND     s.last_updated_by = decode(v_msc_released_only_by_user,1,v_user_id,s.last_updated_by)
8853     and (s.releasable = RELEASABLE or s.releasable is null )
8854     and  (s.batch_id = g_batch_id or (v_batch_id_populated = 2 and s.batch_id is null) )
8855     UNION ALL
8856     SELECT /*+ FIRST_ROWS */
8857             /*1,  Quantity based */
8858             s.last_updated_by,
8859             SYSDATE,
8860             s.last_update_login,
8861             SYSDATE,
8862             s.created_by,
8863             msi.sr_inventory_item_id,
8864             s.implement_quantity,
8865            /* nvl(get_cal_date(s.sr_instance_id,
8866                              cal2.calendar_date,
8867                              mis.delivery_calendar_code), cal2.calendar_date),*/
8868 
8869 	    --implement_dock_date becomes null for manual po
8870 	    nvl(s.implement_dock_date,
8871 		nvl(s.new_dock_date,
8872 			trunc(sysdate)
8873 		)
8874 	    ),
8875 
8876             'MSC',
8877             s.implement_location_id,
8878             s.implement_employee_id,
8879             'INVENTORY',
8880             s.implement_employee_id,
8881             DECODE(s.implement_supplier_id,
8882                    NULL, DECODE(s.implement_source_org_id,
8883                                   NULL,to_char(NULL),
8884                                   decode(mp1.MODELED_SUPPLIER_ID,
8885                                           NULL,'INVENTORY', 'VENDOR') ),
8886                    'VENDOR'), -- PO wants us to pass null now -- spob
8887             'APPROVED',
8888             msi.uom_code, --mr.implement_uom_code,
8889             arg_po_batch_number,
8890             decode(mp.organization_type,1,nvl(mpp.material_account,
8891                       decode( msi1.inventory_asset_flag,
8892                               'Y', mp.material_account,
8893 		              nvl(msi1.expense_account, mp.expense_account))),-1),
8894             decode(arg_po_group_by,
8895                 REQ_GRP_ALL_ON_ONE, 'ALL-ON-ONE',
8896                 REQ_GRP_ITEM, to_char(s.inventory_item_id),
8897                 REQ_GRP_BUYER, nvl(to_char(msi.buyer_id),NULL),
8898                 REQ_GRP_PLANNER, nvl(msi.planner_code,'PLANNER'),
8899                 REQ_GRP_VENDOR,  NULL,
8900                 REQ_GRP_ONE_EACH, to_char(-100),
8901                 REQ_GRP_CATEGORY,nvl(to_char(msi.sr_category_id),NULL),
8902                 REQ_GRP_LOCATION,NULL,
8903                 NULL),
8904             DECODE( v_purchasing_by_rev,
8905                     NULL, DECODE( msi.REVISION_QTY_CONTROL_CODE,
8906 			          NOT_UNDER_REV_CONTROL, NULL,
8907                                   msi.revision),
8908 	            PURCHASING_BY_REV, msi.revision,
8909 	            NOT_PURCHASING_BY_REV, NULL),
8910             s.organization_id,
8911             'P',
8912             mp.operating_unit,
8913             decode(s.implement_source_org_id, NULL, to_number(NULL),
8914                         decode(mp1.MODELED_SUPPLIER_ID, NULL,s.implement_source_org_id, to_number(NULL) ) ),
8915             decode(s.implement_source_org_id, NULL ,supplier.sr_tp_id,
8916                         decode(mp1.MODELED_SUPPLIER_ID, NULL,to_number(NULL),
8917                                      GET_MODELED_SR_TP_ID(mp1.MODELED_SUPPLIER_ID,s.sr_instance_id)
8918                                )),
8919             decode(s.implement_source_org_id, NULL ,mtps.tp_site_code,
8920                         decode(mp1.MODELED_SUPPLIER_ID, NULL,to_char(NULL),
8921                                      GET_MODELED_TP_SITE_CODE(mp1.MODELED_SUPPLIER_ID,
8922                                                               mp1.MODELED_SUPPLIER_SITE_ID,s.sr_instance_id)
8923                                )),
8924             s.implement_project_id,
8925             s.implement_task_id,
8926   	    s.implement_unit_number,
8927 	    DECODE(s.implement_project_id, NULL, 'N', 'Y'),
8928             s.transaction_id, -- Outbound Changes for XML
8929             s.sr_instance_id
8930     FROM    msc_projects mpp,
8931            -- msc_calendar_dates cal1,
8932            -- msc_calendar_dates cal2,
8933             msc_tp_id_lid        supplier,
8934             msc_trading_partner_sites mtps,
8935             msc_trading_partners mp, -- mtl_parameters      mp,
8936             msc_trading_partners mp1,
8937             msc_system_items    msi,
8938             msc_system_items    msi1,
8939             msc_item_suppliers mis,
8940             msc_supplies        s,
8941             msc_plan_organizations_v orgs
8942     WHERE   supplier.tp_id(+)=
8943                 nvl(s.implement_supplier_id, s.source_supplier_id)
8944     AND     supplier.partner_type(+)=1
8945     AND     supplier.sr_instance_id(+)= s.sr_instance_id
8946     AND     mtps.partner_id(+) = nvl(s.implement_supplier_id, s.source_supplier_id)
8947     AND     mtps.partner_type(+) = 1
8948     AND     mtps.partner_site_id(+) = nvl(s.implement_supplier_site_id, s.source_supplier_site_id)
8949     AND     mis.sr_instance_id (+) = s.sr_instance_id
8950     AND     mis.plan_id (+) = s.plan_id
8951     AND     mis.inventory_item_id (+) = s.inventory_item_id
8952     AND     mis.organization_id (+) = s.organization_id
8953     AND     mis.using_organization_id (+) = -1
8954     AND    mis.supplier_id (+) = nvl(s.implement_supplier_id, s.source_supplier_id)
8955     AND     mis.supplier_site_id (+) =  nvl(s.implement_supplier_site_id, s.source_supplier_site_id)
8956     AND     mpp.organization_id (+)= s.organization_id
8957     AND	    mpp.project_id (+)= nvl(s.implement_project_id, -23453)
8958     AND     mpp.plan_id (+)= s.plan_id
8959     AND     mpp.sr_instance_id(+)= s.sr_instance_id
8960    -- AND     cal1.sr_instance_id= mp.sr_instance_id
8961    -- AND	    cal1.calendar_code = mp.calendar_code
8962     --AND     cal1.exception_set_id = mp.calendar_exception_set_id
8963    -- AND     cal1.calendar_date = trunc(s.implement_date)
8964    -- AND     cal2.sr_instance_id = cal1.sr_instance_id
8965    -- AND     cal2.calendar_code = cal1.calendar_code
8966    -- AND     cal2.exception_set_id = cal1.exception_set_id
8967    -- AND     cal2.seq_num = GREATEST(1,NVL(cal1.seq_num, cal1.prior_seq_num) -     NVL(msi.postprocessing_lead_time, 0))
8968     AND     mp.sr_tp_id  = msi.organization_id
8969     AND     mp.sr_instance_id = msi.sr_instance_id
8970     AND     mp.partner_type= 3
8971 AND mp1.sr_tp_id (+)= s.SOURCE_ORGANIZATION_ID
8972 AND mp1.sr_instance_id (+)= s.SOURCE_SR_INSTANCE_ID
8973 AND mp1.partner_type(+)= 3
8974     AND     msi.inventory_item_id = s.inventory_item_id
8975     AND     msi.plan_id = s.plan_id
8976     AND     msi.organization_id = s.organization_id
8977     AND     msi.sr_instance_id = s.sr_instance_id
8978     AND     msi1.plan_id = -1
8979     AND     msi1.sr_instance_id = msi.sr_instance_id
8980     AND     msi1.organization_id = msi.organization_id
8981     AND     msi1.inventory_item_id = msi.inventory_item_id
8982     AND	    s.release_errors is NULL
8983     AND     s.implement_quantity > 0
8984     AND     s.organization_id = orgs.planned_organization
8985     AND     s.sr_instance_id = orgs.sr_instance_id
8986     AND     s.plan_id = orgs.plan_id
8987     AND     orgs.organization_id = arg_owning_org_id
8988     AND     orgs.owning_sr_instance = arg_owning_instance
8989     AND     orgs.plan_id = arg_plan_id
8990     AND     orgs.planned_organization = decode(arg_log_org_id,
8991                     arg_owning_org_id, orgs.planned_organization,
8992                     arg_log_org_id)
8993     AND     orgs.sr_instance_id  = arg_org_instance
8994     AND     (arg_mode = 'WF_BATCH' and s.load_type = PO_MASS_LOAD and s.load_type IS NOT NULL)
8995     and (s.releasable = RELEASABLE or s.releasable is null )
8996     and  (s.batch_id = g_batch_id or (v_batch_id_populated = 2 and s.batch_id is null) )
8997     UNION ALL
8998     SELECT /*+ FIRST_ROWS */
8999             /*1,  Quantity based */
9000             s.last_updated_by,
9001             SYSDATE,
9002             s.last_update_login,
9003             SYSDATE,
9004             s.created_by,
9005             msi.sr_inventory_item_id,
9006             s.implement_quantity,
9007             /*nvl(get_cal_date(s.sr_instance_id,
9008                              cal2.calendar_date,
9009                              mis.delivery_calendar_code), cal2.calendar_date),*/
9010 
9011 	    --implement_dock_date becomes null for manual po
9012 	    nvl(s.implement_dock_date,
9013 		nvl(s.new_dock_date,
9014 			trunc(sysdate)
9015 		)
9016 	    ),
9017 
9018 	    'MSC',
9019             s.implement_location_id,
9020             s.implement_employee_id,
9021             'INVENTORY',
9022             s.implement_employee_id,
9023             DECODE(s.implement_supplier_id,
9024                    NULL, DECODE(s.implement_source_org_id,
9025                                   NULL,to_char(NULL),
9026                                   decode(mp1.MODELED_SUPPLIER_ID,
9027                                           NULL,'INVENTORY', 'VENDOR') ),
9028                    'VENDOR'), -- PO wants us to pass null now -- spob
9029             'APPROVED',
9030             msi.uom_code, --mr.implement_uom_code,
9031             arg_po_batch_number,
9032             decode(mp.organization_type,1,nvl(mpp.material_account,
9033                       decode( msi1.inventory_asset_flag,
9034                               'Y', mp.material_account,
9035 		              nvl(msi1.expense_account, mp.expense_account))),-1),
9036             decode(arg_po_group_by,
9037                 REQ_GRP_ALL_ON_ONE, 'ALL-ON-ONE',
9038                 REQ_GRP_ITEM, to_char(s.inventory_item_id),
9039                 REQ_GRP_BUYER, nvl(to_char(msi.buyer_id),NULL),
9040                 REQ_GRP_PLANNER, nvl(msi.planner_code,'PLANNER'),
9041                 REQ_GRP_VENDOR,  NULL,
9042                 REQ_GRP_ONE_EACH, to_char(-100),
9043                 REQ_GRP_CATEGORY,nvl(to_char(msi.sr_category_id),NULL),
9044                 REQ_GRP_LOCATION,NULL,
9045                 NULL),
9046             DECODE( v_purchasing_by_rev,
9047                     NULL, DECODE( msi.REVISION_QTY_CONTROL_CODE,
9048 			          NOT_UNDER_REV_CONTROL, NULL,
9049                                   msi.revision),
9050 	            PURCHASING_BY_REV, msi.revision,
9051 	            NOT_PURCHASING_BY_REV, NULL),
9052             s.organization_id,
9053             'P',
9054             mp.operating_unit,
9055             decode(s.implement_source_org_id, NULL, to_number(NULL),
9056                         decode(mp1.MODELED_SUPPLIER_ID, NULL,s.implement_source_org_id, to_number(NULL) ) ),
9057             decode(s.implement_source_org_id, NULL ,supplier.sr_tp_id,
9058                         decode(mp1.MODELED_SUPPLIER_ID, NULL,to_number(NULL),
9059                                      GET_MODELED_SR_TP_ID(mp1.MODELED_SUPPLIER_ID,s.sr_instance_id)
9060                                )),
9061             decode(s.implement_source_org_id, NULL ,mtps.tp_site_code,
9062                         decode(mp1.MODELED_SUPPLIER_ID, NULL,to_char(NULL),
9063                                      GET_MODELED_TP_SITE_CODE(mp1.MODELED_SUPPLIER_ID,
9064                                                               mp1.MODELED_SUPPLIER_SITE_ID,s.sr_instance_id)
9065                                )),
9066             s.implement_project_id,
9067             s.implement_task_id,
9068   	    s.implement_unit_number,
9069 	    DECODE(s.implement_project_id, NULL, 'N', 'Y'),
9070             s.transaction_id, --outbound changes for XML
9071             s.sr_instance_id
9072     FROM    msc_projects mpp,
9073            -- msc_calendar_dates cal1,
9074            -- msc_calendar_dates cal2,
9075             msc_tp_id_lid        supplier,
9076             msc_trading_partner_sites mtps,
9077             msc_trading_partners mp, -- mtl_parameters      mp,
9078             msc_trading_partners mp1,
9079             msc_system_items    msi,
9080             msc_system_items    msi1,
9081             msc_item_suppliers mis,
9082             msc_supplies        s,
9083             msc_plan_organizations_v orgs
9084     WHERE   supplier.tp_id(+)=
9085                 nvl(s.implement_supplier_id, s.source_supplier_id)
9086     AND     supplier.partner_type(+)=1
9087     AND     supplier.sr_instance_id(+)= s.sr_instance_id
9088     AND     mtps.partner_id(+) = nvl(s.implement_supplier_id, s.source_supplier_id)
9089     AND     mtps.partner_type(+) = 1
9090     AND     mtps.partner_site_id(+) = nvl(s.implement_supplier_site_id, s.source_supplier_site_id)
9091     AND     mis.sr_instance_id (+) = s.sr_instance_id
9092     AND     mis.plan_id (+) = s.plan_id
9093     AND     mis.inventory_item_id (+) = s.inventory_item_id
9094     AND     mis.organization_id (+) = s.organization_id
9095     AND     mis.using_organization_id (+) = -1
9096     AND    mis.supplier_id (+) = nvl(s.implement_supplier_id, s.source_supplier_id)
9097     AND     mis.supplier_site_id (+) =  nvl(s.implement_supplier_site_id, s.source_supplier_site_id)
9098     AND     mpp.organization_id (+)= s.organization_id
9099     AND	    mpp.project_id (+)= nvl(s.implement_project_id, -23453)
9100     AND     mpp.plan_id (+)= s.plan_id
9101     AND     mpp.sr_instance_id(+)= s.sr_instance_id
9102     --AND     cal1.sr_instance_id= mp.sr_instance_id
9103     --AND	    cal1.calendar_code = mp.calendar_code
9104     --AND     cal1.exception_set_id = mp.calendar_exception_set_id
9105     --AND     cal1.calendar_date = trunc(s.implement_date)
9106     --AND     cal2.sr_instance_id = cal1.sr_instance_id
9107    -- AND     cal2.calendar_code = cal1.calendar_code
9108     --AND     cal2.exception_set_id = cal1.exception_set_id
9109     --AND     cal2.seq_num = GREATEST(1,NVL(cal1.seq_num, cal1.prior_seq_num) -                NVL(msi.postprocessing_lead_time, 0))
9110     AND     mp.sr_tp_id  = msi.organization_id
9111     AND     mp.sr_instance_id = msi.sr_instance_id
9112     AND     mp.partner_type= 3
9113 AND mp1.sr_tp_id (+)= s.SOURCE_ORGANIZATION_ID
9114 AND mp1.sr_instance_id (+)= s.SOURCE_SR_INSTANCE_ID
9115 AND mp1.partner_type(+)= 3
9116     AND     msi.inventory_item_id = s.inventory_item_id
9117     AND     msi.plan_id = s.plan_id
9118     AND     msi.organization_id = s.organization_id
9119     AND     msi.sr_instance_id = s.sr_instance_id
9120     AND     msi1.plan_id = -1
9121     AND     msi1.sr_instance_id = msi.sr_instance_id
9122     AND     msi1.organization_id = msi.organization_id
9123     AND     msi1.inventory_item_id = msi.inventory_item_id
9124     AND	    s.release_errors is NULL
9125     AND     s.implement_quantity > 0
9126     AND     s.organization_id = orgs.planned_organization
9127     AND     s.sr_instance_id = orgs.sr_instance_id
9128     AND     s.plan_id = orgs.plan_id
9129     AND     orgs.organization_id = arg_owning_org_id
9130     AND     orgs.owning_sr_instance = arg_owning_instance
9131     AND     orgs.plan_id = arg_plan_id
9132     AND     orgs.planned_organization = decode(arg_log_org_id,
9133                     arg_owning_org_id, orgs.planned_organization,
9134                     arg_log_org_id)
9135     AND     orgs.sr_instance_id  = arg_org_instance
9136     AND     (arg_mode = 'WF' and s.transaction_id = arg_transaction_id)
9137     and (s.releasable = RELEASABLE or s.releasable is null )
9138     and  (s.batch_id = g_batch_id or (v_batch_id_populated = 2 and s.batch_id is null) );
9139 
9140     IF SQL%ROWCOUNT > 0 THEN
9141         lv_loaded_reqs := SQL%ROWCOUNT;
9142     ELSE
9143         lv_loaded_reqs := 0;
9144     END IF;
9145 
9146     update msc_po_requisitions_interface
9147     set group_code =  to_char(msc_po_requisitions_int_s.nextval)
9148     where group_code = '-100'
9149       and batch_id = arg_po_batch_number;
9150 
9151     msc_cl_cleanse.cleanse_release(lv_errbuf,
9152                                    ln_retcode,
9153                                    PO_MASS_LOAD,
9154                                    arg_org_instance,
9155                                    arg_po_batch_number
9156                                    );
9157     IF ln_retcode = 2 THEN --error
9158       raise lv_custom;
9159     END IF;
9160 
9161    IF arg_mode = 'WF' THEN
9162         update msc_supplies
9163            set releasable = RELEASE_ATTEMPTED
9164                ,batch_id   = nvl(batch_id,g_batch_id)
9165          where plan_id = arg_plan_id
9166            and transaction_id= arg_transaction_id;
9167    ELSE
9168         update msc_supplies
9169            set releasable = RELEASE_ATTEMPTED
9170                ,batch_id   = nvl(batch_id,g_batch_id)
9171          where plan_id = arg_plan_id
9172            and transaction_id in (select source_line_id from msc_po_requisitions_interface
9173                                    where batch_id = arg_po_batch_number);
9174    END IF;
9175 
9176     return lv_loaded_reqs;
9177 
9178 END load_po_requisitions;
9179 
9180 FUNCTION reschedule_po
9181 ( arg_plan_id			IN      NUMBER
9182 , arg_log_org_id 		IN 	NUMBER
9183 , arg_org_instance              IN      NUMBER
9184 , arg_owning_org_id 		IN 	NUMBER
9185 , arg_owning_instance           IN      NUMBER
9186 , arg_user_id 			IN 	NUMBER
9187 , arg_po_batch_number 		IN 	NUMBER
9188 , arg_mode                      IN      VARCHAR2
9189 , arg_transaction_id            IN      NUMBER
9190 )RETURN NUMBER
9191 IS
9192    lv_resched_reqs NUMBER;
9193 BEGIN
9194     -- ------------------------------------------------------------------------
9195     -- Perform the po mass reschedule
9196     -- ------------------------------------------------------------------------
9197 
9198     INSERT INTO msc_po_reschedule_interface
9199            (process_id,
9200             quantity,
9201             need_by_date,
9202             line_id,
9203             last_update_date,
9204             last_updated_by,
9205             creation_date,
9206             created_by,
9207             purchase_order_id, -- outbound Changes for XML
9208             po_number,         -- outbound Changes for XML
9209             source_line_id,    -- outbound Changes for XML
9210             uom,               -- outbound Changes for XML
9211             SR_INSTANCE_ID,
9212             batch_id)
9213     SELECT  NULL,
9214             s.implement_quantity,
9215             --cal2.calendar_date,
9216 	    s.implement_dock_date,
9217             s.po_line_id,
9218             SYSDATE,
9219             arg_user_id,
9220             SYSDATE,
9221             arg_user_id,
9222             s.disposition_id,
9223             s.order_number,
9224             s.transaction_id,
9225             nvl(s.implement_uom_code,msi.uom_code),
9226             s.sr_instance_id,
9227             g_batch_id
9228     FROM    --msc_calendar_dates cal1,
9229             --msc_calendar_dates cal2,
9230             msc_trading_partners mp,
9231             msc_system_items msi,
9232             msc_supplies s,
9233             msc_apps_instances mai,  -- IR/ISO resch Proj
9234             msc_plan_organizations_v orgs
9235     WHERE   --cal1.sr_instance_id= mp.sr_instance_id
9236     --AND     cal1.calendar_code = mp.calendar_code
9237     --AND     cal1.exception_set_id = mp.calendar_exception_set_id
9238     --AND     cal1.calendar_date = trunc(s.implement_date)
9239    -- AND     cal2.sr_instance_id= cal1.sr_instance_id
9240     --AND     cal2.calendar_code = cal1.calendar_code
9241     --AND     cal2.exception_set_id = cal1.exception_set_id
9242     --AND     cal2.seq_num = GREATEST(1,NVL(cal1.seq_num, cal1.prior_seq_num) -                NVL(msi.postprocessing_lead_time, 0))
9243          mp.sr_tp_id = msi.organization_id
9244     AND     mp.sr_instance_id = msi.sr_instance_id
9245     AND     mp.partner_type= 3
9246     AND     msi.inventory_item_id = s.inventory_item_id
9247     AND     msi.plan_id = s.plan_id
9248     AND     msi.organization_id = s.organization_id
9249     AND     msi.sr_instance_id = s.sr_instance_id
9250     AND	    s.release_errors is NULL
9251     AND     s.po_line_id IS NOT NULL
9252     AND     s.organization_id = orgs.planned_organization
9253     AND     s.sr_instance_id = orgs.sr_instance_id
9254     AND     s.plan_id = orgs.plan_id
9255     AND     orgs.organization_id = arg_owning_org_id
9256     AND     orgs.owning_sr_instance = arg_owning_instance
9257     AND     orgs.plan_id = arg_plan_id
9258     AND     orgs.planned_organization = decode(arg_log_org_id,
9259                     arg_owning_org_id, orgs.planned_organization,
9260                     arg_log_org_id)
9261     AND     orgs.sr_instance_id = arg_org_instance
9262     AND     s.sr_instance_id = mai.instance_id
9263     AND     ((arg_mode is null and s.load_type = PO_MASS_RESCHEDULE and s.load_type IS NOT NULL)
9264 			 OR     (arg_mode is null
9265 					 and s.load_type = DRP_REQ_RESCHED
9266 					 and mai.APPS_VER <= MSC_UTIL.G_APPS120
9267 					 and s.load_type IS NOT NULL))  -- IR/ISO resch Proj
9268     AND      s.last_updated_by = decode(v_msc_released_only_by_user,1,v_user_id,s.last_updated_by)
9269     and (s.releasable = RELEASABLE or s.releasable is null )
9270     and  (s.batch_id = g_batch_id or (v_batch_id_populated = 2 and s.batch_id is null) )
9271     UNION ALL
9272     SELECT NULL,
9273             s.implement_quantity,
9274            -- cal2.calendar_date,
9275 	    s.implement_dock_date,
9276             s.po_line_id,
9277             SYSDATE,
9278             arg_user_id,
9279             SYSDATE,
9280             arg_user_id,
9281             s.disposition_id,
9282             s.order_number,
9283             s.transaction_id,
9284             nvl(s.implement_uom_code,msi.uom_code),
9285             s.sr_instance_id,
9286             g_batch_id
9287     FROM    --msc_calendar_dates cal1,
9288             --msc_calendar_dates cal2,
9289             msc_trading_partners mp,
9290             msc_system_items msi,
9291             msc_supplies s,
9292             msc_plan_organizations_v orgs
9293     WHERE   --cal1.sr_instance_id= mp.sr_instance_id
9294     --AND     cal1.calendar_code = mp.calendar_code
9295    -- AND     cal1.exception_set_id = mp.calendar_exception_set_id
9296    -- AND     cal1.calendar_date = trunc(s.implement_date)
9297    -- AND     cal2.sr_instance_id= cal1.sr_instance_id
9298     --AND     cal2.calendar_code = cal1.calendar_code
9299    -- AND     cal2.exception_set_id = cal1.exception_set_id
9300     --AND     cal2.seq_num = GREATEST(1,NVL(cal1.seq_num, cal1.prior_seq_num) -      NVL(msi.postprocessing_lead_time, 0))
9301          mp.sr_tp_id = msi.organization_id
9302     AND     mp.sr_instance_id = msi.sr_instance_id
9303     AND     mp.partner_type= 3
9304     AND     msi.inventory_item_id = s.inventory_item_id
9305     AND     msi.plan_id = s.plan_id
9306     AND     msi.organization_id = s.organization_id
9307     AND     msi.sr_instance_id = s.sr_instance_id
9308     AND	    s.release_errors is NULL
9309     AND     s.po_line_id IS NOT NULL
9310     AND     s.organization_id = orgs.planned_organization
9311     AND     s.sr_instance_id = orgs.sr_instance_id
9312     AND     s.plan_id = orgs.plan_id
9313     AND     orgs.organization_id = arg_owning_org_id
9314     AND     orgs.owning_sr_instance = arg_owning_instance
9315     AND     orgs.plan_id = arg_plan_id
9316     AND     orgs.planned_organization = decode(arg_log_org_id,
9317                     arg_owning_org_id, orgs.planned_organization,
9318                     arg_log_org_id)
9319     AND     orgs.sr_instance_id = arg_org_instance
9320     AND     (arg_mode = 'WF' and s.transaction_id = arg_transaction_id)
9321     and (s.releasable = RELEASABLE or s.releasable is null )
9322     and  (s.batch_id = g_batch_id or (v_batch_id_populated = 2 and s.batch_id is null) );
9323 
9324     IF SQL%ROWCOUNT > 0  THEN
9325         lv_resched_reqs := SQL%ROWCOUNT;
9326     ELSE
9327         lv_resched_reqs := 0;
9328     END IF;
9329 
9330    IF arg_mode = 'WF' THEN
9331         update msc_supplies
9332            set releasable = RELEASE_ATTEMPTED
9333                ,batch_id   = nvl(batch_id,g_batch_id)
9334          where plan_id = arg_plan_id
9335            and transaction_id= arg_transaction_id;
9336    ELSE
9337         IF v_batch_id_populated = 1 THEN
9338             update msc_supplies
9339                set releasable = RELEASE_ATTEMPTED
9340              where batch_id = g_batch_id;
9341         ELSE
9342             update msc_supplies
9343                set releasable = RELEASE_ATTEMPTED
9344                    ,batch_id   = g_batch_id
9345              where plan_id = arg_plan_id
9346                and transaction_id in (select source_line_id from msc_po_reschedule_interface);
9347         END IF;
9348    END IF;
9349 
9350     RETURN lv_resched_reqs;
9351 
9352 END reschedule_po;
9353 
9354 FUNCTION reschedule_po_wf
9355 ( arg_dblink                    IN      VARCHAR2
9356 , arg_plan_id			IN      NUMBER
9357 , arg_log_org_id 		IN 	NUMBER
9358 , arg_org_instance              IN      NUMBER
9359 , arg_owning_org_id 		IN 	NUMBER
9360 , arg_owning_instance           IN      NUMBER
9361 , arg_user_id 			IN 	NUMBER
9362 , arg_mode                      IN      VARCHAR2
9363 , arg_transaction_id            IN      NUMBER
9364 )RETURN NUMBER
9365 IS
9366 
9367     lv_return_code		number;
9368     lv_old_need_by_date		date;
9369     lv_new_need_by_date		date;
9370     lv_po_header_id		number;
9371     lv_po_line_id		number;
9372     lv_po_number			varchar2(60);
9373 
9374     lv_sql_stmt                 VARCHAR2(2000);
9375 BEGIN
9376 
9377     -- ------------------------------------------------------------------------
9378     -- Perform PO reschedule : Workflow version
9379     -- ------------------------------------------------------------------------
9380 
9381 
9382 
9383       BEGIN
9384 
9385         SELECT  s.old_schedule_date,
9386                 --cal2.calendar_date,
9387 		s.implement_dock_date,
9388                 s.disposition_id,         --purchase_order_id,
9389                 s.po_line_id,
9390                 s.order_number            --po_number
9391         INTO    lv_old_need_by_date,
9392                 lv_new_need_by_date,
9393                 lv_po_header_id,
9394                 lv_po_line_id,
9395                 lv_po_number
9396         FROM    --msc_calendar_dates cal1,
9397                 --msc_calendar_dates cal2,
9398                 msc_trading_partners mp,
9399                 msc_system_items msi,
9400                 msc_supplies s,
9401                 msc_plan_organizations_v orgs
9402         WHERE   --cal1.sr_instance_id = mp.sr_instance_id
9403         --AND     cal1.calendar_code = mp.calendar_code
9404         --AND     cal1.exception_set_id = mp.calendar_exception_set_id
9405         --AND     cal1.calendar_date = trunc(NVL(s.implement_date,s.new_schedule_date))
9406        -- AND     cal2.sr_instance_id = cal1.sr_instance_id
9407         --AND     cal2.calendar_code = cal1.calendar_code
9408         --AND     cal2.exception_set_id = cal1.exception_set_id
9409        -- AND     cal2.seq_num = GREATEST(1,NVL(cal1.seq_num, cal1.prior_seq_num) -                  NVL(msi.postprocessing_lead_time, 0))
9410              mp.sr_tp_id = msi.organization_id
9411         AND     mp.sr_instance_id = msi.sr_instance_id
9412         AND     mp.partner_type= 3
9413         AND     msi.inventory_item_id = s.inventory_item_id
9414         AND     msi.plan_id = s.plan_id
9415         AND     msi.organization_id = s.organization_id
9416         AND     msi.sr_instance_id = s.sr_instance_id
9417         AND     s.release_errors is NULL
9418         AND     s.po_line_id IS NOT NULL
9419         AND     s.order_type = 1
9420         AND     s.organization_id = orgs.planned_organization
9421         AND     s.plan_id = orgs.plan_id
9422         AND     orgs.organization_id = arg_owning_org_id
9423         AND     orgs.owning_sr_instance = arg_owning_instance
9424         AND     orgs.plan_id = arg_plan_id
9425         AND     orgs.planned_organization = decode(arg_log_org_id,
9426                         arg_owning_org_id, orgs.planned_organization,
9427                         arg_log_org_id)
9428         AND     orgs.sr_instance_id = arg_org_instance
9429         AND     s.transaction_id = arg_transaction_id
9430         and (s.releasable = RELEASABLE or s.releasable is null )
9431         and  (s.batch_id = g_batch_id or (v_batch_id_populated = 2 and s.batch_id is null) );
9432 
9433       EXCEPTION
9434         WHEN NO_DATA_FOUND THEN
9435           null;
9436       END;
9437 
9438 
9439         lv_return_code:= 0;
9440 
9441       IF lv_po_line_id IS NOT NULL THEN
9442 
9443         lv_sql_stmt:=
9444            'BEGIN'
9445         ||'  IF po_reschedule_pkg.reschedule'||arg_dblink||'('
9446                                           ||'   :lv_old_need_by_date,'
9447                                           ||'   :lv_new_need_by_date,'
9448                                           ||'   :lv_po_header_id,'
9449                                           ||'   :lv_po_line_id,'
9450                                           ||'   :lv_po_number) THEN'
9451         ||'     :lv_return_code:= 1;'
9452         ||'  END IF;'
9453         ||' END;';
9454 
9455         EXECUTE IMMEDIATE lv_sql_stmt
9456                 USING  IN lv_old_need_by_date,
9457                        IN lv_new_need_by_date,
9458                        IN lv_po_header_id,
9459                        IN lv_po_line_id,
9460                        IN lv_po_number,
9461                        OUT lv_return_code;
9462        END IF;
9463 
9464         update msc_supplies
9465            set releasable = RELEASE_ATTEMPTED
9466                ,batch_id   = nvl(batch_id,g_batch_id)
9467          where plan_id = arg_plan_id
9468            and transaction_id= arg_transaction_id;
9469 
9470        RETURN lv_return_code;
9471 
9472 END reschedule_po_wf;
9473 
9474 END MSC_REL_PLAN_PUB;