DBA Data[Home] [Help]

PACKAGE BODY: APPS.MSC_ATP_REQ

Source


1 PACKAGE BODY MSC_ATP_REQ AS
2 /* $Header: MSCRATPB.pls 120.29.12010000.2 2008/08/25 10:43:20 sbnaik 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)
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) -- 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) -- 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) -- 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) -- 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 = p_routing_seq_id -- Local var for common and others
6133                  -- Bug 3432530
6134                  -- Comment out join conditions for msc_system_items
6135                   -- 3358981 Eliminate semi cartesian product, streamline query.
6136         AND      RTG.assembly_item_id = mcbom1.inventory_item_id
6137         -- Join to system items
6138         -- AND      I.PLAN_ID = RTG.PLAN_ID
6139         -- AND      I.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
6140         -- AND      I.ORGANIZATION_ID = RTG.ORGANIZATION_ID
6141         -- AND      I.INVENTORY_ITEM_ID = RTG.assembly_item_id
6142                    -- 3358981 Eliminate semi cartesian product, streamline query.
6143         -- AND      I.INVENTORY_ITEM_ID = l_inventory_item_id
6144                  -- End Bug 3432530
6145         --(ssurendr) Bug 2865389 Removed condition for Alternate Routing designator as
6146         --we are accessing Routing by Routing sequance id.
6147         --We are Driving by routing table for performance gains.
6148        	--AND      RTG.ALTERNATE_ROUTING_DESIGNATOR IS NULL
6149         --  Get all operations for the routing
6150        	AND      OP.PLAN_ID = RTG.PLAN_ID
6151        	AND      OP.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
6152        	AND      OP.ROUTING_SEQUENCE_ID = RTG.ROUTING_SEQUENCE_ID
6153         /* Operation is of type Event (Do not select process) */
6154         and      NVL(OP.operation_type,1 ) = 1
6155         /* rajjain 3008611
6156          * effective date should be greater than or equal to greatest of PTF date, sysdate and start date
6157          * disable date should be less than or equal to greatest of PTF date, sysdate and start date*/
6158        	AND      TRUNC(NVL(OP.DISABLE_DATE, GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE)+1)) >
6159         	         	TRUNC(GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE))
6160        	AND      TRUNC(OP.EFFECTIVITY_DATE) <=
6161          	      	TRUNC(GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE)) -- bug 1404312
6162                  -- SMCs/Mandatory Operations
6163         AND      OP.option_dependent_flag = 2
6164                  -- for the configuration
6165         -- Obtain the Resource Seq numbers.
6166        	AND      SEQ.PLAN_ID = OP.PLAN_ID
6167        	AND      SEQ.ROUTING_SEQUENCE_ID  = OP.ROUTING_SEQUENCE_ID
6168        	AND      SEQ.SR_INSTANCE_ID = OP.SR_INSTANCE_ID
6169        	AND      SEQ.OPERATION_SEQUENCE_ID = OP.OPERATION_SEQUENCE_ID
6170        	AND      RES.BASIS_TYPE in (1,2,3) --4694958
6171        	AND      RES.PLAN_ID = SEQ.PLAN_ID
6172        	AND      RES.ROUTING_SEQUENCE_ID = SEQ.ROUTING_SEQUENCE_ID
6173        	AND      RES.SR_INSTANCE_ID = SEQ.SR_INSTANCE_ID
6174        	AND      RES.OPERATION_SEQUENCE_ID = SEQ.OPERATION_SEQUENCE_ID
6175        	AND      RES.RESOURCE_SEQ_NUM = SEQ.RESOURCE_SEQ_NUM
6176        	AND      NVL(RES.ALTERNATE_NUMBER, 0) = 0 -- bug 1170698
6177        	AND      C1.CALENDAR_DATE = p_requested_date
6178                  -- Bug 3432530 Use RTG instead of MSC_SYSTEM_ITEMS I
6179        	AND      C1.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
6180        	AND      C1.CALENDAR_CODE = l_calendar_code
6181        	AND      C1.EXCEPTION_SET_ID = l_calendar_exception_set_id
6182                  --  Bug 3432530 Use local variables.
6183                  --  In case of common routing, use model's lead times.
6184        	AND      C2.SEQ_NUM = C1.PRIOR_SEQ_NUM - CEIL(((NVL(l_item_fixed_lt,0)+
6185                     NVL(l_item_var_lt,0)* p_requested_quantity) * (1+ l_mso_lead_time_factor))*
6186                        (1-NVL(SEQ.RESOURCE_OFFSET_PERCENT, 0)))
6187                  -- End Bug 3432530
6188        	AND      C2.CALENDAR_CODE = C1.CALENDAR_CODE
6189        	AND      C2.SR_INSTANCE_ID = C1.SR_INSTANCE_ID -- krajan : 2408696  -- cchen
6190        	AND      C2.EXCEPTION_SET_ID = C1.EXCEPTION_SET_ID
6191        	-- krajan: 2408696 - agilent
6192         -- AND   	MUC1.UOM_CODE = l_uom_code
6193        	-- AND   	MUC1.INVENTORY_ITEM_ID = 0
6194        	-- AND   	MUC2.UOM_CLASS = MUC1.UOM_CLASS
6195        	-- AND   	MUC2.INVENTORY_ITEM_ID = 0
6196        	-- AND   	MUC2.UOM_CODE = RES.UOM_CODE
6197         AND      RES.UOM_CODE = l_uom_code
6198        	AND      DR.PLAN_ID = RTG.PLAN_ID
6199        	AND      DR.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
6200        	AND      DR.ORGANIZATION_ID = RTG.ORGANIZATION_ID
6201        	AND      DR.RESOURCE_ID = RES.RESOURCE_ID
6202        	AND      DR.DEPARTMENT_ID = OP.DEPARTMENT_ID
6203         -- performance dsting remove nvl from dr.ctp_flag
6204        	AND      DR.CTP_FLAG = 1
6205        	--bug3601223 get the operations that lie on primary path
6206        	AND      (l_network_scheduling_method = 2
6207        	OR
6208        	         OP.OPERATION_SEQUENCE_ID IN
6209                               ( SELECT FROM_OP_SEQ_ID
6210                                 FROM  MSC_OPERATION_NETWORKS
6211                                 WHERE  PLAN_ID = p_plan_id
6212 				AND    SR_INSTANCE_ID = p_instance_id
6213 				AND    ROUTING_SEQUENCE_ID = p_routing_seq_id
6214 				AND    TRANSITION_TYPE = 1
6215 
6216                                 UNION ALL
6217 
6218                                 SELECT TO_OP_SEQ_ID
6219 				FROM  MSC_OPERATION_NETWORKS
6220 				WHERE  PLAN_ID = p_plan_id
6221 				AND    SR_INSTANCE_ID = p_instance_id
6222 				AND    ROUTING_SEQUENCE_ID = p_routing_seq_id
6223 				AND    TRANSITION_TYPE = 1
6224 			      )
6225 	          )
6226         UNION -- ALL
6227          -- Obtain Option Dependent Routing
6228        	SELECT   /*+ ordered */  DISTINCT DR.DEPARTMENT_ID department_id,
6229                  -- Distinct: Bug 3432530 For CTO we obtain the Op. Seq. related data only once.
6230                 	DR.OWNING_DEPARTMENT_ID owning_department_id,
6231                 	DR.RESOURCE_ID resource_id,
6232                 	RES.BASIS_TYPE basis_type,
6233                         --bug 3766224: Do not chnage usage for lot based resource
6234                         ROUND(DECODE(RES.BASIS_TYPE, 2, NVL(RES.RESOURCE_USAGE,0),
6235                 	(NVL(RES.RESOURCE_USAGE,0)*
6236                         -- krajan : 2408696
6237                         -- MUC2.CONVERSION_RATE/MUC1.CONVERSION_RATE, 0*
6238                         --bug3601223 Only if network_scheduling_method is planning percent then use % else 100%
6239                         Decode(l_network_scheduling_method,2,
6240                               (NVL(OP.NET_PLANNING_PERCENT,100)/100),1)
6241                         /DECODE(OP.REVERSE_CUMULATIVE_YIELD,0,1,NVL(OP.REVERSE_CUMULATIVE_YIELD,1)))
6242                         /(Decode (nvl (MSC_ATP_PVT.G_ORG_INFO_REC.org_type,MSC_ATP_PVT.DISCRETE_ORG), MSC_ATP_PVT.OPM_ORG, --Bug-4694958
6243                                   decode (RES.BASIS_TYPE, 3,
6244                                           NVL(DR.MAX_CAPACITY,1),  nvl(rtg.routing_quantity,1)
6245                                          ),
6246                                   nvl(rtg.routing_quantity,1)
6247                                  )
6248                          )),6) resource_usage, --4694958
6249                             -- Bug 2865389 (ssurendr) routing quantity added for OPM fix.
6250                 	C2.CALENDAR_DATE requested_date,
6251                         --  Bug 3432530 Use local variables.
6252                         --  In case of common routing, use model's lead times.
6253                 	CEIL(((NVL(l_item_fixed_lt,0)+
6254                         NVL(l_item_var_lt,0)* p_requested_quantity) * (1+ l_mso_lead_time_factor))*
6255                         (1-NVL(SEQ.RESOURCE_OFFSET_PERCENT, 0))) lead_time,
6256                         --  End Bug 3432530 Use local variables.
6257                 	NVL((DR.EFFICIENCY/100), 1) efficiency,
6258                 	NVL((DR.UTILIZATION/100), 1) utilization,
6259                         NVL(DR.BATCHABLE_FLAG, 2) batch_flag,
6260                         NVL(DR.MAX_CAPACITY,0) max_capacity,
6261                         --  Bug 3432530 Use local variables.
6262                         --  In case of common routing, use model's item data.
6263                         DECODE(DR.UOM_CLASS_TYPE, 1, l_item_unit_wt, 2, l_item_unit_vol) required_unit_capacity,
6264                         ---bug 1905284
6265                         DECODE(DR.UOM_CLASS_TYPE, 1, l_item_wt_uom, 2, l_item_vol_uom) required_capacity_uom ,
6266                         --  End Bug 3432530 Use local variables.
6267                         DR.UNIT_OF_MEASURE res_uom,
6268                         DR.UOM_CLASS_TYPE res_uom_type,
6269                         OP.STANDARD_OPERATION_CODE std_op_code,
6270                         --diag_atp
6271                         SEQ.RESOURCE_OFFSET_PERCENT resource_offset_percent,
6272                         OP.OPERATION_SEQ_NUM operation_sequence,
6273                         RES.RESOURCE_USAGE actual_resource_usage,
6274                         --NVL(OP.REVERSE_CUMULATIVE_YIELD, 1) reverse_cumulative_yield ,
6275                         DECODE(OP.REVERSE_CUMULATIVE_YIELD,0,1,NVL(OP.REVERSE_CUMULATIVE_YIELD, 1)) reverse_cumulative_yield ,--4694958
6276                         DR.Department_code department_code,
6277                         DR.resource_code resource_code
6278 
6279        	FROM
6280                 	MSC_CTO_BOM  mcbom1,
6281                         MSC_PROCESS_EFFECTIVITY proc,
6282                         MSC_CTO_BOM  mcbom2,
6283                         -- MSC_SYSTEM_ITEMS I, Bug 3432530 Comment out Join table
6284                 	MSC_ROUTINGS RTG,
6285                 	MSC_ROUTING_OPERATIONS OP,
6286                         MSC_BOM_COMPONENTS mbc,
6287                         MSC_OPERATION_COMPONENTS  moc,
6288                 	MSC_OPERATION_RESOURCE_SEQS SEQ,
6289                 	MSC_OPERATION_RESOURCES RES,
6290                         MSC_DEPARTMENT_RESOURCES DR, -- this is the sharing dept
6291                 	MSC_CALENDAR_DATES C1,
6292                 	MSC_CALENDAR_DATES C2
6293 
6294        	WHERE    mcbom1.session_id = MSC_ATP_PVT.G_SESSION_ID
6295         AND      mcbom1.sr_instance_id = p_instance_id
6296                  -- Bug 3358981 line is a model then include,
6297         AND      (mcbom1.ATO_PARENT_MODEL_LINE_ID = l_parent_line_id OR mcbom1.line_id = l_parent_line_id)
6298                  -- get all lines having the same parent model End Bug 3358981
6299         AND      mcbom1.bom_item_type in (1, 2)
6300         AND      mcbom1.inventory_item_id =
6301                        decode(MSC_ATP_PVT.G_ITEM_INFO_REC.bom_item_type,
6302                               1, mcbom1.inventory_item_id,
6303                               2, l_inventory_item_id)
6304         --AND      (mcbom1.parent_line_id = p_line_identifier or
6305                     -- Handle situation when parent_line_id is null.
6306                     -- Basic thing is that this section should handle all cases.
6307         --            mcbom1.inventory_item_id = l_inventory_item_id )
6308         -- Join to msc_process_effectivity
6309         AND      proc.plan_id = p_plan_id
6310         AND      proc.sr_instance_id = mcbom1.sr_instance_id
6311         AND      proc.organization_id = p_organization_id
6312         AND      proc.item_id  = mcbom1.inventory_item_id
6313                  -- Ensure that only items that have a common routing are processed with
6314                  -- the model. OC Items having a separate routing will be processed separately.
6315                  -- This check below with decode on the left side will be removed while
6316                  -- ones below that achieve the same thing retained if performance is an issue.
6317         AND      decode(MSC_ATP_PVT.G_ITEM_INFO_REC.bom_item_type,  1, -- bom_item_type for model
6318                          NVL(proc.routing_sequence_id, p_routing_seq_id),
6319                          proc.routing_sequence_id  -- all other cases including option_classes
6320                        ) =  p_routing_seq_id
6321         -- Quantity filter
6322         AND      mcbom1.quantity BETWEEN NVL(proc.minimum_quantity,0) AND
6323                   DECODE(NVL(proc.maximum_quantity,0),0,99999999,proc.maximum_quantity)
6324         -- Date Filter
6325         -- effective date should be greater than or equal to greatest of PTF date,
6326         -- sysdate and start date, disable date
6327         -- should be less than or equal to greatest of PTF date, sysdate and start date
6328         -- Note p_requested_date is used instead of C2.calendar_date currently,
6329         -- since p_requested_date is used in MSC_ATP_PROC.get_process_effectivity
6330         -- and also from performance considerations.
6331         AND   TRUNC(proc.effectivity_date) <=
6332                           TRUNC(GREATEST(p_requested_date, sysdate, MSC_ATP_PVT.G_PTF_DATE))
6333         AND   TRUNC(NVL(proc.disable_date,GREATEST(p_requested_date, sysdate, MSC_ATP_PVT.G_PTF_DATE)+1))
6334                > TRUNC(GREATEST(p_requested_date, sysdate, MSC_ATP_PVT.G_PTF_DATE))
6335         -- Join again to msc_cto_bom to obtain the components as well.
6336         AND     mcbom2.sr_instance_id = mcbom1.sr_instance_id
6337         AND     mcbom2.session_id = mcbom1.session_id
6338         AND     mcbom2.ato_parent_model_line_id = mcbom1.ATO_PARENT_MODEL_LINE_ID
6339         AND     NVL(mcbom2.parent_line_id, l_parent_line_id) = mcbom1.line_id
6340         -- to obtain all option classes that have a common routing.
6341         -- Get the routing
6342        	AND      RTG.PLAN_ID = proc.plan_id
6343        	AND      RTG.SR_INSTANCE_ID =  proc.sr_instance_id -- Qry streamline 3358981
6344        	AND      RTG.ORGANIZATION_ID = proc.organization_id
6345        	AND      RTG.ROUTING_SEQUENCE_ID =  NVL(proc.routing_sequence_id,
6346                                                  RTG.ROUTING_SEQUENCE_ID)
6347                  -- Bug 3432530
6348                  -- Comment out join conditions for msc_system_items
6349                   -- 3358981 Eliminate semi cartesian product, streamline query.
6350         AND      RTG.assembly_item_id = DECODE (proc.routing_sequence_id, NULL,
6351                                                proc.item_id, l_inventory_item_id )
6352         --(ssurendr) Bug 2865389 Removed condition for Alternate Routing designator as
6353         --we are accessing Routing by Routing sequance id.
6354         --We are Driving by routing table for performance gains.
6355        	--AND      RTG.ALTERNATE_ROUTING_DESIGNATOR IS NULL
6356         -- Join to system items
6357         -- AND      I.PLAN_ID = RTG.PLAN_ID
6358         -- AND      I.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
6359         -- AND      I.ORGANIZATION_ID = RTG.ORGANIZATION_ID
6360                  -- Bug 3358981
6361         -- AND      I.INVENTORY_ITEM_ID = RTG.assembly_item_id
6362                  -- 3358981 Eliminate semi cartesian product, streamline query.
6363                  -- Ensure that only items that have a common routing are processed with
6364                  -- the model. OC Items having a separate routing will be processed separately.
6365         -- AND      I.INVENTORY_ITEM_ID = DECODE (proc.routing_sequence_id, NULL,
6366         --                        RTG.assembly_item_id, l_inventory_item_id ) -- model's item_id
6367                  -- End Bug 3358981
6368                  -- End Bug 3432530
6369         --  Get all operations for the routing
6370        	AND      OP.PLAN_ID = RTG.PLAN_ID
6371        	AND      OP.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
6372        	AND      OP.ROUTING_SEQUENCE_ID = RTG.ROUTING_SEQUENCE_ID
6373                  -- filter only on those components that are in the pseudo bom
6374                  -- AND OP.option_dependent_flag = 1 --
6375         /* Operation is of type Event (Do not select process) */
6376         and      NVL(OP.operation_type,1 ) = 1
6377         /* rajjain 3008611
6378          * effective date should be greater than or equal to greatest of PTF date, sysdate and start date
6379          * disable date should be less than or equal to greatest of PTF date, sysdate and start date*/
6380        	AND      TRUNC(NVL(OP.DISABLE_DATE, GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE)+1)) >
6381         	         	TRUNC(GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE))
6382        	AND      TRUNC(OP.EFFECTIVITY_DATE) <=
6383          	      	TRUNC(GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE)) -- bug 1404312
6384         -- Validate Model's BOM in sales order with model's bom in manufacturing org.
6385         AND     mbc.sr_instance_id = RTG.sr_instance_id -- Qry streamline 3358981
6386         AND     mbc.plan_id =  RTG.plan_id
6387         AND     mbc.organization_id = RTG.organization_id
6388                 -- Bug 3358981
6389                 -- Ensure that only items that have a common routing are processed with
6390                 -- the model. OC Items having a separate routing will be processed separately.
6391         AND     mbc.bill_sequence_id = DECODE(proc.routing_sequence_id, NULL,
6392                                                 proc.bill_sequence_id, p_bill_seq_id)
6393                 -- End Bug 3358981
6394         AND     mbc.using_assembly_id = RTG.assembly_item_id -- Qry streamline 3358981
6395         AND      TRUNC(NVL(MBC.DISABLE_DATE, GREATEST(C2.CALENDAR_DATE,
6396                          sysdate, MSC_ATP_PVT.G_PTF_DATE)+1)) >
6397                TRUNC(GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE))
6398                      AND      TRUNC(MBC.EFFECTIVITY_DATE) <=
6399                TRUNC(GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE))
6400         AND    mbc.inventory_item_id  = mcbom2.inventory_item_id
6401                  -- Optional Items selected in the Sales Order
6402                  -- Select the option dependent operations which are needed
6403                  -- for the configuration
6404         -- Join to determine all the operations
6405         and      moc.plan_id = mbc.plan_id
6406         and      moc.sr_instance_id = mbc.sr_instance_id
6407         and      moc.organization_id = mbc.organization_id
6408         and      moc.bill_sequence_id = mbc.bill_sequence_id
6409         and      moc.component_sequence_id = mbc.component_sequence_id
6410         and      moc.routing_sequence_id = rtg.routing_sequence_id
6411         and      moc.operation_sequence_id = OP.operation_sequence_id
6412         -- Obtain the Resource Seq numbers.
6413        	AND      SEQ.PLAN_ID = OP.PLAN_ID
6414        	AND      SEQ.ROUTING_SEQUENCE_ID  = OP.ROUTING_SEQUENCE_ID
6415        	AND      SEQ.SR_INSTANCE_ID = OP.SR_INSTANCE_ID
6416        	AND      SEQ.OPERATION_SEQUENCE_ID = OP.OPERATION_SEQUENCE_ID
6417        	AND      RES.BASIS_TYPE in (1,2,3) --4694958
6418        	AND      RES.PLAN_ID = SEQ.PLAN_ID
6419        	AND      RES.ROUTING_SEQUENCE_ID = SEQ.ROUTING_SEQUENCE_ID
6420        	AND      RES.SR_INSTANCE_ID = SEQ.SR_INSTANCE_ID
6421        	AND      RES.OPERATION_SEQUENCE_ID = SEQ.OPERATION_SEQUENCE_ID
6422        	AND      RES.RESOURCE_SEQ_NUM = SEQ.RESOURCE_SEQ_NUM
6423        	AND      NVL(RES.ALTERNATE_NUMBER, 0) = 0 -- bug 1170698
6424        	AND      C1.CALENDAR_DATE = p_requested_date
6425                  -- Bug 3432530 Use RTG instead of MSC_SYSTEM_ITEMS I
6426        	AND      C1.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
6427        	AND      C1.CALENDAR_CODE = l_calendar_code
6428        	AND      C1.EXCEPTION_SET_ID = l_calendar_exception_set_id
6429                  --  Bug 3432530 Use local variables.
6430                  --  In case of common routing, use model's lead times.
6431        	AND      C2.SEQ_NUM = C1.PRIOR_SEQ_NUM - CEIL(((NVL(l_item_fixed_lt,0)+
6432                     NVL(l_item_var_lt,0)* p_requested_quantity) * (1+ l_mso_lead_time_factor))*
6433                        (1-NVL(SEQ.RESOURCE_OFFSET_PERCENT, 0)))
6434                  -- End Bug 3432530
6435        	AND      C2.CALENDAR_CODE = C1.CALENDAR_CODE
6436        	AND      C2.SR_INSTANCE_ID = C1.SR_INSTANCE_ID -- krajan : 2408696  -- cchen
6437        	AND      C2.EXCEPTION_SET_ID = C1.EXCEPTION_SET_ID
6438        	-- krajan: 2408696 - agilent
6439         -- AND   	MUC1.UOM_CODE = l_uom_code
6440        	-- AND   	MUC1.INVENTORY_ITEM_ID = 0
6441        	-- AND   	MUC2.UOM_CLASS = MUC1.UOM_CLASS
6442        	-- AND   	MUC2.INVENTORY_ITEM_ID = 0
6443        	-- AND   	MUC2.UOM_CODE = RES.UOM_CODE
6444         AND      RES.UOM_CODE = l_uom_code
6445        	AND      DR.PLAN_ID = RTG.PLAN_ID
6446        	AND      DR.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
6447        	AND      DR.ORGANIZATION_ID = RTG.ORGANIZATION_ID
6448        	AND      DR.RESOURCE_ID = RES.RESOURCE_ID
6449        	AND      DR.DEPARTMENT_ID = OP.DEPARTMENT_ID
6450         -- performance dsting remove nvl from dr.ctp_flag
6451        	AND      DR.CTP_FLAG = 1
6452         --bug3601223  get the operations that lie on primary path
6453        	AND      (l_network_scheduling_method = 2
6454        	OR
6455        	         OP.OPERATION_SEQUENCE_ID IN
6456                               ( SELECT FROM_OP_SEQ_ID
6457                                 FROM  MSC_OPERATION_NETWORKS
6458                                 WHERE  PLAN_ID = p_plan_id
6459 				AND    SR_INSTANCE_ID = p_instance_id
6460 				AND    ROUTING_SEQUENCE_ID = p_routing_seq_id
6461 				AND    TRANSITION_TYPE = 1
6462 
6463                                 UNION ALL
6464 
6465                                 SELECT TO_OP_SEQ_ID
6466 				FROM  MSC_OPERATION_NETWORKS
6467 				WHERE  PLAN_ID = p_plan_id
6468 				AND    SR_INSTANCE_ID = p_instance_id
6469 				AND    ROUTING_SEQUENCE_ID = p_routing_seq_id
6470 				AND    TRANSITION_TYPE = 1
6471 			      )
6472 	          )
6473 	)
6474         ORDER   BY  requested_date,    -- Bug 2313497 Ensure proper order in fetch
6475                     operation_sequence, resource_code;
6476        ELSE -- Not Processing CTO BOM Model or Option Class. ODR
6477       	SELECT  department_id,
6478                 owning_department_id,
6479         	resource_id,
6480            	basis_type,
6481            	resource_usage,
6482            	requested_date,
6483            	lead_time,
6484            	efficiency,
6485            	utilization,
6486                 batch_flag,
6487                 max_capacity,
6488                 required_unit_capacity,
6489                 required_capacity_uom,
6490                 res_uom,
6491                 res_uom_type,
6492                 std_op_code,
6493                 --diag_atp
6494                 resource_offset_percent,
6495                 operation_sequence,
6496                 actual_resource_usage,
6497                 reverse_cumulative_yield,
6498                 department_code,
6499                 resource_code
6500 
6501       	BULK COLLECT INTO l_res_requirements.department_id,
6502                       l_res_requirements.owning_department_id,
6503                       l_res_requirements.resource_id,
6504                       l_res_requirements.basis_type,
6505                       l_res_requirements.resource_usage,
6506                       l_res_requirements.requested_date,
6507                       l_res_requirements.lead_time,
6508                       l_res_requirements.efficiency,
6509                       l_res_requirements.utilization,
6510                       --- these columns have been added for resource batching
6511                       l_res_requirements.batch_flag,
6512                       l_res_requirements.max_capacity,
6513                       l_res_requirements.required_unit_capacity,
6514                       l_res_requirements.required_capacity_uom,
6515                       l_res_requirements.res_uom,
6516                       l_res_requirements.res_uom_type,
6517                       l_res_requirements.std_op_code,
6518                       ---diag_atp
6519                       l_res_requirements.resource_offset_percent,
6520                       l_res_requirements.operation_sequence,
6521                       l_res_requirements.actual_resource_usage,
6522                       l_res_requirements.reverse_cumulative_yield,
6523                       l_res_requirements.department_code,
6524                       l_res_requirements.resource_code
6525       	FROM (
6526        	SELECT   /*+ ordered */  DR.DEPARTMENT_ID department_id,
6527                 	DR.OWNING_DEPARTMENT_ID owning_department_id,
6528                 	DR.RESOURCE_ID resource_id,
6529                 	RES.BASIS_TYPE basis_type,
6530                         --bug 3766224: Do not chnage usage for lot based resource
6531                         ROUND(DECODE(RES.BASIS_TYPE, 2, NVL(RES.RESOURCE_USAGE,0),
6532                 	(NVL(RES.RESOURCE_USAGE,0)*
6533                         -- krajan : 2408696
6534                         -- MUC2.CONVERSION_RATE/MUC1.CONVERSION_RATE, 0*
6535                         --bug3601223 Only if network_scheduling_method is planning percent then use % else 100%
6536                         Decode(l_network_scheduling_method,2,
6537                         (NVL(OP.NET_PLANNING_PERCENT,100)/100),1)
6538                         /DECODE(OP.REVERSE_CUMULATIVE_YIELD,0,1,NVL(OP.REVERSE_CUMULATIVE_YIELD,1)))
6539                         /(Decode (nvl (MSC_ATP_PVT.G_ORG_INFO_REC.org_type,MSC_ATP_PVT.DISCRETE_ORG), MSC_ATP_PVT.OPM_ORG, --Bug-4694958
6540                                   decode (RES.BASIS_TYPE, 3,
6541                                           NVL(DR.MAX_CAPACITY,1),  nvl(rtg.routing_quantity,1)
6542                                          ),
6543                                   nvl(rtg.routing_quantity,1)
6544                                  )
6545                          )),6) resource_usage, --4694958
6546                             -- Bug 2865389 (ssurendr) routing quantity added for OPM fix.
6547                 	C2.CALENDAR_DATE requested_date,
6548                 	CEIL(((NVL(I.FIXED_LEAD_TIME,0)+
6549                         NVL(I.VARIABLE_LEAD_TIME,0)* p_requested_quantity) * (1+ l_mso_lead_time_factor))*
6550                         (1-NVL(SEQ.RESOURCE_OFFSET_PERCENT, 0))) lead_time,
6551                 	NVL((DR.EFFICIENCY/100), 1) efficiency,
6552                 	NVL((DR.UTILIZATION/100), 1) utilization,
6553                         NVL(DR.BATCHABLE_FLAG, 2) batch_flag,
6554                         NVL(DR.MAX_CAPACITY,0) max_capacity,
6555                         DECODE(DR.UOM_CLASS_TYPE, 1, I.UNIT_WEIGHT, 2, I.UNIT_VOLUME) required_unit_capacity,
6556                         ---bug 1905284
6557                         DECODE(DR.UOM_CLASS_TYPE, 1, I.WEIGHT_UOM, 2, I.VOLUME_UOM) required_capacity_uom ,
6558                         DR.UNIT_OF_MEASURE res_uom,
6559                         DR.UOM_CLASS_TYPE res_uom_type,
6560                         OP.STANDARD_OPERATION_CODE std_op_code,
6561                         --diag_atp
6562                         SEQ.RESOURCE_OFFSET_PERCENT resource_offset_percent,
6563                         OP.OPERATION_SEQ_NUM operation_sequence,
6564                         RES.RESOURCE_USAGE actual_resource_usage,
6565                         --NVL(OP.REVERSE_CUMULATIVE_YIELD, 1) reverse_cumulative_yield ,
6566                         DECODE(OP.REVERSE_CUMULATIVE_YIELD,0,1,NVL(OP.REVERSE_CUMULATIVE_YIELD, 1)) reverse_cumulative_yield ,--4694958
6567                         DR.Department_code department_code,
6568                         DR.resource_code resource_code
6569 
6570        	FROM
6571                         -- krajan : 2408696
6572                         --agilent chnages: since plan already store the data in right uom, we dont need to convert it
6573                 	-- MSC_UOM_CONVERSIONS MUC2,
6574                 	-- MSC_UOM_CONVERSIONS MUC1,
6575                 	MSC_SYSTEM_ITEMS  I,
6576                 	MSC_ROUTINGS RTG,
6577                 	MSC_ROUTING_OPERATIONS OP,
6578                 	MSC_OPERATION_RESOURCE_SEQS SEQ,
6579                 	MSC_OPERATION_RESOURCES RES,
6580                         MSC_DEPARTMENT_RESOURCES DR, -- this is the sharing dept
6581                 	MSC_CALENDAR_DATES C1,
6582                 	MSC_CALENDAR_DATES C2
6583 
6584        	WHERE    I.PLAN_ID = RTG.PLAN_ID
6585        	AND      I.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
6586        	AND      I.INVENTORY_ITEM_ID = RTG.ASSEMBLY_ITEM_ID
6587        	AND      I.ORGANIZATION_ID = RTG.ORGANIZATION_ID
6588        	AND      RTG.PLAN_ID = p_plan_id
6589        	AND      RTG.SR_INSTANCE_ID = p_instance_id
6590        	AND      RTG.ORGANIZATION_ID = p_organization_id
6591        	AND      RTG.ROUTING_SEQUENCE_ID = p_routing_seq_id
6592         --(ssurendr) Bug 2865389 Removed condition for Alternate Routing designator as
6593         --we are accessing Routing by Routing sequance id.
6594         --We are Driving by routing table for performance gains.
6595        	--AND      RTG.ALTERNATE_ROUTING_DESIGNATOR IS NULL
6596        	AND      OP.PLAN_ID = RTG.PLAN_ID
6597        	AND      OP.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
6598        	AND      OP.ROUTING_SEQUENCE_ID = RTG.ROUTING_SEQUENCE_ID
6599         /* rajjain 3008611
6600          * effective date should be greater than or equal to greatest of PTF date, sysdate and start date
6601          * disable date should be less than or equal to greatest of PTF date, sysdate and start date*/
6602        	AND      TRUNC(NVL(OP.DISABLE_DATE, GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE)+1)) >
6603         	         	TRUNC(GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE))
6604        	AND      TRUNC(OP.EFFECTIVITY_DATE) <=
6605          	      	TRUNC(GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE)) -- bug 1404312
6606        	AND      SEQ.PLAN_ID = OP.PLAN_ID
6607        	AND      SEQ.ROUTING_SEQUENCE_ID  = OP.ROUTING_SEQUENCE_ID
6608        	AND      SEQ.SR_INSTANCE_ID = OP.SR_INSTANCE_ID
6609        	AND      SEQ.OPERATION_SEQUENCE_ID = OP.OPERATION_SEQUENCE_ID
6610        	AND      RES.BASIS_TYPE in (1,2,3) --4694958
6611        	AND      RES.PLAN_ID = SEQ.PLAN_ID
6612        	AND      RES.ROUTING_SEQUENCE_ID = SEQ.ROUTING_SEQUENCE_ID
6613        	AND      RES.SR_INSTANCE_ID = SEQ.SR_INSTANCE_ID
6614        	AND      RES.OPERATION_SEQUENCE_ID = SEQ.OPERATION_SEQUENCE_ID
6615        	AND      RES.RESOURCE_SEQ_NUM = SEQ.RESOURCE_SEQ_NUM
6616        	AND      NVL(RES.ALTERNATE_NUMBER, 0) = 0 -- bug 1170698
6617        	AND      C1.CALENDAR_DATE = p_requested_date
6618        	AND      C1.SR_INSTANCE_ID = I.SR_INSTANCE_ID
6619        	AND      C1.CALENDAR_CODE = l_calendar_code
6620        	AND      C1.EXCEPTION_SET_ID = l_calendar_exception_set_id
6621        	AND      C2.SEQ_NUM = C1.PRIOR_SEQ_NUM - CEIL(((NVL(I.FIXED_LEAD_TIME,0)+
6622                         NVL(I.VARIABLE_LEAD_TIME,0)* p_requested_quantity) * (1+ l_mso_lead_time_factor))*
6623                         (1-NVL(SEQ.RESOURCE_OFFSET_PERCENT, 0)))
6624        	AND      C2.CALENDAR_CODE = C1.CALENDAR_CODE
6625        	AND      C2.SR_INSTANCE_ID = C1.SR_INSTANCE_ID -- krajan : 2408696  -- cchen
6626        	AND      C2.EXCEPTION_SET_ID = C1.EXCEPTION_SET_ID
6627        	-- krajan: 2408696 - agilent
6628         -- AND   	MUC1.UOM_CODE = l_uom_code
6629        	-- AND   	MUC1.INVENTORY_ITEM_ID = 0
6630        	-- AND   	MUC2.UOM_CLASS = MUC1.UOM_CLASS
6631        	-- AND   	MUC2.INVENTORY_ITEM_ID = 0
6632        	-- AND   	MUC2.UOM_CODE = RES.UOM_CODE
6633         AND      RES.UOM_CODE = l_uom_code
6634        	AND      DR.PLAN_ID = I.PLAN_ID
6635        	AND      DR.SR_INSTANCE_ID = I.SR_INSTANCE_ID
6636        	AND      DR.ORGANIZATION_ID = I.ORGANIZATION_ID
6637        	AND      DR.RESOURCE_ID = RES.RESOURCE_ID
6638        	AND      DR.DEPARTMENT_ID = OP.DEPARTMENT_ID
6639         -- performance dsting remove nvl from dr.ctp_flag
6640        	AND      DR.CTP_FLAG = 1
6641        	--bug3601223  get the operations that lie on primary path
6642        	AND      (l_network_scheduling_method = 2
6643        	OR
6644        	          OP.OPERATION_SEQUENCE_ID IN
6645                               ( SELECT FROM_OP_SEQ_ID
6646                                 FROM  MSC_OPERATION_NETWORKS
6647                                 WHERE  PLAN_ID = p_plan_id
6648 				AND    SR_INSTANCE_ID = p_instance_id
6649 				AND    ROUTING_SEQUENCE_ID = p_routing_seq_id
6650 				AND    TRANSITION_TYPE = 1
6651 
6652                                 UNION ALL
6653 
6654                                 SELECT TO_OP_SEQ_ID
6655 				FROM  MSC_OPERATION_NETWORKS
6656 				WHERE  PLAN_ID = p_plan_id
6657 				AND    SR_INSTANCE_ID = p_instance_id
6658 				AND    ROUTING_SEQUENCE_ID = p_routing_seq_id
6659 				AND    TRANSITION_TYPE = 1
6660 			      )
6661 	           )
6662 
6663 	)
6664         ORDER   BY  requested_date,   -- Bug 2313497 Ensure proper order in fetch
6665                     operation_sequence, resource_code;
6666        END IF;
6667        -- End CTO Option Dependent Resources ODR
6668       ELSE -- traditional routing
6669        -- CTO Option Dependent Resources
6670        -- Option Dependent Routing ODR Determination
6671        IF MSC_ATP_PVT.G_ITEM_INFO_REC.bom_item_type in (1, 2) THEN
6672       	 SELECT DISTINCT            -- collapse common into one in case union all is used.
6673                 -- Uncomment: Bug 3432530 For CTO we obtain the Op. Seq. related data only once.
6674                 department_id,
6675            	owning_department_id,
6676            	resource_id,
6677            	basis_type,
6678            	resource_usage,
6679            	requested_date,
6680            	lead_time,
6681            	efficiency,
6682            	utilization,
6683                 batch_flag,
6684                 max_capacity,
6685                 required_unit_capacity,
6686                 required_capacity_uom,
6687                 res_uom,
6688                 res_uom_type,
6689                 std_op_code,
6690                 --diag_atp
6691                 resource_offset_percent,
6692                 operation_sequence,
6693                 actual_resource_usage,
6694                 reverse_cumulative_yield,
6695                 department_code,
6696                 resource_code
6697       	BULK COLLECT INTO l_res_requirements.department_id,
6698                       	l_res_requirements.owning_department_id,
6699                       	l_res_requirements.resource_id,
6700                       	l_res_requirements.basis_type,
6701                       	l_res_requirements.resource_usage,
6702                       	l_res_requirements.requested_date,
6703                       	l_res_requirements.lead_time,
6704                       	l_res_requirements.efficiency,
6705                       	l_res_requirements.utilization,
6706                         --- the following columns are added for resource batching
6707                         l_res_requirements.batch_flag,
6708                         l_res_requirements.max_capacity,
6709                         l_res_requirements.required_unit_capacity,
6710                         l_res_requirements.required_capacity_uom,
6711 			l_res_requirements.res_uom,
6712                         l_res_requirements.res_uom_type,
6713                         l_res_requirements.std_op_code,
6714                         ---diag_atp
6715                         l_res_requirements.resource_offset_percent,
6716                         l_res_requirements.operation_sequence,
6717                         l_res_requirements.actual_resource_usage,
6718                         l_res_requirements.reverse_cumulative_yield,
6719                         l_res_requirements.department_code,
6720                         l_res_requirements.resource_code
6721 
6722       	FROM (
6723          -- First select mandatory operations
6724          -- for common routing cases the mandatory for option classes
6725          -- will be clubbed together with the model.
6726        	SELECT  /*+ ordered */ DISTINCT DR.DEPARTMENT_ID department_id,
6727                 -- Distinct: Bug 3432530 For CTO we obtain the Op. Seq. related data only once.
6728                 	DR.OWNING_DEPARTMENT_ID owning_department_id,
6729                 	DR.RESOURCE_ID resource_id,
6730                 	RES.BASIS_TYPE basis_type,
6731                         --bug 3766224: Do not chnage usage for lot based resource
6732                         ROUND(DECODE(RES.BASIS_TYPE, 2, NVL(RES.RESOURCE_USAGE,0),
6733                 	(NVL(RES.RESOURCE_USAGE,0)
6734                         -- krajan : 2408696
6735                         -- MUC2.CONVERSION_RATE/MUC1.CONVERSION_RATE, 0
6736                         /DECODE(OP.REVERSE_CUMULATIVE_YIELD,0,1,NVL(OP.REVERSE_CUMULATIVE_YIELD,1)))
6737                         /(Decode (nvl (MSC_ATP_PVT.G_ORG_INFO_REC.org_type,MSC_ATP_PVT.DISCRETE_ORG), MSC_ATP_PVT.OPM_ORG, --Bug-4694958
6738                                   decode (RES.BASIS_TYPE, 3,
6739                                           NVL(DR.MAX_CAPACITY,1),  nvl(rtg.routing_quantity,1)
6740                                          ),
6741                                   nvl(rtg.routing_quantity,1)
6742                                  )
6743                          )),6) resource_usage, --4694958
6744                             -- Bug 2865389 (ssurendr) routing quantity added for OPM fix.
6745 			--(NVL(OP.NET_PLANNING_PERCENT,100)/100)/NVL(OP.REVERSE_CUMULATIVE_YIELD,1) resource_usage,
6746                 	C2.CALENDAR_DATE requested_date,
6747                         --  Bug 3432530 Use local variables.
6748                         --  In case of common routing, use model's lead times.
6749                 	CEIL(((NVL(l_item_fixed_lt,0)+
6750                         NVL(l_item_var_lt,0)* p_requested_quantity) * (1+ l_mso_lead_time_factor))*
6751                         (1-NVL(SEQ.RESOURCE_OFFSET_PERCENT, 0))) lead_time,
6752                         --  End Bug 3432530 Use local variables.
6753                 	NVL((DR.EFFICIENCY/100), 1) efficiency,
6754                 	NVL((DR.UTILIZATION/100), 1) utilization,
6755                         NVL(DR.BATCHABLE_FLAG, 2) batch_flag,
6756                         NVL(DR.MAX_CAPACITY,0) max_capacity,
6757                         --  Bug 3432530 Use local variables.
6758                         --  In case of common routing, use model's item data.
6759                         DECODE(DR.UOM_CLASS_TYPE, 1, l_item_unit_wt, 2, l_item_unit_vol) required_unit_capacity,
6760                         ---bug 1905284
6761                         DECODE(DR.UOM_CLASS_TYPE, 1, l_item_wt_uom, 2, l_item_vol_uom) required_capacity_uom ,
6762                         --  End Bug 3432530 Use local variables.
6763                         DR.UNIT_OF_MEASURE res_uom,
6764                         DR.UOM_CLASS_TYPE res_uom_type,
6765                         OP.STANDARD_OPERATION_CODE std_op_code,
6766                         --diag_atp
6767                         SEQ.RESOURCE_OFFSET_PERCENT resource_offset_percent,
6768                         OP.OPERATION_SEQ_NUM operation_sequence,
6769                         RES.RESOURCE_USAGE actual_resource_usage,
6770                         --NVL(OP.REVERSE_CUMULATIVE_YIELD, 1) reverse_cumulative_yield,
6771                         DECODE(OP.REVERSE_CUMULATIVE_YIELD,0,1,NVL(OP.REVERSE_CUMULATIVE_YIELD, 1)) reverse_cumulative_yield,--4694958
6772                         DR.Department_code Department_code,
6773                         DR.Resource_code Resource_code
6774         FROM
6775                 	MSC_CTO_BOM  mcbom1,
6776                         -- MSC_SYSTEM_ITEMS I, Bug 3432530 Comment out Join table
6777                 	MSC_ROUTINGS RTG,
6778                 	MSC_ROUTING_OPERATIONS OP,
6779                 	MSC_OPERATION_RESOURCE_SEQS SEQ,
6780                 	MSC_OPERATION_RESOURCES RES,
6781                         MSC_DEPARTMENT_RESOURCES DR, -- this is the sharing dept
6782                 	MSC_CALENDAR_DATES C1,
6783                 	MSC_CALENDAR_DATES C2
6784 
6785        	WHERE    mcbom1.session_id = MSC_ATP_PVT.G_SESSION_ID
6786         AND      mcbom1.sr_instance_id = p_instance_id
6787                  -- Bug 3358981 line is a model then include,
6788         AND      (mcbom1.ATO_PARENT_MODEL_LINE_ID = l_parent_line_id OR mcbom1.line_id = l_parent_line_id)
6789                  -- get all lines having the same parent model End Bug 3358981
6790         AND      (  --mcbom1.parent_line_id = p_line_identifier or
6791                     -- Handle situation when parent_line_id is null.
6792                     -- Basic thing is that this section should handle all cases.
6793                     mcbom1.inventory_item_id = l_inventory_item_id )
6794         AND      mcbom1.quantity <> 0
6795         -- Get the routing
6796        	AND      RTG.PLAN_ID = p_plan_id
6797        	AND      RTG.SR_INSTANCE_ID = mcbom1.sr_instance_id
6798        	AND      RTG.ORGANIZATION_ID = p_organization_id
6799        	AND      RTG.ROUTING_SEQUENCE_ID = p_routing_seq_id -- For common routing this will be null.
6800                  -- Bug 3432530
6801                  -- Comment out join conditions for msc_system_items
6802                   -- 3358981 Eliminate semi cartesian product, streamline query.
6803         AND      RTG.assembly_item_id = mcbom1.inventory_item_id
6804         -- Join to system items
6805         -- AND      I.PLAN_ID = RTG.PLAN_ID
6806         -- AND      I.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
6807         -- AND      I.ORGANIZATION_ID = RTG.ORGANIZATION_ID
6808         -- AND      I.INVENTORY_ITEM_ID = RTG.assembly_item_id
6809                  -- 3358981 Eliminate semi cartesian product, streamline query.
6810         -- AND      I.INVENTORY_ITEM_ID = l_inventory_item_id
6811                  -- End Bug 3432530
6812         --(ssurendr) Bug 2865389 Removed condition for Alternate Routing designator as
6813         --we are accessing Routing by Routing sequance id.
6814         --We are Driving by routing table for performance gains.
6815        	--AND      RTG.ALTERNATE_ROUTING_DESIGNATOR IS NULL
6816         --  Get all operations for the routing
6817        	AND      OP.PLAN_ID = RTG.PLAN_ID
6818        	AND      OP.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
6819        	AND      OP.ROUTING_SEQUENCE_ID = RTG.ROUTING_SEQUENCE_ID
6820          /* Operation is of type Event (Do not select process) */
6821         and      NVL(OP.operation_type,1 ) = 1
6822         /* rajjain 3008611
6823          * effective date should be greater than or equal to greatest of PTF date, sysdate and start date
6824          * disable date should be less than or equal to greatest of PTF date, sysdate and start date*/
6825        	AND      TRUNC(NVL(OP.DISABLE_DATE, GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE)+1)) >
6826                   	TRUNC(GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE))
6827        	AND      TRUNC(OP.EFFECTIVITY_DATE) <=
6828                   	TRUNC(GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE)) -- bug 1404312
6829                  -- SMCs/Mandatory Operations
6830         and     OP.option_dependent_flag = 2
6831                  -- for the configuration
6832         -- Obtain the Resource Seq numbers.
6833        	AND      SEQ.PLAN_ID = OP.PLAN_ID
6834        	AND      SEQ.ROUTING_SEQUENCE_ID  = OP.ROUTING_SEQUENCE_ID
6835        	AND      SEQ.SR_INSTANCE_ID = OP.SR_INSTANCE_ID
6836        	AND      SEQ.OPERATION_SEQUENCE_ID = OP.OPERATION_SEQUENCE_ID
6837        	AND      RES.BASIS_TYPE in (1,2,3) --4694958
6838        	AND      RES.PLAN_ID = SEQ.PLAN_ID
6839        	AND      RES.ROUTING_SEQUENCE_ID = SEQ.ROUTING_SEQUENCE_ID
6840        	AND      RES.SR_INSTANCE_ID = SEQ.SR_INSTANCE_ID
6841        	AND      RES.OPERATION_SEQUENCE_ID = SEQ.OPERATION_SEQUENCE_ID
6842        	AND      RES.RESOURCE_SEQ_NUM = SEQ.RESOURCE_SEQ_NUM
6843        	AND      NVL(RES.ALTERNATE_NUMBER, 0) = 0 -- bug 1170698
6844        	AND      C1.CALENDAR_DATE = p_requested_date
6845                  -- Bug 3432530 Use RTG instead of MSC_SYSTEM_ITEMS I
6846        	AND      C1.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
6847        	AND      C1.CALENDAR_CODE = l_calendar_code
6848        	AND      C1.EXCEPTION_SET_ID = l_calendar_exception_set_id
6849                  --  Bug 3432530 Use local variables.
6850                  --  In case of common routing, use model's lead times.
6851        	AND      C2.SEQ_NUM = C1.PRIOR_SEQ_NUM - CEIL(((NVL(l_item_fixed_lt,0)+
6852                     NVL(l_item_var_lt,0)* p_requested_quantity) * (1+ l_mso_lead_time_factor))*
6853                        (1-NVL(SEQ.RESOURCE_OFFSET_PERCENT, 0)))
6854                  -- End Bug 3432530
6855        	AND      C2.CALENDAR_CODE = C1.CALENDAR_CODE
6856        	AND      C2.SR_INSTANCE_ID = C1.SR_INSTANCE_ID -- krajan : 2408696
6857        	AND      C2.EXCEPTION_SET_ID = C1.EXCEPTION_SET_ID
6858        	-- krajan : 2408696
6859         -- AND   	MUC1.UOM_CODE = l_uom_code
6860        	-- AND   	MUC1.INVENTORY_ITEM_ID = 0
6861        	-- AND   	MUC2.UOM_CLASS = MUC1.UOM_CLASS
6862        	-- AND   	MUC2.INVENTORY_ITEM_ID = 0
6863        	-- AND   	MUC2.UOM_CODE = RES.UOM_CODE
6864         AND      RES.UOM_CODE = l_uom_code
6865        	AND      DR.PLAN_ID = RTG.PLAN_ID
6866        	AND      DR.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
6867         AND     DR.ORGANIZATION_ID = RTG.ORGANIZATION_ID
6868        	AND      DR.RESOURCE_ID = RES.RESOURCE_ID
6869        	AND      DR.DEPARTMENT_ID = OP.DEPARTMENT_ID
6870 	-- performance dsting remove nvl from dr.ctp_flag
6871        	AND      DR.CTP_FLAG = 1
6872        	UNION ALL
6873          -- Obtain Option Dependent Routing
6874        	SELECT  /*+ ordered */ DISTINCT DR.DEPARTMENT_ID department_id,
6875                 -- Distinct: Bug 3432530 For CTO we obtain the Op. Seq. related data only once.
6876                 	DR.OWNING_DEPARTMENT_ID owning_department_id,
6877                 	DR.RESOURCE_ID resource_id,
6878                 	RES.BASIS_TYPE basis_type,
6879                         --bug 3766224: Do not chnage usage for lot based resource
6880                         ROUND(DECODE(RES.BASIS_TYPE, 2, NVL(RES.RESOURCE_USAGE,0),
6881                 	(NVL(RES.RESOURCE_USAGE,0)
6882                         -- krajan : 2408696
6883                         -- MUC2.CONVERSION_RATE/MUC1.CONVERSION_RATE, 0
6884                         /DECODE(OP.REVERSE_CUMULATIVE_YIELD,0,1,NVL(OP.REVERSE_CUMULATIVE_YIELD,1)))
6885                         /(Decode (nvl (MSC_ATP_PVT.G_ORG_INFO_REC.org_type,MSC_ATP_PVT.DISCRETE_ORG), MSC_ATP_PVT.OPM_ORG, --Bug-4694958
6886                                   decode (RES.BASIS_TYPE, 3,
6887                                           NVL(DR.MAX_CAPACITY,1),  nvl(rtg.routing_quantity,1)
6888                                          ),
6889                                   nvl(rtg.routing_quantity,1)
6890                                  )
6891                          )),6) resource_usage, --4694958
6892                             -- Bug 2865389 (ssurendr) routing quantity added for OPM fix.
6893 			--(NVL(OP.NET_PLANNING_PERCENT,100)/100)/NVL(OP.REVERSE_CUMULATIVE_YIELD,1) resource_usage,
6894                 	C2.CALENDAR_DATE requested_date,
6895                         --  Bug 3432530 Use local variables.
6896                         --  In case of common routing, use model's lead times.
6897                 	CEIL(((NVL(l_item_fixed_lt,0)+
6898                         NVL(l_item_var_lt,0)* p_requested_quantity) * (1+ l_mso_lead_time_factor))*
6899                         (1-NVL(SEQ.RESOURCE_OFFSET_PERCENT, 0))) lead_time,
6900                         --  End Bug 3432530 Use local variables.
6901                 	NVL((DR.EFFICIENCY/100), 1) efficiency,
6902                 	NVL((DR.UTILIZATION/100), 1) utilization,
6903                         NVL(DR.BATCHABLE_FLAG, 2) batch_flag,
6904                         NVL(DR.MAX_CAPACITY,0) max_capacity,
6905                         --  Bug 3432530 Use local variables.
6906                         --  In case of common routing, use model's item data.
6907                         DECODE(DR.UOM_CLASS_TYPE, 1, l_item_unit_wt, 2, l_item_unit_vol) required_unit_capacity,
6908                         ---bug 1905284
6909                         DECODE(DR.UOM_CLASS_TYPE, 1, l_item_wt_uom, 2, l_item_vol_uom) required_capacity_uom ,
6910                         --  End Bug 3432530 Use local variables.
6911                         DR.UNIT_OF_MEASURE res_uom,
6912                         DR.UOM_CLASS_TYPE res_uom_type,
6913                         OP.STANDARD_OPERATION_CODE std_op_code,
6914                         --diag_atp
6915                         SEQ.RESOURCE_OFFSET_PERCENT resource_offset_percent,
6916                         OP.OPERATION_SEQ_NUM operation_sequence,
6917                         RES.RESOURCE_USAGE actual_resource_usage,
6918                         --NVL(OP.REVERSE_CUMULATIVE_YIELD, 1) reverse_cumulative_yield,
6919                         DECODE(OP.REVERSE_CUMULATIVE_YIELD,0,1,NVL(OP.REVERSE_CUMULATIVE_YIELD, 1)) reverse_cumulative_yield,--4694958
6920                         DR.Department_code Department_code,
6921                         DR.Resource_code Resource_code
6922         FROM
6923                 	MSC_CTO_BOM  mcbom1,
6924                         MSC_PROCESS_EFFECTIVITY proc,
6925                         MSC_CTO_BOM  mcbom2,
6926                         -- MSC_SYSTEM_ITEMS I, Bug 3432530 Comment out Join table
6927                 	MSC_ROUTINGS RTG,
6928                 	MSC_ROUTING_OPERATIONS OP,
6929                         MSC_BOM_COMPONENTS mbc,
6930                         MSC_OPERATION_COMPONENTS  moc,
6931                 	MSC_OPERATION_RESOURCE_SEQS SEQ,
6932                 	MSC_OPERATION_RESOURCES RES,
6933                         MSC_DEPARTMENT_RESOURCES DR, -- this is the sharing dept
6934                 	MSC_CALENDAR_DATES C1,
6935                 	MSC_CALENDAR_DATES C2
6936 
6937        	WHERE    mcbom1.session_id = MSC_ATP_PVT.G_SESSION_ID
6938         AND      mcbom1.sr_instance_id = p_instance_id
6939                  -- Bug 3358981 line is a model then include,
6940         AND      (mcbom1.ATO_PARENT_MODEL_LINE_ID = l_parent_line_id OR mcbom1.line_id = l_parent_line_id)
6941                  -- get all lines having the same parent model End Bug 3358981
6942         AND      mcbom1.bom_item_type in (1, 2)
6943         AND      mcbom1.inventory_item_id =
6944                        decode(MSC_ATP_PVT.G_ITEM_INFO_REC.bom_item_type,
6945                               1, mcbom1.inventory_item_id,
6946                               2, l_inventory_item_id)
6947         --AND      (mcbom1.parent_line_id = p_line_identifier or
6948                     -- Handle situation when parent_line_id is null.
6949                     -- Basic thing is that this section should handle all cases.
6950         --            mcbom1.inventory_item_id = l_inventory_item_id )
6951          -- Join to msc_process_effectivity
6952         AND      proc.plan_id = p_plan_id
6953         AND      proc.sr_instance_id = mcbom1.sr_instance_id
6954         AND      proc.organization_id = p_organization_id
6955         AND      proc.item_id  = mcbom1.inventory_item_id
6956                  -- Ensure that only items that have a common routing are processed with
6957                  -- the model. OC Items having a separate routing will be processed separately.
6958                  -- This check below with decode on the left side will be removed while
6959                  -- ones below that achieve the same thing retained if performance is an issue.
6960         AND      decode(MSC_ATP_PVT.G_ITEM_INFO_REC.bom_item_type, 1, -- bom_item_type for model
6961                          NVL(proc.routing_sequence_id, p_routing_seq_id),
6962                          proc.routing_sequence_id  -- all other cases including option_classes
6963                        ) =  p_routing_seq_id
6964         -- Quantity filter
6965         AND      mcbom1.quantity BETWEEN NVL(proc.minimum_quantity,0) AND
6966                   DECODE(NVL(proc.maximum_quantity,0),0,99999999,proc.maximum_quantity)
6967         -- Date Filter
6968         -- effective date should be greater than or equal to greatest of PTF date,
6969         -- sysdate and start date, disable date
6970         -- should be less than or equal to greatest of PTF date, sysdate and start date
6971         -- Note p_requested_date is used instead of C2.calendar_date currently,
6972         -- since p_requested_date is used in MSC_ATP_PROC.get_process_effectivity
6973         -- and also from performance considerations.
6974         AND   TRUNC(proc.effectivity_date) <=
6975                           TRUNC(GREATEST(p_requested_date, sysdate, MSC_ATP_PVT.G_PTF_DATE))
6976         AND   TRUNC(NVL(proc.disable_date,GREATEST(p_requested_date, sysdate, MSC_ATP_PVT.G_PTF_DATE)+1))
6977                > TRUNC(GREATEST(p_requested_date, sysdate, MSC_ATP_PVT.G_PTF_DATE))
6978         -- Join again to msc_cto_bom to obtain the components as well.
6979         AND     mcbom2.sr_instance_id = mcbom1.sr_instance_id
6980         AND     mcbom2.session_id = mcbom1.session_id
6981         AND     mcbom2.ato_parent_model_line_id = mcbom1.ATO_PARENT_MODEL_LINE_ID
6982         AND     NVL(mcbom2.parent_line_id, l_parent_line_id) = mcbom1.line_id
6983         -- to obtain all option classes that have a common routing.
6984         -- Get the routing
6985        	AND      RTG.PLAN_ID = proc.plan_id
6986        	AND      RTG.SR_INSTANCE_ID = proc.sr_instance_id -- Qry Streamline 3358981
6987        	AND      RTG.ORGANIZATION_ID = proc.organization_id
6988        	AND      RTG.ROUTING_SEQUENCE_ID = NVL(proc.routing_sequence_id,
6989                                                  RTG.ROUTING_SEQUENCE_ID)
6990                  -- Bug 3432530
6991                  -- Comment out join conditions for msc_system_items
6992                   -- 3358981 Eliminate semi cartesian product, streamline query.
6993         AND      RTG.assembly_item_id = DECODE (proc.routing_sequence_id, NULL,
6994                                                proc.item_id, l_inventory_item_id )
6995         -- Join to system items
6996         -- AND      I.PLAN_ID = RTG.PLAN_ID
6997         -- AND      I.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
6998         -- AND      I.ORGANIZATION_ID = RTG.ORGANIZATION_ID
6999                  -- Bug 3358981
7000         -- AND      I.INVENTORY_ITEM_ID = RTG.assembly_item_id
7001                  -- 3358981 Eliminate semi cartesian product, streamline query.
7002                  -- Ensure that only items that have a common routing are processed with
7003                  -- the model. OC Items having a separate routing will be processed separately.
7004         -- AND      I.INVENTORY_ITEM_ID = DECODE (proc.routing_sequence_id, NULL,
7005         --                        RTG.assembly_item_id, l_inventory_item_id ) -- model's item_id
7006                  -- End Bug 3358981
7007                  -- End Bug 3432530
7008         --(ssurendr) Bug 2865389 Removed condition for Alternate Routing designator as
7009         --we are accessing Routing by Routing sequance id.
7010         --We are Driving by routing table for performance gains.
7011        	--AND      RTG.ALTERNATE_ROUTING_DESIGNATOR IS NULL
7012         --  Get all operations for the routing
7013        	AND      OP.PLAN_ID = RTG.PLAN_ID
7014        	AND      OP.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
7015        	AND      OP.ROUTING_SEQUENCE_ID = RTG.ROUTING_SEQUENCE_ID
7016                  -- filter only on those components that are in the pseudo bom
7017                  -- AND OP.option_dependent_flag = 1 --
7018          /* Operation is of type Event (Do not select process) */
7019         and      NVL(OP.operation_type,1 ) = 1
7020         /* rajjain 3008611
7021          * effective date should be greater than or equal to greatest of PTF date, sysdate and start date
7022          * disable date should be less than or equal to greatest of PTF date, sysdate and start date*/
7023        	AND      TRUNC(NVL(OP.DISABLE_DATE, GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE)+1)) >
7024                   	TRUNC(GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE))
7025        	AND      TRUNC(OP.EFFECTIVITY_DATE) <=
7026                   	TRUNC(GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE)) -- bug 1404312
7027         -- Validate Model's BOM in sales order with model's bom in manufacturing org.
7028         AND     mbc.sr_instance_id = RTG.sr_instance_id -- Qry Streamline 3358981
7029         AND     mbc.plan_id =  RTG.plan_id
7030         AND     mbc.organization_id = RTG.organization_id
7031                 -- Bug 3358981
7032                 -- Ensure that only items that have a common routing are processed with
7033                 -- the model. OC Items having a separate routing will be processed separately.
7034         AND     mbc.bill_sequence_id = DECODE(proc.routing_sequence_id, NULL,
7035                                                 proc.bill_sequence_id, p_bill_seq_id)
7036                 -- End Bug 3358981
7037         AND     mbc.using_assembly_id = RTG.assembly_item_id -- Qry Streamline 3358981
7038         AND      TRUNC(NVL(MBC.DISABLE_DATE, GREATEST(C2.CALENDAR_DATE,
7039                          sysdate, MSC_ATP_PVT.G_PTF_DATE)+1)) >
7040                TRUNC(GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE))
7041                      AND      TRUNC(MBC.EFFECTIVITY_DATE) <=
7042                TRUNC(GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE))
7043         AND     mbc.inventory_item_id  = mcbom2.inventory_item_id
7044                  -- Optional Items selected in the Sales Order
7045                  -- Select the option dependent operations which are needed.
7046                  -- for the configuration
7047         -- Join to determine all the operations
7048         and     moc.plan_id = mbc.plan_id
7049         and     moc.sr_instance_id = mbc.sr_instance_id
7050         and     moc.organization_id = mbc.organization_id
7051         and     moc.bill_sequence_id = mbc.bill_sequence_id
7052         and     moc.component_sequence_id = mbc.component_sequence_id
7053         and     moc.routing_sequence_id = rtg.routing_sequence_id
7054         and     moc.operation_sequence_id = OP.operation_sequence_id
7055         -- Obtain the Resource Seq numbers.
7056        	AND      SEQ.PLAN_ID = OP.PLAN_ID
7057        	AND      SEQ.ROUTING_SEQUENCE_ID  = OP.ROUTING_SEQUENCE_ID
7058        	AND      SEQ.SR_INSTANCE_ID = OP.SR_INSTANCE_ID
7059        	AND      SEQ.OPERATION_SEQUENCE_ID = OP.OPERATION_SEQUENCE_ID
7060        	AND      RES.BASIS_TYPE in (1,2,3) --4694958
7061        	AND      RES.PLAN_ID = SEQ.PLAN_ID
7062        	AND      RES.ROUTING_SEQUENCE_ID = SEQ.ROUTING_SEQUENCE_ID
7063        	AND      RES.SR_INSTANCE_ID = SEQ.SR_INSTANCE_ID
7064        	AND      RES.OPERATION_SEQUENCE_ID = SEQ.OPERATION_SEQUENCE_ID
7065        	AND      RES.RESOURCE_SEQ_NUM = SEQ.RESOURCE_SEQ_NUM
7066        	AND      NVL(RES.ALTERNATE_NUMBER, 0) = 0 -- bug 1170698
7067        	AND      C1.CALENDAR_DATE = p_requested_date
7068                  -- Bug 3432530 Use RTG instead of MSC_SYSTEM_ITEMS I
7069        	AND      C1.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
7070        	AND      C1.CALENDAR_CODE = l_calendar_code
7071        	AND      C1.EXCEPTION_SET_ID = l_calendar_exception_set_id
7072                  --  Bug 3432530 Use local variables.
7073                  --  In case of common routing, use model's lead times.
7074        	AND      C2.SEQ_NUM = C1.PRIOR_SEQ_NUM - CEIL(((NVL(l_item_fixed_lt,0)+
7075                     NVL(l_item_var_lt,0)* p_requested_quantity) * (1+ l_mso_lead_time_factor))*
7076                        (1-NVL(SEQ.RESOURCE_OFFSET_PERCENT, 0)))
7077                  -- End Bug 3432530
7078        	AND      C2.CALENDAR_CODE = C1.CALENDAR_CODE
7079        	AND      C2.SR_INSTANCE_ID = C1.SR_INSTANCE_ID -- krajan : 2408696
7080        	AND      C2.EXCEPTION_SET_ID = C1.EXCEPTION_SET_ID
7081        	-- krajan : 2408696
7082         -- AND   	MUC1.UOM_CODE = l_uom_code
7083        	-- AND   	MUC1.INVENTORY_ITEM_ID = 0
7084        	-- AND   	MUC2.UOM_CLASS = MUC1.UOM_CLASS
7085        	-- AND   	MUC2.INVENTORY_ITEM_ID = 0
7086        	-- AND   	MUC2.UOM_CODE = RES.UOM_CODE
7087         AND      RES.UOM_CODE = l_uom_code
7088        	AND      DR.PLAN_ID = RTG.PLAN_ID
7089        	AND      DR.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
7090         AND     DR.ORGANIZATION_ID = RTG.ORGANIZATION_ID
7091        	AND      DR.RESOURCE_ID = RES.RESOURCE_ID
7092        	AND      DR.DEPARTMENT_ID = OP.DEPARTMENT_ID
7093 	-- performance dsting remove nvl from dr.ctp_flag
7094        	AND      DR.CTP_FLAG = 1
7095        	UNION ALL
7096        	SELECT   RTG.LINE_ID department_id,
7097                 	RTG.LINE_ID owning_department_id ,
7098                 	-1 resource_id,
7099                 	1 basis_type,
7100                 	1 resource_usage,
7101                 	C2.CALENDAR_DATE requested_date,
7102                 	CEIL((NVL(I.FIXED_LEAD_TIME,0)+
7103                    	NVL(I.VARIABLE_LEAD_TIME,0)* p_requested_quantity) * (1+ l_mso_lead_time_factor)) lead_time,
7104                 	1 efficiency,
7105                 	1 utilization,
7106                         2 batch_flag,
7107                         0 max_capacity,
7108                         0 required_unit_capacity,
7109                         --bug 2845383: Change all direct reference to null to local variables
7110                         l_null_char required_capacity_uom,
7111                         l_null_char res_uom,
7112                         1 res_uom_type,
7113                         l_null_char std_op_code,
7114                         --diag_atp
7115                         l_null_num resource_offset_percent,
7116                         l_null_num operation_sequence,
7117                         1 actual_resource_usage,
7118                         l_null_num reverse_cumulative_yield,
7119                         l_null_char department_code,
7120                         l_null_char resource_Code
7121        	FROM 	MSC_CALENDAR_DATES C2,
7122                 	MSC_CALENDAR_DATES C1,
7123                 	MSC_ROUTINGS RTG,
7124                 	MSC_SYSTEM_ITEMS  I
7125        	WHERE    I.PLAN_ID = RTG.PLAN_ID
7126        	AND      I.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
7127        	AND      I.INVENTORY_ITEM_ID = RTG.ASSEMBLY_ITEM_ID
7128        	AND      I.ORGANIZATION_ID = RTG.ORGANIZATION_ID
7129        	AND      RTG.PLAN_ID = p_plan_id
7130        	AND      RTG.SR_INSTANCE_ID = p_instance_id
7131        	AND      RTG.ORGANIZATION_ID = p_organization_id
7132        	AND      RTG.ROUTING_SEQUENCE_ID = p_routing_seq_id
7133         --(ssurendr) Bug 2865389 Removed condition for Alternate Routing designator as
7134         --we are accessing Routing by Routing sequance id.
7135         --We are Driving by routing table for performance gains.
7136        	--AND      RTG.ALTERNATE_ROUTING_DESIGNATOR IS NULL
7137        	AND	RTG.CTP_FLAG = 1
7138        	AND      RTG.LINE_ID IS NOT NULL
7139        	AND      C1.CALENDAR_DATE = p_requested_date
7140        	AND      C1.SR_INSTANCE_ID = I.SR_INSTANCE_ID
7141        	AND      C1.CALENDAR_CODE = l_calendar_code
7142        	AND      C1.EXCEPTION_SET_ID = l_calendar_exception_set_id
7143        	AND      C2.SEQ_NUM = C1.PRIOR_SEQ_NUM - CEIL((NVL(I.FIXED_LEAD_TIME,0)+
7144                         	NVL(I.VARIABLE_LEAD_TIME,0)* p_requested_quantity) * (1+ l_mso_lead_time_factor))
7145        	AND      C2.CALENDAR_CODE = C1.CALENDAR_CODE
7146        	AND      C2.SR_INSTANCE_ID = I.SR_INSTANCE_ID
7147        	AND      C2.EXCEPTION_SET_ID = C1.EXCEPTION_SET_ID
7148       	)
7149         ORDER by requested_date,     -- Bug 2313497 Ensure proper order in fetch
7150                     operation_sequence, resource_code;
7151        ELSE -- Not Processing CTO BOM Model or Option Class. ODR
7152       	SELECT department_id,
7153            	owning_department_id,
7154            	resource_id,
7155            	basis_type,
7156            	resource_usage,
7157            	requested_date,
7158            	lead_time,
7159            	efficiency,
7160            	utilization,
7161                 batch_flag,
7162                 max_capacity,
7163                 required_unit_capacity,
7164                 required_capacity_uom,
7165                 res_uom,
7166                 res_uom_type,
7167                 std_op_code,
7168                 --diag_atp
7169                 resource_offset_percent,
7170                 operation_sequence,
7171                 actual_resource_usage,
7172                 reverse_cumulative_yield,
7173                 department_code,
7174                 resource_code
7175       	BULK COLLECT INTO l_res_requirements.department_id,
7176                       	l_res_requirements.owning_department_id,
7177                       	l_res_requirements.resource_id,
7178                       	l_res_requirements.basis_type,
7179                       	l_res_requirements.resource_usage,
7180                       	l_res_requirements.requested_date,
7181                       	l_res_requirements.lead_time,
7182                       	l_res_requirements.efficiency,
7183                       	l_res_requirements.utilization,
7184                         --- the following columns are added for resource batching
7185                         l_res_requirements.batch_flag,
7186                         l_res_requirements.max_capacity,
7187                         l_res_requirements.required_unit_capacity,
7188                         l_res_requirements.required_capacity_uom,
7189 			l_res_requirements.res_uom,
7190                         l_res_requirements.res_uom_type,
7191                         l_res_requirements.std_op_code,
7192                         ---diag_atp
7193                         l_res_requirements.resource_offset_percent,
7194                         l_res_requirements.operation_sequence,
7195                         l_res_requirements.actual_resource_usage,
7196                         l_res_requirements.reverse_cumulative_yield,
7197                         l_res_requirements.department_code,
7198                         l_res_requirements.resource_code
7199 
7200       	FROM (
7201        	SELECT  /*+ ordered */  DR.DEPARTMENT_ID department_id,
7202                 	DR.OWNING_DEPARTMENT_ID owning_department_id,
7203                 	DR.RESOURCE_ID resource_id,
7204                 	RES.BASIS_TYPE basis_type,
7205                         --bug 3766224: Do not chnage usage for lot based resource
7206                         ROUND(DECODE(RES.BASIS_TYPE, 2, NVL(RES.RESOURCE_USAGE,0),
7207                 	(NVL(RES.RESOURCE_USAGE,0)
7208                         -- krajan : 2408696
7209                         -- MUC2.CONVERSION_RATE/MUC1.CONVERSION_RATE, 0
7210                         /DECODE(OP.REVERSE_CUMULATIVE_YIELD,0,1,NVL(OP.REVERSE_CUMULATIVE_YIELD,1)))
7211                         /(Decode (nvl (MSC_ATP_PVT.G_ORG_INFO_REC.org_type,MSC_ATP_PVT.DISCRETE_ORG), MSC_ATP_PVT.OPM_ORG, --Bug-4694958
7212                                   decode (RES.BASIS_TYPE, 3,
7213                                           NVL(DR.MAX_CAPACITY,1),  nvl(rtg.routing_quantity,1)
7214                                          ),
7215                                   nvl(rtg.routing_quantity,1)
7216                                  )
7217                          )),6) resource_usage, --4694958
7218                             -- Bug 2865389 (ssurendr) routing quantity added for OPM fix.
7219 			--(NVL(OP.NET_PLANNING_PERCENT,100)/100)/NVL(OP.REVERSE_CUMULATIVE_YIELD,1) resource_usage,
7220                 	C2.CALENDAR_DATE requested_date,
7221                 	CEIL(((NVL(I.FIXED_LEAD_TIME,0)+
7222                         	NVL(I.VARIABLE_LEAD_TIME,0)* p_requested_quantity) * (1+ l_mso_lead_time_factor))*
7223                         	(1-NVL(SEQ.RESOURCE_OFFSET_PERCENT, 0))) lead_time,
7224                 	NVL((DR.EFFICIENCY/100), 1) efficiency,
7225                 	NVL((DR.UTILIZATION/100), 1) utilization,
7226                         NVL(DR.BATCHABLE_FLAG, 2) batch_flag,
7227                         NVL(DR.MAX_CAPACITY,0) max_capacity,
7228                         DECODE(DR.UOM_CLASS_TYPE, 1, I.UNIT_WEIGHT, 2, I.UNIT_VOLUME) required_unit_capacity,
7229                         --1905284
7230                         DECODE(DR.UOM_CLASS_TYPE, 1, I.WEIGHT_UOM, 2, I.VOLUME_UOM) required_capacity_uom,
7231                         DR.UNIT_OF_MEASURE res_uom,
7232                         DR.UOM_CLASS_TYPE res_uom_type,
7233                         OP.STANDARD_OPERATION_CODE std_op_code,
7234                         --diag_atp
7235                         SEQ.RESOURCE_OFFSET_PERCENT resource_offset_percent,
7236                         OP.OPERATION_SEQ_NUM operation_sequence,
7237                         RES.RESOURCE_USAGE actual_resource_usage,
7238                         --NVL(OP.REVERSE_CUMULATIVE_YIELD, 1) reverse_cumulative_yield,
7239                         DECODE(OP.REVERSE_CUMULATIVE_YIELD,0,1,NVL(OP.REVERSE_CUMULATIVE_YIELD, 1)) reverse_cumulative_yield,--4694958
7240                         DR.Department_code Department_code,
7241                         DR.Resource_code Resource_code
7242         FROM
7243                  MSC_SYSTEM_ITEMS I,
7244                  MSC_ROUTINGS  RTG,
7245                  MSC_ROUTING_OPERATIONS OP,
7246                  MSC_OPERATION_RESOURCE_SEQS SEQ,
7247                  MSC_OPERATION_RESOURCES RES,
7248                  MSC_DEPARTMENT_RESOURCES DR,
7249                  MSC_CALENDAR_DATES C1,
7250                  MSC_CALENDAR_DATES C2
7251 
7252        	WHERE    I.PLAN_ID = RTG.PLAN_ID
7253        	AND      I.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
7254        	AND      I.INVENTORY_ITEM_ID = RTG.ASSEMBLY_ITEM_ID
7255        	AND      I.ORGANIZATION_ID = RTG.ORGANIZATION_ID
7256        	AND      RTG.PLAN_ID = p_plan_id
7257        	AND      RTG.SR_INSTANCE_ID = p_instance_id
7258        	AND      RTG.ORGANIZATION_ID = p_organization_id
7259        	AND      RTG.ROUTING_SEQUENCE_ID = p_routing_seq_id
7260         --(ssurendr) Bug 2865389 Removed condition for Alternate Routing designator as
7261         --we are accessing Routing by Routing sequance id.
7262         --We are Driving by routing table for performance gains.
7263        	--AND      RTG.ALTERNATE_ROUTING_DESIGNATOR IS NULL
7264        	AND      OP.PLAN_ID = RTG.PLAN_ID
7265        	AND      OP.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
7266        	AND      OP.ROUTING_SEQUENCE_ID = RTG.ROUTING_SEQUENCE_ID
7267         /* rajjain 3008611
7268          * effective date should be greater than or equal to greatest of PTF date, sysdate and start date
7269          * disable date should be less than or equal to greatest of PTF date, sysdate and start date*/
7270        	AND      TRUNC(NVL(OP.DISABLE_DATE, GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE)+1)) >
7271                   	TRUNC(GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE))
7272        	AND      TRUNC(OP.EFFECTIVITY_DATE) <=
7273                   	TRUNC(GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE)) -- bug 1404312
7274        	AND      SEQ.PLAN_ID = OP.PLAN_ID
7275        	AND      SEQ.ROUTING_SEQUENCE_ID  = OP.ROUTING_SEQUENCE_ID
7276        	AND      SEQ.SR_INSTANCE_ID = OP.SR_INSTANCE_ID
7277        	AND      SEQ.OPERATION_SEQUENCE_ID = OP.OPERATION_SEQUENCE_ID
7278        	AND      RES.BASIS_TYPE in (1,2,3) --4694958
7279        	AND      RES.PLAN_ID = SEQ.PLAN_ID
7280        	AND      RES.ROUTING_SEQUENCE_ID = SEQ.ROUTING_SEQUENCE_ID
7281        	AND      RES.SR_INSTANCE_ID = SEQ.SR_INSTANCE_ID
7282        	AND      RES.OPERATION_SEQUENCE_ID = SEQ.OPERATION_SEQUENCE_ID
7283        	AND      RES.RESOURCE_SEQ_NUM = SEQ.RESOURCE_SEQ_NUM
7284        	AND      NVL(RES.ALTERNATE_NUMBER, 0) = 0 -- bug 1170698
7285        	AND      C1.CALENDAR_DATE = p_requested_date
7286        	AND      C1.SR_INSTANCE_ID = I.SR_INSTANCE_ID
7287        	AND      C1.CALENDAR_CODE = l_calendar_code
7288        	AND      C1.EXCEPTION_SET_ID = l_calendar_exception_set_id
7289        	AND      C2.SEQ_NUM = C1.PRIOR_SEQ_NUM - CEIL(((NVL(I.FIXED_LEAD_TIME,0)+
7290                         	NVL(I.VARIABLE_LEAD_TIME,0)* p_requested_quantity) * (1+ l_mso_lead_time_factor))*
7291                         	(1-NVL(SEQ.RESOURCE_OFFSET_PERCENT, 0)))
7292        	AND      C2.CALENDAR_CODE = C1.CALENDAR_CODE
7293        	AND      C2.SR_INSTANCE_ID = C1.SR_INSTANCE_ID -- krajan : 2408696
7294        	AND      C2.EXCEPTION_SET_ID = C1.EXCEPTION_SET_ID
7295        	-- krajan : 2408696
7296         -- AND   	MUC1.UOM_CODE = l_uom_code
7297        	-- AND   	MUC1.INVENTORY_ITEM_ID = 0
7298        	-- AND   	MUC2.UOM_CLASS = MUC1.UOM_CLASS
7299        	-- AND   	MUC2.INVENTORY_ITEM_ID = 0
7300        	-- AND   	MUC2.UOM_CODE = RES.UOM_CODE
7301         AND      RES.UOM_CODE = l_uom_code
7302        	AND      DR.PLAN_ID = I.PLAN_ID
7303        	AND      DR.SR_INSTANCE_ID = I.SR_INSTANCE_ID
7304         AND     DR.ORGANIZATION_ID = I.ORGANIZATION_ID
7305        	AND      DR.RESOURCE_ID = RES.RESOURCE_ID
7306        	AND      DR.DEPARTMENT_ID = OP.DEPARTMENT_ID
7307 	-- performance dsting remove nvl from dr.ctp_flag
7308        	AND      DR.CTP_FLAG = 1
7309        	UNION ALL
7310        	SELECT   RTG.LINE_ID department_id,
7311                 	RTG.LINE_ID owning_department_id ,
7312                 	-1 resource_id,
7313                 	1 basis_type,
7314                 	1 resource_usage,
7315                 	C2.CALENDAR_DATE requested_date,
7316                 	CEIL((NVL(I.FIXED_LEAD_TIME,0)+
7317                    	NVL(I.VARIABLE_LEAD_TIME,0)* p_requested_quantity) * (1+ l_mso_lead_time_factor)) lead_time,
7318                 	1 efficiency,
7319                 	1 utilization,
7320                         2 batch_flag,
7321                         0 max_capacity,
7322                         0 required_unit_capacity,
7323                         --bug 2845383: Change all direct reference to null to local variables
7324                         l_null_char required_capacity_uom,
7325                         l_null_char res_uom,
7326                         1 res_uom_type,
7327                         l_null_char std_op_code,
7328                         --diag_atp
7329                         l_null_num resource_offset_percent,
7330                         l_null_num operation_sequence,
7331                         1 actual_resource_usage,
7332                         l_null_num reverse_cumulative_yield,
7333                         l_null_char department_code,
7334                         l_null_char resource_Code
7335        	FROM 	MSC_CALENDAR_DATES C2,
7336                 	MSC_CALENDAR_DATES C1,
7337                 	MSC_ROUTINGS RTG,
7338                 	MSC_SYSTEM_ITEMS  I
7339        	WHERE    I.PLAN_ID = RTG.PLAN_ID
7340        	AND      I.SR_INSTANCE_ID = RTG.SR_INSTANCE_ID
7341        	AND      I.INVENTORY_ITEM_ID = RTG.ASSEMBLY_ITEM_ID
7342        	AND      I.ORGANIZATION_ID = RTG.ORGANIZATION_ID
7343        	AND      RTG.PLAN_ID = p_plan_id
7344        	AND      RTG.SR_INSTANCE_ID = p_instance_id
7345        	AND      RTG.ORGANIZATION_ID = p_organization_id
7346        	AND      RTG.ROUTING_SEQUENCE_ID = p_routing_seq_id
7347         --(ssurendr) Bug 2865389 Removed condition for Alternate Routing designator as
7348         --we are accessing Routing by Routing sequance id.
7349         --We are Driving by routing table for performance gains.
7350        	--AND      RTG.ALTERNATE_ROUTING_DESIGNATOR IS NULL
7351        	AND	RTG.CTP_FLAG = 1
7352        	AND      RTG.LINE_ID IS NOT NULL
7353        	AND      C1.CALENDAR_DATE = p_requested_date
7354        	AND      C1.SR_INSTANCE_ID = I.SR_INSTANCE_ID
7355        	AND      C1.CALENDAR_CODE = l_calendar_code
7356        	AND      C1.EXCEPTION_SET_ID = l_calendar_exception_set_id
7357        	AND      C2.SEQ_NUM = C1.PRIOR_SEQ_NUM - CEIL((NVL(I.FIXED_LEAD_TIME,0)+
7358                         	NVL(I.VARIABLE_LEAD_TIME,0)* p_requested_quantity) * (1+ l_mso_lead_time_factor))
7359        	AND      C2.CALENDAR_CODE = C1.CALENDAR_CODE
7360        	AND      C2.SR_INSTANCE_ID = I.SR_INSTANCE_ID
7361        	AND      C2.EXCEPTION_SET_ID = C1.EXCEPTION_SET_ID
7362       	)
7363         ORDER by requested_date,     -- Bug 2313497 Ensure proper order in fetch
7364                     operation_sequence, resource_code;
7365        END IF;
7366        -- End CTO Option Dependent Resources ODR
7367       END IF;  ---- If l_routing_flag = 3 THEN
7368      ELSE
7369 
7370       IF PG_DEBUG in ('Y', 'C') THEN
7371          msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'using BOR');
7372       END IF;
7373 
7374       -- Added on 01/09/2001 by ngoel for performance improvement
7375       /* Modularize Item and Org Info */
7376       l_inv_item_id  :=  MSC_ATP_PVT.G_ITEM_INFO_REC.dest_inv_item_id;
7377       /* Modularize Item and Org Info */
7378       IF PG_DEBUG in ('Y', 'C') THEN
7379          msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Modular change l_inv_item_id : '||l_inv_item_id);
7380       END IF;
7381 
7382       SELECT b.department_id,
7383            dr.owning_department_id,
7384            b.resource_id,
7385            b.basis,
7386            DECODE(b.resource_id, -1, 1, b.resource_department_hours),
7387            c2.calendar_date,
7388            b.setback_days,
7389            nvl((dr.efficiency/100), 1),
7390            nvl((dr.utilization/100), 1),
7391            --- even though resource batching is not used in case of BOR
7392            --- the following columns are added because the process
7393            -- for all cases is done by the same code. Adding following
7394            --- columns will ensure proper extension of tables
7395            2 batch_flag,
7396            0 max_capacity,
7397            0 required_unit_capacity,
7398            --2845383: change direct refrence to null to local varibale
7399            l_null_char required_capacity_uom,
7400            l_null_char res_uom,
7401            l_null_char res_uom_type,
7402            l_null_char  std_op_code,
7403            --diag_atp
7404            l_null_num resource_offset_percent,
7405            l_null_num operation_sequence,
7406            DECODE(b.resource_id, -1, 1, b.resource_department_hours) actual_resource_usage,
7407            1 reverse_cumulative_yield,
7408            dr.department_code department_code,
7409            dr.resource_code resource_Code
7410 
7411       BULK COLLECT INTO l_res_requirements.department_id,
7412                       l_res_requirements.owning_department_id,
7413                       l_res_requirements.resource_id,
7414                       l_res_requirements.basis_type,
7415                       l_res_requirements.resource_usage,
7416                       l_res_requirements.requested_date,
7417                       l_res_requirements.lead_time,
7418                       l_res_requirements.efficiency,
7419                       l_res_requirements.utilization,
7420                        --- the following columns are added for resource batching
7421                       l_res_requirements.batch_flag,
7422                       l_res_requirements.max_capacity,
7423                       l_res_requirements.required_unit_capacity,
7424                       l_res_requirements.required_capacity_uom ,
7425                       l_res_requirements.res_uom,
7426                       l_res_requirements.res_uom_type,
7427                       l_res_requirements.std_op_code,
7428                       ---diag_atp
7429                       l_res_requirements.resource_offset_percent,
7430                       l_res_requirements.operation_sequence,
7431                       l_res_requirements.actual_resource_usage,
7432                       l_res_requirements.reverse_cumulative_yield,
7433                       l_res_requirements.department_code,
7434                       l_res_requirements.resource_code
7435 
7436       FROM msc_department_resources dr,
7437            msc_bor_requirements b,
7438            msc_calendar_dates c1,
7439            msc_calendar_dates c2
7440       WHERE B.PLAN_ID = p_plan_id
7441       AND   B.SR_INSTANCE_ID = p_instance_id
7442       AND   B.ORGANIZATION_ID = p_organization_id
7443       AND   B.ASSEMBLY_ITEM_ID = l_inv_item_id
7444 
7445 	-- Chnaged on 01/09/2001 by ngoel for performance improvement
7446 	-- MSC_ATP_FUNC.get_inv_item_id(p_instance_id, p_inventory_item_id, p_organization_id)
7447       AND   C1.CALENDAR_DATE = p_requested_date
7448       AND   C1.SR_INSTANCE_ID = B.SR_INSTANCE_ID
7449       AND   C1.CALENDAR_CODE = l_calendar_code
7450       AND   C1.EXCEPTION_SET_ID = l_calendar_exception_set_id
7451       AND   C2.SEQ_NUM = C1.PRIOR_SEQ_NUM - B.SETBACK_DAYS
7452       AND   C2.CALENDAR_CODE = C1.CALENDAR_CODE
7453       AND   C2.SR_INSTANCE_ID = B.SR_INSTANCE_ID
7454       AND   C2.EXCEPTION_SET_ID = C1.EXCEPTION_SET_ID
7455       AND   DR.PLAN_ID = B.PLAN_ID
7456       AND   DR.SR_INSTANCE_ID = B.SR_INSTANCE_ID
7457       AND   DR.RESOURCE_ID = B.RESOURCE_ID
7458       AND   DR.DEPARTMENT_ID = B.DEPARTMENT_ID
7459       AND   DR.ORGANIZATION_ID = B.ORGANIZATION_ID
7460       -- performance dsting remove nvl from dr.ctp_flag
7461       AND   DECODE(DR.LINE_FLAG, 1, 1, DR.CTP_FLAG) = 1
7462       AND   (DR.LINE_FLAG <> 1
7463             OR
7464             (DR.LINE_FLAG = 1 AND
7465              EXISTS ( SELECT 'CTP'
7466                       FROM   MSC_ROUTINGS RTG
7467                       WHERE  RTG.PLAN_ID = p_plan_id
7468       		      AND    RTG.SR_INSTANCE_ID = B.SR_INSTANCE_ID
7469                       AND    RTG.ASSEMBLY_ITEM_ID = B.ASSEMBLY_ITEM_ID
7470                       AND    RTG.ORGANIZATION_ID = B.ORGANIZATION_ID
7471                       AND    RTG.ROUTING_SEQUENCE_ID = p_routing_seq_id
7472                       --(ssurendr) Bug 2865389 OPM fix
7473                       -- AND    RTG.ALTERNATE_ROUTING_DESIGNATOR IS NULL
7474                       AND    RTG.LINE_ID = B.DEPARTMENT_ID
7475                       AND    NVL(RTG.CTP_FLAG, 2) = 1)))
7476       ORDER BY C2.CALENDAR_DATE;  -- Bug 2313497 Ensure proper order in fetch
7477 
7478 
7479     END IF;
7480       IF PG_DEBUG in ('Y', 'C') THEN
7481          msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'After getting resource information');
7482          msc_sch_wb.atp_debug('Get_Res_Requirements: ' ||
7483                'l_res_requirements.resource_id.COUNT ' || l_res_requirements.resource_id.COUNT);
7484       END IF;
7485       -- go over each resource
7486       j := l_res_requirements.resource_id.FIRST;
7487 
7488       -- if j is null, that means we don't have any resource requirements
7489       -- need to consider, so we can assume that we have infinite resource
7490       -- to make the assembly.
7491       -- otherwise we need to know how many assemblies we can make
7492       -- by loop through each resource and find the availibility.
7493       -- If we can make more than the requested quantity, we return
7494       -- the requested quantity.
7495 
7496       -- initially set the x_avail_assembly_qty to the p_request_quantity,
7497       -- and we adjust that later
7498 
7499       -- Initialize l_infinite_time_fence_date, since this is pds,
7500       -- use planning's cutoff date as the infinite time fence date
7501       IF PG_DEBUG in ('Y', 'C') THEN
7502          msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'x_atp_date := ' || x_atp_date);
7503       END IF;
7504       IF j IS NOT NULL THEN
7505          ---bug 2341075: read plan start date
7506 
7507          -- Supplier Capacity and Lead Time (SCLT) Proj
7508          -- Commented out
7509          -- SELECT curr_cutoff_date, trunc(plan_start_date)
7510          -- INTO   l_infinite_time_fence_date, l_plan_start_date
7511          -- FROM   msc_plans
7512          -- WHERE  plan_id = p_plan_id;
7513 
7514          -- Instead re-assigned local values using global variable
7515          l_plan_start_date := MSC_ATP_PVT.G_PLAN_INFO_REC.plan_start_date;
7516          --l_infinite_time_fence_date := MSC_ATP_PVT.G_PLAN_INFO_REC.curr_cutoff_date; (ssurendr) Bug 2865389
7517          l_infinite_time_fence_date := MSC_ATP_PVT.G_PLAN_INFO_REC.plan_cutoff_date;
7518          IF PG_DEBUG in ('Y', 'C') THEN
7519             msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Plan Start Date := ' || l_plan_start_date );
7520             msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Infinite Time Fence := ' ||
7521                                                                l_infinite_time_fence_date );
7522          END IF;
7523          -- End Supplier Capacity and Lead Time Proj
7524 
7525       END IF;
7526 
7527 
7528       ---- bug 1950528
7529       ---- Get assembly item id if 1) there is resource def 2) MSC_corpod is on 3) Its not a BOR
7530       IF (j IS NOT NULL) AND (MSC_ATP_PVT.G_PLAN_COPRODUCTS = 'Y') AND (l_use_bor <> 1)
7531          AND (NVL (MSC_ATP_PVT.G_ORG_INFO_REC.org_type,MSC_ATP_PVT.DISCRETE_ORG) = MSC_ATP_PVT.DISCRETE_ORG) THEN  --Bug-4694958
7532            BEGIN
7533               SELECT BOMS.ASSEMBLY_QUANTITY
7534               INTO l_assembly_quantity
7535               FROM MSC_BOMS BOMS
7536               WHERE    BOMS.PLAN_ID  = p_plan_id
7537               AND      BOMS.SR_INSTANCE_ID  = p_instance_id
7538               AND      BOMS.ORGANIZATION_ID  = p_organization_id
7539               AND      BOMS.BILL_SEQUENCE_ID = p_bill_seq_id;
7540               --(ssurendr) Bug 2865389 Removed condition for Alternate bom designator as
7541               --we are accessing bom by bill sequance id. Also removed MSC_SYSTEM_ITEMS from the SQL
7542               --AND      BOMS.ALTERNATE_BOM_DESIGNATOR IS NULL;
7543            EXCEPTION
7544               WHEN OTHERS THEN
7545                   l_assembly_quantity := 1;
7546            END;
7547       END IF;
7548       IF PG_DEBUG in ('Y', 'C') THEN
7549          msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_assembly_quantity := ' || l_assembly_quantity);
7550       END IF;
7551 
7552 
7553       --// BUG 2313497, 2126520
7554       res_count := l_res_requirements.resource_id.COUNT;
7555       --// BUG 2313497, 2126520
7556       WHILE j IS NOT NULL LOOP
7557 
7558          -- Bug 1610561
7559          -- Now perform infinite_time_fence_date processing for each resource.
7560          -- First Initialize the infinite_time_fence_date to NULL
7561 
7562          l_infinite_time_fence_date := NULL;
7563 
7564          -- Resource Id and Department ID assignment is moved up to here.
7565 
7566          l_resource_id := l_res_requirements.resource_id(j);
7567          l_department_id := NVL(l_res_requirements.owning_department_id(j),
7568                                 l_res_requirements.department_id(j));
7569          -- Now obtain the infinite time fence date if an ATP rule is specified.
7570 
7571          -- Bug 3036513 Get infinite time fence date for resource
7572          -- Existing SQL commented out.
7573          -- Call the Library routine that is now common for both items and resources.
7574 
7575          MSC_ATP_PROC.get_infinite_time_fence_date ( p_instance_id,
7576                                                      l_inv_item_id,
7577                                                      p_organization_id,
7578                                                      p_plan_id,
7579                                                      l_infinite_time_fence_date,
7580                                                      l_atp_rule_name,
7581                                                      l_resource_id,
7582                                                      l_department_id );
7583 
7584          IF PG_DEBUG in ('Y', 'C') THEN
7585             msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Resource Id := ' ||
7586                                                                       l_resource_id);
7587             msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'New Infinite Time Fence := '
7588                                                           || l_infinite_time_fence_date );
7589             msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Plan Cutoff Date := ' ||
7590                                                 MSC_ATP_PVT.G_PLAN_INFO_REC.plan_cutoff_date);
7591             msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'ATP RULE NAME for Resource := '
7592                                                           || l_atp_rule_name );
7593          END IF;
7594          -- Bug 3036513
7595 
7596          -- End Bug 1610561
7597 
7598         --diag_atp
7599         l_pegging_rec.operation_sequence_id := null;
7600         l_pegging_rec.usage := null;
7601         l_pegging_rec.offset := null;
7602         l_pegging_rec.efficiency := null;
7603         l_pegging_rec.utilization := null;
7604         l_pegging_rec.REVERSE_CUM_YIELD := null;
7605         l_pegging_rec.owning_department := null;
7606         l_pegging_rec.pegging_type := null;
7607         l_pegging_rec.required_quantity:=null;
7608         l_pegging_rec.required_date := null;
7609         l_pegging_rec.basis_type := null;
7610         l_pegging_rec.allocation_rule := null;
7611         l_pegging_rec.constraint_type := null;
7612         l_pegging_rec.actual_supply_demand_date := null;
7613 
7614         /** BUG 2313497, 2126520  Check Resource Availability on End Date **/
7615         -- Changing the code to check Resource Availability on
7616         -- resource End Date instead of start date.
7617         -- Typically, if there are two resources
7618         -- R1 and R2 then We calculate the Availability of R1 until
7619         -- R2 is about to be used. i.e R2's start date becomes
7620         -- R1's End date and thus we do availability check on R1's
7621         -- End date.
7622 
7623         IF j = res_count THEN
7624           l_requested_date := p_requested_date;
7625           l_lead_time := 0;
7626 
7627           IF PG_DEBUG in ('Y', 'C') THEN
7628              msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Setting the Lead time 0 for forward case');
7629           END IF;
7630         ELSE
7631           -- Check the the start date of the next resource and
7632           -- Use it as End date of current resource by
7633           -- storing the index. If next resource's Start date
7634           -- is same as current resource's start date then
7635           -- move in the loop until we find the resource whose
7636           -- start date is less then current resource's start date.
7637           For h in j+1..res_count
7638             LOOP
7639               IF PG_DEBUG in ('Y', 'C') THEN
7640                  msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'h = ' || h);
7641                  msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'J' ||l_res_requirements.requested_date(j));
7642                  msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'H' ||l_res_requirements.requested_date(h));
7643               END IF;
7644               IF l_res_requirements.requested_date(j)
7645                 < l_res_requirements.requested_date(h) THEN
7646                    -- Bug 3348095
7647                    -- Assign the Resource start Date
7648                    l_res_start_date := l_res_requirements.requested_date(j);
7649                    IF PG_DEBUG in ('Y', 'C') THEN
7650                       msc_sch_wb.atp_debug('Get_Res_Requirements: Init. l_res_start_date ' ||
7651                                                                          l_res_start_date);
7652                       msc_sch_wb.atp_debug('Get_Res_Requirements: ' || l_res_requirements.requested_date(h));
7653                    END IF;
7654                    -- Bug 3348095
7655                    l_lead_time := l_res_requirements.lead_time(h);
7656                    l_requested_date := l_res_requirements.requested_date(h);
7657                    IF PG_DEBUG in ('Y', 'C') THEN
7658                       msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Next Date found..Exiting');
7659                    END IF;
7660                    EXIT;
7661               ELSIF h = res_count THEN
7662                    l_requested_date := p_requested_date;
7663                    l_lead_time := 0;
7664               -- Bug 3348095
7665               -- Assign the Resource start Date
7666               ELSE
7667                    -- Set resource start date to NULL
7668                    l_res_start_date := NULL;
7669               -- Bug 3348095
7670               END IF;
7671 
7672             END LOOP;
7673         END IF;
7674 
7675         -- Bug 3494178, need to reset l_res_requirements.requested_date(j) same as l_requested_date
7676         l_res_requirements.requested_date(j) := l_requested_date;
7677 
7678         msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'New Requested Date: ' ||l_res_requirements.requested_date(j));
7679 
7680         IF PG_DEBUG in ('Y', 'C') THEN
7681            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Resource Lead time:' || l_lead_time);
7682            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_requested_date:'|| l_requested_date);
7683         END IF;
7684 
7685         -- 1610561
7686         -- l_resource_id := l_res_requirements.resource_id(j);
7687         -- l_department_id := NVL(l_res_requirements.owning_department_id(j),
7688         --                       l_res_requirements.department_id(j));
7689         -- l_requested_date already assigned above
7690         --l_requested_date := l_res_requirements.requested_date(j);
7691         /** BUG 2313497, 2126520 END Changes: Check Resource Availability **/
7692 
7693         l_resource_usage := l_res_requirements.resource_usage(j);
7694         l_basis_type := l_res_requirements.basis_type(j);
7695         l_efficiency := l_res_requirements.efficiency(j);
7696         l_utilization := l_res_requirements.utilization(j);
7697         ---resource batching
7698         l_max_capacity := l_res_requirements.max_capacity(j);
7699         l_batchable_flag := l_res_requirements.batch_flag(j);
7700         l_req_unit_capacity := l_res_requirements.required_unit_capacity(j);
7701         l_req_capacity_uom := NVL(l_res_requirements.required_capacity_uom(j), ' ');
7702         l_std_op_code := l_res_requirements.std_op_code(j);
7703         l_uom_type := l_res_requirements.res_uom_type(j);
7704         l_res_uom := l_res_requirements.res_uom(j);
7705 
7706         -- ODR
7707         l_op_seq_num := l_res_requirements.operation_sequence(j);
7708 
7709         If (l_batchable_flag <> 1 ) OR  (l_use_batching <> 1) THEN
7710           --- if item is not batchable or batching is not done then
7711           -- set the std_op_code back to null
7712            IF PG_DEBUG in ('Y', 'C') THEN
7713               msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'batch flag back to null');
7714               msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Dont do batching');
7715            END IF;
7716           l_batchable_flag := 2;
7717           --l_use_batching := 0;
7718         END IF;
7719 
7720         l_atp_period := l_null_atp_period;
7721         l_atp_supply_demand := l_null_atp_supply_demand;
7722 
7723         IF PG_DEBUG in ('Y', 'C') THEN
7724            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'j := '||j);
7725 	   msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_resource_id := '||l_resource_id);
7726            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_department_id := '||l_department_id);
7727            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_requested_date := '||l_requested_date);
7728            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_resource_usage := '||l_resource_usage);
7729            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_basis_type := '||l_basis_type);
7730            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_efficiency := '||l_efficiency);
7731            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_utilization := '||l_utilization);
7732            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_batchable_flag := '||l_batchable_flag);
7733            -- ODR
7734            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_op_seq_num := '||l_op_seq_num);
7735            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Offset % := '
7736                                                 ||l_res_requirements.resource_offset_percent(j));
7737            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Lead Time := '
7738                                          ||l_res_requirements.lead_time(j));
7739            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Actual Resource_Usage := '
7740                                                ||l_res_requirements.actual_resource_usage(j));
7741            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Reverse Cum Yield := '
7742                                              ||l_res_requirements.reverse_cumulative_yield(j));
7743         END IF;
7744         --- resource batching
7745         IF PG_DEBUG in ('Y', 'C') THEN
7746            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_max_capacity = '|| l_max_capacity);
7747            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_batchable_flag = '|| l_batchable_flag);
7748            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_req_unit_capacity = '||l_req_unit_capacity);
7749            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_req_capacity_uom = ' || l_req_capacity_uom);
7750            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'UOM type := ' || l_UOM_type);
7751            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_res_uom := '|| l_res_uom);
7752         END IF;
7753 
7754         --- get conversion rate for resource
7755         IF ((l_batchable_flag = 1) and (l_use_batching = 1)) THEN
7756                --do unit conversions into base uom
7757                ---first do item unit conversion to base uom
7758                BEGIN
7759                	  SELECT conversion_rate
7760                   INTO   l_item_conversion_rate
7761                   FROM   msc_uom_conversions
7762                   WHERE  inventory_item_id = 0
7763                   AND    sr_instance_id = p_instance_id
7764                   AND    UOM_CODE = l_req_capacity_uom;
7765                EXCEPTION
7766                   WHEN NO_DATA_FOUND THEN
7767                        l_item_conversion_rate := 1;
7768                END;
7769                --- now convert resource uom into base uom
7770                BEGIN
7771                   SELECT conversion_rate
7772                   INTO   l_res_conversion_rate
7773                   FROM   msc_uom_conversions
7774                   WHERE  inventory_item_id = 0
7775                   AND    sr_instance_id = p_instance_id
7776                   AND    UOM_CODE = l_res_uom;
7777                EXCEPTION
7778                   WHEN NO_DATA_FOUND THEN
7779                        l_res_conversion_rate := 1;
7780                END;
7781 
7782         END IF;
7783         IF PG_DEBUG in ('Y', 'C') THEN
7784            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_item_conversion_rate := ' || l_item_conversion_rate);
7785            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_res_conversion_rate := ' || l_res_conversion_rate);
7786         END IF;
7787 
7788         --diag_atp
7789         IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 and p_search = 1 THEN
7790 
7791            IF ((l_batchable_flag = 1) and (l_use_batching = 1)) then
7792                IF PG_DEBUG in ('Y', 'C') THEN
7793                   msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Batching quantity, diagnostic atp');
7794                END IF;
7795                l_requested_res_qty := (l_resource_usage * l_req_unit_capacity * p_requested_quantity)
7796                                        * (l_item_conversion_rate )/(l_efficiency * l_utilization * l_assembly_quantity);
7797            ELSIF l_basis_type in (1,3) THEN --4694958
7798                IF PG_DEBUG in ('Y', 'C') THEN
7799                   msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Basis type 1,3 qty, diagnostic ATP'); --4694958
7800                END IF;
7801                l_requested_res_qty := (l_resource_usage * p_requested_quantity)/
7802                                    (l_efficiency * l_utilization * l_assembly_quantity);
7803            ELSIF l_basis_type = 2 THEN
7804                IF PG_DEBUG in ('Y', 'C') THEN
7805                   msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Basis type 2 qty, diagnostic ATP');
7806                END IF;
7807                --bug 3766202: do not inflate the resource in case of pure lot base resource
7808                ---(no batching) as ATP doesn't consider batch size
7809                l_requested_res_qty := l_resource_usage/
7810                                    --(l_efficiency * l_utilization * l_assembly_quantity);
7811                                    (l_efficiency * l_utilization);
7812            END IF;
7813 
7814         ELSE
7815            IF ((l_batchable_flag = 1) and (l_use_batching = 1)) then
7816                IF PG_DEBUG in ('Y', 'C') THEN
7817                   msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Batching quantity');
7818                END IF;
7819                l_requested_res_qty := (l_resource_usage * l_req_unit_capacity * x_avail_assembly_qty)
7820                                        * (l_item_conversion_rate )/(l_efficiency * l_utilization * l_assembly_quantity);
7821            ELSIF l_basis_type in (1,3) THEN --4694958
7822                IF PG_DEBUG in ('Y', 'C') THEN
7823                   msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Basis type 1,3 qty'); --4694958
7824                END IF;
7825                l_requested_res_qty := (l_resource_usage * x_avail_assembly_qty)/
7826                                    (l_efficiency * l_utilization * l_assembly_quantity);
7827            ELSIF l_basis_type = 2 THEN
7828                IF PG_DEBUG in ('Y', 'C') THEN
7829                   msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Basis type 2 qty');
7830                END IF;
7831                --bug 3766202: do not inflate the resource in case of pure lot base resource
7832                ---(no batching) as ATP doesn't consider batch size
7833                l_requested_res_qty := l_resource_usage/
7834                                    (l_efficiency * l_utilization);
7835            END IF;
7836         --diag_atp
7837         END IF; --IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 and p_search = 1 THEN
7838 
7839         IF PG_DEBUG in ('Y', 'C') THEN
7840            msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_requested_res_qty := '||l_requested_res_qty);
7841         END IF;
7842 
7843         -- we need to have a branch here for allocated atp
7844         -- Bug 2372577 . Check value of profile option : krajan
7845         l_msc_cap_allocation := NVL(FND_PROFILE.VALUE('MSC_CAP_ALLOCATION'), 'Y');
7846 
7847         IF (MSC_ATP_PVT.G_ALLOCATED_ATP = 'N') OR
7848            (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y' AND MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1 AND
7849              MSC_ATP_PVT.G_ALLOCATION_METHOD = 1) OR
7850              -- added for bug 2372577
7851             (l_msc_cap_allocation = 'N') THEN
7852 
7853             l_batching_flag := 0;
7854             IF l_batchable_flag = 1 and l_use_batching = 1 THEN
7855                l_batching_flag := 1;
7856             END IF;
7857 
7858             -- 2859130
7859             get_unalloc_res_avail(
7860                p_insert_flag,
7861                l_batching_flag,
7862                MSC_ATP_PVT.G_Optimized_Plan,
7863                p_instance_id,
7864                p_organization_id,
7865                p_plan_id,
7866                l_plan_start_date,
7867                l_department_id,
7868                l_resource_id,
7869                l_infinite_time_fence_date,
7870                l_uom_type,
7871                l_uom_code,
7872                l_max_capacity,
7873                l_res_conversion_rate,
7874                p_level,
7875                p_scenario_id,
7876                p_inventory_item_id,
7877                l_calendar_code,
7878                l_calendar_exception_set_id,
7879                l_summary_sql,       -- For summary enhancement
7880                p_refresh_number,    -- For summary enhancement
7881                l_atp_period_tab,
7882                l_atp_qty_tab,
7883                l_atp_period
7884             );
7885 
7886             Print_Dates_Qtys(l_atp_period_tab, l_atp_qty_tab);
7887 
7888         ELSE -- of G_ALLOCATED_ATP
7889            -- we are using allocated atp
7890            -- Begin Bug 2424357
7891            IF PG_DEBUG in ('Y', 'C') THEN
7892               msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'MSC_ATP_PVT.G_ATP_DEMAND_CLASS := ' || MSC_ATP_PVT.G_ATP_DEMAND_CLASS);
7893            END IF;
7894            l_demand_Class := MSC_AATP_FUNC.Get_Res_Hierarchy_demand_class(
7895                                                   MSC_ATP_PVT.G_PARTNER_ID,
7896                                                   MSC_ATP_PVT.G_PARTNER_SITE_ID,
7897                                                   l_department_id,
7898                                                   l_resource_id,
7899                                                   p_organization_id,
7900                                                   p_instance_id,
7901                                                   l_requested_date,
7902                                                   NULL,
7903                                                   MSC_ATP_PVT.G_ATP_DEMAND_CLASS);
7904 
7905            --diag_atp
7906            l_allocation_rule_name := MSC_ATP_PVT.G_ALLOCATION_RULE_NAME;
7907            IF PG_DEBUG in ('Y', 'C') THEN
7908               msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_demand_Class := '|| l_demand_Class);
7909            END IF;
7910            -- End Bug 2424357
7911 
7912            MSC_AATP_PVT.Res_Alloc_Cum_Atp(p_plan_id,
7913                               p_level,
7914                               MSC_ATP_PVT.G_ORDER_LINE_ID,
7915                               p_scenario_id,
7916                               l_department_id,
7917                               l_resource_id,
7918                               p_organization_id,
7919                               p_instance_id,
7920                               l_demand_Class, -- Bug 2424357
7921                               --p_demand_class,
7922                               l_requested_date,
7923                               p_insert_flag,
7924                               l_max_capacity,
7925                               l_batchable_flag,
7926                               l_res_conversion_rate,
7927                               l_uom_type,
7928                               l_atp_info,
7929                               l_atp_period,
7930                               l_atp_supply_demand);
7931 
7932            l_atp_period_tab := l_atp_info.atp_period;
7933            l_atp_qty_tab := l_atp_info.atp_qty;
7934 
7935         END IF; -- of G_ALLOCATED_ATP
7936 
7937         IF l_atp_period_tab.COUNT > 0 THEN
7938 
7939           IF PG_DEBUG in ('Y', 'C') THEN
7940              msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_atp_period_tab.count='||l_atp_period_tab.COUNT);
7941              FOR i in 1..l_atp_period_tab.COUNT LOOP
7942                  msc_sch_wb.atp_debug('Date '||l_atp_period_tab(i)||' Qty '||
7943                                   l_atp_qty_tab(i));
7944              END LOOP;
7945           END IF;
7946           l_res_qty_before_ptf := 0;
7947           IF PG_DEBUG in ('Y', 'C') THEN
7948              msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'G_PTF_DATE_THIS_LEVEL := ' || MSC_ATP_PVT.G_PTF_DATE_THIS_LEVEL);
7949           END IF;
7950           --bug 2341075: we should not consider any resources available before sysdate.Therefore,
7951           --- we get rid of all the resources availability before sysdate. Before this fix we use to ommit all
7952           --- the resources before pTF. Now we get rid off all resource availability prior to greatest of
7953           ---sysdate anf PTF date
7954           --Bug3394751 Added Trunc on the sysdate.
7955 
7956           FOR i in 1..l_atp_period_tab.COUNT LOOP
7957               IF (i = 1 AND l_atp_period_tab(i) >= GREATEST(MSC_ATP_PVT.G_PTF_DATE_THIS_LEVEL, trunc(sysdate))) THEN
7958                   l_res_qty_before_ptf := 0;
7959                   EXIT;
7960               ELSIF (i < l_atp_period_tab.COUNT AND
7961                       l_atp_period_tab(i) <  GREATEST(MSC_ATP_PVT.G_PTF_DATE_THIS_LEVEL, trunc(sysdate)) AND
7962                       l_atp_period_tab(i+1) >= GREATEST(MSC_ATP_PVT.G_PTF_DATE_THIS_LEVEL, trunc(sysdate)) ) THEN
7963                   l_res_qty_before_ptf := l_atp_qty_tab(i);
7964                   -- Bug 4108546 Set the Index value
7965                   l_res_ptf_indx := i;
7966                   EXIT;
7967               ELSIF i = l_atp_period_tab.COUNT THEN
7968                   IF l_atp_qty_tab(i) = MSC_ATP_PVT.INFINITE_NUMBER  THEN
7969                      l_res_qty_before_ptf := 0;
7970                   ELSE
7971                      l_res_qty_before_ptf := l_atp_qty_tab(i);
7972                      -- Bug 4108546 Set the Index value
7973                      l_res_ptf_indx := i;
7974                   END IF;
7975                   EXIT;
7976               END IF;
7977           END LOOP;
7978           l_res_qty_before_ptf := GREATEST(l_res_qty_before_ptf, 0);
7979           IF PG_DEBUG in ('Y', 'C') THEN
7980              msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_res_qty_before_ptf := ' || l_res_qty_before_ptf);
7981              -- Bug 4108546 Print the value of Index
7982              IF (l_res_ptf_indx IS NOT NULL) THEN
7983                 msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_res_ptf_indx := ' || l_res_ptf_indx);
7984              END IF;
7985              -- End Bug 4108546
7986           END IF;
7987           IF p_search = 1 THEN -- backward
7988 
7989             IF (l_requested_date < l_atp_period_tab(1)) THEN
7990             -- let say the first period is on Day5 but your
7991             -- request in on Day2.
7992 
7993                 l_requested_date_quantity := 0;
7994             ELSIF (l_requested_date < trunc(sysdate)) THEN
7995 
7996                 l_requested_date_quantity := 0;
7997 
7998             ELSE
7999              IF MSC_ATP_PVT.G_RES_CONSUME = 'Y' THEN
8000               IF PG_DEBUG in ('Y', 'C') THEN
8001                  msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'G_RES_CONSUME = '||MSC_ATP_PVT.G_RES_CONSUME);
8002               END IF;
8003               FOR k IN 1..l_atp_period_tab.COUNT LOOP
8004                 IF k < l_atp_period_tab.LAST THEN
8005                   l_next_period := l_atp_period_tab(k+1);
8006                 ELSE
8007                   l_next_period := l_requested_date + 1;
8008                 END IF;
8009 
8010                 IF ((l_atp_period_tab(k) <= l_requested_date) and
8011                         (l_next_period > l_requested_date)) THEN
8012 
8013                   -- Bug found during fixing 3036513
8014                   -- Change > to >= so that if requested_date is infinite time fence date
8015                   -- then the quantity returned is also infinite.
8016                   IF (l_requested_date >= l_infinite_time_fence_date) THEN
8017                     l_requested_date_quantity := l_atp_qty_tab(k);
8018                   ELSE
8019                     l_requested_date_quantity := GREATEST((l_atp_qty_tab(k) - l_res_qty_before_ptf), 0);
8020                   END IF;
8021                   IF PG_DEBUG in ('Y', 'C') THEN
8022                      msc_sch_wb.atp_debug('Get_Res_Requirements: l_atp_period_tab(k) :' || l_atp_period_tab(k) );
8023                      msc_sch_wb.atp_debug('Get_Res_Requirements: l_requested_date_quantity :' || l_requested_date_quantity );
8024                   END IF;
8025                   EXIT;
8026                 END IF;
8027               END LOOP;
8028              ELSE -- IF G_RES_CONSUME = 'N'
8029               IF PG_DEBUG in ('Y', 'C') THEN
8030                  msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'G_RES_CONSUME = '||MSC_ATP_PVT.G_RES_CONSUME);
8031               END IF;
8032               -- first we initialize the l_requested_date_quantity so that
8033               -- if we cannot find any date that match the requested date,
8034               -- the l_requested_date_quantity is set to 0.
8035 
8036               l_requested_date_quantity := 0.0;
8037               FOR k IN 1..l_atp_period_tab.COUNT LOOP
8038                 IF l_atp_period_tab(k) = l_requested_date THEN
8039                   l_requested_date_quantity := l_atp_qty_tab(k);
8040                   EXIT;
8041                 END IF;
8042               END LOOP;
8043              END IF;  -- END IF G_RES_CONSUME = 'Y'
8044 
8045 
8046             END IF;
8047 
8048             IF PG_DEBUG in ('Y', 'C') THEN
8049                msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_requested_date_quantity ='||l_requested_date_quantity);
8050             END IF;
8051 
8052             IF l_requested_date_quantity >= l_requested_res_qty THEN
8053                 -- for this resource, we satisfy the resource requirement
8054                 -- so we don't need to change the x_avail_assembly_qty
8055                 -- for the assembly.
8056                 NULL;
8057 
8058             ELSIF l_requested_date_quantity >0 THEN
8059             -- for this resource, we cannot satisfy the resource
8060             -- requirement.  so we need to change the x_avail_assembly_qty
8061                 --- resource batching: If req_dat_qty < req_qty then we set the avail_qty = 0
8062                 IF l_basis_type = 2 OR MSC_ATP_PVT.G_RES_CONSUME = 'N'OR
8063                          (l_batchable_flag = 1 AND l_use_batching = 1) THEN
8064                     IF PG_DEBUG in ('Y', 'C') THEN
8065                        msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'G_RES_CONSUME = '||MSC_ATP_PVT.G_RES_CONSUME);
8066                        msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_basis_type = '||l_basis_type);
8067                     END IF;
8068                     -- this requirement is per lot, so we cannot have any
8069                     -- final assembly made
8070                     x_avail_assembly_qty :=0;
8071                     --diag_atp: we want to check for next resource in daignostic mode even if
8072                     --available qty for this resource is zero.
8073                     IF NOT (MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1) THEN
8074 
8075 		       EXIT;
8076                     END IF;
8077                 ELSE
8078                     -- this requirement is per item, so we can make partial of
8079                     -- the requested_quantity.  notes, we should
8080                     -- use the min to get the x_avail_assembly_qty.
8081 
8082                     x_avail_assembly_qty := LEAST(x_avail_assembly_qty,
8083                           trunc((l_requested_date_quantity * l_efficiency * l_utilization)/
8084                                 l_resource_usage,6));	-- 5598066
8085 
8086                     -- 2869830
8087                     IF PG_DEBUG in ('Y', 'C') THEN
8088                        msc_sch_wb.atp_debug('avail_assembly_qty: ' ||
8089                           x_avail_assembly_qty);
8090                     END IF;
8091                     IF l_rounding_flag = 1 THEN
8092                        x_avail_assembly_qty := FLOOR(x_avail_assembly_qty);
8093                        IF PG_DEBUG in ('Y', 'C') THEN
8094                           msc_sch_wb.atp_debug('rounded avail qty: ' ||
8095                              x_avail_assembly_qty);
8096                        END IF;
8097                     END IF;
8098 
8099                 END IF;
8100             ELSE
8101                 -- since we don't have any resource left, we cannot make any
8102                 -- assembly.
8103                 x_avail_assembly_qty :=0;
8104                 --diag_atp: we want to check for next resource in daignostic mode even if
8105                 --available qty for this resource is zero.
8106                 IF NOT (MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1) THEN
8107                    EXIT;
8108                 END IF;
8109             END IF;
8110           ELSE
8111             -- now this is forward.  so what we want to know is the date
8112             -- when whole quantity is available.
8113             IF PG_DEBUG in ('Y', 'C') THEN
8114                msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'in forward, resource check');
8115             END IF;
8116 
8117             IF PG_DEBUG in ('Y', 'C') THEN
8118                msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_requested_date = '||l_requested_date);
8119             END IF;
8120 
8121             -- Bug 3450725, requested date must be at least PTF Date + Lead Time w/ offset % from start of job.
8122             -- Lets say, there are 3 resources R1, R2 and R3 with offset % as 0, 50% and 70% respectively,
8123             -- and Total LT (F+V*Qty)* (1+mso_LT_factor) = 10 days and PTF is D10.
8124             -- Request Date for each resource in this example must be minimum D15, D17 and D20 for R1, R2 and R3.
8125             -- This will ensure start date for 3 resources (assumed to be sequential) be D10, D15 and D17 respectively.
8126 			-- Use Org's Manuf. Calendar
8127 
8128             ---2178544
8129             l_requested_date := GREATEST(l_requested_date,
8130                      				MSC_CALENDAR.DATE_OFFSET
8131                                     (p_organization_id,
8132                                      p_instance_id,
8133                                      1,
8134                                      MSC_ATP_PVT.G_PTF_DATE,
8135                     				 CEIL(((NVL(l_item_fixed_lt,0)+
8136                         					NVL(l_item_var_lt,0)* p_requested_quantity) * (1+ l_mso_lead_time_factor))
8137                         					- l_lead_time)));--4198893,4198445
8138 
8139 
8140             ---2178544
8141             --l_requested_date := GREATEST(l_requested_date, MSC_ATP_PVT.G_PTF_DATE);
8142 
8143             IF PG_DEBUG in ('Y', 'C') THEN
8144                msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'New l_requested_date = '||l_requested_date);
8145             END IF;
8146 
8147             FOR k IN 1..l_atp_period_tab.COUNT LOOP
8148 
8149                 -- bug 1510408
8150                 IF k < l_atp_period_tab.LAST THEN
8151                   l_next_period := l_atp_period_tab(k+1);
8152                 ELSE
8153                   l_next_period := l_requested_date + 1;
8154                 END IF;
8155 
8156                 IF PG_DEBUG in ('Y', 'C') THEN
8157                    msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_atp_period_tab('||k||')='||l_atp_period_tab(k));
8158                    msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_atp_qty_tab('||k||')='||l_atp_qty_tab(k));
8159                 END IF;
8160 
8161                 IF (l_atp_qty_tab(k)  -l_res_qty_before_ptf >= l_requested_res_qty) AND
8162                     --((l_atp_period_tab(k) >= trunc(sysdate) AND --4198893,4198445
8163                     ((l_atp_period_tab(k) >= l_requested_date AND
8164                      l_next_period > l_requested_date
8165                      AND MSC_ATP_PVT.G_RES_CONSUME = 'Y') OR
8166                      (l_atp_period_tab(k) >= l_requested_date
8167                      AND MSC_ATP_PVT.G_RES_CONSUME = 'N')) THEN
8168 
8169                   IF PG_DEBUG in ('Y', 'C') THEN
8170                      msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'inside the loop to find x_atp_date');
8171                      msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'x_atp_date = '||x_atp_date);
8172                      msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_res_requirements.lead_time(j) = '||l_res_requirements.lead_time(j));
8173                   END IF;
8174 
8175                   -- we need to store this quantity and date somewhere
8176                   -- so that we can use them to add to pegging tree
8177 
8178                 -- Bug 1418766 and 1417110. In case of forward scheduling,
8179                 -- instead of using resources on the earliest available date
8180                 -- use them on the latest date before the greatest of requested date
8181                 -- and l_res_atp_date
8182 
8183                   l_res_atp_date := GREATEST(l_atp_period_tab(k), l_requested_date);
8184                   -- BUG found during ODR/CTO-Rearch/ATP_Simplified Pegging.
8185                   -- Bug found during fixing 3036513
8186                   -- Change = to >= so that if requested_date is infinite time fence date
8187                   -- then the quantity returned is also infinite.
8188                   IF (l_res_atp_date >= l_infinite_time_fence_date) THEN
8189                      l_res_atp_qty := l_atp_qty_tab(k)  ;
8190                   ELSE
8191                      l_res_atp_qty := l_atp_qty_tab(k) - l_res_qty_before_ptf ;
8192                   END IF;
8193                   -- End BUG found during ODR/CTO-Rearch/ATP_Simplified Pegging.
8194 
8195             IF PG_DEBUG in ('Y', 'C') THEN
8196                msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_res_atp_date = '||l_res_atp_date);
8197             END IF;
8198 		 --4198893,4198445 First calculate individual availability date. This date will be used to calculate
8199 		 --start date.
8200                   IF nvl(l_lead_time, 0) > 0 THEN
8201                      l_res_availability_date := MSC_CALENDAR.DATE_OFFSET
8202                                     (p_organization_id,
8203                                      p_instance_id,
8204                                      1,
8205                                      l_res_atp_date,
8206                                      NVL(l_lead_time, 0));
8207                      /* x_atp_date :=  GREATEST(MSC_CALENDAR.DATE_OFFSET
8208                                     (p_organization_id,
8209                                      p_instance_id,
8210                                      1,
8211                                      l_res_atp_date,
8212                                      NVL(l_lead_time, 0)), --BUG 2313497, 2126520
8213                                      x_atp_date);
8214                      */
8215                      --// BUG 2313497, 2126520
8216                   ELSE
8217                     -- Bug 3598486: Taking the greatest of the 2 dates
8218                     --4198445: l_res_availability_date is same as l_res_atp_date
8219                     --x_atp_date := GREATEST(l_res_atp_date, x_atp_date);
8220 
8221                     l_res_availability_date := l_res_atp_date;
8222                   END IF;
8223                   x_atp_date := GREATEST(l_res_availability_date, x_atp_date);
8224                   IF PG_DEBUG in ('Y', 'C') THEN
8225                      msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_lead_time = '||l_lead_time);
8226                   END IF;
8227                   --// BUG 2313497, 2126520
8228                   IF PG_DEBUG in ('Y', 'C') THEN
8229                      msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_res_requirements.lead_time(j) = '||
8230                                l_res_requirements.lead_time(j));
8231                      msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'x_atp_date = '||x_atp_date);
8232                   END IF;
8233 
8234                   EXIT;
8235                 ELSIF k = l_atp_period_tab.last THEN -- bug 1169539
8236                   IF PG_DEBUG in ('Y', 'C') THEN
8237                      msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'end of l_atp_period_tab, setting x_atp_date to null');
8238                   END IF;
8239                   x_atp_date := null;
8240                 END IF;
8241             END LOOP;
8242 
8243             IF x_atp_date IS NULL THEN
8244                 -- no available date exists for this resource, we should
8245                 -- exit this resource loop then.
8246 		-- Bug 1608755, set available qty = 0 in this case.
8247                 x_avail_assembly_qty :=0;
8248                 IF PG_DEBUG in ('Y', 'C') THEN
8249                    msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'exiting from resource loop');
8250                 END IF;
8251 
8252                 EXIT;
8253             END IF;
8254 
8255           END IF; -- end if p_search
8256 
8257           IF PG_DEBUG in ('Y', 'C') THEN
8258              msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'x_avail_assembly_qty='||x_avail_assembly_qty);
8259           END IF;
8260 
8261         ELSE  -- else of 'if l_atp_period_tab.count > 0'
8262 
8263             -- no supply demand record for this resource, that means we
8264             -- cannot make any assembly.
8265             x_avail_assembly_qty :=0;
8266             x_atp_date := NULL;
8267             --diag_atp: we want to check for next resource in daignostic mode even if
8268             --available qty for this resource is zero.
8269             IF NOT (MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 AND p_search = 1) THEN
8270 
8271                 EXIT;
8272             END IF;
8273         END IF;  -- end if l_atp_period_tab.count > 0
8274 
8275         -- Note:  we need to post those requirements into database so that
8276         -- if we happen to check the resource again, we won't mess up the
8277         -- quantity. not yet done!!!
8278 
8279         IF p_search = 1 THEN   -- Backward Scheduling
8280           --diag_atp
8281           IF x_avail_assembly_qty <> 0 or MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 THEN
8282             -- get supply_id from the pegging_id
8283             SELECT IDENTIFIER3
8284             INTO   l_supply_id
8285             FROM   MRP_ATP_DETAILS_TEMP
8286             WHERE  PEGGING_ID = p_parent_pegging_id
8287             AND    RECORD_TYPE = 3
8288             AND    SESSION_ID = MSC_ATP_PVT.G_SESSION_ID;
8289             --diag_atp:
8290             IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 THEN
8291               --since we are checking on full quantity, we do not need to convert it back
8292               IF (l_basis_type in (1,3)) THEN --4694958
8293                  --bug 3766202: divide by l_assembly qty to correctly project resource hours
8294                  --- for co-producs
8295                  l_resource_hours       := (p_requested_quantity*l_resource_usage) /
8296                                            (l_efficiency * l_utilization * l_assembly_quantity);
8297 
8298                  l_unadj_resource_hours := (p_requested_quantity*l_resource_usage)/
8299                                             l_assembly_quantity; --5093604
8300 
8301                  l_touch_time           := (p_requested_quantity*l_resource_usage)/
8302                                              (l_efficiency * l_assembly_quantity); --5093604
8303               --bug 3766202: Do not inflate resource qty for unbatachable lot based resource
8304               ELSIF ((l_batchable_flag =1 and l_use_batching = 1)) THEN
8305                  --bug 3766202: divide by l_assembly qty to correctly project resource hours
8306                  --- for co-producs
8307                  l_resource_hours       := l_resource_usage / (l_efficiency * l_utilization * l_assembly_quantity);
8308 
8309                  l_unadj_resource_hours := l_resource_usage/l_assembly_quantity; --5093604
8310 
8311                  l_touch_time           := l_resource_usage/(l_efficiency * l_assembly_quantity); --5093604
8312 
8313               ELSE
8314                  l_resource_hours       := l_resource_usage / (l_efficiency * l_utilization);
8315 
8316                  l_unadj_resource_hours := l_resource_usage; --5093604
8317 
8318                  l_touch_time           := l_resource_usage/l_efficiency; --5093604
8319               END IF;
8320             ELSE
8321                IF (l_basis_type in (1,3)) THEN --4694958
8322                  --bug 3766202: divide by l_assembly qty to correctly project resource hours
8323                  --- for co-producs
8324                  l_resource_hours       := (x_avail_assembly_qty*l_resource_usage) /
8325                                            (l_efficiency * l_utilization * l_assembly_quantity);
8326 
8327                  l_unadj_resource_hours := (x_avail_assembly_qty*l_resource_usage)/
8328                                             l_assembly_quantity; --5093604
8329 
8330                  l_touch_time           := (x_avail_assembly_qty*l_resource_usage)/
8331                                             (l_efficiency * l_assembly_quantity); --5093604
8332 
8333                --bug 3766202: Do not inflate resource qty for unbatachable lot based resource
8334                ELSIF ((l_batchable_flag =1 and l_use_batching = 1)) THEN
8335                  --l_resource_hours := l_requested_res_qty / (l_efficiency * l_utilization);
8336                  --bug: resource hours were getting inflated twice.
8337                  --- replaced l_requested_res_qty with l_resource_usage
8338                  --bug 3766202: divide by l_assembly qty to correctly project resource hours
8339                  --- for co-producs
8340                  l_resource_hours       := l_resource_usage / (l_efficiency * l_utilization * l_assembly_quantity);
8341 
8342                  l_unadj_resource_hours := l_resource_usage/l_assembly_quantity; --5093604
8343 
8344                  l_touch_time           := l_resource_usage/(l_efficiency * l_assembly_quantity); --5093604
8345 
8346                ELSE
8347                  l_resource_hours       := l_resource_usage / (l_efficiency * l_utilization);
8348 
8349                  l_unadj_resource_hours := l_resource_usage; --5093604
8350 
8351                  l_touch_time           := l_resource_usage/l_efficiency; --5093604
8352                END IF;
8353             END IF;
8354 
8355             /*IF (l_basis_type = 2 ) THEN
8356               l_resource_hours := l_requested_res_qty / (l_efficiency * l_utilization);
8357             ELSIF (l_basis_type = 1 ) THEN
8358               l_resource_hours := (x_avail_assembly_qty*l_resource_usage) /
8359                                         (l_efficiency * l_utilization);
8360             END IF; */
8361 
8362             -- Bug 3348095
8363             -- Calculate the start date given the end date in backward case.
8364             IF (l_res_start_date IS NULL) THEN
8365               l_res_start_date :=  LEAST(MSC_CALENDAR.DATE_OFFSET
8366                                     (p_organization_id,
8367                                      p_instance_id,
8368                                      1,
8369                                      l_requested_date,
8370                                      -1 * l_res_requirements.lead_time(j)),
8371                                      l_requested_date);
8372 
8373               IF PG_DEBUG in ('Y', 'C') THEN
8374                  msc_sch_wb.atp_debug('Get_Res_Requirements: Calc. value-1 l_res_start_date ' ||
8375                                                                          l_res_start_date);
8376               END IF;
8377             END IF;
8378             -- End Bug 3348095
8379               -- Bug 3450725
8380               -- Ensure SYSDATE/PTF_DATE integrity while calculating start_date
8381               -- for resource_requirements
8382               -- Bug 3562873 only PTF check is needed.
8383               l_res_start_date := trunc(GREATEST(l_res_start_date,
8384                         --Bug 3562873   l_res_requirements.requested_date(j),
8385                                         MSC_ATP_PVT.G_PTF_DATE_THIS_LEVEL)); --4135752
8386               -- End Bug 3562873.
8387 
8388               IF PG_DEBUG in ('Y', 'C') THEN
8389                 msc_sch_wb.atp_debug('Get_Res_Requirements: l_res_requirements.requested_date(j) '||
8390                                                         l_res_requirements.requested_date(j));
8391                 msc_sch_wb.atp_debug('Get_Res_Requirements: Calc. value-2 l_res_start_date ' ||
8392                                                                          l_res_start_date);
8393               END IF;
8394               -- End Bug 3450725
8395 
8396             MSC_ATP_DB_UTILS.Add_Resource_Demand(p_instance_id,
8397                                                  p_plan_id,
8398                                                  l_supply_id,
8399                                                  p_organization_id,
8400                                                  l_resource_id,
8401                                                  l_res_requirements.department_id(j),
8402                                                  -- Bug 3348095 Pass in Resource Start Dt.
8403                                                  l_res_start_date,
8404                                                  -- End Bug 3348095
8405                                                  l_requested_date,
8406                                                  l_resource_hours, --5093604
8407                                                  l_unadj_resource_hours , --5093604
8408                                                  l_touch_time, --5093604
8409                                                  l_std_op_code,
8410                                                  l_requested_res_qty,
8411                                                  l_inventory_item_id,   -- CTO Option Dependent Resources ODR
8412                                                  l_basis_type,  -- CTO Option Dependent Resources ODR
8413                                                  l_op_seq_num,  -- CTO Option Dependent Resources ODR
8414                                                  p_refresh_number,      -- For summary enhancement
8415                                                  l_transaction_id,
8416                                                  l_return_status);
8417 
8418              msc_sch_wb.atp_debug('Out of the ADD RESOURCE DEMAND');
8419             -- Bug 3348095
8420             -- End date of this resource will be the start date of next resource.
8421             -- Bug 3562873 Comment out this assignment, redundant, done above.
8422             -- l_res_start_date := l_requested_date;
8423             /*
8424             IF PG_DEBUG in ('Y', 'C') THEN
8425                msc_sch_wb.atp_debug('Get_Res_Requirements: Set l_res_start_date for next Res. ' ||
8426                                                                          l_res_start_date);
8427             END IF;
8428             */
8429             -- End Bug 3562873
8430             -- End 3348095
8431 
8432             -- add pegging info for this demand
8433 
8434             -- for performance reason, we call these function here and
8435             -- then populate the pegging tree with the values
8436 
8437             -- 1487344: instead of getting resource code and department code
8438             -- separately, we get them together to ensure we won't accidently
8439             -- get the line info.  Because we display either owing dept or
8440             -- dept for supply or demand pegging tree, we need to get the name
8441             -- each time.
8442             --diag_atp: we are already getting the department code in actual query
8443             IF l_res_requirements.department_code(j) is null THEN
8444                MSC_ATP_PROC.get_dept_res_code(p_instance_id,
8445                               l_res_requirements.department_id(j),
8446                               l_resource_id,
8447                               p_organization_id,
8448                               l_department_code,
8449                               l_resource_code);
8450             ELSE
8451                l_department_code := l_res_requirements.department_code(j);
8452                l_resource_code := l_res_requirements.resource_code(j);
8453             END IF;
8454 
8455             IF NVL(l_res_requirements.department_id(j), -1) <> NVL(l_res_requirements.owning_department_id(j),
8456                                                                   NVL(l_res_requirements.department_id(j), -1)) THEN
8457                MSC_ATP_PROC.get_dept_res_code(p_instance_id,
8458                               l_res_requirements.owning_department_id(j),
8459                               l_resource_id,
8460                               p_organization_id,
8461                               l_owning_department_code,
8462                               l_resource_code);
8463             ELSE
8464                l_owning_department_code := l_department_code;
8465             END IF;
8466 
8467             l_pegging_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
8468             l_pegging_rec.order_line_id:= MSC_ATP_PVT.G_ORDER_LINE_ID;
8469             l_pegging_rec.parent_pegging_id:= p_parent_pegging_id;
8470             l_pegging_rec.atp_level:= p_level;
8471             l_pegging_rec.organization_id:= p_organization_id;
8472             l_pegging_rec.organization_code := l_org_code;
8473             l_pegging_rec.identifier1:= p_instance_id;
8474             l_pegging_rec.identifier2:= p_plan_id;
8475             l_pegging_rec.identifier3 := l_supply_id; -- link to assembly's supply l_transaction_id;
8476             l_pegging_rec.identifier3 := l_transaction_id;
8477             l_pegging_rec.scenario_id:= p_scenario_id;
8478             l_pegging_rec.supply_demand_source_type:= 1;
8479             --l_pegging_rec.supply_demand_quantity:=l_resource_hours;
8480             --- Resource batching
8481             IF (l_use_batching = 1 AND l_batchable_flag = 1) THEN
8482                 l_pegging_rec.supply_demand_quantity:=l_requested_res_qty;
8483             ELSE
8484                 l_pegging_rec.supply_demand_quantity:=l_resource_hours;
8485             END IF;
8486             l_pegging_rec.supply_demand_type:= 1;
8487             l_pegging_rec.supply_demand_date:= l_requested_date;
8488 	    l_pegging_rec.department_id := l_res_requirements.department_id(j);
8489             l_pegging_rec.department_code := l_department_code;
8490 	    l_pegging_rec.resource_id := l_resource_id;
8491             l_pegging_rec.resource_code := l_resource_code;
8492             l_pegging_rec.inventory_item_id := NULL;
8493             l_pegging_rec.inventory_item_name := NULL;
8494             l_pegging_rec.supplier_id := NULL;
8495             l_pegging_rec.supplier_name := NULL;
8496             l_pegging_rec.supplier_site_id := NULL;
8497             l_pegging_rec.supplier_site_name := NULL;
8498             --- resource batching
8499             IF PG_DEBUG in ('Y', 'C') THEN
8500                msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_use_batching = ' || l_use_batching);
8501                msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_batchable_flag = ' || l_batchable_flag);
8502             END IF;
8503             ---bug 1907419: set batchable_flag =1 for batchable resource
8504             --IF (l_use_batching = 1 and l_batchable_flag = 1) THEN
8505             -- add batch flag to pegging
8506             IF PG_DEBUG in ('Y', 'C') THEN
8507                msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'ADD batch flag to pegging');
8508             END IF;
8509             l_pegging_rec.batchable_flag := l_batchable_flag;
8510             --END IF;
8511 
8512             --diag_atp
8513             l_pegging_rec.pegging_type := MSC_ATP_PVT.RESOURCE_DEMAND; --resource demand node
8514 
8515             -- Bug 3348161
8516             IF PG_DEBUG in ('Y', 'C') THEN
8517                msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'G_ITEM_INFO_REC.base_item_id ' ||
8518                                       MSC_ATP_PVT.G_ITEM_INFO_REC.base_item_id);
8519             END IF;
8520             ---s_cto_rearch and ODR
8521             l_pegging_rec.dest_inv_item_id := l_inventory_item_id;
8522             IF (MSC_ATP_PVT.G_ITEM_INFO_REC.bom_item_type in (1, 2) OR
8523                   -- Handle Configuration Items as well.
8524                   MSC_ATP_PVT.G_ITEM_INFO_REC.base_item_id is NOT NULL) THEN
8525                l_pegging_rec.model_sd_flag := 1;
8526                IF PG_DEBUG in ('Y', 'C') THEN
8527                  msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Set model_sd_flag');
8528                END IF;
8529             END IF;
8530             --e_cto_rearch and ODR
8531             -- End Bug 3348161
8532 
8533 	    -- dsting ATO 2465370
8534 	    l_pegging_rec.required_date := TRUNC(l_requested_date) + MSC_ATP_PVT.G_END_OF_DAY;
8535 
8536             --bug 3328421
8537             l_pegging_rec.actual_supply_demand_date := TRUNC(l_requested_date) + MSC_ATP_PVT.G_END_OF_DAY;
8538 
8539             -- for demo:1153192
8540             l_pegging_rec.constraint_flag := 'N';
8541             l_pegging_rec.summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;
8542             -- Bug 3826234
8543             l_pegging_rec.manufacturing_cal_code :=  NULL;
8544             l_pegging_rec.organization_type  := NVL ( MSC_ATP_PVT.G_ORG_INFO_REC.org_type, MSC_ATP_PVT.DISCRETE_ORG); --4775920
8545 
8546             MSC_ATP_DB_UTILS.Add_Pegging(l_pegging_rec, l_pegging_id);
8547 
8548             -- 1487344: instead of getting resource code and department code
8549             -- separately, we get them together to ensure we won't accidently
8550             -- get the line info.  Because we display either owing dept or
8551             -- dept for supply or demand pegging tree, we need to get the name
8552             -- each time.
8553 
8554             --diag_atp: we already got the owning department code before adding pegging for resource demand
8555             l_department_code := l_owning_department_code;
8556 
8557 
8558             /*MSC_ATP_PROC.get_dept_res_code(p_instance_id,
8559                               l_department_id,
8560                               l_resource_id,
8561                               p_organization_id,
8562                               l_department_code,
8563                               l_resource_code); */
8564 
8565             -- add pegging info for the supply
8566 
8567             l_pegging_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
8568             l_pegging_rec.order_line_id:= MSC_ATP_PVT.G_ORDER_LINE_ID;
8569             l_pegging_rec.parent_pegging_id:= l_pegging_id;
8570             l_pegging_rec.atp_level:= p_level+1;
8571             l_pegging_rec.organization_id:= p_organization_id;
8572             l_pegging_rec.organization_code := l_org_code;
8573             l_pegging_rec.identifier1:= p_instance_id;
8574             l_pegging_rec.identifier2:= p_plan_id;
8575             l_pegging_rec.identifier3 := -1;
8576             l_pegging_rec.scenario_id:= p_scenario_id;
8577             l_pegging_rec.supply_demand_source_type:= MSC_ATP_PVT.ATP;
8578             l_pegging_rec.supply_demand_quantity:=l_requested_date_quantity;
8579             l_pegging_rec.supply_demand_type:= 2;
8580             l_pegging_rec.supply_demand_date:= l_requested_date;
8581             l_pegging_rec.department_id := l_department_id;
8582             l_pegging_rec.department_code := l_department_code;
8583             l_pegging_rec.resource_id := l_resource_id;
8584             l_pegging_rec.resource_code := l_resource_code;
8585             l_pegging_rec.inventory_item_id := NULL;
8586             l_pegging_rec.inventory_item_name := NULL;
8587             l_pegging_rec.supplier_id := NULL;
8588             l_pegging_rec.supplier_name := NULL;
8589             l_pegging_rec.supplier_site_id := NULL;
8590             l_pegging_rec.supplier_site_name := NULL;
8591               --- resource batching
8592               IF PG_DEBUG in ('Y', 'C') THEN
8593                  msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_use_batching = ' || l_use_batching);
8594                msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_batchable_flag = ' || l_batchable_flag);
8595             END IF;
8596             --- bug 1907419
8597             ---IF (l_use_batching = 1 and l_batchable_flag = 1) THEN
8598             IF PG_DEBUG in ('Y', 'C') THEN
8599                msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'ADD batch flag to pegging');
8600             END IF;
8601             l_pegging_rec.batchable_flag := l_batchable_flag;
8602             --END IF;
8603 
8604             --diag_atp
8605             l_pegging_rec.operation_sequence_id := l_res_requirements.operation_sequence(j);
8606             l_pegging_rec.usage := l_res_requirements.actual_resource_usage(j);
8607             l_pegging_rec.offset := l_res_requirements.resource_offset_percent(j);
8608             l_pegging_rec.efficiency := l_res_requirements.efficiency(j);
8609             l_pegging_rec.utilization := l_res_requirements.utilization(j);
8610             l_pegging_rec.REVERSE_CUM_YIELD := l_res_requirements.reverse_cumulative_yield(j);
8611             l_pegging_rec.owning_department := l_owning_department_code;
8612             l_pegging_rec.pegging_type := MSC_ATP_PVT.RESOURCE_SUPPLY; --resource supply node
8613 
8614             l_pegging_rec.model_sd_flag := NULL; -- cto_rearch ODR unset flag for supply.
8615 
8616             IF (l_use_batching = 1 AND l_batchable_flag = 1) THEN
8617                 l_pegging_rec.required_quantity:=l_requested_res_qty;
8618             ELSE
8619                 l_pegging_rec.required_quantity:=l_resource_hours;
8620             END IF;
8621             l_pegging_rec.required_date := TRUNC(l_requested_date) + MSC_ATP_PVT.G_END_OF_DAY;
8622             --bug 3328421:
8623             l_pegging_rec.actual_supply_demand_date := TRUNC(l_requested_date) + MSC_ATP_PVT.G_END_OF_DAY;
8624             l_pegging_rec.basis_type := l_res_requirements.basis_type(j);
8625             IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 THEN
8626                IF l_requested_res_qty > l_requested_date_quantity THEN
8627                  l_pegging_rec.constraint_type := 6;
8628                END IF;
8629             END IF;
8630             l_pegging_rec.allocation_rule := l_allocation_rule_name;
8631             --diag_atp_end
8632 
8633             -- for demo:1153192
8634             IF l_resource_hours >= l_requested_date_quantity THEN
8635               l_pegging_rec.constraint_flag := 'Y';
8636             ELSE
8637               l_pegging_rec.constraint_flag := 'N';
8638             END IF;
8639 
8640             l_pegging_rec.source_type := 0;
8641             l_pegging_rec.summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;
8642 
8643             -- Bug 3036513 Add Infinite_Time_fence and ATP Rule Data to Pegging
8644             l_pegging_rec.infinite_time_fence := l_infinite_time_fence_date;
8645             l_pegging_rec.atp_rule_name := l_atp_rule_name;
8646             -- End Bug 3036513
8647             -- Bug 3826234
8648             l_pegging_rec.manufacturing_cal_code :=  l_calendar_code;
8649             l_pegging_rec.organization_type  := NVL ( MSC_ATP_PVT.G_ORG_INFO_REC.org_type, MSC_ATP_PVT.DISCRETE_ORG); --4775920
8650 
8651             MSC_ATP_DB_UTILS.Add_Pegging(l_pegging_rec, l_pegging_id);
8652 
8653           END IF;
8654 
8655         ELSE
8656            --IF p_search = 2 THEN, Forward Scheduling
8657 
8658           IF x_atp_date IS NOT NULL THEN
8659             -- get supply_id from the pegging_id
8660             SELECT IDENTIFIER3
8661             INTO   l_supply_id
8662             FROM   MRP_ATP_DETAILS_TEMP
8663             WHERE  PEGGING_ID = p_parent_pegging_id
8664             AND    RECORD_TYPE = 3
8665             AND    SESSION_ID = MSC_ATP_PVT.G_SESSION_ID;
8666 
8667             IF (l_basis_type in (1,3)) THEN --4694958
8668               --bug 3766202: divide by l_assembly qty to correctly project resource hours
8669               --- for co-producs
8670               l_resource_hours       := (x_avail_assembly_qty*l_resource_usage) /
8671                                         (l_efficiency * l_utilization * l_assembly_quantity);
8672 
8673               l_unadj_resource_hours := (x_avail_assembly_qty*l_resource_usage)/
8674                                          l_assembly_quantity; --5093604
8675 
8676               l_touch_time           := (x_avail_assembly_qty*l_resource_usage)/
8677                                          (l_efficiency * l_assembly_quantity); --5093604
8678 
8679             --bug 3766202: Do no inflate res req for nonbatchable resource
8680             ELSIF (l_batchable_flag =1 and l_use_batching = 1) THEN
8681 
8682               --bug 3766202: divide by l_assembly qty to correctly project resource hours
8683               --- for co-producs
8684               l_resource_hours       := l_resource_usage / (l_efficiency * l_utilization * l_assembly_quantity);
8685 
8686               l_unadj_resource_hours := l_resource_usage/l_assembly_quantity; --5093604
8687 
8688               l_touch_time           := l_resource_usage/(l_efficiency * l_assembly_quantity);  --5093604
8689 
8690             ELSE
8691               l_resource_hours       := l_resource_usage / (l_efficiency * l_utilization);
8692 
8693               l_unadj_resource_hours := l_resource_usage; --5093604
8694 
8695               l_touch_time           := l_resource_usage/l_efficiency; --5093604
8696 
8697             END IF;
8698 
8699             -- Bug 3348095
8700             -- Calculate the start date given the end date in forward case.
8701             l_res_start_date :=  LEAST(MSC_CALENDAR.DATE_OFFSET
8702                                     (p_organization_id,
8703                                      p_instance_id,
8704                                      1,
8705                                      l_res_availability_date,
8706                                      -1 * l_res_requirements.lead_time(j)),
8707                                      l_res_atp_date);
8708                                      --4198893,4198445: Calculate start date from individual resource's end date
8709                                      --l_res_atp_date);
8710 
8711             IF PG_DEBUG in ('Y', 'C') THEN
8712                msc_sch_wb.atp_debug('Get_Res_Requirements: Calculated val-1 l_res_start_date ' || l_res_start_date);
8713             END IF;
8714             -- Bug 3450725
8715             -- Ensure SYSDATE/PTF_DATE integrity while calculating start_date
8716             -- for resource_requirements
8717             -- Bug 3562873 only PTF check is needed.
8718             l_res_start_date := trunc(GREATEST(l_res_start_date,
8719                       --Bug 3562873   l_res_requirements.requested_date(j),
8720                                         MSC_ATP_PVT.G_PTF_DATE_THIS_LEVEL));  --4135752
8721             -- End Bug 3562873.
8722 
8723             IF PG_DEBUG in ('Y', 'C') THEN
8724                msc_sch_wb.atp_debug('Get_Res_Requirements: l_res_requirements.requested_date(j) ' ||
8725                                                         l_res_requirements.requested_date(j));
8726                msc_sch_wb.atp_debug('Get_Res_Requirements: Calculated val-2 l_res_start_date ' || l_res_start_date);
8727             END IF;
8728             -- End Bug 3450725
8729             -- End Bug 3348095
8730 
8731             MSC_ATP_DB_UTILS.Add_Resource_Demand(p_instance_id,
8732                                                  p_plan_id,
8733                                                  l_supply_id,
8734                                                  p_organization_id,
8735                                                  l_resource_id,
8736                                                  l_res_requirements.department_id(j),
8737                                                  -- Bug 3348095 Pass in Resource Start Dt.
8738                                                  l_res_start_date,
8739                                                  -- End Bug 3348095
8740                                                  l_res_atp_date,  -- bug 1238910
8741                                                  --l_requested_res_qty,
8742                                                  l_resource_hours, --5093604
8743                                                  l_unadj_resource_hours, --5093604
8744                                                  l_touch_time, --5093604
8745                                                  l_std_op_code,
8746                                                  l_requested_res_qty,
8747                                                  l_inventory_item_id,   -- CTO Option Dependent Resources ODR
8748                                                  l_basis_type,  -- CTO Option Dependent Resources ODR
8749                                                  l_op_seq_num,  -- CTO Option Dependent Resources ODR
8750                                                  p_refresh_number,      -- For summary enhancement
8751                                                  l_transaction_id,
8752                                                  l_return_status);
8753 
8754             -- add pegging info for this demand
8755 
8756             -- for performance reason, we call these function here and
8757             -- then populate the pegging tree with the values
8758 
8759             -- 1487344: instead of getting resource code and department code
8760             -- separately, we get them together to ensure we won't accidently
8761             -- get the line info.  Because we display either owing dept or
8762             -- dept for supply or demand pegging tree, we need to get the name
8763             -- each time.
8764 
8765             --diag_atp: we are already getting the department code in actual query
8766             IF l_res_requirements.department_code(j) is null THEN
8767                MSC_ATP_PROC.get_dept_res_code(p_instance_id,
8768                               l_res_requirements.department_id(j),
8769                               l_resource_id,
8770                               p_organization_id,
8771                               l_department_code,
8772                               l_resource_code);
8773             ELSE
8774                l_department_code := l_res_requirements.department_code(j);
8775                -- Bug 3308237 Set the assignment right.
8776                -- It was l_res_requirements.resource_id(j) before
8777                l_resource_code := l_res_requirements.resource_code(j);
8778             END IF;
8779 
8780             IF NVL(l_res_requirements.department_id(j), -1) <> NVL(l_res_requirements.owning_department_id(j),
8781                                                                  NVL(l_res_requirements.department_id(j), -1)) THEN
8782                MSC_ATP_PROC.get_dept_res_code(p_instance_id,
8783                               l_res_requirements.owning_department_id(j),
8784                               l_resource_id,
8785                               p_organization_id,
8786                               l_owning_department_code,
8787                               l_resource_code);
8788             ELSE
8789                l_owning_department_code := l_department_code;
8790             END IF;
8791 
8792             IF PG_DEBUG in ('Y', 'C') THEN
8793                msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'in forward piece, adding resource demand pegging');
8794             END IF;
8795             l_pegging_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
8796             l_pegging_rec.order_line_id:= MSC_ATP_PVT.G_ORDER_LINE_ID;
8797             l_pegging_rec.parent_pegging_id:= p_parent_pegging_id;
8798             l_pegging_rec.atp_level:= p_level;
8799             l_pegging_rec.organization_id:= p_organization_id;
8800             l_pegging_rec.organization_code := l_org_code;
8801             l_pegging_rec.identifier1:= p_instance_id;
8802             l_pegging_rec.identifier2:= p_plan_id;
8803             l_pegging_rec.identifier3 := l_transaction_id;
8804             l_pegging_rec.scenario_id:= p_scenario_id;
8805             l_pegging_rec.supply_demand_source_type:= 1;
8806             l_pegging_rec.supply_demand_quantity:=l_requested_res_qty;
8807             l_pegging_rec.supply_demand_type:= 1;
8808             --- 2178544
8809             l_pegging_rec.supply_demand_date:= l_res_requirements.requested_date(j);
8810             --l_pegging_rec.supply_demand_date:= l_requested_date;
8811 	    l_pegging_rec.department_id := l_res_requirements.department_id(j);
8812             l_pegging_rec.department_code := l_department_code;
8813 	    l_pegging_rec.resource_id := l_resource_id;
8814             l_pegging_rec.resource_code := l_resource_code;
8815 
8816             l_pegging_rec.inventory_item_id := NULL;
8817             l_pegging_rec.inventory_item_name := NULL;
8818             l_pegging_rec.supplier_id := NULL;
8819             l_pegging_rec.supplier_name := NULL;
8820             l_pegging_rec.supplier_site_id := NULL;
8821             l_pegging_rec.supplier_site_name := NULL;
8822 
8823               --- resource batching
8824              IF PG_DEBUG in ('Y', 'C') THEN
8825                 msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_use_batching = ' || l_use_batching);
8826                msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_batchable_flag = ' || l_batchable_flag);
8827             END IF;
8828             --- bug 1907419
8829             --IF (l_use_batching = 1 and l_batchable_flag = 1) THEN
8830             IF PG_DEBUG in ('Y', 'C') THEN
8831                msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'ADD batch flag to pegging');
8832             END IF;
8833             l_pegging_rec.batchable_flag := l_batchable_flag;
8834             --END IF;
8835 
8836             --diag_atp
8837             l_pegging_rec.pegging_type := MSC_ATP_PVT.RESOURCE_DEMAND; --resource demand node
8838 
8839             -- for demo:1153192
8840             l_pegging_rec.constraint_flag := 'N';
8841             l_pegging_rec.summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;
8842 
8843 	    -- dsting ATO 2465370
8844             --bug 3328421: store actual req date in req_date col and actual supply demand date in
8845 	    --l_pegging_rec.required_date := TRUNC(l_res_atp_date) + MSC_ATP_PVT.G_END_OF_DAY;
8846             l_pegging_rec.required_date := l_res_requirements.requested_date(j);
8847             l_pegging_rec.actual_supply_demand_date := TRUNC(l_res_atp_date) + MSC_ATP_PVT.G_END_OF_DAY;
8848 
8849             -- Bug 3348161
8850             IF PG_DEBUG in ('Y', 'C') THEN
8851                msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'G_ITEM_INFO_REC.base_item_id ' ||
8852                                       MSC_ATP_PVT.G_ITEM_INFO_REC.base_item_id);
8853             END IF;
8854             ---s_cto_rearch and ODR,
8855             -- Bug 3348161 set model_sd_flag for future case.
8856             l_pegging_rec.dest_inv_item_id := l_inventory_item_id;
8857             IF (MSC_ATP_PVT.G_ITEM_INFO_REC.bom_item_type in (1, 2) OR
8858                   -- Handle Configuration Items as well.
8859                   MSC_ATP_PVT.G_ITEM_INFO_REC.base_item_id is NOT NULL) THEN
8860                l_pegging_rec.model_sd_flag := 1;
8861                IF PG_DEBUG in ('Y', 'C') THEN
8862                  msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'Set model_sd_flag');
8863                END IF;
8864             END IF;
8865             --e_cto_rearch and ODR
8866             -- End Bug 3348161
8867              -- Bug 3826234
8868             l_pegging_rec.manufacturing_cal_code :=  NULL;
8869             l_pegging_rec.organization_type  := NVL ( MSC_ATP_PVT.G_ORG_INFO_REC.org_type, MSC_ATP_PVT.DISCRETE_ORG); --4775920
8870 
8871             MSC_ATP_DB_UTILS.Add_Pegging(l_pegging_rec, l_pegging_id);
8872 
8873             -- 1487344: instead of getting resource code and department code
8874             -- separately, we get them together to ensure we won't accidently
8875             -- get the line info.  Because we display either owing dept or
8876             -- dept for supply or demand pegging tree, we need to get the name
8877             -- each time.
8878 
8879             --diag_atp: we already got the owning department code before adding pegging for resource demand
8880             l_department_code := l_owning_department_code;
8881 
8882 
8883             /*MSC_ATP_PROC.get_dept_res_code(p_instance_id,
8884                               l_department_id,
8885                               l_resource_id,
8886                               p_organization_id,
8887                               l_department_code,
8888                               l_resource_code); */
8889 
8890             -- add pegging info for the supply
8891 IF PG_DEBUG in ('Y', 'C') THEN
8892    msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'in forward piece, adding resource supply pegging');
8893 END IF;
8894 
8895 
8896             l_pegging_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
8897             l_pegging_rec.order_line_id:= MSC_ATP_PVT.G_ORDER_LINE_ID;
8898             l_pegging_rec.parent_pegging_id:= l_pegging_id;
8899             l_pegging_rec.atp_level:= p_level+1;
8900             l_pegging_rec.organization_id:= p_organization_id;
8901             l_pegging_rec.organization_code := l_org_code;
8902             l_pegging_rec.identifier1:= p_instance_id;
8903             l_pegging_rec.identifier2:= p_plan_id;
8904             l_pegging_rec.identifier3 := -1;
8905 
8906             l_pegging_rec.scenario_id:= p_scenario_id;
8907             l_pegging_rec.supply_demand_source_type:= MSC_ATP_PVT.ATP;
8908             l_pegging_rec.supply_demand_quantity:= l_res_atp_qty;
8909             l_pegging_rec.supply_demand_type:= 2;
8910             l_pegging_rec.supply_demand_date:= l_res_atp_date;
8911             l_pegging_rec.department_id := l_department_id;
8912             l_pegging_rec.department_code := l_department_code;
8913             l_pegging_rec.resource_id := l_resource_id;
8914             l_pegging_rec.resource_code := l_resource_code;
8915             l_pegging_rec.inventory_item_id := NULL;
8916             l_pegging_rec.inventory_item_name := NULL;
8917             l_pegging_rec.supplier_id := NULL;
8918             l_pegging_rec.supplier_name := NULL;
8919             l_pegging_rec.supplier_site_id := NULL;
8920             l_pegging_rec.supplier_site_name := NULL;
8921 
8922              --- resource batching
8923             IF PG_DEBUG in ('Y', 'C') THEN
8924                msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_use_batching = ' || l_use_batching);
8925                msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'l_batchable_flag = ' || l_batchable_flag);
8926             END IF;
8927             --- bug 1907419
8928             ---IF (l_use_batching = 1 and l_batchable_flag = 1) THEN
8929             IF PG_DEBUG in ('Y', 'C') THEN
8930                msc_sch_wb.atp_debug('Get_Res_Requirements: ' || 'ADD batch flag to pegging');
8931             END IF;
8932             l_pegging_rec.batchable_flag := l_batchable_flag;
8933             ---END IF;
8934             --diag_atp
8935             l_pegging_rec.operation_sequence_id := l_res_requirements.operation_sequence(j);
8936             l_pegging_rec.usage := l_res_requirements.actual_resource_usage(j);
8937             l_pegging_rec.offset := l_res_requirements.resource_offset_percent(j);
8938             l_pegging_rec.efficiency := l_res_requirements.efficiency(j);
8939             l_pegging_rec.utilization := l_res_requirements.utilization(j);
8940             l_pegging_rec.REVERSE_CUM_YIELD := l_res_requirements.reverse_cumulative_yield(j);
8941             l_pegging_rec.owning_department := l_owning_department_code;
8942             l_pegging_rec.pegging_type := MSC_ATP_PVT.RESOURCE_SUPPLY; --resource supply node
8943             IF (l_use_batching = 1 AND l_batchable_flag = 1) THEN
8944                 l_pegging_rec.required_quantity:=l_requested_res_qty;
8945             ELSE
8946                 l_pegging_rec.required_quantity:=l_resource_hours;
8947             END IF;
8948             l_pegging_rec.required_date := TRUNC(l_requested_date) + MSC_ATP_PVT.G_END_OF_DAY;
8949             --bug 3328421:
8950             l_pegging_rec.actual_supply_demand_date :=  trunc(l_res_atp_date) + MSC_ATP_PVT.G_END_OF_DAY;
8951             l_pegging_rec.basis_type := l_res_requirements.basis_type(j);
8952             l_pegging_rec.allocation_rule := l_allocation_rule_name;
8953 
8954             -- Bug 3348161 Unset model_sd_flag for future supply case.
8955             l_pegging_rec.model_sd_flag := NULL; -- cto_rearch ODR unset flag for supply.
8956 
8957             l_pegging_rec.source_type := 0;
8958 
8959             -- for demo:1153192
8960             l_pegging_rec.constraint_flag := 'N';
8961             l_pegging_rec.summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;
8962 
8963             --s_cto_rearch
8964             IF l_res_atp_date > l_requested_date THEN
8965                IF PG_DEBUG in ('Y', 'C') THEN
8966 
8967                   msc_sch_wb.atp_debug(' Add resource constraint in regular ATP');
8968                END IF;
8969                --l_pegging_rec.constraint_type := 7;
8970                l_pegging_rec.constraint_type := 6; --bug3533073
8971             END IF;
8972             --e_cto_rearch
8973 
8974             -- Bug 3036513 Add Infinite_Time_fence and ATP Rule Data to Pegging
8975             l_pegging_rec.infinite_time_fence := l_infinite_time_fence_date;
8976             l_pegging_rec.atp_rule_name := l_atp_rule_name;
8977             -- End Bug 3036513
8978              -- Bug 3826234
8979             l_pegging_rec.manufacturing_cal_code :=  l_calendar_code;
8980             l_pegging_rec.organization_type  := NVL ( MSC_ATP_PVT.G_ORG_INFO_REC.org_type, MSC_ATP_PVT.DISCRETE_ORG); --4775920
8981 
8982             MSC_ATP_DB_UTILS.Add_Pegging(l_pegging_rec, l_pegging_id);
8983 
8984           END IF;
8985         END IF;
8986 
8987         IF PG_DEBUG in ('Y', 'C') THEN
8988            msc_sch_wb.atp_debug('in get_res_requirements we are here 1');
8989         END IF;
8990         FOR i in 1..l_atp_period.Level.COUNT LOOP
8991             l_atp_period.Pegging_Id(i) := l_pegging_id;
8992             l_atp_period.End_Pegging_Id(i) := MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
8993 
8994         END LOOP;
8995 
8996         IF PG_DEBUG in ('Y', 'C') THEN
8997            msc_sch_wb.atp_debug('in get_res_requirements we are here 2');
8998         END IF;
8999 
9000 	-- dsting supply/demand details pl/sql tables no longer used
9001 /*
9002         FOR i in 1..l_atp_supply_demand.Level.COUNT LOOP
9003             l_atp_supply_demand.Pegging_Id(i) := l_pegging_id;
9004             l_atp_supply_demand.End_Pegging_Id(i) := MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
9005         END LOOP;
9006 */
9007 
9008 	IF p_insert_flag <> 0 THEN
9009 		MSC_ATP_DB_UTILS.move_SD_temp_into_mrp_details(l_pegging_id,
9010 				      MSC_ATP_PVT.G_DEMAND_PEGGING_ID);
9011 	END IF;
9012 
9013         IF PG_DEBUG in ('Y', 'C') THEN
9014            msc_sch_wb.atp_debug('in get_res_requirements we are here 3');
9015         END IF;
9016 
9017         -- Bug 4108546
9018         -- Re-set the Period Data for HP Display
9019 
9020         FOR k IN 1..l_atp_period.level.COUNT LOOP
9021             IF (l_res_ptf_indx IS NOT NULL) THEN
9022                IF k <= l_res_ptf_indx THEN
9023                   l_atp_period.Cumulative_Quantity(k) := 0;
9024                ELSIF l_atp_period.Cumulative_Quantity(k)
9025                      <> MSC_ATP_PVT.INFINITE_NUMBER  THEN
9026                   l_atp_period.Cumulative_Quantity(k) :=
9027                   l_atp_period.Cumulative_Quantity(k) - l_res_qty_before_ptf;
9028                END IF;
9029                IF PG_DEBUG in ('Y', 'C') THEN
9030                      msc_sch_wb.atp_debug('Get_Res_Requirements: ' ||
9031                        'l_atp_period.Cumulative_Quantity(' || k || ') := ' ||
9032                            l_atp_period.Cumulative_Quantity(k));
9033                END IF;
9034             END IF;
9035         END LOOP;
9036         -- End Bug 4108546
9037 
9038         MSC_ATP_PROC.Details_Output(l_atp_period,
9039                        l_atp_supply_demand,
9040                        x_atp_period,
9041                        x_atp_supply_demand,
9042                        l_return_status);
9043 
9044         IF PG_DEBUG in ('Y', 'C') THEN
9045            msc_sch_wb.atp_debug('in get_res_requirements we are here 4');
9046         END IF;
9047 
9048         -- Bug 2814872
9049         IF (MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 2 AND p_search = 2 AND
9050                                   x_atp_date >= p_parent_ship_date) THEN
9051         -- No need to look at other resources in forward pass as for this resource
9052         -- the availability date is greater than or equal to the parent's ATP date.
9053         -- Note that when p_parent_ship_date is NULL all resources will get visited.
9054            IF PG_DEBUG in ('Y', 'C') THEN
9055                msc_sch_wb.atp_debug('Get_Res_Requirements date obtained for parent item is better');
9056                msc_sch_wb.atp_debug('Get_Res_Requirements date for parent item :' ||
9057                                                                      p_parent_ship_date);
9058                msc_sch_wb.atp_debug('Get_Res_Requirements date obtained for Resource :' ||
9059                                                      l_resource_id || ' is ' || x_atp_date);
9060            END IF;
9061            EXIT; -- Exit the loop
9062         ELSE -- diagnostic ATP or resource provides better date.
9063            j := l_res_requirements.resource_id.NEXT(j);
9064         END IF;
9065         -- End Bug 2814872;
9066 
9067       END LOOP;
9068 
9069   IF PG_DEBUG in ('Y', 'C') THEN
9070      msc_sch_wb.atp_debug('***** End Get_Res_Requirements Procedure *****');
9071   END IF;
9072 Exception
9073     WHEN MSC_ATP_PVT.NO_MATCHING_DATE_IN_CAL THEN --bug3583705
9074        IF PG_DEBUG in ('Y', 'C') THEN
9075             msc_sch_wb.atp_debug('Get_Res_Requirements ' || 'No match for sysdate in cal');
9076        END IF;
9077      RAISE MSC_ATP_PVT.NO_MATCHING_DATE_IN_CAL;
9078     WHEN OTHERS THEN
9079         x_return_status := FND_API.G_RET_STS_ERROR;
9080         IF PG_DEBUG in ('Y', 'C') THEN --bug3583705
9081            msc_sch_wb.atp_debug('Get_Res_Requirements ' || 'inside when others');
9082            msc_sch_wb.atp_debug ('error := ' || SQLERRM);
9083         END IF;
9084         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
9085 END Get_Res_Requirements;
9086 
9087 
9088 PROCEDURE Get_Comp_Requirements (
9089   p_instance_id				IN    NUMBER,
9090   p_plan_id                             IN    NUMBER,
9091   p_level 				IN    NUMBER,
9092   p_scenario_id				IN    NUMBER,
9093   p_inventory_item_id                   IN    NUMBER,
9094   p_organization_id                     IN    NUMBER,
9095   p_parent_pegging_id                   IN    NUMBER,
9096   p_demand_class			IN    VARCHAR2,
9097   p_requested_quantity			IN    NUMBER,
9098   p_requested_date		        IN    DATE,
9099   p_refresh_number			IN    NUMBER,
9100   p_insert_flag				IN    NUMBER,
9101   p_search                              IN    NUMBER,
9102   p_assign_set_id                       IN    NUMBER,
9103   --(ssurendr) Bug 2865389 Added routing Sequence id and Bill sequence id for OPM issue.
9104   p_routing_seq_id                      IN    NUMBER,
9105   p_bill_seq_id                         IN    NUMBER,
9106   p_family_id                           IN    NUMBER,   -- For time_phased_atp
9107   p_atf_date                            IN    DATE,     -- For time_phased_atp
9108   p_manufacturing_cal_code              IN    VARCHAR2, -- For ship_rec_cal
9109   x_avail_assembly_qty			OUT   NoCopy NUMBER,
9110   x_atp_date                            OUT   NoCopy DATE,
9111   x_atp_period                          OUT   NOCOPY MRP_ATP_PUB.ATP_Period_Typ,
9112   x_atp_supply_demand                   OUT   NOCOPY MRP_ATP_PUB.ATP_Supply_Demand_Typ,
9113   x_return_status                       OUT   NoCopy VARCHAR2,
9114   p_comp_info_rec                       IN OUT NOCOPY MSC_ATP_REQ.get_comp_info_rec,
9115   p_order_number                        IN    NUMBER := NULL,
9116   p_op_seq_id                           IN    NUMBER --4570421
9117        -- Add new parameter with default value to support creation of
9118        -- Sales Orders for CTO components in a MATO case.
9119 )
9120 IS
9121 l_comp_requirements 		MRP_ATP_PVT.Atp_Comp_Typ;
9122 l_explode_comp                  MRP_ATP_PVT.Atp_Comp_Typ;
9123 l_atp_period_tab                MRP_ATP_PUB.date_arr:=MRP_ATP_PUB.date_arr();
9124 l_atp_qty_tab                   MRP_ATP_PUB.number_arr:=MRP_ATP_PUB.number_arr();
9125 l_requested_date_quantity	number;
9126 l_resource_id			number;
9127 l_department_id 		number;
9128 l_requested_date 		date;
9129 l_resource_usage 		number;
9130 l_basis_type 			number;
9131 l_requested_res_qty		number;
9132 l_avail_assembly_qty		number;
9133 l_next_period			date;
9134 i				PLS_INTEGER;
9135 j				PLS_INTEGER;
9136 l_requested_comp_qty		number;
9137 l_atp_rec               	MRP_ATP_PVT.AtpRec;
9138 l_atp_period            	MRP_ATP_PUB.ATP_Period_Typ;
9139 l_atp_supply_demand     	MRP_ATP_PUB.ATP_Supply_Demand_Typ;
9140 
9141 l_comp_item_id			number;
9142 l_comp_usage			number;
9143 --4570421
9144 l_scaling_type                  NUMBER;
9145 l_scale_multiple                number;
9146 l_scale_rounding_variance       number;
9147 l_rounding_direction            number;
9148 l_component_yield_factor        NUMBER;
9149 l_usage_qty                     NUMBER;
9150 l_op_seq_id                     NUMBER; --4570421
9151 l_comp_date			date;
9152 l_comp_lead_time		number;
9153 l_comp_pre_pro_lead_time	number;
9154 l_comp_wip_supply_type		number;
9155 l_comp_assembly_identifier	number;
9156 l_comp_component_identifier	number;
9157 l_plan_id                       number;
9158 l_assign_set_id                 number;
9159 l_cto_bom                       number := 0;
9160 l_routing_type	        	number;
9161 l_first_op_RCY			number;
9162 -- l_inv_item_id             	number; -- Not required after 3004862 fix
9163 l_processing_lead_time    	number;
9164 l_temp_date		        DATE;
9165 l_mso_lead_time_factor		number;
9166 l_pre_processing_lead_time	NUMBER;
9167 l_fix_var_lead_time		NUMBER;
9168 l_future_order_date             DATE;
9169 l_ptf_date                      DATE;
9170 l_ptf_flag			NUMBER;
9171 l_plan_info_rec                 MSC_ATP_PVT.plan_info_rec;      -- added for bug 2392456
9172 
9173 l_comp_source_organization_id   NUMBER; -- krajan : 2400614
9174 l_comp_atp_flag_src             VARCHAR2(1); -- krajan : 2462661
9175 
9176 l_reverse_cumulative_yield      NUMBER;
9177 
9178 --(3004862) circular BOM issue
9179 l_bill_seq_id                   NUMBER;
9180 l_routing_seq_id                NUMBER;
9181 l_process_seq_id                NUMBER;
9182 
9183 ----OSFM Changes
9184 --- cursor  for network routing
9185 
9186 -- 2869830
9187 l_rounding_flag                 number;
9188 
9189 --s_cto_rearch
9190 l_component_date               date;
9191 l_model_flag                   number :=2;
9192 L_PLAN_FOUND_FOR_MATCH         number;
9193 L_MODEL_ATP_COMP_FLAG          varchar2(1);
9194 L_MATCH_ITEM_ID                number;
9195 L_CTO_BOM_REC                  MRP_ATP_PVT.Atp_Comp_Typ;
9196 l_lead_time                    number;
9197 L_REQUEST_DATE                 date;
9198 L_MAND_COMP_INFO_REC           MSC_ATP_CTO.mand_comp_info_rec;
9199 l_atp_comp_rec                 MRP_ATP_PVT.ATP_COMP_REC;
9200 l_null_atp_comp_rec            MRP_ATP_PVT.ATP_COMP_REC;
9201 l_item_info_rec                MSC_ATP_PVT.item_attribute_rec;
9202 l_model_error_code                  number := 0;
9203 --e_cto_rearch
9204 
9205 -- time_phased_atp
9206 l_atf_date                     date;
9207 l_pegging_rec                  mrp_atp_details_temp%ROWTYPE;
9208 l_org_code                     varchar2(7);
9209 l_pegging_id                   number;
9210 l_return_status                varchar2(1);
9211 l_network_scheduling_method    NUMBER; --bug3601223
9212 l_first_op_seq_num			   NUMBER; -- Bug 4143668
9213 
9214 -- ATP4drp default variables
9215 l_def_wip_sup_type             NUMBER := 1;
9216 l_def_atf_date                 DATE := NULL;
9217 l_comp_uom                    varchar2(3); --bug3110023
9218 
9219 CURSOR net_rout_comp (l_bill_seq_id number, --(3004862) circular BOM issue: changed cursor parameter
9220              l_requested_date date,
9221              l_requested_quantity number,
9222              l_wip_supply_type number) IS --4106269
9223     select  v1.SR_INVENTORY_ITEM_ID,
9224             v1.INVENTORY_ITEM_ID,
9225             v1.qty,
9226             v1.CALENDAR_DATE,
9227             v1.PROCESSING_LEAD_TIME,
9228             v1.WIP_SUPPLY_TYPE,
9229             v1.PREPROCESSING_LEAD_TIME,
9230             v1.REVERSE_CUMULATIVE_YIELD,
9231             v1.AGGREGATE_TIME_FENCE_DATE,
9232             v1.UOM_CODE, --bug3110023
9233             v1.scaling_type,
9234             v1.SCALE_MULTIPLE,
9235             v1.SCALE_ROUNDING_VARIANCE,
9236             v1.ROUNDING_DIRECTION,
9237             v1.component_yield_factor,
9238             v1.usage_qty --4775920
9239     from
9240        (SELECT   I2.SR_INVENTORY_ITEM_ID,
9241                 I2.INVENTORY_ITEM_ID, --(3004862) circular BOM issue: also select destination id
9242                 --4570421
9243                 --4862863, dividing by assembly_qty only in non-lot and non-fix cases.
9244                 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)
9245                                                                                                                                                 /Decode (MSC_ATP_PVT.G_PLAN_COPRODUCTS, 'Y', NVL (BOMS.ASSEMBLY_QUANTITY,1), 1),
9246 	                                                                                                                                    2,  MBC.USAGE_QUANTITY),
9247 	                                                                            MSC_ATP_PVT.OPM_ORG, decode (nvl (mbc.scaling_type, 1), 0,  MBC.USAGE_QUANTITY,
9248 	                                                                                                                                    1, (MBC.USAGE_QUANTITY*l_requested_quantity)
9249 	                                                                                                                                        /Decode (MSC_ATP_PVT.G_PLAN_COPRODUCTS, 'Y', NVL (BOMS.ASSEMBLY_QUANTITY,1), 1),
9250 	                                                                                                                                    2,  MBC.USAGE_QUANTITY,
9251 	                                                                                                                                    3, (MBC.USAGE_QUANTITY*l_requested_quantity)
9252 	                                                                                                                                        /Decode (MSC_ATP_PVT.G_PLAN_COPRODUCTS, 'Y', NVL (BOMS.ASSEMBLY_QUANTITY,1), 1),
9253 	                                                                                                                                    4, (MBC.USAGE_QUANTITY*l_requested_quantity)
9254 	                                                                                                                                        /Decode (MSC_ATP_PVT.G_PLAN_COPRODUCTS, 'Y', NVL (BOMS.ASSEMBLY_QUANTITY,1), 1),
9255 	                                                                                                                                    5, (MBC.USAGE_QUANTITY*l_requested_quantity)
9256 	                                                                                                                                        /Decode (MSC_ATP_PVT.G_PLAN_COPRODUCTS, 'Y', NVL (BOMS.ASSEMBLY_QUANTITY,1), 1))
9257 	               ))
9258 	               --/Decode (MSC_ATP_PVT.G_PLAN_COPRODUCTS, 'Y', NVL (BOMS.ASSEMBLY_QUANTITY,1), 1) --4862863
9259 	                  * DECODE (l_routing_type, 3,Decode (l_network_scheduling_method,
9260 	                                                      2,(NVL(OP.NET_PLANNING_PERCENT,100)/100),1), 1)
9261 	                 --/NVL (OP.REVERSE_CUMULATIVE_YIELD, DECODE (l_routing_type, 3, NVL (l_first_op_RCY, 1), 1))
9262 	                 /DECODE(OP.REVERSE_CUMULATIVE_YIELD,
9263                                 0,
9264                                 DECODE(l_routing_type,
9265                                        3, NVL(l_first_op_RCY, 1)
9266                                        ,1
9267                                        ),
9268                                        NVL(OP.REVERSE_CUMULATIVE_YIELD, DECODE(l_routing_type,
9269                                                                                3,
9270                                                                                NVL(l_first_op_RCY, 1),1
9271                                                                                )
9272                                            )
9273                                 )
9274 	                 --/NVL (mbc.component_yield_factor, 1) --4767982
9275 	        ,6) qty,
9276                 C2.CALENDAR_DATE,
9277                 --bug 4106269 changes start here
9278                 /*----------------------------------------------------------------------
9279                 We will include the Lead time for phantom items based on following parameter
9280                 1)MSC: ATP explode phantom components
9281                 2)Bom Parameter -Use Phantom Routing
9282                 3)Bom Parameter -Inherit Phantom Op-Seq
9283                 Various combinations are
9284                 Case1 :
9285                 MSC: ATP explode phantom components =Yes
9286                 Use Phantom Routing =N/a
9287                 Inherit Phantom Op-Seq =N/a
9288 
9289                 Creates supply for ATPable phantom and uses its LT and Routing like a standard item.
9290 
9291                 Case 2:
9292                 MSC: ATP explode phantom components =No
9293                 Use Phantom Routing =No
9294                 Inherit Phantom Op-Seq =No
9295 
9296 		Ignore ATPable phantom's Lead Time for calculating components requirement dates.
9297 		Phantom is exploded to its components and no supply/resource requirements are created for phantom.
9298 
9299 
9300 		Case 3:
9301 		MSC: ATP explode phantom components =No
9302                 Use Phantom Routing =Yes
9303                 Inherit Phantom Op-Seq =No
9304 
9305                 Adds ATPable phantom's Lead Time for calculating components requirement dates.
9306                 Phantom is exploded to its components and no supply/resource requirements are created for phantom.
9307 
9308                 Case 4:
9309                 MSC: ATP explode phantom components =No
9310                 Use Phantom Routing =No
9311                 Inherit Phantom Op-Seq =Yes
9312 
9313                 Ignore ATPable phantom's Lead Time for calculating components requirement dates.
9314                 Phantom is exploded to its components and no supply/resource requirements are created for phantom.
9315 
9316                 Case 5:
9317 
9318 		MSC: ATP explode phantom components =No
9319                 Use Phantom Routing =Yes
9320                 Inherit Phantom Op-Seq =Yes
9321 
9322                 Adds ATPable phantom's Lead Time for calculating components requirement dates.
9323                 Phantom is exploded to its components and no supply/resource requirements are created for phantom.
9324                 --------------------------------------------------------------------------*/
9325                 DECODE(l_wip_supply_type,
9326        				       6,
9327        				 	DECODE(MSC_ATP_PVT.G_EXPLODE_PHANTOM,
9328        					'N',
9329        					decode(nvl(MSC_ATP_PVT.G_ORG_INFO_REC.use_phantom_routings,2),
9330        					       2,
9331        					       0,
9332        					       CEIL((NVL(I.FIXED_LEAD_TIME,0)+
9333        				                     NVL(I.VARIABLE_LEAD_TIME,0)*
9334        					                 l_requested_quantity
9335        					                 )
9336        					                 *(1 + l_mso_lead_time_factor)
9337        						      )
9338        						     ),
9339        						     CEIL( ( NVL(I.FIXED_LEAD_TIME,0)+
9340        							     NVL(I.VARIABLE_LEAD_TIME,0)*
9341        							     l_requested_quantity
9342        							    )*
9343        							   (1 + l_mso_lead_time_factor)
9344        							  )
9345        					        ),
9346        						CEIL((NVL(I.FIXED_LEAD_TIME,0)+
9347        						      NVL(I.VARIABLE_LEAD_TIME,0)*
9348        						      l_requested_quantity)*
9349        						      (1 + l_mso_lead_time_factor)
9350        						     )
9351        		        ) PROCESSING_LEAD_TIME,
9352        		        --4106269
9353                 MBC.WIP_SUPPLY_TYPE,
9354                 --bug3609031 adding ceil
9355                 NVL(ceil(I.PREPROCESSING_LEAD_TIME),0) PREPROCESSING_LEAD_TIME,
9356                 --diag_atp
9357                 --NVL(OP.REVERSE_CUMULATIVE_YIELD, DECODE(l_routing_type, 3, NVL(l_first_op_RCY, 1),1)) REVERSE_CUMULATIVE_YIELD,
9358                 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
9359                 -- time_phased_atp
9360                 I2.AGGREGATE_TIME_FENCE_DATE,
9361                 OP.OPERATION_SEQUENCE_ID,
9362                 I2.UOM_CODE, --bug3110023
9363                 --4570421
9364                 mbc.scaling_type scaling_type,
9365                 mbc.scale_multiple scale_multiple,
9366                 mbc.scale_rounding_variance scale_rounding_variance,
9367                 mbc.rounding_direction rounding_direction,
9368                 mbc.component_yield_factor component_yield_factor,
9369                 MBC.USAGE_QUANTITY*mbc.component_yield_factor usage_qty --4775920
9370        FROM     MSC_SYSTEM_ITEMS I2,
9371                 MSC_CALENDAR_DATES C2,
9372 	        MSC_CALENDAR_DATES C1,
9373                 MSC_BOM_COMPONENTS MBC,
9374                 MSC_BOMS BOMS,
9375                 MSC_SYSTEM_ITEMS  I,
9376 	        MSC_OPERATION_COMPONENTS OPC,
9377                 MSC_ROUTING_OPERATIONS OP
9378        WHERE    I.PLAN_ID = BOMS.PLAN_ID
9379        AND      I.SR_INSTANCE_ID = BOMS.SR_INSTANCE_ID
9380        AND      I.INVENTORY_ITEM_ID = BOMS.ASSEMBLY_ITEM_ID
9381        AND      I.ORGANIZATION_ID = BOMS.ORGANIZATION_ID
9382        AND      BOMS.PLAN_ID = p_plan_id
9383        AND      BOMS.SR_INSTANCE_ID = p_instance_id
9384        AND      BOMS.ORGANIZATION_ID = p_organization_id
9385        AND      BOMS.BILL_SEQUENCE_ID = l_bill_seq_id --(3004862) circular BOM issue: use cursor parameter
9386        --(ssurendr) Bug 2865389 Removed condition for Alternate bom designator as
9387        --we are accessing bom by bill sequance id.
9388        --We can now drive by msc_boms for performance gains
9389        --AND      BOMS.ALTERNATE_BOM_DESIGNATOR IS NULL
9390        AND      MBC.USAGE_QUANTITY > 0
9391        AND      MBC.BILL_SEQUENCE_ID = BOMS.BILL_SEQUENCE_ID
9392        AND      MBC.PLAN_ID = I.PLAN_ID
9393        AND      MBC.SR_INSTANCE_ID = I.SR_INSTANCE_ID
9394        --s_cto_rearch: we do not look at atp flags any more. Slection of components is contolled by
9395        --atp flags setting of the components itself
9396        --AND      MBC.ATP_FLAG = 1
9397        /* rajjain 3008611
9398         * effective date should be greater than or equal to greatest of PTF date, sysdate and start date
9399         * disable date should be less than or equal to greatest of PTF date, sysdate and start date*/
9400        AND      TRUNC(NVL(MBC.DISABLE_DATE, GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE)+1)) >
9401                         TRUNC(GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE))
9402        AND      TRUNC(MBC.EFFECTIVITY_DATE) <=
9403                         TRUNC(GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE)) -- bug 1404312
9404        AND      C1.CALENDAR_DATE = l_requested_date
9405        AND      C1.SR_INSTANCE_ID = I.SR_INSTANCE_ID
9406        --4106269
9407        AND      C2.SEQ_NUM = C1.PRIOR_SEQ_NUM -  DECODE(l_wip_supply_type,
9408        				       6,
9409        				 	DECODE(MSC_ATP_PVT.G_EXPLODE_PHANTOM,
9410        					'N',
9411        					decode(nvl(MSC_ATP_PVT.G_ORG_INFO_REC.use_phantom_routings,2),
9412        					       2,
9413        					       0,
9414        					       CEIL((NVL(I.FIXED_LEAD_TIME,0)+
9415        				                     NVL(I.VARIABLE_LEAD_TIME,0)*
9416        					                 l_requested_quantity
9417        					                 )
9418        					                 *(1 + l_mso_lead_time_factor)
9419        						      )
9420        						     ),
9421        						     CEIL( ( NVL(I.FIXED_LEAD_TIME,0)+
9422        							     NVL(I.VARIABLE_LEAD_TIME,0)*
9423        							     l_requested_quantity
9424        							    )*
9425        							   (1 + l_mso_lead_time_factor)
9426        							  )
9427        					        ),
9428        						CEIL((NVL(I.FIXED_LEAD_TIME,0)+
9429        						      NVL(I.VARIABLE_LEAD_TIME,0)*
9430        						      l_requested_quantity)*
9431        						      (1 + l_mso_lead_time_factor)
9432        						     )
9433        		        )--4106269
9434        AND      C2.SR_INSTANCE_ID = I.SR_INSTANCE_ID
9435        AND      I2.INVENTORY_ITEM_ID = MBC.INVENTORY_ITEM_ID
9436        AND      I2.ORGANIZATION_ID =MBC.ORGANIZATION_ID
9437        AND      I2.PLAN_ID = I.PLAN_ID
9438        --4570421
9439        AND      C1.CALENDAR_CODE = MSC_ATP_PVT.G_ORG_INFO_REC.CAL_CODE
9440        AND      C1.EXCEPTION_SET_ID = MSC_ATP_PVT.G_ORG_INFO_REC.CAL_EXCEPTION_SET_ID
9441        AND      C2.CALENDAR_CODE = C1.CALENDAR_CODE
9442        AND      C2.EXCEPTION_SET_ID = MSC_ATP_PVT.G_ORG_INFO_REC.CAL_EXCEPTION_SET_ID
9443        --s_cto_rearch
9444        -- select only atpable components. For model and option class, we do not look at atp components
9445        --flag as they do not carry any significance once config is created
9446        AND (I2.atp_flag <> 'N' or I2.atp_components_flag <> DECODE(I2.BOM_ITEM_TYPE,
9447                                                                           1, I2.atp_components_flag,
9448                                                                           2, I2.atp_components_flag,
9449                                                                           'N'))
9450        AND      I2.SR_INSTANCE_ID = I.SR_INSTANCE_ID
9451        AND      OPC.PLAN_ID (+) = MBC.PLAN_ID
9452        AND      OPC.ORGANIZATION_ID (+) = MBC.ORGANIZATION_ID
9453        AND      OPC.SR_INSTANCE_ID (+) = MBC.SR_INSTANCE_ID
9454        AND      OPC.COMPONENT_SEQUENCE_ID (+) = MBC.COMPONENT_SEQUENCE_ID
9455        AND      OPC.BILL_SEQUENCE_ID (+) = MBC.BILL_SEQUENCE_ID
9456        AND      OP.PLAN_ID (+) = OPC.PLAN_ID
9457        AND      OP.SR_INSTANCE_ID (+) = OPC.SR_INSTANCE_ID
9458        AND      OP.OPERATION_SEQUENCE_ID (+) = OPC.OPERATION_SEQUENCE_ID
9459        AND      OP.ROUTING_SEQUENCE_ID (+) = OPC.ROUTING_SEQUENCE_ID ) v1
9460        --bug3601223 get the components that are used on primary path
9461     where       l_routing_type <> 3
9462        OR       l_network_scheduling_method = 2
9463        OR       v1.OPERATION_SEQUENCE_ID IS NULL
9464        OR       v1.OPERATION_SEQUENCE_ID IN
9465                               ( SELECT FROM_OP_SEQ_ID
9466                                 FROM  MSC_OPERATION_NETWORKS
9467                                 WHERE  PLAN_ID = p_plan_id
9468 				AND    SR_INSTANCE_ID = p_instance_id
9469 				AND    ROUTING_SEQUENCE_ID = p_routing_seq_id
9470 				AND    TRANSITION_TYPE = 1
9471 
9472                                 UNION ALL
9473 
9474                                 SELECT TO_OP_SEQ_ID
9475 				FROM  MSC_OPERATION_NETWORKS
9476 				WHERE  PLAN_ID = p_plan_id
9477 				AND    SR_INSTANCE_ID = p_instance_id
9478 				AND    ROUTING_SEQUENCE_ID = p_routing_seq_id
9479 				AND    TRANSITION_TYPE = 1
9480 			      );
9481 
9482 -- ATP4drp New Cursor for handling DRP Kitting.
9483 -- Routing, Net Planning Percent and Operations not applicable for DRP plans.
9484 -- Co-products also not applicable for DRP plans.
9485 CURSOR drp_comp (l_bill_seq_id number, --(3004862) circular BOM issue: changed cursor parameter
9486              l_requested_date date,
9487              l_requested_quantity number) IS
9488        SELECT   I2.SR_INVENTORY_ITEM_ID,
9489                 I2.INVENTORY_ITEM_ID, --(3004862) circular BOM issue: also select destination id
9490                 --ROUND((MBC.USAGE_QUANTITY * l_requested_quantity ),6),
9491                 --4570421
9492                 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),
9493 	                                                                                                                                   2, MBC.USAGE_QUANTITY),
9494 	                                                                           MSC_ATP_PVT.OPM_ORG, decode (nvl (mbc.scaling_type, 1), 0, MBC.USAGE_QUANTITY,
9495 	                                                                                                                                   1, (MBC.USAGE_QUANTITY*l_requested_quantity),
9496 	                                                                                                                                   2, MBC.USAGE_QUANTITY,
9497 	                                                                                                                                   3, (MBC.USAGE_QUANTITY*l_requested_quantity),
9498 	                                                                                                                                   4, (MBC.USAGE_QUANTITY*l_requested_quantity),
9499 	                                                                                                                                   5, (MBC.USAGE_QUANTITY*l_requested_quantity))
9500 	               )) --/NVL (mbc.component_yield_factor, 1)     --4767982
9501 	               ,6),
9502                 C2.CALENDAR_DATE,
9503                 CEIL((NVL(MSC_ATP_PVT.G_ITEM_INFO_REC.FIXED_LT,0)+
9504                         NVL(MSC_ATP_PVT.G_ITEM_INFO_REC.VARIABLE_LT,0)* l_requested_quantity)*(1 + l_mso_lead_time_factor)),
9505                 DECODE(MBC.WIP_SUPPLY_TYPE, 6, l_def_wip_sup_type, MBC.WIP_SUPPLY_TYPE), -- phantoms not be supported for DRP plans
9506                 NVL(ceil(MSC_ATP_PVT.G_ITEM_INFO_REC.PRE_PRO_LT),0),
9507                 1, -- default for RCY Yield unsuppored for DRP plans.
9508                 l_def_atf_date, -- ATF will be NULL as a default for DRP plans.
9509                 I2.UOM_CODE, --bug3110023
9510                 --4570421
9511                 mbc.scaling_type,
9512                 mbc.scale_multiple,
9513                 mbc.scale_rounding_variance ,
9514                 mbc.rounding_direction,
9515                 mbc.component_yield_factor, --4570421
9516                 MBC.USAGE_QUANTITY*mbc.component_yield_factor --4775920
9517        FROM     MSC_SYSTEM_ITEMS I2,
9518                 MSC_CALENDAR_DATES C2,
9519 	        MSC_CALENDAR_DATES C1,
9520                 MSC_BOM_COMPONENTS MBC,
9521                 MSC_BOMS BOMS
9522        WHERE    BOMS.PLAN_ID = p_plan_id
9523        AND      BOMS.SR_INSTANCE_ID = p_instance_id
9524        AND      BOMS.ORGANIZATION_ID = p_organization_id
9525        AND      BOMS.BILL_SEQUENCE_ID = l_bill_seq_id --(3004862) circular BOM issue: use cursor parameter
9526        AND      MBC.USAGE_QUANTITY > 0
9527        AND      MBC.BILL_SEQUENCE_ID = BOMS.BILL_SEQUENCE_ID
9528        AND      MBC.PLAN_ID = BOMS.PLAN_ID
9529        AND      MBC.SR_INSTANCE_ID = BOMS.SR_INSTANCE_ID
9530        AND      TRUNC(NVL(MBC.DISABLE_DATE, GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE)+1)) >
9531                         TRUNC(GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE))
9532        AND      TRUNC(MBC.EFFECTIVITY_DATE) <=
9533                         TRUNC(GREATEST(C2.CALENDAR_DATE, sysdate, MSC_ATP_PVT.G_PTF_DATE)) -- bug 1404312
9534        AND      C1.CALENDAR_DATE = l_requested_date
9535        AND      C1.SR_INSTANCE_ID = BOMS.SR_INSTANCE_ID
9536        AND      C1.CALENDAR_CODE = MSC_ATP_PVT.G_ORG_INFO_REC.CAL_CODE
9537        AND      C1.EXCEPTION_SET_ID = MSC_ATP_PVT.G_ORG_INFO_REC.CAL_EXCEPTION_SET_ID
9538        AND      C2.SEQ_NUM = C1.PRIOR_SEQ_NUM - CEIL((NVL(MSC_ATP_PVT.G_ITEM_INFO_REC.FIXED_LT,0)+
9539                         NVL(MSC_ATP_PVT.G_ITEM_INFO_REC.VARIABLE_LT,0)* l_requested_quantity)*(1 + l_mso_lead_time_factor))
9540        AND      C2.CALENDAR_CODE = C1.CALENDAR_CODE
9541        AND      C2.SR_INSTANCE_ID = C1.SR_INSTANCE_ID
9542        AND      C2.EXCEPTION_SET_ID = MSC_ATP_PVT.G_ORG_INFO_REC.CAL_EXCEPTION_SET_ID
9543        AND      I2.INVENTORY_ITEM_ID = MBC.INVENTORY_ITEM_ID
9544        AND      I2.ORGANIZATION_ID =MBC.ORGANIZATION_ID
9545        AND      I2.PLAN_ID = BOMS.PLAN_ID
9546                 -- select only atpable components.
9547        AND      I2.atp_flag <> 'N'
9548        AND      I2.SR_INSTANCE_ID = BOMS.SR_INSTANCE_ID;
9549 -- End ATP4drp New Cursor for handling DRP Kitting.
9550 
9551 CURSOR cto_comp (l_inventory_item_id number,
9552              l_requested_date date,
9553              l_requested_quantity number,
9554              l_wip_supply_type number) IS
9555        SELECT   mbt.component_item_id,
9556                 (mbt.quantity * l_requested_quantity),
9557                 c2.calendar_date,
9558                 DECODE(l_wip_supply_type,
9559 			6, 0,
9560 			CEIL((NVL(mbt.fixed_lt,0)+
9561                         NVL(mbt.variable_lt,0) * l_requested_quantity)*(1 + l_mso_lead_time_factor))) lead_time,
9562                 wip_supply_type,
9563 		mbt.assembly_identifier,
9564 		mbt.component_identifier,
9565                 mbt.pre_process_lt,
9566                 -- krajan : 2400614
9567                 mbt.source_organization_id,
9568                 -- krajan : 2462661
9569                 mbt.atp_flag
9570        FROM     msc_bom_temp mbt,
9571                 msc_calendar_dates c2,
9572                 msc_calendar_dates c1,
9573                 msc_trading_partners tp
9574        WHERE	mbt.session_id = MSC_ATP_PVT.G_SESSION_ID
9575        --AND      mbt.assembly_identifier = MSC_ATP_PVT.G_ASSEMBLY_LINE_ID
9576        AND      mbt.assembly_identifier = MSC_ATP_PVT.G_COMP_LINE_ID
9577        AND      mbt.assembly_item_id = l_inventory_item_id
9578        /* rajjain 3008611
9579         * effective date should be greater than or equal to greatest of PTF date, sysdate and start date
9580         * disable date should be less than or equal to greatest of PTF date, sysdate and start date*/
9581        AND      TRUNC(NVL(mbt.disable_date, GREATEST(sysdate, c2.calendar_date, MSC_ATP_PVT.G_PTF_DATE)+1)) >
9582                   TRUNC(GREATEST(sysdate, c2.calendar_date, MSC_ATP_PVT.G_PTF_DATE))
9583        AND      TRUNC(mbt.effective_date) <=
9584                   TRUNC(GREATEST(sysdate, c2.calendar_date, MSC_ATP_PVT.G_PTF_DATE))
9585        AND	c1.calendar_date = l_requested_date
9586        AND      c1.sr_instance_id = tp.sr_instance_id
9587        AND      c1.calendar_code = tp.calendar_code
9588        AND      c1.exception_set_id = tp.calendar_exception_set_id
9589        AND      tp.sr_instance_id = p_instance_id
9590        AND      tp.sr_tp_id = p_organization_id
9591        AND      tp.partner_type = 3
9592        AND      c2.seq_num = c1.prior_seq_num -
9593                              DECODE(l_wip_supply_type,
9594                                 6, 0,
9595                                 CEIL((NVL(mbt.fixed_lt,0)+
9596                                      NVL(mbt.variable_lt,0) * l_requested_quantity)*(1 + l_mso_lead_time_factor)))
9597        AND      c2.calendar_code = tp.calendar_code
9598        AND      c2.sr_instance_id = tp.sr_instance_id
9599        AND      c2.exception_set_id = tp.calendar_exception_set_id;
9600 
9601 BEGIN
9602     -- initialize API return status to success
9603     x_return_status := FND_API.G_RET_STS_SUCCESS;
9604 
9605     IF PG_DEBUG in ('Y', 'C') THEN
9606        msc_sch_wb.atp_debug('***** Begin Get_Comp_Requirements *****');
9607     END IF;
9608 
9609     -- Now get the material requirement
9610     IF PG_DEBUG in ('Y', 'C') THEN
9611        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'p_instance_id = '||p_instance_id);
9612        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'p_plan_id = '||p_plan_id);
9613        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'p_level = '||p_level);
9614        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'p_scenario_id = '||p_scenario_id);
9615        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'p_inventory_item_id = '||p_inventory_item_id);
9616        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'p_organization_id = '||p_organization_id);
9617        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'p_parent_pegging_id = '||p_parent_pegging_id);
9618        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'p_demand_class = '||p_demand_class);
9619        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'p_requested_quantity = '||p_requested_quantity);
9620        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'p_requested_date = '||p_requested_date);
9621        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'p_search = '||p_search);
9622        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'p_assign_set_id = '||p_assign_set_id);
9623        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'p_order_number = '||p_order_number);
9624        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'p_routing_seq_id = '||p_routing_seq_id);
9625        msc_sch_wb.atp_debug('bom_item_type := ' || p_comp_info_rec.bom_item_type);
9626        msc_sch_wb.atp_debug('atp flag := ' || p_comp_info_rec.atp_flag);
9627        msc_sch_wb.atp_debug('atp_comp_flag := ' || p_comp_info_rec.atp_comp_flag);
9628        msc_sch_wb.atp_debug('p_bill_seq_id := ' || p_bill_seq_id);
9629         msc_sch_wb.atp_debug('parent_so_quantity := ' || p_comp_info_rec.parent_so_quantity);
9630        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'p_family_id := ' || p_family_id);
9631        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'p_atf_date := ' || p_atf_date);
9632        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'TOP_MODEL_LINE_ID :=' || p_comp_info_rec.TOP_MODEL_LINE_ID);
9633        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'ATO_MODEL_LINE_ID :=' || p_comp_info_rec.ATO_MODEL_LINE_ID);
9634        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'ATO_PARENT_MODEL_LINE_ID := ' || p_comp_info_rec.ATO_PARENT_MODEL_LINE_ID);
9635        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'PARENT_LINE_ID := ' || p_comp_info_rec.PARENT_LINE_ID);
9636        --bug 3059305
9637        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'ship date this level := '
9638                                                       || p_comp_info_rec.ship_date_this_level);
9639 
9640 
9641     END IF;
9642     --S_cto_rearch: If ATP flag on config is 'Y, N' then we check base model's
9643     -- cpacity based on bom level atp flag and atp flags of base model
9644     -- In this case we just need to check capacity on model. So model is only
9645     --component we need to check capacity on.
9646     MSC_ATP_PROC.get_global_item_info(p_instance_id,
9647                                       --3917625: Read data from the plan
9648                                       -- -1,
9649                                       p_plan_id,
9650                                       p_inventory_item_id,
9651                                       p_organization_id,
9652                                       l_item_info_rec);
9653     IF  p_comp_info_rec.check_model_capacity_flag = 1 THEN
9654        --check model's capacity. Add model to comp array
9655 
9656        MSC_ATP_REQ.Extend_Atp_Comp_Typ(l_comp_requirements);
9657 
9658        l_comp_requirements.inventory_item_id(1) :=  MSC_ATP_PF.Get_PF_Atp_Item_Id(
9659                                                                 p_instance_id,
9660                                                                 -1, -- plan_id
9661                                                                 p_comp_info_rec.model_sr_inv_item_id,
9662                                                                 p_organization_id
9663                                                         );
9664        l_comp_requirements.request_item_id(1) :=
9665                          p_comp_info_rec.model_sr_inv_item_id;
9666 
9667        -- I used this comp_usaget to store the required quantity at this level.
9668        l_comp_requirements.comp_usage(1) := p_requested_quantity;
9669 
9670        l_comp_requirements.requested_date(1) := p_requested_date;
9671 
9672        -- assume the wip supply type to be 1 (as long as it is not phantom)
9673        l_comp_requirements.wip_supply_type(1) := 1;
9674        l_comp_requirements.component_identifier(1) := MSC_ATP_PVT.G_ITEM_INFO_REC.dest_inv_item_id;
9675 
9676        l_comp_requirements.bom_item_type(1) :=  1; -- hard code to model's bom type
9677        l_comp_requirements.parent_so_quantity(1) := p_comp_info_rec.parent_so_quantity;
9678        l_comp_requirements.assembly_identifier(1) :=  null;
9679        l_comp_requirements.top_model_line_id(1) := null;
9680        l_comp_requirements.ato_parent_model_line_id(1) := null;
9681        l_comp_requirements.ato_model_line_id(1) := null;
9682        l_comp_requirements.parent_line_id(1) := null;
9683        l_comp_requirements.fixed_lt(1) := 0;
9684        l_comp_requirements.variable_lt(1) := 0;
9685        l_comp_requirements.dest_inventory_item_id(1) :=  null;
9686        l_comp_requirements.lead_time(1) := 0;
9687 
9688     ELSE
9689 
9690        -- assign this assembly into the l_explode_comp record of tables.
9691        -- we will loop through this l_explode_comp to do the explosion.
9692        -- And we will add phantom item into this l_explode_comp so that
9693        -- we can explode through phantom.
9694        MSC_ATP_REQ.Extend_Atp_Comp_Typ(l_explode_comp);
9695 
9696        l_explode_comp.inventory_item_id(1) := p_inventory_item_id;
9697 
9698        -- I used this comp_usaget to store the required quantity at this level.
9699        l_explode_comp.comp_usage(1) := p_requested_quantity;
9700 
9701        l_explode_comp.requested_date(1) := p_requested_date;
9702 
9703        -- assume the wip supply type to be 1 (as long as it is not phantom)
9704        l_explode_comp.wip_supply_type(1) := 1;
9705        l_explode_comp.component_identifier(1) := MSC_ATP_PVT.G_ITEM_INFO_REC.dest_inv_item_id;
9706 
9707        l_explode_comp.bom_item_type(1) :=  p_comp_info_rec.bom_item_type;
9708        l_explode_comp.parent_so_quantity(1) := p_comp_info_rec.parent_so_quantity;
9709        l_explode_comp.assembly_identifier(1) :=  p_comp_info_rec.line_id;
9710        l_explode_comp.top_model_line_id(1) := p_comp_info_rec.top_model_line_id;
9711        l_explode_comp.ato_parent_model_line_id(1) := p_comp_info_rec.ato_parent_model_line_id;
9712        l_explode_comp.ato_model_line_id(1) := p_comp_info_rec.ato_model_line_id;
9713        l_explode_comp.parent_line_id(1) := p_comp_info_rec.parent_line_id;
9714        l_explode_comp.fixed_lt(1) := p_comp_info_rec.fixed_lt;
9715        l_explode_comp.variable_lt(1) := p_comp_info_rec.variable_lt;
9716        l_explode_comp.dest_inventory_item_id(1) :=  MSC_ATP_PVT.G_ITEM_INFO_REC.dest_inv_item_id;
9717 
9718        --(3004862) circular BOM issue - initialize with end item's destination id.
9719 
9720         /*---bug 2680027: Extend lead_time field. This field will contain lead time of  the parent if
9721          the current comp is a phontom
9722        --We need to remember this lead time so that it could be passed to the component of the phantom
9723 
9724                     If Bom IS
9725                        A
9726                        |  Lead _time = 10
9727                        B
9728                        |  Lead time = 5
9729                        C
9730                If B is phantom then prior to this fix we were effectively saying that lead time between A and C is 5 days
9731                It should be 15 days instead. So in lead time field, we are going to save 0 form A
9732                as A doesn't have any par ent.
9733                For B, this filed will contain 10 as lead time between B and its Parent A is 10 days.
9734 
9735        */
9736        l_explode_comp.lead_time(1) := 0;
9737     END IF; --- p_comp_info_rec.check_model_capacity_flag = 1 THEN
9738 
9739     l_org_code := MSC_ATP_PVT.G_ORG_INFO_REC.org_code;
9740        --- get mso_sco_lead_time_factor
9741     l_mso_lead_time_factor := MSC_ATP_PVT.G_MSO_LEAD_TIME_FACTOR;
9742     l_network_scheduling_method := MSC_ATP_PVT.G_ORG_INFO_REC.network_scheduling_method; --bug3601223
9743 
9744      IF PG_DEBUG in ('Y', 'C') THEN
9745        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'MSO LEAD TIME FACTOR =  ' || l_mso_lead_time_factor);
9746        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'after assign the value');
9747        --bug3601223
9748        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_network_scheduling_method = '|| l_network_scheduling_method);
9749     END IF;
9750 
9751     i :=  l_explode_comp.inventory_item_id.FIRST;
9752     l_bill_seq_id := p_bill_seq_id;  --(3004862) circular BOM issue
9753 
9754     -- Check if Records exist in MSC_BOM_TEMP table in case of PDS. If yes,
9755     -- open cto_comp cursor instead of cursor comp.
9756 
9757     -- 2869830
9758     l_rounding_flag := nvl(MSC_ATP_PVT.G_ITEM_INFO_REC.rounding_control_type, 2);
9759 
9760     -- 3027711 move this to the beginning
9761     -- initially set the x_avail_assembly_qty to the p_requested_quantity,
9762     -- and we adjust that later
9763     -- 2869830
9764     IF l_rounding_flag = 1 THEN
9765        x_avail_assembly_qty :=  CEIL(p_requested_quantity);
9766     ELSE
9767        x_avail_assembly_qty :=  trunc(p_requested_quantity, 6);    -- 5598066
9768     END IF;
9769 
9770     -- bug 2178544
9771     x_atp_date := GREATEST(p_requested_date, MSC_ATP_PVT.G_FUTURE_ORDER_DATE, MSC_ATP_PVT.G_FUTURE_START_DATE);
9772     l_ptf_date := MSC_ATP_PVT.G_PTF_DATE;
9773 
9774     IF p_bill_seq_id is null and p_comp_info_rec.check_model_capacity_flag = 2 THEN
9775        RETURN;
9776     END IF;
9777     -- end 3027711
9778 
9779     --s_cto_rearch
9780     IF p_comp_info_rec.bom_item_type = 1  and p_comp_info_rec.replenish_to_order_flag = 'Y' THEN
9781         IF PG_DEBUG in ('Y', 'C') THEN
9782           msc_sch_wb.atp_debug('This is a model line');
9783         END IF;
9784         l_model_atp_comp_flag := p_comp_info_rec.atp_comp_flag;
9785         l_model_flag := p_comp_info_rec.bom_item_type;
9786 
9787         IF p_comp_info_rec.atp_flag = 'Y' THEN
9788            IF PG_DEBUG in ('Y', 'C') THEN
9789               msc_sch_wb.atp_debug('Add this line to comp requirements table');
9790            END IF;
9791            --first extend the record
9792            MSC_ATP_REQ.Extend_Atp_Comp_Typ(l_comp_requirements);
9793 
9794            --now add the model iteself
9795            --l_comp_requirements.inventory_item_id(1) := p_inventory_item_id;
9796            /* time_phased_atp changes begin
9797               Support PF ATP for components*/
9798            -- check this with vivek if inventory_item_id, request_item_id and atf_date need to be populated for model
9799            l_comp_requirements.inventory_item_id(1) := p_family_id;
9800            l_comp_requirements.request_item_id(1) := p_inventory_item_id;
9801            l_comp_requirements.atf_date(1) := p_atf_date;
9802            -- time_phased_atp changes end
9803 
9804            l_comp_requirements.comp_usage(1) := 1;
9805            l_comp_requirements.requested_date(1) := p_requested_date;
9806            l_comp_requirements.lead_time(1) := 0;
9807            l_comp_requirements.wip_supply_type(1) := 1;
9808            l_comp_requirements.assembly_identifier(1) := p_comp_info_rec.line_id;
9809            l_comp_requirements.component_identifier(1) := null;
9810            l_comp_requirements.atp_flag(1) := p_comp_info_rec.atp_flag;
9811            l_comp_requirements.parent_item_id(1) := p_inventory_item_id;
9812         END IF;
9813 
9814     /* ELSIF  p_comp_info_rec.bom_item_type = 4 and p_comp_info_rec.replenish_to_order_flag = 'Y'
9815                                           and MSC_ATP_PVT.G_INV_CTP = 5 THEN
9816         l_model_flag := 1;
9817         l_model_atp_comp_flag := p_comp_info_rec.atp_comp_flag;
9818     */
9819     END IF;
9820     --e_cto_rearch
9821 
9822     --4570421
9823     l_routing_seq_id := p_routing_seq_id;
9824     l_op_seq_id := p_op_seq_id;
9825 
9826     WHILE i IS NOT NULL LOOP
9827 
9828        IF p_comp_info_rec.check_model_capacity_flag = 1 THEN
9829            -- we are checking only base model's capacity. Therefore, we already
9830            --have all the components we need. Threfore we exit out
9831            EXIT;
9832        END IF;
9833 
9834        -- l_inv_item_id  :=  MSC_ATP_PVT.G_ITEM_INFO_REC.dest_inv_item_id; -- Not required after 3004862 fix
9835        IF PG_DEBUG in ('Y', 'C') THEN
9836           msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_explode_comp.component_identifier(' || i || ') : ' || l_explode_comp.component_identifier(i));
9837        END IF;
9838 
9839        -- Bug 4143668, reset routing type ti ensure correct data in between various iterations
9840        l_routing_type := NULL;
9841 
9842        -- 3027711
9843        IF l_routing_seq_id is not null THEN --4570421 replaced p_routing_seq_id by l_routing_seq_id
9844           BEGIN
9845 
9846              ---bug 2320808, set l_first_op_RCY to 1 if null
9847              -- Bug 4143668, Commented as separate SQLs will be used for routing_type, first_op_seq_num in msc_routings
9848              -- and l_first_op_RCY from msc_routing_operations
9849              /*
9850              SELECT r.cfm_routing_flag, NVL(op.reverse_cumulative_yield,1)
9851              INTO l_routing_type, l_first_op_RCY
9852              FROM msc_routings r, msc_routing_operations op
9853              WHERE r.plan_id = p_plan_id and
9854                    r.organization_id = p_organization_id and
9855                    r.sr_instance_id = p_instance_id and
9856                    -- r.assembly_item_id = l_inv_item_id and
9857                    -- (3004862) changed to l_explode_comp.component_identifier so that for i>1 phantom item's id is used.
9858                    r.assembly_item_id = l_explode_comp.component_identifier(i) and
9859                    r.routing_sequence_id = p_routing_seq_id and
9860                    --(ssurendr) Bug 2865389 Removed condition for Alternate Routing designator as
9861                    --we are accessing Routing by Routing sequance id.
9862                    --r.alternate_routing_designator IS NULL and
9863                    r.plan_id = op.plan_id and
9864                    r.sr_instance_id = op.sr_instance_id and
9865                                     --r.organization_id = op.organization_id and
9866                    r.routing_sequence_id = op.routing_sequence_id and
9867                    NVL(r.first_op_seq_num,op.operation_seq_num) = op.operation_seq_num and     -- bug4114765
9868                    rownum = 1; -- Bug 4143668, just pick one for case where r.first_op_seq_num is NULL
9869              */
9870 
9871              -- Bug 4143668, check routing type and first_op_seq_num at routing level
9872              -- Also removed check for assembly item and org as routing sequence, plan and instance is unique.
9873              SELECT r.cfm_routing_flag, r.first_op_seq_num
9874              INTO   l_routing_type, l_first_op_seq_num
9875              FROM   msc_routings r
9876              WHERE  r.plan_id = p_plan_id
9877              AND    r.sr_instance_id = p_instance_id
9878              AND    r.routing_sequence_id = l_routing_seq_id; --4570421 replaced p_routing_seq_id by l_routing_seq_id
9879 
9880              IF PG_DEBUG in ('Y', 'C') THEN
9881                 msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_routing_type := ' || l_routing_type);
9882                 msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'routing l_first_op_seq_num := ' || l_first_op_seq_num);
9883              END IF;
9884 
9885              -- Bug 4143668, check lowest op seq from routing operation as routing level data is only populated for network routings
9886              /* Not needed as of now as RCY is not populated for non-network routing.
9887              IF l_first_op_seq_num IS NULL THEN
9888                 SELECT MIN(op.operation_seq_num)
9889                 INTO   l_first_op_seq_num
9890                 FROM   msc_routing_operations op
9891                 WHERE  op.routing_sequence_id = p_routing_seq_id
9892                 AND    op.sr_instance_id = p_instance_id
9893                 AND    op.plan_id = p_plan_id;
9894 
9895                 IF PG_DEBUG in ('Y', 'C') THEN
9896                    msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'operations l_first_op_seq_num := ' || l_first_op_seq_num);
9897                 END IF;
9898              END IF; 	-- IF l_first_op_seq_num IS NULL THEN
9899              */
9900 
9901              -- Bug 4143668, get first operation's RCY based on op seq num found earlier
9902              IF l_first_op_seq_num IS NOT NULL THEN
9903                 SELECT DECODE(op.reverse_cumulative_yield,0,1,NVL(op.reverse_cumulative_yield,1)) --4694958
9904                 INTO   l_first_op_RCY
9905                 FROM   msc_routing_operations op
9906                 WHERE  op.plan_id = p_plan_id
9907                 AND    op.sr_instance_id = p_instance_id
9908                 AND    op.routing_sequence_id = l_routing_seq_id --4570421 replaced p_routing_seq_id by l_routing_seq_id
9909                 AND    op.operation_seq_num = l_first_op_seq_num
9910                 and    op.operation_sequence_id = l_op_seq_id; --4570421
9911 
9912                 IF PG_DEBUG in ('Y', 'C') THEN
9913                    msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_first_op_RCY := ' || l_first_op_RCY);
9914                 END IF;
9915              ELSE
9916                 l_first_op_RCY := 1; -- Bug 4143668, l_first_op_RCY to 1 if Null
9917              END IF; 	-- IF l_first_op_seq_num IS NOT NULL THEN
9918           EXCEPTION
9919               WHEN OTHERS THEN
9920                   -- Bug 4143668, add debug message for exception
9921                   IF PG_DEBUG in ('Y', 'C') THEN
9922                      msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'exception in cfm: '|| sqlcode);
9923                      msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'set l_routing_type and l_first_op_RCY');
9924                   END IF;
9925                   l_routing_type := NVL(l_routing_type, 2); -- Bug 4143668, use l_routing_type if already set, else 2
9926                   l_first_op_RCY := 1; -- Bug 4143668, l_first_op_RCY to 1 for exception
9927           END;
9928        ELSE
9929           l_routing_type := NVL(l_routing_type, 2); -- Bug 4143668, use l_routing_type if already set, else 2
9930           l_first_op_RCY := 1; -- Bug 4143668, l_first_op_RCY to 1 if Null
9931        END IF;
9932 
9933        IF PG_DEBUG in ('Y', 'C') THEN
9934           msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_routing_type := ' || l_routing_type);
9935           msc_sch_wb.atp_debug('l_model_flag := ' || l_model_flag);
9936        END IF;
9937        IF NVL(l_model_flag, 2) <> 1 THEN
9938            IF i <> 1 THEN
9939                -- (3004862) circular BOM issue: Call get process effectivity again
9940                -- for the Phantom Component's Bill.
9941                msc_sch_wb.atp_debug('Calling Process effectivity for Phantom:'|| l_explode_comp.component_identifier(i));
9942                MSC_ATP_PROC.get_process_effectivity(
9943                    p_plan_id,
9944                    l_explode_comp.component_identifier(i),
9945                    p_organization_id,
9946                    p_instance_id,
9947                    l_explode_comp.requested_date(i),
9948                    l_explode_comp.comp_usage(i),
9949                    l_process_seq_id,
9950                    l_routing_seq_id,
9951                    l_bill_seq_id,
9952                    l_op_seq_id, --4570421
9953                    x_return_status);
9954                IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
9955                    RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
9956                END IF;
9957            END IF;
9958 
9959            -- 3027711
9960            IF l_bill_seq_id is not null THEN
9961 
9962               ---- Get the bom from msc_bom_components and network routing
9963               -- ATP4drp open DRP specific cursor for DRP plans.
9964               IF NVL(MSC_ATP_PVT.G_PLAN_INFO_REC.plan_type, 1) <> 5 THEN
9965 
9966                   OPEN net_rout_comp(l_bill_seq_id, -- (3004862) circular BOM issue: call with changed parameter
9967                                      l_explode_comp.requested_date(i),
9968                                      l_explode_comp.comp_usage(i),
9969                                      l_explode_comp.wip_supply_type(i)); --4106269
9970                   IF PG_DEBUG in ('Y', 'C') THEN
9971                      msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'after opening network routing cursor net_rout_com');
9972                   END IF;
9973               ELSE
9974 
9975                   OPEN drp_comp(l_bill_seq_id, -- (3004862) circular BOM issue: call with changed parameter
9976                                 l_explode_comp.requested_date(i),
9977                                 l_explode_comp.comp_usage(i));
9978                   IF PG_DEBUG in ('Y', 'C') THEN
9979                      msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'after opening DRP cursor drp_comp');
9980                      msc_sch_wb.atp_debug('----- ATP4drp Specific Debug Messages -----');
9981                   END IF;
9982               END IF;
9983               -- End ATP4drp
9984 
9985               IF PG_DEBUG in ('Y', 'C') THEN
9986                   msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'after opening network routing cursor net_rout_com');
9987                   msc_sch_wb.atp_debug('l_bill_seq_id '||l_bill_seq_id);
9988                   msc_sch_wb.atp_debug('requested_date '||l_explode_comp.requested_date(i));
9989                   msc_sch_wb.atp_debug('comp_usage '|| l_explode_comp.comp_usage(i));
9990               END IF;
9991            END IF;
9992        ELSIF l_model_flag = 1 THEN
9993           IF PG_DEBUG in ('Y', 'C') THEN
9994              msc_sch_wb.atp_debug('Explode Model or its components  Bom');
9995              msc_sch_wb.atp_debug('l_model_atp_comp_flag := ' || l_model_atp_comp_flag);
9996           END IF;
9997           IF l_model_atp_comp_flag in ('Y', 'C') THEN
9998              ---explode mandatory components if model or option class
9999              IF l_explode_comp.bom_item_type(i) in (1,2) or
10000                        (l_explode_comp.bom_item_type(i) = 4 and MSC_ATP_PVT.G_INV_CTP = 5)  THEN
10001 
10002                  IF PG_DEBUG in ('Y', 'C') THEN
10003                      msc_sch_wb.atp_debug('i = ' || i);
10004                  END IF;
10005                  l_lead_time := CEIL((NVL(l_explode_comp.fixed_lt(i), 0) +
10006                                              NVL(l_explode_comp.variable_lt(i), 0)*l_explode_comp.comp_usage(i))
10007                                                  * (1 + l_mso_lead_time_factor));
10008 
10009                  IF PG_DEBUG in ('Y', 'C') THEN
10010                      msc_sch_wb.atp_debug('l_lead_time = ' || l_lead_time);
10011                  END IF;
10012 
10013                  IF nvl(l_lead_time, 0) > 0 THEN
10014                    l_request_date := MSC_CALENDAR.DATE_OFFSET
10015                                              (p_organization_id,
10016                                               p_instance_id,
10017                                               1,
10018                                               l_explode_comp.requested_date(i),
10019                                               -1 * l_lead_time);
10020                  ELSE
10021                     l_request_date := l_explode_comp.requested_date(i);
10022                  END IF;
10023 
10024                  IF PG_DEBUG in ('Y', 'C') THEN
10025                        msc_sch_wb.atp_debug('l_request_date := ' || l_request_date);
10026                  END IF;
10027 
10028                  MSC_ATP_CTO.Get_Mandatory_Components(p_plan_id,
10029                                                       p_instance_id,
10030                                                       p_organization_id,
10031                                                       l_explode_comp.inventory_item_id(i),
10032                                                       l_explode_comp.comp_usage(i),
10033                                                       l_request_date,
10034                                                       l_explode_comp.dest_inventory_item_id(i),
10035                                                       l_mand_comp_info_rec);
10036 
10037                  IF PG_DEBUG in ('Y', 'C') THEN
10038                        msc_sch_wb.atp_debug('After Get mand comp, add it to list of components');
10039                  END IF;
10040 
10041                  FOR l_cto_count in 1..l_mand_comp_info_rec.sr_inventory_item_id.count LOOP
10042                      l_atp_comp_rec := l_null_atp_comp_rec;
10043 
10044                      /* time_phased_atp changes begin
10045                         Support PF ATP for components*/
10046                      --l_atp_comp_rec.inventory_item_id := l_mand_comp_info_rec.sr_inventory_item_id(l_cto_count);
10047                      l_atp_comp_rec.inventory_item_id := MSC_ATP_PF.Get_PF_Atp_Item_Id(
10048                                                                 p_instance_id,
10049                                                                 -1, -- plan_id
10050                                                                 l_mand_comp_info_rec.sr_inventory_item_id(l_cto_count),
10051                                                                 p_organization_id
10052                                                          );
10053                      l_atp_comp_rec.request_item_id := l_mand_comp_info_rec.sr_inventory_item_id(l_cto_count);
10054                      l_atp_comp_rec.atf_date := l_mand_comp_info_rec.atf_date(l_cto_count);
10055                      l_atp_comp_rec.match_item_family_id := null;
10056                      -- time_phased_atp changes end
10057                      --4570421
10058                      l_atp_comp_rec.scaling_type                      := l_mand_comp_info_rec.scaling_type(l_cto_count);
10059                      l_atp_comp_rec.scale_multiple                    := l_mand_comp_info_rec.scale_multiple(l_cto_count);
10060                      l_atp_comp_rec.scale_rounding_variance           := l_mand_comp_info_rec.scale_rounding_variance(l_cto_count);
10061                      l_atp_comp_rec.rounding_direction                := l_mand_comp_info_rec.rounding_direction(l_cto_count);
10062                      l_atp_comp_rec.component_yield_factor            := l_mand_comp_info_rec.component_yield_factor(l_cto_count);
10063                      l_atp_comp_rec.usage_qty                         := l_mand_comp_info_rec.usage_qty(l_cto_count); --4775920
10064                      l_atp_comp_rec.organization_type                 := l_mand_comp_info_rec.organization_type(l_cto_count); --4775920
10065                      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
10066                         l_atp_comp_rec.comp_usage := integer_scaling (l_mand_comp_info_rec.quantity(l_cto_count),
10067                                                                       l_mand_comp_info_rec.scale_multiple(l_cto_count),
10068 	                                                              l_mand_comp_info_rec.scale_rounding_variance(l_cto_count) ,
10069 	                                                              l_mand_comp_info_rec.rounding_direction(l_cto_count));
10070 	             ELSE
10071                          l_atp_comp_rec.comp_usage := l_mand_comp_info_rec.quantity(l_cto_count);
10072                      END IF; --4570421
10073 
10074                      --l_atp_comp_rec.comp_usage := l_mand_comp_info_rec.quantity(l_cto_count);
10075                      l_atp_comp_rec.requested_date := l_request_date;
10076                      l_atp_comp_rec.lead_time := l_lead_time + l_explode_comp.lead_time(i);
10077                      l_atp_comp_rec.wip_supply_type := 1;
10078                      l_atp_comp_rec.assembly_identifier := l_explode_comp.assembly_identifier(i);
10079                      l_atp_comp_rec.component_identifier  := null;
10080                      --diag_atp
10081                      l_atp_comp_rec.reverse_cumulative_yield := 1;
10082                      --s_cto_rearch
10083                      l_atp_comp_rec.match_item_id := null;
10084                      l_atp_comp_rec.bom_item_type := l_mand_comp_info_rec.bom_item_type(l_cto_count);
10085                      l_atp_comp_rec.parent_line_id := l_explode_comp.assembly_identifier(i);
10086                      l_atp_comp_rec.top_model_line_id := l_explode_comp.top_model_line_id(i);
10087                      IF l_explode_comp.bom_item_type(i) = 1 THEN
10088                         l_atp_comp_rec.ato_parent_model_line_id := l_explode_comp.assembly_identifier(i);
10089                      ELSE
10090                          l_atp_comp_rec.ato_parent_model_line_id := l_explode_comp.ato_parent_model_line_id(i);
10091                      END IF;
10092 
10093                      l_atp_comp_rec.ato_model_line_id  := l_explode_comp.ato_model_line_id(i);
10094                      l_atp_comp_rec.MAND_COMP_FLAG  := 1;
10095                      l_atp_comp_rec.parent_so_quantity  := 0;
10096                      l_atp_comp_rec.fixed_lt  := l_mand_comp_info_rec.fixed_lead_time(l_cto_count);
10097                      l_atp_comp_rec.variable_lt  := l_mand_comp_info_rec.variable_lead_time(l_cto_count);
10098                      l_atp_comp_rec.oss_error_code  := null;
10099                      l_atp_comp_rec.model_flag := 1;
10100                      l_atp_comp_rec.requested_quantity := p_requested_quantity;
10101                      l_atp_comp_rec.atp_flag := l_mand_comp_info_rec.atp_flag(l_cto_count);
10102                      l_atp_comp_rec.atp_components_flag := l_mand_comp_info_rec.atp_components_flag(l_cto_count);
10103                      l_atp_comp_rec.dest_inventory_item_id := l_mand_comp_info_rec.dest_inventory_item_id(l_cto_count);
10104                      l_atp_comp_rec.parent_repl_ord_flag := NVL(p_comp_info_rec.replenish_to_order_flag, 'N');
10105                      l_atp_comp_rec.comp_uom := l_mand_comp_info_rec.uom_code(l_cto_count); --bug3110023
10106                      MSC_ATP_REQ.Add_To_Comp_List(l_explode_comp,
10107                                             l_comp_requirements,
10108                                             l_atp_comp_rec);
10109 
10110 
10111                  END LOOP;
10112              END IF;
10113 
10114              --now call for CTO Bom
10115               IF PG_DEBUG in ('Y', 'C') THEN
10116                  msc_sch_wb.atp_debug('Get CTO Bom');
10117               END IF;
10118 
10119 		     MSC_ATP_CTO.Get_CTO_BOM(MSC_ATP_PVT.G_SESSION_ID,
10120 					     l_cto_bom_rec,
10121 					     l_explode_comp.assembly_identifier(i),
10122 					     l_explode_comp.requested_date(i),
10123 					     l_explode_comp.comp_usage(i),
10124 					     l_explode_comp.parent_so_quantity(i),
10125 					     l_explode_comp.inventory_item_id(i),
10126 					     p_organization_id,
10127 					     p_plan_id,
10128 					     p_instance_id,
10129                                              l_explode_comp.fixed_lt(i),
10130                                              l_explode_comp.variable_lt(i));
10131 		  END IF;
10132 
10133 	       END IF;
10134 
10135 	       IF PG_DEBUG in ('Y', 'C') THEN
10136 		  msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'i = '||i);
10137 	       END IF;
10138 
10139 
10140 	       -- 3027711
10141 	       IF NVL(l_model_flag,2) <> 1 THEN
10142 		  --standard item
10143 		  LOOP
10144 
10145 		    IF l_bill_seq_id is null THEN
10146 		       EXIT;
10147 		    END IF;
10148 
10149                     -- ATP4drp fetch from DRP specific cursor for DRP plans.
10150                     IF NVL(MSC_ATP_PVT.G_PLAN_INFO_REC.plan_type, 1) <> 5 THEN
10151                        -- Non DRP Plan
10152 		       IF PG_DEBUG in ('Y', 'C') THEN
10153 			     msc_sch_wb.atp_debug('Before Fetch of net_rout_comp');
10154 		       END IF;
10155 		       FETCH net_rout_comp INTO
10156 		             l_comp_item_id,
10157 			     l_comp_component_identifier, -- (3004862) circular BOM issue
10158 			     l_comp_usage,
10159 			     l_comp_date,
10160 			     l_comp_lead_time,
10161 			     l_comp_wip_supply_type,
10162 			     l_comp_pre_pro_lead_time,
10163 			     --diag_atp
10164 			     l_reverse_cumulative_yield,
10165 			     -- time_phased_atp
10166 		    	     l_atf_date,
10167 		    	     l_comp_uom, --bug3110023
10168 		    	     --4570421
10169 		    	     l_scaling_type,
10170 		    	     l_scale_multiple,
10171                              l_scale_rounding_variance,
10172                              l_rounding_direction,
10173                              l_component_yield_factor,
10174                              l_usage_qty; --4775920
10175 
10176 		       EXIT WHEN net_rout_comp%NOTFOUND;
10177 		       IF PG_DEBUG in ('Y', 'C') THEN
10178 		    	  msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'after fetching cursor net_rout_comp');
10179 		       END IF;
10180                     ELSE -- DRP plan
10181 		       IF PG_DEBUG in ('Y', 'C') THEN
10182 			     msc_sch_wb.atp_debug('Before Fetch of drp_comp');
10183 		       END IF;
10184 		       FETCH drp_comp INTO
10185 		  	     l_comp_item_id,
10186 			     l_comp_component_identifier, -- (3004862) circular BOM issue
10187 			     l_comp_usage,
10188 			     l_comp_date,
10189 			     l_comp_lead_time,
10190 			     l_comp_wip_supply_type,
10191 			     l_comp_pre_pro_lead_time,
10192 			     --diag_atp
10193 			     l_reverse_cumulative_yield,
10194 			     -- time_phased_atp
10195 		    	     l_atf_date,
10196 		    	     l_comp_uom, --bug3110023
10197 		    	     --4570421
10198 		    	     l_scaling_type,
10199 		    	     l_scale_multiple,
10200                              l_scale_rounding_variance,
10201                              l_rounding_direction,
10202                              l_component_yield_factor,
10203                              l_usage_qty; --4775920
10204 
10205 		       EXIT WHEN drp_comp%NOTFOUND;
10206 		       IF PG_DEBUG in ('Y', 'C') THEN
10207 		    	  msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'after fetching cursor drp_comp');
10208                           msc_sch_wb.atp_debug('----- ATP4drp Specific Debug Messages -----');
10209 		       END IF;
10210                     END IF;
10211                     -- END ATP4drp
10212 
10213 
10214 		    IF PG_DEBUG in ('Y', 'C') THEN
10215 			msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'after fetch');
10216 			msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_comp_item_id = '||l_comp_item_id);
10217 			msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_comp_usage = '||l_comp_usage);
10218 			msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_comp_date = '||l_comp_date);
10219 			msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_comp_lead_time = '||l_comp_lead_time);
10220 			msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_comp_wip_supply_type = '||l_comp_wip_supply_type);
10221 			msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_comp_assembly_identifier = '||l_comp_assembly_identifier);
10222 			msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_comp_component_identifier = '||l_comp_component_identifier);
10223 			msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_comp_pre_pro_lead_time = '||l_comp_pre_pro_lead_time);
10224 			msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_atf_date = '||l_atf_date);
10225 			msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'G_ASSEMBLY_LINE_ID = '||MSC_ATP_PVT.G_ASSEMBLY_LINE_ID);
10226 			msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'G_COMP_LINE_ID = '||MSC_ATP_PVT.G_COMP_LINE_ID);
10227 			msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_comp_uom = '||l_comp_uom); --bug3110023
10228 			--4570421
10229 			msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_scaling_type = '||l_scaling_type);
10230 			msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_scale_multiple = '||l_scale_multiple);
10231 			msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_scale_rounding_variance = '||l_scale_rounding_variance);
10232 			msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_rounding_direction = '||l_rounding_direction);
10233 			msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_component_yield_factor = '||l_component_yield_factor);
10234 			msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_usage_qty = '||l_usage_qty); --4775920
10235 
10236 
10237 
10238 		    END IF;
10239 
10240 		    IF NVL(MSC_ATP_PVT.G_EXPLODE_PHANTOM, 'N') = 'Y' THEN
10241 			 --- agilent fix: If profile option is set to yes then we consider phantom
10242 			 --- item as any other item and do not add it to the list of items to be exploded
10243 			 l_comp_wip_supply_type := 1;
10244 		    END IF;
10245 
10246 
10247 		    l_comp_lead_time := l_comp_lead_time + l_explode_comp.lead_time(i);
10248 
10249 		    l_atp_comp_rec := l_null_atp_comp_rec;
10250 
10251 		    /* time_phased_atp changes begin
10252 		       Support PF ATP for components*/
10253                     -- ATP4drp PF ATP not supported for DRP.
10254                     IF NVL(MSC_ATP_PVT.G_PLAN_INFO_REC.plan_type, 1) = 5 THEN
10255                        -- Turn Off Product Family ATP for components.
10256                        l_atp_comp_rec.inventory_item_id := l_comp_item_id ;
10257                        -- ATP4drp PF ATP not supported for DRP. Print out Debug Here.
10258                        IF NVL(MSC_ATP_PVT.G_PLAN_INFO_REC.plan_type, 1) = 5 AND PG_DEBUG in ('Y', 'C') THEN
10259                           msc_sch_wb.atp_debug('----- ATP4drp Specific Debug Messages -----');
10260                           msc_sch_wb.atp_debug('Get_Comp_Requirements: l_atp_comp_rec.inventory_item_id '
10261                                                                             || l_atp_comp_rec.inventory_item_id );
10262                           msc_sch_wb.atp_debug('Get_Comp_Requirements: l_atp_comp_rec.request_item_id '
10263                                                                             || l_atp_comp_rec.request_item_id );
10264                           msc_sch_wb.atp_debug('----- ATP4drp Specific Debug Messages -----');
10265                        END IF;
10266                     ELSE -- Non DRP Plans
10267 		       l_atp_comp_rec.inventory_item_id := MSC_ATP_PF.Get_PF_Atp_Item_Id(
10268 							     p_instance_id,
10269 							     -1, -- plan_id
10270 							     l_comp_item_id,
10271 							     p_organization_id
10272 							   );
10273                     END IF;
10274                     -- End ATP4drp
10275 
10276 		    l_atp_comp_rec.request_item_id := l_comp_item_id;
10277 		    l_atp_comp_rec.atf_date := l_atf_date;
10278 		    l_atp_comp_rec.match_item_family_id := null;
10279 		    -- time_phased_atp changes end
10280 
10281 		    l_atp_comp_rec.comp_usage := l_comp_usage;
10282 		    l_atp_comp_rec.requested_date := l_comp_date;
10283 		    l_atp_comp_rec.lead_time := l_comp_lead_time;
10284 		    l_atp_comp_rec.wip_supply_type := l_comp_wip_supply_type;
10285 		    l_atp_comp_rec.assembly_identifier := l_comp_assembly_identifier;
10286 		    l_atp_comp_rec.component_identifier  := l_comp_component_identifier;
10287 		    --4570421, add scaling_type and other parameters here !
10288 		    l_atp_comp_rec.scaling_type := l_scaling_type;
10289 		    l_atp_comp_rec.scale_multiple := l_scale_multiple;
10290 		    l_atp_comp_rec.scale_rounding_variance := l_scale_rounding_variance;
10291 		    l_atp_comp_rec.rounding_direction := l_rounding_direction;
10292 		    l_atp_comp_rec.component_yield_factor := l_component_yield_factor; --4570421
10293 		    l_atp_comp_rec.usage_qty := l_usage_qty; --4775920
10294 		    l_atp_comp_rec.organization_type := NVL ( MSC_ATP_PVT.G_ORG_INFO_REC.org_type, MSC_ATP_PVT.DISCRETE_ORG); --4775920
10295 		    --diag_atp
10296 		    l_atp_comp_rec.reverse_cumulative_yield := l_reverse_cumulative_yield;
10297 		    --s_cto_rearch
10298 		    l_atp_comp_rec.match_item_id := null;
10299 		    l_atp_comp_rec.bom_item_type := null;
10300 		    l_atp_comp_rec.parent_line_id := null;
10301 		    l_atp_comp_rec.top_model_line_id := null;
10302 		    l_atp_comp_rec.ato_parent_model_line_id := null;
10303 		    l_atp_comp_rec.ato_model_line_id  := null;
10304 		    l_atp_comp_rec.MAND_COMP_FLAG  := null;
10305 		    l_atp_comp_rec.parent_so_quantity  := null;
10306 		    l_atp_comp_rec.fixed_lt  := null;
10307 		    l_atp_comp_rec.variable_lt  := null;
10308 		    l_atp_comp_rec.oss_error_code  := null;
10309 		    l_atp_comp_rec.model_flag := l_model_flag;
10310 		    l_atp_comp_rec.requested_quantity := p_requested_quantity;
10311                     l_atp_comp_rec.parent_repl_ord_flag := NVL(p_comp_info_rec.replenish_to_order_flag, 'N');
10312                     l_atp_comp_rec.comp_uom := l_comp_uom; --bug3110023
10313 		    MSC_ATP_REQ.Add_To_Comp_List(l_explode_comp,
10314 						 l_comp_requirements,
10315 						 l_atp_comp_rec);
10316 
10317 
10318 		  END LOOP; -- end the loop of fetch
10319 	       ELSE
10320 		  IF PG_DEBUG in ('Y', 'C') THEN
10321               msc_sch_wb.atp_debug('Model Entity ');
10322              msc_sch_wb.atp_debug('l_model_atp_comp_flag := ' || l_model_atp_comp_flag);
10323           END IF;
10324 
10325           IF l_model_atp_comp_flag in ('Y', 'C') THEN
10326              FOR l_cto_count in 1..l_cto_bom_rec.inventory_item_id.count LOOP
10327                  l_atp_comp_rec := l_null_atp_comp_rec;
10328 
10329                  /* time_phased_atp changes begin
10330                     Support PF ATP for components*/
10331                  l_atp_comp_rec.inventory_item_id := MSC_ATP_PF.Get_PF_Atp_Item_Id(
10332                                                          p_instance_id,
10333                                                          -1,
10334                                                          l_cto_bom_rec.inventory_item_id(l_cto_count),
10335                                                          p_organization_id
10336                                                      );
10337                  l_atp_comp_rec.request_item_id := l_cto_bom_rec.inventory_item_id(l_cto_count);
10338                  l_atp_comp_rec.atf_date := l_cto_bom_rec.atf_date(l_cto_count);
10339                  l_atp_comp_rec.match_item_family_id := MSC_ATP_PF.Get_PF_Atp_Item_Id(
10340                                                             p_instance_id,
10341                                                             -1,
10342                                                             l_cto_bom_rec.match_item_id(l_cto_count),
10343                                                             p_organization_id
10344                                                         );
10345                  -- time_phased_atp changes end
10346 
10347                  l_atp_comp_rec.comp_usage := l_cto_bom_rec.comp_usage(l_cto_count);
10348                  l_atp_comp_rec.requested_date := l_cto_bom_rec.requested_date(l_cto_count);
10349                  l_atp_comp_rec.lead_time := l_cto_bom_rec.lead_time(l_cto_count) + l_explode_comp.lead_time(i);
10350                  l_atp_comp_rec.wip_supply_type := l_cto_bom_rec.wip_supply_type(l_cto_count);
10351                  l_atp_comp_rec.assembly_identifier := l_cto_bom_rec.assembly_identifier(l_cto_count);
10352                  l_atp_comp_rec.component_identifier  := null;
10353                  --diag_atp
10354                  l_atp_comp_rec.reverse_cumulative_yield := null;
10355                  --s_cto_rearch
10356                  l_atp_comp_rec.match_item_id := l_cto_bom_rec.match_item_id(l_cto_count);
10357                  l_atp_comp_rec.bom_item_type := l_cto_bom_rec.bom_item_type(l_cto_count);
10358                  l_atp_comp_rec.parent_line_id := l_cto_bom_rec.parent_line_id(l_cto_count);
10359                  l_atp_comp_rec.top_model_line_id := l_cto_bom_rec.TOP_MODEL_LINE_ID(l_cto_count);
10360                  l_atp_comp_rec.ato_parent_model_line_id := l_cto_bom_rec.ATO_PARENT_MODEL_LINE_ID(l_cto_count);
10361                  l_atp_comp_rec.ato_model_line_id  := l_cto_bom_rec.ATO_MODEL_LINE_ID(l_cto_count);
10362                  l_atp_comp_rec.MAND_COMP_FLAG  := 2;
10363                  l_atp_comp_rec.parent_so_quantity  := l_cto_bom_rec.parent_so_quantity(l_cto_count);
10364                  l_atp_comp_rec.fixed_lt  := l_cto_bom_rec.fixed_lt(l_cto_count);
10365                  l_atp_comp_rec.variable_lt  := l_cto_bom_rec.variable_lt(l_cto_count);
10366                  l_atp_comp_rec.oss_error_code  := l_cto_bom_rec.oss_error_code(l_cto_count);
10367                  l_atp_comp_rec.model_flag := l_model_flag;
10368                  l_atp_comp_rec.requested_quantity := p_requested_quantity;
10369                  l_atp_comp_rec.atp_flag := l_cto_bom_rec.atp_flag(l_cto_count);
10370                  l_atp_comp_rec.atp_components_flag := l_cto_bom_rec.atp_components_flag(l_cto_count);
10371                  l_atp_comp_rec.dest_inventory_item_id := l_cto_bom_rec.dest_inventory_item_id(l_cto_count);
10372                  l_atp_comp_rec.parent_repl_ord_flag := NVL(p_comp_info_rec.replenish_to_order_flag, 'N');
10373                  l_atp_comp_rec.comp_uom := l_cto_bom_rec.comp_uom(l_cto_count); --bug3110023
10374                  l_atp_comp_rec.usage_qty := l_cto_bom_rec.usage_qty(l_cto_count); --4775920
10375                  l_atp_comp_rec.organization_type := l_cto_bom_rec.organization_type(l_cto_count); --4775920
10376 
10377                  MSC_ATP_REQ.Add_To_Comp_List(l_explode_comp,
10378                                             l_comp_requirements,
10379                                             l_atp_comp_rec);
10380 
10381              END LOOP;
10382           END IF;
10383        END IF;
10384 
10385 
10386        -- Bug 4042403, 4047183, 4070094 Check that l_bill_seq_id is not NULL
10387        IF ((l_routing_type = 3) OR (l_model_flag <> 1)) AND l_bill_seq_id IS NOT NULL THEN
10388            -- ATP4drp close DRP specific cursor for DRP plans.
10389            IF NVL(MSC_ATP_PVT.G_PLAN_INFO_REC.plan_type, 1) <> 5 THEN
10390               -- Bug 4042403, 4047183, 4070094 Close the cursor only if it has been opened.
10391               IF net_rout_comp%ISOPEN THEN
10392                  CLOSE net_rout_comp;
10393                  IF PG_DEBUG in ('Y', 'C') THEN
10394                     msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'after closing cursor net_rout_comp');
10395                  END IF;
10396               END IF;
10397               -- End Bug 4042403, 4047183, 4070094
10398            ELSE -- DRP plan
10399               -- Bug 4042403, 4047183, 4070094 Close the cursor only if it has been opened.
10400               IF drp_comp%ISOPEN THEN
10401                  CLOSE drp_comp;
10402                  IF PG_DEBUG in ('Y', 'C') THEN
10403                     msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'after closing cursor drp_comp');
10404                     msc_sch_wb.atp_debug('----- ATP4drp Specific Debug Messages -----');
10405                  END IF;
10406               END IF;
10407               -- End Bug 4042403, 4047183, 4070094
10408            END IF;
10409            -- End ATP4drp
10410 
10411        END IF;
10412 
10413        /*--s_cto_rearch:  do not open cusrosr for cto any more
10414        ELSIF l_cto_bom > 0 THEN
10415            CLOSE cto_comp;
10416 
10417            IF PG_DEBUG in ('Y', 'C') THEN
10418               msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'after closing cursor cto_comp');
10419            END IF;
10420        END IF;
10421        e_cto_rearch */
10422        i := l_explode_comp.inventory_item_id.NEXT(i);
10423 
10424        IF PG_DEBUG in ('Y', 'C') THEN
10425           msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'second time, i = '||i);
10426        END IF;
10427     END LOOP;
10428 
10429     -- 3027711 move initializing x_atp_date/x_avail_assembly_qty to beginning
10430 
10431     IF PG_DEBUG in ('Y', 'C') THEN
10432        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'x_atp_date := ' || x_atp_date);
10433        msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_comp_requirements.inventory_item_id.count = '||
10434                          l_comp_requirements.inventory_item_id.count);
10435     END IF;
10436     --msc_sch_wb.atp_debug('l_comp_requirements.pre_process_lead_time.count = '||
10437     --                    l_comp_requirements.pre_process_lead_time.count);
10438 
10439     j := l_comp_requirements.inventory_item_id.first;
10440     -- if j is null, that means we don't have any comp requirements
10441     -- need to consider, so we can assume that we have infinite comp
10442     -- to make the assembly. (that's why we initialize x_avail_assembly_qty
10443     -- that way).
10444 
10445     -- otherwise we need to know how many assemblies we can make
10446     -- by loop through each comp and find the availibility.
10447     -- If we can make more than the requested quantity, we return
10448     -- the requested quantity.
10449 
10450     WHILE j IS NOT NULL LOOP
10451 
10452         --- IN backward sched. check if request date - (fix + Var + preProcessing lead time)
10453         --- is greater than sysdate or not
10454         --- if not then we can't meet the requirement
10455         --- We check only when we are in first time.
10456         IF PG_DEBUG in ('Y', 'C') THEN
10457            msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'p_search = '|| p_search);
10458         END IF;
10459 
10460         IF PG_DEBUG in ('Y', 'C') THEN
10461            msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'j := '||j);
10462         END IF;
10463 
10464         ---diag_atp: we look for complete quantity in case of diagnostic ATP
10465         IF PG_DEBUG in ('Y', 'C') THEN
10466            msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_comp_requirements.comp_usage(j) := '||l_comp_requirements.comp_usage(j));
10467            msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'x_avail_assembly_qty := '||x_avail_assembly_qty);
10468         END IF;
10469         IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 THEN
10470             --- for diagnostic atp we always order the full quantity.
10471             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
10472                 (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
10473                  l_requested_comp_qty := l_comp_requirements.comp_usage(j);
10474             ELSE
10475                  l_requested_comp_qty := l_comp_requirements.comp_usage(j)* p_requested_quantity;
10476             END IF;
10477         ELSE --4570421
10478             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
10479                 (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
10480                  l_requested_comp_qty := l_comp_requirements.comp_usage(j);
10481             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
10482                  l_requested_comp_qty := integer_scaling (l_comp_requirements.comp_usage(j)*x_avail_assembly_qty,
10483                                                    l_comp_requirements.scale_multiple(j),
10484 	                                           l_comp_requirements.scale_rounding_variance(j) ,
10485 	                                           l_comp_requirements.rounding_direction(j));
10486 	    ELSE
10487 	         l_requested_comp_qty := l_comp_requirements.comp_usage(j)*x_avail_assembly_qty;
10488 	    END IF;
10489 
10490         END IF;
10491         --4570421 , print l_requested_comp_qty here !
10492         IF PG_DEBUG in ('Y', 'C') THEN
10493            msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_requested_comp_qty := '||l_requested_comp_qty);
10494         END IF;
10495 
10496         -- now call atp check for this item:
10497         -- 1: populate the atp rec
10498         -- 2: call ATP_Check
10499 
10500         l_atp_rec.error_code := 0;
10501         l_atp_rec.available_quantity := NULL;
10502         l_atp_rec.requested_date_quantity := NULL;
10503 
10504         -- Bug 1562754, we need to store the line id for a lower level model in
10505         -- case of CTO. For example, the BOM is like : Model A -> Model B -> Item C
10506         -- In such a case, if item C is not available enough say while making Model B
10507         -- in backward case, during adjustment of other resources and components, we want
10508         -- G_ASSEMBLY_LINE_ID to be set to line Id of Model B and not Model A.
10509 
10510         MSC_ATP_PVT.G_ASSEMBLY_LINE_ID := NVL(l_comp_requirements.assembly_identifier(j), MSC_ATP_PVT.G_ASSEMBLY_LINE_ID);
10511         MSC_ATP_PVT.G_COMP_LINE_ID := NVL(l_comp_requirements.component_identifier(j), MSC_ATP_PVT.G_COMP_LINE_ID);
10512         IF PG_DEBUG in ('Y', 'C') THEN
10513            msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'G_COMP_LINE_ID = '||MSC_ATP_PVT.G_COMP_LINE_ID);
10514         END IF;
10515 
10516         -- no need to do uom conversion
10517         l_atp_rec.instance_id := p_instance_id;
10518         l_atp_rec.identifier := MSC_ATP_PVT.G_ORDER_LINE_ID;
10519         l_atp_rec.component_identifier := l_comp_requirements.component_identifier(j);
10520 
10521         -- 2462661 : krajan
10522         --l_atp_rec.src_atp_flag := l_comp_requirements.src_atp_flag(j);
10523 
10524         select identifier3
10525         into   l_atp_rec.demand_source_line
10526         from   mrp_atp_details_temp
10527         where  pegging_id = p_parent_pegging_id
10528         and    record_type = 3
10529         and    session_id = MSC_ATP_PVT.G_SESSION_ID;
10530 
10531 
10532         -- l_atp_rec.demand_source_header_id:= l_atp_table.Demand_Source_Header_Id(i);
10533         -- l_atp_rec.demand_source_delivery:= l_atp_table.Demand_Source_Delivery(i);
10534         l_atp_rec.inventory_item_id := l_comp_requirements.inventory_item_id(j);
10535 
10536         /* time_phased_atp
10537            Support PF ATP for components*/
10538         l_atp_rec.request_item_id := l_comp_requirements.request_item_id(j);
10539 
10540         l_atp_rec.organization_id := p_organization_id;
10541         l_atp_rec.quantity_ordered := l_requested_comp_qty;
10542         -- l_atp_rec.quantity_uom := l_quantity_uom;
10543         l_atp_rec.requested_ship_date := l_comp_requirements.requested_date(j);
10544 
10545         -- krajan: 2408902: duplicate statement
10546         --l_atp_rec.demand_class := p_demand_class;
10547 
10548 
10549         l_atp_rec.insert_flag := p_insert_flag;
10550 
10551         IF l_model_flag = 1 THEN
10552            l_atp_rec.refresh_number := p_refresh_number;
10553         ELSE
10554            l_atp_rec.refresh_number := null;
10555         END IF;
10556 
10557 	l_atp_rec.ship_date := null;
10558 
10559         -- krajan: 2408902: populate demand class from global variable
10560         -- Bug 2424357
10561         l_atp_rec.demand_class := NVL(MSC_ATP_PVT.G_ATP_DEMAND_CLASS, p_demand_class);
10562 
10563         IF PG_DEBUG in ('Y', 'C') THEN
10564            msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'Demand Class being passed is : '|| l_atp_rec.demand_class);
10565         END IF;
10566         -- end 2408902
10567 
10568         --s_cto_enhc
10569         l_plan_found_for_match := 0;
10570         l_model_error_code := 0;
10571         l_atp_rec.base_model_id   := null;
10572 
10573 
10574         IF l_model_flag = 1 and l_comp_requirements.atp_flag(j) is null
10575                             --exclude mand_comp flag as they will always be collected
10576                             and NVL(l_comp_requirements.MAND_COMP_FLAG(j), 2) = 2  THEN
10577 
10578            --item doesn't exists in the given organization
10579            l_model_error_code := MSC_ATP_PVT.ATP_ITEM_NOT_COLLECTED;
10580 
10581            IF PG_DEBUG in ('Y', 'C') THEN
10582                  msc_sch_wb.atp_debug('Item not collected := ' || l_comp_requirements.inventory_item_id(j));
10583            END IF;
10584 
10585         ELSIF l_model_flag = 1 and l_comp_requirements.atp_flag(j) = 'N'
10586                                and l_comp_requirements.atp_components_flag(j) = 'N' THEN
10587            --model entity is non-atpable. Do not find plan, default to parent's plan
10588            l_plan_id := p_plan_id;
10589         ELSIF MSC_ATP_PVT.G_INV_CTP = 5 THEN
10590            l_plan_id := -1;
10591            l_assign_set_id := p_assign_set_id;
10592         ELSE
10593 
10594            IF l_comp_requirements.match_item_id(j) is not null then
10595               --check if match exists or not.
10596 
10597               /* time_phased_atp changes begin
10598                  Support PF ATP for components*/
10599               --l_atp_rec.inventory_item_id := l_comp_requirements.match_item_id(j);
10600               l_atp_rec.inventory_item_id := l_comp_requirements.match_item_family_id(j);
10601               l_atp_rec.request_item_id := l_comp_requirements.match_item_id(j);
10602               -- time_phased_atp changes end
10603               /*
10604               MSC_ATP_PROC.get_global_plan_info(p_instance_id,
10605                                              --l_atp_rec.inventory_item_id,
10606                                              l_atp_rec.request_item_id, -- time_phased_atp
10607                                              l_atp_rec.organization_id,
10608                                              l_atp_rec.demand_class);*/
10609               /* time_phased_atp changes begin
10610                  Call new procedure Get_PF_Plan_Info*/
10611               IF PG_DEBUG in ('Y', 'C') THEN
10612                  msc_sch_wb.atp_debug('Match found := ' || l_atp_rec.inventory_item_id);
10613                  msc_sch_wb.atp_debug('Find plan for match');
10614               END IF;
10615               MSC_ATP_PF.Get_PF_Plan_Info(
10616                           p_instance_id,
10617                           l_atp_rec.request_item_id,
10618                           l_atp_rec.inventory_item_id,
10619                           l_atp_rec.organization_id,
10620                           l_atp_rec.demand_class,
10621                           l_atp_rec.atf_date,
10622                           l_atp_rec.error_code,
10623                           l_return_status,
10624                           p_plan_id --bug3510475 pass component's plan id as parent
10625               );
10626 
10627               IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
10628                    IF PG_DEBUG in ('Y', 'C') THEN
10629                       msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'Error encountered in Get_PF_Plan_Info');
10630                    END IF;
10631               END IF;
10632               /* time_phased_atp changes end*/
10633 
10634               IF MSC_ATP_PVT.G_PLAN_INFO_REC.plan_id is not null and
10635                   NVL(MSC_ATP_PVT.G_PLAN_INFO_REC.plan_id, -1) <> -1 then
10636                   IF PG_DEBUG in ('Y', 'C') THEN
10637                       msc_sch_wb.atp_debug('Plan found for match := '  || MSC_ATP_PVT.G_PLAN_INFO_REC.plan_id );
10638                    END IF;
10639                   ---plan found for match
10640                   l_plan_found_for_match := 1;
10641                   --l_atp_rec.request_item_id := l_comp_requirements.match_item_id(j);
10642                   l_atp_rec.base_model_id   := l_comp_requirements.inventory_item_id(j); -- check with Vivek
10643 
10644               ELSE
10645                   ---plan is not found for match. Do ATP on model level
10646                   --l_atp_rec.inventory_item_id := l_comp_requirements.inventory_item_id(j);
10647                   IF PG_DEBUG in ('Y', 'C') THEN
10648                       msc_sch_wb.atp_debug('Plan not found for match := '  || MSC_ATP_PVT.G_PLAN_INFO_REC.plan_id );
10649                   END IF;
10650                   /* time_phased_atp
10651                      Support PF ATP for components*/
10652                   l_atp_rec.inventory_item_id := l_comp_requirements.inventory_item_id(j);
10653                   l_atp_rec.request_item_id := l_comp_requirements.request_item_id(j);
10654               END IF;
10655            END IF;
10656 
10657            IF l_plan_found_for_match = 0 THEN
10658               -- New procedure for obtaining plan data : Supplier Capacity Lead Time (SCLT) proj.
10659               /*
10660               MSC_ATP_PROC.get_global_plan_info(p_instance_id,
10661                                              --l_atp_rec.inventory_item_id,
10662                                              l_atp_rec.request_item_id, -- time_phased_atp
10663                                              l_atp_rec.organization_id,
10664                                              l_atp_rec.demand_class);*/
10665 
10666               /* time_phased_atp changes begin
10667                  Call new procedure Get_PF_Plan_Info*/
10668 
10669               MSC_ATP_PF.Get_PF_Plan_Info(
10670                           p_instance_id,
10671                           l_atp_rec.request_item_id,
10672                           l_atp_rec.inventory_item_id,
10673                           l_atp_rec.organization_id,
10674                           l_atp_rec.demand_class,
10675                           l_atp_rec.atf_date,
10676                           l_atp_rec.error_code,
10677                           l_return_status,
10678                           p_plan_id --bug3510475 pass component's plan id as parent
10679               );
10680 
10681               IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
10682                    IF PG_DEBUG in ('Y', 'C') THEN
10683                       msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'Error encountered in Get_PF_Plan_Info');
10684                    END IF;
10685               END IF;
10686               /* time_phased_atp changes end*/
10687            END IF;
10688 
10689            l_plan_info_rec := MSC_ATP_PVT.G_PLAN_INFO_REC;
10690            -- End New procedure for obtaining plan data : Supplier Capacity Lead Time proj
10691 
10692            l_plan_id       := l_plan_info_rec.plan_id;
10693            l_assign_set_id := l_plan_info_rec.assignment_set_id;
10694 
10695            --diag_atp
10696            l_atp_rec.plan_name := l_plan_info_rec.plan_name;
10697            l_atp_rec.reverse_cumulative_yield := l_comp_requirements.reverse_cumulative_yield(j);
10698            l_atp_rec.quantity_uom := l_comp_requirements.comp_uom(j); --bug3110023
10699 
10700            -- changes for bug 2392456 ends
10701 
10702            msc_sch_wb.atp_debug ('Plan ID in Get_Comp_Reqs : ' || l_plan_id);
10703            IF (l_plan_id is NULL) or (l_plan_id IN (-100, -200)) THEN
10704                --s_cto_rearch
10705                IF l_model_flag = 1 THEN
10706                   l_model_error_code := MSC_ATP_PVT.PLAN_NOT_FOUND;
10707                ELSE
10708                   --standard item
10709                   -- this should not happen but just in case
10710                   l_plan_id := p_plan_id;
10711                   l_assign_set_id := p_assign_set_id;
10712                END IF;
10713            END IF;
10714 
10715            -- 24x7
10716            IF (l_plan_id = -300) then
10717               l_atp_rec.error_code := MSC_ATP_PVT.TRY_ATP_LATER;
10718               IF PG_DEBUG in ('Y', 'C') THEN
10719                  msc_sch_wb.atp_debug('Get_Comp_Req: ATP Downtime Detected');
10720                  msc_sch_wb.atp_debug('Get_Comp_Requirements: ATP Downtime');
10721               END IF;
10722               MSC_ATP_PVT.G_DOWNTIME_HIT := 'Y';
10723               RAISE MSC_ATP_PVT.EXC_NO_PLAN_FOUND;
10724            End IF;
10725         END IF;
10726         --subst
10727         l_atp_rec.original_item_flag := 2;
10728         l_atp_rec.top_tier_org_flag := 2;
10729         l_atp_rec.substitution_window := 0;
10730 
10731         /* ship_rec_cal changes begin */
10732         l_atp_rec.receiving_cal_code := p_manufacturing_cal_code;
10733         l_atp_rec.intransit_cal_code := p_manufacturing_cal_code;
10734         l_atp_rec.shipping_cal_code := p_manufacturing_cal_code;
10735         l_atp_rec.manufacturing_cal_code := p_manufacturing_cal_code;
10736         /* ship_rec_cal changes end */
10737 
10738         ---s_cto_rearch
10739         IF MSC_ATP_PVT.G_INV_CTP = 5 and
10740            p_comp_info_rec.bom_item_type = 4 and p_comp_info_rec.replenish_to_order_flag = 'Y' THEN
10741            --add ato item's components in ODS case share the same line id as ato item itself
10742            l_atp_rec.demand_source_line := p_comp_info_rec.line_id;
10743         ELSE
10744 
10745            l_atp_rec.demand_source_line := l_comp_requirements.assembly_identifier(j);
10746         END IF;
10747         l_atp_rec.order_number       := p_order_number;
10748         l_atp_rec.Top_Model_line_id := l_comp_requirements.Top_Model_line_id(j);
10749 
10750         l_atp_rec.ATO_Parent_Model_Line_Id := l_comp_requirements.ATO_Parent_Model_Line_Id(j);
10751 
10752         IF p_comp_info_rec.bom_item_type = 4 and p_comp_info_rec.replenish_to_order_flag = 'Y' THEN
10753            -- ato model line id id used to remove stealing records in demand priorit cases
10754            -- there for components for ATO items, we pass this line id
10755            l_atp_rec.ATO_Model_Line_Id := p_comp_info_rec.ATO_Model_Line_Id;
10756         ELSE
10757            l_atp_rec.ATO_Model_Line_Id := l_comp_requirements.ATO_Model_Line_Id(j);
10758         END IF;
10759         l_atp_rec.Parent_line_id :=  l_comp_requirements.Parent_line_id(j);
10760         l_atp_rec.wip_supply_type := l_comp_requirements.wip_supply_type(j);
10761         l_atp_rec.parent_atp_flag := p_comp_info_rec.atp_flag;
10762         l_atp_rec.parent_atp_comp_flag := p_comp_info_rec.atp_comp_flag;
10763         l_atp_rec.parent_repl_order_flag := p_comp_info_rec.replenish_to_order_flag;
10764         l_atp_rec.parent_bom_item_type := p_comp_info_rec.bom_item_type;
10765         l_atp_rec.mand_comp_flag := l_comp_requirements.mand_comp_flag(j);
10766         l_atp_rec.parent_so_quantity := l_comp_requirements.parent_so_quantity(j);
10767         l_atp_rec.wip_supply_type := l_comp_requirements.wip_supply_type(j);
10768         --- This flag is populated only for model where atp_flag = 'Y'
10769         -- This flag will be used in get_item_attribute to turn off ATP comp flag
10770         --- so that model is not reexploded.
10771         l_atp_rec.parent_item_id := l_comp_requirements.parent_item_id(j);
10772 
10773         l_atp_rec.bill_seq_id := l_bill_seq_id;      --4741012 for passing to ATP_Check.
10774 
10775         --4570421
10776         l_atp_rec.scaling_type                      := l_comp_requirements.scaling_type(j);
10777         l_atp_rec.scale_multiple                    := l_comp_requirements.scale_multiple(j);
10778         l_atp_rec.scale_rounding_variance           := l_comp_requirements.scale_rounding_variance(j);
10779         l_atp_rec.rounding_direction                := l_comp_requirements.rounding_direction(j);
10780         l_atp_rec.component_yield_factor            := l_comp_requirements.component_yield_factor(j); --4570421
10781         l_atp_rec.usage_qty                         := l_comp_requirements.usage_qty(j); --4775920
10782         l_atp_rec.organization_type                 := l_comp_requirements.organization_type(j); --4775920
10783 
10784         ---e_cto_rearch
10785         IF l_model_flag = 1 and l_model_error_code > 0 THEN
10786 
10787            IF PG_DEBUG in ('Y', 'C') THEN
10788               msc_sch_wb.atp_debug('Model entity and error has occured');
10789               msc_sch_wb.atp_debug('Error code := ' || l_model_error_code);
10790            END IF;
10791 
10792            l_atp_rec.combined_requested_date_qty := 0;
10793            l_atp_rec.requested_date_quantity := 0;
10794            l_atp_rec.ship_date := null;
10795 
10796            --add pegging for diagnostic case
10797            IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 THEN
10798               l_pegging_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
10799               l_pegging_rec.order_line_id:= MSC_ATP_PVT.G_ORDER_LINE_ID;
10800               l_pegging_rec.parent_pegging_id:= p_parent_pegging_id;
10801               l_pegging_rec.atp_level:= p_level;
10802               l_pegging_rec.organization_id:= p_organization_id;
10803               l_pegging_rec.organization_code := l_org_code;
10804               l_pegging_rec.identifier1:= p_instance_id;
10805               l_pegging_rec.identifier2 := null;
10806               l_pegging_rec.identifier3 := null;
10807               l_pegging_rec.inventory_item_id:= l_comp_requirements.inventory_item_id(j);
10808               l_pegging_rec.inventory_item_name := null; -- item is not collected, how do we show it??
10809               l_pegging_rec.resource_id := NULL;
10810               l_pegging_rec.resource_code := NULL;
10811               l_pegging_rec.department_id := NULL;
10812               l_pegging_rec.department_code := NULL;
10813               l_pegging_rec.supplier_id := NULL;
10814               l_pegging_rec.supplier_name := NULL;
10815               l_pegging_rec.supplier_site_id := NULL;
10816               l_pegging_rec.supplier_site_name := NULL;
10817               l_pegging_rec.scenario_id:= p_scenario_id;
10818               l_pegging_rec.supply_demand_source_type:= 6;
10819               l_pegging_rec.supply_demand_quantity := l_requested_comp_qty;
10820               l_pegging_rec.supply_demand_type:= 1;
10821               l_pegging_rec.supply_demand_date:= l_comp_requirements.requested_date(j);
10822               --4570421
10823               l_pegging_rec.scaling_type                      := l_comp_requirements.scaling_type(j);
10824               l_pegging_rec.scale_multiple                    := l_comp_requirements.scale_multiple(j);
10825               l_pegging_rec.scale_rounding_variance           := l_comp_requirements.scale_rounding_variance(j);
10826               l_pegging_rec.rounding_direction                := l_comp_requirements.rounding_direction(j);
10827               l_pegging_rec.component_yield_factor            := l_comp_requirements.component_yield_factor(j); --4570421
10828               l_pegging_rec.usage                             := l_comp_requirements.usage_qty(j); --4775920
10829               l_pegging_rec.organization_type                 := l_comp_requirements.organization_type(j); --4775920
10830 
10831               --e_cto_rearch
10832 
10833               l_pegging_rec.constraint_flag := 'N';
10834 	      l_pegging_rec.component_identifier := null;
10835 
10836 
10837               --diag_atp
10838               l_pegging_rec.pegging_type := MSC_ATP_PVT.ORG_DEMAND; --demand pegging
10839 
10840               --s_cto_rearch
10841               l_pegging_rec.dest_inv_item_id := null;
10842               l_pegging_rec.error_code := l_model_error_code;
10843               --e_cto_rearch
10844 
10845               l_pegging_rec.summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;     -- for summary enhancement
10846               MSC_ATP_DB_UTILS.Add_Pegging(l_pegging_rec, l_pegging_id);
10847            END IF;
10848         ELSE
10849 
10850            -- ATP4drp Assign parent_item_id for DRP Kitting in DRP plans.
10851            -- Assignment should not be detrimental in other plans.
10852 
10853            IF ( l_plan_id <> -1) THEN --4929084
10854               l_atp_rec.parent_item_id := p_inventory_item_id;
10855            END IF;
10856 
10857            IF PG_DEBUG in ('Y', 'C') THEN
10858               msc_sch_wb.atp_debug('Get_Comp_Requirements: l_atp_rec.parent_item_id ' || l_atp_rec.parent_item_id );
10859            END IF;
10860            -- End ATP4drp
10861 
10862            MSC_ATP_PVT.ATP_Check(l_atp_rec,
10863                      l_plan_id,
10864                      p_level ,
10865                      p_scenario_id,
10866                      p_search,
10867                      p_refresh_number,
10868                      p_parent_pegging_id,
10869                      l_assign_set_id,
10870                      l_atp_period,
10871                      l_atp_supply_demand,
10872                      x_return_status);
10873 
10874         END IF;
10875         --- bug 2178544
10876         -- Since PTF-Date might be chnaged by some different plan for components we reset the global varibale
10877         MSC_ATP_PVT.G_PTF_DATE := l_ptf_date;
10878 
10879         IF x_return_status = MSC_ATP_PVT.CTO_OSS_ERROR THEN
10880 
10881            RAISE MSC_ATP_PVT.INVALID_OSS_SOURCE;
10882 
10883            IF PG_DEBUG in ('Y', 'C') THEN
10884               msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || ' Error in OSS');
10885            END IF;
10886 
10887         -- Bug 1531429, in case return status is not success, raise an exception.
10888         -- krajan: 2400614
10889         -- krajan: If it is 'G', then it is a sourcing mismatch error.
10890         -- krajan: If it is MSC_ATP_PVT.G_ATO_SRC_MISMATCH, then it is a sourcing mismatch error.
10891 
10892         -- krajan: Basically for handling recursive ATP_CHECK <-> Get_Comp_Req calls
10893         -- krajan : 2752705 and dsting 2764213 : Other errors that need to go through to the
10894         --          top level model are also handled the same way as the mismatch case.
10895         ELSIF x_return_status = MSC_ATP_PVT.G_ATO_SRC_MISMATCH THEN
10896            IF PG_DEBUG in ('Y', 'C') THEN
10897               msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'Get_Comp_Req: Error in ATP_CHECK 0.1');
10898               msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'Error in lower level component check');
10899            END IF;
10900            RAISE MSC_ATP_PVT.G_ATO_SOURCING_MISMATCH;
10901 
10902          --bug 3308206: IF ATP rule is not defined on the item then error out with message
10903         ELSIF MSC_ATP_PVT.G_INV_CTP = 5 and x_return_status <>  FND_API.G_RET_STS_SUCCESS
10904                and l_atp_rec.error_code = MSC_ATP_PVT.ATP_BAD_RULE THEN
10905            IF PG_DEBUG in ('Y', 'C') THEN
10906               msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'No ATP rule on Component');
10907            END IF;
10908            RAISE MSC_ATP_PVT.EXC_NO_ATP_RULE;
10909         END IF;
10910 
10911         -- dsting 2764213
10912         IF x_return_status = MSC_ATP_PVT.G_NO_PLAN_FOUND THEN
10913            IF PG_DEBUG in ('Y', 'C') THEN
10914               msc_sch_wb.atp_debug('Get_Comp_Req: Error in ATP_CHECK 0.2');
10915               msc_sch_wb.atp_debug('Get_Comp_Requirements: Error in lower level component check');
10916            END IF;
10917            RAISE MSC_ATP_PVT.EXC_NO_PLAN_FOUND;
10918         END IF;
10919 
10920         -- krajan 2752705
10921         IF x_return_status = MSC_ATP_PVT.G_ATO_UNCOLL_ITEM THEN
10922            IF PG_DEBUG in ('Y', 'C') THEN
10923               msc_sch_wb.atp_debug('Get_Comp_Req: Error in ATP_CHECK 0.3');
10924               msc_sch_wb.atp_debug('Get_Comp_Requirements: Error in lower level component check');
10925            END IF;
10926            RAISE MSC_ATP_PVT.G_EXC_UNCOLLECTED_ITEM;
10927         END IF;
10928 
10929         IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
10930            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
10931         END IF;
10932 
10933         MSC_ATP_PROC.Details_Output(l_atp_period,
10934                        l_atp_supply_demand,
10935                        x_atp_period,
10936                        x_atp_supply_demand,
10937                        x_return_status);
10938 
10939 
10940 
10941         -- now adjust the x_avail_assembly_qty
10942         IF PG_DEBUG in ('Y', 'C') THEN
10943            msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_atp_rec.combined_requested_date_qty := ' || l_atp_rec.combined_requested_date_qty);
10944            msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'l_atp_rec.requested_date_quantity := ' ||l_atp_rec.requested_date_quantity);
10945         END IF;
10946 
10947         IF p_search = 1 THEN
10948 
10949           IF PG_DEBUG in ('Y', 'C') THEN
10950              msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'p_search = 1');
10951           END IF;
10952 
10953           -- cchen 1238941
10954                     --4570421
10955           IF (NVL(l_atp_rec.combined_requested_date_qty,
10956               l_atp_rec.requested_date_quantity) >= l_requested_comp_qty) THEN
10957             NULL;
10958           ELSIF (NVL(l_atp_rec.combined_requested_date_qty,
10959                     l_atp_rec.requested_date_quantity) >0) THEN
10960                     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
10961                          (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
10962                         x_avail_assembly_qty := 0;
10963                         IF PG_DEBUG in ('Y', 'C') THEN
10964                              msc_sch_wb.atp_debug('Fixed or Lot Based Case: Lot qty not available: avail_assembly_qty: ' || x_avail_assembly_qty);
10965                         END IF;
10966                         IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 2 THEN --5403495
10967                            EXIT;
10968                         END IF;
10969                         --EXIT;
10970                     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
10971                         IF PG_DEBUG in ('Y', 'C') THEN
10972                              msc_sch_wb.atp_debug('Before inverse scaling : avail_assembly_qty: ' || x_avail_assembly_qty);
10973                         END IF;
10974                         x_avail_assembly_qty := LEAST(x_avail_assembly_qty,
10975                                                       round(
10976                                                             FLOOR( NVL(l_atp_rec.combined_requested_date_qty,
10977                                                                        l_atp_rec.requested_date_quantity)/l_comp_requirements.scale_multiple(j))* l_comp_requirements.scale_multiple(j)
10978                                                             /l_comp_requirements.comp_usage(j),6));
10979                         IF PG_DEBUG in ('Y', 'C') THEN
10980                              msc_sch_wb.atp_debug('Integer Scaling case : avail_assembly_qty: ' || x_avail_assembly_qty);
10981                         END IF;
10982                     ELSE
10983                         x_avail_assembly_qty := LEAST(x_avail_assembly_qty,
10984                                                        trunc(NVL(l_atp_rec.combined_requested_date_qty,
10985                        																 			 l_atp_rec.requested_date_quantity)/
10986                        																 			 l_comp_requirements.comp_usage(j),6));	-- 5598066
10987                         IF PG_DEBUG in ('Y', 'C') THEN
10988                              msc_sch_wb.atp_debug('Item or Proportional case : avail_assembly_qty: ' || x_avail_assembly_qty);
10989                         END IF;
10990                     END IF;
10991           ELSE
10992               x_avail_assembly_qty := 0;
10993                       --diag_atp
10994               IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 2 THEN
10995                 EXIT;
10996               END IF;
10997           END IF;
10998 
10999           --4570421, adding for testing, remove it
11000           --x_avail_assembly_qty := 0;
11001 
11002           -- 2869830
11003           IF PG_DEBUG in ('Y', 'C') THEN
11004               msc_sch_wb.atp_debug('avail_assembly_qty: ' || x_avail_assembly_qty);
11005           END IF;
11006 
11007           IF l_rounding_flag = 1 THEN
11008               x_avail_assembly_qty := FLOOR(x_avail_assembly_qty);
11009               msc_sch_wb.atp_debug('rounded avail qty: ' ||
11010                           x_avail_assembly_qty);
11011           END IF;
11012           IF PG_DEBUG in ('Y', 'C') THEN
11013              msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'x_avail_assembly_qty' || x_avail_assembly_qty);
11014           END IF;
11015         ELSE
11016           IF PG_DEBUG in ('Y', 'C') THEN
11017              msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'p_search = 2');
11018           END IF;
11019 
11020           IF l_atp_rec.ship_date IS NOT NULL THEN
11021             x_atp_date :=  GREATEST(MSC_CALENDAR.DATE_OFFSET
11022                                  (p_organization_id,
11023                                   p_instance_id,
11024                                   1,
11025                                   l_atp_rec.ship_date,
11026                                   NVL(l_comp_requirements.lead_time(j), 0)),
11027                                   x_atp_date);
11028 
11029             ---bug 3059305: If x_atp_date is greater than or equal to ship date from last source
11030             -- then date from last source will be used as the availability date.
11031             ---Therefore, no need to continue further
11032             IF x_atp_date >= p_comp_info_rec.ship_date_this_level THEN
11033                IF PG_DEBUG in ('Y', 'C') THEN
11034                   msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'x_atp_date := ' || x_atp_date);
11035                   msc_sch_wb.atp_debug('Get_Comp_Requirements: ' || 'x_atp_date > ship_date_this_level. Therefore exit');
11036                END IF;
11037                x_atp_date := null;
11038                EXIT;
11039             END IF;
11040           ELSE
11041             x_atp_date := NULL;
11042             EXIT;
11043           END IF;
11044         END IF;
11045 
11046         j := l_comp_requirements.inventory_item_id.NEXT(j);
11047 
11048     END LOOP;
11049 
11050     IF PG_DEBUG in ('Y', 'C') THEN
11051        msc_sch_wb.atp_debug('***** End Get_Comp_Requirements *****');
11052     END IF;
11053 
11054 
11055 Exception
11056 
11057     WHEN MSC_ATP_PVT.INVALID_OSS_SOURCE THEN
11058 
11059         IF PG_DEBUG in ('Y', 'C') THEN
11060            msc_sch_wb.atp_debug('Get_Comp_Reqs: ' || 'Invalid OSS setup detected');
11061         END IF;
11062         x_avail_assembly_qty  := 0;
11063         x_atp_date            := null;
11064         x_return_status := MSC_ATP_PVT.CTO_OSS_Error;
11065 
11066     -- 2400614 : krajan
11067     WHEN MSC_ATP_PVT.G_ATO_SOURCING_MISMATCH THEN
11068         IF PG_DEBUG in ('Y', 'C') THEN
11069            msc_sch_wb.atp_debug ('Get_Comp_Reqs: IN Exception Block for G_ATO_SOURCE');
11070         END IF;
11071         x_return_status := NVL(x_return_status, FND_API.G_RET_STS_ERROR);
11072         RAISE MSC_ATP_PVT.G_ATO_SOURCING_MISMATCH;
11073 
11074     -- dsting 2764213
11075     WHEN MSC_ATP_PVT.EXC_NO_PLAN_FOUND THEN
11076         IF PG_DEBUG in ('Y', 'C') THEN
11077            msc_sch_wb.atp_debug ('Get_Comp_Reqs: IN Exception Block for EXC_NO_PLAN_FOUND');
11078         END IF;
11079         x_return_status := NVL(x_return_status, FND_API.G_RET_STS_ERROR);
11080         RAISE MSC_ATP_PVT.EXC_NO_PLAN_FOUND;
11081 
11082     -- krajan 2752705
11083     WHEN MSC_ATP_PVT.G_EXC_UNCOLLECTED_ITEM THEN
11084         IF PG_DEBUG in ('Y', 'C') THEN
11085            msc_sch_wb.atp_debug ('Get_Comp_Reqs: IN Exception Block for G_EXC_UNCOLLECTED_ITEM');
11086         END IF;
11087         x_return_status := NVL(x_return_status, FND_API.G_RET_STS_ERROR);
11088         RAISE MSC_ATP_PVT.G_EXC_UNCOLLECTED_ITEM;
11089 
11090     ---bug 3308206: Add exception so that it could be propogated to ATP_check
11091     WHEN  MSC_ATP_PVT.EXC_NO_ATP_RULE THEN
11092         IF PG_DEBUG in ('Y', 'C') THEN
11093            msc_sch_wb.atp_debug('Get_Comp_Reqs: IN Exception Block for EXC_NO_ATP_RULE');
11094         END IF;
11095         RAISE MSC_ATP_PVT.EXC_NO_ATP_RULE;
11096 
11097     WHEN MSC_ATP_PVT.NO_MATCHING_DATE_IN_CAL THEN --bug3583705
11098         IF PG_DEBUG in ('Y', 'C') THEN
11099            msc_sch_wb.atp_debug ('Get_Comp_Reqs: IN Exception Block for NO_MATCHING_DATE_IN_CAL');
11100         END IF;
11101         RAISE MSC_ATP_PVT.NO_MATCHING_DATE_IN_CAL;
11102 
11103     WHEN OTHERS THEN
11104         IF PG_DEBUG in ('Y', 'C') THEN
11105            msc_sch_wb.atp_debug ('Get_Comp_Reqs: IN Exception Block in others');
11106            msc_sch_wb.atp_debug ('error := ' || SQLERRM);
11107         END IF;
11108         x_return_status := NVL(x_return_status, FND_API.G_RET_STS_ERROR);
11109 	RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
11110 
11111 END Get_Comp_Requirements;
11112 
11113 /* Spec changes as part of ship_rec_cal changes
11114    Various input output parameters grouped in the record Atp_Info_Rec*/
11115 PROCEDURE Get_Supplier_Atp_Info (
11116   p_sup_atp_info_rec                    IN OUT  NOCOPY  MSC_ATP_REQ.Atp_Info_Rec,
11117   x_atp_period                          OUT     NOCOPY  MRP_ATP_PUB.ATP_Period_Typ,
11118   x_atp_supply_demand                   OUT     NOCOPY  MRP_ATP_PUB.ATP_Supply_Demand_Typ,
11119   x_return_status                       OUT     NOCOPY  VARCHAR2
11120 )
11121 IS
11122 
11123 i 			PLS_INTEGER := 1;
11124 m                       PLS_INTEGER := 1;
11125 k                       PLS_INTEGER := 1;
11126 j                       PLS_INTEGER := 1; --4055719
11127 l_requested_date	DATE;
11128 l_atp_requested_date    DATE;
11129 l_pre_process_date      DATE;
11130 l_sysdate               DATE;
11131 
11132 l_atp_period_tab 	MRP_ATP_PUB.date_arr:=MRP_ATP_PUB.date_arr();
11133 l_atp_qty_tab 		MRP_ATP_PUB.number_arr:=MRP_ATP_PUB.number_arr();
11134 l_next_period           DATE;
11135 l_null_num              number := null;
11136 l_null_char             varchar2(3) := null;
11137 l_plan_start_date       DATE;
11138 l_demand_class          varchar2(30) := null;
11139 l_uom_code              varchar2(10);
11140 l_instance_id           number;
11141 l_org_id                number;
11142 l_processing_lead_time  number;
11143 l_postprocessing_lead_time      NUMBER;   -- SCLT new variable to remove join with msc_system_items
11144 
11145 l_default_atp_rule_id           NUMBER;
11146 l_calendar_code                 VARCHAR2(14);
11147 l_calendar_exception_set_id     NUMBER;
11148 l_default_demand_class          VARCHAR2(34);
11149 l_atp_info                      MRP_ATP_PVT.ATP_Info;
11150 l_cutoff_date                   DATE;
11151 l_capacity_defined              NUMBER;
11152 l_pre_process_lt		NUMBER;
11153 l_fix_var_lt 			NUMBER;
11154 l_tolerence_defined             NUMBER;
11155 
11156 l_org_code                      VARCHAR2(7);
11157 L_QTY_BEFORE_SYSDATE            number;
11158 
11159 l_last_cap_date                 date;
11160 
11161 --s_cto_rearch
11162 l_inv_item_id                   number;
11163 l_check_cap_model_flag          number;
11164 --e_cto_rearch
11165 
11166 -- For summary enhancement
11167 l_summary_flag  NUMBER;
11168 l_summary_sql   VARCHAR2(1);
11169 
11170 -- Variables added for ship_rec_cal
11171 -- l_plan_type                  PLS_INTEGER;    -- Variables commented for
11172 -- l_enforce_sup_capacity       PLS_INTEGER;    -- Enforce Pur LT
11173 l_last_cap_next_date		DATE;
11174 l_atp_date_this_level           DATE := NULL ;
11175 l_atp_date_quantity_this_level  NUMBER := NULL;
11176 l_requested_date_quantity	NUMBER := 0;
11177 -- l_optimized_plan             NUMBER;         -- Variables commented for
11178 -- l_constrain_plan             NUMBER;         -- Enforce Pur LT
11179 
11180 -- time_phased_atp
11181 l_return_status                 VARCHAR2(1);
11182 
11183 BEGIN
11184 
11185     IF PG_DEBUG in ('Y', 'C') THEN
11186         msc_sch_wb.atp_debug('***** Begin Get_Supplier_Atp_Info Procedure *****');
11187         msc_sch_wb.atp_debug('********** INPUT DATA:Get_Supplier_Atp_Info **********');
11188         msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'inventory_item_id: '|| to_char(p_sup_atp_info_rec.inventory_item_id));
11189         msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'organization_id: '|| to_char(p_sup_atp_info_rec.organization_id));
11190         msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'requested_date: '|| to_char(p_sup_atp_info_rec.requested_date));
11191         msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'instance_id: '|| to_char(p_sup_atp_info_rec.instance_id));
11192         msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'plan_id: '|| to_char(p_sup_atp_info_rec.plan_id));
11193         msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'quantity_ordered: '|| to_char(p_sup_atp_info_rec.quantity_ordered));
11194         msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'insert_flag: '|| to_char(p_sup_atp_info_rec.insert_flag));
11195         msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'supplier_id: '|| to_char(p_sup_atp_info_rec.supplier_id));
11196         msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'supplier_site_id: '|| to_char(p_sup_atp_info_rec.supplier_site_id));
11197         msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'sup_cap_cum_date: '|| to_char(p_sup_atp_info_rec.sup_cap_cum_date));
11198         msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || ' bom_item_type : ' || p_sup_atp_info_rec.bom_item_type);
11199         msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'rep_ord_flag := ' || p_sup_atp_info_rec.rep_ord_flag);
11200         msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'base item id := ' ||  p_sup_atp_info_rec.base_item_id);
11201         msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'MSC_ATP_PVT.G_PTF_DATE := ' || MSC_ATP_PVT.G_PTF_DATE);
11202     END IF;
11203 
11204 
11205     -- initialize API return status to success
11206     x_return_status := FND_API.G_RET_STS_SUCCESS;
11207 
11208     l_requested_date := trunc(p_sup_atp_info_rec.requested_date);
11209 
11210     ---profile option for including purchase order
11211     MSC_ATP_REQ.G_PURCHASE_ORDER_PREFERENCE := NVL(FND_PROFILE.VALUE('MSC_PO_DOCK_DATE_CALC_PREF'), 2);
11212 
11213     -- Instead re-assigned local values using global variable
11214     l_uom_code := MSC_ATP_PVT.G_ITEM_INFO_REC.uom_code;
11215     l_postprocessing_lead_time := MSC_ATP_PVT.G_ITEM_INFO_REC.pre_pro_lt;
11216 
11217     IF PG_DEBUG in ('Y', 'C') THEN
11218        msc_sch_wb.atp_debug('G_PURCHASE_ORDER_PREFERENCE := ' || G_PURCHASE_ORDER_PREFERENCE);
11219        msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'l_uom_code and l_postprocessing_lead_time = '||
11220           l_uom_code ||' : '||l_postprocessing_lead_time);
11221     END IF;
11222 
11223     --s_cto_rearch
11224     IF (p_sup_atp_info_rec.bom_item_type = 4 and p_sup_atp_info_rec.rep_ord_flag = 'Y') THEN
11225            IF PG_DEBUG in ('Y', 'C') THEN
11226               msc_sch_wb.atp_debug('ATO item');
11227            END IF;
11228            l_inv_item_id := p_sup_atp_info_rec.base_item_id;
11229            l_check_cap_model_flag := 1;
11230     ELSIF  p_sup_atp_info_rec.bom_item_type = 1 THEN
11231            IF PG_DEBUG in ('Y', 'C') THEN
11232               msc_sch_wb.atp_debug('Model entity');
11233            END IF;
11234            l_inv_item_id := p_sup_atp_info_rec.inventory_item_id;
11235            l_check_cap_model_flag := 1;
11236     ELSE
11237            IF PG_DEBUG in ('Y', 'C') THEN
11238               msc_sch_wb.atp_debug('Standard Item');
11239            END IF;
11240            l_inv_item_id := p_sup_atp_info_rec.inventory_item_id;
11241     END IF;
11242     --e_cto_rearch
11243     IF PG_DEBUG in ('Y', 'C') THEN
11244         msc_sch_wb.atp_debug('l_inv_item_id := ' || l_inv_item_id);
11245     END IF;
11246 
11247 
11248     -- bug 1169467
11249     -- get the plan start date. later on we will use this restrict the
11250     -- availability
11251 
11252     -- Instead re-assigned local values using global variable
11253     l_plan_start_date := MSC_ATP_PVT.G_PLAN_INFO_REC.plan_start_date;
11254     l_instance_id     := MSC_ATP_PVT.G_PLAN_INFO_REC.sr_instance_id;
11255     l_org_id          := MSC_ATP_PVT.G_PLAN_INFO_REC.organization_id;
11256     l_cutoff_date     := MSC_ATP_PVT.G_PLAN_INFO_REC.plan_cutoff_date;
11257 
11258     /* Modularize Item and Org Info */
11259     -- changed call, re-use info already obtained.
11260     -- Assumption is that since the instance and org is obtained using the plan_id,
11261     -- they are the same as the parameters p_sup_atp_info_rec.instance_id, p_sup_atp_info_rec.organization_id.
11262     MSC_ATP_PROC.get_global_org_info(l_instance_id, l_org_id);
11263     l_default_atp_rule_id := MSC_ATP_PVT.G_ORG_INFO_REC.default_atp_rule_id;
11264     l_default_demand_class := MSC_ATP_PVT.G_ORG_INFO_REC.default_demand_class;
11265     l_org_code := MSC_ATP_PVT.G_ORG_INFO_REC.org_code;
11266 
11267     /*
11268      Changes for ship_rec_cal begin
11269      1. For ship_rec_cal, Use SMC rather thn OMC for supplier capacity
11270      2. Supplier Capacity is considered infinite in following cases:
11271       (a) Plan is a constraint plan and 'enforce supplier capacity constraint' is unchecked.
11272       (b) No sources are defined. That means supplier_id = -99
11273       (b) Plan is an unconstraint plan and supplier is defined but it has not have any capacity, ie l_last_cap_date is null
11274     */
11275     l_calendar_code := p_sup_atp_info_rec.manufacturing_cal_code;
11276 
11277     l_calendar_exception_set_id := -1;
11278     -- l_enforce_sup_capacity	:= NVL(MSC_ATP_PVT.G_PLAN_INFO_REC.enforce_sup_capacity, 2); -- Enforce Pur LT
11279     l_sysdate := MSC_CALENDAR.PREV_WORK_DAY(l_calendar_code, p_sup_atp_info_rec.instance_id, sysdate);
11280     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);
11281 
11282     /* Enforce Pur LT - capacity is always enforced
11283        Removed code to check the plan type. This was earlier required as capacity constraints were earlier enforced
11284        only for unconstrained plans or if the plan option to enforce capacity was enforced. Now this is not required
11285        as capacity is always enforced. */
11286 
11287     IF PG_DEBUG in ('Y', 'C') THEN
11288        msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'l_plan_start_date = '||l_plan_start_date);
11289        msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'l_instance_id = '||l_instance_id);
11290        msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'l_org_id = '||l_org_id);
11291        msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'l_cutoff_date = '||l_cutoff_date);
11292        msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'l_default_atp_rule_id='||
11293                             l_default_atp_rule_id);
11294        msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'l_calendar_code='||l_calendar_code);
11295        msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'l_calendar_exception_set_id'||
11296                             l_calendar_exception_set_id);
11297        msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'l_default_demand_class'||
11298                             l_default_demand_class);
11299     END IF;
11300 
11301     /* Enforce Pur LT - capacity is always enforced.
11302        Aslo removed code to get last cap date as this code has been moved upstream in ATP_Check.
11303        Date is passed from there. If the passed date is NULL then it means capacity is not defined.*/
11304     -- IF (p_sup_atp_info_rec.supplier_id = -99) OR (l_plan_type = 1 AND l_enforce_sup_capacity = 2) THEN
11305     IF (p_sup_atp_info_rec.supplier_id = -99) OR p_sup_atp_info_rec.last_cap_date IS NULL THEN
11306 	l_capacity_defined := 0;
11307 	l_last_cap_next_date := MSC_CALENDAR.NEXT_WORK_DAY(
11308 		l_calendar_code,
11309 		p_sup_atp_info_rec.instance_id,
11310 		GREATEST(l_plan_start_date, MSC_ATP_PVT.G_PTF_DATE, p_sup_atp_info_rec.sup_cap_cum_date));
11311     ELSE
11312 	l_capacity_defined := 1;
11313 	l_last_cap_next_date :=  MSC_CALENDAR.NEXT_WORK_DAY(
11314                 l_calendar_code,
11315                 p_sup_atp_info_rec.instance_id,
11316                 p_sup_atp_info_rec.last_cap_date + 1);
11317     END IF;
11318     /* Enforce Pur LT changes end */
11319 
11320     IF PG_DEBUG in ('Y', 'C') THEN
11321         msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'l_last_cap_next_date='||
11322                             l_last_cap_next_date);
11323     END IF;
11324 
11325     IF l_capacity_defined = 0 THEN
11326         -- no capacity is ever defined, treat it as infinite capacity and
11327         -- by pass the huge sql for net atp
11328 
11329         -- add one more entry to indicate sysdate
11330         -- and infinite quantity.
11331         l_atp_period_tab.EXTEND;
11332         l_atp_qty_tab.EXTEND;
11333         i:= l_atp_period_tab.COUNT;
11334         IF PG_DEBUG in ('Y', 'C') THEN
11335             msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'l_plan_start_date := ' || l_plan_start_date);
11336         END IF;
11337 
11338         -- Ship_rec_cal.
11339         l_atp_period_tab(i) := l_last_cap_next_date;
11340         l_atp_qty_tab(i) := MSC_ATP_PVT.INFINITE_NUMBER;
11341 
11342         IF (NVL(p_sup_atp_info_rec.insert_flag, 0) <> 0) THEN
11343 
11344             -- dsting clear sd details temp table
11345             MSC_ATP_DB_UTILS.Clear_SD_Details_Temp();
11346 
11347             -- add one more entry to indicate infinite time fence date
11348             -- and quantity.
11349             MSC_SATP_FUNC.Extend_Atp_Period(x_atp_period, x_return_status);
11350             i:= x_atp_period.level.COUNT;
11351 
11352             x_atp_period.Level(i) := p_sup_atp_info_rec.level;
11353             x_atp_period.Identifier(i) := p_sup_atp_info_rec.identifier;
11354             x_atp_period.Scenario_Id(i) := p_sup_atp_info_rec.scenario_id;
11355             x_atp_period.Pegging_Id(i) := NULL;
11356             x_atp_period.End_Pegging_Id(i) := NULL;
11357 
11358             x_atp_period.Supplier_Id(i) := p_sup_atp_info_rec.supplier_id;
11359             x_atp_period.Supplier_Site_Id(i) := p_sup_atp_info_rec.supplier_site_id;
11360             x_atp_period.Organization_id(i) := p_sup_atp_info_rec.organization_id;
11361 
11362             --  ship_rec_cal changes begin
11363             x_atp_period.Period_Start_Date(i) := l_last_cap_next_date;
11364             --  ship_rec_cal changes end
11365 
11366             x_atp_period.Total_Supply_Quantity(i) := MSC_ATP_PVT.INFINITE_NUMBER;
11367             x_atp_period.Total_Demand_Quantity(i) := 0;
11368             x_atp_period.Period_Quantity(i) := MSC_ATP_PVT.INFINITE_NUMBER;
11369             x_atp_period.Cumulative_Quantity(i) := MSC_ATP_PVT.INFINITE_NUMBER;
11370 
11371         END IF;
11372     ELSE  -- else of l_capacity_defined = 0
11373 
11374         -- we really need to check net supplier site capacity
11375         -- we need to have a branch here for allocated atp
11376         IF (MSC_ATP_PVT.G_ALLOCATED_ATP = 'N') OR
11377            (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y' AND MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1 AND
11378                  MSC_ATP_PVT.G_ALLOCATION_METHOD = 1)  THEN
11379 
11380          -- check if we have tolerence defined for this item/org/supplier/site
11381 
11382          l_tolerence_defined := 0;
11383          BEGIN
11384            SELECT rownum
11385            INTO   l_tolerence_defined
11386            FROM   msc_supplier_flex_fences
11387            WHERE  plan_id = p_sup_atp_info_rec.plan_id
11388            AND    sr_instance_id = p_sup_atp_info_rec.instance_id
11389            AND    organization_id = p_sup_atp_info_rec.organization_id
11390            --s_cto_rearch
11391            --AND    inventory_item_id = p_sup_atp_info_rec.inventory_item_id
11392            AND    inventory_item_id = l_inv_item_id
11393            AND    supplier_id = p_sup_atp_info_rec.supplier_id
11394            AND    supplier_site_id = p_sup_atp_info_rec.supplier_site_id
11395            AND    rownum = 1;
11396         EXCEPTION
11397            WHEN NO_DATA_FOUND THEN
11398                l_tolerence_defined := 0;
11399         END;
11400 
11401         --  structure changed slightly for summary enhancement. Now it is as follows
11402         --  IF insert_flag = 0 THEN
11403         --      IF l_tolerence_defined = 0 AND G_SUMMARY_FLAG='Y' THEN
11404         --          IF summary_flag not in (1,3,9) THEN
11405         --              l_summary_sql := 'Y'
11406         --          ELSE
11407         --              l_summary_sql := 'N'
11408         --          END IF;
11409         --      END IF;
11410         --      IF l_summary_sql = 'Y' THEN
11411         --          use summary SQLs
11412         --      ELSE
11413         --          use non summary SQLs
11414         --      END IF;
11415         --  ELSE
11416         --      use details SQLs
11417         --  END IF;
11418 
11419         --=======================================================================================================
11420         --  ship_rec_cal changes
11421         --  use SMC instead of OMC for netting
11422         --  IF SMC is FOC get plan owning org's calendar. Since we assume that every org must have atleast a
11423         --  manufacturing calendar defined, we use plan owning org's calendar as it will be spanning atleast
11424         --  upto plan end date
11425         --=======================================================================================================
11426         IF l_calendar_code = '@@@' THEN
11427                 SELECT  tp.calendar_code
11428                 INTO    l_calendar_code
11429                 FROM    msc_trading_partners tp,
11430                         msc_plans mp
11431                 WHERE   mp.plan_id = p_sup_atp_info_rec.plan_id
11432                 AND     tp.sr_instance_id  = mp.sr_instance_id
11433                 AND     tp.partner_type    = 3
11434                 AND     tp.sr_tp_id        = mp.organization_id;
11435         END IF;
11436 
11437         msc_sch_wb.atp_debug('l_calendar_code := ' || l_calendar_code);
11438         --=======================================================================================================
11439         -- ship_rec_cal changes begin
11440         --=======================================================================================================
11441         --  In all the SQLs that get supplier capacities following are the changes:
11442         --  1. Pass (c.seq_num - p_sup_atp_info_rec.sysdate_seq_num) to get_tolerance_percentage fn instead of
11443         --     passing c.calendar_date.
11444         --  2. If calendar code passed in FOC, we use plan owning org's calendar and remove p_seq_num is not
11445         --     null filter condition.
11446         --
11447         --  In all the SQLs that get planned orders, purchase orders and purchase requisitions following
11448         --  are the changes:
11449         --  1. We use new_dock_date or new_ship_date depending on whether supplier capacity is dock capacity or
11450         --     ship capacity.
11451         --     Earlier we used to look at new_schedule_date and offset post_processing_lead_time.
11452         --  2. Removed join with msc_calendar_dates
11453         --=======================================================================================================
11454 
11455         IF (NVL(p_sup_atp_info_rec.insert_flag, 0) = 0) THEN
11456             IF (l_tolerence_defined = 0) AND (MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y') THEN
11457                 ---- we do summary approach only if tolerance is not defined
11458                 ---- since one of the components for calculating tolerance is difference of sys_date and
11459                 ---- request date, we might not get a right data if we include tolerance in summary data
11460 
11461                 -- Summary enhancement - check summary flag
11462                 SELECT  summary_flag
11463                 INTO    l_summary_flag
11464                 FROM    msc_plans plans
11465                 WHERE   plans.plan_id = p_sup_atp_info_rec.plan_id;
11466 
11467                 IF PG_DEBUG in ('Y', 'C') THEN
11468                     msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'l_summary_flag := ' || l_summary_flag);
11469                 END IF;
11470 
11471                 IF l_summary_flag NOT IN (MSC_POST_PRO.G_SF_SUMMARY_NOT_RUN, MSC_POST_PRO.G_SF_PREALLOC_COMPLETED,
11472                                           MSC_POST_PRO.G_SF_FULL_SUMMARY_RUNNING) THEN
11473                     -- Summary SQL can be used
11474                     l_summary_sql := 'Y';
11475                 ELSE
11476                     l_summary_sql := 'N';
11477                 END IF;
11478             ELSE
11479                 l_summary_sql := 'N';
11480             END IF;
11481 
11482             IF l_summary_sql = 'Y' THEN
11483                 IF PG_DEBUG in ('Y', 'C') THEN
11484                     msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'Summary mode supplier info');
11485                 END IF;
11486 
11487                 IF l_check_cap_model_flag = 1 THEN
11488 
11489                     SELECT  SD_DATE,
11490                             SUM(SD_QTY)
11491                     BULK COLLECT INTO l_atp_period_tab, l_atp_qty_tab
11492                     FROM
11493                         (
11494                             select  /*+ INDEX(msc_atp_summary_sup MSC_ATP_SUMMARY_SUP_U1) */
11495                                     sd_date,
11496                                     sd_qty
11497                             from    msc_atp_summary_sup
11498                             where   plan_id = p_sup_atp_info_rec.plan_id
11499                             and     sr_instance_id = p_sup_atp_info_rec.instance_id
11500                             and     supplier_id = p_sup_atp_info_rec.supplier_id
11501                             and     supplier_site_id = p_sup_atp_info_rec.supplier_site_id
11502                             --and     sd_date >= l_plan_start_date
11503                             and     sd_date BETWEEN l_plan_start_date
11504                                                     AND  least(p_sup_atp_info_rec.last_cap_date,l_cutoff_date) --4055719
11505                             and     sd_qty <> 0
11506                             AND    (inventory_item_id = l_inv_item_id OR
11507                                     inventory_item_id in
11508                                            (select inventory_item_id from msc_system_items msi
11509                                             where  msi.base_item_id = l_inv_item_id
11510                                             and    msi.plan_id = p_sup_atp_info_rec.plan_id
11511                                             and    msi.organization_id = p_sup_atp_info_rec.organization_id
11512                                             and    msi.base_item_id = l_inv_item_id))
11513 
11514                             UNION ALL
11515 
11516                             -- Summary enhancement : differences from non summary SQL: ship/rec cal changes pending
11517                             --  1. No union with MSC_SUPPLIER_CAPACITIES
11518                             --  2. MSC_PLANS included in the join to get latest refresh number
11519                             --  3. Filter records based on refresh_number
11520                             --Fixing as a part of bug3709707 adding trunc so that 2 column are not seen in HP
11521                             SELECT  TRUNC(Decode(p_sup_atp_info_rec.sup_cap_type,
11522                                                 1, p.new_ship_date,
11523                                                 p.new_dock_date)) l_date, -- For ship_rec_cal
11524                                     (NVL(p.implement_quantity,0) - p.new_order_quantity) quantity
11525                             FROM    msc_supplies p,
11526                                     msc_plans pl            -- For summary enhancement
11527                             WHERE  (p.order_type IN (5, 2,60)
11528                                     OR (MSC_ATP_REQ.G_PURCHASE_ORDER_PREFERENCE = MSC_ATP_REQ.G_PROMISE_DATE
11529                                     AND p.order_type = 1 AND p.promised_date IS NULL))
11530                             AND     p.plan_id = p_sup_atp_info_rec.plan_id
11531                             AND     p.sr_instance_id = p_sup_atp_info_rec.instance_id
11532                             AND     p.supplier_id  = p_sup_atp_info_rec.supplier_id
11533                             AND     NVL(p.supplier_site_id, -1) = NVL(p_sup_atp_info_rec.supplier_site_id, -1)
11534                             AND     NVL(P.DISPOSITION_STATUS_TYPE, 1) <> 2
11535                             AND    (p.inventory_item_id = l_inv_item_id OR
11536                                     p.inventory_item_id in
11537                                            (select inventory_item_id from msc_system_items msi
11538                                             where  msi.sr_instance_id = p_sup_atp_info_rec.instance_id
11539                                             and    msi.plan_id = p_sup_atp_info_rec.plan_id
11540                                             and    msi.organization_id = p_sup_atp_info_rec.organization_id
11541                                             and    msi.base_item_id = l_inv_item_id))
11542                             AND     pl.plan_id = p.plan_id                          -- For summary enhancement
11543                             AND     (p.refresh_number > pl.latest_refresh_number    -- For summary enhancement
11544                                      OR p.refresh_number = p_sup_atp_info_rec.refresh_number)        -- For summary enhancement
11545                             AND    Decode(p_sup_atp_info_rec.sup_cap_type, 1, p.new_ship_date,p.new_dock_date) --4055719
11546                                           <= least(p_sup_atp_info_rec.last_cap_date,l_cutoff_date)
11547                         )
11548                     GROUP BY SD_DATE
11549                     ORDER BY SD_DATE;
11550 
11551                 ELSE
11552 
11553                     SELECT  SD_DATE,
11554                             SUM(SD_QTY)
11555                     BULK COLLECT INTO l_atp_period_tab, l_atp_qty_tab
11556                     FROM
11557                         (
11558                             select  /*+ INDEX(msc_atp_summary_sup MSC_ATP_SUMMARY_SUP_U1) */
11559                                    trunc( sd_date) SD_DATE, --4135752
11560                                     sd_qty
11561                             from    msc_atp_summary_sup
11562                             where   plan_id = p_sup_atp_info_rec.plan_id
11563                             and     sr_instance_id = p_sup_atp_info_rec.instance_id
11564                             and     inventory_item_id = p_sup_atp_info_rec.inventory_item_id
11565                             and     supplier_id = p_sup_atp_info_rec.supplier_id
11566                             and     supplier_site_id = p_sup_atp_info_rec.supplier_site_id
11567                             --and     sd_date >= l_plan_start_date
11568                             and     sd_date BETWEEN l_plan_start_date
11569                                                     AND  least(p_sup_atp_info_rec.last_cap_date,l_cutoff_date) --4055719
11570                             and     sd_qty <> 0
11571 
11572                             UNION ALL
11573 
11574                             -- Summary enhancement : differences from non summary SQL: ship/rec cal changes pending
11575                             --  1. No union with MSC_SUPPLIER_CAPACITIES
11576                             --  2. MSC_PLANS included in the join to get latest refresh number
11577                             --  3. Filter records based on refresh_number
11578                             --Fixing as a part of bug3709707 adding trunc so that 2 column are not seen in HP
11579                             SELECT  TRUNC(Decode(p_sup_atp_info_rec.sup_cap_type,
11580                                                 1, p.new_ship_date,
11581                                                 p.new_dock_date)) l_date, -- For ship_rec_cal
11582                                     (NVL(p.implement_quantity,0) - p.new_order_quantity) quantity
11583                             FROM    msc_supplies p,
11584                                     msc_plans pl            -- For summary enhancement
11585                             WHERE  (p.order_type IN (5, 2, 60)
11586                                     OR (MSC_ATP_REQ.G_PURCHASE_ORDER_PREFERENCE = MSC_ATP_REQ.G_PROMISE_DATE
11587                                     AND p.order_type = 1 AND p.promised_date IS NULL))
11588                             AND     p.plan_id = p_sup_atp_info_rec.plan_id
11589                             AND     p.sr_instance_id = p_sup_atp_info_rec.instance_id
11590                             AND     p.inventory_item_id = p_sup_atp_info_rec.inventory_item_id
11591                             AND     p.supplier_id  = p_sup_atp_info_rec.supplier_id
11592                             AND     NVL(p.supplier_site_id, -1) = NVL(p_sup_atp_info_rec.supplier_site_id, -1)
11593                             AND     NVL(P.DISPOSITION_STATUS_TYPE, 1) <> 2
11594                             AND     pl.plan_id = p.plan_id                          -- For summary enhancement
11595                             AND     (p.refresh_number > pl.latest_refresh_number    -- For summary enhancement
11596                                      OR p.refresh_number = p_sup_atp_info_rec.refresh_number)        -- For summary enhancement
11597                             AND     Decode(p_sup_atp_info_rec.sup_cap_type, 1, trunc(p.new_ship_date),trunc(p.new_dock_date)) --4055719 --4135752
11598                                           <= trunc(least(p_sup_atp_info_rec.last_cap_date,l_cutoff_date))          --4135752
11599                         )
11600                     GROUP BY SD_DATE
11601                     ORDER BY SD_DATE;
11602 
11603                 END IF;
11604 
11605                 MSC_ATP_PROC.atp_consume(l_atp_qty_tab, l_atp_qty_tab.COUNT);
11606 
11607                 /* Cum drop issue changes begin*/
11608                 MSC_AATP_PROC.Atp_Remove_Negatives(l_atp_qty_tab, l_return_status);
11609                 IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
11610                         IF PG_DEBUG in ('Y', 'C') THEN
11611                                 msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'Error occured in procedure Atp_Remove_Negatives');
11612                         END IF;
11613                         RAISE FND_API.G_EXC_ERROR;
11614                 END IF;
11615                 /* Cum drop issue changes end*/
11616 
11617             ELSE    -- IF l_summary_sql = 'Y' THEN
11618 
11619                 IF l_check_cap_model_flag = 1 THEN
11620                     msc_sch_wb.atp_debug('Check Sources for model, details are off');
11621 
11622                     SELECT  trunc(l_date), SUM(quantity)  --4135752
11623                     BULK COLLECT INTO
11624                     l_atp_period_tab,
11625                     l_atp_qty_tab
11626                     FROM (
11627                     SELECT c.calendar_date l_date, s.capacity*(1+
11628                                                  DECODE(l_tolerence_defined, 0, 0,
11629                                                  NVL(MSC_ATP_FUNC.get_tolerance_percentage(
11630                                                  p_sup_atp_info_rec.instance_id,
11631                                                  p_sup_atp_info_rec.plan_id,
11632                                                  l_inv_item_id,
11633                                                  p_sup_atp_info_rec.organization_id,
11634                                                  p_sup_atp_info_rec.supplier_id,
11635                                                  p_sup_atp_info_rec.supplier_site_id,
11636                                                  -- ship_rec_cal
11637                                                  c.seq_num - p_sup_atp_info_rec.sysdate_seq_num),0))) quantity
11638                     FROM   msc_calendar_dates c,
11639                            msc_supplier_capacities s
11640                     WHERE  s.inventory_item_id = l_inv_item_id
11641                     AND    s.sr_instance_id = p_sup_atp_info_rec.instance_id
11642                     AND    s.plan_id = p_sup_atp_info_rec.plan_id
11643                     AND    s.organization_id = p_sup_atp_info_rec.organization_id
11644                     AND    s.supplier_id = p_sup_atp_info_rec.supplier_id
11645                     AND    NVL(s.supplier_site_id, -1) = NVL(p_sup_atp_info_rec.supplier_site_id, -1)
11646                     AND    c.calendar_date BETWEEN trunc(s.from_date)
11647                                            --AND NVL(s.to_date,l_cutoff_date)
11648                                            AND trunc(NVL(s.to_date,least(p_sup_atp_info_rec.last_cap_date,l_cutoff_date))) --4055719
11649                     AND    (c.seq_num IS NOT NULL OR p_sup_atp_info_rec.manufacturing_cal_code  = MSC_CALENDAR.FOC)
11650                     AND    c.calendar_code = l_calendar_code
11651                     AND    c.exception_set_id = l_calendar_exception_set_id
11652                     AND    c.sr_instance_id = s.sr_instance_id -- Changed from l_instance_id ?
11653                     AND    c.calendar_date >= p_sup_atp_info_rec.sup_cap_cum_date
11654                     -- Supplier Capacity (SCLT) Accumulation starts from this date.
11655                     -- AND    c.calendar_date >= l_plan_start_date -- bug 1169467
11656                     UNION ALL
11657                     /* Net out planned orders, purchase orders and purchase requisitions */
11658                     -- bug 1303196
11659                     --Fixing as a part of bug3709707 adding trunc so that 2 column are not seen in HP
11660                     SELECT TRUNC(Decode(p_sup_atp_info_rec.sup_cap_type,
11661                                                 1, p.new_ship_date,
11662                                                 p.new_dock_date)) l_date, -- For ship_rec_cal
11663         	           -- performance dsting rearrange signs to get rid of multiply times -1
11664                            (NVL(p.implement_quantity,0) - p.new_order_quantity) quantity
11665                     FROM   msc_supplies p
11666                     WHERE  (p.order_type IN (5, 2, 60)
11667                             --include purchase orders based on profile option
11668                             OR (MSC_ATP_REQ.G_PURCHASE_ORDER_PREFERENCE = MSC_ATP_REQ.G_PROMISE_DATE
11669                                 AND p.order_type = 1 AND p.promised_date IS NULL))
11670                     -- Supplier Capacity (SCLT) Accumulation Ignore Purchase Orders
11671                     -- WHERE  p.order_type IN (5, 1, 2)
11672                     AND    p.plan_id = p_sup_atp_info_rec.plan_id
11673                     AND    p.sr_instance_id = p_sup_atp_info_rec.instance_id
11674               -- 1214694      AND    p.organization_id = p_sup_atp_info_rec.organization_id
11675                     AND    p.supplier_id  = p_sup_atp_info_rec.supplier_id
11676                     AND    NVL(p.supplier_site_id, -1) = NVL(p_sup_atp_info_rec.supplier_site_id, -1)
11677                            -- Exclude Cancelled Supplies 2460645
11678                     AND    NVL(P.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
11679                     -- Supplier Capacity (SCLT) Changes End
11680                            ---only consider ATP inserted POs
11681                            --- Plan POs are tied to forecast.
11682                     AND    ((p.inventory_item_id = l_inv_item_id and p.record_source=2) OR
11683                             p.inventory_item_id in
11684                                    (select inventory_item_id from msc_system_items msi
11685                                     where  msi.sr_instance_id = p_sup_atp_info_rec.instance_id
11686                                     and    msi.plan_id = p_sup_atp_info_rec.plan_id
11687                                     and    msi.organization_id = p_sup_atp_info_rec.organization_id
11688                                     and    msi.base_item_id = l_inv_item_id))
11689                      AND    trunc(Decode(p_sup_atp_info_rec.sup_cap_type, 1, p.new_ship_date,p.new_dock_date)) --4055719 --4135752
11690                                  <= trunc(least(p_sup_atp_info_rec.last_cap_date,l_cutoff_date)))
11691                     GROUP BY l_date
11692                     ORDER BY l_date;
11693 
11694                     MSC_ATP_PROC.atp_consume(l_atp_qty_tab, l_atp_qty_tab.COUNT);
11695 
11696                     /* Cum drop issue changes begin*/
11697                     MSC_AATP_PROC.Atp_Remove_Negatives(l_atp_qty_tab, l_return_status);
11698                     IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
11699                         IF PG_DEBUG in ('Y', 'C') THEN
11700                                 msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'Error occured in procedure Atp_Remove_Negatives');
11701                         END IF;
11702                         RAISE FND_API.G_EXC_ERROR;
11703                     END IF;
11704                     /* Cum drop issue changes end*/
11705 
11706                 ELSE
11707 
11708                     SELECT  trunc(l_date), SUM(quantity)  --4135752
11709                     BULK COLLECT INTO
11710                     l_atp_period_tab,
11711                     l_atp_qty_tab
11712                     FROM (
11713                     SELECT c.calendar_date l_date, s.capacity*(1+
11714                                                  DECODE(l_tolerence_defined, 0, 0,
11715                                                  NVL(MSC_ATP_FUNC.get_tolerance_percentage(
11716                                                  p_sup_atp_info_rec.instance_id,
11717                                                  p_sup_atp_info_rec.plan_id,
11718                                                  p_sup_atp_info_rec.inventory_item_id,
11719                                                  p_sup_atp_info_rec.organization_id,
11720                                                  p_sup_atp_info_rec.supplier_id,
11721                                                  p_sup_atp_info_rec.supplier_site_id,
11722                                                  -- ship_rec_cal
11723                                                  c.seq_num - p_sup_atp_info_rec.sysdate_seq_num),0))) quantity
11724                     FROM   msc_calendar_dates c,
11725                            msc_supplier_capacities s
11726                     WHERE  s.inventory_item_id = p_sup_atp_info_rec.inventory_item_id
11727                     AND    s.sr_instance_id = p_sup_atp_info_rec.instance_id
11728                     AND    s.plan_id = p_sup_atp_info_rec.plan_id
11729                     AND    s.organization_id = p_sup_atp_info_rec.organization_id
11730                     AND    s.supplier_id = p_sup_atp_info_rec.supplier_id
11731                     AND    NVL(s.supplier_site_id, -1) = NVL(p_sup_atp_info_rec.supplier_site_id, -1)
11732                     AND    c.calendar_date BETWEEN trunc(s.from_date)
11733                                            --AND NVL(s.to_date,l_cutoff_date)
11734                                            AND trunc(NVL(s.to_date,least(p_sup_atp_info_rec.last_cap_date,l_cutoff_date))) --4055719
11735                     AND    (c.seq_num IS NOT NULL OR p_sup_atp_info_rec.manufacturing_cal_code  = MSC_CALENDAR.FOC)
11736                     AND    c.calendar_code = l_calendar_code
11737                     AND    c.exception_set_id = l_calendar_exception_set_id
11738                     AND    c.sr_instance_id = s.sr_instance_id -- Changed from l_instance_id ?
11739                     AND    c.calendar_date >= trunc(p_sup_atp_info_rec.sup_cap_cum_date) --4135752
11740                     -- Supplier Capacity (SCLT) Accumulation starts from this date.
11741                     -- AND    c.calendar_date >= l_plan_start_date -- bug 1169467
11742                     UNION ALL
11743                     /* Net out planned orders, purchase orders and purchase requisitions */
11744                     -- bug 1303196
11745                     --Fixing as a part of bug3709707 adding trunc so that 2 column are not seen in HP
11746                     SELECT TRUNC(Decode(p_sup_atp_info_rec.sup_cap_type,
11747                                                 1, p.new_ship_date,
11748                                                 p.new_dock_date)) l_date, -- For ship_rec_cal
11749         	           -- performance dsting rearrange signs to get rid of multiply times -1
11750                            (NVL(p.implement_quantity,0) - p.new_order_quantity) quantity
11751                     FROM   msc_supplies p
11752                         WHERE  (p.order_type IN (5, 2, 60)
11753                                 --include purchase orders based on profile option
11754                                 OR (MSC_ATP_REQ.G_PURCHASE_ORDER_PREFERENCE = MSC_ATP_REQ.G_PROMISE_DATE
11755                                     AND p.order_type = 1 AND p.promised_date IS NULL))
11756                         -- Supplier Capacity (SCLT) Accumulation Ignore Purchase Orders
11757                         -- WHERE  p.order_type IN (5, 1, 2)
11758                         AND    p.plan_id = p_sup_atp_info_rec.plan_id
11759                         AND    p.sr_instance_id = p_sup_atp_info_rec.instance_id
11760                         AND    p.inventory_item_id = p_sup_atp_info_rec.inventory_item_id
11761                   -- 1214694      AND    p.organization_id = p_sup_atp_info_rec.organization_id
11762                         AND    p.supplier_id  = p_sup_atp_info_rec.supplier_id
11763                         AND    NVL(p.supplier_site_id, -1) = NVL(p_sup_atp_info_rec.supplier_site_id, -1)
11764                                -- Exclude Cancelled Supplies 2460645
11765                         AND    NVL(P.DISPOSITION_STATUS_TYPE, 1) <> 2
11766                         AND   trunc( Decode(p_sup_atp_info_rec.sup_cap_type, 1, p.new_ship_date,p.new_dock_date)) --4055719 --4135752
11767                                       <= trunc(least(p_sup_atp_info_rec.last_cap_date,l_cutoff_date)))
11768                         GROUP BY l_date
11769                         ORDER BY l_date;
11770 
11771                         MSC_ATP_PROC.atp_consume(l_atp_qty_tab, l_atp_qty_tab.COUNT);
11772 
11773                         /* Cum drop issue changes begin*/
11774                         MSC_AATP_PROC.Atp_Remove_Negatives(l_atp_qty_tab, l_return_status);
11775                         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
11776                                 IF PG_DEBUG in ('Y', 'C') THEN
11777                                         msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'Error occured in procedure Atp_Remove_Negatives');
11778                                 END IF;
11779                                 RAISE FND_API.G_EXC_ERROR;
11780                         END IF;
11781                         /* Cum drop issue changes end*/
11782 
11783                     END IF; --IF l_check_cap_model_flag = 1 THEN
11784 
11785                 END IF; -- IF l_summary_sql = 'Y' THEN
11786 
11787             ELSE  -- now p_sup_atp_info_rec.insert_flag <> 0
11788 
11789                 MSC_ATP_DB_UTILS.Clear_SD_Details_temp();
11790 
11791                 IF l_check_cap_model_flag = 1 THEN
11792                     msc_sch_wb.atp_debug('Check Sources for model, details are on');
11793                     INSERT INTO msc_atp_sd_details_temp (
11794                        ATP_level,
11795                        Order_line_id,
11796                        Scenario_Id,
11797                        Inventory_Item_Id,
11798                        Request_Item_Id,
11799                        Organization_Id,
11800                        Department_Id,
11801                        Resource_Id,
11802                        Supplier_Id,
11803                        Supplier_Site_Id,
11804                        From_Organization_Id,
11805                        From_Location_Id,
11806                        To_Organization_Id,
11807                        To_Location_Id,
11808                        Ship_Method,
11809                        UOM_code,
11810                        Supply_Demand_Type,
11811                        Supply_Demand_Source_Type,
11812                        Supply_Demand_Source_Type_Name,
11813                        Identifier1,
11814                        Identifier2,
11815                        Identifier3,
11816                        Identifier4,
11817                        Supply_Demand_Quantity,
11818                        Supply_Demand_Date,
11819                        Disposition_Type,
11820                        Disposition_Name,
11821                        Pegging_Id,
11822                        End_Pegging_Id,
11823                        creation_date,
11824                        created_by,
11825                        last_update_date,
11826                        last_updated_by,
11827                        last_update_login
11828                     )
11829 
11830                          (SELECT
11831                                 p_sup_atp_info_rec.level col1,
11832                                 MSC_ATP_PVT.G_ORDER_LINE_ID col2,
11833                                 p_sup_atp_info_rec.scenario_id col3,
11834                                 l_null_num col4 ,
11835                                 l_null_num col5,
11836                                 p_sup_atp_info_rec.organization_id col6,
11837                                 l_null_num col7,
11838                                 l_null_num col8,
11839                                 p_sup_atp_info_rec.supplier_id col9,
11840                                 p_sup_atp_info_rec.supplier_site_id col10,
11841                                 l_null_num col11,
11842                                 l_null_num col12,
11843                                 l_null_num col13,
11844                                 l_null_num col14,
11845                                 l_null_char col15,
11846                                 l_uom_code col16,
11847                                 2 col17, -- supply
11848                                 l_null_num col18,
11849                                 l_null_char col19,
11850                                 p_sup_atp_info_rec.instance_id col20,
11851                                 l_null_num col21,
11852                                 l_null_num col22,
11853                                 l_null_num col23,
11854                                 s.capacity*(1+ DECODE(l_tolerence_defined, 0, 0,
11855                                                   NVL(MSC_ATP_FUNC.get_tolerance_percentage(
11856                                                   p_sup_atp_info_rec.instance_id,
11857                                                   p_sup_atp_info_rec.plan_id,
11858                                                   l_inv_item_id,
11859                                                   p_sup_atp_info_rec.organization_id,
11860                                                   p_sup_atp_info_rec.supplier_id,
11861                                                   p_sup_atp_info_rec.supplier_site_id,
11862                                                   -- ship_rec_cal
11863                                                   c.seq_num - p_sup_atp_info_rec.sysdate_seq_num),0))) col24,
11864                                 C.CALENDAR_DATE col25,
11865                                 l_null_num col26,
11866                                 l_null_char col27,
11867                                 l_null_num col28,
11868                                 l_null_num col29,
11869                                 l_sysdate,
11870                                 FND_GLOBAL.User_ID,
11871                                 l_sysdate,
11872                                 FND_GLOBAL.User_ID,
11873                                 FND_GLOBAL.User_ID
11874                          FROM   msc_calendar_dates c,
11875                                 msc_supplier_capacities s
11876                          WHERE  s.inventory_item_id = l_inv_item_id
11877                          AND    s.sr_instance_id = p_sup_atp_info_rec.instance_id
11878                          AND    s.plan_id = p_sup_atp_info_rec.plan_id
11879                          AND    s.organization_id = p_sup_atp_info_rec.organization_id
11880                          AND    s.supplier_id = p_sup_atp_info_rec.supplier_id
11881                          AND    NVL(s.supplier_site_id, -1) = NVL(p_sup_atp_info_rec.supplier_site_id, -1)
11882                          AND    c.calendar_date BETWEEN trunc(s.from_date)
11883                                                 --AND NVL(s.to_date,l_cutoff_date)
11884                                                 AND trunc(NVL(s.to_date,least(p_sup_atp_info_rec.last_cap_date,l_cutoff_date))) --4055719
11885                          AND    (c.seq_num IS NOT NULL OR p_sup_atp_info_rec.manufacturing_cal_code  = MSC_CALENDAR.FOC)
11886                          AND    c.calendar_code = l_calendar_code
11887                          AND    c.exception_set_id = l_calendar_exception_set_id
11888                          AND    c.sr_instance_id = s.sr_instance_id  -- Changed from l_instance_id ?
11889                          AND    c.calendar_date >= trunc(p_sup_atp_info_rec.sup_cap_cum_date) --4135752
11890                          -- Supplier Capacity (SCLT) Accumulation starts from this date.
11891                          -- AND    c.calendar_date >= l_plan_start_date -- bug 1169467
11892                          UNION ALL
11893                          SELECT
11894                                 p_sup_atp_info_rec.level col1,
11895                                 MSC_ATP_PVT.G_ORDER_LINE_ID col2,
11896                                 p_sup_atp_info_rec.scenario_id col3,
11897                                 l_null_num col4 ,
11898                                 l_null_num col5,
11899                                 p_sup_atp_info_rec.organization_id col6,
11900                                 l_null_num col7,
11901                                 l_null_num col8,
11902                                 p_sup_atp_info_rec.supplier_id col9,
11903                                 p_sup_atp_info_rec.supplier_site_id col10,
11904                                 l_null_num col11,
11905                                 l_null_num col12,
11906                                 l_null_num col13,
11907                                 l_null_num col14,
11908                                 l_null_char col15,
11909                                 l_uom_code col16,
11910                                 1 col17, -- demand
11911                                 p.order_type col18,
11912                                 l_null_char col19,
11913                                 p_sup_atp_info_rec.instance_id col20,
11914                                 l_null_num col21,
11915                                 TRANSACTION_ID col22,
11916                                 l_null_num col23,
11917                                 -- performance dsting rearrange signs to get rid of multiply times - 1
11918                                 (NVL(p.implement_quantity,0) - p.new_order_quantity) col24,
11919                                 --Fixing as a part of bug3709707 adding trunc so that 2 column are not seen in HP
11920                                 TRUNC(Decode(p_sup_atp_info_rec.sup_cap_type,
11921                                                 1, p.new_ship_date,
11922                                                 p.new_dock_date)) col25, -- For ship_rec_cal
11923                                 l_null_num col26,
11924                                 --bug 4493399: show transaction id for PO
11925                                 --p.order_number col27,
11926                                 DECODE(p.ORDER_TYPE, 5, to_char(p.TRANSACTION_ID), p.order_number) col27,
11927                                 l_null_num col28,
11928                                 l_null_num col29,
11929                                 l_sysdate,
11930                                 FND_GLOBAL.User_ID,
11931                                 l_sysdate,
11932                                 FND_GLOBAL.User_ID,
11933                                 FND_GLOBAL.User_ID
11934                                 -- Supplier Capacity (SCLT) Changes Begin
11935                          FROM   msc_supplies p
11936                          WHERE  (p.order_type IN (5, 2, 60)
11937                                  --include purchase orders based on profile option
11938                                  OR (MSC_ATP_REQ.G_PURCHASE_ORDER_PREFERENCE = MSC_ATP_REQ.G_PROMISE_DATE
11939                                      AND p.order_type = 1 AND p.promised_date IS NULL))
11940                          -- Supplier Capacity (SCLT) Accumulation Ignore Purchase Orders
11941                          -- WHERE  p.order_type IN (5, 1, 2)
11942                          AND    p.plan_id = p_sup_atp_info_rec.plan_id
11943                          AND    p.sr_instance_id = p_sup_atp_info_rec.instance_id
11944                          --AND    p.inventory_item_id = p_sup_atp_info_rec.inventory_item_id
11945                    -- 1214694      AND    p.organization_id = p_sup_atp_info_rec.organization_id
11946                          AND    p.supplier_id  = p_sup_atp_info_rec.supplier_id
11947                          AND    NVL(p.supplier_site_id, -1) = NVL(p_sup_atp_info_rec.supplier_site_id, -1)
11948                                 -- Exclude Cancelled Supplies 2460645
11949                          AND    NVL(P.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
11950                                  ---we only consider ATP inserted PO for MOdels.
11951                                  --Ignore Planning inserted POs for models as they would be tied to forecats
11952                          AND    ((p.inventory_item_id = l_inv_item_id and p.record_source=2) OR
11953                                     p.inventory_item_id in
11954                                            (select inventory_item_id from msc_system_items msi
11955                                             where  msi.sr_instance_id = p_sup_atp_info_rec.instance_id
11956                                             and    msi.plan_id = p_sup_atp_info_rec.plan_id
11957                                             and    msi.organization_id = p_sup_atp_info_rec.organization_id
11958                                             and    msi.base_item_id = l_inv_item_id))
11959                          AND    Decode(p_sup_atp_info_rec.sup_cap_type, 1, p.new_ship_date,p.new_dock_date) --4055719
11960                                        <= least(p_sup_atp_info_rec.last_cap_date,l_cutoff_date)
11961                     );
11962                 ELSE
11963             	   INSERT INTO msc_atp_sd_details_temp (
11964             		   ATP_level,
11965             		   Order_line_id,
11966             		   Scenario_Id,
11967             		   Inventory_Item_Id,
11968             		   Request_Item_Id,
11969             		   Organization_Id,
11970             		   Department_Id,
11971             		   Resource_Id,
11972             		   Supplier_Id,
11973             		   Supplier_Site_Id,
11974             		   From_Organization_Id,
11975             		   From_Location_Id,
11976             		   To_Organization_Id,
11977             		   To_Location_Id,
11978             		   Ship_Method,
11979             		   UOM_code,
11980             		   Supply_Demand_Type,
11981             		   Supply_Demand_Source_Type,
11982             		   Supply_Demand_Source_Type_Name,
11983             		   Identifier1,
11984             		   Identifier2,
11985             		   Identifier3,
11986             		   Identifier4,
11987             		   Supply_Demand_Quantity,
11988             		   Supply_Demand_Date,
11989             		   Disposition_Type,
11990             		   Disposition_Name,
11991             		   Pegging_Id,
11992             		   End_Pegging_Id,
11993             		   creation_date,
11994             		   created_by,
11995             		   last_update_date,
11996             		   last_updated_by,
11997             		   last_update_login
11998             	   )
11999 
12000                          (SELECT
12001                                p_sup_atp_info_rec.level col1,
12002                                MSC_ATP_PVT.G_ORDER_LINE_ID col2,
12003                                p_sup_atp_info_rec.scenario_id col3,
12004                                l_null_num col4 ,
12005                                l_null_num col5,
12006                                p_sup_atp_info_rec.organization_id col6,
12007                                l_null_num col7,
12008                                l_null_num col8,
12009                                p_sup_atp_info_rec.supplier_id col9,
12010                                p_sup_atp_info_rec.supplier_site_id col10,
12011                                l_null_num col11,
12012                                l_null_num col12,
12013                                l_null_num col13,
12014                                l_null_num col14,
12015                                l_null_char col15,
12016                                l_uom_code col16,
12017                                2 col17, -- supply
12018                                l_null_num col18,
12019                                l_null_char col19,
12020                                p_sup_atp_info_rec.instance_id col20,
12021                                l_null_num col21,
12022                                l_null_num col22,
12023                                l_null_num col23,
12024                                s.capacity*(1+ DECODE(l_tolerence_defined, 0, 0,
12025                                                   NVL(MSC_ATP_FUNC.get_tolerance_percentage(
12026                                                   p_sup_atp_info_rec.instance_id,
12027                                                   p_sup_atp_info_rec.plan_id,
12028                                                   p_sup_atp_info_rec.inventory_item_id,
12029                                                   p_sup_atp_info_rec.organization_id,
12030                                                   p_sup_atp_info_rec.supplier_id,
12031                                                   p_sup_atp_info_rec.supplier_site_id,
12032                                                   -- ship_rec_cal
12033                                                   c.seq_num - p_sup_atp_info_rec.sysdate_seq_num),0))) col24,
12034                                C.CALENDAR_DATE col25,
12035                                l_null_num col26,
12036                                l_null_char col27,
12037                                l_null_num col28,
12038                                l_null_num col29,
12039             		   l_sysdate,
12040             		   FND_GLOBAL.User_ID,
12041             		   l_sysdate,
12042             		   FND_GLOBAL.User_ID,
12043             		   FND_GLOBAL.User_ID
12044                      FROM   msc_calendar_dates c,
12045                             msc_supplier_capacities s
12046                      WHERE  s.inventory_item_id = l_inv_item_id
12047                      AND    s.sr_instance_id = p_sup_atp_info_rec.instance_id
12048                      AND    s.plan_id = p_sup_atp_info_rec.plan_id
12049                      AND    s.organization_id = p_sup_atp_info_rec.organization_id
12050                      AND    s.supplier_id = p_sup_atp_info_rec.supplier_id
12051                      AND    NVL(s.supplier_site_id, -1) = NVL(p_sup_atp_info_rec.supplier_site_id, -1)
12052                      AND    c.calendar_date BETWEEN trunc(s.from_date)
12053                                             --AND NVL(s.to_date,l_cutoff_date)
12054                                             AND trunc(NVL(s.to_date,least(p_sup_atp_info_rec.last_cap_date,l_cutoff_date))) --4055719
12055                      AND    (c.seq_num IS NOT NULL OR p_sup_atp_info_rec.manufacturing_cal_code  = MSC_CALENDAR.FOC)
12056                      AND    c.calendar_code = l_calendar_code
12057                      AND    c.exception_set_id = l_calendar_exception_set_id
12058                      AND    c.sr_instance_id = s.sr_instance_id  -- Changed from l_instance_id ?
12059                      AND    c.calendar_date >= p_sup_atp_info_rec.sup_cap_cum_date
12060                      -- Supplier Capacity (SCLT) Accumulation starts from this date.
12061                      -- AND    c.calendar_date >= l_plan_start_date -- bug 1169467
12062                      UNION ALL
12063                      SELECT
12064                                p_sup_atp_info_rec.level col1,
12065                                MSC_ATP_PVT.G_ORDER_LINE_ID col2,
12066                                p_sup_atp_info_rec.scenario_id col3,
12067                                l_null_num col4 ,
12068                                l_null_num col5,
12069                                p_sup_atp_info_rec.organization_id col6,
12070                                l_null_num col7,
12071                                l_null_num col8,
12072                                p_sup_atp_info_rec.supplier_id col9,
12073                                p_sup_atp_info_rec.supplier_site_id col10,
12074                                l_null_num col11,
12075                                l_null_num col12,
12076                                l_null_num col13,
12077                                l_null_num col14,
12078                                l_null_char col15,
12079                                l_uom_code col16,
12080                                1 col17, -- demand
12081                                p.order_type col18,
12082                                l_null_char col19,
12083                                p_sup_atp_info_rec.instance_id col20,
12084                                l_null_num col21,
12085                                TRANSACTION_ID col22,
12086                                l_null_num col23,
12087             		   -- performance dsting rearrange signs to get rid of multiply times - 1
12088                                (NVL(p.implement_quantity,0) - p.new_order_quantity) col24,
12089                                --Fixing as a part of bug3709707 adding trunc so that 2 column are not seen in HP
12090                                TRUNC(Decode(p_sup_atp_info_rec.sup_cap_type,
12091                                                 1, p.new_ship_date,
12092                                                 p.new_dock_date)) col25, -- For ship_rec_cal
12093                                l_null_num col26,
12094                                --bug 4493399: show transaction id for PO
12095                                --p.order_number col27,
12096                                DECODE(p.ORDER_TYPE, 5, to_char(p.TRANSACTION_ID), p.order_number) col27,
12097                                l_null_num col28,
12098                                l_null_num col29,
12099             		   l_sysdate,
12100             		   FND_GLOBAL.User_ID,
12101             		   l_sysdate,
12102             		   FND_GLOBAL.User_ID,
12103             		   FND_GLOBAL.User_ID
12104                      -- Supplier Capacity (SCLT) Changes Begin
12105                      FROM   msc_supplies p
12106                      WHERE  (p.order_type IN (5, 2, 60)
12107                              --include purchase orders based on profile option
12108                              OR (MSC_ATP_REQ.G_PURCHASE_ORDER_PREFERENCE = MSC_ATP_REQ.G_PROMISE_DATE
12109                                  AND p.order_type = 1 AND p.promised_date IS NULL))
12110                      -- Supplier Capacity (SCLT) Accumulation Ignore Purchase Orders
12111                      -- WHERE  p.order_type IN (5, 1, 2)
12112                      AND    p.plan_id = p_sup_atp_info_rec.plan_id
12113                      AND    p.sr_instance_id = p_sup_atp_info_rec.instance_id
12114                      AND    p.inventory_item_id = p_sup_atp_info_rec.inventory_item_id
12115                -- 1214694      AND    p.organization_id = p_sup_atp_info_rec.organization_id
12116                      AND    p.supplier_id  = p_sup_atp_info_rec.supplier_id
12117                      AND    NVL(p.supplier_site_id, -1) = NVL(p_sup_atp_info_rec.supplier_site_id, -1)
12118                      AND    trunc(Decode(p_sup_atp_info_rec.sup_cap_type, 1, p.new_ship_date,p.new_dock_date)) --4055719 --4135752
12119                             <= trunc(least(p_sup_atp_info_rec.last_cap_date,l_cutoff_date)) --4135752
12120                             -- Exclude Cancelled Supplies 2460645
12121                      AND    NVL(P.DISPOSITION_STATUS_TYPE, 1) <> 2);
12122                      -- Supplier Capacity (SCLT) Changes End
12123 
12124                     -- dsting: removed 'order by col 25'
12125                 END IF; -- IF l_check_cap_model_flag = 1 THEN
12126 
12127                 -- for period ATP
12128                 IF PG_DEBUG in ('Y', 'C') THEN
12129                     msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'after inserting into msc_atp_sd_details_temp');
12130                     msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'rows processed: ' || SQL%ROWCOUNT);
12131                 END IF;
12132 
12133                 MSC_ATP_PROC.get_period_data_from_SD_temp(x_atp_period);
12134 
12135                 x_atp_period.Cumulative_Quantity := x_atp_period.Period_Quantity;
12136                 IF PG_DEBUG in ('Y', 'C') THEN
12137                     msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'before atp_consume');
12138                 END IF;
12139                 -- do the accumulation
12140                 -- 1487804
12141                 -- atp_consume(x_atp_period.Cumulative_Quantity, m);
12142                 MSC_ATP_PROC.atp_consume(x_atp_period.Cumulative_Quantity,
12143                     x_atp_period.Cumulative_Quantity.COUNT);
12144 
12145                 IF PG_DEBUG in ('Y', 'C') THEN
12146                     msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'after atp_consume');
12147                 END IF;
12148 
12149                 /* Cum drop issue changes begin*/
12150                 MSC_AATP_PROC.Atp_Remove_Negatives(x_atp_period.Cumulative_Quantity, l_return_status);
12151                 IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
12152                         IF PG_DEBUG in ('Y', 'C') THEN
12153                                 msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'Error occured in procedure Atp_Remove_Negatives');
12154                         END IF;
12155                         RAISE FND_API.G_EXC_ERROR;
12156                 END IF;
12157                 /* Cum drop issue changes end*/
12158 
12159                 l_atp_period_tab := x_atp_period.Period_Start_Date;
12160                 l_atp_qty_tab := x_atp_period.Cumulative_Quantity;
12161 
12162 
12163             END IF; -- p_sup_atp_info_rec.insert_flag <> 0
12164 
12165             --4055719
12166 
12167                 l_atp_period_tab.EXTEND;
12168                 l_atp_qty_tab.EXTEND;
12169                 i:= l_atp_period_tab.COUNT;
12170                 IF PG_DEBUG in ('Y', 'C') THEN
12171                    msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'l_plan_start_date := ' || l_plan_start_date);
12172                 END IF;
12173 
12174                 -- ship_rec_cal
12175                 l_atp_period_tab(i) := l_last_cap_next_date;
12176                 l_atp_qty_tab(i) := MSC_ATP_PVT.INFINITE_NUMBER;
12177 
12178                 IF PG_DEBUG in ('Y', 'C') THEN
12179                    msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'l_plan_start_date1 := ' || l_plan_start_date);
12180                 END IF;
12181 
12182                 IF (NVL(p_sup_atp_info_rec.insert_flag, 0) <> 0) THEN
12183 
12184                    -- add one more entry to indicate infinite time fence date
12185                    -- and quantity.
12186                    MSC_SATP_FUNC.Extend_Atp_Period(x_atp_period, x_return_status);
12187                    i:= x_atp_period.level.COUNT;
12188 
12189                    x_atp_period.Level(i) := p_sup_atp_info_rec.level;
12190                    x_atp_period.Identifier(i) := p_sup_atp_info_rec.identifier;
12191                    x_atp_period.Scenario_Id(i) := p_sup_atp_info_rec.scenario_id;
12192                    x_atp_period.Pegging_Id(i) := NULL;
12193                    x_atp_period.End_Pegging_Id(i) := NULL;
12194 
12195                    x_atp_period.Supplier_Id(i) := p_sup_atp_info_rec.supplier_id;
12196                    x_atp_period.Supplier_Site_Id(i) := p_sup_atp_info_rec.supplier_site_id;
12197                    x_atp_period.Organization_id(i) := p_sup_atp_info_rec.organization_id;
12198 
12199                    -- ship_rec_cal
12200                    x_atp_period.Period_Start_Date(i) := l_last_cap_next_date;
12201 
12202                    x_atp_period.Total_Supply_Quantity(i) := MSC_ATP_PVT.INFINITE_NUMBER;
12203                    x_atp_period.Total_Demand_Quantity(i) := 0;
12204                    x_atp_period.Period_Quantity(i) := MSC_ATP_PVT.INFINITE_NUMBER;
12205                    x_atp_period.Cumulative_Quantity(i) := MSC_ATP_PVT.INFINITE_NUMBER;
12206 
12207                 END IF;
12208 
12209             --=======================================================================================================
12210             -- ship_rec_cal changes end
12211             --=======================================================================================================
12212 
12213         ELSE --  (G_ALLOCATED_ATP = 'N')
12214             -- we are using allocated atp
12215             MSC_AATP_PVT.Supplier_Alloc_Cum_Atp(p_sup_atp_info_rec,
12216                                MSC_ATP_PVT.G_ORDER_LINE_ID,
12217                                l_requested_date,
12218                                l_atp_info,
12219                                x_atp_period,
12220                                x_atp_supply_demand);
12221 
12222             --4055719, this piece of code is moved from MSC_AATP_PVT.Supplier_Alloc_Cum_Atp to here.
12223             -- also l_last_cap_next_date is added instead of l_infinite_time_fence_date.
12224             IF l_last_cap_next_date IS NOT NULL THEN
12225                -- add one more entry to indicate infinite time fence date
12226                -- and quantity.
12227                l_atp_info.atp_qty.EXTEND;
12228                l_atp_info.atp_period.EXTEND;
12229                --- bug 1657855, remove support for min alloc
12230                l_atp_info.limit_qty.EXTEND;
12231 
12232                i := l_atp_info.atp_qty.COUNT;
12233                l_atp_info.atp_period(i) := l_last_cap_next_date;
12234                l_atp_info.atp_qty(i) := MSC_ATP_PVT.INFINITE_NUMBER;
12235                ---x_atp_info.limit_qty(i) := MSC_ATP_PVT.INFINITE_NUMBER;
12236 
12237 
12238               IF NVL(p_sup_atp_info_rec.insert_flag, 0) <> 0 THEN
12239                  -- add one more entry to indicate infinite time fence date
12240                  -- and quantity.
12241 
12242                  x_atp_period.Cumulative_Quantity := l_atp_info.atp_qty;
12243 
12244                  j := x_atp_period.Level.COUNT;
12245                  MSC_SATP_FUNC.Extend_Atp_Period(x_atp_period, l_return_status);
12246                  j := j + 1;
12247                  IF j > 1 THEN
12248                     --x_atp_period.Period_End_Date(j-1) := l_infinite_time_fence_date -1; --4055719
12249                     x_atp_period.Period_End_Date(j-1) := l_last_cap_next_date -1;
12250                     x_atp_period.Identifier1(j) := x_atp_period.Identifier1(j-1);
12251                     x_atp_period.Identifier2(j) := x_atp_period.Identifier2(j-1);
12252                  END IF;
12253 
12254                  x_atp_period.Level(j) := p_sup_atp_info_rec.level;
12255                  x_atp_period.Identifier(j) := MSC_ATP_PVT.G_ORDER_LINE_ID;
12256                  x_atp_period.Scenario_Id(j) := p_sup_atp_info_rec.scenario_id;
12257                  x_atp_period.Pegging_Id(j) := NULL;
12258                  x_atp_period.End_Pegging_Id(j) := NULL;
12259                  x_atp_period.Supplier_Id(j) := p_sup_atp_info_rec.supplier_id;
12260                  x_atp_period.Supplier_site_id(j) := p_sup_atp_info_rec.supplier_site_id;
12261                  x_atp_period.Organization_id(j) := p_sup_atp_info_rec.organization_id;
12262                  --x_atp_period.Period_Start_Date(j) := l_infinite_time_fence_date; --4055719
12263                  x_atp_period.Period_Start_Date(j) := l_last_cap_next_date;
12264                  x_atp_period.Total_Supply_Quantity(j) := MSC_ATP_PVT.INFINITE_NUMBER;
12265                  x_atp_period.Total_Demand_Quantity(j) := 0;
12266                  x_atp_period.Period_Quantity(j) := MSC_ATP_PVT.INFINITE_NUMBER;
12267                  x_atp_period.Cumulative_Quantity(j) := MSC_ATP_PVT.INFINITE_NUMBER;
12268               END IF;
12269             END IF;
12270 
12271             l_atp_period_tab := l_atp_info.atp_period;
12272             l_atp_qty_tab := l_atp_info.atp_qty;
12273 
12274         END IF; -- end of G_ALLOCATED_ATP
12275 
12276         --4055719 , commented this piece of code because of following reasons
12277         -- 1. This code will lead to adding of infinite date/qty record twice
12278         -- 2. Seperated code is added to allocated/unallocated cases (we cannot join the
12279         --    code because appending of records are slightly diff in both cases.
12280 
12281         /* Removed redundant code as l_last_cap_date does not anyway get used after this point
12282            Done with Enforce Pur LT changes
12283         --sup_cap chnages
12284         --first get the next working from the last day on which capacity is defined.
12285         IF PG_DEBUG in ('Y', 'C') THEN
12286             msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'l_last_cap_date := ' || l_last_cap_date);
12287         END IF;
12288         l_last_cap_date :=  MSC_CALENDAR.DATE_OFFSET(p_sup_atp_info_rec.organization_id,
12289                                                      p_sup_atp_info_rec.instance_id,
12290                                                      1,
12291                                                      l_last_cap_date,
12292                                                      1);
12293         IF PG_DEBUG in ('Y', 'C') THEN
12294            msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'l_last_cap_date after offset := ' || l_last_cap_date);
12295         END IF;
12296         */
12297 
12298         -- add one more entry to indicate sysdate
12299         -- and infinite quantity.
12300 
12301         --4055719
12302         /*
12303         l_atp_period_tab.EXTEND;
12304         l_atp_qty_tab.EXTEND;
12305         i:= l_atp_period_tab.COUNT;
12306         IF PG_DEBUG in ('Y', 'C') THEN
12307              msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'l_plan_start_date := ' || l_plan_start_date);
12308         END IF;
12309 
12310         -- ship_rec_cal
12311         l_atp_period_tab(i) := l_last_cap_next_date;
12312         l_atp_qty_tab(i) := MSC_ATP_PVT.INFINITE_NUMBER;
12313 
12314 
12315         IF (NVL(p_sup_atp_info_rec.insert_flag, 0) <> 0) THEN
12316 
12317            -- add one more entry to indicate infinite time fence date
12318            -- and quantity.
12319            MSC_SATP_FUNC.Extend_Atp_Period(x_atp_period, x_return_status);
12320            i:= x_atp_period.level.COUNT;
12321 
12322            x_atp_period.Level(i) := p_sup_atp_info_rec.level;
12323            x_atp_period.Identifier(i) := p_sup_atp_info_rec.identifier;
12324            x_atp_period.Scenario_Id(i) := p_sup_atp_info_rec.scenario_id;
12325            x_atp_period.Pegging_Id(i) := NULL;
12326            x_atp_period.End_Pegging_Id(i) := NULL;
12327 
12328            x_atp_period.Supplier_Id(i) := p_sup_atp_info_rec.supplier_id;
12329            x_atp_period.Supplier_Site_Id(i) := p_sup_atp_info_rec.supplier_site_id;
12330            x_atp_period.Organization_id(i) := p_sup_atp_info_rec.organization_id;
12331 
12332            -- ship_rec_cal
12333            x_atp_period.Period_Start_Date(i) := l_last_cap_next_date;
12334 
12335            x_atp_period.Total_Supply_Quantity(i) := MSC_ATP_PVT.INFINITE_NUMBER;
12336            x_atp_period.Total_Demand_Quantity(i) := 0;
12337            x_atp_period.Period_Quantity(i) := MSC_ATP_PVT.INFINITE_NUMBER;
12338            x_atp_period.Cumulative_Quantity(i) := MSC_ATP_PVT.INFINITE_NUMBER;
12339 
12340         END IF; */
12341 
12342    END IF;  -- l_capacity_defined = 0
12343 
12344     i := l_atp_period_tab.COUNT;
12345 
12346     IF i = 0 THEN
12347       -- need to add error message
12348         IF PG_DEBUG in ('Y', 'C') THEN
12349            msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'No rows in cursor!!!');
12350         END IF;
12351         RAISE NO_DATA_FOUND;
12352     END IF;
12353 
12354     FOR i in 1..l_atp_period_tab.COUNT LOOP
12355         IF PG_DEBUG in ('Y', 'C') THEN
12356            msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'Date '||l_atp_period_tab(i)||' Qty '||
12357                                   l_atp_qty_tab(i));
12358         END IF;
12359     END LOOP;
12360 
12361 
12362     --bug 2341075: Capacity before sysdate should not be considered
12363     --- we find out how much is available before sydate and reduce it from cumulative qty
12364 
12365     -- Rewrite the l_qty_before_sysdate logic as part of ship_rec_cal changes for better performance
12366     IF (l_atp_period_tab(1) >= l_sysdate OR l_atp_period_tab(l_atp_period_tab.COUNT) <= l_sysdate) THEN
12367 		l_qty_before_sysdate := 0;
12368     ELSE
12369 		FOR i in 1..l_atp_period_tab.COUNT LOOP
12370 			-- For loop will never reach COUNT as that case has already been handled above.
12371 			IF (l_atp_period_tab(i) <  l_sysdate AND l_atp_period_tab(i+1) >= l_sysdate) THEN
12372 				l_qty_before_sysdate := l_atp_qty_tab(i);
12373 				EXIT;
12374 			END IF;
12375 		END LOOP;
12376     END IF;
12377     l_qty_before_sysdate := GREATEST(l_qty_before_sysdate, 0);
12378 
12379     IF PG_DEBUG in ('Y', 'C') THEN
12380        msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'l_qty_before_sysdate := ' || l_qty_before_sysdate);
12381     END IF;
12382 
12383 
12384 	-- we use this l_atp_requested_date to do the search
12385     l_atp_requested_date := GREATEST(l_requested_date, l_sysdate); -- Change for ship_rec_cal
12386 
12387     IF PG_DEBUG in ('Y', 'C') THEN
12388        msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'l_atp_requested_date := ' || l_atp_requested_date);
12389     END IF;
12390 
12391     IF (l_atp_requested_date < l_atp_period_tab(1)) THEN
12392             -- let say the first period is on Day5 but your
12393             -- request in on Day2.  for bug 948863
12394             p_sup_atp_info_rec.requested_date_quantity := 0;
12395             FOR k IN 1..l_atp_period_tab.COUNT LOOP
12396                 IF (l_atp_qty_tab(k) >= p_sup_atp_info_rec.quantity_ordered)
12397                     AND l_atp_period_tab(k) >= MSC_ATP_PVT.G_PTF_DATE THEN      -- Bug 3782472 - Added PTF check
12398                     p_sup_atp_info_rec.atp_date_quantity_this_level := l_atp_qty_tab(k);
12399                     p_sup_atp_info_rec.atp_date_this_level := l_atp_period_tab(k);
12400                     EXIT;
12401                 ELSIF (l_atp_qty_tab(k) >= p_sup_atp_info_rec.quantity_ordered)
12402                     AND (l_atp_period_tab.COUNT = k
12403                         -- Bug 3862224, handled the case where ptf_date has some supply/demand activity, removed equality check.
12404                         --OR l_atp_period_tab(k+1) >= MSC_ATP_PVT.G_PTF_DATE) THEN      -- Bug 3782472 - Added PTF check
12405                         OR l_atp_period_tab(k+1) > MSC_ATP_PVT.G_PTF_DATE) THEN  -- Bug 3782472 - Added PTF check
12406                     p_sup_atp_info_rec.atp_date_quantity_this_level := l_atp_qty_tab(k);
12407                     p_sup_atp_info_rec.atp_date_this_level := MSC_ATP_PVT.G_PTF_DATE;
12408                     EXIT;
12409                 END IF;
12410             END LOOP; -- end of k loop
12411     ELSE
12412 
12413 
12414         FOR j IN 1..l_atp_period_tab.COUNT LOOP
12415 
12416             -- Please state reason for the else condition here
12417             -- the reason that we need this else condition is the following
12418             -- let say the last record in the bucket is Day5, and request
12419             -- date is Day10.  So the bucket that the the request date is
12420             -- falling into is Day5. So we should use Day5's quantity
12421             -- as the quantity for Day10. By setting l_next_period this way,
12422             -- we make sure we are using the right bucket to get
12423             -- request date quantuty.
12424 
12425             IF j < l_atp_period_tab.LAST THEN
12426                 l_next_period := l_atp_period_tab(j+1);
12427             ELSE
12428                 l_next_period := l_atp_requested_date + 1;
12429             END IF;
12430 
12431             IF ((l_atp_period_tab(j) <= l_atp_requested_date) and
12432                         (l_next_period > l_atp_requested_date)) THEN
12433 
12434                 IF (l_pre_process_date IS NOT NULL and l_pre_process_date < l_sysdate)
12435                    or (l_pre_process_date IS NULL and
12436                        l_requested_date < l_atp_requested_date) THEN
12437                        -- Bug 3828469 - Removed the regression introduced in 3782472
12438                        -- l_requested_date < GREATEST(MSC_ATP_PVT.G_PTF_DATE,l_atp_requested_date)) THEN -- Bug 3782472 - Added PTF check
12439                     p_sup_atp_info_rec.requested_date_quantity := 0;
12440 
12441                     IF PG_DEBUG in ('Y', 'C') THEN
12442                        msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'inside p_sup_atp_info_rec.requested_date_quantity 0 = '||
12443                       p_sup_atp_info_rec.requested_date_quantity);
12444                     END IF;
12445                 ELSE
12446                     ---bug 2341075: availability should not include what is available before sysdate
12447                     p_sup_atp_info_rec.requested_date_quantity := l_atp_qty_tab(j) - l_qty_before_sysdate;
12448                 END IF;
12449 
12450 
12451                 IF PG_DEBUG in ('Y', 'C') THEN
12452                    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));
12453                 END IF;
12454                 --  now find the atp_date_quantity and atp_date at this level
12455                 ---bug 2341075: Cum Qty should not include cum qty before sysdate
12456                 IF (l_atp_qty_tab(j) - l_qty_before_sysdate) >= p_sup_atp_info_rec.quantity_ordered
12457                     AND l_atp_requested_date >= MSC_ATP_PVT.G_PTF_DATE THEN      -- Bug 3782472 - Added PTF check
12458 
12459                     p_sup_atp_info_rec.atp_date_quantity_this_level := l_atp_qty_tab(j) - l_qty_before_sysdate;
12460                     p_sup_atp_info_rec.atp_date_this_level := l_atp_requested_date;
12461 
12462                     IF PG_DEBUG in ('Y', 'C') THEN
12463                        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));
12464                        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));
12465                     END IF;
12466 
12467                 ELSE
12468                     IF j = l_atp_period_tab.COUNT THEN
12469                         p_sup_atp_info_rec.atp_date_quantity_this_level := NULL;
12470                         p_sup_atp_info_rec.atp_date_this_level := NULL;
12471 
12472                     ELSE
12473                         FOR k IN j+1..l_atp_period_tab.COUNT LOOP
12474                             ---bug 2341075: exclude qty before sysdate
12475                             IF ((l_atp_qty_tab(k)- l_qty_before_sysdate) >= p_sup_atp_info_rec.quantity_ordered)
12476                                 AND l_atp_period_tab(k) >= MSC_ATP_PVT.G_PTF_DATE THEN      -- Bug 3782472 - Added PTF check
12477                                 p_sup_atp_info_rec.atp_date_quantity_this_level := l_atp_qty_tab(k) - l_qty_before_sysdate;
12478                                 p_sup_atp_info_rec.atp_date_this_level := l_atp_period_tab(k);
12479                                 EXIT;
12480                             ELSIF ((l_atp_qty_tab(k)- l_qty_before_sysdate) >= p_sup_atp_info_rec.quantity_ordered)
12481                                 AND (l_atp_period_tab.COUNT = k
12482                                         -- Bug 3862224, handled the case where ptf_date has some supply/demand activity, removed equality check
12483                                         --OR l_atp_period_tab(k+1) >= MSC_ATP_PVT.G_PTF_DATE) THEN
12484                                         OR l_atp_period_tab(k+1) > MSC_ATP_PVT.G_PTF_DATE) THEN
12485                                 p_sup_atp_info_rec.atp_date_quantity_this_level := l_atp_qty_tab(k) - l_qty_before_sysdate;
12486                                 p_sup_atp_info_rec.atp_date_this_level := MSC_ATP_PVT.G_PTF_DATE;
12487                                 EXIT;
12488                             END IF;
12489                         END LOOP; -- end of k loop
12490                     END IF; -- end if j = l_atp_period_tab.COUNT
12491                 END IF; -- end if  l_atp_qty_tab(j) >=p_sup_atp_info_rec.quantity_ordered
12492                 EXIT;
12493             END IF; -- end if we find the bucket
12494         END LOOP; -- end j loop
12495     END IF;
12496 
12497     IF PG_DEBUG in ('Y', 'C') THEN
12498        msc_sch_wb.atp_debug('Get_Supplier_Atp_Info: ' || 'in supplier, count = '||x_atp_supply_demand.supplier_id.count);
12499        msc_sch_wb.atp_debug('***** End Get_Supplier_Atp_Info Procedure *****');
12500     END IF;
12501 
12502 EXCEPTION
12503 
12504     WHEN NO_DATA_FOUND THEN
12505         p_sup_atp_info_rec.requested_date_quantity := 0.0;
12506         x_return_status := FND_API.G_RET_STS_ERROR;
12507 END Get_Supplier_Atp_Info;
12508 
12509 
12510 PROCEDURE Get_Transport_Cap_Atp_Info (
12511   p_plan_id				IN    NUMBER,
12512   p_from_organization_id                IN    NUMBER,
12513   p_to_organization_id                  IN    NUMBER,
12514   p_ship_method                         IN    VARCHAR2,
12515   p_inventory_item_id                   IN    NUMBER,
12516   p_source_org_instance_id		IN    NUMBER,
12517   p_dest_org_instance_id		IN    NUMBER,
12518   p_requested_date                      IN    DATE,
12519   p_quantity_ordered                    IN    NUMBER,
12520   p_insert_flag                         IN    NUMBER,
12521   p_level				IN    NUMBER,
12522   p_scenario_id				IN    NUMBER,
12523   p_identifier				IN    NUMBER,
12524   p_parent_pegging_id			IN    NUMBER,
12525   x_requested_date_quantity             OUT   NoCopy NUMBER,
12526   x_atp_date_this_level                 OUT   NoCopy DATE,
12527   x_atp_date_quantity_this_level        OUT   NoCopy NUMBER,
12528   x_atp_period                          OUT   NoCopy MRP_ATP_PUB.ATP_Period_Typ,
12529   x_atp_supply_demand                   OUT   NoCopy MRP_ATP_PUB.ATP_Supply_Demand_Typ,
12530   x_return_status                       OUT   NoCopy VARCHAR2
12531 )
12532 IS
12533 
12534 i                               PLS_INTEGER := 1;
12535 l_requested_date                DATE;
12536 l_unit_weight			NUMBER;
12537 l_unit_volume			NUMBER;
12538 l_available_quantity		NUMBER;
12539 l_atp_period_tab                MRP_ATP_PUB.date_arr:=MRP_ATP_PUB.date_arr();
12540 l_atp_qty_tab                   MRP_ATP_PUB.number_arr := MRP_ATP_PUB.number_arr();
12541 l_atp_qty_tab2                  MRP_ATP_PUB.number_arr := MRP_ATP_PUB.number_arr();
12542 l_atp_period                    MRP_ATP_PUB.ATP_Period_Typ;
12543 l_atp_period2                    MRP_ATP_PUB.ATP_Period_Typ;
12544 l_atp_supply_demand		MRP_ATP_PUB.ATP_Supply_Demand_Typ;
12545 l_atp_supply_demand2             MRP_ATP_PUB.ATP_Supply_Demand_Typ;
12546 l_null_atp_period		MRP_ATP_PUB.ATP_Period_Typ;
12547 l_null_atp_supply_demand	MRP_ATP_PUB.ATP_Supply_Demand_Typ;
12548 m				NUMBER;
12549 k				NUMBER;
12550 l_item_weight_qty		NUMBER;
12551 l_item_volume_qty		NUMBER;
12552 l_atp_date_weight		NUMBER;
12553 l_atp_date_volume		NUMBER;
12554 l_pegging_rec                   mrp_atp_details_temp%ROWTYPE;
12555 l_pegging_id			NUMBER;
12556 l_vol_demand_pegging_id	        NUMBER;
12557 l_wt_demand_pegging_id 		NUMBER;
12558 --INFINITE_NUMBER                 CONSTANT NUMBER := 1.0e+10;
12559 
12560 BEGIN
12561 
12562   IF PG_DEBUG in ('Y', 'C') THEN
12563      msc_sch_wb.atp_debug('Get_Transport_Cap_Atp_Info: ' || 'inside get_TRANSPORT_info ');
12564   END IF;
12565 
12566   l_null_atp_period := x_atp_period;
12567   l_null_atp_supply_demand := x_atp_supply_demand;
12568 
12569   IF PG_DEBUG in ('Y', 'C') THEN
12570      msc_sch_wb.atp_debug('Get_Transport_Cap_Atp_Info: ' || 'p_insert_flag = '||p_insert_flag);
12571   END IF;
12572 
12573   -- initialize API return status to success
12574   x_return_status := FND_API.G_RET_STS_SUCCESS;
12575 
12576   l_requested_date := trunc(p_requested_date);
12577 
12578   -- Initialize the record with null
12579 
12580   l_atp_period := l_null_atp_period;
12581   l_atp_supply_demand := l_null_atp_supply_demand;
12582   l_atp_period2 := l_null_atp_period;
12583   l_atp_supply_demand2 := l_null_atp_supply_demand;
12584 
12585   -- Planning will be populating the 'weight_capacitiy_used' and
12586   -- 'volume_capacity_used' fields in msc_supplies. For now, we need
12587   -- to multiply the quantitiy by the unit volume and unit capacity
12588   -- to obtain capacity used.
12589 
12590   SELECT NVL(unit_weight,1), NVL(unit_volume,1)
12591   INTO   l_unit_weight, l_unit_volume
12592   FROM   msc_system_items
12593   WHERE  plan_id = p_plan_id
12594   AND    organization_id = p_to_organization_id
12595   AND    inventory_item_id = p_inventory_item_id
12596   AND    sr_instance_id = p_dest_org_instance_id;
12597 
12598   IF PG_DEBUG in ('Y', 'C') THEN
12599      msc_sch_wb.atp_debug('Get_Transport_Cap_Atp_Info: ' || 'the unit weight is: '||to_char(l_unit_weight));
12600      msc_sch_wb.atp_debug('Get_Transport_Cap_Atp_Info: ' || 'the unit volume is: '||to_char(l_unit_volume));
12601   END IF;
12602 
12603   SELECT  l_date, SUM(weight), SUM(volume)
12604   BULK COLLECT INTO
12605       l_atp_period_tab,
12606       l_atp_qty_tab,
12607       l_atp_qty_tab2
12608   FROM (
12609     SELECT c.calendar_date l_date,
12610              s.weight_capacity weight,
12611 	     s.volume_capacity volume
12612       FROM   msc_calendar_dates c,
12613              msc_interorg_ship_methods s,
12614              msc_trading_partners tp,
12615 	     msc_plans p
12616       WHERE  s.plan_id = p_plan_id
12617       AND    s.from_organization_id = p_from_organization_id
12618       AND    s.to_organization_id = p_to_organization_id
12619       AND    s.ship_method = p_ship_method
12620       AND    s.sr_instance_id = p_source_org_instance_id
12621       AND    s.sr_instance_id2 = p_dest_org_instance_id
12622       AND    s.from_organization_id = tp.sr_tp_id
12623       AND    tp.sr_instance_id = NVL(s.sr_instance_id, s.sr_instance_id2)
12624       AND    c.calendar_date BETWEEN trunc(SYSDATE) and trunc(p.curr_cutoff_date)   -- to_date changed to trunc to avoid GSCC error
12625       AND    c.calendar_code = tp.calendar_code
12626       AND    c.exception_set_id = tp.calendar_exception_set_id
12627       AND    p.plan_id = p_plan_id
12628       UNION ALL
12629       SELECT sup.new_schedule_date l_date,
12630              -1*(sup.new_order_quantity)*l_unit_weight weight,
12631 	     -1*(sup.new_order_quantity)*l_unit_volume  volume
12632       FROM   msc_supplies sup
12633       WHERE  sup.plan_id = p_plan_id
12634       AND    sup.organization_id = p_to_organization_id
12635       AND    sup.sr_instance_id = p_dest_org_instance_id
12636       AND    sup.source_organization_id is not null
12637       AND    sup.source_organization_id = p_from_organization_id
12638       AND    sup.source_sr_instance_id = p_source_org_instance_id
12639       AND    sup.ship_method = p_ship_method
12640       AND    sup.inventory_item_id = p_inventory_item_id
12641       AND    sup.transaction_id <>     (SELECT identifier3
12642 					FROM   mrp_atp_details_temp
12643 					WHERE  record_type = 3
12644 					AND    pegging_id = p_parent_pegging_id
12645                                         AND    session_id = MSC_ATP_PVT.G_SESSION_ID
12646 				       ))
12647       GROUP BY l_date
12648       ORDER BY l_date;
12649 
12650       i := l_atp_period_tab.COUNT;
12651 
12652       IF i = 0 THEN
12653         -- need to add error message
12654         IF PG_DEBUG in ('Y', 'C') THEN
12655            msc_sch_wb.atp_debug('Get_Transport_Cap_Atp_Info: ' || 'No rows in cursor!!!');
12656         END IF;
12657         RAISE NO_DATA_FOUND;
12658       END IF;
12659 
12660       -- do accumulation for transportation capacity
12661       MSC_ATP_PROC.atp_consume(l_atp_qty_tab, i);
12662       MSC_ATP_PROC.atp_consume(l_atp_qty_tab2, i);
12663 
12664       IF PG_DEBUG in ('Y', 'C') THEN
12665          msc_sch_wb.atp_debug('Get_Transport_Cap_Atp_Info: ' || 'just before printing values');
12666       END IF;
12667 
12668     FOR k IN 1..i LOOP
12669       IF PG_DEBUG in ('Y', 'C') THEN
12670          msc_sch_wb.atp_debug('Get_Transport_Cap_Atp_Info: ' || 'date is: '||to_char(l_atp_period_tab(k)));
12671          msc_sch_wb.atp_debug('Get_Transport_Cap_Atp_Info: ' || 'weight is: '||to_char(l_atp_qty_tab(k)));
12672          msc_sch_wb.atp_debug('Get_Transport_Cap_Atp_Info: ' || 'volume is: '||to_char(l_atp_qty_tab2(k)));
12673       END IF;
12674 
12675     END LOOP;
12676 
12677     -- find the requested date atp quantity
12678 
12679     FOR j IN 1..i LOOP
12680 
12681         IF ((l_atp_period_tab(j) <= l_requested_date) and
12682             (l_atp_period_tab(j+1) > l_requested_date)) THEN
12683 
12684             -- Convert volume and weight capacity to units
12685 
12686             l_item_weight_qty := l_atp_qty_tab(j)/l_unit_weight;
12687             l_item_volume_qty := l_atp_qty_tab2(j)/l_unit_volume;
12688 
12689             -- Find out volume or weight capacity is constraining to
12690             -- determine quantity for request date
12691 
12692             IF l_item_volume_qty > l_item_weight_qty THEN
12693               x_requested_date_quantity := l_item_weight_qty;
12694             ELSE
12695               x_requested_date_quantity := l_item_volume_qty;
12696             END IF;
12697 
12698             IF PG_DEBUG in ('Y', 'C') THEN
12699                msc_sch_wb.atp_debug('Get_Transport_Cap_Atp_Info: ' || 'the date is: '||to_char(l_atp_period_tab(j)));
12700                msc_sch_wb.atp_debug('Get_Transport_Cap_Atp_Info: ' || 'the quantity is: '||to_char(x_requested_date_quantity));
12701             END IF;
12702 
12703             --  now find the atp_date_quantity and atp_date at this level
12704             IF x_requested_date_quantity >= p_quantity_ordered THEN
12705                 x_atp_date_quantity_this_level := x_requested_date_quantity;
12706                 x_atp_date_this_level := p_requested_date;
12707 		l_atp_date_weight := l_atp_qty_tab(j);
12708 		l_atp_date_volume := l_atp_qty_tab2(j);
12709 
12710                 IF PG_DEBUG in ('Y', 'C') THEN
12711                    msc_sch_wb.atp_debug('Get_Transport_Cap_Atp_Info: ' || 'quantity is enough.');
12712                 END IF;
12713 
12714    		k := j;
12715             ELSE
12716 
12717                 IF PG_DEBUG in ('Y', 'C') THEN
12718                    msc_sch_wb.atp_debug('Get_Transport_Cap_Atp_Info: ' || 'quantity is not enough.');
12719                 END IF;
12720 
12721                 FOR k IN j+1..i LOOP
12722 		  -- Convert volume and weight capacity in units
12723 
12724                   l_item_weight_qty := l_atp_qty_tab(k)/l_unit_weight;
12725                   l_item_volume_qty := l_atp_qty_tab2(k)/l_unit_volume;
12726 
12727                     IF l_item_volume_qty > l_item_weight_qty THEN
12728                       l_available_quantity := l_item_weight_qty;
12729                     ELSE
12730                       l_available_quantity := l_item_volume_qty;
12731                     END IF;
12732 
12733                     IF PG_DEBUG in ('Y', 'C') THEN
12734                        msc_sch_wb.atp_debug('Get_Transport_Cap_Atp_Info: ' || 'Date is: '||to_char(l_atp_period_tab(k)));
12735                        msc_sch_wb.atp_debug('Get_Transport_Cap_Atp_Info: ' || 'Quantity available is: '||to_char(l_available_quantity));
12736                     END IF;
12737 
12738                     IF (l_available_quantity >= p_quantity_ordered) THEN
12739                         x_atp_date_quantity_this_level := l_available_quantity;
12740                         x_atp_date_this_level := l_atp_period_tab(k);
12741 
12742 			l_atp_date_weight := l_atp_qty_tab(k);
12743 	                l_atp_date_volume := l_atp_qty_tab2(k);
12744 
12745                         IF PG_DEBUG in ('Y', 'C') THEN
12746                            msc_sch_wb.atp_debug('Get_Transport_Cap_Atp_Info: ' || 'done');
12747                            msc_sch_wb.atp_debug('Get_Transport_Cap_Atp_Info: ' || 'quantity: '||to_char(x_atp_date_quantity_this_level));
12748                            msc_sch_wb.atp_debug('Get_Transport_Cap_Atp_Info: ' || 'date: '||to_char(x_atp_date_this_level));
12749                         END IF;
12750                     END IF;
12751                 END LOOP;
12752             END IF;
12753         END IF;
12754     END LOOP;
12755 
12756     IF NVL(p_insert_flag,0) <> 0 THEN -- p_insert_flag
12757       -- add pegging info for weight demand
12758 
12759     l_pegging_rec.session_id := MSC_ATP_PVT.G_SESSION_ID;
12760     l_pegging_rec.order_line_id:= MSC_ATP_PVT.G_ORDER_LINE_ID;
12761     l_pegging_rec.parent_pegging_id:= p_parent_pegging_id;
12762     l_pegging_rec.atp_level:= p_level;
12763     l_pegging_rec.from_organization_id := p_from_organization_id;
12764     l_pegging_rec.to_organization_id := p_to_organization_id;
12765     l_pegging_rec.ship_method := p_ship_method;
12766     l_pegging_rec.identifier1 := p_source_org_instance_id;
12767     l_pegging_rec.identifier2 := 'WEIGHT';
12768 --    l_pegging_rec.identifier3 := l_transaction_id;
12769     l_pegging_rec.identifier4 := p_dest_org_instance_id;
12770     l_pegging_rec.scenario_id:= p_scenario_id;
12771     l_pegging_rec.supply_demand_source_type:= 1;
12772     l_pegging_rec.supply_demand_quantity := p_quantity_ordered*l_unit_weight;
12773     l_pegging_rec.weight_capacity := p_quantity_ordered*l_unit_weight;
12774     l_pegging_rec.supply_demand_type:= 1;
12775     l_pegging_rec.supply_demand_date:= l_requested_date;
12776     l_pegging_rec.department_id := NULL;
12777     l_pegging_rec.department_code := NULL;
12778     l_pegging_rec.resource_id := NULL;
12779     l_pegging_rec.resource_code := NULL;
12780     l_pegging_rec.inventory_item_id := NULL;
12781     l_pegging_rec.inventory_item_name := NULL;
12782     l_pegging_rec.supplier_id := NULL;
12783     l_pegging_rec.supplier_name := NULL;
12784     l_pegging_rec.supplier_site_id := NULL;
12785     l_pegging_rec.supplier_site_name := NULL;
12786 
12787 
12788     MSC_ATP_DB_UTILS.Add_Pegging(l_pegging_rec, l_pegging_id);
12789 
12790     l_wt_demand_pegging_id := l_pegging_id;
12791 
12792     -- add pegging info for volume demand
12793 
12794     l_pegging_rec.session_id := MSC_ATP_PVT.G_SESSION_ID;
12795     l_pegging_rec.order_line_id:= MSC_ATP_PVT.G_ORDER_LINE_ID;
12796     l_pegging_rec.parent_pegging_id:= p_parent_pegging_id;
12797     l_pegging_rec.atp_level:= p_level;
12798     l_pegging_rec.from_organization_id := p_from_organization_id;
12799     l_pegging_rec.to_organization_id := p_to_organization_id;
12800     l_pegging_rec.ship_method := p_ship_method;
12801     l_pegging_rec.identifier1 := p_source_org_instance_id;
12802     l_pegging_rec.identifier2 := 'VOLUME';
12803 --    l_pegging_rec.identifier3 := l_transaction_id;
12804     l_pegging_rec.identifier4 := p_dest_org_instance_id;
12805     l_pegging_rec.scenario_id:= p_scenario_id;
12806     l_pegging_rec.supply_demand_source_type:= 1;
12807     l_pegging_rec.supply_demand_quantity := p_quantity_ordered*l_unit_volume;
12808     l_pegging_rec.volume_capacity := p_quantity_ordered*l_unit_volume;
12809     l_pegging_rec.supply_demand_type:= 1;
12810     l_pegging_rec.supply_demand_date:= l_requested_date;
12811     l_pegging_rec.department_id := NULL;
12812     l_pegging_rec.department_code := NULL;
12813     l_pegging_rec.resource_id := NULL;
12814     l_pegging_rec.resource_code := NULL;
12815     l_pegging_rec.inventory_item_id := NULL;
12816     l_pegging_rec.inventory_item_name := NULL;
12817     l_pegging_rec.supplier_id := NULL;
12818     l_pegging_rec.supplier_name := NULL;
12819     l_pegging_rec.supplier_site_id := NULL;
12820     l_pegging_rec.supplier_site_name := NULL;
12821 
12822     MSC_ATP_DB_UTILS.Add_Pegging(l_pegging_rec, l_pegging_id);
12823 
12824     l_vol_demand_pegging_id := l_pegging_id;
12825 
12826     -- add pegging info for weight supply
12827 
12828     l_pegging_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
12829     l_pegging_rec.order_line_id:= MSC_ATP_PVT.G_ORDER_LINE_ID;
12830     l_pegging_rec.parent_pegging_id:= l_wt_demand_pegging_id;
12831     l_pegging_rec.atp_level:= p_level;
12832     l_pegging_rec.from_organization_id := p_from_organization_id;
12833     l_pegging_rec.to_organization_id := p_to_organization_id;
12834     l_pegging_rec.ship_method := p_ship_method;
12835     l_pegging_rec.identifier1 := p_source_org_instance_id;
12836     l_pegging_rec.identifier2 := 'WEIGHT';
12837     l_pegging_rec.identifier3 := -1;
12838     l_pegging_rec.identifier4 := p_dest_org_instance_id;
12839     l_pegging_rec.scenario_id := p_scenario_id;
12840     l_pegging_rec.supply_demand_source_type := MSC_ATP_PVT.ATP;
12841     l_pegging_rec.supply_demand_quantity := l_atp_qty_tab(k);
12842     l_pegging_rec.weight_capacity := l_atp_qty_tab(k);
12843     l_pegging_rec.supply_demand_type := 2;
12844     l_pegging_rec.supply_demand_date := x_atp_date_this_level;
12845     l_pegging_rec.department_id := NULL;
12846     l_pegging_rec.department_code := NULL;
12847     l_pegging_rec.resource_id := NULL;
12848     l_pegging_rec.resource_code := NULL;
12849     l_pegging_rec.inventory_item_id := NULL;
12850     l_pegging_rec.inventory_item_name := NULL;
12851     l_pegging_rec.supplier_id := NULL;
12852     l_pegging_rec.supplier_name := NULL;
12853     l_pegging_rec.supplier_site_id := NULL;
12854     l_pegging_rec.supplier_site_name := NULL;
12855     l_pegging_rec.source_type := 0;
12856 
12857     MSC_ATP_DB_UTILS.Add_Pegging(l_pegging_rec, l_pegging_id);
12858 
12859     -- add pegging info for volume 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_vol_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 := 'VOLUME';
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_tab2(k);
12875     l_pegging_rec.volume_capacity := l_atp_qty_tab2(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 
12889     l_pegging_rec.source_type := 0;
12890 
12891     MSC_ATP_DB_UTILS.Add_Pegging(l_pegging_rec, l_pegging_id);
12892 
12893     FOR i in 1..l_atp_period.Level.COUNT LOOP
12894             l_atp_period.Pegging_Id(i) := l_pegging_id;
12895             l_atp_period.End_Pegging_Id(i) := MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
12896 
12897 	    l_atp_period2.Pegging_Id(i) := l_pegging_id;
12898             l_atp_period2.End_Pegging_Id(i) := MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
12899 
12900     END LOOP;
12901 
12902     IF PG_DEBUG in ('Y', 'C') THEN
12903        msc_sch_wb.atp_debug('Get_Transport_Cap_Atp_Info: ' || 'in get_res_requirements we are here 2');
12904     END IF;
12905 
12906     FOR i in 1..l_atp_supply_demand.Level.COUNT LOOP
12907             l_atp_supply_demand.Pegging_Id(i) := l_pegging_id;
12908             l_atp_supply_demand.End_Pegging_Id(i) := MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
12909 
12910             l_atp_supply_demand2.Pegging_Id(i) := l_pegging_id;
12911             l_atp_supply_demand2.End_Pegging_Id(i) := MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
12912 
12913     END LOOP;
12914 
12915     -- select weight information into table
12916 
12917     SELECT
12918     col1,
12919     col2,
12920     col3,
12921     col4,
12922     col5,
12923     col6,
12924     col7,
12925     col8,
12926     col9,
12927     col10,
12928     col11,
12929     col12,
12930     col13,
12931     col14,
12932     col15,
12933     col16,
12934     col17,
12935     col18,
12936     col19,
12937     col20,
12938     col21,
12939     col22,
12940     col23,
12941     col24,
12942     col25,
12943     col26,
12944     col27,
12945     col28,
12946     col29
12947     BULK COLLECT INTO
12948 		     l_atp_supply_demand.Level,
12949                      l_atp_supply_demand.Identifier,
12950                      l_atp_supply_demand.Inventory_Item_Id,
12951                      l_atp_supply_demand.Request_Item_Id,
12952                      l_atp_supply_demand.Organization_Id,
12953                      l_atp_supply_demand.Department_Id,
12954                      l_atp_supply_demand.Resource_Id,
12955                      l_atp_supply_demand.Supplier_Id,
12956                      l_atp_supply_demand.Supplier_Site_Id,
12957                      l_atp_supply_demand.From_Organization_Id,
12958                      l_atp_supply_demand.From_Location_Id,
12959                      l_atp_supply_demand.To_Organization_Id,
12960                      l_atp_supply_demand.To_Location_Id,
12961                      l_atp_supply_demand.Ship_Method,
12962                      l_atp_supply_demand.Uom,
12963                      l_atp_supply_demand.Supply_Demand_Type,
12964                      l_atp_supply_demand.Supply_Demand_Source_Type,
12965                      l_atp_supply_demand.Supply_Demand_Source_Type_Name,
12966                      l_atp_supply_demand.Identifier1,
12967                      l_atp_supply_demand.Identifier2,
12968                      l_atp_supply_demand.Identifier3,
12969                      l_atp_supply_demand.Identifier4,
12970                      l_atp_supply_demand.Supply_Demand_Quantity,
12971                      l_atp_supply_demand.Supply_Demand_Date,
12972                      l_atp_supply_demand.Disposition_Type,
12973                      l_atp_supply_demand.Disposition_Name,
12974 		     l_atp_supply_demand.Scenario_Id,
12975                      l_atp_supply_demand.Pegging_Id,
12976                      l_atp_supply_demand.End_Pegging_Id
12977     FROM
12978     (
12979     SELECT
12980 	     p_level col1,
12981 	     p_identifier col2,
12982 	     p_inventory_item_id col3,
12983 	     null col4, -- request_item_id
12984 	     null col5, -- organization_id
12985 	     null col6, -- department_id
12986 	     null col7, -- resource_id
12987 	     null col8, -- supplier_id
12988 	     null col9, -- supplier_site_id
12989 	     p_from_organization_id col10,
12990 	     null col11, -- from_location_id
12991 	     p_to_organization_id col12,
12992 	     null col13, -- to_location_id
12993 	     p_ship_method col14,
12994 	     s.weight_uom col15, -- uom
12995 	     2 col16, -- supply
12996 	     null col17, -- supply_demand_source_type
12997 	     null col18, -- supply_demand_source_type_name
12998 	     p_source_org_instance_id col19, -- identifier1
12999              1 col20, -- identifier2, weight(1) or volume(2)
13000              null col21, -- identifier3
13001              p_dest_org_instance_id col22, -- identifier4
13002 	     s.weight_capacity col23,
13003 	     c.calendar_date col24,
13004 	     null col25, -- disposition_type
13005 	     null col26, -- disposition_name
13006 	     p_scenario_id col27, -- scenario_id
13007        	     null col28, -- pegging_id
13008 	     null col29  -- end_pegging_id
13009     FROM
13010 	     msc_calendar_dates c,
13011              msc_interorg_ship_methods s,
13012              msc_trading_partners tp,
13013              msc_plans p
13014     WHERE
13015  	     s.plan_id = p_plan_id
13016       AND    s.from_organization_id = p_from_organization_id
13017       AND    s.to_organization_id = p_to_organization_id
13018       AND    s.ship_method = p_ship_method
13019       AND    s.sr_instance_id = p_source_org_instance_id
13020       AND    s.sr_instance_id2 = p_dest_org_instance_id
13021       AND    s.from_organization_id = tp.sr_tp_id
13022       AND    tp.sr_instance_id = NVL(s.sr_instance_id, s.sr_instance_id2)
13023       AND    c.calendar_date BETWEEN trunc(SYSDATE) and trunc(p.curr_cutoff_date)    -- to_date changed to trunc to avoid GSCC error
13024       AND    c.calendar_code = tp.calendar_code
13025       AND    c.exception_set_id = tp.calendar_exception_set_id
13026       AND    p.plan_id = p_plan_id
13027       UNION ALL
13028       SELECT p_level col1,
13029              p_identifier col2,
13030              p_inventory_item_id col3,
13031              null col4, -- request_item_id
13032              null col5, -- organization_id
13033              null col6, -- department_id
13034              null col7, -- resource_id
13035              null col8, -- supplier_id
13036              null col9, -- supplier_site_id
13037              p_from_organization_id col10,
13038              null col11, -- from_location_id
13039              p_to_organization_id col12,
13040              null col13, -- to_location_id
13041              p_ship_method col14,
13042              s.weight_uom col15, -- uom
13043 	     1 col16, -- demand
13044 	     null col17, -- supply_demand_source_type
13045              null col18, -- supply_demand_source_type_name
13046 	     p_source_org_instance_id col19, -- identifier1
13047              1 col20, -- identifier2, weight(1) or volume(2)
13048              null col21, -- identifier3
13049              p_dest_org_instance_id col22, -- identifier4
13050 	     -1*(sup.new_order_quantity)*l_unit_weight col23,
13051 	     sup.new_schedule_date col24,
13052              null col25, -- disposition_type
13053              null col26, -- disposition_name
13054              p_scenario_id col27, -- scenairo_id
13055              null col28, -- pegging_id
13056              null col29  -- end_pegging_id
13057       FROM   msc_supplies sup,
13058 	     msc_interorg_ship_methods s
13059       WHERE  sup.plan_id = p_plan_id
13060       AND    sup.organization_id = p_to_organization_id
13061       AND    sup.sr_instance_id = p_dest_org_instance_id
13062       AND    sup.source_organization_id is not null
13063       AND    sup.source_organization_id = p_from_organization_id
13064       AND    sup.source_sr_instance_id = p_source_org_instance_id
13065       AND    sup.ship_method = p_ship_method
13066       AND    sup.inventory_item_id = p_inventory_item_id
13067       AND    s.plan_id = p_plan_id
13068       AND    s.from_organization_id = p_from_organization_id
13069       AND    s.to_organization_id = p_to_organization_id
13070       AND    s.ship_method = p_ship_method
13071       AND    s.sr_instance_id = p_source_org_instance_id
13072       AND    s.sr_instance_id2 = p_dest_org_instance_id
13073       )
13074       ORDER BY col25;
13075 
13076   -- select volume information into table
13077     SELECT
13078     col1,
13079     col2,
13080     col3,
13081     col4,
13082     col5,
13083     col6,
13084     col7,
13085     col8,
13086     col9,
13087     col10,
13088     col11,
13089     col12,
13090     col13,
13091     col14,
13092     col15,
13093     col16,
13094     col17,
13095     col18,
13096     col19,
13097     col20,
13098     col21,
13099     col22,
13100     col23,
13101     col24,
13102     col25,
13103     col26,
13104     col27,
13105     col28,
13106     col29
13107     BULK COLLECT INTO
13108 		     l_atp_supply_demand2.Level,
13109                      l_atp_supply_demand2.Identifier,
13110                      l_atp_supply_demand2.Inventory_Item_Id,
13111                      l_atp_supply_demand2.Request_Item_Id,
13112                      l_atp_supply_demand2.Organization_Id,
13113                      l_atp_supply_demand2.Department_Id,
13114                      l_atp_supply_demand2.Resource_Id,
13115                      l_atp_supply_demand2.Supplier_Id,
13116                      l_atp_supply_demand2.Supplier_Site_Id,
13117                      l_atp_supply_demand2.From_Organization_Id,
13118                      l_atp_supply_demand2.From_Location_Id,
13119                      l_atp_supply_demand2.To_Organization_Id,
13120                      l_atp_supply_demand2.To_Location_Id,
13121                      l_atp_supply_demand2.Ship_Method,
13122                      l_atp_supply_demand2.Uom,
13123                      l_atp_supply_demand2.Supply_Demand_Type,
13124                      l_atp_supply_demand2.Supply_Demand_Source_Type,
13125                      l_atp_supply_demand2.Supply_Demand_Source_Type_Name,
13126                      l_atp_supply_demand2.Identifier1,
13127                      l_atp_supply_demand2.Identifier2,
13128                      l_atp_supply_demand2.Identifier3,
13129                      l_atp_supply_demand2.Identifier4,
13130                      l_atp_supply_demand2.Supply_Demand_Quantity,
13131                      l_atp_supply_demand2.Supply_Demand_Date,
13132                      l_atp_supply_demand2.Disposition_Type,
13133                      l_atp_supply_demand2.Disposition_Name,
13134 		     l_atp_supply_demand2.Scenario_Id,
13135                      l_atp_supply_demand2.Pegging_Id,
13136                      l_atp_supply_demand2.End_Pegging_Id
13137     FROM
13138     (
13139     SELECT
13140 	     p_level col1,
13141 	     p_identifier col2,
13142 	     p_inventory_item_id col3,
13143 	     null col4, -- request_item_id
13144 	     null col5, -- organization_id
13145 	     null col6, -- department_id
13146 	     null col7, -- resource_id
13147 	     null col8, -- supplier_id
13148 	     null col9, -- supplier_site_id
13149 	     p_from_organization_id col10,
13150 	     null col11, -- from_location_id
13151 	     p_to_organization_id col12,
13152 	     null col13, -- to_location_id
13153 	     p_ship_method col14,
13154 	     s.volume_uom col15, -- uom
13155 	     2 col16, -- supply
13156 	     null col17, -- supply_demand_source_type
13157              null col18, -- supply_demand_source_type_name
13158 	     p_source_org_instance_id col19, -- identifier1
13159              2 col20, -- identifier2, weight(1) or volume(2)
13160              null col21, -- identifier3
13161              p_dest_org_instance_id col22, -- identifier4
13162 	     s.volume_capacity col23,
13163 	     c.calendar_date col24,
13164 	     null col25, -- disposition_type
13165 	     null col26, -- disposition_name
13166 	     p_scenario_id col27, -- scenairo_id
13167        	     null col28, -- pegging_id
13168 	     null col29  -- end_pegging_id
13169     FROM
13170 	     msc_calendar_dates c,
13171              msc_interorg_ship_methods s,
13172              msc_trading_partners tp,
13173              msc_plans p
13174     WHERE
13175  	     s.plan_id = p_plan_id
13176       AND    s.from_organization_id = p_from_organization_id
13177       AND    s.to_organization_id = p_to_organization_id
13178       AND    s.ship_method = p_ship_method
13179       AND    s.sr_instance_id = p_source_org_instance_id
13180       AND    s.sr_instance_id2 = p_dest_org_instance_id
13181       AND    s.from_organization_id = tp.sr_tp_id
13182       AND    tp.sr_instance_id = NVL(s.sr_instance_id, s.sr_instance_id2)
13183       AND    c.calendar_date BETWEEN trunc(SYSDATE) and trunc(p.curr_cutoff_date)   -- to_date changed to trunc to avoid GSCC error
13184       AND    c.calendar_code = tp.calendar_code
13185       AND    c.exception_set_id = tp.calendar_exception_set_id
13186       AND    p.plan_id = p_plan_id
13187       UNION ALL
13188       SELECT p_level col1,
13189              p_identifier col2,
13190              p_inventory_item_id col3,
13191              null col4, -- request_item_id
13192              null col5, -- organization_id
13193              null col6, -- department_id
13194              null col7, -- resource_id
13195              null col8, -- supplier_id
13196              null col9, -- supplier_site_id
13197              p_from_organization_id col10,
13198              null col11, -- from_location_id
13199              p_to_organization_id col12,
13200              null col13, -- to_location_id
13201              p_ship_method col14,
13202              s.volume_uom col15, -- uom
13203 	     1 col16, -- demand
13204 	     null col17, -- supply_demand_source_type
13205              null col18, -- supply_demand_source_type_name
13206 	     p_source_org_instance_id col19, -- identifier1
13207              2 col20, -- identifier2, weight(1) or volume(2)
13208              null col21, -- identifier3
13209              p_dest_org_instance_id col22, -- identifier4
13210 	     -1*(sup.new_order_quantity)*l_unit_volume col23,
13211 	     sup.new_schedule_date col24,
13212              null col25, -- disposition_type
13213              null col26, -- disposition_name
13214              p_scenario_id col27, -- scenairo_id
13215              null col28, -- pegging_id
13216              null col29  -- end_pegging_id
13217       FROM   msc_supplies sup,
13218 	     msc_interorg_ship_methods s
13219       WHERE  sup.plan_id = p_plan_id
13220       AND    sup.organization_id = p_to_organization_id
13221       AND    sup.sr_instance_id = p_dest_org_instance_id
13222       AND    sup.source_organization_id is not null
13223       AND    sup.source_organization_id = p_from_organization_id
13224       AND    sup.source_sr_instance_id = p_source_org_instance_id
13225       AND    sup.ship_method = p_ship_method
13226       AND    sup.inventory_item_id = p_inventory_item_id
13227       AND    s.plan_id = p_plan_id
13228       AND    s.from_organization_id = p_from_organization_id
13229       AND    s.to_organization_id = p_to_organization_id
13230       AND    s.ship_method = p_ship_method
13231       AND    s.sr_instance_id = p_source_org_instance_id
13232       AND    s.sr_instance_id2 = p_dest_org_instance_id
13233       )
13234       ORDER BY col25;
13235 
13236       m := 1;
13237 
13238       IF l_atp_supply_demand.Supply_Demand_Date.COUNT > 0 THEN
13239         MSC_SATP_FUNC.Extend_Atp_Period(l_atp_period, x_return_status);
13240 
13241    	FOR i IN 1..l_atp_supply_demand.Supply_Demand_Date.COUNT LOOP
13242      	  l_atp_period.Level(m) := l_atp_supply_demand.Level(i);
13243           l_atp_period.Identifier(m) := l_atp_supply_demand.Identifier(i);
13244           l_atp_period.Scenario_Id(m) := l_atp_supply_demand.Scenario_Id(i);
13245 	  l_atp_period.From_Organization_Id := l_atp_supply_demand.From_Organization_Id;
13246 	  l_atp_period.To_Organization_Id := l_atp_supply_demand.To_Organization_Id;
13247 	  l_atp_period.Ship_Method := l_atp_supply_demand.Ship_Method;
13248 	  l_atp_period.Uom := l_atp_supply_demand.Uom;
13249 
13250 	  IF i = 1 THEN
13251 	    l_atp_period.Period_Start_Date(m) := l_atp_supply_demand.Supply_Demand_Date(i);
13252 
13253   	    l_atp_period.Identifier1(m) := l_atp_supply_demand.Identifier1(i);
13254             l_atp_period.Identifier2(m) := l_atp_supply_demand.Identifier2(i);
13255 
13256 	    -- working on first supply demand record
13257             IF l_atp_supply_demand.Supply_Demand_Type(i) = 1 THEN
13258               l_atp_period.Total_Demand_Quantity(m) :=
13259                    l_atp_supply_demand.Supply_Demand_Quantity(i);
13260               l_atp_period.Total_Supply_Quantity(m) := 0;
13261               l_atp_period.Period_Quantity(m) :=
13262                    l_atp_period.Total_Supply_Quantity(m)+
13263                    l_atp_period.Total_Demand_Quantity(m);
13264 
13265 	    ELSE
13266                l_atp_period.Total_Supply_Quantity(m) :=
13267                    l_atp_supply_demand.Supply_Demand_Quantity(i);
13268                l_atp_period.Total_Demand_Quantity(m) := 0;
13269                l_atp_period.Period_Quantity(m) :=
13270                    l_atp_period.Total_Supply_Quantity(m)+
13271                    l_atp_period.Total_Demand_Quantity(m);
13272             END IF;
13273 
13274 	  ELSE
13275             -- working on 2nd record or later
13276             -- make sure the supply demand date of this record is
13277             -- greater than the previous bucket or not.
13278             IF l_atp_supply_demand.Supply_Demand_Date(i) >
13279                l_atp_period.Period_Start_Date(m) THEN
13280 
13281 	       -- populate the period_end_date and
13282                -- period_atp information.
13283                l_atp_period.Period_End_Date(m) :=
13284                    l_atp_supply_demand.Supply_Demand_Date(i) -1;
13285 
13286                -- add one more bucket
13287                m:=m+1;
13288 
13289                MSC_SATP_FUNC.Extend_Atp_Period(l_atp_period, x_return_status);
13290 
13291 	       l_atp_period.Level(m) := l_atp_supply_demand.Level(i);
13292                l_atp_period.Identifier(m) := l_atp_supply_demand.Identifier(i);
13293                l_atp_period.Scenario_Id(m) := l_atp_supply_demand.Scenario_Id(i);
13294                l_atp_period.From_Organization_Id := l_atp_supply_demand.From_Organization_Id;
13295                l_atp_period.To_Organization_Id := l_atp_supply_demand.To_Organization_Id;
13296                l_atp_period.Ship_Method := l_atp_supply_demand.Ship_Method;
13297                l_atp_period.Uom := l_atp_supply_demand.Uom;
13298 
13299                l_atp_period.Period_Start_Date(m) :=
13300                    l_atp_supply_demand.Supply_Demand_Date(i);
13301 
13302                l_atp_period.Identifier1(m):=l_atp_supply_demand.Identifier1(i);
13303                l_atp_period.Identifier2(m):=l_atp_supply_demand.Identifier2(i);
13304 
13305 	       IF l_atp_supply_demand.Supply_Demand_Type(i) = 1 THEN
13306                    l_atp_period.Total_Demand_Quantity(m) :=
13307                    l_atp_supply_demand.Supply_Demand_Quantity(i);
13308                    l_atp_period.Total_Supply_Quantity(m) := 0;
13309                    l_atp_period.Period_Quantity(m) :=
13310                    l_atp_period.Total_Supply_Quantity(m)+
13311                    l_atp_period.Total_Demand_Quantity(m);
13312 
13313                ELSE
13314                    l_atp_period.Total_Supply_Quantity(m) :=
13315                    l_atp_supply_demand.Supply_Demand_Quantity(m);
13316                    l_atp_period.Total_Demand_Quantity(m) := 0;
13317                    l_atp_period.Period_Quantity(m) :=
13318                    l_atp_period.Total_Supply_Quantity(m)+
13319                    l_atp_period.Total_Demand_Quantity(m);
13320 
13321                END IF;
13322 
13323 	    ELSE
13324 	       -- same bucket
13325                IF l_atp_supply_demand.Supply_Demand_Type(i) = 1 THEN
13326                    l_atp_period.Total_Demand_Quantity(m) :=
13327                    l_atp_period.Total_Demand_Quantity(m) +
13328                    l_atp_supply_demand.Supply_Demand_Quantity(i);
13329                ELSE
13330                    l_atp_period.Total_Supply_Quantity(m) :=
13331                    l_atp_period.Total_Supply_Quantity(m)+
13332                    l_atp_supply_demand.Supply_Demand_Quantity(i);
13333                END IF;
13334                l_atp_period.Period_Quantity(m) :=
13335                l_atp_period.Total_Supply_Quantity(m)+
13336                l_atp_period.Total_Demand_Quantity(m);
13337 
13338            END IF;
13339  	END IF; -- end 2nd record or later
13340       END LOOP;
13341     END IF;
13342 
13343     m := 1;
13344 
13345       IF l_atp_supply_demand2.Supply_Demand_Date.COUNT > 0 THEN
13346         MSC_SATP_FUNC.Extend_Atp_Period(l_atp_period2, x_return_status);
13347 
13348    	FOR i IN 1..l_atp_supply_demand2.Supply_Demand_Date.COUNT LOOP
13349      	  l_atp_period2.Level(m) := l_atp_supply_demand2.Level(i);
13350           l_atp_period2.Identifier(m) := l_atp_supply_demand2.Identifier(i);
13351           l_atp_period2.Scenario_Id(m) := l_atp_supply_demand2.Scenario_Id(i);
13352 	  l_atp_period2.From_Organization_Id := l_atp_supply_demand2.From_Organization_Id;
13353 	  l_atp_period2.To_Organization_Id := l_atp_supply_demand2.To_Organization_Id;
13354 	  l_atp_period2.Ship_Method := l_atp_supply_demand2.Ship_Method;
13355 	  l_atp_period2.Uom := l_atp_supply_demand2.Uom;
13356 
13357 	  IF i = 1 THEN
13358 	    l_atp_period2.Period_Start_Date(m) := l_atp_supply_demand2.Supply_Demand_Date(i);
13359 
13360   	    l_atp_period2.Identifier1(m) := l_atp_supply_demand2.Identifier1(i);
13361             l_atp_period2.Identifier2(m) := l_atp_supply_demand2.Identifier2(i);
13362 
13363 	    -- working on first supply demand record
13364             IF l_atp_supply_demand2.Supply_Demand_Type(i) = 1 THEN
13365               l_atp_period2.Total_Demand_Quantity(m) :=
13366                    l_atp_supply_demand2.Supply_Demand_Quantity(i);
13367               l_atp_period2.Total_Supply_Quantity(m) := 0;
13368               l_atp_period2.Period_Quantity(m) :=
13369                    l_atp_period2.Total_Supply_Quantity(m)+
13370                    l_atp_period2.Total_Demand_Quantity(m);
13371 
13372 	    ELSE
13373                l_atp_period2.Total_Supply_Quantity(m) :=
13374                    l_atp_supply_demand2.Supply_Demand_Quantity(i);
13375                l_atp_period2.Total_Demand_Quantity(m) := 0;
13376                l_atp_period2.Period_Quantity(m) :=
13377                    l_atp_period2.Total_Supply_Quantity(m)+
13378                    l_atp_period2.Total_Demand_Quantity(m);
13379             END IF;
13380 
13381 	  ELSE
13382             -- working on 2nd record or later
13383             -- make sure the supply demand date of this record is
13384             -- greater than the previous bucket or not.
13385             IF l_atp_supply_demand2.Supply_Demand_Date(i) >
13386                l_atp_period2.Period_Start_Date(m) THEN
13387 
13388 	       -- populate the period_end_date and
13389                -- period_atp information.
13390                l_atp_period2.Period_End_Date(m) :=
13391                    l_atp_supply_demand2.Supply_Demand_Date(i) -1;
13392 
13393                -- add one more bucket
13394                m:=m+1;
13395 
13396                MSC_SATP_FUNC.Extend_Atp_Period(l_atp_period2, x_return_status);
13397 
13398 	       l_atp_period2.Level(m) := l_atp_supply_demand2.Level(i);
13399                l_atp_period2.Identifier(m) := l_atp_supply_demand2.Identifier(i);
13400                l_atp_period2.Scenario_Id(m) := l_atp_supply_demand2.Scenario_Id(i);
13401                l_atp_period2.From_Organization_Id := l_atp_supply_demand2.From_Organization_Id;
13402                l_atp_period2.To_Organization_Id := l_atp_supply_demand2.To_Organization_Id;
13403                l_atp_period2.Ship_Method := l_atp_supply_demand2.Ship_Method;
13404                l_atp_period2.Uom := l_atp_supply_demand2.Uom;
13405 
13406                l_atp_period2.Period_Start_Date(m) :=
13407                    l_atp_supply_demand2.Supply_Demand_Date(i);
13408 
13409                l_atp_period2.Identifier1(m):=l_atp_supply_demand2.Identifier1(i);
13410                l_atp_period2.Identifier2(m):=l_atp_supply_demand2.Identifier2(i);
13411 
13412 	       IF l_atp_supply_demand2.Supply_Demand_Type(i) = 1 THEN
13413                    l_atp_period2.Total_Demand_Quantity(m) :=
13414                    l_atp_supply_demand2.Supply_Demand_Quantity(i);
13415                    l_atp_period2.Total_Supply_Quantity(m) := 0;
13416                    l_atp_period2.Period_Quantity(m) :=
13417                    l_atp_period2.Total_Supply_Quantity(m)+
13418                    l_atp_period2.Total_Demand_Quantity(m);
13419 
13420                ELSE
13421                    l_atp_period2.Total_Supply_Quantity(m) :=
13422                    l_atp_supply_demand2.Supply_Demand_Quantity(m);
13423                    l_atp_period2.Total_Demand_Quantity(m) := 0;
13424                    l_atp_period2.Period_Quantity(m) :=
13425                    l_atp_period2.Total_Supply_Quantity(m)+
13426                    l_atp_period2.Total_Demand_Quantity(m);
13427 
13428                END IF;
13429 
13430 	    ELSE
13431 	       -- same bucket
13432                IF l_atp_supply_demand2.Supply_Demand_Type(i) = 1 THEN
13433                    l_atp_period2.Total_Demand_Quantity(m) :=
13434                    l_atp_period2.Total_Demand_Quantity(m) +
13435                    l_atp_supply_demand2.Supply_Demand_Quantity(i);
13436                ELSE
13437                    l_atp_period2.Total_Supply_Quantity(m) :=
13438                    l_atp_period2.Total_Supply_Quantity(m)+
13439                    l_atp_supply_demand2.Supply_Demand_Quantity(i);
13440                END IF;
13441                l_atp_period2.Period_Quantity(m) :=
13442                l_atp_period2.Total_Supply_Quantity(m)+
13443                l_atp_period2.Total_Demand_Quantity(m);
13444 
13445            END IF;
13446  	END IF; -- end 2nd record or later
13447       END LOOP;
13448     END IF;
13449 
13450     l_atp_period.Cumulative_Quantity := l_atp_period.Period_Quantity;
13451     -- do the accumulation
13452     MSC_ATP_PROC.atp_consume(l_atp_period.Cumulative_Quantity, m);
13453 
13454     l_atp_period2.Cumulative_Quantity := l_atp_period2.Period_Quantity;
13455     -- do the accumulation
13456     MSC_ATP_PROC.atp_consume(l_atp_period2.Cumulative_Quantity, m);
13457 
13458   END IF;
13459 
13460   MSC_ATP_PROC.Details_Output(l_atp_period2, l_atp_supply_demand2, l_atp_period, l_atp_supply_demand,
13461 			x_return_status);
13462 
13463   x_atp_period := l_atp_period;
13464   x_atp_supply_demand := l_atp_supply_demand;
13465 
13466 
13467 EXCEPTION
13468 
13469   WHEN NO_DATA_FOUND THEN
13470     x_return_status := FND_API.G_RET_STS_ERROR;
13471 END Get_Transport_Cap_Atp_Info;
13472 
13473 
13474 --s_cto_rearch
13475 procedure Extend_Atp_Comp_Typ ( P_Atp_Comp_Typ IN OUT NOCOPY MRP_ATP_PVT.Atp_Comp_Typ)
13476 IS
13477 BEGIN
13478 
13479    P_Atp_Comp_Typ.inventory_item_id.extend;
13480    P_Atp_Comp_Typ.comp_usage.extend;
13481    P_Atp_Comp_Typ.requested_date.extend;
13482    P_Atp_Comp_Typ.lead_time.extend;
13483    P_Atp_Comp_Typ.wip_supply_type.extend;
13484    P_Atp_Comp_Typ.assembly_identifier.extend;
13485    P_Atp_Comp_Typ.component_identifier.extend;
13486    P_Atp_Comp_Typ.reverse_cumulative_yield.extend;
13487    P_Atp_Comp_Typ.match_item_id.extend;
13488    P_Atp_Comp_Typ.bom_item_type.extend;
13489    P_Atp_Comp_Typ.parent_line_id.extend;
13490    P_Atp_Comp_Typ.top_model_line_id.extend;
13491    P_Atp_Comp_Typ.ato_parent_model_line_id.extend;
13492    P_Atp_Comp_Typ.ato_model_line_id.extend;
13493    P_Atp_Comp_Typ.mand_comp_flag.extend;
13494    P_Atp_Comp_Typ.parent_so_quantity.extend;
13495    P_Atp_Comp_Typ.fixed_lt.extend;
13496    p_atp_comp_typ.variable_lt.extend;
13497    p_atp_comp_typ.oss_error_code.extend;
13498    p_atp_comp_typ.atp_flag.extend;
13499    p_atp_comp_typ.atp_components_flag.extend;
13500    P_Atp_Comp_Typ.request_item_id.extend;       -- For time_phased_atp
13501    P_Atp_Comp_Typ.atf_date.extend;              -- For time_phased_atp
13502    P_Atp_Comp_Typ.match_item_family_id.extend;  -- For time_phased_atp
13503    P_Atp_Comp_Typ.dest_inventory_item_id.extend;
13504    P_Atp_Comp_Typ.parent_item_id.extend;
13505    P_Atp_Comp_Typ.comp_uom.extend; --bug3110023
13506    --4570421
13507    P_Atp_Comp_Typ.scaling_type.extend;
13508    P_Atp_Comp_Typ.scale_multiple.extend;
13509    P_Atp_Comp_Typ.scale_rounding_variance.extend;
13510    P_Atp_Comp_Typ.rounding_direction.extend;
13511    P_Atp_Comp_Typ.component_yield_factor.extend;
13512    P_Atp_Comp_Typ.usage_qty.extend; --4775920
13513    P_Atp_Comp_Typ.organization_type.extend; --4775920
13514 
13515 END Extend_Atp_Comp_Typ;
13516 
13517 
13518 Procedure Add_To_Comp_List(p_explode_comp_rec          IN OUT NOCOPY MRP_ATP_PVT.Atp_Comp_Typ,
13519                            p_component_rec             IN OUT NOCOPY MRP_ATP_PVT.Atp_Comp_Typ,
13520                            p_atp_comp_rec              IN MRP_ATP_PVT.ATP_COMP_REC)
13521 
13522 IS
13523 j  number;
13524 BEGIN
13525 
13526    IF PG_DEBUG in ('Y', 'C') THEN
13527        msc_sch_wb.atp_debug('Add_To_Comp_List: Enter Into Add_To_Comp_List');
13528        msc_sch_wb.atp_debug('Add_To_Comp_List: inventory_item_id := ' || p_atp_comp_rec.inventory_item_id );
13529        msc_sch_wb.atp_debug('Add_To_Comp_List: comp_usage := ' || p_atp_comp_rec.comp_usage );
13530        msc_sch_wb.atp_debug('Add_To_Comp_List: requested_date := ' || p_atp_comp_rec.requested_date );
13531        msc_sch_wb.atp_debug('Add_To_Comp_List: lead_time := ' || p_atp_comp_rec.lead_time );
13532        msc_sch_wb.atp_debug('Add_To_Comp_List: wip_supply_type := ' || p_atp_comp_rec.wip_supply_type );
13533        msc_sch_wb.atp_debug('Add_To_Comp_List: assembly_identifier := ' || p_atp_comp_rec.assembly_identifier );
13534        msc_sch_wb.atp_debug('Add_To_Comp_List: component_identifier := ' || p_atp_comp_rec.component_identifier );
13535        msc_sch_wb.atp_debug('Add_To_Comp_List: reverse_cumulative_yield := ' || p_atp_comp_rec.reverse_cumulative_yield );
13536        msc_sch_wb.atp_debug('Add_To_Comp_List: match_item_id:= ' || p_atp_comp_rec.match_item_id );
13537        msc_sch_wb.atp_debug('Add_To_Comp_List: match_item_family_id := ' || p_atp_comp_rec.match_item_family_id);
13538        msc_sch_wb.atp_debug('Add_To_Comp_List: bom_item_type := ' || p_atp_comp_rec.bom_item_type );
13539        msc_sch_wb.atp_debug('Add_To_Comp_List: parent_line_id := ' || p_atp_comp_rec.parent_line_id );
13540        msc_sch_wb.atp_debug('Add_To_Comp_List: top_model_line_id := ' || p_atp_comp_rec.top_model_line_id );
13541        msc_sch_wb.atp_debug('Add_To_Comp_List: ato_parent_model_line_id := ' || p_atp_comp_rec.ato_parent_model_line_id );
13542        msc_sch_wb.atp_debug('Add_To_Comp_List: ato_model_line_id := ' || p_atp_comp_rec.ato_model_line_id );
13543        msc_sch_wb.atp_debug('Add_To_Comp_List: MAND_COMP_FLAG := ' || p_atp_comp_rec.MAND_COMP_FLAG );
13544        msc_sch_wb.atp_debug('Add_To_Comp_List: parent_so_quantity := ' || p_atp_comp_rec.parent_so_quantity );
13545        msc_sch_wb.atp_debug('Add_To_Comp_List: fixed_lt := ' || p_atp_comp_rec.fixed_lt );
13546        msc_sch_wb.atp_debug('Add_To_Comp_List: variable_lt := ' || p_atp_comp_rec.variable_lt );
13547        msc_sch_wb.atp_debug('Add_To_Comp_List: oss_error_code := ' || p_atp_comp_rec.oss_error_code );
13548        msc_sch_wb.atp_debug('Add_To_Comp_List: model_flag := ' || p_atp_comp_rec.model_flag );
13549        msc_sch_wb.atp_debug('Add_To_Comp_List: requested_quantity := ' || p_atp_comp_rec.requested_quantity );
13550        msc_sch_wb.atp_debug('Add_To_Comp_List: atp flag := ' || p_atp_comp_rec.atp_flag);
13551        msc_sch_wb.atp_debug('Add_To_Comp_List: atp_comp_flag := '|| p_atp_comp_rec.atp_components_flag);
13552        msc_sch_wb.atp_debug('Add_To_Comp_List: atf_date := '|| p_atp_comp_rec.atf_date);
13553        msc_sch_wb.atp_debug('Add_To_Comp_List: dest_inventory_item_id := ' || p_atp_comp_rec.dest_inventory_item_id);
13554        msc_sch_wb.atp_debug('Add_To_Comp_List: parent_repl_ord_flag := ' || p_atp_comp_rec.parent_repl_ord_flag);
13555        msc_sch_wb.atp_debug('Add_To_Comp_List: comp_uom := ' || p_atp_comp_rec.comp_uom); --bug3110023
13556 
13557    END IF;
13558 
13559    IF ((p_atp_comp_rec.model_flag = 1 and p_atp_comp_rec.bom_item_type = 2) or
13560                                  (p_atp_comp_rec.model_flag <>  1 and p_atp_comp_rec.wip_supply_type = 6
13561                                                                   and p_atp_comp_rec.parent_repl_ord_flag = 'N'
13562                                                                   and MSC_ATP_PVT.G_INV_CTP = 4)) THEN
13563       -- this is phantom, add to explode list
13564 
13565       j := p_explode_comp_rec.inventory_item_id.COUNT;
13566 
13567       -- bug 1831563: removed the extend and assign to
13568       -- l_explode_comp.pre_process_lead_time since it is not needed.
13569 
13570       IF PG_DEBUG in ('Y', 'C') THEN
13571           msc_sch_wb.atp_debug('Add_To_Comp_List: ' || 'in side phantom, count = '||j);
13572           msc_sch_wb.atp_debug('Add_To_Comp_List: Count in explode comp := ' || p_explode_comp_rec.inventory_item_id.count);
13573       END IF;
13574 
13575       MSC_ATP_REQ.Extend_ATP_Comp_TYP(p_explode_comp_rec);
13576 
13577       IF PG_DEBUG in ('Y', 'C') THEN
13578           msc_sch_wb.atp_debug('Add_To_Comp_List: ' || 'after extend');
13579       END IF;
13580 
13581       p_explode_comp_rec.inventory_item_id(j+1):= p_atp_comp_rec.inventory_item_id;
13582       /* time_phased_atp changes begin
13583          Support PF ATP for components*/
13584       p_explode_comp_rec.request_item_id(j+1):= p_atp_comp_rec.request_item_id;
13585       p_explode_comp_rec.atf_date(j+1):= p_atp_comp_rec.atf_date;
13586       p_explode_comp_rec.match_item_family_id(j + 1) := p_atp_comp_rec.match_item_family_id;
13587       /* time_phased_atp changes end*/
13588 
13589       p_explode_comp_rec.requested_date(j+1) := p_atp_comp_rec.requested_date;
13590       p_explode_comp_rec.comp_usage(j+1) := p_atp_comp_rec.comp_usage;
13591       p_explode_comp_rec.wip_supply_type(j+1) := p_atp_comp_rec.wip_supply_type;
13592       --(3004862) circular BOM issue Assign the Inventory item id for component. We'll
13593       --be using this later when accessing process effectivity.
13594       p_explode_comp_rec.component_identifier(j+1) := p_atp_comp_rec.component_identifier;
13595 
13596       --- bug 2680027: Now remember the lead time for parent
13597       p_explode_comp_rec.lead_time(j+1) := p_atp_comp_rec.lead_time;
13598 
13599       p_explode_comp_rec.assembly_identifier(j +1) := p_atp_comp_rec.assembly_identifier;
13600       p_explode_comp_rec.match_item_id(j + 1) := p_atp_comp_rec.match_item_id;
13601       p_explode_comp_rec.bom_item_type(j + 1) := p_atp_comp_rec.bom_item_type;
13602       p_explode_comp_rec.top_model_line_id(j +1) := p_atp_comp_rec.top_model_line_id;
13603       p_explode_comp_rec.ato_parent_model_line_id(j +1 ) := p_atp_comp_rec.ato_parent_model_line_id;
13604       p_explode_comp_rec.ato_model_line_id(j+ 1) := p_atp_comp_rec.ato_model_line_id;
13605       p_explode_comp_rec.parent_line_id(j+1) := p_atp_comp_rec.parent_line_id;
13606       p_explode_comp_rec.reverse_cumulative_yield(j+1) := p_atp_comp_rec.reverse_cumulative_yield;
13607       p_explode_comp_rec.parent_so_quantity(j + 1) := p_atp_comp_rec.parent_so_quantity;
13608       p_explode_comp_rec.mand_comp_flag(j + 1) := p_atp_comp_rec.mand_comp_flag;
13609       p_explode_comp_rec.fixed_lt(j +1) := p_atp_comp_rec.fixed_lt;
13610       p_explode_comp_rec.variable_lt(j +1 ) := p_atp_comp_rec.variable_lt;
13611       p_explode_comp_rec.oss_error_code(j + 1) := p_atp_comp_rec.oss_error_code;
13612       p_explode_comp_rec.atp_flag(j +1 ) := p_atp_comp_rec.atp_flag;
13613       p_explode_comp_rec.atp_components_flag(j +1 ) := p_atp_comp_rec.atp_components_flag;
13614       p_explode_comp_rec.dest_inventory_item_id(j +1)  := p_atp_comp_rec.dest_inventory_item_id;
13615       p_explode_comp_rec.comp_uom(j +1)  := p_atp_comp_rec.comp_uom; --bug3110023
13616       IF PG_DEBUG in ('Y', 'C') THEN
13617            msc_sch_wb.atp_debug('Add_To_Comp_List: explode lead_time := ' || p_explode_comp_rec.lead_time(j+1));
13618            msc_sch_wb.atp_debug('Add_To_Comp_List: ' || 'after assign ');
13619       END IF;
13620    END IF;
13621 
13622    IF p_atp_comp_rec.wip_supply_type <> 6 or
13623        p_atp_comp_rec.parent_repl_ord_flag = 'Y' or p_atp_comp_rec.model_flag = 1 THEN
13624 
13625          -- this is not phantom, add to the component list
13626 
13627          j := p_component_rec.inventory_item_id.COUNT;
13628 
13629          IF PG_DEBUG in ('Y', 'C') THEN
13630              msc_sch_wb.atp_debug('Add_To_Comp_List: ' || 'not phantom, j='||j);
13631          END IF;
13632 
13633          MSC_ATP_REQ.Extend_ATP_Comp_TYP(p_component_rec);
13634 
13635          p_component_rec.inventory_item_id(j+1):= p_atp_comp_rec.inventory_item_id;
13636          /* time_phased_atp changes begin
13637             Support PF ATP for components*/
13638          p_component_rec.request_item_id(j+1):= p_atp_comp_rec.request_item_id;
13639          p_component_rec.atf_date(j+1):= p_atp_comp_rec.atf_date;
13640          -- time_phased_atp changes end
13641 
13642          p_component_rec.requested_date(j+1) := p_atp_comp_rec.requested_date;
13643          --p_component_rec.comp_usage(j+1) := p_atp_comp_rec.comp_usage/p_atp_comp_rec.requested_quantity;
13644          --4570421
13645          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
13646                 (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
13647                 p_component_rec.comp_usage(j+1) := p_atp_comp_rec.comp_usage/p_atp_comp_rec.requested_quantity;
13648          ELSE
13649                 p_component_rec.comp_usage(j+1) := p_atp_comp_rec.comp_usage;
13650          END IF;
13651          IF PG_DEBUG in ('Y', 'C') THEN
13652              msc_sch_wb.atp_debug('Add_To_Comp_List: p_atp_comp_rec.comp_usage ' || p_atp_comp_rec.comp_usage);
13653              msc_sch_wb.atp_debug('Add_To_Comp_List: p_atp_comp_rec.requested_quantity ' || p_atp_comp_rec.requested_quantity);
13654              msc_sch_wb.atp_debug('Add_To_Comp_List: p_component_rec.comp_usage(j+1) ' || p_component_rec.comp_usage(j+1));
13655          END IF;
13656 
13657          --4570421
13658          p_component_rec.scaling_type(j+1) := p_atp_comp_rec.scaling_type;
13659          p_component_rec.scale_multiple(j+1) := p_atp_comp_rec.scale_multiple;
13660          p_component_rec.scale_rounding_variance(j+1) := p_atp_comp_rec.scale_rounding_variance;
13661          p_component_rec.rounding_direction(j+1) := p_atp_comp_rec.rounding_direction;
13662          p_component_rec.component_yield_factor(j+1) := p_atp_comp_rec.component_yield_factor; --4570421
13663          p_component_rec.usage_qty(j+1) := p_atp_comp_rec.usage_qty; --4775920
13664          p_component_rec.organization_type(j+1) := p_atp_comp_rec.organization_type; --4775920
13665 
13666          --- bug 2680027: Add lead time from Parent
13667          --l_comp_requirements.lead_time(j+1) := l_atp_comp_rec.comp_lead_time;
13668          p_component_rec.lead_time(j+1) := p_atp_comp_rec.lead_time;
13669 
13670          p_component_rec.assembly_identifier(j+1) := p_atp_comp_rec.assembly_identifier;
13671          p_component_rec.component_identifier(j+1) := p_atp_comp_rec.component_identifier;
13672 
13673          p_component_rec.reverse_cumulative_yield(j+1) := p_atp_comp_rec.reverse_cumulative_yield;
13674          p_component_rec.wip_supply_type(j +1) := p_atp_comp_rec.wip_supply_type;
13675          p_component_rec.match_item_id(j+1) := p_atp_comp_rec.match_item_id;
13676          p_component_rec.bom_item_type(j +1) := p_atp_comp_rec.bom_item_type;
13677          p_component_rec.parent_line_id(j +1) := p_atp_comp_rec.parent_line_id;
13678          p_component_rec.top_model_line_id(j +1) := p_atp_comp_rec.top_model_line_id;
13679          p_component_rec.ato_parent_model_line_id(j+1) := p_atp_comp_rec.ato_parent_model_line_id;
13680          p_component_rec.ato_model_line_id(j+1) := p_atp_comp_rec.ato_model_line_id;
13681          p_component_rec.parent_so_quantity(j +1) := p_atp_comp_rec.parent_so_quantity;
13682          p_component_rec.mand_comp_flag(j +1) := p_atp_comp_rec.mand_comp_flag;
13683          p_component_rec.fixed_lt(j+1) := p_atp_comp_rec.fixed_lt;
13684          p_component_rec.variable_lt(j+1) := p_atp_comp_rec.variable_lt;
13685          p_component_rec.oss_error_code(j +1) := p_atp_comp_rec.oss_error_code;
13686          p_component_rec.atp_flag(j +1)  := p_atp_comp_rec.atp_flag;
13687          p_component_rec.atp_components_flag(j +1) := p_atp_comp_rec.atp_components_flag;
13688          p_component_rec.dest_inventory_item_id(j +1)  := p_atp_comp_rec.dest_inventory_item_id;
13689          p_component_rec.match_item_family_id(j + 1) := p_atp_comp_rec.match_item_family_id;
13690          p_component_rec.comp_uom(j + 1) := p_atp_comp_rec.comp_uom; --bug3110023
13691 
13692 
13693          IF PG_DEBUG in ('Y', 'C') THEN
13694             msc_sch_wb.atp_debug('Add_To_Comp_List: ' || 'in side not phantom');
13695          END IF;
13696    END IF;
13697 
13698 END Add_To_Comp_List;
13699 ---e_cto_rearch
13700 
13701 
13702 END MSC_ATP_REQ;