DBA Data[Home] [Help]

PACKAGE BODY: APPS.MSC_ATP_REQ

Source


1 PACKAGE BODY MSC_ATP_REQ AS
2 /* $Header: MSCRATPB.pls 120.48 2011/12/06 10:08:10 vjuluri ship $  */
3 G_PKG_NAME 		CONSTANT VARCHAR2(30) := 'MSC_ATP_REQ';
4 
5 PG_DEBUG varchar2(1) := NVL(FND_PROFILE.value('MSC_ATP_DEBUG'), 'N');
6 
7 -- The resource functions will perform atp_consume and add the infinite time
8 -- fence and populate all the output tables correctly
9 -- The material functions will only execute the sqls. atp_consume and
10 -- adding the itf will be done elsewhere. Also, making sure all 3
11 -- output tables are populated is also done elsewhere.
12 
13 PROCEDURE Print_Dates_Qtys(
14    p_atp_dates          IN MRP_ATP_PUB.date_arr,
15    p_atp_qtys           IN MRP_ATP_PUB.number_arr
16 ) IS
17 BEGIN
18         IF PG_DEBUG in ('Y', 'C') THEN
19                 for i in 1..p_atp_dates.count loop
20                         msc_sch_wb.atp_debug('date: ' || p_atp_dates(i) ||
21                                              ' qty: ' || p_atp_qtys(i));
22                 end loop;
23         END IF;
24 END Print_Dates_Qtys;
25 
26 --4570421
27 FUNCTION INTEGER_SCALING ( p_scale_qty IN NUMBER,
28                            p_scale_multiple IN NUMBER,
29                            p_scale_rounding_variance IN NUMBER,
30                            p_rounding_direction IN NUMBER
31 )
32 RETURN NUMBER IS
33 var number;
34 rem_var number;
35 floor_qty number;
36 floor_deviation number;
37 ceil_qty number;
38 ceil_deviation number;
39 l_Scale_qty number;
40 
41 BEGIN
42 
43   IF PG_DEBUG in ('Y', 'C') THEN
44       msc_sch_wb.atp_debug('Integer Scaling');
45       msc_sch_wb.atp_debug('Integer Scaling : p_scale_qty ' || p_scale_qty);
46       msc_sch_wb.atp_debug('Integer Scaling : p_scale_multiple ' || p_scale_multiple);
47       msc_sch_wb.atp_debug('Integer Scaling : p_scale_rounding_variance ' || p_scale_rounding_variance);
48       msc_sch_wb.atp_debug('Integer Scaling : p_rounding_direction ' || p_rounding_direction);
49   END IF;
50 
51   l_scale_qty := p_scale_qty;
52 
53   --variance
54   var := p_scale_rounding_variance * l_scale_qty/100;
55 
56   IF PG_DEBUG in ('Y', 'C') THEN
57        msc_sch_wb.atp_debug('Integer Scaling: : var ' || var);
58   END IF;
59   --variance remainder
60   rem_var := l_scale_qty / p_scale_multiple;
61 
62   IF ( P_ROUNDING_DIRECTION = 1 ) THEN -- 1 for Down
63       floor_qty := floor(rem_var) * p_scale_multiple;
64       floor_deviation := l_scale_qty - floor_qty;
65       IF (var >= floor_deviation AND floor_qty <> 0) THEN
66          l_scale_qty := floor_qty;
67       END IF;
68   ELSIF ( P_ROUNDING_DIRECTION = 2 )THEN -- 2 for Up
69       ceil_qty := ceil(rem_var) * p_scale_multiple;
70       ceil_deviation := ceil_qty - l_scale_qty;
71       IF (var >= ceil_deviation AND ceil_qty <> 0) THEN
72           l_scale_qty := ceil_qty;
73       END IF;
74       IF PG_DEBUG in ('Y', 'C') THEN
75          msc_sch_wb.atp_debug('Integer Scaling:  Rounding direction: UP, l_scale_qty: ' || l_scale_qty);
76       END IF;
77   ELSIF ( P_ROUNDING_DIRECTION = 0 )THEN --0 for Both
78      IF PG_DEBUG in ('Y', 'C') THEN
79          msc_sch_wb.atp_debug('Integer Scaling:  Rounding direction: BOTH');
80      END IF;
81      floor_qty := floor(rem_var) * p_scale_multiple;
82      floor_deviation := l_scale_qty - floor_qty;
83      ceil_qty := ceil(rem_var) * p_scale_multiple;
84      ceil_deviation := ceil_qty - l_scale_qty;
85      IF ( floor_qty = 0 OR ceil_qty = 0 ) THEN
86         --retain the value
87         IF PG_DEBUG in ('Y', 'C') THEN
88                 msc_sch_wb.atp_debug('retaining the value');
89         END IF;
90      ELSIF (var >= ceil_deviation AND ceil_qty <> 0) THEN --4904094, changed the cindition from var > ceil_deviation to var >= ceil_deviation
91             l_scale_qty := ceil_qty;
92            IF PG_DEBUG in ('Y', 'C') THEN
93               msc_sch_wb.atp_debug('Integer Scaling: Qty CEILed, l_scale_qty: ' || l_scale_qty);
94            END IF;
95      ELSIF (var >= floor_deviation AND floor_qty <> 0) THEN
96            l_scale_qty := floor_qty;
97            IF PG_DEBUG in ('Y', 'C') THEN
98               msc_sch_wb.atp_debug('Integer Scaling: Qty FLOORed, l_scale_qty: ' || l_scale_qty);
99            END IF;
100      END IF;
101   END IF;
102   IF PG_DEBUG in ('Y', 'C') THEN
103       msc_sch_wb.atp_debug('Integer Scaling :l_scale_qty ' || l_scale_qty);
104   END IF;
105 
106   return l_scale_qty;
107 
108 EXCEPTION
109     WHEN ZERO_DIVIDE THEN
110         IF PG_DEBUG in ('Y', 'C') THEN
111            msc_sch_wb.atp_debug('Integer Scaling: Division by Zero');
112         END IF;
113         l_scale_qty := p_scale_multiple;
114         return l_scale_qty;
115 END integer_scaling;
116 
117 -- 2859130
118 -- The final decision on this bug is to not change the sql for unconstrained plan and
119 -- not to change the logic to find the first available bucket. The sqls are moved out
120 -- into separate functions to improve readability
121 -- The only change to the unconstrained plan sqls is to add an itf
122 -- constrained plan
123 PROCEDURE get_res_avail_opt(
124    p_instance_id        IN NUMBER,
125    p_org_id             IN NUMBER,
126    p_plan_id            IN NUMBER,
127    p_plan_start_date    IN DATE,
128    p_dept_id            IN NUMBER,
129    p_res_id             IN NUMBER,
130    p_itf                IN DATE,
131    x_atp_dates          OUT NoCopy MRP_ATP_PUB.date_arr,
132    x_atp_qtys           OUT NoCopy MRP_ATP_PUB.number_arr
133 ) IS
134 l_null_num      NUMBER;
135 l_null_char     VARCHAR2(1);
136 l_sysdate       DATE := trunc(sysdate); --4135752
137 BEGIN
138         IF PG_DEBUG in ('Y', 'C') THEN
139                 msc_sch_wb.atp_debug('get_res_avail_opt');
140         END IF;
141 
142         SELECT  SD_DATE,
143                 SUM(SD_QTY)
144         BULK COLLECT INTO x_atp_dates, x_atp_qtys
145         FROM
146         (
147            SELECT  -- 2859130
148                    --C.CALENDAR_DATE SD_DATE,
149                    -- Bug 3348095
150                    -- For ATP created records use end_date otherwise start_date
151                    DECODE(REQ.record_source, 2, TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
152                            TRUNC(REQ.START_DATE)) SD_DATE,
153                    -1*DECODE(REQ.RESOURCE_ID, -1, REQ.LOAD_RATE,
154                       DECODE(REQ.END_DATE, NULL, REQ.RESOURCE_HOURS,
155                         DECODE(REQ.record_source, 2, REQ.RESOURCE_HOURS,
156                                  REQ.DAILY_RESOURCE_HOURS)))  SD_QTY
157                    -- For ATP created records use resource_hours
158                    -- End Bug 3348095
159            FROM    MSC_DEPARTMENT_RESOURCES DR,
160                    MSC_RESOURCE_REQUIREMENTS REQ,
161                    -- CTO Option Dependent Resources ODR
162                    -- Option Dependent Resources Capacity Check
163                    -- Add Link to Items
164                    MSC_SYSTEM_ITEMS I
165                    -- 2859130
166                    -- MSC_CALENDAR_DATES C
167                    -- Bug 2675504, 2665805,
168            --bug3394866
169            WHERE   DR.PLAN_ID = p_plan_id
170            AND     NVL(DR.OWNING_DEPARTMENT_ID, DR.DEPARTMENT_ID)=p_dept_id
171            AND     DR.RESOURCE_ID = p_res_id
172            AND     DR.SR_INSTANCE_ID = p_instance_id
173            -- krajan: 2408696 --
174            AND     DR.organization_id = p_org_id
175 
176            AND     REQ.PLAN_ID = DR.PLAN_ID
177            AND     REQ.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
178            AND     REQ.RESOURCE_ID = DR.RESOURCE_ID
179            AND     REQ.DEPARTMENT_ID = DR.DEPARTMENT_ID
180            AND     REQ.ORGANIZATION_ID = DR.ORGANIZATION_ID
181            --bug3394866
182            -- End Bug 2675504, 2665805,
183            AND     NVL(REQ.PARENT_ID, 1) = 1 -- parent_id is 1 for constrained plans. Bug 2809639
184            -- CTO Option Dependent Resources ODR
185            -- Option Dependent Resources Capacity Check
186            AND     I.SR_INSTANCE_ID = REQ.SR_INSTANCE_Id
187            AND     I.PLAN_ID = REQ.PLAN_ID
188            AND     I.ORGANIZATION_ID = REQ.ORGANIZATION_ID
189            AND     I.inventory_item_id = REQ.assembly_item_id
190            AND     ((I.bom_item_type <> 1 and I.bom_item_type <> 2)
191                -- bom_item_type not model and option_class always committed.
192                     AND   (I.atp_flag <> 'N')
193                -- atp_flag is 'Y' then committed.
194                     OR    (REQ.record_source = 2) ) -- this OR may be changed during performance analysis.
195               -- if record created by ATP then committed.
196            -- End CTO Option Dependent Resources ODR
197            -- 2859130
198            --AND     C.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
199            --AND     C.CALENDAR_CODE = p_cal_code
200            --AND     C.EXCEPTION_SET_ID = p_cal_exc_set_id
201            --AND     C.CALENDAR_DATE = TRUNC(REQ.START_DATE) -- Bug 2809639
202            -- AND     C.SEQ_NUM IS NOT NULL -- 2859130
203            --bug 2341075: chnage sysdate to plan_start_date
204            --AND     C.CALENDAR_DATE >= trunc(sysdate)
205            --AND     C.CALENDAR_DATE >= p_plan_start_date
206            --bug3693892 added trunc
207            AND TRUNC(REQ.START_DATE) >= p_plan_start_date
208            -- 2859130
209            AND TRUNC(REQ.START_DATE) < trunc(nvl(p_itf,REQ.START_DATE+1))--4135752
210            UNION ALL
211            SELECT  trunc(SHIFT_DATE) SD_DATE,--4135752
212                    CAPACITY_UNITS * ((DECODE(LEAST(from_time, to_time),
213                    to_time,to_time + 24*3600,
214                         to_time) - from_time)/3600) SD_QTY
215            FROM    MSC_NET_RESOURCE_AVAIL
216            WHERE   PLAN_ID = p_plan_id
217            AND     NVL(PARENT_ID, -2) <> -1
218            AND     SR_INSTANCE_ID = p_instance_id
219            AND     RESOURCE_ID = p_res_id
220            AND     DEPARTMENT_ID = p_dept_id
221            --bug 2341075: chnage sysdate to plan_start_date
222            --AND     SHIFT_DATE >= trunc(sysdate)
223            AND     trunc(SHIFT_DATE) >= p_plan_start_date --4135752
224            AND     trunc(SHIFT_DATE) < trunc(nvl(p_itf, SHIFT_DATE+1)) -- 2859130--4135752
225         )
226         GROUP BY SD_DATE
227         ORDER BY SD_DATE;
228 END get_res_avail_opt;
229 
230 -- unconstrained plan
231 PROCEDURE get_res_avail_unopt(
232    p_instance_id        IN NUMBER,
233    p_org_id             IN NUMBER,
234    p_plan_id            IN NUMBER,
235    p_plan_start_date    IN DATE,
236    p_dept_id            IN NUMBER,
237    p_res_id             IN NUMBER,
238    p_itf                IN DATE,
239    p_cal_code           IN VARCHAR2,
240    p_cal_exc_set_id     IN NUMBER,
241    x_atp_dates          OUT NoCopy MRP_ATP_PUB.date_arr,
242    x_atp_qtys           OUT NoCopy MRP_ATP_PUB.number_arr
243 ) IS
244 l_null_num      NUMBER;
245 l_null_char     VARCHAR2(1);
246 l_sysdate       DATE := trunc(sysdate); --4135752
247 BEGIN
248         IF PG_DEBUG in ('Y', 'C') THEN
249                 msc_sch_wb.atp_debug('get_res_avail_unopt');
250         END IF;
251 
252         SELECT  SD_DATE,
253                 SUM(SD_QTY)
254         BULK COLLECT INTO x_atp_dates, x_atp_qtys
255         FROM
256         (
257          SELECT  C.CALENDAR_DATE SD_DATE,
258                  -1*DECODE(REQ.RESOURCE_ID, -1, REQ.LOAD_RATE,
259                     DECODE(REQ.END_DATE, NULL, REQ.RESOURCE_HOURS,
260                    -- Bug 3348095
261                         DECODE(REQ.record_source, 2, REQ.RESOURCE_HOURS,
262                                  REQ.DAILY_RESOURCE_HOURS)))  SD_QTY
263                    -- For ATP created records use resource_hours
264                    -- End Bug 3348095
265          FROM    MSC_DEPARTMENT_RESOURCES DR,
266                  MSC_RESOURCE_REQUIREMENTS REQ,
267                  -- CTO Option Dependent Resources ODR
268                  -- Option Dependent Resources Capacity Check
269                  -- Add Link to Items
270                  MSC_SYSTEM_ITEMS I,
271                  MSC_CALENDAR_DATES C
272                  -- Bug 2675504, 2665805,
273          --bug3394866
274          WHERE   DR.PLAN_ID = p_plan_id
275          AND     NVL(DR.OWNING_DEPARTMENT_ID, DR.DEPARTMENT_ID)=p_dept_id
276          AND     DR.RESOURCE_ID = p_res_id
277          AND     DR.SR_INSTANCE_ID = p_instance_id
278            -- krajan: 2408696 --
279          AND     DR.organization_id = p_org_id
280 
281          AND     REQ.PLAN_ID = DR.PLAN_ID
282          AND     REQ.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
283          AND     REQ.RESOURCE_ID = DR.RESOURCE_ID
284          AND     REQ.DEPARTMENT_ID = DR.DEPARTMENT_ID
285          AND     REQ.ORGANIZATION_ID = DR.ORGANIZATION_ID
286          --bug3394866
287          -- End Bug 2675504, 2665805
288          AND     NVL(REQ.PARENT_ID, MSC_ATP_PVT.G_OPTIMIZED_PLAN) = MSC_ATP_PVT.G_OPTIMIZED_PLAN
289          -- CTO Option Dependent Resources ODR
290          -- Option Dependent Resources Capacity Check
291          AND     I.SR_INSTANCE_ID = REQ.SR_INSTANCE_Id
292          AND     I.PLAN_ID = REQ.PLAN_ID
293          AND     I.ORGANIZATION_ID = REQ.ORGANIZATION_ID
294          AND     I.inventory_item_id = REQ.assembly_item_id
295          AND     ((I.bom_item_type <> 1 and I.bom_item_type <> 2)
296                -- bom_item_type not model and option_class always committed.
297                     AND   (I.atp_flag <> 'N')
298                -- atp_flag is 'Y' then committed.
299                     OR    (REQ.record_source = 2) ) -- this OR may be changed during performance analysis.
300               -- if record created by ATP then committed.
301          -- End CTO Option Dependent Resources ODR
302          AND     C.SR_INSTANCE_ID = REQ.SR_INSTANCE_ID --bug3394866
303          AND     C.CALENDAR_CODE = p_cal_code
304          AND     C.EXCEPTION_SET_ID = p_cal_exc_set_id
305                  -- Bug 3348095
306                  -- Ensure that the ATP created resource Reqs
307                  -- do not get double counted.
308          AND     C.CALENDAR_DATE BETWEEN DECODE(REQ.record_source, 2,
309                           TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)), TRUNC(REQ.START_DATE))
310                    AND TRUNC(NVL(REQ.END_DATE, REQ.START_DATE))
311                  -- End Bug 3348095
312          AND     C.SEQ_NUM IS NOT NULL
313          --bug 2341075: chnage sysdate to plan_start_date
314          --AND     C.CALENDAR_DATE >= trunc(sysdate)
315          AND     C.CALENDAR_DATE >= p_plan_start_date
316          -- 2859130
317          AND     C.CALENDAR_DATE < NVL(p_itf, C.CALENDAR_DATE+1)
318          UNION ALL
319          SELECT  trunc(SHIFT_DATE) SD_DATE,--4135752
320                  CAPACITY_UNITS * ((DECODE(LEAST(from_time, to_time),
321                  to_time,to_time + 24*3600,
322                       to_time) - from_time)/3600) SD_QTY
323          FROM    MSC_NET_RESOURCE_AVAIL
324          WHERE   PLAN_ID = p_plan_id
325          AND     NVL(PARENT_ID, -2) <> -1
326          AND     SR_INSTANCE_ID = p_instance_id
327          AND     RESOURCE_ID = p_res_id
328          AND     DEPARTMENT_ID = p_dept_id
329          --bug 2341075: chnage sysdate to plan_start_date
330          --AND     SHIFT_DATE >= trunc(sysdate)
331          AND     trunc(SHIFT_DATE) >= p_plan_start_date
332          AND     trunc(SHIFT_DATE) < nvl(p_itf, SHIFT_DATE+1) -- 2859130
333          )
334          GROUP BY SD_DATE
335          ORDER BY SD_DATE;
336 END get_res_avail_unopt;
337 
338 -- constrained plan batching
339 PROCEDURE get_res_avail_opt_bat(
340    p_instance_id        IN NUMBER,
341    p_org_id             IN NUMBER,
342    p_plan_id            IN NUMBER,
343    p_plan_start_date    IN DATE,
344    p_dept_id            IN NUMBER,
345    p_res_id             IN NUMBER,
346    p_itf                IN DATE,
347    p_uom_type           IN NUMBER,
348    p_max_capacity       IN NUMBER,
349    p_res_conv_rate      IN NUMBER,
350    x_atp_dates          OUT NoCopy MRP_ATP_PUB.date_arr,
351    x_atp_qtys           OUT NoCopy MRP_ATP_PUB.number_arr
352 ) IS
353 l_null_num      NUMBER;
354 l_null_char     VARCHAR2(1);
355 l_sysdate       DATE := trunc(sysdate);--4135752
356 BEGIN
357         IF PG_DEBUG in ('Y', 'C') THEN
358                 msc_sch_wb.atp_debug('get_res_avail_opt_bat');
359         END IF;
360 
361         SELECT  SD_DATE,
362                 SUM(SD_QTY)
363         BULK COLLECT INTO x_atp_dates, x_atp_qtys
364         FROM
365         (
366            SELECT  -- 2859130 C.CALENDAR_DATE SD_DATE,
367                    -- Bug 3348095
368                    -- For ATP created records use end_date otherwise start_date
369                    DECODE(REQ.record_source, 2, TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)),
370                            TRUNC(REQ.START_DATE)) SD_DATE,
371                    -1*DECODE(REQ.RESOURCE_ID, -1, REQ.LOAD_RATE,
372                        DECODE(REQ.END_DATE, NULL, REQ.RESOURCE_HOURS,
373                       -- Bug 3348095
374                         DECODE(REQ.record_source, 2, REQ.RESOURCE_HOURS,
375                                  REQ.DAILY_RESOURCE_HOURS)))
376                       -- For ATP created records use resource_hours
377                       -- End Bug 3348095
378                            *
379                          DECODE(DR.UOM_CLASS_TYPE, 1, I.UNIT_WEIGHT, 2, UNIT_VOLUME) *
380                           NVL(MUC.CONVERSION_RATE,1) * NVL(S.NEW_ORDER_QUANTITY, S.FIRM_QUANTITY) SD_QTY
381 
382            FROM    MSC_DEPARTMENT_RESOURCES DR,
383                    MSC_RESOURCE_REQUIREMENTS REQ,
384                    -- 2859130 MSC_CALENDAR_DATES C,
385                    --- add table for resource batching
386                    --- these tables are added to determine how much apacity has already been consumed by the
387                    --- existing supplies
388                    MSC_SYSTEM_ITEMS I,
389                    MSC_SUPPLIES S,
390                    MSC_UOM_CONVERSIONS MUC
391            -- Bug 2675504, 2665805,
392            --bug3394866
393            WHERE   DR.PLAN_ID = p_plan_id
394            AND     NVL(DR.OWNING_DEPARTMENT_ID, DR.DEPARTMENT_ID)=p_dept_id
395            AND     DR.RESOURCE_ID = p_res_id
396            AND     DR.SR_INSTANCE_ID = p_instance_id
397            -- krajan: 2408696 --
398            AND     DR.organization_id = p_org_id
399 
400            AND     REQ.PLAN_ID = DR.PLAN_ID
401            AND     REQ.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
402            AND     REQ.RESOURCE_ID = DR.RESOURCE_ID
403            AND     REQ.DEPARTMENT_ID = DR.DEPARTMENT_ID
404            AND     REQ.ORGANIZATION_ID = DR.ORGANIZATION_ID
405            --bug3394866
406            -- End Bug 2675504, 2665805,
407            AND     NVL(REQ.PARENT_ID, 1) = 1 -- parent_id is 1 for constrained plans. Bug 2809639
408            AND     I.SR_INSTANCE_ID = S.SR_INSTANCE_Id
409            AND     I.PLAN_ID = S.PLAN_ID
410            AND     I.ORGANIZATION_ID = S.ORGANIZATION_ID
411            AND     I.INVENTORY_ITEM_ID = S.INVENTORY_ITEM_ID
412            AND     DECODE(p_uom_type, 1, I.WEIGHT_UOM, 2 , I.VOLUME_UOM) = MUC.UOM_CODE (+)
413            AND     MUC.SR_INSTANCE_ID (+) = I.SR_INSTANCE_ID
414            AND     MUC.INVENTORY_ITEM_ID  (+)= 0
415            AND     S.TRANSACTION_ID = REQ.SUPPLY_ID
416            AND     S.PLAN_ID = REQ.PLAN_ID
417            AND     S.SR_INSTANCE_ID = REQ.SR_INSTANCE_ID
418            AND     S.ORGANIZATION_ID = REQ.ORGANIZATION_ID
419                    -- Exclude Cancelled Supplies 2460645
420            AND     NVL(S.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
421            -- CTO Option Dependent Resources ODR
422            -- Option Dependent Resources Capacity Check
423            AND     I.inventory_item_id = REQ.assembly_item_id
424            AND     ((I.bom_item_type <> 1 and I.bom_item_type <> 2)
425                -- bom_item_type not model and option_class always committed.
426                     AND   (I.atp_flag <> 'N')
427                -- atp_flag is 'Y' then committed.
428                     OR    (REQ.record_source = 2) ) -- this OR may be changed during performance analysis.
429               -- if record created by ATP then committed.
430            -- End CTO Option Dependent Resources ODR
431            -- 2859130
432            --AND     C.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
433            --AND     C.CALENDAR_CODE = p_cal_code
434            --AND     C.EXCEPTION_SET_ID = p_cal_exc_set_id
435            --AND     C.CALENDAR_DATE = TRUNC(REQ.START_DATE) -- Bug 2809639
436            -- AND     C.SEQ_NUM IS NOT NULL -- 2859130
437            ---bug 2341075: change sysdate to plan_start_date
438            --AND     C.CALENDAR_DATE >= trunc(sysdate)
439            --AND     C.CALENDAR_DATE >= p_plan_start_date
440            --bug3693892 added trunc
441            AND     TRUNC(REQ.START_DATE) >= p_plan_start_date
442            -- 2859130
443            AND TRUNC(REQ.START_DATE) < trunc(nvl(p_itf, REQ.START_DATE+1)) --4135752
444            UNION ALL
445            SELECT  trunc(SHIFT_DATE) SD_DATE,--4135752
446                    CAPACITY_UNITS * ((DECODE(LEAST(from_time, to_time),
447                    to_time,to_time + 24*3600,
448                         to_time) - from_time)/3600)* p_max_capacity * p_res_conv_rate SD_QTY
449            FROM    MSC_NET_RESOURCE_AVAIL
450            WHERE   PLAN_ID = p_plan_id
451            AND     NVL(PARENT_ID, -2) <> -1
452            AND     SR_INSTANCE_ID = p_instance_id
453            AND     RESOURCE_ID = p_res_id
454            AND     DEPARTMENT_ID = p_dept_id
455            -- krajan : 2408696 -- agilent
456            AND     organization_id = p_org_id
457            ---bug 2341075: change sysdate to plan_start_date
458            --AND     SHIFT_DATE >= trunc(sysdate)
459            AND     trunc(SHIFT_DATE) >= p_plan_start_date --4135752
460            AND     trunc(SHIFT_DATE) < trunc(nvl(p_itf, SHIFT_DATE+1)) -- 2859130 --4135752
461         )
462         GROUP BY SD_DATE
463         ORDER BY SD_DATE;
464 END get_res_avail_opt_bat;
465 
466 -- unconstrained plan batching
467 PROCEDURE get_res_avail_unopt_bat(
468    p_instance_id        IN NUMBER,
469    p_org_id             IN NUMBER,
470    p_plan_id            IN NUMBER,
471    p_plan_start_date    IN DATE,
472    p_dept_id            IN NUMBER,
473    p_res_id             IN NUMBER,
474    p_itf                IN DATE,
475    p_uom_type           IN NUMBER,
476    p_max_capacity       IN NUMBER,
477    p_res_conv_rate      IN NUMBER,
478    p_cal_code           IN VARCHAR2,
479    p_cal_exc_set_id     IN NUMBER,
480    x_atp_dates          OUT NoCopy MRP_ATP_PUB.date_arr,
481    x_atp_qtys           OUT NoCopy MRP_ATP_PUB.number_arr
482 ) IS
483 l_null_num      NUMBER;
484 l_null_char     VARCHAR2(1);
485 l_sysdate       DATE := trunc(sysdate);--4135752
486 BEGIN
487         IF PG_DEBUG in ('Y', 'C') THEN
488                 msc_sch_wb.atp_debug('get_res_avail_unopt_bat');
489         END IF;
490 
491         SELECT  SD_DATE,
492                 SUM(SD_QTY)
493         BULK COLLECT INTO x_atp_dates, x_atp_qtys
494         FROM
495         (
496          SELECT  C.CALENDAR_DATE SD_DATE,
497                -1*DECODE(REQ.RESOURCE_ID, -1, REQ.LOAD_RATE,
498                      DECODE(REQ.END_DATE, NULL, REQ.RESOURCE_HOURS,
499                       -- Bug 3348095
500                         DECODE(REQ.record_source, 2, REQ.RESOURCE_HOURS,
501                                  REQ.DAILY_RESOURCE_HOURS)))
502                       -- For ATP created records use resource_hours
503                       -- End Bug 3348095
504                        *
505                        DECODE(DR.UOM_CLASS_TYPE, 1, I.UNIT_WEIGHT, 2, UNIT_VOLUME) *
506                         NVL(MUC.CONVERSION_RATE,1) * NVL(S.NEW_ORDER_QUANTITY, S.FIRM_QUANTITY) SD_QTY
507 
508          FROM    MSC_DEPARTMENT_RESOURCES DR,
509                  MSC_RESOURCE_REQUIREMENTS REQ,
510                  MSC_CALENDAR_DATES C,
511                  --- add table for resource batching
512                  --- these tables are added to determine how much apacity has already been consumed by the
513                  --- existing supplies
514                  MSC_SYSTEM_ITEMS I,
515                  MSC_SUPPLIES S,
516                  MSC_UOM_CONVERSIONS MUC
517          -- Bug 2675504, 2665805,
518          --bug3394866
519          WHERE   DR.PLAN_ID = p_plan_id
520          AND     NVL(DR.OWNING_DEPARTMENT_ID, DR.DEPARTMENT_ID)=p_dept_id
521          AND     DR.RESOURCE_ID = p_res_id
522          AND     DR.SR_INSTANCE_ID = p_instance_id
523            -- krajan: 2408696 --
524          AND     DR.organization_id = p_org_id
525 
526          AND     REQ.PLAN_ID = DR.PLAN_ID
527          AND     REQ.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
528          AND     REQ.RESOURCE_ID = DR.RESOURCE_ID
529          AND     REQ.DEPARTMENT_ID = DR.DEPARTMENT_ID
530          AND     REQ.ORGANIZATION_ID = DR.ORGANIZATION_ID
531          --bug3394866
532          -- End Bug 2675504, 2665805,
533          AND     NVL(REQ.PARENT_ID, MSC_ATP_PVT.G_OPTIMIZED_PLAN) = MSC_ATP_PVT.G_OPTIMIZED_PLAN
534          AND     I.SR_INSTANCE_ID = S.SR_INSTANCE_ID
535          AND     I.PLAN_ID = S.PLAN_ID
536          AND     I.ORGANIZATION_ID = S.ORGANIZATION_ID
537          AND     I.INVENTORY_ITEM_ID = S.INVENTORY_ITEM_ID
538          AND     DECODE(p_uom_type, 1, I.WEIGHT_UOM, 2 , I.VOLUME_UOM) = MUC.UOM_CODE (+)
539          AND     MUC.SR_INSTANCE_ID (+) = I.SR_INSTANCE_ID
540          AND     MUC.INVENTORY_ITEM_ID  (+)= 0
541          AND     S.TRANSACTION_ID = REQ.SUPPLY_ID
542          AND     S.PLAN_ID = REQ.PLAN_ID
543          AND     S.SR_INSTANCE_ID = REQ.SR_INSTANCE_ID
544          AND     S.ORGANIZATION_ID = REQ.ORGANIZATION_ID
545                  -- Exclude Cancelled Supplies 2460645
546          AND     NVL(S.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
547          -- CTO Option Dependent Resources ODR
548          -- Option Dependent Resources Capacity Check
549          AND     I.inventory_item_id = REQ.assembly_item_id
550          AND     ((I.bom_item_type <> 1 and I.bom_item_type <> 2)
551                -- bom_item_type not model and option_class always committed.
552                     AND   (I.atp_flag <> 'N')
553                -- atp_flag is 'Y' then committed.
554                     OR    (REQ.record_source = 2) ) -- this OR may be changed during performance analysis.
555               -- if record created by ATP then committed.
556          -- End CTO Option Dependent Resources ODR
557          AND     C.SR_INSTANCE_ID = REQ.SR_INSTANCE_ID --bug3394866
558          AND     C.CALENDAR_CODE = p_cal_code
559          AND     C.EXCEPTION_SET_ID = p_cal_exc_set_id
560                  -- Bug 3348095
561                  -- Ensure that the ATP created resource Reqs
562                  -- do not get double counted.
563          AND     C.CALENDAR_DATE BETWEEN DECODE(REQ.record_source, 2,
564                           TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)), TRUNC(REQ.START_DATE))
565                    AND TRUNC(NVL(REQ.END_DATE, REQ.START_DATE))
566                  -- End Bug 3348095
567          AND     C.SEQ_NUM IS NOT NULL
568          ---bug 2341075: change sysdate to plan_start_date
569          --AND     C.CALENDAR_DATE >= trunc(sysdate)
570          AND     C.CALENDAR_DATE >= p_plan_start_date
571          -- 2859130
572          AND     C.CALENDAR_DATE < NVL(p_itf, C.CALENDAR_DATE+1)
573          UNION ALL
574          SELECT  trunc(SHIFT_DATE) SD_DATE,  --4135752
575                  CAPACITY_UNITS * ((DECODE(LEAST(from_time, to_time),
576                  to_time,to_time + 24*3600,
577                       to_time) - from_time)/3600)* p_max_capacity * p_res_conv_rate SD_QTY
578          FROM    MSC_NET_RESOURCE_AVAIL
579          WHERE   PLAN_ID = p_plan_id
580          AND     NVL(PARENT_ID, -2) <> -1
581          AND     SR_INSTANCE_ID = p_instance_id
582          AND     RESOURCE_ID = p_res_id
583          AND     DEPARTMENT_ID = p_dept_id
584          -- krajan : 2408696 -- agilent
585          AND     organization_id = p_org_id
586          ---bug 2341075: change sysdate to plan_start_date
587          --AND     SHIFT_DATE >= trunc(sysdate)
588          AND     trunc(SHIFT_DATE) >= p_plan_start_date  --4135752
589          AND     trunc(SHIFT_DATE) < trunc(nvl(p_itf, SHIFT_DATE+1)) -- 2859130  --4135752
590          )
591          GROUP BY SD_DATE
592          ORDER BY SD_DATE;
593 END get_res_avail_unopt_bat;
594 
595 -- constrained plan details
596 PROCEDURE get_res_avail_opt_dtls(
597    p_instance_id        IN NUMBER,
598    p_org_id             IN NUMBER,
599    p_plan_id            IN NUMBER,
600    p_plan_start_date    IN DATE,
601    p_dept_id            IN NUMBER,
602    p_res_id             IN NUMBER,
603    p_itf                IN DATE,
604    p_uom_code           IN VARCHAR2,
605    p_level              IN NUMBER,
606    p_scenario_id        IN NUMBER
607 ) IS
608 l_null_num      NUMBER;
609 l_null_char     VARCHAR2(1);
610 l_sysdate       DATE := trunc(sysdate); --4135752
611 BEGIN
612         IF PG_DEBUG in ('Y', 'C') THEN
613                 msc_sch_wb.atp_debug('get_res_avail_opt_dtls');
614         END IF;
615 
616          INSERT INTO msc_atp_sd_details_temp (
617                 ATP_Level,
618                 Order_line_id,
619                 Scenario_Id,
620                 Inventory_Item_Id,
621                 Request_Item_Id,
622                 Organization_Id,
623                 Department_Id,
624                 Resource_Id,
625                 Supplier_Id,
626                 Supplier_Site_Id,
627                 From_Organization_Id,
628                 From_Location_Id,
629                 To_Organization_Id,
630                 To_Location_Id,
631                 Ship_Method,
632                 UOM_code,
633                 Supply_Demand_Type,
634                 Supply_Demand_Source_Type,
635                 Supply_Demand_Source_Type_Name,
636                 Identifier1,
637                 Identifier2,
638                 Identifier3,
639                 Identifier4,
640                 Supply_Demand_Quantity,
641                 Supply_Demand_Date,
642                 Disposition_Type,
643                 Disposition_Name,
644                 Pegging_Id,
645                 End_Pegging_Id,
646                 creation_date,
647                 created_by,
648                 last_update_date,
649                 last_updated_by,
650                 last_update_login
651         )
652 
653         (SELECT
654                 p_level col1,
655                 MSC_ATP_PVT.G_ORDER_LINE_ID col2,
656                 p_scenario_id col3,
657                 l_null_num col4 ,
658                 l_null_num col5,
659                 p_org_id col6,
660                 p_dept_id col7,
661                 p_res_id col8,
662                 l_null_num col9,
663                 l_null_num col10,
664                 l_null_num col11,
665                 l_null_num col12,
666                 l_null_num col13,
667                 l_null_num col14,
668                 l_null_char col15,
669                 p_uom_code col16,
670                 1 col17, -- demand
671                 MSC_ATP_FUNC.Get_Order_Type(REQ.SUPPLY_ID, p_plan_id) col18,
672                 l_null_char col19,
673   --              	L.MEANING col19 ,
674                 REQ.SR_INSTANCE_ID col20,
675                 l_null_num col21,
676                 REQ.TRANSACTION_ID col22,
677                 l_null_num col23,
678                 -- Bug 3348095
679                 -1* DECODE(REQ.RESOURCE_ID, -1, REQ.LOAD_RATE,
680                 DECODE(REQ.END_DATE, NULL, REQ.RESOURCE_HOURS,
681                     -- For ATP created records use resource_hours
682                       DECODE(REQ.record_source, 2, REQ.RESOURCE_HOURS,
683                                  REQ.DAILY_RESOURCE_HOURS))) col24,
684                 -- C.CALENDAR_DATE col25, -- 2859130
685                 -- For ATP created records use end_date otherwise start_date
686                 DECODE(REQ.record_source, 2, TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)),
687                            TRUNC(REQ.START_DATE)) col25,
688                 -- End Bug 3348095
689                 l_null_num col26,
690                 MSC_ATP_FUNC.Get_Order_Number(REQ.SUPPLY_ID, p_plan_id) col27,
691                 l_null_num col28,
692                l_null_num col29,
693                 l_sysdate,
694                 FND_GLOBAL.User_ID,
695                 l_sysdate,
696                 FND_GLOBAL.User_ID,
697                 FND_GLOBAL.User_ID
698         FROM    MSC_DEPARTMENT_RESOURCES DR,
699                 MSC_RESOURCE_REQUIREMENTS REQ,
700                 -- CTO Option Dependent Resources
701                 -- Option Dependent Resources Capacity Check
702                 -- Add Link to Items
703                 MSC_SYSTEM_ITEMS I
704                -- 2859130 MSC_CALENDAR_DATES C
705         -- Bug 2675504, 2665805,
706         --bug3394866
707         WHERE   DR.PLAN_ID = p_plan_id
708         AND     NVL(DR.OWNING_DEPARTMENT_ID, DR.DEPARTMENT_ID)=p_dept_id
709         AND     DR.RESOURCE_ID = p_res_id
710         AND     DR.SR_INSTANCE_ID = p_instance_id
711         -- krajan: 2408696 --
712         AND     DR.organization_id = p_org_id
713 
714         AND     REQ.PLAN_ID = DR.PLAN_ID
715         AND     REQ.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
716         AND     REQ.RESOURCE_ID = DR.RESOURCE_ID
717         AND     REQ.DEPARTMENT_ID = DR.DEPARTMENT_ID
718         AND     REQ.ORGANIZATION_ID = DR.ORGANIZATION_ID
719         --bug3394866-- End Bug 2675504, 2665805,
720         AND    NVL(REQ.PARENT_ID, 1) = 1 -- parent_id is 1 for constrained plans. Bug 2809639
721          -- CTO Option Dependent Resources ODR
722          -- Option Dependent Resources Capacity Check
723          AND     I.SR_INSTANCE_ID = REQ.SR_INSTANCE_Id
724          AND     I.PLAN_ID = REQ.PLAN_ID
725          AND     I.ORGANIZATION_ID = REQ.ORGANIZATION_ID
726          AND     I.inventory_item_id = REQ.assembly_item_id
727          AND     ((I.bom_item_type <> 1 and I.bom_item_type <> 2)
728                -- bom_item_type not model and option_class always committed.
729                     AND   (I.atp_flag <> 'N')
730                -- atp_flag is 'Y' then committed.
731                     OR    (REQ.record_source = 2) ) -- this OR may be changed during performance analysis.
732               -- if record created by ATP then committed.
733          -- End CTO Option Dependent Resources ODR
734         -- 2859130
735         --AND    C.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
736         --AND    C.CALENDAR_CODE = p_cal_code
737         --AND    C.EXCEPTION_SET_ID = p_cal_exc_set_id
738         --AND    C.CALENDAR_DATE = TRUNC(REQ.START_DATE)  -- Bug 2809639
739         -- AND    C.SEQ_NUM IS NOT NULL -- 2859130
740         ---bug 2341075: change sysdate to plan_start_date
741         --AND    C.CALENDAR_DATE >= trunc(sysdate)
742         --AND    C.CALENDAR_DATE >= p_plan_start_date
743         --bug3693892 added trunc
744         AND    TRUNC(REQ.START_DATE) >= p_plan_start_date
745         -- 2859130
746         AND TRUNC(REQ.START_DATE) < nvl(p_itf, REQ.START_DATE+1)
747         UNION ALL
748         SELECT
749                 p_level col1,
750                 MSC_ATP_PVT.G_ORDER_LINE_ID col2,
751                 p_scenario_id col3,
752                 l_null_num col4 ,
753                 l_null_num col5,
754                 p_org_id col6,
755                 p_dept_id col7,
756                 p_res_id col8,
757                 l_null_num col9,
758                 l_null_num col10,
759                 l_null_num col11,
760                 l_null_num col12,
761                 l_null_num col13,
762                 l_null_num col14,
763                 l_null_char col15,
764                 p_uom_code col16,
765                 2 col17, -- supply
766                 l_null_num col18,
767                 l_null_char col19,
768   --              	L.MEANING col19 ,
769                 SR_INSTANCE_ID col20,
770                 l_null_num col21,
771                 TRANSACTION_ID col22,
772                 l_null_num col23,
773                 CAPACITY_UNITS * ((DECODE(LEAST(from_time, to_time),
774                         to_time,to_time + 24*3600,
775                        to_time) - from_time)/3600) col24,
776                 trunc(SHIFT_DATE) col25, --4135752
777                 l_null_num col26,
778                 l_null_char col27,
779                 l_null_num col28,
780                 l_null_num col29,
781                 l_sysdate,
782                 FND_GLOBAL.User_ID,
783                 l_sysdate,
784                 FND_GLOBAL.User_ID,
785                 FND_GLOBAL.User_ID
786         FROM    MSC_NET_RESOURCE_AVAIL
787         WHERE   PLAN_ID = p_plan_id
788         AND     NVL(PARENT_ID, -2) <> -1
789         AND     SR_INSTANCE_ID = p_instance_id
790         AND     RESOURCE_ID = p_res_id
791         -- 2408696 : krajan
792         AND     organization_id = p_org_id
793         AND     DEPARTMENT_ID = p_dept_id
794         ---bug 2341075: change sysdate to plan_start_date
795         --AND     SHIFT_DATE >= trunc(sysdate)
796         AND     trunc(SHIFT_DATE) >= p_plan_start_date --4135752
797         AND     trunc(SHIFT_DATE) < trunc(nvl(p_itf, SHIFT_DATE+1)) -- 2859130  --4135752
798         );
799 END get_res_avail_opt_dtls;
800 
801 -- unconstrained plan dtls
802 PROCEDURE get_res_avail_unopt_dtls(
803    p_instance_id        IN NUMBER,
804    p_org_id             IN NUMBER,
805    p_plan_id            IN NUMBER,
806    p_plan_start_date    IN DATE,
807    p_dept_id            IN NUMBER,
808    p_res_id             IN NUMBER,
809    p_itf                IN DATE,
810    p_uom_code           IN VARCHAR2,
811    p_level              IN NUMBER,
812    p_scenario_id        IN NUMBER,
813    p_cal_code           IN VARCHAR2,
814    p_cal_exc_set_id     IN NUMBER
815 ) IS
816 l_null_num      NUMBER;
817 l_null_char     VARCHAR2(1);
818 l_sysdate       DATE := trunc(sysdate); --4135752
819 BEGIN
820         IF PG_DEBUG in ('Y', 'C') THEN
821                 msc_sch_wb.atp_debug('get_res_avail_unopt_dtls');
822         END IF;
823 
824 		 INSERT INTO msc_atp_sd_details_temp (
825 			ATP_Level,
826 			Order_line_id,
827 			Scenario_Id,
828 			Inventory_Item_Id,
829 			Request_Item_Id,
830 			Organization_Id,
831 			Department_Id,
832 			Resource_Id,
833 			Supplier_Id,
834 			Supplier_Site_Id,
835 			From_Organization_Id,
836 			From_Location_Id,
837 			To_Organization_Id,
838 			To_Location_Id,
839 			Ship_Method,
840 			UOM_code,
841 			Supply_Demand_Type,
842 			Supply_Demand_Source_Type,
843 			Supply_Demand_Source_Type_Name,
844 			Identifier1,
845 			Identifier2,
846 			Identifier3,
847 			Identifier4,
848 			Supply_Demand_Quantity,
849 			Supply_Demand_Date,
850 			Disposition_Type,
851 			Disposition_Name,
852 			Pegging_Id,
853 			End_Pegging_Id,
854 			creation_date,
855 			created_by,
856 			last_update_date,
857 			last_updated_by,
858 			last_update_login
859 		)
860 
861           	(SELECT
862                 	p_level col1,
863                 	MSC_ATP_PVT.G_ORDER_LINE_ID col2,
864                 	p_scenario_id col3,
865                 	l_null_num col4 ,
866                 	l_null_num col5,
867                 	p_org_id col6,
868                 	p_dept_id col7,
869                 	p_res_id col8,
870                 	l_null_num col9,
871                 	l_null_num col10,
872                 	l_null_num col11,
873                 	l_null_num col12,
874                 	l_null_num col13,
875                 	l_null_num col14,
876                 	l_null_char col15,
877                 	p_uom_code col16,
878                 	1 col17, -- demand
879                 	MSC_ATP_FUNC.Get_Order_Type(REQ.SUPPLY_ID, p_plan_id) col18,
880 	                l_null_char col19,
881 --              	L.MEANING col19 ,
882                 	REQ.SR_INSTANCE_ID col20,
883                 	l_null_num col21,
884                 	REQ.TRANSACTION_ID col22,
885                 	l_null_num col23,
886                         -- Bug 3348095
887                         -1* DECODE(REQ.RESOURCE_ID, -1, REQ.LOAD_RATE,
888                           DECODE(REQ.END_DATE, NULL, REQ.RESOURCE_HOURS,
889                         -- For ATP created records use resource_hours
890                             DECODE(REQ.record_source, 2, REQ.RESOURCE_HOURS,
891                                  REQ.DAILY_RESOURCE_HOURS))) col24,
892                         -- End Bug 3348095
893                 	C.CALENDAR_DATE col25,
894                 	l_null_num col26,
895                 	MSC_ATP_FUNC.Get_Order_Number(REQ.SUPPLY_ID, p_plan_id) col27,
896                 	l_null_num col28,
897          	       l_null_num col29,
898 			l_sysdate,
899 			FND_GLOBAL.User_ID,
900 			l_sysdate,
901 			FND_GLOBAL.User_ID,
902 			FND_GLOBAL.User_ID
903          	FROM   MSC_DEPARTMENT_RESOURCES DR,
904                 	MSC_RESOURCE_REQUIREMENTS REQ,
905                        -- CTO Option Dependent Resources
906                        -- Option Dependent Resources Capacity Check
907                        -- Add Link to Items
908                        MSC_SYSTEM_ITEMS I,
909          	       MSC_CALENDAR_DATES C
910                -- Bug 2675504, 2665805,
911                --bug3394866
912                WHERE   DR.PLAN_ID = p_plan_id
913                AND     NVL(DR.OWNING_DEPARTMENT_ID, DR.DEPARTMENT_ID)=p_dept_id
914                AND     DR.RESOURCE_ID = p_res_id
915                AND     DR.SR_INSTANCE_ID = p_instance_id
916                -- krajan: 2408696 --
917                AND     DR.organization_id = p_org_id
918 
919                AND     REQ.PLAN_ID = DR.PLAN_ID
920                AND     REQ.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
921                AND     REQ.RESOURCE_ID = DR.RESOURCE_ID
922                AND     REQ.DEPARTMENT_ID = DR.DEPARTMENT_ID
923                AND     REQ.ORGANIZATION_ID = DR.ORGANIZATION_ID
924                --bug3394866-- End Bug 2675504, 2665805,
925                AND    NVL(REQ.PARENT_ID, MSC_ATP_PVT.G_OPTIMIZED_PLAN) = MSC_ATP_PVT.G_OPTIMIZED_PLAN
926                 -- CTO Option Dependent Resources ODR
927                 -- Option Dependent Resources Capacity Check
928                 AND     I.SR_INSTANCE_ID = REQ.SR_INSTANCE_Id
929                 AND     I.PLAN_ID = REQ.PLAN_ID
930                 AND     I.ORGANIZATION_ID = REQ.ORGANIZATION_ID
931                 AND     I.inventory_item_id = REQ.assembly_item_id
932                 AND     ((I.bom_item_type <> 1 and I.bom_item_type <> 2)
933                       -- bom_item_type not model and option_class always committed.
934                            AND   (I.atp_flag <> 'N')
935                       -- atp_flag is 'Y' then committed.
936                            OR    (REQ.record_source = 2) ) -- this OR may be changed during performance analysis.
937                      -- if record created by ATP then committed.
938                 -- End CTO Option Dependent Resources ODR
939          	AND    C.SR_INSTANCE_ID = REQ.SR_INSTANCE_ID --bug3394866
940          	AND    C.CALENDAR_CODE = p_cal_code
941          	AND    C.EXCEPTION_SET_ID = p_cal_exc_set_id
942                        -- Bug 3348095
943                        -- Ensure that the ATP created resource Reqs
944                        -- do not get double counted.
945                 AND    C.CALENDAR_DATE BETWEEN DECODE(REQ.record_source, 2,
946                             TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)), TRUNC(REQ.START_DATE))
947                          AND TRUNC(NVL(REQ.END_DATE, REQ.START_DATE))
948                        -- End Bug 3348095
949          	AND    C.SEQ_NUM IS NOT NULL
950                 ---bug 2341075: change sysdate to plan_start_date
951          	--AND    C.CALENDAR_DATE >= trunc(sysdate)
952          	AND    C.CALENDAR_DATE >= p_plan_start_date
953                 -- 2859130
954                 AND     C.CALENDAR_DATE < NVL(p_itf, C.CALENDAR_DATE+1)
955          	UNION ALL
956          	SELECT
957                 	p_level col1,
958                 	MSC_ATP_PVT.G_ORDER_LINE_ID col2,
959                 	p_scenario_id col3,
960                 	l_null_num col4 ,
961                 	l_null_num col5,
962                 	p_org_id col6,
963                 	p_dept_id col7,
964                 	p_res_id col8,
965                 	l_null_num col9,
966                 	l_null_num col10,
967                 	l_null_num col11,
968                 	l_null_num col12,
969                 	l_null_num col13,
970                 	l_null_num col14,
971                 	l_null_char col15,
972                 	p_uom_code col16,
973                 	2 col17, -- supply
974                 	l_null_num col18,
975 	                l_null_char col19,
976 --              	L.MEANING col19 ,
977                 	SR_INSTANCE_ID col20,
978                 	l_null_num col21,
979                 	TRANSACTION_ID col22,
980                 	l_null_num col23,
981                 	CAPACITY_UNITS * ((DECODE(LEAST(from_time, to_time),
982                 	       	to_time,to_time + 24*3600,
983                 	       to_time) - from_time)/3600) col24,
984                 	trunc(SHIFT_DATE) col25,  --4135752
985                 	l_null_num col26,
986                 	l_null_char col27,
987                 	l_null_num col28,
988         	        l_null_num col29,
989 			l_sysdate,
990 			FND_GLOBAL.User_ID,
991 			l_sysdate,
992 			FND_GLOBAL.User_ID,
993 			FND_GLOBAL.User_ID
994         	FROM    MSC_NET_RESOURCE_AVAIL
995         	WHERE   PLAN_ID = p_plan_id
996         	AND     NVL(PARENT_ID, -2) <> -1
997         	AND     SR_INSTANCE_ID = p_instance_id
998         	AND     RESOURCE_ID = p_res_id
999                 -- 2408696 : krajan
1000                 AND     organization_id = p_org_id
1001         	AND     DEPARTMENT_ID = p_dept_id
1002                 ---bug 2341075: change sysdate to plan_start_date
1003         	--AND     SHIFT_DATE >= trunc(sysdate)
1004         	AND     trunc(SHIFT_DATE) >= p_plan_start_date  --4135752
1005                 AND     trunc(SHIFT_DATE) < trunc(nvl(p_itf, SHIFT_DATE+1)) -- 2859130  --4135752
1006      	   	);
1007 		-- dsting removed 'order by col25'
1008 END get_res_avail_unopt_dtls;
1009 
1010 -- constrained plan batching details
1011 PROCEDURE get_res_avail_opt_bat_dtls(
1012    p_instance_id        IN NUMBER,
1013    p_org_id             IN NUMBER,
1014    p_plan_id            IN NUMBER,
1015    p_plan_start_date    IN DATE,
1016    p_dept_id            IN NUMBER,
1017    p_res_id             IN NUMBER,
1018    p_itf                IN DATE,
1019    p_uom_type           IN NUMBER,
1020    p_uom_code           IN VARCHAR2,
1021    p_max_capacity       IN NUMBER,
1022    p_res_conv_rate      IN NUMBER,
1023    p_level              IN NUMBER,
1024    p_scenario_id        IN NUMBER
1025 ) IS
1026 l_null_num      NUMBER;
1027 l_null_char     VARCHAR2(1);
1028 l_sysdate       DATE := trunc(sysdate);  --4135752
1029 BEGIN
1030         IF PG_DEBUG in ('Y', 'C') THEN
1031                 msc_sch_wb.atp_debug('get_res_avail_opt_bat_dtls');
1032         END IF;
1033 
1034         INSERT INTO msc_atp_sd_details_temp (
1035                 ATP_Level,
1036                 Order_line_id,
1037                 Scenario_Id,
1038                 Inventory_Item_Id,
1039                 Request_Item_Id,
1040                 Organization_Id,
1041                 Department_Id,
1042                 Resource_Id,
1043                 Supplier_Id,
1044                 Supplier_Site_Id,
1045                 From_Organization_Id,
1046                 From_Location_Id,
1047                 To_Organization_Id,
1048                 To_Location_Id,
1049                 Ship_Method,
1050                 UOM_code,
1051                 Supply_Demand_Type,
1052                 Supply_Demand_Source_Type,
1053                 Supply_Demand_Source_Type_Name,
1054                 Identifier1,
1055                 Identifier2,
1056                 Identifier3,
1057                 Identifier4,
1058                 Supply_Demand_Quantity,
1059                 Supply_Demand_Date,
1060                 Disposition_Type,
1061                 Disposition_Name,
1062                 Pegging_Id,
1063                 End_Pegging_Id,
1064                 creation_date,
1065                 created_by,
1066                 last_update_date,
1067                 last_updated_by,
1068                 last_update_login
1069         )
1070 
1071         (SELECT
1072                 p_level col1,
1073                 MSC_ATP_PVT.G_ORDER_LINE_ID col2,
1074                 p_scenario_id col3,
1075                 l_null_num col4 ,
1076                 l_null_num col5,
1077                 p_org_id col6,
1078                 p_dept_id col7,
1079                 p_res_id col8,
1080                 l_null_num col9,
1081                 l_null_num col10,
1082                 l_null_num col11,
1083                 l_null_num col12,
1084                 l_null_num col13,
1085                 l_null_num col14,
1086                 l_null_char col15,
1087                 p_uom_code col16,
1088                 1 col17, -- demand
1089                 MSC_ATP_FUNC.Get_Order_Type(REQ.SUPPLY_ID, p_plan_id) col18,
1090                 l_null_char col19,
1091   --              	L.MEANING col19 ,
1092                 REQ.SR_INSTANCE_ID col20,
1093                 l_null_num col21,
1094                 REQ.TRANSACTION_ID col22,
1095                 l_null_num col23,
1096                 -1* DECODE(REQ.RESOURCE_ID, -1, REQ.LOAD_RATE, -- 2859130 remove daily_resource_hours
1097                            REQ.RESOURCE_HOURS) *
1098                               DECODE(DR.UOM_CLASS_TYPE, 1, I.UNIT_WEIGHT, 2, UNIT_VOLUME)
1099                               * NVL(MUC.CONVERSION_RATE, 1) * NVL(S.NEW_ORDER_QUANTITY, S.FIRM_QUANTITY) col24,
1100                 -- 2859130 C.CALENDAR_DATE col25,
1101                 -- Bug 3348095
1102                 -- For ATP created records use end_date otherwise start_date
1103                 DECODE(REQ.record_source, 2, TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)),
1104                            TRUNC(REQ.START_DATE)) col25,
1105                 -- End Bug 3348095
1106                 l_null_num col26,
1107                 MSC_ATP_FUNC.Get_Order_Number(REQ.SUPPLY_ID, p_plan_id) col27,
1108                 l_null_num col28,
1109                l_null_num col29,
1110                 l_sysdate,
1111                 FND_GLOBAL.User_ID,
1112                 l_sysdate,
1113                 FND_GLOBAL.User_ID,
1114                 FND_GLOBAL.User_ID
1115         FROM   MSC_DEPARTMENT_RESOURCES DR,
1116                 MSC_RESOURCE_REQUIREMENTS REQ,
1117                -- 2859130 MSC_CALENDAR_DATES C,
1118                ---tables added for resource batching
1119                MSC_SYSTEM_ITEMS I,
1120                MSC_SUPPLIES S,
1121                MSC_UOM_CONVERSIONS MUC
1122         -- Bug 2675504, 2665805,
1123         --bug3394866
1124         WHERE   DR.PLAN_ID = p_plan_id
1125         AND     NVL(DR.OWNING_DEPARTMENT_ID, DR.DEPARTMENT_ID)=p_dept_id
1126         AND     DR.RESOURCE_ID = p_res_id
1127         AND     DR.SR_INSTANCE_ID = p_instance_id
1128         -- krajan: 2408696 --
1129         AND     DR.organization_id = p_org_id
1130 
1131         AND     REQ.PLAN_ID = DR.PLAN_ID
1132         AND     REQ.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
1133         AND     REQ.RESOURCE_ID = DR.RESOURCE_ID
1134         AND     REQ.DEPARTMENT_ID = DR.DEPARTMENT_ID
1135         AND     REQ.ORGANIZATION_ID = DR.ORGANIZATION_ID
1136         --bug3394866
1137         -- End Bug 2675504, 2665805,
1138         AND     NVL(REQ.PARENT_ID, 1) = 1 -- parent_id is 1 for constrained plans. Bug 2809639
1139         AND     I.SR_INSTANCE_ID = S.SR_INSTANCE_Id
1140         AND     I.PLAN_ID = S.PLAN_ID
1141         AND     I.ORGANIZATION_ID = S.ORGANIZATION_ID
1142         AND     I.INVENTORY_ITEM_ID = S.INVENTORY_ITEM_ID
1143         AND     DECODE(p_uom_type, 1, I.WEIGHT_UOM, 2 , I.VOLUME_UOM) = MUC.UOM_CODE (+)
1144         AND     MUC.SR_INSTANCE_ID (+) = I.SR_INSTANCE_ID
1145         AND     MUC.INVENTORY_ITEM_ID (+) = 0
1146         AND     S.TRANSACTION_ID = REQ.SUPPLY_ID
1147         AND     S.PLAN_ID = REQ.PLAN_ID
1148         AND     S.SR_INSTANCE_ID = REQ.SR_INSTANCE_ID
1149         AND     S.ORGANIZATION_ID = REQ.ORGANIZATION_ID
1150                 -- Exclude Cancelled Supplies 2460645
1151         AND     NVL(S.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
1152          -- CTO Option Dependent Resources ODR
1153          -- Option Dependent Resources Capacity Check
1154          AND     I.inventory_item_id = REQ.assembly_item_id
1155          AND     ((I.bom_item_type <> 1 and I.bom_item_type <> 2)
1156                -- bom_item_type not model and option_class always committed.
1157                     AND   (I.atp_flag <> 'N')
1158                -- atp_flag is 'Y' then committed.
1159                     OR    (REQ.record_source = 2) ) -- this OR may be changed during performance analysis.
1160               -- if record created by ATP then committed.
1161          -- End CTO Option Dependent Resources ODR
1162         -- 2859130
1163         --AND    C.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
1164         --AND    C.CALENDAR_CODE = p_cal_code
1165         --AND    C.EXCEPTION_SET_ID = p_cal_exc_set_id
1166         --AND    C.CALENDAR_DATE = TRUNC(REQ.START_DATE) -- Bug 2809639
1167         -- AND    C.SEQ_NUM IS NOT NULL -- 2859130
1168         ---bug 2341075: change sysdate to plan_start_date
1169         --AND    C.CALENDAR_DATE >= trunc(sysdate)
1170         -- AND    C.CALENDAR_DATE >= p_plan_start_date
1171         --bug3693892 added trunc
1172         AND TRUNC(REQ.START_DATE) >= p_plan_start_date
1173         -- 2859130
1174         AND TRUNC(REQ.START_DATE) < trunc(nvl(p_itf, REQ.START_DATE+1))  --4135752
1175         UNION ALL
1176         SELECT
1177                 p_level col1,
1178                 MSC_ATP_PVT.G_ORDER_LINE_ID col2,
1179                 p_scenario_id col3,
1180                 l_null_num col4 ,
1181                 l_null_num col5,
1182                 p_org_id col6,
1183                 p_dept_id col7,
1184                 p_res_id col8,
1185                 l_null_num col9,
1186                 l_null_num col10,
1187                 l_null_num col11,
1188                 l_null_num col12,
1189                 l_null_num col13,
1190                 l_null_num col14,
1191                 l_null_char col15,
1192                 p_uom_code col16,
1193                 2 col17, -- supply
1194                 l_null_num col18,
1195                 l_null_char col19,
1196   --              	L.MEANING col19 ,
1197                 SR_INSTANCE_ID col20,
1198                 l_null_num col21,
1199                 TRANSACTION_ID col22,
1200                 l_null_num col23,
1201                 CAPACITY_UNITS * ((DECODE(LEAST(from_time, to_time),
1202                         to_time,to_time + 24*3600,
1203                        to_time) - from_time)/3600) * p_max_capacity * p_res_conv_rate col24,
1204                 trunc(SHIFT_DATE) col25,  --4135752
1205                 l_null_num col26,
1206                 l_null_char col27,
1207                 l_null_num col28,
1208                 l_null_num col29,
1209                 l_sysdate,
1210                 FND_GLOBAL.User_ID,
1211                 l_sysdate,
1212                 FND_GLOBAL.User_ID,
1213                 FND_GLOBAL.User_ID
1214         FROM    MSC_NET_RESOURCE_AVAIL
1215         WHERE   PLAN_ID = p_plan_id
1216         AND     NVL(PARENT_ID, -2) <> -1
1217         AND     SR_INSTANCE_ID = p_instance_id
1218         AND     RESOURCE_ID = p_res_id
1219         -- 2408696 : krajan agilent
1220         AND     organization_id = p_org_id
1221 
1222         AND     DEPARTMENT_ID = p_dept_id
1223         ---bug 2341075: chnage sysdate to plan_start_date
1224         --AND     SHIFT_DATE >= trunc(sysdate)
1225         AND     trunc(SHIFT_DATE) >= p_plan_start_date  --4135752
1226         AND     trunc(SHIFT_DATE) < trunc(nvl(p_itf, SHIFT_DATE+1)) -- 2859130  --4135752
1227         );
1228 END get_res_avail_opt_bat_dtls;
1229 
1230 -- unconstrained plan batching details
1231 PROCEDURE get_res_avail_unopt_bat_dtls(
1232    p_instance_id        IN NUMBER,
1233    p_org_id             IN NUMBER,
1234    p_plan_id            IN NUMBER,
1235    p_plan_start_date    IN DATE,
1236    p_dept_id            IN NUMBER,
1237    p_res_id             IN NUMBER,
1238    p_itf                IN DATE,
1239    p_uom_type           IN NUMBER,
1240    p_uom_code           IN VARCHAR2,
1241    p_max_capacity       IN NUMBER,
1242    p_res_conv_rate      IN NUMBER,
1243    p_level              IN NUMBER,
1244    p_scenario_id        IN NUMBER,
1245    p_cal_code           IN VARCHAR2,
1246    p_cal_exc_set_id     IN NUMBER
1247 ) IS
1248 l_null_num      NUMBER;
1249 l_null_char     VARCHAR2(1);
1250 l_sysdate       DATE := trunc(sysdate);  --4135752
1251 BEGIN
1252         IF PG_DEBUG in ('Y', 'C') THEN
1253                 msc_sch_wb.atp_debug('get_res_avail_unopt_bat_dtls');
1254         END IF;
1255 
1256 	 	 INSERT INTO msc_atp_sd_details_temp (
1257 			ATP_Level,
1258 			Order_line_id,
1259 			Scenario_Id,
1260 			Inventory_Item_Id,
1261 			Request_Item_Id,
1262 			Organization_Id,
1263 			Department_Id,
1264 			Resource_Id,
1265 			Supplier_Id,
1266 			Supplier_Site_Id,
1267 			From_Organization_Id,
1268 			From_Location_Id,
1269 			To_Organization_Id,
1270 			To_Location_Id,
1271 			Ship_Method,
1272 			UOM_code,
1273 			Supply_Demand_Type,
1274 			Supply_Demand_Source_Type,
1275 			Supply_Demand_Source_Type_Name,
1276 			Identifier1,
1277 			Identifier2,
1278 			Identifier3,
1279 			Identifier4,
1280 			Supply_Demand_Quantity,
1281 			Supply_Demand_Date,
1282 			Disposition_Type,
1283 			Disposition_Name,
1284 			Pegging_Id,
1285 			End_Pegging_Id,
1286 			creation_date,
1287 			created_by,
1288 			last_update_date,
1289 			last_updated_by,
1290 			last_update_login
1291 		)
1292 
1293           	(SELECT
1294                 	p_level col1,
1295                 	MSC_ATP_PVT.G_ORDER_LINE_ID col2,
1296                 	p_scenario_id col3,
1297                 	l_null_num col4 ,
1298                 	l_null_num col5,
1299                 	p_org_id col6,
1300                 	p_dept_id col7,
1301                 	p_res_id col8,
1302                 	l_null_num col9,
1303                 	l_null_num col10,
1304                 	l_null_num col11,
1305                 	l_null_num col12,
1306                 	l_null_num col13,
1307                 	l_null_num col14,
1308                 	l_null_char col15,
1309                 	p_uom_code col16,
1310                 	1 col17, -- demand
1311                 	MSC_ATP_FUNC.Get_Order_Type(REQ.SUPPLY_ID, p_plan_id) col18,
1312 	                l_null_char col19,
1313 --              	L.MEANING col19 ,
1314                 	REQ.SR_INSTANCE_ID col20,
1315                 	l_null_num col21,
1316                 	REQ.TRANSACTION_ID col22,
1317                 	l_null_num col23,
1318                         -- Bug 3348095
1319                         -1* DECODE(REQ.RESOURCE_ID, -1, REQ.LOAD_RATE,
1320                           DECODE(REQ.END_DATE, NULL, REQ.RESOURCE_HOURS,
1321                         -- For ATP created records use resource_hours
1322                             DECODE(REQ.record_source, 2, REQ.RESOURCE_HOURS,
1323                                  REQ.DAILY_RESOURCE_HOURS)))
1324                         -- End Bug 3348095
1325                 	          *
1326                                DECODE(DR.UOM_CLASS_TYPE, 1, I.UNIT_WEIGHT, 2, UNIT_VOLUME)
1327                                 * NVL(MUC.CONVERSION_RATE, 1) * NVL(S.NEW_ORDER_QUANTITY, S.FIRM_QUANTITY) col24,
1328                 	C.CALENDAR_DATE col25,
1329                 	l_null_num col26,
1330                 	MSC_ATP_FUNC.Get_Order_Number(REQ.SUPPLY_ID, p_plan_id) col27,
1331                 	l_null_num col28,
1332          	       l_null_num col29,
1333 			l_sysdate,
1334 			FND_GLOBAL.User_ID,
1335 			l_sysdate,
1336 			FND_GLOBAL.User_ID,
1337 			FND_GLOBAL.User_ID
1338          	FROM   MSC_DEPARTMENT_RESOURCES DR,
1339                 	MSC_RESOURCE_REQUIREMENTS REQ,
1340          	       MSC_CALENDAR_DATES C,
1341                        ---tables added for resource batching
1342                        MSC_SYSTEM_ITEMS I,
1343                        MSC_SUPPLIES S,
1344                        MSC_UOM_CONVERSIONS MUC
1345                 -- Bug 2675504, 2665805,
1346                 --bug3394866
1347                 WHERE   DR.PLAN_ID = p_plan_id
1348                 AND     NVL(DR.OWNING_DEPARTMENT_ID, DR.DEPARTMENT_ID)=p_dept_id
1349                 AND     DR.RESOURCE_ID = p_res_id
1350                 AND     DR.SR_INSTANCE_ID = p_instance_id
1351                 -- krajan: 2408696 --
1352                 AND     DR.organization_id = p_org_id
1353 
1354                 AND     REQ.PLAN_ID = DR.PLAN_ID
1355                 AND     REQ.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
1356                 AND     REQ.RESOURCE_ID = DR.RESOURCE_ID
1357                 AND     REQ.DEPARTMENT_ID = DR.DEPARTMENT_ID
1358                 AND     REQ.ORGANIZATION_ID = DR.ORGANIZATION_ID
1359                 --bug3394866
1360                 -- End Bug 2675504, 2665805,
1361          	AND    NVL(REQ.PARENT_ID, MSC_ATP_PVT.G_OPTIMIZED_PLAN) = MSC_ATP_PVT.G_OPTIMIZED_PLAN
1362                 AND     I.SR_INSTANCE_ID = S.SR_INSTANCE_Id
1363                 AND     I.PLAN_ID = S.PLAN_ID
1364                 AND     I.ORGANIZATION_ID = S.ORGANIZATION_ID
1365                 AND     I.INVENTORY_ITEM_ID = S.INVENTORY_ITEM_ID
1366                 AND     DECODE(p_uom_type, 1, I.WEIGHT_UOM, 2 , I.VOLUME_UOM) = MUC.UOM_CODE (+)
1367                 AND     MUC.SR_INSTANCE_ID (+) = I.SR_INSTANCE_ID
1368                 AND     MUC.INVENTORY_ITEM_ID (+) = 0
1369                 AND     S.TRANSACTION_ID = REQ.SUPPLY_ID
1370                 AND     S.PLAN_ID = REQ.PLAN_ID
1371                 AND     S.SR_INSTANCE_ID = REQ.SR_INSTANCE_ID
1372                 AND     S.ORGANIZATION_ID = REQ.ORGANIZATION_ID
1373                         -- Exclude Cancelled Supplies 2460645
1374                 AND     NVL(S.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
1375                 -- CTO Option Dependent Resources ODR
1376                 -- Option Dependent Resources Capacity Check
1377                 AND     I.inventory_item_id = REQ.assembly_item_id
1378                 AND     ((I.bom_item_type <> 1 and I.bom_item_type <> 2)
1379                       -- bom_item_type not model and option_class always committed.
1380                            AND   (I.atp_flag <> 'N')
1381                       -- atp_flag is 'Y' then committed.
1382                            OR    (REQ.record_source = 2) ) -- this OR may be changed during performance analysis.
1383                      -- if record created by ATP then committed.
1384                 -- End CTO Option Dependent Resources ODR
1385          	AND    C.SR_INSTANCE_ID = REQ.SR_INSTANCE_ID --bug3394866
1386          	AND    C.CALENDAR_CODE = p_cal_code
1387          	AND    C.EXCEPTION_SET_ID = p_cal_exc_set_id
1388                        -- Bug 3348095
1389                        -- Ensure that the ATP created resource Reqs
1390                        -- do not get double counted.
1391                 AND    C.CALENDAR_DATE BETWEEN DECODE(REQ.record_source, 2,
1392                             TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)), TRUNC(REQ.START_DATE))
1393                          AND TRUNC(NVL(REQ.END_DATE, REQ.START_DATE))
1394                        -- End Bug 3348095
1395          	AND    C.SEQ_NUM IS NOT NULL
1396                 ---bug 2341075: change sysdate to plan_start_date
1397          	--AND    C.CALENDAR_DATE >= trunc(sysdate)
1398          	AND    C.CALENDAR_DATE >= trunc(p_plan_start_date)  --4135752
1399                 -- 2859130
1400                 AND     C.CALENDAR_DATE < NVL(p_itf, C.CALENDAR_DATE+1)
1401          	UNION ALL
1402          	SELECT
1403                 	p_level col1,
1404                 	MSC_ATP_PVT.G_ORDER_LINE_ID col2,
1405                 	p_scenario_id col3,
1406                 	l_null_num col4 ,
1407                 	l_null_num col5,
1408                 	p_org_id col6,
1409                 	p_dept_id col7,
1410                 	p_res_id col8,
1411                 	l_null_num col9,
1412                 	l_null_num col10,
1413                 	l_null_num col11,
1414                 	l_null_num col12,
1415                 	l_null_num col13,
1416                 	l_null_num col14,
1417                 	l_null_char col15,
1418                 	p_uom_code col16,
1419                 	2 col17, -- supply
1420                 	l_null_num col18,
1421 	                l_null_char col19,
1422 --              	L.MEANING col19 ,
1423                 	SR_INSTANCE_ID col20,
1424                 	l_null_num col21,
1425                 	TRANSACTION_ID col22,
1426                 	l_null_num col23,
1427                 	CAPACITY_UNITS * ((DECODE(LEAST(from_time, to_time),
1428                 	       	to_time,to_time + 24*3600,
1429                 	       to_time) - from_time)/3600) * p_max_capacity * p_res_conv_rate col24,
1430                 	trunc(SHIFT_DATE) col25,  --4135752
1431                 	l_null_num col26,
1432                 	l_null_char col27,
1433                 	l_null_num col28,
1434         	        l_null_num col29,
1435 			l_sysdate,
1436 			FND_GLOBAL.User_ID,
1437 			l_sysdate,
1438 			FND_GLOBAL.User_ID,
1439 			FND_GLOBAL.User_ID
1440         	FROM    MSC_NET_RESOURCE_AVAIL
1441         	WHERE   PLAN_ID = p_plan_id
1442         	AND     NVL(PARENT_ID, -2) <> -1
1443         	AND     SR_INSTANCE_ID = p_instance_id
1444         	AND     RESOURCE_ID = p_res_id
1445                 -- 2408696 : krajan agilent
1446                 AND     organization_id = p_org_id
1447 
1448         	AND     DEPARTMENT_ID = p_dept_id
1449                 ---bug 2341075: chnage sysdate to plan_start_date
1450         	--AND     SHIFT_DATE >= trunc(sysdate)
1451         	AND     trunc(SHIFT_DATE) >= p_plan_start_date  --4135752
1452                 AND     trunc(SHIFT_DATE) < trunc(nvl(p_itf, SHIFT_DATE+1)) -- 2859130  --4135752
1453      	   	);
1454 END get_res_avail_unopt_bat_dtls;
1455 
1456 PROCEDURE get_res_avail_summ(
1457    p_instance_id        IN NUMBER,
1458    p_org_id             IN NUMBER,
1459    p_plan_id            IN NUMBER,
1460    p_plan_start_date    IN DATE,
1461    p_dept_id            IN NUMBER,
1462    p_res_id             IN NUMBER,
1463    p_itf                IN DATE,
1464    p_refresh_number     IN NUMBER,  -- For summary enhancement
1465    x_atp_dates          OUT NoCopy MRP_ATP_PUB.date_arr,
1466    x_atp_qtys           OUT NoCopy MRP_ATP_PUB.number_arr
1467 ) IS
1468 BEGIN
1469     IF PG_DEBUG in ('Y', 'C') THEN
1470         msc_sch_wb.atp_debug('get_res_avail_summ');
1471     END IF;
1472 
1473     SELECT  SD_DATE,
1474             SUM(SD_QTY)
1475     BULK COLLECT INTO x_atp_dates, x_atp_qtys
1476     FROM
1477         (
1478             select  /*+ INDEX(r MSC_ATP_SUMMARY_RES_U1) */
1479                     trunc(r.sd_date) SD_DATE,  --4135752
1480                     r.sd_qty
1481             from    msc_atp_summary_res r
1482             where   r.plan_id = p_plan_id and
1483                     r.sr_instance_id = p_instance_id and
1484                     r.organization_id = p_org_id and
1485                     r.department_id = p_dept_id and
1486                     r.resource_id = p_res_id and
1487                     ---bug 2341075: change sysdate to plan start date
1488                     --sd_date >= trunc(sysdate) and
1489                     sd_date >= p_plan_start_date and  --4135752
1490                     sd_date < trunc(nvl(p_itf, sd_date + 1)) and -- 2859130  --4135752
1491                     sd_qty <> 0
1492 
1493             UNION ALL
1494 
1495             -- Summary enhancement : differences from non summary SQL:
1496             --  1. No union with MSC_NET_RES_AVAIL
1497             --  2. Get the hours always from RESOURCE_HOURS - never from LOAD_RATE or DAILY_RESOURCE_HOURS
1498             --  3. PARENT_ID removed from where clause. No difference between constrained and unconstrained plans
1499             --  4. MSC_SYSTEM_ITEMS not included in the join because the filters on items is not applied for ATP records
1500             --  5. MSC_PLANS included in the join to get latest refresh number
1501             --  6. Filter records based on refresh_number
1502                     -- Bug 3348095
1503                     -- For ATP created records use end_date otherwise start_date
1504             SELECT  TRUNC(NVL(REQ.END_DATE, REQ.START_DATE))   SD_DATE,
1505                     -- End Bug 3348095
1506                     -1 * REQ.RESOURCE_HOURS SD_QTY  -- Summary enhancement: Need to bother only about ATP generated records
1507             FROM    MSC_DEPARTMENT_RESOURCES DR,
1508                     MSC_RESOURCE_REQUIREMENTS REQ,
1509                     MSC_PLANS P                     -- For summary enhancement
1510             --bug3394866
1511             WHERE   DR.PLAN_ID = p_plan_id
1512             AND     NVL(DR.OWNING_DEPARTMENT_ID, DR.DEPARTMENT_ID)=p_dept_id
1513             AND     DR.RESOURCE_ID = p_res_id
1514             AND     DR.SR_INSTANCE_ID = p_instance_id
1515             AND     DR.organization_id = p_org_id
1516             AND     REQ.PLAN_ID = DR.PLAN_ID
1517             AND     REQ.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
1518             AND     REQ.RESOURCE_ID = DR.RESOURCE_ID
1519             AND     REQ.DEPARTMENT_ID = DR.DEPARTMENT_ID
1520             AND     REQ.ORGANIZATION_ID = DR.ORGANIZATION_ID
1521             --bug3394866
1522             --bug3693892 added trunc
1523             AND     TRUNC(REQ.START_DATE) >= p_plan_start_date  --4135752
1524             AND     TRUNC(REQ.START_DATE) < trunc(nvl(p_itf, REQ.START_DATE+1))
1525             AND     P.PLAN_ID = REQ.PLAN_ID
1526             AND     (REQ.REFRESH_NUMBER > P.LATEST_REFRESH_NUMBER
1527                      OR REQ.REFRESH_NUMBER = p_refresh_number)
1528         )
1529     GROUP BY SD_DATE
1530     ORDER BY SD_DATE;
1531 
1532     print_dates_qtys(x_atp_dates, x_atp_qtys);
1533 
1534     IF MSC_ATP_PVT.G_RES_CONSUME = 'Y' THEN
1535         MSC_ATP_PROC.atp_consume(x_atp_qtys, x_atp_qtys.COUNT);
1536     END IF;
1537 
1538 END get_res_avail_summ;
1539 
1540 PROCEDURE get_res_avail(
1541    p_batching_flag      IN NUMBER,
1542    p_optimized_flag     IN NUMBER,
1543    p_instance_id        IN NUMBER,
1544    p_org_id             IN NUMBER,
1545    p_plan_id            IN NUMBER,
1546    p_plan_start_date    IN DATE,
1547    p_dept_id            IN NUMBER,
1548    p_res_id             IN NUMBER,
1549    p_itf                IN DATE,
1550    p_uom_type           IN NUMBER,
1551    p_max_capacity       IN NUMBER,
1552    p_res_conv_rate      IN NUMBER,
1553    p_cal_code           IN VARCHAR2,
1554    p_cal_exc_set_id     IN NUMBER,
1555    x_atp_dates          OUT NoCopy MRP_ATP_PUB.date_arr,
1556    x_atp_qtys           OUT NoCopy MRP_ATP_PUB.number_arr
1557 ) IS
1558 BEGIN
1559    IF PG_DEBUG in ('Y', 'C') THEN
1560            msc_sch_wb.atp_debug('get_res_avail');
1561    END IF;
1562 
1563    IF nvl(p_batching_flag, 0) = 1 THEN
1564       IF nvl(p_optimized_flag, 0) = 1 THEN
1565          get_res_avail_opt_bat(
1566             p_instance_id,
1567             p_org_id,
1568             p_plan_id,
1569             p_plan_start_date,
1570             p_dept_id,
1571             p_res_id,
1572             p_itf,
1573             p_uom_type,
1574             p_max_capacity,
1575             p_res_conv_rate,
1576             x_atp_dates,
1577             x_atp_qtys
1578          );
1579       ELSE
1580          get_res_avail_unopt_bat(
1581             p_instance_id,
1582             p_org_id,
1583             p_plan_id,
1584             p_plan_start_date,
1585             p_dept_id,
1586             p_res_id,
1587             p_itf,
1588             p_uom_type,
1589             p_max_capacity,
1590             p_res_conv_rate,
1591             p_cal_code,
1592             p_cal_exc_set_id,
1593             x_atp_dates,
1594             x_atp_qtys
1595          );
1596       END IF;
1597    ELSE
1598       IF nvl(p_optimized_flag, 0) = 1 THEN
1599          get_res_avail_opt(
1600             p_instance_id,
1601             p_org_id,
1602             p_plan_id,
1603             p_plan_start_date,
1604             p_dept_id,
1605             p_res_id,
1606             p_itf,
1607             x_atp_dates,
1608             x_atp_qtys
1609          );
1610       ELSE
1611          get_res_avail_unopt(
1612             p_instance_id,
1613             p_org_id,
1614             p_plan_id,
1615             p_plan_start_date,
1616             p_dept_id,
1617             p_res_id,
1618             p_itf,
1619             p_cal_code,
1620             p_cal_exc_set_id,
1621             x_atp_dates,
1622             x_atp_qtys
1623          );
1624       END IF;
1625    END IF;
1626 
1627    IF MSC_ATP_PVT.G_RES_CONSUME = 'Y' THEN
1628        MSC_ATP_PROC.atp_consume(x_atp_qtys, x_atp_qtys.COUNT);
1629    END IF;
1630 
1631 END get_res_avail;
1632 
1633 PROCEDURE get_res_avail_dtls(
1634    p_batching_flag      IN NUMBER,
1635    p_optimized_flag     IN NUMBER,
1636    p_instance_id        IN NUMBER,
1637    p_org_id             IN NUMBER,
1638    p_plan_id            IN NUMBER,
1639    p_plan_start_date    IN DATE,
1640    p_dept_id            IN NUMBER,
1641    p_res_id             IN NUMBER,
1642    p_itf                IN DATE,
1643    p_uom_type           IN NUMBER,
1644    p_uom_code           IN VARCHAR2,
1645    p_max_capacity       IN NUMBER,
1646    p_res_conv_rate      IN NUMBER,
1647    p_level              IN NUMBER,
1648    p_scenario_id        IN NUMBER,
1649    p_item_id            IN NUMBER,
1650    p_cal_code           IN VARCHAR2,
1651    p_cal_exc_set_id     IN NUMBER,
1652    x_atp_period         OUT NOCOPY MRP_ATP_PUB.ATP_Period_Typ
1653 ) IS
1654 BEGIN
1655 
1656    MSC_ATP_DB_UTILS.Clear_SD_Details_temp();
1657 
1658    IF nvl(p_batching_flag, 0) = 1 THEN
1659       IF nvl(p_optimized_flag, 0) = 1 THEN
1660          get_res_avail_opt_bat_dtls(
1661             p_instance_id,
1662             p_org_id,
1663             p_plan_id,
1664             p_plan_start_date,
1665             p_dept_id,
1666             p_res_id,
1667             p_itf,
1668             p_uom_type,
1669             p_uom_code,
1670             p_max_capacity,
1671             p_res_conv_rate,
1672             p_level,
1673             p_scenario_id
1674          );
1675       ELSE
1676          get_res_avail_unopt_bat_dtls(
1677             p_instance_id,
1678             p_org_id,
1679             p_plan_id,
1680             p_plan_start_date,
1681             p_dept_id,
1682             p_res_id,
1683             p_itf,
1684             p_uom_type,
1685             p_uom_code,
1686             p_max_capacity,
1687             p_res_conv_rate,
1688             p_level,
1689             p_scenario_id,
1690             p_cal_code,
1691             p_cal_exc_set_id
1692          );
1693       END IF;
1694    ELSE
1695       IF nvl(p_optimized_flag, 0) = 1 THEN
1696          get_res_avail_opt_dtls(
1697             p_instance_id,
1698             p_org_id,
1699             p_plan_id,
1700             p_plan_start_date,
1701             p_dept_id,
1702             p_res_id,
1703             p_itf,
1704             p_uom_code,
1705             p_level,
1706             p_scenario_id
1707          );
1708       ELSE
1709          get_res_avail_unopt_dtls(
1710             p_instance_id,
1711             p_org_id,
1712             p_plan_id,
1713             p_plan_start_date,
1714             p_dept_id,
1715             p_res_id,
1716             p_itf,
1717             p_uom_code,
1718             p_level,
1719             p_scenario_id,
1720             p_cal_code,
1721             p_cal_exc_set_id
1722          );
1723       END IF;
1724    END IF;
1725 
1726    MSC_ATP_PROC.get_period_data_from_SD_temp(x_atp_period);
1727    x_atp_period.cumulative_quantity := x_atp_period.period_quantity;
1728 
1729 print_dates_qtys(x_atp_period.period_start_date,x_atp_period.period_quantity);
1730    IF MSC_ATP_PVT.G_RES_CONSUME = 'Y' THEN
1731        MSC_ATP_PROC.atp_consume(x_atp_period.Cumulative_Quantity,
1732                                 x_atp_period.Cumulative_Quantity.count);
1733    END IF;
1734 
1735 END get_res_avail_dtls;
1736 
1737 PROCEDURE get_unalloc_res_avail(
1738    p_insert_flag        IN NUMBER,
1739    p_batching_flag      IN NUMBER,
1740    p_optimized_flag     IN NUMBER,
1741    p_instance_id        IN NUMBER,
1742    p_org_id             IN NUMBER,
1743    p_plan_id            IN NUMBER,
1744    p_plan_start_date    IN DATE,
1745    p_dept_id            IN NUMBER,
1746    p_res_id             IN NUMBER,
1747    p_itf                IN DATE,
1748    p_uom_type           IN NUMBER,
1749    p_uom_code           IN VARCHAR2,
1750    p_max_capacity       IN NUMBER,
1751    p_res_conv_rate      IN NUMBER,
1752    p_level              IN NUMBER,
1753    p_scenario_id        IN NUMBER,
1754    p_item_id            IN NUMBER,
1755    p_cal_code           IN VARCHAR2,
1756    p_cal_exc_set_id     IN NUMBER,
1757    p_summary_flag       IN VARCHAR2,    -- For summary enhancement
1758    p_refresh_number     IN NUMBER,      -- For summary enhancement
1759    x_atp_dates          OUT NoCopy MRP_ATP_PUB.date_arr,
1760    x_atp_qtys           OUT NoCopy MRP_ATP_PUB.number_arr,
1761    x_atp_period         OUT NOCOPY MRP_ATP_PUB.ATP_Period_Typ
1762 ) IS
1763 BEGIN
1764   IF nvl(p_insert_flag,0) = 1 THEN
1765      get_res_avail_dtls(
1766             p_batching_flag,
1767             p_optimized_flag,
1768             p_instance_id,
1769             p_org_id,
1770             p_plan_id,
1771             p_plan_start_date,
1772             p_dept_id,
1773             p_res_id,
1774             p_itf,
1775             p_uom_type,
1776             p_uom_code,
1777             p_max_capacity,
1778             p_res_conv_rate,
1779             p_level,
1780             p_scenario_id,
1781             p_item_id,
1782             p_cal_code,
1783             p_cal_exc_set_id,
1784             x_atp_period
1785      );
1786 
1787      IF p_itf IS NOT NULL THEN
1788          MSC_ATP_PROC.add_inf_time_fence_to_period(
1789                  p_level,
1790                  MSC_ATP_PVT.G_ORDER_LINE_ID, -- identifier
1791                  p_scenario_id,
1792                  p_item_id,
1793                  p_item_id, -- requested item id
1794                  p_org_id,
1795                  null,                -- p_supplier_id
1796                  null,                -- p_supplier_site_id
1797                  p_itf,
1798                  x_atp_period);
1799 
1800      END IF;
1801 
1802      x_atp_dates := x_atp_period.Period_Start_Date;
1803      x_atp_qtys  := x_atp_period.Cumulative_Quantity;
1804   ELSE
1805      IF p_summary_flag = 'Y' AND  -- MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y' AND -- changed for summary enhancement
1806         (MSC_ATP_PVT.G_ALLOCATED_ATP = 'N' OR
1807             (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y' AND
1808              MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1 AND
1809              MSC_ATP_PVT.G_ALLOCATION_METHOD = 1))
1810      THEN
1811         get_res_avail_summ(
1812             p_instance_id,
1813             p_org_id,
1814             p_plan_id,
1815             p_plan_start_date,
1816             p_dept_id,
1817             p_res_id,
1818             p_itf,
1819             p_refresh_number,   -- For summary enhancement
1820             x_atp_dates,
1821             x_atp_qtys
1822         );
1823      ELSE
1824         get_res_avail(
1825             p_batching_flag,
1826             p_optimized_flag,
1827             p_instance_id,
1828             p_org_id,
1829             p_plan_id,
1830             p_plan_start_date,
1831             p_dept_id,
1832             p_res_id,
1833             p_itf,
1834             p_uom_type,
1835             p_max_capacity,
1836             p_res_conv_rate,
1837             p_cal_code,
1838             p_cal_exc_set_id,
1839             x_atp_dates,
1840             x_atp_qtys
1841          );
1842       END IF;
1843 
1844       IF p_itf IS NOT NULL THEN
1845         -- add one more entry to indicate infinite time fence date
1846         -- and quantity.
1847         x_atp_dates.EXTEND;
1848         x_atp_qtys.EXTEND;
1849         x_atp_dates(x_atp_dates.count) := p_itf;
1850         x_atp_qtys(x_atp_qtys.count) := MSC_ATP_PVT.INFINITE_NUMBER;
1851       END IF;
1852 
1853    END IF;
1854 END get_unalloc_res_avail;
1855 
1856 ----------------------------------------------------------------------------
1857 
1858 PROCEDURE get_mat_avail_ods_summ (
1859    p_item_id            IN NUMBER,
1860    p_org_id             IN NUMBER,
1861    p_instance_id        IN NUMBER,
1862    p_plan_id            IN NUMBER,
1863    p_demand_class       IN VARCHAR2,
1864    p_default_atp_rule_id IN NUMBER,
1865    p_default_dmd_class  IN VARCHAR2,
1866    p_itf                IN DATE,
1867    x_atp_dates          OUT NoCopy MRP_ATP_PUB.date_arr,
1868    x_atp_qtys           OUT NoCopy MRP_ATP_PUB.number_arr
1869 ) IS
1870 BEGIN
1871         IF PG_DEBUG in ('Y', 'C') THEN
1872            msc_sch_wb.atp_debug('Begin get_mat_avail_ods_summ');
1873         END IF;
1874 
1875         SELECT SD_DATE, sum(SD_QTY)
1876         BULK COLLECT INTO x_atp_dates, x_atp_qtys
1877         FROM
1878         (SELECT  /*+ INDEX(D MSC_ATP_SUMMARY_SO_U1) */
1879                  trunc(D.SD_DATE) SD_DATE,   --4135752
1880                  -1* D.SD_QTY SD_QTY
1881         FROM        MSC_ATP_SUMMARY_SO D,
1882                     MSC_ATP_RULES R,
1883                     MSC_SYSTEM_ITEMS I
1884         WHERE       I.SR_INVENTORY_ITEM_ID = p_item_id
1885         AND         I.ORGANIZATION_ID = p_org_id
1886         AND         I.SR_INSTANCE_ID = p_instance_id
1887         AND         I.PLAN_ID = p_plan_id
1888         AND         R.RULE_ID (+) = NVL(I.ATP_RULE_ID, p_default_atp_rule_id)
1889         AND	    R.SR_INSTANCE_ID (+)= I.SR_INSTANCE_ID
1890         AND	    D.PLAN_ID = I.PLAN_ID
1891         AND	    D.SR_INSTANCE_ID = I.SR_INSTANCE_ID
1892         AND	    D.INVENTORY_ITEM_ID = I.INVENTORY_ITEM_ID
1893         AND 	    D.ORGANIZATION_ID = I.ORGANIZATION_ID
1894         AND         trunc(D.SD_DATE) < trunc(NVL(p_itf,  --4135752
1895                          D.SD_DATE + 1))
1896         AND         NVL(D.DEMAND_CLASS, NVL(p_default_dmd_class,'@@@')) =
1897                              DECODE(R.DEMAND_CLASS_ATP_FLAG,
1898                              1, NVL(P_DEMAND_CLASS, NVL(p_default_dmd_class,'@@@')),
1899                              NVL(D.DEMAND_CLASS, NVL(p_default_dmd_class,'@@@')))
1900         AND D.SD_QTY <> 0
1901         UNION ALL
1902 
1903         SELECT      /*+ INDEX(S MSC_ATP_SUMMARY_SD_U1) */
1904                     trunc(S.SD_DATE) SD_DATE,   --4135752
1905                     S.SD_QTY SD_QTY
1906         FROM        MSC_ATP_SUMMARY_SD S,
1907                     MSC_ATP_RULES R,
1908                     MSC_SYSTEM_ITEMS I
1909         WHERE       I.SR_INVENTORY_ITEM_ID = p_item_id
1910         AND         I.ORGANIZATION_ID = p_org_id
1911         AND         I.SR_INSTANCE_ID = p_instance_id
1912         AND         I.PLAN_ID = p_plan_id
1913         AND         R.RULE_ID (+) = NVL(I.ATP_RULE_ID, p_default_atp_rule_id)
1914         AND         R.SR_INSTANCE_ID (+)= I.SR_INSTANCE_ID
1915         AND	    S.PLAN_ID = I.PLAN_ID
1916         AND	    S.SR_INSTANCE_ID = I.SR_INSTANCE_ID
1917         AND	    S.INVENTORY_ITEM_ID = I.INVENTORY_ITEM_ID
1918         AND 	    S.ORGANIZATION_ID = I.ORGANIZATION_ID
1919         AND         trunc(S.SD_DATE) < trunc(NVL(p_itf, S.SD_DATE + 1))  --4135752
1920         AND         NVL(S.DEMAND_CLASS, NVL(p_default_dmd_class,'@@@')) =
1921                              DECODE(R.DEMAND_CLASS_ATP_FLAG,
1922                              1, NVL(P_DEMAND_CLASS, NVL(p_default_dmd_class,'@@@')),
1923                              NVL(S.DEMAND_CLASS, NVL(p_default_dmd_class,'@@@')))
1924         AND      S.SD_QTY <> 0
1925 
1926         )
1927         group by SD_DATE
1928         order by SD_DATE;--4698199
1929 END get_mat_avail_ods_summ;
1930 
1931 PROCEDURE get_mat_avail_summ (
1932     p_item_id           IN NUMBER,
1933     p_org_id            IN NUMBER,
1934     p_instance_id       IN NUMBER,
1935     p_plan_id           IN NUMBER,
1936     p_itf               IN DATE,
1937     p_refresh_number    IN NUMBER,   -- For summary enhancement
1938     x_atp_dates         OUT NoCopy MRP_ATP_PUB.date_arr,
1939     x_atp_qtys          OUT NoCopy MRP_ATP_PUB.number_arr
1940 ) IS
1941 BEGIN
1942         IF PG_DEBUG in ('Y', 'C') THEN
1943            msc_sch_wb.atp_debug('Begin get_mat_avail_summ');
1944         END IF;
1945 
1946     -- SQL changed for summary enhancement
1947     SELECT  SD_DATE, SUM(SD_QTY)
1948     BULK COLLECT INTO x_atp_dates, x_atp_qtys
1949     FROM   (
1950             SELECT  /*+ INDEX(S MSC_ATP_SUMMARY_SD_U1) */
1951                     SD_DATE, SD_QTY
1952             FROM    MSC_ATP_SUMMARY_SD S,
1953                     MSC_SYSTEM_ITEMS I
1954             WHERE   I.SR_INVENTORY_ITEM_ID = p_item_id
1955             AND     I.ORGANIZATION_ID = p_org_id
1956             AND     I.SR_INSTANCE_ID = p_instance_id
1957             AND     I.PLAN_ID = p_plan_id
1958             AND     S.PLAN_ID = I.PLAN_ID
1959             AND     S.SR_INSTANCE_ID = I.SR_INSTANCE_ID
1960             AND     S.INVENTORY_ITEM_ID = I.INVENTORY_ITEM_ID
1961             AND     S.ORGANIZATION_ID = I.ORGANIZATION_ID
1962             AND     trunc(S.SD_DATE) < trunc(NVL(p_itf, S.SD_DATE + 1))  --4135752
1963 
1964             UNION ALL
1965 
1966             SELECT  TRUNC(NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)) SD_DATE,--plan by request,promise,schedule date
1967                     decode(D.USING_REQUIREMENT_QUANTITY,            -- Consider unscheduled orders as dummy supplies
1968                     0, nvl(D.OLD_DEMAND_QUANTITY,0), --4658238                -- For summary enhancement
1969                     -1 * D.USING_REQUIREMENT_QUANTITY)  SD_QTY
1970             FROM    MSC_DEMANDS D,
1971                     MSC_SYSTEM_ITEMS I,
1972                     MSC_PLANS P                                     -- For summary enhancement
1973             WHERE   I.SR_INVENTORY_ITEM_ID = p_item_id
1974             AND     I.ORGANIZATION_ID = p_org_id
1975             AND     I.SR_INSTANCE_ID = p_instance_id
1976             AND     I.PLAN_ID = p_plan_id
1977             AND     D.PLAN_ID = I.PLAN_ID
1978             AND     D.SR_INSTANCE_ID = I.SR_INSTANCE_ID
1979             AND     D.INVENTORY_ITEM_ID = I.INVENTORY_ITEM_ID
1980             AND     D.ORGANIZATION_ID = I.ORGANIZATION_ID
1981             AND     D.ORIGINATION_TYPE NOT IN (4,5,7,8,9,11,15,22,28,29,31,70)
1982             AND     D.USING_REQUIREMENT_QUANTITY <> 0 --4501434
1983                     --bug3693892 added trunc
1984             AND     trunc(NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)) <
1985             		   trunc(NVL(p_itf, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE) + 1))
1986             		   --plan by requestdate,promisedate,scheduledate
1987             AND     P.PLAN_ID = I.PLAN_ID
1988             AND     (D.REFRESH_NUMBER > P.LATEST_REFRESH_NUMBER
1989                      OR D.REFRESH_NUMBER = p_refresh_number)
1990 
1991             UNION ALL
1992 
1993             SELECT  TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)) SD_DATE,
1994                     NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY)  SD_QTY
1995             FROM    MSC_SUPPLIES S,
1996                     MSC_SYSTEM_ITEMS I,
1997                     MSC_PLANS P                                     -- For summary enhancement
1998             WHERE   I.SR_INVENTORY_ITEM_ID = p_item_id
1999             AND     I.ORGANIZATION_ID = p_org_id
2000             AND     I.SR_INSTANCE_ID = p_instance_id
2001             AND     I.PLAN_ID = p_plan_id
2002             AND     S.PLAN_ID = I.PLAN_ID
2003             AND     S.SR_INSTANCE_ID = I.SR_INSTANCE_ID
2004             AND     S.INVENTORY_ITEM_ID = I.INVENTORY_ITEM_ID
2005             AND     S.ORGANIZATION_ID = I.ORGANIZATION_ID
2006             AND     NVL(S.DISPOSITION_STATUS_TYPE, 1) <> 2          -- These two conditions
2007             AND     NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY) <> 0  -- may not be required
2008             AND     TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)) < NVL(p_itf, TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)) + 1)
2009             AND     P.PLAN_ID = I.PLAN_ID
2010             AND     (S.REFRESH_NUMBER > P.LATEST_REFRESH_NUMBER
2011                      OR S.REFRESH_NUMBER = p_refresh_number)
2012 
2013     )
2014     GROUP BY SD_DATE
2015     ORDER BY SD_DATE;
2016 END get_mat_avail_summ;
2017 
2018 PROCEDURE get_mat_avail_ods (
2019    p_item_id            IN NUMBER,
2020    p_org_id             IN NUMBER,
2021    p_instance_id        IN NUMBER,
2022    p_plan_id            IN NUMBER,
2023    p_cal_code           IN VARCHAR2,
2024    p_cal_exc_set_id     IN NUMBER,
2025    p_sysdate_seq_num    IN NUMBER,
2026    p_sys_next_date      IN DATE,
2027    p_demand_class       IN VARCHAR2,
2028    p_default_atp_rule_id IN NUMBER,
2029    p_default_dmd_class  IN VARCHAR2,
2030    p_itf                IN DATE,
2031    x_atp_dates          OUT NoCopy MRP_ATP_PUB.date_arr,
2032    x_atp_qtys           OUT NoCopy MRP_ATP_PUB.number_arr
2033 ) IS
2034 BEGIN
2035         IF PG_DEBUG in ('Y', 'C') THEN
2036            msc_sch_wb.atp_debug('Begin get_mat_avail_ods');
2037         END IF;
2038 
2039         -- SQL Query changes Begin 2640489
2040         SELECT 	SD_DATE, SUM(SD_QTY)
2041         BULK COLLECT INTO x_atp_dates, x_atp_qtys
2042         FROM (
2043         SELECT  C.PRIOR_DATE SD_DATE,
2044                 -1* D.USING_REQUIREMENT_QUANTITY SD_QTY
2045         FROM    MSC_CALENDAR_DATES C,
2046                 MSC_DEMANDS D,
2047                 MSC_ATP_RULES R,
2048                 MSC_SYSTEM_ITEMS I
2049         WHERE   I.SR_INVENTORY_ITEM_ID = p_item_id
2050         AND     I.ORGANIZATION_ID = p_org_id
2051         AND     I.SR_INSTANCE_ID = p_instance_id
2052         AND     I.PLAN_ID = p_plan_id
2053         AND     R.RULE_ID (+) = NVL(I.ATP_RULE_ID, p_default_atp_rule_id)
2054         AND     R.SR_INSTANCE_ID (+)= I.SR_INSTANCE_ID
2055         AND     D.PLAN_ID = I.PLAN_ID
2056         AND     D.SR_INSTANCE_ID = I.SR_INSTANCE_ID
2057         AND     D.INVENTORY_ITEM_ID = I.INVENTORY_ITEM_ID
2058         AND     D.ORGANIZATION_ID = I.ORGANIZATION_ID
2059         -- 1243985
2060         AND     USING_REQUIREMENT_QUANTITY <> 0
2061         AND     D.ORIGINATION_TYPE in (
2062                 DECODE(R.INCLUDE_DISCRETE_WIP_DEMAND, 1, 3, -1),
2063                 DECODE(R.INCLUDE_FLOW_SCHEDULE_DEMAND, 1, 25, -1),
2064                 DECODE(R.INCLUDE_USER_DEFINED_DEMAND, 1, 42, -1),
2065                 DECODE(R.INCLUDE_NONSTD_WIP_RECEIPTS, 1, 2, -1),
2066                 DECODE(R.INCLUDE_REP_WIP_DEMAND, 1, 4, -1))
2067         -- Bug 1530311, forecast to be excluded
2068         AND	C.CALENDAR_CODE = p_cal_code
2069         AND	C.EXCEPTION_SET_ID = p_cal_exc_set_id
2070         AND     C.SR_INSTANCE_ID = I.SR_INSTANCE_ID
2071         -- since we store repetitive schedule demand in different ways for
2072         -- ods (total quantity on start date) and pds  (daily quantity from
2073         -- start date to end date), we need to make sure we only select work day
2074         -- for pds's repetitive schedule demand.
2075         AND     C.CALENDAR_DATE BETWEEN TRUNC(D.USING_ASSEMBLY_DEMAND_DATE) AND
2076                 TRUNC(NVL(D.ASSEMBLY_DEMAND_COMP_DATE,
2077                           D.USING_ASSEMBLY_DEMAND_DATE))
2078                 -- new clause 2640489, DECODE is also OR, Explicit OR gives CBO choices
2079         AND     (R.PAST_DUE_DEMAND_CUTOFF_FENCE is NULL OR
2080                  C.PRIOR_SEQ_NUM >= p_sysdate_seq_num - R.PAST_DUE_DEMAND_CUTOFF_FENCE)
2081         -- AND     C.PRIOR_SEQ_NUM >= DECODE(R.PAST_DUE_DEMAND_CUTOFF_FENCE,
2082         --                NULL, C.PRIOR_SEQ_NUM,
2083         --                p_sysdate_seq_num - NVL(R.PAST_DUE_DEMAND_CUTOFF_FENCE,0))
2084         AND     C.PRIOR_DATE < NVL(p_itf,
2085                                  C.PRIOR_DATE + 1)
2086                 -- new clause 2640489, DECODE is also OR, Explicit OR gives CBO choices
2087         AND     (R.DEMAND_CLASS_ATP_FLAG <> 1 OR
2088                  NVL(D.DEMAND_CLASS, NVL(p_default_dmd_class,'@@@')) =
2089                    NVL(P_DEMAND_CLASS, NVL(p_default_dmd_class,'@@@')) )
2090         -- AND     NVL(D.DEMAND_CLASS, NVL(p_default_dmd_class,'@@@')) =
2091         --         DECODE(R.DEMAND_CLASS_ATP_FLAG,
2092         --         1, NVL(P_DEMAND_CLASS, NVL(p_default_dmd_class,'@@@')),
2093         --         NVL(D.DEMAND_CLASS, NVL(p_default_dmd_class,'@@@')))
2094         UNION ALL
2095         -- bug 2461071 to_date and trunc
2096         SELECT  DECODE(D.RESERVATION_TYPE, 2, p_sys_next_date, -- to_date removed to avoid GSCC error
2097                 TRUNC(D.REQUIREMENT_DATE)) SD_DATE, --2287148
2098                 -1*(D.PRIMARY_UOM_QUANTITY-GREATEST(NVL(D.RESERVATION_QUANTITY,0),
2099                     D.COMPLETED_QUANTITY)) SD_QTY
2100         FROM
2101                 -- Bug 1756263, performance fix, use EXISTS subquery instead.
2102                 --MSC_CALENDAR_DATES C,
2103                 MSC_SALES_ORDERS D,
2104                 MSC_ATP_RULES R,
2105                 MSC_SYSTEM_ITEMS I,
2106                 MSC_CALENDAR_DATES C
2107         WHERE   I.SR_INVENTORY_ITEM_ID = p_item_id
2108         AND     I.ORGANIZATION_ID = p_org_id
2109         AND     I.SR_INSTANCE_ID = p_instance_id
2110         AND     I.PLAN_ID = p_plan_id
2111         AND     R.RULE_ID (+) = NVL(I.ATP_RULE_ID, p_default_atp_rule_id)
2112         AND     R.SR_INSTANCE_ID (+)= I.SR_INSTANCE_ID
2113         AND     D.SR_INSTANCE_ID = I.SR_INSTANCE_ID
2114         AND     D.INVENTORY_ITEM_ID = I.INVENTORY_ITEM_ID
2115         AND     D.ORGANIZATION_ID = I.ORGANIZATION_ID
2116         AND     D.DEMAND_SOURCE_TYPE <> DECODE(R.INCLUDE_SALES_ORDERS,2,2,-1)
2117         AND     D.DEMAND_SOURCE_TYPE <> DECODE(R.INCLUDE_INTERNAL_ORDERS,2,8,-1)
2118         AND     D.PRIMARY_UOM_QUANTITY > GREATEST(NVL(D.RESERVATION_QUANTITY,0),
2119                 D.COMPLETED_QUANTITY)
2120         AND     DECODE(MSC_ATP_PVT.G_APPS_VER,3,D.COMPLETED_QUANTITY,0) = 0 -- 2300767
2121         AND     (D.SUBINVENTORY IS NULL OR D.SUBINVENTORY IN
2122                    (SELECT S.SUB_INVENTORY_CODE
2123                     FROM   MSC_SUB_INVENTORIES S
2124                     WHERE  S.ORGANIZATION_ID=D.ORGANIZATION_ID
2125                     AND    S.PLAN_ID = I.PLAN_ID
2126                     AND    S.SR_INSTANCE_ID = D.SR_INSTANCE_ID
2127                     AND    S.INVENTORY_ATP_CODE =DECODE(R.DEFAULT_ATP_SOURCES,
2128                                    1, 1, NULL, 1, S.INVENTORY_ATP_CODE)
2129                     AND    S.NETTING_TYPE =DECODE(R.DEFAULT_ATP_SOURCES,
2130                                    2, 1, S.NETTING_TYPE)))
2131         AND     (D.RESERVATION_TYPE = 2
2132                  OR D.PARENT_DEMAND_ID IS NULL
2133                  OR (D.RESERVATION_TYPE = 3 AND
2134                      ((R.INCLUDE_DISCRETE_WIP_RECEIPTS = 1) or
2135                       (R.INCLUDE_NONSTD_WIP_RECEIPTS = 1))))
2136                 -- new clause, remove existing Exists Query 2640489
2137         AND     (R.PAST_DUE_DEMAND_CUTOFF_FENCE is NULL OR
2138                     C.PRIOR_SEQ_NUM >= p_sysdate_seq_num - R.PAST_DUE_DEMAND_CUTOFF_FENCE)
2139         AND     C.CALENDAR_CODE = p_cal_code
2140         AND     C.SR_INSTANCE_ID = I.SR_INSTANCE_ID
2141         AND     C.EXCEPTION_SET_ID = -1
2142         AND     C.CALENDAR_DATE = TRUNC(D.REQUIREMENT_DATE)
2143         AND     C.PRIOR_DATE < NVL(p_itf, C.PRIOR_DATE + 1)
2144         -- new clause 2640489, DECODE is also OR, Explicit OR gives CBO choices
2145         AND         (R.DEMAND_CLASS_ATP_FLAG <> 1 OR
2146                      NVL(D.DEMAND_CLASS, NVL(p_default_dmd_class,'@@@')) =
2147                        NVL(P_DEMAND_CLASS, NVL(p_default_dmd_class,'@@@')) )
2148         UNION ALL
2149         SELECT  C.NEXT_DATE SD_DATE,
2150                 Decode(order_type,
2151                 30, Decode(Sign(S.Daily_rate * (TRUNC(C.Calendar_date) -  TRUNC(S.FIRST_UNIT_START_DATE))- S.qty_completed),
2152                              -1,S.Daily_rate* (TRUNC(C.Calendar_date) - TRUNC(S.First_Unit_Start_date) +1)- S.qty_completed,
2153                               S.Daily_rate),
2154                 5, NVL(S.DAILY_RATE, NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY)),
2155                     (NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY) - NVL(S.NON_NETTABLE_QTY, 0)) )SD_QTY
2156         FROM    MSC_CALENDAR_DATES C,
2157                 MSC_SUPPLIES S,
2158                 MSC_ATP_RULES R,
2159                 MSC_SYSTEM_ITEMS I,
2160                 MSC_SUB_INVENTORIES MSI
2161         WHERE   I.SR_INVENTORY_ITEM_ID = p_item_id
2162         AND     I.ORGANIZATION_ID = p_org_id
2163         AND     I.SR_INSTANCE_ID = p_instance_id
2164         AND     I.PLAN_ID = p_plan_id
2165         AND     R.RULE_ID (+) = NVL(I.ATP_RULE_ID, p_default_atp_rule_id)
2166         AND     R.SR_INSTANCE_ID (+)= I.SR_INSTANCE_ID
2167         AND     S.PLAN_ID = I.PLAN_ID
2168         AND     S.SR_INSTANCE_ID = I.SR_INSTANCE_ID
2169         AND     S.INVENTORY_ITEM_ID = I.INVENTORY_ITEM_ID
2170         AND     S.ORGANIZATION_ID = I.ORGANIZATION_ID
2171         --AND   NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY) <> 0 -- 1243985
2172         ---bug 1843471, 2563139
2173                 -- Bug 2132288, 2442009, 2453938
2174                 -- Do not include supplies equal to 0 as per 1243985
2175                 -- However at the same time, support negative supplies as per Bug 2362079 use ABS.
2176                 -- Support Repetitive schedules as per 1843471
2177                 -- Support Repetitive MPS as per 2132288, 2442009
2178         AND     Decode(S.order_type, 30, S.Daily_rate* (TRUNC(C.Calendar_date) - TRUNC(S.First_Unit_Start_date) + 1),
2179                                      5, NVL(S.Daily_rate, ABS(NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY)) ),
2180                                      ABS(NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY)) ) >
2181                       Decode(S.order_type, 30, S.qty_completed,0)
2182                 -- End Bug 2132288, 2442009, 2453938
2183         AND     (S.ORDER_TYPE IN (
2184                 DECODE(R.INCLUDE_PURCHASE_ORDERS, 1, 1, -1),
2185                 DECODE(R.INCLUDE_PURCHASE_ORDERS, 1, 8, -1), --1882898
2186                 DECODE(R.INCLUDE_DISCRETE_WIP_RECEIPTS, 1, 3, -1),
2187                 DECODE(R.INCLUDE_REP_WIP_RECEIPTS, 1, 30, -1),
2188                 DECODE(R.INCLUDE_NONSTD_WIP_RECEIPTS, 1, 7, -1),
2189                 DECODE(R.INCLUDE_NONSTD_WIP_RECEIPTS, 1, 15, -1),
2190                 DECODE(R.INCLUDE_INTERORG_TRANSFERS, 1, 11, -1),
2191                 DECODE(R.INCLUDE_INTERORG_TRANSFERS, 1, 12, -1),
2192                 DECODE(R.INCLUDE_ONHAND_AVAILABLE, 1, 18, -1),
2193                 DECODE(R.INCLUDE_USER_DEFINED_SUPPLY, 1, 41, -1),
2194                 DECODE(R.INCLUDE_FLOW_SCHEDULE_RECEIPTS, 1, 27, -1),
2195                 DECODE(R.INCLUDE_FLOW_SCHEDULE_RECEIPTS, 1, 28, -1))
2196                 OR
2197                 (INCLUDE_INTERNAL_REQS = 1 AND S.ORDER_TYPE = 2 AND
2198                  S.SOURCE_ORGANIZATION_ID IS NOT NULL)
2199                 OR
2200                 (INCLUDE_SUPPLIER_REQS = 1 AND S.ORDER_TYPE = 2 AND
2201                  S.SOURCE_ORGANIZATION_ID IS NULL)
2202                 OR
2203                 ((R.INCLUDE_REP_MPS = 1 OR R.INCLUDE_DISCRETE_MPS = 1) AND
2204                 S.ORDER_TYPE = 5
2205                 -- bug 2461071
2206                 AND exists (SELECT '1'
2207                                 FROM    MSC_DESIGNATORS
2208                                 WHERE   INVENTORY_ATP_FLAG = 1
2209                                 AND     DESIGNATOR_TYPE = 2
2210                                 AND     DESIGNATOR_ID = S.SCHEDULE_DESIGNATOR_ID
2211                                 AND     DECODE(R.demand_class_atp_flag,1,
2212                                         nvl(demand_class,
2213                                         nvl(p_default_dmd_class,'@@@')),'@@@') =
2214                                         DECODE(R.demand_class_atp_flag,1,
2215                                         nvl(p_demand_class,
2216                                         nvl(p_default_dmd_class,'@@@')),'@@@')
2217     )))
2218                 --AND MSC_ATP_FUNC.MPS_ATP(S.SCHEDULE_DESIGNATOR_ID) = 1))
2219         AND	C.CALENDAR_CODE = p_cal_code
2220         AND	C.EXCEPTION_SET_ID = p_cal_exc_set_id
2221         AND     C.SR_INSTANCE_ID = I.SR_INSTANCE_ID
2222                 -- Bug 2132288, 2442009
2223         AND     C.CALENDAR_DATE BETWEEN TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE))
2224                     AND TRUNC(NVL(DECODE(S.ORDER_TYPE, 5, S.LAST_UNIT_START_DATE,
2225                                    S.LAST_UNIT_COMPLETION_DATE), NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)))
2226         AND     DECODE(DECODE(S.ORDER_TYPE, 5, S.LAST_UNIT_START_DATE,
2227                                    S.LAST_UNIT_COMPLETION_DATE),
2228                        NULL, C.NEXT_SEQ_NUM, C.SEQ_NUM) IS NOT NULL
2229                  -- End Bug 2132288, 2442009
2230                  -- new clause 2640489, SIMPLIFY FOR CBO
2231         AND     (S.ORDER_TYPE = 18
2232                  OR R.PAST_DUE_SUPPLY_CUTOFF_FENCE is NULL
2233                  OR C.NEXT_SEQ_NUM >= p_sysdate_seq_num - R.PAST_DUE_SUPPLY_CUTOFF_FENCE)
2234         AND     C.NEXT_DATE >= DECODE(S.ORDER_TYPE, 27, TRUNC(SYSDATE),
2235                                                 28, TRUNC(SYSDATE),
2236                                                     C.NEXT_DATE)
2237         AND     C.NEXT_DATE < NVL(p_itf, C.NEXT_DATE + 1)
2238         AND     (R.DEMAND_CLASS_ATP_FLAG <> 1
2239                  OR S.ORDER_TYPE = 5
2240                  OR NVL(S.DEMAND_CLASS, NVL(p_default_dmd_class,'@@@')) =
2241                     NVL(P_DEMAND_CLASS, NVL(p_default_dmd_class,'@@@')) )
2242                                          ---bug 1735580
2243                 --- filter out non-atpable sub-inventories
2244         AND     MSI.plan_id (+) =  p_plan_id
2245         AND     MSI.organization_id (+) = p_org_id
2246         AND     MSI.sr_instance_id (+) =  p_instance_id
2247         --aND     S.subinventory_code = (+) MSI.sub_inventory_code
2248         AND     MSI.sub_inventory_code (+) = S.subinventory_code
2249         AND     NVL(MSI.inventory_atp_code,1) <> 2 -- filter out non-atpable subinventories
2250         -- SQL Query changes End 2640489
2251     )
2252     GROUP BY SD_DATE
2253     order by SD_DATE;--4698199
2254 END get_mat_avail_ods;
2255 
2256 PROCEDURE get_mat_avail_opt (
2257    p_item_id            IN NUMBER,
2258    p_org_id             IN NUMBER,
2259    p_instance_id        IN NUMBER,
2260    p_plan_id            IN NUMBER,
2261    p_itf                IN DATE,
2262    x_atp_dates          OUT NoCopy MRP_ATP_PUB.date_arr,
2263    x_atp_qtys           OUT NoCopy MRP_ATP_PUB.number_arr
2264 ) IS
2265 BEGIN
2266         IF PG_DEBUG in ('Y', 'C') THEN
2267            msc_sch_wb.atp_debug('Begin get_mat_avail_opt');
2268         END IF;
2269 
2270         -- 2859130 repetitive schedule demands (4) not supported
2271         -- remove join to msc_calendar_dates
2272         SELECT 	SD_DATE, SUM(SD_QTY)
2273         BULK COLLECT INTO x_atp_dates, x_atp_qtys
2274         FROM (
2275             SELECT     -- C.PRIOR_DATE SD_DATE, -- 2859130
2276             -- Bug 3550296 and 3574164. IMPLEMENT_DATE AND DMD_SATISFIED_DATE are changed to
2277             -- IMPLEMENT_SHIP_DATE and PLANNED_SHIP_DATE resp.
2278                        TRUNC(DECODE(D.RECORD_SOURCE,
2279                                     2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2280                                        DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2281                                               2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2282                                                  NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))) SD_DATE,
2283                                                  --plan by requestdate,promisedate,scheduledate
2284                        -- -1*D.USING_REQUIREMENT_QUANTITY SD_QTY
2285                        -1*(D.USING_REQUIREMENT_QUANTITY - NVL(d.reserved_quantity, 0)) SD_QTY --5027568
2286             FROM        MSC_DEMANDS D,
2287                         MSC_SYSTEM_ITEMS I
2288             WHERE       I.SR_INVENTORY_ITEM_ID = p_item_id
2289             AND         I.ORGANIZATION_ID = p_org_id
2290             AND         I.SR_INSTANCE_ID = p_instance_id
2291             AND         I.PLAN_ID = p_plan_id
2292             AND         D.PLAN_ID = I.PLAN_ID
2293             AND         D.SR_INSTANCE_ID = I.SR_INSTANCE_ID
2294             AND         D.INVENTORY_ITEM_ID = I.INVENTORY_ITEM_ID
2295             AND         D.ORGANIZATION_ID = I.ORGANIZATION_ID
2296             AND         D.ORIGINATION_TYPE NOT IN (4,5,7,8,9,11,15,22,28,29,31,52,70) -- ignore copy SO for summary enhancement
2297             -- Bug1990155, 1995835 exclude the expired lots demand datreya 9/18/2001
2298             -- Bug 1530311, forecast to be excluded
2299             -- new clause 2640489 SIMPLIFY
2300             -- AND         (C.SEQ_NUM IS NOT NULL OR D.ORIGINATION_TYPE  <> 4)
2301             -- AND         ((D.ORIGINATION_TYPE = 4 AND C.SEQ_NUM IS NOT NULL) OR
2302             --               (D.ORIGINATION_TYPE  <> 4))
2303             -- AND         C.PRIOR_DATE < NVL(p_itf, C.PRIOR_DATE + 1)
2304             -- Bug 3550296 and 3574164. IMPLEMENT_DATE AND DMD_SATISFIED_DATE are changed to
2305             -- IMPLEMENT_SHIP_DATE and PLANNED_SHIP_DATE resp.
2306             --bug3693892 added trunc
2307             AND         D.USING_REQUIREMENT_QUANTITY <> 0 --4501434
2308             AND         TRUNC(DECODE(D.RECORD_SOURCE,
2309                                     2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2310                                        DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2311                                               2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2312                                                  NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))))
2313             		< TRUNC(NVL(p_itf, DECODE(D.RECORD_SOURCE,
2314                                     2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2315                                        DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2316                                               2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2317                                                  NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))) + 1))
2318                                                  --plan by request date,promise date ,ship date
2319             UNION ALL
2320             SELECT      -- C.NEXT_DATE SD_DATE, -- 2859130
2321                         TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)) SD_DATE,
2322                         NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY)  SD_QTY
2323             FROM        MSC_SUPPLIES S,
2324                         MSC_SYSTEM_ITEMS I
2325             WHERE       I.SR_INVENTORY_ITEM_ID = p_item_id
2326             AND         I.ORGANIZATION_ID = p_org_id
2327             AND         I.SR_INSTANCE_ID = p_instance_id
2328             AND         I.PLAN_ID = p_plan_id
2329             AND         S.PLAN_ID = I.PLAN_ID
2330             AND         S.SR_INSTANCE_ID = I.SR_INSTANCE_ID
2331             AND         S.INVENTORY_ITEM_ID = I.INVENTORY_ITEM_ID
2332             AND         S.ORGANIZATION_ID = I.ORGANIZATION_ID
2333                         -- Exclude Cancelled Supplies 2460645
2334             AND         NVL(S.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
2335             AND         NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY) <> 0 -- 1243985
2336             -- AND         DECODE(S.LAST_UNIT_COMPLETION_DATE,
2337             --                   NULL, C.NEXT_SEQ_NUM, C.SEQ_NUM) IS NOT NULL
2338             -- AND         C.NEXT_DATE < NVL(p_itf, C.NEXT_DATE + 1)
2339             AND         TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)) < NVL(p_itf, TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)) + 1) -- 2859130
2340                                                  ---bug 1735580
2341         )
2342         GROUP BY SD_DATE
2343         ORDER BY SD_DATE; --4698199
2344 END get_mat_avail_opt;
2345 
2346 PROCEDURE get_mat_avail_unopt (
2347    p_item_id            IN NUMBER,
2348    p_org_id             IN NUMBER,
2349    p_instance_id        IN NUMBER,
2350    p_plan_id            IN NUMBER,
2351    p_cal_code           IN VARCHAR2,
2352    p_cal_exc_set_id     IN NUMBER,
2353    p_itf                IN DATE,
2354    x_atp_dates          OUT NoCopy MRP_ATP_PUB.date_arr,
2355    x_atp_qtys           OUT NoCopy MRP_ATP_PUB.number_arr
2356 ) IS
2357 BEGIN
2358         IF PG_DEBUG in ('Y', 'C') THEN
2359            msc_sch_wb.atp_debug('Begin get_mat_avail_unopt');
2360         END IF;
2361 
2362         SELECT 	SD_DATE, SUM(SD_QTY)
2363         BULK COLLECT INTO x_atp_dates, x_atp_qtys
2364         FROM (
2365             SELECT     -- C.PRIOR_DATE SD_DATE, -- 2859130
2366                        C.CALENDAR_DATE SD_DATE,
2367                        -1* DECODE(D.ORIGINATION_TYPE,
2368                                            4, D.DAILY_DEMAND_RATE,
2369                                            --D.USING_REQUIREMENT_QUANTITY) SD_QTY
2370                                            (D.USING_REQUIREMENT_QUANTITY - NVL(d.reserved_quantity, 0))) SD_QTY --5027568
2371             FROM        MSC_CALENDAR_DATES C,
2372                         MSC_DEMANDS D,
2373                         MSC_SYSTEM_ITEMS I
2374             WHERE       I.SR_INVENTORY_ITEM_ID = p_item_id
2375             AND         I.ORGANIZATION_ID = p_org_id
2376             AND         I.SR_INSTANCE_ID = p_instance_id
2377             AND         I.PLAN_ID = p_plan_id
2378             AND		D.PLAN_ID = I.PLAN_ID
2379             AND		D.SR_INSTANCE_ID = I.SR_INSTANCE_ID
2380             AND		D.INVENTORY_ITEM_ID = I.INVENTORY_ITEM_ID
2381             AND 	D.ORGANIZATION_ID = I.ORGANIZATION_ID
2382             -- 1243985
2383             AND         D.ORIGINATION_TYPE NOT IN (5,7,8,9,11,15,22,28,29,31,52,70) -- ignore copy SO for summary enhancement
2384             -- Bug1990155, 1995835 exclude the expired lots demand datreya 9/18/2001
2385             -- Bug 1530311, forecast to be excluded
2386             AND		C.CALENDAR_CODE = p_cal_code
2387             AND		C.EXCEPTION_SET_ID = p_cal_exc_set_id
2388             AND         C.SR_INSTANCE_ID = I.SR_INSTANCE_ID
2389             AND         D.USING_REQUIREMENT_QUANTITY <> 0 --4501434
2390             -- since we store repetitive schedule demand in different ways for
2391             -- ods (total quantity on start date) and pds  (daily quantity from
2392             -- start date to end date), we need to make sure we only select work day
2393             -- for pds's repetitive schedule demand.
2394             -- Bug 3550296 and 3574164. IMPLEMENT_DATE AND DMD_SATISFIED_DATE are changed to
2395             -- IMPLEMENT_SHIP_DATE and PLANNED_SHIP_DATE resp.
2396             AND         C.CALENDAR_DATE
2397             		BETWEEN
2398             		TRUNC(DECODE(D.RECORD_SOURCE,
2399                                     2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2400                                        DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2401                                               2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2402                                                  NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))))
2403             		AND
2404                         TRUNC(NVL(D.ASSEMBLY_DEMAND_COMP_DATE,
2405                               DECODE(D.RECORD_SOURCE,
2406                                     2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2407                                        DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2408                                               2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2409                                                  NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))))
2410                                                  --plan by request date, promise date, schedule date
2411                         -- new clause 2640489 SIMPLIFY
2412             AND         (C.SEQ_NUM IS NOT NULL OR D.ORIGINATION_TYPE  <> 4)
2413             -- AND         ((D.ORIGINATION_TYPE = 4 AND C.SEQ_NUM IS NOT NULL) OR
2414             --               (D.ORIGINATION_TYPE  <> 4))
2415             -- AND         C.PRIOR_DATE < NVL(p_itf, C.PRIOR_DATE + 1)
2416             AND         C.CALENDAR_DATE < NVL(p_itf, C.CALENDAR_DATE + 1)
2417             UNION ALL
2418             SELECT      -- C.NEXT_DATE SD_DATE, -- 2859130
2419                         C.CALENDAR_DATE SD_DATE,
2420                         NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY)  SD_QTY
2421             FROM        MSC_CALENDAR_DATES C,
2422                         MSC_SUPPLIES S,
2423                         MSC_SYSTEM_ITEMS I
2424             WHERE       I.SR_INVENTORY_ITEM_ID = p_item_id
2425             AND         I.ORGANIZATION_ID = p_org_id
2426             AND         I.SR_INSTANCE_ID = p_instance_id
2427             AND         I.PLAN_ID = p_plan_id
2428             AND		S.PLAN_ID = I.PLAN_ID
2429             AND		S.SR_INSTANCE_ID = I.SR_INSTANCE_ID
2430             AND		S.INVENTORY_ITEM_ID = I.INVENTORY_ITEM_ID
2431             AND 	S.ORGANIZATION_ID = I.ORGANIZATION_ID
2432                         -- Exclude Cancelled Supplies 2460645
2433             AND         NVL(S.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
2434             AND         NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY) <> 0 -- 1243985
2435             AND		C.CALENDAR_CODE = p_cal_code
2436             AND		C.EXCEPTION_SET_ID = p_cal_exc_set_id
2437             AND         C.SR_INSTANCE_ID = I.SR_INSTANCE_ID
2438             AND		C.CALENDAR_DATE BETWEEN TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE))
2439                                 AND TRUNC(NVL(S.LAST_UNIT_COMPLETION_DATE, NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)))
2440             -- 2859130 change next_seq_num to 1
2441             AND         DECODE(S.LAST_UNIT_COMPLETION_DATE,
2442                                NULL, 1, C.SEQ_NUM) IS NOT NULL
2443             -- AND         C.NEXT_DATE < NVL(p_itf, C.NEXT_DATE + 1)
2444             AND         C.CALENDAR_DATE < NVL(p_itf, C.CALENDAR_DATE + 1) -- 2859130
2445                                                  ---bug 1735580
2446         )
2447         GROUP BY SD_DATE
2448         ORDER BY SD_DATE; --5353882
2449 END get_mat_avail_unopt;
2450 
2451 PROCEDURE get_mat_avail_ods_dtls (
2452    p_item_id            IN NUMBER,
2453    p_request_item_id    IN NUMBER,
2454    p_org_id             IN NUMBER,
2455    p_instance_id        IN NUMBER,
2456    p_plan_id            IN NUMBER,
2457    p_cal_code           IN VARCHAR2,
2458    p_cal_exc_set_id     IN NUMBER,
2459    p_sysdate_seq_num    IN NUMBER,
2460    p_sys_next_date      IN DATE,
2461    p_demand_class       IN VARCHAR2,
2462    p_default_atp_rule_id IN NUMBER,
2463    p_default_dmd_class  IN VARCHAR2,
2464    p_itf                IN DATE,
2465    p_level              IN NUMBER,
2466    p_scenario_id        IN NUMBER,
2467    p_identifier         IN NUMBER
2468 ) IS
2469    l_null_num   NUMBER;
2470    l_null_char  VARCHAR2(1);
2471    l_null_date  DATE; --bug3814584
2472    l_sysdate    DATE := sysdate;
2473 BEGIN
2474         IF PG_DEBUG in ('Y', 'C') THEN
2475            msc_sch_wb.atp_debug('Begin get_mat_avail_ods_dtls');
2476         END IF;
2477 
2478 -- dsting: S/D details changes
2479 INSERT INTO msc_atp_sd_details_temp (
2480 	ATP_Level,
2481 	Order_line_id,
2482 	Scenario_Id,
2483 	Inventory_Item_Id,
2484 	Request_Item_Id,
2485 	Organization_Id,
2486 	Department_Id,
2487 	Resource_Id,
2488 	Supplier_Id,
2489 	Supplier_Site_Id,
2490 	From_Organization_Id,
2491 	From_Location_Id,
2492 	To_Organization_Id,
2493 	To_Location_Id,
2494 	Ship_Method,
2495 	UOM_code,
2496 	Supply_Demand_Type,
2497 	Supply_Demand_Source_Type,
2498 	Supply_Demand_Source_Type_Name,
2499 	Identifier1,
2500 	Identifier2,
2501 	Identifier3,
2502 	Identifier4,
2503 	Supply_Demand_Quantity,
2504 	Supply_Demand_Date,
2505 	Disposition_Type,
2506 	Disposition_Name,
2507 	Pegging_Id,
2508 	End_Pegging_Id,
2509 	creation_date,
2510 	created_by,
2511 	last_update_date,
2512 	last_updated_by,
2513 	last_update_login,
2514         ORIG_CUSTOMER_SITE_NAME,--bug3263368
2515         ORIG_CUSTOMER_NAME, --bug3263368
2516         ORIG_DEMAND_CLASS, --bug3263368
2517         ORIG_REQUEST_DATE --bug3263368
2518                 )
2519 
2520 (        -- SQL Query changes Begin 2640489
2521     SELECT	p_level col1,
2522 		p_identifier col2,
2523                 p_scenario_id col3,
2524                 p_item_id col4 ,
2525                 p_request_item_id col5,
2526 		p_org_id col6,
2527                 l_null_num col7,
2528                 l_null_num col8,
2529                 l_null_num col9,
2530                 l_null_num col10,
2531                 l_null_num col11,
2532                 l_null_num col12,
2533                 l_null_num col13,
2534                 l_null_num col14,
2535 		l_null_char col15,
2536 		I.UOM_CODE col16,
2537 		1 col17, -- demand
2538 		D.ORIGINATION_TYPE col18,
2539                 l_null_char col19,
2540 		D.SR_INSTANCE_ID col20,
2541                 l_null_num col21,
2542 		D.DEMAND_ID col22,
2543 		l_null_num col23,
2544                 -1* D.USING_REQUIREMENT_QUANTITY col24,
2545 		C.PRIOR_DATE col25,
2546                 l_null_num col26,
2547                 DECODE(D.ORIGINATION_TYPE, 1, to_char(D.DISPOSITION_ID), D.ORDER_NUMBER) col27,
2548                        -- rajjain 04/25/2003 Bug 2771075
2549                        -- For Planned Order Demands We will populate disposition_id
2550                        -- in disposition_name column
2551                 l_null_num col28,
2552                 l_null_num col29,
2553 		l_sysdate,
2554 		FND_GLOBAL.User_ID,
2555 		l_sysdate,
2556 		FND_GLOBAL.User_ID,
2557 		FND_GLOBAL.User_ID,
2558 		MTPS.LOCATION, --bug3263368
2559                 MTP.PARTNER_NAME, --bug3263368
2560                 D.DEMAND_CLASS, --bug3263368
2561                 trunc(DECODE(D.ORDER_DATE_TYPE_CODE,2,D.REQUEST_DATE,  --4135752
2562                                            D.REQUEST_SHIP_DATE)) --bug3263368
2563     FROM        MSC_CALENDAR_DATES C,
2564 		MSC_DEMANDS D,
2565                 MSC_ATP_RULES R,
2566                 MSC_SYSTEM_ITEMS I,
2567                 MSC_TRADING_PARTNERS    MTP,--bug3263368
2568                 MSC_TRADING_PARTNER_SITES    MTPS --bug3263368
2569     WHERE       I.SR_INVENTORY_ITEM_ID = p_item_id
2570     AND         I.ORGANIZATION_ID = p_org_id
2571     AND		I.SR_INSTANCE_ID = p_instance_id
2572     AND		I.PLAN_ID = p_plan_id
2573     AND         R.RULE_ID (+) = NVL(I.ATP_RULE_ID, p_default_atp_rule_id)
2574     AND         R.SR_INSTANCE_ID (+) = I.SR_INSTANCE_ID
2575     AND		D.PLAN_ID = I.PLAN_ID
2576     AND		D.SR_INSTANCE_ID = I.SR_INSTANCE_ID
2577     AND		D.INVENTORY_ITEM_ID = I.INVENTORY_ITEM_ID
2578     AND 	D.ORGANIZATION_ID = I.ORGANIZATION_ID
2579     AND         USING_REQUIREMENT_QUANTITY <> 0
2580     AND	        D.ORIGINATION_TYPE in (
2581                 DECODE(R.INCLUDE_DISCRETE_WIP_DEMAND, 1, 3, -1),
2582                 DECODE(R.INCLUDE_FLOW_SCHEDULE_DEMAND, 1, 25, -1),
2583                 DECODE(R.INCLUDE_USER_DEFINED_DEMAND, 1, 42, -1),
2584                 DECODE(R.INCLUDE_NONSTD_WIP_RECEIPTS, 1, 2, -1),
2585                 DECODE(R.INCLUDE_REP_WIP_DEMAND, 1, 4, -1))
2586     AND         D.SHIP_TO_SITE_ID = MTPS.PARTNER_SITE_ID(+) --bug3263368
2587     AND         D.CUSTOMER_ID = MTP.PARTNER_ID(+) --bug3263368
2588     AND		C.CALENDAR_CODE=p_cal_code
2589     AND		C.EXCEPTION_SET_ID=p_cal_exc_set_id
2590     AND         C.SR_INSTANCE_ID = p_instance_id
2591     -- since we store repetitive schedule demand in different ways for
2592     -- ods (total quantity on start date) and pds  (daily quantity from
2593     -- start date to end date), we need to make sure we only select work day
2594     -- for pds's repetitive schedule demand.
2595     AND         C.CALENDAR_DATE BETWEEN TRUNC(D.USING_ASSEMBLY_DEMAND_DATE) AND
2596                 TRUNC(NVL(D.ASSEMBLY_DEMAND_COMP_DATE,
2597                           D.USING_ASSEMBLY_DEMAND_DATE))
2598     AND         (R.PAST_DUE_DEMAND_CUTOFF_FENCE is NULL OR
2599                  C.PRIOR_SEQ_NUM >= p_sysdate_seq_num - R.PAST_DUE_DEMAND_CUTOFF_FENCE)
2600     AND         C.PRIOR_DATE < NVL(p_itf, C.PRIOR_DATE + 1)
2601     AND         (R.DEMAND_CLASS_ATP_FLAG <> 1 OR
2602                  NVL(D.DEMAND_CLASS, NVL(p_default_dmd_class,'@@@')) =
2603                    NVL(P_DEMAND_CLASS, NVL(p_default_dmd_class,'@@@')) )
2604     UNION ALL
2605     SELECT      p_level col1,
2606                 p_identifier col2,
2607                 p_scenario_id col3,
2608                 p_item_id col4,
2609                 p_request_item_id col5,
2610                 p_org_id col6,
2611                 l_null_num col7,
2612                 l_null_num col8,
2613                 l_null_num col9,
2614                 l_null_num col10,
2615                 l_null_num col11,
2616                 l_null_num col12,
2617                 l_null_num col13,
2618                 l_null_num col14,
2619                 l_null_char col15,
2620                 I.UOM_CODE col16,
2621                 1 col17, -- demand
2622                 DECODE(D.RESERVATION_TYPE, 1, 30, 10)  col18,
2623                 l_null_char col19,
2624                 D.SR_INSTANCE_ID col20,
2625                 l_null_num col21,
2626                 to_number(D.DEMAND_SOURCE_LINE) col22,
2627                 l_null_num col23,
2628                 -1*(D.PRIMARY_UOM_QUANTITY-
2629                 GREATEST(NVL(D.RESERVATION_QUANTITY,0), D.COMPLETED_QUANTITY))
2630                 col24,
2631 	DECODE(D.RESERVATION_TYPE,2,p_sys_next_date, TRUNC(D.REQUIREMENT_DATE)) col25 ,  -- to_date removed to avoid GSCC error
2632                 l_null_num col26,
2633                 D.SALES_ORDER_NUMBER col27,
2634                 l_null_num col28,
2635                 l_null_num col29,
2636 		l_sysdate,
2637 		FND_GLOBAL.User_ID,
2638 		l_sysdate,
2639 		FND_GLOBAL.User_ID,
2640 		FND_GLOBAL.User_ID,
2641 		MTPS.LOCATION, --bug3263368
2642                 MTP.PARTNER_NAME, --bug3263368
2643                 D.DEMAND_CLASS, --bug3263368
2644                 trunc(DECODE(D.ORDER_DATE_TYPE_CODE,2,D.REQUEST_DATE,  --4135752
2645                                            D.REQUEST_SHIP_DATE)) --bug3263368
2646     FROM
2647 		MSC_SALES_ORDERS D,
2648                 MSC_ATP_RULES R,
2649                 MSC_SYSTEM_ITEMS I,
2650                 MSC_CALENDAR_DATES C,
2651                 MSC_TRADING_PARTNERS    MTP,--bug3263368
2652                 MSC_TRADING_PARTNER_SITES    MTPS --bug3263368
2653     WHERE       I.SR_INVENTORY_ITEM_ID = p_item_id
2654     AND         I.ORGANIZATION_ID = p_org_id
2655     AND         I.SR_INSTANCE_ID = p_instance_id
2656     AND         I.PLAN_ID = p_plan_id
2657     AND         R.RULE_ID (+) = NVL(I.ATP_RULE_ID, p_default_atp_rule_id)
2658     AND         R.SR_INSTANCE_ID (+) = I.SR_INSTANCE_ID
2659     AND		D.SR_INSTANCE_ID = I.SR_INSTANCE_ID
2660     AND		D.INVENTORY_ITEM_ID = I.INVENTORY_ITEM_ID
2661     AND 	D.ORGANIZATION_ID = I.ORGANIZATION_ID
2662     AND         D.DEMAND_SOURCE_TYPE <> DECODE(R.INCLUDE_SALES_ORDERS,2,2,-1)
2663     AND         D.DEMAND_SOURCE_TYPE <> DECODE(R.INCLUDE_INTERNAL_ORDERS,2,8,-1)
2664     AND         D.PRIMARY_UOM_QUANTITY > GREATEST(NVL(D.RESERVATION_QUANTITY,0),
2665                 D.COMPLETED_QUANTITY)
2666     AND         DECODE(MSC_ATP_PVT.G_APPS_VER,3,D.COMPLETED_QUANTITY,0) = 0 -- 2300767
2667     AND         (D.SUBINVENTORY IS NULL OR D.SUBINVENTORY IN
2668                    (SELECT S.SUB_INVENTORY_CODE
2669                     FROM   MSC_SUB_INVENTORIES S
2670                     WHERE  S.ORGANIZATION_ID=D.ORGANIZATION_ID
2671                     AND    S.PLAN_ID = I.PLAN_ID
2672                     AND    S.SR_INSTANCE_ID = D.SR_INSTANCE_ID
2673                     AND    S.INVENTORY_ATP_CODE =DECODE(R.DEFAULT_ATP_SOURCES,
2674                                    1, 1, NULL, 1, S.INVENTORY_ATP_CODE)
2675                     AND    S.NETTING_TYPE =DECODE(R.DEFAULT_ATP_SOURCES,
2676                                    2, 1, S.NETTING_TYPE)))
2677     AND         (D.RESERVATION_TYPE = 2
2678                  OR D.PARENT_DEMAND_ID IS NULL
2679                  OR (D.RESERVATION_TYPE = 3 AND
2680                      ((R.INCLUDE_DISCRETE_WIP_RECEIPTS = 1) or
2681                       (R.INCLUDE_NONSTD_WIP_RECEIPTS = 1))))
2682                 -- new clause, remove existing Exists Query 2640489
2683     AND         D.SHIP_TO_SITE_USE_ID = MTPS.PARTNER_SITE_ID(+) --bug3263368
2684     AND         D.CUSTOMER_ID = MTP.PARTNER_ID(+) --bug3263368
2685     AND      (R.PAST_DUE_DEMAND_CUTOFF_FENCE is NULL OR
2686                  C.PRIOR_SEQ_NUM >= p_sysdate_seq_num - R.PAST_DUE_DEMAND_CUTOFF_FENCE)
2687     AND      C.CALENDAR_CODE = p_cal_code
2688     AND      C.SR_INSTANCE_ID = I.SR_INSTANCE_ID
2689     AND      C.EXCEPTION_SET_ID = -1
2690     AND      C.CALENDAR_DATE = TRUNC(D.REQUIREMENT_DATE)
2691     AND      C.PRIOR_DATE < NVL(p_itf, C.PRIOR_DATE + 1)
2692                 -- new clause 2640489, DECODE is also OR, Explicit OR gives CBO choices
2693     AND         (R.DEMAND_CLASS_ATP_FLAG <> 1 OR
2694                  NVL(D.DEMAND_CLASS, NVL(p_default_dmd_class,'@@@')) =
2695                    NVL(P_DEMAND_CLASS, NVL(p_default_dmd_class,'@@@')) )
2696     UNION ALL
2697     SELECT      p_level col1,
2698                 p_identifier col2,
2699                 p_scenario_id col3,
2700                 p_item_id col4 ,
2701                 p_request_item_id col5,
2702                 p_org_id col6,
2703                 l_null_num col7,
2704                 l_null_num col8,
2705                 l_null_num col9,
2706                 l_null_num col10,
2707                 l_null_num col11,
2708                 l_null_num col12,
2709                 l_null_num col13,
2710                 l_null_num col14,
2711                 l_null_char col15,
2712                 I.UOM_CODE col16,
2713                 2 col17, -- supply
2714                 S.ORDER_TYPE col18,
2715                 l_null_char col19,
2716                 S.SR_INSTANCE_ID col20,
2717                 l_null_num col21,
2718                 S.TRANSACTION_ID col22,
2719                 l_null_num col23,
2720                 Decode(order_type,
2721                 30, Decode(Sign(S.Daily_rate * (TRUNC(C.Calendar_date) -  TRUNC(S.FIRST_UNIT_START_DATE) )- S.qty_completed),
2722                              -1,S.Daily_rate* (TRUNC(C.Calendar_date) - TRUNC(S.First_Unit_Start_date) +1)- S.qty_completed,
2723                               S.Daily_rate),
2724                 5, NVL(S.DAILY_RATE, NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY)),
2725 
2726                     (NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY) - NVL(S.NON_NETTABLE_QTY, 0)) ) col24,
2727                 C.NEXT_DATE col25,
2728                 l_null_num col26,
2729                 DECODE(S.ORDER_TYPE,
2730                        1, S.ORDER_NUMBER,
2731 		       2, S.ORDER_NUMBER,
2732 		       3, S.ORDER_NUMBER,
2733                        7, S.ORDER_NUMBER,
2734                        8, S.ORDER_NUMBER,
2735                        5, MSC_ATP_FUNC.Get_Designator(S.SCHEDULE_DESIGNATOR_ID),
2736                       11, S.ORDER_NUMBER,
2737                       12, S.ORDER_NUMBER,
2738                       14, S.ORDER_NUMBER,
2739                       15, S.ORDER_NUMBER,
2740                       27, S.ORDER_NUMBER,
2741                       28, S.ORDER_NUMBER,
2742                       41, S.ORDER_NUMBER, -- bug 3745082 'User Defined Supply'
2743                       -- NULL) col27,
2744                       l_null_char) col27, --bug3814584
2745                 l_null_num col28,
2746 		l_null_num col29,
2747 		l_sysdate,
2748 		FND_GLOBAL.User_ID,
2749 		l_sysdate,
2750 		FND_GLOBAL.User_ID,
2751 		FND_GLOBAL.User_ID,
2752 		--null, --bug3263368 ORIG_CUSTOMER_SITE_NAME
2753                 --null, --bug3263368 ORIG_CUSTOMER_NAME
2754                 --null, --bug3263368 ORIG_DEMAND_CLASS
2755                 --null  --bug3263368 ORIG_REQUEST_DATE
2756                 l_null_char, --bug3814584
2757                 l_null_char, --bug3814584
2758                 l_null_char, --bug3814584
2759                 l_null_date  --bug3814584
2760     FROM        MSC_CALENDAR_DATES C,
2761 		MSC_SUPPLIES S,
2762                 MSC_ATP_RULES R,
2763                 MSC_SYSTEM_ITEMS I,
2764                 MSC_SUB_INVENTORIES MSI
2765     WHERE       I.SR_INVENTORY_ITEM_ID = p_item_id
2766     AND         I.ORGANIZATION_ID = p_org_id
2767     AND         I.SR_INSTANCE_ID = p_instance_id
2768     AND         I.PLAN_ID = p_plan_id
2769     AND         R.RULE_ID (+) = NVL(I.ATP_RULE_ID, p_default_atp_rule_id)
2770     AND         R.SR_INSTANCE_ID (+) = I.SR_INSTANCE_ID
2771     AND		S.PLAN_ID = I.PLAN_ID
2772     AND		S.SR_INSTANCE_ID = I.SR_INSTANCE_ID
2773     AND		S.INVENTORY_ITEM_ID = I.INVENTORY_ITEM_ID
2774     AND 	S.ORGANIZATION_ID = I.ORGANIZATION_ID
2775     AND         Decode(S.order_type, 30, S.Daily_rate* (TRUNC(C.Calendar_date)
2776 					- TRUNC(S.First_Unit_Start_date) + 1),
2777                                      5, NVL(S.Daily_rate, ABS(NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY)) ),
2778                         ABS(NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY)) ) >
2779                       Decode(S.order_type, 30, S.qty_completed,0)
2780     AND		(S.ORDER_TYPE IN (
2781 		DECODE(R.INCLUDE_PURCHASE_ORDERS, 1, 1, -1),
2782 		DECODE(R.INCLUDE_PURCHASE_ORDERS, 1, 8, -1), -- 1882898
2783 		DECODE(R.INCLUDE_DISCRETE_WIP_RECEIPTS, 1, 3, -1),
2784 		DECODE(R.INCLUDE_REP_WIP_RECEIPTS, 1, 30, -1),
2785 		DECODE(R.INCLUDE_NONSTD_WIP_RECEIPTS, 1, 7, -1),
2786 		DECODE(R.INCLUDE_NONSTD_WIP_RECEIPTS, 1, 15, -1),
2787 		DECODE(R.INCLUDE_INTERORG_TRANSFERS, 1, 11, -1),
2788                 DECODE(R.INCLUDE_INTERORG_TRANSFERS, 1, 12, -1),
2789 		DECODE(R.INCLUDE_ONHAND_AVAILABLE, 1, 18, -1),
2790                 DECODE(R.INCLUDE_USER_DEFINED_SUPPLY, 1, 41, -1),
2791 		DECODE(R.INCLUDE_FLOW_SCHEDULE_RECEIPTS, 1, 27, -1),
2792 		DECODE(R.INCLUDE_FLOW_SCHEDULE_RECEIPTS, 1, 28, -1))
2793                 OR
2794                 (INCLUDE_INTERNAL_REQS = 1 AND S.ORDER_TYPE = 2 AND
2795                  S.SOURCE_ORGANIZATION_ID IS NOT NULL)
2796                 OR
2797                 (INCLUDE_SUPPLIER_REQS = 1 AND S.ORDER_TYPE = 2 AND
2798                  S.SOURCE_ORGANIZATION_ID IS NULL)
2799                 OR
2800                 ((R.INCLUDE_REP_MPS = 1 OR R.INCLUDE_DISCRETE_MPS = 1) AND
2801                 S.ORDER_TYPE = 5
2802                  -- bug 2461071
2803                 AND exists (SELECT '1'
2804                             FROM    MSC_DESIGNATORS
2805                             WHERE   INVENTORY_ATP_FLAG = 1
2806                             AND     DESIGNATOR_TYPE = 2
2807                             AND     DESIGNATOR_ID = S.SCHEDULE_DESIGNATOR_ID
2808                             AND     DECODE(R.demand_class_atp_flag,1,
2809                                     nvl(demand_class,
2810                                     nvl(p_default_dmd_class,'@@@')),'@@@') =
2811                                     DECODE(R.demand_class_atp_flag,1,
2812                                     nvl(p_demand_class,
2813                                     nvl(p_default_dmd_class,'@@@')),'@@@')
2814 )))
2815                 --AND MSC_ATP_FUNC.MPS_ATP(S.SCHEDULE_DESIGNATOR_ID) = 1
2816     AND		C.CALENDAR_CODE = p_cal_code
2817     AND		C.EXCEPTION_SET_ID = p_cal_exc_set_id
2818     AND         C.SR_INSTANCE_ID = p_instance_id
2819                  -- Bug 2132288, 2442009
2820     AND         C.CALENDAR_DATE BETWEEN TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE))
2821                     AND TRUNC(NVL(DECODE(S.ORDER_TYPE, 5, S.LAST_UNIT_START_DATE,
2822                                    S.LAST_UNIT_COMPLETION_DATE), NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)))
2823     AND         DECODE(DECODE(S.ORDER_TYPE, 5, S.LAST_UNIT_START_DATE,
2824                                    S.LAST_UNIT_COMPLETION_DATE),
2825                        NULL, C.NEXT_SEQ_NUM, C.SEQ_NUM) IS NOT NULL
2826                  -- End Bug 2132288, 2442009
2827                  -- new clause 2640489, SIMPLIFY FOR CBO
2828     AND         (S.ORDER_TYPE = 18
2829                  OR R.PAST_DUE_SUPPLY_CUTOFF_FENCE is NULL
2830                  OR C.NEXT_SEQ_NUM >= p_sysdate_seq_num - R.PAST_DUE_SUPPLY_CUTOFF_FENCE)
2831     AND         C.NEXT_DATE >= DECODE(S.ORDER_TYPE, 27, TRUNC(SYSDATE),
2832                                                 28, TRUNC(SYSDATE),
2833                                                     C.NEXT_DATE)
2834     AND         C.NEXT_DATE < NVL(p_itf, C.NEXT_DATE + 1)
2835     AND         (R.DEMAND_CLASS_ATP_FLAG <> 1
2836                  OR S.ORDER_TYPE = 5
2837                  OR NVL(S.DEMAND_CLASS, NVL(p_default_dmd_class,'@@@')) =
2838                     NVL(P_DEMAND_CLASS, NVL(p_default_dmd_class,'@@@')) )
2839                 --- filter out non-atpable sub-inventories
2840     AND          MSI.plan_id (+) = p_plan_id
2841     AND          MSI.organization_id (+) = p_org_id
2842     AND          MSI.sr_instance_id (+) = p_instance_id
2843     -- AND          S.subinventory_code = MSI.sub_inventory_code
2844     AND          MSI.sub_inventory_code (+) = S.subinventory_code
2845     AND          NVL(MSI.inventory_atp_code,1)  <> 2  -- filter out non-atpable subinventories
2846     -- SQL Query changes End 2640489
2847 )
2848 ;
2849 -- dsting 'removed order by col25'
2850 END get_mat_avail_ods_dtls;
2851 
2852 
2853 PROCEDURE get_mat_avail_opt_dtls (
2854    p_item_id            IN NUMBER,
2855    p_request_item_id    IN NUMBER,
2856    p_org_id             IN NUMBER,
2857    p_instance_id        IN NUMBER,
2858    p_plan_id            IN NUMBER,
2859    p_itf                IN DATE,
2860    p_level              IN NUMBER,
2861    p_scenario_id        IN NUMBER,
2862    p_identifier         IN NUMBER
2863 ) IS
2864    l_null_num   NUMBER;
2865    l_null_char  VARCHAR2(1);
2866    l_null_date  DATE; --bug3814584
2867    l_sysdate    DATE := sysdate;
2868 BEGIN
2869         IF PG_DEBUG in ('Y', 'C') THEN
2870            msc_sch_wb.atp_debug('Begin get_mat_avail_opt_dtls');
2871         END IF;
2872 
2873 INSERT INTO msc_atp_sd_details_temp (
2874 	ATP_Level,
2875 	Order_line_id,
2876 	Scenario_Id,
2877 	Inventory_Item_Id,
2878 	Request_Item_Id,
2879 	Organization_Id,
2880 	Department_Id,
2881 	Resource_Id,
2882 	Supplier_Id,
2883 	Supplier_Site_Id,
2884 	From_Organization_Id,
2885 	From_Location_Id,
2886 	To_Organization_Id,
2887 	To_Location_Id,
2888 	Ship_Method,
2889 	UOM_code,
2890 	Supply_Demand_Type,
2891 	Supply_Demand_Source_Type,
2892 	Supply_Demand_Source_Type_Name,
2893 	Identifier1,
2894 	Identifier2,
2895 	Identifier3,
2896 	Identifier4,
2897 	Supply_Demand_Quantity,
2898 	Supply_Demand_Date,
2899 	Disposition_Type,
2900 	Disposition_Name,
2901 	Pegging_Id,
2902 	End_Pegging_Id,
2903 	creation_date,
2904 	created_by,
2905 	last_update_date,
2906 	last_updated_by,
2907 	last_update_login,
2908 	ORIG_CUSTOMER_SITE_NAME,--bug3263368
2909         ORIG_CUSTOMER_NAME, --bug3263368
2910         ORIG_DEMAND_CLASS, --bug3263368
2911         ORIG_REQUEST_DATE --bug3263368
2912      )
2913 (
2914     SELECT      p_level col1,
2915 		p_identifier col2,
2916                 p_scenario_id col3,
2917                 p_item_id col4 ,
2918                 p_request_item_id col5,
2919 		p_org_id col6,
2920                 l_null_num col7,
2921                 l_null_num col8,
2922                 l_null_num col9,
2923                 l_null_num col10,
2924                 l_null_num col11,
2925                 l_null_num col12,
2926                 l_null_num col13,
2927                 l_null_num col14,
2928 		l_null_char col15,
2929 		I.UOM_CODE col16,
2930 		1 col17, -- demand
2931 		--D.ORIGINATION_TYPE col18,
2932 		DECODE( D.ORIGINATION_TYPE, -100, 30, D.ORIGINATION_TYPE) col18, --5027568
2933                 l_null_char col19,
2934 		D.SR_INSTANCE_ID col20,
2935                 l_null_num col21,
2936 		D.DEMAND_ID col22,
2937 		l_null_num col23,
2938                 -- -1* D.USING_REQUIREMENT_QUANTITY col24,
2939                 -1*(D.USING_REQUIREMENT_QUANTITY - NVL(d.reserved_quantity, 0)) col24, --5027568
2940 		-- C.PRIOR_DATE col25, -- 2859130
2941 		-- Bug 3550296 and 3574164. IMPLEMENT_DATE AND DMD_SATISFIED_DATE are changed to
2942                 -- IMPLEMENT_SHIP_DATE and PLANNED_SHIP_DATE resp.
2943                 TRUNC(DECODE(D.RECORD_SOURCE,
2944                              2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2945                                 DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2946                                        2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2947                                           NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))) col25,
2948                                           --plan by request date,promise date, schedule date
2949                 l_null_num col26,
2950                 D.ORDER_NUMBER col27,
2951                 l_null_num col28,
2952                 l_null_num col29,
2953 		l_sysdate,
2954 		FND_GLOBAL.User_ID,
2955 		l_sysdate,
2956 		FND_GLOBAL.User_ID,
2957 		FND_GLOBAL.User_ID,
2958 		MTPS.LOCATION, --bug3263368
2959                 MTP.PARTNER_NAME, --bug3263368
2960                 D.DEMAND_CLASS, --bug3263368
2961                 DECODE(D.ORDER_DATE_TYPE_CODE,2,D.REQUEST_DATE,
2962                                            D.REQUEST_SHIP_DATE) --bug3263368
2963     FROM        MSC_SYSTEM_ITEMS I,
2964 		MSC_DEMANDS D,
2965 		MSC_TRADING_PARTNERS    MTP,--bug3263368
2966                 MSC_TRADING_PARTNER_SITES    MTPS --bug3263368
2967     WHERE       I.SR_INVENTORY_ITEM_ID = p_item_id
2968     AND         I.ORGANIZATION_ID = p_org_id
2969     AND		I.SR_INSTANCE_ID = p_instance_id
2970     AND		I.PLAN_ID = p_plan_id
2971     AND		D.PLAN_ID = I.PLAN_ID
2972     AND		D.SR_INSTANCE_ID = I.SR_INSTANCE_ID
2973     AND		D.INVENTORY_ITEM_ID = I.INVENTORY_ITEM_ID
2974     AND         D.SHIP_TO_SITE_ID = MTPS.PARTNER_SITE_ID(+) --bug3263368
2975     AND         D.CUSTOMER_ID = MTP.PARTNER_ID(+) --bug3263368
2976     AND         D.USING_REQUIREMENT_QUANTITY <> 0 --4501434
2977     AND 	D.ORGANIZATION_ID = I.ORGANIZATION_ID
2978     -- 1243985
2979     -- 2859130 repetitive schedule (4) not supported for constrained plan
2980     AND	        D.ORIGINATION_TYPE NOT IN (4,5,7,8,9,11,15,22,28,29,31,52,70) -- ignore copy SO for summary enhancement
2981     -- Bug1990155, 1995835 exclude the expired lots demand datreya 9/18/2001
2982     -- Bug 1530311, need to exclude forecast
2983     -- Bug 3550296 and 3574164. IMPLEMENT_DATE AND DMD_SATISFIED_DATE are changed to
2984     -- IMPLEMENT_SHIP_DATE and PLANNED_SHIP_DATE resp.
2985     --bug3693892 added trunc
2986     AND         TRUNC(DECODE(D.RECORD_SOURCE,
2987                             2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2988                                DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2989                                       2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2990                                          NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))))
2991     		< TRUNC(NVL(p_itf,
2992     			DECODE(D.RECORD_SOURCE,
2993                             2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2994                                DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2995                                       2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2996                                          NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))) + 1))
2997                                          --plan by request date, promise date, schedule date
2998     UNION ALL
2999     SELECT      p_level col1,
3000                 p_identifier col2,
3001                 p_scenario_id col3,
3002                 p_item_id col4 ,
3003                 p_request_item_id col5,
3004                 p_org_id col6,
3005                 l_null_num col7,
3006                 l_null_num col8,
3007                 l_null_num col9,
3008                 l_null_num col10,
3009                 l_null_num col11,
3010                 l_null_num col12,
3011                 l_null_num col13,
3012                 l_null_num col14,
3013                 l_null_char col15,
3014                 I.UOM_CODE col16,
3015                 2 col17, -- supply
3016                 S.ORDER_TYPE col18,
3017                 l_null_char col19,
3018                 S.SR_INSTANCE_ID col20,
3019                 l_null_num col21,
3020                 S.TRANSACTION_ID col22,
3021                 l_null_num col23,
3022 		NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY) col24,
3023                 -- C.NEXT_DATE col25, -- 2859130
3024                 TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)) col25,
3025                 l_null_num col26,
3026                 -- Bug 2771075. For Planned Orders, we will populate transaction_id
3027 		-- in the disposition_name column to be consistent with Planning.
3028 		-- S.ORDER_NUMBER col27,
3029 		DECODE(S.ORDER_TYPE, 5, to_char(S.TRANSACTION_ID), S.ORDER_NUMBER) col27,
3030                 l_null_num col28,
3031 		l_null_num col29,
3032 		l_sysdate,
3033 		FND_GLOBAL.User_ID,
3034 		l_sysdate,
3035 		FND_GLOBAL.User_ID,
3036 		FND_GLOBAL.User_ID,
3037 		--null, --bug3263368 ORIG_CUSTOMER_SITE_NAME
3038                 --null, --bug3263368 ORIG_CUSTOMER_NAME
3039                 --null, --bug3263368 ORIG_DEMAND_CLASS
3040                 --null  --bug3263368 ORIG_REQUEST_DATE
3041                 l_null_char, --bug3814584
3042                 l_null_char, --bug3814584
3043                 l_null_char, --bug3814584
3044                 l_null_date  --bug3814584
3045     FROM        MSC_SYSTEM_ITEMS I,
3046 		MSC_SUPPLIES S
3047     WHERE       I.SR_INVENTORY_ITEM_ID = p_item_id
3048     AND         I.ORGANIZATION_ID = p_org_id
3049     AND         I.SR_INSTANCE_ID = p_instance_id
3050     AND         I.PLAN_ID = p_plan_id
3051     AND		S.PLAN_ID = I.PLAN_ID
3052     AND		S.SR_INSTANCE_ID = I.SR_INSTANCE_ID
3053     AND		S.INVENTORY_ITEM_ID = I.INVENTORY_ITEM_ID
3054     AND 	S.ORGANIZATION_ID = I.ORGANIZATION_ID
3055                 -- Exclude Cancelled Supplies 2460645
3056     AND         NVL(S.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
3057     AND         NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY) <> 0 -- 1243985
3058     AND         TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)) < NVL(p_itf, TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)) + 1)
3059 )
3060 ;
3061 END get_mat_avail_opt_dtls;
3062 
3063 PROCEDURE get_mat_avail_unopt_dtls (
3064    p_item_id            IN NUMBER,
3065    p_request_item_id    IN NUMBER,
3066    p_org_id             IN NUMBER,
3067    p_instance_id        IN NUMBER,
3068    p_plan_id            IN NUMBER,
3069    p_cal_code           IN VARCHAR2,
3070    p_cal_exc_set_id     IN NUMBER,
3071    p_itf                IN DATE,
3072    p_level              IN NUMBER,
3073    p_scenario_id        IN NUMBER,
3074    p_identifier         IN NUMBER
3075 ) IS
3076    l_null_num   NUMBER;
3077    l_null_char  VARCHAR2(1);
3078    l_null_date  DATE; --bug3814584
3079    l_sysdate    DATE := sysdate;
3080 BEGIN
3081         IF PG_DEBUG in ('Y', 'C') THEN
3082            msc_sch_wb.atp_debug('Begin get_mat_avail_unopt_dtls');
3083         END IF;
3084 
3085 INSERT INTO msc_atp_sd_details_temp (
3086 	ATP_Level,
3087 	Order_line_id,
3088 	Scenario_Id,
3089 	Inventory_Item_Id,
3090 	Request_Item_Id,
3091 	Organization_Id,
3092 	Department_Id,
3093 	Resource_Id,
3094 	Supplier_Id,
3095 	Supplier_Site_Id,
3096 	From_Organization_Id,
3097 	From_Location_Id,
3098 	To_Organization_Id,
3099 	To_Location_Id,
3100 	Ship_Method,
3101 	UOM_code,
3102 	Supply_Demand_Type,
3103 	Supply_Demand_Source_Type,
3104 	Supply_Demand_Source_Type_Name,
3105 	Identifier1,
3106 	Identifier2,
3107 	Identifier3,
3108 	Identifier4,
3109 	Supply_Demand_Quantity,
3110 	Supply_Demand_Date,
3111 	Disposition_Type,
3112 	Disposition_Name,
3113 	Pegging_Id,
3114 	End_Pegging_Id,
3115 	creation_date,
3116 	created_by,
3117 	last_update_date,
3118 	last_updated_by,
3119 	last_update_login,
3120 	ORIG_CUSTOMER_SITE_NAME,--bug3263368
3121         ORIG_CUSTOMER_NAME, --bug3263368
3122         ORIG_DEMAND_CLASS, --bug3263368
3123         ORIG_REQUEST_DATE --bug3263368
3124 )
3125 (
3126     SELECT      p_level col1,
3127 		p_identifier col2,
3128                 p_scenario_id col3,
3129                 p_item_id col4 ,
3130                 p_request_item_id col5,
3131 		p_org_id col6,
3132                 l_null_num col7,
3133                 l_null_num col8,
3134                 l_null_num col9,
3135                 l_null_num col10,
3136                 l_null_num col11,
3137                 l_null_num col12,
3138                 l_null_num col13,
3139                 l_null_num col14,
3140 		l_null_char col15,
3141 		I.UOM_CODE col16,
3142 		1 col17, -- demand
3143 		--D.ORIGINATION_TYPE col18,
3144 		DECODE( D.ORIGINATION_TYPE, -100, 30, D.ORIGINATION_TYPE) col18, --5027568
3145                 l_null_char col19,
3146 		D.SR_INSTANCE_ID col20,
3147                 l_null_num col21,
3148 		D.DEMAND_ID col22,
3149 		l_null_num col23,
3150                 -1* DECODE(D.ORIGINATION_TYPE,
3151                                     4, D.DAILY_DEMAND_RATE,
3152                                     --D.USING_REQUIREMENT_QUANTITY) col24,
3153                                     (D.USING_REQUIREMENT_QUANTITY - NVL(d.reserved_quantity, 0))) col24, --5027568
3154 		-- C.PRIOR_DATE col25, -- 2859130
3155                 C.CALENDAR_DATE col25,
3156                 l_null_num col26,
3157                 D.ORDER_NUMBER col27,
3158                 l_null_num col28,
3159                 l_null_num col29,
3160 		l_sysdate,
3161 		FND_GLOBAL.User_ID,
3162 		l_sysdate,
3163 		FND_GLOBAL.User_ID,
3164 		FND_GLOBAL.User_ID,
3165 		MTPS.LOCATION, --bug3263368
3166                 MTP.PARTNER_NAME, --bug3263368
3167                 D.DEMAND_CLASS, --bug3263368
3168                 DECODE(D.ORDER_DATE_TYPE_CODE,2,D.REQUEST_DATE,
3169                                             D.REQUEST_SHIP_DATE) --bug3263368
3170     FROM        MSC_SYSTEM_ITEMS I,
3171 		MSC_DEMANDS D,
3172                 MSC_CALENDAR_DATES C,
3173                 MSC_TRADING_PARTNERS    MTP,--bug3263368
3174                 MSC_TRADING_PARTNER_SITES    MTPS --bug3263368
3175     WHERE       I.SR_INVENTORY_ITEM_ID = p_item_id
3176     AND         I.ORGANIZATION_ID = p_org_id
3177     AND		I.SR_INSTANCE_ID = p_instance_id
3178     AND		I.PLAN_ID = p_plan_id
3179     AND		D.PLAN_ID = I.PLAN_ID
3180     AND		D.SR_INSTANCE_ID = I.SR_INSTANCE_ID
3181     AND		D.INVENTORY_ITEM_ID = I.INVENTORY_ITEM_ID
3182     AND 	D.ORGANIZATION_ID = I.ORGANIZATION_ID
3183     AND         D.USING_REQUIREMENT_QUANTITY <> 0 --4501434
3184     AND         D.SHIP_TO_SITE_ID = MTPS.PARTNER_SITE_ID(+) --bug3263368
3185     AND         D.CUSTOMER_ID = MTP.PARTNER_ID(+) --bug3263368
3186     -- 1243985
3187     AND	        D.ORIGINATION_TYPE NOT IN (5,7,8,9,11,15,22,28,29,31,52,70) -- ignore copy SO for summary enhancement
3188     -- Bug1990155, 1995835 exclude the expired lots demand datreya 9/18/2001
3189     -- Bug 1530311, need to exclude forecast
3190     AND		C.CALENDAR_CODE=p_cal_code
3191     AND	        C.EXCEPTION_SET_ID=p_cal_exc_set_id
3192     AND         C.SR_INSTANCE_ID = D.SR_INSTANCE_ID
3193     -- since we store repetitive schedule demand in different ways for
3194     -- ods (total quantity on start date) and pds  (daily quantity from
3195     -- start date to end date), we need to make sure we only select work day
3196     -- for pds's repetitive schedule demand.
3197     -- Bug 3550296 and 3574164. IMPLEMENT_DATE AND DMD_SATISFIED_DATE are changed to
3198     -- IMPLEMENT_SHIP_DATE and PLANNED_SHIP_DATE resp.
3199     AND         C.CALENDAR_DATE
3200                 BETWEEN
3201                 TRUNC(DECODE(D.RECORD_SOURCE,
3202                             2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
3203                                DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
3204                                       2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
3205                                          NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))))
3206                 AND
3207                 TRUNC(NVL(D.ASSEMBLY_DEMAND_COMP_DATE,
3208                       DECODE(D.RECORD_SOURCE,
3209                             2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
3210                                DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
3211                                       2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
3212                                          NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))))--plan by request date,promisedate, schedule date
3213                 -- new clause 2640489 SIMPLIFY
3214     AND         (C.SEQ_NUM IS NOT NULL OR D.ORIGINATION_TYPE  <> 4)
3215     -- AND         ((D.ORIGINATION_TYPE = 4 AND C.SEQ_NUM IS NOT NULL) OR
3216     --               (D.ORIGINATION_TYPE  <> 4))
3217     AND         C.PRIOR_DATE < NVL(p_itf, C.PRIOR_DATE + 1)
3218     UNION ALL
3219     SELECT      p_level col1,
3220                 p_identifier col2,
3221                 p_scenario_id col3,
3222                 p_item_id col4 ,
3223                 p_request_item_id col5,
3224                 p_org_id col6,
3225                 l_null_num col7,
3226                 l_null_num col8,
3227                 l_null_num col9,
3228                 l_null_num col10,
3229                 l_null_num col11,
3230                 l_null_num col12,
3231                 l_null_num col13,
3232                 l_null_num col14,
3233                 l_null_char col15,
3234                 I.UOM_CODE col16,
3235                 2 col17, -- supply
3236                 S.ORDER_TYPE col18,
3237                 l_null_char col19,
3238                 S.SR_INSTANCE_ID col20,
3239                 l_null_num col21,
3240                 S.TRANSACTION_ID col22,
3241                 l_null_num col23,
3242 		NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY) col24,
3243                 -- C.NEXT_DATE col25, -- 2859130
3244                 C.CALENDAR_DATE col25,
3245                 l_null_num col26,
3246                 -- Bug 2771075. For Planned Orders, we will populate transaction_id
3247 		-- in the disposition_name column to be consistent with Planning.
3248 		-- S.ORDER_NUMBER col27,
3249 		DECODE(S.ORDER_TYPE, 5, to_char(S.TRANSACTION_ID), S.ORDER_NUMBER) col27,
3250                 l_null_num col28,
3251 		l_null_num col29,
3252 		l_sysdate,
3253 		FND_GLOBAL.User_ID,
3254 		l_sysdate,
3255 		FND_GLOBAL.User_ID,
3256 		FND_GLOBAL.User_ID,
3257 		--null, --bug3263368 ORIG_CUSTOMER_SITE_NAME
3258                 --null, --bug3263368 ORIG_CUSTOMER_NAME
3259                 --null, --bug3263368 ORIG_DEMAND_CLASS
3260                 --null  --bug3263368 ORIG_REQUEST_DATE
3261                 l_null_char, --bug3814584
3262                 l_null_char, --bug3814584
3263                 l_null_char, --bug3814584
3264                 l_null_date  --bug3814584
3265     FROM        MSC_SYSTEM_ITEMS I,
3266 		MSC_SUPPLIES S,
3267                 MSC_CALENDAR_DATES C
3268     WHERE       I.SR_INVENTORY_ITEM_ID = p_item_id
3269     AND         I.ORGANIZATION_ID = p_org_id
3270     AND         I.SR_INSTANCE_ID = p_instance_id
3271     AND         I.PLAN_ID = p_plan_id
3272     AND		S.PLAN_ID = I.PLAN_ID
3273     AND		S.SR_INSTANCE_ID = I.SR_INSTANCE_ID
3274     AND		S.INVENTORY_ITEM_ID = I.INVENTORY_ITEM_ID
3275     AND 	S.ORGANIZATION_ID = I.ORGANIZATION_ID
3276                 -- Exclude Cancelled Supplies 2460645
3277     AND         NVL(S.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
3278     AND         NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY) <> 0 -- 1243985
3279     AND		C.CALENDAR_CODE = p_cal_code
3280     AND		C.EXCEPTION_SET_ID = p_cal_exc_set_id
3281     AND         C.SR_INSTANCE_ID = S.SR_INSTANCE_ID
3282     AND         C.CALENDAR_DATE BETWEEN TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE))
3283     AND TRUNC(NVL(S.LAST_UNIT_COMPLETION_DATE, NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)))
3284     AND         DECODE(S.LAST_UNIT_COMPLETION_DATE,
3285                        NULL, C.NEXT_SEQ_NUM, C.SEQ_NUM) IS NOT NULL
3286     AND         C.NEXT_DATE < NVL(p_itf, C.NEXT_DATE + 1)
3287 
3288 )
3289 ;
3290 -- dsting 'removed order by col25'
3291 END get_mat_avail_unopt_dtls;
3292 
3293 PROCEDURE get_mat_avail (
3294    p_summary_flag       IN VARCHAR2,
3295    p_optimized_plan     IN NUMBER,
3296    p_item_id            IN NUMBER,
3297    p_org_id             IN NUMBER,
3298    p_instance_id        IN NUMBER,
3299    p_plan_id            IN NUMBER,
3300    p_cal_code           IN VARCHAR2,
3301    p_cal_exc_set_id     IN NUMBER,
3302    p_sysdate_seq_num    IN NUMBER,
3303    p_sys_next_date      IN DATE,
3304    p_demand_class       IN VARCHAR2,
3305    p_default_atp_rule_id IN NUMBER,
3306    p_default_dmd_class  IN VARCHAR2,
3307    p_itf                IN DATE,
3308    p_refresh_number     IN NUMBER,  -- For summary enhancement
3309    x_atp_dates          OUT NoCopy MRP_ATP_PUB.date_arr,
3310    x_atp_qtys           OUT NoCopy MRP_ATP_PUB.number_arr
3311 ) IS
3312 BEGIN
3313    IF PG_DEBUG in ('Y', 'C') THEN
3314       msc_sch_wb.atp_debug('Begin get_mat_avail');
3315    END IF;
3316 
3317    IF MSC_ATP_PVT.G_INV_CTP = 5 THEN
3318       -- ODS atp
3319       IF p_summary_flag = 'Y' THEN
3320          -- summary ODS atp
3321          get_mat_avail_ods_summ(
3322             p_item_id,
3323             p_org_id,
3324             p_instance_id,
3325             p_plan_id,
3326             p_demand_class,
3327             p_default_atp_rule_id,
3328             p_default_dmd_class,
3329             p_itf,
3330             x_atp_dates,
3331             x_atp_qtys
3332          );
3333       ELSE
3334          -- ODS atp
3335          get_mat_avail_ods(
3336             p_item_id,
3337             p_org_id,
3338             p_instance_id,
3339             p_plan_id,
3340             p_cal_code,
3341             p_cal_exc_set_id,
3342             p_sysdate_seq_num,
3343             p_sys_next_date,
3344             p_demand_class,
3345             p_default_atp_rule_id,
3346             p_default_dmd_class,
3347             p_itf,
3348             x_atp_dates,
3349             x_atp_qtys
3350          );
3351       END IF;
3352    ELSE
3353       -- PDS atp
3354       IF p_summary_flag = 'Y' THEN
3355          -- ATP4drp Changes to support ATP for DRP plans.
3356          IF (NVL(MSC_ATP_PVT.G_PLAN_INFO_REC.plan_type,1) = 5) THEN
3357              -- DRP plan call DRP plan specific summary
3358              MSC_ATP_DRP.get_mat_avail_drp_summ(
3359                 p_item_id,
3360                 p_org_id,
3361                 p_instance_id,
3362                 p_plan_id,
3363                 p_itf,
3364                 p_refresh_number,   -- For summary enhancement
3365                 x_atp_dates,
3366                 x_atp_qtys
3367              );
3368          ELSE -- Call regular summary
3369              get_mat_avail_summ(
3370                 p_item_id,
3371                 p_org_id,
3372                 p_instance_id,
3373                 p_plan_id,
3374                 p_itf,
3375                 p_refresh_number,   -- For summary enhancement
3376                 x_atp_dates,
3377                 x_atp_qtys
3378              );
3379          END IF;
3380          -- End ATP4drp
3381       ELSE
3382          IF nvl(p_optimized_plan, 2) = 1 THEN
3383             -- constrained plan
3384             -- ATP4drp Changes to support ATP for DRP plans.
3385             IF (NVL(MSC_ATP_PVT.G_PLAN_INFO_REC.plan_type,1) = 5) THEN
3386               MSC_ATP_DRP.get_mat_avail_drp(
3387                  p_item_id,
3388                  p_org_id,
3389                  p_instance_id,
3390                  p_plan_id,
3391                  p_itf,
3392                  x_atp_dates,
3393                  x_atp_qtys
3394               );
3395             ELSE
3396               get_mat_avail_opt(
3397                  p_item_id,
3398                  p_org_id,
3399                  p_instance_id,
3400                  p_plan_id,
3401                  p_itf,
3402                  x_atp_dates,
3403                  x_atp_qtys
3404               );
3405             END IF;
3406             -- End ATP4drp
3407          ELSE
3408             -- unconstrained plan
3409             get_mat_avail_unopt(
3410                p_item_id,
3411                p_org_id,
3412                p_instance_id,
3413                p_plan_id,
3414                p_cal_code,
3415                p_cal_exc_set_id,
3416                p_itf,
3417                x_atp_dates,
3418                x_atp_qtys
3419             );
3420          END IF; -- (un)optimized plan
3421       END IF; -- summary atp
3422    END IF; -- ODS/PDS
3423 END get_mat_avail;
3424 
3425 PROCEDURE get_mat_avail_dtls (
3426    p_optimized_plan     IN NUMBER,
3427    p_item_id            IN NUMBER,
3428    p_request_item_id    IN NUMBER,
3429    p_org_id             IN NUMBER,
3430    p_instance_id        IN NUMBER,
3431    p_plan_id            IN NUMBER,
3432    p_cal_code           IN VARCHAR2,
3433    p_cal_exc_set_id     IN NUMBER,
3434    p_sysdate_seq_num    IN NUMBER,
3435    p_sys_next_date      IN DATE,
3436    p_demand_class       IN VARCHAR2,
3437    p_default_atp_rule_id IN NUMBER,
3438    p_default_dmd_class  IN VARCHAR2,
3439    p_itf                IN DATE,
3440    p_level              IN NUMBER,
3441    p_scenario_id        IN NUMBER,
3442    p_identifier         IN NUMBER
3443 ) IS
3444 BEGIN
3445    IF PG_DEBUG in ('Y', 'C') THEN
3446       msc_sch_wb.atp_debug('Begin get_mat_avail_dtls');
3447    END IF;
3448 
3449    IF MSC_ATP_PVT.G_INV_CTP = 5 THEN
3450       -- ODS atp
3451       get_mat_avail_ods_dtls(
3452          p_item_id,
3453          p_request_item_id,
3454          p_org_id,
3455          p_instance_id,
3456          p_plan_id,
3457          p_cal_code,
3458          p_cal_exc_set_id,
3459          p_sysdate_seq_num,
3460          p_sys_next_date,
3461          p_demand_class,
3462          p_default_atp_rule_id,
3463          p_default_dmd_class,
3464          p_itf,
3465          p_level,
3466          p_scenario_id,
3467          p_identifier
3468       );
3469    ELSE
3470       -- PDS atp
3471       IF nvl(p_optimized_plan, 2) = 1 THEN
3472          -- constrained plan
3473          -- ATP4drp Changes to support ATP for DRP plans.
3474          IF (NVL(MSC_ATP_PVT.G_PLAN_INFO_REC.plan_type,1) = 5) THEN
3475               MSC_ATP_DRP.get_mat_avail_drp_dtls(
3476                  p_item_id,
3477                  p_request_item_id,
3478                  p_org_id,
3479                  p_instance_id,
3480                  p_plan_id,
3481                  p_itf,
3482                  p_level,
3483                  p_scenario_id,
3484                  p_identifier
3485               );
3486          ELSE
3487               get_mat_avail_opt_dtls(
3488                  p_item_id,
3489                  p_request_item_id,
3490                  p_org_id,
3491                  p_instance_id,
3492                  p_plan_id,
3493                  p_itf,
3494                  p_level,
3495                  p_scenario_id,
3496                  p_identifier
3497               );
3498          END IF;
3499          -- End ATP4drp
3500       ELSE
3501          -- unconstrained plan
3502          get_mat_avail_unopt_dtls(
3503             p_item_id,
3504             p_request_item_id,
3505             p_org_id,
3506             p_instance_id,
3507             p_plan_id,
3508             p_cal_code,
3509             p_cal_exc_set_id,
3510             p_itf,
3511             p_level,
3512             p_scenario_id,
3513             p_identifier
3514          );
3515       END IF; -- (un)optimized plan
3516    END IF; -- ODS/PDS
3517 END get_mat_avail_dtls;
3518 -- 2859130 end sqls
3519 
3520 /*--Calculate_Atp_Dates_Qtys------------------------------------------------
3521 |  o  New private procedure added as part of time_phased_atp project
3522 |  o  Moved ATP dates and qty calculation code from Get_Material_Atp_Info
3523 |     procedure to this procedure
3524 +-------------------------------------------------------------------------*/
3525 PROCEDURE Calculate_Atp_Dates_Qtys (
3526         p_atp_period_tab                      IN    MRP_ATP_PUB.Date_arr,
3527         p_atp_qty_tab                         IN    MRP_ATP_PUB.Number_arr,
3528         p_requested_date                      IN    DATE,
3529         p_atf_date                            IN    DATE,
3530         p_quantity_ordered                    IN    NUMBER,
3531         p_sys_next_date                       IN    DATE,
3532         p_round_flag                          IN    NUMBER,
3533         x_requested_date_quantity             OUT   NOCOPY NUMBER,
3534         x_atf_date_quantity                   OUT   NOCOPY NUMBER,
3535         x_atp_date_this_level                 OUT   NOCOPY DATE,
3536         x_atp_date_quantity_this_level        OUT   NOCOPY NUMBER,
3537         x_return_status                       OUT   NOCOPY VARCHAR2
3538 )
3539 IS
3540         l_atp_requested_date            DATE;
3541         l_next_period			DATE;
3542 
3543 BEGIN
3544         IF PG_DEBUG in ('Y', 'C') THEN
3545            msc_sch_wb.atp_debug('********** Calculate_Atp_Dates_Qtys **********');
3546            msc_sch_wb.atp_debug('Calculate_Atp_Dates_Qtys: ' || 'p_requested_date: '|| to_char(p_requested_date));
3547            msc_sch_wb.atp_debug('Calculate_Atp_Dates_Qtys: ' || 'p_quantity_ordered: '|| to_char(p_quantity_ordered));
3548            msc_sch_wb.atp_debug('Calculate_Atp_Dates_Qtys: ' || 'p_sys_next_date: '|| to_char(p_sys_next_date));
3549            msc_sch_wb.atp_debug('Calculate_Atp_Dates_Qtys: ' || 'p_round_flag: '|| to_char(p_round_flag));
3550         END IF;
3551 
3552         -- initialize API return status to success
3553         x_return_status := FND_API.G_RET_STS_SUCCESS;
3554 
3555         -- if requested date is eariler than sysdate, we have an issue here.
3556         -- this is possible since we have the offset from requested arrival
3557         -- date.  if requested date is eariler than sysdate, we should set
3558         -- the x_requested_date_quantity = 0, and find the atp date and
3559         -- quantity from sysdate.
3560 
3561         -- we use this l_atp_requested_date to do the search
3562         l_atp_requested_date := GREATEST(p_requested_date, trunc(p_sys_next_date));
3563 
3564         IF (l_atp_requested_date < p_atp_period_tab(1)) THEN
3565 
3566             -- let say the first period is on Day5 but your
3567             -- request in on Day2.  for bug 948863
3568             x_requested_date_quantity := 0;
3569             FOR k IN 1..p_atp_period_tab.COUNT LOOP
3570                 IF K = p_atp_period_tab.COUNT THEN
3571                     -- RAJJAIN Bug 2558593, in case component is available only on infinite time fence
3572                     -- which is prior to PTF date, return PTF date as the avaialble date
3573                     IF p_atp_qty_tab(k) >= p_quantity_ordered THEN
3574                        IF (p_round_flag = 1) THEN
3575                           x_atp_date_quantity_this_level := FLOOR(p_atp_qty_tab(k));
3576                        ELSE
3577                           x_atp_date_quantity_this_level := p_atp_qty_tab(k);
3578                        END IF;
3579                        x_atp_date_this_level := GREATEST(p_atp_period_tab(k), MSC_ATP_PVT.G_PTF_DATE);
3580                        IF PG_DEBUG in ('Y', 'C') THEN
3581                           msc_sch_wb.atp_debug('Calculate_Atp_Dates_Qtys: ' || x_atp_date_quantity_this_level);
3582                           msc_sch_wb.atp_debug('Calculate_Atp_Dates_Qtys: ' || x_atp_date_this_level);
3583                        END IF;
3584 
3585                     END IF;
3586                     EXIT;
3587                 ELSIF (p_atp_qty_tab(k) >= p_quantity_ordered) AND
3588                       ((p_atp_period_tab(k) <= MSC_ATP_PVT.G_PTF_DATE AND
3589                       p_atp_period_tab(k+1)> MSC_ATP_PVT.G_PTF_DATE)
3590                           OR (p_atp_period_tab(k) > MSC_ATP_PVT.G_PTF_DATE))  THEN
3591                     IF (p_round_flag = 1) THEN
3592                        x_atp_date_quantity_this_level := FLOOR(p_atp_qty_tab(k));
3593                     ELSE
3594                        x_atp_date_quantity_this_level := p_atp_qty_tab(k);
3595                     END IF;
3596                     x_atp_date_this_level := GREATEST(p_atp_period_tab(k), MSC_ATP_PVT.G_PTF_DATE);
3597                     IF PG_DEBUG in ('Y', 'C') THEN
3598                        msc_sch_wb.atp_debug('Calculate_Atp_Dates_Qtys: ' || x_atp_date_quantity_this_level);
3599                        msc_sch_wb.atp_debug('Calculate_Atp_Dates_Qtys: ' || x_atp_date_this_level);
3600                     END IF;
3601                     EXIT;
3602                 END IF;
3603             END LOOP; -- end of k loop
3604 
3605         ELSE
3606                 -- find the requested date atp quantity
3607 
3608                 -- if requested date is eariler than sysdate, we have an issue here.
3609                 -- this is possible since we have the offset from requested arrival
3610                 -- date.  if requested date is eariler than sysdate, we should set
3611                 -- the x_requested_date_quantity = 0, and find the atp date and
3612                 -- quantity from sysdate.
3613                 IF PG_DEBUG in ('Y', 'C') THEN
3614                    msc_sch_wb.atp_debug('Calculate_Atp_Dates_Qtys: ' || 'I am here 1');
3615                 END IF;
3616                 FOR j IN 1..p_atp_period_tab.COUNT LOOP
3617 
3618                     -- time_phased_atp changes begin
3619                     IF (x_atf_date_quantity is null) and (p_atf_date is not null) THEN
3620                         IF PG_DEBUG in ('Y', 'C') THEN
3621                             msc_sch_wb.atp_debug('*********************');
3622                             msc_sch_wb.atp_debug('Calculate_Atp_Dates_Qtys: p_atp_period_tab(j): ' || p_atp_period_tab(j));
3623                             msc_sch_wb.atp_debug('Calculate_Atp_Dates_Qtys: p_atf_date: ' || p_atf_date);
3624                             msc_sch_wb.atp_debug('Calculate_Atp_Dates_Qtys: p_atp_qty_tab(j): ' || p_atp_qty_tab(j));
3625                         END IF;
3626                         IF p_atp_period_tab(j) = p_atf_date THEN
3627                             x_atf_date_quantity := p_atp_qty_tab(j);
3628                         ELSIF p_atp_period_tab(j) > p_atf_date THEN
3629                             IF j = 1 THEN
3630                                 x_atf_date_quantity := 0;
3631                             ELSE
3632                                 IF PG_DEBUG in ('Y', 'C') THEN
3633                                     msc_sch_wb.atp_debug('Calculate_Atp_Dates_Qtys: p_atp_qty_tab(j-1): ' || p_atp_qty_tab(j-1));
3634                                 END IF;
3635                                 x_atf_date_quantity := p_atp_qty_tab(j-1);
3636                             END IF;
3637                         END IF;
3638 
3639                         IF PG_DEBUG in ('Y', 'C') THEN
3640                             msc_sch_wb.atp_debug('Calculate_Atp_Dates_Qtys: x_atf_date_quantity: ' || x_atf_date_quantity);
3641                         END IF;
3642                     END IF;
3643                     -- time_phased_atp changes end
3644 
3645                     -- Please state reason for the else condition here
3646                     -- the reason that we need this else condition is the following
3647                     -- let say the last record in the bucket is Day5, and request
3648                     -- date is Day10.  So the bucket that the the request date is
3649                     -- falling into is Day5. So we should use Day5's quantity
3650                     -- as the quantity for Day10. By setting l_next_period this way,
3651                     -- we make sure we are using the right bucket to get
3652                     -- request date quantuty.
3653 
3654                     IF j < p_atp_period_tab.LAST THEN
3655                         l_next_period := p_atp_period_tab(j+1);
3656                     ELSE
3657                         l_next_period := l_atp_requested_date + 1;
3658                     END IF;
3659                     IF PG_DEBUG in ('Y', 'C') THEN
3660                        msc_sch_wb.atp_debug('Calculate_Atp_Dates_Qtys: ' || 'I am here 2');
3661                     END IF;
3662 
3663                     --- bug 1819638:  If sysdate is non-work day then we want to check the request on
3664                     --- next working day (p_sys_next_date)
3665 
3666                     IF ((p_atp_period_tab(j) <= GREATEST(l_atp_requested_date, p_sys_next_date)) and
3667                            (l_next_period > GREATEST(l_atp_requested_date, p_sys_next_date))) THEN
3668 
3669 
3670                         --IF p_requested_date < l_atp_requested_date THEN
3671         		IF PG_DEBUG in ('Y', 'C') THEN
3672         		   msc_sch_wb.atp_debug('Calculate_Atp_Dates_Qtys: ' || 'I am here 3');
3673         		END IF;
3674                         -- Bug 3512996 - For request before PTF return 0 as request date quantity
3675                         -- IF (p_requested_date < l_atp_requested_date) THEN
3676                         -- IF (p_requested_date < GREATEST(l_atp_requested_date,MSC_ATP_PVT.G_PTF_DATE)) THEN
3677                         IF (p_requested_date < l_atp_requested_date) THEN -- Bug 3828469 - Undo the regression introduced by Bug 3512996
3678                               IF PG_DEBUG in ('Y', 'C') THEN
3679                                  msc_sch_wb.atp_debug('Calculate_Atp_Dates_Qtys: ' || 'p_requested_date < l_atp_requested_date');
3680                               END IF;
3681                               x_requested_date_quantity := 0;
3682                         ELSE
3683                               IF (p_round_flag = 1) THEN --- bug 1774959
3684                                    x_requested_date_quantity := FLOOR(p_atp_qty_tab(j));
3685                               ELSE
3686                                    x_requested_date_quantity := p_atp_qty_tab(j);
3687                               END IF;
3688                               IF PG_DEBUG in ('Y', 'C') THEN
3689                                  msc_sch_wb.atp_debug('Calculate_Atp_Dates_Qtys: ' || 'p_requested_date > l_atp_requested_date');
3690                               END IF;
3691                         END IF;
3692                 	IF PG_DEBUG in ('Y', 'C') THEN
3693                 	   msc_sch_wb.atp_debug('Calculate_Atp_Dates_Qtys: ' || 'x_requested_date_quantity: '|| to_char(x_requested_date_quantity));
3694                 	   msc_sch_wb.atp_debug('Calculate_Atp_Dates_Qtys: ' || 'test line p_atp_qty_tab(j): '|| to_char(p_atp_qty_tab(j)));
3695                 	   msc_sch_wb.atp_debug('Calculate_Atp_Dates_Qtys: ' || 'p_quantity_ordered: '|| to_char(p_quantity_ordered));
3696                 	END IF;
3697 
3698                         --  now find the atp_date_quantity and atp_date at this level
3699                         IF p_atp_qty_tab(j) >= p_quantity_ordered AND
3700                           (p_atp_period_tab(j) >= NVL(MSC_ATP_PVT.G_PTF_DATE,p_sys_next_date)) THEN
3701                             IF(p_round_flag = 1) THEN --- bug 1774959
3702                                 x_atp_date_quantity_this_level := FLOOR(p_atp_qty_tab(j));
3703                             ELSE
3704                                 x_atp_date_quantity_this_level := p_atp_qty_tab(j);
3705                             END IF;
3706                             -- bug 1560255
3707                             --x_atp_date_quantity_this_level := p_quantity_ordered;
3708                             IF PG_DEBUG in ('Y', 'C') THEN
3709                                msc_sch_wb.atp_debug('Calculate_Atp_Dates_Qtys: ' || 'Checkpoint 1');
3710                             END IF;
3711                             x_atp_date_this_level := GREATEST(l_atp_requested_date, p_sys_next_date);
3712 
3713                             IF PG_DEBUG in ('Y', 'C') THEN
3714                                msc_sch_wb.atp_debug('Calculate_Atp_Dates_Qtys: ' || 'x_atp_date_this_level: '|| to_char(x_atp_date_this_level));
3715                                msc_sch_wb.atp_debug('Calculate_Atp_Dates_Qtys: ' || 'x_atp_date_quantity_this_level: '|| to_char(x_atp_date_quantity_this_level));
3716                             END IF;
3717 
3718 
3719                         ELSE
3720                             IF j = p_atp_period_tab.COUNT THEN
3721                                 IF p_atp_qty_tab(j) >= p_quantity_ordered THEN
3722                                    IF(p_round_flag = 1) THEN --- bug 1774959
3723                                       x_atp_date_quantity_this_level := FLOOR(p_atp_qty_tab(j));
3724                                    ELSE
3725                                       x_atp_date_quantity_this_level := p_atp_qty_tab(j);
3726                                    END IF;
3727 
3728                                    x_atp_date_this_level := GREATEST(p_atp_period_tab(j),
3729                                                         GREATEST(MSC_ATP_PVT.G_PTF_DATE,p_sys_next_date));
3730                                 ELSE
3731                                    x_atp_date_quantity_this_level := NULL;
3732                                    x_atp_date_this_level := NULL;
3733                                 END IF;
3734 
3735                             ELSIF (p_atp_qty_tab(j) >= p_quantity_ordered AND
3736                                            p_atp_period_tab(j+1) > NVL(MSC_ATP_PVT.G_PTF_DATE,p_sys_next_date)) THEN
3737                                 IF(p_round_flag = 1) THEN --- bug 1774959
3738                                    x_atp_date_quantity_this_level := FLOOR(p_atp_qty_tab(j));
3739                                 ELSE
3740                                    x_atp_date_quantity_this_level := p_atp_qty_tab(j);
3741                                 END IF;
3742                                 -- bug 1560255
3743                                 --x_atp_date_quantity_this_level := p_quantity_ordered;
3744                                 IF PG_DEBUG in ('Y', 'C') THEN
3745                                    msc_sch_wb.atp_debug('Calculate_Atp_Dates_Qtys: ' || 'Checkpoint 3');
3746                                 END IF;
3747                                 x_atp_date_this_level := GREATEST(NVL(MSC_ATP_PVT.G_PTF_DATE,p_sys_next_date)
3748                                                                   , p_atp_period_tab(j));
3749                             ELSE
3750                                 IF PG_DEBUG in ('Y', 'C') THEN
3751                                    msc_sch_wb.atp_debug('Calculate_Atp_Dates_Qtys: ' || 'Checkpoint 2');
3752                                 END IF;
3753                                 FOR k IN j+1..p_atp_period_tab.COUNT LOOP
3754 
3755                                     -- time_phased_atp changes begin
3756                                     IF (x_atf_date_quantity is null) and (p_atf_date is not null) THEN
3757                                         IF PG_DEBUG in ('Y', 'C') THEN
3758                                             msc_sch_wb.atp_debug('*********************');
3759                                             msc_sch_wb.atp_debug('Calculate_Atp_Dates_Qtys: p_atp_period_tab(k): ' || p_atp_period_tab(k));
3760                                             msc_sch_wb.atp_debug('Calculate_Atp_Dates_Qtys: p_atf_date: ' || p_atf_date);
3761                                             msc_sch_wb.atp_debug('Calculate_Atp_Dates_Qtys: p_atp_qty_tab(k): ' || p_atp_qty_tab(k));
3762                                         END IF;
3763                                         IF p_atp_period_tab(k) = p_atf_date THEN
3764                                             x_atf_date_quantity := p_atp_qty_tab(k);
3765                                         ELSIF p_atp_period_tab(k) > p_atf_date THEN
3766                                             IF k = 1 THEN
3767                                                 x_atf_date_quantity := 0;
3768                                             ELSE
3769                                                 IF PG_DEBUG in ('Y', 'C') THEN
3770                                                     msc_sch_wb.atp_debug('Calculate_Atp_Dates_Qtys: p_atp_qty_tab(k-1): ' || p_atp_qty_tab(k-1));
3771                                                 END IF;
3772                                                 x_atf_date_quantity := p_atp_qty_tab(k-1);
3773                                             END IF;
3774                                         END IF;
3775 
3776                                         IF PG_DEBUG in ('Y', 'C') THEN
3777                                             msc_sch_wb.atp_debug('Calculate_Atp_Dates_Qtys: x_atf_date_quantity: ' || x_atf_date_quantity);
3778                                         END IF;
3779                                     END IF;
3780                                     -- time_phased_atp changes end
3781 
3782                                     IF (p_atp_qty_tab(k) >= p_quantity_ordered) AND
3783                                         p_atp_period_tab(k) >= NVL(MSC_ATP_PVT.G_PTF_DATE, p_sys_next_date) THEN
3784 
3785                                         IF(p_round_flag = 1) THEN --- bug 1774959
3786                                            x_atp_date_quantity_this_level := FLOOR(p_atp_qty_tab(k));
3787                                         ELSE
3788                                            x_atp_date_quantity_this_level := p_atp_qty_tab(k);
3789                                         END IF;
3790                                         x_atp_date_this_level := p_atp_period_tab(k);
3791                                         EXIT;
3792                                     ELSIF (p_atp_qty_tab(k) >= p_quantity_ordered) AND k = p_atp_period_tab.COUNT THEN
3793                                         --bug 2787159: This condition is redundant
3794                                         --IF p_atp_qty_tab(j) >= p_quantity_ordered THEN
3795                                            IF(p_round_flag = 1) THEN --- bug 1774959
3796                                               x_atp_date_quantity_this_level := FLOOR(p_atp_qty_tab(k));
3797                                            ELSE
3798                                               x_atp_date_quantity_this_level := p_atp_qty_tab(k);
3799                                            END IF;
3800                                            --bug 2787159: We should be using the record which has higher capacity.
3801                                            ---Record with j will
3802                                            -- always have lower quantity or lower date than G_PTF_DATE.
3803                                            --x_atp_date_this_level := GREATEST(p_atp_period_tab(j),
3804                                            x_atp_date_this_level := GREATEST(p_atp_period_tab(k),
3805                                                          GREATEST(MSC_ATP_PVT.G_PTF_DATE,p_sys_next_date));
3806                                         --ELSE
3807                                         --    x_atp_date_quantity_this_level := NULL;
3808                                         --    x_atp_date_this_level := NULL;
3809                                         --END IF;
3810 
3811                                     ELSIF  (p_atp_qty_tab(k) >= p_quantity_ordered) AND
3812                                              -- Bug 3862224, handled the case where ptf_date has some supply/demand activity, removed equality check
3813                                              --p_atp_period_tab(k+1) >=
3814                                              p_atp_period_tab(k+1) >
3815                                                    NVL(MSC_ATP_PVT.G_PTF_DATE, p_sys_next_date) THEN
3816 
3817                                          x_atp_date_this_level := NVL(MSC_ATP_PVT.G_PTF_DATE, p_sys_next_date);
3818                                          IF PG_DEBUG in ('Y', 'C') THEN
3819                                              msc_sch_wb.atp_debug('x_atp_date_this_level '||x_atp_date_this_level);
3820                                          END IF;
3821                                          IF(p_round_flag = 1) THEN --- bug 1774959
3822                                             x_atp_date_quantity_this_level := FLOOR(p_atp_qty_tab(k));
3823                                          ELSE
3824                                             x_atp_date_quantity_this_level := p_atp_qty_tab(k);
3825                                          END IF;
3826                                          EXIT;
3827 
3828 
3829                                     END IF;
3830                                 END LOOP; -- end of k loop
3831                             END IF; -- end if j = p_atp_period_tab.COUNT
3832                         END IF; -- end if p_atp_qty_tab(j)>=p_quantity_ordered
3833                         EXIT;
3834                     END IF; -- end if we find the bucket
3835                 END LOOP; -- end j loop
3836         END IF;
3837 
3838 EXCEPTION
3839     WHEN OTHERS THEN
3840         IF PG_DEBUG in ('Y', 'C') THEN
3841            msc_sch_wb.atp_debug('Calculate_Atp_Dates_Qtys: ' || 'Encountered error = ' || sqlerrm);
3842            msc_sch_wb.atp_debug('Calculate_Atp_Dates_Qtys: ' || 'Error Code = ' || sqlcode);
3843         END IF;
3844         x_return_status := FND_API.G_RET_STS_ERROR;
3845 
3846 END Calculate_Atp_Dates_Qtys;
3847 
3848 PROCEDURE Check_Substitutes(
3849   p_atp_record        IN OUT NoCopy MRP_ATP_PVT.AtpRec,
3850   p_parent_pegging_id IN     NUMBER,
3851   p_instance_id       IN     NUMBER,
3852   p_scenario_id       IN     NUMBER,
3853   p_level             IN     NUMBER,
3854   p_search            IN     NUMBER,
3855   p_plan_id           IN     NUMBER,
3856   p_inventory_item_id IN     NUMBER,
3857   p_organization_id   IN     NUMBER,
3858   p_quantity          IN     NUMBER,
3859   l_net_demand        IN OUT NoCopy NUMBER,
3860   l_supply_demand     IN OUT NoCopy MRP_ATP_PUB.ATP_Supply_Demand_Typ,
3861   l_atp_period        IN OUT NoCopy MRP_ATP_PUB.ATP_Period_Typ,
3862   l_substitutes_rec   OUT    NoCopy MSC_ATP_REQ.get_subs_out_rec,--5216528
3863   l_return_status     OUT    NoCopy varchar2,
3864   p_refresh_number    IN     NUMBER) -- For summary enhancement
3865 IS
3866   /* Bug 4741012 changed the cursor.
3867   CURSOR substitute(l_requested_ship_date date) IS
3868   SELECT msi.inventory_item_id, msi.sr_inventory_item_id,
3869          (sub.usage_quantity/comp.usage_quantity),
3870          msi.atp_flag,msi.atp_components_flag,comp.usage_quantity
3871          , msi.item_name        -- Modularize remove unecessary calls.
3872          --diag_atp
3873          --bug3609031 adding ceil
3874          ,ceil(msi.postprocessing_lead_time), ceil(msi.preprocessing_lead_time),
3875           msi.variable_lead_time, msi.fixed_lead_time,
3876           msi.unit_weight, msi.unit_volume,
3877           msi.weight_uom, msi.volume_uom, msi.rounding_control_type
3878          --time_phased_atp
3879         -- ,nvl(msi.product_family_id, msi.inventory_item_id)
3880          --bug 4891470
3881         ,DECODE(msi.atp_flag, 'N', msi.inventory_item_id,
3882                 DECODE(msi.product_family_id,
3883            NULL, msi.inventory_item_id,
3884            -23453, msi.inventory_item_id,
3885           msi.product_family_id))
3886          ,msi.aggregate_time_fence_date
3887          --4570421
3888          ,comp.scaling_type scaling_type
3889          ,comp.scale_multiple scale_multiple
3890          ,comp.scale_rounding_variance scale_rounding_variance
3891          ,comp.rounding_direction rounding_direction
3892          ,comp.component_yield_factor component_yield_factor
3893 
3894   FROM   msc_system_items msi, msc_component_substitutes sub,
3895          msc_bom_components comp, msc_boms bom, msc_system_items ch,
3896          msc_system_items pt, mrp_atp_details_temp peg
3897   WHERE  peg.session_id = MSC_ATP_PVT.G_SESSION_ID
3898   AND    peg.pegging_id = p_parent_pegging_id
3899   AND    pt.sr_instance_id = p_instance_id
3900   AND    pt.organization_id = peg.organization_id
3901   AND    pt.sr_inventory_item_id = peg.inventory_item_id
3902   AND    pt.plan_id = p_plan_id
3903   AND    ch.plan_id = pt.plan_id
3904   AND    ch.organization_id = pt.organization_id
3905   AND    ch.sr_instance_id = pt.sr_instance_id
3906   AND    ch.sr_inventory_item_id = p_inventory_item_id
3907   AND    bom.plan_id = pt.plan_id
3908   AND    bom.assembly_item_id = pt.inventory_item_id
3909   AND    bom.organization_id = peg.organization_id
3910 -- performance dsting change p_instance_id to pt.sr_instance_id
3911   AND    bom.sr_instance_id = pt.sr_instance_id
3912   and    bom.alternate_bom_designator is null
3913   AND    comp.bill_sequence_id = bom.bill_sequence_id
3914   AND    comp.inventory_item_id = ch.inventory_item_id
3915   AND    TRUNC(NVL(comp.DISABLE_DATE, l_requested_ship_date+1)) >  -- 1221363
3916                   trunc(l_requested_ship_date)
3917   AND    TRUNC(comp.EFFECTIVITY_DATE) <= TRUNC(l_requested_ship_date)
3918   AND    comp.plan_id = bom.plan_id
3919   AND    sub.bill_sequence_id = comp.bill_sequence_id
3920   AND    sub.plan_id = comp.plan_id
3921   AND    sub.component_sequence_id = comp.component_sequence_id
3922   AND    msi.inventory_item_id = sub.substitute_item_id
3923   AND    msi.organization_id = comp.organization_id
3924   AND    msi.sr_instance_id = comp.sr_instance_id
3925   AND    msi.plan_id = sub.plan_id
3926   -- BUG 2752227 only get ATPeable substitutes.
3927   AND    msi.atp_flag in ('Y', 'C')
3928   ORDER BY priority;
3929   */
3930   CURSOR substitute(l_requested_ship_date date) IS
3931   SELECT msi.inventory_item_id, msi.sr_inventory_item_id,
3932          decode (NVL(MSC_ATP_PVT.G_ORG_INFO_REC.org_type, MSC_ATP_PVT.DISCRETE_ORG),
3933                  MSC_ATP_PVT.DISCRETE_ORG, decode (nvl (comp.scaling_type, 1),
3934                                                    1, (sub.usage_quantity/comp.usage_quantity),
3935 	                                           2,  sub.usage_quantity
3936 	                                           ),
3937 	         MSC_ATP_PVT.OPM_ORG     , decode (nvl (comp.scaling_type, 1),
3938 	                                           0,  sub.usage_quantity,
3939 	                                           1, (sub.usage_quantity/comp.usage_quantity),
3940 	                                           2,  sub.usage_quantity,
3941 	                                           3, (sub.usage_quantity/comp.usage_quantity),
3942 	                                           4, (sub.usage_quantity/comp.usage_quantity),
3943 	                                           5, (sub.usage_quantity/comp.usage_quantity)
3944 	                                           )
3945 	 ), --5008983
3946          --(sub.usage_quantity/comp.usage_quantity),
3947          msi.atp_flag,msi.atp_components_flag,comp.usage_quantity
3948          , msi.item_name        -- Modularize remove unecessary calls.
3949          --diag_atp
3950          --bug3609031 adding ceil
3951          ,ceil(msi.postprocessing_lead_time), ceil(msi.preprocessing_lead_time),
3952           msi.variable_lead_time, msi.fixed_lead_time,
3953           msi.unit_weight, msi.unit_volume,
3954           msi.weight_uom, msi.volume_uom, msi.rounding_control_type
3955          --time_phased_atp
3956          --,nvl(msi.product_family_id, msi.inventory_item_id) --5006799
3957          ,DECODE(msi.product_family_id,
3958            NULL, msi.inventory_item_id,
3959            -23453, msi.inventory_item_id,
3960           msi.product_family_id)
3961          ,msi.aggregate_time_fence_date
3962          --4570421
3963          ,comp.scaling_type scaling_type
3964          ,comp.scale_multiple scale_multiple
3965          ,comp.scale_rounding_variance scale_rounding_variance
3966          ,comp.rounding_direction rounding_direction
3967          ,comp.component_yield_factor component_yield_factor
3968          ,sub.usage_quantity/(comp.usage_quantity*comp.component_yield_factor) usage_qty --4775920
3969 
3970   FROM   msc_system_items msi, msc_component_substitutes sub,
3971          msc_bom_components comp, msc_system_items ch
3972   WHERE  ch.plan_id = p_plan_id
3973   AND    ch.organization_id = p_organization_id
3974   AND    ch.sr_instance_id = p_instance_id
3975   AND    ch.sr_inventory_item_id = p_inventory_item_id
3976   AND    comp.bill_sequence_id = p_atp_record.bill_seq_id
3977   AND    comp.inventory_item_id = ch.inventory_item_id
3978   AND    TRUNC(NVL(comp.DISABLE_DATE, l_requested_ship_date+1)) >
3979                   trunc(l_requested_ship_date)
3980   AND    TRUNC(comp.EFFECTIVITY_DATE) <= TRUNC(l_requested_ship_date)
3981   AND    comp.plan_id = ch.plan_id
3982   AND    sub.bill_sequence_id = comp.bill_sequence_id
3983   AND    sub.plan_id = comp.plan_id
3984   AND    sub.component_sequence_id = comp.component_sequence_id
3985   AND    msi.inventory_item_id = sub.substitute_item_id
3986   AND    msi.organization_id = comp.organization_id
3987   AND    msi.sr_instance_id = comp.sr_instance_id
3988   AND    msi.plan_id = sub.plan_id
3989   AND    msi.atp_flag in ('Y', 'C')
3990   ORDER BY priority;
3991 
3992   l_requested_ship_date          date;
3993   l_atp_date_this_level          date;
3994   l_atp_date_quantity_this_level number;
3995   l_requested_date_quantity      number;
3996   l_substitute_id                number := 0.0;
3997   l_primary_comp_usage           number;
3998   --4570421
3999   l_demand_quantity              number; --4570421
4000   l_usage                        number;
4001   l_demand_id                    number;
4002   g_atp_record                   MRP_ATP_PVT.AtpRec;
4003   l_atp_insert_rec               MRP_ATP_PVT.AtpRec;
4004   g_atp_period                   MRP_ATP_PUB.ATP_Period_Typ;
4005   g_atp_supply_demand            MRP_ATP_PUB.ATP_Supply_Demand_Typ;
4006   l_pegging_rec                  mrp_atp_details_temp%ROWTYPE;
4007   l_pegging_id                   number;
4008   l_atp_pegging_id               number;
4009   l_atp_flag                     varchar2(1) := 'Y';
4010   l_atp_comp_flag                varchar2(1) := 'N';
4011   --l_sysdate                      date;
4012   l_plan_id                      number;
4013   l_assign_set_id                number;
4014   l_inv_item_name                varchar2(250); --bug 2246200
4015   l_org_code                     varchar2(7);
4016   l_inv_item_id                  number; -- 1665483
4017   l_summary_flag		 varchar2(1);
4018   l_plan_info_rec                MSC_ATP_PVT.plan_info_rec;   -- added for bug 2392456
4019 
4020   --diag_atp
4021   L_GET_MAT_IN_REC               MSC_ATP_REQ.GET_MAT_IN_REC;
4022   l_get_mat_out_rec              MSC_ATP_REQ.get_mat_out_rec;
4023   l_post_pro_lt                  number;
4024   l_pre_pro_lt                   number;
4025   l_process_lt                   number;
4026   l_fixed_lt                     number;
4027   l_variable_lt                  number;
4028   l_rounding_control_flag        number;
4029   l_weight_capacity              number;
4030   l_volume_capacity              number;
4031   l_weight_uom                   varchar2(3);
4032   l_volume_uom                   varchar2(3);
4033 
4034   -- time_phased_atp
4035   l_pf_item_id                   NUMBER;
4036   l_atf_date                     DATE;
4037   l_time_phased_atp              VARCHAR2(1) := 'N';
4038   l_pf_atp                       VARCHAR2(1) := 'N';
4039   l_mat_atp_info_rec             ATP_INFO_REC;
4040   l_atf_date_qty                 NUMBER;
4041   --4570421
4042   l_scaling_type                 NUMBER;
4043   l_scale_multiple               number;
4044   l_scale_rounding_variance      number;
4045   l_rounding_direction           number;
4046   l_component_yield_factor       NUMBER;
4047   l_lot_fail                     NUMBER := 0;
4048   l_usage_qty                    NUMBER; --4775920
4049   l_index                        NUMBER := 2; --5216528 as it points to substitutes/ 5216528
4050   x_substitutes_rec              MSC_ATP_REQ.get_subs_out_rec;-- 5216528/5216528
4051   l_item_info_rec                MSC_ATP_PVT.item_attribute_rec; --5147647
4052 
4053 
4054   -- ATP4drp Bug 3986053, 4052808
4055   -- Once the item changes the corresponding global data should change.
4056 --  l_item_info_rec                MSC_ATP_PVT.item_attribute_rec;
4057 BEGIN
4058 
4059     IF PG_DEBUG in ('Y', 'C') THEN
4060         msc_sch_wb.atp_debug('**********Begin Check_Substitutes Procedure************');
4061     END IF;
4062 
4063     IF PG_DEBUG in ('Y', 'C') THEN
4064         msc_sch_wb.atp_debug('********** INPUT DATA:Check_Substitutes **********');
4065         msc_sch_wb.atp_debug('Check_Substitutes: ' || 'p_parent_pegging_id: '|| to_char(p_parent_pegging_id));
4066         msc_sch_wb.atp_debug('Check_Substitutes: ' || 'p_instance_id: '|| to_char(p_instance_id));
4067         msc_sch_wb.atp_debug('Check_Substitutes: ' || 'p_scenario_id: '|| to_char(p_scenario_id));
4068         msc_sch_wb.atp_debug('Check_Substitutes: ' || 'p_level: '|| to_char(p_level));
4069         msc_sch_wb.atp_debug('Check_Substitutes: ' || 'p_search: '|| to_char(p_search));
4070         msc_sch_wb.atp_debug('Check_Substitutes: ' || 'p_plan_id: '|| to_char(p_plan_id));
4071         msc_sch_wb.atp_debug('Check_Substitutes: ' || 'p_inventory_item_id: '|| to_char(p_inventory_item_id));
4072         msc_sch_wb.atp_debug('Check_Substitutes: ' || 'p_organization_id: '|| to_char(p_organization_id));
4073         msc_sch_wb.atp_debug('Check_Substitutes: ' || 'p_quantity: '|| to_char(p_quantity));
4074     END IF;
4075   -- Loop through the substitutes and do a single level check for each of them
4076   -- If partial quantity is available, insert that into the details and also
4077   -- set the flag that the component is substitute so that UI can check that
4078   -- Keep decrementing the net_demand and exit if it is <= 0
4079   -- If the net_demand is still > 0, pass that back to the calling routing
4080   IF PG_DEBUG in ('Y', 'C') THEN
4081      msc_sch_wb.atp_debug('Check_Substitutes: ' || 'Opening the substitute cursor:' || to_char(MSC_ATP_PVT.G_SESSION_ID) || ':' ||
4082 	to_char(p_parent_pegging_id) || ':' || to_char(p_plan_id) || ':'
4083   	|| to_char(p_instance_id) || ':' || p_inventory_item_id  || ':' || p_atp_record.bill_seq_id); --4741012
4084   END IF;
4085   --bug3583705
4086   /*l_sysdate := MSC_ATP_FUNC.prev_work_day(p_atp_record.organization_id,
4087                              p_atp_record.instance_id,
4088                              sysdate);*/
4089   -- Note that p_quantity is the amount of the component B that is not yet
4090   -- fulfilled; B is the primary and B` the substitute component
4091   --               A
4092   --              / \
4093   --             B   B`
4094   -- We will resolve this quantity into remaining quantity for A that is needed and
4095 
4096   l_net_demand := p_quantity;
4097 
4098   --- store the summary flag so that the flag can be restored to the value with which we entered this
4099   --- module
4100   l_summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;
4101 
4102   /* rajjain 3008611 select component substitutes for which:
4103    * effective date is greater than or equal to greatest of PTF date, sysdate and component due date
4104    * disable date is less than or equal to greatest of PTF date, sysdate and component due date*/
4105   IF PG_DEBUG in ('Y', 'C') THEN
4106      msc_sch_wb.atp_debug('Check_Substitutes: ' || 'p_atp_record.requested_ship_date: ' || p_atp_record.requested_ship_date);
4107      msc_sch_wb.atp_debug('Check_Substitutes: ' || 'p_atp_record.shipping_cal_code: ' || p_atp_record.shipping_cal_code);
4108      msc_sch_wb.atp_debug('Check_Substitutes: ' || 'Date passed to cursor: ' ||
4109                                                 GREATEST(p_atp_record.requested_ship_date, sysdate, MSC_ATP_PVT.G_PTF_DATE));
4110   END IF;
4111 
4112   --5216528/5216528 Start First should be having the main item.
4113   x_substitutes_rec.inventory_item_id.EXTEND;
4114   x_substitutes_rec.pegging_id.EXTEND;
4115   x_substitutes_rec.sub_atp_qty.EXTEND;
4116   x_substitutes_rec.demand_id.EXTEND;
4117   x_substitutes_rec.atf_date_quantity.EXTEND; --5283809
4118   x_substitutes_rec.quantity_ordered.EXTEND;
4119   x_substitutes_rec.pf_item_id.EXTEND;
4120   --5216528/5216528 End
4121 
4122   OPEN substitute(GREATEST(p_atp_record.requested_ship_date, sysdate, MSC_ATP_PVT.G_PTF_DATE));
4123   LOOP
4124     FETCH substitute INTO  l_inv_item_id, l_substitute_id, l_usage, l_atp_flag,  --4570421
4125                         l_atp_comp_flag, l_primary_comp_usage, l_inv_item_name
4126                         --diag_atp
4127                         ,l_post_pro_lt, l_pre_pro_lt, l_variable_lt, l_fixed_lt,
4128                         l_weight_capacity, l_volume_capacity,
4129                         l_weight_uom, l_volume_uom, l_rounding_control_flag
4130                         -- time_phased_atp
4131                         ,l_pf_item_id, l_atf_date,
4132                          --4570421
4133                          l_scaling_type,
4134                          l_scale_multiple,
4135                          l_scale_rounding_variance,
4136                          l_rounding_direction ,
4137                          l_component_yield_factor,
4138                          l_usage_qty --4775920
4139                          ;
4140     EXIT WHEN substitute%NOTFOUND;
4141     /* Make an array of inventory_ids of the substitutes
4142        and return to ATP_Check to do a CTP on Substitutes.
4143     */
4144     if l_atp_comp_flag <> 'N' then
4145     --5216528/5216528 Start Insert the subtitutes
4146     x_substitutes_rec.inventory_item_id.EXTEND;
4147     x_substitutes_rec.pegging_id.EXTEND;
4148     x_substitutes_rec.sub_atp_qty.EXTEND;
4149     x_substitutes_rec.demand_id.EXTEND;
4150     x_substitutes_rec.atf_date_quantity.EXTEND; --5283809
4151     x_substitutes_rec.quantity_ordered.EXTEND;
4152     x_substitutes_rec.pf_item_id.EXTEND;
4153 
4154     x_substitutes_rec.inventory_item_id(l_index) :=   l_substitute_id;
4155     --5216528/5216528 End
4156     end if;
4157       IF PG_DEBUG in ('Y', 'C') THEN
4158          msc_sch_wb.atp_debug('Check_Substitutes: ' || '**Substitute:' || to_char(l_substitute_id) || ':'
4159               || to_char(l_usage) || ':' || l_atp_flag || ':' ||
4160                  to_char(l_primary_comp_usage));
4161          --4570421
4162          msc_sch_wb.atp_debug('Check_Substitutes: ' || 'l_scaling_type: ' || l_scaling_type);
4163          msc_sch_wb.atp_debug('Check_Substitutes: ' || 'l_scale_multiple: ' || l_scale_multiple);
4164          msc_sch_wb.atp_debug('Check_Substitutes: ' || 'l_scale_rounding_variance: ' || l_scale_rounding_variance);
4165          msc_sch_wb.atp_debug('Check_Substitutes: ' || 'l_rounding_direction: ' || l_rounding_direction);
4166          msc_sch_wb.atp_debug('Check_Substitutes: ' || 'l_component_yield_factor: ' || l_component_yield_factor);
4167          msc_sch_wb.atp_debug('Check_Substitutes: ' || 'l_usage_qty: ' || l_usage_qty);
4168 
4169 
4170       END IF;
4171       -- Setup new g_atp_record during each loop, we should not resue the
4172       -- original record, because we need the original one for CTP if there
4173       -- is not enough supply of components
4174 
4175       g_atp_record.error_code := MSC_ATP_PVT.ALLSUCCESS;
4176       g_atp_record.instance_id := p_atp_record.instance_id;
4177       g_atp_record.identifier := p_atp_record.identifier;
4178 
4179       --5147647, populating the global item info rec.
4180       MSC_ATP_PROC.get_global_item_info(p_atp_record.instance_id,
4181                                             p_plan_id,
4182                                             l_substitute_id, --sr_inventory_item_id
4183                                             p_atp_record.organization_id,
4184                                             l_item_info_rec );
4185 
4186       /* time_phased_atp
4187          To support PF ATP for components*/
4188       g_atp_record.inventory_item_id :=
4189                             MSC_ATP_PF.Get_PF_Atp_Item_Id(
4190                                 p_atp_record.instance_id,
4191                                 p_plan_id,
4192                                 l_substitute_id,
4193                                 p_atp_record.organization_id
4194                             );
4195       g_atp_record.request_item_id := l_substitute_id;
4196       -- time_phased_atp changes end
4197 
4198       g_atp_record.organization_id := p_atp_record.organization_id;
4199       --4570421 , here multiply by conversion rate
4200       IF ( ( MSC_ATP_PVT.G_ORG_INFO_REC.org_type = MSC_ATP_PVT.DISCRETE_ORG AND nvl(l_scaling_type,1) = 2) OR
4201            (MSC_ATP_PVT.G_ORG_INFO_REC.org_type = MSC_ATP_PVT.OPM_ORG AND nvl(l_scaling_type,1) IN (0,2))) then --Lot based ot Fixed Scaling
4202            --g_atp_record.quantity_ordered := l_net_demand
4203            --g_atp_record.quantity_ordered := l_usage/l_component_yield_factor; --4570421 , here multiply by conversion rate
4204            g_atp_record.quantity_ordered := l_usage; --4767982
4205       ELSIF (MSC_ATP_PVT.G_ORG_INFO_REC.org_type = MSC_ATP_PVT.OPM_ORG AND nvl(l_scaling_type,1) IN (4,5)) THEN  --Integer Scaling
4206             g_atp_record.quantity_ordered := integer_scaling (l_net_demand * l_usage,--4570421 , here multiply by conversion rate
4207                                              l_scale_multiple,
4208 	                                     l_scale_rounding_variance ,
4209 	                                     l_rounding_direction) ;
4210       ELSE
4211            g_atp_record.quantity_ordered := l_net_demand * l_usage; --4570421 , here multiply by conversion rate
4212       END IF; --4570421
4213       --g_atp_record.quantity_ordered := l_net_demand * l_usage; -- remaining qty --4570421
4214       g_atp_record.quantity_UOM := p_atp_record.quantity_UOM;
4215       g_atp_record.requested_ship_date := p_atp_record.requested_ship_date;
4216       g_atp_record.requested_arrival_date :=
4217                             p_atp_record.requested_arrival_date;
4218       g_atp_record.latest_acceptable_date :=
4219                             p_atp_record.latest_acceptable_date;
4220       g_atp_record.delivery_lead_time := p_atp_record.delivery_lead_time;
4221       g_atp_record.freight_carrier := p_atp_record.freight_carrier;
4222       g_atp_record.ship_method := p_atp_record.ship_method;
4223       g_atp_record.demand_class := p_atp_record.demand_class;
4224       g_atp_record.override_flag := p_atp_record.override_flag;
4225       g_atp_record.action := p_atp_record.action;
4226       g_atp_record.ship_date := p_atp_record.ship_date;
4227       g_atp_record.available_quantity := NULL;
4228       g_atp_record.requested_date_quantity :=
4229                             p_atp_record.requested_date_quantity;
4230       g_atp_record.supplier_id := NULL;
4231       g_atp_record.supplier_site_id := NULL;
4232       g_atp_record.insert_flag := p_atp_record.insert_flag;
4233       g_atp_record.order_number := p_atp_record.order_number;
4234 
4235       l_requested_ship_date := g_atp_record.requested_ship_date;
4236 
4237 
4238         IF PG_DEBUG in ('Y', 'C') THEN
4239            msc_sch_wb.atp_debug('Check_Substitutes: ' || 'Before calling subs atp info');
4240         END IF;
4241 
4242         -- bug 1665483:
4243         --IF (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 2) AND ((MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y')
4244         --        OR MSC_ATP_PVT.G_ALLOCATION_METHOD = 1) THEN
4245         IF (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y')  THEN
4246           g_atp_record.demand_class :=
4247           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(MSC_ATP_PVT.G_PARTNER_ID,
4248                            MSC_ATP_PVT.G_PARTNER_SITE_ID,
4249                            l_inv_item_id,
4250                            g_atp_record.organization_id,
4251                            g_atp_record.instance_id,
4252                            l_requested_ship_date,
4253                            NULL, -- level_id
4254                            g_atp_record.demand_class);
4255         END IF;
4256 
4257         /*
4258         -- New procedure for obtaining plan data : Supplier Capacity Lead Time (SCLT) proj.
4259         MSC_ATP_PROC.get_global_plan_info(g_atp_record.instance_id,
4260                                           g_atp_record.request_item_id,
4261                                           g_atp_record.organization_id,
4262                                           g_atp_record.demand_class);*/
4263 
4264         /* time_phased_atp changes begin
4265            Call new procedure Get_PF_Plan_Info*/
4266         MSC_ATP_PF.Get_PF_Plan_Info(
4267                g_atp_record.instance_id,
4268                g_atp_record.request_item_id,
4269                g_atp_record.inventory_item_id,
4270                g_atp_record.organization_id,
4271                g_atp_record.demand_class,
4272                g_atp_record.atf_date,
4273                g_atp_record.error_code,
4274                l_return_status,
4275                p_plan_id --bug3510475
4276         );
4277 
4278         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4279                 IF PG_DEBUG in ('Y', 'C') THEN
4280                         msc_sch_wb.atp_debug('Check_Substitutes: ' || 'Error encountered in call to Get_PF_Plan_Info');
4281                 END IF;
4282         END IF;
4283         /* time_phased_atp changes end*/
4284 
4285         l_plan_info_rec := MSC_ATP_PVT.G_PLAN_INFO_REC;
4286         -- End New procedure for obtaining plan data : Supplier Capacity Lead Time proj.
4287 
4288         l_plan_id       := l_plan_info_rec.plan_id;
4289         l_assign_set_id := l_plan_info_rec.assignment_set_id;
4290         -- changes for bug 2392456 ends
4291 
4292         -- 24x7
4293         IF (l_plan_id is NULL) or (l_plan_id IN (-100, -200)) THEN
4294             -- this should not happen but just in case
4295             l_plan_id := p_plan_id;
4296             /* time_phased_atp
4297                As we are using the same plan for substitutes set ATF date for substitute id*/
4298             g_atp_record.atf_date := l_atf_date;
4299         END IF;
4300 
4301         if (l_plan_id = -300) then
4302             IF PG_DEBUG in ('Y', 'C') THEN
4303                 msc_sch_wb.atp_debug('Check_Substitutes: ' || 'ATP Downtime Encountered');
4304             END IF;
4305             RAISE MSC_ATP_PVT.EXC_NO_PLAN_FOUND;
4306         end if;
4307 
4308         -- ATP4drp Product Family ATP not supported for DRP plans.
4309         IF MSC_ATP_PVT.G_PLAN_INFO_REC.plan_type = 5 THEN
4310            l_pf_atp := 'N';
4311            l_time_phased_atp := 'N';
4312            g_atp_record.atf_date := NULL;
4313            -- To handle case where family item id is different re-set it.
4314            g_atp_record.inventory_item_id := g_atp_record.request_item_id;
4315            l_pf_item_id := l_inv_item_id;
4316            IF PG_DEBUG in ('Y', 'C') THEN
4317               msc_sch_wb.atp_debug('----- ATP4drp Specific Debug Messages -----');
4318               msc_sch_wb.atp_debug('Check_Substitutes: ' || 'PF and Allocated ATP not applicable for DRP plans');
4319               msc_sch_wb.atp_debug('Check_Substitutes: ' || 'g_atp_record.inventory_item_id' ||
4320                                                                    g_atp_record.inventory_item_id);
4321               msc_sch_wb.atp_debug('Check_Substitutes: ' || 'g_atp_record.request_item_id' ||
4322                                                                    g_atp_record.request_item_id);
4323            END IF;
4324         ELSE
4325           -- time_phased_atp changes begin
4326           IF g_atp_record.atf_date is not null THEN
4327                 l_time_phased_atp := 'Y';
4328                 l_pf_atp := 'N';
4329           ELSE
4330                 l_time_phased_atp := 'N';
4331                 l_pf_atp := 'Y';
4332           END IF;
4333         END IF;
4334         -- Call Item Info Global Procedure to obtain Component Substitute
4335         -- Data into memory.
4336         -- ATP4drp Bug 3986053, 4052808
4337         -- Once the item changes the corresponding global data should change.
4338         MSC_ATP_PROC.get_global_item_info(g_atp_record.instance_id,
4339                                         --3917625: Read data from the plan
4340                                         -- -1,
4341                                         p_plan_id,
4342                                         g_atp_record.request_item_id,
4343                                         g_atp_record.organization_id,
4344                                         l_item_info_rec);
4345         -- End ATP4drp
4346         l_mat_atp_info_rec.instance_id                       := g_atp_record.instance_id;
4347         l_mat_atp_info_rec.plan_id                           := l_plan_id;
4348         l_mat_atp_info_rec.level                             := p_level + 1;
4349         l_mat_atp_info_rec.identifier                        := g_atp_record.identifier;
4350         l_mat_atp_info_rec.scenario_id                       := p_scenario_id;
4351         l_mat_atp_info_rec.inventory_item_id                 := g_atp_record.inventory_item_id;
4352         l_mat_atp_info_rec.request_item_id                   := g_atp_record.request_item_id;
4353         l_mat_atp_info_rec.organization_id                   := g_atp_record.organization_id;
4354         l_mat_atp_info_rec.requested_date                    := l_requested_ship_date;
4355         l_mat_atp_info_rec.quantity_ordered                  := g_atp_record.quantity_ordered;
4356         l_mat_atp_info_rec.demand_class                      := g_atp_record.demand_class;
4357         l_mat_atp_info_rec.insert_flag                       := g_atp_record.insert_flag;
4358         l_mat_atp_info_rec.rounding_control_flag             := l_get_mat_in_rec.rounding_control_flag;
4359         l_mat_atp_info_rec.dest_inv_item_id                  := l_get_mat_in_rec.dest_inv_item_id;
4360         l_mat_atp_info_rec.infinite_time_fence_date          := l_get_mat_in_rec.infinite_time_fence_date;
4361         l_mat_atp_info_rec.plan_name                         := l_get_mat_in_rec.plan_name;
4362         l_mat_atp_info_rec.optimized_plan                    := l_get_mat_in_rec.optimized_plan;
4363         l_mat_atp_info_rec.requested_date_quantity           := null;
4364         l_mat_atp_info_rec.atp_date_this_level               := null;
4365         l_mat_atp_info_rec.atp_date_quantity_this_level      := null;
4366         l_mat_atp_info_rec.substitution_window               := null;
4367         l_mat_atp_info_rec.atf_date                          := g_atp_record.atf_date; -- For time_phased_atp
4368         l_mat_atp_info_rec.refresh_number                    := p_refresh_number;   -- For summary enhancement
4369         l_mat_atp_info_rec.shipping_cal_code                 := p_atp_record.shipping_cal_code; -- Bug 3371817
4370 
4371         --4570421
4372         l_mat_atp_info_rec.scaling_type                      := p_atp_record.scaling_type;
4373         l_mat_atp_info_rec.scale_multiple                    := p_atp_record.scale_multiple;
4374         l_mat_atp_info_rec.scale_rounding_variance           := p_atp_record.scale_rounding_variance;
4375         l_mat_atp_info_rec.rounding_direction                := p_atp_record.rounding_direction;
4376         l_mat_atp_info_rec.component_yield_factor            := p_atp_record.component_yield_factor; --4570421
4377         l_mat_atp_info_rec.usage_qty                         := p_atp_record.usage_qty; --4775920
4378         l_mat_atp_info_rec.organization_type                 := p_atp_record.organization_type; --4775920
4379 
4380         MSC_ATP_REQ.Get_Material_Atp_Info(
4381                 l_mat_atp_info_rec,
4382                 g_atp_period,
4383                 g_atp_supply_demand,
4384                 l_return_status);
4385 
4386         l_requested_date_quantity                    := l_mat_atp_info_rec.requested_date_quantity;
4387         l_atf_date_qty                               := l_mat_atp_info_rec.atf_date_quantity;
4388         l_atp_date_this_level                        := l_mat_atp_info_rec.atp_date_this_level;
4389         l_atp_date_quantity_this_level               := l_mat_atp_info_rec.atp_date_quantity_this_level;
4390         l_get_mat_out_rec.atp_rule_name              := l_mat_atp_info_rec.atp_rule_name;
4391         l_get_mat_out_rec.infinite_time_fence_date   := l_mat_atp_info_rec.infinite_time_fence_date;
4392         p_atp_record.requested_date_quantity         := l_requested_date_quantity;
4393         -- time_phased_atp changes end
4394 
4395       -- BUG 2752227: Only substitutes with atp_flag in ('Y', 'C') are selected, don't need END IF.
4396       -- END IF;  -- end if atp_flag in ('Y', 'C')
4397 
4398       -- Normalize this demand to the primary component B
4399       --4570421
4400       IF ( ( MSC_ATP_PVT.G_ORG_INFO_REC.org_type = MSC_ATP_PVT.DISCRETE_ORG AND nvl(l_scaling_type,1) = 2) OR
4401            (MSC_ATP_PVT.G_ORG_INFO_REC.org_type = MSC_ATP_PVT.OPM_ORG AND nvl(l_scaling_type,1) IN (0,2))) then --Lot based ot Fixed Scaling
4402          IF ( l_requested_date_quantity < g_atp_record.quantity_ordered ) THEN
4403               l_net_demand := g_atp_record.quantity_ordered;
4404               l_lot_fail := 1;
4405          ElSE
4406               l_net_demand := 0;
4407          END IF;
4408          IF PG_DEBUG in ('Y', 'C') THEN
4409             msc_sch_wb.atp_debug('Check_Substitutes: Lot Based or Fixed Scaling ');
4410             msc_sch_wb.atp_debug('Check_Substitutes: l_lot_fail : '|| to_char(l_lot_fail));
4411          END IF;
4412       ELSIF (MSC_ATP_PVT.G_ORG_INFO_REC.org_type = MSC_ATP_PVT.OPM_ORG AND nvl(l_scaling_type,1) IN (4,5)) THEN  --Integer Scaling
4413         l_net_demand :=    (g_atp_record.quantity_ordered -
4414                             FLOOR(greatest(l_requested_date_quantity, 0)/l_scale_multiple) *l_scale_multiple);
4415         IF PG_DEBUG in ('Y', 'C') THEN
4416            msc_sch_wb.atp_debug('Check_Substitutes: Integer_scaling ');
4417         END IF;
4418       ELSE
4419       l_net_demand := (g_atp_record.quantity_ordered -
4420                             greatest(l_requested_date_quantity, 0)) *
4421                          (1/l_usage);
4422       END IF;
4423 
4424       /*l_net_demand := (g_atp_record.quantity_ordered -
4425                             greatest(l_requested_date_quantity, 0)) *
4426                          (1/l_usage); */
4427 
4428       -- print the net_demand
4429       IF PG_DEBUG in ('Y', 'C') THEN
4430          msc_sch_wb.atp_debug('Check_Substitutes: ' || 'l_net_demand : '|| to_char(l_net_demand));
4431       END IF;
4432 
4433       -- if we don't have atp for this sub component , don't bother
4434       -- generate pegging tree, demand record.
4435       /* --5373603 we need to add pegging
4436       IF (p_atp_record.requested_date_quantity  > 0 AND l_lot_fail = 0 ) --4570421
4437 	 OR MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1
4438       THEN
4439 
4440             -- prepare the insert record
4441             -- no matter it is a demand or not, we need to insert this
4442             -- record into database since it is a recursive procedure
4443             -- and we will rollback in ATP procedure if it is not a demand. */
4444 	IF p_atp_record.requested_date_quantity > 0 THEN
4445             l_atp_insert_rec.instance_id := g_atp_record.instance_id;
4446             -- time_phased_atp changes begin
4447             l_atp_insert_rec.inventory_item_id := l_pf_item_id;
4448             l_atp_insert_rec.request_item_id := l_inv_item_id;
4449             l_atp_insert_rec.atf_date_quantity := l_atf_date_qty;
4450             -- time_phased_atp end
4451             l_atp_insert_rec.organization_id := g_atp_record.organization_id;
4452             l_atp_insert_rec.identifier := g_atp_record.identifier;
4453             l_atp_insert_rec.demand_source_type:=
4454                           nvl(g_atp_record.demand_source_type, 2);
4455             l_atp_insert_rec.demand_source_header_id :=
4456                             nvl(g_atp_record.demand_source_header_id, -1);
4457             l_atp_insert_rec.demand_source_delivery :=
4458                             g_atp_record.demand_source_delivery;
4459             -- bug 1279984: we only demand the quantity that we have
4460             l_atp_insert_rec.quantity_ordered:= LEAST(
4461                                           p_atp_record.requested_date_quantity,
4462                                           g_atp_record.quantity_ordered);
4463             l_atp_insert_rec.requested_ship_date := l_requested_ship_date;
4464             l_atp_insert_rec.demand_class := g_atp_record.demand_class;
4465             l_atp_insert_rec.refresh_number := p_refresh_number; -- summary enhancement g_atp_record.refresh_number;
4466             l_atp_insert_rec.order_number := g_atp_record.order_number;
4467             -- ATP4drp Component Subst. we are dealing with a component
4468             -- then set the origination type to Constrained Kit Demand
4469             -- for DRP plans.
4470             IF NVL(MSC_ATP_PVT.G_PLAN_INFO_REC.plan_type,1) = 5 THEN
4471                 l_atp_insert_rec.origination_type := 47;
4472                 IF PG_DEBUG in ('Y', 'C') THEN
4473                    msc_sch_wb.atp_debug('Check_Substitutes: ' || 'DRP origination_type ='|| l_atp_insert_rec.origination_type);
4474                    msc_sch_wb.atp_debug('----- ATP4drp Specific Debug Messages -----');
4475                 END IF;
4476             ELSE
4477                 l_atp_insert_rec.origination_type := 1;
4478             END IF;
4479             -- End ATP4drp
4480 
4481 	    MSC_ATP_DB_UTILS.Add_Mat_Demand(l_atp_insert_rec,
4482                               l_plan_id,
4483                               0,
4484                               l_demand_id);
4485 	END IF;
4486 
4487             -- populate insert rec to pegging tree for this demand
4488 
4489             -- for performance reason, we call these function here and
4490             -- then populate the pegging tree with the values
4491 
4492             /* Modularize Item and Org Info */
4493             MSC_ATP_PROC.get_global_org_info (g_atp_record.instance_id,
4494                                               g_atp_record.organization_id);
4495             l_org_code := MSC_ATP_PVT.G_ORG_INFO_REC.org_code;
4496             /*Modularize Item and Org Info */
4497 
4498 
4499             l_pegging_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
4500             l_pegging_rec.order_line_id:= MSC_ATP_PVT.G_ORDER_LINE_ID;
4501             l_pegging_rec.parent_pegging_id:= p_parent_pegging_id;
4502             l_pegging_rec.atp_level:= p_level;
4503             l_pegging_rec.organization_id:= g_atp_record.organization_id;
4504             l_pegging_rec.organization_code := l_org_code;
4505             l_pegging_rec.identifier1:= g_atp_record.instance_id;
4506             l_pegging_rec.identifier2 := l_plan_id;
4507             l_pegging_rec.identifier3 := l_demand_id;
4508             --4570421
4509             l_pegging_rec.scaling_type                      := l_scaling_type;
4510             l_pegging_rec.scale_multiple                    := l_scale_multiple;
4511             l_pegging_rec.scale_rounding_variance           := l_scale_rounding_variance;
4512             l_pegging_rec.rounding_direction                := l_rounding_direction;
4513             l_pegging_rec.component_yield_factor            := l_component_yield_factor;
4514             l_pegging_rec.usage                             := l_usage_qty; --4775920
4515             l_pegging_rec.organization_type                 := NVL ( MSC_ATP_PVT.G_ORG_INFO_REC.org_type, MSC_ATP_PVT.DISCRETE_ORG); --4775920
4516             --4570421
4517 
4518             -- time_phased_atp changes begin
4519             IF l_pf_atp = 'Y' THEN
4520                     l_pegging_rec.inventory_item_id:= g_atp_record.inventory_item_id;
4521             ELSE
4522                     l_pegging_rec.inventory_item_id:= g_atp_record.request_item_id;
4523             END IF;
4524             l_pegging_rec.request_item_id:= g_atp_record.request_item_id;
4525             l_pegging_rec.aggregate_time_fence_date := g_atp_record.atf_date;
4526             -- time_phased_atp changes end
4527 
4528             l_pegging_rec.inventory_item_name := l_inv_item_name;
4529             l_pegging_rec.resource_id := NULL;
4530             l_pegging_rec.resource_code := NULL;
4531             l_pegging_rec.department_id := NULL;
4532             l_pegging_rec.department_code := NULL;
4533             l_pegging_rec.supplier_id := NULL;
4534             l_pegging_rec.supplier_name := NULL;
4535             l_pegging_rec.supplier_site_id := NULL;
4536             l_pegging_rec.supplier_site_name := NULL;
4537             l_pegging_rec.scenario_id:= p_scenario_id;
4538             l_pegging_rec.supply_demand_source_type:= 1;  -- cchen 08/31
4539             -- bug 1279984: we only demand the quantity that we have
4540 
4541 	    -- dsting diag_atp only add demand for available quantity for component substitute
4542 	    l_pegging_rec.supply_demand_quantity:= LEAST(
4543                                           p_atp_record.requested_date_quantity,
4544                                           g_atp_record.quantity_ordered);
4545             l_pegging_rec.supply_demand_type:= 1;
4546             l_pegging_rec.supply_demand_date:= l_requested_ship_date;
4547             l_pegging_rec.number1 := 1;
4548 
4549 	    -- dsting ATO 2465370
4550 	    l_pegging_rec.required_date := l_requested_ship_date;
4551             --bug 3328421: comp subst si supported only for backward ATP. Therefroe we always store req_date
4552             l_pegging_rec.actual_supply_demand_date := l_requested_ship_date;
4553 
4554             -- for demo:1153192
4555             l_pegging_rec.constraint_flag := 'N';
4556 	    l_pegging_rec.component_identifier :=
4557 			NVL(p_atp_record.component_identifier, MSC_ATP_PVT.G_COMP_LINE_ID);
4558             l_pegging_rec.summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;
4559 
4560             --diag_atp
4561 	    l_pegging_rec.pegging_type := MSC_ATP_PVT.ORG_DEMAND; -- demand node
4562             l_pegging_rec.constraint_type := NULL;
4563             --s_cto_rearch
4564             l_pegging_rec.dest_inv_item_id := l_pf_item_id;
4565 
4566             MSC_ATP_DB_UTILS.Add_Pegging(l_pegging_rec, l_pegging_id);
4567 
4568             l_pegging_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
4569             l_pegging_rec.order_line_id:= MSC_ATP_PVT.G_ORDER_LINE_ID;
4570             l_pegging_rec.parent_pegging_id:= l_pegging_id;
4571             l_pegging_rec.atp_level:= p_level + 1;
4572             l_pegging_rec.organization_id:= g_atp_record.organization_id;
4573             l_pegging_rec.organization_code:= l_org_code;
4574             l_pegging_rec.identifier1:= g_atp_record.instance_id;
4575             l_pegging_rec.identifier2 := l_plan_id;
4576             l_pegging_rec.identifier3 := NULL;
4577             l_pegging_rec.inventory_item_id:= g_atp_record.inventory_item_id;
4578             l_pegging_rec.inventory_item_name := l_inv_item_name;
4579 
4580             -- time_phased_atp changes begin
4581             IF l_time_phased_atp = 'Y' and l_requested_ship_date <= g_atp_record.atf_date THEN
4582                     l_pegging_rec.inventory_item_id:= g_atp_record.request_item_id;
4583                     l_pegging_rec.inventory_item_name := g_atp_record.request_item_name;
4584             ELSE
4585                     l_pegging_rec.inventory_item_id:= p_atp_record.inventory_item_id;
4586                     l_pegging_rec.inventory_item_name := l_inv_item_name;
4587             END IF;
4588             l_pegging_rec.request_item_id:= g_atp_record.request_item_id;
4589             -- time_phased_atp changes end
4590 
4591             l_pegging_rec.resource_id := NULL;
4592             l_pegging_rec.resource_code := NULL;
4593             l_pegging_rec.department_id := NULL;
4594             l_pegging_rec.department_code := NULL;
4595             l_pegging_rec.supplier_id := NULL;
4596             l_pegging_rec.supplier_name := NULL;
4597             l_pegging_rec.supplier_site_id := NULL;
4598             l_pegging_rec.supplier_site_name := NULL;
4599             l_pegging_rec.scenario_id:= p_scenario_id;
4600             l_pegging_rec.supply_demand_source_type:= MSC_ATP_PVT.ATP;
4601             l_pegging_rec.supply_demand_quantity:=
4602                             p_atp_record.requested_date_quantity;
4603             l_pegging_rec.supply_demand_date:= l_requested_ship_date;
4604             l_pegging_rec.supply_demand_type:= 2;
4605             l_pegging_rec.source_type := 0;
4606             l_pegging_rec.component_identifier :=
4607 			 NVL(p_atp_record.component_identifier, MSC_ATP_PVT.G_COMP_LINE_ID);
4608 
4609             -- for demo:1153192
4610             IF  (p_search = 1) AND
4611 	       ( g_atp_record.quantity_ordered >= l_requested_date_quantity)
4612 	    THEN
4613                   l_pegging_rec.constraint_flag := 'Y';
4614             ELSE
4615                   l_pegging_rec.constraint_flag := 'N';
4616 
4617             END IF;
4618 
4619             l_pegging_rec.pegging_type := MSC_ATP_PVT.ATP_SUPPLY; ---atp supply node
4620             l_pegging_rec.summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;
4621              -- Bug 3826234
4622             l_pegging_rec.manufacturing_cal_code :=  p_atp_record.manufacturing_cal_code;
4623 
4624             MSC_ATP_DB_UTILS.Add_Pegging(l_pegging_rec, l_atp_pegging_id);
4625 
4626             -- Add pegging_id to the l_atp_period and l_atp_supply_demand
4627 
4628             FOR i in 1..g_atp_period.Level.COUNT LOOP
4629                 g_atp_period.Pegging_Id(i) := l_atp_pegging_id;
4630                 g_atp_period.End_Pegging_Id(i) := MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
4631             END LOOP;
4632 
4633 	    IF p_atp_record.insert_flag <> 0 THEN
4634 	   	MSC_ATP_DB_UTILS.move_SD_temp_into_mrp_details(l_atp_pegging_id,
4635 				MSC_ATP_PVT.G_DEMAND_PEGGING_ID);
4636    	    END IF;
4637 
4638 
4639             MSC_ATP_PROC.Details_Output(g_atp_period,
4640        			     g_atp_supply_demand,
4641                              l_atp_period,
4642                              l_supply_demand,
4643                              l_return_status);
4644 
4645       --END IF;  -- IF p_atp_record.requested_date_quantity > 0 --5373603
4646        if l_atp_comp_flag <> 'N' then
4647                --5216528/5216528 insert the remaining values.
4648                x_substitutes_rec.pegging_id(l_index) := l_pegging_id;
4649                x_substitutes_rec.sub_atp_qty(l_index) :=  l_pegging_rec.supply_demand_quantity;
4650                x_substitutes_rec.demand_id(l_index) := l_demand_id;
4651                x_substitutes_rec.pf_item_id(l_index) := l_pf_item_id; --5283809
4652                x_substitutes_rec.atf_date_quantity(l_index) := l_atf_date_qty; --5283809
4653                x_substitutes_rec.quantity_ordered(l_index) := g_atp_record.quantity_ordered; --5283809
4654                l_index := l_index + 1;
4655        end if;
4656       --4570421
4657       l_lot_fail := 0; --resetting the variable to zero
4658 
4659       IF (l_net_demand <= 0) then
4660           EXIT;
4661       END IF;
4662   END LOOP;
4663          l_substitutes_rec := x_substitutes_rec;
4664   CLOSE substitute;
4665   MSC_ATP_PVT.G_SUMMARY_FLAG := l_summary_flag;
4666 EXCEPTION
4667   -- 24x7
4668   WHEN MSC_ATP_PVT.EXC_NO_PLAN_FOUND THEN
4669      IF PG_DEBUG in ('Y', 'C') THEN
4670            msc_sch_wb.atp_debug ('Check Substitutes : Plan Downtime encountered');
4671      END IF;
4672      MSC_ATP_PVT.G_DOWNTIME_HIT := 'Y';
4673      MSC_ATP_PVT.G_SUMMARY_FLAG := l_summary_flag;
4674      l_return_status := FND_API.G_RET_STS_ERROR;
4675      CLOSE substitute;
4676      RAISE MSC_ATP_PVT.EXC_NO_PLAN_FOUND;
4677 
4678   WHEN MSC_ATP_PVT.NO_MATCHING_DATE_IN_CAL THEN --bug3583705
4679      IF PG_DEBUG in ('Y', 'C') THEN
4680            msc_sch_wb.atp_debug ('Check Substitutes : NO_MATCHING_DATE_IN_CAL');
4681      END IF;
4682      MSC_ATP_PVT.G_SUMMARY_FLAG := l_summary_flag;
4683      l_return_status := FND_API.G_RET_STS_ERROR;
4684      CLOSE substitute;
4685      RAISE MSC_ATP_PVT.NO_MATCHING_DATE_IN_CAL;
4686 
4687   WHEN OTHERS THEN
4688     MSC_ATP_PVT.G_SUMMARY_FLAG := l_summary_flag;
4689     l_return_status := FND_API.G_RET_STS_ERROR;
4690     CLOSE substitute;
4691     return;
4692 END Check_Substitutes;
4693 
4694 /* time_phased_atp
4695    Grouped various input parameters to this procedure in a new record Atp_Info_Rec*/
4696 PROCEDURE Get_Material_Atp_Info (
4697     p_mat_atp_info_rec      IN OUT  NOCOPY Atp_Info_Rec,
4698     x_atp_period            OUT     NOCOPY MRP_ATP_PUB.ATP_Period_Typ,
4699     x_atp_supply_demand     OUT     NOCOPY MRP_ATP_PUB.ATP_Supply_Demand_Typ,
4700     x_return_status         OUT     NoCopy VARCHAR2
4701 )
4702 IS
4703 l_infinite_time_fence_date	DATE;
4704 l_sysdate_seq_num               NUMBER;
4705 l_requested_date                DATE;
4706 --l_atp_mat_atp_info_rec.requested_date            DATE; -- time_phased_atp
4707 i 				PLS_INTEGER := 1;
4708 l_atp_period_tab 		MRP_ATP_PUB.date_arr:=MRP_ATP_PUB.date_arr();
4709 l_atp_qty_tab 			MRP_ATP_PUB.number_arr:=MRP_ATP_PUB.number_arr();
4710 --l_next_period			DATE; -- time_phased_atp
4711 l_return_status			VARCHAR2(1);
4712 g_atp_record                    MRP_ATP_PVT.AtpRec;
4713 l_net_demand                    number;
4714 g_quantity_ordered              number;
4715 my_sqlcode			NUMBER;
4716 tmp1				DATE;
4717 tmp2				NUMBER;
4718 temp				NUMBER;
4719 l_default_atp_rule_id           NUMBER;
4720 l_calendar_code                 VARCHAR2(14);
4721 l_calendar_exception_set_id     NUMBER;
4722 l_default_demand_class          VARCHAR2(34);
4723 l_atp_info            		MRP_ATP_PVT.ATP_Info;
4724 l_pre_process_date		DATE;
4725 l_processing_lead_time		NUMBER;
4726 l_sysdate			DATE;
4727 l_sys_next_date                 DATE;
4728 l_sys_next_osc_date             DATE; -- Bug 3371817
4729 --l_round_flag			NUMBER; -- time_phased_atp
4730 l_Summary_atp 		        VARCHAR(1);
4731 --bug 2152184
4732 l_request_item_id               NUMBER;
4733 
4734 L_SUBST_LIMIT_DATE              DATE;
4735 
4736 
4737 l_org_code                      VARCHAR2(7);
4738 
4739  --diag_atp
4740   L_GET_MAT_IN_REC               MSC_ATP_REQ.GET_MAT_IN_REC;
4741 
4742 -- for summary enhancement
4743 l_summary_flag                  NUMBER := -1;  -- Bug 3813302 - Initiallize the variable.
4744 
4745 -- time_phased_atp
4746 l_time_phased_atp               VARCHAR2(1) := 'N';
4747 
4748 BEGIN
4749 
4750     IF PG_DEBUG in ('Y', 'C') THEN
4751         msc_sch_wb.atp_debug('**********Begin Get_Material_Atp_Info Procedure************');
4752     END IF;
4753 
4754     -- initialize API return status to success
4755     x_return_status := FND_API.G_RET_STS_SUCCESS;
4756 
4757     IF PG_DEBUG in ('Y', 'C') THEN
4758         msc_sch_wb.atp_debug('********** INPUT DATA:Get_Material_Atp_Info **********');
4759         msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'p_mat_atp_info_rec.inventory_item_id: '|| to_char(p_mat_atp_info_rec.inventory_item_id));
4760         msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'p_mat_atp_info_rec.request_item_id: '|| to_char(p_mat_atp_info_rec.request_item_id));
4761         msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'p_mat_atp_info_rec.organization_id: '|| to_char(p_mat_atp_info_rec.organization_id));
4762         msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'p_mat_atp_info_rec.requested_date: '|| to_char(p_mat_atp_info_rec.requested_date));
4763         msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'p_mat_atp_info_rec.instance_id: '|| to_char(p_mat_atp_info_rec.instance_id));
4764         msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'p_mat_atp_info_rec.plan_id: '|| to_char(p_mat_atp_info_rec.plan_id));
4765         msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'p_mat_atp_info_rec.quantity_ordered: '|| to_char(p_mat_atp_info_rec.quantity_ordered));
4766         msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'p_mat_atp_info_rec.insert_flag: '|| to_char(p_mat_atp_info_rec.insert_flag));
4767         msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'p_mat_atp_info_rec.demand_class: '|| p_mat_atp_info_rec.demand_class);
4768         msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'p_mat_atp_info_rec.substitution_window := '|| p_mat_atp_info_rec.substitution_window);
4769         msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'p_mat_atp_info_rec.atf_date: '|| p_mat_atp_info_rec.atf_date);
4770         msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'p_mat_atp_info_rec.shipping_cal_code: '|| p_mat_atp_info_rec.shipping_cal_code);
4771     END IF;
4772      /* time_phased_atp changes begin*/
4773      IF (MSC_ATP_PVT.G_INV_CTP = 4)
4774         and (p_mat_atp_info_rec.inventory_item_id <> p_mat_atp_info_rec.request_item_id)
4775             and (p_mat_atp_info_rec.atf_date is null)
4776      THEN
4777          l_request_item_id := p_mat_atp_info_rec.inventory_item_id;
4778      ELSIF (MSC_ATP_PVT.G_INV_CTP = 4)
4779         and (p_mat_atp_info_rec.inventory_item_id <> p_mat_atp_info_rec.request_item_id)
4780             and (p_mat_atp_info_rec.atf_date is not null)
4781      THEN
4782          l_request_item_id := p_mat_atp_info_rec.request_item_id;
4783          l_time_phased_atp := 'Y';
4784      ELSE
4785          l_request_item_id := p_mat_atp_info_rec.request_item_id;
4786      END IF;
4787 
4788      IF PG_DEBUG in ('Y', 'C') THEN
4789         msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'l_request_item_id := ' || l_request_item_id);
4790         msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'Time Phased ATP = ' || l_time_phased_atp);
4791      END IF;
4792      /* time_phased_atp changes end*/
4793 
4794     -- get the infinite time fence date if it exists
4795     /*l_infinite_time_fence_date := MSC_ATP_FUNC.get_infinite_time_fence_date(p_mat_atp_info_rec.instance_id,
4796         p_mat_atp_info_rec.inventory_item_id,p_mat_atp_info_rec.organization_id, p_mat_atp_info_rec.plan_id); */
4797     --diag_atp
4798     MSC_ATP_PROC.get_infinite_time_fence_date(p_mat_atp_info_rec.instance_id,
4799                                               p_mat_atp_info_rec.inventory_item_id,
4800                                               p_mat_atp_info_rec.organization_id,
4801                                               p_mat_atp_info_rec.plan_id,
4802                                               l_infinite_time_fence_date,
4803                                               p_mat_atp_info_rec.atp_rule_name);
4804 
4805     p_mat_atp_info_rec.infinite_time_fence_date := l_infinite_time_fence_date;
4806     l_get_mat_in_rec.infinite_time_fence_date := l_infinite_time_fence_date;
4807     IF PG_DEBUG in ('Y', 'C') THEN
4808         msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || ' ATP Rule := ' || p_mat_atp_info_rec.atp_rule_name);
4809     END IF;
4810 
4811     -- 2859130 get if this is a constrained plan or not
4812     IF p_mat_atp_info_rec.plan_id <> -1 THEN
4813         BEGIN
4814             SELECT  DECODE(plans.plan_type, 4, 2,
4815                         DECODE(daily_material_constraints, 1, 1,
4816                             DECODE(daily_resource_constraints, 1, 1,
4817                                 DECODE(weekly_material_constraints, 1, 1,
4818                                     DECODE(weekly_resource_constraints, 1, 1,
4819                                         DECODE(period_material_constraints, 1, 1,
4820                                             DECODE(period_resource_constraints, 1, 1, 2)
4821                                               )
4822                                           )
4823                                       )
4824                                   )
4825                               )
4826                           ),
4827                     summary_flag
4828             INTO    MSC_ATP_PVT.G_OPTIMIZED_PLAN, l_summary_flag    -- For summary enhancement
4829             FROM    msc_plans plans
4830             WHERE   plans.plan_id = p_mat_atp_info_rec.plan_id;
4831         EXCEPTION WHEN NO_DATA_FOUND THEN
4832             MSC_ATP_PVT.G_OPTIMIZED_PLAN := 2;
4833         END;
4834     END IF;
4835     l_get_mat_in_rec.optimized_plan := MSC_ATP_PVT.G_OPTIMIZED_PLAN; -- 2859130
4836 
4837     IF PG_DEBUG in ('Y', 'C') THEN
4838         msc_sch_wb.atp_debug('Optimized plan: ' || MSC_ATP_PVT.G_OPTIMIZED_PLAN);
4839     END IF;
4840 
4841     --- bug 1819638:  get next working day.
4842 
4843     -- msc_calendar.select_calendar_defaults(p_mat_atp_info_rec.organization_id,p_mat_atp_info_rec.instance_id,
4844     --          l_calendar_code, l_exception_set_id);
4845     /* Modularize Item and Org Info */
4846     -- changed call, re-use info already obtained.
4847     MSC_ATP_PROC.get_global_org_info(p_mat_atp_info_rec.instance_id, p_mat_atp_info_rec.organization_id);
4848     l_default_atp_rule_id := MSC_ATP_PVT.G_ORG_INFO_REC.default_atp_rule_id;
4849     l_calendar_code := MSC_ATP_PVT.G_ORG_INFO_REC.cal_code;
4850     l_calendar_exception_set_id := MSC_ATP_PVT.G_ORG_INFO_REC.cal_exception_set_id;
4851     l_default_demand_class := MSC_ATP_PVT.G_ORG_INFO_REC.default_demand_class;
4852     l_org_code := MSC_ATP_PVT.G_ORG_INFO_REC.org_code;
4853     /*Modularize Item and Org Info */
4854 
4855 
4856     /************ Bug 1510853 ATP Rule Check ************/
4857     IF (MSC_ATP_PVT.G_INV_CTP = 5) AND
4858         (l_default_atp_rule_id IS NULL) AND    -- no default rule at org level
4859         (MSC_ATP_PVT.G_ATP_RULE_FLAG = 'N') THEN
4860         IF PG_DEBUG in ('Y', 'C') THEN
4861             msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || ' RAISING EXCEPTION for NO ATP RULE ');
4862         END IF;
4863         RAISE MSC_ATP_PVT.EXC_NO_ATP_RULE;  -- ATPeable item has no rule
4864     ELSE
4865         IF PG_DEBUG in ('Y', 'C') THEN
4866             msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'ATP RULE EXISTS at Item or Org level ' ||
4867                 'ORG LEVEL Default Rule: '||l_default_atp_rule_id);
4868         END IF;
4869         MSC_ATP_PVT.G_ATP_RULE_FLAG := 'Y';  -- Item has an applicable ATP rule
4870     END IF;
4871     /************ Bug 1510853 ATP Rule Check ************/
4872 
4873     --- Write SQL statement so that we do not need to
4874     --- make two calls to database for l_sysdate_seq_num and l_sys_next_date
4875 
4876     BEGIN
4877         SELECT  cal.next_seq_num, cal.next_date
4878         INTO    l_sysdate_seq_num, l_sys_next_date
4879         FROM    msc_calendar_dates  cal
4880         WHERE   cal.exception_set_id = l_calendar_exception_set_id
4881         AND     cal.calendar_code = l_calendar_code
4882         AND     cal.calendar_date = TRUNC(sysdate)
4883         AND     cal.sr_instance_id = p_mat_atp_info_rec.instance_id ;
4884 
4885         -- Bug 3371817 - Calculate OSC sysdate
4886         IF p_mat_atp_info_rec.shipping_cal_code = l_calendar_code THEN
4887             -- OMC and OSC are same or looking at components for make case
4888             l_sys_next_osc_date := l_sys_next_date;
4889         ELSE
4890             l_sys_next_osc_date := MSC_CALENDAR.NEXT_WORK_DAY(
4891                                     p_mat_atp_info_rec.shipping_cal_code,
4892                                     p_mat_atp_info_rec.instance_id,
4893                                     TRUNC(sysdate));
4894         END IF;
4895         l_get_mat_in_rec.sys_next_osc_date :=  l_sys_next_osc_date; --bug3333114
4896     EXCEPTION
4897         WHEN OTHERS THEN
4898             RAISE MSC_ATP_PVT.NO_MATCHING_DATE_IN_CAL;
4899     END;
4900 
4901     IF PG_DEBUG in ('Y', 'C') THEN
4902         msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'l_sysdate_seq_num = ' || l_sysdate_seq_num);
4903         msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'l_sys_next_date = ' || l_sys_next_date);
4904         msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'l_sys_next_osc_date = ' || l_sys_next_osc_date);
4905     END IF;
4906     -- in case we want to support flex date
4907     l_requested_date := p_mat_atp_info_rec.requested_date;
4908 
4909     IF PG_DEBUG in ('Y', 'C') THEN
4910         msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'l_infinite_time_fence_date: '|| to_char(l_infinite_time_fence_date));
4911         msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'l_requested_date: '|| to_char(p_mat_atp_info_rec.requested_date));
4912     END IF;
4913 
4914     IF (l_requested_date >=
4915         NVL(l_infinite_time_fence_date, l_requested_date +1)) THEN
4916         -- requested date outside the infinite time fence.  no need to do
4917         -- the actual atp check.
4918        MSC_ATP_DB_UTILS.Clear_SD_Details_temp(); --bug 4618369
4919         p_mat_atp_info_rec.requested_date_quantity := MSC_ATP_PVT.INFINITE_NUMBER;
4920         p_mat_atp_info_rec.atp_date_quantity_this_level := MSC_ATP_PVT.INFINITE_NUMBER;
4921         p_mat_atp_info_rec.atp_date_this_level := l_requested_date;
4922 
4923     ELSE
4924 
4925         -- Check if full summary has been run - for summary enhancement
4926         IF MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y' AND
4927             l_summary_flag NOT IN (MSC_POST_PRO.G_SF_SUMMARY_NOT_RUN, MSC_POST_PRO.G_SF_PREALLOC_COMPLETED,
4928                                    MSC_POST_PRO.G_SF_FULL_SUMMARY_RUNNING) THEN
4929             -- Summary SQL can be used
4930             MSC_ATP_PVT.G_SUMMARY_SQL := 'Y';
4931         ELSE
4932             -- Use the SQL for non summary case
4933             MSC_ATP_PVT.G_SUMMARY_SQL := 'N';
4934         END IF;
4935 
4936         -- we need to have a branch here for allocated atp
4937         IF (MSC_ATP_PVT.G_ALLOCATED_ATP = 'N') THEN
4938             -- if we need the detail information, we will get individual s/d rows
4939             -- and do the sum ourselves.  if we don't need the detail infomation,
4940             -- we will do a group by and select the sum in the sql statement.
4941             IF PG_DEBUG in ('Y', 'C') THEN
4942                 msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'MSC_ATP_PVT.G_SUMMARY_FLAG := ' || MSC_ATP_PVT.G_SUMMARY_FLAG);
4943             END IF;
4944 
4945             -- 2859130 replace sql stmts with calls to procedures containing the sql stmts
4946             IF nvl(p_mat_atp_info_rec.insert_flag, 0) = 0 THEN
4947                 IF p_mat_atp_info_rec.inventory_item_id = l_request_item_id  THEN
4948                     get_mat_avail(
4949                             MSC_ATP_PVT.G_SUMMARY_SQL, -- MSC_ATP_PVT.G_SUMMARY_FLAG, -- changed for summary enhancement
4950                             MSC_ATP_PVT.G_OPTIMIZED_PLAN,
4951                             p_mat_atp_info_rec.inventory_item_id,
4952                             p_mat_atp_info_rec.organization_id,
4953                             p_mat_atp_info_rec.instance_id,
4954                             p_mat_atp_info_rec.plan_id,
4955                             l_calendar_code,
4956                             l_calendar_exception_set_id,
4957                             l_sysdate_seq_num,
4958                             l_sys_next_date,
4959                          -- l_default_atp_rule_id,
4960                          -- p_mat_atp_info_rec.demand_class,
4961                             p_mat_atp_info_rec.demand_class, --Sequence of parameters changed as a part of cmro changes
4962                             l_default_atp_rule_id,--to correct them
4963                             l_default_demand_class,
4964                             l_infinite_time_fence_date,
4965                             p_mat_atp_info_rec.refresh_number,           -- For summary enhancement
4966                             l_atp_period_tab,
4967                             l_atp_qty_tab
4968                     );
4969                 ELSE
4970                     -- time_phased_atp
4971                     MSC_ATP_PF.Get_Mat_Avail_Pf(
4972                             MSC_ATP_PVT.G_SUMMARY_SQL, -- MSC_ATP_PVT.G_SUMMARY_FLAG, -- changed for summary enhancement
4973                             p_mat_atp_info_rec.inventory_item_id,
4974                             p_mat_atp_info_rec.request_item_id,
4975                             p_mat_atp_info_rec.organization_id,
4976                             p_mat_atp_info_rec.instance_id,
4977                             p_mat_atp_info_rec.plan_id,
4978                             l_calendar_code,
4979                             l_sysdate_seq_num,
4980                             l_sys_next_date,
4981                             p_mat_atp_info_rec.demand_class,
4982                             l_default_atp_rule_id,
4983                             l_default_demand_class,
4984                             l_infinite_time_fence_date,
4985                             p_mat_atp_info_rec.refresh_number,           -- For summary enhancement
4986                             l_atp_period_tab,
4987                             l_atp_qty_tab,
4988                             l_return_status
4989                     );
4990                     IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4991                            IF PG_DEBUG in ('Y', 'C') THEN
4992                                 msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'Error occured in procedure Get_Mat_Avail_Pf');
4993                            END IF;
4994                            RAISE FND_API.G_EXC_ERROR;
4995                     END IF;
4996                 END IF;
4997 
4998                 IF PG_DEBUG in ('Y', 'C') THEN
4999                     msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'after getting netted qtys');
5000                 END IF;
5001 
5002                 -- time_phased_atp
5003                 IF l_time_phased_atp = 'Y' THEN
5004                     MSC_ATP_PF.pf_atp_consume(
5005                            l_atp_qty_tab,
5006                            l_return_status,
5007                            l_atp_period_tab,
5008                            MSC_ATP_PF.Bw_Fw_Cum, --b/w, f/w consumption and accumulation
5009                            p_mat_atp_info_rec.atf_date);
5010                     IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5011                            IF PG_DEBUG in ('Y', 'C') THEN
5012                                 msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'Error occured in procedure Pf_Atp_Consume');
5013                            END IF;
5014                            RAISE FND_API.G_EXC_ERROR;
5015                     END IF;
5016                 ELSE
5017                     MSC_ATP_PROC.atp_consume(l_atp_qty_tab, l_atp_qty_tab.count);
5018                 END IF;
5019                 IF PG_DEBUG in ('Y', 'C') THEN
5020                     msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'after atp_consume');
5021                 END IF;
5022 
5023                 /* Cum drop issue changes begin*/
5024                 MSC_AATP_PROC.Atp_Remove_Negatives(l_atp_qty_tab, l_return_status);
5025                 IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5026                        IF PG_DEBUG in ('Y', 'C') THEN
5027                           msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'Error occured in procedure Atp_Remove_Negatives');
5028                        END IF;
5029                        RAISE FND_API.G_EXC_ERROR;
5030                 END IF;
5031                 /* Cum drop issue changes end*/
5032 
5033                 IF l_infinite_time_fence_date IS NOT NULL THEN
5034                     -- add one more entry to indicate infinite time fence date
5035                     -- and quantity.
5036                     IF PG_DEBUG in ('Y', 'C') THEN
5037                         msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'adding itf');
5038                     END IF;
5039                     l_atp_period_tab.EXTEND;
5040                     l_atp_qty_tab.EXTEND;
5041                     l_atp_period_tab(l_atp_period_tab.count) := l_infinite_time_fence_date;
5042                     l_atp_qty_tab(l_atp_qty_tab.count) := MSC_ATP_PVT.INFINITE_NUMBER;
5043                 END IF;
5044 
5045                 Print_Dates_Qtys(l_atp_period_tab, l_atp_qty_tab);
5046 
5047             ELSE    -- IF nvl(p_mat_atp_info_rec.insert_flag, 0) = 0 THEN
5048 
5049                 l_get_mat_in_rec.infinite_time_fence_date := l_infinite_time_fence_date;
5050                 l_get_mat_in_rec.dest_inv_item_id := p_mat_atp_info_rec.dest_inv_item_id;
5051 
5052                 MSC_ATP_REQ.Insert_Details(p_mat_atp_info_rec.instance_id,
5053                         p_mat_atp_info_rec.plan_id,
5054                         p_mat_atp_info_rec.level,
5055                         p_mat_atp_info_rec.identifier,
5056                         p_mat_atp_info_rec.scenario_id,
5057                         p_mat_atp_info_rec.request_item_id,
5058                         p_mat_atp_info_rec.inventory_item_id,
5059                         p_mat_atp_info_rec.organization_id,
5060                         p_mat_atp_info_rec.demand_class,
5061                         p_mat_atp_info_rec.insert_flag,
5062                         x_atp_period,
5063                         x_atp_supply_demand,
5064                         l_return_status,
5065                         --diag_atp
5066                         l_get_mat_in_rec,
5067                         p_mat_atp_info_rec.atf_date); -- For time_phased_atp
5068 
5069                 IF PG_DEBUG in ('Y', 'C') THEN
5070                     msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'after Insert_Details');
5071                 END IF;
5072 
5073                 l_atp_period_tab := x_atp_period.Period_Start_Date;
5074                 l_atp_qty_tab := x_atp_period.Cumulative_Quantity;
5075 
5076                 Print_Dates_Qtys(l_atp_period_tab, l_atp_qty_tab);
5077             END IF; -- IF nvl(p_mat_atp_info_rec.insert_flag, 0) = 0 THEN
5078 
5079         ELSE  -- IF (MSC_ATP_PVT.G_ALLOCATED_ATP = 'N') THEN
5080 
5081             -- we are using allocated atp
5082             /* Modularize Item and Org Info */
5083             -- changed call, re-use info already obtained.
5084             MSC_ATP_PROC.get_global_org_info(p_mat_atp_info_rec.instance_id, p_mat_atp_info_rec.organization_id);
5085             l_default_atp_rule_id := MSC_ATP_PVT.G_ORG_INFO_REC.default_atp_rule_id;
5086             l_calendar_code := MSC_ATP_PVT.G_ORG_INFO_REC.cal_code;
5087             l_calendar_exception_set_id :=
5088                                 MSC_ATP_PVT.G_ORG_INFO_REC.cal_exception_set_id;
5089             l_default_demand_class := MSC_ATP_PVT.G_ORG_INFO_REC.default_demand_class;
5090             l_org_code := MSC_ATP_PVT.G_ORG_INFO_REC.org_code;
5091             /*Modularize Item and Org Info */
5092 
5093             IF PG_DEBUG in ('Y', 'C') THEN
5094                 msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'l_default_atp_rule_id='|| l_default_atp_rule_id);
5095                 msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'l_calendar_code='||l_calendar_code);
5096                 msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'l_calendar_exception_set_id'|| l_calendar_exception_set_id);
5097                 msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'l_default_demand_class'|| l_default_demand_class);
5098             END IF;
5099             IF MSC_ATP_PVT.G_ALLOCATION_METHOD = 1 AND MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1 THEN
5100                 IF PG_DEBUG in ('Y', 'C') THEN
5101                     msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'Pre-alllocated demand class AATP');
5102                 END IF;
5103 
5104                 --diag_atp: pass infinite time fence as in parameter so that we dont need to recalculate it
5105                 l_get_mat_in_rec.infinite_time_fence_date := l_infinite_time_fence_date;
5106                 l_get_mat_in_rec.dest_inv_item_id := p_mat_atp_info_rec.dest_inv_item_id;
5107 
5108                 MSC_AATP_REQ.Item_Pre_Allocated_Atp(p_mat_atp_info_rec.plan_id,
5109                                                     p_mat_atp_info_rec.level,
5110                                                     p_mat_atp_info_rec.identifier,
5111                                                     p_mat_atp_info_rec.scenario_id,
5112                                                     p_mat_atp_info_rec.inventory_item_id,
5113                                                     p_mat_atp_info_rec.organization_id,
5114                                                     p_mat_atp_info_rec.instance_id,
5115                                                     p_mat_atp_info_rec.demand_class,
5116                                                     l_requested_date,
5117                                                     p_mat_atp_info_rec.insert_flag,
5118                                                     l_atp_info,
5119                                                     x_atp_period,
5120                                                     x_atp_supply_demand,
5121                                                     --diag_atp
5122                                                     l_get_mat_in_rec,
5123                                                     p_mat_atp_info_rec.refresh_number,   -- For summary enhancement
5124                                                     p_mat_atp_info_rec.request_item_id,  -- For time_phased_atp
5125                                                     p_mat_atp_info_rec.atf_date);        -- For time_phased_atp
5126 
5127 
5128             -- rajjain 02/20/2003 Bug 2813095 Begin
5129             ELSIF MSC_ATP_PVT.G_ALLOCATION_METHOD = 1 AND MSC_ATP_PVT.G_HIERARCHY_PROFILE = 2 THEN
5130                 RAISE MSC_ATP_PVT.ALLOC_ATP_INVALID_PROFILE;
5131                 -- rajjain 02/20/2003 Bug 2813095 End
5132             ELSE
5133 
5134                 IF PG_DEBUG in ('Y', 'C') THEN
5135                     msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'Customer class or Allocation based demand class AATP');
5136                 END IF;
5137                 --diag_atp: pass infinite time fence as in parameter so that we dont need to recalculate it
5138                 l_get_mat_in_rec.infinite_time_fence_date := l_infinite_time_fence_date;
5139                 l_get_mat_in_rec.dest_inv_item_id := p_mat_atp_info_rec.dest_inv_item_id;
5140                 MSC_AATP_PVT.Item_Alloc_Cum_Atp(p_mat_atp_info_rec.plan_id,
5141                                                 p_mat_atp_info_rec.level,
5142                                                 p_mat_atp_info_rec.identifier,
5143                                                 p_mat_atp_info_rec.scenario_id,
5144                                                 p_mat_atp_info_rec.inventory_item_id,
5145                                                 p_mat_atp_info_rec.organization_id,
5146                                                 p_mat_atp_info_rec.instance_id,
5147                                                 NVL(p_mat_atp_info_rec.demand_class,
5148                                                     NVL(l_default_demand_class, '@@@')),
5149                                                 l_requested_date,
5150                                                 p_mat_atp_info_rec.insert_flag,
5151                                                 l_atp_info,
5152                                                 x_atp_period,
5153                                                 x_atp_supply_demand,
5154                                                 l_get_mat_in_rec,
5155                                                 p_mat_atp_info_rec.request_item_id,  -- For time_phased_atp
5156                                                 p_mat_atp_info_rec.atf_date);         -- For time_phased_atp
5157             END IF;
5158             l_atp_period_tab := l_atp_info.atp_period;
5159             l_atp_qty_tab := l_atp_info.atp_qty;
5160 
5161             --bug2471377 pumehta Begin Changes
5162             --copy the period information which can be used
5163             --later in atp_check procedure in the case where
5164             -- comp_flag is 'N' and stealing has happened.
5165             --Copy these values only if we are at Top assembly level.
5166 
5167             IF NVL(p_mat_atp_info_rec.insert_flag,0) = 0 and
5168                 p_mat_atp_info_rec.level = 1 AND
5169                 nvl(MSC_ATP_PVT.G_ITEM_INFO_REC.atp_comp_flag,'N') = 'N' THEN
5170                 x_atp_period.cumulative_quantity := l_atp_info.atp_qty;
5171                 x_atp_period.period_start_date := l_atp_info.atp_period;
5172             END IF;
5173             --Bug2471377 End Changes.
5174 
5175             IF PG_DEBUG in ('Y', 'C') THEN
5176                 msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'l_atp_info.atp_period.count = '||l_atp_info.atp_period.COUNT);
5177                 msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'l_atp_info.atp_qty.count = '||l_atp_info.atp_qty.COUNT);
5178                 msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'l_atp_info.limit_qty.count = '||l_atp_info.limit_qty.COUNT);
5179             END IF;
5180 
5181         END IF; -- end of G_ALLOCATED_ATP
5182 
5183         IF PG_DEBUG in ('Y', 'C') THEN
5184             msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'l_atp_period_tabl.count = '||l_atp_period_tab.COUNT);
5185         END IF;
5186 
5187         IF l_atp_period_tab.COUNT = 0 THEN
5188             -- need to add error message
5189             RAISE NO_DATA_FOUND;
5190         END IF;
5191 
5192         /* time_phased_atp
5193         Atp dates qtys calculation code moved to a private procedure*/
5194         Calculate_Atp_Dates_Qtys(
5195                 l_atp_period_tab,
5196                 l_atp_qty_tab,
5197                 l_requested_date,
5198                 p_mat_atp_info_rec.atf_date,
5199                 p_mat_atp_info_rec.quantity_ordered,
5200                 -- l_sys_next_date, Bug 3371817
5201                 l_sys_next_osc_date,
5202                 p_mat_atp_info_rec.rounding_control_flag,
5203                 p_mat_atp_info_rec.requested_date_quantity,
5204                 p_mat_atp_info_rec.atf_date_quantity,
5205                 p_mat_atp_info_rec.atp_date_this_level,
5206                 p_mat_atp_info_rec.atp_date_quantity_this_level,
5207                 x_return_status
5208         );
5209 
5210         IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5211             x_return_status := FND_API.G_RET_STS_ERROR;
5212             return;
5213         END IF;
5214     END IF; --end if l_requested_date > l_infinite_time_fence_date
5215     --- subst
5216     --- IF future date is after substitution window then we will move atp date to infinite time fence date
5217     IF PG_DEBUG in ('Y', 'C') THEN
5218         msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'p_mat_atp_info_rec.atp_date_this_level :=' || p_mat_atp_info_rec.atp_date_this_level);
5219         msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'l_infinite_time_fence_date := ' || l_infinite_time_fence_date);
5220     END IF;
5221 
5222     IF MSC_ATP_PVT.G_SUBSTITUTION_FLAG = 'Y' AND NVL(p_mat_atp_info_rec.substitution_window,0) > 0
5223         AND p_mat_atp_info_rec.atp_date_this_level is not null THEN
5224         IF PG_DEBUG in ('Y', 'C') THEN
5225             msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'we have substitution window');
5226         END IF;
5227         IF l_infinite_time_fence_date is not null and p_mat_atp_info_rec.atp_date_this_level = l_infinite_time_fence_date THEN
5228             IF PG_DEBUG in ('Y', 'C') THEN
5229                 msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'p_mat_atp_info_rec.atp_date_this_level = infinite supply, dont move the date');
5230             END IF;
5231         ELSE
5232             IF PG_DEBUG in ('Y', 'C') THEN
5233                 msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'Do substitution check');
5234             END IF;
5235             l_subst_limit_date := MSC_CALENDAR.DATE_OFFSET(
5236                                                 p_mat_atp_info_rec.organization_id,
5237                                                 p_mat_atp_info_rec.instance_id,
5238                                                 1,
5239                                                 p_mat_atp_info_rec.requested_date,
5240                                                 p_mat_atp_info_rec.substitution_window);
5241             IF PG_DEBUG in ('Y', 'C') THEN
5242                 msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'l_subst_limit_date := ' || l_subst_limit_date);
5243                 msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'p_mat_atp_info_rec.atp_date_this_level := ' || p_mat_atp_info_rec.atp_date_this_level);
5244             END IF;
5245             IF p_mat_atp_info_rec.atp_date_this_level > l_subst_limit_date THEN
5246                 IF PG_DEBUG in ('Y', 'C') THEN
5247                     msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || ' p_mat_atp_info_rec.atp_date_this_level > l_subst_limit_date');
5248                     msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'moving p_mat_atp_info_rec.atp_date_this_level to infinite time fence');
5249                 END IF;
5250                 p_mat_atp_info_rec.atp_date_this_level := l_infinite_time_fence_date;
5251                 p_mat_atp_info_rec.atp_date_quantity_this_level := MSC_ATP_PVT.INFINITE_NUMBER;
5252             END IF;
5253         END IF;
5254     END IF;
5255 
5256     IF PG_DEBUG in ('Y', 'C') THEN
5257         msc_sch_wb.atp_debug('**********End Get_Material_Atp_Info Procedure************');
5258     END IF;
5259 
5260 EXCEPTION
5261 
5262     WHEN NO_DATA_FOUND THEN
5263         --        x_return_status := FND_API.G_RET_STS_ERROR;
5264         p_mat_atp_info_rec.requested_date_quantity := 0.0;
5265 
5266         -- RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5267 
5268 
5269         --- Bug 1819638: This exception is added too handel error
5270         --- when no next_date corresponding to sys date is found in
5271         --- the calendar
5272         ---- This exception raises exception NO_MATCHING_DATE_IN_CAL
5273         --- This exception is defined in ATP_CHECK.
5274         --- Since we can't pass back the error code to atp_check from get_mat_atp_info,
5275         --- we are using exceptions
5276     WHEN MSC_ATP_PVT.NO_MATCHING_DATE_IN_CAL THEN
5277         p_mat_atp_info_rec.requested_date_quantity := 0.0;
5278         IF PG_DEBUG in ('Y', 'C') THEN
5279             msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'No match for sysdate in cal');
5280         END IF;
5281         RAISE MSC_ATP_PVT.NO_MATCHING_DATE_IN_CAL;
5282 
5283     /************ Bug 1510853 ATP Rule Check ************/
5284     WHEN MSC_ATP_PVT.EXC_NO_ATP_RULE  THEN
5285         p_mat_atp_info_rec.requested_date_quantity := 0.0;
5286         IF PG_DEBUG in ('Y', 'C') THEN
5287             msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'No Applicable ATP rule in Get Material ATP Info');
5288         END IF;
5289         RAISE MSC_ATP_PVT.EXC_NO_ATP_RULE;
5290 
5291     -- rajjain 02/20/2003 Bug 2813095
5292     WHEN MSC_ATP_PVT.ALLOC_ATP_INVALID_PROFILE THEN
5293         IF PG_DEBUG in ('Y', 'C') THEN
5294             msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' ||
5295                 'Incompatible setup of MSC: ATP Allocation Method and MSC: Class Hierarchy');
5296         END IF;
5297         RAISE MSC_ATP_PVT.ALLOC_ATP_INVALID_PROFILE;
5298 
5299     WHEN OTHERS THEN
5300         temp := SQLCODE;
5301         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5302         IF PG_DEBUG in ('Y', 'C') THEN
5303             msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'Get_Mater, sqlcode= '||temp);
5304             msc_sch_wb.atp_debug ('Get_Material_Atp_Info: IN Exception Block in others');
5305             msc_sch_wb.atp_debug ('error := ' || SQLERRM);
5306         END IF;
5307         --bug3583705 commenting this out as this resets the FND_MESSAGE.SET_NAME
5308         /*IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
5309             FND_MSG_PUB.Add_Exc_Msg (G_PKG_NAME , 'Get_Material_Atp_Info');
5310         END IF;*/
5311 
5312         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5313 
5314 
5315 END Get_Material_Atp_Info;
5316 
5317 /*
5318  * dsting: 9/16/2002. S/D performance enh using temp table
5319  *
5320  * x_atp_supply_demand never gets populated
5321  *
5322  */
5323 PROCEDURE Insert_Details (
5324   p_instance_id         IN    NUMBER,
5325   p_plan_id             IN    NUMBER,
5326   p_level               IN    NUMBER,
5327   p_identifier          IN    NUMBER,
5328   p_scenario_id         IN    NUMBER,
5329   p_request_item_id     IN    NUMBER,
5330   p_inventory_item_id   IN    NUMBER,
5331   p_organization_id     IN    NUMBER,
5332   p_demand_class        IN    VARCHAR2,
5333   p_insert_flag         IN    NUMBER,
5334   x_atp_period          OUT   NOCOPY MRP_ATP_PUB.ATP_Period_Typ,
5335   x_atp_supply_demand   OUT   NOCOPY MRP_ATP_PUB.ATP_Supply_Demand_Typ,
5336   x_return_status       OUT   NoCopy VARCHAR2,
5337   p_get_mat_in_rec      In    MSC_ATP_REQ.get_mat_in_rec,
5338   p_atf_date            IN    DATE      -- For time_phased_atp
5339 )
5340 
5341 IS
5342 i PLS_INTEGER;
5343 j PLS_INTEGER;
5344 l_null_num  number := null;
5345 l_null_char    varchar2(3) := null;
5346 l_infinite_time_fence_date DATE;
5347 l_sysdate_seq_num               NUMBER;
5348 l_sys_next_date                 DATE;
5349 l_default_atp_rule_id           NUMBER;
5350 l_calendar_code                 VARCHAR2(14);
5351 l_calendar_exception_set_id     NUMBER;
5352 l_default_demand_class          VARCHAR2(34);
5353 l_request_item_id               NUMBER;
5354 
5355 l_org_code                      VARCHAR2(7);
5356 --bug3583705
5357 --NO_MATCHING_CAL_DATE            EXCEPTION;
5358 
5359 l_sysdate			DATE := trunc(sysdate);  --4135752
5360 
5361 -- time_phased_atp
5362 l_time_phased_atp               VARCHAR2(1):= 'N';
5363 l_return_status                 VARCHAR2(1);
5364 
5365 Begin
5366 
5367   IF PG_DEBUG in ('Y', 'C') THEN
5368      msc_sch_wb.atp_debug('***** Begin Insert_Details Procedure *****');
5369   END IF;
5370 
5371   x_return_status := FND_API.G_RET_STS_SUCCESS;
5372 
5373   IF p_insert_flag >0 THEN
5374 
5375      /* time_phased_atp changes begin*/
5376      IF PG_DEBUG in ('Y', 'C') THEN
5377         msc_sch_wb.atp_debug('Insert_Details: ' || 'p_atf_date := ' || p_atf_date);
5378      END IF;
5379      IF (MSC_ATP_PVT.G_INV_CTP = 4) and (p_inventory_item_id <> p_request_item_id) and (p_atf_date is null) THEN
5380          l_request_item_id := p_inventory_item_id;
5381      ELSIF (MSC_ATP_PVT.G_INV_CTP = 4) and (p_inventory_item_id <> p_request_item_id) and (p_atf_date is not null) THEN
5382          l_request_item_id := p_request_item_id;
5383          l_time_phased_atp := 'Y';
5384      ELSE
5385          l_request_item_id := p_request_item_id;
5386      END IF;
5387      IF PG_DEBUG in ('Y', 'C') THEN
5388         msc_sch_wb.atp_debug('Insert_Details: ' || 'p_request_item_id := ' || p_request_item_id);
5389         msc_sch_wb.atp_debug('Insert_Details: ' || 'p_inventory_item_id := ' || p_inventory_item_id);
5390         msc_sch_wb.atp_debug('Insert_Details: ' || 'l_request_item_id := ' || l_request_item_id);
5391         msc_sch_wb.atp_debug('Insert_Details: ' || 'Time Phased ATP = ' || l_time_phased_atp);
5392      END IF;
5393      /* time_phased_atp changes end*/
5394 
5395      --diag_atp
5396      /*l_infinite_time_fence_date := MSC_ATP_FUNC.get_infinite_time_fence_date(p_instance_id,
5397              p_inventory_item_id,p_organization_id,p_plan_id);
5398      */
5399      l_infinite_time_fence_date := p_get_mat_in_rec.infinite_time_fence_date;
5400      /* --bug 2287148
5401          l_sysdate_seq_num := MSC_ATP_FUNC.NEXT_WORK_DAY_SEQNUM(p_organization_id,
5402                                               p_instance_id,
5403                                               sysdate);
5404      */
5405      -- for performance reason, we need to get the following info and
5406      -- store in variables instead of joining it
5407 
5408      /* Modularize Item and Org Info */
5409      -- changed call, re-use info already obtained.
5410      MSC_ATP_PROC.get_global_org_info(p_instance_id, p_organization_id);
5411      l_default_atp_rule_id := MSC_ATP_PVT.G_ORG_INFO_REC.default_atp_rule_id;
5412      l_calendar_code := MSC_ATP_PVT.G_ORG_INFO_REC.cal_code;
5413      l_calendar_exception_set_id :=
5414                             MSC_ATP_PVT.G_ORG_INFO_REC.cal_exception_set_id;
5415      l_default_demand_class := MSC_ATP_PVT.G_ORG_INFO_REC.default_demand_class;
5416      l_org_code := MSC_ATP_PVT.G_ORG_INFO_REC.org_code;
5417      /*Modularize Item and Org Info */
5418 
5419      IF PG_DEBUG in ('Y', 'C') THEN
5420         msc_sch_wb.atp_debug('Insert_Details: ' || 'l_default_atp_rule_id='|| l_default_atp_rule_id);
5421         msc_sch_wb.atp_debug('Insert_Details: ' || 'l_calendar_code='||l_calendar_code);
5422         msc_sch_wb.atp_debug('Insert_Details: ' || 'l_calendar_exception_set_id'|| l_calendar_exception_set_id);
5423         msc_sch_wb.atp_debug('Insert_Details: ' || 'l_default_demand_class'|| l_default_demand_class);
5424      END IF;
5425 
5426      --Bug 2287148
5427      BEGIN
5428         SELECT cal.next_seq_num,cal.next_date
5429         INTO   l_sysdate_seq_num,l_sys_next_date
5430         FROM   msc_calendar_dates  cal
5431         WHERE  cal.exception_set_id = l_calendar_exception_set_id
5432         AND    cal.calendar_code = l_calendar_code
5433         AND    cal.calendar_date = TRUNC(sysdate)
5434         AND    cal.sr_instance_id = p_instance_id ;
5435      EXCEPTION
5436         WHEN OTHERS THEN
5437            --RAISE NO_MATCHING_CAL_DATE; bug3583705
5438            RAISE MSC_ATP_PVT.NO_MATCHING_DATE_IN_CAL;
5439 
5440      END;
5441      IF PG_DEBUG in ('Y', 'C') THEN
5442         msc_sch_wb.atp_debug('Insert_Details: ' || 'System Next Date is : '|| l_sys_next_date);
5443         msc_sch_wb.atp_debug('Insert_Details: ' || 'Sequence Number Is :'|| l_sysdate_seq_num);
5444         msc_sch_wb.atp_debug('Insert_Details: ' || 'before select');
5445      END IF;
5446 
5447 	MSC_ATP_DB_UTILS.Clear_SD_Details_temp();
5448 
5449         --- bug 2152184: compare p_inventory_item_id and l_request_item_id
5450         -- previous condition : p_inventory_item_id = p_request_item_id
5451         -- 2859130
5452         IF p_inventory_item_id = l_request_item_id THEN
5453            get_mat_avail_dtls(
5454                  MSC_ATP_PVT.G_OPTIMIZED_PLAN,
5455                  p_inventory_item_id,
5456                  p_request_item_id,
5457                  p_organization_id,
5458                  p_instance_id,
5459                  p_plan_id,
5460                  l_calendar_code,
5461                  l_calendar_exception_set_id,
5462                  l_sysdate_seq_num,
5463                  l_sys_next_date,
5464                  p_demand_class,
5465                  l_default_atp_rule_id,
5466                  l_default_demand_class,
5467                  l_infinite_time_fence_date,
5468                  p_level,
5469                  p_scenario_id,
5470                  p_identifier
5471            );
5472         ELSE
5473            -- time_phased_atp
5474            MSC_ATP_PF.Get_Mat_Avail_Pf_Dtls(
5475                  p_inventory_item_id,
5476                  p_request_item_id,
5477                  p_organization_id,
5478                  p_instance_id,
5479                  p_plan_id,
5480                  l_calendar_code,
5481                  l_sysdate_seq_num,
5482                  l_sys_next_date,
5483                  p_demand_class,
5484                  l_default_atp_rule_id,
5485                  l_default_demand_class,
5486                  l_infinite_time_fence_date,
5487                  p_level,
5488                  p_scenario_id,
5489                  p_identifier,
5490                  l_return_status
5491            );
5492            IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5493                    IF PG_DEBUG in ('Y', 'C') THEN
5494                         msc_sch_wb.atp_debug('Insert_Details: ' || 'Error occured in procedure Get_Mat_Avail_Pf_Dtls');
5495                    END IF;
5496                    RAISE FND_API.G_EXC_ERROR;
5497            END IF;
5498         END IF;
5499 
5500         IF PG_DEBUG in ('Y', 'C') THEN
5501            msc_sch_wb.atp_debug('Insert_Details: ' || 'after inserting into msc_atp_sd_details_temp');
5502            msc_sch_wb.atp_debug('Insert_Details: ' || 'Total Supply/Demand Recs : '|| SQL%ROWCOUNT);
5503         END IF;
5504 
5505         -- time_phased_atp
5506         IF l_time_phased_atp = 'Y' THEN
5507 	    MSC_ATP_PF.get_period_data_from_SD_temp(x_atp_period, l_return_status);
5508             IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5509                    IF PG_DEBUG in ('Y', 'C') THEN
5510                         msc_sch_wb.atp_debug('Insert_Details: ' || 'Error occured in procedure Get_Period_Data_From_Sd_Temp');
5511                    END IF;
5512                    RAISE FND_API.G_EXC_ERROR;
5513             END IF;
5514 	ELSE
5515 	    MSC_ATP_PROC.get_period_data_from_SD_temp(x_atp_period);
5516 	END IF;
5517 
5518         x_atp_period.Cumulative_Quantity := x_atp_period.Period_Quantity;
5519 
5520         IF PG_DEBUG in ('Y', 'C') THEN
5521            msc_sch_wb.atp_debug('Insert_Details: ' || 'before atp_consume');
5522         END IF;
5523 
5524         -- time_phased_atp changes begin
5525         Print_Dates_Qtys(x_atp_period.Period_Start_Date, x_atp_period.Cumulative_Quantity);
5526 
5527         IF l_time_phased_atp = 'Y' THEN
5528             MSC_ATP_PF.pf_atp_consume(
5529                    x_atp_period.Cumulative_Quantity,
5530                    l_return_status,
5531                    x_atp_period.Period_Start_Date,
5532                    MSC_ATP_PF.Bw_Fw_Cum, --b/w, f/w consumption and accumulation
5533                    p_atf_date);
5534             IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5535                    IF PG_DEBUG in ('Y', 'C') THEN
5536                         msc_sch_wb.atp_debug('Insert_Details: ' || 'Error occured in procedure pf_atp_consume');
5537                    END IF;
5538                    RAISE FND_API.G_EXC_ERROR;
5539             END IF;
5540         -- time_phased_atp changes end
5541         ELSE
5542             MSC_ATP_PROC.atp_consume(x_atp_period.Cumulative_Quantity,
5543                    x_atp_period.Cumulative_Quantity.COUNT);
5544         END IF;
5545 
5546         IF PG_DEBUG in ('Y', 'C') THEN
5547             msc_sch_wb.atp_debug('Insert_Details: ' || 'after atp_consume');
5548         END IF;
5549 
5550         /* Cum drop issue changes begin*/
5551         MSC_AATP_PROC.Atp_Remove_Negatives(x_atp_period.Cumulative_Quantity, l_return_status);
5552         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5553                 IF PG_DEBUG in ('Y', 'C') THEN
5554                         msc_sch_wb.atp_debug('Get_Material_Atp_Info: ' || 'Error occured in procedure Atp_Remove_Negatives');
5555                 END IF;
5556                 RAISE FND_API.G_EXC_ERROR;
5557         END IF;
5558         /* Cum drop issue changes end*/
5559 
5560         IF l_infinite_time_fence_date IS NOT NULL THEN
5561         	MSC_ATP_PROC.add_inf_time_fence_to_period(
5562         		p_level,
5563         		p_identifier,
5564         		p_scenario_id,
5565         		p_inventory_item_id,
5566         		p_request_item_id,
5567         		p_organization_id,
5568         		null,  -- p_supplier_id
5569         		null,  -- p_supplier_site_id
5570         		l_infinite_time_fence_date,
5571         		x_atp_period
5572         	);
5573         END IF;
5574 
5575   END IF;
5576 
5577   IF PG_DEBUG in ('Y', 'C') THEN
5578      msc_sch_wb.atp_debug('***** End Insert_Details Procedure *****');
5579   END IF;
5580 
5581 END Insert_Details;
5582 
5583 PROCEDURE Get_Res_Requirements (
5584     p_instance_id           IN      NUMBER,
5585     p_plan_id               IN      NUMBER,
5586     p_level                 IN      NUMBER,
5587     p_scenario_id           IN      NUMBER,
5588     p_inventory_item_id     IN      NUMBER,
5589     p_organization_id       IN      NUMBER,
5590     p_parent_pegging_id     IN      NUMBER,
5591     p_requested_quantity    IN      NUMBER,
5592     p_requested_date        IN      DATE,
5593     p_refresh_number        IN      NUMBER,
5594     p_insert_flag           IN      NUMBER,
5595     p_search                IN      NUMBER,
5596     p_demand_class          IN      VARCHAR2,
5597     --(ssurendr) Bug 2865389 Added routing Sequence id and Bill sequence id for OPM issue.
5598     p_routing_seq_id        IN      NUMBER,
5599     p_bill_seq_id           IN      NUMBER,
5600     p_parent_ship_date      IN      DATE,       -- Bug 2814872 Cut-off Date for Resource Check
5601     p_line_identifier       IN      NUMBER,     -- CTO ODR Identifies the line being processed.
5602     x_avail_assembly_qty    OUT     NoCopy NUMBER,
5603     x_atp_date              OUT     NoCopy DATE,
5604     x_atp_period            OUT     NOCOPY MRP_ATP_PUB.ATP_Period_Typ,
5605     x_atp_supply_demand     OUT     NOCOPY MRP_ATP_PUB.ATP_Supply_Demand_Typ,
5606     x_return_status         OUT     NoCopy VARCHAR2
5607 )
5608 IS
5609 l_res_requirements 		MRP_ATP_PVT.Atp_Res_Typ;
5610 l_atp_period_tab                MRP_ATP_PUB.date_arr:=MRP_ATP_PUB.date_arr();
5611 l_atp_qty_tab                   MRP_ATP_PUB.number_arr:=MRP_ATP_PUB.number_arr();
5612 l_requested_date_quantity	number;
5613 l_resource_id			number;
5614 l_department_id 		number;
5615 l_requested_date 		date;
5616 l_resource_usage 		number;
5617 l_basis_type 			number;
5618 l_efficiency                    number;
5619 l_utilization                   number;
5620 l_requested_res_qty		number;
5621 l_op_seq_num     		number;
5622 l_avail_assembly_qty		number;
5623 l_next_period			date;
5624 i				PLS_INTEGER;
5625 j				PLS_INTEGER;
5626 k                               PLS_INTEGER;
5627 m                               PLS_INTEGER;
5628 /************ BUG 2313497, 2126520  ************/
5629 h                               PLS_INTEGER;
5630 res_count                       PLS_INTEGER;
5631 l_lead_time                     number;
5632 /************ BUG 2313497, 2126520  ************/
5633 /*  Bug  3348095  */
5634 l_res_start_date                DATE := NULL;
5635 -- Variable to store the start date.
5636 /*  Bug  3348095  */
5637 l_atp_comp_flag			VARCHAR2(1);
5638 l_supply_id                     number;
5639 l_transaction_id                number;
5640 l_pegging_rec			mrp_atp_details_temp%ROWTYPE;
5641 l_resource_hours                number;
5642 l_pegging_id                    number;
5643 l_null_num                      number := null;
5644 l_null_char                     varchar2(3) := null;
5645 l_infinite_time_fence_date      DATE;
5646 -- Bug 3036513, Place holder for out parameter in get_infinite_time_fence_date.
5647 l_atp_rule_name                 VARCHAR2(80);
5648 -- End Bug 3036513
5649 l_return_status                 VARCHAR2(1);
5650 l_atp_period                    MRP_ATP_PUB.ATP_Period_Typ;
5651 l_atp_supply_demand             MRP_ATP_PUB.ATP_Supply_Demand_Typ;
5652 l_null_atp_period               MRP_ATP_PUB.ATP_Period_Typ;
5653 l_null_atp_supply_demand        MRP_ATP_PUB.ATP_Supply_Demand_Typ;
5654 l_res_atp_date                  date;
5655 l_res_atp_qty                   number;
5656 l_uom_code                      varchar2(10);
5657 l_plan_id                       NUMBER;
5658 l_assign_set_id                 NUMBER;
5659 l_use_bor                       NUMBER;
5660 l_org_code                      VARCHAR2(7);
5661 l_resource_code                 VARCHAR2(16);--4774169
5662 l_department_code               VARCHAR2(10);  -- 1487344
5663 l_default_atp_rule_id           NUMBER;
5664 l_calendar_code                 VARCHAR2(14);
5665 l_calendar_exception_set_id     NUMBER;
5666 l_default_demand_class          VARCHAR2(34);
5667 l_demand_class                  VARCHAR2(34);  -- Bug 2424357
5668 l_atp_info                      MRP_ATP_PVT.ATP_Info;
5669 l_inv_item_id                   NUMBER;
5670 l_parent_line_id                NUMBER;  --  CTO Option Dependent Resources ODR
5671 l_routing_seq_id                NUMBER;  --  CTO Option Dependent Resources ODR
5672 l_routing_Type						  NUMBER;
5673 l_routing_number                NUMBER;
5674 l_inventory_item_id				  NUMBER;
5675 l_MSO_Batch_Flag                  VARCHAR2(1);
5676 l_constraint_plan               NUMBER;
5677 l_use_batching                  NUMBER;
5678 l_max_capacity                  NUMBER;
5679 l_batchable_flag                NUMBER;
5680 l_req_unit_capacity             NUMBER;
5681 l_req_capacity_uom              VARCHAR2(3);
5682 l_std_op_code                   VARCHAR2(7);
5683 l_item_conversion_rate		number :=1;
5684 l_res_conversion_rate           number :=1;
5685 l_uom_type			number;
5686 l_res_uom                       varchar2(3);
5687 l_assembly_quantity             number := 1;
5688 l_mso_lead_time_factor          number;
5689 l_res_qty_before_ptf            number;
5690 l_plan_start_date               date;
5691 -- Bug 2372577
5692 l_msc_cap_allocation            VARCHAR2(1);
5693 -- Bug 4108546
5694 -- Variable to track PL/SQL Index when l_res_qty_before_ptf is set.
5695 l_res_ptf_indx                  NUMBER;
5696 
5697 --diag_atp
5698 l_owning_department_code        VARCHAR2(10);
5699 l_allocation_rule_name          VARCHAR2(30);
5700 
5701 l_sysdate DATE := sysdate;
5702 
5703 -- 2869380
5704 l_rounding_flag                 number;
5705 
5706 l_batching_flag                 number;
5707 l_item_info_rec                 MSC_ATP_PVT.item_attribute_rec;
5708 
5709 -- for summary enhancement
5710 l_summary_flag  NUMBER;
5711 l_summary_sql   VARCHAR2(1);
5712 
5713 -- Bug 3432530 Obtain Item Data into local variables.
5714 l_item_fixed_lt                 NUMBER;
5715 l_item_var_lt                   NUMBER;
5716 l_item_unit_vol                 NUMBER;
5717 l_item_unit_wt                  NUMBER;
5718 l_item_vol_uom                  VARCHAR2(3);
5719 l_item_wt_uom                   VARCHAR2(3);
5720 -- End Bug 3432530
5721 --bug 3516835
5722 CONST_USE_BOR CONSTANT NUMBER := 2;
5723 l_network_scheduling_method    NUMBER; --bug3601223
5724 --4198893,4198445
5725 l_res_availability_date        date;
5726 
5727 l_unadj_resource_hours         NUMBER; --5093604
5728 l_touch_time                   NUMBER; --5093604
5729 
5730 BEGIN
5731     IF PG_DEBUG in ('Y', 'C') THEN
5732        msc_sch_wb.atp_debug('***** Begin Get_Res_Requirements Procedure *****');
5733     END IF;
5734     -- initialize API return status to success
5735     x_return_status := FND_API.G_RET_STS_SUCCESS;
5736     l_uom_code := NVL(fnd_profile.value('MSC:HOUR_UOM_CODE'),
5737                       fnd_profile.value('BOM:HOUR_UOM_CODE'));
5738     l_MSO_Batch_flag := NVL(fnd_profile.value('MSO_BATCHABLE_FLAG'),'N');
5739     IF PG_DEBUG in ('Y', 'C') THEN
5740        msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'mso batchable flag := ' || l_MSO_Batch_flag );
5741     END IF;
5742 
5743     -- 3027711
5744     -- for performance reason, we need to get the following info and
5745     -- store in variables instead of joining it
5746 
5747     /* Modularize Item and Org Info */
5748     -- changed call, re-use info already obtained.
5749     MSC_ATP_PROC.get_global_org_info(p_instance_id, p_organization_id);
5750     l_default_atp_rule_id := MSC_ATP_PVT.G_ORG_INFO_REC.default_atp_rule_id;
5751     l_calendar_code := MSC_ATP_PVT.G_ORG_INFO_REC.cal_code;
5752     l_calendar_exception_set_id := MSC_ATP_PVT.G_ORG_INFO_REC.cal_exception_set_id;
5753     l_default_demand_class := MSC_ATP_PVT.G_ORG_INFO_REC.default_demand_class;
5754     l_org_code := MSC_ATP_PVT.G_ORG_INFO_REC.org_code;
5755     l_network_scheduling_method := MSC_ATP_PVT.G_ORG_INFO_REC.network_scheduling_method; --bug3601223
5756     /*Modularize Item and Org Info */
5757 
5758       /* Modularize Item and Org Info */
5759       -- Move the item fetch outside of the IF ELSE for BOR
5760       MSC_ATP_PROC.get_global_item_info(p_instance_id,
5761                                         --3917625: Read data from the plan
5762                                         -- -1,
5763                                         p_plan_id,
5764                                         p_inventory_item_id,
5765                                         p_organization_id,
5766                                         l_item_info_rec);
5767     -- 2869830
5768     l_rounding_flag := nvl(MSC_ATP_PVT.G_ITEM_INFO_REC.rounding_control_type,
5769 2);
5770     -- initially set the x_avail_assembly_qty to the p_request_quantity,
5771     -- and we adjust that later
5772 
5773     -- 2869830
5774     IF l_rounding_flag = 1 THEN
5775        x_avail_assembly_qty := CEIL(p_requested_quantity);
5776     ELSE
5777        x_avail_assembly_qty := trunc(p_requested_quantity, 6) ;		--5598066
5778     END IF;
5779 
5780     -- 2178544
5781     --x_atp_date := sysdate;
5782     x_atp_date := GREATEST(p_requested_date, MSC_ATP_PVT.G_FUTURE_ORDER_DATE, MSC_ATP_PVT.G_FUTURE_START_DATE);
5783 
5784     IF (p_routing_seq_id is null) AND
5785        (MSC_ATP_PVT.G_ITEM_INFO_REC.bom_item_type <> 1 )THEN
5786         RETURN;
5787     END IF;
5788     -- end 3027711
5789 
5790     Begin
5791 
5792     --3516835: Honor plan options to see if routing or BOR need to be used
5793     --SELECT decode(designator_type, 2, 1, 0),
5794     SELECT decode(daily_rtg_aggregation_level, CONST_USE_BOR, 1, 0),
5795            DECODE(plans.plan_type, 4, 2,
5796              DECODE(daily_material_constraints, 1, 1,
5797                DECODE(daily_resource_constraints, 1, 1,
5798                  DECODE(weekly_material_constraints, 1, 1,
5799                    DECODE(weekly_resource_constraints, 1, 1,
5800                      DECODE(period_material_constraints, 1, 1,
5801                        DECODE(period_resource_constraints, 1, 1, 2)
5802                            )
5803                          )
5804                        )
5805                      )
5806                    )
5807                  ),
5808            DECODE(l_MSO_Batch_Flag, 'Y', DECODE(plans.plan_type, 4, 0,2,0,  -- filter out MPS plans
5809              DECODE(daily_material_constraints, 1, 1,
5810                DECODE(daily_resource_constraints, 1, 1,
5811                  DECODE(weekly_material_constraints, 1, 1,
5812                    DECODE(weekly_resource_constraints, 1, 1,
5813                      DECODE(period_material_constraints, 1, 1,
5814                        DECODE(period_resource_constraints, 1, 1, 0)
5815                            )
5816                          )
5817                        )
5818                      )
5819                    )
5820                  ), 0),
5821            plans.summary_flag              -- for summary enhancement
5822     INTO   l_use_bor, MSC_ATP_PVT.G_OPTIMIZED_PLAN, l_constraint_plan, l_summary_flag
5823     FROM   msc_designators desig,
5824            msc_plans plans
5825     WHERE  plans.plan_id = p_plan_id
5826     AND    desig.designator = plans.compile_designator
5827     AND    desig.sr_instance_id = plans.sr_instance_id
5828     AND    desig.organization_id = plans.organization_id;
5829     EXCEPTION WHEN NO_DATA_FOUND THEN
5830            l_use_bor := 0;
5831            MSC_ATP_PVT.G_OPTIMIZED_PLAN := 2;
5832            l_constraint_plan := 0;
5833     END;
5834 
5835     IF PG_DEBUG in ('Y', 'C') THEN
5836        msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_use_bor = '||l_use_bor);
5837        msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'G_OPTIMIZED_PLAN = '||MSC_ATP_PVT.G_OPTIMIZED_PLAN);
5838        msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_constaint_plan  =' || l_constraint_plan);
5839     END IF;
5840     IF (l_MSO_Batch_Flag = 'Y') and (l_use_bor = 0) and (l_constraint_plan = 1) THEN
5841         IF PG_DEBUG in ('Y', 'C') THEN
5842            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Do Batching');
5843         END IF;
5844         l_use_batching := 1;
5845     ELSE
5846         IF PG_DEBUG in ('Y', 'C') THEN
5847            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'No Batching');
5848         END IF;
5849         l_use_batching := 0;
5850     END IF;
5851     IF PG_DEBUG in ('Y', 'C') THEN
5852        msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_use_batching =' || l_use_batching);
5853     END IF;
5854 
5855     -- Check if full summary has been run - for summary enhancement
5856     IF MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y' AND
5857         l_summary_flag NOT IN (MSC_POST_PRO.G_SF_SUMMARY_NOT_RUN, MSC_POST_PRO.G_SF_PREALLOC_COMPLETED,
5858                                MSC_POST_PRO.G_SF_FULL_SUMMARY_RUNNING) THEN
5859         -- Summary SQL can be used
5860         l_summary_sql := 'Y';
5861     ELSE
5862         -- Use the SQL for non summary case
5863         l_summary_sql := 'N';
5864     END IF;
5865 
5866     l_null_atp_period := x_atp_period;
5867     l_null_atp_supply_demand := x_atp_supply_demand;
5868      IF PG_DEBUG in ('Y', 'C') THEN
5869         msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_uom_code = ' || l_uom_code);
5870        msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'p_insert_flag = '||p_insert_flag);
5871        msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'p_instance_id = '||p_instance_id);
5872        msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'p_plan_id = '||p_plan_id);
5873        msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'p_level = '||p_level);
5874        msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'p_scenario_id = '||p_scenario_id);
5875        msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'p_inventory_item_id = '||p_inventory_item_id);
5876        msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'p_organization_id = '||p_organization_id);
5877        msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'p_parent_pegging_id = '||p_parent_pegging_id);
5878        msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'p_requested_quantity = '||p_requested_quantity);
5879        msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'p_requested_date = '||p_requested_date);
5880        msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'p_refresh_number = '||p_refresh_number);
5881        msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'p_search = '||p_search);
5882        -- Then add the latest parameter
5883        msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'p_bill_seq_id = '|| p_bill_seq_id);
5884        msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'p_parent_ship_date = '|| p_parent_ship_date);
5885        msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'p_line_identifier = '|| p_line_identifier);
5886        msc_sch_wb.atp_debug('Get_Res_Requirements: ' ||
5887            'MSC_ATP_PVT.G_ITEM_INFO_REC.bom_item_type= '||
5888                     MSC_ATP_PVT.G_ITEM_INFO_REC.bom_item_type);
5889        --bug3601223
5890        msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_network_scheduling_method = '|| l_network_scheduling_method);
5891        -- End Bug 2814872
5892     END IF;
5893     --- get msc_lead_time factor
5894     l_mso_lead_time_factor := MSC_ATP_PVT.G_MSO_LEAD_TIME_FACTOR;
5895 
5896     -- CTO Option Dependent Resources ODR
5897     l_parent_line_id := NULL;
5898     -- Set Destination Inventory Item Id
5899     l_inventory_item_id  :=  MSC_ATP_PVT.G_ITEM_INFO_REC.dest_inv_item_id;
5900     -- Bug 3432530 Obtain Item Data into local variables.
5901     l_item_fixed_lt      :=  MSC_ATP_PVT.G_ITEM_INFO_REC.fixed_lt;
5902     l_item_var_lt        :=  MSC_ATP_PVT.G_ITEM_INFO_REC.variable_lt;
5903     l_item_unit_vol      :=  MSC_ATP_PVT.G_ITEM_INFO_REC.unit_volume;
5904     l_item_unit_wt       :=  MSC_ATP_PVT.G_ITEM_INFO_REC.unit_weight;
5905     --Bug 3976771 assigned correct values to local variables.
5906     --l_item_vol_uom       :=  MSC_ATP_PVT.G_ITEM_INFO_REC.unit_volume;
5907     --l_item_wt_uom        :=  MSC_ATP_PVT.G_ITEM_INFO_REC.unit_weight;
5908     l_item_vol_uom       :=  MSC_ATP_PVT.G_ITEM_INFO_REC.volume_uom;
5909     l_item_wt_uom        :=  MSC_ATP_PVT.G_ITEM_INFO_REC.weight_uom;
5910     IF PG_DEBUG in ('Y', 'C') THEN
5911        msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_item_fixed_lt = '
5912                                                             || l_item_fixed_lt);
5913        msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_item_var_lt = '
5914                                                             || l_item_var_lt);
5915        msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_item_unit_vol = '
5916                                                             ||l_item_unit_vol);
5917        msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_item_unit_wt  = '
5918                                                             ||l_item_unit_wt);
5919        msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_item_wt_uom = '
5920                                                             ||l_item_wt_uom);
5921        msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_item_vol_uom  = '
5922                                                             ||l_item_vol_uom);
5923     END IF;
5924     -- End Bug 3432530
5925     IF MSC_ATP_PVT.G_ITEM_INFO_REC.bom_item_type in (1, 2) THEN
5926           BEGIN
5927              -- Bug 3358981
5928              -- Default the ATO_PARENT_MODEL_LINE_ID for models to line_id
5929              SELECT DECODE(MSC_ATP_PVT.G_ITEM_INFO_REC.bom_item_type, 1,
5930                             p_line_identifier, ATO_PARENT_MODEL_LINE_ID)
5931              -- End Bug 3358981
5932              INTO   l_parent_line_id
5933              FROM   msc_cto_bom
5934              WHERE  line_id = p_line_identifier
5935              AND    session_id = MSC_ATP_PVT.G_SESSION_ID
5936              AND    sr_inventory_item_id = p_inventory_item_id;
5937 
5938              -- Common Routing check
5939              IF p_routing_seq_id IS NOT NULL THEN
5940                  l_routing_seq_id := p_routing_seq_id;
5941              ELSE
5942                  SELECT routing_sequence_id
5943                  INTO   l_routing_seq_id
5944                  FROM   msc_routings
5945                  WHERE  sr_instance_id = p_instance_id
5946                  AND    plan_id = p_plan_id
5947                  AND    organization_id = p_organization_id
5948                  AND    assembly_item_id = l_inventory_item_id;
5949              END IF;
5950 
5951           EXCEPTION
5952             WHEN OTHERS THEN
5953                 l_parent_line_id := NULL;
5954           END;
5955     ELSE
5956        l_routing_seq_id := p_routing_seq_id;
5957     END IF;
5958     IF PG_DEBUG in ('Y', 'C') THEN
5959        msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_parent_line_id= '|| l_parent_line_id);
5960        msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_inventory_item_id= '|| l_inventory_item_id);
5961        msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_routing_sequence_id= '|| l_routing_seq_id);
5962     END IF;
5963     -- End CTO Option Dependent Resources ODR
5964     -- 3027711 moved getting item/org info to beginning
5965 
5966     IF (l_use_bor <> 1) THEN
5967       IF PG_DEBUG in ('Y', 'C') THEN
5968          msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'not using BOR');
5969       END IF;
5970       -- get resource requirements
5971      	-- OSFM changes
5972       l_inventory_item_id  :=  MSC_ATP_PVT.G_ITEM_INFO_REC.dest_inv_item_id;
5973       /*Modularize Item and Org Info */
5974     	-- get the routing flag to determine the type of routing
5975       BEGIN
5976          SELECT cfm_routing_flag, routing_sequence_id
5977          INTO   l_routing_type,l_routing_number
5978       	 FROM   msc_routings
5979          WHERE  plan_id = p_plan_id and
5980                 organization_id = p_organization_id and
5981                 sr_instance_id = p_instance_id and
5982                 assembly_item_id = l_inventory_item_id and
5983                 routing_sequence_id = l_routing_seq_id;  -- CTO ODR
5984                 --routing_sequence_id = p_routing_seq_id;
5985                 --(ssurendr) Bug 28655389 removed the alternate routing desgnator
5986                 --condition for OPM fix.
5987       EXCEPTION
5988       	WHEN OTHERS THEN
5989          	l_routing_type := null;
5990       END;
5991 
5992 
5993       IF PG_DEBUG in ('Y', 'C') THEN
5994          msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'cfm_routing_flag= '|| l_routing_type);
5995          msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'routing sequence id = ' || l_routing_number);
5996        END IF;
5997       IF l_routing_type = 3 THEN --- network routing
5998 
5999        -- CTO Option Dependent Resources
6000        -- Option Dependent Routing ODR Determination
6001        IF MSC_ATP_PVT.G_ITEM_INFO_REC.bom_item_type in (1, 2) THEN
6002       	SELECT  DISTINCT            -- collapse common into one in case union all is used.
6003                 -- Uncomment: Bug 3432530 For CTO we obtain the Op. Seq. related data only once.
6004                 department_id,
6005                 owning_department_id,
6006         	resource_id,
6007            	basis_type,
6008            	resource_usage,
6009            	requested_date,
6010            	lead_time,
6011            	efficiency,
6012            	utilization,
6013                 batch_flag,
6014                 max_capacity,
6015                 required_unit_capacity,
6016                 required_capacity_uom,
6017                 res_uom,
6018                 res_uom_type,
6019                 std_op_code,
6020                 --diag_atp
6021                 resource_offset_percent,
6022                 operation_sequence,
6023                 actual_resource_usage,
6024                 reverse_cumulative_yield,
6025                 department_code,
6026                 resource_code
6027 
6028       	BULK COLLECT INTO l_res_requirements.department_id,
6029                       l_res_requirements.owning_department_id,
6030                       l_res_requirements.resource_id,
6031                       l_res_requirements.basis_type,
6032                       l_res_requirements.resource_usage,
6033                       l_res_requirements.requested_date,
6034                       l_res_requirements.lead_time,
6035                       l_res_requirements.efficiency,
6036                       l_res_requirements.utilization,
6037                       --- these columns have been added for resource batching
6038                       l_res_requirements.batch_flag,
6039                       l_res_requirements.max_capacity,
6040                       l_res_requirements.required_unit_capacity,
6041                       l_res_requirements.required_capacity_uom,
6042                       l_res_requirements.res_uom,
6043                       l_res_requirements.res_uom_type,
6044                       l_res_requirements.std_op_code,
6045                       ---diag_atp
6046                       l_res_requirements.resource_offset_percent,
6047                       l_res_requirements.operation_sequence,
6048                       l_res_requirements.actual_resource_usage,
6049                       l_res_requirements.reverse_cumulative_yield,
6050                       l_res_requirements.department_code,
6051                       l_res_requirements.resource_code
6052       	FROM (
6053          -- First select mandatory operations
6054          -- for common routing cases the mandatory for option classes
6055          -- will be clubbed together with the model.
6056        	 SELECT   /*+ ordered */ DISTINCT  DR.DEPARTMENT_ID department_id,
6057                   -- Distinct: Bug 3432530 For CTO we obtain the Op. Seq. related data only once.
6058                 	DR.OWNING_DEPARTMENT_ID owning_department_id,
6059                 	DR.RESOURCE_ID resource_id,
6060                 	RES.BASIS_TYPE basis_type,
6061                         --bug 3766224: Do not chnage usage for lot based resource
6062                         ROUND(DECODE(RES.BASIS_TYPE, 2, NVL(RES.RESOURCE_USAGE,0),
6063                 	(NVL(RES.RESOURCE_USAGE,0)*
6064                         -- krajan : 2408696
6065                         -- MUC2.CONVERSION_RATE/MUC1.CONVERSION_RATE, 0*
6066                         --bug3601223 Only if network_scheduling_method is planning percent then use % else 100%
6067                         Decode(l_network_scheduling_method,2,
6068                         (NVL(OP.NET_PLANNING_PERCENT,100)/100),1)
6069                         /DECODE(OP.REVERSE_CUMULATIVE_YIELD,0,1,NVL(OP.REVERSE_CUMULATIVE_YIELD,1)))
6070                         /(Decode (nvl (MSC_ATP_PVT.G_ORG_INFO_REC.org_type,MSC_ATP_PVT.DISCRETE_ORG), MSC_ATP_PVT.OPM_ORG, --Bug-4694958
6071                                   decode (RES.BASIS_TYPE, 3,
6072                                           NVL(DR.MAX_CAPACITY,1),  nvl(rtg.routing_quantity,1)
6073                                          ),
6074                                   nvl(rtg.routing_quantity,1)
6075                                  )
6076                          )),6) resource_usage, --4694958
6077                             -- Bug 2865389 (ssurendr) routing quantity added for OPM fix.
6078                 	C2.CALENDAR_DATE requested_date,
6079                         --  Bug 3432530 Use local variables.
6080                         --  In case of common routing, use model's lead times.
6081                 	CEIL(((NVL(l_item_fixed_lt,0)+
6082                         NVL(l_item_var_lt,0)* p_requested_quantity) * (1+ l_mso_lead_time_factor))*
6083                         (1-NVL(SEQ.RESOURCE_OFFSET_PERCENT, 0))) lead_time,
6084                         --  End Bug 3432530 Use local variables.
6085                 	NVL((DR.EFFICIENCY/100), 1) efficiency,
6086                 	NVL((DR.UTILIZATION/100), 1) utilization,
6087                         NVL(DR.BATCHABLE_FLAG, 2) batch_flag,
6088                         NVL(DR.MAX_CAPACITY,0) max_capacity,
6089                         --  Bug 3432530 Use local variables.
6090                         --  In case of common routing, use model's item data.
6091                         DECODE(DR.UOM_CLASS_TYPE, 1, l_item_unit_wt, 2, l_item_unit_vol) required_unit_capacity,
6092                         ---bug 1905284
6093                         DECODE(DR.UOM_CLASS_TYPE, 1, l_item_wt_uom, 2, l_item_vol_uom) required_capacity_uom ,
6094                         --  End Bug 3432530 Use local variables.
6095                         DR.UNIT_OF_MEASURE res_uom,
6096                         DR.UOM_CLASS_TYPE res_uom_type,
6097                         OP.STANDARD_OPERATION_CODE std_op_code,
6098                         --diag_atp
6099                         SEQ.RESOURCE_OFFSET_PERCENT resource_offset_percent,
6100                         OP.OPERATION_SEQ_NUM operation_sequence,
6101                         RES.RESOURCE_USAGE actual_resource_usage,
6102                         --NVL(OP.REVERSE_CUMULATIVE_YIELD, 1) reverse_cumulative_yield ,
6103                         DECODE(OP.REVERSE_CUMULATIVE_YIELD,0,1,NVL(OP.REVERSE_CUMULATIVE_YIELD, 1)) reverse_cumulative_yield ,--4694958
6104                         DR.Department_code department_code,
6105                         DR.resource_code resource_code
6106 
6107        	FROM
6108                 	MSC_CTO_BOM  mcbom1,
6109                         -- MSC_SYSTEM_ITEMS I, Bug 3432530 Comment out Join table
6110                 	MSC_ROUTINGS RTG,
6111                 	MSC_ROUTING_OPERATIONS OP,
6112                 	MSC_OPERATION_RESOURCE_SEQS SEQ,
6113                 	MSC_OPERATION_RESOURCES RES,
6114                         MSC_DEPARTMENT_RESOURCES DR, -- this is the sharing dept
6115                 	MSC_CALENDAR_DATES C1,
6116                 	MSC_CALENDAR_DATES C2
6117 
6118        	WHERE    mcbom1.session_id = MSC_ATP_PVT.G_SESSION_ID
6119         AND      mcbom1.sr_instance_id = p_instance_id
6120                  -- Bug 3358981 line is a model then include,
6121         AND      (mcbom1.ATO_PARENT_MODEL_LINE_ID = l_parent_line_id OR mcbom1.line_id = l_parent_line_id)
6122                  -- get all lines having the same parent model End Bug 3358981
6123         AND      (  --mcbom1.parent_line_id = p_line_identifier or
6124                     -- Handle situation when parent_line_id is null.
6125                     -- Basic thing is that this section should handle all cases.
6126                     mcbom1.inventory_item_id = l_inventory_item_id )
6127         AND      mcbom1.quantity <> 0
6128         -- Get the routing
6129        	AND      RTG.PLAN_ID = p_plan_id
6130        	AND      RTG.SR_INSTANCE_ID =  mcbom1.sr_instance_id
6131        	AND      RTG.ORGANIZATION_ID = p_organization_id
6132        	AND      RTG.ROUTING_SEQUENCE_ID = l_routing_seq_id
6133 --       	AND      RTG.ROUTING_SEQUENCE_ID = p_routing_seq_id -- Local var for common and others
6134                  -- Bug 3432530
6135                  -- Comment out join conditions for msc_system_items
6136                   -- 3358981 Eliminate semi cartesian product, streamline query.
6137         AND      RTG.assembly_item_id = mcbom1.inventory_item_id
6138         -- Join to system items
6139         -- AND      I.PLAN_ID = RTG.PLAN_ID
6140         -- AND      I.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
6141         -- AND      I.ORGANIZATION_ID = RTG.ORGANIZATION_ID
6142         -- AND      I.INVENTORY_ITEM_ID = RTG.assembly_item_id
6143                    -- 3358981 Eliminate semi cartesian product, streamline query.
6144         -- AND      I.INVENTORY_ITEM_ID = l_inventory_item_id
6145                  -- End Bug 3432530
6146         --(ssurendr) Bug 2865389 Removed condition for Alternate Routing designator as
6147         --we are accessing Routing by Routing sequance id.
6148         --We are Driving by routing table for performance gains.
6149        	--AND      RTG.ALTERNATE_ROUTING_DESIGNATOR IS NULL
6150         --  Get all operations for the routing
6151        	AND      OP.PLAN_ID = RTG.PLAN_ID
6152        	AND      OP.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
6153        	AND      OP.ROUTING_SEQUENCE_ID = RTG.ROUTING_SEQUENCE_ID
6154         /* Operation is of type Event (Do not select process) */
6155         and      NVL(OP.operation_type,1 ) = 1
6156         /* rajjain 3008611
6157          * effective date should be greater than or equal to greatest of PTF date, sysdate and start date
6158          * disable date should be less than or equal to greatest of PTF date, sysdate and start date*/
6159        	AND      TRUNC(NVL(OP.DISABLE_DATE, GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE)+1)) >
6160         	         	TRUNC(GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE))
6161        	AND      TRUNC(OP.EFFECTIVITY_DATE) <=
6162          	      	TRUNC(GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE)) -- bug 1404312
6163                  -- SMCs/Mandatory Operations
6164         AND      OP.option_dependent_flag = 2
6165                  -- for the configuration
6166         -- Obtain the Resource Seq numbers.
6167        	AND      SEQ.PLAN_ID = OP.PLAN_ID
6168        	AND      SEQ.ROUTING_SEQUENCE_ID  = OP.ROUTING_SEQUENCE_ID
6169        	AND      SEQ.SR_INSTANCE_ID = OP.SR_INSTANCE_ID
6170        	AND      SEQ.OPERATION_SEQUENCE_ID = OP.OPERATION_SEQUENCE_ID
6171        	AND      RES.BASIS_TYPE in (1,2,3) --4694958
6172        	AND      RES.PLAN_ID = SEQ.PLAN_ID
6173        	AND      RES.ROUTING_SEQUENCE_ID = SEQ.ROUTING_SEQUENCE_ID
6174        	AND      RES.SR_INSTANCE_ID = SEQ.SR_INSTANCE_ID
6175        	AND      RES.OPERATION_SEQUENCE_ID = SEQ.OPERATION_SEQUENCE_ID
6176        	AND      RES.RESOURCE_SEQ_NUM = SEQ.RESOURCE_SEQ_NUM
6177        	AND      NVL(RES.ALTERNATE_NUMBER, 0) = 0 -- bug 1170698
6178        	AND      C1.CALENDAR_DATE = p_requested_date
6179                  -- Bug 3432530 Use RTG instead of MSC_SYSTEM_ITEMS I
6180        	AND      C1.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
6181        	AND      C1.CALENDAR_CODE = l_calendar_code
6182        	AND      C1.EXCEPTION_SET_ID = l_calendar_exception_set_id
6183                  --  Bug 3432530 Use local variables.
6184                  --  In case of common routing, use model's lead times.
6185        	AND      C2.SEQ_NUM = C1.PRIOR_SEQ_NUM - CEIL(((NVL(l_item_fixed_lt,0)+
6186                     NVL(l_item_var_lt,0)* p_requested_quantity) * (1+ l_mso_lead_time_factor))*
6187                        (1-NVL(SEQ.RESOURCE_OFFSET_PERCENT, 0)))
6188                  -- End Bug 3432530
6189        	AND      C2.CALENDAR_CODE = C1.CALENDAR_CODE
6190        	AND      C2.SR_INSTANCE_ID = C1.SR_INSTANCE_ID -- krajan : 2408696  -- cchen
6191        	AND      C2.EXCEPTION_SET_ID = C1.EXCEPTION_SET_ID
6192        	-- krajan: 2408696 - agilent
6193         -- AND   	MUC1.UOM_CODE = l_uom_code
6194        	-- AND   	MUC1.INVENTORY_ITEM_ID = 0
6195        	-- AND   	MUC2.UOM_CLASS = MUC1.UOM_CLASS
6196        	-- AND   	MUC2.INVENTORY_ITEM_ID = 0
6197        	-- AND   	MUC2.UOM_CODE = RES.UOM_CODE
6198         AND      RES.UOM_CODE = l_uom_code
6199        	AND      DR.PLAN_ID = RTG.PLAN_ID
6200        	AND      DR.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
6201        	AND      DR.ORGANIZATION_ID = RTG.ORGANIZATION_ID
6202        	AND      DR.RESOURCE_ID = RES.RESOURCE_ID
6203        	AND      DR.DEPARTMENT_ID = OP.DEPARTMENT_ID
6204         -- performance dsting remove nvl from dr.ctp_flag
6205        	AND      DR.CTP_FLAG = 1
6206        	--bug3601223 get the operations that lie on primary path
6207        	AND      (l_network_scheduling_method = 2
6208        	OR
6209        	         OP.OPERATION_SEQUENCE_ID IN
6210                               ( SELECT FROM_OP_SEQ_ID
6211                                 FROM  MSC_OPERATION_NETWORKS
6212                                 WHERE  PLAN_ID = p_plan_id
6213 				AND    SR_INSTANCE_ID = p_instance_id
6214 				AND    ROUTING_SEQUENCE_ID = l_routing_seq_id
6215 --				AND    ROUTING_SEQUENCE_ID = p_routing_seq_id
6216 				AND    TRANSITION_TYPE = 1
6217 
6218                                 UNION ALL
6219 
6220                                 SELECT TO_OP_SEQ_ID
6221 				FROM  MSC_OPERATION_NETWORKS
6222 				WHERE  PLAN_ID = p_plan_id
6223 				AND    SR_INSTANCE_ID = p_instance_id
6224 				AND    ROUTING_SEQUENCE_ID = l_routing_seq_id
6225 --				AND    ROUTING_SEQUENCE_ID = p_routing_seq_id
6226 				AND    TRANSITION_TYPE = 1
6227 			      )
6228 	          )
6229         UNION -- ALL
6230          -- Obtain Option Dependent Routing
6231        	SELECT   /*+ ordered */  DISTINCT DR.DEPARTMENT_ID department_id,
6232                  -- Distinct: Bug 3432530 For CTO we obtain the Op. Seq. related data only once.
6233                 	DR.OWNING_DEPARTMENT_ID owning_department_id,
6234                 	DR.RESOURCE_ID resource_id,
6235                 	RES.BASIS_TYPE basis_type,
6236                         --bug 3766224: Do not chnage usage for lot based resource
6237                         ROUND(DECODE(RES.BASIS_TYPE, 2, NVL(RES.RESOURCE_USAGE,0),
6238                 	(NVL(RES.RESOURCE_USAGE,0)*
6239                         -- krajan : 2408696
6240                         -- MUC2.CONVERSION_RATE/MUC1.CONVERSION_RATE, 0*
6241                         --bug3601223 Only if network_scheduling_method is planning percent then use % else 100%
6242                         Decode(l_network_scheduling_method,2,
6243                               (NVL(OP.NET_PLANNING_PERCENT,100)/100),1)
6244                         /DECODE(OP.REVERSE_CUMULATIVE_YIELD,0,1,NVL(OP.REVERSE_CUMULATIVE_YIELD,1)))
6245                         /(Decode (nvl (MSC_ATP_PVT.G_ORG_INFO_REC.org_type,MSC_ATP_PVT.DISCRETE_ORG), MSC_ATP_PVT.OPM_ORG, --Bug-4694958
6246                                   decode (RES.BASIS_TYPE, 3,
6247                                           NVL(DR.MAX_CAPACITY,1),  nvl(rtg.routing_quantity,1)
6248                                          ),
6249                                   nvl(rtg.routing_quantity,1)
6250                                  )
6251                          )),6) resource_usage, --4694958
6252                             -- Bug 2865389 (ssurendr) routing quantity added for OPM fix.
6253                 	C2.CALENDAR_DATE requested_date,
6254                         --  Bug 3432530 Use local variables.
6255                         --  In case of common routing, use model's lead times.
6256                 	CEIL(((NVL(l_item_fixed_lt,0)+
6257                         NVL(l_item_var_lt,0)* p_requested_quantity) * (1+ l_mso_lead_time_factor))*
6258                         (1-NVL(SEQ.RESOURCE_OFFSET_PERCENT, 0))) lead_time,
6259                         --  End Bug 3432530 Use local variables.
6260                 	NVL((DR.EFFICIENCY/100), 1) efficiency,
6261                 	NVL((DR.UTILIZATION/100), 1) utilization,
6262                         NVL(DR.BATCHABLE_FLAG, 2) batch_flag,
6263                         NVL(DR.MAX_CAPACITY,0) max_capacity,
6264                         --  Bug 3432530 Use local variables.
6265                         --  In case of common routing, use model's item data.
6266                         DECODE(DR.UOM_CLASS_TYPE, 1, l_item_unit_wt, 2, l_item_unit_vol) required_unit_capacity,
6267                         ---bug 1905284
6268                         DECODE(DR.UOM_CLASS_TYPE, 1, l_item_wt_uom, 2, l_item_vol_uom) required_capacity_uom ,
6269                         --  End Bug 3432530 Use local variables.
6270                         DR.UNIT_OF_MEASURE res_uom,
6271                         DR.UOM_CLASS_TYPE res_uom_type,
6272                         OP.STANDARD_OPERATION_CODE std_op_code,
6273                         --diag_atp
6274                         SEQ.RESOURCE_OFFSET_PERCENT resource_offset_percent,
6275                         OP.OPERATION_SEQ_NUM operation_sequence,
6276                         RES.RESOURCE_USAGE actual_resource_usage,
6277                         --NVL(OP.REVERSE_CUMULATIVE_YIELD, 1) reverse_cumulative_yield ,
6278                         DECODE(OP.REVERSE_CUMULATIVE_YIELD,0,1,NVL(OP.REVERSE_CUMULATIVE_YIELD, 1)) reverse_cumulative_yield ,--4694958
6279                         DR.Department_code department_code,
6280                         DR.resource_code resource_code
6281 
6282        	FROM
6283                 	MSC_CTO_BOM  mcbom1,
6284                         MSC_PROCESS_EFFECTIVITY proc,
6285                         MSC_CTO_BOM  mcbom2,
6286                         -- MSC_SYSTEM_ITEMS I, Bug 3432530 Comment out Join table
6287                 	MSC_ROUTINGS RTG,
6288                 	MSC_ROUTING_OPERATIONS OP,
6289                         MSC_BOM_COMPONENTS mbc,
6290                         MSC_OPERATION_COMPONENTS  moc,
6291                 	MSC_OPERATION_RESOURCE_SEQS SEQ,
6292                 	MSC_OPERATION_RESOURCES RES,
6293                         MSC_DEPARTMENT_RESOURCES DR, -- this is the sharing dept
6294                 	MSC_CALENDAR_DATES C1,
6295                 	MSC_CALENDAR_DATES C2
6296 
6297        	WHERE    mcbom1.session_id = MSC_ATP_PVT.G_SESSION_ID
6298         AND      mcbom1.sr_instance_id = p_instance_id
6299                  -- Bug 3358981 line is a model then include,
6300         AND      (mcbom1.ATO_PARENT_MODEL_LINE_ID = l_parent_line_id OR mcbom1.line_id = l_parent_line_id)
6301                  -- get all lines having the same parent model End Bug 3358981
6302         AND      mcbom1.bom_item_type in (1, 2)
6303         AND      mcbom1.inventory_item_id =
6304                        decode(MSC_ATP_PVT.G_ITEM_INFO_REC.bom_item_type,
6305                               1, mcbom1.inventory_item_id,
6306                               2, l_inventory_item_id)
6307         --AND      (mcbom1.parent_line_id = p_line_identifier or
6308                     -- Handle situation when parent_line_id is null.
6309                     -- Basic thing is that this section should handle all cases.
6310         --            mcbom1.inventory_item_id = l_inventory_item_id )
6311         -- Join to msc_process_effectivity
6312         AND      proc.plan_id = p_plan_id
6313         AND      proc.sr_instance_id = mcbom1.sr_instance_id
6314         AND      proc.organization_id = p_organization_id
6315         AND      proc.item_id  = mcbom1.inventory_item_id
6316                  -- Ensure that only items that have a common routing are processed with
6317                  -- the model. OC Items having a separate routing will be processed separately.
6318                  -- This check below with decode on the left side will be removed while
6319                  -- ones below that achieve the same thing retained if performance is an issue.
6320         AND      decode(MSC_ATP_PVT.G_ITEM_INFO_REC.bom_item_type,  1, -- bom_item_type for model
6321                          NVL(proc.routing_sequence_id, l_routing_seq_id),
6322 --                         NVL(proc.routing_sequence_id, p_routing_seq_id),
6323                          proc.routing_sequence_id  -- all other cases including option_classes
6324                        ) =  l_routing_seq_id
6325         -- Quantity filter
6326         AND      mcbom1.quantity BETWEEN NVL(proc.minimum_quantity,0) AND
6327                   DECODE(NVL(proc.maximum_quantity,0),0,99999999,proc.maximum_quantity)
6328         -- Date Filter
6329         -- effective date should be greater than or equal to greatest of PTF date,
6330         -- sysdate and start date, disable date
6331         -- should be less than or equal to greatest of PTF date, sysdate and start date
6332         -- Note p_requested_date is used instead of C2.calendar_date currently,
6333         -- since p_requested_date is used in MSC_ATP_PROC.get_process_effectivity
6334         -- and also from performance considerations.
6335         AND   TRUNC(proc.effectivity_date) <=
6336                           TRUNC(GREATEST(p_requested_date, sysdate, MSC_ATP_PVT.G_PTF_DATE))
6337         AND   TRUNC(NVL(proc.disable_date,GREATEST(p_requested_date, sysdate, MSC_ATP_PVT.G_PTF_DATE)+1))
6338                > TRUNC(GREATEST(p_requested_date, sysdate, MSC_ATP_PVT.G_PTF_DATE))
6339         -- Join again to msc_cto_bom to obtain the components as well.
6340         AND     mcbom2.sr_instance_id = mcbom1.sr_instance_id
6341         AND     mcbom2.session_id = mcbom1.session_id
6342         AND     mcbom2.ato_parent_model_line_id = mcbom1.ATO_PARENT_MODEL_LINE_ID
6343         AND     NVL(mcbom2.parent_line_id, l_parent_line_id) = mcbom1.line_id
6344         -- to obtain all option classes that have a common routing.
6345         -- Get the routing
6346        	AND      RTG.PLAN_ID = proc.plan_id
6347        	AND      RTG.SR_INSTANCE_ID =  proc.sr_instance_id -- Qry streamline 3358981
6348        	AND      RTG.ORGANIZATION_ID = proc.organization_id
6349        	AND      RTG.ROUTING_SEQUENCE_ID =  NVL(proc.routing_sequence_id,
6350                                                  RTG.ROUTING_SEQUENCE_ID)
6351                  -- Bug 3432530
6352                  -- Comment out join conditions for msc_system_items
6353                   -- 3358981 Eliminate semi cartesian product, streamline query.
6354         AND      RTG.assembly_item_id = DECODE (proc.routing_sequence_id, NULL,
6355                                                proc.item_id, l_inventory_item_id )
6356         --(ssurendr) Bug 2865389 Removed condition for Alternate Routing designator as
6357         --we are accessing Routing by Routing sequance id.
6358         --We are Driving by routing table for performance gains.
6359        	--AND      RTG.ALTERNATE_ROUTING_DESIGNATOR IS NULL
6360         -- Join to system items
6361         -- AND      I.PLAN_ID = RTG.PLAN_ID
6362         -- AND      I.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
6363         -- AND      I.ORGANIZATION_ID = RTG.ORGANIZATION_ID
6364                  -- Bug 3358981
6365         -- AND      I.INVENTORY_ITEM_ID = RTG.assembly_item_id
6366                  -- 3358981 Eliminate semi cartesian product, streamline query.
6367                  -- Ensure that only items that have a common routing are processed with
6368                  -- the model. OC Items having a separate routing will be processed separately.
6369         -- AND      I.INVENTORY_ITEM_ID = DECODE (proc.routing_sequence_id, NULL,
6370         --                        RTG.assembly_item_id, l_inventory_item_id ) -- model's item_id
6371                  -- End Bug 3358981
6372                  -- End Bug 3432530
6373         --  Get all operations for the routing
6374        	AND      OP.PLAN_ID = RTG.PLAN_ID
6375        	AND      OP.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
6376        	AND      OP.ROUTING_SEQUENCE_ID = RTG.ROUTING_SEQUENCE_ID
6377                  -- filter only on those components that are in the pseudo bom
6378                  -- AND OP.option_dependent_flag = 1 --
6379         /* Operation is of type Event (Do not select process) */
6380         and      NVL(OP.operation_type,1 ) = 1
6381         /* rajjain 3008611
6382          * effective date should be greater than or equal to greatest of PTF date, sysdate and start date
6383          * disable date should be less than or equal to greatest of PTF date, sysdate and start date*/
6384        	AND      TRUNC(NVL(OP.DISABLE_DATE, GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE)+1)) >
6385         	         	TRUNC(GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE))
6386        	AND      TRUNC(OP.EFFECTIVITY_DATE) <=
6387          	      	TRUNC(GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE)) -- bug 1404312
6388         -- Validate Model's BOM in sales order with model's bom in manufacturing org.
6389         AND     mbc.sr_instance_id = RTG.sr_instance_id -- Qry streamline 3358981
6390         AND     mbc.plan_id =  RTG.plan_id
6391         AND     mbc.organization_id = RTG.organization_id
6392                 -- Bug 3358981
6393                 -- Ensure that only items that have a common routing are processed with
6394                 -- the model. OC Items having a separate routing will be processed separately.
6395         AND     mbc.bill_sequence_id = DECODE(proc.routing_sequence_id, NULL,
6396                                                 proc.bill_sequence_id, p_bill_seq_id)
6397                 -- End Bug 3358981
6398         AND     mbc.using_assembly_id = RTG.assembly_item_id -- Qry streamline 3358981
6399         AND      TRUNC(NVL(MBC.DISABLE_DATE, GREATEST(C2.CALENDAR_DATE,
6400                          sysdate, MSC_ATP_PVT.G_PTF_DATE)+1)) >
6401                TRUNC(GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE))
6402                      AND      TRUNC(MBC.EFFECTIVITY_DATE) <=
6403                TRUNC(GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE))
6404         AND    mbc.inventory_item_id  = mcbom2.inventory_item_id
6405                  -- Optional Items selected in the Sales Order
6406                  -- Select the option dependent operations which are needed
6407                  -- for the configuration
6408         -- Join to determine all the operations
6409         and      moc.plan_id = mbc.plan_id
6410         and      moc.sr_instance_id = mbc.sr_instance_id
6411         and      moc.organization_id = mbc.organization_id
6412         and      moc.bill_sequence_id = mbc.bill_sequence_id
6413         and      moc.component_sequence_id = mbc.component_sequence_id
6414         and      moc.routing_sequence_id = rtg.routing_sequence_id
6415         and      moc.operation_sequence_id = OP.operation_sequence_id
6416         -- Obtain the Resource Seq numbers.
6417        	AND      SEQ.PLAN_ID = OP.PLAN_ID
6418        	AND      SEQ.ROUTING_SEQUENCE_ID  = OP.ROUTING_SEQUENCE_ID
6419        	AND      SEQ.SR_INSTANCE_ID = OP.SR_INSTANCE_ID
6420        	AND      SEQ.OPERATION_SEQUENCE_ID = OP.OPERATION_SEQUENCE_ID
6421        	AND      RES.BASIS_TYPE in (1,2,3) --4694958
6422        	AND      RES.PLAN_ID = SEQ.PLAN_ID
6423        	AND      RES.ROUTING_SEQUENCE_ID = SEQ.ROUTING_SEQUENCE_ID
6424        	AND      RES.SR_INSTANCE_ID = SEQ.SR_INSTANCE_ID
6425        	AND      RES.OPERATION_SEQUENCE_ID = SEQ.OPERATION_SEQUENCE_ID
6426        	AND      RES.RESOURCE_SEQ_NUM = SEQ.RESOURCE_SEQ_NUM
6427        	AND      NVL(RES.ALTERNATE_NUMBER, 0) = 0 -- bug 1170698
6428        	AND      C1.CALENDAR_DATE = p_requested_date
6429                  -- Bug 3432530 Use RTG instead of MSC_SYSTEM_ITEMS I
6430        	AND      C1.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
6431        	AND      C1.CALENDAR_CODE = l_calendar_code
6432        	AND      C1.EXCEPTION_SET_ID = l_calendar_exception_set_id
6433                  --  Bug 3432530 Use local variables.
6434                  --  In case of common routing, use model's lead times.
6435        	AND      C2.SEQ_NUM = C1.PRIOR_SEQ_NUM - CEIL(((NVL(l_item_fixed_lt,0)+
6436                     NVL(l_item_var_lt,0)* p_requested_quantity) * (1+ l_mso_lead_time_factor))*
6437                        (1-NVL(SEQ.RESOURCE_OFFSET_PERCENT, 0)))
6438                  -- End Bug 3432530
6439        	AND      C2.CALENDAR_CODE = C1.CALENDAR_CODE
6440        	AND      C2.SR_INSTANCE_ID = C1.SR_INSTANCE_ID -- krajan : 2408696  -- cchen
6441        	AND      C2.EXCEPTION_SET_ID = C1.EXCEPTION_SET_ID
6442        	-- krajan: 2408696 - agilent
6443         -- AND   	MUC1.UOM_CODE = l_uom_code
6444        	-- AND   	MUC1.INVENTORY_ITEM_ID = 0
6445        	-- AND   	MUC2.UOM_CLASS = MUC1.UOM_CLASS
6446        	-- AND   	MUC2.INVENTORY_ITEM_ID = 0
6447        	-- AND   	MUC2.UOM_CODE = RES.UOM_CODE
6448         AND      RES.UOM_CODE = l_uom_code
6449        	AND      DR.PLAN_ID = RTG.PLAN_ID
6450        	AND      DR.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
6451        	AND      DR.ORGANIZATION_ID = RTG.ORGANIZATION_ID
6452        	AND      DR.RESOURCE_ID = RES.RESOURCE_ID
6453        	AND      DR.DEPARTMENT_ID = OP.DEPARTMENT_ID
6454         -- performance dsting remove nvl from dr.ctp_flag
6455        	AND      DR.CTP_FLAG = 1
6456         --bug3601223  get the operations that lie on primary path
6457        	AND      (l_network_scheduling_method = 2
6458        	OR
6459        	         OP.OPERATION_SEQUENCE_ID IN
6460                               ( SELECT FROM_OP_SEQ_ID
6461                                 FROM  MSC_OPERATION_NETWORKS
6462                                 WHERE  PLAN_ID = p_plan_id
6463 				AND    SR_INSTANCE_ID = p_instance_id
6464 				AND    ROUTING_SEQUENCE_ID = l_routing_seq_id
6465 				AND    TRANSITION_TYPE = 1
6466 
6467                                 UNION ALL
6468 
6469                                 SELECT TO_OP_SEQ_ID
6470 				FROM  MSC_OPERATION_NETWORKS
6471 				WHERE  PLAN_ID = p_plan_id
6472 				AND    SR_INSTANCE_ID = p_instance_id
6473 				AND    ROUTING_SEQUENCE_ID = l_routing_seq_id
6474 				AND    TRANSITION_TYPE = 1
6475 			      )
6476 	          )
6477 	)
6478         ORDER   BY  requested_date,    -- Bug 2313497 Ensure proper order in fetch
6479                     operation_sequence, resource_code;
6480        ELSE -- Not Processing CTO BOM Model or Option Class. ODR
6481       	SELECT  department_id,
6482                 owning_department_id,
6483         	resource_id,
6484            	basis_type,
6485            	resource_usage,
6486            	requested_date,
6487            	lead_time,
6488            	efficiency,
6489            	utilization,
6490                 batch_flag,
6491                 max_capacity,
6492                 required_unit_capacity,
6493                 required_capacity_uom,
6494                 res_uom,
6495                 res_uom_type,
6496                 std_op_code,
6497                 --diag_atp
6498                 resource_offset_percent,
6499                 operation_sequence,
6500                 actual_resource_usage,
6501                 reverse_cumulative_yield,
6502                 department_code,
6503                 resource_code
6504 
6505       	BULK COLLECT INTO l_res_requirements.department_id,
6506                       l_res_requirements.owning_department_id,
6507                       l_res_requirements.resource_id,
6508                       l_res_requirements.basis_type,
6509                       l_res_requirements.resource_usage,
6510                       l_res_requirements.requested_date,
6511                       l_res_requirements.lead_time,
6512                       l_res_requirements.efficiency,
6513                       l_res_requirements.utilization,
6514                       --- these columns have been added for resource batching
6515                       l_res_requirements.batch_flag,
6516                       l_res_requirements.max_capacity,
6517                       l_res_requirements.required_unit_capacity,
6518                       l_res_requirements.required_capacity_uom,
6519                       l_res_requirements.res_uom,
6520                       l_res_requirements.res_uom_type,
6521                       l_res_requirements.std_op_code,
6522                       ---diag_atp
6523                       l_res_requirements.resource_offset_percent,
6524                       l_res_requirements.operation_sequence,
6525                       l_res_requirements.actual_resource_usage,
6526                       l_res_requirements.reverse_cumulative_yield,
6527                       l_res_requirements.department_code,
6528                       l_res_requirements.resource_code
6529       	FROM (
6530        	SELECT   /*+ ordered */  DR.DEPARTMENT_ID department_id,
6531                 	DR.OWNING_DEPARTMENT_ID owning_department_id,
6532                 	DR.RESOURCE_ID resource_id,
6533                 	RES.BASIS_TYPE basis_type,
6534                         --bug 3766224: Do not chnage usage for lot based resource
6535                         ROUND(DECODE(RES.BASIS_TYPE, 2, NVL(RES.RESOURCE_USAGE,0),
6536                 	(NVL(RES.RESOURCE_USAGE,0)*
6537                         -- krajan : 2408696
6538                         -- MUC2.CONVERSION_RATE/MUC1.CONVERSION_RATE, 0*
6539                         --bug3601223 Only if network_scheduling_method is planning percent then use % else 100%
6540                         Decode(l_network_scheduling_method,2,
6541                         (NVL(OP.NET_PLANNING_PERCENT,100)/100),1)
6542                         /DECODE(OP.REVERSE_CUMULATIVE_YIELD,0,1,NVL(OP.REVERSE_CUMULATIVE_YIELD,1)))
6543                         /(Decode (nvl (MSC_ATP_PVT.G_ORG_INFO_REC.org_type,MSC_ATP_PVT.DISCRETE_ORG), MSC_ATP_PVT.OPM_ORG, --Bug-4694958
6544                                   decode (RES.BASIS_TYPE, 3,
6545                                           NVL(DR.MAX_CAPACITY,1),  nvl(rtg.routing_quantity,1)
6546                                          ),
6547                                   nvl(rtg.routing_quantity,1)
6548                                  )
6549                          )),6) resource_usage, --4694958
6550                             -- Bug 2865389 (ssurendr) routing quantity added for OPM fix.
6551                 	C2.CALENDAR_DATE requested_date,
6552                 	CEIL(((NVL(I.FIXED_LEAD_TIME,0)+
6553                         NVL(I.VARIABLE_LEAD_TIME,0)* p_requested_quantity) * (1+ l_mso_lead_time_factor))*
6554                         (1-NVL(SEQ.RESOURCE_OFFSET_PERCENT, 0))) lead_time,
6555                 	NVL((DR.EFFICIENCY/100), 1) efficiency,
6556                 	NVL((DR.UTILIZATION/100), 1) utilization,
6557                         NVL(DR.BATCHABLE_FLAG, 2) batch_flag,
6558                         NVL(DR.MAX_CAPACITY,0) max_capacity,
6559                         DECODE(DR.UOM_CLASS_TYPE, 1, I.UNIT_WEIGHT, 2, I.UNIT_VOLUME) required_unit_capacity,
6560                         ---bug 1905284
6561                         DECODE(DR.UOM_CLASS_TYPE, 1, I.WEIGHT_UOM, 2, I.VOLUME_UOM) required_capacity_uom ,
6562                         DR.UNIT_OF_MEASURE res_uom,
6563                         DR.UOM_CLASS_TYPE res_uom_type,
6564                         OP.STANDARD_OPERATION_CODE std_op_code,
6565                         --diag_atp
6566                         SEQ.RESOURCE_OFFSET_PERCENT resource_offset_percent,
6567                         OP.OPERATION_SEQ_NUM operation_sequence,
6568                         RES.RESOURCE_USAGE actual_resource_usage,
6569                         --NVL(OP.REVERSE_CUMULATIVE_YIELD, 1) reverse_cumulative_yield ,
6570                         DECODE(OP.REVERSE_CUMULATIVE_YIELD,0,1,NVL(OP.REVERSE_CUMULATIVE_YIELD, 1)) reverse_cumulative_yield ,--4694958
6571                         DR.Department_code department_code,
6572                         DR.resource_code resource_code
6573 
6574        	FROM
6575                         -- krajan : 2408696
6576                         --agilent chnages: since plan already store the data in right uom, we dont need to convert it
6577                 	-- MSC_UOM_CONVERSIONS MUC2,
6578                 	-- MSC_UOM_CONVERSIONS MUC1,
6579                 	MSC_SYSTEM_ITEMS  I,
6580                 	MSC_ROUTINGS RTG,
6581                 	MSC_ROUTING_OPERATIONS OP,
6582                 	MSC_OPERATION_RESOURCE_SEQS SEQ,
6583                 	MSC_OPERATION_RESOURCES RES,
6584                         MSC_DEPARTMENT_RESOURCES DR, -- this is the sharing dept
6585                 	MSC_CALENDAR_DATES C1,
6586                 	MSC_CALENDAR_DATES C2
6587 
6588        	WHERE    I.PLAN_ID = RTG.PLAN_ID
6589        	AND      I.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
6590        	AND      I.INVENTORY_ITEM_ID = RTG.ASSEMBLY_ITEM_ID
6591        	AND      I.ORGANIZATION_ID = RTG.ORGANIZATION_ID
6592        	AND      RTG.PLAN_ID = p_plan_id
6593        	AND      RTG.SR_INSTANCE_ID = p_instance_id
6594        	AND      RTG.ORGANIZATION_ID = p_organization_id
6595        	AND      RTG.ROUTING_SEQUENCE_ID = l_routing_seq_id
6596         --(ssurendr) Bug 2865389 Removed condition for Alternate Routing designator as
6597         --we are accessing Routing by Routing sequance id.
6598         --We are Driving by routing table for performance gains.
6599        	--AND      RTG.ALTERNATE_ROUTING_DESIGNATOR IS NULL
6600        	AND      OP.PLAN_ID = RTG.PLAN_ID
6601        	AND      OP.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
6602        	AND      OP.ROUTING_SEQUENCE_ID = RTG.ROUTING_SEQUENCE_ID
6603         /* rajjain 3008611
6604          * effective date should be greater than or equal to greatest of PTF date, sysdate and start date
6605          * disable date should be less than or equal to greatest of PTF date, sysdate and start date*/
6606        	AND      TRUNC(NVL(OP.DISABLE_DATE, GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE)+1)) >
6607         	         	TRUNC(GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE))
6608        	AND      TRUNC(OP.EFFECTIVITY_DATE) <=
6609          	      	TRUNC(GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE)) -- bug 1404312
6610        	AND      SEQ.PLAN_ID = OP.PLAN_ID
6611        	AND      SEQ.ROUTING_SEQUENCE_ID  = OP.ROUTING_SEQUENCE_ID
6612        	AND      SEQ.SR_INSTANCE_ID = OP.SR_INSTANCE_ID
6613        	AND      SEQ.OPERATION_SEQUENCE_ID = OP.OPERATION_SEQUENCE_ID
6614        	AND      RES.BASIS_TYPE in (1,2,3) --4694958
6615        	AND      RES.PLAN_ID = SEQ.PLAN_ID
6616        	AND      RES.ROUTING_SEQUENCE_ID = SEQ.ROUTING_SEQUENCE_ID
6617        	AND      RES.SR_INSTANCE_ID = SEQ.SR_INSTANCE_ID
6618        	AND      RES.OPERATION_SEQUENCE_ID = SEQ.OPERATION_SEQUENCE_ID
6619        	AND      RES.RESOURCE_SEQ_NUM = SEQ.RESOURCE_SEQ_NUM
6620        	AND      NVL(RES.ALTERNATE_NUMBER, 0) = 0 -- bug 1170698
6621        	AND      C1.CALENDAR_DATE = p_requested_date
6622        	AND      C1.SR_INSTANCE_ID = I.SR_INSTANCE_ID
6623        	AND      C1.CALENDAR_CODE = l_calendar_code
6624        	AND      C1.EXCEPTION_SET_ID = l_calendar_exception_set_id
6625        	AND      C2.SEQ_NUM = C1.PRIOR_SEQ_NUM - CEIL(((NVL(I.FIXED_LEAD_TIME,0)+
6626                         NVL(I.VARIABLE_LEAD_TIME,0)* p_requested_quantity) * (1+ l_mso_lead_time_factor))*
6627                         (1-NVL(SEQ.RESOURCE_OFFSET_PERCENT, 0)))
6628        	AND      C2.CALENDAR_CODE = C1.CALENDAR_CODE
6629        	AND      C2.SR_INSTANCE_ID = C1.SR_INSTANCE_ID -- krajan : 2408696  -- cchen
6630        	AND      C2.EXCEPTION_SET_ID = C1.EXCEPTION_SET_ID
6631        	-- krajan: 2408696 - agilent
6632         -- AND   	MUC1.UOM_CODE = l_uom_code
6633        	-- AND   	MUC1.INVENTORY_ITEM_ID = 0
6634        	-- AND   	MUC2.UOM_CLASS = MUC1.UOM_CLASS
6635        	-- AND   	MUC2.INVENTORY_ITEM_ID = 0
6636        	-- AND   	MUC2.UOM_CODE = RES.UOM_CODE
6637         AND      RES.UOM_CODE = l_uom_code
6638        	AND      DR.PLAN_ID = I.PLAN_ID
6639        	AND      DR.SR_INSTANCE_ID = I.SR_INSTANCE_ID
6640        	AND      DR.ORGANIZATION_ID = I.ORGANIZATION_ID
6641        	AND      DR.RESOURCE_ID = RES.RESOURCE_ID
6642        	AND      DR.DEPARTMENT_ID = OP.DEPARTMENT_ID
6643         -- performance dsting remove nvl from dr.ctp_flag
6644        	AND      DR.CTP_FLAG = 1
6645        	--bug3601223  get the operations that lie on primary path
6646        	AND      (l_network_scheduling_method = 2
6647        	OR
6648        	          OP.OPERATION_SEQUENCE_ID IN
6649                               ( SELECT FROM_OP_SEQ_ID
6650                                 FROM  MSC_OPERATION_NETWORKS
6651                                 WHERE  PLAN_ID = p_plan_id
6652 				AND    SR_INSTANCE_ID = p_instance_id
6653 				AND    ROUTING_SEQUENCE_ID = l_routing_seq_id
6654 				AND    TRANSITION_TYPE = 1
6655 
6656                                 UNION ALL
6657 
6658                                 SELECT TO_OP_SEQ_ID
6659 				FROM  MSC_OPERATION_NETWORKS
6660 				WHERE  PLAN_ID = p_plan_id
6661 				AND    SR_INSTANCE_ID = p_instance_id
6662 				AND    ROUTING_SEQUENCE_ID = l_routing_seq_id
6663 				AND    TRANSITION_TYPE = 1
6664 			      )
6665 	           )
6666 
6667 	)
6668         ORDER   BY  requested_date,   -- Bug 2313497 Ensure proper order in fetch
6669                     operation_sequence, resource_code;
6670        END IF;
6671        -- End CTO Option Dependent Resources ODR
6672       ELSE -- traditional routing
6673        -- CTO Option Dependent Resources
6674        -- Option Dependent Routing ODR Determination
6675        IF MSC_ATP_PVT.G_ITEM_INFO_REC.bom_item_type in (1, 2) THEN
6676       	 SELECT DISTINCT            -- collapse common into one in case union all is used.
6677                 -- Uncomment: Bug 3432530 For CTO we obtain the Op. Seq. related data only once.
6678                 department_id,
6679            	owning_department_id,
6680            	resource_id,
6681            	basis_type,
6682            	resource_usage,
6683            	requested_date,
6684            	lead_time,
6685            	efficiency,
6686            	utilization,
6687                 batch_flag,
6688                 max_capacity,
6689                 required_unit_capacity,
6690                 required_capacity_uom,
6691                 res_uom,
6692                 res_uom_type,
6693                 std_op_code,
6694                 --diag_atp
6695                 resource_offset_percent,
6696                 operation_sequence,
6697                 actual_resource_usage,
6698                 reverse_cumulative_yield,
6699                 department_code,
6700                 resource_code
6701       	BULK COLLECT INTO l_res_requirements.department_id,
6702                       	l_res_requirements.owning_department_id,
6703                       	l_res_requirements.resource_id,
6704                       	l_res_requirements.basis_type,
6705                       	l_res_requirements.resource_usage,
6706                       	l_res_requirements.requested_date,
6707                       	l_res_requirements.lead_time,
6708                       	l_res_requirements.efficiency,
6709                       	l_res_requirements.utilization,
6710                         --- the following columns are added for resource batching
6711                         l_res_requirements.batch_flag,
6712                         l_res_requirements.max_capacity,
6713                         l_res_requirements.required_unit_capacity,
6714                         l_res_requirements.required_capacity_uom,
6715 			l_res_requirements.res_uom,
6716                         l_res_requirements.res_uom_type,
6717                         l_res_requirements.std_op_code,
6718                         ---diag_atp
6719                         l_res_requirements.resource_offset_percent,
6720                         l_res_requirements.operation_sequence,
6721                         l_res_requirements.actual_resource_usage,
6722                         l_res_requirements.reverse_cumulative_yield,
6723                         l_res_requirements.department_code,
6724                         l_res_requirements.resource_code
6725 
6726       	FROM (
6727          -- First select mandatory operations
6728          -- for common routing cases the mandatory for option classes
6729          -- will be clubbed together with the model.
6730        	SELECT  /*+ ordered */ DISTINCT DR.DEPARTMENT_ID department_id,
6731                 -- Distinct: Bug 3432530 For CTO we obtain the Op. Seq. related data only once.
6732                 	DR.OWNING_DEPARTMENT_ID owning_department_id,
6733                 	DR.RESOURCE_ID resource_id,
6734                 	RES.BASIS_TYPE basis_type,
6735                         --bug 3766224: Do not chnage usage for lot based resource
6736                         ROUND(DECODE(RES.BASIS_TYPE, 2, NVL(RES.RESOURCE_USAGE,0),
6737                 	(NVL(RES.RESOURCE_USAGE,0)
6738                         -- krajan : 2408696
6739                         -- MUC2.CONVERSION_RATE/MUC1.CONVERSION_RATE, 0
6740                         /DECODE(OP.REVERSE_CUMULATIVE_YIELD,0,1,NVL(OP.REVERSE_CUMULATIVE_YIELD,1)))
6741                         /(Decode (nvl (MSC_ATP_PVT.G_ORG_INFO_REC.org_type,MSC_ATP_PVT.DISCRETE_ORG), MSC_ATP_PVT.OPM_ORG, --Bug-4694958
6742                                   decode (RES.BASIS_TYPE, 3,
6743                                           NVL(DR.MAX_CAPACITY,1),  nvl(rtg.routing_quantity,1)
6744                                          ),
6745                                   nvl(rtg.routing_quantity,1)
6746                                  )
6747                          )),6) resource_usage, --4694958
6748                             -- Bug 2865389 (ssurendr) routing quantity added for OPM fix.
6749 			--(NVL(OP.NET_PLANNING_PERCENT,100)/100)/NVL(OP.REVERSE_CUMULATIVE_YIELD,1) resource_usage,
6750                 	C2.CALENDAR_DATE requested_date,
6751                         --  Bug 3432530 Use local variables.
6752                         --  In case of common routing, use model's lead times.
6753                 	CEIL(((NVL(l_item_fixed_lt,0)+
6754                         NVL(l_item_var_lt,0)* p_requested_quantity) * (1+ l_mso_lead_time_factor))*
6755                         (1-NVL(SEQ.RESOURCE_OFFSET_PERCENT, 0))) lead_time,
6756                         --  End Bug 3432530 Use local variables.
6757                 	NVL((DR.EFFICIENCY/100), 1) efficiency,
6758                 	NVL((DR.UTILIZATION/100), 1) utilization,
6759                         NVL(DR.BATCHABLE_FLAG, 2) batch_flag,
6760                         NVL(DR.MAX_CAPACITY,0) max_capacity,
6761                         --  Bug 3432530 Use local variables.
6762                         --  In case of common routing, use model's item data.
6763                         DECODE(DR.UOM_CLASS_TYPE, 1, l_item_unit_wt, 2, l_item_unit_vol) required_unit_capacity,
6764                         ---bug 1905284
6765                         DECODE(DR.UOM_CLASS_TYPE, 1, l_item_wt_uom, 2, l_item_vol_uom) required_capacity_uom ,
6766                         --  End Bug 3432530 Use local variables.
6767                         DR.UNIT_OF_MEASURE res_uom,
6768                         DR.UOM_CLASS_TYPE res_uom_type,
6769                         OP.STANDARD_OPERATION_CODE std_op_code,
6770                         --diag_atp
6771                         SEQ.RESOURCE_OFFSET_PERCENT resource_offset_percent,
6772                         OP.OPERATION_SEQ_NUM operation_sequence,
6773                         RES.RESOURCE_USAGE actual_resource_usage,
6774                         --NVL(OP.REVERSE_CUMULATIVE_YIELD, 1) reverse_cumulative_yield,
6775                         DECODE(OP.REVERSE_CUMULATIVE_YIELD,0,1,NVL(OP.REVERSE_CUMULATIVE_YIELD, 1)) reverse_cumulative_yield,--4694958
6776                         DR.Department_code Department_code,
6777                         DR.Resource_code Resource_code
6778         FROM
6779                 	MSC_CTO_BOM  mcbom1,
6780                         -- MSC_SYSTEM_ITEMS I, Bug 3432530 Comment out Join table
6781                 	MSC_ROUTINGS RTG,
6782                 	MSC_ROUTING_OPERATIONS OP,
6783                 	MSC_OPERATION_RESOURCE_SEQS SEQ,
6784                 	MSC_OPERATION_RESOURCES RES,
6785                         MSC_DEPARTMENT_RESOURCES DR, -- this is the sharing dept
6786                 	MSC_CALENDAR_DATES C1,
6787                 	MSC_CALENDAR_DATES C2
6788 
6789        	WHERE    mcbom1.session_id = MSC_ATP_PVT.G_SESSION_ID
6790         AND      mcbom1.sr_instance_id = p_instance_id
6791                  -- Bug 3358981 line is a model then include,
6792         AND      (mcbom1.ATO_PARENT_MODEL_LINE_ID = l_parent_line_id OR mcbom1.line_id = l_parent_line_id)
6793                  -- get all lines having the same parent model End Bug 3358981
6794         AND      (  --mcbom1.parent_line_id = p_line_identifier or
6795                     -- Handle situation when parent_line_id is null.
6796                     -- Basic thing is that this section should handle all cases.
6797                     mcbom1.inventory_item_id = l_inventory_item_id )
6798         AND      mcbom1.quantity <> 0
6799         -- Get the routing
6800        	AND      RTG.PLAN_ID = p_plan_id
6801        	AND      RTG.SR_INSTANCE_ID = mcbom1.sr_instance_id
6802        	AND      RTG.ORGANIZATION_ID = p_organization_id
6803        	AND      RTG.ROUTING_SEQUENCE_ID = l_routing_seq_id -- For common routing this will be null.
6804                  -- Bug 3432530
6805                  -- Comment out join conditions for msc_system_items
6806                   -- 3358981 Eliminate semi cartesian product, streamline query.
6807         AND      RTG.assembly_item_id = mcbom1.inventory_item_id
6808         -- Join to system items
6809         -- AND      I.PLAN_ID = RTG.PLAN_ID
6810         -- AND      I.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
6811         -- AND      I.ORGANIZATION_ID = RTG.ORGANIZATION_ID
6812         -- AND      I.INVENTORY_ITEM_ID = RTG.assembly_item_id
6813                  -- 3358981 Eliminate semi cartesian product, streamline query.
6814         -- AND      I.INVENTORY_ITEM_ID = l_inventory_item_id
6815                  -- End Bug 3432530
6816         --(ssurendr) Bug 2865389 Removed condition for Alternate Routing designator as
6817         --we are accessing Routing by Routing sequance id.
6818         --We are Driving by routing table for performance gains.
6819        	--AND      RTG.ALTERNATE_ROUTING_DESIGNATOR IS NULL
6820         --  Get all operations for the routing
6821        	AND      OP.PLAN_ID = RTG.PLAN_ID
6822        	AND      OP.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
6823        	AND      OP.ROUTING_SEQUENCE_ID = RTG.ROUTING_SEQUENCE_ID
6824          /* Operation is of type Event (Do not select process) */
6825         and      NVL(OP.operation_type,1 ) = 1
6826         /* rajjain 3008611
6827          * effective date should be greater than or equal to greatest of PTF date, sysdate and start date
6828          * disable date should be less than or equal to greatest of PTF date, sysdate and start date*/
6829        	AND      TRUNC(NVL(OP.DISABLE_DATE, GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE)+1)) >
6830                   	TRUNC(GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE))
6831        	AND      TRUNC(OP.EFFECTIVITY_DATE) <=
6832                   	TRUNC(GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE)) -- bug 1404312
6833                  -- SMCs/Mandatory Operations
6834         and     OP.option_dependent_flag = 2
6835                  -- for the configuration
6836         -- Obtain the Resource Seq numbers.
6837        	AND      SEQ.PLAN_ID = OP.PLAN_ID
6838        	AND      SEQ.ROUTING_SEQUENCE_ID  = OP.ROUTING_SEQUENCE_ID
6839        	AND      SEQ.SR_INSTANCE_ID = OP.SR_INSTANCE_ID
6840        	AND      SEQ.OPERATION_SEQUENCE_ID = OP.OPERATION_SEQUENCE_ID
6841        	AND      RES.BASIS_TYPE in (1,2,3) --4694958
6842        	AND      RES.PLAN_ID = SEQ.PLAN_ID
6843        	AND      RES.ROUTING_SEQUENCE_ID = SEQ.ROUTING_SEQUENCE_ID
6844        	AND      RES.SR_INSTANCE_ID = SEQ.SR_INSTANCE_ID
6845        	AND      RES.OPERATION_SEQUENCE_ID = SEQ.OPERATION_SEQUENCE_ID
6846        	AND      RES.RESOURCE_SEQ_NUM = SEQ.RESOURCE_SEQ_NUM
6847        	AND      NVL(RES.ALTERNATE_NUMBER, 0) = 0 -- bug 1170698
6848        	AND      C1.CALENDAR_DATE = p_requested_date
6849                  -- Bug 3432530 Use RTG instead of MSC_SYSTEM_ITEMS I
6850        	AND      C1.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
6851        	AND      C1.CALENDAR_CODE = l_calendar_code
6852        	AND      C1.EXCEPTION_SET_ID = l_calendar_exception_set_id
6853                  --  Bug 3432530 Use local variables.
6854                  --  In case of common routing, use model's lead times.
6855        	AND      C2.SEQ_NUM = C1.PRIOR_SEQ_NUM - CEIL(((NVL(l_item_fixed_lt,0)+
6856                     NVL(l_item_var_lt,0)* p_requested_quantity) * (1+ l_mso_lead_time_factor))*
6857                        (1-NVL(SEQ.RESOURCE_OFFSET_PERCENT, 0)))
6858                  -- End Bug 3432530
6859        	AND      C2.CALENDAR_CODE = C1.CALENDAR_CODE
6860        	AND      C2.SR_INSTANCE_ID = C1.SR_INSTANCE_ID -- krajan : 2408696
6861        	AND      C2.EXCEPTION_SET_ID = C1.EXCEPTION_SET_ID
6862        	-- krajan : 2408696
6863         -- AND   	MUC1.UOM_CODE = l_uom_code
6864        	-- AND   	MUC1.INVENTORY_ITEM_ID = 0
6865        	-- AND   	MUC2.UOM_CLASS = MUC1.UOM_CLASS
6866        	-- AND   	MUC2.INVENTORY_ITEM_ID = 0
6867        	-- AND   	MUC2.UOM_CODE = RES.UOM_CODE
6868         AND      RES.UOM_CODE = l_uom_code
6869        	AND      DR.PLAN_ID = RTG.PLAN_ID
6870        	AND      DR.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
6871         AND     DR.ORGANIZATION_ID = RTG.ORGANIZATION_ID
6872        	AND      DR.RESOURCE_ID = RES.RESOURCE_ID
6873        	AND      DR.DEPARTMENT_ID = OP.DEPARTMENT_ID
6874 	-- performance dsting remove nvl from dr.ctp_flag
6875        	AND      DR.CTP_FLAG = 1
6876        	UNION ALL
6877          -- Obtain Option Dependent Routing
6878        	SELECT  /*+ ordered */ DISTINCT DR.DEPARTMENT_ID department_id,
6879                 -- Distinct: Bug 3432530 For CTO we obtain the Op. Seq. related data only once.
6880                 	DR.OWNING_DEPARTMENT_ID owning_department_id,
6881                 	DR.RESOURCE_ID resource_id,
6882                 	RES.BASIS_TYPE basis_type,
6883                         --bug 3766224: Do not chnage usage for lot based resource
6884                         ROUND(DECODE(RES.BASIS_TYPE, 2, NVL(RES.RESOURCE_USAGE,0),
6885                 	(NVL(RES.RESOURCE_USAGE,0)
6886                         -- krajan : 2408696
6887                         -- MUC2.CONVERSION_RATE/MUC1.CONVERSION_RATE, 0
6888                         /DECODE(OP.REVERSE_CUMULATIVE_YIELD,0,1,NVL(OP.REVERSE_CUMULATIVE_YIELD,1)))
6889                         /(Decode (nvl (MSC_ATP_PVT.G_ORG_INFO_REC.org_type,MSC_ATP_PVT.DISCRETE_ORG), MSC_ATP_PVT.OPM_ORG, --Bug-4694958
6890                                   decode (RES.BASIS_TYPE, 3,
6891                                           NVL(DR.MAX_CAPACITY,1),  nvl(rtg.routing_quantity,1)
6892                                          ),
6893                                   nvl(rtg.routing_quantity,1)
6894                                  )
6895                          )),6) resource_usage, --4694958
6896                             -- Bug 2865389 (ssurendr) routing quantity added for OPM fix.
6897 			--(NVL(OP.NET_PLANNING_PERCENT,100)/100)/NVL(OP.REVERSE_CUMULATIVE_YIELD,1) resource_usage,
6898                 	C2.CALENDAR_DATE requested_date,
6899                         --  Bug 3432530 Use local variables.
6900                         --  In case of common routing, use model's lead times.
6901                 	CEIL(((NVL(l_item_fixed_lt,0)+
6902                         NVL(l_item_var_lt,0)* p_requested_quantity) * (1+ l_mso_lead_time_factor))*
6903                         (1-NVL(SEQ.RESOURCE_OFFSET_PERCENT, 0))) lead_time,
6904                         --  End Bug 3432530 Use local variables.
6905                 	NVL((DR.EFFICIENCY/100), 1) efficiency,
6906                 	NVL((DR.UTILIZATION/100), 1) utilization,
6907                         NVL(DR.BATCHABLE_FLAG, 2) batch_flag,
6908                         NVL(DR.MAX_CAPACITY,0) max_capacity,
6909                         --  Bug 3432530 Use local variables.
6910                         --  In case of common routing, use model's item data.
6911                         DECODE(DR.UOM_CLASS_TYPE, 1, l_item_unit_wt, 2, l_item_unit_vol) required_unit_capacity,
6912                         ---bug 1905284
6913                         DECODE(DR.UOM_CLASS_TYPE, 1, l_item_wt_uom, 2, l_item_vol_uom) required_capacity_uom ,
6914                         --  End Bug 3432530 Use local variables.
6915                         DR.UNIT_OF_MEASURE res_uom,
6916                         DR.UOM_CLASS_TYPE res_uom_type,
6917                         OP.STANDARD_OPERATION_CODE std_op_code,
6918                         --diag_atp
6919                         SEQ.RESOURCE_OFFSET_PERCENT resource_offset_percent,
6920                         OP.OPERATION_SEQ_NUM operation_sequence,
6921                         RES.RESOURCE_USAGE actual_resource_usage,
6922                         --NVL(OP.REVERSE_CUMULATIVE_YIELD, 1) reverse_cumulative_yield,
6923                         DECODE(OP.REVERSE_CUMULATIVE_YIELD,0,1,NVL(OP.REVERSE_CUMULATIVE_YIELD, 1)) reverse_cumulative_yield,--4694958
6924                         DR.Department_code Department_code,
6925                         DR.Resource_code Resource_code
6926         FROM
6927                 	MSC_CTO_BOM  mcbom1,
6928                         MSC_PROCESS_EFFECTIVITY proc,
6929                         MSC_CTO_BOM  mcbom2,
6930                         -- MSC_SYSTEM_ITEMS I, Bug 3432530 Comment out Join table
6931                 	MSC_ROUTINGS RTG,
6932                 	MSC_ROUTING_OPERATIONS OP,
6933                         MSC_BOM_COMPONENTS mbc,
6934                         MSC_OPERATION_COMPONENTS  moc,
6935                 	MSC_OPERATION_RESOURCE_SEQS SEQ,
6936                 	MSC_OPERATION_RESOURCES RES,
6937                         MSC_DEPARTMENT_RESOURCES DR, -- this is the sharing dept
6938                 	MSC_CALENDAR_DATES C1,
6939                 	MSC_CALENDAR_DATES C2
6940 
6941        	WHERE    mcbom1.session_id = MSC_ATP_PVT.G_SESSION_ID
6942         AND      mcbom1.sr_instance_id = p_instance_id
6943                  -- Bug 3358981 line is a model then include,
6944         AND      (mcbom1.ATO_PARENT_MODEL_LINE_ID = l_parent_line_id OR mcbom1.line_id = l_parent_line_id)
6945                  -- get all lines having the same parent model End Bug 3358981
6946         AND      mcbom1.bom_item_type in (1, 2)
6947         AND      mcbom1.inventory_item_id =
6948                        decode(MSC_ATP_PVT.G_ITEM_INFO_REC.bom_item_type,
6949                               1, mcbom1.inventory_item_id,
6950                               2, l_inventory_item_id)
6951         --AND      (mcbom1.parent_line_id = p_line_identifier or
6952                     -- Handle situation when parent_line_id is null.
6953                     -- Basic thing is that this section should handle all cases.
6954         --            mcbom1.inventory_item_id = l_inventory_item_id )
6955          -- Join to msc_process_effectivity
6956         AND      proc.plan_id = p_plan_id
6957         AND      proc.sr_instance_id = mcbom1.sr_instance_id
6958         AND      proc.organization_id = p_organization_id
6959         AND      proc.item_id  = mcbom1.inventory_item_id
6960                  -- Ensure that only items that have a common routing are processed with
6961                  -- the model. OC Items having a separate routing will be processed separately.
6962                  -- This check below with decode on the left side will be removed while
6963                  -- ones below that achieve the same thing retained if performance is an issue.
6964         AND      decode(MSC_ATP_PVT.G_ITEM_INFO_REC.bom_item_type, 1, -- bom_item_type for model
6965                          NVL(proc.routing_sequence_id, l_routing_seq_id),
6966                          proc.routing_sequence_id  -- all other cases including option_classes
6967                        ) =  l_routing_seq_id
6968         -- Quantity filter
6969         AND      mcbom1.quantity BETWEEN NVL(proc.minimum_quantity,0) AND
6970                   DECODE(NVL(proc.maximum_quantity,0),0,99999999,proc.maximum_quantity)
6971         -- Date Filter
6972         -- effective date should be greater than or equal to greatest of PTF date,
6973         -- sysdate and start date, disable date
6974         -- should be less than or equal to greatest of PTF date, sysdate and start date
6975         -- Note p_requested_date is used instead of C2.calendar_date currently,
6976         -- since p_requested_date is used in MSC_ATP_PROC.get_process_effectivity
6977         -- and also from performance considerations.
6978         AND   TRUNC(proc.effectivity_date) <=
6979                           TRUNC(GREATEST(p_requested_date, sysdate, MSC_ATP_PVT.G_PTF_DATE))
6980         AND   TRUNC(NVL(proc.disable_date,GREATEST(p_requested_date, sysdate, MSC_ATP_PVT.G_PTF_DATE)+1))
6981                > TRUNC(GREATEST(p_requested_date, sysdate, MSC_ATP_PVT.G_PTF_DATE))
6982         -- Join again to msc_cto_bom to obtain the components as well.
6983         AND     mcbom2.sr_instance_id = mcbom1.sr_instance_id
6984         AND     mcbom2.session_id = mcbom1.session_id
6985         AND     mcbom2.ato_parent_model_line_id = mcbom1.ATO_PARENT_MODEL_LINE_ID
6986         AND     NVL(mcbom2.parent_line_id, l_parent_line_id) = mcbom1.line_id
6987         -- to obtain all option classes that have a common routing.
6988         -- Get the routing
6989        	AND      RTG.PLAN_ID = proc.plan_id
6990        	AND      RTG.SR_INSTANCE_ID = proc.sr_instance_id -- Qry Streamline 3358981
6991        	AND      RTG.ORGANIZATION_ID = proc.organization_id
6992        	AND      RTG.ROUTING_SEQUENCE_ID = NVL(proc.routing_sequence_id,
6993                                                  RTG.ROUTING_SEQUENCE_ID)
6994                  -- Bug 3432530
6995                  -- Comment out join conditions for msc_system_items
6996                   -- 3358981 Eliminate semi cartesian product, streamline query.
6997         AND      RTG.assembly_item_id = DECODE (proc.routing_sequence_id, NULL,
6998                                                proc.item_id, l_inventory_item_id )
6999         -- Join to system items
7000         -- AND      I.PLAN_ID = RTG.PLAN_ID
7001         -- AND      I.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
7002         -- AND      I.ORGANIZATION_ID = RTG.ORGANIZATION_ID
7003                  -- Bug 3358981
7004         -- AND      I.INVENTORY_ITEM_ID = RTG.assembly_item_id
7005                  -- 3358981 Eliminate semi cartesian product, streamline query.
7006                  -- Ensure that only items that have a common routing are processed with
7007                  -- the model. OC Items having a separate routing will be processed separately.
7008         -- AND      I.INVENTORY_ITEM_ID = DECODE (proc.routing_sequence_id, NULL,
7009         --                        RTG.assembly_item_id, l_inventory_item_id ) -- model's item_id
7010                  -- End Bug 3358981
7011                  -- End Bug 3432530
7012         --(ssurendr) Bug 2865389 Removed condition for Alternate Routing designator as
7013         --we are accessing Routing by Routing sequance id.
7014         --We are Driving by routing table for performance gains.
7015        	--AND      RTG.ALTERNATE_ROUTING_DESIGNATOR IS NULL
7016         --  Get all operations for the routing
7017        	AND      OP.PLAN_ID = RTG.PLAN_ID
7018        	AND      OP.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
7019        	AND      OP.ROUTING_SEQUENCE_ID = RTG.ROUTING_SEQUENCE_ID
7020                  -- filter only on those components that are in the pseudo bom
7021                  -- AND OP.option_dependent_flag = 1 --
7022          /* Operation is of type Event (Do not select process) */
7023         and      NVL(OP.operation_type,1 ) = 1
7024         /* rajjain 3008611
7025          * effective date should be greater than or equal to greatest of PTF date, sysdate and start date
7026          * disable date should be less than or equal to greatest of PTF date, sysdate and start date*/
7027        	AND      TRUNC(NVL(OP.DISABLE_DATE, GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE)+1)) >
7028                   	TRUNC(GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE))
7029        	AND      TRUNC(OP.EFFECTIVITY_DATE) <=
7030                   	TRUNC(GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE)) -- bug 1404312
7031         -- Validate Model's BOM in sales order with model's bom in manufacturing org.
7032         AND     mbc.sr_instance_id = RTG.sr_instance_id -- Qry Streamline 3358981
7033         AND     mbc.plan_id =  RTG.plan_id
7034         AND     mbc.organization_id = RTG.organization_id
7035                 -- Bug 3358981
7036                 -- Ensure that only items that have a common routing are processed with
7037                 -- the model. OC Items having a separate routing will be processed separately.
7038         AND     mbc.bill_sequence_id = DECODE(proc.routing_sequence_id, NULL,
7039                                                 proc.bill_sequence_id, p_bill_seq_id)
7040                 -- End Bug 3358981
7041         AND     mbc.using_assembly_id = RTG.assembly_item_id -- Qry Streamline 3358981
7042         AND      TRUNC(NVL(MBC.DISABLE_DATE, GREATEST(C2.CALENDAR_DATE,
7043                          sysdate, MSC_ATP_PVT.G_PTF_DATE)+1)) >
7044                TRUNC(GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE))
7045                      AND      TRUNC(MBC.EFFECTIVITY_DATE) <=
7046                TRUNC(GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE))
7047         AND     mbc.inventory_item_id  = mcbom2.inventory_item_id
7048                  -- Optional Items selected in the Sales Order
7049                  -- Select the option dependent operations which are needed.
7050                  -- for the configuration
7051         -- Join to determine all the operations
7052         and     moc.plan_id = mbc.plan_id
7053         and     moc.sr_instance_id = mbc.sr_instance_id
7054         and     moc.organization_id = mbc.organization_id
7055         and     moc.bill_sequence_id = mbc.bill_sequence_id
7056         and     moc.component_sequence_id = mbc.component_sequence_id
7057         and     moc.routing_sequence_id = rtg.routing_sequence_id
7058         and     moc.operation_sequence_id = OP.operation_sequence_id
7059         -- Obtain the Resource Seq numbers.
7060        	AND      SEQ.PLAN_ID = OP.PLAN_ID
7061        	AND      SEQ.ROUTING_SEQUENCE_ID  = OP.ROUTING_SEQUENCE_ID
7062        	AND      SEQ.SR_INSTANCE_ID = OP.SR_INSTANCE_ID
7063        	AND      SEQ.OPERATION_SEQUENCE_ID = OP.OPERATION_SEQUENCE_ID
7064        	AND      RES.BASIS_TYPE in (1,2,3) --4694958
7065        	AND      RES.PLAN_ID = SEQ.PLAN_ID
7066        	AND      RES.ROUTING_SEQUENCE_ID = SEQ.ROUTING_SEQUENCE_ID
7067        	AND      RES.SR_INSTANCE_ID = SEQ.SR_INSTANCE_ID
7068        	AND      RES.OPERATION_SEQUENCE_ID = SEQ.OPERATION_SEQUENCE_ID
7069        	AND      RES.RESOURCE_SEQ_NUM = SEQ.RESOURCE_SEQ_NUM
7070        	AND      NVL(RES.ALTERNATE_NUMBER, 0) = 0 -- bug 1170698
7071        	AND      C1.CALENDAR_DATE = p_requested_date
7072                  -- Bug 3432530 Use RTG instead of MSC_SYSTEM_ITEMS I
7073        	AND      C1.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
7074        	AND      C1.CALENDAR_CODE = l_calendar_code
7075        	AND      C1.EXCEPTION_SET_ID = l_calendar_exception_set_id
7076                  --  Bug 3432530 Use local variables.
7077                  --  In case of common routing, use model's lead times.
7078        	AND      C2.SEQ_NUM = C1.PRIOR_SEQ_NUM - CEIL(((NVL(l_item_fixed_lt,0)+
7079                     NVL(l_item_var_lt,0)* p_requested_quantity) * (1+ l_mso_lead_time_factor))*
7080                        (1-NVL(SEQ.RESOURCE_OFFSET_PERCENT, 0)))
7081                  -- End Bug 3432530
7082        	AND      C2.CALENDAR_CODE = C1.CALENDAR_CODE
7083        	AND      C2.SR_INSTANCE_ID = C1.SR_INSTANCE_ID -- krajan : 2408696
7084        	AND      C2.EXCEPTION_SET_ID = C1.EXCEPTION_SET_ID
7085        	-- krajan : 2408696
7086         -- AND   	MUC1.UOM_CODE = l_uom_code
7087        	-- AND   	MUC1.INVENTORY_ITEM_ID = 0
7088        	-- AND   	MUC2.UOM_CLASS = MUC1.UOM_CLASS
7089        	-- AND   	MUC2.INVENTORY_ITEM_ID = 0
7090        	-- AND   	MUC2.UOM_CODE = RES.UOM_CODE
7091         AND      RES.UOM_CODE = l_uom_code
7092        	AND      DR.PLAN_ID = RTG.PLAN_ID
7093        	AND      DR.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
7094         AND     DR.ORGANIZATION_ID = RTG.ORGANIZATION_ID
7095        	AND      DR.RESOURCE_ID = RES.RESOURCE_ID
7096        	AND      DR.DEPARTMENT_ID = OP.DEPARTMENT_ID
7097 	-- performance dsting remove nvl from dr.ctp_flag
7098        	AND      DR.CTP_FLAG = 1
7099        	UNION ALL
7100        	SELECT   RTG.LINE_ID department_id,
7101                 	RTG.LINE_ID owning_department_id ,
7102                 	-1 resource_id,
7103                 	1 basis_type,
7104                 	1 resource_usage,
7105                 	C2.CALENDAR_DATE requested_date,
7106                 	CEIL((NVL(I.FIXED_LEAD_TIME,0)+
7107                    	NVL(I.VARIABLE_LEAD_TIME,0)* p_requested_quantity) * (1+ l_mso_lead_time_factor)) lead_time,
7108                 	1 efficiency,
7109                 	1 utilization,
7110                         2 batch_flag,
7111                         0 max_capacity,
7112                         0 required_unit_capacity,
7113                         --bug 2845383: Change all direct reference to null to local variables
7114                         l_null_char required_capacity_uom,
7115                         l_null_char res_uom,
7116                         1 res_uom_type,
7117                         l_null_char std_op_code,
7118                         --diag_atp
7119                         l_null_num resource_offset_percent,
7120                         l_null_num operation_sequence,
7121                         1 actual_resource_usage,
7122                         l_null_num reverse_cumulative_yield,
7123                         l_null_char department_code,
7124                         l_null_char resource_Code
7125        	FROM 	MSC_CALENDAR_DATES C2,
7126                 	MSC_CALENDAR_DATES C1,
7127                 	MSC_ROUTINGS RTG,
7128                 	MSC_SYSTEM_ITEMS  I
7129        	WHERE    I.PLAN_ID = RTG.PLAN_ID
7130        	AND      I.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
7131        	AND      I.INVENTORY_ITEM_ID = RTG.ASSEMBLY_ITEM_ID
7132        	AND      I.ORGANIZATION_ID = RTG.ORGANIZATION_ID
7133        	AND      RTG.PLAN_ID = p_plan_id
7134        	AND      RTG.SR_INSTANCE_ID = p_instance_id
7135        	AND      RTG.ORGANIZATION_ID = p_organization_id
7136        	AND      RTG.ROUTING_SEQUENCE_ID = l_routing_seq_id
7137         --(ssurendr) Bug 2865389 Removed condition for Alternate Routing designator as
7138         --we are accessing Routing by Routing sequance id.
7139         --We are Driving by routing table for performance gains.
7140        	--AND      RTG.ALTERNATE_ROUTING_DESIGNATOR IS NULL
7141        	AND	RTG.CTP_FLAG = 1
7142        	AND      RTG.LINE_ID IS NOT NULL
7143        	AND      C1.CALENDAR_DATE = p_requested_date
7144        	AND      C1.SR_INSTANCE_ID = I.SR_INSTANCE_ID
7145        	AND      C1.CALENDAR_CODE = l_calendar_code
7146        	AND      C1.EXCEPTION_SET_ID = l_calendar_exception_set_id
7147        	AND      C2.SEQ_NUM = C1.PRIOR_SEQ_NUM - CEIL((NVL(I.FIXED_LEAD_TIME,0)+
7148                         	NVL(I.VARIABLE_LEAD_TIME,0)* p_requested_quantity) * (1+ l_mso_lead_time_factor))
7149        	AND      C2.CALENDAR_CODE = C1.CALENDAR_CODE
7150        	AND      C2.SR_INSTANCE_ID = I.SR_INSTANCE_ID
7151        	AND      C2.EXCEPTION_SET_ID = C1.EXCEPTION_SET_ID
7152       	)
7153         ORDER by requested_date,     -- Bug 2313497 Ensure proper order in fetch
7154                     operation_sequence, resource_code;
7155        ELSE -- Not Processing CTO BOM Model or Option Class. ODR
7156       	SELECT department_id,
7157            	owning_department_id,
7158            	resource_id,
7159            	basis_type,
7160            	resource_usage,
7161            	requested_date,
7162            	lead_time,
7163            	efficiency,
7164            	utilization,
7165                 batch_flag,
7166                 max_capacity,
7167                 required_unit_capacity,
7168                 required_capacity_uom,
7169                 res_uom,
7170                 res_uom_type,
7171                 std_op_code,
7172                 --diag_atp
7173                 resource_offset_percent,
7174                 operation_sequence,
7175                 actual_resource_usage,
7176                 reverse_cumulative_yield,
7177                 department_code,
7178                 resource_code
7179       	BULK COLLECT INTO l_res_requirements.department_id,
7180                       	l_res_requirements.owning_department_id,
7181                       	l_res_requirements.resource_id,
7182                       	l_res_requirements.basis_type,
7183                       	l_res_requirements.resource_usage,
7184                       	l_res_requirements.requested_date,
7185                       	l_res_requirements.lead_time,
7186                       	l_res_requirements.efficiency,
7187                       	l_res_requirements.utilization,
7188                         --- the following columns are added for resource batching
7189                         l_res_requirements.batch_flag,
7190                         l_res_requirements.max_capacity,
7191                         l_res_requirements.required_unit_capacity,
7192                         l_res_requirements.required_capacity_uom,
7193 			l_res_requirements.res_uom,
7194                         l_res_requirements.res_uom_type,
7195                         l_res_requirements.std_op_code,
7196                         ---diag_atp
7197                         l_res_requirements.resource_offset_percent,
7198                         l_res_requirements.operation_sequence,
7199                         l_res_requirements.actual_resource_usage,
7200                         l_res_requirements.reverse_cumulative_yield,
7201                         l_res_requirements.department_code,
7202                         l_res_requirements.resource_code
7203 
7204       	FROM (
7205        	SELECT  /*+ ordered */  DR.DEPARTMENT_ID department_id,
7206                 	DR.OWNING_DEPARTMENT_ID owning_department_id,
7207                 	DR.RESOURCE_ID resource_id,
7208                 	RES.BASIS_TYPE basis_type,
7209                         --bug 3766224: Do not chnage usage for lot based resource
7210                         ROUND(DECODE(RES.BASIS_TYPE, 2, NVL(RES.RESOURCE_USAGE,0),
7211                 	(NVL(RES.RESOURCE_USAGE,0)
7212                         -- krajan : 2408696
7213                         -- MUC2.CONVERSION_RATE/MUC1.CONVERSION_RATE, 0
7214                         /DECODE(OP.REVERSE_CUMULATIVE_YIELD,0,1,NVL(OP.REVERSE_CUMULATIVE_YIELD,1)))
7215                         /(Decode (nvl (MSC_ATP_PVT.G_ORG_INFO_REC.org_type,MSC_ATP_PVT.DISCRETE_ORG), MSC_ATP_PVT.OPM_ORG, --Bug-4694958
7216                                   decode (RES.BASIS_TYPE, 3,
7217                                           NVL(DR.MAX_CAPACITY,1),  nvl(rtg.routing_quantity,1)
7218                                          ),
7219                                   nvl(rtg.routing_quantity,1)
7220                                  )
7221                          )),6) resource_usage, --4694958
7222                             -- Bug 2865389 (ssurendr) routing quantity added for OPM fix.
7223 			--(NVL(OP.NET_PLANNING_PERCENT,100)/100)/NVL(OP.REVERSE_CUMULATIVE_YIELD,1) resource_usage,
7224                 	C2.CALENDAR_DATE requested_date,
7225                 	CEIL(((NVL(I.FIXED_LEAD_TIME,0)+
7226                         	NVL(I.VARIABLE_LEAD_TIME,0)* p_requested_quantity) * (1+ l_mso_lead_time_factor))*
7227                         	(1-NVL(SEQ.RESOURCE_OFFSET_PERCENT, 0))) lead_time,
7228                 	NVL((DR.EFFICIENCY/100), 1) efficiency,
7229                 	NVL((DR.UTILIZATION/100), 1) utilization,
7230                         NVL(DR.BATCHABLE_FLAG, 2) batch_flag,
7231                         NVL(DR.MAX_CAPACITY,0) max_capacity,
7232                         DECODE(DR.UOM_CLASS_TYPE, 1, I.UNIT_WEIGHT, 2, I.UNIT_VOLUME) required_unit_capacity,
7233                         --1905284
7234                         DECODE(DR.UOM_CLASS_TYPE, 1, I.WEIGHT_UOM, 2, I.VOLUME_UOM) required_capacity_uom,
7235                         DR.UNIT_OF_MEASURE res_uom,
7236                         DR.UOM_CLASS_TYPE res_uom_type,
7237                         OP.STANDARD_OPERATION_CODE std_op_code,
7238                         --diag_atp
7239                         SEQ.RESOURCE_OFFSET_PERCENT resource_offset_percent,
7240                         OP.OPERATION_SEQ_NUM operation_sequence,
7241                         RES.RESOURCE_USAGE actual_resource_usage,
7242                         --NVL(OP.REVERSE_CUMULATIVE_YIELD, 1) reverse_cumulative_yield,
7243                         DECODE(OP.REVERSE_CUMULATIVE_YIELD,0,1,NVL(OP.REVERSE_CUMULATIVE_YIELD, 1)) reverse_cumulative_yield,--4694958
7244                         DR.Department_code Department_code,
7245                         DR.Resource_code Resource_code
7246         FROM
7247                  MSC_SYSTEM_ITEMS I,
7248                  MSC_ROUTINGS  RTG,
7249                  MSC_ROUTING_OPERATIONS OP,
7250                  MSC_OPERATION_RESOURCE_SEQS SEQ,
7251                  MSC_OPERATION_RESOURCES RES,
7252                  MSC_DEPARTMENT_RESOURCES DR,
7253                  MSC_CALENDAR_DATES C1,
7254                  MSC_CALENDAR_DATES C2
7255 
7256        	WHERE    I.PLAN_ID = RTG.PLAN_ID
7257        	AND      I.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
7258        	AND      I.INVENTORY_ITEM_ID = RTG.ASSEMBLY_ITEM_ID
7259        	AND      I.ORGANIZATION_ID = RTG.ORGANIZATION_ID
7260        	AND      RTG.PLAN_ID = p_plan_id
7261        	AND      RTG.SR_INSTANCE_ID = p_instance_id
7262        	AND      RTG.ORGANIZATION_ID = p_organization_id
7263        	AND      RTG.ROUTING_SEQUENCE_ID = l_routing_seq_id
7264         --(ssurendr) Bug 2865389 Removed condition for Alternate Routing designator as
7265         --we are accessing Routing by Routing sequance id.
7266         --We are Driving by routing table for performance gains.
7267        	--AND      RTG.ALTERNATE_ROUTING_DESIGNATOR IS NULL
7268        	AND      OP.PLAN_ID = RTG.PLAN_ID
7269        	AND      OP.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
7270        	AND      OP.ROUTING_SEQUENCE_ID = RTG.ROUTING_SEQUENCE_ID
7271         /* rajjain 3008611
7272          * effective date should be greater than or equal to greatest of PTF date, sysdate and start date
7273          * disable date should be less than or equal to greatest of PTF date, sysdate and start date*/
7274        	AND      TRUNC(NVL(OP.DISABLE_DATE, GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE)+1)) >
7275                   	TRUNC(GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE))
7276        	AND      TRUNC(OP.EFFECTIVITY_DATE) <=
7277                   	TRUNC(GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE)) -- bug 1404312
7278        	AND      SEQ.PLAN_ID = OP.PLAN_ID
7279        	AND      SEQ.ROUTING_SEQUENCE_ID  = OP.ROUTING_SEQUENCE_ID
7280        	AND      SEQ.SR_INSTANCE_ID = OP.SR_INSTANCE_ID
7281        	AND      SEQ.OPERATION_SEQUENCE_ID = OP.OPERATION_SEQUENCE_ID
7282        	AND      RES.BASIS_TYPE in (1,2,3) --4694958
7283        	AND      RES.PLAN_ID = SEQ.PLAN_ID
7284        	AND      RES.ROUTING_SEQUENCE_ID = SEQ.ROUTING_SEQUENCE_ID
7285        	AND      RES.SR_INSTANCE_ID = SEQ.SR_INSTANCE_ID
7286        	AND      RES.OPERATION_SEQUENCE_ID = SEQ.OPERATION_SEQUENCE_ID
7287        	AND      RES.RESOURCE_SEQ_NUM = SEQ.RESOURCE_SEQ_NUM
7288        	AND      NVL(RES.ALTERNATE_NUMBER, 0) = 0 -- bug 1170698
7289        	AND      C1.CALENDAR_DATE = p_requested_date
7290        	AND      C1.SR_INSTANCE_ID = I.SR_INSTANCE_ID
7291        	AND      C1.CALENDAR_CODE = l_calendar_code
7292        	AND      C1.EXCEPTION_SET_ID = l_calendar_exception_set_id
7293        	AND      C2.SEQ_NUM = C1.PRIOR_SEQ_NUM - CEIL(((NVL(I.FIXED_LEAD_TIME,0)+
7294                         	NVL(I.VARIABLE_LEAD_TIME,0)* p_requested_quantity) * (1+ l_mso_lead_time_factor))*
7295                         	(1-NVL(SEQ.RESOURCE_OFFSET_PERCENT, 0)))
7296        	AND      C2.CALENDAR_CODE = C1.CALENDAR_CODE
7297        	AND      C2.SR_INSTANCE_ID = C1.SR_INSTANCE_ID -- krajan : 2408696
7298        	AND      C2.EXCEPTION_SET_ID = C1.EXCEPTION_SET_ID
7299        	-- krajan : 2408696
7300         -- AND   	MUC1.UOM_CODE = l_uom_code
7301        	-- AND   	MUC1.INVENTORY_ITEM_ID = 0
7302        	-- AND   	MUC2.UOM_CLASS = MUC1.UOM_CLASS
7303        	-- AND   	MUC2.INVENTORY_ITEM_ID = 0
7304        	-- AND   	MUC2.UOM_CODE = RES.UOM_CODE
7305         AND      RES.UOM_CODE = l_uom_code
7306        	AND      DR.PLAN_ID = I.PLAN_ID
7307        	AND      DR.SR_INSTANCE_ID = I.SR_INSTANCE_ID
7308         AND     DR.ORGANIZATION_ID = I.ORGANIZATION_ID
7309        	AND      DR.RESOURCE_ID = RES.RESOURCE_ID
7310        	AND      DR.DEPARTMENT_ID = OP.DEPARTMENT_ID
7311 	-- performance dsting remove nvl from dr.ctp_flag
7312        	AND      DR.CTP_FLAG = 1
7313        	UNION ALL
7314        	SELECT   RTG.LINE_ID department_id,
7315                 	RTG.LINE_ID owning_department_id ,
7316                 	-1 resource_id,
7317                 	1 basis_type,
7318                 	1 resource_usage,
7319                 	C2.CALENDAR_DATE requested_date,
7320                 	CEIL((NVL(I.FIXED_LEAD_TIME,0)+
7321                    	NVL(I.VARIABLE_LEAD_TIME,0)* p_requested_quantity) * (1+ l_mso_lead_time_factor)) lead_time,
7322                 	1 efficiency,
7323                 	1 utilization,
7324                         2 batch_flag,
7325                         0 max_capacity,
7326                         0 required_unit_capacity,
7327                         --bug 2845383: Change all direct reference to null to local variables
7328                         l_null_char required_capacity_uom,
7329                         l_null_char res_uom,
7330                         1 res_uom_type,
7331                         l_null_char std_op_code,
7332                         --diag_atp
7333                         l_null_num resource_offset_percent,
7334                         l_null_num operation_sequence,
7335                         1 actual_resource_usage,
7336                         l_null_num reverse_cumulative_yield,
7337                         l_null_char department_code,
7338                         l_null_char resource_Code
7339        	FROM 	MSC_CALENDAR_DATES C2,
7340                 	MSC_CALENDAR_DATES C1,
7341                 	MSC_ROUTINGS RTG,
7342                 	MSC_SYSTEM_ITEMS  I
7343        	WHERE    I.PLAN_ID = RTG.PLAN_ID
7344        	AND      I.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
7345        	AND      I.INVENTORY_ITEM_ID = RTG.ASSEMBLY_ITEM_ID
7346        	AND      I.ORGANIZATION_ID = RTG.ORGANIZATION_ID
7347        	AND      RTG.PLAN_ID = p_plan_id
7348        	AND      RTG.SR_INSTANCE_ID = p_instance_id
7349        	AND      RTG.ORGANIZATION_ID = p_organization_id
7350        	AND      RTG.ROUTING_SEQUENCE_ID = l_routing_seq_id
7351         --(ssurendr) Bug 2865389 Removed condition for Alternate Routing designator as
7352         --we are accessing Routing by Routing sequance id.
7353         --We are Driving by routing table for performance gains.
7354        	--AND      RTG.ALTERNATE_ROUTING_DESIGNATOR IS NULL
7355        	AND	RTG.CTP_FLAG = 1
7356        	AND      RTG.LINE_ID IS NOT NULL
7357        	AND      C1.CALENDAR_DATE = p_requested_date
7358        	AND      C1.SR_INSTANCE_ID = I.SR_INSTANCE_ID
7359        	AND      C1.CALENDAR_CODE = l_calendar_code
7360        	AND      C1.EXCEPTION_SET_ID = l_calendar_exception_set_id
7361        	AND      C2.SEQ_NUM = C1.PRIOR_SEQ_NUM - CEIL((NVL(I.FIXED_LEAD_TIME,0)+
7362                         	NVL(I.VARIABLE_LEAD_TIME,0)* p_requested_quantity) * (1+ l_mso_lead_time_factor))
7363        	AND      C2.CALENDAR_CODE = C1.CALENDAR_CODE
7364        	AND      C2.SR_INSTANCE_ID = I.SR_INSTANCE_ID
7365        	AND      C2.EXCEPTION_SET_ID = C1.EXCEPTION_SET_ID
7366       	)
7367         ORDER by requested_date,     -- Bug 2313497 Ensure proper order in fetch
7368                     operation_sequence, resource_code;
7369        END IF;
7370        -- End CTO Option Dependent Resources ODR
7371       END IF;  ---- If l_routing_flag = 3 THEN
7372      ELSE
7373 
7374       IF PG_DEBUG in ('Y', 'C') THEN
7375          msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'using BOR');
7376       END IF;
7377 
7378       -- Added on 01/09/2001 by ngoel for performance improvement
7379       /* Modularize Item and Org Info */
7380       l_inv_item_id  :=  MSC_ATP_PVT.G_ITEM_INFO_REC.dest_inv_item_id;
7381       /* Modularize Item and Org Info */
7382       IF PG_DEBUG in ('Y', 'C') THEN
7383          msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Modular change l_inv_item_id : '||l_inv_item_id);
7384       END IF;
7385 
7386       SELECT b.department_id,
7387            dr.owning_department_id,
7388            b.resource_id,
7389            b.basis,
7390            DECODE(b.resource_id, -1, 1, b.resource_department_hours),
7391            c2.calendar_date,
7392            b.setback_days,
7393            nvl((dr.efficiency/100), 1),
7394            nvl((dr.utilization/100), 1),
7395            --- even though resource batching is not used in case of BOR
7396            --- the following columns are added because the process
7397            -- for all cases is done by the same code. Adding following
7398            --- columns will ensure proper extension of tables
7399            2 batch_flag,
7400            0 max_capacity,
7401            0 required_unit_capacity,
7402            --2845383: change direct refrence to null to local varibale
7403            l_null_char required_capacity_uom,
7404            l_null_char res_uom,
7405            l_null_char res_uom_type,
7406            l_null_char  std_op_code,
7407            --diag_atp
7408            l_null_num resource_offset_percent,
7409            l_null_num operation_sequence,
7410            DECODE(b.resource_id, -1, 1, b.resource_department_hours) actual_resource_usage,
7411            1 reverse_cumulative_yield,
7412            dr.department_code department_code,
7413            dr.resource_code resource_Code
7414 
7415       BULK COLLECT INTO l_res_requirements.department_id,
7416                       l_res_requirements.owning_department_id,
7417                       l_res_requirements.resource_id,
7418                       l_res_requirements.basis_type,
7419                       l_res_requirements.resource_usage,
7420                       l_res_requirements.requested_date,
7421                       l_res_requirements.lead_time,
7422                       l_res_requirements.efficiency,
7423                       l_res_requirements.utilization,
7424                        --- the following columns are added for resource batching
7425                       l_res_requirements.batch_flag,
7426                       l_res_requirements.max_capacity,
7427                       l_res_requirements.required_unit_capacity,
7428                       l_res_requirements.required_capacity_uom ,
7429                       l_res_requirements.res_uom,
7430                       l_res_requirements.res_uom_type,
7431                       l_res_requirements.std_op_code,
7432                       ---diag_atp
7433                       l_res_requirements.resource_offset_percent,
7434                       l_res_requirements.operation_sequence,
7435                       l_res_requirements.actual_resource_usage,
7436                       l_res_requirements.reverse_cumulative_yield,
7437                       l_res_requirements.department_code,
7438                       l_res_requirements.resource_code
7439 
7440       FROM msc_department_resources dr,
7441            msc_bor_requirements b,
7442            msc_calendar_dates c1,
7443            msc_calendar_dates c2
7444       WHERE B.PLAN_ID = p_plan_id
7445       AND   B.SR_INSTANCE_ID = p_instance_id
7446       AND   B.ORGANIZATION_ID = p_organization_id
7447       AND   B.ASSEMBLY_ITEM_ID = l_inv_item_id
7448 
7449 	-- Chnaged on 01/09/2001 by ngoel for performance improvement
7450 	-- MSC_ATP_FUNC.get_inv_item_id(p_instance_id, p_inventory_item_id, p_organization_id)
7451       AND   C1.CALENDAR_DATE = p_requested_date
7452       AND   C1.SR_INSTANCE_ID = B.SR_INSTANCE_ID
7453       AND   C1.CALENDAR_CODE = l_calendar_code
7454       AND   C1.EXCEPTION_SET_ID = l_calendar_exception_set_id
7455       AND   C2.SEQ_NUM = C1.PRIOR_SEQ_NUM - B.SETBACK_DAYS
7456       AND   C2.CALENDAR_CODE = C1.CALENDAR_CODE
7457       AND   C2.SR_INSTANCE_ID = B.SR_INSTANCE_ID
7458       AND   C2.EXCEPTION_SET_ID = C1.EXCEPTION_SET_ID
7459       AND   DR.PLAN_ID = B.PLAN_ID
7460       AND   DR.SR_INSTANCE_ID = B.SR_INSTANCE_ID
7461       AND   DR.RESOURCE_ID = B.RESOURCE_ID
7462       AND   DR.DEPARTMENT_ID = B.DEPARTMENT_ID
7463       AND   DR.ORGANIZATION_ID = B.ORGANIZATION_ID
7464       -- performance dsting remove nvl from dr.ctp_flag
7465       AND   DECODE(DR.LINE_FLAG, 1, 1, DR.CTP_FLAG) = 1
7466       AND   (DR.LINE_FLAG <> 1
7467             OR
7468             (DR.LINE_FLAG = 1 AND
7469              EXISTS ( SELECT 'CTP'
7470                       FROM   MSC_ROUTINGS RTG
7471                       WHERE  RTG.PLAN_ID = p_plan_id
7472       		      AND    RTG.SR_INSTANCE_ID = B.SR_INSTANCE_ID
7473                       AND    RTG.ASSEMBLY_ITEM_ID = B.ASSEMBLY_ITEM_ID
7474                       AND    RTG.ORGANIZATION_ID = B.ORGANIZATION_ID
7475                       AND    RTG.ROUTING_SEQUENCE_ID = l_routing_seq_id
7476                       --(ssurendr) Bug 2865389 OPM fix
7477                       -- AND    RTG.ALTERNATE_ROUTING_DESIGNATOR IS NULL
7478                       AND    RTG.LINE_ID = B.DEPARTMENT_ID
7479                       AND    NVL(RTG.CTP_FLAG, 2) = 1)))
7480       ORDER BY C2.CALENDAR_DATE;  -- Bug 2313497 Ensure proper order in fetch
7481 
7482 
7483     END IF;
7484       IF PG_DEBUG in ('Y', 'C') THEN
7485          msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'After getting resource information');
7486          msc_sch_wb.atp_debug('Get_Res_Requirements: ' ||
7487                'l_res_requirements.resource_id.COUNT ' || l_res_requirements.resource_id.COUNT);
7488       END IF;
7489       -- go over each resource
7490       j := l_res_requirements.resource_id.FIRST;
7491 
7492       -- if j is null, that means we don't have any resource requirements
7493       -- need to consider, so we can assume that we have infinite resource
7494       -- to make the assembly.
7495       -- otherwise we need to know how many assemblies we can make
7496       -- by loop through each resource and find the availibility.
7497       -- If we can make more than the requested quantity, we return
7498       -- the requested quantity.
7499 
7500       -- initially set the x_avail_assembly_qty to the p_request_quantity,
7501       -- and we adjust that later
7502 
7503       -- Initialize l_infinite_time_fence_date, since this is pds,
7504       -- use planning's cutoff date as the infinite time fence date
7505       IF PG_DEBUG in ('Y', 'C') THEN
7506          msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'x_atp_date := ' || x_atp_date);
7507       END IF;
7508       IF j IS NOT NULL THEN
7509          ---bug 2341075: read plan start date
7510 
7511          -- Supplier Capacity and Lead Time (SCLT) Proj
7512          -- Commented out
7513          -- SELECT curr_cutoff_date, trunc(plan_start_date)
7514          -- INTO   l_infinite_time_fence_date, l_plan_start_date
7515          -- FROM   msc_plans
7516          -- WHERE  plan_id = p_plan_id;
7517 
7518          -- Instead re-assigned local values using global variable
7519          l_plan_start_date := MSC_ATP_PVT.G_PLAN_INFO_REC.plan_start_date;
7520          --l_infinite_time_fence_date := MSC_ATP_PVT.G_PLAN_INFO_REC.curr_cutoff_date; (ssurendr) Bug 2865389
7521          l_infinite_time_fence_date := MSC_ATP_PVT.G_PLAN_INFO_REC.plan_cutoff_date;
7522          IF PG_DEBUG in ('Y', 'C') THEN
7523             msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Plan Start Date := ' || l_plan_start_date );
7524             msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Infinite Time Fence := ' ||
7525                                                                l_infinite_time_fence_date );
7526          END IF;
7527          -- End Supplier Capacity and Lead Time Proj
7528 
7529       END IF;
7530 
7531 
7532       ---- bug 1950528
7533       ---- Get assembly item id if 1) there is resource def 2) MSC_corpod is on 3) Its not a BOR
7534       IF (j IS NOT NULL) AND (MSC_ATP_PVT.G_PLAN_COPRODUCTS = 'Y') AND (l_use_bor <> 1)
7535          AND (NVL (MSC_ATP_PVT.G_ORG_INFO_REC.org_type,MSC_ATP_PVT.DISCRETE_ORG) = MSC_ATP_PVT.DISCRETE_ORG) THEN  --Bug-4694958
7536            BEGIN
7537               SELECT BOMS.ASSEMBLY_QUANTITY
7538               INTO l_assembly_quantity
7539               FROM MSC_BOMS BOMS
7540               WHERE    BOMS.PLAN_ID  = p_plan_id
7541               AND      BOMS.SR_INSTANCE_ID  = p_instance_id
7542               AND      BOMS.ORGANIZATION_ID  = p_organization_id
7543               AND      BOMS.BILL_SEQUENCE_ID = p_bill_seq_id;
7544               --(ssurendr) Bug 2865389 Removed condition for Alternate bom designator as
7545               --we are accessing bom by bill sequance id. Also removed MSC_SYSTEM_ITEMS from the SQL
7546               --AND      BOMS.ALTERNATE_BOM_DESIGNATOR IS NULL;
7547            EXCEPTION
7548               WHEN OTHERS THEN
7549                   l_assembly_quantity := 1;
7550            END;
7551       END IF;
7552       IF PG_DEBUG in ('Y', 'C') THEN
7553          msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_assembly_quantity := ' || l_assembly_quantity);
7554       END IF;
7555 
7556 
7557       --// BUG 2313497, 2126520
7558       res_count := l_res_requirements.resource_id.COUNT;
7559       --// BUG 2313497, 2126520
7560       WHILE j IS NOT NULL LOOP
7561 
7562          -- Bug 1610561
7563          -- Now perform infinite_time_fence_date processing for each resource.
7564          -- First Initialize the infinite_time_fence_date to NULL
7565 
7566          l_infinite_time_fence_date := NULL;
7567 
7568          -- Resource Id and Department ID assignment is moved up to here.
7569 
7570          l_resource_id := l_res_requirements.resource_id(j);
7571          l_department_id := NVL(l_res_requirements.owning_department_id(j),
7572                                 l_res_requirements.department_id(j));
7573          -- Now obtain the infinite time fence date if an ATP rule is specified.
7574 
7575          -- Bug 3036513 Get infinite time fence date for resource
7576          -- Existing SQL commented out.
7577          -- Call the Library routine that is now common for both items and resources.
7578 
7579          MSC_ATP_PROC.get_infinite_time_fence_date ( p_instance_id,
7580                                                      l_inv_item_id,
7581                                                      p_organization_id,
7582                                                      p_plan_id,
7583                                                      l_infinite_time_fence_date,
7584                                                      l_atp_rule_name,
7585                                                      l_resource_id,
7586                                                      l_department_id );
7587 
7588          IF PG_DEBUG in ('Y', 'C') THEN
7589             msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Resource Id := ' ||
7590                                                                       l_resource_id);
7591             msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'New Infinite Time Fence := '
7592                                                           || l_infinite_time_fence_date );
7593             msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Plan Cutoff Date := ' ||
7594                                                 MSC_ATP_PVT.G_PLAN_INFO_REC.plan_cutoff_date);
7595             msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'ATP RULE NAME for Resource := '
7596                                                           || l_atp_rule_name );
7597          END IF;
7598          -- Bug 3036513
7599 
7600          -- End Bug 1610561
7601 
7602         --diag_atp
7603         l_pegging_rec.operation_sequence_id := null;
7604         l_pegging_rec.usage := null;
7605         l_pegging_rec.offset := null;
7606         l_pegging_rec.efficiency := null;
7607         l_pegging_rec.utilization := null;
7608         l_pegging_rec.REVERSE_CUM_YIELD := null;
7609         l_pegging_rec.owning_department := null;
7610         l_pegging_rec.pegging_type := null;
7611         l_pegging_rec.required_quantity:=null;
7612         l_pegging_rec.required_date := null;
7613         l_pegging_rec.basis_type := null;
7614         l_pegging_rec.allocation_rule := null;
7615         l_pegging_rec.constraint_type := null;
7616         l_pegging_rec.actual_supply_demand_date := null;
7617 
7618         /** BUG 2313497, 2126520  Check Resource Availability on End Date **/
7619         -- Changing the code to check Resource Availability on
7620         -- resource End Date instead of start date.
7621         -- Typically, if there are two resources
7622         -- R1 and R2 then We calculate the Availability of R1 until
7623         -- R2 is about to be used. i.e R2's start date becomes
7624         -- R1's End date and thus we do availability check on R1's
7625         -- End date.
7626 
7627         IF j = res_count THEN
7628           l_requested_date := p_requested_date;
7629           l_lead_time := 0;
7630 
7631           IF PG_DEBUG in ('Y', 'C') THEN
7632              msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Setting the Lead time 0 for forward case');
7633           END IF;
7634         ELSE
7635           -- Check the the start date of the next resource and
7636           -- Use it as End date of current resource by
7637           -- storing the index. If next resource's Start date
7638           -- is same as current resource's start date then
7639           -- move in the loop until we find the resource whose
7640           -- start date is less then current resource's start date.
7641           For h in j+1..res_count
7642             LOOP
7643               IF PG_DEBUG in ('Y', 'C') THEN
7644                  msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'h = ' || h);
7645                  msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'J' ||l_res_requirements.requested_date(j));
7646                  msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'H' ||l_res_requirements.requested_date(h));
7647               END IF;
7648               IF l_res_requirements.requested_date(j)
7649                 < l_res_requirements.requested_date(h) THEN
7650                    -- Bug 3348095
7651                    -- Assign the Resource start Date
7652                    l_res_start_date := l_res_requirements.requested_date(j);
7653                    IF PG_DEBUG in ('Y', 'C') THEN
7654                       msc_sch_wb.atp_debug('Get_Res_Requirements: Init. l_res_start_date ' ||
7655                                                                          l_res_start_date);
7656                       msc_sch_wb.atp_debug('Get_Res_Requirements: ' || l_res_requirements.requested_date(h));
7657                    END IF;
7658                    -- Bug 3348095
7659                    l_lead_time := l_res_requirements.lead_time(h);
7660                    l_requested_date := l_res_requirements.requested_date(h);
7661                    IF PG_DEBUG in ('Y', 'C') THEN
7662                       msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Next Date found..Exiting');
7663                    END IF;
7664                    EXIT;
7665               ELSIF h = res_count THEN
7666                    l_requested_date := p_requested_date;
7667                    l_lead_time := 0;
7668               -- Bug 3348095
7669               -- Assign the Resource start Date
7670               ELSE
7671                    -- Set resource start date to NULL
7672                    l_res_start_date := NULL;
7673               -- Bug 3348095
7674               END IF;
7675 
7676             END LOOP;
7677         END IF;
7678 
7679         -- Bug 3494178, need to reset l_res_requirements.requested_date(j) same as l_requested_date
7680         l_res_requirements.requested_date(j) := l_requested_date;
7681 
7682         msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'New Requested Date: ' ||l_res_requirements.requested_date(j));
7683 
7684         IF PG_DEBUG in ('Y', 'C') THEN
7685            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Resource Lead time:' || l_lead_time);
7686            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_requested_date:'|| l_requested_date);
7687         END IF;
7688 
7689         -- 1610561
7690         -- l_resource_id := l_res_requirements.resource_id(j);
7691         -- l_department_id := NVL(l_res_requirements.owning_department_id(j),
7692         --                       l_res_requirements.department_id(j));
7693         -- l_requested_date already assigned above
7694         --l_requested_date := l_res_requirements.requested_date(j);
7695         /** BUG 2313497, 2126520 END Changes: Check Resource Availability **/
7696 
7697         l_resource_usage := l_res_requirements.resource_usage(j);
7698         l_basis_type := l_res_requirements.basis_type(j);
7699         l_efficiency := l_res_requirements.efficiency(j);
7700         l_utilization := l_res_requirements.utilization(j);
7701         ---resource batching
7702         l_max_capacity := l_res_requirements.max_capacity(j);
7703         l_batchable_flag := l_res_requirements.batch_flag(j);
7704         l_req_unit_capacity := l_res_requirements.required_unit_capacity(j);
7705         l_req_capacity_uom := NVL(l_res_requirements.required_capacity_uom(j), ' ');
7706         l_std_op_code := l_res_requirements.std_op_code(j);
7707         l_uom_type := l_res_requirements.res_uom_type(j);
7708         l_res_uom := l_res_requirements.res_uom(j);
7709 
7710         -- ODR
7711         l_op_seq_num := l_res_requirements.operation_sequence(j);
7712 
7713         If (l_batchable_flag <> 1 ) OR  (l_use_batching <> 1) THEN
7714           --- if item is not batchable or batching is not done then
7715           -- set the std_op_code back to null
7716            IF PG_DEBUG in ('Y', 'C') THEN
7717               msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'batch flag back to null');
7718               msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Dont do batching');
7719            END IF;
7720           l_batchable_flag := 2;
7721           --l_use_batching := 0;
7722         END IF;
7723 
7724         l_atp_period := l_null_atp_period;
7725         l_atp_supply_demand := l_null_atp_supply_demand;
7726 
7727         IF PG_DEBUG in ('Y', 'C') THEN
7728            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'j := '||j);
7729 	   msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_resource_id := '||l_resource_id);
7730            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_department_id := '||l_department_id);
7731            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_requested_date := '||l_requested_date);
7732            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_resource_usage := '||l_resource_usage);
7733            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_basis_type := '||l_basis_type);
7734            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_efficiency := '||l_efficiency);
7735            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_utilization := '||l_utilization);
7736            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_batchable_flag := '||l_batchable_flag);
7737            -- ODR
7738            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_op_seq_num := '||l_op_seq_num);
7739            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Offset % := '
7740                                                 ||l_res_requirements.resource_offset_percent(j));
7741            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Lead Time := '
7742                                          ||l_res_requirements.lead_time(j));
7743            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Actual Resource_Usage := '
7744                                                ||l_res_requirements.actual_resource_usage(j));
7745            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Reverse Cum Yield := '
7746                                              ||l_res_requirements.reverse_cumulative_yield(j));
7747         END IF;
7748         --- resource batching
7749         IF PG_DEBUG in ('Y', 'C') THEN
7750            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_max_capacity = '|| l_max_capacity);
7751            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_batchable_flag = '|| l_batchable_flag);
7752            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_req_unit_capacity = '||l_req_unit_capacity);
7753            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_req_capacity_uom = ' || l_req_capacity_uom);
7754            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'UOM type := ' || l_UOM_type);
7755            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_res_uom := '|| l_res_uom);
7756         END IF;
7757 
7758         --- get conversion rate for resource
7759         IF ((l_batchable_flag = 1) and (l_use_batching = 1)) THEN
7760                --do unit conversions into base uom
7761                ---first do item unit conversion to base uom
7762                BEGIN
7763                	  SELECT conversion_rate
7764                   INTO   l_item_conversion_rate
7765                   FROM   msc_uom_conversions
7766                   WHERE  inventory_item_id = 0
7767                   AND    sr_instance_id = p_instance_id
7768                   AND    UOM_CODE = l_req_capacity_uom;
7769                EXCEPTION
7770                   WHEN NO_DATA_FOUND THEN
7771                        l_item_conversion_rate := 1;
7772                END;
7773                --- now convert resource uom into base uom
7774                BEGIN
7775                   SELECT conversion_rate
7776                   INTO   l_res_conversion_rate
7777                   FROM   msc_uom_conversions
7778                   WHERE  inventory_item_id = 0
7779                   AND    sr_instance_id = p_instance_id
7780                   AND    UOM_CODE = l_res_uom;
7781                EXCEPTION
7782                   WHEN NO_DATA_FOUND THEN
7783                        l_res_conversion_rate := 1;
7784                END;
7785 
7786         END IF;
7787         IF PG_DEBUG in ('Y', 'C') THEN
7788            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_item_conversion_rate := ' || l_item_conversion_rate);
7789            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_res_conversion_rate := ' || l_res_conversion_rate);
7790         END IF;
7791 
7792         --diag_atp
7793         IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 and p_search = 1 THEN
7794 
7795            IF ((l_batchable_flag = 1) and (l_use_batching = 1)) then
7796                IF PG_DEBUG in ('Y', 'C') THEN
7797                   msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Batching quantity, diagnostic atp');
7798                END IF;
7799                l_requested_res_qty := (l_resource_usage * l_req_unit_capacity * p_requested_quantity)
7800                                        * (l_item_conversion_rate )/(l_efficiency * l_utilization * l_assembly_quantity);
7801            ELSIF l_basis_type in (1,3) THEN --4694958
7802                IF PG_DEBUG in ('Y', 'C') THEN
7803                   msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Basis type 1,3 qty, diagnostic ATP'); --4694958
7804                END IF;
7805                l_requested_res_qty := (l_resource_usage * p_requested_quantity)/
7806                                    (l_efficiency * l_utilization * l_assembly_quantity);
7807            ELSIF l_basis_type = 2 THEN
7808                IF PG_DEBUG in ('Y', 'C') THEN
7809                   msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Basis type 2 qty, diagnostic ATP');
7810                END IF;
7811                --bug 3766202: do not inflate the resource in case of pure lot base resource
7812                ---(no batching) as ATP doesn't consider batch size
7813                l_requested_res_qty := l_resource_usage/
7814                                    --(l_efficiency * l_utilization * l_assembly_quantity);
7815                                    (l_efficiency * l_utilization);
7816            END IF;
7817 
7818         ELSE
7819            IF ((l_batchable_flag = 1) and (l_use_batching = 1)) then
7820                IF PG_DEBUG in ('Y', 'C') THEN
7821                   msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Batching quantity');
7822                END IF;
7823                l_requested_res_qty := (l_resource_usage * l_req_unit_capacity * x_avail_assembly_qty)
7824                                        * (l_item_conversion_rate )/(l_efficiency * l_utilization * l_assembly_quantity);
7825            ELSIF l_basis_type in (1,3) THEN --4694958
7826                IF PG_DEBUG in ('Y', 'C') THEN
7827                   msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Basis type 1,3 qty'); --4694958
7828                END IF;
7829                l_requested_res_qty := (l_resource_usage * x_avail_assembly_qty)/
7830                                    (l_efficiency * l_utilization * l_assembly_quantity);
7831            ELSIF l_basis_type = 2 THEN
7832                IF PG_DEBUG in ('Y', 'C') THEN
7833                   msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Basis type 2 qty');
7834                END IF;
7835                --bug 3766202: do not inflate the resource in case of pure lot base resource
7836                ---(no batching) as ATP doesn't consider batch size
7837                l_requested_res_qty := l_resource_usage/
7838                                    (l_efficiency * l_utilization);
7839            END IF;
7840         --diag_atp
7841         END IF; --IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 and p_search = 1 THEN
7842 
7843         IF PG_DEBUG in ('Y', 'C') THEN
7844            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_requested_res_qty := '||l_requested_res_qty);
7845         END IF;
7846 
7847         -- we need to have a branch here for allocated atp
7848         -- Bug 2372577 . Check value of profile option : krajan
7849         l_msc_cap_allocation := NVL(FND_PROFILE.VALUE('MSC_CAP_ALLOCATION'), 'Y');
7850 
7851         IF (MSC_ATP_PVT.G_ALLOCATED_ATP = 'N') OR
7852            (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y' AND MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1 AND
7853              MSC_ATP_PVT.G_ALLOCATION_METHOD = 1) OR
7854              -- added for bug 2372577
7855             (l_msc_cap_allocation = 'N') THEN
7856 
7857             l_batching_flag := 0;
7858             IF l_batchable_flag = 1 and l_use_batching = 1 THEN
7859                l_batching_flag := 1;
7860             END IF;
7861 
7862             -- 2859130
7863             get_unalloc_res_avail(
7864                p_insert_flag,
7865                l_batching_flag,
7866                MSC_ATP_PVT.G_Optimized_Plan,
7867                p_instance_id,
7868                p_organization_id,
7869                p_plan_id,
7870                l_plan_start_date,
7871                l_department_id,
7872                l_resource_id,
7873                l_infinite_time_fence_date,
7874                l_uom_type,
7875                l_uom_code,
7876                l_max_capacity,
7877                l_res_conversion_rate,
7878                p_level,
7879                p_scenario_id,
7880                p_inventory_item_id,
7881                l_calendar_code,
7882                l_calendar_exception_set_id,
7883                l_summary_sql,       -- For summary enhancement
7884                p_refresh_number,    -- For summary enhancement
7885                l_atp_period_tab,
7886                l_atp_qty_tab,
7887                l_atp_period
7888             );
7889 
7890             Print_Dates_Qtys(l_atp_period_tab, l_atp_qty_tab);
7891 
7892         ELSE -- of G_ALLOCATED_ATP
7893            -- we are using allocated atp
7894            -- Begin Bug 2424357
7895            IF PG_DEBUG in ('Y', 'C') THEN
7896               msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'MSC_ATP_PVT.G_ATP_DEMAND_CLASS := ' || MSC_ATP_PVT.G_ATP_DEMAND_CLASS);
7897            END IF;
7898            l_demand_Class := MSC_AATP_FUNC.Get_Res_Hierarchy_demand_class(
7899                                                   MSC_ATP_PVT.G_PARTNER_ID,
7900                                                   MSC_ATP_PVT.G_PARTNER_SITE_ID,
7901                                                   l_department_id,
7902                                                   l_resource_id,
7903                                                   p_organization_id,
7904                                                   p_instance_id,
7905                                                   l_requested_date,
7906                                                   NULL,
7907                                                   MSC_ATP_PVT.G_ATP_DEMAND_CLASS);
7908 
7909            --diag_atp
7910            l_allocation_rule_name := MSC_ATP_PVT.G_ALLOCATION_RULE_NAME;
7911            IF PG_DEBUG in ('Y', 'C') THEN
7912               msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_demand_Class := '|| l_demand_Class);
7913            END IF;
7914            -- End Bug 2424357
7915 
7916            MSC_AATP_PVT.Res_Alloc_Cum_Atp(p_plan_id,
7917                               p_level,
7918                               MSC_ATP_PVT.G_ORDER_LINE_ID,
7919                               p_scenario_id,
7920                               l_department_id,
7921                               l_resource_id,
7922                               p_organization_id,
7923                               p_instance_id,
7924                               l_demand_Class, -- Bug 2424357
7925                               --p_demand_class,
7926                               l_requested_date,
7927                               p_insert_flag,
7928                               l_max_capacity,
7929                               l_batchable_flag,
7930                               l_res_conversion_rate,
7931                               l_uom_type,
7932                               l_atp_info,
7933                               l_atp_period,
7934                               l_atp_supply_demand);
7935 
7936            l_atp_period_tab := l_atp_info.atp_period;
7937            l_atp_qty_tab := l_atp_info.atp_qty;
7938 
7939         END IF; -- of G_ALLOCATED_ATP
7940 
7941         IF l_atp_period_tab.COUNT > 0 THEN
7942 
7943           IF PG_DEBUG in ('Y', 'C') THEN
7944              msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_atp_period_tab.count='||l_atp_period_tab.COUNT);
7945              FOR i in 1..l_atp_period_tab.COUNT LOOP
7946                  msc_sch_wb.atp_debug('Date '||l_atp_period_tab(i)||' Qty '||
7947                                   l_atp_qty_tab(i));
7948              END LOOP;
7949           END IF;
7950           l_res_qty_before_ptf := 0;
7951           IF PG_DEBUG in ('Y', 'C') THEN
7952              msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'G_PTF_DATE_THIS_LEVEL := ' || MSC_ATP_PVT.G_PTF_DATE_THIS_LEVEL);
7953           END IF;
7954           --bug 2341075: we should not consider any resources available before sysdate.Therefore,
7955           --- we get rid of all the resources availability before sysdate. Before this fix we use to ommit all
7956           --- the resources before pTF. Now we get rid off all resource availability prior to greatest of
7957           ---sysdate anf PTF date
7958           --Bug3394751 Added Trunc on the sysdate.
7959 
7960           FOR i in 1..l_atp_period_tab.COUNT LOOP
7961               IF (i = 1 AND l_atp_period_tab(i) >= GREATEST(MSC_ATP_PVT.G_PTF_DATE_THIS_LEVEL, trunc(sysdate))) THEN
7962                   l_res_qty_before_ptf := 0;
7963                   EXIT;
7964               ELSIF (i < l_atp_period_tab.COUNT AND
7965                       l_atp_period_tab(i) <  GREATEST(MSC_ATP_PVT.G_PTF_DATE_THIS_LEVEL, trunc(sysdate)) AND
7966                       l_atp_period_tab(i+1) >= GREATEST(MSC_ATP_PVT.G_PTF_DATE_THIS_LEVEL, trunc(sysdate)) ) THEN
7967                   l_res_qty_before_ptf := l_atp_qty_tab(i);
7968                   -- Bug 4108546 Set the Index value
7969                   l_res_ptf_indx := i;
7970                   EXIT;
7971               ELSIF i = l_atp_period_tab.COUNT THEN
7972                   IF l_atp_qty_tab(i) = MSC_ATP_PVT.INFINITE_NUMBER  THEN
7973                      l_res_qty_before_ptf := 0;
7974                   ELSE
7975                      l_res_qty_before_ptf := l_atp_qty_tab(i);
7976                      -- Bug 4108546 Set the Index value
7977                      l_res_ptf_indx := i;
7978                   END IF;
7979                   EXIT;
7980               END IF;
7981           END LOOP;
7982           l_res_qty_before_ptf := GREATEST(l_res_qty_before_ptf, 0);
7983           IF PG_DEBUG in ('Y', 'C') THEN
7984              msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_res_qty_before_ptf := ' || l_res_qty_before_ptf);
7985              -- Bug 4108546 Print the value of Index
7986              IF (l_res_ptf_indx IS NOT NULL) THEN
7987                 msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_res_ptf_indx := ' || l_res_ptf_indx);
7988              END IF;
7989              -- End Bug 4108546
7990           END IF;
7991           IF p_search = 1 THEN -- backward
7992 
7993             IF (l_requested_date < l_atp_period_tab(1)) THEN
7994             -- let say the first period is on Day5 but your
7995             -- request in on Day2.
7996 
7997                 l_requested_date_quantity := 0;
7998             ELSIF (l_requested_date < trunc(sysdate)) THEN
7999 
8000                 l_requested_date_quantity := 0;
8001 
8002             ELSE
8003              IF MSC_ATP_PVT.G_RES_CONSUME = 'Y' THEN
8004               IF PG_DEBUG in ('Y', 'C') THEN
8005                  msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'G_RES_CONSUME = '||MSC_ATP_PVT.G_RES_CONSUME);
8006               END IF;
8007               FOR k IN 1..l_atp_period_tab.COUNT LOOP
8008                 IF k < l_atp_period_tab.LAST THEN
8009                   l_next_period := l_atp_period_tab(k+1);
8010                 ELSE
8011                   l_next_period := l_requested_date + 1;
8012                 END IF;
8013 
8014                 IF ((l_atp_period_tab(k) <= l_requested_date) and
8015                         (l_next_period > l_requested_date)) THEN
8016 
8017                   -- Bug found during fixing 3036513
8018                   -- Change > to >= so that if requested_date is infinite time fence date
8019                   -- then the quantity returned is also infinite.
8020                   IF (l_requested_date >= l_infinite_time_fence_date) THEN
8021                     l_requested_date_quantity := l_atp_qty_tab(k);
8022                   ELSE
8023                     l_requested_date_quantity := GREATEST((l_atp_qty_tab(k) - l_res_qty_before_ptf), 0);
8024                   END IF;
8025                   IF PG_DEBUG in ('Y', 'C') THEN
8026                      msc_sch_wb.atp_debug('Get_Res_Requirements: l_atp_period_tab(k) :' || l_atp_period_tab(k) );
8027                      msc_sch_wb.atp_debug('Get_Res_Requirements: l_requested_date_quantity :' || l_requested_date_quantity );
8028                   END IF;
8029                   EXIT;
8030                 END IF;
8031               END LOOP;
8032              ELSE -- IF G_RES_CONSUME = 'N'
8033               IF PG_DEBUG in ('Y', 'C') THEN
8034                  msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'G_RES_CONSUME = '||MSC_ATP_PVT.G_RES_CONSUME);
8035               END IF;
8036               -- first we initialize the l_requested_date_quantity so that
8037               -- if we cannot find any date that match the requested date,
8038               -- the l_requested_date_quantity is set to 0.
8039 
8040               l_requested_date_quantity := 0.0;
8041               FOR k IN 1..l_atp_period_tab.COUNT LOOP
8042                 IF l_atp_period_tab(k) = l_requested_date THEN
8043                   l_requested_date_quantity := l_atp_qty_tab(k);
8044                   EXIT;
8045                 END IF;
8046               END LOOP;
8047              END IF;  -- END IF G_RES_CONSUME = 'Y'
8048 
8049 
8050             END IF;
8051 
8052             IF PG_DEBUG in ('Y', 'C') THEN
8053                msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_requested_date_quantity ='||l_requested_date_quantity);
8054             END IF;
8055 
8056             IF l_requested_date_quantity >= l_requested_res_qty THEN
8057                 -- for this resource, we satisfy the resource requirement
8058                 -- so we don't need to change the x_avail_assembly_qty
8059                 -- for the assembly.
8060                 NULL;
8061 
8062             ELSIF l_requested_date_quantity >0 THEN
8063             -- for this resource, we cannot satisfy the resource
8064             -- requirement.  so we need to change the x_avail_assembly_qty
8065                 --- resource batching: If req_dat_qty < req_qty then we set the avail_qty = 0
8066                 IF l_basis_type = 2 OR MSC_ATP_PVT.G_RES_CONSUME = 'N'OR
8067                          (l_batchable_flag = 1 AND l_use_batching = 1) THEN
8068                     IF PG_DEBUG in ('Y', 'C') THEN
8069                        msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'G_RES_CONSUME = '||MSC_ATP_PVT.G_RES_CONSUME);
8070                        msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_basis_type = '||l_basis_type);
8071                     END IF;
8072                     -- this requirement is per lot, so we cannot have any
8073                     -- final assembly made
8074                     x_avail_assembly_qty :=0;
8075                     --diag_atp: we want to check for next resource in daignostic mode even if
8076                     --available qty for this resource is zero.
8077                     IF NOT (MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1) THEN
8078 
8079 		       EXIT;
8080                     END IF;
8081                 ELSE
8082                     -- this requirement is per item, so we can make partial of
8083                     -- the requested_quantity.  notes, we should
8084                     -- use the min to get the x_avail_assembly_qty.
8085 
8086                     x_avail_assembly_qty := LEAST(x_avail_assembly_qty,
8087                           trunc((l_requested_date_quantity * l_efficiency * l_utilization)/
8088                                 l_resource_usage,6));	-- 5598066
8089 
8090                     -- 2869830
8091                     IF PG_DEBUG in ('Y', 'C') THEN
8092                        msc_sch_wb.atp_debug('avail_assembly_qty: ' ||
8093                           x_avail_assembly_qty);
8094                     END IF;
8095                     IF l_rounding_flag = 1 THEN
8096                        x_avail_assembly_qty := FLOOR(x_avail_assembly_qty);
8097                        IF PG_DEBUG in ('Y', 'C') THEN
8098                           msc_sch_wb.atp_debug('rounded avail qty: ' ||
8099                              x_avail_assembly_qty);
8100                        END IF;
8101                     END IF;
8102 
8103                 END IF;
8104             ELSE
8105                 -- since we don't have any resource left, we cannot make any
8106                 -- assembly.
8107                 x_avail_assembly_qty :=0;
8108                 --diag_atp: we want to check for next resource in daignostic mode even if
8109                 --available qty for this resource is zero.
8110                 IF NOT (MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1) THEN
8111                    EXIT;
8112                 END IF;
8113             END IF;
8114           ELSE
8115             -- now this is forward.  so what we want to know is the date
8116             -- when whole quantity is available.
8117             IF PG_DEBUG in ('Y', 'C') THEN
8118                msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'in forward, resource check');
8119             END IF;
8120 
8121             IF PG_DEBUG in ('Y', 'C') THEN
8122                msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_requested_date = '||l_requested_date);
8123             END IF;
8124 
8125             -- Bug 3450725, requested date must be at least PTF Date + Lead Time w/ offset % from start of job.
8126             -- Lets say, there are 3 resources R1, R2 and R3 with offset % as 0, 50% and 70% respectively,
8127             -- and Total LT (F+V*Qty)* (1+mso_LT_factor) = 10 days and PTF is D10.
8128             -- Request Date for each resource in this example must be minimum D15, D17 and D20 for R1, R2 and R3.
8129             -- This will ensure start date for 3 resources (assumed to be sequential) be D10, D15 and D17 respectively.
8130 			-- Use Org's Manuf. Calendar
8131 
8132             ---2178544
8133             l_requested_date := GREATEST(l_requested_date,
8134                      				MSC_CALENDAR.DATE_OFFSET
8135                                     (p_organization_id,
8136                                      p_instance_id,
8137                                      1,
8138                                      MSC_ATP_PVT.G_PTF_DATE,
8139                     				 CEIL(((NVL(l_item_fixed_lt,0)+
8140                         					NVL(l_item_var_lt,0)* p_requested_quantity) * (1+ l_mso_lead_time_factor))
8141                         					- l_lead_time)));--4198893,4198445
8142 
8143 
8144             ---2178544
8145             --l_requested_date := GREATEST(l_requested_date, MSC_ATP_PVT.G_PTF_DATE);
8146 
8147             IF PG_DEBUG in ('Y', 'C') THEN
8148                msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'New l_requested_date = '||l_requested_date);
8149             END IF;
8150 
8151             FOR k IN 1..l_atp_period_tab.COUNT LOOP
8152 
8153                 -- bug 1510408
8154                 IF k < l_atp_period_tab.LAST THEN
8155                   l_next_period := l_atp_period_tab(k+1);
8156                 ELSE
8157                   l_next_period := l_requested_date + 1;
8158                 END IF;
8159 
8160                 IF PG_DEBUG in ('Y', 'C') THEN
8161                    msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_atp_period_tab('||k||')='||l_atp_period_tab(k));
8162                    msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_atp_qty_tab('||k||')='||l_atp_qty_tab(k));
8163                 END IF;
8164 
8165                 IF (l_atp_qty_tab(k)  -l_res_qty_before_ptf >= l_requested_res_qty) AND
8166                     --((l_atp_period_tab(k) >= trunc(sysdate) AND --4198893,4198445
8167                     ((l_atp_period_tab(k) >= trunc(sysdate) AND -- bug 8552388
8168                      l_next_period > l_requested_date
8169                      AND MSC_ATP_PVT.G_RES_CONSUME = 'Y') OR
8170                      (l_atp_period_tab(k) >= l_requested_date
8171                      AND MSC_ATP_PVT.G_RES_CONSUME = 'N')) THEN
8172 
8173                   IF PG_DEBUG in ('Y', 'C') THEN
8174                      msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'inside the loop to find x_atp_date');
8175                      msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'x_atp_date = '||x_atp_date);
8176                      msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_res_requirements.lead_time(j) = '||l_res_requirements.lead_time(j));
8177                   END IF;
8178 
8179                   -- we need to store this quantity and date somewhere
8180                   -- so that we can use them to add to pegging tree
8181 
8182                 -- Bug 1418766 and 1417110. In case of forward scheduling,
8183                 -- instead of using resources on the earliest available date
8184                 -- use them on the latest date before the greatest of requested date
8185                 -- and l_res_atp_date
8186 
8187                   l_res_atp_date := GREATEST(l_atp_period_tab(k), l_requested_date);
8188                   -- BUG found during ODR/CTO-Rearch/ATP_Simplified Pegging.
8189                   -- Bug found during fixing 3036513
8190                   -- Change = to >= so that if requested_date is infinite time fence date
8191                   -- then the quantity returned is also infinite.
8192                   IF (l_res_atp_date >= l_infinite_time_fence_date) THEN
8193                      l_res_atp_qty := l_atp_qty_tab(k)  ;
8194                   ELSE
8195                      l_res_atp_qty := l_atp_qty_tab(k) - l_res_qty_before_ptf ;
8196                   END IF;
8197                   -- End BUG found during ODR/CTO-Rearch/ATP_Simplified Pegging.
8198 
8199             IF PG_DEBUG in ('Y', 'C') THEN
8200                msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_res_atp_date = '||l_res_atp_date);
8201             END IF;
8202 		 --4198893,4198445 First calculate individual availability date. This date will be used to calculate
8203 		 --start date.
8204                   IF nvl(l_lead_time, 0) > 0 THEN
8205                      l_res_availability_date := MSC_CALENDAR.DATE_OFFSET
8206                                     (p_organization_id,
8207                                      p_instance_id,
8208                                      1,
8209                                      l_res_atp_date,
8210                                      NVL(l_lead_time, 0));
8211                      /* x_atp_date :=  GREATEST(MSC_CALENDAR.DATE_OFFSET
8212                                     (p_organization_id,
8213                                      p_instance_id,
8214                                      1,
8215                                      l_res_atp_date,
8216                                      NVL(l_lead_time, 0)), --BUG 2313497, 2126520
8217                                      x_atp_date);
8218                      */
8219                      --// BUG 2313497, 2126520
8220                   ELSE
8221                     -- Bug 3598486: Taking the greatest of the 2 dates
8222                     --4198445: l_res_availability_date is same as l_res_atp_date
8223                     --x_atp_date := GREATEST(l_res_atp_date, x_atp_date);
8224 
8225                     l_res_availability_date := l_res_atp_date;
8226                   END IF;
8227                   x_atp_date := GREATEST(l_res_availability_date, x_atp_date);
8228                   IF PG_DEBUG in ('Y', 'C') THEN
8229                      msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_lead_time = '||l_lead_time);
8230                   END IF;
8231                   --// BUG 2313497, 2126520
8232                   IF PG_DEBUG in ('Y', 'C') THEN
8233                      msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_res_requirements.lead_time(j) = '||
8234                                l_res_requirements.lead_time(j));
8235                      msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'x_atp_date = '||x_atp_date);
8236                   END IF;
8237 
8238                   EXIT;
8239                 ELSIF k = l_atp_period_tab.last THEN -- bug 1169539
8240                   IF PG_DEBUG in ('Y', 'C') THEN
8241                      msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'end of l_atp_period_tab, setting x_atp_date to null');
8242                   END IF;
8243                   x_atp_date := null;
8244                 END IF;
8245             END LOOP;
8246 
8247             IF x_atp_date IS NULL THEN
8248                 -- no available date exists for this resource, we should
8249                 -- exit this resource loop then.
8250 		-- Bug 1608755, set available qty = 0 in this case.
8251                 x_avail_assembly_qty :=0;
8252                 IF PG_DEBUG in ('Y', 'C') THEN
8253                    msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'exiting from resource loop');
8254                 END IF;
8255 
8256                 EXIT;
8257             END IF;
8258 
8259           END IF; -- end if p_search
8260 
8261           IF PG_DEBUG in ('Y', 'C') THEN
8262              msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'x_avail_assembly_qty='||x_avail_assembly_qty);
8263           END IF;
8264 
8265         ELSE  -- else of 'if l_atp_period_tab.count > 0'
8266 
8267             -- no supply demand record for this resource, that means we
8268             -- cannot make any assembly.
8269             x_avail_assembly_qty :=0;
8270             x_atp_date := NULL;
8271             --diag_atp: we want to check for next resource in daignostic mode even if
8272             --available qty for this resource is zero.
8273             IF NOT (MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 AND p_search = 1) THEN
8274 
8275                 EXIT;
8276             END IF;
8277         END IF;  -- end if l_atp_period_tab.count > 0
8278 
8279         -- Note:  we need to post those requirements into database so that
8280         -- if we happen to check the resource again, we won't mess up the
8281         -- quantity. not yet done!!!
8282 
8283         IF p_search = 1 THEN   -- Backward Scheduling
8284           --diag_atp
8285           IF x_avail_assembly_qty <> 0 or MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 THEN
8286             -- get supply_id from the pegging_id
8287             SELECT IDENTIFIER3
8288             INTO   l_supply_id
8289             FROM   MRP_ATP_DETAILS_TEMP
8290             WHERE  PEGGING_ID = p_parent_pegging_id
8291             AND    RECORD_TYPE = 3
8292             AND    SESSION_ID = MSC_ATP_PVT.G_SESSION_ID;
8293             --diag_atp:
8294             IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 THEN
8295               --since we are checking on full quantity, we do not need to convert it back
8296               IF (l_basis_type in (1,3)) THEN --4694958
8297                  --bug 3766202: divide by l_assembly qty to correctly project resource hours
8298                  --- for co-producs
8299                  l_resource_hours       := (p_requested_quantity*l_resource_usage) /
8300                                            (l_efficiency * l_utilization * l_assembly_quantity);
8301 
8302                  l_unadj_resource_hours := (p_requested_quantity*l_resource_usage)/
8303                                             l_assembly_quantity; --5093604
8304 
8305                  l_touch_time           := (p_requested_quantity*l_resource_usage)/
8306                                              (l_efficiency * l_assembly_quantity); --5093604
8307               --bug 3766202: Do not inflate resource qty for unbatachable lot based resource
8308               ELSIF ((l_batchable_flag =1 and l_use_batching = 1)) THEN
8309                  --bug 3766202: divide by l_assembly qty to correctly project resource hours
8310                  --- for co-producs
8311                  l_resource_hours       := l_resource_usage / (l_efficiency * l_utilization * l_assembly_quantity);
8312 
8313                  l_unadj_resource_hours := l_resource_usage/l_assembly_quantity; --5093604
8314 
8315                  l_touch_time           := l_resource_usage/(l_efficiency * l_assembly_quantity); --5093604
8316 
8317               ELSE
8318                  l_resource_hours       := l_resource_usage / (l_efficiency * l_utilization);
8319 
8320                  l_unadj_resource_hours := l_resource_usage; --5093604
8321 
8322                  l_touch_time           := l_resource_usage/l_efficiency; --5093604
8323               END IF;
8324             ELSE
8325                IF (l_basis_type in (1,3)) THEN --4694958
8326                  --bug 3766202: divide by l_assembly qty to correctly project resource hours
8327                  --- for co-producs
8328                  l_resource_hours       := (x_avail_assembly_qty*l_resource_usage) /
8329                                            (l_efficiency * l_utilization * l_assembly_quantity);
8330 
8331                  l_unadj_resource_hours := (x_avail_assembly_qty*l_resource_usage)/
8332                                             l_assembly_quantity; --5093604
8333 
8334                  l_touch_time           := (x_avail_assembly_qty*l_resource_usage)/
8335                                             (l_efficiency * l_assembly_quantity); --5093604
8336 
8337                --bug 3766202: Do not inflate resource qty for unbatachable lot based resource
8338                ELSIF ((l_batchable_flag =1 and l_use_batching = 1)) THEN
8339                  --l_resource_hours := l_requested_res_qty / (l_efficiency * l_utilization);
8340                  --bug: resource hours were getting inflated twice.
8341                  --- replaced l_requested_res_qty with l_resource_usage
8342                  --bug 3766202: divide by l_assembly qty to correctly project resource hours
8343                  --- for co-producs
8344                  l_resource_hours       := l_resource_usage / (l_efficiency * l_utilization * l_assembly_quantity);
8345 
8346                  l_unadj_resource_hours := l_resource_usage/l_assembly_quantity; --5093604
8347 
8348                  l_touch_time           := l_resource_usage/(l_efficiency * l_assembly_quantity); --5093604
8349 
8350                ELSE
8351                  l_resource_hours       := l_resource_usage / (l_efficiency * l_utilization);
8352 
8353                  l_unadj_resource_hours := l_resource_usage; --5093604
8354 
8355                  l_touch_time           := l_resource_usage/l_efficiency; --5093604
8356                END IF;
8357             END IF;
8358 
8359             /*IF (l_basis_type = 2 ) THEN
8360               l_resource_hours := l_requested_res_qty / (l_efficiency * l_utilization);
8361             ELSIF (l_basis_type = 1 ) THEN
8362               l_resource_hours := (x_avail_assembly_qty*l_resource_usage) /
8363                                         (l_efficiency * l_utilization);
8364             END IF; */
8365 
8366             -- Bug 3348095
8367             -- Calculate the start date given the end date in backward case.
8368             IF (l_res_start_date IS NULL) THEN
8369               l_res_start_date :=  LEAST(MSC_CALENDAR.DATE_OFFSET
8370                                     (p_organization_id,
8371                                      p_instance_id,
8372                                      1,
8373                                      l_requested_date,
8374                                      -1 * l_res_requirements.lead_time(j)),
8375                                      l_requested_date);
8376 
8377               IF PG_DEBUG in ('Y', 'C') THEN
8378                  msc_sch_wb.atp_debug('Get_Res_Requirements: Calc. value-1 l_res_start_date ' ||
8379                                                                          l_res_start_date);
8380               END IF;
8381             END IF;
8382             -- End Bug 3348095
8383               -- Bug 3450725
8384               -- Ensure SYSDATE/PTF_DATE integrity while calculating start_date
8385               -- for resource_requirements
8386               -- Bug 3562873 only PTF check is needed.
8387               l_res_start_date := trunc(GREATEST(l_res_start_date,
8388                         --Bug 3562873   l_res_requirements.requested_date(j),
8389                                         MSC_ATP_PVT.G_PTF_DATE_THIS_LEVEL)); --4135752
8390               -- End Bug 3562873.
8391 
8392               IF PG_DEBUG in ('Y', 'C') THEN
8393                 msc_sch_wb.atp_debug('Get_Res_Requirements: l_res_requirements.requested_date(j) '||
8394                                                         l_res_requirements.requested_date(j));
8395                 msc_sch_wb.atp_debug('Get_Res_Requirements: Calc. value-2 l_res_start_date ' ||
8396                                                                          l_res_start_date);
8397               END IF;
8398               -- End Bug 3450725
8399 
8400             MSC_ATP_DB_UTILS.Add_Resource_Demand(p_instance_id,
8401                                                  p_plan_id,
8402                                                  l_supply_id,
8403                                                  p_organization_id,
8404                                                  l_resource_id,
8405                                                  l_res_requirements.department_id(j),
8406                                                  -- Bug 3348095 Pass in Resource Start Dt.
8407                                                  l_res_start_date,
8408                                                  -- End Bug 3348095
8409                                                  l_requested_date,
8410                                                  l_resource_hours, --5093604
8411                                                  l_unadj_resource_hours , --5093604
8412                                                  l_touch_time, --5093604
8413                                                  l_std_op_code,
8414                                                  l_requested_res_qty,
8415                                                  l_inventory_item_id,   -- CTO Option Dependent Resources ODR
8416                                                  l_basis_type,  -- CTO Option Dependent Resources ODR
8417                                                  l_op_seq_num,  -- CTO Option Dependent Resources ODR
8418                                                  p_refresh_number,      -- For summary enhancement
8419                                                  l_transaction_id,
8420                                                  l_return_status);
8421 
8422              msc_sch_wb.atp_debug('Out of the ADD RESOURCE DEMAND');
8423             -- Bug 3348095
8424             -- End date of this resource will be the start date of next resource.
8425             -- Bug 3562873 Comment out this assignment, redundant, done above.
8426             -- l_res_start_date := l_requested_date;
8427             /*
8428             IF PG_DEBUG in ('Y', 'C') THEN
8429                msc_sch_wb.atp_debug('Get_Res_Requirements: Set l_res_start_date for next Res. ' ||
8430                                                                          l_res_start_date);
8431             END IF;
8432             */
8433             -- End Bug 3562873
8434             -- End 3348095
8435 
8436             -- add pegging info for this demand
8437 
8438             -- for performance reason, we call these function here and
8439             -- then populate the pegging tree with the values
8440 
8441             -- 1487344: instead of getting resource code and department code
8442             -- separately, we get them together to ensure we won't accidently
8443             -- get the line info.  Because we display either owing dept or
8444             -- dept for supply or demand pegging tree, we need to get the name
8445             -- each time.
8446             --diag_atp: we are already getting the department code in actual query
8447             IF l_res_requirements.department_code(j) is null THEN
8448                MSC_ATP_PROC.get_dept_res_code(p_instance_id,
8449                               l_res_requirements.department_id(j),
8450                               l_resource_id,
8451                               p_organization_id,
8452                               l_department_code,
8453                               l_resource_code);
8454             ELSE
8455                l_department_code := l_res_requirements.department_code(j);
8456                l_resource_code := l_res_requirements.resource_code(j);
8457             END IF;
8458 
8459             IF NVL(l_res_requirements.department_id(j), -1) <> NVL(l_res_requirements.owning_department_id(j),
8460                                                                   NVL(l_res_requirements.department_id(j), -1)) THEN
8461                MSC_ATP_PROC.get_dept_res_code(p_instance_id,
8462                               l_res_requirements.owning_department_id(j),
8463                               l_resource_id,
8464                               p_organization_id,
8465                               l_owning_department_code,
8466                               l_resource_code);
8467             ELSE
8468                l_owning_department_code := l_department_code;
8469             END IF;
8470 
8471             l_pegging_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
8472             l_pegging_rec.order_line_id:= MSC_ATP_PVT.G_ORDER_LINE_ID;
8473             l_pegging_rec.parent_pegging_id:= p_parent_pegging_id;
8474             l_pegging_rec.atp_level:= p_level;
8475             l_pegging_rec.organization_id:= p_organization_id;
8476             l_pegging_rec.organization_code := l_org_code;
8477             l_pegging_rec.identifier1:= p_instance_id;
8478             l_pegging_rec.identifier2:= p_plan_id;
8479             l_pegging_rec.identifier3 := l_supply_id; -- link to assembly's supply l_transaction_id;
8480             l_pegging_rec.identifier3 := l_transaction_id;
8481             l_pegging_rec.scenario_id:= p_scenario_id;
8482             l_pegging_rec.supply_demand_source_type:= 1;
8483             --l_pegging_rec.supply_demand_quantity:=l_resource_hours;
8484             --- Resource batching
8485             IF (l_use_batching = 1 AND l_batchable_flag = 1) THEN
8486                 l_pegging_rec.supply_demand_quantity:=l_requested_res_qty;
8487             ELSE
8488                 l_pegging_rec.supply_demand_quantity:=l_resource_hours;
8489             END IF;
8490             l_pegging_rec.supply_demand_type:= 1;
8491             l_pegging_rec.supply_demand_date:= l_requested_date;
8492 	    l_pegging_rec.department_id := l_res_requirements.department_id(j);
8493             l_pegging_rec.department_code := l_department_code;
8494 	    l_pegging_rec.resource_id := l_resource_id;
8495             l_pegging_rec.resource_code := l_resource_code;
8496             l_pegging_rec.inventory_item_id := NULL;
8497             l_pegging_rec.inventory_item_name := NULL;
8498             l_pegging_rec.supplier_id := NULL;
8499             l_pegging_rec.supplier_name := NULL;
8500             l_pegging_rec.supplier_site_id := NULL;
8501             l_pegging_rec.supplier_site_name := NULL;
8502             --- resource batching
8503             IF PG_DEBUG in ('Y', 'C') THEN
8504                msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_use_batching = ' || l_use_batching);
8505                msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_batchable_flag = ' || l_batchable_flag);
8506             END IF;
8507             ---bug 1907419: set batchable_flag =1 for batchable resource
8508             --IF (l_use_batching = 1 and l_batchable_flag = 1) THEN
8509             -- add batch flag to pegging
8510             IF PG_DEBUG in ('Y', 'C') THEN
8511                msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'ADD batch flag to pegging');
8512             END IF;
8513             l_pegging_rec.batchable_flag := l_batchable_flag;
8514             --END IF;
8515 
8516             --diag_atp
8517             l_pegging_rec.pegging_type := MSC_ATP_PVT.RESOURCE_DEMAND; --resource demand node
8518 
8519             -- Bug 3348161
8520             IF PG_DEBUG in ('Y', 'C') THEN
8521                msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'G_ITEM_INFO_REC.base_item_id ' ||
8522                                       MSC_ATP_PVT.G_ITEM_INFO_REC.base_item_id);
8523             END IF;
8524             ---s_cto_rearch and ODR
8525             l_pegging_rec.dest_inv_item_id := l_inventory_item_id;
8526             IF (MSC_ATP_PVT.G_ITEM_INFO_REC.bom_item_type in (1, 2) OR
8527                   -- Handle Configuration Items as well.
8528                   MSC_ATP_PVT.G_ITEM_INFO_REC.base_item_id is NOT NULL) THEN
8529                l_pegging_rec.model_sd_flag := 1;
8530                IF PG_DEBUG in ('Y', 'C') THEN
8531                  msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Set model_sd_flag');
8532                END IF;
8533             END IF;
8534             --e_cto_rearch and ODR
8535             -- End Bug 3348161
8536 
8537 	    -- dsting ATO 2465370
8538 	    l_pegging_rec.required_date := TRUNC(l_requested_date) + MSC_ATP_PVT.G_END_OF_DAY;
8539 
8540             --bug 3328421
8541             l_pegging_rec.actual_supply_demand_date := TRUNC(l_requested_date) + MSC_ATP_PVT.G_END_OF_DAY;
8542 
8543             -- for demo:1153192
8544             l_pegging_rec.constraint_flag := 'N';
8545             l_pegging_rec.summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;
8546             -- Bug 3826234
8547             l_pegging_rec.manufacturing_cal_code :=  NULL;
8548             l_pegging_rec.organization_type  := NVL ( MSC_ATP_PVT.G_ORG_INFO_REC.org_type, MSC_ATP_PVT.DISCRETE_ORG); --4775920
8549 
8550             MSC_ATP_DB_UTILS.Add_Pegging(l_pegging_rec, l_pegging_id);
8551 
8552             -- 1487344: instead of getting resource code and department code
8553             -- separately, we get them together to ensure we won't accidently
8554             -- get the line info.  Because we display either owing dept or
8555             -- dept for supply or demand pegging tree, we need to get the name
8556             -- each time.
8557 
8558             --diag_atp: we already got the owning department code before adding pegging for resource demand
8559             l_department_code := l_owning_department_code;
8560 
8561 
8562             /*MSC_ATP_PROC.get_dept_res_code(p_instance_id,
8563                               l_department_id,
8564                               l_resource_id,
8565                               p_organization_id,
8566                               l_department_code,
8567                               l_resource_code); */
8568 
8569             -- add pegging info for the supply
8570 
8571             l_pegging_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
8572             l_pegging_rec.order_line_id:= MSC_ATP_PVT.G_ORDER_LINE_ID;
8573             l_pegging_rec.parent_pegging_id:= l_pegging_id;
8574             l_pegging_rec.atp_level:= p_level+1;
8575             l_pegging_rec.organization_id:= p_organization_id;
8576             l_pegging_rec.organization_code := l_org_code;
8577             l_pegging_rec.identifier1:= p_instance_id;
8578             l_pegging_rec.identifier2:= p_plan_id;
8579             l_pegging_rec.identifier3 := -1;
8580             l_pegging_rec.scenario_id:= p_scenario_id;
8581             l_pegging_rec.supply_demand_source_type:= MSC_ATP_PVT.ATP;
8582             l_pegging_rec.supply_demand_quantity:=l_requested_date_quantity;
8583             l_pegging_rec.supply_demand_type:= 2;
8584             l_pegging_rec.supply_demand_date:= l_requested_date;
8585             l_pegging_rec.department_id := l_department_id;
8586             l_pegging_rec.department_code := l_department_code;
8587             l_pegging_rec.resource_id := l_resource_id;
8588             l_pegging_rec.resource_code := l_resource_code;
8589             l_pegging_rec.inventory_item_id := NULL;
8590             l_pegging_rec.inventory_item_name := NULL;
8591             l_pegging_rec.supplier_id := NULL;
8592             l_pegging_rec.supplier_name := NULL;
8593             l_pegging_rec.supplier_site_id := NULL;
8594             l_pegging_rec.supplier_site_name := NULL;
8595               --- resource batching
8596               IF PG_DEBUG in ('Y', 'C') THEN
8597                  msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_use_batching = ' || l_use_batching);
8598                msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_batchable_flag = ' || l_batchable_flag);
8599             END IF;
8600             --- bug 1907419
8601             ---IF (l_use_batching = 1 and l_batchable_flag = 1) THEN
8602             IF PG_DEBUG in ('Y', 'C') THEN
8603                msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'ADD batch flag to pegging');
8604             END IF;
8605             l_pegging_rec.batchable_flag := l_batchable_flag;
8606             --END IF;
8607 
8608             --diag_atp
8609             l_pegging_rec.operation_sequence_id := l_res_requirements.operation_sequence(j);
8610             l_pegging_rec.usage := l_res_requirements.actual_resource_usage(j);
8611             l_pegging_rec.offset := l_res_requirements.resource_offset_percent(j);
8612             l_pegging_rec.efficiency := l_res_requirements.efficiency(j);
8613             l_pegging_rec.utilization := l_res_requirements.utilization(j);
8614             l_pegging_rec.REVERSE_CUM_YIELD := l_res_requirements.reverse_cumulative_yield(j);
8615             l_pegging_rec.owning_department := l_owning_department_code;
8616             l_pegging_rec.pegging_type := MSC_ATP_PVT.RESOURCE_SUPPLY; --resource supply node
8617 
8618             l_pegging_rec.model_sd_flag := NULL; -- cto_rearch ODR unset flag for supply.
8619 
8620             IF (l_use_batching = 1 AND l_batchable_flag = 1) THEN
8621                 l_pegging_rec.required_quantity:=l_requested_res_qty;
8622             ELSE
8623                 l_pegging_rec.required_quantity:=l_resource_hours;
8624             END IF;
8625             l_pegging_rec.required_date := TRUNC(l_requested_date) + MSC_ATP_PVT.G_END_OF_DAY;
8626             --bug 3328421:
8627             l_pegging_rec.actual_supply_demand_date := TRUNC(l_requested_date) + MSC_ATP_PVT.G_END_OF_DAY;
8628             l_pegging_rec.basis_type := l_res_requirements.basis_type(j);
8629             IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 THEN
8630                IF l_requested_res_qty > l_requested_date_quantity THEN
8631                  l_pegging_rec.constraint_type := 6;
8632                END IF;
8633             END IF;
8634             l_pegging_rec.allocation_rule := l_allocation_rule_name;
8635             --diag_atp_end
8636 
8637             -- for demo:1153192
8638             IF l_resource_hours >= l_requested_date_quantity THEN
8639               l_pegging_rec.constraint_flag := 'Y';
8640             ELSE
8641               l_pegging_rec.constraint_flag := 'N';
8642             END IF;
8643 
8644             l_pegging_rec.source_type := 0;
8645             l_pegging_rec.summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;
8646 
8647             -- Bug 3036513 Add Infinite_Time_fence and ATP Rule Data to Pegging
8648             l_pegging_rec.infinite_time_fence := l_infinite_time_fence_date;
8649             l_pegging_rec.atp_rule_name := l_atp_rule_name;
8650             -- End Bug 3036513
8651             -- Bug 3826234
8652             l_pegging_rec.manufacturing_cal_code :=  l_calendar_code;
8653             l_pegging_rec.organization_type  := NVL ( MSC_ATP_PVT.G_ORG_INFO_REC.org_type, MSC_ATP_PVT.DISCRETE_ORG); --4775920
8654 
8655             MSC_ATP_DB_UTILS.Add_Pegging(l_pegging_rec, l_pegging_id);
8656 
8657           END IF;
8658 
8659         ELSE
8660            --IF p_search = 2 THEN, Forward Scheduling
8661 
8662           IF x_atp_date IS NOT NULL THEN
8663             -- get supply_id from the pegging_id
8664             SELECT IDENTIFIER3
8665             INTO   l_supply_id
8666             FROM   MRP_ATP_DETAILS_TEMP
8667             WHERE  PEGGING_ID = p_parent_pegging_id
8668             AND    RECORD_TYPE = 3
8669             AND    SESSION_ID = MSC_ATP_PVT.G_SESSION_ID;
8670 
8671             IF (l_basis_type in (1,3)) THEN --4694958
8672               --bug 3766202: divide by l_assembly qty to correctly project resource hours
8673               --- for co-producs
8674               l_resource_hours       := (x_avail_assembly_qty*l_resource_usage) /
8675                                         (l_efficiency * l_utilization * l_assembly_quantity);
8676 
8677               l_unadj_resource_hours := (x_avail_assembly_qty*l_resource_usage)/
8678                                          l_assembly_quantity; --5093604
8679 
8680               l_touch_time           := (x_avail_assembly_qty*l_resource_usage)/
8681                                          (l_efficiency * l_assembly_quantity); --5093604
8682 
8683             --bug 3766202: Do no inflate res req for nonbatchable resource
8684             ELSIF (l_batchable_flag =1 and l_use_batching = 1) THEN
8685 
8686               --bug 3766202: divide by l_assembly qty to correctly project resource hours
8687               --- for co-producs
8688               l_resource_hours       := l_resource_usage / (l_efficiency * l_utilization * l_assembly_quantity);
8689 
8690               l_unadj_resource_hours := l_resource_usage/l_assembly_quantity; --5093604
8691 
8692               l_touch_time           := l_resource_usage/(l_efficiency * l_assembly_quantity);  --5093604
8693 
8694             ELSE
8695               l_resource_hours       := l_resource_usage / (l_efficiency * l_utilization);
8696 
8697               l_unadj_resource_hours := l_resource_usage; --5093604
8698 
8699               l_touch_time           := l_resource_usage/l_efficiency; --5093604
8700 
8701             END IF;
8702 
8703             -- Bug 3348095
8704             -- Calculate the start date given the end date in forward case.
8705             l_res_start_date :=  LEAST(MSC_CALENDAR.DATE_OFFSET
8706                                     (p_organization_id,
8707                                      p_instance_id,
8708                                      1,
8709                                      l_res_availability_date,
8710                                      -1 * l_res_requirements.lead_time(j)),
8711                                      l_res_atp_date);
8712                                      --4198893,4198445: Calculate start date from individual resource's end date
8713                                      --l_res_atp_date);
8714 
8715             IF PG_DEBUG in ('Y', 'C') THEN
8716                msc_sch_wb.atp_debug('Get_Res_Requirements: Calculated val-1 l_res_start_date ' || l_res_start_date);
8717             END IF;
8718             -- Bug 3450725
8719             -- Ensure SYSDATE/PTF_DATE integrity while calculating start_date
8720             -- for resource_requirements
8721             -- Bug 3562873 only PTF check is needed.
8722             l_res_start_date := trunc(GREATEST(l_res_start_date,
8723                       --Bug 3562873   l_res_requirements.requested_date(j),
8724                                         MSC_ATP_PVT.G_PTF_DATE_THIS_LEVEL));  --4135752
8725             -- End Bug 3562873.
8726 
8727             IF PG_DEBUG in ('Y', 'C') THEN
8728                msc_sch_wb.atp_debug('Get_Res_Requirements: l_res_requirements.requested_date(j) ' ||
8729                                                         l_res_requirements.requested_date(j));
8730                msc_sch_wb.atp_debug('Get_Res_Requirements: Calculated val-2 l_res_start_date ' || l_res_start_date);
8731             END IF;
8732             -- End Bug 3450725
8733             -- End Bug 3348095
8734 
8735             MSC_ATP_DB_UTILS.Add_Resource_Demand(p_instance_id,
8736                                                  p_plan_id,
8737                                                  l_supply_id,
8738                                                  p_organization_id,
8739                                                  l_resource_id,
8740                                                  l_res_requirements.department_id(j),
8741                                                  -- Bug 3348095 Pass in Resource Start Dt.
8742                                                  l_res_start_date,
8743                                                  -- End Bug 3348095
8744                                                  l_res_atp_date,  -- bug 1238910
8745                                                  --l_requested_res_qty,
8746                                                  l_resource_hours, --5093604
8747                                                  l_unadj_resource_hours, --5093604
8748                                                  l_touch_time, --5093604
8749                                                  l_std_op_code,
8750                                                  l_requested_res_qty,
8751                                                  l_inventory_item_id,   -- CTO Option Dependent Resources ODR
8752                                                  l_basis_type,  -- CTO Option Dependent Resources ODR
8753                                                  l_op_seq_num,  -- CTO Option Dependent Resources ODR
8754                                                  p_refresh_number,      -- For summary enhancement
8755                                                  l_transaction_id,
8756                                                  l_return_status);
8757 
8758             -- add pegging info for this demand
8759 
8760             -- for performance reason, we call these function here and
8761             -- then populate the pegging tree with the values
8762 
8763             -- 1487344: instead of getting resource code and department code
8764             -- separately, we get them together to ensure we won't accidently
8765             -- get the line info.  Because we display either owing dept or
8766             -- dept for supply or demand pegging tree, we need to get the name
8767             -- each time.
8768 
8769             --diag_atp: we are already getting the department code in actual query
8770             IF l_res_requirements.department_code(j) is null THEN
8771                MSC_ATP_PROC.get_dept_res_code(p_instance_id,
8772                               l_res_requirements.department_id(j),
8773                               l_resource_id,
8774                               p_organization_id,
8775                               l_department_code,
8776                               l_resource_code);
8777             ELSE
8778                l_department_code := l_res_requirements.department_code(j);
8779                -- Bug 3308237 Set the assignment right.
8780                -- It was l_res_requirements.resource_id(j) before
8781                l_resource_code := l_res_requirements.resource_code(j);
8782             END IF;
8783 
8784             IF NVL(l_res_requirements.department_id(j), -1) <> NVL(l_res_requirements.owning_department_id(j),
8785                                                                  NVL(l_res_requirements.department_id(j), -1)) THEN
8786                MSC_ATP_PROC.get_dept_res_code(p_instance_id,
8787                               l_res_requirements.owning_department_id(j),
8788                               l_resource_id,
8789                               p_organization_id,
8790                               l_owning_department_code,
8791                               l_resource_code);
8792             ELSE
8793                l_owning_department_code := l_department_code;
8794             END IF;
8795 
8796             IF PG_DEBUG in ('Y', 'C') THEN
8797                msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'in forward piece, adding resource demand pegging');
8798             END IF;
8799             l_pegging_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
8800             l_pegging_rec.order_line_id:= MSC_ATP_PVT.G_ORDER_LINE_ID;
8801             l_pegging_rec.parent_pegging_id:= p_parent_pegging_id;
8802             l_pegging_rec.atp_level:= p_level;
8803             l_pegging_rec.organization_id:= p_organization_id;
8804             l_pegging_rec.organization_code := l_org_code;
8805             l_pegging_rec.identifier1:= p_instance_id;
8806             l_pegging_rec.identifier2:= p_plan_id;
8807             l_pegging_rec.identifier3 := l_transaction_id;
8808             l_pegging_rec.scenario_id:= p_scenario_id;
8809             l_pegging_rec.supply_demand_source_type:= 1;
8810             l_pegging_rec.supply_demand_quantity:=l_requested_res_qty;
8811             l_pegging_rec.supply_demand_type:= 1;
8812             --- 2178544
8813             l_pegging_rec.supply_demand_date:= l_res_requirements.requested_date(j);
8814             --l_pegging_rec.supply_demand_date:= l_requested_date;
8815 	    l_pegging_rec.department_id := l_res_requirements.department_id(j);
8816             l_pegging_rec.department_code := l_department_code;
8817 	    l_pegging_rec.resource_id := l_resource_id;
8818             l_pegging_rec.resource_code := l_resource_code;
8819 
8820             l_pegging_rec.inventory_item_id := NULL;
8821             l_pegging_rec.inventory_item_name := NULL;
8822             l_pegging_rec.supplier_id := NULL;
8823             l_pegging_rec.supplier_name := NULL;
8824             l_pegging_rec.supplier_site_id := NULL;
8825             l_pegging_rec.supplier_site_name := NULL;
8826 
8827               --- resource batching
8828              IF PG_DEBUG in ('Y', 'C') THEN
8829                 msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_use_batching = ' || l_use_batching);
8830                msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_batchable_flag = ' || l_batchable_flag);
8831             END IF;
8832             --- bug 1907419
8833             --IF (l_use_batching = 1 and l_batchable_flag = 1) THEN
8834             IF PG_DEBUG in ('Y', 'C') THEN
8835                msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'ADD batch flag to pegging');
8836             END IF;
8837             l_pegging_rec.batchable_flag := l_batchable_flag;
8838             --END IF;
8839 
8840             --diag_atp
8841             l_pegging_rec.pegging_type := MSC_ATP_PVT.RESOURCE_DEMAND; --resource demand node
8842 
8843             -- for demo:1153192
8844             l_pegging_rec.constraint_flag := 'N';
8845             l_pegging_rec.summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;
8846 
8847 	    -- dsting ATO 2465370
8848             --bug 3328421: store actual req date in req_date col and actual supply demand date in
8849 	    --l_pegging_rec.required_date := TRUNC(l_res_atp_date) + MSC_ATP_PVT.G_END_OF_DAY;
8850             l_pegging_rec.required_date := l_res_requirements.requested_date(j);
8851             l_pegging_rec.actual_supply_demand_date := TRUNC(l_res_atp_date) + MSC_ATP_PVT.G_END_OF_DAY;
8852 
8853             -- Bug 3348161
8854             IF PG_DEBUG in ('Y', 'C') THEN
8855                msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'G_ITEM_INFO_REC.base_item_id ' ||
8856                                       MSC_ATP_PVT.G_ITEM_INFO_REC.base_item_id);
8857             END IF;
8858             ---s_cto_rearch and ODR,
8859             -- Bug 3348161 set model_sd_flag for future case.
8860             l_pegging_rec.dest_inv_item_id := l_inventory_item_id;
8861             IF (MSC_ATP_PVT.G_ITEM_INFO_REC.bom_item_type in (1, 2) OR
8862                   -- Handle Configuration Items as well.
8863                   MSC_ATP_PVT.G_ITEM_INFO_REC.base_item_id is NOT NULL) THEN
8864                l_pegging_rec.model_sd_flag := 1;
8865                IF PG_DEBUG in ('Y', 'C') THEN
8866                  msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Set model_sd_flag');
8867                END IF;
8868             END IF;
8869             --e_cto_rearch and ODR
8870             -- End Bug 3348161
8871              -- Bug 3826234
8872             l_pegging_rec.manufacturing_cal_code :=  NULL;
8873             l_pegging_rec.organization_type  := NVL ( MSC_ATP_PVT.G_ORG_INFO_REC.org_type, MSC_ATP_PVT.DISCRETE_ORG); --4775920
8874 
8875             MSC_ATP_DB_UTILS.Add_Pegging(l_pegging_rec, l_pegging_id);
8876 
8877             -- 1487344: instead of getting resource code and department code
8878             -- separately, we get them together to ensure we won't accidently
8879             -- get the line info.  Because we display either owing dept or
8880             -- dept for supply or demand pegging tree, we need to get the name
8881             -- each time.
8882 
8883             --diag_atp: we already got the owning department code before adding pegging for resource demand
8884             l_department_code := l_owning_department_code;
8885 
8886 
8887             /*MSC_ATP_PROC.get_dept_res_code(p_instance_id,
8888                               l_department_id,
8889                               l_resource_id,
8890                               p_organization_id,
8891                               l_department_code,
8892                               l_resource_code); */
8893 
8894             -- add pegging info for the supply
8895 IF PG_DEBUG in ('Y', 'C') THEN
8896    msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'in forward piece, adding resource supply pegging');
8897 END IF;
8898 
8899 
8900             l_pegging_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
8901             l_pegging_rec.order_line_id:= MSC_ATP_PVT.G_ORDER_LINE_ID;
8902             l_pegging_rec.parent_pegging_id:= l_pegging_id;
8903             l_pegging_rec.atp_level:= p_level+1;
8904             l_pegging_rec.organization_id:= p_organization_id;
8905             l_pegging_rec.organization_code := l_org_code;
8906             l_pegging_rec.identifier1:= p_instance_id;
8907             l_pegging_rec.identifier2:= p_plan_id;
8908             l_pegging_rec.identifier3 := -1;
8909 
8910             l_pegging_rec.scenario_id:= p_scenario_id;
8911             l_pegging_rec.supply_demand_source_type:= MSC_ATP_PVT.ATP;
8912             l_pegging_rec.supply_demand_quantity:= l_res_atp_qty;
8913             l_pegging_rec.supply_demand_type:= 2;
8914             l_pegging_rec.supply_demand_date:= l_res_atp_date;
8915             l_pegging_rec.department_id := l_department_id;
8916             l_pegging_rec.department_code := l_department_code;
8917             l_pegging_rec.resource_id := l_resource_id;
8918             l_pegging_rec.resource_code := l_resource_code;
8919             l_pegging_rec.inventory_item_id := NULL;
8920             l_pegging_rec.inventory_item_name := NULL;
8921             l_pegging_rec.supplier_id := NULL;
8922             l_pegging_rec.supplier_name := NULL;
8923             l_pegging_rec.supplier_site_id := NULL;
8924             l_pegging_rec.supplier_site_name := NULL;
8925 
8926              --- resource batching
8927             IF PG_DEBUG in ('Y', 'C') THEN
8928                msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_use_batching = ' || l_use_batching);
8929                msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_batchable_flag = ' || l_batchable_flag);
8930             END IF;
8931             --- bug 1907419
8932             ---IF (l_use_batching = 1 and l_batchable_flag = 1) THEN
8933             IF PG_DEBUG in ('Y', 'C') THEN
8934                msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'ADD batch flag to pegging');
8935             END IF;
8936             l_pegging_rec.batchable_flag := l_batchable_flag;
8937             ---END IF;
8938             --diag_atp
8939             l_pegging_rec.operation_sequence_id := l_res_requirements.operation_sequence(j);
8940             l_pegging_rec.usage := l_res_requirements.actual_resource_usage(j);
8941             l_pegging_rec.offset := l_res_requirements.resource_offset_percent(j);
8942             l_pegging_rec.efficiency := l_res_requirements.efficiency(j);
8943             l_pegging_rec.utilization := l_res_requirements.utilization(j);
8944             l_pegging_rec.REVERSE_CUM_YIELD := l_res_requirements.reverse_cumulative_yield(j);
8945             l_pegging_rec.owning_department := l_owning_department_code;
8946             l_pegging_rec.pegging_type := MSC_ATP_PVT.RESOURCE_SUPPLY; --resource supply node
8947             IF (l_use_batching = 1 AND l_batchable_flag = 1) THEN
8948                 l_pegging_rec.required_quantity:=l_requested_res_qty;
8949             ELSE
8950                 l_pegging_rec.required_quantity:=l_resource_hours;
8951             END IF;
8952             l_pegging_rec.required_date := TRUNC(l_requested_date) + MSC_ATP_PVT.G_END_OF_DAY;
8953             --bug 3328421:
8954             l_pegging_rec.actual_supply_demand_date :=  trunc(l_res_atp_date) + MSC_ATP_PVT.G_END_OF_DAY;
8955             l_pegging_rec.basis_type := l_res_requirements.basis_type(j);
8956             l_pegging_rec.allocation_rule := l_allocation_rule_name;
8957 
8958             -- Bug 3348161 Unset model_sd_flag for future supply case.
8959             l_pegging_rec.model_sd_flag := NULL; -- cto_rearch ODR unset flag for supply.
8960 
8961             l_pegging_rec.source_type := 0;
8962 
8963             -- for demo:1153192
8964             l_pegging_rec.constraint_flag := 'N';
8965             l_pegging_rec.summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;
8966 
8967             --s_cto_rearch
8968             IF l_res_atp_date > l_requested_date THEN
8969                IF PG_DEBUG in ('Y', 'C') THEN
8970 
8971                   msc_sch_wb.atp_debug(' Add resource constraint in regular ATP');
8972                END IF;
8973                --l_pegging_rec.constraint_type := 7;
8974                l_pegging_rec.constraint_type := 6; --bug3533073
8975             END IF;
8976             --e_cto_rearch
8977 
8978             -- Bug 3036513 Add Infinite_Time_fence and ATP Rule Data to Pegging
8979             l_pegging_rec.infinite_time_fence := l_infinite_time_fence_date;
8980             l_pegging_rec.atp_rule_name := l_atp_rule_name;
8981             -- End Bug 3036513
8982              -- Bug 3826234
8983             l_pegging_rec.manufacturing_cal_code :=  l_calendar_code;
8984             l_pegging_rec.organization_type  := NVL ( MSC_ATP_PVT.G_ORG_INFO_REC.org_type, MSC_ATP_PVT.DISCRETE_ORG); --4775920
8985 
8986             MSC_ATP_DB_UTILS.Add_Pegging(l_pegging_rec, l_pegging_id);
8987 
8988           END IF;
8989         END IF;
8990 
8991         IF PG_DEBUG in ('Y', 'C') THEN
8992            msc_sch_wb.atp_debug('in get_res_requirements we are here 1');
8993         END IF;
8994         FOR i in 1..l_atp_period.Level.COUNT LOOP
8995             l_atp_period.Pegging_Id(i) := l_pegging_id;
8996             l_atp_period.End_Pegging_Id(i) := MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
8997 
8998         END LOOP;
8999 
9000         IF PG_DEBUG in ('Y', 'C') THEN
9001            msc_sch_wb.atp_debug('in get_res_requirements we are here 2');
9002         END IF;
9003 
9004 	-- dsting supply/demand details pl/sql tables no longer used
9005 /*
9006         FOR i in 1..l_atp_supply_demand.Level.COUNT LOOP
9007             l_atp_supply_demand.Pegging_Id(i) := l_pegging_id;
9008             l_atp_supply_demand.End_Pegging_Id(i) := MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
9009         END LOOP;
9010 */
9011 
9012 	IF p_insert_flag <> 0 THEN
9013 		MSC_ATP_DB_UTILS.move_SD_temp_into_mrp_details(l_pegging_id,
9014 				      MSC_ATP_PVT.G_DEMAND_PEGGING_ID);
9015 	END IF;
9016 
9017         IF PG_DEBUG in ('Y', 'C') THEN
9018            msc_sch_wb.atp_debug('in get_res_requirements we are here 3');
9019         END IF;
9020 
9021         -- Bug 4108546
9022         -- Re-set the Period Data for HP Display
9023 
9024         FOR k IN 1..l_atp_period.level.COUNT LOOP
9025             IF (l_res_ptf_indx IS NOT NULL) THEN
9026                IF k <= l_res_ptf_indx THEN
9027                   l_atp_period.Cumulative_Quantity(k) := 0;
9028                ELSIF l_atp_period.Cumulative_Quantity(k)
9029                      <> MSC_ATP_PVT.INFINITE_NUMBER  THEN
9030                   l_atp_period.Cumulative_Quantity(k) :=
9031                   l_atp_period.Cumulative_Quantity(k) - l_res_qty_before_ptf;
9032                END IF;
9033                IF PG_DEBUG in ('Y', 'C') THEN
9034                      msc_sch_wb.atp_debug('Get_Res_Requirements: ' ||
9035                        'l_atp_period.Cumulative_Quantity(' || k || ') := ' ||
9036                            l_atp_period.Cumulative_Quantity(k));
9037                END IF;
9038             END IF;
9039         END LOOP;
9040         -- End Bug 4108546
9041 
9042         MSC_ATP_PROC.Details_Output(l_atp_period,
9043                        l_atp_supply_demand,
9044                        x_atp_period,
9045                        x_atp_supply_demand,
9046                        l_return_status);
9047 
9048         IF PG_DEBUG in ('Y', 'C') THEN
9049            msc_sch_wb.atp_debug('in get_res_requirements we are here 4');
9050         END IF;
9051 
9052         -- Bug 2814872
9053         IF (MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 2 AND p_search = 2 AND
9054                                   x_atp_date >= p_parent_ship_date) THEN
9055         -- No need to look at other resources in forward pass as for this resource
9056         -- the availability date is greater than or equal to the parent's ATP date.
9057         -- Note that when p_parent_ship_date is NULL all resources will get visited.
9058            IF PG_DEBUG in ('Y', 'C') THEN
9059                msc_sch_wb.atp_debug('Get_Res_Requirements date obtained for parent item is better');
9060                msc_sch_wb.atp_debug('Get_Res_Requirements date for parent item :' ||
9061                                                                      p_parent_ship_date);
9062                msc_sch_wb.atp_debug('Get_Res_Requirements date obtained for Resource :' ||
9063                                                      l_resource_id || ' is ' || x_atp_date);
9064            END IF;
9065            EXIT; -- Exit the loop
9066         ELSE -- diagnostic ATP or resource provides better date.
9067            j := l_res_requirements.resource_id.NEXT(j);
9068         END IF;
9069         -- End Bug 2814872;
9070 
9071       END LOOP;
9072 
9073   IF PG_DEBUG in ('Y', 'C') THEN
9074      msc_sch_wb.atp_debug('***** End Get_Res_Requirements Procedure *****');
9075   END IF;
9076 Exception
9077     WHEN MSC_ATP_PVT.NO_MATCHING_DATE_IN_CAL THEN --bug3583705
9078        IF PG_DEBUG in ('Y', 'C') THEN
9079             msc_sch_wb.atp_debug('Get_Res_Requirements ' || 'No match for sysdate in cal');
9080        END IF;
9081      RAISE MSC_ATP_PVT.NO_MATCHING_DATE_IN_CAL;
9082     WHEN OTHERS THEN
9083         x_return_status := FND_API.G_RET_STS_ERROR;
9084         IF PG_DEBUG in ('Y', 'C') THEN --bug3583705
9085            msc_sch_wb.atp_debug('Get_Res_Requirements ' || 'inside when others');
9086            msc_sch_wb.atp_debug ('error := ' || SQLERRM);
9087         END IF;
9088         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
9089 END Get_Res_Requirements;
9090 
9091 
9092 PROCEDURE Get_Comp_Requirements (
9093   p_instance_id				IN    NUMBER,
9094   p_plan_id                             IN    NUMBER,
9095   p_level 				IN    NUMBER,
9096   p_scenario_id				IN    NUMBER,
9097   p_inventory_item_id                   IN    NUMBER,
9098   p_organization_id                     IN    NUMBER,
9099   p_parent_pegging_id                   IN    NUMBER,
9100   p_demand_class			IN    VARCHAR2,
9101   p_requested_quantity			IN    NUMBER,
9102   p_requested_date		        IN    DATE,
9103   p_refresh_number			IN    NUMBER,
9104   p_insert_flag				IN    NUMBER,
9105   p_search                              IN    NUMBER,
9106   p_assign_set_id                       IN    NUMBER,
9107   --(ssurendr) Bug 2865389 Added routing Sequence id and Bill sequence id for OPM issue.
9108   p_routing_seq_id                      IN    NUMBER,
9109   p_bill_seq_id                         IN    NUMBER,
9110   p_family_id                           IN    NUMBER,   -- For time_phased_atp
9111   p_atf_date                            IN    DATE,     -- For time_phased_atp
9112   p_manufacturing_cal_code              IN    VARCHAR2, -- For ship_rec_cal
9113   x_avail_assembly_qty			OUT   NoCopy NUMBER,
9114   x_atp_date                            OUT   NoCopy DATE,
9115   x_atp_period                          OUT   NOCOPY MRP_ATP_PUB.ATP_Period_Typ,
9116   x_atp_supply_demand                   OUT   NOCOPY MRP_ATP_PUB.ATP_Supply_Demand_Typ,
9117   x_return_status                       OUT   NoCopy VARCHAR2,
9118   p_comp_info_rec                       IN OUT NOCOPY MSC_ATP_REQ.get_comp_info_rec,
9119   p_order_number                        IN    NUMBER := NULL,
9120   p_op_seq_id                           IN    NUMBER --4570421
9121        -- Add new parameter with default value to support creation of
9122        -- Sales Orders for CTO components in a MATO case.
9123 )
9124 IS
9125 l_comp_requirements 		MRP_ATP_PVT.Atp_Comp_Typ;
9126 l_explode_comp                  MRP_ATP_PVT.Atp_Comp_Typ;
9127 l_atp_period_tab                MRP_ATP_PUB.date_arr:=MRP_ATP_PUB.date_arr();
9128 l_atp_qty_tab                   MRP_ATP_PUB.number_arr:=MRP_ATP_PUB.number_arr();
9129 l_requested_date_quantity	number;
9130 l_resource_id			number;
9131 l_department_id 		number;
9132 l_requested_date 		date;
9133 l_resource_usage 		number;
9134 l_basis_type 			number;
9135 l_requested_res_qty		number;
9136 l_avail_assembly_qty		number;
9137 l_next_period			date;
9138 i				PLS_INTEGER;
9139 j				PLS_INTEGER;
9140 l_requested_comp_qty		number;
9141 l_atp_rec               	MRP_ATP_PVT.AtpRec;
9142 l_atp_period            	MRP_ATP_PUB.ATP_Period_Typ;
9143 l_atp_supply_demand     	MRP_ATP_PUB.ATP_Supply_Demand_Typ;
9144 
9145 l_comp_item_id			number;
9146 l_comp_usage			number;
9147 --4570421
9148 l_scaling_type                  NUMBER;
9149 l_scale_multiple                number;
9150 l_scale_rounding_variance       number;
9151 l_rounding_direction            number;
9152 l_component_yield_factor        NUMBER;
9153 l_usage_qty                     NUMBER;
9154 l_op_seq_id                     NUMBER; --4570421
9155 l_comp_date			date;
9156 l_comp_lead_time		number;
9157 l_comp_pre_pro_lead_time	number;
9158 l_comp_wip_supply_type		number;
9159 l_comp_assembly_identifier	number;
9160 l_comp_component_identifier	number;
9161 l_plan_id                       number;
9162 l_assign_set_id                 number;
9163 l_cto_bom                       number := 0;
9164 l_routing_type	        	number;
9165 l_first_op_RCY			number;
9166 -- l_inv_item_id             	number; -- Not required after 3004862 fix
9167 l_processing_lead_time    	number;
9168 l_temp_date		        DATE;
9169 l_mso_lead_time_factor		number;
9170 l_pre_processing_lead_time	NUMBER;
9171 l_fix_var_lead_time		NUMBER;
9172 l_future_order_date             DATE;
9173 l_ptf_date                      DATE;
9174 l_ptf_flag			NUMBER;
9175 l_plan_info_rec                 MSC_ATP_PVT.plan_info_rec;      -- added for bug 2392456
9176 
9177 l_comp_source_organization_id   NUMBER; -- krajan : 2400614
9178 l_comp_atp_flag_src             VARCHAR2(1); -- krajan : 2462661
9179 
9180 l_reverse_cumulative_yield      NUMBER;
9181 
9182 --(3004862) circular BOM issue
9183 l_bill_seq_id                   NUMBER;
9184 l_routing_seq_id                NUMBER;
9185 l_process_seq_id                NUMBER;
9186 
9187 ----OSFM Changes
9188 --- cursor  for network routing
9189 
9190 -- 2869830
9191 l_rounding_flag                 number;
9192 
9193 --s_cto_rearch
9194 l_component_date               date;
9195 l_model_flag                   number :=2;
9196 L_PLAN_FOUND_FOR_MATCH         number;
9197 L_MODEL_ATP_COMP_FLAG          varchar2(1);
9198 L_MATCH_ITEM_ID                number;
9199 L_CTO_BOM_REC                  MRP_ATP_PVT.Atp_Comp_Typ;
9200 l_lead_time                    number;
9201 L_REQUEST_DATE                 date;
9202 L_MAND_COMP_INFO_REC           MSC_ATP_CTO.mand_comp_info_rec;
9203 l_atp_comp_rec                 MRP_ATP_PVT.ATP_COMP_REC;
9204 l_null_atp_comp_rec            MRP_ATP_PVT.ATP_COMP_REC;
9205 l_item_info_rec                MSC_ATP_PVT.item_attribute_rec;
9206 l_model_error_code                  number := 0;
9207 --e_cto_rearch
9208 
9209 -- time_phased_atp
9210 l_atf_date                     date;
9211 l_pegging_rec                  mrp_atp_details_temp%ROWTYPE;
9212 l_org_code                     varchar2(7);
9213 l_pegging_id                   number;
9214 l_return_status                varchar2(1);
9215 l_network_scheduling_method    NUMBER; --bug3601223
9216 l_first_op_seq_num			   NUMBER; -- Bug 4143668
9217 
9218 -- ATP4drp default variables
9219 l_def_wip_sup_type             NUMBER := 1;
9220 l_def_atf_date                 DATE := NULL;
9221 l_comp_uom                    varchar2(3); --bug3110023
9222 l_alloc_atp varchar2(1); --ALLOC ATP CHANGES, 12973673
9223 
9224 CURSOR net_rout_comp (l_bill_seq_id number, --(3004862) circular BOM issue: changed cursor parameter
9225              l_requested_date date,
9226              l_requested_quantity number,
9227              l_wip_supply_type number) IS --4106269
9228     select  v1.SR_INVENTORY_ITEM_ID,
9229             v1.INVENTORY_ITEM_ID,
9230             v1.qty,
9231             v1.CALENDAR_DATE,
9232             v1.PROCESSING_LEAD_TIME,
9233             v1.WIP_SUPPLY_TYPE,
9234             v1.PREPROCESSING_LEAD_TIME,
9235             v1.REVERSE_CUMULATIVE_YIELD,
9236             v1.AGGREGATE_TIME_FENCE_DATE,
9237             v1.UOM_CODE, --bug3110023
9238             v1.scaling_type,
9239             v1.SCALE_MULTIPLE,
9240             v1.SCALE_ROUNDING_VARIANCE,
9241             v1.ROUNDING_DIRECTION,
9242             v1.component_yield_factor,
9243             v1.usage_qty --4775920
9244     from
9245        (SELECT   I2.SR_INVENTORY_ITEM_ID,
9246                 I2.INVENTORY_ITEM_ID, --(3004862) circular BOM issue: also select destination id
9247                 --4570421
9248                 --4862863, dividing by assembly_qty only in non-lot and non-fix cases.
9249                 ROUND ((decode (NVL ( MSC_ATP_PVT.G_ORG_INFO_REC.org_type, MSC_ATP_PVT.DISCRETE_ORG), 1, decode (nvl (mbc.scaling_type, 1), 1, (MBC.USAGE_QUANTITY*l_requested_quantity)
9250                                                                                                                                                 /Decode (MSC_ATP_PVT.G_PLAN_COPRODUCTS, 'Y', NVL (BOMS.ASSEMBLY_QUANTITY,1), 1),
9251 	                                                                                                                                    2,  MBC.USAGE_QUANTITY),
9252 	                                                                            MSC_ATP_PVT.OPM_ORG, decode (nvl (mbc.scaling_type, 1), 0,  MBC.USAGE_QUANTITY,
9253 	                                                                                                                                    1, (MBC.USAGE_QUANTITY*l_requested_quantity)
9254 	                                                                                                                                        /Decode (MSC_ATP_PVT.G_PLAN_COPRODUCTS, 'Y', NVL (BOMS.ASSEMBLY_QUANTITY,1), 1),
9255 	                                                                                                                                    2,  MBC.USAGE_QUANTITY,
9256 	                                                                                                                                    3, (MBC.USAGE_QUANTITY*l_requested_quantity)
9257 	                                                                                                                                        /Decode (MSC_ATP_PVT.G_PLAN_COPRODUCTS, 'Y', NVL (BOMS.ASSEMBLY_QUANTITY,1), 1),
9258 	                                                                                                                                    4, (MBC.USAGE_QUANTITY*l_requested_quantity)
9259 	                                                                                                                                        /Decode (MSC_ATP_PVT.G_PLAN_COPRODUCTS, 'Y', NVL (BOMS.ASSEMBLY_QUANTITY,1), 1),
9260 	                                                                                                                                    5, (MBC.USAGE_QUANTITY*l_requested_quantity)
9261 	                                                                                                                                        /Decode (MSC_ATP_PVT.G_PLAN_COPRODUCTS, 'Y', NVL (BOMS.ASSEMBLY_QUANTITY,1), 1))
9262 	               ))
9263 	               --/Decode (MSC_ATP_PVT.G_PLAN_COPRODUCTS, 'Y', NVL (BOMS.ASSEMBLY_QUANTITY,1), 1) --4862863
9264 	                  * DECODE (l_routing_type, 3,Decode (l_network_scheduling_method,
9265 	                                                      2,(NVL(OP.NET_PLANNING_PERCENT,100)/100),1), 1)
9266 	                 --/NVL (OP.REVERSE_CUMULATIVE_YIELD, DECODE (l_routing_type, 3, NVL (l_first_op_RCY, 1), 1))
9267 	                 /DECODE(OP.REVERSE_CUMULATIVE_YIELD,
9268                                 0,
9269                                 DECODE(l_routing_type,
9270                                        3, NVL(l_first_op_RCY, 1)
9271                                        ,1
9272                                        ),
9273                                        NVL(OP.REVERSE_CUMULATIVE_YIELD, DECODE(l_routing_type,
9274                                                                                3,
9275                                                                                NVL(l_first_op_RCY, 1),1
9276                                                                                )
9277                                            )
9278                                 )
9279 	                 --/NVL (mbc.component_yield_factor, 1) --4767982
9280 	        ,6) qty,
9281                 C2.CALENDAR_DATE,
9282                 --bug 4106269 changes start here
9283                 /*----------------------------------------------------------------------
9284                 We will include the Lead time for phantom items based on following parameter
9285                 1)MSC: ATP explode phantom components
9286                 2)Bom Parameter -Use Phantom Routing
9287                 3)Bom Parameter -Inherit Phantom Op-Seq
9288                 Various combinations are
9289                 Case1 :
9290                 MSC: ATP explode phantom components =Yes
9291                 Use Phantom Routing =N/a
9292                 Inherit Phantom Op-Seq =N/a
9293 
9294                 Creates supply for ATPable phantom and uses its LT and Routing like a standard item.
9295 
9296                 Case 2:
9297                 MSC: ATP explode phantom components =No
9298                 Use Phantom Routing =No
9299                 Inherit Phantom Op-Seq =No
9300 
9301 		Ignore ATPable phantom's Lead Time for calculating components requirement dates.
9302 		Phantom is exploded to its components and no supply/resource requirements are created for phantom.
9303 
9304 
9305 		Case 3:
9306 		MSC: ATP explode phantom components =No
9307                 Use Phantom Routing =Yes
9308                 Inherit Phantom Op-Seq =No
9309 
9310                 Adds ATPable phantom's Lead Time for calculating components requirement dates.
9311                 Phantom is exploded to its components and no supply/resource requirements are created for phantom.
9312 
9313                 Case 4:
9314                 MSC: ATP explode phantom components =No
9315                 Use Phantom Routing =No
9316                 Inherit Phantom Op-Seq =Yes
9317 
9318                 Ignore ATPable phantom's Lead Time for calculating components requirement dates.
9319                 Phantom is exploded to its components and no supply/resource requirements are created for phantom.
9320 
9321                 Case 5:
9322 
9323 		MSC: ATP explode phantom components =No
9324                 Use Phantom Routing =Yes
9325                 Inherit Phantom Op-Seq =Yes
9326 
9327                 Adds ATPable phantom's Lead Time for calculating components requirement dates.
9328                 Phantom is exploded to its components and no supply/resource requirements are created for phantom.
9329                 --------------------------------------------------------------------------*/
9330                 DECODE(l_wip_supply_type,
9331        				       6,
9332        				 	DECODE(MSC_ATP_PVT.G_EXPLODE_PHANTOM,
9333        					'N',
9334        					decode(nvl(MSC_ATP_PVT.G_ORG_INFO_REC.use_phantom_routings,2),
9335        					       2,
9336        					       0,
9337        					       CEIL((NVL(I.FIXED_LEAD_TIME,0)+
9338        				                     NVL(I.VARIABLE_LEAD_TIME,0)*
9339        					                 l_requested_quantity
9340        					                 )
9341        					                 *(1 + l_mso_lead_time_factor)
9342        						      )
9343        						     ),
9344        						     CEIL( ( NVL(I.FIXED_LEAD_TIME,0)+
9345        							     NVL(I.VARIABLE_LEAD_TIME,0)*
9346        							     l_requested_quantity
9347        							    )*
9348        							   (1 + l_mso_lead_time_factor)
9349        							  )
9350        					        ),
9351        						CEIL((NVL(I.FIXED_LEAD_TIME,0)+
9352        						      NVL(I.VARIABLE_LEAD_TIME,0)*
9353        						      l_requested_quantity)*
9354        						      (1 + l_mso_lead_time_factor)
9355        						     )
9356        		        ) PROCESSING_LEAD_TIME,
9357        		        --4106269
9358                 MBC.WIP_SUPPLY_TYPE,
9359                 --bug3609031 adding ceil
9360                 NVL(ceil(I.PREPROCESSING_LEAD_TIME),0) PREPROCESSING_LEAD_TIME,
9361                 --diag_atp
9362                 --NVL(OP.REVERSE_CUMULATIVE_YIELD, DECODE(l_routing_type, 3, NVL(l_first_op_RCY, 1),1)) REVERSE_CUMULATIVE_YIELD,
9363                 DECODE(OP.REVERSE_CUMULATIVE_YIELD,0,DECODE(l_routing_type, 3, NVL(l_first_op_RCY, 1),1),NVL(OP.REVERSE_CUMULATIVE_YIELD, DECODE(l_routing_type, 3, NVL(l_first_op_RCY, 1),1))) REVERSE_CUMULATIVE_YIELD, --4694958
9364                 -- time_phased_atp
9365                 I2.AGGREGATE_TIME_FENCE_DATE,
9366                 OP.OPERATION_SEQUENCE_ID,
9367                 I2.UOM_CODE, --bug3110023
9368                 --4570421
9369                 mbc.scaling_type scaling_type,
9370                 mbc.scale_multiple scale_multiple,
9371                 mbc.scale_rounding_variance scale_rounding_variance,
9372                 mbc.rounding_direction rounding_direction,
9373                 mbc.component_yield_factor component_yield_factor,
9374                 MBC.USAGE_QUANTITY*mbc.component_yield_factor usage_qty --4775920
9375        FROM     MSC_SYSTEM_ITEMS I2,
9376                 MSC_CALENDAR_DATES C2,
9377 	        MSC_CALENDAR_DATES C1,
9378                 MSC_BOM_COMPONENTS MBC,
9379                 MSC_BOMS BOMS,
9380                 MSC_SYSTEM_ITEMS  I,
9381 	        MSC_OPERATION_COMPONENTS OPC,
9382                 MSC_ROUTING_OPERATIONS OP
9383        WHERE    I.PLAN_ID = BOMS.PLAN_ID
9384        AND      I.SR_INSTANCE_ID = BOMS.SR_INSTANCE_ID
9385        AND      I.INVENTORY_ITEM_ID = BOMS.ASSEMBLY_ITEM_ID
9386        AND      I.ORGANIZATION_ID = BOMS.ORGANIZATION_ID
9387        AND      BOMS.PLAN_ID = p_plan_id
9388        AND      BOMS.SR_INSTANCE_ID = p_instance_id
9389        AND      BOMS.ORGANIZATION_ID = p_organization_id
9390        AND      BOMS.BILL_SEQUENCE_ID = l_bill_seq_id --(3004862) circular BOM issue: use cursor parameter
9391        --(ssurendr) Bug 2865389 Removed condition for Alternate bom designator as
9392        --we are accessing bom by bill sequance id.
9393        --We can now drive by msc_boms for performance gains
9394        --AND      BOMS.ALTERNATE_BOM_DESIGNATOR IS NULL
9395        AND      MBC.USAGE_QUANTITY > 0
9396        AND      MBC.BILL_SEQUENCE_ID = BOMS.BILL_SEQUENCE_ID
9397        AND      MBC.PLAN_ID = I.PLAN_ID
9398        AND      MBC.SR_INSTANCE_ID = I.SR_INSTANCE_ID
9399        --s_cto_rearch: we do not look at atp flags any more. Slection of components is contolled by
9400        --atp flags setting of the components itself
9401        --AND      MBC.ATP_FLAG = 1
9402        /* rajjain 3008611
9403         * effective date should be greater than or equal to greatest of PTF date, sysdate and start date
9404         * disable date should be less than or equal to greatest of PTF date, sysdate and start date*/
9405        AND      TRUNC(NVL(MBC.DISABLE_DATE, GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE)+1)) >
9406                         TRUNC(GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE))
9407        AND      TRUNC(MBC.EFFECTIVITY_DATE) <=
9408                         TRUNC(GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE)) -- bug 1404312
9409        AND      C1.CALENDAR_DATE = l_requested_date
9410        AND      C1.SR_INSTANCE_ID = I.SR_INSTANCE_ID
9411        --4106269
9412        AND      C2.SEQ_NUM = C1.PRIOR_SEQ_NUM -  DECODE(l_wip_supply_type,
9413        				       6,
9414        				 	DECODE(MSC_ATP_PVT.G_EXPLODE_PHANTOM,
9415        					'N',
9416        					decode(nvl(MSC_ATP_PVT.G_ORG_INFO_REC.use_phantom_routings,2),
9417        					       2,
9418        					       0,
9419        					       CEIL((NVL(I.FIXED_LEAD_TIME,0)+
9420        				                     NVL(I.VARIABLE_LEAD_TIME,0)*
9421        					                 l_requested_quantity
9422        					                 )
9423        					                 *(1 + l_mso_lead_time_factor)
9424        						      )
9425        						     ),
9426        						     CEIL( ( NVL(I.FIXED_LEAD_TIME,0)+
9427        							     NVL(I.VARIABLE_LEAD_TIME,0)*
9428        							     l_requested_quantity
9429        							    )*
9430        							   (1 + l_mso_lead_time_factor)
9431        							  )
9432        					        ),
9433        						CEIL((NVL(I.FIXED_LEAD_TIME,0)+
9434        						      NVL(I.VARIABLE_LEAD_TIME,0)*
9435        						      l_requested_quantity)*
9436        						      (1 + l_mso_lead_time_factor)
9437        						     )
9438        		        )--4106269
9439        AND      C2.SR_INSTANCE_ID = I.SR_INSTANCE_ID
9440        AND      I2.INVENTORY_ITEM_ID = MBC.INVENTORY_ITEM_ID
9441        AND      I2.ORGANIZATION_ID =MBC.ORGANIZATION_ID
9442        AND      I2.PLAN_ID = I.PLAN_ID
9443        --4570421
9444        AND      C1.CALENDAR_CODE = MSC_ATP_PVT.G_ORG_INFO_REC.CAL_CODE
9445        AND      C1.EXCEPTION_SET_ID = MSC_ATP_PVT.G_ORG_INFO_REC.CAL_EXCEPTION_SET_ID
9446        AND      C2.CALENDAR_CODE = C1.CALENDAR_CODE
9447        AND      C2.EXCEPTION_SET_ID = MSC_ATP_PVT.G_ORG_INFO_REC.CAL_EXCEPTION_SET_ID
9448        --s_cto_rearch
9449        -- select only atpable components. For model and option class, we do not look at atp components
9450        --flag as they do not carry any significance once config is created
9451        AND (I2.atp_flag <> 'N' or I2.atp_components_flag <> DECODE(I2.BOM_ITEM_TYPE,
9452                                                                           1, I2.atp_components_flag,
9453                                                                           2, I2.atp_components_flag,
9454                                                                           'N'))
9455        AND      I2.SR_INSTANCE_ID = I.SR_INSTANCE_ID
9456        AND      nvl(MBC.WIP_SUPPLY_TYPE,1) <> 5  --7354119,8413492, to filter out records where wip_supply_type is supplier
9457        AND      OPC.PLAN_ID (+) = MBC.PLAN_ID
9458        AND      OPC.ORGANIZATION_ID (+) = MBC.ORGANIZATION_ID
9459        AND      OPC.SR_INSTANCE_ID (+) = MBC.SR_INSTANCE_ID
9460        AND      OPC.COMPONENT_SEQUENCE_ID (+) = MBC.COMPONENT_SEQUENCE_ID
9461        AND      OPC.BILL_SEQUENCE_ID (+) = MBC.BILL_SEQUENCE_ID
9462        AND      OP.PLAN_ID (+) = OPC.PLAN_ID
9463        AND      OP.SR_INSTANCE_ID (+) = OPC.SR_INSTANCE_ID
9464        AND      OP.OPERATION_SEQUENCE_ID (+) = OPC.OPERATION_SEQUENCE_ID
9465        AND      OP.ROUTING_SEQUENCE_ID (+) = OPC.ROUTING_SEQUENCE_ID ) v1
9466        --bug3601223 get the components that are used on primary path
9467     where       l_routing_type <> 3
9468        OR       l_network_scheduling_method = 2
9469        OR       v1.OPERATION_SEQUENCE_ID IS NULL
9470        OR       v1.OPERATION_SEQUENCE_ID IN
9471                               ( SELECT FROM_OP_SEQ_ID
9472                                 FROM  MSC_OPERATION_NETWORKS
9473                                 WHERE  PLAN_ID = p_plan_id
9474 				AND    SR_INSTANCE_ID = p_instance_id
9475 				AND    ROUTING_SEQUENCE_ID = p_routing_seq_id
9476 				AND    TRANSITION_TYPE = 1
9477 
9478                                 UNION ALL
9479 
9480                                 SELECT TO_OP_SEQ_ID
9481 				FROM  MSC_OPERATION_NETWORKS
9482 				WHERE  PLAN_ID = p_plan_id
9483 				AND    SR_INSTANCE_ID = p_instance_id
9484 				AND    ROUTING_SEQUENCE_ID = p_routing_seq_id
9485 				AND    TRANSITION_TYPE = 1
9486 			      );
9487 
9488 -- ATP4drp New Cursor for handling DRP Kitting.
9489 -- Routing, Net Planning Percent and Operations not applicable for DRP plans.
9490 -- Co-products also not applicable for DRP plans.
9491 CURSOR drp_comp (l_bill_seq_id number, --(3004862) circular BOM issue: changed cursor parameter
9492              l_requested_date date,
9493              l_requested_quantity number) IS
9494        SELECT   I2.SR_INVENTORY_ITEM_ID,
9495                 I2.INVENTORY_ITEM_ID, --(3004862) circular BOM issue: also select destination id
9496                 --ROUND((MBC.USAGE_QUANTITY * l_requested_quantity ),6),
9497                 --4570421
9498                 ROUND ((decode (NVL (MSC_ATP_PVT.G_ORG_INFO_REC.org_type, MSC_ATP_PVT.DISCRETE_ORG), 1, decode ( nvl(mbc.scaling_type, 1), 1,  (MBC.USAGE_QUANTITY*l_requested_quantity),
9499 	                                                                                                                                   2, MBC.USAGE_QUANTITY),
9500 	                                                                           MSC_ATP_PVT.OPM_ORG, decode (nvl (mbc.scaling_type, 1), 0, MBC.USAGE_QUANTITY,
9501 	                                                                                                                                   1, (MBC.USAGE_QUANTITY*l_requested_quantity),
9502 	                                                                                                                                   2, MBC.USAGE_QUANTITY,
9503 	                                                                                                                                   3, (MBC.USAGE_QUANTITY*l_requested_quantity),
9504 	                                                                                                                                   4, (MBC.USAGE_QUANTITY*l_requested_quantity),
9505 	                                                                                                                                   5, (MBC.USAGE_QUANTITY*l_requested_quantity))
9506 	               )) --/NVL (mbc.component_yield_factor, 1)     --4767982
9507 	               ,6),
9508                 C2.CALENDAR_DATE,
9509                 CEIL((NVL(MSC_ATP_PVT.G_ITEM_INFO_REC.FIXED_LT,0)+
9510                         NVL(MSC_ATP_PVT.G_ITEM_INFO_REC.VARIABLE_LT,0)* l_requested_quantity)*(1 + l_mso_lead_time_factor)),
9511                 DECODE(MBC.WIP_SUPPLY_TYPE, 6, l_def_wip_sup_type, MBC.WIP_SUPPLY_TYPE), -- phantoms not be supported for DRP plans
9512                 NVL(ceil(MSC_ATP_PVT.G_ITEM_INFO_REC.PRE_PRO_LT),0),
9513                 1, -- default for RCY Yield unsuppored for DRP plans.
9514                 l_def_atf_date, -- ATF will be NULL as a default for DRP plans.
9515                 I2.UOM_CODE, --bug3110023
9516                 --4570421
9517                 mbc.scaling_type,
9518                 mbc.scale_multiple,
9519                 mbc.scale_rounding_variance ,
9520                 mbc.rounding_direction,
9521                 mbc.component_yield_factor, --4570421
9522                 MBC.USAGE_QUANTITY*mbc.component_yield_factor --4775920
9523        FROM     MSC_SYSTEM_ITEMS I2,
9524                 MSC_CALENDAR_DATES C2,
9525 	        MSC_CALENDAR_DATES C1,
9526                 MSC_BOM_COMPONENTS MBC,
9527                 MSC_BOMS BOMS
9528        WHERE    BOMS.PLAN_ID = p_plan_id
9529        AND      BOMS.SR_INSTANCE_ID = p_instance_id
9530        AND      BOMS.ORGANIZATION_ID = p_organization_id
9531        AND      BOMS.BILL_SEQUENCE_ID = l_bill_seq_id --(3004862) circular BOM issue: use cursor parameter
9532        AND      MBC.USAGE_QUANTITY > 0
9533        AND      MBC.BILL_SEQUENCE_ID = BOMS.BILL_SEQUENCE_ID
9534        AND      MBC.PLAN_ID = BOMS.PLAN_ID
9535        AND      MBC.SR_INSTANCE_ID = BOMS.SR_INSTANCE_ID
9536        AND      TRUNC(NVL(MBC.DISABLE_DATE, GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE)+1)) >
9537                         TRUNC(GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE))
9538        AND      TRUNC(MBC.EFFECTIVITY_DATE) <=
9539                         TRUNC(GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE)) -- bug 1404312
9540        AND      C1.CALENDAR_DATE = l_requested_date
9541        AND      C1.SR_INSTANCE_ID = BOMS.SR_INSTANCE_ID
9542        AND      C1.CALENDAR_CODE = MSC_ATP_PVT.G_ORG_INFO_REC.CAL_CODE
9543        AND      C1.EXCEPTION_SET_ID = MSC_ATP_PVT.G_ORG_INFO_REC.CAL_EXCEPTION_SET_ID
9544        AND      C2.SEQ_NUM = C1.PRIOR_SEQ_NUM - CEIL((NVL(MSC_ATP_PVT.G_ITEM_INFO_REC.FIXED_LT,0)+
9545                         NVL(MSC_ATP_PVT.G_ITEM_INFO_REC.VARIABLE_LT,0)* l_requested_quantity)*(1 + l_mso_lead_time_factor))
9546        AND      C2.CALENDAR_CODE = C1.CALENDAR_CODE
9547        AND      C2.SR_INSTANCE_ID = C1.SR_INSTANCE_ID
9548        AND      C2.EXCEPTION_SET_ID = MSC_ATP_PVT.G_ORG_INFO_REC.CAL_EXCEPTION_SET_ID
9549        AND      I2.INVENTORY_ITEM_ID = MBC.INVENTORY_ITEM_ID
9550        AND      I2.ORGANIZATION_ID =MBC.ORGANIZATION_ID
9551        AND      I2.PLAN_ID = BOMS.PLAN_ID
9552                 -- select only atpable components.
9553        AND      I2.atp_flag <> 'N'
9554        AND      I2.SR_INSTANCE_ID = BOMS.SR_INSTANCE_ID;
9555 -- End ATP4drp New Cursor for handling DRP Kitting.
9556 
9557 CURSOR cto_comp (l_inventory_item_id number,
9558              l_requested_date date,
9559              l_requested_quantity number,
9560              l_wip_supply_type number) IS
9561        SELECT   mbt.component_item_id,
9562                 (mbt.quantity * l_requested_quantity),
9563                 c2.calendar_date,
9564                 DECODE(l_wip_supply_type,
9565 			6, 0,
9566 			CEIL((NVL(mbt.fixed_lt,0)+
9567                         NVL(mbt.variable_lt,0) * l_requested_quantity)*(1 + l_mso_lead_time_factor))) lead_time,
9568                 wip_supply_type,
9569 		mbt.assembly_identifier,
9570 		mbt.component_identifier,
9571                 mbt.pre_process_lt,
9572                 -- krajan : 2400614
9573                 mbt.source_organization_id,
9574                 -- krajan : 2462661
9575                 mbt.atp_flag
9576        FROM     msc_bom_temp mbt,
9577                 msc_calendar_dates c2,
9578                 msc_calendar_dates c1,
9579                 msc_trading_partners tp
9580        WHERE	mbt.session_id = MSC_ATP_PVT.G_SESSION_ID
9581        --AND      mbt.assembly_identifier = MSC_ATP_PVT.G_ASSEMBLY_LINE_ID
9582        AND      mbt.assembly_identifier = MSC_ATP_PVT.G_COMP_LINE_ID
9583        AND      mbt.assembly_item_id = l_inventory_item_id
9584        /* rajjain 3008611
9585         * effective date should be greater than or equal to greatest of PTF date, sysdate and start date
9586         * disable date should be less than or equal to greatest of PTF date, sysdate and start date*/
9587        AND      TRUNC(NVL(mbt.disable_date, GREATEST(sysdate, c2.calendar_date, MSC_ATP_PVT.G_PTF_DATE)+1)) >
9588                   TRUNC(GREATEST(sysdate, c2.calendar_date, MSC_ATP_PVT.G_PTF_DATE))
9589        AND      TRUNC(mbt.effective_date) <=
9590                   TRUNC(GREATEST(sysdate, c2.calendar_date, MSC_ATP_PVT.G_PTF_DATE))
9591        AND	c1.calendar_date = l_requested_date
9592        AND      c1.sr_instance_id = tp.sr_instance_id
9593        AND      c1.calendar_code = tp.calendar_code
9594        AND      c1.exception_set_id = tp.calendar_exception_set_id
9595        AND      tp.sr_instance_id = p_instance_id
9596        AND      tp.sr_tp_id = p_organization_id
9597        AND      tp.partner_type = 3
9598        AND      c2.seq_num = c1.prior_seq_num -
9599                              DECODE(l_wip_supply_type,
9600                                 6, 0,
9601                                 CEIL((NVL(mbt.fixed_lt,0)+
9602                                      NVL(mbt.variable_lt,0) * l_requested_quantity)*(1 + l_mso_lead_time_factor)))
9603        AND      c2.calendar_code = tp.calendar_code
9604        AND      c2.sr_instance_id = tp.sr_instance_id
9605        AND      c2.exception_set_id = tp.calendar_exception_set_id;
9606 
9607 BEGIN
9608     -- initialize API return status to success
9609     x_return_status := FND_API.G_RET_STS_SUCCESS;
9610 
9611     IF PG_DEBUG in ('Y', 'C') THEN
9612        msc_sch_wb.atp_debug('***** Begin Get_Comp_Requirements *****');
9613     END IF;
9614 
9615     -- Now get the material requirement
9616     IF PG_DEBUG in ('Y', 'C') THEN
9617        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'p_instance_id = '||p_instance_id);
9618        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'p_plan_id = '||p_plan_id);
9619        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'p_level = '||p_level);
9620        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'p_scenario_id = '||p_scenario_id);
9621        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'p_inventory_item_id = '||p_inventory_item_id);
9622        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'p_organization_id = '||p_organization_id);
9623        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'p_parent_pegging_id = '||p_parent_pegging_id);
9624        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'p_demand_class = '||p_demand_class);
9625        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'p_requested_quantity = '||p_requested_quantity);
9626        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'p_requested_date = '||p_requested_date);
9627        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'p_search = '||p_search);
9628        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'p_assign_set_id = '||p_assign_set_id);
9629        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'p_order_number = '||p_order_number);
9630        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'p_routing_seq_id = '||p_routing_seq_id);
9631        msc_sch_wb.atp_debug('bom_item_type := ' || p_comp_info_rec.bom_item_type);
9632        msc_sch_wb.atp_debug('atp flag := ' || p_comp_info_rec.atp_flag);
9633        msc_sch_wb.atp_debug('atp_comp_flag := ' || p_comp_info_rec.atp_comp_flag);
9634        msc_sch_wb.atp_debug('p_bill_seq_id := ' || p_bill_seq_id);
9635         msc_sch_wb.atp_debug('parent_so_quantity := ' || p_comp_info_rec.parent_so_quantity);
9636        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'p_family_id := ' || p_family_id);
9637        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'p_atf_date := ' || p_atf_date);
9638        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'TOP_MODEL_LINE_ID :=' || p_comp_info_rec.TOP_MODEL_LINE_ID);
9639        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'ATO_MODEL_LINE_ID :=' || p_comp_info_rec.ATO_MODEL_LINE_ID);
9640        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'ATO_PARENT_MODEL_LINE_ID := ' || p_comp_info_rec.ATO_PARENT_MODEL_LINE_ID);
9641        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'PARENT_LINE_ID := ' || p_comp_info_rec.PARENT_LINE_ID);
9642        --bug 3059305
9643        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'ship date this level := '
9644                                                       || p_comp_info_rec.ship_date_this_level);
9645 
9646 
9647     END IF;
9648     --S_cto_rearch: If ATP flag on config is 'Y, N' then we check base model's
9649     -- cpacity based on bom level atp flag and atp flags of base model
9650     -- In this case we just need to check capacity on model. So model is only
9651     --component we need to check capacity on.
9652     MSC_ATP_PROC.get_global_item_info(p_instance_id,
9653                                       --3917625: Read data from the plan
9654                                       -- -1,
9655                                       p_plan_id,
9656                                       p_inventory_item_id,
9657                                       p_organization_id,
9658                                       l_item_info_rec);
9659     IF  p_comp_info_rec.check_model_capacity_flag = 1 THEN
9660        --check model's capacity. Add model to comp array
9661 
9662        MSC_ATP_REQ.Extend_Atp_Comp_Typ(l_comp_requirements);
9663 
9664        l_comp_requirements.inventory_item_id(1) :=  MSC_ATP_PF.Get_PF_Atp_Item_Id(
9665                                                                 p_instance_id,
9666                                                                 -1, -- plan_id
9667                                                                 p_comp_info_rec.model_sr_inv_item_id,
9668                                                                 p_organization_id
9669                                                         );
9670        l_comp_requirements.request_item_id(1) :=
9671                          p_comp_info_rec.model_sr_inv_item_id;
9672 
9673        -- I used this comp_usaget to store the required quantity at this level.
9674        l_comp_requirements.comp_usage(1) := p_requested_quantity;
9675 
9676        l_comp_requirements.requested_date(1) := p_requested_date;
9677 
9678        -- assume the wip supply type to be 1 (as long as it is not phantom)
9679        l_comp_requirements.wip_supply_type(1) := 1;
9680        l_comp_requirements.component_identifier(1) := MSC_ATP_PVT.G_ITEM_INFO_REC.dest_inv_item_id;
9681 
9682        l_comp_requirements.bom_item_type(1) :=  1; -- hard code to model's bom type
9683        l_comp_requirements.parent_so_quantity(1) := p_comp_info_rec.parent_so_quantity;
9684        l_comp_requirements.assembly_identifier(1) :=  null;
9685        l_comp_requirements.top_model_line_id(1) := null;
9686        l_comp_requirements.ato_parent_model_line_id(1) := null;
9687        l_comp_requirements.ato_model_line_id(1) := null;
9688        l_comp_requirements.parent_line_id(1) := null;
9689        l_comp_requirements.fixed_lt(1) := 0;
9690        l_comp_requirements.variable_lt(1) := 0;
9691        l_comp_requirements.dest_inventory_item_id(1) :=  null;
9692        l_comp_requirements.lead_time(1) := 0;
9693 
9694     ELSE
9695 
9696        -- assign this assembly into the l_explode_comp record of tables.
9697        -- we will loop through this l_explode_comp to do the explosion.
9698        -- And we will add phantom item into this l_explode_comp so that
9699        -- we can explode through phantom.
9700        MSC_ATP_REQ.Extend_Atp_Comp_Typ(l_explode_comp);
9701 
9702        l_explode_comp.inventory_item_id(1) := p_inventory_item_id;
9703 
9704        -- I used this comp_usaget to store the required quantity at this level.
9705        l_explode_comp.comp_usage(1) := p_requested_quantity;
9706 
9707        l_explode_comp.requested_date(1) := p_requested_date;
9708 
9709        -- assume the wip supply type to be 1 (as long as it is not phantom)
9710        l_explode_comp.wip_supply_type(1) := 1;
9711        l_explode_comp.component_identifier(1) := MSC_ATP_PVT.G_ITEM_INFO_REC.dest_inv_item_id;
9712 
9713        l_explode_comp.bom_item_type(1) :=  p_comp_info_rec.bom_item_type;
9714        l_explode_comp.parent_so_quantity(1) := p_comp_info_rec.parent_so_quantity;
9715        l_explode_comp.assembly_identifier(1) :=  p_comp_info_rec.line_id;
9716        l_explode_comp.top_model_line_id(1) := p_comp_info_rec.top_model_line_id;
9717        l_explode_comp.ato_parent_model_line_id(1) := p_comp_info_rec.ato_parent_model_line_id;
9718        l_explode_comp.ato_model_line_id(1) := p_comp_info_rec.ato_model_line_id;
9719        l_explode_comp.parent_line_id(1) := p_comp_info_rec.parent_line_id;
9720        l_explode_comp.fixed_lt(1) := p_comp_info_rec.fixed_lt;
9721        l_explode_comp.variable_lt(1) := p_comp_info_rec.variable_lt;
9722        l_explode_comp.dest_inventory_item_id(1) :=  MSC_ATP_PVT.G_ITEM_INFO_REC.dest_inv_item_id;
9723 
9724        --(3004862) circular BOM issue - initialize with end item's destination id.
9725 
9726         /*---bug 2680027: Extend lead_time field. This field will contain lead time of  the parent if
9727          the current comp is a phontom
9728        --We need to remember this lead time so that it could be passed to the component of the phantom
9729 
9730                     If Bom IS
9731                        A
9732                        |  Lead _time = 10
9733                        B
9734                        |  Lead time = 5
9735                        C
9736                If B is phantom then prior to this fix we were effectively saying that lead time between A and C is 5 days
9737                It should be 15 days instead. So in lead time field, we are going to save 0 form A
9738                as A doesn't have any par ent.
9739                For B, this filed will contain 10 as lead time between B and its Parent A is 10 days.
9740 
9741        */
9742        l_explode_comp.lead_time(1) := 0;
9743     END IF; --- p_comp_info_rec.check_model_capacity_flag = 1 THEN
9744 
9745     l_org_code := MSC_ATP_PVT.G_ORG_INFO_REC.org_code;
9746        --- get mso_sco_lead_time_factor
9747     l_mso_lead_time_factor := MSC_ATP_PVT.G_MSO_LEAD_TIME_FACTOR;
9748     l_network_scheduling_method := MSC_ATP_PVT.G_ORG_INFO_REC.network_scheduling_method; --bug3601223
9749 
9750      IF PG_DEBUG in ('Y', 'C') THEN
9751        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'MSO LEAD TIME FACTOR =  ' || l_mso_lead_time_factor);
9752        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'after assign the value');
9753        --bug3601223
9754        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_network_scheduling_method = '|| l_network_scheduling_method);
9755     END IF;
9756 
9757     i :=  l_explode_comp.inventory_item_id.FIRST;
9758     l_bill_seq_id := p_bill_seq_id;  --(3004862) circular BOM issue
9759 
9760     -- Check if Records exist in MSC_BOM_TEMP table in case of PDS. If yes,
9761     -- open cto_comp cursor instead of cursor comp.
9762 
9763     -- 2869830
9764     l_rounding_flag := nvl(MSC_ATP_PVT.G_ITEM_INFO_REC.rounding_control_type, 2);
9765 
9766     -- 3027711 move this to the beginning
9767     -- initially set the x_avail_assembly_qty to the p_requested_quantity,
9768     -- and we adjust that later
9769     -- 2869830
9770     IF l_rounding_flag = 1 THEN
9771        x_avail_assembly_qty :=  CEIL(p_requested_quantity);
9772     ELSE
9773        x_avail_assembly_qty :=  trunc(p_requested_quantity, 6);    -- 5598066
9774     END IF;
9775 
9776     -- bug 2178544
9777     x_atp_date := GREATEST(p_requested_date, MSC_ATP_PVT.G_FUTURE_ORDER_DATE, MSC_ATP_PVT.G_FUTURE_START_DATE);
9778     l_ptf_date := MSC_ATP_PVT.G_PTF_DATE;
9779 
9780     IF p_bill_seq_id is null and p_comp_info_rec.check_model_capacity_flag = 2 THEN
9781        RETURN;
9782     END IF;
9783     -- end 3027711
9784 
9785     --s_cto_rearch
9786     IF p_comp_info_rec.bom_item_type = 1  and p_comp_info_rec.replenish_to_order_flag = 'Y' THEN
9787         IF PG_DEBUG in ('Y', 'C') THEN
9788           msc_sch_wb.atp_debug('This is a model line');
9789         END IF;
9790         l_model_atp_comp_flag := p_comp_info_rec.atp_comp_flag;
9791         l_model_flag := p_comp_info_rec.bom_item_type;
9792 
9793         IF p_comp_info_rec.atp_flag = 'Y' THEN
9794            IF PG_DEBUG in ('Y', 'C') THEN
9795               msc_sch_wb.atp_debug('Add this line to comp requirements table');
9796            END IF;
9797            --first extend the record
9798            MSC_ATP_REQ.Extend_Atp_Comp_Typ(l_comp_requirements);
9799 
9800            --now add the model iteself
9801            --l_comp_requirements.inventory_item_id(1) := p_inventory_item_id;
9802            /* time_phased_atp changes begin
9803               Support PF ATP for components*/
9804            -- check this with vivek if inventory_item_id, request_item_id and atf_date need to be populated for model
9805            l_comp_requirements.inventory_item_id(1) := p_family_id;
9806            l_comp_requirements.request_item_id(1) := p_inventory_item_id;
9807            l_comp_requirements.atf_date(1) := p_atf_date;
9808            -- time_phased_atp changes end
9809 
9810            l_comp_requirements.comp_usage(1) := 1;
9811            l_comp_requirements.requested_date(1) := p_requested_date;
9812            l_comp_requirements.lead_time(1) := 0;
9813            l_comp_requirements.wip_supply_type(1) := 1;
9814            l_comp_requirements.assembly_identifier(1) := p_comp_info_rec.line_id;
9815            l_comp_requirements.component_identifier(1) := null;
9816            l_comp_requirements.atp_flag(1) := p_comp_info_rec.atp_flag;
9817            l_comp_requirements.parent_item_id(1) := p_inventory_item_id;
9818         END IF;
9819 
9820     /* ELSIF  p_comp_info_rec.bom_item_type = 4 and p_comp_info_rec.replenish_to_order_flag = 'Y'
9821                                           and MSC_ATP_PVT.G_INV_CTP = 5 THEN
9822         l_model_flag := 1;
9823         l_model_atp_comp_flag := p_comp_info_rec.atp_comp_flag;
9824     */
9825     END IF;
9826     --e_cto_rearch
9827 
9828     --4570421
9829     l_routing_seq_id := p_routing_seq_id;
9830     l_op_seq_id := p_op_seq_id;
9831 
9832     WHILE i IS NOT NULL LOOP
9833 
9834        IF p_comp_info_rec.check_model_capacity_flag = 1 THEN
9835            -- we are checking only base model's capacity. Therefore, we already
9836            --have all the components we need. Threfore we exit out
9837            EXIT;
9838        END IF;
9839 
9840        -- l_inv_item_id  :=  MSC_ATP_PVT.G_ITEM_INFO_REC.dest_inv_item_id; -- Not required after 3004862 fix
9841        IF PG_DEBUG in ('Y', 'C') THEN
9842           msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_explode_comp.component_identifier(' || i || ') : ' || l_explode_comp.component_identifier(i));
9843        END IF;
9844 
9845        -- Bug 4143668, reset routing type ti ensure correct data in between various iterations
9846        l_routing_type := NULL;
9847 
9848        -- 3027711
9849        IF l_routing_seq_id is not null THEN --4570421 replaced p_routing_seq_id by l_routing_seq_id
9850           BEGIN
9851 
9852              ---bug 2320808, set l_first_op_RCY to 1 if null
9853              -- Bug 4143668, Commented as separate SQLs will be used for routing_type, first_op_seq_num in msc_routings
9854              -- and l_first_op_RCY from msc_routing_operations
9855              /*
9856              SELECT r.cfm_routing_flag, NVL(op.reverse_cumulative_yield,1)
9857              INTO l_routing_type, l_first_op_RCY
9858              FROM msc_routings r, msc_routing_operations op
9859              WHERE r.plan_id = p_plan_id and
9860                    r.organization_id = p_organization_id and
9861                    r.sr_instance_id = p_instance_id and
9862                    -- r.assembly_item_id = l_inv_item_id and
9863                    -- (3004862) changed to l_explode_comp.component_identifier so that for i>1 phantom item's id is used.
9864                    r.assembly_item_id = l_explode_comp.component_identifier(i) and
9865                    r.routing_sequence_id = p_routing_seq_id and
9866                    --(ssurendr) Bug 2865389 Removed condition for Alternate Routing designator as
9867                    --we are accessing Routing by Routing sequance id.
9868                    --r.alternate_routing_designator IS NULL and
9869                    r.plan_id = op.plan_id and
9870                    r.sr_instance_id = op.sr_instance_id and
9871                                     --r.organization_id = op.organization_id and
9872                    r.routing_sequence_id = op.routing_sequence_id and
9873                    NVL(r.first_op_seq_num,op.operation_seq_num) = op.operation_seq_num and     -- bug4114765
9874                    rownum = 1; -- Bug 4143668, just pick one for case where r.first_op_seq_num is NULL
9875              */
9876 
9877              -- Bug 4143668, check routing type and first_op_seq_num at routing level
9878              -- Also removed check for assembly item and org as routing sequence, plan and instance is unique.
9879              SELECT r.cfm_routing_flag, r.first_op_seq_num
9880              INTO   l_routing_type, l_first_op_seq_num
9881              FROM   msc_routings r
9882              WHERE  r.plan_id = p_plan_id
9883              AND    r.sr_instance_id = p_instance_id
9884              AND    r.routing_sequence_id = l_routing_seq_id; --4570421 replaced p_routing_seq_id by l_routing_seq_id
9885 
9886              IF PG_DEBUG in ('Y', 'C') THEN
9887                 msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_routing_type := ' || l_routing_type);
9888                 msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'routing l_first_op_seq_num := ' || l_first_op_seq_num);
9889              END IF;
9890 
9891              -- Bug 4143668, check lowest op seq from routing operation as routing level data is only populated for network routings
9892              /* Not needed as of now as RCY is not populated for non-network routing.
9893              IF l_first_op_seq_num IS NULL THEN
9894                 SELECT MIN(op.operation_seq_num)
9895                 INTO   l_first_op_seq_num
9896                 FROM   msc_routing_operations op
9897                 WHERE  op.routing_sequence_id = p_routing_seq_id
9898                 AND    op.sr_instance_id = p_instance_id
9899                 AND    op.plan_id = p_plan_id;
9900 
9901                 IF PG_DEBUG in ('Y', 'C') THEN
9902                    msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'operations l_first_op_seq_num := ' || l_first_op_seq_num);
9903                 END IF;
9904              END IF; 	-- IF l_first_op_seq_num IS NULL THEN
9905              */
9906 
9907              -- Bug 4143668, get first operation's RCY based on op seq num found earlier
9908              IF l_first_op_seq_num IS NOT NULL THEN
9909                 SELECT DECODE(op.reverse_cumulative_yield,0,1,NVL(op.reverse_cumulative_yield,1)) --4694958
9910                 INTO   l_first_op_RCY
9911                 FROM   msc_routing_operations op
9912                 WHERE  op.plan_id = p_plan_id
9913                 AND    op.sr_instance_id = p_instance_id
9914                 AND    op.routing_sequence_id = l_routing_seq_id --4570421 replaced p_routing_seq_id by l_routing_seq_id
9915                 AND    op.operation_seq_num = l_first_op_seq_num
9916                 and    op.operation_sequence_id = l_op_seq_id; --4570421
9917 
9918                 IF PG_DEBUG in ('Y', 'C') THEN
9919                    msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_first_op_RCY := ' || l_first_op_RCY);
9920                 END IF;
9921              ELSE
9922                 l_first_op_RCY := 1; -- Bug 4143668, l_first_op_RCY to 1 if Null
9923              END IF; 	-- IF l_first_op_seq_num IS NOT NULL THEN
9924           EXCEPTION
9925               WHEN OTHERS THEN
9926                   -- Bug 4143668, add debug message for exception
9927                   IF PG_DEBUG in ('Y', 'C') THEN
9928                      msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'exception in cfm: '|| sqlcode);
9929                      msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'set l_routing_type and l_first_op_RCY');
9930                   END IF;
9931                   l_routing_type := NVL(l_routing_type, 2); -- Bug 4143668, use l_routing_type if already set, else 2
9932                   l_first_op_RCY := 1; -- Bug 4143668, l_first_op_RCY to 1 for exception
9933           END;
9934        ELSE
9935           l_routing_type := NVL(l_routing_type, 2); -- Bug 4143668, use l_routing_type if already set, else 2
9936           l_first_op_RCY := 1; -- Bug 4143668, l_first_op_RCY to 1 if Null
9937        END IF;
9938 
9939        IF PG_DEBUG in ('Y', 'C') THEN
9940           msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_routing_type := ' || l_routing_type);
9941           msc_sch_wb.atp_debug('l_model_flag := ' || l_model_flag);
9942        END IF;
9943        IF NVL(l_model_flag, 2) <> 1 THEN
9944            IF i <> 1 THEN
9945                -- (3004862) circular BOM issue: Call get process effectivity again
9946                -- for the Phantom Component's Bill.
9947                msc_sch_wb.atp_debug('Calling Process effectivity for Phantom:'|| l_explode_comp.component_identifier(i));
9948                MSC_ATP_PROC.get_process_effectivity(
9949                    p_plan_id,
9950                    l_explode_comp.component_identifier(i),
9951                    p_organization_id,
9952                    p_instance_id,
9953                    l_explode_comp.requested_date(i),
9954                    l_explode_comp.comp_usage(i),
9955                    l_process_seq_id,
9956                    l_routing_seq_id,
9957                    l_bill_seq_id,
9958                    l_op_seq_id, --4570421
9959                    x_return_status);
9960                IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
9961                    RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
9962                END IF;
9963            END IF;
9964 
9965            -- 3027711
9966            IF l_bill_seq_id is not null THEN
9967 
9968               ---- Get the bom from msc_bom_components and network routing
9969               -- ATP4drp open DRP specific cursor for DRP plans.
9970               IF NVL(MSC_ATP_PVT.G_PLAN_INFO_REC.plan_type, 1) <> 5 THEN
9971 
9972                   OPEN net_rout_comp(l_bill_seq_id, -- (3004862) circular BOM issue: call with changed parameter
9973                                      l_explode_comp.requested_date(i),
9974                                      l_explode_comp.comp_usage(i),
9975                                      l_explode_comp.wip_supply_type(i)); --4106269
9976                   IF PG_DEBUG in ('Y', 'C') THEN
9977                      msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'after opening network routing cursor net_rout_com');
9978                   END IF;
9979               ELSE
9980 
9981                   OPEN drp_comp(l_bill_seq_id, -- (3004862) circular BOM issue: call with changed parameter
9982                                 l_explode_comp.requested_date(i),
9983                                 l_explode_comp.comp_usage(i));
9984                   IF PG_DEBUG in ('Y', 'C') THEN
9985                      msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'after opening DRP cursor drp_comp');
9986                      msc_sch_wb.atp_debug('----- ATP4drp Specific Debug Messages -----');
9987                   END IF;
9988               END IF;
9989               -- End ATP4drp
9990 
9991               IF PG_DEBUG in ('Y', 'C') THEN
9992                   msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'after opening network routing cursor net_rout_com');
9993                   msc_sch_wb.atp_debug('l_bill_seq_id '||l_bill_seq_id);
9994                   msc_sch_wb.atp_debug('requested_date '||l_explode_comp.requested_date(i));
9995                   msc_sch_wb.atp_debug('comp_usage '|| l_explode_comp.comp_usage(i));
9996               END IF;
9997            END IF;
9998        ELSIF l_model_flag = 1 THEN
9999           IF PG_DEBUG in ('Y', 'C') THEN
10000              msc_sch_wb.atp_debug('Explode Model or its components  Bom');
10001              msc_sch_wb.atp_debug('l_model_atp_comp_flag := ' || l_model_atp_comp_flag);
10002           END IF;
10003           IF l_model_atp_comp_flag in ('Y', 'C') THEN
10004              ---explode mandatory components if model or option class
10005              IF l_explode_comp.bom_item_type(i) in (1,2) or
10006                        (l_explode_comp.bom_item_type(i) = 4 and MSC_ATP_PVT.G_INV_CTP = 5)  THEN
10007 
10008                  IF PG_DEBUG in ('Y', 'C') THEN
10009                      msc_sch_wb.atp_debug('i = ' || i);
10010                  END IF;
10011                  l_lead_time := CEIL((NVL(l_explode_comp.fixed_lt(i), 0) +
10012                                              NVL(l_explode_comp.variable_lt(i), 0)*l_explode_comp.comp_usage(i))
10013                                                  * (1 + l_mso_lead_time_factor));
10014 
10015                  IF PG_DEBUG in ('Y', 'C') THEN
10016                      msc_sch_wb.atp_debug('l_lead_time = ' || l_lead_time);
10017                  END IF;
10018 
10019                  IF nvl(l_lead_time, 0) > 0 AND p_search = 1 THEN
10020                    l_request_date := MSC_CALENDAR.DATE_OFFSET
10021                                              (p_organization_id,
10022                                               p_instance_id,
10023                                               1,
10024                                               l_explode_comp.requested_date(i),
10025                                               -1 * l_lead_time);
10026                  ELSE
10027                     l_request_date := l_explode_comp.requested_date(i);
10028                  END IF;
10029 
10030                  IF PG_DEBUG in ('Y', 'C') THEN
10031                        msc_sch_wb.atp_debug('l_request_date := ' || l_request_date);
10032                  END IF;
10033 
10034                  IF MSC_ATP_PVT.G_WS_CALL = 'N' THEN --Not a Web Serviec call
10035 					 MSC_ATP_CTO.Get_Mandatory_Components(p_plan_id,
10036 														  p_instance_id,
10037 														  p_organization_id,
10038 														  l_explode_comp.inventory_item_id(i),
10039 														  l_explode_comp.comp_usage(i),
10040 														  l_request_date,
10041 														  l_explode_comp.dest_inventory_item_id(i),
10042 														  l_mand_comp_info_rec);
10043 
10044 					 IF PG_DEBUG in ('Y', 'C') THEN
10045 						   msc_sch_wb.atp_debug('After Get mand comp, add it to list of components');
10046 					 END IF;
10047 				 END IF;
10048 
10049                  FOR l_cto_count in 1..l_mand_comp_info_rec.sr_inventory_item_id.count LOOP
10050                      l_atp_comp_rec := l_null_atp_comp_rec;
10051 
10052                      /* time_phased_atp changes begin
10053                         Support PF ATP for components*/
10054                      --l_atp_comp_rec.inventory_item_id := l_mand_comp_info_rec.sr_inventory_item_id(l_cto_count);
10055                      l_atp_comp_rec.inventory_item_id := MSC_ATP_PF.Get_PF_Atp_Item_Id(
10056                                                                 p_instance_id,
10057                                                                 -1, -- plan_id
10058                                                                 l_mand_comp_info_rec.sr_inventory_item_id(l_cto_count),
10059                                                                 p_organization_id
10060                                                          );
10061                      l_atp_comp_rec.request_item_id := l_mand_comp_info_rec.sr_inventory_item_id(l_cto_count);
10062                      l_atp_comp_rec.atf_date := l_mand_comp_info_rec.atf_date(l_cto_count);
10063                      l_atp_comp_rec.match_item_family_id := null;
10064                      -- time_phased_atp changes end
10065                      --4570421
10066                      l_atp_comp_rec.scaling_type                      := l_mand_comp_info_rec.scaling_type(l_cto_count);
10067                      l_atp_comp_rec.scale_multiple                    := l_mand_comp_info_rec.scale_multiple(l_cto_count);
10068                      l_atp_comp_rec.scale_rounding_variance           := l_mand_comp_info_rec.scale_rounding_variance(l_cto_count);
10069                      l_atp_comp_rec.rounding_direction                := l_mand_comp_info_rec.rounding_direction(l_cto_count);
10070                      l_atp_comp_rec.component_yield_factor            := l_mand_comp_info_rec.component_yield_factor(l_cto_count);
10071                      l_atp_comp_rec.usage_qty                         := l_mand_comp_info_rec.usage_qty(l_cto_count); --4775920
10072                      l_atp_comp_rec.organization_type                 := l_mand_comp_info_rec.organization_type(l_cto_count); --4775920
10073                      IF (MSC_ATP_PVT.G_ORG_INFO_REC.org_type = MSC_ATP_PVT.OPM_ORG AND l_atp_comp_rec.scaling_type IN (4,5)) THEN
10074                         l_atp_comp_rec.comp_usage := integer_scaling (l_mand_comp_info_rec.quantity(l_cto_count),
10075                                                                       l_mand_comp_info_rec.scale_multiple(l_cto_count),
10076 	                                                              l_mand_comp_info_rec.scale_rounding_variance(l_cto_count) ,
10077 	                                                              l_mand_comp_info_rec.rounding_direction(l_cto_count));
10078 	             ELSE
10079                          l_atp_comp_rec.comp_usage := l_mand_comp_info_rec.quantity(l_cto_count);
10080                      END IF; --4570421
10081 
10082                      --l_atp_comp_rec.comp_usage := l_mand_comp_info_rec.quantity(l_cto_count);
10083                      l_atp_comp_rec.requested_date := l_request_date;
10084                      l_atp_comp_rec.lead_time := l_lead_time + l_explode_comp.lead_time(i);
10085                      l_atp_comp_rec.wip_supply_type := 1;
10086                      l_atp_comp_rec.assembly_identifier := l_explode_comp.assembly_identifier(i);
10087                      l_atp_comp_rec.component_identifier  := null;
10088                      --diag_atp
10089                      l_atp_comp_rec.reverse_cumulative_yield := 1;
10090                      --s_cto_rearch
10091                      l_atp_comp_rec.match_item_id := null;
10092                      l_atp_comp_rec.bom_item_type := l_mand_comp_info_rec.bom_item_type(l_cto_count);
10093                      l_atp_comp_rec.parent_line_id := l_explode_comp.assembly_identifier(i);
10094                      l_atp_comp_rec.top_model_line_id := l_explode_comp.top_model_line_id(i);
10095                      IF l_explode_comp.bom_item_type(i) = 1 THEN
10096                         l_atp_comp_rec.ato_parent_model_line_id := l_explode_comp.assembly_identifier(i);
10097                      ELSE
10098                          l_atp_comp_rec.ato_parent_model_line_id := l_explode_comp.ato_parent_model_line_id(i);
10099                      END IF;
10100 
10101                      l_atp_comp_rec.ato_model_line_id  := l_explode_comp.ato_model_line_id(i);
10102                      l_atp_comp_rec.MAND_COMP_FLAG  := 1;
10103                      l_atp_comp_rec.parent_so_quantity  := 0;
10104                      l_atp_comp_rec.fixed_lt  := l_mand_comp_info_rec.fixed_lead_time(l_cto_count);
10105                      l_atp_comp_rec.variable_lt  := l_mand_comp_info_rec.variable_lead_time(l_cto_count);
10106                      l_atp_comp_rec.oss_error_code  := null;
10107                      l_atp_comp_rec.model_flag := 1;
10108                      l_atp_comp_rec.requested_quantity := p_requested_quantity;
10109                      l_atp_comp_rec.atp_flag := l_mand_comp_info_rec.atp_flag(l_cto_count);
10110                      l_atp_comp_rec.atp_components_flag := l_mand_comp_info_rec.atp_components_flag(l_cto_count);
10111                      l_atp_comp_rec.dest_inventory_item_id := l_mand_comp_info_rec.dest_inventory_item_id(l_cto_count);
10112                      l_atp_comp_rec.parent_repl_ord_flag := NVL(p_comp_info_rec.replenish_to_order_flag, 'N');
10113                      l_atp_comp_rec.comp_uom := l_mand_comp_info_rec.uom_code(l_cto_count); --bug3110023
10114                      MSC_ATP_REQ.Add_To_Comp_List(l_explode_comp,
10115                                             l_comp_requirements,
10116                                             l_atp_comp_rec);
10117 
10118 
10119                  END LOOP;
10120              END IF;
10121 
10122              --now call for CTO Bom
10123               IF PG_DEBUG in ('Y', 'C') THEN
10124                  msc_sch_wb.atp_debug('Get CTO Bom');
10125               END IF;
10126 
10127 		     MSC_ATP_CTO.Get_CTO_BOM(MSC_ATP_PVT.G_SESSION_ID,
10128 					     l_cto_bom_rec,
10129 					     l_explode_comp.assembly_identifier(i),
10130 					     l_explode_comp.requested_date(i),
10131 					     l_explode_comp.comp_usage(i),
10132 					     l_explode_comp.parent_so_quantity(i),
10133 					     l_explode_comp.inventory_item_id(i),
10134 					     p_organization_id,
10135 					     p_plan_id,
10136 					     p_instance_id,
10137                                              l_explode_comp.fixed_lt(i),
10138                                              l_explode_comp.variable_lt(i));
10139 		  END IF;
10140 
10141 	       END IF;
10142 
10143 	       IF PG_DEBUG in ('Y', 'C') THEN
10144 		  msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'i = '||i);
10145 	       END IF;
10146 
10147 
10148 	       -- 3027711
10149 	       IF NVL(l_model_flag,2) <> 1 THEN
10150 		  --standard item
10151 		  LOOP
10152 
10153 		    IF l_bill_seq_id is null THEN
10154 		       EXIT;
10155 		    END IF;
10156 
10157                     -- ATP4drp fetch from DRP specific cursor for DRP plans.
10158                     IF NVL(MSC_ATP_PVT.G_PLAN_INFO_REC.plan_type, 1) <> 5 THEN
10159                        -- Non DRP Plan
10160 		       IF PG_DEBUG in ('Y', 'C') THEN
10161 			     msc_sch_wb.atp_debug('Before Fetch of net_rout_comp');
10162 		       END IF;
10163 		       FETCH net_rout_comp INTO
10164 		             l_comp_item_id,
10165 			     l_comp_component_identifier, -- (3004862) circular BOM issue
10166 			     l_comp_usage,
10167 			     l_comp_date,
10168 			     l_comp_lead_time,
10169 			     l_comp_wip_supply_type,
10170 			     l_comp_pre_pro_lead_time,
10171 			     --diag_atp
10172 			     l_reverse_cumulative_yield,
10173 			     -- time_phased_atp
10174 		    	     l_atf_date,
10175 		    	     l_comp_uom, --bug3110023
10176 		    	     --4570421
10177 		    	     l_scaling_type,
10178 		    	     l_scale_multiple,
10179                              l_scale_rounding_variance,
10180                              l_rounding_direction,
10181                              l_component_yield_factor,
10182                              l_usage_qty; --4775920
10183 
10184 		       EXIT WHEN net_rout_comp%NOTFOUND;
10185 		       IF PG_DEBUG in ('Y', 'C') THEN
10186 		    	  msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'after fetching cursor net_rout_comp');
10187 		       END IF;
10188                     ELSE -- DRP plan
10189 		       IF PG_DEBUG in ('Y', 'C') THEN
10190 			     msc_sch_wb.atp_debug('Before Fetch of drp_comp');
10191 		       END IF;
10192 		       FETCH drp_comp INTO
10193 		  	     l_comp_item_id,
10194 			     l_comp_component_identifier, -- (3004862) circular BOM issue
10195 			     l_comp_usage,
10196 			     l_comp_date,
10197 			     l_comp_lead_time,
10198 			     l_comp_wip_supply_type,
10199 			     l_comp_pre_pro_lead_time,
10200 			     --diag_atp
10201 			     l_reverse_cumulative_yield,
10202 			     -- time_phased_atp
10203 		    	     l_atf_date,
10204 		    	     l_comp_uom, --bug3110023
10205 		    	     --4570421
10206 		    	     l_scaling_type,
10207 		    	     l_scale_multiple,
10208                              l_scale_rounding_variance,
10209                              l_rounding_direction,
10210                              l_component_yield_factor,
10211                              l_usage_qty; --4775920
10212 
10213 		       EXIT WHEN drp_comp%NOTFOUND;
10214 		       IF PG_DEBUG in ('Y', 'C') THEN
10215 		    	  msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'after fetching cursor drp_comp');
10216                           msc_sch_wb.atp_debug('----- ATP4drp Specific Debug Messages -----');
10217 		       END IF;
10218                     END IF;
10219                     -- END ATP4drp
10220 
10221 
10222 		    IF PG_DEBUG in ('Y', 'C') THEN
10223 			msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'after fetch');
10224 			msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_comp_item_id = '||l_comp_item_id);
10225 			msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_comp_usage = '||l_comp_usage);
10226 			msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_comp_date = '||l_comp_date);
10227 			msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_comp_lead_time = '||l_comp_lead_time);
10228 			msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_comp_wip_supply_type = '||l_comp_wip_supply_type);
10229 			msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_comp_assembly_identifier = '||l_comp_assembly_identifier);
10230 			msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_comp_component_identifier = '||l_comp_component_identifier);
10231 			msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_comp_pre_pro_lead_time = '||l_comp_pre_pro_lead_time);
10232 			msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_atf_date = '||l_atf_date);
10233 			msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'G_ASSEMBLY_LINE_ID = '||MSC_ATP_PVT.G_ASSEMBLY_LINE_ID);
10234 			msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'G_COMP_LINE_ID = '||MSC_ATP_PVT.G_COMP_LINE_ID);
10235 			msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_comp_uom = '||l_comp_uom); --bug3110023
10236 			--4570421
10237 			msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_scaling_type = '||l_scaling_type);
10238 			msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_scale_multiple = '||l_scale_multiple);
10239 			msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_scale_rounding_variance = '||l_scale_rounding_variance);
10240 			msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_rounding_direction = '||l_rounding_direction);
10241 			msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_component_yield_factor = '||l_component_yield_factor);
10242 			msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_usage_qty = '||l_usage_qty); --4775920
10243 
10244 
10245 
10246 		    END IF;
10247 
10248 		    IF NVL(MSC_ATP_PVT.G_EXPLODE_PHANTOM, 'N') = 'Y' THEN
10249 			 --- agilent fix: If profile option is set to yes then we consider phantom
10250 			 --- item as any other item and do not add it to the list of items to be exploded
10251 			 l_comp_wip_supply_type := 1;
10252 		    END IF;
10253 
10254 
10255 		    l_comp_lead_time := l_comp_lead_time + l_explode_comp.lead_time(i);
10256 
10257 		    l_atp_comp_rec := l_null_atp_comp_rec;
10258 
10259 		    /* time_phased_atp changes begin
10260 		       Support PF ATP for components*/
10261                     -- ATP4drp PF ATP not supported for DRP.
10262                     IF NVL(MSC_ATP_PVT.G_PLAN_INFO_REC.plan_type, 1) = 5 THEN
10263                        -- Turn Off Product Family ATP for components.
10264                        l_atp_comp_rec.inventory_item_id := l_comp_item_id ;
10265                        -- ATP4drp PF ATP not supported for DRP. Print out Debug Here.
10266                        IF NVL(MSC_ATP_PVT.G_PLAN_INFO_REC.plan_type, 1) = 5 AND PG_DEBUG in ('Y', 'C') THEN
10267                           msc_sch_wb.atp_debug('----- ATP4drp Specific Debug Messages -----');
10268                           msc_sch_wb.atp_debug('Get_Comp_Requirements: l_atp_comp_rec.inventory_item_id '
10269                                                                             || l_atp_comp_rec.inventory_item_id );
10270                           msc_sch_wb.atp_debug('Get_Comp_Requirements: l_atp_comp_rec.request_item_id '
10271                                                                             || l_atp_comp_rec.request_item_id );
10272                           msc_sch_wb.atp_debug('----- ATP4drp Specific Debug Messages -----');
10273                        END IF;
10274                     ELSE -- Non DRP Plans
10275 		       l_atp_comp_rec.inventory_item_id := MSC_ATP_PF.Get_PF_Atp_Item_Id(
10276 							     p_instance_id,
10277 							     -1, -- plan_id
10278 							     l_comp_item_id,
10279 							     p_organization_id
10280 							   );
10281                     END IF;
10282                     -- End ATP4drp
10283 
10284 		    l_atp_comp_rec.request_item_id := l_comp_item_id;
10285 		    l_atp_comp_rec.atf_date := l_atf_date;
10286 		    l_atp_comp_rec.match_item_family_id := null;
10287 		    -- time_phased_atp changes end
10288 
10289 		    l_atp_comp_rec.comp_usage := l_comp_usage;
10290 		    l_atp_comp_rec.requested_date := l_comp_date;
10291 		    l_atp_comp_rec.lead_time := l_comp_lead_time;
10292 		    l_atp_comp_rec.wip_supply_type := l_comp_wip_supply_type;
10293 		    l_atp_comp_rec.assembly_identifier := l_comp_assembly_identifier;
10294 		    l_atp_comp_rec.component_identifier  := l_comp_component_identifier;
10295 		    --4570421, add scaling_type and other parameters here !
10296 		    l_atp_comp_rec.scaling_type := l_scaling_type;
10297 		    l_atp_comp_rec.scale_multiple := l_scale_multiple;
10298 		    l_atp_comp_rec.scale_rounding_variance := l_scale_rounding_variance;
10299 		    l_atp_comp_rec.rounding_direction := l_rounding_direction;
10300 		    l_atp_comp_rec.component_yield_factor := l_component_yield_factor; --4570421
10301 		    l_atp_comp_rec.usage_qty := l_usage_qty; --4775920
10302 		    l_atp_comp_rec.organization_type := NVL ( MSC_ATP_PVT.G_ORG_INFO_REC.org_type, MSC_ATP_PVT.DISCRETE_ORG); --4775920
10303 		    --diag_atp
10304 		    l_atp_comp_rec.reverse_cumulative_yield := l_reverse_cumulative_yield;
10305 		    --s_cto_rearch
10306 		    l_atp_comp_rec.match_item_id := null;
10307 		    l_atp_comp_rec.bom_item_type := null;
10308 		    l_atp_comp_rec.parent_line_id := null;
10309 		    l_atp_comp_rec.top_model_line_id := null;
10310 		    l_atp_comp_rec.ato_parent_model_line_id := null;
10311 		    l_atp_comp_rec.ato_model_line_id  := null;
10312 		    l_atp_comp_rec.MAND_COMP_FLAG  := null;
10313 		    l_atp_comp_rec.parent_so_quantity  := null;
10314 		    l_atp_comp_rec.fixed_lt  := null;
10315 		    l_atp_comp_rec.variable_lt  := null;
10316 		    l_atp_comp_rec.oss_error_code  := null;
10317 		    l_atp_comp_rec.model_flag := l_model_flag;
10318 		    l_atp_comp_rec.requested_quantity := p_requested_quantity;
10319                     l_atp_comp_rec.parent_repl_ord_flag := NVL(p_comp_info_rec.replenish_to_order_flag, 'N');
10320                     l_atp_comp_rec.comp_uom := l_comp_uom; --bug3110023
10321 		    MSC_ATP_REQ.Add_To_Comp_List(l_explode_comp,
10322 						 l_comp_requirements,
10323 						 l_atp_comp_rec);
10324 
10325 
10326 		  END LOOP; -- end the loop of fetch
10327 	       ELSE
10328 		  IF PG_DEBUG in ('Y', 'C') THEN
10329               msc_sch_wb.atp_debug('Model Entity ');
10330              msc_sch_wb.atp_debug('l_model_atp_comp_flag := ' || l_model_atp_comp_flag);
10331           END IF;
10332 
10333           IF l_model_atp_comp_flag in ('Y', 'C') THEN
10334              FOR l_cto_count in 1..l_cto_bom_rec.inventory_item_id.count LOOP
10335                  l_atp_comp_rec := l_null_atp_comp_rec;
10336 
10337                  /* time_phased_atp changes begin
10338                     Support PF ATP for components*/
10339                  l_atp_comp_rec.inventory_item_id := MSC_ATP_PF.Get_PF_Atp_Item_Id(
10340                                                          p_instance_id,
10341                                                          -1,
10342                                                          l_cto_bom_rec.inventory_item_id(l_cto_count),
10343                                                          p_organization_id
10344                                                      );
10345                  l_atp_comp_rec.request_item_id := l_cto_bom_rec.inventory_item_id(l_cto_count);
10346                  l_atp_comp_rec.atf_date := l_cto_bom_rec.atf_date(l_cto_count);
10347                  l_atp_comp_rec.match_item_family_id := MSC_ATP_PF.Get_PF_Atp_Item_Id(
10348                                                             p_instance_id,
10349                                                             -1,
10350                                                             l_cto_bom_rec.match_item_id(l_cto_count),
10351                                                             p_organization_id
10352                                                         );
10353                  -- time_phased_atp changes end
10354 
10355                  l_atp_comp_rec.comp_usage := l_cto_bom_rec.comp_usage(l_cto_count);
10356                  l_atp_comp_rec.requested_date := l_cto_bom_rec.requested_date(l_cto_count);
10357                  l_atp_comp_rec.lead_time := l_cto_bom_rec.lead_time(l_cto_count) + l_explode_comp.lead_time(i);
10358                  l_atp_comp_rec.wip_supply_type := l_cto_bom_rec.wip_supply_type(l_cto_count);
10359                  l_atp_comp_rec.assembly_identifier := l_cto_bom_rec.assembly_identifier(l_cto_count);
10360                  l_atp_comp_rec.component_identifier  := null;
10361                  --diag_atp
10362                  l_atp_comp_rec.reverse_cumulative_yield := null;
10363                  --s_cto_rearch
10364                  l_atp_comp_rec.match_item_id := l_cto_bom_rec.match_item_id(l_cto_count);
10365                  --bug 8602072, Phantom Models in ODS ATP treated as Option Classes
10366                  IF (l_cto_bom_rec.bom_item_type(l_cto_count) =1 AND
10367                        l_cto_bom_rec.wip_supply_type(l_cto_count)=6 AND
10368                          MSC_ATP_PVT.G_INV_CTP =5) THEN
10369                     l_atp_comp_rec.bom_item_type := 2;
10370                  ELSE
10371                     l_atp_comp_rec.bom_item_type := l_cto_bom_rec.bom_item_type(l_cto_count);
10372                  END IF;
10373                  l_atp_comp_rec.parent_line_id := l_cto_bom_rec.parent_line_id(l_cto_count);
10374                  l_atp_comp_rec.top_model_line_id := l_cto_bom_rec.TOP_MODEL_LINE_ID(l_cto_count);
10375                  l_atp_comp_rec.ato_parent_model_line_id := l_cto_bom_rec.ATO_PARENT_MODEL_LINE_ID(l_cto_count);
10376                  l_atp_comp_rec.ato_model_line_id  := l_cto_bom_rec.ATO_MODEL_LINE_ID(l_cto_count);
10377                  l_atp_comp_rec.MAND_COMP_FLAG  := 2;
10378                  l_atp_comp_rec.parent_so_quantity  := l_cto_bom_rec.parent_so_quantity(l_cto_count);
10379                  l_atp_comp_rec.fixed_lt  := l_cto_bom_rec.fixed_lt(l_cto_count);
10380                  l_atp_comp_rec.variable_lt  := l_cto_bom_rec.variable_lt(l_cto_count);
10381                  l_atp_comp_rec.oss_error_code  := l_cto_bom_rec.oss_error_code(l_cto_count);
10382                  l_atp_comp_rec.model_flag := l_model_flag;
10383                  l_atp_comp_rec.requested_quantity := p_requested_quantity;
10384                  l_atp_comp_rec.atp_flag := l_cto_bom_rec.atp_flag(l_cto_count);
10385                  l_atp_comp_rec.atp_components_flag := l_cto_bom_rec.atp_components_flag(l_cto_count);
10386                  l_atp_comp_rec.dest_inventory_item_id := l_cto_bom_rec.dest_inventory_item_id(l_cto_count);
10387                  l_atp_comp_rec.parent_repl_ord_flag := NVL(p_comp_info_rec.replenish_to_order_flag, 'N');
10388                  l_atp_comp_rec.comp_uom := l_cto_bom_rec.comp_uom(l_cto_count); --bug3110023
10389                  l_atp_comp_rec.usage_qty := l_cto_bom_rec.usage_qty(l_cto_count); --4775920
10390                  l_atp_comp_rec.organization_type := l_cto_bom_rec.organization_type(l_cto_count); --4775920
10391 
10392                  MSC_ATP_REQ.Add_To_Comp_List(l_explode_comp,
10393                                             l_comp_requirements,
10394                                             l_atp_comp_rec);
10395 
10396              END LOOP;
10397           END IF;
10398        END IF;
10399 
10400 
10401        -- Bug 4042403, 4047183, 4070094 Check that l_bill_seq_id is not NULL
10402        IF ((l_routing_type = 3) OR (l_model_flag <> 1)) AND l_bill_seq_id IS NOT NULL THEN
10403            -- ATP4drp close DRP specific cursor for DRP plans.
10404            IF NVL(MSC_ATP_PVT.G_PLAN_INFO_REC.plan_type, 1) <> 5 THEN
10405               -- Bug 4042403, 4047183, 4070094 Close the cursor only if it has been opened.
10406               IF net_rout_comp%ISOPEN THEN
10407                  CLOSE net_rout_comp;
10408                  IF PG_DEBUG in ('Y', 'C') THEN
10409                     msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'after closing cursor net_rout_comp');
10410                  END IF;
10411               END IF;
10412               -- End Bug 4042403, 4047183, 4070094
10413            ELSE -- DRP plan
10414               -- Bug 4042403, 4047183, 4070094 Close the cursor only if it has been opened.
10415               IF drp_comp%ISOPEN THEN
10416                  CLOSE drp_comp;
10417                  IF PG_DEBUG in ('Y', 'C') THEN
10418                     msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'after closing cursor drp_comp');
10419                     msc_sch_wb.atp_debug('----- ATP4drp Specific Debug Messages -----');
10420                  END IF;
10421               END IF;
10422               -- End Bug 4042403, 4047183, 4070094
10423            END IF;
10424            -- End ATP4drp
10425 
10426        END IF;
10427 
10428        /*--s_cto_rearch:  do not open cusrosr for cto any more
10429        ELSIF l_cto_bom > 0 THEN
10430            CLOSE cto_comp;
10431 
10432            IF PG_DEBUG in ('Y', 'C') THEN
10433               msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'after closing cursor cto_comp');
10434            END IF;
10435        END IF;
10436        e_cto_rearch */
10437        i := l_explode_comp.inventory_item_id.NEXT(i);
10438 
10439        IF PG_DEBUG in ('Y', 'C') THEN
10440           msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'second time, i = '||i);
10441        END IF;
10442     END LOOP;
10443 
10444     -- 3027711 move initializing x_atp_date/x_avail_assembly_qty to beginning
10445 
10446     IF PG_DEBUG in ('Y', 'C') THEN
10447        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'x_atp_date := ' || x_atp_date);
10448        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_comp_requirements.inventory_item_id.count = '||
10449                          l_comp_requirements.inventory_item_id.count);
10450     END IF;
10451     --msc_sch_wb.atp_debug('l_comp_requirements.pre_process_lead_time.count = '||
10452     --                    l_comp_requirements.pre_process_lead_time.count);
10453 
10454     j := l_comp_requirements.inventory_item_id.first;
10455     -- if j is null, that means we don't have any comp requirements
10456     -- need to consider, so we can assume that we have infinite comp
10457     -- to make the assembly. (that's why we initialize x_avail_assembly_qty
10458     -- that way).
10459 
10460     -- otherwise we need to know how many assemblies we can make
10461     -- by loop through each comp and find the availibility.
10462     -- If we can make more than the requested quantity, we return
10463     -- the requested quantity.
10464 
10465     WHILE j IS NOT NULL LOOP
10466 
10467         --- IN backward sched. check if request date - (fix + Var + preProcessing lead time)
10468         --- is greater than sysdate or not
10469         --- if not then we can't meet the requirement
10470         --- We check only when we are in first time.
10471         IF PG_DEBUG in ('Y', 'C') THEN
10472            msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'p_search = '|| p_search);
10473         END IF;
10474 
10475         IF PG_DEBUG in ('Y', 'C') THEN
10476            msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'j := '||j);
10477         END IF;
10478 
10479         ---diag_atp: we look for complete quantity in case of diagnostic ATP
10480         IF PG_DEBUG in ('Y', 'C') THEN
10481            msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_comp_requirements.comp_usage(j) := '||l_comp_requirements.comp_usage(j));
10482            msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'x_avail_assembly_qty := '||x_avail_assembly_qty);
10483         END IF;
10484         IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 THEN
10485             --- for diagnostic atp we always order the full quantity.
10486             IF ( (MSC_ATP_PVT.G_ORG_INFO_REC.org_type = MSC_ATP_PVT.DISCRETE_ORG AND nvl(l_comp_requirements.scaling_type(j),1) = 2) OR
10487                 (MSC_ATP_PVT.G_ORG_INFO_REC.org_type = MSC_ATP_PVT.OPM_ORG AND nvl(l_comp_requirements.scaling_type(j),1) IN (0,2))) THEN
10488                  l_requested_comp_qty := l_comp_requirements.comp_usage(j);
10489             ELSE
10490                  l_requested_comp_qty := l_comp_requirements.comp_usage(j)* p_requested_quantity;
10491             END IF;
10492         ELSE --4570421
10493             IF ( (MSC_ATP_PVT.G_ORG_INFO_REC.org_type = MSC_ATP_PVT.DISCRETE_ORG AND nvl(l_comp_requirements.scaling_type(j),1) = 2) OR
10494                 (MSC_ATP_PVT.G_ORG_INFO_REC.org_type = MSC_ATP_PVT.OPM_ORG AND nvl(l_comp_requirements.scaling_type(j),1) IN (0,2))) THEN
10495                  l_requested_comp_qty := l_comp_requirements.comp_usage(j);
10496             ELSIF (MSC_ATP_PVT.G_ORG_INFO_REC.org_type = MSC_ATP_PVT.OPM_ORG AND nvl(l_comp_requirements.scaling_type(j),1) IN (4,5)) THEN
10497                  l_requested_comp_qty := integer_scaling (l_comp_requirements.comp_usage(j)*x_avail_assembly_qty,
10498                                                    l_comp_requirements.scale_multiple(j),
10499 	                                           l_comp_requirements.scale_rounding_variance(j) ,
10500 	                                           l_comp_requirements.rounding_direction(j));
10501 	    ELSE
10502 	         l_requested_comp_qty := l_comp_requirements.comp_usage(j)*x_avail_assembly_qty;
10503 	    END IF;
10504 
10505         END IF;
10506         --4570421 , print l_requested_comp_qty here !
10507         IF PG_DEBUG in ('Y', 'C') THEN
10508            msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_requested_comp_qty := '||l_requested_comp_qty);
10509         END IF;
10510 
10511         -- now call atp check for this item:
10512         -- 1: populate the atp rec
10513         -- 2: call ATP_Check
10514 
10515         l_atp_rec.error_code := 0;
10516         l_atp_rec.available_quantity := NULL;
10517         l_atp_rec.requested_date_quantity := NULL;
10518 
10519         -- Bug 1562754, we need to store the line id for a lower level model in
10520         -- case of CTO. For example, the BOM is like : Model A -> Model B -> Item C
10521         -- In such a case, if item C is not available enough say while making Model B
10522         -- in backward case, during adjustment of other resources and components, we want
10523         -- G_ASSEMBLY_LINE_ID to be set to line Id of Model B and not Model A.
10524 
10525         MSC_ATP_PVT.G_ASSEMBLY_LINE_ID := NVL(l_comp_requirements.assembly_identifier(j), MSC_ATP_PVT.G_ASSEMBLY_LINE_ID);
10526         MSC_ATP_PVT.G_COMP_LINE_ID := NVL(l_comp_requirements.component_identifier(j), MSC_ATP_PVT.G_COMP_LINE_ID);
10527         IF PG_DEBUG in ('Y', 'C') THEN
10528            msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'G_COMP_LINE_ID = '||MSC_ATP_PVT.G_COMP_LINE_ID);
10529         END IF;
10530 
10531         -- no need to do uom conversion
10532         l_atp_rec.instance_id := p_instance_id;
10533         l_atp_rec.identifier := MSC_ATP_PVT.G_ORDER_LINE_ID;
10534         l_atp_rec.component_identifier := l_comp_requirements.component_identifier(j);
10535 
10536         -- 2462661 : krajan
10537         --l_atp_rec.src_atp_flag := l_comp_requirements.src_atp_flag(j);
10538 
10539         select identifier3
10540         into   l_atp_rec.demand_source_line
10541         from   mrp_atp_details_temp
10542         where  pegging_id = p_parent_pegging_id
10543         and    record_type = 3
10544         and    session_id = MSC_ATP_PVT.G_SESSION_ID;
10545 
10546 
10547         -- l_atp_rec.demand_source_header_id:= l_atp_table.Demand_Source_Header_Id(i);
10548         -- l_atp_rec.demand_source_delivery:= l_atp_table.Demand_Source_Delivery(i);
10549         l_atp_rec.inventory_item_id := l_comp_requirements.inventory_item_id(j);
10550 
10551         /* time_phased_atp
10552            Support PF ATP for components*/
10553         l_atp_rec.request_item_id := l_comp_requirements.request_item_id(j);
10554 
10555         l_atp_rec.organization_id := p_organization_id;
10556         l_atp_rec.quantity_ordered := l_requested_comp_qty;
10557         -- l_atp_rec.quantity_uom := l_quantity_uom;
10558         l_atp_rec.requested_ship_date := l_comp_requirements.requested_date(j);
10559 
10560         -- krajan: 2408902: duplicate statement
10561         --l_atp_rec.demand_class := p_demand_class;
10562 
10563 
10564         l_atp_rec.insert_flag := p_insert_flag;
10565 
10566         IF l_model_flag = 1 THEN
10567            l_atp_rec.refresh_number := p_refresh_number;
10568         ELSE
10569            l_atp_rec.refresh_number := null;
10570         END IF;
10571 
10572 	l_atp_rec.ship_date := null;
10573 
10574         -- krajan: 2408902: populate demand class from global variable
10575         -- Bug 2424357
10576         l_atp_rec.demand_class := NVL(MSC_ATP_PVT.G_ATP_DEMAND_CLASS, p_demand_class);
10577 
10578         IF PG_DEBUG in ('Y', 'C') THEN
10579            msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'Demand Class being passed is : '|| l_atp_rec.demand_class);
10580         END IF;
10581         -- end 2408902
10582 
10583         --s_cto_enhc
10584         l_plan_found_for_match := 0;
10585         l_model_error_code := 0;
10586         l_atp_rec.base_model_id   := null;
10587 
10588 
10589         IF l_model_flag = 1 and l_comp_requirements.atp_flag(j) is null
10590                             --exclude mand_comp flag as they will always be collected
10591                             and NVL(l_comp_requirements.MAND_COMP_FLAG(j), 2) = 2  THEN
10592 
10593            --item doesn't exists in the given organization
10594            l_model_error_code := MSC_ATP_PVT.ATP_ITEM_NOT_COLLECTED;
10595 
10596            IF PG_DEBUG in ('Y', 'C') THEN
10597                  msc_sch_wb.atp_debug('Item not collected := ' || l_comp_requirements.inventory_item_id(j));
10598            END IF;
10599 
10600         ELSIF l_model_flag = 1 and l_comp_requirements.atp_flag(j) = 'N'
10601                                and l_comp_requirements.atp_components_flag(j) = 'N' THEN
10602            --model entity is non-atpable. Do not find plan, default to parent's plan
10603            l_plan_id := p_plan_id;
10604         ELSIF MSC_ATP_PVT.G_INV_CTP = 5 THEN
10605            l_plan_id := -1;
10606            l_assign_set_id := p_assign_set_id;
10607         ELSE
10608 
10609            IF l_comp_requirements.match_item_id(j) is not null then
10610               --check if match exists or not.
10611 
10612               /* time_phased_atp changes begin
10613                  Support PF ATP for components*/
10614               --l_atp_rec.inventory_item_id := l_comp_requirements.match_item_id(j);
10615               l_atp_rec.inventory_item_id := l_comp_requirements.match_item_family_id(j);
10616               l_atp_rec.request_item_id := l_comp_requirements.match_item_id(j);
10617               -- time_phased_atp changes end
10618               /*
10619               MSC_ATP_PROC.get_global_plan_info(p_instance_id,
10620                                              --l_atp_rec.inventory_item_id,
10621                                              l_atp_rec.request_item_id, -- time_phased_atp
10622                                              l_atp_rec.organization_id,
10623                                              l_atp_rec.demand_class);*/
10624               /* time_phased_atp changes begin
10625                  Call new procedure Get_PF_Plan_Info*/
10626               IF PG_DEBUG in ('Y', 'C') THEN
10627                  msc_sch_wb.atp_debug('Match found := ' || l_atp_rec.inventory_item_id);
10628                  msc_sch_wb.atp_debug('Find plan for match');
10629               END IF;
10630               MSC_ATP_PF.Get_PF_Plan_Info(
10631                           p_instance_id,
10632                           l_atp_rec.request_item_id,
10633                           l_atp_rec.inventory_item_id,
10634                           l_atp_rec.organization_id,
10635                           l_atp_rec.demand_class,
10636                           l_atp_rec.atf_date,
10637                           l_atp_rec.error_code,
10638                           l_return_status,
10639                           p_plan_id --bug3510475 pass component's plan id as parent
10640               );
10641 
10642               IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
10643                    IF PG_DEBUG in ('Y', 'C') THEN
10644                       msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'Error encountered in Get_PF_Plan_Info');
10645                    END IF;
10646               END IF;
10647               /* time_phased_atp changes end*/
10648 
10649               IF MSC_ATP_PVT.G_PLAN_INFO_REC.plan_id is not null and
10650                   NVL(MSC_ATP_PVT.G_PLAN_INFO_REC.plan_id, -1) <> -1 then
10651                   IF PG_DEBUG in ('Y', 'C') THEN
10652                       msc_sch_wb.atp_debug('Plan found for match := '  || MSC_ATP_PVT.G_PLAN_INFO_REC.plan_id );
10653                    END IF;
10654                   ---plan found for match
10655                   l_plan_found_for_match := 1;
10656                   --l_atp_rec.request_item_id := l_comp_requirements.match_item_id(j);
10657                   l_atp_rec.base_model_id   := l_comp_requirements.inventory_item_id(j); -- check with Vivek
10658 
10659               ELSE
10660                   ---plan is not found for match. Do ATP on model level
10661                   --l_atp_rec.inventory_item_id := l_comp_requirements.inventory_item_id(j);
10662                   IF PG_DEBUG in ('Y', 'C') THEN
10663                       msc_sch_wb.atp_debug('Plan not found for match := '  || MSC_ATP_PVT.G_PLAN_INFO_REC.plan_id );
10664                   END IF;
10665                   /* time_phased_atp
10666                      Support PF ATP for components*/
10667                   l_atp_rec.inventory_item_id := l_comp_requirements.inventory_item_id(j);
10668                   l_atp_rec.request_item_id := l_comp_requirements.request_item_id(j);
10669               END IF;
10670            END IF;
10671 
10672            IF l_plan_found_for_match = 0 THEN
10673               -- New procedure for obtaining plan data : Supplier Capacity Lead Time (SCLT) proj.
10674               /*
10675               MSC_ATP_PROC.get_global_plan_info(p_instance_id,
10676                                              --l_atp_rec.inventory_item_id,
10677                                              l_atp_rec.request_item_id, -- time_phased_atp
10678                                              l_atp_rec.organization_id,
10679                                              l_atp_rec.demand_class);*/
10680 
10681               /* time_phased_atp changes begin
10682                  Call new procedure Get_PF_Plan_Info*/
10683 
10684               MSC_ATP_PF.Get_PF_Plan_Info(
10685                           p_instance_id,
10686                           l_atp_rec.request_item_id,
10687                           l_atp_rec.inventory_item_id,
10688                           l_atp_rec.organization_id,
10689                           l_atp_rec.demand_class,
10690                           l_atp_rec.atf_date,
10691                           l_atp_rec.error_code,
10692                           l_return_status,
10693                           p_plan_id --bug3510475 pass component's plan id as parent
10694               );
10695 
10696               IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
10697                    IF PG_DEBUG in ('Y', 'C') THEN
10698                       msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'Error encountered in Get_PF_Plan_Info');
10699                    END IF;
10700               END IF;
10701               /* time_phased_atp changes end*/
10702            END IF;
10703 
10704            l_plan_info_rec := MSC_ATP_PVT.G_PLAN_INFO_REC;
10705            -- End New procedure for obtaining plan data : Supplier Capacity Lead Time proj
10706 
10707            l_plan_id       := l_plan_info_rec.plan_id;
10708            l_assign_set_id := l_plan_info_rec.assignment_set_id;
10709 
10710            --diag_atp
10711            l_atp_rec.plan_name := l_plan_info_rec.plan_name;
10712            l_atp_rec.reverse_cumulative_yield := l_comp_requirements.reverse_cumulative_yield(j);
10713            l_atp_rec.quantity_uom := l_comp_requirements.comp_uom(j); --bug3110023
10714 
10715            -- changes for bug 2392456 ends
10716 
10717            msc_sch_wb.atp_debug ('Plan ID in Get_Comp_Reqs : ' || l_plan_id);
10718            IF (l_plan_id is NULL) or (l_plan_id IN (-100, -200)) THEN
10719                --s_cto_rearch
10720                IF l_model_flag = 1 THEN
10721                   l_model_error_code := MSC_ATP_PVT.PLAN_NOT_FOUND;
10722                ELSE
10723                   --standard item
10724                   -- this should not happen but just in case
10725                   l_plan_id := p_plan_id;
10726                   l_assign_set_id := p_assign_set_id;
10727                END IF;
10728            END IF;
10729 
10730            -- 24x7
10731            IF (l_plan_id = -300) then
10732               l_atp_rec.error_code := MSC_ATP_PVT.TRY_ATP_LATER;
10733               IF PG_DEBUG in ('Y', 'C') THEN
10734                  msc_sch_wb.atp_debug('Get_Comp_Req: ATP Downtime Detected');
10735                  msc_sch_wb.atp_debug('Get_Comp_Requirements: ATP Downtime');
10736               END IF;
10737               MSC_ATP_PVT.G_DOWNTIME_HIT := 'Y';
10738               RAISE MSC_ATP_PVT.EXC_NO_PLAN_FOUND;
10739            End IF;
10740         END IF;
10741         --subst
10742         l_atp_rec.original_item_flag := 2;
10743         l_atp_rec.top_tier_org_flag := 2;
10744         l_atp_rec.substitution_window := 0;
10745 
10746         /* ship_rec_cal changes begin */
10747         l_atp_rec.receiving_cal_code := p_manufacturing_cal_code;
10748         l_atp_rec.intransit_cal_code := p_manufacturing_cal_code;
10749         l_atp_rec.shipping_cal_code := p_manufacturing_cal_code;
10750         l_atp_rec.manufacturing_cal_code := p_manufacturing_cal_code;
10751         /* ship_rec_cal changes end */
10752 
10753         ---s_cto_rearch
10754         IF MSC_ATP_PVT.G_INV_CTP = 5 and
10755            p_comp_info_rec.bom_item_type = 4 and p_comp_info_rec.replenish_to_order_flag = 'Y' THEN
10756            --add ato item's components in ODS case share the same line id as ato item itself
10757            l_atp_rec.demand_source_line := p_comp_info_rec.line_id;
10758         ELSE
10759 
10760            l_atp_rec.demand_source_line := l_comp_requirements.assembly_identifier(j);
10761         END IF;
10762         l_atp_rec.order_number       := p_order_number;
10763         l_atp_rec.Top_Model_line_id := l_comp_requirements.Top_Model_line_id(j);
10764 
10765         l_atp_rec.ATO_Parent_Model_Line_Id := l_comp_requirements.ATO_Parent_Model_Line_Id(j);
10766 
10767         IF p_comp_info_rec.bom_item_type = 4 and p_comp_info_rec.replenish_to_order_flag = 'Y' THEN
10768            -- ato model line id id used to remove stealing records in demand priorit cases
10769            -- there for components for ATO items, we pass this line id
10770            l_atp_rec.ATO_Model_Line_Id := p_comp_info_rec.ATO_Model_Line_Id;
10771         ELSE
10772            l_atp_rec.ATO_Model_Line_Id := l_comp_requirements.ATO_Model_Line_Id(j);
10773         END IF;
10774         l_atp_rec.Parent_line_id :=  l_comp_requirements.Parent_line_id(j);
10775         l_atp_rec.wip_supply_type := l_comp_requirements.wip_supply_type(j);
10776         l_atp_rec.parent_atp_flag := p_comp_info_rec.atp_flag;
10777         l_atp_rec.parent_atp_comp_flag := p_comp_info_rec.atp_comp_flag;
10778         l_atp_rec.parent_repl_order_flag := p_comp_info_rec.replenish_to_order_flag;
10779         l_atp_rec.parent_bom_item_type := p_comp_info_rec.bom_item_type;
10780         l_atp_rec.mand_comp_flag := l_comp_requirements.mand_comp_flag(j);
10781         l_atp_rec.parent_so_quantity := l_comp_requirements.parent_so_quantity(j);
10782         l_atp_rec.wip_supply_type := l_comp_requirements.wip_supply_type(j);
10783         --- This flag is populated only for model where atp_flag = 'Y'
10784         -- This flag will be used in get_item_attribute to turn off ATP comp flag
10785         --- so that model is not reexploded.
10786         l_atp_rec.parent_item_id := l_comp_requirements.parent_item_id(j);
10787 
10788         l_atp_rec.bill_seq_id := l_bill_seq_id;      --4741012 for passing to ATP_Check.
10789 
10790         --4570421
10791         l_atp_rec.scaling_type                      := l_comp_requirements.scaling_type(j);
10792         l_atp_rec.scale_multiple                    := l_comp_requirements.scale_multiple(j);
10793         l_atp_rec.scale_rounding_variance           := l_comp_requirements.scale_rounding_variance(j);
10794         l_atp_rec.rounding_direction                := l_comp_requirements.rounding_direction(j);
10795         l_atp_rec.component_yield_factor            := l_comp_requirements.component_yield_factor(j); --4570421
10796         l_atp_rec.usage_qty                         := l_comp_requirements.usage_qty(j); --4775920
10797         l_atp_rec.organization_type                 := l_comp_requirements.organization_type(j); --4775920
10798 
10799         ---e_cto_rearch
10800         IF l_model_flag = 1 and l_model_error_code > 0 THEN
10801 
10802            IF PG_DEBUG in ('Y', 'C') THEN
10803               msc_sch_wb.atp_debug('Model entity and error has occured');
10804               msc_sch_wb.atp_debug('Error code := ' || l_model_error_code);
10805            END IF;
10806 
10807 /* bug 7508506
10808  *        l_atp_rec.combined_requested_date_qty := 0;
10809  *        l_atp_rec.requested_date_quantity := 0;
10810  *        l_atp_rec.ship_date := null;
10811  *        */
10812            l_atp_rec.combined_requested_date_qty := l_requested_comp_qty;
10813            l_atp_rec.requested_date_quantity := l_requested_comp_qty;
10814            l_atp_rec.ship_date := l_comp_requirements.requested_date(j);
10815 
10816 
10817            --add pegging for diagnostic case
10818            IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 THEN
10819               l_pegging_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
10820               l_pegging_rec.order_line_id:= MSC_ATP_PVT.G_ORDER_LINE_ID;
10821               l_pegging_rec.parent_pegging_id:= p_parent_pegging_id;
10822               l_pegging_rec.atp_level:= p_level;
10823               l_pegging_rec.organization_id:= p_organization_id;
10824               l_pegging_rec.organization_code := l_org_code;
10825               l_pegging_rec.identifier1:= p_instance_id;
10826               l_pegging_rec.identifier2 := null;
10827               l_pegging_rec.identifier3 := null;
10828               l_pegging_rec.inventory_item_id:= l_comp_requirements.inventory_item_id(j);
10829               l_pegging_rec.inventory_item_name := null; -- item is not collected, how do we show it??
10830               l_pegging_rec.resource_id := NULL;
10831               l_pegging_rec.resource_code := NULL;
10832               l_pegging_rec.department_id := NULL;
10833               l_pegging_rec.department_code := NULL;
10834               l_pegging_rec.supplier_id := NULL;
10835               l_pegging_rec.supplier_name := NULL;
10836               l_pegging_rec.supplier_site_id := NULL;
10837               l_pegging_rec.supplier_site_name := NULL;
10838               l_pegging_rec.scenario_id:= p_scenario_id;
10839               l_pegging_rec.supply_demand_source_type:= 6;
10840               l_pegging_rec.supply_demand_quantity := l_requested_comp_qty;
10841               l_pegging_rec.supply_demand_type:= 1;
10842               l_pegging_rec.supply_demand_date:= l_comp_requirements.requested_date(j);
10843               --4570421
10844               l_pegging_rec.scaling_type                      := l_comp_requirements.scaling_type(j);
10845               l_pegging_rec.scale_multiple                    := l_comp_requirements.scale_multiple(j);
10846               l_pegging_rec.scale_rounding_variance           := l_comp_requirements.scale_rounding_variance(j);
10847               l_pegging_rec.rounding_direction                := l_comp_requirements.rounding_direction(j);
10848               l_pegging_rec.component_yield_factor            := l_comp_requirements.component_yield_factor(j); --4570421
10849               l_pegging_rec.usage                             := l_comp_requirements.usage_qty(j); --4775920
10850               l_pegging_rec.organization_type                 := l_comp_requirements.organization_type(j); --4775920
10851 
10852               --e_cto_rearch
10853 
10854               l_pegging_rec.constraint_flag := 'N';
10855 	      l_pegging_rec.component_identifier := null;
10856 
10857 
10858               --diag_atp
10859               l_pegging_rec.pegging_type := MSC_ATP_PVT.ORG_DEMAND; --demand pegging
10860 
10861               --s_cto_rearch
10862               l_pegging_rec.dest_inv_item_id := null;
10863               l_pegging_rec.error_code := l_model_error_code;
10864               --e_cto_rearch
10865 
10866               l_pegging_rec.summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;     -- for summary enhancement
10867               MSC_ATP_DB_UTILS.Add_Pegging(l_pegging_rec, l_pegging_id);
10868            END IF;
10869         ELSE
10870 
10871            -- ATP4drp Assign parent_item_id for DRP Kitting in DRP plans.
10872            -- Assignment should not be detrimental in other plans.
10873 
10874            IF ( l_plan_id <> -1) THEN --4929084
10875               l_atp_rec.parent_item_id := p_inventory_item_id;
10876            END IF;
10877 
10878            IF PG_DEBUG in ('Y', 'C') THEN
10879               msc_sch_wb.atp_debug('Get_Comp_Requirements: l_atp_rec.parent_item_id ' || l_atp_rec.parent_item_id );
10880            END IF;
10881            -- End ATP4drp
10882 
10883            l_alloc_atp := MSC_ATP_PVT.G_ALLOCATED_ATP; --ALLOC ATP CHANGES, 12973673
10884            MSC_ATP_PVT.ATP_Check(l_atp_rec,
10885                      l_plan_id,
10886                      p_level ,
10887                      p_scenario_id,
10888                      p_search,
10889                      p_refresh_number,
10890                      p_parent_pegging_id,
10891                      l_assign_set_id,
10892                      l_atp_period,
10893                      l_atp_supply_demand,
10894                      x_return_status);
10895 			IF MSC_ATP_PVT.G_HYBRID_ALLOC_ATP = 'Y' THEN
10896 				MSC_ATP_PVT.G_ALLOCATED_ATP := l_alloc_atp;
10897 			END IF;
10898 
10899         END IF;
10900         --- bug 2178544
10901         -- Since PTF-Date might be chnaged by some different plan for components we reset the global varibale
10902         MSC_ATP_PVT.G_PTF_DATE := l_ptf_date;
10903 
10904         IF x_return_status = MSC_ATP_PVT.CTO_OSS_ERROR THEN
10905 
10906            RAISE MSC_ATP_PVT.INVALID_OSS_SOURCE;
10907 
10908            IF PG_DEBUG in ('Y', 'C') THEN
10909               msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || ' Error in OSS');
10910            END IF;
10911 
10912         -- Bug 1531429, in case return status is not success, raise an exception.
10913         -- krajan: 2400614
10914         -- krajan: If it is 'G', then it is a sourcing mismatch error.
10915         -- krajan: If it is MSC_ATP_PVT.G_ATO_SRC_MISMATCH, then it is a sourcing mismatch error.
10916 
10917         -- krajan: Basically for handling recursive ATP_CHECK <-> Get_Comp_Req calls
10918         -- krajan : 2752705 and dsting 2764213 : Other errors that need to go through to the
10919         --          top level model are also handled the same way as the mismatch case.
10920         ELSIF x_return_status = MSC_ATP_PVT.G_ATO_SRC_MISMATCH THEN
10921            IF PG_DEBUG in ('Y', 'C') THEN
10922               msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'Get_Comp_Req: Error in ATP_CHECK 0.1');
10923               msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'Error in lower level component check');
10924            END IF;
10925            RAISE MSC_ATP_PVT.G_ATO_SOURCING_MISMATCH;
10926 
10927          --bug 3308206: IF ATP rule is not defined on the item then error out with message
10928         ELSIF MSC_ATP_PVT.G_INV_CTP = 5 and x_return_status <>  FND_API.G_RET_STS_SUCCESS
10929                and l_atp_rec.error_code = MSC_ATP_PVT.ATP_BAD_RULE THEN
10930            IF PG_DEBUG in ('Y', 'C') THEN
10931               msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'No ATP rule on Component');
10932            END IF;
10933            RAISE MSC_ATP_PVT.EXC_NO_ATP_RULE;
10934         END IF;
10935 
10936         -- dsting 2764213
10937         IF x_return_status = MSC_ATP_PVT.G_NO_PLAN_FOUND THEN
10938            IF PG_DEBUG in ('Y', 'C') THEN
10939               msc_sch_wb.atp_debug('Get_Comp_Req: Error in ATP_CHECK 0.2');
10940               msc_sch_wb.atp_debug('Get_Comp_Requirements: Error in lower level component check');
10941            END IF;
10942            RAISE MSC_ATP_PVT.EXC_NO_PLAN_FOUND;
10943         END IF;
10944 
10945         -- krajan 2752705
10946         IF x_return_status = MSC_ATP_PVT.G_ATO_UNCOLL_ITEM THEN
10947            IF PG_DEBUG in ('Y', 'C') THEN
10948               msc_sch_wb.atp_debug('Get_Comp_Req: Error in ATP_CHECK 0.3');
10949               msc_sch_wb.atp_debug('Get_Comp_Requirements: Error in lower level component check');
10950            END IF;
10951            RAISE MSC_ATP_PVT.G_EXC_UNCOLLECTED_ITEM;
10952         END IF;
10953 
10954         IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
10955            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
10956         END IF;
10957 
10958         MSC_ATP_PROC.Details_Output(l_atp_period,
10959                        l_atp_supply_demand,
10960                        x_atp_period,
10961                        x_atp_supply_demand,
10962                        x_return_status);
10963 
10964 
10965 
10966         -- now adjust the x_avail_assembly_qty
10967         IF PG_DEBUG in ('Y', 'C') THEN
10968            msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_atp_rec.combined_requested_date_qty := ' || l_atp_rec.combined_requested_date_qty);
10969            msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_atp_rec.requested_date_quantity := ' ||l_atp_rec.requested_date_quantity);
10970         END IF;
10971 
10972         IF p_search = 1 THEN
10973 
10974           IF PG_DEBUG in ('Y', 'C') THEN
10975              msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'p_search = 1');
10976           END IF;
10977 
10978           -- cchen 1238941
10979                     --4570421
10980           IF (NVL(l_atp_rec.combined_requested_date_qty,
10981               l_atp_rec.requested_date_quantity) >= l_requested_comp_qty) THEN
10982             NULL;
10983           ELSIF (NVL(l_atp_rec.combined_requested_date_qty,
10984                     l_atp_rec.requested_date_quantity) >0) THEN
10985                     IF ( (MSC_ATP_PVT.G_ORG_INFO_REC.org_type = MSC_ATP_PVT.DISCRETE_ORG AND nvl(l_comp_requirements.scaling_type(j),1) = 2) OR
10986                          (MSC_ATP_PVT.G_ORG_INFO_REC.org_type = MSC_ATP_PVT.OPM_ORG AND nvl(l_comp_requirements.scaling_type(j),1) IN (0,2))) THEN
10987                         x_avail_assembly_qty := 0;
10988                         IF PG_DEBUG in ('Y', 'C') THEN
10989                              msc_sch_wb.atp_debug('Fixed or Lot Based Case: Lot qty not available: avail_assembly_qty: ' || x_avail_assembly_qty);
10990                         END IF;
10991                         IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 2 THEN --5403495
10992                            EXIT;
10993                         END IF;
10994                         --EXIT;
10995                     ELSIF ( (MSC_ATP_PVT.G_ORG_INFO_REC.org_type = MSC_ATP_PVT.OPM_ORG) AND (nvl(l_comp_requirements.scaling_type(j),1) IN (4,5)) ) THEN
10996                         IF PG_DEBUG in ('Y', 'C') THEN
10997                              msc_sch_wb.atp_debug('Before inverse scaling : avail_assembly_qty: ' || x_avail_assembly_qty);
10998                         END IF;
10999                         x_avail_assembly_qty := LEAST(x_avail_assembly_qty,
11000                                                       round(
11001                                                             FLOOR( NVL(l_atp_rec.combined_requested_date_qty,
11002                                                                        l_atp_rec.requested_date_quantity)/l_comp_requirements.scale_multiple(j))* l_comp_requirements.scale_multiple(j)
11003                                                             /l_comp_requirements.comp_usage(j),6));
11004                         IF PG_DEBUG in ('Y', 'C') THEN
11005                              msc_sch_wb.atp_debug('Integer Scaling case : avail_assembly_qty: ' || x_avail_assembly_qty);
11006                         END IF;
11007                     ELSE
11008                         x_avail_assembly_qty := LEAST(x_avail_assembly_qty,
11009                                                        trunc(NVL(l_atp_rec.combined_requested_date_qty,
11010                        																 			 l_atp_rec.requested_date_quantity)/
11011                        																 			 l_comp_requirements.comp_usage(j),6));	-- 5598066
11012                         IF PG_DEBUG in ('Y', 'C') THEN
11013                              msc_sch_wb.atp_debug('Item or Proportional case : avail_assembly_qty: ' || x_avail_assembly_qty);
11014                         END IF;
11015                     END IF;
11016           ELSE
11017               x_avail_assembly_qty := 0;
11018                       --diag_atp
11019               IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 2 THEN
11020                 EXIT;
11021               END IF;
11022           END IF;
11023 
11024           --4570421, adding for testing, remove it
11025           --x_avail_assembly_qty := 0;
11026 
11027           -- 2869830
11028           IF PG_DEBUG in ('Y', 'C') THEN
11029               msc_sch_wb.atp_debug('avail_assembly_qty: ' || x_avail_assembly_qty);
11030           END IF;
11031 
11032           IF l_rounding_flag = 1 THEN
11033               x_avail_assembly_qty := FLOOR(x_avail_assembly_qty);
11034               msc_sch_wb.atp_debug('rounded avail qty: ' ||
11035                           x_avail_assembly_qty);
11036           END IF;
11037           IF PG_DEBUG in ('Y', 'C') THEN
11038              msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'x_avail_assembly_qty' || x_avail_assembly_qty);
11039           END IF;
11040         ELSE
11041           IF PG_DEBUG in ('Y', 'C') THEN
11042              msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'p_search = 2');
11043           END IF;
11044 
11045           IF l_atp_rec.ship_date IS NOT NULL THEN
11046             x_atp_date :=  GREATEST(MSC_CALENDAR.DATE_OFFSET
11047                                  (p_organization_id,
11048                                   p_instance_id,
11049                                   1,
11050                                   l_atp_rec.ship_date,
11051                                   NVL(l_comp_requirements.lead_time(j), 0)),
11052                                   x_atp_date);
11053 
11054             ---bug 3059305: If x_atp_date is greater than or equal to ship date from last source
11055             -- then date from last source will be used as the availability date.
11056             ---Therefore, no need to continue further
11057             IF x_atp_date >= p_comp_info_rec.ship_date_this_level THEN
11058                IF PG_DEBUG in ('Y', 'C') THEN
11059                   msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'x_atp_date := ' || x_atp_date);
11060                   msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'x_atp_date > ship_date_this_level. Therefore exit');
11061                END IF;
11062                x_atp_date := null;
11063                EXIT;
11064             END IF;
11065           ELSE
11066             x_atp_date := NULL;
11067             EXIT;
11068           END IF;
11069         END IF;
11070 
11071         j := l_comp_requirements.inventory_item_id.NEXT(j);
11072 
11073     END LOOP;
11074 
11075     IF PG_DEBUG in ('Y', 'C') THEN
11076        msc_sch_wb.atp_debug('***** End Get_Comp_Requirements *****');
11077     END IF;
11078 
11079 
11080 Exception
11081 
11082     WHEN MSC_ATP_PVT.INVALID_OSS_SOURCE THEN
11083 
11084         IF PG_DEBUG in ('Y', 'C') THEN
11085            msc_sch_wb.atp_debug('Get_Comp_Reqs: ' || 'Invalid OSS setup detected');
11086         END IF;
11087         x_avail_assembly_qty  := 0;
11088         x_atp_date            := null;
11089         x_return_status := MSC_ATP_PVT.CTO_OSS_Error;
11090 
11091     -- 2400614 : krajan
11092     WHEN MSC_ATP_PVT.G_ATO_SOURCING_MISMATCH THEN
11093         IF PG_DEBUG in ('Y', 'C') THEN
11094            msc_sch_wb.atp_debug ('Get_Comp_Reqs: IN Exception Block for G_ATO_SOURCE');
11095         END IF;
11096         x_return_status := NVL(x_return_status, FND_API.G_RET_STS_ERROR);
11097         RAISE MSC_ATP_PVT.G_ATO_SOURCING_MISMATCH;
11098 
11099     -- dsting 2764213
11100     WHEN MSC_ATP_PVT.EXC_NO_PLAN_FOUND THEN
11101         IF PG_DEBUG in ('Y', 'C') THEN
11102            msc_sch_wb.atp_debug ('Get_Comp_Reqs: IN Exception Block for EXC_NO_PLAN_FOUND');
11103         END IF;
11104         x_return_status := NVL(x_return_status, FND_API.G_RET_STS_ERROR);
11105         RAISE MSC_ATP_PVT.EXC_NO_PLAN_FOUND;
11106 
11107     -- krajan 2752705
11108     WHEN MSC_ATP_PVT.G_EXC_UNCOLLECTED_ITEM THEN
11109         IF PG_DEBUG in ('Y', 'C') THEN
11110            msc_sch_wb.atp_debug ('Get_Comp_Reqs: IN Exception Block for G_EXC_UNCOLLECTED_ITEM');
11111         END IF;
11112         x_return_status := NVL(x_return_status, FND_API.G_RET_STS_ERROR);
11113         RAISE MSC_ATP_PVT.G_EXC_UNCOLLECTED_ITEM;
11114 
11115     ---bug 3308206: Add exception so that it could be propogated to ATP_check
11116     WHEN  MSC_ATP_PVT.EXC_NO_ATP_RULE THEN
11117         IF PG_DEBUG in ('Y', 'C') THEN
11118            msc_sch_wb.atp_debug('Get_Comp_Reqs: IN Exception Block for EXC_NO_ATP_RULE');
11119         END IF;
11120         RAISE MSC_ATP_PVT.EXC_NO_ATP_RULE;
11121 
11122     WHEN MSC_ATP_PVT.NO_MATCHING_DATE_IN_CAL THEN --bug3583705
11123         IF PG_DEBUG in ('Y', 'C') THEN
11124            msc_sch_wb.atp_debug ('Get_Comp_Reqs: IN Exception Block for NO_MATCHING_DATE_IN_CAL');
11125         END IF;
11126         RAISE MSC_ATP_PVT.NO_MATCHING_DATE_IN_CAL;
11127 
11128     WHEN OTHERS THEN
11129         IF PG_DEBUG in ('Y', 'C') THEN
11130            msc_sch_wb.atp_debug ('Get_Comp_Reqs: IN Exception Block in others');
11131            msc_sch_wb.atp_debug ('error := ' || SQLERRM);
11132         END IF;
11133         x_return_status := NVL(x_return_status, FND_API.G_RET_STS_ERROR);
11134 	RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
11135 
11136 END Get_Comp_Requirements;
11137 
11138 /* Spec changes as part of ship_rec_cal changes
11139    Various input output parameters grouped in the record Atp_Info_Rec*/
11140 PROCEDURE Get_Supplier_Atp_Info (
11141   p_sup_atp_info_rec                    IN OUT  NOCOPY  MSC_ATP_REQ.Atp_Info_Rec,
11142   x_atp_period                          OUT     NOCOPY  MRP_ATP_PUB.ATP_Period_Typ,
11143   x_atp_supply_demand                   OUT     NOCOPY  MRP_ATP_PUB.ATP_Supply_Demand_Typ,
11144   x_return_status                       OUT     NOCOPY  VARCHAR2
11145 )
11146 IS
11147 
11148 i 			PLS_INTEGER := 1;
11149 m                       PLS_INTEGER := 1;
11150 k                       PLS_INTEGER := 1;
11151 j                       PLS_INTEGER := 1; --4055719
11152 l_requested_date	DATE;
11153 l_atp_requested_date    DATE;
11154 l_pre_process_date      DATE;
11155 l_sysdate               DATE;
11156 
11157 l_atp_period_tab 	MRP_ATP_PUB.date_arr:=MRP_ATP_PUB.date_arr();
11158 l_atp_qty_tab 		MRP_ATP_PUB.number_arr:=MRP_ATP_PUB.number_arr();
11159 l_next_period           DATE;
11160 l_null_num              number := null;
11161 l_null_char             varchar2(3) := null;
11162 l_plan_start_date       DATE;
11163 l_demand_class          varchar2(30) := null;
11164 l_uom_code              varchar2(10);
11165 l_instance_id           number;
11166 l_org_id                number;
11167 l_processing_lead_time  number;
11168 l_postprocessing_lead_time      NUMBER;   -- SCLT new variable to remove join with msc_system_items
11169 
11170 l_default_atp_rule_id           NUMBER;
11171 l_calendar_code                 VARCHAR2(14);
11172 l_calendar_exception_set_id     NUMBER;
11173 l_default_demand_class          VARCHAR2(34);
11174 l_atp_info                      MRP_ATP_PVT.ATP_Info;
11175 l_cutoff_date                   DATE;
11176 l_capacity_defined              NUMBER;
11177 l_pre_process_lt		NUMBER;
11178 l_fix_var_lt 			NUMBER;
11179 l_tolerence_defined             NUMBER;
11180 
11181 l_org_code                      VARCHAR2(7);
11182 L_QTY_BEFORE_SYSDATE            number;
11183 
11184 l_last_cap_date                 date;
11185 
11186 --s_cto_rearch
11187 l_inv_item_id                   number;
11188 l_check_cap_model_flag          number;
11189 --e_cto_rearch
11190 
11191 -- For summary enhancement
11192 l_summary_flag  NUMBER;
11193 l_summary_sql   VARCHAR2(1);
11194 
11195 -- Variables added for ship_rec_cal
11196 -- l_plan_type                  PLS_INTEGER;    -- Variables commented for
11197 -- l_enforce_sup_capacity       PLS_INTEGER;    -- Enforce Pur LT
11198 l_last_cap_next_date		DATE;
11199 l_atp_date_this_level           DATE := NULL ;
11200 l_atp_date_quantity_this_level  NUMBER := NULL;
11201 l_requested_date_quantity	NUMBER := 0;
11202 -- l_optimized_plan             NUMBER;         -- Variables commented for
11203 -- l_constrain_plan             NUMBER;         -- Enforce Pur LT
11204 
11205 -- time_phased_atp
11206 l_return_status                 VARCHAR2(1);
11207 
11208 BEGIN
11209 
11210     IF PG_DEBUG in ('Y', 'C') THEN
11211         msc_sch_wb.atp_debug('***** Begin Get_Supplier_Atp_Info Procedure *****');
11212         msc_sch_wb.atp_debug('********** INPUT DATA:Get_Supplier_Atp_Info **********');
11213         msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'inventory_item_id: '|| to_char(p_sup_atp_info_rec.inventory_item_id));
11214         msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'organization_id: '|| to_char(p_sup_atp_info_rec.organization_id));
11215         msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'requested_date: '|| to_char(p_sup_atp_info_rec.requested_date));
11216         msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'instance_id: '|| to_char(p_sup_atp_info_rec.instance_id));
11217         msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'plan_id: '|| to_char(p_sup_atp_info_rec.plan_id));
11218         msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'quantity_ordered: '|| to_char(p_sup_atp_info_rec.quantity_ordered));
11219         msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'insert_flag: '|| to_char(p_sup_atp_info_rec.insert_flag));
11220         msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'supplier_id: '|| to_char(p_sup_atp_info_rec.supplier_id));
11221         msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'supplier_site_id: '|| to_char(p_sup_atp_info_rec.supplier_site_id));
11222         msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'sup_cap_cum_date: '|| to_char(p_sup_atp_info_rec.sup_cap_cum_date));
11223         msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || ' bom_item_type : ' || p_sup_atp_info_rec.bom_item_type);
11224         msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'rep_ord_flag := ' || p_sup_atp_info_rec.rep_ord_flag);
11225         msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'base item id := ' ||  p_sup_atp_info_rec.base_item_id);
11226         msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'MSC_ATP_PVT.G_PTF_DATE := ' || MSC_ATP_PVT.G_PTF_DATE);
11227     END IF;
11228 
11229 
11230     -- initialize API return status to success
11231     x_return_status := FND_API.G_RET_STS_SUCCESS;
11232 
11233     l_requested_date := trunc(p_sup_atp_info_rec.requested_date);
11234 
11235     ---profile option for including purchase order
11236     MSC_ATP_REQ.G_PURCHASE_ORDER_PREFERENCE := NVL(FND_PROFILE.VALUE('MSC_PO_DOCK_DATE_CALC_PREF'), 2);
11237 
11238     -- Instead re-assigned local values using global variable
11239     l_uom_code := MSC_ATP_PVT.G_ITEM_INFO_REC.uom_code;
11240     l_postprocessing_lead_time := MSC_ATP_PVT.G_ITEM_INFO_REC.pre_pro_lt;
11241 
11242     IF PG_DEBUG in ('Y', 'C') THEN
11243        msc_sch_wb.atp_debug('G_PURCHASE_ORDER_PREFERENCE := ' || G_PURCHASE_ORDER_PREFERENCE);
11244        msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'l_uom_code and l_postprocessing_lead_time = '||
11245           l_uom_code ||' : '||l_postprocessing_lead_time);
11246     END IF;
11247 
11248     --s_cto_rearch
11249     IF (p_sup_atp_info_rec.bom_item_type = 4 and p_sup_atp_info_rec.rep_ord_flag = 'Y') THEN
11250            IF PG_DEBUG in ('Y', 'C') THEN
11251               msc_sch_wb.atp_debug('ATO item');
11252            END IF;
11253            l_inv_item_id := p_sup_atp_info_rec.base_item_id;
11254            l_check_cap_model_flag := 1;
11255     ELSIF  p_sup_atp_info_rec.bom_item_type = 1 THEN
11256            IF PG_DEBUG in ('Y', 'C') THEN
11257               msc_sch_wb.atp_debug('Model entity');
11258            END IF;
11259            l_inv_item_id := p_sup_atp_info_rec.inventory_item_id;
11260            l_check_cap_model_flag := 1;
11261 
11262     --bug 8631827,7592457 - support aggregate capacity check for non-ATO cases also.
11263     ELSIF (p_sup_atp_info_rec.bom_item_type = 4 and p_sup_atp_info_rec.base_item_id is not null)  THEN
11264            IF PG_DEBUG in ('Y', 'C') THEN
11265               msc_sch_wb.atp_debug('Category entity: Aggregate Supply');
11266            END IF;
11267            l_inv_item_id := p_sup_atp_info_rec.base_item_id;
11268            l_check_cap_model_flag := 1;
11269     ELSE
11270            IF PG_DEBUG in ('Y', 'C') THEN
11271               msc_sch_wb.atp_debug('Standard Item');
11272            END IF;
11273            l_inv_item_id := p_sup_atp_info_rec.inventory_item_id;
11274     END IF;
11275     --e_cto_rearch
11276     IF PG_DEBUG in ('Y', 'C') THEN
11277         msc_sch_wb.atp_debug('l_inv_item_id := ' || l_inv_item_id);
11278     END IF;
11279 
11280 
11281     -- bug 1169467
11282     -- get the plan start date. later on we will use this restrict the
11283     -- availability
11284 
11285     -- Instead re-assigned local values using global variable
11286     l_plan_start_date := MSC_ATP_PVT.G_PLAN_INFO_REC.plan_start_date;
11287     l_instance_id     := MSC_ATP_PVT.G_PLAN_INFO_REC.sr_instance_id;
11288     l_org_id          := MSC_ATP_PVT.G_PLAN_INFO_REC.organization_id;
11289     l_cutoff_date     := MSC_ATP_PVT.G_PLAN_INFO_REC.plan_cutoff_date;
11290 
11291     /* Modularize Item and Org Info */
11292     -- changed call, re-use info already obtained.
11293     -- Assumption is that since the instance and org is obtained using the plan_id,
11294     -- they are the same as the parameters p_sup_atp_info_rec.instance_id, p_sup_atp_info_rec.organization_id.
11295     MSC_ATP_PROC.get_global_org_info(l_instance_id, l_org_id);
11296     l_default_atp_rule_id := MSC_ATP_PVT.G_ORG_INFO_REC.default_atp_rule_id;
11297     l_default_demand_class := MSC_ATP_PVT.G_ORG_INFO_REC.default_demand_class;
11298     l_org_code := MSC_ATP_PVT.G_ORG_INFO_REC.org_code;
11299 
11300     /*
11301      Changes for ship_rec_cal begin
11302      1. For ship_rec_cal, Use SMC rather thn OMC for supplier capacity
11303      2. Supplier Capacity is considered infinite in following cases:
11304       (a) Plan is a constraint plan and 'enforce supplier capacity constraint' is unchecked.
11305       (b) No sources are defined. That means supplier_id = -99
11306       (b) Plan is an unconstraint plan and supplier is defined but it has not have any capacity, ie l_last_cap_date is null
11307     */
11308     l_calendar_code := p_sup_atp_info_rec.manufacturing_cal_code;
11309 
11310     l_calendar_exception_set_id := -1;
11311     -- l_enforce_sup_capacity	:= NVL(MSC_ATP_PVT.G_PLAN_INFO_REC.enforce_sup_capacity, 2); -- Enforce Pur LT
11312     l_sysdate := MSC_CALENDAR.PREV_WORK_DAY(l_calendar_code, p_sup_atp_info_rec.instance_id, sysdate);
11313     l_requested_date := MSC_CALENDAR.PREV_WORK_DAY(l_calendar_code, p_sup_atp_info_rec.instance_id, p_sup_atp_info_rec.requested_date);
11314 
11315     /* Enforce Pur LT - capacity is always enforced
11316        Removed code to check the plan type. This was earlier required as capacity constraints were earlier enforced
11317        only for unconstrained plans or if the plan option to enforce capacity was enforced. Now this is not required
11318        as capacity is always enforced. */
11319 
11320     IF PG_DEBUG in ('Y', 'C') THEN
11321        msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'l_plan_start_date = '||l_plan_start_date);
11322        msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'l_instance_id = '||l_instance_id);
11323        msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'l_org_id = '||l_org_id);
11324        msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'l_cutoff_date = '||l_cutoff_date);
11325        msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'l_default_atp_rule_id='||
11326                             l_default_atp_rule_id);
11327        msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'l_calendar_code='||l_calendar_code);
11328        msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'l_calendar_exception_set_id'||
11329                             l_calendar_exception_set_id);
11330        msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'l_default_demand_class'||
11331                             l_default_demand_class);
11332     END IF;
11333 
11334     /* Enforce Pur LT - capacity is always enforced.
11335        Aslo removed code to get last cap date as this code has been moved upstream in ATP_Check.
11336        Date is passed from there. If the passed date is NULL then it means capacity is not defined.*/
11337     -- IF (p_sup_atp_info_rec.supplier_id = -99) OR (l_plan_type = 1 AND l_enforce_sup_capacity = 2) THEN
11338     IF (p_sup_atp_info_rec.supplier_id = -99) OR p_sup_atp_info_rec.last_cap_date IS NULL THEN
11339 	l_capacity_defined := 0;
11340 	l_last_cap_next_date := MSC_CALENDAR.NEXT_WORK_DAY(
11341 		l_calendar_code,
11342 		p_sup_atp_info_rec.instance_id,
11343 		GREATEST(l_plan_start_date, MSC_ATP_PVT.G_PTF_DATE, p_sup_atp_info_rec.sup_cap_cum_date));
11344     ELSE
11345 	l_capacity_defined := 1;
11346 	l_last_cap_next_date :=  MSC_CALENDAR.NEXT_WORK_DAY(
11347                 l_calendar_code,
11348                 p_sup_atp_info_rec.instance_id,
11349                 p_sup_atp_info_rec.last_cap_date + 1);
11350     END IF;
11351     /* Enforce Pur LT changes end */
11352 
11353     IF PG_DEBUG in ('Y', 'C') THEN
11354         msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'l_last_cap_next_date='||
11355                             l_last_cap_next_date);
11356     END IF;
11357 
11358     IF l_capacity_defined = 0 THEN
11359         -- no capacity is ever defined, treat it as infinite capacity and
11360         -- by pass the huge sql for net atp
11361 
11362         -- add one more entry to indicate sysdate
11363         -- and infinite quantity.
11364         l_atp_period_tab.EXTEND;
11365         l_atp_qty_tab.EXTEND;
11366         i:= l_atp_period_tab.COUNT;
11367         IF PG_DEBUG in ('Y', 'C') THEN
11368             msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'l_plan_start_date := ' || l_plan_start_date);
11369         END IF;
11370 
11371         -- Ship_rec_cal.
11372         l_atp_period_tab(i) := l_last_cap_next_date;
11373         l_atp_qty_tab(i) := MSC_ATP_PVT.INFINITE_NUMBER;
11374 
11375         IF (NVL(p_sup_atp_info_rec.insert_flag, 0) <> 0) THEN
11376 
11377             -- dsting clear sd details temp table
11378             MSC_ATP_DB_UTILS.Clear_SD_Details_Temp();
11379 
11380             -- add one more entry to indicate infinite time fence date
11381             -- and quantity.
11382             MSC_SATP_FUNC.Extend_Atp_Period(x_atp_period, x_return_status);
11383             i:= x_atp_period.level.COUNT;
11384 
11385             x_atp_period.Level(i) := p_sup_atp_info_rec.level;
11386             x_atp_period.Identifier(i) := p_sup_atp_info_rec.identifier;
11387             x_atp_period.Scenario_Id(i) := p_sup_atp_info_rec.scenario_id;
11388             x_atp_period.Pegging_Id(i) := NULL;
11389             x_atp_period.End_Pegging_Id(i) := NULL;
11390 
11391             x_atp_period.Supplier_Id(i) := p_sup_atp_info_rec.supplier_id;
11392             x_atp_period.Supplier_Site_Id(i) := p_sup_atp_info_rec.supplier_site_id;
11393             x_atp_period.Organization_id(i) := p_sup_atp_info_rec.organization_id;
11394 
11395             --  ship_rec_cal changes begin
11396             x_atp_period.Period_Start_Date(i) := l_last_cap_next_date;
11397             --  ship_rec_cal changes end
11398 
11399             x_atp_period.Total_Supply_Quantity(i) := MSC_ATP_PVT.INFINITE_NUMBER;
11400             x_atp_period.Total_Demand_Quantity(i) := 0;
11401             x_atp_period.Period_Quantity(i) := MSC_ATP_PVT.INFINITE_NUMBER;
11402             x_atp_period.Cumulative_Quantity(i) := MSC_ATP_PVT.INFINITE_NUMBER;
11403 
11404         END IF;
11405     ELSE  -- else of l_capacity_defined = 0
11406 
11407         -- we really need to check net supplier site capacity
11408         -- we need to have a branch here for allocated atp
11409         IF (MSC_ATP_PVT.G_ALLOCATED_ATP = 'N') OR
11410            (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y' AND MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1 AND
11411                  MSC_ATP_PVT.G_ALLOCATION_METHOD = 1)  THEN
11412 
11413          -- check if we have tolerence defined for this item/org/supplier/site
11414 
11415          l_tolerence_defined := 0;
11416          BEGIN
11417            SELECT rownum
11418            INTO   l_tolerence_defined
11419            FROM   msc_supplier_flex_fences
11420            WHERE  plan_id = p_sup_atp_info_rec.plan_id
11421            AND    sr_instance_id = p_sup_atp_info_rec.instance_id
11422            AND    organization_id = p_sup_atp_info_rec.organization_id
11423            --s_cto_rearch
11424            --AND    inventory_item_id = p_sup_atp_info_rec.inventory_item_id
11425            AND    inventory_item_id = l_inv_item_id
11426            AND    supplier_id = p_sup_atp_info_rec.supplier_id
11427            AND    supplier_site_id = p_sup_atp_info_rec.supplier_site_id
11428            AND    rownum = 1;
11429         EXCEPTION
11430            WHEN NO_DATA_FOUND THEN
11431                l_tolerence_defined := 0;
11432         END;
11433 
11434         --  structure changed slightly for summary enhancement. Now it is as follows
11435         --  IF insert_flag = 0 THEN
11436         --      IF l_tolerence_defined = 0 AND G_SUMMARY_FLAG='Y' THEN
11437         --          IF summary_flag not in (1,3,9) THEN
11438         --              l_summary_sql := 'Y'
11439         --          ELSE
11440         --              l_summary_sql := 'N'
11441         --          END IF;
11442         --      END IF;
11443         --      IF l_summary_sql = 'Y' THEN
11444         --          use summary SQLs
11445         --      ELSE
11446         --          use non summary SQLs
11447         --      END IF;
11448         --  ELSE
11449         --      use details SQLs
11450         --  END IF;
11451 
11452         --=======================================================================================================
11453         --  ship_rec_cal changes
11454         --  use SMC instead of OMC for netting
11455         --  IF SMC is FOC get plan owning org's calendar. Since we assume that every org must have atleast a
11456         --  manufacturing calendar defined, we use plan owning org's calendar as it will be spanning atleast
11457         --  upto plan end date
11458         --=======================================================================================================
11459         IF l_calendar_code = '@@@' THEN
11460                 SELECT  tp.calendar_code
11461                 INTO    l_calendar_code
11462                 FROM    msc_trading_partners tp,
11463                         msc_plans mp
11464                 WHERE   mp.plan_id = p_sup_atp_info_rec.plan_id
11465                 AND     tp.sr_instance_id  = mp.sr_instance_id
11466                 AND     tp.partner_type    = 3
11467                 AND     tp.sr_tp_id        = mp.organization_id;
11468         END IF;
11469 
11470         msc_sch_wb.atp_debug('l_calendar_code := ' || l_calendar_code);
11471         --=======================================================================================================
11472         -- ship_rec_cal changes begin
11473         --=======================================================================================================
11474         --  In all the SQLs that get supplier capacities following are the changes:
11475         --  1. Pass (c.seq_num - p_sup_atp_info_rec.sysdate_seq_num) to get_tolerance_percentage fn instead of
11476         --     passing c.calendar_date.
11477         --  2. If calendar code passed in FOC, we use plan owning org's calendar and remove p_seq_num is not
11478         --     null filter condition.
11479         --
11480         --  In all the SQLs that get planned orders, purchase orders and purchase requisitions following
11481         --  are the changes:
11482         --  1. We use new_dock_date or new_ship_date depending on whether supplier capacity is dock capacity or
11483         --     ship capacity.
11484         --     Earlier we used to look at new_schedule_date and offset post_processing_lead_time.
11485         --  2. Removed join with msc_calendar_dates
11486         --=======================================================================================================
11487 
11488         IF (NVL(p_sup_atp_info_rec.insert_flag, 0) = 0) THEN
11489             IF (l_tolerence_defined = 0) AND (MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y') THEN
11490                 ---- we do summary approach only if tolerance is not defined
11491                 ---- since one of the components for calculating tolerance is difference of sys_date and
11492                 ---- request date, we might not get a right data if we include tolerance in summary data
11493 
11494                 -- Summary enhancement - check summary flag
11495                 SELECT  summary_flag
11496                 INTO    l_summary_flag
11497                 FROM    msc_plans plans
11498                 WHERE   plans.plan_id = p_sup_atp_info_rec.plan_id;
11499 
11500                 IF PG_DEBUG in ('Y', 'C') THEN
11501                     msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'l_summary_flag := ' || l_summary_flag);
11502                 END IF;
11503 
11504                 IF l_summary_flag NOT IN (MSC_POST_PRO.G_SF_SUMMARY_NOT_RUN, MSC_POST_PRO.G_SF_PREALLOC_COMPLETED,
11505                                           MSC_POST_PRO.G_SF_FULL_SUMMARY_RUNNING) THEN
11506                     -- Summary SQL can be used
11507                     l_summary_sql := 'Y';
11508                 ELSE
11509                     l_summary_sql := 'N';
11510                 END IF;
11511             ELSE
11512                 l_summary_sql := 'N';
11513             END IF;
11514 
11515             IF l_summary_sql = 'Y' THEN
11516                 IF PG_DEBUG in ('Y', 'C') THEN
11517                     msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'Summary mode supplier info');
11518                 END IF;
11519 
11520                 IF l_check_cap_model_flag = 1 THEN
11521 
11522                     SELECT  SD_DATE,
11523                             SUM(SD_QTY)
11524                     BULK COLLECT INTO l_atp_period_tab, l_atp_qty_tab
11525                     FROM
11526                         (
11527                             select  /*+ INDEX(msc_atp_summary_sup MSC_ATP_SUMMARY_SUP_U1) */
11528                                     sd_date,
11529                                     sd_qty
11530                             from    msc_atp_summary_sup
11531                             where   plan_id = p_sup_atp_info_rec.plan_id
11532                             and     sr_instance_id = p_sup_atp_info_rec.instance_id
11533                             and     supplier_id = p_sup_atp_info_rec.supplier_id
11534                             and     supplier_site_id = p_sup_atp_info_rec.supplier_site_id
11535                             --and     sd_date >= l_plan_start_date
11536                             and     sd_date BETWEEN l_plan_start_date
11537                                                     AND  least(p_sup_atp_info_rec.last_cap_date,l_cutoff_date) --4055719
11538                             and     sd_qty <> 0
11539                             AND    (inventory_item_id = l_inv_item_id OR
11540                                     inventory_item_id in
11541                                            (select inventory_item_id from msc_system_items msi
11542                                             where  msi.base_item_id = l_inv_item_id
11543                                             and    msi.plan_id = p_sup_atp_info_rec.plan_id
11544                                             and    msi.organization_id = p_sup_atp_info_rec.organization_id
11545                                             and    msi.base_item_id = l_inv_item_id))
11546 
11547                             UNION ALL
11548 
11549                             -- Summary enhancement : differences from non summary SQL: ship/rec cal changes pending
11550                             --  1. No union with MSC_SUPPLIER_CAPACITIES
11551                             --  2. MSC_PLANS included in the join to get latest refresh number
11552                             --  3. Filter records based on refresh_number
11553                             --Fixing as a part of bug3709707 adding trunc so that 2 column are not seen in HP
11554                             SELECT  TRUNC(Decode(p_sup_atp_info_rec.sup_cap_type,
11555                                                 1, p.new_ship_date,
11556                                                 p.new_dock_date)) l_date, -- For ship_rec_cal
11557                                     (NVL(p.implement_quantity,0) - p.new_order_quantity) quantity
11558                             FROM    msc_supplies p,
11559                                     msc_plans pl            -- For summary enhancement
11560                             WHERE  (p.order_type IN (5, 2,60)
11561                                     OR (MSC_ATP_REQ.G_PURCHASE_ORDER_PREFERENCE = MSC_ATP_REQ.G_PROMISE_DATE
11562                                     AND p.order_type = 1 AND p.promised_date IS NULL))
11563                             AND     p.plan_id = p_sup_atp_info_rec.plan_id
11564                             AND     p.sr_instance_id = p_sup_atp_info_rec.instance_id
11565                             AND     p.supplier_id  = p_sup_atp_info_rec.supplier_id
11566                             AND     NVL(p.supplier_site_id, -1) = NVL(p_sup_atp_info_rec.supplier_site_id, -1)
11567                             AND     NVL(P.DISPOSITION_STATUS_TYPE, 1) <> 2
11568                             AND    (p.inventory_item_id = l_inv_item_id OR
11569                                     p.inventory_item_id in
11570                                            (select inventory_item_id from msc_system_items msi
11571                                             where  msi.sr_instance_id = p_sup_atp_info_rec.instance_id
11572                                             and    msi.plan_id = p_sup_atp_info_rec.plan_id
11573                                             and    msi.organization_id = p_sup_atp_info_rec.organization_id
11574                                             and    msi.base_item_id = l_inv_item_id))
11575                             AND     pl.plan_id = p.plan_id                          -- For summary enhancement
11576                             AND     (p.refresh_number > pl.latest_refresh_number    -- For summary enhancement
11577                                      OR p.refresh_number = p_sup_atp_info_rec.refresh_number)        -- For summary enhancement
11578                             AND    Decode(p_sup_atp_info_rec.sup_cap_type, 1, p.new_ship_date,p.new_dock_date) --4055719
11579                                           <= least(p_sup_atp_info_rec.last_cap_date,l_cutoff_date)
11580                         )
11581                     GROUP BY SD_DATE
11582                     ORDER BY SD_DATE;
11583 
11584                 ELSE
11585 
11586                     SELECT  SD_DATE,
11587                             SUM(SD_QTY)
11588                     BULK COLLECT INTO l_atp_period_tab, l_atp_qty_tab
11589                     FROM
11590                         (
11591                             select  /*+ INDEX(msc_atp_summary_sup MSC_ATP_SUMMARY_SUP_U1) */
11592                                    trunc( sd_date) SD_DATE, --4135752
11593                                     sd_qty
11594                             from    msc_atp_summary_sup
11595                             where   plan_id = p_sup_atp_info_rec.plan_id
11596                             and     sr_instance_id = p_sup_atp_info_rec.instance_id
11597                             and     inventory_item_id = p_sup_atp_info_rec.inventory_item_id
11598                             and     supplier_id = p_sup_atp_info_rec.supplier_id
11599                             and     supplier_site_id = p_sup_atp_info_rec.supplier_site_id
11600                             --and     sd_date >= l_plan_start_date
11601                             and     sd_date BETWEEN l_plan_start_date
11602                                                     AND  least(p_sup_atp_info_rec.last_cap_date,l_cutoff_date) --4055719
11603                             and     sd_qty <> 0
11604 
11605                             UNION ALL
11606 
11607                             -- Summary enhancement : differences from non summary SQL: ship/rec cal changes pending
11608                             --  1. No union with MSC_SUPPLIER_CAPACITIES
11609                             --  2. MSC_PLANS included in the join to get latest refresh number
11610                             --  3. Filter records based on refresh_number
11611                             --Fixing as a part of bug3709707 adding trunc so that 2 column are not seen in HP
11612                             SELECT  TRUNC(Decode(p_sup_atp_info_rec.sup_cap_type,
11613                                                 1, p.new_ship_date,
11614                                                 p.new_dock_date)) l_date, -- For ship_rec_cal
11615                                     (NVL(p.implement_quantity,0) - p.new_order_quantity) quantity
11616                             FROM    msc_supplies p,
11617                                     msc_plans pl            -- For summary enhancement
11618                             WHERE  (p.order_type IN (5, 2, 60)
11619                                     OR (MSC_ATP_REQ.G_PURCHASE_ORDER_PREFERENCE = MSC_ATP_REQ.G_PROMISE_DATE
11620                                     AND p.order_type = 1 AND p.promised_date IS NULL))
11621                             AND     p.plan_id = p_sup_atp_info_rec.plan_id
11622                             AND     p.sr_instance_id = p_sup_atp_info_rec.instance_id
11623                             AND     p.inventory_item_id = p_sup_atp_info_rec.inventory_item_id
11624                             AND     p.supplier_id  = p_sup_atp_info_rec.supplier_id
11625                             AND     NVL(p.supplier_site_id, -1) = NVL(p_sup_atp_info_rec.supplier_site_id, -1)
11626                             AND     NVL(P.DISPOSITION_STATUS_TYPE, 1) <> 2
11627                             AND     pl.plan_id = p.plan_id                          -- For summary enhancement
11628                             AND     (p.refresh_number > pl.latest_refresh_number    -- For summary enhancement
11629                                      OR p.refresh_number = p_sup_atp_info_rec.refresh_number)        -- For summary enhancement
11630                             AND     Decode(p_sup_atp_info_rec.sup_cap_type, 1, trunc(p.new_ship_date),trunc(p.new_dock_date)) --4055719 --4135752
11631                                           <= trunc(least(p_sup_atp_info_rec.last_cap_date,l_cutoff_date))          --4135752
11632                         )
11633                     GROUP BY SD_DATE
11634                     ORDER BY SD_DATE;
11635 
11636                 END IF;
11637 
11638                 MSC_ATP_PROC.atp_consume(l_atp_qty_tab, l_atp_qty_tab.COUNT);
11639 
11640                 /* Cum drop issue changes begin*/
11641                 MSC_AATP_PROC.Atp_Remove_Negatives(l_atp_qty_tab, l_return_status);
11642                 IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
11643                         IF PG_DEBUG in ('Y', 'C') THEN
11644                                 msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'Error occured in procedure Atp_Remove_Negatives');
11645                         END IF;
11646                         RAISE FND_API.G_EXC_ERROR;
11647                 END IF;
11648                 /* Cum drop issue changes end*/
11649 
11650             ELSE    -- IF l_summary_sql = 'Y' THEN
11651 
11652                 IF l_check_cap_model_flag = 1 THEN
11653                     msc_sch_wb.atp_debug('Check Sources for model, details are off');
11654 
11655                     SELECT  trunc(l_date), SUM(quantity)  --4135752
11656                     BULK COLLECT INTO
11657                     l_atp_period_tab,
11658                     l_atp_qty_tab
11659                     FROM (
11660                     SELECT c.calendar_date l_date, s.capacity*(1+
11661                                                  DECODE(l_tolerence_defined, 0, 0,
11662                                                  NVL(MSC_ATP_FUNC.get_tolerance_percentage(
11663                                                  p_sup_atp_info_rec.instance_id,
11664                                                  p_sup_atp_info_rec.plan_id,
11665                                                  l_inv_item_id,
11666                                                  p_sup_atp_info_rec.organization_id,
11667                                                  p_sup_atp_info_rec.supplier_id,
11668                                                  p_sup_atp_info_rec.supplier_site_id,
11669                                                  -- ship_rec_cal
11670                                                  c.seq_num - p_sup_atp_info_rec.sysdate_seq_num),0))) quantity
11671                     FROM   msc_calendar_dates c,
11672                            msc_supplier_capacities s
11673                     WHERE  s.inventory_item_id = l_inv_item_id
11674                     AND    s.sr_instance_id = p_sup_atp_info_rec.instance_id
11675                     AND    s.plan_id = p_sup_atp_info_rec.plan_id
11676                     AND    s.organization_id = p_sup_atp_info_rec.organization_id
11677                     AND    s.supplier_id = p_sup_atp_info_rec.supplier_id
11678                     AND    NVL(s.supplier_site_id, -1) = NVL(p_sup_atp_info_rec.supplier_site_id, -1)
11679                     AND    c.calendar_date BETWEEN trunc(s.from_date)
11680                                            --AND NVL(s.to_date,l_cutoff_date)
11681                                            AND trunc(NVL(s.to_date,least(p_sup_atp_info_rec.last_cap_date,l_cutoff_date))) --4055719
11682                     AND    (c.seq_num IS NOT NULL OR p_sup_atp_info_rec.manufacturing_cal_code  = MSC_CALENDAR.FOC)
11683                     AND    c.calendar_code = l_calendar_code
11684                     AND    c.exception_set_id = l_calendar_exception_set_id
11685                     AND    c.sr_instance_id = s.sr_instance_id -- Changed from l_instance_id ?
11686                     AND    c.calendar_date >= p_sup_atp_info_rec.sup_cap_cum_date
11687                     -- Supplier Capacity (SCLT) Accumulation starts from this date.
11688                     -- AND    c.calendar_date >= l_plan_start_date -- bug 1169467
11689                     UNION ALL
11690                     /* Net out planned orders, purchase orders and purchase requisitions */
11691                     -- bug 1303196
11692                     --Fixing as a part of bug3709707 adding trunc so that 2 column are not seen in HP
11693                     SELECT TRUNC(Decode(p_sup_atp_info_rec.sup_cap_type,
11694                                                 1, p.new_ship_date,
11695                                                 p.new_dock_date)) l_date, -- For ship_rec_cal
11696         	           -- performance dsting rearrange signs to get rid of multiply times -1
11697                            (NVL(p.implement_quantity,0) - p.new_order_quantity) quantity
11698                     FROM   msc_supplies p
11699                     WHERE  (p.order_type IN (5, 2, 60)
11700                             --include purchase orders based on profile option
11701                             OR (MSC_ATP_REQ.G_PURCHASE_ORDER_PREFERENCE = MSC_ATP_REQ.G_PROMISE_DATE
11702                                 AND p.order_type = 1 AND p.promised_date IS NULL))
11703                     -- Supplier Capacity (SCLT) Accumulation Ignore Purchase Orders
11704                     -- WHERE  p.order_type IN (5, 1, 2)
11705                     AND    p.plan_id = p_sup_atp_info_rec.plan_id
11706                     AND    p.sr_instance_id = p_sup_atp_info_rec.instance_id
11707               -- 1214694      AND    p.organization_id = p_sup_atp_info_rec.organization_id
11708                     AND    p.supplier_id  = p_sup_atp_info_rec.supplier_id
11709                     AND    NVL(p.supplier_site_id, -1) = NVL(p_sup_atp_info_rec.supplier_site_id, -1)
11710                            -- Exclude Cancelled Supplies 2460645
11711                     AND    NVL(P.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
11712                     -- Supplier Capacity (SCLT) Changes End
11713                            ---only consider ATP inserted POs
11714                            --- Plan POs are tied to forecast.
11715                     AND    ((p.inventory_item_id = l_inv_item_id and p.record_source=2) OR
11716                             p.inventory_item_id in
11717                                    (select inventory_item_id from msc_system_items msi
11718                                     where  msi.sr_instance_id = p_sup_atp_info_rec.instance_id
11719                                     and    msi.plan_id = p_sup_atp_info_rec.plan_id
11720                                     and    msi.organization_id = p_sup_atp_info_rec.organization_id
11721                                     and    msi.base_item_id = l_inv_item_id))
11722                      AND    trunc(Decode(p_sup_atp_info_rec.sup_cap_type, 1, p.new_ship_date,p.new_dock_date)) --4055719 --4135752
11723                                  <= trunc(least(p_sup_atp_info_rec.last_cap_date,l_cutoff_date)))
11724                     GROUP BY l_date
11725                     ORDER BY l_date;
11726 
11727                     MSC_ATP_PROC.atp_consume(l_atp_qty_tab, l_atp_qty_tab.COUNT);
11728 
11729                     /* Cum drop issue changes begin*/
11730                     MSC_AATP_PROC.Atp_Remove_Negatives(l_atp_qty_tab, l_return_status);
11731                     IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
11732                         IF PG_DEBUG in ('Y', 'C') THEN
11733                                 msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'Error occured in procedure Atp_Remove_Negatives');
11734                         END IF;
11735                         RAISE FND_API.G_EXC_ERROR;
11736                     END IF;
11737                     /* Cum drop issue changes end*/
11738 
11739                 ELSE
11740 
11741                     SELECT  trunc(l_date), SUM(quantity)  --4135752
11742                     BULK COLLECT INTO
11743                     l_atp_period_tab,
11744                     l_atp_qty_tab
11745                     FROM (
11746                     SELECT c.calendar_date l_date, s.capacity*(1+
11747                                                  DECODE(l_tolerence_defined, 0, 0,
11748                                                  NVL(MSC_ATP_FUNC.get_tolerance_percentage(
11749                                                  p_sup_atp_info_rec.instance_id,
11750                                                  p_sup_atp_info_rec.plan_id,
11751                                                  p_sup_atp_info_rec.inventory_item_id,
11752                                                  p_sup_atp_info_rec.organization_id,
11753                                                  p_sup_atp_info_rec.supplier_id,
11754                                                  p_sup_atp_info_rec.supplier_site_id,
11755                                                  -- ship_rec_cal
11756                                                  c.seq_num - p_sup_atp_info_rec.sysdate_seq_num),0))) quantity
11757                     FROM   msc_calendar_dates c,
11758                            msc_supplier_capacities s
11759                     WHERE  s.inventory_item_id = p_sup_atp_info_rec.inventory_item_id
11760                     AND    s.sr_instance_id = p_sup_atp_info_rec.instance_id
11761                     AND    s.plan_id = p_sup_atp_info_rec.plan_id
11762                     AND    s.organization_id = p_sup_atp_info_rec.organization_id
11763                     AND    s.supplier_id = p_sup_atp_info_rec.supplier_id
11764                     AND    NVL(s.supplier_site_id, -1) = NVL(p_sup_atp_info_rec.supplier_site_id, -1)
11765                     AND    c.calendar_date BETWEEN trunc(s.from_date)
11766                                            --AND NVL(s.to_date,l_cutoff_date)
11767                                            AND trunc(NVL(s.to_date,least(p_sup_atp_info_rec.last_cap_date,l_cutoff_date))) --4055719
11768                     AND    (c.seq_num IS NOT NULL OR p_sup_atp_info_rec.manufacturing_cal_code  = MSC_CALENDAR.FOC)
11769                     AND    c.calendar_code = l_calendar_code
11770                     AND    c.exception_set_id = l_calendar_exception_set_id
11771                     AND    c.sr_instance_id = s.sr_instance_id -- Changed from l_instance_id ?
11772                     AND    c.calendar_date >= trunc(p_sup_atp_info_rec.sup_cap_cum_date) --4135752
11773                     -- Supplier Capacity (SCLT) Accumulation starts from this date.
11774                     -- AND    c.calendar_date >= l_plan_start_date -- bug 1169467
11775                     UNION ALL
11776                     /* Net out planned orders, purchase orders and purchase requisitions */
11777                     -- bug 1303196
11778                     --Fixing as a part of bug3709707 adding trunc so that 2 column are not seen in HP
11779                     SELECT TRUNC(Decode(p_sup_atp_info_rec.sup_cap_type,
11780                                                 1, p.new_ship_date,
11781                                                 p.new_dock_date)) l_date, -- For ship_rec_cal
11782         	           -- performance dsting rearrange signs to get rid of multiply times -1
11783                            (NVL(p.implement_quantity,0) - p.new_order_quantity) quantity
11784                     FROM   msc_supplies p
11785                         WHERE  (p.order_type IN (5, 2, 60)
11786                                 --include purchase orders based on profile option
11787                                 OR (MSC_ATP_REQ.G_PURCHASE_ORDER_PREFERENCE = MSC_ATP_REQ.G_PROMISE_DATE
11788                                     AND p.order_type = 1 AND p.promised_date IS NULL))
11789                         -- Supplier Capacity (SCLT) Accumulation Ignore Purchase Orders
11790                         -- WHERE  p.order_type IN (5, 1, 2)
11791                         AND    p.plan_id = p_sup_atp_info_rec.plan_id
11792                         AND    p.sr_instance_id = p_sup_atp_info_rec.instance_id
11793                         AND    p.inventory_item_id = p_sup_atp_info_rec.inventory_item_id
11794                   -- 1214694      AND    p.organization_id = p_sup_atp_info_rec.organization_id
11795                         AND    p.supplier_id  = p_sup_atp_info_rec.supplier_id
11796                         AND    NVL(p.supplier_site_id, -1) = NVL(p_sup_atp_info_rec.supplier_site_id, -1)
11797                                -- Exclude Cancelled Supplies 2460645
11798                         AND    NVL(P.DISPOSITION_STATUS_TYPE, 1) <> 2
11799                         AND   trunc( Decode(p_sup_atp_info_rec.sup_cap_type, 1, p.new_ship_date,p.new_dock_date)) --4055719 --4135752
11800                                       <= trunc(least(p_sup_atp_info_rec.last_cap_date,l_cutoff_date)))
11801                         GROUP BY l_date
11802                         ORDER BY l_date;
11803 
11804                         MSC_ATP_PROC.atp_consume(l_atp_qty_tab, l_atp_qty_tab.COUNT);
11805 
11806                         /* Cum drop issue changes begin*/
11807                         MSC_AATP_PROC.Atp_Remove_Negatives(l_atp_qty_tab, l_return_status);
11808                         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
11809                                 IF PG_DEBUG in ('Y', 'C') THEN
11810                                         msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'Error occured in procedure Atp_Remove_Negatives');
11811                                 END IF;
11812                                 RAISE FND_API.G_EXC_ERROR;
11813                         END IF;
11814                         /* Cum drop issue changes end*/
11815 
11816                     END IF; --IF l_check_cap_model_flag = 1 THEN
11817 
11818                 END IF; -- IF l_summary_sql = 'Y' THEN
11819 
11820             ELSE  -- now p_sup_atp_info_rec.insert_flag <> 0
11821 
11822                 MSC_ATP_DB_UTILS.Clear_SD_Details_temp();
11823 
11824                 IF l_check_cap_model_flag = 1 THEN
11825                     msc_sch_wb.atp_debug('Check Sources for model, details are on');
11826                     INSERT INTO msc_atp_sd_details_temp (
11827                        ATP_level,
11828                        Order_line_id,
11829                        Scenario_Id,
11830                        Inventory_Item_Id,
11831                        Request_Item_Id,
11832                        Organization_Id,
11833                        Department_Id,
11834                        Resource_Id,
11835                        Supplier_Id,
11836                        Supplier_Site_Id,
11837                        From_Organization_Id,
11838                        From_Location_Id,
11839                        To_Organization_Id,
11840                        To_Location_Id,
11841                        Ship_Method,
11842                        UOM_code,
11843                        Supply_Demand_Type,
11844                        Supply_Demand_Source_Type,
11845                        Supply_Demand_Source_Type_Name,
11846                        Identifier1,
11847                        Identifier2,
11848                        Identifier3,
11849                        Identifier4,
11850                        Supply_Demand_Quantity,
11851                        Supply_Demand_Date,
11852                        Disposition_Type,
11853                        Disposition_Name,
11854                        Pegging_Id,
11855                        End_Pegging_Id,
11856                        creation_date,
11857                        created_by,
11858                        last_update_date,
11859                        last_updated_by,
11860                        last_update_login
11861                     )
11862 
11863                          (SELECT
11864                                 p_sup_atp_info_rec.level col1,
11865                                 MSC_ATP_PVT.G_ORDER_LINE_ID col2,
11866                                 p_sup_atp_info_rec.scenario_id col3,
11867                                 l_null_num col4 ,
11868                                 l_null_num col5,
11869                                 p_sup_atp_info_rec.organization_id col6,
11870                                 l_null_num col7,
11871                                 l_null_num col8,
11872                                 p_sup_atp_info_rec.supplier_id col9,
11873                                 p_sup_atp_info_rec.supplier_site_id col10,
11874                                 l_null_num col11,
11875                                 l_null_num col12,
11876                                 l_null_num col13,
11877                                 l_null_num col14,
11878                                 l_null_char col15,
11879                                 l_uom_code col16,
11880                                 2 col17, -- supply
11881                                 l_null_num col18,
11882                                 l_null_char col19,
11883                                 p_sup_atp_info_rec.instance_id col20,
11884                                 l_null_num col21,
11885                                 l_null_num col22,
11886                                 l_null_num col23,
11887                                 s.capacity*(1+ DECODE(l_tolerence_defined, 0, 0,
11888                                                   NVL(MSC_ATP_FUNC.get_tolerance_percentage(
11889                                                   p_sup_atp_info_rec.instance_id,
11890                                                   p_sup_atp_info_rec.plan_id,
11891                                                   l_inv_item_id,
11892                                                   p_sup_atp_info_rec.organization_id,
11893                                                   p_sup_atp_info_rec.supplier_id,
11894                                                   p_sup_atp_info_rec.supplier_site_id,
11895                                                   -- ship_rec_cal
11896                                                   c.seq_num - p_sup_atp_info_rec.sysdate_seq_num),0))) col24,
11897                                 C.CALENDAR_DATE col25,
11898                                 l_null_num col26,
11899                                 l_null_char col27,
11900                                 l_null_num col28,
11901                                 l_null_num col29,
11902                                 l_sysdate,
11903                                 FND_GLOBAL.User_ID,
11904                                 l_sysdate,
11905                                 FND_GLOBAL.User_ID,
11906                                 FND_GLOBAL.User_ID
11907                          FROM   msc_calendar_dates c,
11908                                 msc_supplier_capacities s
11909                          WHERE  s.inventory_item_id = l_inv_item_id
11910                          AND    s.sr_instance_id = p_sup_atp_info_rec.instance_id
11911                          AND    s.plan_id = p_sup_atp_info_rec.plan_id
11912                          AND    s.organization_id = p_sup_atp_info_rec.organization_id
11913                          AND    s.supplier_id = p_sup_atp_info_rec.supplier_id
11914                          AND    NVL(s.supplier_site_id, -1) = NVL(p_sup_atp_info_rec.supplier_site_id, -1)
11915                          AND    c.calendar_date BETWEEN trunc(s.from_date)
11916                                                 --AND NVL(s.to_date,l_cutoff_date)
11917                                                 AND trunc(NVL(s.to_date,least(p_sup_atp_info_rec.last_cap_date,l_cutoff_date))) --4055719
11918                          AND    (c.seq_num IS NOT NULL OR p_sup_atp_info_rec.manufacturing_cal_code  = MSC_CALENDAR.FOC)
11919                          AND    c.calendar_code = l_calendar_code
11920                          AND    c.exception_set_id = l_calendar_exception_set_id
11921                          AND    c.sr_instance_id = s.sr_instance_id  -- Changed from l_instance_id ?
11922                          AND    c.calendar_date >= trunc(p_sup_atp_info_rec.sup_cap_cum_date) --4135752
11923                          -- Supplier Capacity (SCLT) Accumulation starts from this date.
11924                          -- AND    c.calendar_date >= l_plan_start_date -- bug 1169467
11925                          UNION ALL
11926                          SELECT
11927                                 p_sup_atp_info_rec.level col1,
11928                                 MSC_ATP_PVT.G_ORDER_LINE_ID col2,
11929                                 p_sup_atp_info_rec.scenario_id col3,
11930                                 l_null_num col4 ,
11931                                 l_null_num col5,
11932                                 p_sup_atp_info_rec.organization_id col6,
11933                                 l_null_num col7,
11934                                 l_null_num col8,
11935                                 p_sup_atp_info_rec.supplier_id col9,
11936                                 p_sup_atp_info_rec.supplier_site_id col10,
11937                                 l_null_num col11,
11938                                 l_null_num col12,
11939                                 l_null_num col13,
11940                                 l_null_num col14,
11941                                 l_null_char col15,
11942                                 l_uom_code col16,
11943                                 1 col17, -- demand
11944                                 p.order_type col18,
11945                                 l_null_char col19,
11946                                 p_sup_atp_info_rec.instance_id col20,
11947                                 l_null_num col21,
11948                                 TRANSACTION_ID col22,
11949                                 l_null_num col23,
11950                                 -- performance dsting rearrange signs to get rid of multiply times - 1
11951                                 (NVL(p.implement_quantity,0) - p.new_order_quantity) col24,
11952                                 --Fixing as a part of bug3709707 adding trunc so that 2 column are not seen in HP
11953                                 TRUNC(Decode(p_sup_atp_info_rec.sup_cap_type,
11954                                                 1, p.new_ship_date,
11955                                                 p.new_dock_date)) col25, -- For ship_rec_cal
11956                                 l_null_num col26,
11957                                 --bug 4493399: show transaction id for PO
11958                                 --p.order_number col27,
11959                                 DECODE(p.ORDER_TYPE, 5, to_char(p.TRANSACTION_ID), p.order_number) col27,
11960                                 l_null_num col28,
11961                                 l_null_num col29,
11962                                 l_sysdate,
11963                                 FND_GLOBAL.User_ID,
11964                                 l_sysdate,
11965                                 FND_GLOBAL.User_ID,
11966                                 FND_GLOBAL.User_ID
11967                                 -- Supplier Capacity (SCLT) Changes Begin
11968                          FROM   msc_supplies p
11969                          WHERE  (p.order_type IN (5, 2, 60)
11970                                  --include purchase orders based on profile option
11971                                  OR (MSC_ATP_REQ.G_PURCHASE_ORDER_PREFERENCE = MSC_ATP_REQ.G_PROMISE_DATE
11972                                      AND p.order_type = 1 AND p.promised_date IS NULL))
11973                          -- Supplier Capacity (SCLT) Accumulation Ignore Purchase Orders
11974                          -- WHERE  p.order_type IN (5, 1, 2)
11975                          AND    p.plan_id = p_sup_atp_info_rec.plan_id
11976                          AND    p.sr_instance_id = p_sup_atp_info_rec.instance_id
11977                          --AND    p.inventory_item_id = p_sup_atp_info_rec.inventory_item_id
11978                    -- 1214694      AND    p.organization_id = p_sup_atp_info_rec.organization_id
11979                          AND    p.supplier_id  = p_sup_atp_info_rec.supplier_id
11980                          AND    NVL(p.supplier_site_id, -1) = NVL(p_sup_atp_info_rec.supplier_site_id, -1)
11981                                 -- Exclude Cancelled Supplies 2460645
11982                          AND    NVL(P.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
11983                                  ---we only consider ATP inserted PO for MOdels.
11984                                  --Ignore Planning inserted POs for models as they would be tied to forecats
11985                          AND    ((p.inventory_item_id = l_inv_item_id and p.record_source=2) OR
11986                                     p.inventory_item_id in
11987                                            (select inventory_item_id from msc_system_items msi
11988                                             where  msi.sr_instance_id = p_sup_atp_info_rec.instance_id
11989                                             and    msi.plan_id = p_sup_atp_info_rec.plan_id
11990                                             and    msi.organization_id = p_sup_atp_info_rec.organization_id
11991                                             and    msi.base_item_id = l_inv_item_id))
11992                          AND    Decode(p_sup_atp_info_rec.sup_cap_type, 1, p.new_ship_date,p.new_dock_date) --4055719
11993                                        <= least(p_sup_atp_info_rec.last_cap_date,l_cutoff_date)
11994                     );
11995                 ELSE
11996             	   INSERT INTO msc_atp_sd_details_temp (
11997             		   ATP_level,
11998             		   Order_line_id,
11999             		   Scenario_Id,
12000             		   Inventory_Item_Id,
12001             		   Request_Item_Id,
12002             		   Organization_Id,
12003             		   Department_Id,
12004             		   Resource_Id,
12005             		   Supplier_Id,
12006             		   Supplier_Site_Id,
12007             		   From_Organization_Id,
12008             		   From_Location_Id,
12009             		   To_Organization_Id,
12010             		   To_Location_Id,
12011             		   Ship_Method,
12012             		   UOM_code,
12013             		   Supply_Demand_Type,
12014             		   Supply_Demand_Source_Type,
12015             		   Supply_Demand_Source_Type_Name,
12016             		   Identifier1,
12017             		   Identifier2,
12018             		   Identifier3,
12019             		   Identifier4,
12020             		   Supply_Demand_Quantity,
12021             		   Supply_Demand_Date,
12022             		   Disposition_Type,
12023             		   Disposition_Name,
12024             		   Pegging_Id,
12025             		   End_Pegging_Id,
12026             		   creation_date,
12027             		   created_by,
12028             		   last_update_date,
12029             		   last_updated_by,
12030             		   last_update_login
12031             	   )
12032 
12033                          (SELECT
12034                                p_sup_atp_info_rec.level col1,
12035                                MSC_ATP_PVT.G_ORDER_LINE_ID col2,
12036                                p_sup_atp_info_rec.scenario_id col3,
12037                                l_null_num col4 ,
12038                                l_null_num col5,
12039                                p_sup_atp_info_rec.organization_id col6,
12040                                l_null_num col7,
12041                                l_null_num col8,
12042                                p_sup_atp_info_rec.supplier_id col9,
12043                                p_sup_atp_info_rec.supplier_site_id col10,
12044                                l_null_num col11,
12045                                l_null_num col12,
12046                                l_null_num col13,
12047                                l_null_num col14,
12048                                l_null_char col15,
12049                                l_uom_code col16,
12050                                2 col17, -- supply
12051                                l_null_num col18,
12052                                l_null_char col19,
12053                                p_sup_atp_info_rec.instance_id col20,
12054                                l_null_num col21,
12055                                l_null_num col22,
12056                                l_null_num col23,
12057                                s.capacity*(1+ DECODE(l_tolerence_defined, 0, 0,
12058                                                   NVL(MSC_ATP_FUNC.get_tolerance_percentage(
12059                                                   p_sup_atp_info_rec.instance_id,
12060                                                   p_sup_atp_info_rec.plan_id,
12061                                                   p_sup_atp_info_rec.inventory_item_id,
12062                                                   p_sup_atp_info_rec.organization_id,
12063                                                   p_sup_atp_info_rec.supplier_id,
12064                                                   p_sup_atp_info_rec.supplier_site_id,
12065                                                   -- ship_rec_cal
12066                                                   c.seq_num - p_sup_atp_info_rec.sysdate_seq_num),0))) col24,
12067                                C.CALENDAR_DATE col25,
12068                                l_null_num col26,
12069                                l_null_char col27,
12070                                l_null_num col28,
12071                                l_null_num col29,
12072             		   l_sysdate,
12073             		   FND_GLOBAL.User_ID,
12074             		   l_sysdate,
12075             		   FND_GLOBAL.User_ID,
12076             		   FND_GLOBAL.User_ID
12077                      FROM   msc_calendar_dates c,
12078                             msc_supplier_capacities s
12079                      WHERE  s.inventory_item_id = l_inv_item_id
12080                      AND    s.sr_instance_id = p_sup_atp_info_rec.instance_id
12081                      AND    s.plan_id = p_sup_atp_info_rec.plan_id
12082                      AND    s.organization_id = p_sup_atp_info_rec.organization_id
12083                      AND    s.supplier_id = p_sup_atp_info_rec.supplier_id
12084                      AND    NVL(s.supplier_site_id, -1) = NVL(p_sup_atp_info_rec.supplier_site_id, -1)
12085                      AND    c.calendar_date BETWEEN trunc(s.from_date)
12086                                             --AND NVL(s.to_date,l_cutoff_date)
12087                                             AND trunc(NVL(s.to_date,least(p_sup_atp_info_rec.last_cap_date,l_cutoff_date))) --4055719
12088                      AND    (c.seq_num IS NOT NULL OR p_sup_atp_info_rec.manufacturing_cal_code  = MSC_CALENDAR.FOC)
12089                      AND    c.calendar_code = l_calendar_code
12090                      AND    c.exception_set_id = l_calendar_exception_set_id
12091                      AND    c.sr_instance_id = s.sr_instance_id  -- Changed from l_instance_id ?
12092                      AND    c.calendar_date >= p_sup_atp_info_rec.sup_cap_cum_date
12093                      -- Supplier Capacity (SCLT) Accumulation starts from this date.
12094                      -- AND    c.calendar_date >= l_plan_start_date -- bug 1169467
12095                      UNION ALL
12096                      SELECT
12097                                p_sup_atp_info_rec.level col1,
12098                                MSC_ATP_PVT.G_ORDER_LINE_ID col2,
12099                                p_sup_atp_info_rec.scenario_id col3,
12100                                l_null_num col4 ,
12101                                l_null_num col5,
12102                                p_sup_atp_info_rec.organization_id col6,
12103                                l_null_num col7,
12104                                l_null_num col8,
12105                                p_sup_atp_info_rec.supplier_id col9,
12106                                p_sup_atp_info_rec.supplier_site_id col10,
12107                                l_null_num col11,
12108                                l_null_num col12,
12109                                l_null_num col13,
12110                                l_null_num col14,
12111                                l_null_char col15,
12112                                l_uom_code col16,
12113                                1 col17, -- demand
12114                                p.order_type col18,
12115                                l_null_char col19,
12116                                p_sup_atp_info_rec.instance_id col20,
12117                                l_null_num col21,
12118                                TRANSACTION_ID col22,
12119                                l_null_num col23,
12120             		   -- performance dsting rearrange signs to get rid of multiply times - 1
12121                                (NVL(p.implement_quantity,0) - p.new_order_quantity) col24,
12122                                --Fixing as a part of bug3709707 adding trunc so that 2 column are not seen in HP
12123                                TRUNC(Decode(p_sup_atp_info_rec.sup_cap_type,
12124                                                 1, p.new_ship_date,
12125                                                 p.new_dock_date)) col25, -- For ship_rec_cal
12126                                l_null_num col26,
12127                                --bug 4493399: show transaction id for PO
12128                                --p.order_number col27,
12129                                DECODE(p.ORDER_TYPE, 5, to_char(p.TRANSACTION_ID), p.order_number) col27,
12130                                l_null_num col28,
12131                                l_null_num col29,
12132             		   l_sysdate,
12133             		   FND_GLOBAL.User_ID,
12134             		   l_sysdate,
12135             		   FND_GLOBAL.User_ID,
12136             		   FND_GLOBAL.User_ID
12137                      -- Supplier Capacity (SCLT) Changes Begin
12138                      FROM   msc_supplies p
12139                      WHERE  (p.order_type IN (5, 2, 60)
12140                              --include purchase orders based on profile option
12141                              OR (MSC_ATP_REQ.G_PURCHASE_ORDER_PREFERENCE = MSC_ATP_REQ.G_PROMISE_DATE
12142                                  AND p.order_type = 1 AND p.promised_date IS NULL))
12143                      -- Supplier Capacity (SCLT) Accumulation Ignore Purchase Orders
12144                      -- WHERE  p.order_type IN (5, 1, 2)
12145                      AND    p.plan_id = p_sup_atp_info_rec.plan_id
12146                      AND    p.sr_instance_id = p_sup_atp_info_rec.instance_id
12147                      AND    p.inventory_item_id = p_sup_atp_info_rec.inventory_item_id
12148                -- 1214694      AND    p.organization_id = p_sup_atp_info_rec.organization_id
12149                      AND    p.supplier_id  = p_sup_atp_info_rec.supplier_id
12150                      AND    NVL(p.supplier_site_id, -1) = NVL(p_sup_atp_info_rec.supplier_site_id, -1)
12151                      AND    trunc(Decode(p_sup_atp_info_rec.sup_cap_type, 1, p.new_ship_date,p.new_dock_date)) --4055719 --4135752
12152                             <= trunc(least(p_sup_atp_info_rec.last_cap_date,l_cutoff_date)) --4135752
12153                             -- Exclude Cancelled Supplies 2460645
12154                      AND    NVL(P.DISPOSITION_STATUS_TYPE, 1) <> 2);
12155                      -- Supplier Capacity (SCLT) Changes End
12156 
12157                     -- dsting: removed 'order by col 25'
12158                 END IF; -- IF l_check_cap_model_flag = 1 THEN
12159 
12160                 -- for period ATP
12161                 IF PG_DEBUG in ('Y', 'C') THEN
12162                     msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'after inserting into msc_atp_sd_details_temp');
12163                     msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'rows processed: ' || SQL%ROWCOUNT);
12164                 END IF;
12165 
12166                 MSC_ATP_PROC.get_period_data_from_SD_temp(x_atp_period);
12167 
12168                 x_atp_period.Cumulative_Quantity := x_atp_period.Period_Quantity;
12169                 IF PG_DEBUG in ('Y', 'C') THEN
12170                     msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'before atp_consume');
12171                 END IF;
12172                 -- do the accumulation
12173                 -- 1487804
12174                 -- atp_consume(x_atp_period.Cumulative_Quantity, m);
12175                 MSC_ATP_PROC.atp_consume(x_atp_period.Cumulative_Quantity,
12176                     x_atp_period.Cumulative_Quantity.COUNT);
12177 
12178                 IF PG_DEBUG in ('Y', 'C') THEN
12179                     msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'after atp_consume');
12180                 END IF;
12181 
12182                 /* Cum drop issue changes begin*/
12183                 MSC_AATP_PROC.Atp_Remove_Negatives(x_atp_period.Cumulative_Quantity, l_return_status);
12184                 IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
12185                         IF PG_DEBUG in ('Y', 'C') THEN
12186                                 msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'Error occured in procedure Atp_Remove_Negatives');
12187                         END IF;
12188                         RAISE FND_API.G_EXC_ERROR;
12189                 END IF;
12190                 /* Cum drop issue changes end*/
12191 
12192                 l_atp_period_tab := x_atp_period.Period_Start_Date;
12193                 l_atp_qty_tab := x_atp_period.Cumulative_Quantity;
12194 
12195 
12196             END IF; -- p_sup_atp_info_rec.insert_flag <> 0
12197 
12198             --4055719
12199 
12200                 l_atp_period_tab.EXTEND;
12201                 l_atp_qty_tab.EXTEND;
12202                 i:= l_atp_period_tab.COUNT;
12203                 IF PG_DEBUG in ('Y', 'C') THEN
12204                    msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'l_plan_start_date := ' || l_plan_start_date);
12205                 END IF;
12206 
12207                 -- ship_rec_cal
12208                 l_atp_period_tab(i) := l_last_cap_next_date;
12209                 l_atp_qty_tab(i) := MSC_ATP_PVT.INFINITE_NUMBER;
12210 
12211                 IF PG_DEBUG in ('Y', 'C') THEN
12212                    msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'l_plan_start_date1 := ' || l_plan_start_date);
12213                 END IF;
12214 
12215                 IF (NVL(p_sup_atp_info_rec.insert_flag, 0) <> 0) THEN
12216 
12217                    -- add one more entry to indicate infinite time fence date
12218                    -- and quantity.
12219                    MSC_SATP_FUNC.Extend_Atp_Period(x_atp_period, x_return_status);
12220                    i:= x_atp_period.level.COUNT;
12221 
12222                    x_atp_period.Level(i) := p_sup_atp_info_rec.level;
12223                    x_atp_period.Identifier(i) := p_sup_atp_info_rec.identifier;
12224                    x_atp_period.Scenario_Id(i) := p_sup_atp_info_rec.scenario_id;
12225                    x_atp_period.Pegging_Id(i) := NULL;
12226                    x_atp_period.End_Pegging_Id(i) := NULL;
12227 
12228                    x_atp_period.Supplier_Id(i) := p_sup_atp_info_rec.supplier_id;
12229                    x_atp_period.Supplier_Site_Id(i) := p_sup_atp_info_rec.supplier_site_id;
12230                    x_atp_period.Organization_id(i) := p_sup_atp_info_rec.organization_id;
12231 
12232                    -- ship_rec_cal
12233                    x_atp_period.Period_Start_Date(i) := l_last_cap_next_date;
12234 
12235                    x_atp_period.Total_Supply_Quantity(i) := MSC_ATP_PVT.INFINITE_NUMBER;
12236                    x_atp_period.Total_Demand_Quantity(i) := 0;
12237                    x_atp_period.Period_Quantity(i) := MSC_ATP_PVT.INFINITE_NUMBER;
12238                    x_atp_period.Cumulative_Quantity(i) := MSC_ATP_PVT.INFINITE_NUMBER;
12239 
12240                 END IF;
12241 
12242             --=======================================================================================================
12243             -- ship_rec_cal changes end
12244             --=======================================================================================================
12245 
12246         ELSE --  (G_ALLOCATED_ATP = 'N')
12247             -- we are using allocated atp
12248             MSC_AATP_PVT.Supplier_Alloc_Cum_Atp(p_sup_atp_info_rec,
12249                                MSC_ATP_PVT.G_ORDER_LINE_ID,
12250                                l_requested_date,
12251                                l_atp_info,
12252                                x_atp_period,
12253                                x_atp_supply_demand);
12254 
12255             --4055719, this piece of code is moved from MSC_AATP_PVT.Supplier_Alloc_Cum_Atp to here.
12256             -- also l_last_cap_next_date is added instead of l_infinite_time_fence_date.
12257             IF l_last_cap_next_date IS NOT NULL THEN
12258                -- add one more entry to indicate infinite time fence date
12259                -- and quantity.
12260                l_atp_info.atp_qty.EXTEND;
12261                l_atp_info.atp_period.EXTEND;
12262                --- bug 1657855, remove support for min alloc
12263                l_atp_info.limit_qty.EXTEND;
12264 
12265                i := l_atp_info.atp_qty.COUNT;
12266                l_atp_info.atp_period(i) := l_last_cap_next_date;
12267                l_atp_info.atp_qty(i) := MSC_ATP_PVT.INFINITE_NUMBER;
12268                ---x_atp_info.limit_qty(i) := MSC_ATP_PVT.INFINITE_NUMBER;
12269 
12270 
12271               IF NVL(p_sup_atp_info_rec.insert_flag, 0) <> 0 THEN
12272                  -- add one more entry to indicate infinite time fence date
12273                  -- and quantity.
12274 
12275                  x_atp_period.Cumulative_Quantity := l_atp_info.atp_qty;
12276 
12277                  j := x_atp_period.Level.COUNT;
12278                  MSC_SATP_FUNC.Extend_Atp_Period(x_atp_period, l_return_status);
12279                  j := j + 1;
12280                  IF j > 1 THEN
12281                     --x_atp_period.Period_End_Date(j-1) := l_infinite_time_fence_date -1; --4055719
12282                     x_atp_period.Period_End_Date(j-1) := l_last_cap_next_date -1;
12283                     x_atp_period.Identifier1(j) := x_atp_period.Identifier1(j-1);
12284                     x_atp_period.Identifier2(j) := x_atp_period.Identifier2(j-1);
12285                  END IF;
12286 
12287                  x_atp_period.Level(j) := p_sup_atp_info_rec.level;
12288                  x_atp_period.Identifier(j) := MSC_ATP_PVT.G_ORDER_LINE_ID;
12289                  x_atp_period.Scenario_Id(j) := p_sup_atp_info_rec.scenario_id;
12290                  x_atp_period.Pegging_Id(j) := NULL;
12291                  x_atp_period.End_Pegging_Id(j) := NULL;
12292                  x_atp_period.Supplier_Id(j) := p_sup_atp_info_rec.supplier_id;
12293                  x_atp_period.Supplier_site_id(j) := p_sup_atp_info_rec.supplier_site_id;
12294                  x_atp_period.Organization_id(j) := p_sup_atp_info_rec.organization_id;
12295                  --x_atp_period.Period_Start_Date(j) := l_infinite_time_fence_date; --4055719
12296                  x_atp_period.Period_Start_Date(j) := l_last_cap_next_date;
12297                  x_atp_period.Total_Supply_Quantity(j) := MSC_ATP_PVT.INFINITE_NUMBER;
12298                  x_atp_period.Total_Demand_Quantity(j) := 0;
12299                  x_atp_period.Period_Quantity(j) := MSC_ATP_PVT.INFINITE_NUMBER;
12300                  x_atp_period.Cumulative_Quantity(j) := MSC_ATP_PVT.INFINITE_NUMBER;
12301               END IF;
12302             END IF;
12303 
12304             l_atp_period_tab := l_atp_info.atp_period;
12305             l_atp_qty_tab := l_atp_info.atp_qty;
12306 
12307         END IF; -- end of G_ALLOCATED_ATP
12308 
12309         --4055719 , commented this piece of code because of following reasons
12310         -- 1. This code will lead to adding of infinite date/qty record twice
12311         -- 2. Seperated code is added to allocated/unallocated cases (we cannot join the
12312         --    code because appending of records are slightly diff in both cases.
12313 
12314         /* Removed redundant code as l_last_cap_date does not anyway get used after this point
12315            Done with Enforce Pur LT changes
12316         --sup_cap chnages
12317         --first get the next working from the last day on which capacity is defined.
12318         IF PG_DEBUG in ('Y', 'C') THEN
12319             msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'l_last_cap_date := ' || l_last_cap_date);
12320         END IF;
12321         l_last_cap_date :=  MSC_CALENDAR.DATE_OFFSET(p_sup_atp_info_rec.organization_id,
12322                                                      p_sup_atp_info_rec.instance_id,
12323                                                      1,
12324                                                      l_last_cap_date,
12325                                                      1);
12326         IF PG_DEBUG in ('Y', 'C') THEN
12327            msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'l_last_cap_date after offset := ' || l_last_cap_date);
12328         END IF;
12329         */
12330 
12331         -- add one more entry to indicate sysdate
12332         -- and infinite quantity.
12333 
12334         --4055719
12335         /*
12336         l_atp_period_tab.EXTEND;
12337         l_atp_qty_tab.EXTEND;
12338         i:= l_atp_period_tab.COUNT;
12339         IF PG_DEBUG in ('Y', 'C') THEN
12340              msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'l_plan_start_date := ' || l_plan_start_date);
12341         END IF;
12342 
12343         -- ship_rec_cal
12344         l_atp_period_tab(i) := l_last_cap_next_date;
12345         l_atp_qty_tab(i) := MSC_ATP_PVT.INFINITE_NUMBER;
12346 
12347 
12348         IF (NVL(p_sup_atp_info_rec.insert_flag, 0) <> 0) THEN
12349 
12350            -- add one more entry to indicate infinite time fence date
12351            -- and quantity.
12352            MSC_SATP_FUNC.Extend_Atp_Period(x_atp_period, x_return_status);
12353            i:= x_atp_period.level.COUNT;
12354 
12355            x_atp_period.Level(i) := p_sup_atp_info_rec.level;
12356            x_atp_period.Identifier(i) := p_sup_atp_info_rec.identifier;
12357            x_atp_period.Scenario_Id(i) := p_sup_atp_info_rec.scenario_id;
12358            x_atp_period.Pegging_Id(i) := NULL;
12359            x_atp_period.End_Pegging_Id(i) := NULL;
12360 
12361            x_atp_period.Supplier_Id(i) := p_sup_atp_info_rec.supplier_id;
12362            x_atp_period.Supplier_Site_Id(i) := p_sup_atp_info_rec.supplier_site_id;
12363            x_atp_period.Organization_id(i) := p_sup_atp_info_rec.organization_id;
12364 
12365            -- ship_rec_cal
12366            x_atp_period.Period_Start_Date(i) := l_last_cap_next_date;
12367 
12368            x_atp_period.Total_Supply_Quantity(i) := MSC_ATP_PVT.INFINITE_NUMBER;
12369            x_atp_period.Total_Demand_Quantity(i) := 0;
12370            x_atp_period.Period_Quantity(i) := MSC_ATP_PVT.INFINITE_NUMBER;
12371            x_atp_period.Cumulative_Quantity(i) := MSC_ATP_PVT.INFINITE_NUMBER;
12372 
12373         END IF; */
12374 
12375    END IF;  -- l_capacity_defined = 0
12376 
12377     i := l_atp_period_tab.COUNT;
12378 
12379     IF i = 0 THEN
12380       -- need to add error message
12381         IF PG_DEBUG in ('Y', 'C') THEN
12382            msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'No rows in cursor!!!');
12383         END IF;
12384         RAISE NO_DATA_FOUND;
12385     END IF;
12386 
12387     FOR i in 1..l_atp_period_tab.COUNT LOOP
12388         IF PG_DEBUG in ('Y', 'C') THEN
12389            msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'Date '||l_atp_period_tab(i)||' Qty '||
12390                                   l_atp_qty_tab(i));
12391         END IF;
12392     END LOOP;
12393 
12394 
12395     --bug 2341075: Capacity before sysdate should not be considered
12396     --- we find out how much is available before sydate and reduce it from cumulative qty
12397 
12398     -- Rewrite the l_qty_before_sysdate logic as part of ship_rec_cal changes for better performance
12399     IF (l_atp_period_tab(1) >= l_sysdate OR l_atp_period_tab(l_atp_period_tab.COUNT) <= l_sysdate) THEN
12400 		l_qty_before_sysdate := 0;
12401     ELSE
12402 		FOR i in 1..l_atp_period_tab.COUNT LOOP
12403 			-- For loop will never reach COUNT as that case has already been handled above.
12404 			IF (l_atp_period_tab(i) <  l_sysdate AND l_atp_period_tab(i+1) >= l_sysdate) THEN
12405 				l_qty_before_sysdate := l_atp_qty_tab(i);
12406 				EXIT;
12407 			END IF;
12408 		END LOOP;
12409     END IF;
12410     l_qty_before_sysdate := GREATEST(l_qty_before_sysdate, 0);
12411 
12412     IF PG_DEBUG in ('Y', 'C') THEN
12413        msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'l_qty_before_sysdate := ' || l_qty_before_sysdate);
12414     END IF;
12415 
12416 
12417 	-- we use this l_atp_requested_date to do the search
12418     l_atp_requested_date := GREATEST(l_requested_date, l_sysdate); -- Change for ship_rec_cal
12419 
12420     IF PG_DEBUG in ('Y', 'C') THEN
12421        msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'l_atp_requested_date := ' || l_atp_requested_date);
12422     END IF;
12423 
12424     IF (l_atp_requested_date < l_atp_period_tab(1)) THEN
12425             -- let say the first period is on Day5 but your
12426             -- request in on Day2.  for bug 948863
12427             p_sup_atp_info_rec.requested_date_quantity := 0;
12428             FOR k IN 1..l_atp_period_tab.COUNT LOOP
12429                 IF (l_atp_qty_tab(k) >= p_sup_atp_info_rec.quantity_ordered)
12430                     AND l_atp_period_tab(k) >= MSC_ATP_PVT.G_PTF_DATE THEN      -- Bug 3782472 - Added PTF check
12431                     p_sup_atp_info_rec.atp_date_quantity_this_level := l_atp_qty_tab(k);
12432                     p_sup_atp_info_rec.atp_date_this_level := l_atp_period_tab(k);
12433                     EXIT;
12434                 ELSIF (l_atp_qty_tab(k) >= p_sup_atp_info_rec.quantity_ordered)
12435                     AND (l_atp_period_tab.COUNT = k
12436                         -- Bug 3862224, handled the case where ptf_date has some supply/demand activity, removed equality check.
12437                         --OR l_atp_period_tab(k+1) >= MSC_ATP_PVT.G_PTF_DATE) THEN      -- Bug 3782472 - Added PTF check
12438                         OR l_atp_period_tab(k+1) > MSC_ATP_PVT.G_PTF_DATE) THEN  -- Bug 3782472 - Added PTF check
12439                     p_sup_atp_info_rec.atp_date_quantity_this_level := l_atp_qty_tab(k);
12440                     p_sup_atp_info_rec.atp_date_this_level := MSC_ATP_PVT.G_PTF_DATE;
12441                     EXIT;
12442                 END IF;
12443             END LOOP; -- end of k loop
12444     ELSE
12445 
12446 
12447         FOR j IN 1..l_atp_period_tab.COUNT LOOP
12448 
12449             -- Please state reason for the else condition here
12450             -- the reason that we need this else condition is the following
12451             -- let say the last record in the bucket is Day5, and request
12452             -- date is Day10.  So the bucket that the the request date is
12453             -- falling into is Day5. So we should use Day5's quantity
12454             -- as the quantity for Day10. By setting l_next_period this way,
12455             -- we make sure we are using the right bucket to get
12456             -- request date quantuty.
12457 
12458             IF j < l_atp_period_tab.LAST THEN
12459                 l_next_period := l_atp_period_tab(j+1);
12460             ELSE
12461                 l_next_period := l_atp_requested_date + 1;
12462             END IF;
12463 
12464             IF ((l_atp_period_tab(j) <= l_atp_requested_date) and
12465                         (l_next_period > l_atp_requested_date)) THEN
12466 
12467                 IF (l_pre_process_date IS NOT NULL and l_pre_process_date < l_sysdate)
12468                    or (l_pre_process_date IS NULL and
12469                        l_requested_date < l_atp_requested_date) THEN
12470                        -- Bug 3828469 - Removed the regression introduced in 3782472
12471                        -- l_requested_date < GREATEST(MSC_ATP_PVT.G_PTF_DATE,l_atp_requested_date)) THEN -- Bug 3782472 - Added PTF check
12472                     p_sup_atp_info_rec.requested_date_quantity := 0;
12473 
12474                     IF PG_DEBUG in ('Y', 'C') THEN
12475                        msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'inside p_sup_atp_info_rec.requested_date_quantity 0 = '||
12476                       p_sup_atp_info_rec.requested_date_quantity);
12477                     END IF;
12478                 ELSE
12479                     ---bug 2341075: availability should not include what is available before sysdate
12480                     p_sup_atp_info_rec.requested_date_quantity := l_atp_qty_tab(j) - l_qty_before_sysdate;
12481                 END IF;
12482 
12483 
12484                 IF PG_DEBUG in ('Y', 'C') THEN
12485                    msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'p_sup_atp_info_rec.requested_date_quantity: '|| to_char(p_sup_atp_info_rec.requested_date_quantity));
12486                 END IF;
12487                 --  now find the atp_date_quantity and atp_date at this level
12488                 ---bug 2341075: Cum Qty should not include cum qty before sysdate
12489                 IF (l_atp_qty_tab(j) - l_qty_before_sysdate) >= p_sup_atp_info_rec.quantity_ordered
12490                     AND l_atp_requested_date >= MSC_ATP_PVT.G_PTF_DATE THEN      -- Bug 3782472 - Added PTF check
12491 
12492                     p_sup_atp_info_rec.atp_date_quantity_this_level := l_atp_qty_tab(j) - l_qty_before_sysdate;
12493                     p_sup_atp_info_rec.atp_date_this_level := l_atp_requested_date;
12494 
12495                     IF PG_DEBUG in ('Y', 'C') THEN
12496                        msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'p_sup_atp_info_rec.atp_date_this_level: '|| to_char(p_sup_atp_info_rec.atp_date_this_level));
12497                        msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'p_sup_atp_info_rec.atp_date_quantity_this_level: '|| to_char(p_sup_atp_info_rec.atp_date_quantity_this_level));
12498                     END IF;
12499 
12500                 ELSE
12501                     IF j = l_atp_period_tab.COUNT THEN
12502                         p_sup_atp_info_rec.atp_date_quantity_this_level := NULL;
12503                         p_sup_atp_info_rec.atp_date_this_level := NULL;
12504 
12505                     ELSE
12506                         FOR k IN j+1..l_atp_period_tab.COUNT LOOP
12507                             ---bug 2341075: exclude qty before sysdate
12508                             IF ((l_atp_qty_tab(k)- l_qty_before_sysdate) >= p_sup_atp_info_rec.quantity_ordered)
12509                                 AND l_atp_period_tab(k) >= MSC_ATP_PVT.G_PTF_DATE THEN      -- Bug 3782472 - Added PTF check
12510                                 p_sup_atp_info_rec.atp_date_quantity_this_level := l_atp_qty_tab(k) - l_qty_before_sysdate;
12511                                 p_sup_atp_info_rec.atp_date_this_level := l_atp_period_tab(k);
12512                                 EXIT;
12513                             ELSIF ((l_atp_qty_tab(k)- l_qty_before_sysdate) >= p_sup_atp_info_rec.quantity_ordered)
12514                                 AND (l_atp_period_tab.COUNT = k
12515                                         -- Bug 3862224, handled the case where ptf_date has some supply/demand activity, removed equality check
12516                                         --OR l_atp_period_tab(k+1) >= MSC_ATP_PVT.G_PTF_DATE) THEN
12517                                         OR l_atp_period_tab(k+1) > MSC_ATP_PVT.G_PTF_DATE) THEN
12518                                 p_sup_atp_info_rec.atp_date_quantity_this_level := l_atp_qty_tab(k) - l_qty_before_sysdate;
12519                                 p_sup_atp_info_rec.atp_date_this_level := MSC_ATP_PVT.G_PTF_DATE;
12520                                 EXIT;
12521                             END IF;
12522                         END LOOP; -- end of k loop
12523                     END IF; -- end if j = l_atp_period_tab.COUNT
12524                 END IF; -- end if  l_atp_qty_tab(j) >=p_sup_atp_info_rec.quantity_ordered
12525                 EXIT;
12526             END IF; -- end if we find the bucket
12527         END LOOP; -- end j loop
12528     END IF;
12529 
12530     IF PG_DEBUG in ('Y', 'C') THEN
12531        msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'in supplier, count = '||x_atp_supply_demand.supplier_id.count);
12532        msc_sch_wb.atp_debug('***** End Get_Supplier_Atp_Info Procedure *****');
12533     END IF;
12534 
12535 EXCEPTION
12536 
12537     WHEN NO_DATA_FOUND THEN
12538         p_sup_atp_info_rec.requested_date_quantity := 0.0;
12539         x_return_status := FND_API.G_RET_STS_ERROR;
12540 END Get_Supplier_Atp_Info;
12541 
12542 
12543 PROCEDURE Get_Transport_Cap_Atp_Info (
12544   p_plan_id				IN    NUMBER,
12545   p_from_organization_id                IN    NUMBER,
12546   p_to_organization_id                  IN    NUMBER,
12547   p_ship_method                         IN    VARCHAR2,
12548   p_inventory_item_id                   IN    NUMBER,
12549   p_source_org_instance_id		IN    NUMBER,
12550   p_dest_org_instance_id		IN    NUMBER,
12551   p_requested_date                      IN    DATE,
12552   p_quantity_ordered                    IN    NUMBER,
12553   p_insert_flag                         IN    NUMBER,
12554   p_level				IN    NUMBER,
12555   p_scenario_id				IN    NUMBER,
12556   p_identifier				IN    NUMBER,
12557   p_parent_pegging_id			IN    NUMBER,
12558   x_requested_date_quantity             OUT   NoCopy NUMBER,
12559   x_atp_date_this_level                 OUT   NoCopy DATE,
12560   x_atp_date_quantity_this_level        OUT   NoCopy NUMBER,
12561   x_atp_period                          OUT   NoCopy MRP_ATP_PUB.ATP_Period_Typ,
12562   x_atp_supply_demand                   OUT   NoCopy MRP_ATP_PUB.ATP_Supply_Demand_Typ,
12563   x_return_status                       OUT   NoCopy VARCHAR2
12564 )
12565 IS
12566 
12567 i                               PLS_INTEGER := 1;
12568 l_requested_date                DATE;
12569 l_unit_weight			NUMBER;
12570 l_unit_volume			NUMBER;
12571 l_available_quantity		NUMBER;
12572 l_atp_period_tab                MRP_ATP_PUB.date_arr:=MRP_ATP_PUB.date_arr();
12573 l_atp_qty_tab                   MRP_ATP_PUB.number_arr := MRP_ATP_PUB.number_arr();
12574 l_atp_qty_tab2                  MRP_ATP_PUB.number_arr := MRP_ATP_PUB.number_arr();
12575 l_atp_period                    MRP_ATP_PUB.ATP_Period_Typ;
12576 l_atp_period2                    MRP_ATP_PUB.ATP_Period_Typ;
12577 l_atp_supply_demand		MRP_ATP_PUB.ATP_Supply_Demand_Typ;
12578 l_atp_supply_demand2             MRP_ATP_PUB.ATP_Supply_Demand_Typ;
12579 l_null_atp_period		MRP_ATP_PUB.ATP_Period_Typ;
12580 l_null_atp_supply_demand	MRP_ATP_PUB.ATP_Supply_Demand_Typ;
12581 m				NUMBER;
12582 k				NUMBER;
12583 l_item_weight_qty		NUMBER;
12584 l_item_volume_qty		NUMBER;
12585 l_atp_date_weight		NUMBER;
12586 l_atp_date_volume		NUMBER;
12587 l_pegging_rec                   mrp_atp_details_temp%ROWTYPE;
12588 l_pegging_id			NUMBER;
12589 l_vol_demand_pegging_id	        NUMBER;
12590 l_wt_demand_pegging_id 		NUMBER;
12591 --INFINITE_NUMBER                 CONSTANT NUMBER := 1.0e+10;
12592 
12593 BEGIN
12594 
12595   IF PG_DEBUG in ('Y', 'C') THEN
12596      msc_sch_wb.atp_debug('Get_Transport_Cap_Atp_Info: ' || 'inside get_TRANSPORT_info ');
12597   END IF;
12598 
12599   l_null_atp_period := x_atp_period;
12600   l_null_atp_supply_demand := x_atp_supply_demand;
12601 
12602   IF PG_DEBUG in ('Y', 'C') THEN
12603      msc_sch_wb.atp_debug('Get_Transport_Cap_Atp_Info: ' || 'p_insert_flag = '||p_insert_flag);
12604   END IF;
12605 
12606   -- initialize API return status to success
12607   x_return_status := FND_API.G_RET_STS_SUCCESS;
12608 
12609   l_requested_date := trunc(p_requested_date);
12610 
12611   -- Initialize the record with null
12612 
12613   l_atp_period := l_null_atp_period;
12614   l_atp_supply_demand := l_null_atp_supply_demand;
12615   l_atp_period2 := l_null_atp_period;
12616   l_atp_supply_demand2 := l_null_atp_supply_demand;
12617 
12618   -- Planning will be populating the 'weight_capacitiy_used' and
12619   -- 'volume_capacity_used' fields in msc_supplies. For now, we need
12620   -- to multiply the quantitiy by the unit volume and unit capacity
12621   -- to obtain capacity used.
12622 
12623   SELECT NVL(unit_weight,1), NVL(unit_volume,1)
12624   INTO   l_unit_weight, l_unit_volume
12625   FROM   msc_system_items
12626   WHERE  plan_id = p_plan_id
12627   AND    organization_id = p_to_organization_id
12628   AND    inventory_item_id = p_inventory_item_id
12629   AND    sr_instance_id = p_dest_org_instance_id;
12630 
12631   IF PG_DEBUG in ('Y', 'C') THEN
12632      msc_sch_wb.atp_debug('Get_Transport_Cap_Atp_Info: ' || 'the unit weight is: '||to_char(l_unit_weight));
12633      msc_sch_wb.atp_debug('Get_Transport_Cap_Atp_Info: ' || 'the unit volume is: '||to_char(l_unit_volume));
12634   END IF;
12635 
12636   SELECT  l_date, SUM(weight), SUM(volume)
12637   BULK COLLECT INTO
12638       l_atp_period_tab,
12639       l_atp_qty_tab,
12640       l_atp_qty_tab2
12641   FROM (
12642     SELECT c.calendar_date l_date,
12643              s.weight_capacity weight,
12644 	     s.volume_capacity volume
12645       FROM   msc_calendar_dates c,
12646              msc_interorg_ship_methods s,
12647              msc_trading_partners tp,
12648 	     msc_plans p
12649       WHERE  s.plan_id = p_plan_id
12650       AND    s.from_organization_id = p_from_organization_id
12651       AND    s.to_organization_id = p_to_organization_id
12652       AND    s.ship_method = p_ship_method
12653       AND    s.sr_instance_id = p_source_org_instance_id
12654       AND    s.sr_instance_id2 = p_dest_org_instance_id
12655       AND    s.from_organization_id = tp.sr_tp_id
12656       AND    tp.sr_instance_id = NVL(s.sr_instance_id, s.sr_instance_id2)
12657       AND    c.calendar_date BETWEEN trunc(SYSDATE) and trunc(p.curr_cutoff_date)   -- to_date changed to trunc to avoid GSCC error
12658       AND    c.calendar_code = tp.calendar_code
12659       AND    c.exception_set_id = tp.calendar_exception_set_id
12660       AND    p.plan_id = p_plan_id
12661       UNION ALL
12662       SELECT sup.new_schedule_date l_date,
12663              -1*(sup.new_order_quantity)*l_unit_weight weight,
12664 	     -1*(sup.new_order_quantity)*l_unit_volume  volume
12665       FROM   msc_supplies sup
12666       WHERE  sup.plan_id = p_plan_id
12667       AND    sup.organization_id = p_to_organization_id
12668       AND    sup.sr_instance_id = p_dest_org_instance_id
12669       AND    sup.source_organization_id is not null
12670       AND    sup.source_organization_id = p_from_organization_id
12671       AND    sup.source_sr_instance_id = p_source_org_instance_id
12672       AND    sup.ship_method = p_ship_method
12673       AND    sup.inventory_item_id = p_inventory_item_id
12674       AND    sup.transaction_id <>     (SELECT identifier3
12675 					FROM   mrp_atp_details_temp
12676 					WHERE  record_type = 3
12677 					AND    pegging_id = p_parent_pegging_id
12678                                         AND    session_id = MSC_ATP_PVT.G_SESSION_ID
12679 				       ))
12680       GROUP BY l_date
12681       ORDER BY l_date;
12682 
12683       i := l_atp_period_tab.COUNT;
12684 
12685       IF i = 0 THEN
12686         -- need to add error message
12687         IF PG_DEBUG in ('Y', 'C') THEN
12688            msc_sch_wb.atp_debug('Get_Transport_Cap_Atp_Info: ' || 'No rows in cursor!!!');
12689         END IF;
12690         RAISE NO_DATA_FOUND;
12691       END IF;
12692 
12693       -- do accumulation for transportation capacity
12694       MSC_ATP_PROC.atp_consume(l_atp_qty_tab, i);
12695       MSC_ATP_PROC.atp_consume(l_atp_qty_tab2, i);
12696 
12697       IF PG_DEBUG in ('Y', 'C') THEN
12698          msc_sch_wb.atp_debug('Get_Transport_Cap_Atp_Info: ' || 'just before printing values');
12699       END IF;
12700 
12701     FOR k IN 1..i LOOP
12702       IF PG_DEBUG in ('Y', 'C') THEN
12703          msc_sch_wb.atp_debug('Get_Transport_Cap_Atp_Info: ' || 'date is: '||to_char(l_atp_period_tab(k)));
12704          msc_sch_wb.atp_debug('Get_Transport_Cap_Atp_Info: ' || 'weight is: '||to_char(l_atp_qty_tab(k)));
12705          msc_sch_wb.atp_debug('Get_Transport_Cap_Atp_Info: ' || 'volume is: '||to_char(l_atp_qty_tab2(k)));
12706       END IF;
12707 
12708     END LOOP;
12709 
12710     -- find the requested date atp quantity
12711 
12712     FOR j IN 1..i LOOP
12713 
12714         IF ((l_atp_period_tab(j) <= l_requested_date) and
12715             (l_atp_period_tab(j+1) > l_requested_date)) THEN
12716 
12717             -- Convert volume and weight capacity to units
12718 
12719             l_item_weight_qty := l_atp_qty_tab(j)/l_unit_weight;
12720             l_item_volume_qty := l_atp_qty_tab2(j)/l_unit_volume;
12721 
12722             -- Find out volume or weight capacity is constraining to
12723             -- determine quantity for request date
12724 
12725             IF l_item_volume_qty > l_item_weight_qty THEN
12726               x_requested_date_quantity := l_item_weight_qty;
12727             ELSE
12728               x_requested_date_quantity := l_item_volume_qty;
12729             END IF;
12730 
12731             IF PG_DEBUG in ('Y', 'C') THEN
12732                msc_sch_wb.atp_debug('Get_Transport_Cap_Atp_Info: ' || 'the date is: '||to_char(l_atp_period_tab(j)));
12733                msc_sch_wb.atp_debug('Get_Transport_Cap_Atp_Info: ' || 'the quantity is: '||to_char(x_requested_date_quantity));
12734             END IF;
12735 
12736             --  now find the atp_date_quantity and atp_date at this level
12737             IF x_requested_date_quantity >= p_quantity_ordered THEN
12738                 x_atp_date_quantity_this_level := x_requested_date_quantity;
12739                 x_atp_date_this_level := p_requested_date;
12740 		l_atp_date_weight := l_atp_qty_tab(j);
12741 		l_atp_date_volume := l_atp_qty_tab2(j);
12742 
12743                 IF PG_DEBUG in ('Y', 'C') THEN
12744                    msc_sch_wb.atp_debug('Get_Transport_Cap_Atp_Info: ' || 'quantity is enough.');
12745                 END IF;
12746 
12747    		k := j;
12748             ELSE
12749 
12750                 IF PG_DEBUG in ('Y', 'C') THEN
12751                    msc_sch_wb.atp_debug('Get_Transport_Cap_Atp_Info: ' || 'quantity is not enough.');
12752                 END IF;
12753 
12754                 FOR k IN j+1..i LOOP
12755 		  -- Convert volume and weight capacity in units
12756 
12757                   l_item_weight_qty := l_atp_qty_tab(k)/l_unit_weight;
12758                   l_item_volume_qty := l_atp_qty_tab2(k)/l_unit_volume;
12759 
12760                     IF l_item_volume_qty > l_item_weight_qty THEN
12761                       l_available_quantity := l_item_weight_qty;
12762                     ELSE
12763                       l_available_quantity := l_item_volume_qty;
12764                     END IF;
12765 
12766                     IF PG_DEBUG in ('Y', 'C') THEN
12767                        msc_sch_wb.atp_debug('Get_Transport_Cap_Atp_Info: ' || 'Date is: '||to_char(l_atp_period_tab(k)));
12768                        msc_sch_wb.atp_debug('Get_Transport_Cap_Atp_Info: ' || 'Quantity available is: '||to_char(l_available_quantity));
12769                     END IF;
12770 
12771                     IF (l_available_quantity >= p_quantity_ordered) THEN
12772                         x_atp_date_quantity_this_level := l_available_quantity;
12773                         x_atp_date_this_level := l_atp_period_tab(k);
12774 
12775 			l_atp_date_weight := l_atp_qty_tab(k);
12776 	                l_atp_date_volume := l_atp_qty_tab2(k);
12777 
12778                         IF PG_DEBUG in ('Y', 'C') THEN
12779                            msc_sch_wb.atp_debug('Get_Transport_Cap_Atp_Info: ' || 'done');
12780                            msc_sch_wb.atp_debug('Get_Transport_Cap_Atp_Info: ' || 'quantity: '||to_char(x_atp_date_quantity_this_level));
12781                            msc_sch_wb.atp_debug('Get_Transport_Cap_Atp_Info: ' || 'date: '||to_char(x_atp_date_this_level));
12782                         END IF;
12783                     END IF;
12784                 END LOOP;
12785             END IF;
12786         END IF;
12787     END LOOP;
12788 
12789     IF NVL(p_insert_flag,0) <> 0 THEN -- p_insert_flag
12790       -- add pegging info for weight demand
12791 
12792     l_pegging_rec.session_id := MSC_ATP_PVT.G_SESSION_ID;
12793     l_pegging_rec.order_line_id:= MSC_ATP_PVT.G_ORDER_LINE_ID;
12794     l_pegging_rec.parent_pegging_id:= p_parent_pegging_id;
12795     l_pegging_rec.atp_level:= p_level;
12796     l_pegging_rec.from_organization_id := p_from_organization_id;
12797     l_pegging_rec.to_organization_id := p_to_organization_id;
12798     l_pegging_rec.ship_method := p_ship_method;
12799     l_pegging_rec.identifier1 := p_source_org_instance_id;
12800     l_pegging_rec.identifier2 := 'WEIGHT';
12801 --    l_pegging_rec.identifier3 := l_transaction_id;
12802     l_pegging_rec.identifier4 := p_dest_org_instance_id;
12803     l_pegging_rec.scenario_id:= p_scenario_id;
12804     l_pegging_rec.supply_demand_source_type:= 1;
12805     l_pegging_rec.supply_demand_quantity := p_quantity_ordered*l_unit_weight;
12806     l_pegging_rec.weight_capacity := p_quantity_ordered*l_unit_weight;
12807     l_pegging_rec.supply_demand_type:= 1;
12808     l_pegging_rec.supply_demand_date:= l_requested_date;
12809     l_pegging_rec.department_id := NULL;
12810     l_pegging_rec.department_code := NULL;
12811     l_pegging_rec.resource_id := NULL;
12812     l_pegging_rec.resource_code := NULL;
12813     l_pegging_rec.inventory_item_id := NULL;
12814     l_pegging_rec.inventory_item_name := NULL;
12815     l_pegging_rec.supplier_id := NULL;
12816     l_pegging_rec.supplier_name := NULL;
12817     l_pegging_rec.supplier_site_id := NULL;
12818     l_pegging_rec.supplier_site_name := NULL;
12819 
12820 
12821     MSC_ATP_DB_UTILS.Add_Pegging(l_pegging_rec, l_pegging_id);
12822 
12823     l_wt_demand_pegging_id := l_pegging_id;
12824 
12825     -- add pegging info for volume demand
12826 
12827     l_pegging_rec.session_id := MSC_ATP_PVT.G_SESSION_ID;
12828     l_pegging_rec.order_line_id:= MSC_ATP_PVT.G_ORDER_LINE_ID;
12829     l_pegging_rec.parent_pegging_id:= p_parent_pegging_id;
12830     l_pegging_rec.atp_level:= p_level;
12831     l_pegging_rec.from_organization_id := p_from_organization_id;
12832     l_pegging_rec.to_organization_id := p_to_organization_id;
12833     l_pegging_rec.ship_method := p_ship_method;
12834     l_pegging_rec.identifier1 := p_source_org_instance_id;
12835     l_pegging_rec.identifier2 := 'VOLUME';
12836 --    l_pegging_rec.identifier3 := l_transaction_id;
12837     l_pegging_rec.identifier4 := p_dest_org_instance_id;
12838     l_pegging_rec.scenario_id:= p_scenario_id;
12839     l_pegging_rec.supply_demand_source_type:= 1;
12840     l_pegging_rec.supply_demand_quantity := p_quantity_ordered*l_unit_volume;
12841     l_pegging_rec.volume_capacity := p_quantity_ordered*l_unit_volume;
12842     l_pegging_rec.supply_demand_type:= 1;
12843     l_pegging_rec.supply_demand_date:= l_requested_date;
12844     l_pegging_rec.department_id := NULL;
12845     l_pegging_rec.department_code := NULL;
12846     l_pegging_rec.resource_id := NULL;
12847     l_pegging_rec.resource_code := NULL;
12848     l_pegging_rec.inventory_item_id := NULL;
12849     l_pegging_rec.inventory_item_name := NULL;
12850     l_pegging_rec.supplier_id := NULL;
12851     l_pegging_rec.supplier_name := NULL;
12852     l_pegging_rec.supplier_site_id := NULL;
12853     l_pegging_rec.supplier_site_name := NULL;
12854 
12855     MSC_ATP_DB_UTILS.Add_Pegging(l_pegging_rec, l_pegging_id);
12856 
12857     l_vol_demand_pegging_id := l_pegging_id;
12858 
12859     -- add pegging info for weight supply
12860 
12861     l_pegging_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
12862     l_pegging_rec.order_line_id:= MSC_ATP_PVT.G_ORDER_LINE_ID;
12863     l_pegging_rec.parent_pegging_id:= l_wt_demand_pegging_id;
12864     l_pegging_rec.atp_level:= p_level;
12865     l_pegging_rec.from_organization_id := p_from_organization_id;
12866     l_pegging_rec.to_organization_id := p_to_organization_id;
12867     l_pegging_rec.ship_method := p_ship_method;
12868     l_pegging_rec.identifier1 := p_source_org_instance_id;
12869     l_pegging_rec.identifier2 := 'WEIGHT';
12870     l_pegging_rec.identifier3 := -1;
12871     l_pegging_rec.identifier4 := p_dest_org_instance_id;
12872     l_pegging_rec.scenario_id := p_scenario_id;
12873     l_pegging_rec.supply_demand_source_type := MSC_ATP_PVT.ATP;
12874     l_pegging_rec.supply_demand_quantity := l_atp_qty_tab(k);
12875     l_pegging_rec.weight_capacity := l_atp_qty_tab(k);
12876     l_pegging_rec.supply_demand_type := 2;
12877     l_pegging_rec.supply_demand_date := x_atp_date_this_level;
12878     l_pegging_rec.department_id := NULL;
12879     l_pegging_rec.department_code := NULL;
12880     l_pegging_rec.resource_id := NULL;
12881     l_pegging_rec.resource_code := NULL;
12882     l_pegging_rec.inventory_item_id := NULL;
12883     l_pegging_rec.inventory_item_name := NULL;
12884     l_pegging_rec.supplier_id := NULL;
12885     l_pegging_rec.supplier_name := NULL;
12886     l_pegging_rec.supplier_site_id := NULL;
12887     l_pegging_rec.supplier_site_name := NULL;
12888     l_pegging_rec.source_type := 0;
12889 
12890     MSC_ATP_DB_UTILS.Add_Pegging(l_pegging_rec, l_pegging_id);
12891 
12892     -- add pegging info for volume supply
12893 
12894     l_pegging_rec.session_id := MSC_ATP_PVT.G_SESSION_ID;
12895     l_pegging_rec.order_line_id := MSC_ATP_PVT.G_ORDER_LINE_ID;
12896     l_pegging_rec.parent_pegging_id := l_vol_demand_pegging_id;
12897     l_pegging_rec.atp_level := p_level;
12898     l_pegging_rec.from_organization_id := p_from_organization_id;
12899     l_pegging_rec.to_organization_id := p_to_organization_id;
12900     l_pegging_rec.ship_method := p_ship_method;
12901     l_pegging_rec.identifier1 := p_source_org_instance_id;
12902     l_pegging_rec.identifier2 := 'VOLUME';
12903     l_pegging_rec.identifier3 := -1;
12904     l_pegging_rec.identifier4 := p_dest_org_instance_id;
12905     l_pegging_rec.scenario_id := p_scenario_id;
12906     l_pegging_rec.supply_demand_source_type := MSC_ATP_PVT.ATP;
12907     l_pegging_rec.supply_demand_quantity := l_atp_qty_tab2(k);
12908     l_pegging_rec.volume_capacity := l_atp_qty_tab2(k);
12909     l_pegging_rec.supply_demand_type := 2;
12910     l_pegging_rec.supply_demand_date := x_atp_date_this_level;
12911     l_pegging_rec.department_id := NULL;
12912     l_pegging_rec.department_code := NULL;
12913     l_pegging_rec.resource_id := NULL;
12914     l_pegging_rec.resource_code := NULL;
12915     l_pegging_rec.inventory_item_id := NULL;
12916     l_pegging_rec.inventory_item_name := NULL;
12917     l_pegging_rec.supplier_id := NULL;
12918     l_pegging_rec.supplier_name := NULL;
12919     l_pegging_rec.supplier_site_id := NULL;
12920     l_pegging_rec.supplier_site_name := NULL;
12921 
12922     l_pegging_rec.source_type := 0;
12923 
12924     MSC_ATP_DB_UTILS.Add_Pegging(l_pegging_rec, l_pegging_id);
12925 
12926     FOR i in 1..l_atp_period.Level.COUNT LOOP
12927             l_atp_period.Pegging_Id(i) := l_pegging_id;
12928             l_atp_period.End_Pegging_Id(i) := MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
12929 
12930 	    l_atp_period2.Pegging_Id(i) := l_pegging_id;
12931             l_atp_period2.End_Pegging_Id(i) := MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
12932 
12933     END LOOP;
12934 
12935     IF PG_DEBUG in ('Y', 'C') THEN
12936        msc_sch_wb.atp_debug('Get_Transport_Cap_Atp_Info: ' || 'in get_res_requirements we are here 2');
12937     END IF;
12938 
12939     FOR i in 1..l_atp_supply_demand.Level.COUNT LOOP
12940             l_atp_supply_demand.Pegging_Id(i) := l_pegging_id;
12941             l_atp_supply_demand.End_Pegging_Id(i) := MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
12942 
12943             l_atp_supply_demand2.Pegging_Id(i) := l_pegging_id;
12944             l_atp_supply_demand2.End_Pegging_Id(i) := MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
12945 
12946     END LOOP;
12947 
12948     -- select weight information into table
12949 
12950     SELECT
12951     col1,
12952     col2,
12953     col3,
12954     col4,
12955     col5,
12956     col6,
12957     col7,
12958     col8,
12959     col9,
12960     col10,
12961     col11,
12962     col12,
12963     col13,
12964     col14,
12965     col15,
12966     col16,
12967     col17,
12968     col18,
12969     col19,
12970     col20,
12971     col21,
12972     col22,
12973     col23,
12974     col24,
12975     col25,
12976     col26,
12977     col27,
12978     col28,
12979     col29
12980     BULK COLLECT INTO
12981 		     l_atp_supply_demand.Level,
12982                      l_atp_supply_demand.Identifier,
12983                      l_atp_supply_demand.Inventory_Item_Id,
12984                      l_atp_supply_demand.Request_Item_Id,
12985                      l_atp_supply_demand.Organization_Id,
12986                      l_atp_supply_demand.Department_Id,
12987                      l_atp_supply_demand.Resource_Id,
12988                      l_atp_supply_demand.Supplier_Id,
12989                      l_atp_supply_demand.Supplier_Site_Id,
12990                      l_atp_supply_demand.From_Organization_Id,
12991                      l_atp_supply_demand.From_Location_Id,
12992                      l_atp_supply_demand.To_Organization_Id,
12993                      l_atp_supply_demand.To_Location_Id,
12994                      l_atp_supply_demand.Ship_Method,
12995                      l_atp_supply_demand.Uom,
12996                      l_atp_supply_demand.Supply_Demand_Type,
12997                      l_atp_supply_demand.Supply_Demand_Source_Type,
12998                      l_atp_supply_demand.Supply_Demand_Source_Type_Name,
12999                      l_atp_supply_demand.Identifier1,
13000                      l_atp_supply_demand.Identifier2,
13001                      l_atp_supply_demand.Identifier3,
13002                      l_atp_supply_demand.Identifier4,
13003                      l_atp_supply_demand.Supply_Demand_Quantity,
13004                      l_atp_supply_demand.Supply_Demand_Date,
13005                      l_atp_supply_demand.Disposition_Type,
13006                      l_atp_supply_demand.Disposition_Name,
13007 		     l_atp_supply_demand.Scenario_Id,
13008                      l_atp_supply_demand.Pegging_Id,
13009                      l_atp_supply_demand.End_Pegging_Id
13010     FROM
13011     (
13012     SELECT
13013 	     p_level col1,
13014 	     p_identifier col2,
13015 	     p_inventory_item_id col3,
13016 	     null col4, -- request_item_id
13017 	     null col5, -- organization_id
13018 	     null col6, -- department_id
13019 	     null col7, -- resource_id
13020 	     null col8, -- supplier_id
13021 	     null col9, -- supplier_site_id
13022 	     p_from_organization_id col10,
13023 	     null col11, -- from_location_id
13024 	     p_to_organization_id col12,
13025 	     null col13, -- to_location_id
13026 	     p_ship_method col14,
13027 	     s.weight_uom col15, -- uom
13028 	     2 col16, -- supply
13029 	     null col17, -- supply_demand_source_type
13030 	     null col18, -- supply_demand_source_type_name
13031 	     p_source_org_instance_id col19, -- identifier1
13032              1 col20, -- identifier2, weight(1) or volume(2)
13033              null col21, -- identifier3
13034              p_dest_org_instance_id col22, -- identifier4
13035 	     s.weight_capacity col23,
13036 	     c.calendar_date col24,
13037 	     null col25, -- disposition_type
13038 	     null col26, -- disposition_name
13039 	     p_scenario_id col27, -- scenario_id
13040        	     null col28, -- pegging_id
13041 	     null col29  -- end_pegging_id
13042     FROM
13043 	     msc_calendar_dates c,
13044              msc_interorg_ship_methods s,
13045              msc_trading_partners tp,
13046              msc_plans p
13047     WHERE
13048  	     s.plan_id = p_plan_id
13049       AND    s.from_organization_id = p_from_organization_id
13050       AND    s.to_organization_id = p_to_organization_id
13051       AND    s.ship_method = p_ship_method
13052       AND    s.sr_instance_id = p_source_org_instance_id
13053       AND    s.sr_instance_id2 = p_dest_org_instance_id
13054       AND    s.from_organization_id = tp.sr_tp_id
13055       AND    tp.sr_instance_id = NVL(s.sr_instance_id, s.sr_instance_id2)
13056       AND    c.calendar_date BETWEEN trunc(SYSDATE) and trunc(p.curr_cutoff_date)    -- to_date changed to trunc to avoid GSCC error
13057       AND    c.calendar_code = tp.calendar_code
13058       AND    c.exception_set_id = tp.calendar_exception_set_id
13059       AND    p.plan_id = p_plan_id
13060       UNION ALL
13061       SELECT p_level col1,
13062              p_identifier col2,
13063              p_inventory_item_id col3,
13064              null col4, -- request_item_id
13065              null col5, -- organization_id
13066              null col6, -- department_id
13067              null col7, -- resource_id
13068              null col8, -- supplier_id
13069              null col9, -- supplier_site_id
13070              p_from_organization_id col10,
13071              null col11, -- from_location_id
13072              p_to_organization_id col12,
13073              null col13, -- to_location_id
13074              p_ship_method col14,
13075              s.weight_uom col15, -- uom
13076 	     1 col16, -- demand
13077 	     null col17, -- supply_demand_source_type
13078              null col18, -- supply_demand_source_type_name
13079 	     p_source_org_instance_id col19, -- identifier1
13080              1 col20, -- identifier2, weight(1) or volume(2)
13081              null col21, -- identifier3
13082              p_dest_org_instance_id col22, -- identifier4
13083 	     -1*(sup.new_order_quantity)*l_unit_weight col23,
13084 	     sup.new_schedule_date col24,
13085              null col25, -- disposition_type
13086              null col26, -- disposition_name
13087              p_scenario_id col27, -- scenairo_id
13088              null col28, -- pegging_id
13089              null col29  -- end_pegging_id
13090       FROM   msc_supplies sup,
13091 	     msc_interorg_ship_methods s
13092       WHERE  sup.plan_id = p_plan_id
13093       AND    sup.organization_id = p_to_organization_id
13094       AND    sup.sr_instance_id = p_dest_org_instance_id
13095       AND    sup.source_organization_id is not null
13096       AND    sup.source_organization_id = p_from_organization_id
13097       AND    sup.source_sr_instance_id = p_source_org_instance_id
13098       AND    sup.ship_method = p_ship_method
13099       AND    sup.inventory_item_id = p_inventory_item_id
13100       AND    s.plan_id = p_plan_id
13101       AND    s.from_organization_id = p_from_organization_id
13102       AND    s.to_organization_id = p_to_organization_id
13103       AND    s.ship_method = p_ship_method
13104       AND    s.sr_instance_id = p_source_org_instance_id
13105       AND    s.sr_instance_id2 = p_dest_org_instance_id
13106       )
13107       ORDER BY col25;
13108 
13109   -- select volume information into table
13110     SELECT
13111     col1,
13112     col2,
13113     col3,
13114     col4,
13115     col5,
13116     col6,
13117     col7,
13118     col8,
13119     col9,
13120     col10,
13121     col11,
13122     col12,
13123     col13,
13124     col14,
13125     col15,
13126     col16,
13127     col17,
13128     col18,
13129     col19,
13130     col20,
13131     col21,
13132     col22,
13133     col23,
13134     col24,
13135     col25,
13136     col26,
13137     col27,
13138     col28,
13139     col29
13140     BULK COLLECT INTO
13141 		     l_atp_supply_demand2.Level,
13142                      l_atp_supply_demand2.Identifier,
13143                      l_atp_supply_demand2.Inventory_Item_Id,
13144                      l_atp_supply_demand2.Request_Item_Id,
13145                      l_atp_supply_demand2.Organization_Id,
13146                      l_atp_supply_demand2.Department_Id,
13147                      l_atp_supply_demand2.Resource_Id,
13148                      l_atp_supply_demand2.Supplier_Id,
13149                      l_atp_supply_demand2.Supplier_Site_Id,
13150                      l_atp_supply_demand2.From_Organization_Id,
13151                      l_atp_supply_demand2.From_Location_Id,
13152                      l_atp_supply_demand2.To_Organization_Id,
13153                      l_atp_supply_demand2.To_Location_Id,
13154                      l_atp_supply_demand2.Ship_Method,
13155                      l_atp_supply_demand2.Uom,
13156                      l_atp_supply_demand2.Supply_Demand_Type,
13157                      l_atp_supply_demand2.Supply_Demand_Source_Type,
13158                      l_atp_supply_demand2.Supply_Demand_Source_Type_Name,
13159                      l_atp_supply_demand2.Identifier1,
13160                      l_atp_supply_demand2.Identifier2,
13161                      l_atp_supply_demand2.Identifier3,
13162                      l_atp_supply_demand2.Identifier4,
13163                      l_atp_supply_demand2.Supply_Demand_Quantity,
13164                      l_atp_supply_demand2.Supply_Demand_Date,
13165                      l_atp_supply_demand2.Disposition_Type,
13166                      l_atp_supply_demand2.Disposition_Name,
13167 		     l_atp_supply_demand2.Scenario_Id,
13168                      l_atp_supply_demand2.Pegging_Id,
13169                      l_atp_supply_demand2.End_Pegging_Id
13170     FROM
13171     (
13172     SELECT
13173 	     p_level col1,
13174 	     p_identifier col2,
13175 	     p_inventory_item_id col3,
13176 	     null col4, -- request_item_id
13177 	     null col5, -- organization_id
13178 	     null col6, -- department_id
13179 	     null col7, -- resource_id
13180 	     null col8, -- supplier_id
13181 	     null col9, -- supplier_site_id
13182 	     p_from_organization_id col10,
13183 	     null col11, -- from_location_id
13184 	     p_to_organization_id col12,
13185 	     null col13, -- to_location_id
13186 	     p_ship_method col14,
13187 	     s.volume_uom col15, -- uom
13188 	     2 col16, -- supply
13189 	     null col17, -- supply_demand_source_type
13190              null col18, -- supply_demand_source_type_name
13191 	     p_source_org_instance_id col19, -- identifier1
13192              2 col20, -- identifier2, weight(1) or volume(2)
13193              null col21, -- identifier3
13194              p_dest_org_instance_id col22, -- identifier4
13195 	     s.volume_capacity col23,
13196 	     c.calendar_date col24,
13197 	     null col25, -- disposition_type
13198 	     null col26, -- disposition_name
13199 	     p_scenario_id col27, -- scenairo_id
13200        	     null col28, -- pegging_id
13201 	     null col29  -- end_pegging_id
13202     FROM
13203 	     msc_calendar_dates c,
13204              msc_interorg_ship_methods s,
13205              msc_trading_partners tp,
13206              msc_plans p
13207     WHERE
13208  	     s.plan_id = p_plan_id
13209       AND    s.from_organization_id = p_from_organization_id
13210       AND    s.to_organization_id = p_to_organization_id
13211       AND    s.ship_method = p_ship_method
13212       AND    s.sr_instance_id = p_source_org_instance_id
13213       AND    s.sr_instance_id2 = p_dest_org_instance_id
13214       AND    s.from_organization_id = tp.sr_tp_id
13215       AND    tp.sr_instance_id = NVL(s.sr_instance_id, s.sr_instance_id2)
13216       AND    c.calendar_date BETWEEN trunc(SYSDATE) and trunc(p.curr_cutoff_date)   -- to_date changed to trunc to avoid GSCC error
13217       AND    c.calendar_code = tp.calendar_code
13218       AND    c.exception_set_id = tp.calendar_exception_set_id
13219       AND    p.plan_id = p_plan_id
13220       UNION ALL
13221       SELECT p_level col1,
13222              p_identifier col2,
13223              p_inventory_item_id col3,
13224              null col4, -- request_item_id
13225              null col5, -- organization_id
13226              null col6, -- department_id
13227              null col7, -- resource_id
13228              null col8, -- supplier_id
13229              null col9, -- supplier_site_id
13230              p_from_organization_id col10,
13231              null col11, -- from_location_id
13232              p_to_organization_id col12,
13233              null col13, -- to_location_id
13234              p_ship_method col14,
13235              s.volume_uom col15, -- uom
13236 	     1 col16, -- demand
13237 	     null col17, -- supply_demand_source_type
13238              null col18, -- supply_demand_source_type_name
13239 	     p_source_org_instance_id col19, -- identifier1
13240              2 col20, -- identifier2, weight(1) or volume(2)
13241              null col21, -- identifier3
13242              p_dest_org_instance_id col22, -- identifier4
13243 	     -1*(sup.new_order_quantity)*l_unit_volume col23,
13244 	     sup.new_schedule_date col24,
13245              null col25, -- disposition_type
13246              null col26, -- disposition_name
13247              p_scenario_id col27, -- scenairo_id
13248              null col28, -- pegging_id
13249              null col29  -- end_pegging_id
13250       FROM   msc_supplies sup,
13251 	     msc_interorg_ship_methods s
13252       WHERE  sup.plan_id = p_plan_id
13253       AND    sup.organization_id = p_to_organization_id
13254       AND    sup.sr_instance_id = p_dest_org_instance_id
13255       AND    sup.source_organization_id is not null
13256       AND    sup.source_organization_id = p_from_organization_id
13257       AND    sup.source_sr_instance_id = p_source_org_instance_id
13258       AND    sup.ship_method = p_ship_method
13259       AND    sup.inventory_item_id = p_inventory_item_id
13260       AND    s.plan_id = p_plan_id
13261       AND    s.from_organization_id = p_from_organization_id
13262       AND    s.to_organization_id = p_to_organization_id
13263       AND    s.ship_method = p_ship_method
13264       AND    s.sr_instance_id = p_source_org_instance_id
13265       AND    s.sr_instance_id2 = p_dest_org_instance_id
13266       )
13267       ORDER BY col25;
13268 
13269       m := 1;
13270 
13271       IF l_atp_supply_demand.Supply_Demand_Date.COUNT > 0 THEN
13272         MSC_SATP_FUNC.Extend_Atp_Period(l_atp_period, x_return_status);
13273 
13274    	FOR i IN 1..l_atp_supply_demand.Supply_Demand_Date.COUNT LOOP
13275      	  l_atp_period.Level(m) := l_atp_supply_demand.Level(i);
13276           l_atp_period.Identifier(m) := l_atp_supply_demand.Identifier(i);
13277           l_atp_period.Scenario_Id(m) := l_atp_supply_demand.Scenario_Id(i);
13278 	  l_atp_period.From_Organization_Id := l_atp_supply_demand.From_Organization_Id;
13279 	  l_atp_period.To_Organization_Id := l_atp_supply_demand.To_Organization_Id;
13280 	  l_atp_period.Ship_Method := l_atp_supply_demand.Ship_Method;
13281 	  l_atp_period.Uom := l_atp_supply_demand.Uom;
13282 
13283 	  IF i = 1 THEN
13284 	    l_atp_period.Period_Start_Date(m) := l_atp_supply_demand.Supply_Demand_Date(i);
13285 
13286   	    l_atp_period.Identifier1(m) := l_atp_supply_demand.Identifier1(i);
13287             l_atp_period.Identifier2(m) := l_atp_supply_demand.Identifier2(i);
13288 
13289 	    -- working on first supply demand record
13290             IF l_atp_supply_demand.Supply_Demand_Type(i) = 1 THEN
13291               l_atp_period.Total_Demand_Quantity(m) :=
13292                    l_atp_supply_demand.Supply_Demand_Quantity(i);
13293               l_atp_period.Total_Supply_Quantity(m) := 0;
13294               l_atp_period.Period_Quantity(m) :=
13295                    l_atp_period.Total_Supply_Quantity(m)+
13296                    l_atp_period.Total_Demand_Quantity(m);
13297 
13298 	    ELSE
13299                l_atp_period.Total_Supply_Quantity(m) :=
13300                    l_atp_supply_demand.Supply_Demand_Quantity(i);
13301                l_atp_period.Total_Demand_Quantity(m) := 0;
13302                l_atp_period.Period_Quantity(m) :=
13303                    l_atp_period.Total_Supply_Quantity(m)+
13304                    l_atp_period.Total_Demand_Quantity(m);
13305             END IF;
13306 
13307 	  ELSE
13308             -- working on 2nd record or later
13309             -- make sure the supply demand date of this record is
13310             -- greater than the previous bucket or not.
13311             IF l_atp_supply_demand.Supply_Demand_Date(i) >
13312                l_atp_period.Period_Start_Date(m) THEN
13313 
13314 	       -- populate the period_end_date and
13315                -- period_atp information.
13316                l_atp_period.Period_End_Date(m) :=
13317                    l_atp_supply_demand.Supply_Demand_Date(i) -1;
13318 
13319                -- add one more bucket
13320                m:=m+1;
13321 
13322                MSC_SATP_FUNC.Extend_Atp_Period(l_atp_period, x_return_status);
13323 
13324 	       l_atp_period.Level(m) := l_atp_supply_demand.Level(i);
13325                l_atp_period.Identifier(m) := l_atp_supply_demand.Identifier(i);
13326                l_atp_period.Scenario_Id(m) := l_atp_supply_demand.Scenario_Id(i);
13327                l_atp_period.From_Organization_Id := l_atp_supply_demand.From_Organization_Id;
13328                l_atp_period.To_Organization_Id := l_atp_supply_demand.To_Organization_Id;
13329                l_atp_period.Ship_Method := l_atp_supply_demand.Ship_Method;
13330                l_atp_period.Uom := l_atp_supply_demand.Uom;
13331 
13332                l_atp_period.Period_Start_Date(m) :=
13333                    l_atp_supply_demand.Supply_Demand_Date(i);
13334 
13335                l_atp_period.Identifier1(m):=l_atp_supply_demand.Identifier1(i);
13336                l_atp_period.Identifier2(m):=l_atp_supply_demand.Identifier2(i);
13337 
13338 	       IF l_atp_supply_demand.Supply_Demand_Type(i) = 1 THEN
13339                    l_atp_period.Total_Demand_Quantity(m) :=
13340                    l_atp_supply_demand.Supply_Demand_Quantity(i);
13341                    l_atp_period.Total_Supply_Quantity(m) := 0;
13342                    l_atp_period.Period_Quantity(m) :=
13343                    l_atp_period.Total_Supply_Quantity(m)+
13344                    l_atp_period.Total_Demand_Quantity(m);
13345 
13346                ELSE
13347                    l_atp_period.Total_Supply_Quantity(m) :=
13348                    l_atp_supply_demand.Supply_Demand_Quantity(m);
13349                    l_atp_period.Total_Demand_Quantity(m) := 0;
13350                    l_atp_period.Period_Quantity(m) :=
13351                    l_atp_period.Total_Supply_Quantity(m)+
13352                    l_atp_period.Total_Demand_Quantity(m);
13353 
13354                END IF;
13355 
13356 	    ELSE
13357 	       -- same bucket
13358                IF l_atp_supply_demand.Supply_Demand_Type(i) = 1 THEN
13359                    l_atp_period.Total_Demand_Quantity(m) :=
13360                    l_atp_period.Total_Demand_Quantity(m) +
13361                    l_atp_supply_demand.Supply_Demand_Quantity(i);
13362                ELSE
13363                    l_atp_period.Total_Supply_Quantity(m) :=
13364                    l_atp_period.Total_Supply_Quantity(m)+
13365                    l_atp_supply_demand.Supply_Demand_Quantity(i);
13366                END IF;
13367                l_atp_period.Period_Quantity(m) :=
13368                l_atp_period.Total_Supply_Quantity(m)+
13369                l_atp_period.Total_Demand_Quantity(m);
13370 
13371            END IF;
13372  	END IF; -- end 2nd record or later
13373       END LOOP;
13374     END IF;
13375 
13376     m := 1;
13377 
13378       IF l_atp_supply_demand2.Supply_Demand_Date.COUNT > 0 THEN
13379         MSC_SATP_FUNC.Extend_Atp_Period(l_atp_period2, x_return_status);
13380 
13381    	FOR i IN 1..l_atp_supply_demand2.Supply_Demand_Date.COUNT LOOP
13382      	  l_atp_period2.Level(m) := l_atp_supply_demand2.Level(i);
13383           l_atp_period2.Identifier(m) := l_atp_supply_demand2.Identifier(i);
13384           l_atp_period2.Scenario_Id(m) := l_atp_supply_demand2.Scenario_Id(i);
13385 	  l_atp_period2.From_Organization_Id := l_atp_supply_demand2.From_Organization_Id;
13386 	  l_atp_period2.To_Organization_Id := l_atp_supply_demand2.To_Organization_Id;
13387 	  l_atp_period2.Ship_Method := l_atp_supply_demand2.Ship_Method;
13388 	  l_atp_period2.Uom := l_atp_supply_demand2.Uom;
13389 
13390 	  IF i = 1 THEN
13391 	    l_atp_period2.Period_Start_Date(m) := l_atp_supply_demand2.Supply_Demand_Date(i);
13392 
13393   	    l_atp_period2.Identifier1(m) := l_atp_supply_demand2.Identifier1(i);
13394             l_atp_period2.Identifier2(m) := l_atp_supply_demand2.Identifier2(i);
13395 
13396 	    -- working on first supply demand record
13397             IF l_atp_supply_demand2.Supply_Demand_Type(i) = 1 THEN
13398               l_atp_period2.Total_Demand_Quantity(m) :=
13399                    l_atp_supply_demand2.Supply_Demand_Quantity(i);
13400               l_atp_period2.Total_Supply_Quantity(m) := 0;
13401               l_atp_period2.Period_Quantity(m) :=
13402                    l_atp_period2.Total_Supply_Quantity(m)+
13403                    l_atp_period2.Total_Demand_Quantity(m);
13404 
13405 	    ELSE
13406                l_atp_period2.Total_Supply_Quantity(m) :=
13407                    l_atp_supply_demand2.Supply_Demand_Quantity(i);
13408                l_atp_period2.Total_Demand_Quantity(m) := 0;
13409                l_atp_period2.Period_Quantity(m) :=
13410                    l_atp_period2.Total_Supply_Quantity(m)+
13411                    l_atp_period2.Total_Demand_Quantity(m);
13412             END IF;
13413 
13414 	  ELSE
13415             -- working on 2nd record or later
13416             -- make sure the supply demand date of this record is
13417             -- greater than the previous bucket or not.
13418             IF l_atp_supply_demand2.Supply_Demand_Date(i) >
13419                l_atp_period2.Period_Start_Date(m) THEN
13420 
13421 	       -- populate the period_end_date and
13422                -- period_atp information.
13423                l_atp_period2.Period_End_Date(m) :=
13424                    l_atp_supply_demand2.Supply_Demand_Date(i) -1;
13425 
13426                -- add one more bucket
13427                m:=m+1;
13428 
13429                MSC_SATP_FUNC.Extend_Atp_Period(l_atp_period2, x_return_status);
13430 
13431 	       l_atp_period2.Level(m) := l_atp_supply_demand2.Level(i);
13432                l_atp_period2.Identifier(m) := l_atp_supply_demand2.Identifier(i);
13433                l_atp_period2.Scenario_Id(m) := l_atp_supply_demand2.Scenario_Id(i);
13434                l_atp_period2.From_Organization_Id := l_atp_supply_demand2.From_Organization_Id;
13435                l_atp_period2.To_Organization_Id := l_atp_supply_demand2.To_Organization_Id;
13436                l_atp_period2.Ship_Method := l_atp_supply_demand2.Ship_Method;
13437                l_atp_period2.Uom := l_atp_supply_demand2.Uom;
13438 
13439                l_atp_period2.Period_Start_Date(m) :=
13440                    l_atp_supply_demand2.Supply_Demand_Date(i);
13441 
13442                l_atp_period2.Identifier1(m):=l_atp_supply_demand2.Identifier1(i);
13443                l_atp_period2.Identifier2(m):=l_atp_supply_demand2.Identifier2(i);
13444 
13445 	       IF l_atp_supply_demand2.Supply_Demand_Type(i) = 1 THEN
13446                    l_atp_period2.Total_Demand_Quantity(m) :=
13447                    l_atp_supply_demand2.Supply_Demand_Quantity(i);
13448                    l_atp_period2.Total_Supply_Quantity(m) := 0;
13449                    l_atp_period2.Period_Quantity(m) :=
13450                    l_atp_period2.Total_Supply_Quantity(m)+
13451                    l_atp_period2.Total_Demand_Quantity(m);
13452 
13453                ELSE
13454                    l_atp_period2.Total_Supply_Quantity(m) :=
13455                    l_atp_supply_demand2.Supply_Demand_Quantity(m);
13456                    l_atp_period2.Total_Demand_Quantity(m) := 0;
13457                    l_atp_period2.Period_Quantity(m) :=
13458                    l_atp_period2.Total_Supply_Quantity(m)+
13459                    l_atp_period2.Total_Demand_Quantity(m);
13460 
13461                END IF;
13462 
13463 	    ELSE
13464 	       -- same bucket
13465                IF l_atp_supply_demand2.Supply_Demand_Type(i) = 1 THEN
13466                    l_atp_period2.Total_Demand_Quantity(m) :=
13467                    l_atp_period2.Total_Demand_Quantity(m) +
13468                    l_atp_supply_demand2.Supply_Demand_Quantity(i);
13469                ELSE
13470                    l_atp_period2.Total_Supply_Quantity(m) :=
13471                    l_atp_period2.Total_Supply_Quantity(m)+
13472                    l_atp_supply_demand2.Supply_Demand_Quantity(i);
13473                END IF;
13474                l_atp_period2.Period_Quantity(m) :=
13475                l_atp_period2.Total_Supply_Quantity(m)+
13476                l_atp_period2.Total_Demand_Quantity(m);
13477 
13478            END IF;
13479  	END IF; -- end 2nd record or later
13480       END LOOP;
13481     END IF;
13482 
13483     l_atp_period.Cumulative_Quantity := l_atp_period.Period_Quantity;
13484     -- do the accumulation
13485     MSC_ATP_PROC.atp_consume(l_atp_period.Cumulative_Quantity, m);
13486 
13487     l_atp_period2.Cumulative_Quantity := l_atp_period2.Period_Quantity;
13488     -- do the accumulation
13489     MSC_ATP_PROC.atp_consume(l_atp_period2.Cumulative_Quantity, m);
13490 
13491   END IF;
13492 
13493   MSC_ATP_PROC.Details_Output(l_atp_period2, l_atp_supply_demand2, l_atp_period, l_atp_supply_demand,
13494 			x_return_status);
13495 
13496   x_atp_period := l_atp_period;
13497   x_atp_supply_demand := l_atp_supply_demand;
13498 
13499 
13500 EXCEPTION
13501 
13502   WHEN NO_DATA_FOUND THEN
13503     x_return_status := FND_API.G_RET_STS_ERROR;
13504 END Get_Transport_Cap_Atp_Info;
13505 
13506 
13507 --s_cto_rearch
13508 procedure Extend_Atp_Comp_Typ ( P_Atp_Comp_Typ IN OUT NOCOPY MRP_ATP_PVT.Atp_Comp_Typ)
13509 IS
13510 BEGIN
13511 
13512    P_Atp_Comp_Typ.inventory_item_id.extend;
13513    P_Atp_Comp_Typ.comp_usage.extend;
13514    P_Atp_Comp_Typ.requested_date.extend;
13515    P_Atp_Comp_Typ.lead_time.extend;
13516    P_Atp_Comp_Typ.wip_supply_type.extend;
13517    P_Atp_Comp_Typ.assembly_identifier.extend;
13518    P_Atp_Comp_Typ.component_identifier.extend;
13519    P_Atp_Comp_Typ.reverse_cumulative_yield.extend;
13520    P_Atp_Comp_Typ.match_item_id.extend;
13521    P_Atp_Comp_Typ.bom_item_type.extend;
13522    P_Atp_Comp_Typ.parent_line_id.extend;
13523    P_Atp_Comp_Typ.top_model_line_id.extend;
13524    P_Atp_Comp_Typ.ato_parent_model_line_id.extend;
13525    P_Atp_Comp_Typ.ato_model_line_id.extend;
13526    P_Atp_Comp_Typ.mand_comp_flag.extend;
13527    P_Atp_Comp_Typ.parent_so_quantity.extend;
13528    P_Atp_Comp_Typ.fixed_lt.extend;
13529    p_atp_comp_typ.variable_lt.extend;
13530    p_atp_comp_typ.oss_error_code.extend;
13531    p_atp_comp_typ.atp_flag.extend;
13532    p_atp_comp_typ.atp_components_flag.extend;
13533    P_Atp_Comp_Typ.request_item_id.extend;       -- For time_phased_atp
13534    P_Atp_Comp_Typ.atf_date.extend;              -- For time_phased_atp
13535    P_Atp_Comp_Typ.match_item_family_id.extend;  -- For time_phased_atp
13536    P_Atp_Comp_Typ.dest_inventory_item_id.extend;
13537    P_Atp_Comp_Typ.parent_item_id.extend;
13538    P_Atp_Comp_Typ.comp_uom.extend; --bug3110023
13539    --4570421
13540    P_Atp_Comp_Typ.scaling_type.extend;
13541    P_Atp_Comp_Typ.scale_multiple.extend;
13542    P_Atp_Comp_Typ.scale_rounding_variance.extend;
13543    P_Atp_Comp_Typ.rounding_direction.extend;
13544    P_Atp_Comp_Typ.component_yield_factor.extend;
13545    P_Atp_Comp_Typ.usage_qty.extend; --4775920
13546    P_Atp_Comp_Typ.organization_type.extend; --4775920
13547 
13548 END Extend_Atp_Comp_Typ;
13549 
13550 
13551 Procedure Add_To_Comp_List(p_explode_comp_rec          IN OUT NOCOPY MRP_ATP_PVT.Atp_Comp_Typ,
13552                            p_component_rec             IN OUT NOCOPY MRP_ATP_PVT.Atp_Comp_Typ,
13553                            p_atp_comp_rec              IN MRP_ATP_PVT.ATP_COMP_REC)
13554 
13555 IS
13556 j  number;
13557 BEGIN
13558 
13559    IF PG_DEBUG in ('Y', 'C') THEN
13560        msc_sch_wb.atp_debug('Add_To_Comp_List: Enter Into Add_To_Comp_List');
13561        msc_sch_wb.atp_debug('Add_To_Comp_List: inventory_item_id := ' || p_atp_comp_rec.inventory_item_id );
13562        msc_sch_wb.atp_debug('Add_To_Comp_List: comp_usage := ' || p_atp_comp_rec.comp_usage );
13563        msc_sch_wb.atp_debug('Add_To_Comp_List: requested_date := ' || p_atp_comp_rec.requested_date );
13564        msc_sch_wb.atp_debug('Add_To_Comp_List: lead_time := ' || p_atp_comp_rec.lead_time );
13565        msc_sch_wb.atp_debug('Add_To_Comp_List: wip_supply_type := ' || p_atp_comp_rec.wip_supply_type );
13566        msc_sch_wb.atp_debug('Add_To_Comp_List: assembly_identifier := ' || p_atp_comp_rec.assembly_identifier );
13567        msc_sch_wb.atp_debug('Add_To_Comp_List: component_identifier := ' || p_atp_comp_rec.component_identifier );
13568        msc_sch_wb.atp_debug('Add_To_Comp_List: reverse_cumulative_yield := ' || p_atp_comp_rec.reverse_cumulative_yield );
13569        msc_sch_wb.atp_debug('Add_To_Comp_List: match_item_id:= ' || p_atp_comp_rec.match_item_id );
13570        msc_sch_wb.atp_debug('Add_To_Comp_List: match_item_family_id := ' || p_atp_comp_rec.match_item_family_id);
13571        msc_sch_wb.atp_debug('Add_To_Comp_List: bom_item_type := ' || p_atp_comp_rec.bom_item_type );
13572        msc_sch_wb.atp_debug('Add_To_Comp_List: parent_line_id := ' || p_atp_comp_rec.parent_line_id );
13573        msc_sch_wb.atp_debug('Add_To_Comp_List: top_model_line_id := ' || p_atp_comp_rec.top_model_line_id );
13574        msc_sch_wb.atp_debug('Add_To_Comp_List: ato_parent_model_line_id := ' || p_atp_comp_rec.ato_parent_model_line_id );
13575        msc_sch_wb.atp_debug('Add_To_Comp_List: ato_model_line_id := ' || p_atp_comp_rec.ato_model_line_id );
13576        msc_sch_wb.atp_debug('Add_To_Comp_List: MAND_COMP_FLAG := ' || p_atp_comp_rec.MAND_COMP_FLAG );
13577        msc_sch_wb.atp_debug('Add_To_Comp_List: parent_so_quantity := ' || p_atp_comp_rec.parent_so_quantity );
13578        msc_sch_wb.atp_debug('Add_To_Comp_List: fixed_lt := ' || p_atp_comp_rec.fixed_lt );
13579        msc_sch_wb.atp_debug('Add_To_Comp_List: variable_lt := ' || p_atp_comp_rec.variable_lt );
13580        msc_sch_wb.atp_debug('Add_To_Comp_List: oss_error_code := ' || p_atp_comp_rec.oss_error_code );
13581        msc_sch_wb.atp_debug('Add_To_Comp_List: model_flag := ' || p_atp_comp_rec.model_flag );
13582        msc_sch_wb.atp_debug('Add_To_Comp_List: requested_quantity := ' || p_atp_comp_rec.requested_quantity );
13583        msc_sch_wb.atp_debug('Add_To_Comp_List: atp flag := ' || p_atp_comp_rec.atp_flag);
13584        msc_sch_wb.atp_debug('Add_To_Comp_List: atp_comp_flag := '|| p_atp_comp_rec.atp_components_flag);
13585        msc_sch_wb.atp_debug('Add_To_Comp_List: atf_date := '|| p_atp_comp_rec.atf_date);
13586        msc_sch_wb.atp_debug('Add_To_Comp_List: dest_inventory_item_id := ' || p_atp_comp_rec.dest_inventory_item_id);
13587        msc_sch_wb.atp_debug('Add_To_Comp_List: parent_repl_ord_flag := ' || p_atp_comp_rec.parent_repl_ord_flag);
13588        msc_sch_wb.atp_debug('Add_To_Comp_List: comp_uom := ' || p_atp_comp_rec.comp_uom); --bug3110023
13589 
13590    END IF;
13591 
13592    IF ((p_atp_comp_rec.model_flag = 1 and p_atp_comp_rec.bom_item_type = 2) or
13593                                  (p_atp_comp_rec.model_flag <>  1 and p_atp_comp_rec.wip_supply_type = 6
13594                                                                   and p_atp_comp_rec.parent_repl_ord_flag = 'N'
13595                                                                   and MSC_ATP_PVT.G_INV_CTP = 4)) THEN
13596       -- this is phantom, add to explode list
13597 
13598       j := p_explode_comp_rec.inventory_item_id.COUNT;
13599 
13600       -- bug 1831563: removed the extend and assign to
13601       -- l_explode_comp.pre_process_lead_time since it is not needed.
13602 
13603       IF PG_DEBUG in ('Y', 'C') THEN
13604           msc_sch_wb.atp_debug('Add_To_Comp_List: ' || 'in side phantom, count = '||j);
13605           msc_sch_wb.atp_debug('Add_To_Comp_List: Count in explode comp := ' || p_explode_comp_rec.inventory_item_id.count);
13606       END IF;
13607 
13608       MSC_ATP_REQ.Extend_ATP_Comp_TYP(p_explode_comp_rec);
13609 
13610       IF PG_DEBUG in ('Y', 'C') THEN
13611           msc_sch_wb.atp_debug('Add_To_Comp_List: ' || 'after extend');
13612       END IF;
13613 
13614       p_explode_comp_rec.inventory_item_id(j+1):= p_atp_comp_rec.inventory_item_id;
13615       /* time_phased_atp changes begin
13616          Support PF ATP for components*/
13617       p_explode_comp_rec.request_item_id(j+1):= p_atp_comp_rec.request_item_id;
13618       p_explode_comp_rec.atf_date(j+1):= p_atp_comp_rec.atf_date;
13619       p_explode_comp_rec.match_item_family_id(j + 1) := p_atp_comp_rec.match_item_family_id;
13620       /* time_phased_atp changes end*/
13621 
13622       p_explode_comp_rec.requested_date(j+1) := p_atp_comp_rec.requested_date;
13623       p_explode_comp_rec.comp_usage(j+1) := p_atp_comp_rec.comp_usage;
13624       p_explode_comp_rec.wip_supply_type(j+1) := p_atp_comp_rec.wip_supply_type;
13625       --(3004862) circular BOM issue Assign the Inventory item id for component. We'll
13626       --be using this later when accessing process effectivity.
13627       p_explode_comp_rec.component_identifier(j+1) := p_atp_comp_rec.component_identifier;
13628 
13629       --- bug 2680027: Now remember the lead time for parent
13630       p_explode_comp_rec.lead_time(j+1) := p_atp_comp_rec.lead_time;
13631 
13632       p_explode_comp_rec.assembly_identifier(j +1) := p_atp_comp_rec.assembly_identifier;
13633       p_explode_comp_rec.match_item_id(j + 1) := p_atp_comp_rec.match_item_id;
13634       p_explode_comp_rec.bom_item_type(j + 1) := p_atp_comp_rec.bom_item_type;
13635       p_explode_comp_rec.top_model_line_id(j +1) := p_atp_comp_rec.top_model_line_id;
13636       p_explode_comp_rec.ato_parent_model_line_id(j +1 ) := p_atp_comp_rec.ato_parent_model_line_id;
13637       p_explode_comp_rec.ato_model_line_id(j+ 1) := p_atp_comp_rec.ato_model_line_id;
13638       p_explode_comp_rec.parent_line_id(j+1) := p_atp_comp_rec.parent_line_id;
13639       p_explode_comp_rec.reverse_cumulative_yield(j+1) := p_atp_comp_rec.reverse_cumulative_yield;
13640       p_explode_comp_rec.parent_so_quantity(j + 1) := p_atp_comp_rec.parent_so_quantity;
13641       p_explode_comp_rec.mand_comp_flag(j + 1) := p_atp_comp_rec.mand_comp_flag;
13642       p_explode_comp_rec.fixed_lt(j +1) := p_atp_comp_rec.fixed_lt;
13643       p_explode_comp_rec.variable_lt(j +1 ) := p_atp_comp_rec.variable_lt;
13644       p_explode_comp_rec.oss_error_code(j + 1) := p_atp_comp_rec.oss_error_code;
13645       p_explode_comp_rec.atp_flag(j +1 ) := p_atp_comp_rec.atp_flag;
13646       p_explode_comp_rec.atp_components_flag(j +1 ) := p_atp_comp_rec.atp_components_flag;
13647       p_explode_comp_rec.dest_inventory_item_id(j +1)  := p_atp_comp_rec.dest_inventory_item_id;
13648       p_explode_comp_rec.comp_uom(j +1)  := p_atp_comp_rec.comp_uom; --bug3110023
13649       IF PG_DEBUG in ('Y', 'C') THEN
13650            msc_sch_wb.atp_debug('Add_To_Comp_List: explode lead_time := ' || p_explode_comp_rec.lead_time(j+1));
13651            msc_sch_wb.atp_debug('Add_To_Comp_List: ' || 'after assign ');
13652       END IF;
13653    END IF;
13654 
13655    IF p_atp_comp_rec.wip_supply_type <> 6 or
13656        p_atp_comp_rec.parent_repl_ord_flag = 'Y' or p_atp_comp_rec.model_flag = 1 THEN
13657 
13658          -- this is not phantom, add to the component list
13659 
13660          j := p_component_rec.inventory_item_id.COUNT;
13661 
13662          IF PG_DEBUG in ('Y', 'C') THEN
13663              msc_sch_wb.atp_debug('Add_To_Comp_List: ' || 'not phantom, j='||j);
13664          END IF;
13665 
13666          MSC_ATP_REQ.Extend_ATP_Comp_TYP(p_component_rec);
13667 
13668          p_component_rec.inventory_item_id(j+1):= p_atp_comp_rec.inventory_item_id;
13669          /* time_phased_atp changes begin
13670             Support PF ATP for components*/
13671          p_component_rec.request_item_id(j+1):= p_atp_comp_rec.request_item_id;
13672          p_component_rec.atf_date(j+1):= p_atp_comp_rec.atf_date;
13673          -- time_phased_atp changes end
13674 
13675          p_component_rec.requested_date(j+1) := p_atp_comp_rec.requested_date;
13676          --p_component_rec.comp_usage(j+1) := p_atp_comp_rec.comp_usage/p_atp_comp_rec.requested_quantity;
13677          --4570421
13678          IF ( (MSC_ATP_PVT.G_ORG_INFO_REC.org_type = MSC_ATP_PVT.DISCRETE_ORG AND nvl(p_atp_comp_rec.scaling_type,1) = 1) OR
13679                 (MSC_ATP_PVT.G_ORG_INFO_REC.org_type = MSC_ATP_PVT.OPM_ORG AND nvl(p_atp_comp_rec.scaling_type,1) IN (1,3,4,5))) THEN
13680                 p_component_rec.comp_usage(j+1) := p_atp_comp_rec.comp_usage/p_atp_comp_rec.requested_quantity;
13681          ELSE
13682                 p_component_rec.comp_usage(j+1) := p_atp_comp_rec.comp_usage;
13683          END IF;
13684          IF PG_DEBUG in ('Y', 'C') THEN
13685              msc_sch_wb.atp_debug('Add_To_Comp_List: p_atp_comp_rec.comp_usage ' || p_atp_comp_rec.comp_usage);
13686              msc_sch_wb.atp_debug('Add_To_Comp_List: p_atp_comp_rec.requested_quantity ' || p_atp_comp_rec.requested_quantity);
13687              msc_sch_wb.atp_debug('Add_To_Comp_List: p_component_rec.comp_usage(j+1) ' || p_component_rec.comp_usage(j+1));
13688          END IF;
13689 
13690          --4570421
13691          p_component_rec.scaling_type(j+1) := p_atp_comp_rec.scaling_type;
13692          p_component_rec.scale_multiple(j+1) := p_atp_comp_rec.scale_multiple;
13693          p_component_rec.scale_rounding_variance(j+1) := p_atp_comp_rec.scale_rounding_variance;
13694          p_component_rec.rounding_direction(j+1) := p_atp_comp_rec.rounding_direction;
13695          p_component_rec.component_yield_factor(j+1) := p_atp_comp_rec.component_yield_factor; --4570421
13696          p_component_rec.usage_qty(j+1) := p_atp_comp_rec.usage_qty; --4775920
13697          p_component_rec.organization_type(j+1) := p_atp_comp_rec.organization_type; --4775920
13698 
13699          --- bug 2680027: Add lead time from Parent
13700          --l_comp_requirements.lead_time(j+1) := l_atp_comp_rec.comp_lead_time;
13701          p_component_rec.lead_time(j+1) := p_atp_comp_rec.lead_time;
13702 
13703          p_component_rec.assembly_identifier(j+1) := p_atp_comp_rec.assembly_identifier;
13704          p_component_rec.component_identifier(j+1) := p_atp_comp_rec.component_identifier;
13705 
13706          p_component_rec.reverse_cumulative_yield(j+1) := p_atp_comp_rec.reverse_cumulative_yield;
13707          p_component_rec.wip_supply_type(j +1) := p_atp_comp_rec.wip_supply_type;
13708          p_component_rec.match_item_id(j+1) := p_atp_comp_rec.match_item_id;
13709          p_component_rec.bom_item_type(j +1) := p_atp_comp_rec.bom_item_type;
13710          p_component_rec.parent_line_id(j +1) := p_atp_comp_rec.parent_line_id;
13711          p_component_rec.top_model_line_id(j +1) := p_atp_comp_rec.top_model_line_id;
13712          p_component_rec.ato_parent_model_line_id(j+1) := p_atp_comp_rec.ato_parent_model_line_id;
13713          p_component_rec.ato_model_line_id(j+1) := p_atp_comp_rec.ato_model_line_id;
13714          p_component_rec.parent_so_quantity(j +1) := p_atp_comp_rec.parent_so_quantity;
13715          p_component_rec.mand_comp_flag(j +1) := p_atp_comp_rec.mand_comp_flag;
13716          p_component_rec.fixed_lt(j+1) := p_atp_comp_rec.fixed_lt;
13717          p_component_rec.variable_lt(j+1) := p_atp_comp_rec.variable_lt;
13718          p_component_rec.oss_error_code(j +1) := p_atp_comp_rec.oss_error_code;
13719          p_component_rec.atp_flag(j +1)  := p_atp_comp_rec.atp_flag;
13720          p_component_rec.atp_components_flag(j +1) := p_atp_comp_rec.atp_components_flag;
13721          p_component_rec.dest_inventory_item_id(j +1)  := p_atp_comp_rec.dest_inventory_item_id;
13722          p_component_rec.match_item_family_id(j + 1) := p_atp_comp_rec.match_item_family_id;
13723          p_component_rec.comp_uom(j + 1) := p_atp_comp_rec.comp_uom; --bug3110023
13724 
13725 
13726          IF PG_DEBUG in ('Y', 'C') THEN
13727             msc_sch_wb.atp_debug('Add_To_Comp_List: ' || 'in side not phantom');
13728          END IF;
13729    END IF;
13730 
13731 END Add_To_Comp_List;
13732 ---e_cto_rearch
13733 
13734 
13735 END MSC_ATP_REQ;