DBA Data[Home] [Help]

PACKAGE BODY: APPS.MSC_AATP_PVT

Source


1 PACKAGE BODY MSC_AATP_PVT AS
2 /* $Header: MSCAATPB.pls 120.25 2011/12/06 09:42:32 vjuluri 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,
182                               decode(MSC_ATP_PVT.G_INTERNAL_SALES_ORDER,'N',  --10425563
183                                decode(d.origination_type,
184                                  6, decode(d.source_organization_id,
185                                     NULL, DECODE(D.DEMAND_CLASS, null, null,
186                                        DECODE(p_demand_class, '-1',
187                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
188                                              null, null, p_item_id, p_org_id,
189                                              p_instance_id, TRUNC(
190                                                   DECODE(RECORD_SOURCE,
191                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
192                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
193                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
194                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
195                                                                       --plan by request date,promise date, schedule date
196                                              		p_level_id, D.DEMAND_CLASS),
197                                              D.DEMAND_CLASS)),
198                                     -23453, DECODE(D.DEMAND_CLASS, null, null,
199                                        DECODE(p_demand_class, '-1',
200                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
201                                              null, null, p_item_id, p_org_id,
202                                              p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
203                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
204                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
205                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
206                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
207                                              					p_level_id, D.DEMAND_CLASS),  --plan by request date,promise date, schedule date
208                                              D.DEMAND_CLASS)),
209                                     d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
210                                        DECODE(p_demand_class, '-1',
211                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
212                                              null, null, p_item_id, p_org_id,
213                                              p_instance_id, TRUNC(
214                                              	    DECODE(RECORD_SOURCE,
215                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
216                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
217                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
218                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
219                                              			p_level_id, D.DEMAND_CLASS),  --plan by request date,promise date, schedule date
220                                              D.DEMAND_CLASS)), TO_CHAR(NULL)),
221                                  30, decode(d.source_organization_id,
222                                     NULL, DECODE(D.DEMAND_CLASS, null, null,
223                                        DECODE(p_demand_class, '-1',
224                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
225                                              null, null, p_item_id, p_org_id,
226                                              p_instance_id, TRUNC(
227                                              	    DECODE(RECORD_SOURCE,
228                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
229                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
230                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
231                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
232                                              				p_level_id, D.DEMAND_CLASS),  --plan by request date,promise date, schedule date
233                                              D.DEMAND_CLASS)),
234                                     -23453, DECODE(D.DEMAND_CLASS, null, null,
235                                        DECODE(p_demand_class, '-1',
236                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
237                                              null, null, p_item_id, p_org_id,
238                                              p_instance_id, TRUNC(
239                                              	    DECODE(RECORD_SOURCE,
240                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
241                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
242                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
243                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
244                                              				p_level_id, D.DEMAND_CLASS),  --plan by request date,promise date, schedule date
245                                              D.DEMAND_CLASS)),
246                                     d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
247                                        DECODE(p_demand_class, '-1',
248                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
249                                              null, null, p_item_id, p_org_id,
250                                              p_instance_id, TRUNC(
251                                              	    DECODE(RECORD_SOURCE,
252                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
253                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
254                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
255                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
256                                              			p_level_id, D.DEMAND_CLASS),  --plan by request date,promise date, schedule date
257                                              D.DEMAND_CLASS)), TO_CHAR(NULL)),
258                                  DECODE(D.DEMAND_CLASS, null, null,
259                                     DECODE(p_demand_class, '-1',
260                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
261                                           null, null, p_item_id, p_org_id,
262                                           p_instance_id, TRUNC(
263                                                    DECODE(RECORD_SOURCE,
264                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
265                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
266                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
267                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
268                                           			p_level_id, D.DEMAND_CLASS),  --plan by request date,promise date, schedule date
269                                           D.DEMAND_CLASS))),
270                                  DECODE(D.DEMAND_CLASS, null, null,  --10425563
271                                     DECODE(p_demand_class, '-1',
272                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
273                                           null, null, p_item_id, p_org_id,
274                                           p_instance_id, TRUNC(
275                                                    DECODE(RECORD_SOURCE,
276                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
277                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
278                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
279                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
280                                           			p_level_id, D.DEMAND_CLASS),  --plan by request date,promise date, schedule date
281                                           D.DEMAND_CLASS)) ),
282                               -- rajjain end
283                               2, DECODE(D.CUSTOMER_ID, NULL, TO_CHAR(NULL),
284                                                    0, TO_CHAR(NULL),
285                                  -- rajjain begin 07/19/2002
286                                  decode(MSC_ATP_PVT.G_INTERNAL_SALES_ORDER,'N', --10425563
287                                  decode(d.origination_type,
288                                     6, decode(d.source_organization_id,
289                                        NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
290                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
291                                              p_org_id, p_instance_id,
292                                              TRUNC(DECODE(RECORD_SOURCE,
293                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
294                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
295                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
296                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), --plan by request date,promise date, schedule date
297                                              p_level_id, NULL),
298                                        -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
299                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
300                                              p_org_id, p_instance_id,
301                                              TRUNC(DECODE(RECORD_SOURCE,
302                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
303                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
304                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
305                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), --plan by request date,promise date, schedule date
306                                              p_level_id, NULL),
307                                        d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
308                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
309                                              p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
310                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
311                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
312                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
313                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), --plan by request date,promise date, schedule date
314                                              p_level_id, NULL),
315                                        TO_CHAR(NULL)),
316                                     30, decode(d.source_organization_id,
317                                        NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
318                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
319                                              p_org_id, p_instance_id,
320                                              	TRUNC(DECODE(RECORD_SOURCE,
321                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
322                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
323                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
324                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), --plan by request date,promise date, schedule date
325                                              p_level_id, NULL),
326                                        -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
327                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
328                                              p_org_id, p_instance_id,
329                                              	TRUNC(DECODE(RECORD_SOURCE,
330                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
331                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
332                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
333                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), --plan by request date,promise date, schedule date
334                                              p_level_id, NULL),
335                                        d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
336                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
337                                              p_org_id, p_instance_id,
338                                              TRUNC(DECODE(RECORD_SOURCE,
339                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
340                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
341                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
342                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), --plan by request date,promise date, schedule date
343                                              p_level_id, NULL),
344                                        TO_CHAR(NULL)),
345                                     MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(D.CUSTOMER_ID, D.SHIP_TO_SITE_ID,
346                                        p_item_id, p_org_id, p_instance_id,
347                                        	          TRUNC(DECODE(RECORD_SOURCE,
348                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
349                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
350                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
351                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))))--plan by request date,promise date, schedule date
352                                        				,p_level_id, NULL)),
353                                     MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, --10425563
354                                        p_item_id, p_org_id, p_instance_id,
355                                        	          TRUNC(DECODE(RECORD_SOURCE,
356                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
357                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
358                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
359                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))))--plan by request date,promise date, schedule date
360                                        				,p_level_id, NULL))
361                                        				)),
362                                  -- rajjain end 07/19/2002
363                            p_demand_class, 1,
364                              Decode(D.Demand_Class, NULL, --4365873 If l_demand_class is not null and demand class is populated
365                              -- on  supplies record then 0 should be allocated.
366                               MSC_AATP_FUNC.Get_Item_Demand_Alloc_Percent(p_plan_id,
367                                  D.DEMAND_ID,
368 		                                 TRUNC(DECODE(RECORD_SOURCE,
369                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
370                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
371                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
372                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), --plan by request date,promise date, schedule date
373                                  D.USING_ASSEMBLY_ITEM_ID,
374                                  DECODE(D.SOURCE_ORGANIZATION_ID,
375                                     -23453, null,
376                                     D.SOURCE_ORGANIZATION_ID), -- 1665483
377                                  p_item_id,
378                                  p_org_id, -- 1665483
379                                  p_instance_id,
380                                  decode (d.origination_type, -100, 30,d.origination_type), --5027568
381                                  DECODE(G_HIERARCHY_PROFILE,
382                                  /*-----------------------------------------------------------------+
383                                  | rajjain begin 07/19/2002                                         |
384                                  |                                                                  |
385                                  | Case 1: For internal sales orders [origination type is in (6,30) |
386                                  |         and source_organization_id is not null                   |
387                                  |         and <> organization_id] -> Return p_demand_class         |
388                                  | Case 2: For others if DEMAND_CLASS is null then return null      |
389                                  |           else if p_demand_class is '-1' then call               |
390                                  |           Get_Hierarchy_Demand_class else return DEMAND_CLASS    |
391                                  +-----------------------------------------------------------------*/
392                                     1,
393                                     decode(MSC_ATP_PVT.G_INTERNAL_SALES_ORDER,'N', --10425563
394                                      decode(d.origination_type,
395                                        6, decode(d.source_organization_id,
396                                           NULL, DECODE(D.DEMAND_CLASS, null, null,
397                                              DECODE(p_demand_class, '-1',
398                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
399                                                    null, null, p_item_id, p_org_id,
400                                                    p_instance_id, TRUNC(
401                                                    DECODE(RECORD_SOURCE,
402                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
403                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
404                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
405                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), --plan by request date,promise date, schedule date
406                                                    	p_level_id, D.DEMAND_CLASS),
407                                                 D.DEMAND_CLASS)),
408                                           -23453, 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                                           d.organization_id, 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                                           p_demand_class),
433                                     30, decode(d.source_organization_id,
434                                        NULL, DECODE(D.DEMAND_CLASS, null, null,
435                                           DECODE(p_demand_class, '-1',
436                                              MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
437                                                 null, null, p_item_id, p_org_id,
438                                                 p_instance_id,
439                                                     TRUNC(DECODE(RECORD_SOURCE,
440                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
441                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
442                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
443                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), --plan by request date,promise date, schedule date
444                                                 		p_level_id, D.DEMAND_CLASS),
445                                              D.DEMAND_CLASS)),
446                                        -23453, DECODE(D.DEMAND_CLASS, null, null,
447                                           DECODE(p_demand_class, '-1',
448                                              MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
449                                                 null, null, p_item_id, p_org_id,
450                                                 p_instance_id,
451                                                      TRUNC(DECODE(RECORD_SOURCE,
452                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
453                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
454                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
455                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), --plan by request date,promise date, schedule date
456                                                 			p_level_id, D.DEMAND_CLASS),
457                                              D.DEMAND_CLASS)),
458                                        d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
459                                           DECODE(p_demand_class, '-1',
460                                              MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
461                                                 null, null, p_item_id, p_org_id,
462                                                 p_instance_id,
463                                                      TRUNC(DECODE(RECORD_SOURCE,
464                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
465                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
466                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
467                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), --plan by request date,promise date, schedule date
468                                                 	p_level_id, D.DEMAND_CLASS),
469                                              D.DEMAND_CLASS)),
470                                        p_demand_class),
471                                     DECODE(D.DEMAND_CLASS, null, null,
472                                        DECODE(p_demand_class, '-1',
473                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
474                                              null, null, p_item_id, p_org_id,
475                                              p_instance_id, TRUNC(
476                                              	DECODE(RECORD_SOURCE,
477                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
478                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
479                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
480                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))))--plan by request date,promise date, schedule date
481                                              		, p_level_id, D.DEMAND_CLASS),
482                                           D.DEMAND_CLASS))),
483                                     DECODE(D.DEMAND_CLASS, null, null,  --10425563
484                                        DECODE(p_demand_class, '-1',
485                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
486                                              null, null, p_item_id, p_org_id,
487                                              p_instance_id, TRUNC(
488                                              	DECODE(RECORD_SOURCE,
489                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
490                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
491                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
492                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))))--plan by request date,promise date, schedule date
493                                              		, p_level_id, D.DEMAND_CLASS),
494                                           D.DEMAND_CLASS))),
495                                     -- rajjain end
496                                     2, DECODE(D.CUSTOMER_ID, NULL, p_demand_class,
497                                                    0, p_demand_class,
498                                        -- rajjain begin 07/19/2002
499                                        decode(MSC_ATP_PVT.G_INTERNAL_SALES_ORDER,'N', --10425563
500                                        decode(d.origination_type,
501                                           6, decode(d.source_organization_id,
502                                              NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
503                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
504                                                    p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
505                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
506                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
507                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
508                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
509                                                    p_level_id, NULL),
510                                              -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
511                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
512                                                    p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
513                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
514                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
515                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
516                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
517                                                    p_level_id, NULL),
518                                              d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
519                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
520                                                    p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
521                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
522                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
523                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
524                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
525                                                    p_level_id, NULL),
526                                              p_demand_class),
527                                           30, decode(d.source_organization_id,
528                                              NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
529                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
530                                                    p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
531                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
532                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
533                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
534                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
535                                                    p_level_id, NULL),
536                                              -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
537                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
538                                                    p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
539                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
540                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
541                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
542                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
543                                                    p_level_id, NULL),
544                                              d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
545                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
546                                                    p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
547                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
548                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
549                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
550                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
551                                                    p_level_id, NULL),
552                                              p_demand_class),
553                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
554                                         D.CUSTOMER_ID, D.SHIP_TO_SITE_ID,
555                                         p_item_id, p_org_id, p_instance_id,
556                                         TRUNC(DECODE
557                                                     (RECORD_SOURCE,
558                                                      2,
559                                                      NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
560                                                      DECODE(
561                                                      	    MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
562                                                             2,
563                                                             NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
564                                                             NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)
565                                                             )
566                                                       )
567                                                ),p_level_id, NULL)
568                                        ),
569                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(  --10425563
570                                        			D.CUSTOMER_ID, D.SHIP_TO_SITE_ID,
571                                              		p_item_id, p_org_id, p_instance_id,
572                                              		TRUNC(DECODE
573                                              			(RECORD_SOURCE,
574                                                          	 2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
575                                                             	 DECODE(
576                                                             	 	MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
577                                                                    	2,
578                                                                    	NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
579                                                                       	NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)
580                                                                       	)
581                                                                   )
582                                                                  ),p_level_id, NULL)
583                                       ))),
584                                        -- rajjain end 07/19/2002
585                                        p_demand_class,
586                                        p_level_id),0)) SD_QTY --4365873
587                    FROM    -- MSC_CALENDAR_DATES C, -- 2859130
588                            MSC_DEMANDS D
589                    WHERE        D.PLAN_ID = p_plan_id
590                    AND                D.SR_INSTANCE_ID = p_instance_id
591                    AND                D.INVENTORY_ITEM_ID = p_item_id
592                    AND         D.ORGANIZATION_ID = p_org_id
593                    AND         D.USING_REQUIREMENT_QUANTITY <> 0 --4501434
594                    AND         D.ORIGINATION_TYPE NOT IN (5,7,8,9,11,15,22,28,29,31,52,70) -- For summary enhancement
595                    -- 2859130
596                    -- Bug1990155, 1995835 exclude the expired lots demand datreya 9/18/2001
597                    -- Bug 1530311, need to exclude forecast, ngoel 12/05/2000
598                    -- AND                C.CALENDAR_CODE = p_cal_code
599                    -- AND                C.EXCEPTION_SET_ID = p_cal_exc_set_id
600                    -- AND         C.SR_INSTANCE_ID = D.SR_INSTANCE_ID
601                    -- since we store repetitive schedule demand in different ways for
602                    -- ods (total quantity on start date) and pds  (daily quantity from
603                    -- start date to end date), we need to make sure we only select work day
604                    -- for pds's repetitive schedule demand.
605                    -- AND         C.CALENDAR_DATE BETWEEN TRUNC(D.USING_ASSEMBLY_DEMAND_DATE) AND
606                    --             TRUNC(NVL(D.ASSEMBLY_DEMAND_COMP_DATE,
607                    --                       D.USING_ASSEMBLY_DEMAND_DATE))
608                    -- AND         (( D.ORIGINATION_TYPE = 4
609                    --         AND C.SEQ_NUM IS NOT NULL) OR
610                    --         ( D.ORIGINATION_TYPE  <> 4))
611                    -- AND         C.PRIOR_DATE < NVL(p_itf,
612                    --                  C.PRIOR_DATE + 1)
613                    --bug3693892 added trunc
614                    AND   TRUNC(DECODE(RECORD_SOURCE,
615                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
616                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
617                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
618                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))) <
619                          TRUNC(NVL(p_itf,DECODE(RECORD_SOURCE,
620                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
621                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
622                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
623                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))+1))
624                    UNION ALL
625                    SELECT  --C.NEXT_DATE SD_DATE, -- 2859130
626                            --TRUNC(NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE)) SD_DATE,
627                            GREATEST(TRUNC(NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE)),p_sys_next_date) SD_DATE,--3099066
628                            NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY)*
629                               DECODE(DECODE(G_HIERARCHY_PROFILE,
630                                      --2424357: Convert the demand calls in case of others for
631                                      --- demand class allocated ATP
632                                      1, DECODE(S.DEMAND_CLASS, null, null,
633                                         DECODE(p_demand_class, '-1',
634                                            MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
635                                                          null,
636                                                          null,
637                                                          S.inventory_item_id,
638                                                          p_org_id,
639                                                          p_instance_id,
640                                                          trunc(nvl(S.firm_date, S.new_schedule_date)),
641                                                          -- c.next_date, -- 2859130
642                                                          p_level_id,
643                                                          S.DEMAND_CLASS),S.DEMAND_CLASS)),
644                                      2, DECODE(S.CUSTOMER_ID, NULL, TO_CHAR(NULL),
645                                                        0, TO_CHAR(NULL),
646                                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
647                                                          S.CUSTOMER_ID,
648                                                          S.SHIP_TO_SITE_ID,
649                                                          S.inventory_item_id,
650                                                          p_org_id,
651                                                          p_instance_id,
652                                                          trunc(nvl(S.firm_date, S.new_schedule_date)),
653                                                          -- c.next_date, -- 2859130
654                                                          p_level_id,
655                                                          NULL))),
656                                  p_demand_class, 1,
657                                  NULL, nvl(MIHM.allocation_percent/100,1), --4365873
658                                  /*NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
659                                            p_instance_id,
660                                            s.inventory_item_id,
661                                            p_org_id,
662                                            null,
663                                            null,
664                                            p_demand_class,
665                                            -- c.next_date -- 2859130
666                                            trunc(nvl(s.firm_date, s.new_schedule_date))), 1),*/
667                                  DECODE(
668                                   MIHM.allocation_percent/100, --4365873
669                                  /*DECODE(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
670                                            p_instance_id,
671                                            s.inventory_item_id,
672                                            p_org_id,
673                                            null,
674                                            null,
675                                            p_demand_class,
676                                            -- c.next_date -- 2859130
677                                            trunc(nvl(s.firm_date, s.new_schedule_date))),*/
678                                    NULL, 1,
679                                  0)) SD_QTY
680                    FROM    -- MSC_CALENDAR_DATES C, -- 2859130
681                            MSC_SUPPLIES S,MSC_ITEM_HIERARCHY_MV MIHM
682                    WHERE   S.PLAN_ID = p_plan_id
683                    AND     S.SR_INSTANCE_ID = p_instance_id
684                    AND     S.INVENTORY_ITEM_ID = p_item_id
685                    AND     S.ORGANIZATION_ID = p_org_id
686                            -- Exclude Cancelled Supplies 2460645
687                    AND     NVL(S.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
688                    AND     NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY) <> 0 -- 1243985
689                    -- 2859130
690                    -- AND     C.CALENDAR_CODE = p_cal_code
691                    -- AND     C.EXCEPTION_SET_ID = p_cal_exc_set_id
692                    -- AND     C.SR_INSTANCE_ID = S.SR_INSTANCE_ID
693                    -- AND     C.CALENDAR_DATE BETWEEN TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE))
694                    --         AND TRUNC(NVL(S.LAST_UNIT_COMPLETION_DATE,
695                    --             NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)))
696                    -- AND     DECODE(S.LAST_UNIT_COMPLETION_DATE,
697                    --                    NULL, C.NEXT_SEQ_NUM, C.SEQ_NUM) IS NOT NULL
698                    -- AND     C.NEXT_DATE >= DECODE(S.ORDER_TYPE, 27, TRUNC(SYSDATE),
699                    --                                            28, TRUNC(SYSDATE),
700                    --                                            C.NEXT_DATE)
701                    --AND     C.NEXT_DATE < NVL(p_itf,
702                    --                          C.NEXT_DATE + 1)
703                    AND     trunc(NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE)) >=
704                                           trunc(DECODE(S.ORDER_TYPE, 27, SYSDATE,
705                                                                28, SYSDATE,
706                                                                NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE))) --4135752
707                    AND     trunc(NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE)) < trunc(NVL(p_itf,
708                                             NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE) + 1))
709 		--4365873
710                 AND    S.INVENTORY_ITEM_ID = MIHM.INVENTORY_ITEM_ID(+)
711                 AND    S.SR_INSTANCE_ID = MIHM.SR_INSTANCE_ID (+)
712                 AND    S.ORGANIZATION_ID = MIHM.ORGANIZATION_ID (+)
713                 AND    decode(MIHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
714                 AND    trunc(NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE)) >= MIHM.effective_date (+)
715                 AND    trunc(NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE)) <= MIHM.disable_date (+)
716                 AND    MIHM.demand_class (+) = p_demand_class
717                    )
718              GROUP BY SD_DATE
719              ORDER BY SD_DATE; --4698199
720 END item_alloc_avail_opt;
721 
722 -- unconstrained plan
723 --avjain All netting sqls have been changed to incorporate Plan by Request Date Enhancements
724 PROCEDURE item_alloc_avail_unopt (
725    p_item_id            IN NUMBER,
726    p_org_id             IN NUMBER,
727    p_instance_id        IN NUMBER,
728    p_plan_id            IN NUMBER,
729    p_demand_class       IN VARCHAR2,
730    p_level_id           IN NUMBER,
731    p_itf                IN DATE,
732    p_cal_code           IN VARCHAR2,
733    p_cal_exc_set_id     IN NUMBER,
734    p_sys_next_date	IN DATE,			--bug3099066
735    x_atp_dates          OUT NoCopy MRP_ATP_PUB.date_arr,
736    x_atp_qtys           OUT NoCopy MRP_ATP_PUB.number_arr
737 ) IS
738 BEGIN
739         IF PG_DEBUG in ('Y', 'C') THEN
740                 msc_sch_wb.atp_debug('item_alloc_avail_unopt: UNconstrained plan: ' || p_plan_id);
741         END IF;
742 
743              SELECT        SD_DATE,
744                            SUM(SD_QTY)
745              BULK COLLECT INTO
746                            x_atp_dates,
747                            x_atp_qtys
748              FROM (
749                    SELECT  ---C.CALENDAR_DATE SD_DATE,
750                    GREATEST(C.CALENDAR_DATE,p_sys_next_date) SD_DATE,--3099066
751                            -1* DECODE(D.ORIGINATION_TYPE,
752                                   4, D.DAILY_DEMAND_RATE,
753                                   --D.USING_REQUIREMENT_QUANTITY)*
754                                   (D.USING_REQUIREMENT_QUANTITY - NVL(d.reserved_quantity, 0)))*  --5027568
755                            DECODE(DECODE(G_HIERARCHY_PROFILE,
756                            /*------------------------------------------------------------------------+
757                            | rajjain begin 07/19/2002                                                |
758                            |                                                                         |
759                            | Case 1: For internal sales orders [origination type is in (6,30) and    |
760                            |            source_organization_id is not null and <> organization_id]   |
761                            |                  Return NULL                                            |
762                            | Case 2: For others if DEMAND_CLASS is null then return null             |
763                            |          else if p_demand_class is '-1' then call                       |
764                            |            Get_Hierarchy_Demand_class else return DEMAND_CLASS          |
765                            +------------------------------------------------------------------------*/
766                               1,
767                               decode(MSC_ATP_PVT.G_INTERNAL_SALES_ORDER,'N', --10425563
768                               decode(d.origination_type,
769                                  6, decode(d.source_organization_id,
770                                     NULL, DECODE(D.DEMAND_CLASS, null, null,
771                                        DECODE(p_demand_class, '-1',
772                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
773                                              null, null, p_item_id, p_org_id,
774                                              p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
775                                              D.DEMAND_CLASS)),
776                                     -23453, DECODE(D.DEMAND_CLASS, null, null,
777                                        DECODE(p_demand_class, '-1',
778                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
779                                              null, null, p_item_id, p_org_id,
780                                              p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
781                                              D.DEMAND_CLASS)),
782                                     d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
783                                        DECODE(p_demand_class, '-1',
784                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
785                                              null, null, p_item_id, p_org_id,
786                                              p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
787                                              D.DEMAND_CLASS)), TO_CHAR(NULL)),
788                                  30, decode(d.source_organization_id,
789                                     NULL, DECODE(D.DEMAND_CLASS, null, null,
790                                        DECODE(p_demand_class, '-1',
791                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
792                                              null, null, p_item_id, p_org_id,
793                                              p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
794                                              D.DEMAND_CLASS)),
795                                     -23453, DECODE(D.DEMAND_CLASS, null, null,
796                                        DECODE(p_demand_class, '-1',
797                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
798                                              null, null, p_item_id, p_org_id,
799                                              p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
800                                              D.DEMAND_CLASS)),
801                                     d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
802                                        DECODE(p_demand_class, '-1',
803                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
804                                              null, null, p_item_id, p_org_id,
805                                              p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
806                                              D.DEMAND_CLASS)), TO_CHAR(NULL)),
807                                  DECODE(D.DEMAND_CLASS, null, null,
808                                     DECODE(p_demand_class, '-1',
809                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
810                                           null, null, p_item_id, p_org_id,
811                                           p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
812                                           D.DEMAND_CLASS))),
813                                 DECODE(D.DEMAND_CLASS, null, null, --10425563
814                                     DECODE(p_demand_class, '-1',
815                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
816                                           null, null, p_item_id, p_org_id,
817                                           p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
818                                           D.DEMAND_CLASS))),
819                               -- rajjain end
820                               2, DECODE(D.CUSTOMER_ID, NULL, TO_CHAR(NULL),
821                                                    0, TO_CHAR(NULL),
822                                  -- rajjain begin 07/19/2002
823                                  decode(MSC_ATP_PVT.G_INTERNAL_SALES_ORDER,'N', --10425563
824                                  decode(d.origination_type,
825                                     6, decode(d.source_organization_id,
826                                        NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
827                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
828                                              p_org_id, p_instance_id, c.calendar_date,
829                                              p_level_id, NULL),
830                                        -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
831                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
832                                              p_org_id, p_instance_id, c.calendar_date,
833                                              p_level_id, NULL),
834                                        d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
835                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
836                                              p_org_id, p_instance_id, c.calendar_date,
837                                              p_level_id, NULL),
838                                        TO_CHAR(NULL)),
839                                     30, decode(d.source_organization_id,
840                                        NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
841                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
842                                              p_org_id, p_instance_id, c.calendar_date,
843                                              p_level_id, NULL),
844                                        -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
845                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
846                                              p_org_id, p_instance_id, c.calendar_date,
847                                              p_level_id, NULL),
848                                        d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
849                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
850                                              p_org_id, p_instance_id, c.calendar_date,
851                                              p_level_id, NULL),
852                                        TO_CHAR(NULL)),
853                                     MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(D.CUSTOMER_ID, D.SHIP_TO_SITE_ID,
854                                        p_item_id, p_org_id, p_instance_id,
855                                        c.calendar_date,p_level_id, NULL)),
856                                    MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, --10425563
857                                        p_item_id, p_org_id, p_instance_id,
858                                        c.calendar_date,p_level_id, NULL))
859                                        )),
860                                  -- rajjain end 07/19/2002
861                            p_demand_class, 1,
862                              Decode(D.Demand_Class, NULL, --4365873
863                               MSC_AATP_FUNC.Get_Item_Demand_Alloc_Percent(p_plan_id,
864                                  D.DEMAND_ID,
865                                  c.calendar_date,
866                                  D.USING_ASSEMBLY_ITEM_ID,
867                                  DECODE(D.SOURCE_ORGANIZATION_ID,
868                                     -23453, null,
869                                     D.SOURCE_ORGANIZATION_ID), -- 1665483
870                                  p_item_id,
871                                  p_org_id, -- 1665483
872                                  p_instance_id,
873                                  decode (d.origination_type, -100, 30,d.origination_type), --5027568
874                                  DECODE(G_HIERARCHY_PROFILE,
875                                  /*-----------------------------------------------------------------+
876                                  | rajjain begin 07/19/2002                                         |
877                                  |                                                                  |
878                                  | Case 1: For internal sales orders [origination type is in (6,30) |
879                                  |         and source_organization_id is not null                   |
880                                  |         and <> organization_id] -> Return p_demand_class         |
881                                  | Case 2: For others if DEMAND_CLASS is null then return null      |
882                                  |           else if p_demand_class is '-1' then call               |
883                                  |           Get_Hierarchy_Demand_class else return DEMAND_CLASS    |
884                                  +-----------------------------------------------------------------*/
885                                     1,
886                                     decode(MSC_ATP_PVT.G_INTERNAL_SALES_ORDER,'N', --10425563
887                                     decode(d.origination_type,
888                                        6, decode(d.source_organization_id,
889                                           NULL, DECODE(D.DEMAND_CLASS, null, null,
890                                              DECODE(p_demand_class, '-1',
891                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
892                                                    null, null, p_item_id, p_org_id,
893                                                    p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
894                                                 D.DEMAND_CLASS)),
895                                           -23453, DECODE(D.DEMAND_CLASS, null, null,
896                                              DECODE(p_demand_class, '-1',
897                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
898                                                    null, null, p_item_id, p_org_id,
899                                                    p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
900                                                 D.DEMAND_CLASS)),
901                                           d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
902                                              DECODE(p_demand_class, '-1',
903                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
904                                                    null, null, p_item_id, p_org_id,
905                                                    p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
906                                                 D.DEMAND_CLASS)),
907                                           p_demand_class),
908                                     30, decode(d.source_organization_id,
909                                        NULL, DECODE(D.DEMAND_CLASS, null, null,
910                                           DECODE(p_demand_class, '-1',
911                                              MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
912                                                 null, null, p_item_id, p_org_id,
913                                                 p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
914                                              D.DEMAND_CLASS)),
915                                        -23453, DECODE(D.DEMAND_CLASS, null, null,
916                                           DECODE(p_demand_class, '-1',
917                                              MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
918                                                 null, null, p_item_id, p_org_id,
919                                                 p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
920                                              D.DEMAND_CLASS)),
921                                        d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
922                                           DECODE(p_demand_class, '-1',
923                                              MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
924                                                 null, null, p_item_id, p_org_id,
925                                                 p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
926                                              D.DEMAND_CLASS)),
927                                        p_demand_class),
928                                     DECODE(D.DEMAND_CLASS, null, null,
929                                        DECODE(p_demand_class, '-1',
930                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
931                                              null, null, p_item_id, p_org_id,
932                                              p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
933                                           D.DEMAND_CLASS))),
934                                     DECODE(D.DEMAND_CLASS, null, null, --10425563
935                                        DECODE(p_demand_class, '-1',
936                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
937                                              null, null, p_item_id, p_org_id,
938                                              p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
939                                           D.DEMAND_CLASS))),
940                                     -- rajjain end
941                                     2, DECODE(D.CUSTOMER_ID, NULL, p_demand_class,
942                                                    0, p_demand_class,
943                                        -- rajjain begin 07/19/2002
944                                        decode(MSC_ATP_PVT.G_INTERNAL_SALES_ORDER,'N', --10425563
945                                        decode(d.origination_type,
946                                           6, decode(d.source_organization_id,
947                                              NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
948                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
949                                                    p_org_id, p_instance_id, c.calendar_date,
950                                                    p_level_id, NULL),
951                                              -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
952                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
953                                                    p_org_id, p_instance_id, c.calendar_date,
954                                                    p_level_id, NULL),
955                                              d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
956                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
957                                                    p_org_id, p_instance_id, c.calendar_date,
958                                                    p_level_id, NULL),
959                                              p_demand_class),
960                                           30, decode(d.source_organization_id,
961                                              NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
962                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
963                                                    p_org_id, p_instance_id, c.calendar_date,
964                                                    p_level_id, NULL),
965                                              -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
966                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
967                                                    p_org_id, p_instance_id, c.calendar_date,
968                                                    p_level_id, NULL),
969                                              d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
970                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
971                                                    p_org_id, p_instance_id, c.calendar_date,
972                                                    p_level_id, NULL),
973                                              p_demand_class),
974                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(D.CUSTOMER_ID, D.SHIP_TO_SITE_ID,
975                                              p_item_id, p_org_id, p_instance_id,
976                                              c.calendar_date,p_level_id, NULL)),
977                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(D.CUSTOMER_ID, D.SHIP_TO_SITE_ID,  --10425563
978                                              p_item_id, p_org_id, p_instance_id,
979                                              c.calendar_date,p_level_id, NULL))
980                                              )),
981                                        -- rajjain end 07/19/2002
982                                        p_demand_class,
983                                        p_level_id),0)) SD_QTY --4365873
984                    FROM    MSC_CALENDAR_DATES C,
985                            MSC_DEMANDS D
986                    WHERE        D.PLAN_ID = p_plan_id
987                    AND                D.SR_INSTANCE_ID = p_instance_id
988                    AND                D.INVENTORY_ITEM_ID = p_item_id
989                    AND         D.ORGANIZATION_ID = p_org_id
990                    AND         D.USING_REQUIREMENT_QUANTITY <> 0 --4501434
991                    AND         D.ORIGINATION_TYPE NOT IN (5,7,8,9,11,15,22,28,29,31,52,70) -- For summary enhancement
992                    -- Bug1990155, 1995835 exclude the expired lots demand datreya 9/18/2001
993                    -- Bug 1530311, need to exclude forecast, ngoel 12/05/2000
994                    AND                C.CALENDAR_CODE = p_cal_code
995                    AND                C.EXCEPTION_SET_ID = p_cal_exc_set_id
996                    AND         C.SR_INSTANCE_ID = D.SR_INSTANCE_ID
997                    -- since we store repetitive schedule demand in different ways for
998                    -- ods (total quantity on start date) and pds  (daily quantity from
999                    -- start date to end date), we need to make sure we only select work day
1000                    -- for pds's repetitive schedule demand.
1001                    AND         C.CALENDAR_DATE
1002                    -- Bug 3550296 and 3574164. IMPLEMENT_DATE AND DMD_SATISFIED_DATE are changed to
1003                    -- IMPLEMENT_SHIP_DATE and PLANNED_SHIP_DATE resp.
1004                                BETWEEN TRUNC(DECODE(RECORD_SOURCE,
1005                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1006                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1007                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1008                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))))
1009                                AND     TRUNC(NVL(D.ASSEMBLY_DEMAND_COMP_DATE,
1010                                              DECODE(RECORD_SOURCE,
1011                                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1012                                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1013                                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1014                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))))
1015                    AND         (( D.ORIGINATION_TYPE = 4
1016                            AND C.SEQ_NUM IS NOT NULL) OR
1017                            ( D.ORIGINATION_TYPE  <> 4))
1018                    -- AND         C.PRIOR_DATE < NVL(p_itf,
1019                    --                  C.PRIOR_DATE + 1)
1020                    AND         C.CALENDAR_DATE < NVL(p_itf, C.CALENDAR_DATE+1)
1021                    UNION ALL
1022                    SELECT  ---C.CALENDAR_DATE SD_DATE,
1023                            GREATEST(CS.CALENDAR_DATE,p_sys_next_date) SD_DATE,--3099066
1024                            NVL(CS.FIRM_QUANTITY,CS.NEW_ORDER_QUANTITY)*
1025                               DECODE(DECODE(G_HIERARCHY_PROFILE,
1026                                      --2424357: Convert the demand calls in case of others for
1027                                      --- demand class allocated ATP
1028                                      1, DECODE(CS.DEMAND_CLASS, null, null,
1029                                         DECODE(p_demand_class, '-1',
1030                                            MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1031                                                          null,
1032                                                          null,
1033                                                          cs.inventory_item_id,
1034                                                          p_org_id,
1035                                                          p_instance_id,
1036                                                          cs.calendar_date,
1037                                                          p_level_id,
1038                                                          CS.DEMAND_CLASS),CS.DEMAND_CLASS)),
1039                                      2, DECODE(CS.CUSTOMER_ID, NULL, TO_CHAR(NULL),
1040                                                        0, TO_CHAR(NULL),
1041                                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1042                                                          CS.CUSTOMER_ID,
1043                                                          CS.SHIP_TO_SITE_ID,
1044                                                          cs.inventory_item_id,
1045                                                          p_org_id,
1046                                                          p_instance_id,
1047                                                          cs.calendar_date,
1048                                                          p_level_id,
1049                                                          NULL))),
1050                                  p_demand_class, 1,
1051                                  NULL, nvl(MIHM.allocation_percent/100,1), --4365873
1052                                  /*NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
1053                                            p_instance_id,
1054                                            cs.inventory_item_id,
1055                                            p_org_id,
1056                                            null,
1057                                            null,
1058                                            p_demand_class,
1059                                            cs.calendar_date), 1),*/
1060                                  DECODE(
1061                                  MIHM.allocation_percent/100, --4365873
1062                                  /*DECODE(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
1063                                            p_instance_id,
1064                                            cs.inventory_item_id,
1065                                            p_org_id,
1066                                            null,
1067                                            null,
1068                                            p_demand_class,
1069                                            cs.calendar_date),*/
1070                                    NULL, 1,
1071                                  0)) SD_QTY
1072                 FROM
1073                 (
1074                 select
1075                 	C.NEXT_DATE,
1076                 	C.CALENDAR_DATE,
1077 			S.FIRM_QUANTITY,
1078 			S.NEW_ORDER_QUANTITY,
1079 			S.DEMAND_CLASS,
1080 			s.inventory_item_id,
1081 			S.CUSTOMER_ID,
1082 			S.SHIP_TO_SITE_ID,
1083 			S.SR_INSTANCE_ID,
1084 			S.ORGANIZATION_ID
1085                    FROM    MSC_CALENDAR_DATES C,
1086                            MSC_SUPPLIES S
1087                    WHERE   S.PLAN_ID = p_plan_id
1088                    AND     S.SR_INSTANCE_ID = p_instance_id
1089                    AND     S.INVENTORY_ITEM_ID = p_item_id
1090                    AND     S.ORGANIZATION_ID = p_org_id
1091                            -- Exclude Cancelled Supplies 2460645
1092                    AND     NVL(S.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
1093                    AND     NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY) <> 0 -- 1243985
1094                    AND     C.CALENDAR_CODE = p_cal_code
1095                    AND     C.EXCEPTION_SET_ID = p_cal_exc_set_id
1096                    AND     C.SR_INSTANCE_ID = S.SR_INSTANCE_ID
1097                    AND     C.CALENDAR_DATE BETWEEN TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE))
1098                            AND TRUNC(NVL(S.LAST_UNIT_COMPLETION_DATE,
1099                                NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)))
1100                    AND     DECODE(S.LAST_UNIT_COMPLETION_DATE,
1101                                       NULL, C.NEXT_SEQ_NUM, C.SEQ_NUM) IS NOT NULL
1102                    -- 2859130
1103                    AND     C.CALENDAR_DATE >= DECODE(S.ORDER_TYPE, 27, TRUNC(SYSDATE),
1104                                                                    28, TRUNC(SYSDATE),
1105                                                                    C.CALENDAR_DATE)
1106                    AND     C.CALENDAR_DATE < NVL(p_itf,
1107                                             C.CALENDAR_DATE + 1)
1108                    --AND     C.NEXT_DATE >= DECODE(S.ORDER_TYPE, 27, TRUNC(SYSDATE),
1109                    --                                            28, TRUNC(SYSDATE),
1110                    --                                            C.NEXT_DATE)
1111                    --AND     C.NEXT_DATE < NVL(p_itf,
1112                    --                          C.NEXT_DATE + 1)
1113                    )CS,
1114 		MSC_ITEM_HIERARCHY_MV MIHM
1115 		WHERE
1116 		--4365873
1117                        CS.INVENTORY_ITEM_ID = MIHM.INVENTORY_ITEM_ID(+)
1118                 AND    CS.SR_INSTANCE_ID = MIHM.SR_INSTANCE_ID (+)
1119                 AND    CS.ORGANIZATION_ID = MIHM.ORGANIZATION_ID (+)
1120                 AND    decode(MIHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
1121                 AND    CS.NEXT_DATE >= MIHM.effective_date (+)
1122                 AND    CS.NEXT_DATE <= MIHM.disable_date (+)
1123                 AND    MIHM.demand_class (+) = p_demand_class
1124                    )
1125                 GROUP BY SD_DATE
1126                 ORDER BY SD_DATE;--4698199
1127 END item_alloc_avail_unopt;
1128 --avjain All netting sqls have been changed to incorporate Plan by Request Date Enhancements
1129 PROCEDURE item_alloc_avail_opt_unalloc (
1130    p_item_id            IN NUMBER,
1131    p_org_id             IN NUMBER,
1132    p_instance_id        IN NUMBER,
1133    p_plan_id            IN NUMBER,
1134    p_demand_class       IN VARCHAR2,
1135    p_level_id           IN NUMBER,
1136    p_itf                IN DATE,
1137    p_cal_code           IN VARCHAR2,
1138    p_cal_exc_set_id     IN NUMBER,
1139    p_sys_next_date	IN DATE,                        --bug3099066
1140    x_atp_dates          OUT NoCopy MRP_ATP_PUB.date_arr,
1141    x_atp_qtys           OUT NoCopy MRP_ATP_PUB.number_arr,
1142    x_atp_unalloc_qtys   OUT NoCopy MRP_ATP_PUB.number_arr
1143 ) IS
1144 BEGIN
1145         IF PG_DEBUG in ('Y', 'C') THEN
1146                 msc_sch_wb.atp_debug('item_alloc_avail_opt_unalloc: Get unallocated qtys as well. constrained plan: ' || p_plan_id);
1147         END IF;
1148               -- Bug 3550296 and 3574164. IMPLEMENT_DATE AND DMD_SATISFIED_DATE are changed to
1149               -- IMPLEMENT_SHIP_DATE and PLANNED_SHIP_DATE resp.
1150               SELECT        SD_DATE,
1151                            SUM(UNALLOC_SD_QTY),
1152                            SUM(SD_QTY)
1153              BULK COLLECT INTO
1154                            x_atp_dates,
1155                            x_atp_unalloc_qtys,
1156                            x_atp_qtys
1157              FROM (
1158                    SELECT  -- C.PRIOR_DATE SD_DATE, -- 2859130
1159 			GREATEST(
1160                            TRUNC(DECODE(RECORD_SOURCE,
1161                            	2,
1162                            	NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1163                            	DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1164                            	2,
1165                            	(NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
1166                            	NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))))
1167                            	,p_sys_next_date) SD_DATE, --3099066 --plan by request date
1168 
1169                            -1* (D.USING_REQUIREMENT_QUANTITY - NVL(d.reserved_quantity, 0)) UNALLOC_SD_QTY, --5027568
1170                            -1* (D.USING_REQUIREMENT_QUANTITY - NVL(d.reserved_quantity, 0))* --5027568
1171                            DECODE(DECODE(G_HIERARCHY_PROFILE,
1172                            /*------------------------------------------------------------------------+
1173                            | rajjain begin 07/19/2002                                                |
1174                            |                                                                         |
1175                            | Case 1: For internal sales orders [origination type is in (6,30) and    |
1176                            |            source_organization_id is not null and <> organization_id]   |
1177                            |                  Return NULL                                            |
1178                            | Case 2: For others if DEMAND_CLASS is null then return null             |
1179                            |          else if p_demand_class is '-1' then call                       |
1180                            |            Get_Hierarchy_Demand_class else return DEMAND_CLASS          |
1181                            +------------------------------------------------------------------------*/
1182                               1,
1183                               decode(MSC_ATP_PVT.G_INTERNAL_SALES_ORDER,'N', --10425563
1184                               decode(d.origination_type,
1185                                  6, decode(d.source_organization_id,
1186                                     NULL, DECODE(D.DEMAND_CLASS, null, null,
1187                                        DECODE(p_demand_class, '-1',
1188                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1189                                              null, null, p_item_id, p_org_id,
1190                                              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)))), p_level_id, D.DEMAND_CLASS),
1195                                              D.DEMAND_CLASS)),
1196                                     -23453, DECODE(D.DEMAND_CLASS, null, null,
1197                                        DECODE(p_demand_class, '-1',
1198                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1199                                              null, null, p_item_id, p_org_id,
1200                                              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)))), p_level_id, D.DEMAND_CLASS),
1205                                              D.DEMAND_CLASS)),
1206                                     d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
1207                                        DECODE(p_demand_class, '-1',
1208                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1209                                              null, null, p_item_id, p_org_id,
1210                                              p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1211                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1212                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1213                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1214                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
1215                                              D.DEMAND_CLASS)), TO_CHAR(NULL)),
1216                                  30, decode(d.source_organization_id,
1217                                     NULL, DECODE(D.DEMAND_CLASS, null, null,
1218                                        DECODE(p_demand_class, '-1',
1219                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1220                                              null, null, p_item_id, p_org_id,
1221                                              p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1222                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1223                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1224                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1225                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
1226                                              D.DEMAND_CLASS)),
1227                                     -23453, DECODE(D.DEMAND_CLASS, null, null,
1228                                        DECODE(p_demand_class, '-1',
1229                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1230                                              null, null, p_item_id, p_org_id,
1231                                              p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1232                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1233                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1234                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1235                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
1236                                              D.DEMAND_CLASS)),
1237                                     d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
1238                                        DECODE(p_demand_class, '-1',
1239                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1240                                              null, null, p_item_id, p_org_id,
1241                                              p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1242                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1243                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1244                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1245                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
1246                                              D.DEMAND_CLASS)), TO_CHAR(NULL)),
1247                                  DECODE(D.DEMAND_CLASS, null, null,
1248                                     DECODE(p_demand_class, '-1',
1249                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1250                                           null, null, p_item_id, p_org_id,
1251                                           p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1252                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1253                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1254                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1255                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
1256                                           D.DEMAND_CLASS))),
1257                                  DECODE(D.DEMAND_CLASS, null, null, --10425563
1258                                     DECODE(p_demand_class, '-1',
1259                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1260                                           null, null, p_item_id, p_org_id,
1261                                           p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1262                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1263                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1264                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1265                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
1266                                           D.DEMAND_CLASS))),
1267                               -- rajjain end
1268                               2, DECODE(D.CUSTOMER_ID, NULL, TO_CHAR(NULL),
1269                                                    0, TO_CHAR(NULL),
1270                                  -- rajjain begin 07/19/2002
1271                                  decode(MSC_ATP_PVT.G_INTERNAL_SALES_ORDER,'N', --10425563
1272                                  decode(d.origination_type,
1273                                     6, decode(d.source_organization_id,
1274                                        NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1275                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1276                                              p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1277                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1278                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1279                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1280                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
1281                                              p_level_id, NULL),
1282                                        -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1283                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1284                                              p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1285                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1286                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1287                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1288                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
1289                                              p_level_id, NULL),
1290                                        d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1291                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1292                                              p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1293                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1294                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1295                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1296                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
1297                                              p_level_id, NULL),
1298                                        TO_CHAR(NULL)),
1299                                     30, decode(d.source_organization_id,
1300                                        NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1301                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1302                                              p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1303                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1304                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1305                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1306                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
1307                                              p_level_id, NULL),
1308                                        -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1309                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1310                                              p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1311                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1312                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1313                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1314                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
1315                                              p_level_id, NULL),
1316                                        d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1317                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1318                                              p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1319                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1320                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1321                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1322                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
1323                                              p_level_id, NULL),
1324                                        TO_CHAR(NULL)),
1325                                     MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(D.CUSTOMER_ID, D.SHIP_TO_SITE_ID,
1326                                        p_item_id, p_org_id, p_instance_id,
1327                                        TRUNC(DECODE(RECORD_SOURCE,
1328                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1329                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1330                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1331                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),p_level_id, NULL)),
1332                                     MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, --10425563
1333                                        p_item_id, p_org_id, p_instance_id,
1334                                        TRUNC(DECODE(RECORD_SOURCE,
1335                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1336                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1337                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1338                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),p_level_id, NULL))
1339                                     )),
1340                                  -- rajjain end 07/19/2002
1341                            p_demand_class, 1,
1342                               Decode(D.Demand_Class, NULL, --4365873
1343                               MSC_AATP_FUNC.Get_Item_Demand_Alloc_Percent(p_plan_id,
1344                                  D.DEMAND_ID,
1345                                  TRUNC(DECODE(RECORD_SOURCE,
1346                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1347                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1348                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1349                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
1350                                  D.USING_ASSEMBLY_ITEM_ID,
1351                                  DECODE(D.SOURCE_ORGANIZATION_ID,
1352                                     -23453, null,
1353                                     D.SOURCE_ORGANIZATION_ID), -- 1665483
1354                                  p_item_id,
1355                                  p_org_id, -- 1665483
1356                                  p_instance_id,
1357                                  decode (d.origination_type, -100, 30,d.origination_type), --5027568
1358                                  DECODE(G_HIERARCHY_PROFILE,
1359                                  /*-----------------------------------------------------------------+
1360                                  | rajjain begin 07/19/2002                                         |
1361                                  |                                                                  |
1362                                  | Case 1: For internal sales orders [origination type is in (6,30) |
1363                                  |         and source_organization_id is not null                   |
1364                                  |         and <> organization_id] -> Return p_demand_class         |
1365                                  | Case 2: For others if DEMAND_CLASS is null then return null      |
1366                                  |           else if p_demand_class is '-1' then call               |
1367                                  |           Get_Hierarchy_Demand_class else return DEMAND_CLASS    |
1368                                  +-----------------------------------------------------------------*/
1369                                     1,
1370                                      decode(MSC_ATP_PVT.G_INTERNAL_SALES_ORDER,'N', --10425563
1371                                      decode(d.origination_type,
1372                                        6, decode(d.source_organization_id,
1373                                           NULL, DECODE(D.DEMAND_CLASS, null, null,
1374                                              DECODE(p_demand_class, '-1',
1375                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1376                                                    null, null, p_item_id, p_org_id,
1377                                                    p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1378                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1379                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1380                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1381                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
1382                                                 D.DEMAND_CLASS)),
1383                                           -23453, DECODE(D.DEMAND_CLASS, null, null,
1384                                              DECODE(p_demand_class, '-1',
1385                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1386                                                    null, null, p_item_id, p_org_id,
1387                                                    p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1388                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1389                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1390                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1391                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
1392                                                 D.DEMAND_CLASS)),
1393                                           d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
1394                                              DECODE(p_demand_class, '-1',
1395                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1396                                                    null, null, p_item_id, p_org_id,
1397                                                    p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1398                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1399                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1400                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1401                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
1402                                                 D.DEMAND_CLASS)),
1403                                           p_demand_class),
1404                                     30, decode(d.source_organization_id,
1405                                        NULL, DECODE(D.DEMAND_CLASS, null, null,
1406                                           DECODE(p_demand_class, '-1',
1407                                              MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1408                                                 null, null, p_item_id, p_org_id,
1409                                                 p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1410                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1411                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1412                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1413                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
1414                                              D.DEMAND_CLASS)),
1415                                        -23453, DECODE(D.DEMAND_CLASS, null, null,
1416                                           DECODE(p_demand_class, '-1',
1417                                              MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1418                                                 null, null, p_item_id, p_org_id,
1419                                                 p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1420                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1421                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1422                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1423                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
1424                                              D.DEMAND_CLASS)),
1425                                        d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
1426                                           DECODE(p_demand_class, '-1',
1427                                              MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1428                                                 null, null, p_item_id, p_org_id,
1429                                                 p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1430                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1431                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1432                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1433                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
1434                                              D.DEMAND_CLASS)),
1435                                        p_demand_class),
1436                                     DECODE(D.DEMAND_CLASS, null, null,
1437                                        DECODE(p_demand_class, '-1',
1438                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1439                                              null, null, p_item_id, p_org_id,
1440                                              p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1441                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1442                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1443                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1444                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
1445                                           D.DEMAND_CLASS))),
1446                                     DECODE(D.DEMAND_CLASS, null, null, --10425563
1447                                        DECODE(p_demand_class, '-1',
1448                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1449                                              null, null, p_item_id, p_org_id,
1450                                              p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1451                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1452                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1453                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1454                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
1455                                           D.DEMAND_CLASS))),
1456                                     -- rajjain end
1457                                     2, DECODE(D.CUSTOMER_ID, NULL, p_demand_class,
1458                                                    0, p_demand_class,
1459                                        -- rajjain begin 07/19/2002
1460                                        decode(MSC_ATP_PVT.G_INTERNAL_SALES_ORDER,'N', --10425563
1461                                        decode(d.origination_type,
1462                                           6, decode(d.source_organization_id,
1463                                              NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1464                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1465                                                    p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1466                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1467                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1468                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1469                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
1470                                                    p_level_id, NULL),
1471                                              -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1472                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1473                                                    p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1474                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1475                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1476                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1477                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
1478                                                    p_level_id, NULL),
1479                                              d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1480                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1481                                                    p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1482                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1483                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1484                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1485                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
1486                                                    p_level_id, NULL),
1487                                              p_demand_class),
1488                                           30, decode(d.source_organization_id,
1489                                              NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1490                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1491                                                    p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1492                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1493                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1494                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1495                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
1496                                                    p_level_id, NULL),
1497                                              -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1498                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1499                                                    p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1500                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1501                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1502                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1503                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
1504                                                    p_level_id, NULL),
1505                                              d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1506                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1507                                                    p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
1508                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1509                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1510                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1511                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
1512                                                    p_level_id, NULL),
1513                                              p_demand_class),
1514                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(D.CUSTOMER_ID, D.SHIP_TO_SITE_ID,
1515                                              p_item_id, p_org_id, p_instance_id,
1516                                              TRUNC(DECODE(RECORD_SOURCE,
1517                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1518                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1519                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1520                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),p_level_id, NULL)),
1521                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, --10425563
1522                                              p_item_id, p_org_id, p_instance_id,
1523                                              TRUNC(DECODE(RECORD_SOURCE,
1524                                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1525                                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1526                                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1527                                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),p_level_id, NULL))
1528                                          )),
1529                                        -- rajjain end 07/19/2002
1530                                        p_demand_class,
1531                                        p_level_id),0)) SD_QTY --4365873
1532                    FROM    MSC_DEMANDS D
1533                    WHERE        D.PLAN_ID = p_plan_id
1534                    AND                D.SR_INSTANCE_ID = p_instance_id
1535                    AND                D.INVENTORY_ITEM_ID = p_item_id
1536                    AND         D.ORGANIZATION_ID = p_org_id
1537                    AND         D.USING_REQUIREMENT_QUANTITY <> 0 --4501434
1538                    AND         D.ORIGINATION_TYPE NOT IN (5,7,8,9,11,15,22,28,29,31,52,70) -- For summary enhancement
1539                    -- Bug1990155, 1995835 exclude the expired lots demand datreya 9/18/2001
1540                    -- Bug 1530311, need to exclude forecast, ngoel 12/05/2000
1541                    --AND                C.CALENDAR_CODE = p_cal_code
1542                    --AND                C.EXCEPTION_SET_ID = p_cal_exc_set_id
1543                    --AND         C.SR_INSTANCE_ID = D.SR_INSTANCE_ID
1544                    -- since we store repetitive schedule demand in different ways for
1545                    -- ods (total quantity on start date) and pds  (daily quantity from
1546                    -- start date to end date), we need to make sure we only select work day
1547                    -- for pds's repetitive schedule demand.
1548                    -- 2859130 repetitive schedule not supported for constrained plan
1549                    --AND         C.CALENDAR_DATE BETWEEN TRUNC(D.USING_ASSEMBLY_DEMAND_DATE) AND
1550                    --            TRUNC(NVL(D.ASSEMBLY_DEMAND_COMP_DATE,
1551                    --                      D.USING_ASSEMBLY_DEMAND_DATE))
1552                    --AND         (( D.ORIGINATION_TYPE = 4
1553                    --        AND C.SEQ_NUM IS NOT NULL) OR
1554                    --        ( D.ORIGINATION_TYPE  <> 4))
1555                    -- AND         C.PRIOR_DATE < NVL(p_itf,
1556                    --                 C.PRIOR_DATE + 1)
1557                    --bug3693892 added trunc
1558                    AND         TRUNC(DECODE(RECORD_SOURCE,
1559                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1560                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1561                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1562                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))) <
1563                                TRUNC(NVL(p_itf,DECODE(RECORD_SOURCE,
1564                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1565                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1566                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1567                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))) + 1))
1568                    UNION ALL
1569                    SELECT  -- C.NEXT_DATE SD_DATE, -- 2859130
1570                            -- TRUNC(NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE)) SD_DATE,
1571                            greatest(TRUNC(NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE)),p_sys_next_date) SD_DATE,--3099066
1572                            NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY) UNALLOC_SD_QTY,
1573                            NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY)*
1574                               DECODE(DECODE(G_HIERARCHY_PROFILE,
1575                                      --2424357: Convert the demand calls in case of others for
1576                                      --- demand class allocated ATP
1577                                      1, DECODE(S.DEMAND_CLASS, null, null,
1578                                         DECODE(p_demand_class, '-1',
1579                                            MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1580                                                          null,
1581                                                          null,
1582                                                          S.inventory_item_id,
1583                                                          p_org_id,
1584                                                          p_instance_id,
1585                                                          trunc(nvl(S.firm_date, S.new_schedule_date)),
1586                                                          p_level_id,
1587                                                          S.DEMAND_CLASS),S.DEMAND_CLASS)),
1588                                      2, DECODE(S.CUSTOMER_ID, NULL, TO_CHAR(NULL),
1589                                                        0, TO_CHAR(NULL),
1590                                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1591                                                          S.CUSTOMER_ID,
1592                                                          S.SHIP_TO_SITE_ID,
1593                                                          S.inventory_item_id,
1594                                                          p_org_id,
1595                                                          p_instance_id,
1596                                                          trunc(nvl(S.firm_date, S.new_schedule_date)),
1597                                                          p_level_id,
1598                                                          NULL))),
1599                                  p_demand_class, 1,
1600                                  NULL, nvl(MIHM.allocation_percent/100,1), --4365873
1601                                  /*NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
1602                                            p_instance_id,
1603                                            s.inventory_item_id,
1604                                            p_org_id,
1605                                            null,
1606                                            null,
1607                                            p_demand_class,
1608                                            trunc(nvl(s.firm_date, s.new_schedule_date))), 1),*/
1609                                  DECODE(MIHM.allocation_percent/100, --4365873
1610                                  /*MSC_AATP_FUNC.Get_DC_Alloc_Percent(
1611                                            p_instance_id,
1612                                            s.inventory_item_id,
1613                                            p_org_id,
1614                                            null,
1615                                            null,
1616                                            p_demand_class,
1617                                            trunc(nvl(s.firm_date, s.new_schedule_date))),*/
1618                                    NULL, 1,
1619                                  0)) SD_QTY
1620                    FROM    -- MSC_CALENDAR_DATES C,
1621                            MSC_SUPPLIES S,MSC_ITEM_HIERARCHY_MV MIHM
1622                    WHERE   S.PLAN_ID = p_plan_id
1623                    AND     S.SR_INSTANCE_ID = p_instance_id
1624                    AND     S.INVENTORY_ITEM_ID = p_item_id
1625                    AND     S.ORGANIZATION_ID = p_org_id
1626                            -- Exclude Cancelled Supplies 2460645
1627                    AND     NVL(S.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
1628                    AND     NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY) <> 0 -- 1243985
1629                    --AND     C.CALENDAR_CODE = p_cal_code
1630                    --AND     C.EXCEPTION_SET_ID = p_cal_exc_set_id
1631                    --AND     C.SR_INSTANCE_ID = S.SR_INSTANCE_ID
1632                    --AND     C.CALENDAR_DATE BETWEEN TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE))
1633                    --        AND TRUNC(NVL(S.LAST_UNIT_COMPLETION_DATE,
1634                    --            NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)))
1635                    --AND     DECODE(S.LAST_UNIT_COMPLETION_DATE,
1636                    --                   NULL, C.NEXT_SEQ_NUM, C.SEQ_NUM) IS NOT NULL
1637                    -- 2859130
1638                    -- AND     C.NEXT_DATE >= DECODE(S.ORDER_TYPE, 27, TRUNC(SYSDATE),
1639                    --                                             28, TRUNC(SYSDATE),
1640                    --                                             C.NEXT_DATE)
1641                    -- AND     C.NEXT_DATE < NVL(p_itf,
1642                    --                           C.NEXT_DATE + 1)
1643                    --AND     C.CALENDAR_DATE >= DECODE(S.ORDER_TYPE, 27, TRUNC(SYSDATE),
1644                    --                                            28, TRUNC(SYSDATE),
1645                    --                                            C.CALENDAR_DATE)
1646                    AND     trunc(NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE)) >= trunc(DECODE(S.ORDER_TYPE, 27, SYSDATE,
1647                                                                28, SYSDATE,
1648                                                                trunc(NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE)))) --4135752
1649                    AND     trunc(NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE)) < NVL(p_itf,
1650                                              trunc(NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE)) + 1)
1651 		--4365873
1652                 AND    S.INVENTORY_ITEM_ID = MIHM.INVENTORY_ITEM_ID(+)
1653                 AND    S.SR_INSTANCE_ID = MIHM.SR_INSTANCE_ID (+)
1654                 AND    S.ORGANIZATION_ID = MIHM.ORGANIZATION_ID (+)
1655                 AND    decode(MIHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
1656                 AND    trunc(NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE)) >= MIHM.effective_date (+)
1657                 AND    trunc(NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE)) <= MIHM.disable_date (+)
1658                 AND    MIHM.demand_class (+) = p_demand_class
1659                    )
1660              GROUP BY SD_DATE
1661              ORDER BY SD_DATE;--4698199
1662 
1663 END item_alloc_avail_opt_unalloc;
1664 --avjain All netting sqls have been changed to incorporate Plan by Request Date Enhancements
1665 PROCEDURE item_alloc_avail_unopt_unalloc (
1666    p_item_id            IN NUMBER,
1667    p_org_id             IN NUMBER,
1668    p_instance_id        IN NUMBER,
1669    p_plan_id            IN NUMBER,
1670    p_demand_class       IN VARCHAR2,
1671    p_level_id           IN NUMBER,
1672    p_itf                IN DATE,
1673    p_cal_code           IN VARCHAR2,
1674    p_cal_exc_set_id     IN NUMBER,
1675    p_sys_next_date	IN DATE,			--bug3099066
1676    x_atp_dates          OUT NoCopy MRP_ATP_PUB.date_arr,
1677    x_atp_qtys           OUT NoCopy MRP_ATP_PUB.number_arr,
1678    x_atp_unalloc_qtys   OUT NoCopy MRP_ATP_PUB.number_arr
1679 ) IS
1680 BEGIN
1681         IF PG_DEBUG in ('Y', 'C') THEN
1682                 msc_sch_wb.atp_debug('item_alloc_avail_unopt_unalloc: Get unallocated qtys as well. UNconstrained plan: ' || p_plan_id);
1683         END IF;
1684 
1685              SELECT        SD_DATE,
1686                            SUM(UNALLOC_SD_QTY),
1687                            SUM(SD_QTY)
1688              BULK COLLECT INTO
1689                            x_atp_dates,
1690                            x_atp_unalloc_qtys,
1691                            x_atp_qtys
1692              FROM (
1693                    SELECT  --C.CALENDAR_DATE SD_DATE, -- 2859130 change to calendar date
1694                    	   GREATEST(C.CALENDAR_DATE,p_sys_next_date) SD_DATE,--3099066
1695                            -1* DECODE(D.ORIGINATION_TYPE,
1696                                   4, D.DAILY_DEMAND_RATE,
1697                                   --D.USING_REQUIREMENT_QUANTITY) UNALLOC_SD_QTY,
1698                                   (D.USING_REQUIREMENT_QUANTITY - NVL(d.reserved_quantity, 0))) UNALLOC_SD_QTY, --5027568
1699                            -1* DECODE(D.ORIGINATION_TYPE,
1700                                   4, D.DAILY_DEMAND_RATE,
1701                                   --D.USING_REQUIREMENT_QUANTITY)*
1702                                   (D.USING_REQUIREMENT_QUANTITY - NVL(d.reserved_quantity, 0)))* --5027568
1703                            DECODE(DECODE(G_HIERARCHY_PROFILE,
1704                            /*------------------------------------------------------------------------+
1705                            | rajjain begin 07/19/2002                                                |
1706                            |                                                                         |
1707                            | Case 1: For internal sales orders [origination type is in (6,30) and    |
1708                            |            source_organization_id is not null and <> organization_id]   |
1709                            |                  Return NULL                                            |
1710                            | Case 2: For others if DEMAND_CLASS is null then return null             |
1711                            |          else if p_demand_class is '-1' then call                       |
1712                            |            Get_Hierarchy_Demand_class else return DEMAND_CLASS          |
1713                            +------------------------------------------------------------------------*/
1714                               1,
1715                               decode(MSC_ATP_PVT.G_INTERNAL_SALES_ORDER,'N', --10425563
1716                               decode(d.origination_type,
1717                                  6, decode(d.source_organization_id,
1718                                     NULL, DECODE(D.DEMAND_CLASS, null, null,
1719                                        DECODE(p_demand_class, '-1',
1720                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1721                                              null, null, p_item_id, p_org_id,
1722                                              p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
1723                                              D.DEMAND_CLASS)),
1724                                     -23453, DECODE(D.DEMAND_CLASS, null, null,
1725                                        DECODE(p_demand_class, '-1',
1726                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1727                                              null, null, p_item_id, p_org_id,
1728                                              p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
1729                                              D.DEMAND_CLASS)),
1730                                     d.organization_id, 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)), TO_CHAR(NULL)),
1736                                  30, decode(d.source_organization_id,
1737                                     NULL, DECODE(D.DEMAND_CLASS, null, null,
1738                                        DECODE(p_demand_class, '-1',
1739                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1740                                              null, null, p_item_id, p_org_id,
1741                                              p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
1742                                              D.DEMAND_CLASS)),
1743                                     -23453, DECODE(D.DEMAND_CLASS, null, null,
1744                                        DECODE(p_demand_class, '-1',
1745                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1746                                              null, null, p_item_id, p_org_id,
1747                                              p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
1748                                              D.DEMAND_CLASS)),
1749                                     d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
1750                                        DECODE(p_demand_class, '-1',
1751                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1752                                              null, null, p_item_id, p_org_id,
1753                                              p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
1754                                              D.DEMAND_CLASS)), TO_CHAR(NULL)),
1755                                  DECODE(D.DEMAND_CLASS, null, null,
1756                                     DECODE(p_demand_class, '-1',
1757                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1758                                           null, null, p_item_id, p_org_id,
1759                                           p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
1760                                           D.DEMAND_CLASS))),
1761                                 DECODE(D.DEMAND_CLASS, null, null, --10425563
1762                                     DECODE(p_demand_class, '-1',
1763                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1764                                           null, null, p_item_id, p_org_id,
1765                                           p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
1766                                           D.DEMAND_CLASS))),
1767                               -- rajjain end
1768                               2, DECODE(D.CUSTOMER_ID, NULL, TO_CHAR(NULL),
1769                                                    0, TO_CHAR(NULL),
1770                                  -- rajjain begin 07/19/2002
1771                                  decode(MSC_ATP_PVT.G_INTERNAL_SALES_ORDER,'N', --10425563
1772                                  decode(d.origination_type,
1773                                     6, decode(d.source_organization_id,
1774                                        NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1775                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1776                                              p_org_id, p_instance_id, c.calendar_date,
1777                                              p_level_id, NULL),
1778                                        -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1779                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1780                                              p_org_id, p_instance_id, c.calendar_date,
1781                                              p_level_id, NULL),
1782                                        d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1783                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1784                                              p_org_id, p_instance_id, c.calendar_date,
1785                                              p_level_id, NULL),
1786                                        TO_CHAR(NULL)),
1787                                     30, decode(d.source_organization_id,
1788                                        NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1789                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1790                                              p_org_id, p_instance_id, c.calendar_date,
1791                                              p_level_id, NULL),
1792                                        -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1793                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1794                                              p_org_id, p_instance_id, c.calendar_date,
1795                                              p_level_id, NULL),
1796                                        d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1797                                              D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1798                                              p_org_id, p_instance_id, c.calendar_date,
1799                                              p_level_id, NULL),
1800                                        TO_CHAR(NULL)),
1801                                     MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, --10425563
1802                                        p_item_id, p_org_id, p_instance_id,
1803                                        c.calendar_date,p_level_id, NULL)),
1804                                     MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(D.CUSTOMER_ID, D.SHIP_TO_SITE_ID,
1805                                        p_item_id, p_org_id, p_instance_id,
1806                                        c.calendar_date,p_level_id, NULL)))),
1807                                  -- rajjain end 07/19/2002
1808                            p_demand_class, 1,
1809                              Decode(D.Demand_Class, NULL, --4365873
1810                               MSC_AATP_FUNC.Get_Item_Demand_Alloc_Percent(p_plan_id,
1811                                  D.DEMAND_ID,
1812                                  c.calendar_date,
1813                                  D.USING_ASSEMBLY_ITEM_ID,
1814                                  DECODE(D.SOURCE_ORGANIZATION_ID,
1815                                     -23453, null,
1816                                     D.SOURCE_ORGANIZATION_ID), -- 1665483
1817                                  p_item_id,
1818                                  p_org_id, -- 1665483
1819                                  p_instance_id,
1820                                  DECODE (D.ORIGINATION_TYPE, -100, 30,D.ORIGINATION_TYPE) , --5027568
1821                                  DECODE(G_HIERARCHY_PROFILE,
1822                                  /*-----------------------------------------------------------------+
1823                                  | rajjain begin 07/19/2002                                         |
1824                                  |                                                                  |
1825                                  | Case 1: For internal sales orders [origination type is in (6,30) |
1826                                  |         and source_organization_id is not null                   |
1827                                  |         and <> organization_id] -> Return p_demand_class         |
1828                                  | Case 2: For others if DEMAND_CLASS is null then return null      |
1829                                  |           else if p_demand_class is '-1' then call               |
1830                                  |           Get_Hierarchy_Demand_class else return DEMAND_CLASS    |
1831                                  +-----------------------------------------------------------------*/
1832                                     1,
1833                                     decode(MSC_ATP_PVT.G_INTERNAL_SALES_ORDER,'N', --10425563
1834                                     decode(d.origination_type,
1835                                        6, decode(d.source_organization_id,
1836                                           NULL, DECODE(D.DEMAND_CLASS, null, null,
1837                                              DECODE(p_demand_class, '-1',
1838                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1839                                                    null, null, p_item_id, p_org_id,
1840                                                    p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
1841                                                 D.DEMAND_CLASS)),
1842                                           -23453, DECODE(D.DEMAND_CLASS, null, null,
1843                                              DECODE(p_demand_class, '-1',
1844                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1845                                                    null, null, p_item_id, p_org_id,
1846                                                    p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
1847                                                 D.DEMAND_CLASS)),
1848                                           d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
1849                                              DECODE(p_demand_class, '-1',
1850                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1851                                                    null, null, p_item_id, p_org_id,
1852                                                    p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
1853                                                 D.DEMAND_CLASS)),
1854                                           p_demand_class),
1855                                     30, decode(d.source_organization_id,
1856                                        NULL, DECODE(D.DEMAND_CLASS, null, null,
1857                                           DECODE(p_demand_class, '-1',
1858                                              MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1859                                                 null, null, p_item_id, p_org_id,
1860                                                 p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
1861                                              D.DEMAND_CLASS)),
1862                                        -23453, DECODE(D.DEMAND_CLASS, null, null,
1863                                           DECODE(p_demand_class, '-1',
1864                                              MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1865                                                 null, null, p_item_id, p_org_id,
1866                                                 p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
1867                                              D.DEMAND_CLASS)),
1868                                        d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
1869                                           DECODE(p_demand_class, '-1',
1870                                              MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1871                                                 null, null, p_item_id, p_org_id,
1872                                                 p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
1873                                              D.DEMAND_CLASS)),
1874                                        p_demand_class),
1875                                     DECODE(D.DEMAND_CLASS, null, null,
1876                                        DECODE(p_demand_class, '-1',
1877                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1878                                              null, null, p_item_id, p_org_id,
1879                                              p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
1880                                           D.DEMAND_CLASS))),
1881                                     DECODE(D.DEMAND_CLASS, null, null, --10425563
1882                                        DECODE(p_demand_class, '-1',
1883                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1884                                              null, null, p_item_id, p_org_id,
1885                                              p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
1886                                           D.DEMAND_CLASS))),
1887                                     -- rajjain end
1888                                     2, DECODE(D.CUSTOMER_ID, NULL, p_demand_class,
1889                                                    0, p_demand_class,
1890                                        -- rajjain begin 07/19/2002
1891                                        decode(MSC_ATP_PVT.G_INTERNAL_SALES_ORDER,'N', --10425563
1892                                        decode(d.origination_type,
1893                                           6, decode(d.source_organization_id,
1894                                              NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1895                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1896                                                    p_org_id, p_instance_id, c.calendar_date,
1897                                                    p_level_id, NULL),
1898                                              -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1899                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1900                                                    p_org_id, p_instance_id, c.calendar_date,
1901                                                    p_level_id, NULL),
1902                                              d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1903                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1904                                                    p_org_id, p_instance_id, c.calendar_date,
1905                                                    p_level_id, NULL),
1906                                              p_demand_class),
1907                                           30, decode(d.source_organization_id,
1908                                              NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1909                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1910                                                    p_org_id, p_instance_id, c.calendar_date,
1911                                                    p_level_id, NULL),
1912                                              -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1913                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1914                                                    p_org_id, p_instance_id, c.calendar_date,
1915                                                    p_level_id, NULL),
1916                                              d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1917                                                    D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
1918                                                    p_org_id, p_instance_id, c.calendar_date,
1919                                                    p_level_id, NULL),
1920                                              p_demand_class),
1921                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(D.CUSTOMER_ID, D.SHIP_TO_SITE_ID,
1922                                              p_item_id, p_org_id, p_instance_id,
1923                                              c.calendar_date,p_level_id, NULL)),
1924                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, --10425563
1925                                              p_item_id, p_org_id, p_instance_id,
1926                                              c.calendar_date,p_level_id, NULL))
1927                                              )),
1928                                        -- rajjain end 07/19/2002
1929                                        p_demand_class,
1930                                        p_level_id),0)) SD_QTY --4365873
1931                    FROM    MSC_CALENDAR_DATES C,
1932                            MSC_DEMANDS D
1933                    WHERE        D.PLAN_ID = p_plan_id
1934                    AND                D.SR_INSTANCE_ID = p_instance_id
1935                    AND                D.INVENTORY_ITEM_ID = p_item_id
1936                    AND         D.ORGANIZATION_ID = p_org_id
1937                    AND         D.USING_REQUIREMENT_QUANTITY <> 0 --4501434
1938                    AND         D.ORIGINATION_TYPE NOT IN (5,7,8,9,11,15,22,28,29,31,52,70) -- For summary enhancement
1939                    -- Bug1990155, 1995835 exclude the expired lots demand datreya 9/18/2001
1940                    -- Bug 1530311, need to exclude forecast, ngoel 12/05/2000
1941                    AND                C.CALENDAR_CODE = p_cal_code
1942                    AND                C.EXCEPTION_SET_ID = p_cal_exc_set_id
1943                    AND         C.SR_INSTANCE_ID = D.SR_INSTANCE_ID
1944                    -- since we store repetitive schedule demand in different ways for
1945                    -- ods (total quantity on start date) and pds  (daily quantity from
1946                    -- start date to end date), we need to make sure we only select work day
1947                    -- for pds's repetitive schedule demand.
1948                    -- Bug 3550296 and 3574164. IMPLEMENT_DATE AND DMD_SATISFIED_DATE are changed to
1949                    -- IMPLEMENT_SHIP_DATE and PLANNED_SHIP_DATE resp.
1950                    AND         C.CALENDAR_DATE BETWEEN TRUNC(DECODE(RECORD_SOURCE,
1951                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1952                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1953                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1954                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))) AND
1955                                TRUNC(NVL(D.ASSEMBLY_DEMAND_COMP_DATE,
1956                                          DECODE(RECORD_SOURCE,
1957                                  2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1958                                     DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1959                                            2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1960                                               NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))))
1961                    AND         (( D.ORIGINATION_TYPE = 4
1962                            AND C.SEQ_NUM IS NOT NULL) OR
1963                            ( D.ORIGINATION_TYPE  <> 4))
1964                    AND         C.CALENDAR_DATE < NVL(p_itf,
1965                                    C.CALENDAR_DATE + 1)
1966                    UNION ALL
1967                    SELECT  --C.CALENDAR_DATE SD_DATE, --2859130
1968                            greatest(CS.CALENDAR_DATE,p_sys_next_date) SD_DATE,--3099066
1969                            NVL(CS.FIRM_QUANTITY,CS.NEW_ORDER_QUANTITY) UNALLOC_SD_QTY,
1970                            NVL(CS.FIRM_QUANTITY,CS.NEW_ORDER_QUANTITY)*
1971                               DECODE(DECODE(G_HIERARCHY_PROFILE,
1972                                      --2424357: Convert the demand calls in case of others for
1973                                      --- demand class allocated ATP
1974                                      1, DECODE(CS.DEMAND_CLASS, null, null,
1975                                         DECODE(p_demand_class, '-1',
1976                                            MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1977                                                          null,
1978                                                          null,
1979                                                          cs.inventory_item_id,
1980                                                          p_org_id,
1981                                                          p_instance_id,
1982                                                          cs.calendar_date,
1983                                                          p_level_id,
1984                                                          CS.DEMAND_CLASS),CS.DEMAND_CLASS)),
1985                                      2, DECODE(CS.CUSTOMER_ID, NULL, TO_CHAR(NULL),
1986                                                        0, TO_CHAR(NULL),
1987                                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1988                                                          CS.CUSTOMER_ID,
1989                                                          CS.SHIP_TO_SITE_ID,
1990                                                          cs.inventory_item_id,
1991                                                          p_org_id,
1992                                                          p_instance_id,
1993                                                          cs.calendar_date,
1994                                                          p_level_id,
1995                                                          NULL))),
1996                                  p_demand_class, 1,
1997                                  NULL, nvl(MIHM.allocation_percent/100,1), --4365873
1998                                  /*NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
1999                                            p_instance_id,
2000                                            s.inventory_item_id,
2001                                            p_org_id,
2002                                            null,
2003                                            null,
2004                                            p_demand_class,
2005                                            c.calendar_date), 1),*/
2006                                  DECODE(
2007                                  MIHM.allocation_percent/100, --4365873
2008                                  /*MSC_AATP_FUNC.Get_DC_Alloc_Percent(
2009                                            p_instance_id,
2010                                            s.inventory_item_id,
2011                                            p_org_id,
2012                                            null,
2013                                            null,
2014                                            p_demand_class,
2015                                            c.calendar_date),*/
2016                                    NULL, 1,
2017                                  0)) SD_QTY
2018                 FROM
2019                 (
2020                 select
2021                 	C.NEXT_DATE,
2022                 	C.CALENDAR_DATE,
2023 			S.FIRM_QUANTITY,
2024 			S.NEW_ORDER_QUANTITY,
2025 			S.DEMAND_CLASS,
2026 			s.inventory_item_id,
2027 			S.CUSTOMER_ID,
2028 			S.SHIP_TO_SITE_ID,
2029 			S.SR_INSTANCE_ID,
2030 			S.ORGANIZATION_ID
2031                    FROM    MSC_CALENDAR_DATES C,
2032                            MSC_SUPPLIES S
2033                    WHERE   S.PLAN_ID = p_plan_id
2034                    AND     S.SR_INSTANCE_ID = p_instance_id
2035                    AND     S.INVENTORY_ITEM_ID = p_item_id
2036                    AND     S.ORGANIZATION_ID = p_org_id
2037                            -- Exclude Cancelled Supplies 2460645
2038                    AND     NVL(S.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
2039                    AND     NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY) <> 0 -- 1243985
2040                    AND     C.CALENDAR_CODE = p_cal_code
2041                    AND     C.EXCEPTION_SET_ID = p_cal_exc_set_id
2042                    AND     C.SR_INSTANCE_ID = S.SR_INSTANCE_ID
2043                    AND     C.CALENDAR_DATE BETWEEN TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE))
2044                            AND TRUNC(NVL(S.LAST_UNIT_COMPLETION_DATE,
2045                                NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)))
2046                    AND     DECODE(S.LAST_UNIT_COMPLETION_DATE,
2047                                       NULL, C.NEXT_SEQ_NUM, C.SEQ_NUM) IS NOT NULL
2048                    AND     C.CALENDAR_DATE >= DECODE(S.ORDER_TYPE, 27, TRUNC(SYSDATE),
2049                                                                28, TRUNC(SYSDATE),
2050                                                                C.CALENDAR_DATE)
2051                    AND     C.CALENDAR_DATE < NVL(p_itf,
2052                                              C.CALENDAR_DATE + 1))CS,
2053 		MSC_ITEM_HIERARCHY_MV MIHM
2054 		WHERE
2055 		--4365873
2056                        CS.INVENTORY_ITEM_ID = MIHM.INVENTORY_ITEM_ID(+)
2057                 AND    CS.SR_INSTANCE_ID = MIHM.SR_INSTANCE_ID (+)
2058                 AND    CS.ORGANIZATION_ID = MIHM.ORGANIZATION_ID (+)
2059                 AND    decode(MIHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
2060                 AND    CS.NEXT_DATE >= MIHM.effective_date (+)
2061                 AND    CS.NEXT_DATE <= MIHM.disable_date (+)
2062                 AND    MIHM.demand_class (+) = p_demand_class
2063                    )
2064              GROUP BY SD_DATE
2065              ORDER BY SD_DATE;--4698199
2066 END item_alloc_avail_unopt_unalloc;
2067 --avjain All netting sqls have been changed to incorporate Plan by Request Date Enhancements
2068 PROCEDURE item_alloc_avail_opt_dtls (
2069    p_item_id            IN NUMBER,
2070    p_org_id             IN NUMBER,
2071    p_instance_id        IN NUMBER,
2072    p_plan_id            IN NUMBER,
2073    p_demand_class       IN VARCHAR2,
2074    p_level_id           IN NUMBER,
2075    p_itf                IN DATE,
2076    p_cal_code           IN VARCHAR2,
2077    p_cal_exc_set_id     IN NUMBER,
2078    p_sr_item_id         IN NUMBER,
2079    p_level              IN NUMBER,
2080    p_identifier         IN NUMBER,
2081    p_scenario_id        IN NUMBER,
2082    p_uom_code           IN VARCHAR2,
2083    p_sys_next_date	IN DATE				--bug3099066
2084 ) IS
2085    l_null_num   NUMBER;
2086    l_null_char  VARCHAR2(1);
2087    l_sysdate    DATE := trunc(sysdate); --4135752
2088 BEGIN
2089         IF PG_DEBUG in ('Y', 'C') THEN
2090                 msc_sch_wb.atp_debug('item_alloc_avail_opt_dtls: constrained plan: ' || p_plan_id);
2091         END IF;
2092 
2093         INSERT INTO msc_atp_sd_details_temp (
2094                 ATP_Level,
2095                 Order_line_id,
2096                 Scenario_Id,
2097                 Inventory_Item_Id,
2098                 Request_Item_Id,
2099                 Organization_Id,
2100                 Department_Id,
2101                 Resource_Id,
2102                 Supplier_Id,
2103                 Supplier_Site_Id,
2104                 From_Organization_Id,
2105                 From_Location_Id,
2106                 To_Organization_Id,
2107                 To_Location_Id,
2108                 Ship_Method,
2109                 UOM_code,
2110                 Supply_Demand_Type,
2111                 Supply_Demand_Source_Type,
2112                 Supply_Demand_Source_Type_Name,
2113                 Identifier1,
2114                 Identifier2,
2115                 Identifier3,
2116                 Identifier4,
2117                 Allocated_Quantity, -- fixed as part of time_phased_atp
2118                 Supply_Demand_Quantity,
2119                 Supply_Demand_Date,
2120                 Disposition_Type,
2121                 Disposition_Name,
2122                 Pegging_Id,
2123                 End_Pegging_Id,
2124                 creation_date,
2125                 created_by,
2126                 last_update_date,
2127                 last_updated_by,
2128                 last_update_login,
2129                 Unallocated_Quantity,
2130                 ORIG_CUSTOMER_SITE_NAME,--bug3263368
2131                 ORIG_CUSTOMER_NAME, --bug3263368
2132                 ORIG_DEMAND_CLASS, --bug3263368
2133                 ORIG_REQUEST_DATE --bug3263368
2134                 )
2135            (
2136             SELECT      p_level col1,
2137                         p_identifier col2,
2138                         p_scenario_id col3,
2139                         p_sr_item_id col4 ,
2140                         p_sr_item_id col5,
2141                         p_org_id col6,
2142                         l_null_num col7,
2143                         l_null_num col8,
2144                         l_null_num col9,
2145                         l_null_num col10,
2146                         l_null_num col11,
2147                         l_null_num col12,
2148                         l_null_num col13,
2149                         l_null_num col14,
2150                         l_null_char col15,
2151                         p_uom_code col16,
2152                         1 col17, -- demand
2153                         --D.ORIGINATION_TYPE col18,
2154                         DECODE( D.ORIGINATION_TYPE, -100, 30, D.ORIGINATION_TYPE) col18,  --5027568
2155                         l_null_char col19,
2156                         D.SR_INSTANCE_ID col20,
2157                         l_null_num col21,
2158                         D.DEMAND_ID col22,
2159                         l_null_num col23,
2160                         -- -1* D.USING_REQUIREMENT_QUANTITY * -- 2859130 remove decode originition_type 4
2161                         -1*(D.USING_REQUIREMENT_QUANTITY - NVL(d.reserved_quantity, 0))* --5027568
2162 			/*New*/
2163                         DECODE(p_scenario_id, -1, 1,
2164                         --2424357
2165                         DECODE(DECODE(G_HIERARCHY_PROFILE,
2166                         /*------------------------------------------------------------------------+
2167                         | rajjain begin 07/19/2002                                                |
2168                         |                                                                         |
2169                         | Case 1: For internal sales orders [origination type is in (6,30) and    |
2170                         |            source_organization_id is not null and <> organization_id]   |
2171                         |                  Return NULL                                            |
2172                         | Case 2: For others if DEMAND_CLASS is null then return null             |
2173                         |          else if p_demand_class is '-1' then call                       |
2174                         |            Get_Hierarchy_Demand_class else return DEMAND_CLASS          |
2175                         +------------------------------------------------------------------------*/
2176                            1,
2177                            decode(MSC_ATP_PVT.G_INTERNAL_SALES_ORDER,'N', --10425563
2178                            decode(d.origination_type,
2179                               6, decode(d.source_organization_id,
2180                                  NULL, DECODE(D.DEMAND_CLASS, null, null,
2181                                     DECODE(p_demand_class, '-1',
2182                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2183                                           null, null, p_item_id, p_org_id,
2184                                           p_instance_id, trunc(DECODE(RECORD_SOURCE,
2185                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2186                                             -- Bug 3550296 and 3574164. IMPLEMENT_DATE AND DMD_SATISFIED_DATE are changed to
2187                                             -- IMPLEMENT_SHIP_DATE and PLANNED_SHIP_DATE resp.
2188                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2189                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2190                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
2191                                           D.DEMAND_CLASS)),
2192                                  -23453, DECODE(D.DEMAND_CLASS, null, null,
2193                                     DECODE(p_demand_class, '-1',
2194                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2195                                           null, null, p_item_id, p_org_id,
2196                                           p_instance_id, trunc(DECODE(RECORD_SOURCE,
2197                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2198                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2199                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2200                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
2201                                           D.DEMAND_CLASS)),
2202                                  d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
2203                                     DECODE(p_demand_class, '-1',
2204                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2205                                           null, null, p_item_id, p_org_id,
2206                                           p_instance_id, trunc(DECODE(RECORD_SOURCE,
2207                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2208                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2209                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2210                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
2211                                           D.DEMAND_CLASS)), NULL),
2212                               30, decode(d.source_organization_id,
2213                                  NULL, DECODE(D.DEMAND_CLASS, null, null,
2214                                     DECODE(p_demand_class, '-1',
2215                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2216                                           null, null, p_item_id, p_org_id,
2217                                           p_instance_id, trunc(DECODE(RECORD_SOURCE,
2218                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2219                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2220                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2221                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
2222                                           D.DEMAND_CLASS)),
2223                                  -23453, DECODE(D.DEMAND_CLASS, null, null,
2224                                     DECODE(p_demand_class, '-1',
2225                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2226                                           null, null, p_item_id, p_org_id,
2227                                           p_instance_id, trunc(DECODE(RECORD_SOURCE,
2228                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2229                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2230                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2231                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
2232                                           D.DEMAND_CLASS)),
2233                                  d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
2234                                     DECODE(p_demand_class, '-1',
2235                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2236                                           null, null, p_item_id, p_org_id,
2237                                           p_instance_id, trunc(DECODE(RECORD_SOURCE,
2238                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2239                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2240                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2241                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
2242                                           D.DEMAND_CLASS)), NULL),
2243                               DECODE(D.DEMAND_CLASS, null, null,
2244                                  DECODE(p_demand_class, '-1',
2245                                     MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2246                                        null, null, p_item_id, p_org_id,
2247                                        p_instance_id, trunc(DECODE(RECORD_SOURCE,
2248                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2249                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2250                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2251                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
2252                                        D.DEMAND_CLASS))),
2253                              DECODE(D.DEMAND_CLASS, null, null, --10425563
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, NULL,
2265                                                 0, NULL,
2266                               -- rajjain begin 07/19/2002
2267                               decode(MSC_ATP_PVT.G_INTERNAL_SALES_ORDER,'N', --10425563
2268                               decode(d.origination_type,
2269                                  6, decode(d.source_organization_id,
2270                                     NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2271                                           D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
2272                                           p_org_id, p_instance_id, trunc(DECODE(RECORD_SOURCE,
2273                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2274                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2275                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2276                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
2277                                           p_level_id, NULL),
2278                                     -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2279                                           D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
2280                                           p_org_id, p_instance_id, trunc(DECODE(RECORD_SOURCE,
2281                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2282                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2283                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2284                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
2285                                           p_level_id, NULL),
2286                                     d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2287                                           D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
2288                                           p_org_id, p_instance_id, trunc(DECODE(RECORD_SOURCE,
2289                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2290                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2291                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2292                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
2293                                           p_level_id, NULL),
2294                                     NULL),
2295                                  30, decode(d.source_organization_id,
2296                                     NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2297                                           D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
2298                                           p_org_id, p_instance_id, trunc(DECODE(RECORD_SOURCE,
2299                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2300                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2301                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2302                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
2303                                           p_level_id, NULL),
2304                                     -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2305                                           D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
2306                                           p_org_id, p_instance_id, trunc(DECODE(RECORD_SOURCE,
2307                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2308                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2309                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2310                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
2311                                           p_level_id, NULL),
2312                                     d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2313                                           D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
2314                                           p_org_id, p_instance_id, trunc(DECODE(RECORD_SOURCE,
2315                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2316                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2317                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2318                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
2319                                           p_level_id, NULL),
2320                                     NULL),
2321                                  MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(D.CUSTOMER_ID, D.SHIP_TO_SITE_ID,
2322                                     p_item_id, p_org_id, p_instance_id,
2323                                     trunc(DECODE(RECORD_SOURCE,
2324                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2325                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2326                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2327                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),p_level_id, NULL)),
2328                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, --10425563
2329                                     p_item_id, p_org_id, p_instance_id,
2330                                     trunc(DECODE(RECORD_SOURCE,
2331                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2332                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2333                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2334                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),p_level_id, NULL))
2335                                  )),
2336                               -- rajjain end 07/19/2002
2337                         p_demand_class, 1,
2338                           Decode(D.Demand_Class, NULL, --4365873
2339                            MSC_AATP_FUNC.Get_Item_Demand_Alloc_Percent(p_plan_id,
2340                               D.DEMAND_ID,
2341                               trunc(DECODE(RECORD_SOURCE,
2342                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2343                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2344                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2345                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
2346                               D.USING_ASSEMBLY_ITEM_ID,
2347                               DECODE(D.SOURCE_ORGANIZATION_ID,
2348                                  -23453, null,
2349                                  D.SOURCE_ORGANIZATION_ID),
2350                               p_item_id,
2351                               p_org_id, -- 1665483
2352                               p_instance_id,
2353                               decode (d.origination_type, -100, 30,d.origination_type),
2354                               DECODE(G_HIERARCHY_PROFILE,
2355                               /*-----------------------------------------------------------------+
2356                               | rajjain begin 07/19/2002                                         |
2357                               |                                                                  |
2358                               | Case 1: For internal sales orders [origination type is in (6,30) |
2359                               |         and source_organization_id is not null                   |
2360                               |         and <> organization_id] -> Return p_demand_class         |
2361                               | Case 2: For others if DEMAND_CLASS is null then return null      |
2362                               |           else if p_demand_class is '-1' then call               |
2363                               |           Get_Hierarchy_Demand_class else return DEMAND_CLASS    |
2364                               +-----------------------------------------------------------------*/
2365                                  1,
2366                                  decode(MSC_ATP_PVT.G_INTERNAL_SALES_ORDER,'N', --10425563
2367                                  decode(d.origination_type,
2368                                     6, decode(d.source_organization_id,
2369                                        NULL, DECODE(D.DEMAND_CLASS, null, null,
2370                                           DECODE(p_demand_class, '-1',
2371                                              MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2372                                                 null, null, p_item_id, p_org_id,
2373                                                 p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
2374                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2375                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2376                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2377                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
2378                                              D.DEMAND_CLASS)),
2379                                        -23453, DECODE(D.DEMAND_CLASS, null, null,
2380                                           DECODE(p_demand_class, '-1',
2381                                              MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2382                                                 null, null, p_item_id, p_org_id,
2383                                                 p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
2384                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2385                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2386                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2387                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
2388                                              D.DEMAND_CLASS)),
2389                                        d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
2390                                           DECODE(p_demand_class, '-1',
2391                                              MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2392                                                 null, null, p_item_id, p_org_id,
2393                                                 p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
2394                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2395                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2396                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2397                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
2398                                              D.DEMAND_CLASS)),
2399                                        p_demand_class),
2400                                  30, decode(d.source_organization_id,
2401                                     NULL, DECODE(D.DEMAND_CLASS, null, null,
2402                                        DECODE(p_demand_class, '-1',
2403                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2404                                              null, null, p_item_id, p_org_id,
2405                                              p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
2406                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2407                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2408                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2409                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
2410                                           D.DEMAND_CLASS)),
2411                                     -23453, DECODE(D.DEMAND_CLASS, null, null,
2412                                        DECODE(p_demand_class, '-1',
2413                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2414                                              null, null, p_item_id, p_org_id,
2415                                              p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
2416                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2417                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2418                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2419                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
2420                                           D.DEMAND_CLASS)),
2421                                     d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
2422                                        DECODE(p_demand_class, '-1',
2423                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2424                                              null, null, p_item_id, p_org_id,
2425                                              p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
2426                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2427                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2428                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2429                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
2430                                           D.DEMAND_CLASS)),
2431                                     p_demand_class),
2432                                  DECODE(D.DEMAND_CLASS, null, null,
2433                                     DECODE(p_demand_class, '-1',
2434                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2435                                           null, null, p_item_id, p_org_id,
2436                                           p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
2437                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2438                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2439                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2440                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
2441                                        D.DEMAND_CLASS))),
2442                                  DECODE(D.DEMAND_CLASS, null, null, --10425563
2443                                     DECODE(p_demand_class, '-1',
2444                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2445                                           null, null, p_item_id, p_org_id,
2446                                           p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
2447                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2448                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2449                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2450                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), p_level_id, D.DEMAND_CLASS),
2451                                        D.DEMAND_CLASS))),
2452                                  -- rajjain end
2453                                  2, DECODE(D.CUSTOMER_ID, NULL, p_demand_class,
2454                                                 0, p_demand_class,
2455                                     -- rajjain begin 07/19/2002
2456                                     decode(MSC_ATP_PVT.G_INTERNAL_SALES_ORDER,'N', --10425563
2457                                     decode(d.origination_type,
2458                                        6, decode(d.source_organization_id,
2459                                           NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2460                                                 D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
2461                                                 p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
2462                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2463                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2464                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2465                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
2466                                                 p_level_id, NULL),
2467                                           -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2468                                                 D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
2469                                                 p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
2470                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2471                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2472                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2473                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
2474                                                 p_level_id, NULL),
2475                                           d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2476                                                 D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
2477                                                 p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
2478                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2479                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2480                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2481                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
2482                                                 p_level_id, NULL),
2483                                           p_demand_class),
2484                                        30, decode(d.source_organization_id,
2485                                           NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2486                                                 D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
2487                                                 p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
2488                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2489                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2490                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2491                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
2492                                                 p_level_id, NULL),
2493                                           -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2494                                                 D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
2495                                                 p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
2496                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2497                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2498                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2499                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
2500                                                 p_level_id, NULL),
2501                                           d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2502                                                 D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
2503                                                 p_org_id, p_instance_id, TRUNC(DECODE(RECORD_SOURCE,
2504                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2505                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2506                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2507                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
2508                                                 p_level_id, NULL),
2509                                           p_demand_class),
2510                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(D.CUSTOMER_ID, D.SHIP_TO_SITE_ID,
2511                                           p_item_id, p_org_id, p_instance_id,
2512                                           TRUNC(DECODE(RECORD_SOURCE,
2513                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2514                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2515                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2516                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),p_level_id, NULL)),
2517                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, --10425563
2518                                           p_item_id, p_org_id, p_instance_id,
2519                                           TRUNC(DECODE(RECORD_SOURCE,
2520                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2521                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2522                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2523                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),p_level_id, NULL))
2524                                                       )),
2525                                     -- rajjain end 07/19/2002
2526                                     p_demand_class,
2527                                     p_level_id),0))) col24,
2528                                 /*New*/
2529                         -- -1* D.USING_REQUIREMENT_QUANTITY, -- fixed as part of time_phased_atp
2530                         -1* (D.USING_REQUIREMENT_QUANTITY - nvl(d.reserved_quantity,0)),--5027568
2531                         -- C.PRIOR_DATE col25, -- 2859130
2532                         GREATEST(TRUNC(DECODE(RECORD_SOURCE,
2533                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2534                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2535                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2536                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
2537                         	 p_sys_next_date) col25, --plan by request date --3099066
2538                         l_null_num col26,
2539                         DECODE(D.ORIGINATION_TYPE, 1, to_char(D.DISPOSITION_ID), D.ORDER_NUMBER) col27,
2540                                -- rajjain 04/25/2003 Bug 2771075
2541                                -- For Planned Order Demands We will populate disposition_id
2542                                -- in disposition_name column
2543                         l_null_num col28,
2544                         l_null_num col29,
2545         		-- ship_rec_cal changes begin
2546         		l_sysdate,
2547         		G_USER_ID,
2548         		l_sysdate,
2549         		G_USER_ID,
2550         		G_USER_ID,
2551         		-- ship_rec_cal changes end
2552                         -- Unallocated_Quantity
2553                         -- -1* D.USING_REQUIREMENT_QUANTITY, -- 2859130 remove decode for origination_type 4
2554                         -1* (D.USING_REQUIREMENT_QUANTITY - nvl(d.reserved_quantity,0)), --5027568
2555                         MTPS.LOCATION, --bug3263368
2556                         MTP.PARTNER_NAME, --bug3263368
2557                         D.DEMAND_CLASS, --bug3263368
2558                         DECODE(D.ORDER_DATE_TYPE_CODE,2,D.REQUEST_DATE,
2559                                                     D.REQUEST_SHIP_DATE) --bug3263368
2560             FROM        -- MSC_CALENDAR_DATES C, --2859130
2561                         MSC_DEMANDS D,
2562                         MSC_TRADING_PARTNERS    MTP,--bug3263368
2563                         MSC_TRADING_PARTNER_SITES    MTPS --bug3263368
2564             WHERE       D.PLAN_ID = p_plan_id
2565             AND         D.SR_INSTANCE_ID = p_instance_id
2566             AND         D.INVENTORY_ITEM_ID = p_item_id
2567             AND         D.ORGANIZATION_ID = p_org_id
2568             AND         D.USING_REQUIREMENT_QUANTITY <> 0 --4501434
2569             AND         D.ORIGINATION_TYPE NOT IN (5,7,8,9,11,15,22,28,29,31,52,70) -- For summary enhancement
2570             AND         D.SHIP_TO_SITE_ID = MTPS.PARTNER_SITE_ID(+) --bug3263368
2571             AND         D.CUSTOMER_ID = MTP.PARTNER_ID(+) --bug3263368
2572             -- 2859130
2573             -- Bug1990155, 1995835 exclude the expired lots demand datreya 9/18/2001
2574             -- Bug 1530311, need to exclude forecast, ngoel 12/05/2000
2575             -- AND         C.CALENDAR_CODE = p_cal_code
2576             -- AND         C.EXCEPTION_SET_ID = p_cal_exc_set_id
2577             -- AND         C.SR_INSTANCE_ID = D.SR_INSTANCE_ID
2578             -- since we store repetitive schedule demand in different ways for
2579             -- ods (total quantity on start date) and pds  (daily quantity from
2580             -- start date to end date), we need to make sure we only select work day
2581             -- for pds's repetitive schedule demand.
2582             -- AND         C.CALENDAR_DATE BETWEEN TRUNC(D.USING_ASSEMBLY_DEMAND_DATE) AND
2583             --             TRUNC(NVL(D.ASSEMBLY_DEMAND_COMP_DATE,
2584             --                       D.USING_ASSEMBLY_DEMAND_DATE))
2585             -- AND         (( D.ORIGINATION_TYPE = 4
2586             --               AND C.SEQ_NUM IS NOT NULL) OR
2587             --               ( D.ORIGINATION_TYPE  <> 4))
2588             -- 2859130
2589             -- Bug 3550296 and 3574164. IMPLEMENT_DATE AND DMD_SATISFIED_DATE are changed to
2590             -- IMPLEMENT_SHIP_DATE and PLANNED_SHIP_DATE resp.
2591             --bug3693892 added trunc
2592             AND         TRUNC(DECODE(RECORD_SOURCE,
2593                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2594                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2595                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2596                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))))
2597                        < TRUNC(NVL(p_itf,DECODE(RECORD_SOURCE,
2598                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2599                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2600                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2601                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))) + 1))
2602             -- AND C.CALENDAR_DATE < NVL(p_itf, C.CALENDAR_DATE+1)
2603             UNION ALL
2604             SELECT      p_level col1,
2605                         p_identifier col2,
2606                         p_scenario_id col3,
2607                         p_sr_item_id col4 ,
2608                         p_sr_item_id col5,
2609                         p_org_id col6,
2610                         l_null_num col7,
2611                         l_null_num col8,
2612                         l_null_num col9,
2613                         l_null_num col10,
2614                         l_null_num col11,
2615                         l_null_num col12,
2616                         l_null_num col13,
2617                         l_null_num col14,
2618                         l_null_char col15,
2619                         p_uom_code col16,
2620                         2 col17, -- supply
2621                         S.ORDER_TYPE col18,
2622                         l_null_char col19,
2623                         S.SR_INSTANCE_ID col20,
2624                                 l_null_num col21,
2625                         S.TRANSACTION_ID col22,
2626                         l_null_num col23,
2627                         NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY)*
2628                                 DECODE(p_scenario_id, -1, 1,
2629                                 DECODE(DECODE(G_HIERARCHY_PROFILE,
2630                                               --2424357
2631                                               1, DECODE(S.DEMAND_CLASS, null, null,
2632                                                      DECODE(p_demand_class,'-1',
2633                                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2634                                                           null,
2635                                                           null,
2636                                                           S.inventory_item_id,
2637                                                           p_org_id,
2638                                                           p_instance_id,
2639                                                           TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)),
2640                                                           p_level_id,
2641                                                           S.DEMAND_CLASS), S.DEMAND_CLASS)),
2642                                               2, DECODE(S.CUSTOMER_ID, NULL, TO_CHAR(NULL),
2643                                                         --0, TO_CHAR(NULL),
2644                                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2645                                                           S.CUSTOMER_ID,
2646                                                           S.SHIP_TO_SITE_ID,
2647                                                           S.inventory_item_id,
2648                                                           p_org_id,
2649                                                           p_instance_id,
2650                                                           TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)),
2651                                                           p_level_id,
2652                                                           NULL))),
2653                                         p_demand_class, 1,
2654                                         NULL, NVL(MIHM.allocation_percent/100,--4365873
2655                                         /*NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
2656                                                 p_instance_id,
2657                                                 s.inventory_item_id,
2658                                                 p_org_id,
2659                                                 null,
2660                                                 null,
2661                                                 p_demand_class,
2662                                                 TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE))),*/
2663                                                  1),
2664                                         DECODE(MIHM.allocation_percent/100, --4365873
2665                                         /*DECODE(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
2666                                                 p_instance_id,
2667                                                 s.inventory_item_id,
2668                                                 p_org_id,
2669                                                 null,
2670                                                 null,
2671                                                 p_demand_class,
2672                                                 TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE))),*/
2673                                         NULL, 1,
2674                                         0))) col24,
2675                         NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY),
2676                         --C.NEXT_DATE col25, -- 2859130
2677                         --TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)) col25,
2678                         GREATEST(TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)),p_sys_next_date) col25,--3099066
2679                         l_null_num col26,
2680                         DECODE(S.ORDER_TYPE, 5, to_char(S.TRANSACTION_ID), S.ORDER_NUMBER) col27,
2681                                -- Bug 2771075. For Planned Orders, we will populate transaction_id
2682 			       -- in the disposition_name column to be consistent with Planning.
2683                         l_null_num col28,
2684                         l_null_num col29,
2685         		-- ship_rec_cal changes begin
2686         		l_sysdate,
2687         		G_USER_ID,
2688         		l_sysdate,
2689         		G_USER_ID,
2690         		G_USER_ID,
2691         		-- ship_rec_cal changes end
2692                         NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY), -- unallocated quantity
2693                         null, --bug3263368 ORIG_CUSTOMER_SITE_NAME
2694                         null, --bug3263368 ORIG_CUSTOMER_NAME
2695                         null, --bug3263368 ORIG_DEMAND_CLASS
2696                         null  --bug3263368 ORIG_REQUEST_DATE
2697             FROM        -- MSC_CALENDAR_DATES C, -- 2859130
2698                         MSC_SUPPLIES S,MSC_ITEM_HIERARCHY_MV  MIHM
2699             WHERE       S.PLAN_ID = p_plan_id
2700             AND         S.SR_INSTANCE_ID = p_instance_id
2701             AND         S.INVENTORY_ITEM_ID = p_item_id
2702             AND         S.ORGANIZATION_ID = p_org_id
2703                         -- Exclude Cancelled Supplies 2460645
2704             AND         NVL(S.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
2705             AND         NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY) <> 0 -- 1243985
2706             --2859130
2707             --AND         C.CALENDAR_CODE = p_cal_code
2708             --AND         C.EXCEPTION_SET_ID = p_cal_exc_set_id
2709             --AND         C.SR_INSTANCE_ID = S.SR_INSTANCE_ID
2710             --AND         C.CALENDAR_DATE BETWEEN TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE))
2711             --                    AND TRUNC(NVL(S.LAST_UNIT_COMPLETION_DATE,
2712             --                        NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)))
2713             --AND         DECODE(S.LAST_UNIT_COMPLETION_DATE,
2714             --                   NULL, C.NEXT_SEQ_NUM, C.SEQ_NUM) IS NOT NULL
2715             --AND         C.NEXT_DATE >= DECODE(S.ORDER_TYPE, 27, TRUNC(SYSDATE),
2716             --                                                28, TRUNC(SYSDATE),
2717             --                                                C.NEXT_DATE)
2718             --AND         C.NEXT_DATE < NVL(p_itf,
2719             --                             C.NEXT_DATE + 1)
2720             AND         TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)) >= DECODE(S.ORDER_TYPE, 27, TRUNC(SYSDATE),
2721                                                             28, TRUNC(SYSDATE),
2722                                                             TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)))
2723             AND         TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)) < NVL(p_itf,
2724                                          TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)) + 1)
2725 	    --4365873
2726             AND    S.INVENTORY_ITEM_ID = MIHM.INVENTORY_ITEM_ID(+)
2727             AND    S.SR_INSTANCE_ID = MIHM.SR_INSTANCE_ID (+)
2728             AND    S.ORGANIZATION_ID = MIHM.ORGANIZATION_ID (+)
2729             AND    decode(MIHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
2730             AND    TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)) >= MIHM.effective_date (+)
2731             AND    TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)) <= MIHM.disable_date (+)
2732             AND    MIHM.demand_class (+) = p_demand_class
2733            ); -- dsting removed order by col25
2734 
2735 END item_alloc_avail_opt_dtls;
2736 --avjain All netting sqls have been changed to incorporate Plan by Request Date Enhancements
2737 PROCEDURE item_alloc_avail_unopt_dtls (
2738    p_item_id            IN NUMBER,
2739    p_org_id             IN NUMBER,
2740    p_instance_id        IN NUMBER,
2741    p_plan_id            IN NUMBER,
2742    p_demand_class       IN VARCHAR2,
2743    p_level_id           IN NUMBER,
2744    p_itf                IN DATE,
2745    p_cal_code           IN VARCHAR2,
2746    p_cal_exc_set_id     IN NUMBER,
2747    p_sr_item_id         IN NUMBER,
2748    p_level              IN NUMBER,
2749    p_identifier         IN NUMBER,
2750    p_scenario_id        IN NUMBER,
2751    p_uom_code           IN VARCHAR2,
2752    p_sys_next_date	IN DATE
2753 ) IS
2754    l_null_num   NUMBER;
2755    l_null_char  VARCHAR2(1);
2756    l_sysdate    DATE := trunc(sysdate);--4135752
2757 BEGIN
2758         IF PG_DEBUG in ('Y', 'C') THEN
2759                 msc_sch_wb.atp_debug('item_alloc_avail_unopt_dtls: UNconstrained plan: ' || p_plan_id);
2760         END IF;
2761 
2762         INSERT INTO msc_atp_sd_details_temp (
2763                 ATP_Level,
2764                 Order_line_id,
2765                 Scenario_Id,
2766                 Inventory_Item_Id,
2767                 Request_Item_Id,
2768                 Organization_Id,
2769                 Department_Id,
2770                 Resource_Id,
2771                 Supplier_Id,
2772                 Supplier_Site_Id,
2773                 From_Organization_Id,
2774                 From_Location_Id,
2775                 To_Organization_Id,
2776                 To_Location_Id,
2777                 Ship_Method,
2778                 UOM_code,
2779                 Supply_Demand_Type,
2780                 Supply_Demand_Source_Type,
2781                 Supply_Demand_Source_Type_Name,
2782                 Identifier1,
2783                 Identifier2,
2784                 Identifier3,
2785                 Identifier4,
2786                 Allocated_Quantity,
2787                 Supply_Demand_Quantity,
2788                 Supply_Demand_Date,
2789                 Disposition_Type,
2790                 Disposition_Name,
2791                 Pegging_Id,
2792                 End_Pegging_Id,
2793                 creation_date,
2794                 created_by,
2795                 last_update_date,
2796                 last_updated_by,
2797                 last_update_login,
2798                 Unallocated_Quantity,
2799                 ORIG_CUSTOMER_SITE_NAME,--bug3263368
2800                 ORIG_CUSTOMER_NAME, --bug3263368
2801                 ORIG_DEMAND_CLASS, --bug3263368
2802                 ORIG_REQUEST_DATE --bug3263368
2803                 )
2804            (
2805             SELECT      p_level col1,
2806                         p_identifier col2,
2807                         p_scenario_id col3,
2808                         p_sr_item_id col4 ,
2809                         p_sr_item_id col5,
2810                         p_org_id col6,
2811                         l_null_num col7,
2812                         l_null_num col8,
2813                         l_null_num col9,
2814                         l_null_num col10,
2815                         l_null_num col11,
2816                         l_null_num col12,
2817                         l_null_num col13,
2818                         l_null_num col14,
2819                         l_null_char col15,
2820                         p_uom_code col16,
2821                         1 col17, -- demand
2822                         --D.ORIGINATION_TYPE col18,
2823                         DECODE( D.ORIGINATION_TYPE, -100, 30, D.ORIGINATION_TYPE) col18,  --5027568
2824                         l_null_char col19,
2825                         D.SR_INSTANCE_ID col20,
2826                         l_null_num col21,
2827                         D.DEMAND_ID col22,
2828                         l_null_num col23,
2829                         -1* DECODE(D.ORIGINATION_TYPE,
2830                                4, D.DAILY_DEMAND_RATE,
2831                                --D.USING_REQUIREMENT_QUANTITY)*
2832                                (D.USING_REQUIREMENT_QUANTITY - NVL(d.reserved_quantity, 0)))* --5027568
2833 			/*New*/
2834                         DECODE(p_scenario_id, -1, 1,
2835                         --2424357
2836                         DECODE(DECODE(G_HIERARCHY_PROFILE,
2837                         /*------------------------------------------------------------------------+
2838                         | rajjain begin 07/19/2002                                                |
2839                         |                                                                         |
2840                         | Case 1: For internal sales orders [origination type is in (6,30) and    |
2841                         |            source_organization_id is not null and <> organization_id]   |
2842                         |                  Return NULL                                            |
2843                         | Case 2: For others if DEMAND_CLASS is null then return null             |
2844                         |          else if p_demand_class is '-1' then call                       |
2845                         |            Get_Hierarchy_Demand_class else return DEMAND_CLASS          |
2846                         +------------------------------------------------------------------------*/
2847                            1,
2848                            decode(MSC_ATP_PVT.G_INTERNAL_SALES_ORDER,'N', --10425563
2849                            decode(d.origination_type,
2850                               6, decode(d.source_organization_id,
2851                                  NULL, DECODE(D.DEMAND_CLASS, null, null,
2852                                     DECODE(p_demand_class, '-1',
2853                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2854                                           null, null, p_item_id, p_org_id,
2855                                           p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
2856                                           D.DEMAND_CLASS)),
2857                                  -23453, DECODE(D.DEMAND_CLASS, null, null,
2858                                     DECODE(p_demand_class, '-1',
2859                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2860                                           null, null, p_item_id, p_org_id,
2861                                           p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
2862                                           D.DEMAND_CLASS)),
2863                                  d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
2864                                     DECODE(p_demand_class, '-1',
2865                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2866                                           null, null, p_item_id, p_org_id,
2867                                           p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
2868                                           D.DEMAND_CLASS)), NULL),
2869                               30, decode(d.source_organization_id,
2870                                  NULL, DECODE(D.DEMAND_CLASS, null, null,
2871                                     DECODE(p_demand_class, '-1',
2872                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2873                                           null, null, p_item_id, p_org_id,
2874                                           p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
2875                                           D.DEMAND_CLASS)),
2876                                  -23453, DECODE(D.DEMAND_CLASS, null, null,
2877                                     DECODE(p_demand_class, '-1',
2878                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2879                                           null, null, p_item_id, p_org_id,
2880                                           p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
2881                                           D.DEMAND_CLASS)),
2882                                  d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
2883                                     DECODE(p_demand_class, '-1',
2884                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2885                                           null, null, p_item_id, p_org_id,
2886                                           p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
2887                                           D.DEMAND_CLASS)), NULL),
2888                               DECODE(D.DEMAND_CLASS, null, null,
2889                                  DECODE(p_demand_class, '-1',
2890                                     MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2891                                        null, null, p_item_id, p_org_id,
2892                                        p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
2893                                        D.DEMAND_CLASS))),
2894                               DECODE(D.DEMAND_CLASS, null, null, --10425563
2895                                  DECODE(p_demand_class, '-1',
2896                                     MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2897                                        null, null, p_item_id, p_org_id,
2898                                        p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
2899                                        D.DEMAND_CLASS))),
2900                            -- rajjain end
2901                            2, DECODE(D.CUSTOMER_ID, NULL, NULL,
2902                                                 0, NULL,
2903                               -- rajjain begin 07/19/2002
2904                               decode(MSC_ATP_PVT.G_INTERNAL_SALES_ORDER,'N', --10425563
2905                               decode(d.origination_type,
2906                                  6, decode(d.source_organization_id,
2907                                     NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2908                                           D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
2909                                           p_org_id, p_instance_id, c.calendar_date,
2910                                           p_level_id, NULL),
2911                                     -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2912                                           D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
2913                                           p_org_id, p_instance_id, c.calendar_date,
2914                                           p_level_id, NULL),
2915                                     d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2916                                           D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
2917                                           p_org_id, p_instance_id, c.calendar_date,
2918                                           p_level_id, NULL),
2919                                     NULL),
2920                                  30, decode(d.source_organization_id,
2921                                     NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2922                                           D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
2923                                           p_org_id, p_instance_id, c.calendar_date,
2924                                           p_level_id, NULL),
2925                                     -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2926                                           D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
2927                                           p_org_id, p_instance_id, c.calendar_date,
2928                                           p_level_id, NULL),
2929                                     d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2930                                           D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
2931                                           p_org_id, p_instance_id, c.calendar_date,
2932                                           p_level_id, NULL),
2933                                     NULL),
2934                                  MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(D.CUSTOMER_ID, D.SHIP_TO_SITE_ID,
2935                                     p_item_id, p_org_id, p_instance_id,
2936                                     c.calendar_date,p_level_id, NULL)),
2937                                  MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, --10425563
2938                                     p_item_id, p_org_id, p_instance_id,
2939                                     c.calendar_date,p_level_id, NULL))
2940                                     )),
2941                               -- rajjain end 07/19/2002
2942                         p_demand_class, 1,
2943                         Decode(D.Demand_Class, NULL, --4365873
2944                            MSC_AATP_FUNC.Get_Item_Demand_Alloc_Percent(p_plan_id,
2945                               D.DEMAND_ID,
2946                               c.calendar_date,
2947                               D.USING_ASSEMBLY_ITEM_ID,
2948                               DECODE(D.SOURCE_ORGANIZATION_ID,
2949                                  -23453, null,
2950                                  D.SOURCE_ORGANIZATION_ID),
2951                               p_item_id,
2952                               p_org_id, -- 1665483
2953                               p_instance_id,
2954                               decode (d.origination_type, -100, 30,d.origination_type),
2955                               DECODE(G_HIERARCHY_PROFILE,
2956                               /*-----------------------------------------------------------------+
2957                               | rajjain begin 07/19/2002                                         |
2958                               |                                                                  |
2959                               | Case 1: For internal sales orders [origination type is in (6,30) |
2960                               |         and source_organization_id is not null                   |
2961                               |         and <> organization_id] -> Return p_demand_class         |
2962                               | Case 2: For others if DEMAND_CLASS is null then return null      |
2963                               |           else if p_demand_class is '-1' then call               |
2964                               |           Get_Hierarchy_Demand_class else return DEMAND_CLASS    |
2965                               +-----------------------------------------------------------------*/
2966                                  1,
2967                                  decode(MSC_ATP_PVT.G_INTERNAL_SALES_ORDER,'N', --10425563
2968                                  decode(d.origination_type,
2969                                     6, decode(d.source_organization_id,
2970                                        NULL, DECODE(D.DEMAND_CLASS, null, null,
2971                                           DECODE(p_demand_class, '-1',
2972                                              MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2973                                                 null, null, p_item_id, p_org_id,
2974                                                 p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
2975                                              D.DEMAND_CLASS)),
2976                                        -23453, DECODE(D.DEMAND_CLASS, null, null,
2977                                           DECODE(p_demand_class, '-1',
2978                                              MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2979                                                 null, null, p_item_id, p_org_id,
2980                                                 p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
2981                                              D.DEMAND_CLASS)),
2982                                        d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
2983                                           DECODE(p_demand_class, '-1',
2984                                              MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2985                                                 null, null, p_item_id, p_org_id,
2986                                                 p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
2987                                              D.DEMAND_CLASS)),
2988                                        p_demand_class),
2989                                  30, decode(d.source_organization_id,
2990                                     NULL, DECODE(D.DEMAND_CLASS, null, null,
2991                                        DECODE(p_demand_class, '-1',
2992                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2993                                              null, null, p_item_id, p_org_id,
2994                                              p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
2995                                           D.DEMAND_CLASS)),
2996                                     -23453, DECODE(D.DEMAND_CLASS, null, null,
2997                                        DECODE(p_demand_class, '-1',
2998                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2999                                              null, null, p_item_id, p_org_id,
3000                                              p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
3001                                           D.DEMAND_CLASS)),
3002                                     d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
3003                                        DECODE(p_demand_class, '-1',
3004                                           MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
3005                                              null, null, p_item_id, p_org_id,
3006                                              p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
3007                                           D.DEMAND_CLASS)),
3008                                     p_demand_class),
3009                                  DECODE(D.DEMAND_CLASS, null, null,
3010                                     DECODE(p_demand_class, '-1',
3011                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
3012                                           null, null, p_item_id, p_org_id,
3013                                           p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
3014                                        D.DEMAND_CLASS))),
3015                                  DECODE(D.DEMAND_CLASS, null, null,
3016                                     DECODE(p_demand_class, '-1',
3017                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
3018                                           null, null, p_item_id, p_org_id,
3019                                           p_instance_id, c.calendar_date, p_level_id, D.DEMAND_CLASS),
3020                                        D.DEMAND_CLASS))),
3021                                  -- rajjain end
3022                                  2, DECODE(D.CUSTOMER_ID, NULL, p_demand_class,
3023                                                 0, p_demand_class,
3024                                     -- rajjain begin 07/19/2002
3025                                     decode(MSC_ATP_PVT.G_INTERNAL_SALES_ORDER,'N',
3026                                     decode(d.origination_type,
3027                                        6, decode(d.source_organization_id,
3028                                           NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
3029                                                 D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
3030                                                 p_org_id, p_instance_id, c.calendar_date,
3031                                                 p_level_id, NULL),
3032                                           -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
3033                                                 D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
3034                                                 p_org_id, p_instance_id, c.calendar_date,
3035                                                 p_level_id, NULL),
3036                                           d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
3037                                                 D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
3038                                                 p_org_id, p_instance_id, c.calendar_date,
3039                                                 p_level_id, NULL),
3040                                           p_demand_class),
3041                                        30, decode(d.source_organization_id,
3042                                           NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
3043                                                 D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
3044                                                 p_org_id, p_instance_id, c.calendar_date,
3045                                                 p_level_id, NULL),
3046                                           -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
3047                                                 D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
3048                                                 p_org_id, p_instance_id, c.calendar_date,
3049                                                 p_level_id, NULL),
3050                                           d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
3051                                                 D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, p_item_id,
3052                                                 p_org_id, p_instance_id, c.calendar_date,
3053                                                 p_level_id, NULL),
3054                                           p_demand_class),
3055                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(D.CUSTOMER_ID, D.SHIP_TO_SITE_ID,
3056                                           p_item_id, p_org_id, p_instance_id,
3057                                           c.calendar_date,p_level_id, NULL)),
3058                                        MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(D.CUSTOMER_ID, D.SHIP_TO_SITE_ID,
3059                                           p_item_id, p_org_id, p_instance_id,
3060                                           c.calendar_date,p_level_id, NULL)) --10425563
3061                                           )),
3062                                     -- rajjain end 07/19/2002
3063                                     p_demand_class,
3064                                     p_level_id),0))) col24,
3065                                 /*New*/
3066                         -1* DECODE(D.ORIGINATION_TYPE,
3067                                4, D.DAILY_DEMAND_RATE,
3068                                --D.USING_REQUIREMENT_QUANTITY),
3069                                (D.USING_REQUIREMENT_QUANTITY- nvl(d.reserved_quantity,0))),  --5027568
3070 
3071                         -- C.PRIOR_DATE col25, -- 2859130
3072                         --C.CALENDAR_DATE col25,
3073                         GREATEST(C.CALENDAR_DATE,p_sys_next_date) col25,
3074                         l_null_num col26,
3075                         DECODE(D.ORIGINATION_TYPE, 1, to_char(D.DISPOSITION_ID), D.ORDER_NUMBER) col27,
3076                                -- rajjain 04/25/2003 Bug 2771075
3077                                -- For Planned Order Demands We will populate disposition_id
3078                                -- in disposition_name column
3079                         l_null_num col28,
3080                         l_null_num col29,
3081                         -- ship_rec_cal changes begin
3082                         l_sysdate,
3083                         G_USER_ID,
3084                         l_sysdate,
3085                         G_USER_ID,
3086                         G_USER_ID,
3087                         -- ship_rec_cal changes end
3088                         -- Unallocated_Quantity
3089                         -1* DECODE(D.ORIGINATION_TYPE,
3090                                 4, D.DAILY_DEMAND_RATE,
3091                         --D.USING_REQUIREMENT_QUANTITY),
3092                         (D.USING_REQUIREMENT_QUANTITY- nvl(d.reserved_quantity,0))),  --5027568
3093                         MTPS.LOCATION, --bug3263368
3094                         MTP.PARTNER_NAME, --bug3263368
3095                         D.DEMAND_CLASS, --bug3263368
3096                         DECODE(D.ORDER_DATE_TYPE_CODE,2,D.REQUEST_DATE,
3097                                                 D.REQUEST_SHIP_DATE) --bug3263368
3098 
3099             FROM        MSC_CALENDAR_DATES C,
3100                         MSC_DEMANDS D,
3101                         MSC_TRADING_PARTNERS    MTP,--bug3263368
3102                         MSC_TRADING_PARTNER_SITES    MTPS --bug3263368
3103 
3104             WHERE       D.PLAN_ID = p_plan_id
3105             AND         D.SR_INSTANCE_ID = p_instance_id
3106             AND         D.INVENTORY_ITEM_ID = p_item_id
3107             AND         D.USING_REQUIREMENT_QUANTITY <> 0 --4501434
3108             AND         D.ORGANIZATION_ID = p_org_id
3109             AND         D.ORIGINATION_TYPE NOT IN (5,7,8,9,11,15,22,28,29,31,52,70) -- For summary enhancement
3110             AND         D.SHIP_TO_SITE_ID = MTPS.PARTNER_SITE_ID(+) --bug3263368
3111             AND         D.CUSTOMER_ID = MTP.PARTNER_ID(+)--bug3263368
3112 
3113             -- Bug1990155, 1995835 exclude the expired lots demand datreya 9/18/2001
3114             -- Bug 1530311, need to exclude forecast, ngoel 12/05/2000
3115             AND         C.CALENDAR_CODE = p_cal_code
3116             AND         C.EXCEPTION_SET_ID = p_cal_exc_set_id
3117             AND         C.SR_INSTANCE_ID = D.SR_INSTANCE_ID
3118             -- since we store repetitive schedule demand in different ways for
3119             -- ods (total quantity on start date) and pds  (daily quantity from
3120             -- start date to end date), we need to make sure we only select work day
3121             -- for pds's repetitive schedule demand.
3122             -- Bug 3550296 and 3574164. IMPLEMENT_DATE AND DMD_SATISFIED_DATE are changed to
3123             -- IMPLEMENT_SHIP_DATE and PLANNED_SHIP_DATE resp.
3124             AND         C.CALENDAR_DATE BETWEEN TRUNC(DECODE(RECORD_SOURCE,
3125                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
3126                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
3127                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
3128                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))) AND
3129                         TRUNC(NVL(D.ASSEMBLY_DEMAND_COMP_DATE,
3130                                   DECODE(RECORD_SOURCE,
3131                                          2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
3132                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
3133                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
3134                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))))
3135             AND         (( D.ORIGINATION_TYPE = 4
3136                           AND C.SEQ_NUM IS NOT NULL) OR
3137                           ( D.ORIGINATION_TYPE  <> 4))
3138             -- 2859130
3139             -- AND         C.PRIOR_DATE < NVL(p_itf,
3140             --                              C.PRIOR_DATE + 1)
3141             AND C.CALENDAR_DATE < NVL(p_itf, C.CALENDAR_DATE+1)
3142             UNION ALL
3143             SELECT      p_level col1,
3144                         p_identifier col2,
3145                         p_scenario_id col3,
3146                         p_sr_item_id col4 ,
3147                         p_sr_item_id col5,
3148                         p_org_id col6,
3149                         l_null_num col7,
3150                         l_null_num col8,
3151                         l_null_num col9,
3152                         l_null_num col10,
3153                         l_null_num col11,
3154                         l_null_num col12,
3155                         l_null_num col13,
3156                         l_null_num col14,
3157                         l_null_char col15,
3158                         p_uom_code col16,
3159                         2 col17, -- supply
3160                         CS.ORDER_TYPE col18,
3161                         l_null_char col19,
3162                         CS.SR_INSTANCE_ID col20,
3163                                 l_null_num col21,
3164                         CS.TRANSACTION_ID col22,
3165                         l_null_num col23,
3166                         NVL(CS.FIRM_QUANTITY,CS.NEW_ORDER_QUANTITY)*
3167                                 DECODE(p_scenario_id, -1, 1,
3168                                 DECODE(DECODE(G_HIERARCHY_PROFILE,
3169                                               --2424357
3170                                               1, DECODE(CS.DEMAND_CLASS, null, null,
3171                                                      DECODE(p_demand_class,'-1',
3172                                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
3173                                                           null,
3174                                                           null,
3175                                                           cs.inventory_item_id,
3176                                                           p_org_id,
3177                                                           p_instance_id,
3178                                                           cs.calendar_date,
3179                                                           p_level_id,
3180                                                           CS.DEMAND_CLASS), CS.DEMAND_CLASS)),
3181                                               2, DECODE(CS.CUSTOMER_ID, NULL, TO_CHAR(NULL),
3182                                                         --0, TO_CHAR(NULL),
3183                                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
3184                                                           CS.CUSTOMER_ID,
3185                                                           CS.SHIP_TO_SITE_ID,
3186                                                           cs.inventory_item_id,
3187                                                           p_org_id,
3188                                                           p_instance_id,
3189                                                           cs.calendar_date,
3190                                                           p_level_id,
3191                                                           NULL))),
3192                                         p_demand_class, 1,
3193                                         NULL, nvl(MIHM.allocation_percent/100,1), --4365873
3194                                         /*NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
3195                                                 p_instance_id,
3196                                                 cs.inventory_item_id,
3197                                                 p_org_id,
3198                                                 null,
3199                                                 null,
3200                                                 p_demand_class,
3201                                                 cs.calendar_date), 1),*/
3202                                         DECODE(
3203                                         MIHM.allocation_percent/100, --4365873
3204                                         /*DECODE(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
3205                                                 p_instance_id,
3206                                                 cs.inventory_item_id,
3207                                                 p_org_id,
3208                                                 null,
3209                                                 null,
3210                                                 p_demand_class,
3211                                                 cs.calendar_date),*/
3212                                         NULL, 1,
3213                                         0))) col24,
3214                         NVL(CS.FIRM_QUANTITY,CS.NEW_ORDER_QUANTITY),
3215                         --C.NEXT_DATE col25, -- 2859130
3216                         --C.CALENDAR_DATE col25,
3217                         GREATEST(CS.CALENDAR_DATE,p_sys_next_date) col25,--3099066
3218                         l_null_num col26,
3219                         DECODE(CS.ORDER_TYPE, 5, to_char(CS.TRANSACTION_ID), CS.ORDER_NUMBER) col27,
3220                                -- Bug 2771075. For Planned Orders, we will populate transaction_id
3221 			       -- in the disposition_name column to be consistent with Planning.
3222                         l_null_num col28,
3223                         l_null_num col29,
3224                         -- ship_rec_cal changes begin
3225                         l_sysdate,
3226                         G_USER_ID,
3227                         l_sysdate,
3228                         G_USER_ID,
3229                         G_USER_ID,
3230                         -- ship_rec_cal changes end
3231                         NVL(CS.FIRM_QUANTITY,CS.NEW_ORDER_QUANTITY), -- unallocated quantity
3232                         null, --bug3263368 ORIG_CUSTOMER_SITE_NAME
3233                         null, --bug3263368 ORIG_CUSTOMER_NAME
3234                         null, --bug3263368 ORIG_DEMAND_CLASS
3235                         null  --bug3263368 ORIG_REQUEST_DATE
3236             FROM
3237 
3238             	(	select
3239             		S.ORDER_TYPE,
3240 			S.TRANSACTION_ID,
3241 			S.ORDER_NUMBER,
3242 			S.SR_INSTANCE_ID,
3243 			C.NEXT_DATE,
3244 			S.FIRM_QUANTITY,
3245 			S.NEW_ORDER_QUANTITY,
3246 			S.DEMAND_CLASS,
3247 			s.inventory_item_id,
3248 			S.CUSTOMER_ID,
3249 			S.SHIP_TO_SITE_ID,
3250 			S.ORGANIZATION_ID,
3251 			C.CALENDAR_DATE
3252 
3253             FROM        MSC_CALENDAR_DATES C,
3254                         MSC_SUPPLIES S
3255             WHERE       S.PLAN_ID = p_plan_id
3256             AND         S.SR_INSTANCE_ID = p_instance_id
3257             AND         S.INVENTORY_ITEM_ID = p_item_id
3258             AND         S.ORGANIZATION_ID = p_org_id
3259                         -- Exclude Cancelled Supplies 2460645
3260             AND         NVL(S.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
3261             AND         NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY) <> 0 -- 1243985
3262             AND         C.CALENDAR_CODE = p_cal_code
3263             AND         C.EXCEPTION_SET_ID = p_cal_exc_set_id
3264             AND         C.SR_INSTANCE_ID = S.SR_INSTANCE_ID
3265             AND         C.CALENDAR_DATE BETWEEN TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE))
3266                                 AND TRUNC(NVL(S.LAST_UNIT_COMPLETION_DATE,
3267                                     NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)))
3268             AND         DECODE(TRUNC(S.LAST_UNIT_COMPLETION_DATE), --4135752
3269                                NULL, C.NEXT_SEQ_NUM, C.SEQ_NUM) IS NOT NULL
3270             --2859130
3271             --AND         C.NEXT_DATE >= DECODE(S.ORDER_TYPE, 27, TRUNC(SYSDATE),
3272             --                                                28, TRUNC(SYSDATE),
3273             --                                                C.NEXT_DATE)
3274             --AND         C.NEXT_DATE < NVL(p_itf,
3275             --                             C.NEXT_DATE + 1)
3276             AND         C.CALENDAR_DATE >= DECODE(S.ORDER_TYPE, 27, TRUNC(SYSDATE),
3277                                                             28, TRUNC(SYSDATE),
3278                                                             C.CALENDAR_DATE)
3279             AND         C.CALENDAR_DATE < NVL(p_itf,
3280                                          C.CALENDAR_DATE + 1))CS,
3281             MSC_ITEM_HIERARCHY_MV  MIHM
3282             WHERE
3283 	    --4365873
3284                    CS.INVENTORY_ITEM_ID = MIHM.INVENTORY_ITEM_ID(+)
3285             AND    CS.SR_INSTANCE_ID = MIHM.SR_INSTANCE_ID (+)
3286             AND    CS.ORGANIZATION_ID = MIHM.ORGANIZATION_ID (+)
3287             AND    decode(MIHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
3288             AND    CS.NEXT_DATE >= MIHM.effective_date (+)
3289             AND    CS.NEXT_DATE <= MIHM.disable_date (+)
3290             AND    MIHM.demand_class (+) = p_demand_class
3291            )
3292            ; -- dsting removed order by col25
3293 END item_alloc_avail_unopt_dtls;
3294 
3295 ---------------------------------------------------------------------------
3296 
3297 PROCEDURE item_alloc_avail (
3298    p_optimized_plan     IN NUMBER,
3299    p_item_id            IN NUMBER,
3300    p_org_id             IN NUMBER,
3301    p_instance_id        IN NUMBER,
3302    p_plan_id            IN NUMBER,
3303    p_demand_class       IN VARCHAR2,
3304    p_level_id           IN NUMBER,
3305    p_itf                IN DATE,
3306    p_cal_code           IN VARCHAR2,
3307    p_cal_exc_set_id     IN NUMBER,
3308    p_sys_next_date	IN DATE,			--bug3099066
3309    x_atp_dates          OUT NoCopy MRP_ATP_PUB.date_arr,
3310    x_atp_qtys           OUT NoCopy MRP_ATP_PUB.number_arr
3311 ) IS
3312 BEGIN
3313         IF PG_DEBUG in ('Y', 'C') THEN
3314                 msc_sch_wb.atp_debug('item_alloc_avail');
3315         END IF;
3316 
3317         IF nvl(p_optimized_plan, 2) = 1 THEN
3318                 item_alloc_avail_opt(
3319                      p_item_id,
3320                      p_org_id,
3321                      p_instance_id,
3322                      p_plan_id,
3323                      p_demand_class,
3324                      p_level_id,
3325                      p_itf,
3326                      p_cal_code,
3327                      p_cal_exc_set_id,
3328                      p_sys_next_date,	--bug3099066
3329                      x_atp_dates,
3330                      x_atp_qtys
3331           );
3332         ELSE
3333                 item_alloc_avail_unopt(
3334                      p_item_id,
3335                      p_org_id,
3336                      p_instance_id,
3337                      p_plan_id,
3338                      p_demand_class,
3339                      p_level_id,
3340                      p_itf,
3341                      p_cal_code,
3342                      p_cal_exc_set_id,
3343                      p_sys_next_date,	--bug3099066
3344                      x_atp_dates,
3345                      x_atp_qtys
3346           );
3347         END IF;
3348 END item_alloc_avail;
3349 
3350 PROCEDURE item_alloc_avail_unalloc (
3351    p_optimized_plan     IN NUMBER,
3352    p_item_id            IN NUMBER,
3353    p_org_id             IN NUMBER,
3354    p_instance_id        IN NUMBER,
3355    p_plan_id            IN NUMBER,
3356    p_demand_class       IN VARCHAR2,
3357    p_level_id           IN NUMBER,
3358    p_itf                IN DATE,
3359    p_cal_code           IN VARCHAR2,
3360    p_cal_exc_set_id     IN NUMBER,
3361    p_sys_next_date	IN DATE,			--bug3099066
3362    x_atp_dates          OUT NoCopy MRP_ATP_PUB.date_arr,
3363    x_atp_qtys           OUT NoCopy MRP_ATP_PUB.number_arr,
3364    x_atp_unalloc_qtys   OUT NoCopy MRP_ATP_PUB.number_arr
3365 ) IS
3366 BEGIN
3367         IF PG_DEBUG in ('Y', 'C') THEN
3368                 msc_sch_wb.atp_debug('item_alloc_avail_unalloc: Get unallocated qtys as well');
3369         END IF;
3370 
3371         IF nvl(p_optimized_plan, 2) = 1 THEN
3372                 item_alloc_avail_opt_unalloc(
3373                      p_item_id,
3374                      p_org_id,
3375                      p_instance_id,
3376                      p_plan_id,
3377                      p_demand_class,
3378                      p_level_id,
3379                      p_itf,
3380                      p_cal_code,
3381                      p_cal_exc_set_id,
3382                      p_sys_next_date,		--bug3099066
3383                      x_atp_dates,
3384                      x_atp_qtys,
3385                      x_atp_unalloc_qtys
3386           );
3387         ELSE
3388                 item_alloc_avail_unopt_unalloc(
3389                      p_item_id,
3390                      p_org_id,
3391                      p_instance_id,
3392                      p_plan_id,
3393                      p_demand_class,
3394                      p_level_id,
3395                      p_itf,
3396                      p_cal_code,
3397                      p_cal_exc_set_id,
3398                      p_sys_next_date,		--bug3099066
3399                      x_atp_dates,
3400                      x_atp_qtys,
3401                      x_atp_unalloc_qtys
3402           );
3403         END IF;
3404 END item_alloc_avail_unalloc;
3405 
3406 PROCEDURE item_alloc_avail_dtls (
3407    p_optimized_plan     IN NUMBER,
3408    p_item_id            IN NUMBER,
3409    p_org_id             IN NUMBER,
3410    p_instance_id        IN NUMBER,
3411    p_plan_id            IN NUMBER,
3412    p_demand_class       IN VARCHAR2,
3413    p_level_id           IN NUMBER,
3414    p_itf                IN DATE,
3415    p_cal_code           IN VARCHAR2,
3416    p_cal_exc_set_id     IN NUMBER,
3417    p_sr_item_id         IN NUMBER,
3418    p_level              IN NUMBER,
3419    p_identifier         IN NUMBER,
3420    p_scenario_id        IN NUMBER,
3421    p_uom_code           IN VARCHAR2,
3422    p_sys_next_date	IN DATE)  	--bug3099066
3423    IS
3424 BEGIN
3425         IF PG_DEBUG in ('Y', 'C') THEN
3426                 msc_sch_wb.atp_debug('item_alloc_avail_dtls');
3427         END IF;
3428 
3429         IF nvl(p_optimized_plan, 2) = 1 THEN
3430                 item_alloc_avail_opt_dtls(
3431                    p_item_id,
3432                    p_org_id,
3433                    p_instance_id,
3434                    p_plan_id,
3435                    p_demand_class,
3436                    p_level_id,
3437                    p_itf,
3438                    p_cal_code,
3439                    p_cal_exc_set_id,
3440                    p_sr_item_id,
3441                    p_level,
3442                    p_identifier,
3443                    p_scenario_id,
3444                    p_uom_code,
3445                    p_sys_next_date		--bug3099066
3446                 );
3447         ELSE
3448                 item_alloc_avail_unopt_dtls(
3449                    p_item_id,
3450                    p_org_id,
3451                    p_instance_id,
3452                    p_plan_id,
3453                    p_demand_class,
3454                    p_level_id,
3455                    p_itf,
3456                    p_cal_code,
3457                    p_cal_exc_set_id,
3458                    p_sr_item_id,
3459                    p_level,
3460                    p_identifier,
3461                    p_scenario_id,
3462                    p_uom_code,
3463                    p_sys_next_date		--bug3099066
3464                 );
3465         END IF;
3466 
3467 END item_alloc_avail_dtls;
3468 -- end 2859130
3469 
3470 ---------------------------------------------------------------------------
3471 
3472 PROCEDURE Atp_Demand_Class_Consume(
3473         p_current_atp   IN OUT  NoCopy MRP_ATP_PVT.ATP_Info,
3474         p_steal_atp     IN OUT  NoCopy MRP_ATP_PVT.ATP_Info,
3475         p_atf_date      IN DATE := NULL)   -- time_phased_atp
3476 IS
3477 	i NUMBER; -- index for p_steal_atp
3478 	j NUMBER; -- index for p_current_atp
3479 	k NUMBER; -- starting point for consumption of p_current_atp
3480 	m NUMBER;
3481         l_allowed_stealing_qty NUMBER;
3482 BEGIN
3483 
3484     IF PG_DEBUG in ('Y', 'C') THEN
3485        msc_sch_wb.atp_debug('*******Begin Atp_Demand_Class_Consume Procedure******');
3486 
3487        m := p_current_atp.atp_period.FIRST;
3488        WHILE m is not null LOOP
3489             msc_sch_wb.atp_debug('Atp_Demand_Class_Consume: ' || 'p_current_atp.atp_period , qty  = '||
3490 	    p_current_atp.atp_period(m) ||' : '|| p_current_atp.atp_qty(m) );
3491          m := p_current_atp.atp_qty.Next(m);
3492        END LOOP;
3493 
3494        m := p_steal_atp.atp_period.FIRST;
3495        WHILE m is not null LOOP
3496             msc_sch_wb.atp_debug('Atp_Demand_Class_Consume: ' || 'p_steal_atp.atp_period and qty = '||
3497 	    p_steal_atp.atp_period(m) ||' : '|| p_steal_atp.atp_qty(m));
3498          m := p_steal_atp.atp_qty.Next(m);
3499        END LOOP;
3500     END IF;
3501 
3502     k := p_current_atp.atp_period.FIRST;
3503     -- i is the index for steal_atp
3504 
3505     FOR i in 1..p_steal_atp.atp_qty.COUNT LOOP
3506 
3507         -- consume current_atp (backward) if we have neg in steal_atp
3508         IF (p_steal_atp.atp_qty(i) < 0 ) THEN
3509 
3510             k := NVL(k, 1); --  if k is null, make it as 1 so that
3511                             --  we can find the starting point for the first
3512                             --  element.
3513 
3514             WHILE (k IS NOT NULL)  LOOP
3515              IF k = p_current_atp.atp_period.LAST THEN
3516                -- this is the last record
3517               IF (p_current_atp.atp_period(k) > p_steal_atp.atp_period(i)) THEN
3518                 -- cannot do any consumption since the date from p_steal_atp
3519                 -- is greater than p_ccurrent_atp
3520                 k := NULL;
3521               END IF;
3522               EXIT; -- exit the loop since this is the last record
3523 
3524              ELSE
3525                -- this is not the last record
3526               IF ((p_current_atp.atp_period(k) <= p_steal_atp.atp_period(i))
3527                  AND (p_current_atp.atp_period(k+1)>p_steal_atp.atp_period(i)))
3528               THEN
3529                  -- this is the starting point, we can exit now
3530 		 IF PG_DEBUG in ('Y', 'C') THEN
3531 		    msc_sch_wb.atp_debug('Atp_Demand_Class_Consume: ' || 'exit at k = ' ||to_char(k)||' and i = ' ||to_char(i));
3532 		 END IF;
3533                  EXIT;
3534               ELSE
3535                  k := p_current_atp.atp_period.NEXT(k);
3536               END IF;
3537              END IF;
3538             END LOOP;
3539 
3540             j:= k;
3541 
3542 	IF PG_DEBUG in ('Y', 'C') THEN
3543 	   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);
3544 	END IF;
3545 
3546             WHILE (NVL(j, -1) > 0) LOOP
3547 
3548                 -- 02/16: find the allowed stealing quantity
3549                 -- time_phased_atp
3550                 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
3551                     -- exit loop when crossing time fence
3552                     j := 0;
3553                 -- 02/16: changed the if
3554                 ELSIF (p_current_atp.atp_qty(j) <=0 ) THEN
3555                     --  backward one more period
3556                     j := j-1 ;
3557                 ELSE
3558                     IF (p_current_atp.atp_qty(j) + p_steal_atp.atp_qty(i)< 0) THEN
3559                         -- not enough to cover the shortage
3560                         p_steal_atp.atp_qty(i) := p_steal_atp.atp_qty(i) +
3561                                                   p_current_atp.atp_qty(j);
3562                         --- bug 1657855, remove support for min alloc
3563                         p_current_atp.atp_qty(j) := 0;
3564                         --p_current_atp.limit_qty(j) := 0;
3565                         j := j-1;
3566                     ELSE
3567                         -- enough to cover the shortage
3568 			-- Bug 1665096, index i is being used instead of j for p_current_atp
3569 			-- ngoel 3/2/2001
3570                         --p_current_atp.atp_qty(j) := p_current_atp.atp_qty(i) +
3571                         p_current_atp.atp_qty(j) := p_current_atp.atp_qty(j) +
3572                                                     p_steal_atp.atp_qty(i);
3573                         ---p_current_atp.limit_qty(j) :=
3574                         ---               p_current_atp.limit_qty(j)+
3575                         ---               p_steal_atp.atp_qty(i);
3576                         p_steal_atp.atp_qty(i) := 0;
3577                         j := -1;
3578                     END IF;
3579                 END IF;
3580             END LOOP;
3581         END IF;
3582     END LOOP;
3583 
3584 END Atp_Demand_Class_Consume;
3585 
3586 
3587 PROCEDURE Add_to_Next_Steal_Atp(
3588         p_current_atp      IN OUT  NOCOPY MRP_ATP_PVT.ATP_Info,
3589         p_next_steal_atp   IN OUT  NOCOPY MRP_ATP_PVT.ATP_Info)
3590 IS
3591 	i 			PLS_INTEGER; -- index for p_current_atp
3592 	j 			PLS_INTEGER; -- index for p_next_steal_atp
3593 	k 			PLS_INTEGER; -- index for l_next_steal_atp
3594 	n 			PLS_INTEGER; -- starting point of p_next_steal_atp
3595 	l_next_steal_atp  	MRP_ATP_PVT.ATP_Info; -- this will be the output
3596         l_processed             BOOLEAN := FALSE ;
3597 BEGIN
3598 
3599   -- this procedure will combine p_current_atp and p_next_steal_atp to form
3600   -- a new record of tables and then return as p_next_steal_atp.
3601   -- they need to be ordered by.
3602 
3603   j := p_next_steal_atp.atp_period.FIRST;
3604   k := 0;
3605   FOR i IN 1..p_current_atp.atp_period.COUNT LOOP
3606     -- we only worry about the neg quantity (that's why we need to steal
3607 
3608     IF PG_DEBUG in ('Y', 'C') THEN
3609        msc_sch_wb.atp_debug ('Add_to_Next_Steal_Atp: ' ||  'we are in i loop for current steal, i='||i);
3610     END IF;
3611     IF p_current_atp.atp_qty(i) < 0 THEN
3612        l_processed := FALSE; --1923405
3613        WHILE (j IS NOT NULL) LOOP
3614          IF PG_DEBUG in ('Y', 'C') THEN
3615             msc_sch_wb.atp_debug ('Add_to_Next_Steal_Atp: ' ||  'we are in j loop for next steal, j='||j);
3616          END IF;
3617          IF p_next_steal_atp.atp_qty(j) < 0 THEN
3618 
3619           k := k+1;
3620           l_next_steal_atp.atp_period.Extend;
3621           l_next_steal_atp.atp_qty.Extend;
3622 
3623           IF p_next_steal_atp.atp_period(j) < p_current_atp.atp_period(i) THEN
3624 
3625             -- we add this to l_next_steal_atp
3626             l_next_steal_atp.atp_period(k) := p_next_steal_atp.atp_period(j);
3627             l_next_steal_atp.atp_qty(k) := p_next_steal_atp.atp_qty(j);
3628 
3629           ELSIF p_next_steal_atp.atp_period(j)=p_current_atp.atp_period(i) THEN
3630 
3631             -- both record (p_next_steal_atp and p_current_atp) are on the same
3632             -- date.  we need to sum them up
3633             l_processed := TRUE; -- 1923405
3634             l_next_steal_atp.atp_period(k) := p_next_steal_atp.atp_period(j);
3635             l_next_steal_atp.atp_qty(k) := p_next_steal_atp.atp_qty(j) +
3636                                            p_current_atp.atp_qty(i);
3637             -- j := j+1;
3638             j := p_next_steal_atp.atp_period.NEXT(j);
3639             EXIT; -- exit the loop since we had done group by before. so
3640                   -- we don't need to go to next record any more
3641           ELSE -- this is the greater part
3642             l_processed := TRUE; -- 1923405
3643             l_next_steal_atp.atp_period(k) := p_current_atp.atp_period(i);
3644             l_next_steal_atp.atp_qty(k) := p_current_atp.atp_qty(i);
3645             EXIT; -- exit the loop since we had done group by before.
3646 
3647           END IF;
3648          END IF; -- p_next_steal_atp.atp_qty < 0
3649          j := p_next_steal_atp.atp_period.NEXT(j) ;
3650        END LOOP;
3651 
3652        -- 1923405: undo 1739629, add l_processed = FALSE condition
3653        IF (j is null) AND (l_processed = FALSE) THEN
3654          -- this means p_next_steal_atp is over,
3655          -- so we don't need to worry about p_next_steak_atp,
3656          -- we just keep add p_current_atp to l_next_steal_atp
3657          -- if they are not added before
3658          k := k+1;
3659          l_next_steal_atp.atp_period.Extend;
3660          l_next_steal_atp.atp_qty.Extend;
3661 
3662          l_next_steal_atp.atp_period(k) := p_current_atp.atp_period(i);
3663          l_next_steal_atp.atp_qty(k) := p_current_atp.atp_qty(i);
3664        END IF;
3665        -- AATP Forward Consumption rajjain begin
3666        -- After adding negatives from p_current_atp to p_next_steal_atp
3667        -- we update the the negatives in p_current_atp to zero.
3668        p_current_atp.atp_qty(i) := 0;
3669        -- AATP Forward Consumption rajjain end
3670     END IF; -- p_current_atp.atp_qty < 0
3671   END LOOP;
3672 
3673   -- now we have taken care of all p_current_atp and part of
3674   -- p_next_steal_atp. now we need to take care the rest of p_next_steal_atp
3675 
3676   -- FOR j IN n..p_next_steal_atp.atp_period.COUNT LOOP
3677   WHILE j is not null LOOP
3678          IF p_next_steal_atp.atp_qty(j) < 0 THEN
3679             -- we add this to l_next_steal_atp
3680             k := k+1;
3681             l_next_steal_atp.atp_period.Extend;
3682             l_next_steal_atp.atp_qty.Extend;
3683             l_next_steal_atp.atp_period(k) := p_next_steal_atp.atp_period(j);
3684             l_next_steal_atp.atp_qty(k) := p_next_steal_atp.atp_qty(j);
3685          END IF;
3686          j := p_next_steal_atp.atp_period.NEXT(j);
3687   END LOOP;
3688 
3689   p_next_steal_atp := l_next_steal_atp;
3690 
3691 END Add_to_Next_Steal_Atp;
3692 
3693 
3694 PROCEDURE Item_Alloc_Cum_Atp(
3695 	p_plan_id 	      IN NUMBER,
3696 	p_level               IN NUMBER,
3697 	p_identifier          IN NUMBER,
3698 	p_scenario_id         IN NUMBER,
3699 	p_inventory_item_id   IN NUMBER,
3700 	p_organization_id     IN NUMBER,
3701 	p_instance_id         IN NUMBER,
3702 	p_demand_class        IN VARCHAR2,
3703 	p_request_date        IN DATE,
3704 	p_insert_flag         IN NUMBER,
3705 	x_atp_info            OUT  NoCopy MRP_ATP_PVT.ATP_Info,
3706 	x_atp_period          OUT  NoCopy MRP_ATP_PUB.ATP_Period_Typ,
3707 	x_atp_supply_demand   OUT  NoCopy MRP_ATP_PUB.ATP_Supply_Demand_Typ,
3708         p_get_mat_in_rec      IN   MSC_ATP_REQ.get_mat_in_rec,
3709 	p_request_item_id     IN NUMBER, -- For time_phased_atp
3710 	p_atf_date            IN DATE)   -- For time_phased_atp
3711 IS
3712 	l_infinite_time_fence_date	DATE;
3713 	l_default_atp_rule_id           NUMBER;
3714 	l_calendar_exception_set_id     NUMBER;
3715         l_level_id                      NUMBER;
3716 	l_priority			NUMBER := 1;
3717 	l_allocation_percent		NUMBER := 100;
3718 	l_inv_item_id			NUMBER;
3719 	l_null_num  			NUMBER := null;
3720 	l_steal_period_quantity		NUMBER;
3721 	l_demand_class			VARCHAR2(80);
3722 	l_uom_code			VARCHAR2(3);
3723 	l_null_char    			VARCHAR2(3) := null;
3724 	l_return_status			VARCHAR2(1);
3725 	l_default_demand_class          VARCHAR2(34);
3726 	l_calendar_code                 VARCHAR2(14);
3727 	i				PLS_INTEGER;
3728 	mm				PLS_INTEGER;
3729 	ii                              PLS_INTEGER;
3730 	jj                              PLS_INTEGER;
3731 	j				PLS_INTEGER;
3732 	k				PLS_INTEGER;
3733 	l_demand_class_tab		MRP_ATP_PUB.char80_arr
3734        		                            := MRP_ATP_PUB.char80_arr();
3735 	l_demand_class_priority_tab	MRP_ATP_PUB.number_arr
3736        		                            := MRP_ATP_PUB.number_arr();
3737 	l_current_atp			MRP_ATP_PVT.ATP_Info;
3738 	l_next_steal_atp		MRP_ATP_PVT.ATP_Info;
3739 	l_current_steal_atp             MRP_ATP_PVT.ATP_Info;
3740         l_temp_atp                      MRP_ATP_PVT.ATP_Info;
3741 	l_null_steal_atp		MRP_ATP_PVT.ATP_Info;
3742 
3743         -- AATP Forward Consumption rajjain begin
3744 	l_unallocated_atp		MRP_ATP_PVT.ATP_Info;
3745 	l_lowest_priority_demand_class	VARCHAR2(80);
3746         l_lowest_priority		NUMBER;
3747 	l_fw_consume_tab		MRP_ATP_PUB.number_arr
3748        		                            := MRP_ATP_PUB.number_arr();
3749 	l_allocation_percent_tab	MRP_ATP_PUB.number_arr
3750        		                            := MRP_ATP_PUB.number_arr();
3751 	l_next_fw_consume		PLS_INTEGER := 0;
3752 	l_lowest_cust_priority		NUMBER;
3753 	l_lowest_site_priority		NUMBER;
3754 	-- AATP Forward Consumption rajjain end
3755 
3756         -- 1680719
3757         l_class_tab                     MRP_ATP_PUB.char30_arr
3758                                             := MRP_ATP_PUB.char30_arr();
3759         l_partner_tab                   MRP_ATP_PUB.number_arr
3760                                             := MRP_ATP_PUB.number_arr();
3761         l_class_next_steal_atp          MRP_ATP_PVT.ATP_Info;
3762         l_partner_next_steal_atp        MRP_ATP_PVT.ATP_Info;
3763         l_class_curr_steal_atp          MRP_ATP_PVT.ATP_Info;
3764         l_partner_curr_steal_atp        MRP_ATP_PVT.ATP_Info;
3765         l_pos1                            NUMBER;
3766         l_pos2                            NUMBER;
3767         delim     constant varchar2(1) := fnd_global.local_chr(13);
3768 
3769         -- krajan - 04/01/02 - Variable added for fsteal
3770         l_org_code                      VARCHAR2(7);
3771 
3772 	l_temp_atp_supply_demand        MRP_ATP_PUB.ATP_Supply_Demand_Typ;
3773 	l_sysdate 			DATE := trunc(sysdate);--4135752
3774 
3775 	-- time_phased_atp
3776 	l_time_phased_atp               VARCHAR2(1) := 'N';
3777 	l_pf_item_id                    NUMBER;
3778         l_item_to_use                   NUMBER;
3779 
3780 
3781 BEGIN
3782 
3783   IF PG_DEBUG in ('Y', 'C') THEN
3784      msc_sch_wb.atp_debug('******* Item_Alloc_Cum_Atp *******');
3785      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'p_plan_id =' || p_plan_id );
3786      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'p_level =' || p_level );
3787      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'p_identifier =' || p_identifier);
3788      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'p_scenario_id =' || p_scenario_id);
3789      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'p_instance_id =' || p_instance_id);
3790      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'p_inventory_item_id =' || p_inventory_item_id);
3791      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'p_request_item_id =' || p_request_item_id);
3792      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'p_organization_id =' || p_organization_id);
3793      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'p_demand_class =' || p_demand_class);
3794      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'p_request_date =' || p_request_date );
3795      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'p_insert_flag =' || p_insert_flag );
3796      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'p_atf_date =' || p_atf_date );
3797      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
3798   END IF;
3799 
3800 
3801   /*-----------------------------------------------------------------------------------------+
3802   | rajjain begin 10/08/2002                                                                 |
3803   |                                                                                          |
3804   | Logic:                                                                                   |
3805   |                                                                                          |
3806   | Step 1: Find all the demand classes in the hierarchy                                     |
3807   |                                                                                          |
3808   | Step 2: If "MSC:Allocated ATP Forward Consumption Method" profile is set to              |
3809   |         "Reduce future supply from lowest priority", store the type of forward           |
3810   |         consumption in a PL/SQL table                                                    |
3811   |                                                                                          |
3812   | Step 3: If "MSC:Allocated ATP Forward Consumption Method" profile is set to              |
3813   |         "Reduce available supply from any priority", calculate unallocated availability  |
3814   |         Do backward consumption                                                          |
3815   |         Do forward consumption, do accumulation                                          |
3816   |                                                                                          |
3817   | Step 4: For each demand class DCi ->                                                     |
3818   |         1. get the daily net availability                                                |
3819   |         2. do backward consumption for DCi                                               |
3820   |         3. do demand class consumption if DC1 to DC(i-1) has any negative bucket         |
3821   |         4. If "MSC:Allocated ATP Forward Consumption Method" profile is set to           |
3822   |            a) "Reduce future supply from lowest priority" (Method 1)                     |
3823   |               Do the type of forward consumption we stored in the PL/SQL table           |
3824   |               Do accumulation                                                            |
3825   |            b) "Reduce available supply from any priority" (Method 2)                     |
3826   |               Do accumulation                                                            |
3827   |               Use the unallocated cum and calculated the adjusted cum                    |
3828   |                                                                                          |
3829   | Step 5: Exit from the l_demand_class_tab loop if l_demand_class is the requested DC      |
3830   |                                                                                          |
3831   +-----------------------------------------------------------------------------------------*/
3832 
3833  -- time_phased_atp
3834  IF (p_inventory_item_id <> p_request_item_id and p_atf_date is not null) THEN
3835         l_time_phased_atp := 'Y';
3836         l_pf_item_id := MSC_ATP_PVT.G_ITEM_INFO_REC.product_family_id;
3837  END IF;
3838 
3839  -- rajjain 01/29/2003 begin Bug 2737596
3840  IF p_identifier = -1 THEN
3841     BEGIN
3842        SELECT inventory_item_id, uom_code
3843        INTO   l_inv_item_id, l_uom_code
3844        FROM   msc_system_items
3845        WHERE  plan_id = p_plan_id
3846        AND    sr_instance_id = p_instance_id
3847        AND    organization_id = p_organization_id
3848        AND    sr_inventory_item_id = p_inventory_item_id;
3849     EXCEPTION
3850        WHEN OTHERS THEN
3851           IF PG_DEBUG in ('Y', 'C') THEN
3852              msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error selecting uom_code for the item');
3853           END IF;
3854     END;
3855  ELSE
3856     -- we need not select this in case of ATP QUERY as this information is available in G_ITEM_INFO_REC
3857     l_inv_item_id := MSC_ATP_PVT.G_ITEM_INFO_REC.inventory_item_id;
3858     l_uom_code := MSC_ATP_PVT.G_ITEM_INFO_REC.uom_code;
3859  END IF;
3860  -- rajjain 01/29/2003 end Bug 2737596
3861 
3862  /* New allocation logic for time_phased_atp changes begin
3863     For time phased ATP scenarios if allocation rule at member item is not defined then within ATF use
3864     allocation rule defined at family */
3865  IF l_time_phased_atp = 'Y' THEN
3866      IF p_request_date <= p_atf_date THEN
3867          IF MSC_ATP_PVT.G_MEM_RULE_WITHIN_ATF = 'Y' THEN
3868              l_item_to_use := l_inv_item_id;
3869          ELSE
3870              l_item_to_use := l_pf_item_id;
3871          END IF;
3872      ELSE
3873          l_item_to_use := l_pf_item_id;
3874      END IF;
3875  ELSE
3876      l_item_to_use := l_inv_item_id;
3877  END IF;
3878 
3879  IF PG_DEBUG in ('Y', 'C') THEN
3880     msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Item to be used = ' || l_item_to_use);
3881  END IF;
3882  /* New allocation logic for time_phased_atp changes end */
3883 
3884  -- select the priority  and allocation_percent for that item/demand class.
3885  -- if no data found, check if this item has a valid allocation rule.
3886  -- otherwise return error.
3887  IF p_scenario_id <> -1 THEN
3888     MSC_AATP_PVT.Get_DC_Info(p_instance_id, l_item_to_use, p_organization_id, null, null,
3889      p_demand_class, p_request_date, l_level_id, l_priority, l_allocation_percent, l_return_status);
3890 
3891     IF  l_return_status = FND_API.G_RET_STS_ERROR THEN
3892         IF PG_DEBUG in ('Y', 'C') THEN
3893            msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error retrieving Priority and Demand Class');
3894         END IF;
3895     END IF;
3896  ELSE
3897      l_priority := -1;
3898      l_allocation_percent := NULL;
3899  END IF;
3900 
3901   -- find the demand classes that have priority higher (small number) than
3902   -- the requested demand class
3903 
3904   IF PG_DEBUG in ('Y', 'C') THEN
3905      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_allocation_percent = '||l_allocation_percent);
3906   END IF;
3907 
3908  /* rajjain 01/29/2003 Bug 2737596
3909     We don't need to select all demand classes in case this procdure is
3910     called from MSC_AATP_PVT.VIEW_ALLOCATION for view total*/
3911  IF p_identifier <> -1 or (p_identifier = -1 and p_scenario_id <> -1) THEN
3912 
3913   IF PG_DEBUG in ('Y', 'C') THEN
3914      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'before select the high priority demand class');
3915   END IF;
3916 
3917   /*AATP Forward Consumption rajjain begin
3918   1. Now we select all the demand classes in l_demand_class_tab irrespective of demand class for which
3919      Inquiry has come.
3920   2. l_allocation_percent_tab stores the allocation_percent for the demand classes.
3921   3. If inquiry is at level 2 or 3 we also populate l_fw_consume_tab which stores the type
3922      of forward consumption. We bulk collect 0 in l_fw_consume_tab to initialise it.
3923 
3924   Note:
3925   1. We order by allocation_percent in demand class scenario if we have multiple
3926      demand classes at same priority.
3927   2. We are not doing this for customer class scenario as it may impact performance
3928      negatively due to extra joins.*/
3929 
3930   /* time_phased_atp changes begin */
3931   -- bug 1680719
3932   IF l_level_id = -1 THEN
3933 
3934     IF PG_DEBUG in ('Y', 'C') THEN
3935        msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_level_id = '||l_level_id);
3936     END IF;
3937     SELECT demand_class, priority, allocation_percent
3938     BULK COLLECT INTO l_demand_class_tab, l_demand_class_priority_tab, l_allocation_percent_tab
3939     FROM   msc_item_hierarchy_mv
3940     WHERE  inventory_item_id = l_item_to_use /* New allocation logic for time_phased_atp changes*/
3941     AND	   organization_id = p_organization_id
3942     AND    sr_instance_id = p_instance_id
3943     AND    p_request_date BETWEEN effective_date AND disable_date
3944     AND    level_id = l_level_id
3945     --rajjain added demand_class asc
3946     ORDER BY priority asc, allocation_percent desc, demand_class asc;
3947 
3948   ELSIF l_level_id = 1 THEN
3949 
3950     IF PG_DEBUG in ('Y', 'C') THEN
3951        msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_level_id = '||l_level_id);
3952     END IF;
3953     SELECT demand_class, priority, allocation_percent
3954     BULK COLLECT INTO l_demand_class_tab, l_demand_class_priority_tab, l_allocation_percent_tab
3955     FROM   msc_item_hierarchy_mv
3956     WHERE  inventory_item_id = l_item_to_use /* New allocation logic for time_phased_atp changes*/
3957     AND    organization_id = p_organization_id
3958     AND    sr_instance_id = p_instance_id
3959     AND    p_request_date BETWEEN effective_date AND disable_date
3960     AND    level_id = l_level_id
3961     ORDER BY priority asc, class asc;
3962 
3963   ELSIF l_level_id = 2 THEN
3964 
3965     IF PG_DEBUG in ('Y', 'C') THEN
3966        msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_level_id = '||l_level_id);
3967     END IF;
3968     SELECT mv1.demand_class, mv1.priority, mv1.class, mv1.partner_id, 0, mv1.allocation_percent
3969     BULK COLLECT INTO l_demand_class_tab, l_demand_class_priority_tab,
3970                       l_class_tab, l_partner_tab, l_fw_consume_tab, l_allocation_percent_tab
3971     FROM   msc_item_hierarchy_mv mv1
3972     WHERE  mv1.inventory_item_id = l_item_to_use /* New allocation logic for time_phased_atp changes*/
3973     AND    mv1.organization_id = p_organization_id
3974     AND    mv1.sr_instance_id = p_instance_id
3975     AND    p_request_date BETWEEN mv1.effective_date AND mv1.disable_date
3976     AND    mv1.level_id = l_level_id
3977     ORDER BY trunc(mv1.priority, -3), mv1.class ,
3978              trunc(mv1.priority, -2), mv1.partner_id;
3979 
3980 
3981   ELSIF l_level_id = 3 THEN
3982 
3983     -- bug 1680719
3984     -- we need to select the class, partner_id, partner_site_id
3985 
3986     IF PG_DEBUG in ('Y', 'C') THEN
3987        msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_level_id = '||l_level_id);
3988     END IF;
3989     SELECT mv1.demand_class, mv1.priority, mv1.class, mv1.partner_id, 0, mv1.allocation_percent
3990     BULK COLLECT INTO l_demand_class_tab, l_demand_class_priority_tab,
3991                       l_class_tab, l_partner_tab, l_fw_consume_tab, l_allocation_percent_tab
3992     FROM   msc_item_hierarchy_mv mv1
3993     WHERE  mv1.inventory_item_id = l_item_to_use /* New allocation logic for time_phased_atp changes*/
3994     AND    mv1.organization_id = p_organization_id
3995     AND    mv1.sr_instance_id = p_instance_id
3996     AND    p_request_date BETWEEN mv1.effective_date AND mv1.disable_date
3997     AND    mv1.level_id = l_level_id
3998     ORDER BY trunc(mv1.priority, -3), mv1.class ,
3999              trunc(mv1.priority, -2), mv1.partner_id,
4000              mv1.priority, mv1.partner_site_id;
4001 
4002   END IF;
4003   -- AATP Forward Consumption rajjain end
4004   /* time_phased_atp changes end */
4005 
4006   mm := l_demand_class_tab.FIRST;
4007   IF PG_DEBUG in ('Y', 'C') THEN
4008      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_demand_class_tab.count = '||
4009                         l_demand_class_tab.count);
4010      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_demand_class_priority_tab.count = '||
4011                         l_demand_class_priority_tab.count);
4012      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_allocation_percent_tab.count = '||
4013                         l_allocation_percent_tab.count);
4014      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_class_tab.count = '||
4015                         l_class_tab.count);
4016      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_partner_tab.count = '||
4017                         l_partner_tab.count);
4018   END IF;
4019 
4020  END IF;
4021 
4022  -- Need to add the requested demand class in case call is from
4023  -- View_Allocation for View Total, else l_demand_class_tab remains empty.
4024  IF l_demand_class_tab.count = 0 THEN
4025 --/* 1665110
4026   -- add the request demand class into the list
4027   l_demand_class_tab.Extend;
4028   l_demand_class_priority_tab.Extend;
4029   -- krajan: 2745212
4030   l_allocation_percent_tab.Extend;
4031 
4032   i := l_demand_class_tab.COUNT;
4033   l_demand_class_priority_tab(i) := l_priority;
4034   l_demand_class_tab(i) := p_demand_class;
4035   -- 2745212
4036   l_allocation_percent_tab(i) := 100;
4037 
4038   -- 1680719
4039   IF l_level_id in (2, 3) THEN
4040       l_class_tab.Extend;
4041       l_partner_tab.Extend;
4042       -- krajan : 2745212
4043       l_fw_consume_tab.Extend;
4044 
4045       l_pos1 := instr(p_demand_class,delim,1,1);
4046       l_pos2 := instr(p_demand_class,delim,1,2);
4047       l_class_tab(i) := substr(p_demand_class,1,l_pos1-1);
4048       IF l_pos2 = 0 THEN
4049         l_partner_tab(i) := substr(p_demand_class,l_pos1+1);
4050       ELSE
4051         l_partner_tab(i) := substr(p_demand_class,l_pos1+1,l_pos2-l_pos1-1) ;
4052       END IF;
4053   END IF;
4054 --1665110 */
4055  END IF;
4056 
4057   mm := l_demand_class_tab.FIRST;
4058   IF PG_DEBUG in ('Y', 'C') THEN
4059      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_demand_class_tab.count = '|| l_demand_class_tab.count);
4060   END IF;
4061 
4062   /*AATP Forward Consumption rajjain begin
4063   1. Level_id in (-1, 1) ->
4064      a. We calculate the l_lowest_priority and l_lowest_priority_demand_class.
4065      b. l_lowest_priority_demand_class is the lowest demand class with non zero
4066         allocation percentage.
4067      c. l_lowest_priority is the priority of l_lowest_priority_demand_class.
4068   2. Level_id in (2,3) ->
4069      a. We form the l_fw_consume_tab. This takes values from 0-4 which depends on
4070         type of forward consumption we do for this demand class.*/
4071   mm := l_demand_class_tab.LAST;
4072   -- we go bottom up in l_demand_class_tab
4073   IF l_level_id in (-1, 1) THEN
4074 
4075      WHILE mm is not null LOOP
4076 
4077      msc_sch_wb.atp_debug('---------------------l_demand_class_tab'||l_demand_class_tab(mm));--6359986
4078      IF (l_demand_class_tab(mm) ='-1') then							--6359986
4079       IF(MSC_ATP_PVT.G_ZERO_ALLOCATION_PERC = 'N') THEN	--6359986
4080       IF l_allocation_percent_tab(mm) <> 0 THEN
4081 	  l_lowest_priority_demand_class := l_demand_class_tab(mm);
4082 	  l_lowest_priority := l_demand_class_priority_tab(mm);
4083 	  IF PG_DEBUG in ('Y', 'C') THEN
4084 	     msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_lowest_priority_demand_class: ' ||
4085 	                        l_lowest_priority_demand_class);
4086 	     msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_lowest_priority: ' ||
4087 	                        l_lowest_priority);
4088 	  END IF;
4089 	  EXIT;
4090       END IF;
4091       ELSE			--6359986 start
4092           l_lowest_priority_demand_class := l_demand_class_tab(mm);
4093 	  			l_lowest_priority := l_demand_class_priority_tab(mm);
4094 	  			IF PG_DEBUG in ('Y', 'C') THEN
4095 	     			msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_lowest_priority_demand_class: ' ||
4096 	                        l_lowest_priority_demand_class);
4097 	     			msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_lowest_priority: ' ||
4098 	                        l_lowest_priority);
4099 	  			END IF;
4100 	  			EXIT;
4101       END IF;
4102 
4103      ELSE
4104           l_lowest_priority_demand_class := l_demand_class_tab(mm);
4105 	  			l_lowest_priority := l_demand_class_priority_tab(mm);
4106 	  			IF PG_DEBUG in ('Y', 'C') THEN
4107 	     			msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_lowest_priority_demand_class: ' ||
4108 	                        l_lowest_priority_demand_class);
4109 	     			msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_lowest_priority: ' ||
4110 	                        l_lowest_priority);
4111 	  			END IF;
4112 	  			EXIT;
4113      END IF;	--6359986 end
4114       mm := l_demand_class_tab.Prior(mm);
4115      END LOOP;
4116   ELSIF l_level_id in (2, 3) THEN
4117     WHILE mm is not null LOOP
4118      IF (REPLACE(l_demand_class_tab(mm),FND_GLOBAL.LOCAL_CHR(13),' ') in ('-1','-1 -1 -1','-1 -1')) then --6359986
4119      IF(MSC_ATP_PVT.G_ZERO_ALLOCATION_PERC = 'N') THEN ----6359986
4120      IF l_allocation_percent_tab(mm) <> 0 THEN
4121         -- this is the lowest demand class with non zero allocation percentage
4122         l_fw_consume_tab(mm) := 4;
4123 				l_lowest_priority_demand_class := l_demand_class_tab(mm);
4124 				--5634348
4125 	      l_lowest_priority := trunc(l_demand_class_priority_tab(mm), -3);
4126         -- krajan : 2745212
4127         mm := l_demand_class_tab.Prior(mm);
4128 
4129 	exit;
4130      ELSE
4131         -- this is demand class with zero allocation percentage
4132         -- this dc needs to do forward consumption for its own negatives
4133         l_fw_consume_tab(mm) := 1;
4134      END IF;
4135      ELSE		--6359986 start
4136         -- this is the lowest demand class with non zero allocation percentage
4137         l_lowest_priority_demand_class := l_demand_class_tab(mm);
4138 	      --5634348
4139 	      l_lowest_priority := trunc(l_demand_class_priority_tab(mm), -3);
4140         -- krajan : 2745212
4141 
4142         l_fw_consume_tab(mm) := 4;
4143         mm := l_demand_class_tab.Prior(mm);
4144         exit;
4145      END IF;
4146     ELSE
4147        -- this is the lowest demand class with non zero allocation percentage
4148         l_lowest_priority_demand_class := l_demand_class_tab(mm);
4149 	      --5634348
4150 	      l_lowest_priority := trunc(l_demand_class_priority_tab(mm), -3);
4151         -- krajan : 2745212
4152         l_fw_consume_tab(mm) := 4;
4153         mm := l_demand_class_tab.Prior(mm);
4154         exit;
4155     END IF; --6359986 end
4156       msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'mm: '||mm);
4157       mm := l_demand_class_tab.Prior(mm);
4158     END LOOP;
4159     --l_lowest_priority := trunc(l_demand_class_priority_tab(mm), -3);	--5634348, commenting it
4160 
4161     WHILE mm is not null LOOP
4162      IF l_next_fw_consume <> 0 THEN
4163         IF l_allocation_percent_tab(mm) <> 0 THEN
4164             IF l_next_fw_consume = 2 THEN
4165               -- this is the lowest priority dc with non zero allocation % at current customer level
4166               -- set the lowest site priority
4167               l_lowest_site_priority := l_demand_class_priority_tab(mm);
4168             ELSIF l_next_fw_consume = 3 THEN
4169               -- this is the lowest priority customer with non zero allocation % at current
4170               -- customer class level. set the lowest customer and site priority
4171               l_lowest_cust_priority := trunc(l_demand_class_priority_tab(mm), -2);
4172               l_lowest_site_priority := l_demand_class_priority_tab(mm);
4173             END IF;
4174             l_fw_consume_tab(mm) := l_next_fw_consume;
4175             l_next_fw_consume := 0; -- reset l_next_fw_consume to zero
4176         ELSE
4177             -- dc with zero allocation %
4178             l_fw_consume_tab(mm) := 1;
4179         END IF;
4180      ELSIF l_class_tab(mm) <> l_class_tab(mm+1) THEN
4181       --customer class changed
4182       IF trunc(l_demand_class_priority_tab(mm), -3)<>trunc(l_demand_class_priority_tab(mm+1), -3) THEN
4183          --customer class priority changed
4184          exit;
4185       ELSE
4186          --reset the lowest customer and site priority at this level to null
4187          l_lowest_cust_priority := null;
4188          l_lowest_site_priority := null;
4189          IF l_allocation_percent_tab(mm) = 0 THEN
4190             -- allocation % zero set l_next_fw_consume
4191             l_next_fw_consume := 3;
4192             l_fw_consume_tab(mm) := 1;
4193          ELSE
4194             l_fw_consume_tab(mm) := 3;
4195             l_lowest_cust_priority := trunc(l_demand_class_priority_tab(mm), -2);
4196             l_lowest_site_priority := l_demand_class_priority_tab(mm);
4197          END IF;
4198       END IF;
4199      ELSIF l_partner_tab(mm) <> l_partner_tab(mm+1) THEN
4200        --customer changed
4201        --reset the lowest site priority at this level to null
4202        l_lowest_site_priority := null;
4203        IF trunc(l_demand_class_priority_tab(mm), -2) = trunc(l_demand_class_priority_tab(mm+1), -2)
4204           AND (l_lowest_cust_priority is null
4205                OR trunc(l_demand_class_priority_tab(mm), -2) = l_lowest_cust_priority) THEN
4206          --customer priority same
4207          IF l_allocation_percent_tab(mm) = 0 THEN
4208             -- allocation % zero set l_next_fw_consume
4209             l_next_fw_consume := 2;
4210             l_fw_consume_tab(mm) := 1;
4211          ELSE
4212             l_fw_consume_tab(mm) := 2;
4213             l_lowest_site_priority := l_demand_class_priority_tab(mm);
4214          END IF;
4215        END IF;
4216      ELSIF l_demand_class_priority_tab(mm) = l_demand_class_priority_tab(mm+1)
4217            AND (l_lowest_site_priority is null OR l_demand_class_priority_tab(mm) = l_lowest_site_priority) THEN
4218        --site priority same
4219        l_fw_consume_tab(mm) := 1;
4220      END IF;
4221 
4222      mm := l_demand_class_tab.Prior(mm);
4223 
4224     END LOOP;
4225   END IF;
4226   --bug3948494  For demand_class cases, if request comes for highest priority
4227   -- and l_lowest_priority is > 1 then process only requested demand class
4228   IF l_level_id = -1 and l_priority = 1 and l_lowest_priority > 1 THEN
4229 
4230     l_demand_class_tab.Delete;
4231     l_demand_class_priority_tab.Delete;
4232     l_allocation_percent_tab.Delete;
4233 
4234     l_demand_class_tab.Extend;
4235     l_demand_class_priority_tab.Extend;
4236     l_allocation_percent_tab.Extend;
4237 
4238     i := l_demand_class_tab.COUNT;
4239     l_demand_class_priority_tab(i) := l_priority;
4240     l_demand_class_tab(i) := p_demand_class;
4241     l_allocation_percent_tab(i) := l_allocation_percent;
4242 
4243     IF PG_DEBUG in ('Y', 'C') THEN
4244       msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_demand_class_tab.count = '||
4245                         l_demand_class_tab.count);
4246       msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_demand_class_priority_tab.count = '||
4247                         l_demand_class_priority_tab.count);
4248       msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_allocation_percent_tab.count = '||
4249                         l_allocation_percent_tab.count);
4250     END IF;
4251 
4252   END IF;
4253 
4254   mm := l_fw_consume_tab.FIRST;
4255   IF PG_DEBUG in ('Y', 'C') THEN
4256      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_fw_consume_tab.count = '|| l_fw_consume_tab.count);
4257      WHILE mm is not null LOOP
4258         msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_fw_consume_tab = '||
4259            l_fw_consume_tab(mm));
4260 
4261         -- krajan : 2745212
4262         mm := l_fw_consume_tab.Next(mm);
4263 
4264      END LOOP;
4265   END IF;
4266   -- AATP Forward Consumption rajjain end 10/21/2002
4267 
4268   -- for performance reason, we need to get the following info and
4269   -- store in variables instead of joining it
4270 
4271   -- krajan: 04/01/02 Added l_org_code to call.
4272   /* Modularize Item and Org Info */
4273   -- changed Call
4274   MSC_ATP_PROC.get_global_org_info(p_instance_id, p_organization_id);
4275   l_default_atp_rule_id := MSC_ATP_PVT.G_ORG_INFO_REC.default_atp_rule_id;
4276   l_calendar_code := MSC_ATP_PVT.G_ORG_INFO_REC.cal_code;
4277   l_calendar_exception_set_id := MSC_ATP_PVT.G_ORG_INFO_REC.cal_exception_set_id;
4278   l_default_demand_class := MSC_ATP_PVT.G_ORG_INFO_REC.default_demand_class;
4279   l_org_code := MSC_ATP_PVT.G_ORG_INFO_REC.org_code;
4280   /* Modularize Item and Org Info */
4281 
4282   IF PG_DEBUG in ('Y', 'C') THEN
4283      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_default_atp_rule_id='|| l_default_atp_rule_id);
4284      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_calendar_code='||l_calendar_code);
4285      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_calendar_exception_set_id'|| l_calendar_exception_set_id);
4286      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_default_demand_class'|| l_default_demand_class);
4287      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_org_code'|| l_org_code);
4288   END IF;
4289   --diag_atp
4290   -- rajjain 01/29/2003 Bug 2737596
4291   -- Need to call get_infinite_time_fence_date procedure in case call is from View allocation
4292   -- as p_get_mat_in_rec will not be populated
4293   IF p_identifier = -1 THEN
4294      -- get the infinite time fence date if it exists
4295      l_infinite_time_fence_date := MSC_ATP_FUNC.get_infinite_time_fence_date(p_instance_id,
4296              p_inventory_item_id,p_organization_id, p_plan_id);
4297   ELSE
4298      ---diag_atp
4299      l_infinite_time_fence_date := p_get_mat_in_rec.infinite_time_fence_date;
4300   END IF;
4301   IF PG_DEBUG in ('Y', 'C') THEN
4302      msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_infinite_time_fence_date'|| l_infinite_time_fence_date);
4303   END IF;
4304 
4305   -- Now go demand class by demand class and calculate the allocated picture
4306   FOR i in 1..l_demand_class_tab.COUNT LOOP
4307 
4308           l_demand_class := l_demand_class_tab(i);
4309           IF PG_DEBUG in ('Y', 'C') THEN
4310              msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'in i loop, i = '||i);
4311              msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_demand_class = '||l_demand_class);
4312           END IF;
4313     -- get the daily net availability for DCi
4314     IF (NVL(p_insert_flag, 0) = 0  OR l_demand_class <> p_demand_class) THEN
4315        -- we don't want details
4316        IF PG_DEBUG in ('Y', 'C') THEN
4317           msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' ||
4318                              'Inside IF (NVL(p_insert_flag, 0) = 0');
4319        END IF;
4320 
4321        /* AATP Forward Consumption rajjain
4322        We calculate unallocated availability alongwith allocated availability calculation
4323        for first demand class if Forward consumption method is method2*/
4324        IF i=1 AND G_ATP_FW_CONSUME_METHOD = 2 THEN
4325           IF PG_DEBUG in ('Y', 'C') THEN
4326              msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' ||
4327                                 'Inside IF i=1 AND Method2');
4328           END IF;
4329           -- time_phased_atp
4330           IF l_time_phased_atp='N' THEN
4331                   -- 2859130
4332                   item_alloc_avail_unalloc(
4333                      p_get_mat_in_rec.optimized_plan,
4334                      l_inv_item_id,
4335                      p_organization_id,
4336                      p_instance_id,
4337                      p_plan_id,
4338                      l_demand_class,
4339                      --l_level_id,  bug 12973673
4340                      NULL,
4341                      l_infinite_time_fence_date,
4342                      l_calendar_code,
4343                      l_calendar_exception_set_id,
4344                      p_get_mat_in_rec.sys_next_osc_date,			--bug3099066 bug3333114
4345                      l_current_atp.atp_period,
4346                      l_current_atp.atp_qty,
4347                      l_unallocated_atp.atp_qty
4348                   );
4349           ELSE
4350                   MSC_ATP_PF.Item_Alloc_Avail_Pf_Unalloc(
4351                      l_inv_item_id,
4352                      l_pf_item_id,
4353                      p_organization_id,
4354                      p_instance_id,
4355                      p_plan_id,
4356                      l_demand_class,
4357                      --l_level_id,  bug 12973673
4358                      NULL,
4359                      l_infinite_time_fence_date,
4360                      p_get_mat_in_rec.sys_next_osc_date,			--bug3099066 bug3333114
4361                      p_atf_date,
4362                      l_current_atp.atp_period,
4363                      l_current_atp.atp_qty,
4364                      l_unallocated_atp.atp_qty,
4365                      l_return_status
4366                   );
4367                   IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4368                         IF PG_DEBUG in ('Y', 'C') THEN
4369                                 msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error occured in procedure Item_Alloc_Avail_Pf_Unalloc');
4370                         END IF;
4371                         RAISE FND_API.G_EXC_ERROR;
4372                   END IF;
4373           END IF;
4374 
4375         IF PG_DEBUG in ('Y', 'C') THEN
4376            msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Before Atp_Consume for l_unallocated_atp');
4377 	   mm := l_unallocated_atp.atp_qty.FIRST;
4378 	   while mm is not null loop
4379 		msc_sch_wb.atp_debug('l_unallocated_atp.atp_period:atp_qty = ' ||
4380 		            l_current_atp.atp_period(mm) || ':' || l_unallocated_atp.atp_qty(mm));
4381 		mm := l_unallocated_atp.atp_qty.NEXT(mm);
4382 	   end loop;
4383         END IF;
4384 
4385         -- after calculating the net unallocated availability do b/w+f/w consumption and accumulation
4386         -- time_phased_atp
4387         IF l_time_phased_atp = 'Y' THEN
4388             MSC_ATP_PF.pf_atp_consume(
4389                    l_unallocated_atp.atp_qty,
4390                    l_return_status,
4391                    l_current_atp.atp_period,
4392                    MSC_ATP_PF.Bw_Fw_Cum, --b/w, f/w consumption and accumulation
4393                    p_atf_date);
4394             IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4395                 IF PG_DEBUG in ('Y', 'C') THEN
4396                         msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error occured in procedure Pf_Atp_Consume');
4397                 END IF;
4398                 RAISE FND_API.G_EXC_ERROR;
4399             END IF;
4400         ELSE
4401             MSC_ATP_PROC.Atp_Consume(l_unallocated_atp.atp_qty, l_unallocated_atp.atp_qty.COUNT);
4402         END IF;
4403 
4404         IF PG_DEBUG in ('Y', 'C') THEN
4405            msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'After Atp_Consume for l_unallocated_atp');
4406 	   mm := l_unallocated_atp.atp_qty.FIRST;
4407 	   while mm is not null loop
4408 		msc_sch_wb.atp_debug('l_unallocated_atp.atp_period:atp_qty = ' ||
4409 		            l_current_atp.atp_period(mm) || ':' || l_unallocated_atp.atp_qty(mm));
4410 		mm := l_unallocated_atp.atp_qty.NEXT(mm);
4411 	   end loop;
4412         END IF;
4413 
4414        ELSE
4415           IF PG_DEBUG in ('Y', 'C') THEN
4416              msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' ||
4417                 'Inside ELSE of IF i=1 AND G_ATP_FW_CONSUME_METHOD = 2 THEN');
4418           END IF;
4419           -- time_phased_atp
4420           IF l_time_phased_atp='N' THEN
4421                   item_alloc_avail(
4422                      p_get_mat_in_rec.optimized_plan,
4423                      l_inv_item_id,
4424                      p_organization_id,
4425                      p_instance_id,
4426                      p_plan_id,
4427                      l_demand_class,
4428                      --l_level_id,  bug 12973673
4429                      NULL,
4430                      l_infinite_time_fence_date,
4431                      l_calendar_code,
4432                      l_calendar_exception_set_id,
4433                      p_get_mat_in_rec.sys_next_osc_date,			--bug3099066 bug3333114
4434                      l_current_atp.atp_period,
4435                      l_current_atp.atp_qty
4436                   );
4437           ELSE
4438                   MSC_ATP_PF.Item_Alloc_Avail_Pf(
4439                      l_inv_item_id,
4440                      l_pf_item_id,
4441                      p_organization_id,
4442                      p_instance_id,
4443                      p_plan_id,
4444                      l_demand_class,
4445                      --l_level_id,  bug 12973673
4446                      NULL,
4447                      l_infinite_time_fence_date,
4448                      p_get_mat_in_rec.sys_next_osc_date,			--bug3099066 bug3333114
4449                      p_atf_date,
4450                      l_current_atp.atp_period,
4451                      l_current_atp.atp_qty,
4452                      l_return_status
4453                   );
4454                   IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4455                         IF PG_DEBUG in ('Y', 'C') THEN
4456                                 msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error occured in procedure Item_Alloc_Avail_Pf');
4457                         END IF;
4458                         RAISE FND_API.G_EXC_ERROR;
4459                   END IF;
4460           END IF;
4461        END IF;
4462 
4463        IF PG_DEBUG in ('Y', 'C') THEN
4464            msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: After calculating daily net availability');
4465            Print_Period_Qty('l_current_atp.atp_period:atp_qty = ', l_current_atp);
4466        END IF;
4467 
4468     ELSE
4469         -- IF (NVL(p_insert_flag, 0) <> 0  AND l_demand_class = p_demand_class)
4470         -- OR p_scenario_id = -1
4471         IF PG_DEBUG in ('Y', 'C') THEN
4472            msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' ||
4473                  'Inside ELSE of IF (NVL(p_insert_flag, 0)=0 OR l_demand_class<>p_demand_class)');
4474         END IF;
4475         MSC_ATP_DB_UTILS.Clear_SD_Details_Temp();
4476 
4477         -- time_phased_atp
4478         IF l_time_phased_atp='N' THEN
4479                 item_alloc_avail_dtls(
4480                    p_get_mat_in_rec.optimized_plan,
4481                    l_inv_item_id,
4482                    p_organization_id,
4483                    p_instance_id,
4484                    p_plan_id,
4485                    l_demand_class,
4486                    --l_level_id,  bug 12973673
4487                    NULL,
4488                    l_infinite_time_fence_date,
4489                    l_calendar_code,
4490                    l_calendar_exception_set_id,
4491                    p_inventory_item_id,
4492                    p_level,
4493                    p_identifier,
4494                    p_scenario_id,
4495                    l_uom_code,
4496                    p_get_mat_in_rec.sys_next_osc_date			--bug3099066 bug3333114
4497                 );
4498         ELSE
4499                 MSC_ATP_PF.Item_Alloc_Avail_Pf_Dtls(
4500                    l_inv_item_id,
4501                    l_pf_item_id,
4502                    p_request_item_id,
4503                    p_inventory_item_id,
4504                    p_organization_id,
4505                    p_instance_id,
4506                    p_plan_id,
4507                    l_demand_class,
4508                    --l_level_id,  bug 12973673
4509                    NULL,
4510                    l_infinite_time_fence_date,
4511                    p_level,
4512                    p_identifier,
4513                    p_scenario_id,
4514                    l_uom_code,
4515                    p_get_mat_in_rec.sys_next_osc_date,			--bug3099066 bug3333114
4516                    p_atf_date,
4517                    l_return_status
4518                 );
4519                 IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4520                         IF PG_DEBUG in ('Y', 'C') THEN
4521                                 msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error occured in procedure Item_Alloc_Avail_Pf_Dtls');
4522                         END IF;
4523                         RAISE FND_API.G_EXC_ERROR;
4524                 END IF;
4525         END IF;
4526 
4527      -- for period ATP
4528      IF PG_DEBUG in ('Y', 'C') THEN
4529         msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'after insert into msc_atp_sd_details_temp');
4530         msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'num rows : ' || SQL%ROWCOUNT);
4531      END IF;
4532 
4533      /*AATP Forward Consumption rajjain begin
4534      If request comes at topmost demand class level get unallocated data
4535      along with period data from sd temp table*/
4536      IF i=1 AND G_ATP_FW_CONSUME_METHOD = 2 THEN
4537 
4538         -- time_phased_atp
4539         IF l_time_phased_atp='Y' THEN
4540            MSC_ATP_PF.Get_Unalloc_Data_From_Sd_Temp(x_atp_period, l_unallocated_atp, l_return_status);
4541         ELSE
4542            MSC_AATP_PROC.get_unalloc_data_from_SD_temp(x_atp_period, l_unallocated_atp, l_return_status);
4543         END IF;
4544 
4545         IF  l_return_status = FND_API.G_RET_STS_ERROR THEN
4546            IF PG_DEBUG in ('Y', 'C') THEN
4547               msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error in call to get_unalloc_data_from_SD_temp');
4548            END IF;
4549         END IF;
4550 
4551         IF PG_DEBUG in ('Y', 'C') THEN
4552            msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'After get_unalloc_data_from_SD_temp');
4553            msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_unallocated_atp.atp_qty.COUNT : '||l_unallocated_atp.atp_qty.COUNT);
4554            msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Before Atp_Consume for l_unallocated_atp');
4555 	   mm := l_unallocated_atp.atp_qty.FIRST;
4556 	   while mm is not null loop
4557 		msc_sch_wb.atp_debug('l_unallocated_atp.atp_period:atp_qty = ' ||
4558 		            x_atp_period.Period_Start_Date(mm) || ':' || l_unallocated_atp.atp_qty(mm));
4559 		mm := l_unallocated_atp.atp_qty.NEXT(mm);
4560 	   end loop;
4561         END IF;
4562 
4563         -- after calculating the net unallocated availability do b/w+f/w consumption and accumulation
4564         -- time_phased_atp
4565         IF l_time_phased_atp = 'Y' THEN
4566             MSC_ATP_PF.pf_atp_consume(
4567                    l_unallocated_atp.atp_qty,
4568                    l_return_status,
4569                    x_atp_period.Period_Start_Date,
4570                    MSC_ATP_PF.Bw_Fw_Cum, --b/w, f/w consumption and accumulation
4571                    p_atf_date);
4572             IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4573                 IF PG_DEBUG in ('Y', 'C') THEN
4574                         msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error occured in procedure Pf_Atp_Consume');
4575                 END IF;
4576                 RAISE FND_API.G_EXC_ERROR;
4577             END IF;
4578         ELSE
4579             MSC_ATP_PROC.Atp_Consume(l_unallocated_atp.atp_qty, l_unallocated_atp.atp_qty.COUNT);
4580         END IF;
4581 
4582         IF PG_DEBUG in ('Y', 'C') THEN
4583            msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'After Atp_Consume for l_unallocated_atp');
4584 	   mm := l_unallocated_atp.atp_qty.FIRST;
4585 	   while mm is not null loop
4586 		msc_sch_wb.atp_debug('l_unallocated_atp.atp_period:atp_qty = ' ||
4587 		            x_atp_period.Period_Start_Date(mm) || ':' || l_unallocated_atp.atp_qty(mm));
4588 		mm := l_unallocated_atp.atp_qty.NEXT(mm);
4589 	   end loop;
4590         END IF;
4591 
4592      ELSE
4593         -- time_phased_atp
4594         IF l_time_phased_atp='Y' THEN
4595            MSC_ATP_PF.Get_Period_Data_From_Sd_Temp(x_atp_period, l_return_status);
4596            IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4597                 IF PG_DEBUG in ('Y', 'C') THEN
4598                         msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error occured in procedure Get_Period_Data_From_Sd_Temp');
4599                 END IF;
4600                 RAISE FND_API.G_EXC_ERROR;
4601            END IF;
4602         ELSE
4603            /* time_phased_atp
4604               call new procedure to fix the issue of not displaying correct quantities in ATP SD Window when
4605               user opens ATP SD window from ATP pegging in allocated scenarios*/
4606            MSC_ATP_PROC.Get_Alloc_Data_From_Sd_Temp(x_atp_period, l_return_status);
4607            IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4608                 IF PG_DEBUG in ('Y', 'C') THEN
4609                         msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error occured in procedure get_alloc_data_from_SD_temp');
4610                 END IF;
4611                 RAISE FND_API.G_EXC_ERROR;
4612            END IF;
4613         END IF;
4614 
4615         IF PG_DEBUG in ('Y', 'C') THEN
4616            msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Need not get unalloc data from SD temp');
4617         END IF;
4618      END IF;
4619 
4620      -- old netting code here
4621 
4622       l_current_atp.atp_period := x_atp_period.Period_Start_Date;
4623       l_current_atp.atp_qty := x_atp_period.Period_Quantity;
4624       -- bug 1657855, remove support for min alloc
4625       --l_current_atp.limit_qty := l_current_atp.atp_qty; -- 02/16
4626 
4627     END IF;
4628 
4629     IF PG_DEBUG in ('Y', 'C') THEN
4630        msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'right after the big query');
4631        Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
4632 		l_current_atp);
4633     END IF;
4634 
4635     /* rajjain 01/29/2003 Bug 2737596
4636        We only need to do b/w consumption, f/w consumption and accumulation in case the call is
4637        from MSC_AATP_PVT.VIEW_ALLOCATION for view total*/
4638     IF p_identifier <> -1 or (p_identifier = -1 and p_scenario_id <> -1) THEN
4639 
4640       -- do backward consumption for DCi
4641 
4642       -- time_phased_atp
4643       IF l_time_phased_atp = 'Y' THEN
4644           MSC_ATP_PF.pf_atp_consume(
4645                  l_current_atp.atp_qty,
4646                  l_return_status,
4647                  l_current_atp.atp_period,
4648                  MSC_ATP_PF.Backward, --b/w consumption
4649                  p_atf_date);
4650           IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4651                 IF PG_DEBUG in ('Y', 'C') THEN
4652                         msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error occured in procedure Pf_Atp_Consume');
4653                 END IF;
4654                 RAISE FND_API.G_EXC_ERROR;
4655           END IF;
4656       ELSE
4657             MSC_ATP_PROC.Atp_Backward_Consume(l_current_atp.atp_qty);
4658       END IF;
4659 
4660       IF PG_DEBUG in ('Y', 'C') THEN
4661          msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'right after the backward consume');
4662          Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
4663    	        l_current_atp);
4664       END IF;
4665 
4666 
4667 
4668       -- we have 3 records of tables.
4669       -- l_current_atp: stores the date and quantity for this demand class,
4670       --                and since we need to do backward consumption on this.
4671       -- l_current_steal_atp: stores the date and quantity from higher priority
4672       --                      demand class, this need to consume l_current_atp
4673       -- l_next_steal_atp : stores  the date and quantity for next priority
4674       --                    demand class to cunsume.  we need this because we may
4675       --                    have multiple demand classes at same priority .
4676       -- for example, we have DC1 in priority 1, DC21, DC22 in priority 2,
4677       -- DC3 in priority  3.
4678       -- now DC21 need to take care DC1, DC22 need to take care DC1 but not DC21,
4679       -- DC3 need to take care DC1, DC21, and DC22.  so if we are in the loop for
4680       -- DC22, than l_current_atp is the atp info for DC22,
4681       -- l_current_steal_atp is the atp info for DC1(which does not include DC21),
4682       -- and l_next_steal_atp is the stealing data that we need to take care
4683       -- for DC1, DC21 and DC22  when later on we move to the loop for DC3.
4684 
4685       -- do backward consumption if DC1 to DC(i-1) has any negative bucket,and
4686       -- the priority  is higher than DCi
4687       -- the l_current_atp is an in/out parameter
4688 
4689       -- for 1680719, since in hierarchy demand class we cannot
4690       -- judge the priority by just looking at the priority (we need
4691       -- the information from the parent, so the condition needs to be changed.
4692 
4693       IF l_level_id IN (-1, 1) THEN
4694         -- here is the old logic which should still be ok for level id 1 and -1
4695        IF (i > 1) THEN
4696 
4697         IF (l_demand_class_priority_tab(i) >
4698             l_demand_class_priority_tab (i-1)) THEN
4699         -- we don't need to change the l_current_steal_atp if we don't
4700         -- move to next priority.
4701         -- but we do need to change the l_current_steal_atp
4702         -- if we are in different priority  now.
4703 
4704           l_current_steal_atp := l_next_steal_atp;
4705 
4706           -- Added for bug 1409335. Need to initialize l_next_steal_atp
4707           -- otherwise quanities would be getting accumulated
4708           -- repeatedly.
4709           l_next_steal_atp := l_null_steal_atp;
4710         END IF;
4711        END IF;
4712       ELSE -- IF l_level_id IN (-1, 1) THEN
4713 
4714        IF (i > 1) THEN
4715 
4716         IF (l_class_tab(i) <> l_class_tab(i-1)) THEN
4717 
4718           -- class changed.  If priority of both classes are not the same,
4719           -- then we need to change the curr_steal_atp  at class level.
4720 
4721           IF PG_DEBUG in ('Y', 'C') THEN
4722              msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'class changed');
4723           END IF;
4724 
4725           IF trunc(l_demand_class_priority_tab(i), -3) >
4726              trunc(l_demand_class_priority_tab (i-1), -3) THEN
4727 
4728             IF PG_DEBUG in ('Y', 'C') THEN
4729                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'class priority changed');
4730             END IF;
4731             l_class_curr_steal_atp := l_class_next_steal_atp;
4732             l_class_next_steal_atp := l_null_steal_atp;
4733           END IF;
4734 
4735           l_partner_next_steal_atp := l_null_steal_atp;
4736           l_partner_curr_steal_atp := l_null_steal_atp;
4737           l_partner_next_steal_atp := l_null_steal_atp;
4738           l_current_steal_atp := l_null_steal_atp;
4739           l_next_steal_atp := l_null_steal_atp;
4740 
4741         ELSE
4742           IF PG_DEBUG in ('Y', 'C') THEN
4743              msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'same class');
4744           END IF;
4745           IF (l_partner_tab(i) <> l_partner_tab(i-1)) THEN
4746             -- customer changed.  If priority of both customers are not the
4747             -- same, we need to change the curr_steal_atp  at partner level.
4748 
4749             IF PG_DEBUG in ('Y', 'C') THEN
4750                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'customer changed');
4751             END IF;
4752 
4753             IF trunc(l_demand_class_priority_tab(i), -2) >
4754                trunc(l_demand_class_priority_tab (i-1), -2) THEN
4755               IF PG_DEBUG in ('Y', 'C') THEN
4756                  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'customer priority changed');
4757               END IF;
4758 
4759               l_partner_curr_steal_atp := l_partner_next_steal_atp;
4760               l_partner_next_steal_atp := l_null_steal_atp;
4761             END IF;
4762 
4763             l_current_steal_atp := l_null_steal_atp;
4764             l_next_steal_atp := l_null_steal_atp;
4765 
4766           ELSE
4767             -- same customer
4768             IF PG_DEBUG in ('Y', 'C') THEN
4769                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'same customer ');
4770             END IF;
4771 
4772             IF (l_demand_class_priority_tab(i) >
4773                 l_demand_class_priority_tab (i-1)) THEN
4774               -- site level priority changed
4775 
4776               IF PG_DEBUG in ('Y', 'C') THEN
4777                  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'site priority changed');
4778               END IF;
4779               l_current_steal_atp := l_next_steal_atp;
4780               l_next_steal_atp := l_null_steal_atp;
4781 
4782             END IF;
4783           END IF; -- IF (l_partner_tab(i) <> l_partner_tab(i-1))
4784         END IF; -- IF (l_class_tab(i) <> l_class_tab(i-1))
4785 
4786        END IF; -- IF (i > 1)
4787 
4788       END IF; -- IF l_level_id IN (-1, 1)
4789       IF PG_DEBUG in ('Y', 'C') THEN
4790          msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'before we decide we need to do dc consumption');
4791       END IF;
4792       IF (i > 1) THEN
4793        IF (  -- this is the huge condition
4794              ((l_level_id IN (-1, 1)) AND
4795              (l_demand_class_priority_tab(i) <> l_demand_class_priority_tab(1)))
4796            OR
4797              (l_level_id in (2, 3))
4798           ) THEN
4799 
4800         -- we need to do demand class consume only if we are not in the first
4801         -- preferred priority
4802 
4803         -- bug 1413459
4804         -- we need to remember what's the atp picture before the
4805         -- demand class consumption but after it's own backward
4806         -- consumption.  so that we can figure out the stealing
4807         -- quantity correctly.
4808         IF (NVL(p_insert_flag, 0) <>0)
4809            AND (l_demand_class_tab(i) = p_demand_class) THEN
4810             l_temp_atp := l_current_atp;
4811         END IF;
4812 
4813         IF PG_DEBUG in ('Y', 'C') THEN
4814            msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'p_demand_class : '||p_demand_class);
4815            msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_demand_class_tab(i) : '||l_demand_class_tab(i));
4816         END IF;
4817 
4818         -- 1680719
4819         -- since we have hierarchy now, before we do demand class
4820         -- consumption for site level, we need to do the class level and
4821         -- partner level first
4822 
4823         IF l_level_id IN (2,3) THEN
4824 
4825           IF l_class_tab(i) <> l_class_tab(1) THEN
4826 
4827             IF PG_DEBUG in ('Y', 'C') THEN
4828                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'before consume l_class_curr_steal_atp');
4829                Print_Period_Qty('l_class_curr_steal_atp.atp_period:atp_qty = ',
4830 			l_class_curr_steal_atp);
4831             END IF;
4832 
4833             /* time_phased_atp
4834                pass p_atf_date to make sure we do not do demand class consumption across ATF*/
4835             MSC_AATP_PVT.Atp_Demand_Class_Consume(l_current_atp, l_class_curr_steal_atp, p_atf_date);
4836 
4837             IF PG_DEBUG in ('Y', 'C') THEN
4838                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'After consume l_class_curr_steal_atp');
4839                Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
4840 			l_current_atp);
4841                Print_Period_Qty('l_class_curr_steal_atp.atp_period:atp_qty = ',
4842 			l_class_curr_steal_atp);
4843 
4844             END IF;
4845 
4846           END IF; --IF l_class_tab(i) <> l_class_tab(1) THEN
4847 
4848           -- bug 1922942: although partner_id should be unique, we introduced
4849           -- -1 for 'Other' which make the partner_id not unique.
4850           -- for example, Class1/Other and Class2/Other will have same
4851           -- partner_id -1. so the if condition needs to be modified.
4852 
4853           -- IF l_partner_tab(i) <> l_partner_tab(1) THEN
4854 
4855           IF (l_class_tab(i) <> l_class_tab(1)) OR
4856               (l_partner_tab(i) <> l_partner_tab(1)) THEN
4857             IF PG_DEBUG in ('Y', 'C') THEN
4858                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'before consume l_partner_curr_steal_atp');
4859             END IF;
4860             /* time_phased_atp
4861                pass p_atf_date to make sure we do not do demand class consumption across ATF*/
4862             MSC_AATP_PVT.Atp_Demand_Class_Consume(l_current_atp, l_partner_curr_steal_atp, p_atf_date);
4863 
4864             IF PG_DEBUG in ('Y', 'C') THEN
4865                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'After consume l_partner_curr_steal_atp');
4866                Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
4867 			l_current_atp);
4868                Print_Period_Qty('l_partner_curr_steal_atp.atp_period:atp_qty = ',
4869 			l_partner_curr_steal_atp);
4870             END IF;
4871 
4872           END IF; -- IF l_partner_tab(i) <> l_partner_tab(1) THEN
4873 
4874         END IF; -- IF l_level_id IN (2,3)
4875 
4876         IF PG_DEBUG in ('Y', 'C') THEN
4877            msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Before consume current_steal_atp');
4878            Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
4879 		l_current_atp);
4880         END IF;
4881 
4882         /* time_phased_atp
4883            pass p_atf_date to make sure we do not do demand class consumption across ATF*/
4884         MSC_AATP_PVT.Atp_Demand_Class_Consume(l_current_atp, l_current_steal_atp, p_atf_date);
4885 
4886         IF PG_DEBUG in ('Y', 'C') THEN
4887            msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'After consume l_current_steal_atp');
4888 
4889            Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
4890 		l_current_atp);
4891            Print_Period_Qty('l_current_steal_atp.atp_period:atp_qty = ',
4892 		l_current_steal_atp);
4893         END IF;
4894 
4895         -- this part is not in the original design.
4896         -- original design is that we will ignore the inconsistancy
4897         -- in the s/d and period atp for display when stealing happens, as long
4898         -- as we take care the stealing in the logic.
4899         -- but i think it is still better to put it in.
4900         -- and actually if we change Atp_Demand_Class_Consume we can
4901         -- deal with this together.  but for now...
4902 
4903         -- we need to know if we need to store the stealing
4904         -- results in to x_atp_supply_demand and x_atp_period or not.
4905         -- we only do it if this is the demand class we request and
4906         -- insert_flag is on
4907 
4908         IF (NVL(p_insert_flag, 0) <>0) AND (l_demand_class_tab(i) = p_demand_class) THEN
4909 
4910           /*rajjain begin 12/10/2002
4911           We now do following instead of making call to MSC_SATP_FUNC.Extend_Atp_Supply_Demand
4912           number of times condition l_current_atp.atp_qty(j) < l_temp_atp.atp_qty(j) is true
4913           inside FOR LOOP
4914           1. Before FOR loop we extend l_temp_atp_supply_demand by l_current_atp.atp_qty.COUNT
4915           2. After FOR loop we trim the remaining.*/
4916 
4917           -- initialize k
4918           k := l_temp_atp_supply_demand.Level.Count;
4919           MSC_SATP_FUNC.Extend_Atp_Supply_Demand(l_temp_atp_supply_demand, l_return_status,
4920                         l_current_atp.atp_qty.COUNT);
4921 
4922           IF PG_DEBUG in ('Y', 'C') THEN
4923             msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'k before FOR LOOP = ' || k);
4924           END IF;
4925 
4926           FOR j in 1..l_current_atp.atp_qty.COUNT LOOP
4927 
4928             IF l_current_atp.atp_qty(j) < l_temp_atp.atp_qty(j) THEN
4929               -- this is the stealing quantity in that period
4930 
4931               IF PG_DEBUG in ('Y', 'C') THEN
4932                  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' ||
4933                             'l_current_atp.atp_qty(j)='||l_current_atp.atp_qty(j));
4934                  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' ||
4935                             'l_temp_atp.atp_qty(j)='||l_temp_atp.atp_qty(j));
4936                  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' ||
4937                             'l_steal_period_quantity='||l_steal_period_quantity);
4938                  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' ||
4939                             'x_atp_period.period_start_date(j)='||x_atp_period.period_start_date(j));
4940               END IF;
4941 
4942               -- bug 1413459: the stealing quantity should be the current
4943               -- period quantity (after backward consumption, after stealing)
4944               -- minus the period quantity after backward consumption but
4945               -- before the stealing
4946               l_steal_period_quantity := l_current_atp.atp_qty(j) -
4947                                          l_temp_atp.atp_qty(j);
4948 
4949               /*rajjain 12/10/2002 This procedure call is not needed as we have already extended
4950               l_temp_atp_supply_demand outside this FOR loop
4951               MSC_SATP_FUNC.Extend_Atp_Supply_Demand(l_temp_atp_supply_demand, l_return_status);
4952 
4953               k := l_temp_atp_supply_demand.Level.Count;*/
4954 
4955               k := k+1; -- rajjain increment k
4956               l_temp_atp_supply_demand.level(k) := p_level;
4957               l_temp_atp_supply_demand.identifier(k) := p_identifier;
4958               l_temp_atp_supply_demand.scenario_id(k) := p_scenario_id;
4959               l_temp_atp_supply_demand.inventory_item_id(k) := p_inventory_item_id;
4960               l_temp_atp_supply_demand.uom(k):= l_uom_code;
4961               l_temp_atp_supply_demand.supply_demand_type(k) := 1;
4962 
4963               -- Bug 1408132 and 1416290, Need to insert type as
4964               -- Demand Class Consumption (45).
4965 
4966               l_temp_atp_supply_demand.identifier1(k) := p_instance_id;
4967               l_temp_atp_supply_demand.supply_demand_date (k) := l_current_atp.atp_period(j);
4968               l_temp_atp_supply_demand.supply_demand_quantity(k) := l_steal_period_quantity;
4969 
4970               -- time_phased_atp change begin
4971               l_temp_atp_supply_demand.organization_id(k) := p_organization_id;
4972               IF l_time_phased_atp='Y' THEN
4973                  l_temp_atp_supply_demand.request_item_id(k) := p_request_item_id;
4974                  l_temp_atp_supply_demand.supply_demand_source_type(k) := 51;
4975                  l_temp_atp_supply_demand.Original_Supply_Demand_Type(k) := 45;
4976                  l_temp_atp_supply_demand.Original_Item_Id(k) := p_request_item_id;
4977                  l_temp_atp_supply_demand.Original_Demand_Date(k) := l_current_atp.atp_period(j);
4978                  l_temp_atp_supply_demand.Original_Demand_Quantity(k) := l_steal_period_quantity;
4979                  l_temp_atp_supply_demand.Allocated_Quantity(k) := l_steal_period_quantity;
4980                  l_temp_atp_supply_demand.Pf_Display_Flag(k) := 1;
4981                  x_atp_period.Total_Bucketed_Demand_Quantity(j):=
4982                         x_atp_period.Total_Bucketed_Demand_Quantity(j) + l_steal_period_quantity;
4983               ELSE
4984                  l_temp_atp_supply_demand.request_item_id(k) := p_inventory_item_id;
4985                  l_temp_atp_supply_demand.supply_demand_source_type(k) := 45;
4986               END IF;
4987               -- time_phased_atp change end
4988 
4989               x_atp_period.Total_Demand_Quantity(j):=
4990                      x_atp_period.Total_Demand_Quantity(j) + l_steal_period_quantity;
4991 
4992               x_atp_period.period_quantity(j):=x_atp_period.period_quantity(j)
4993                      + l_steal_period_quantity;
4994 
4995             END IF;
4996           END LOOP;
4997 
4998           IF PG_DEBUG in ('Y', 'C') THEN
4999             msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'k after FOR LOOP = ' || k);
5000           END IF;
5001 
5002           --rajjain trim the remaining 12/10/2002
5003           MSC_SATP_FUNC.Trim_Atp_Supply_Demand(l_temp_atp_supply_demand, l_return_status,
5004                         (l_temp_atp_supply_demand.Level.Count - k));
5005 
5006 	  -- dsting dump the data into msc_atp_sd_details_temp
5007 	  -- and null out the record
5008 	  move_SD_plsql_into_SD_temp(l_temp_atp_supply_demand);
5009 
5010         END IF;  -- IF (NVL(p_insert_flag, 0) <>0) .....
5011        END IF; -- the huge condition
5012       END IF; -- IF (i > 1)
5013 
5014       -- AATP Forward Consumption rajjain begin
5015       IF l_level_id IN (-1, 1) THEN
5016         IF PG_DEBUG in ('Y', 'C') THEN
5017            msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Inside IF l_level_id IN (-1, 1) THEN');
5018         END IF;
5019 
5020         /*1. If level_id is in (-1, 1) and forward consumption method is method1
5021               for the lowest priority demand class -> add negatives from l_current_steal to
5022               l_current_atp and do forward consumption
5023           2. If level_id is in (-1, 1) and forward consumption method is method2
5024               for the lowest priority demand class -> call Atp_Remove_Negatives to remove
5025               all the negatives from l_current_atp*/
5026 	IF l_demand_class = l_lowest_priority_demand_class THEN
5027           IF PG_DEBUG in ('Y', 'C') THEN
5028                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'This is l_lowest_priority_demand_class');
5029           END IF;
5030           IF G_ATP_FW_CONSUME_METHOD = 1 THEN
5031             IF PG_DEBUG in ('Y', 'C') THEN
5032                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Method 1');
5033                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'level = '||l_level_id);
5034                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'before Add_to_Current_Atp');
5035                Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5036 			l_current_atp);
5037                Print_Period_Qty('l_current_steal_atp.atp_period:atp_qty = ',
5038 			l_current_steal_atp);
5039             END IF;
5040 
5041             MSC_AATP_PROC.Add_to_Current_Atp(l_current_steal_atp, l_current_atp, l_return_status);
5042             IF  l_return_status = FND_API.G_RET_STS_ERROR THEN
5043                IF PG_DEBUG in ('Y', 'C') THEN
5044                   msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error in call to Add_to_Current_Atp');
5045                END IF;
5046             END IF;
5047             IF PG_DEBUG in ('Y', 'C') THEN
5048                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'after Add_to_Current_Atp');
5049                Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5050 			l_current_atp);
5051             END IF;
5052 
5053             MSC_AATP_PROC.Atp_Forward_Consume(l_current_atp.atp_period, p_atf_date, l_current_atp.atp_qty, l_return_status);
5054             IF  l_return_status = FND_API.G_RET_STS_ERROR THEN
5055                IF PG_DEBUG in ('Y', 'C') THEN
5056                   msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error in call to Atp_Forward_Consume');
5057                END IF;
5058             END IF;
5059 
5060             IF PG_DEBUG in ('Y', 'C') THEN
5061                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'after Atp_Forward_Consume for last demand class');
5062                Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5063 			l_current_atp);
5064             END IF;
5065 
5066             /*Call Atp_Remove_Negatives to remove negatives in case we have negatives left
5067             after forward consumption. we will not show these negatives in atp_inquiry for
5068             Allocated ATP with User Defined %.*/
5069 
5070             MSC_AATP_PROC.Atp_Remove_Negatives(l_current_atp.atp_qty, l_return_status);
5071             IF  l_return_status = FND_API.G_RET_STS_ERROR THEN
5072                IF PG_DEBUG in ('Y', 'C') THEN
5073                   msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error in call to Atp_Remove_Negatives');
5074                END IF;
5075             END IF;
5076 
5077             IF PG_DEBUG in ('Y', 'C') THEN
5078                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'after Atp_Remove_Negatives');
5079                Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5080 			l_current_atp);
5081             END IF;
5082 
5083           ELSE
5084             IF PG_DEBUG in ('Y', 'C') THEN
5085                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Method 2');
5086             END IF;
5087 	    MSC_AATP_PROC.Atp_remove_negatives(l_current_atp.atp_qty, l_return_status);
5088             IF  l_return_status = FND_API.G_RET_STS_ERROR THEN
5089                IF PG_DEBUG in ('Y', 'C') THEN
5090                   msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error in call to Atp_remove_negatives');
5091                END IF;
5092             END IF;
5093 
5094           END IF;
5095 
5096 	ELSE
5097           IF PG_DEBUG in ('Y', 'C') THEN
5098                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'This is not the l_lowest_priority_demand_class');
5099           END IF;
5100 	  IF l_demand_class_priority_tab(i) < l_lowest_priority OR G_ATP_FW_CONSUME_METHOD = 2 THEN
5101             -- this demand class is not the lowest priority demand class
5102             -- add negatives from l_current_atp to l_next_steal_atp
5103             IF PG_DEBUG in ('Y', 'C') THEN
5104                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: '||'l_demand_class_priority_tab(i)<l_lowest_priority'||
5105                           ' OR G_ATP_FW_CONSUME_METHOD = 2');
5106                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'before Add_to_Next_Steal_Atp');
5107                Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5108 			l_current_atp);
5109                Print_Period_Qty('l_next_steal_atp.atp_period:atp_qty = ',
5110 			l_next_steal_atp);
5111             END IF;
5112 
5113             -- we need to prepare the l_next_steal_atp for next priority
5114             MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_current_atp, l_next_steal_atp);
5115             IF PG_DEBUG in ('Y', 'C') THEN
5116                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'after Add_to_Next_Steal_Atp');
5117                Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5118 			l_current_atp);
5119                Print_Period_Qty('l_next_steal_atp.atp_period:atp_qty = ',
5120 			l_next_steal_atp);
5121             END IF;
5122 
5123             IF i < l_demand_class_priority_tab.LAST AND l_demand_class_priority_tab(i)<
5124                l_demand_class_priority_tab(i+1) THEN
5125                -- this is the last element of current priority, so we also need
5126                -- to add l_steal_atp into l_next_steal_atp if we can not finish
5127                -- the stealing at this priority
5128                IF PG_DEBUG in ('Y', 'C') THEN
5129                   msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'This is the last element of current priority');
5130                   msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'level = '||l_level_id);
5131 		  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'before Adding Add_to_Next_Steal_Atp');
5132 
5133                   Print_Period_Qty('l_next_steal_atp.atp_period:atp_qty = ',
5134 		       l_next_steal_atp);
5135                END IF;
5136 
5137                MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_current_steal_atp, l_next_steal_atp);
5138 
5139                IF PG_DEBUG in ('Y', 'C') THEN
5140                   msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'after Add_to_Next_Steal_Atp');
5141 
5142                   Print_Period_Qty('l_next_steal_atp.atp_period:atp_qty = ',
5143 		       l_next_steal_atp);
5144                END IF;
5145 
5146             END IF;
5147 
5148           ELSE
5149 	    /* this is the lowest priority demand class in case
5150 	    we have multiple demand classes at lowest priority
5151 	    do forward consumption for all the lowest priority demand classes for method 1*/
5152             IF PG_DEBUG in ('Y', 'C') THEN
5153                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'This demand class is of l_lowest_priority');
5154                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'level = '||l_level_id);
5155                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' ||
5156                           'before Forward Consumption for lowest priority demand classes');
5157                Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5158 			l_current_atp);
5159             END IF;
5160 
5161 	    MSC_AATP_PROC.Atp_Forward_Consume(l_current_atp.atp_period, p_atf_date, l_current_atp.atp_qty, l_return_status);
5162             IF  l_return_status = FND_API.G_RET_STS_ERROR THEN
5163                IF PG_DEBUG in ('Y', 'C') THEN
5164                   msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error in call to Atp_Forward_Consume');
5165                END IF;
5166             END IF;
5167             IF PG_DEBUG in ('Y', 'C') THEN
5168                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' ||
5169                           'after Forward Consumption for lowest priority demand classes');
5170                Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5171 			l_current_atp);
5172             END IF;
5173 
5174             /*Call Atp_Remove_Negatives to remove negatives in case we have negatives left
5175             after forward consumption. we will not show these negatives in atp_inquiry for
5176             Allocated ATP with User Defined %.*/
5177 
5178             MSC_AATP_PROC.Atp_Remove_Negatives(l_current_atp.atp_qty, l_return_status);
5179             IF  l_return_status = FND_API.G_RET_STS_ERROR THEN
5180                IF PG_DEBUG in ('Y', 'C') THEN
5181                   msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error in call to Atp_Remove_Negatives');
5182                END IF;
5183             END IF;
5184 
5185             IF PG_DEBUG in ('Y', 'C') THEN
5186                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'after Atp_Remove_Negatives');
5187                Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5188 			l_current_atp);
5189             END IF;
5190 
5191 	  END IF;
5192 
5193 	END IF;
5194 
5195       ELSE --IF l_level_id IN (-1, 1) THEN
5196         IF PG_DEBUG in ('Y', 'C') THEN
5197            msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Inside ELSE of IF l_level_id IN (-1, 1) THEN');
5198         END IF;
5199 	IF l_demand_class = l_lowest_priority_demand_class THEN
5200           IF PG_DEBUG in ('Y', 'C') THEN
5201                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'This is l_lowest_priority_demand_class');
5202           END IF;
5203           IF G_ATP_FW_CONSUME_METHOD = 1 THEN
5204             IF PG_DEBUG in ('Y', 'C') THEN
5205                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Method 1');
5206             END IF;
5207 	    IF l_fw_consume_tab(i) = 4 THEN
5208               -- add all negatives to l_current_atp and do forward consumption
5209               IF PG_DEBUG in ('Y', 'C') THEN
5210                  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_fw_consume_tab(i) is 4');
5211               END IF;
5212               MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_current_steal_atp, l_next_steal_atp);
5213               MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_next_steal_atp, l_partner_next_steal_atp);
5214               MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_partner_curr_steal_atp,
5215                                     l_partner_next_steal_atp);
5216               MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_partner_next_steal_atp,
5217                                     l_class_next_steal_atp);
5218               MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_class_curr_steal_atp,
5219                                     l_class_next_steal_atp);
5220               IF PG_DEBUG in ('Y', 'C') THEN
5221                  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'before Add_to_Next_Steal_Atp');
5222 
5223                  Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5224 		      l_current_atp);
5225                  Print_Period_Qty('l_class_next_steal_atp.atp_period:atp_qty = ',
5226 		      l_class_next_steal_atp);
5227               END IF;
5228               MSC_AATP_PROC.Add_to_Current_Atp(l_class_next_steal_atp, l_current_atp, l_return_status);
5229               IF  l_return_status = FND_API.G_RET_STS_ERROR THEN
5230                  IF PG_DEBUG in ('Y', 'C') THEN
5231                     msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error in call to Add_to_Current_Atp');
5232                  END IF;
5233               END IF;
5234 
5235               IF PG_DEBUG in ('Y', 'C') THEN
5236                  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'after Add_to_Current_Atp');
5237 
5238                  Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5239 		      l_current_atp);
5240                  Print_Period_Qty('l_class_next_steal_atp.atp_period:atp_qty = ',
5241 		      l_class_next_steal_atp);
5242               END IF;
5243               MSC_AATP_PROC.Atp_Forward_Consume(l_current_atp.atp_period, p_atf_date, l_current_atp.atp_qty, l_return_status);
5244               IF  l_return_status = FND_API.G_RET_STS_ERROR THEN
5245                  IF PG_DEBUG in ('Y', 'C') THEN
5246                     msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error in call to Atp_Forward_Consume');
5247                  END IF;
5248               END IF;
5249               IF PG_DEBUG in ('Y', 'C') THEN
5250                  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'after Atp_forward_consume');
5251 
5252                  Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5253 		      l_current_atp);
5254               END IF;
5255 
5256               /*Call Atp_Remove_Negatives to remove negatives in case we have negatives left
5257               after forward consumption. we will not show these negatives in atp_inquiry for
5258               Allocated ATP with User Defined %.*/
5259 
5260               MSC_AATP_PROC.Atp_Remove_Negatives(l_current_atp.atp_qty, l_return_status);
5261               IF  l_return_status = FND_API.G_RET_STS_ERROR THEN
5262                  IF PG_DEBUG in ('Y', 'C') THEN
5263                     msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error in call to Atp_Remove_Negatives');
5264                  END IF;
5265               END IF;
5266 
5267               IF PG_DEBUG in ('Y', 'C') THEN
5268                  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'after Atp_Remove_Negatives');
5269                  Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5270 	  		  l_current_atp);
5271               END IF;
5272 	    END IF;
5273           ELSE
5274             -- method2, remove negatives from l_current_atp
5275             IF PG_DEBUG in ('Y', 'C') THEN
5276                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Method 2');
5277             END IF;
5278 	    MSC_AATP_PROC.Atp_remove_negatives(l_current_atp.atp_qty, l_return_status);
5279             IF  l_return_status = FND_API.G_RET_STS_ERROR THEN
5280                IF PG_DEBUG in ('Y', 'C') THEN
5281                   msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error in call to Atp_remove_negatives');
5282                END IF;
5283             END IF;
5284 
5285           END IF;
5286 	ELSE --IF l_demand_class = l_lowest_priority_demand_class THEN
5287           IF PG_DEBUG in ('Y', 'C') THEN
5288                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' ||
5289                           'This is not the l_lowest_priority_demand_class');
5290           END IF;
5291           IF G_ATP_FW_CONSUME_METHOD = 2 OR
5292              (G_ATP_FW_CONSUME_METHOD = 1 AND l_fw_consume_tab(i) = 0) THEN
5293               IF PG_DEBUG in ('Y', 'C') THEN
5294                  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Inside IF G_ATP_FW_CONSUME_METHOD = 2 OR '||
5295                           '(G_ATP_FW_CONSUME_METHOD = 1 AND l_fw_consume_tab(i) = 0)');
5296 		 msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'before Adding Add_to_Next_Steal_Atp');
5297 
5298                  Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5299 		      l_current_atp);
5300                  Print_Period_Qty('l_next_steal_atp.atp_period:atp_qty = ',
5301 		      l_next_steal_atp);
5302               END IF;
5303 
5304               MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_current_atp, l_next_steal_atp);
5305 
5306               IF PG_DEBUG in ('Y', 'C') THEN
5307                  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'after Add_to_Next_Steal_Atp');
5308 
5309                  Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5310 		      l_current_atp);
5311                  Print_Period_Qty('l_next_steal_atp.atp_period:atp_qty = ',
5312 		      l_next_steal_atp);
5313               END IF;
5314 
5315 	  END IF;
5316 
5317           IF i = l_class_tab.LAST OR (l_class_tab(i) <> l_class_tab(i+1))
5318              OR (G_ATP_FW_CONSUME_METHOD = 1 AND l_fw_consume_tab(i) = 3) THEN
5319             -- either we are at last record in l_demand_class_tab or
5320             -- we are at last record at current customer class level in l_demand_class_tab or
5321             -- this is the lowest priority customer at customer class level
5322             IF PG_DEBUG in ('Y', 'C') THEN
5323                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' ||
5324                    'We are at lowest priority customer at current customer class level ');
5325             END IF;
5326 
5327             MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_current_steal_atp, l_next_steal_atp);
5328             MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_next_steal_atp, l_partner_next_steal_atp);
5329             MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_partner_curr_steal_atp,
5330                                   l_partner_next_steal_atp);
5331             IF G_ATP_FW_CONSUME_METHOD <> 1 OR l_fw_consume_tab(i) <> 3 THEN
5332                MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_partner_next_steal_atp,
5333                                      l_class_next_steal_atp);
5334             END IF;
5335             IF PG_DEBUG in ('Y', 'C') THEN
5336 		 msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'after Adding Add_to_Next_Steal_Atp');
5337 
5338                  Print_Period_Qty('l_class_next_steal_atp.atp_period:atp_qty = ',
5339 		      l_class_next_steal_atp);
5340                  Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5341 		      l_current_atp);
5342             END IF;
5343 
5344             IF i = l_class_tab.LAST OR trunc(l_demand_class_priority_tab(i), -3)<
5345                trunc(l_demand_class_priority_tab (i+1), -3) THEN
5346 
5347               -- next customer class is at higher priority then this cc
5348               -- add negatives from class_curr_steal to class_next_steal
5349               IF PG_DEBUG in ('Y', 'C') THEN
5350                   msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' ||
5351                              'next customer class is at higher priority then this cc');
5352               END IF;
5353               IF PG_DEBUG in ('Y', 'C') THEN
5354 		 msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'before Adding Add_to_Next_Steal_Atp');
5355 
5356                  Print_Period_Qty('l_class_next_steal_atp.atp_period:atp_qty = ',
5357 		      l_class_next_steal_atp);
5358                  Print_Period_Qty('l_class_curr_steal_atp.atp_period:atp_qty = ',
5359 		      l_class_curr_steal_atp);
5360               END IF;
5361               MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_class_curr_steal_atp,
5362                                     l_class_next_steal_atp);
5363               IF PG_DEBUG in ('Y', 'C') THEN
5364 		 msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'after Adding Add_to_Next_Steal_Atp');
5365 
5366                  Print_Period_Qty('l_class_next_steal_atp.atp_period:atp_qty = ',
5367 		      l_class_next_steal_atp);
5368                  Print_Period_Qty('l_class_curr_steal_atp.atp_period:atp_qty = ',
5369 		      l_class_curr_steal_atp);
5370               END IF;
5371 
5372             END IF;
5373 
5374           ELSE
5375             IF PG_DEBUG in ('Y', 'C') THEN
5376                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' ||
5377                'We are not at lowest priority customer at current customer class level ');
5378             END IF;
5379             IF (l_partner_tab(i) <> l_partner_tab(i+1))
5380                OR (G_ATP_FW_CONSUME_METHOD = 1 AND l_fw_consume_tab(i) = 2) THEN
5381               -- either we are at last record at current customer level in l_demand_class_tab or
5382               -- this is the lowest priority site at this customer level
5383               IF PG_DEBUG in ('Y', 'C') THEN
5384                   msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' ||
5385                   'We are at lowest priority customer site at current customer level  ');
5386               END IF;
5387               IF PG_DEBUG in ('Y', 'C') THEN
5388 		 msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'before Adding Add_to_Next_Steal_Atp');
5389 
5390                  Print_Period_Qty('l_current_steal_atp.atp_period:atp_qty = ',
5391 		      l_current_steal_atp);
5392                  Print_Period_Qty('l_partner_next_steal_atp.atp_period:atp_qty = ',
5393 		      l_partner_next_steal_atp);
5394               END IF;
5395               MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_current_steal_atp, l_next_steal_atp);
5396 
5397               IF G_ATP_FW_CONSUME_METHOD <> 1 OR l_fw_consume_tab(i) <> 2 THEN
5398                  -- if forward consumption type is 2 we wont add the negatives in l_next_steal_atp
5399                  -- to l_partner_steal_atp. we will add the negatives to l_current_atp and do forward
5400                  -- consumption instead.
5401                  MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_next_steal_atp, l_partner_next_steal_atp);
5402               END IF;
5403 
5404               IF PG_DEBUG in ('Y', 'C') THEN
5405 		 msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'after Adding Add_to_Next_Steal_Atp');
5406 
5407                  Print_Period_Qty('l_current_steal_atp.atp_period:atp_qty = ',
5408 		      l_current_steal_atp);
5409                  Print_Period_Qty('l_partner_next_steal_atp.atp_period:atp_qty = ',
5410 		      l_partner_next_steal_atp);
5411               END IF;
5412               IF trunc(l_demand_class_priority_tab(i), -2)<
5413                  trunc(l_demand_class_priority_tab (i+1), -2) THEN
5414                 IF PG_DEBUG in ('Y', 'C') THEN
5415                   msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' ||
5416                              'next customer is at higher priority then this customer');
5417 		  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'before Adding Add_to_Next_Steal_Atp');
5418 
5419                   Print_Period_Qty('l_partner_curr_steal_atp.atp_period:atp_qty = ',
5420 		       l_partner_curr_steal_atp);
5421                   Print_Period_Qty('l_partner_next_steal_atp.atp_period:atp_qty = ',
5422 		       l_partner_next_steal_atp);
5423                 END IF;
5424 
5425                 MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_partner_curr_steal_atp,
5426                                       l_partner_next_steal_atp);
5427 
5428                 IF PG_DEBUG in ('Y', 'C') THEN
5429 		  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'after Adding Add_to_Next_Steal_Atp');
5430 
5431                   Print_Period_Qty('l_partner_curr_steal_atp.atp_period:atp_qty = ',
5432 		       l_partner_curr_steal_atp);
5433                   Print_Period_Qty('l_partner_next_steal_atp.atp_period:atp_qty = ',
5434 		       l_partner_next_steal_atp);
5435                 END IF;
5436 
5437               END IF;
5438 
5439 
5440             ELSE
5441               -- we are not at last site under this customer
5442               IF PG_DEBUG in ('Y', 'C') THEN
5443                   msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' ||
5444                              'we are not at last site under this customer');
5445               END IF;
5446               IF (l_demand_class_priority_tab(i)<>
5447                   l_demand_class_priority_tab (i+1)) THEN
5448                 -- next site under this customer is not at same priority as this site
5449                 IF PG_DEBUG in ('Y', 'C') THEN
5450                     msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' ||
5451                          'next site under this customer is not at same priority as this site');
5452                 END IF;
5453                 IF PG_DEBUG in ('Y', 'C') THEN
5454 		  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'before Adding Add_to_Next_Steal_Atp');
5455 
5456                   Print_Period_Qty('l_current_steal_atp.atp_period:atp_qty = ',
5457 		       l_current_steal_atp);
5458                   Print_Period_Qty('l_next_steal_atp.atp_period:atp_qty = ',
5459 		       l_next_steal_atp);
5460                 END IF;
5461                 MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_current_steal_atp, l_next_steal_atp);
5462                 IF PG_DEBUG in ('Y', 'C') THEN
5463 		  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'after Adding Add_to_Next_Steal_Atp');
5464 
5465                   Print_Period_Qty('l_current_steal_atp.atp_period:atp_qty = ',
5466 		       l_current_steal_atp);
5467                   Print_Period_Qty('l_next_steal_atp.atp_period:atp_qty = ',
5468 		       l_next_steal_atp);
5469                 END IF;
5470 
5471               END IF;
5472             END IF;
5473           END IF;
5474 	  IF G_ATP_FW_CONSUME_METHOD = 1 AND l_fw_consume_tab(i) <> 0 THEN
5475 	    IF l_fw_consume_tab(i) = 2 THEN
5476               -- add negatives from l_next_steal_atp to l_current_atp and then do forward consumption
5477               IF PG_DEBUG in ('Y', 'C') THEN
5478                   msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_fw_consume_tab(i) = 2');
5479 		  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'before Adding Add_to_Current_Atp');
5480 
5481                   Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5482 		       l_current_atp);
5483                   Print_Period_Qty('l_next_steal_atp.atp_period:atp_qty = ',
5484 		       l_next_steal_atp);
5485               END IF;
5486 	      MSC_AATP_PROC.Add_to_Current_Atp(l_next_steal_atp, l_current_atp, l_return_status);
5487               IF  l_return_status = FND_API.G_RET_STS_ERROR THEN
5488                  IF PG_DEBUG in ('Y', 'C') THEN
5489                     msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error in call to Add_to_Current_Atp');
5490                  END IF;
5491               END IF;
5492               IF PG_DEBUG in ('Y', 'C') THEN
5493 		  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'after Adding Add_to_Current_Atp');
5494 
5495                   Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5496 		       l_current_atp);
5497                   Print_Period_Qty('l_next_steal_atp.atp_period:atp_qty = ',
5498 		       l_next_steal_atp);
5499               END IF;
5500 	    ELSIF l_fw_consume_tab(i) = 3 THEN
5501 	      -- add negatives from l_partner_next_steal_atp to l_current_atp and then do forward consumption
5502               IF PG_DEBUG in ('Y', 'C') THEN
5503                   msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_fw_consume_tab(i) = 3');
5504               END IF;
5505                 IF PG_DEBUG in ('Y', 'C') THEN
5506 		  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'before Adding Add_to_Current_Atp');
5507 
5508                   Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5509 		       l_current_atp);
5510                   Print_Period_Qty('l_partner_next_steal_atp.atp_period:atp_qty = ',
5511 		       l_partner_next_steal_atp);
5512                 END IF;
5513 	      MSC_AATP_PROC.Add_to_Current_Atp(l_partner_next_steal_atp, l_current_atp, l_return_status);
5514               IF  l_return_status = FND_API.G_RET_STS_ERROR THEN
5515                  IF PG_DEBUG in ('Y', 'C') THEN
5516                     msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error in call to Add_to_Current_Atp');
5517                  END IF;
5518               END IF;
5519               IF PG_DEBUG in ('Y', 'C') THEN
5520 		  msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'after Adding Add_to_Current_Atp');
5521 
5522                   Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5523 		       l_current_atp);
5524                   Print_Period_Qty('l_partner_next_steal_atp.atp_period:atp_qty = ',
5525 		       l_partner_next_steal_atp);
5526               END IF;
5527 	    END IF;
5528             IF PG_DEBUG in ('Y', 'C') THEN
5529                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Before Atp_Forward_Consume for Method1');
5530                Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5531 	  	        l_current_atp);
5532             END IF;
5533 	    MSC_AATP_PROC.Atp_Forward_Consume(l_current_atp.atp_period, p_atf_date, l_current_atp.atp_qty, l_return_status);
5534             IF  l_return_status = FND_API.G_RET_STS_ERROR THEN
5535                IF PG_DEBUG in ('Y', 'C') THEN
5536                   msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error in call to Atp_Forward_Consume');
5537                END IF;
5538             END IF;
5539             IF PG_DEBUG in ('Y', 'C') THEN
5540                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'After Atp_Forward_Consume for Method1');
5541                Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5542 	  	        l_current_atp);
5543             END IF;
5544 
5545             /*Call Atp_Remove_Negatives to remove negatives in case we have negatives left
5546             after forward consumption. we will not show these negatives in atp_inquiry for
5547             Allocated ATP with User Defined %.*/
5548 
5549             MSC_AATP_PROC.Atp_Remove_Negatives(l_current_atp.atp_qty, l_return_status);
5550             IF  l_return_status = FND_API.G_RET_STS_ERROR THEN
5551                IF PG_DEBUG in ('Y', 'C') THEN
5552                   msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error in call to Atp_Remove_Negatives');
5553                END IF;
5554             END IF;
5555 
5556             IF PG_DEBUG in ('Y', 'C') THEN
5557                msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'after Atp_Remove_Negatives');
5558                Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5559 	  		l_current_atp);
5560             END IF;
5561 	  END IF;
5562         END IF;
5563 
5564       END IF;
5565 
5566       IF G_ATP_FW_CONSUME_METHOD = 2 THEN
5567         -- method2, do accumulation and then calculate the adjusted cum
5568         IF PG_DEBUG in ('Y', 'C') THEN
5569            msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Before Atp_Accumulate for Method2');
5570            Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5571 		    l_current_atp);
5572         END IF;
5573 
5574         MSC_ATP_PROC.Atp_Accumulate(l_current_atp.atp_qty);
5575 
5576         IF PG_DEBUG in ('Y', 'C') THEN
5577            msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'After Atp_Accumulate for Method2');
5578            Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5579 		    l_current_atp);
5580         END IF;
5581 
5582 	MSC_AATP_PROC.Atp_Adjusted_Cum(l_current_atp, l_unallocated_atp, l_return_status);
5583         IF  l_return_status = FND_API.G_RET_STS_ERROR THEN
5584            IF PG_DEBUG in ('Y', 'C') THEN
5585               msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Error in call to Atp_Forward_Consume');
5586            END IF;
5587         END IF;
5588         IF PG_DEBUG in ('Y', 'C') THEN
5589            msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' ||
5590                     'l_current_atp after Atp_Adjusted_Cum for Method2');
5591            Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5592 		    l_current_atp);
5593            msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' ||
5594                     'l_unallocated_atp after Atp_Adjusted_Cum for Method2');
5595 	   mm := l_unallocated_atp.atp_qty.FIRST;
5596 	   while mm is not null loop
5597 		msc_sch_wb.atp_debug('l_unallocated_atp.atp_period:atp_qty = ' ||
5598 		            l_current_atp.atp_period(mm) || ':' || l_unallocated_atp.atp_qty(mm));
5599 		mm := l_unallocated_atp.atp_qty.NEXT(mm);
5600 	   end loop;
5601         END IF;
5602 
5603       END IF;
5604       -- rajjain AATP Forward Consumption end
5605     ELSE --IF p_identifier <> -1 or (p_identifier = -1 and p_scenario_id <> -1) THEN
5606        /* rajjain 01/29/2003 Bug 2737596
5607           We only need to do b/w consumption, f/w consumption and accumulation in case the call is
5608           from MSC_AATP_PVT.VIEW_ALLOCATION for view total*/
5609       MSC_ATP_PROC.Atp_Consume(l_current_atp.atp_qty, l_current_atp.atp_qty.COUNT);
5610     END IF;
5611 
5612     -- 1665110
5613     IF PG_DEBUG in ('Y', 'C') THEN
5614        msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'p_demand_class = '||p_demand_class);
5615     END IF;
5616     EXIT WHEN (l_demand_class = p_demand_class);
5617     IF PG_DEBUG in ('Y', 'C') THEN
5618        msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'after the exit statement, so we did not exit');
5619     END IF;
5620 
5621   END LOOP;
5622 
5623   /* rajjain 01/29/2003 Bug 2737596
5624      We need not do accumulation in case the call is from VIEW_ALLOCATION
5625      for view total as we have already done this in atp_consume*/
5626   IF p_identifier <> -1 or (p_identifier = -1 and p_scenario_id <> -1) THEN
5627     IF G_ATP_FW_CONSUME_METHOD = 1 THEN
5628       -- we need to do accumulation only if forward consumption method is 1
5629       -- as we have the adjusted cum picture available if forward consumption method is 2
5630       MSC_ATP_PROC.Atp_Accumulate(l_current_atp.atp_qty);
5631 
5632       IF PG_DEBUG in ('Y', 'C') THEN
5633          msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'Right after the Atp_Accumulate');
5634          msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_current_atp.atp_period.count = '||
5635                     l_current_atp.atp_period.count);
5636          msc_sch_wb.atp_debug('Item_Alloc_Cum_Atp: ' || 'l_current_atp.atp_qty.count = '||
5637                     l_current_atp.atp_qty.count);
5638          Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
5639 	    l_current_atp);
5640       END IF;
5641     END IF;
5642   END IF;
5643 
5644   x_atp_info := l_current_atp;
5645   --diag_atp: we have already calculate this date.
5646   /*-- get the infinite time fence date if it exists
5647   l_infinite_time_fence_date := MSC_ATP_FUNC.get_infinite_time_fence_date(p_instance_id,
5648              p_inventory_item_id,p_organization_id, p_plan_id);
5649   */
5650 
5651   IF l_infinite_time_fence_date IS NOT NULL THEN
5652       -- add one more entry to indicate infinite time fence date and quantity.
5653       x_atp_info.atp_qty.EXTEND;
5654       --x_atp_info.limit_qty.EXTEND;
5655       x_atp_info.atp_period.EXTEND;
5656 
5657       i := x_atp_info.atp_qty.COUNT;
5658       x_atp_info.atp_period(i) := l_infinite_time_fence_date;
5659       x_atp_info.atp_qty(i) := MSC_ATP_PVT.INFINITE_NUMBER;
5660       --x_atp_info.limit_qty(i) := MSC_ATP_PVT.INFINITE_NUMBER;
5661 
5662 
5663       IF NVL(p_insert_flag, 0) <> 0 THEN
5664         -- add one more entry to indicate infinite time fence date and quantity.
5665 
5666         x_atp_period.Cumulative_Quantity := x_atp_info.atp_qty;
5667 
5668         j := x_atp_period.Level.COUNT;
5669         MSC_SATP_FUNC.Extend_Atp_Period(x_atp_period, l_return_status);
5670         j := j + 1;
5671         IF j > 1 THEN
5672           x_atp_period.Period_End_Date(j-1) := l_infinite_time_fence_date -1;
5673 	  -- dsting
5674           --x_atp_period.Identifier1(j) := x_atp_supply_demand.Identifier1(j-1);
5675           --x_atp_period.Identifier2(j) := x_atp_supply_demand.Identifier2(j-1);
5676           x_atp_period.Identifier1(j) := x_atp_period.Identifier1(j-1);
5677           x_atp_period.Identifier2(j) := x_atp_period.Identifier2(j-1);
5678         END IF;
5679 
5680         x_atp_period.Level(j) := p_level;
5681         x_atp_period.Identifier(j) := p_identifier;
5682         x_atp_period.Scenario_Id(j) := p_scenario_id;
5683         x_atp_period.Pegging_Id(j) := NULL;
5684         x_atp_period.End_Pegging_Id(j) := NULL;
5685         x_atp_period.Inventory_Item_Id(j) := p_inventory_item_id;
5686         x_atp_period.Request_Item_Id(j) := p_inventory_item_id;
5687         x_atp_period.Organization_id(j) := p_organization_id;
5688         x_atp_period.Period_Start_Date(j) := l_infinite_time_fence_date;
5689         x_atp_period.Total_Supply_Quantity(j) := MSC_ATP_PVT.INFINITE_NUMBER;
5690         x_atp_period.Total_Demand_Quantity(j) := 0;
5691         -- time_phased_atp
5692         x_atp_period.Total_Bucketed_Demand_Quantity(j) := 0;
5693         x_atp_period.Period_Quantity(j) := MSC_ATP_PVT.INFINITE_NUMBER;
5694         x_atp_period.Cumulative_Quantity(j) := MSC_ATP_PVT.INFINITE_NUMBER;
5695 
5696     END IF;
5697   END IF;
5698  --END IF;
5699 
5700 END Item_Alloc_Cum_Atp;
5701 
5702 PROCEDURE Res_Alloc_Cum_Atp(
5703 	p_plan_id 	      IN 	NUMBER,
5704 	p_level               IN 	NUMBER,
5705 	p_identifier          IN 	NUMBER,
5706 	p_scenario_id         IN 	NUMBER,
5707 	p_department_id       IN 	NUMBER,
5708 	p_resource_id         IN 	NUMBER,
5709 	p_organization_id     IN 	NUMBER,
5710 	p_instance_id         IN 	NUMBER,
5711 	p_demand_class        IN 	VARCHAR2,
5712 	p_request_date        IN 	DATE,
5713 	p_insert_flag         IN 	NUMBER,
5714         p_max_capacity        IN        NUMBER,
5715         p_batchable_flag      IN        NUMBER,
5716         p_res_conversion_rate IN        NUMBER,
5717         p_res_uom_type	      IN        NUMBER,
5718 	x_atp_info            OUT  	NoCopy MRP_ATP_PVT.ATP_Info,
5719 	x_atp_period          OUT  	NoCopy MRP_ATP_PUB.ATP_Period_Typ,
5720 	x_atp_supply_demand   OUT  	NoCopy MRP_ATP_PUB.ATP_Supply_Demand_Typ)
5721 IS
5722 l_infinite_time_fence_date	DATE;
5723 l_default_atp_rule_id           NUMBER;
5724 l_calendar_exception_set_id     NUMBER;
5725 l_level_id                      NUMBER;
5726 l_priority			NUMBER := 1;
5727 l_allocation_percent		NUMBER := 100;
5728 l_inv_item_id			NUMBER;
5729 l_null_num  			NUMBER := null;
5730 l_steal_period_quantity		NUMBER;
5731 l_demand_class			VARCHAR2(80);
5732 l_uom_code			VARCHAR2(3);
5733 l_null_char    			VARCHAR2(3) := null;
5734 l_return_status			VARCHAR(1);
5735 l_calendar_code                 VARCHAR2(14);
5736 l_default_demand_class          VARCHAR2(34);
5737 i				PLS_INTEGER;
5738 mm				PLS_INTEGER;
5739 ii                              PLS_INTEGER;
5740 jj                              PLS_INTEGER;
5741 j				PLS_INTEGER;
5742 k				PLS_INTEGER;
5743 l_demand_class_tab		MRP_ATP_PUB.char80_arr
5744                                    := MRP_ATP_PUB.char80_arr();
5745 l_demand_class_priority_tab	MRP_ATP_PUB.number_arr
5746                                    := MRP_ATP_PUB.number_arr();
5747 l_current_atp			MRP_ATP_PVT.ATP_Info;
5748 l_next_steal_atp		MRP_ATP_PVT.ATP_Info;
5749 l_null_steal_atp		MRP_ATP_PVT.ATP_Info;
5750 l_current_steal_atp             MRP_ATP_PVT.ATP_Info;
5751 l_temp_atp                      MRP_ATP_PVT.ATP_Info;
5752 l_optimized_plan                PLS_INTEGER := 2;
5753 
5754 -- 1680719
5755 l_class_tab                     MRP_ATP_PUB.char30_arr
5756                                     := MRP_ATP_PUB.char30_arr();
5757 l_partner_tab                   MRP_ATP_PUB.number_arr
5758                                     := MRP_ATP_PUB.number_arr();
5759 l_class_next_steal_atp          MRP_ATP_PVT.ATP_Info;
5760 l_partner_next_steal_atp        MRP_ATP_PVT.ATP_Info;
5761 l_class_curr_steal_atp          MRP_ATP_PVT.ATP_Info;
5762 l_partner_curr_steal_atp        MRP_ATP_PVT.ATP_Info;
5763 l_pos1                          NUMBER;
5764 l_pos2                          NUMBER;
5765 delim     constant varchar2(1) := fnd_global.local_chr(13);
5766 MSO_Batch_Flag                  VARCHAR2(1);
5767 l_constraint_plan               NUMBER;
5768 l_use_batching                  NUMBER;
5769 --krajan 04/01/02 added for fstealing
5770 l_org_code                      VARCHAR2(7);
5771 l_plan_start_date               date;
5772 
5773 -- dsting for s/d performance enh
5774 l_temp_atp_supply_demand     	MRP_ATP_PUB.ATP_Supply_Demand_Typ;
5775 
5776 -- ship_rec_cal
5777 l_sysdate                       DATE := trunc(sysdate); --4135752
5778 l_sys_next_date                 DATE; --bug3333114
5779 
5780 BEGIN
5781 
5782   IF PG_DEBUG in ('Y', 'C') THEN
5783      msc_sch_wb.atp_debug('******* Res_Alloc_Cum_Atp *******');
5784      msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'p_plan_id =' || p_plan_id);
5785      msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'p_instance_id =' || p_instance_id);
5786      msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'p_department_id =' || p_department_id);
5787      msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'p_resource_id =' || p_resource_id);
5788      msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'p_organization_id =' || p_organization_id);
5789      msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'p_demand_class =' || p_demand_class);
5790      msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'p_request_date =' || p_request_date );
5791   END IF;
5792 MSO_Batch_flag := NVL(fnd_profile.value('MSO_BATCHABLE_FLAG'),'N');
5793   IF PG_DEBUG in ('Y', 'C') THEN
5794      msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'MSO_Batch_flag := ' || MSO_Batch_flag);
5795   END IF;
5796   Begin
5797     SELECT DECODE(plans.plan_type, 4, 2,
5798              DECODE(daily_material_constraints, 1, 1,
5799                DECODE(daily_resource_constraints, 1, 1,
5800                  DECODE(weekly_material_constraints, 1, 1,
5801                    DECODE(weekly_resource_constraints, 1, 1,
5802                      DECODE(period_material_constraints, 1, 1,
5803                        DECODE(period_resource_constraints, 1, 1, 2)
5804                            )
5805                          )
5806                        )
5807                      )
5808                    )
5809                  ),
5810            DECODE(MSO_Batch_Flag, 'Y', DECODE(plans.plan_type, 4, 0,2,0,  -- filter out MPS plans
5811              DECODE(daily_material_constraints, 1, 1,
5812                DECODE(daily_resource_constraints, 1, 1,
5813                  DECODE(weekly_material_constraints, 1, 1,
5814                    DECODE(weekly_resource_constraints, 1, 1,
5815                      DECODE(period_material_constraints, 1, 1,
5816                        DECODE(period_resource_constraints, 1, 1, 2)
5817                            )
5818                          )
5819                        )
5820                      )
5821                    )
5822                  ), 0),
5823            trunc(plan_start_date)
5824 
5825     INTO   l_optimized_plan,l_constraint_plan, l_plan_start_date
5826     FROM   msc_designators desig,
5827            msc_plans plans
5828     WHERE  plans.plan_id = p_plan_id
5829     AND    desig.designator = plans.compile_designator
5830     AND    desig.sr_instance_id = plans.sr_instance_id
5831     AND    desig.organization_id = plans.organization_id;
5832     EXCEPTION WHEN NO_DATA_FOUND THEN
5833            l_optimized_plan := 2;
5834            l_constraint_plan := 0;
5835   END;
5836   IF PG_DEBUG in ('Y', 'C') THEN
5837      msc_sch_wb.atp_debug('Res_alloc_Cum_ATP: l_optimized_plan: ' || l_optimized_plan);
5838      msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'l_constraint_plan  =' || l_constraint_plan);
5839   END IF;
5840 
5841   IF (MSO_Batch_Flag = 'Y')  and (l_constraint_plan = 1) and (p_batchable_flag = 1) THEN
5842         IF PG_DEBUG in ('Y', 'C') THEN
5843            msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'Do Batching');
5844         END IF;
5845         l_use_batching := 1;
5846   ELSE
5847         IF PG_DEBUG in ('Y', 'C') THEN
5848            msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'No Batching');
5849         END IF;
5850         l_use_batching := 0;
5851   END IF;
5852 
5853 
5854   -- find all the demand classes that we need to take care: all the demand
5855   -- classes that have higher priority  + this requested demand class.
5856 
5857   -- Logic
5858   -- Step 1:
5859   -- 	FOR each demand class DCi, we need to
5860   --  	1. get the net daily availability
5861   --  	2. do backward consumption
5862   --  	3. do backward consumption if DC1 to DC(i-1) has any negative bucket
5863   -- 	END LOOP
5864   -- Step 2:
5865   --    do accumulation for the requested demand class
5866 
5867   -- select the priority  and allocation_percent for that item/demand class.
5868   -- if no data found, check if this item has a valid allocation rule.
5869   -- otherwise return error.
5870 
5871  -- If request is from view allocation for total ATP, assign
5872  -- l_priority = -1 and l_allocation_percent = NULL
5873  IF p_scenario_id <> -1 THEN
5874     MSC_AATP_PVT.Get_DC_Info(p_instance_id, null, p_organization_id,
5875         p_department_id, p_resource_id, p_demand_class,
5876         p_request_date, l_level_id, l_priority, l_allocation_percent, l_return_status);
5877 
5878     IF  l_return_status = FND_API.G_RET_STS_ERROR THEN
5879         IF PG_DEBUG in ('Y', 'C') THEN
5880            msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'Error retrieving Priority and Demand Class');
5881         END IF;
5882     END IF;
5883  ELSE
5884      l_priority := -1;
5885      l_allocation_percent := NULL;
5886  END IF;
5887 
5888   -- find the demand classes that have priority  higher (small number) than
5889   -- the requested demand class
5890 
5891 -- IF l_allocation_percent <> 0.0 THEN
5892 
5893   IF PG_DEBUG in ('Y', 'C') THEN
5894      msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'l_allocation_percent = '||l_allocation_percent);
5895   END IF;
5896   -- We don't need to select all demand classes in case this procdure is
5897   -- called from MSC_AATP_PVT.VIEW_ALLOCATION as we don't need to take care
5898   -- of any existing stealing.
5899  IF p_identifier <> -1 THEN
5900 
5901  IF PG_DEBUG in ('Y', 'C') THEN
5902     msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'before select the high priority demand class');
5903  END IF;
5904 
5905   -- bug 1680719
5906  --bug3948494 Do not select Higher priority DC if the requested DC
5907  --is at highest priority, we donot honor for forward consumption method here.
5908   IF l_level_id = -1 AND l_priority <> 1 THEN
5909 
5910 
5911     IF PG_DEBUG in ('Y', 'C') THEN
5912        msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'l_level_id = '||l_level_id);
5913     END IF;
5914     SELECT demand_class, priority
5915     BULK COLLECT INTO l_demand_class_tab, l_demand_class_priority_tab
5916     FROM   msc_resource_hierarchy_mv
5917     WHERE  department_id = p_department_id
5918     AND    resource_id = p_resource_id
5919     AND	   organization_id = p_organization_id
5920     AND    sr_instance_id = p_instance_id
5921     AND    p_request_date BETWEEN effective_date AND disable_date
5922     AND    priority  <= l_priority  -- 1665110, add '='
5923     AND    level_id = l_level_id
5924     ORDER BY priority asc, allocation_percent desc;
5925 
5926   ELSIF l_level_id = 1 THEN
5927 
5928     IF PG_DEBUG in ('Y', 'C') THEN
5929        msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'l_level_id = '||l_level_id);
5930     END IF;
5931     SELECT demand_class, priority
5932     BULK COLLECT INTO l_demand_class_tab, l_demand_class_priority_tab
5933     FROM   msc_resource_hierarchy_mv
5934     WHERE  department_id = p_department_id
5935     AND    resource_id = p_resource_id
5936     AND    organization_id = p_organization_id
5937     AND    sr_instance_id = p_instance_id
5938     AND    p_request_date BETWEEN effective_date AND disable_date
5939     AND    priority  <= l_priority  -- 1665110, add '='
5940     AND    level_id = l_level_id
5941     ORDER BY priority asc, class;
5942 
5943   ELSIF l_level_id = 2 THEN
5944 
5945 
5946     IF PG_DEBUG in ('Y', 'C') THEN
5947        msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'l_level_id = '||l_level_id);
5948     END IF;
5949     SELECT mv1.demand_class, mv1.priority, mv1.class, mv1.partner_id
5950     BULK COLLECT INTO l_demand_class_tab, l_demand_class_priority_tab,
5951                       l_class_tab, l_partner_tab
5952     FROM   msc_resource_hierarchy_mv mv1
5953     WHERE  mv1.department_id = p_department_id
5954     AND    mv1.resource_id = p_resource_id
5955     AND    mv1.organization_id = p_organization_id
5956     AND    mv1.sr_instance_id = p_instance_id
5957     AND    p_request_date BETWEEN mv1.effective_date AND mv1.disable_date
5958     --AND    mv1.priority  <= l_priority -- 1665110, add '='
5959     AND    mv1.level_id = l_level_id
5960     AND trunc(mv1.priority, -3) <= trunc(l_priority, -3)
5961       ORDER BY trunc(mv1.priority, -3), mv1.class ,
5962                trunc(mv1.priority, -2), mv1.partner_id;
5963 
5964   ELSIF l_level_id = 3 THEN
5965 
5966     -- bug 1680719
5967     -- we need to select the class, partner_id, partner_site_id
5968 
5969     IF PG_DEBUG in ('Y', 'C') THEN
5970        msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'l_level_id = '||l_level_id);
5971     END IF;
5972     SELECT mv1.demand_class, mv1.priority, mv1.class, mv1.partner_id
5973     BULK COLLECT INTO l_demand_class_tab, l_demand_class_priority_tab,
5974                       l_class_tab, l_partner_tab
5975     FROM   msc_resource_hierarchy_mv mv1
5976     WHERE  mv1.department_id = p_department_id
5977     AND    mv1.resource_id = p_resource_id
5978     AND    mv1.organization_id = p_organization_id
5979     AND    mv1.sr_instance_id = p_instance_id
5980     AND    p_request_date BETWEEN mv1.effective_date AND mv1.disable_date
5981     --AND    mv1.priority  <= l_priority -- 1665110, add '='
5982     AND    mv1.level_id = l_level_id
5983     AND trunc(mv1.priority, -3) <= trunc(l_priority, -3)
5984       ORDER BY trunc(mv1.priority, -3), mv1.class ,
5985                trunc(mv1.priority, -2), mv1.partner_id,
5986                mv1.priority, mv1.partner_site_id;
5987   END IF;
5988 
5989  -- Bug 1807827, need to add the requested demand class in case call is from
5990  -- View_Allocation, else l_demand_class_tab remains empty.
5991  END IF;
5992  --ELSE           -- p_scenario_id = -1 for View_Allocation
5993  IF l_demand_class_tab.count = 0 THEN
5994    --/* 1665110
5995      -- add the request demand class into the list
5996      l_demand_class_tab.Extend;
5997      l_demand_class_priority_tab.Extend;
5998      i := l_demand_class_tab.COUNT;
5999      l_demand_class_priority_tab(i) := l_priority;
6000      l_demand_class_tab(i) := p_demand_class;
6001 
6002      -- 1680719
6003      IF l_level_id in (2, 3) THEN
6004          l_class_tab.Extend;
6005          l_partner_tab.Extend;
6006          l_pos1 := instr(p_demand_class,delim,1,1);
6007          l_pos2 := instr(p_demand_class,delim,1,2);
6008          l_class_tab(i) := substr(p_demand_class,1,l_pos1-1);
6009          IF l_pos2 = 0 THEN
6010            l_partner_tab(i) := substr(p_demand_class,l_pos1+1);
6011          ELSE
6012            l_partner_tab(i) := substr(p_demand_class,l_pos1+1,l_pos2-l_pos1-1) ;
6013          END IF;
6014      END IF;
6015  END IF;
6016 --1665110 */
6017   mm := l_demand_class_tab.FIRST;
6018 
6019   WHILE mm is not null LOOP
6020      IF PG_DEBUG in ('Y', 'C') THEN
6021         msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'l_demand_class_tab and priority = '||
6022         l_demand_class_tab(mm) ||' : '|| l_demand_class_priority_tab(mm));
6023      END IF;
6024 
6025      IF l_level_id in (2,3) THEN
6026        IF PG_DEBUG in ('Y', 'C') THEN
6027           msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'l_class_tab and partner = '||
6028         l_class_tab(mm) ||' : '||l_partner_tab(mm));
6029        END IF;
6030      END IF;
6031 
6032      mm := l_demand_class_tab.Next(mm);
6033   END LOOP;
6034 
6035   l_uom_code := NVL(fnd_profile.value('MSC:HOUR_UOM_CODE'),
6036                     fnd_profile.value('BOM:HOUR_UOM_CODE'));
6037 
6038   -- for performance reason, we need to get the following info and
6039   -- store in variables instead of joining it
6040 
6041   /* Modularize Item and Org Info */
6042   -- changed call
6043   MSC_ATP_PROC.get_global_org_info(p_instance_id, p_organization_id);
6044   l_default_atp_rule_id := MSC_ATP_PVT.G_ORG_INFO_REC.default_atp_rule_id;
6045   l_calendar_code := MSC_ATP_PVT.G_ORG_INFO_REC.cal_code;
6046   l_calendar_exception_set_id := MSC_ATP_PVT.G_ORG_INFO_REC.cal_exception_set_id;
6047   l_default_demand_class := MSC_ATP_PVT.G_ORG_INFO_REC.default_demand_class;
6048   l_org_code := MSC_ATP_PVT.G_ORG_INFO_REC.org_code;
6049   /* Modularize Item and Org Info */
6050   --bug3333114 start
6051   l_sys_next_date := MSC_CALENDAR.NEXT_WORK_DAY (
6052                                         l_calendar_code,
6053                                         p_instance_id,
6054                                         TRUNC(sysdate));
6055 
6056   IF PG_DEBUG in ('Y', 'C') THEN
6057   msc_sch_wb.atp_debug('Sys next Date : '||to_char(l_sys_next_date, 'DD-MON-YYYY'));
6058   END IF;
6059 
6060   IF (l_sys_next_date is NULL) THEN
6061       msc_sch_wb.atp_debug('Sys Next Date is null');
6062       MSC_SCH_WB.G_ATP_ERROR_CODE := MSC_ATP_PVT.NO_MATCHING_CAL_DATE;
6063       RAISE FND_API.G_EXC_ERROR;
6064   END IF;
6065   --bug3333114 end
6066 
6067   IF PG_DEBUG in ('Y', 'C') THEN
6068      msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'l_default_atp_rule_id='|| l_default_atp_rule_id);
6069      msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'l_calendar_code='||l_calendar_code);
6070      msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'l_calendar_exception_set_id'|| l_calendar_exception_set_id);
6071      msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'l_default_demand_class'|| l_default_demand_class);
6072      msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'l_org_code'|| l_org_code);
6073   END IF;
6074 
6075   FOR i in 1..l_demand_class_tab.COUNT LOOP
6076           l_demand_class := l_demand_class_tab(i);
6077           IF PG_DEBUG in ('Y', 'C') THEN
6078              msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'in i loop, i = '||i);
6079              msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'l_demand_class = '||l_demand_class);
6080           END IF;
6081    -- get the daily net availability for DCi
6082    IF (NVL(p_insert_flag, 0) = 0  OR l_demand_class <> p_demand_class) THEN
6083        -- we don't want details
6084 
6085      IF (l_use_batching = 1) THEN
6086        IF (l_optimized_plan = 1) THEN -- Constrained Plan Bug 2809639
6087           IF PG_DEBUG in ('Y', 'C') THEN
6088               msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'Constrained Plan Batching');
6089           END IF;
6090        	  SELECT 	SD_DATE,
6091        			SUM(SD_QTY)
6092           BULK COLLECT INTO
6093                 	l_current_atp.atp_period,
6094                 	l_current_atp.atp_qty
6095           FROM (
6096           SELECT  -- C.CALENDAR_DATE SD_DATE, -- 2859130
6097                   -- Bug 3348095
6098                   -- For ATP created records use end_date otherwise start_date
6099                   GREATEST(DECODE(REQ.record_source, 2, TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
6100                            TRUNC(REQ.START_DATE)),l_sys_next_date) SD_DATE, --bug3333114
6101                 -1*DECODE(REQ.RESOURCE_ID, -1, REQ.LOAD_RATE,
6102                        DECODE(REQ.END_DATE, NULL, REQ.RESOURCE_HOURS,
6103                          DECODE(REQ.record_source, 2, REQ.RESOURCE_HOURS,
6104                                  REQ.DAILY_RESOURCE_HOURS))) *
6105                    -- For ATP created records use resource_hours
6106                    -- End Bug 3348095
6107                 ---resource batching
6108                 DECODE(DR.UOM_CLASS_TYPE, 1, I.UNIT_WEIGHT, 2, UNIT_VOLUME) *
6109                        NVL(MUC.CONVERSION_RATE, 1) * NVL(S.NEW_ORDER_QUANTITY, FIRM_QUANTITY) *
6110 
6111                         /*New*/
6112 		DECODE(DECODE(G_HIERARCHY_PROFILE,
6113                               --bug 2424357
6114                               1, DECODE(S.DEMAND_CLASS, null, null,
6115                                     DECODE(l_demand_class, '-1',
6116                                         MSC_AATP_FUNC.Get_RES_Hierarchy_Demand_Class(
6117                                                   null,
6118                                                   null,
6119                                                   p_department_id,
6120                                                   p_resource_id,
6121                                                   p_organization_id,
6122                                                   p_instance_id,
6123                                                   -- 2859130 c.calendar_date,
6124                                                   -- Bug 3348095
6125                                                   -- For ATP created records use end_date
6126                                                   -- otherwise start_date
6127                                                   DECODE(REQ.record_source, 2,
6128                                                     TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
6129                                                       TRUNC(REQ.START_DATE)),
6130                                                   --trunc(req.start_date),
6131                                                   -- End Bug 3348095
6132                                                   l_level_id,
6133                                                   S.DEMAND_CLASS), S.DEMAND_CLASS)),
6134                               2, DECODE(S.CUSTOMER_ID, NULL, TO_CHAR(NULL),
6135                                         0, TO_CHAR(NULL),
6136                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
6137                                                   S.CUSTOMER_ID,
6138                                                   S.SHIP_TO_SITE_ID,
6139                                                   s.inventory_item_id,
6140                                                   p_organization_id,
6141                                                   p_instance_id,
6142                                                   -- 2859130 c.calendar_date,
6143                                                   -- Bug 3348095
6144                                                   -- For ATP created records use end_date
6145                                                   -- otherwise start_date
6146                                                   DECODE(REQ.record_source, 2,
6147                                                     TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
6148                                                       TRUNC(REQ.START_DATE)),
6149                                                   --trunc(req.start_date),
6150                                                   -- End Bug 3348095
6151                                                   l_level_id,
6152                                                   NULL))),
6153 		       l_demand_class, 1,
6154                        --bug 4089293: If l_demand_class is not null and demand class is populated
6155                        -- on  supplies record then 0 should be allocated.
6156                        Decode (S.Demand_Class, NULL,
6157 		       MSC_AATP_FUNC.Get_Res_Demand_Alloc_Percent(
6158                          -- 2859130 C.CALENDAR_DATE,
6159                          -- Bug 3348095
6160                          -- For ATP created records use end_date
6161                          -- otherwise start_date
6162                          DECODE(REQ.record_source, 2,
6163                            TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
6164                              TRUNC(REQ.START_DATE)),
6165                          --trunc(req.start_date),
6166                          -- End Bug 3348095
6167                          REQ.ASSEMBLY_ITEM_ID,
6168 			 p_organization_id,
6169                          p_instance_id,
6170                          p_department_id,
6171 			 p_resource_id,
6172                          DECODE(G_HIERARCHY_PROFILE,
6173                                 --2424357
6174                                 1, DECODE(S.DEMAND_CLASS, null, null,
6175                                        DECODE(l_demand_class, -1,
6176                                           MSC_AATP_FUNC.Get_RES_Hierarchy_Demand_Class(
6177                                                   null,
6178                                                   null,
6179                                                   p_department_id,
6180                                                   p_resource_id,
6181                                                   p_organization_id,
6182                                                   p_instance_id,
6183                                                   -- 2859130 c.calendar_date,
6184                                                   -- Bug 3348095
6185                                                   -- For ATP created records use end_date
6186                                                   -- otherwise start_date
6187                                                   DECODE(REQ.record_source, 2,
6188                                                     TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
6189                                                       TRUNC(REQ.START_DATE)),
6190                                                   --trunc(req.start_date),
6191                                                   -- End Bug 3348095
6192                                                   l_level_id,
6193                                                   S.DEMAND_CLASS), S.DEMAND_CLASS)),
6194                                 2, DECODE(S.CUSTOMER_ID, NULL, l_demand_class,
6195                                           0, l_demand_class,
6196                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
6197                                                   S.CUSTOMER_ID,
6198                                                   S.SHIP_TO_SITE_ID,
6199                                                   l_inv_item_id,
6200                                                   p_organization_id,
6201                                                   p_instance_id,
6202                                                   -- 2859130 c.calendar_date,
6203                                                   -- Bug 3348095
6204                                                   -- For ATP created records use end_date
6205                                                   -- otherwise start_date
6206                                                   DECODE(REQ.record_source, 2,
6207                                                     TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
6208                                                       TRUNC(REQ.START_DATE)),
6209                                                   --trunc(req.start_date),
6210                                                   -- End Bug 3348095
6211                                                   l_level_id,
6212                                                   NULL))),
6213                          l_demand_class), 0)) SD_QTY
6214                         /*New*/
6215           FROM    MSC_DEPARTMENT_RESOURCES DR,
6216                   MSC_SUPPLIES S,
6217                   MSC_RESOURCE_REQUIREMENTS REQ,
6218                   -- 2859130 MSC_CALENDAR_DATES C,
6219                   MSC_SYSTEM_ITEMS I,
6220                   MSC_UOM_CONVERSIONS  MUC
6221           WHERE   DR.PLAN_ID = p_plan_id
6222           AND     NVL(DR.OWNING_DEPARTMENT_ID, DR.DEPARTMENT_ID)=p_department_id
6223           AND     DR.RESOURCE_ID = p_resource_id
6224           AND     DR.SR_INSTANCE_ID = p_instance_id
6225           AND     DR.ORGANIZATION_ID = p_organization_id -- for performance
6226           AND     REQ.PLAN_ID = DR.PLAN_ID
6227           AND     REQ.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
6228           AND     REQ.RESOURCE_ID = DR.RESOURCE_ID
6229           AND     REQ.DEPARTMENT_ID = DR.DEPARTMENT_ID
6230           AND     NVL(REQ.PARENT_ID, 1)  = 1  -- Bug 2809639
6231           AND     S.PLAN_ID = DR.PLAN_ID
6232           AND     S.TRANSACTION_ID = REQ.SUPPLY_ID
6233           AND     S.SR_INSTANCE_ID = REQ.SR_INSTANCE_ID --bug3948494
6234                   -- Exclude Cancelled Supplies 2460645
6235           AND     NVL(S.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
6236           AND     I.SR_INSTANCE_ID = REQ.SR_INSTANCE_Id
6237           AND     I.PLAN_ID = S.PLAN_ID
6238           AND     I.ORGANIZATION_ID = S.ORGANIZATION_ID
6239           AND     I.INVENTORY_ITEM_ID = S.INVENTORY_ITEM_ID
6240           -- Begin CTO Option Dependent Resources ODR
6241           AND     ((I.bom_item_type <> 1 and I.bom_item_type <> 2)
6242                -- bom_item_type not model and option_class always committed.
6243                     AND   (I.atp_flag <> 'N')
6244                -- atp_flag is 'Y' then committed.
6245                     OR    (REQ.record_source = 2) ) -- this OR may be changed during performance analysis.
6246               -- if record created by ATP then committed.
6247           -- End CTO Option Dependent Resources
6248           AND     DECODE(p_res_uom_type, 1, I.WEIGHT_UOM, 2 , I.VOLUME_UOM) = MUC.UOM_CODE (+)
6249           AND     MUC.SR_INSTANCE_ID (+)= I.SR_INSTANCE_ID
6250           AND     MUC.INVENTORY_ITEM_ID (+)= 0
6251           -- 2859130
6252           --AND     C.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
6253           --AND     C.CALENDAR_CODE = l_calendar_code
6254           --AND     C.EXCEPTION_SET_ID = l_calendar_exception_set_id
6255           --AND     C.CALENDAR_DATE = TRUNC(REQ.START_DATE) -- Bug 2809639
6256           --AND     C.SEQ_NUM IS NOT NULL
6257           ---bug 2341075: get data from plan_satrt date instead of sysdate
6258           --AND     C.CALENDAR_DATE >= trunc(sysdate)
6259           AND     trunc(REQ.START_DATE) >= l_plan_start_date --4135752
6260           UNION ALL
6261           SELECT  trunc(MNRA.SHIFT_DATE) SD_DATE, --4135752
6262                   MNRA.CAPACITY_UNITS * ((DECODE(LEAST(MNRA.from_time, MNRA.to_time),
6263                        MNRA.to_time,MNRA.to_time + 24*3600,
6264                        MNRA.to_time) - MNRA.from_time)/3600) *
6265                                               NVL((MRHM.allocation_percent/100), 1)
6266 /*
6267                                               NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
6268                 				     p_instance_id,
6269                 			             null,
6270                                                      p_organization_id,
6271                                                      p_department_id,
6272                                                      p_resource_id,
6273                                                      l_demand_class,
6274                                                      SHIFT_DATE),1) SD_QTY
6275 */
6276           FROM    MSC_NET_RESOURCE_AVAIL MNRA,
6277                   msc_resource_hierarchy_mv MRHM
6278           WHERE   MNRA.PLAN_ID = p_plan_id
6279           AND     NVL(MNRA.PARENT_ID, -2) <> -1
6280           AND     MNRA.SR_INSTANCE_ID = p_instance_id
6281           AND     MNRA.RESOURCE_ID = p_resource_id
6282           AND     MNRA.DEPARTMENT_ID = p_department_id
6283           ---bug 2341075; get data from plan_start date
6284           --AND     SHIFT_DATE >= trunc(sysdate)
6285           --bug 4232627: select only those records which are after plan start date
6286           AND     MNRA.SHIFT_DATE >= l_plan_start_date
6287           --bug 4089293
6288           AND     MNRA.organization_id = p_organization_id
6289           AND     MRHM.department_id (+) = MNRA.department_id
6290           AND     MRHM.resource_id  (+)= MNRA.resource_id
6291           AND     MRHM.organization_id (+) = MNRA.organization_id
6292           AND     MRHM.sr_instance_id  (+)= MNRA.sr_instance_id
6293           --AND     MRHM.level_id (+) = -1 --4365873
6294           AND     decode(MRHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
6295           --bug 4232627:
6296           --AND     MNRA.shift_date >=  GREATEST(l_plan_start_date,MRHM.effective_date (+))
6297           AND     trunc(MNRA.shift_date) >=  trunc(MRHM.effective_date (+))
6298           AND     trunc(MNRA.shift_date) <=  trunc(MRHM.disable_date (+))
6299           AND     MRHM.demand_class (+)= l_demand_class
6300           )
6301           GROUP BY SD_DATE
6302           ORDER BY SD_DATE;--4698199
6303        ELSE  -- now Other plans Bug 2809639
6304           IF PG_DEBUG in ('Y', 'C') THEN
6305               msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'Other Plans Batching');
6306           END IF;
6307        	  SELECT 	SD_DATE,
6308        			SUM(SD_QTY)
6309           BULK COLLECT INTO
6310                 	l_current_atp.atp_period,
6311                 	l_current_atp.atp_qty
6312           FROM (
6313           SELECT  GREATEST(C.CALENDAR_DATE,l_sys_next_date) SD_DATE, --bug3333114
6314                 -1*DECODE(REQ.RESOURCE_ID, -1, REQ.LOAD_RATE,
6315                        DECODE(REQ.END_DATE, NULL, REQ.RESOURCE_HOURS,
6316                           -- Bug 3348095
6317                           DECODE(REQ.record_source, 2, REQ.RESOURCE_HOURS,
6318                                  REQ.DAILY_RESOURCE_HOURS))) *
6319                           -- For ATP created records use resource_hours
6320                           -- End Bug 3348095
6321                 ---resource batching
6322                 DECODE(DR.UOM_CLASS_TYPE, 1, I.UNIT_WEIGHT, 2, UNIT_VOLUME) *
6323                        NVL(MUC.CONVERSION_RATE, 1) * NVL(S.NEW_ORDER_QUANTITY, FIRM_QUANTITY) *
6324 
6325                         /*New*/
6326 		DECODE(DECODE(G_HIERARCHY_PROFILE,
6327                               --bug 2424357
6328                               1, DECODE(S.DEMAND_CLASS, null, null,
6329                                     DECODE(l_demand_class, '-1',
6330                                         MSC_AATP_FUNC.Get_RES_Hierarchy_Demand_Class(
6331                                                   null,
6332                                                   null,
6333                                                   p_department_id,
6334                                                   p_resource_id,
6335                                                   p_organization_id,
6336                                                   p_instance_id,
6337                                                   c.calendar_date,
6338                                                   l_level_id,
6339                                                   S.DEMAND_CLASS), S.DEMAND_CLASS)),
6340                               2, DECODE(S.CUSTOMER_ID, NULL, TO_CHAR(NULL),
6341                                         0, TO_CHAR(NULL),
6342                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
6343                                                   S.CUSTOMER_ID,
6344                                                   S.SHIP_TO_SITE_ID,
6345                                                   s.inventory_item_id,
6346                                                   p_organization_id,
6347                                                   p_instance_id,
6348                                                   c.calendar_date,
6349                                                   l_level_id,
6350                                                   NULL))),
6351 		       l_demand_class, 1,
6352                        --bug 4156016: If l_demand_class is not null and demand class is populated
6353                        -- on  supplies record then 0 should be allocated.
6354                        Decode (S.Demand_Class, NULL,
6355 		       MSC_AATP_FUNC.Get_Res_Demand_Alloc_Percent(C.CALENDAR_DATE,
6356                          REQ.ASSEMBLY_ITEM_ID,
6357 			 p_organization_id,
6358                          p_instance_id,
6359                          p_department_id,
6360 			 p_resource_id,
6361                          DECODE(G_HIERARCHY_PROFILE,
6362                                 --2424357
6363                                 1, DECODE(S.DEMAND_CLASS, null, null,
6364                                        DECODE(l_demand_class, -1,
6365                                           MSC_AATP_FUNC.Get_RES_Hierarchy_Demand_Class(
6366                                                   null,
6367                                                   null,
6368                                                   p_department_id,
6369                                                   p_resource_id,
6370                                                   p_organization_id,
6371                                                   p_instance_id,
6372                                                   c.calendar_date,
6373                                                   l_level_id,
6374                                                   S.DEMAND_CLASS), S.DEMAND_CLASS)),
6375                                 2, DECODE(S.CUSTOMER_ID, NULL, l_demand_class,
6376                                           0, l_demand_class,
6377                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
6378                                                   S.CUSTOMER_ID,
6379                                                   S.SHIP_TO_SITE_ID,
6380                                                   l_inv_item_id,
6381                                                   p_organization_id,
6382                                                   p_instance_id,
6383                                                   c.calendar_date,
6384                                                   l_level_id,
6385                                                   NULL))),
6386                          l_demand_class), 0)) SD_QTY
6387                         /*New*/
6388           FROM    MSC_DEPARTMENT_RESOURCES DR,
6389                   MSC_SUPPLIES S,
6390                   MSC_RESOURCE_REQUIREMENTS REQ,
6391                   MSC_CALENDAR_DATES C,
6392                   MSC_SYSTEM_ITEMS I,
6393                   MSC_UOM_CONVERSIONS  MUC
6394           WHERE   DR.PLAN_ID = p_plan_id
6395           AND     NVL(DR.OWNING_DEPARTMENT_ID, DR.DEPARTMENT_ID)=p_department_id
6396           AND     DR.RESOURCE_ID = p_resource_id
6397           AND     DR.SR_INSTANCE_ID = p_instance_id
6398           AND     DR.ORGANIZATION_ID = p_organization_id -- for performance
6399           AND     REQ.PLAN_ID = DR.PLAN_ID
6400           AND     REQ.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
6401           AND     REQ.RESOURCE_ID = DR.RESOURCE_ID
6402           AND     REQ.DEPARTMENT_ID = DR.DEPARTMENT_ID
6403           AND     NVL(REQ.PARENT_ID, l_optimized_plan) = l_optimized_plan
6404           AND     S.PLAN_ID = DR.PLAN_ID
6405           AND     S.TRANSACTION_ID = REQ.SUPPLY_ID
6406           AND     S.SR_INSTANCE_ID = REQ.SR_INSTANCE_ID --bug3948494
6407                   -- Exclude Cancelled Supplies 2460645
6408           AND     NVL(S.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
6409           AND     I.SR_INSTANCE_ID = REQ.SR_INSTANCE_Id
6410           AND     I.PLAN_ID = S.PLAN_ID
6411           AND     I.ORGANIZATION_ID = S.ORGANIZATION_ID
6412           AND     I.INVENTORY_ITEM_ID = S.INVENTORY_ITEM_ID
6413           -- Begin CTO Option Dependent Resources ODR
6414           AND     ((I.bom_item_type <> 1 and I.bom_item_type <> 2)
6415                -- bom_item_type not model and option_class always committed.
6416                     AND   (I.atp_flag <> 'N')
6417                -- atp_flag is 'Y' then committed.
6418                     OR    (REQ.record_source = 2) ) -- this OR may be changed during performance analysis.
6419               -- if record created by ATP then committed.
6420           -- End CTO Option Dependent Resources
6421           AND     DECODE(p_res_uom_type, 1, I.WEIGHT_UOM, 2 , I.VOLUME_UOM) = MUC.UOM_CODE (+)
6422           AND     MUC.SR_INSTANCE_ID (+)= I.SR_INSTANCE_ID
6423           AND     MUC.INVENTORY_ITEM_ID (+)= 0
6424           AND     C.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
6425           AND     C.CALENDAR_CODE = l_calendar_code
6426           AND     C.EXCEPTION_SET_ID = l_calendar_exception_set_id
6427                   -- Bug 3348095
6428                   -- Ensure that the ATP created resource Reqs
6429                   -- do not get double counted.
6430          AND      C.CALENDAR_DATE BETWEEN DECODE(REQ.record_source, 2,
6431                        TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)), TRUNC(REQ.START_DATE))
6432                    AND TRUNC(NVL(REQ.END_DATE, REQ.START_DATE))
6433                   -- End Bug 3348095
6434           AND     C.SEQ_NUM IS NOT NULL
6435           ---bug 2341075: get data from plan_satrt date instead of sysdate
6436           --AND     C.CALENDAR_DATE >= trunc(sysdate)
6437           AND     C.CALENDAR_DATE >= l_plan_start_date
6438           UNION ALL
6439           SELECT  trunc(MNRA.SHIFT_DATE) SD_DATE, --4135752
6440                   MNRA.CAPACITY_UNITS * ((DECODE(LEAST(MNRA.from_time, MNRA.to_time),
6441                        MNRA.to_time,MNRA.to_time + 24*3600,
6442                        MNRA.to_time) - MNRA.from_time)/3600) *
6443                                               NVL((MRHM.allocation_percent/100), 1)
6444 /*
6445                                               NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
6446                 				     p_instance_id,
6447                 			             null,
6448                                                      p_organization_id,
6449                                                      p_department_id,
6450                                                      p_resource_id,
6451                                                      l_demand_class,
6452                                                      SHIFT_DATE),1) SD_QTY
6453 */
6454           FROM    MSC_NET_RESOURCE_AVAIL MNRA,
6455                   msc_resource_hierarchy_mv MRHM
6456           WHERE   MNRA.PLAN_ID = p_plan_id
6457           AND     NVL(MNRA.PARENT_ID, -2) <> -1
6458           AND     MNRA.SR_INSTANCE_ID = p_instance_id
6459           AND     MNRA.RESOURCE_ID = p_resource_id
6460           AND     MNRA.DEPARTMENT_ID = p_department_id
6461           ---bug 2341075; get data from plan_start date
6462           --AND     SHIFT_DATE >= trunc(sysdate)
6463           --bug 4232627: select only those records which are after plan start date
6464           AND     MNRA.SHIFT_DATE >= l_plan_start_date
6465           --bug 4156016
6466           AND     MNRA.organization_id = p_organization_id
6467           AND     MRHM.department_id (+) = MNRA.department_id
6468           AND     MRHM.resource_id  (+)= MNRA.resource_id
6469           AND     MRHM.organization_id (+) = MNRA.organization_id
6470           AND     MRHM.sr_instance_id  (+)= MNRA.sr_instance_id
6471           --AND     MRHM.level_id (+) = -1 --4365873
6472           AND     decode(MRHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
6473           --bug 4232627:
6474           AND     trunc(MNRA.shift_date) >=  trunc(GREATEST(l_plan_start_date,MRHM.effective_date (+))) --4135752
6475           AND     trunc(MNRA.shift_date) <=  trunc(MRHM.disable_date (+))--4135752
6476           AND     MRHM.demand_class (+)= l_demand_class
6477           )
6478           GROUP BY SD_DATE
6479           ORDER BY SD_DATE;--4698199
6480        END IF; -- l_optimized_plan = 1 Bug 2809639
6481      ELSE --- IF l_use_batching =1 THEN
6482 
6483        IF (l_optimized_plan = 1) THEN -- Constrained Plan Bug 2809639
6484           IF PG_DEBUG in ('Y', 'C') THEN
6485               msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'Constrained Plan No Batching');
6486           END IF;
6487        	  SELECT 	SD_DATE,
6488        			SUM(SD_QTY)
6489           BULK COLLECT INTO
6490                 	l_current_atp.atp_period,
6491                 	l_current_atp.atp_qty
6492           FROM (
6493           SELECT  -- C.CALENDAR_DATE SD_DATE, -- 2859130
6494                   -- Bug 3348095
6495                   -- For ATP created records use end_date otherwise start_date
6496                  GREATEST(DECODE(REQ.record_source, 2, TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
6497                            TRUNC(REQ.START_DATE)),l_sys_next_date) SD_DATE, --bug3333114
6498                 -1*DECODE(REQ.RESOURCE_ID, -1, REQ.LOAD_RATE,
6499                        DECODE(REQ.END_DATE, NULL, REQ.RESOURCE_HOURS,
6500                       -- Bug 3348095
6501                         DECODE(REQ.record_source, 2, REQ.RESOURCE_HOURS,
6502                                  REQ.DAILY_RESOURCE_HOURS))) *
6503                       -- For ATP created records use resource_hours
6504                       -- End Bug 3348095
6505                         /*New*/
6506 		DECODE(DECODE(G_HIERARCHY_PROFILE,
6507                               --2424357
6508                               1, DECODE(S.DEMAND_CLASS, null, null,
6509                                     DECODE(l_demand_class, '-1',
6510                                        MSC_AATP_FUNC.Get_RES_Hierarchy_Demand_Class(
6511                                                   null,
6512                                                   null,
6513                                                   p_department_id,
6514   				                  p_resource_id,
6515                                                   p_organization_id,
6516                                                   p_instance_id,
6517                                                   -- 2859130 c.calendar_date,
6518                                                   -- Bug 3348095
6519                                                   -- For ATP created records use end_date
6520                                                   -- otherwise start_date
6521                                                   DECODE(REQ.record_source, 2,
6522                                                     TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
6523                                                       TRUNC(REQ.START_DATE)),
6524                                                   --trunc(req.start_date),
6525                                                   -- End Bug 3348095
6526                                                   l_level_id,
6527                                                   S.DEMAND_CLASS),S.DEMAND_CLASS)),
6528                               2, DECODE(S.CUSTOMER_ID, NULL, TO_CHAR(NULL),
6529                                         0, TO_CHAR(NULL),
6530                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
6531                                                   S.CUSTOMER_ID,
6532                                                   S.SHIP_TO_SITE_ID,
6533                                                   s.inventory_item_id,
6534                                                   p_organization_id,
6535                                                   p_instance_id,
6536                                                   -- 2859130 c.calendar_date,
6537                                                   -- Bug 3348095
6538                                                   -- For ATP created records use end_date
6539                                                   -- otherwise start_date
6540                                                   DECODE(REQ.record_source, 2,
6541                                                     TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
6542                                                       TRUNC(REQ.START_DATE)),
6543                                                   --trunc(req.start_date),
6544                                                   -- End Bug 3348095
6545                                                   l_level_id,
6546                                                   NULL))),
6547 		       l_demand_class, 1,
6548                        --bug 4156016: If l_demand_class is not null and demand class is populated
6549                        -- on  supplies record then 0 should be allocated.
6550                        Decode (S.Demand_Class, NULL,
6551 		       MSC_AATP_FUNC.Get_Res_Demand_Alloc_Percent(
6552                          -- 2859130 C.CALENDAR_DATE,
6553                          -- Bug 3348095
6554                          -- For ATP created records use end_date
6555                          -- otherwise start_date
6556                          DECODE(REQ.record_source, 2,
6557                            TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
6558                              TRUNC(REQ.START_DATE)),
6559                          --trunc(req.start_date),
6560                          -- End Bug 3348095
6561                          REQ.ASSEMBLY_ITEM_ID,
6562 			 p_organization_id,
6563                          p_instance_id,
6564                          p_department_id,
6565 			 p_resource_id,
6566                          DECODE(G_HIERARCHY_PROFILE,
6567                                 --2424357
6568                                 1, DECODE(S.DEMAND_CLASS, null, null,
6569                                        DECODE(l_demand_class, '-1',
6570                                           MSC_AATP_FUNC.Get_RES_Hierarchy_Demand_Class(
6571                                                   null,
6572                                                   null,
6573                                                   p_department_id,
6574                                                   p_resource_id,
6575                                                   p_organization_id,
6576                                                   p_instance_id,
6577                                                   -- c.calendar_date,
6578                                                   -- Bug 3348095
6579                                                   -- For ATP created records use end_date
6580                                                   -- otherwise start_date
6581                                                   DECODE(REQ.record_source, 2,
6582                                                     TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
6583                                                       TRUNC(REQ.START_DATE)),
6584                                                   --trunc(req.start_date),
6585                                                   -- End Bug 3348095
6586                                                   l_level_id,
6587                                                   S.DEMAND_CLASS), S.DEMAND_CLASS)),
6588                                 2, DECODE(S.CUSTOMER_ID, NULL, l_demand_class,
6589                                           0, l_demand_class,
6590                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
6591                                                   S.CUSTOMER_ID,
6592                                                   S.SHIP_TO_SITE_ID,
6593                                                   l_inv_item_id,
6594                                                   p_organization_id,
6595                                                   p_instance_id,
6596                                                   -- 2859130 c.calendar_date,
6597                                                   -- Bug 3348095
6598                                                   -- For ATP created records use end_date
6599                                                   -- otherwise start_date
6600                                                   DECODE(REQ.record_source, 2,
6601                                                     TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
6602                                                       TRUNC(REQ.START_DATE)),
6603                                                   --trunc(req.start_date),
6604                                                   -- End Bug 3348095
6605                                                   l_level_id,
6606                                                   NULL))),
6607                          l_demand_class), 0)) SD_QTY
6608                         /*New*/
6609           FROM    MSC_DEPARTMENT_RESOURCES DR,
6610                   MSC_SUPPLIES S,
6611                   MSC_SYSTEM_ITEMS I,   -- CTO ODR
6612                   MSC_RESOURCE_REQUIREMENTS REQ
6613                   -- 2859130 MSC_CALENDAR_DATES C
6614           WHERE   DR.PLAN_ID = p_plan_id
6615           AND     NVL(DR.OWNING_DEPARTMENT_ID, DR.DEPARTMENT_ID)=p_department_id
6616           AND     DR.RESOURCE_ID = p_resource_id
6617           AND     DR.SR_INSTANCE_ID = p_instance_id
6618           AND     DR.ORGANIZATION_ID = p_organization_id -- for performance
6619           AND     REQ.PLAN_ID = DR.PLAN_ID
6620           AND     REQ.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
6621           AND     REQ.RESOURCE_ID = DR.RESOURCE_ID
6622           AND     REQ.DEPARTMENT_ID = DR.DEPARTMENT_ID
6623           AND     NVL(REQ.PARENT_ID, 1)  = 1 -- Bug 2809639
6624           -- CTO Option Dependent Resources ODR
6625           AND     I.SR_INSTANCE_ID = REQ.SR_INSTANCE_Id
6626           AND     I.PLAN_ID = REQ.PLAN_ID
6627           AND     I.ORGANIZATION_ID = REQ.ORGANIZATION_ID
6628           AND     I.INVENTORY_ITEM_ID = REQ.ASSEMBLY_ITEM_ID
6629           AND     ((I.bom_item_type <> 1 and I.bom_item_type <> 2)
6630                 -- bom_item_type not model and option_class always committed.
6631                      AND   (I.atp_flag <> 'N')
6632                 -- atp_flag is 'Y' then committed.
6633                      OR    (REQ.record_source = 2) ) -- this OR may be changed during performance analysis.
6634                -- if record created by ATP then committed.
6635           -- End CTO Option Dependent Resources
6636           AND     S.PLAN_ID = DR.PLAN_ID
6637           AND     S.TRANSACTION_ID = REQ.SUPPLY_ID
6638           AND     S.SR_INSTANCE_ID = REQ.SR_INSTANCE_ID --bug3948494
6639           -- Exclude Cancelled Supplies 2460645
6640           AND     NVL(S.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
6641 
6642           -- 2859130
6643           -- AND     C.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
6644           -- AND     C.CALENDAR_CODE = l_calendar_code
6645           -- AND     C.EXCEPTION_SET_ID = l_calendar_exception_set_id
6646           -- AND     C.CALENDAR_DATE = TRUNC(REQ.START_DATE) -- Bug 2809639
6647           -- AND     C.SEQ_NUM IS NOT NULL
6648           ---bug 2341075
6649           --AND     C.CALENDAR_DATE >= trunc(sysdate)
6650           AND     trunc(REQ.START_DATE) >= l_plan_start_date --4135752
6651           UNION ALL
6652           SELECT  trunc(MNRA.SHIFT_DATE) SD_DATE, --4135752
6653                   MNRA.CAPACITY_UNITS * ((DECODE(LEAST(MNRA.from_time, MNRA.to_time),
6654                        MNRA.to_time,MNRA.to_time + 24*3600,
6655                        MNRA.to_time) - MNRA.from_time)/3600) *
6656                                               NVL((MRHM.allocation_percent/100), 1)
6657                                                     /*
6658                                                      NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
6659                 				     p_instance_id,
6660                 			             null,
6661                                                      p_organization_id,
6662                                                      p_department_id,
6663                                                      p_resource_id,
6664                                                      l_demand_class,
6665                                                      SHIFT_DATE),1) SD_QTY */
6666           FROM    MSC_NET_RESOURCE_AVAIL MNRA,
6667                   msc_resource_hierarchy_mv MRHM
6668           WHERE   MNRA.PLAN_ID = p_plan_id
6669           AND     NVL(MNRA.PARENT_ID, -2) <> -1
6670           AND     MNRA.SR_INSTANCE_ID = p_instance_id
6671           AND     MNRA.RESOURCE_ID = p_resource_id
6672           AND     MNRA.DEPARTMENT_ID = p_department_id
6673           --bug 2341075
6674           --AND     SHIFT_DATE >= trunc(sysdate)
6675           --bug 4232627: select only those records which are after plan start date
6676           AND     MNRA.SHIFT_DATE >= l_plan_start_date
6677           --bug 4156016
6678           AND     MNRA.organization_id = p_organization_id
6679           AND     MRHM.department_id (+) = MNRA.department_id
6680           AND     MRHM.resource_id  (+)= MNRA.resource_id
6681           AND     MRHM.organization_id (+) = MNRA.organization_id
6682           AND     MRHM.sr_instance_id  (+)= MNRA.sr_instance_id
6683           --AND     MRHM.level_id (+) = -1 --4365873
6684           AND     decode(MRHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
6685            --bug 4232627:
6686           AND     trunc(MNRA.shift_date) >=  trunc(GREATEST(l_plan_start_date,MRHM.effective_date (+))) --4135752
6687           AND     trunc(MNRA.shift_date) <=  trunc(MRHM.disable_date (+)) --4135752
6688           AND     MRHM.demand_class (+)= l_demand_class
6689           )
6690           GROUP BY SD_DATE
6691           ORDER BY SD_DATE;--4698199
6692        ELSE  -- now Other plans Bug 2809639
6693           IF PG_DEBUG in ('Y', 'C') THEN
6694               msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'Other Plans No Batching');
6695           END IF;
6696        	  SELECT 	SD_DATE,
6697        			SUM(SD_QTY)
6698           BULK COLLECT INTO
6699                 	l_current_atp.atp_period,
6700                 	l_current_atp.atp_qty
6701           FROM (
6702           SELECT GREATEST(C.CALENDAR_DATE,l_sys_next_date) SD_DATE,--bug3333114
6703                 -1*DECODE(REQ.RESOURCE_ID, -1, REQ.LOAD_RATE,
6704                        DECODE(REQ.END_DATE, NULL, REQ.RESOURCE_HOURS,
6705                           -- Bug 3348095
6706                           DECODE(REQ.record_source, 2, REQ.RESOURCE_HOURS,
6707                                  REQ.DAILY_RESOURCE_HOURS))) *
6708                           -- For ATP created records use resource_hours
6709                           -- End Bug 3348095
6710                         /*New*/
6711 		DECODE(DECODE(G_HIERARCHY_PROFILE,
6712                               --2424357
6713                               1, DECODE(S.DEMAND_CLASS, null, null,
6714                                     DECODE(l_demand_class, '-1',
6715                                        MSC_AATP_FUNC.Get_RES_Hierarchy_Demand_Class(
6716                                                   null,
6717                                                   null,
6718                                                   p_department_id,
6719   				                  p_resource_id,
6720                                                   p_organization_id,
6721                                                   p_instance_id,
6722                                                   c.calendar_date,
6723                                                   l_level_id,
6724                                                   S.DEMAND_CLASS),S.DEMAND_CLASS)),
6725                               2, DECODE(S.CUSTOMER_ID, NULL, TO_CHAR(NULL),
6726                                         0, TO_CHAR(NULL),
6727                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
6728                                                   S.CUSTOMER_ID,
6729                                                   S.SHIP_TO_SITE_ID,
6730                                                   s.inventory_item_id,
6731                                                   p_organization_id,
6732                                                   p_instance_id,
6733                                                   c.calendar_date,
6734                                                   l_level_id,
6735                                                   NULL))),
6736 		       l_demand_class, 1,
6737                        --bug 4156016: If l_demand_class is not null and demand class is populated
6738                        -- on  supplies record then 0 should be allocated.
6739                        Decode (S.Demand_Class, NULL,
6740 		       MSC_AATP_FUNC.Get_Res_Demand_Alloc_Percent(C.CALENDAR_DATE,
6741                          REQ.ASSEMBLY_ITEM_ID,
6742 			 p_organization_id,
6743                          p_instance_id,
6744                          p_department_id,
6745 			 p_resource_id,
6746                          DECODE(G_HIERARCHY_PROFILE,
6747                                 --2424357
6748                                 1, DECODE(S.DEMAND_CLASS, null, null,
6749                                        DECODE(l_demand_class, '-1',
6750                                           MSC_AATP_FUNC.Get_RES_Hierarchy_Demand_Class(
6751                                                   null,
6752                                                   null,
6753                                                   p_department_id,
6754                                                   p_resource_id,
6755                                                   p_organization_id,
6756                                                   p_instance_id,
6757                                                   c.calendar_date,
6758                                                   l_level_id,
6759                                                   S.DEMAND_CLASS), S.DEMAND_CLASS)),
6760                                 2, DECODE(S.CUSTOMER_ID, NULL, l_demand_class,
6761                                           0, l_demand_class,
6762                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
6763                                                   S.CUSTOMER_ID,
6764                                                   S.SHIP_TO_SITE_ID,
6765                                                   l_inv_item_id,
6766                                                   p_organization_id,
6767                                                   p_instance_id,
6768                                                   c.calendar_date,
6769                                                   l_level_id,
6770                                                   NULL))),
6771                          l_demand_class), 0)) SD_QTY
6772                         /*New*/
6773           FROM    MSC_DEPARTMENT_RESOURCES DR,
6774                   MSC_SUPPLIES S,
6775                   MSC_SYSTEM_ITEMS I,   -- CTO ODR
6776                   MSC_RESOURCE_REQUIREMENTS REQ,
6777                   MSC_CALENDAR_DATES C
6778           WHERE   DR.PLAN_ID = p_plan_id
6779           AND     NVL(DR.OWNING_DEPARTMENT_ID, DR.DEPARTMENT_ID)=p_department_id
6780           AND     DR.RESOURCE_ID = p_resource_id
6781           AND     DR.SR_INSTANCE_ID = p_instance_id
6782           AND     DR.ORGANIZATION_ID = p_organization_id -- for performance
6783           AND     REQ.PLAN_ID = DR.PLAN_ID
6784           AND     REQ.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
6785           AND     REQ.RESOURCE_ID = DR.RESOURCE_ID
6786           AND     REQ.DEPARTMENT_ID = DR.DEPARTMENT_ID
6787           AND     NVL(REQ.PARENT_ID, l_optimized_plan) = l_optimized_plan
6788           -- CTO Option Dependent Resources ODR
6789           AND     I.SR_INSTANCE_ID = REQ.SR_INSTANCE_Id
6790           AND     I.PLAN_ID = REQ.PLAN_ID
6791           AND     I.ORGANIZATION_ID = REQ.ORGANIZATION_ID
6792           AND     I.INVENTORY_ITEM_ID = REQ.ASSEMBLY_ITEM_ID
6793           AND     ((I.bom_item_type <> 1 and I.bom_item_type <> 2)
6794                 -- bom_item_type not model and option_class always committed.
6795                      AND   (I.atp_flag <> 'N')
6796                 -- atp_flag is 'Y' then committed.
6797                      OR    (REQ.record_source = 2) ) -- this OR may be changed during performance analysis.
6798                -- if record created by ATP then committed.
6799           -- End CTO Option Dependent Resources
6800           AND     S.PLAN_ID = DR.PLAN_ID
6801           AND     S.TRANSACTION_ID = REQ.SUPPLY_ID
6802           AND     S.SR_INSTANCE_ID = REQ.SR_INSTANCE_ID --bug3948494
6803           -- Exclude Cancelled Supplies 2460645
6804           AND     NVL(S.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
6805 
6806           AND     C.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
6807           AND     C.CALENDAR_CODE = l_calendar_code
6808           AND     C.EXCEPTION_SET_ID = l_calendar_exception_set_id
6809                   -- Bug 3348095
6810                   -- Ensure that the ATP created resource Reqs
6811                   -- do not get double counted.
6812          AND      C.CALENDAR_DATE BETWEEN DECODE(REQ.record_source, 2,
6813                        TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)), TRUNC(REQ.START_DATE))
6814                    AND TRUNC(NVL(REQ.END_DATE, REQ.START_DATE))
6815                   -- End Bug 3348095
6816           AND     C.SEQ_NUM IS NOT NULL
6817           ---bug 2341075
6818           --AND     C.CALENDAR_DATE >= trunc(sysdate)
6819           AND     C.CALENDAR_DATE >= l_plan_start_date
6820           UNION ALL
6821           SELECT  trunc(MNRA.SHIFT_DATE) SD_DATE, --4135752
6822                   MNRA.CAPACITY_UNITS * ((DECODE(LEAST(MNRA.from_time, MNRA.to_time),
6823                        MNRA.to_time,MNRA.to_time + 24*3600,
6824                        MNRA.to_time) - MNRA.from_time)/3600) *
6825                                               NVL((MRHM.allocation_percent/100), 1)
6826 /*
6827                                               NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
6828                 				     p_instance_id,
6829                 			             null,
6830                                                      p_organization_id,
6831                                                      p_department_id,
6832                                                      p_resource_id,
6833                                                      l_demand_class,
6834                                                      SHIFT_DATE),1) SD_QTY
6835 */
6836           FROM    MSC_NET_RESOURCE_AVAIL MNRA,
6837                   msc_resource_hierarchy_mv MRHM
6838           WHERE   MNRA.PLAN_ID = p_plan_id
6839           AND     NVL(MNRA.PARENT_ID, -2) <> -1
6840           AND     MNRA.SR_INSTANCE_ID = p_instance_id
6841           AND     MNRA.RESOURCE_ID = p_resource_id
6842           AND     MNRA.DEPARTMENT_ID = p_department_id
6843           ---bug 2341075; get data from plan_start date
6844           --AND     SHIFT_DATE >= trunc(sysdate)
6845           --bug 4232627: select only those records which are after plan start date
6846           AND     MNRA.SHIFT_DATE >= l_plan_start_date
6847           --bug 4156016
6848           AND     MNRA.organization_id = p_organization_id
6849           AND     MRHM.department_id (+) = MNRA.department_id
6850           AND     MRHM.resource_id  (+)= MNRA.resource_id
6851           AND     MRHM.organization_id (+) = MNRA.organization_id
6852           AND     MRHM.sr_instance_id  (+)= MNRA.sr_instance_id
6853           --AND     MRHM.level_id (+) = -1 --4365873
6854           AND     decode(MRHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
6855           --bug 4232627:
6856           AND     trunc(MNRA.shift_date) >=  trunc(GREATEST(l_plan_start_date,MRHM.effective_date (+))) --4135752
6857           AND     trunc(MNRA.shift_date) <=  trunc(MRHM.disable_date (+)) --4135752
6858           AND     MRHM.demand_class (+)= l_demand_class
6859           )
6860           GROUP BY SD_DATE
6861           ORDER BY SD_DATE;--4698199
6862        END IF; -- l_optimized_plan = 1 Bug 2809639
6863       -- bug 1657855,  remove support for min alloc
6864       --l_current_atp.limit_qty := l_current_atp.atp_qty;
6865      END IF;
6866    ELSE
6867         -- IF (NVL(p_insert_flag, 0) <> 0  AND l_demand_class = p_demand_class)
6868         -- OR p_scenario_id = -1 - we want details
6869 
6870       -- 2792336
6871      MSC_ATP_DB_UTILS.Clear_SD_Details_Temp();
6872 
6873      IF (l_use_batching = 1) THEN
6874        IF (l_optimized_plan = 1) THEN -- Constrained Plan Bug 2809639
6875          IF PG_DEBUG in ('Y', 'C') THEN
6876               msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'Constrained Plan Batching Details');
6877          END IF;
6878 	 INSERT INTO msc_atp_sd_details_temp (
6879 		ATP_Level,
6880 		Order_line_id,
6881 		Scenario_Id,
6882 		Inventory_Item_Id,
6883 		Request_Item_Id,
6884 		Organization_Id,
6885 		Department_Id,
6886 		Resource_Id,
6887 		Supplier_Id,
6888 		Supplier_Site_Id,
6889 		From_Organization_Id,
6890 		From_Location_Id,
6891 		To_Organization_Id,
6892 		To_Location_Id,
6893 		Ship_Method,
6894 		UOM_code,
6895 		Supply_Demand_Type,
6896 		Supply_Demand_Source_Type,
6897 		Supply_Demand_Source_Type_Name,
6898 		Identifier1,
6899 		Identifier2,
6900 		Identifier3,
6901 		Identifier4,
6902 		Supply_Demand_Quantity,
6903 		Supply_Demand_Date,
6904 		Disposition_Type,
6905 		Disposition_Name,
6906 		Pegging_Id,
6907 		End_Pegging_Id,
6908 		creation_date,
6909 		created_by,
6910 		last_update_date,
6911 		last_updated_by,
6912 		last_update_login,
6913 		Unallocated_Quantity
6914 	)
6915 
6916 	(SELECT
6917     	 	p_level col1,
6918 		p_identifier col2,
6919                 p_scenario_id col3,
6920                 l_null_num col4 ,
6921                 l_null_num col5,
6922 		p_organization_id col6,
6923                 p_department_id col7,
6924                 p_resource_id col8,
6925                 l_null_num col9,
6926                 l_null_num col10,
6927                 l_null_num col11,
6928                 l_null_num col12,
6929                 l_null_num col13,
6930                 l_null_num col14,
6931 		l_null_char col15,
6932 		l_uom_code col16,
6933 		1 col17, -- demand
6934 		S.ORDER_TYPE col18,
6935                 l_null_char col19,
6936 		REQ.SR_INSTANCE_ID col20,
6937                 l_null_num col21,
6938 		REQ.TRANSACTION_ID col22,
6939 		l_null_num col23,
6940                 -1* DECODE(REQ.RESOURCE_ID, -1, REQ.LOAD_RATE,
6941                        DECODE(REQ.END_DATE, NULL, REQ.RESOURCE_HOURS,
6942                          -- Bug 3348095
6943                          DECODE(REQ.record_source, 2, REQ.RESOURCE_HOURS,
6944                                  REQ.DAILY_RESOURCE_HOURS))) *
6945                          -- For ATP created records use resource_hours
6946                          -- End Bug 3348095
6947                  ---- resource batching
6948                 DECODE(DR.UOM_CLASS_TYPE, 1, I.UNIT_WEIGHT, 2, UNIT_VOLUME) *
6949                 NVL(MUC.CONVERSION_RATE, 1) * NVL(S.NEW_ORDER_QUANTITY,S.FIRM_QUANTITY) *
6950 
6951                         /*New*/
6952                 DECODE(p_scenario_id, -1, 1,
6953                        DECODE(DECODE(G_HIERARCHY_PROFILE,
6954                               --2424357
6955                               1, DECODE(S.DEMAND_CLASS, null, null,
6956                                      DECODE(l_demand_class, '-1',
6957                                          MSC_AATP_FUNC.Get_RES_Hierarchy_Demand_Class(
6958                                                   null,
6959                                                   null,
6960                                                   p_department_id,
6961                                                   p_resource_id,
6962                                                   p_organization_id,
6963                                                   p_instance_id,
6964                                                   -- 2859130 c.calendar_date,
6965                                                   -- Bug 3348095
6966                                                   -- For ATP created records use end_date
6967                                                   -- otherwise start_date
6968                                                   DECODE(REQ.record_source, 2,
6969                                                     TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
6970                                                       TRUNC(REQ.START_DATE)),
6971                                                   --trunc(req.start_date),
6972                                                   -- End Bug 3348095
6973                                                   l_level_id,
6974                                                   S.DEMAND_CLASS), S.DEMAND_CLASS)),
6975                               2, DECODE(S.CUSTOMER_ID, NULL, TO_CHAR(NULL),
6976                                         0, TO_CHAR(NULL),
6977                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
6978                                                   S.CUSTOMER_ID,
6979                                                   S.SHIP_TO_SITE_ID,
6980                                                   s.inventory_item_id,
6981                                                   p_organization_id,
6982                                                   p_instance_id,
6983                                                   -- 2859130 c.calendar_date,
6984                                                   -- Bug 3348095
6985                                                   -- For ATP created records use end_date
6986                                                   -- otherwise start_date
6987                                                   DECODE(REQ.record_source, 2,
6988                                                     TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
6989                                                       TRUNC(REQ.START_DATE)),
6990                                                   --trunc(req.start_date),
6991                                                   -- End Bug 3348095
6992                                                   l_level_id,
6993                                                   NULL))),
6994                        l_demand_class, 1,
6995                        --bug 4156016: If l_demand_class is not null and demand class is populated
6996                        -- on  supplies record then 0 should be allocated.
6997                        Decode (S.Demand_Class, NULL,
6998                        MSC_AATP_FUNC.Get_Res_Demand_Alloc_Percent(
6999                          -- 2859130 C.CALENDAR_DATE,
7000                          -- Bug 3348095
7001                          -- For ATP created records use end_date
7002                          -- otherwise start_date
7003                          DECODE(REQ.record_source, 2,
7004                             TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
7005                                TRUNC(REQ.START_DATE)),
7006                          --trunc(req.start_date),
7007                          -- End Bug 3348095
7008                          REQ.ASSEMBLY_ITEM_ID,
7009                          p_organization_id,
7010                          p_instance_id,
7011                          p_department_id,
7012                          p_resource_id,
7013                          DECODE(G_HIERARCHY_PROFILE,
7014                                 ---2424357
7015                                 1, DECODE(S.DEMAND_CLASS, null, null,
7016                                       DECODE(l_demand_class, '-1',
7017                                           MSC_AATP_FUNC.Get_RES_Hierarchy_Demand_Class(
7018                                                   null,
7019                                                   null,
7020                                                   p_department_id,
7021                                                   p_resource_id,
7022                                                   p_organization_id,
7023                                                   p_instance_id,
7024                                                   -- 2859130 c.calendar_date,
7025                                                   -- Bug 3348095
7026                                                   -- For ATP created records use end_date
7027                                                   -- otherwise start_date
7028                                                   DECODE(REQ.record_source, 2,
7029                                                     TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
7030                                                       TRUNC(REQ.START_DATE)),
7031                                                   --trunc(req.start_date),
7032                                                   -- End Bug 3348095
7033                                                   l_level_id,
7034                                                    S.DEMAND_CLASS), S.DEMAND_CLASS)),
7035                                 2, DECODE(S.CUSTOMER_ID, NULL, l_demand_class,
7036                                           0, l_demand_class,
7037                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
7038                                                   S.CUSTOMER_ID,
7039                                                   S.SHIP_TO_SITE_ID,
7040                                                   l_inv_item_id,
7041                                                   p_organization_id,
7042                                                   p_instance_id,
7043                                                   --2859130 c.calendar_date,
7044                                                   -- Bug 3348095
7045                                                   -- For ATP created records use end_date
7046                                                   -- otherwise start_date
7047                                                   DECODE(REQ.record_source, 2,
7048                                                     TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
7049                                                       TRUNC(REQ.START_DATE)),
7050                                                   --trunc(req.start_date),
7051                                                   -- End Bug 3348095
7052                                                   l_level_id,
7053                                                   NULL))),
7054                          l_demand_class), 0))) col24,
7055                         /*New*/
7056 		-- 2859130 C.CALENDAR_DATE col25,
7057                 -- Bug 3348095
7058                 -- For ATP created records use end_date otherwise start_date
7059                 GREATEST(DECODE(REQ.record_source, 2,
7060                    TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) , TRUNC(REQ.START_DATE)),l_sys_next_date) col25,  --bug3333114
7061                 -- End Bug 3348095
7062                 -- TRUNC(req.start_date) col25,
7063                 l_null_num col26,
7064                 -- Bug 2771075. For Planned Orders, we will populate transaction_id
7065 		-- in the disposition_name column to be consistent with Planning.
7066 		-- S.ORDER_NUMBER col27,
7067 		DECODE(S.ORDER_TYPE, 5, to_char(S.TRANSACTION_ID), S.ORDER_NUMBER ) col27,
7068                 l_null_num col28,
7069                 l_null_num col29,
7070 		-- ship_rec_cal changes begin
7071 		l_sysdate,
7072 		G_USER_ID,
7073 		l_sysdate,
7074 		G_USER_ID,
7075 		G_USER_ID,
7076 		-- ship_rec_cal changes end
7077 		-- Unallocated_Quantity
7078                 -1* DECODE(REQ.RESOURCE_ID, -1, REQ.LOAD_RATE,
7079                        DECODE(REQ.END_DATE, NULL, REQ.RESOURCE_HOURS,
7080                           -- Bug 3348095
7081                           DECODE(REQ.record_source, 2, REQ.RESOURCE_HOURS,
7082                                  REQ.DAILY_RESOURCE_HOURS))) *
7083                           -- For ATP created records use resource_hours
7084                           -- End Bug 3348095
7085                  ---- resource batching
7086                 DECODE(DR.UOM_CLASS_TYPE, 1, I.UNIT_WEIGHT, 2, UNIT_VOLUME) *
7087                 NVL(MUC.CONVERSION_RATE, 1) * NVL(S.NEW_ORDER_QUANTITY,S.FIRM_QUANTITY)
7088          FROM   MSC_DEPARTMENT_RESOURCES DR,
7089                 MSC_SUPPLIES S,
7090                 MSC_RESOURCE_REQUIREMENTS REQ,
7091                 -- 2859130 MSC_CALENDAR_DATES C,
7092                 MSC_SYSTEM_ITEMS I,
7093                 MSC_UOM_CONVERSIONS MUC
7094          WHERE  DR.PLAN_ID = p_plan_id
7095          AND    NVL(DR.OWNING_DEPARTMENT_ID, DR.DEPARTMENT_ID)=p_department_id
7096          AND    DR.RESOURCE_ID = p_resource_id
7097          AND    DR.SR_INSTANCE_ID = p_instance_id
7098          AND    DR.ORGANIZATION_ID = p_organization_id -- for performance
7099          AND    REQ.PLAN_ID = DR.PLAN_ID
7100          AND    REQ.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
7101          AND    REQ.RESOURCE_ID = DR.RESOURCE_ID
7102          AND    REQ.DEPARTMENT_ID = DR.DEPARTMENT_ID
7103          AND    NVL(REQ.PARENT_ID, 1) = 1 -- Bug 2809639
7104          AND     I.SR_INSTANCE_ID = REQ.SR_INSTANCE_Id
7105          AND     I.PLAN_ID = REQ.PLAN_ID
7106          AND     I.ORGANIZATION_ID = REQ.ORGANIZATION_ID
7107          AND     I.INVENTORY_ITEM_ID = REQ.ASSEMBLY_ITEM_ID
7108           -- Begin CTO Option Dependent Resources ODR
7109           AND     ((I.bom_item_type <> 1 and I.bom_item_type <> 2)
7110                -- bom_item_type not model and option_class always committed.
7111                     AND   (I.atp_flag <> 'N')
7112                -- atp_flag is 'Y' then committed.
7113                     OR    (REQ.record_source = 2) ) -- this OR may be changed during performance analysis.
7114               -- if record created by ATP then committed.
7115          -- End CTO Option Dependent Resources
7116          AND     DECODE(p_res_uom_type, 1, I.WEIGHT_UOM, 2 , I.VOLUME_UOM) = MUC.UOM_CODE (+)
7117          AND     MUC.SR_INSTANCE_ID (+)= I.SR_INSTANCE_ID
7118          AND     MUC.INVENTORY_ITEM_ID (+)= 0
7119          AND    S.PLAN_ID = DR.PLAN_ID
7120          AND    S.TRANSACTION_ID = REQ.SUPPLY_ID
7121          AND    S.SR_INSTANCE_ID = REQ.SR_INSTANCE_ID --bug3948494
7122          -- Exclude Cancelled Supplies 2460645
7123          AND     NVL(S.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
7124          -- 2859130
7125          -- AND    C.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
7126          -- AND    C.CALENDAR_CODE = l_calendar_code
7127          -- AND    C.EXCEPTION_SET_ID = l_calendar_exception_set_id
7128          -- AND    C.CALENDAR_DATE = TRUNC(REQ.START_DATE) -- Bug 2809639
7129          -- AND    C.SEQ_NUM IS NOT NULL
7130          ---bug 2341075
7131          --AND    C.CALENDAR_DATE >= trunc(sysdate)
7132          -- AND    C.CALENDAR_DATE >= l_plan_start_date
7133          AND    trunc(req.start_date) >= l_plan_start_date --4135752
7134          UNION ALL
7135          SELECT p_level col1,
7136                 p_identifier col2,
7137                 p_scenario_id col3,
7138                 l_null_num col4 ,
7139                 l_null_num col5,
7140                 p_organization_id col6,
7141                 p_department_id col7,
7142                 p_resource_id col8,
7143                 l_null_num col9,
7144                 l_null_num col10,
7145                 l_null_num col11,
7146                 l_null_num col12,
7147                 l_null_num col13,
7148                 l_null_num col14,
7149                 l_null_char col15,
7150                 l_uom_code col16,
7151                 2 col17, -- supply
7152                 l_null_num col18,
7153                 l_null_char col19,
7154                 MNRA.SR_INSTANCE_ID col20,
7155                 l_null_num col21,
7156                 MNRA.TRANSACTION_ID col22,
7157                 l_null_num col23,
7158                 MNRA.CAPACITY_UNITS  * ((DECODE(LEAST(MNRA.from_time, MNRA.to_time),
7159                        MNRA.to_time,to_time + 24*3600,
7160                        MNRA.to_time) - MNRA.from_time)/3600) *
7161                 DECODE(p_scenario_id, -1, 1, NVL((MRHM.allocation_percent/100), 1)) col24,
7162                 --bug 4156016
7163 	        /*		NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
7164                                                      p_instance_id,
7165                                                      null,
7166                                                      p_organization_id,
7167                                                      p_department_id,
7168                                                      p_resource_id,
7169                                                      l_demand_class,
7170                                                      SHIFT_DATE),1)) col24, */
7171                 MNRA.SHIFT_DATE col25,
7172                 l_null_num col26,
7173                 l_null_char col27,
7174                 l_null_num col28,
7175 		l_null_num col29,
7176 		-- ship_rec_cal changes begin
7177 		l_sysdate,
7178 		G_USER_ID,
7179 		l_sysdate,
7180 		G_USER_ID,
7181 		G_USER_ID,
7182 		-- ship_rec_cal changes end
7183                 MNRA.CAPACITY_UNITS * p_max_capacity * p_res_conversion_rate * ((DECODE(LEAST(MNRA.from_time, MNRA.to_time),
7184                        MNRA.to_time,MNRA.to_time + 24*3600,
7185                        MNRA.to_time) - MNRA.from_time)/3600)
7186           FROM    MSC_NET_RESOURCE_AVAIL MNRA,
7187                   msc_resource_hierarchy_mv MRHM
7188           WHERE   MNRA.PLAN_ID = p_plan_id
7189           AND     NVL(MNRA.PARENT_ID, -2) <> -1
7190           AND     MNRA.SR_INSTANCE_ID = p_instance_id
7191           AND     MNRA.RESOURCE_ID = p_resource_id
7192           AND     MNRA.DEPARTMENT_ID = p_department_id
7193           --bug 2341075
7194           --AND     SHIFT_DATE >= trunc(sysdate)
7195           --bug 4232627: select only those records which are after plan start date
7196           AND     MNRA.SHIFT_DATE >= l_plan_start_date
7197           --bug 4156016
7198           AND     MNRA.organization_id = p_organization_id
7199           AND     MRHM.department_id (+) = MNRA.department_id
7200           AND     MRHM.resource_id  (+)= MNRA.resource_id
7201           AND     MRHM.organization_id (+) = MNRA.organization_id
7202           AND     MRHM.sr_instance_id  (+)= MNRA.sr_instance_id
7203           --AND     MRHM.level_id (+) = -1 --4365873
7204           AND     decode(MRHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
7205           --bug 4232627: select only those records which are after plan start date
7206           AND     trunc(MNRA.shift_date) >=  trunc(MRHM.effective_date (+))
7207           AND     trunc(MNRA.shift_date) <=  trunc(MRHM.disable_date (+))
7208           AND     MRHM.demand_class (+) = l_demand_class
7209 	)
7210 	; -- dsting removed order by col25;
7211        ELSE  -- now Other plans Bug 2809639
7212          IF PG_DEBUG in ('Y', 'C') THEN
7213               msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'Other Plans Batching Details');
7214          END IF;
7215 	 INSERT INTO msc_atp_sd_details_temp (
7216 		ATP_Level,
7217 		Order_line_id,
7218 		Scenario_Id,
7219 		Inventory_Item_Id,
7220 		Request_Item_Id,
7221 		Organization_Id,
7222 		Department_Id,
7223 		Resource_Id,
7224 		Supplier_Id,
7225 		Supplier_Site_Id,
7226 		From_Organization_Id,
7227 		From_Location_Id,
7228 		To_Organization_Id,
7229 		To_Location_Id,
7230 		Ship_Method,
7231 		UOM_code,
7232 		Supply_Demand_Type,
7233 		Supply_Demand_Source_Type,
7234 		Supply_Demand_Source_Type_Name,
7235 		Identifier1,
7236 		Identifier2,
7237 		Identifier3,
7238 		Identifier4,
7239 		Supply_Demand_Quantity,
7240 		Supply_Demand_Date,
7241 		Disposition_Type,
7242 		Disposition_Name,
7243 		Pegging_Id,
7244 		End_Pegging_Id,
7245 		creation_date,
7246 		created_by,
7247 		last_update_date,
7248 		last_updated_by,
7249 		last_update_login,
7250 		Unallocated_Quantity
7251 	)
7252 
7253 	(SELECT
7254     	 	p_level col1,
7255 		p_identifier col2,
7256                 p_scenario_id col3,
7257                 l_null_num col4 ,
7258                 l_null_num col5,
7259 		p_organization_id col6,
7260                 p_department_id col7,
7261                 p_resource_id col8,
7262                 l_null_num col9,
7263                 l_null_num col10,
7264                 l_null_num col11,
7265                 l_null_num col12,
7266                 l_null_num col13,
7267                 l_null_num col14,
7268 		l_null_char col15,
7269 		l_uom_code col16,
7270 		1 col17, -- demand
7271 		S.ORDER_TYPE col18,
7272                 l_null_char col19,
7273 		REQ.SR_INSTANCE_ID col20,
7274                 l_null_num col21,
7275 		REQ.TRANSACTION_ID col22,
7276 		l_null_num col23,
7277                 -1* DECODE(REQ.RESOURCE_ID, -1, REQ.LOAD_RATE,
7278                        DECODE(REQ.END_DATE, NULL, REQ.RESOURCE_HOURS,
7279                          -- Bug 3348095
7280                          DECODE(REQ.record_source, 2, REQ.RESOURCE_HOURS,
7281                                  REQ.DAILY_RESOURCE_HOURS))) *
7282                          -- For ATP created records use resource_hours
7283                          -- End Bug 3348095
7284                  ---- resource batching
7285                 DECODE(DR.UOM_CLASS_TYPE, 1, I.UNIT_WEIGHT, 2, UNIT_VOLUME) *
7286                 NVL(MUC.CONVERSION_RATE, 1) * NVL(S.NEW_ORDER_QUANTITY,S.FIRM_QUANTITY) *
7287 
7288                         /*New*/
7289                 DECODE(p_scenario_id, -1, 1,
7290                        DECODE(DECODE(G_HIERARCHY_PROFILE,
7291                               --2424357
7292                               1, DECODE(S.DEMAND_CLASS, null, null,
7293                                      DECODE(l_demand_class, '-1',
7294                                          MSC_AATP_FUNC.Get_RES_Hierarchy_Demand_Class(
7295                                                   null,
7296                                                   null,
7297                                                   p_department_id,
7298                                                   p_resource_id,
7299                                                   p_organization_id,
7300                                                   p_instance_id,
7301                                                   c.calendar_date,
7302                                                   l_level_id,
7303                                                   S.DEMAND_CLASS), S.DEMAND_CLASS)),
7304                               2, DECODE(S.CUSTOMER_ID, NULL, TO_CHAR(NULL),
7305                                         0, TO_CHAR(NULL),
7306                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
7307                                                   S.CUSTOMER_ID,
7308                                                   S.SHIP_TO_SITE_ID,
7309                                                   s.inventory_item_id,
7310                                                   p_organization_id,
7311                                                   p_instance_id,
7312                                                   c.calendar_date,
7313                                                   l_level_id,
7314                                                   NULL))),
7315                        l_demand_class, 1,
7316                        --bug 4156016: If l_demand_class is not null and demand class is populated
7317                        -- on  supplies record then 0 should be allocated.
7318                        Decode (S.Demand_Class, NULL,
7319                        MSC_AATP_FUNC.Get_Res_Demand_Alloc_Percent(
7320                          C.CALENDAR_DATE,
7321                          REQ.ASSEMBLY_ITEM_ID,
7322                          p_organization_id,
7323                          p_instance_id,
7324                          p_department_id,
7325                          p_resource_id,
7326                          DECODE(G_HIERARCHY_PROFILE,
7327                                 ---2424357
7328                                 1, DECODE(S.DEMAND_CLASS, null, null,
7329                                       DECODE(l_demand_class, '-1',
7330                                           MSC_AATP_FUNC.Get_RES_Hierarchy_Demand_Class(
7331                                                   null,
7332                                                   null,
7333                                                   p_department_id,
7334                                                   p_resource_id,
7335                                                   p_organization_id,
7336                                                   p_instance_id,
7337                                                   c.calendar_date,
7338                                                   l_level_id,
7339                                                    S.DEMAND_CLASS), S.DEMAND_CLASS)),
7340                                 2, DECODE(S.CUSTOMER_ID, NULL, l_demand_class,
7341                                           0, l_demand_class,
7342                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
7343                                                   S.CUSTOMER_ID,
7344                                                   S.SHIP_TO_SITE_ID,
7345                                                   l_inv_item_id,
7346                                                   p_organization_id,
7347                                                   p_instance_id,
7348                                                   c.calendar_date,
7349                                                   l_level_id,
7350                                                   NULL))),
7351                          l_demand_class), 0))) col24,
7352                         /*New*/
7353 		GREATEST(C.CALENDAR_DATE,l_sys_next_date) col25, --bug3333114
7354                 l_null_num col26,
7355                 -- Bug 2771075. For Planned Orders, we will populate transaction_id
7356 		-- in the disposition_name column to be consistent with Planning.
7357 		-- S.ORDER_NUMBER col27,
7358 		DECODE(S.ORDER_TYPE, 5, to_char(S.TRANSACTION_ID), S.ORDER_NUMBER ) col27,
7359                 l_null_num col28,
7360                 l_null_num col29,
7361 		-- ship_rec_cal changes begin
7362 		l_sysdate,
7363 		G_USER_ID,
7364 		l_sysdate,
7365 		G_USER_ID,
7366 		G_USER_ID,
7367 		-- ship_rec_cal changes end
7368 		-- Unallocated_Quantity
7369                 -1* DECODE(REQ.RESOURCE_ID, -1, REQ.LOAD_RATE,
7370                        DECODE(REQ.END_DATE, NULL, REQ.RESOURCE_HOURS,
7371                           -- Bug 3348095
7372                           DECODE(REQ.record_source, 2, REQ.RESOURCE_HOURS,
7373                                    REQ.DAILY_RESOURCE_HOURS))) *
7374                           -- For ATP created records use resource_hours
7375                           -- End Bug 3348095
7376                  ---- resource batching
7377                 DECODE(DR.UOM_CLASS_TYPE, 1, I.UNIT_WEIGHT, 2, UNIT_VOLUME) *
7378                 NVL(MUC.CONVERSION_RATE, 1) * NVL(S.NEW_ORDER_QUANTITY,S.FIRM_QUANTITY)
7379          FROM   MSC_DEPARTMENT_RESOURCES DR,
7380                 MSC_SUPPLIES S,
7381                 MSC_RESOURCE_REQUIREMENTS REQ,
7382                 MSC_CALENDAR_DATES C,
7383                 MSC_SYSTEM_ITEMS I,
7384                 MSC_UOM_CONVERSIONS MUC
7385          WHERE  DR.PLAN_ID = p_plan_id
7386          AND    NVL(DR.OWNING_DEPARTMENT_ID, DR.DEPARTMENT_ID)=p_department_id
7387          AND    DR.RESOURCE_ID = p_resource_id
7388          AND    DR.SR_INSTANCE_ID = p_instance_id
7389          AND    DR.ORGANIZATION_ID = p_organization_id -- for performance
7390          AND    REQ.PLAN_ID = DR.PLAN_ID
7391          AND    REQ.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
7392          AND    REQ.RESOURCE_ID = DR.RESOURCE_ID
7393          AND    REQ.DEPARTMENT_ID = DR.DEPARTMENT_ID
7394          AND    NVL(REQ.PARENT_ID, l_optimized_plan) = l_optimized_plan
7395          AND     I.SR_INSTANCE_ID = REQ.SR_INSTANCE_Id
7396          AND     I.PLAN_ID = REQ.PLAN_ID
7397          AND     I.ORGANIZATION_ID = REQ.ORGANIZATION_ID
7398          AND     I.INVENTORY_ITEM_ID = REQ.ASSEMBLY_ITEM_ID
7399           -- Begin CTO Option Dependent Resources ODR
7400           AND     ((I.bom_item_type <> 1 and I.bom_item_type <> 2)
7401                -- bom_item_type not model and option_class always committed.
7402                     AND   (I.atp_flag <> 'N')
7403                -- atp_flag is 'Y' then committed.
7404                     OR    (REQ.record_source = 2) ) -- this OR may be changed during performance analysis.
7405               -- if record created by ATP then committed.
7406          -- End CTO Option Dependent Resources
7407          AND     DECODE(p_res_uom_type, 1, I.WEIGHT_UOM, 2 , I.VOLUME_UOM) = MUC.UOM_CODE (+)
7408          AND     MUC.SR_INSTANCE_ID (+)= I.SR_INSTANCE_ID
7409          AND     MUC.INVENTORY_ITEM_ID (+)= 0
7410          AND    S.PLAN_ID = DR.PLAN_ID
7411          AND    S.TRANSACTION_ID = REQ.SUPPLY_ID
7412          AND    S.SR_INSTANCE_ID = REQ.SR_INSTANCE_ID --bug3948494
7413          -- Exclude Cancelled Supplies 2460645
7414          AND     NVL(S.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
7415          AND    C.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
7416          AND    C.CALENDAR_CODE = l_calendar_code
7417          AND    C.EXCEPTION_SET_ID = l_calendar_exception_set_id
7418                 -- Bug 3348095
7419                 -- Ensure that the ATP created resource Reqs
7420                 -- do not get double counted.
7421          AND     C.CALENDAR_DATE BETWEEN DECODE(REQ.record_source, 2,
7422                           TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)), TRUNC(REQ.START_DATE))
7423                    AND TRUNC(NVL(REQ.END_DATE, REQ.START_DATE))
7424                 -- End Bug 3348095
7425          AND    C.SEQ_NUM IS NOT NULL
7426          ---bug 2341075
7427          --AND    C.CALENDAR_DATE >= trunc(sysdate)
7428          AND    C.CALENDAR_DATE >= l_plan_start_date
7429          UNION ALL
7430          SELECT p_level col1,
7431                 p_identifier col2,
7432                 p_scenario_id col3,
7433                 l_null_num col4 ,
7434                 l_null_num col5,
7435                 p_organization_id col6,
7436                 p_department_id col7,
7437                 p_resource_id col8,
7438                 l_null_num col9,
7439                 l_null_num col10,
7440                 l_null_num col11,
7441                 l_null_num col12,
7442                 l_null_num col13,
7443                 l_null_num col14,
7444                 l_null_char col15,
7445                 l_uom_code col16,
7446                 2 col17, -- supply
7447                 l_null_num col18,
7448                 l_null_char col19,
7449                 MNRA.SR_INSTANCE_ID col20,
7450                 l_null_num col21,
7451                 MNRA.TRANSACTION_ID col22,
7452                 l_null_num col23,
7453                 MNRA.CAPACITY_UNITS  * ((DECODE(LEAST(MNRA.from_time, MNRA.to_time),
7454                        MNRA.to_time,to_time + 24*3600,
7455                        MNRA.to_time) - MNRA.from_time)/3600) *
7456                 DECODE(p_scenario_id, -1, 1, NVL((MRHM.allocation_percent/100), 1)) col24,
7457                 --bug 4156016
7458 	        /*		NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
7459                                                      p_instance_id,
7460                                                      null,
7461                                                      p_organization_id,
7462                                                      p_department_id,
7463                                                      p_resource_id,
7464                                                      l_demand_class,
7465                                                      SHIFT_DATE),1)) col24, */
7466                 MNRA.SHIFT_DATE col25,
7467                 l_null_num col26,
7468                 l_null_char col27,
7469                 l_null_num col28,
7470 		l_null_num col29,
7471 		-- ship_rec_cal changes begin
7472 		l_sysdate,
7473 		G_USER_ID,
7474 		l_sysdate,
7475 		G_USER_ID,
7476 		G_USER_ID,
7477 		-- ship_rec_cal changes end
7478                 MNRA.CAPACITY_UNITS * p_max_capacity * p_res_conversion_rate * ((DECODE(LEAST(MNRA.from_time, MNRA.to_time),
7479                        MNRA.to_time,MNRA.to_time + 24*3600,
7480                        MNRA.to_time) - MNRA.from_time)/3600)
7481           FROM    MSC_NET_RESOURCE_AVAIL MNRA,
7482                   msc_resource_hierarchy_mv MRHM
7483           WHERE   MNRA.PLAN_ID = p_plan_id
7484           AND     NVL(MNRA.PARENT_ID, -2) <> -1
7485           AND     MNRA.SR_INSTANCE_ID = p_instance_id
7486           AND     MNRA.RESOURCE_ID = p_resource_id
7487           AND     MNRA.DEPARTMENT_ID = p_department_id
7488           --bug 2341075
7489           --AND     SHIFT_DATE >= trunc(sysdate)
7490           --bug 4232627: select only those records which are after plan start date
7491           AND     MNRA.SHIFT_DATE >= l_plan_start_date
7492           --bug 4156016
7493           AND     MNRA.organization_id = p_organization_id
7494           AND     MRHM.department_id (+) = MNRA.department_id
7495           AND     MRHM.resource_id  (+)= MNRA.resource_id
7496           AND     MRHM.organization_id (+) = MNRA.organization_id
7497           AND     MRHM.sr_instance_id  (+)= MNRA.sr_instance_id
7498           --AND     MRHM.level_id (+) = -1 --4365873
7499           AND     decode(MRHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
7500           --bug 4232627:
7501           AND     trunc(MNRA.shift_date) >=  trunc(MRHM.effective_date (+))
7502           AND     trunc(MNRA.shift_date) <=  trunc(MRHM.disable_date (+))
7503           AND     MRHM.demand_class (+) = l_demand_class
7504 	)
7505 	; -- dsting removed order by col25;
7506        END IF; -- l_optimized_plan = 1 Bug 2809639
7507      ELSE
7508 
7509        IF (l_optimized_plan = 1) THEN -- Constrained Plan Bug 2809639
7510          IF PG_DEBUG in ('Y', 'C') THEN
7511               msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'Constrained Plan No Batching Details');
7512          END IF;
7513 	 INSERT INTO msc_atp_sd_details_temp (
7514 		ATP_Level,
7515 		Order_line_id,
7516 		Scenario_Id,
7517 		Inventory_Item_Id,
7518 		Request_Item_Id,
7519 		Organization_Id,
7520 		Department_Id,
7521 		Resource_Id,
7522 		Supplier_Id,
7523 		Supplier_Site_Id,
7524 		From_Organization_Id,
7525 		From_Location_Id,
7526 		To_Organization_Id,
7527 		To_Location_Id,
7528 		Ship_Method,
7529 		UOM_code,
7530 		Supply_Demand_Type,
7531 		Supply_Demand_Source_Type,
7532 		Supply_Demand_Source_Type_Name,
7533 		Identifier1,
7534 		Identifier2,
7535 		Identifier3,
7536 		Identifier4,
7537 		Supply_Demand_Quantity,
7538 		Supply_Demand_Date,
7539 		Disposition_Type,
7540 		Disposition_Name,
7541 		Pegging_Id,
7542 		End_Pegging_Id,
7543 		creation_date,
7544 		created_by,
7545 		last_update_date,
7546 		last_updated_by,
7547 		last_update_login,
7548 		Unallocated_Quantity
7549 	)
7550     	(SELECT
7551 		p_level col1,
7552 		p_identifier col2,
7553                 p_scenario_id col3,
7554                 l_null_num col4 ,
7555                 l_null_num col5,
7556 		p_organization_id col6,
7557                 p_department_id col7,
7558                 p_resource_id col8,
7559                 l_null_num col9,
7560                 l_null_num col10,
7561                 l_null_num col11,
7562                 l_null_num col12,
7563                 l_null_num col13,
7564                 l_null_num col14,
7565 		l_null_char col15,
7566 		l_uom_code col16,
7567 		1 col17, -- demand
7568 		S.ORDER_TYPE col18,
7569                 l_null_char col19,
7570 		REQ.SR_INSTANCE_ID col20,
7571                 l_null_num col21,
7572 		REQ.TRANSACTION_ID col22,
7573 		l_null_num col23,
7574                 -1* DECODE(REQ.RESOURCE_ID, -1, REQ.LOAD_RATE,
7575                        DECODE(REQ.END_DATE, NULL, REQ.RESOURCE_HOURS,
7576                          -- Bug 3348095
7577                          DECODE(REQ.record_source, 2, REQ.RESOURCE_HOURS,
7578                                  REQ.DAILY_RESOURCE_HOURS))) *
7579                          -- For ATP created records use resource_hours
7580                          -- End Bug 3348095
7581                         /*New*/
7582                 DECODE(p_scenario_id, -1, 1,
7583                        DECODE(DECODE(G_HIERARCHY_PROFILE,
7584                               --2424357
7585                               1, DECODE(S.DEMAND_CLASS, null, null,
7586                                     DECODE(l_demand_class, '-1',
7587                                        MSC_AATP_FUNC.Get_RES_Hierarchy_Demand_Class(
7588                                                   null,
7589                                                   null,
7590                                                   p_department_id,
7591                                                   p_resource_id,
7592                                                   p_organization_id,
7593                                                   p_instance_id,
7594                                                   -- 2859130 c.calendar_date,
7595                                                   -- Bug 3348095
7596                                                   -- For ATP created records use end_date
7597                                                   -- otherwise start_date
7598                                                   DECODE(REQ.record_source, 2,
7599                                                     TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
7600                                                       TRUNC(REQ.START_DATE)),
7601                                                   --trunc(req.start_date),
7602                                                   -- End Bug 3348095
7603                                                   l_level_id,
7604                                                   S.DEMAND_CLASS), S.DEMAND_CLASS)),
7605                               2, DECODE(S.CUSTOMER_ID, NULL, TO_CHAR(NULL),
7606                                         0, TO_CHAR(NULL),
7607                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
7608                                                   S.CUSTOMER_ID,
7609                                                   S.SHIP_TO_SITE_ID,
7610                                                   s.inventory_item_id,
7611                                                   p_organization_id,
7612                                                   p_instance_id,
7613                                                   -- 2859130 c.calendar_date,
7614                                                   -- Bug 3348095
7615                                                   -- For ATP created records use end_date
7616                                                   -- otherwise start_date
7617                                                   DECODE(REQ.record_source, 2,
7618                                                     TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
7619                                                       TRUNC(REQ.START_DATE)),
7620                                                   --trunc(req.start_date),
7621                                                   -- End Bug 3348095
7622                                                   l_level_id,
7623                                                   NULL))),
7624                        l_demand_class, 1,
7625                        --bug 4156016: If l_demand_class is not null and demand class is populated
7626                        -- on  supplies record then 0 should be allocated.
7627                        Decode (S.Demand_Class, NULL,
7628                        MSC_AATP_FUNC.Get_Res_Demand_Alloc_Percent(
7629                          -- 2859130 C.CALENDAR_DATE,
7630                          -- Bug 3348095
7631                          -- For ATP created records use end_date
7632                          -- otherwise start_date
7633                          DECODE(REQ.record_source, 2,
7634                             TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
7635                                 TRUNC(REQ.START_DATE)),
7636                          --trunc(req.start_date),
7637                          -- End Bug 3348095
7638                          REQ.ASSEMBLY_ITEM_ID,
7639                          p_organization_id,
7640                          p_instance_id,
7641                          p_department_id,
7642                          p_resource_id,
7643                          DECODE(G_HIERARCHY_PROFILE,
7644                                 --2424357
7645                                 1, DECODE(S.DEMAND_CLASS, null, null,
7646                                      DECODE(l_demand_class, '-1',
7647                                          MSC_AATP_FUNC.Get_RES_Hierarchy_Demand_Class(
7648                                                   null,
7649                                                   null,
7650                                                   p_department_id,
7651                                                   p_resource_id,
7652                                                   p_organization_id,
7653                                                   p_instance_id,
7654                                                   -- 2859130 c.calendar_date,
7655                                                   -- Bug 3348095
7656                                                   -- For ATP created records use end_date
7657                                                   -- otherwise start_date
7658                                                   DECODE(REQ.record_source, 2,
7659                                                     TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
7660                                                       TRUNC(REQ.START_DATE)),
7661                                                   --trunc(req.start_date),
7662                                                   -- End Bug 3348095
7663                                                   l_level_id,
7664                                                   S.DEMAND_CLASS), S.DEMAND_CLASS)),
7665                                 2, DECODE(S.CUSTOMER_ID, NULL, l_demand_class,
7666                                           0, l_demand_class,
7667                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
7668                                                   S.CUSTOMER_ID,
7669                                                   S.SHIP_TO_SITE_ID,
7670                                                   l_inv_item_id,
7671                                                   p_organization_id,
7672                                                   p_instance_id,
7673                                                   --2859130 c.calendar_date,
7674                                                   -- Bug 3348095
7675                                                   -- For ATP created records use end_date
7676                                                   -- otherwise start_date
7677                                                   DECODE(REQ.record_source, 2,
7678                                                     TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) ,
7679                                                       TRUNC(REQ.START_DATE)),
7680                                                   --trunc(req.start_date),
7681                                                   -- End Bug 3348095
7682                                                   l_level_id,
7683                                                   NULL))),
7684                          l_demand_class), 0))) col24,
7685                         /*New*/
7686 		-- 2859130 C.CALENDAR_DATE col25,
7687                 -- Bug 3348095
7688                 -- For ATP created records use end_date otherwise start_date
7689                 GREATEST(DECODE(REQ.record_source, 2,
7690                    TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)) , TRUNC(REQ.START_DATE)),l_sys_next_date) col25,  --bug3333114
7691                 -- End Bug 3348095
7692                 -- TRUNC(req.start_date) col25,
7693                 l_null_num col26,
7694 		-- Bug 2771075. For Planned Orders, we will populate transaction_id
7695 		-- in the disposition_name column to be consistent with Planning.
7696 		-- S.ORDER_NUMBER col27,
7697 		DECODE(S.ORDER_TYPE, 5, to_char(S.TRANSACTION_ID), S.ORDER_NUMBER) col27,
7698                 l_null_num col28,
7699                 l_null_num col29,
7700 		-- ship_rec_cal changes begin
7701 		l_sysdate,
7702 		G_USER_ID,
7703 		l_sysdate,
7704 		G_USER_ID,
7705 		G_USER_ID,
7706 		-- ship_rec_cal changes end
7707 		-- Unallocated_Quantity
7708                 -1* DECODE(REQ.RESOURCE_ID, -1, REQ.LOAD_RATE,
7709                        DECODE(REQ.END_DATE, NULL, REQ.RESOURCE_HOURS,
7710                           -- Bug 3348095
7711                           DECODE(REQ.record_source, 2, REQ.RESOURCE_HOURS,
7712                                  REQ.DAILY_RESOURCE_HOURS)))
7713                           -- For ATP created records use resource_hours
7714                           -- End Bug 3348095
7715          FROM   MSC_DEPARTMENT_RESOURCES DR,
7716                 MSC_SUPPLIES S,
7717                 MSC_SYSTEM_ITEMS I,  -- CTO ODR
7718                 MSC_RESOURCE_REQUIREMENTS REQ
7719                 -- 2859130 MSC_CALENDAR_DATES C
7720          WHERE  DR.PLAN_ID = p_plan_id
7721          AND    NVL(DR.OWNING_DEPARTMENT_ID, DR.DEPARTMENT_ID)=p_department_id
7722          AND    DR.RESOURCE_ID = p_resource_id
7723          AND    DR.SR_INSTANCE_ID = p_instance_id
7724          AND    DR.ORGANIZATION_ID = p_organization_id -- for performance
7725          AND    REQ.PLAN_ID = DR.PLAN_ID
7726          AND    REQ.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
7727          AND    REQ.RESOURCE_ID = DR.RESOURCE_ID
7728          AND    REQ.DEPARTMENT_ID = DR.DEPARTMENT_ID
7729          AND    NVL(REQ.PARENT_ID, 1) = 1 -- parent_id is 1 for constrained plans. Bug 2809639
7730          -- CTO Option Dependent Resources ODR
7731          AND     I.SR_INSTANCE_ID = REQ.SR_INSTANCE_Id
7732          AND     I.PLAN_ID = REQ.PLAN_ID
7733          AND     I.ORGANIZATION_ID = REQ.ORGANIZATION_ID
7734          AND     I.INVENTORY_ITEM_ID = REQ.ASSEMBLY_ITEM_ID
7735          AND     ((I.bom_item_type <> 1 and I.bom_item_type <> 2)
7736                -- bom_item_type not model and option_class always committed.
7737                     AND   (I.atp_flag <> 'N')
7738                -- atp_flag is 'Y' then committed.
7739                     OR    (REQ.record_source = 2) ) -- this OR may be changed during performance analysis.
7740               -- if record created by ATP then committed.
7741          -- End CTO Option Dependent Resources
7742          AND    S.PLAN_ID = DR.PLAN_ID
7743          AND    S.TRANSACTION_ID = REQ.SUPPLY_ID
7744          AND    S.SR_INSTANCE_ID = REQ.SR_INSTANCE_ID --bug3948494
7745                 -- Exclude Cancelled Supplies 2460645
7746          AND     NVL(S.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
7747          -- 2859130
7748          -- AND    C.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
7749          -- AND    C.CALENDAR_CODE = l_calendar_code
7750          -- AND    C.EXCEPTION_SET_ID = l_calendar_exception_set_id
7751          -- AND    C.CALENDAR_DATE = TRUNC(REQ.START_DATE) -- Bug 2809639
7752          -- AND    C.SEQ_NUM IS NOT NULL
7753          ---bug 2341075
7754          --AND    C.CALENDAR_DATE >= trunc(sysdate)
7755          -- AND    C.CALENDAR_DATE >= l_plan_start_date
7756          AND    trunc(req.start_date) >= l_plan_start_date --4135752
7757          UNION ALL
7758          SELECT p_level col1,
7759                 p_identifier col2,
7760                 p_scenario_id col3,
7761                 l_null_num col4 ,
7762                 l_null_num col5,
7763                 p_organization_id col6,
7764                 p_department_id col7,
7765                 p_resource_id col8,
7766                 l_null_num col9,
7767                 l_null_num col10,
7768                 l_null_num col11,
7769                 l_null_num col12,
7770                 l_null_num col13,
7771                 l_null_num col14,
7772                 l_null_char col15,
7773                 l_uom_code col16,
7774                 2 col17, -- supply
7775                 l_null_num col18,
7776                 l_null_char col19,
7777                 MNRA.SR_INSTANCE_ID col20,
7778                 l_null_num col21,
7779                 MNRA.TRANSACTION_ID col22,
7780                 l_null_num col23,
7781                 MNRA.CAPACITY_UNITS  * ((DECODE(LEAST(MNRA.from_time, MNRA.to_time),
7782                        MNRA.to_time,to_time + 24*3600,
7783                        MNRA.to_time) - MNRA.from_time)/3600) *
7784                 DECODE(p_scenario_id, -1, 1, NVL((MRHM.allocation_percent/100), 1)) col24,
7785                 --bug 4156016
7786 	        /*		NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
7787                                                      p_instance_id,
7788                                                      null,
7789                                                      p_organization_id,
7790                                                      p_department_id,
7791                                                      p_resource_id,
7792                                                      l_demand_class,
7793                                                      SHIFT_DATE),1)) col24, */
7794                 MNRA.SHIFT_DATE col25,
7795                 l_null_num col26,
7796                 l_null_char col27,
7797                 l_null_num col28,
7798 		l_null_num col29,
7799 		-- ship_rec_cal changes begin
7800 		l_sysdate,
7801 		G_USER_ID,
7802 		l_sysdate,
7803 		G_USER_ID,
7804 		G_USER_ID,
7805 		-- ship_rec_cal changes end
7806 		-- Unallocated_Quantity
7807                 MNRA.CAPACITY_UNITS  * ((DECODE(LEAST(MNRA.from_time, MNRA.to_time),
7808                        MNRA.to_time,MNRA.to_time + 24*3600,
7809                        MNRA.to_time) - MNRA.from_time)/3600)
7810           FROM    MSC_NET_RESOURCE_AVAIL MNRA,
7811                   msc_resource_hierarchy_mv MRHM
7812           WHERE   MNRA.PLAN_ID = p_plan_id
7813           AND     NVL(MNRA.PARENT_ID, -2) <> -1
7814           AND     MNRA.SR_INSTANCE_ID = p_instance_id
7815           AND     MNRA.RESOURCE_ID = p_resource_id
7816           AND     MNRA.DEPARTMENT_ID = p_department_id
7817           --bug 2341075
7818           --AND     SHIFT_DATE >= trunc(sysdate)
7819           --bug 4232627: select only those records which are after plan start date
7820           AND     MNRA.SHIFT_DATE >= l_plan_start_date
7821           --bug 4156016
7822           AND     MNRA.organization_id = p_organization_id
7823           AND     MRHM.department_id (+) = MNRA.department_id
7824           AND     MRHM.resource_id  (+)= MNRA.resource_id
7825           AND     MRHM.organization_id (+) = MNRA.organization_id
7826           AND     MRHM.sr_instance_id  (+)= MNRA.sr_instance_id
7827           --AND     MRHM.level_id (+) = -1 --4365873
7828           AND     decode(MRHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
7829           --bug 4232627:
7830           AND     trunc(MNRA.shift_date) >=  trunc(MRHM.effective_date (+))
7831           AND     trunc(MNRA.shift_date) <=  trunc(MRHM.disable_date (+))
7832           AND     MRHM.demand_class (+) = l_demand_class
7833 	)
7834 	; -- dsting removed order by col25;
7835        ELSE  -- now Other plans Bug 2809639
7836          IF PG_DEBUG in ('Y', 'C') THEN
7837               msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'Other Plans No Batching Details');
7838          END IF;
7839 	 INSERT INTO msc_atp_sd_details_temp (
7840 		ATP_Level,
7841 		Order_line_id,
7842 		Scenario_Id,
7843 		Inventory_Item_Id,
7844 		Request_Item_Id,
7845 		Organization_Id,
7846 		Department_Id,
7847 		Resource_Id,
7848 		Supplier_Id,
7849 		Supplier_Site_Id,
7850 		From_Organization_Id,
7851 		From_Location_Id,
7852 		To_Organization_Id,
7853 		To_Location_Id,
7854 		Ship_Method,
7855 		UOM_code,
7856 		Supply_Demand_Type,
7857 		Supply_Demand_Source_Type,
7858 		Supply_Demand_Source_Type_Name,
7859 		Identifier1,
7860 		Identifier2,
7861 		Identifier3,
7862 		Identifier4,
7863 		Supply_Demand_Quantity,
7864 		Supply_Demand_Date,
7865 		Disposition_Type,
7866 		Disposition_Name,
7867 		Pegging_Id,
7868 		End_Pegging_Id,
7869 		creation_date,
7870 		created_by,
7871 		last_update_date,
7872 		last_updated_by,
7873 		last_update_login,
7874 		Unallocated_Quantity
7875 	)
7876     	(SELECT
7877 		p_level col1,
7878 		p_identifier col2,
7879                 p_scenario_id col3,
7880                 l_null_num col4 ,
7881                 l_null_num col5,
7882 		p_organization_id col6,
7883                 p_department_id col7,
7884                 p_resource_id col8,
7885                 l_null_num col9,
7886                 l_null_num col10,
7887                 l_null_num col11,
7888                 l_null_num col12,
7889                 l_null_num col13,
7890                 l_null_num col14,
7891 		l_null_char col15,
7892 		l_uom_code col16,
7893 		1 col17, -- demand
7894 		S.ORDER_TYPE col18,
7895                 l_null_char col19,
7896 		REQ.SR_INSTANCE_ID col20,
7897                 l_null_num col21,
7898 		REQ.TRANSACTION_ID col22,
7899 		l_null_num col23,
7900                 -1* DECODE(REQ.RESOURCE_ID, -1, REQ.LOAD_RATE,
7901                        DECODE(REQ.END_DATE, NULL, REQ.RESOURCE_HOURS,
7902                           -- Bug 3348095
7903                           DECODE(REQ.record_source, 2, REQ.RESOURCE_HOURS,
7904                                    REQ.DAILY_RESOURCE_HOURS))) *
7905                           -- For ATP created records use resource_hours
7906                           -- End Bug 3348095
7907                         /*New*/
7908                 DECODE(p_scenario_id, -1, 1,
7909                        DECODE(DECODE(G_HIERARCHY_PROFILE,
7910                               --2424357
7911                               1, DECODE(S.DEMAND_CLASS, null, null,
7912                                     DECODE(l_demand_class, '-1',
7913                                        MSC_AATP_FUNC.Get_RES_Hierarchy_Demand_Class(
7914                                                   null,
7915                                                   null,
7916                                                   p_department_id,
7917                                                   p_resource_id,
7918                                                   p_organization_id,
7919                                                   p_instance_id,
7920                                                   c.calendar_date,
7921                                                   l_level_id,
7922                                                   S.DEMAND_CLASS), S.DEMAND_CLASS)),
7923                               2, DECODE(S.CUSTOMER_ID, NULL, TO_CHAR(NULL),
7924                                         0, TO_CHAR(NULL),
7925                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
7926                                                   S.CUSTOMER_ID,
7927                                                   S.SHIP_TO_SITE_ID,
7928                                                   s.inventory_item_id,
7929                                                   p_organization_id,
7930                                                   p_instance_id,
7931                                                   c.calendar_date,
7932                                                   l_level_id,
7933                                                   NULL))),
7934                        l_demand_class, 1,
7935                        --bug 4156016: If l_demand_class is not null and demand class is populated
7936                        -- on  supplies record then 0 should be allocated.
7937                        Decode (S.Demand_Class, NULL,
7938                        MSC_AATP_FUNC.Get_Res_Demand_Alloc_Percent(C.CALENDAR_DATE,
7939                          REQ.ASSEMBLY_ITEM_ID,
7940                          p_organization_id,
7941                          p_instance_id,
7942                          p_department_id,
7943                          p_resource_id,
7944                          DECODE(G_HIERARCHY_PROFILE,
7945                                 --2424357
7946                                 1, DECODE(S.DEMAND_CLASS, null, null,
7947                                      DECODE(l_demand_class, '-1',
7948                                          MSC_AATP_FUNC.Get_RES_Hierarchy_Demand_Class(
7949                                                   null,
7950                                                   null,
7951                                                   p_department_id,
7952                                                   p_resource_id,
7953                                                   p_organization_id,
7954                                                   p_instance_id,
7955                                                   c.calendar_date,
7956                                                   l_level_id,
7957                                                   S.DEMAND_CLASS), S.DEMAND_CLASS)),
7958                                 2, DECODE(S.CUSTOMER_ID, NULL, l_demand_class,
7959                                           0, l_demand_class,
7960                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
7961                                                   S.CUSTOMER_ID,
7962                                                   S.SHIP_TO_SITE_ID,
7963                                                   l_inv_item_id,
7964                                                   p_organization_id,
7965                                                   p_instance_id,
7966                                                   c.calendar_date,
7967                                                   l_level_id,
7968                                                   NULL))),
7969                          l_demand_class), 0))) col24,
7970                         /*New*/
7971 		GREATEST(C.CALENDAR_DATE,l_sys_next_date) col25, --bug3333114
7972                 l_null_num col26,
7973 		-- Bug 2771075. For Planned Orders, we will populate transaction_id
7974 		-- in the disposition_name column to be consistent with Planning.
7975 		-- S.ORDER_NUMBER col27,
7976 		DECODE(S.ORDER_TYPE, 5, to_char(S.TRANSACTION_ID), S.ORDER_NUMBER) col27,
7977                 l_null_num col28,
7978                 l_null_num col29,
7979 		-- ship_rec_cal changes begin
7980 		l_sysdate,
7981 		G_USER_ID,
7982 		l_sysdate,
7983 		G_USER_ID,
7984 		G_USER_ID,
7985 		-- ship_rec_cal changes end
7986 		-- Unallocated_Quantity
7987                 -1* DECODE(REQ.RESOURCE_ID, -1, REQ.LOAD_RATE,
7988                        DECODE(REQ.END_DATE, NULL, REQ.RESOURCE_HOURS,
7989                           -- Bug 3348095
7990                           DECODE(REQ.record_source, 2, REQ.RESOURCE_HOURS,
7991                                    REQ.DAILY_RESOURCE_HOURS)))
7992                           -- For ATP created records use resource_hours
7993                           -- End Bug 3348095
7994          FROM   MSC_DEPARTMENT_RESOURCES DR,
7995                 MSC_SUPPLIES S,
7996                 MSC_SYSTEM_ITEMS I, -- CTO ODR
7997                 MSC_RESOURCE_REQUIREMENTS REQ,
7998                 MSC_CALENDAR_DATES C
7999          WHERE  DR.PLAN_ID = p_plan_id
8000          AND    NVL(DR.OWNING_DEPARTMENT_ID, DR.DEPARTMENT_ID)=p_department_id
8001          AND    DR.RESOURCE_ID = p_resource_id
8002          AND    DR.SR_INSTANCE_ID = p_instance_id
8003          AND    DR.ORGANIZATION_ID = p_organization_id -- for performance
8004          AND    REQ.PLAN_ID = DR.PLAN_ID
8005          AND    REQ.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
8006          AND    REQ.RESOURCE_ID = DR.RESOURCE_ID
8007          AND    REQ.DEPARTMENT_ID = DR.DEPARTMENT_ID
8008          AND    NVL(REQ.PARENT_ID, l_optimized_plan) = l_optimized_plan
8009          -- CTO Option Dependent Resources ODR
8010          AND     I.SR_INSTANCE_ID = REQ.SR_INSTANCE_Id
8011          AND     I.PLAN_ID = REQ.PLAN_ID
8012          AND     I.ORGANIZATION_ID = REQ.ORGANIZATION_ID
8013          AND     I.INVENTORY_ITEM_ID = REQ.ASSEMBLY_ITEM_ID
8014          AND     ((I.bom_item_type <> 1 and I.bom_item_type <> 2)
8015                -- bom_item_type not model and option_class always committed.
8016                     AND   (I.atp_flag <> 'N')
8017                -- atp_flag is 'Y' then committed.
8018                     OR    (REQ.record_source = 2) ) -- this OR may be changed during performance analysis.
8019               -- if record created by ATP then committed.
8020          -- End CTO Option Dependent Resources
8021          AND    S.PLAN_ID = DR.PLAN_ID
8022          AND    S.TRANSACTION_ID = REQ.SUPPLY_ID
8023          AND    S.SR_INSTANCE_ID = REQ.SR_INSTANCE_ID --bug3948494
8024                 -- Exclude Cancelled Supplies 2460645
8025          AND     NVL(S.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
8026          AND    C.SR_INSTANCE_ID = DR.SR_INSTANCE_ID
8027          AND    C.CALENDAR_CODE = l_calendar_code
8028          AND    C.EXCEPTION_SET_ID = l_calendar_exception_set_id
8029                 -- Bug 3348095
8030                 -- Ensure that the ATP created resource Reqs
8031                 -- do not get double counted.
8032          AND     C.CALENDAR_DATE BETWEEN DECODE(REQ.record_source, 2,
8033                           TRUNC(NVL(REQ.END_DATE, REQ.START_DATE)), TRUNC(REQ.START_DATE))
8034                    AND TRUNC(NVL(REQ.END_DATE, REQ.START_DATE))
8035                 -- End Bug 3348095
8036          AND    C.SEQ_NUM IS NOT NULL
8037          ---bug 2341075
8038          --AND    C.CALENDAR_DATE >= trunc(sysdate)
8039          AND    C.CALENDAR_DATE >= l_plan_start_date
8040          UNION ALL
8041          SELECT p_level col1,
8042                 p_identifier col2,
8043                 p_scenario_id col3,
8044                 l_null_num col4 ,
8045                 l_null_num col5,
8046                 p_organization_id col6,
8047                 p_department_id col7,
8048                 p_resource_id col8,
8049                 l_null_num col9,
8050                 l_null_num col10,
8051                 l_null_num col11,
8052                 l_null_num col12,
8053                 l_null_num col13,
8054                 l_null_num col14,
8055                 l_null_char col15,
8056                 l_uom_code col16,
8057                 2 col17, -- supply
8058                 l_null_num col18,
8059                 l_null_char col19,
8060                 MNRA.SR_INSTANCE_ID col20,
8061                 l_null_num col21,
8062                 MNRA.TRANSACTION_ID col22,
8063                 l_null_num col23,
8064                 MNRA.CAPACITY_UNITS  * ((DECODE(LEAST(MNRA.from_time, MNRA.to_time),
8065                        MNRA.to_time,to_time + 24*3600,
8066                        MNRA.to_time) - MNRA.from_time)/3600) *
8067                 DECODE(p_scenario_id, -1, 1, NVL((MRHM.allocation_percent/100), 1)) col24,
8068                 --bug 4156016
8069 	        /*		NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
8070                                                      p_instance_id,
8071                                                      null,
8072                                                      p_organization_id,
8073                                                      p_department_id,
8074                                                      p_resource_id,
8075                                                      l_demand_class,
8076                                                      SHIFT_DATE),1)) col24, */
8077                 MNRA.SHIFT_DATE col25,
8078                 l_null_num col26,
8079                 l_null_char col27,
8080                 l_null_num col28,
8081 		l_null_num col29,
8082 		-- ship_rec_cal changes begin
8083 		l_sysdate,
8084 		G_USER_ID,
8085 		l_sysdate,
8086 		G_USER_ID,
8087 		G_USER_ID,
8088 		-- ship_rec_cal changes end
8089 		-- Unallocated_Quantity
8090                 MNRA.CAPACITY_UNITS  * ((DECODE(LEAST(MNRA.from_time, MNRA.to_time),
8091                        MNRA.to_time,MNRA.to_time + 24*3600,
8092                        MNRA.to_time) - MNRA.from_time)/3600)
8093           FROM    MSC_NET_RESOURCE_AVAIL MNRA,
8094                   msc_resource_hierarchy_mv MRHM
8095           WHERE   MNRA.PLAN_ID = p_plan_id
8096           AND     NVL(MNRA.PARENT_ID, -2) <> -1
8097           AND     MNRA.SR_INSTANCE_ID = p_instance_id
8098           AND     MNRA.RESOURCE_ID = p_resource_id
8099           AND     MNRA.DEPARTMENT_ID = p_department_id
8100           --bug 2341075
8101           --AND     SHIFT_DATE >= trunc(sysdate)
8102           --bug 4232627: select only those records which are after plan start date
8103           AND     MNRA.SHIFT_DATE >= l_plan_start_date --bug#12872862
8104           --bug 4156016
8105           AND     MNRA.organization_id = p_organization_id
8106           AND     MRHM.department_id (+) = MNRA.department_id
8107           AND     MRHM.resource_id  (+)= MNRA.resource_id
8108           AND     MRHM.organization_id (+) = MNRA.organization_id
8109           AND     MRHM.sr_instance_id  (+)= MNRA.sr_instance_id
8110           --AND     MRHM.level_id (+) = -1 --4365873
8111           AND     decode(MRHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
8112           --bug 4232627:
8113           AND     trunc(MNRA.shift_date) >=  trunc(MRHM.effective_date (+))
8114           AND     trunc(MNRA.shift_date) <=  trunc(MRHM.disable_date (+))
8115           AND     MRHM.demand_class (+) = l_demand_class
8116 	)
8117 	; -- dsting removed order by col25;
8118        END IF; -- l_optimized_plan = 1 Bug 2809639
8119 
8120      END IF; --- If l_use_batching =1 then
8121 
8122       -- for period ATP
8123       IF PG_DEBUG in ('Y', 'C') THEN
8124          msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'after insert into msc_atp_sd_details_temp');
8125       END IF;
8126       MSC_ATP_PROC.get_period_data_from_SD_temp(x_atp_period);
8127 
8128       l_current_atp.atp_period := x_atp_period.Period_Start_Date;
8129       l_current_atp.atp_qty := x_atp_period.Period_Quantity;
8130       --- bug 1657855, remove support for min alloc
8131       --l_current_atp.limit_qty := l_current_atp.atp_qty; -- 02/16
8132 
8133    END IF;
8134 
8135     IF PG_DEBUG in ('Y', 'C') THEN
8136        msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'right after the big query');
8137        Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
8138 		l_current_atp);
8139     END IF;
8140 
8141     -- do backward consumption for DCi
8142     MSC_ATP_PROC.Atp_Backward_Consume(l_current_atp.atp_qty);
8143 
8144     IF PG_DEBUG in ('Y', 'C') THEN
8145        msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'right after the backward consume');
8146        Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
8147 		l_current_atp);
8148     END IF;
8149 
8150     -- we have 3 records of tables.
8151     -- l_current_atp: stores the date and quantity for this demand class,
8152     --                and since we need to do backward consumption on this.
8153     -- l_current_steal_atp: stores the date and quantity from higher priority
8154     --                      demand class, this need to consume l_current_atp
8155     -- l_next_steal_atp : stores  the date and quantity for next priority
8156     --                    demand class to cunsume.  we need this because we may
8157     --                    have multiple demand classes at same priority .
8158     -- for example, we have DC1 in priority 1, DC21, DC22 in priority 2,
8159     -- DC3 in priority  3.
8160     -- now DC21 need to take care DC1, DC22 need to take care DC1 but not DC21,
8161     -- DC3 need to take care DC1, DC21, and DC22.  so if we are in the loop for
8162     -- DC22, than l_current_atp is the atp info for DC22,
8163     -- l_current_steal_atp is the atp info for DC1(which does not include DC21),
8164     -- and l_next_steal_atp is the stealing data that we need to take care
8165     -- for DC1, DC21 and DC22  when later on we move to the loop for DC3.
8166 
8167        -- do backward consumption if DC1 to DC(i-1) has any negative bucket,and
8168        -- the priority  is higher than DCi
8169        -- the l_current_atp is an in/out parameter
8170 
8171       -- for 1680719, since in hierarchy demand class we cannot
8172       -- judge the priority by just looking at the priority (we need
8173       -- the information from the parent, so the condition needs to be changed.
8174 
8175       IF l_level_id IN (-1, 1) THEN
8176         -- here is the old logic which should still be ok for level id 1 and -1
8177        IF (i > 1) THEN
8178 
8179         IF (l_demand_class_priority_tab(i) >
8180             l_demand_class_priority_tab (i-1)) THEN
8181         -- we don't need to change the l_current_steal_atp if we don't
8182         -- move to next priority.
8183         -- but we do need to change the l_current_steal_atp
8184         -- if we are in different priority  now.
8185 
8186           l_current_steal_atp := l_next_steal_atp;
8187 
8188           -- Added for bug 1409335. Need to initialize l_next_steal_atp
8189           -- otherwise quanities would be getting accumulated
8190           -- repeatedly.
8191           l_next_steal_atp := l_null_steal_atp;
8192         END IF;
8193        END IF;
8194       ELSE -- IF l_level_id IN (-1, 1) THEN
8195 
8196        IF (i > 1) THEN
8197 
8198         IF (l_class_tab(i) <> l_class_tab(i-1)) THEN
8199 
8200           -- class changed.  If priority of both classes are not the same,
8201           -- then we need to change the curr_steal_atp  at class level.
8202 
8203           IF PG_DEBUG in ('Y', 'C') THEN
8204              msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'class changed');
8205           END IF;
8206 
8207           IF trunc(l_demand_class_priority_tab(i), -3) >
8208              trunc(l_demand_class_priority_tab (i-1), -3) THEN
8209 
8210             IF PG_DEBUG in ('Y', 'C') THEN
8211                msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'class priority changed');
8212             END IF;
8213             l_class_curr_steal_atp := l_class_next_steal_atp;
8214             l_class_next_steal_atp := l_null_steal_atp;
8215           END IF;
8216 
8217           l_partner_next_steal_atp := l_null_steal_atp;
8218           l_partner_curr_steal_atp := l_null_steal_atp;
8219           l_partner_next_steal_atp := l_null_steal_atp;
8220           l_current_steal_atp := l_null_steal_atp;
8221           l_next_steal_atp := l_null_steal_atp;
8222 
8223         ELSE
8224           IF PG_DEBUG in ('Y', 'C') THEN
8225              msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'same class');
8226           END IF;
8227           IF (l_partner_tab(i) <> l_partner_tab(i-1)) THEN
8228             -- customer changed.  If priority of both customers are not the
8229             -- same, we need to change the curr_steal_atp  at partner level.
8230 
8231             IF PG_DEBUG in ('Y', 'C') THEN
8232                msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'customer changed');
8233             END IF;
8234 
8235             IF trunc(l_demand_class_priority_tab(i), -2) >
8236                trunc(l_demand_class_priority_tab (i-1), -2) THEN
8237               IF PG_DEBUG in ('Y', 'C') THEN
8238                  msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'customer priority changed');
8239               END IF;
8240 
8241               l_partner_curr_steal_atp := l_partner_next_steal_atp;
8242               l_partner_next_steal_atp := l_null_steal_atp;
8243             END IF;
8244 
8245             l_current_steal_atp := l_null_steal_atp;
8246             l_next_steal_atp := l_null_steal_atp;
8247 
8248           ELSE
8249             -- same customer
8250             IF PG_DEBUG in ('Y', 'C') THEN
8251                msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'same customer ');
8252             END IF;
8253 
8254             IF (l_demand_class_priority_tab(i) >
8255                 l_demand_class_priority_tab (i-1)) THEN
8256               -- site level priority changed
8257 
8258               IF PG_DEBUG in ('Y', 'C') THEN
8259                  msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'site priority changed');
8260               END IF;
8261               l_current_steal_atp := l_next_steal_atp;
8262               l_next_steal_atp := l_null_steal_atp;
8263 
8264             END IF;
8265           END IF; -- IF (l_partner_tab(i) <> l_partner_tab(i-1))
8266         END IF; -- IF (l_class_tab(i) <> l_class_tab(i-1))
8267 
8268        END IF; -- IF (i > 1)
8269 
8270       END IF; -- IF l_level_id IN (-1, 1)
8271       IF PG_DEBUG in ('Y', 'C') THEN
8272          msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'before we decide we need to do dc consumption');
8273       END IF;
8274 
8275       IF (i > 1) THEN
8276        IF (  -- this is the huge condition
8277              ((l_level_id IN (-1, 1)) AND
8278              (l_demand_class_priority_tab(i) <> l_demand_class_priority_tab(1)))
8279            OR
8280              (l_level_id in (2, 3))
8281           ) THEN
8282 
8283         -- we need to do demand class consume only if we are not in the first
8284         -- preferred priority
8285 
8286         -- bug 1413459
8287         -- we need to remember what's the atp picture before the
8288         -- demand class consumption but after it's own backward
8289         -- consumption.  so that we can figure out the stealing
8290         -- quantity correctly.
8291         IF (NVL(p_insert_flag, 0) <>0)
8292            AND (l_demand_class_tab(i) = p_demand_class) THEN
8293             l_temp_atp := l_current_atp;
8294         END IF;
8295 
8296 --------------
8297         -- 1680719
8298         -- since we have hierarchy now, before we do demand class
8299         -- consumption for site level, we need to do the class level and
8300         -- partner level first
8301 
8302         IF l_level_id IN (2,3) THEN
8303 
8304           IF l_class_tab(i) <> l_class_tab(1) THEN
8305 
8306             IF PG_DEBUG in ('Y', 'C') THEN
8307                msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'before consume l_class_curr_steal_atp');
8308                Print_Period_Qty('l_class_curr_steal_atp.atp_period:atp_qty = ',
8309 			l_class_curr_steal_atp);
8310             END IF;
8311 
8312             MSC_AATP_PVT.Atp_Demand_Class_Consume(l_current_atp, l_class_curr_steal_atp);
8313 
8314             IF PG_DEBUG in ('Y', 'C') THEN
8315                msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'After consume l_class_curr_steal_atp');
8316                Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
8317 			l_current_atp);
8318                Print_Period_Qty('l_class_curr_steal_atp.atp_period:atp_qty = ',
8319 			l_class_curr_steal_atp);
8320             END IF;
8321           END IF; --IF l_class_tab(i) <> l_class_tab(1) THEN
8322 
8323           -- bug 1922942: although partner_id should be unique, we introduced
8324           -- -1 for 'Other' which make the partner_id not unique.
8325           -- for example, Class1/Other and Class2/Other will have same
8326           -- partner_id -1. so the if condition needs to be modified.
8327 
8328           -- IF l_partner_tab(i) <> l_partner_tab(1) THEN
8329 
8330           IF (l_class_tab(i) <> l_class_tab(1)) OR
8331               (l_partner_tab(i) <> l_partner_tab(1)) THEN
8332 
8333             IF PG_DEBUG in ('Y', 'C') THEN
8334                msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'before consume l_partner_curr_steal_atp');
8335             END IF;
8336             MSC_AATP_PVT.Atp_Demand_Class_Consume(l_current_atp, l_partner_curr_steal_atp);
8337 
8338             IF PG_DEBUG in ('Y', 'C') THEN
8339                msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'After consume l_partner_curr_steal_atp');
8340                Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
8341 			l_current_atp);
8342                Print_Period_Qty('l_partner_curr_steal_atp.atp_period:atp_qty = ',
8343 			l_partner_curr_steal_atp);
8344 
8345             END IF;
8346           END IF; -- IF l_partner_tab(i) <> l_partner_tab(1) THEN
8347 
8348         END IF; -- IF l_level_id IN (2,3)
8349 
8350         IF PG_DEBUG in ('Y', 'C') THEN
8351            msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'Before consume current_steal_atp');
8352 
8353            Print_Period_Qty('l_current_steal_atp.atp_period:atp_qty = ',
8354 		l_current_steal_atp);
8355 
8356         END IF;
8357 
8358         MSC_AATP_PVT.Atp_Demand_Class_Consume(l_current_atp, l_current_steal_atp);
8359 
8360         IF PG_DEBUG in ('Y', 'C') THEN
8361            msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'After consume l_current_steal_atp');
8362 
8363            Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
8364 		l_current_atp);
8365            Print_Period_Qty('l_current_steal_atp.atp_period:atp_qty = ',
8366 		l_current_steal_atp);
8367         END IF;
8368 
8369         -- this part is not in the original design.
8370         -- original design is that we will ignore the inconsistancy
8371         -- in the s/d and period atp for display when stealing happens, as long
8372         -- as we take care the stealing in the logic.
8373         -- but i think it is still better to put it in.
8374         -- and actually if we change Atp_Demand_Class_Consume we can
8375         -- deal with this together.  but for now...
8376 
8377         -- we need to know if we need to store the stealing
8378         -- results in to x_atp_supply_demand and x_atp_period or not.
8379         -- we only do it if this is the demand class we request and
8380         -- insert_flag is on
8381 
8382         IF (NVL(p_insert_flag, 0) <>0) AND
8383            (l_demand_class_tab(i) = p_demand_class) THEN
8384 
8385           FOR j in 1..l_current_atp.atp_qty.COUNT LOOP
8386 
8387             IF l_current_atp.atp_qty(j) < l_temp_atp.atp_qty(j) THEN
8388               -- this is the stealing quantity in that period
8389               -- bug 1413459: the stealing quantity should be the current
8390               -- period quantity (after backward consumption, after stealing)
8391               -- minus the period quantity after backward consumption but
8392               -- before the stealing
8393               l_steal_period_quantity := l_current_atp.atp_qty(j) -
8394                                          l_temp_atp.atp_qty(j);
8395 
8396               MSC_SATP_FUNC.Extend_Atp_Supply_Demand(l_temp_atp_supply_demand, l_return_status);
8397 
8398               k := l_temp_atp_supply_demand.Level.Count;
8399               l_temp_atp_supply_demand.level(k) := p_level;
8400               l_temp_atp_supply_demand.identifier(k) := p_identifier;
8401               l_temp_atp_supply_demand.scenario_id(k) := p_scenario_id;
8402               l_temp_atp_supply_demand.department_id(k) := p_department_id;
8403               l_temp_atp_supply_demand.resource_id(k) := p_resource_id;
8404               l_temp_atp_supply_demand.uom(k):= l_uom_code;
8405               l_temp_atp_supply_demand.supply_demand_type(k) := 1;
8406 
8407               -- Bug 1408132 and 1416290, Need to insert type as
8408               -- Demand Class Consumption (45).
8409               l_temp_atp_supply_demand.supply_demand_source_type(k) := 45;
8410 
8411               l_temp_atp_supply_demand.identifier1(k) := p_instance_id;
8412               l_temp_atp_supply_demand.supply_demand_date (k) := l_current_atp.atp_period(j);
8413               l_temp_atp_supply_demand.supply_demand_quantity(k) := l_steal_period_quantity;
8414 
8415               x_atp_period.Total_Demand_Quantity(j):=
8416                      x_atp_period.Total_Demand_Quantity(j) +
8417                      l_steal_period_quantity;
8418 
8419               x_atp_period.period_quantity(j):= x_atp_period.period_quantity(j)
8420                      + l_steal_period_quantity;
8421 
8422             END IF;
8423           END LOOP;
8424 
8425 	  -- dsting
8426 	  move_SD_plsql_into_SD_temp(l_temp_atp_supply_demand);
8427 
8428         END IF;  -- IF (NVL(p_insert_flag, 0) <>0) .....
8429        END IF; -- the huge condition
8430       END IF; -- IF (i > 1)
8431 
8432       --IF l_demand_class_priority_tab(i) < l_priority THEN
8433       ---bug 1655110
8434       IF (l_demand_class <> p_demand_class) THEN
8435         -- we need to prepare the l_next_steal_atp for next priorit
8436 
8437         MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_current_atp, l_next_steal_atp);
8438 
8439         -- 1680719
8440         IF l_level_id IN (-1, 1) THEN
8441           IF l_demand_class_priority_tab(i)<
8442              l_demand_class_priority_tab(i+1) THEN
8443           -- this is the last element of current priority, so we also need
8444           -- to add l_steal_atp into l_next_steal_atp if we can not finish
8445           -- the stealing at this priority
8446 
8447             IF PG_DEBUG in ('Y', 'C') THEN
8448                msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'level = '||l_level_id);
8449                msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'before Adding Add_to_Next_Steal_Atp');
8450                Print_Period_Qty('l_next_steal_atp.atp_period:atp_qty = ',
8451 			l_next_steal_atp);
8452 
8453             END IF;
8454 
8455             MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_current_steal_atp, l_next_steal_atp);
8456 
8457             IF PG_DEBUG in ('Y', 'C') THEN
8458                msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'after Add_to_Next_Steal_Atp');
8459                Print_Period_Qty('l_next_steal_atp.atp_period:atp_qty = ',
8460 			l_next_steal_atp);
8461             END IF;
8462 
8463           END IF;
8464 
8465         ELSE -- IF l_level_id IN (-1, 1)
8466           -- this is for hierarchy customer level and site level
8467           IF PG_DEBUG in ('Y', 'C') THEN
8468              msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'level = '||l_level_id);
8469              msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'i = '||i);
8470              msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'l_class_tab(i) = '||l_class_tab(i));
8471              msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'l_class_tab(i+1) = '||l_class_tab(i+1));
8472           END IF;
8473           IF (l_class_tab(i) <> l_class_tab(i+1)) THEN
8474 
8475             -- class changed.  If priority of both classes are not the same,
8476             -- then we need to change the curr_steal_atp  at class level.
8477             IF PG_DEBUG in ('Y', 'C') THEN
8478                msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'class changed');
8479             END IF;
8480 
8481             MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_current_steal_atp, l_next_steal_atp);
8482             MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_next_steal_atp, l_partner_next_steal_atp);
8483             MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_partner_curr_steal_atp,
8484                                   l_partner_next_steal_atp);
8485             MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_partner_next_steal_atp,
8486                                   l_class_next_steal_atp);
8487 
8488             IF trunc(l_demand_class_priority_tab(i), -3)<
8489                trunc(l_demand_class_priority_tab (i+1), -3) THEN
8490 
8491               MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_class_curr_steal_atp,
8492                                     l_class_next_steal_atp);
8493 
8494             END IF;
8495 
8496           ELSE
8497 
8498             IF PG_DEBUG in ('Y', 'C') THEN
8499                msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'class the same');
8500             END IF;
8501             IF (l_partner_tab(i) <> l_partner_tab(i+1)) THEN
8502               -- customer changed
8503               IF PG_DEBUG in ('Y', 'C') THEN
8504                  msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'customer not the same');
8505               END IF;
8506               MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_current_steal_atp, l_next_steal_atp);
8507 
8508               MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_next_steal_atp, l_partner_next_steal_atp);
8509 
8510               IF trunc(l_demand_class_priority_tab(i), -2)<
8511                  trunc(l_demand_class_priority_tab (i+1), -2) THEN
8512                 -- customer priority changed
8513 
8514                 IF PG_DEBUG in ('Y', 'C') THEN
8515                    msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'customer priority changed');
8516                 END IF;
8517                 MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_partner_curr_steal_atp,
8518                                       l_partner_next_steal_atp);
8519 
8520               END IF;
8521 
8522 
8523             ELSE
8524               -- same customer
8525               IF PG_DEBUG in ('Y', 'C') THEN
8526                  msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'customer the same');
8527               END IF;
8528               IF (l_demand_class_priority_tab(i)<>
8529                   l_demand_class_priority_tab (i+1)) THEN
8530                 -- site level priority changed
8531                 MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_current_steal_atp, l_next_steal_atp);
8532 
8533               END IF;
8534             END IF;
8535           END IF;
8536         END IF; -- IF l_level_id IN (-1, 1)
8537       END IF;
8538 
8539       -- 1665110
8540       IF PG_DEBUG in ('Y', 'C') THEN
8541          msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'p_demand_class = '||p_demand_class);
8542       END IF;
8543       EXIT WHEN (l_demand_class = p_demand_class);
8544       IF PG_DEBUG in ('Y', 'C') THEN
8545          msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'after the exit statement, so we did not exit');
8546       END IF;
8547 
8548   END LOOP;
8549 
8550   MSC_ATP_PROC.Atp_Accumulate(l_current_atp.atp_qty);
8551 
8552   IF PG_DEBUG in ('Y', 'C') THEN
8553      msc_sch_wb.atp_debug('Res_Alloc_Cum_Atp: ' || 'right after the Atp_Accumulate');
8554      Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
8555 	l_current_atp);
8556   END IF;
8557 
8558   x_atp_info := l_current_atp;
8559 
8560   -- get the infinite time fence date if it exists
8561   l_infinite_time_fence_date := MSC_ATP_FUNC.get_infinite_time_fence_date(null,
8562              null,null, p_plan_id);
8563 
8564 
8565   IF l_infinite_time_fence_date IS NOT NULL THEN
8566       -- add one more entry to indicate infinite time fence date
8567       -- and quantity.
8568       x_atp_info.atp_qty.EXTEND;
8569       x_atp_info.atp_period.EXTEND;
8570       --- bug 1657855, remove support for alloc
8571       --x_atp_info.limit_qty.EXTEND;
8572 
8573       i := x_atp_info.atp_qty.COUNT;
8574       x_atp_info.atp_period(i) := l_infinite_time_fence_date;
8575       x_atp_info.atp_qty(i) := MSC_ATP_PVT.INFINITE_NUMBER;
8576       --x_atp_info.limit_qty(i) := MSC_ATP_PVT.INFINITE_NUMBER;
8577 
8578       IF NVL(p_insert_flag, 0) <> 0 THEN
8579         -- add one more entry to indicate infinite time fence date
8580         -- and quantity.
8581 
8582         x_atp_period.Cumulative_Quantity := x_atp_info.atp_qty;
8583 
8584         j := x_atp_period.Level.COUNT;
8585         MSC_SATP_FUNC.Extend_Atp_Period(x_atp_period, l_return_status);
8586         j := j + 1;
8587         IF j > 1 THEN
8588           x_atp_period.Period_End_Date(j-1) := l_infinite_time_fence_date -1;
8589 	  -- dsting
8590           --x_atp_period.Identifier1(j) := x_atp_supply_demand.Identifier1(j-1);
8591           --x_atp_period.Identifier2(j) := x_atp_supply_demand.Identifier2(j-1);
8592           x_atp_period.Identifier1(j) := x_atp_period.Identifier1(j-1);
8593           x_atp_period.Identifier2(j) := x_atp_period.Identifier2(j-1);
8594         END IF;
8595 
8596         x_atp_period.Level(j) := p_level;
8597         x_atp_period.Identifier(j) := p_identifier;
8598         x_atp_period.Scenario_Id(j) := p_scenario_id;
8599         x_atp_period.Pegging_Id(j) := NULL;
8600         x_atp_period.End_Pegging_Id(j) := NULL;
8601         x_atp_period.Department_Id(j) := p_department_id;
8602         x_atp_period.Resource_Id(j) := p_resource_id;
8603         x_atp_period.Organization_id(j) := p_organization_id;
8604         x_atp_period.Period_Start_Date(j) := l_infinite_time_fence_date;
8605         x_atp_period.Total_Supply_Quantity(j) := MSC_ATP_PVT.INFINITE_NUMBER;
8606         x_atp_period.Total_Demand_Quantity(j) := 0;
8607         x_atp_period.Period_Quantity(j) := MSC_ATP_PVT.INFINITE_NUMBER;
8608         x_atp_period.Cumulative_Quantity(j) := MSC_ATP_PVT.INFINITE_NUMBER;
8609 
8610     END IF;
8611   END IF;
8612 -- END IF;
8613 
8614 END Res_Alloc_Cum_Atp;
8615 
8616 /* spec changed as part of ship_rec_cal changes
8617    various input parameters passed in a record atp_info_rec
8618 */
8619 PROCEDURE Supplier_Alloc_Cum_Atp(
8620         p_sup_atp_info_rec      IN      MSC_ATP_REQ.ATP_Info_Rec,
8621 	p_identifier          	IN 	NUMBER,
8622 	p_request_date        	IN 	DATE,
8623 	x_atp_info            	OUT 	NoCopy MRP_ATP_PVT.ATP_Info,
8624 	x_atp_period          	OUT 	NoCopy MRP_ATP_PUB.ATP_Period_Typ,
8625 	x_atp_supply_demand   	OUT 	NoCopy MRP_ATP_PUB.ATP_Supply_Demand_Typ
8626 )
8627 IS
8628 l_calendar_code                 VARCHAR2(14);
8629 l_calendar_exception_set_id     NUMBER;
8630 l_level_id                      NUMBER;
8631 l_priority			NUMBER := 1;
8632 l_allocation_percent		NUMBER := 100;
8633 l_demand_class_tab		MRP_ATP_PUB.char80_arr
8634                                    := MRP_ATP_PUB.char80_arr();
8635 l_demand_class_priority_tab	MRP_ATP_PUB.number_arr
8636                                    := MRP_ATP_PUB.number_arr();
8637 l_current_atp			MRP_ATP_PVT.ATP_Info;
8638 l_next_steal_atp		MRP_ATP_PVT.ATP_Info;
8639 l_null_steal_atp		MRP_ATP_PVT.ATP_Info;
8640 l_current_steal_atp             MRP_ATP_PVT.ATP_Info;
8641 l_temp_atp                      MRP_ATP_PVT.ATP_Info;
8642 i				PLS_INTEGER;
8643 l_infinite_time_fence_date	DATE;
8644 mm				PLS_INTEGER;
8645 ii                              PLS_INTEGER;
8646 jj                              PLS_INTEGER;
8647 j				PLS_INTEGER;
8648 k				PLS_INTEGER;
8649 l_demand_class			VARCHAR2(80);
8650 l_inv_item_id			NUMBER;
8651 l_uom_code			VARCHAR2(3);
8652 l_null_num  			number := null;
8653 l_null_char    			varchar2(3) := null;
8654 l_return_status			VARCHAR(1);
8655 l_steal_period_quantity		number;
8656 l_instance_id                   number;
8657 l_org_id                        number;
8658 l_plan_start_date               DATE;
8659 l_postprocessing_lead_time      NUMBER;
8660 l_cutoff_date                   DATE;
8661 
8662 -- 1680719
8663 l_class_tab                     MRP_ATP_PUB.char30_arr
8664                                     := MRP_ATP_PUB.char30_arr();
8665 l_partner_tab                   MRP_ATP_PUB.number_arr
8666                                     := MRP_ATP_PUB.number_arr();
8667 l_class_next_steal_atp          MRP_ATP_PVT.ATP_Info;
8668 l_partner_next_steal_atp        MRP_ATP_PVT.ATP_Info;
8669 l_class_curr_steal_atp          MRP_ATP_PVT.ATP_Info;
8670 l_partner_curr_steal_atp        MRP_ATP_PVT.ATP_Info;
8671 l_pos1                          NUMBER;
8672 l_pos2                          NUMBER;
8673 delim     constant varchar2(1) := fnd_global.local_chr(13);
8674 
8675 -- dsting for s/d performance enh
8676 l_temp_atp_supply_demand   		MRP_ATP_PUB.ATP_Supply_Demand_Typ;
8677 
8678 --s_cto_rearch
8679 l_check_cap_model_flag          number;
8680 --e_cto_arch
8681 
8682 -- ship_rec_cal
8683 l_sysdate               DATE := trunc(sysdate); --4135752
8684 l_sys_next_date                 DATE; --bug3333114
8685 
8686 BEGIN
8687 
8688   IF PG_DEBUG in ('Y', 'C') THEN
8689      msc_sch_wb.atp_debug('******* Supplier_Alloc_Cum_Atp *******');
8690      msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'p_sup_atp_info_rec.instance_id =' || p_sup_atp_info_rec.instance_id);
8691      msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'p_sup_atp_info_rec.supplier_id =' || p_sup_atp_info_rec.supplier_id);
8692      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);
8693      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);
8694      msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'p_sup_atp_info_rec.organization_id =' || p_sup_atp_info_rec.organization_id);
8695      msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'p_sup_atp_info_rec.demand_class =' || p_sup_atp_info_rec.demand_class);
8696      msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'p_request_date =' || p_request_date );
8697      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 );
8698   END IF;
8699 
8700 
8701   -- find all the demand classes that we need to take care: all the demand
8702   -- classes that have higher priority  + this requested demand class.
8703 
8704   -- Logic
8705   -- Step 1:
8706   -- 	FOR each demand class DCi, we need to
8707   --  	1. get the net daily availability
8708   --  	2. do backward consumption
8709   --  	3. do backward consumption if DC1 to DC(i-1) has any negative bucket
8710   -- 	END LOOP
8711   -- Step 2:
8712   --    do accumulation for the requested demand class
8713 
8714   -- select the priority  and allocation_percent for that item/demand class.
8715   -- if no data found, check if this item has a valid allocation rule.
8716   -- otherwise return error.
8717 
8718  -- bug 1169467
8719  -- get the plan start date. later on we will use this restrict the
8720  -- availability
8721 
8722  -- Supplier Capacity and Lead Time (SCLT) Proj
8723  -- Commented out
8724  -- SELECT trunc(plan_start_date), sr_instance_id, organization_id,
8725  --        trunc(cutoff_date)
8726  -- INTO   l_plan_start_date, l_instance_id, l_org_id, l_cutoff_date
8727  -- FROM   msc_plans
8728  -- WHERE  plan_id = p_sup_atp_info_rec.plan_id;
8729 
8730  -- Instead re-assigned local values using global variable
8731     l_plan_start_date := MSC_ATP_PVT.G_PLAN_INFO_REC.plan_start_date;
8732     l_instance_id     := MSC_ATP_PVT.G_PLAN_INFO_REC.sr_instance_id;
8733     l_org_id          := MSC_ATP_PVT.G_PLAN_INFO_REC.organization_id;
8734     l_cutoff_date     := MSC_ATP_PVT.G_PLAN_INFO_REC.plan_cutoff_date;
8735 
8736  IF PG_DEBUG in ('Y', 'C') THEN
8737     msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'l_plan_start_date = '||l_plan_start_date);
8738     msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'l_instance_id = '||l_instance_id);
8739     msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'l_org_id = '||l_org_id);
8740     msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'l_cutoff_date = '||l_cutoff_date);
8741  END IF;
8742 
8743  --s_cto_rearch
8744  IF (p_sup_atp_info_rec.bom_item_type = 4 AND (p_sup_atp_info_rec.rep_ord_flag = 'Y'
8745                                           OR   p_sup_atp_info_rec.base_item_id is not null)) THEN --bug 8631827,7592457
8746         l_inv_item_id := p_sup_atp_info_rec.base_item_id;
8747         l_check_cap_model_flag := 1;
8748  ELSIF  p_sup_atp_info_rec.bom_item_type = 1 THEN
8749         l_inv_item_id := p_sup_atp_info_rec.inventory_item_id;
8750         l_check_cap_model_flag := 1;
8751  ELSE
8752         l_inv_item_id := p_sup_atp_info_rec.inventory_item_id;
8753  END IF;
8754  --e_cto_rearch
8755 
8756 
8757  --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,
8758  MSC_AATP_PVT.Get_DC_Info(l_instance_id, l_inv_item_id, p_sup_atp_info_rec.organization_id, null, null,
8759 	 p_sup_atp_info_rec.demand_class, p_request_date, l_level_id, l_priority, l_allocation_percent, l_return_status);
8760   -- find the demand classes that have priority  higher (small number) than
8761   -- the requested demand class
8762 
8763 -- IF l_allocation_percent <> 0.0 THEN
8764  IF PG_DEBUG in ('Y', 'C') THEN
8765     msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'before select the high priority demand class');
8766  END IF;
8767 
8768   -- bug 1680719
8769   --bug3948494 Do not select Higher priority DC if the requested DC
8770   --is at highest priority , we donot honor for forward consumption method here.
8771   IF l_level_id = -1 AND l_priority <> 1 THEN
8772 
8773     IF PG_DEBUG in ('Y', 'C') THEN
8774        msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'l_allocation_percent = '||l_allocation_percent);
8775     END IF;
8776     SELECT demand_class, priority
8777     BULK COLLECT INTO l_demand_class_tab, l_demand_class_priority_tab
8778     FROM   msc_item_hierarchy_mv
8779     --WHERE  inventory_item_id = p_sup_atp_info_rec.inventory_item_id
8780     WHERE  inventory_item_id = l_inv_item_id
8781     AND    organization_id = p_sup_atp_info_rec.organization_id -- Ship To org
8782     AND    sr_instance_id = l_instance_id
8783     AND    p_request_date BETWEEN effective_date AND disable_date
8784     AND    priority  <= l_priority   -- 1665110, add '='
8785     AND    level_id = l_level_id
8786     ORDER BY priority asc, allocation_percent desc ;
8787 
8788   ELSIF l_level_id = 1 THEN
8789 
8790     IF PG_DEBUG in ('Y', 'C') THEN
8791        msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'l_level_id = '||l_level_id);
8792     END IF;
8793     SELECT demand_class, priority
8794     BULK COLLECT INTO l_demand_class_tab, l_demand_class_priority_tab
8795     FROM   msc_item_hierarchy_mv
8796     --WHERE  inventory_item_id = p_sup_atp_info_rec.inventory_item_id
8797     WHERE  inventory_item_id = l_inv_item_id
8798     AND    organization_id = p_sup_atp_info_rec.organization_id
8799     AND    sr_instance_id = l_instance_id
8800     AND    p_request_date BETWEEN effective_date AND disable_date
8801     AND    priority  <= l_priority   -- 1665110, add '='
8802     AND    level_id = l_level_id
8803     ORDER BY priority asc, class;
8804 
8805   ELSIF l_level_id = 2 THEN
8806 
8807 
8808     IF PG_DEBUG in ('Y', 'C') THEN
8809        msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'l_level_id = '||l_level_id);
8810     END IF;
8811     SELECT mv1.demand_class, mv1.priority, mv1.class, mv1.partner_id
8812     BULK COLLECT INTO l_demand_class_tab, l_demand_class_priority_tab,
8813                       l_class_tab, l_partner_tab
8814     FROM   msc_item_hierarchy_mv mv1
8815     --WHERE  mv1.inventory_item_id = p_sup_atp_info_rec.inventory_item_id
8816     WHERE  inventory_item_id = l_inv_item_id
8817     AND    mv1.organization_id = p_sup_atp_info_rec.organization_id
8818     AND    mv1.sr_instance_id = l_instance_id
8819     AND    p_request_date BETWEEN mv1.effective_date AND mv1.disable_date
8820     --AND    mv1.priority  <= l_priority   -- 1665110, add '='
8821     AND    mv1.level_id = l_level_id
8822     AND trunc(mv1.priority, -3) <= trunc(l_priority, -3)
8823       ORDER BY trunc(mv1.priority, -3), mv1.class ,
8824                trunc(mv1.priority, -2), mv1.partner_id;
8825 
8826 
8827   ELSIF l_level_id = 3 THEN
8828 
8829     -- bug 1680719
8830     -- we need to select the class, partner_id, partner_site_id
8831 
8832     IF PG_DEBUG in ('Y', 'C') THEN
8833        msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'l_level_id = '||l_level_id);
8834     END IF;
8835     SELECT mv1.demand_class, mv1.priority, mv1.class, mv1.partner_id
8836     BULK COLLECT INTO l_demand_class_tab, l_demand_class_priority_tab,
8837                       l_class_tab, l_partner_tab
8838     FROM   msc_item_hierarchy_mv mv1
8839     --WHERE  mv1.inventory_item_id = p_sup_atp_info_rec.inventory_item_id
8840     WHERE  mv1.inventory_item_id = l_inv_item_id
8841     AND    mv1.organization_id = p_sup_atp_info_rec.organization_id
8842     AND    mv1.sr_instance_id = l_instance_id
8843     AND    p_request_date BETWEEN mv1.effective_date AND mv1.disable_date
8844     --AND    mv1.priority  <= l_priority   -- 1665110, add '='
8845     AND    mv1.level_id = l_level_id
8846     AND trunc(mv1.priority, -3) <= trunc(l_priority, -3)
8847       ORDER BY trunc(mv1.priority, -3), mv1.class ,
8848                trunc(mv1.priority, -2), mv1.partner_id,
8849                mv1.priority, mv1.partner_site_id;
8850   END IF;
8851 
8852 --/* 1665110
8853   IF l_demand_class_tab.count = 0 THEN
8854      -- add the request demand class into the list
8855      l_demand_class_tab.Extend;
8856      l_demand_class_priority_tab.Extend;
8857      i := l_demand_class_tab.COUNT;
8858      l_demand_class_priority_tab(i) := l_priority;
8859      l_demand_class_tab(i) := p_sup_atp_info_rec.demand_class;
8860 
8861      -- 1680719
8862      IF l_level_id in (2, 3) THEN
8863          l_class_tab.Extend;
8864          l_partner_tab.Extend;
8865          l_pos1 := instr(p_sup_atp_info_rec.demand_class,delim,1,1);
8866          l_pos2 := instr(p_sup_atp_info_rec.demand_class,delim,1,2);
8867          l_class_tab(i) := substr(p_sup_atp_info_rec.demand_class,1,l_pos1-1);
8868          IF l_pos2 = 0 THEN
8869            l_partner_tab(i) := substr(p_sup_atp_info_rec.demand_class,l_pos1+1);
8870          ELSE
8871            l_partner_tab(i) := substr(p_sup_atp_info_rec.demand_class,l_pos1+1,l_pos2-l_pos1-1) ;
8872          END IF;
8873      END IF;
8874   END IF;
8875 --1665110 */
8876 
8877   mm := l_demand_class_tab.FIRST;
8878 
8879   WHILE mm is not null LOOP
8880      IF PG_DEBUG in ('Y', 'C') THEN
8881         msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'l_demand_class_tab and priority = '||
8882         l_demand_class_tab(mm) ||' : '|| l_demand_class_priority_tab(mm));
8883      END IF;
8884 
8885      IF l_level_id in (2,3) THEN
8886        IF PG_DEBUG in ('Y', 'C') THEN
8887           msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'l_class_tab and partner = '||
8888         l_class_tab(mm) ||' : '||l_partner_tab(mm));
8889        END IF;
8890      END IF;
8891 
8892      mm := l_demand_class_tab.Next(mm);
8893 
8894   END LOOP;
8895 
8896   -- get the uom code :bug 1187141
8897   SELECT uom_code, postprocessing_lead_time
8898   INTO   l_uom_code, l_postprocessing_lead_time
8899   FROM   msc_system_items
8900   WHERE  plan_id = p_sup_atp_info_rec.plan_id
8901   AND    sr_instance_id = p_sup_atp_info_rec.instance_id
8902   AND    organization_id = p_sup_atp_info_rec.organization_id
8903   --AND    inventory_item_id = p_sup_atp_info_rec.inventory_item_id;
8904   AND    inventory_item_id = l_inv_item_id;
8905 
8906   IF PG_DEBUG in ('Y', 'C') THEN
8907      msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'l_uom_code and l_postprocessing_lead_time = '||
8908         l_uom_code ||' : '||l_postprocessing_lead_time);
8909   END IF;
8910 
8911   -- for performance reason, we need to get the following info and
8912   -- store in variables instead of joining it
8913 
8914   --=======================================================================================================
8915   --  ship_rec_cal changes begin
8916   --=======================================================================================================
8917   --  use SMC instead of OMC for netting
8918   --  IF SMC is FOC get plan owning org's calendar. Since we assume that every org must have atleast a
8919   --  manufacturing calendar defined, we use plan owning org's calendar as it will be spanning atleast
8920   --  upto plan end date
8921   --=======================================================================================================
8922   IF p_sup_atp_info_rec.manufacturing_cal_code <> '@@@' THEN
8923      l_calendar_code := p_sup_atp_info_rec.manufacturing_cal_code;
8924   ELSE
8925         SELECT  tp.calendar_code
8926         INTO    l_calendar_code
8927         FROM    msc_trading_partners tp,
8928                 msc_plans mp
8929         WHERE   mp.plan_id = p_sup_atp_info_rec.plan_id
8930         AND     tp.sr_instance_id  = mp.sr_instance_id
8931         AND     tp.partner_type    = 3
8932         AND     tp.sr_tp_id        = mp.organization_id;
8933   END IF;
8934   l_calendar_exception_set_id := -1;
8935 
8936   --bug3333114 start
8937   l_sys_next_date := MSC_CALENDAR.NEXT_WORK_DAY (
8938                                         p_sup_atp_info_rec.shipping_cal_code,
8939                                         l_instance_id,
8940                                         TRUNC(sysdate));
8941 
8942   IF PG_DEBUG in ('Y', 'C') THEN
8943   msc_sch_wb.atp_debug('Sys next Date : '||to_char(l_sys_next_date, 'DD-MON-YYYY'));
8944   END IF;
8945 
8946   IF (l_sys_next_date is NULL) THEN
8947       msc_sch_wb.atp_debug('Sys Next Date is null');
8948       MSC_SCH_WB.G_ATP_ERROR_CODE := MSC_ATP_PVT.NO_MATCHING_CAL_DATE;
8949       RAISE FND_API.G_EXC_ERROR;
8950   END IF;
8951   --bug3333114 end
8952 
8953   IF PG_DEBUG in ('Y', 'C') THEN
8954      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);
8955      msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'l_calendar_code='||l_calendar_code);
8956      msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'l_calendar_exception_set_id'|| l_calendar_exception_set_id);
8957   END IF;
8958   --  ship_rec_cal changes end
8959 
8960 
8961   FOR i in 1..l_demand_class_tab.COUNT LOOP
8962           l_demand_class := l_demand_class_tab(i);
8963           IF PG_DEBUG in ('Y', 'C') THEN
8964              msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'in i loop, i = '||i);
8965              msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'l_demand_class = '||l_demand_class);
8966           END IF;
8967     --=======================================================================================================
8968     -- ship_rec_cal changes begin
8969     --=======================================================================================================
8970     --  In all the SQLs that get supplier capacities following are the changes:
8971     --  1. Pass (c.seq_num - p_sup_atp_info_rec.sysdate_seq_num) to get_tolerance_percentage fn instead of
8972     --     passing c.calendar_date.
8973     --  2. If calendar code passed in FOC, we use plan owning org's calendar and remove p_seq_num is not
8974     --     null filter condition.
8975     --
8976     --  In all the SQLs that get planned orders, purchase orders and purchase requisitions following
8977     --  are the changes:
8978     --  1. We use new_dock_date or new_ship_date depending on whether supplier capacity is dock capacity or
8979     --     ship capacity.
8980     --     Earlier we used to look at new_schedule_date and offset post_processing_lead_time.
8981     --  2. Removed join with msc_calendar_dates
8982     --=======================================================================================================
8983     -- get the daily net availability for DCi
8984     IF (NVL(p_sup_atp_info_rec.insert_flag, 0) = 0  OR l_demand_class <> p_sup_atp_info_rec.demand_class) THEN
8985       IF l_check_cap_model_flag = 1 THEN
8986 
8987           -- we don't want details
8988          SELECT 	trunc(l_date), --4135752
8989       		   SUM(quantity)
8990          BULK COLLECT INTO
8991                	   l_current_atp.atp_period,
8992                	   l_current_atp.atp_qty
8993          FROM (
8994          SELECT GREATEST(cs.calendar_date,l_sys_next_date) l_date, --bug3333114
8995 	        cs.capacity*(1+NVL(MSC_ATP_FUNC.get_tolerance_percentage(
8996                                       p_sup_atp_info_rec.instance_id, p_sup_atp_info_rec.plan_id,
8997                                       l_inv_item_id, p_sup_atp_info_rec.organization_id,
8998                                       p_sup_atp_info_rec.supplier_id, p_sup_atp_info_rec.supplier_site_id,
8999                                       cs.seq_num - p_sup_atp_info_rec.sysdate_seq_num),0))*
9000 		   /*NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(l_instance_id,
9001                                             s.inventory_item_id,
9002                                             p_sup_atp_info_rec.organization_id,
9003                                             null,
9004                                             null,
9005                                             l_demand_class,
9006                                             c.calendar_date), 1) quantity*/
9007                    NVL(MIHM.allocation_percent/100, 1) quantity --4365873
9008 	FROM
9009       (
9010       SELECT
9011       	    c.calendar_date,
9012       	    c.seq_num,
9013 	    s.inventory_item_id,
9014 	    s.sr_instance_id,
9015 	    S.ORGANIZATION_ID,
9016 	    S.capacity
9017          FROM   msc_calendar_dates c,
9018                 msc_supplier_capacities s
9019          --WHERE  s.inventory_item_id = p_sup_atp_info_rec.inventory_item_id
9020          WHERE  s.inventory_item_id = l_inv_item_id
9021          AND    s.sr_instance_id = p_sup_atp_info_rec.instance_id
9022          AND    s.plan_id = p_sup_atp_info_rec.plan_id
9023          AND    s.organization_id = p_sup_atp_info_rec.organization_id
9024          AND    s.supplier_id = p_sup_atp_info_rec.supplier_id
9025          AND    NVL(s.supplier_site_id, -1) = NVL(p_sup_atp_info_rec.supplier_site_id, -1)
9026          AND    c.calendar_date BETWEEN trunc(s.from_date)
9027                                 --AND NVL(s.to_date,l_cutoff_date)
9028                                 AND trunc(NVL(s.to_date,least(p_sup_atp_info_rec.last_cap_date,l_cutoff_date))) --4055719
9029          AND    (c.seq_num IS NOT NULL or p_sup_atp_info_rec.manufacturing_cal_code = '@@@')
9030          AND    c.calendar_code = l_calendar_code
9031          AND    c.exception_set_id = l_calendar_exception_set_id
9032          AND    c.sr_instance_id = l_instance_id
9033          AND    c.calendar_date >= NVL(p_sup_atp_info_rec.sup_cap_cum_date, l_plan_start_date))CS,
9034       msc_item_hierarchy_mv mihm
9035       WHERE
9036       --4365873
9037       	     CS.INVENTORY_ITEM_ID = MIHM.INVENTORY_ITEM_ID(+)
9038       AND    CS.SR_INSTANCE_ID = MIHM.SR_INSTANCE_ID (+)
9039       AND    CS.ORGANIZATION_ID = MIHM.ORGANIZATION_ID (+)
9040       AND    decode(MIHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
9041       AND    CS.calendar_date >= MIHM.effective_date (+)
9042       AND    CS.calendar_date <= MIHM.disable_date (+)
9043       AND    MIHM.demand_class (+) = l_demand_class
9044          -- Supplier Capacity (SCLT) Accumulation starts from this date.
9045          -- AND    c.calendar_date >= l_plan_start_date -- bug 1169467
9046          UNION ALL
9047          SELECT GREATEST(trunc(Decode(p_sup_atp_info_rec.sup_cap_type,  --4135752
9048                                 1, p.new_ship_date,
9049                                 p.new_dock_date)),l_sys_next_date) l_date, -- For ship_rec_cal --bug3333114
9050                 (-1)*(p.new_order_quantity - NVL(p.implement_quantity,0))*
9051 		   DECODE(DECODE(G_HIERARCHY_PROFILE,
9052                                  --2424357
9053                                  1, DECODE(p.DEMAND_CLASS, null, null,
9054                                        DECODE(l_demand_class, '-1',
9055                                            MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
9056                                                      null,
9057                                                      null,
9058                                                      p.inventory_item_id,
9059                                                      p.organization_id,
9060                                                      p.sr_instance_id,
9061                                                      trunc(Decode(p_sup_atp_info_rec.sup_cap_type,
9062                                                           1, p.new_ship_date,
9063                                                           p.new_dock_date
9064                                                      )), --4135752
9065                                                      l_level_id,
9066                                                      p.DEMAND_CLASS), p.DEMAND_CLASS)),
9067                                  2, DECODE(p.CUSTOMER_ID, NULL, TO_CHAR(NULL),
9068                                            0, TO_CHAR(NULL),
9069                                                    MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
9070                                                      p.CUSTOMER_ID,
9071                                                      p.SHIP_TO_SITE_ID,
9072                                                      p.inventory_item_id,
9073                                                      p.organization_id,
9074                                                      p.sr_instance_id,
9075                                                      trunc(Decode(p_sup_atp_info_rec.sup_cap_type,
9076                                                           1, p.new_ship_date,
9077                                                           p.new_dock_date
9078                                                      )), --4135752
9079                                                      l_level_id,
9080                                                      NULL))),
9081 		           l_demand_class, 1,
9082 			   NULL, NVL(MIHM.allocation_percent/100,  --4365873
9083 			   /*NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(p.sr_instance_id,
9084                                                           p.inventory_item_id ,
9085 				                          p.organization_id,
9086                                                           null,
9087                                                           null,
9088 				                          l_demand_class,
9089                                                           trunc(Decode(p_sup_atp_info_rec.sup_cap_type,
9090                                                                         1, p.new_ship_date,
9091                                                                         p.new_dock_date
9092                                                           ))),*/
9093                                                            1), --4135752
9094                            DECODE(MIHM.allocation_percent/100,
9095                            /*DECODE(MSC_AATP_FUNC.Get_DC_Alloc_Percent(p.sr_instance_id,
9096                                                        p.inventory_item_id,
9097                                                        p.organization_id,
9098                                                        null,
9099                                                        null,
9100                                                        l_demand_class,
9101                                                        trunc(Decode(p_sup_atp_info_rec.sup_cap_type,  --4135752
9102                                                           1, p.new_ship_date,
9103                                                           p.new_dock_date
9104                                                        ))),*/
9105 				   NULL, 1,
9106 				   0)) quantity
9107          -- Supplier Capacity (SCLT) Changes Begin
9108          FROM   msc_supplies p,msc_item_hierarchy_mv MIHM
9109          WHERE  (p.order_type IN (5, 2)
9110                 OR (MSC_ATP_REQ.G_PURCHASE_ORDER_PREFERENCE = trunc(MSC_ATP_REQ.G_PROMISE_DATE) --4135752
9111                      AND p.order_type = 1 AND p.promised_date IS NULL))
9112          -- Supplier Capacity (SCLT) Accumulation Ignore Purchase Orders
9113          -- WHERE  p.order_type IN (5, 1, 2)
9114          AND    p.plan_id = p_sup_atp_info_rec.plan_id
9115          AND    p.sr_instance_id = p_sup_atp_info_rec.instance_id
9116          --AND    p.inventory_item_id = p_sup_atp_info_rec.inventory_item_id
9117    -- 1214694      AND    p.organization_id = p_sup_atp_info_rec.organization_id
9118          AND    p.supplier_id  = p_sup_atp_info_rec.supplier_id
9119          AND    NVL(p.supplier_site_id, -1) = NVL(p_sup_atp_info_rec.supplier_site_id, -1)
9120                 -- Exclude Cancelled Supplies 2460645
9121          AND    NVL(P.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
9122                  --consider ATP inserted PO only and palling PO will be tied to forecats
9123          AND    ((p.inventory_item_id = l_inv_item_id and p.record_source=2) OR
9124                     p.inventory_item_id in
9125                            (select inventory_item_id from msc_system_items msi
9126                             where  msi.base_item_id = l_inv_item_id
9127                             and    msi.plan_id = p_sup_atp_info_rec.plan_id
9128                             and    msi.organization_id = p_sup_atp_info_rec.organization_id
9129                             and    msi.base_item_id = l_inv_item_id))
9130          AND    trunc(Decode(p_sup_atp_info_rec.sup_cap_type, 1, p.new_ship_date,p.new_dock_date)) --4055719 --4135752
9131                       <= trunc(least(p_sup_atp_info_rec.last_cap_date,l_cutoff_date))                    --4135752
9132          -- Supplier Capacity (SCLT) Changes End
9133       --4365873
9134       AND    p.INVENTORY_ITEM_ID = MIHM.INVENTORY_ITEM_ID(+)
9135       AND    p.SR_INSTANCE_ID = MIHM.SR_INSTANCE_ID (+)
9136       AND    p.ORGANIZATION_ID = MIHM.ORGANIZATION_ID (+)
9137       AND    decode(MIHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
9138       AND    trunc(Decode(p_sup_atp_info_rec.sup_cap_type, 1, p.new_ship_date,p.new_dock_date)) >= MIHM.effective_date (+)
9139       AND    trunc(Decode(p_sup_atp_info_rec.sup_cap_type, 1, p.new_ship_date,p.new_dock_date)) <= MIHM.disable_date (+)
9140       AND    MIHM.demand_class (+) = l_demand_class
9141          )
9142          GROUP BY l_date
9143          ORDER BY l_DATE;--4698199
9144          --- bug 1657855, remove support for min alloc
9145          --l_current_atp.limit_qty := l_current_atp.atp_qty;
9146       ELSE
9147           -- we don't want details
9148          SELECT 	trunc(l_date), --4135752
9149       		   SUM(quantity)
9150          BULK COLLECT INTO
9151                	   l_current_atp.atp_period,
9152                	   l_current_atp.atp_qty
9153          FROM (
9154          SELECT GREATEST(cs.calendar_date,l_sys_next_date) l_date, --bug3333114
9155 	        cs.capacity*(1+NVL(MSC_ATP_FUNC.get_tolerance_percentage(
9156                                       p_sup_atp_info_rec.instance_id, p_sup_atp_info_rec.plan_id,
9157                                       p_sup_atp_info_rec.inventory_item_id, p_sup_atp_info_rec.organization_id,
9158                                       p_sup_atp_info_rec.supplier_id, p_sup_atp_info_rec.supplier_site_id,
9159                                       cs.seq_num - p_sup_atp_info_rec.sysdate_seq_num),0))*
9160 		   /*NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(l_instance_id,
9161                                             s.inventory_item_id,
9162                                             p_sup_atp_info_rec.organization_id,
9163                                             null,
9164                                             null,
9165                                             l_demand_class,
9166                                             c.calendar_date), 1) quantity*/
9167                     NVL(MIHM.allocation_percent/100, 1) quantity --4365873
9168          FROM
9169       (
9170       SELECT
9171       	    c.calendar_date,
9172       	    c.seq_num,
9173 	    s.inventory_item_id,
9174 	    s.sr_instance_id,
9175 	    S.ORGANIZATION_ID,
9176 	    S.capacity
9177          FROM   msc_calendar_dates c,
9178                 msc_supplier_capacities s
9179          WHERE  s.inventory_item_id = p_sup_atp_info_rec.inventory_item_id
9180          AND    s.sr_instance_id = p_sup_atp_info_rec.instance_id
9181          AND    s.plan_id = p_sup_atp_info_rec.plan_id
9182          AND    s.organization_id = p_sup_atp_info_rec.organization_id
9183          AND    s.supplier_id = p_sup_atp_info_rec.supplier_id
9184          AND    NVL(s.supplier_site_id, -1) = NVL(p_sup_atp_info_rec.supplier_site_id, -1)
9185          AND    c.calendar_date BETWEEN trunc(s.from_date)
9186                                 --AND NVL(s.to_date,l_cutoff_date)
9187                                 AND trunc(NVL(s.to_date,least(p_sup_atp_info_rec.last_cap_date,l_cutoff_date))) --4055719
9188          AND    (c.seq_num IS NOT NULL or p_sup_atp_info_rec.manufacturing_cal_code = '@@@')
9189          AND    c.calendar_code = l_calendar_code
9190          AND    c.exception_set_id = l_calendar_exception_set_id
9191          AND    c.sr_instance_id = l_instance_id
9192          AND    c.calendar_date >= NVL(p_sup_atp_info_rec.sup_cap_cum_date, l_plan_start_date))CS,
9193       msc_item_hierarchy_mv mihm
9194       WHERE
9195       --4365873
9196       	     CS.INVENTORY_ITEM_ID = MIHM.INVENTORY_ITEM_ID(+)
9197       AND    CS.SR_INSTANCE_ID = MIHM.SR_INSTANCE_ID (+)
9198       AND    CS.ORGANIZATION_ID = MIHM.ORGANIZATION_ID (+)
9199       AND    decode(MIHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
9200       AND    CS.calendar_date >= MIHM.effective_date (+)
9201       AND    CS.calendar_date <= MIHM.disable_date (+)
9202       AND    MIHM.demand_class (+) = l_demand_class
9203          -- Supplier Capacity (SCLT) Accumulation starts from this date.
9204          -- AND    c.calendar_date >= l_plan_start_date -- bug 1169467
9205          UNION ALL
9206          SELECT GREATEST(trunc(Decode(p_sup_atp_info_rec.sup_cap_type,  --4135752
9207                    1, p.new_ship_date,
9208                    p.new_dock_date
9209                 )),l_sys_next_date) l_date, --bug3333114
9210                 -- ship_rec_cal rearrange signs to get rid of multiply times -1
9211                 (NVL(p.implement_quantity,0) - p.new_order_quantity)*
9212 		   DECODE(DECODE(G_HIERARCHY_PROFILE,
9213                                  --2424357
9214                                  1, DECODE(p.DEMAND_CLASS, null, null,
9215                                        DECODE(l_demand_class, '-1',
9216                                            MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
9217                                                      null,
9218                                                      null,
9219                                                      p.inventory_item_id,
9220                                                      p.organization_id,
9221                                                      p.sr_instance_id,
9222                                                      trunc(Decode(p_sup_atp_info_rec.sup_cap_type,  --4135752
9223                                                            1, p.new_ship_date,
9224                                                            p.new_dock_date
9225                                                      )),
9226                                                      l_level_id,
9227                                                      p.DEMAND_CLASS), p.DEMAND_CLASS)),
9228                                  2, DECODE(p.CUSTOMER_ID, NULL, TO_CHAR(NULL),
9229                                            0, TO_CHAR(NULL),
9230                                                    MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
9231                                                      p.CUSTOMER_ID,
9232                                                      p.SHIP_TO_SITE_ID,
9233                                                      p.inventory_item_id,
9234                                                      p.organization_id,
9235                                                      p.sr_instance_id,
9236                                                      trunc(Decode(p_sup_atp_info_rec.sup_cap_type,  --4135752
9237                                                            1, p.new_ship_date,
9238                                                            p.new_dock_date
9239                                                      )),
9240                                                      l_level_id,
9241                                                      NULL))),
9242 		           l_demand_class, 1,
9243 			   NULL, NVL(MIHM.allocation_percent/100,  --4365874
9244 			   /*NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(p.sr_instance_id,
9245                                                           p.inventory_item_id ,
9246 				                          p.organization_id,
9247                                                           null,
9248                                                           null,
9249 				                          l_demand_class,
9250                                                           trunc(Decode(p_sup_atp_info_rec.sup_cap_type,  --4135752
9251                                                              1, p.new_ship_date,
9252                                                              p.new_dock_date
9253                                                           ))),*/
9254                                                            1), --4135752
9255                            DECODE(MIHM.allocation_percent/100,
9256 			   /*DECODE(MSC_AATP_FUNC.Get_DC_Alloc_Percent(p.sr_instance_id,
9257                                                        p.inventory_item_id,
9258                                                        p.organization_id,
9259                                                        null,
9260                                                        null,
9261                                                        l_demand_class,
9262                                                        trunc(Decode(p_sup_atp_info_rec.sup_cap_type, --4135752
9263                                                              1, p.new_ship_date,
9264                                                              p.new_dock_date
9265                                                        ))), */
9266 				   NULL, 1,
9267 				   0)) quantity
9268          -- Supplier Capacity (SCLT) Changes Begin
9269          FROM   msc_supplies p,msc_item_hierarchy_mv MIHM
9270          WHERE  (p.order_type IN (5, 2)
9271                 OR (MSC_ATP_REQ.G_PURCHASE_ORDER_PREFERENCE = MSC_ATP_REQ.G_PROMISE_DATE
9272                      AND p.order_type = 1 AND p.promised_date IS NULL))
9273          -- Supplier Capacity (SCLT) Accumulation Ignore Purchase Orders
9274          -- WHERE  p.order_type IN (5, 1, 2)
9275          AND    p.plan_id = p_sup_atp_info_rec.plan_id
9276          AND    p.sr_instance_id = p_sup_atp_info_rec.instance_id
9277          AND    p.inventory_item_id = p_sup_atp_info_rec.inventory_item_id
9278    -- 1214694      AND    p.organization_id = p_sup_atp_info_rec.organization_id
9279          AND    p.supplier_id  = p_sup_atp_info_rec.supplier_id
9280          AND    NVL(p.supplier_site_id, -1) = NVL(p_sup_atp_info_rec.supplier_site_id, -1)
9281                 -- Exclude Cancelled Supplies 2460645
9282          AND    NVL(P.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
9283          AND    trunc(Decode(p_sup_atp_info_rec.sup_cap_type, 1, p.new_ship_date,p.new_dock_date)) --4055719 --4135752
9284                        <= least(p_sup_atp_info_rec.last_cap_date,l_cutoff_date)
9285       --4365873
9286       AND    p.INVENTORY_ITEM_ID = MIHM.INVENTORY_ITEM_ID(+)
9287       AND    p.SR_INSTANCE_ID = MIHM.SR_INSTANCE_ID (+)
9288       AND    p.ORGANIZATION_ID = MIHM.ORGANIZATION_ID (+)
9289       AND    decode(MIHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
9290       AND    trunc(Decode(p_sup_atp_info_rec.sup_cap_type, 1, p.new_ship_date,p.new_dock_date)) >= MIHM.effective_date (+)
9291       AND    trunc(Decode(p_sup_atp_info_rec.sup_cap_type, 1, p.new_ship_date,p.new_dock_date)) <= MIHM.disable_date (+)
9292       AND    MIHM.demand_class (+) = l_demand_class
9293          )
9294          GROUP BY l_date
9295          ORDER BY l_DATE;--4698199
9296          --- bug 1657855, remove support for min alloc
9297          --l_current_atp.limit_qty := l_current_atp.atp_qty;
9298       END IF;
9299     ELSE
9300         --IF (NVL(p_sup_atp_info_rec.insert_flag, 0) <> 0  AND
9301         -- l_demand_class <> p_sup_atp_info_rec.demand_class) THEN we want details
9302 	MSC_ATP_DB_UTILS.Clear_SD_Details_Temp();
9303 
9304         IF l_check_cap_model_flag = 1 THEN
9305 
9306            -- dsting: s/d details performance enh
9307            INSERT INTO msc_atp_sd_details_temp (
9308            	ATP_Level,
9309            	Order_line_id,
9310            	Scenario_Id,
9311            	Inventory_Item_Id,
9312            	Request_Item_Id,
9313 	        Organization_Id,
9314                 Department_Id,
9315 	        Resource_Id,
9316                 Supplier_Id,
9317                 Supplier_Site_Id,
9318 	        From_Organization_Id,
9319 	        From_Location_Id,
9320 	        To_Organization_Id,
9321 	        To_Location_Id,
9322 	        Ship_Method,
9323 	        UOM_code,
9324 	        Supply_Demand_Type,
9325 	        Supply_Demand_Source_Type,
9326 	        Supply_Demand_Source_Type_Name,
9327 	        Identifier1,
9328 	        Identifier2,
9329 	        Identifier3,
9330 	        Identifier4,
9331 	        Supply_Demand_Quantity,
9332 	        Supply_Demand_Date,
9333 	        Disposition_Type,
9334 	        Disposition_Name,
9335 	        Pegging_Id,
9336 	        End_Pegging_Id,
9337 	        creation_date,
9338 	        created_by,
9339 	        last_update_date,
9340 	        last_updated_by,
9341 	        last_update_login,
9342 	        Unallocated_Quantity
9343             )
9344 
9345             (SELECT     p_sup_atp_info_rec.level col1,
9346 		p_identifier col2,
9347                 p_sup_atp_info_rec.scenario_id col3,
9348                 l_null_num col4 ,
9349                 l_null_num col5,
9350 		p_sup_atp_info_rec.organization_id col6,
9351                 l_null_num col7,
9352                 l_null_num col8,
9353                 p_sup_atp_info_rec.supplier_id col9,
9354                 p_sup_atp_info_rec.supplier_site_id col10,
9355                 l_null_num col11,
9356                 l_null_num col12,
9357                 l_null_num col13,
9358                 l_null_num col14,
9359 		l_null_char col15,
9360 		l_uom_code col16,
9361 		2 col17, -- supply
9362 		l_null_num col18,
9363                 l_null_char col19,
9364 		p_sup_atp_info_rec.instance_id col20,
9365                 l_null_num col21,
9366 		l_null_num col22,
9367 		l_null_num col23,
9368                 cs.capacity*(1+NVL(MSC_ATP_FUNC.get_tolerance_percentage(
9369                                    p_sup_atp_info_rec.instance_id,
9370                                    p_sup_atp_info_rec.plan_id,
9371                                    l_inv_item_id,
9372                                    p_sup_atp_info_rec.organization_id,
9373                                    p_sup_atp_info_rec.supplier_id,
9374                                    p_sup_atp_info_rec.supplier_site_id,
9375                                    cs.seq_num - p_sup_atp_info_rec.sysdate_seq_num),0)) *
9376                                    NVL(MIHM.allocation_percent/100,  --4365873
9377                                    /*NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
9378                                                      l_instance_id,
9379                                                      s.inventory_item_id,
9380                                                      p_sup_atp_info_rec.organization_id,
9381                                                      null,
9382                                                      null,
9383                                                      l_demand_class,
9384                                                      c.calendar_date),*/
9385                                                       1) col24,
9386 		GREATEST(CS.CALENDAR_DATE,l_sys_next_date) col25, --bug3333114
9387                 l_null_num col26,
9388                 l_null_char col27,
9389                 l_null_num col28,
9390                 l_null_num col29,
9391 		-- ship_rec_cal changes begin
9392 		l_sysdate,
9393 		G_USER_ID,
9394 		l_sysdate,
9395 		G_USER_ID,
9396 		G_USER_ID,
9397 		-- ship_rec_cal changes end
9398 		-- Unallocated_Quantity
9399                 cs.capacity*(1+NVL(MSC_ATP_FUNC.get_tolerance_percentage(
9400                                    p_sup_atp_info_rec.instance_id,
9401                                    p_sup_atp_info_rec.plan_id,
9402                                    l_inv_item_id,
9403                                    p_sup_atp_info_rec.organization_id,
9404                                    p_sup_atp_info_rec.supplier_id,
9405                                    p_sup_atp_info_rec.supplier_site_id,
9406                                    cs.seq_num - p_sup_atp_info_rec.sysdate_seq_num),0))
9407          FROM
9408          (
9409          SELECT
9410             s.capacity,
9411 	    c.calendar_date,
9412 	    s.inventory_item_id,
9413 	    s.sr_instance_id,
9414 	    s.organization_id,
9415 	    c.seq_num
9416 
9417                 FROM   msc_calendar_dates c,
9418                        msc_supplier_capacities s
9419                 --WHERE  s.inventory_item_id = p_sup_atp_info_rec.inventory_item_id
9420                 WHERE  s.inventory_item_id = l_inv_item_id
9421                 AND    s.sr_instance_id = p_sup_atp_info_rec.instance_id
9422                 AND    s.plan_id = p_sup_atp_info_rec.plan_id
9423                 AND    s.organization_id = p_sup_atp_info_rec.organization_id
9424                 AND    s.supplier_id = p_sup_atp_info_rec.supplier_id
9425                 AND    NVL(s.supplier_site_id, -1) = NVL(p_sup_atp_info_rec.supplier_site_id, -1)
9426                 AND    c.calendar_date BETWEEN trunc(s.from_date)
9427                                 --AND NVL(s.to_date,l_cutoff_date)
9428                                 AND trunc(NVL(s.to_date,least(p_sup_atp_info_rec.last_cap_date,l_cutoff_date))) --4055719
9429                 AND    (c.seq_num IS NOT NULL or p_sup_atp_info_rec.manufacturing_cal_code = '@@@')
9430                 AND    c.calendar_code = l_calendar_code
9431                 AND    c.exception_set_id = l_calendar_exception_set_id
9432                 AND    c.sr_instance_id = l_instance_id
9433                 AND    c.calendar_date >= NVL(p_sup_atp_info_rec.sup_cap_cum_date, l_plan_start_date))CS,
9434          msc_item_hierarchy_mv MIHM
9435          WHERE
9436          --4365873
9437                 cs.INVENTORY_ITEM_ID = MIHM.INVENTORY_ITEM_ID(+)
9438       	 AND    cs.SR_INSTANCE_ID = MIHM.SR_INSTANCE_ID (+)
9439       	 AND    cs.ORGANIZATION_ID = MIHM.ORGANIZATION_ID (+)
9440       	 --AND    decode(MIHM.level_id,-1,1,2) (+) = (select decode(fnd_profile.value('XXXX'),1,1,2) from dual)
9441       	 --AND   MIHM.level_id(+)=-1
9442       	 AND    decode(MIHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
9443       	 AND    cs.calendar_date >= MIHM.effective_date (+)
9444       	 AND    cs.calendar_date <= MIHM.disable_date (+)
9445       	 AND    MIHM.demand_class (+) = l_demand_class
9446                 -- Supplier Capacity (SCLT) Accumulation starts from this date.
9447                 -- AND    c.calendar_date >= l_plan_start_date -- bug 1169467
9448                 UNION ALL
9449                 SELECT p_sup_atp_info_rec.level col1,
9450                 p_identifier col2,
9451                 p_sup_atp_info_rec.scenario_id col3,
9452                 l_null_num col4 ,
9453                 l_null_num col5,
9454                 p_sup_atp_info_rec.organization_id col6,
9455                 l_null_num col7,
9456                 l_null_num col8,
9457                 p_sup_atp_info_rec.supplier_id col9,
9458                 p_sup_atp_info_rec.supplier_site_id col10,
9459                 l_null_num col11,
9460                 l_null_num col12,
9461                 l_null_num col13,
9462                 l_null_num col14,
9463                 l_null_char col15,
9464                 l_uom_code col16,
9465                 1 col17, -- demand
9466                 p.order_type col18,
9467                 l_null_char col19,
9468                 p_sup_atp_info_rec.instance_id col20,
9469                 l_null_num col21,
9470                 p.TRANSACTION_ID col22,
9471                 l_null_num col23,
9472                 -- ship_rec_cal rearrange signs to get rid of multiply times -1
9473                 (NVL(p.implement_quantity,0) - p.new_order_quantity)*
9474                 DECODE(DECODE(G_HIERARCHY_PROFILE,
9475                               --2424357
9476                               1, DECODE(p.DEMAND_CLASS, null, null,
9477                                     DECODE(l_demand_class, '-1',
9478                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
9479                                                   null,
9480                                                   null,
9481                                                   p.inventory_item_id,
9482                                                   p.organization_id,
9483                                                   p.sr_instance_id,
9484                                                   trunc(Decode(p_sup_atp_info_rec.sup_cap_type, --4135752
9485                                                      1, p.new_ship_date,
9486                                                      p.new_dock_date
9487                                                   )),
9488                                                   l_level_id,
9489                                                    p.DEMAND_CLASS), p.DEMAND_CLASS)),
9490                               2, DECODE(p.CUSTOMER_ID, NULL, TO_CHAR(NULL),
9491                                         0, TO_CHAR(NULL),
9492                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
9493                                                   p.CUSTOMER_ID,
9494                                                   p.SHIP_TO_SITE_ID,
9495                                                   p.inventory_item_id,
9496                                                   p.organization_id,
9497                                                   p.sr_instance_id,
9498                                                   trunc(Decode(p_sup_atp_info_rec.sup_cap_type, --4135752
9499                                                      1, p.new_ship_date,
9500                                                      p.new_dock_date
9501                                                   )),
9502                                                   l_level_id,
9503                                                   NULL))),
9504                         l_demand_class, 1,
9505                         NULL, NVL(MIHM.allocation_percent/100,  --4365873
9506                         /*NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(p.sr_instance_id,
9507                                                        p.inventory_item_id ,
9508                                                        p.organization_id,
9509                                                        null,
9510                                                        null,
9511                                                        l_demand_class,
9512                                                        trunc(Decode(p_sup_atp_info_rec.sup_cap_type, --4135752
9513                                                           1, p.new_ship_date,
9514                                                           p.new_dock_date
9515                                                        ))),*/
9516                                                         1),
9517                         DECODE(MIHM.allocation_percent/100,
9518                         /*DECODE(MSC_AATP_FUNC.Get_DC_Alloc_Percent(p.sr_instance_id,
9519                                                     p.inventory_item_id,
9520                                                     p.organization_id,
9521                                                     null,
9522                                                     null,
9523                                                     l_demand_class,
9524                                                     trunc(Decode(p_sup_atp_info_rec.sup_cap_type, --4135752
9525                                                        1, p.new_ship_date,
9526                                                        p.new_dock_date
9527                                                     ))),*/
9528                                 NULL, 1,
9529                                 0)) col24,
9530                 GREATEST(trunc(Decode(p_sup_atp_info_rec.sup_cap_type,  --4135752
9531                    1, p.new_ship_date,
9532                    p.new_dock_date
9533                 )),l_sys_next_date) col25, --bug3333114
9534                 l_null_num col26,
9535                 -- Bug 2771075. For Planned Orders, we will populate transaction_id
9536 		-- in the disposition_name column to be consistent with Planning.
9537 		-- p.order_number col27,
9538 		DECODE(p.order_type, 5, to_char(p.transaction_id), p.order_number ) col27,
9539                 l_null_num col28,
9540 		l_null_num col29,
9541 		-- ship_rec_cal changes begin
9542 		l_sysdate,
9543 		G_USER_ID,
9544 		l_sysdate,
9545 		G_USER_ID,
9546 		G_USER_ID,
9547 		-- ship_rec_cal changes end
9548 		-- Unallocated_Quantity
9549                 -- ship_rec_cal rearrange signs to get rid of multiply times -1
9550                 (NVL(p.implement_quantity,0) - p.new_order_quantity)
9551              -- Supplier Capacity (SCLT) Changes Begin
9552              FROM   msc_supplies p,msc_item_hierarchy_mv MIHM
9553              WHERE  (p.order_type IN (5, 2)
9554                     OR (MSC_ATP_REQ.G_PURCHASE_ORDER_PREFERENCE = MSC_ATP_REQ.G_PROMISE_DATE
9555                   AND p.order_type = 1 AND p.promised_date IS NULL))
9556 
9557              -- Supplier Capacity (SCLT) Accumulation Ignore Purchase Orders
9558              -- WHERE  p.order_type IN (5, 1, 2)
9559              AND    p.plan_id = p_sup_atp_info_rec.plan_id
9560              AND    p.sr_instance_id = p_sup_atp_info_rec.instance_id
9561              --AND    p.inventory_item_id = p_sup_atp_info_rec.inventory_item_id
9562        -- 1214694      AND    p.organization_id = p_sup_atp_info_rec.organization_id
9563              AND    p.supplier_id  = p_sup_atp_info_rec.supplier_id
9564              AND    NVL(p.supplier_site_id, -1) = NVL(p_sup_atp_info_rec.supplier_site_id, -1)
9565              -- Exclude Cancelled Supplies 2460645
9566              AND    NVL(P.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
9567                     --consider ATP inserted POs only. Do not use Planning inserted POs as
9568                     -- theu would be tied to forecast
9569              AND    ((p.inventory_item_id = l_inv_item_id and p.record_source=2) OR
9570                     p.inventory_item_id in
9571                            (select inventory_item_id from msc_system_items msi
9572                             where  msi.base_item_id = l_inv_item_id
9573                             and    msi.plan_id = p_sup_atp_info_rec.plan_id
9574                             and    msi.organization_id = p_sup_atp_info_rec.organization_id
9575                             and    msi.base_item_id = l_inv_item_id))
9576              AND    trunc(Decode(p_sup_atp_info_rec.sup_cap_type, 1, p.new_ship_date,p.new_dock_date)) --4055719 --4135752
9577                       <= least(p_sup_atp_info_rec.last_cap_date,l_cutoff_date)
9578       --4365873
9579       AND    p.INVENTORY_ITEM_ID = MIHM.INVENTORY_ITEM_ID(+)
9580       AND    p.SR_INSTANCE_ID = MIHM.SR_INSTANCE_ID (+)
9581       AND    p.ORGANIZATION_ID = MIHM.ORGANIZATION_ID (+)
9582       --AND    MIHM.level_id (+) = decode(G_HIERARCHY_PROFILE,1,-1 )
9583       AND    decode(MIHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
9584       AND    trunc(Decode(p_sup_atp_info_rec.sup_cap_type, 1, p.new_ship_date,p.new_dock_date)) >= MIHM.effective_date (+)
9585       AND    trunc(Decode(p_sup_atp_info_rec.sup_cap_type, 1, p.new_ship_date,p.new_dock_date)) <= MIHM.disable_date (+)
9586       AND    MIHM.demand_class (+) = l_demand_class
9587        )
9588        ; -- dsting removed order by col25;
9589         ELSE
9590 
9591            -- dsting: s/d details performance enh
9592            INSERT INTO msc_atp_sd_details_temp (
9593            	ATP_Level,
9594            	Order_line_id,
9595            	Scenario_Id,
9596            	Inventory_Item_Id,
9597            	Request_Item_Id,
9598 	        Organization_Id,
9599                 Department_Id,
9600 	        Resource_Id,
9601                 Supplier_Id,
9602                 Supplier_Site_Id,
9603 	        From_Organization_Id,
9604 	        From_Location_Id,
9605 	        To_Organization_Id,
9606 	        To_Location_Id,
9607 	        Ship_Method,
9608 	        UOM_code,
9609 	        Supply_Demand_Type,
9610 	        Supply_Demand_Source_Type,
9611 	        Supply_Demand_Source_Type_Name,
9612 	        Identifier1,
9613 	        Identifier2,
9614 	        Identifier3,
9615 	        Identifier4,
9616 	        Supply_Demand_Quantity,
9617 	        Supply_Demand_Date,
9618 	        Disposition_Type,
9619 	        Disposition_Name,
9620 	        Pegging_Id,
9621 	        End_Pegging_Id,
9622 	        creation_date,
9623 	        created_by,
9624 	        last_update_date,
9625 	        last_updated_by,
9626 	        last_update_login,
9627 	        Unallocated_Quantity
9628             )
9629 
9630             (SELECT     p_sup_atp_info_rec.level col1,
9631 		p_identifier col2,
9632                 p_sup_atp_info_rec.scenario_id col3,
9633                 l_null_num col4 ,
9634                 l_null_num col5,
9635 		p_sup_atp_info_rec.organization_id col6,
9636                 l_null_num col7,
9637                 l_null_num col8,
9638                 p_sup_atp_info_rec.supplier_id col9,
9639                 p_sup_atp_info_rec.supplier_site_id col10,
9640                 l_null_num col11,
9641                 l_null_num col12,
9642                 l_null_num col13,
9643                 l_null_num col14,
9644 		l_null_char col15,
9645 		l_uom_code col16,
9646 		2 col17, -- supply
9647 		l_null_num col18,
9648                 l_null_char col19,
9649 		p_sup_atp_info_rec.instance_id col20,
9650                 l_null_num col21,
9651 		l_null_num col22,
9652 		l_null_num col23,
9653                 cs.capacity*(1+NVL(MSC_ATP_FUNC.get_tolerance_percentage(
9654                                    p_sup_atp_info_rec.instance_id,
9655                                    p_sup_atp_info_rec.plan_id,
9656                                    p_sup_atp_info_rec.inventory_item_id,
9657                                    p_sup_atp_info_rec.organization_id,
9658                                    p_sup_atp_info_rec.supplier_id,
9659                                    p_sup_atp_info_rec.supplier_site_id,
9660                                    cs.seq_num - p_sup_atp_info_rec.sysdate_seq_num),0)) *
9661                                    NVL(MIHM.allocation_percent/100,  --4365873
9662                                    /*NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
9663                                                      l_instance_id,
9664                                                      cs.inventory_item_id,
9665                                                      p_sup_atp_info_rec.organization_id,
9666                                                      null,
9667                                                      null,
9668                                                      l_demand_class,
9669                                                      c.calendar_date),*/
9670                                                       1) col24,
9671 		GREATEST(CS.CALENDAR_DATE,l_sys_next_date) col25, --bug3333114
9672                 l_null_num col26,
9673                 l_null_char col27,
9674                 l_null_num col28,
9675                 l_null_num col29,
9676 		-- ship_rec_cal changes begin
9677 		l_sysdate,
9678 		G_USER_ID,
9679 		l_sysdate,
9680 		G_USER_ID,
9681 		G_USER_ID,
9682 		-- ship_rec_cal changes end
9683 		-- Unallocated_Quantity
9684                 cs.capacity*(1+NVL(MSC_ATP_FUNC.get_tolerance_percentage(
9685                                    p_sup_atp_info_rec.instance_id,
9686                                    p_sup_atp_info_rec.plan_id,
9687                                    p_sup_atp_info_rec.inventory_item_id,
9688                                    p_sup_atp_info_rec.organization_id,
9689                                    p_sup_atp_info_rec.supplier_id,
9690                                    p_sup_atp_info_rec.supplier_site_id,
9691                                    cs.seq_num - p_sup_atp_info_rec.sysdate_seq_num),0))
9692          FROM
9693          (
9694          SELECT
9695             s.capacity,
9696 	    c.calendar_date,
9697 	    s.inventory_item_id,
9698 	    s.sr_instance_id,
9699 	    s.organization_id,
9700 	    c.seq_num
9701                 FROM   msc_calendar_dates c,
9702                        msc_supplier_capacities s
9703                 WHERE  s.inventory_item_id = p_sup_atp_info_rec.inventory_item_id
9704                 AND    s.sr_instance_id = p_sup_atp_info_rec.instance_id
9705                 AND    s.plan_id = p_sup_atp_info_rec.plan_id
9706                 AND    s.organization_id = p_sup_atp_info_rec.organization_id
9707                 AND    s.supplier_id = p_sup_atp_info_rec.supplier_id
9708                 AND    NVL(s.supplier_site_id, -1) = NVL(p_sup_atp_info_rec.supplier_site_id, -1)
9709                 AND    c.calendar_date BETWEEN trunc(s.from_date)
9710                                 --AND NVL(s.to_date,l_cutoff_date)
9711                                 AND trunc(NVL(s.to_date,least(p_sup_atp_info_rec.last_cap_date,l_cutoff_date))) --4055719
9712                 AND    (c.seq_num IS NOT NULL or p_sup_atp_info_rec.manufacturing_cal_code = '@@@')
9713                 AND    c.calendar_code = l_calendar_code
9714                 AND    c.exception_set_id = l_calendar_exception_set_id
9715                 AND    c.sr_instance_id = l_instance_id
9716                 AND    c.calendar_date >= NVL(p_sup_atp_info_rec.sup_cap_cum_date, l_plan_start_date))CS,
9717          msc_item_hierarchy_mv MIHM
9718          WHERE
9719          --4365873
9720                 cs.INVENTORY_ITEM_ID = MIHM.INVENTORY_ITEM_ID(+)
9721       	 AND    cs.SR_INSTANCE_ID = MIHM.SR_INSTANCE_ID (+)
9722       	 AND    cs.ORGANIZATION_ID = MIHM.ORGANIZATION_ID (+)
9723       	 --AND    decode(MIHM.level_id,-1,1,2) (+) = (select decode(fnd_profile.value('XXXX'),1,1,2) from dual)
9724       	 --AND   MIHM.level_id(+)=-1
9725       	 AND    decode(MIHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
9726       	 AND    cs.calendar_date >= MIHM.effective_date (+)
9727       	 AND    cs.calendar_date <= MIHM.disable_date (+)
9728       	 AND    MIHM.demand_class (+) = l_demand_class
9729                 -- Supplier Capacity (SCLT) Accumulation starts from this date.
9730                 -- AND    c.calendar_date >= l_plan_start_date -- bug 1169467
9731                 UNION ALL
9732                 SELECT p_sup_atp_info_rec.level col1,
9733                 p_identifier col2,
9734                 p_sup_atp_info_rec.scenario_id col3,
9735                 l_null_num col4 ,
9736                 l_null_num col5,
9737                 p_sup_atp_info_rec.organization_id col6,
9738                 l_null_num col7,
9739                 l_null_num col8,
9740                 p_sup_atp_info_rec.supplier_id col9,
9741                 p_sup_atp_info_rec.supplier_site_id col10,
9742                 l_null_num col11,
9743                 l_null_num col12,
9744                 l_null_num col13,
9745                 l_null_num col14,
9746                 l_null_char col15,
9747                 l_uom_code col16,
9748                 1 col17, -- demand
9749                 p.order_type col18,
9750                 l_null_char col19,
9751                 p_sup_atp_info_rec.instance_id col20,
9752                 l_null_num col21,
9753                 TRANSACTION_ID col22,
9754                 l_null_num col23,
9755                 -- ship_rec_cal rearrange signs to get rid of multiply times -1
9756                 (NVL(p.implement_quantity,0) - p.new_order_quantity)*
9757                 DECODE(DECODE(G_HIERARCHY_PROFILE,
9758                               --2424357
9759                               1, DECODE(p.DEMAND_CLASS, null, null,
9760                                     DECODE(l_demand_class, '-1',
9761                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
9762                                                   null,
9763                                                   null,
9764                                                   p.inventory_item_id,
9765                                                   p.organization_id,
9766                                                   p.sr_instance_id,
9767                                                   trunc(Decode(p_sup_atp_info_rec.sup_cap_type, --4135752
9768                                                           1, p.new_ship_date,
9769                                                           p.new_dock_date
9770                                                   )),
9771                                                   l_level_id,
9772                                                    p.DEMAND_CLASS), p.DEMAND_CLASS)),
9773                               2, DECODE(p.CUSTOMER_ID, NULL, TO_CHAR(NULL),
9774                                         0, TO_CHAR(NULL),
9775                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
9776                                                   p.CUSTOMER_ID,
9777                                                   p.SHIP_TO_SITE_ID,
9778                                                   p.inventory_item_id,
9779                                                   p.organization_id,
9780                                                   p.sr_instance_id,
9781                                                   trunc(Decode(p_sup_atp_info_rec.sup_cap_type, --4135752
9782                                                           1, p.new_ship_date,
9783                                                           p.new_dock_date
9784                                                   )),
9785                                                   l_level_id,
9786                                                   NULL))),
9787                         l_demand_class, 1,
9788                         NULL, NVL(MIHM.allocation_percent/100,  --4365873
9789                         /*NULL, NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(p.sr_instance_id,
9790                                                        p.inventory_item_id ,
9791                                                        p.organization_id,
9792                                                        null,
9793                                                        null,
9794                                                        l_demand_class,
9795                                                        trunc(Decode(p_sup_atp_info_rec.sup_cap_type, --4135752
9796                                                           1, p.new_ship_date,
9797                                                           p.new_dock_date
9798                                                        ))),*/
9799                                                         1),
9800                         DECODE(MIHM.allocation_percent/100,
9801                         /*DECODE(MSC_AATP_FUNC.Get_DC_Alloc_Percent(p.sr_instance_id,
9802                                                     p.inventory_item_id,
9803                                                     p.organization_id,
9804                                                     null,
9805                                                     null,
9806                                                     l_demand_class,
9807                                                     trunc(Decode(p_sup_atp_info_rec.sup_cap_type, --4135752
9808                                                           1, p.new_ship_date,
9809                                                           p.new_dock_date
9810                                                     ))),*/
9811                                 NULL, 1,
9812                                 0)) col24,
9813                 GREATEST(trunc(Decode(p_sup_atp_info_rec.sup_cap_type, --4135752
9814                     1, p.new_ship_date,
9815                     p.new_dock_date
9816                 )),l_sys_next_date) col25, --bug3333114
9817                 l_null_num col26,
9818                 -- Bug 2771075. For Planned Orders, we will populate transaction_id
9819 		-- in the disposition_name column to be consistent with Planning.
9820 		-- p.order_number col27,
9821 		DECODE(p.order_type, 5, to_char(p.transaction_id), p.order_number ) col27,
9822                 l_null_num col28,
9823 		l_null_num col29,
9824 		-- ship_rec_cal changes begin
9825 		l_sysdate,
9826 		G_USER_ID,
9827 		l_sysdate,
9828 		G_USER_ID,
9829 		G_USER_ID,
9830 		-- ship_rec_cal changes end
9831 		-- Unallocated_Quantity
9832                 -- ship_rec_cal rearrange signs to get rid of multiply times -1
9833                 (NVL(p.implement_quantity,0) - p.new_order_quantity)
9834              -- Supplier Capacity (SCLT) Changes Begin
9835              FROM   msc_supplies p ,msc_item_hierarchy_mv MIHM
9836              WHERE  (p.order_type IN (5, 2)
9837                     OR (MSC_ATP_REQ.G_PURCHASE_ORDER_PREFERENCE = MSC_ATP_REQ.G_PROMISE_DATE
9838                   AND p.order_type = 1 AND p.promised_date IS NULL))
9839 
9840              -- Supplier Capacity (SCLT) Accumulation Ignore Purchase Orders
9841              -- WHERE  p.order_type IN (5, 1, 2)
9842              AND    p.plan_id = p_sup_atp_info_rec.plan_id
9843              AND    p.sr_instance_id = p_sup_atp_info_rec.instance_id
9844              AND    p.inventory_item_id = p_sup_atp_info_rec.inventory_item_id
9845        -- 1214694      AND    p.organization_id = p_sup_atp_info_rec.organization_id
9846              AND    p.supplier_id  = p_sup_atp_info_rec.supplier_id
9847              AND    NVL(p.supplier_site_id, -1) = NVL(p_sup_atp_info_rec.supplier_site_id, -1)
9848              -- Exclude Cancelled Supplies 2460645
9849              AND    NVL(P.DISPOSITION_STATUS_TYPE, 1) <> 2 -- Bug 2460645
9850              AND    trunc(Decode(p_sup_atp_info_rec.sup_cap_type, 1, p.new_ship_date,p.new_dock_date)) --4055719--4135752
9851                            <= least(p_sup_atp_info_rec.last_cap_date,l_cutoff_date)
9852              --4365873
9853       AND    p.INVENTORY_ITEM_ID = MIHM.INVENTORY_ITEM_ID(+)
9854       AND    p.SR_INSTANCE_ID = MIHM.SR_INSTANCE_ID (+)
9855       AND    p.ORGANIZATION_ID = MIHM.ORGANIZATION_ID (+)
9856       --AND    MIHM.level_id (+) = decode(G_HIERARCHY_PROFILE,1,-1 )
9857       AND    decode(MIHM.level_id (+),-1,1,2) = decode(G_HIERARCHY_PROFILE,1,1,2)
9858       AND    trunc(Decode(p_sup_atp_info_rec.sup_cap_type, 1, p.new_ship_date,p.new_dock_date)) >= MIHM.effective_date (+)
9859       AND    trunc(Decode(p_sup_atp_info_rec.sup_cap_type, 1, p.new_ship_date,p.new_dock_date)) <= MIHM.disable_date (+)
9860       AND    MIHM.demand_class (+) = l_demand_class
9861        )
9862        ; -- dsting removed order by col25;
9863      END IF;
9864      --=======================================================================================================
9865      -- ship_rec_cal changes end
9866      --=======================================================================================================
9867      -- for period ATP
9868      IF PG_DEBUG in ('Y', 'C') THEN
9869         msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'after insert into msc_atp_sd_details_temp');
9870      END IF;
9871      MSC_ATP_PROC.get_period_data_from_SD_temp(x_atp_period);
9872 
9873       l_current_atp.atp_period := x_atp_period.Period_Start_Date;
9874       l_current_atp.atp_qty := x_atp_period.Period_Quantity;
9875       -- bug 1657855, remove support for min alloc
9876       --l_current_atp.limit_qty := l_current_atp.atp_qty; -- 02/16
9877 
9878     END IF;
9879 
9880     IF PG_DEBUG in ('Y', 'C') THEN
9881        msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'right after the big query');
9882        Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
9883 		l_current_atp);
9884     END IF;
9885 
9886     -- do backward consumption for DCi
9887     MSC_ATP_PROC.Atp_Backward_Consume(l_current_atp.atp_qty);
9888 
9889     IF PG_DEBUG in ('Y', 'C') THEN
9890        msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'right after the backward consume');
9891        Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
9892 		l_current_atp);
9893     END IF;
9894 
9895     -- we have 3 records of tables.
9896     -- l_current_atp: stores the date and quantity for this demand class,
9897     --                and since we need to do backward consumption on this.
9898     -- l_current_steal_atp: stores the date and quantity from higher priority
9899     --                      demand class, this need to consume l_current_atp
9900     -- l_next_steal_atp : stores  the date and quantity for next priority
9901     --                    demand class to cunsume.  we need this because we may
9902     --                    have multiple demand classes at same priority .
9903     -- for example, we have DC1 in priority 1, DC21, DC22 in priority 2,
9904     -- DC3 in priority  3.
9905     -- now DC21 need to take care DC1, DC22 need to take care DC1 but not DC21,
9906     -- DC3 need to take care DC1, DC21, and DC22.  so if we are in the loop for
9907     -- DC22, than l_current_atp is the atp info for DC22,
9908     -- l_current_steal_atp is the atp info for DC1(which does not include DC21),
9909     -- and l_next_steal_atp is the stealing data that we need to take care
9910     -- for DC1, DC21 and DC22  when later on we move to the loop for DC3.
9911 
9912        -- do backward consumption if DC1 to DC(i-1) has any negative bucket,and
9913        -- the priority  is higher than DCi
9914        -- the l_current_atp is an in/out parameter
9915 
9916       -- for 1680719, since in hierarchy demand class we cannot
9917       -- judge the priority by just looking at the priority (we need
9918       -- the information from the parent, so the condition needs to be changed.
9919 
9920       IF l_level_id IN (-1, 1) THEN
9921         -- here is the old logic which should still be ok for level id 1 and -1
9922        IF (i > 1) THEN
9923 
9924         IF (l_demand_class_priority_tab(i) >
9925             l_demand_class_priority_tab (i-1)) THEN
9926         -- we don't need to change the l_current_steal_atp if we don't
9927         -- move to next priority.
9928         -- but we do need to change the l_current_steal_atp
9929         -- if we are in different priority  now.
9930 
9931           l_current_steal_atp := l_next_steal_atp;
9932 
9933           -- Added for bug 1409335. Need to initialize l_next_steal_atp
9934           -- otherwise quanities would be getting accumulated
9935           -- repeatedly.
9936           l_next_steal_atp := l_null_steal_atp;
9937         END IF;
9938        END IF;
9939       ELSE -- IF l_level_id IN (-1, 1) THEN
9940 
9941        IF (i > 1) THEN
9942 
9943         IF (l_class_tab(i) <> l_class_tab(i-1)) THEN
9944 
9945           -- class changed.  If priority of both classes are not the same,
9946           -- then we need to change the curr_steal_atp  at class level.
9947 
9948           IF PG_DEBUG in ('Y', 'C') THEN
9949              msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'class changed');
9950           END IF;
9951 
9952           IF trunc(l_demand_class_priority_tab(i), -3) >
9953              trunc(l_demand_class_priority_tab (i-1), -3) THEN
9954 
9955             IF PG_DEBUG in ('Y', 'C') THEN
9956                msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'class priority changed');
9957             END IF;
9958             l_class_curr_steal_atp := l_class_next_steal_atp;
9959             l_class_next_steal_atp := l_null_steal_atp;
9960           END IF;
9961 
9962           l_partner_next_steal_atp := l_null_steal_atp;
9963           l_partner_curr_steal_atp := l_null_steal_atp;
9964           l_partner_next_steal_atp := l_null_steal_atp;
9965           l_current_steal_atp := l_null_steal_atp;
9966           l_next_steal_atp := l_null_steal_atp;
9967 
9968         ELSE
9969           IF PG_DEBUG in ('Y', 'C') THEN
9970              msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'same class');
9971           END IF;
9972           IF (l_partner_tab(i) <> l_partner_tab(i-1)) THEN
9973             -- customer changed.  If priority of both customers are not the
9974             -- same, we need to change the curr_steal_atp  at partner level.
9975 
9976             IF PG_DEBUG in ('Y', 'C') THEN
9977                msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'customer changed');
9978             END IF;
9979 
9980             IF trunc(l_demand_class_priority_tab(i), -2) >
9981                trunc(l_demand_class_priority_tab (i-1), -2) THEN
9982               IF PG_DEBUG in ('Y', 'C') THEN
9983                  msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'customer priority changed');
9984               END IF;
9985 
9986               l_partner_curr_steal_atp := l_partner_next_steal_atp;
9987               l_partner_next_steal_atp := l_null_steal_atp;
9988             END IF;
9989 
9990             l_current_steal_atp := l_null_steal_atp;
9991             l_next_steal_atp := l_null_steal_atp;
9992 
9993 
9994           ELSE
9995             -- same customer
9996             IF PG_DEBUG in ('Y', 'C') THEN
9997                msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'same customer ');
9998             END IF;
9999 
10000             IF (l_demand_class_priority_tab(i) >
10001                 l_demand_class_priority_tab (i-1)) THEN
10002               -- site level priority changed
10003 
10004               IF PG_DEBUG in ('Y', 'C') THEN
10005                  msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'site priority changed');
10006               END IF;
10007               l_current_steal_atp := l_next_steal_atp;
10008               l_next_steal_atp := l_null_steal_atp;
10009 
10010             END IF;
10011           END IF; -- IF (l_partner_tab(i) <> l_partner_tab(i-1))
10012         END IF; -- IF (l_class_tab(i) <> l_class_tab(i-1))
10013 
10014        END IF; -- IF (i > 1)
10015 
10016       END IF; -- IF l_level_id IN (-1, 1)
10017       IF PG_DEBUG in ('Y', 'C') THEN
10018          msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'before we decide we need to do dc consumption');
10019       END IF;
10020       IF (i > 1) THEN
10021        IF (  -- this is the huge condition
10022              ((l_level_id IN (-1, 1)) AND
10023              (l_demand_class_priority_tab(i) <> l_demand_class_priority_tab(1)))
10024            OR
10025              (l_level_id in (2, 3))
10026           ) THEN
10027 
10028         -- we need to do demand class consume only if we are not in the first
10029         -- preferred priority
10030 
10031         -- bug 1413459
10032         -- we need to remember what's the atp picture before the
10033         -- demand class consumption but after it's own backward
10034         -- consumption.  so that we can figure out the stealing
10035         -- quantity correctly.
10036         IF (NVL(p_sup_atp_info_rec.insert_flag, 0) <>0)
10037            AND (l_demand_class_tab(i) = p_sup_atp_info_rec.demand_class) THEN
10038             l_temp_atp := l_current_atp;
10039         END IF;
10040 
10041         -- 1680719
10042         -- since we have hierarchy now, before we do demand class
10043         -- consumption for site level, we need to do the class level and
10044         -- partner level first
10045 
10046         IF l_level_id IN (2,3) THEN
10047 
10048           IF l_class_tab(i) <> l_class_tab(1) THEN
10049 
10050             IF PG_DEBUG in ('Y', 'C') THEN
10051                msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'before consume l_class_curr_steal_atp');
10052                Print_Period_Qty('l_class_curr_steal_atp.atp_period:atp_qty = ',
10053 			l_class_curr_steal_atp);
10054             END IF;
10055 
10056             MSC_AATP_PVT.Atp_demand_class_Consume(l_current_atp, l_class_curr_steal_atp);
10057 
10058             IF PG_DEBUG in ('Y', 'C') THEN
10059                msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'After consume l_class_curr_steal_atp');
10060                Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
10061 			l_current_atp);
10062                Print_Period_Qty('l_class_curr_steal_atp.atp_period:atp_qty = ',
10063 			l_class_curr_steal_atp);
10064             END IF;
10065           END IF; --IF l_class_tab(i) <> l_class_tab(1) THEN
10066 
10067           -- bug 1922942: although partner_id should be unique, we introduced
10068           -- -1 for 'Other' which make the partner_id not unique.
10069           -- for example, Class1/Other and Class2/Other will have same
10070           -- partner_id -1. so the if condition needs to be modified.
10071 
10072           -- IF l_partner_tab(i) <> l_partner_tab(1) THEN
10073 
10074           IF (l_class_tab(i) <> l_class_tab(1)) OR
10075               (l_partner_tab(i) <> l_partner_tab(1)) THEN
10076 
10077             IF PG_DEBUG in ('Y', 'C') THEN
10078                msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'before consume l_partner_curr_steal_atp');
10079             END IF;
10080             MSC_AATP_PVT.Atp_demand_class_Consume(l_current_atp, l_partner_curr_steal_atp);
10081 
10082             IF PG_DEBUG in ('Y', 'C') THEN
10083                msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'After consume l_partner_curr_steal_atp');
10084                Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
10085 			l_current_atp);
10086                Print_Period_Qty('l_partner_curr_steal_atp.atp_period:atp_qty = ',
10087 			l_partner_curr_steal_atp);
10088             END IF;
10089           END IF; -- IF l_partner_tab(i) <> l_partner_tab(1) THEN
10090 
10091         END IF; -- IF l_level_id IN (2,3)
10092 
10093         IF PG_DEBUG in ('Y', 'C') THEN
10094            msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'Before consume current_steal_atp');
10095            Print_Period_Qty('l_current_steal_atp.atp_period:atp_qty = ',
10096 		l_current_steal_atp);
10097         END IF;
10098 
10099         MSC_AATP_PVT.Atp_demand_class_Consume(l_current_atp, l_current_steal_atp);
10100 
10101         IF PG_DEBUG in ('Y', 'C') THEN
10102            msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'After consume l_current_steal_atp');
10103            Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
10104 		l_current_atp);
10105            Print_Period_Qty('l_current_steal_atp.atp_period:atp_qty = ',
10106 		l_current_steal_atp);
10107         END IF;
10108 
10109         -- this part is not in the original design.
10110         -- original design is that we will ignore the inconsistancy
10111         -- in the s/d and period atp for display when stealing happens, as long
10112         -- as we take care the stealing in the logic.
10113         -- but i think it is still better to put it in.
10114         -- and actually if we change Atp_sup_atp_info_rec.demand_class_Consume we can
10115         -- deal with this together.  but for now...
10116 
10117         -- we need to know if we need to store the stealing
10118         -- results in to x_atp_supply_demand and x_atp_period or not.
10119         -- we only do it if this is the demand class we request and
10120         -- insert_flag is on
10121 
10122         IF (NVL(p_sup_atp_info_rec.insert_flag, 0) <>0) AND
10123            (l_demand_class_tab(i) = p_sup_atp_info_rec.demand_class) THEN
10124 
10125           FOR j in 1..l_current_atp.atp_qty.COUNT LOOP
10126 
10127             IF l_current_atp.atp_qty(j) < l_temp_atp.atp_qty(j) THEN
10128               -- this is the stealing quantity in that period
10129 
10130               -- bug 1413459: the stealing quantity should be the current
10131               -- period quantity (after backward consumption, after stealing)
10132               -- minus the period quantity after backward consumption but
10133               -- before the stealing
10134               l_steal_period_quantity := l_current_atp.atp_qty(j) -
10135                                          l_temp_atp.atp_qty(j);
10136 
10137 
10138               MSC_SATP_FUNC.Extend_Atp_Supply_Demand(l_temp_atp_supply_demand,
10139                                        l_return_status);
10140               k := l_temp_atp_supply_demand.Level.Count;
10141               l_temp_atp_supply_demand.level(k) := p_sup_atp_info_rec.level;
10142               l_temp_atp_supply_demand.identifier(k) := p_identifier;
10143               l_temp_atp_supply_demand.scenario_id(k) := p_sup_atp_info_rec.scenario_id;
10144               l_temp_atp_supply_demand.supplier_id(k) := p_sup_atp_info_rec.supplier_id;
10145               l_temp_atp_supply_demand.supplier_site_id(k) := p_sup_atp_info_rec.supplier_site_id;
10146               l_temp_atp_supply_demand.uom(k):= l_uom_code;
10147               l_temp_atp_supply_demand.supply_demand_type(k) := 1;
10148 
10149               -- Bug 1408132 and 1416290, Need to insert type as
10150               -- Demand Class Consumption (45).
10151               l_temp_atp_supply_demand.supply_demand_source_type(k) := 45;
10152 
10153               l_temp_atp_supply_demand.identifier1(k) := p_sup_atp_info_rec.instance_id;
10154               l_temp_atp_supply_demand.supply_demand_date (k) :=
10155                     l_current_atp.atp_period(j);
10156               l_temp_atp_supply_demand.supply_demand_quantity(k) :=
10157                     l_steal_period_quantity;
10158 
10159               x_atp_period.Total_Demand_Quantity(j):=
10160                      x_atp_period.Total_Demand_Quantity(j) +
10161                      l_steal_period_quantity;
10162 
10163               x_atp_period.period_quantity(j):=x_atp_period.period_quantity(j)
10164                      + l_steal_period_quantity;
10165 
10166             END IF;
10167           END LOOP;
10168 
10169           --dsting
10170           move_SD_plsql_into_SD_temp(l_temp_atp_supply_demand);
10171 
10172         END IF;  -- IF (NVL(p_sup_atp_info_rec.insert_flag, 0) <>0) ....
10173        END IF; -- the huge condition
10174       END IF; -- IF (i > 1)
10175 
10176       ---IF l_demand_class_priority_tab(i) < l_priority THEN
10177       ---bug 1665110
10178       IF (l_demand_class <> p_sup_atp_info_rec.demand_class) THEN
10179         -- we need to prepare the l_next_steal_atp for next priorit
10180 
10181         MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_current_atp, l_next_steal_atp);
10182 
10183         -- 1680719
10184         IF l_level_id IN (-1, 1) THEN
10185           IF l_demand_class_priority_tab(i)<
10186              l_demand_class_priority_tab(i+1) THEN
10187           -- this is the last element of current priority, so we also need
10188           -- to add l_steal_atp into l_next_steal_atp if we can not finish
10189           -- the stealing at this priority
10190 
10191             IF PG_DEBUG in ('Y', 'C') THEN
10192                msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'level = '||l_level_id);
10193                msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'before Adding Add_to_Next_Steal_Atp');
10194                Print_Period_Qty('l_next_steal_atp.atp_period:atp_qty = ',
10195 			l_next_steal_atp);
10196             END IF;
10197 
10198             MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_current_steal_atp, l_next_steal_atp);
10199 
10200             IF PG_DEBUG in ('Y', 'C') THEN
10201                msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'after Add_to_Next_Steal_Atp');
10202                Print_Period_Qty('l_next_steal_atp.atp_period:atp_qty = ',
10203 			l_next_steal_atp);
10204             END IF;
10205 
10206           END IF;
10207 
10208         ELSE -- IF l_level_id IN (-1, 1)
10209           -- this is for hierarchy customer level and site level
10210           IF PG_DEBUG in ('Y', 'C') THEN
10211              msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'level = '||l_level_id);
10212              msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'i = '||i);
10213              msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'l_class_tab(i) = '||l_class_tab(i));
10214              msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'l_class_tab(i+1) = '||l_class_tab(i+1));
10215           END IF;
10216           IF (l_class_tab(i) <> l_class_tab(i+1)) THEN
10217 
10218             -- class changed.  If priority of both classes are not the same,
10219             -- then we need to change the curr_steal_atp  at class level.
10220             IF PG_DEBUG in ('Y', 'C') THEN
10221                msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'class changed');
10222             END IF;
10223 
10224             MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_current_steal_atp, l_next_steal_atp);
10225             MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_next_steal_atp, l_partner_next_steal_atp);
10226             MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_partner_curr_steal_atp,
10227                                   l_partner_next_steal_atp);
10228             MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_partner_next_steal_atp,
10229                                   l_class_next_steal_atp);
10230 
10231 
10232             IF trunc(l_demand_class_priority_tab(i), -3)<
10233                trunc(l_demand_class_priority_tab (i+1), -3) THEN
10234 
10235               MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_class_curr_steal_atp,
10236                                     l_class_next_steal_atp);
10237 
10238             END IF;
10239 
10240           ELSE
10241 
10242             IF PG_DEBUG in ('Y', 'C') THEN
10243                msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'class the same');
10244             END IF;
10245             IF (l_partner_tab(i) <> l_partner_tab(i+1)) THEN
10246               -- customer changed
10247               IF PG_DEBUG in ('Y', 'C') THEN
10248                  msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'customer not the same');
10249               END IF;
10250               MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_current_steal_atp, l_next_steal_atp);
10251 
10252               MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_next_steal_atp, l_partner_next_steal_atp);
10253 
10254               IF trunc(l_demand_class_priority_tab(i), -2)<
10255                  trunc(l_demand_class_priority_tab (i+1), -2) THEN
10256                 -- customer priority changed
10257 
10258                 IF PG_DEBUG in ('Y', 'C') THEN
10259                    msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'customer priority changed');
10260                 END IF;
10261                 MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_partner_curr_steal_atp,
10262                                       l_partner_next_steal_atp);
10263 
10264               END IF;
10265 
10266 
10267             ELSE
10268               -- same customer
10269               IF PG_DEBUG in ('Y', 'C') THEN
10270                  msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'customer the same');
10271               END IF;
10272               IF (l_demand_class_priority_tab(i)<>
10273                   l_demand_class_priority_tab (i+1)) THEN
10274                 -- site level priority changed
10275                 MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_current_steal_atp, l_next_steal_atp);
10276 
10277               END IF;
10278             END IF;
10279           END IF;
10280         END IF; -- IF l_level_id IN (-1, 1)
10281       END IF;
10282 
10283       -- 1665110
10284       IF PG_DEBUG in ('Y', 'C') THEN
10285          msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'p_sup_atp_info_rec.demand_class = '||p_sup_atp_info_rec.demand_class);
10286       END IF;
10287       EXIT WHEN (l_demand_class = p_sup_atp_info_rec.demand_class);
10288       IF PG_DEBUG in ('Y', 'C') THEN
10289          msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'after the exit statement, so we did not exit');
10290       END IF;
10291 
10292   END LOOP;
10293   MSC_ATP_PROC.Atp_Accumulate(l_current_atp.atp_qty);
10294 
10295   IF PG_DEBUG in ('Y', 'C') THEN
10296      msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'right after the Atp_Accumulate');
10297      Print_Period_Qty('l_current_atp.atp_period:atp_qty = ',
10298 	l_current_atp);
10299   END IF;
10300 
10301   x_atp_info := l_current_atp;
10302 
10303   --4055719 , calling remove -ves
10304   MSC_AATP_PROC.Atp_Remove_Negatives(x_atp_period.Cumulative_Quantity, l_return_status);
10305   IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
10306      IF PG_DEBUG in ('Y', 'C') THEN
10307         msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ' || 'Error occured in procedure Atp_Remove_Negatives');
10308      END IF;
10309      RAISE FND_API.G_EXC_ERROR;
10310   END IF;
10311 
10312   --4055719 , moved this code to MSC_ATP_REQ.get_supplier_atp_info
10313   -- get the infinite time fence date if it exists
10314   /*l_infinite_time_fence_date := MSC_ATP_FUNC.get_infinite_time_fence_date(null, --4055719
10315              null,null, p_sup_atp_info_rec.plan_id);
10316 
10317     IF l_infinite_time_fence_datee IS NOT NULL THEN
10318       -- add one more entry to indicate infinite time fence date
10319       -- and quantity.
10320       x_atp_info.atp_qty.EXTEND;
10321       x_atp_info.atp_period.EXTEND;
10322       --- bug 1657855, remove support for min alloc
10323       x_atp_info.limit_qty.EXTEND;
10324 
10325       i := x_atp_info.atp_qty.COUNT;
10326       x_atp_info.atp_period(i) := l_last_cap_next_date;
10327       x_atp_info.atp_qty(i) := MSC_ATP_PVT.INFINITE_NUMBER;
10328       ---x_atp_info.limit_qty(i) := MSC_ATP_PVT.INFINITE_NUMBER;
10329 
10330 
10331       IF NVL(p_sup_atp_info_rec.insert_flag, 0) <> 0 THEN
10332         -- add one more entry to indicate infinite time fence date
10333         -- and quantity.
10334 
10335         x_atp_period.Cumulative_Quantity := x_atp_info.atp_qty;
10336 
10337         j := x_atp_period.Level.COUNT;
10338         MSC_SATP_FUNC.Extend_Atp_Period(x_atp_period, l_return_status);
10339         j := j + 1;
10340         IF j > 1 THEN
10341           x_atp_period.Period_End_Date(j-1) := l_infinite_time_fence_date -1;
10342           x_atp_period.Identifier1(j) := x_atp_period.Identifier1(j-1);
10343           x_atp_period.Identifier2(j) := x_atp_period.Identifier2(j-1);
10344         END IF;
10345 
10346         x_atp_period.Level(j) := p_sup_atp_info_rec.level;
10347         x_atp_period.Identifier(j) := p_identifier;
10348         x_atp_period.Scenario_Id(j) := p_sup_atp_info_rec.scenario_id;
10349         x_atp_period.Pegging_Id(j) := NULL;
10350         x_atp_period.End_Pegging_Id(j) := NULL;
10351         x_atp_period.Supplier_Id(j) := p_sup_atp_info_rec.supplier_id;
10352         x_atp_period.Supplier_site_id(j) := p_sup_atp_info_rec.supplier_site_id;
10353         x_atp_period.Organization_id(j) := p_sup_atp_info_rec.organization_id;
10354         x_atp_period.Period_Start_Date(j) := l_infinite_time_fence_date;
10355         x_atp_period.Total_Supply_Quantity(j) := MSC_ATP_PVT.INFINITE_NUMBER;
10356         x_atp_period.Total_Demand_Quantity(j) := 0;
10357         x_atp_period.Period_Quantity(j) := MSC_ATP_PVT.INFINITE_NUMBER;
10358         x_atp_period.Cumulative_Quantity(j) := MSC_ATP_PVT.INFINITE_NUMBER;
10359 
10360      END IF;
10361    END IF; */
10362 -- END IF;
10363 
10364 END Supplier_Alloc_Cum_Atp;
10365 
10366 
10367 PROCEDURE Get_DC_Info(
10368 	p_instance_id	IN 	NUMBER,
10369 	p_inv_item_id	IN 	NUMBER,
10370 	p_org_id	IN 	NUMBER,
10371 	p_dept_id	IN 	NUMBER,
10372 	p_res_id	IN 	NUMBER,
10373 	p_demand_class	IN 	VARCHAR2,
10374 	p_request_date	IN 	DATE,
10375         x_level_id      OUT     NoCopy NUMBER,
10376 	x_priority	OUT  	NoCopy NUMBER,
10377 	x_alloc_percent	OUT 	NoCopy NUMBER,
10378 	x_return_status	OUT 	NoCopy VARCHAR2)
10379 IS
10380         l_rule_name     VARCHAR2(30);
10381 	l_time_phase 	NUMBER;
10382 
10383 BEGIN
10384   IF PG_DEBUG in ('Y', 'C') THEN
10385      msc_sch_wb.atp_debug('******** Get_DC_Info ********');
10386      msc_sch_wb.atp_debug('Get_DC_Info: ' || 'p_instance_id =' || p_instance_id);
10387      msc_sch_wb.atp_debug('Get_DC_Info: ' || 'p_inv_item_id =' || p_inv_item_id);
10388      msc_sch_wb.atp_debug('Get_DC_Info: ' || 'p_org_id =' || p_org_id);
10389      msc_sch_wb.atp_debug('Get_DC_Info: ' || 'p_dept_id =' || p_dept_id);
10390      msc_sch_wb.atp_debug('Get_DC_Info: ' || 'p_res_id =' || p_res_id);
10391      msc_sch_wb.atp_debug('Get_DC_Info: ' || 'p_demand_class =' || p_demand_class);
10392      msc_sch_wb.atp_debug('Get_DC_Info: ' || 'p_request_date =' || p_request_date );
10393   END IF;
10394 
10395   -- initialize API return status to success
10396   x_return_status := FND_API.G_RET_STS_SUCCESS;
10397 
10398   IF p_inv_item_id is not null THEN
10399         -- Get the allocation percent for the item/demand class. If no rule found,
10400 	-- check if a rule on the specified date exists for any demand class
10401 	-- for the specific item, take allocation percentage as NULL.
10402 	-- Though we will treat NULL as 1, but we need to differentiate them
10403 	-- so as to group demands/ supplies by demand classes. - ngoel 8/31/2000.
10404 	BEGIN
10405                 -- Modified by NGOEL on 2/23/2001 as there may be more than 1 rule assigned
10406                 -- to an item/org/instance combinantion at a given level based on time phase.
10407                 --SELECT distinct allocation_rule_name
10408                 --SELECT distinct allocation_rule_name, time_phase_id
10409                 --bug3948494 removed distinct and introduced rownum
10410                 SELECT allocation_rule_name, time_phase_id
10411                 INTO   l_rule_name, l_time_phase
10412                 FROM   msc_item_hierarchy_mv
10413                 WHERE  inventory_item_id = p_inv_item_id
10414                 AND    organization_id = p_org_id
10415       		AND    sr_instance_id = p_instance_id
10416       		AND    p_request_date between effective_date and disable_date
10417       		AND    rownum = 1 ;
10418 
10419 		-- Changes for Bug 2384551 start
10420                 IF (G_HIERARCHY_PROFILE = 1) THEN
10421 
10422                 SELECT ma.priority, ma.allocation_percent/100, ma.level_id
10423                 INTO   x_priority, x_alloc_percent, x_level_id
10424                 FROM   msc_allocations ma
10425                 WHERE  ma.demand_class = p_demand_class
10426                 AND    ma.time_phase_id = l_time_phase
10427                 AND    ma.level_id = -1;
10428 
10429                 ELSE
10430 
10431                 SELECT ma.priority, ma.allocation_percent/100, ma.level_id
10432                 INTO   x_priority, x_alloc_percent, x_level_id
10433                 FROM   msc_allocations ma
10434                 WHERE  ma.demand_class = p_demand_class
10435                 AND    ma.time_phase_id = l_time_phase
10436                 AND    ma.level_id <> -1;
10437 
10438                 END IF;
10439                  -- Changes for Bug 2384551 end
10440 
10441 	EXCEPTION
10442 	   WHEN NO_DATA_FOUND THEN
10443              IF G_HIERARCHY_PROFILE = 1 THEN
10444       		SELECT DECODE(count(allocation_percent), 0, NULL, 0)
10445 	      	INTO   x_alloc_percent
10446       		FROM   msc_item_hierarchy_mv
10447       		WHERE  inventory_item_id = p_inv_item_id
10448       		AND    organization_id = p_org_id
10449       		AND    sr_instance_id = p_instance_id
10450       		AND    p_request_date between effective_date and disable_date
10451                 AND    NVL(level_id, -1) = -1;
10452 
10453 		x_priority := -1;
10454                 x_level_id := -1;
10455              ELSIF G_HIERARCHY_PROFILE = 2 THEN
10456 
10457                 -- this should never happen
10458                 x_alloc_percent := NULL;
10459                 x_priority := -1;
10460                 x_level_id := 1;
10461 
10462              END IF;
10463 	   WHEN OTHERS THEN
10464 		IF PG_DEBUG in ('Y', 'C') THEN
10465 		   msc_sch_wb.atp_debug('Exception in Get_DC_Info');
10466 		END IF;
10467 
10468 		x_priority := -1;
10469 	END;
10470 
10471   ELSE
10472         -- Get the allocation percent for the dept/res/demand class
10473 	BEGIN
10474                 -- Modified by NGOEL on 2/23/2001 as there may be more than 1 rule assigned
10475                 -- to an item/org/instance combinantion at a given level based on time phase.
10476                 --SELECT distinct allocation_rule_name
10477                 --bug3948494 removed distinct and introduced rownum
10478                 --SELECT distinct allocation_rule_name, time_phase_id
10479                 SELECT allocation_rule_name, time_phase_id
10480                 INTO   l_rule_name, l_time_phase
10481                 FROM   msc_resource_hierarchy_mv
10482                 WHERE  department_id = p_dept_id
10483                 AND    resource_id = p_res_id
10484                 AND    organization_id = p_org_id
10485         	AND    sr_instance_id = p_instance_id
10486         	AND    p_request_date between effective_date and disable_date
10487         	AND    rownum = 1 ;
10488 
10489 		-- Changes for Bug 2384551  start
10490                 IF G_HIERARCHY_PROFILE = 1 THEN
10491 
10492                 SELECT ma.priority, ma.allocation_percent/100, ma.level_id
10493                 INTO   x_priority, x_alloc_percent, x_level_id
10494                 FROM   msc_allocations ma
10495                 WHERE  ma.demand_class = p_demand_class
10496                 AND    ma.time_phase_id = l_time_phase
10497                 AND    ma.level_id = -1;
10498 
10499                 ELSE
10500 
10501                 SELECT ma.priority, ma.allocation_percent/100, ma.level_id
10502                 INTO   x_priority, x_alloc_percent, x_level_id
10503                 FROM   msc_allocations ma
10504                 WHERE  ma.demand_class = p_demand_class
10505                 AND    ma.time_phase_id = l_time_phase
10506                 AND    ma.level_id <> -1;
10507 
10508                 END IF;
10509                  -- Changes for Bug 2384551 end
10510 
10511 	EXCEPTION
10512 	   WHEN NO_DATA_FOUND THEN
10513              IF G_HIERARCHY_PROFILE = 1 THEN
10514       		SELECT DECODE(count(allocation_percent), 0, NULL, 0)
10515 	      	INTO   x_alloc_percent
10516         	FROM   msc_resource_hierarchy_mv
10517         	WHERE  department_id = p_dept_id
10518         	AND    resource_id = p_res_id
10519         	AND    organization_id = p_org_id
10520         	AND    sr_instance_id = p_instance_id
10521         	AND    p_request_date between effective_date and disable_date
10522                 AND    NVL(level_id, -1) = -1;
10523 
10524 		x_priority := -1;
10525                 x_level_id := -1;
10526              ELSIF G_HIERARCHY_PROFILE = 2 THEN
10527 
10528                 -- this should never happen
10529                 x_alloc_percent := NULL;
10530                 x_priority := -1;
10531                 x_level_id := 1;
10532 
10533              END IF;
10534 
10535 	   WHEN OTHERS THEN
10536 		IF PG_DEBUG in ('Y', 'C') THEN
10537 		   msc_sch_wb.atp_debug('Exception in Get_DC_Info');
10538 		END IF;
10539 
10540 		x_priority := -1;
10541 	END;
10542   END IF;
10543 EXCEPTION
10544   WHEN OTHERS THEN
10545 	IF PG_DEBUG in ('Y', 'C') THEN
10546 	   msc_sch_wb.atp_debug('Get_DC_Info: ' || 'Error code:' || to_char(sqlcode));
10547 	END IF;
10548 	x_priority := -1;
10549 	x_alloc_percent := 0;
10550 	x_return_status := FND_API.G_RET_STS_ERROR;
10551 END Get_DC_Info;
10552 
10553 PROCEDURE View_Allocation(
10554   p_session_id         IN    NUMBER,
10555   p_inventory_item_id  IN    NUMBER,
10556   p_instance_id        IN    NUMBER,
10557   p_organization_id    IN    NUMBER,
10558   p_department_id      IN    NUMBER,
10559   p_resource_id        IN    NUMBER,
10560   p_demand_class       IN    VARCHAR2,
10561   x_return_status      OUT   NoCopy VARCHAR2
10562 )
10563 IS
10564 	l_plan_id			NUMBER;
10565 	l_assign_set_id			NUMBER;
10566 	l_msg_count			NUMBER;
10567 	l_mode				NUMBER;
10568 	mm				PLS_INTEGER;
10569 	l_request_date			DATE;
10570 	l_return_status			VARCHAR2(100);
10571 	l_msg_data			VARCHAR2(200);
10572 	l_atp_info			MRP_ATP_PVT.ATP_Info;
10573 	l_atp_period			MRP_ATP_PUB.ATP_Period_Typ;
10574 	l_atp_supply_demand		MRP_ATP_PUB.ATP_Supply_Demand_Typ;
10575 	l_atp_rec			MRP_ATP_PUB.atp_rec_typ;
10576 	l_atp_details			MRP_ATP_PUB.ATP_Details_Typ;
10577         l_atp_supply_demand_null        MRP_ATP_PUB.ATP_Supply_Demand_Typ;
10578         l_batchable_flag                NUMBER;
10579         l_max_capacity                  NUMBER;
10580         l_res_conversion_rate	number :=1;
10581         l_res_uom			varchar2(3);
10582         l_res_uom_type			NUMBER;
10583         l_plan_info_rec                 MSC_ATP_PVT.plan_info_rec;  -- added for bug 2392456
10584 
10585         --diag_atp
10586         l_get_mat_in_rec               MSC_ATP_REQ.get_mat_in_rec;
10587 
10588 
10589         --  Agilent Allocated ATP Based on Planning Details changes Begin
10590         l_demand_class                  VARCHAR2(80);
10591         --  Agilent Allocated ATP Based on Planning Details changes End
10592 
10593 BEGIN
10594 	msc_sch_wb.set_session_id(p_session_id);
10595 	IF PG_DEBUG in ('Y', 'C') THEN
10596 	   msc_sch_wb.atp_debug('Begin View_Allocation');
10597 	   msc_sch_wb.atp_debug('View_Allocation: ' || 'p_inventory_item_id = ' ||to_char(p_inventory_item_id));
10598 	   msc_sch_wb.atp_debug('View_Allocation: ' || 'p_instance_id = ' ||to_char(p_instance_id));
10599 	   msc_sch_wb.atp_debug('View_Allocation: ' || 'p_organization_id = ' ||to_char(p_organization_id));
10600 	   msc_sch_wb.atp_debug('View_Allocation: ' || 'p_department_id = ' ||to_char(p_department_id));
10601 	   msc_sch_wb.atp_debug('View_Allocation: ' || 'p_resource_id = ' ||to_char(p_resource_id));
10602            msc_sch_wb.atp_debug('View_Allocation: ' || 'p_demand_class = ' ||p_demand_class);
10603         END IF;
10604 
10605         -- Bug 2396523 : krajan : Added debug messages
10606         IF PG_DEBUG in ('Y', 'C') THEN
10607            msc_sch_wb.atp_debug('View_Allocation: ' || 'P_DEMAND_CLASS ' || p_demand_class);
10608            msc_sch_wb.atp_debug('View_Allocation: ' || 'G_INV_CTP= ' || MSC_ATP_PVT.G_INV_CTP);
10609            msc_sch_wb.atp_debug('View_Allocation: ' || 'G_HIERARCHY_PROFILE = '|| MSC_ATP_PVT.G_HIERARCHY_PROFILE );
10610            msc_sch_wb.atp_debug('View_Allocation: ' || 'G_ALLOCATED_ATP = ' || MSC_ATP_PVT.G_ALLOCATED_ATP );
10611            msc_sch_wb.atp_debug('View_Allocation: ' || 'G_ALLOCATION_METHOD = '|| MSC_ATP_PVT.G_ALLOCATION_METHOD );
10612         END IF;
10613 
10614         -- krajan : 2400676
10615         IF PG_DEBUG in ('Y', 'C') THEN
10616            msc_sch_wb.atp_debug('View_Allocation: ' || 'Resetting Global Error Code');
10617         END IF;
10618         MSC_SCH_WB.G_ATP_ERROR_CODE := 0;
10619 
10620 	-- As part of ship_rec_cal. No need for SQL here. Just call the function straight.
10621         /*
10622 	--Get sysdate to later pass on to the procedure.
10623         -- Changed to get the next working day from sysdate.
10624         SELECT   MSC_CALENDAR.NEXT_WORK_DAY(p_organization_id,
10625 			p_instance_id, 1, TRUNC(sysdate))
10626         INTO    l_request_date
10627         FROM    dual;
10628 	*/
10629 
10630 	l_request_date := MSC_CALENDAR.NEXT_WORK_DAY(p_organization_id, p_instance_id, 1, sysdate);
10631 
10632         IF PG_DEBUG in ('Y', 'C') THEN
10633            msc_sch_wb.atp_debug('View_Allocation: ' || 'Request Date : '||to_char(l_request_date, 'DD-MON-YYYY'));
10634         END IF;
10635         -- krajan : 2400676
10636         IF (l_request_date = NULL) THEN
10637                 IF PG_DEBUG in ('Y', 'C') THEN
10638                    msc_sch_wb.atp_debug('View_Allocation: ' || 'Request date is null');
10639                 END IF;
10640                 MSC_SCH_WB.G_ATP_ERROR_CODE := MSC_ATP_PVT.NO_MATCHING_CAL_DATE;
10641                 RAISE FND_API.G_EXC_ERROR;
10642         END IF;
10643 
10644 	--Check if the request is for Item or Department-Resource.
10645 	IF p_inventory_item_id IS NOT NULL THEN
10646 
10647 		 --Get Plan Id for the item/org/instance.
10648                 /* commented for bug 2392456
10649                 MSC_ATP_PROC.Get_plan_Info(p_instance_id, p_inventory_item_id,
10650                         p_organization_id, p_demand_class, l_plan_id, l_assign_set_id);
10651 
10652                 -- changes for bug 2392456 starts
10653                  MSC_ATP_PROC.Get_plan_Info(p_instance_id, p_inventory_item_id,
10654                         p_organization_id, p_demand_class, l_plan_info_rec);
10655                 */
10656                 -- New procedure for obtaining plan data : Supplier Capacity Lead Time (SCLT) proj.
10657                 MSC_ATP_PROC.get_global_plan_info(p_instance_id, p_inventory_item_id,
10658                                                   p_organization_id, p_demand_class);
10659 
10660                 l_plan_info_rec := MSC_ATP_PVT.G_PLAN_INFO_REC;
10661                 -- End New procedure for obtaining plan data : Supplier Capacity Lead Time proj.
10662 
10663                 l_plan_id       := l_plan_info_rec.plan_id;
10664                 l_assign_set_id := l_plan_info_rec.assignment_set_id;
10665                 -- changes for bug 2392456 ends
10666 
10667 	        IF PG_DEBUG in ('Y', 'C') THEN
10668 	           msc_sch_wb.atp_debug('View_Allocation: ' || 'Plan Id : '||to_char(l_plan_id));
10669 	           msc_sch_wb.atp_debug('View_Allocation: ' || 'Assignment Set Id : '||to_char(l_assign_set_id));
10670 	        END IF;
10671 
10672                 -- krajan : 2400676
10673                 IF (l_plan_id IS NULL) OR (l_plan_id = -1) THEN
10674                         IF PG_DEBUG in ('Y', 'C') THEN
10675                            msc_sch_wb.atp_debug ('View_Allocation: ' || 'Plan_ID is null or -1');
10676                         END IF;
10677                         MSC_SCH_WB.G_ATP_ERROR_CODE := MSC_ATP_PVT.PLAN_NOT_FOUND;
10678                         RAISE FND_API.G_EXC_ERROR;
10679                 END IF;
10680 
10681                 IF (l_plan_id = -100) THEN
10682                         IF PG_DEBUG in ('Y', 'C') THEN
10683                            msc_sch_wb.atp_debug ('View_Allocation: ' || 'Plan_ID is -100 : Summary Running');
10684                         END IF;
10685                         MSC_SCH_WB.G_ATP_ERROR_CODE := MSC_ATP_PVT.SUMM_CONC_PROG_RUNNING;
10686                         RAISE FND_API.G_EXC_ERROR;
10687                 END IF;
10688 
10689                 IF (l_plan_id = -200) THEN
10690                         IF PG_DEBUG in ('Y', 'C') THEN
10691                            msc_sch_wb.atp_debug ('View_Allocation: ' || 'Plan_ID is -200 : Summary Running');
10692                         END IF;
10693                         MSC_SCH_WB.G_ATP_ERROR_CODE := MSC_ATP_PVT.RUN_POST_PLAN_ALLOC;
10694                         RAISE FND_API.G_EXC_ERROR;
10695                 END IF;
10696 
10697 
10698 
10699 		--Call Item_Alloc_Cum_Atp to get the period ATP info
10700 		--for the item. Set insert_flag = 1, for period atp details.
10701 		--Pass p_identifier = -1 to identify the call from this procedure,
10702 		--so as not to do any demand class consumption/ stealing.
10703 
10704                 IF p_demand_class IS NULL THEN
10705 
10706                 -- To get the total ATP, demand class is passed as NULL,
10707                 -- Pass p_scenario_id = -1 to identify the call for
10708                 -- toatl ATP and demand class to be null
10709 
10710               -- Agilent Allocated ATP Based on Planning Details changes Begin
10711 
10712                   IF ((MSC_ATP_PVT.G_HIERARCHY_PROFILE <> 1) OR
10713                      ((MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
10714                       (MSC_ATP_PVT.G_ALLOCATION_METHOD <> 1))) THEN
10715 
10716                        -- Original Code
10717 
10718                        MSC_AATP_PVT.Item_Alloc_Cum_Atp(
10719                                 l_plan_id,
10720                                 0,      --p_level(for top level item)
10721                                 -1,     --p_identifier
10722                                 -1,     --p_scenario_id - For total request
10723                                 p_inventory_item_id,
10724                                 p_organization_id,
10725                                 p_instance_id,
10726                                 p_demand_class,
10727                                 l_request_date,
10728                                 1,      --p_insert_flag,
10729                                 l_atp_info,
10730                                 l_atp_period,
10731                                 l_atp_supply_demand,
10732                                 --diag_atp
10733                                 l_get_mat_in_rec,
10734                                 p_inventory_item_id, -- time_phased_atp passing p_inventory_item_id for compilation
10735                                 NULL); -- time_phased_atp passing null for compilation
10736                   ELSIF ((MSC_ATP_PVT.G_INV_CTP = 4) AND
10737                       (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
10738                       (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
10739                       (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1)) THEN
10740 
10741                      IF PG_DEBUG in ('Y', 'C') THEN
10742                         msc_sch_wb.atp_debug('View_Allocation: before '||
10743                                 'calling Item_Pre_Allocated_Atp for Totals');
10744                      END IF;
10745                      -- Set demand_class to NULL to obtain totals.
10746 
10747                    l_demand_class :=  NULL;
10748                      -- and MSC_AATP_REQ.Item_Pre_Allocated_Atp
10749 
10750                         MSC_AATP_REQ.Item_Pre_Allocated_Atp(
10751                                 l_plan_id,
10752                                 0,      --p_level(for top level item)
10753                                 -1,     --p_identifier
10754                                 -1,      --p_scenario_id - Not Used
10755                                 p_inventory_item_id,
10756                                 p_organization_id,
10757                                 p_instance_id,
10758                                 l_demand_class,
10759                                 l_request_date,
10760                                 1,      --p_insert_flag,
10761                                 l_atp_info,
10762                                 l_atp_period,
10763                                 l_atp_supply_demand,
10764                                 --diag_atp
10765                                 l_get_mat_in_rec,
10766                                 NULL,  -- p_refresh_number - For summary enhancement - Allocation WB will not use summary
10767                                 NULL,  -- time_phased_atp - This procedure is not used
10768                                 NULL); -- time_phased_atp - This procedure is not used
10769                  END IF;
10770                 -- Agilent Allocated ATP Based on Planning Details changes End
10771 
10772                 -- Assign null record of table to supply demand table as we
10773                 -- don't need supply demand details in temp table.
10774                         l_atp_supply_demand := l_atp_supply_demand_null;
10775                 ELSE
10776 
10777               -- Agilent Allocated ATP Based on Planning Details changes Begin
10778 
10779                   IF ((MSC_ATP_PVT.G_INV_CTP = 4) AND
10780                       (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
10781                       (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
10782                       (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1)) THEN
10783 
10784                      IF PG_DEBUG in ('Y', 'C') THEN
10785                         msc_sch_wb.atp_debug('View_Allocation: before calling '||
10786                                 'Get_Hierarchy_Demand_Class for Demand Class');
10787                      END IF;
10788                      -- Code for Get_Hierarchy_Demand_Class
10789 
10790                    l_demand_class := MSC_AATP_FUNC.Get_Hierarchy_Demand_Class
10791                                       (
10792                                          NULL,    --partner_id
10793                                          NULL,    --partner_site_id
10794                                          --p_inventory_item_id,
10795                                          MSC_ATP_FUNC.Get_inv_item_id(
10796                                           p_instance_id,
10797                                           p_inventory_item_id,  -- src_inv_item
10798                                           null,
10799                                           p_organization_id  ), -- Get Inv Item
10800                                          p_organization_id,
10801                                          p_instance_id,
10802                                          l_request_date,
10803                                          1,        --p_level_id
10804                                          p_demand_class
10805                                       );
10806                      -- and MSC_AATP_REQ.Item_Pre_Allocated_Atp
10807 
10808                         MSC_AATP_REQ.Item_Pre_Allocated_Atp(
10809                                 l_plan_id,
10810                                 0,      --p_level(for top level item)
10811                                 -1,     --p_identifier
10812                                 -1,      --p_scenario_id - Not Used
10813                                 p_inventory_item_id,
10814                                 p_organization_id,
10815                                 p_instance_id,
10816                                 l_demand_class,
10817                                 l_request_date,
10818                                 1,      --p_insert_flag,
10819                                 l_atp_info,
10820                                 l_atp_period,
10821                                 l_atp_supply_demand,
10822                                 l_get_mat_in_rec,
10823                                 NULL,  -- p_refresh_number - For summary enhancement - Allocation WB will not use summary
10824                                 p_inventory_item_id,  -- time_phased_atp - Added for compilation
10825                                 NULL); -- time_phased_atp - Added for compilation
10826 
10827                   -- Bug 2396523 : krajan. Commented out and added ELSIF
10828                   --ELSE  -- Original Code
10829                   ELSIF ((MSC_ATP_PVT.G_HIERARCHY_PROFILE <> 1) OR
10830                         ((MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
10831                         (MSC_ATP_PVT.G_ALLOCATION_METHOD <> 1))) THEN
10832 
10833                             MSC_AATP_PVT.Item_Alloc_Cum_Atp(
10834                                 l_plan_id,
10835                                 0,      --p_level(for top level item)
10836                                 -1,     --p_identifier
10837                                 0,      --p_scenario_id - Not Used
10838                                 p_inventory_item_id,
10839                                 p_organization_id,
10840                                 p_instance_id,
10841                                 p_demand_class,
10842                                 l_request_date,
10843                                 1,      --p_insert_flag,
10844                                 l_atp_info,
10845                                 l_atp_period,
10846                                 l_atp_supply_demand,
10847                                 --diag_atp
10848                                 l_get_mat_in_rec,
10849                                 p_inventory_item_id, -- time_phased_atp passing p_inventory_item_id for compilation
10850                                 NULL); -- time_phased_atp passing null for compilation
10851                   END IF;
10852 
10853                 -- Agilent Allocated ATP Based on Planning Details changes End
10854 
10855                 END IF;
10856 
10857 
10858 	ELSIF p_inventory_item_id IS NULL THEN
10859 
10860                 --Get Plan Id for the department/resource/org/instance.
10861                 --In case we have multiple plans,get MRP plan as first
10862                 --choice, for now just choose one with lowest plan id.
10863 
10864                 BEGIN
10865                         SELECT  min(mdr.plan_id) as plan_id
10866                         INTO    l_plan_id
10867                         FROM    msc_department_resources mdr,
10868                                 msc_trading_partners tp,
10869                                 msc_apps_instances ins,
10870                                 msc_plans plans,
10871                                 msc_designators desig
10872                         WHERE   desig.inventory_atp_flag = 1
10873                         AND     plans.compile_designator = desig.designator
10874                         AND     plans.sr_instance_id = desig.sr_instance_id
10875                         AND     plans.organization_id = desig.organization_id
10876                         AND     plans.plan_completion_date is not null
10877                         AND     plans.data_completion_date is not null
10878                         AND     ins.instance_id = plans.sr_instance_id
10879                         AND     ins.enable_flag = 1
10880                         AND     tp.sr_tp_id = plans.organization_id
10881                         AND     tp.sr_instance_id = plans.sr_instance_id
10882                         AND     tp.partner_type = 3
10883                         AND     mdr.plan_id = plans.plan_id
10884                         AND     mdr.organization_id = p_organization_id
10885                         AND     mdr.sr_instance_id = p_instance_id
10886                         AND     mdr.resource_id = p_resource_id
10887                         AND     mdr.department_id = p_department_id
10888 --                      AND     mdr.demand_class = p_demand_class
10889                         group by mdr.organization_id, mdr.sr_instance_id,
10890                               mdr.resource_id, mdr.department_id;--, desig.demand_class;
10891                 EXCEPTION
10892                         WHEN NO_DATA_FOUND THEN
10893                                 IF PG_DEBUG in ('Y', 'C') THEN
10894                                    msc_sch_wb.atp_debug('View_Allocation: ' || 'Plan not found ');
10895                                 END IF;
10896                                 MSC_SCH_WB.G_ATP_ERROR_CODE := MSC_ATP_PVT.PLAN_NOT_FOUND;
10897                                 RAISE FND_API.G_EXC_ERROR;
10898                         WHEN others THEN
10899                                 IF PG_DEBUG in ('Y', 'C') THEN
10900                                    msc_sch_wb.atp_debug('View_Allocation: ' || 'Error getting plan id :'||sqlcode);
10901                                 END IF;
10902                                 MSC_SCH_WB.G_ATP_ERROR_CODE := MSC_ATP_PVT.ATP_PROCESSING_ERROR;
10903                                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
10904                 END;
10905 
10906                 IF PG_DEBUG in ('Y', 'C') THEN
10907                    msc_sch_wb.atp_debug('View_Allocation: ' || 'Plan Id : '||to_char(l_plan_id));
10908                 END IF;
10909 
10910 		--Call Res_Alloc_Cum_Atp to get the period ATP info
10911 		--for the department/resource. Set insert_flag = 1, for period atp details.
10912 		--Pass p_identifier = -1 to identify the call from this procedure,
10913 		--so as not to do any demand class consumption/ stealing.
10914                 --resource bacting: find out if resource is batchable or not and the max capacity
10915                 BEGIN
10916                    SELECT  batchable_flag, max_capacity, unit_of_measure, uom_class_type
10917                    INTO    l_batchable_flag, l_max_capacity, l_res_uom, l_res_uom_type
10918                    FROM    msc_department_resources
10919                    WHERE   department_id = p_department_id
10920                    AND     resource_id = p_resource_id
10921                    AND     organization_id = p_organization_id
10922                    AND     plan_id = l_plan_id
10923                    AND     sr_instance_id = p_instance_id ;
10924                 EXCEPTION
10925                    WHEN OTHERS THEN
10926                       l_batchable_flag := 0;
10927                       l_max_capacity := 0;
10928                 END;
10929                 IF PG_DEBUG in ('Y', 'C') THEN
10930                    msc_sch_wb.atp_debug('View_Allocation: ' || 'Convert res_item UOM');
10931                 END IF;
10932                 IF (l_batchable_flag = 1) THEN
10933                       BEGIN
10934                           SELECT conversion_rate
10935                           INTO   l_res_conversion_rate
10936                           FROM   msc_uom_conversions
10937                           WHERE  inventory_item_id = 0
10938                           AND    sr_instance_id = p_instance_id
10939                           AND    UOM_CODE = l_res_uom;
10940                       EXCEPTION
10941                           WHEN NO_DATA_FOUND THEN
10942                                 l_res_conversion_rate := 1;
10943                       END;
10944 
10945                 END IF;
10946                 IF PG_DEBUG in ('Y', 'C') THEN
10947                    msc_sch_wb.atp_debug('View_Allocation: ' || 'l_res_conversion_rate := ' || l_res_conversion_rate);
10948                 END IF;
10949 
10950               -- Agilent Allocated ATP Based on Planning Details changes Begin
10951 
10952                 IF ((MSC_ATP_PVT.G_INV_CTP = 4) AND
10953                     (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
10954                     (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
10955                     (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1)) THEN
10956 
10957                    l_demand_class := NULL;
10958 
10959                 ELSE   -- original_code
10960 
10961                    l_demand_class := p_demand_class;
10962 
10963                 END IF;
10964 
10965               -- Agilent Allocated ATP Based on Planning Details changes End
10966 
10967                 IF l_demand_class IS NULL THEN
10968 
10969                 -- To get the total ATP, demand class is passed as NULL,
10970                 -- Pass p_scenario_id = -1 to identify the call for
10971                 -- toatl ATP and demand class to be null
10972 
10973                         MSC_AATP_PVT.Res_Alloc_Cum_Atp(
10974                                 l_plan_id,
10975                                 0,      --p_level(for top level item)
10976                                 -1,     --p_identifier
10977                                 -1,     --p_scenario_id - To identify total request
10978                                 p_department_id,
10979                                 p_resource_id,
10980                                 p_organization_id,
10981                                 p_instance_id,
10982                                 l_demand_class,
10983                                 l_request_date,
10984                                 1,      --p_insert_flag
10985                                  ---resource batching
10986                                 l_max_capacity,
10987                                 l_batchable_flag,
10988                                 l_res_conversion_rate,
10989                                 l_res_uom_type,
10990                                 l_atp_info,
10991                                 l_atp_period,
10992                                 l_atp_supply_demand);
10993 
10994                 -- Assign null record of table to supply demand table as we
10995                 -- don't need supply demand details in temp table.
10996 
10997                         l_atp_supply_demand := l_atp_supply_demand_null;
10998                 ELSE
10999                         MSC_AATP_PVT.Res_Alloc_Cum_Atp(
11000                                 l_plan_id,
11001                                 0,      --p_level(for top level item)
11002                                 -1,     --p_identifier
11003                                 0,      --p_scenario_id - Not Used
11004                                 p_department_id,
11005                                 p_resource_id,
11006                                 p_organization_id,
11007                                 p_instance_id,
11008                                 l_demand_class,
11009                                 l_request_date,
11010                                 1,      --p_insert_flag
11011                                  ---resource batching
11012                                 l_max_capacity,
11013                                 l_batchable_flag,
11014                                 l_res_conversion_rate,
11015                                 l_res_uom_type,
11016                                 l_atp_info,
11017                                 l_atp_period,
11018                                 l_atp_supply_demand);
11019 
11020                 END IF;
11021 
11022 	END IF;
11023 
11024   IF PG_DEBUG in ('Y', 'C') THEN
11025      msc_sch_wb.atp_debug('Supplier_Alloc_Cum_Atp: ');
11026      Print_Period_Qty('l_atp_info.atp_period:atp_qty = ',
11027 	l_atp_info);
11028   END IF;
11029 
11030   -- krajan : 2400676
11031   IF (l_atp_period.period_quantity.COUNT = 0) THEN
11032         -- rajjain bug 2951786 05/13/2003
11033         MSC_SCH_WB.G_ATP_ERROR_CODE := MSC_ATP_PVT.NO_SUPPLY_DEMAND;
11034         -- MSC_SCH_WB.G_ATP_ERROR_CODE := MSC_ATP_PVT.ATP_PROCESSING_ERROR;
11035         IF PG_DEBUG in ('Y', 'C') THEN
11036            msc_sch_wb.atp_debug ('View_Allocation: ' || 'l_atp_period is NULL');
11037            msc_sch_wb.atp_debug ('View_Allocation: ' || 'Error Code: ' || MSC_SCH_WB.G_ATP_ERROR_CODE);
11038         END IF;
11039         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
11040   END IF;
11041 
11042   IF PG_DEBUG in ('Y', 'C') THEN
11043      mm := l_atp_period.Period_Quantity.FIRST;
11044 
11045      WHILE mm is not null LOOP
11046         msc_sch_wb.atp_debug('View_Allocation: ' || 'l_atp_period.period_start_date and Period_Quantity = '||
11047            l_atp_period.period_start_date(mm) ||' : '|| l_atp_period.Period_Quantity(mm));
11048         mm := l_atp_period.Period_Quantity.Next(mm);
11049      END LOOP;
11050   END IF;
11051 
11052 	/* rajjain 01/29/2003 begin Bug 2737596
11053 	   This call is not needed now. Now we directly call PUT_SD_DATA
11054 	   and PUT_PERIOD_DATA from this procedure.
11055 
11056 	--Call procedure to insert l_atp_period
11057 	--into mrp_atp_details_temp table.
11058 
11059 	MSC_ATP_UTILS.put_into_temp_table(
11060 		NULL,		--x_dblink
11061 		p_session_id,
11062 		l_atp_rec,
11063 		l_atp_supply_demand,
11064 		l_atp_period,
11065 		l_atp_details,
11066 		l_mode,
11067 		x_return_status,
11068 		l_msg_data,
11069 		l_msg_count);
11070 
11071          IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
11072                 IF PG_DEBUG in ('Y', 'C') THEN
11073                    msc_sch_wb.atp_debug ('View_Allocation: ' || 'Something wrong in call to PUT_INTO_TEMP_TABLE');
11074                 END IF;
11075                 MSC_SCH_WB.G_ATP_ERROR_CODE := MSC_ATP_PVT.ATP_PROCESSING_ERROR;
11076                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
11077          END IF;*/
11078 
11079          -- rajjain 01/29/2003 begin Bug 2737596
11080          IF PG_DEBUG in ('Y', 'C') THEN
11081             msc_sch_wb.atp_debug('View_Allocation: ' || 'l_atp_supply_demand.level.COUNT: ' || l_atp_supply_demand.level.COUNT);
11082          END IF;
11083 
11084          MSC_ATP_UTILS.PUT_SD_DATA(l_atp_supply_demand, NULL, p_session_id);
11085 
11086          IF PG_DEBUG in ('Y', 'C') THEN
11087             msc_sch_wb.atp_debug('View_Allocation: ' || ' Inserted supply demand  records ');
11088          END IF;
11089 
11090          IF PG_DEBUG in ('Y', 'C') THEN
11091             msc_sch_wb.atp_debug('View_Allocation: ' || 'l_atp_period.level.count: ' || l_atp_period.level.count);
11092          END IF;
11093 
11094          MSC_ATP_UTILS.PUT_PERIOD_DATA(l_atp_period, NULL, p_session_id);
11095 
11096          IF PG_DEBUG in ('Y', 'C') THEN
11097             msc_sch_wb.atp_debug('View_Allocation: ' || ' Inserted period records ');
11098          END IF;
11099          -- rajjain 01/29/2003 end Bug 2737596
11100 
11101 -- krajan : 2400614
11102 EXCEPTION
11103 
11104 WHEN FND_API.G_EXC_ERROR THEN
11105         -- ATP ERROR CODE WILL BE SET before this exception is raised
11106         IF PG_DEBUG in ('Y', 'C') THEN
11107            msc_sch_wb.atp_debug ('Error in View_Allocation: Expected Error Raised');
11108         END IF;
11109         x_return_status := FND_API.G_RET_STS_ERROR;
11110         IF (MSC_SCH_WB.G_ATP_ERROR_CODE = 0) THEN
11111                 MSC_SCH_WB.G_ATP_ERROR_CODE := MSC_ATP_PVT.ATP_PROCESSING_ERROR;
11112                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
11113         END IF;
11114 
11115 WHEN  MSC_ATP_PUB.ATP_INVALID_OBJECTS_FOUND THEN
11116         IF PG_DEBUG in ('Y', 'C') THEN
11117            msc_sch_wb.atp_debug ('Error in View_Allocation: Invalid Objects Found');
11118         END IF;
11119         MSC_SCH_WB.G_ATP_ERROR_CODE := MSC_ATP_PVT.ATP_INVALID_OBJECTS;
11120         x_return_status := FND_API.G_RET_STS_ERROR;
11121 
11122 WHEN OTHERS THEN
11123         IF PG_DEBUG in ('Y', 'C') THEN
11124            msc_sch_wb.atp_debug ('Error in View_Allocation');
11125         END IF;
11126         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
11127         IF (MSC_SCH_WB.G_ATP_ERROR_CODE = 0) THEN
11128                 MSC_SCH_WB.G_ATP_ERROR_CODE := MSC_ATP_PVT.ATP_PROCESSING_ERROR;
11129         END IF;
11130 
11131 END View_allocation;
11132 
11133 
11134 PROCEDURE Stealing (
11135                p_atp_record             IN OUT  NoCopy MRP_ATP_PVT.AtpRec,
11136                p_parent_pegging_id      IN      NUMBER,
11137                p_scenario_id            IN      NUMBER,
11138                p_level                  IN      NUMBER,
11139                p_search                 IN      NUMBER,
11140                p_plan_id                IN      NUMBER,
11141                p_net_demand             IN OUT  NoCopy NUMBER,
11142                x_total_mem_stealing_qty OUT     NOCOPY NUMBER, -- For time_phased_atp
11143                x_total_pf_stealing_qty  OUT     NOCOPY NUMBER, -- For time_phased_atp
11144                x_atp_supply_demand      OUT     NOCOPY MRP_ATP_PUB.ATP_Supply_Demand_Typ,
11145                x_atp_period             OUT     NOCOPY MRP_ATP_PUB.ATP_Period_Typ,
11146                x_return_status          OUT     NoCopy VARCHAR2,
11147                p_refresh_number         IN             NUMBER    -- For summary enhancement
11148 )
11149 IS
11150 l_requested_ship_date          date;
11151 l_atp_date_this_level          date;
11152 l_atp_date_quantity_this_level number;
11153 --l_requested_date_quantity      number;
11154 l_atp_period                   MRP_ATP_PUB.ATP_Period_Typ;
11155 l_atp_supply_demand            MRP_ATP_PUB.ATP_Supply_Demand_Typ;
11156 l_pegging_rec                  mrp_atp_details_temp%ROWTYPE;
11157 l_demand_class_tab             MRP_ATP_PUB.char80_arr
11158                                    := MRP_ATP_PUB.char80_arr();
11159 l_demand_class_tab_new          MRP_ATP_PUB.char80_arr
11160                                    := MRP_ATP_PUB.char80_arr();--6359986
11161 l_demand_class_priority_tab    MRP_ATP_PUB.number_arr
11162                                    := MRP_ATP_PUB.number_arr();
11163 l_dmd_class_priority_tab_new MRP_ATP_PUB.number_arr
11164                                    := MRP_ATP_PUB.number_arr();
11165 
11166 l_allocation_percent_tab	MRP_ATP_PUB.number_arr
11167        		                            := MRP_ATP_PUB.number_arr();
11168 l_inv_item_id                  NUMBER;
11169 l_demand_class                 VARCHAR2(30);
11170 l_atp_insert_rec	       MRP_ATP_PVT.AtpRec;
11171 l_inv_item_name                varchar2(250); --bug 2246200
11172 l_org_code                     varchar2(7);
11173 l_pegging_id                   number;
11174 l_atp_pegging_id               number;
11175 l_demand_id                    number;
11176 l_priority                     number;
11177 l_level_id                     number;
11178 l_class                        varchar2(30);
11179 l_partner_id                   number;
11180 
11181 --  Agilent Allocated ATP Based on Planning Details changes
11182 l_stealing_quantity            NUMBER;
11183 --diag_atp
11184 L_GET_MAT_IN_REC               MSC_ATP_REQ.GET_MAT_IN_REC;
11185 l_get_mat_out_rec              MSC_ATP_REQ.get_mat_out_rec;
11186 
11187 l_item_info_rec                MSC_ATP_PVT.item_attribute_rec;
11188 
11189 -- time_phased_atp
11190 l_time_phased_atp               VARCHAR2(1) := 'N';
11191 l_mem_stealing_qty              NUMBER := 0;
11192 l_pf_stealing_qty               NUMBER := 0;
11193 l_atf_date_qty                  NUMBER;
11194 l_pf_item_id                    NUMBER;
11195 l_mat_atp_info_rec              MSC_ATP_REQ.Atp_Info_Rec;
11196 l_process_item_id               NUMBER;
11197 l_return_status                 VARCHAR2(1);
11198 l_item_to_use                   NUMBER;
11199 
11200 BEGIN
11201 
11202   -- Loop through the demand_class and do a single level check for each of them
11203   -- If partial quantity is available, insert that into the details.
11204   -- Keep decrementing the net_demand and exit if it is <= 0
11205   -- If the net_demand is still > 0, pass that back to the calling routing
11206 
11207   IF PG_DEBUG in ('Y', 'C') THEN
11208      msc_sch_wb.atp_debug('********* Begin Stealing ************');
11209   END IF;
11210 
11211   /* time_phased_atp changes begin
11212      initialize variables*/
11213   x_total_mem_stealing_qty        := 0;
11214   x_total_pf_stealing_qty         := 0;
11215 
11216   IF (p_atp_record.inventory_item_id <> p_atp_record.request_item_id) and
11217          p_atp_record.atf_date is not null THEN
11218         l_time_phased_atp := 'Y';
11219         l_process_item_id := p_atp_record.request_item_id;
11220         l_pf_item_id := MSC_ATP_PVT.G_ITEM_INFO_REC.product_family_id;
11221         IF PG_DEBUG in ('Y', 'C') THEN
11222                 msc_sch_wb.atp_debug('Stealing: ' || 'Time Phased ATP = ' || l_time_phased_atp);
11223                 msc_sch_wb.atp_debug('Stealing: ' || 'ATF Date = ' || p_atp_record.atf_date);
11224                 msc_sch_wb.atp_debug('Stealing: ' || 'l_pf_item_id = ' || l_pf_item_id);
11225         END IF;
11226   ELSE
11227         l_process_item_id := p_atp_record.inventory_item_id;
11228   END IF;
11229   -- time_phased_atp changes end
11230 
11231   /* Modularize Item and Org Info */
11232   MSC_ATP_PROC.get_global_item_info(p_atp_record.instance_id,
11233                                       ---bug 3917625: Pass in the real plan id
11234                                        --  -1,
11235                                        p_plan_id,
11236                                        l_process_item_id,
11237                                        p_atp_record.organization_id,
11238                                        l_item_info_rec );
11239   l_inv_item_id := MSC_ATP_PVT.G_ITEM_INFO_REC.dest_inv_item_id;
11240   /*l_inv_item_id := MSC_ATP_FUNC.get_inv_item_id (p_atp_record.instance_id,
11241                                                 p_atp_record.request_item_id,
11242                                                 null,
11243                                                 p_atp_record.organization_id);
11244    Modularize Item and Org Info */
11245 
11246   /* New allocation logic for time_phased_atp changes begin */
11247   IF l_time_phased_atp = 'Y' THEN
11248         IF p_atp_record.requested_ship_date <= p_atp_record.atf_date THEN
11249             IF MSC_ATP_PVT.G_MEM_RULE_WITHIN_ATF = 'Y' THEN
11250                 l_item_to_use := l_inv_item_id;
11251             ELSE
11252                 l_item_to_use := l_pf_item_id;
11253             END IF;
11254         ELSE
11255             IF MSC_ATP_PVT.G_PF_RULE_OUTSIDE_ATF = 'Y' THEN
11256                 l_item_to_use := l_pf_item_id;
11257             ELSE
11258                 l_item_to_use := l_inv_item_id;
11259             END IF;
11260         END IF;
11261   ELSE
11262         l_item_to_use := l_inv_item_id;
11263   END IF;
11264   IF PG_DEBUG in ('Y', 'C') THEN
11265    msc_sch_wb.atp_debug('ATP_Check: ' || 'p_atp_record.requested_ship_date = '||p_atp_record.requested_ship_date);
11266    msc_sch_wb.atp_debug('ATP_Check: ' || 'Item to be used = '||l_item_to_use);
11267   END IF;
11268   /* New allocation logic for time_phased_atp changes end */
11269 
11270   BEGIN
11271 
11272    -- Changes for Bug 2384551 start
11273   IF G_HIERARCHY_PROFILE = 1 THEN
11274 
11275   SELECT mv.priority, mv.level_id, mv.class, mv.partner_id
11276   INTO   l_priority, l_level_id, l_class, l_partner_id
11277   FROM   msc_item_hierarchy_mv mv
11278   WHERE  mv.inventory_item_id = l_item_to_use /* New allocation logic for time_phased_atp changes*/
11279   AND    mv.organization_id = p_atp_record.organization_id
11280   AND    mv.sr_instance_id = p_atp_record.instance_id
11281   AND    p_atp_record.requested_ship_date BETWEEN effective_date
11282                                           AND disable_date
11283   AND    mv.demand_class = p_atp_record.demand_class
11284   AND    mv.level_id = -1;
11285 
11286   ELSE
11287 
11288   SELECT mv.priority, mv.level_id, mv.class, mv.partner_id
11289   INTO   l_priority, l_level_id, l_class, l_partner_id
11290   FROM   msc_item_hierarchy_mv mv
11291   WHERE  mv.inventory_item_id = l_item_to_use /* New allocation logic for time_phased_atp changes*/
11292   AND    mv.organization_id = p_atp_record.organization_id
11293   AND    mv.sr_instance_id = p_atp_record.instance_id
11294   AND    p_atp_record.requested_ship_date BETWEEN effective_date
11295                                           AND disable_date
11296   AND    mv.demand_class = p_atp_record.demand_class
11297   AND    mv.level_id <> -1;
11298 
11299   END IF;
11300    -- Changes for Bug 2384551 end
11301 
11302   EXCEPTION
11303     WHEN NO_DATA_FOUND THEN
11304       IF PG_DEBUG in ('Y', 'C') THEN
11305          msc_sch_wb.atp_debug('Stealing: ' || 'No Data found ');
11306       END IF;
11307       l_priority := NULL;
11308       l_level_id := NULL;
11309       l_partner_id := NULL;
11310       l_class := NULL;
11311   END ;
11312 
11313   IF PG_DEBUG in ('Y', 'C') THEN
11314      msc_sch_wb.atp_debug('Stealing: ' || 'l_priority = '||l_priority);
11315      msc_sch_wb.atp_debug('Stealing: ' || 'l_level_id = '||l_level_id);
11316      msc_sch_wb.atp_debug('Stealing: ' || 'l_partner_id = '||l_partner_id);
11317      msc_sch_wb.atp_debug('Stealing: ' || 'l_class = '||l_class);
11318      msc_sch_wb.atp_debug('Stealing: ' || 'before finding the lower priority dc ');
11319   END IF;
11320 
11321   IF l_level_id = -1 THEN
11322     IF PG_DEBUG in ('Y', 'C') THEN
11323        msc_sch_wb.atp_debug('Stealing: ' || 'l_level_id = '||l_level_id);
11324     END IF;
11325 
11326     SELECT mv.demand_class, mv.priority, mv.allocation_percent
11327     BULK COLLECT INTO l_demand_class_tab_new, l_dmd_class_priority_tab_new, l_allocation_percent_tab	--6359986
11328     FROM   msc_item_hierarchy_mv mv
11329     WHERE  mv.inventory_item_id = l_item_to_use -- time_phased_atp
11330     AND    mv.organization_id = p_atp_record.organization_id
11331     AND    mv.sr_instance_id = p_atp_record.instance_id
11332     AND    p_atp_record.requested_ship_date BETWEEN effective_date
11333                                             AND disable_date
11334     AND    mv.priority  > l_priority
11335     AND    mv.level_id = l_level_id
11336     ORDER BY mv.priority asc , mv.allocation_percent desc ;
11337 
11338   ELSIF l_level_id = 1 THEN
11339     IF PG_DEBUG in ('Y', 'C') THEN
11340        msc_sch_wb.atp_debug('Stealing: ' || 'l_level_id = '||l_level_id);
11341     END IF;
11342 
11343     SELECT mv.demand_class, mv.priority, mv.allocation_percent
11344     BULK COLLECT INTO l_demand_class_tab_new, l_dmd_class_priority_tab_new, l_allocation_percent_tab	--6359986
11345     FROM   msc_item_hierarchy_mv mv
11346     WHERE  mv.inventory_item_id = l_item_to_use -- time_phased_atp
11347     AND    mv.organization_id = p_atp_record.organization_id
11348     AND    mv.sr_instance_id = p_atp_record.instance_id
11349     AND    p_atp_record.requested_ship_date BETWEEN effective_date
11350                                             AND disable_date
11351     AND    mv.priority  > l_priority
11352     AND    mv.level_id = l_level_id
11353     ORDER BY mv.priority  , mv.class  ;
11354 
11355   ELSIF l_level_id = 2 THEN
11356 
11357     IF PG_DEBUG in ('Y', 'C') THEN
11358        msc_sch_wb.atp_debug('Stealing: ' || 'l_level_id = '||l_level_id);
11359     END IF;
11360     SELECT mv1.demand_class, mv1.priority, mv1.allocation_percent
11361     BULK COLLECT INTO l_demand_class_tab_new, l_dmd_class_priority_tab_new,l_allocation_percent_tab	--6359986
11362     FROM   msc_item_hierarchy_mv mv1
11363     WHERE  mv1.inventory_item_id = l_item_to_use -- time_phased_atp
11364     AND    mv1.organization_id = p_atp_record.organization_id
11365     AND    mv1.sr_instance_id = p_atp_record.instance_id
11366     AND    p_atp_record.requested_ship_date BETWEEN mv1.effective_date
11367                                             AND mv1.disable_date
11368     AND    mv1.priority  > l_priority
11369     AND    mv1.level_id = l_level_id
11370     AND    NOT (trunc(mv1.priority, -3) = trunc(l_priority, -3)
11371             AND (mv1.class <> l_class))
11372 --    ORDER BY mv1.priority asc, mv1.allocation_percent desc;
11373       ORDER BY trunc(mv1.priority, -3), mv1.class ,
11374                trunc(mv1.priority, -2), mv1.partner_id;
11375 
11376   ELSIF l_level_id = 3 THEN
11377 
11378     IF PG_DEBUG in ('Y', 'C') THEN
11379        msc_sch_wb.atp_debug('Stealing: ' || 'l_level_id = '||l_level_id);
11380     END IF;
11381     SELECT mv1.demand_class, mv1.priority, mv1.allocation_percent
11382     BULK COLLECT INTO l_demand_class_tab_new, l_dmd_class_priority_tab_new,l_allocation_percent_tab	--6359986
11383     FROM   msc_item_hierarchy_mv mv1
11384     WHERE  mv1.inventory_item_id = l_item_to_use -- time_phased_atp
11385     AND    mv1.organization_id = p_atp_record.organization_id
11386     AND    mv1.sr_instance_id = p_atp_record.instance_id
11387     AND    p_atp_record.requested_ship_date BETWEEN mv1.effective_date
11388                                             AND mv1.disable_date
11389     AND    mv1.priority  > l_priority
11390     AND    mv1.level_id = l_level_id
11391     AND    NOT (trunc(mv1.priority, -3) = trunc(l_priority, -3)
11392             AND (mv1.class <> l_class))
11393     AND    NOT (trunc(mv1.priority, -2) = trunc(l_priority, -2)
11394             AND (mv1.class = l_class)
11395             AND (mv1.partner_id <> l_partner_id))
11396 --    ORDER BY mv1.priority asc, mv1.allocation_percent desc;
11397     ORDER BY trunc(mv1.priority, -3), mv1.class ,
11398              trunc(mv1.priority, -2), mv1.partner_id,
11399              mv1.priority, mv1.partner_site_id;
11400 
11401   END IF;
11402 
11403   IF PG_DEBUG in ('Y', 'C') THEN
11404      msc_sch_wb.atp_debug('Stealing: ' || 'l_demand_class_tab_new.count = '||l_demand_class_tab_new.count);--6359986
11405   END IF;
11406   --diag_atp
11407   l_get_mat_in_rec.rounding_control_flag := MSC_ATP_PVT.G_ITEM_INFO_REC.rounding_control_type;
11408   l_get_mat_in_rec.dest_inv_item_id := l_inv_item_id;
11409 --6359986 start
11410 --bug5974491:Others with allocation 0% will be considered valid or invalid demand class
11411 
11412   FOR i in 1..l_demand_class_tab_new.COUNT LOOP
11413 
11414 
11415   IF (REPLACE(l_demand_class_tab_new(i),FND_GLOBAL.LOCAL_CHR(13),' ') in  ('-1','-1 -1 -1','-1 -1') ) then
11416 
11417 
11418         IF(MSC_ATP_PVT.G_ZERO_ALLOCATION_PERC = 'N') then
11419          IF nvl(l_allocation_percent_tab(i),1) <> 0 THEN   --Bug12367917:In Demand Priority, % will be null
11420             l_demand_class_tab.EXTEND;
11421             l_demand_class_priority_tab.EXTEND;
11422             l_demand_class_tab(i) := l_demand_class_tab_new(i);
11423             l_demand_class_priority_tab(i) := l_dmd_class_priority_tab_new(i);
11424           END IF;
11425         ELSE
11426             l_demand_class_tab.EXTEND;
11427             l_demand_class_priority_tab.EXTEND;
11428             l_demand_class_tab(i) := l_demand_class_tab_new(i);
11429             l_demand_class_priority_tab(i) := l_dmd_class_priority_tab_new(i);
11430         END IF;
11431   ELSE
11432 
11433           l_demand_class_tab.EXTEND;
11434           l_demand_class_priority_tab.EXTEND;
11435           l_demand_class_tab(i) := l_demand_class_tab_new(i);
11436           l_demand_class_priority_tab(i) := l_dmd_class_priority_tab_new(i);
11437   END IF;
11438 
11439   END LOOP; --6359986 end
11440 
11441   FOR i in 1..l_demand_class_tab.COUNT LOOP
11442         l_demand_class := l_demand_class_tab(i);
11443         IF PG_DEBUG in ('Y', 'C') THEN
11444            msc_sch_wb.atp_debug('Stealing: ' || 'Inside the loop of demand class');
11445            msc_sch_wb.atp_debug('Stealing: ' || 'l_demand_class'||l_demand_class);
11446         END IF;
11447 
11448         -- time_phased_atp changes begin
11449         l_mat_atp_info_rec.instance_id                       := p_atp_record.instance_id;
11450         l_mat_atp_info_rec.plan_id                           := p_plan_id;
11451         l_mat_atp_info_rec.level                             := p_level + 1;
11452         l_mat_atp_info_rec.identifier                        := p_atp_record.identifier;
11453         l_mat_atp_info_rec.scenario_id                       := p_scenario_id;
11454         l_mat_atp_info_rec.inventory_item_id                 := p_atp_record.inventory_item_id;
11455         l_mat_atp_info_rec.request_item_id                   := p_atp_record.request_item_id;
11456         l_mat_atp_info_rec.organization_id                   := p_atp_record.organization_id;
11457         l_mat_atp_info_rec.requested_date                    := p_atp_record.requested_ship_date;
11458         l_mat_atp_info_rec.quantity_ordered                  := p_net_demand;
11459         l_mat_atp_info_rec.demand_class                      := l_demand_class;
11460         l_mat_atp_info_rec.insert_flag                       := p_atp_record.insert_flag;
11461         l_mat_atp_info_rec.rounding_control_flag             := l_get_mat_in_rec.rounding_control_flag;
11462         l_mat_atp_info_rec.dest_inv_item_id                  := l_get_mat_in_rec.dest_inv_item_id;
11463         l_mat_atp_info_rec.infinite_time_fence_date          := l_get_mat_in_rec.infinite_time_fence_date;
11464         l_mat_atp_info_rec.plan_name                         := l_get_mat_in_rec.plan_name;
11465         l_mat_atp_info_rec.optimized_plan                    := l_get_mat_in_rec.optimized_plan;
11466         l_mat_atp_info_rec.requested_date_quantity           := null;
11467         l_mat_atp_info_rec.atp_date_this_level               := null;
11468         l_mat_atp_info_rec.atp_date_quantity_this_level      := null;
11469         l_mat_atp_info_rec.substitution_window               := null;
11470         l_mat_atp_info_rec.atf_date                          := p_atp_record.atf_date;   -- For time_phased_atp
11471         l_mat_atp_info_rec.refresh_number                    := p_refresh_number;   -- For summary enhancement
11472         l_mat_atp_info_rec.shipping_cal_code                 := p_atp_record.shipping_cal_code; -- Bug 3371817
11473 
11474         MSC_ATP_REQ.Get_Material_Atp_Info(
11475                 l_mat_atp_info_rec,
11476                 l_atp_period,
11477                 l_atp_supply_demand,
11478                 x_return_status);
11479 
11480         l_atf_date_qty                               := l_mat_atp_info_rec.atf_date_quantity;
11481         l_atp_date_this_level                        := l_mat_atp_info_rec.atp_date_this_level;
11482         l_atp_date_quantity_this_level               := l_mat_atp_info_rec.atp_date_quantity_this_level;
11483         l_get_mat_out_rec.atp_rule_name              := l_mat_atp_info_rec.atp_rule_name;
11484         l_get_mat_out_rec.infinite_time_fence_date   := l_mat_atp_info_rec.infinite_time_fence_date;
11485         p_atp_record.requested_date_quantity         := l_mat_atp_info_rec.requested_date_quantity;
11486 
11487 	IF PG_DEBUG in ('Y', 'C') THEN
11488 	   msc_sch_wb.atp_debug('Back in Stealing');
11489 	END IF;
11490         -- time_phased_atp changes end
11491 
11492         -- 1430561: move the p_net_demand calculation to the end so that
11493         -- we insert the right demand quantity
11494         -- p_net_demand := (p_net_demand - greatest(l_requested_date_quantity, 0)) ;
11495 
11496 	IF PG_DEBUG in ('Y', 'C') THEN
11497 	   msc_sch_wb.atp_debug('Stealing: ' || 'p_net_demand = '||to_char(p_net_demand));
11498 	END IF;
11499         -- if we don't have atp for this demand class , don't bother
11500         -- generate pegging tree, demand record.
11501 
11502 	IF PG_DEBUG in ('Y', 'C') THEN
11503 	   msc_sch_wb.atp_debug('Stealing: ' || 'p_atp_record.requested_date_quantity = '||
11504                      p_atp_record.requested_date_quantity);
11505 	END IF;
11506 
11507         IF p_atp_record.requested_date_quantity > 0 THEN
11508 
11509             -- time_phased_atp changes begin
11510             IF ((MSC_ATP_PVT.G_INV_CTP = 4) AND
11511                 (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
11512                 (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
11513                 (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1) ) THEN
11514 
11515                IF l_time_phased_atp = 'N' THEN
11516                        l_stealing_quantity :=  LEAST(
11517                                                 p_atp_record.requested_date_quantity,
11518                                                 p_net_demand);
11519                                                 --p_atp_record.quantity_ordered);
11520 
11521                        MSC_ATP_DB_UTILS.Add_Stealing_Supply_Details (
11522                                         p_plan_id,
11523                                         -- rajjain 07/18/2003 bug 3010846
11524                                         -- pass component's sales order line id
11525                                         --p_atp_record.identifier,
11526                                         p_atp_record.demand_source_line,
11527                                         --p_atp_record.inventory_item_id,
11528                                         l_inv_item_id,
11529                                         p_atp_record.organization_id,
11530                                         p_atp_record.instance_id,
11531                                         l_stealing_quantity,
11532                                         p_atp_record.demand_class,
11533                                         l_demand_class,
11534                                         p_atp_record.requested_ship_date,
11535                                         l_demand_id,
11536                                         p_refresh_number,
11537                                         p_atp_record.ato_model_line_id,-- For summary enhancement
11538                                         p_atp_record.demand_source_type,  --cmro
11539                                         --bug3684383
11540                                         p_atp_record.order_number);
11541                ELSE
11542                        IF p_atp_record.requested_ship_date <= p_atp_record.atf_date THEN
11543                                 l_mem_stealing_qty :=  LEAST(
11544                                                 p_atp_record.requested_date_quantity,
11545                                                 p_net_demand);
11546                        ELSE
11547                                 IF (p_atp_record.requested_date_quantity - NVL(l_atf_date_qty, 0)) > p_net_demand THEN
11548                                         l_pf_stealing_qty := p_net_demand;
11549                                         l_mem_stealing_qty := 0;
11550                                 ELSE
11551                                         l_pf_stealing_qty := p_atp_record.requested_date_quantity - NVL(l_atf_date_qty, 0);
11552                                         l_mem_stealing_qty := LEAST(NVL(l_atf_date_qty, 0), (p_net_demand - l_pf_stealing_qty));
11553                                 END IF;
11554                        END IF;
11555 
11556                        -- get family item's dest id
11557                        l_pf_item_id := MSC_ATP_FUNC.get_inv_item_id (
11558                                                 p_atp_record.instance_id,
11559                                                 p_atp_record.inventory_item_id,
11560                                                 null,
11561                                                 p_atp_record.organization_id
11562                                              );
11563                        MSC_ATP_PF.Add_PF_Stealing_Supply_Details (
11564                                         p_plan_id,
11565                                         p_atp_record.demand_source_line,
11566                                         l_inv_item_id,
11567                                         l_pf_item_id,
11568                                         p_atp_record.organization_id,
11569                                         p_atp_record.instance_id,
11570                                         l_mem_stealing_qty,
11571                                         l_pf_stealing_qty,
11572                                         p_atp_record.demand_class,
11573                                         l_demand_class,
11574                                         p_atp_record.requested_ship_date,
11575                                         p_atp_record.atf_date,
11576                                         p_refresh_number, -- for summary enhancement
11577                                         l_demand_id,
11578                                         p_atp_record.ato_model_line_id,
11579                                         p_atp_record.demand_source_type,--cmro
11580                                         --bug3684383
11581                                         p_atp_record.order_number,
11582                                         l_return_status);
11583                         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
11584                                 IF PG_DEBUG in ('Y', 'C') THEN
11585                                         msc_sch_wb.atp_debug('Stealing: ' || 'Error occured in procedure Add_PF_Stealing_Supply_Details');
11586                                 END IF;
11587                                 RAISE FND_API.G_EXC_ERROR;
11588                         END IF;
11589                END IF;
11590                -- time_phased_atp changes end
11591 
11592                IF PG_DEBUG in ('Y', 'C') THEN
11593                   msc_sch_wb.atp_debug('Stealing Supply Id : ' || l_demand_id);
11594                END IF;
11595 
11596             ELSIF l_time_phased_atp = 'Y' THEN
11597                 IF p_atp_record.requested_ship_date <= p_atp_record.atf_date THEN
11598                         l_mem_stealing_qty :=  LEAST(
11599                                         p_atp_record.requested_date_quantity,
11600                                         p_net_demand);
11601                 ELSE
11602                         IF (p_atp_record.requested_date_quantity - NVL(l_atf_date_qty, 0)) > p_net_demand THEN
11603                                 l_pf_stealing_qty := p_net_demand;
11604                                 l_mem_stealing_qty := 0;
11605                         ELSE
11606                                 l_pf_stealing_qty := p_atp_record.requested_date_quantity - NVL(l_atf_date_qty, 0);
11607                                 l_mem_stealing_qty := LEAST(NVL(l_atf_date_qty, 0), (p_net_demand - l_pf_stealing_qty));
11608                         END IF;
11609                 END IF;
11610                 IF PG_DEBUG in ('Y', 'C') THEN
11611                   msc_sch_wb.atp_debug('Stealing: l_mem_stealing_qty = ' || l_mem_stealing_qty);
11612                   msc_sch_wb.atp_debug('Stealing: l_pf_stealing_qty = ' || l_pf_stealing_qty);
11613                 END IF;
11614             END IF;
11615 
11616             x_total_mem_stealing_qty    := x_total_mem_stealing_qty + l_mem_stealing_qty;
11617             x_total_pf_stealing_qty     := x_total_pf_stealing_qty + l_pf_stealing_qty;
11618             IF PG_DEBUG in ('Y', 'C') THEN
11619                 msc_sch_wb.atp_debug('Stealing: x_total_mem_stealing_qty = ' || x_total_mem_stealing_qty);
11620                 msc_sch_wb.atp_debug('Stealing: x_total_pf_stealing_qty = ' || x_total_pf_stealing_qty);
11621             END IF;
11622             -- time_phased_atp changes end
11623 
11624             -- populate insert rec to pegging tree for this demand
11625             -- for performance reason, we call these function here and
11626             -- then populate the pegging tree with the values
11627 
11628             /* Modularize Item and Org Info */
11629             l_inv_item_name := MSC_ATP_PVT.G_ITEM_INFO_REC.item_name;
11630             /*l_inv_item_name := MSC_ATP_FUNC.get_inv_item_name(p_atp_record.instance_id,
11631                                       p_atp_record.inventory_item_id,
11632                                       p_atp_record.organization_id);
11633              Modularize Item and Org Info */
11634 
11635             /* Modularize Item and Org Info */
11636             MSC_ATP_PROC.get_global_org_info (p_atp_record.instance_id,
11637                                  p_atp_record.organization_id );
11638             l_org_code := MSC_ATP_PVT.G_ORG_INFO_REC.org_code;
11639             IF PG_DEBUG in ('Y', 'C') THEN
11640                msc_sch_wb.atp_debug('Stealing: ' || 'Modular Use Org_code : '||l_org_code);
11641             END IF;
11642             /*l_org_code := MSC_ATP_FUNC.get_org_code(p_atp_record.instance_id,
11643                                          p_atp_record.organization_id);
11644              Modularize Item and Org Info */
11645 
11646             l_pegging_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
11647             l_pegging_rec.order_line_id:= MSC_ATP_PVT.G_ORDER_LINE_ID;
11648             -- l_pegging_rec.parent_pegging_id:= l_pegging_id;
11649             l_pegging_rec.parent_pegging_id:= p_parent_pegging_id;
11650             l_pegging_rec.atp_level:= p_level + 1;
11651             l_pegging_rec.organization_id:= p_atp_record.organization_id;
11652             l_pegging_rec.organization_code:= l_org_code;
11653             l_pegging_rec.identifier1:= p_atp_record.instance_id;
11654             l_pegging_rec.identifier2 := p_plan_id;
11655 
11656             -- Bug 1419121, Insert Demand id to be used while deleting.
11657 
11658             l_pegging_rec.identifier3 := l_demand_id;
11659             --l_pegging_rec.identifier3 := NULL;
11660 
11661             -- time_phased_atp changes begin
11662             IF l_time_phased_atp = 'Y' and p_atp_record.requested_ship_date <= p_atp_record.atf_date THEN
11663                     l_pegging_rec.inventory_item_id:= p_atp_record.request_item_id;
11664                     l_pegging_rec.inventory_item_name := l_inv_item_name;
11665             ELSE
11666                     l_pegging_rec.inventory_item_id:= p_atp_record.inventory_item_id;
11667                     l_pegging_rec.inventory_item_name := MSC_ATP_FUNC.get_inv_item_name(
11668                                                             p_atp_record.instance_id,
11669                                                             p_atp_record.inventory_item_id,
11670                                                             p_atp_record.organization_id
11671                                                          );
11672             END IF;
11673             l_pegging_rec.aggregate_time_fence_date:= p_atp_record.atf_date;
11674             l_pegging_rec.request_item_id:= p_atp_record.request_item_id;
11675             -- time_phased_atp changes end
11676 
11677             l_pegging_rec.resource_id := NULL;
11678             l_pegging_rec.resource_code := NULL;
11679             l_pegging_rec.department_id := NULL;
11680             l_pegging_rec.department_code := NULL;
11681             l_pegging_rec.supplier_id := NULL;
11682             l_pegging_rec.supplier_name := NULL;
11683             l_pegging_rec.supplier_site_id := NULL;
11684             l_pegging_rec.supplier_site_name := NULL;
11685             l_pegging_rec.scenario_id:= p_scenario_id;
11686             l_pegging_rec.supply_demand_source_type:= MSC_ATP_PVT.ATP;
11687             l_pegging_rec.supply_demand_quantity:=
11688                             p_atp_record.requested_date_quantity;
11689             l_pegging_rec.supply_demand_date:= p_atp_record.requested_ship_date;
11690             l_pegging_rec.supply_demand_type:= 2;
11691             l_pegging_rec.source_type := 0;
11692 
11693             l_pegging_rec.char1 := l_demand_class;
11694 
11695             -- bug 1527660
11696             --l_pegging_rec.allocated_quantity :=
11697             --                l_atp_insert_rec.quantity_ordered;
11698             --bug3830147 Earlier allocated_quantity was getting passed as Null always.
11699             -- Populating it with correct stealing qty so that it can be used for
11700             -- workflow notification.
11701             IF l_time_phased_atp = 'Y' THEN
11702                l_pegging_rec.allocated_quantity := l_mem_stealing_qty + l_pf_stealing_qty;
11703 
11704             ELSE
11705                l_pegging_rec.allocated_quantity := LEAST(
11706                                                     p_atp_record.requested_date_quantity,
11707                                                     p_net_demand);
11708             END IF;
11709 
11710             IF PG_DEBUG in ('Y', 'C') THEN
11711                 msc_sch_wb.atp_debug('Stealing: l_pegging_rec.allocated_quantity  = ' ||l_pegging_rec.allocated_quantity);
11712             END IF;
11713 	    l_pegging_rec.component_identifier :=
11714                          NVL(p_atp_record.component_identifier, MSC_ATP_PVT.G_COMP_LINE_ID);
11715 
11716             -- for demo:1153192
11717             --optional_fw only if profile is set to 'N' populate constraint
11718             IF ((p_search = 1)
11719                    AND ( p_atp_record.quantity_ordered >=
11720                         l_mat_atp_info_rec.requested_date_quantity)) AND MSC_ATP_PVT.G_FORWARD_ATP = 'N' THEN
11721                   l_pegging_rec.constraint_flag := 'Y';
11722             ELSE
11723                   l_pegging_rec.constraint_flag := 'N';
11724 
11725             END IF;
11726 
11727             --diag_atp
11728             l_pegging_rec.plan_name := p_atp_record.plan_name;
11729             l_pegging_rec.required_quantity:= p_net_demand;
11730             l_pegging_rec.required_date := p_atp_record.requested_ship_date;
11731             l_pegging_rec.infinite_time_fence := l_get_mat_out_rec.infinite_time_fence_date;
11732             l_pegging_rec.atp_rule_name := l_get_mat_out_rec.atp_rule_name;
11733             l_pegging_rec.rounding_control := MSC_ATP_PVT.G_ITEM_INFO_REC.rounding_control_type;
11734             l_pegging_rec.atp_flag := MSC_ATP_PVT.G_ITEM_INFO_REC.atp_flag;
11735             l_pegging_rec.atp_component_flag := MSC_ATP_PVT.G_ITEM_INFO_REC.atp_comp_flag;
11736             l_pegging_rec.pegging_type := 3; ---atp supply node
11737             l_pegging_rec.postprocessing_lead_time := MSC_ATP_PVT.G_ITEM_INFO_REC.post_pro_lt;
11738             l_pegging_rec.preprocessing_lead_time := MSC_ATP_PVT.G_ITEM_INFO_REC.pre_pro_lt;
11739             l_pegging_rec.fixed_lead_time := MSC_ATP_PVT.G_ITEM_INFO_REC.fixed_lt;
11740             l_pegging_rec.variable_lead_time := MSC_ATP_PVT.G_ITEM_INFO_REC.variable_lt;
11741             l_pegging_rec.weight_capacity := MSC_ATP_PVT.G_ITEM_INFO_REC.unit_weight;
11742             l_pegging_rec.volume_capacity := MSC_ATP_PVT.G_ITEM_INFO_REC.unit_volume;
11743             l_pegging_rec.weight_uom := MSC_ATP_PVT.G_ITEM_INFO_REC.weight_uom;
11744             l_pegging_rec.volume_uom := MSC_ATP_PVT.G_ITEM_INFO_REC.volume_uom;
11745             l_pegging_rec.allocation_rule := MSC_ATP_PVT.G_ALLOCATION_RULE_NAME;
11746 
11747             l_pegging_rec.summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;     -- for summary enhancement
11748             l_pegging_rec.demand_class := l_demand_class;
11749             -- Bug 3826234 start
11750             IF PG_DEBUG in ('Y', 'C') THEN
11751               msc_sch_wb.atp_debug('ATP_Check: ' || '----------- Calendars passed to Pegging -----------------');
11752               msc_sch_wb.atp_debug('ATP_Check: ' || 'shipping_cal_code = '      ||p_atp_record.shipping_cal_code);
11753               msc_sch_wb.atp_debug('ATP_Check: ' || 'receiving_cal_code = '     ||p_atp_record.receiving_cal_code);
11754               msc_sch_wb.atp_debug('ATP_Check: ' || 'intransit_cal_code = '     ||p_atp_record.intransit_cal_code);
11755               msc_sch_wb.atp_debug('ATP_Check: ' || 'manufacturing_cal_code = ' ||p_atp_record.manufacturing_cal_code);
11756               msc_sch_wb.atp_debug('ATP_Check: ' || 'to_organization_id = ' ||p_atp_record.to_organization_id);
11757             END IF;
11758             IF p_parent_pegging_id = MSC_ATP_PVT.G_DEMAND_PEGGING_ID THEN
11759                l_pegging_rec.shipping_cal_code      :=  p_atp_record.shipping_cal_code;
11760                l_pegging_rec.receiving_cal_code     :=  p_atp_record.receiving_cal_code;
11761                l_pegging_rec.intransit_cal_code     :=  p_atp_record.intransit_cal_code;
11762                l_pegging_rec.manufacturing_cal_code :=  p_atp_record.manufacturing_cal_code;
11763                IF PG_DEBUG in ('Y', 'C') THEN
11764                   msc_sch_wb.atp_debug('ATP_Check: ' || 'Inside IF');
11765                END IF;
11766             ELSIF NVL(p_atp_record.to_organization_id,p_atp_record.organization_id)
11767                                                              <> p_atp_record.organization_id THEN
11768                l_pegging_rec.shipping_cal_code      :=  p_atp_record.shipping_cal_code;
11769                l_pegging_rec.receiving_cal_code     :=  p_atp_record.receiving_cal_code;
11770                l_pegging_rec.intransit_cal_code     :=  p_atp_record.intransit_cal_code;
11771                l_pegging_rec.manufacturing_cal_code :=  NULL;
11772                IF PG_DEBUG in ('Y', 'C') THEN
11773                   msc_sch_wb.atp_debug('ATP_Check: ' || 'Inside ELSIF');
11774                END IF;
11775             ELSE
11776                l_pegging_rec.manufacturing_cal_code :=  p_atp_record.manufacturing_cal_code;
11777                l_pegging_rec.shipping_cal_code      :=  NULL;
11778                l_pegging_rec.receiving_cal_code     :=  NULL;
11779                l_pegging_rec.intransit_cal_code     :=  NULL;
11780                IF PG_DEBUG in ('Y', 'C') THEN
11781                   msc_sch_wb.atp_debug('ATP_Check: ' || 'Inside ELSE');
11782                END IF;
11783             END IF;
11784             -- Bug 3826234 end
11785             MSC_ATP_DB_UTILS.Add_Pegging(l_pegging_rec, l_atp_pegging_id);
11786             --optional_fw G_OPTIONAL_FW will be not null only when profile is Y
11787             IF MSC_ATP_PVT.G_OPTIONAL_FW is not null AND MSC_ATP_PVT.G_FORWARD_ATP = 'Y' THEN
11788                MSC_ATP_PVT.G_FW_STEAL_PEGGING_ID.EXTEND;
11789                MSC_ATP_PVT.G_FW_STEAL_PEGGING_ID(MSC_ATP_PVT.G_FW_STEAL_PEGGING_ID.COUNT) := l_atp_pegging_id;
11790                IF PG_DEBUG in ('Y', 'C') THEN
11791                            msc_sch_wb.atp_debug('ATP_Check: ' || 'MSC_ATP_PVT.G_FW_STEAL_PEGGING_ID := '
11792                                                               || MSC_ATP_PVT.G_FW_STEAL_PEGGING_ID(MSC_ATP_PVT.G_FW_STEAL_PEGGING_ID.COUNT));
11793                END IF;
11794             END IF;
11795             -- Add pegging_id to the l_atp_period and l_atp_supply_demand
11796 
11797             FOR i in 1..l_atp_period.Level.COUNT LOOP
11798                 l_atp_period.Pegging_Id(i) := l_atp_pegging_id;
11799                 l_atp_period.End_Pegging_Id(i) := MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
11800             END LOOP;
11801 
11802             -- dsting: supply/demand details pl/sql table no longer used
11803 /*          FOR i in 1..l_atp_supply_demand.Level.COUNT LOOP
11804                 l_atp_supply_demand.Pegging_Id(i) := l_atp_pegging_id;
11805                 l_atp_supply_demand.End_Pegging_Id(i) := MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
11806             END LOOP;
11807 */
11808 
11809 	    IF p_atp_record.insert_flag <> 0 THEN
11810 		    MSC_ATP_DB_UTILS.move_SD_temp_into_mrp_details(l_atp_pegging_id,
11811 					  MSC_ATP_PVT.G_DEMAND_PEGGING_ID);
11812 	    END IF;
11813 
11814             MSC_ATP_PROC.Details_Output(l_atp_period,
11815                              l_atp_supply_demand,
11816                              x_atp_period,
11817                              x_atp_supply_demand,
11818                              x_return_status);
11819 
11820         END IF;  -- IF p_atp_record.requested_date_quantity > 0
11821 
11822         -- 1430561: we moved the p_net_demand to here
11823         p_net_demand := (p_net_demand - greatest(l_mat_atp_info_rec.requested_date_quantity, 0)) ;
11824 
11825         IF (p_net_demand <= 0) then
11826           EXIT;
11827         END IF;
11828 
11829   END LOOP;
11830   IF PG_DEBUG in ('Y', 'C') THEN
11831      msc_sch_wb.atp_debug('********* END Stealing ************');
11832   END IF;
11833 END Stealing;
11834 END MSC_AATP_PVT;