DBA Data[Home] [Help]

PACKAGE BODY: APPS.MSC_AATP_PVT

Source


1 PACKAGE BODY MSC_AATP_PVT AS
2 /* $Header: MSCAATPB.pls 120.4.12010000.3 2008/08/25 10:22:08 sbnaik ship $  */
3 
4 G_PKG_NAME 		CONSTANT VARCHAR2(30) := 'MSC_AATP_PVT';
5 -- ship_rec_cal
6 G_USER_ID               CONSTANT NUMBER := FND_GLOBAL.USER_ID;
7 
8 -- INFINITE_NUMBER         CONSTANT NUMBER := 1.0e+10;
9 
10 -- demand type
11 DEMAND_SALES_ORDER_MDS  CONSTANT INTEGER := 6;
12 DEMAND_FORECAST         CONSTANT INTEGER := 7;
13 DEMAND_MANUAL           CONSTANT INTEGER := 8;
14 DEMAND_OTHER            CONSTANT INTEGER := 9;
15 DEMAND_HARD_RESERVE     CONSTANT INTEGER := 10;
16 DEMND_MDS_IND           CONSTANT INTEGER := 11;
17 DEMND_MPS_COMPILE       CONSTANT INTEGER := 12;
18 FORECAST                CONSTANT INTEGER := 29;
19 DEMAND_SALES_ORDER      CONSTANT INTEGER := 30;
20 
21 -- AATP Forward Consumption rajjain
22 G_ATP_FW_CONSUME_METHOD NUMBER := NVL(FND_PROFILE.VALUE('MSC_ATP_FORWARD_CONSUME_METHOD'), 1);
23 ----------------------------------------------------------------------
24 
25 PG_DEBUG varchar2(1) := NVL(FND_PROFILE.value('MSC_ATP_DEBUG'), 'N');
26 
27 PROCEDURE Print_Period_Qty(
28 	p_msg		IN varchar2,
29 	p_atp_info	IN MRP_ATP_PVT.ATP_Info
30 ) IS
31 	i	PLS_INTEGER;
32 BEGIN
33 	i := p_atp_info.atp_qty.FIRST;
34 	while i is not null loop
35 		msc_sch_wb.atp_debug(p_msg || p_atp_info.atp_period(i) || ':' ||
36 			p_atp_info.atp_qty(i));
37 		i := p_atp_info.atp_qty.NEXT(i);
38 	end loop;
39 END Print_Period_Qty;
40 
41 
42 PROCEDURE move_SD_plsql_into_SD_temp(
43 	x_atp_supply_demand 	IN OUT NOCOPY  MRP_ATP_PUB.ATP_Supply_Demand_Typ
44 ) IS
45 	null_atp_supply_demand  MRP_ATP_PUB.ATP_Supply_Demand_Typ;
46 	-- ship_rec_cal
47 	l_sysdate               DATE := trunc(sysdate); --4135752
48 BEGIN
49 	IF PG_DEBUG in ('Y', 'C') THEN
50 	   msc_sch_wb.atp_debug('**** PROCEDURE move_SD_plsql_into_SD_temp ****');
51 	END IF;
52 
53 	FORALL k IN 1..x_atp_supply_demand.Level.COUNT
54 		INSERT INTO msc_atp_sd_details_temp (
55 			session_id,
56 			Atp_Level,
57 			order_line_id,
58 			scenario_id,
59 			inventory_item_id,
60 			request_item_id,
61 			department_id,
62 			resource_id,
63 			Supplier_id,
64 			Supplier_Site_id,
65 			UOM_code,
66 			Supply_Demand_Type,
67 			Supply_Demand_Source_Type,
68 			Identifier1,
69 			Supply_Demand_Date,
70 			Supply_Demand_Quantity,
71 			creation_date,
72 			created_by,
73 			last_update_date,
74 			last_updated_by,
75 			last_update_login,
76 			-- time_phased_atp changes begin
77 			organization_id,
78 			original_item_id,
79                         original_supply_demand_type,
80                         original_demand_date,
81                         original_demand_quantity,
82                         allocated_quantity,
83                         pf_display_flag
84                         -- time_phased_atp changes end
85 		) VALUES (
86 			MSC_ATP_PVT.G_SESSION_ID,
87 			x_atp_supply_demand.level(k),
88 			x_atp_supply_demand.identifier(k),
89 			x_atp_supply_demand.scenario_id(k),
90 			x_atp_supply_demand.inventory_item_id(k),
91 			x_atp_supply_demand.request_item_id(k),
92 			x_atp_supply_demand.department_id(k),
93 			x_atp_supply_demand.resource_id(k),
94 			x_atp_supply_demand.supplier_id(k),
95 			x_atp_supply_demand.supplier_site_id(k),
96 			x_atp_supply_demand.uom(k),
97 			x_atp_supply_demand.supply_demand_type(k),
98 			x_atp_supply_demand.supply_demand_source_type(k),
99 			x_atp_supply_demand.identifier1(k),
100 			x_atp_supply_demand.supply_demand_date(k),
101 	                x_atp_supply_demand.supply_demand_quantity(k),
102         		-- ship_rec_cal changes begin
103         		l_sysdate,
104         		G_USER_ID,
105         		l_sysdate,
106         		G_USER_ID,
107         		G_USER_ID,
108         		-- ship_rec_cal changes end
109 			-- time_phased_atp changes begin
110 			x_atp_supply_demand.organization_id(k),
111 			x_atp_supply_demand.original_item_id(k),
112                         x_atp_supply_demand.original_supply_demand_type(k),
113                         x_atp_supply_demand.original_demand_date(k),
114                         x_atp_supply_demand.original_demand_quantity(k),
115                         x_atp_supply_demand.allocated_quantity(k),
116                         x_atp_supply_demand.pf_display_flag(k)
117 			-- time_phased_atp changes end
118 		);
119 	-- end forall
120 
121 	x_atp_supply_demand := null_atp_supply_demand;
122 
123 END move_SD_plsql_into_SD_temp;
124 
125 ---------------------------------------------------------------------------
126 
127 -- 2859130
128 -- constrained plan
129 -- from previous resource requirements fix 2809639, optimized plan really means
130 -- constrained plan. here opt=constrained, unopt=unconstrained
131 --avjain Plan by request date  all netting sqls have been changed to incorporate Plan by Request Date Enhancements
132 PROCEDURE item_alloc_avail_opt (
133    p_item_id            IN NUMBER,
134    p_org_id             IN NUMBER,
135    p_instance_id        IN NUMBER,
136    p_plan_id            IN NUMBER,
137    p_demand_class       IN VARCHAR2,
138    p_level_id           IN NUMBER,
139    p_itf                IN DATE,
140    p_cal_code           IN VARCHAR2,
141    p_cal_exc_set_id     IN NUMBER,
142    p_sys_next_date	IN DATE,
143    x_atp_dates          OUT NoCopy MRP_ATP_PUB.date_arr,
144    x_atp_qtys           OUT NoCopy MRP_ATP_PUB.number_arr
145 ) IS
146 BEGIN
147         IF PG_DEBUG in ('Y', 'C') THEN
148                 msc_sch_wb.atp_debug('item_alloc_avail_opt: constrained plan: ' || p_plan_id);
149         END IF;
150               -- Bug 3550296 and 3574164. IMPLEMENT_DATE AND DMD_SATISFIED_DATE are changed to
151               -- IMPLEMENT_SHIP_DATE and PLANNED_SHIP_DATE resp.
152               SELECT        SD_DATE,
153                            SUM(SD_QTY)
154              BULK COLLECT INTO
155                            x_atp_dates,
156                            x_atp_qtys
157              FROM (
158                    SELECT  --C.PRIOR_DATE SD_DATE, -- 2859130
159 			  GREATEST(
160                            TRUNC(DECODE(RECORD_SOURCE,
161                            	2,
162                            	NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
163                            	DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
164                            	2,
165                            	(NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
166                            	NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
167                            	 p_sys_next_date)SD_DATE, --plan by request date,promise date, schedule date--3099066
168                            -- -1* D.USING_REQUIREMENT_QUANTITY*
169                            -1*(D.USING_REQUIREMENT_QUANTITY - NVL(d.reserved_quantity, 0))* --5027568
170                            DECODE(DECODE(G_HIERARCHY_PROFILE,
171                            /*------------------------------------------------------------------------+
172                            | rajjain begin 07/19/2002                                                |
173                            |                                                                         |
174                            | Case 1: For internal sales orders [origination type is in (6,30) and    |
175                            |            source_organization_id is not null and <> organization_id]   |
176                            |                  Return NULL                                            |
177                            | Case 2: For others if DEMAND_CLASS is null then return null             |
178                            |          else if p_demand_class is '-1' then call                       |
179                            |            Get_Hierarchy_Demand_class else return DEMAND_CLASS          |
180                            +------------------------------------------------------------------------*/
181                               1, decode(decode (d.origination_type, -100, 30,d.origination_type),
182                                  6, decode(d.source_organization_id,
183                                     NULL, DECODE(D.DEMAND_CLASS, null, null,
184                                        DECODE(p_demand_class, '-1',
185                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
186                                              null, null, p_item_id, p_org_id,
187                                              p_instance_id, TRUNC(
188                                                   DECODE(RECORD_SOURCE,
189                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
190                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
191                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
192                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
193                                                                       --plan by request date,promise date, schedule date
194                                              		p_level_id, D.DEMAND_CLASS),
195                                              D.DEMAND_CLASS)),
196                                     -23453, DECODE(D.DEMAND_CLASS, null, null,
197                                        DECODE(p_demand_class, '-1',
198                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
199                                              null, null, p_item_id, p_org_id,
200                                              p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
201                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
202                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
203                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
204                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
205                                              					p_level_id, D.DEMAND_CLASS),  --plan by request date,promise date, schedule date
206                                              D.DEMAND_CLASS)),
207                                     d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
208                                        DECODE(p_demand_class, '-1',
209                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
210                                              null, null, p_item_id, p_org_id,
211                                              p_instance_id, TRUNC(
212                                              	    DECODE(RECORD_SOURCE,
213                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
214                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
215                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
216                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
217                                              			p_level_id, D.DEMAND_CLASS),  --plan by request date,promise date, schedule date
218                                              D.DEMAND_CLASS)), TO_CHAR(NULL)),
219                                  30, decode(d.source_organization_id,
220                                     NULL, DECODE(D.DEMAND_CLASS, null, null,
221                                        DECODE(p_demand_class, '-1',
222                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
223                                              null, null, p_item_id, p_org_id,
224                                              p_instance_id, TRUNC(
225                                              	    DECODE(RECORD_SOURCE,
226                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
227                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
228                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
229                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
230                                              				p_level_id, D.DEMAND_CLASS),  --plan by request date,promise date, schedule date
231                                              D.DEMAND_CLASS)),
232                                     -23453, DECODE(D.DEMAND_CLASS, null, null,
233                                        DECODE(p_demand_class, '-1',
234                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
235                                              null, null, p_item_id, p_org_id,
236                                              p_instance_id, TRUNC(
237                                              	    DECODE(RECORD_SOURCE,
238                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
239                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
240                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
241                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
242                                              				p_level_id, D.DEMAND_CLASS),  --plan by request date,promise date, schedule date
243                                              D.DEMAND_CLASS)),
244                                     d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
245                                        DECODE(p_demand_class, '-1',
246                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
247                                              null, null, p_item_id, p_org_id,
248                                              p_instance_id, TRUNC(
249                                              	    DECODE(RECORD_SOURCE,
250                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
251                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
252                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
253                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
254                                              			p_level_id, D.DEMAND_CLASS),  --plan by request date,promise date, schedule date
255                                              D.DEMAND_CLASS)), TO_CHAR(NULL)),
256                                  DECODE(D.DEMAND_CLASS, null, null,
257                                     DECODE(p_demand_class, '-1',
258                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
259                                           null, null, p_item_id, p_org_id,
260                                           p_instance_id, TRUNC(
261                                                    DECODE(RECORD_SOURCE,
262                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
263                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
264                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
265                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
266                                           			p_level_id, D.DEMAND_CLASS),  --plan by request date,promise date, schedule date
267                                           D.DEMAND_CLASS))),
268                               -- rajjain end
269                               2, DECODE(D.CUSTOMER_ID, NULL, TO_CHAR(NULL),
270                                                    0, TO_CHAR(NULL),
271                                  -- rajjain begin 07/19/2002
272                                  decode(decode (d.origination_type, -100, 30,d.origination_type),
273                                     6, decode(d.source_organization_id,
274                                        NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
275                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
276                                              p_org_id, p_instance_id,
277                                              TRUNC(DECODE(RECORD_SOURCE,
278                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
279                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
280                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
281                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), --plan by request date,promise date, schedule date
282                                              p_level_id, NULL),
283                                        -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
284                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
285                                              p_org_id, p_instance_id,
286                                              TRUNC(DECODE(RECORD_SOURCE,
287                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
288                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
289                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
290                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), --plan by request date,promise date, schedule date
291                                              p_level_id, NULL),
292                                        d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
293                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
294                                              p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
295                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
296                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
297                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
298                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), --plan by request date,promise date, schedule date
299                                              p_level_id, NULL),
300                                        TO_CHAR(NULL)),
301                                     30, decode(d.source_organization_id,
302                                        NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
303                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
304                                              p_org_id, p_instance_id,
305                                              	TRUNC(DECODE(RECORD_SOURCE,
306                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
307                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
308                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
309                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), --plan by request date,promise date, schedule date
310                                              p_level_id, NULL),
311                                        -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
312                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
313                                              p_org_id, p_instance_id,
314                                              	TRUNC(DECODE(RECORD_SOURCE,
315                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
316                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
317                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
318                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), --plan by request date,promise date, schedule date
319                                              p_level_id, NULL),
320                                        d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
321                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
322                                              p_org_id, p_instance_id,
323                                              TRUNC(DECODE(RECORD_SOURCE,
324                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
325                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
326                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
327                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), --plan by request date,promise date, schedule date
328                                              p_level_id, NULL),
329                                        TO_CHAR(NULL)),
330                                     MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(D.CUSTOMER_ID, D.SHIP_TO_SITE_ID,
331                                        p_item_id, p_org_id, p_instance_id,
332                                        	          TRUNC(DECODE(RECORD_SOURCE,
333                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
334                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
335                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
336                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))))--plan by request date,promise date, schedule date
337                                        				,p_level_id, NULL)))),
338                                  -- rajjain end 07/19/2002
339                            p_demand_class, 1,
340                              Decode(D.Demand_Class, NULL, --4365873 If l_demand_class is not null and demand class is populated
341                              -- on  supplies record then 0 should be allocated.
342                               MSC_AATP_FUNC.Get_Item_Demand_Alloc_Percent(p_plan_id,
343                                  D.DEMAND_ID,
344 		                                 TRUNC(DECODE(RECORD_SOURCE,
345                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
346                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
347                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
348                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), --plan by request date,promise date, schedule date
349                                  D.USING_ASSEMBLY_ITEM_ID,
350                                  DECODE(D.SOURCE_ORGANIZATION_ID,
351                                     -23453, null,
352                                     D.SOURCE_ORGANIZATION_ID), -- 1665483
353                                  p_item_id,
354                                  p_org_id, -- 1665483
355                                  p_instance_id,
356                                  decode (d.origination_type, -100, 30,d.origination_type), --5027568
357                                  DECODE(G_HIERARCHY_PROFILE,
358                                  /*-----------------------------------------------------------------+
359                                  | rajjain begin 07/19/2002                                         |
360                                  |                                                                  |
361                                  | Case 1: For internal sales orders [origination type is in (6,30) |
362                                  |         and source_organization_id is not null                   |
363                                  |         and <> organization_id] -> Return p_demand_class         |
364                                  | Case 2: For others if DEMAND_CLASS is null then return null      |
365                                  |           else if p_demand_class is '-1' then call               |
366                                  |           Get_Hierarchy_Demand_class else return DEMAND_CLASS    |
367                                  +-----------------------------------------------------------------*/
368                                     1, decode(decode (d.origination_type, -100, 30,d.origination_type),
369                                        6, decode(d.source_organization_id,
370                                           NULL, DECODE(D.DEMAND_CLASS, null, null,
371                                              DECODE(p_demand_class, '-1',
372                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
373                                                    null, null, p_item_id, p_org_id,
374                                                    p_instance_id, TRUNC(
375                                                    DECODE(RECORD_SOURCE,
376                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
377                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
378                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
379                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), --plan by request date,promise date, schedule date
380                                                    	p_level_id, D.DEMAND_CLASS),
381                                                 D.DEMAND_CLASS)),
382                                           -23453, DECODE(D.DEMAND_CLASS, null, null,
383                                              DECODE(p_demand_class, '-1',
384                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
385                                                    null, null, p_item_id, p_org_id,
386                                                    p_instance_id,
387                                                    	TRUNC(DECODE(RECORD_SOURCE,
388                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
389                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
390                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
391                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), --plan by request date,promise date, schedule date
392                                                    			p_level_id, D.DEMAND_CLASS),
393                                                 D.DEMAND_CLASS)),
394                                           d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
395                                              DECODE(p_demand_class, '-1',
396                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
397                                                    null, null, p_item_id, p_org_id,
398                                                    p_instance_id,
399                                                    TRUNC(DECODE(RECORD_SOURCE,
400                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
401                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
402                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
403                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), --plan by request date,promise date, schedule date
404                                                    		p_level_id, D.DEMAND_CLASS),
405                                                 D.DEMAND_CLASS)),
406                                           p_demand_class),
407                                     30, decode(d.source_organization_id,
408                                        NULL, DECODE(D.DEMAND_CLASS, null, null,
409                                           DECODE(p_demand_class, '-1',
410                                              MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
411                                                 null, null, p_item_id, p_org_id,
412                                                 p_instance_id,
413                                                     TRUNC(DECODE(RECORD_SOURCE,
414                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
415                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
416                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
417                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), --plan by request date,promise date, schedule date
418                                                 		p_level_id, D.DEMAND_CLASS),
419                                              D.DEMAND_CLASS)),
420                                        -23453, DECODE(D.DEMAND_CLASS, null, null,
421                                           DECODE(p_demand_class, '-1',
422                                              MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
423                                                 null, null, p_item_id, p_org_id,
424                                                 p_instance_id,
425                                                      TRUNC(DECODE(RECORD_SOURCE,
426                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
427                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
428                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
429                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), --plan by request date,promise date, schedule date
430                                                 			p_level_id, D.DEMAND_CLASS),
431                                              D.DEMAND_CLASS)),
432                                        d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
433                                           DECODE(p_demand_class, '-1',
434                                              MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
435                                                 null, null, p_item_id, p_org_id,
436                                                 p_instance_id,
437                                                      TRUNC(DECODE(RECORD_SOURCE,
438                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
439                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
440                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
441                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), --plan by request date,promise date, schedule date
442                                                 	p_level_id, D.DEMAND_CLASS),
443                                              D.DEMAND_CLASS)),
444                                        p_demand_class),
445                                     DECODE(D.DEMAND_CLASS, null, null,
446                                        DECODE(p_demand_class, '-1',
447                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
448                                              null, null, p_item_id, p_org_id,
449                                              p_instance_id, TRUNC(
450                                              	DECODE(RECORD_SOURCE,
451                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
452                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
453                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
454                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))))--plan by request date,promise date, schedule date
455                                              		, p_level_id, D.DEMAND_CLASS),
456                                           D.DEMAND_CLASS))),
457                                     -- rajjain end
458                                     2, DECODE(D.CUSTOMER_ID, NULL, p_demand_class,
459                                                    0, p_demand_class,
460                                        -- rajjain begin 07/19/2002
461                                        decode(decode (d.origination_type, -100, 30,d.origination_type),  --5027568
462                                           6, decode(d.source_organization_id,
463                                              NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
464                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
465                                                    p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
466                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
467                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
468                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
469                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
470                                                    p_level_id, NULL),
471                                              -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
472                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
473                                                    p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
474                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
475                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
476                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
477                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
478                                                    p_level_id, NULL),
479                                              d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
480                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
481                                                    p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
482                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
483                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
484                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
485                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
486                                                    p_level_id, NULL),
487                                              p_demand_class),
488                                           30, decode(d.source_organization_id,
489                                              NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
490                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
491                                                    p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
492                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
493                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
494                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
495                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
496                                                    p_level_id, NULL),
497                                              -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
498                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
499                                                    p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
500                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
501                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
502                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
503                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
504                                                    p_level_id, NULL),
505                                              d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
506                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
507                                                    p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
508                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
509                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
510                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
511                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
512                                                    p_level_id, NULL),
513                                              p_demand_class),
514                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(D.CUSTOMER_ID, D.SHIP_TO_SITE_ID,
515                                              p_item_id, p_org_id, p_instance_id,
516                                              TRUNC(DECODE(RECORD_SOURCE,
517                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
518                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
519                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
520                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),p_level_id, NULL)))),
521                                        -- rajjain end 07/19/2002
522                                        p_demand_class,
523                                        p_level_id),0)) SD_QTY --4365873
524                    FROM    -- MSC_CALENDAR_DATES C, -- 2859130
525                            MSC_DEMANDS D
526                    WHERE        D.PLAN_ID = p_plan_id
527                    AND                D.SR_INSTANCE_ID = p_instance_id
528                    AND                D.INVENTORY_ITEM_ID = p_item_id
529                    AND         D.ORGANIZATION_ID = p_org_id
530                    AND         D.USING_REQUIREMENT_QUANTITY <> 0 --4501434
531                    AND         D.ORIGINATION_TYPE NOT IN (5,7,8,9,11,15,22,28,29,31,52) -- For summary enhancement
532                    -- 2859130
533                    -- Bug1990155, 1995835 exclude the expired lots demand datreya 9/18/2001
534                    -- Bug 1530311, need to exclude forecast, ngoel 12/05/2000
535                    -- AND                C.CALENDAR_CODE = p_cal_code
536                    -- AND                C.EXCEPTION_SET_ID = p_cal_exc_set_id
537                    -- AND         C.SR_INSTANCE_ID = D.SR_INSTANCE_ID
538                    -- since we store repetitive schedule demand in different ways for
539                    -- ods (total quantity on start date) and pds  (daily quantity from
540                    -- start date to end date), we need to make sure we only select work day
541                    -- for pds's repetitive schedule demand.
542                    -- AND         C.CALENDAR_DATE BETWEEN TRUNC(D.USING_ASSEMBLY_DEMAND_DATE) AND
543                    --             TRUNC(NVL(D.ASSEMBLY_DEMAND_COMP_DATE,
544                    --                       D.USING_ASSEMBLY_DEMAND_DATE))
545                    -- AND         (( D.ORIGINATION_TYPE = 4
546                    --         AND C.SEQ_NUM IS NOT NULL) OR
547                    --         ( D.ORIGINATION_TYPE  <> 4))
548                    -- AND         C.PRIOR_DATE < NVL(p_itf,
549                    --                  C.PRIOR_DATE + 1)
550                    --bug3693892 added trunc
551                    AND   TRUNC(DECODE(RECORD_SOURCE,
552                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
553                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
554                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
555                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))) <
556                          TRUNC(NVL(p_itf,DECODE(RECORD_SOURCE,
557                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
558                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
559                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
560                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))+1))
561                    UNION ALL
562                    SELECT  --C.NEXT_DATE SD_DATE, -- 2859130
563                            --TRUNC(NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE)) SD_DATE,
564                            GREATEST(TRUNC(NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE)),p_sys_next_date) SD_DATE,--3099066
565                            NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY)*
566                               DECODE(DECODE(G_HIERARCHY_PROFILE,
567                                      --2424357: Convert the demand calls in case of others for
568                                      --- demand class allocated ATP
569                                      1, DECODE(S.DEMAND_CLASS, null, null,
570                                         DECODE(p_demand_class, '-1',
571                                            MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
572                                                          null,
573                                                          null,
574                                                          S.inventory_item_id,
575                                                          p_org_id,
576                                                          p_instance_id,
577                                                          trunc(nvl(S.firm_date, S.new_schedule_date)),
578                                                          -- c.next_date, -- 2859130
579                                                          p_level_id,
580                                                          S.DEMAND_CLASS),S.DEMAND_CLASS)),
581                                      2, DECODE(S.CUSTOMER_ID, NULL, TO_CHAR(NULL),
582                                                        0, TO_CHAR(NULL),
583                                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
584                                                          S.CUSTOMER_ID,
585                                                          S.SHIP_TO_SITE_ID,
586                                                          S.inventory_item_id,
587                                                          p_org_id,
588                                                          p_instance_id,
589                                                          trunc(nvl(S.firm_date, S.new_schedule_date)),
590                                                          -- c.next_date, -- 2859130
591                                                          p_level_id,
592                                                          NULL))),
593                                  p_demand_class, 1,
594                                  NULL, nvl(MIHM.allocation_percent/100,1), --4365873
595                                  /*NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
596                                            p_instance_id,
597                                            s.inventory_item_id,
598                                            p_org_id,
599                                            null,
600                                            null,
601                                            p_demand_class,
602                                            -- c.next_date -- 2859130
603                                            trunc(nvl(s.firm_date, s.new_schedule_date))), 1),*/
604                                  DECODE(
605                                   MIHM.allocation_percent/100, --4365873
606                                  /*DECODE(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
607                                            p_instance_id,
608                                            s.inventory_item_id,
609                                            p_org_id,
610                                            null,
611                                            null,
612                                            p_demand_class,
613                                            -- c.next_date -- 2859130
614                                            trunc(nvl(s.firm_date, s.new_schedule_date))),*/
615                                    NULL, 1,
616                                  0)) SD_QTY
617                    FROM    -- MSC_CALENDAR_DATES C, -- 2859130
618                            MSC_SUPPLIES S,MSC_ITEM_HIERARCHY_MV MIHM
619                    WHERE   S.PLAN_ID = p_plan_id
620                    AND     S.SR_INSTANCE_ID = p_instance_id
621                    AND     S.INVENTORY_ITEM_ID = p_item_id
622                    AND     S.ORGANIZATION_ID = p_org_id
623                            -- Exclude Cancelled Supplies 2460645
624                    AND     NVL(S.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
625                    AND     NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY) <> 0 -- 1243985
626                    -- 2859130
627                    -- AND     C.CALENDAR_CODE = p_cal_code
628                    -- AND     C.EXCEPTION_SET_ID = p_cal_exc_set_id
629                    -- AND     C.SR_INSTANCE_ID = S.SR_INSTANCE_ID
630                    -- AND     C.CALENDAR_DATE BETWEEN TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE))
631                    --         AND TRUNC(NVL(S.LAST_UNIT_COMPLETION_DATE,
632                    --             NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)))
633                    -- AND     DECODE(S.LAST_UNIT_COMPLETION_DATE,
634                    --                    NULL, C.NEXT_SEQ_NUM, C.SEQ_NUM) IS NOT NULL
635                    -- AND     C.NEXT_DATE >= DECODE(S.ORDER_TYPE, 27, TRUNC(SYSDATE),
636                    --                                            28, TRUNC(SYSDATE),
637                    --                                            C.NEXT_DATE)
638                    --AND     C.NEXT_DATE < NVL(p_itf,
639                    --                          C.NEXT_DATE + 1)
640                    AND     trunc(NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE)) >=
641                                           trunc(DECODE(S.ORDER_TYPE, 27, SYSDATE,
642                                                                28, SYSDATE,
643                                                                NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE))) --4135752
644                    AND     trunc(NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE)) < trunc(NVL(p_itf,
645                                             NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE) + 1))
646 		--4365873
647                 AND    S.INVENTORY_ITEM_ID = MIHM.INVENTORY_ITEM_ID(+)
648                 AND    S.SR_INSTANCE_ID = MIHM.SR_INSTANCE_ID (+)
649                 AND    S.ORGANIZATION_ID = MIHM.ORGANIZATION_ID (+)
650                 AND    decode(MIHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
651                 AND    trunc(NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE)) >= MIHM.effective_date (+)
652                 AND    trunc(NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE)) <= MIHM.disable_date (+)
653                 AND    MIHM.demand_class (+) = p_demand_class
654                    )
655              GROUP BY SD_DATE
656              ORDER BY SD_DATE; --4698199
657 END item_alloc_avail_opt;
658 
659 -- unconstrained plan
660 --avjain All netting sqls have been changed to incorporate Plan by Request Date Enhancements
661 PROCEDURE item_alloc_avail_unopt (
662    p_item_id            IN NUMBER,
663    p_org_id             IN NUMBER,
664    p_instance_id        IN NUMBER,
665    p_plan_id            IN NUMBER,
666    p_demand_class       IN VARCHAR2,
667    p_level_id           IN NUMBER,
668    p_itf                IN DATE,
669    p_cal_code           IN VARCHAR2,
670    p_cal_exc_set_id     IN NUMBER,
671    p_sys_next_date	IN DATE,			--bug3099066
672    x_atp_dates          OUT NoCopy MRP_ATP_PUB.date_arr,
673    x_atp_qtys           OUT NoCopy MRP_ATP_PUB.number_arr
674 ) IS
675 BEGIN
676         IF PG_DEBUG in ('Y', 'C') THEN
677                 msc_sch_wb.atp_debug('item_alloc_avail_unopt: UNconstrained plan: ' || p_plan_id);
678         END IF;
679 
680              SELECT        SD_DATE,
681                            SUM(SD_QTY)
682              BULK COLLECT INTO
683                            x_atp_dates,
684                            x_atp_qtys
685              FROM (
686                    SELECT  ---C.CALENDAR_DATE SD_DATE,
687                    GREATEST(C.CALENDAR_DATE,p_sys_next_date) SD_DATE,--3099066
688                            -1* DECODE(D.ORIGINATION_TYPE,
689                                   4, D.DAILY_DEMAND_RATE,
690                                   --D.USING_REQUIREMENT_QUANTITY)*
691                                   (D.USING_REQUIREMENT_QUANTITY - NVL(d.reserved_quantity, 0)))*  --5027568
692                            DECODE(DECODE(G_HIERARCHY_PROFILE,
693                            /*------------------------------------------------------------------------+
694                            | rajjain begin 07/19/2002                                                |
695                            |                                                                         |
696                            | Case 1: For internal sales orders [origination type is in (6,30) and    |
697                            |            source_organization_id is not null and <> organization_id]   |
698                            |                  Return NULL                                            |
699                            | Case 2: For others if DEMAND_CLASS is null then return null             |
700                            |          else if p_demand_class is '-1' then call                       |
701                            |            Get_Hierarchy_Demand_class else return DEMAND_CLASS          |
702                            +------------------------------------------------------------------------*/
703                               1, decode(decode (d.origination_type, -100, 30,d.origination_type),  --5027568
704                                  6, decode(d.source_organization_id,
705                                     NULL, DECODE(D.DEMAND_CLASS, null, null,
706                                        DECODE(p_demand_class, '-1',
707                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
708                                              null, null, p_item_id, p_org_id,
709                                              p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
710                                              D.DEMAND_CLASS)),
711                                     -23453, DECODE(D.DEMAND_CLASS, null, null,
712                                        DECODE(p_demand_class, '-1',
713                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
714                                              null, null, p_item_id, p_org_id,
715                                              p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
716                                              D.DEMAND_CLASS)),
717                                     d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
718                                        DECODE(p_demand_class, '-1',
719                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
720                                              null, null, p_item_id, p_org_id,
721                                              p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
722                                              D.DEMAND_CLASS)), TO_CHAR(NULL)),
723                                  30, decode(d.source_organization_id,
724                                     NULL, DECODE(D.DEMAND_CLASS, null, null,
725                                        DECODE(p_demand_class, '-1',
726                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
727                                              null, null, p_item_id, p_org_id,
728                                              p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
729                                              D.DEMAND_CLASS)),
730                                     -23453, DECODE(D.DEMAND_CLASS, null, null,
731                                        DECODE(p_demand_class, '-1',
732                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
733                                              null, null, p_item_id, p_org_id,
734                                              p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
735                                              D.DEMAND_CLASS)),
736                                     d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
737                                        DECODE(p_demand_class, '-1',
738                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
739                                              null, null, p_item_id, p_org_id,
740                                              p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
741                                              D.DEMAND_CLASS)), TO_CHAR(NULL)),
742                                  DECODE(D.DEMAND_CLASS, null, null,
743                                     DECODE(p_demand_class, '-1',
744                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
745                                           null, null, p_item_id, p_org_id,
746                                           p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
747                                           D.DEMAND_CLASS))),
748                               -- rajjain end
749                               2, DECODE(D.CUSTOMER_ID, NULL, TO_CHAR(NULL),
750                                                    0, TO_CHAR(NULL),
751                                  -- rajjain begin 07/19/2002
752                                  decode(decode (d.origination_type, -100, 30,d.origination_type),  --5027568
753                                     6, decode(d.source_organization_id,
754                                        NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
755                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
756                                              p_org_id, p_instance_id, c.calendar_date,
757                                              p_level_id, NULL),
758                                        -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
759                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
760                                              p_org_id, p_instance_id, c.calendar_date,
761                                              p_level_id, NULL),
762                                        d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
763                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
764                                              p_org_id, p_instance_id, c.calendar_date,
765                                              p_level_id, NULL),
766                                        TO_CHAR(NULL)),
767                                     30, decode(d.source_organization_id,
768                                        NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
769                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
770                                              p_org_id, p_instance_id, c.calendar_date,
771                                              p_level_id, NULL),
772                                        -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
773                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
774                                              p_org_id, p_instance_id, c.calendar_date,
775                                              p_level_id, NULL),
776                                        d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
777                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
778                                              p_org_id, p_instance_id, c.calendar_date,
779                                              p_level_id, NULL),
780                                        TO_CHAR(NULL)),
781                                     MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(D.CUSTOMER_ID, D.SHIP_TO_SITE_ID,
782                                        p_item_id, p_org_id, p_instance_id,
783                                        c.calendar_date,p_level_id, NULL)))),
784                                  -- rajjain end 07/19/2002
785                            p_demand_class, 1,
786                              Decode(D.Demand_Class, NULL, --4365873
787                               MSC_AATP_FUNC.Get_Item_Demand_Alloc_Percent(p_plan_id,
788                                  D.DEMAND_ID,
789                                  c.calendar_date,
790                                  D.USING_ASSEMBLY_ITEM_ID,
791                                  DECODE(D.SOURCE_ORGANIZATION_ID,
792                                     -23453, null,
793                                     D.SOURCE_ORGANIZATION_ID), -- 1665483
794                                  p_item_id,
795                                  p_org_id, -- 1665483
796                                  p_instance_id,
797                                  decode (d.origination_type, -100, 30,d.origination_type), --5027568
798                                  DECODE(G_HIERARCHY_PROFILE,
799                                  /*-----------------------------------------------------------------+
800                                  | rajjain begin 07/19/2002                                         |
801                                  |                                                                  |
802                                  | Case 1: For internal sales orders [origination type is in (6,30) |
803                                  |         and source_organization_id is not null                   |
804                                  |         and <> organization_id] -> Return p_demand_class         |
805                                  | Case 2: For others if DEMAND_CLASS is null then return null      |
806                                  |           else if p_demand_class is '-1' then call               |
807                                  |           Get_Hierarchy_Demand_class else return DEMAND_CLASS    |
808                                  +-----------------------------------------------------------------*/
809                                     1, decode(decode (d.origination_type, -100, 30,d.origination_type),  --5027568
810                                        6, decode(d.source_organization_id,
811                                           NULL, DECODE(D.DEMAND_CLASS, null, null,
812                                              DECODE(p_demand_class, '-1',
813                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
814                                                    null, null, p_item_id, p_org_id,
815                                                    p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
816                                                 D.DEMAND_CLASS)),
817                                           -23453, DECODE(D.DEMAND_CLASS, null, null,
818                                              DECODE(p_demand_class, '-1',
819                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
820                                                    null, null, p_item_id, p_org_id,
821                                                    p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
822                                                 D.DEMAND_CLASS)),
823                                           d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
824                                              DECODE(p_demand_class, '-1',
825                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
826                                                    null, null, p_item_id, p_org_id,
827                                                    p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
828                                                 D.DEMAND_CLASS)),
829                                           p_demand_class),
830                                     30, decode(d.source_organization_id,
831                                        NULL, DECODE(D.DEMAND_CLASS, null, null,
832                                           DECODE(p_demand_class, '-1',
833                                              MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
834                                                 null, null, p_item_id, p_org_id,
835                                                 p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
836                                              D.DEMAND_CLASS)),
837                                        -23453, DECODE(D.DEMAND_CLASS, null, null,
838                                           DECODE(p_demand_class, '-1',
839                                              MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
840                                                 null, null, p_item_id, p_org_id,
841                                                 p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
842                                              D.DEMAND_CLASS)),
843                                        d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
844                                           DECODE(p_demand_class, '-1',
845                                              MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
846                                                 null, null, p_item_id, p_org_id,
847                                                 p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
848                                              D.DEMAND_CLASS)),
849                                        p_demand_class),
850                                     DECODE(D.DEMAND_CLASS, null, null,
851                                        DECODE(p_demand_class, '-1',
852                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
853                                              null, null, p_item_id, p_org_id,
854                                              p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
855                                           D.DEMAND_CLASS))),
856                                     -- rajjain end
857                                     2, DECODE(D.CUSTOMER_ID, NULL, p_demand_class,
858                                                    0, p_demand_class,
859                                        -- rajjain begin 07/19/2002
860                                        decode(decode (d.origination_type, -100, 30,d.origination_type),  --5027568
861                                           6, decode(d.source_organization_id,
862                                              NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
863                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
864                                                    p_org_id, p_instance_id, c.calendar_date,
865                                                    p_level_id, NULL),
866                                              -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
867                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
868                                                    p_org_id, p_instance_id, c.calendar_date,
869                                                    p_level_id, NULL),
870                                              d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
871                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
872                                                    p_org_id, p_instance_id, c.calendar_date,
873                                                    p_level_id, NULL),
874                                              p_demand_class),
875                                           30, decode(d.source_organization_id,
876                                              NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
877                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
878                                                    p_org_id, p_instance_id, c.calendar_date,
879                                                    p_level_id, NULL),
880                                              -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
881                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
882                                                    p_org_id, p_instance_id, c.calendar_date,
883                                                    p_level_id, NULL),
884                                              d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
885                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
886                                                    p_org_id, p_instance_id, c.calendar_date,
887                                                    p_level_id, NULL),
888                                              p_demand_class),
889                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(D.CUSTOMER_ID, D.SHIP_TO_SITE_ID,
890                                              p_item_id, p_org_id, p_instance_id,
891                                              c.calendar_date,p_level_id, NULL)))),
892                                        -- rajjain end 07/19/2002
893                                        p_demand_class,
894                                        p_level_id),0)) SD_QTY --4365873
895                    FROM    MSC_CALENDAR_DATES C,
896                            MSC_DEMANDS D
897                    WHERE        D.PLAN_ID = p_plan_id
898                    AND                D.SR_INSTANCE_ID = p_instance_id
899                    AND                D.INVENTORY_ITEM_ID = p_item_id
900                    AND         D.ORGANIZATION_ID = p_org_id
901                    AND         D.USING_REQUIREMENT_QUANTITY <> 0 --4501434
902                    AND         D.ORIGINATION_TYPE NOT IN (5,7,8,9,11,15,22,28,29,31,52) -- For summary enhancement
903                    -- Bug1990155, 1995835 exclude the expired lots demand datreya 9/18/2001
904                    -- Bug 1530311, need to exclude forecast, ngoel 12/05/2000
905                    AND                C.CALENDAR_CODE = p_cal_code
906                    AND                C.EXCEPTION_SET_ID = p_cal_exc_set_id
907                    AND         C.SR_INSTANCE_ID = D.SR_INSTANCE_ID
908                    -- since we store repetitive schedule demand in different ways for
909                    -- ods (total quantity on start date) and pds  (daily quantity from
910                    -- start date to end date), we need to make sure we only select work day
911                    -- for pds's repetitive schedule demand.
912                    AND         C.CALENDAR_DATE
913                    -- Bug 3550296 and 3574164. IMPLEMENT_DATE AND DMD_SATISFIED_DATE are changed to
914                    -- IMPLEMENT_SHIP_DATE and PLANNED_SHIP_DATE resp.
915                                BETWEEN TRUNC(DECODE(RECORD_SOURCE,
916                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
917                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
918                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
919                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))))
920                                AND     TRUNC(NVL(D.ASSEMBLY_DEMAND_COMP_DATE,
921                                              DECODE(RECORD_SOURCE,
922                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
923                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
924                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
925                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))))
926                    AND         (( D.ORIGINATION_TYPE = 4
927                            AND C.SEQ_NUM IS NOT NULL) OR
928                            ( D.ORIGINATION_TYPE  <> 4))
929                    -- AND         C.PRIOR_DATE < NVL(p_itf,
930                    --                  C.PRIOR_DATE + 1)
931                    AND         C.CALENDAR_DATE < NVL(p_itf, C.CALENDAR_DATE+1)
932                    UNION ALL
933                    SELECT  ---C.CALENDAR_DATE SD_DATE,
934                            GREATEST(CS.CALENDAR_DATE,p_sys_next_date) SD_DATE,--3099066
935                            NVL(CS.FIRM_QUANTITY,CS.NEW_ORDER_QUANTITY)*
936                               DECODE(DECODE(G_HIERARCHY_PROFILE,
937                                      --2424357: Convert the demand calls in case of others for
938                                      --- demand class allocated ATP
939                                      1, DECODE(CS.DEMAND_CLASS, null, null,
940                                         DECODE(p_demand_class, '-1',
941                                            MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
942                                                          null,
943                                                          null,
944                                                          cs.inventory_item_id,
945                                                          p_org_id,
946                                                          p_instance_id,
947                                                          cs.calendar_date,
948                                                          p_level_id,
949                                                          CS.DEMAND_CLASS),CS.DEMAND_CLASS)),
950                                      2, DECODE(CS.CUSTOMER_ID, NULL, TO_CHAR(NULL),
951                                                        0, TO_CHAR(NULL),
952                                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
953                                                          CS.CUSTOMER_ID,
954                                                          CS.SHIP_TO_SITE_ID,
955                                                          cs.inventory_item_id,
956                                                          p_org_id,
957                                                          p_instance_id,
958                                                          cs.calendar_date,
959                                                          p_level_id,
960                                                          NULL))),
961                                  p_demand_class, 1,
962                                  NULL, nvl(MIHM.allocation_percent/100,1), --4365873
963                                  /*NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
964                                            p_instance_id,
965                                            cs.inventory_item_id,
966                                            p_org_id,
967                                            null,
968                                            null,
969                                            p_demand_class,
970                                            cs.calendar_date), 1),*/
971                                  DECODE(
972                                  MIHM.allocation_percent/100, --4365873
973                                  /*DECODE(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
974                                            p_instance_id,
975                                            cs.inventory_item_id,
976                                            p_org_id,
977                                            null,
978                                            null,
979                                            p_demand_class,
980                                            cs.calendar_date),*/
981                                    NULL, 1,
982                                  0)) SD_QTY
983                 FROM
984                 (
985                 select
986                 	C.NEXT_DATE,
987                 	C.CALENDAR_DATE,
988 			S.FIRM_QUANTITY,
989 			S.NEW_ORDER_QUANTITY,
990 			S.DEMAND_CLASS,
991 			s.inventory_item_id,
992 			S.CUSTOMER_ID,
993 			S.SHIP_TO_SITE_ID,
994 			S.SR_INSTANCE_ID,
995 			S.ORGANIZATION_ID
996                    FROM    MSC_CALENDAR_DATES C,
997                            MSC_SUPPLIES S
998                    WHERE   S.PLAN_ID = p_plan_id
999                    AND     S.SR_INSTANCE_ID = p_instance_id
1000                    AND     S.INVENTORY_ITEM_ID = p_item_id
1001                    AND     S.ORGANIZATION_ID = p_org_id
1002                            -- Exclude Cancelled Supplies 2460645
1003                    AND     NVL(S.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
1004                    AND     NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY) <> 0 -- 1243985
1005                    AND     C.CALENDAR_CODE = p_cal_code
1006                    AND     C.EXCEPTION_SET_ID = p_cal_exc_set_id
1007                    AND     C.SR_INSTANCE_ID = S.SR_INSTANCE_ID
1008                    AND     C.CALENDAR_DATE BETWEEN TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE))
1009                            AND TRUNC(NVL(S.LAST_UNIT_COMPLETION_DATE,
1010                                NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)))
1011                    AND     DECODE(S.LAST_UNIT_COMPLETION_DATE,
1012                                       NULL, C.NEXT_SEQ_NUM, C.SEQ_NUM) IS NOT NULL
1013                    -- 2859130
1014                    AND     C.CALENDAR_DATE >= DECODE(S.ORDER_TYPE, 27, TRUNC(SYSDATE),
1015                                                                    28, TRUNC(SYSDATE),
1016                                                                    C.CALENDAR_DATE)
1017                    AND     C.CALENDAR_DATE < NVL(p_itf,
1018                                             C.CALENDAR_DATE + 1)
1019                    --AND     C.NEXT_DATE >= DECODE(S.ORDER_TYPE, 27, TRUNC(SYSDATE),
1020                    --                                            28, TRUNC(SYSDATE),
1021                    --                                            C.NEXT_DATE)
1022                    --AND     C.NEXT_DATE < NVL(p_itf,
1023                    --                          C.NEXT_DATE + 1)
1024                    )CS,
1025 		MSC_ITEM_HIERARCHY_MV MIHM
1026 		WHERE
1027 		--4365873
1028                        CS.INVENTORY_ITEM_ID = MIHM.INVENTORY_ITEM_ID(+)
1029                 AND    CS.SR_INSTANCE_ID = MIHM.SR_INSTANCE_ID (+)
1030                 AND    CS.ORGANIZATION_ID = MIHM.ORGANIZATION_ID (+)
1031                 AND    decode(MIHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
1032                 AND    CS.NEXT_DATE >= MIHM.effective_date (+)
1033                 AND    CS.NEXT_DATE <= MIHM.disable_date (+)
1034                 AND    MIHM.demand_class (+) = p_demand_class
1035                    )
1036                 GROUP BY SD_DATE
1037                 ORDER BY SD_DATE;--4698199
1038 END item_alloc_avail_unopt;
1039 --avjain All netting sqls have been changed to incorporate Plan by Request Date Enhancements
1040 PROCEDURE item_alloc_avail_opt_unalloc (
1041    p_item_id            IN NUMBER,
1042    p_org_id             IN NUMBER,
1043    p_instance_id        IN NUMBER,
1044    p_plan_id            IN NUMBER,
1045    p_demand_class       IN VARCHAR2,
1046    p_level_id           IN NUMBER,
1047    p_itf                IN DATE,
1048    p_cal_code           IN VARCHAR2,
1049    p_cal_exc_set_id     IN NUMBER,
1050    p_sys_next_date	IN DATE,                        --bug3099066
1051    x_atp_dates          OUT NoCopy MRP_ATP_PUB.date_arr,
1052    x_atp_qtys           OUT NoCopy MRP_ATP_PUB.number_arr,
1053    x_atp_unalloc_qtys   OUT NoCopy MRP_ATP_PUB.number_arr
1054 ) IS
1055 BEGIN
1056         IF PG_DEBUG in ('Y', 'C') THEN
1057                 msc_sch_wb.atp_debug('item_alloc_avail_opt_unalloc: Get unallocated qtys as well. constrained plan: ' || p_plan_id);
1058         END IF;
1059               -- Bug 3550296 and 3574164. IMPLEMENT_DATE AND DMD_SATISFIED_DATE are changed to
1060               -- IMPLEMENT_SHIP_DATE and PLANNED_SHIP_DATE resp.
1061               SELECT        SD_DATE,
1062                            SUM(UNALLOC_SD_QTY),
1063                            SUM(SD_QTY)
1064              BULK COLLECT INTO
1065                            x_atp_dates,
1066                            x_atp_unalloc_qtys,
1067                            x_atp_qtys
1068              FROM (
1069                    SELECT  -- C.PRIOR_DATE SD_DATE, -- 2859130
1070 			GREATEST(
1071                            TRUNC(DECODE(RECORD_SOURCE,
1072                            	2,
1073                            	NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1074                            	DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1075                            	2,
1076                            	(NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
1077                            	NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))))
1078                            	,p_sys_next_date) SD_DATE, --3099066 --plan by request date
1079 
1080                            -1* (D.USING_REQUIREMENT_QUANTITY - NVL(d.reserved_quantity, 0)) UNALLOC_SD_QTY, --5027568
1081                            -1* (D.USING_REQUIREMENT_QUANTITY - NVL(d.reserved_quantity, 0))* --5027568
1082                            DECODE(DECODE(G_HIERARCHY_PROFILE,
1083                            /*------------------------------------------------------------------------+
1084                            | rajjain begin 07/19/2002                                                |
1085                            |                                                                         |
1086                            | Case 1: For internal sales orders [origination type is in (6,30) and    |
1087                            |            source_organization_id is not null and <> organization_id]   |
1088                            |                  Return NULL                                            |
1089                            | Case 2: For others if DEMAND_CLASS is null then return null             |
1090                            |          else if p_demand_class is '-1' then call                       |
1091                            |            Get_Hierarchy_Demand_class else return DEMAND_CLASS          |
1092                            +------------------------------------------------------------------------*/
1093                               1, decode(decode (d.origination_type, -100, 30,d.origination_type),  --5027568
1094                                  6, decode(d.source_organization_id,
1095                                     NULL, DECODE(D.DEMAND_CLASS, null, null,
1096                                        DECODE(p_demand_class, '-1',
1097                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1098                                              null, null, p_item_id, p_org_id,
1099                                              p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1100                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1101                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1102                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1103                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
1104                                              D.DEMAND_CLASS)),
1105                                     -23453, DECODE(D.DEMAND_CLASS, null, null,
1106                                        DECODE(p_demand_class, '-1',
1107                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1108                                              null, null, p_item_id, p_org_id,
1109                                              p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1110                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1111                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1112                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1113                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
1114                                              D.DEMAND_CLASS)),
1115                                     d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
1116                                        DECODE(p_demand_class, '-1',
1117                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1118                                              null, null, p_item_id, p_org_id,
1119                                              p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1120                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1121                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1122                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1123                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
1124                                              D.DEMAND_CLASS)), TO_CHAR(NULL)),
1125                                  30, decode(d.source_organization_id,
1126                                     NULL, DECODE(D.DEMAND_CLASS, null, null,
1127                                        DECODE(p_demand_class, '-1',
1128                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1129                                              null, null, p_item_id, p_org_id,
1130                                              p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1131                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1132                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1133                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1134                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
1135                                              D.DEMAND_CLASS)),
1136                                     -23453, DECODE(D.DEMAND_CLASS, null, null,
1137                                        DECODE(p_demand_class, '-1',
1138                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1139                                              null, null, p_item_id, p_org_id,
1140                                              p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1141                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1142                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1143                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1144                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
1145                                              D.DEMAND_CLASS)),
1146                                     d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
1147                                        DECODE(p_demand_class, '-1',
1148                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1149                                              null, null, p_item_id, p_org_id,
1150                                              p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1151                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1152                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1153                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1154                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
1155                                              D.DEMAND_CLASS)), TO_CHAR(NULL)),
1156                                  DECODE(D.DEMAND_CLASS, null, null,
1157                                     DECODE(p_demand_class, '-1',
1158                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1159                                           null, null, p_item_id, p_org_id,
1160                                           p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1161                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1162                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1163                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1164                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
1165                                           D.DEMAND_CLASS))),
1166                               -- rajjain end
1167                               2, DECODE(D.CUSTOMER_ID, NULL, TO_CHAR(NULL),
1168                                                    0, TO_CHAR(NULL),
1169                                  -- rajjain begin 07/19/2002
1170                                  decode(decode (d.origination_type, -100, 30,d.origination_type),  --5027568
1171                                     6, decode(d.source_organization_id,
1172                                        NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1173                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1174                                              p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1175                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1176                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1177                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1178                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
1179                                              p_level_id, NULL),
1180                                        -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1181                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1182                                              p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1183                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1184                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1185                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1186                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
1187                                              p_level_id, NULL),
1188                                        d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1189                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1190                                              p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1191                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1192                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1193                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1194                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
1195                                              p_level_id, NULL),
1196                                        TO_CHAR(NULL)),
1197                                     30, decode(d.source_organization_id,
1198                                        NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1199                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1200                                              p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1201                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1202                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1203                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1204                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
1205                                              p_level_id, NULL),
1206                                        -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1207                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1208                                              p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1209                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1210                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1211                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1212                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
1213                                              p_level_id, NULL),
1214                                        d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1215                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1216                                              p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1217                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1218                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1219                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1220                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
1221                                              p_level_id, NULL),
1222                                        TO_CHAR(NULL)),
1223                                     MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(D.CUSTOMER_ID, D.SHIP_TO_SITE_ID,
1224                                        p_item_id, p_org_id, p_instance_id,
1225                                        TRUNC(DECODE(RECORD_SOURCE,
1226                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1227                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1228                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1229                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),p_level_id, NULL)))),
1230                                  -- rajjain end 07/19/2002
1231                            p_demand_class, 1,
1232                               Decode(D.Demand_Class, NULL, --4365873
1233                               MSC_AATP_FUNC.Get_Item_Demand_Alloc_Percent(p_plan_id,
1234                                  D.DEMAND_ID,
1235                                  TRUNC(DECODE(RECORD_SOURCE,
1236                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1237                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1238                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1239                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
1240                                  D.USING_ASSEMBLY_ITEM_ID,
1241                                  DECODE(D.SOURCE_ORGANIZATION_ID,
1242                                     -23453, null,
1243                                     D.SOURCE_ORGANIZATION_ID), -- 1665483
1244                                  p_item_id,
1245                                  p_org_id, -- 1665483
1246                                  p_instance_id,
1247                                  decode (d.origination_type, -100, 30,d.origination_type), --5027568
1248                                  DECODE(G_HIERARCHY_PROFILE,
1249                                  /*-----------------------------------------------------------------+
1250                                  | rajjain begin 07/19/2002                                         |
1251                                  |                                                                  |
1252                                  | Case 1: For internal sales orders [origination type is in (6,30) |
1253                                  |         and source_organization_id is not null                   |
1254                                  |         and <> organization_id] -> Return p_demand_class         |
1255                                  | Case 2: For others if DEMAND_CLASS is null then return null      |
1256                                  |           else if p_demand_class is '-1' then call               |
1257                                  |           Get_Hierarchy_Demand_class else return DEMAND_CLASS    |
1258                                  +-----------------------------------------------------------------*/
1259                                     1, decode(decode (d.origination_type, -100, 30,d.origination_type),  --5027568
1260                                        6, decode(d.source_organization_id,
1261                                           NULL, DECODE(D.DEMAND_CLASS, null, null,
1262                                              DECODE(p_demand_class, '-1',
1263                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1264                                                    null, null, p_item_id, p_org_id,
1265                                                    p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1266                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1267                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1268                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1269                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
1270                                                 D.DEMAND_CLASS)),
1271                                           -23453, DECODE(D.DEMAND_CLASS, null, null,
1272                                              DECODE(p_demand_class, '-1',
1273                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1274                                                    null, null, p_item_id, p_org_id,
1275                                                    p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1276                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1277                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1278                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1279                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
1280                                                 D.DEMAND_CLASS)),
1281                                           d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
1282                                              DECODE(p_demand_class, '-1',
1283                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1284                                                    null, null, p_item_id, p_org_id,
1285                                                    p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1286                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1287                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1288                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1289                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
1290                                                 D.DEMAND_CLASS)),
1291                                           p_demand_class),
1292                                     30, decode(d.source_organization_id,
1293                                        NULL, DECODE(D.DEMAND_CLASS, null, null,
1294                                           DECODE(p_demand_class, '-1',
1295                                              MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1296                                                 null, null, p_item_id, p_org_id,
1297                                                 p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1298                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1299                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1300                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1301                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
1302                                              D.DEMAND_CLASS)),
1303                                        -23453, DECODE(D.DEMAND_CLASS, null, null,
1304                                           DECODE(p_demand_class, '-1',
1305                                              MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1306                                                 null, null, p_item_id, p_org_id,
1307                                                 p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1308                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1309                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1310                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1311                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
1312                                              D.DEMAND_CLASS)),
1313                                        d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
1314                                           DECODE(p_demand_class, '-1',
1315                                              MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1316                                                 null, null, p_item_id, p_org_id,
1317                                                 p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1318                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1319                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1320                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1321                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
1322                                              D.DEMAND_CLASS)),
1323                                        p_demand_class),
1324                                     DECODE(D.DEMAND_CLASS, null, null,
1325                                        DECODE(p_demand_class, '-1',
1326                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1327                                              null, null, p_item_id, p_org_id,
1328                                              p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1329                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1330                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1331                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1332                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
1333                                           D.DEMAND_CLASS))),
1334                                     -- rajjain end
1335                                     2, DECODE(D.CUSTOMER_ID, NULL, p_demand_class,
1336                                                    0, p_demand_class,
1337                                        -- rajjain begin 07/19/2002
1338                                        decode(decode (d.origination_type, -100, 30,d.origination_type),  --5027568
1339                                           6, decode(d.source_organization_id,
1340                                              NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1341                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1342                                                    p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1343                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1344                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1345                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1346                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
1347                                                    p_level_id, NULL),
1348                                              -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1349                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1350                                                    p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1351                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1352                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1353                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1354                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
1355                                                    p_level_id, NULL),
1356                                              d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1357                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1358                                                    p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1359                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1360                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1361                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1362                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
1363                                                    p_level_id, NULL),
1364                                              p_demand_class),
1365                                           30, decode(d.source_organization_id,
1366                                              NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1367                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1368                                                    p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1369                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1370                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1371                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1372                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
1373                                                    p_level_id, NULL),
1374                                              -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1375                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1376                                                    p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1377                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1378                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1379                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1380                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
1381                                                    p_level_id, NULL),
1382                                              d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1383                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1384                                                    p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1385                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1386                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1387                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1388                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
1389                                                    p_level_id, NULL),
1390                                              p_demand_class),
1391                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(D.CUSTOMER_ID, D.SHIP_TO_SITE_ID,
1392                                              p_item_id, p_org_id, p_instance_id,
1393                                              TRUNC(DECODE(RECORD_SOURCE,
1394                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1395                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1396                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1397                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),p_level_id, NULL)))),
1398                                        -- rajjain end 07/19/2002
1399                                        p_demand_class,
1400                                        p_level_id),0)) SD_QTY --4365873
1401                    FROM    MSC_DEMANDS D
1402                    WHERE        D.PLAN_ID = p_plan_id
1403                    AND                D.SR_INSTANCE_ID = p_instance_id
1404                    AND                D.INVENTORY_ITEM_ID = p_item_id
1405                    AND         D.ORGANIZATION_ID = p_org_id
1406                    AND         D.USING_REQUIREMENT_QUANTITY <> 0 --4501434
1407                    AND         D.ORIGINATION_TYPE NOT IN (5,7,8,9,11,15,22,28,29,31,52) -- For summary enhancement
1408                    -- Bug1990155, 1995835 exclude the expired lots demand datreya 9/18/2001
1409                    -- Bug 1530311, need to exclude forecast, ngoel 12/05/2000
1410                    --AND                C.CALENDAR_CODE = p_cal_code
1411                    --AND                C.EXCEPTION_SET_ID = p_cal_exc_set_id
1412                    --AND         C.SR_INSTANCE_ID = D.SR_INSTANCE_ID
1413                    -- since we store repetitive schedule demand in different ways for
1414                    -- ods (total quantity on start date) and pds  (daily quantity from
1415                    -- start date to end date), we need to make sure we only select work day
1416                    -- for pds's repetitive schedule demand.
1417                    -- 2859130 repetitive schedule not supported for constrained plan
1418                    --AND         C.CALENDAR_DATE BETWEEN TRUNC(D.USING_ASSEMBLY_DEMAND_DATE) AND
1419                    --            TRUNC(NVL(D.ASSEMBLY_DEMAND_COMP_DATE,
1420                    --                      D.USING_ASSEMBLY_DEMAND_DATE))
1421                    --AND         (( D.ORIGINATION_TYPE = 4
1422                    --        AND C.SEQ_NUM IS NOT NULL) OR
1423                    --        ( D.ORIGINATION_TYPE  <> 4))
1424                    -- AND         C.PRIOR_DATE < NVL(p_itf,
1425                    --                 C.PRIOR_DATE + 1)
1426                    --bug3693892 added trunc
1427                    AND         TRUNC(DECODE(RECORD_SOURCE,
1428                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1429                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1430                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1431                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))) <
1432                                TRUNC(NVL(p_itf,DECODE(RECORD_SOURCE,
1433                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1434                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1435                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1436                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))) + 1))
1437                    UNION ALL
1438                    SELECT  -- C.NEXT_DATE SD_DATE, -- 2859130
1439                            -- TRUNC(NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE)) SD_DATE,
1440                            greatest(TRUNC(NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE)),p_sys_next_date) SD_DATE,--3099066
1441                            NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY) UNALLOC_SD_QTY,
1442                            NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY)*
1443                               DECODE(DECODE(G_HIERARCHY_PROFILE,
1444                                      --2424357: Convert the demand calls in case of others for
1445                                      --- demand class allocated ATP
1446                                      1, DECODE(S.DEMAND_CLASS, null, null,
1447                                         DECODE(p_demand_class, '-1',
1448                                            MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1449                                                          null,
1450                                                          null,
1451                                                          S.inventory_item_id,
1452                                                          p_org_id,
1453                                                          p_instance_id,
1454                                                          trunc(nvl(S.firm_date, S.new_schedule_date)),
1455                                                          p_level_id,
1456                                                          S.DEMAND_CLASS),S.DEMAND_CLASS)),
1457                                      2, DECODE(S.CUSTOMER_ID, NULL, TO_CHAR(NULL),
1458                                                        0, TO_CHAR(NULL),
1459                                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1460                                                          S.CUSTOMER_ID,
1461                                                          S.SHIP_TO_SITE_ID,
1462                                                          S.inventory_item_id,
1463                                                          p_org_id,
1464                                                          p_instance_id,
1465                                                          trunc(nvl(S.firm_date, S.new_schedule_date)),
1466                                                          p_level_id,
1467                                                          NULL))),
1468                                  p_demand_class, 1,
1469                                  NULL, nvl(MIHM.allocation_percent/100,1), --4365873
1470                                  /*NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
1471                                            p_instance_id,
1472                                            s.inventory_item_id,
1473                                            p_org_id,
1474                                            null,
1475                                            null,
1476                                            p_demand_class,
1477                                            trunc(nvl(s.firm_date, s.new_schedule_date))), 1),*/
1478                                  DECODE(MIHM.allocation_percent/100, --4365873
1479                                  /*MSC_AATP_FUNC.Get_DC_Alloc_Percent(
1480                                            p_instance_id,
1481                                            s.inventory_item_id,
1482                                            p_org_id,
1483                                            null,
1484                                            null,
1485                                            p_demand_class,
1486                                            trunc(nvl(s.firm_date, s.new_schedule_date))),*/
1487                                    NULL, 1,
1488                                  0)) SD_QTY
1489                    FROM    -- MSC_CALENDAR_DATES C,
1490                            MSC_SUPPLIES S,MSC_ITEM_HIERARCHY_MV MIHM
1491                    WHERE   S.PLAN_ID = p_plan_id
1492                    AND     S.SR_INSTANCE_ID = p_instance_id
1493                    AND     S.INVENTORY_ITEM_ID = p_item_id
1494                    AND     S.ORGANIZATION_ID = p_org_id
1495                            -- Exclude Cancelled Supplies 2460645
1496                    AND     NVL(S.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
1497                    AND     NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY) <> 0 -- 1243985
1498                    --AND     C.CALENDAR_CODE = p_cal_code
1499                    --AND     C.EXCEPTION_SET_ID = p_cal_exc_set_id
1500                    --AND     C.SR_INSTANCE_ID = S.SR_INSTANCE_ID
1501                    --AND     C.CALENDAR_DATE BETWEEN TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE))
1502                    --        AND TRUNC(NVL(S.LAST_UNIT_COMPLETION_DATE,
1503                    --            NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)))
1504                    --AND     DECODE(S.LAST_UNIT_COMPLETION_DATE,
1505                    --                   NULL, C.NEXT_SEQ_NUM, C.SEQ_NUM) IS NOT NULL
1506                    -- 2859130
1507                    -- AND     C.NEXT_DATE >= DECODE(S.ORDER_TYPE, 27, TRUNC(SYSDATE),
1508                    --                                             28, TRUNC(SYSDATE),
1509                    --                                             C.NEXT_DATE)
1510                    -- AND     C.NEXT_DATE < NVL(p_itf,
1511                    --                           C.NEXT_DATE + 1)
1512                    --AND     C.CALENDAR_DATE >= DECODE(S.ORDER_TYPE, 27, TRUNC(SYSDATE),
1513                    --                                            28, TRUNC(SYSDATE),
1514                    --                                            C.CALENDAR_DATE)
1515                    AND     trunc(NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE)) >= trunc(DECODE(S.ORDER_TYPE, 27, SYSDATE,
1516                                                                28, SYSDATE,
1517                                                                trunc(NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE)))) --4135752
1518                    AND     trunc(NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE)) < NVL(p_itf,
1519                                              trunc(NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE)) + 1)
1520 		--4365873
1521                 AND    S.INVENTORY_ITEM_ID = MIHM.INVENTORY_ITEM_ID(+)
1522                 AND    S.SR_INSTANCE_ID = MIHM.SR_INSTANCE_ID (+)
1523                 AND    S.ORGANIZATION_ID = MIHM.ORGANIZATION_ID (+)
1524                 AND    decode(MIHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
1525                 AND    trunc(NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE)) >= MIHM.effective_date (+)
1526                 AND    trunc(NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE)) <= MIHM.disable_date (+)
1527                 AND    MIHM.demand_class (+) = p_demand_class
1528                    )
1529              GROUP BY SD_DATE
1530              ORDER BY SD_DATE;--4698199
1531 
1532 END item_alloc_avail_opt_unalloc;
1533 --avjain All netting sqls have been changed to incorporate Plan by Request Date Enhancements
1534 PROCEDURE item_alloc_avail_unopt_unalloc (
1535    p_item_id            IN NUMBER,
1536    p_org_id             IN NUMBER,
1537    p_instance_id        IN NUMBER,
1538    p_plan_id            IN NUMBER,
1539    p_demand_class       IN VARCHAR2,
1540    p_level_id           IN NUMBER,
1541    p_itf                IN DATE,
1542    p_cal_code           IN VARCHAR2,
1543    p_cal_exc_set_id     IN NUMBER,
1544    p_sys_next_date	IN DATE,			--bug3099066
1545    x_atp_dates          OUT NoCopy MRP_ATP_PUB.date_arr,
1546    x_atp_qtys           OUT NoCopy MRP_ATP_PUB.number_arr,
1547    x_atp_unalloc_qtys   OUT NoCopy MRP_ATP_PUB.number_arr
1548 ) IS
1549 BEGIN
1550         IF PG_DEBUG in ('Y', 'C') THEN
1551                 msc_sch_wb.atp_debug('item_alloc_avail_unopt_unalloc: Get unallocated qtys as well. UNconstrained plan: ' || p_plan_id);
1552         END IF;
1553 
1554              SELECT        SD_DATE,
1555                            SUM(UNALLOC_SD_QTY),
1556                            SUM(SD_QTY)
1557              BULK COLLECT INTO
1558                            x_atp_dates,
1559                            x_atp_unalloc_qtys,
1560                            x_atp_qtys
1561              FROM (
1562                    SELECT  --C.CALENDAR_DATE SD_DATE, -- 2859130 change to calendar date
1563                    	   GREATEST(C.CALENDAR_DATE,p_sys_next_date) SD_DATE,--3099066
1564                            -1* DECODE(D.ORIGINATION_TYPE,
1565                                   4, D.DAILY_DEMAND_RATE,
1566                                   --D.USING_REQUIREMENT_QUANTITY) UNALLOC_SD_QTY,
1567                                   (D.USING_REQUIREMENT_QUANTITY - NVL(d.reserved_quantity, 0))) UNALLOC_SD_QTY, --5027568
1568                            -1* DECODE(D.ORIGINATION_TYPE,
1569                                   4, D.DAILY_DEMAND_RATE,
1570                                   --D.USING_REQUIREMENT_QUANTITY)*
1571                                   (D.USING_REQUIREMENT_QUANTITY - NVL(d.reserved_quantity, 0)))* --5027568
1572                            DECODE(DECODE(G_HIERARCHY_PROFILE,
1573                            /*------------------------------------------------------------------------+
1574                            | rajjain begin 07/19/2002                                                |
1575                            |                                                                         |
1576                            | Case 1: For internal sales orders [origination type is in (6,30) and    |
1577                            |            source_organization_id is not null and <> organization_id]   |
1578                            |                  Return NULL                                            |
1579                            | Case 2: For others if DEMAND_CLASS is null then return null             |
1580                            |          else if p_demand_class is '-1' then call                       |
1581                            |            Get_Hierarchy_Demand_class else return DEMAND_CLASS          |
1582                            +------------------------------------------------------------------------*/
1583                               1, decode(decode (d.origination_type, -100, 30,d.origination_type),  --5027568
1584                                  6, decode(d.source_organization_id,
1585                                     NULL, DECODE(D.DEMAND_CLASS, null, null,
1586                                        DECODE(p_demand_class, '-1',
1587                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1588                                              null, null, p_item_id, p_org_id,
1589                                              p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
1590                                              D.DEMAND_CLASS)),
1591                                     -23453, DECODE(D.DEMAND_CLASS, null, null,
1592                                        DECODE(p_demand_class, '-1',
1593                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1594                                              null, null, p_item_id, p_org_id,
1595                                              p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
1596                                              D.DEMAND_CLASS)),
1597                                     d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
1598                                        DECODE(p_demand_class, '-1',
1599                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1600                                              null, null, p_item_id, p_org_id,
1601                                              p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
1602                                              D.DEMAND_CLASS)), TO_CHAR(NULL)),
1603                                  30, decode(d.source_organization_id,
1604                                     NULL, DECODE(D.DEMAND_CLASS, null, null,
1605                                        DECODE(p_demand_class, '-1',
1606                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1607                                              null, null, p_item_id, p_org_id,
1608                                              p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
1609                                              D.DEMAND_CLASS)),
1610                                     -23453, DECODE(D.DEMAND_CLASS, null, null,
1611                                        DECODE(p_demand_class, '-1',
1612                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1613                                              null, null, p_item_id, p_org_id,
1614                                              p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
1615                                              D.DEMAND_CLASS)),
1616                                     d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
1617                                        DECODE(p_demand_class, '-1',
1618                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1619                                              null, null, p_item_id, p_org_id,
1620                                              p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
1621                                              D.DEMAND_CLASS)), TO_CHAR(NULL)),
1622                                  DECODE(D.DEMAND_CLASS, null, null,
1623                                     DECODE(p_demand_class, '-1',
1624                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1625                                           null, null, p_item_id, p_org_id,
1626                                           p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
1627                                           D.DEMAND_CLASS))),
1628                               -- rajjain end
1629                               2, DECODE(D.CUSTOMER_ID, NULL, TO_CHAR(NULL),
1630                                                    0, TO_CHAR(NULL),
1631                                  -- rajjain begin 07/19/2002
1632                                  decode(decode (d.origination_type, -100, 30,d.origination_type),  --5027568
1633                                     6, decode(d.source_organization_id,
1634                                        NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1635                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1636                                              p_org_id, p_instance_id, c.calendar_date,
1637                                              p_level_id, NULL),
1638                                        -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1639                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1640                                              p_org_id, p_instance_id, c.calendar_date,
1641                                              p_level_id, NULL),
1642                                        d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1643                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1644                                              p_org_id, p_instance_id, c.calendar_date,
1645                                              p_level_id, NULL),
1646                                        TO_CHAR(NULL)),
1647                                     30, decode(d.source_organization_id,
1648                                        NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1649                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1650                                              p_org_id, p_instance_id, c.calendar_date,
1651                                              p_level_id, NULL),
1652                                        -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1653                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1654                                              p_org_id, p_instance_id, c.calendar_date,
1655                                              p_level_id, NULL),
1656                                        d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1657                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1658                                              p_org_id, p_instance_id, c.calendar_date,
1659                                              p_level_id, NULL),
1660                                        TO_CHAR(NULL)),
1661                                     MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(D.CUSTOMER_ID, D.SHIP_TO_SITE_ID,
1662                                        p_item_id, p_org_id, p_instance_id,
1663                                        c.calendar_date,p_level_id, NULL)))),
1664                                  -- rajjain end 07/19/2002
1665                            p_demand_class, 1,
1666                              Decode(D.Demand_Class, NULL, --4365873
1667                               MSC_AATP_FUNC.Get_Item_Demand_Alloc_Percent(p_plan_id,
1668                                  D.DEMAND_ID,
1669                                  c.calendar_date,
1670                                  D.USING_ASSEMBLY_ITEM_ID,
1671                                  DECODE(D.SOURCE_ORGANIZATION_ID,
1672                                     -23453, null,
1673                                     D.SOURCE_ORGANIZATION_ID), -- 1665483
1674                                  p_item_id,
1675                                  p_org_id, -- 1665483
1676                                  p_instance_id,
1677                                  DECODE (D.ORIGINATION_TYPE, -100, 30,D.ORIGINATION_TYPE) , --5027568
1678                                  DECODE(G_HIERARCHY_PROFILE,
1679                                  /*-----------------------------------------------------------------+
1680                                  | rajjain begin 07/19/2002                                         |
1681                                  |                                                                  |
1682                                  | Case 1: For internal sales orders [origination type is in (6,30) |
1683                                  |         and source_organization_id is not null                   |
1684                                  |         and <> organization_id] -> Return p_demand_class         |
1685                                  | Case 2: For others if DEMAND_CLASS is null then return null      |
1686                                  |           else if p_demand_class is '-1' then call               |
1687                                  |           Get_Hierarchy_Demand_class else return DEMAND_CLASS    |
1688                                  +-----------------------------------------------------------------*/
1689                                     1, decode(decode (d.origination_type, -100, 30,d.origination_type),  --5027568
1690                                        6, decode(d.source_organization_id,
1691                                           NULL, DECODE(D.DEMAND_CLASS, null, null,
1692                                              DECODE(p_demand_class, '-1',
1693                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1694                                                    null, null, p_item_id, p_org_id,
1695                                                    p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
1696                                                 D.DEMAND_CLASS)),
1697                                           -23453, DECODE(D.DEMAND_CLASS, null, null,
1698                                              DECODE(p_demand_class, '-1',
1699                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1700                                                    null, null, p_item_id, p_org_id,
1701                                                    p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
1702                                                 D.DEMAND_CLASS)),
1703                                           d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
1704                                              DECODE(p_demand_class, '-1',
1705                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1706                                                    null, null, p_item_id, p_org_id,
1707                                                    p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
1708                                                 D.DEMAND_CLASS)),
1709                                           p_demand_class),
1710                                     30, decode(d.source_organization_id,
1711                                        NULL, DECODE(D.DEMAND_CLASS, null, null,
1712                                           DECODE(p_demand_class, '-1',
1713                                              MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1714                                                 null, null, p_item_id, p_org_id,
1715                                                 p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
1716                                              D.DEMAND_CLASS)),
1717                                        -23453, DECODE(D.DEMAND_CLASS, null, null,
1718                                           DECODE(p_demand_class, '-1',
1719                                              MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1720                                                 null, null, p_item_id, p_org_id,
1721                                                 p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
1722                                              D.DEMAND_CLASS)),
1723                                        d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
1724                                           DECODE(p_demand_class, '-1',
1725                                              MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1726                                                 null, null, p_item_id, p_org_id,
1727                                                 p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
1728                                              D.DEMAND_CLASS)),
1729                                        p_demand_class),
1730                                     DECODE(D.DEMAND_CLASS, null, null,
1731                                        DECODE(p_demand_class, '-1',
1732                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1733                                              null, null, p_item_id, p_org_id,
1734                                              p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
1735                                           D.DEMAND_CLASS))),
1736                                     -- rajjain end
1737                                     2, DECODE(D.CUSTOMER_ID, NULL, p_demand_class,
1738                                                    0, p_demand_class,
1739                                        -- rajjain begin 07/19/2002
1740                                        decode(decode (d.origination_type, -100, 30,d.origination_type),  --5027568
1741                                           6, decode(d.source_organization_id,
1742                                              NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1743                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1744                                                    p_org_id, p_instance_id, c.calendar_date,
1745                                                    p_level_id, NULL),
1746                                              -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1747                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1748                                                    p_org_id, p_instance_id, c.calendar_date,
1749                                                    p_level_id, NULL),
1750                                              d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1751                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1752                                                    p_org_id, p_instance_id, c.calendar_date,
1753                                                    p_level_id, NULL),
1754                                              p_demand_class),
1755                                           30, decode(d.source_organization_id,
1756                                              NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1757                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1758                                                    p_org_id, p_instance_id, c.calendar_date,
1759                                                    p_level_id, NULL),
1760                                              -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1761                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1762                                                    p_org_id, p_instance_id, c.calendar_date,
1763                                                    p_level_id, NULL),
1764                                              d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1765                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1766                                                    p_org_id, p_instance_id, c.calendar_date,
1767                                                    p_level_id, NULL),
1768                                              p_demand_class),
1769                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(D.CUSTOMER_ID, D.SHIP_TO_SITE_ID,
1770                                              p_item_id, p_org_id, p_instance_id,
1771                                              c.calendar_date,p_level_id, NULL)))),
1772                                        -- rajjain end 07/19/2002
1773                                        p_demand_class,
1774                                        p_level_id),0)) SD_QTY --4365873
1775                    FROM    MSC_CALENDAR_DATES C,
1776                            MSC_DEMANDS D
1777                    WHERE        D.PLAN_ID = p_plan_id
1778                    AND                D.SR_INSTANCE_ID = p_instance_id
1779                    AND                D.INVENTORY_ITEM_ID = p_item_id
1780                    AND         D.ORGANIZATION_ID = p_org_id
1781                    AND         D.USING_REQUIREMENT_QUANTITY <> 0 --4501434
1782                    AND         D.ORIGINATION_TYPE NOT IN (5,7,8,9,11,15,22,28,29,31,52) -- For summary enhancement
1783                    -- Bug1990155, 1995835 exclude the expired lots demand datreya 9/18/2001
1784                    -- Bug 1530311, need to exclude forecast, ngoel 12/05/2000
1785                    AND                C.CALENDAR_CODE = p_cal_code
1786                    AND                C.EXCEPTION_SET_ID = p_cal_exc_set_id
1787                    AND         C.SR_INSTANCE_ID = D.SR_INSTANCE_ID
1788                    -- since we store repetitive schedule demand in different ways for
1789                    -- ods (total quantity on start date) and pds  (daily quantity from
1790                    -- start date to end date), we need to make sure we only select work day
1791                    -- for pds's repetitive schedule demand.
1792                    -- Bug 3550296 and 3574164. IMPLEMENT_DATE AND DMD_SATISFIED_DATE are changed to
1793                    -- IMPLEMENT_SHIP_DATE and PLANNED_SHIP_DATE resp.
1794                    AND         C.CALENDAR_DATE BETWEEN TRUNC(DECODE(RECORD_SOURCE,
1795                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1796                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1797                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1798                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))) AND
1799                                TRUNC(NVL(D.ASSEMBLY_DEMAND_COMP_DATE,
1800                                          DECODE(RECORD_SOURCE,
1801                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1802                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1803                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1804                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))))
1805                    AND         (( D.ORIGINATION_TYPE = 4
1806                            AND C.SEQ_NUM IS NOT NULL) OR
1807                            ( D.ORIGINATION_TYPE  <> 4))
1808                    AND         C.CALENDAR_DATE < NVL(p_itf,
1809                                    C.CALENDAR_DATE + 1)
1810                    UNION ALL
1811                    SELECT  --C.CALENDAR_DATE SD_DATE, --2859130
1812                            greatest(CS.CALENDAR_DATE,p_sys_next_date) SD_DATE,--3099066
1813                            NVL(CS.FIRM_QUANTITY,CS.NEW_ORDER_QUANTITY) UNALLOC_SD_QTY,
1814                            NVL(CS.FIRM_QUANTITY,CS.NEW_ORDER_QUANTITY)*
1815                               DECODE(DECODE(G_HIERARCHY_PROFILE,
1816                                      --2424357: Convert the demand calls in case of others for
1817                                      --- demand class allocated ATP
1818                                      1, DECODE(CS.DEMAND_CLASS, null, null,
1819                                         DECODE(p_demand_class, '-1',
1820                                            MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1821                                                          null,
1822                                                          null,
1823                                                          cs.inventory_item_id,
1824                                                          p_org_id,
1825                                                          p_instance_id,
1826                                                          cs.calendar_date,
1827                                                          p_level_id,
1828                                                          CS.DEMAND_CLASS),CS.DEMAND_CLASS)),
1829                                      2, DECODE(CS.CUSTOMER_ID, NULL, TO_CHAR(NULL),
1830                                                        0, TO_CHAR(NULL),
1831                                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1832                                                          CS.CUSTOMER_ID,
1833                                                          CS.SHIP_TO_SITE_ID,
1834                                                          cs.inventory_item_id,
1835                                                          p_org_id,
1836                                                          p_instance_id,
1837                                                          cs.calendar_date,
1838                                                          p_level_id,
1839                                                          NULL))),
1840                                  p_demand_class, 1,
1841                                  NULL, nvl(MIHM.allocation_percent/100,1), --4365873
1842                                  /*NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
1843                                            p_instance_id,
1844                                            s.inventory_item_id,
1845                                            p_org_id,
1846                                            null,
1847                                            null,
1848                                            p_demand_class,
1849                                            c.calendar_date), 1),*/
1850                                  DECODE(
1851                                  MIHM.allocation_percent/100, --4365873
1852                                  /*MSC_AATP_FUNC.Get_DC_Alloc_Percent(
1853                                            p_instance_id,
1854                                            s.inventory_item_id,
1855                                            p_org_id,
1856                                            null,
1857                                            null,
1858                                            p_demand_class,
1859                                            c.calendar_date),*/
1860                                    NULL, 1,
1861                                  0)) SD_QTY
1862                 FROM
1863                 (
1864                 select
1865                 	C.NEXT_DATE,
1866                 	C.CALENDAR_DATE,
1867 			S.FIRM_QUANTITY,
1868 			S.NEW_ORDER_QUANTITY,
1869 			S.DEMAND_CLASS,
1870 			s.inventory_item_id,
1871 			S.CUSTOMER_ID,
1872 			S.SHIP_TO_SITE_ID,
1873 			S.SR_INSTANCE_ID,
1874 			S.ORGANIZATION_ID
1875                    FROM    MSC_CALENDAR_DATES C,
1876                            MSC_SUPPLIES S
1877                    WHERE   S.PLAN_ID = p_plan_id
1878                    AND     S.SR_INSTANCE_ID = p_instance_id
1879                    AND     S.INVENTORY_ITEM_ID = p_item_id
1880                    AND     S.ORGANIZATION_ID = p_org_id
1881                            -- Exclude Cancelled Supplies 2460645
1882                    AND     NVL(S.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
1883                    AND     NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY) <> 0 -- 1243985
1884                    AND     C.CALENDAR_CODE = p_cal_code
1885                    AND     C.EXCEPTION_SET_ID = p_cal_exc_set_id
1886                    AND     C.SR_INSTANCE_ID = S.SR_INSTANCE_ID
1887                    AND     C.CALENDAR_DATE BETWEEN TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE))
1888                            AND TRUNC(NVL(S.LAST_UNIT_COMPLETION_DATE,
1889                                NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)))
1890                    AND     DECODE(S.LAST_UNIT_COMPLETION_DATE,
1891                                       NULL, C.NEXT_SEQ_NUM, C.SEQ_NUM) IS NOT NULL
1892                    AND     C.CALENDAR_DATE >= DECODE(S.ORDER_TYPE, 27, TRUNC(SYSDATE),
1893                                                                28, TRUNC(SYSDATE),
1894                                                                C.CALENDAR_DATE)
1895                    AND     C.CALENDAR_DATE < NVL(p_itf,
1896                                              C.CALENDAR_DATE + 1))CS,
1897 		MSC_ITEM_HIERARCHY_MV MIHM
1898 		WHERE
1899 		--4365873
1900                        CS.INVENTORY_ITEM_ID = MIHM.INVENTORY_ITEM_ID(+)
1901                 AND    CS.SR_INSTANCE_ID = MIHM.SR_INSTANCE_ID (+)
1902                 AND    CS.ORGANIZATION_ID = MIHM.ORGANIZATION_ID (+)
1903                 AND    decode(MIHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
1904                 AND    CS.NEXT_DATE >= MIHM.effective_date (+)
1905                 AND    CS.NEXT_DATE <= MIHM.disable_date (+)
1906                 AND    MIHM.demand_class (+) = p_demand_class
1907                    )
1908              GROUP BY SD_DATE
1909              ORDER BY SD_DATE;--4698199
1910 END item_alloc_avail_unopt_unalloc;
1911 --avjain All netting sqls have been changed to incorporate Plan by Request Date Enhancements
1912 PROCEDURE item_alloc_avail_opt_dtls (
1913    p_item_id            IN NUMBER,
1914    p_org_id             IN NUMBER,
1915    p_instance_id        IN NUMBER,
1916    p_plan_id            IN NUMBER,
1917    p_demand_class       IN VARCHAR2,
1918    p_level_id           IN NUMBER,
1919    p_itf                IN DATE,
1920    p_cal_code           IN VARCHAR2,
1921    p_cal_exc_set_id     IN NUMBER,
1922    p_sr_item_id         IN NUMBER,
1923    p_level              IN NUMBER,
1924    p_identifier         IN NUMBER,
1925    p_scenario_id        IN NUMBER,
1926    p_uom_code           IN VARCHAR2,
1927    p_sys_next_date	IN DATE				--bug3099066
1928 ) IS
1929    l_null_num   NUMBER;
1930    l_null_char  VARCHAR2(1);
1931    l_sysdate    DATE := trunc(sysdate); --4135752
1932 BEGIN
1933         IF PG_DEBUG in ('Y', 'C') THEN
1934                 msc_sch_wb.atp_debug('item_alloc_avail_opt_dtls: constrained plan: ' || p_plan_id);
1935         END IF;
1936 
1937         INSERT INTO msc_atp_sd_details_temp (
1938                 ATP_Level,
1939                 Order_line_id,
1940                 Scenario_Id,
1941                 Inventory_Item_Id,
1942                 Request_Item_Id,
1943                 Organization_Id,
1944                 Department_Id,
1945                 Resource_Id,
1946                 Supplier_Id,
1947                 Supplier_Site_Id,
1948                 From_Organization_Id,
1949                 From_Location_Id,
1950                 To_Organization_Id,
1951                 To_Location_Id,
1952                 Ship_Method,
1953                 UOM_code,
1954                 Supply_Demand_Type,
1955                 Supply_Demand_Source_Type,
1956                 Supply_Demand_Source_Type_Name,
1957                 Identifier1,
1958                 Identifier2,
1959                 Identifier3,
1960                 Identifier4,
1961                 Allocated_Quantity, -- fixed as part of time_phased_atp
1962                 Supply_Demand_Quantity,
1963                 Supply_Demand_Date,
1964                 Disposition_Type,
1965                 Disposition_Name,
1966                 Pegging_Id,
1967                 End_Pegging_Id,
1968                 creation_date,
1969                 created_by,
1970                 last_update_date,
1971                 last_updated_by,
1972                 last_update_login,
1973                 Unallocated_Quantity,
1974                 ORIG_CUSTOMER_SITE_NAME,--bug3263368
1975                 ORIG_CUSTOMER_NAME, --bug3263368
1976                 ORIG_DEMAND_CLASS, --bug3263368
1977                 ORIG_REQUEST_DATE --bug3263368
1978                 )
1979            (
1980             SELECT      p_level col1,
1981                         p_identifier col2,
1982                         p_scenario_id col3,
1983                         p_sr_item_id col4 ,
1984                         p_sr_item_id col5,
1985                         p_org_id col6,
1986                         l_null_num col7,
1987                         l_null_num col8,
1988                         l_null_num col9,
1989                         l_null_num col10,
1990                         l_null_num col11,
1991                         l_null_num col12,
1992                         l_null_num col13,
1993                         l_null_num col14,
1994                         l_null_char col15,
1995                         p_uom_code col16,
1996                         1 col17, -- demand
1997                         --D.ORIGINATION_TYPE col18,
1998                         DECODE( D.ORIGINATION_TYPE, -100, 30, D.ORIGINATION_TYPE) col18,  --5027568
1999                         l_null_char col19,
2000                         D.SR_INSTANCE_ID col20,
2001                         l_null_num col21,
2002                         D.DEMAND_ID col22,
2003                         l_null_num col23,
2004                         -- -1* D.USING_REQUIREMENT_QUANTITY * -- 2859130 remove decode originition_type 4
2005                         -1*(D.USING_REQUIREMENT_QUANTITY - NVL(d.reserved_quantity, 0))* --5027568
2006 			/*New*/
2007                         DECODE(p_scenario_id, -1, 1,
2008                         --2424357
2009                         DECODE(DECODE(G_HIERARCHY_PROFILE,
2010                         /*------------------------------------------------------------------------+
2011                         | rajjain begin 07/19/2002                                                |
2012                         |                                                                         |
2013                         | Case 1: For internal sales orders [origination type is in (6,30) and    |
2014                         |            source_organization_id is not null and <> organization_id]   |
2015                         |                  Return NULL                                            |
2016                         | Case 2: For others if DEMAND_CLASS is null then return null             |
2017                         |          else if p_demand_class is '-1' then call                       |
2018                         |            Get_Hierarchy_Demand_class else return DEMAND_CLASS          |
2019                         +------------------------------------------------------------------------*/
2020                            1, decode(decode (d.origination_type, -100, 30,d.origination_type),  --5027568
2021                               6, decode(d.source_organization_id,
2022                                  NULL, DECODE(D.DEMAND_CLASS, null, null,
2023                                     DECODE(p_demand_class, '-1',
2024                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2025                                           null, null, p_item_id, p_org_id,
2026                                           p_instance_id, trunc(DECODE(RECORD_SOURCE,
2027                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2028                                             -- Bug 3550296 and 3574164. IMPLEMENT_DATE AND DMD_SATISFIED_DATE are changed to
2029                                             -- IMPLEMENT_SHIP_DATE and PLANNED_SHIP_DATE resp.
2030                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2031                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2032                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
2033                                           D.DEMAND_CLASS)),
2034                                  -23453, DECODE(D.DEMAND_CLASS, null, null,
2035                                     DECODE(p_demand_class, '-1',
2036                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2037                                           null, null, p_item_id, p_org_id,
2038                                           p_instance_id, trunc(DECODE(RECORD_SOURCE,
2039                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2040                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2041                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2042                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
2043                                           D.DEMAND_CLASS)),
2044                                  d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
2045                                     DECODE(p_demand_class, '-1',
2046                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2047                                           null, null, p_item_id, p_org_id,
2048                                           p_instance_id, trunc(DECODE(RECORD_SOURCE,
2049                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2050                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2051                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2052                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
2053                                           D.DEMAND_CLASS)), NULL),
2054                               30, decode(d.source_organization_id,
2055                                  NULL, DECODE(D.DEMAND_CLASS, null, null,
2056                                     DECODE(p_demand_class, '-1',
2057                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2058                                           null, null, p_item_id, p_org_id,
2059                                           p_instance_id, trunc(DECODE(RECORD_SOURCE,
2060                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2061                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2062                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2063                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
2064                                           D.DEMAND_CLASS)),
2065                                  -23453, DECODE(D.DEMAND_CLASS, null, null,
2066                                     DECODE(p_demand_class, '-1',
2067                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2068                                           null, null, p_item_id, p_org_id,
2069                                           p_instance_id, trunc(DECODE(RECORD_SOURCE,
2070                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2071                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2072                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2073                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
2074                                           D.DEMAND_CLASS)),
2075                                  d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
2076                                     DECODE(p_demand_class, '-1',
2077                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2078                                           null, null, p_item_id, p_org_id,
2079                                           p_instance_id, trunc(DECODE(RECORD_SOURCE,
2080                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2081                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2082                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2083                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
2084                                           D.DEMAND_CLASS)), NULL),
2085                               DECODE(D.DEMAND_CLASS, null, null,
2086                                  DECODE(p_demand_class, '-1',
2087                                     MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2088                                        null, null, p_item_id, p_org_id,
2089                                        p_instance_id, trunc(DECODE(RECORD_SOURCE,
2090                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2091                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2092                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2093                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
2094                                        D.DEMAND_CLASS))),
2095                            -- rajjain end
2096                            2, DECODE(D.CUSTOMER_ID, NULL, NULL,
2097                                                 0, NULL,
2098                               -- rajjain begin 07/19/2002
2099                               decode(decode (d.origination_type, -100, 30,d.origination_type),  --5027568
2100                                  6, decode(d.source_organization_id,
2101                                     NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2102                                           D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
2103                                           p_org_id, p_instance_id, trunc(DECODE(RECORD_SOURCE,
2104                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2105                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2106                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2107                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
2108                                           p_level_id, NULL),
2109                                     -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2110                                           D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
2111                                           p_org_id, p_instance_id, trunc(DECODE(RECORD_SOURCE,
2112                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2113                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2114                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2115                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
2116                                           p_level_id, NULL),
2117                                     d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2118                                           D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
2119                                           p_org_id, p_instance_id, trunc(DECODE(RECORD_SOURCE,
2120                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2121                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2122                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2123                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
2124                                           p_level_id, NULL),
2125                                     NULL),
2126                                  30, decode(d.source_organization_id,
2127                                     NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2128                                           D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
2129                                           p_org_id, p_instance_id, trunc(DECODE(RECORD_SOURCE,
2130                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2131                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2132                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2133                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
2134                                           p_level_id, NULL),
2135                                     -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2136                                           D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
2137                                           p_org_id, p_instance_id, trunc(DECODE(RECORD_SOURCE,
2138                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2139                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2140                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2141                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
2142                                           p_level_id, NULL),
2143                                     d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2144                                           D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
2145                                           p_org_id, p_instance_id, trunc(DECODE(RECORD_SOURCE,
2146                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2147                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2148                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2149                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
2150                                           p_level_id, NULL),
2151                                     NULL),
2152                                  MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(D.CUSTOMER_ID, D.SHIP_TO_SITE_ID,
2153                                     p_item_id, p_org_id, p_instance_id,
2154                                     trunc(DECODE(RECORD_SOURCE,
2155                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2156                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2157                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2158                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),p_level_id, NULL)))),
2159                               -- rajjain end 07/19/2002
2160                         p_demand_class, 1,
2161                           Decode(D.Demand_Class, NULL, --4365873
2162                            MSC_AATP_FUNC.Get_Item_Demand_Alloc_Percent(p_plan_id,
2163                               D.DEMAND_ID,
2164                               trunc(DECODE(RECORD_SOURCE,
2165                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2166                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2167                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2168                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
2169                               D.USING_ASSEMBLY_ITEM_ID,
2170                               DECODE(D.SOURCE_ORGANIZATION_ID,
2171                                  -23453, null,
2172                                  D.SOURCE_ORGANIZATION_ID),
2173                               p_item_id,
2174                               p_org_id, -- 1665483
2175                               p_instance_id,
2176                               decode (d.origination_type, -100, 30,d.origination_type),
2177                               DECODE(G_HIERARCHY_PROFILE,
2178                               /*-----------------------------------------------------------------+
2179                               | rajjain begin 07/19/2002                                         |
2180                               |                                                                  |
2181                               | Case 1: For internal sales orders [origination type is in (6,30) |
2182                               |         and source_organization_id is not null                   |
2183                               |         and <> organization_id] -> Return p_demand_class         |
2184                               | Case 2: For others if DEMAND_CLASS is null then return null      |
2185                               |           else if p_demand_class is '-1' then call               |
2186                               |           Get_Hierarchy_Demand_class else return DEMAND_CLASS    |
2187                               +-----------------------------------------------------------------*/
2188                                  1, decode(decode (d.origination_type, -100, 30,d.origination_type),  --5027568
2189                                     6, decode(d.source_organization_id,
2190                                        NULL, DECODE(D.DEMAND_CLASS, null, null,
2191                                           DECODE(p_demand_class, '-1',
2192                                              MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2193                                                 null, null, p_item_id, p_org_id,
2194                                                 p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
2195                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2196                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2197                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2198                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
2199                                              D.DEMAND_CLASS)),
2200                                        -23453, DECODE(D.DEMAND_CLASS, null, null,
2201                                           DECODE(p_demand_class, '-1',
2202                                              MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2203                                                 null, null, p_item_id, p_org_id,
2204                                                 p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
2205                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2206                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2207                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2208                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
2209                                              D.DEMAND_CLASS)),
2210                                        d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
2211                                           DECODE(p_demand_class, '-1',
2212                                              MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2213                                                 null, null, p_item_id, p_org_id,
2214                                                 p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
2215                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2216                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2217                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2218                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
2219                                              D.DEMAND_CLASS)),
2220                                        p_demand_class),
2221                                  30, decode(d.source_organization_id,
2222                                     NULL, DECODE(D.DEMAND_CLASS, null, null,
2223                                        DECODE(p_demand_class, '-1',
2224                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2225                                              null, null, p_item_id, p_org_id,
2226                                              p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
2227                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2228                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2229                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2230                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
2231                                           D.DEMAND_CLASS)),
2232                                     -23453, DECODE(D.DEMAND_CLASS, null, null,
2233                                        DECODE(p_demand_class, '-1',
2234                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2235                                              null, null, p_item_id, p_org_id,
2236                                              p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
2237                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2238                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2239                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2240                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
2241                                           D.DEMAND_CLASS)),
2242                                     d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
2243                                        DECODE(p_demand_class, '-1',
2244                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2245                                              null, null, p_item_id, p_org_id,
2246                                              p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
2247                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2248                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2249                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2250                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
2251                                           D.DEMAND_CLASS)),
2252                                     p_demand_class),
2253                                  DECODE(D.DEMAND_CLASS, null, null,
2254                                     DECODE(p_demand_class, '-1',
2255                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2256                                           null, null, p_item_id, p_org_id,
2257                                           p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
2258                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2259                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2260                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2261                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
2262                                        D.DEMAND_CLASS))),
2263                                  -- rajjain end
2264                                  2, DECODE(D.CUSTOMER_ID, NULL, p_demand_class,
2265                                                 0, p_demand_class,
2266                                     -- rajjain begin 07/19/2002
2267                                     decode(decode (d.origination_type, -100, 30,d.origination_type),  --5027568
2268                                        6, decode(d.source_organization_id,
2269                                           NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2270                                                 D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
2271                                                 p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
2272                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2273                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2274                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2275                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
2276                                                 p_level_id, NULL),
2277                                           -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2278                                                 D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
2279                                                 p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
2280                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2281                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2282                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2283                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
2284                                                 p_level_id, NULL),
2285                                           d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2286                                                 D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
2287                                                 p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
2288                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2289                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2290                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2291                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
2292                                                 p_level_id, NULL),
2293                                           p_demand_class),
2294                                        30, decode(d.source_organization_id,
2295                                           NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2296                                                 D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
2297                                                 p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
2298                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2299                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2300                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2301                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
2302                                                 p_level_id, NULL),
2303                                           -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2304                                                 D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
2305                                                 p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
2306                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2307                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2308                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2309                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
2310                                                 p_level_id, NULL),
2311                                           d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2312                                                 D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
2313                                                 p_org_id, p_instance_id, TRUNC(DECODE(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)))),
2318                                                 p_level_id, NULL),
2319                                           p_demand_class),
2320                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(D.CUSTOMER_ID, D.SHIP_TO_SITE_ID,
2321                                           p_item_id, p_org_id, p_instance_id,
2322                                           TRUNC(DECODE(RECORD_SOURCE,
2323                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2324                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2325                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2326                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),p_level_id, NULL)))),
2327                                     -- rajjain end 07/19/2002
2328                                     p_demand_class,
2329                                     p_level_id),0))) col24,
2330                                 /*New*/
2331                         -- -1* D.USING_REQUIREMENT_QUANTITY, -- fixed as part of time_phased_atp
2332                         -1* (D.USING_REQUIREMENT_QUANTITY - nvl(d.reserved_quantity,0)),--5027568
2333                         -- C.PRIOR_DATE col25, -- 2859130
2334                         GREATEST(TRUNC(DECODE(RECORD_SOURCE,
2335                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2336                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2337                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2338                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
2339                         	 p_sys_next_date) col25, --plan by request date --3099066
2340                         l_null_num col26,
2341                         DECODE(D.ORIGINATION_TYPE, 1, to_char(D.DISPOSITION_ID), D.ORDER_NUMBER) col27,
2342                                -- rajjain 04/25/2003 Bug 2771075
2343                                -- For Planned Order Demands We will populate disposition_id
2344                                -- in disposition_name column
2345                         l_null_num col28,
2346                         l_null_num col29,
2347         		-- ship_rec_cal changes begin
2348         		l_sysdate,
2349         		G_USER_ID,
2350         		l_sysdate,
2351         		G_USER_ID,
2352         		G_USER_ID,
2353         		-- ship_rec_cal changes end
2354                         -- Unallocated_Quantity
2355                         -- -1* D.USING_REQUIREMENT_QUANTITY, -- 2859130 remove decode for origination_type 4
2356                         -1* (D.USING_REQUIREMENT_QUANTITY - nvl(d.reserved_quantity,0)), --5027568
2357                         MTPS.LOCATION, --bug3263368
2358                         MTP.PARTNER_NAME, --bug3263368
2359                         D.DEMAND_CLASS, --bug3263368
2360                         DECODE(D.ORDER_DATE_TYPE_CODE,2,D.REQUEST_DATE,
2361                                                     D.REQUEST_SHIP_DATE) --bug3263368
2362             FROM        -- MSC_CALENDAR_DATES C, --2859130
2363                         MSC_DEMANDS D,
2364                         MSC_TRADING_PARTNERS    MTP,--bug3263368
2365                         MSC_TRADING_PARTNER_SITES    MTPS --bug3263368
2366             WHERE       D.PLAN_ID = p_plan_id
2367             AND         D.SR_INSTANCE_ID = p_instance_id
2368             AND         D.INVENTORY_ITEM_ID = p_item_id
2369             AND         D.ORGANIZATION_ID = p_org_id
2370             AND         D.USING_REQUIREMENT_QUANTITY <> 0 --4501434
2371             AND         D.ORIGINATION_TYPE NOT IN (5,7,8,9,11,15,22,28,29,31,52) -- For summary enhancement
2372             AND         D.SHIP_TO_SITE_ID = MTPS.PARTNER_SITE_ID(+) --bug3263368
2373             AND         D.CUSTOMER_ID = MTP.PARTNER_ID(+) --bug3263368
2374             -- 2859130
2375             -- Bug1990155, 1995835 exclude the expired lots demand datreya 9/18/2001
2376             -- Bug 1530311, need to exclude forecast, ngoel 12/05/2000
2377             -- AND         C.CALENDAR_CODE = p_cal_code
2378             -- AND         C.EXCEPTION_SET_ID = p_cal_exc_set_id
2379             -- AND         C.SR_INSTANCE_ID = D.SR_INSTANCE_ID
2380             -- since we store repetitive schedule demand in different ways for
2381             -- ods (total quantity on start date) and pds  (daily quantity from
2382             -- start date to end date), we need to make sure we only select work day
2383             -- for pds's repetitive schedule demand.
2384             -- AND         C.CALENDAR_DATE BETWEEN TRUNC(D.USING_ASSEMBLY_DEMAND_DATE) AND
2385             --             TRUNC(NVL(D.ASSEMBLY_DEMAND_COMP_DATE,
2386             --                       D.USING_ASSEMBLY_DEMAND_DATE))
2387             -- AND         (( D.ORIGINATION_TYPE = 4
2388             --               AND C.SEQ_NUM IS NOT NULL) OR
2389             --               ( D.ORIGINATION_TYPE  <> 4))
2390             -- 2859130
2391             -- Bug 3550296 and 3574164. IMPLEMENT_DATE AND DMD_SATISFIED_DATE are changed to
2392             -- IMPLEMENT_SHIP_DATE and PLANNED_SHIP_DATE resp.
2393             --bug3693892 added trunc
2394             AND         TRUNC(DECODE(RECORD_SOURCE,
2395                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2396                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2397                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2398                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))))
2399                        < TRUNC(NVL(p_itf,DECODE(RECORD_SOURCE,
2400                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2401                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2402                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2403                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))) + 1))
2404             -- AND C.CALENDAR_DATE < NVL(p_itf, C.CALENDAR_DATE+1)
2405             UNION ALL
2406             SELECT      p_level col1,
2407                         p_identifier col2,
2408                         p_scenario_id col3,
2409                         p_sr_item_id col4 ,
2410                         p_sr_item_id col5,
2411                         p_org_id col6,
2412                         l_null_num col7,
2413                         l_null_num col8,
2414                         l_null_num col9,
2415                         l_null_num col10,
2416                         l_null_num col11,
2417                         l_null_num col12,
2418                         l_null_num col13,
2419                         l_null_num col14,
2420                         l_null_char col15,
2421                         p_uom_code col16,
2422                         2 col17, -- supply
2423                         S.ORDER_TYPE col18,
2424                         l_null_char col19,
2425                         S.SR_INSTANCE_ID col20,
2426                                 l_null_num col21,
2427                         S.TRANSACTION_ID col22,
2428                         l_null_num col23,
2429                         NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY)*
2430                                 DECODE(p_scenario_id, -1, 1,
2431                                 DECODE(DECODE(G_HIERARCHY_PROFILE,
2432                                               --2424357
2433                                               1, DECODE(S.DEMAND_CLASS, null, null,
2434                                                      DECODE(p_demand_class,'-1',
2435                                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2436                                                           null,
2437                                                           null,
2438                                                           S.inventory_item_id,
2439                                                           p_org_id,
2440                                                           p_instance_id,
2441                                                           TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)),
2442                                                           p_level_id,
2443                                                           S.DEMAND_CLASS), S.DEMAND_CLASS)),
2444                                               2, DECODE(S.CUSTOMER_ID, NULL, TO_CHAR(NULL),
2445                                                         --0, TO_CHAR(NULL),
2446                                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2447                                                           S.CUSTOMER_ID,
2448                                                           S.SHIP_TO_SITE_ID,
2449                                                           S.inventory_item_id,
2450                                                           p_org_id,
2451                                                           p_instance_id,
2452                                                           TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)),
2453                                                           p_level_id,
2454                                                           NULL))),
2455                                         p_demand_class, 1,
2456                                         NULL, NVL(MIHM.allocation_percent/100,--4365873
2457                                         /*NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
2458                                                 p_instance_id,
2459                                                 s.inventory_item_id,
2460                                                 p_org_id,
2461                                                 null,
2462                                                 null,
2463                                                 p_demand_class,
2464                                                 TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE))),*/
2465                                                  1),
2466                                         DECODE(MIHM.allocation_percent/100, --4365873
2467                                         /*DECODE(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
2468                                                 p_instance_id,
2469                                                 s.inventory_item_id,
2470                                                 p_org_id,
2471                                                 null,
2472                                                 null,
2473                                                 p_demand_class,
2474                                                 TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE))),*/
2475                                         NULL, 1,
2476                                         0))) col24,
2477                         NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY),
2478                         --C.NEXT_DATE col25, -- 2859130
2479                         --TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)) col25,
2480                         GREATEST(TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)),p_sys_next_date) col25,--3099066
2481                         l_null_num col26,
2482                         DECODE(S.ORDER_TYPE, 5, to_char(S.TRANSACTION_ID), S.ORDER_NUMBER) col27,
2483                                -- Bug 2771075. For Planned Orders, we will populate transaction_id
2484 			       -- in the disposition_name column to be consistent with Planning.
2485                         l_null_num col28,
2486                         l_null_num col29,
2487         		-- ship_rec_cal changes begin
2488         		l_sysdate,
2489         		G_USER_ID,
2490         		l_sysdate,
2491         		G_USER_ID,
2492         		G_USER_ID,
2493         		-- ship_rec_cal changes end
2494                         NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY), -- unallocated quantity
2495                         null, --bug3263368 ORIG_CUSTOMER_SITE_NAME
2496                         null, --bug3263368 ORIG_CUSTOMER_NAME
2497                         null, --bug3263368 ORIG_DEMAND_CLASS
2498                         null  --bug3263368 ORIG_REQUEST_DATE
2499             FROM        -- MSC_CALENDAR_DATES C, -- 2859130
2500                         MSC_SUPPLIES S,MSC_ITEM_HIERARCHY_MV  MIHM
2501             WHERE       S.PLAN_ID = p_plan_id
2502             AND         S.SR_INSTANCE_ID = p_instance_id
2503             AND         S.INVENTORY_ITEM_ID = p_item_id
2504             AND         S.ORGANIZATION_ID = p_org_id
2505                         -- Exclude Cancelled Supplies 2460645
2506             AND         NVL(S.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
2507             AND         NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY) <> 0 -- 1243985
2508             --2859130
2509             --AND         C.CALENDAR_CODE = p_cal_code
2510             --AND         C.EXCEPTION_SET_ID = p_cal_exc_set_id
2511             --AND         C.SR_INSTANCE_ID = S.SR_INSTANCE_ID
2512             --AND         C.CALENDAR_DATE BETWEEN TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE))
2513             --                    AND TRUNC(NVL(S.LAST_UNIT_COMPLETION_DATE,
2514             --                        NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)))
2515             --AND         DECODE(S.LAST_UNIT_COMPLETION_DATE,
2516             --                   NULL, C.NEXT_SEQ_NUM, C.SEQ_NUM) IS NOT NULL
2517             --AND         C.NEXT_DATE >= DECODE(S.ORDER_TYPE, 27, TRUNC(SYSDATE),
2518             --                                                28, TRUNC(SYSDATE),
2519             --                                                C.NEXT_DATE)
2520             --AND         C.NEXT_DATE < NVL(p_itf,
2521             --                             C.NEXT_DATE + 1)
2522             AND         TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)) >= DECODE(S.ORDER_TYPE, 27, TRUNC(SYSDATE),
2523                                                             28, TRUNC(SYSDATE),
2524                                                             TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)))
2525             AND         TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)) < NVL(p_itf,
2526                                          TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)) + 1)
2527 	    --4365873
2528             AND    S.INVENTORY_ITEM_ID = MIHM.INVENTORY_ITEM_ID(+)
2529             AND    S.SR_INSTANCE_ID = MIHM.SR_INSTANCE_ID (+)
2530             AND    S.ORGANIZATION_ID = MIHM.ORGANIZATION_ID (+)
2531             AND    decode(MIHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
2532             AND    TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)) >= MIHM.effective_date (+)
2533             AND    TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)) <= MIHM.disable_date (+)
2534             AND    MIHM.demand_class (+) = p_demand_class
2535            ); -- dsting removed order by col25
2536 
2537 END item_alloc_avail_opt_dtls;
2538 --avjain All netting sqls have been changed to incorporate Plan by Request Date Enhancements
2539 PROCEDURE item_alloc_avail_unopt_dtls (
2540    p_item_id            IN NUMBER,
2541    p_org_id             IN NUMBER,
2542    p_instance_id        IN NUMBER,
2543    p_plan_id            IN NUMBER,
2544    p_demand_class       IN VARCHAR2,
2545    p_level_id           IN NUMBER,
2546    p_itf                IN DATE,
2547    p_cal_code           IN VARCHAR2,
2548    p_cal_exc_set_id     IN NUMBER,
2549    p_sr_item_id         IN NUMBER,
2550    p_level              IN NUMBER,
2551    p_identifier         IN NUMBER,
2552    p_scenario_id        IN NUMBER,
2553    p_uom_code           IN VARCHAR2,
2554    p_sys_next_date	IN DATE
2555 ) IS
2556    l_null_num   NUMBER;
2557    l_null_char  VARCHAR2(1);
2558    l_sysdate    DATE := trunc(sysdate);--4135752
2559 BEGIN
2560         IF PG_DEBUG in ('Y', 'C') THEN
2561                 msc_sch_wb.atp_debug('item_alloc_avail_unopt_dtls: UNconstrained plan: ' || p_plan_id);
2562         END IF;
2563 
2564         INSERT INTO msc_atp_sd_details_temp (
2565                 ATP_Level,
2566                 Order_line_id,
2567                 Scenario_Id,
2568                 Inventory_Item_Id,
2569                 Request_Item_Id,
2570                 Organization_Id,
2571                 Department_Id,
2572                 Resource_Id,
2573                 Supplier_Id,
2574                 Supplier_Site_Id,
2575                 From_Organization_Id,
2576                 From_Location_Id,
2577                 To_Organization_Id,
2578                 To_Location_Id,
2579                 Ship_Method,
2580                 UOM_code,
2581                 Supply_Demand_Type,
2582                 Supply_Demand_Source_Type,
2583                 Supply_Demand_Source_Type_Name,
2584                 Identifier1,
2585                 Identifier2,
2586                 Identifier3,
2587                 Identifier4,
2588                 Allocated_Quantity,
2589                 Supply_Demand_Quantity,
2590                 Supply_Demand_Date,
2591                 Disposition_Type,
2592                 Disposition_Name,
2593                 Pegging_Id,
2594                 End_Pegging_Id,
2595                 creation_date,
2596                 created_by,
2597                 last_update_date,
2598                 last_updated_by,
2599                 last_update_login,
2600                 Unallocated_Quantity,
2601                 ORIG_CUSTOMER_SITE_NAME,--bug3263368
2602                 ORIG_CUSTOMER_NAME, --bug3263368
2603                 ORIG_DEMAND_CLASS, --bug3263368
2604                 ORIG_REQUEST_DATE --bug3263368
2605                 )
2606            (
2607             SELECT      p_level col1,
2608                         p_identifier col2,
2609                         p_scenario_id col3,
2610                         p_sr_item_id col4 ,
2611                         p_sr_item_id col5,
2612                         p_org_id col6,
2613                         l_null_num col7,
2614                         l_null_num col8,
2615                         l_null_num col9,
2616                         l_null_num col10,
2617                         l_null_num col11,
2618                         l_null_num col12,
2619                         l_null_num col13,
2620                         l_null_num col14,
2621                         l_null_char col15,
2622                         p_uom_code col16,
2623                         1 col17, -- demand
2624                         --D.ORIGINATION_TYPE col18,
2625                         DECODE( D.ORIGINATION_TYPE, -100, 30, D.ORIGINATION_TYPE) col18,  --5027568
2626                         l_null_char col19,
2627                         D.SR_INSTANCE_ID col20,
2628                         l_null_num col21,
2629                         D.DEMAND_ID col22,
2630                         l_null_num col23,
2631                         -1* DECODE(D.ORIGINATION_TYPE,
2632                                4, D.DAILY_DEMAND_RATE,
2633                                --D.USING_REQUIREMENT_QUANTITY)*
2634                                (D.USING_REQUIREMENT_QUANTITY - NVL(d.reserved_quantity, 0)))* --5027568
2635 			/*New*/
2636                         DECODE(p_scenario_id, -1, 1,
2637                         --2424357
2638                         DECODE(DECODE(G_HIERARCHY_PROFILE,
2639                         /*------------------------------------------------------------------------+
2640                         | rajjain begin 07/19/2002                                                |
2641                         |                                                                         |
2642                         | Case 1: For internal sales orders [origination type is in (6,30) and    |
2643                         |            source_organization_id is not null and <> organization_id]   |
2644                         |                  Return NULL                                            |
2645                         | Case 2: For others if DEMAND_CLASS is null then return null             |
2646                         |          else if p_demand_class is '-1' then call                       |
2647                         |            Get_Hierarchy_Demand_class else return DEMAND_CLASS          |
2648                         +------------------------------------------------------------------------*/
2649                            1, decode(decode (d.origination_type, -100, 30,d.origination_type),  --5027568
2650                               6, decode(d.source_organization_id,
2651                                  NULL, DECODE(D.DEMAND_CLASS, null, null,
2652                                     DECODE(p_demand_class, '-1',
2653                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2654                                           null, null, p_item_id, p_org_id,
2655                                           p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
2656                                           D.DEMAND_CLASS)),
2657                                  -23453, DECODE(D.DEMAND_CLASS, null, null,
2658                                     DECODE(p_demand_class, '-1',
2659                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2660                                           null, null, p_item_id, p_org_id,
2661                                           p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
2662                                           D.DEMAND_CLASS)),
2663                                  d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
2664                                     DECODE(p_demand_class, '-1',
2665                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2666                                           null, null, p_item_id, p_org_id,
2667                                           p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
2668                                           D.DEMAND_CLASS)), NULL),
2669                               30, decode(d.source_organization_id,
2670                                  NULL, DECODE(D.DEMAND_CLASS, null, null,
2671                                     DECODE(p_demand_class, '-1',
2672                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2673                                           null, null, p_item_id, p_org_id,
2674                                           p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
2675                                           D.DEMAND_CLASS)),
2676                                  -23453, DECODE(D.DEMAND_CLASS, null, null,
2677                                     DECODE(p_demand_class, '-1',
2678                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2679                                           null, null, p_item_id, p_org_id,
2680                                           p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
2681                                           D.DEMAND_CLASS)),
2682                                  d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
2683                                     DECODE(p_demand_class, '-1',
2684                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2685                                           null, null, p_item_id, p_org_id,
2686                                           p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
2687                                           D.DEMAND_CLASS)), NULL),
2688                               DECODE(D.DEMAND_CLASS, null, null,
2689                                  DECODE(p_demand_class, '-1',
2690                                     MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2691                                        null, null, p_item_id, p_org_id,
2692                                        p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
2693                                        D.DEMAND_CLASS))),
2694                            -- rajjain end
2695                            2, DECODE(D.CUSTOMER_ID, NULL, NULL,
2696                                                 0, NULL,
2697                               -- rajjain begin 07/19/2002
2698                               decode(decode (d.origination_type, -100, 30,d.origination_type),  --5027568
2699                                  6, decode(d.source_organization_id,
2700                                     NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2701                                           D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
2702                                           p_org_id, p_instance_id, c.calendar_date,
2703                                           p_level_id, NULL),
2704                                     -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2705                                           D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
2706                                           p_org_id, p_instance_id, c.calendar_date,
2707                                           p_level_id, NULL),
2708                                     d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2709                                           D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
2710                                           p_org_id, p_instance_id, c.calendar_date,
2711                                           p_level_id, NULL),
2712                                     NULL),
2713                                  30, decode(d.source_organization_id,
2714                                     NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2715                                           D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
2716                                           p_org_id, p_instance_id, c.calendar_date,
2717                                           p_level_id, NULL),
2718                                     -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2719                                           D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
2720                                           p_org_id, p_instance_id, c.calendar_date,
2721                                           p_level_id, NULL),
2722                                     d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2723                                           D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
2724                                           p_org_id, p_instance_id, c.calendar_date,
2725                                           p_level_id, NULL),
2726                                     NULL),
2727                                  MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(D.CUSTOMER_ID, D.SHIP_TO_SITE_ID,
2728                                     p_item_id, p_org_id, p_instance_id,
2729                                     c.calendar_date,p_level_id, NULL)))),
2730                               -- rajjain end 07/19/2002
2731                         p_demand_class, 1,
2732                         Decode(D.Demand_Class, NULL, --4365873
2733                            MSC_AATP_FUNC.Get_Item_Demand_Alloc_Percent(p_plan_id,
2734                               D.DEMAND_ID,
2735                               c.calendar_date,
2736                               D.USING_ASSEMBLY_ITEM_ID,
2737                               DECODE(D.SOURCE_ORGANIZATION_ID,
2738                                  -23453, null,
2739                                  D.SOURCE_ORGANIZATION_ID),
2740                               p_item_id,
2741                               p_org_id, -- 1665483
2742                               p_instance_id,
2743                               decode (d.origination_type, -100, 30,d.origination_type),
2744                               DECODE(G_HIERARCHY_PROFILE,
2745                               /*-----------------------------------------------------------------+
2746                               | rajjain begin 07/19/2002                                         |
2747                               |                                                                  |
2748                               | Case 1: For internal sales orders [origination type is in (6,30) |
2749                               |         and source_organization_id is not null                   |
2750                               |         and <> organization_id] -> Return p_demand_class         |
2751                               | Case 2: For others if DEMAND_CLASS is null then return null      |
2752                               |           else if p_demand_class is '-1' then call               |
2753                               |           Get_Hierarchy_Demand_class else return DEMAND_CLASS    |
2754                               +-----------------------------------------------------------------*/
2755                                  1, decode(decode (d.origination_type, -100, 30,d.origination_type),  --5027568
2756                                     6, decode(d.source_organization_id,
2757                                        NULL, DECODE(D.DEMAND_CLASS, null, null,
2758                                           DECODE(p_demand_class, '-1',
2759                                              MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2760                                                 null, null, p_item_id, p_org_id,
2761                                                 p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
2762                                              D.DEMAND_CLASS)),
2763                                        -23453, DECODE(D.DEMAND_CLASS, null, null,
2764                                           DECODE(p_demand_class, '-1',
2765                                              MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2766                                                 null, null, p_item_id, p_org_id,
2767                                                 p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
2768                                              D.DEMAND_CLASS)),
2769                                        d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
2770                                           DECODE(p_demand_class, '-1',
2771                                              MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2772                                                 null, null, p_item_id, p_org_id,
2773                                                 p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
2774                                              D.DEMAND_CLASS)),
2775                                        p_demand_class),
2776                                  30, decode(d.source_organization_id,
2777                                     NULL, DECODE(D.DEMAND_CLASS, null, null,
2778                                        DECODE(p_demand_class, '-1',
2779                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2780                                              null, null, p_item_id, p_org_id,
2781                                              p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
2782                                           D.DEMAND_CLASS)),
2783                                     -23453, DECODE(D.DEMAND_CLASS, null, null,
2784                                        DECODE(p_demand_class, '-1',
2785                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2786                                              null, null, p_item_id, p_org_id,
2787                                              p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
2788                                           D.DEMAND_CLASS)),
2789                                     d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
2790                                        DECODE(p_demand_class, '-1',
2791                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2792                                              null, null, p_item_id, p_org_id,
2793                                              p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
2794                                           D.DEMAND_CLASS)),
2795                                     p_demand_class),
2796                                  DECODE(D.DEMAND_CLASS, null, null,
2797                                     DECODE(p_demand_class, '-1',
2798                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2799                                           null, null, p_item_id, p_org_id,
2800                                           p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
2801                                        D.DEMAND_CLASS))),
2802                                  -- rajjain end
2803                                  2, DECODE(D.CUSTOMER_ID, NULL, p_demand_class,
2804                                                 0, p_demand_class,
2805                                     -- rajjain begin 07/19/2002
2806                                     decode(decode (d.origination_type, -100, 30,d.origination_type),  --5027568
2807                                        6, decode(d.source_organization_id,
2808                                           NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2809                                                 D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
2810                                                 p_org_id, p_instance_id, c.calendar_date,
2811                                                 p_level_id, NULL),
2812                                           -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2813                                                 D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
2814                                                 p_org_id, p_instance_id, c.calendar_date,
2815                                                 p_level_id, NULL),
2816                                           d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2817                                                 D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
2818                                                 p_org_id, p_instance_id, c.calendar_date,
2819                                                 p_level_id, NULL),
2820                                           p_demand_class),
2821                                        30, decode(d.source_organization_id,
2822                                           NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2823                                                 D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
2824                                                 p_org_id, p_instance_id, c.calendar_date,
2825                                                 p_level_id, NULL),
2826                                           -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2827                                                 D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
2828                                                 p_org_id, p_instance_id, c.calendar_date,
2829                                                 p_level_id, NULL),
2830                                           d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2831                                                 D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
2832                                                 p_org_id, p_instance_id, c.calendar_date,
2833                                                 p_level_id, NULL),
2834                                           p_demand_class),
2835                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(D.CUSTOMER_ID, D.SHIP_TO_SITE_ID,
2836                                           p_item_id, p_org_id, p_instance_id,
2837                                           c.calendar_date,p_level_id, NULL)))),
2838                                     -- rajjain end 07/19/2002
2839                                     p_demand_class,
2840                                     p_level_id),0))) col24,
2841                                 /*New*/
2842                         -1* DECODE(D.ORIGINATION_TYPE,
2843                                4, D.DAILY_DEMAND_RATE,
2844                                --D.USING_REQUIREMENT_QUANTITY),
2845                                (D.USING_REQUIREMENT_QUANTITY- nvl(d.reserved_quantity,0))),  --5027568
2846 
2847                         -- C.PRIOR_DATE col25, -- 2859130
2848                         --C.CALENDAR_DATE col25,
2849                         GREATEST(C.CALENDAR_DATE,p_sys_next_date) col25,
2850                         l_null_num col26,
2851                         DECODE(D.ORIGINATION_TYPE, 1, to_char(D.DISPOSITION_ID), D.ORDER_NUMBER) col27,
2852                                -- rajjain 04/25/2003 Bug 2771075
2853                                -- For Planned Order Demands We will populate disposition_id
2854                                -- in disposition_name column
2855                         l_null_num col28,
2856                         l_null_num col29,
2857                         -- ship_rec_cal changes begin
2858                         l_sysdate,
2859                         G_USER_ID,
2860                         l_sysdate,
2861                         G_USER_ID,
2862                         G_USER_ID,
2863                         -- ship_rec_cal changes end
2864                         -- Unallocated_Quantity
2865                         -1* DECODE(D.ORIGINATION_TYPE,
2866                                 4, D.DAILY_DEMAND_RATE,
2867                         --D.USING_REQUIREMENT_QUANTITY),
2868                         (D.USING_REQUIREMENT_QUANTITY- nvl(d.reserved_quantity,0))),  --5027568
2869                         MTPS.LOCATION, --bug3263368
2870                         MTP.PARTNER_NAME, --bug3263368
2871                         D.DEMAND_CLASS, --bug3263368
2872                         DECODE(D.ORDER_DATE_TYPE_CODE,2,D.REQUEST_DATE,
2873                                                 D.REQUEST_SHIP_DATE) --bug3263368
2874 
2875             FROM        MSC_CALENDAR_DATES C,
2876                         MSC_DEMANDS D,
2877                         MSC_TRADING_PARTNERS    MTP,--bug3263368
2878                         MSC_TRADING_PARTNER_SITES    MTPS --bug3263368
2879 
2880             WHERE       D.PLAN_ID = p_plan_id
2881             AND         D.SR_INSTANCE_ID = p_instance_id
2882             AND         D.INVENTORY_ITEM_ID = p_item_id
2883             AND         D.USING_REQUIREMENT_QUANTITY <> 0 --4501434
2884             AND         D.ORGANIZATION_ID = p_org_id
2885             AND         D.ORIGINATION_TYPE NOT IN (5,7,8,9,11,15,22,28,29,31,52) -- For summary enhancement
2886             AND         D.SHIP_TO_SITE_ID = MTPS.PARTNER_SITE_ID(+) --bug3263368
2887             AND         D.CUSTOMER_ID = MTP.PARTNER_ID(+)--bug3263368
2888 
2889             -- Bug1990155, 1995835 exclude the expired lots demand datreya 9/18/2001
2890             -- Bug 1530311, need to exclude forecast, ngoel 12/05/2000
2891             AND         C.CALENDAR_CODE = p_cal_code
2892             AND         C.EXCEPTION_SET_ID = p_cal_exc_set_id
2893             AND         C.SR_INSTANCE_ID = D.SR_INSTANCE_ID
2894             -- since we store repetitive schedule demand in different ways for
2895             -- ods (total quantity on start date) and pds  (daily quantity from
2896             -- start date to end date), we need to make sure we only select work day
2897             -- for pds's repetitive schedule demand.
2898             -- Bug 3550296 and 3574164. IMPLEMENT_DATE AND DMD_SATISFIED_DATE are changed to
2899             -- IMPLEMENT_SHIP_DATE and PLANNED_SHIP_DATE resp.
2900             AND         C.CALENDAR_DATE BETWEEN TRUNC(DECODE(RECORD_SOURCE,
2901                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2902                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2903                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2904                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))) AND
2905                         TRUNC(NVL(D.ASSEMBLY_DEMAND_COMP_DATE,
2906                                   DECODE(RECORD_SOURCE,
2907                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2908                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2909                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2910                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))))
2911             AND         (( D.ORIGINATION_TYPE = 4
2912                           AND C.SEQ_NUM IS NOT NULL) OR
2913                           ( D.ORIGINATION_TYPE  <> 4))
2914             -- 2859130
2915             -- AND         C.PRIOR_DATE < NVL(p_itf,
2916             --                              C.PRIOR_DATE + 1)
2917             AND C.CALENDAR_DATE < NVL(p_itf, C.CALENDAR_DATE+1)
2918             UNION ALL
2919             SELECT      p_level col1,
2920                         p_identifier col2,
2921                         p_scenario_id col3,
2922                         p_sr_item_id col4 ,
2923                         p_sr_item_id col5,
2924                         p_org_id col6,
2925                         l_null_num col7,
2926                         l_null_num col8,
2927                         l_null_num col9,
2928                         l_null_num col10,
2929                         l_null_num col11,
2930                         l_null_num col12,
2931                         l_null_num col13,
2932                         l_null_num col14,
2933                         l_null_char col15,
2934                         p_uom_code col16,
2935                         2 col17, -- supply
2936                         CS.ORDER_TYPE col18,
2937                         l_null_char col19,
2938                         CS.SR_INSTANCE_ID col20,
2939                                 l_null_num col21,
2940                         CS.TRANSACTION_ID col22,
2941                         l_null_num col23,
2942                         NVL(CS.FIRM_QUANTITY,CS.NEW_ORDER_QUANTITY)*
2943                                 DECODE(p_scenario_id, -1, 1,
2944                                 DECODE(DECODE(G_HIERARCHY_PROFILE,
2945                                               --2424357
2946                                               1, DECODE(CS.DEMAND_CLASS, null, null,
2947                                                      DECODE(p_demand_class,'-1',
2948                                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2949                                                           null,
2950                                                           null,
2951                                                           cs.inventory_item_id,
2952                                                           p_org_id,
2953                                                           p_instance_id,
2954                                                           cs.calendar_date,
2955                                                           p_level_id,
2956                                                           CS.DEMAND_CLASS), CS.DEMAND_CLASS)),
2957                                               2, DECODE(CS.CUSTOMER_ID, NULL, TO_CHAR(NULL),
2958                                                         --0, TO_CHAR(NULL),
2959                                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2960                                                           CS.CUSTOMER_ID,
2961                                                           CS.SHIP_TO_SITE_ID,
2962                                                           cs.inventory_item_id,
2963                                                           p_org_id,
2964                                                           p_instance_id,
2965                                                           cs.calendar_date,
2966                                                           p_level_id,
2967                                                           NULL))),
2968                                         p_demand_class, 1,
2969                                         NULL, nvl(MIHM.allocation_percent/100,1), --4365873
2970                                         /*NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
2971                                                 p_instance_id,
2972                                                 cs.inventory_item_id,
2973                                                 p_org_id,
2974                                                 null,
2975                                                 null,
2976                                                 p_demand_class,
2977                                                 cs.calendar_date), 1),*/
2978                                         DECODE(
2979                                         MIHM.allocation_percent/100, --4365873
2980                                         /*DECODE(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
2981                                                 p_instance_id,
2982                                                 cs.inventory_item_id,
2983                                                 p_org_id,
2984                                                 null,
2985                                                 null,
2986                                                 p_demand_class,
2987                                                 cs.calendar_date),*/
2988                                         NULL, 1,
2989                                         0))) col24,
2990                         NVL(CS.FIRM_QUANTITY,CS.NEW_ORDER_QUANTITY),
2991                         --C.NEXT_DATE col25, -- 2859130
2992                         --C.CALENDAR_DATE col25,
2993                         GREATEST(CS.CALENDAR_DATE,p_sys_next_date) col25,--3099066
2994                         l_null_num col26,
2995                         DECODE(CS.ORDER_TYPE, 5, to_char(CS.TRANSACTION_ID), CS.ORDER_NUMBER) col27,
2996                                -- Bug 2771075. For Planned Orders, we will populate transaction_id
2997 			       -- in the disposition_name column to be consistent with Planning.
2998                         l_null_num col28,
2999                         l_null_num col29,
3000                         -- ship_rec_cal changes begin
3001                         l_sysdate,
3002                         G_USER_ID,
3003                         l_sysdate,
3004                         G_USER_ID,
3005                         G_USER_ID,
3006                         -- ship_rec_cal changes end
3007                         NVL(CS.FIRM_QUANTITY,CS.NEW_ORDER_QUANTITY), -- unallocated quantity
3008                         null, --bug3263368 ORIG_CUSTOMER_SITE_NAME
3009                         null, --bug3263368 ORIG_CUSTOMER_NAME
3010                         null, --bug3263368 ORIG_DEMAND_CLASS
3011                         null  --bug3263368 ORIG_REQUEST_DATE
3012             FROM
3013 
3014             	(	select
3015             		S.ORDER_TYPE,
3016 			S.TRANSACTION_ID,
3017 			S.ORDER_NUMBER,
3018 			S.SR_INSTANCE_ID,
3019 			C.NEXT_DATE,
3020 			S.FIRM_QUANTITY,
3021 			S.NEW_ORDER_QUANTITY,
3022 			S.DEMAND_CLASS,
3023 			s.inventory_item_id,
3024 			S.CUSTOMER_ID,
3025 			S.SHIP_TO_SITE_ID,
3026 			S.ORGANIZATION_ID,
3027 			C.CALENDAR_DATE
3028 
3029             FROM        MSC_CALENDAR_DATES C,
3030                         MSC_SUPPLIES S
3031             WHERE       S.PLAN_ID = p_plan_id
3032             AND         S.SR_INSTANCE_ID = p_instance_id
3033             AND         S.INVENTORY_ITEM_ID = p_item_id
3034             AND         S.ORGANIZATION_ID = p_org_id
3035                         -- Exclude Cancelled Supplies 2460645
3036             AND         NVL(S.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
3037             AND         NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY) <> 0 -- 1243985
3038             AND         C.CALENDAR_CODE = p_cal_code
3039             AND         C.EXCEPTION_SET_ID = p_cal_exc_set_id
3040             AND         C.SR_INSTANCE_ID = S.SR_INSTANCE_ID
3041             AND         C.CALENDAR_DATE BETWEEN TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE))
3042                                 AND TRUNC(NVL(S.LAST_UNIT_COMPLETION_DATE,
3043                                     NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)))
3044             AND         DECODE(TRUNC(S.LAST_UNIT_COMPLETION_DATE), --4135752
3045                                NULL, C.NEXT_SEQ_NUM, C.SEQ_NUM) IS NOT NULL
3046             --2859130
3047             --AND         C.NEXT_DATE >= DECODE(S.ORDER_TYPE, 27, TRUNC(SYSDATE),
3048             --                                                28, TRUNC(SYSDATE),
3049             --                                                C.NEXT_DATE)
3050             --AND         C.NEXT_DATE < NVL(p_itf,
3051             --                             C.NEXT_DATE + 1)
3052             AND         C.CALENDAR_DATE >= DECODE(S.ORDER_TYPE, 27, TRUNC(SYSDATE),
3053                                                             28, TRUNC(SYSDATE),
3054                                                             C.CALENDAR_DATE)
3055             AND         C.CALENDAR_DATE < NVL(p_itf,
3056                                          C.CALENDAR_DATE + 1))CS,
3057             MSC_ITEM_HIERARCHY_MV  MIHM
3058             WHERE
3059 	    --4365873
3060                    CS.INVENTORY_ITEM_ID = MIHM.INVENTORY_ITEM_ID(+)
3061             AND    CS.SR_INSTANCE_ID = MIHM.SR_INSTANCE_ID (+)
3062             AND    CS.ORGANIZATION_ID = MIHM.ORGANIZATION_ID (+)
3063             AND    decode(MIHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
3064             AND    CS.NEXT_DATE >= MIHM.effective_date (+)
3065             AND    CS.NEXT_DATE <= MIHM.disable_date (+)
3066             AND    MIHM.demand_class (+) = p_demand_class
3067            )
3068            ; -- dsting removed order by col25
3069 END item_alloc_avail_unopt_dtls;
3070 
3071 ---------------------------------------------------------------------------
3072 
3073 PROCEDURE item_alloc_avail (
3074    p_optimized_plan     IN NUMBER,
3075    p_item_id            IN NUMBER,
3076    p_org_id             IN NUMBER,
3077    p_instance_id        IN NUMBER,
3078    p_plan_id            IN NUMBER,
3079    p_demand_class       IN VARCHAR2,
3080    p_level_id           IN NUMBER,
3081    p_itf                IN DATE,
3082    p_cal_code           IN VARCHAR2,
3083    p_cal_exc_set_id     IN NUMBER,
3084    p_sys_next_date	IN DATE,			--bug3099066
3085    x_atp_dates          OUT NoCopy MRP_ATP_PUB.date_arr,
3086    x_atp_qtys           OUT NoCopy MRP_ATP_PUB.number_arr
3087 ) IS
3088 BEGIN
3089         IF PG_DEBUG in ('Y', 'C') THEN
3090                 msc_sch_wb.atp_debug('item_alloc_avail');
3091         END IF;
3092 
3093         IF nvl(p_optimized_plan, 2) = 1 THEN
3094                 item_alloc_avail_opt(
3095                      p_item_id,
3096                      p_org_id,
3097                      p_instance_id,
3098                      p_plan_id,
3099                      p_demand_class,
3100                      p_level_id,
3101                      p_itf,
3102                      p_cal_code,
3103                      p_cal_exc_set_id,
3104                      p_sys_next_date,	--bug3099066
3105                      x_atp_dates,
3106                      x_atp_qtys
3107           );
3108         ELSE
3109                 item_alloc_avail_unopt(
3110                      p_item_id,
3111                      p_org_id,
3112                      p_instance_id,
3113                      p_plan_id,
3114                      p_demand_class,
3115                      p_level_id,
3116                      p_itf,
3117                      p_cal_code,
3118                      p_cal_exc_set_id,
3119                      p_sys_next_date,	--bug3099066
3120                      x_atp_dates,
3121                      x_atp_qtys
3122           );
3123         END IF;
3124 END item_alloc_avail;
3125 
3126 PROCEDURE item_alloc_avail_unalloc (
3127    p_optimized_plan     IN NUMBER,
3128    p_item_id            IN NUMBER,
3129    p_org_id             IN NUMBER,
3130    p_instance_id        IN NUMBER,
3131    p_plan_id            IN NUMBER,
3132    p_demand_class       IN VARCHAR2,
3133    p_level_id           IN NUMBER,
3134    p_itf                IN DATE,
3135    p_cal_code           IN VARCHAR2,
3136    p_cal_exc_set_id     IN NUMBER,
3137    p_sys_next_date	IN DATE,			--bug3099066
3138    x_atp_dates          OUT NoCopy MRP_ATP_PUB.date_arr,
3139    x_atp_qtys           OUT NoCopy MRP_ATP_PUB.number_arr,
3140    x_atp_unalloc_qtys   OUT NoCopy MRP_ATP_PUB.number_arr
3141 ) IS
3142 BEGIN
3143         IF PG_DEBUG in ('Y', 'C') THEN
3144                 msc_sch_wb.atp_debug('item_alloc_avail_unalloc: Get unallocated qtys as well');
3145         END IF;
3146 
3147         IF nvl(p_optimized_plan, 2) = 1 THEN
3148                 item_alloc_avail_opt_unalloc(
3149                      p_item_id,
3150                      p_org_id,
3151                      p_instance_id,
3152                      p_plan_id,
3153                      p_demand_class,
3154                      p_level_id,
3155                      p_itf,
3156                      p_cal_code,
3157                      p_cal_exc_set_id,
3158                      p_sys_next_date,		--bug3099066
3159                      x_atp_dates,
3160                      x_atp_qtys,
3161                      x_atp_unalloc_qtys
3162           );
3163         ELSE
3164                 item_alloc_avail_unopt_unalloc(
3165                      p_item_id,
3166                      p_org_id,
3167                      p_instance_id,
3168                      p_plan_id,
3169                      p_demand_class,
3170                      p_level_id,
3171                      p_itf,
3172                      p_cal_code,
3173                      p_cal_exc_set_id,
3174                      p_sys_next_date,		--bug3099066
3175                      x_atp_dates,
3176                      x_atp_qtys,
3177                      x_atp_unalloc_qtys
3178           );
3179         END IF;
3180 END item_alloc_avail_unalloc;
3181 
3182 PROCEDURE item_alloc_avail_dtls (
3183    p_optimized_plan     IN NUMBER,
3184    p_item_id            IN NUMBER,
3185    p_org_id             IN NUMBER,
3186    p_instance_id        IN NUMBER,
3187    p_plan_id            IN NUMBER,
3188    p_demand_class       IN VARCHAR2,
3189    p_level_id           IN NUMBER,
3190    p_itf                IN DATE,
3191    p_cal_code           IN VARCHAR2,
3192    p_cal_exc_set_id     IN NUMBER,
3193    p_sr_item_id         IN NUMBER,
3194    p_level              IN NUMBER,
3195    p_identifier         IN NUMBER,
3196    p_scenario_id        IN NUMBER,
3197    p_uom_code           IN VARCHAR2,
3198    p_sys_next_date	IN DATE)  	--bug3099066
3199    IS
3200 BEGIN
3201         IF PG_DEBUG in ('Y', 'C') THEN
3202                 msc_sch_wb.atp_debug('item_alloc_avail_dtls');
3203         END IF;
3204 
3205         IF nvl(p_optimized_plan, 2) = 1 THEN
3206                 item_alloc_avail_opt_dtls(
3207                    p_item_id,
3208                    p_org_id,
3209                    p_instance_id,
3210                    p_plan_id,
3211                    p_demand_class,
3212                    p_level_id,
3213                    p_itf,
3214                    p_cal_code,
3215                    p_cal_exc_set_id,
3216                    p_sr_item_id,
3217                    p_level,
3218                    p_identifier,
3219                    p_scenario_id,
3220                    p_uom_code,
3221                    p_sys_next_date		--bug3099066
3222                 );
3223         ELSE
3224                 item_alloc_avail_unopt_dtls(
3225                    p_item_id,
3226                    p_org_id,
3227                    p_instance_id,
3228                    p_plan_id,
3229                    p_demand_class,
3230                    p_level_id,
3231                    p_itf,
3232                    p_cal_code,
3233                    p_cal_exc_set_id,
3234                    p_sr_item_id,
3235                    p_level,
3236                    p_identifier,
3237                    p_scenario_id,
3238                    p_uom_code,
3239                    p_sys_next_date		--bug3099066
3240                 );
3241         END IF;
3242 
3243 END item_alloc_avail_dtls;
3244 -- end 2859130
3245 
3246 ---------------------------------------------------------------------------
3247 
3248 PROCEDURE Atp_Demand_Class_Consume(
3249         p_current_atp   IN OUT  NoCopy MRP_ATP_PVT.ATP_Info,
3250         p_steal_atp     IN OUT  NoCopy MRP_ATP_PVT.ATP_Info,
3251         p_atf_date      IN DATE := NULL)   -- time_phased_atp
3252 IS
3253 	i NUMBER; -- index for p_steal_atp
3254 	j NUMBER; -- index for p_current_atp
3255 	k NUMBER; -- starting point for consumption of p_current_atp
3256 	m NUMBER;
3257         l_allowed_stealing_qty NUMBER;
3258 BEGIN
3259 
3260     IF PG_DEBUG in ('Y', 'C') THEN
3261        msc_sch_wb.atp_debug('*******Begin Atp_Demand_Class_Consume Procedure******');
3262 
3263        m := p_current_atp.atp_period.FIRST;
3264        WHILE m is not null LOOP
3265             msc_sch_wb.atp_debug('Atp_Demand_Class_Consume: ' || 'p_current_atp.atp_period , qty  = '||
3266 	    p_current_atp.atp_period(m) ||' : '|| p_current_atp.atp_qty(m) );
3267          m := p_current_atp.atp_qty.Next(m);
3268        END LOOP;
3269 
3270        m := p_steal_atp.atp_period.FIRST;
3271        WHILE m is not null LOOP
3272             msc_sch_wb.atp_debug('Atp_Demand_Class_Consume: ' || 'p_steal_atp.atp_period and qty = '||
3273 	    p_steal_atp.atp_period(m) ||' : '|| p_steal_atp.atp_qty(m));
3274          m := p_steal_atp.atp_qty.Next(m);
3275        END LOOP;
3276     END IF;
3277 
3278     k := p_current_atp.atp_period.FIRST;
3279     -- i is the index for steal_atp
3280 
3281     FOR i in 1..p_steal_atp.atp_qty.COUNT LOOP
3282 
3283         -- consume current_atp (backward) if we have neg in steal_atp
3284         IF (p_steal_atp.atp_qty(i) < 0 ) THEN
3285 
3286             k := NVL(k, 1); --  if k is null, make it as 1 so that
3287                             --  we can find the starting point for the first
3288                             --  element.
3289 
3290             WHILE (k IS NOT NULL)  LOOP
3291              IF k = p_current_atp.atp_period.LAST THEN
3292                -- this is the last record
3293               IF (p_current_atp.atp_period(k) > p_steal_atp.atp_period(i)) THEN
3294                 -- cannot do any consumption since the date from p_steal_atp
3295                 -- is greater than p_ccurrent_atp
3296                 k := NULL;
3297               END IF;
3298               EXIT; -- exit the loop since this is the last record
3299 
3300              ELSE
3301                -- this is not the last record
3302               IF ((p_current_atp.atp_period(k) <= p_steal_atp.atp_period(i))
3303                  AND (p_current_atp.atp_period(k+1)>p_steal_atp.atp_period(i)))
3304               THEN
3305                  -- this is the starting point, we can exit now
3306 		 IF PG_DEBUG in ('Y', 'C') THEN
3307 		    msc_sch_wb.atp_debug('Atp_Demand_Class_Consume: ' || 'exit at k = ' ||to_char(k)||' and i = ' ||to_char(i));
3308 		 END IF;
3309                  EXIT;
3310               ELSE
3311                  k := p_current_atp.atp_period.NEXT(k);
3312               END IF;
3313              END IF;
3314             END LOOP;
3315 
3316             j:= k;
3317 
3318 	IF PG_DEBUG in ('Y', 'C') THEN
3319 	   msc_sch_wb.atp_debug ('Atp_Demand_Class_Consume: ' || 'starting consumption for (i) th element in stealing tab, start from j th element of current tab.  i = '||i || 'j = '||j);
3320 	END IF;
3321 
3322             WHILE (NVL(j, -1) > 0) LOOP
3323 
3324                 -- 02/16: find the allowed stealing quantity
3325                 -- time_phased_atp
3326                 IF ((p_atf_date is not null) and (p_steal_atp.atp_period(i)>p_atf_date) and (p_current_atp.atp_period(j)<=p_atf_date)) THEN
3327                     -- exit loop when crossing time fence
3328                     j := 0;
3329                 -- 02/16: changed the if
3330                 ELSIF (p_current_atp.atp_qty(j) <=0 ) THEN
3331                     --  backward one more period
3332                     j := j-1 ;
3333                 ELSE
3334                     IF (p_current_atp.atp_qty(j) + p_steal_atp.atp_qty(i)< 0) THEN
3335                         -- not enough to cover the shortage
3336                         p_steal_atp.atp_qty(i) := p_steal_atp.atp_qty(i) +
3337                                                   p_current_atp.atp_qty(j);
3338                         --- bug 1657855, remove support for min alloc
3339                         p_current_atp.atp_qty(j) := 0;
3340                         --p_current_atp.limit_qty(j) := 0;
3341                         j := j-1;
3342                     ELSE
3343                         -- enough to cover the shortage
3344 			-- Bug 1665096, index i is being used instead of j for p_current_atp
3345 			-- ngoel 3/2/2001
3346                         --p_current_atp.atp_qty(j) := p_current_atp.atp_qty(i) +
3347                         p_current_atp.atp_qty(j) := p_current_atp.atp_qty(j) +
3348                                                     p_steal_atp.atp_qty(i);
3349                         ---p_current_atp.limit_qty(j) :=
3350                         ---               p_current_atp.limit_qty(j)+
3351                         ---               p_steal_atp.atp_qty(i);
3352                         p_steal_atp.atp_qty(i) := 0;
3353                         j := -1;
3354                     END IF;
3355                 END IF;
3356             END LOOP;
3357         END IF;
3358     END LOOP;
3359 
3360 END Atp_Demand_Class_Consume;
3361 
3362 
3363 PROCEDURE Add_to_Next_Steal_Atp(
3364         p_current_atp      IN OUT  NOCOPY MRP_ATP_PVT.ATP_Info,
3365         p_next_steal_atp   IN OUT  NOCOPY MRP_ATP_PVT.ATP_Info)
3366 IS
3367 	i 			PLS_INTEGER; -- index for p_current_atp
3368 	j 			PLS_INTEGER; -- index for p_next_steal_atp
3369 	k 			PLS_INTEGER; -- index for l_next_steal_atp
3370 	n 			PLS_INTEGER; -- starting point of p_next_steal_atp
3371 	l_next_steal_atp  	MRP_ATP_PVT.ATP_Info; -- this will be the output
3372         l_processed             BOOLEAN := FALSE ;
3373 BEGIN
3374 
3375   -- this procedure will combine p_current_atp and p_next_steal_atp to form
3376   -- a new record of tables and then return as p_next_steal_atp.
3377   -- they need to be ordered by.
3378 
3379   j := p_next_steal_atp.atp_period.FIRST;
3380   k := 0;
3381   FOR i IN 1..p_current_atp.atp_period.COUNT LOOP
3382     -- we only worry about the neg quantity (that's why we need to steal
3383 
3384     IF PG_DEBUG in ('Y', 'C') THEN
3385        msc_sch_wb.atp_debug ('Add_to_Next_Steal_Atp: ' ||  'we are in i loop for current steal, i='||i);
3386     END IF;
3387     IF p_current_atp.atp_qty(i) < 0 THEN
3388        l_processed := FALSE; --1923405
3389        WHILE (j IS NOT NULL) LOOP
3390          IF PG_DEBUG in ('Y', 'C') THEN
3391             msc_sch_wb.atp_debug ('Add_to_Next_Steal_Atp: ' ||  'we are in j loop for next steal, j='||j);
3392          END IF;
3393          IF p_next_steal_atp.atp_qty(j) < 0 THEN
3394 
3395           k := k+1;
3396           l_next_steal_atp.atp_period.Extend;
3397           l_next_steal_atp.atp_qty.Extend;
3398 
3399           IF p_next_steal_atp.atp_period(j) < p_current_atp.atp_period(i) THEN
3400 
3401             -- we add this to l_next_steal_atp
3402             l_next_steal_atp.atp_period(k) := p_next_steal_atp.atp_period(j);
3403             l_next_steal_atp.atp_qty(k) := p_next_steal_atp.atp_qty(j);
3404 
3405           ELSIF p_next_steal_atp.atp_period(j)=p_current_atp.atp_period(i) THEN
3406 
3407             -- both record (p_next_steal_atp and p_current_atp) are on the same
3408             -- date.  we need to sum them up
3409             l_processed := TRUE; -- 1923405
3410             l_next_steal_atp.atp_period(k) := p_next_steal_atp.atp_period(j);
3411             l_next_steal_atp.atp_qty(k) := p_next_steal_atp.atp_qty(j) +
3412                                            p_current_atp.atp_qty(i);
3413             -- j := j+1;
3414             j := p_next_steal_atp.atp_period.NEXT(j);
3415             EXIT; -- exit the loop since we had done group by before. so
3416                   -- we don't need to go to next record any more
3417           ELSE -- this is the greater part
3418             l_processed := TRUE; -- 1923405
3419             l_next_steal_atp.atp_period(k) := p_current_atp.atp_period(i);
3420             l_next_steal_atp.atp_qty(k) := p_current_atp.atp_qty(i);
3421             EXIT; -- exit the loop since we had done group by before.
3422 
3423           END IF;
3424          END IF; -- p_next_steal_atp.atp_qty < 0
3425          j := p_next_steal_atp.atp_period.NEXT(j) ;
3426        END LOOP;
3427 
3428        -- 1923405: undo 1739629, add l_processed = FALSE condition
3429        IF (j is null) AND (l_processed = FALSE) THEN
3430          -- this means p_next_steal_atp is over,
3431          -- so we don't need to worry about p_next_steak_atp,
3432          -- we just keep add p_current_atp to l_next_steal_atp
3433          -- if they are not added before
3434          k := k+1;
3435          l_next_steal_atp.atp_period.Extend;
3436          l_next_steal_atp.atp_qty.Extend;
3437 
3438          l_next_steal_atp.atp_period(k) := p_current_atp.atp_period(i);
3439          l_next_steal_atp.atp_qty(k) := p_current_atp.atp_qty(i);
3440        END IF;
3441        -- AATP Forward Consumption rajjain begin
3442        -- After adding negatives from p_current_atp to p_next_steal_atp
3443        -- we update the the negatives in p_current_atp to zero.
3444        p_current_atp.atp_qty(i) := 0;
3445        -- AATP Forward Consumption rajjain end
3446     END IF; -- p_current_atp.atp_qty < 0
3447   END LOOP;
3448 
3449   -- now we have taken care of all p_current_atp and part of
3450   -- p_next_steal_atp. now we need to take care the rest of p_next_steal_atp
3451 
3452   -- FOR j IN n..p_next_steal_atp.atp_period.COUNT LOOP
3453   WHILE j is not null LOOP
3454          IF p_next_steal_atp.atp_qty(j) < 0 THEN
3455             -- we add this to l_next_steal_atp
3456             k := k+1;
3457             l_next_steal_atp.atp_period.Extend;
3458             l_next_steal_atp.atp_qty.Extend;
3459             l_next_steal_atp.atp_period(k) := p_next_steal_atp.atp_period(j);
3460             l_next_steal_atp.atp_qty(k) := p_next_steal_atp.atp_qty(j);
3461          END IF;
3462          j := p_next_steal_atp.atp_period.NEXT(j);
3463   END LOOP;
3464 
3465   p_next_steal_atp := l_next_steal_atp;
3466 
3467 END Add_to_Next_Steal_Atp;
3468 
3469 
3470 PROCEDURE Item_Alloc_Cum_Atp(
3471 	p_plan_id 	      IN NUMBER,
3472 	p_level               IN NUMBER,
3473 	p_identifier          IN NUMBER,
3474 	p_scenario_id         IN NUMBER,
3475 	p_inventory_item_id   IN NUMBER,
3476 	p_organization_id     IN NUMBER,
3477 	p_instance_id         IN NUMBER,
3478 	p_demand_class        IN VARCHAR2,
3479 	p_request_date        IN DATE,
3480 	p_insert_flag         IN NUMBER,
3481 	x_atp_info            OUT  NoCopy MRP_ATP_PVT.ATP_Info,
3482 	x_atp_period          OUT  NoCopy MRP_ATP_PUB.ATP_Period_Typ,
3483 	x_atp_supply_demand   OUT  NoCopy MRP_ATP_PUB.ATP_Supply_Demand_Typ,
3484         p_get_mat_in_rec      IN   MSC_ATP_REQ.get_mat_in_rec,
3485 	p_request_item_id     IN NUMBER, -- For time_phased_atp
3486 	p_atf_date            IN DATE)   -- For time_phased_atp
3487 IS
3488 	l_infinite_time_fence_date	DATE;
3489 	l_default_atp_rule_id           NUMBER;
3490 	l_calendar_exception_set_id     NUMBER;
3491         l_level_id                      NUMBER;
3492 	l_priority			NUMBER := 1;
3493 	l_allocation_percent		NUMBER := 100;
3494 	l_inv_item_id			NUMBER;
3495 	l_null_num  			NUMBER := null;
3496 	l_steal_period_quantity		NUMBER;
3497 	l_demand_class			VARCHAR2(80);
3498 	l_uom_code			VARCHAR2(3);
3499 	l_null_char    			VARCHAR2(3) := null;
3500 	l_return_status			VARCHAR2(1);
3501 	l_default_demand_class          VARCHAR2(34);
3502 	l_calendar_code                 VARCHAR2(14);
3503 	i				PLS_INTEGER;
3504 	mm				PLS_INTEGER;
3505 	ii                              PLS_INTEGER;
3506 	jj                              PLS_INTEGER;
3507 	j				PLS_INTEGER;
3508 	k				PLS_INTEGER;
3509 	l_demand_class_tab		MRP_ATP_PUB.char80_arr
3510        		                            := MRP_ATP_PUB.char80_arr();
3511 	l_demand_class_priority_tab	MRP_ATP_PUB.number_arr
3512        		                            := MRP_ATP_PUB.number_arr();
3513 	l_current_atp			MRP_ATP_PVT.ATP_Info;
3514 	l_next_steal_atp		MRP_ATP_PVT.ATP_Info;
3515 	l_current_steal_atp             MRP_ATP_PVT.ATP_Info;
3516         l_temp_atp                      MRP_ATP_PVT.ATP_Info;
3517 	l_null_steal_atp		MRP_ATP_PVT.ATP_Info;
3518 
3519         -- AATP Forward Consumption rajjain begin
3520 	l_unallocated_atp		MRP_ATP_PVT.ATP_Info;
3521 	l_lowest_priority_demand_class	VARCHAR2(80);
3522         l_lowest_priority		NUMBER;
3523 	l_fw_consume_tab		MRP_ATP_PUB.number_arr
3524        		                            := MRP_ATP_PUB.number_arr();
3525 	l_allocation_percent_tab	MRP_ATP_PUB.number_arr
3526        		                            := MRP_ATP_PUB.number_arr();
3527 	l_next_fw_consume		PLS_INTEGER := 0;
3528 	l_lowest_cust_priority		NUMBER;
3529 	l_lowest_site_priority		NUMBER;
3530 	-- AATP Forward Consumption rajjain end
3531 
3532         -- 1680719
3533         l_class_tab                     MRP_ATP_PUB.char30_arr
3534                                             := MRP_ATP_PUB.char30_arr();
3535         l_partner_tab                   MRP_ATP_PUB.number_arr
3536                                             := MRP_ATP_PUB.number_arr();
3537         l_class_next_steal_atp          MRP_ATP_PVT.ATP_Info;
3538         l_partner_next_steal_atp        MRP_ATP_PVT.ATP_Info;
3539         l_class_curr_steal_atp          MRP_ATP_PVT.ATP_Info;
3540         l_partner_curr_steal_atp        MRP_ATP_PVT.ATP_Info;
3541         l_pos1                            NUMBER;
3542         l_pos2                            NUMBER;
3543         delim     constant varchar2(1) := fnd_global.local_chr(13);
3544 
3545         -- krajan - 04/01/02 - Variable added for fsteal
3546         l_org_code                      VARCHAR2(7);
3547 
3548 	l_temp_atp_supply_demand        MRP_ATP_PUB.ATP_Supply_Demand_Typ;
3549 	l_sysdate 			DATE := trunc(sysdate);--4135752
3550 
3551 	-- time_phased_atp
3552 	l_time_phased_atp               VARCHAR2(1) := 'N';
3553 	l_pf_item_id                    NUMBER;
3554         l_item_to_use                   NUMBER;
3555 
3556 
3557 BEGIN
3558 
3559   IF PG_DEBUG in ('Y', 'C') THEN
3560      msc_sch_wb.atp_debug('******* Item_Alloc_Cum_Atp *******');
3561      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'p_plan_id =' || p_plan_id );
3562      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'p_level =' || p_level );
3563      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'p_identifier =' || p_identifier);
3564      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'p_scenario_id =' || p_scenario_id);
3565      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'p_instance_id =' || p_instance_id);
3566      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'p_inventory_item_id =' || p_inventory_item_id);
3567      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'p_request_item_id =' || p_request_item_id);
3568      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'p_organization_id =' || p_organization_id);
3569      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'p_demand_class =' || p_demand_class);
3570      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'p_request_date =' || p_request_date );
3571      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'p_insert_flag =' || p_insert_flag );
3572      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'p_atf_date =' || p_atf_date );
3573      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'p_get_mat_in_rec.sys_next_osc_date =' || p_get_mat_in_rec.sys_next_osc_date); --bug3333114
3574   END IF;
3575 
3576 
3577   /*-----------------------------------------------------------------------------------------+
3578   | rajjain begin 10/08/2002                                                                 |
3579   |                                                                                          |
3580   | Logic:                                                                                   |
3581   |                                                                                          |
3582   | Step 1: Find all the demand classes in the hierarchy                                     |
3583   |                                                                                          |
3584   | Step 2: If "MSC:Allocated ATP Forward Consumption Method" profile is set to              |
3585   |         "Reduce future supply from lowest priority", store the type of forward           |
3586   |         consumption in a PL/SQL table                                                    |
3587   |                                                                                          |
3588   | Step 3: If "MSC:Allocated ATP Forward Consumption Method" profile is set to              |
3589   |         "Reduce available supply from any priority", calculate unallocated availability  |
3590   |         Do backward consumption                                                          |
3591   |         Do forward consumption, do accumulation                                          |
3592   |                                                                                          |
3593   | Step 4: For each demand class DCi ->                                                     |
3594   |         1. get the daily net availability                                                |
3595   |         2. do backward consumption for DCi                                               |
3596   |         3. do demand class consumption if DC1 to DC(i-1) has any negative bucket         |
3597   |         4. If "MSC:Allocated ATP Forward Consumption Method" profile is set to           |
3598   |            a) "Reduce future supply from lowest priority" (Method 1)                     |
3599   |               Do the type of forward consumption we stored in the PL/SQL table           |
3600   |               Do accumulation                                                            |
3601   |            b) "Reduce available supply from any priority" (Method 2)                     |
3602   |               Do accumulation                                                            |
3603   |               Use the unallocated cum and calculated the adjusted cum                    |
3604   |                                                                                          |
3605   | Step 5: Exit from the l_demand_class_tab loop if l_demand_class is the requested DC      |
3606   |                                                                                          |
3607   +-----------------------------------------------------------------------------------------*/
3608 
3609  -- time_phased_atp
3610  IF (p_inventory_item_id <> p_request_item_id and p_atf_date is not null) THEN
3611         l_time_phased_atp := 'Y';
3612         l_pf_item_id := MSC_ATP_PVT.G_ITEM_INFO_REC.product_family_id;
3613  END IF;
3614 
3615  -- rajjain 01/29/2003 begin Bug 2737596
3616  IF p_identifier = -1 THEN
3617     BEGIN
3618        SELECT inventory_item_id, uom_code
3619        INTO   l_inv_item_id, l_uom_code
3620        FROM   msc_system_items
3621        WHERE  plan_id = p_plan_id
3622        AND    sr_instance_id = p_instance_id
3623        AND    organization_id = p_organization_id
3624        AND    sr_inventory_item_id = p_inventory_item_id;
3625     EXCEPTION
3626        WHEN OTHERS THEN
3627           IF PG_DEBUG in ('Y', 'C') THEN
3628              msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error selecting uom_code for the item');
3629           END IF;
3630     END;
3631  ELSE
3632     -- we need not select this in case of ATP QUERY as this information is available in G_ITEM_INFO_REC
3633     l_inv_item_id := MSC_ATP_PVT.G_ITEM_INFO_REC.inventory_item_id;
3634     l_uom_code := MSC_ATP_PVT.G_ITEM_INFO_REC.uom_code;
3635  END IF;
3636  -- rajjain 01/29/2003 end Bug 2737596
3637 
3638  /* New allocation logic for time_phased_atp changes begin
3639     For time phased ATP scenarios if allocation rule at member item is not defined then within ATF use
3640     allocation rule defined at family */
3641  IF l_time_phased_atp = 'Y' THEN
3642      IF p_request_date <= p_atf_date THEN
3643          IF MSC_ATP_PVT.G_MEM_RULE_WITHIN_ATF = 'Y' THEN
3644              l_item_to_use := l_inv_item_id;
3645          ELSE
3646              l_item_to_use := l_pf_item_id;
3647          END IF;
3648      ELSE
3649          l_item_to_use := l_pf_item_id;
3650      END IF;
3651  ELSE
3652      l_item_to_use := l_inv_item_id;
3653  END IF;
3654 
3655  IF PG_DEBUG in ('Y', 'C') THEN
3656     msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Item to be used = ' || l_item_to_use);
3657  END IF;
3658  /* New allocation logic for time_phased_atp changes end */
3659 
3660  -- select the priority  and allocation_percent for that item/demand class.
3661  -- if no data found, check if this item has a valid allocation rule.
3662  -- otherwise return error.
3663  IF p_scenario_id <> -1 THEN
3664     MSC_AATP_PVT.Get_DC_Info(p_instance_id, l_item_to_use, p_organization_id, null, null,
3665      p_demand_class, p_request_date, l_level_id, l_priority, l_allocation_percent, l_return_status);
3666 
3667     IF  l_return_status = FND_API.G_RET_STS_ERROR THEN
3668         IF PG_DEBUG in ('Y', 'C') THEN
3669            msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error retrieving Priority and Demand Class');
3670         END IF;
3671     END IF;
3672  ELSE
3673      l_priority := -1;
3674      l_allocation_percent := NULL;
3675  END IF;
3676 
3677   -- find the demand classes that have priority higher (small number) than
3678   -- the requested demand class
3679 
3680   IF PG_DEBUG in ('Y', 'C') THEN
3681      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_allocation_percent = '||l_allocation_percent);
3682   END IF;
3683 
3684  /* rajjain 01/29/2003 Bug 2737596
3685     We don't need to select all demand classes in case this procdure is
3686     called from MSC_AATP_PVT.VIEW_ALLOCATION for view total*/
3687  IF p_identifier <> -1 or (p_identifier = -1 and p_scenario_id <> -1) THEN
3688 
3689   IF PG_DEBUG in ('Y', 'C') THEN
3690      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'before select the high priority demand class');
3691   END IF;
3692 
3693   /*AATP Forward Consumption rajjain begin
3694   1. Now we select all the demand classes in l_demand_class_tab irrespective of demand class for which
3695      Inquiry has come.
3696   2. l_allocation_percent_tab stores the allocation_percent for the demand classes.
3697   3. If inquiry is at level 2 or 3 we also populate l_fw_consume_tab which stores the type
3698      of forward consumption. We bulk collect 0 in l_fw_consume_tab to initialise it.
3699 
3700   Note:
3701   1. We order by allocation_percent in demand class scenario if we have multiple
3702      demand classes at same priority.
3703   2. We are not doing this for customer class scenario as it may impact performance
3704      negatively due to extra joins.*/
3705 
3706   /* time_phased_atp changes begin */
3707   -- bug 1680719
3708   IF l_level_id = -1 THEN
3709 
3710     IF PG_DEBUG in ('Y', 'C') THEN
3711        msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_level_id = '||l_level_id);
3712     END IF;
3713     SELECT demand_class, priority, allocation_percent
3714     BULK COLLECT INTO l_demand_class_tab, l_demand_class_priority_tab, l_allocation_percent_tab
3715     FROM   msc_item_hierarchy_mv
3716     WHERE  inventory_item_id = l_item_to_use /* New allocation logic for time_phased_atp changes*/
3717     AND	   organization_id = p_organization_id
3718     AND    sr_instance_id = p_instance_id
3719     AND    p_request_date BETWEEN effective_date AND disable_date
3720     AND    level_id = l_level_id
3721     --rajjain added demand_class asc
3722     ORDER BY priority asc, allocation_percent desc, demand_class asc;
3723 
3724   ELSIF l_level_id = 1 THEN
3725 
3726     IF PG_DEBUG in ('Y', 'C') THEN
3727        msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_level_id = '||l_level_id);
3728     END IF;
3729     SELECT demand_class, priority, allocation_percent
3730     BULK COLLECT INTO l_demand_class_tab, l_demand_class_priority_tab, l_allocation_percent_tab
3731     FROM   msc_item_hierarchy_mv
3732     WHERE  inventory_item_id = l_item_to_use /* New allocation logic for time_phased_atp changes*/
3733     AND    organization_id = p_organization_id
3734     AND    sr_instance_id = p_instance_id
3735     AND    p_request_date BETWEEN effective_date AND disable_date
3736     AND    level_id = l_level_id
3737     ORDER BY priority asc, class asc;
3738 
3739   ELSIF l_level_id = 2 THEN
3740 
3741     IF PG_DEBUG in ('Y', 'C') THEN
3742        msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_level_id = '||l_level_id);
3743     END IF;
3744     SELECT mv1.demand_class, mv1.priority, mv1.class, mv1.partner_id, 0, mv1.allocation_percent
3745     BULK COLLECT INTO l_demand_class_tab, l_demand_class_priority_tab,
3746                       l_class_tab, l_partner_tab, l_fw_consume_tab, l_allocation_percent_tab
3747     FROM   msc_item_hierarchy_mv mv1
3748     WHERE  mv1.inventory_item_id = l_item_to_use /* New allocation logic for time_phased_atp changes*/
3749     AND    mv1.organization_id = p_organization_id
3750     AND    mv1.sr_instance_id = p_instance_id
3751     AND    p_request_date BETWEEN mv1.effective_date AND mv1.disable_date
3752     AND    mv1.level_id = l_level_id
3753     ORDER BY trunc(mv1.priority, -3), mv1.class ,
3754              trunc(mv1.priority, -2), mv1.partner_id;
3755 
3756 
3757   ELSIF l_level_id = 3 THEN
3758 
3759     -- bug 1680719
3760     -- we need to select the class, partner_id, partner_site_id
3761 
3762     IF PG_DEBUG in ('Y', 'C') THEN
3763        msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_level_id = '||l_level_id);
3764     END IF;
3765     SELECT mv1.demand_class, mv1.priority, mv1.class, mv1.partner_id, 0, mv1.allocation_percent
3766     BULK COLLECT INTO l_demand_class_tab, l_demand_class_priority_tab,
3767                       l_class_tab, l_partner_tab, l_fw_consume_tab, l_allocation_percent_tab
3768     FROM   msc_item_hierarchy_mv mv1
3769     WHERE  mv1.inventory_item_id = l_item_to_use /* New allocation logic for time_phased_atp changes*/
3770     AND    mv1.organization_id = p_organization_id
3771     AND    mv1.sr_instance_id = p_instance_id
3772     AND    p_request_date BETWEEN mv1.effective_date AND mv1.disable_date
3773     AND    mv1.level_id = l_level_id
3774     ORDER BY trunc(mv1.priority, -3), mv1.class ,
3775              trunc(mv1.priority, -2), mv1.partner_id,
3776              mv1.priority, mv1.partner_site_id;
3777 
3778   END IF;
3779   -- AATP Forward Consumption rajjain end
3780   /* time_phased_atp changes end */
3781 
3782   mm := l_demand_class_tab.FIRST;
3783   IF PG_DEBUG in ('Y', 'C') THEN
3784      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_demand_class_tab.count = '||
3785                         l_demand_class_tab.count);
3786      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_demand_class_priority_tab.count = '||
3787                         l_demand_class_priority_tab.count);
3788      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_allocation_percent_tab.count = '||
3789                         l_allocation_percent_tab.count);
3790      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_class_tab.count = '||
3791                         l_class_tab.count);
3792      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_partner_tab.count = '||
3793                         l_partner_tab.count);
3794   END IF;
3795 
3796  END IF;
3797 
3798  -- Need to add the requested demand class in case call is from
3799  -- View_Allocation for View Total, else l_demand_class_tab remains empty.
3800  IF l_demand_class_tab.count = 0 THEN
3801 --/* 1665110
3802   -- add the request demand class into the list
3803   l_demand_class_tab.Extend;
3804   l_demand_class_priority_tab.Extend;
3805   -- krajan: 2745212
3806   l_allocation_percent_tab.Extend;
3807 
3808   i := l_demand_class_tab.COUNT;
3809   l_demand_class_priority_tab(i) := l_priority;
3810   l_demand_class_tab(i) := p_demand_class;
3811   -- 2745212
3812   l_allocation_percent_tab(i) := 100;
3813 
3814   -- 1680719
3815   IF l_level_id in (2, 3) THEN
3816       l_class_tab.Extend;
3817       l_partner_tab.Extend;
3818       -- krajan : 2745212
3819       l_fw_consume_tab.Extend;
3820 
3821       l_pos1 := instr(p_demand_class,delim,1,1);
3822       l_pos2 := instr(p_demand_class,delim,1,2);
3823       l_class_tab(i) := substr(p_demand_class,1,l_pos1-1);
3824       IF l_pos2 = 0 THEN
3825         l_partner_tab(i) := substr(p_demand_class,l_pos1+1);
3826       ELSE
3827         l_partner_tab(i) := substr(p_demand_class,l_pos1+1,l_pos2-l_pos1-1) ;
3828       END IF;
3829   END IF;
3830 --1665110 */
3831  END IF;
3832 
3833   mm := l_demand_class_tab.FIRST;
3834   IF PG_DEBUG in ('Y', 'C') THEN
3835      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_demand_class_tab.count = '|| l_demand_class_tab.count);
3836   END IF;
3837 
3838   /*AATP Forward Consumption rajjain begin
3839   1. Level_id in (-1, 1) ->
3840      a. We calculate the l_lowest_priority and l_lowest_priority_demand_class.
3841      b. l_lowest_priority_demand_class is the lowest demand class with non zero
3842         allocation percentage.
3843      c. l_lowest_priority is the priority of l_lowest_priority_demand_class.
3844   2. Level_id in (2,3) ->
3845      a. We form the l_fw_consume_tab. This takes values from 0-4 which depends on
3846         type of forward consumption we do for this demand class.*/
3847   mm := l_demand_class_tab.LAST;
3848   -- we go bottom up in l_demand_class_tab
3849   IF l_level_id in (-1, 1) THEN
3850 
3851      WHILE mm is not null LOOP
3852 
3853      msc_sch_wb.atp_debug('---------------------l_demand_class_tab'||l_demand_class_tab(mm));--6359986
3854      IF (l_demand_class_tab(mm) ='-1') then							--6359986
3855       IF(MSC_ATP_PVT.G_ZERO_ALLOCATION_PERC = 'N') THEN	--6359986
3856       IF l_allocation_percent_tab(mm) <> 0 THEN
3857 	  l_lowest_priority_demand_class := l_demand_class_tab(mm);
3858 	  l_lowest_priority := l_demand_class_priority_tab(mm);
3859 	  IF PG_DEBUG in ('Y', 'C') THEN
3860 	     msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_lowest_priority_demand_class: ' ||
3861 	                        l_lowest_priority_demand_class);
3862 	     msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_lowest_priority: ' ||
3863 	                        l_lowest_priority);
3864 	  END IF;
3865 	  EXIT;
3866       END IF;
3867       ELSE			--6359986 start
3868           l_lowest_priority_demand_class := l_demand_class_tab(mm);
3869 	  			l_lowest_priority := l_demand_class_priority_tab(mm);
3870 	  			IF PG_DEBUG in ('Y', 'C') THEN
3871 	     			msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_lowest_priority_demand_class: ' ||
3872 	                        l_lowest_priority_demand_class);
3873 	     			msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_lowest_priority: ' ||
3874 	                        l_lowest_priority);
3875 	  			END IF;
3876 	  			EXIT;
3877       END IF;
3878 
3879      ELSE
3880           l_lowest_priority_demand_class := l_demand_class_tab(mm);
3881 	  			l_lowest_priority := l_demand_class_priority_tab(mm);
3882 	  			IF PG_DEBUG in ('Y', 'C') THEN
3883 	     			msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_lowest_priority_demand_class: ' ||
3884 	                        l_lowest_priority_demand_class);
3885 	     			msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_lowest_priority: ' ||
3886 	                        l_lowest_priority);
3887 	  			END IF;
3888 	  			EXIT;
3889      END IF;	--6359986 end
3890       mm := l_demand_class_tab.Prior(mm);
3891      END LOOP;
3892   ELSIF l_level_id in (2, 3) THEN
3893     WHILE mm is not null LOOP
3894      IF (REPLACE(l_demand_class_tab(mm),FND_GLOBAL.LOCAL_CHR(13),' ') in ('-1','-1 -1 -1','-1 -1')) then --6359986
3895      IF(MSC_ATP_PVT.G_ZERO_ALLOCATION_PERC = 'N') THEN ----6359986
3896      IF l_allocation_percent_tab(mm) <> 0 THEN
3897         -- this is the lowest demand class with non zero allocation percentage
3898         l_fw_consume_tab(mm) := 4;
3899 				l_lowest_priority_demand_class := l_demand_class_tab(mm);
3900 				--5634348
3901 	      l_lowest_priority := trunc(l_demand_class_priority_tab(mm), -3);
3902         -- krajan : 2745212
3903         mm := l_demand_class_tab.Prior(mm);
3904 
3905 	exit;
3906      ELSE
3907         -- this is demand class with zero allocation percentage
3908         -- this dc needs to do forward consumption for its own negatives
3909         l_fw_consume_tab(mm) := 1;
3910      END IF;
3911      ELSE		--6359986 start
3912         -- this is the lowest demand class with non zero allocation percentage
3913         l_lowest_priority_demand_class := l_demand_class_tab(mm);
3914 	      --5634348
3915 	      l_lowest_priority := trunc(l_demand_class_priority_tab(mm), -3);
3916         -- krajan : 2745212
3917 
3918         l_fw_consume_tab(mm) := 4;
3919         mm := l_demand_class_tab.Prior(mm);
3920         exit;
3921      END IF;
3922     ELSE
3923        -- this is the lowest demand class with non zero allocation percentage
3924         l_lowest_priority_demand_class := l_demand_class_tab(mm);
3925 	      --5634348
3926 	      l_lowest_priority := trunc(l_demand_class_priority_tab(mm), -3);
3927         -- krajan : 2745212
3928         l_fw_consume_tab(mm) := 4;
3929         mm := l_demand_class_tab.Prior(mm);
3930         exit;
3931     END IF; --6359986 end
3932       msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'mm: '||mm);
3933       mm := l_demand_class_tab.Prior(mm);
3934     END LOOP;
3935     --l_lowest_priority := trunc(l_demand_class_priority_tab(mm), -3);	--5634348, commenting it
3936 
3937     WHILE mm is not null LOOP
3938      IF l_next_fw_consume <> 0 THEN
3939         IF l_allocation_percent_tab(mm) <> 0 THEN
3940             IF l_next_fw_consume = 2 THEN
3941               -- this is the lowest priority dc with non zero allocation % at current customer level
3942               -- set the lowest site priority
3943               l_lowest_site_priority := l_demand_class_priority_tab(mm);
3944             ELSIF l_next_fw_consume = 3 THEN
3945               -- this is the lowest priority customer with non zero allocation % at current
3946               -- customer class level. set the lowest customer and site priority
3947               l_lowest_cust_priority := trunc(l_demand_class_priority_tab(mm), -2);
3948               l_lowest_site_priority := l_demand_class_priority_tab(mm);
3949             END IF;
3950             l_fw_consume_tab(mm) := l_next_fw_consume;
3951             l_next_fw_consume := 0; -- reset l_next_fw_consume to zero
3952         ELSE
3953             -- dc with zero allocation %
3954             l_fw_consume_tab(mm) := 1;
3955         END IF;
3956      ELSIF l_class_tab(mm) <> l_class_tab(mm+1) THEN
3957       --customer class changed
3958       IF trunc(l_demand_class_priority_tab(mm), -3)<>trunc(l_demand_class_priority_tab(mm+1), -3) THEN
3959          --customer class priority changed
3960          exit;
3961       ELSE
3962          --reset the lowest customer and site priority at this level to null
3963          l_lowest_cust_priority := null;
3964          l_lowest_site_priority := null;
3965          IF l_allocation_percent_tab(mm) = 0 THEN
3966             -- allocation % zero set l_next_fw_consume
3967             l_next_fw_consume := 3;
3968             l_fw_consume_tab(mm) := 1;
3969          ELSE
3970             l_fw_consume_tab(mm) := 3;
3971             l_lowest_cust_priority := trunc(l_demand_class_priority_tab(mm), -2);
3972             l_lowest_site_priority := l_demand_class_priority_tab(mm);
3973          END IF;
3974       END IF;
3975      ELSIF l_partner_tab(mm) <> l_partner_tab(mm+1) THEN
3976        --customer changed
3977        --reset the lowest site priority at this level to null
3978        l_lowest_site_priority := null;
3979        IF trunc(l_demand_class_priority_tab(mm), -2) = trunc(l_demand_class_priority_tab(mm+1), -2)
3980           AND (l_lowest_cust_priority is null
3981                OR trunc(l_demand_class_priority_tab(mm), -2) = l_lowest_cust_priority) THEN
3982          --customer priority same
3983          IF l_allocation_percent_tab(mm) = 0 THEN
3984             -- allocation % zero set l_next_fw_consume
3985             l_next_fw_consume := 2;
3986             l_fw_consume_tab(mm) := 1;
3987          ELSE
3988             l_fw_consume_tab(mm) := 2;
3989             l_lowest_site_priority := l_demand_class_priority_tab(mm);
3990          END IF;
3991        END IF;
3992      ELSIF l_demand_class_priority_tab(mm) = l_demand_class_priority_tab(mm+1)
3993            AND (l_lowest_site_priority is null OR l_demand_class_priority_tab(mm) = l_lowest_site_priority) THEN
3994        --site priority same
3995        l_fw_consume_tab(mm) := 1;
3996      END IF;
3997 
3998      mm := l_demand_class_tab.Prior(mm);
3999 
4000     END LOOP;
4001   END IF;
4002   --bug3948494  For demand_class cases, if request comes for highest priority
4003   -- and l_lowest_priority is > 1 then process only requested demand class
4004   IF l_level_id = -1 and l_priority = 1 and l_lowest_priority > 1 THEN
4005 
4006     l_demand_class_tab.Delete;
4007     l_demand_class_priority_tab.Delete;
4008     l_allocation_percent_tab.Delete;
4009 
4010     l_demand_class_tab.Extend;
4011     l_demand_class_priority_tab.Extend;
4012     l_allocation_percent_tab.Extend;
4013 
4014     i := l_demand_class_tab.COUNT;
4015     l_demand_class_priority_tab(i) := l_priority;
4016     l_demand_class_tab(i) := p_demand_class;
4017     l_allocation_percent_tab(i) := l_allocation_percent;
4018 
4019     IF PG_DEBUG in ('Y', 'C') THEN
4020       msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_demand_class_tab.count = '||
4021                         l_demand_class_tab.count);
4022       msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_demand_class_priority_tab.count = '||
4023                         l_demand_class_priority_tab.count);
4024       msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_allocation_percent_tab.count = '||
4025                         l_allocation_percent_tab.count);
4026     END IF;
4027 
4028   END IF;
4029 
4030   mm := l_fw_consume_tab.FIRST;
4031   IF PG_DEBUG in ('Y', 'C') THEN
4032      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_fw_consume_tab.count = '|| l_fw_consume_tab.count);
4033      WHILE mm is not null LOOP
4034         msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_fw_consume_tab = '||
4035            l_fw_consume_tab(mm));
4036 
4037         -- krajan : 2745212
4038         mm := l_fw_consume_tab.Next(mm);
4039 
4040      END LOOP;
4041   END IF;
4042   -- AATP Forward Consumption rajjain end 10/21/2002
4043 
4044   -- for performance reason, we need to get the following info and
4045   -- store in variables instead of joining it
4046 
4047   -- krajan: 04/01/02 Added l_org_code to call.
4048   /* Modularize Item and Org Info */
4049   -- changed Call
4050   MSC_ATP_PROC.get_global_org_info(p_instance_id, p_organization_id);
4051   l_default_atp_rule_id := MSC_ATP_PVT.G_ORG_INFO_REC.default_atp_rule_id;
4052   l_calendar_code := MSC_ATP_PVT.G_ORG_INFO_REC.cal_code;
4053   l_calendar_exception_set_id := MSC_ATP_PVT.G_ORG_INFO_REC.cal_exception_set_id;
4054   l_default_demand_class := MSC_ATP_PVT.G_ORG_INFO_REC.default_demand_class;
4055   l_org_code := MSC_ATP_PVT.G_ORG_INFO_REC.org_code;
4056   /* Modularize Item and Org Info */
4057 
4058   IF PG_DEBUG in ('Y', 'C') THEN
4059      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_default_atp_rule_id='|| l_default_atp_rule_id);
4060      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_calendar_code='||l_calendar_code);
4061      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_calendar_exception_set_id'|| l_calendar_exception_set_id);
4062      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_default_demand_class'|| l_default_demand_class);
4063      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_org_code'|| l_org_code);
4064   END IF;
4065   --diag_atp
4066   -- rajjain 01/29/2003 Bug 2737596
4067   -- Need to call get_infinite_time_fence_date procedure in case call is from View allocation
4068   -- as p_get_mat_in_rec will not be populated
4069   IF p_identifier = -1 THEN
4070      -- get the infinite time fence date if it exists
4071      l_infinite_time_fence_date := MSC_ATP_FUNC.get_infinite_time_fence_date(p_instance_id,
4072              p_inventory_item_id,p_organization_id, p_plan_id);
4073   ELSE
4074      ---diag_atp
4075      l_infinite_time_fence_date := p_get_mat_in_rec.infinite_time_fence_date;
4076   END IF;
4077   IF PG_DEBUG in ('Y', 'C') THEN
4078      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_infinite_time_fence_date'|| l_infinite_time_fence_date);
4079   END IF;
4080 
4081   -- Now go demand class by demand class and calculate the allocated picture
4082   FOR i in 1..l_demand_class_tab.COUNT LOOP
4083 
4084           l_demand_class := l_demand_class_tab(i);
4085           IF PG_DEBUG in ('Y', 'C') THEN
4086              msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'in i loop, i = '||i);
4087              msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_demand_class = '||l_demand_class);
4088           END IF;
4089     -- get the daily net availability for DCi
4090     IF (NVL(p_insert_flag, 0) = 0  OR l_demand_class <> p_demand_class) THEN
4091        -- we don't want details
4092        IF PG_DEBUG in ('Y', 'C') THEN
4093           msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' ||
4094                              'Inside IF (NVL(p_insert_flag, 0) = 0');
4095        END IF;
4096 
4097        /* AATP Forward Consumption rajjain
4098        We calculate unallocated availability alongwith allocated availability calculation
4099        for first demand class if Forward consumption method is method2*/
4100        IF i=1 AND G_ATP_FW_CONSUME_METHOD = 2 THEN
4101           IF PG_DEBUG in ('Y', 'C') THEN
4102              msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' ||
4103                                 'Inside IF i=1 AND Method2');
4104           END IF;
4105           -- time_phased_atp
4106           IF l_time_phased_atp='N' THEN
4107                   -- 2859130
4108                   item_alloc_avail_unalloc(
4109                      p_get_mat_in_rec.optimized_plan,
4110                      l_inv_item_id,
4111                      p_organization_id,
4112                      p_instance_id,
4113                      p_plan_id,
4114                      l_demand_class,
4115                      l_level_id,
4116                      l_infinite_time_fence_date,
4117                      l_calendar_code,
4118                      l_calendar_exception_set_id,
4119                      p_get_mat_in_rec.sys_next_osc_date,			--bug3099066 bug3333114
4120                      l_current_atp.atp_period,
4121                      l_current_atp.atp_qty,
4122                      l_unallocated_atp.atp_qty
4123                   );
4124           ELSE
4125                   MSC_ATP_PF.Item_Alloc_Avail_Pf_Unalloc(
4126                      l_inv_item_id,
4127                      l_pf_item_id,
4128                      p_organization_id,
4129                      p_instance_id,
4130                      p_plan_id,
4131                      l_demand_class,
4132                      l_level_id,
4133                      l_infinite_time_fence_date,
4134                      p_get_mat_in_rec.sys_next_osc_date,			--bug3099066 bug3333114
4135                      p_atf_date,
4136                      l_current_atp.atp_period,
4137                      l_current_atp.atp_qty,
4138                      l_unallocated_atp.atp_qty,
4139                      l_return_status
4140                   );
4141                   IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4142                         IF PG_DEBUG in ('Y', 'C') THEN
4143                                 msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error occured in procedure Item_Alloc_Avail_Pf_Unalloc');
4144                         END IF;
4145                         RAISE FND_API.G_EXC_ERROR;
4146                   END IF;
4147           END IF;
4148 
4149         IF PG_DEBUG in ('Y', 'C') THEN
4150            msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Before Atp_Consume for l_unallocated_atp');
4151 	   mm := l_unallocated_atp.atp_qty.FIRST;
4152 	   while mm is not null loop
4153 		msc_sch_wb.atp_debug('l_unallocated_atp.atp_period:atp_qty = ' ||
4154 		            l_current_atp.atp_period(mm) || ':' || l_unallocated_atp.atp_qty(mm));
4155 		mm := l_unallocated_atp.atp_qty.NEXT(mm);
4156 	   end loop;
4157         END IF;
4158 
4159         -- after calculating the net unallocated availability do b/w+f/w consumption and accumulation
4160         -- time_phased_atp
4161         IF l_time_phased_atp = 'Y' THEN
4162             MSC_ATP_PF.pf_atp_consume(
4163                    l_unallocated_atp.atp_qty,
4164                    l_return_status,
4165                    l_current_atp.atp_period,
4166                    MSC_ATP_PF.Bw_Fw_Cum, --b/w, f/w consumption and accumulation
4167                    p_atf_date);
4168             IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4169                 IF PG_DEBUG in ('Y', 'C') THEN
4170                         msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error occured in procedure Pf_Atp_Consume');
4171                 END IF;
4172                 RAISE FND_API.G_EXC_ERROR;
4173             END IF;
4174         ELSE
4175             MSC_ATP_PROC.Atp_Consume(l_unallocated_atp.atp_qty, l_unallocated_atp.atp_qty.COUNT);
4176         END IF;
4177 
4178         IF PG_DEBUG in ('Y', 'C') THEN
4179            msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'After Atp_Consume for l_unallocated_atp');
4180 	   mm := l_unallocated_atp.atp_qty.FIRST;
4181 	   while mm is not null loop
4182 		msc_sch_wb.atp_debug('l_unallocated_atp.atp_period:atp_qty = ' ||
4183 		            l_current_atp.atp_period(mm) || ':' || l_unallocated_atp.atp_qty(mm));
4184 		mm := l_unallocated_atp.atp_qty.NEXT(mm);
4185 	   end loop;
4186         END IF;
4187 
4188        ELSE
4189           IF PG_DEBUG in ('Y', 'C') THEN
4190              msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' ||
4191                 'Inside ELSE of IF i=1 AND G_ATP_FW_CONSUME_METHOD = 2 THEN');
4192           END IF;
4193           -- time_phased_atp
4194           IF l_time_phased_atp='N' THEN
4195                   item_alloc_avail(
4196                      p_get_mat_in_rec.optimized_plan,
4197                      l_inv_item_id,
4198                      p_organization_id,
4199                      p_instance_id,
4200                      p_plan_id,
4201                      l_demand_class,
4202                      l_level_id,
4203                      l_infinite_time_fence_date,
4204                      l_calendar_code,
4205                      l_calendar_exception_set_id,
4206                      p_get_mat_in_rec.sys_next_osc_date,			--bug3099066 bug3333114
4207                      l_current_atp.atp_period,
4208                      l_current_atp.atp_qty
4209                   );
4210           ELSE
4211                   MSC_ATP_PF.Item_Alloc_Avail_Pf(
4212                      l_inv_item_id,
4213                      l_pf_item_id,
4214                      p_organization_id,
4215                      p_instance_id,
4216                      p_plan_id,
4217                      l_demand_class,
4218                      l_level_id,
4219                      l_infinite_time_fence_date,
4220                      p_get_mat_in_rec.sys_next_osc_date,			--bug3099066 bug3333114
4221                      p_atf_date,
4222                      l_current_atp.atp_period,
4223                      l_current_atp.atp_qty,
4224                      l_return_status
4225                   );
4226                   IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4227                         IF PG_DEBUG in ('Y', 'C') THEN
4228                                 msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error occured in procedure Item_Alloc_Avail_Pf');
4229                         END IF;
4230                         RAISE FND_API.G_EXC_ERROR;
4231                   END IF;
4232           END IF;
4233        END IF;
4234 
4235        IF PG_DEBUG in ('Y', 'C') THEN
4236            msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: After calculating daily net availability');
4237            Print_Period_Qty('l_current_atp.atp_period:atp_qty = ', l_current_atp);
4238        END IF;
4239 
4240     ELSE
4241         -- IF (NVL(p_insert_flag, 0) <> 0  AND l_demand_class = p_demand_class)
4242         -- OR p_scenario_id = -1
4243         IF PG_DEBUG in ('Y', 'C') THEN
4244            msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' ||
4245                  'Inside ELSE of IF (NVL(p_insert_flag, 0)=0 OR l_demand_class<>p_demand_class)');
4246         END IF;
4247         MSC_ATP_DB_UTILS.Clear_SD_Details_Temp();
4248 
4249         -- time_phased_atp
4250         IF l_time_phased_atp='N' THEN
4251                 item_alloc_avail_dtls(
4252                    p_get_mat_in_rec.optimized_plan,
4253                    l_inv_item_id,
4254                    p_organization_id,
4255                    p_instance_id,
4256                    p_plan_id,
4257                    l_demand_class,
4258                    l_level_id,
4259                    l_infinite_time_fence_date,
4260                    l_calendar_code,
4261                    l_calendar_exception_set_id,
4262                    p_inventory_item_id,
4263                    p_level,
4264                    p_identifier,
4265                    p_scenario_id,
4266                    l_uom_code,
4267                    p_get_mat_in_rec.sys_next_osc_date			--bug3099066 bug3333114
4268                 );
4269         ELSE
4270                 MSC_ATP_PF.Item_Alloc_Avail_Pf_Dtls(
4271                    l_inv_item_id,
4272                    l_pf_item_id,
4273                    p_request_item_id,
4274                    p_inventory_item_id,
4275                    p_organization_id,
4276                    p_instance_id,
4277                    p_plan_id,
4278                    l_demand_class,
4279                    l_level_id,
4280                    l_infinite_time_fence_date,
4281                    p_level,
4282                    p_identifier,
4283                    p_scenario_id,
4284                    l_uom_code,
4285                    p_get_mat_in_rec.sys_next_osc_date,			--bug3099066 bug3333114
4286                    p_atf_date,
4287                    l_return_status
4288                 );
4289                 IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4290                         IF PG_DEBUG in ('Y', 'C') THEN
4291                                 msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error occured in procedure Item_Alloc_Avail_Pf_Dtls');
4292                         END IF;
4293                         RAISE FND_API.G_EXC_ERROR;
4294                 END IF;
4295         END IF;
4296 
4297      -- for period ATP
4298      IF PG_DEBUG in ('Y', 'C') THEN
4299         msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'after insert into msc_atp_sd_details_temp');
4300         msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'num rows : ' || SQL%ROWCOUNT);
4301      END IF;
4302 
4303      /*AATP Forward Consumption rajjain begin
4304      If request comes at topmost demand class level get unallocated data
4305      along with period data from sd temp table*/
4306      IF i=1 AND G_ATP_FW_CONSUME_METHOD = 2 THEN
4307 
4308         -- time_phased_atp
4309         IF l_time_phased_atp='Y' THEN
4310            MSC_ATP_PF.Get_Unalloc_Data_From_Sd_Temp(x_atp_period, l_unallocated_atp, l_return_status);
4311         ELSE
4312            MSC_AATP_PROC.get_unalloc_data_from_SD_temp(x_atp_period, l_unallocated_atp, l_return_status);
4313         END IF;
4314 
4315         IF  l_return_status = FND_API.G_RET_STS_ERROR THEN
4316            IF PG_DEBUG in ('Y', 'C') THEN
4317               msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error in call to get_unalloc_data_from_SD_temp');
4318            END IF;
4319         END IF;
4320 
4321         IF PG_DEBUG in ('Y', 'C') THEN
4322            msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'After get_unalloc_data_from_SD_temp');
4323            msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_unallocated_atp.atp_qty.COUNT : '||l_unallocated_atp.atp_qty.COUNT);
4324            msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Before Atp_Consume for l_unallocated_atp');
4325 	   mm := l_unallocated_atp.atp_qty.FIRST;
4326 	   while mm is not null loop
4327 		msc_sch_wb.atp_debug('l_unallocated_atp.atp_period:atp_qty = ' ||
4328 		            x_atp_period.Period_Start_Date(mm) || ':' || l_unallocated_atp.atp_qty(mm));
4329 		mm := l_unallocated_atp.atp_qty.NEXT(mm);
4330 	   end loop;
4331         END IF;
4332 
4333         -- after calculating the net unallocated availability do b/w+f/w consumption and accumulation
4334         -- time_phased_atp
4335         IF l_time_phased_atp = 'Y' THEN
4336             MSC_ATP_PF.pf_atp_consume(
4337                    l_unallocated_atp.atp_qty,
4338                    l_return_status,
4339                    x_atp_period.Period_Start_Date,
4340                    MSC_ATP_PF.Bw_Fw_Cum, --b/w, f/w consumption and accumulation
4341                    p_atf_date);
4342             IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4343                 IF PG_DEBUG in ('Y', 'C') THEN
4344                         msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error occured in procedure Pf_Atp_Consume');
4345                 END IF;
4346                 RAISE FND_API.G_EXC_ERROR;
4347             END IF;
4348         ELSE
4349             MSC_ATP_PROC.Atp_Consume(l_unallocated_atp.atp_qty, l_unallocated_atp.atp_qty.COUNT);
4350         END IF;
4351 
4352         IF PG_DEBUG in ('Y', 'C') THEN
4353            msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'After Atp_Consume for l_unallocated_atp');
4354 	   mm := l_unallocated_atp.atp_qty.FIRST;
4355 	   while mm is not null loop
4356 		msc_sch_wb.atp_debug('l_unallocated_atp.atp_period:atp_qty = ' ||
4357 		            x_atp_period.Period_Start_Date(mm) || ':' || l_unallocated_atp.atp_qty(mm));
4358 		mm := l_unallocated_atp.atp_qty.NEXT(mm);
4359 	   end loop;
4360         END IF;
4361 
4362      ELSE
4363         -- time_phased_atp
4364         IF l_time_phased_atp='Y' THEN
4365            MSC_ATP_PF.Get_Period_Data_From_Sd_Temp(x_atp_period, l_return_status);
4366            IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4367                 IF PG_DEBUG in ('Y', 'C') THEN
4368                         msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error occured in procedure Get_Period_Data_From_Sd_Temp');
4369                 END IF;
4370                 RAISE FND_API.G_EXC_ERROR;
4371            END IF;
4372         ELSE
4373            /* time_phased_atp
4374               call new procedure to fix the issue of not displaying correct quantities in ATP SD Window when
4375               user opens ATP SD window from ATP pegging in allocated scenarios*/
4376            MSC_ATP_PROC.Get_Alloc_Data_From_Sd_Temp(x_atp_period, l_return_status);
4377            IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4378                 IF PG_DEBUG in ('Y', 'C') THEN
4379                         msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error occured in procedure get_alloc_data_from_SD_temp');
4380                 END IF;
4381                 RAISE FND_API.G_EXC_ERROR;
4382            END IF;
4383         END IF;
4384 
4385         IF PG_DEBUG in ('Y', 'C') THEN
4386            msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Need not get unalloc data from SD temp');
4387         END IF;
4388      END IF;
4389 
4390      -- old netting code here
4391 
4392       l_current_atp.atp_period := x_atp_period.Period_Start_Date;
4393       l_current_atp.atp_qty := x_atp_period.Period_Quantity;
4394       -- bug 1657855, remove support for min alloc
4395       --l_current_atp.limit_qty := l_current_atp.atp_qty; -- 02/16
4396 
4397     END IF;
4398 
4399     IF PG_DEBUG in ('Y', 'C') THEN
4400        msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'right after the big query');
4401        Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
4402 		l_current_atp);
4403     END IF;
4404 
4405     /* rajjain 01/29/2003 Bug 2737596
4406        We only need to do b/w consumption, f/w consumption and accumulation in case the call is
4407        from MSC_AATP_PVT.VIEW_ALLOCATION for view total*/
4408     IF p_identifier <> -1 or (p_identifier = -1 and p_scenario_id <> -1) THEN
4409 
4410       -- do backward consumption for DCi
4411 
4412       -- time_phased_atp
4413       IF l_time_phased_atp = 'Y' THEN
4414           MSC_ATP_PF.pf_atp_consume(
4415                  l_current_atp.atp_qty,
4416                  l_return_status,
4417                  l_current_atp.atp_period,
4418                  MSC_ATP_PF.Backward, --b/w consumption
4419                  p_atf_date);
4420           IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4421                 IF PG_DEBUG in ('Y', 'C') THEN
4422                         msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error occured in procedure Pf_Atp_Consume');
4423                 END IF;
4424                 RAISE FND_API.G_EXC_ERROR;
4425           END IF;
4426       ELSE
4427             MSC_ATP_PROC.Atp_Backward_Consume(l_current_atp.atp_qty);
4428       END IF;
4429 
4430       IF PG_DEBUG in ('Y', 'C') THEN
4431          msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'right after the backward consume');
4432          Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
4433    	        l_current_atp);
4434       END IF;
4435 
4436 
4437 
4438       -- we have 3 records of tables.
4439       -- l_current_atp: stores the date and quantity for this demand class,
4440       --                and since we need to do backward consumption on this.
4441       -- l_current_steal_atp: stores the date and quantity from higher priority
4442       --                      demand class, this need to consume l_current_atp
4443       -- l_next_steal_atp : stores  the date and quantity for next priority
4444       --                    demand class to cunsume.  we need this because we may
4445       --                    have multiple demand classes at same priority .
4446       -- for example, we have DC1 in priority 1, DC21, DC22 in priority 2,
4447       -- DC3 in priority  3.
4448       -- now DC21 need to take care DC1, DC22 need to take care DC1 but not DC21,
4449       -- DC3 need to take care DC1, DC21, and DC22.  so if we are in the loop for
4450       -- DC22, than l_current_atp is the atp info for DC22,
4451       -- l_current_steal_atp is the atp info for DC1(which does not include DC21),
4452       -- and l_next_steal_atp is the stealing data that we need to take care
4453       -- for DC1, DC21 and DC22  when later on we move to the loop for DC3.
4454 
4455       -- do backward consumption if DC1 to DC(i-1) has any negative bucket,and
4456       -- the priority  is higher than DCi
4457       -- the l_current_atp is an in/out parameter
4458 
4459       -- for 1680719, since in hierarchy demand class we cannot
4460       -- judge the priority by just looking at the priority (we need
4461       -- the information from the parent, so the condition needs to be changed.
4462 
4463       IF l_level_id IN (-1, 1) THEN
4464         -- here is the old logic which should still be ok for level id 1 and -1
4465        IF (i > 1) THEN
4466 
4467         IF (l_demand_class_priority_tab(i) >
4468             l_demand_class_priority_tab (i-1)) THEN
4469         -- we don't need to change the l_current_steal_atp if we don't
4470         -- move to next priority.
4471         -- but we do need to change the l_current_steal_atp
4472         -- if we are in different priority  now.
4473 
4474           l_current_steal_atp := l_next_steal_atp;
4475 
4476           -- Added for bug 1409335. Need to initialize l_next_steal_atp
4477           -- otherwise quanities would be getting accumulated
4478           -- repeatedly.
4479           l_next_steal_atp := l_null_steal_atp;
4480         END IF;
4481        END IF;
4482       ELSE -- IF l_level_id IN (-1, 1) THEN
4483 
4484        IF (i > 1) THEN
4485 
4486         IF (l_class_tab(i) <> l_class_tab(i-1)) THEN
4487 
4488           -- class changed.  If priority of both classes are not the same,
4489           -- then we need to change the curr_steal_atp  at class level.
4490 
4491           IF PG_DEBUG in ('Y', 'C') THEN
4492              msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'class changed');
4493           END IF;
4494 
4495           IF trunc(l_demand_class_priority_tab(i), -3) >
4496              trunc(l_demand_class_priority_tab (i-1), -3) THEN
4497 
4498             IF PG_DEBUG in ('Y', 'C') THEN
4499                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'class priority changed');
4500             END IF;
4501             l_class_curr_steal_atp := l_class_next_steal_atp;
4502             l_class_next_steal_atp := l_null_steal_atp;
4503           END IF;
4504 
4505           l_partner_next_steal_atp := l_null_steal_atp;
4506           l_partner_curr_steal_atp := l_null_steal_atp;
4507           l_partner_next_steal_atp := l_null_steal_atp;
4508           l_current_steal_atp := l_null_steal_atp;
4509           l_next_steal_atp := l_null_steal_atp;
4510 
4511         ELSE
4512           IF PG_DEBUG in ('Y', 'C') THEN
4513              msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'same class');
4514           END IF;
4515           IF (l_partner_tab(i) <> l_partner_tab(i-1)) THEN
4516             -- customer changed.  If priority of both customers are not the
4517             -- same, we need to change the curr_steal_atp  at partner level.
4518 
4519             IF PG_DEBUG in ('Y', 'C') THEN
4520                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'customer changed');
4521             END IF;
4522 
4523             IF trunc(l_demand_class_priority_tab(i), -2) >
4524                trunc(l_demand_class_priority_tab (i-1), -2) THEN
4525               IF PG_DEBUG in ('Y', 'C') THEN
4526                  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'customer priority changed');
4527               END IF;
4528 
4529               l_partner_curr_steal_atp := l_partner_next_steal_atp;
4530               l_partner_next_steal_atp := l_null_steal_atp;
4531             END IF;
4532 
4533             l_current_steal_atp := l_null_steal_atp;
4534             l_next_steal_atp := l_null_steal_atp;
4535 
4536           ELSE
4537             -- same customer
4538             IF PG_DEBUG in ('Y', 'C') THEN
4539                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'same customer ');
4540             END IF;
4541 
4542             IF (l_demand_class_priority_tab(i) >
4543                 l_demand_class_priority_tab (i-1)) THEN
4544               -- site level priority changed
4545 
4546               IF PG_DEBUG in ('Y', 'C') THEN
4547                  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'site priority changed');
4548               END IF;
4549               l_current_steal_atp := l_next_steal_atp;
4550               l_next_steal_atp := l_null_steal_atp;
4551 
4552             END IF;
4553           END IF; -- IF (l_partner_tab(i) <> l_partner_tab(i-1))
4554         END IF; -- IF (l_class_tab(i) <> l_class_tab(i-1))
4555 
4556        END IF; -- IF (i > 1)
4557 
4558       END IF; -- IF l_level_id IN (-1, 1)
4559       IF PG_DEBUG in ('Y', 'C') THEN
4560          msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'before we decide we need to do dc consumption');
4561       END IF;
4562       IF (i > 1) THEN
4563        IF (  -- this is the huge condition
4564              ((l_level_id IN (-1, 1)) AND
4565              (l_demand_class_priority_tab(i) <> l_demand_class_priority_tab(1)))
4566            OR
4567              (l_level_id in (2, 3))
4568           ) THEN
4569 
4570         -- we need to do demand class consume only if we are not in the first
4571         -- preferred priority
4572 
4573         -- bug 1413459
4574         -- we need to remember what's the atp picture before the
4575         -- demand class consumption but after it's own backward
4576         -- consumption.  so that we can figure out the stealing
4577         -- quantity correctly.
4578         IF (NVL(p_insert_flag, 0) <>0)
4579            AND (l_demand_class_tab(i) = p_demand_class) THEN
4580             l_temp_atp := l_current_atp;
4581         END IF;
4582 
4583         IF PG_DEBUG in ('Y', 'C') THEN
4584            msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'p_demand_class : '||p_demand_class);
4585            msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_demand_class_tab(i) : '||l_demand_class_tab(i));
4586         END IF;
4587 
4588         -- 1680719
4589         -- since we have hierarchy now, before we do demand class
4590         -- consumption for site level, we need to do the class level and
4591         -- partner level first
4592 
4593         IF l_level_id IN (2,3) THEN
4594 
4595           IF l_class_tab(i) <> l_class_tab(1) THEN
4596 
4597             IF PG_DEBUG in ('Y', 'C') THEN
4598                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'before consume l_class_curr_steal_atp');
4599                Print_Period_Qty('l_class_curr_steal_atp.atp_period:atp_qty = ',
4600 			l_class_curr_steal_atp);
4601             END IF;
4602 
4603             /* time_phased_atp
4604                pass p_atf_date to make sure we do not do demand class consumption across ATF*/
4605             MSC_AATP_PVT.Atp_Demand_Class_Consume(l_current_atp, l_class_curr_steal_atp, p_atf_date);
4606 
4607             IF PG_DEBUG in ('Y', 'C') THEN
4608                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'After consume l_class_curr_steal_atp');
4609                Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
4610 			l_current_atp);
4611                Print_Period_Qty('l_class_curr_steal_atp.atp_period:atp_qty = ',
4612 			l_class_curr_steal_atp);
4613 
4614             END IF;
4615 
4616           END IF; --IF l_class_tab(i) <> l_class_tab(1) THEN
4617 
4618           -- bug 1922942: although partner_id should be unique, we introduced
4619           -- -1 for 'Other' which make the partner_id not unique.
4620           -- for example, Class1/Other and Class2/Other will have same
4621           -- partner_id -1. so the if condition needs to be modified.
4622 
4623           -- IF l_partner_tab(i) <> l_partner_tab(1) THEN
4624 
4625           IF (l_class_tab(i) <> l_class_tab(1)) OR
4626               (l_partner_tab(i) <> l_partner_tab(1)) THEN
4627             IF PG_DEBUG in ('Y', 'C') THEN
4628                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'before consume l_partner_curr_steal_atp');
4629             END IF;
4630             /* time_phased_atp
4631                pass p_atf_date to make sure we do not do demand class consumption across ATF*/
4632             MSC_AATP_PVT.Atp_Demand_Class_Consume(l_current_atp, l_partner_curr_steal_atp, p_atf_date);
4633 
4634             IF PG_DEBUG in ('Y', 'C') THEN
4635                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'After consume l_partner_curr_steal_atp');
4636                Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
4637 			l_current_atp);
4638                Print_Period_Qty('l_partner_curr_steal_atp.atp_period:atp_qty = ',
4639 			l_partner_curr_steal_atp);
4640             END IF;
4641 
4642           END IF; -- IF l_partner_tab(i) <> l_partner_tab(1) THEN
4643 
4644         END IF; -- IF l_level_id IN (2,3)
4645 
4646         IF PG_DEBUG in ('Y', 'C') THEN
4647            msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Before consume current_steal_atp');
4648            Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
4649 		l_current_atp);
4650         END IF;
4651 
4652         /* time_phased_atp
4653            pass p_atf_date to make sure we do not do demand class consumption across ATF*/
4654         MSC_AATP_PVT.Atp_Demand_Class_Consume(l_current_atp, l_current_steal_atp, p_atf_date);
4655 
4656         IF PG_DEBUG in ('Y', 'C') THEN
4657            msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'After consume l_current_steal_atp');
4658 
4659            Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
4660 		l_current_atp);
4661            Print_Period_Qty('l_current_steal_atp.atp_period:atp_qty = ',
4662 		l_current_steal_atp);
4663         END IF;
4664 
4665         -- this part is not in the original design.
4666         -- original design is that we will ignore the inconsistancy
4667         -- in the s/d and period atp for display when stealing happens, as long
4668         -- as we take care the stealing in the logic.
4669         -- but i think it is still better to put it in.
4670         -- and actually if we change Atp_Demand_Class_Consume we can
4671         -- deal with this together.  but for now...
4672 
4673         -- we need to know if we need to store the stealing
4674         -- results in to x_atp_supply_demand and x_atp_period or not.
4675         -- we only do it if this is the demand class we request and
4676         -- insert_flag is on
4677 
4678         IF (NVL(p_insert_flag, 0) <>0) AND (l_demand_class_tab(i) = p_demand_class) THEN
4679 
4680           /*rajjain begin 12/10/2002
4681           We now do following instead of making call to MSC_SATP_FUNC.Extend_Atp_Supply_Demand
4682           number of times condition l_current_atp.atp_qty(j) < l_temp_atp.atp_qty(j) is true
4683           inside FOR LOOP
4684           1. Before FOR loop we extend l_temp_atp_supply_demand by l_current_atp.atp_qty.COUNT
4685           2. After FOR loop we trim the remaining.*/
4686 
4687           -- initialize k
4688           k := l_temp_atp_supply_demand.Level.Count;
4689           MSC_SATP_FUNC.Extend_Atp_Supply_Demand(l_temp_atp_supply_demand, l_return_status,
4690                         l_current_atp.atp_qty.COUNT);
4691 
4692           IF PG_DEBUG in ('Y', 'C') THEN
4693             msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'k before FOR LOOP = ' || k);
4694           END IF;
4695 
4696           FOR j in 1..l_current_atp.atp_qty.COUNT LOOP
4697 
4698             IF l_current_atp.atp_qty(j) < l_temp_atp.atp_qty(j) THEN
4699               -- this is the stealing quantity in that period
4700 
4701               IF PG_DEBUG in ('Y', 'C') THEN
4702                  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' ||
4703                             'l_current_atp.atp_qty(j)='||l_current_atp.atp_qty(j));
4704                  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' ||
4705                             'l_temp_atp.atp_qty(j)='||l_temp_atp.atp_qty(j));
4706                  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' ||
4707                             'l_steal_period_quantity='||l_steal_period_quantity);
4708                  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' ||
4709                             'x_atp_period.period_start_date(j)='||x_atp_period.period_start_date(j));
4710               END IF;
4711 
4712               -- bug 1413459: the stealing quantity should be the current
4713               -- period quantity (after backward consumption, after stealing)
4714               -- minus the period quantity after backward consumption but
4715               -- before the stealing
4716               l_steal_period_quantity := l_current_atp.atp_qty(j) -
4717                                          l_temp_atp.atp_qty(j);
4718 
4719               /*rajjain 12/10/2002 This procedure call is not needed as we have already extended
4720               l_temp_atp_supply_demand outside this FOR loop
4721               MSC_SATP_FUNC.Extend_Atp_Supply_Demand(l_temp_atp_supply_demand, l_return_status);
4722 
4723               k := l_temp_atp_supply_demand.Level.Count;*/
4724 
4725               k := k+1; -- rajjain increment k
4726               l_temp_atp_supply_demand.level(k) := p_level;
4727               l_temp_atp_supply_demand.identifier(k) := p_identifier;
4728               l_temp_atp_supply_demand.scenario_id(k) := p_scenario_id;
4729               l_temp_atp_supply_demand.inventory_item_id(k) := p_inventory_item_id;
4730               l_temp_atp_supply_demand.uom(k):= l_uom_code;
4731               l_temp_atp_supply_demand.supply_demand_type(k) := 1;
4732 
4733               -- Bug 1408132 and 1416290, Need to insert type as
4734               -- Demand Class Consumption (45).
4735 
4736               l_temp_atp_supply_demand.identifier1(k) := p_instance_id;
4737               l_temp_atp_supply_demand.supply_demand_date (k) := l_current_atp.atp_period(j);
4738               l_temp_atp_supply_demand.supply_demand_quantity(k) := l_steal_period_quantity;
4739 
4740               -- time_phased_atp change begin
4741               l_temp_atp_supply_demand.organization_id(k) := p_organization_id;
4742               IF l_time_phased_atp='Y' THEN
4743                  l_temp_atp_supply_demand.request_item_id(k) := p_request_item_id;
4744                  l_temp_atp_supply_demand.supply_demand_source_type(k) := 51;
4745                  l_temp_atp_supply_demand.Original_Supply_Demand_Type(k) := 45;
4746                  l_temp_atp_supply_demand.Original_Item_Id(k) := p_request_item_id;
4747                  l_temp_atp_supply_demand.Original_Demand_Date(k) := l_current_atp.atp_period(j);
4748                  l_temp_atp_supply_demand.Original_Demand_Quantity(k) := l_steal_period_quantity;
4749                  l_temp_atp_supply_demand.Allocated_Quantity(k) := l_steal_period_quantity;
4750                  l_temp_atp_supply_demand.Pf_Display_Flag(k) := 1;
4751                  x_atp_period.Total_Bucketed_Demand_Quantity(j):=
4752                         x_atp_period.Total_Bucketed_Demand_Quantity(j) + l_steal_period_quantity;
4753               ELSE
4754                  l_temp_atp_supply_demand.request_item_id(k) := p_inventory_item_id;
4755                  l_temp_atp_supply_demand.supply_demand_source_type(k) := 45;
4756               END IF;
4757               -- time_phased_atp change end
4758 
4759               x_atp_period.Total_Demand_Quantity(j):=
4760                      x_atp_period.Total_Demand_Quantity(j) + l_steal_period_quantity;
4761 
4762               x_atp_period.period_quantity(j):=x_atp_period.period_quantity(j)
4763                      + l_steal_period_quantity;
4764 
4765             END IF;
4766           END LOOP;
4767 
4768           IF PG_DEBUG in ('Y', 'C') THEN
4769             msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'k after FOR LOOP = ' || k);
4770           END IF;
4771 
4772           --rajjain trim the remaining 12/10/2002
4773           MSC_SATP_FUNC.Trim_Atp_Supply_Demand(l_temp_atp_supply_demand, l_return_status,
4774                         (l_temp_atp_supply_demand.Level.Count - k));
4775 
4776 	  -- dsting dump the data into msc_atp_sd_details_temp
4777 	  -- and null out the record
4778 	  move_SD_plsql_into_SD_temp(l_temp_atp_supply_demand);
4779 
4780         END IF;  -- IF (NVL(p_insert_flag, 0) <>0) .....
4781        END IF; -- the huge condition
4782       END IF; -- IF (i > 1)
4783 
4784       -- AATP Forward Consumption rajjain begin
4785       IF l_level_id IN (-1, 1) THEN
4786         IF PG_DEBUG in ('Y', 'C') THEN
4787            msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Inside IF l_level_id IN (-1, 1) THEN');
4788         END IF;
4789 
4790         /*1. If level_id is in (-1, 1) and forward consumption method is method1
4791               for the lowest priority demand class -> add negatives from l_current_steal to
4792               l_current_atp and do forward consumption
4793           2. If level_id is in (-1, 1) and forward consumption method is method2
4794               for the lowest priority demand class -> call Atp_Remove_Negatives to remove
4795               all the negatives from l_current_atp*/
4796 	IF l_demand_class = l_lowest_priority_demand_class THEN
4797           IF PG_DEBUG in ('Y', 'C') THEN
4798                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'This is l_lowest_priority_demand_class');
4799           END IF;
4800           IF G_ATP_FW_CONSUME_METHOD = 1 THEN
4801             IF PG_DEBUG in ('Y', 'C') THEN
4802                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Method 1');
4803                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'level = '||l_level_id);
4804                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'before Add_to_Current_Atp');
4805                Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
4806 			l_current_atp);
4807                Print_Period_Qty('l_current_steal_atp.atp_period:atp_qty = ',
4808 			l_current_steal_atp);
4809             END IF;
4810 
4811             MSC_AATP_PROC.Add_to_Current_Atp(l_current_steal_atp, l_current_atp, l_return_status);
4812             IF  l_return_status = FND_API.G_RET_STS_ERROR THEN
4813                IF PG_DEBUG in ('Y', 'C') THEN
4814                   msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error in call to Add_to_Current_Atp');
4815                END IF;
4816             END IF;
4817             IF PG_DEBUG in ('Y', 'C') THEN
4818                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'after Add_to_Current_Atp');
4819                Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
4820 			l_current_atp);
4821             END IF;
4822 
4823             MSC_AATP_PROC.Atp_Forward_Consume(l_current_atp.atp_period, p_atf_date, l_current_atp.atp_qty, l_return_status);
4824             IF  l_return_status = FND_API.G_RET_STS_ERROR THEN
4825                IF PG_DEBUG in ('Y', 'C') THEN
4826                   msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error in call to Atp_Forward_Consume');
4827                END IF;
4828             END IF;
4829 
4830             IF PG_DEBUG in ('Y', 'C') THEN
4831                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'after Atp_Forward_Consume for last demand class');
4832                Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
4833 			l_current_atp);
4834             END IF;
4835 
4836             /*Call Atp_Remove_Negatives to remove negatives in case we have negatives left
4837             after forward consumption. we will not show these negatives in atp_inquiry for
4838             Allocated ATP with User Defined %.*/
4839 
4840             MSC_AATP_PROC.Atp_Remove_Negatives(l_current_atp.atp_qty, l_return_status);
4841             IF  l_return_status = FND_API.G_RET_STS_ERROR THEN
4842                IF PG_DEBUG in ('Y', 'C') THEN
4843                   msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error in call to Atp_Remove_Negatives');
4844                END IF;
4845             END IF;
4846 
4847             IF PG_DEBUG in ('Y', 'C') THEN
4848                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'after Atp_Remove_Negatives');
4849                Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
4850 			l_current_atp);
4851             END IF;
4852 
4853           ELSE
4854             IF PG_DEBUG in ('Y', 'C') THEN
4855                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Method 2');
4856             END IF;
4857 	    MSC_AATP_PROC.Atp_remove_negatives(l_current_atp.atp_qty, l_return_status);
4858             IF  l_return_status = FND_API.G_RET_STS_ERROR THEN
4859                IF PG_DEBUG in ('Y', 'C') THEN
4860                   msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error in call to Atp_remove_negatives');
4861                END IF;
4862             END IF;
4863 
4864           END IF;
4865 
4866 	ELSE
4867           IF PG_DEBUG in ('Y', 'C') THEN
4868                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'This is not the l_lowest_priority_demand_class');
4869           END IF;
4870 	  IF l_demand_class_priority_tab(i) < l_lowest_priority OR G_ATP_FW_CONSUME_METHOD = 2 THEN
4871             -- this demand class is not the lowest priority demand class
4872             -- add negatives from l_current_atp to l_next_steal_atp
4873             IF PG_DEBUG in ('Y', 'C') THEN
4874                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: '||'l_demand_class_priority_tab(i)<l_lowest_priority'||
4875                           ' OR G_ATP_FW_CONSUME_METHOD = 2');
4876                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'before Add_to_Next_Steal_Atp');
4877                Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
4878 			l_current_atp);
4879                Print_Period_Qty('l_next_steal_atp.atp_period:atp_qty = ',
4880 			l_next_steal_atp);
4881             END IF;
4882 
4883             -- we need to prepare the l_next_steal_atp for next priority
4884             MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_current_atp, l_next_steal_atp);
4885             IF PG_DEBUG in ('Y', 'C') THEN
4886                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'after Add_to_Next_Steal_Atp');
4887                Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
4888 			l_current_atp);
4889                Print_Period_Qty('l_next_steal_atp.atp_period:atp_qty = ',
4890 			l_next_steal_atp);
4891             END IF;
4892 
4893             IF i < l_demand_class_priority_tab.LAST AND l_demand_class_priority_tab(i)<
4894                l_demand_class_priority_tab(i+1) THEN
4895                -- this is the last element of current priority, so we also need
4896                -- to add l_steal_atp into l_next_steal_atp if we can not finish
4897                -- the stealing at this priority
4898                IF PG_DEBUG in ('Y', 'C') THEN
4899                   msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'This is the last element of current priority');
4900                   msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'level = '||l_level_id);
4901 		  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'before Adding Add_to_Next_Steal_Atp');
4902 
4903                   Print_Period_Qty('l_next_steal_atp.atp_period:atp_qty = ',
4904 		       l_next_steal_atp);
4905                END IF;
4906 
4907                MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_current_steal_atp, l_next_steal_atp);
4908 
4909                IF PG_DEBUG in ('Y', 'C') THEN
4910                   msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'after Add_to_Next_Steal_Atp');
4911 
4912                   Print_Period_Qty('l_next_steal_atp.atp_period:atp_qty = ',
4913 		       l_next_steal_atp);
4914                END IF;
4915 
4916             END IF;
4917 
4918           ELSE
4919 	    /* this is the lowest priority demand class in case
4920 	    we have multiple demand classes at lowest priority
4921 	    do forward consumption for all the lowest priority demand classes for method 1*/
4922             IF PG_DEBUG in ('Y', 'C') THEN
4923                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'This demand class is of l_lowest_priority');
4924                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'level = '||l_level_id);
4925                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' ||
4926                           'before Forward Consumption for lowest priority demand classes');
4927                Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
4928 			l_current_atp);
4929             END IF;
4930 
4931 	    MSC_AATP_PROC.Atp_Forward_Consume(l_current_atp.atp_period, p_atf_date, l_current_atp.atp_qty, l_return_status);
4932             IF  l_return_status = FND_API.G_RET_STS_ERROR THEN
4933                IF PG_DEBUG in ('Y', 'C') THEN
4934                   msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error in call to Atp_Forward_Consume');
4935                END IF;
4936             END IF;
4937             IF PG_DEBUG in ('Y', 'C') THEN
4938                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' ||
4939                           'after Forward Consumption for lowest priority demand classes');
4940                Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
4941 			l_current_atp);
4942             END IF;
4943 
4944             /*Call Atp_Remove_Negatives to remove negatives in case we have negatives left
4945             after forward consumption. we will not show these negatives in atp_inquiry for
4946             Allocated ATP with User Defined %.*/
4947 
4948             MSC_AATP_PROC.Atp_Remove_Negatives(l_current_atp.atp_qty, l_return_status);
4949             IF  l_return_status = FND_API.G_RET_STS_ERROR THEN
4950                IF PG_DEBUG in ('Y', 'C') THEN
4951                   msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error in call to Atp_Remove_Negatives');
4952                END IF;
4953             END IF;
4954 
4955             IF PG_DEBUG in ('Y', 'C') THEN
4956                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'after Atp_Remove_Negatives');
4957                Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
4958 			l_current_atp);
4959             END IF;
4960 
4961 	  END IF;
4962 
4963 	END IF;
4964 
4965       ELSE --IF l_level_id IN (-1, 1) THEN
4966         IF PG_DEBUG in ('Y', 'C') THEN
4967            msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Inside ELSE of IF l_level_id IN (-1, 1) THEN');
4968         END IF;
4969 	IF l_demand_class = l_lowest_priority_demand_class THEN
4970           IF PG_DEBUG in ('Y', 'C') THEN
4971                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'This is l_lowest_priority_demand_class');
4972           END IF;
4973           IF G_ATP_FW_CONSUME_METHOD = 1 THEN
4974             IF PG_DEBUG in ('Y', 'C') THEN
4975                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Method 1');
4976             END IF;
4977 	    IF l_fw_consume_tab(i) = 4 THEN
4978               -- add all negatives to l_current_atp and do forward consumption
4979               IF PG_DEBUG in ('Y', 'C') THEN
4980                  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_fw_consume_tab(i) is 4');
4981               END IF;
4982               MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_current_steal_atp, l_next_steal_atp);
4983               MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_next_steal_atp, l_partner_next_steal_atp);
4984               MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_partner_curr_steal_atp,
4985                                     l_partner_next_steal_atp);
4986               MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_partner_next_steal_atp,
4987                                     l_class_next_steal_atp);
4988               MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_class_curr_steal_atp,
4989                                     l_class_next_steal_atp);
4990               IF PG_DEBUG in ('Y', 'C') THEN
4991                  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'before Add_to_Next_Steal_Atp');
4992 
4993                  Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
4994 		      l_current_atp);
4995                  Print_Period_Qty('l_class_next_steal_atp.atp_period:atp_qty = ',
4996 		      l_class_next_steal_atp);
4997               END IF;
4998               MSC_AATP_PROC.Add_to_Current_Atp(l_class_next_steal_atp, l_current_atp, l_return_status);
4999               IF  l_return_status = FND_API.G_RET_STS_ERROR THEN
5000                  IF PG_DEBUG in ('Y', 'C') THEN
5001                     msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error in call to Add_to_Current_Atp');
5002                  END IF;
5003               END IF;
5004 
5005               IF PG_DEBUG in ('Y', 'C') THEN
5006                  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'after Add_to_Current_Atp');
5007 
5008                  Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5009 		      l_current_atp);
5010                  Print_Period_Qty('l_class_next_steal_atp.atp_period:atp_qty = ',
5011 		      l_class_next_steal_atp);
5012               END IF;
5013               MSC_AATP_PROC.Atp_Forward_Consume(l_current_atp.atp_period, p_atf_date, l_current_atp.atp_qty, l_return_status);
5014               IF  l_return_status = FND_API.G_RET_STS_ERROR THEN
5015                  IF PG_DEBUG in ('Y', 'C') THEN
5016                     msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error in call to Atp_Forward_Consume');
5017                  END IF;
5018               END IF;
5019               IF PG_DEBUG in ('Y', 'C') THEN
5020                  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'after Atp_forward_consume');
5021 
5022                  Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5023 		      l_current_atp);
5024               END IF;
5025 
5026               /*Call Atp_Remove_Negatives to remove negatives in case we have negatives left
5027               after forward consumption. we will not show these negatives in atp_inquiry for
5028               Allocated ATP with User Defined %.*/
5029 
5030               MSC_AATP_PROC.Atp_Remove_Negatives(l_current_atp.atp_qty, l_return_status);
5031               IF  l_return_status = FND_API.G_RET_STS_ERROR THEN
5032                  IF PG_DEBUG in ('Y', 'C') THEN
5033                     msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error in call to Atp_Remove_Negatives');
5034                  END IF;
5035               END IF;
5036 
5037               IF PG_DEBUG in ('Y', 'C') THEN
5038                  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'after Atp_Remove_Negatives');
5039                  Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5040 	  		  l_current_atp);
5041               END IF;
5042 	    END IF;
5043           ELSE
5044             -- method2, remove negatives from l_current_atp
5045             IF PG_DEBUG in ('Y', 'C') THEN
5046                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Method 2');
5047             END IF;
5048 	    MSC_AATP_PROC.Atp_remove_negatives(l_current_atp.atp_qty, l_return_status);
5049             IF  l_return_status = FND_API.G_RET_STS_ERROR THEN
5050                IF PG_DEBUG in ('Y', 'C') THEN
5051                   msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error in call to Atp_remove_negatives');
5052                END IF;
5053             END IF;
5054 
5055           END IF;
5056 	ELSE --IF l_demand_class = l_lowest_priority_demand_class THEN
5057           IF PG_DEBUG in ('Y', 'C') THEN
5058                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' ||
5059                           'This is not the l_lowest_priority_demand_class');
5060           END IF;
5061           IF G_ATP_FW_CONSUME_METHOD = 2 OR
5062              (G_ATP_FW_CONSUME_METHOD = 1 AND l_fw_consume_tab(i) = 0) THEN
5063               IF PG_DEBUG in ('Y', 'C') THEN
5064                  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Inside IF G_ATP_FW_CONSUME_METHOD = 2 OR '||
5065                           '(G_ATP_FW_CONSUME_METHOD = 1 AND l_fw_consume_tab(i) = 0)');
5066 		 msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'before Adding Add_to_Next_Steal_Atp');
5067 
5068                  Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5069 		      l_current_atp);
5070                  Print_Period_Qty('l_next_steal_atp.atp_period:atp_qty = ',
5071 		      l_next_steal_atp);
5072               END IF;
5073 
5074               MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_current_atp, l_next_steal_atp);
5075 
5076               IF PG_DEBUG in ('Y', 'C') THEN
5077                  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'after Add_to_Next_Steal_Atp');
5078 
5079                  Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5080 		      l_current_atp);
5081                  Print_Period_Qty('l_next_steal_atp.atp_period:atp_qty = ',
5082 		      l_next_steal_atp);
5083               END IF;
5084 
5085 	  END IF;
5086 
5087           IF i = l_class_tab.LAST OR (l_class_tab(i) <> l_class_tab(i+1))
5088              OR (G_ATP_FW_CONSUME_METHOD = 1 AND l_fw_consume_tab(i) = 3) THEN
5089             -- either we are at last record in l_demand_class_tab or
5090             -- we are at last record at current customer class level in l_demand_class_tab or
5091             -- this is the lowest priority customer at customer class level
5092             IF PG_DEBUG in ('Y', 'C') THEN
5093                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' ||
5094                    'We are at lowest priority customer at current customer class level ');
5095             END IF;
5096 
5097             MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_current_steal_atp, l_next_steal_atp);
5098             MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_next_steal_atp, l_partner_next_steal_atp);
5099             MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_partner_curr_steal_atp,
5100                                   l_partner_next_steal_atp);
5101             IF G_ATP_FW_CONSUME_METHOD <> 1 OR l_fw_consume_tab(i) <> 3 THEN
5102                MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_partner_next_steal_atp,
5103                                      l_class_next_steal_atp);
5104             END IF;
5105             IF PG_DEBUG in ('Y', 'C') THEN
5106 		 msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'after Adding Add_to_Next_Steal_Atp');
5107 
5108                  Print_Period_Qty('l_class_next_steal_atp.atp_period:atp_qty = ',
5109 		      l_class_next_steal_atp);
5110                  Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5111 		      l_current_atp);
5112             END IF;
5113 
5114             IF i = l_class_tab.LAST OR trunc(l_demand_class_priority_tab(i), -3)<
5115                trunc(l_demand_class_priority_tab (i+1), -3) THEN
5116 
5117               -- next customer class is at higher priority then this cc
5118               -- add negatives from class_curr_steal to class_next_steal
5119               IF PG_DEBUG in ('Y', 'C') THEN
5120                   msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' ||
5121                              'next customer class is at higher priority then this cc');
5122               END IF;
5123               IF PG_DEBUG in ('Y', 'C') THEN
5124 		 msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'before Adding Add_to_Next_Steal_Atp');
5125 
5126                  Print_Period_Qty('l_class_next_steal_atp.atp_period:atp_qty = ',
5127 		      l_class_next_steal_atp);
5128                  Print_Period_Qty('l_class_curr_steal_atp.atp_period:atp_qty = ',
5129 		      l_class_curr_steal_atp);
5130               END IF;
5131               MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_class_curr_steal_atp,
5132                                     l_class_next_steal_atp);
5133               IF PG_DEBUG in ('Y', 'C') THEN
5134 		 msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'after Adding Add_to_Next_Steal_Atp');
5135 
5136                  Print_Period_Qty('l_class_next_steal_atp.atp_period:atp_qty = ',
5137 		      l_class_next_steal_atp);
5138                  Print_Period_Qty('l_class_curr_steal_atp.atp_period:atp_qty = ',
5139 		      l_class_curr_steal_atp);
5140               END IF;
5141 
5142             END IF;
5143 
5144           ELSE
5145             IF PG_DEBUG in ('Y', 'C') THEN
5146                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' ||
5147                'We are not at lowest priority customer at current customer class level ');
5148             END IF;
5149             IF (l_partner_tab(i) <> l_partner_tab(i+1))
5150                OR (G_ATP_FW_CONSUME_METHOD = 1 AND l_fw_consume_tab(i) = 2) THEN
5151               -- either we are at last record at current customer level in l_demand_class_tab or
5152               -- this is the lowest priority site at this customer level
5153               IF PG_DEBUG in ('Y', 'C') THEN
5154                   msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' ||
5155                   'We are at lowest priority customer site at current customer level  ');
5156               END IF;
5157               IF PG_DEBUG in ('Y', 'C') THEN
5158 		 msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'before Adding Add_to_Next_Steal_Atp');
5159 
5160                  Print_Period_Qty('l_current_steal_atp.atp_period:atp_qty = ',
5161 		      l_current_steal_atp);
5162                  Print_Period_Qty('l_partner_next_steal_atp.atp_period:atp_qty = ',
5163 		      l_partner_next_steal_atp);
5164               END IF;
5165               MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_current_steal_atp, l_next_steal_atp);
5166 
5167               IF G_ATP_FW_CONSUME_METHOD <> 1 OR l_fw_consume_tab(i) <> 2 THEN
5168                  -- if forward consumption type is 2 we wont add the negatives in l_next_steal_atp
5169                  -- to l_partner_steal_atp. we will add the negatives to l_current_atp and do forward
5170                  -- consumption instead.
5171                  MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_next_steal_atp, l_partner_next_steal_atp);
5172               END IF;
5173 
5174               IF PG_DEBUG in ('Y', 'C') THEN
5175 		 msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'after Adding Add_to_Next_Steal_Atp');
5176 
5177                  Print_Period_Qty('l_current_steal_atp.atp_period:atp_qty = ',
5178 		      l_current_steal_atp);
5179                  Print_Period_Qty('l_partner_next_steal_atp.atp_period:atp_qty = ',
5180 		      l_partner_next_steal_atp);
5181               END IF;
5182               IF trunc(l_demand_class_priority_tab(i), -2)<
5183                  trunc(l_demand_class_priority_tab (i+1), -2) THEN
5184                 IF PG_DEBUG in ('Y', 'C') THEN
5185                   msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' ||
5186                              'next customer is at higher priority then this customer');
5187 		  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'before Adding Add_to_Next_Steal_Atp');
5188 
5189                   Print_Period_Qty('l_partner_curr_steal_atp.atp_period:atp_qty = ',
5190 		       l_partner_curr_steal_atp);
5191                   Print_Period_Qty('l_partner_next_steal_atp.atp_period:atp_qty = ',
5192 		       l_partner_next_steal_atp);
5193                 END IF;
5194 
5195                 MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_partner_curr_steal_atp,
5196                                       l_partner_next_steal_atp);
5197 
5198                 IF PG_DEBUG in ('Y', 'C') THEN
5199 		  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'after Adding Add_to_Next_Steal_Atp');
5200 
5201                   Print_Period_Qty('l_partner_curr_steal_atp.atp_period:atp_qty = ',
5202 		       l_partner_curr_steal_atp);
5203                   Print_Period_Qty('l_partner_next_steal_atp.atp_period:atp_qty = ',
5204 		       l_partner_next_steal_atp);
5205                 END IF;
5206 
5207               END IF;
5208 
5209 
5210             ELSE
5211               -- we are not at last site under this customer
5212               IF PG_DEBUG in ('Y', 'C') THEN
5213                   msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' ||
5214                              'we are not at last site under this customer');
5215               END IF;
5216               IF (l_demand_class_priority_tab(i)<>
5217                   l_demand_class_priority_tab (i+1)) THEN
5218                 -- next site under this customer is not at same priority as this site
5219                 IF PG_DEBUG in ('Y', 'C') THEN
5220                     msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' ||
5221                          'next site under this customer is not at same priority as this site');
5222                 END IF;
5223                 IF PG_DEBUG in ('Y', 'C') THEN
5224 		  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'before Adding Add_to_Next_Steal_Atp');
5225 
5226                   Print_Period_Qty('l_current_steal_atp.atp_period:atp_qty = ',
5227 		       l_current_steal_atp);
5228                   Print_Period_Qty('l_next_steal_atp.atp_period:atp_qty = ',
5229 		       l_next_steal_atp);
5230                 END IF;
5231                 MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_current_steal_atp, l_next_steal_atp);
5232                 IF PG_DEBUG in ('Y', 'C') THEN
5233 		  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'after Adding Add_to_Next_Steal_Atp');
5234 
5235                   Print_Period_Qty('l_current_steal_atp.atp_period:atp_qty = ',
5236 		       l_current_steal_atp);
5237                   Print_Period_Qty('l_next_steal_atp.atp_period:atp_qty = ',
5238 		       l_next_steal_atp);
5239                 END IF;
5240 
5241               END IF;
5242             END IF;
5243           END IF;
5244 	  IF G_ATP_FW_CONSUME_METHOD = 1 AND l_fw_consume_tab(i) <> 0 THEN
5245 	    IF l_fw_consume_tab(i) = 2 THEN
5246               -- add negatives from l_next_steal_atp to l_current_atp and then do forward consumption
5247               IF PG_DEBUG in ('Y', 'C') THEN
5248                   msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_fw_consume_tab(i) = 2');
5249 		  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'before Adding Add_to_Current_Atp');
5250 
5251                   Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5252 		       l_current_atp);
5253                   Print_Period_Qty('l_next_steal_atp.atp_period:atp_qty = ',
5254 		       l_next_steal_atp);
5255               END IF;
5256 	      MSC_AATP_PROC.Add_to_Current_Atp(l_next_steal_atp, l_current_atp, l_return_status);
5257               IF  l_return_status = FND_API.G_RET_STS_ERROR THEN
5258                  IF PG_DEBUG in ('Y', 'C') THEN
5259                     msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error in call to Add_to_Current_Atp');
5260                  END IF;
5261               END IF;
5262               IF PG_DEBUG in ('Y', 'C') THEN
5263 		  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'after Adding Add_to_Current_Atp');
5264 
5265                   Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5266 		       l_current_atp);
5267                   Print_Period_Qty('l_next_steal_atp.atp_period:atp_qty = ',
5268 		       l_next_steal_atp);
5269               END IF;
5270 	    ELSIF l_fw_consume_tab(i) = 3 THEN
5271 	      -- add negatives from l_partner_next_steal_atp to l_current_atp and then do forward consumption
5272               IF PG_DEBUG in ('Y', 'C') THEN
5273                   msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_fw_consume_tab(i) = 3');
5274               END IF;
5275                 IF PG_DEBUG in ('Y', 'C') THEN
5276 		  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'before Adding Add_to_Current_Atp');
5277 
5278                   Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5279 		       l_current_atp);
5280                   Print_Period_Qty('l_partner_next_steal_atp.atp_period:atp_qty = ',
5281 		       l_partner_next_steal_atp);
5282                 END IF;
5283 	      MSC_AATP_PROC.Add_to_Current_Atp(l_partner_next_steal_atp, l_current_atp, l_return_status);
5284               IF  l_return_status = FND_API.G_RET_STS_ERROR THEN
5285                  IF PG_DEBUG in ('Y', 'C') THEN
5286                     msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error in call to Add_to_Current_Atp');
5287                  END IF;
5288               END IF;
5289               IF PG_DEBUG in ('Y', 'C') THEN
5290 		  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'after Adding Add_to_Current_Atp');
5291 
5292                   Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5293 		       l_current_atp);
5294                   Print_Period_Qty('l_partner_next_steal_atp.atp_period:atp_qty = ',
5295 		       l_partner_next_steal_atp);
5296               END IF;
5297 	    END IF;
5298             IF PG_DEBUG in ('Y', 'C') THEN
5299                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Before Atp_Forward_Consume for Method1');
5300                Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5301 	  	        l_current_atp);
5302             END IF;
5303 	    MSC_AATP_PROC.Atp_Forward_Consume(l_current_atp.atp_period, p_atf_date, l_current_atp.atp_qty, l_return_status);
5304             IF  l_return_status = FND_API.G_RET_STS_ERROR THEN
5305                IF PG_DEBUG in ('Y', 'C') THEN
5306                   msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error in call to Atp_Forward_Consume');
5307                END IF;
5308             END IF;
5309             IF PG_DEBUG in ('Y', 'C') THEN
5310                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'After Atp_Forward_Consume for Method1');
5311                Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5312 	  	        l_current_atp);
5313             END IF;
5314 
5315             /*Call Atp_Remove_Negatives to remove negatives in case we have negatives left
5316             after forward consumption. we will not show these negatives in atp_inquiry for
5317             Allocated ATP with User Defined %.*/
5318 
5319             MSC_AATP_PROC.Atp_Remove_Negatives(l_current_atp.atp_qty, l_return_status);
5320             IF  l_return_status = FND_API.G_RET_STS_ERROR THEN
5321                IF PG_DEBUG in ('Y', 'C') THEN
5322                   msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error in call to Atp_Remove_Negatives');
5323                END IF;
5324             END IF;
5325 
5326             IF PG_DEBUG in ('Y', 'C') THEN
5327                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'after Atp_Remove_Negatives');
5328                Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5329 	  		l_current_atp);
5330             END IF;
5331 	  END IF;
5332         END IF;
5333 
5334       END IF;
5335 
5336       IF G_ATP_FW_CONSUME_METHOD = 2 THEN
5337         -- method2, do accumulation and then calculate the adjusted cum
5338         IF PG_DEBUG in ('Y', 'C') THEN
5339            msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Before Atp_Accumulate for Method2');
5340            Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5341 		    l_current_atp);
5342         END IF;
5343 
5344         MSC_ATP_PROC.Atp_Accumulate(l_current_atp.atp_qty);
5345 
5346         IF PG_DEBUG in ('Y', 'C') THEN
5347            msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'After Atp_Accumulate for Method2');
5348            Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5349 		    l_current_atp);
5350         END IF;
5351 
5352 	MSC_AATP_PROC.Atp_Adjusted_Cum(l_current_atp, l_unallocated_atp, l_return_status);
5353         IF  l_return_status = FND_API.G_RET_STS_ERROR THEN
5354            IF PG_DEBUG in ('Y', 'C') THEN
5355               msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error in call to Atp_Forward_Consume');
5356            END IF;
5357         END IF;
5358         IF PG_DEBUG in ('Y', 'C') THEN
5359            msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' ||
5360                     'l_current_atp after Atp_Adjusted_Cum for Method2');
5361            Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5362 		    l_current_atp);
5363            msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' ||
5364                     'l_unallocated_atp after Atp_Adjusted_Cum for Method2');
5365 	   mm := l_unallocated_atp.atp_qty.FIRST;
5366 	   while mm is not null loop
5367 		msc_sch_wb.atp_debug('l_unallocated_atp.atp_period:atp_qty = ' ||
5368 		            l_current_atp.atp_period(mm) || ':' || l_unallocated_atp.atp_qty(mm));
5369 		mm := l_unallocated_atp.atp_qty.NEXT(mm);
5370 	   end loop;
5371         END IF;
5372 
5373       END IF;
5374       -- rajjain AATP Forward Consumption end
5375     ELSE --IF p_identifier <> -1 or (p_identifier = -1 and p_scenario_id <> -1) THEN
5376        /* rajjain 01/29/2003 Bug 2737596
5377           We only need to do b/w consumption, f/w consumption and accumulation in case the call is
5378           from MSC_AATP_PVT.VIEW_ALLOCATION for view total*/
5379       MSC_ATP_PROC.Atp_Consume(l_current_atp.atp_qty, l_current_atp.atp_qty.COUNT);
5380     END IF;
5381 
5382     -- 1665110
5383     IF PG_DEBUG in ('Y', 'C') THEN
5384        msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'p_demand_class = '||p_demand_class);
5385     END IF;
5386     EXIT WHEN (l_demand_class = p_demand_class);
5387     IF PG_DEBUG in ('Y', 'C') THEN
5388        msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'after the exit statement, so we did not exit');
5389     END IF;
5390 
5391   END LOOP;
5392 
5393   /* rajjain 01/29/2003 Bug 2737596
5394      We need not do accumulation in case the call is from VIEW_ALLOCATION
5395      for view total as we have already done this in atp_consume*/
5396   IF p_identifier <> -1 or (p_identifier = -1 and p_scenario_id <> -1) THEN
5397     IF G_ATP_FW_CONSUME_METHOD = 1 THEN
5398       -- we need to do accumulation only if forward consumption method is 1
5399       -- as we have the adjusted cum picture available if forward consumption method is 2
5400       MSC_ATP_PROC.Atp_Accumulate(l_current_atp.atp_qty);
5401 
5402       IF PG_DEBUG in ('Y', 'C') THEN
5403          msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Right after the Atp_Accumulate');
5404          msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_current_atp.atp_period.count = '||
5405                     l_current_atp.atp_period.count);
5406          msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_current_atp.atp_qty.count = '||
5407                     l_current_atp.atp_qty.count);
5408          Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5409 	    l_current_atp);
5410       END IF;
5411     END IF;
5412   END IF;
5413 
5414   x_atp_info := l_current_atp;
5415   --diag_atp: we have already calculate this date.
5416   /*-- get the infinite time fence date if it exists
5417   l_infinite_time_fence_date := MSC_ATP_FUNC.get_infinite_time_fence_date(p_instance_id,
5418              p_inventory_item_id,p_organization_id, p_plan_id);
5419   */
5420 
5421   IF l_infinite_time_fence_date IS NOT NULL THEN
5422       -- add one more entry to indicate infinite time fence date and quantity.
5423       x_atp_info.atp_qty.EXTEND;
5424       --x_atp_info.limit_qty.EXTEND;
5425       x_atp_info.atp_period.EXTEND;
5426 
5427       i := x_atp_info.atp_qty.COUNT;
5428       x_atp_info.atp_period(i) := l_infinite_time_fence_date;
5429       x_atp_info.atp_qty(i) := MSC_ATP_PVT.INFINITE_NUMBER;
5430       --x_atp_info.limit_qty(i) := MSC_ATP_PVT.INFINITE_NUMBER;
5431 
5432 
5433       IF NVL(p_insert_flag, 0) <> 0 THEN
5434         -- add one more entry to indicate infinite time fence date and quantity.
5435 
5436         x_atp_period.Cumulative_Quantity := x_atp_info.atp_qty;
5437 
5438         j := x_atp_period.Level.COUNT;
5439         MSC_SATP_FUNC.Extend_Atp_Period(x_atp_period, l_return_status);
5440         j := j + 1;
5441         IF j > 1 THEN
5442           x_atp_period.Period_End_Date(j-1) := l_infinite_time_fence_date -1;
5443 	  -- dsting
5444           --x_atp_period.Identifier1(j) := x_atp_supply_demand.Identifier1(j-1);
5445           --x_atp_period.Identifier2(j) := x_atp_supply_demand.Identifier2(j-1);
5446           x_atp_period.Identifier1(j) := x_atp_period.Identifier1(j-1);
5447           x_atp_period.Identifier2(j) := x_atp_period.Identifier2(j-1);
5448         END IF;
5449 
5450         x_atp_period.Level(j) := p_level;
5451         x_atp_period.Identifier(j) := p_identifier;
5452         x_atp_period.Scenario_Id(j) := p_scenario_id;
5453         x_atp_period.Pegging_Id(j) := NULL;
5454         x_atp_period.End_Pegging_Id(j) := NULL;
5455         x_atp_period.Inventory_Item_Id(j) := p_inventory_item_id;
5456         x_atp_period.Request_Item_Id(j) := p_inventory_item_id;
5457         x_atp_period.Organization_id(j) := p_organization_id;
5458         x_atp_period.Period_Start_Date(j) := l_infinite_time_fence_date;
5459         x_atp_period.Total_Supply_Quantity(j) := MSC_ATP_PVT.INFINITE_NUMBER;
5460         x_atp_period.Total_Demand_Quantity(j) := 0;
5461         -- time_phased_atp
5462         x_atp_period.Total_Bucketed_Demand_Quantity(j) := 0;
5463         x_atp_period.Period_Quantity(j) := MSC_ATP_PVT.INFINITE_NUMBER;
5464         x_atp_period.Cumulative_Quantity(j) := MSC_ATP_PVT.INFINITE_NUMBER;
5465 
5466     END IF;
5467   END IF;
5468  --END IF;
5469 
5470 END Item_Alloc_Cum_Atp;
5471 
5472 PROCEDURE Res_Alloc_Cum_Atp(
5473 	p_plan_id 	      IN 	NUMBER,
5474 	p_level               IN 	NUMBER,
5475 	p_identifier          IN 	NUMBER,
5476 	p_scenario_id         IN 	NUMBER,
5477 	p_department_id       IN 	NUMBER,
5478 	p_resource_id         IN 	NUMBER,
5479 	p_organization_id     IN 	NUMBER,
5480 	p_instance_id         IN 	NUMBER,
5481 	p_demand_class        IN 	VARCHAR2,
5482 	p_request_date        IN 	DATE,
5483 	p_insert_flag         IN 	NUMBER,
5484         p_max_capacity        IN        NUMBER,
5485         p_batchable_flag      IN        NUMBER,
5486         p_res_conversion_rate IN        NUMBER,
5487         p_res_uom_type	      IN        NUMBER,
5488 	x_atp_info            OUT  	NoCopy MRP_ATP_PVT.ATP_Info,
5489 	x_atp_period          OUT  	NoCopy MRP_ATP_PUB.ATP_Period_Typ,
5490 	x_atp_supply_demand   OUT  	NoCopy MRP_ATP_PUB.ATP_Supply_Demand_Typ)
5491 IS
5492 l_infinite_time_fence_date	DATE;
5493 l_default_atp_rule_id           NUMBER;
5494 l_calendar_exception_set_id     NUMBER;
5495 l_level_id                      NUMBER;
5496 l_priority			NUMBER := 1;
5497 l_allocation_percent		NUMBER := 100;
5498 l_inv_item_id			NUMBER;
5499 l_null_num  			NUMBER := null;
5500 l_steal_period_quantity		NUMBER;
5501 l_demand_class			VARCHAR2(80);
5502 l_uom_code			VARCHAR2(3);
5503 l_null_char    			VARCHAR2(3) := null;
5504 l_return_status			VARCHAR(1);
5505 l_calendar_code                 VARCHAR2(14);
5506 l_default_demand_class          VARCHAR2(34);
5507 i				PLS_INTEGER;
5508 mm				PLS_INTEGER;
5509 ii                              PLS_INTEGER;
5510 jj                              PLS_INTEGER;
5511 j				PLS_INTEGER;
5512 k				PLS_INTEGER;
5513 l_demand_class_tab		MRP_ATP_PUB.char80_arr
5514                                    := MRP_ATP_PUB.char80_arr();
5515 l_demand_class_priority_tab	MRP_ATP_PUB.number_arr
5516                                    := MRP_ATP_PUB.number_arr();
5517 l_current_atp			MRP_ATP_PVT.ATP_Info;
5518 l_next_steal_atp		MRP_ATP_PVT.ATP_Info;
5519 l_null_steal_atp		MRP_ATP_PVT.ATP_Info;
5520 l_current_steal_atp             MRP_ATP_PVT.ATP_Info;
5521 l_temp_atp                      MRP_ATP_PVT.ATP_Info;
5522 l_optimized_plan                PLS_INTEGER := 2;
5523 
5524 -- 1680719
5525 l_class_tab                     MRP_ATP_PUB.char30_arr
5526                                     := MRP_ATP_PUB.char30_arr();
5527 l_partner_tab                   MRP_ATP_PUB.number_arr
5528                                     := MRP_ATP_PUB.number_arr();
5529 l_class_next_steal_atp          MRP_ATP_PVT.ATP_Info;
5530 l_partner_next_steal_atp        MRP_ATP_PVT.ATP_Info;
5531 l_class_curr_steal_atp          MRP_ATP_PVT.ATP_Info;
5532 l_partner_curr_steal_atp        MRP_ATP_PVT.ATP_Info;
5533 l_pos1                          NUMBER;
5534 l_pos2                          NUMBER;
5535 delim     constant varchar2(1) := fnd_global.local_chr(13);
5536 MSO_Batch_Flag                  VARCHAR2(1);
5537 l_constraint_plan               NUMBER;
5538 l_use_batching                  NUMBER;
5539 --krajan 04/01/02 added for fstealing
5540 l_org_code                      VARCHAR2(7);
5541 l_plan_start_date               date;
5542 
5543 -- dsting for s/d performance enh
5544 l_temp_atp_supply_demand     	MRP_ATP_PUB.ATP_Supply_Demand_Typ;
5545 
5546 -- ship_rec_cal
5547 l_sysdate                       DATE := trunc(sysdate); --4135752
5548 l_sys_next_date                 DATE; --bug3333114
5549 
5550 BEGIN
5551 
5552   IF PG_DEBUG in ('Y', 'C') THEN
5553      msc_sch_wb.atp_debug('******* Res_Alloc_Cum_Atp *******');
5554      msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'p_plan_id =' || p_plan_id);
5555      msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'p_instance_id =' || p_instance_id);
5556      msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'p_department_id =' || p_department_id);
5557      msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'p_resource_id =' || p_resource_id);
5558      msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'p_organization_id =' || p_organization_id);
5559      msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'p_demand_class =' || p_demand_class);
5560      msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'p_request_date =' || p_request_date );
5561   END IF;
5562 MSO_Batch_flag := NVL(fnd_profile.value('MSO_BATCHABLE_FLAG'),'N');
5563   IF PG_DEBUG in ('Y', 'C') THEN
5564      msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'MSO_Batch_flag := ' || MSO_Batch_flag);
5565   END IF;
5566   Begin
5567     SELECT DECODE(plans.plan_type, 4, 2,
5568              DECODE(daily_material_constraints, 1, 1,
5569                DECODE(daily_resource_constraints, 1, 1,
5570                  DECODE(weekly_material_constraints, 1, 1,
5571                    DECODE(weekly_resource_constraints, 1, 1,
5572                      DECODE(period_material_constraints, 1, 1,
5573                        DECODE(period_resource_constraints, 1, 1, 2)
5574                            )
5575                          )
5576                        )
5577                      )
5578                    )
5579                  ),
5580            DECODE(MSO_Batch_Flag, 'Y', DECODE(plans.plan_type, 4, 0,2,0,  -- filter out MPS plans
5581              DECODE(daily_material_constraints, 1, 1,
5582                DECODE(daily_resource_constraints, 1, 1,
5583                  DECODE(weekly_material_constraints, 1, 1,
5584                    DECODE(weekly_resource_constraints, 1, 1,
5585                      DECODE(period_material_constraints, 1, 1,
5586                        DECODE(period_resource_constraints, 1, 1, 2)
5587                            )
5588                          )
5589                        )
5590                      )
5591                    )
5592                  ), 0),
5593            trunc(plan_start_date)
5594 
5595     INTO   l_optimized_plan,l_constraint_plan, l_plan_start_date
5596     FROM   msc_designators desig,
5597            msc_plans plans
5598     WHERE  plans.plan_id = p_plan_id
5599     AND    desig.designator = plans.compile_designator
5600     AND    desig.sr_instance_id = plans.sr_instance_id
5601     AND    desig.organization_id = plans.organization_id;
5602     EXCEPTION WHEN NO_DATA_FOUND THEN
5603            l_optimized_plan := 2;
5604            l_constraint_plan := 0;
5605   END;
5606   IF PG_DEBUG in ('Y', 'C') THEN
5607      msc_sch_wb.atp_debug('Res_alloc_Cum_ATP: l_optimized_plan: ' || l_optimized_plan);
5608      msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'l_constraint_plan  =' || l_constraint_plan);
5609   END IF;
5610 
5611   IF (MSO_Batch_Flag = 'Y')  and (l_constraint_plan = 1) and (p_batchable_flag = 1) THEN
5612         IF PG_DEBUG in ('Y', 'C') THEN
5613            msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'Do Batching');
5614         END IF;
5615         l_use_batching := 1;
5616   ELSE
5617         IF PG_DEBUG in ('Y', 'C') THEN
5618            msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'No Batching');
5619         END IF;
5620         l_use_batching := 0;
5621   END IF;
5622 
5623 
5624   -- find all the demand classes that we need to take care: all the demand
5625   -- classes that have higher priority  + this requested demand class.
5626 
5627   -- Logic
5628   -- Step 1:
5629   -- 	FOR each demand class DCi, we need to
5630   --  	1. get the net daily availability
5631   --  	2. do backward consumption
5632   --  	3. do backward consumption if DC1 to DC(i-1) has any negative bucket
5633   -- 	END LOOP
5634   -- Step 2:
5635   --    do accumulation for the requested demand class
5636 
5637   -- select the priority  and allocation_percent for that item/demand class.
5638   -- if no data found, check if this item has a valid allocation rule.
5639   -- otherwise return error.
5640 
5641  -- If request is from view allocation for total ATP, assign
5642  -- l_priority = -1 and l_allocation_percent = NULL
5643  IF p_scenario_id <> -1 THEN
5644     MSC_AATP_PVT.Get_DC_Info(p_instance_id, null, p_organization_id,
5645         p_department_id, p_resource_id, p_demand_class,
5646         p_request_date, l_level_id, l_priority, l_allocation_percent, l_return_status);
5647 
5648     IF  l_return_status = FND_API.G_RET_STS_ERROR THEN
5649         IF PG_DEBUG in ('Y', 'C') THEN
5650            msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'Error retrieving Priority and Demand Class');
5651         END IF;
5652     END IF;
5653  ELSE
5654      l_priority := -1;
5655      l_allocation_percent := NULL;
5656  END IF;
5657 
5658   -- find the demand classes that have priority  higher (small number) than
5659   -- the requested demand class
5660 
5661 -- IF l_allocation_percent <> 0.0 THEN
5662 
5663   IF PG_DEBUG in ('Y', 'C') THEN
5664      msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'l_allocation_percent = '||l_allocation_percent);
5665   END IF;
5666   -- We don't need to select all demand classes in case this procdure is
5667   -- called from MSC_AATP_PVT.VIEW_ALLOCATION as we don't need to take care
5668   -- of any existing stealing.
5669  IF p_identifier <> -1 THEN
5670 
5671  IF PG_DEBUG in ('Y', 'C') THEN
5672     msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'before select the high priority demand class');
5673  END IF;
5674 
5675   -- bug 1680719
5676  --bug3948494 Do not select Higher priority DC if the requested DC
5677  --is at highest priority, we donot honor for forward consumption method here.
5678   IF l_level_id = -1 AND l_priority <> 1 THEN
5679 
5680 
5681     IF PG_DEBUG in ('Y', 'C') THEN
5682        msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'l_level_id = '||l_level_id);
5683     END IF;
5684     SELECT demand_class, priority
5685     BULK COLLECT INTO l_demand_class_tab, l_demand_class_priority_tab
5686     FROM   msc_resource_hierarchy_mv
5687     WHERE  department_id = p_department_id
5688     AND    resource_id = p_resource_id
5689     AND	   organization_id = p_organization_id
5690     AND    sr_instance_id = p_instance_id
5691     AND    p_request_date BETWEEN effective_date AND disable_date
5692     AND    priority  <= l_priority  -- 1665110, add '='
5693     AND    level_id = l_level_id
5694     ORDER BY priority asc, allocation_percent desc;
5695 
5696   ELSIF l_level_id = 1 THEN
5697 
5698     IF PG_DEBUG in ('Y', 'C') THEN
5699        msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'l_level_id = '||l_level_id);
5700     END IF;
5701     SELECT demand_class, priority
5702     BULK COLLECT INTO l_demand_class_tab, l_demand_class_priority_tab
5703     FROM   msc_resource_hierarchy_mv
5704     WHERE  department_id = p_department_id
5705     AND    resource_id = p_resource_id
5706     AND    organization_id = p_organization_id
5707     AND    sr_instance_id = p_instance_id
5708     AND    p_request_date BETWEEN effective_date AND disable_date
5709     AND    priority  <= l_priority  -- 1665110, add '='
5710     AND    level_id = l_level_id
5711     ORDER BY priority asc, class;
5712 
5713   ELSIF l_level_id = 2 THEN
5714 
5715 
5716     IF PG_DEBUG in ('Y', 'C') THEN
5717        msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'l_level_id = '||l_level_id);
5718     END IF;
5719     SELECT mv1.demand_class, mv1.priority, mv1.class, mv1.partner_id
5720     BULK COLLECT INTO l_demand_class_tab, l_demand_class_priority_tab,
5721                       l_class_tab, l_partner_tab
5722     FROM   msc_resource_hierarchy_mv mv1
5723     WHERE  mv1.department_id = p_department_id
5724     AND    mv1.resource_id = p_resource_id
5725     AND    mv1.organization_id = p_organization_id
5726     AND    mv1.sr_instance_id = p_instance_id
5727     AND    p_request_date BETWEEN mv1.effective_date AND mv1.disable_date
5728     --AND    mv1.priority  <= l_priority -- 1665110, add '='
5729     AND    mv1.level_id = l_level_id
5730     AND trunc(mv1.priority, -3) <= trunc(l_priority, -3)
5731       ORDER BY trunc(mv1.priority, -3), mv1.class ,
5732                trunc(mv1.priority, -2), mv1.partner_id;
5733 
5734   ELSIF l_level_id = 3 THEN
5735 
5736     -- bug 1680719
5737     -- we need to select the class, partner_id, partner_site_id
5738 
5739     IF PG_DEBUG in ('Y', 'C') THEN
5740        msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'l_level_id = '||l_level_id);
5741     END IF;
5742     SELECT mv1.demand_class, mv1.priority, mv1.class, mv1.partner_id
5743     BULK COLLECT INTO l_demand_class_tab, l_demand_class_priority_tab,
5744                       l_class_tab, l_partner_tab
5745     FROM   msc_resource_hierarchy_mv mv1
5746     WHERE  mv1.department_id = p_department_id
5747     AND    mv1.resource_id = p_resource_id
5748     AND    mv1.organization_id = p_organization_id
5749     AND    mv1.sr_instance_id = p_instance_id
5750     AND    p_request_date BETWEEN mv1.effective_date AND mv1.disable_date
5751     --AND    mv1.priority  <= l_priority -- 1665110, add '='
5752     AND    mv1.level_id = l_level_id
5753     AND trunc(mv1.priority, -3) <= trunc(l_priority, -3)
5754       ORDER BY trunc(mv1.priority, -3), mv1.class ,
5755                trunc(mv1.priority, -2), mv1.partner_id,
5756                mv1.priority, mv1.partner_site_id;
5757   END IF;
5758 
5759  -- Bug 1807827, need to add the requested demand class in case call is from
5760  -- View_Allocation, else l_demand_class_tab remains empty.
5761  END IF;
5762  --ELSE           -- p_scenario_id = -1 for View_Allocation
5763  IF l_demand_class_tab.count = 0 THEN
5764    --/* 1665110
5765      -- add the request demand class into the list
5766      l_demand_class_tab.Extend;
5767      l_demand_class_priority_tab.Extend;
5768      i := l_demand_class_tab.COUNT;
5769      l_demand_class_priority_tab(i) := l_priority;
5770      l_demand_class_tab(i) := p_demand_class;
5771 
5772      -- 1680719
5773      IF l_level_id in (2, 3) THEN
5774          l_class_tab.Extend;
5775          l_partner_tab.Extend;
5776          l_pos1 := instr(p_demand_class,delim,1,1);
5777          l_pos2 := instr(p_demand_class,delim,1,2);
5778          l_class_tab(i) := substr(p_demand_class,1,l_pos1-1);
5779          IF l_pos2 = 0 THEN
5780            l_partner_tab(i) := substr(p_demand_class,l_pos1+1);
5781          ELSE
5782            l_partner_tab(i) := substr(p_demand_class,l_pos1+1,l_pos2-l_pos1-1) ;
5783          END IF;
5784      END IF;
5785  END IF;
5786 --1665110 */
5787   mm := l_demand_class_tab.FIRST;
5788 
5789   WHILE mm is not null LOOP
5790      IF PG_DEBUG in ('Y', 'C') THEN
5791         msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'l_demand_class_tab and priority = '||
5792         l_demand_class_tab(mm) ||' : '|| l_demand_class_priority_tab(mm));
5793      END IF;
5794 
5795      IF l_level_id in (2,3) THEN
5796        IF PG_DEBUG in ('Y', 'C') THEN
5797           msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'l_class_tab and partner = '||
5798         l_class_tab(mm) ||' : '||l_partner_tab(mm));
5799        END IF;
5800      END IF;
5801 
5802      mm := l_demand_class_tab.Next(mm);
5803   END LOOP;
5804 
5805   l_uom_code := NVL(fnd_profile.value('MSC:HOUR_UOM_CODE'),
5806                     fnd_profile.value('BOM:HOUR_UOM_CODE'));
5807 
5808   -- for performance reason, we need to get the following info and
5809   -- store in variables instead of joining it
5810 
5811   /* Modularize Item and Org Info */
5812   -- changed call
5813   MSC_ATP_PROC.get_global_org_info(p_instance_id, p_organization_id);
5814   l_default_atp_rule_id := MSC_ATP_PVT.G_ORG_INFO_REC.default_atp_rule_id;
5815   l_calendar_code := MSC_ATP_PVT.G_ORG_INFO_REC.cal_code;
5816   l_calendar_exception_set_id := MSC_ATP_PVT.G_ORG_INFO_REC.cal_exception_set_id;
5817   l_default_demand_class := MSC_ATP_PVT.G_ORG_INFO_REC.default_demand_class;
5818   l_org_code := MSC_ATP_PVT.G_ORG_INFO_REC.org_code;
5819   /* Modularize Item and Org Info */
5820   --bug3333114 start
5821   l_sys_next_date := MSC_CALENDAR.NEXT_WORK_DAY (
5822                                         l_calendar_code,
5823                                         p_instance_id,
5824                                         TRUNC(sysdate));
5825 
5826   IF PG_DEBUG in ('Y', 'C') THEN
5827   msc_sch_wb.atp_debug('Sys next Date : '||to_char(l_sys_next_date, 'DD-MON-YYYY'));
5828   END IF;
5829 
5830   IF (l_sys_next_date is NULL) THEN
5831       msc_sch_wb.atp_debug('Sys Next Date is null');
5832       MSC_SCH_WB.G_ATP_ERROR_CODE := MSC_ATP_PVT.NO_MATCHING_CAL_DATE;
5833       RAISE FND_API.G_EXC_ERROR;
5834   END IF;
5835   --bug3333114 end
5836 
5837   IF PG_DEBUG in ('Y', 'C') THEN
5838      msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'l_default_atp_rule_id='|| l_default_atp_rule_id);
5839      msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'l_calendar_code='||l_calendar_code);
5840      msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'l_calendar_exception_set_id'|| l_calendar_exception_set_id);
5841      msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'l_default_demand_class'|| l_default_demand_class);
5842      msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'l_org_code'|| l_org_code);
5843   END IF;
5844 
5845   FOR i in 1..l_demand_class_tab.COUNT LOOP
5846           l_demand_class := l_demand_class_tab(i);
5847           IF PG_DEBUG in ('Y', 'C') THEN
5848              msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'in i loop, i = '||i);
5849              msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'l_demand_class = '||l_demand_class);
5850           END IF;
5851    -- get the daily net availability for DCi
5852    IF (NVL(p_insert_flag, 0) = 0  OR l_demand_class <> p_demand_class) THEN
5853        -- we don't want details
5854 
5855      IF (l_use_batching = 1) THEN
5856        IF (l_optimized_plan = 1) THEN -- Constrained Plan Bug 2809639
5857           IF PG_DEBUG in ('Y', 'C') THEN
5858               msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'Constrained Plan Batching');
5859           END IF;
5860        	  SELECT 	SD_DATE,
5861        			SUM(SD_QTY)
5862           BULK COLLECT INTO
5863                 	l_current_atp.atp_period,
5864                 	l_current_atp.atp_qty
5865           FROM (
5866           SELECT  -- C.CALENDAR_DATE SD_DATE, -- 2859130
5867                   -- Bug 3348095
5868                   -- For ATP created records use end_date otherwise start_date
5869                   GREATEST(DECODE(REQ.record_source, 2, TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
5870                            TRUNC(REQ.START_DATE)),l_sys_next_date) SD_DATE, --bug3333114
5871                 -1*DECODE(REQ.RESOURCE_ID, -1, REQ.LOAD_RATE,
5872                        DECODE(REQ.END_DATE, NULL, REQ.RESOURCE_HOURS,
5873                          DECODE(REQ.record_source, 2, REQ.RESOURCE_HOURS,
5874                                  REQ.DAILY_RESOURCE_HOURS))) *
5875                    -- For ATP created records use resource_hours
5876                    -- End Bug 3348095
5877                 ---resource batching
5878                 DECODE(DR.UOM_CLASS_TYPE, 1, I.UNIT_WEIGHT, 2, UNIT_VOLUME) *
5879                        NVL(MUC.CONVERSION_RATE, 1) * NVL(S.NEW_ORDER_QUANTITY, FIRM_QUANTITY) *
5880 
5881                         /*New*/
5882 		DECODE(DECODE(G_HIERARCHY_PROFILE,
5883                               --bug 2424357
5884                               1, DECODE(S.DEMAND_CLASS, null, null,
5885                                     DECODE(l_demand_class, '-1',
5886                                         MSC_AATP_FUNC.Get_RES_Hierarchy_Demand_Class(
5887                                                   null,
5888                                                   null,
5889                                                   p_department_id,
5890                                                   p_resource_id,
5891                                                   p_organization_id,
5892                                                   p_instance_id,
5893                                                   -- 2859130 c.calendar_date,
5894                                                   -- Bug 3348095
5895                                                   -- For ATP created records use end_date
5896                                                   -- otherwise start_date
5897                                                   DECODE(REQ.record_source, 2,
5898                                                     TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
5899                                                       TRUNC(REQ.START_DATE)),
5900                                                   --trunc(req.start_date),
5901                                                   -- End Bug 3348095
5902                                                   l_level_id,
5903                                                   S.DEMAND_CLASS), S.DEMAND_CLASS)),
5904                               2, DECODE(S.CUSTOMER_ID, NULL, TO_CHAR(NULL),
5905                                         0, TO_CHAR(NULL),
5906                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
5907                                                   S.CUSTOMER_ID,
5908                                                   S.SHIP_TO_SITE_ID,
5909                                                   s.inventory_item_id,
5910                                                   p_organization_id,
5911                                                   p_instance_id,
5912                                                   -- 2859130 c.calendar_date,
5913                                                   -- Bug 3348095
5914                                                   -- For ATP created records use end_date
5915                                                   -- otherwise start_date
5916                                                   DECODE(REQ.record_source, 2,
5917                                                     TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
5918                                                       TRUNC(REQ.START_DATE)),
5919                                                   --trunc(req.start_date),
5920                                                   -- End Bug 3348095
5921                                                   l_level_id,
5922                                                   NULL))),
5923 		       l_demand_class, 1,
5924                        --bug 4089293: If l_demand_class is not null and demand class is populated
5925                        -- on  supplies record then 0 should be allocated.
5926                        Decode (S.Demand_Class, NULL,
5927 		       MSC_AATP_FUNC.Get_Res_Demand_Alloc_Percent(
5928                          -- 2859130 C.CALENDAR_DATE,
5929                          -- Bug 3348095
5930                          -- For ATP created records use end_date
5931                          -- otherwise start_date
5932                          DECODE(REQ.record_source, 2,
5933                            TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
5934                              TRUNC(REQ.START_DATE)),
5935                          --trunc(req.start_date),
5936                          -- End Bug 3348095
5937                          REQ.ASSEMBLY_ITEM_ID,
5938 			 p_organization_id,
5939                          p_instance_id,
5940                          p_department_id,
5941 			 p_resource_id,
5942                          DECODE(G_HIERARCHY_PROFILE,
5943                                 --2424357
5944                                 1, DECODE(S.DEMAND_CLASS, null, null,
5945                                        DECODE(l_demand_class, -1,
5946                                           MSC_AATP_FUNC.Get_RES_Hierarchy_Demand_Class(
5947                                                   null,
5948                                                   null,
5949                                                   p_department_id,
5950                                                   p_resource_id,
5951                                                   p_organization_id,
5952                                                   p_instance_id,
5953                                                   -- 2859130 c.calendar_date,
5954                                                   -- Bug 3348095
5955                                                   -- For ATP created records use end_date
5956                                                   -- otherwise start_date
5957                                                   DECODE(REQ.record_source, 2,
5958                                                     TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
5959                                                       TRUNC(REQ.START_DATE)),
5960                                                   --trunc(req.start_date),
5961                                                   -- End Bug 3348095
5962                                                   l_level_id,
5963                                                   S.DEMAND_CLASS), S.DEMAND_CLASS)),
5964                                 2, DECODE(S.CUSTOMER_ID, NULL, l_demand_class,
5965                                           0, l_demand_class,
5966                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
5967                                                   S.CUSTOMER_ID,
5968                                                   S.SHIP_TO_SITE_ID,
5969                                                   l_inv_item_id,
5970                                                   p_organization_id,
5971                                                   p_instance_id,
5972                                                   -- 2859130 c.calendar_date,
5973                                                   -- Bug 3348095
5974                                                   -- For ATP created records use end_date
5975                                                   -- otherwise start_date
5976                                                   DECODE(REQ.record_source, 2,
5977                                                     TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
5978                                                       TRUNC(REQ.START_DATE)),
5979                                                   --trunc(req.start_date),
5980                                                   -- End Bug 3348095
5981                                                   l_level_id,
5982                                                   NULL))),
5983                          l_demand_class), 0)) SD_QTY
5984                         /*New*/
5985           FROM    MSC_DEPARTMENT_RESOURCES DR,
5986                   MSC_SUPPLIES S,
5987                   MSC_RESOURCE_REQUIREMENTS REQ,
5988                   -- 2859130 MSC_CALENDAR_DATES C,
5989                   MSC_SYSTEM_ITEMS I,
5990                   MSC_UOM_CONVERSIONS  MUC
5991           WHERE   DR.PLAN_ID = p_plan_id
5992           AND     NVL(DR.OWNING_DEPARTMENT_ID, DR.DEPARTMENT_ID)=p_department_id
5993           AND     DR.RESOURCE_ID = p_resource_id
5994           AND     DR.SR_INSTANCE_ID = p_instance_id
5995           AND     DR.ORGANIZATION_ID = p_organization_id -- for performance
5996           AND     REQ.PLAN_ID = DR.PLAN_ID
5997           AND     REQ.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
5998           AND     REQ.RESOURCE_ID = DR.RESOURCE_ID
5999           AND     REQ.DEPARTMENT_ID = DR.DEPARTMENT_ID
6000           AND     NVL(REQ.PARENT_ID, 1)  = 1  -- Bug 2809639
6001           AND     S.PLAN_ID = DR.PLAN_ID
6002           AND     S.TRANSACTION_ID = REQ.SUPPLY_ID
6003           AND     S.SR_INSTANCE_ID = REQ.SR_INSTANCE_ID --bug3948494
6004                   -- Exclude Cancelled Supplies 2460645
6005           AND     NVL(S.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
6006           AND     I.SR_INSTANCE_ID = REQ.SR_INSTANCE_Id
6007           AND     I.PLAN_ID = S.PLAN_ID
6008           AND     I.ORGANIZATION_ID = S.ORGANIZATION_ID
6009           AND     I.INVENTORY_ITEM_ID = S.INVENTORY_ITEM_ID
6010           -- Begin CTO Option Dependent Resources ODR
6011           AND     ((I.bom_item_type <> 1 and I.bom_item_type <> 2)
6012                -- bom_item_type not model and option_class always committed.
6013                     AND   (I.atp_flag <> 'N')
6014                -- atp_flag is 'Y' then committed.
6015                     OR    (REQ.record_source = 2) ) -- this OR may be changed during performance analysis.
6016               -- if record created by ATP then committed.
6017           -- End CTO Option Dependent Resources
6018           AND     DECODE(p_res_uom_type, 1, I.WEIGHT_UOM, 2 , I.VOLUME_UOM) = MUC.UOM_CODE (+)
6019           AND     MUC.SR_INSTANCE_ID (+)= I.SR_INSTANCE_ID
6020           AND     MUC.INVENTORY_ITEM_ID (+)= 0
6021           -- 2859130
6022           --AND     C.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
6023           --AND     C.CALENDAR_CODE = l_calendar_code
6024           --AND     C.EXCEPTION_SET_ID = l_calendar_exception_set_id
6025           --AND     C.CALENDAR_DATE = TRUNC(REQ.START_DATE) -- Bug 2809639
6026           --AND     C.SEQ_NUM IS NOT NULL
6027           ---bug 2341075: get data from plan_satrt date instead of sysdate
6028           --AND     C.CALENDAR_DATE >= trunc(sysdate)
6029           AND     trunc(REQ.START_DATE) >= l_plan_start_date --4135752
6030           UNION ALL
6031           SELECT  trunc(MNRA.SHIFT_DATE) SD_DATE, --4135752
6032                   MNRA.CAPACITY_UNITS * ((DECODE(LEAST(MNRA.from_time, MNRA.to_time),
6033                        MNRA.to_time,MNRA.to_time + 24*3600,
6034                        MNRA.to_time) - MNRA.from_time)/3600) *
6035                                               NVL((MRHM.allocation_percent/100), 1)
6036 /*
6037                                               NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
6038                 				     p_instance_id,
6039                 			             null,
6040                                                      p_organization_id,
6041                                                      p_department_id,
6042                                                      p_resource_id,
6043                                                      l_demand_class,
6044                                                      SHIFT_DATE),1) SD_QTY
6045 */
6046           FROM    MSC_NET_RESOURCE_AVAIL MNRA,
6047                   msc_resource_hierarchy_mv MRHM
6048           WHERE   MNRA.PLAN_ID = p_plan_id
6049           AND     NVL(MNRA.PARENT_ID, -2) <> -1
6050           AND     MNRA.SR_INSTANCE_ID = p_instance_id
6051           AND     MNRA.RESOURCE_ID = p_resource_id
6052           AND     MNRA.DEPARTMENT_ID = p_department_id
6053           ---bug 2341075; get data from plan_start date
6054           --AND     SHIFT_DATE >= trunc(sysdate)
6055           --bug 4232627: select only those records which are after plan start date
6056           AND     MNRA.SHIFT_DATE >= l_plan_start_date
6057           --bug 4089293
6058           AND     MNRA.organization_id = p_organization_id
6059           AND     MRHM.department_id (+) = MNRA.department_id
6060           AND     MRHM.resource_id  (+)= MNRA.resource_id
6061           AND     MRHM.organization_id (+) = MNRA.organization_id
6062           AND     MRHM.sr_instance_id  (+)= MNRA.sr_instance_id
6063           --AND     MRHM.level_id (+) = -1 --4365873
6064           AND     decode(MRHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
6065           --bug 4232627:
6066           --AND     MNRA.shift_date >=  GREATEST(l_plan_start_date,MRHM.effective_date (+))
6067           AND     trunc(MNRA.shift_date) >=  trunc(MRHM.effective_date (+))
6068           AND     trunc(MNRA.shift_date) <=  trunc(MRHM.disable_date (+))
6069           AND     MRHM.demand_class (+)= l_demand_class
6070           )
6071           GROUP BY SD_DATE
6072           ORDER BY SD_DATE;--4698199
6073        ELSE  -- now Other plans Bug 2809639
6074           IF PG_DEBUG in ('Y', 'C') THEN
6075               msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'Other Plans Batching');
6076           END IF;
6077        	  SELECT 	SD_DATE,
6078        			SUM(SD_QTY)
6079           BULK COLLECT INTO
6080                 	l_current_atp.atp_period,
6081                 	l_current_atp.atp_qty
6082           FROM (
6083           SELECT  GREATEST(C.CALENDAR_DATE,l_sys_next_date) SD_DATE, --bug3333114
6084                 -1*DECODE(REQ.RESOURCE_ID, -1, REQ.LOAD_RATE,
6085                        DECODE(REQ.END_DATE, NULL, REQ.RESOURCE_HOURS,
6086                           -- Bug 3348095
6087                           DECODE(REQ.record_source, 2, REQ.RESOURCE_HOURS,
6088                                  REQ.DAILY_RESOURCE_HOURS))) *
6089                           -- For ATP created records use resource_hours
6090                           -- End Bug 3348095
6091                 ---resource batching
6092                 DECODE(DR.UOM_CLASS_TYPE, 1, I.UNIT_WEIGHT, 2, UNIT_VOLUME) *
6093                        NVL(MUC.CONVERSION_RATE, 1) * NVL(S.NEW_ORDER_QUANTITY, FIRM_QUANTITY) *
6094 
6095                         /*New*/
6096 		DECODE(DECODE(G_HIERARCHY_PROFILE,
6097                               --bug 2424357
6098                               1, DECODE(S.DEMAND_CLASS, null, null,
6099                                     DECODE(l_demand_class, '-1',
6100                                         MSC_AATP_FUNC.Get_RES_Hierarchy_Demand_Class(
6101                                                   null,
6102                                                   null,
6103                                                   p_department_id,
6104                                                   p_resource_id,
6105                                                   p_organization_id,
6106                                                   p_instance_id,
6107                                                   c.calendar_date,
6108                                                   l_level_id,
6109                                                   S.DEMAND_CLASS), S.DEMAND_CLASS)),
6110                               2, DECODE(S.CUSTOMER_ID, NULL, TO_CHAR(NULL),
6111                                         0, TO_CHAR(NULL),
6112                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
6113                                                   S.CUSTOMER_ID,
6114                                                   S.SHIP_TO_SITE_ID,
6115                                                   s.inventory_item_id,
6116                                                   p_organization_id,
6117                                                   p_instance_id,
6118                                                   c.calendar_date,
6119                                                   l_level_id,
6120                                                   NULL))),
6121 		       l_demand_class, 1,
6122                        --bug 4156016: If l_demand_class is not null and demand class is populated
6123                        -- on  supplies record then 0 should be allocated.
6124                        Decode (S.Demand_Class, NULL,
6125 		       MSC_AATP_FUNC.Get_Res_Demand_Alloc_Percent(C.CALENDAR_DATE,
6126                          REQ.ASSEMBLY_ITEM_ID,
6127 			 p_organization_id,
6128                          p_instance_id,
6129                          p_department_id,
6130 			 p_resource_id,
6131                          DECODE(G_HIERARCHY_PROFILE,
6132                                 --2424357
6133                                 1, DECODE(S.DEMAND_CLASS, null, null,
6134                                        DECODE(l_demand_class, -1,
6135                                           MSC_AATP_FUNC.Get_RES_Hierarchy_Demand_Class(
6136                                                   null,
6137                                                   null,
6138                                                   p_department_id,
6139                                                   p_resource_id,
6140                                                   p_organization_id,
6141                                                   p_instance_id,
6142                                                   c.calendar_date,
6143                                                   l_level_id,
6144                                                   S.DEMAND_CLASS), S.DEMAND_CLASS)),
6145                                 2, DECODE(S.CUSTOMER_ID, NULL, l_demand_class,
6146                                           0, l_demand_class,
6147                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
6148                                                   S.CUSTOMER_ID,
6149                                                   S.SHIP_TO_SITE_ID,
6150                                                   l_inv_item_id,
6151                                                   p_organization_id,
6152                                                   p_instance_id,
6153                                                   c.calendar_date,
6154                                                   l_level_id,
6155                                                   NULL))),
6156                          l_demand_class), 0)) SD_QTY
6157                         /*New*/
6158           FROM    MSC_DEPARTMENT_RESOURCES DR,
6159                   MSC_SUPPLIES S,
6160                   MSC_RESOURCE_REQUIREMENTS REQ,
6161                   MSC_CALENDAR_DATES C,
6162                   MSC_SYSTEM_ITEMS I,
6163                   MSC_UOM_CONVERSIONS  MUC
6164           WHERE   DR.PLAN_ID = p_plan_id
6165           AND     NVL(DR.OWNING_DEPARTMENT_ID, DR.DEPARTMENT_ID)=p_department_id
6166           AND     DR.RESOURCE_ID = p_resource_id
6167           AND     DR.SR_INSTANCE_ID = p_instance_id
6168           AND     DR.ORGANIZATION_ID = p_organization_id -- for performance
6169           AND     REQ.PLAN_ID = DR.PLAN_ID
6170           AND     REQ.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
6171           AND     REQ.RESOURCE_ID = DR.RESOURCE_ID
6172           AND     REQ.DEPARTMENT_ID = DR.DEPARTMENT_ID
6173           AND     NVL(REQ.PARENT_ID, l_optimized_plan) = l_optimized_plan
6174           AND     S.PLAN_ID = DR.PLAN_ID
6175           AND     S.TRANSACTION_ID = REQ.SUPPLY_ID
6176           AND     S.SR_INSTANCE_ID = REQ.SR_INSTANCE_ID --bug3948494
6177                   -- Exclude Cancelled Supplies 2460645
6178           AND     NVL(S.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
6179           AND     I.SR_INSTANCE_ID = REQ.SR_INSTANCE_Id
6180           AND     I.PLAN_ID = S.PLAN_ID
6181           AND     I.ORGANIZATION_ID = S.ORGANIZATION_ID
6182           AND     I.INVENTORY_ITEM_ID = S.INVENTORY_ITEM_ID
6183           -- Begin CTO Option Dependent Resources ODR
6184           AND     ((I.bom_item_type <> 1 and I.bom_item_type <> 2)
6185                -- bom_item_type not model and option_class always committed.
6186                     AND   (I.atp_flag <> 'N')
6187                -- atp_flag is 'Y' then committed.
6188                     OR    (REQ.record_source = 2) ) -- this OR may be changed during performance analysis.
6189               -- if record created by ATP then committed.
6190           -- End CTO Option Dependent Resources
6191           AND     DECODE(p_res_uom_type, 1, I.WEIGHT_UOM, 2 , I.VOLUME_UOM) = MUC.UOM_CODE (+)
6192           AND     MUC.SR_INSTANCE_ID (+)= I.SR_INSTANCE_ID
6193           AND     MUC.INVENTORY_ITEM_ID (+)= 0
6194           AND     C.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
6195           AND     C.CALENDAR_CODE = l_calendar_code
6196           AND     C.EXCEPTION_SET_ID = l_calendar_exception_set_id
6197                   -- Bug 3348095
6198                   -- Ensure that the ATP created resource Reqs
6199                   -- do not get double counted.
6200          AND      C.CALENDAR_DATE BETWEEN DECODE(REQ.record_source, 2,
6201                        TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)), TRUNC(REQ.START_DATE))
6202                    AND TRUNC(NVL(REQ.END_DATE, REQ.START_DATE))
6203                   -- End Bug 3348095
6204           AND     C.SEQ_NUM IS NOT NULL
6205           ---bug 2341075: get data from plan_satrt date instead of sysdate
6206           --AND     C.CALENDAR_DATE >= trunc(sysdate)
6207           AND     C.CALENDAR_DATE >= l_plan_start_date
6208           UNION ALL
6209           SELECT  trunc(MNRA.SHIFT_DATE) SD_DATE, --4135752
6210                   MNRA.CAPACITY_UNITS * ((DECODE(LEAST(MNRA.from_time, MNRA.to_time),
6211                        MNRA.to_time,MNRA.to_time + 24*3600,
6212                        MNRA.to_time) - MNRA.from_time)/3600) *
6213                                               NVL((MRHM.allocation_percent/100), 1)
6214 /*
6215                                               NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
6216                 				     p_instance_id,
6217                 			             null,
6218                                                      p_organization_id,
6219                                                      p_department_id,
6220                                                      p_resource_id,
6221                                                      l_demand_class,
6222                                                      SHIFT_DATE),1) SD_QTY
6223 */
6224           FROM    MSC_NET_RESOURCE_AVAIL MNRA,
6225                   msc_resource_hierarchy_mv MRHM
6226           WHERE   MNRA.PLAN_ID = p_plan_id
6227           AND     NVL(MNRA.PARENT_ID, -2) <> -1
6228           AND     MNRA.SR_INSTANCE_ID = p_instance_id
6229           AND     MNRA.RESOURCE_ID = p_resource_id
6230           AND     MNRA.DEPARTMENT_ID = p_department_id
6231           ---bug 2341075; get data from plan_start date
6232           --AND     SHIFT_DATE >= trunc(sysdate)
6233           --bug 4232627: select only those records which are after plan start date
6234           AND     MNRA.SHIFT_DATE >= l_plan_start_date
6235           --bug 4156016
6236           AND     MNRA.organization_id = p_organization_id
6237           AND     MRHM.department_id (+) = MNRA.department_id
6238           AND     MRHM.resource_id  (+)= MNRA.resource_id
6239           AND     MRHM.organization_id (+) = MNRA.organization_id
6240           AND     MRHM.sr_instance_id  (+)= MNRA.sr_instance_id
6241           --AND     MRHM.level_id (+) = -1 --4365873
6242           AND     decode(MRHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
6243           --bug 4232627:
6244           AND     trunc(MNRA.shift_date) >=  trunc(GREATEST(l_plan_start_date,MRHM.effective_date (+))) --4135752
6245           AND     trunc(MNRA.shift_date) <=  trunc(MRHM.disable_date (+))--4135752
6246           AND     MRHM.demand_class (+)= l_demand_class
6247           )
6248           GROUP BY SD_DATE
6249           ORDER BY SD_DATE;--4698199
6250        END IF; -- l_optimized_plan = 1 Bug 2809639
6251      ELSE --- IF l_use_batching =1 THEN
6252 
6253        IF (l_optimized_plan = 1) THEN -- Constrained Plan Bug 2809639
6254           IF PG_DEBUG in ('Y', 'C') THEN
6255               msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'Constrained Plan No Batching');
6256           END IF;
6257        	  SELECT 	SD_DATE,
6258        			SUM(SD_QTY)
6259           BULK COLLECT INTO
6260                 	l_current_atp.atp_period,
6261                 	l_current_atp.atp_qty
6262           FROM (
6263           SELECT  -- C.CALENDAR_DATE SD_DATE, -- 2859130
6264                   -- Bug 3348095
6265                   -- For ATP created records use end_date otherwise start_date
6266                  GREATEST(DECODE(REQ.record_source, 2, TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
6267                            TRUNC(REQ.START_DATE)),l_sys_next_date) SD_DATE, --bug3333114
6268                 -1*DECODE(REQ.RESOURCE_ID, -1, REQ.LOAD_RATE,
6269                        DECODE(REQ.END_DATE, NULL, REQ.RESOURCE_HOURS,
6270                       -- Bug 3348095
6271                         DECODE(REQ.record_source, 2, REQ.RESOURCE_HOURS,
6272                                  REQ.DAILY_RESOURCE_HOURS))) *
6273                       -- For ATP created records use resource_hours
6274                       -- End Bug 3348095
6275                         /*New*/
6276 		DECODE(DECODE(G_HIERARCHY_PROFILE,
6277                               --2424357
6278                               1, DECODE(S.DEMAND_CLASS, null, null,
6279                                     DECODE(l_demand_class, '-1',
6280                                        MSC_AATP_FUNC.Get_RES_Hierarchy_Demand_Class(
6281                                                   null,
6282                                                   null,
6283                                                   p_department_id,
6284   				                  p_resource_id,
6285                                                   p_organization_id,
6286                                                   p_instance_id,
6287                                                   -- 2859130 c.calendar_date,
6288                                                   -- Bug 3348095
6289                                                   -- For ATP created records use end_date
6290                                                   -- otherwise start_date
6291                                                   DECODE(REQ.record_source, 2,
6292                                                     TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
6293                                                       TRUNC(REQ.START_DATE)),
6294                                                   --trunc(req.start_date),
6295                                                   -- End Bug 3348095
6296                                                   l_level_id,
6297                                                   S.DEMAND_CLASS),S.DEMAND_CLASS)),
6298                               2, DECODE(S.CUSTOMER_ID, NULL, TO_CHAR(NULL),
6299                                         0, TO_CHAR(NULL),
6300                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
6301                                                   S.CUSTOMER_ID,
6302                                                   S.SHIP_TO_SITE_ID,
6303                                                   s.inventory_item_id,
6304                                                   p_organization_id,
6305                                                   p_instance_id,
6306                                                   -- 2859130 c.calendar_date,
6307                                                   -- Bug 3348095
6308                                                   -- For ATP created records use end_date
6309                                                   -- otherwise start_date
6310                                                   DECODE(REQ.record_source, 2,
6311                                                     TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
6312                                                       TRUNC(REQ.START_DATE)),
6313                                                   --trunc(req.start_date),
6314                                                   -- End Bug 3348095
6315                                                   l_level_id,
6316                                                   NULL))),
6317 		       l_demand_class, 1,
6318                        --bug 4156016: If l_demand_class is not null and demand class is populated
6319                        -- on  supplies record then 0 should be allocated.
6320                        Decode (S.Demand_Class, NULL,
6321 		       MSC_AATP_FUNC.Get_Res_Demand_Alloc_Percent(
6322                          -- 2859130 C.CALENDAR_DATE,
6323                          -- Bug 3348095
6324                          -- For ATP created records use end_date
6325                          -- otherwise start_date
6326                          DECODE(REQ.record_source, 2,
6327                            TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
6328                              TRUNC(REQ.START_DATE)),
6329                          --trunc(req.start_date),
6330                          -- End Bug 3348095
6331                          REQ.ASSEMBLY_ITEM_ID,
6332 			 p_organization_id,
6333                          p_instance_id,
6334                          p_department_id,
6335 			 p_resource_id,
6336                          DECODE(G_HIERARCHY_PROFILE,
6337                                 --2424357
6338                                 1, DECODE(S.DEMAND_CLASS, null, null,
6339                                        DECODE(l_demand_class, '-1',
6340                                           MSC_AATP_FUNC.Get_RES_Hierarchy_Demand_Class(
6341                                                   null,
6342                                                   null,
6343                                                   p_department_id,
6344                                                   p_resource_id,
6345                                                   p_organization_id,
6346                                                   p_instance_id,
6347                                                   -- c.calendar_date,
6348                                                   -- Bug 3348095
6349                                                   -- For ATP created records use end_date
6350                                                   -- otherwise start_date
6351                                                   DECODE(REQ.record_source, 2,
6352                                                     TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
6353                                                       TRUNC(REQ.START_DATE)),
6354                                                   --trunc(req.start_date),
6355                                                   -- End Bug 3348095
6356                                                   l_level_id,
6357                                                   S.DEMAND_CLASS), S.DEMAND_CLASS)),
6358                                 2, DECODE(S.CUSTOMER_ID, NULL, l_demand_class,
6359                                           0, l_demand_class,
6360                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
6361                                                   S.CUSTOMER_ID,
6362                                                   S.SHIP_TO_SITE_ID,
6363                                                   l_inv_item_id,
6364                                                   p_organization_id,
6365                                                   p_instance_id,
6366                                                   -- 2859130 c.calendar_date,
6367                                                   -- Bug 3348095
6368                                                   -- For ATP created records use end_date
6369                                                   -- otherwise start_date
6370                                                   DECODE(REQ.record_source, 2,
6371                                                     TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
6372                                                       TRUNC(REQ.START_DATE)),
6373                                                   --trunc(req.start_date),
6374                                                   -- End Bug 3348095
6375                                                   l_level_id,
6376                                                   NULL))),
6377                          l_demand_class), 0)) SD_QTY
6378                         /*New*/
6379           FROM    MSC_DEPARTMENT_RESOURCES DR,
6380                   MSC_SUPPLIES S,
6381                   MSC_SYSTEM_ITEMS I,   -- CTO ODR
6382                   MSC_RESOURCE_REQUIREMENTS REQ
6383                   -- 2859130 MSC_CALENDAR_DATES C
6384           WHERE   DR.PLAN_ID = p_plan_id
6385           AND     NVL(DR.OWNING_DEPARTMENT_ID, DR.DEPARTMENT_ID)=p_department_id
6386           AND     DR.RESOURCE_ID = p_resource_id
6387           AND     DR.SR_INSTANCE_ID = p_instance_id
6388           AND     DR.ORGANIZATION_ID = p_organization_id -- for performance
6389           AND     REQ.PLAN_ID = DR.PLAN_ID
6390           AND     REQ.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
6391           AND     REQ.RESOURCE_ID = DR.RESOURCE_ID
6392           AND     REQ.DEPARTMENT_ID = DR.DEPARTMENT_ID
6393           AND     NVL(REQ.PARENT_ID, 1)  = 1 -- Bug 2809639
6394           -- CTO Option Dependent Resources ODR
6395           AND     I.SR_INSTANCE_ID = REQ.SR_INSTANCE_Id
6396           AND     I.PLAN_ID = REQ.PLAN_ID
6397           AND     I.ORGANIZATION_ID = REQ.ORGANIZATION_ID
6398           AND     I.INVENTORY_ITEM_ID = REQ.ASSEMBLY_ITEM_ID
6399           AND     ((I.bom_item_type <> 1 and I.bom_item_type <> 2)
6400                 -- bom_item_type not model and option_class always committed.
6401                      AND   (I.atp_flag <> 'N')
6402                 -- atp_flag is 'Y' then committed.
6403                      OR    (REQ.record_source = 2) ) -- this OR may be changed during performance analysis.
6404                -- if record created by ATP then committed.
6405           -- End CTO Option Dependent Resources
6406           AND     S.PLAN_ID = DR.PLAN_ID
6407           AND     S.TRANSACTION_ID = REQ.SUPPLY_ID
6408           AND     S.SR_INSTANCE_ID = REQ.SR_INSTANCE_ID --bug3948494
6409           -- Exclude Cancelled Supplies 2460645
6410           AND     NVL(S.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
6411 
6412           -- 2859130
6413           -- AND     C.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
6414           -- AND     C.CALENDAR_CODE = l_calendar_code
6415           -- AND     C.EXCEPTION_SET_ID = l_calendar_exception_set_id
6416           -- AND     C.CALENDAR_DATE = TRUNC(REQ.START_DATE) -- Bug 2809639
6417           -- AND     C.SEQ_NUM IS NOT NULL
6418           ---bug 2341075
6419           --AND     C.CALENDAR_DATE >= trunc(sysdate)
6420           AND     trunc(REQ.START_DATE) >= l_plan_start_date --4135752
6421           UNION ALL
6422           SELECT  trunc(MNRA.SHIFT_DATE) SD_DATE, --4135752
6423                   MNRA.CAPACITY_UNITS * ((DECODE(LEAST(MNRA.from_time, MNRA.to_time),
6424                        MNRA.to_time,MNRA.to_time + 24*3600,
6425                        MNRA.to_time) - MNRA.from_time)/3600) *
6426                                               NVL((MRHM.allocation_percent/100), 1)
6427                                                     /*
6428                                                      NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
6429                 				     p_instance_id,
6430                 			             null,
6431                                                      p_organization_id,
6432                                                      p_department_id,
6433                                                      p_resource_id,
6434                                                      l_demand_class,
6435                                                      SHIFT_DATE),1) SD_QTY */
6436           FROM    MSC_NET_RESOURCE_AVAIL MNRA,
6437                   msc_resource_hierarchy_mv MRHM
6438           WHERE   MNRA.PLAN_ID = p_plan_id
6439           AND     NVL(MNRA.PARENT_ID, -2) <> -1
6440           AND     MNRA.SR_INSTANCE_ID = p_instance_id
6441           AND     MNRA.RESOURCE_ID = p_resource_id
6442           AND     MNRA.DEPARTMENT_ID = p_department_id
6443           --bug 2341075
6444           --AND     SHIFT_DATE >= trunc(sysdate)
6445           --bug 4232627: select only those records which are after plan start date
6446           AND     MNRA.SHIFT_DATE >= l_plan_start_date
6447           --bug 4156016
6448           AND     MNRA.organization_id = p_organization_id
6449           AND     MRHM.department_id (+) = MNRA.department_id
6450           AND     MRHM.resource_id  (+)= MNRA.resource_id
6451           AND     MRHM.organization_id (+) = MNRA.organization_id
6452           AND     MRHM.sr_instance_id  (+)= MNRA.sr_instance_id
6453           --AND     MRHM.level_id (+) = -1 --4365873
6454           AND     decode(MRHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
6455            --bug 4232627:
6456           AND     trunc(MNRA.shift_date) >=  trunc(GREATEST(l_plan_start_date,MRHM.effective_date (+))) --4135752
6457           AND     trunc(MNRA.shift_date) <=  trunc(MRHM.disable_date (+)) --4135752
6458           AND     MRHM.demand_class (+)= l_demand_class
6459           )
6460           GROUP BY SD_DATE
6461           ORDER BY SD_DATE;--4698199
6462        ELSE  -- now Other plans Bug 2809639
6463           IF PG_DEBUG in ('Y', 'C') THEN
6464               msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'Other Plans No Batching');
6465           END IF;
6466        	  SELECT 	SD_DATE,
6467        			SUM(SD_QTY)
6468           BULK COLLECT INTO
6469                 	l_current_atp.atp_period,
6470                 	l_current_atp.atp_qty
6471           FROM (
6472           SELECT GREATEST(C.CALENDAR_DATE,l_sys_next_date) SD_DATE,--bug3333114
6473                 -1*DECODE(REQ.RESOURCE_ID, -1, REQ.LOAD_RATE,
6474                        DECODE(REQ.END_DATE, NULL, REQ.RESOURCE_HOURS,
6475                           -- Bug 3348095
6476                           DECODE(REQ.record_source, 2, REQ.RESOURCE_HOURS,
6477                                  REQ.DAILY_RESOURCE_HOURS))) *
6478                           -- For ATP created records use resource_hours
6479                           -- End Bug 3348095
6480                         /*New*/
6481 		DECODE(DECODE(G_HIERARCHY_PROFILE,
6482                               --2424357
6483                               1, DECODE(S.DEMAND_CLASS, null, null,
6484                                     DECODE(l_demand_class, '-1',
6485                                        MSC_AATP_FUNC.Get_RES_Hierarchy_Demand_Class(
6486                                                   null,
6487                                                   null,
6488                                                   p_department_id,
6489   				                  p_resource_id,
6490                                                   p_organization_id,
6491                                                   p_instance_id,
6492                                                   c.calendar_date,
6493                                                   l_level_id,
6494                                                   S.DEMAND_CLASS),S.DEMAND_CLASS)),
6495                               2, DECODE(S.CUSTOMER_ID, NULL, TO_CHAR(NULL),
6496                                         0, TO_CHAR(NULL),
6497                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
6498                                                   S.CUSTOMER_ID,
6499                                                   S.SHIP_TO_SITE_ID,
6500                                                   s.inventory_item_id,
6501                                                   p_organization_id,
6502                                                   p_instance_id,
6503                                                   c.calendar_date,
6504                                                   l_level_id,
6505                                                   NULL))),
6506 		       l_demand_class, 1,
6507                        --bug 4156016: If l_demand_class is not null and demand class is populated
6508                        -- on  supplies record then 0 should be allocated.
6509                        Decode (S.Demand_Class, NULL,
6510 		       MSC_AATP_FUNC.Get_Res_Demand_Alloc_Percent(C.CALENDAR_DATE,
6511                          REQ.ASSEMBLY_ITEM_ID,
6512 			 p_organization_id,
6513                          p_instance_id,
6514                          p_department_id,
6515 			 p_resource_id,
6516                          DECODE(G_HIERARCHY_PROFILE,
6517                                 --2424357
6518                                 1, DECODE(S.DEMAND_CLASS, null, null,
6519                                        DECODE(l_demand_class, '-1',
6520                                           MSC_AATP_FUNC.Get_RES_Hierarchy_Demand_Class(
6521                                                   null,
6522                                                   null,
6523                                                   p_department_id,
6524                                                   p_resource_id,
6525                                                   p_organization_id,
6526                                                   p_instance_id,
6527                                                   c.calendar_date,
6528                                                   l_level_id,
6529                                                   S.DEMAND_CLASS), S.DEMAND_CLASS)),
6530                                 2, DECODE(S.CUSTOMER_ID, NULL, l_demand_class,
6531                                           0, l_demand_class,
6532                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
6533                                                   S.CUSTOMER_ID,
6534                                                   S.SHIP_TO_SITE_ID,
6535                                                   l_inv_item_id,
6536                                                   p_organization_id,
6537                                                   p_instance_id,
6538                                                   c.calendar_date,
6539                                                   l_level_id,
6540                                                   NULL))),
6541                          l_demand_class), 0)) SD_QTY
6542                         /*New*/
6543           FROM    MSC_DEPARTMENT_RESOURCES DR,
6544                   MSC_SUPPLIES S,
6545                   MSC_SYSTEM_ITEMS I,   -- CTO ODR
6546                   MSC_RESOURCE_REQUIREMENTS REQ,
6547                   MSC_CALENDAR_DATES C
6548           WHERE   DR.PLAN_ID = p_plan_id
6549           AND     NVL(DR.OWNING_DEPARTMENT_ID, DR.DEPARTMENT_ID)=p_department_id
6550           AND     DR.RESOURCE_ID = p_resource_id
6551           AND     DR.SR_INSTANCE_ID = p_instance_id
6552           AND     DR.ORGANIZATION_ID = p_organization_id -- for performance
6553           AND     REQ.PLAN_ID = DR.PLAN_ID
6554           AND     REQ.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
6555           AND     REQ.RESOURCE_ID = DR.RESOURCE_ID
6556           AND     REQ.DEPARTMENT_ID = DR.DEPARTMENT_ID
6557           AND     NVL(REQ.PARENT_ID, l_optimized_plan) = l_optimized_plan
6558           -- CTO Option Dependent Resources ODR
6559           AND     I.SR_INSTANCE_ID = REQ.SR_INSTANCE_Id
6560           AND     I.PLAN_ID = REQ.PLAN_ID
6561           AND     I.ORGANIZATION_ID = REQ.ORGANIZATION_ID
6562           AND     I.INVENTORY_ITEM_ID = REQ.ASSEMBLY_ITEM_ID
6563           AND     ((I.bom_item_type <> 1 and I.bom_item_type <> 2)
6564                 -- bom_item_type not model and option_class always committed.
6565                      AND   (I.atp_flag <> 'N')
6566                 -- atp_flag is 'Y' then committed.
6567                      OR    (REQ.record_source = 2) ) -- this OR may be changed during performance analysis.
6568                -- if record created by ATP then committed.
6569           -- End CTO Option Dependent Resources
6570           AND     S.PLAN_ID = DR.PLAN_ID
6571           AND     S.TRANSACTION_ID = REQ.SUPPLY_ID
6572           AND     S.SR_INSTANCE_ID = REQ.SR_INSTANCE_ID --bug3948494
6573           -- Exclude Cancelled Supplies 2460645
6574           AND     NVL(S.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
6575 
6576           AND     C.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
6577           AND     C.CALENDAR_CODE = l_calendar_code
6578           AND     C.EXCEPTION_SET_ID = l_calendar_exception_set_id
6579                   -- Bug 3348095
6580                   -- Ensure that the ATP created resource Reqs
6581                   -- do not get double counted.
6582          AND      C.CALENDAR_DATE BETWEEN DECODE(REQ.record_source, 2,
6583                        TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)), TRUNC(REQ.START_DATE))
6584                    AND TRUNC(NVL(REQ.END_DATE, REQ.START_DATE))
6585                   -- End Bug 3348095
6586           AND     C.SEQ_NUM IS NOT NULL
6587           ---bug 2341075
6588           --AND     C.CALENDAR_DATE >= trunc(sysdate)
6589           AND     C.CALENDAR_DATE >= l_plan_start_date
6590           UNION ALL
6591           SELECT  trunc(MNRA.SHIFT_DATE) SD_DATE, --4135752
6592                   MNRA.CAPACITY_UNITS * ((DECODE(LEAST(MNRA.from_time, MNRA.to_time),
6593                        MNRA.to_time,MNRA.to_time + 24*3600,
6594                        MNRA.to_time) - MNRA.from_time)/3600) *
6595                                               NVL((MRHM.allocation_percent/100), 1)
6596 /*
6597                                               NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
6598                 				     p_instance_id,
6599                 			             null,
6600                                                      p_organization_id,
6601                                                      p_department_id,
6602                                                      p_resource_id,
6603                                                      l_demand_class,
6604                                                      SHIFT_DATE),1) SD_QTY
6605 */
6606           FROM    MSC_NET_RESOURCE_AVAIL MNRA,
6607                   msc_resource_hierarchy_mv MRHM
6608           WHERE   MNRA.PLAN_ID = p_plan_id
6609           AND     NVL(MNRA.PARENT_ID, -2) <> -1
6610           AND     MNRA.SR_INSTANCE_ID = p_instance_id
6611           AND     MNRA.RESOURCE_ID = p_resource_id
6612           AND     MNRA.DEPARTMENT_ID = p_department_id
6613           ---bug 2341075; get data from plan_start date
6614           --AND     SHIFT_DATE >= trunc(sysdate)
6615           --bug 4232627: select only those records which are after plan start date
6616           AND     MNRA.SHIFT_DATE >= l_plan_start_date
6617           --bug 4156016
6618           AND     MNRA.organization_id = p_organization_id
6619           AND     MRHM.department_id (+) = MNRA.department_id
6620           AND     MRHM.resource_id  (+)= MNRA.resource_id
6621           AND     MRHM.organization_id (+) = MNRA.organization_id
6622           AND     MRHM.sr_instance_id  (+)= MNRA.sr_instance_id
6623           --AND     MRHM.level_id (+) = -1 --4365873
6624           AND     decode(MRHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
6625           --bug 4232627:
6626           AND     trunc(MNRA.shift_date) >=  trunc(GREATEST(l_plan_start_date,MRHM.effective_date (+))) --4135752
6627           AND     trunc(MNRA.shift_date) <=  trunc(MRHM.disable_date (+)) --4135752
6628           AND     MRHM.demand_class (+)= l_demand_class
6629           )
6630           GROUP BY SD_DATE
6631           ORDER BY SD_DATE;--4698199
6632        END IF; -- l_optimized_plan = 1 Bug 2809639
6633       -- bug 1657855,  remove support for min alloc
6634       --l_current_atp.limit_qty := l_current_atp.atp_qty;
6635      END IF;
6636    ELSE
6637         -- IF (NVL(p_insert_flag, 0) <> 0  AND l_demand_class = p_demand_class)
6638         -- OR p_scenario_id = -1 - we want details
6639 
6640       -- 2792336
6641      MSC_ATP_DB_UTILS.Clear_SD_Details_Temp();
6642 
6643      IF (l_use_batching = 1) THEN
6644        IF (l_optimized_plan = 1) THEN -- Constrained Plan Bug 2809639
6645          IF PG_DEBUG in ('Y', 'C') THEN
6646               msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'Constrained Plan Batching Details');
6647          END IF;
6648 	 INSERT INTO msc_atp_sd_details_temp (
6649 		ATP_Level,
6650 		Order_line_id,
6651 		Scenario_Id,
6652 		Inventory_Item_Id,
6653 		Request_Item_Id,
6654 		Organization_Id,
6655 		Department_Id,
6656 		Resource_Id,
6657 		Supplier_Id,
6658 		Supplier_Site_Id,
6659 		From_Organization_Id,
6660 		From_Location_Id,
6661 		To_Organization_Id,
6662 		To_Location_Id,
6663 		Ship_Method,
6664 		UOM_code,
6665 		Supply_Demand_Type,
6666 		Supply_Demand_Source_Type,
6667 		Supply_Demand_Source_Type_Name,
6668 		Identifier1,
6669 		Identifier2,
6670 		Identifier3,
6671 		Identifier4,
6672 		Supply_Demand_Quantity,
6673 		Supply_Demand_Date,
6674 		Disposition_Type,
6675 		Disposition_Name,
6676 		Pegging_Id,
6677 		End_Pegging_Id,
6678 		creation_date,
6679 		created_by,
6680 		last_update_date,
6681 		last_updated_by,
6682 		last_update_login,
6683 		Unallocated_Quantity
6684 	)
6685 
6686 	(SELECT
6687     	 	p_level col1,
6688 		p_identifier col2,
6689                 p_scenario_id col3,
6690                 l_null_num col4 ,
6691                 l_null_num col5,
6692 		p_organization_id col6,
6693                 p_department_id col7,
6694                 p_resource_id col8,
6695                 l_null_num col9,
6696                 l_null_num col10,
6697                 l_null_num col11,
6698                 l_null_num col12,
6699                 l_null_num col13,
6700                 l_null_num col14,
6701 		l_null_char col15,
6702 		l_uom_code col16,
6703 		1 col17, -- demand
6704 		S.ORDER_TYPE col18,
6705                 l_null_char col19,
6706 		REQ.SR_INSTANCE_ID col20,
6707                 l_null_num col21,
6708 		REQ.TRANSACTION_ID col22,
6709 		l_null_num col23,
6710                 -1* DECODE(REQ.RESOURCE_ID, -1, REQ.LOAD_RATE,
6711                        DECODE(REQ.END_DATE, NULL, REQ.RESOURCE_HOURS,
6712                          -- Bug 3348095
6713                          DECODE(REQ.record_source, 2, REQ.RESOURCE_HOURS,
6714                                  REQ.DAILY_RESOURCE_HOURS))) *
6715                          -- For ATP created records use resource_hours
6716                          -- End Bug 3348095
6717                  ---- resource batching
6718                 DECODE(DR.UOM_CLASS_TYPE, 1, I.UNIT_WEIGHT, 2, UNIT_VOLUME) *
6719                 NVL(MUC.CONVERSION_RATE, 1) * NVL(S.NEW_ORDER_QUANTITY,S.FIRM_QUANTITY) *
6720 
6721                         /*New*/
6722                 DECODE(p_scenario_id, -1, 1,
6723                        DECODE(DECODE(G_HIERARCHY_PROFILE,
6724                               --2424357
6725                               1, DECODE(S.DEMAND_CLASS, null, null,
6726                                      DECODE(l_demand_class, '-1',
6727                                          MSC_AATP_FUNC.Get_RES_Hierarchy_Demand_Class(
6728                                                   null,
6729                                                   null,
6730                                                   p_department_id,
6731                                                   p_resource_id,
6732                                                   p_organization_id,
6733                                                   p_instance_id,
6734                                                   -- 2859130 c.calendar_date,
6735                                                   -- Bug 3348095
6736                                                   -- For ATP created records use end_date
6737                                                   -- otherwise start_date
6738                                                   DECODE(REQ.record_source, 2,
6739                                                     TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
6740                                                       TRUNC(REQ.START_DATE)),
6741                                                   --trunc(req.start_date),
6742                                                   -- End Bug 3348095
6743                                                   l_level_id,
6744                                                   S.DEMAND_CLASS), S.DEMAND_CLASS)),
6745                               2, DECODE(S.CUSTOMER_ID, NULL, TO_CHAR(NULL),
6746                                         0, TO_CHAR(NULL),
6747                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
6748                                                   S.CUSTOMER_ID,
6749                                                   S.SHIP_TO_SITE_ID,
6750                                                   s.inventory_item_id,
6751                                                   p_organization_id,
6752                                                   p_instance_id,
6753                                                   -- 2859130 c.calendar_date,
6754                                                   -- Bug 3348095
6755                                                   -- For ATP created records use end_date
6756                                                   -- otherwise start_date
6757                                                   DECODE(REQ.record_source, 2,
6758                                                     TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
6759                                                       TRUNC(REQ.START_DATE)),
6760                                                   --trunc(req.start_date),
6761                                                   -- End Bug 3348095
6762                                                   l_level_id,
6763                                                   NULL))),
6764                        l_demand_class, 1,
6765                        --bug 4156016: If l_demand_class is not null and demand class is populated
6766                        -- on  supplies record then 0 should be allocated.
6767                        Decode (S.Demand_Class, NULL,
6768                        MSC_AATP_FUNC.Get_Res_Demand_Alloc_Percent(
6769                          -- 2859130 C.CALENDAR_DATE,
6770                          -- Bug 3348095
6771                          -- For ATP created records use end_date
6772                          -- otherwise start_date
6773                          DECODE(REQ.record_source, 2,
6774                             TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
6775                                TRUNC(REQ.START_DATE)),
6776                          --trunc(req.start_date),
6777                          -- End Bug 3348095
6778                          REQ.ASSEMBLY_ITEM_ID,
6779                          p_organization_id,
6780                          p_instance_id,
6781                          p_department_id,
6782                          p_resource_id,
6783                          DECODE(G_HIERARCHY_PROFILE,
6784                                 ---2424357
6785                                 1, DECODE(S.DEMAND_CLASS, null, null,
6786                                       DECODE(l_demand_class, '-1',
6787                                           MSC_AATP_FUNC.Get_RES_Hierarchy_Demand_Class(
6788                                                   null,
6789                                                   null,
6790                                                   p_department_id,
6791                                                   p_resource_id,
6792                                                   p_organization_id,
6793                                                   p_instance_id,
6794                                                   -- 2859130 c.calendar_date,
6795                                                   -- Bug 3348095
6796                                                   -- For ATP created records use end_date
6797                                                   -- otherwise start_date
6798                                                   DECODE(REQ.record_source, 2,
6799                                                     TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
6800                                                       TRUNC(REQ.START_DATE)),
6801                                                   --trunc(req.start_date),
6802                                                   -- End Bug 3348095
6803                                                   l_level_id,
6804                                                    S.DEMAND_CLASS), S.DEMAND_CLASS)),
6805                                 2, DECODE(S.CUSTOMER_ID, NULL, l_demand_class,
6806                                           0, l_demand_class,
6807                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
6808                                                   S.CUSTOMER_ID,
6809                                                   S.SHIP_TO_SITE_ID,
6810                                                   l_inv_item_id,
6811                                                   p_organization_id,
6812                                                   p_instance_id,
6813                                                   --2859130 c.calendar_date,
6814                                                   -- Bug 3348095
6815                                                   -- For ATP created records use end_date
6816                                                   -- otherwise start_date
6817                                                   DECODE(REQ.record_source, 2,
6818                                                     TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
6819                                                       TRUNC(REQ.START_DATE)),
6820                                                   --trunc(req.start_date),
6821                                                   -- End Bug 3348095
6822                                                   l_level_id,
6823                                                   NULL))),
6824                          l_demand_class), 0))) col24,
6825                         /*New*/
6826 		-- 2859130 C.CALENDAR_DATE col25,
6827                 -- Bug 3348095
6828                 -- For ATP created records use end_date otherwise start_date
6829                 GREATEST(DECODE(REQ.record_source, 2,
6830                    TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) , TRUNC(REQ.START_DATE)),l_sys_next_date) col25,  --bug3333114
6831                 -- End Bug 3348095
6832                 -- TRUNC(req.start_date) col25,
6833                 l_null_num col26,
6834                 -- Bug 2771075. For Planned Orders, we will populate transaction_id
6835 		-- in the disposition_name column to be consistent with Planning.
6836 		-- S.ORDER_NUMBER col27,
6837 		DECODE(S.ORDER_TYPE, 5, to_char(S.TRANSACTION_ID), S.ORDER_NUMBER ) col27,
6838                 l_null_num col28,
6839                 l_null_num col29,
6840 		-- ship_rec_cal changes begin
6841 		l_sysdate,
6842 		G_USER_ID,
6843 		l_sysdate,
6844 		G_USER_ID,
6845 		G_USER_ID,
6846 		-- ship_rec_cal changes end
6847 		-- Unallocated_Quantity
6848                 -1* DECODE(REQ.RESOURCE_ID, -1, REQ.LOAD_RATE,
6849                        DECODE(REQ.END_DATE, NULL, REQ.RESOURCE_HOURS,
6850                           -- Bug 3348095
6851                           DECODE(REQ.record_source, 2, REQ.RESOURCE_HOURS,
6852                                  REQ.DAILY_RESOURCE_HOURS))) *
6853                           -- For ATP created records use resource_hours
6854                           -- End Bug 3348095
6855                  ---- resource batching
6856                 DECODE(DR.UOM_CLASS_TYPE, 1, I.UNIT_WEIGHT, 2, UNIT_VOLUME) *
6857                 NVL(MUC.CONVERSION_RATE, 1) * NVL(S.NEW_ORDER_QUANTITY,S.FIRM_QUANTITY)
6858          FROM   MSC_DEPARTMENT_RESOURCES DR,
6859                 MSC_SUPPLIES S,
6860                 MSC_RESOURCE_REQUIREMENTS REQ,
6861                 -- 2859130 MSC_CALENDAR_DATES C,
6862                 MSC_SYSTEM_ITEMS I,
6863                 MSC_UOM_CONVERSIONS MUC
6864          WHERE  DR.PLAN_ID = p_plan_id
6865          AND    NVL(DR.OWNING_DEPARTMENT_ID, DR.DEPARTMENT_ID)=p_department_id
6866          AND    DR.RESOURCE_ID = p_resource_id
6867          AND    DR.SR_INSTANCE_ID = p_instance_id
6868          AND    DR.ORGANIZATION_ID = p_organization_id -- for performance
6869          AND    REQ.PLAN_ID = DR.PLAN_ID
6870          AND    REQ.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
6871          AND    REQ.RESOURCE_ID = DR.RESOURCE_ID
6872          AND    REQ.DEPARTMENT_ID = DR.DEPARTMENT_ID
6873          AND    NVL(REQ.PARENT_ID, 1) = 1 -- Bug 2809639
6874          AND     I.SR_INSTANCE_ID = REQ.SR_INSTANCE_Id
6875          AND     I.PLAN_ID = REQ.PLAN_ID
6876          AND     I.ORGANIZATION_ID = REQ.ORGANIZATION_ID
6877          AND     I.INVENTORY_ITEM_ID = REQ.ASSEMBLY_ITEM_ID
6878           -- Begin CTO Option Dependent Resources ODR
6879           AND     ((I.bom_item_type <> 1 and I.bom_item_type <> 2)
6880                -- bom_item_type not model and option_class always committed.
6881                     AND   (I.atp_flag <> 'N')
6882                -- atp_flag is 'Y' then committed.
6883                     OR    (REQ.record_source = 2) ) -- this OR may be changed during performance analysis.
6884               -- if record created by ATP then committed.
6885          -- End CTO Option Dependent Resources
6886          AND     DECODE(p_res_uom_type, 1, I.WEIGHT_UOM, 2 , I.VOLUME_UOM) = MUC.UOM_CODE (+)
6887          AND     MUC.SR_INSTANCE_ID (+)= I.SR_INSTANCE_ID
6888          AND     MUC.INVENTORY_ITEM_ID (+)= 0
6889          AND    S.PLAN_ID = DR.PLAN_ID
6890          AND    S.TRANSACTION_ID = REQ.SUPPLY_ID
6891          AND    S.SR_INSTANCE_ID = REQ.SR_INSTANCE_ID --bug3948494
6892          -- Exclude Cancelled Supplies 2460645
6893          AND     NVL(S.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
6894          -- 2859130
6895          -- AND    C.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
6896          -- AND    C.CALENDAR_CODE = l_calendar_code
6897          -- AND    C.EXCEPTION_SET_ID = l_calendar_exception_set_id
6898          -- AND    C.CALENDAR_DATE = TRUNC(REQ.START_DATE) -- Bug 2809639
6899          -- AND    C.SEQ_NUM IS NOT NULL
6900          ---bug 2341075
6901          --AND    C.CALENDAR_DATE >= trunc(sysdate)
6902          -- AND    C.CALENDAR_DATE >= l_plan_start_date
6903          AND    trunc(req.start_date) >= l_plan_start_date --4135752
6904          UNION ALL
6905          SELECT p_level col1,
6906                 p_identifier col2,
6907                 p_scenario_id col3,
6908                 l_null_num col4 ,
6909                 l_null_num col5,
6910                 p_organization_id col6,
6911                 p_department_id col7,
6912                 p_resource_id col8,
6913                 l_null_num col9,
6914                 l_null_num col10,
6915                 l_null_num col11,
6916                 l_null_num col12,
6917                 l_null_num col13,
6918                 l_null_num col14,
6919                 l_null_char col15,
6920                 l_uom_code col16,
6921                 2 col17, -- supply
6922                 l_null_num col18,
6923                 l_null_char col19,
6924                 MNRA.SR_INSTANCE_ID col20,
6925                 l_null_num col21,
6926                 MNRA.TRANSACTION_ID col22,
6927                 l_null_num col23,
6928                 MNRA.CAPACITY_UNITS  * ((DECODE(LEAST(MNRA.from_time, MNRA.to_time),
6929                        MNRA.to_time,to_time + 24*3600,
6930                        MNRA.to_time) - MNRA.from_time)/3600) *
6931                 DECODE(p_scenario_id, -1, 1, NVL((MRHM.allocation_percent/100), 1)) col24,
6932                 --bug 4156016
6933 	        /*		NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
6934                                                      p_instance_id,
6935                                                      null,
6936                                                      p_organization_id,
6937                                                      p_department_id,
6938                                                      p_resource_id,
6939                                                      l_demand_class,
6940                                                      SHIFT_DATE),1)) col24, */
6941                 MNRA.SHIFT_DATE col25,
6942                 l_null_num col26,
6943                 l_null_char col27,
6944                 l_null_num col28,
6945 		l_null_num col29,
6946 		-- ship_rec_cal changes begin
6947 		l_sysdate,
6948 		G_USER_ID,
6949 		l_sysdate,
6950 		G_USER_ID,
6951 		G_USER_ID,
6952 		-- ship_rec_cal changes end
6953                 MNRA.CAPACITY_UNITS * p_max_capacity * p_res_conversion_rate * ((DECODE(LEAST(MNRA.from_time, MNRA.to_time),
6954                        MNRA.to_time,MNRA.to_time + 24*3600,
6955                        MNRA.to_time) - MNRA.from_time)/3600)
6956           FROM    MSC_NET_RESOURCE_AVAIL MNRA,
6957                   msc_resource_hierarchy_mv MRHM
6958           WHERE   MNRA.PLAN_ID = p_plan_id
6959           AND     NVL(MNRA.PARENT_ID, -2) <> -1
6960           AND     MNRA.SR_INSTANCE_ID = p_instance_id
6961           AND     MNRA.RESOURCE_ID = p_resource_id
6962           AND     MNRA.DEPARTMENT_ID = p_department_id
6963           --bug 2341075
6964           --AND     SHIFT_DATE >= trunc(sysdate)
6965           --bug 4232627: select only those records which are after plan start date
6966           AND     MNRA.SHIFT_DATE >= l_plan_start_date
6967           --bug 4156016
6968           AND     MNRA.organization_id = p_organization_id
6969           AND     MRHM.department_id (+) = MNRA.department_id
6970           AND     MRHM.resource_id  (+)= MNRA.resource_id
6971           AND     MRHM.organization_id (+) = MNRA.organization_id
6972           AND     MRHM.sr_instance_id  (+)= MNRA.sr_instance_id
6973           --AND     MRHM.level_id (+) = -1 --4365873
6974           AND     decode(MRHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
6975           --bug 4232627: select only those records which are after plan start date
6976           AND     trunc(MNRA.shift_date) >=  trunc(MRHM.effective_date (+))
6977           AND     trunc(MNRA.shift_date) <=  trunc(MRHM.disable_date (+))
6978           AND     MRHM.demand_class (+) = l_demand_class
6979 	)
6980 	; -- dsting removed order by col25;
6981        ELSE  -- now Other plans Bug 2809639
6982          IF PG_DEBUG in ('Y', 'C') THEN
6983               msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'Other Plans Batching Details');
6984          END IF;
6985 	 INSERT INTO msc_atp_sd_details_temp (
6986 		ATP_Level,
6987 		Order_line_id,
6988 		Scenario_Id,
6989 		Inventory_Item_Id,
6990 		Request_Item_Id,
6991 		Organization_Id,
6992 		Department_Id,
6993 		Resource_Id,
6994 		Supplier_Id,
6995 		Supplier_Site_Id,
6996 		From_Organization_Id,
6997 		From_Location_Id,
6998 		To_Organization_Id,
6999 		To_Location_Id,
7000 		Ship_Method,
7001 		UOM_code,
7002 		Supply_Demand_Type,
7003 		Supply_Demand_Source_Type,
7004 		Supply_Demand_Source_Type_Name,
7005 		Identifier1,
7006 		Identifier2,
7007 		Identifier3,
7008 		Identifier4,
7009 		Supply_Demand_Quantity,
7010 		Supply_Demand_Date,
7011 		Disposition_Type,
7012 		Disposition_Name,
7013 		Pegging_Id,
7014 		End_Pegging_Id,
7015 		creation_date,
7016 		created_by,
7017 		last_update_date,
7018 		last_updated_by,
7019 		last_update_login,
7020 		Unallocated_Quantity
7021 	)
7022 
7023 	(SELECT
7024     	 	p_level col1,
7025 		p_identifier col2,
7026                 p_scenario_id col3,
7027                 l_null_num col4 ,
7028                 l_null_num col5,
7029 		p_organization_id col6,
7030                 p_department_id col7,
7031                 p_resource_id col8,
7032                 l_null_num col9,
7033                 l_null_num col10,
7034                 l_null_num col11,
7035                 l_null_num col12,
7036                 l_null_num col13,
7037                 l_null_num col14,
7038 		l_null_char col15,
7039 		l_uom_code col16,
7040 		1 col17, -- demand
7041 		S.ORDER_TYPE col18,
7042                 l_null_char col19,
7043 		REQ.SR_INSTANCE_ID col20,
7044                 l_null_num col21,
7045 		REQ.TRANSACTION_ID col22,
7046 		l_null_num col23,
7047                 -1* DECODE(REQ.RESOURCE_ID, -1, REQ.LOAD_RATE,
7048                        DECODE(REQ.END_DATE, NULL, REQ.RESOURCE_HOURS,
7049                          -- Bug 3348095
7050                          DECODE(REQ.record_source, 2, REQ.RESOURCE_HOURS,
7051                                  REQ.DAILY_RESOURCE_HOURS))) *
7052                          -- For ATP created records use resource_hours
7053                          -- End Bug 3348095
7054                  ---- resource batching
7055                 DECODE(DR.UOM_CLASS_TYPE, 1, I.UNIT_WEIGHT, 2, UNIT_VOLUME) *
7056                 NVL(MUC.CONVERSION_RATE, 1) * NVL(S.NEW_ORDER_QUANTITY,S.FIRM_QUANTITY) *
7057 
7058                         /*New*/
7059                 DECODE(p_scenario_id, -1, 1,
7060                        DECODE(DECODE(G_HIERARCHY_PROFILE,
7061                               --2424357
7062                               1, DECODE(S.DEMAND_CLASS, null, null,
7063                                      DECODE(l_demand_class, '-1',
7064                                          MSC_AATP_FUNC.Get_RES_Hierarchy_Demand_Class(
7065                                                   null,
7066                                                   null,
7067                                                   p_department_id,
7068                                                   p_resource_id,
7069                                                   p_organization_id,
7070                                                   p_instance_id,
7071                                                   c.calendar_date,
7072                                                   l_level_id,
7073                                                   S.DEMAND_CLASS), S.DEMAND_CLASS)),
7074                               2, DECODE(S.CUSTOMER_ID, NULL, TO_CHAR(NULL),
7075                                         0, TO_CHAR(NULL),
7076                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
7077                                                   S.CUSTOMER_ID,
7078                                                   S.SHIP_TO_SITE_ID,
7079                                                   s.inventory_item_id,
7080                                                   p_organization_id,
7081                                                   p_instance_id,
7082                                                   c.calendar_date,
7083                                                   l_level_id,
7084                                                   NULL))),
7085                        l_demand_class, 1,
7086                        --bug 4156016: If l_demand_class is not null and demand class is populated
7087                        -- on  supplies record then 0 should be allocated.
7088                        Decode (S.Demand_Class, NULL,
7089                        MSC_AATP_FUNC.Get_Res_Demand_Alloc_Percent(
7090                          C.CALENDAR_DATE,
7091                          REQ.ASSEMBLY_ITEM_ID,
7092                          p_organization_id,
7093                          p_instance_id,
7094                          p_department_id,
7095                          p_resource_id,
7096                          DECODE(G_HIERARCHY_PROFILE,
7097                                 ---2424357
7098                                 1, DECODE(S.DEMAND_CLASS, null, null,
7099                                       DECODE(l_demand_class, '-1',
7100                                           MSC_AATP_FUNC.Get_RES_Hierarchy_Demand_Class(
7101                                                   null,
7102                                                   null,
7103                                                   p_department_id,
7104                                                   p_resource_id,
7105                                                   p_organization_id,
7106                                                   p_instance_id,
7107                                                   c.calendar_date,
7108                                                   l_level_id,
7109                                                    S.DEMAND_CLASS), S.DEMAND_CLASS)),
7110                                 2, DECODE(S.CUSTOMER_ID, NULL, l_demand_class,
7111                                           0, l_demand_class,
7112                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
7113                                                   S.CUSTOMER_ID,
7114                                                   S.SHIP_TO_SITE_ID,
7115                                                   l_inv_item_id,
7116                                                   p_organization_id,
7117                                                   p_instance_id,
7118                                                   c.calendar_date,
7119                                                   l_level_id,
7120                                                   NULL))),
7121                          l_demand_class), 0))) col24,
7122                         /*New*/
7123 		GREATEST(C.CALENDAR_DATE,l_sys_next_date) col25, --bug3333114
7124                 l_null_num col26,
7125                 -- Bug 2771075. For Planned Orders, we will populate transaction_id
7126 		-- in the disposition_name column to be consistent with Planning.
7127 		-- S.ORDER_NUMBER col27,
7128 		DECODE(S.ORDER_TYPE, 5, to_char(S.TRANSACTION_ID), S.ORDER_NUMBER ) col27,
7129                 l_null_num col28,
7130                 l_null_num col29,
7131 		-- ship_rec_cal changes begin
7132 		l_sysdate,
7133 		G_USER_ID,
7134 		l_sysdate,
7135 		G_USER_ID,
7136 		G_USER_ID,
7137 		-- ship_rec_cal changes end
7138 		-- Unallocated_Quantity
7139                 -1* DECODE(REQ.RESOURCE_ID, -1, REQ.LOAD_RATE,
7140                        DECODE(REQ.END_DATE, NULL, REQ.RESOURCE_HOURS,
7141                           -- Bug 3348095
7142                           DECODE(REQ.record_source, 2, REQ.RESOURCE_HOURS,
7143                                    REQ.DAILY_RESOURCE_HOURS))) *
7144                           -- For ATP created records use resource_hours
7145                           -- End Bug 3348095
7146                  ---- resource batching
7147                 DECODE(DR.UOM_CLASS_TYPE, 1, I.UNIT_WEIGHT, 2, UNIT_VOLUME) *
7148                 NVL(MUC.CONVERSION_RATE, 1) * NVL(S.NEW_ORDER_QUANTITY,S.FIRM_QUANTITY)
7149          FROM   MSC_DEPARTMENT_RESOURCES DR,
7150                 MSC_SUPPLIES S,
7151                 MSC_RESOURCE_REQUIREMENTS REQ,
7152                 MSC_CALENDAR_DATES C,
7153                 MSC_SYSTEM_ITEMS I,
7154                 MSC_UOM_CONVERSIONS MUC
7155          WHERE  DR.PLAN_ID = p_plan_id
7156          AND    NVL(DR.OWNING_DEPARTMENT_ID, DR.DEPARTMENT_ID)=p_department_id
7157          AND    DR.RESOURCE_ID = p_resource_id
7158          AND    DR.SR_INSTANCE_ID = p_instance_id
7159          AND    DR.ORGANIZATION_ID = p_organization_id -- for performance
7160          AND    REQ.PLAN_ID = DR.PLAN_ID
7161          AND    REQ.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
7162          AND    REQ.RESOURCE_ID = DR.RESOURCE_ID
7163          AND    REQ.DEPARTMENT_ID = DR.DEPARTMENT_ID
7164          AND    NVL(REQ.PARENT_ID, l_optimized_plan) = l_optimized_plan
7165          AND     I.SR_INSTANCE_ID = REQ.SR_INSTANCE_Id
7166          AND     I.PLAN_ID = REQ.PLAN_ID
7167          AND     I.ORGANIZATION_ID = REQ.ORGANIZATION_ID
7168          AND     I.INVENTORY_ITEM_ID = REQ.ASSEMBLY_ITEM_ID
7169           -- Begin CTO Option Dependent Resources ODR
7170           AND     ((I.bom_item_type <> 1 and I.bom_item_type <> 2)
7171                -- bom_item_type not model and option_class always committed.
7172                     AND   (I.atp_flag <> 'N')
7173                -- atp_flag is 'Y' then committed.
7174                     OR    (REQ.record_source = 2) ) -- this OR may be changed during performance analysis.
7175               -- if record created by ATP then committed.
7176          -- End CTO Option Dependent Resources
7177          AND     DECODE(p_res_uom_type, 1, I.WEIGHT_UOM, 2 , I.VOLUME_UOM) = MUC.UOM_CODE (+)
7178          AND     MUC.SR_INSTANCE_ID (+)= I.SR_INSTANCE_ID
7179          AND     MUC.INVENTORY_ITEM_ID (+)= 0
7180          AND    S.PLAN_ID = DR.PLAN_ID
7181          AND    S.TRANSACTION_ID = REQ.SUPPLY_ID
7182          AND    S.SR_INSTANCE_ID = REQ.SR_INSTANCE_ID --bug3948494
7183          -- Exclude Cancelled Supplies 2460645
7184          AND     NVL(S.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
7185          AND    C.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
7186          AND    C.CALENDAR_CODE = l_calendar_code
7187          AND    C.EXCEPTION_SET_ID = l_calendar_exception_set_id
7188                 -- Bug 3348095
7189                 -- Ensure that the ATP created resource Reqs
7190                 -- do not get double counted.
7191          AND     C.CALENDAR_DATE BETWEEN DECODE(REQ.record_source, 2,
7192                           TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)), TRUNC(REQ.START_DATE))
7193                    AND TRUNC(NVL(REQ.END_DATE, REQ.START_DATE))
7194                 -- End Bug 3348095
7195          AND    C.SEQ_NUM IS NOT NULL
7196          ---bug 2341075
7197          --AND    C.CALENDAR_DATE >= trunc(sysdate)
7198          AND    C.CALENDAR_DATE >= l_plan_start_date
7199          UNION ALL
7200          SELECT p_level col1,
7201                 p_identifier col2,
7202                 p_scenario_id col3,
7203                 l_null_num col4 ,
7204                 l_null_num col5,
7205                 p_organization_id col6,
7206                 p_department_id col7,
7207                 p_resource_id col8,
7208                 l_null_num col9,
7209                 l_null_num col10,
7210                 l_null_num col11,
7211                 l_null_num col12,
7212                 l_null_num col13,
7213                 l_null_num col14,
7214                 l_null_char col15,
7215                 l_uom_code col16,
7216                 2 col17, -- supply
7217                 l_null_num col18,
7218                 l_null_char col19,
7219                 MNRA.SR_INSTANCE_ID col20,
7220                 l_null_num col21,
7221                 MNRA.TRANSACTION_ID col22,
7222                 l_null_num col23,
7223                 MNRA.CAPACITY_UNITS  * ((DECODE(LEAST(MNRA.from_time, MNRA.to_time),
7224                        MNRA.to_time,to_time + 24*3600,
7225                        MNRA.to_time) - MNRA.from_time)/3600) *
7226                 DECODE(p_scenario_id, -1, 1, NVL((MRHM.allocation_percent/100), 1)) col24,
7227                 --bug 4156016
7228 	        /*		NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
7229                                                      p_instance_id,
7230                                                      null,
7231                                                      p_organization_id,
7232                                                      p_department_id,
7233                                                      p_resource_id,
7234                                                      l_demand_class,
7235                                                      SHIFT_DATE),1)) col24, */
7236                 MNRA.SHIFT_DATE col25,
7237                 l_null_num col26,
7238                 l_null_char col27,
7239                 l_null_num col28,
7240 		l_null_num col29,
7241 		-- ship_rec_cal changes begin
7242 		l_sysdate,
7243 		G_USER_ID,
7244 		l_sysdate,
7245 		G_USER_ID,
7246 		G_USER_ID,
7247 		-- ship_rec_cal changes end
7248                 MNRA.CAPACITY_UNITS * p_max_capacity * p_res_conversion_rate * ((DECODE(LEAST(MNRA.from_time, MNRA.to_time),
7249                        MNRA.to_time,MNRA.to_time + 24*3600,
7250                        MNRA.to_time) - MNRA.from_time)/3600)
7251           FROM    MSC_NET_RESOURCE_AVAIL MNRA,
7252                   msc_resource_hierarchy_mv MRHM
7253           WHERE   MNRA.PLAN_ID = p_plan_id
7254           AND     NVL(MNRA.PARENT_ID, -2) <> -1
7255           AND     MNRA.SR_INSTANCE_ID = p_instance_id
7256           AND     MNRA.RESOURCE_ID = p_resource_id
7257           AND     MNRA.DEPARTMENT_ID = p_department_id
7258           --bug 2341075
7259           --AND     SHIFT_DATE >= trunc(sysdate)
7260           --bug 4232627: select only those records which are after plan start date
7261           AND     MNRA.SHIFT_DATE >= l_plan_start_date
7262           --bug 4156016
7263           AND     MNRA.organization_id = p_organization_id
7264           AND     MRHM.department_id (+) = MNRA.department_id
7265           AND     MRHM.resource_id  (+)= MNRA.resource_id
7266           AND     MRHM.organization_id (+) = MNRA.organization_id
7267           AND     MRHM.sr_instance_id  (+)= MNRA.sr_instance_id
7268           --AND     MRHM.level_id (+) = -1 --4365873
7269           AND     decode(MRHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
7270           --bug 4232627:
7271           AND     trunc(MNRA.shift_date) >=  trunc(MRHM.effective_date (+))
7272           AND     trunc(MNRA.shift_date) <=  trunc(MRHM.disable_date (+))
7273           AND     MRHM.demand_class (+) = l_demand_class
7274 	)
7275 	; -- dsting removed order by col25;
7276        END IF; -- l_optimized_plan = 1 Bug 2809639
7277      ELSE
7278 
7279        IF (l_optimized_plan = 1) THEN -- Constrained Plan Bug 2809639
7280          IF PG_DEBUG in ('Y', 'C') THEN
7281               msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'Constrained Plan No Batching Details');
7282          END IF;
7283 	 INSERT INTO msc_atp_sd_details_temp (
7284 		ATP_Level,
7285 		Order_line_id,
7286 		Scenario_Id,
7287 		Inventory_Item_Id,
7288 		Request_Item_Id,
7289 		Organization_Id,
7290 		Department_Id,
7291 		Resource_Id,
7292 		Supplier_Id,
7293 		Supplier_Site_Id,
7294 		From_Organization_Id,
7295 		From_Location_Id,
7296 		To_Organization_Id,
7297 		To_Location_Id,
7298 		Ship_Method,
7299 		UOM_code,
7300 		Supply_Demand_Type,
7301 		Supply_Demand_Source_Type,
7302 		Supply_Demand_Source_Type_Name,
7303 		Identifier1,
7304 		Identifier2,
7305 		Identifier3,
7306 		Identifier4,
7307 		Supply_Demand_Quantity,
7308 		Supply_Demand_Date,
7309 		Disposition_Type,
7310 		Disposition_Name,
7311 		Pegging_Id,
7312 		End_Pegging_Id,
7313 		creation_date,
7314 		created_by,
7315 		last_update_date,
7316 		last_updated_by,
7317 		last_update_login,
7318 		Unallocated_Quantity
7319 	)
7320     	(SELECT
7321 		p_level col1,
7322 		p_identifier col2,
7323                 p_scenario_id col3,
7324                 l_null_num col4 ,
7325                 l_null_num col5,
7326 		p_organization_id col6,
7327                 p_department_id col7,
7328                 p_resource_id col8,
7329                 l_null_num col9,
7330                 l_null_num col10,
7331                 l_null_num col11,
7332                 l_null_num col12,
7333                 l_null_num col13,
7334                 l_null_num col14,
7335 		l_null_char col15,
7336 		l_uom_code col16,
7337 		1 col17, -- demand
7338 		S.ORDER_TYPE col18,
7339                 l_null_char col19,
7340 		REQ.SR_INSTANCE_ID col20,
7341                 l_null_num col21,
7342 		REQ.TRANSACTION_ID col22,
7343 		l_null_num col23,
7344                 -1* DECODE(REQ.RESOURCE_ID, -1, REQ.LOAD_RATE,
7345                        DECODE(REQ.END_DATE, NULL, REQ.RESOURCE_HOURS,
7346                          -- Bug 3348095
7347                          DECODE(REQ.record_source, 2, REQ.RESOURCE_HOURS,
7348                                  REQ.DAILY_RESOURCE_HOURS))) *
7349                          -- For ATP created records use resource_hours
7350                          -- End Bug 3348095
7351                         /*New*/
7352                 DECODE(p_scenario_id, -1, 1,
7353                        DECODE(DECODE(G_HIERARCHY_PROFILE,
7354                               --2424357
7355                               1, DECODE(S.DEMAND_CLASS, null, null,
7356                                     DECODE(l_demand_class, '-1',
7357                                        MSC_AATP_FUNC.Get_RES_Hierarchy_Demand_Class(
7358                                                   null,
7359                                                   null,
7360                                                   p_department_id,
7361                                                   p_resource_id,
7362                                                   p_organization_id,
7363                                                   p_instance_id,
7364                                                   -- 2859130 c.calendar_date,
7365                                                   -- Bug 3348095
7366                                                   -- For ATP created records use end_date
7367                                                   -- otherwise start_date
7368                                                   DECODE(REQ.record_source, 2,
7369                                                     TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
7370                                                       TRUNC(REQ.START_DATE)),
7371                                                   --trunc(req.start_date),
7372                                                   -- End Bug 3348095
7373                                                   l_level_id,
7374                                                   S.DEMAND_CLASS), S.DEMAND_CLASS)),
7375                               2, DECODE(S.CUSTOMER_ID, NULL, TO_CHAR(NULL),
7376                                         0, TO_CHAR(NULL),
7377                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
7378                                                   S.CUSTOMER_ID,
7379                                                   S.SHIP_TO_SITE_ID,
7380                                                   s.inventory_item_id,
7381                                                   p_organization_id,
7382                                                   p_instance_id,
7383                                                   -- 2859130 c.calendar_date,
7384                                                   -- Bug 3348095
7385                                                   -- For ATP created records use end_date
7386                                                   -- otherwise start_date
7387                                                   DECODE(REQ.record_source, 2,
7388                                                     TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
7389                                                       TRUNC(REQ.START_DATE)),
7390                                                   --trunc(req.start_date),
7391                                                   -- End Bug 3348095
7392                                                   l_level_id,
7393                                                   NULL))),
7394                        l_demand_class, 1,
7395                        --bug 4156016: If l_demand_class is not null and demand class is populated
7396                        -- on  supplies record then 0 should be allocated.
7397                        Decode (S.Demand_Class, NULL,
7398                        MSC_AATP_FUNC.Get_Res_Demand_Alloc_Percent(
7399                          -- 2859130 C.CALENDAR_DATE,
7400                          -- Bug 3348095
7401                          -- For ATP created records use end_date
7402                          -- otherwise start_date
7403                          DECODE(REQ.record_source, 2,
7404                             TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
7405                                 TRUNC(REQ.START_DATE)),
7406                          --trunc(req.start_date),
7407                          -- End Bug 3348095
7408                          REQ.ASSEMBLY_ITEM_ID,
7409                          p_organization_id,
7410                          p_instance_id,
7411                          p_department_id,
7412                          p_resource_id,
7413                          DECODE(G_HIERARCHY_PROFILE,
7414                                 --2424357
7415                                 1, DECODE(S.DEMAND_CLASS, null, null,
7416                                      DECODE(l_demand_class, '-1',
7417                                          MSC_AATP_FUNC.Get_RES_Hierarchy_Demand_Class(
7418                                                   null,
7419                                                   null,
7420                                                   p_department_id,
7421                                                   p_resource_id,
7422                                                   p_organization_id,
7423                                                   p_instance_id,
7424                                                   -- 2859130 c.calendar_date,
7425                                                   -- Bug 3348095
7426                                                   -- For ATP created records use end_date
7427                                                   -- otherwise start_date
7428                                                   DECODE(REQ.record_source, 2,
7429                                                     TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
7430                                                       TRUNC(REQ.START_DATE)),
7431                                                   --trunc(req.start_date),
7432                                                   -- End Bug 3348095
7433                                                   l_level_id,
7434                                                   S.DEMAND_CLASS), S.DEMAND_CLASS)),
7435                                 2, DECODE(S.CUSTOMER_ID, NULL, l_demand_class,
7436                                           0, l_demand_class,
7437                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
7438                                                   S.CUSTOMER_ID,
7439                                                   S.SHIP_TO_SITE_ID,
7440                                                   l_inv_item_id,
7441                                                   p_organization_id,
7442                                                   p_instance_id,
7443                                                   --2859130 c.calendar_date,
7444                                                   -- Bug 3348095
7445                                                   -- For ATP created records use end_date
7446                                                   -- otherwise start_date
7447                                                   DECODE(REQ.record_source, 2,
7448                                                     TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
7449                                                       TRUNC(REQ.START_DATE)),
7450                                                   --trunc(req.start_date),
7451                                                   -- End Bug 3348095
7452                                                   l_level_id,
7453                                                   NULL))),
7454                          l_demand_class), 0))) col24,
7455                         /*New*/
7456 		-- 2859130 C.CALENDAR_DATE col25,
7457                 -- Bug 3348095
7458                 -- For ATP created records use end_date otherwise start_date
7459                 GREATEST(DECODE(REQ.record_source, 2,
7460                    TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) , TRUNC(REQ.START_DATE)),l_sys_next_date) col25,  --bug3333114
7461                 -- End Bug 3348095
7462                 -- TRUNC(req.start_date) col25,
7463                 l_null_num col26,
7464 		-- Bug 2771075. For Planned Orders, we will populate transaction_id
7465 		-- in the disposition_name column to be consistent with Planning.
7466 		-- S.ORDER_NUMBER col27,
7467 		DECODE(S.ORDER_TYPE, 5, to_char(S.TRANSACTION_ID), S.ORDER_NUMBER) col27,
7468                 l_null_num col28,
7469                 l_null_num col29,
7470 		-- ship_rec_cal changes begin
7471 		l_sysdate,
7472 		G_USER_ID,
7473 		l_sysdate,
7474 		G_USER_ID,
7475 		G_USER_ID,
7476 		-- ship_rec_cal changes end
7477 		-- Unallocated_Quantity
7478                 -1* DECODE(REQ.RESOURCE_ID, -1, REQ.LOAD_RATE,
7479                        DECODE(REQ.END_DATE, NULL, REQ.RESOURCE_HOURS,
7480                           -- Bug 3348095
7481                           DECODE(REQ.record_source, 2, REQ.RESOURCE_HOURS,
7482                                  REQ.DAILY_RESOURCE_HOURS)))
7483                           -- For ATP created records use resource_hours
7484                           -- End Bug 3348095
7485          FROM   MSC_DEPARTMENT_RESOURCES DR,
7486                 MSC_SUPPLIES S,
7487                 MSC_SYSTEM_ITEMS I,  -- CTO ODR
7488                 MSC_RESOURCE_REQUIREMENTS REQ
7489                 -- 2859130 MSC_CALENDAR_DATES C
7490          WHERE  DR.PLAN_ID = p_plan_id
7491          AND    NVL(DR.OWNING_DEPARTMENT_ID, DR.DEPARTMENT_ID)=p_department_id
7492          AND    DR.RESOURCE_ID = p_resource_id
7493          AND    DR.SR_INSTANCE_ID = p_instance_id
7494          AND    DR.ORGANIZATION_ID = p_organization_id -- for performance
7495          AND    REQ.PLAN_ID = DR.PLAN_ID
7496          AND    REQ.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
7497          AND    REQ.RESOURCE_ID = DR.RESOURCE_ID
7498          AND    REQ.DEPARTMENT_ID = DR.DEPARTMENT_ID
7499          AND    NVL(REQ.PARENT_ID, 1) = 1 -- parent_id is 1 for constrained plans. Bug 2809639
7500          -- CTO Option Dependent Resources ODR
7501          AND     I.SR_INSTANCE_ID = REQ.SR_INSTANCE_Id
7502          AND     I.PLAN_ID = REQ.PLAN_ID
7503          AND     I.ORGANIZATION_ID = REQ.ORGANIZATION_ID
7504          AND     I.INVENTORY_ITEM_ID = REQ.ASSEMBLY_ITEM_ID
7505          AND     ((I.bom_item_type <> 1 and I.bom_item_type <> 2)
7506                -- bom_item_type not model and option_class always committed.
7507                     AND   (I.atp_flag <> 'N')
7508                -- atp_flag is 'Y' then committed.
7509                     OR    (REQ.record_source = 2) ) -- this OR may be changed during performance analysis.
7510               -- if record created by ATP then committed.
7511          -- End CTO Option Dependent Resources
7512          AND    S.PLAN_ID = DR.PLAN_ID
7513          AND    S.TRANSACTION_ID = REQ.SUPPLY_ID
7514          AND    S.SR_INSTANCE_ID = REQ.SR_INSTANCE_ID --bug3948494
7515                 -- Exclude Cancelled Supplies 2460645
7516          AND     NVL(S.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
7517          -- 2859130
7518          -- AND    C.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
7519          -- AND    C.CALENDAR_CODE = l_calendar_code
7520          -- AND    C.EXCEPTION_SET_ID = l_calendar_exception_set_id
7521          -- AND    C.CALENDAR_DATE = TRUNC(REQ.START_DATE) -- Bug 2809639
7522          -- AND    C.SEQ_NUM IS NOT NULL
7523          ---bug 2341075
7524          --AND    C.CALENDAR_DATE >= trunc(sysdate)
7525          -- AND    C.CALENDAR_DATE >= l_plan_start_date
7526          AND    trunc(req.start_date) >= l_plan_start_date --4135752
7527          UNION ALL
7528          SELECT p_level col1,
7529                 p_identifier col2,
7530                 p_scenario_id col3,
7531                 l_null_num col4 ,
7532                 l_null_num col5,
7533                 p_organization_id col6,
7534                 p_department_id col7,
7535                 p_resource_id col8,
7536                 l_null_num col9,
7537                 l_null_num col10,
7538                 l_null_num col11,
7539                 l_null_num col12,
7540                 l_null_num col13,
7541                 l_null_num col14,
7542                 l_null_char col15,
7543                 l_uom_code col16,
7544                 2 col17, -- supply
7545                 l_null_num col18,
7546                 l_null_char col19,
7547                 MNRA.SR_INSTANCE_ID col20,
7548                 l_null_num col21,
7549                 MNRA.TRANSACTION_ID col22,
7550                 l_null_num col23,
7551                 MNRA.CAPACITY_UNITS  * ((DECODE(LEAST(MNRA.from_time, MNRA.to_time),
7552                        MNRA.to_time,to_time + 24*3600,
7553                        MNRA.to_time) - MNRA.from_time)/3600) *
7554                 DECODE(p_scenario_id, -1, 1, NVL((MRHM.allocation_percent/100), 1)) col24,
7555                 --bug 4156016
7556 	        /*		NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
7557                                                      p_instance_id,
7558                                                      null,
7559                                                      p_organization_id,
7560                                                      p_department_id,
7561                                                      p_resource_id,
7562                                                      l_demand_class,
7563                                                      SHIFT_DATE),1)) col24, */
7564                 MNRA.SHIFT_DATE col25,
7565                 l_null_num col26,
7566                 l_null_char col27,
7567                 l_null_num col28,
7568 		l_null_num col29,
7569 		-- ship_rec_cal changes begin
7570 		l_sysdate,
7571 		G_USER_ID,
7572 		l_sysdate,
7573 		G_USER_ID,
7574 		G_USER_ID,
7575 		-- ship_rec_cal changes end
7576 		-- Unallocated_Quantity
7577                 MNRA.CAPACITY_UNITS  * ((DECODE(LEAST(MNRA.from_time, MNRA.to_time),
7578                        MNRA.to_time,MNRA.to_time + 24*3600,
7579                        MNRA.to_time) - MNRA.from_time)/3600)
7580           FROM    MSC_NET_RESOURCE_AVAIL MNRA,
7581                   msc_resource_hierarchy_mv MRHM
7582           WHERE   MNRA.PLAN_ID = p_plan_id
7583           AND     NVL(MNRA.PARENT_ID, -2) <> -1
7584           AND     MNRA.SR_INSTANCE_ID = p_instance_id
7585           AND     MNRA.RESOURCE_ID = p_resource_id
7586           AND     MNRA.DEPARTMENT_ID = p_department_id
7587           --bug 2341075
7588           --AND     SHIFT_DATE >= trunc(sysdate)
7589           --bug 4232627: select only those records which are after plan start date
7590           AND     MNRA.SHIFT_DATE >= l_plan_start_date
7591           --bug 4156016
7592           AND     MNRA.organization_id = p_organization_id
7593           AND     MRHM.department_id (+) = MNRA.department_id
7594           AND     MRHM.resource_id  (+)= MNRA.resource_id
7595           AND     MRHM.organization_id (+) = MNRA.organization_id
7596           AND     MRHM.sr_instance_id  (+)= MNRA.sr_instance_id
7597           --AND     MRHM.level_id (+) = -1 --4365873
7598           AND     decode(MRHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
7599           --bug 4232627:
7600           AND     trunc(MNRA.shift_date) >=  trunc(MRHM.effective_date (+))
7601           AND     trunc(MNRA.shift_date) <=  trunc(MRHM.disable_date (+))
7602           AND     MRHM.demand_class (+) = l_demand_class
7603 	)
7604 	; -- dsting removed order by col25;
7605        ELSE  -- now Other plans Bug 2809639
7606          IF PG_DEBUG in ('Y', 'C') THEN
7607               msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'Other Plans No Batching Details');
7608          END IF;
7609 	 INSERT INTO msc_atp_sd_details_temp (
7610 		ATP_Level,
7611 		Order_line_id,
7612 		Scenario_Id,
7613 		Inventory_Item_Id,
7614 		Request_Item_Id,
7615 		Organization_Id,
7616 		Department_Id,
7617 		Resource_Id,
7618 		Supplier_Id,
7619 		Supplier_Site_Id,
7620 		From_Organization_Id,
7621 		From_Location_Id,
7622 		To_Organization_Id,
7623 		To_Location_Id,
7624 		Ship_Method,
7625 		UOM_code,
7626 		Supply_Demand_Type,
7627 		Supply_Demand_Source_Type,
7628 		Supply_Demand_Source_Type_Name,
7629 		Identifier1,
7630 		Identifier2,
7631 		Identifier3,
7632 		Identifier4,
7633 		Supply_Demand_Quantity,
7634 		Supply_Demand_Date,
7635 		Disposition_Type,
7636 		Disposition_Name,
7637 		Pegging_Id,
7638 		End_Pegging_Id,
7639 		creation_date,
7640 		created_by,
7641 		last_update_date,
7642 		last_updated_by,
7643 		last_update_login,
7644 		Unallocated_Quantity
7645 	)
7646     	(SELECT
7647 		p_level col1,
7648 		p_identifier col2,
7649                 p_scenario_id col3,
7650                 l_null_num col4 ,
7651                 l_null_num col5,
7652 		p_organization_id col6,
7653                 p_department_id col7,
7654                 p_resource_id col8,
7655                 l_null_num col9,
7656                 l_null_num col10,
7657                 l_null_num col11,
7658                 l_null_num col12,
7659                 l_null_num col13,
7660                 l_null_num col14,
7661 		l_null_char col15,
7662 		l_uom_code col16,
7663 		1 col17, -- demand
7664 		S.ORDER_TYPE col18,
7665                 l_null_char col19,
7666 		REQ.SR_INSTANCE_ID col20,
7667                 l_null_num col21,
7668 		REQ.TRANSACTION_ID col22,
7669 		l_null_num col23,
7670                 -1* DECODE(REQ.RESOURCE_ID, -1, REQ.LOAD_RATE,
7671                        DECODE(REQ.END_DATE, NULL, REQ.RESOURCE_HOURS,
7672                           -- Bug 3348095
7673                           DECODE(REQ.record_source, 2, REQ.RESOURCE_HOURS,
7674                                    REQ.DAILY_RESOURCE_HOURS))) *
7675                           -- For ATP created records use resource_hours
7676                           -- End Bug 3348095
7677                         /*New*/
7678                 DECODE(p_scenario_id, -1, 1,
7679                        DECODE(DECODE(G_HIERARCHY_PROFILE,
7680                               --2424357
7681                               1, DECODE(S.DEMAND_CLASS, null, null,
7682                                     DECODE(l_demand_class, '-1',
7683                                        MSC_AATP_FUNC.Get_RES_Hierarchy_Demand_Class(
7684                                                   null,
7685                                                   null,
7686                                                   p_department_id,
7687                                                   p_resource_id,
7688                                                   p_organization_id,
7689                                                   p_instance_id,
7690                                                   c.calendar_date,
7691                                                   l_level_id,
7692                                                   S.DEMAND_CLASS), S.DEMAND_CLASS)),
7693                               2, DECODE(S.CUSTOMER_ID, NULL, TO_CHAR(NULL),
7694                                         0, TO_CHAR(NULL),
7695                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
7696                                                   S.CUSTOMER_ID,
7697                                                   S.SHIP_TO_SITE_ID,
7698                                                   s.inventory_item_id,
7699                                                   p_organization_id,
7700                                                   p_instance_id,
7701                                                   c.calendar_date,
7702                                                   l_level_id,
7703                                                   NULL))),
7704                        l_demand_class, 1,
7705                        --bug 4156016: If l_demand_class is not null and demand class is populated
7706                        -- on  supplies record then 0 should be allocated.
7707                        Decode (S.Demand_Class, NULL,
7708                        MSC_AATP_FUNC.Get_Res_Demand_Alloc_Percent(C.CALENDAR_DATE,
7709                          REQ.ASSEMBLY_ITEM_ID,
7710                          p_organization_id,
7711                          p_instance_id,
7712                          p_department_id,
7713                          p_resource_id,
7714                          DECODE(G_HIERARCHY_PROFILE,
7715                                 --2424357
7716                                 1, DECODE(S.DEMAND_CLASS, null, null,
7717                                      DECODE(l_demand_class, '-1',
7718                                          MSC_AATP_FUNC.Get_RES_Hierarchy_Demand_Class(
7719                                                   null,
7720                                                   null,
7721                                                   p_department_id,
7722                                                   p_resource_id,
7723                                                   p_organization_id,
7724                                                   p_instance_id,
7725                                                   c.calendar_date,
7726                                                   l_level_id,
7727                                                   S.DEMAND_CLASS), S.DEMAND_CLASS)),
7728                                 2, DECODE(S.CUSTOMER_ID, NULL, l_demand_class,
7729                                           0, l_demand_class,
7730                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
7731                                                   S.CUSTOMER_ID,
7732                                                   S.SHIP_TO_SITE_ID,
7733                                                   l_inv_item_id,
7734                                                   p_organization_id,
7735                                                   p_instance_id,
7736                                                   c.calendar_date,
7737                                                   l_level_id,
7738                                                   NULL))),
7739                          l_demand_class), 0))) col24,
7740                         /*New*/
7741 		GREATEST(C.CALENDAR_DATE,l_sys_next_date) col25, --bug3333114
7742                 l_null_num col26,
7743 		-- Bug 2771075. For Planned Orders, we will populate transaction_id
7744 		-- in the disposition_name column to be consistent with Planning.
7745 		-- S.ORDER_NUMBER col27,
7746 		DECODE(S.ORDER_TYPE, 5, to_char(S.TRANSACTION_ID), S.ORDER_NUMBER) col27,
7747                 l_null_num col28,
7748                 l_null_num col29,
7749 		-- ship_rec_cal changes begin
7750 		l_sysdate,
7751 		G_USER_ID,
7752 		l_sysdate,
7753 		G_USER_ID,
7754 		G_USER_ID,
7755 		-- ship_rec_cal changes end
7756 		-- Unallocated_Quantity
7757                 -1* DECODE(REQ.RESOURCE_ID, -1, REQ.LOAD_RATE,
7758                        DECODE(REQ.END_DATE, NULL, REQ.RESOURCE_HOURS,
7759                           -- Bug 3348095
7760                           DECODE(REQ.record_source, 2, REQ.RESOURCE_HOURS,
7761                                    REQ.DAILY_RESOURCE_HOURS)))
7762                           -- For ATP created records use resource_hours
7763                           -- End Bug 3348095
7764          FROM   MSC_DEPARTMENT_RESOURCES DR,
7765                 MSC_SUPPLIES S,
7766                 MSC_SYSTEM_ITEMS I, -- CTO ODR
7767                 MSC_RESOURCE_REQUIREMENTS REQ,
7768                 MSC_CALENDAR_DATES C
7769          WHERE  DR.PLAN_ID = p_plan_id
7770          AND    NVL(DR.OWNING_DEPARTMENT_ID, DR.DEPARTMENT_ID)=p_department_id
7771          AND    DR.RESOURCE_ID = p_resource_id
7772          AND    DR.SR_INSTANCE_ID = p_instance_id
7773          AND    DR.ORGANIZATION_ID = p_organization_id -- for performance
7774          AND    REQ.PLAN_ID = DR.PLAN_ID
7775          AND    REQ.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
7776          AND    REQ.RESOURCE_ID = DR.RESOURCE_ID
7777          AND    REQ.DEPARTMENT_ID = DR.DEPARTMENT_ID
7778          AND    NVL(REQ.PARENT_ID, l_optimized_plan) = l_optimized_plan
7779          -- CTO Option Dependent Resources ODR
7780          AND     I.SR_INSTANCE_ID = REQ.SR_INSTANCE_Id
7781          AND     I.PLAN_ID = REQ.PLAN_ID
7782          AND     I.ORGANIZATION_ID = REQ.ORGANIZATION_ID
7783          AND     I.INVENTORY_ITEM_ID = REQ.ASSEMBLY_ITEM_ID
7784          AND     ((I.bom_item_type <> 1 and I.bom_item_type <> 2)
7785                -- bom_item_type not model and option_class always committed.
7786                     AND   (I.atp_flag <> 'N')
7787                -- atp_flag is 'Y' then committed.
7788                     OR    (REQ.record_source = 2) ) -- this OR may be changed during performance analysis.
7789               -- if record created by ATP then committed.
7790          -- End CTO Option Dependent Resources
7791          AND    S.PLAN_ID = DR.PLAN_ID
7792          AND    S.TRANSACTION_ID = REQ.SUPPLY_ID
7793          AND    S.SR_INSTANCE_ID = REQ.SR_INSTANCE_ID --bug3948494
7794                 -- Exclude Cancelled Supplies 2460645
7795          AND     NVL(S.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
7796          AND    C.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
7797          AND    C.CALENDAR_CODE = l_calendar_code
7798          AND    C.EXCEPTION_SET_ID = l_calendar_exception_set_id
7799                 -- Bug 3348095
7800                 -- Ensure that the ATP created resource Reqs
7801                 -- do not get double counted.
7802          AND     C.CALENDAR_DATE BETWEEN DECODE(REQ.record_source, 2,
7803                           TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)), TRUNC(REQ.START_DATE))
7804                    AND TRUNC(NVL(REQ.END_DATE, REQ.START_DATE))
7805                 -- End Bug 3348095
7806          AND    C.SEQ_NUM IS NOT NULL
7807          ---bug 2341075
7808          --AND    C.CALENDAR_DATE >= trunc(sysdate)
7809          AND    C.CALENDAR_DATE >= l_plan_start_date
7810          UNION ALL
7811          SELECT p_level col1,
7812                 p_identifier col2,
7813                 p_scenario_id col3,
7814                 l_null_num col4 ,
7815                 l_null_num col5,
7816                 p_organization_id col6,
7817                 p_department_id col7,
7818                 p_resource_id col8,
7819                 l_null_num col9,
7820                 l_null_num col10,
7821                 l_null_num col11,
7822                 l_null_num col12,
7823                 l_null_num col13,
7824                 l_null_num col14,
7825                 l_null_char col15,
7826                 l_uom_code col16,
7827                 2 col17, -- supply
7828                 l_null_num col18,
7829                 l_null_char col19,
7830                 MNRA.SR_INSTANCE_ID col20,
7831                 l_null_num col21,
7832                 MNRA.TRANSACTION_ID col22,
7833                 l_null_num col23,
7834                 MNRA.CAPACITY_UNITS  * ((DECODE(LEAST(MNRA.from_time, MNRA.to_time),
7835                        MNRA.to_time,to_time + 24*3600,
7836                        MNRA.to_time) - MNRA.from_time)/3600) *
7837                 DECODE(p_scenario_id, -1, 1, NVL((MRHM.allocation_percent/100), 1)) col24,
7838                 --bug 4156016
7839 	        /*		NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
7840                                                      p_instance_id,
7841                                                      null,
7842                                                      p_organization_id,
7843                                                      p_department_id,
7844                                                      p_resource_id,
7845                                                      l_demand_class,
7846                                                      SHIFT_DATE),1)) col24, */
7847                 MNRA.SHIFT_DATE col25,
7848                 l_null_num col26,
7849                 l_null_char col27,
7850                 l_null_num col28,
7851 		l_null_num col29,
7852 		-- ship_rec_cal changes begin
7853 		l_sysdate,
7854 		G_USER_ID,
7855 		l_sysdate,
7856 		G_USER_ID,
7857 		G_USER_ID,
7858 		-- ship_rec_cal changes end
7859 		-- Unallocated_Quantity
7860                 MNRA.CAPACITY_UNITS  * ((DECODE(LEAST(MNRA.from_time, MNRA.to_time),
7861                        MNRA.to_time,MNRA.to_time + 24*3600,
7862                        MNRA.to_time) - MNRA.from_time)/3600)
7863           FROM    MSC_NET_RESOURCE_AVAIL MNRA,
7864                   msc_resource_hierarchy_mv MRHM
7865           WHERE   MNRA.PLAN_ID = p_plan_id
7866           AND     NVL(MNRA.PARENT_ID, -2) <> -1
7867           AND     MNRA.SR_INSTANCE_ID = p_instance_id
7868           AND     MNRA.RESOURCE_ID = p_resource_id
7869           AND     MNRA.DEPARTMENT_ID = p_department_id
7870           --bug 2341075
7871           --AND     SHIFT_DATE >= trunc(sysdate)
7872           --bug 4232627: select only those records which are after plan start date
7873           --AND     MNRA.SHIFT_DATE >= l_plan_start_date
7874           --bug 4156016
7875           AND     MNRA.organization_id = p_organization_id
7876           AND     MRHM.department_id (+) = MNRA.department_id
7877           AND     MRHM.resource_id  (+)= MNRA.resource_id
7878           AND     MRHM.organization_id (+) = MNRA.organization_id
7879           AND     MRHM.sr_instance_id  (+)= MNRA.sr_instance_id
7880           --AND     MRHM.level_id (+) = -1 --4365873
7881           AND     decode(MRHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
7882           --bug 4232627:
7883           AND     trunc(MNRA.shift_date) >=  trunc(MRHM.effective_date (+))
7884           AND     trunc(MNRA.shift_date) <=  trunc(MRHM.disable_date (+))
7885           AND     MRHM.demand_class (+) = l_demand_class
7886 	)
7887 	; -- dsting removed order by col25;
7888        END IF; -- l_optimized_plan = 1 Bug 2809639
7889 
7890      END IF; --- If l_use_batching =1 then
7891 
7892       -- for period ATP
7893       IF PG_DEBUG in ('Y', 'C') THEN
7894          msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'after insert into msc_atp_sd_details_temp');
7895       END IF;
7896       MSC_ATP_PROC.get_period_data_from_SD_temp(x_atp_period);
7897 
7898       l_current_atp.atp_period := x_atp_period.Period_Start_Date;
7899       l_current_atp.atp_qty := x_atp_period.Period_Quantity;
7900       --- bug 1657855, remove support for min alloc
7901       --l_current_atp.limit_qty := l_current_atp.atp_qty; -- 02/16
7902 
7903    END IF;
7904 
7905     IF PG_DEBUG in ('Y', 'C') THEN
7906        msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'right after the big query');
7907        Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
7908 		l_current_atp);
7909     END IF;
7910 
7911     -- do backward consumption for DCi
7912     MSC_ATP_PROC.Atp_Backward_Consume(l_current_atp.atp_qty);
7913 
7914     IF PG_DEBUG in ('Y', 'C') THEN
7915        msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'right after the backward consume');
7916        Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
7917 		l_current_atp);
7918     END IF;
7919 
7920     -- we have 3 records of tables.
7921     -- l_current_atp: stores the date and quantity for this demand class,
7922     --                and since we need to do backward consumption on this.
7923     -- l_current_steal_atp: stores the date and quantity from higher priority
7924     --                      demand class, this need to consume l_current_atp
7925     -- l_next_steal_atp : stores  the date and quantity for next priority
7926     --                    demand class to cunsume.  we need this because we may
7927     --                    have multiple demand classes at same priority .
7928     -- for example, we have DC1 in priority 1, DC21, DC22 in priority 2,
7929     -- DC3 in priority  3.
7930     -- now DC21 need to take care DC1, DC22 need to take care DC1 but not DC21,
7931     -- DC3 need to take care DC1, DC21, and DC22.  so if we are in the loop for
7932     -- DC22, than l_current_atp is the atp info for DC22,
7933     -- l_current_steal_atp is the atp info for DC1(which does not include DC21),
7934     -- and l_next_steal_atp is the stealing data that we need to take care
7935     -- for DC1, DC21 and DC22  when later on we move to the loop for DC3.
7936 
7937        -- do backward consumption if DC1 to DC(i-1) has any negative bucket,and
7938        -- the priority  is higher than DCi
7939        -- the l_current_atp is an in/out parameter
7940 
7941       -- for 1680719, since in hierarchy demand class we cannot
7942       -- judge the priority by just looking at the priority (we need
7943       -- the information from the parent, so the condition needs to be changed.
7944 
7945       IF l_level_id IN (-1, 1) THEN
7946         -- here is the old logic which should still be ok for level id 1 and -1
7947        IF (i > 1) THEN
7948 
7949         IF (l_demand_class_priority_tab(i) >
7950             l_demand_class_priority_tab (i-1)) THEN
7951         -- we don't need to change the l_current_steal_atp if we don't
7952         -- move to next priority.
7953         -- but we do need to change the l_current_steal_atp
7954         -- if we are in different priority  now.
7955 
7956           l_current_steal_atp := l_next_steal_atp;
7957 
7958           -- Added for bug 1409335. Need to initialize l_next_steal_atp
7959           -- otherwise quanities would be getting accumulated
7960           -- repeatedly.
7961           l_next_steal_atp := l_null_steal_atp;
7962         END IF;
7963        END IF;
7964       ELSE -- IF l_level_id IN (-1, 1) THEN
7965 
7966        IF (i > 1) THEN
7967 
7968         IF (l_class_tab(i) <> l_class_tab(i-1)) THEN
7969 
7970           -- class changed.  If priority of both classes are not the same,
7971           -- then we need to change the curr_steal_atp  at class level.
7972 
7973           IF PG_DEBUG in ('Y', 'C') THEN
7974              msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'class changed');
7975           END IF;
7976 
7977           IF trunc(l_demand_class_priority_tab(i), -3) >
7978              trunc(l_demand_class_priority_tab (i-1), -3) THEN
7979 
7980             IF PG_DEBUG in ('Y', 'C') THEN
7981                msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'class priority changed');
7982             END IF;
7983             l_class_curr_steal_atp := l_class_next_steal_atp;
7984             l_class_next_steal_atp := l_null_steal_atp;
7985           END IF;
7986 
7987           l_partner_next_steal_atp := l_null_steal_atp;
7988           l_partner_curr_steal_atp := l_null_steal_atp;
7989           l_partner_next_steal_atp := l_null_steal_atp;
7990           l_current_steal_atp := l_null_steal_atp;
7991           l_next_steal_atp := l_null_steal_atp;
7992 
7993         ELSE
7994           IF PG_DEBUG in ('Y', 'C') THEN
7995              msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'same class');
7996           END IF;
7997           IF (l_partner_tab(i) <> l_partner_tab(i-1)) THEN
7998             -- customer changed.  If priority of both customers are not the
7999             -- same, we need to change the curr_steal_atp  at partner level.
8000 
8001             IF PG_DEBUG in ('Y', 'C') THEN
8002                msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'customer changed');
8003             END IF;
8004 
8005             IF trunc(l_demand_class_priority_tab(i), -2) >
8006                trunc(l_demand_class_priority_tab (i-1), -2) THEN
8007               IF PG_DEBUG in ('Y', 'C') THEN
8008                  msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'customer priority changed');
8009               END IF;
8010 
8011               l_partner_curr_steal_atp := l_partner_next_steal_atp;
8012               l_partner_next_steal_atp := l_null_steal_atp;
8013             END IF;
8014 
8015             l_current_steal_atp := l_null_steal_atp;
8016             l_next_steal_atp := l_null_steal_atp;
8017 
8018           ELSE
8019             -- same customer
8020             IF PG_DEBUG in ('Y', 'C') THEN
8021                msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'same customer ');
8022             END IF;
8023 
8024             IF (l_demand_class_priority_tab(i) >
8025                 l_demand_class_priority_tab (i-1)) THEN
8026               -- site level priority changed
8027 
8028               IF PG_DEBUG in ('Y', 'C') THEN
8029                  msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'site priority changed');
8030               END IF;
8031               l_current_steal_atp := l_next_steal_atp;
8032               l_next_steal_atp := l_null_steal_atp;
8033 
8034             END IF;
8035           END IF; -- IF (l_partner_tab(i) <> l_partner_tab(i-1))
8036         END IF; -- IF (l_class_tab(i) <> l_class_tab(i-1))
8037 
8038        END IF; -- IF (i > 1)
8039 
8040       END IF; -- IF l_level_id IN (-1, 1)
8041       IF PG_DEBUG in ('Y', 'C') THEN
8042          msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'before we decide we need to do dc consumption');
8043       END IF;
8044 
8045       IF (i > 1) THEN
8046        IF (  -- this is the huge condition
8047              ((l_level_id IN (-1, 1)) AND
8048              (l_demand_class_priority_tab(i) <> l_demand_class_priority_tab(1)))
8049            OR
8050              (l_level_id in (2, 3))
8051           ) THEN
8052 
8053         -- we need to do demand class consume only if we are not in the first
8054         -- preferred priority
8055 
8056         -- bug 1413459
8057         -- we need to remember what's the atp picture before the
8058         -- demand class consumption but after it's own backward
8059         -- consumption.  so that we can figure out the stealing
8060         -- quantity correctly.
8061         IF (NVL(p_insert_flag, 0) <>0)
8062            AND (l_demand_class_tab(i) = p_demand_class) THEN
8063             l_temp_atp := l_current_atp;
8064         END IF;
8065 
8066 --------------
8067         -- 1680719
8068         -- since we have hierarchy now, before we do demand class
8069         -- consumption for site level, we need to do the class level and
8070         -- partner level first
8071 
8072         IF l_level_id IN (2,3) THEN
8073 
8074           IF l_class_tab(i) <> l_class_tab(1) THEN
8075 
8076             IF PG_DEBUG in ('Y', 'C') THEN
8077                msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'before consume l_class_curr_steal_atp');
8078                Print_Period_Qty('l_class_curr_steal_atp.atp_period:atp_qty = ',
8079 			l_class_curr_steal_atp);
8080             END IF;
8081 
8082             MSC_AATP_PVT.Atp_Demand_Class_Consume(l_current_atp, l_class_curr_steal_atp);
8083 
8084             IF PG_DEBUG in ('Y', 'C') THEN
8085                msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'After consume l_class_curr_steal_atp');
8086                Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
8087 			l_current_atp);
8088                Print_Period_Qty('l_class_curr_steal_atp.atp_period:atp_qty = ',
8089 			l_class_curr_steal_atp);
8090             END IF;
8091           END IF; --IF l_class_tab(i) <> l_class_tab(1) THEN
8092 
8093           -- bug 1922942: although partner_id should be unique, we introduced
8094           -- -1 for 'Other' which make the partner_id not unique.
8095           -- for example, Class1/Other and Class2/Other will have same
8096           -- partner_id -1. so the if condition needs to be modified.
8097 
8098           -- IF l_partner_tab(i) <> l_partner_tab(1) THEN
8099 
8100           IF (l_class_tab(i) <> l_class_tab(1)) OR
8101               (l_partner_tab(i) <> l_partner_tab(1)) THEN
8102 
8103             IF PG_DEBUG in ('Y', 'C') THEN
8104                msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'before consume l_partner_curr_steal_atp');
8105             END IF;
8106             MSC_AATP_PVT.Atp_Demand_Class_Consume(l_current_atp, l_partner_curr_steal_atp);
8107 
8108             IF PG_DEBUG in ('Y', 'C') THEN
8109                msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'After consume l_partner_curr_steal_atp');
8110                Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
8111 			l_current_atp);
8112                Print_Period_Qty('l_partner_curr_steal_atp.atp_period:atp_qty = ',
8113 			l_partner_curr_steal_atp);
8114 
8115             END IF;
8116           END IF; -- IF l_partner_tab(i) <> l_partner_tab(1) THEN
8117 
8118         END IF; -- IF l_level_id IN (2,3)
8119 
8120         IF PG_DEBUG in ('Y', 'C') THEN
8121            msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'Before consume current_steal_atp');
8122 
8123            Print_Period_Qty('l_current_steal_atp.atp_period:atp_qty = ',
8124 		l_current_steal_atp);
8125 
8126         END IF;
8127 
8128         MSC_AATP_PVT.Atp_Demand_Class_Consume(l_current_atp, l_current_steal_atp);
8129 
8130         IF PG_DEBUG in ('Y', 'C') THEN
8131            msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'After consume l_current_steal_atp');
8132 
8133            Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
8134 		l_current_atp);
8135            Print_Period_Qty('l_current_steal_atp.atp_period:atp_qty = ',
8136 		l_current_steal_atp);
8137         END IF;
8138 
8139         -- this part is not in the original design.
8140         -- original design is that we will ignore the inconsistancy
8141         -- in the s/d and period atp for display when stealing happens, as long
8142         -- as we take care the stealing in the logic.
8143         -- but i think it is still better to put it in.
8144         -- and actually if we change Atp_Demand_Class_Consume we can
8145         -- deal with this together.  but for now...
8146 
8147         -- we need to know if we need to store the stealing
8148         -- results in to x_atp_supply_demand and x_atp_period or not.
8149         -- we only do it if this is the demand class we request and
8150         -- insert_flag is on
8151 
8152         IF (NVL(p_insert_flag, 0) <>0) AND
8153            (l_demand_class_tab(i) = p_demand_class) THEN
8154 
8155           FOR j in 1..l_current_atp.atp_qty.COUNT LOOP
8156 
8157             IF l_current_atp.atp_qty(j) < l_temp_atp.atp_qty(j) THEN
8158               -- this is the stealing quantity in that period
8159               -- bug 1413459: the stealing quantity should be the current
8160               -- period quantity (after backward consumption, after stealing)
8161               -- minus the period quantity after backward consumption but
8162               -- before the stealing
8163               l_steal_period_quantity := l_current_atp.atp_qty(j) -
8164                                          l_temp_atp.atp_qty(j);
8165 
8166               MSC_SATP_FUNC.Extend_Atp_Supply_Demand(l_temp_atp_supply_demand, l_return_status);
8167 
8168               k := l_temp_atp_supply_demand.Level.Count;
8169               l_temp_atp_supply_demand.level(k) := p_level;
8170               l_temp_atp_supply_demand.identifier(k) := p_identifier;
8171               l_temp_atp_supply_demand.scenario_id(k) := p_scenario_id;
8172               l_temp_atp_supply_demand.department_id(k) := p_department_id;
8173               l_temp_atp_supply_demand.resource_id(k) := p_resource_id;
8174               l_temp_atp_supply_demand.uom(k):= l_uom_code;
8175               l_temp_atp_supply_demand.supply_demand_type(k) := 1;
8176 
8177               -- Bug 1408132 and 1416290, Need to insert type as
8178               -- Demand Class Consumption (45).
8179               l_temp_atp_supply_demand.supply_demand_source_type(k) := 45;
8180 
8181               l_temp_atp_supply_demand.identifier1(k) := p_instance_id;
8182               l_temp_atp_supply_demand.supply_demand_date (k) := l_current_atp.atp_period(j);
8183               l_temp_atp_supply_demand.supply_demand_quantity(k) := l_steal_period_quantity;
8184 
8185               x_atp_period.Total_Demand_Quantity(j):=
8186                      x_atp_period.Total_Demand_Quantity(j) +
8187                      l_steal_period_quantity;
8188 
8189               x_atp_period.period_quantity(j):= x_atp_period.period_quantity(j)
8190                      + l_steal_period_quantity;
8191 
8192             END IF;
8193           END LOOP;
8194 
8195 	  -- dsting
8196 	  move_SD_plsql_into_SD_temp(l_temp_atp_supply_demand);
8197 
8198         END IF;  -- IF (NVL(p_insert_flag, 0) <>0) .....
8199        END IF; -- the huge condition
8200       END IF; -- IF (i > 1)
8201 
8202       --IF l_demand_class_priority_tab(i) < l_priority THEN
8203       ---bug 1655110
8204       IF (l_demand_class <> p_demand_class) THEN
8205         -- we need to prepare the l_next_steal_atp for next priorit
8206 
8207         MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_current_atp, l_next_steal_atp);
8208 
8209         -- 1680719
8210         IF l_level_id IN (-1, 1) THEN
8211           IF l_demand_class_priority_tab(i)<
8212              l_demand_class_priority_tab(i+1) THEN
8213           -- this is the last element of current priority, so we also need
8214           -- to add l_steal_atp into l_next_steal_atp if we can not finish
8215           -- the stealing at this priority
8216 
8217             IF PG_DEBUG in ('Y', 'C') THEN
8218                msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'level = '||l_level_id);
8219                msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'before Adding Add_to_Next_Steal_Atp');
8220                Print_Period_Qty('l_next_steal_atp.atp_period:atp_qty = ',
8221 			l_next_steal_atp);
8222 
8223             END IF;
8224 
8225             MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_current_steal_atp, l_next_steal_atp);
8226 
8227             IF PG_DEBUG in ('Y', 'C') THEN
8228                msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'after Add_to_Next_Steal_Atp');
8229                Print_Period_Qty('l_next_steal_atp.atp_period:atp_qty = ',
8230 			l_next_steal_atp);
8231             END IF;
8232 
8233           END IF;
8234 
8235         ELSE -- IF l_level_id IN (-1, 1)
8236           -- this is for hierarchy customer level and site level
8237           IF PG_DEBUG in ('Y', 'C') THEN
8238              msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'level = '||l_level_id);
8239              msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'i = '||i);
8240              msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'l_class_tab(i) = '||l_class_tab(i));
8241              msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'l_class_tab(i+1) = '||l_class_tab(i+1));
8242           END IF;
8243           IF (l_class_tab(i) <> l_class_tab(i+1)) THEN
8244 
8245             -- class changed.  If priority of both classes are not the same,
8246             -- then we need to change the curr_steal_atp  at class level.
8247             IF PG_DEBUG in ('Y', 'C') THEN
8248                msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'class changed');
8249             END IF;
8250 
8251             MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_current_steal_atp, l_next_steal_atp);
8252             MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_next_steal_atp, l_partner_next_steal_atp);
8253             MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_partner_curr_steal_atp,
8254                                   l_partner_next_steal_atp);
8255             MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_partner_next_steal_atp,
8256                                   l_class_next_steal_atp);
8257 
8258             IF trunc(l_demand_class_priority_tab(i), -3)<
8259                trunc(l_demand_class_priority_tab (i+1), -3) THEN
8260 
8261               MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_class_curr_steal_atp,
8262                                     l_class_next_steal_atp);
8263 
8264             END IF;
8265 
8266           ELSE
8267 
8268             IF PG_DEBUG in ('Y', 'C') THEN
8269                msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'class the same');
8270             END IF;
8271             IF (l_partner_tab(i) <> l_partner_tab(i+1)) THEN
8272               -- customer changed
8273               IF PG_DEBUG in ('Y', 'C') THEN
8274                  msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'customer not the same');
8275               END IF;
8276               MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_current_steal_atp, l_next_steal_atp);
8277 
8278               MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_next_steal_atp, l_partner_next_steal_atp);
8279 
8280               IF trunc(l_demand_class_priority_tab(i), -2)<
8281                  trunc(l_demand_class_priority_tab (i+1), -2) THEN
8282                 -- customer priority changed
8283 
8284                 IF PG_DEBUG in ('Y', 'C') THEN
8285                    msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'customer priority changed');
8286                 END IF;
8287                 MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_partner_curr_steal_atp,
8288                                       l_partner_next_steal_atp);
8289 
8290               END IF;
8291 
8292 
8293             ELSE
8294               -- same customer
8295               IF PG_DEBUG in ('Y', 'C') THEN
8296                  msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'customer the same');
8297               END IF;
8298               IF (l_demand_class_priority_tab(i)<>
8299                   l_demand_class_priority_tab (i+1)) THEN
8300                 -- site level priority changed
8301                 MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_current_steal_atp, l_next_steal_atp);
8302 
8303               END IF;
8304             END IF;
8305           END IF;
8306         END IF; -- IF l_level_id IN (-1, 1)
8307       END IF;
8308 
8309       -- 1665110
8310       IF PG_DEBUG in ('Y', 'C') THEN
8311          msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'p_demand_class = '||p_demand_class);
8312       END IF;
8313       EXIT WHEN (l_demand_class = p_demand_class);
8314       IF PG_DEBUG in ('Y', 'C') THEN
8315          msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'after the exit statement, so we did not exit');
8316       END IF;
8317 
8318   END LOOP;
8319 
8320   MSC_ATP_PROC.Atp_Accumulate(l_current_atp.atp_qty);
8321 
8322   IF PG_DEBUG in ('Y', 'C') THEN
8323      msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'right after the Atp_Accumulate');
8324      Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
8325 	l_current_atp);
8326   END IF;
8327 
8328   x_atp_info := l_current_atp;
8329 
8330   -- get the infinite time fence date if it exists
8331   l_infinite_time_fence_date := MSC_ATP_FUNC.get_infinite_time_fence_date(null,
8332              null,null, p_plan_id);
8333 
8334 
8335   IF l_infinite_time_fence_date IS NOT NULL THEN
8336       -- add one more entry to indicate infinite time fence date
8337       -- and quantity.
8338       x_atp_info.atp_qty.EXTEND;
8339       x_atp_info.atp_period.EXTEND;
8340       --- bug 1657855, remove support for alloc
8341       --x_atp_info.limit_qty.EXTEND;
8342 
8343       i := x_atp_info.atp_qty.COUNT;
8344       x_atp_info.atp_period(i) := l_infinite_time_fence_date;
8345       x_atp_info.atp_qty(i) := MSC_ATP_PVT.INFINITE_NUMBER;
8346       --x_atp_info.limit_qty(i) := MSC_ATP_PVT.INFINITE_NUMBER;
8347 
8348       IF NVL(p_insert_flag, 0) <> 0 THEN
8349         -- add one more entry to indicate infinite time fence date
8350         -- and quantity.
8351 
8352         x_atp_period.Cumulative_Quantity := x_atp_info.atp_qty;
8353 
8354         j := x_atp_period.Level.COUNT;
8355         MSC_SATP_FUNC.Extend_Atp_Period(x_atp_period, l_return_status);
8356         j := j + 1;
8357         IF j > 1 THEN
8358           x_atp_period.Period_End_Date(j-1) := l_infinite_time_fence_date -1;
8359 	  -- dsting
8360           --x_atp_period.Identifier1(j) := x_atp_supply_demand.Identifier1(j-1);
8361           --x_atp_period.Identifier2(j) := x_atp_supply_demand.Identifier2(j-1);
8362           x_atp_period.Identifier1(j) := x_atp_period.Identifier1(j-1);
8363           x_atp_period.Identifier2(j) := x_atp_period.Identifier2(j-1);
8364         END IF;
8365 
8366         x_atp_period.Level(j) := p_level;
8367         x_atp_period.Identifier(j) := p_identifier;
8368         x_atp_period.Scenario_Id(j) := p_scenario_id;
8369         x_atp_period.Pegging_Id(j) := NULL;
8370         x_atp_period.End_Pegging_Id(j) := NULL;
8371         x_atp_period.Department_Id(j) := p_department_id;
8372         x_atp_period.Resource_Id(j) := p_resource_id;
8373         x_atp_period.Organization_id(j) := p_organization_id;
8374         x_atp_period.Period_Start_Date(j) := l_infinite_time_fence_date;
8375         x_atp_period.Total_Supply_Quantity(j) := MSC_ATP_PVT.INFINITE_NUMBER;
8376         x_atp_period.Total_Demand_Quantity(j) := 0;
8377         x_atp_period.Period_Quantity(j) := MSC_ATP_PVT.INFINITE_NUMBER;
8378         x_atp_period.Cumulative_Quantity(j) := MSC_ATP_PVT.INFINITE_NUMBER;
8379 
8380     END IF;
8381   END IF;
8382 -- END IF;
8383 
8384 END Res_Alloc_Cum_Atp;
8385 
8386 /* spec changed as part of ship_rec_cal changes
8387    various input parameters passed in a record atp_info_rec
8388 */
8389 PROCEDURE Supplier_Alloc_Cum_Atp(
8390         p_sup_atp_info_rec      IN      MSC_ATP_REQ.ATP_Info_Rec,
8391 	p_identifier          	IN 	NUMBER,
8392 	p_request_date        	IN 	DATE,
8393 	x_atp_info            	OUT 	NoCopy MRP_ATP_PVT.ATP_Info,
8394 	x_atp_period          	OUT 	NoCopy MRP_ATP_PUB.ATP_Period_Typ,
8395 	x_atp_supply_demand   	OUT 	NoCopy MRP_ATP_PUB.ATP_Supply_Demand_Typ
8396 )
8397 IS
8398 l_calendar_code                 VARCHAR2(14);
8399 l_calendar_exception_set_id     NUMBER;
8400 l_level_id                      NUMBER;
8401 l_priority			NUMBER := 1;
8402 l_allocation_percent		NUMBER := 100;
8403 l_demand_class_tab		MRP_ATP_PUB.char80_arr
8404                                    := MRP_ATP_PUB.char80_arr();
8405 l_demand_class_priority_tab	MRP_ATP_PUB.number_arr
8406                                    := MRP_ATP_PUB.number_arr();
8407 l_current_atp			MRP_ATP_PVT.ATP_Info;
8408 l_next_steal_atp		MRP_ATP_PVT.ATP_Info;
8409 l_null_steal_atp		MRP_ATP_PVT.ATP_Info;
8410 l_current_steal_atp             MRP_ATP_PVT.ATP_Info;
8411 l_temp_atp                      MRP_ATP_PVT.ATP_Info;
8412 i				PLS_INTEGER;
8413 l_infinite_time_fence_date	DATE;
8414 mm				PLS_INTEGER;
8415 ii                              PLS_INTEGER;
8416 jj                              PLS_INTEGER;
8417 j				PLS_INTEGER;
8418 k				PLS_INTEGER;
8419 l_demand_class			VARCHAR2(80);
8420 l_inv_item_id			NUMBER;
8421 l_uom_code			VARCHAR2(3);
8422 l_null_num  			number := null;
8423 l_null_char    			varchar2(3) := null;
8424 l_return_status			VARCHAR(1);
8425 l_steal_period_quantity		number;
8426 l_instance_id                   number;
8427 l_org_id                        number;
8428 l_plan_start_date               DATE;
8429 l_postprocessing_lead_time      NUMBER;
8430 l_cutoff_date                   DATE;
8431 
8432 -- 1680719
8433 l_class_tab                     MRP_ATP_PUB.char30_arr
8434                                     := MRP_ATP_PUB.char30_arr();
8435 l_partner_tab                   MRP_ATP_PUB.number_arr
8436                                     := MRP_ATP_PUB.number_arr();
8437 l_class_next_steal_atp          MRP_ATP_PVT.ATP_Info;
8438 l_partner_next_steal_atp        MRP_ATP_PVT.ATP_Info;
8439 l_class_curr_steal_atp          MRP_ATP_PVT.ATP_Info;
8440 l_partner_curr_steal_atp        MRP_ATP_PVT.ATP_Info;
8441 l_pos1                          NUMBER;
8442 l_pos2                          NUMBER;
8443 delim     constant varchar2(1) := fnd_global.local_chr(13);
8444 
8445 -- dsting for s/d performance enh
8446 l_temp_atp_supply_demand   		MRP_ATP_PUB.ATP_Supply_Demand_Typ;
8447 
8448 --s_cto_rearch
8449 l_check_cap_model_flag          number;
8450 --e_cto_arch
8451 
8452 -- ship_rec_cal
8453 l_sysdate               DATE := trunc(sysdate); --4135752
8454 l_sys_next_date                 DATE; --bug3333114
8455 
8456 BEGIN
8457 
8458   IF PG_DEBUG in ('Y', 'C') THEN
8459      msc_sch_wb.atp_debug('******* Supplier_Alloc_Cum_Atp *******');
8460      msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'p_sup_atp_info_rec.instance_id =' || p_sup_atp_info_rec.instance_id);
8461      msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'p_sup_atp_info_rec.supplier_id =' || p_sup_atp_info_rec.supplier_id);
8462      msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'p_sup_atp_info_rec.supplier_site_id =' || p_sup_atp_info_rec.supplier_site_id);
8463      msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'p_sup_atp_info_rec.inventory_item_id =' || p_sup_atp_info_rec.inventory_item_id);
8464      msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'p_sup_atp_info_rec.organization_id =' || p_sup_atp_info_rec.organization_id);
8465      msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'p_sup_atp_info_rec.demand_class =' || p_sup_atp_info_rec.demand_class);
8466      msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'p_request_date =' || p_request_date );
8467      msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'p_sup_atp_info_rec.shipping_cal_code =' || p_sup_atp_info_rec.shipping_cal_code );
8468   END IF;
8469 
8470 
8471   -- find all the demand classes that we need to take care: all the demand
8472   -- classes that have higher priority  + this requested demand class.
8473 
8474   -- Logic
8475   -- Step 1:
8476   -- 	FOR each demand class DCi, we need to
8477   --  	1. get the net daily availability
8478   --  	2. do backward consumption
8479   --  	3. do backward consumption if DC1 to DC(i-1) has any negative bucket
8480   -- 	END LOOP
8481   -- Step 2:
8482   --    do accumulation for the requested demand class
8483 
8484   -- select the priority  and allocation_percent for that item/demand class.
8485   -- if no data found, check if this item has a valid allocation rule.
8486   -- otherwise return error.
8487 
8488  -- bug 1169467
8489  -- get the plan start date. later on we will use this restrict the
8490  -- availability
8491 
8492  -- Supplier Capacity and Lead Time (SCLT) Proj
8493  -- Commented out
8494  -- SELECT trunc(plan_start_date), sr_instance_id, organization_id,
8495  --        trunc(cutoff_date)
8496  -- INTO   l_plan_start_date, l_instance_id, l_org_id, l_cutoff_date
8497  -- FROM   msc_plans
8498  -- WHERE  plan_id = p_sup_atp_info_rec.plan_id;
8499 
8500  -- Instead re-assigned local values using global variable
8501     l_plan_start_date := MSC_ATP_PVT.G_PLAN_INFO_REC.plan_start_date;
8502     l_instance_id     := MSC_ATP_PVT.G_PLAN_INFO_REC.sr_instance_id;
8503     l_org_id          := MSC_ATP_PVT.G_PLAN_INFO_REC.organization_id;
8504     l_cutoff_date     := MSC_ATP_PVT.G_PLAN_INFO_REC.plan_cutoff_date;
8505 
8506  IF PG_DEBUG in ('Y', 'C') THEN
8507     msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'l_plan_start_date = '||l_plan_start_date);
8508     msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'l_instance_id = '||l_instance_id);
8509     msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'l_org_id = '||l_org_id);
8510     msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'l_cutoff_date = '||l_cutoff_date);
8511  END IF;
8512 
8513  --s_cto_rearch
8514  IF (p_sup_atp_info_rec.bom_item_type = 4 and p_sup_atp_info_rec.rep_ord_flag = 'Y') THEN
8515         l_inv_item_id := p_sup_atp_info_rec.base_item_id;
8516         l_check_cap_model_flag := 1;
8517  ELSIF  p_sup_atp_info_rec.bom_item_type = 1 THEN
8518         l_inv_item_id := p_sup_atp_info_rec.inventory_item_id;
8519         l_check_cap_model_flag := 1;
8520  ELSE
8521         l_inv_item_id := p_sup_atp_info_rec.inventory_item_id;
8522  END IF;
8523  --e_cto_rearch
8524 
8525 
8526  --MSC_AATP_PVT.Get_DC_Info(l_instance_id, p_sup_atp_info_rec.inventory_item_id, p_sup_atp_info_rec.organization_id, null, null,
8527  MSC_AATP_PVT.Get_DC_Info(l_instance_id, l_inv_item_id, p_sup_atp_info_rec.organization_id, null, null,
8528 	 p_sup_atp_info_rec.demand_class, p_request_date, l_level_id, l_priority, l_allocation_percent, l_return_status);
8529   -- find the demand classes that have priority  higher (small number) than
8530   -- the requested demand class
8531 
8532 -- IF l_allocation_percent <> 0.0 THEN
8533  IF PG_DEBUG in ('Y', 'C') THEN
8534     msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'before select the high priority demand class');
8535  END IF;
8536 
8537   -- bug 1680719
8538   --bug3948494 Do not select Higher priority DC if the requested DC
8539   --is at highest priority , we donot honor for forward consumption method here.
8540   IF l_level_id = -1 AND l_priority <> 1 THEN
8541 
8542     IF PG_DEBUG in ('Y', 'C') THEN
8543        msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'l_allocation_percent = '||l_allocation_percent);
8544     END IF;
8545     SELECT demand_class, priority
8546     BULK COLLECT INTO l_demand_class_tab, l_demand_class_priority_tab
8547     FROM   msc_item_hierarchy_mv
8548     --WHERE  inventory_item_id = p_sup_atp_info_rec.inventory_item_id
8549     WHERE  inventory_item_id = l_inv_item_id
8550     AND    organization_id = p_sup_atp_info_rec.organization_id -- Ship To org
8551     AND    sr_instance_id = l_instance_id
8552     AND    p_request_date BETWEEN effective_date AND disable_date
8553     AND    priority  <= l_priority   -- 1665110, add '='
8554     AND    level_id = l_level_id
8555     ORDER BY priority asc, allocation_percent desc ;
8556 
8557   ELSIF l_level_id = 1 THEN
8558 
8559     IF PG_DEBUG in ('Y', 'C') THEN
8560        msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'l_level_id = '||l_level_id);
8561     END IF;
8562     SELECT demand_class, priority
8563     BULK COLLECT INTO l_demand_class_tab, l_demand_class_priority_tab
8564     FROM   msc_item_hierarchy_mv
8565     --WHERE  inventory_item_id = p_sup_atp_info_rec.inventory_item_id
8566     WHERE  inventory_item_id = l_inv_item_id
8567     AND    organization_id = p_sup_atp_info_rec.organization_id
8568     AND    sr_instance_id = l_instance_id
8569     AND    p_request_date BETWEEN effective_date AND disable_date
8570     AND    priority  <= l_priority   -- 1665110, add '='
8571     AND    level_id = l_level_id
8572     ORDER BY priority asc, class;
8573 
8574   ELSIF l_level_id = 2 THEN
8575 
8576 
8577     IF PG_DEBUG in ('Y', 'C') THEN
8578        msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'l_level_id = '||l_level_id);
8579     END IF;
8580     SELECT mv1.demand_class, mv1.priority, mv1.class, mv1.partner_id
8581     BULK COLLECT INTO l_demand_class_tab, l_demand_class_priority_tab,
8582                       l_class_tab, l_partner_tab
8583     FROM   msc_item_hierarchy_mv mv1
8584     --WHERE  mv1.inventory_item_id = p_sup_atp_info_rec.inventory_item_id
8585     WHERE  inventory_item_id = l_inv_item_id
8586     AND    mv1.organization_id = p_sup_atp_info_rec.organization_id
8587     AND    mv1.sr_instance_id = l_instance_id
8588     AND    p_request_date BETWEEN mv1.effective_date AND mv1.disable_date
8589     --AND    mv1.priority  <= l_priority   -- 1665110, add '='
8590     AND    mv1.level_id = l_level_id
8591     AND trunc(mv1.priority, -3) <= trunc(l_priority, -3)
8592       ORDER BY trunc(mv1.priority, -3), mv1.class ,
8593                trunc(mv1.priority, -2), mv1.partner_id;
8594 
8595 
8596   ELSIF l_level_id = 3 THEN
8597 
8598     -- bug 1680719
8599     -- we need to select the class, partner_id, partner_site_id
8600 
8601     IF PG_DEBUG in ('Y', 'C') THEN
8602        msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'l_level_id = '||l_level_id);
8603     END IF;
8604     SELECT mv1.demand_class, mv1.priority, mv1.class, mv1.partner_id
8605     BULK COLLECT INTO l_demand_class_tab, l_demand_class_priority_tab,
8606                       l_class_tab, l_partner_tab
8607     FROM   msc_item_hierarchy_mv mv1
8608     --WHERE  mv1.inventory_item_id = p_sup_atp_info_rec.inventory_item_id
8609     WHERE  mv1.inventory_item_id = l_inv_item_id
8610     AND    mv1.organization_id = p_sup_atp_info_rec.organization_id
8611     AND    mv1.sr_instance_id = l_instance_id
8612     AND    p_request_date BETWEEN mv1.effective_date AND mv1.disable_date
8613     --AND    mv1.priority  <= l_priority   -- 1665110, add '='
8614     AND    mv1.level_id = l_level_id
8615     AND trunc(mv1.priority, -3) <= trunc(l_priority, -3)
8616       ORDER BY trunc(mv1.priority, -3), mv1.class ,
8617                trunc(mv1.priority, -2), mv1.partner_id,
8618                mv1.priority, mv1.partner_site_id;
8619   END IF;
8620 
8621 --/* 1665110
8622   IF l_demand_class_tab.count = 0 THEN
8623      -- add the request demand class into the list
8624      l_demand_class_tab.Extend;
8625      l_demand_class_priority_tab.Extend;
8626      i := l_demand_class_tab.COUNT;
8627      l_demand_class_priority_tab(i) := l_priority;
8628      l_demand_class_tab(i) := p_sup_atp_info_rec.demand_class;
8629 
8630      -- 1680719
8631      IF l_level_id in (2, 3) THEN
8632          l_class_tab.Extend;
8633          l_partner_tab.Extend;
8634          l_pos1 := instr(p_sup_atp_info_rec.demand_class,delim,1,1);
8635          l_pos2 := instr(p_sup_atp_info_rec.demand_class,delim,1,2);
8636          l_class_tab(i) := substr(p_sup_atp_info_rec.demand_class,1,l_pos1-1);
8637          IF l_pos2 = 0 THEN
8638            l_partner_tab(i) := substr(p_sup_atp_info_rec.demand_class,l_pos1+1);
8639          ELSE
8640            l_partner_tab(i) := substr(p_sup_atp_info_rec.demand_class,l_pos1+1,l_pos2-l_pos1-1) ;
8641          END IF;
8642      END IF;
8643   END IF;
8644 --1665110 */
8645 
8646   mm := l_demand_class_tab.FIRST;
8647 
8648   WHILE mm is not null LOOP
8649      IF PG_DEBUG in ('Y', 'C') THEN
8650         msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'l_demand_class_tab and priority = '||
8651         l_demand_class_tab(mm) ||' : '|| l_demand_class_priority_tab(mm));
8652      END IF;
8653 
8654      IF l_level_id in (2,3) THEN
8655        IF PG_DEBUG in ('Y', 'C') THEN
8656           msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'l_class_tab and partner = '||
8657         l_class_tab(mm) ||' : '||l_partner_tab(mm));
8658        END IF;
8659      END IF;
8660 
8661      mm := l_demand_class_tab.Next(mm);
8662 
8663   END LOOP;
8664 
8665   -- get the uom code :bug 1187141
8666   SELECT uom_code, postprocessing_lead_time
8667   INTO   l_uom_code, l_postprocessing_lead_time
8668   FROM   msc_system_items
8669   WHERE  plan_id = p_sup_atp_info_rec.plan_id
8670   AND    sr_instance_id = p_sup_atp_info_rec.instance_id
8671   AND    organization_id = p_sup_atp_info_rec.organization_id
8672   --AND    inventory_item_id = p_sup_atp_info_rec.inventory_item_id;
8673   AND    inventory_item_id = l_inv_item_id;
8674 
8675   IF PG_DEBUG in ('Y', 'C') THEN
8676      msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'l_uom_code and l_postprocessing_lead_time = '||
8677         l_uom_code ||' : '||l_postprocessing_lead_time);
8678   END IF;
8679 
8680   -- for performance reason, we need to get the following info and
8681   -- store in variables instead of joining it
8682 
8683   --=======================================================================================================
8684   --  ship_rec_cal changes begin
8685   --=======================================================================================================
8686   --  use SMC instead of OMC for netting
8687   --  IF SMC is FOC get plan owning org's calendar. Since we assume that every org must have atleast a
8688   --  manufacturing calendar defined, we use plan owning org's calendar as it will be spanning atleast
8689   --  upto plan end date
8690   --=======================================================================================================
8691   IF p_sup_atp_info_rec.manufacturing_cal_code <> '@@@' THEN
8692      l_calendar_code := p_sup_atp_info_rec.manufacturing_cal_code;
8693   ELSE
8694         SELECT  tp.calendar_code
8695         INTO    l_calendar_code
8696         FROM    msc_trading_partners tp,
8697                 msc_plans mp
8698         WHERE   mp.plan_id = p_sup_atp_info_rec.plan_id
8699         AND     tp.sr_instance_id  = mp.sr_instance_id
8700         AND     tp.partner_type    = 3
8701         AND     tp.sr_tp_id        = mp.organization_id;
8702   END IF;
8703   l_calendar_exception_set_id := -1;
8704 
8705   --bug3333114 start
8706   l_sys_next_date := MSC_CALENDAR.NEXT_WORK_DAY (
8707                                         p_sup_atp_info_rec.shipping_cal_code,
8708                                         l_instance_id,
8709                                         TRUNC(sysdate));
8710 
8711   IF PG_DEBUG in ('Y', 'C') THEN
8712   msc_sch_wb.atp_debug('Sys next Date : '||to_char(l_sys_next_date, 'DD-MON-YYYY'));
8713   END IF;
8714 
8715   IF (l_sys_next_date is NULL) THEN
8716       msc_sch_wb.atp_debug('Sys Next Date is null');
8717       MSC_SCH_WB.G_ATP_ERROR_CODE := MSC_ATP_PVT.NO_MATCHING_CAL_DATE;
8718       RAISE FND_API.G_EXC_ERROR;
8719   END IF;
8720   --bug3333114 end
8721 
8722   IF PG_DEBUG in ('Y', 'C') THEN
8723      msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'p_sup_atp_info_rec.manufacturing_cal_code='||p_sup_atp_info_rec.manufacturing_cal_code);
8724      msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'l_calendar_code='||l_calendar_code);
8725      msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'l_calendar_exception_set_id'|| l_calendar_exception_set_id);
8726   END IF;
8727   --  ship_rec_cal changes end
8728 
8729 
8730   FOR i in 1..l_demand_class_tab.COUNT LOOP
8731           l_demand_class := l_demand_class_tab(i);
8732           IF PG_DEBUG in ('Y', 'C') THEN
8733              msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'in i loop, i = '||i);
8734              msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'l_demand_class = '||l_demand_class);
8735           END IF;
8736     --=======================================================================================================
8737     -- ship_rec_cal changes begin
8738     --=======================================================================================================
8739     --  In all the SQLs that get supplier capacities following are the changes:
8740     --  1. Pass (c.seq_num - p_sup_atp_info_rec.sysdate_seq_num) to get_tolerance_percentage fn instead of
8741     --     passing c.calendar_date.
8742     --  2. If calendar code passed in FOC, we use plan owning org's calendar and remove p_seq_num is not
8743     --     null filter condition.
8744     --
8745     --  In all the SQLs that get planned orders, purchase orders and purchase requisitions following
8746     --  are the changes:
8747     --  1. We use new_dock_date or new_ship_date depending on whether supplier capacity is dock capacity or
8748     --     ship capacity.
8749     --     Earlier we used to look at new_schedule_date and offset post_processing_lead_time.
8750     --  2. Removed join with msc_calendar_dates
8751     --=======================================================================================================
8752     -- get the daily net availability for DCi
8753     IF (NVL(p_sup_atp_info_rec.insert_flag, 0) = 0  OR l_demand_class <> p_sup_atp_info_rec.demand_class) THEN
8754       IF l_check_cap_model_flag = 1 THEN
8755 
8756           -- we don't want details
8757          SELECT 	trunc(l_date), --4135752
8758       		   SUM(quantity)
8759          BULK COLLECT INTO
8760                	   l_current_atp.atp_period,
8761                	   l_current_atp.atp_qty
8762          FROM (
8763          SELECT GREATEST(cs.calendar_date,l_sys_next_date) l_date, --bug3333114
8764 	        cs.capacity*(1+NVL(MSC_ATP_FUNC.get_tolerance_percentage(
8765                                       p_sup_atp_info_rec.instance_id, p_sup_atp_info_rec.plan_id,
8766                                       l_inv_item_id, p_sup_atp_info_rec.organization_id,
8767                                       p_sup_atp_info_rec.supplier_id, p_sup_atp_info_rec.supplier_site_id,
8768                                       cs.seq_num - p_sup_atp_info_rec.sysdate_seq_num),0))*
8769 		   /*NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(l_instance_id,
8770                                             s.inventory_item_id,
8771                                             p_sup_atp_info_rec.organization_id,
8772                                             null,
8773                                             null,
8774                                             l_demand_class,
8775                                             c.calendar_date), 1) quantity*/
8776                    NVL(MIHM.allocation_percent/100, 1) quantity --4365873
8777 	FROM
8778       (
8779       SELECT
8780       	    c.calendar_date,
8781       	    c.seq_num,
8782 	    s.inventory_item_id,
8783 	    s.sr_instance_id,
8784 	    S.ORGANIZATION_ID,
8785 	    S.capacity
8786          FROM   msc_calendar_dates c,
8787                 msc_supplier_capacities s
8788          --WHERE  s.inventory_item_id = p_sup_atp_info_rec.inventory_item_id
8789          WHERE  s.inventory_item_id = l_inv_item_id
8790          AND    s.sr_instance_id = p_sup_atp_info_rec.instance_id
8791          AND    s.plan_id = p_sup_atp_info_rec.plan_id
8792          AND    s.organization_id = p_sup_atp_info_rec.organization_id
8793          AND    s.supplier_id = p_sup_atp_info_rec.supplier_id
8794          AND    NVL(s.supplier_site_id, -1) = NVL(p_sup_atp_info_rec.supplier_site_id, -1)
8795          AND    c.calendar_date BETWEEN trunc(s.from_date)
8796                                 --AND NVL(s.to_date,l_cutoff_date)
8797                                 AND trunc(NVL(s.to_date,least(p_sup_atp_info_rec.last_cap_date,l_cutoff_date))) --4055719
8798          AND    (c.seq_num IS NOT NULL or p_sup_atp_info_rec.manufacturing_cal_code = '@@@')
8799          AND    c.calendar_code = l_calendar_code
8800          AND    c.exception_set_id = l_calendar_exception_set_id
8801          AND    c.sr_instance_id = l_instance_id
8802          AND    c.calendar_date >= NVL(p_sup_atp_info_rec.sup_cap_cum_date, l_plan_start_date))CS,
8803       msc_item_hierarchy_mv mihm
8804       WHERE
8805       --4365873
8806       	     CS.INVENTORY_ITEM_ID = MIHM.INVENTORY_ITEM_ID(+)
8807       AND    CS.SR_INSTANCE_ID = MIHM.SR_INSTANCE_ID (+)
8808       AND    CS.ORGANIZATION_ID = MIHM.ORGANIZATION_ID (+)
8809       AND    decode(MIHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
8810       AND    CS.calendar_date >= MIHM.effective_date (+)
8811       AND    CS.calendar_date <= MIHM.disable_date (+)
8812       AND    MIHM.demand_class (+) = l_demand_class
8813          -- Supplier Capacity (SCLT) Accumulation starts from this date.
8814          -- AND    c.calendar_date >= l_plan_start_date -- bug 1169467
8815          UNION ALL
8816          SELECT GREATEST(trunc(Decode(p_sup_atp_info_rec.sup_cap_type,  --4135752
8817                                 1, p.new_ship_date,
8818                                 p.new_dock_date)),l_sys_next_date) l_date, -- For ship_rec_cal --bug3333114
8819                 (-1)*(p.new_order_quantity - NVL(p.implement_quantity,0))*
8820 		   DECODE(DECODE(G_HIERARCHY_PROFILE,
8821                                  --2424357
8822                                  1, DECODE(p.DEMAND_CLASS, null, null,
8823                                        DECODE(l_demand_class, '-1',
8824                                            MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
8825                                                      null,
8826                                                      null,
8827                                                      p.inventory_item_id,
8828                                                      p.organization_id,
8829                                                      p.sr_instance_id,
8830                                                      trunc(Decode(p_sup_atp_info_rec.sup_cap_type,
8831                                                           1, p.new_ship_date,
8832                                                           p.new_dock_date
8833                                                      )), --4135752
8834                                                      l_level_id,
8835                                                      p.DEMAND_CLASS), p.DEMAND_CLASS)),
8836                                  2, DECODE(p.CUSTOMER_ID, NULL, TO_CHAR(NULL),
8837                                            0, TO_CHAR(NULL),
8838                                                    MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
8839                                                      p.CUSTOMER_ID,
8840                                                      p.SHIP_TO_SITE_ID,
8841                                                      p.inventory_item_id,
8842                                                      p.organization_id,
8843                                                      p.sr_instance_id,
8844                                                      trunc(Decode(p_sup_atp_info_rec.sup_cap_type,
8845                                                           1, p.new_ship_date,
8846                                                           p.new_dock_date
8847                                                      )), --4135752
8848                                                      l_level_id,
8849                                                      NULL))),
8850 		           l_demand_class, 1,
8851 			   NULL, NVL(MIHM.allocation_percent/100,  --4365873
8852 			   /*NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(p.sr_instance_id,
8853                                                           p.inventory_item_id ,
8854 				                          p.organization_id,
8855                                                           null,
8856                                                           null,
8857 				                          l_demand_class,
8858                                                           trunc(Decode(p_sup_atp_info_rec.sup_cap_type,
8859                                                                         1, p.new_ship_date,
8860                                                                         p.new_dock_date
8861                                                           ))),*/
8862                                                            1), --4135752
8863                            DECODE(MIHM.allocation_percent/100,
8864                            /*DECODE(MSC_AATP_FUNC.Get_DC_Alloc_Percent(p.sr_instance_id,
8865                                                        p.inventory_item_id,
8866                                                        p.organization_id,
8867                                                        null,
8868                                                        null,
8869                                                        l_demand_class,
8870                                                        trunc(Decode(p_sup_atp_info_rec.sup_cap_type,  --4135752
8871                                                           1, p.new_ship_date,
8872                                                           p.new_dock_date
8873                                                        ))),*/
8874 				   NULL, 1,
8875 				   0)) quantity
8876          -- Supplier Capacity (SCLT) Changes Begin
8877          FROM   msc_supplies p,msc_item_hierarchy_mv MIHM
8878          WHERE  (p.order_type IN (5, 2)
8879                 OR (MSC_ATP_REQ.G_PURCHASE_ORDER_PREFERENCE = trunc(MSC_ATP_REQ.G_PROMISE_DATE) --4135752
8880                      AND p.order_type = 1 AND p.promised_date IS NULL))
8881          -- Supplier Capacity (SCLT) Accumulation Ignore Purchase Orders
8882          -- WHERE  p.order_type IN (5, 1, 2)
8883          AND    p.plan_id = p_sup_atp_info_rec.plan_id
8884          AND    p.sr_instance_id = p_sup_atp_info_rec.instance_id
8885          --AND    p.inventory_item_id = p_sup_atp_info_rec.inventory_item_id
8886    -- 1214694      AND    p.organization_id = p_sup_atp_info_rec.organization_id
8887          AND    p.supplier_id  = p_sup_atp_info_rec.supplier_id
8888          AND    NVL(p.supplier_site_id, -1) = NVL(p_sup_atp_info_rec.supplier_site_id, -1)
8889                 -- Exclude Cancelled Supplies 2460645
8890          AND    NVL(P.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
8891                  --consider ATP inserted PO only and palling PO will be tied to forecats
8892          AND    ((p.inventory_item_id = l_inv_item_id and p.record_source=2) OR
8893                     p.inventory_item_id in
8894                            (select inventory_item_id from msc_system_items msi
8895                             where  msi.base_item_id = l_inv_item_id
8896                             and    msi.plan_id = p_sup_atp_info_rec.plan_id
8897                             and    msi.organization_id = p_sup_atp_info_rec.organization_id
8898                             and    msi.base_item_id = l_inv_item_id))
8899          AND    trunc(Decode(p_sup_atp_info_rec.sup_cap_type, 1, p.new_ship_date,p.new_dock_date)) --4055719 --4135752
8900                       <= trunc(least(p_sup_atp_info_rec.last_cap_date,l_cutoff_date))                    --4135752
8901          -- Supplier Capacity (SCLT) Changes End
8902       --4365873
8903       AND    p.INVENTORY_ITEM_ID = MIHM.INVENTORY_ITEM_ID(+)
8904       AND    p.SR_INSTANCE_ID = MIHM.SR_INSTANCE_ID (+)
8905       AND    p.ORGANIZATION_ID = MIHM.ORGANIZATION_ID (+)
8906       AND    decode(MIHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
8907       AND    trunc(Decode(p_sup_atp_info_rec.sup_cap_type, 1, p.new_ship_date,p.new_dock_date)) >= MIHM.effective_date (+)
8908       AND    trunc(Decode(p_sup_atp_info_rec.sup_cap_type, 1, p.new_ship_date,p.new_dock_date)) <= MIHM.disable_date (+)
8909       AND    MIHM.demand_class (+) = l_demand_class
8910          )
8911          GROUP BY l_date
8912          ORDER BY l_DATE;--4698199
8913          --- bug 1657855, remove support for min alloc
8914          --l_current_atp.limit_qty := l_current_atp.atp_qty;
8915       ELSE
8916           -- we don't want details
8917          SELECT 	trunc(l_date), --4135752
8918       		   SUM(quantity)
8919          BULK COLLECT INTO
8920                	   l_current_atp.atp_period,
8921                	   l_current_atp.atp_qty
8922          FROM (
8923          SELECT GREATEST(cs.calendar_date,l_sys_next_date) l_date, --bug3333114
8924 	        cs.capacity*(1+NVL(MSC_ATP_FUNC.get_tolerance_percentage(
8925                                       p_sup_atp_info_rec.instance_id, p_sup_atp_info_rec.plan_id,
8926                                       p_sup_atp_info_rec.inventory_item_id, p_sup_atp_info_rec.organization_id,
8927                                       p_sup_atp_info_rec.supplier_id, p_sup_atp_info_rec.supplier_site_id,
8928                                       cs.seq_num - p_sup_atp_info_rec.sysdate_seq_num),0))*
8929 		   /*NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(l_instance_id,
8930                                             s.inventory_item_id,
8931                                             p_sup_atp_info_rec.organization_id,
8932                                             null,
8933                                             null,
8934                                             l_demand_class,
8935                                             c.calendar_date), 1) quantity*/
8936                     NVL(MIHM.allocation_percent/100, 1) quantity --4365873
8937          FROM
8938       (
8939       SELECT
8940       	    c.calendar_date,
8941       	    c.seq_num,
8942 	    s.inventory_item_id,
8943 	    s.sr_instance_id,
8944 	    S.ORGANIZATION_ID,
8945 	    S.capacity
8946          FROM   msc_calendar_dates c,
8947                 msc_supplier_capacities s
8948          WHERE  s.inventory_item_id = p_sup_atp_info_rec.inventory_item_id
8949          AND    s.sr_instance_id = p_sup_atp_info_rec.instance_id
8950          AND    s.plan_id = p_sup_atp_info_rec.plan_id
8951          AND    s.organization_id = p_sup_atp_info_rec.organization_id
8952          AND    s.supplier_id = p_sup_atp_info_rec.supplier_id
8953          AND    NVL(s.supplier_site_id, -1) = NVL(p_sup_atp_info_rec.supplier_site_id, -1)
8954          AND    c.calendar_date BETWEEN trunc(s.from_date)
8955                                 --AND NVL(s.to_date,l_cutoff_date)
8956                                 AND trunc(NVL(s.to_date,least(p_sup_atp_info_rec.last_cap_date,l_cutoff_date))) --4055719
8957          AND    (c.seq_num IS NOT NULL or p_sup_atp_info_rec.manufacturing_cal_code = '@@@')
8958          AND    c.calendar_code = l_calendar_code
8959          AND    c.exception_set_id = l_calendar_exception_set_id
8960          AND    c.sr_instance_id = l_instance_id
8961          AND    c.calendar_date >= NVL(p_sup_atp_info_rec.sup_cap_cum_date, l_plan_start_date))CS,
8962       msc_item_hierarchy_mv mihm
8963       WHERE
8964       --4365873
8965       	     CS.INVENTORY_ITEM_ID = MIHM.INVENTORY_ITEM_ID(+)
8966       AND    CS.SR_INSTANCE_ID = MIHM.SR_INSTANCE_ID (+)
8967       AND    CS.ORGANIZATION_ID = MIHM.ORGANIZATION_ID (+)
8968       AND    decode(MIHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
8969       AND    CS.calendar_date >= MIHM.effective_date (+)
8970       AND    CS.calendar_date <= MIHM.disable_date (+)
8971       AND    MIHM.demand_class (+) = l_demand_class
8972          -- Supplier Capacity (SCLT) Accumulation starts from this date.
8973          -- AND    c.calendar_date >= l_plan_start_date -- bug 1169467
8974          UNION ALL
8975          SELECT GREATEST(trunc(Decode(p_sup_atp_info_rec.sup_cap_type,  --4135752
8976                    1, p.new_ship_date,
8977                    p.new_dock_date
8978                 )),l_sys_next_date) l_date, --bug3333114
8979                 -- ship_rec_cal rearrange signs to get rid of multiply times -1
8980                 (NVL(p.implement_quantity,0) - p.new_order_quantity)*
8981 		   DECODE(DECODE(G_HIERARCHY_PROFILE,
8982                                  --2424357
8983                                  1, DECODE(p.DEMAND_CLASS, null, null,
8984                                        DECODE(l_demand_class, '-1',
8985                                            MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
8986                                                      null,
8987                                                      null,
8988                                                      p.inventory_item_id,
8989                                                      p.organization_id,
8990                                                      p.sr_instance_id,
8991                                                      trunc(Decode(p_sup_atp_info_rec.sup_cap_type,  --4135752
8992                                                            1, p.new_ship_date,
8993                                                            p.new_dock_date
8994                                                      )),
8995                                                      l_level_id,
8996                                                      p.DEMAND_CLASS), p.DEMAND_CLASS)),
8997                                  2, DECODE(p.CUSTOMER_ID, NULL, TO_CHAR(NULL),
8998                                            0, TO_CHAR(NULL),
8999                                                    MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
9000                                                      p.CUSTOMER_ID,
9001                                                      p.SHIP_TO_SITE_ID,
9002                                                      p.inventory_item_id,
9003                                                      p.organization_id,
9004                                                      p.sr_instance_id,
9005                                                      trunc(Decode(p_sup_atp_info_rec.sup_cap_type,  --4135752
9006                                                            1, p.new_ship_date,
9007                                                            p.new_dock_date
9008                                                      )),
9009                                                      l_level_id,
9010                                                      NULL))),
9011 		           l_demand_class, 1,
9012 			   NULL, NVL(MIHM.allocation_percent/100,  --4365874
9013 			   /*NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(p.sr_instance_id,
9014                                                           p.inventory_item_id ,
9015 				                          p.organization_id,
9016                                                           null,
9017                                                           null,
9018 				                          l_demand_class,
9019                                                           trunc(Decode(p_sup_atp_info_rec.sup_cap_type,  --4135752
9020                                                              1, p.new_ship_date,
9021                                                              p.new_dock_date
9022                                                           ))),*/
9023                                                            1), --4135752
9024                            DECODE(MIHM.allocation_percent/100,
9025 			   /*DECODE(MSC_AATP_FUNC.Get_DC_Alloc_Percent(p.sr_instance_id,
9026                                                        p.inventory_item_id,
9027                                                        p.organization_id,
9028                                                        null,
9029                                                        null,
9030                                                        l_demand_class,
9031                                                        trunc(Decode(p_sup_atp_info_rec.sup_cap_type, --4135752
9032                                                              1, p.new_ship_date,
9033                                                              p.new_dock_date
9034                                                        ))), */
9035 				   NULL, 1,
9036 				   0)) quantity
9037          -- Supplier Capacity (SCLT) Changes Begin
9038          FROM   msc_supplies p,msc_item_hierarchy_mv MIHM
9039          WHERE  (p.order_type IN (5, 2)
9040                 OR (MSC_ATP_REQ.G_PURCHASE_ORDER_PREFERENCE = MSC_ATP_REQ.G_PROMISE_DATE
9041                      AND p.order_type = 1 AND p.promised_date IS NULL))
9042          -- Supplier Capacity (SCLT) Accumulation Ignore Purchase Orders
9043          -- WHERE  p.order_type IN (5, 1, 2)
9044          AND    p.plan_id = p_sup_atp_info_rec.plan_id
9045          AND    p.sr_instance_id = p_sup_atp_info_rec.instance_id
9046          AND    p.inventory_item_id = p_sup_atp_info_rec.inventory_item_id
9047    -- 1214694      AND    p.organization_id = p_sup_atp_info_rec.organization_id
9048          AND    p.supplier_id  = p_sup_atp_info_rec.supplier_id
9049          AND    NVL(p.supplier_site_id, -1) = NVL(p_sup_atp_info_rec.supplier_site_id, -1)
9050                 -- Exclude Cancelled Supplies 2460645
9051          AND    NVL(P.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
9052          AND    trunc(Decode(p_sup_atp_info_rec.sup_cap_type, 1, p.new_ship_date,p.new_dock_date)) --4055719 --4135752
9053                        <= least(p_sup_atp_info_rec.last_cap_date,l_cutoff_date)
9054       --4365873
9055       AND    p.INVENTORY_ITEM_ID = MIHM.INVENTORY_ITEM_ID(+)
9056       AND    p.SR_INSTANCE_ID = MIHM.SR_INSTANCE_ID (+)
9057       AND    p.ORGANIZATION_ID = MIHM.ORGANIZATION_ID (+)
9058       AND    decode(MIHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
9059       AND    trunc(Decode(p_sup_atp_info_rec.sup_cap_type, 1, p.new_ship_date,p.new_dock_date)) >= MIHM.effective_date (+)
9060       AND    trunc(Decode(p_sup_atp_info_rec.sup_cap_type, 1, p.new_ship_date,p.new_dock_date)) <= MIHM.disable_date (+)
9061       AND    MIHM.demand_class (+) = l_demand_class
9062          )
9063          GROUP BY l_date
9064          ORDER BY l_DATE;--4698199
9065          --- bug 1657855, remove support for min alloc
9066          --l_current_atp.limit_qty := l_current_atp.atp_qty;
9067       END IF;
9068     ELSE
9069         --IF (NVL(p_sup_atp_info_rec.insert_flag, 0) <> 0  AND
9070         -- l_demand_class <> p_sup_atp_info_rec.demand_class) THEN we want details
9071 	MSC_ATP_DB_UTILS.Clear_SD_Details_Temp();
9072 
9073         IF l_check_cap_model_flag = 1 THEN
9074 
9075            -- dsting: s/d details performance enh
9076            INSERT INTO msc_atp_sd_details_temp (
9077            	ATP_Level,
9078            	Order_line_id,
9079            	Scenario_Id,
9080            	Inventory_Item_Id,
9081            	Request_Item_Id,
9082 	        Organization_Id,
9083                 Department_Id,
9084 	        Resource_Id,
9085                 Supplier_Id,
9086                 Supplier_Site_Id,
9087 	        From_Organization_Id,
9088 	        From_Location_Id,
9089 	        To_Organization_Id,
9090 	        To_Location_Id,
9091 	        Ship_Method,
9092 	        UOM_code,
9093 	        Supply_Demand_Type,
9094 	        Supply_Demand_Source_Type,
9095 	        Supply_Demand_Source_Type_Name,
9096 	        Identifier1,
9097 	        Identifier2,
9098 	        Identifier3,
9099 	        Identifier4,
9100 	        Supply_Demand_Quantity,
9101 	        Supply_Demand_Date,
9102 	        Disposition_Type,
9103 	        Disposition_Name,
9104 	        Pegging_Id,
9105 	        End_Pegging_Id,
9106 	        creation_date,
9107 	        created_by,
9108 	        last_update_date,
9109 	        last_updated_by,
9110 	        last_update_login,
9111 	        Unallocated_Quantity
9112             )
9113 
9114             (SELECT     p_sup_atp_info_rec.level col1,
9115 		p_identifier col2,
9116                 p_sup_atp_info_rec.scenario_id col3,
9117                 l_null_num col4 ,
9118                 l_null_num col5,
9119 		p_sup_atp_info_rec.organization_id col6,
9120                 l_null_num col7,
9121                 l_null_num col8,
9122                 p_sup_atp_info_rec.supplier_id col9,
9123                 p_sup_atp_info_rec.supplier_site_id col10,
9124                 l_null_num col11,
9125                 l_null_num col12,
9126                 l_null_num col13,
9127                 l_null_num col14,
9128 		l_null_char col15,
9129 		l_uom_code col16,
9130 		2 col17, -- supply
9131 		l_null_num col18,
9132                 l_null_char col19,
9133 		p_sup_atp_info_rec.instance_id col20,
9134                 l_null_num col21,
9135 		l_null_num col22,
9136 		l_null_num col23,
9137                 cs.capacity*(1+NVL(MSC_ATP_FUNC.get_tolerance_percentage(
9138                                    p_sup_atp_info_rec.instance_id,
9139                                    p_sup_atp_info_rec.plan_id,
9140                                    l_inv_item_id,
9141                                    p_sup_atp_info_rec.organization_id,
9142                                    p_sup_atp_info_rec.supplier_id,
9143                                    p_sup_atp_info_rec.supplier_site_id,
9144                                    cs.seq_num - p_sup_atp_info_rec.sysdate_seq_num),0)) *
9145                                    NVL(MIHM.allocation_percent/100,  --4365873
9146                                    /*NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
9147                                                      l_instance_id,
9148                                                      s.inventory_item_id,
9149                                                      p_sup_atp_info_rec.organization_id,
9150                                                      null,
9151                                                      null,
9152                                                      l_demand_class,
9153                                                      c.calendar_date),*/
9154                                                       1) col24,
9155 		GREATEST(CS.CALENDAR_DATE,l_sys_next_date) col25, --bug3333114
9156                 l_null_num col26,
9157                 l_null_char col27,
9158                 l_null_num col28,
9159                 l_null_num col29,
9160 		-- ship_rec_cal changes begin
9161 		l_sysdate,
9162 		G_USER_ID,
9163 		l_sysdate,
9164 		G_USER_ID,
9165 		G_USER_ID,
9166 		-- ship_rec_cal changes end
9167 		-- Unallocated_Quantity
9168                 cs.capacity*(1+NVL(MSC_ATP_FUNC.get_tolerance_percentage(
9169                                    p_sup_atp_info_rec.instance_id,
9170                                    p_sup_atp_info_rec.plan_id,
9171                                    l_inv_item_id,
9172                                    p_sup_atp_info_rec.organization_id,
9173                                    p_sup_atp_info_rec.supplier_id,
9174                                    p_sup_atp_info_rec.supplier_site_id,
9175                                    cs.seq_num - p_sup_atp_info_rec.sysdate_seq_num),0))
9176          FROM
9177          (
9178          SELECT
9179             s.capacity,
9180 	    c.calendar_date,
9181 	    s.inventory_item_id,
9182 	    s.sr_instance_id,
9183 	    s.organization_id,
9184 	    c.seq_num
9185 
9186                 FROM   msc_calendar_dates c,
9187                        msc_supplier_capacities s
9188                 --WHERE  s.inventory_item_id = p_sup_atp_info_rec.inventory_item_id
9189                 WHERE  s.inventory_item_id = l_inv_item_id
9190                 AND    s.sr_instance_id = p_sup_atp_info_rec.instance_id
9191                 AND    s.plan_id = p_sup_atp_info_rec.plan_id
9192                 AND    s.organization_id = p_sup_atp_info_rec.organization_id
9193                 AND    s.supplier_id = p_sup_atp_info_rec.supplier_id
9194                 AND    NVL(s.supplier_site_id, -1) = NVL(p_sup_atp_info_rec.supplier_site_id, -1)
9195                 AND    c.calendar_date BETWEEN trunc(s.from_date)
9196                                 --AND NVL(s.to_date,l_cutoff_date)
9197                                 AND trunc(NVL(s.to_date,least(p_sup_atp_info_rec.last_cap_date,l_cutoff_date))) --4055719
9198                 AND    (c.seq_num IS NOT NULL or p_sup_atp_info_rec.manufacturing_cal_code = '@@@')
9199                 AND    c.calendar_code = l_calendar_code
9200                 AND    c.exception_set_id = l_calendar_exception_set_id
9201                 AND    c.sr_instance_id = l_instance_id
9202                 AND    c.calendar_date >= NVL(p_sup_atp_info_rec.sup_cap_cum_date, l_plan_start_date))CS,
9203          msc_item_hierarchy_mv MIHM
9204          WHERE
9205          --4365873
9206                 cs.INVENTORY_ITEM_ID = MIHM.INVENTORY_ITEM_ID(+)
9207       	 AND    cs.SR_INSTANCE_ID = MIHM.SR_INSTANCE_ID (+)
9208       	 AND    cs.ORGANIZATION_ID = MIHM.ORGANIZATION_ID (+)
9209       	 --AND    decode(MIHM.level_id,-1,1,2) (+) = (select decode(fnd_profile.value('XXXX'),1,1,2) from dual)
9210       	 --AND   MIHM.level_id(+)=-1
9211       	 AND    decode(MIHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
9212       	 AND    cs.calendar_date >= MIHM.effective_date (+)
9213       	 AND    cs.calendar_date <= MIHM.disable_date (+)
9214       	 AND    MIHM.demand_class (+) = l_demand_class
9215                 -- Supplier Capacity (SCLT) Accumulation starts from this date.
9216                 -- AND    c.calendar_date >= l_plan_start_date -- bug 1169467
9217                 UNION ALL
9218                 SELECT p_sup_atp_info_rec.level col1,
9219                 p_identifier col2,
9220                 p_sup_atp_info_rec.scenario_id col3,
9221                 l_null_num col4 ,
9222                 l_null_num col5,
9223                 p_sup_atp_info_rec.organization_id col6,
9224                 l_null_num col7,
9225                 l_null_num col8,
9226                 p_sup_atp_info_rec.supplier_id col9,
9227                 p_sup_atp_info_rec.supplier_site_id col10,
9228                 l_null_num col11,
9229                 l_null_num col12,
9230                 l_null_num col13,
9231                 l_null_num col14,
9232                 l_null_char col15,
9233                 l_uom_code col16,
9234                 1 col17, -- demand
9235                 p.order_type col18,
9236                 l_null_char col19,
9237                 p_sup_atp_info_rec.instance_id col20,
9238                 l_null_num col21,
9239                 p.TRANSACTION_ID col22,
9240                 l_null_num col23,
9241                 -- ship_rec_cal rearrange signs to get rid of multiply times -1
9242                 (NVL(p.implement_quantity,0) - p.new_order_quantity)*
9243                 DECODE(DECODE(G_HIERARCHY_PROFILE,
9244                               --2424357
9245                               1, DECODE(p.DEMAND_CLASS, null, null,
9246                                     DECODE(l_demand_class, '-1',
9247                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
9248                                                   null,
9249                                                   null,
9250                                                   p.inventory_item_id,
9251                                                   p.organization_id,
9252                                                   p.sr_instance_id,
9253                                                   trunc(Decode(p_sup_atp_info_rec.sup_cap_type, --4135752
9254                                                      1, p.new_ship_date,
9255                                                      p.new_dock_date
9256                                                   )),
9257                                                   l_level_id,
9258                                                    p.DEMAND_CLASS), p.DEMAND_CLASS)),
9259                               2, DECODE(p.CUSTOMER_ID, NULL, TO_CHAR(NULL),
9260                                         0, TO_CHAR(NULL),
9261                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
9262                                                   p.CUSTOMER_ID,
9263                                                   p.SHIP_TO_SITE_ID,
9264                                                   p.inventory_item_id,
9265                                                   p.organization_id,
9266                                                   p.sr_instance_id,
9267                                                   trunc(Decode(p_sup_atp_info_rec.sup_cap_type, --4135752
9268                                                      1, p.new_ship_date,
9269                                                      p.new_dock_date
9270                                                   )),
9271                                                   l_level_id,
9272                                                   NULL))),
9273                         l_demand_class, 1,
9274                         NULL, NVL(MIHM.allocation_percent/100,  --4365873
9275                         /*NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(p.sr_instance_id,
9276                                                        p.inventory_item_id ,
9277                                                        p.organization_id,
9278                                                        null,
9279                                                        null,
9280                                                        l_demand_class,
9281                                                        trunc(Decode(p_sup_atp_info_rec.sup_cap_type, --4135752
9282                                                           1, p.new_ship_date,
9283                                                           p.new_dock_date
9284                                                        ))),*/
9285                                                         1),
9286                         DECODE(MIHM.allocation_percent/100,
9287                         /*DECODE(MSC_AATP_FUNC.Get_DC_Alloc_Percent(p.sr_instance_id,
9288                                                     p.inventory_item_id,
9289                                                     p.organization_id,
9290                                                     null,
9291                                                     null,
9292                                                     l_demand_class,
9293                                                     trunc(Decode(p_sup_atp_info_rec.sup_cap_type, --4135752
9294                                                        1, p.new_ship_date,
9295                                                        p.new_dock_date
9296                                                     ))),*/
9297                                 NULL, 1,
9298                                 0)) col24,
9299                 GREATEST(trunc(Decode(p_sup_atp_info_rec.sup_cap_type,  --4135752
9300                    1, p.new_ship_date,
9301                    p.new_dock_date
9302                 )),l_sys_next_date) col25, --bug3333114
9303                 l_null_num col26,
9304                 -- Bug 2771075. For Planned Orders, we will populate transaction_id
9305 		-- in the disposition_name column to be consistent with Planning.
9306 		-- p.order_number col27,
9307 		DECODE(p.order_type, 5, to_char(p.transaction_id), p.order_number ) col27,
9308                 l_null_num col28,
9309 		l_null_num col29,
9310 		-- ship_rec_cal changes begin
9311 		l_sysdate,
9312 		G_USER_ID,
9313 		l_sysdate,
9314 		G_USER_ID,
9315 		G_USER_ID,
9316 		-- ship_rec_cal changes end
9317 		-- Unallocated_Quantity
9318                 -- ship_rec_cal rearrange signs to get rid of multiply times -1
9319                 (NVL(p.implement_quantity,0) - p.new_order_quantity)
9320              -- Supplier Capacity (SCLT) Changes Begin
9321              FROM   msc_supplies p,msc_item_hierarchy_mv MIHM
9322              WHERE  (p.order_type IN (5, 2)
9323                     OR (MSC_ATP_REQ.G_PURCHASE_ORDER_PREFERENCE = MSC_ATP_REQ.G_PROMISE_DATE
9324                   AND p.order_type = 1 AND p.promised_date IS NULL))
9325 
9326              -- Supplier Capacity (SCLT) Accumulation Ignore Purchase Orders
9327              -- WHERE  p.order_type IN (5, 1, 2)
9328              AND    p.plan_id = p_sup_atp_info_rec.plan_id
9329              AND    p.sr_instance_id = p_sup_atp_info_rec.instance_id
9330              --AND    p.inventory_item_id = p_sup_atp_info_rec.inventory_item_id
9331        -- 1214694      AND    p.organization_id = p_sup_atp_info_rec.organization_id
9332              AND    p.supplier_id  = p_sup_atp_info_rec.supplier_id
9333              AND    NVL(p.supplier_site_id, -1) = NVL(p_sup_atp_info_rec.supplier_site_id, -1)
9334              -- Exclude Cancelled Supplies 2460645
9335              AND    NVL(P.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
9336                     --consider ATP inserted POs only. Do not use Planning inserted POs as
9337                     -- theu would be tied to forecast
9338              AND    ((p.inventory_item_id = l_inv_item_id and p.record_source=2) OR
9339                     p.inventory_item_id in
9340                            (select inventory_item_id from msc_system_items msi
9341                             where  msi.base_item_id = l_inv_item_id
9342                             and    msi.plan_id = p_sup_atp_info_rec.plan_id
9343                             and    msi.organization_id = p_sup_atp_info_rec.organization_id
9344                             and    msi.base_item_id = l_inv_item_id))
9345              AND    trunc(Decode(p_sup_atp_info_rec.sup_cap_type, 1, p.new_ship_date,p.new_dock_date)) --4055719 --4135752
9346                       <= least(p_sup_atp_info_rec.last_cap_date,l_cutoff_date)
9347       --4365873
9348       AND    p.INVENTORY_ITEM_ID = MIHM.INVENTORY_ITEM_ID(+)
9349       AND    p.SR_INSTANCE_ID = MIHM.SR_INSTANCE_ID (+)
9350       AND    p.ORGANIZATION_ID = MIHM.ORGANIZATION_ID (+)
9351       --AND    MIHM.level_id (+) = decode(G_HIERARCHY_PROFILE,1,-1 )
9352       AND    decode(MIHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
9353       AND    trunc(Decode(p_sup_atp_info_rec.sup_cap_type, 1, p.new_ship_date,p.new_dock_date)) >= MIHM.effective_date (+)
9354       AND    trunc(Decode(p_sup_atp_info_rec.sup_cap_type, 1, p.new_ship_date,p.new_dock_date)) <= MIHM.disable_date (+)
9355       AND    MIHM.demand_class (+) = l_demand_class
9356        )
9357        ; -- dsting removed order by col25;
9358         ELSE
9359 
9360            -- dsting: s/d details performance enh
9361            INSERT INTO msc_atp_sd_details_temp (
9362            	ATP_Level,
9363            	Order_line_id,
9364            	Scenario_Id,
9365            	Inventory_Item_Id,
9366            	Request_Item_Id,
9367 	        Organization_Id,
9368                 Department_Id,
9369 	        Resource_Id,
9370                 Supplier_Id,
9371                 Supplier_Site_Id,
9372 	        From_Organization_Id,
9373 	        From_Location_Id,
9374 	        To_Organization_Id,
9375 	        To_Location_Id,
9376 	        Ship_Method,
9377 	        UOM_code,
9378 	        Supply_Demand_Type,
9379 	        Supply_Demand_Source_Type,
9380 	        Supply_Demand_Source_Type_Name,
9381 	        Identifier1,
9382 	        Identifier2,
9383 	        Identifier3,
9384 	        Identifier4,
9385 	        Supply_Demand_Quantity,
9386 	        Supply_Demand_Date,
9387 	        Disposition_Type,
9388 	        Disposition_Name,
9389 	        Pegging_Id,
9390 	        End_Pegging_Id,
9391 	        creation_date,
9392 	        created_by,
9393 	        last_update_date,
9394 	        last_updated_by,
9395 	        last_update_login,
9396 	        Unallocated_Quantity
9397             )
9398 
9399             (SELECT     p_sup_atp_info_rec.level col1,
9400 		p_identifier col2,
9401                 p_sup_atp_info_rec.scenario_id col3,
9402                 l_null_num col4 ,
9403                 l_null_num col5,
9404 		p_sup_atp_info_rec.organization_id col6,
9405                 l_null_num col7,
9406                 l_null_num col8,
9407                 p_sup_atp_info_rec.supplier_id col9,
9408                 p_sup_atp_info_rec.supplier_site_id col10,
9409                 l_null_num col11,
9410                 l_null_num col12,
9411                 l_null_num col13,
9412                 l_null_num col14,
9413 		l_null_char col15,
9414 		l_uom_code col16,
9415 		2 col17, -- supply
9416 		l_null_num col18,
9417                 l_null_char col19,
9418 		p_sup_atp_info_rec.instance_id col20,
9419                 l_null_num col21,
9420 		l_null_num col22,
9421 		l_null_num col23,
9422                 cs.capacity*(1+NVL(MSC_ATP_FUNC.get_tolerance_percentage(
9423                                    p_sup_atp_info_rec.instance_id,
9424                                    p_sup_atp_info_rec.plan_id,
9425                                    p_sup_atp_info_rec.inventory_item_id,
9426                                    p_sup_atp_info_rec.organization_id,
9427                                    p_sup_atp_info_rec.supplier_id,
9428                                    p_sup_atp_info_rec.supplier_site_id,
9429                                    cs.seq_num - p_sup_atp_info_rec.sysdate_seq_num),0)) *
9430                                    NVL(MIHM.allocation_percent/100,  --4365873
9431                                    /*NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
9432                                                      l_instance_id,
9433                                                      cs.inventory_item_id,
9434                                                      p_sup_atp_info_rec.organization_id,
9435                                                      null,
9436                                                      null,
9437                                                      l_demand_class,
9438                                                      c.calendar_date),*/
9439                                                       1) col24,
9440 		GREATEST(CS.CALENDAR_DATE,l_sys_next_date) col25, --bug3333114
9441                 l_null_num col26,
9442                 l_null_char col27,
9443                 l_null_num col28,
9444                 l_null_num col29,
9445 		-- ship_rec_cal changes begin
9446 		l_sysdate,
9447 		G_USER_ID,
9448 		l_sysdate,
9449 		G_USER_ID,
9450 		G_USER_ID,
9451 		-- ship_rec_cal changes end
9452 		-- Unallocated_Quantity
9453                 cs.capacity*(1+NVL(MSC_ATP_FUNC.get_tolerance_percentage(
9454                                    p_sup_atp_info_rec.instance_id,
9455                                    p_sup_atp_info_rec.plan_id,
9456                                    p_sup_atp_info_rec.inventory_item_id,
9457                                    p_sup_atp_info_rec.organization_id,
9458                                    p_sup_atp_info_rec.supplier_id,
9459                                    p_sup_atp_info_rec.supplier_site_id,
9460                                    cs.seq_num - p_sup_atp_info_rec.sysdate_seq_num),0))
9461          FROM
9462          (
9463          SELECT
9464             s.capacity,
9465 	    c.calendar_date,
9466 	    s.inventory_item_id,
9467 	    s.sr_instance_id,
9468 	    s.organization_id,
9469 	    c.seq_num
9470                 FROM   msc_calendar_dates c,
9471                        msc_supplier_capacities s
9472                 WHERE  s.inventory_item_id = p_sup_atp_info_rec.inventory_item_id
9473                 AND    s.sr_instance_id = p_sup_atp_info_rec.instance_id
9474                 AND    s.plan_id = p_sup_atp_info_rec.plan_id
9475                 AND    s.organization_id = p_sup_atp_info_rec.organization_id
9476                 AND    s.supplier_id = p_sup_atp_info_rec.supplier_id
9477                 AND    NVL(s.supplier_site_id, -1) = NVL(p_sup_atp_info_rec.supplier_site_id, -1)
9478                 AND    c.calendar_date BETWEEN trunc(s.from_date)
9479                                 --AND NVL(s.to_date,l_cutoff_date)
9480                                 AND trunc(NVL(s.to_date,least(p_sup_atp_info_rec.last_cap_date,l_cutoff_date))) --4055719
9481                 AND    (c.seq_num IS NOT NULL or p_sup_atp_info_rec.manufacturing_cal_code = '@@@')
9482                 AND    c.calendar_code = l_calendar_code
9483                 AND    c.exception_set_id = l_calendar_exception_set_id
9484                 AND    c.sr_instance_id = l_instance_id
9485                 AND    c.calendar_date >= NVL(p_sup_atp_info_rec.sup_cap_cum_date, l_plan_start_date))CS,
9486          msc_item_hierarchy_mv MIHM
9487          WHERE
9488          --4365873
9489                 cs.INVENTORY_ITEM_ID = MIHM.INVENTORY_ITEM_ID(+)
9490       	 AND    cs.SR_INSTANCE_ID = MIHM.SR_INSTANCE_ID (+)
9491       	 AND    cs.ORGANIZATION_ID = MIHM.ORGANIZATION_ID (+)
9492       	 --AND    decode(MIHM.level_id,-1,1,2) (+) = (select decode(fnd_profile.value('XXXX'),1,1,2) from dual)
9493       	 --AND   MIHM.level_id(+)=-1
9494       	 AND    decode(MIHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
9495       	 AND    cs.calendar_date >= MIHM.effective_date (+)
9496       	 AND    cs.calendar_date <= MIHM.disable_date (+)
9497       	 AND    MIHM.demand_class (+) = l_demand_class
9498                 -- Supplier Capacity (SCLT) Accumulation starts from this date.
9499                 -- AND    c.calendar_date >= l_plan_start_date -- bug 1169467
9500                 UNION ALL
9501                 SELECT p_sup_atp_info_rec.level col1,
9502                 p_identifier col2,
9503                 p_sup_atp_info_rec.scenario_id col3,
9504                 l_null_num col4 ,
9505                 l_null_num col5,
9506                 p_sup_atp_info_rec.organization_id col6,
9507                 l_null_num col7,
9508                 l_null_num col8,
9509                 p_sup_atp_info_rec.supplier_id col9,
9510                 p_sup_atp_info_rec.supplier_site_id col10,
9511                 l_null_num col11,
9512                 l_null_num col12,
9513                 l_null_num col13,
9514                 l_null_num col14,
9515                 l_null_char col15,
9516                 l_uom_code col16,
9517                 1 col17, -- demand
9518                 p.order_type col18,
9519                 l_null_char col19,
9520                 p_sup_atp_info_rec.instance_id col20,
9521                 l_null_num col21,
9522                 TRANSACTION_ID col22,
9523                 l_null_num col23,
9524                 -- ship_rec_cal rearrange signs to get rid of multiply times -1
9525                 (NVL(p.implement_quantity,0) - p.new_order_quantity)*
9526                 DECODE(DECODE(G_HIERARCHY_PROFILE,
9527                               --2424357
9528                               1, DECODE(p.DEMAND_CLASS, null, null,
9529                                     DECODE(l_demand_class, '-1',
9530                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
9531                                                   null,
9532                                                   null,
9533                                                   p.inventory_item_id,
9534                                                   p.organization_id,
9535                                                   p.sr_instance_id,
9536                                                   trunc(Decode(p_sup_atp_info_rec.sup_cap_type, --4135752
9537                                                           1, p.new_ship_date,
9538                                                           p.new_dock_date
9539                                                   )),
9540                                                   l_level_id,
9541                                                    p.DEMAND_CLASS), p.DEMAND_CLASS)),
9542                               2, DECODE(p.CUSTOMER_ID, NULL, TO_CHAR(NULL),
9543                                         0, TO_CHAR(NULL),
9544                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
9545                                                   p.CUSTOMER_ID,
9546                                                   p.SHIP_TO_SITE_ID,
9547                                                   p.inventory_item_id,
9548                                                   p.organization_id,
9549                                                   p.sr_instance_id,
9550                                                   trunc(Decode(p_sup_atp_info_rec.sup_cap_type, --4135752
9551                                                           1, p.new_ship_date,
9552                                                           p.new_dock_date
9553                                                   )),
9554                                                   l_level_id,
9555                                                   NULL))),
9556                         l_demand_class, 1,
9557                         NULL, NVL(MIHM.allocation_percent/100,  --4365873
9558                         /*NULL, NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(p.sr_instance_id,
9559                                                        p.inventory_item_id ,
9560                                                        p.organization_id,
9561                                                        null,
9562                                                        null,
9563                                                        l_demand_class,
9564                                                        trunc(Decode(p_sup_atp_info_rec.sup_cap_type, --4135752
9565                                                           1, p.new_ship_date,
9566                                                           p.new_dock_date
9567                                                        ))),*/
9568                                                         1),
9569                         DECODE(MIHM.allocation_percent/100,
9570                         /*DECODE(MSC_AATP_FUNC.Get_DC_Alloc_Percent(p.sr_instance_id,
9571                                                     p.inventory_item_id,
9572                                                     p.organization_id,
9573                                                     null,
9574                                                     null,
9575                                                     l_demand_class,
9576                                                     trunc(Decode(p_sup_atp_info_rec.sup_cap_type, --4135752
9577                                                           1, p.new_ship_date,
9578                                                           p.new_dock_date
9579                                                     ))),*/
9580                                 NULL, 1,
9581                                 0)) col24,
9582                 GREATEST(trunc(Decode(p_sup_atp_info_rec.sup_cap_type, --4135752
9583                     1, p.new_ship_date,
9584                     p.new_dock_date
9585                 )),l_sys_next_date) col25, --bug3333114
9586                 l_null_num col26,
9587                 -- Bug 2771075. For Planned Orders, we will populate transaction_id
9588 		-- in the disposition_name column to be consistent with Planning.
9589 		-- p.order_number col27,
9590 		DECODE(p.order_type, 5, to_char(p.transaction_id), p.order_number ) col27,
9591                 l_null_num col28,
9592 		l_null_num col29,
9593 		-- ship_rec_cal changes begin
9594 		l_sysdate,
9595 		G_USER_ID,
9596 		l_sysdate,
9597 		G_USER_ID,
9598 		G_USER_ID,
9599 		-- ship_rec_cal changes end
9600 		-- Unallocated_Quantity
9601                 -- ship_rec_cal rearrange signs to get rid of multiply times -1
9602                 (NVL(p.implement_quantity,0) - p.new_order_quantity)
9603              -- Supplier Capacity (SCLT) Changes Begin
9604              FROM   msc_supplies p ,msc_item_hierarchy_mv MIHM
9605              WHERE  (p.order_type IN (5, 2)
9606                     OR (MSC_ATP_REQ.G_PURCHASE_ORDER_PREFERENCE = MSC_ATP_REQ.G_PROMISE_DATE
9607                   AND p.order_type = 1 AND p.promised_date IS NULL))
9608 
9609              -- Supplier Capacity (SCLT) Accumulation Ignore Purchase Orders
9610              -- WHERE  p.order_type IN (5, 1, 2)
9611              AND    p.plan_id = p_sup_atp_info_rec.plan_id
9612              AND    p.sr_instance_id = p_sup_atp_info_rec.instance_id
9613              AND    p.inventory_item_id = p_sup_atp_info_rec.inventory_item_id
9614        -- 1214694      AND    p.organization_id = p_sup_atp_info_rec.organization_id
9615              AND    p.supplier_id  = p_sup_atp_info_rec.supplier_id
9616              AND    NVL(p.supplier_site_id, -1) = NVL(p_sup_atp_info_rec.supplier_site_id, -1)
9617              -- Exclude Cancelled Supplies 2460645
9618              AND    NVL(P.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
9619              AND    trunc(Decode(p_sup_atp_info_rec.sup_cap_type, 1, p.new_ship_date,p.new_dock_date)) --4055719--4135752
9620                            <= least(p_sup_atp_info_rec.last_cap_date,l_cutoff_date)
9621              --4365873
9622       AND    p.INVENTORY_ITEM_ID = MIHM.INVENTORY_ITEM_ID(+)
9623       AND    p.SR_INSTANCE_ID = MIHM.SR_INSTANCE_ID (+)
9624       AND    p.ORGANIZATION_ID = MIHM.ORGANIZATION_ID (+)
9625       --AND    MIHM.level_id (+) = decode(G_HIERARCHY_PROFILE,1,-1 )
9626       AND    decode(MIHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
9627       AND    trunc(Decode(p_sup_atp_info_rec.sup_cap_type, 1, p.new_ship_date,p.new_dock_date)) >= MIHM.effective_date (+)
9628       AND    trunc(Decode(p_sup_atp_info_rec.sup_cap_type, 1, p.new_ship_date,p.new_dock_date)) <= MIHM.disable_date (+)
9629       AND    MIHM.demand_class (+) = l_demand_class
9630        )
9631        ; -- dsting removed order by col25;
9632      END IF;
9633      --=======================================================================================================
9634      -- ship_rec_cal changes end
9635      --=======================================================================================================
9636      -- for period ATP
9637      IF PG_DEBUG in ('Y', 'C') THEN
9638         msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'after insert into msc_atp_sd_details_temp');
9639      END IF;
9640      MSC_ATP_PROC.get_period_data_from_SD_temp(x_atp_period);
9641 
9642       l_current_atp.atp_period := x_atp_period.Period_Start_Date;
9643       l_current_atp.atp_qty := x_atp_period.Period_Quantity;
9644       -- bug 1657855, remove support for min alloc
9645       --l_current_atp.limit_qty := l_current_atp.atp_qty; -- 02/16
9646 
9647     END IF;
9648 
9649     IF PG_DEBUG in ('Y', 'C') THEN
9650        msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'right after the big query');
9651        Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
9652 		l_current_atp);
9653     END IF;
9654 
9655     -- do backward consumption for DCi
9656     MSC_ATP_PROC.Atp_Backward_Consume(l_current_atp.atp_qty);
9657 
9658     IF PG_DEBUG in ('Y', 'C') THEN
9659        msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'right after the backward consume');
9660        Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
9661 		l_current_atp);
9662     END IF;
9663 
9664     -- we have 3 records of tables.
9665     -- l_current_atp: stores the date and quantity for this demand class,
9666     --                and since we need to do backward consumption on this.
9667     -- l_current_steal_atp: stores the date and quantity from higher priority
9668     --                      demand class, this need to consume l_current_atp
9669     -- l_next_steal_atp : stores  the date and quantity for next priority
9670     --                    demand class to cunsume.  we need this because we may
9671     --                    have multiple demand classes at same priority .
9672     -- for example, we have DC1 in priority 1, DC21, DC22 in priority 2,
9673     -- DC3 in priority  3.
9674     -- now DC21 need to take care DC1, DC22 need to take care DC1 but not DC21,
9675     -- DC3 need to take care DC1, DC21, and DC22.  so if we are in the loop for
9676     -- DC22, than l_current_atp is the atp info for DC22,
9677     -- l_current_steal_atp is the atp info for DC1(which does not include DC21),
9678     -- and l_next_steal_atp is the stealing data that we need to take care
9679     -- for DC1, DC21 and DC22  when later on we move to the loop for DC3.
9680 
9681        -- do backward consumption if DC1 to DC(i-1) has any negative bucket,and
9682        -- the priority  is higher than DCi
9683        -- the l_current_atp is an in/out parameter
9684 
9685       -- for 1680719, since in hierarchy demand class we cannot
9686       -- judge the priority by just looking at the priority (we need
9687       -- the information from the parent, so the condition needs to be changed.
9688 
9689       IF l_level_id IN (-1, 1) THEN
9690         -- here is the old logic which should still be ok for level id 1 and -1
9691        IF (i > 1) THEN
9692 
9693         IF (l_demand_class_priority_tab(i) >
9694             l_demand_class_priority_tab (i-1)) THEN
9695         -- we don't need to change the l_current_steal_atp if we don't
9696         -- move to next priority.
9697         -- but we do need to change the l_current_steal_atp
9698         -- if we are in different priority  now.
9699 
9700           l_current_steal_atp := l_next_steal_atp;
9701 
9702           -- Added for bug 1409335. Need to initialize l_next_steal_atp
9703           -- otherwise quanities would be getting accumulated
9704           -- repeatedly.
9705           l_next_steal_atp := l_null_steal_atp;
9706         END IF;
9707        END IF;
9708       ELSE -- IF l_level_id IN (-1, 1) THEN
9709 
9710        IF (i > 1) THEN
9711 
9712         IF (l_class_tab(i) <> l_class_tab(i-1)) THEN
9713 
9714           -- class changed.  If priority of both classes are not the same,
9715           -- then we need to change the curr_steal_atp  at class level.
9716 
9717           IF PG_DEBUG in ('Y', 'C') THEN
9718              msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'class changed');
9719           END IF;
9720 
9721           IF trunc(l_demand_class_priority_tab(i), -3) >
9722              trunc(l_demand_class_priority_tab (i-1), -3) THEN
9723 
9724             IF PG_DEBUG in ('Y', 'C') THEN
9725                msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'class priority changed');
9726             END IF;
9727             l_class_curr_steal_atp := l_class_next_steal_atp;
9728             l_class_next_steal_atp := l_null_steal_atp;
9729           END IF;
9730 
9731           l_partner_next_steal_atp := l_null_steal_atp;
9732           l_partner_curr_steal_atp := l_null_steal_atp;
9733           l_partner_next_steal_atp := l_null_steal_atp;
9734           l_current_steal_atp := l_null_steal_atp;
9735           l_next_steal_atp := l_null_steal_atp;
9736 
9737         ELSE
9738           IF PG_DEBUG in ('Y', 'C') THEN
9739              msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'same class');
9740           END IF;
9741           IF (l_partner_tab(i) <> l_partner_tab(i-1)) THEN
9742             -- customer changed.  If priority of both customers are not the
9743             -- same, we need to change the curr_steal_atp  at partner level.
9744 
9745             IF PG_DEBUG in ('Y', 'C') THEN
9746                msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'customer changed');
9747             END IF;
9748 
9749             IF trunc(l_demand_class_priority_tab(i), -2) >
9750                trunc(l_demand_class_priority_tab (i-1), -2) THEN
9751               IF PG_DEBUG in ('Y', 'C') THEN
9752                  msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'customer priority changed');
9753               END IF;
9754 
9755               l_partner_curr_steal_atp := l_partner_next_steal_atp;
9756               l_partner_next_steal_atp := l_null_steal_atp;
9757             END IF;
9758 
9759             l_current_steal_atp := l_null_steal_atp;
9760             l_next_steal_atp := l_null_steal_atp;
9761 
9762 
9763           ELSE
9764             -- same customer
9765             IF PG_DEBUG in ('Y', 'C') THEN
9766                msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'same customer ');
9767             END IF;
9768 
9769             IF (l_demand_class_priority_tab(i) >
9770                 l_demand_class_priority_tab (i-1)) THEN
9771               -- site level priority changed
9772 
9773               IF PG_DEBUG in ('Y', 'C') THEN
9774                  msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'site priority changed');
9775               END IF;
9776               l_current_steal_atp := l_next_steal_atp;
9777               l_next_steal_atp := l_null_steal_atp;
9778 
9779             END IF;
9780           END IF; -- IF (l_partner_tab(i) <> l_partner_tab(i-1))
9781         END IF; -- IF (l_class_tab(i) <> l_class_tab(i-1))
9782 
9783        END IF; -- IF (i > 1)
9784 
9785       END IF; -- IF l_level_id IN (-1, 1)
9786       IF PG_DEBUG in ('Y', 'C') THEN
9787          msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'before we decide we need to do dc consumption');
9788       END IF;
9789       IF (i > 1) THEN
9790        IF (  -- this is the huge condition
9791              ((l_level_id IN (-1, 1)) AND
9792              (l_demand_class_priority_tab(i) <> l_demand_class_priority_tab(1)))
9793            OR
9794              (l_level_id in (2, 3))
9795           ) THEN
9796 
9797         -- we need to do demand class consume only if we are not in the first
9798         -- preferred priority
9799 
9800         -- bug 1413459
9801         -- we need to remember what's the atp picture before the
9802         -- demand class consumption but after it's own backward
9803         -- consumption.  so that we can figure out the stealing
9804         -- quantity correctly.
9805         IF (NVL(p_sup_atp_info_rec.insert_flag, 0) <>0)
9806            AND (l_demand_class_tab(i) = p_sup_atp_info_rec.demand_class) THEN
9807             l_temp_atp := l_current_atp;
9808         END IF;
9809 
9810         -- 1680719
9811         -- since we have hierarchy now, before we do demand class
9812         -- consumption for site level, we need to do the class level and
9813         -- partner level first
9814 
9815         IF l_level_id IN (2,3) THEN
9816 
9817           IF l_class_tab(i) <> l_class_tab(1) THEN
9818 
9819             IF PG_DEBUG in ('Y', 'C') THEN
9820                msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'before consume l_class_curr_steal_atp');
9821                Print_Period_Qty('l_class_curr_steal_atp.atp_period:atp_qty = ',
9822 			l_class_curr_steal_atp);
9823             END IF;
9824 
9825             MSC_AATP_PVT.Atp_demand_class_Consume(l_current_atp, l_class_curr_steal_atp);
9826 
9827             IF PG_DEBUG in ('Y', 'C') THEN
9828                msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'After consume l_class_curr_steal_atp');
9829                Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
9830 			l_current_atp);
9831                Print_Period_Qty('l_class_curr_steal_atp.atp_period:atp_qty = ',
9832 			l_class_curr_steal_atp);
9833             END IF;
9834           END IF; --IF l_class_tab(i) <> l_class_tab(1) THEN
9835 
9836           -- bug 1922942: although partner_id should be unique, we introduced
9837           -- -1 for 'Other' which make the partner_id not unique.
9838           -- for example, Class1/Other and Class2/Other will have same
9839           -- partner_id -1. so the if condition needs to be modified.
9840 
9841           -- IF l_partner_tab(i) <> l_partner_tab(1) THEN
9842 
9843           IF (l_class_tab(i) <> l_class_tab(1)) OR
9844               (l_partner_tab(i) <> l_partner_tab(1)) THEN
9845 
9846             IF PG_DEBUG in ('Y', 'C') THEN
9847                msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'before consume l_partner_curr_steal_atp');
9848             END IF;
9849             MSC_AATP_PVT.Atp_demand_class_Consume(l_current_atp, l_partner_curr_steal_atp);
9850 
9851             IF PG_DEBUG in ('Y', 'C') THEN
9852                msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'After consume l_partner_curr_steal_atp');
9853                Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
9854 			l_current_atp);
9855                Print_Period_Qty('l_partner_curr_steal_atp.atp_period:atp_qty = ',
9856 			l_partner_curr_steal_atp);
9857             END IF;
9858           END IF; -- IF l_partner_tab(i) <> l_partner_tab(1) THEN
9859 
9860         END IF; -- IF l_level_id IN (2,3)
9861 
9862         IF PG_DEBUG in ('Y', 'C') THEN
9863            msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'Before consume current_steal_atp');
9864            Print_Period_Qty('l_current_steal_atp.atp_period:atp_qty = ',
9865 		l_current_steal_atp);
9866         END IF;
9867 
9868         MSC_AATP_PVT.Atp_demand_class_Consume(l_current_atp, l_current_steal_atp);
9869 
9870         IF PG_DEBUG in ('Y', 'C') THEN
9871            msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'After consume l_current_steal_atp');
9872            Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
9873 		l_current_atp);
9874            Print_Period_Qty('l_current_steal_atp.atp_period:atp_qty = ',
9875 		l_current_steal_atp);
9876         END IF;
9877 
9878         -- this part is not in the original design.
9879         -- original design is that we will ignore the inconsistancy
9880         -- in the s/d and period atp for display when stealing happens, as long
9881         -- as we take care the stealing in the logic.
9882         -- but i think it is still better to put it in.
9883         -- and actually if we change Atp_sup_atp_info_rec.demand_class_Consume we can
9884         -- deal with this together.  but for now...
9885 
9886         -- we need to know if we need to store the stealing
9887         -- results in to x_atp_supply_demand and x_atp_period or not.
9888         -- we only do it if this is the demand class we request and
9889         -- insert_flag is on
9890 
9891         IF (NVL(p_sup_atp_info_rec.insert_flag, 0) <>0) AND
9892            (l_demand_class_tab(i) = p_sup_atp_info_rec.demand_class) THEN
9893 
9894           FOR j in 1..l_current_atp.atp_qty.COUNT LOOP
9895 
9896             IF l_current_atp.atp_qty(j) < l_temp_atp.atp_qty(j) THEN
9897               -- this is the stealing quantity in that period
9898 
9899               -- bug 1413459: the stealing quantity should be the current
9900               -- period quantity (after backward consumption, after stealing)
9901               -- minus the period quantity after backward consumption but
9902               -- before the stealing
9903               l_steal_period_quantity := l_current_atp.atp_qty(j) -
9904                                          l_temp_atp.atp_qty(j);
9905 
9906 
9907               MSC_SATP_FUNC.Extend_Atp_Supply_Demand(l_temp_atp_supply_demand,
9908                                        l_return_status);
9909               k := l_temp_atp_supply_demand.Level.Count;
9910               l_temp_atp_supply_demand.level(k) := p_sup_atp_info_rec.level;
9911               l_temp_atp_supply_demand.identifier(k) := p_identifier;
9912               l_temp_atp_supply_demand.scenario_id(k) := p_sup_atp_info_rec.scenario_id;
9913               l_temp_atp_supply_demand.supplier_id(k) := p_sup_atp_info_rec.supplier_id;
9914               l_temp_atp_supply_demand.supplier_site_id(k) := p_sup_atp_info_rec.supplier_site_id;
9915               l_temp_atp_supply_demand.uom(k):= l_uom_code;
9916               l_temp_atp_supply_demand.supply_demand_type(k) := 1;
9917 
9918               -- Bug 1408132 and 1416290, Need to insert type as
9919               -- Demand Class Consumption (45).
9920               l_temp_atp_supply_demand.supply_demand_source_type(k) := 45;
9921 
9922               l_temp_atp_supply_demand.identifier1(k) := p_sup_atp_info_rec.instance_id;
9923               l_temp_atp_supply_demand.supply_demand_date (k) :=
9924                     l_current_atp.atp_period(j);
9925               l_temp_atp_supply_demand.supply_demand_quantity(k) :=
9926                     l_steal_period_quantity;
9927 
9928               x_atp_period.Total_Demand_Quantity(j):=
9929                      x_atp_period.Total_Demand_Quantity(j) +
9930                      l_steal_period_quantity;
9931 
9932               x_atp_period.period_quantity(j):=x_atp_period.period_quantity(j)
9933                      + l_steal_period_quantity;
9934 
9935             END IF;
9936           END LOOP;
9937 
9938           --dsting
9939           move_SD_plsql_into_SD_temp(l_temp_atp_supply_demand);
9940 
9941         END IF;  -- IF (NVL(p_sup_atp_info_rec.insert_flag, 0) <>0) ....
9942        END IF; -- the huge condition
9943       END IF; -- IF (i > 1)
9944 
9945       ---IF l_demand_class_priority_tab(i) < l_priority THEN
9946       ---bug 1665110
9947       IF (l_demand_class <> p_sup_atp_info_rec.demand_class) THEN
9948         -- we need to prepare the l_next_steal_atp for next priorit
9949 
9950         MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_current_atp, l_next_steal_atp);
9951 
9952         -- 1680719
9953         IF l_level_id IN (-1, 1) THEN
9954           IF l_demand_class_priority_tab(i)<
9955              l_demand_class_priority_tab(i+1) THEN
9956           -- this is the last element of current priority, so we also need
9957           -- to add l_steal_atp into l_next_steal_atp if we can not finish
9958           -- the stealing at this priority
9959 
9960             IF PG_DEBUG in ('Y', 'C') THEN
9961                msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'level = '||l_level_id);
9962                msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'before Adding Add_to_Next_Steal_Atp');
9963                Print_Period_Qty('l_next_steal_atp.atp_period:atp_qty = ',
9964 			l_next_steal_atp);
9965             END IF;
9966 
9967             MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_current_steal_atp, l_next_steal_atp);
9968 
9969             IF PG_DEBUG in ('Y', 'C') THEN
9970                msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'after Add_to_Next_Steal_Atp');
9971                Print_Period_Qty('l_next_steal_atp.atp_period:atp_qty = ',
9972 			l_next_steal_atp);
9973             END IF;
9974 
9975           END IF;
9976 
9977         ELSE -- IF l_level_id IN (-1, 1)
9978           -- this is for hierarchy customer level and site level
9979           IF PG_DEBUG in ('Y', 'C') THEN
9980              msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'level = '||l_level_id);
9981              msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'i = '||i);
9982              msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'l_class_tab(i) = '||l_class_tab(i));
9983              msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'l_class_tab(i+1) = '||l_class_tab(i+1));
9984           END IF;
9985           IF (l_class_tab(i) <> l_class_tab(i+1)) THEN
9986 
9987             -- class changed.  If priority of both classes are not the same,
9988             -- then we need to change the curr_steal_atp  at class level.
9989             IF PG_DEBUG in ('Y', 'C') THEN
9990                msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'class changed');
9991             END IF;
9992 
9993             MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_current_steal_atp, l_next_steal_atp);
9994             MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_next_steal_atp, l_partner_next_steal_atp);
9995             MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_partner_curr_steal_atp,
9996                                   l_partner_next_steal_atp);
9997             MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_partner_next_steal_atp,
9998                                   l_class_next_steal_atp);
9999 
10000 
10001             IF trunc(l_demand_class_priority_tab(i), -3)<
10002                trunc(l_demand_class_priority_tab (i+1), -3) THEN
10003 
10004               MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_class_curr_steal_atp,
10005                                     l_class_next_steal_atp);
10006 
10007             END IF;
10008 
10009           ELSE
10010 
10011             IF PG_DEBUG in ('Y', 'C') THEN
10012                msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'class the same');
10013             END IF;
10014             IF (l_partner_tab(i) <> l_partner_tab(i+1)) THEN
10015               -- customer changed
10016               IF PG_DEBUG in ('Y', 'C') THEN
10017                  msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'customer not the same');
10018               END IF;
10019               MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_current_steal_atp, l_next_steal_atp);
10020 
10021               MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_next_steal_atp, l_partner_next_steal_atp);
10022 
10023               IF trunc(l_demand_class_priority_tab(i), -2)<
10024                  trunc(l_demand_class_priority_tab (i+1), -2) THEN
10025                 -- customer priority changed
10026 
10027                 IF PG_DEBUG in ('Y', 'C') THEN
10028                    msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'customer priority changed');
10029                 END IF;
10030                 MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_partner_curr_steal_atp,
10031                                       l_partner_next_steal_atp);
10032 
10033               END IF;
10034 
10035 
10036             ELSE
10037               -- same customer
10038               IF PG_DEBUG in ('Y', 'C') THEN
10039                  msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'customer the same');
10040               END IF;
10041               IF (l_demand_class_priority_tab(i)<>
10042                   l_demand_class_priority_tab (i+1)) THEN
10043                 -- site level priority changed
10044                 MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_current_steal_atp, l_next_steal_atp);
10045 
10046               END IF;
10047             END IF;
10048           END IF;
10049         END IF; -- IF l_level_id IN (-1, 1)
10050       END IF;
10051 
10052       -- 1665110
10053       IF PG_DEBUG in ('Y', 'C') THEN
10054          msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'p_sup_atp_info_rec.demand_class = '||p_sup_atp_info_rec.demand_class);
10055       END IF;
10056       EXIT WHEN (l_demand_class = p_sup_atp_info_rec.demand_class);
10057       IF PG_DEBUG in ('Y', 'C') THEN
10058          msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'after the exit statement, so we did not exit');
10059       END IF;
10060 
10061   END LOOP;
10062   MSC_ATP_PROC.Atp_Accumulate(l_current_atp.atp_qty);
10063 
10064   IF PG_DEBUG in ('Y', 'C') THEN
10065      msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'right after the Atp_Accumulate');
10066      Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
10067 	l_current_atp);
10068   END IF;
10069 
10070   x_atp_info := l_current_atp;
10071 
10072   --4055719 , calling remove -ves
10073   MSC_AATP_PROC.Atp_Remove_Negatives(x_atp_period.Cumulative_Quantity, l_return_status);
10074   IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
10075      IF PG_DEBUG in ('Y', 'C') THEN
10076         msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'Error occured in procedure Atp_Remove_Negatives');
10077      END IF;
10078      RAISE FND_API.G_EXC_ERROR;
10079   END IF;
10080 
10081   --4055719 , moved this code to MSC_ATP_REQ.get_supplier_atp_info
10082   -- get the infinite time fence date if it exists
10083   /*l_infinite_time_fence_date := MSC_ATP_FUNC.get_infinite_time_fence_date(null, --4055719
10084              null,null, p_sup_atp_info_rec.plan_id);
10085 
10086     IF l_infinite_time_fence_datee IS NOT NULL THEN
10087       -- add one more entry to indicate infinite time fence date
10088       -- and quantity.
10089       x_atp_info.atp_qty.EXTEND;
10090       x_atp_info.atp_period.EXTEND;
10091       --- bug 1657855, remove support for min alloc
10092       x_atp_info.limit_qty.EXTEND;
10093 
10094       i := x_atp_info.atp_qty.COUNT;
10095       x_atp_info.atp_period(i) := l_last_cap_next_date;
10096       x_atp_info.atp_qty(i) := MSC_ATP_PVT.INFINITE_NUMBER;
10097       ---x_atp_info.limit_qty(i) := MSC_ATP_PVT.INFINITE_NUMBER;
10098 
10099 
10100       IF NVL(p_sup_atp_info_rec.insert_flag, 0) <> 0 THEN
10101         -- add one more entry to indicate infinite time fence date
10102         -- and quantity.
10103 
10104         x_atp_period.Cumulative_Quantity := x_atp_info.atp_qty;
10105 
10106         j := x_atp_period.Level.COUNT;
10107         MSC_SATP_FUNC.Extend_Atp_Period(x_atp_period, l_return_status);
10108         j := j + 1;
10109         IF j > 1 THEN
10110           x_atp_period.Period_End_Date(j-1) := l_infinite_time_fence_date -1;
10111           x_atp_period.Identifier1(j) := x_atp_period.Identifier1(j-1);
10112           x_atp_period.Identifier2(j) := x_atp_period.Identifier2(j-1);
10113         END IF;
10114 
10115         x_atp_period.Level(j) := p_sup_atp_info_rec.level;
10116         x_atp_period.Identifier(j) := p_identifier;
10117         x_atp_period.Scenario_Id(j) := p_sup_atp_info_rec.scenario_id;
10118         x_atp_period.Pegging_Id(j) := NULL;
10119         x_atp_period.End_Pegging_Id(j) := NULL;
10120         x_atp_period.Supplier_Id(j) := p_sup_atp_info_rec.supplier_id;
10121         x_atp_period.Supplier_site_id(j) := p_sup_atp_info_rec.supplier_site_id;
10122         x_atp_period.Organization_id(j) := p_sup_atp_info_rec.organization_id;
10123         x_atp_period.Period_Start_Date(j) := l_infinite_time_fence_date;
10124         x_atp_period.Total_Supply_Quantity(j) := MSC_ATP_PVT.INFINITE_NUMBER;
10125         x_atp_period.Total_Demand_Quantity(j) := 0;
10126         x_atp_period.Period_Quantity(j) := MSC_ATP_PVT.INFINITE_NUMBER;
10127         x_atp_period.Cumulative_Quantity(j) := MSC_ATP_PVT.INFINITE_NUMBER;
10128 
10129      END IF;
10130    END IF; */
10131 -- END IF;
10132 
10133 END Supplier_Alloc_Cum_Atp;
10134 
10135 
10136 PROCEDURE Get_DC_Info(
10137 	p_instance_id	IN 	NUMBER,
10138 	p_inv_item_id	IN 	NUMBER,
10139 	p_org_id	IN 	NUMBER,
10140 	p_dept_id	IN 	NUMBER,
10141 	p_res_id	IN 	NUMBER,
10142 	p_demand_class	IN 	VARCHAR2,
10143 	p_request_date	IN 	DATE,
10144         x_level_id      OUT     NoCopy NUMBER,
10145 	x_priority	OUT  	NoCopy NUMBER,
10146 	x_alloc_percent	OUT 	NoCopy NUMBER,
10147 	x_return_status	OUT 	NoCopy VARCHAR2)
10148 IS
10149         l_rule_name     VARCHAR2(30);
10150 	l_time_phase 	NUMBER;
10151 
10152 BEGIN
10153   IF PG_DEBUG in ('Y', 'C') THEN
10154      msc_sch_wb.atp_debug('******** Get_DC_Info ********');
10155      msc_sch_wb.atp_debug('Get_DC_Info: ' || 'p_instance_id =' || p_instance_id);
10156      msc_sch_wb.atp_debug('Get_DC_Info: ' || 'p_inv_item_id =' || p_inv_item_id);
10157      msc_sch_wb.atp_debug('Get_DC_Info: ' || 'p_org_id =' || p_org_id);
10158      msc_sch_wb.atp_debug('Get_DC_Info: ' || 'p_dept_id =' || p_dept_id);
10159      msc_sch_wb.atp_debug('Get_DC_Info: ' || 'p_res_id =' || p_res_id);
10160      msc_sch_wb.atp_debug('Get_DC_Info: ' || 'p_demand_class =' || p_demand_class);
10161      msc_sch_wb.atp_debug('Get_DC_Info: ' || 'p_request_date =' || p_request_date );
10162   END IF;
10163 
10164   -- initialize API return status to success
10165   x_return_status := FND_API.G_RET_STS_SUCCESS;
10166 
10167   IF p_inv_item_id is not null THEN
10168         -- Get the allocation percent for the item/demand class. If no rule found,
10169 	-- check if a rule on the specified date exists for any demand class
10170 	-- for the specific item, take allocation percentage as NULL.
10171 	-- Though we will treat NULL as 1, but we need to differentiate them
10172 	-- so as to group demands/ supplies by demand classes. - ngoel 8/31/2000.
10173 	BEGIN
10174                 -- Modified by NGOEL on 2/23/2001 as there may be more than 1 rule assigned
10175                 -- to an item/org/instance combinantion at a given level based on time phase.
10176                 --SELECT distinct allocation_rule_name
10177                 --SELECT distinct allocation_rule_name, time_phase_id
10178                 --bug3948494 removed distinct and introduced rownum
10179                 SELECT allocation_rule_name, time_phase_id
10180                 INTO   l_rule_name, l_time_phase
10181                 FROM   msc_item_hierarchy_mv
10182                 WHERE  inventory_item_id = p_inv_item_id
10183                 AND    organization_id = p_org_id
10184       		AND    sr_instance_id = p_instance_id
10185       		AND    p_request_date between effective_date and disable_date
10186       		AND    rownum = 1 ;
10187 
10188 		-- Changes for Bug 2384551 start
10189                 IF (G_HIERARCHY_PROFILE = 1) THEN
10190 
10191                 SELECT ma.priority, ma.allocation_percent/100, ma.level_id
10192                 INTO   x_priority, x_alloc_percent, x_level_id
10193                 FROM   msc_allocations ma
10194                 WHERE  ma.demand_class = p_demand_class
10195                 AND    ma.time_phase_id = l_time_phase
10196                 AND    ma.level_id = -1;
10197 
10198                 ELSE
10199 
10200                 SELECT ma.priority, ma.allocation_percent/100, ma.level_id
10201                 INTO   x_priority, x_alloc_percent, x_level_id
10202                 FROM   msc_allocations ma
10203                 WHERE  ma.demand_class = p_demand_class
10204                 AND    ma.time_phase_id = l_time_phase
10205                 AND    ma.level_id <> -1;
10206 
10207                 END IF;
10208                  -- Changes for Bug 2384551 end
10209 
10210 	EXCEPTION
10211 	   WHEN NO_DATA_FOUND THEN
10212              IF G_HIERARCHY_PROFILE = 1 THEN
10213       		SELECT DECODE(count(allocation_percent), 0, NULL, 0)
10214 	      	INTO   x_alloc_percent
10215       		FROM   msc_item_hierarchy_mv
10216       		WHERE  inventory_item_id = p_inv_item_id
10217       		AND    organization_id = p_org_id
10218       		AND    sr_instance_id = p_instance_id
10219       		AND    p_request_date between effective_date and disable_date
10220                 AND    NVL(level_id, -1) = -1;
10221 
10222 		x_priority := -1;
10223                 x_level_id := -1;
10224              ELSIF G_HIERARCHY_PROFILE = 2 THEN
10225 
10226                 -- this should never happen
10227                 x_alloc_percent := NULL;
10228                 x_priority := -1;
10229                 x_level_id := 1;
10230 
10231              END IF;
10232 	   WHEN OTHERS THEN
10233 		IF PG_DEBUG in ('Y', 'C') THEN
10234 		   msc_sch_wb.atp_debug('Exception in Get_DC_Info');
10235 		END IF;
10236 
10237 		x_priority := -1;
10238 	END;
10239 
10240   ELSE
10241         -- Get the allocation percent for the dept/res/demand class
10242 	BEGIN
10243                 -- Modified by NGOEL on 2/23/2001 as there may be more than 1 rule assigned
10244                 -- to an item/org/instance combinantion at a given level based on time phase.
10245                 --SELECT distinct allocation_rule_name
10246                 --bug3948494 removed distinct and introduced rownum
10247                 --SELECT distinct allocation_rule_name, time_phase_id
10248                 SELECT allocation_rule_name, time_phase_id
10249                 INTO   l_rule_name, l_time_phase
10250                 FROM   msc_resource_hierarchy_mv
10251                 WHERE  department_id = p_dept_id
10252                 AND    resource_id = p_res_id
10253                 AND    organization_id = p_org_id
10254         	AND    sr_instance_id = p_instance_id
10255         	AND    p_request_date between effective_date and disable_date
10256         	AND    rownum = 1 ;
10257 
10258 		-- Changes for Bug 2384551  start
10259                 IF G_HIERARCHY_PROFILE = 1 THEN
10260 
10261                 SELECT ma.priority, ma.allocation_percent/100, ma.level_id
10262                 INTO   x_priority, x_alloc_percent, x_level_id
10263                 FROM   msc_allocations ma
10264                 WHERE  ma.demand_class = p_demand_class
10265                 AND    ma.time_phase_id = l_time_phase
10266                 AND    ma.level_id = -1;
10267 
10268                 ELSE
10269 
10270                 SELECT ma.priority, ma.allocation_percent/100, ma.level_id
10271                 INTO   x_priority, x_alloc_percent, x_level_id
10272                 FROM   msc_allocations ma
10273                 WHERE  ma.demand_class = p_demand_class
10274                 AND    ma.time_phase_id = l_time_phase
10275                 AND    ma.level_id <> -1;
10276 
10277                 END IF;
10278                  -- Changes for Bug 2384551 end
10279 
10280 	EXCEPTION
10281 	   WHEN NO_DATA_FOUND THEN
10282              IF G_HIERARCHY_PROFILE = 1 THEN
10283       		SELECT DECODE(count(allocation_percent), 0, NULL, 0)
10284 	      	INTO   x_alloc_percent
10285         	FROM   msc_resource_hierarchy_mv
10286         	WHERE  department_id = p_dept_id
10287         	AND    resource_id = p_res_id
10288         	AND    organization_id = p_org_id
10289         	AND    sr_instance_id = p_instance_id
10290         	AND    p_request_date between effective_date and disable_date
10291                 AND    NVL(level_id, -1) = -1;
10292 
10293 		x_priority := -1;
10294                 x_level_id := -1;
10295              ELSIF G_HIERARCHY_PROFILE = 2 THEN
10296 
10297                 -- this should never happen
10298                 x_alloc_percent := NULL;
10299                 x_priority := -1;
10300                 x_level_id := 1;
10301 
10302              END IF;
10303 
10304 	   WHEN OTHERS THEN
10305 		IF PG_DEBUG in ('Y', 'C') THEN
10306 		   msc_sch_wb.atp_debug('Exception in Get_DC_Info');
10307 		END IF;
10308 
10309 		x_priority := -1;
10310 	END;
10311   END IF;
10312 EXCEPTION
10313   WHEN OTHERS THEN
10314 	IF PG_DEBUG in ('Y', 'C') THEN
10315 	   msc_sch_wb.atp_debug('Get_DC_Info: ' || 'Error code:' || to_char(sqlcode));
10316 	END IF;
10317 	x_priority := -1;
10318 	x_alloc_percent := 0;
10319 	x_return_status := FND_API.G_RET_STS_ERROR;
10320 END Get_DC_Info;
10321 
10322 PROCEDURE View_Allocation(
10323   p_session_id         IN    NUMBER,
10324   p_inventory_item_id  IN    NUMBER,
10325   p_instance_id        IN    NUMBER,
10326   p_organization_id    IN    NUMBER,
10327   p_department_id      IN    NUMBER,
10328   p_resource_id        IN    NUMBER,
10329   p_demand_class       IN    VARCHAR2,
10330   x_return_status      OUT   NoCopy VARCHAR2
10331 )
10332 IS
10333 	l_plan_id			NUMBER;
10334 	l_assign_set_id			NUMBER;
10335 	l_msg_count			NUMBER;
10336 	l_mode				NUMBER;
10337 	mm				PLS_INTEGER;
10338 	l_request_date			DATE;
10339 	l_return_status			VARCHAR2(100);
10340 	l_msg_data			VARCHAR2(200);
10341 	l_atp_info			MRP_ATP_PVT.ATP_Info;
10342 	l_atp_period			MRP_ATP_PUB.ATP_Period_Typ;
10343 	l_atp_supply_demand		MRP_ATP_PUB.ATP_Supply_Demand_Typ;
10344 	l_atp_rec			MRP_ATP_PUB.atp_rec_typ;
10345 	l_atp_details			MRP_ATP_PUB.ATP_Details_Typ;
10346         l_atp_supply_demand_null        MRP_ATP_PUB.ATP_Supply_Demand_Typ;
10347         l_batchable_flag                NUMBER;
10348         l_max_capacity                  NUMBER;
10349         l_res_conversion_rate	number :=1;
10350         l_res_uom			varchar2(3);
10351         l_res_uom_type			NUMBER;
10352         l_plan_info_rec                 MSC_ATP_PVT.plan_info_rec;  -- added for bug 2392456
10353 
10354         --diag_atp
10355         l_get_mat_in_rec               MSC_ATP_REQ.get_mat_in_rec;
10356 
10357 
10358         --  Agilent Allocated ATP Based on Planning Details changes Begin
10359         l_demand_class                  VARCHAR2(80);
10360         --  Agilent Allocated ATP Based on Planning Details changes End
10361 
10362 BEGIN
10363 	msc_sch_wb.set_session_id(p_session_id);
10364 	IF PG_DEBUG in ('Y', 'C') THEN
10365 	   msc_sch_wb.atp_debug('Begin View_Allocation');
10366 	   msc_sch_wb.atp_debug('View_Allocation: ' || 'p_inventory_item_id = ' ||to_char(p_inventory_item_id));
10367 	   msc_sch_wb.atp_debug('View_Allocation: ' || 'p_instance_id = ' ||to_char(p_instance_id));
10368 	   msc_sch_wb.atp_debug('View_Allocation: ' || 'p_organization_id = ' ||to_char(p_organization_id));
10369 	   msc_sch_wb.atp_debug('View_Allocation: ' || 'p_department_id = ' ||to_char(p_department_id));
10370 	   msc_sch_wb.atp_debug('View_Allocation: ' || 'p_resource_id = ' ||to_char(p_resource_id));
10371            msc_sch_wb.atp_debug('View_Allocation: ' || 'p_demand_class = ' ||p_demand_class);
10372         END IF;
10373 
10374         -- Bug 2396523 : krajan : Added debug messages
10375         IF PG_DEBUG in ('Y', 'C') THEN
10376            msc_sch_wb.atp_debug('View_Allocation: ' || 'P_DEMAND_CLASS ' || p_demand_class);
10377            msc_sch_wb.atp_debug('View_Allocation: ' || 'G_INV_CTP= ' || MSC_ATP_PVT.G_INV_CTP);
10378            msc_sch_wb.atp_debug('View_Allocation: ' || 'G_HIERARCHY_PROFILE = '|| MSC_ATP_PVT.G_HIERARCHY_PROFILE );
10379            msc_sch_wb.atp_debug('View_Allocation: ' || 'G_ALLOCATED_ATP = ' || MSC_ATP_PVT.G_ALLOCATED_ATP );
10380            msc_sch_wb.atp_debug('View_Allocation: ' || 'G_ALLOCATION_METHOD = '|| MSC_ATP_PVT.G_ALLOCATION_METHOD );
10381         END IF;
10382 
10383         -- krajan : 2400676
10384         IF PG_DEBUG in ('Y', 'C') THEN
10385            msc_sch_wb.atp_debug('View_Allocation: ' || 'Resetting Global Error Code');
10386         END IF;
10387         MSC_SCH_WB.G_ATP_ERROR_CODE := 0;
10388 
10389 	-- As part of ship_rec_cal. No need for SQL here. Just call the function straight.
10390         /*
10391 	--Get sysdate to later pass on to the procedure.
10392         -- Changed to get the next working day from sysdate.
10393         SELECT   MSC_CALENDAR.NEXT_WORK_DAY(p_organization_id,
10394 			p_instance_id, 1, TRUNC(sysdate))
10395         INTO    l_request_date
10396         FROM    dual;
10397 	*/
10398 
10399 	l_request_date := MSC_CALENDAR.NEXT_WORK_DAY(p_organization_id, p_instance_id, 1, sysdate);
10400 
10401         IF PG_DEBUG in ('Y', 'C') THEN
10402            msc_sch_wb.atp_debug('View_Allocation: ' || 'Request Date : '||to_char(l_request_date, 'DD-MON-YYYY'));
10403         END IF;
10404         -- krajan : 2400676
10405         IF (l_request_date = NULL) THEN
10406                 IF PG_DEBUG in ('Y', 'C') THEN
10407                    msc_sch_wb.atp_debug('View_Allocation: ' || 'Request date is null');
10408                 END IF;
10409                 MSC_SCH_WB.G_ATP_ERROR_CODE := MSC_ATP_PVT.NO_MATCHING_CAL_DATE;
10410                 RAISE FND_API.G_EXC_ERROR;
10411         END IF;
10412 
10413 	--Check if the request is for Item or Department-Resource.
10414 	IF p_inventory_item_id IS NOT NULL THEN
10415 
10416 		 --Get Plan Id for the item/org/instance.
10417                 /* commented for bug 2392456
10418                 MSC_ATP_PROC.Get_plan_Info(p_instance_id, p_inventory_item_id,
10419                         p_organization_id, p_demand_class, l_plan_id, l_assign_set_id);
10420 
10421                 -- changes for bug 2392456 starts
10422                  MSC_ATP_PROC.Get_plan_Info(p_instance_id, p_inventory_item_id,
10423                         p_organization_id, p_demand_class, l_plan_info_rec);
10424                 */
10425                 -- New procedure for obtaining plan data : Supplier Capacity Lead Time (SCLT) proj.
10426                 MSC_ATP_PROC.get_global_plan_info(p_instance_id, p_inventory_item_id,
10427                                                   p_organization_id, p_demand_class);
10428 
10429                 l_plan_info_rec := MSC_ATP_PVT.G_PLAN_INFO_REC;
10430                 -- End New procedure for obtaining plan data : Supplier Capacity Lead Time proj.
10431 
10432                 l_plan_id       := l_plan_info_rec.plan_id;
10433                 l_assign_set_id := l_plan_info_rec.assignment_set_id;
10434                 -- changes for bug 2392456 ends
10435 
10436 	        IF PG_DEBUG in ('Y', 'C') THEN
10437 	           msc_sch_wb.atp_debug('View_Allocation: ' || 'Plan Id : '||to_char(l_plan_id));
10438 	           msc_sch_wb.atp_debug('View_Allocation: ' || 'Assignment Set Id : '||to_char(l_assign_set_id));
10439 	        END IF;
10440 
10441                 -- krajan : 2400676
10442                 IF (l_plan_id IS NULL) OR (l_plan_id = -1) THEN
10443                         IF PG_DEBUG in ('Y', 'C') THEN
10444                            msc_sch_wb.atp_debug ('View_Allocation: ' || 'Plan_ID is null or -1');
10445                         END IF;
10446                         MSC_SCH_WB.G_ATP_ERROR_CODE := MSC_ATP_PVT.PLAN_NOT_FOUND;
10447                         RAISE FND_API.G_EXC_ERROR;
10448                 END IF;
10449 
10450                 IF (l_plan_id = -100) THEN
10451                         IF PG_DEBUG in ('Y', 'C') THEN
10452                            msc_sch_wb.atp_debug ('View_Allocation: ' || 'Plan_ID is -100 : Summary Running');
10453                         END IF;
10454                         MSC_SCH_WB.G_ATP_ERROR_CODE := MSC_ATP_PVT.SUMM_CONC_PROG_RUNNING;
10455                         RAISE FND_API.G_EXC_ERROR;
10456                 END IF;
10457 
10458                 IF (l_plan_id = -200) THEN
10459                         IF PG_DEBUG in ('Y', 'C') THEN
10460                            msc_sch_wb.atp_debug ('View_Allocation: ' || 'Plan_ID is -200 : Summary Running');
10461                         END IF;
10462                         MSC_SCH_WB.G_ATP_ERROR_CODE := MSC_ATP_PVT.RUN_POST_PLAN_ALLOC;
10463                         RAISE FND_API.G_EXC_ERROR;
10464                 END IF;
10465 
10466 
10467 
10468 		--Call Item_Alloc_Cum_Atp to get the period ATP info
10469 		--for the item. Set insert_flag = 1, for period atp details.
10470 		--Pass p_identifier = -1 to identify the call from this procedure,
10471 		--so as not to do any demand class consumption/ stealing.
10472 
10473                 IF p_demand_class IS NULL THEN
10474 
10475                 -- To get the total ATP, demand class is passed as NULL,
10476                 -- Pass p_scenario_id = -1 to identify the call for
10477                 -- toatl ATP and demand class to be null
10478 
10479               -- Agilent Allocated ATP Based on Planning Details changes Begin
10480 
10481                   IF ((MSC_ATP_PVT.G_HIERARCHY_PROFILE <> 1) OR
10482                      ((MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
10483                       (MSC_ATP_PVT.G_ALLOCATION_METHOD <> 1))) THEN
10484 
10485                        -- Original Code
10486 
10487                        MSC_AATP_PVT.Item_Alloc_Cum_Atp(
10488                                 l_plan_id,
10489                                 0,      --p_level(for top level item)
10490                                 -1,     --p_identifier
10491                                 -1,     --p_scenario_id - For total request
10492                                 p_inventory_item_id,
10493                                 p_organization_id,
10494                                 p_instance_id,
10495                                 p_demand_class,
10496                                 l_request_date,
10497                                 1,      --p_insert_flag,
10498                                 l_atp_info,
10499                                 l_atp_period,
10500                                 l_atp_supply_demand,
10501                                 --diag_atp
10502                                 l_get_mat_in_rec,
10503                                 p_inventory_item_id, -- time_phased_atp passing p_inventory_item_id for compilation
10504                                 NULL); -- time_phased_atp passing null for compilation
10505                   ELSIF ((MSC_ATP_PVT.G_INV_CTP = 4) AND
10506                       (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
10507                       (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
10508                       (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1)) THEN
10509 
10510                      IF PG_DEBUG in ('Y', 'C') THEN
10511                         msc_sch_wb.atp_debug('View_Allocation: before '||
10512                                 'calling Item_Pre_Allocated_Atp for Totals');
10513                      END IF;
10514                      -- Set demand_class to NULL to obtain totals.
10515 
10516                    l_demand_class :=  NULL;
10517                      -- and MSC_AATP_REQ.Item_Pre_Allocated_Atp
10518 
10519                         MSC_AATP_REQ.Item_Pre_Allocated_Atp(
10520                                 l_plan_id,
10521                                 0,      --p_level(for top level item)
10522                                 -1,     --p_identifier
10523                                 -1,      --p_scenario_id - Not Used
10524                                 p_inventory_item_id,
10525                                 p_organization_id,
10526                                 p_instance_id,
10527                                 l_demand_class,
10528                                 l_request_date,
10529                                 1,      --p_insert_flag,
10530                                 l_atp_info,
10531                                 l_atp_period,
10532                                 l_atp_supply_demand,
10533                                 --diag_atp
10534                                 l_get_mat_in_rec,
10535                                 NULL,  -- p_refresh_number - For summary enhancement - Allocation WB will not use summary
10536                                 NULL,  -- time_phased_atp - This procedure is not used
10537                                 NULL); -- time_phased_atp - This procedure is not used
10538                  END IF;
10539                 -- Agilent Allocated ATP Based on Planning Details changes End
10540 
10541                 -- Assign null record of table to supply demand table as we
10542                 -- don't need supply demand details in temp table.
10543                         l_atp_supply_demand := l_atp_supply_demand_null;
10544                 ELSE
10545 
10546               -- Agilent Allocated ATP Based on Planning Details changes Begin
10547 
10548                   IF ((MSC_ATP_PVT.G_INV_CTP = 4) AND
10549                       (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
10550                       (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
10551                       (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1)) THEN
10552 
10553                      IF PG_DEBUG in ('Y', 'C') THEN
10554                         msc_sch_wb.atp_debug('View_Allocation: before calling '||
10555                                 'Get_Hierarchy_Demand_Class for Demand Class');
10556                      END IF;
10557                      -- Code for Get_Hierarchy_Demand_Class
10558 
10559                    l_demand_class := MSC_AATP_FUNC.Get_Hierarchy_Demand_Class
10560                                       (
10561                                          NULL,    --partner_id
10562                                          NULL,    --partner_site_id
10563                                          --p_inventory_item_id,
10564                                          MSC_ATP_FUNC.Get_inv_item_id(
10565                                           p_instance_id,
10566                                           p_inventory_item_id,  -- src_inv_item
10567                                           null,
10568                                           p_organization_id  ), -- Get Inv Item
10569                                          p_organization_id,
10570                                          p_instance_id,
10571                                          l_request_date,
10572                                          1,        --p_level_id
10573                                          p_demand_class
10574                                       );
10575                      -- and MSC_AATP_REQ.Item_Pre_Allocated_Atp
10576 
10577                         MSC_AATP_REQ.Item_Pre_Allocated_Atp(
10578                                 l_plan_id,
10579                                 0,      --p_level(for top level item)
10580                                 -1,     --p_identifier
10581                                 -1,      --p_scenario_id - Not Used
10582                                 p_inventory_item_id,
10583                                 p_organization_id,
10584                                 p_instance_id,
10585                                 l_demand_class,
10586                                 l_request_date,
10587                                 1,      --p_insert_flag,
10588                                 l_atp_info,
10589                                 l_atp_period,
10590                                 l_atp_supply_demand,
10591                                 l_get_mat_in_rec,
10592                                 NULL,  -- p_refresh_number - For summary enhancement - Allocation WB will not use summary
10593                                 p_inventory_item_id,  -- time_phased_atp - Added for compilation
10594                                 NULL); -- time_phased_atp - Added for compilation
10595 
10596                   -- Bug 2396523 : krajan. Commented out and added ELSIF
10597                   --ELSE  -- Original Code
10598                   ELSIF ((MSC_ATP_PVT.G_HIERARCHY_PROFILE <> 1) OR
10599                         ((MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
10600                         (MSC_ATP_PVT.G_ALLOCATION_METHOD <> 1))) THEN
10601 
10602                             MSC_AATP_PVT.Item_Alloc_Cum_Atp(
10603                                 l_plan_id,
10604                                 0,      --p_level(for top level item)
10605                                 -1,     --p_identifier
10606                                 0,      --p_scenario_id - Not Used
10607                                 p_inventory_item_id,
10608                                 p_organization_id,
10609                                 p_instance_id,
10610                                 p_demand_class,
10611                                 l_request_date,
10612                                 1,      --p_insert_flag,
10613                                 l_atp_info,
10614                                 l_atp_period,
10615                                 l_atp_supply_demand,
10616                                 --diag_atp
10617                                 l_get_mat_in_rec,
10618                                 p_inventory_item_id, -- time_phased_atp passing p_inventory_item_id for compilation
10619                                 NULL); -- time_phased_atp passing null for compilation
10620                   END IF;
10621 
10622                 -- Agilent Allocated ATP Based on Planning Details changes End
10623 
10624                 END IF;
10625 
10626 
10627 	ELSIF p_inventory_item_id IS NULL THEN
10628 
10629                 --Get Plan Id for the department/resource/org/instance.
10630                 --In case we have multiple plans,get MRP plan as first
10631                 --choice, for now just choose one with lowest plan id.
10632 
10633                 BEGIN
10634                         SELECT  min(mdr.plan_id) as plan_id
10635                         INTO    l_plan_id
10636                         FROM    msc_department_resources mdr,
10637                                 msc_trading_partners tp,
10638                                 msc_apps_instances ins,
10639                                 msc_plans plans,
10640                                 msc_designators desig
10641                         WHERE   desig.inventory_atp_flag = 1
10642                         AND     plans.compile_designator = desig.designator
10643                         AND     plans.sr_instance_id = desig.sr_instance_id
10644                         AND     plans.organization_id = desig.organization_id
10645                         AND     plans.plan_completion_date is not null
10646                         AND     plans.data_completion_date is not null
10647                         AND     ins.instance_id = plans.sr_instance_id
10648                         AND     ins.enable_flag = 1
10649                         AND     tp.sr_tp_id = plans.organization_id
10650                         AND     tp.sr_instance_id = plans.sr_instance_id
10651                         AND     tp.partner_type = 3
10652                         AND     mdr.plan_id = plans.plan_id
10653                         AND     mdr.organization_id = p_organization_id
10654                         AND     mdr.sr_instance_id = p_instance_id
10655                         AND     mdr.resource_id = p_resource_id
10656                         AND     mdr.department_id = p_department_id
10657 --                      AND     mdr.demand_class = p_demand_class
10658                         group by mdr.organization_id, mdr.sr_instance_id,
10659                               mdr.resource_id, mdr.department_id;--, desig.demand_class;
10660                 EXCEPTION
10661                         WHEN NO_DATA_FOUND THEN
10662                                 IF PG_DEBUG in ('Y', 'C') THEN
10663                                    msc_sch_wb.atp_debug('View_Allocation: ' || 'Plan not found ');
10664                                 END IF;
10665                                 MSC_SCH_WB.G_ATP_ERROR_CODE := MSC_ATP_PVT.PLAN_NOT_FOUND;
10666                                 RAISE FND_API.G_EXC_ERROR;
10667                         WHEN others THEN
10668                                 IF PG_DEBUG in ('Y', 'C') THEN
10669                                    msc_sch_wb.atp_debug('View_Allocation: ' || 'Error getting plan id :'||sqlcode);
10670                                 END IF;
10671                                 MSC_SCH_WB.G_ATP_ERROR_CODE := MSC_ATP_PVT.ATP_PROCESSING_ERROR;
10672                                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
10673                 END;
10674 
10675                 IF PG_DEBUG in ('Y', 'C') THEN
10676                    msc_sch_wb.atp_debug('View_Allocation: ' || 'Plan Id : '||to_char(l_plan_id));
10677                 END IF;
10678 
10679 		--Call Res_Alloc_Cum_Atp to get the period ATP info
10680 		--for the department/resource. Set insert_flag = 1, for period atp details.
10681 		--Pass p_identifier = -1 to identify the call from this procedure,
10682 		--so as not to do any demand class consumption/ stealing.
10683                 --resource bacting: find out if resource is batchable or not and the max capacity
10684                 BEGIN
10685                    SELECT  batchable_flag, max_capacity, unit_of_measure, uom_class_type
10686                    INTO    l_batchable_flag, l_max_capacity, l_res_uom, l_res_uom_type
10687                    FROM    msc_department_resources
10688                    WHERE   department_id = p_department_id
10689                    AND     resource_id = p_resource_id
10690                    AND     organization_id = p_organization_id
10691                    AND     plan_id = l_plan_id
10692                    AND     sr_instance_id = p_instance_id ;
10693                 EXCEPTION
10694                    WHEN OTHERS THEN
10695                       l_batchable_flag := 0;
10696                       l_max_capacity := 0;
10697                 END;
10698                 IF PG_DEBUG in ('Y', 'C') THEN
10699                    msc_sch_wb.atp_debug('View_Allocation: ' || 'Convert res_item UOM');
10700                 END IF;
10701                 IF (l_batchable_flag = 1) THEN
10702                       BEGIN
10703                           SELECT conversion_rate
10704                           INTO   l_res_conversion_rate
10705                           FROM   msc_uom_conversions
10706                           WHERE  inventory_item_id = 0
10707                           AND    sr_instance_id = p_instance_id
10708                           AND    UOM_CODE = l_res_uom;
10709                       EXCEPTION
10710                           WHEN NO_DATA_FOUND THEN
10711                                 l_res_conversion_rate := 1;
10712                       END;
10713 
10714                 END IF;
10715                 IF PG_DEBUG in ('Y', 'C') THEN
10716                    msc_sch_wb.atp_debug('View_Allocation: ' || 'l_res_conversion_rate := ' || l_res_conversion_rate);
10717                 END IF;
10718 
10719               -- Agilent Allocated ATP Based on Planning Details changes Begin
10720 
10721                 IF ((MSC_ATP_PVT.G_INV_CTP = 4) AND
10722                     (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
10723                     (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
10724                     (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1)) THEN
10725 
10726                    l_demand_class := NULL;
10727 
10728                 ELSE   -- original_code
10729 
10730                    l_demand_class := p_demand_class;
10731 
10732                 END IF;
10733 
10734               -- Agilent Allocated ATP Based on Planning Details changes End
10735 
10736                 IF l_demand_class IS NULL THEN
10737 
10738                 -- To get the total ATP, demand class is passed as NULL,
10739                 -- Pass p_scenario_id = -1 to identify the call for
10740                 -- toatl ATP and demand class to be null
10741 
10742                         MSC_AATP_PVT.Res_Alloc_Cum_Atp(
10743                                 l_plan_id,
10744                                 0,      --p_level(for top level item)
10745                                 -1,     --p_identifier
10746                                 -1,     --p_scenario_id - To identify total request
10747                                 p_department_id,
10748                                 p_resource_id,
10749                                 p_organization_id,
10750                                 p_instance_id,
10751                                 l_demand_class,
10752                                 l_request_date,
10753                                 1,      --p_insert_flag
10754                                  ---resource batching
10755                                 l_max_capacity,
10756                                 l_batchable_flag,
10757                                 l_res_conversion_rate,
10758                                 l_res_uom_type,
10759                                 l_atp_info,
10760                                 l_atp_period,
10761                                 l_atp_supply_demand);
10762 
10763                 -- Assign null record of table to supply demand table as we
10764                 -- don't need supply demand details in temp table.
10765 
10766                         l_atp_supply_demand := l_atp_supply_demand_null;
10767                 ELSE
10768                         MSC_AATP_PVT.Res_Alloc_Cum_Atp(
10769                                 l_plan_id,
10770                                 0,      --p_level(for top level item)
10771                                 -1,     --p_identifier
10772                                 0,      --p_scenario_id - Not Used
10773                                 p_department_id,
10774                                 p_resource_id,
10775                                 p_organization_id,
10776                                 p_instance_id,
10777                                 l_demand_class,
10778                                 l_request_date,
10779                                 1,      --p_insert_flag
10780                                  ---resource batching
10781                                 l_max_capacity,
10782                                 l_batchable_flag,
10783                                 l_res_conversion_rate,
10784                                 l_res_uom_type,
10785                                 l_atp_info,
10786                                 l_atp_period,
10787                                 l_atp_supply_demand);
10788 
10789                 END IF;
10790 
10791 	END IF;
10792 
10793   IF PG_DEBUG in ('Y', 'C') THEN
10794      msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ');
10795      Print_Period_Qty('l_atp_info.atp_period:atp_qty = ',
10796 	l_atp_info);
10797   END IF;
10798 
10799   -- krajan : 2400676
10800   IF (l_atp_period.period_quantity.COUNT = 0) THEN
10801         -- rajjain bug 2951786 05/13/2003
10802         MSC_SCH_WB.G_ATP_ERROR_CODE := MSC_ATP_PVT.NO_SUPPLY_DEMAND;
10803         -- MSC_SCH_WB.G_ATP_ERROR_CODE := MSC_ATP_PVT.ATP_PROCESSING_ERROR;
10804         IF PG_DEBUG in ('Y', 'C') THEN
10805            msc_sch_wb.atp_debug ('View_Allocation: ' || 'l_atp_period is NULL');
10806            msc_sch_wb.atp_debug ('View_Allocation: ' || 'Error Code: ' || MSC_SCH_WB.G_ATP_ERROR_CODE);
10807         END IF;
10808         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
10809   END IF;
10810 
10811   IF PG_DEBUG in ('Y', 'C') THEN
10812      mm := l_atp_period.Period_Quantity.FIRST;
10813 
10814      WHILE mm is not null LOOP
10815         msc_sch_wb.atp_debug('View_Allocation: ' || 'l_atp_period.period_start_date and Period_Quantity = '||
10816            l_atp_period.period_start_date(mm) ||' : '|| l_atp_period.Period_Quantity(mm));
10817         mm := l_atp_period.Period_Quantity.Next(mm);
10818      END LOOP;
10819   END IF;
10820 
10821 	/* rajjain 01/29/2003 begin Bug 2737596
10822 	   This call is not needed now. Now we directly call PUT_SD_DATA
10823 	   and PUT_PERIOD_DATA from this procedure.
10824 
10825 	--Call procedure to insert l_atp_period
10826 	--into mrp_atp_details_temp table.
10827 
10828 	MSC_ATP_UTILS.put_into_temp_table(
10829 		NULL,		--x_dblink
10830 		p_session_id,
10831 		l_atp_rec,
10832 		l_atp_supply_demand,
10833 		l_atp_period,
10834 		l_atp_details,
10835 		l_mode,
10836 		x_return_status,
10837 		l_msg_data,
10838 		l_msg_count);
10839 
10840          IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
10841                 IF PG_DEBUG in ('Y', 'C') THEN
10842                    msc_sch_wb.atp_debug ('View_Allocation: ' || 'Something wrong in call to PUT_INTO_TEMP_TABLE');
10843                 END IF;
10844                 MSC_SCH_WB.G_ATP_ERROR_CODE := MSC_ATP_PVT.ATP_PROCESSING_ERROR;
10845                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
10846          END IF;*/
10847 
10848          -- rajjain 01/29/2003 begin Bug 2737596
10849          IF PG_DEBUG in ('Y', 'C') THEN
10850             msc_sch_wb.atp_debug('View_Allocation: ' || 'l_atp_supply_demand.level.COUNT: ' || l_atp_supply_demand.level.COUNT);
10851          END IF;
10852 
10853          MSC_ATP_UTILS.PUT_SD_DATA(l_atp_supply_demand, NULL, p_session_id);
10854 
10855          IF PG_DEBUG in ('Y', 'C') THEN
10856             msc_sch_wb.atp_debug('View_Allocation: ' || ' Inserted supply demand  records ');
10857          END IF;
10858 
10859          IF PG_DEBUG in ('Y', 'C') THEN
10860             msc_sch_wb.atp_debug('View_Allocation: ' || 'l_atp_period.level.count: ' || l_atp_period.level.count);
10861          END IF;
10862 
10863          MSC_ATP_UTILS.PUT_PERIOD_DATA(l_atp_period, NULL, p_session_id);
10864 
10865          IF PG_DEBUG in ('Y', 'C') THEN
10866             msc_sch_wb.atp_debug('View_Allocation: ' || ' Inserted period records ');
10867          END IF;
10868          -- rajjain 01/29/2003 end Bug 2737596
10869 
10870 -- krajan : 2400614
10871 EXCEPTION
10872 
10873 WHEN FND_API.G_EXC_ERROR THEN
10874         -- ATP ERROR CODE WILL BE SET before this exception is raised
10875         IF PG_DEBUG in ('Y', 'C') THEN
10876            msc_sch_wb.atp_debug ('Error in View_Allocation: Expected Error Raised');
10877         END IF;
10878         x_return_status := FND_API.G_RET_STS_ERROR;
10879         IF (MSC_SCH_WB.G_ATP_ERROR_CODE = 0) THEN
10880                 MSC_SCH_WB.G_ATP_ERROR_CODE := MSC_ATP_PVT.ATP_PROCESSING_ERROR;
10881                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
10882         END IF;
10883 
10884 WHEN  MSC_ATP_PUB.ATP_INVALID_OBJECTS_FOUND THEN
10885         IF PG_DEBUG in ('Y', 'C') THEN
10886            msc_sch_wb.atp_debug ('Error in View_Allocation: Invalid Objects Found');
10887         END IF;
10888         MSC_SCH_WB.G_ATP_ERROR_CODE := MSC_ATP_PVT.ATP_INVALID_OBJECTS;
10889         x_return_status := FND_API.G_RET_STS_ERROR;
10890 
10891 WHEN OTHERS THEN
10892         IF PG_DEBUG in ('Y', 'C') THEN
10893            msc_sch_wb.atp_debug ('Error in View_Allocation');
10894         END IF;
10895         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
10896         IF (MSC_SCH_WB.G_ATP_ERROR_CODE = 0) THEN
10897                 MSC_SCH_WB.G_ATP_ERROR_CODE := MSC_ATP_PVT.ATP_PROCESSING_ERROR;
10898         END IF;
10899 
10900 END View_allocation;
10901 
10902 
10903 PROCEDURE Stealing (
10904                p_atp_record             IN OUT  NoCopy MRP_ATP_PVT.AtpRec,
10905                p_parent_pegging_id      IN      NUMBER,
10906                p_scenario_id            IN      NUMBER,
10907                p_level                  IN      NUMBER,
10908                p_search                 IN      NUMBER,
10909                p_plan_id                IN      NUMBER,
10910                p_net_demand             IN OUT  NoCopy NUMBER,
10911                x_total_mem_stealing_qty OUT     NOCOPY NUMBER, -- For time_phased_atp
10912                x_total_pf_stealing_qty  OUT     NOCOPY NUMBER, -- For time_phased_atp
10913                x_atp_supply_demand      OUT     NOCOPY MRP_ATP_PUB.ATP_Supply_Demand_Typ,
10914                x_atp_period             OUT     NOCOPY MRP_ATP_PUB.ATP_Period_Typ,
10915                x_return_status          OUT     NoCopy VARCHAR2,
10916                p_refresh_number         IN             NUMBER    -- For summary enhancement
10917 )
10918 IS
10919 l_requested_ship_date          date;
10920 l_atp_date_this_level          date;
10921 l_atp_date_quantity_this_level number;
10922 --l_requested_date_quantity      number;
10923 l_atp_period                   MRP_ATP_PUB.ATP_Period_Typ;
10924 l_atp_supply_demand            MRP_ATP_PUB.ATP_Supply_Demand_Typ;
10925 l_pegging_rec                  mrp_atp_details_temp%ROWTYPE;
10926 l_demand_class_tab             MRP_ATP_PUB.char80_arr
10927                                    := MRP_ATP_PUB.char80_arr();
10928 l_demand_class_tab_new          MRP_ATP_PUB.char80_arr
10929                                    := MRP_ATP_PUB.char80_arr();--6359986
10930 l_demand_class_priority_tab    MRP_ATP_PUB.number_arr
10931                                    := MRP_ATP_PUB.number_arr();
10932 l_dmd_class_priority_tab_new MRP_ATP_PUB.number_arr
10933                                    := MRP_ATP_PUB.number_arr();
10934 
10935 l_allocation_percent_tab	MRP_ATP_PUB.number_arr
10936        		                            := MRP_ATP_PUB.number_arr();
10937 l_inv_item_id                  NUMBER;
10938 l_demand_class                 VARCHAR2(30);
10939 l_atp_insert_rec	       MRP_ATP_PVT.AtpRec;
10940 l_inv_item_name                varchar2(250); --bug 2246200
10941 l_org_code                     varchar2(7);
10942 l_pegging_id                   number;
10943 l_atp_pegging_id               number;
10944 l_demand_id                    number;
10945 l_priority                     number;
10946 l_level_id                     number;
10947 l_class                        varchar2(30);
10948 l_partner_id                   number;
10949 
10950 --  Agilent Allocated ATP Based on Planning Details changes
10951 l_stealing_quantity            NUMBER;
10952 --diag_atp
10953 L_GET_MAT_IN_REC               MSC_ATP_REQ.GET_MAT_IN_REC;
10954 l_get_mat_out_rec              MSC_ATP_REQ.get_mat_out_rec;
10955 
10956 l_item_info_rec                MSC_ATP_PVT.item_attribute_rec;
10957 
10958 -- time_phased_atp
10959 l_time_phased_atp               VARCHAR2(1) := 'N';
10960 l_mem_stealing_qty              NUMBER := 0;
10961 l_pf_stealing_qty               NUMBER := 0;
10962 l_atf_date_qty                  NUMBER;
10963 l_pf_item_id                    NUMBER;
10964 l_mat_atp_info_rec              MSC_ATP_REQ.Atp_Info_Rec;
10965 l_process_item_id               NUMBER;
10966 l_return_status                 VARCHAR2(1);
10967 l_item_to_use                   NUMBER;
10968 
10969 BEGIN
10970 
10971   -- Loop through the demand_class and do a single level check for each of them
10972   -- If partial quantity is available, insert that into the details.
10973   -- Keep decrementing the net_demand and exit if it is <= 0
10974   -- If the net_demand is still > 0, pass that back to the calling routing
10975 
10976   IF PG_DEBUG in ('Y', 'C') THEN
10977      msc_sch_wb.atp_debug('********* Begin Stealing ************');
10978   END IF;
10979 
10980   /* time_phased_atp changes begin
10981      initialize variables*/
10982   x_total_mem_stealing_qty        := 0;
10983   x_total_pf_stealing_qty         := 0;
10984 
10985   IF (p_atp_record.inventory_item_id <> p_atp_record.request_item_id) and
10986          p_atp_record.atf_date is not null THEN
10987         l_time_phased_atp := 'Y';
10988         l_process_item_id := p_atp_record.request_item_id;
10989         l_pf_item_id := MSC_ATP_PVT.G_ITEM_INFO_REC.product_family_id;
10990         IF PG_DEBUG in ('Y', 'C') THEN
10991                 msc_sch_wb.atp_debug('Stealing: ' || 'Time Phased ATP = ' || l_time_phased_atp);
10992                 msc_sch_wb.atp_debug('Stealing: ' || 'ATF Date = ' || p_atp_record.atf_date);
10993                 msc_sch_wb.atp_debug('Stealing: ' || 'l_pf_item_id = ' || l_pf_item_id);
10994         END IF;
10995   ELSE
10996         l_process_item_id := p_atp_record.inventory_item_id;
10997   END IF;
10998   -- time_phased_atp changes end
10999 
11000   /* Modularize Item and Org Info */
11001   MSC_ATP_PROC.get_global_item_info(p_atp_record.instance_id,
11002                                       ---bug 3917625: Pass in the real plan id
11003                                        --  -1,
11004                                        p_plan_id,
11005                                        l_process_item_id,
11006                                        p_atp_record.organization_id,
11007                                        l_item_info_rec );
11008   l_inv_item_id := MSC_ATP_PVT.G_ITEM_INFO_REC.dest_inv_item_id;
11009   /*l_inv_item_id := MSC_ATP_FUNC.get_inv_item_id (p_atp_record.instance_id,
11010                                                 p_atp_record.request_item_id,
11011                                                 null,
11012                                                 p_atp_record.organization_id);
11013    Modularize Item and Org Info */
11014 
11015   /* New allocation logic for time_phased_atp changes begin */
11016   IF l_time_phased_atp = 'Y' THEN
11017         IF p_atp_record.requested_ship_date <= p_atp_record.atf_date THEN
11018             IF MSC_ATP_PVT.G_MEM_RULE_WITHIN_ATF = 'Y' THEN
11019                 l_item_to_use := l_inv_item_id;
11020             ELSE
11021                 l_item_to_use := l_pf_item_id;
11022             END IF;
11023         ELSE
11024             IF MSC_ATP_PVT.G_PF_RULE_OUTSIDE_ATF = 'Y' THEN
11025                 l_item_to_use := l_pf_item_id;
11026             ELSE
11027                 l_item_to_use := l_inv_item_id;
11028             END IF;
11029         END IF;
11030   ELSE
11031         l_item_to_use := l_inv_item_id;
11032   END IF;
11033   IF PG_DEBUG in ('Y', 'C') THEN
11034    msc_sch_wb.atp_debug('ATP_Check: ' || 'p_atp_record.requested_ship_date = '||p_atp_record.requested_ship_date);
11035    msc_sch_wb.atp_debug('ATP_Check: ' || 'Item to be used = '||l_item_to_use);
11036   END IF;
11037   /* New allocation logic for time_phased_atp changes end */
11038 
11039   BEGIN
11040 
11041    -- Changes for Bug 2384551 start
11042   IF G_HIERARCHY_PROFILE = 1 THEN
11043 
11044   SELECT mv.priority, mv.level_id, mv.class, mv.partner_id
11045   INTO   l_priority, l_level_id, l_class, l_partner_id
11046   FROM   msc_item_hierarchy_mv mv
11047   WHERE  mv.inventory_item_id = l_item_to_use /* New allocation logic for time_phased_atp changes*/
11048   AND    mv.organization_id = p_atp_record.organization_id
11049   AND    mv.sr_instance_id = p_atp_record.instance_id
11050   AND    p_atp_record.requested_ship_date BETWEEN effective_date
11051                                           AND disable_date
11052   AND    mv.demand_class = p_atp_record.demand_class
11053   AND    mv.level_id = -1;
11054 
11055   ELSE
11056 
11057   SELECT mv.priority, mv.level_id, mv.class, mv.partner_id
11058   INTO   l_priority, l_level_id, l_class, l_partner_id
11059   FROM   msc_item_hierarchy_mv mv
11060   WHERE  mv.inventory_item_id = l_item_to_use /* New allocation logic for time_phased_atp changes*/
11061   AND    mv.organization_id = p_atp_record.organization_id
11062   AND    mv.sr_instance_id = p_atp_record.instance_id
11063   AND    p_atp_record.requested_ship_date BETWEEN effective_date
11064                                           AND disable_date
11065   AND    mv.demand_class = p_atp_record.demand_class
11066   AND    mv.level_id <> -1;
11067 
11068   END IF;
11069    -- Changes for Bug 2384551 end
11070 
11071   EXCEPTION
11072     WHEN NO_DATA_FOUND THEN
11073       IF PG_DEBUG in ('Y', 'C') THEN
11074          msc_sch_wb.atp_debug('Stealing: ' || 'No Data found ');
11075       END IF;
11076       l_priority := NULL;
11077       l_level_id := NULL;
11078       l_partner_id := NULL;
11079       l_class := NULL;
11080   END ;
11081 
11082   IF PG_DEBUG in ('Y', 'C') THEN
11083      msc_sch_wb.atp_debug('Stealing: ' || 'l_priority = '||l_priority);
11084      msc_sch_wb.atp_debug('Stealing: ' || 'l_level_id = '||l_level_id);
11085      msc_sch_wb.atp_debug('Stealing: ' || 'l_partner_id = '||l_partner_id);
11086      msc_sch_wb.atp_debug('Stealing: ' || 'l_class = '||l_class);
11087      msc_sch_wb.atp_debug('Stealing: ' || 'before finding the lower priority dc ');
11088   END IF;
11089 
11090   IF l_level_id = -1 THEN
11091     IF PG_DEBUG in ('Y', 'C') THEN
11092        msc_sch_wb.atp_debug('Stealing: ' || 'l_level_id = '||l_level_id);
11093     END IF;
11094 
11095     SELECT mv.demand_class, mv.priority, mv.allocation_percent
11096     BULK COLLECT INTO l_demand_class_tab_new, l_dmd_class_priority_tab_new, l_allocation_percent_tab	--6359986
11097     FROM   msc_item_hierarchy_mv mv
11098     WHERE  mv.inventory_item_id = l_item_to_use -- time_phased_atp
11099     AND    mv.organization_id = p_atp_record.organization_id
11100     AND    mv.sr_instance_id = p_atp_record.instance_id
11101     AND    p_atp_record.requested_ship_date BETWEEN effective_date
11102                                             AND disable_date
11103     AND    mv.priority  > l_priority
11104     AND    mv.level_id = l_level_id
11105     ORDER BY mv.priority asc , mv.allocation_percent desc ;
11106 
11107   ELSIF l_level_id = 1 THEN
11108     IF PG_DEBUG in ('Y', 'C') THEN
11109        msc_sch_wb.atp_debug('Stealing: ' || 'l_level_id = '||l_level_id);
11110     END IF;
11111 
11112     SELECT mv.demand_class, mv.priority, mv.allocation_percent
11113     BULK COLLECT INTO l_demand_class_tab_new, l_dmd_class_priority_tab_new, l_allocation_percent_tab	--6359986
11114     FROM   msc_item_hierarchy_mv mv
11115     WHERE  mv.inventory_item_id = l_item_to_use -- time_phased_atp
11116     AND    mv.organization_id = p_atp_record.organization_id
11117     AND    mv.sr_instance_id = p_atp_record.instance_id
11118     AND    p_atp_record.requested_ship_date BETWEEN effective_date
11119                                             AND disable_date
11120     AND    mv.priority  > l_priority
11121     AND    mv.level_id = l_level_id
11122     ORDER BY mv.priority  , mv.class  ;
11123 
11124   ELSIF l_level_id = 2 THEN
11125 
11126     IF PG_DEBUG in ('Y', 'C') THEN
11127        msc_sch_wb.atp_debug('Stealing: ' || 'l_level_id = '||l_level_id);
11128     END IF;
11129     SELECT mv1.demand_class, mv1.priority, mv1.allocation_percent
11130     BULK COLLECT INTO l_demand_class_tab_new, l_dmd_class_priority_tab_new,l_allocation_percent_tab	--6359986
11131     FROM   msc_item_hierarchy_mv mv1
11132     WHERE  mv1.inventory_item_id = l_item_to_use -- time_phased_atp
11133     AND    mv1.organization_id = p_atp_record.organization_id
11134     AND    mv1.sr_instance_id = p_atp_record.instance_id
11135     AND    p_atp_record.requested_ship_date BETWEEN mv1.effective_date
11136                                             AND mv1.disable_date
11137     AND    mv1.priority  > l_priority
11138     AND    mv1.level_id = l_level_id
11139     AND    NOT (trunc(mv1.priority, -3) = trunc(l_priority, -3)
11140             AND (mv1.class <> l_class))
11141 --    ORDER BY mv1.priority asc, mv1.allocation_percent desc;
11142       ORDER BY trunc(mv1.priority, -3), mv1.class ,
11143                trunc(mv1.priority, -2), mv1.partner_id;
11144 
11145   ELSIF l_level_id = 3 THEN
11146 
11147     IF PG_DEBUG in ('Y', 'C') THEN
11148        msc_sch_wb.atp_debug('Stealing: ' || 'l_level_id = '||l_level_id);
11149     END IF;
11150     SELECT mv1.demand_class, mv1.priority, mv1.allocation_percent
11151     BULK COLLECT INTO l_demand_class_tab_new, l_dmd_class_priority_tab_new,l_allocation_percent_tab	--6359986
11152     FROM   msc_item_hierarchy_mv mv1
11153     WHERE  mv1.inventory_item_id = l_item_to_use -- time_phased_atp
11154     AND    mv1.organization_id = p_atp_record.organization_id
11155     AND    mv1.sr_instance_id = p_atp_record.instance_id
11156     AND    p_atp_record.requested_ship_date BETWEEN mv1.effective_date
11157                                             AND mv1.disable_date
11158     AND    mv1.priority  > l_priority
11159     AND    mv1.level_id = l_level_id
11160     AND    NOT (trunc(mv1.priority, -3) = trunc(l_priority, -3)
11161             AND (mv1.class <> l_class))
11162     AND    NOT (trunc(mv1.priority, -2) = trunc(l_priority, -2)
11163             AND (mv1.class = l_class)
11164             AND (mv1.partner_id <> l_partner_id))
11165 --    ORDER BY mv1.priority asc, mv1.allocation_percent desc;
11166     ORDER BY trunc(mv1.priority, -3), mv1.class ,
11167              trunc(mv1.priority, -2), mv1.partner_id,
11168              mv1.priority, mv1.partner_site_id;
11169 
11170   END IF;
11171 
11172   IF PG_DEBUG in ('Y', 'C') THEN
11173      msc_sch_wb.atp_debug('Stealing: ' || 'l_demand_class_tab_new.count = '||l_demand_class_tab_new.count);--6359986
11174   END IF;
11175   --diag_atp
11176   l_get_mat_in_rec.rounding_control_flag := MSC_ATP_PVT.G_ITEM_INFO_REC.rounding_control_type;
11177   l_get_mat_in_rec.dest_inv_item_id := l_inv_item_id;
11178 --6359986 start
11179 --bug5974491:Others with allocation 0% will be considered valid or invalid demand class
11180 
11181   FOR i in 1..l_demand_class_tab_new.COUNT LOOP
11182 
11183 
11184   IF (REPLACE(l_demand_class_tab_new(i),FND_GLOBAL.LOCAL_CHR(13),' ') in  ('-1','-1 -1 -1','-1 -1') ) then
11185 
11186 
11187         IF(MSC_ATP_PVT.G_ZERO_ALLOCATION_PERC = 'N') then
11188          IF l_allocation_percent_tab(i) <> 0 THEN
11189             l_demand_class_tab.EXTEND;
11190             l_demand_class_priority_tab.EXTEND;
11191             l_demand_class_tab(i) := l_demand_class_tab_new(i);
11192             l_demand_class_priority_tab(i) := l_dmd_class_priority_tab_new(i);
11193           END IF;
11194         ELSE
11195             l_demand_class_tab.EXTEND;
11196             l_demand_class_priority_tab.EXTEND;
11197             l_demand_class_tab(i) := l_demand_class_tab_new(i);
11198             l_demand_class_priority_tab(i) := l_dmd_class_priority_tab_new(i);
11199         END IF;
11200   ELSE
11201 
11202           l_demand_class_tab.EXTEND;
11203           l_demand_class_priority_tab.EXTEND;
11204           l_demand_class_tab(i) := l_demand_class_tab_new(i);
11205           l_demand_class_priority_tab(i) := l_dmd_class_priority_tab_new(i);
11206   END IF;
11207 
11208   END LOOP; --6359986 end
11209 
11210   FOR i in 1..l_demand_class_tab.COUNT LOOP
11211         l_demand_class := l_demand_class_tab(i);
11212         IF PG_DEBUG in ('Y', 'C') THEN
11213            msc_sch_wb.atp_debug('Stealing: ' || 'Inside the loop of demand class');
11214            msc_sch_wb.atp_debug('Stealing: ' || 'l_demand_class'||l_demand_class);
11215         END IF;
11216 
11217         -- time_phased_atp changes begin
11218         l_mat_atp_info_rec.instance_id                       := p_atp_record.instance_id;
11219         l_mat_atp_info_rec.plan_id                           := p_plan_id;
11220         l_mat_atp_info_rec.level                             := p_level + 1;
11221         l_mat_atp_info_rec.identifier                        := p_atp_record.identifier;
11222         l_mat_atp_info_rec.scenario_id                       := p_scenario_id;
11223         l_mat_atp_info_rec.inventory_item_id                 := p_atp_record.inventory_item_id;
11224         l_mat_atp_info_rec.request_item_id                   := p_atp_record.request_item_id;
11225         l_mat_atp_info_rec.organization_id                   := p_atp_record.organization_id;
11226         l_mat_atp_info_rec.requested_date                    := p_atp_record.requested_ship_date;
11227         l_mat_atp_info_rec.quantity_ordered                  := p_net_demand;
11228         l_mat_atp_info_rec.demand_class                      := l_demand_class;
11229         l_mat_atp_info_rec.insert_flag                       := p_atp_record.insert_flag;
11230         l_mat_atp_info_rec.rounding_control_flag             := l_get_mat_in_rec.rounding_control_flag;
11231         l_mat_atp_info_rec.dest_inv_item_id                  := l_get_mat_in_rec.dest_inv_item_id;
11232         l_mat_atp_info_rec.infinite_time_fence_date          := l_get_mat_in_rec.infinite_time_fence_date;
11233         l_mat_atp_info_rec.plan_name                         := l_get_mat_in_rec.plan_name;
11234         l_mat_atp_info_rec.optimized_plan                    := l_get_mat_in_rec.optimized_plan;
11235         l_mat_atp_info_rec.requested_date_quantity           := null;
11236         l_mat_atp_info_rec.atp_date_this_level               := null;
11237         l_mat_atp_info_rec.atp_date_quantity_this_level      := null;
11238         l_mat_atp_info_rec.substitution_window               := null;
11239         l_mat_atp_info_rec.atf_date                          := p_atp_record.atf_date;   -- For time_phased_atp
11240         l_mat_atp_info_rec.refresh_number                    := p_refresh_number;   -- For summary enhancement
11241         l_mat_atp_info_rec.shipping_cal_code                 := p_atp_record.shipping_cal_code; -- Bug 3371817
11242 
11243         MSC_ATP_REQ.Get_Material_Atp_Info(
11244                 l_mat_atp_info_rec,
11245                 l_atp_period,
11246                 l_atp_supply_demand,
11247                 x_return_status);
11248 
11249         l_atf_date_qty                               := l_mat_atp_info_rec.atf_date_quantity;
11250         l_atp_date_this_level                        := l_mat_atp_info_rec.atp_date_this_level;
11251         l_atp_date_quantity_this_level               := l_mat_atp_info_rec.atp_date_quantity_this_level;
11252         l_get_mat_out_rec.atp_rule_name              := l_mat_atp_info_rec.atp_rule_name;
11253         l_get_mat_out_rec.infinite_time_fence_date   := l_mat_atp_info_rec.infinite_time_fence_date;
11254         p_atp_record.requested_date_quantity         := l_mat_atp_info_rec.requested_date_quantity;
11255 
11256 	IF PG_DEBUG in ('Y', 'C') THEN
11257 	   msc_sch_wb.atp_debug('Back in Stealing');
11258 	END IF;
11259         -- time_phased_atp changes end
11260 
11261         -- 1430561: move the p_net_demand calculation to the end so that
11262         -- we insert the right demand quantity
11263         -- p_net_demand := (p_net_demand - greatest(l_requested_date_quantity, 0)) ;
11264 
11265 	IF PG_DEBUG in ('Y', 'C') THEN
11266 	   msc_sch_wb.atp_debug('Stealing: ' || 'p_net_demand = '||to_char(p_net_demand));
11267 	END IF;
11268         -- if we don't have atp for this demand class , don't bother
11269         -- generate pegging tree, demand record.
11270 
11271 	IF PG_DEBUG in ('Y', 'C') THEN
11272 	   msc_sch_wb.atp_debug('Stealing: ' || 'p_atp_record.requested_date_quantity = '||
11273                      p_atp_record.requested_date_quantity);
11274 	END IF;
11275 
11276         IF p_atp_record.requested_date_quantity > 0 THEN
11277 
11278             -- time_phased_atp changes begin
11279             IF ((MSC_ATP_PVT.G_INV_CTP = 4) AND
11280                 (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
11281                 (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
11282                 (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1) ) THEN
11283 
11284                IF l_time_phased_atp = 'N' THEN
11285                        l_stealing_quantity :=  LEAST(
11286                                                 p_atp_record.requested_date_quantity,
11287                                                 p_net_demand);
11288                                                 --p_atp_record.quantity_ordered);
11289 
11290                        MSC_ATP_DB_UTILS.Add_Stealing_Supply_Details (
11291                                         p_plan_id,
11292                                         -- rajjain 07/18/2003 bug 3010846
11293                                         -- pass component's sales order line id
11294                                         --p_atp_record.identifier,
11295                                         p_atp_record.demand_source_line,
11296                                         --p_atp_record.inventory_item_id,
11297                                         l_inv_item_id,
11298                                         p_atp_record.organization_id,
11299                                         p_atp_record.instance_id,
11300                                         l_stealing_quantity,
11301                                         p_atp_record.demand_class,
11302                                         l_demand_class,
11303                                         p_atp_record.requested_ship_date,
11304                                         l_demand_id,
11305                                         p_refresh_number,
11306                                         p_atp_record.ato_model_line_id,-- For summary enhancement
11307                                         p_atp_record.demand_source_type,  --cmro
11308                                         --bug3684383
11309                                         p_atp_record.order_number);
11310                ELSE
11311                        IF p_atp_record.requested_ship_date <= p_atp_record.atf_date THEN
11312                                 l_mem_stealing_qty :=  LEAST(
11313                                                 p_atp_record.requested_date_quantity,
11314                                                 p_net_demand);
11315                        ELSE
11316                                 IF (p_atp_record.requested_date_quantity - NVL(l_atf_date_qty, 0)) > p_net_demand THEN
11317                                         l_pf_stealing_qty := p_net_demand;
11318                                         l_mem_stealing_qty := 0;
11319                                 ELSE
11320                                         l_pf_stealing_qty := p_atp_record.requested_date_quantity - NVL(l_atf_date_qty, 0);
11321                                         l_mem_stealing_qty := LEAST(NVL(l_atf_date_qty, 0), (p_net_demand - l_pf_stealing_qty));
11322                                 END IF;
11323                        END IF;
11324 
11325                        -- get family item's dest id
11326                        l_pf_item_id := MSC_ATP_FUNC.get_inv_item_id (
11327                                                 p_atp_record.instance_id,
11328                                                 p_atp_record.inventory_item_id,
11329                                                 null,
11330                                                 p_atp_record.organization_id
11331                                              );
11332                        MSC_ATP_PF.Add_PF_Stealing_Supply_Details (
11333                                         p_plan_id,
11334                                         p_atp_record.demand_source_line,
11335                                         l_inv_item_id,
11336                                         l_pf_item_id,
11337                                         p_atp_record.organization_id,
11338                                         p_atp_record.instance_id,
11339                                         l_mem_stealing_qty,
11340                                         l_pf_stealing_qty,
11341                                         p_atp_record.demand_class,
11342                                         l_demand_class,
11343                                         p_atp_record.requested_ship_date,
11344                                         p_atp_record.atf_date,
11345                                         p_refresh_number, -- for summary enhancement
11346                                         l_demand_id,
11347                                         p_atp_record.ato_model_line_id,
11348                                         p_atp_record.demand_source_type,--cmro
11349                                         --bug3684383
11350                                         p_atp_record.order_number,
11351                                         l_return_status);
11352                         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
11353                                 IF PG_DEBUG in ('Y', 'C') THEN
11354                                         msc_sch_wb.atp_debug('Stealing: ' || 'Error occured in procedure Add_PF_Stealing_Supply_Details');
11355                                 END IF;
11356                                 RAISE FND_API.G_EXC_ERROR;
11357                         END IF;
11358                END IF;
11359                -- time_phased_atp changes end
11360 
11361                IF PG_DEBUG in ('Y', 'C') THEN
11362                   msc_sch_wb.atp_debug('Stealing Supply Id : ' || l_demand_id);
11363                END IF;
11364 
11365             ELSIF l_time_phased_atp = 'Y' THEN
11366                 IF p_atp_record.requested_ship_date <= p_atp_record.atf_date THEN
11367                         l_mem_stealing_qty :=  LEAST(
11368                                         p_atp_record.requested_date_quantity,
11369                                         p_net_demand);
11370                 ELSE
11371                         IF (p_atp_record.requested_date_quantity - NVL(l_atf_date_qty, 0)) > p_net_demand THEN
11372                                 l_pf_stealing_qty := p_net_demand;
11373                                 l_mem_stealing_qty := 0;
11374                         ELSE
11375                                 l_pf_stealing_qty := p_atp_record.requested_date_quantity - NVL(l_atf_date_qty, 0);
11376                                 l_mem_stealing_qty := LEAST(NVL(l_atf_date_qty, 0), (p_net_demand - l_pf_stealing_qty));
11377                         END IF;
11378                 END IF;
11379                 IF PG_DEBUG in ('Y', 'C') THEN
11380                   msc_sch_wb.atp_debug('Stealing: l_mem_stealing_qty = ' || l_mem_stealing_qty);
11381                   msc_sch_wb.atp_debug('Stealing: l_pf_stealing_qty = ' || l_pf_stealing_qty);
11382                 END IF;
11383             END IF;
11384 
11385             x_total_mem_stealing_qty    := x_total_mem_stealing_qty + l_mem_stealing_qty;
11386             x_total_pf_stealing_qty     := x_total_pf_stealing_qty + l_pf_stealing_qty;
11387             IF PG_DEBUG in ('Y', 'C') THEN
11388                 msc_sch_wb.atp_debug('Stealing: x_total_mem_stealing_qty = ' || x_total_mem_stealing_qty);
11389                 msc_sch_wb.atp_debug('Stealing: x_total_pf_stealing_qty = ' || x_total_pf_stealing_qty);
11390             END IF;
11391             -- time_phased_atp changes end
11392 
11393             -- populate insert rec to pegging tree for this demand
11394             -- for performance reason, we call these function here and
11395             -- then populate the pegging tree with the values
11396 
11397             /* Modularize Item and Org Info */
11398             l_inv_item_name := MSC_ATP_PVT.G_ITEM_INFO_REC.item_name;
11399             /*l_inv_item_name := MSC_ATP_FUNC.get_inv_item_name(p_atp_record.instance_id,
11400                                       p_atp_record.inventory_item_id,
11401                                       p_atp_record.organization_id);
11402              Modularize Item and Org Info */
11403 
11404             /* Modularize Item and Org Info */
11405             MSC_ATP_PROC.get_global_org_info (p_atp_record.instance_id,
11406                                  p_atp_record.organization_id );
11407             l_org_code := MSC_ATP_PVT.G_ORG_INFO_REC.org_code;
11408             IF PG_DEBUG in ('Y', 'C') THEN
11409                msc_sch_wb.atp_debug('Stealing: ' || 'Modular Use Org_code : '||l_org_code);
11410             END IF;
11411             /*l_org_code := MSC_ATP_FUNC.get_org_code(p_atp_record.instance_id,
11412                                          p_atp_record.organization_id);
11413              Modularize Item and Org Info */
11414 
11415             l_pegging_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
11416             l_pegging_rec.order_line_id:= MSC_ATP_PVT.G_ORDER_LINE_ID;
11417             -- l_pegging_rec.parent_pegging_id:= l_pegging_id;
11418             l_pegging_rec.parent_pegging_id:= p_parent_pegging_id;
11419             l_pegging_rec.atp_level:= p_level + 1;
11420             l_pegging_rec.organization_id:= p_atp_record.organization_id;
11421             l_pegging_rec.organization_code:= l_org_code;
11422             l_pegging_rec.identifier1:= p_atp_record.instance_id;
11423             l_pegging_rec.identifier2 := p_plan_id;
11424 
11425             -- Bug 1419121, Insert Demand id to be used while deleting.
11426 
11427             l_pegging_rec.identifier3 := l_demand_id;
11428             --l_pegging_rec.identifier3 := NULL;
11429 
11430             -- time_phased_atp changes begin
11431             IF l_time_phased_atp = 'Y' and p_atp_record.requested_ship_date <= p_atp_record.atf_date THEN
11432                     l_pegging_rec.inventory_item_id:= p_atp_record.request_item_id;
11433                     l_pegging_rec.inventory_item_name := l_inv_item_name;
11434             ELSE
11435                     l_pegging_rec.inventory_item_id:= p_atp_record.inventory_item_id;
11436                     l_pegging_rec.inventory_item_name := MSC_ATP_FUNC.get_inv_item_name(
11437                                                             p_atp_record.instance_id,
11438                                                             p_atp_record.inventory_item_id,
11439                                                             p_atp_record.organization_id
11440                                                          );
11441             END IF;
11442             l_pegging_rec.aggregate_time_fence_date:= p_atp_record.atf_date;
11443             l_pegging_rec.request_item_id:= p_atp_record.request_item_id;
11444             -- time_phased_atp changes end
11445 
11446             l_pegging_rec.resource_id := NULL;
11447             l_pegging_rec.resource_code := NULL;
11448             l_pegging_rec.department_id := NULL;
11449             l_pegging_rec.department_code := NULL;
11450             l_pegging_rec.supplier_id := NULL;
11451             l_pegging_rec.supplier_name := NULL;
11452             l_pegging_rec.supplier_site_id := NULL;
11453             l_pegging_rec.supplier_site_name := NULL;
11454             l_pegging_rec.scenario_id:= p_scenario_id;
11455             l_pegging_rec.supply_demand_source_type:= MSC_ATP_PVT.ATP;
11456             l_pegging_rec.supply_demand_quantity:=
11457                             p_atp_record.requested_date_quantity;
11458             l_pegging_rec.supply_demand_date:= p_atp_record.requested_ship_date;
11459             l_pegging_rec.supply_demand_type:= 2;
11460             l_pegging_rec.source_type := 0;
11461 
11462             l_pegging_rec.char1 := l_demand_class;
11463 
11464             -- bug 1527660
11465             --l_pegging_rec.allocated_quantity :=
11466             --                l_atp_insert_rec.quantity_ordered;
11467             --bug3830147 Earlier allocated_quantity was getting passed as Null always.
11468             -- Populating it with correct stealing qty so that it can be used for
11469             -- workflow notification.
11470             IF l_time_phased_atp = 'Y' THEN
11471                l_pegging_rec.allocated_quantity := l_mem_stealing_qty + l_pf_stealing_qty;
11472 
11473             ELSE
11474                l_pegging_rec.allocated_quantity := LEAST(
11475                                                     p_atp_record.requested_date_quantity,
11476                                                     p_net_demand);
11477             END IF;
11478 
11479             IF PG_DEBUG in ('Y', 'C') THEN
11480                 msc_sch_wb.atp_debug('Stealing: l_pegging_rec.allocated_quantity  = ' ||l_pegging_rec.allocated_quantity);
11481             END IF;
11482 	    l_pegging_rec.component_identifier :=
11483                          NVL(p_atp_record.component_identifier, MSC_ATP_PVT.G_COMP_LINE_ID);
11484 
11485             -- for demo:1153192
11486             IF ((p_search = 1)
11487                    AND ( p_atp_record.quantity_ordered >=
11488                         l_mat_atp_info_rec.requested_date_quantity)) THEN
11489                   l_pegging_rec.constraint_flag := 'Y';
11490             ELSE
11491                   l_pegging_rec.constraint_flag := 'N';
11492 
11493             END IF;
11494 
11495             --diag_atp
11496             l_pegging_rec.plan_name := p_atp_record.plan_name;
11497             l_pegging_rec.required_quantity:= p_net_demand;
11498             l_pegging_rec.required_date := p_atp_record.requested_ship_date;
11499             l_pegging_rec.infinite_time_fence := l_get_mat_out_rec.infinite_time_fence_date;
11500             l_pegging_rec.atp_rule_name := l_get_mat_out_rec.atp_rule_name;
11501             l_pegging_rec.rounding_control := MSC_ATP_PVT.G_ITEM_INFO_REC.rounding_control_type;
11502             l_pegging_rec.atp_flag := MSC_ATP_PVT.G_ITEM_INFO_REC.atp_flag;
11503             l_pegging_rec.atp_component_flag := MSC_ATP_PVT.G_ITEM_INFO_REC.atp_comp_flag;
11504             l_pegging_rec.pegging_type := 3; ---atp supply node
11505             l_pegging_rec.postprocessing_lead_time := MSC_ATP_PVT.G_ITEM_INFO_REC.post_pro_lt;
11506             l_pegging_rec.preprocessing_lead_time := MSC_ATP_PVT.G_ITEM_INFO_REC.pre_pro_lt;
11507             l_pegging_rec.fixed_lead_time := MSC_ATP_PVT.G_ITEM_INFO_REC.fixed_lt;
11508             l_pegging_rec.variable_lead_time := MSC_ATP_PVT.G_ITEM_INFO_REC.variable_lt;
11509             l_pegging_rec.weight_capacity := MSC_ATP_PVT.G_ITEM_INFO_REC.unit_weight;
11510             l_pegging_rec.volume_capacity := MSC_ATP_PVT.G_ITEM_INFO_REC.unit_volume;
11511             l_pegging_rec.weight_uom := MSC_ATP_PVT.G_ITEM_INFO_REC.weight_uom;
11512             l_pegging_rec.volume_uom := MSC_ATP_PVT.G_ITEM_INFO_REC.volume_uom;
11513             l_pegging_rec.allocation_rule := MSC_ATP_PVT.G_ALLOCATION_RULE_NAME;
11514 
11515             l_pegging_rec.summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;     -- for summary enhancement
11516             l_pegging_rec.demand_class := l_demand_class;
11517             -- Bug 3826234 start
11518             IF PG_DEBUG in ('Y', 'C') THEN
11519               msc_sch_wb.atp_debug('ATP_Check: ' || '----------- Calendars passed to Pegging -----------------');
11520               msc_sch_wb.atp_debug('ATP_Check: ' || 'shipping_cal_code = '      ||p_atp_record.shipping_cal_code);
11521               msc_sch_wb.atp_debug('ATP_Check: ' || 'receiving_cal_code = '     ||p_atp_record.receiving_cal_code);
11522               msc_sch_wb.atp_debug('ATP_Check: ' || 'intransit_cal_code = '     ||p_atp_record.intransit_cal_code);
11523               msc_sch_wb.atp_debug('ATP_Check: ' || 'manufacturing_cal_code = ' ||p_atp_record.manufacturing_cal_code);
11524               msc_sch_wb.atp_debug('ATP_Check: ' || 'to_organization_id = ' ||p_atp_record.to_organization_id);
11525             END IF;
11526             IF p_parent_pegging_id = MSC_ATP_PVT.G_DEMAND_PEGGING_ID THEN
11527                l_pegging_rec.shipping_cal_code      :=  p_atp_record.shipping_cal_code;
11528                l_pegging_rec.receiving_cal_code     :=  p_atp_record.receiving_cal_code;
11529                l_pegging_rec.intransit_cal_code     :=  p_atp_record.intransit_cal_code;
11530                l_pegging_rec.manufacturing_cal_code :=  p_atp_record.manufacturing_cal_code;
11531                IF PG_DEBUG in ('Y', 'C') THEN
11532                   msc_sch_wb.atp_debug('ATP_Check: ' || 'Inside IF');
11533                END IF;
11534             ELSIF NVL(p_atp_record.to_organization_id,p_atp_record.organization_id)
11535                                                              <> p_atp_record.organization_id THEN
11536                l_pegging_rec.shipping_cal_code      :=  p_atp_record.shipping_cal_code;
11537                l_pegging_rec.receiving_cal_code     :=  p_atp_record.receiving_cal_code;
11538                l_pegging_rec.intransit_cal_code     :=  p_atp_record.intransit_cal_code;
11539                l_pegging_rec.manufacturing_cal_code :=  NULL;
11540                IF PG_DEBUG in ('Y', 'C') THEN
11541                   msc_sch_wb.atp_debug('ATP_Check: ' || 'Inside ELSIF');
11542                END IF;
11543             ELSE
11544                l_pegging_rec.manufacturing_cal_code :=  p_atp_record.manufacturing_cal_code;
11545                l_pegging_rec.shipping_cal_code      :=  NULL;
11546                l_pegging_rec.receiving_cal_code     :=  NULL;
11547                l_pegging_rec.intransit_cal_code     :=  NULL;
11548                IF PG_DEBUG in ('Y', 'C') THEN
11549                   msc_sch_wb.atp_debug('ATP_Check: ' || 'Inside ELSE');
11550                END IF;
11551             END IF;
11552             -- Bug 3826234 end
11553             MSC_ATP_DB_UTILS.Add_Pegging(l_pegging_rec, l_atp_pegging_id);
11554 
11555             -- Add pegging_id to the l_atp_period and l_atp_supply_demand
11556 
11557             FOR i in 1..l_atp_period.Level.COUNT LOOP
11558                 l_atp_period.Pegging_Id(i) := l_atp_pegging_id;
11559                 l_atp_period.End_Pegging_Id(i) := MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
11560             END LOOP;
11561 
11562             -- dsting: supply/demand details pl/sql table no longer used
11563 /*          FOR i in 1..l_atp_supply_demand.Level.COUNT LOOP
11564                 l_atp_supply_demand.Pegging_Id(i) := l_atp_pegging_id;
11565                 l_atp_supply_demand.End_Pegging_Id(i) := MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
11566             END LOOP;
11567 */
11568 
11569 	    IF p_atp_record.insert_flag <> 0 THEN
11570 		    MSC_ATP_DB_UTILS.move_SD_temp_into_mrp_details(l_atp_pegging_id,
11571 					  MSC_ATP_PVT.G_DEMAND_PEGGING_ID);
11572 	    END IF;
11573 
11574             MSC_ATP_PROC.Details_Output(l_atp_period,
11575                              l_atp_supply_demand,
11576                              x_atp_period,
11577                              x_atp_supply_demand,
11578                              x_return_status);
11579 
11580         END IF;  -- IF p_atp_record.requested_date_quantity > 0
11581 
11582         -- 1430561: we moved the p_net_demand to here
11583         p_net_demand := (p_net_demand - greatest(l_mat_atp_info_rec.requested_date_quantity, 0)) ;
11584 
11585         IF (p_net_demand <= 0) then
11586           EXIT;
11587         END IF;
11588 
11589   END LOOP;
11590   IF PG_DEBUG in ('Y', 'C') THEN
11591      msc_sch_wb.atp_debug('********* END Stealing ************');
11592   END IF;
11593 END Stealing;
11594 END MSC_AATP_PVT;