DBA Data[Home] [Help]

PACKAGE BODY: APPS.MSC_ATP_ALLOC

Source


1 PACKAGE BODY MSC_ATP_ALLOC AS
2 /* $Header: MSCATALB.pls 120.2.12010000.2 2008/08/25 10:46:01 sbnaik ship $  */
3 G_PKG_NAME              CONSTANT VARCHAR2(30) := 'MSC_ATP_ALLOC';
4 G_UNALLOCATED_DC        CONSTANT VARCHAR2(2) := '-2';
5 G_MSG_DEBUG             CONSTANT NUMBER := 0;
6 G_MSG_LOG               CONSTANT NUMBER := 1;
7 
8 G_REFRESH_ALLOCATION    BOOLEAN         := false;
9 PG_DEBUG                varchar2(1)     := NVL(FND_PROFILE.value('MSC_ATP_DEBUG'), 'N');
10 G_ATP_FW_CONSUME_METHOD NUMBER          := NVL(FND_PROFILE.VALUE('MSC_ATP_FORWARD_CONSUME_METHOD'), 1);
11 -- bug 2763784 (ssurendr)
12 -- To store rounding control type
13 --G_ROUNDING_CONTROL_FLAG NUMBER; --rajjain 02/12/2003 bug 2795992
14 
15 -- Begin private procedures declaration
16 PROCEDURE Compute_Allocation_Details(
17         p_session_id                    IN              NUMBER,
18         p_inventory_item_id             IN              NUMBER,
19         p_instance_id                   IN              NUMBER,
20         p_organization_id               IN              NUMBER,
21         p_plan_id                       IN              NUMBER,
22         p_request_date                  IN              DATE,
23         p_infinite_time_fence_date      IN              DATE,
24         x_atp_period                    OUT     NOCOPY  MRP_ATP_PUB.ATP_Period_Typ,
25         p_dest_inv_item_id              OUT     NOCOPY  NUMBER, -- For new allocation logic for time phased ATP
26         p_dest_family_item_id           OUT     NOCOPY  NUMBER, -- For new allocation logic for time phased ATP
27         x_return_status                 OUT     NOCOPY  VARCHAR2);
28 
29 PROCEDURE Insert_Allocation_Details(
30         p_session_id                    IN              NUMBER,
31         p_inventory_item_id             IN              NUMBER,
32         p_organization_id               IN              NUMBER,
33         p_instance_id                   IN              NUMBER,
34         p_infinite_time_fence_date      IN              DATE,
35         p_atp_period                    IN              MRP_ATP_PUB.ATP_Period_Typ,
36         p_plan_name                     IN              VARCHAR2,  -- bug 2771192
37         p_dest_inv_item_id              IN              NUMBER, -- For new allocation logic for time phased ATP
38         p_dest_family_item_id           IN              NUMBER, -- For new allocation logic for time phased ATP
39         x_return_status                 OUT     NOCOPY  VARCHAR2);
40 
41 PROCEDURE Backward_Consume(
42         p_atp_period                    IN OUT  NOCOPY  MRP_ATP_PUB.ATP_Period_Typ,
43         x_dc_list_tab                   OUT     NOCOPY  MRP_ATP_PUB.char80_arr,
44         x_dc_start_index                OUT     NOCOPY  MRP_ATP_PUB.number_arr,
45         x_dc_end_index                  OUT     NOCOPY  MRP_ATP_PUB.number_arr,
46         x_return_status                 OUT     NOCOPY  VARCHAR2);
47 
48 PROCEDURE Forward_Consume(
49         p_atp_period                    IN OUT  NOCOPY  MRP_ATP_PUB.ATP_Period_Typ,
50         p_start_index                   IN              NUMBER,
51         p_end_index                     IN              NUMBER,
52         x_return_status                 OUT     NOCOPY  VARCHAR2);
53 
54 PROCEDURE Backward_Forward_Consume(
55         p_atp_period                    IN OUT  NOCOPY  MRP_ATP_PUB.ATP_Period_Typ,
56         x_dc_list_tab                   OUT     NOCOPY  MRP_ATP_PUB.char80_arr,
57         x_dc_start_index                OUT     NOCOPY  MRP_ATP_PUB.number_arr,
58         x_dc_end_index                  OUT     NOCOPY  MRP_ATP_PUB.number_arr,
59         x_return_status                 OUT     NOCOPY  VARCHAR2);
60 
61 PROCEDURE Compute_Cum(
62         p_atp_period                    IN OUT  NOCOPY  MRP_ATP_PUB.ATP_Period_Typ,
63         p_dc_list_tab                   IN              MRP_ATP_PUB.char80_arr,
64         p_dc_start_index                IN              MRP_ATP_PUB.number_arr,
65         p_dc_end_index                  IN              MRP_ATP_PUB.number_arr,
66         x_return_status                 OUT     NOCOPY  VARCHAR2);
67 
68 PROCEDURE Adjust_Allocation_Details(
69         p_atp_period                    IN OUT  NOCOPY  MRP_ATP_PUB.ATP_Period_Typ,
70         x_dc_list_tab                   OUT     NOCOPY  MRP_ATP_PUB.char80_arr,
71         x_dc_start_index                OUT     NOCOPY  MRP_ATP_PUB.number_arr,
72         x_dc_end_index                  OUT     NOCOPY  MRP_ATP_PUB.number_arr,
73         x_return_status                 OUT     NOCOPY  VARCHAR2);
74 
75 PROCEDURE Demand_Class_Consumption(
76         p_atp_period                    IN OUT  NOCOPY  MRP_ATP_PUB.ATP_Period_Typ,
77         p_start_index                   IN              number,
78         p_end_index                     IN              number,
79         p_steal_atp                     IN OUT  NOCOPY  MRP_ATP_PVT.ATP_Info,
80         x_return_status                 OUT     NOCOPY  VARCHAR2);
81 
82 PROCEDURE Add_to_Next_Steal(
83         p_atp_period                    IN OUT  NOCOPY  MRP_ATP_PUB.ATP_Period_Typ,
84         p_start_index                   IN              number,
85         p_end_index                     IN              number,
86         p_next_steal_atp                IN OUT  NOCOPY  MRP_ATP_PVT.ATP_Info,
87         x_return_status                 OUT     NOCOPY  VARCHAR2);
88 
89 PROCEDURE Add_to_Current_Atp(
90         p_atp_period                    IN OUT  NOCOPY  MRP_ATP_PUB.ATP_Period_Typ,
91         p_start_index                   IN              number,
92         p_end_index                     IN              number,
93         p_steal_atp                     IN              MRP_ATP_PVT.ATP_Info,
94         x_return_status                 OUT     NOCOPY  VARCHAR2);
95 
96 PROCEDURE Compute_Cum_Individual(
97         p_atp_period                    IN OUT  NOCOPY  MRP_ATP_PUB.ATP_Period_Typ,
98         p_start_index                   IN              NUMBER,
99         p_end_index                     IN              NUMBER,
100         x_return_status                 OUT     NOCOPY  VARCHAR2);
101 
102 PROCEDURE Remove_Negatives(
103         p_atp_period                    IN OUT  NOCOPY  MRP_ATP_PUB.ATP_Period_Typ,
104         p_start_index                   IN              NUMBER,
105         p_end_index                     IN              NUMBER,
106         x_return_status                 OUT     NOCOPY  VARCHAR2);
107 
108 PROCEDURE Adjust_Cum(
109         p_atp_period                    IN OUT  NOCOPY  MRP_ATP_PUB.ATP_Period_Typ,
110         p_cur_start_index               IN              NUMBER,
111         p_cur_end_index                 IN              NUMBER,
112         p_unalloc_start_index           IN              NUMBER,
113         p_unalloc_end_index             IN              NUMBER,
114         x_return_status                 OUT     NOCOPY  VARCHAR2);
115 
116 PROCEDURE Set_Error(
117         p_error_code                    IN      INTEGER);
118 
119 -- End private procedures declaration
120 
121 
122 /*--View_Allocation_Details------------------------------------------------
123 |  o This is the entry point for the engine when directly called.
124 |  o Refresh_Allocation_Details calls this when the engine is called in
125 |    concurrent program mode.
126 |  o Calls Compute_Allocation_Details followed by Insert_Allocation_Details.
127 +-------------------------------------------------------------------------*/
128 PROCEDURE View_Allocation_Details(
129         p_session_id            IN              NUMBER,
130         p_inventory_item_id     IN              NUMBER,
131         p_instance_id           IN              NUMBER,
132         p_organization_id       IN              NUMBER,
133         x_return_status         OUT     NOCOPY  VARCHAR2)
134 IS
135         -- local variables
136         l_request_date                  DATE;
137         l_plan_info_rec                 MSC_ATP_PVT.plan_info_rec;
138         l_infinite_time_fence_date      DATE;
139         l_atp_period                    MRP_ATP_PUB.ATP_Period_Typ;
140         l_counter                       PLS_INTEGER;
141         l_return_status                 VARCHAR2(10) := FND_API.G_RET_STS_SUCCESS;
142 
143         -- For new allocation logic for time phased ATP
144         l_dest_inv_item_id              NUMBER;
145         l_dest_family_item_id           NUMBER;
146 
147 BEGIN
148         -- Setting the session_id
149         msc_sch_wb.set_session_id(p_session_id);
150 
151         -- Debug Messages
152         IF PG_DEBUG in ('Y', 'C') THEN
153                 msc_sch_wb.atp_debug('View_Allocation_Details: ' ||  '*********Begin procedure View_Allocation_Details ********');
154                 msc_sch_wb.atp_debug('View_Allocation_Details: ' ||  'p_inventory_item_id = ' ||to_char(p_inventory_item_id));
155                 msc_sch_wb.atp_debug('View_Allocation_Details: ' ||  'p_instance_id = ' ||to_char(p_instance_id));
156                 msc_sch_wb.atp_debug('View_Allocation_Details: ' ||  'p_organization_id = ' ||to_char(p_organization_id));
157         END IF;
158 
159         -- Initializing global error code, API return code
160         MSC_SCH_WB.G_ATP_ERROR_CODE := 0;
161         x_return_status := FND_API.G_RET_STS_SUCCESS;
162 
163 
164         -- Get next working day from sysdate required for compute_allocation_details.
165         SELECT   MSC_CALENDAR.NEXT_WORK_DAY(p_organization_id, p_instance_id, 1, TRUNC(sysdate))
166         INTO    l_request_date
167         FROM    dual;
168 
169         IF (l_request_date = NULL) THEN
170                 IF PG_DEBUG in ('Y', 'C') THEN
171                         msc_sch_wb.atp_debug('View_Allocation_Details: ' || 'There is no matching calander date');
172                 END IF;
173                 x_return_status := FND_API.G_RET_STS_ERROR;
174                 Set_Error(MSC_ATP_PVT.NO_MATCHING_CAL_DATE);
175                 return;
176         END IF;
177         IF PG_DEBUG in ('Y', 'C') THEN
178                 msc_sch_wb.atp_debug('View_Allocation_Details: ' ||  'Request Date : '||to_char(l_request_date, 'DD-MON-YYYY'));
179         END IF;
180 
181         -- Check the profile settings.
182         -- IF (MSC_ATP_PVT.G_INV_CTP <> 4 OR MSC_ATP_PVT.G_ALLOCATED_ATP <> 'Y') THEN
183         -- bug 2813095 (ssurendr) breaking the validations into two
184         IF (MSC_ATP_PVT.G_INV_CTP <> 4) THEN
185                 IF PG_DEBUG in ('Y', 'C') THEN
186                         msc_sch_wb.atp_debug('View_Allocation_Details: ' || 'INV_CTP is not 4');
187                 END IF;
188                 Set_Error(MSC_ATP_PVT.INVALID_INV_CTP_PROFILE_SETUP);
189                 x_return_status := FND_API.G_RET_STS_ERROR;
190                 return;
191         END IF;
192 
193         IF (MSC_ATP_PVT.G_ALLOCATED_ATP <> 'Y') THEN
194                 IF PG_DEBUG in ('Y', 'C') THEN
195                         msc_sch_wb.atp_debug('View_Allocation_Details: ' || 'Enable Allocated ATP is not Yes');
196                 END IF;
197                 Set_Error(MSC_ATP_PVT.INVALID_ALLOC_ATP_OFF);
198                 x_return_status := FND_API.G_RET_STS_ERROR;
199                 return;
200         END IF;
201 
202         -- Trap preliminary plan related errors
203         MSC_ATP_PROC.Get_plan_Info(p_instance_id, p_inventory_item_id, p_organization_id, null, l_plan_info_rec);
204 
205         IF (l_plan_info_rec.plan_id IS NULL) OR (l_plan_info_rec.plan_id = -1) THEN
206                 IF PG_DEBUG in ('Y', 'C') THEN
207                         msc_sch_wb.atp_debug('View_Allocation_Details: ' || 'Plan_ID is null or -1');
208                 END IF;
209                 x_return_status := FND_API.G_RET_STS_ERROR;
210                 Set_Error(MSC_ATP_PVT.PLAN_NOT_FOUND);
211                 return;
212         ELSIF (l_plan_info_rec.plan_id = -100) THEN
213                 IF PG_DEBUG in ('Y', 'C') THEN
214                         msc_sch_wb.atp_debug('View_Allocation_Details: ' || 'Plan_ID is -100 : Summary is Running');
215                 END IF;
216                 x_return_status := FND_API.G_RET_STS_ERROR;
217                 Set_Error(MSC_ATP_PVT.SUMM_CONC_PROG_RUNNING);
218                 return;
219         ELSIF (l_plan_info_rec.plan_id = -200) THEN
220                 IF PG_DEBUG in ('Y', 'C') THEN
221                         msc_sch_wb.atp_debug('View_Allocation_Details: ' || 'Plan_ID is -200 : Post Plan Alloc progranm has not been run');
222                 END IF;
223                 x_return_status := FND_API.G_RET_STS_ERROR;
224                 Set_Error(MSC_ATP_PVT.RUN_POST_PLAN_ALLOC);
225                 return;
226         ELSIF (l_plan_info_rec.plan_id = -300) THEN
227                 IF PG_DEBUG in ('Y', 'C') THEN
228                         msc_sch_wb.atp_debug('View_Allocation_Details: ' || 'Plan_ID is -300 : ATP Downtime');
229                 END IF;
230                 x_return_status := FND_API.G_RET_STS_ERROR;
231                 Set_Error(MSC_ATP_PVT.TRY_ATP_LATER);
232                 return;
233         END IF;
234 
235         -- Get the infinite time fence date
236         l_infinite_time_fence_date := MSC_ATP_FUNC.get_infinite_time_fence_date(p_instance_id,
237                                         p_inventory_item_id, p_organization_id, l_plan_info_rec.plan_id);
238 
239         IF PG_DEBUG in ('Y', 'C') THEN
240                 msc_sch_wb.atp_debug('View_Allocation_Details: ' ||  'Plan Id : '||to_char(l_plan_info_rec.plan_id));
241                 msc_sch_wb.atp_debug('View_Allocation_Details: ' ||  'infinite time fence date : ' || l_infinite_time_fence_date);
242         END IF;
243 
244 
245         /* Call Compute_Allocation_Details to get horizontal period information. */
246 
247         Compute_Allocation_Details(p_session_id, p_inventory_item_id, p_instance_id,
248         p_organization_id, l_plan_info_rec.plan_id, l_request_date, l_infinite_time_fence_date, l_atp_period,
249         l_dest_inv_item_id, l_dest_family_item_id, -- For new allocation logic for time phased ATP
250         l_return_status);
251 
252         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
253                 IF PG_DEBUG in ('Y', 'C') THEN
254                         msc_sch_wb.atp_debug('View_Allocation_Details: ' ||  'Error occured in procedure Compute_Allocation_Details');
255                 END IF;
256                 x_return_status := FND_API.G_RET_STS_ERROR;
257                 Commit;
258                 return;
259         END IF;
260 
261         -- Debug messages
262         IF (l_atp_period.period_quantity.COUNT = 0) THEN
263                 IF PG_DEBUG in ('Y', 'C') THEN
264                         msc_sch_wb.atp_debug('View_Allocation_Details: ' ||  'l_atp_period is NULL');
265                 END IF;
266                 -- rajjain bug 2951786 05/13/2003
267                 Set_Error(MSC_ATP_PVT.NO_SUPPLY_DEMAND);
268                 --Set_Error(MSC_ATP_PVT.ATP_PROCESSING_ERROR);
269                 x_return_status := FND_API.G_RET_STS_ERROR;
270                 Commit;
271                 return;
272         END IF;
273 
274         IF PG_DEBUG in ('Y', 'C') THEN
275                 l_counter := l_atp_period.Period_Quantity.FIRST;
276                 WHILE l_counter is not null LOOP
277                         msc_sch_wb.atp_debug('View_Allocation_Details: ' ||  'demand_class, period_start_date, End date, Period_Qty, Cum_Qty = '||
278                                 l_atp_period.demand_class(l_counter) ||' : '||
279                                 l_atp_period.period_start_date(l_counter) ||' : '||
280                                 l_atp_period.period_end_date(l_counter) ||' : '||
281                                 l_atp_period.Period_Quantity(l_counter) ||' : '||
282                                 l_atp_period.Cumulative_Quantity(l_counter)
283                                 );
284 
285                         l_counter := l_atp_period.Period_Quantity.Next(l_counter);
286                 END LOOP;
287         END IF;
288 
289         /* Call Insert_Allocation_Details to insert horizontal period information into temp table. */
290 
291         Insert_Allocation_Details(p_session_id, p_inventory_item_id, p_organization_id,
292         p_instance_id, l_infinite_time_fence_date, l_atp_period, l_plan_info_rec.plan_name,
293         l_dest_inv_item_id, l_dest_family_item_id, -- For new allocation logic for time phased ATP
294         l_return_status);
295         -- plan_name added for bug 2771192
296 
297         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
298                 IF PG_DEBUG in ('Y', 'C') THEN
299                         msc_sch_wb.atp_debug('View_Allocation_Details: ' ||  'Error occured in procedure Insert_Allocation_Details');
300                 END IF;
301                 x_return_status := FND_API.G_RET_STS_ERROR;
302         END IF;
303 
304         IF PG_DEBUG in ('Y', 'C') THEN
305                 msc_sch_wb.atp_debug('View_Allocation_Details: ' ||  '*********End of procedure View_Allocation_Details ********');
306         END IF;
307 
308         Commit;
309 
310 EXCEPTION
311 WHEN FND_API.G_EXC_ERROR THEN
312         IF PG_DEBUG in ('Y', 'C') THEN
313                 msc_sch_wb.atp_debug('View_Allocation_Details: ' || 'Error in View_Allocation_Details: Expected Error Raised' || to_char(sqlcode) || ':' || SQLERRM);
314         END IF;
315         x_return_status := FND_API.G_RET_STS_ERROR;
316         Set_Error(MSC_ATP_PVT.ATP_PROCESSING_ERROR);
317 
318 WHEN  MSC_ATP_PUB.ATP_INVALID_OBJECTS_FOUND THEN
319         IF PG_DEBUG in ('Y', 'C') THEN
320                 msc_sch_wb.atp_debug('View_Allocation_Details: ' || 'Error in View_Allocation_Details: Invalid Objects Found');
321         END IF;
322         x_return_status := FND_API.G_RET_STS_ERROR;
323         Set_Error(MSC_ATP_PVT.ATP_INVALID_OBJECTS);
324 
325 WHEN OTHERS THEN
326         IF PG_DEBUG in ('Y', 'C') THEN
327                 msc_sch_wb.atp_debug('View_Allocation_Details: ' || 'Error in View_Allocation_Details: Unexpected Error Raised: ' || to_char(sqlcode) || ':' || SQLERRM);
328         END IF;
329         x_return_status := FND_API.G_RET_STS_ERROR;
330         Set_Error(MSC_ATP_PVT.ATP_PROCESSING_ERROR);
331 
332 END View_allocation_Details;
333 
334 
335 /*--Compute_Allocation_Details----------------------------------------------
336 |  o Called by View_Allocation_Details after doing preliminary checks.
337 |  o Does allocation and puts supply/demand data in temp table.
338 |  o Does netting and gets period data in plsql tables.
339 |  o For demand priority, calls Backward_Forward_Consume and Coplute_Cum
340 |  o For rule based case, calls Adjust_Allocation_Details.
341 +-------------------------------------------------------------------------*/
342 PROCEDURE Compute_Allocation_Details(
343         p_session_id                    IN              NUMBER,
344         p_inventory_item_id             IN              NUMBER,
345         p_instance_id                   IN              NUMBER,
346         p_organization_id               IN              NUMBER,
347         p_plan_id                       IN              NUMBER,
348         p_request_date                  IN              DATE,
349         p_infinite_time_fence_date      IN              DATE,
350         x_atp_period                    OUT     NOCOPY  MRP_ATP_PUB.ATP_Period_Typ,
351         p_dest_inv_item_id              OUT     NOCOPY  NUMBER, -- For new allocation logic for time phased ATP
352         p_dest_family_item_id           OUT     NOCOPY  NUMBER, -- For new allocation logic for time phased ATP
353         x_return_status                 OUT     NOCOPY  VARCHAR2)
354 IS
355         -- local variables
356         l_inv_item_id                   PLS_INTEGER;
357         l_inv_item_name                 MSC_SYSTEM_ITEMS.ITEM_NAME%TYPE;
358         l_uom_code                      VARCHAR2(3);
359         l_dc_list_tab                   MRP_ATP_PUB.char80_arr;
360         l_dc_start_index                MRP_ATP_PUB.number_arr;
361         l_dc_end_index                  MRP_ATP_PUB.number_arr;
362         l_index_counter                 PLS_INTEGER;
363         l_start_index                   PLS_INTEGER;
364         l_end_index                     PLS_INTEGER;
365         l_period_counter                PLS_INTEGER;
366         l_count                         PLS_INTEGER;
367         l_default_atp_rule_id           PLS_INTEGER;
368         l_calendar_exception_set_id     PLS_INTEGER;
369         l_default_demand_class          VARCHAR2(80);
370         l_calendar_code                 VARCHAR2(14);
371         l_org_code                      VARCHAR2(7);
372         l_scenario_id                   PLS_INTEGER;
373         l_record_type                   PLS_INTEGER;
374         l_level_id                      PLS_INTEGER;
375         l_return_status                 VARCHAR2(1);
376         -- bug 2763784 (ssurendr)
377         -- Should not error out if no s/d record found
378         l_class_tab                     MRP_ATP_PUB.char80_arr;
379         l_customer_id_tab               MRP_ATP_PUB.number_arr;
380         l_customer_site_id_tab          MRP_ATP_PUB.number_arr;
381         -- rajjain 02/19/2003 Bug 2806076
382         l_all_dc_list_tab               MRP_ATP_PUB.char80_arr;
383         -- bug 2813095 (ssurendr)
384         l_atp_flag                      VARCHAR2(1);
385         -- time_phased_atp
386         l_time_phased_atp               VARCHAR2(1) := 'N';
387         l_pf_dest_id                    NUMBER;
388         l_pf_sr_id                      NUMBER;
389         l_atf_date                      DATE;
390         l_insert_count                  NUMBER;
391         l_item_to_use                   NUMBER;
392         --bug3700564 added family name
393         l_family_name                   VARCHAR2(250);
394         -- Bug 3823042
395         l_sys_next_date                 DATE;
396         l_item_name_to_use              MSC_SYSTEM_ITEMS.ITEM_NAME%TYPE; --Bug 3823042
397         l_null_date                     DATE;   -- Bug 3875786
398         l_null_char                     VARCHAR2(1); --Bug 3875786
399 
400 BEGIN
401         -- Debug Messages
402         IF PG_DEBUG in ('Y', 'C') THEN
403                 msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  '*********Inside procedure Compute_Allocation_Details ********');
404                 msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'G_INV_CTP= ' || MSC_ATP_PVT.G_INV_CTP);
405                 msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'G_HIERARCHY_PROFILE = '|| MSC_ATP_PVT.G_HIERARCHY_PROFILE );
406                 msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'G_ALLOCATED_ATP = ' || MSC_ATP_PVT.G_ALLOCATED_ATP );
407                 msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'G_ALLOCATION_METHOD = '|| MSC_ATP_PVT.G_ALLOCATION_METHOD );
408         END IF;
409 
410         -- Initialization section.
411         x_return_status := FND_API.G_RET_STS_SUCCESS;
412         l_scenario_id := 0;
413         l_record_type   := 2;
414         /* To support new logic for dependent demands allocation in time phased PF rule based AATP scenarios
415            Reset global variable*/
416         MSC_ATP_PVT.G_TIME_PHASED_PF_ENABLED := 'N';
417 
418         -- Get inventory_item_id and uom_code from msc_system_items
419         BEGIN
420                 -- bug 2763784 (ssurendr)
421                 -- Get the rounding control type as well
422                 -- bug 2813095 (ssurendr)
423                 -- Get the atp flag as well
424                 -- rajjain 02/12/2003 bug 2795992
425                 SELECT msi.inventory_item_id, msi.uom_code,
426                        msi.item_name, mtp.organization_code, msi.atp_flag
427                        --, NVL(msi.rounding_control_type, 2)
428                 INTO   l_inv_item_id, l_uom_code,
429                        l_inv_item_name,
430                        l_org_code, l_atp_flag
431                        --, G_ROUNDING_CONTROL_FLAG
432                 FROM   msc_system_items msi,
433                        msc_trading_partners mtp
434                 WHERE  msi.plan_id = p_plan_id
435                 AND    msi.sr_instance_id = p_instance_id
436                 AND    msi.organization_id = p_organization_id
437                 AND    msi.sr_inventory_item_id = p_inventory_item_id
438                 AND    msi.organization_id = mtp.sr_tp_id
439                 AND    msi.sr_instance_id = mtp.sr_instance_id
440                 AND    mtp.partner_type=3;
441         EXCEPTION
442                 WHEN OTHERS THEN
443                         IF PG_DEBUG in ('Y', 'C') THEN
444                                 msc_sch_wb.atp_debug('Compute_Allocation_Details: ' || 'Error selecting inventory item id from msc_system_items: ' || to_char(sqlcode) || ':' || SQLERRM);
445                         END IF;
446                         Set_Error(MSC_ATP_PVT.ATP_PROCESSING_ERROR);
447                         x_return_status := FND_API.G_RET_STS_ERROR;
448                         return;
449         END;
450 
451         -- time_phased_atp
452         MSC_ATP_PF.Get_Family_Item_Info(
453                p_instance_id,
454                p_plan_id,
455                l_inv_item_id,
456                p_organization_id,
457                l_pf_dest_id,
458                l_pf_sr_id,
459                l_atf_date,
460                --bug3700564
461                l_family_name,
462                l_return_status
463         );
464         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
465                 IF PG_DEBUG in ('Y', 'C') THEN
466                         msc_sch_wb.atp_debug('Compute_Allocation_Details: ' || 'Error occured in procedure Get_Family_Item_Info');
467                 END IF;
468                 RAISE FND_API.G_EXC_ERROR;
469         END IF;
470 
471         IF (l_inv_item_id <> l_pf_dest_id) and (l_atf_date is not null) THEN
472                 l_time_phased_atp := 'Y';
473                 G_ATF_DATE        := l_atf_date;
474                 /* To support new logic for dependent demands allocation in time phased PF rule based AATP scenarios
475                    Set global variable too. This is used in Get_Item_Demand_Alloc_Percent function*/
476                 MSC_ATP_PVT.G_TIME_PHASED_PF_ENABLED := 'Y';
477                 IF PG_DEBUG in ('Y', 'C') THEN
478                         msc_sch_wb.atp_debug('Compute_Allocation_Details: ' || 'Time phased atp = ' || l_time_phased_atp);
479                         msc_sch_wb.atp_debug('Compute_Allocation_Details: ' || 'G_TIME_PHASED_PF_ENABLED = ' || MSC_ATP_PVT.G_TIME_PHASED_PF_ENABLED);
480                 END IF;
481         END IF;
482 
483         -- For new allocation logic for time phased ATP
484         p_dest_inv_item_id      := l_inv_item_id;
485         p_dest_family_item_id   := l_pf_dest_id;
486 
487         -- bug 2813095 (ssurendr) error out if not atpable
488         IF l_atp_flag <> 'Y' THEN
489                 IF PG_DEBUG in ('Y', 'C') THEN
490                         msc_sch_wb.atp_debug('Compute_Allocation_Details: ' || 'Item not ATPable');
491                 END IF;
492                 Set_Error(MSC_ATP_PVT.ATP_NOT_APPL);
493                 x_return_status := FND_API.G_RET_STS_ERROR;
494                 return;
495         END IF;
496 
497         IF PG_DEBUG in ('Y', 'C') THEN
498                 msc_sch_wb.atp_debug('Compute_Allocation_Details: ' || 'l_inv_item_id: ' || l_inv_item_id);
499                 msc_sch_wb.atp_debug('Compute_Allocation_Details: ' || 'p_organization_id: ' || p_organization_id);
500                 msc_sch_wb.atp_debug('Compute_Allocation_Details: ' || 'p_instance_id: ' || p_instance_id);
501                 msc_sch_wb.atp_debug('Compute_Allocation_Details: ' || 'p_plan_id: ' || p_plan_id);
502                 --msc_sch_wb.atp_debug('Compute_Allocation_Details: ' || 'G_ROUNDING_CONTROL_FLAG: ' || G_ROUNDING_CONTROL_FLAG);
503         END IF;
504 
505         /* New allocation logic for time_phased_atp changes begin */
506         IF l_time_phased_atp = 'Y' THEN
507                 MSC_ATP_PF.Set_Alloc_Rule_Variables(
508                     l_inv_item_id,
509                     l_pf_dest_id,
510                     p_organization_id,
511                     p_instance_id,
512                     '-1',
513                     l_atf_date,
514                     l_return_status
515                 );
516                 IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
517                      IF PG_DEBUG in ('Y', 'C') THEN
518                           msc_sch_wb.atp_debug('Compute_Allocation_Details: ' || 'Error occured in procedure Set_Alloc_Rule_Variables');
519                      END IF;
520                      RAISE FND_API.G_EXC_ERROR;
521                 END IF;
522 
523                 IF p_request_date <= l_atf_date THEN
524                     IF MSC_ATP_PVT.G_MEM_RULE_WITHIN_ATF = 'Y' THEN
525                         l_item_to_use := l_inv_item_id;
526                     ELSE
527                         l_item_to_use := l_pf_dest_id;
528                     END IF;
529                 ELSE
530                     IF MSC_ATP_PVT.G_PF_RULE_OUTSIDE_ATF = 'Y' THEN
531                         l_item_to_use := l_pf_dest_id;
532                     ELSE
533                         l_item_to_use := l_inv_item_id;
534                     END IF;
535                 END IF;
536         ELSE
537                 l_item_to_use := l_pf_dest_id;
538                 l_item_name_to_use := l_family_name; -- Bug 3823042
539         END IF;
540         IF PG_DEBUG in ('Y', 'C') THEN
541            msc_sch_wb.atp_debug('Compute_Allocation_Details: ' || 'p_request_date = '||p_request_date);
542            msc_sch_wb.atp_debug('Compute_Allocation_Details: ' || 'Item to be used = '||l_item_to_use);
543         END IF;
544         /* New allocation logic for time_phased_atp changes end */
545 
546         IF (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1 AND MSC_ATP_PVT.G_ALLOCATION_METHOD = 1) THEN
547                 -- Demand Priority
548                 IF PG_DEBUG in ('Y', 'C') THEN
549                         msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'We are in Demand Priority Allocation');
550                 END IF;
551 
552                 -- initialize l_level_id. level_id remains -1 for demand priority
553                 l_level_id      := -1;
554 
555                 /* Find all the demand classes on the request date = next working day from SYSDATE
556                    from materialized view and store them in msc_alloc_temp table.
557                    If no demand class found on request date, dont proceed any further, flag an error. */
558                 IF PG_DEBUG in ('Y', 'C') THEN
559                         msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'Checking the demand classes on p_request_date.');
560                 END IF;
561 
562                 INSERT INTO MSC_ALLOC_TEMP(DEMAND_CLASS)
563                 SELECT mv.demand_class
564                 FROM   msc_item_hierarchy_mv mv
565                 WHERE  mv.inventory_item_id = l_item_to_use
566                 AND    mv.organization_id = p_organization_id
567                 AND    mv.sr_instance_id = p_instance_id
568                 AND    p_request_date BETWEEN effective_date AND disable_date
569                 AND    mv.level_id = l_level_id;
570 
571                 IF (SQL%ROWCOUNT = 0) THEN
572                         IF PG_DEBUG in ('Y', 'C') THEN
573                                 msc_sch_wb.atp_debug('No Demand Class found');
574                         END IF;
575                         Set_Error(MSC_ATP_PVT.ATP_PROCESSING_ERROR);
576                         x_return_status := FND_API.G_RET_STS_ERROR;
577                         return;
578                 END IF;
579                 IF PG_DEBUG in ('Y', 'C') THEN
580                         msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'Demand classes found and stored in msc_alloc_temp table');
581                 END IF;
582 
583                 /*
584                 1. Copy the supply demand records from msc_alloc_supplies and msc_alloc_demands
585                    into mrp_atp_details_temp.
586                 2. We copy from_demand_class for stealing records from MSC_ALLOC_SUPPLIES.
587                 3. Allocated Qty <= Total Supply Demand Quantity.
588                 4. MSC_ALLOC_TEMP holds the demand classes on p_request_date. A join between MSC_ALLOC_TEMP
589                    and msc_alloc_supplies/msc_alloc_demands ensures we chose only request date demand classes.
590                 5. Transform order types 46,47 (Supply/Demand due to Stealing) to 48 (Supply Adjustment).
591                    This is required only for MSC_ALLOC_SUPPLIES.
592                 */
593 
594                 IF PG_DEBUG in ('Y', 'C') THEN
595                         msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'Before copying supply/demand records from alloc into temp tables.');
596                 END IF;
597 
598                 -- time_phased_atp changes begin
599                 IF l_time_phased_atp = 'Y' THEN
600                         MSC_ATP_PF.Insert_SD_Into_Details_Temp(
601                                 MSC_ATP_PF.Demand_Priority,
602                                 l_inv_item_id,
603                                 l_pf_dest_id,
604                                 p_inventory_item_id,
605                                 l_pf_sr_id,
606                                 p_organization_id,
607                                 --bug3671294 now we donot need this as we will join with msc_system_items
608                                 --l_inv_item_name,
609                                 l_org_code,
610                                 p_instance_id,
611                                 p_plan_id,
612                                 p_infinite_time_fence_date,
613                                 l_level_id,
614                                 p_session_id,
615                                 l_record_type,
616                                 l_scenario_id,
617                                 l_uom_code,
618                                 l_insert_count,
619                                 l_return_status
620                         );
621                         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
622                                 IF PG_DEBUG in ('Y', 'C') THEN
623                                         msc_sch_wb.atp_debug('Compute_Allocation_Details: ' || 'Error occured in procedure Insert_SD_Into_Details_Temp');
624                                 END IF;
625                                 RAISE FND_API.G_EXC_ERROR;
626                         END IF;
627                 -- time_phased_atp changes end
628                 ELSE
629                         INSERT INTO MRP_ATP_DETAILS_TEMP
630                         (session_id, atp_level, inventory_item_id, organization_id, identifier1, identifier3,
631                         supply_demand_type, supply_demand_date, supply_demand_quantity, supply_demand_source_type,
632                         allocated_quantity, record_type, scenario_id, disposition_name, demand_class, char1,
633                         uom_code, plan_id, inventory_item_name, organization_code,
634                         ORIG_CUSTOMER_SITE_NAME,ORIG_CUSTOMER_NAME,ORIG_DEMAND_CLASS,ORIG_REQUEST_DATE ) --bug3263368
635                         SELECT col1, col2, col3, col4, col5, col6, col7, col8, col9, col10,
636                         col11, col12, col13, col14, col15, col16, col17, col18, col19, col20,
637                         col21, col22, col23, col24
638                         FROM
639                         (SELECT p_session_id                    col1, -- session_id
640                                 l_level_id                      col2, -- level_id
641                                 p_inventory_item_id             col3, -- inventory_item_id
642                                 p_organization_id               col4, -- organization_id
643                                 p_instance_id                   col5, -- Identifier1
644                                 AD.PARENT_DEMAND_ID             col6, -- Identifier3
645                                 1                               col7, -- supply_demand_type
646                                 TRUNC(AD.DEMAND_DATE)           col8, -- supply_demand_date
647                                 -1 * NVL(AD.DEMAND_QUANTITY,
648                                 AD.ALLOCATED_QUANTITY)          col9, -- supply_demand_quantity
649                                 decode(AD.ORIGINATION_TYPE,-100,30,AD.ORIGINATION_TYPE)     col10, -- supply_demand_source_type
650                                 -1 * AD.ALLOCATED_QUANTITY      col11, -- allocated_quantity
651                                 l_record_type                   col12, -- record_type
652                                 l_scenario_id                   col13, -- scenario_id
653                                 AD.ORDER_NUMBER                 col14, -- disposition_name
654                                 AD.DEMAND_CLASS                 col15, -- demand_class
655                                 l_null_char                     col16, -- from_demand_class --Bug 3875786
656                                 l_uom_code                      col17, -- UOM Code
657                                 p_plan_id                       col18, -- Plan id
658                                 l_item_name_to_use              col19, -- Item name --Bug 3823042
659                                 --l_inv_item_name                 col19, -- Item name
660                                 l_org_code                      col20,   -- Organization code
661                                 MTPS.LOCATION                   col21, --bug3263368
662                                 MTP.PARTNER_NAME                col22, --bug3263368
663                                 AD.DEMAND_CLASS                 col23, --bug3263368
664                                 AD.REQUEST_DATE                 col24  --bug3263368
665                         FROM
666                                 MSC_ALLOC_DEMANDS AD,
667                                 MSC_ALLOC_TEMP TEMP,
668                                 MSC_TRADING_PARTNERS    MTP,--bug3263368
669                                 MSC_TRADING_PARTNER_SITES    MTPS --bug3263368
670                         WHERE
671                                 AD.PLAN_ID = p_plan_id
672                                 AND      AD.SR_INSTANCE_ID = p_instance_id
673                                 AND      AD.INVENTORY_ITEM_ID = l_item_to_use -- Bug 3823042
674                                 AND      AD.ORGANIZATION_ID = p_organization_id
675                                 AND      AD.ALLOCATED_QUANTITY <> 0
676                                 AND      AD.DEMAND_CLASS = TEMP.DEMAND_CLASS
677                                 AND      TRUNC(AD.DEMAND_DATE) < TRUNC(p_infinite_time_fence_date)  -- Bug 3823042
678                                 AND      AD.ORIGINATION_TYPE <> 52  -- Ignore copy SO and copy stealing records for allocation WB - summary enhancement
679                                 AND      AD.SHIP_TO_SITE_ID  = MTPS.PARTNER_SITE_ID(+) --bug3263368
680                                 AND      AD.CUSTOMER_ID = MTP.PARTNER_ID(+)--bug3263368
681                         UNION ALL
682                         SELECT  p_session_id                    col1,
683                                 l_level_id                      col2,
684                                 p_inventory_item_id             col3 ,
685                                 p_organization_id               col4,
686                                 p_instance_id                   col5,
687                                 SA.PARENT_TRANSACTION_ID        col6,
688                                 2                               col7, -- supply
689                                 TRUNC(SA.SUPPLY_DATE)           col8,
690                                 NVL(SA.SUPPLY_QUANTITY,
691                                 SA.ALLOCATED_QUANTITY)          col9,
692                                 DECODE(SA.ORDER_TYPE,
693                                         46, 48,                 -- Change Supply due to Stealing to Supply Adjustment
694                                         47, 48,                 -- Change Demand due to Stealing to Supply Adjustment
695                                         SA.ORDER_TYPE)          col10,
696                                 SA.ALLOCATED_QUANTITY           col11,
697                                 l_record_type                   col12, -- record_type
698                                 l_scenario_id                   col13, -- scenario_id
699                              -- Bug 2771075. For Planned Orders, we will populate transaction_id
700                              -- in the disposition_name column to be consistent with Planning.
701                                 DECODE(SA.ORDER_TYPE,
702                                         5, to_char(SA.PARENT_TRANSACTION_ID),
703                                         SA.ORDER_NUMBER)        col14,
704                                 SA.DEMAND_CLASS                 col15,
705                                 SA.FROM_DEMAND_CLASS            col16,
706                                 l_uom_code                      col17,
707                                 p_plan_id                       col18,
708                                 l_item_name_to_use              col19, -- Item name --Bug 3823042
709                                 --l_inv_item_name                 col19, -- Item name
710                                 l_org_code                      col20, -- Organization code
711                                 MTPS.LOCATION                   col21, --bug3684383
712                                 MTP.PARTNER_NAME                col22, --bug3684383
713                                 SA.DEMAND_CLASS                 col23, --bug3684383
714                                 l_null_date                     col24  --bug3263368 ORIG_REQUEST_DATE -- Bug 3875786 - null removed
715                         FROM
716                                 MSC_ALLOC_SUPPLIES SA,
717                                 MSC_ALLOC_TEMP TEMP,
718                                 MSC_TRADING_PARTNERS    MTP,--bug3684383
719                                 MSC_TRADING_PARTNER_SITES    MTPS --bug3684383
720                         WHERE
721                                 SA.PLAN_ID = p_plan_id
722                                 AND      SA.SR_INSTANCE_ID = p_instance_id
723                                 AND      SA.INVENTORY_ITEM_ID = l_item_to_use -- Bug 3823042
724                                 AND      SA.ORGANIZATION_ID = p_organization_id
725                                 AND      SA.ALLOCATED_QUANTITY <> 0
726                                 AND      SA.DEMAND_CLASS = TEMP.DEMAND_CLASS
727                                 AND      TRUNC(SA.SUPPLY_DATE) < TRUNC(p_infinite_time_fence_date) -- Bug 3823042
728                                 AND      SA.SHIP_TO_SITE_ID  = MTPS.PARTNER_SITE_ID(+) --bug3684383
729                                 AND      SA.CUSTOMER_ID = MTP.PARTNER_ID(+)--bug3684383
730                         );
731                         l_insert_count := SQL%ROWCOUNT;
732                 END IF;
733 
734                 IF (l_insert_count = 0) THEN
735 		        IF PG_DEBUG in ('Y', 'C') THEN
736 		                msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'No s/d records could be inserted from msc_alloc tables into temp table');
737 		        END IF;
738 
739                         -- bug 2763784 (ssurendr)
740                         -- Should not error out if no s/d record found
741                         --Set_Error(MSC_ATP_PVT.ATP_PROCESSING_ERROR);
742                         --x_return_status := FND_API.G_RET_STS_ERROR;
743                         --return;
744                 ELSE
745                         IF PG_DEBUG in ('Y', 'C') THEN
746                                 msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'After copying supply/demand records from alloc into temp tables.');
747                         END IF;
748 
749                         /* Bulk Collect Allocated Supply, Stolen Supply, Total Supply,
750                         Allocated Demand, Stolen Demand, Total Demand and Net into PL/SQL Period table.
751                         Cum is calculated after b/w, f/w consumption and accumulation. */
752                         IF PG_DEBUG in ('Y', 'C') THEN
753                                 msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'Before selecting supply/demand records from temp table into PL/SQL period table.');
754                         END IF;
755 
756                         -- time_phased_atp changes begin
757                         IF l_time_phased_atp = 'Y' THEN
758                                 MSC_ATP_PF.Get_Period_From_Details_Temp(
759                                         MSC_ATP_PF.Demand_Priority,
760                                         p_inventory_item_id,
761                                         p_organization_id,
762                                         p_instance_id,
763                                         l_scenario_id,
764                                         l_level_id,
765                                         l_record_type,
766                                         p_session_id,
767                                         x_atp_period,
768                                         l_return_status
769                                 );
770                                 IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
771                                         IF PG_DEBUG in ('Y', 'C') THEN
772                                                 msc_sch_wb.atp_debug('Compute_Allocation_Details: ' || 'Error occured in procedure Get_Period_From_Details_Temp');
773                                         END IF;
774                                         RAISE FND_API.G_EXC_ERROR;
775                                 END IF;
776                         -- time_phased_atp changes end
777                         ELSE
778                                 SELECT
779                                         final.col1,
780                                         final.col2,
781                                         final.col3,
782                                         final.col4,
783                                         final.col5,
784                                         final.col6,
785                                         final.col7,
786                                         null,
787                                         p_inventory_item_id,
788                                         p_organization_id,
789                                         p_instance_id,
790                                         l_scenario_id,
791                                         l_level_id,
792                                         null,           -- Initialize period end date with null
793                                         0,              -- Initialize backward_forward_quantity with 0
794                                         0               -- Initialize cumulative quantity with 0
795                                 BULK COLLECT INTO
796                                         x_atp_period.Demand_Class,
797                                         x_atp_period.Period_Start_Date,
798                                         x_atp_period.Allocated_Supply_Quantity,
799                                         x_atp_period.Supply_Adjustment_Quantity,
800                                         x_atp_period.Total_Supply_Quantity,
801                                         x_atp_period.Total_Demand_Quantity,
802                                         x_atp_period.Period_Quantity,
803                                         x_atp_period.Total_Bucketed_Demand_Quantity,
804                                         x_atp_period.Inventory_Item_Id,
805                                         x_atp_period.Organization_Id,
806                                         x_atp_period.Identifier1,
807                                         x_atp_period.Scenario_Id,
808                                         x_atp_period.Level,
809                                         x_atp_period.Period_End_Date,
810                                         x_atp_period.Backward_Forward_Quantity,
811                                         x_atp_period.Cumulative_Quantity
812                                 FROM
813                                 (SELECT DEMAND_CLASS                                                    col1,
814                                         SUPPLY_DEMAND_DATE                                              col2,
815                                         SUM(DECODE(SUPPLY_DEMAND_TYPE, 2,
816                                                 DECODE(SUPPLY_DEMAND_SOURCE_TYPE,
817                                                                 48, 0,
818                                                                 ALLOCATED_QUANTITY),
819                                                 0))                                                     col3, -- Allocated Supply Quantity
820                                         SUM(DECODE(SUPPLY_DEMAND_TYPE, 2,
821                                                 DECODE(SUPPLY_DEMAND_SOURCE_TYPE,
822                                                                 48,  ALLOCATED_QUANTITY,
823                                                                 0),
824                                                 0))                                                     col4, -- Supply Adjustment Quantity
825                                         SUM(DECODE(SUPPLY_DEMAND_TYPE, 2, ALLOCATED_QUANTITY, 0))       col5, -- Total Supply
826                                         SUM(DECODE(SUPPLY_DEMAND_TYPE, 1, ALLOCATED_QUANTITY, 0))       col6, -- Total Demand
827                                         SUM(ALLOCATED_QUANTITY)                                         col7  -- Period Quantity
828                                 FROM
829                                         MRP_ATP_DETAILS_TEMP
830                                 WHERE
831                                         SESSION_ID = p_session_id
832                                         AND RECORD_TYPE = l_record_type
833                                 GROUP BY
834                                         DEMAND_CLASS, SUPPLY_DEMAND_DATE
835                                 ORDER BY
836                                         DEMAND_CLASS, SUPPLY_DEMAND_DATE --5233538 10G issue
837                                 ) final;
838                         END IF;
839 
840                         IF PG_DEBUG in ('Y', 'C') THEN
841                                 msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'After selecting supply/demand records from temp table into PL/SQL period table.');
842                                 msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'Before backward and forward consumption for each demand class');
843                         END IF;
844 
845                         -- Call procedure Backward_Forward_Consume to do backward and forward consumption
846                         Backward_Forward_Consume(x_atp_period, l_dc_list_tab, l_dc_start_index, l_dc_end_index, l_return_status);
847                         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
848                                 IF PG_DEBUG in ('Y', 'C') THEN
849                                         msc_sch_wb.atp_debug('Compute_Allocation_Details: ' || 'Error occured in procedure Backward_Forward_Consume');
850                                 END IF;
851                                 x_return_status := FND_API.G_RET_STS_ERROR;
852                                 return;
853                         END IF;
854 
855                         IF PG_DEBUG in ('Y', 'C') THEN
856                                 msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'Before accumulation for each demand class');
857                         END IF;
858 
859                         -- Copy Cumulative Quantity from Backward_Forward_Quantity
860                         x_atp_period.Cumulative_Quantity := x_atp_period.Backward_Forward_Quantity;
861 
862                         -- Call procedure Compute_Cum to do accumulation
863                         Compute_Cum(x_atp_period, l_dc_list_tab, l_dc_start_index, l_dc_end_index, l_return_status);
864                         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
865                                 IF PG_DEBUG in ('Y', 'C') THEN
866                                         msc_sch_wb.atp_debug('Compute_Allocation_Details: ' || 'Error occured in procedure Compute_Cum');
867                                 END IF;
868                                 x_return_status := FND_API.G_RET_STS_ERROR;
869                                 return;
870                         END IF;
871                 END IF;
872 
873                 /* Compute Period_End_Date for all demand classes */
874                 IF PG_DEBUG in ('Y', 'C') THEN
875                         msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'Before computing period_end_date');
876                 END IF;
877                 -- bug 2763784 (ssurendr)
878                 -- Should not error out if no s/d record found
879                 -- rajjain 03/20/2003 Bug 2860891
880                 -- Taken the IF condition out of FOR loop
881                 IF l_dc_start_index IS NOT NULL
882                     AND l_dc_start_index.COUNT>0 THEN
883                         FOR l_index_counter IN 1..l_dc_list_tab.COUNT LOOP
884                                 l_start_index   := l_dc_start_index(l_index_counter);
885                                 l_end_index     := l_dc_end_index(l_index_counter);
886 
887                                 -- Find Period End Date for all demand class records
888                                 FOR l_period_counter IN l_start_index..l_end_index LOOP
889                                         IF (l_period_counter = l_end_index) THEN
890                                                 /*IF (p_infinite_time_fence_date IS NOT NULL) THEN
891                                                         x_atp_period.Period_End_Date(l_period_counter) := p_infinite_time_fence_date - 1;
892                                                 ELSE
893                                                         x_atp_period.Period_End_Date(l_period_counter) := x_atp_period.Period_Start_Date(l_period_counter);
894                                                 END IF;*/
895                                                 -- Bug 3823042
896                                                 x_atp_period.Period_End_Date(l_period_counter) := p_infinite_time_fence_date - 1;
897                                         ELSE
898                                                 x_atp_period.Period_End_Date(l_period_counter) := x_atp_period.Period_Start_Date(l_period_counter + 1) - 1;
899                                         END IF;
900                                 END LOOP;
901                         END LOOP;
902                 END IF;
903                 IF PG_DEBUG in ('Y', 'C') THEN
904                         msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'After computing period_end_date');
905                         msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'Before appending infinite time fence date records');
906                 END IF;
907                 /* rajjain 02/19/2003 Bug 2806076
908                  * Add infinite time fence record for all the demand classes in demand class hierarchy*/
909 
910                 --IF p_infinite_time_fence_date IS NOT NULL THEN -- Bug 3823042, as in PDS, p_infinite_time_fence_date is never NULL
911 
912         	        SELECT demand_class
913         	        BULK   COLLECT INTO l_all_dc_list_tab
914         	        FROM   MSC_ALLOC_TEMP;
915 
916         	        IF PG_DEBUG in ('Y', 'C') THEN
917         	                msc_sch_wb.atp_debug('Compute_Allocation_Details: ' || 'l_all_dc_list_tab.COUNT: ' || l_all_dc_list_tab.COUNT);
918         	        END IF;
919 
920                         FOR l_index_counter IN 1..l_all_dc_list_tab.COUNT LOOP
921 
922                                 IF PG_DEBUG in ('Y', 'C') THEN
923                                         msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'Adding infinite time fence date for demand class '|| l_all_dc_list_tab(l_index_counter));
924                                 END IF;
925                                 MSC_SATP_FUNC.Extend_Atp_Period(x_atp_period, l_return_status);
926 
927                                 l_count := x_atp_period.Period_Start_Date.COUNT;
928 
929                                 x_atp_period.Demand_Class(l_count)                      := l_all_dc_list_tab(l_index_counter);
930                                 x_atp_period.Period_Start_Date(l_count)                 := p_infinite_time_fence_date;
931                                 x_atp_period.Allocated_Supply_Quantity(l_count)         := MSC_ATP_PVT.INFINITE_NUMBER;
932                                 x_atp_period.Supply_Adjustment_Quantity(l_count)        := 0;
933                                 x_atp_period.Total_Supply_Quantity(l_count)             := MSC_ATP_PVT.INFINITE_NUMBER;
934                                 x_atp_period.Total_Demand_Quantity(l_count)             := 0;
935                                 x_atp_period.Total_Bucketed_Demand_Quantity(l_count)    := 0; -- for time_phased_atp
936                                 x_atp_period.Period_Quantity(l_count)                   := MSC_ATP_PVT.INFINITE_NUMBER;
937                                 x_atp_period.Backward_Forward_Quantity(l_count)         := MSC_ATP_PVT.INFINITE_NUMBER;
938                                 x_atp_period.Period_End_Date(l_count)                   := p_infinite_time_fence_date;
939                                 x_atp_period.Cumulative_Quantity(l_count)               := MSC_ATP_PVT.INFINITE_NUMBER;
940                                 x_atp_period.Inventory_Item_Id(l_count)                 := p_inventory_item_id;
941                                 x_atp_period.Organization_Id(l_count)                   := p_organization_id;
942                                 x_atp_period.Identifier1(l_count)                       := p_instance_id;
943                                 x_atp_period.Scenario_Id(l_count)                       := l_scenario_id;
944                                 x_atp_period.Level(l_count)                             := l_level_id;
945                          END LOOP;
946 
947                 -- END IF;
948                 IF PG_DEBUG in ('Y', 'C') THEN
949                         msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'After appending infinite time fence records');
950                 END IF;
951 
952                 -- time_phased_atp changes begin
953                 IF l_time_phased_atp = 'Y' THEN
954                         IF PG_DEBUG in ('Y', 'C') THEN
955                                 msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'Before populating original demand qty in MADT');
956                         END IF;
957                         /* Now populate Original_Demand_Qty*/
958                         MSC_ATP_PF.Populate_Original_Demand_Qty(
959                         	MSC_ATP_PF.MADT,
960                         	p_session_id,
961                         	p_plan_id,
962                         	NULL,
963                                 l_return_status
964                         );
965 
966                         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
967                                 IF PG_DEBUG in ('Y', 'C') THEN
968                                         msc_sch_wb.atp_debug('Insert_SD_Into_Details_Temp: ' || 'Error occured in procedure Populate_Original_Demand_Qty');
969                                 END IF;
970                                 x_return_status := FND_API.G_RET_STS_ERROR;
971                                 return;
972                         END IF;
973                         IF PG_DEBUG in ('Y', 'C') THEN
974                                 msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'After populating original demand qty in MADT');
975                         END IF;
976                 END IF;
977                 -- time_phased_atp changes end
978 
979         ELSIF ((MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND (MSC_ATP_PVT.G_ALLOCATION_METHOD = 2)) THEN
980                 -- IF (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1 AND MSC_ATP_PVT.G_ALLOCATION_METHOD = 1) THEN
981 
982                 -- Rule based allocation; Demand class case
983                 -- initialize l_level_id. level_id remains -1 for demand class allocation
984                 l_level_id      := -1;
985 
986                 IF PG_DEBUG in ('Y', 'C') THEN
987                         msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'We are in demand class allocation.');
988                 END IF;
989 
990                 /* Store all the demand classes on the request date = next working day from SYSDATE
991                    and store them in msc_alloc_temp table.
992                    If no demand class found on request date, dont proceed any further*/
993 
994                 INSERT INTO MSC_ALLOC_TEMP(DEMAND_CLASS, PRIORITY, ALLOCATION_PERCENT)
995                 SELECT mv.demand_class, mv.priority, mv.allocation_percent
996                 FROM   msc_item_hierarchy_mv mv
997                 WHERE  mv.inventory_item_id = l_item_to_use
998                 AND    mv.organization_id = p_organization_id
999                 AND    mv.sr_instance_id = p_instance_id
1000                 AND    p_request_date BETWEEN effective_date AND disable_date
1001                 AND    mv.level_id = l_level_id;
1002 
1003                 IF (SQL%ROWCOUNT = 0) THEN
1004                         IF PG_DEBUG in ('Y', 'C') THEN
1005                                 msc_sch_wb.atp_debug('No Demand Class found');
1006                         END IF;
1007                         Set_Error(MSC_ATP_PVT.ATP_PROCESSING_ERROR);
1008                         x_return_status := FND_API.G_RET_STS_ERROR;
1009                         return;
1010                 END IF;
1011 
1012 
1013                 /* Modularize Item and Org Info */
1014                 MSC_ATP_PROC.get_global_org_info(p_instance_id, p_organization_id);
1015                 l_default_atp_rule_id := MSC_ATP_PVT.G_ORG_INFO_REC.default_atp_rule_id;
1016                 l_calendar_code := MSC_ATP_PVT.G_ORG_INFO_REC.cal_code;
1017                 l_calendar_exception_set_id := MSC_ATP_PVT.G_ORG_INFO_REC.cal_exception_set_id;
1018                 l_default_demand_class := MSC_ATP_PVT.G_ORG_INFO_REC.default_demand_class;
1019                 l_org_code := MSC_ATP_PVT.G_ORG_INFO_REC.org_code;
1020 
1021                 -- Bug 3823042
1022                 l_sys_next_date := MSC_CALENDAR.NEXT_WORK_DAY(
1023                                     l_calendar_code,
1024                                     p_instance_id,
1025                                     TRUNC(sysdate));
1026 
1027                 -- Debug info
1028                 IF PG_DEBUG in ('Y', 'C') THEN
1029                         msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'l_default_atp_rule_id='|| l_default_atp_rule_id);
1030                         msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'l_calendar_code='||l_calendar_code);
1031                         msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'l_calendar_exception_set_id'|| l_calendar_exception_set_id);
1032                         msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'l_default_demand_class'|| l_default_demand_class);
1033                         msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'l_org_code'|| l_org_code);
1034                 END IF;
1035 
1036                 /*
1037                 1. Copy the supply demand records from msc_supplies and msc_demands
1038                    into mrp_atp_details_temp. Perform allocation in the process.
1039                 2. Forward consumtion logic is such that individual stealing acts cannot be recorded,
1040                    therefore, we do not select from_demand_class.
1041                 3. MSC_ALLOC_TEMP here stores allocation demand classes. We make a cartesian to split the supplies/demands
1042                 4. Finally we put only those records where allocated_quantity <> 0
1043                 */
1044                 IF PG_DEBUG in ('Y', 'C') THEN
1045                         msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'Before copying supply/demand records from msc_supplies/msc_demands into temp tables.');
1046                 END IF;
1047 
1048                 -- time_phased_atp changes begin
1049                 IF l_time_phased_atp = 'Y' THEN
1050                         MSC_ATP_PF.Insert_SD_Into_Details_Temp(
1051                                 MSC_ATP_PF.User_Defined_DC,
1052                                 l_inv_item_id,
1053                                 l_pf_dest_id,
1054                                 p_inventory_item_id,
1055                                 l_pf_sr_id,
1056                                 p_organization_id,
1057                                 --bug3671294 now we donot need this as we will join with msc_system_items
1058                                 --l_inv_item_name,
1059                                 l_org_code,
1060                                 p_instance_id,
1061                                 p_plan_id,
1062                                 p_infinite_time_fence_date,
1063                                 l_level_id,
1064                                 p_session_id,
1065                                 l_record_type,
1066                                 l_scenario_id,
1067                                 l_uom_code,
1068                                 l_insert_count,
1069                                 l_return_status
1070                         );
1071                         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1072                                 IF PG_DEBUG in ('Y', 'C') THEN
1073                                         msc_sch_wb.atp_debug('Compute_Allocation_Details: ' || 'Error occured in procedure Insert_SD_Into_Details_Temp');
1074                                 END IF;
1075                                 RAISE FND_API.G_EXC_ERROR;
1076                         END IF;
1077                 -- time_phased_atp changes end
1078                 ELSE
1079 
1080                     IF ( MSC_ATP_PVT.G_OPTIMIZED_PLAN = 1) THEN
1081                         -- Bug 3823042: optimized plan, donot use msc_calendar_dates
1082 
1083                         IF PG_DEBUG in ('Y', 'C') THEN
1084                            msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'User defined demand class, Optimized plan');
1085                         END IF;
1086 
1087                         INSERT INTO MRP_ATP_DETAILS_TEMP
1088                         (session_id, atp_level, inventory_item_id, organization_id, identifier1, identifier3,
1089                         supply_demand_type, supply_demand_date, supply_demand_quantity, supply_demand_source_type,
1090                         allocated_quantity, record_type, scenario_id, disposition_name, demand_class, uom_code,
1091                         inventory_item_name, organization_code, identifier2, identifier4,
1092                         ORIG_CUSTOMER_SITE_NAME,ORIG_CUSTOMER_NAME,ORIG_DEMAND_CLASS,ORIG_REQUEST_DATE ) --bug3263368
1093                         SELECT col1, col2, col3, col4, col5, col6, col7, col8, col9, col10,
1094                         col11, col12, col13, col14, col15, col16, col17, col18, col19, col20,col21,col22,col23,col24
1095                         FROM
1096                         (SELECT p_session_id                            col1, -- session_id
1097                                 l_level_id                              col2, -- level_id
1098                                 p_inventory_item_id                     col3, -- inventory_item_id
1099                                 p_organization_id                       col4, -- organization_id
1100                                 p_instance_id                           col5, -- Identifier1
1101                                 D.DEMAND_ID                             col6, -- Identifier3
1102                                 1                                       col7, -- supply_demand_type
1103                                 -- Bug 3823042
1104                                 GREATEST(TRUNC(DECODE(D.RECORD_SOURCE,
1105                                     2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1106                                        DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1107                                               2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1108                                                  NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), l_sys_next_date) col8,
1109                                 --C.PRIOR_DATE                            col8, -- supply_demand_date
1110                                 -1 * (D.USING_REQUIREMENT_QUANTITY - NVL(d.reserved_quantity, 0))       col9, -- supply_demand_quantity -- Bug 3823042
1111                                 decode(D.ORIGINATION_TYPE,-100,30,D.ORIGINATION_TYPE)                      col10, -- supply_demand_source_type
1112                                 -1* (D.USING_REQUIREMENT_QUANTITY - NVL(d.reserved_quantity, 0)) *      -- Bug 3823042
1113                                         DECODE(decode(decode(D.ORIGINATION_TYPE,-100,30,D.ORIGINATION_TYPE),
1114                                                 6, decode(d.source_organization_id,
1115                                                         NULL, DECODE(D.DEMAND_CLASS, null, null,
1116                                                                 DECODE(TEMP.DEMAND_CLASS, '-1',
1117                                                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1118                                                                                 null, null, l_item_to_use, p_organization_id,
1119                                                                                 p_instance_id, /*c.prior_date,*/ TRUNC(DECODE(D.RECORD_SOURCE,
1120                                                                                                                         2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1121                                                                                                                            DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1122                                                                                                                                   2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1123                                                                                                                                   NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), l_level_id, D.DEMAND_CLASS),
1124                                                                         D.DEMAND_CLASS)),
1125                                                         -23453, DECODE(D.DEMAND_CLASS, null, null,
1126                                                                 DECODE(TEMP.DEMAND_CLASS, '-1',
1127                                                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1128                                                                                 null, null, l_item_to_use, p_organization_id,
1129                                                                                 p_instance_id, /*c.prior_date,*/ TRUNC(DECODE(D.RECORD_SOURCE,
1130                                                                                                                         2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1131                                                                                                                            DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1132                                                                                                                                   2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1133                                                                                                                                   NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), l_level_id, D.DEMAND_CLASS),
1134                                                                         D.DEMAND_CLASS)),
1135                                                         d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
1136                                                                 DECODE(TEMP.DEMAND_CLASS, '-1',
1137                                                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1138                                                                                 null, null, l_item_to_use, p_organization_id,
1139                                                                                 p_instance_id, /*c.prior_date,*/ TRUNC(DECODE(D.RECORD_SOURCE,
1140                                                                                                                         2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1141                                                                                                                            DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1142                                                                                                                                   2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1143                                                                                                                                   NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), l_level_id, D.DEMAND_CLASS),
1144                                                                         D.DEMAND_CLASS)), NULL),
1145                                                 30, decode(d.source_organization_id,
1146                                                         NULL, DECODE(D.DEMAND_CLASS, null, null,
1147                                                                 DECODE(TEMP.DEMAND_CLASS, '-1',
1148                                                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1149                                                                                 null, null, l_item_to_use, p_organization_id,
1150                                                                                 p_instance_id, /*c.prior_date,*/ TRUNC(DECODE(D.RECORD_SOURCE,
1151                                                                                                                         2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1152                                                                                                                            DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1153                                                                                                                                   2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1154                                                                                                                                   NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), l_level_id, D.DEMAND_CLASS),
1155                                                                         D.DEMAND_CLASS)),
1156                                                         -23453, DECODE(D.DEMAND_CLASS, null, null,
1157                                                                 DECODE(TEMP.DEMAND_CLASS, '-1',
1158                                                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1159                                                                                 null, null, l_item_to_use, p_organization_id,
1160                                                                                 p_instance_id, /*c.prior_date,*/ TRUNC(DECODE(D.RECORD_SOURCE,
1161                                                                                                                         2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1162                                                                                                                            DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1163                                                                                                                                   2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1164                                                                                                                                   NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), l_level_id, D.DEMAND_CLASS),
1165                                                                         D.DEMAND_CLASS)),
1166                                                         d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
1167                                                                 DECODE(TEMP.DEMAND_CLASS, '-1',
1168                                                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1169                                                                                 null, null, l_item_to_use, p_organization_id,
1170                                                                                 p_instance_id, /*c.prior_date,*/ TRUNC(DECODE(D.RECORD_SOURCE,
1171                                                                                                                         2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1172                                                                                                                            DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1173                                                                                                                                   2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1174                                                                                                                                   NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), l_level_id, D.DEMAND_CLASS),
1175                                                                         D.DEMAND_CLASS)), NULL),
1176                                                 DECODE(D.DEMAND_CLASS, null, null,
1177                                                         DECODE(TEMP.DEMAND_CLASS, '-1',
1178                                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1179                                                                         null, null, l_item_to_use, p_organization_id,
1180                                                                         p_instance_id, /*c.prior_date,*/ TRUNC(DECODE(D.RECORD_SOURCE,
1181                                                                                                                         2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1182                                                                                                                            DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1183                                                                                                                                   2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1184                                                                                                                                   NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), l_level_id, D.DEMAND_CLASS),
1185                                                                 D.DEMAND_CLASS))),
1186                                                 TEMP.DEMAND_CLASS, 1,
1187                                                 MSC_AATP_FUNC.Get_Item_Demand_Alloc_Percent(p_plan_id,
1188                                                         D.DEMAND_ID,
1189                                                         TRUNC(DECODE(D.RECORD_SOURCE,
1190                                                               2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1191                                                               DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1192                                                                      2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1193                                                                             NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
1194                                                         --c.prior_date,
1195                                                         D.USING_ASSEMBLY_ITEM_ID,
1196                                                         DECODE(D.SOURCE_ORGANIZATION_ID,
1197                                                         -23453, null,
1198                                                         D.SOURCE_ORGANIZATION_ID),
1199                                                         l_item_to_use,
1200                                                         p_organization_id,
1201                                                         p_instance_id,
1202                                                         decode(D.ORIGINATION_TYPE,-100,30,D.ORIGINATION_TYPE),
1203                                                         decode(decode(D.ORIGINATION_TYPE,-100,30,D.ORIGINATION_TYPE),
1204                                                                 6, decode(d.source_organization_id,
1205                                                                         NULL, DECODE(D.DEMAND_CLASS, null, null,
1206                                                                                 DECODE(TEMP.DEMAND_CLASS, '-1',
1207                                                                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1208                                                                                                 null, null, l_item_to_use, p_organization_id,
1209                                                                                                 p_instance_id, /*c.prior_date,*/ TRUNC(DECODE(D.RECORD_SOURCE,
1210                                                                                                                                                 2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1211                                                                                                                                                    DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1212                                                                                                                                                       2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1213                                                                                                                                                       NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), l_level_id, D.DEMAND_CLASS),
1214                                                                                                 D.DEMAND_CLASS)),
1215                                                                         -23453, DECODE(D.DEMAND_CLASS, null, null,
1216                                                                                 DECODE(TEMP.DEMAND_CLASS, '-1',
1217                                                                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1218                                                                                                 null, null, l_item_to_use, p_organization_id,
1219                                                                                                 p_instance_id, /*c.prior_date,*/ TRUNC(DECODE(D.RECORD_SOURCE,
1220                                                                                                                                                 2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1221                                                                                                                                                    DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1222                                                                                                                                                      2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1223                                                                                                                                                      NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),  l_level_id, D.DEMAND_CLASS),
1224                                                                                                 D.DEMAND_CLASS)),
1225                                                                         d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
1226                                                                                 DECODE(TEMP.DEMAND_CLASS, '-1',
1227                                                                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1228                                                                                                 null, null, l_item_to_use, p_organization_id,
1229                                                                                                 p_instance_id, /*c.prior_date,*/ TRUNC(DECODE(D.RECORD_SOURCE,
1230                                                                                                                                                 2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1231                                                                                                                                                    DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1232                                                                                                                                                      2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1233                                                                                                                                                      NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),  l_level_id, D.DEMAND_CLASS),
1234                                                                                                 D.DEMAND_CLASS)), TEMP.DEMAND_CLASS),
1235                                                                 30, decode(d.source_organization_id,
1236                                                                         NULL, DECODE(D.DEMAND_CLASS, null, null,
1237                                                                                 DECODE(TEMP.DEMAND_CLASS, '-1',
1238                                                                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1239                                                                                                 null, null, l_item_to_use, p_organization_id,
1240                                                                                                 p_instance_id, /*c.prior_date,*/ TRUNC(DECODE(D.RECORD_SOURCE,
1241                                                                                                                                                 2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1242                                                                                                                                                    DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1243                                                                                                                                                      2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1244                                                                                                                                                      NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),  l_level_id, D.DEMAND_CLASS),
1245                                                                                                 D.DEMAND_CLASS)),
1246                                                                         -23453, DECODE(D.DEMAND_CLASS, null, null,
1247                                                                                 DECODE(TEMP.DEMAND_CLASS, '-1',
1248                                                                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1249                                                                                                 null, null, l_item_to_use, p_organization_id,
1250                                                                                                 p_instance_id, /*c.prior_date,*/ TRUNC(DECODE(D.RECORD_SOURCE,
1251                                                                                                                                                 2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1252                                                                                                                                                    DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1253                                                                                                                                                      2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1254                                                                                                                                                      NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),  l_level_id, D.DEMAND_CLASS),
1255                                                                                                 D.DEMAND_CLASS)),
1256                                                                         d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
1257                                                                                 DECODE(TEMP.DEMAND_CLASS, '-1',
1258                                                                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1259                                                                                                 null, null, l_item_to_use, p_organization_id,
1260                                                                                                 p_instance_id, /*c.prior_date,*/ TRUNC(DECODE(D.RECORD_SOURCE,
1261                                                                                                                                                 2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1262                                                                                                                                                    DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1263                                                                                                                                                      2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1264                                                                                                                                                      NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),  l_level_id, D.DEMAND_CLASS),
1265                                                                                                 D.DEMAND_CLASS)), TEMP.DEMAND_CLASS),
1266                                                                 DECODE(D.DEMAND_CLASS, null, null,
1267                                                                         DECODE(TEMP.DEMAND_CLASS, '-1',
1268                                                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1269                                                                                         null, null, l_item_to_use, p_organization_id,
1270                                                                                         p_instance_id, /*c.prior_date,*/ TRUNC(DECODE(D.RECORD_SOURCE,
1271                                                                                                                                                 2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1272                                                                                                                                                    DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1273                                                                                                                                                      2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1274                                                                                                                                                      NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),  l_level_id, D.DEMAND_CLASS),
1275                                                                                         D.DEMAND_CLASS))),
1276                                                         TEMP.DEMAND_CLASS,
1277                                                         l_level_id))    col11, -- allocated_quantity
1278                                 l_record_type                           col12, -- record_type
1279                                 l_scenario_id                           col13, -- scenario_id
1280                                 -- rajjain 04/25/2003 Bug 2771075
1281                                 -- For Planned Order Demands We will populate disposition_id in disposition_name column
1282                                 DECODE(D.ORIGINATION_TYPE,
1283                                 1, to_char(D.DISPOSITION_ID),
1284                                 D.ORDER_NUMBER)                         col14, -- disposition_name
1285                                 TEMP.DEMAND_CLASS                       col15, -- demand_class
1286                                 l_uom_code                              col16, -- UOM Code
1287                                 l_item_name_to_use                      col17, -- Item name --Bug 3823042
1288                                 --l_inv_item_name                         col17, -- Item name
1289                                 l_org_code                              col18, -- Org code
1290                                 TEMP.PRIORITY                           col19, -- sysdate priroty
1291                                 TEMP.ALLOCATION_PERCENT                 col20,  -- sysdate allocation percent
1292                                 MTPS.LOCATION                           col21, --bug3263368
1293                                 MTP.PARTNER_NAME                        col22, --bug3263368
1294                                 D.DEMAND_CLASS                          col23, --bug3263368
1295                                 DECODE(D.ORDER_DATE_TYPE_CODE,2,
1296                                 D.REQUEST_DATE,D.REQUEST_SHIP_DATE)     col24 --bug3263368
1297 
1298                         FROM
1299                                 MSC_DEMANDS             D,
1300                                 --Bug 3823042, donot use msc_calendar_dates
1301                                 --MSC_CALENDAR_DATES      C,
1302                                 MSC_ALLOC_TEMP          TEMP,
1303                                 MSC_TRADING_PARTNERS    MTP, --bug3263368
1304                                 MSC_TRADING_PARTNER_SITES    MTPS --bug3263368
1305                         WHERE
1306                                 D.PLAN_ID = p_plan_id
1307                                 AND D.SR_INSTANCE_ID = p_instance_id
1308                                 AND D.INVENTORY_ITEM_ID = l_item_to_use -- Bug 3823042
1309                                 AND D.ORGANIZATION_ID = p_organization_id
1310                                 --AND D.ORIGINATION_TYPE NOT IN (5,7,8,9,11,15,22,28,29,31)
1311                                 AND D.ORIGINATION_TYPE NOT IN (5,7,8,9,11,15,22,28,29,31,52) -- Ignore copy SO
1312                                 AND D.SHIP_TO_SITE_ID = MTPS.PARTNER_SITE_ID(+) --bug3263368
1313                                 AND D.CUSTOMER_ID = MTP.PARTNER_ID(+)--bug3263368
1314                                 --Bug 3823042
1315                                 /*
1316                                 AND C.CALENDAR_CODE = l_calendar_code
1317                                 AND C.EXCEPTION_SET_ID = l_calendar_exception_set_id
1318                                 AND C.SR_INSTANCE_ID = D.SR_INSTANCE_ID
1319                                 AND C.CALENDAR_DATE
1320                                         BETWEEN
1321                                         -- Bug 3550296 and 3574164. IMPLEMENT_DATE AND DMD_SATISFIED_DATE are changed to
1322                                         -- IMPLEMENT_SHIP_DATE and PLANNED_SHIP_DATE resp.
1323                                         TRUNC(DECODE(RECORD_SOURCE,
1324                                               2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1325                                                  DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1326                                                         2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1327                                                            NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))))
1328                                         AND
1329                                         TRUNC(NVL(D.ASSEMBLY_DEMAND_COMP_DATE,
1330                                               DECODE(RECORD_SOURCE,
1331                                               2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1332                                                  DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1333                                                         2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1334                                                            NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))))
1335                                 AND (( D.ORIGINATION_TYPE = 4
1336                                         AND C.SEQ_NUM IS NOT NULL) OR
1337                                         ( D.ORIGINATION_TYPE  <> 4))
1338                                 AND C.PRIOR_DATE < NVL(p_infinite_time_fence_date, C.PRIOR_DATE + 1)
1339                                 */
1340                                 -- Bug 3823042, donot use msc_calendar_dates
1341                                 AND   TRUNC(DECODE(RECORD_SOURCE,
1342                                             2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1343                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1344                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1345                                                    NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))) < TRUNC(p_infinite_time_fence_date) -- Bug 3823042, pitf is not NULL in PDS case
1346                                 -- bug 2763784 (ssurendr)
1347                                 -- Should not select supply/demand where the original quantity itself is 0
1348                                 AND (D.USING_REQUIREMENT_QUANTITY - NVL(d.reserved_quantity, 0)) <> 0 -- Bug 3823042 , donot care about repetitive demands
1349                         UNION ALL
1350                         SELECT  p_session_id                            col1, -- session_id
1351                                 l_level_id                              col2, -- level_id
1352                                 p_inventory_item_id                     col3, -- inventory_item_id
1353                                 p_organization_id                       col4, -- organization_id
1354                                 p_instance_id                           col5, -- Identifier1
1355                                 S.TRANSACTION_ID                        col6, -- Identifier3
1356                                 2                                       col7, -- supply_demand_type
1357                                 -- Bug 3823042, donot use calendar_dates
1358                                 --C.NEXT_DATE                             col8, -- supply_demand_date
1359                                 GREATEST(TRUNC(NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE)),l_sys_next_date) col8,
1360                                 NVL(S.FIRM_QUANTITY,
1361                                         S.NEW_ORDER_QUANTITY)           col9, -- supply_demand_source_quantity
1362                                 S.ORDER_TYPE                            col10, -- supply_demand_source_type
1363                                 NVL(S.FIRM_QUANTITY,
1364                                         S.NEW_ORDER_QUANTITY)
1365                                         * DECODE(DECODE(S.DEMAND_CLASS, null, null,
1366                                                      DECODE(TEMP.DEMAND_CLASS,'-1',
1367                                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1368                                                           null,
1369                                                           null,
1370                                                           l_item_to_use,
1371                                                           p_organization_id,
1372                                                           p_instance_id,
1373                                                           --C.NEXT_DATE,
1374                                                           -- Bug 3823042
1375                                                           TRUNC(NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE)),
1376                                                           l_level_id,
1377                                                           S.DEMAND_CLASS), S.DEMAND_CLASS)),
1378                                                        TEMP.DEMAND_CLASS,
1379                                                         1,
1380                                                  NULL,
1381                                                         NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
1382                                                         p_instance_id,
1383                                                         S.inventory_item_id,
1384                                                         p_organization_id,
1385                                                         null,
1386                                                         null,
1387                                                         TEMP.DEMAND_CLASS,
1388                                                         -- Bug 3823042
1389                                                         --c.next_date,
1390                                                         TRUNC(NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE))),
1391                                                          1),
1392                                                 DECODE(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
1393                                                         p_instance_id,
1394                                                         S.inventory_item_id,
1395                                                         p_organization_id,
1396                                                         null,
1397                                                         null,
1398                                                         TEMP.DEMAND_CLASS,
1399                                                         -- Bug 3823042
1400                                                         --c.next_date,
1401                                                         TRUNC(NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE))),
1402                                                         NULL, 1, 0)
1403                                                 )                       col11, -- allocated_quantity
1404                                 l_record_type                           col12, -- record_type
1405                                 l_scenario_id                           col13, -- scenario_id
1406                              -- Bug 2771075. For Planned Orders, we will populate transaction_id
1407                              -- in the disposition_name column to be consistent with Planning.
1408                                 DECODE(S.ORDER_TYPE,
1409                                         5, to_char(S.TRANSACTION_ID),
1410                                         S.ORDER_NUMBER)                 col14, -- disposition_name
1411                                 TEMP.DEMAND_CLASS                       col15, -- demand_class
1412                                 l_uom_code                              col16, -- UOM Code
1413                                 l_item_name_to_use                      col17, -- Item Name --Bug 3823042
1414                                 --l_inv_item_name                         col17, -- Item name
1415                                 l_org_code                              col18, -- Org code
1416                                 TEMP.PRIORITY                           col19, -- sysdate priroty
1417                                 TEMP.ALLOCATION_PERCENT                 col20, -- sysdate allocation percent
1418                                 l_null_char                             col21, --bug3263368 ORIG_CUSTOMER_SITE_NAME --Bug 3875786
1419                                 l_null_char                             col22, --bug3263368 ORIG_CUSTOMER_NAME --Bug 3875786
1420                                 l_null_char                             col23, --bug3263368 ORIG_DEMAND_CLASS --Bug 3875786
1421                                 l_null_date                             col24  --bug3263368 ORIG_REQUEST_DATE -- Bug 3875786 - null removed
1422                         FROM
1423                                 -- Bug 3823042
1424                                 --MSC_CALENDAR_DATES      C,
1425                                 MSC_SUPPLIES            S,
1426                                 MSC_ALLOC_TEMP          TEMP
1427                         WHERE
1428                                 S.PLAN_ID = p_plan_id
1429                                 AND S.SR_INSTANCE_ID = p_instance_id
1430                                 AND S.INVENTORY_ITEM_ID = l_item_to_use -- Bug 3823042
1431                                 AND S.ORGANIZATION_ID = p_organization_id
1432                                 AND NVL(S.DISPOSITION_STATUS_TYPE, 1) <> 2
1433                                 AND NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY) <> 0
1434                                 /*
1435                                 AND C.CALENDAR_CODE = l_calendar_code
1436                                 AND C.EXCEPTION_SET_ID = l_calendar_exception_set_id
1437                                 AND C.SR_INSTANCE_ID = S.SR_INSTANCE_ID
1438                                 AND C.CALENDAR_DATE
1439                                         BETWEEN TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE))
1440                                         AND TRUNC(NVL(S.LAST_UNIT_COMPLETION_DATE, NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)))
1441                                 AND DECODE(S.LAST_UNIT_COMPLETION_DATE, NULL, C.NEXT_SEQ_NUM, C.SEQ_NUM) IS NOT NULL
1442                                 AND C.NEXT_DATE >= DECODE(S.ORDER_TYPE, 27, TRUNC(SYSDATE), 28, TRUNC(SYSDATE), C.NEXT_DATE)
1443                                 AND C.NEXT_DATE < NVL(p_infinite_time_fence_date, C.NEXT_DATE + 1)
1444                                 */
1445                                 AND     TRUNC(NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE)) >=  -- Bug 3823042, Using TRUNC's wherever required
1446                                             TRUNC(DECODE(S.ORDER_TYPE, 27,SYSDATE,
1447                                                                  28, SYSDATE,
1448                                                                  NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE)))
1449                                 AND     TRUNC(NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE)) < TRUNC(p_infinite_time_fence_date) -- Bug 3823042
1450                         );
1451                         l_insert_count := SQL%ROWCOUNT;
1452 
1453                    ELSE  -- Else of Optimized plan
1454                         -- Bug 3823042: Unoptimized Plan, Use msc_calendar_dates
1455 
1456                         IF PG_DEBUG in ('Y', 'C') THEN
1457                            msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'User defined demand class, Unoptimized plan');
1458                         END IF;
1459 
1460                         INSERT INTO MRP_ATP_DETAILS_TEMP
1461                         (session_id, atp_level, inventory_item_id, organization_id, identifier1, identifier3,
1462                         supply_demand_type, supply_demand_date, supply_demand_quantity, supply_demand_source_type,
1463                         allocated_quantity, record_type, scenario_id, disposition_name, demand_class, uom_code,
1464                         inventory_item_name, organization_code, identifier2, identifier4,
1465                         ORIG_CUSTOMER_SITE_NAME,ORIG_CUSTOMER_NAME,ORIG_DEMAND_CLASS,ORIG_REQUEST_DATE ) --bug3263368
1466                         SELECT col1, col2, col3, col4, col5, col6, col7, col8, col9, col10,
1467                         col11, col12, col13, col14, col15, col16, col17, col18, col19, col20,col21,col22,col23,col24
1468                         FROM
1469                         (SELECT p_session_id                            col1, -- session_id
1470                                 l_level_id                              col2, -- level_id
1471                                 p_inventory_item_id                     col3, -- inventory_item_id
1472                                 p_organization_id                       col4, -- organization_id
1473                                 p_instance_id                           col5, -- Identifier1
1474                                 D.DEMAND_ID                             col6, -- Identifier3
1475                                 1                                       col7, -- supply_demand_type
1476                                 GREATEST(C.CALENDAR_DATE,l_sys_next_date) col8, -- supply_demand_date
1477                                 --C.PRIOR_DATE                            col8, -- supply_demand_date
1478                                 -1 * DECODE(D.ORIGINATION_TYPE,
1479                                         4, D.DAILY_DEMAND_RATE,
1480                                         (D.USING_REQUIREMENT_QUANTITY - NVL(d.reserved_quantity, 0)))   col9, -- supply_demand_quantity
1481                                 decode(D.ORIGINATION_TYPE,-100,30,D.ORIGINATION_TYPE)                      col10, -- supply_demand_source_type
1482                                 -1* DECODE(D.ORIGINATION_TYPE,
1483                                         4, D.DAILY_DEMAND_RATE,
1484                                         (D.USING_REQUIREMENT_QUANTITY - NVL(d.reserved_quantity, 0)))*
1485                                         DECODE(decode(decode(D.ORIGINATION_TYPE,-100,30,D.ORIGINATION_TYPE),
1486                                                 6, decode(d.source_organization_id,
1487                                                         NULL, DECODE(D.DEMAND_CLASS, null, null,
1488                                                                 DECODE(TEMP.DEMAND_CLASS, '-1',
1489                                                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1490                                                                                 null, null, l_item_to_use, p_organization_id,
1491                                                                                 p_instance_id, C.CALENDAR_DATE, l_level_id, D.DEMAND_CLASS),
1492                                                                         D.DEMAND_CLASS)),
1493                                                         -23453, DECODE(D.DEMAND_CLASS, null, null,
1494                                                                 DECODE(TEMP.DEMAND_CLASS, '-1',
1495                                                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1496                                                                                 null, null, l_item_to_use, p_organization_id,
1497                                                                                 p_instance_id, C.CALENDAR_DATE, l_level_id, D.DEMAND_CLASS),
1498                                                                         D.DEMAND_CLASS)),
1499                                                         d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
1500                                                                 DECODE(TEMP.DEMAND_CLASS, '-1',
1501                                                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1502                                                                                 null, null, l_item_to_use, p_organization_id,
1503                                                                                 p_instance_id, C.CALENDAR_DATE, l_level_id, D.DEMAND_CLASS),
1504                                                                         D.DEMAND_CLASS)), NULL),
1505                                                 30, decode(d.source_organization_id,
1506                                                         NULL, DECODE(D.DEMAND_CLASS, null, null,
1507                                                                 DECODE(TEMP.DEMAND_CLASS, '-1',
1508                                                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1509                                                                                 null, null, l_item_to_use, p_organization_id,
1510                                                                                 p_instance_id, C.CALENDAR_DATE, l_level_id, D.DEMAND_CLASS),
1511                                                                         D.DEMAND_CLASS)),
1512                                                         -23453, DECODE(D.DEMAND_CLASS, null, null,
1513                                                                 DECODE(TEMP.DEMAND_CLASS, '-1',
1514                                                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1515                                                                                 null, null, l_item_to_use, p_organization_id,
1516                                                                                 p_instance_id, C.CALENDAR_DATE, l_level_id, D.DEMAND_CLASS),
1517                                                                         D.DEMAND_CLASS)),
1518                                                         d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
1519                                                                 DECODE(TEMP.DEMAND_CLASS, '-1',
1520                                                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1521                                                                                 null, null, l_item_to_use, p_organization_id,
1522                                                                                 p_instance_id, C.CALENDAR_DATE, l_level_id, D.DEMAND_CLASS),
1523                                                                         D.DEMAND_CLASS)), NULL),
1524                                                 DECODE(D.DEMAND_CLASS, null, null,
1525                                                         DECODE(TEMP.DEMAND_CLASS, '-1',
1526                                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1527                                                                         null, null, l_item_to_use, p_organization_id,
1528                                                                         p_instance_id, C.CALENDAR_DATE, l_level_id, D.DEMAND_CLASS),
1529                                                                 D.DEMAND_CLASS))),
1530                                                 TEMP.DEMAND_CLASS, 1,
1531                                                 MSC_AATP_FUNC.Get_Item_Demand_Alloc_Percent(p_plan_id,
1532                                                         D.DEMAND_ID,
1533                                                         C.CALENDAR_DATE,
1534                                                         D.USING_ASSEMBLY_ITEM_ID,
1535                                                         DECODE(D.SOURCE_ORGANIZATION_ID,
1536                                                         -23453, null,
1537                                                         D.SOURCE_ORGANIZATION_ID),
1538                                                         l_item_to_use,
1539                                                         p_organization_id,
1540                                                         p_instance_id,
1541                                                         decode(D.ORIGINATION_TYPE,-100,30,D.ORIGINATION_TYPE),
1542                                                         decode(decode(D.ORIGINATION_TYPE,-100,30,D.ORIGINATION_TYPE),
1543                                                                 6, decode(d.source_organization_id,
1544                                                                         NULL, DECODE(D.DEMAND_CLASS, null, null,
1545                                                                                 DECODE(TEMP.DEMAND_CLASS, '-1',
1546                                                                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1547                                                                                                 null, null, l_item_to_use, p_organization_id,
1548                                                                                                 p_instance_id, C.CALENDAR_DATE, l_level_id, D.DEMAND_CLASS),
1549                                                                                                 D.DEMAND_CLASS)),
1550                                                                         -23453, DECODE(D.DEMAND_CLASS, null, null,
1551                                                                                 DECODE(TEMP.DEMAND_CLASS, '-1',
1552                                                                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1553                                                                                                 null, null, l_item_to_use, p_organization_id,
1554                                                                                                 p_instance_id, C.CALENDAR_DATE, l_level_id, D.DEMAND_CLASS),
1555                                                                                                 D.DEMAND_CLASS)),
1556                                                                         d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
1557                                                                                 DECODE(TEMP.DEMAND_CLASS, '-1',
1558                                                                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1559                                                                                                 null, null, l_item_to_use, p_organization_id,
1560                                                                                                 p_instance_id, C.CALENDAR_DATE, l_level_id, D.DEMAND_CLASS),
1561                                                                                                 D.DEMAND_CLASS)), TEMP.DEMAND_CLASS),
1562                                                                 30, decode(d.source_organization_id,
1563                                                                         NULL, DECODE(D.DEMAND_CLASS, null, null,
1564                                                                                 DECODE(TEMP.DEMAND_CLASS, '-1',
1565                                                                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1566                                                                                                 null, null, l_item_to_use, p_organization_id,
1567                                                                                                 p_instance_id, C.CALENDAR_DATE, l_level_id, D.DEMAND_CLASS),
1568                                                                                                 D.DEMAND_CLASS)),
1569                                                                         -23453, DECODE(D.DEMAND_CLASS, null, null,
1570                                                                                 DECODE(TEMP.DEMAND_CLASS, '-1',
1571                                                                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1572                                                                                                 null, null, l_item_to_use, p_organization_id,
1573                                                                                                 p_instance_id, C.CALENDAR_DATE, l_level_id, D.DEMAND_CLASS),
1574                                                                                                 D.DEMAND_CLASS)),
1575                                                                         d.organization_id, DECODE(D.DEMAND_CLASS, null, null,
1576                                                                                 DECODE(TEMP.DEMAND_CLASS, '-1',
1577                                                                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1578                                                                                                 null, null, l_item_to_use, p_organization_id,
1579                                                                                                 p_instance_id, C.CALENDAR_DATE, l_level_id, D.DEMAND_CLASS),
1580                                                                                                 D.DEMAND_CLASS)), TEMP.DEMAND_CLASS),
1581                                                                 DECODE(D.DEMAND_CLASS, null, null,
1582                                                                         DECODE(TEMP.DEMAND_CLASS, '-1',
1583                                                                                 MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1584                                                                                         null, null, l_item_to_use, p_organization_id,
1585                                                                                         p_instance_id, C.CALENDAR_DATE, l_level_id, D.DEMAND_CLASS),
1586                                                                                         D.DEMAND_CLASS))),
1587                                                         TEMP.DEMAND_CLASS,
1588                                                         l_level_id))    col11, -- allocated_quantity
1589                                 l_record_type                           col12, -- record_type
1590                                 l_scenario_id                           col13, -- scenario_id
1591                                 -- rajjain 04/25/2003 Bug 2771075
1592                                 -- For Planned Order Demands We will populate disposition_id in disposition_name column
1593                                 DECODE(D.ORIGINATION_TYPE,
1594                                 1, to_char(D.DISPOSITION_ID),
1595                                 D.ORDER_NUMBER)                         col14, -- disposition_name
1596                                 TEMP.DEMAND_CLASS                       col15, -- demand_class
1597                                 l_uom_code                              col16, -- UOM Code
1598                                 l_item_name_to_use                      col17, -- Item Name --Bug 3823042
1599                                 --l_inv_item_name                         col17, -- Item name
1600                                 l_org_code                              col18, -- Org code
1601                                 TEMP.PRIORITY                           col19, -- sysdate priroty
1602                                 TEMP.ALLOCATION_PERCENT                 col20,  -- sysdate allocation percent
1603                                 MTPS.LOCATION                           col21, --bug3263368
1604                                 MTP.PARTNER_NAME                        col22, --bug3263368
1605                                 D.DEMAND_CLASS                          col23, --bug3263368
1606                                 DECODE(D.ORDER_DATE_TYPE_CODE,2,
1607                                 D.REQUEST_DATE,D.REQUEST_SHIP_DATE)     col24 --bug3263368
1608 
1609                         FROM
1610                                 MSC_DEMANDS             D,
1611                                 MSC_CALENDAR_DATES      C,
1612                                 MSC_ALLOC_TEMP          TEMP,
1613                                 MSC_TRADING_PARTNERS    MTP, --bug3263368
1614                                 MSC_TRADING_PARTNER_SITES    MTPS --bug3263368
1615                         WHERE
1616                                 D.PLAN_ID = p_plan_id
1617                                 AND D.SR_INSTANCE_ID = p_instance_id
1618                                 AND D.INVENTORY_ITEM_ID = l_item_to_use -- Bug 3823042
1619                                 AND D.ORGANIZATION_ID = p_organization_id
1620                                 --AND D.ORIGINATION_TYPE NOT IN (5,7,8,9,11,15,22,28,29,31)
1621                                 AND D.ORIGINATION_TYPE NOT IN (5,7,8,9,11,15,22,28,29,31,52) -- Ignore copy SO
1622                                 AND D.SHIP_TO_SITE_ID = MTPS.PARTNER_SITE_ID(+) --bug3263368
1623                                 AND D.CUSTOMER_ID = MTP.PARTNER_ID(+)--bug3263368
1624                                 AND C.CALENDAR_CODE = l_calendar_code
1625                                 AND C.EXCEPTION_SET_ID = l_calendar_exception_set_id
1626                                 AND C.SR_INSTANCE_ID = D.SR_INSTANCE_ID
1627                                 AND C.CALENDAR_DATE
1628                                         BETWEEN
1629                                         -- Bug 3550296 and 3574164. IMPLEMENT_DATE AND DMD_SATISFIED_DATE are changed to
1630                                         -- IMPLEMENT_SHIP_DATE and PLANNED_SHIP_DATE resp.
1631                                         TRUNC(DECODE(RECORD_SOURCE,
1632                                               2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1633                                                  DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1634                                                         2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1635                                                            NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))))
1636                                         AND
1637                                         TRUNC(NVL(D.ASSEMBLY_DEMAND_COMP_DATE,
1638                                               DECODE(RECORD_SOURCE,
1639                                               2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
1640                                                  DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
1641                                                         2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
1642                                                            NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))))
1643                                 AND (( D.ORIGINATION_TYPE = 4
1644                                         AND C.SEQ_NUM IS NOT NULL) OR
1645                                         ( D.ORIGINATION_TYPE  <> 4))
1646                                 AND C.CALENDAR_DATE <  TRUNC(p_infinite_time_fence_date) -- Bug 3823042
1647                                 -- bug 2763784 (ssurendr)
1648                                 -- Should not select supply/demand where the original quantity itself is 0
1649                                 AND DECODE(D.ORIGINATION_TYPE, 4, D.DAILY_DEMAND_RATE,
1650                                            (D.USING_REQUIREMENT_QUANTITY - NVL(d.reserved_quantity, 0))) <> 0
1651                         UNION ALL
1652                         SELECT  p_session_id                            col1, -- session_id
1653                                 l_level_id                              col2, -- level_id
1654                                 p_inventory_item_id                     col3, -- inventory_item_id
1655                                 p_organization_id                       col4, -- organization_id
1656                                 p_instance_id                           col5, -- Identifier1
1657                                 S.TRANSACTION_ID                        col6, -- Identifier3
1658                                 2                                       col7, -- supply_demand_type
1659                                 GREATEST(C.CALENDAR_DATE,l_sys_next_date) col8, -- supply_demand_date
1660                                 NVL(S.FIRM_QUANTITY,
1661                                         S.NEW_ORDER_QUANTITY)           col9, -- supply_demand_source_quantity
1662                                 S.ORDER_TYPE                            col10, -- supply_demand_source_type
1663                                 NVL(S.FIRM_QUANTITY,
1664                                         S.NEW_ORDER_QUANTITY)
1665                                         * DECODE(DECODE(S.DEMAND_CLASS, null, null,
1666                                                      DECODE(TEMP.DEMAND_CLASS,'-1',
1667                                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
1668                                                           null,
1669                                                           null,
1670                                                           l_item_to_use,
1671                                                           p_organization_id,
1672                                                           p_instance_id,
1673                                                           C.CALENDAR_DATE,
1674                                                           l_level_id,
1675                                                           S.DEMAND_CLASS), S.DEMAND_CLASS)),
1676                                                 TEMP.DEMAND_CLASS,
1677                                                         1,
1678                                                 NULL,
1679                                                         NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
1680                                                         p_instance_id,
1681                                                         S.inventory_item_id,
1682                                                         p_organization_id,
1683                                                         null,
1684                                                         null,
1685                                                         TEMP.DEMAND_CLASS,
1686                                                         c.calendar_date), 1),
1687                                                 DECODE(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
1688                                                         p_instance_id,
1689                                                         S.inventory_item_id,
1690                                                         p_organization_id,
1691                                                         null,
1692                                                         null,
1693                                                         TEMP.DEMAND_CLASS,
1694                                                         C.CALENDAR_DATE),
1695                                                         NULL, 1, 0)
1696                                                 )                       col11, -- allocated_quantity
1697                                 l_record_type                           col12, -- record_type
1698                                 l_scenario_id                           col13, -- scenario_id
1699                              -- Bug 2771075. For Planned Orders, we will populate transaction_id
1700                              -- in the disposition_name column to be consistent with Planning.
1701                                 DECODE(S.ORDER_TYPE,
1702                                         5, to_char(S.TRANSACTION_ID),
1703                                         S.ORDER_NUMBER)                 col14, -- disposition_name
1704                                 TEMP.DEMAND_CLASS                       col15, -- demand_class
1705                                 l_uom_code                              col16, -- UOM Code
1706                                 l_item_name_to_use                      col17, -- Item name --Bug 3823042
1707                                 --l_inv_item_name                         col17, -- Item name
1708                                 l_org_code                              col18, -- Org code
1709                                 TEMP.PRIORITY                           col19, -- sysdate priroty
1710                                 TEMP.ALLOCATION_PERCENT                 col20, -- sysdate allocation percent
1711                                 l_null_char                             col21, --bug3263368 ORIG_CUSTOMER_SITE_NAME --Bug 3875786
1712                                 l_null_char                             col22, --bug3263368 ORIG_CUSTOMER_NAME --Bug 3875786
1713                                 l_null_char                             col23, --bug3263368 ORIG_DEMAND_CLASS --Bug 3875786
1714                                 l_null_date                             col24  --bug3263368 ORIG_REQUEST_DATE -- Bug 3875786 - null removed
1715                         FROM
1716                                 MSC_CALENDAR_DATES      C,
1717                                 MSC_SUPPLIES            S,
1718                                 MSC_ALLOC_TEMP          TEMP
1719                         WHERE
1720                                 S.PLAN_ID = p_plan_id
1721                                 AND S.SR_INSTANCE_ID = p_instance_id
1722                                 AND S.INVENTORY_ITEM_ID = l_item_to_use
1723                                 AND S.ORGANIZATION_ID = p_organization_id
1724                                 AND NVL(S.DISPOSITION_STATUS_TYPE, 1) <> 2
1725                                 AND NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY) <> 0
1726                                 AND C.CALENDAR_CODE = l_calendar_code
1727                                 AND C.EXCEPTION_SET_ID = l_calendar_exception_set_id
1728                                 AND C.SR_INSTANCE_ID = S.SR_INSTANCE_ID
1729                                 AND C.CALENDAR_DATE
1730                                         BETWEEN TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE))
1731                                         AND TRUNC(NVL(S.LAST_UNIT_COMPLETION_DATE, NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)))
1732                                 AND DECODE(S.LAST_UNIT_COMPLETION_DATE, NULL, C.NEXT_SEQ_NUM, C.SEQ_NUM) IS NOT NULL
1733                                 -- Bug 3823042, Using TRUNC's wherever required
1734                                 AND C.CALENDAR_DATE >= TRUNC(DECODE(S.ORDER_TYPE, 27, SYSDATE, 28, SYSDATE, C.CALENDAR_DATE))
1735                                 AND C.CALENDAR_DATE < TRUNC(p_infinite_time_fence_date) -- Bug 3823042
1736                         );
1737                         l_insert_count := SQL%ROWCOUNT;
1738 
1739                    END IF;
1740                 END IF;
1741 
1742                 IF (l_insert_count = 0) THEN
1743                         IF PG_DEBUG in ('Y', 'C') THEN
1744                                 msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'No s/d records could be inserted from msc_supplies/demands tables into temp table');
1745                         END IF;
1746 
1747                         -- bug 2763784 (ssurendr)
1748                         -- Should not error out if no s/d record found
1749                         --Set_Error(MSC_ATP_PVT.ATP_PROCESSING_ERROR);
1750                         --x_return_status := FND_API.G_RET_STS_ERROR;
1751                         --return;
1752                         SELECT demand_class
1753                         BULK   COLLECT INTO l_dc_list_tab
1754                         FROM   MSC_ALLOC_TEMP;
1755 
1756                         l_dc_list_tab.Extend();
1757                         l_dc_list_tab(l_dc_list_tab.COUNT) := G_UNALLOCATED_DC;
1758 
1759                         IF PG_DEBUG in ('Y', 'C') THEN
1760                                 msc_sch_wb.atp_debug('Compute_Allocation_Details: ' || 'l_dc_list_tab.COUNT: ' || l_dc_list_tab.COUNT);
1761                         END IF;
1762                 ELSE
1763 
1764                         IF PG_DEBUG in ('Y', 'C') THEN
1765                                 msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'After copying supply/demand records from msc_supplies/msc_demands into temp tables.');
1766                         END IF;
1767 
1768                         /* Bulk Collect Allocated Supply, Total Supply,
1769                         Allocated Demand, Stolen Demand, Total Demand, Net into PL/SQL Period table. */
1770                         IF PG_DEBUG in ('Y', 'C') THEN
1771                                 msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'Before selecting supply/demand records from temp table into PL/SQL period table.');
1772                         END IF;
1773 
1774                         -- IF G_ATP_FW_CONSUME_METHOD = 1 THEN
1775                                 -- here was the same query without the unallocated columns
1776                                 -- always get unallocated figures
1777                                 -- removed for bug 2763784 (ssurendr)
1778                         -- ELSE
1779                                 -- Get unallocated picture as well.
1780 
1781                         -- time_phased_atp changes begin
1782                         IF l_time_phased_atp = 'Y' THEN
1783                                 MSC_ATP_PF.Get_Period_From_Details_Temp(
1784                                         MSC_ATP_PF.User_Defined_DC,
1785                                         p_inventory_item_id,
1786                                         p_organization_id,
1787                                         p_instance_id,
1788                                         l_scenario_id,
1789                                         l_level_id,
1790                                         l_record_type,
1791                                         p_session_id,
1792                                         x_atp_period,
1793                                         l_return_status
1794                                 );
1795                                 IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1796                                         IF PG_DEBUG in ('Y', 'C') THEN
1797                                                 msc_sch_wb.atp_debug('Compute_Allocation_Details: ' || 'Error occured in procedure Get_Period_From_Details_Temp');
1798                                         END IF;
1799                                         RAISE FND_API.G_EXC_ERROR;
1800                                 END IF;
1801                         -- time_phased_atp changes end
1802                         ELSE
1803                                 SELECT
1804                                         final.col1,
1805                                         final.col2,
1806                                         final.col3,
1807                                         final.col4,
1808                                         final.col5,
1809                                         null,
1810                                         p_inventory_item_id,
1811                                         p_organization_id,
1812                                         p_instance_id,
1813                                         l_scenario_id,
1814                                         l_level_id,
1815                                         null,
1816                                         0,
1817                                         0,
1818                                         final.col6,
1819                                         final.col7,
1820                                         final.col8,
1821                                         null, -- bug 3282426
1822                                         final.col9,
1823                                         final.col10
1824                                 BULK COLLECT INTO
1825                                         x_atp_period.Demand_Class,
1826                                         x_atp_period.Period_Start_Date,
1827                                         x_atp_period.Total_Supply_Quantity,
1828                                         x_atp_period.Total_Demand_Quantity,
1829                                         --x_atp_period.Total_Bucketed_Demand_Quantity, --time_phased_atp /*Bug 3263304*/
1830                                         x_atp_period.Period_Quantity,
1831                                         x_atp_period.Total_Bucketed_Demand_Quantity, --time_phased_atp /*Bug 3263304*/
1832                                         x_atp_period.Inventory_Item_Id,
1833                                         x_atp_period.Organization_Id,
1834                                         x_atp_period.Identifier1,
1835                                         x_atp_period.Scenario_Id,
1836                                         x_atp_period.Level,
1837                                         x_atp_period.Period_End_Date,
1838                                         x_atp_period.Cumulative_Quantity,
1839                                         x_atp_period.Demand_Adjustment_Quantity,
1840                                         x_atp_period.Identifier2,
1841                                         x_atp_period.Unallocated_Supply_Quantity,
1842                                         x_atp_period.Unallocated_Demand_Quantity,
1843                                         x_atp_period.Unalloc_Bucketed_Demand_Qty, -- bug 3282426
1844                                         x_atp_period.Unallocated_Net_Quantity,
1845                                         x_atp_period.Identifier4
1846                                 FROM
1847                                 (SELECT DEMAND_CLASS                                                    col1,
1848                                         SUPPLY_DEMAND_DATE                                              col2,
1849                                         SUM(DECODE(SUPPLY_DEMAND_TYPE, 2, ALLOCATED_QUANTITY, 0))       col3,
1850                                         SUM(DECODE(SUPPLY_DEMAND_TYPE, 1, ALLOCATED_QUANTITY, 0))       col4,
1851                                         SUM(ALLOCATED_QUANTITY)                                         col5,
1852                                         IDENTIFIER2                                                     col6,
1853                                         SUM(DECODE(SUPPLY_DEMAND_TYPE, 2, SUPPLY_DEMAND_QUANTITY, 0))   col7,
1854                                         SUM(DECODE(SUPPLY_DEMAND_TYPE, 1, SUPPLY_DEMAND_QUANTITY, 0))   col8,
1855                                         SUM(SUPPLY_DEMAND_QUANTITY)                                     col9,
1856                                         IDENTIFIER4                                                     col10
1857                                 FROM    MRP_ATP_DETAILS_TEMP
1858                                 WHERE   SESSION_ID = p_session_id
1859                                 AND     RECORD_TYPE = l_record_type
1860                                 GROUP BY DEMAND_CLASS, SUPPLY_DEMAND_DATE,
1861                                         IDENTIFIER2, IDENTIFIER4
1862                                 ORDER BY IDENTIFIER2 ASC, -- Priority
1863                                         IDENTIFIER4 DESC, -- Allocation percent
1864                                         DEMAND_CLASS ASC, SUPPLY_DEMAND_DATE) final;
1865                         END IF;
1866 
1867                         IF PG_DEBUG in ('Y', 'C') THEN
1868                                 msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'After selecting supply/demand records from temp table into PL/SQL period table.');
1869                         END IF;
1870 
1871                         -- Call Adjust_Allocation_Details to compute everything except Infinite time fence records
1872                         Adjust_Allocation_Details(x_atp_period, l_dc_list_tab, l_dc_start_index, l_dc_end_index, l_return_status);
1873                         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1874                                 IF PG_DEBUG in ('Y', 'C') THEN
1875                                         msc_sch_wb.atp_debug('Compute_Allocation_Details: ' || 'Error occured in procedure Adjust_Allocation_Details');
1876                                 END IF;
1877                                 x_return_status := FND_API.G_RET_STS_ERROR;
1878                                 return;
1879                         END IF;
1880                 END IF;
1881 
1882 
1883                 /* Compute Period_End_Date for all demand classes and add infinite time fence records*/
1884                 IF PG_DEBUG in ('Y', 'C') THEN
1885                         msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'Before computing period_end_date');
1886                 END IF;
1887                 FOR l_index_counter IN 1..l_dc_list_tab.COUNT LOOP
1888                         -- bug 2763784 (ssurendr)
1889                         -- Should not error out if no s/d record found
1890                         IF l_dc_start_index IS NOT NULL
1891                             AND l_dc_start_index.COUNT>0 THEN
1892                                 l_start_index   := l_dc_start_index(l_index_counter);
1893                                 l_end_index     := l_dc_end_index(l_index_counter);
1894 
1895                                 -- Find Period End Date for all demand class records
1896                                 FOR l_period_counter IN l_start_index..l_end_index LOOP
1897                                         IF (l_period_counter = l_end_index) THEN
1898                                                 /*IF (p_infinite_time_fence_date IS NOT NULL) THEN
1899                                                         x_atp_period.Period_End_Date(l_period_counter) := p_infinite_time_fence_date - 1;
1900                                                 ELSE
1901                                                         x_atp_period.Period_End_Date(l_period_counter) := x_atp_period.Period_Start_Date(l_period_counter);
1902                                                 END IF;*/
1903                                                 -- Bug 3823042
1904                                                 x_atp_period.Period_End_Date(l_period_counter) := p_infinite_time_fence_date - 1;
1905                                         ELSE
1906                                                 x_atp_period.Period_End_Date(l_period_counter) := x_atp_period.Period_Start_Date(l_period_counter + 1) - 1;
1907                                         END IF;
1908                                 END LOOP;
1909                         END IF;
1910 
1911                         -- Add Inifinite time fence date records for each demand class at the end.
1912                         -- Bug 3823042, as in PDS, p_infinite_time_fence_date is never NULL
1913                         -- IF p_infinite_time_fence_date IS NOT NULL THEN
1914                                 IF PG_DEBUG in ('Y', 'C') THEN
1915                                         msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'Adding infinite time fence date for demand class '|| l_dc_list_tab(l_index_counter));
1916                                 END IF;
1917 
1918                                 MSC_SATP_FUNC.Extend_Atp_Period(x_atp_period, l_return_status);
1919                                 l_count := x_atp_period.Period_Start_Date.COUNT;
1920 
1921                                 x_atp_period.Demand_Class(l_count) := l_dc_list_tab(l_index_counter);
1922                                 x_atp_period.Period_Start_Date(l_count) := p_infinite_time_fence_date;
1923                                 x_atp_period.Total_Supply_Quantity(l_count) := MSC_ATP_PVT.INFINITE_NUMBER;
1924                                 x_atp_period.Total_Demand_Quantity(l_count) := 0;
1925                                 x_atp_period.Total_Bucketed_Demand_Quantity(l_count) := 0; -- for time_phased_atp
1926                                 x_atp_period.Period_Quantity(l_count) := MSC_ATP_PVT.INFINITE_NUMBER;
1927                                 x_atp_period.Period_End_Date(l_count) := p_infinite_time_fence_date;
1928                                 x_atp_period.Cumulative_Quantity(l_count) := MSC_ATP_PVT.INFINITE_NUMBER;
1929                                 x_atp_period.Inventory_Item_Id(l_count) := p_inventory_item_id;
1930                                 x_atp_period.Organization_Id(l_count) := p_organization_id;
1931                                 x_atp_period.Identifier1(l_count) := p_instance_id;
1932                                 x_atp_period.Scenario_Id(l_count) := l_scenario_id;
1933                                 x_atp_period.Level(l_count) := l_level_id;
1934                                 x_atp_period.Backward_Quantity(l_count) := MSC_ATP_PVT.INFINITE_NUMBER;
1935                                 x_atp_period.Demand_Adjustment_Quantity(l_count) := 0;
1936                                 x_atp_period.Adjusted_Availability_Quantity(l_count) := MSC_ATP_PVT.INFINITE_NUMBER;
1937 
1938                                 IF G_ATP_FW_CONSUME_METHOD = 2 THEN
1939                                         x_atp_period.Adjusted_Cum_Quantity(l_count) := MSC_ATP_PVT.INFINITE_NUMBER;
1940                                 END IF;
1941                       --END IF;
1942 
1943                 END LOOP;
1944                 IF PG_DEBUG in ('Y', 'C') THEN
1945                         msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'After computing period_end_date and appending infinite time fence records');
1946                 END IF;
1947 
1948         ELSIF (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 2 AND MSC_ATP_PVT.G_ALLOCATION_METHOD = 2) THEN
1949                 -- ELSIF (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1 AND MSC_ATP_PVT.G_ALLOCATION_METHOD = 2) THEN
1950 
1951                 -- initialize l_level_id. We first select all leaf node records
1952                 l_level_id      := 3;
1953 
1954                 IF PG_DEBUG in ('Y', 'C') THEN
1955                         msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'We are in customer class allocation.');
1956                 END IF;
1957 
1958                 /* Modularize Item and Org Info */
1959                 MSC_ATP_PROC.get_global_org_info(p_instance_id, p_organization_id);
1960                 l_default_atp_rule_id := MSC_ATP_PVT.G_ORG_INFO_REC.default_atp_rule_id;
1961                 l_calendar_code := MSC_ATP_PVT.G_ORG_INFO_REC.cal_code;
1962                 l_calendar_exception_set_id := MSC_ATP_PVT.G_ORG_INFO_REC.cal_exception_set_id;
1963                 l_default_demand_class := MSC_ATP_PVT.G_ORG_INFO_REC.default_demand_class;
1964                 l_org_code := MSC_ATP_PVT.G_ORG_INFO_REC.org_code;
1965 
1966                 -- Bug 3823042
1967                 l_sys_next_date := MSC_CALENDAR.NEXT_WORK_DAY(
1968                                     l_calendar_code,
1969                                     p_instance_id,
1970                                     TRUNC(sysdate));
1971 
1972                 -- Debug info
1973                 IF PG_DEBUG in ('Y', 'C') THEN
1974                         msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'l_default_atp_rule_id='|| l_default_atp_rule_id);
1975                         msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'l_calendar_code='||l_calendar_code);
1976                         msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'l_calendar_exception_set_id'|| l_calendar_exception_set_id);
1977                         msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'l_default_demand_class'|| l_default_demand_class);
1978                         msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'l_org_code'|| l_org_code);
1979                 END IF;
1980 
1981                 /* 1. Find all the level 3 nodes for given item/inst/org/on p_request_date
1982                    2. For each level 3 record, find parent demand class and grandparent demand class
1983                    3. Store all these in 8i temp table msc_alloc_hierarchy_temp.
1984                    4. Also store the partner_id, partner_site_id and priority of level 3 demand classes*/
1985 
1986                 INSERT INTO MSC_ALLOC_HIERARCHY_TEMP( LEVEL_3_DEMAND_CLASS,
1987                 LEVEL_2_DEMAND_CLASS, LEVEL_1_DEMAND_CLASS, PARTNER_ID, PARTNER_SITE_ID,
1988                 LEVEL_3_DEMAND_CLASS_PRIORITY, ALLOCATION_PERCENT, CUSTOMER_NAME, CUSTOMER_SITE_NAME)
1989                 SELECT A.demand_class, B.demand_class, A.class, A.partner_id,
1990                        A.partner_site_id, A.priority, A.allocation_percent, mtp.partner_name, mtps.location
1991                 FROM   msc_item_hierarchy_mv A, msc_item_hierarchy_mv B,
1992                        msc_trading_partners mtp, msc_trading_partner_sites mtps
1993                 WHERE  A.inventory_item_id = l_item_to_use
1994                 AND    A.organization_id = p_organization_id
1995                 AND    A.sr_instance_id = p_instance_id
1996                 AND    p_request_date BETWEEN A.effective_date AND A.disable_date
1997                 AND    A.level_id = 3
1998                 AND    B.inventory_item_id = A.inventory_item_id
1999                 AND    B.organization_id = A.organization_id
2000                 AND    B.sr_instance_id = A.sr_instance_id
2001                 AND    p_request_date BETWEEN B.effective_date AND B.disable_date
2002                 AND    B.level_id = 2
2003                 AND    B.class = A.class
2004                 AND    B.partner_id = A.partner_id
2005                 AND    A.partner_id = mtp.partner_id (+)
2006                 AND    A.partner_site_id = mtps.partner_site_id (+);
2007 
2008                 IF (SQL%ROWCOUNT = 0) THEN
2009                         -- Need an appropriate error message.
2010                         IF PG_DEBUG in ('Y', 'C') THEN
2011                                 msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'No Demand Class found');
2012                         END IF;
2013                         Set_Error(MSC_ATP_PVT.ATP_PROCESSING_ERROR);
2014                         x_return_status := FND_API.G_RET_STS_ERROR;
2015                         return;
2016                 END IF;
2017 
2018 
2019                 /*
2020                 1. Copy the supply demand records from msc_supplies and msc_demands
2021                    into mrp_atp_details_temp in case of Customer Class Allocated ATP.
2022                 2. Forward consumtion logic is such that individual stealing acts cannot be recorded,
2023                    therefore, we do not select from_demand_class.
2024                    But we select partner_id and partner_site_id, class and demand_class.
2025                 3. Finally we put only those records where allocated_quantity <> 0
2026                 */
2027 
2028 
2029                 IF PG_DEBUG in ('Y', 'C') THEN
2030                         msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'Before copying supply/demand records from msc_supplies/msc_demands into temp tables.');
2031                 END IF;
2032 
2033                 /* time_phased_atp changes begin */
2034                 IF l_time_phased_atp = 'Y' THEN
2035                         MSC_ATP_PF.Insert_SD_Into_Details_Temp(
2036                                 MSC_ATP_PF.User_Defined_CC,
2037                                 l_inv_item_id,
2038                                 l_pf_dest_id,
2039                                 p_inventory_item_id,
2040                                 l_pf_sr_id,
2041                                 p_organization_id,
2042                                 --bug3671294 now we donot need this as we will join with msc_system_items
2043                                 --l_inv_item_name,
2044                                 l_org_code,
2045                                 p_instance_id,
2046                                 p_plan_id,
2047                                 p_infinite_time_fence_date,
2048                                 l_level_id,
2049                                 p_session_id,
2050                                 l_record_type,
2051                                 l_scenario_id,
2052                                 l_uom_code,
2053                                 l_insert_count,
2054                                 l_return_status
2055                         );
2056                         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
2057                                 IF PG_DEBUG in ('Y', 'C') THEN
2058                                         msc_sch_wb.atp_debug('Compute_Allocation_Details: ' || 'Error occured in procedure Insert_SD_Into_Details_Temp');
2059                                 END IF;
2060                                 RAISE FND_API.G_EXC_ERROR;
2061                         END IF;
2062                 ELSE
2063                      IF (MSC_ATP_PVT.G_OPTIMIZED_PLAN = 1) THEN
2064                         -- Bug 3823042: optimized plan, donot use msc_calendar_dates
2065 
2066                         IF PG_DEBUG in ('Y', 'C') THEN
2067                            msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'User defined customer class, Optimized plan');
2068                         END IF;
2069 
2070                         INSERT INTO MRP_ATP_DETAILS_TEMP
2071                         (session_id, atp_level, inventory_item_id, organization_id, identifier1, identifier3,
2072                         supply_demand_type, supply_demand_date, supply_demand_quantity, supply_demand_source_type,
2073                         allocated_quantity, record_type, scenario_id, disposition_name, demand_class, class, customer_id,
2074                         customer_site_id, uom_code, inventory_item_name, organization_code, identifier2, identifier4,
2075                         Customer_Name, Customer_Site_Name,
2076                         ORIG_CUSTOMER_SITE_NAME,ORIG_CUSTOMER_NAME,ORIG_DEMAND_CLASS,ORIG_REQUEST_DATE ) --bug3263368
2077                         SELECT col1, col2, col3, col4, col5, col6, col7, col8, col9, col10,
2078                         col11, col12, col13, col14, col15, col16, col17, col18, col19, col20, col21,
2079                         col22, col23, col24, col25,col26, col27, col28, col29
2080                         FROM
2081                         (SELECT p_session_id                            col1, -- session_id
2082                                 l_level_id                              col2, -- level_id
2083                                 p_inventory_item_id                     col3, -- inventory_item_id
2084                                 p_organization_id                       col4, -- organization_id
2085                                 p_instance_id                           col5, -- Identifier1
2086                                 D.DEMAND_ID                             col6, -- Identifier3
2087                                 1                                       col7, -- supply_demand_type
2088                                 -- Bug 3823042
2089                                 --C.PRIOR_DATE                            col8, -- supply_demand_date
2090                                 GREATEST(
2091                                         TRUNC(DECODE(D.RECORD_SOURCE,
2092                                               2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2093                                               DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2094                                                      2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2095                                                      NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))), l_sys_next_date) col8,
2096                                 -1 * (D.USING_REQUIREMENT_QUANTITY - NVL(d.reserved_quantity, 0))       col9, -- supply_demand_quantity  -- Bug 3823042
2097                                 decode(D.ORIGINATION_TYPE,-100,30,D.ORIGINATION_TYPE)                      col10, -- supply_demand_source_type -- Bug 3823042
2098                                 -1* (D.USING_REQUIREMENT_QUANTITY - NVL(d.reserved_quantity, 0)) *
2099                                         DECODE(DECODE(D.CUSTOMER_ID, NULL, NULL,
2100                                                 0, NULL,
2101                                                 decode(decode(D.ORIGINATION_TYPE,-100,30,D.ORIGINATION_TYPE),
2102                                                         6, decode(d.source_organization_id,
2103                                                                 NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2104                                                                         D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, l_item_to_use,
2105                                                                         p_organization_id, p_instance_id, /*c.prior_date, */
2106                                                                         TRUNC(DECODE(D.RECORD_SOURCE,
2107                                                                               2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2108                                                                               DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2109                                                                                      2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2110                                                                                      NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
2111                                                                         l_level_id, NULL),
2112                                                                 -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2113                                                                         D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, l_item_to_use,
2114                                                                         p_organization_id, p_instance_id, /*c.prior_date,*/
2115                                                                         TRUNC(DECODE(D.RECORD_SOURCE,
2116                                                                               2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2117                                                                               DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2118                                                                                      2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2119                                                                                      NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
2120                                                                         l_level_id, NULL),
2121                                                                 d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2122                                                                         D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, l_item_to_use,
2123                                                                         p_organization_id, p_instance_id, /*c.prior_date,*/
2124                                                                         TRUNC(DECODE(D.RECORD_SOURCE,
2125                                                                               2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2126                                                                               DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2127                                                                                      2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2128                                                                                      NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
2129                                                                         l_level_id, NULL),
2130                                                                 NULL),
2131                                                         30, decode(d.source_organization_id,
2132                                                                 NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2133                                                                         D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, l_item_to_use,
2134                                                                         p_organization_id, p_instance_id, /*c.prior_date, */
2135                                                                         TRUNC(DECODE(D.RECORD_SOURCE,
2136                                                                               2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2137                                                                               DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2138                                                                                      2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2139                                                                                      NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
2140                                                                         l_level_id, NULL),
2141                                                                 -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2142                                                                         D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, l_item_to_use,
2143                                                                         p_organization_id, p_instance_id, /*c.prior_date,*/
2144                                                                         TRUNC(DECODE(D.RECORD_SOURCE,
2145                                                                               2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2146                                                                               DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2147                                                                                      2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2148                                                                               	     NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
2149                                                                         l_level_id, NULL),
2150                                                                 d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2151                                                                         D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, l_item_to_use,
2152                                                                         p_organization_id, p_instance_id, /*c.prior_date,*/
2153                                                                         TRUNC(DECODE(D.RECORD_SOURCE,
2154                                                                               2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2155                                                                               DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2156                                                                                      2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2157                                                                                      NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
2158                                                                         l_level_id, NULL),
2159                                                                 NULL),
2160                                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(D.CUSTOMER_ID, D.SHIP_TO_SITE_ID,
2161                                                                 l_item_to_use, p_organization_id, p_instance_id, /*c.prior_date*/
2162                                                                 TRUNC(DECODE(D.RECORD_SOURCE,
2163                                                                               2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2164                                                                               DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2165                                                                                      2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2166                                                                                      NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
2167                                                                 l_level_id, NULL))),
2168                                                 TEMP.LEVEL_3_DEMAND_CLASS, 1,
2169                                                 MSC_AATP_FUNC.Get_Item_Demand_Alloc_Percent(p_plan_id,
2170                                                         D.DEMAND_ID,
2171                                                         --c.prior_date,
2172                                                         TRUNC(DECODE(D.RECORD_SOURCE,
2173                                                               2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2174                                                               DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2175                                                                      2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2176                                                                      NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
2177                                                         D.USING_ASSEMBLY_ITEM_ID,
2178                                                         DECODE(D.SOURCE_ORGANIZATION_ID,
2179                                                         -23453, null,
2180                                                         D.SOURCE_ORGANIZATION_ID),
2181                                                         l_item_to_use,
2182                                                         p_organization_id,
2183                                                         p_instance_id,
2184                                                         decode(D.ORIGINATION_TYPE,-100,30,D.ORIGINATION_TYPE),
2185                                                         DECODE(D.CUSTOMER_ID, NULL, TEMP.LEVEL_3_DEMAND_CLASS,
2186                                                                 0, TEMP.LEVEL_3_DEMAND_CLASS,
2187                                                                 decode(decode(D.ORIGINATION_TYPE,-100,30,D.ORIGINATION_TYPE),
2188                                                                         6, decode(d.source_organization_id,
2189                                                                                 NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2190                                                                                         D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, l_item_to_use,
2191                                                                                         p_organization_id, p_instance_id, /*c.prior_date, */
2192                                                                                         TRUNC(DECODE(D.RECORD_SOURCE,
2193                                                                                               2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2194                                                                                               DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2195                                                                                                      2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2196                                                                                                      NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
2197                                                                                         l_level_id, NULL),
2198                                                                                 -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2199                                                                                         D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, l_item_to_use,
2200                                                                                         p_organization_id, p_instance_id, /*c.prior_date, */
2201                                                                                         TRUNC(DECODE(D.RECORD_SOURCE,
2202                                                                                               2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2203                                                                                               DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2204                                                                                                      2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2205                                                                                                      NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
2206                                                                                         l_level_id, NULL),
2207                                                                                 d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2208                                                                                         D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, l_item_to_use,
2209                                                                                         p_organization_id, p_instance_id, /*c.prior_date, */
2210                                                                                         TRUNC(DECODE(D.RECORD_SOURCE,
2211                                                                                               2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2212                                                                                               DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2213                                                                                                      2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2214                                                                                                      NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
2215                                                                                         l_level_id, NULL),
2216                                                                                 TEMP.LEVEL_3_DEMAND_CLASS),
2217                                                                         30, decode(d.source_organization_id,
2218                                                                                 NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2219                                                                                         D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, l_item_to_use,
2220                                                                                         p_organization_id, p_instance_id, /*c.prior_date, */
2221                                                                                         TRUNC(DECODE(D.RECORD_SOURCE,
2222                                                                                               2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2223                                                                                               DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2224                                                                                                      2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2225                                                                                                      NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
2226                                                                                         l_level_id, NULL),
2227                                                                                 -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2228                                                                                         D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, l_item_to_use,
2229                                                                                         p_organization_id, p_instance_id, /*c.prior_date, */
2230                                                                                         TRUNC(DECODE(D.RECORD_SOURCE,
2231                                                                                               2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2232                                                                                               DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2233                                                                                                      2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2234                                                                                                      NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
2235                                                                                         l_level_id, NULL),
2236                                                                                 d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2237                                                                                         D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, l_item_to_use,
2238                                                                                         p_organization_id, p_instance_id, /*c.prior_date,*/
2239                                                                                         TRUNC(DECODE(D.RECORD_SOURCE,
2240                                                                                               2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2241                                                                                               DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2242                                                                                                      2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2243                                                                                                      NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
2244                                                                                         l_level_id, NULL),
2245                                                                                 TEMP.LEVEL_3_DEMAND_CLASS),
2246                                                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(D.CUSTOMER_ID, D.SHIP_TO_SITE_ID,
2247                                                                                 l_item_to_use, p_organization_id, p_instance_id, /*c.prior_date,*/
2248                                                                                 TRUNC(DECODE(D.RECORD_SOURCE,
2249                                                                                       2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2250                                                                                       DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2251                                                                                              2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2252                                                                                              NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))),
2253                                                                                 l_level_id, NULL))),
2254                                                         TEMP.LEVEL_3_DEMAND_CLASS,
2255                                                         l_level_id))    col11, -- allocated_quantity
2256                                 l_record_type                           col12, -- record_type
2257                                 l_scenario_id                           col13, -- scenario_id
2258                                 -- rajjain 04/25/2003 Bug 2771075
2259                                 -- For Planned Order Demands We will populate disposition_id in disposition_name column
2260                                 DECODE(D.ORIGINATION_TYPE,
2261                                 1, to_char(D.DISPOSITION_ID),
2262                                 D.ORDER_NUMBER)                         col14, -- disposition_name
2263                                 TEMP.LEVEL_3_DEMAND_CLASS               col15, -- demand_class
2264                                 TEMP.LEVEL_1_DEMAND_CLASS               col16, -- class
2265                                 TEMP.PARTNER_ID                         col17, -- partner_id
2266                                 TEMP.PARTNER_SITE_ID                    col18, -- partner_site_id
2267                                 l_uom_code                              col19, -- UOM Code
2268                                 l_item_name_to_use                      col20, -- Item name --Bug 3823042
2269                                 --l_inv_item_name                         col20, -- Item name
2270                                 l_org_code                              col21, -- Org code
2271                                 TEMP.LEVEL_3_DEMAND_CLASS_PRIORITY      col22, -- Level 3 priority
2272                                 TEMP.ALLOCATION_PERCENT                 col23, -- Sysdate allocation percent
2273                                 TEMP.customer_name                      col24, -- Customer Name
2274                                 TEMP.customer_site_name                 col25, -- Customer Site Name
2275                                 MTPS.LOCATION                           col26, --bug3263368
2276                                 MTP.PARTNER_NAME                        col27, --bug3263368
2277                                 D.DEMAND_CLASS                          col28, --bug3263368
2278                                 DECODE(D.ORDER_DATE_TYPE_CODE,2,
2279                                 D.REQUEST_DATE,D.REQUEST_SHIP_DATE)     col29 --bug3263368
2280                         FROM
2281                                 MSC_DEMANDS             D,
2282                                 -- Bug 3823042
2283                                 --MSC_CALENDAR_DATES      C,
2284                                 MSC_ALLOC_HIERARCHY_TEMP TEMP,
2285                                 MSC_TRADING_PARTNERS    MTP,--bug3263368
2286                                 MSC_TRADING_PARTNER_SITES    MTPS --bug3263368
2287 
2288                         WHERE
2289                                 D.PLAN_ID = p_plan_id
2290                                 AND D.SR_INSTANCE_ID = p_instance_id
2291                                 AND D.INVENTORY_ITEM_ID = l_item_to_use
2292                                 AND D.ORGANIZATION_ID = p_organization_id
2293                                 AND D.ORIGINATION_TYPE NOT IN (5,7,8,9,11,15,22,28,29,31,52) -- For summary enhancement
2294                                 AND D.CUSTOMER_SITE_ID = MTPS.PARTNER_SITE_ID(+) --bug3263368
2295                                 AND D.CUSTOMER_ID = MTP.PARTNER_ID(+)--bug3263368
2296                                 -- Bug 3823042
2297                                 /*
2298                                 AND C.CALENDAR_CODE = l_calendar_code
2299                                 AND C.EXCEPTION_SET_ID = l_calendar_exception_set_id
2300                                 AND C.SR_INSTANCE_ID = D.SR_INSTANCE_ID
2301                                 AND C.CALENDAR_DATE
2302                                         BETWEEN
2303                                         -- Bug 3550296 and 3574164. IMPLEMENT_DATE AND DMD_SATISFIED_DATE are changed to
2304                                         -- IMPLEMENT_SHIP_DATE and PLANNED_SHIP_DATE resp.
2305                                         TRUNC(DECODE(RECORD_SOURCE,
2306                                               2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2307                                                  DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2308                                                         2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2309                                                            NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))))
2310                                         AND
2311                                         TRUNC(NVL(D.ASSEMBLY_DEMAND_COMP_DATE,
2312                                               DECODE(RECORD_SOURCE,
2313                                               2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2314                                                  DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2315                                                         2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2316                                                            NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))))
2317                                 AND (( D.ORIGINATION_TYPE = 4
2318                                         AND C.SEQ_NUM IS NOT NULL) OR
2319                                         ( D.ORIGINATION_TYPE  <> 4))
2320                                 AND C.PRIOR_DATE < NVL(p_infinite_time_fence_date, C.PRIOR_DATE + 1)
2321                                 */
2322                                 AND   TRUNC(DECODE(RECORD_SOURCE,
2323                                             2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2324                                             DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2325                                                    2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2326                                                    NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))) < TRUNC(p_infinite_time_fence_date)
2327                                 -- bug 2763784 (ssurendr)
2328                                 -- Should not select supply/demand where the original quantity itself is 0
2329                                 AND (D.USING_REQUIREMENT_QUANTITY - NVL(d.reserved_quantity, 0)) <> 0 -- Bug 3823042, donot care about repititive demands
2330                         UNION ALL
2331                         SELECT  p_session_id                            col1, -- session_id
2332                                 l_level_id                              col2, -- level_id
2333                                 p_inventory_item_id                     col3, -- inventory_item_id
2334                                 p_organization_id                       col4, -- organization_id
2335                                 p_instance_id                           col5, -- Identifier1
2336                                 S.TRANSACTION_ID                        col6, -- Identifier3
2337                                 2                                       col7, -- supply_demand_type
2338                                 -- Bug 3823042
2339                                 --C.NEXT_DATE                             col8, -- supply_demand_date
2340                                 GREATEST(TRUNC(NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE)),l_sys_next_date) col8,
2341                                 NVL(S.FIRM_QUANTITY,
2342                                         S.NEW_ORDER_QUANTITY)           col9, -- supply_demand_source_quantity
2343                                 S.ORDER_TYPE                            col10, -- supply_demand_source_type
2344                                 NVL(S.FIRM_QUANTITY,
2345                                         S.NEW_ORDER_QUANTITY)
2346                                         * DECODE(DECODE(S.CUSTOMER_ID, NULL, NULL,
2347                                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2348                                                           S.CUSTOMER_ID,
2349                                                           S.SHIP_TO_SITE_ID,
2350                                                           l_item_to_use,
2351                                                           p_organization_id,
2352                                                           p_instance_id,
2353                                                           --C.NEXT_DATE,
2354                                                           TRUNC(NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE)),
2355                                                           l_level_id,
2356                                                           NULL)),
2357                                                 TEMP.LEVEL_3_DEMAND_CLASS,
2358                                                         1,
2359                                                 NULL,
2360                                                         NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
2361                                                         p_instance_id,
2362                                                         S.inventory_item_id,
2363                                                         p_organization_id,
2364                                                         null,
2365                                                         null,
2366                                                         TEMP.LEVEL_3_DEMAND_CLASS,
2367                                                         --c.next_date),
2368                                                         TRUNC(NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE))),
2369                                                          1),
2370                                                 DECODE(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
2371                                                         p_instance_id,
2372                                                         S.inventory_item_id,
2373                                                         p_organization_id,
2374                                                         null,
2375                                                         null,
2376                                                         TEMP.LEVEL_3_DEMAND_CLASS,
2377                                                         --c.next_date),
2378                                                         TRUNC(NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE))),
2379                                                         NULL, 1, 0)
2380                                                 )                       col11, -- allocated_quantity
2381                                 l_record_type                           col12, -- record_type
2382                                 l_scenario_id                           col13, -- scenario_id
2383                              -- Bug 2771075. For Planned Orders, we will populate transaction_id
2384                              -- in the disposition_name column to be consistent with Planning.
2385                                 DECODE(S.ORDER_TYPE,
2386                                         5, to_char(S.TRANSACTION_ID),
2387                                         S.ORDER_NUMBER)                 col14, -- disposition_name
2388                                 TEMP.LEVEL_3_DEMAND_CLASS               col15, -- demand_class
2389                                 TEMP.LEVEL_1_DEMAND_CLASS               col16, -- class
2390                                 TEMP.PARTNER_ID                         col17, -- partner_id
2391                                 TEMP.PARTNER_SITE_ID                    col18, -- partner_site_id
2392                                 l_uom_code                              col19, -- UOM Code
2393                                 l_item_name_to_use                      col20, -- Item name --Bug 3823042
2394                                 --l_inv_item_name                         col20, -- Item name
2395                                 l_org_code                              col21, -- Org code
2396                                 TEMP.LEVEL_3_DEMAND_CLASS_PRIORITY      col22, -- Level 3 priority
2397                                 TEMP.ALLOCATION_PERCENT                 col23, -- Sysdate allocation percent
2398                                 TEMP.customer_name                      col24, -- Customer Name
2399                                 TEMP.customer_site_name                 col25, -- Customer Site Name
2400                                 l_null_char                                    col26, --bug3263368 ORIG_CUSTOMER_SITE_NAME --Bug 3875786
2401                                 l_null_char                                    col27, --bug3263368 ORIG_CUSTOMER_NAME --Bug 3875786
2402                                 l_null_char                                    col28, --bug3263368 ORIG_DEMAND_CLASS --Bug 3875786
2403                                 l_null_date                             COL29  --bug3263368 ORIG_REQUEST_DATE -- Bug 3875786 - null removed
2404                         FROM
2405                                 -- Bug 3823042
2406                                 --MSC_CALENDAR_DATES      C,
2407                                 MSC_SUPPLIES            S,
2408                                 MSC_ALLOC_HIERARCHY_TEMP TEMP
2409                         WHERE
2410                                 S.PLAN_ID = p_plan_id
2411                                 AND S.SR_INSTANCE_ID = p_instance_id
2412                                 AND S.INVENTORY_ITEM_ID = l_item_to_use
2413                                 AND S.ORGANIZATION_ID = p_organization_id
2414                                 AND NVL(S.DISPOSITION_STATUS_TYPE, 1) <> 2
2415                                 AND NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY) <> 0
2416                                 /*
2417                                 AND C.CALENDAR_CODE = l_calendar_code
2418                                 AND C.EXCEPTION_SET_ID = l_calendar_exception_set_id
2419                                 AND C.SR_INSTANCE_ID = S.SR_INSTANCE_ID
2420                                 AND C.CALENDAR_DATE
2421                                         BETWEEN TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE))
2422                                         AND TRUNC(NVL(S.LAST_UNIT_COMPLETION_DATE, NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)))
2423                                 AND DECODE(S.LAST_UNIT_COMPLETION_DATE, NULL, C.NEXT_SEQ_NUM, C.SEQ_NUM) IS NOT NULL
2424                                 AND C.NEXT_DATE >= DECODE(S.ORDER_TYPE, 27, TRUNC(SYSDATE), 28, TRUNC(SYSDATE), C.NEXT_DATE)
2425                                 AND C.NEXT_DATE < NVL(p_infinite_time_fence_date, C.NEXT_DATE + 1)
2426                                 */
2427                                 -- Bug 3823042, Using TRUNC's wherever required
2428                                 AND     TRUNC(NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE)) >=
2429                                             TRUNC(DECODE(S.ORDER_TYPE, 27,SYSDATE,
2430                                                                  28, SYSDATE,
2431                                                                  NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE)))
2432                                AND     TRUNC(NVL(S.FIRM_DATE, S.NEW_SCHEDULE_DATE)) < TRUNC(p_infinite_time_fence_date) -- Bug 3823042
2433                         );
2434                         l_insert_count := SQL%ROWCOUNT;
2435 
2436                     ELSE -- Else of Optimized Plan
2437                         -- Bug 3823042: Unoptimized plan
2438 
2439                         IF PG_DEBUG in ('Y', 'C') THEN
2440                            msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'User defined customer class, Unoptimized plan');
2441                         END IF;
2442 
2443                         INSERT INTO MRP_ATP_DETAILS_TEMP
2444                         (session_id, atp_level, inventory_item_id, organization_id, identifier1, identifier3,
2445                         supply_demand_type, supply_demand_date, supply_demand_quantity, supply_demand_source_type,
2446                         allocated_quantity, record_type, scenario_id, disposition_name, demand_class, class, customer_id,
2447                         customer_site_id, uom_code, inventory_item_name, organization_code, identifier2, identifier4,
2448                         Customer_Name, Customer_Site_Name,
2449                         ORIG_CUSTOMER_SITE_NAME,ORIG_CUSTOMER_NAME,ORIG_DEMAND_CLASS,ORIG_REQUEST_DATE ) --bug3263368
2450                         SELECT col1, col2, col3, col4, col5, col6, col7, col8, col9, col10,
2451                         col11, col12, col13, col14, col15, col16, col17, col18, col19, col20, col21,
2452                         col22, col23, col24, col25,col26, col27, col28, col29
2453                         FROM
2454                         (SELECT p_session_id                            col1, -- session_id
2455                                 l_level_id                              col2, -- level_id
2456                                 p_inventory_item_id                     col3, -- inventory_item_id
2457                                 p_organization_id                       col4, -- organization_id
2458                                 p_instance_id                           col5, -- Identifier1
2459                                 D.DEMAND_ID                             col6, -- Identifier3
2460                                 1                                       col7, -- supply_demand_type
2461                                 --C.PRIOR_DATE                            col8, -- supply_demand_date
2462                                 GREATEST(C.CALENDAR_DATE,l_sys_next_date) col8, -- Supply_demand_date
2463                                 -1 * DECODE(D.ORIGINATION_TYPE,
2464                                         4, D.DAILY_DEMAND_RATE,
2465                                         (D.USING_REQUIREMENT_QUANTITY - NVL(d.reserved_quantity, 0)))   col9, -- supply_demand_quantity
2466                                 decode(D.ORIGINATION_TYPE,-100,30,D.ORIGINATION_TYPE)                      col10, -- supply_demand_source_type
2467                                 -1* DECODE(D.ORIGINATION_TYPE,
2468                                         4, D.DAILY_DEMAND_RATE,
2469                                         (D.USING_REQUIREMENT_QUANTITY - NVL(d.reserved_quantity, 0)))*
2470                                         DECODE(DECODE(D.CUSTOMER_ID, NULL, NULL,
2471                                                 0, NULL,
2472                                                 decode(decode(D.ORIGINATION_TYPE,-100,30,D.ORIGINATION_TYPE),
2473                                                         6, decode(d.source_organization_id,
2474                                                                 NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2475                                                                         D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, l_item_to_use,
2476                                                                         p_organization_id, p_instance_id, /*c.prior_date,*/
2477                                                                         C.CALENDAR_DATE,
2478                                                                         l_level_id, NULL),
2479                                                                 -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2480                                                                         D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, l_item_to_use,
2481                                                                         p_organization_id, p_instance_id, /*c.prior_date,*/
2482                                                                         C.CALENDAR_DATE,
2483                                                                         l_level_id, NULL),
2484                                                                 d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2485                                                                         D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, l_item_to_use,
2486                                                                         p_organization_id, p_instance_id, /*c.prior_date, */
2487                                                                         C.CALENDAR_DATE,
2488                                                                         l_level_id, NULL),
2489                                                                 NULL),
2490                                                         30, decode(d.source_organization_id,
2491                                                                 NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2492                                                                         D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, l_item_to_use,
2493                                                                         p_organization_id, p_instance_id, /*c.prior_date,*/
2494                                                                         C.CALENDAR_DATE,
2495                                                                         l_level_id, NULL),
2496                                                                 -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2497                                                                         D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, l_item_to_use,
2498                                                                         p_organization_id, p_instance_id, /*c.prior_date,*/
2499                                                                         C.CALENDAR_DATE,
2500                                                                         l_level_id, NULL),
2501                                                                 d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2502                                                                         D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, l_item_to_use,
2503                                                                         p_organization_id, p_instance_id, /*c.prior_date,*/
2504                                                                         C.CALENDAR_DATE,
2505                                                                         l_level_id, NULL),
2506                                                                 NULL),
2507                                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(D.CUSTOMER_ID, D.SHIP_TO_SITE_ID,
2508                                                                 l_item_to_use, p_organization_id, p_instance_id, /*c.prior_date,*/
2509                                                                 C.CALENDAR_DATE,
2510                                                                 l_level_id, NULL))),
2511                                                 TEMP.LEVEL_3_DEMAND_CLASS, 1,
2512                                                 MSC_AATP_FUNC.Get_Item_Demand_Alloc_Percent(p_plan_id,
2513                                                         D.DEMAND_ID,
2514                                                         --c.prior_date,
2515                                                         C.CALENDAR_DATE,
2516                                                         D.USING_ASSEMBLY_ITEM_ID,
2517                                                         DECODE(D.SOURCE_ORGANIZATION_ID,
2518                                                         -23453, null,
2519                                                         D.SOURCE_ORGANIZATION_ID),
2520                                                         l_item_to_use,
2521                                                         p_organization_id,
2522                                                         p_instance_id,
2523                                                         decode(D.ORIGINATION_TYPE,-100,30,D.ORIGINATION_TYPE),
2524                                                         DECODE(D.CUSTOMER_ID, NULL, TEMP.LEVEL_3_DEMAND_CLASS,
2525                                                                 0, TEMP.LEVEL_3_DEMAND_CLASS,
2526                                                                 decode(decode(D.ORIGINATION_TYPE,-100,30,D.ORIGINATION_TYPE),
2527                                                                         6, decode(d.source_organization_id,
2528                                                                                 NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2529                                                                                         D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, l_item_to_use,
2530                                                                                         p_organization_id, p_instance_id, /*c.prior_date,*/
2531                                                                                         C.CALENDAR_DATE,
2532                                                                                         l_level_id, NULL),
2533                                                                                 -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2534                                                                                         D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, l_item_to_use,
2535                                                                                         p_organization_id, p_instance_id, /*c.prior_date, */
2536                                                                                         C.CALENDAR_DATE,
2537                                                                                         l_level_id, NULL),
2538                                                                                 d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2539                                                                                         D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, l_item_to_use,
2540                                                                                         p_organization_id, p_instance_id, /*c.prior_date, */
2541                                                                                         C.CALENDAR_DATE,
2542                                                                                         l_level_id, NULL),
2543                                                                                 TEMP.LEVEL_3_DEMAND_CLASS),
2544                                                                         30, decode(d.source_organization_id,
2545                                                                                 NULL, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2546                                                                                         D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, l_item_to_use,
2547                                                                                         p_organization_id, p_instance_id, /*c.prior_date,*/
2548                                                                                         C.CALENDAR_DATE,
2549                                                                                         l_level_id, NULL),
2550                                                                                 -23453, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2551                                                                                         D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, l_item_to_use,
2552                                                                                         p_organization_id, p_instance_id, /*c.prior_date,*/
2553                                                                                         C.CALENDAR_DATE,
2554                                                                                         l_level_id, NULL),
2555                                                                                 d.organization_id, MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2556                                                                                         D.CUSTOMER_ID, D.SHIP_TO_SITE_ID, l_item_to_use,
2557                                                                                         p_organization_id, p_instance_id, /*c.prior_date,*/
2558                                                                                         C.CALENDAR_DATE,
2559                                                                                         l_level_id, NULL),
2560                                                                                 TEMP.LEVEL_3_DEMAND_CLASS),
2561                                                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(D.CUSTOMER_ID, D.SHIP_TO_SITE_ID,
2562                                                                                 l_item_to_use, p_organization_id, p_instance_id, /*c.prior_date,*/
2563                                                                                 C.CALENDAR_DATE,
2564                                                                                 l_level_id, NULL))),
2565                                                         TEMP.LEVEL_3_DEMAND_CLASS,
2566                                                         l_level_id))    col11, -- allocated_quantity
2567                                 l_record_type                           col12, -- record_type
2568                                 l_scenario_id                           col13, -- scenario_id
2569                                 -- rajjain 04/25/2003 Bug 2771075
2570                                 -- For Planned Order Demands We will populate disposition_id in disposition_name column
2571                                 DECODE(D.ORIGINATION_TYPE,
2572                                 1, to_char(D.DISPOSITION_ID),
2573                                 D.ORDER_NUMBER)                         col14, -- disposition_name
2574                                 TEMP.LEVEL_3_DEMAND_CLASS               col15, -- demand_class
2575                                 TEMP.LEVEL_1_DEMAND_CLASS               col16, -- class
2576                                 TEMP.PARTNER_ID                         col17, -- partner_id
2577                                 TEMP.PARTNER_SITE_ID                    col18, -- partner_site_id
2578                                 l_uom_code                              col19, -- UOM Code
2579                                 l_item_name_to_use                      col20, -- Item name --Bug 3823042
2580                                 --l_inv_item_name                         col20, -- Item name
2581                                 l_org_code                              col21, -- Org code
2582                                 TEMP.LEVEL_3_DEMAND_CLASS_PRIORITY      col22, -- Level 3 priority
2583                                 TEMP.ALLOCATION_PERCENT                 col23, -- Sysdate allocation percent
2584                                 TEMP.customer_name                      col24, -- Customer Name
2585                                 TEMP.customer_site_name                 col25,  -- Customer Site Name
2586                                 MTPS.LOCATION                           col26, --bug3263368
2587                                 MTP.PARTNER_NAME                        col27, --bug3263368
2588                                 D.DEMAND_CLASS                          col28, --bug3263368
2589                                 DECODE(D.ORDER_DATE_TYPE_CODE,2,
2590                                 D.REQUEST_DATE,D.REQUEST_SHIP_DATE)     col29 --bug3263368
2591                         FROM
2592                                 MSC_DEMANDS             D,
2593                                 MSC_CALENDAR_DATES      C,
2594                                 MSC_ALLOC_HIERARCHY_TEMP TEMP,
2595                                 MSC_TRADING_PARTNERS    MTP,--bug3263368
2596                                 MSC_TRADING_PARTNER_SITES    MTPS --bug3263368
2597 
2598                         WHERE
2599                                 D.PLAN_ID = p_plan_id
2600                                 AND D.SR_INSTANCE_ID = p_instance_id
2601                                 AND D.INVENTORY_ITEM_ID = l_item_to_use
2602                                 AND D.ORGANIZATION_ID = p_organization_id
2603                                 AND D.ORIGINATION_TYPE NOT IN (5,7,8,9,11,15,22,28,29,31,52) -- For summary enhancement
2604                                 AND D.CUSTOMER_SITE_ID = MTPS.PARTNER_SITE_ID(+) --bug3263368
2605                                 AND D.CUSTOMER_ID = MTP.PARTNER_ID(+)--bug3263368
2606                                 AND C.CALENDAR_CODE = l_calendar_code
2607                                 AND C.EXCEPTION_SET_ID = l_calendar_exception_set_id
2608                                 AND C.SR_INSTANCE_ID = D.SR_INSTANCE_ID
2609                                 AND C.CALENDAR_DATE
2610                                         BETWEEN
2611                                         -- Bug 3550296 and 3574164. IMPLEMENT_DATE AND DMD_SATISFIED_DATE are changed to
2612                                         -- IMPLEMENT_SHIP_DATE and PLANNED_SHIP_DATE resp.
2613                                         TRUNC(DECODE(RECORD_SOURCE,
2614                                               2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2615                                                  DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2616                                                         2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2617                                                            NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))))
2618                                         AND
2619                                         TRUNC(NVL(D.ASSEMBLY_DEMAND_COMP_DATE,
2620                                               DECODE(RECORD_SOURCE,
2621                                               2, NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE),
2622                                                  DECODE(MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF,
2623                                                         2, NVL(D.IMPLEMENT_SHIP_DATE,NVL(D.FIRM_DATE,NVL(D.PLANNED_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE))),
2624                                                            NVL(D.SCHEDULE_SHIP_DATE,D.USING_ASSEMBLY_DEMAND_DATE)))))
2625                                 AND (( D.ORIGINATION_TYPE = 4
2626                                         AND C.SEQ_NUM IS NOT NULL) OR
2627                                         ( D.ORIGINATION_TYPE  <> 4))
2628                                 -- Bug 3823042 , prior_date to calendar_date
2629                                 AND C.CALENDAR_DATE < TRUNC(p_infinite_time_fence_date)
2630                                 -- bug 2763784 (ssurendr)
2631                                 -- Should not select supply/demand where the original quantity itself is 0
2632                                 AND DECODE(D.ORIGINATION_TYPE, 4, D.DAILY_DEMAND_RATE,
2633                                            (D.USING_REQUIREMENT_QUANTITY - NVL(d.reserved_quantity, 0))) <> 0
2634                         UNION ALL
2635                         SELECT  p_session_id                            col1, -- session_id
2636                                 l_level_id                              col2, -- level_id
2637                                 p_inventory_item_id                     col3, -- inventory_item_id
2638                                 p_organization_id                       col4, -- organization_id
2639                                 p_instance_id                           col5, -- Identifier1
2640                                 S.TRANSACTION_ID                        col6, -- Identifier3
2641                                 2                                       col7, -- supply_demand_type
2642                                 --C.NEXT_DATE                             col8, -- supply_demand_date
2643                                 GREATEST(C.CALENDAR_DATE,l_sys_next_date) col8, -- supply_demand_date
2644                                 NVL(S.FIRM_QUANTITY,
2645                                         S.NEW_ORDER_QUANTITY)           col9, -- supply_demand_source_quantity
2646                                 S.ORDER_TYPE                            col10, -- supply_demand_source_type
2647                                 NVL(S.FIRM_QUANTITY,
2648                                         S.NEW_ORDER_QUANTITY)
2649                                         * DECODE(DECODE(S.CUSTOMER_ID, NULL, NULL,
2650                                                         MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(
2651                                                           S.CUSTOMER_ID,
2652                                                           S.SHIP_TO_SITE_ID,
2653                                                           l_item_to_use,
2654                                                           p_organization_id,
2655                                                           p_instance_id,
2656                                                           --C.NEXT_DATE,
2657                                                           C.CALENDAR_DATE,
2658                                                           l_level_id,
2659                                                           NULL)),
2660                                                 TEMP.LEVEL_3_DEMAND_CLASS,
2661                                                         1,
2662                                                 NULL,
2663                                                         NVL(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
2664                                                         p_instance_id,
2665                                                         S.inventory_item_id,
2666                                                         p_organization_id,
2667                                                         null,
2668                                                         null,
2669                                                         TEMP.LEVEL_3_DEMAND_CLASS,
2670                                                         --c.next_date),
2671                                                         C.CALENDAR_DATE),
2672                                                         1),
2673                                                 DECODE(MSC_AATP_FUNC.Get_DC_Alloc_Percent(
2674                                                         p_instance_id,
2675                                                         S.inventory_item_id,
2676                                                         p_organization_id,
2677                                                         null,
2678                                                         null,
2679                                                         TEMP.LEVEL_3_DEMAND_CLASS,
2680                                                         --c.next_date),
2681                                                         C.CALENDAR_DATE),
2682                                                         NULL, 1, 0)
2683                                                 )                       col11, -- allocated_quantity
2684                                 l_record_type                           col12, -- record_type
2685                                 l_scenario_id                           col13, -- scenario_id
2686                              -- Bug 2771075. For Planned Orders, we will populate transaction_id
2687                              -- in the disposition_name column to be consistent with Planning.
2688                                 DECODE(S.ORDER_TYPE,
2689                                         5, to_char(S.TRANSACTION_ID),
2690                                         S.ORDER_NUMBER)                 col14, -- disposition_name
2691                                 TEMP.LEVEL_3_DEMAND_CLASS               col15, -- demand_class
2692                                 TEMP.LEVEL_1_DEMAND_CLASS               col16, -- class
2693                                 TEMP.PARTNER_ID                         col17, -- partner_id
2694                                 TEMP.PARTNER_SITE_ID                    col18, -- partner_site_id
2695                                 l_uom_code                              col19, -- UOM Code
2696                                 l_item_name_to_use                      col20, -- Item name --Bug 3823042
2697                                 --l_inv_item_name                         col20, -- Item name
2698                                 l_org_code                              col21, -- Org code
2699                                 TEMP.LEVEL_3_DEMAND_CLASS_PRIORITY      col22, -- Level 3 priority
2700                                 TEMP.ALLOCATION_PERCENT                 col23, -- Sysdate allocation percent
2701                                 TEMP.customer_name                      col24, -- Customer Name
2702                                 TEMP.customer_site_name                 col25, -- Customer Site Name
2703                                 l_null_char                                    col26, --bug3263368 ORIG_CUSTOMER_SITE_NAME --Bug 3875786
2704                                 l_null_char                                    col27, --bug3263368 ORIG_CUSTOMER_NAME --Bug 3875786
2705                                 l_null_char                                    col28, --bug3263368 ORIG_DEMAND_CLASS --Bug 3875786
2706                                 l_null_date                             COL29  --bug3263368 ORIG_REQUEST_DATE -- Bug 3875786 - null removed
2707                         FROM
2708                                 MSC_CALENDAR_DATES      C,
2709                                 MSC_SUPPLIES            S,
2710                                 MSC_ALLOC_HIERARCHY_TEMP TEMP
2711                         WHERE
2712                                 S.PLAN_ID = p_plan_id
2713                                 AND S.SR_INSTANCE_ID = p_instance_id
2714                                 AND S.INVENTORY_ITEM_ID = l_item_to_use -- Bug 3823042
2715                                 AND S.ORGANIZATION_ID = p_organization_id
2716                                 AND NVL(S.DISPOSITION_STATUS_TYPE, 1) <> 2
2717                                 AND NVL(S.FIRM_QUANTITY,S.NEW_ORDER_QUANTITY) <> 0
2718                                 AND C.CALENDAR_CODE = l_calendar_code
2719                                 AND C.EXCEPTION_SET_ID = l_calendar_exception_set_id
2720                                 AND C.SR_INSTANCE_ID = S.SR_INSTANCE_ID
2721                                 AND C.CALENDAR_DATE
2722                                         BETWEEN TRUNC(NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE))
2723                                         AND TRUNC(NVL(S.LAST_UNIT_COMPLETION_DATE, NVL(S.FIRM_DATE,S.NEW_SCHEDULE_DATE)))
2724                                 AND DECODE(S.LAST_UNIT_COMPLETION_DATE, NULL, C.NEXT_SEQ_NUM, C.SEQ_NUM) IS NOT NULL
2725                                 -- Bug 3823042 , next_date to calendar_date
2726                                 AND C.CALENDAR_DATE >= TRUNC(DECODE(S.ORDER_TYPE, 27,SYSDATE, 28,SYSDATE, C.CALENDAR_DATE))
2727                                 AND C.CALENDAR_DATE < TRUNC(p_infinite_time_fence_date)
2728                         );
2729                         l_insert_count := SQL%ROWCOUNT;
2730                   END IF;
2731                 END IF;
2732 
2733                 IF (l_insert_count = 0) THEN
2734                         IF PG_DEBUG in ('Y', 'C') THEN
2735                                 msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'No s/d records inserted into temp table');
2736                         END IF;
2737 
2738                         -- bug 2763784 (ssurendr)
2739                         -- Should not error out if no s/d record found
2740                         --Set_Error(MSC_ATP_PVT.ATP_PROCESSING_ERROR);
2741                         --x_return_status := FND_API.G_RET_STS_ERROR;
2742                         --return;
2743                         SELECT LEVEL_3_DEMAND_CLASS, LEVEL_1_DEMAND_CLASS, PARTNER_ID, PARTNER_SITE_ID
2744                         BULK   COLLECT INTO l_dc_list_tab, l_class_tab, l_customer_id_tab, l_customer_site_id_tab
2745                         FROM   MSC_ALLOC_HIERARCHY_TEMP;
2746 
2747                         l_dc_list_tab.Extend();
2748                         l_class_tab.Extend();
2749                         l_customer_id_tab.Extend();
2750                         l_customer_site_id_tab.Extend();
2751                         l_dc_list_tab(l_dc_list_tab.COUNT) := G_UNALLOCATED_DC;
2752 
2753                         IF PG_DEBUG in ('Y', 'C') THEN
2754                                 msc_sch_wb.atp_debug('Compute_Allocation_Details: ' || 'l_dc_list_tab.COUNT: ' || l_dc_list_tab.COUNT);
2755                         END IF;
2756                 ELSE
2757 
2758                         IF PG_DEBUG in ('Y', 'C') THEN
2759                                 msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'After copying supply/demand records from msc_supplies/msc_demands into temp tables.');
2760                         END IF;
2761 
2762                         /* Bulk Collect Allocated Supply, Total Supply,
2763                         Allocated Demand, Stolen Demand, Total Demand, Net into PL/SQL Period table. */
2764                         IF PG_DEBUG in ('Y', 'C') THEN
2765                                 msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'Before selecting supply/demand records from temp table into PL/SQL period table.');
2766                         END IF;
2767 
2768                         -- IF G_ATP_FW_CONSUME_METHOD = 1 THEN
2769                                 -- here was the same query without the unallocated columns
2770                                 -- always get unallocated figures
2771                                 -- removed for bug 2763784 (ssurendr)
2772                         -- ELSE -- IF G_ATP_FW_CONSUME_METHOD = 1 THEN
2773                                 -- Get unallocated picture as well
2774 
2775                         -- time_phased_atp changes begin
2776                         IF l_time_phased_atp = 'Y' THEN
2777                                 MSC_ATP_PF.Get_Period_From_Details_Temp(
2778                                         MSC_ATP_PF.User_Defined_CC,
2779                                         p_inventory_item_id,
2780                                         p_organization_id,
2781                                         p_instance_id,
2782                                         l_scenario_id,
2783                                         l_level_id,
2784                                         l_record_type,
2785                                         p_session_id,
2786                                         x_atp_period,
2787                                         l_return_status
2788                                 );
2789                                 IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
2790                                         IF PG_DEBUG in ('Y', 'C') THEN
2791                                                 msc_sch_wb.atp_debug('Compute_Allocation_Details: ' || 'Error occured in procedure Get_Period_From_Details_Temp');
2792                                         END IF;
2793                                         RAISE FND_API.G_EXC_ERROR;
2794                                 END IF;
2795                         -- time_phased_atp changes end
2796                         ELSE
2797                                 SELECT
2798                                         final.col1,
2799                                         final.col2,
2800                                         final.col3,
2801                                         final.col4,
2802                                         final.col5,
2803                                         null,
2804                                         p_inventory_item_id,
2805                                         p_organization_id,
2806                                         p_instance_id,
2807                                         l_scenario_id,
2808                                         l_level_id,
2809                                         null,
2810                                         0,
2811                                         0,
2812                                         final.col6,
2813                                         final.col7,
2814                                         final.col8,
2815                                         final.col9,
2816                                         final.col10,
2817                                         final.col11,
2818                                         null, -- bug 3282426
2819                                         final.col12,
2820                                         final.col13
2821                                 BULK COLLECT INTO
2822                                         x_atp_period.Demand_Class,
2823                                         x_atp_period.Period_Start_Date,
2824                                         x_atp_period.Total_Supply_Quantity,
2825                                         x_atp_period.Total_Demand_Quantity,
2826                                         x_atp_period.Period_Quantity,
2827                                         x_atp_period.Total_Bucketed_Demand_Quantity, --time_phased_atp
2828                                         x_atp_period.Inventory_Item_Id,
2829                                         x_atp_period.Organization_Id,
2830                                         x_atp_period.Identifier1,
2831                                         x_atp_period.Scenario_Id,
2832                                         x_atp_period.Level,
2833                                         x_atp_period.Period_End_Date,
2834                                         x_atp_period.Cumulative_Quantity,
2835                                         x_atp_period.Demand_Adjustment_Quantity,
2836                                         x_atp_period.Identifier2,
2837                                         x_atp_period.Identifier4,
2838                                         x_atp_period.Class,
2839                                         x_atp_period.Customer_Id,
2840                                         x_atp_period.Unallocated_Supply_Quantity,
2841                                         x_atp_period.Unallocated_Demand_Quantity,
2842                                         x_atp_period.Unalloc_Bucketed_Demand_Qty, -- bug 3282426
2843                                         x_atp_period.Unallocated_Net_Quantity,
2844                                         x_atp_period.Customer_Site_Id
2845                                 FROM
2846                                 (SELECT DEMAND_CLASS                                                    col1,
2847                                         SUPPLY_DEMAND_DATE                                              col2,
2848                                         SUM(DECODE(SUPPLY_DEMAND_TYPE, 2, ALLOCATED_QUANTITY, 0))       col3,
2849                                         SUM(DECODE(SUPPLY_DEMAND_TYPE, 1, ALLOCATED_QUANTITY, 0))       col4,
2850                                         SUM(ALLOCATED_QUANTITY)                                         col5,
2851                                         IDENTIFIER2                                                     col6,
2852                                         IDENTIFIER4                                                     col7,
2853                                         CLASS                                                           col8,
2854                                         CUSTOMER_ID                                                     col9,
2855                                         SUM(DECODE(SUPPLY_DEMAND_TYPE, 2, SUPPLY_DEMAND_QUANTITY, 0))   col10,
2856                                         SUM(DECODE(SUPPLY_DEMAND_TYPE, 1, SUPPLY_DEMAND_QUANTITY, 0))   col11,
2857                                         SUM(SUPPLY_DEMAND_QUANTITY)                                     col12,
2858                                         CUSTOMER_SITE_ID                                                col13
2859                                 FROM    MRP_ATP_DETAILS_TEMP
2860                                 WHERE   SESSION_ID = p_session_id
2861                                 AND     RECORD_TYPE = l_record_type
2862                                 GROUP BY DEMAND_CLASS, SUPPLY_DEMAND_DATE, IDENTIFIER2, IDENTIFIER4,
2863                                         CLASS, CUSTOMER_ID, CUSTOMER_SITE_ID
2864                                 ORDER  BY trunc(IDENTIFIER2,-3),        -- Customer class priority
2865                                         CLASS,                          -- Customer class
2866                                         trunc(IDENTIFIER2,-2),          -- Customer priority
2867                                         CUSTOMER_ID,                    -- Customer
2868                                         IDENTIFIER2,                    -- Customer site priority
2869                                         CUSTOMER_SITE_ID,SUPPLY_DEMAND_DATE) final;
2870                         END IF;
2871 
2872                         -- END IF;              -- IF G_ATP_FW_CONSUME_METHOD = 1 THEN
2873 
2874                         IF PG_DEBUG in ('Y', 'C') THEN
2875                                 msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'After selecting supply/demand records from temp table into PL/SQL period table.');
2876                         END IF;
2877 
2878                         -- Call Adjust_Allocation_Details to compute everything except Infinite time fence records
2879                         Adjust_Allocation_Details(x_atp_period, l_dc_list_tab, l_dc_start_index, l_dc_end_index, l_return_status);
2880                         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
2881                                 IF PG_DEBUG in ('Y', 'C') THEN
2882                                         msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'Error occured in procedure Adjust_Allocation_Details');
2883                                 END IF;
2884                                 x_return_status := FND_API.G_RET_STS_ERROR;
2885                                 return;
2886                         END IF;
2887                 END IF;
2888 
2889                 /* Compute Period_End_Date for all demand classes and add infinite time fence records*/
2890                 IF PG_DEBUG in ('Y', 'C') THEN
2891                         msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'Before computing period_end_date');
2892                 END IF;
2893                 FOR l_index_counter IN 1..l_dc_list_tab.COUNT LOOP
2894                         -- bug 2763784 (ssurendr)
2895                         -- Should not error out if no s/d record found
2896                         IF l_dc_start_index IS NOT NULL
2897                             AND l_dc_start_index.COUNT>0 THEN
2898                                 l_start_index   := l_dc_start_index(l_index_counter);
2899                                 l_end_index     := l_dc_end_index(l_index_counter);
2900 
2901                                 -- Find Period End Date for all demand class records
2902                                 FOR l_period_counter IN l_start_index..l_end_index LOOP
2903                                         IF (l_period_counter = l_end_index) THEN
2904                                                 /*
2905                                                 IF (p_infinite_time_fence_date IS NOT NULL) THEN
2906                                                         x_atp_period.Period_End_Date(l_period_counter) := p_infinite_time_fence_date - 1;
2907                                                 ELSE
2908                                                         x_atp_period.Period_End_Date(l_period_counter) := x_atp_period.Period_Start_Date(l_period_counter);
2909                                                 END IF;
2910                                                 */
2911                                                 -- Bug 3823042
2912                                                 x_atp_period.Period_End_Date(l_period_counter) := p_infinite_time_fence_date - 1;
2913                                         ELSE
2914                                                 x_atp_period.Period_End_Date(l_period_counter) := x_atp_period.Period_Start_Date(l_period_counter + 1) - 1;
2915                                         END IF;
2916                                 END LOOP;
2917                         END IF;
2918 
2919                         -- Add Inifinite time fence date records for each demand class at the end.
2920                         -- Bug 3823042, In PDS cases, p_infinite_time_fence_date is never NULL
2921                         -- IF p_infinite_time_fence_date IS NOT NULL THEN
2922                                 IF PG_DEBUG in ('Y', 'C') THEN
2923                                         msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'Adding infinite time fence date for demand class '|| l_dc_list_tab(l_index_counter));
2924                                 END IF;
2925                                 MSC_SATP_FUNC.Extend_Atp_Period(x_atp_period, l_return_status);
2926 
2927                                 l_count := x_atp_period.Period_Start_Date.COUNT;
2928 
2929                                 x_atp_period.Demand_Class(l_count) := l_dc_list_tab(l_index_counter);
2930                                 IF l_dc_start_index IS NOT NULL
2931                                     AND l_dc_start_index.COUNT>0 THEN
2932                                         x_atp_period.Class(l_count) := x_atp_period.Class(l_start_index);
2933                                         x_atp_period.Customer_Id(l_count) := x_atp_period.Customer_Id(l_start_index);
2934                                         x_atp_period.Customer_Site_Id(l_count) := x_atp_period.Customer_Site_Id(l_start_index);
2935                                 ELSE
2936                                         x_atp_period.Class(l_count) := l_class_tab(l_index_counter);
2937                                         x_atp_period.Customer_Id(l_count) := l_customer_id_tab(l_index_counter);
2938                                         x_atp_period.Customer_Site_Id(l_count) := l_customer_site_id_tab(l_index_counter);
2939                                 END IF;
2940                                 x_atp_period.Period_Start_Date(l_count) := p_infinite_time_fence_date;
2941                                 x_atp_period.Total_Supply_Quantity(l_count) := MSC_ATP_PVT.INFINITE_NUMBER;
2942                                 x_atp_period.Total_Demand_Quantity(l_count) := 0;
2943                                 x_atp_period.Total_Bucketed_Demand_Quantity(l_count) := 0; -- for time_phased_atp
2944                                 x_atp_period.Period_Quantity(l_count) := MSC_ATP_PVT.INFINITE_NUMBER;
2945                                 x_atp_period.Period_End_Date(l_count) := p_infinite_time_fence_date;
2946                                 x_atp_period.Cumulative_Quantity(l_count) := MSC_ATP_PVT.INFINITE_NUMBER;
2947                                 x_atp_period.Inventory_Item_Id(l_count) := p_inventory_item_id;
2948                                 x_atp_period.Organization_Id(l_count) := p_organization_id;
2949                                 x_atp_period.Identifier1(l_count) := p_instance_id;
2950                                 x_atp_period.Scenario_Id(l_count) := l_scenario_id;
2951                                 x_atp_period.Level(l_count) := l_level_id;
2952                                 x_atp_period.Backward_Quantity(l_count) := MSC_ATP_PVT.INFINITE_NUMBER;
2953                                 x_atp_period.Demand_Adjustment_Quantity(l_count) := 0;
2954                                 x_atp_period.Adjusted_Availability_Quantity(l_count) := MSC_ATP_PVT.INFINITE_NUMBER;
2955 
2956                                 IF G_ATP_FW_CONSUME_METHOD = 2 THEN
2957                                         x_atp_period.Adjusted_Cum_Quantity(l_count) := MSC_ATP_PVT.INFINITE_NUMBER;
2958                                 END IF;
2959                         --END IF; -- Bug 3823042
2960 
2961                 END LOOP;
2962                 IF PG_DEBUG in ('Y', 'C') THEN
2963                         msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  'After computing period_end_date and appending infinite time fence records');
2964                 END IF;
2965 
2966         ELSE -- Customer Class Allocation ends
2967                 -- bug 2813095 (ssurendr) Profiles ALLOCATION_METHOD and CLASS_HIERARCHY are not set properly
2968                 IF PG_DEBUG in ('Y', 'C') THEN
2969                         msc_sch_wb.atp_debug('Compute_Allocation_Details: ' || 'Profiles ALLOCATION_METHOD and CLASS_HIERARCHY are not set properly');
2970                 END IF;
2971                 x_return_status := FND_API.G_RET_STS_ERROR;
2972                 Set_Error(MSC_ATP_PVT.INVALID_ALLOC_PROFILE_SETUP);
2973                 return;
2974 
2975         END IF;
2976         /* We have computed horizontal period. */
2977 
2978         IF PG_DEBUG in ('Y', 'C') THEN
2979                 msc_sch_wb.atp_debug('Compute_Allocation_Details: ' ||  '*********End of procedure Compute_Allocation_Details ********');
2980         END IF;
2981 
2982 EXCEPTION
2983 WHEN  MSC_ATP_PUB.ATP_INVALID_OBJECTS_FOUND THEN
2984         IF PG_DEBUG in ('Y', 'C') THEN
2985                 msc_sch_wb.atp_debug('Compute_Allocation_Details: ' || 'Error in Compute_Allocation_Details: Invalid Objects Found');
2986         END IF;
2987         x_return_status := FND_API.G_RET_STS_ERROR;
2988         Set_Error(MSC_ATP_PVT.ATP_INVALID_OBJECTS);
2989 
2990 WHEN OTHERS THEN
2991         IF PG_DEBUG in ('Y', 'C') THEN
2992                 msc_sch_wb.atp_debug('Compute_Allocation_Details: ' || 'Error in Compute_Allocation_Details: ' || to_char(sqlcode) || ':' || SQLERRM);
2993         END IF;
2994         x_return_status := FND_API.G_RET_STS_ERROR;
2995         Set_Error(MSC_ATP_PVT.ATP_PROCESSING_ERROR);
2996 
2997 END Compute_allocation_Details;
2998 
2999 
3000 /*--Insert_Allocation_Details-----------------------------------------------
3001 |  o Called by View_Allocation_Details after calling
3002 |    Compute_Allocation_Details.
3003 |  o Inserts period data in temp table and does totalling.
3004 +-------------------------------------------------------------------------*/
3005 PROCEDURE Insert_Allocation_Details(
3006         p_session_id                    IN              NUMBER,
3007         p_inventory_item_id             IN              NUMBER,
3008         p_organization_id               IN              NUMBER,
3009         p_instance_id                   IN              NUMBER,
3010         p_infinite_time_fence_date      IN              DATE,
3011         p_atp_period                    IN              MRP_ATP_PUB.ATP_Period_Typ,
3012         p_plan_name                     IN              VARCHAR2,  -- bug 2771192
3013         p_dest_inv_item_id              IN              NUMBER, -- For new allocation logic for time phased ATP
3014         p_dest_family_item_id           IN              NUMBER, -- For new allocation logic for time phased ATP
3015         x_return_status                 OUT     NOCOPY  VARCHAR2)
3016 IS
3017         -- local variables
3018         l_period_counter        PLS_INTEGER;
3019         l_record_type           PLS_INTEGER;
3020         l_scenario_id           PLS_INTEGER;
3021         l_level_id              PLS_INTEGER;
3022         l_return_status         PLS_INTEGER;
3023 
3024 
3025 BEGIN
3026         IF PG_DEBUG in ('Y', 'C') THEN
3027                 msc_sch_wb.atp_debug('Insert_Allocation_Details: ' ||  '*********Inside procedure Insert_Allocation_Details ********');
3028         END IF;
3029 
3030         -- Initialization section
3031         x_return_status := FND_API.G_RET_STS_SUCCESS;
3032         l_record_type   := 1;
3033         l_scenario_id   := 0;
3034 
3035         IF ((MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1)) THEN
3036                 -- Demand priority; Demand class
3037                 l_level_id := -1;
3038 
3039                 /* Insert ATP Period Information */
3040                 IF PG_DEBUG in ('Y', 'C') THEN
3041                         msc_sch_wb.atp_debug('Insert_Allocation_Details: ' ||  'Before inserting period records into the temp table for demand priority');
3042                 END IF;
3043 
3044                 -- bug 2763784 (ssurendr)
3045                 -- Honor the rounding control type
3046                 /* rajjain 02/12/2003 bug 2795992
3047                 IF G_ROUNDING_CONTROL_FLAG=1 THEN
3048                         msc_sch_wb.atp_debug('Insert_Allocation_Details: ' ||  'Will round off.');
3049                         FORALL l_period_counter IN 1..p_atp_period.Period_Start_Date.COUNT
3050                                 INSERT INTO MRP_ATP_DETAILS_TEMP
3051                                 (
3052                                         session_id,
3053                                         scenario_id,
3054                                         atp_level,
3055                                         record_type,
3056                                         inventory_item_id,
3057                                         organization_id,
3058                                         identifier1,
3059                                         demand_class,
3060                                         period_start_date,
3061                                         period_end_date,
3062                                         allocated_supply_quantity,
3063                                         supply_adjustment_quantity,
3064                                         total_supply_quantity,
3065                                         total_demand_quantity,
3066                                         period_quantity,
3067                                         backward_forward_quantity,
3068                                         cumulative_quantity,
3069                                         plan_name  -- bug 2771192
3070                                 )
3071                                 VALUES
3072                                 (
3073                                         p_session_id,
3074                                         p_atp_period.scenario_id(l_period_counter),
3075                                         p_atp_period.level(l_period_counter),
3076                                         l_record_type,
3077                                         p_atp_period.inventory_item_id(l_period_counter),
3078                                         p_atp_period.organization_id(l_period_counter),
3079                                         p_atp_period.identifier1(l_period_counter),
3080                                         p_atp_period.demand_class(l_period_counter),
3081                                         p_atp_period.period_start_date(l_period_counter),
3082                                         p_atp_period.period_end_date(l_period_counter),
3083                                         FLOOR(p_atp_period.allocated_supply_quantity(l_period_counter)),
3084                                         FLOOR(p_atp_period.supply_adjustment_quantity(l_period_counter)),
3085                                         FLOOR(p_atp_period.total_supply_quantity(l_period_counter)),
3086                                         FLOOR(p_atp_period.total_demand_quantity(l_period_counter)),
3087                                         FLOOR(p_atp_period.period_quantity(l_period_counter)),
3088                                         FLOOR(p_atp_period.backward_forward_quantity(l_period_counter)),
3089                                         FLOOR(p_atp_period.cumulative_quantity(l_period_counter)),
3090                                         p_plan_name  -- bug 2771192
3091                                 );
3092                 ELSE  -- IF G_ROUNDING_CONTROL_FLAG=1 THEN
3093                         msc_sch_wb.atp_debug('Insert_Allocation_Details: ' ||  'Will not round off.');*/
3094                         FORALL l_period_counter IN 1..p_atp_period.Period_Start_Date.COUNT
3095                                 INSERT INTO MRP_ATP_DETAILS_TEMP
3096                                 (
3097                                         session_id,
3098                                         scenario_id,
3099                                         atp_level,
3100                                         record_type,
3101                                         inventory_item_id,
3102                                         organization_id,
3103                                         identifier1,
3104                                         demand_class,
3105                                         period_start_date,
3106                                         period_end_date,
3107                                         allocated_supply_quantity,
3108                                         supply_adjustment_quantity,
3109                                         total_supply_quantity,
3110                                         total_demand_quantity,
3111                                         total_bucketed_demand_quantity, -- For time_phased_atp
3112                                         period_quantity,
3113                                         backward_forward_quantity,
3114                                         cumulative_quantity,
3115                                         plan_name,  -- bug 2771192
3116                                         aggregate_time_fence_date -- for time_phased_atp
3117                                 )
3118                                 VALUES
3119                                 (
3120                                         p_session_id,
3121                                         p_atp_period.scenario_id(l_period_counter),
3122                                         p_atp_period.level(l_period_counter),
3123                                         l_record_type,
3124                                         p_atp_period.inventory_item_id(l_period_counter),
3125                                         p_atp_period.organization_id(l_period_counter),
3126                                         p_atp_period.identifier1(l_period_counter),
3127                                         p_atp_period.demand_class(l_period_counter),
3128                                         p_atp_period.period_start_date(l_period_counter),
3129                                         p_atp_period.period_end_date(l_period_counter),
3130                                         p_atp_period.allocated_supply_quantity(l_period_counter),
3131                                         p_atp_period.supply_adjustment_quantity(l_period_counter),
3132                                         p_atp_period.total_supply_quantity(l_period_counter),
3133                                         p_atp_period.total_demand_quantity(l_period_counter),
3134                                         p_atp_period.total_bucketed_demand_quantity(l_period_counter), -- For time_phased_atp
3135                                         p_atp_period.period_quantity(l_period_counter),
3136                                         p_atp_period.backward_forward_quantity(l_period_counter),
3137                                         p_atp_period.cumulative_quantity(l_period_counter),
3138                                         p_plan_name,  -- bug 2771192
3139                                         G_ATF_DATE    -- for time_phased_atp
3140                                 );
3141                 --END IF;
3142 
3143                 IF PG_DEBUG in ('Y', 'C') THEN
3144                         msc_sch_wb.atp_debug('Insert_Allocation_Details: ' ||  'After inserting period records into the temp table');
3145                 END IF;
3146 
3147                 /* Do Totaling. In case of Demand Priority, we do Total of all demand classes.
3148 
3149                 1. We let the demand class remain and period_end_date null for Total.
3150                 2. The net of Total on any period date = Sum of net's of all the demand classes on
3151                    the period start date.
3152                 3. The cum of Total on any period date = Sum of cum's of all the demand classes
3153                    (such that period start date of Total falls between the period start date and
3154                    period end date of the demand class.)
3155                 4. If period start date is infinite time fence date, then we insert INFINITE_NUMBER for supplies
3156                    and 0 for demands.
3157                 5. If period start date is not equal to infinite time fence date, then we do sum(quantity)
3158                 */
3159 
3160 
3161                 -- Now do the summing for Total.
3162                 IF PG_DEBUG in ('Y', 'C') THEN
3163                         msc_sch_wb.atp_debug('Insert_Allocation_Details: ' ||  'Before the summing SQL for Total');
3164                 END IF;
3165                 INSERT INTO MRP_ATP_DETAILS_TEMP
3166                 (
3167                         session_id,
3168                         scenario_id,
3169                         atp_level,
3170                         record_type,
3171                         inventory_item_id,
3172                         organization_id,
3173                         identifier1,
3174                         period_start_date,
3175                         allocated_supply_quantity,
3176                         supply_adjustment_quantity,
3177                         total_supply_quantity,
3178                         --total_bucketed_demand_quantity, -- for time_phased_atp
3179                         --total_demand_quantity,
3180                         total_demand_quantity, --bug3519965
3181                         total_bucketed_demand_quantity, --bug3519965
3182                         period_quantity,
3183                         backward_forward_quantity,
3184                         cumulative_quantity,
3185                         plan_name,  -- bug 2771192
3186                         aggregate_time_fence_date -- for time_phased_atp
3187                 )
3188                 SELECT
3189                         p_session_id,
3190                         l_scenario_id,
3191                         l_level_id,
3192                         l_record_type,
3193                         p_inventory_item_id,
3194                         p_organization_id,
3195                         p_instance_id,
3196                         final.period_start_date,
3197                         final.allocated_supply_quantity,
3198                         final.supply_adjustment_quantity,
3199                         final.total_supply_quantity,
3200                         final.total_demand_quantity,
3201                         final.total_bucketed_demand_quantity, -- for time_phased_atp
3202                         final.period_quantity,
3203                         final.backward_forward_quantity,
3204                         final.cumulative_quantity,
3205                         p_plan_name,  -- bug 2771192
3206                         G_ATF_DATE    -- for time_phased_atp
3207                 FROM
3208                 (
3209                         SELECT
3210                                 mapt.period_start_date                                  period_start_date,
3211                                 DECODE(mapt.period_start_date,
3212                                         p_infinite_time_fence_date,
3213                                         MSC_ATP_PVT.INFINITE_NUMBER,
3214                                         SUM(DECODE(mapt.period_start_date,
3215                                                 madt.period_start_date,
3216                                                 madt.allocated_supply_quantity, 0)))    allocated_supply_quantity,
3217                                 DECODE(mapt.period_start_date,
3218                                         p_infinite_time_fence_date,
3219                                         0,
3220                                         SUM(DECODE(mapt.period_start_date,
3221                                                 madt.period_start_date,
3222                                                 madt.supply_adjustment_quantity, 0)))   supply_adjustment_quantity,
3223                                 DECODE(mapt.period_start_date,
3224                                         p_infinite_time_fence_date,
3225                                         MSC_ATP_PVT.INFINITE_NUMBER,
3226                                         SUM(DECODE(mapt.period_start_date,
3227                                                 madt.period_start_date,
3228                                                 madt.total_supply_quantity, 0)))        total_supply_quantity,
3229                                 DECODE(mapt.period_start_date,
3230                                         p_infinite_time_fence_date,
3231                                         0,
3232                                         SUM(DECODE(mapt.period_start_date,
3233                                                 madt.period_start_date,
3234                                                 madt.total_demand_quantity, 0)))        total_demand_quantity,
3235                                 DECODE(mapt.period_start_date,
3236                                         p_infinite_time_fence_date,
3237                                         0,
3238                                         SUM(DECODE(mapt.period_start_date,
3239                                                 madt.period_start_date,
3240                                                 --madt.total_demand_quantity, 0)))        total_bucketed_demand_quantity, -- for time_phased_atp
3241                                                 madt.total_bucketed_demand_quantity, 0)))        total_bucketed_demand_quantity, --bug3519965
3242                                 DECODE(mapt.period_start_date,
3243                                         p_infinite_time_fence_date,
3244                                         MSC_ATP_PVT.INFINITE_NUMBER,
3245                                         SUM(DECODE(mapt.period_start_date,
3246                                                 madt.period_start_date,
3247                                                 madt.period_quantity, 0)))              period_quantity,
3248                                 DECODE(mapt.period_start_date,
3249                                         p_infinite_time_fence_date,
3250                                         MSC_ATP_PVT.INFINITE_NUMBER,
3251                                         SUM(DECODE(mapt.period_start_date,
3252                                                 madt.period_start_date,
3253                                                 madt.backward_forward_quantity, 0)))    backward_forward_quantity,
3254                                 DECODE(mapt.period_start_date,
3255                                         p_infinite_time_fence_date,
3256                                         MSC_ATP_PVT.INFINITE_NUMBER,
3257                                         -- rajjain 02/13/2003 Bug 2795372
3258                                         SUM(GREATEST(madt.cumulative_quantity, 0)))     cumulative_quantity
3259                         FROM
3260                                 MRP_ATP_DETAILS_TEMP                                    madt,
3261                                 (SELECT DISTINCT(period_start_date) period_start_date
3262                                 FROM MRP_ATP_DETAILS_TEMP
3263                                 WHERE session_id = p_session_id
3264                                 AND record_type = l_record_type)                        mapt
3265                         WHERE
3266                                 madt.session_id = p_session_id
3267                                 AND madt.record_type = l_record_type
3268                                 AND mapt.period_start_date BETWEEN madt.period_start_date
3269                                 AND madt.period_end_date
3270                         GROUP BY
3271                                 mapt.period_start_date
3272                 ) final;
3273 
3274                 IF PG_DEBUG in ('Y', 'C') THEN
3275                         msc_sch_wb.atp_debug('Insert_Allocation_Details: ' ||  'After the summing SQL for Total');
3276                 END IF;
3277 
3278         ELSIF ((MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND (MSC_ATP_PVT.G_ALLOCATION_METHOD = 2)) THEN
3279                 -- IF ((MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1)) THEN
3280                 -- Rule based allocation; Demand class
3281 
3282                 -- initialize l_level_id
3283                 l_level_id := -1;
3284 
3285                 /* Insert ATP Period Information */
3286                 IF PG_DEBUG in ('Y', 'C') THEN
3287                         msc_sch_wb.atp_debug('Insert_Allocation_Details: ' ||  'Before inserting period records into the temp table for demand class ATP');
3288                 END IF;
3289 
3290                 IF G_ATP_FW_CONSUME_METHOD = 1 THEN
3291 
3292                         -- bug 2763784 (ssurendr)
3293                         -- Honor the rounding control type
3294                         /* rajjain 02/12/2003 bug 2795992
3295                         IF G_ROUNDING_CONTROL_FLAG=1 THEN
3296                                 msc_sch_wb.atp_debug('Insert_Allocation_Details: ' ||  'Will round off.');
3297                                 FORALL l_period_counter IN 1..p_atp_period.Period_Start_Date.COUNT
3298                                         INSERT INTO MRP_ATP_DETAILS_TEMP
3299                                         (
3300                                                 session_id,
3301                                                 scenario_id,
3302                                                 atp_level,
3303                                                 record_type,
3304                                                 inventory_item_id,
3305                                                 organization_id,
3306                                                 identifier1,
3307                                                 demand_class,
3308                                                 allocated_supply_quantity,
3309                                                 total_demand_quantity,
3310                                                 period_start_date,
3311                                                 period_end_date,
3312                                                 period_quantity,
3313                                                 cumulative_quantity,
3314                                                 backward_quantity,
3315                                                 demand_adjustment_quantity,
3316                                                 adjusted_availability_quantity,
3317                                                 plan_name  -- bug 2771192
3318                                         )
3319                                         VALUES
3320                                         (
3321                                                 p_session_id,
3322                                                 p_atp_period.scenario_id(l_period_counter),
3323                                                 p_atp_period.level(l_period_counter),
3324                                                 l_record_type,
3325                                                 p_atp_period.inventory_item_id(l_period_counter),
3326                                                 p_atp_period.organization_id(l_period_counter),
3327                                                 p_atp_period.identifier1(l_period_counter),
3328                                                 p_atp_period.demand_class(l_period_counter),
3329                                                 FLOOR(p_atp_period.total_supply_quantity(l_period_counter)),
3330                                                 FLOOR(p_atp_period.total_demand_quantity(l_period_counter)),
3331                                                 p_atp_period.period_start_date(l_period_counter),
3332                                                 p_atp_period.period_end_date(l_period_counter),
3333                                                 FLOOR(p_atp_period.period_quantity(l_period_counter)),
3334                                                 FLOOR(p_atp_period.cumulative_quantity(l_period_counter)),
3335                                                 FLOOR(p_atp_period.Backward_Quantity(l_period_counter)),
3336                                                 FLOOR(p_atp_period.Demand_Adjustment_Quantity(l_period_counter)),
3337                                                 FLOOR(p_atp_period.Adjusted_Availability_Quantity(l_period_counter)),
3338                                                 p_plan_name  -- bug 2771192
3339                                         );
3340                         ELSE  -- IF G_ROUNDING_CONTROL_FLAG=1 THEN
3341                                 msc_sch_wb.atp_debug('Insert_Allocation_Details: ' ||  'Will not round off.');*/
3342                                 FORALL l_period_counter IN 1..p_atp_period.Period_Start_Date.COUNT
3343                                         INSERT INTO MRP_ATP_DETAILS_TEMP
3344                                         (
3345                                                 session_id,
3346                                                 scenario_id,
3347                                                 atp_level,
3348                                                 record_type,
3349                                                 inventory_item_id,
3350                                                 organization_id,
3351                                                 identifier1,
3352                                                 demand_class,
3353                                                 allocated_supply_quantity,
3354                                                 total_demand_quantity,
3355                                                 total_bucketed_demand_quantity, -- For time_phased_atp
3356                                                 period_start_date,
3357                                                 period_end_date,
3358                                                 period_quantity,
3359                                                 cumulative_quantity,
3360                                                 backward_quantity,
3361                                                 demand_adjustment_quantity,
3362                                                 adjusted_availability_quantity,
3363                                                 plan_name,  -- bug 2771192
3364                                                 aggregate_time_fence_date -- for time_phased_atp
3365                                         )
3366                                         VALUES
3367                                         (
3368                                                 p_session_id,
3369                                                 p_atp_period.scenario_id(l_period_counter),
3370                                                 p_atp_period.level(l_period_counter),
3371                                                 l_record_type,
3372                                                 p_atp_period.inventory_item_id(l_period_counter),
3373                                                 p_atp_period.organization_id(l_period_counter),
3374                                                 p_atp_period.identifier1(l_period_counter),
3375                                                 p_atp_period.demand_class(l_period_counter),
3376                                                 p_atp_period.total_supply_quantity(l_period_counter),
3377                                                 p_atp_period.total_demand_quantity(l_period_counter),
3378                                                 p_atp_period.total_bucketed_demand_quantity(l_period_counter), -- For time_phased_atp
3379                                                 p_atp_period.period_start_date(l_period_counter),
3380                                                 p_atp_period.period_end_date(l_period_counter),
3381                                                 p_atp_period.period_quantity(l_period_counter),
3382                                                 p_atp_period.cumulative_quantity(l_period_counter),
3383                                                 p_atp_period.Backward_Quantity(l_period_counter),
3384                                                 p_atp_period.Demand_Adjustment_Quantity(l_period_counter),
3385                                                 p_atp_period.Adjusted_Availability_Quantity(l_period_counter),
3386                                                 p_plan_name,  -- bug 2771192
3387                                                 G_ATF_DATE    -- for time_phased_atp
3388                                         );
3389                         --END IF;
3390 
3391                 ELSE    -- IF G_ATP_FW_CONSUME_METHOD = 1 THEN
3392 
3393                         -- bug 2763784 (ssurendr)
3394                         -- Honor the rounding control type
3395                         /* rajjain 02/12/2003 bug 2795992
3396                         IF G_ROUNDING_CONTROL_FLAG=1 THEN
3397                                 msc_sch_wb.atp_debug('Insert_Allocation_Details: ' ||  'Will round off.');
3398                                 FORALL l_period_counter IN 1..p_atp_period.Period_Start_Date.COUNT
3399                                         INSERT INTO MRP_ATP_DETAILS_TEMP
3400                                         (
3401                                                 session_id,
3402                                                 scenario_id,
3403                                                 atp_level,
3404                                                 record_type,
3405                                                 inventory_item_id,
3406                                                 organization_id,
3407                                                 identifier1,
3408                                                 demand_class,
3409                                                 allocated_supply_quantity,
3410                                                 total_demand_quantity,
3411                                                 period_start_date,
3412                                                 period_end_date,
3413                                                 period_quantity,
3414                                                 cumulative_quantity,
3415                                                 backward_quantity,
3416                                                 demand_adjustment_quantity,
3417                                                 adjusted_availability_quantity,
3418                                                 adjusted_cum_quantity,
3419                                                 plan_name  -- bug 2771192
3420                                         )
3421                                         VALUES
3422                                         (
3423                                                 p_session_id,
3424                                                 p_atp_period.scenario_id(l_period_counter),
3425                                                 p_atp_period.level(l_period_counter),
3426                                                 l_record_type,
3427                                                 p_atp_period.inventory_item_id(l_period_counter),
3428                                                 p_atp_period.organization_id(l_period_counter),
3429                                                 p_atp_period.identifier1(l_period_counter),
3430                                                 p_atp_period.demand_class(l_period_counter),
3431                                                 FLOOR(p_atp_period.total_supply_quantity(l_period_counter)),
3432                                                 FLOOR(p_atp_period.total_demand_quantity(l_period_counter)),
3433                                                 p_atp_period.period_start_date(l_period_counter),
3434                                                 p_atp_period.period_end_date(l_period_counter),
3435                                                 FLOOR(p_atp_period.period_quantity(l_period_counter)),
3436                                                 FLOOR(p_atp_period.cumulative_quantity(l_period_counter)),
3437                                                 FLOOR(p_atp_period.Backward_Quantity(l_period_counter)),
3438                                                 FLOOR(p_atp_period.Demand_Adjustment_Quantity(l_period_counter)),
3439                                                 FLOOR(p_atp_period.Adjusted_Availability_Quantity(l_period_counter)),
3440                                                 FLOOR(p_atp_period.Adjusted_Cum_Quantity(l_period_counter)),
3441                                                 p_plan_name  -- bug 2771192
3442                                         );
3443                         ELSE -- IF G_ROUNDING_CONTROL_FLAG=1 THEN
3444                                 msc_sch_wb.atp_debug('Insert_Allocation_Details: ' ||  'Will not round off.');*/
3445                                 FORALL l_period_counter IN 1..p_atp_period.Period_Start_Date.COUNT
3446                                         INSERT INTO MRP_ATP_DETAILS_TEMP
3447                                         (
3448                                                 session_id,
3449                                                 scenario_id,
3450                                                 atp_level,
3451                                                 record_type,
3452                                                 inventory_item_id,
3453                                                 organization_id,
3454                                                 identifier1,
3455                                                 demand_class,
3456                                                 allocated_supply_quantity,
3457                                                 total_demand_quantity,
3458                                                 total_bucketed_demand_quantity, -- for time_phased_atp
3459                                                 period_start_date,
3460                                                 period_end_date,
3461                                                 period_quantity,
3462                                                 cumulative_quantity,
3463                                                 backward_quantity,
3464                                                 demand_adjustment_quantity,
3465                                                 adjusted_availability_quantity,
3466                                                 adjusted_cum_quantity,
3467                                                 plan_name,  -- bug 2771192
3468                                                 aggregate_time_fence_date -- for time_phased_atp
3469                                         )
3470                                         VALUES
3471                                         (
3472                                                 p_session_id,
3473                                                 p_atp_period.scenario_id(l_period_counter),
3474                                                 p_atp_period.level(l_period_counter),
3475                                                 l_record_type,
3476                                                 p_atp_period.inventory_item_id(l_period_counter),
3477                                                 p_atp_period.organization_id(l_period_counter),
3478                                                 p_atp_period.identifier1(l_period_counter),
3479                                                 p_atp_period.demand_class(l_period_counter),
3480                                                 p_atp_period.total_supply_quantity(l_period_counter),
3481                                                 p_atp_period.total_demand_quantity(l_period_counter),
3482                                                 p_atp_period.total_bucketed_demand_quantity(l_period_counter), -- For time_phased_atp
3483                                                 p_atp_period.period_start_date(l_period_counter),
3484                                                 p_atp_period.period_end_date(l_period_counter),
3485                                                 p_atp_period.period_quantity(l_period_counter),
3486                                                 p_atp_period.cumulative_quantity(l_period_counter),
3487                                                 p_atp_period.Backward_Quantity(l_period_counter),
3488                                                 p_atp_period.Demand_Adjustment_Quantity(l_period_counter),
3489                                                 p_atp_period.Adjusted_Availability_Quantity(l_period_counter),
3490                                                 p_atp_period.Adjusted_Cum_Quantity(l_period_counter),
3491                                                 p_plan_name,  -- bug 2771192
3492                                                 G_ATF_DATE    -- for time_phased_atp
3493                                         );
3494                         --END IF;
3495                 END IF;
3496 
3497                 IF PG_DEBUG in ('Y', 'C') THEN
3498                         msc_sch_wb.atp_debug('Insert_Allocation_Details: ' ||  'After inserting period records into the temp table');
3499                 END IF;
3500 
3501                 /* Do Totaling.
3502                   1. We let the demand class remain null for Total.
3503                   2. We do a direct total for all columns because for all period_start_date data would be existing
3504                      for all demand classes due to allocation SQL.
3505                   3. If period start date is infinite time fence date, then we insert INFINITE_NUMBER for supplies
3506                      and 0 for demands.
3507                   4. If period start date is not equal to infinite time fence date, then we do sum(quantity)
3508                 */
3509 
3510                 -- Now do the summing for Total.
3511                 IF PG_DEBUG in ('Y', 'C') THEN
3512                         msc_sch_wb.atp_debug('Insert_Allocation_Details: ' ||  'Before the summing SQL for Total');
3513                 END IF;
3514                 INSERT INTO MRP_ATP_DETAILS_TEMP
3515                 (
3516                         session_id,
3517                         scenario_id,
3518                         atp_level,
3519                         record_type,
3520                         inventory_item_id,
3521                         organization_id,
3522                         identifier1,
3523                         period_start_date,
3524                         period_end_date,
3525                         allocated_supply_quantity,
3526                         total_demand_quantity,
3527                         total_bucketed_demand_quantity, -- for time_phased_atp
3528                         period_quantity,
3529                         cumulative_quantity,
3530                         backward_quantity,
3531                         demand_adjustment_quantity,
3532                         adjusted_availability_quantity,
3533                         adjusted_cum_quantity,
3534                         plan_name,  -- bug 2771192
3535                         aggregate_time_fence_date -- for time_phased_atp
3536                 )
3537                 SELECT
3538                         p_session_id,
3539                         l_scenario_id,
3540                         l_level_id,
3541                         l_record_type,
3542                         p_inventory_item_id,
3543                         p_organization_id,
3544                         p_instance_id,
3545                         final.period_start_date,
3546                         final.period_end_date,
3547                         final.allocated_supply_quantity,
3548                         final.total_demand_quantity,
3549                         final.total_bucketed_demand_quantity, -- for time_phased_atp
3550                         final.period_quantity,
3551                         final.cumulative_quantity,
3552                         final.backward_quantity,
3553                         final.demand_adjustment_quantity,
3554                         final.adjusted_availability_quantity,
3555                         final.adjusted_cum_quantity,
3556                         p_plan_name,  -- bug 2771192
3557                         G_ATF_DATE    -- for time_phased_atp
3558                 FROM
3559                 (
3560                         SELECT
3561                                 madt.period_start_date                                  period_start_date,
3562                                 madt.period_end_date                                    period_end_date,
3563                                 DECODE(madt.period_start_date,
3564                                         p_infinite_time_fence_date,
3565                                         MSC_ATP_PVT.INFINITE_NUMBER,
3566                                         SUM(madt.allocated_supply_quantity))            allocated_supply_quantity,
3567                                 DECODE(madt.period_start_date,
3568                                         p_infinite_time_fence_date,
3569                                         0,
3570                                         SUM(madt.total_demand_quantity))                total_demand_quantity,
3571                                 DECODE(madt.period_start_date,
3572                                         p_infinite_time_fence_date,
3573                                         0,
3574                                         SUM(madt.total_bucketed_demand_quantity))       total_bucketed_demand_quantity, -- for time_phased_atp
3575                                 DECODE(madt.period_start_date,
3576                                         p_infinite_time_fence_date,
3577                                         MSC_ATP_PVT.INFINITE_NUMBER,
3578                                         SUM(madt.period_quantity))                      period_quantity,
3579                                 DECODE(madt.period_start_date,
3580                                         p_infinite_time_fence_date,
3581                                         MSC_ATP_PVT.INFINITE_NUMBER,
3582                                         SUM(madt.cumulative_quantity))                  cumulative_quantity,
3583                                 DECODE(madt.period_start_date,
3584                                         p_infinite_time_fence_date,
3585                                         MSC_ATP_PVT.INFINITE_NUMBER,
3586                                         SUM(madt.backward_quantity))                    backward_quantity,
3587                                 DECODE(madt.period_start_date,
3588                                         p_infinite_time_fence_date,
3589                                         0,
3590                                         SUM(madt.demand_adjustment_quantity))           demand_adjustment_quantity,
3591                                 DECODE(madt.period_start_date,
3592                                         p_infinite_time_fence_date,
3593                                         MSC_ATP_PVT.INFINITE_NUMBER,
3594                                         SUM(madt.adjusted_availability_quantity))       adjusted_availability_quantity,
3595                                 DECODE(madt.period_start_date,
3596                                         p_infinite_time_fence_date,
3597                                         MSC_ATP_PVT.INFINITE_NUMBER,
3598                                         SUM(madt.adjusted_cum_quantity))                adjusted_cum_quantity
3599                         FROM
3600                                 MRP_ATP_DETAILS_TEMP                                    madt
3601                         WHERE
3602                                 madt.session_id = p_session_id
3603                                 AND madt.record_type = l_record_type
3604                                 AND madt.demand_class <> G_UNALLOCATED_DC
3605                         GROUP BY
3606                                 period_start_date, period_end_date
3607                 ) final;
3608 
3609                 /* New allocation logic for time phased ATP changes begin */
3610                 IF (p_dest_inv_item_id <> p_dest_family_item_id) and (G_ATF_Date is not null) THEN
3611                         UPDATE MRP_ATP_DETAILS_TEMP madt
3612                         SET (Actual_Allocation_Percent, Allocation_Percent)=
3613                                 (SELECT mv.allocation_percent, mv.level_alloc_percent
3614                                 FROM    MSC_ITEM_HIERARCHY_MV mv
3615                                 WHERE   mv.Demand_Class = madt.Demand_Class
3616                                         AND mv.Organization_Id = p_organization_id
3617                                         AND mv.Sr_Instance_Id = p_instance_id
3618                                         AND madt.Period_Start_Date between mv.Effective_Date and mv.Disable_Date
3619                                         AND mv.Level_Id = madt.Atp_Level
3620                                         AND mv.Inventory_Item_Id = Decode(sign(trunc(madt.Period_Start_Date) - G_ATF_Date),
3621                                                                         1, p_dest_family_item_id,
3622                                                                         Decode(MSC_ATP_PVT.G_MEM_RULE_WITHIN_ATF,
3623                                                                                 'Y', p_dest_inv_item_id,
3624                                                                                 p_dest_family_item_id)))
3625                         WHERE madt.Record_Type = 1
3626                         AND   madt.Session_Id = p_session_id;
3627                 ELSE
3628                         /* Removed join with msc_system_items as part of New allocation logic for time phased ATP changes*/
3629                         UPDATE MRP_ATP_DETAILS_TEMP madt
3630                         --rajjain 02/13/2003 Bug 2795636
3631                         --SET (Allocation_Percent, Actual_Allocation_Percent)=
3632                         SET (Actual_Allocation_Percent, Allocation_Percent)=
3633                                 (SELECT mv.allocation_percent, mv.level_alloc_percent
3634                                 FROM    MSC_ITEM_HIERARCHY_MV mv
3635                                 WHERE   mv.Demand_Class = madt.Demand_Class
3636                                         AND mv.Organization_Id = p_organization_id
3637                                         AND mv.Sr_Instance_Id = p_instance_id
3638                                         AND madt.Period_Start_Date between mv.Effective_Date and mv.Disable_Date
3639                                         AND mv.Level_Id = madt.Atp_Level
3640                                         AND mv.Inventory_Item_Id = p_dest_inv_item_id)
3641                         WHERE madt.Record_Type = 1
3642                         AND   madt.Session_Id = p_session_id;
3643                 END IF;
3644                 /* New allocation logic for time phased ATP changes end */
3645 
3646                 IF PG_DEBUG in ('Y', 'C') THEN
3647                         msc_sch_wb.atp_debug('Insert_Allocation_Details: ' ||  'After the summing SQL for Total');
3648                 END IF;
3649 
3650         ELSIF ((MSC_ATP_PVT.G_HIERARCHY_PROFILE = 2) AND (MSC_ATP_PVT.G_ALLOCATION_METHOD = 2)) THEN
3651                 -- ELSIF ((MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND (MSC_ATP_PVT.G_ALLOCATION_METHOD = 2)) THEN
3652                 -- Rule based allocation; customer class
3653 
3654                 -- initialize l_level_id
3655                 l_level_id := 3;
3656 
3657                 /* Insert ATP Period Information */
3658                 IF PG_DEBUG in ('Y', 'C') THEN
3659                         msc_sch_wb.atp_debug('Insert_Allocation_Details: ' ||  'Before inserting customer site level period records into the temp table');
3660                 END IF;
3661 
3662                 IF G_ATP_FW_CONSUME_METHOD = 1 THEN
3663                         -- bug 2763784 (ssurendr)
3664                         -- Honor the rounding control type
3665                         /* rajjain 02/12/2003 bug 2795992
3666                         IF G_ROUNDING_CONTROL_FLAG=1 THEN
3667                                 msc_sch_wb.atp_debug('Insert_Allocation_Details: ' ||  'Will round off.');
3668                                 FORALL l_period_counter IN 1..p_atp_period.Period_Start_Date.COUNT
3669                                         INSERT INTO MRP_ATP_DETAILS_TEMP
3670                                         (
3671                                                 session_id,
3672                                                 scenario_id,
3673                                                 atp_level,
3674                                                 record_type,
3675                                                 inventory_item_id,
3676                                                 organization_id,
3677                                                 identifier1,
3678                                                 demand_class,
3679                                                 allocated_supply_quantity,
3680                                                 total_demand_quantity,
3681                                                 period_start_date,
3682                                                 period_end_date,
3683                                                 period_quantity,
3684                                                 cumulative_quantity,
3685                                                 backward_quantity,
3686                                                 demand_adjustment_quantity,
3687                                                 adjusted_availability_quantity,
3688                                                 class,
3689                                                 customer_id,
3690                                                 customer_site_id,
3691                                                 plan_name  -- bug 2771192
3692                                         )
3693                                         VALUES
3694                                         (
3695                                                 p_session_id,
3696                                                 p_atp_period.scenario_id(l_period_counter),
3697                                                 p_atp_period.level(l_period_counter),
3698                                                 l_record_type,
3699                                                 p_atp_period.inventory_item_id(l_period_counter),
3700                                                 p_atp_period.organization_id(l_period_counter),
3701                                                 p_atp_period.identifier1(l_period_counter),
3702                                                 p_atp_period.demand_class(l_period_counter),
3703                                                 FLOOR(p_atp_period.total_supply_quantity(l_period_counter)),
3704                                                 FLOOR(p_atp_period.total_demand_quantity(l_period_counter)),
3705                                                 p_atp_period.period_start_date(l_period_counter),
3706                                                 p_atp_period.period_end_date(l_period_counter),
3707                                                 FLOOR(p_atp_period.period_quantity(l_period_counter)),
3708                                                 FLOOR(p_atp_period.cumulative_quantity(l_period_counter)),
3709                                                 FLOOR(p_atp_period.Backward_Quantity(l_period_counter)),
3710                                                 FLOOR(p_atp_period.Demand_Adjustment_Quantity(l_period_counter)),
3711                                                 FLOOR(p_atp_period.Adjusted_Availability_Quantity(l_period_counter)),
3712                                                 p_atp_period.Class(l_period_counter),
3713                                                 p_atp_period.Customer_Id(l_period_counter),
3714                                                 p_atp_period.Customer_Site_Id(l_period_counter),
3715                                                 p_plan_name  -- bug 2771192
3716                                         );
3717                         ELSE  -- IF G_ROUNDING_CONTROL_FLAG=1 THEN
3718                                 msc_sch_wb.atp_debug('Insert_Allocation_Details: ' ||  'Will not round off.');*/
3719                                 FORALL l_period_counter IN 1..p_atp_period.Period_Start_Date.COUNT
3720                                         INSERT INTO MRP_ATP_DETAILS_TEMP
3721                                         (
3722                                                 session_id,
3723                                                 scenario_id,
3724                                                 atp_level,
3725                                                 record_type,
3726                                                 inventory_item_id,
3727                                                 organization_id,
3728                                                 identifier1,
3729                                                 demand_class,
3730                                                 allocated_supply_quantity,
3731                                                 total_demand_quantity,
3732                                                 total_bucketed_demand_quantity, -- for time_phased_atp
3733                                                 period_start_date,
3734                                                 period_end_date,
3735                                                 period_quantity,
3736                                                 cumulative_quantity,
3737                                                 backward_quantity,
3738                                                 demand_adjustment_quantity,
3739                                                 adjusted_availability_quantity,
3740                                                 class,
3741                                                 customer_id,
3742                                                 customer_site_id,
3743                                                 plan_name,  -- bug 2771192
3744                                                 aggregate_time_fence_date -- for time_phased_atp
3745                                         )
3746                                         VALUES
3747                                         (
3748                                                 p_session_id,
3749                                                 p_atp_period.scenario_id(l_period_counter),
3750                                                 p_atp_period.level(l_period_counter),
3751                                                 l_record_type,
3752                                                 p_atp_period.inventory_item_id(l_period_counter),
3753                                                 p_atp_period.organization_id(l_period_counter),
3754                                                 p_atp_period.identifier1(l_period_counter),
3755                                                 p_atp_period.demand_class(l_period_counter),
3756                                                 p_atp_period.total_supply_quantity(l_period_counter),
3757                                                 p_atp_period.total_demand_quantity(l_period_counter),
3758                                                 p_atp_period.total_bucketed_demand_quantity(l_period_counter), -- For time_phased_atp
3759                                                 p_atp_period.period_start_date(l_period_counter),
3760                                                 p_atp_period.period_end_date(l_period_counter),
3761                                                 p_atp_period.period_quantity(l_period_counter),
3762                                                 p_atp_period.cumulative_quantity(l_period_counter),
3763                                                 p_atp_period.Backward_Quantity(l_period_counter),
3764                                                 p_atp_period.Demand_Adjustment_Quantity(l_period_counter),
3765                                                 p_atp_period.Adjusted_Availability_Quantity(l_period_counter),
3766                                                 p_atp_period.Class(l_period_counter),
3767                                                 p_atp_period.Customer_Id(l_period_counter),
3768                                                 p_atp_period.Customer_Site_Id(l_period_counter),
3769                                                 p_plan_name,  -- bug 2771192
3770                                                 G_ATF_DATE    -- for time_phased_atp
3771                                         );
3772                         --END IF;
3773 
3774                 ELSE
3775 
3776                         -- bug 2763784 (ssurendr)
3777                         -- Honor the rounding control type
3778                         /* rajjain 02/12/2003 bug 2795992
3779                         IF G_ROUNDING_CONTROL_FLAG=1 THEN
3780                                 msc_sch_wb.atp_debug('Insert_Allocation_Details: ' ||  'Will round off.');
3781                                 FORALL l_period_counter IN 1..p_atp_period.Period_Start_Date.COUNT
3782                                         INSERT INTO MRP_ATP_DETAILS_TEMP
3783                                         (
3784                                                 session_id,
3785                                                 scenario_id,
3786                                                 atp_level,
3787                                                 record_type,
3788                                                 inventory_item_id,
3789                                                 organization_id,
3790                                                 identifier1,
3791                                                 demand_class,
3792                                                 allocated_supply_quantity,
3793                                                 total_demand_quantity,
3794                                                 period_start_date,
3795                                                 period_end_date,
3796                                                 period_quantity,
3797                                                 cumulative_quantity,
3798                                                 backward_quantity,
3799                                                 demand_adjustment_quantity,
3800                                                 adjusted_availability_quantity,
3801                                                 adjusted_cum_quantity,
3802                                                 class,
3803                                                 customer_id,
3804                                                 customer_site_id,
3805                                                 plan_name  -- bug 2771192
3806                                         )
3807                                         VALUES
3808                                         (
3809                                                 p_session_id,
3810                                                 p_atp_period.scenario_id(l_period_counter),
3811                                                 p_atp_period.level(l_period_counter),
3812                                                 l_record_type,
3813                                                 p_atp_period.inventory_item_id(l_period_counter),
3814                                                 p_atp_period.organization_id(l_period_counter),
3815                                                 p_atp_period.identifier1(l_period_counter),
3816                                                 p_atp_period.demand_class(l_period_counter),
3817                                                 FLOOR(p_atp_period.total_supply_quantity(l_period_counter)),
3818                                                 FLOOR(p_atp_period.total_demand_quantity(l_period_counter)),
3819                                                 p_atp_period.period_start_date(l_period_counter),
3820                                                 p_atp_period.period_end_date(l_period_counter),
3821                                                 FLOOR(p_atp_period.period_quantity(l_period_counter)),
3822                                                 FLOOR(p_atp_period.cumulative_quantity(l_period_counter)),
3823                                                 FLOOR(p_atp_period.Backward_Quantity(l_period_counter)),
3824                                                 FLOOR(p_atp_period.Demand_Adjustment_Quantity(l_period_counter)),
3825                                                 FLOOR(p_atp_period.Adjusted_Availability_Quantity(l_period_counter)),
3826                                                 FLOOR(p_atp_period.Adjusted_Cum_Quantity(l_period_counter)),
3827                                                 p_atp_period.Class(l_period_counter),
3828                                                 p_atp_period.Customer_Id(l_period_counter),
3829                                                 p_atp_period.Customer_Site_Id(l_period_counter),
3830                                                 p_plan_name  -- bug 2771192
3831                                         );
3832                         ELSE -- IF G_ROUNDING_CONTROL_FLAG=1 THEN
3833                                 msc_sch_wb.atp_debug('Insert_Allocation_Details: ' ||  'Will not round off.');*/
3834                                 FORALL l_period_counter IN 1..p_atp_period.Period_Start_Date.COUNT
3835                                         INSERT INTO MRP_ATP_DETAILS_TEMP
3836                                         (
3837                                                 session_id,
3838                                                 scenario_id,
3839                                                 atp_level,
3840                                                 record_type,
3841                                                 inventory_item_id,
3842                                                 organization_id,
3843                                                 identifier1,
3844                                                 demand_class,
3845                                                 allocated_supply_quantity,
3846                                                 total_demand_quantity,
3847                                                 total_bucketed_demand_quantity, -- for time_phased_atp
3848                                                 period_start_date,
3849                                                 period_end_date,
3850                                                 period_quantity,
3851                                                 cumulative_quantity,
3852                                                 backward_quantity,
3853                                                 demand_adjustment_quantity,
3854                                                 adjusted_availability_quantity,
3855                                                 adjusted_cum_quantity,
3856                                                 class,
3857                                                 customer_id,
3858                                                 customer_site_id,
3859                                                 plan_name,  -- bug 2771192
3860                                                 aggregate_time_fence_date -- for time_phased_atp
3861                                         )
3862                                         VALUES
3863                                         (
3864                                                 p_session_id,
3865                                                 p_atp_period.scenario_id(l_period_counter),
3866                                                 p_atp_period.level(l_period_counter),
3867                                                 l_record_type,
3868                                                 p_atp_period.inventory_item_id(l_period_counter),
3869                                                 p_atp_period.organization_id(l_period_counter),
3870                                                 p_atp_period.identifier1(l_period_counter),
3871                                                 p_atp_period.demand_class(l_period_counter),
3872                                                 p_atp_period.total_supply_quantity(l_period_counter),
3873                                                 p_atp_period.total_demand_quantity(l_period_counter),
3874                                                 p_atp_period.total_bucketed_demand_quantity(l_period_counter), -- For time_phased_atp
3875                                                 p_atp_period.period_start_date(l_period_counter),
3876                                                 p_atp_period.period_end_date(l_period_counter),
3877                                                 p_atp_period.period_quantity(l_period_counter),
3878                                                 p_atp_period.cumulative_quantity(l_period_counter),
3879                                                 p_atp_period.Backward_Quantity(l_period_counter),
3880                                                 p_atp_period.Demand_Adjustment_Quantity(l_period_counter),
3881                                                 p_atp_period.Adjusted_Availability_Quantity(l_period_counter),
3882                                                 p_atp_period.Adjusted_Cum_Quantity(l_period_counter),
3883                                                 p_atp_period.Class(l_period_counter),
3884                                                 p_atp_period.Customer_Id(l_period_counter),
3885                                                 p_atp_period.Customer_Site_Id(l_period_counter),
3886                                                 p_plan_name,  -- bug 2771192
3887                                                 G_ATF_DATE    -- for time_phased_atp
3888                                         );
3889                         --END IF;
3890 
3891                 END IF;
3892 
3893                 IF PG_DEBUG in ('Y', 'C') THEN
3894                         msc_sch_wb.atp_debug('Insert_Allocation_Details: ' ||  'After inserting customer site level period records into the temp table');
3895                 END IF;
3896 
3897                 /* Do sub-Total for level 2 records.
3898                 1. We let demand_class be null.
3899                 2. We do a direct sum for all columns because data for all sites will be present for all dates.
3900                 3. If period start date is infinite time fence date, then we insert INFINITE_NUMBER for supply
3901                    columns and 0 for demand columns.
3902                 4. If period start date is not equal to infinite time fence date, then we do sum(quantity)
3903                 5. The grouping is done on class, customer_id, period_start_date and period_end_date
3904                 */
3905 
3906                 IF PG_DEBUG in ('Y', 'C') THEN
3907                         msc_sch_wb.atp_debug('Insert_Allocation_Details: ' ||  'Before the summing SQL for customer level');
3908                 END IF;
3909                 INSERT INTO MRP_ATP_DETAILS_TEMP
3910                 (
3911                         session_id,
3912                         scenario_id,
3913                         atp_level,
3914                         record_type,
3915                         inventory_item_id,
3916                         organization_id,
3917                         identifier1,
3918                         period_start_date,
3919                         period_end_date,
3920                         allocated_supply_quantity,
3921                         total_demand_quantity,
3922                         total_bucketed_demand_quantity, -- for time_phased_atp
3923                         period_quantity,
3924                         cumulative_quantity,
3925                         backward_quantity,
3926                         demand_adjustment_quantity,
3927                         adjusted_availability_quantity,
3928                         adjusted_cum_quantity,
3929                         class,
3930                         customer_id,
3931                         plan_name,  -- bug 2771192
3932                         aggregate_time_fence_date -- for time_phased_atp
3933                 )
3934                 SELECT
3935                         p_session_id,
3936                         l_scenario_id,
3937                         final.level_id,
3938                         l_record_type,
3939                         p_inventory_item_id,
3940                         p_organization_id,
3941                         p_instance_id,
3942                         final.period_start_date,
3943                         final.period_end_date,
3944                         final.allocated_supply_quantity,
3945                         final.total_demand_quantity,
3946                         final.total_bucketed_demand_quantity, -- for time_phased_atp
3947                         final.period_quantity,
3948                         final.cumulative_quantity,
3949                         final.backward_quantity,
3950                         final.demand_adjustment_quantity,
3951                         final.adjusted_availability_quantity,
3952                         final.adjusted_cum_quantity,
3953                         final.class,
3954                         final.customer_id,
3955                         p_plan_name,  -- bug 2771192
3956                         G_ATF_DATE    -- for time_phased_atp
3957                 FROM
3958                 (
3959                         SELECT
3960                                 madt.class                                              class,
3961                                 madt.customer_id                                        customer_id,
3962                                 2                                                       level_id,
3963                                 madt.period_start_date                                  period_start_date,
3964                                 madt.period_end_date                                    period_end_date,
3965                                 DECODE(madt.period_start_date,
3966                                         p_infinite_time_fence_date,
3967                                         MSC_ATP_PVT.INFINITE_NUMBER,
3968                                         SUM(madt.allocated_supply_quantity))            allocated_supply_quantity,
3969                                 DECODE(madt.period_start_date,
3970                                         p_infinite_time_fence_date,
3971                                         0,
3972                                         SUM(madt.total_demand_quantity))                total_demand_quantity,
3973                                 DECODE(madt.period_start_date,
3974                                         p_infinite_time_fence_date,
3975                                         0,
3976                                         SUM(madt.total_bucketed_demand_quantity))       total_bucketed_demand_quantity, -- for time_phased_atp
3977                                 DECODE(madt.period_start_date,
3978                                         p_infinite_time_fence_date,
3979                                         MSC_ATP_PVT.INFINITE_NUMBER,
3980                                         SUM(madt.period_quantity))                      period_quantity,
3981                                 DECODE(madt.period_start_date,
3982                                         p_infinite_time_fence_date,
3983                                         MSC_ATP_PVT.INFINITE_NUMBER,
3984                                         SUM(madt.cumulative_quantity))                  cumulative_quantity,
3985                                 DECODE(madt.period_start_date,
3986                                         p_infinite_time_fence_date,
3987                                         MSC_ATP_PVT.INFINITE_NUMBER,
3988                                         SUM(madt.backward_quantity))                    backward_quantity,
3989                                 DECODE(madt.period_start_date,
3990                                         p_infinite_time_fence_date,
3991                                         0,
3992                                         SUM(madt.demand_adjustment_quantity))           demand_adjustment_quantity,
3993                                 DECODE(madt.period_start_date,
3994                                         p_infinite_time_fence_date,
3995                                         MSC_ATP_PVT.INFINITE_NUMBER,
3996                                         SUM(madt.adjusted_availability_quantity))       adjusted_availability_quantity,
3997                                 DECODE(madt.period_start_date,
3998                                         p_infinite_time_fence_date,
3999                                         MSC_ATP_PVT.INFINITE_NUMBER,
4000                                         SUM(madt.adjusted_cum_quantity))                adjusted_cum_quantity
4001                         FROM
4002                                 MRP_ATP_DETAILS_TEMP                                    madt
4003                         WHERE
4004                                 madt.session_id = p_session_id
4005                                 AND madt.record_type = l_record_type
4006                                 AND madt.ATP_Level = 3
4007                                 AND madt.demand_class <> G_UNALLOCATED_DC
4008                         GROUP BY
4009                                 class, customer_id, period_start_date, period_end_date
4010                 ) final;
4011 
4012                 IF (SQL%ROWCOUNT = 0) THEN
4013                         IF PG_DEBUG in ('Y', 'C') THEN
4014                                 msc_sch_wb.atp_debug('Insert_Allocation_Details: ' ||  'Error occured while doing sub-Total for customer level');
4015                         END IF;
4016                         Set_Error(MSC_ATP_PVT.ATP_PROCESSING_ERROR);
4017                         x_return_status := FND_API.G_RET_STS_ERROR;
4018                         return;
4019                 END IF;
4020 
4021                 IF PG_DEBUG in ('Y', 'C') THEN
4022                         msc_sch_wb.atp_debug('Insert_Allocation_Details: ' ||  'After the summing SQL for Customer level');
4023                 END IF;
4024 
4025                 /* Do sub-Total for level 1 records.
4026                 1. We let demand_class be null.
4027                 2. We do sum over the customer level records inserted by the earlier SQL.
4028                 3. We do a direct sum for all columns because data for all customers will be present for all dates.
4029                 4. If period start date is infinite time fence date, then we insert INFINITE_NUMBER for supply
4030                    columns and 0 for demand columns.
4031                 5. If period start date is not equal to infinite time fence date, then we do sum(quantity)
4032                 6. The grouping is done on class, period_start_date and period_end_date
4033                 */
4034 
4035                 IF PG_DEBUG in ('Y', 'C') THEN
4036                         msc_sch_wb.atp_debug('Insert_Allocation_Details: ' ||  'Before the summing SQL for customer class level');
4037                 END IF;
4038 
4039                 INSERT INTO MRP_ATP_DETAILS_TEMP
4040                 (
4041                         session_id,
4042                         scenario_id,
4043                         atp_level,
4044                         record_type,
4045                         inventory_item_id,
4046                         organization_id,
4047                         identifier1,
4048                         period_start_date,
4049                         period_end_date,
4050                         allocated_supply_quantity,
4051                         total_demand_quantity,
4052                         total_bucketed_demand_quantity, -- for time_phased_atp
4053                         period_quantity,
4054                         cumulative_quantity,
4055                         backward_quantity,
4056                         demand_adjustment_quantity,
4057                         adjusted_availability_quantity,
4058                         adjusted_cum_quantity,
4059                         class,
4060                         plan_name,  -- bug 2771192
4061                         aggregate_time_fence_date -- for time_phased_atp
4062                 )
4063                 SELECT
4064                         p_session_id,
4065                         l_scenario_id,
4066                         final.level_id,
4067                         l_record_type,
4068                         p_inventory_item_id,
4069                         p_organization_id,
4070                         p_instance_id,
4071                         final.period_start_date,
4072                         final.period_end_date,
4073                         final.allocated_supply_quantity,
4074                         final.total_demand_quantity,
4075                         final.total_bucketed_demand_quantity, -- for time_phased_atp
4076                         final.period_quantity,
4077                         final.cumulative_quantity,
4078                         final.backward_quantity,
4079                         final.demand_adjustment_quantity,
4080                         final.adjusted_availability_quantity,
4081                         final.adjusted_cum_quantity,
4082                         final.class,
4083                         p_plan_name,  -- bug 2771192
4084                         G_ATF_DATE    -- for time_phased_atp
4085                 FROM
4086                 (
4087                         SELECT
4088                                 madt.class                                              class,
4089                                 1                                                       level_id,
4090                                 madt.period_start_date                                  period_start_date,
4091                                 madt.period_end_date                                    period_end_date,
4092                                 DECODE(madt.period_start_date,
4093                                         p_infinite_time_fence_date,
4094                                         MSC_ATP_PVT.INFINITE_NUMBER,
4095                                         SUM(madt.allocated_supply_quantity))            allocated_supply_quantity,
4096                                 DECODE(madt.period_start_date,
4097                                         p_infinite_time_fence_date,
4098                                         0,
4099                                         SUM(madt.total_demand_quantity))                total_demand_quantity,
4100                                 DECODE(madt.period_start_date,
4101                                         p_infinite_time_fence_date,
4102                                         0,
4103                                         SUM(madt.total_bucketed_demand_quantity))       total_bucketed_demand_quantity, -- for time_phased_atp
4104                                 DECODE(madt.period_start_date,
4105                                         p_infinite_time_fence_date,
4106                                         MSC_ATP_PVT.INFINITE_NUMBER,
4107                                         SUM(madt.period_quantity))                      period_quantity,
4108                                 DECODE(madt.period_start_date,
4109                                         p_infinite_time_fence_date,
4110                                         MSC_ATP_PVT.INFINITE_NUMBER,
4111                                         SUM(madt.cumulative_quantity))                  cumulative_quantity,
4112                                 DECODE(madt.period_start_date,
4113                                         p_infinite_time_fence_date,
4114                                         MSC_ATP_PVT.INFINITE_NUMBER,
4115                                         SUM(madt.backward_quantity))                    backward_quantity,
4116                                 DECODE(madt.period_start_date,
4117                                         p_infinite_time_fence_date,
4118                                         0,
4119                                         SUM(madt.demand_adjustment_quantity))           demand_adjustment_quantity,
4120                                 DECODE(madt.period_start_date,
4121                                         p_infinite_time_fence_date,
4122                                         MSC_ATP_PVT.INFINITE_NUMBER,
4123                                         SUM(madt.adjusted_availability_quantity))       adjusted_availability_quantity,
4124                                 DECODE(madt.period_start_date,
4125                                         p_infinite_time_fence_date,
4126                                         MSC_ATP_PVT.INFINITE_NUMBER,
4127                                         SUM(madt.adjusted_cum_quantity))                adjusted_cum_quantity
4128                         FROM
4129                                 MRP_ATP_DETAILS_TEMP                                    madt
4130                         WHERE
4131                                 madt.session_id = p_session_id
4132                                 AND madt.record_type = l_record_type
4133                                 AND madt.ATP_Level = 2
4134                         GROUP BY
4135                                 class, period_start_date, period_end_date
4136                 ) final;
4137 
4138                 IF (SQL%ROWCOUNT = 0) THEN
4139                         IF PG_DEBUG in ('Y', 'C') THEN
4140                                 msc_sch_wb.atp_debug('Insert_Allocation_Details: ' ||  'Error occured while doing sub-Total for customer class level');
4141                         END IF;
4142                         Set_Error(MSC_ATP_PVT.ATP_PROCESSING_ERROR);
4143                         x_return_status := FND_API.G_RET_STS_ERROR;
4144                         return;
4145                 END IF;
4146 
4147                 IF PG_DEBUG in ('Y', 'C') THEN
4148                         msc_sch_wb.atp_debug('Insert_Allocation_Details: ' ||  'After the summing SQL for Customer class level');
4149                 END IF;
4150 
4151                 /* Do Total for level -1 records (grand total).
4152                 1. We let demand_class be null.
4153                 2. We do sum over the customer class level records inserted by the earlier SQL.
4154                 3. We do a direct sum for all columns because data for all classes will be present for all dates.
4155                 4. If period start date is infinite time fence date, then we insert INFINITE_NUMBER for supply
4156                    columns and 0 for demand columns.
4157                 5. If period start date is not equal to infinite time fence date, then we do sum(quantity)
4158                 6. The grouping is done on period_start_date and period_end_date
4159                 */
4160 
4161                 IF PG_DEBUG in ('Y', 'C') THEN
4162                         msc_sch_wb.atp_debug('Insert_Allocation_Details: ' ||  'Before the summing SQL at grand total level');
4163                 END IF;
4164 
4165                 INSERT INTO MRP_ATP_DETAILS_TEMP
4166                 (
4167                         session_id,
4168                         scenario_id,
4169                         atp_level,
4170                         record_type,
4171                         inventory_item_id,
4172                         organization_id,
4173                         identifier1,
4174                         period_start_date,
4175                         period_end_date,
4176                         allocated_supply_quantity,
4177                         total_demand_quantity,
4178                         total_bucketed_demand_quantity, -- for time_phased_atp
4179                         period_quantity,
4180                         cumulative_quantity,
4181                         backward_quantity,
4182                         demand_adjustment_quantity,
4183                         adjusted_availability_quantity,
4184                         adjusted_cum_quantity,
4185                         plan_name,  -- bug 2771192
4186                         aggregate_time_fence_date -- for time_phased_atp
4187                 )
4188                 SELECT
4189                         p_session_id,
4190                         l_scenario_id,
4191                         final.level_id,
4192                         l_record_type,
4193                         p_inventory_item_id,
4194                         p_organization_id,
4195                         p_instance_id,
4196                         final.period_start_date,
4197                         final.period_end_date,
4198                         final.allocated_supply_quantity,
4199                         final.total_demand_quantity,
4200                         final.total_bucketed_demand_quantity, -- for time_phased_atp
4201                         final.period_quantity,
4202                         final.cumulative_quantity,
4203                         final.backward_quantity,
4204                         final.demand_adjustment_quantity,
4205                         final.adjusted_availability_quantity,
4206                         final.adjusted_cum_quantity,
4207                         p_plan_name,  -- bug 2771192
4208                         G_ATF_DATE    -- for time_phased_atp
4209                 FROM
4210                 (
4211                         SELECT
4212                                 -1                                                      level_id,
4213                                 madt.period_start_date                                  period_start_date,
4214                                 madt.period_end_date                                    period_end_date,
4215                                 DECODE(madt.period_start_date,
4216                                         p_infinite_time_fence_date,
4217                                         MSC_ATP_PVT.INFINITE_NUMBER,
4218                                         SUM(madt.allocated_supply_quantity))            allocated_supply_quantity,
4219                                 DECODE(madt.period_start_date,
4220                                         p_infinite_time_fence_date,
4221                                         0,
4222                                         SUM(madt.total_demand_quantity))                total_demand_quantity,
4223                                 DECODE(madt.period_start_date,
4224                                         p_infinite_time_fence_date,
4225                                         0,
4226                                         SUM(madt.total_bucketed_demand_quantity))       total_bucketed_demand_quantity, -- for time_phased_atp
4227                                 DECODE(madt.period_start_date,
4228                                         p_infinite_time_fence_date,
4229                                         MSC_ATP_PVT.INFINITE_NUMBER,
4230                                         SUM(madt.period_quantity))                      period_quantity,
4231                                 DECODE(madt.period_start_date,
4232                                         p_infinite_time_fence_date,
4233                                         MSC_ATP_PVT.INFINITE_NUMBER,
4234                                         SUM(madt.cumulative_quantity))                  cumulative_quantity,
4235                                 DECODE(madt.period_start_date,
4236                                         p_infinite_time_fence_date,
4237                                         MSC_ATP_PVT.INFINITE_NUMBER,
4238                                         SUM(madt.backward_quantity))                    backward_quantity,
4239                                 DECODE(madt.period_start_date,
4240                                         p_infinite_time_fence_date,
4241                                         0,
4242                                         SUM(madt.demand_adjustment_quantity))           demand_adjustment_quantity,
4243                                 DECODE(madt.period_start_date,
4244                                         p_infinite_time_fence_date,
4245                                         MSC_ATP_PVT.INFINITE_NUMBER,
4246                                         SUM(madt.adjusted_availability_quantity))       adjusted_availability_quantity,
4247                                 DECODE(madt.period_start_date,
4248                                         p_infinite_time_fence_date,
4249                                         MSC_ATP_PVT.INFINITE_NUMBER,
4250                                         SUM(madt.adjusted_cum_quantity))                adjusted_cum_quantity
4251                         FROM
4252                                 MRP_ATP_DETAILS_TEMP                                    madt
4253                         WHERE
4254                                 madt.session_id = p_session_id
4255                                 AND madt.record_type = l_record_type
4256                                 AND madt.ATP_Level = 1
4257                         GROUP BY
4258                                 period_start_date, period_end_date
4259                 ) final;
4260 
4261                 IF (SQL%ROWCOUNT = 0) THEN
4262                         IF PG_DEBUG in ('Y', 'C') THEN
4263                                 msc_sch_wb.atp_debug('Insert_Allocation_Details: ' ||  'Error occured while doing grand-Total for customer class case');
4264                         END IF;
4265                         Set_Error(MSC_ATP_PVT.ATP_PROCESSING_ERROR);
4266                         x_return_status := FND_API.G_RET_STS_ERROR;
4267                         return;
4268                 END IF;
4269 
4270                 IF PG_DEBUG in ('Y', 'C') THEN
4271                         msc_sch_wb.atp_debug('Insert_Allocation_Details: ' ||  'After the summing SQL at grand total level level');
4272                 END IF;
4273 
4274                 /* New allocation logic for time phased ATP changes begin */
4275                 IF (p_dest_inv_item_id <> p_dest_family_item_id) and (G_ATF_Date is not null) THEN
4276                         UPDATE MRP_ATP_DETAILS_TEMP madt
4277                         --rajjain 02/13/2003 Bug 2795636
4278                         --SET (Allocation_Percent, Actual_Allocation_Percent)=
4279                         SET (Actual_Allocation_Percent, Allocation_Percent)=
4280                                 (SELECT mv.allocation_percent, mv.level_alloc_percent
4281                                 FROM    MSC_ITEM_HIERARCHY_MV mv
4282                                 WHERE   mv.Class = madt.Class
4283                                         AND nvl(mv.Partner_Id, -23453) = nvl(madt.Customer_Id, -23453)
4284                                         AND nvl(mv.Partner_Site_Id, -23453) = nvl(madt.Customer_Site_Id, -23453)
4285                                         AND mv.Organization_Id = p_organization_id
4286                                         AND mv.Sr_Instance_Id = p_instance_id
4287                                         AND madt.Period_Start_Date between mv.Effective_Date and mv.Disable_Date
4288                                         AND mv.Level_Id = madt.Atp_Level
4289                                         AND mv.Inventory_Item_Id = Decode(sign(trunc(madt.Period_Start_Date) - G_ATF_Date),
4290                                                                         1, p_dest_family_item_id,
4291                                                                         Decode(MSC_ATP_PVT.G_MEM_RULE_WITHIN_ATF,
4292                                                                                 'Y', p_dest_inv_item_id,
4293                                                                                 p_dest_family_item_id)))
4294                         WHERE madt.Record_Type = 1
4295                         AND   madt.Session_Id = p_session_id;
4296                 ELSE
4297                         /* Removed join with msc_system_items as part of New allocation logic for time phased ATP changes*/
4298                         UPDATE MRP_ATP_DETAILS_TEMP madt
4299                         --rajjain 02/13/2003 Bug 2795636
4300                         --SET (Allocation_Percent, Actual_Allocation_Percent)=
4301                         SET (Actual_Allocation_Percent, Allocation_Percent)=
4302                                 (SELECT mv.allocation_percent, mv.level_alloc_percent
4303                                 FROM    MSC_ITEM_HIERARCHY_MV mv
4304                                 WHERE   mv.Class = madt.Class
4305                                         AND nvl(mv.Partner_Id, -23453) = nvl(madt.Customer_Id, -23453)
4306                                         AND nvl(mv.Partner_Site_Id, -23453) = nvl(madt.Customer_Site_Id, -23453)
4307                                         AND mv.Organization_Id = p_organization_id
4308                                         AND mv.Sr_Instance_Id = p_instance_id
4309                                         AND madt.Period_Start_Date between mv.Effective_Date and mv.Disable_Date
4310                                         AND mv.Level_Id = madt.Atp_Level
4311                                         AND mv.Inventory_Item_Id = p_dest_inv_item_id)
4312                         WHERE madt.Record_Type = 1
4313                         AND   madt.Session_Id = p_session_id;
4314                 END IF;
4315                 /* New allocation logic for time phased ATP changes end */
4316 
4317                 IF PG_DEBUG in ('Y', 'C') THEN
4318                         msc_sch_wb.atp_debug('Insert_Allocation_Details: ' ||  'After the summing for Total for customer class allocation');
4319                 END IF;
4320 
4321         END IF; -- Customer Class Allocated ATP
4322 
4323         IF PG_DEBUG in ('Y', 'C') THEN
4324                 msc_sch_wb.atp_debug('Insert_Allocation_Details: ' ||  '*********End of procedure Insert_Allocation_Details ********');
4325         END IF;
4326 
4327 EXCEPTION
4328 WHEN OTHERS THEN
4329         IF PG_DEBUG in ('Y', 'C') THEN
4330                 msc_sch_wb.atp_debug('Insert_Allocation_Details: ' || 'Error in Insert_Allocation_Details: ' || to_char(sqlcode) || ':' || SQLERRM);
4331         END IF;
4332         x_return_status := FND_API.G_RET_STS_ERROR;
4333         Set_Error(MSC_ATP_PVT.ATP_PROCESSING_ERROR);
4334 
4335 END Insert_allocation_Details;
4336 
4337 /*--Refresh_Allocation_Details----------------------------------------------
4338 |  o This procedure will be called when engine is called in concurrent
4339 |    program mode when user refreshes the allocation horizontal picture in
4340 |    allocation workbench.
4341 |  o It makes a call to view_allocation_details.
4342 +-------------------------------------------------------------------------*/
4343 PROCEDURE Refresh_Allocation_Details(
4344         ERRBUF                  OUT     NOCOPY  VARCHAR2,
4345         RETCODE                 OUT     NOCOPY  NUMBER,
4346         p_session_id            IN              NUMBER,
4347         p_inventory_item_id     IN              NUMBER,
4348         p_instance_id           IN              NUMBER,
4349         p_organization_id       IN              NUMBER)
4350 IS
4351         l_return_status VARCHAR2(1);
4352         l_spid          VARCHAR2(12);
4353         l_error_meaning VARCHAR2(100);
4354 
4355         cursor Error_Meaning (p_error_code NUMBER) IS
4356         select meaning
4357         from   mfg_lookups
4358         where  lookup_type = 'MTL_DEMAND_INTERFACE_ERRORS'
4359         and    lookup_code = p_error_code;
4360 
4361 BEGIN
4362         G_REFRESH_ALLOCATION := true;
4363 
4364         -- Bug 3304390 Disable Trace
4365         -- Deleted Related Code.
4366 
4367         RETCODE:= G_SUCCESS;
4368 
4369         IF PG_DEBUG in ('Y', 'C') THEN
4370                 msc_sch_wb.atp_debug('Refresh_Allocation_Details: ' || 'Begin Refresh_Allocation_Details');
4371                 msc_sch_wb.atp_debug('Refresh_Allocation_Details: ' || 'PG_DEBUG := ' || PG_DEBUG);
4372         END IF;
4373 
4374         -- Call View_Allocation_Details
4375         MSC_ATP_ALLOC.View_Allocation_Details(p_session_id,
4376                 p_inventory_item_id,
4377                 p_instance_id,
4378                 p_organization_id,
4379                 l_return_status);
4380 
4381         IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
4382                 IF PG_DEBUG in ('Y', 'C') THEN
4383                         msc_sch_wb.atp_debug('Refresh_Allocation_Details: ' || 'Refresh_Allocation_Details could not complete successfully');
4384                         msc_sch_wb.atp_debug('Refresh_Allocation_Details: ' || 'MSC_SCH_WB.G_ATP_ERROR_CODE := ' || MSC_SCH_WB.G_ATP_ERROR_CODE);
4385 
4386                 END IF;
4387                 OPEN Error_Meaning(MSC_SCH_WB.G_ATP_ERROR_CODE);
4388                 FETCH Error_Meaning INTO l_error_meaning;
4389                 IF Error_Meaning%notfound THEN
4390                         msc_util.msc_log('Error: ' || MSC_SCH_WB.G_ATP_ERROR_CODE);
4391                 ELSE
4392                         msc_util.msc_log(l_error_meaning);
4393                         IF PG_DEBUG in ('Y', 'C') THEN
4394                                 msc_sch_wb.atp_debug('Refresh_Allocation_Details: ' ||  l_error_meaning);
4395                         END IF;
4396                 END IF;
4397                 CLOSE Error_Meaning;
4398                 RETCODE:= G_ERROR;
4399         ELSE
4400                 IF PG_DEBUG in ('Y', 'C') THEN
4401                         msc_sch_wb.atp_debug('Refresh_Allocation_Details: ' ||  'Refresh_Allocation_Details completed successfully');
4402                 END IF;
4403         END IF;
4404 
4405         IF PG_DEBUG in ('Y', 'C') THEN
4406                 msc_sch_wb.atp_debug('Refresh_Allocation_Details: ' ||  'End Refresh_Allocation_Details');
4407         END IF;
4408 
4409 EXCEPTION
4410 WHEN OTHERS THEN
4411         IF PG_DEBUG in ('Y', 'C') THEN
4412                 msc_sch_wb.atp_debug('Refresh_Allocation_Details: ' || to_char(sqlcode) || ':' || sqlerrm);
4413         END IF;
4414         RETCODE:= G_ERROR;
4415         ERRBUF:= SQLERRM;
4416 
4417 END Refresh_Allocation_Details;
4418 
4419 
4420 /*--Backward_Consume-------------------------------------------------------
4421 |  o Does backward consumption.
4422 +-------------------------------------------------------------------------*/
4423 PROCEDURE Backward_Consume(
4424         p_atp_period                    IN OUT  NOCOPY  MRP_ATP_PUB.ATP_Period_Typ,
4425         x_dc_list_tab                   OUT     NOCOPY  MRP_ATP_PUB.char80_arr,
4426         x_dc_start_index                OUT     NOCOPY  MRP_ATP_PUB.number_arr,
4427         x_dc_end_index                  OUT     NOCOPY  MRP_ATP_PUB.number_arr,
4428         x_return_status                 OUT     NOCOPY  VARCHAR2)
4429 IS
4430         i PLS_INTEGER;
4431         j PLS_INTEGER;
4432         -- time_phased_atp
4433         l_atf_date      DATE := MSC_ATP_ALLOC.G_ATF_Date;
4434 BEGIN
4435 
4436         IF PG_DEBUG in ('Y', 'C') THEN
4437                 msc_sch_wb.atp_debug('Backward_Consume: ' ||  '**********Begin Backward_Consume Procedure************');
4438         END IF;
4439 
4440         -- Initializing API return code
4441         x_return_status := FND_API.G_RET_STS_SUCCESS;
4442 
4443         x_dc_list_tab := MRP_ATP_PUB.Char80_Arr();
4444         x_dc_start_index := MRP_ATP_PUB.Number_Arr();
4445         x_dc_end_index := MRP_ATP_PUB.Number_Arr();
4446 
4447         x_dc_list_tab.EXTEND;
4448         x_dc_start_index.EXTEND;
4449         x_dc_end_index.EXTEND;
4450 
4451         x_dc_list_tab(1) := p_atp_period.demand_class(p_atp_period.demand_class.FIRST);
4452         x_dc_start_index(1) := 1;
4453 
4454         -- Copy Backward Forward Quantity from Period Quantity initially.
4455         p_atp_period.Backward_Quantity := p_atp_period.Period_Quantity;
4456 
4457         -- this for loop will do backward consumption
4458         FOR i in 1..p_atp_period.demand_class.COUNT LOOP
4459 
4460                 -- If demand class changes, re-initialize these variables.
4461                 IF p_atp_period.demand_class(i) <> x_dc_list_tab(x_dc_list_tab.COUNT) THEN
4462                         -- Demand class changing
4463 
4464                         x_dc_end_index(x_dc_end_index.COUNT) := i - 1;
4465 
4466                         x_dc_list_tab.EXTEND;
4467                         x_dc_start_index.EXTEND;
4468                         x_dc_end_index.EXTEND;
4469                         x_dc_list_tab(x_dc_list_tab.COUNT) := p_atp_period.demand_class(i);
4470                         x_dc_start_index(x_dc_start_index.COUNT) := i;
4471                         x_dc_end_index(x_dc_end_index.COUNT) := i;
4472                 ELSE
4473                         x_dc_end_index(x_dc_end_index.COUNT) := i;
4474                 END IF;
4475 
4476                 -- backward consumption when neg atp quantity occurs
4477                 IF (p_atp_period.backward_quantity(i) < 0 ) THEN
4478                         j := i - 1;
4479                         WHILE ((j >= x_dc_start_index(x_dc_start_index.COUNT)) and
4480                                 (p_atp_period.backward_quantity(j) >= 0))  LOOP
4481                                 -- time_phased_atp
4482                                 IF ((l_atf_date is not null) and (p_atp_period.Period_Start_Date(i)>l_atf_date) and (p_atp_period.Period_Start_Date(j)<=l_atf_date)) THEN
4483                                         -- exit loop when crossing time fence
4484                                         j := 0;
4485                                 ELSIF (p_atp_period.backward_quantity(j) = 0) THEN
4486                                         --  backward one more period
4487                                         j := j-1 ;
4488                                 ELSE
4489                                         IF (p_atp_period.backward_quantity(j) + p_atp_period.backward_quantity(i) < 0) THEN
4490                                                 -- not enough to cover the shortage
4491                                                 p_atp_period.backward_quantity(i) := p_atp_period.backward_quantity(i) +
4492                                                                         p_atp_period.backward_quantity(j);
4493                                                 p_atp_period.backward_quantity(j) := 0;
4494                                                 j := j-1;
4495                                         ELSE
4496                                                 -- enough to cover the shortage
4497                                                 p_atp_period.backward_quantity(j) := p_atp_period.backward_quantity(j) +
4498                                                                         p_atp_period.backward_quantity(i);
4499                                                 p_atp_period.backward_quantity(i) := 0;
4500                                                 j := -1;
4501                                         END IF;
4502                                 END IF;
4503                         END LOOP;
4504                 END IF;
4505 
4506         END LOOP;
4507 
4508         IF PG_DEBUG in ('Y', 'C') THEN
4509                 FOR i in 1..x_dc_list_tab.COUNT LOOP
4510                         msc_sch_wb.atp_debug('Backward_Consume: ' ||  'DC:start:end:priority - ' || x_dc_list_tab(i) || ':' ||
4511                                 x_dc_start_index(i) || ':' ||
4512                                 x_dc_end_index(i) || ':' ||
4513                                 p_atp_period.Identifier2(x_dc_start_index(i)));
4514                 END LOOP;
4515 
4516                 msc_sch_wb.atp_debug('Backward_Consume: ' ||  '**********End Backward_Consume Procedure************');
4517         END IF;
4518 
4519 EXCEPTION
4520 WHEN OTHERS THEN
4521         IF PG_DEBUG in ('Y', 'C') THEN
4522                 msc_sch_wb.atp_debug('Backward_Consume: ' || 'Error occurred: ' || to_char(sqlcode) || ':' || SQLERRM);
4523         END IF;
4524         x_return_status := FND_API.G_RET_STS_ERROR;
4525         Set_Error(MSC_ATP_PVT.ATP_PROCESSING_ERROR);
4526 
4527 END Backward_Consume;
4528 
4529 
4530 /*--Forward_Consume--------------------------------------------------------
4531 |  o Does forward consumption.
4532 +-------------------------------------------------------------------------*/
4533 PROCEDURE Forward_Consume(
4534         p_atp_period                    IN OUT  NOCOPY  MRP_ATP_PUB.ATP_Period_Typ,
4535         p_start_index                   IN              NUMBER,
4536         p_end_index                     IN              NUMBER,
4537         x_return_status                 OUT     NOCOPY  VARCHAR2)
4538 IS
4539         i PLS_INTEGER;
4540         j PLS_INTEGER;
4541         -- time_phased_atp
4542         l_atf_date                      DATE    := MSC_ATP_ALLOC.G_ATF_Date;
4543         l_fw_nullifying_bucket_index    NUMBER  := 1;
4544 BEGIN
4545         IF PG_DEBUG in ('Y', 'C') THEN
4546                 msc_sch_wb.atp_debug('Forward_Consume: ' ||  '**********Begin Forward_Consume Procedure************');
4547         END IF;
4548 
4549         -- Initializing API return code
4550         x_return_status := FND_API.G_RET_STS_SUCCESS;
4551 
4552         -- this procedure will add p_steal_atp's negatives in p_atp_period.
4553         -- It is assumed here that the dates in p_steal_atp is always a subset of dates in p_atp_period
4554 
4555         IF PG_DEBUG in ('Y', 'C') THEN
4556                 i := p_start_index;
4557                 WHILE (i is not null) AND (i <= p_end_index) LOOP
4558                         msc_sch_wb.atp_debug('Forward_Consume: ' ||  'current date:qty - '||
4559                         p_atp_period.Period_Start_Date(i) ||' : '|| p_atp_period.Adjusted_Availability_Quantity(i) );
4560                         i := p_atp_period.Period_Start_Date.Next(i);
4561                 END LOOP;
4562         END IF;
4563 
4564         -- this for loop will do backward consumption
4565         FOR i in p_start_index..p_end_index LOOP
4566 
4567                 -- forward consumption when neg atp quantity occurs
4568                 IF (p_atp_period.Adjusted_Availability_Quantity(i) < 0 ) THEN
4569 
4570                         j := i + 1;
4571 
4572                         IF PG_DEBUG in ('Y', 'C') THEN
4573                                 msc_sch_wb.atp_debug('Forward_Consume: ' ||  'shortage - qty,i:' || p_atp_period.Adjusted_Availability_Quantity(i) || ',' || i);
4574                         END IF;
4575 
4576                         WHILE (j <= p_end_index) LOOP
4577                                 -- time_phased_atp
4578                                 IF ((l_atf_date is not null) and (p_atp_period.Period_Start_Date(i)<=l_atf_date) and (p_atp_period.Period_Start_Date(j)>l_atf_date)) THEN
4579                                         -- exit loop when crossing time fence
4580                                         j := p_end_index+1;
4581                                 ELSIF (p_atp_period.Adjusted_Availability_Quantity(j) <= 0
4582                                     -- time_phased_atp
4583                                     OR j < l_fw_nullifying_bucket_index)
4584                                 THEN
4585                                         --  forward one more period
4586                                         j := j+1 ;
4587                                 ELSE
4588                                         -- You can get something from here. So set the nullifying bucket index
4589                                         l_fw_nullifying_bucket_index := j;
4590                                         IF (p_atp_period.Adjusted_Availability_Quantity(j) +
4591                                                 p_atp_period.Adjusted_Availability_Quantity(i) < 0) THEN
4592                                                 -- not enough to cover the shortage
4593                                                 IF PG_DEBUG in ('Y', 'C') THEN
4594                                                         msc_sch_wb.atp_debug('Forward_Consume: ' ||  'consuming      - qty,j:'  ||
4595                                                                 p_atp_period.Adjusted_Availability_Quantity(j) || ',' || j);
4596                                                 END IF;
4597                                                 p_atp_period.Adjusted_Availability_Quantity(i) := p_atp_period.Adjusted_Availability_Quantity(i) +
4598                                                         p_atp_period.Adjusted_Availability_Quantity(j);
4599                                                 p_atp_period.Adjusted_Availability_Quantity(j) := 0;
4600                                                 j := j + 1;
4601                                         ELSE
4602                                                 -- enough to cover the shortage
4603                                                 IF PG_DEBUG in ('Y', 'C') THEN
4604                                                         msc_sch_wb.atp_debug('Forward_Consume: ' ||  'consuming last - qty,j:'  ||
4605                                                                 p_atp_period.Adjusted_Availability_Quantity(i) || ',' || j);
4606                                                 END IF;
4607                                                 p_atp_period.Adjusted_Availability_Quantity(j) := p_atp_period.Adjusted_Availability_Quantity(j) +
4608                                                         p_atp_period.Adjusted_Availability_Quantity(i);
4609                                                 p_atp_period.Adjusted_Availability_Quantity(i) := 0;
4610                                                 EXIT;
4611                                         END IF;
4612                                 END IF;
4613                         END LOOP;
4614 
4615                 END IF;
4616         END LOOP;
4617 
4618         IF PG_DEBUG in ('Y', 'C') THEN
4619                 i := p_start_index;
4620                 WHILE (i is not null) AND (i <= p_end_index) LOOP
4621                         msc_sch_wb.atp_debug('Forward_Consume: ' ||  'current date:qty - '||
4622                         p_atp_period.Period_Start_Date(i) ||' : '|| p_atp_period.Adjusted_Availability_Quantity(i) );
4623                         i := p_atp_period.Period_Start_Date.Next(i);
4624                 END LOOP;
4625                 msc_sch_wb.atp_debug('Forward_Consume: ' ||  '**********End Forward_Consume Procedure************');
4626         END IF;
4627 EXCEPTION
4628 WHEN OTHERS THEN
4629         IF PG_DEBUG in ('Y', 'C') THEN
4630                 msc_sch_wb.atp_debug('Forward_Consume: ' || 'Error occurred: ' || to_char(sqlcode) || ':' || SQLERRM);
4631         END IF;
4632         x_return_status := FND_API.G_RET_STS_ERROR;
4633         Set_Error(MSC_ATP_PVT.ATP_PROCESSING_ERROR);
4634 
4635 END Forward_Consume;
4636 
4637 
4638 /*--Backward_Forward_Consume-----------------------------------------------
4639 |  o Does backward/forward consumption.
4640 |  o Used in demand priority case.
4641 +-------------------------------------------------------------------------*/
4642 PROCEDURE Backward_Forward_Consume(
4643         p_atp_period                    IN OUT  NOCOPY  MRP_ATP_PUB.ATP_Period_Typ,
4644         x_dc_list_tab                   OUT     NOCOPY  MRP_ATP_PUB.char80_arr,
4645         x_dc_start_index                OUT     NOCOPY  MRP_ATP_PUB.number_arr,
4646         x_dc_end_index                  OUT     NOCOPY  MRP_ATP_PUB.number_arr,
4647         x_return_status                 OUT     NOCOPY  VARCHAR2)
4648 IS
4649         i PLS_INTEGER;
4650         j PLS_INTEGER;
4651         -- time_phased_atp
4652         l_atf_date                      DATE   := MSC_ATP_ALLOC.G_Atf_Date;
4653         l_fw_nullifying_bucket_index    NUMBER := 1;
4654 BEGIN
4655 
4656         IF PG_DEBUG in ('Y', 'C') THEN
4657                 msc_sch_wb.atp_debug('Backward_Forward_Consume: ' ||  '**********Begin Backward_Forward_Consume Procedure************');
4658         END IF;
4659 
4660         -- Initializing API return code
4661         x_return_status := FND_API.G_RET_STS_SUCCESS;
4662 
4663         x_dc_list_tab := MRP_ATP_PUB.Char80_Arr();
4664         x_dc_start_index := MRP_ATP_PUB.Number_Arr();
4665         x_dc_end_index := MRP_ATP_PUB.Number_Arr();
4666 
4667         x_dc_list_tab.EXTEND;
4668         x_dc_start_index.EXTEND;
4669         x_dc_end_index.EXTEND;
4670 
4671 
4672         x_dc_list_tab(1) := p_atp_period.demand_class(p_atp_period.demand_class.FIRST);
4673         x_dc_start_index(1) := 1;
4674 
4675         -- Copy Backward Forward Quantity from Period Quantity initially.
4676         p_atp_period.Backward_Forward_Quantity := p_atp_period.Period_Quantity;
4677 
4678         -- this for loop will do backward consumption
4679         FOR i in 1..p_atp_period.demand_class.COUNT LOOP
4680 
4681                 IF PG_DEBUG in ('Y', 'C') THEN
4682                         msc_sch_wb.atp_debug('Backward_Forward_Consume: ' ||  'index : ' || i);
4683                         msc_sch_wb.atp_debug('Backward_Forward_Consume: ' ||  'x_dc_list_tab : ' || x_dc_list_tab(x_dc_list_tab.COUNT));
4684                         msc_sch_wb.atp_debug('Backward_Forward_Consume: ' ||  'p_atp_period.demand_class : ' || p_atp_period.demand_class(i));
4685                 END IF;
4686 
4687                 -- If demand class changes, re-initialize these variables.
4688                 IF p_atp_period.demand_class(i) <> x_dc_list_tab(x_dc_list_tab.COUNT) THEN
4689                         IF PG_DEBUG in ('Y', 'C') THEN
4690                                 msc_sch_wb.atp_debug('Backward_Forward_Consume: ' ||  'Demand class changing');
4691                         END IF;
4692 
4693                         x_dc_end_index(x_dc_end_index.COUNT) := i - 1;
4694 
4695                         IF PG_DEBUG in ('Y', 'C') THEN
4696                                 msc_sch_wb.atp_debug('Backward_Forward_Consume: ' ||  'demand class, end index : ' || x_dc_list_tab(x_dc_list_tab.COUNT)
4697                                                 || ', ' || x_dc_end_index(x_dc_end_index.COUNT));
4698                         END IF;
4699 
4700                         x_dc_list_tab.EXTEND;
4701                         x_dc_start_index.EXTEND;
4702                         x_dc_end_index.EXTEND;
4703                         x_dc_list_tab(x_dc_list_tab.COUNT) := p_atp_period.demand_class(i);
4704                         x_dc_start_index(x_dc_start_index.COUNT) := i;
4705                         x_dc_end_index(x_dc_end_index.COUNT) := i;
4706 
4707                         IF PG_DEBUG in ('Y', 'C') THEN
4708                                 msc_sch_wb.atp_debug('Backward_Forward_Consume: ' ||  'demand class, start index : ' || x_dc_list_tab(x_dc_list_tab.COUNT)
4709                                                 || ', ' || x_dc_start_index(x_dc_start_index.COUNT));
4710                         END IF;
4711                 ELSE
4712                    x_dc_end_index(x_dc_end_index.COUNT) := i;
4713                 END IF;
4714 
4715                 -- backward consumption when neg atp quantity occurs
4716                 IF (p_atp_period.backward_forward_quantity(i) < 0 ) THEN
4717                         j := i - 1;
4718                         WHILE ((j >= x_dc_start_index(x_dc_start_index.COUNT)) and
4719                                 (p_atp_period.backward_forward_quantity(j) >= 0))  LOOP
4720                                 -- time_phased_atp
4721                                 IF ((l_atf_date is not null) and (p_atp_period.Period_Start_Date(i)>l_atf_date) and (p_atp_period.Period_Start_Date(j)<=l_atf_date)) THEN
4722                                         -- exit loop when crossing time fence
4723                                         j := 0;
4724                                 ELSIF (p_atp_period.backward_forward_quantity(j) = 0) THEN
4725                                         --  backward one more period
4726                                         j := j-1 ;
4727                                 ELSE
4728                                         IF (p_atp_period.backward_forward_quantity(j) + p_atp_period.backward_forward_quantity(i) < 0) THEN
4729                                                 -- not enough to cover the shortage
4730                                                 p_atp_period.backward_forward_quantity(i) := p_atp_period.backward_forward_quantity(i) +
4731                                                         p_atp_period.backward_forward_quantity(j);
4732                                                 p_atp_period.backward_forward_quantity(j) := 0;
4733                                                 j := j-1;
4734                                         ELSE
4735                                                 -- enough to cover the shortage
4736                                                 p_atp_period.backward_forward_quantity(j) := p_atp_period.backward_forward_quantity(j) +
4737                                                         p_atp_period.backward_forward_quantity(i);
4738                                                 p_atp_period.backward_forward_quantity(i) := 0;
4739                                                 j := -1;
4740                                         END IF;
4741                                 END IF;
4742                         END LOOP;
4743                 END IF;
4744 
4745                 IF PG_DEBUG in ('Y', 'C') THEN
4746                         msc_sch_wb.atp_debug('Backward_Forward_Consume: ' ||  'before forward consumption');
4747                 END IF;
4748                 -- this for loop will do forward consumption
4749 
4750                 -- forward consumption when neg atp quantity occurs
4751                 IF (p_atp_period.backward_forward_quantity(i) < 0 ) THEN
4752 
4753                         j := i + 1;
4754 
4755                         IF PG_DEBUG in ('Y', 'C') THEN
4756                                 msc_sch_wb.atp_debug('Backward_Forward_Consume: ' ||  'in forward consumption : '  || i || ':' || j);
4757                                 msc_sch_wb.atp_debug('Backward_Forward_Consume: ' ||  'in forward : '  || p_atp_period.demand_class.COUNT);
4758                         END IF;
4759 
4760                         IF j <= p_atp_period.demand_class.COUNT THEN
4761 
4762                                 IF PG_DEBUG in ('Y', 'C') THEN
4763                                         msc_sch_wb.atp_debug('Backward_Forward_Consume: ' ||  'in j : '  || p_atp_period.demand_class.COUNT);
4764                                 END IF;
4765 
4766                                 WHILE (p_atp_period.demand_class(j) = x_dc_list_tab(x_dc_list_tab.COUNT))  LOOP
4767                                         -- time_phased_atp
4768                                         IF ((l_atf_date is not null) and (p_atp_period.Period_Start_Date(i)<=l_atf_date) and (p_atp_period.Period_Start_Date(j)>l_atf_date)) THEN
4769                                                 -- exit loop when crossing time fence
4770                                                 j := p_atp_period.demand_class.COUNT+1;
4771                                         ELSIF (p_atp_period.backward_forward_quantity(j) <= 0
4772                                             -- time_phased_atp
4773                                             OR j < l_fw_nullifying_bucket_index)
4774                                         THEN
4775                                                 --  forward one more period
4776                                                 j := j+1 ;
4777                                         ELSE
4778                                                 -- You can get something from here. So set the nullifying bucket index
4779                                                 l_fw_nullifying_bucket_index := j;
4780                                                 IF (p_atp_period.backward_forward_quantity(j) + p_atp_period.backward_forward_quantity(i) < 0) THEN
4781                                                         -- not enough to cover the shortage
4782                                                         p_atp_period.backward_forward_quantity(i) := p_atp_period.backward_forward_quantity(i) +
4783                                                                 p_atp_period.backward_forward_quantity(j);
4784                                                         p_atp_period.backward_forward_quantity(j) := 0;
4785                                                         j := j + 1;
4786                                                 ELSE
4787                                                         -- enough to cover the shortage
4788                                                         p_atp_period.backward_forward_quantity(j) := p_atp_period.backward_forward_quantity(j) +
4789                                                                 p_atp_period.backward_forward_quantity(i);
4790                                                         p_atp_period.backward_forward_quantity(i) := 0;
4791                                                         EXIT;
4792                                                 END IF;
4793                                         END IF;
4794 
4795                                         IF j > p_atp_period.demand_class.COUNT THEN
4796                                                 EXIT;
4797                                         END IF;
4798 
4799                                 END LOOP;
4800                         END IF;
4801                 END IF;
4802 
4803         END LOOP;
4804 
4805         IF PG_DEBUG in ('Y', 'C') THEN
4806                 msc_sch_wb.atp_debug('Backward_Forward_Consume: ' ||  '**********End Backward_Forward_Consume Procedure************');
4807         END IF;
4808 
4809 EXCEPTION
4810 WHEN OTHERS THEN
4811         IF PG_DEBUG in ('Y', 'C') THEN
4812                 msc_sch_wb.atp_debug('Backward_Forward_Consume: ' || 'Error occurred: ' || to_char(sqlcode) || ':' || SQLERRM);
4813         END IF;
4814         x_return_status := FND_API.G_RET_STS_ERROR;
4815         Set_Error(MSC_ATP_PVT.ATP_PROCESSING_ERROR);
4816 
4817 END Backward_Forward_Consume;
4818 
4819 
4820 
4821 /*--Compute_Cum------------------------------------------------------------
4822 |  o Does accumulation for all demand classes.
4823 +-------------------------------------------------------------------------*/
4824 PROCEDURE Compute_Cum(
4825         p_atp_period                    IN OUT  NOCOPY  MRP_ATP_PUB.ATP_Period_Typ,
4826         p_dc_list_tab                   IN              MRP_ATP_PUB.char80_arr,
4827         p_dc_start_index                IN              MRP_ATP_PUB.number_arr,
4828         p_dc_end_index                  IN              MRP_ATP_PUB.number_arr,
4829         x_return_status                 OUT     NOCOPY  VARCHAR2)
4830 IS
4831         i PLS_INTEGER;
4832         j PLS_INTEGER;
4833 BEGIN
4834 
4835         IF PG_DEBUG in ('Y', 'C') THEN
4836                 msc_sch_wb.atp_debug('Compute_Cum: ' ||  '**********Begin Compute_Cum Procedure************');
4837         END IF;
4838 
4839         -- Initializing API return code
4840         x_return_status := FND_API.G_RET_STS_SUCCESS;
4841 
4842         -- this for loop will do the acculumation
4843         FOR j in 1..p_dc_list_tab.COUNT LOOP
4844 
4845                 FOR i in (p_dc_start_index(j) + 1)..p_dc_end_index(j) LOOP
4846                         p_atp_period.cumulative_quantity(i) := p_atp_period.cumulative_quantity(i) +
4847                                         p_atp_period.cumulative_quantity(i-1);
4848                 END LOOP;
4849 
4850         END LOOP;
4851 
4852         IF PG_DEBUG in ('Y', 'C') THEN
4853                 msc_sch_wb.atp_debug('Compute_Cum: ' ||  '**********End Compute_Cum Procedure************');
4854         END IF;
4855 EXCEPTION
4856 WHEN OTHERS THEN
4857         IF PG_DEBUG in ('Y', 'C') THEN
4858                 msc_sch_wb.atp_debug('Compute_Cum: ' || 'Error occurred: ' || to_char(sqlcode) || ':' || SQLERRM);
4859         END IF;
4860         x_return_status := FND_API.G_RET_STS_ERROR;
4861         Set_Error(MSC_ATP_PVT.ATP_PROCESSING_ERROR);
4862 END Compute_Cum;
4863 
4864 
4865 /*--Compute_Cum------------------------------------------------------------
4866 |  o Does accumulation for a specific demand class.
4867 +-------------------------------------------------------------------------*/
4868 PROCEDURE Compute_Cum_Individual(
4869         p_atp_period                    IN OUT  NOCOPY  MRP_ATP_PUB.ATP_Period_Typ,
4870         p_start_index                   IN              NUMBER,
4871         p_end_index                     IN              NUMBER,
4872         x_return_status                 OUT     NOCOPY  VARCHAR2)
4873 IS
4874         i       PLS_INTEGER;
4875         l_cumm  NUMBER;
4876 BEGIN
4877 
4878         IF PG_DEBUG in ('Y', 'C') THEN
4879                 msc_sch_wb.atp_debug('Compute_Cum_Individual: ' ||  '**********Begin Compute_Cum_Individual Procedure************');
4880                 msc_sch_wb.atp_debug('Compute_Cum_Individual: ' ||  'p_start_index : ' || p_start_index);
4881                 msc_sch_wb.atp_debug('Compute_Cum_Individual: ' ||  'p_end_index : ' || p_end_index);
4882         END IF;
4883 
4884         -- Initializing API return code
4885         x_return_status := FND_API.G_RET_STS_SUCCESS;
4886 
4887         -- this for loop will do the acculumation for one demand class
4888         -- this will also convert negatives to zero
4889 
4890         l_cumm := 0;
4891         FOR i in p_start_index..p_end_index LOOP
4892                 l_cumm := p_atp_period.Adjusted_Availability_Quantity(i) + l_cumm;
4893                 p_atp_period.cumulative_quantity(i) := GREATEST(l_cumm,0);
4894         END LOOP;
4895 
4896         IF PG_DEBUG in ('Y', 'C') THEN
4897                 i := p_start_index;
4898                 WHILE (i is not null) AND (i <= p_end_index) LOOP
4899                         msc_sch_wb.atp_debug('Compute_Cum_Individual: ' ||  'cum date:qty - '||
4900                                 p_atp_period.Period_Start_Date(i) ||' : '|| p_atp_period.cumulative_quantity(i) );
4901                         i := p_atp_period.Period_Start_Date.Next(i);
4902                 END LOOP;
4903                 msc_sch_wb.atp_debug('Compute_Cum_Individual: ' ||  '**********End Compute_Cum_Individual Procedure************');
4904         END IF;
4905 EXCEPTION
4906 WHEN OTHERS THEN
4907         IF PG_DEBUG in ('Y', 'C') THEN
4908                 msc_sch_wb.atp_debug('Compute_Cum_Individual: ' || 'Error occurred: ' || to_char(sqlcode) || ':' || SQLERRM);
4909         END IF;
4910         x_return_status := FND_API.G_RET_STS_ERROR;
4911         Set_Error(MSC_ATP_PVT.ATP_PROCESSING_ERROR);
4912 END Compute_Cum_Individual;
4913 
4914 
4915 
4916 /*--Adjust_Allocation_Details----------------------------------------------
4917 |  o Called by Compute_Allocation_Details only for rule based case.
4918 |  o Performs demand class consumption and forward consumption.
4919 +-------------------------------------------------------------------------*/
4920 PROCEDURE Adjust_Allocation_Details(
4921         p_atp_period                    IN OUT  NOCOPY  MRP_ATP_PUB.ATP_Period_Typ,
4922         x_dc_list_tab                   OUT     NOCOPY  MRP_ATP_PUB.char80_arr,
4923         x_dc_start_index                OUT     NOCOPY  MRP_ATP_PUB.number_arr,
4924         x_dc_end_index                  OUT     NOCOPY  MRP_ATP_PUB.number_arr,
4925         x_return_status                 OUT     NOCOPY  VARCHAR2)
4926 IS
4927         l_return_status                 VARCHAR2(1);
4928         l_lowest_priority_demand_class  VARCHAR2(80);
4929         l_lowest_priority               PLS_INTEGER;
4930         l_lowest_found                  BOOLEAN := false;
4931         l_start_index                   PLS_INTEGER;
4932         l_end_index                     PLS_INTEGER;
4933         l_dc_count                      PLS_INTEGER;
4934         l_fw_consume_next               PLS_INTEGER := 0;
4935         l_lowest_cust_priority          PLS_INTEGER;
4936         l_lowest_site_priority          PLS_INTEGER;
4937         l_fw_consume_tab                MRP_ATP_PUB.number_arr := MRP_ATP_PUB.number_arr();
4938         l_class_curr_steal_atp          MRP_ATP_PVT.ATP_Info;
4939         l_class_next_steal_atp          MRP_ATP_PVT.ATP_Info;
4940         l_partner_curr_steal_atp        MRP_ATP_PVT.ATP_Info;
4941         l_partner_next_steal_atp        MRP_ATP_PVT.ATP_Info;
4942         l_current_steal_atp             MRP_ATP_PVT.ATP_Info;
4943         l_next_steal_atp                MRP_ATP_PVT.ATP_Info;
4944         l_null_steal_atp                MRP_ATP_PVT.ATP_Info;
4945         i                               PLS_INTEGER;
4946         j                               PLS_INTEGER;
4947         mm                              PLS_INTEGER;
4948 BEGIN
4949         IF PG_DEBUG in ('Y', 'C') THEN
4950                 msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  '*********Begin procedure Adjust_Allocation_Details ********');
4951         END IF;
4952 
4953         -- Initializing API return code
4954         x_return_status := FND_API.G_RET_STS_SUCCESS;
4955 
4956         -- IF G_ATP_FW_CONSUME_METHOD = 2 THEN
4957         -- if condition removed for bug 2763784 (ssurendr)
4958         -- always get unallocated figures
4959         -- Append unallocated records
4960         i := 1;
4961         WHILE (p_atp_period.demand_class(i) = p_atp_period.demand_class(1)) LOOP
4962                 -- get data from 1st demand class
4963                 MSC_SATP_FUNC.Extend_Atp_Period(p_atp_period, l_return_status);
4964                 j := p_atp_period.Period_Start_Date.COUNT;
4965 
4966                 p_atp_period.Demand_Class(j)            := G_UNALLOCATED_DC;
4967                 p_atp_period.Period_Start_Date(j)       := p_atp_period.Period_Start_Date(i);
4968                 p_atp_period.Total_Supply_Quantity(j)   := p_atp_period.Unallocated_Supply_Quantity(i);
4969                 p_atp_period.Total_Demand_Quantity(j)   := p_atp_period.Unallocated_Demand_Quantity(i);
4970                 p_atp_period.Period_Quantity(j)         := p_atp_period.Unallocated_Net_Quantity(i);
4971                 p_atp_period.Period_End_Date(j)         := p_atp_period.Period_End_Date(i);
4972                 p_atp_period.Inventory_Item_Id(j)       := p_atp_period.Inventory_Item_Id(i);
4973                 p_atp_period.Organization_Id(j)         := p_atp_period.Organization_Id(i);
4974                 p_atp_period.Identifier1(j)             := p_atp_period.Identifier1(i);
4975                 p_atp_period.Scenario_Id(j)             := p_atp_period.Scenario_Id(i);
4976                 p_atp_period.Level(j)                   := p_atp_period.Level(i);
4977                 -- time_phased_atp
4978                 p_atp_period.Total_Bucketed_Demand_Quantity(j)   := p_atp_period.Unalloc_Bucketed_Demand_Qty(i);
4979 
4980                 i := p_atp_period.demand_class.Next(i);
4981         END LOOP;
4982         -- END IF;
4983 
4984         Backward_Consume(p_atp_period, x_dc_list_tab, x_dc_start_index, x_dc_end_index, l_return_status);
4985         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4986                 IF PG_DEBUG in ('Y', 'C') THEN
4987                         msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'Error occured in procedure Backward_Consume');
4988                 END IF;
4989                 x_return_status := FND_API.G_RET_STS_ERROR;
4990                 return;
4991         END IF;
4992 
4993         IF PG_DEBUG in ('Y', 'C') THEN
4994                 FOR mm in 1..p_atp_period.demand_class.COUNT LOOP
4995                         msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'DC:Date:Qty - ' || p_atp_period.demand_class(mm) || ':' ||
4996                                                 p_atp_period.Period_Start_Date(mm) || ':' ||
4997                                                 p_atp_period.Backward_Quantity(mm));
4998                 END LOOP;
4999         END IF;
5000 
5001         -- Copy Adjusted_Availability_Quantity from Backward_Quantity
5002         p_atp_period.Adjusted_Availability_Quantity := p_atp_period.Backward_Quantity;
5003 
5004         -- IF G_ATP_FW_CONSUME_METHOD = 2 THEN
5005         -- if condition removed for bug 2763784 (ssurendr)
5006         -- always get unallocated figures
5007         -- Do f/w consumption and accumulation for unallocated records
5008         l_start_index := x_dc_start_index(x_dc_list_tab.COUNT);
5009         l_end_index := x_dc_end_index(x_dc_list_tab.COUNT);
5010         Forward_Consume(p_atp_period, l_start_index, l_end_index, l_return_status);
5011         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5012                 IF PG_DEBUG in ('Y', 'C') THEN
5013                         msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'Error occured in procedure Forward_Consume');
5014                 END IF;
5015                 x_return_status := FND_API.G_RET_STS_ERROR;
5016                 return;
5017         END IF;
5018 
5019         /* rajjain 02/12/2003
5020          * Call to Compute_Cum_Individual procedure is not required in Method1
5021          * as Unallocated Cum will be calculated as part of Compute_Cum procedure*/
5022         IF G_ATP_FW_CONSUME_METHOD = 2 THEN
5023 	        Compute_Cum_Individual(p_atp_period, l_start_index, l_end_index, l_return_status);
5024 	        IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5025 	                IF PG_DEBUG in ('Y', 'C') THEN
5026 	                        msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'Error occured in procedure Compute_Cum_Individual');
5027 	                END IF;
5028 	                x_return_status := FND_API.G_RET_STS_ERROR;
5029 	                return;
5030 	        END IF;
5031         END IF;
5032         p_atp_period.Adjusted_Cum_Quantity := p_atp_period.Cumulative_Quantity;
5033         -- END IF;
5034 
5035         l_dc_count := x_dc_list_tab.LAST;
5036         -- IF G_ATP_FW_CONSUME_METHOD = 2 THEN
5037         -- if condition removed for bug 2763784 (ssurendr)
5038         -- always get unallocated figures
5039         l_dc_count := x_dc_list_tab.Prior(l_dc_count);
5040         -- subtract 1 bacause of unallocated records
5041         -- END IF;
5042         IF PG_DEBUG in ('Y', 'C') THEN
5043                 msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'l_dc_count: ' || l_dc_count);
5044         END IF;
5045 
5046         IF (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) THEN
5047                 -- Demand class case
5048 
5049                 IF PG_DEBUG in ('Y', 'C') THEN
5050                         msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'Demand class case.');
5051                 END IF;
5052 
5053                 j := l_dc_count;
5054                 WHILE (j is not null) LOOP
5055                         IF p_atp_period.Identifier4(x_dc_start_index(j)) <> 0 THEN
5056                                 l_lowest_priority_demand_class := x_dc_list_tab(j);
5057                                 l_lowest_priority := p_atp_period.Identifier2(x_dc_start_index(j));
5058                                 EXIT;
5059                         END IF;
5060                         j := x_dc_list_tab.Prior(j);
5061                 END LOOP;
5062 
5063                 IF PG_DEBUG in ('Y', 'C') THEN
5064                         msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'l_lowest_priority_demand_class: ' || l_lowest_priority_demand_class);
5065                         msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'l_lowest_priority: ' || l_lowest_priority);
5066                 END IF;
5067 
5068                 -- this for loop will do the demand class consumption
5069                 FOR j in 1..l_dc_count LOOP
5070 
5071                         l_start_index := x_dc_start_index(j);
5072                         l_end_index := x_dc_end_index(j);
5073 
5074                         IF PG_DEBUG in ('Y', 'C') THEN
5075                                 msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'DC changed. limits : ' || l_start_index || ',' || l_end_index);
5076                         END IF;
5077 
5078                         IF (j > 1) THEN
5079                                 IF p_atp_period.Identifier2(x_dc_start_index(j)) > p_atp_period.Identifier2(x_dc_start_index(j-1)) THEN
5080                                         IF PG_DEBUG in ('Y', 'C') THEN
5081                                                 msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'Priority changing. Changing l_current_steal_atp');
5082                                         END IF;
5083                                         MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_next_steal_atp, l_current_steal_atp);
5084                                         l_next_steal_atp := l_null_steal_atp;
5085                                 END IF;
5086                         END IF;
5087 
5088                         IF p_atp_period.Identifier2(x_dc_start_index(j)) <> p_atp_period.Identifier2(1) THEN
5089                                 -- We need to do demand class consumption only if we are not in
5090                                 -- the highest priority
5091                                 IF PG_DEBUG in ('Y', 'C') THEN
5092                                         msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'Before doing DC consumption.');
5093                                 END IF;
5094                                 Demand_Class_Consumption(p_atp_period, l_start_index,
5095                                         l_end_index, l_current_steal_atp, l_return_status);
5096                                 IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5097                                         IF PG_DEBUG in ('Y', 'C') THEN
5098                                                 msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'Error occured in procedure Demand_Class_Consumption(');
5099                                         END IF;
5100                                         x_return_status := FND_API.G_RET_STS_ERROR;
5101                                         return;
5102                                 END IF;
5103                                 IF PG_DEBUG in ('Y', 'C') THEN
5104                                         msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'After doing DC consumption.');
5105                                 END IF;
5106                         END IF;
5107 
5108                         IF p_atp_period.Identifier2(x_dc_start_index(j)) < l_lowest_priority OR
5109                                 G_ATP_FW_CONSUME_METHOD = 2 THEN
5110                                 -- This is not the lowest priority DC. No need to do f/w consumption in Method 1
5111                                 -- But we need to add to next steal
5112                                 -- Two points to be noted:
5113                                 -- 1. For Method 2 this is called even for lowest priority DC. This is an extra
5114                                 --    overhead when compared to simply calling Remove_Nagatives. But this will
5115                                 --    provide an OK solution for the corner case when the actual lowest priority DC
5116                                 --    (which can be at a lower priority that l_lowest_priority) has 0 allocation %
5117                                 --    on sysdate but non-zero on some future date.
5118                                 -- 2. Even for method 1 the check is "higher than lowest priority" and not
5119                                 --    "not equal to lowest priority". What this means is that DCs lower than
5120                                 --    l_lowest_priority will do f/w consumtion for at least itself.
5121                                 Add_to_Next_Steal(p_atp_period, l_start_index,
5122                                         l_end_index, l_next_steal_atp, l_return_status);
5123                                 IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5124                                         IF PG_DEBUG in ('Y', 'C') THEN
5125                                                 msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'Error occured in procedure Add_to_Next_Steal');
5126                                         END IF;
5127                                         x_return_status := FND_API.G_RET_STS_ERROR;
5128                                         return;
5129                                 END IF;
5130                         ELSIF G_ATP_FW_CONSUME_METHOD = 1 THEN
5131                                 -- Forward consumption is required only if G_ATP_FW_CONSUME_METHOD = 1
5132                                 IF p_atp_period.Demand_Class(x_dc_start_index(j)) = l_lowest_priority_demand_class THEN
5133                                         -- Last demand class. Need to consider l_next_steal_atp's
5134                                         -- negatives before forward consumption
5135                                         Add_to_Current_Atp(p_atp_period, l_start_index,
5136                                         l_end_index, l_current_steal_atp, l_return_status);
5137                                         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5138                                                 IF PG_DEBUG in ('Y', 'C') THEN
5139                                                         msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'Error occured in procedure Add_to_Current_Atp');
5140                                                 END IF;
5141                                                 x_return_status := FND_API.G_RET_STS_ERROR;
5142                                                 return;
5143                                         END IF;
5144                                 END IF;
5145 
5146                                 Forward_Consume(p_atp_period, l_start_index, l_end_index, l_return_status);
5147                                 IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5148                                         IF PG_DEBUG in ('Y', 'C') THEN
5149                                                 msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'Error occured in procedure Forward_Consume');
5150                                         END IF;
5151                                         x_return_status := FND_API.G_RET_STS_ERROR;
5152                                         return;
5153                                 END IF;
5154 
5155                                 Remove_Negatives(p_atp_period, l_start_index, l_end_index, l_return_status);
5156                                 IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5157                                         IF PG_DEBUG in ('Y', 'C') THEN
5158                                                 msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'Error occured in procedure Remove_Negatives');
5159                                         END IF;
5160                                         x_return_status := FND_API.G_RET_STS_ERROR;
5161                                         return;
5162                                 END IF;
5163                         END IF;
5164 
5165                         IF PG_DEBUG in ('Y', 'C') THEN
5166                                 FOR mm in l_start_index..l_end_index LOOP
5167                                         msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'adjusted date:qty - '||
5168                                                 p_atp_period.Period_Start_Date(mm) ||' : '|| p_atp_period.Adjusted_Availability_Quantity(mm) );
5169                                 END LOOP;
5170                         END IF;
5171 
5172                         IF G_ATP_FW_CONSUME_METHOD = 2 THEN
5173                                 Compute_Cum_Individual(p_atp_period, l_start_index, l_end_index, l_return_status);
5174                                 IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5175                                         IF PG_DEBUG in ('Y', 'C') THEN
5176                                                 msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'Error occured in procedure Compute_Cum_Individual');
5177                                         END IF;
5178                                         x_return_status := FND_API.G_RET_STS_ERROR;
5179                                         return;
5180                                 END IF;
5181 
5182                                 Adjust_Cum(p_atp_period, l_start_index, l_end_index,
5183                                         x_dc_start_index(x_dc_list_tab.COUNT), x_dc_end_index(x_dc_list_tab.COUNT), l_return_status);
5184                                 IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5185                                         IF PG_DEBUG in ('Y', 'C') THEN
5186                                                 msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'Error occured in procedure Adjust_Cum');
5187                                         END IF;
5188                                         x_return_status := FND_API.G_RET_STS_ERROR;
5189                                         return;
5190                                 END IF;
5191 
5192                                 IF PG_DEBUG in ('Y', 'C') THEN
5193                                         FOR mm in l_start_index..l_end_index LOOP
5194                                                 msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'adjusted cum date:qty - '||
5195                                                         p_atp_period.Period_Start_Date(mm) ||' : '|| p_atp_period.Adjusted_Cum_Quantity(mm) );
5196                                         END LOOP;
5197                                 END IF;
5198                         END IF;
5199 
5200                 END LOOP;
5201 
5202 
5203         ELSIF (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 2) THEN
5204                 -- IF (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) THEN
5205                 -- Customer class heirarchy
5206 
5207                 IF PG_DEBUG in ('Y', 'C') THEN
5208                         msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'Customer class case.');
5209                 END IF;
5210 
5211                 IF G_ATP_FW_CONSUME_METHOD = 1 THEN
5212                         -- steal from lower priority
5213 
5214                         -- Extend and initiallize l_fw_consume_tab
5215                         FOR j in 1..l_dc_count LOOP
5216                                 l_fw_consume_tab.Extend;
5217                                 l_fw_consume_tab(j) := 0;
5218                         END LOOP;
5219 
5220                         j := l_fw_consume_tab.LAST;
5221                         l_fw_consume_next := 4;
5222 
5223                         WHILE (j is not null) LOOP
5224 
5225                                 IF l_fw_consume_next <> 0 THEN
5226                                         IF p_atp_period.Identifier4(x_dc_start_index(j)) <> 0 THEN
5227                                                 IF l_fw_consume_next = 2 THEN
5228                                                         l_lowest_site_priority := p_atp_period.Identifier2(x_dc_start_index(j));
5229                                                 ELSIF l_fw_consume_next IN (3,4) THEN
5230                                                         l_lowest_cust_priority := trunc(p_atp_period.Identifier2(x_dc_start_index(j)), -2);
5231                                                         l_lowest_site_priority := p_atp_period.Identifier2(x_dc_start_index(j));
5232                                                 END IF;
5233                                                 -- sysdate allocation percent was zero
5234                                                 l_fw_consume_tab(j) := l_fw_consume_next;
5235                                                 l_fw_consume_next := 0;
5236                                         ELSE
5237                                                 l_fw_consume_tab(j) := 1;
5238                                         END IF;
5239                                 ELSIF p_atp_period.Class(x_dc_start_index(j)) <> p_atp_period.Class(x_dc_start_index(j+1)) THEN
5240                                         -- customer class changed
5241                                         IF trunc(p_atp_period.Identifier2(x_dc_start_index(j)),-3) <>
5242                                                 trunc(p_atp_period.Identifier2(x_dc_start_index(j+1)),-3) THEN
5243                                                 -- customer class priority changed
5244                                                 Exit;
5245                                         ELSE
5246                                                 l_lowest_cust_priority := null;
5247                                                 l_lowest_site_priority := null;
5248                                                 IF p_atp_period.Identifier4(x_dc_start_index(j)) = 0 THEN
5249                                                         -- sysdate allocation percent is zero
5250                                                         l_fw_consume_next := 3;
5251                                                         l_fw_consume_tab(j) := 1;
5252                                                 ELSE
5253                                                         l_fw_consume_tab(j) := 3;
5254                                                         l_lowest_cust_priority := trunc(p_atp_period.Identifier2(x_dc_start_index(j)), -2);
5255                                                         l_lowest_site_priority := p_atp_period.Identifier2(x_dc_start_index(j));
5256                                                 END IF;
5257                                         END IF;
5258                                 ELSIF p_atp_period.Customer_Id(x_dc_start_index(j)) <> p_atp_period.Customer_Id(x_dc_start_index(j+1)) THEN
5259                                         -- customer changed
5260                                         --reset the lowest site priority at this level to null
5261                                         l_lowest_site_priority := null;
5262                                         IF trunc(p_atp_period.Identifier2(x_dc_start_index(j)),-2) =
5263                                                 trunc(p_atp_period.Identifier2(x_dc_start_index(j+1)),-2)
5264                                                 AND (l_lowest_cust_priority IS NULL OR
5265                                                      l_lowest_cust_priority = trunc(p_atp_period.Identifier2(x_dc_start_index(j)),-2)) THEN
5266                                                 -- customer priority did not change
5267                                                 IF p_atp_period.Identifier4(x_dc_start_index(j)) = 0 THEN
5268                                                         -- sysdate allocation percent is zero
5269                                                         l_fw_consume_next := 2;
5270                                                         l_fw_consume_tab(j) := 1;
5271                                                 ELSE
5272                                                         l_fw_consume_tab(j) := 2;
5273                                                         l_lowest_site_priority := p_atp_period.Identifier2(x_dc_start_index(j));
5274                                                 END IF;
5275                                         END IF;
5276                                 ELSIF p_atp_period.Identifier2(x_dc_start_index(j)) =
5277                                         p_atp_period.Identifier2(x_dc_start_index(j+1))
5278                                         AND (l_lowest_site_priority IS NULL OR
5279                                              l_lowest_site_priority = p_atp_period.Identifier2(x_dc_start_index(j))) THEN
5280                                         -- customer site priority did not change
5281                                         l_fw_consume_tab(j) := 1;
5282                                 END IF;
5283 
5284                                 j := l_fw_consume_tab.prior(j);
5285 
5286                         END LOOP;
5287 
5288                         IF PG_DEBUG in ('Y', 'C') THEN
5289                                 FOR mm in 1..l_fw_consume_tab.COUNT LOOP
5290                                         msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'priotity:l_fw_consume_tab(' || mm || ') = '
5291                                                         || p_atp_period.Identifier2(x_dc_start_index(mm)) || ':'
5292                                                         || l_fw_consume_tab(mm));
5293                                 END LOOP;
5294                         END IF;
5295 
5296                 ELSE    -- IF G_ATP_FW_CONSUME_METHOD = 1 THEN
5297                         -- Steal from any priority
5298 
5299                         -- store lowest priority
5300                         l_lowest_priority := trunc(p_atp_period.Identifier2(x_dc_start_index(l_dc_count)),-3);
5301 
5302                 END IF;
5303 
5304                 -- this for loop will do the demand class consumption
5305                 FOR j in 1..l_dc_count LOOP
5306 
5307                         l_start_index := x_dc_start_index(j);
5308                         l_end_index := x_dc_end_index(j);
5309 
5310                         IF PG_DEBUG in ('Y', 'C') THEN
5311                                 msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'DC changed. limits : ' || l_start_index || ',' || l_end_index);
5312                         END IF;
5313 
5314                         IF (j > 1) THEN
5315                                 IF p_atp_period.Class(x_dc_start_index(j)) <> p_atp_period.Class(x_dc_start_index(j-1)) THEN
5316                                         IF PG_DEBUG in ('Y', 'C') THEN
5317                                                 msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'Class changing. Changing l_class_next_steal_atp');
5318                                         END IF;
5319                                         -- class_next += curr + next + partner_curr + partner_next
5320                                         MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_current_steal_atp, l_class_next_steal_atp);
5321                                         MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_next_steal_atp, l_class_next_steal_atp);
5322                                         MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_partner_curr_steal_atp, l_class_next_steal_atp);
5323                                         MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_partner_next_steal_atp, l_class_next_steal_atp);
5324 
5325                                         l_current_steal_atp := l_null_steal_atp;
5326                                         l_next_steal_atp := l_null_steal_atp;
5327                                         l_partner_curr_steal_atp := l_null_steal_atp;
5328                                         l_partner_next_steal_atp := l_null_steal_atp;
5329 
5330                                         IF trunc(p_atp_period.Identifier2(x_dc_start_index(j)),-3) > trunc(p_atp_period.Identifier2(x_dc_start_index(j-1)),-3) THEN
5331                                                 IF PG_DEBUG in ('Y', 'C') THEN
5332                                                         msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'Class priority changing. Changing l_class_curr_steal_atp');
5333                                                 END IF;
5334                                                 -- class_curr += class_next
5335                                                 MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_class_next_steal_atp, l_class_curr_steal_atp);
5336                                                 l_class_next_steal_atp := l_null_steal_atp;
5337                                         END IF;
5338 
5339                                 ELSIF p_atp_period.Customer_Id(x_dc_start_index(j)) <> p_atp_period.Customer_Id(x_dc_start_index(j-1)) THEN
5340                                         IF PG_DEBUG in ('Y', 'C') THEN
5341                                                 msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'Customer changing. Changing l_partner_next_steal_atp');
5342                                         END IF;
5343                                         -- partner_next += curr + next
5344                                         MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_current_steal_atp, l_partner_next_steal_atp);
5345                                         MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_next_steal_atp, l_partner_next_steal_atp);
5346 
5347                                         l_current_steal_atp := l_null_steal_atp;
5348                                         l_next_steal_atp := l_null_steal_atp;
5349 
5350                                         IF trunc(p_atp_period.Identifier2(x_dc_start_index(j)),-2) > trunc(p_atp_period.Identifier2(x_dc_start_index(j-1)),-2) THEN
5351                                                 IF PG_DEBUG in ('Y', 'C') THEN
5352                                                         msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'Customer priority changing. Changing l_partner_curr_steal_atp');
5353                                                 END IF;
5354                                                 -- partner_curr += partner_next
5355                                                 MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_partner_next_steal_atp, l_partner_curr_steal_atp);
5356                                                 l_partner_next_steal_atp := l_null_steal_atp;
5357                                         END IF;
5358 
5359                                 ELSIF p_atp_period.Identifier2(x_dc_start_index(j)) > p_atp_period.Identifier2(x_dc_start_index(j-1)) THEN
5360                                         IF PG_DEBUG in ('Y', 'C') THEN
5361                                                 msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'Priority changing. Changing l_current_steal_atp');
5362                                         END IF;
5363                                         -- curr += next
5364                                         MSC_AATP_PVT.Add_to_Next_Steal_Atp(l_next_steal_atp, l_current_steal_atp);
5365                                         l_next_steal_atp := l_null_steal_atp;
5366                                 END IF;
5367                         END IF;
5368 
5369                         IF p_atp_period.Class(x_dc_start_index(j)) <> p_atp_period.Class(1) THEN
5370                                 -- We need to do demand class consumption with l_class_curr_steal_atp only
5371                                 -- if we are not in the first demand class
5372                                 IF PG_DEBUG in ('Y', 'C') THEN
5373                                         msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'Before doing DC consumption with l_class_curr_steal_atp.');
5374                                 END IF;
5375                                 Demand_Class_Consumption(p_atp_period, l_start_index,
5376                                         l_end_index, l_class_curr_steal_atp, l_return_status);
5377                                 IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5378                                         IF PG_DEBUG in ('Y', 'C') THEN
5379                                                 msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'Error occured in procedure Demand_Class_Consumption');
5380                                         END IF;
5381                                         x_return_status := FND_API.G_RET_STS_ERROR;
5382                                         return;
5383                                 END IF;
5384                                 IF PG_DEBUG in ('Y', 'C') THEN
5385                                         msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'After doing DC consumption with l_class_curr_steal_atp.');
5386                                 END IF;
5387                         END IF;
5388 
5389                         -- Customer_Id is not unique because it is -1 for 'Other' at each level.
5390                         -- Therefore class also needs to be in the condition.
5391                         IF (p_atp_period.Class(x_dc_start_index(j)) <> p_atp_period.Class(1)) OR
5392                                 (p_atp_period.Customer_Id(x_dc_start_index(j)) <> p_atp_period.Customer_Id(1)) THEN
5393                                 -- We need to do demand class consumption with l_partner_curr_steal_atp only
5394                                 -- if we are not in the first customer
5395                                 IF PG_DEBUG in ('Y', 'C') THEN
5396                                         msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'Before doing DC consumption with l_partner_curr_steal_atp.');
5397                                 END IF;
5398                                 Demand_Class_Consumption(p_atp_period, l_start_index,
5399                                         l_end_index, l_partner_curr_steal_atp, l_return_status);
5400                                 IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5401                                         IF PG_DEBUG in ('Y', 'C') THEN
5402                                                 msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'Error occured in procedure Demand_Class_Consumption');
5403                                         END IF;
5404                                         x_return_status := FND_API.G_RET_STS_ERROR;
5405                                         return;
5406                                 END IF;
5407                                 IF PG_DEBUG in ('Y', 'C') THEN
5408                                         msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'After doing DC consumption with l_partner_curr_steal_atp.');
5409                                 END IF;
5410                         END IF;
5411 
5412                         IF p_atp_period.Identifier2(x_dc_start_index(j)) <> p_atp_period.Identifier2(1) THEN
5413                                 -- We need to do demand class consumption only if we are not in
5414                                 -- the highest priority
5415                                 IF PG_DEBUG in ('Y', 'C') THEN
5416                                         msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'Before doing DC consumption.');
5417                                 END IF;
5418                                 Demand_Class_Consumption(p_atp_period, l_start_index,
5419                                         l_end_index, l_current_steal_atp, l_return_status);
5420                                 IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5421                                         IF PG_DEBUG in ('Y', 'C') THEN
5422                                                 msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'Error occured in procedure Demand_Class_Consumption');
5423                                         END IF;
5424                                         x_return_status := FND_API.G_RET_STS_ERROR;
5425                                         return;
5426                                 END IF;
5427                                 IF PG_DEBUG in ('Y', 'C') THEN
5428                                         msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'After doing DC consumption.');
5429                                 END IF;
5430                         END IF;
5431 
5432                         IF G_ATP_FW_CONSUME_METHOD = 1 THEN
5433                                 -- steal from lower priority
5434                                 IF l_fw_consume_tab(j) = 0 THEN
5435                                         -- No need to do f/w consumption, but we need to add to next steal
5436                                         Add_to_Next_Steal(p_atp_period, l_start_index, l_end_index, l_next_steal_atp, l_return_status);
5437                                         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5438                                                 IF PG_DEBUG in ('Y', 'C') THEN
5439                                                         msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'Error occured in procedure Add_to_Next_Steal');
5440                                                 END IF;
5441                                                 x_return_status := FND_API.G_RET_STS_ERROR;
5442                                                 return;
5443                                         END IF;
5444                                 ELSIF l_fw_consume_tab(j) = 1 THEN
5445                                         -- Do f/w consumption for only one's own negatives
5446                                                 Forward_Consume(p_atp_period, l_start_index, l_end_index, l_return_status);
5447                                                 IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5448                                                         IF PG_DEBUG in ('Y', 'C') THEN
5449                                                                 msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'Error occured in procedure Forward_Consume');
5450                                                         END IF;
5451                                                         x_return_status := FND_API.G_RET_STS_ERROR;
5452                                                         return;
5453                                                 END IF;
5454                                 ELSIF l_fw_consume_tab(j) = 2 THEN
5455                                         -- Do f/w consumption for only one's own negatives + negatives from higher
5456                                         -- priority customer sites under the same customer
5457                                         Add_to_Current_Atp(p_atp_period, l_start_index, l_end_index, l_current_steal_atp, l_return_status);
5458                                         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5459                                                 IF PG_DEBUG in ('Y', 'C') THEN
5460                                                         msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'Error occured in procedure Add_to_Current_Atp');
5461                                                 END IF;
5462                                                 x_return_status := FND_API.G_RET_STS_ERROR;
5463                                                 return;
5464                                         END IF;
5465 
5466                                         l_current_steal_atp := l_null_steal_atp;
5467 
5468                                         Forward_Consume(p_atp_period, l_start_index, l_end_index, l_return_status);
5469                                         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5470                                                 IF PG_DEBUG in ('Y', 'C') THEN
5471                                                         msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'Error occured in procedure Forward_Consume');
5472                                                 END IF;
5473                                                 x_return_status := FND_API.G_RET_STS_ERROR;
5474                                                 return;
5475                                         END IF;
5476                                 ELSIF l_fw_consume_tab(j) = 3 THEN
5477                                         -- Do f/w consumption for only one's own negatives + negatives from higher
5478                                         -- priority customer sites under the same customer + negatives from higher
5479                                         -- priority customers under the same customer class
5480                                         Add_to_Current_Atp(p_atp_period, l_start_index, l_end_index, l_current_steal_atp, l_return_status);
5481                                         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5482                                                 IF PG_DEBUG in ('Y', 'C') THEN
5483                                                         msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'Error occured in procedure Add_to_Current_Atp');
5484                                                 END IF;
5485                                                 x_return_status := FND_API.G_RET_STS_ERROR;
5486                                                 return;
5487                                         END IF;
5488 
5489                                         Add_to_Current_Atp(p_atp_period, l_start_index, l_end_index, l_partner_curr_steal_atp, l_return_status);
5490                                         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5491                                                 IF PG_DEBUG in ('Y', 'C') THEN
5492                                                         msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'Error occured in procedure Add_to_Current_Atp');
5493                                                 END IF;
5494                                                 x_return_status := FND_API.G_RET_STS_ERROR;
5495                                                 return;
5496                                         END IF;
5497 
5498                                         l_current_steal_atp := l_null_steal_atp;
5499                                         l_partner_curr_steal_atp := l_null_steal_atp;
5500 
5501                                         Forward_Consume(p_atp_period, l_start_index, l_end_index, l_return_status);
5502                                         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5503                                                 IF PG_DEBUG in ('Y', 'C') THEN
5504                                                         msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'Error occured in procedure Forward_Consume');
5505                                                 END IF;
5506                                                 x_return_status := FND_API.G_RET_STS_ERROR;
5507                                                 return;
5508                                         END IF;
5509                                 ELSE
5510                                         -- Do f/w consumption for only one's own negatives + negatives from higher
5511                                         -- priority customer sites under the same customer + negatives from higher
5512                                         -- priority customers under the same customer class + negatives from higher
5513                                         -- priority customer classes
5514                                         Add_to_Current_Atp(p_atp_period, l_start_index, l_end_index, l_current_steal_atp, l_return_status);
5515                                         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5516                                                 IF PG_DEBUG in ('Y', 'C') THEN
5517                                                         msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'Error occured in procedure Add_to_Current_Atp');
5518                                                 END IF;
5519                                                 x_return_status := FND_API.G_RET_STS_ERROR;
5520                                                 return;
5521                                         END IF;
5522 
5523                                         Add_to_Current_Atp(p_atp_period, l_start_index, l_end_index, l_partner_curr_steal_atp, l_return_status);
5524                                         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5525                                                 IF PG_DEBUG in ('Y', 'C') THEN
5526                                                         msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'Error occured in procedure Add_to_Current_Atp');
5527                                                 END IF;
5528                                                 x_return_status := FND_API.G_RET_STS_ERROR;
5529                                                 return;
5530                                         END IF;
5531 
5532                                         Add_to_Current_Atp(p_atp_period, l_start_index, l_end_index, l_class_curr_steal_atp, l_return_status);
5533                                         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5534                                                 IF PG_DEBUG in ('Y', 'C') THEN
5535                                                         msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'Error occured in procedure Add_to_Current_Atp');
5536                                                 END IF;
5537                                                 x_return_status := FND_API.G_RET_STS_ERROR;
5538                                                 return;
5539                                         END IF;
5540 
5541                                         l_current_steal_atp := l_null_steal_atp;
5542                                         l_partner_curr_steal_atp := l_null_steal_atp;
5543                                         l_class_curr_steal_atp := l_null_steal_atp;
5544 
5545                                         Forward_Consume(p_atp_period, l_start_index, l_end_index, l_return_status);
5546                                         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5547                                                 IF PG_DEBUG in ('Y', 'C') THEN
5548                                                         msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'Error occured in procedure Forward_Consume');
5549                                                 END IF;
5550                                                 x_return_status := FND_API.G_RET_STS_ERROR;
5551                                                 return;
5552                                         END IF;
5553                                 END IF;
5554                                 Remove_Negatives(p_atp_period, l_start_index, l_end_index, l_return_status);
5555                                 IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5556                                         IF PG_DEBUG in ('Y', 'C') THEN
5557                                                 msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'Error occured in procedure Remove_Negatives');
5558                                         END IF;
5559                                         x_return_status := FND_API.G_RET_STS_ERROR;
5560                                         return;
5561                                 END IF;
5562                         ELSE    -- IF G_ATP_FW_CONSUME_METHOD = 1 THEN
5563                                 -- steal from any priority
5564                                 -- For Method 2 Add_to_Next_Steal is called even for all DCs. This is an extra
5565                                 -- overhead when compared to simply calling Remove_Nagatives for lowest priority DC.
5566                                 -- But this will provide an OK solution for the corner case when the actual lowest
5567                                 -- priority DC (which can be at a lower priority that l_lowest_priority) has
5568                                 -- 0 allocation % on sysdate but non-zero on some future date.
5569                                 IF p_atp_period.Identifier2(x_dc_start_index(j)) <> l_lowest_priority THEN
5570                                         -- This is not the lowest priority DC.
5571                                         -- Need to add to next steal
5572                                         Add_to_Next_Steal(p_atp_period, l_start_index,
5573                                                 l_end_index, l_next_steal_atp, l_return_status);
5574                                         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5575                                                 IF PG_DEBUG in ('Y', 'C') THEN
5576                                                         msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'Error occured in procedure Add_to_Next_Steal');
5577                                                 END IF;
5578                                                 x_return_status := FND_API.G_RET_STS_ERROR;
5579                                                 return;
5580                                         END IF;
5581                                 ELSE
5582                                         Remove_Negatives(p_atp_period, l_start_index, l_end_index, l_return_status);
5583                                         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5584                                                 IF PG_DEBUG in ('Y', 'C') THEN
5585                                                         msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'Error occured in procedure Remove_Negatives');
5586                                                 END IF;
5587                                                 x_return_status := FND_API.G_RET_STS_ERROR;
5588                                                 return;
5589                                         END IF;
5590                                 END IF;
5591 
5592                                 Compute_Cum_Individual(p_atp_period, l_start_index, l_end_index, l_return_status);
5593                                 IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5594                                         IF PG_DEBUG in ('Y', 'C') THEN
5595                                                 msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'Error occured in procedure Compute_Cum_Individual');
5596                                         END IF;
5597                                         x_return_status := FND_API.G_RET_STS_ERROR;
5598                                         return;
5599                                 END IF;
5600 
5601                                 Adjust_Cum(p_atp_period, l_start_index, l_end_index,
5602                                         x_dc_start_index(x_dc_list_tab.COUNT), x_dc_end_index(x_dc_list_tab.COUNT), l_return_status);
5603                                 IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5604                                         IF PG_DEBUG in ('Y', 'C') THEN
5605                                                 msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'Error occured in procedure Adjust_Cum');
5606                                         END IF;
5607                                         x_return_status := FND_API.G_RET_STS_ERROR;
5608                                         return;
5609                                 END IF;
5610 
5611                                 IF PG_DEBUG in ('Y', 'C') THEN
5612                                         FOR mm in l_start_index..l_end_index LOOP
5613                                                 msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'adjusted cum date:qty - '||
5614                                                         p_atp_period.Period_Start_Date(mm) ||' : '|| p_atp_period.Adjusted_Cum_Quantity(mm) );
5615                                         END LOOP;
5616                                 END IF;
5617                         END IF;
5618 
5619                         IF PG_DEBUG in ('Y', 'C') THEN
5620                                 FOR mm in l_start_index..l_end_index LOOP
5621                                         msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'adjusted date:qty - '||
5622                                                 p_atp_period.Period_Start_Date(mm) ||' : '|| p_atp_period.Adjusted_Availability_Quantity(mm) );
5623                                 END LOOP;
5624                         END IF;
5625                 END LOOP;
5626 
5627         END IF; -- ELSIF (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) THEN
5628 
5629         IF G_ATP_FW_CONSUME_METHOD = 1 THEN
5630                 -- steal from lower priority
5631 
5632                 -- Copy Cumulative Quantity from Adjusted_Availability_Quantity
5633                 p_atp_period.Cumulative_Quantity := p_atp_period.Adjusted_Availability_Quantity;
5634 
5635                 -- Call procedure Compute_Cum to do accumulation
5636                 Compute_Cum(p_atp_period, x_dc_list_tab, x_dc_start_index, x_dc_end_index, l_return_status);
5637                 IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5638                         IF PG_DEBUG in ('Y', 'C') THEN
5639                                 msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  'Error occured in procedure Compute_Cum');
5640                         END IF;
5641                         x_return_status := FND_API.G_RET_STS_ERROR;
5642                         return;
5643                 END IF;
5644 
5645         END IF;
5646 
5647         IF PG_DEBUG in ('Y', 'C') THEN
5648                 msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' ||  '**********End Adjust_Allocation_Details Procedure************');
5649         END IF;
5650 
5651 EXCEPTION
5652 WHEN OTHERS THEN
5653         IF PG_DEBUG in ('Y', 'C') THEN
5654                 msc_sch_wb.atp_debug('Adjust_Allocation_Details: ' || 'Error occurred: ' || to_char(sqlcode) || ':' || SQLERRM);
5655         END IF;
5656         x_return_status := FND_API.G_RET_STS_ERROR;
5657         Set_Error(MSC_ATP_PVT.ATP_PROCESSING_ERROR);
5658 END Adjust_Allocation_Details;
5659 
5660 
5661 
5662 /*--Demand_Class_Consumption-----------------------------------------------
5663 |  o Performs demand class consumption.
5664 +-------------------------------------------------------------------------*/
5665 PROCEDURE Demand_Class_Consumption(
5666         p_atp_period                    IN OUT  NOCOPY  MRP_ATP_PUB.ATP_Period_Typ,
5667         p_start_index                   IN              number,
5668         p_end_index                     IN              number,
5669         p_steal_atp                     IN OUT  NOCOPY  MRP_ATP_PVT.ATP_Info,
5670         x_return_status                 OUT     NOCOPY  VARCHAR2)
5671 IS
5672         i                       PLS_INTEGER; -- index for p_steal_atp
5673         j                       PLS_INTEGER; -- index for p_atp_period
5674         k                       PLS_INTEGER; -- starting point for consumption of p_current_atp
5675         m                       PLS_INTEGER;
5676         l_adjustment_quantity   number;
5677         -- time_phased_atp
5678         l_atf_date      DATE := MSC_ATP_ALLOC.G_ATF_Date;
5679 BEGIN
5680         IF PG_DEBUG in ('Y', 'C') THEN
5681                 msc_sch_wb.atp_debug('Demand_Class_Consumption: ' ||  '*********Begin procedure Demand_Class_Consumption ********');
5682                 m := p_start_index;
5683                 WHILE (m is not null) AND (m <= p_end_index) LOOP
5684                         msc_sch_wb.atp_debug('Demand_Class_Consumption: ' ||  'current date:qty - '||
5685                                 p_atp_period.Period_Start_Date(m) ||' : '|| p_atp_period.Adjusted_Availability_Quantity(m) );
5686                         m := p_atp_period.Period_Start_Date.Next(m);
5687                 END LOOP;
5688 
5689                 m := p_steal_atp.atp_period.FIRST;
5690                 WHILE m is not null LOOP
5691                         msc_sch_wb.atp_debug('Demand_Class_Consumption: ' ||  'steal date:qty '||
5692                                 p_steal_atp.atp_period(m) ||' : '|| p_steal_atp.atp_qty(m));
5693                         m := p_steal_atp.atp_period.Next(m);
5694                 END LOOP;
5695         END IF;
5696 
5697         -- Initializing API return code
5698         x_return_status := FND_API.G_RET_STS_SUCCESS;
5699 
5700         k := p_start_index;
5701 
5702         -- i is the index for steal_atp
5703         FOR i in 1..p_steal_atp.atp_qty.COUNT LOOP
5704 
5705                 -- consume current_atp (backward) if we have neg in steal_atp
5706                 IF (p_steal_atp.atp_qty(i) < 0 ) THEN
5707 
5708                         k := NVL(k, 1); --  if k is null, make it as 1 so that
5709                                         --  we can find the starting point for the first
5710                                         --  element.
5711 
5712                         WHILE (k IS NOT NULL)  LOOP
5713                                 IF k = p_end_index THEN
5714                                         -- this is the last record
5715                                         IF (p_atp_period.Period_Start_Date(k) > p_steal_atp.atp_period(i)) THEN
5716                                                 -- cannot do any consumption since the date from p_steal_atp
5717                                                 -- is greater than p_ccurrent_atp
5718                                                 k := NULL;
5719                                         END IF;
5720                                         EXIT; -- exit the loop since this is the last record
5721                                 ELSE
5722                                         -- this is not the last record
5723                                         IF ((p_atp_period.Period_Start_Date(k) <= p_steal_atp.atp_period(i))
5724                                                 AND (p_atp_period.Period_Start_Date(k+1) > p_steal_atp.atp_period(i))) THEN
5725                                                 -- this is the starting point, we can exit now
5726                                                 IF PG_DEBUG in ('Y', 'C') THEN
5727                                                         msc_sch_wb.atp_debug('Demand_Class_Consumption: ' ||  'exit at k = ' ||to_char(k)||' and i = ' ||to_char(i));
5728                                                 END IF;
5729                                                 EXIT;
5730                                         ELSE
5731                                                 k := p_atp_period.Period_Start_Date.NEXT(k);
5732                                         END IF;
5733                                 END IF;
5734                         END LOOP;
5735 
5736                         j:= k;
5737 
5738                         WHILE (NVL(j, -1) >= p_start_index) LOOP
5739                                 -- time_phased_atp
5740                                 IF ((l_atf_date is not null) and (p_steal_atp.atp_period(i)>l_atf_date) and (p_atp_period.Period_Start_Date(j)<=l_atf_date)) THEN
5741                                         -- exit loop when crossing time fence
5742                                         j := 0;
5743                                 ELSIF (p_atp_period.Adjusted_Availability_Quantity(j) < 0) THEN
5744                                         -- Since backward consumption has been done, a negative here
5745                                         -- means that all previous would be negative. So no need to continue
5746                                         j := -1;
5747                                 ELSIF (p_atp_period.Adjusted_Availability_Quantity(j) =0) THEN
5748                                         --  backward one more period
5749                                         j := j-1 ;
5750                                 ELSE
5751                                         -- There will be some adjustment
5752                                         IF (p_atp_period.Adjusted_Availability_Quantity(j) + p_steal_atp.atp_qty(i)< 0) THEN
5753                                                 -- not enough to cover the shortage
5754                                                 p_steal_atp.atp_qty(i) := p_steal_atp.atp_qty(i) +
5755                                                         p_atp_period.Adjusted_Availability_Quantity(j);
5756                                                 l_adjustment_quantity := - p_atp_period.Adjusted_Availability_Quantity(j);
5757                                                 p_atp_period.Adjusted_Availability_Quantity(j) := 0;
5758                                         ELSE
5759                                                 -- enough to cover the shortage
5760                                                 p_atp_period.Adjusted_Availability_Quantity(j) := p_steal_atp.atp_qty(i) +
5761                                                         p_atp_period.Adjusted_Availability_Quantity(j);
5762                                                 l_adjustment_quantity := p_steal_atp.atp_qty(i);
5763                                                 p_steal_atp.atp_qty(i) := 0;
5764                                         END IF;
5765 
5766                                         -- Update demand adjustment quantity
5767                                         m := j;
5768                                         WHILE (m is not null) AND (m <= p_end_index) LOOP
5769                                                 IF p_atp_period.Period_Start_Date(m) = p_steal_atp.atp_period(i) THEN
5770                                                         p_atp_period.Demand_Adjustment_Quantity(m) := p_atp_period.Demand_Adjustment_Quantity(m) +
5771                                                                 l_adjustment_quantity;
5772                                                         EXIT;
5773                                                 END IF;
5774                                                 m := p_atp_period.Period_Start_Date.Next(m);
5775                                         END LOOP;
5776 
5777                                         IF p_steal_atp.atp_qty(i) = 0 THEN
5778                                                 -- shortage has been covered
5779                                                 j := -1;
5780                                         ELSE
5781                                                 -- shortage has not been covered
5782                                                 j := j - 1;
5783                                         END IF;
5784                                 END IF;
5785                         END LOOP;
5786                 END IF;
5787         END LOOP;
5788 
5789         IF PG_DEBUG in ('Y', 'C') THEN
5790                 m := p_start_index;
5791                 WHILE (m is not null) AND (m <= p_end_index) LOOP
5792                         msc_sch_wb.atp_debug('Demand_Class_Consumption: ' ||  'current date:qty - '||
5793                                 p_atp_period.Period_Start_Date(m) ||' : '|| p_atp_period.Adjusted_Availability_Quantity(m) );
5794                         m := p_atp_period.Period_Start_Date.Next(m);
5795                 END LOOP;
5796 
5797                 m := p_steal_atp.atp_period.FIRST;
5798                 WHILE m is not null LOOP
5799                         msc_sch_wb.atp_debug('Demand_Class_Consumption: ' ||  'steal date:qty '||
5800                                 p_steal_atp.atp_period(m) ||' : '|| p_steal_atp.atp_qty(m));
5801                         m := p_steal_atp.atp_period.Next(m);
5802                 END LOOP;
5803 
5804                 msc_sch_wb.atp_debug('Demand_Class_Consumption: ' ||  '*********End procedure Demand_Class_Consumption ********');
5805         END IF;
5806 
5807 EXCEPTION
5808 WHEN OTHERS THEN
5809         IF PG_DEBUG in ('Y', 'C') THEN
5810                 msc_sch_wb.atp_debug('Demand_Class_Consumption: ' || 'Error occurred: ' || to_char(sqlcode) || ':' || SQLERRM);
5811         END IF;
5812         x_return_status := FND_API.G_RET_STS_ERROR;
5813         Set_Error(MSC_ATP_PVT.ATP_PROCESSING_ERROR);
5814 END Demand_Class_Consumption;
5815 
5816 
5817 /*--Add_to_Next_Steal------------------------------------------------------
5818 |  o Same as MSC_AATP_PVT.Add_to_Next_Steal_Atp except for the input
5819 |    parameters.
5820 +-------------------------------------------------------------------------*/
5821 PROCEDURE Add_to_Next_Steal(
5822         p_atp_period                    IN OUT  NOCOPY  MRP_ATP_PUB.ATP_Period_Typ,
5823         p_start_index                   IN              number,
5824         p_end_index                     IN              number,
5825         p_next_steal_atp                IN OUT  NOCOPY  MRP_ATP_PVT.ATP_Info,
5826         x_return_status                 OUT     NOCOPY  VARCHAR2)
5827 IS
5828         i                       PLS_INTEGER;            -- index for p_atp_period
5829         j                       PLS_INTEGER;            -- index for p_next_steal_atp
5830         k                       PLS_INTEGER;            -- index for l_next_steal_atp
5831         n                       PLS_INTEGER;            -- starting point of p_next_steal_atp
5832         l_next_steal_atp        MRP_ATP_PVT.ATP_Info;   -- this will be the output
5833         l_processed             BOOLEAN := FALSE ;
5834 BEGIN
5835         -- this procedure will combine p_atp_period and p_next_steal_atp to form
5836         -- a new record of tables and then return as p_next_steal_atp.
5837         -- they need to be ordered by date.
5838         -- The only difference between this version of the procedure and the one in MSCAATPB is that
5839         -- this accepts start/end indices to restrict the operation
5840 
5841         -- Initializing API return code
5842         x_return_status := FND_API.G_RET_STS_SUCCESS;
5843 
5844         IF PG_DEBUG in ('Y', 'C') THEN
5845                 msc_sch_wb.atp_debug('Add_to_Next_Steal: ' ||  '*********Begin procedure Add_to_Next_Steal ********');
5846 
5847                 i := p_start_index;
5848                 WHILE (i is not null) AND (i <= p_end_index) LOOP
5849                         msc_sch_wb.atp_debug('Add_to_Next_Steal: ' ||  'current date:qty - '||
5850                                 p_atp_period.Period_Start_Date(i) ||' : '|| p_atp_period.Adjusted_Availability_Quantity(i) );
5851                         i := p_atp_period.Period_Start_Date.Next(i);
5852                 END LOOP;
5853 
5854                 i := p_next_steal_atp.atp_period.FIRST;
5855                 WHILE i is not null LOOP
5856                         msc_sch_wb.atp_debug('Add_to_Next_Steal: ' ||  'steal date:qty '||
5857                                 p_next_steal_atp.atp_period(i) ||' : '|| p_next_steal_atp.atp_qty(i));
5858                         i := p_next_steal_atp.atp_period.Next(i);
5859                 END LOOP;
5860         END IF;
5861 
5862         j := p_next_steal_atp.atp_period.FIRST;
5863         k := 0;
5864         FOR i IN p_start_index..p_end_index LOOP
5865                 IF p_atp_period.Adjusted_Availability_Quantity(i) < 0 THEN
5866                         l_processed := FALSE;
5867                         WHILE (j IS NOT NULL) LOOP
5868                                 IF p_next_steal_atp.atp_qty(j) < 0 THEN
5869 
5870                                         k := k+1;
5871                                         l_next_steal_atp.atp_period.Extend;
5872                                         l_next_steal_atp.atp_qty.Extend;
5873 
5874                                         IF p_next_steal_atp.atp_period(j) < p_atp_period.Period_Start_Date(i) THEN
5875 
5876                                                 -- we add this to l_next_steal_atp
5877                                                 l_next_steal_atp.atp_period(k) := p_next_steal_atp.atp_period(j);
5878                                                 l_next_steal_atp.atp_qty(k) := p_next_steal_atp.atp_qty(j);
5879 
5880                                         ELSIF p_next_steal_atp.atp_period(j)=p_atp_period.Period_Start_Date(i) THEN
5881 
5882                                                 -- both record (p_next_steal_atp and p_atp_period) are on the same
5883                                                 -- date.  we need to sum them up
5884                                                 l_processed := TRUE;
5885                                                 l_next_steal_atp.atp_period(k) := p_next_steal_atp.atp_period(j);
5886                                                 l_next_steal_atp.atp_qty(k) := p_next_steal_atp.atp_qty(j) +
5887                                                         p_atp_period.Adjusted_Availability_Quantity(i);
5888                                                 p_atp_period.Adjusted_Availability_Quantity(i) := 0;
5889                                                 -- to show non-negative availability and to remove negative accumulation
5890                                                 j := p_next_steal_atp.atp_period.NEXT(j);
5891                                                 EXIT;
5892                                                 -- subsequent records will be covered in next iteration of the outer
5893                                                 -- FOR loop. so we don't need to go to next record any more
5894                                         ELSE -- this is the greater part
5895                                                 l_processed := TRUE;
5896                                                 l_next_steal_atp.atp_period(k) := p_atp_period.Period_Start_Date(i);
5897                                                 l_next_steal_atp.atp_qty(k) := p_atp_period.Adjusted_Availability_Quantity(i);
5898                                                 p_atp_period.Adjusted_Availability_Quantity(i) := 0;
5899                                                 -- to show non-negative availability and to remove negative accumulation
5900                                                 EXIT;
5901                                                 -- subsequent records will be covered in next iteration of the outer
5902                                                 -- FOR loop. so we don't need to go to next record any more
5903                                                 -- Also j is not incremented as p_next_steal_atp(j)'s negative
5904                                                 -- has not been considered as yet.
5905                                         END IF;
5906                                 END IF; -- p_next_steal_atp.atp_qty < 0
5907 
5908                                 j := p_next_steal_atp.atp_period.NEXT(j) ;
5909                         END LOOP;
5910 
5911                         IF (j is null) AND (l_processed = FALSE) THEN
5912                                 -- this means p_next_steal_atp is over,
5913                                 -- so we don't need to worry about p_next_steal_atp,
5914                                 -- we just keep add p_atp_period to l_next_steal_atp
5915                                 -- if they are not added before
5916                                 k := k+1;
5917                                 l_next_steal_atp.atp_period.Extend;
5918                                 l_next_steal_atp.atp_qty.Extend;
5919 
5920                                 l_next_steal_atp.atp_period(k) := p_atp_period.Period_Start_Date(i);
5921                                 l_next_steal_atp.atp_qty(k) := p_atp_period.Adjusted_Availability_Quantity(i);
5922                                 p_atp_period.Adjusted_Availability_Quantity(i) := 0;
5923                                 -- to show non-negative availability and to remove negative accumulation
5924                         END IF;
5925 
5926                 END IF; -- p_current_atp.atp_qty < 0
5927         END LOOP;
5928 
5929         -- now we have taken care of all p_atp_period and part of
5930         -- p_next_steal_atp. now we need to take care the rest of p_next_steal_atp
5931 
5932         WHILE j is not null LOOP
5933                 IF p_next_steal_atp.atp_qty(j) < 0 THEN
5934                         -- we add this to l_next_steal_atp
5935                         k := k+1;
5936                         l_next_steal_atp.atp_period.Extend;
5937                         l_next_steal_atp.atp_qty.Extend;
5938                         l_next_steal_atp.atp_period(k) := p_next_steal_atp.atp_period(j);
5939                         l_next_steal_atp.atp_qty(k) := p_next_steal_atp.atp_qty(j);
5940                 END IF;
5941                 j := p_next_steal_atp.atp_period.NEXT(j);
5942         END LOOP;
5943 
5944         p_next_steal_atp := l_next_steal_atp;
5945 
5946         IF PG_DEBUG in ('Y', 'C') THEN
5947                 i := p_start_index;
5948                 WHILE (i is not null) AND (i <= p_end_index) LOOP
5949                         msc_sch_wb.atp_debug('Add_to_Next_Steal: ' ||  'current date:qty - '||
5950                                 p_atp_period.Period_Start_Date(i) ||' : '|| p_atp_period.Adjusted_Availability_Quantity(i) );
5951                         i := p_atp_period.Period_Start_Date.Next(i);
5952                 END LOOP;
5953 
5954                 i := p_next_steal_atp.atp_period.FIRST;
5955                 WHILE i is not null LOOP
5956                         msc_sch_wb.atp_debug('Add_to_Next_Steal: ' ||  'steal date:qty '||
5957                                 p_next_steal_atp.atp_period(i) ||' : '|| p_next_steal_atp.atp_qty(i));
5958                         i := p_next_steal_atp.atp_period.Next(i);
5959                 END LOOP;
5960 
5961                 msc_sch_wb.atp_debug('Add_to_Next_Steal: ' ||  '*********End procedure Add_to_Next_Steal ********');
5962         END IF;
5963 
5964 EXCEPTION
5965 WHEN OTHERS THEN
5966         IF PG_DEBUG in ('Y', 'C') THEN
5967                 msc_sch_wb.atp_debug('Add_to_Next_Steal: ' || 'Error occurred: ' || to_char(sqlcode) || ':' || SQLERRM);
5968         END IF;
5969         x_return_status := FND_API.G_RET_STS_ERROR;
5970         Set_Error(MSC_ATP_PVT.ATP_PROCESSING_ERROR);
5971 END Add_to_Next_Steal;
5972 
5973 
5974 
5975 /*--Add_to_Current_Atp-----------------------------------------------------
5976 |  o Same as MSC_AATP_PROC.Add_to_current_atp except for the input
5977 |    parameters.
5978 +-------------------------------------------------------------------------*/
5979 PROCEDURE Add_to_Current_Atp(
5980         p_atp_period                    IN OUT  NOCOPY  MRP_ATP_PUB.ATP_Period_Typ,
5981         p_start_index                   IN              number,
5982         p_end_index                     IN              number,
5983         p_steal_atp                     IN              MRP_ATP_PVT.ATP_Info,
5984         x_return_status                 OUT     NOCOPY  VARCHAR2)
5985 IS
5986         i                       PLS_INTEGER;            -- index for p_atp_period
5987         j                       PLS_INTEGER;            -- index for p_steal_atp
5988 BEGIN
5989         IF PG_DEBUG in ('Y', 'C') THEN
5990                 msc_sch_wb.atp_debug('Add_to_Current_Atp: ' ||  '*********Begin procedure Add_to_Current_Atp ********');
5991         END IF;
5992 
5993         -- Initializing API return code
5994         x_return_status := FND_API.G_RET_STS_SUCCESS;
5995 
5996         -- this procedure will add p_steal_atp's negatives in p_atp_period.
5997         -- It is assumed here that the dates in p_steal_atp is always a subset of dates in p_atp_period
5998 
5999         i := p_start_index;
6000         j := p_steal_atp.atp_period.FIRST;
6001         WHILE (j IS NOT NULL) LOOP
6002 
6003                 IF p_steal_atp.atp_qty(j) < 0 THEN
6004                         WHILE (i is not null) AND (i <= p_end_index) LOOP
6005                                 IF p_atp_period.Period_Start_Date(i) = p_steal_atp.atp_period(j) THEN
6006                                         p_atp_period.Demand_Adjustment_Quantity(i) := p_steal_atp.atp_qty(j) +
6007                                                 nvl(p_atp_period.Demand_Adjustment_Quantity(i),0);
6008                                         p_atp_period.Adjusted_Availability_Quantity(i) := p_steal_atp.atp_qty(j) +
6009                                                 p_atp_period.Adjusted_Availability_Quantity(i);
6010                                         EXIT;
6011                                 END IF;
6012                                 i := p_atp_period.Period_Start_Date.Next(i);
6013                         END LOOP;
6014                 END IF;
6015                 j := p_steal_atp.atp_period.Next(j);
6016         END LOOP;
6017 
6018         IF PG_DEBUG in ('Y', 'C') THEN
6019                 i := p_start_index;
6020                 WHILE (i is not null) AND (i <= p_end_index) LOOP
6021                         msc_sch_wb.atp_debug('Add_to_Current_Atp: ' ||  'current date:qty - '||
6022                                 p_atp_period.Period_Start_Date(i) ||' : '|| p_atp_period.Adjusted_Availability_Quantity(i) );
6023                         i := p_atp_period.Period_Start_Date.Next(i);
6024                 END LOOP;
6025 
6026                 i := p_steal_atp.atp_period.FIRST;
6027                 WHILE i is not null LOOP
6028                         msc_sch_wb.atp_debug('Add_to_Current_Atp: ' ||  'steal date:qty '||
6029                                 p_steal_atp.atp_period(i) ||' : '|| p_steal_atp.atp_qty(i));
6030                         i := p_steal_atp.atp_period.Next(i);
6031                 END LOOP;
6032 
6033                 msc_sch_wb.atp_debug('Add_to_Current_Atp: ' ||  '*********End procedure Add_to_Current_Atp ********');
6034         END IF;
6035 
6036 EXCEPTION
6037 WHEN OTHERS THEN
6038         IF PG_DEBUG in ('Y', 'C') THEN
6039                 msc_sch_wb.atp_debug('Add_to_Current_Atp: ' || 'Error occurred: ' || to_char(sqlcode) || ':' || SQLERRM);
6040         END IF;
6041         x_return_status := FND_API.G_RET_STS_ERROR;
6042         Set_Error(MSC_ATP_PVT.ATP_PROCESSING_ERROR);
6043 END Add_to_Current_Atp;
6044 
6045 
6046 /*--Remove_Negatives-------------------------------------------------------
6047 |  o Same as MSC_AATP_PROC.Atp_Remove_Negatives except for the input
6048 |    parameters.
6049 +-------------------------------------------------------------------------*/
6050 PROCEDURE Remove_Negatives(
6051         p_atp_period                    IN OUT  NOCOPY  MRP_ATP_PUB.ATP_Period_Typ,
6052         p_start_index                   IN              NUMBER,
6053         p_end_index                     IN              NUMBER,
6054         x_return_status                 OUT     NOCOPY  VARCHAR2)
6055 IS
6056         i                       PLS_INTEGER;            -- index for p_atp_period
6057 BEGIN
6058         IF PG_DEBUG in ('Y', 'C') THEN
6059                 msc_sch_wb.atp_debug('Remove_Negatives: ' || '*********Begin procedure Remove_Negatives ********');
6060         END IF;
6061 
6062         -- Initializing API return code
6063         x_return_status := FND_API.G_RET_STS_SUCCESS;
6064 
6065         -- this procedure will remove negatives
6066 
6067         FOR i IN p_start_index..p_end_index LOOP
6068                 IF p_atp_period.Adjusted_Availability_Quantity(i) < 0 THEN
6069                         p_atp_period.Adjusted_Availability_Quantity(i) := 0;
6070                 END IF;
6071         END LOOP;
6072 
6073         IF PG_DEBUG in ('Y', 'C') THEN
6074                 msc_sch_wb.atp_debug('Remove_Negatives: ' || '*********End procedure Remove_Negatives ********');
6075         END IF;
6076 
6077 EXCEPTION
6078 WHEN OTHERS THEN
6079         IF PG_DEBUG in ('Y', 'C') THEN
6080                 msc_sch_wb.atp_debug('Remove_Negatives: ' || 'Error occurred: ' || to_char(sqlcode) || ':' || SQLERRM);
6081         END IF;
6082         x_return_status := FND_API.G_RET_STS_ERROR;
6083         Set_Error(MSC_ATP_PVT.ATP_PROCESSING_ERROR);
6084 END Remove_Negatives;
6085 
6086 
6087 /*--Adjust_Cum-------------------------------------------------------------
6088 |  o Same as MSC_AATP_PROC.Atp_Adjusted_Cum except for the input
6089 |    parameters.
6090 +-------------------------------------------------------------------------*/
6091 PROCEDURE Adjust_Cum(
6092         p_atp_period                    IN OUT  NOCOPY  MRP_ATP_PUB.ATP_Period_Typ,
6093         p_cur_start_index               IN              NUMBER,
6094         p_cur_end_index                 IN              NUMBER,
6095         p_unalloc_start_index           IN              NUMBER,
6096         p_unalloc_end_index             IN              NUMBER,
6097         x_return_status                 OUT     NOCOPY  VARCHAR2)
6098 IS
6099         i                       PLS_INTEGER;
6100         j                       PLS_INTEGER;
6101 BEGIN
6102 
6103         -- Initializing API return code
6104         x_return_status := FND_API.G_RET_STS_SUCCESS;
6105 
6106         IF PG_DEBUG in ('Y', 'C') THEN
6107                 msc_sch_wb.atp_debug('Adjust_Cum: ' || '**********Begin Adjust_Cum Procedure************');
6108         END IF;
6109 
6110         --rajjain 02/11/2003 Bug 2793336 Begin
6111         i := p_unalloc_end_index;
6112         j := p_cur_end_index;
6113         WHILE i >= p_unalloc_start_index LOOP
6114                 -- do adjustment
6115                 p_atp_period.Adjusted_Cum_Quantity(j) := GREATEST(LEAST(p_atp_period.Cumulative_Quantity(j),
6116                         p_atp_period.Adjusted_Cum_Quantity(i)), 0);
6117                 p_atp_period.Adjusted_Cum_Quantity(i) := p_atp_period.Adjusted_Cum_Quantity(i) -
6118                         p_atp_period.Adjusted_Cum_Quantity(j);
6119 
6120                 IF i <> p_unalloc_end_index
6121                   AND p_atp_period.Adjusted_Cum_Quantity(i) > p_atp_period.Adjusted_Cum_Quantity(i+1)
6122                 THEN
6123                    IF PG_DEBUG in ('Y', 'C') THEN
6124                       msc_sch_wb.atp_debug ('Adjust_Cum: ' || 'Unallocated Cum date:qty - '||
6125                                 p_atp_period.Period_Start_Date(i) ||' : '|| p_atp_period.Adjusted_Cum_Quantity(i) );
6126                    END IF;
6127                    p_atp_period.Adjusted_Cum_Quantity(i) := p_atp_period.Adjusted_Cum_Quantity(i+1);
6128                    IF PG_DEBUG in ('Y', 'C') THEN
6129                       msc_sch_wb.atp_debug ('Adjust_Cum: ' || 'Updated Unallocated Cum date:qty - '||
6130                                 p_atp_period.Period_Start_Date(i) ||' : '|| p_atp_period.Adjusted_Cum_Quantity(i) );
6131                    END IF;
6132                 END IF;
6133                 i := p_atp_period.Period_Start_Date.PRIOR(i);
6134                 j := p_atp_period.Period_Start_Date.PRIOR(j);
6135         END LOOP;
6136         --rajjain 02/11/2003 Bug 2793336 End
6137 
6138         IF PG_DEBUG in ('Y', 'C') THEN
6139                 i := p_cur_start_index;
6140                 WHILE (i is not null) AND (i <= p_cur_end_index) LOOP
6141                         msc_sch_wb.atp_debug('Adjust_Cum: ' || 'cur adjusted cum date:qty - '||
6142                                 p_atp_period.Period_Start_Date(i) ||' : '|| p_atp_period.Adjusted_Cum_Quantity(i) );
6143                         i := p_atp_period.Period_Start_Date.Next(i);
6144                 END LOOP;
6145 
6146                 i := p_unalloc_start_index;
6147                 WHILE (i is not null) AND (i <= p_unalloc_end_index) LOOP
6148                         msc_sch_wb.atp_debug('Adjust_Cum: ' || 'unalloc cum date:qty - '||
6149                                 p_atp_period.Period_Start_Date(i) ||' : '|| p_atp_period.Adjusted_Cum_Quantity(i) );
6150                         i := p_atp_period.Period_Start_Date.Next(i);
6151                 END LOOP;
6152 
6153                 msc_sch_wb.atp_debug('Adjust_Cum: ' || '**********End Adjust_Cum Procedure************');
6154         END IF;
6155 
6156 EXCEPTION
6157 WHEN OTHERS THEN
6158         IF PG_DEBUG in ('Y', 'C') THEN
6159                 msc_sch_wb.atp_debug('Adjust_Cum: ' || 'Error occurred: ' || to_char(sqlcode) || ':' || SQLERRM);
6160         END IF;
6161         x_return_status := FND_API.G_RET_STS_ERROR;
6162         Set_Error(MSC_ATP_PVT.ATP_PROCESSING_ERROR);
6163 END Adjust_Cum;
6164 
6165 
6166 /*--Set_Error--------------------------------------------------------------
6167 |  o Set error code if it has already not been set
6168 +-------------------------------------------------------------------------*/
6169 PROCEDURE Set_Error(
6170         p_error_code                    IN      INTEGER)
6171 IS
6172 BEGIN
6173         IF PG_DEBUG in ('Y', 'C') THEN
6174                 msc_sch_wb.atp_debug('Set_Error: ' || 'Old Error: ' || to_char(MSC_SCH_WB.G_ATP_ERROR_CODE));
6175                 msc_sch_wb.atp_debug('Set_Error: ' || 'New Error: ' || to_char(p_error_code));
6176         END IF;
6177         -- Setting API return code
6178         IF MSC_SCH_WB.G_ATP_ERROR_CODE = 0 THEN
6179                 MSC_SCH_WB.G_ATP_ERROR_CODE := p_error_code;
6180         END IF;
6181 
6182 END Set_Error;
6183 
6184 
6185 -- Added function to call the refresh allocation concurrent program
6186 -- from database package.
6187 -- fix for bug 2781625
6188 function Refresh_Alloc_request(
6189                      p_new_session_id in number ,
6190                      p_inventory_item_id in number ,
6191                      p_instance_id in number ,
6192                      p_organization_id in number ) return number IS
6193 l_return_code number;
6194 begin
6195         l_return_code:= Fnd_Request.Submit_Request('MSC',
6196                                                 'MSC_ATP_REFRESH_WB',
6197                                                  '',
6198                                                  '',
6199                                                  FALSE,
6200                                                  p_new_session_id,
6201                                                  p_inventory_item_id,
6202                                                  p_instance_id,
6203                                                  p_organization_id);
6204         if l_return_code <> 0 then
6205                 commit;
6206         end if;
6207         return l_return_code;
6208 end Refresh_Alloc_request;
6209 
6210 END MSC_ATP_ALLOC;