DBA Data[Home] [Help]

PACKAGE BODY: APPS.MSC_ATP_SUBST

Source


1 PACKAGE BODY MSC_ATP_SUBST AS
2 /* $Header: MSCSUBAB.pls 120.10 2011/12/06 10:09:53 vjuluri ship $  */
3 G_PKG_NAME              CONSTANT VARCHAR2(30) := 'MSC_ATP_SUBST';
4 
5 MATERIAL_CONSTRAINT 	CONSTANT NUMBER := 1;
6 PTF_CONSTRAINT 		CONSTANT NUMBER := 2;
7 TRANSIT_LT_CONSTRAINT 	CONSTANT NUMBER := 5;
8 
9 NOSOURCES_NONCONSTRAINT CONSTANT NUMBER := -1;
10 DIAGNOSTIC_ATP		CONSTANT NUMBER := 1;
11 ORG_DEMAND_PEG_TYP	CONSTANT NUMBER := 1;
12 ORG_SUPPLY_PEG_TYP	CONSTANT NUMBER := 3;
13 TRANSFER_PEG_TYP	CONSTANT NUMBER := 6;
14 
15 PG_DEBUG varchar2(1) := NVL(FND_PROFILE.value('MSC_ATP_DEBUG'), 'N');
16 
17 PROCEDURE top_org_supply_qty(
18    p_org_avail_info     IN OUT NoCopy MSC_ATP_SUBST.ATP_Org_Info_Rec_Typ,
19    p_org_idx            IN NUMBER
20 ) IS
21    j                    number;
22    l_qty                number;
23    l_parent             number;
24    l_PO_qty             number;
25 BEGIN
26    j := p_org_idx;
27 
28    while j <> 1 loop
29         l_parent := p_org_avail_info.parent_org_idx(j);
30         l_qty := p_org_avail_info.rnding_leftover(j)
31                     / p_org_avail_info.conversion_rate(j);
32         IF nvl(p_org_avail_info.rounding_flag(l_parent), 2) = 1 THEN
33            l_qty := FLOOR(l_qty);
34            p_org_avail_info.rnding_leftover(j) := p_org_avail_info.rnding_leftover(j)
35               - (l_qty * p_org_avail_info.conversion_rate(j));
36            l_PO_qty := FLOOR(ROUND(p_org_avail_info.demand_quantity(j)
37                                    / p_org_avail_info.conversion_rate(j), 10));
38         ELSE
39            l_PO_qty := p_org_avail_info.demand_quantity(j);
40            p_org_avail_info.rnding_leftover(j) := 0;
41         END IF;
42         p_org_avail_info.rnding_leftover(l_parent) :=
43            nvl(p_org_avail_info.rnding_leftover(l_parent), 0) + l_qty;
44 
45         p_org_avail_info.quantity_from_children(l_parent) :=
46            nvl(p_org_avail_info.quantity_from_children(l_parent), 0)
47                + least(l_qty, l_PO_qty);
48         --bug3467631
49         --Adding the atf_date_quantity got through the supply chain.
50         IF p_org_avail_info.requested_ship_date(l_parent) <=
51            p_org_avail_info.atf_date(l_parent) THEN
52          p_org_avail_info.Atf_Date_Quantity(l_parent) :=
53            nvl(p_org_avail_info.Atf_Date_Quantity(l_parent), 0) +
54            least(l_qty, l_PO_qty);
55         END IF;
56         --bug3467631
57         IF PG_DEBUG in ('Y', 'C') THEN
58            msc_sch_wb.atp_debug('qty_from_children(' ||l_parent||'): ' ||
59                 p_org_avail_info.quantity_from_children(l_parent));
60            msc_sch_wb.atp_debug('rnding_leftover(' ||j||'): ' ||
61                 p_org_avail_info.rnding_leftover(j));
62         END IF;
63         j := l_parent;
64    end loop;
65 
66 END top_org_supply_qty;
67 
68 FUNCTION org_req_dmd_qty(
69    p_qty                IN NUMBER,
70    p_org_avail_info     IN MSC_ATP_SUBST.ATP_Org_Info_Rec_Typ,
71    p_org_idx            IN NUMBER
72 ) RETURN NUMBER IS
73    l_qty                number;
74    l_parent             number;
75 BEGIN
76    IF p_org_idx = 1 THEN
77       msc_sch_wb.atp_debug('req_dmd_qty1: ' || p_qty);
78       return p_qty;
79    END IF;
80 
81    l_parent := p_org_avail_info.parent_org_idx(p_org_idx);
82    l_qty := org_req_dmd_qty(p_qty, p_org_avail_info, l_parent);
83    IF nvl(p_org_avail_info.rounding_flag(l_parent), 2) = 1 THEN
84       l_qty := CEIL(l_qty);
85    END IF;
86 
87    msc_sch_wb.atp_debug('req_dmd_qty0: ' || l_qty);
88    l_qty := l_qty * p_org_avail_info.conversion_rate(p_org_idx);
89    l_qty := l_qty - nvl(p_org_avail_info.rnding_leftover(p_org_idx),0);
90 
91 msc_sch_wb.atp_debug('rnding_leftover('||p_org_idx||'): ' || nvl(p_org_avail_info.rnding_leftover(p_org_idx),0));
92    msc_sch_wb.atp_debug('req_dmd_qty: ' || l_qty);
93    return l_qty;
94 END org_req_dmd_qty;
95 
96 PROCEDURE Extend_Org_Avail_Info_Rec (
97   p_org_avail_info         IN OUT NOCOPY  MSC_ATP_SUBST.ATP_Org_Info_Rec_Typ,
98   x_return_status          OUT      NoCopy VARCHAR2)
99 IS
100 Begin
101 
102     x_return_status := FND_API.G_RET_STS_SUCCESS;
103     ----
104     p_org_avail_info.Organization_Id.extend;
105     p_org_avail_info.Parent_Org_Idx.extend;
106     p_org_avail_info.Requested_ship_date.extend;
107     p_org_avail_info.Request_date_Quantity.extend;
108     p_org_avail_info.demand_quantity.extend;
109     p_org_avail_info.Demand_Pegging_id.extend;
110     p_org_avail_info.Supply_pegging_id.extend;
111     p_org_avail_info.PO_pegging_id.extend;
112     p_org_avail_info.Demand_ID.extend;
113     p_org_avail_info.org_code.extend;
114     p_org_avail_info.Lead_time.extend;
115     p_org_avail_info.Quantity_from_children.extend;
116     p_org_avail_info.Atp_Flag.extend;
117     p_org_avail_info.atp_comp_flag.extend;
118     p_org_avail_info.post_pro_lt.extend;
119     p_org_avail_info.plan_id.extend;
120     p_org_avail_info.assign_set_id.extend;
121     p_org_avail_info.location_id.extend;
122     p_org_avail_info.demand_Class.extend;
123     p_org_avail_info.steal_qty.extend;
124 
125     -- dsting diagnostic atp
126     p_org_avail_info.pre_pro_lt.extend;
127     p_org_avail_info.fixed_lt.extend;
128     p_org_avail_info.variable_lt.extend;
129     p_org_avail_info.ship_method.extend;
130     p_org_avail_info.plan_name.extend;
131     p_org_avail_info.rounding_flag.extend;
132     p_org_avail_info.unit_weight.extend;
133     p_org_avail_info.weight_uom.extend;
134     p_org_avail_info.unit_volume.extend;
135     p_org_avail_info.volume_uom.extend;
136     p_org_avail_info.ptf_date.extend;
137     p_org_avail_info.substitution_window.extend;
138     p_org_avail_info.allocation_rule.extend;
139     p_org_avail_info.infinite_time_fence.extend;
140     p_org_avail_info.atp_rule_name.extend;
141     p_org_avail_info.constraint_type.extend;
142     p_org_avail_info.constraint_date.extend;
143 
144     -- 2754446
145     p_org_avail_info.conversion_rate.extend;
146     p_org_avail_info.primary_uom.extend;
147     p_org_avail_info.req_date_unadj_qty.extend;
148     p_org_avail_info.rnding_leftover.extend;
149 
150     --time_phased_atp
151     p_org_avail_info.Family_sr_id.EXTEND;
152     p_org_avail_info.Family_dest_id.EXTEND;
153     p_org_avail_info.Family_item_name.EXTEND;
154     p_org_avail_info.Atf_Date.EXTEND;
155     p_org_avail_info.Atf_Date_Quantity.EXTEND;
156 
157     -- ship_rec_cal
158     p_org_avail_info.shipping_cal_code.EXTEND;
159     p_org_avail_info.receiving_cal_code.EXTEND;
160     p_org_avail_info.intransit_cal_code.EXTEND;
161     p_org_avail_info.manufacturing_cal_code.EXTEND;
162     --Extend only once
163     --p_org_avail_info.intransit_cal_code.EXTEND;
164     --p_org_avail_info.manufacturing_cal_code.EXTEND;
165     p_org_avail_info.new_ship_date.EXTEND;
166     p_org_avail_info.new_dock_date.EXTEND;
167     p_org_avail_info.new_start_date.EXTEND;     -- Bug 3241766
168     p_org_avail_info.new_order_date.EXTEND;     -- Bug 3241766
169 
170 
171 END Extend_Org_Avail_Info_Rec;
172 
173 /*------------------------- PEGGING_REC PROCEDURES ------------------------- */
174 
175 PROCEDURE Prep_Common_Pegging_Rec(
176 	x_pegging_rec		OUT NOCOPY mrp_atp_details_temp%ROWTYPE,
177 	p_atp_record		IN	MRP_ATP_PVT.AtpRec,
178 	ORG_AVAIL_INFO   IN	MSC_ATP_SUBST.ATP_Org_Info_Rec_Typ,
179 	p_org_idx		IN	NUMBER,
180 	Item_Availability_Info  IN      Item_Info_Rec_Typ,
181 	p_item_idx		In	NUMBER
182 ) IS
183 	l_parent_org_idx	NUMBER;
184 BEGIN
185 
186         x_pegging_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
187         x_pegging_rec.order_line_id := MSC_ATP_PVT.G_ORDER_LINE_ID;
188 
189         x_pegging_rec.organization_id := org_avail_info.organization_id(p_org_idx);
190 
191         x_pegging_rec.organization_code := org_avail_info.org_code(p_org_idx);
192         x_pegging_rec.identifier1:= p_atp_record.instance_id;
193 
194         x_pegging_rec.inventory_item_id:= item_availability_info.sr_inventory_item_id(p_item_idx);
195         x_pegging_rec.inventory_item_name := item_availability_info.item_name(p_item_idx);
196         x_pegging_rec.resource_id := NULL;
197         x_pegging_rec.resource_code := NULL;
198         x_pegging_rec.department_id := NULL;
199         x_pegging_rec.department_code := NULL;
200         x_pegging_rec.supplier_id := NULL;
201         x_pegging_rec.supplier_name := NULL;
202         x_pegging_rec.supplier_site_id := NULL;
203         x_pegging_rec.supplier_site_name := NULL;
204 	x_pegging_rec.substitution_window := item_availability_info.substitution_window(p_item_idx);
205 
206         x_pegging_rec.summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;
207         --bug3467631 start
208         x_pegging_rec.aggregate_time_fence_date:= org_avail_info.Atf_Date(p_org_idx);
209         --populating atf date in pegging as records are deleted from alloc table
210         --based on atf date also.
211         IF PG_DEBUG in ('Y', 'C') THEN
212            msc_sch_wb.atp_debug('Prep_Common_Pegging_Rec: ' || 'Atf_Date ' || org_avail_info.Atf_Date(p_org_idx));
213            msc_sch_wb.atp_debug('Prep_Common_Pegging_Rec: ' || 'inventory_item_id ' || x_pegging_rec.inventory_item_id);
214            msc_sch_wb.atp_debug('Prep_Common_Pegging_Rec: ' || 'inventory_item_name ' || x_pegging_rec.inventory_item_name);
215         END IF;
216         --bug3467631 end
217 END Prep_Common_Pegging_Rec;
218 
219 
220 PROCEDURE Prep_Demand_Pegging_Rec(
221 	x_pegging_rec		OUT NOCOPY mrp_atp_details_temp%ROWTYPE,
222 	p_atp_record		IN	MRP_ATP_PVT.AtpRec,
223 	ORG_AVAIL_INFO   IN	MSC_ATP_SUBST.ATP_Org_Info_Rec_Typ,
224 	p_org_idx		IN	NUMBER,
225 	Item_Availability_Info  IN      Item_Info_Rec_Typ,
226 	p_item_idx		In	NUMBER
227 ) IS
228 BEGIN
229 	IF PG_DEBUG in ('Y', 'C') THEN
230 	   msc_sch_wb.atp_debug('Prep_Demand_Pegging_Rec');
231 	END IF;
232 
233 	Prep_Common_pegging_rec(x_pegging_rec,
234 				p_atp_record,
235 				org_avail_info,
236 				p_org_idx,
237 				item_availability_info,
238 				p_item_idx);
239 
240         IF p_org_idx = 1 THEN
241 		x_pegging_rec.parent_pegging_id:= null;
242         ELSE
243                 x_pegging_rec.parent_pegging_id:= org_avail_info.PO_pegging_id(p_org_idx);
244         END IF;
245 
246         --bug3467631 In Old PF case we always want to show family item else member
247         IF ((item_availability_info.sr_inventory_item_id(p_item_idx) <>
248                 org_avail_info.family_sr_id(p_org_idx))
249             and org_avail_info.atf_date(p_org_idx) is null)
250         THEN
251                 x_pegging_rec.inventory_item_id:= org_avail_info.family_sr_id(p_org_idx);
252                 x_pegging_rec.inventory_item_name := org_avail_info.family_item_name(p_org_idx);
253          IF PG_DEBUG in ('Y', 'C') THEN
254            msc_sch_wb.atp_debug('Prep_Demand_Pegging_Rec: ' || 'inventory_item_name ' || org_avail_info.family_item_name(p_org_idx));
255            msc_sch_wb.atp_debug('Prep_Demand_Pegging_Rec: ' || 'x_pegging_rec.inventory_item_id ' || org_avail_info.family_sr_id(p_org_idx));
256          END IF;
257         END IF;
258         --bug3467631
259         x_pegging_rec.pegging_id := org_avail_info.demand_pegging_id(p_org_idx);
260         x_pegging_rec.end_pegging_id := org_avail_info.demand_pegging_id(1);
261 
262         x_pegging_rec.identifier2 := org_avail_info.plan_id(p_org_idx);
263         x_pegging_rec.identifier3 := org_avail_info.demand_id(p_org_idx);
264         x_pegging_rec.supply_demand_source_type:= 6;
265 
266         x_pegging_rec.supply_demand_type:= 1;
267         x_pegging_rec.source_type := null;
268         x_pegging_rec.supply_demand_date:= org_avail_info.requested_ship_date(p_org_idx);
269         x_pegging_rec.required_date:= org_avail_info.requested_ship_date(p_org_idx);
270 
271         -- for demo:1153192
272         x_pegging_rec.constraint_flag := 'N';
273 	x_pegging_rec.component_identifier :=
274              NVL(p_atp_record.component_identifier, MSC_ATP_PVT.G_COMP_LINE_ID);
275 
276         --- bug 2152184: For PF based ATP inventory_item_id field contains id for PF item
277         --- cto looks at pegging tree to place their demands. Since CTO expects to find
278         --  id for the requested item, we add the following column. CTO will now read from this column
279         --bug3467631
280         x_pegging_rec.request_item_id := NVL(p_atp_record.request_item_id,
281                                              p_atp_record.inventory_item_id);
282 
283 	x_pegging_rec.pegging_type := ORG_DEMAND_PEG_TYP;
284 
285 	IF PG_DEBUG in ('Y', 'C') THEN
286 	   msc_sch_wb.atp_debug('Prep_Demand_Pegging_Rec: ' || 'pegging_id ' || x_pegging_rec.pegging_id);
287 	   msc_sch_wb.atp_debug('Prep_Demand_Pegging_Rec: ' || 'parent_pegging_id ' || x_pegging_rec.parent_pegging_id);
288 	   msc_sch_wb.atp_debug('Prep_Demand_Pegging_Rec: ' || 'end_pegging_id ' || x_pegging_rec.end_pegging_id);
289 	END IF;
290 
291 END Prep_Demand_Pegging_Rec;
292 
293 /*
294  * dsting 10/18/02
295  *
296  * Adds the relevant information from the
297  *   - AtpRec
298  *   - Org_Info_Rec_Type
299  *   - Item_Info_Rec_Typ
300  *   - item availabile quantity
301  *
302  * source_type is always TRANSFER
303  * supply_demand_type is always 2
304  *
305  */
306 PROCEDURE Prep_PO_Pegging_Rec(
307 	x_pegging_rec		OUT NOCOPY mrp_atp_details_temp%ROWTYPE,
308 	p_atp_record		IN	MRP_ATP_PVT.AtpRec,
309 	ORG_AVAIL_INFO   IN	MSC_ATP_SUBST.ATP_Org_Info_Rec_Typ,
310 	p_org_idx		IN	NUMBER,
311 	Item_Availability_Info  IN      Item_Info_Rec_Typ,
312 	p_item_idx		In	NUMBER,
313 	p_PO_qty                IN	NUMBER,
314 	p_transaction_id	IN	NUMBER
315 ) IS
316 	l_parent_idx	NUMBER;
317 BEGIN
318 	IF PG_DEBUG in ('Y', 'C') THEN
319 	   msc_sch_wb.atp_debug('Prep_PO_Pegging_Rec');
320 	END IF;
321 
322 	l_parent_idx := org_avail_info.parent_org_idx(p_org_idx);
323 
324 	Prep_Common_pegging_rec(x_pegging_rec,
325 				p_atp_record,
326 				org_avail_info,
327 				p_org_idx,
328 				item_availability_info,
329 				p_item_idx);
330 
331         IF PG_DEBUG in ('Y', 'C') THEN
332            msc_sch_wb.atp_debug('Prep_PO_Pegging_Rec: ' || 'parent_idx ' || l_parent_idx);
333            msc_sch_wb.atp_debug('Prep_PO_Pegging_Rec: ' || 'p_org_idx ' || p_org_idx);
334         END IF;
335 
336         -- time_phased_atp changes begin
337         --bug3467631 start
338         /*IF (item_availability_info.sr_inventory_item_id(p_item_idx) <>
339                 item_availability_info.family_sr_id(p_item_idx))
340             and org_avail_info.requested_ship_date(l_parent_idx) > item_availability_info.atf_date(p_item_idx)
341         THEN
342                 x_pegging_rec.inventory_item_id:= item_availability_info.family_sr_id(p_item_idx);
343                 x_pegging_rec.inventory_item_name := item_availability_info.family_item_name(p_item_idx);
344         ELSE
345                 x_pegging_rec.inventory_item_id:= item_availability_info.sr_inventory_item_id(p_item_idx);
346                 x_pegging_rec.inventory_item_name := item_availability_info.item_name(p_item_idx);
347         END IF;*/
348         --Commenting out as atf_date and family_id are item-org attributes . A member can have
349         --different family in different orgs across supply chain and similarly atf_date.
350         --Handling OLD PF cases also .
351         IF ((item_availability_info.sr_inventory_item_id(p_item_idx) <>
352                 org_avail_info.family_sr_id(p_org_idx))
353             and ((org_avail_info.requested_ship_date(l_parent_idx) > org_avail_info.atf_date(p_org_idx)) OR
354                  org_avail_info.atf_date(p_org_idx) is null))
355         THEN
356                 x_pegging_rec.inventory_item_id:= org_avail_info.family_sr_id(p_org_idx);
357                 x_pegging_rec.inventory_item_name := org_avail_info.family_item_name(p_org_idx);
358         ELSE
359                 x_pegging_rec.inventory_item_id:= item_availability_info.sr_inventory_item_id(p_item_idx);
360                 x_pegging_rec.inventory_item_name := item_availability_info.item_name(p_item_idx);
361         END IF;
362         --bug3467631 end
363         -- time_phased_atp changes end
364 
365         x_pegging_rec.parent_pegging_id:= org_avail_info.demand_pegging_id(l_parent_idx);
366         x_pegging_rec.end_pegging_id := org_avail_info.demand_pegging_id(1);
367         x_pegging_rec.pegging_id := org_avail_info.po_pegging_id(p_org_idx);
368 
369         x_pegging_rec.organization_id := org_avail_info.organization_id(l_parent_idx);
370         x_pegging_rec.organization_code := org_avail_info.org_code(p_org_idx);
371 
372         x_pegging_rec.identifier2:= org_avail_info.plan_id(l_parent_idx);
373         x_pegging_rec.identifier3 := p_transaction_id;
374         x_pegging_rec.supply_demand_source_type:= MSC_ATP_PVT.TRANSFER;
375 
376         x_pegging_rec.supply_demand_quantity := p_PO_qty;
377         x_pegging_rec.supply_demand_type:= 2;
378         x_pegging_rec.supply_demand_date:= org_avail_info.requested_ship_date(l_parent_idx);
379         x_pegging_rec.source_type :=  MSC_ATP_PVT.TRANSFER;
380 
381         -- for demo:1153192
382         x_pegging_rec.component_identifier :=
383                          NVL(p_atp_record.component_identifier, MSC_ATP_PVT.G_COMP_LINE_ID);
384         --bug3467631
385         x_pegging_rec.request_item_id := NVL(p_atp_record.request_item_id,
386                                              p_atp_record.inventory_item_id);
387 
388 	-- additional columns from pegging enhancement
389 	x_pegging_rec.Postprocessing_lead_time := org_avail_info.post_pro_lt(l_parent_idx);
390 	x_pegging_rec.Intransit_lead_time := org_avail_info.Lead_time(p_org_idx);
391 	IF( NVL(org_avail_info.ship_method(p_org_idx), '@@@') <> '@@@' ) THEN
392 		x_pegging_rec.ship_method := org_avail_info.ship_method(p_org_idx);
393 	END IF;
394 	x_pegging_rec.ptf_date := org_avail_info.ptf_date(p_org_idx);
395 
396 	x_pegging_rec.weight_capacity := org_avail_info.unit_weight(p_org_idx);
397 	x_pegging_rec.volume_capacity := org_avail_info.unit_volume(p_org_idx);
398 
399 	x_pegging_rec.weight_uom := org_avail_info.weight_uom(p_org_idx);
400 	x_pegging_rec.volume_uom := org_avail_info.volume_uom(p_org_idx);
401 
402 	x_pegging_rec.plan_name := org_avail_info.plan_name(p_org_idx);
403 	x_pegging_rec.rounding_control := org_avail_info.rounding_flag(p_org_idx);
404         -- 2754446
405 	x_pegging_rec.required_quantity := org_avail_info.demand_quantity(p_org_idx)
406                     / org_avail_info.conversion_rate(p_org_idx);
407         IF org_avail_info.rounding_flag(l_parent_idx) = 1 THEN
408            x_pegging_rec.required_quantity := CEIL(ROUND(x_pegging_rec.required_quantity,10));
409         END IF;
410 	x_pegging_rec.required_date := org_avail_info.requested_ship_date(p_org_idx);
411 
412 	IF PG_DEBUG in ('Y', 'C') THEN
413 	   msc_sch_wb.atp_debug('Prep_PO_Pegging_Rec: ' || 'required_quantity: ' || x_pegging_rec.required_quantity);
414 	   msc_sch_wb.atp_debug('Prep_PO_Pegging_Rec: ' || 'required_date: ' || x_pegging_rec.required_date);
415 	END IF;
416 
417 	x_pegging_rec.pegging_type := 6;
418 	-- Bug 3826234
419 	x_pegging_rec.shipping_cal_code := org_avail_info.shipping_cal_code(p_org_idx);
420 	x_pegging_rec.receiving_cal_code := org_avail_info.receiving_cal_code(p_org_idx);
421 	x_pegging_rec.intransit_cal_code := org_avail_info.intransit_cal_code(p_org_idx);
422 	x_pegging_rec.manufacturing_cal_code := org_avail_info.manufacturing_cal_code(p_org_idx);
423 
424 	IF PG_DEBUG in ('Y', 'C') THEN
425 	   msc_sch_wb.atp_debug('Prep_PO_Pegging_Rec: ' || 'pegging_id ' || x_pegging_rec.pegging_id);
426 	   msc_sch_wb.atp_debug('Prep_PO_Pegging_Rec: ' || 'parent_pegging_id ' || x_pegging_rec.parent_pegging_id);
427 	   msc_sch_wb.atp_debug('Prep_PO_Pegging_Rec: ' || 'end_pegging_id ' || x_pegging_rec.end_pegging_id);
428 	   msc_sch_wb.atp_debug('Prep_PO_Pegging_Rec: ' || 'shipping_cal_code ' || x_pegging_rec.shipping_cal_code);
429 	   msc_sch_wb.atp_debug('Prep_PO_Pegging_Rec: ' || 'receiving_cal_code ' || x_pegging_rec.receiving_cal_code);
430 	   msc_sch_wb.atp_debug('Prep_PO_Pegging_Rec: ' || 'intransit_cal_code ' || x_pegging_rec.intransit_cal_code);
431 	END IF;
432 
433 END Prep_PO_Pegging_Rec;
434 
435 PROCEDURE Prep_Supply_Pegging_Rec(
436 	x_pegging_rec		OUT NOCOPY mrp_atp_details_temp%ROWTYPE,
437 	p_atp_record		IN	MRP_ATP_PVT.AtpRec,
438 	ORG_AVAIL_INFO   IN	MSC_ATP_SUBST.ATP_Org_Info_Rec_Typ,
439 	p_org_idx		IN	NUMBER,
440 	Item_Availability_Info  IN      Item_Info_Rec_Typ,
441 	p_item_idx		In	NUMBER,
442 	p_transaction_id	IN	NUMBER
443 ) IS
444 	l_parent_idx	NUMBER;
445 BEGIN
446 	IF PG_DEBUG in ('Y', 'C') THEN
447 	   msc_sch_wb.atp_debug('Prep_Supply_Pegging_Rec');
448 	END IF;
449 
450 	l_parent_idx := org_avail_info.parent_org_idx(p_org_idx);
451 
452 	IF PG_DEBUG in ('Y', 'C') THEN
453 	   msc_sch_wb.atp_debug('Prep_Supply_Pegging_Rec: ' || 'parent_idx ' || l_parent_idx);
454 	   msc_sch_wb.atp_debug('Prep_Supply_Pegging_Rec: ' || 'p_org_idx ' || p_org_idx);
455 	   msc_sch_wb.atp_debug('Prep_Supply_Pegging_Rec: ' || 'item_availability_info.sr_inventory_item_id(p_item_idx) ' || item_availability_info.sr_inventory_item_id(p_item_idx));
456 	   msc_sch_wb.atp_debug('Prep_Supply_Pegging_Rec: ' || 'item_availability_info.family_sr_id(p_item_idx) ' || item_availability_info.family_sr_id(p_item_idx));
457 	   msc_sch_wb.atp_debug('Prep_Supply_Pegging_Rec: ' || 'org_avail_info.requested_ship_date(p_org_idx) ' || org_avail_info.requested_ship_date(p_org_idx));
458 	   msc_sch_wb.atp_debug('Prep_Supply_Pegging_Rec: ' || 'item_availability_info.atf_date(p_item_idx) ' || item_availability_info.atf_date(p_item_idx));
459 	END IF;
460 
461 	Prep_Common_pegging_rec(x_pegging_rec,
462 				p_atp_record,
463 				org_avail_info,
464 				p_org_idx,
465 				item_availability_info,
466 				p_item_idx);
467 
468         -- time_phased_atp changes begin
469         --bug3467631 start
470         /*IF (item_availability_info.sr_inventory_item_id(p_item_idx) <>
471                 item_availability_info.family_sr_id(p_item_idx))
472             and org_avail_info.requested_ship_date(p_org_idx) > item_availability_info.atf_date(p_item_idx)
473         THEN
474                 x_pegging_rec.inventory_item_id:= item_availability_info.family_sr_id(p_item_idx);
475                 x_pegging_rec.inventory_item_name := item_availability_info.family_item_name(p_item_idx);
476         ELSE
477                 x_pegging_rec.inventory_item_id:= item_availability_info.sr_inventory_item_id(p_item_idx);
478                 x_pegging_rec.inventory_item_name := item_availability_info.item_name(p_item_idx);
479         END IF;*/
480         --Commenting out as atf_date and family_id are item-org attributes . A member can have
481         --different family in different orgs across supply chain and similarly atf_date.
482         --Handling OLD PF cases also .
483         IF ((item_availability_info.sr_inventory_item_id(p_item_idx) <>
484                 org_avail_info.family_sr_id(p_org_idx))
485             and ((org_avail_info.requested_ship_date(p_org_idx) > org_avail_info.atf_date(p_org_idx)) OR
486                  org_avail_info.atf_date(p_org_idx) is null ))
487         THEN
488                 x_pegging_rec.inventory_item_id:= org_avail_info.family_sr_id(p_org_idx);
489                 x_pegging_rec.inventory_item_name := org_avail_info.family_item_name(p_org_idx);
490         ELSE
491                 x_pegging_rec.inventory_item_id:= item_availability_info.sr_inventory_item_id(p_item_idx);
492                 x_pegging_rec.inventory_item_name := item_availability_info.item_name(p_item_idx);
493         END IF;
494         --bug3467631 end
495         -- time_phased_atp changes end
496 
497 	IF PG_DEBUG in ('Y', 'C') THEN
498 	   msc_sch_wb.atp_debug('Prep_Supply_Pegging_Rec: ' || 'x_pegging_rec.inventory_item_id ' || x_pegging_rec.inventory_item_id);
499 	   msc_sch_wb.atp_debug('Prep_Supply_Pegging_Rec: ' || 'x_pegging_rec.inventory_item_name ' || x_pegging_rec.inventory_item_name);
500 	END IF;
501 
502         x_pegging_rec.parent_pegging_id:= org_avail_info.demand_pegging_id(p_org_idx);
503         x_pegging_rec.pegging_id := org_avail_info.supply_pegging_id(p_org_idx);
504         x_pegging_rec.end_pegging_id := org_avail_info.demand_pegging_id(1);
505         x_pegging_rec.organization_id:= org_avail_info.organization_id(p_org_idx);
506         x_pegging_rec.organization_code := org_avail_info.org_code(p_org_idx);
507         x_pegging_rec.identifier2 := org_avail_info.plan_id(p_org_idx);
508         x_pegging_rec.identifier3 := p_transaction_id;
509         x_pegging_rec.supply_demand_source_type:= MSC_ATP_PVT.ATP;
510         -- dsting 2754446
511 --        x_pegging_rec.supply_demand_quantity:= org_avail_info.request_date_quantity(p_org_idx);
512         x_pegging_rec.supply_demand_quantity:= org_avail_info.req_date_unadj_qty(p_org_idx);
513         x_pegging_rec.supply_demand_date:= org_avail_info.requested_ship_date(p_org_idx);
514         x_pegging_rec.supply_demand_type:= 2;
515         x_pegging_rec.source_type := 0;
516 	x_pegging_rec.component_identifier :=
517 	        NVL(p_atp_record.component_identifier, MSC_ATP_PVT.G_COMP_LINE_ID);
518 
519 	IF NVL(org_avail_info.constraint_type(p_org_idx), NOSOURCES_NONCONSTRAINT)
520 	   <> NOSOURCES_NONCONSTRAINT
521 	THEN
522 		x_pegging_rec.constraint_type := org_avail_info.constraint_type(p_org_idx);
523 		x_pegging_rec.constraint_date := org_avail_info.constraint_date(p_org_idx);
524 	END IF;
525 
526         x_pegging_rec.request_item_id := NVL(p_atp_record.request_item_id, --bug3467631
527                                              p_atp_record.inventory_item_id);
528 
529 	-- dsting additional columns for diagnostic ATP
530 	x_pegging_rec.weight_capacity := org_avail_info.unit_weight(p_org_idx);
531 	x_pegging_rec.volume_capacity := org_avail_info.unit_volume(p_org_idx);
532 	x_pegging_rec.weight_uom := org_avail_info.weight_uom(p_org_idx);
533 	x_pegging_rec.volume_uom := org_avail_info.volume_uom(p_org_idx);
534 	x_pegging_rec.variable_lead_time := org_avail_info.variable_lt(p_org_idx);
535 	x_pegging_rec.fixed_lead_time := org_avail_info.fixed_lt(p_org_idx);
536 	x_pegging_rec.postprocessing_lead_time := org_avail_info.post_pro_lt(p_org_idx);
537 	x_pegging_rec.preprocessing_lead_time := org_avail_info.pre_pro_lt(p_org_idx);
538 
539 	x_pegging_rec.plan_name := org_avail_info.plan_name(p_org_idx);
540 	x_pegging_rec.atp_flag := org_avail_info.atp_flag(p_org_idx);
541 	x_pegging_rec.atp_component_flag := org_avail_info.atp_comp_flag(p_org_idx);
542 	x_pegging_rec.rounding_control := org_avail_info.rounding_flag(p_org_idx);
543 	x_pegging_rec.required_quantity := org_avail_info.demand_quantity(p_org_idx);
544 	x_pegging_rec.required_date := org_avail_info.requested_ship_date(p_org_idx);
545 	x_pegging_rec.atp_rule_name := org_avail_info.atp_rule_name(p_org_idx);
546 	x_pegging_rec.infinite_time_fence := org_avail_info.infinite_time_fence(p_org_idx);
547 	x_pegging_rec.allocation_rule := org_avail_info.allocation_rule(p_org_idx);
548 
549 	-- Bug 3826234
550 	x_pegging_rec.shipping_cal_code := org_avail_info.shipping_cal_code(p_org_idx);
551 	x_pegging_rec.receiving_cal_code := org_avail_info.receiving_cal_code(p_org_idx);
552 	x_pegging_rec.intransit_cal_code := org_avail_info.intransit_cal_code(p_org_idx);
553 	x_pegging_rec.manufacturing_cal_code := org_avail_info.manufacturing_cal_code(p_org_idx);
554 
555 	IF PG_DEBUG in ('Y', 'C') THEN
556 	   msc_sch_wb.atp_debug('Prep_Supply_Pegging_Rec: ' || 'required_quantity ' || x_pegging_rec.required_quantity);
557 	   msc_sch_wb.atp_debug('Prep_Supply_Pegging_Rec: ' || 'required_date ' || x_pegging_rec.required_date);
558 	   msc_sch_wb.atp_debug('Prep_Supply_Pegging_Rec: ' || 'infinite_time_fence ' || x_pegging_rec.infinite_time_fence);
559 	   msc_sch_wb.atp_debug('Prep_Supply_Pegging_Rec: ' || 'pegging_id ' || x_pegging_rec.pegging_id);
560 	   msc_sch_wb.atp_debug('Prep_Supply_Pegging_Rec: ' || 'parent_pegging_id ' || x_pegging_rec.parent_pegging_id);
561 	   msc_sch_wb.atp_debug('Prep_Supply_Pegging_Rec: ' || 'end_pegging_id ' || x_pegging_rec.end_pegging_id);
562 	   msc_sch_wb.atp_debug('Prep_Supply_Pegging_Rec: ' || 'shipping_cal_code ' || x_pegging_rec.shipping_cal_code);
563 	   msc_sch_wb.atp_debug('Prep_Supply_Pegging_Rec: ' || 'receiving_cal_code ' || x_pegging_rec.receiving_cal_code);
564 	   msc_sch_wb.atp_debug('Prep_Supply_Pegging_Rec: ' || 'intransit_cal_code ' || x_pegging_rec.intransit_cal_code);
565 	END IF;
566 
567 	x_pegging_rec.pegging_type := ORG_SUPPLY_PEG_TYP;
568 
569 END Prep_Supply_Pegging_Rec;
570 
571 /*------------------------- END PEGGING_REC PROCEDURES ------------------------- */
572 
573 
574 PROCEDURE ATP_Check_Subst
575               (p_atp_record                     IN OUT   NoCopy MRP_ATP_PVT.AtpRec,
576                p_item_substitute_rec            IN       Item_Info_Rec_Typ,
577                p_requested_ship_date            IN       DATE,
578                p_plan_id                        IN       NUMBER,
579                p_level                          IN       NUMBER,
580                p_scenario_id                    IN       NUMBER,
581                p_search                         IN       NUMBER,
582                p_refresh_number                 IN       NUMBER,
583                p_parent_pegging_id              IN       NUMBER,
584                p_assign_set_id                  IN       NUMBER,
585                x_atp_period                     OUT NOCOPY MRP_ATP_PUB.ATP_Period_Typ,
586                x_atp_supply_demand              OUT NOCOPY MRP_ATP_PUB.ATP_Supply_Demand_Typ,
587                x_return_status                  OUT      NoCopy VARCHAR2
588 ) IS
589 
590 l_parent_org_cntr       number;
591 l_process_org_cntr      number;
592 l_sources               MRP_ATP_PVT.Atp_source_typ;
593 l_null_sources          MRP_ATP_PVT.Atp_source_typ;
594 l_ship_method           varchar2(50);
595 l_from_location_id      number;
596 l_delivery_lead_time    number;
597 l_inventory_item_id     number;
598 l_requested_ship_date   date;
599 L_substitution_type     number;
600 L_ATP_FLAG              varchar2(1);
601 l_atp_comp_flag         varchar2(1);
602 l_item_cntr             number;
603 l_sysdate               date;
604 l_net_demand            number;
605 l_period_begin_idx      number;
606 l_sd_begin_idx          number;
607 ORG_AVAILABILITY_INFO   MSC_ATP_SUBST.ATP_Org_Info_Rec_Typ;
608 l_null_org_avail_info   MSC_ATP_SUBST.ATP_Org_Info_Rec_Typ;
609 l_return_status         VARCHAR2(1);
610 l_atp_period            MRP_ATP_PUB.ATP_Period_Typ;
611 L_NULL_ATP_PERIOD       MRP_ATP_PUB.ATP_Period_Typ;
612 l_atp_supply_demand     MRP_ATP_PUB.ATP_Supply_Demand_Typ;
613 L_NULL_ATP_SUPPLY_DEMAND MRP_ATP_PUB.ATP_Supply_Demand_Typ;
614 l_all_atp_period        MRP_ATP_PUB.ATP_Period_Typ;
615 l_all_atp_supply_demand MRP_ATP_PUB.ATP_Supply_Demand_Typ;
616 l_org_code              VARCHAR2(7);
617 l_top_tier_org_code     VARCHAR2(7);
618 L_REQUESTED_DATE_QUANTITY number;
619 --bug3467631 not needed
620 --L_PRE_PRO_LT            number;
621 --L_POST_PRO_LT           number;
622 l_atp_insert_rec        MRP_ATP_PVT.AtpRec;
623 L_DEMAND_ID             number;
624 l_fixed_lt              number;
625 --L_INV_ITEM_NAME         varchar2(255);
626 L_ATP_DATE_THIS_LEVEL   date;
627 L_DEMAND_CLASS_FLAG     NUMBER := 0;
628 L_PEGGING_ID            number;
629 L_ATP_DATE_QUANTITY_THIS_LEVEL number;
630 L_VARIABLE_LT          number;
631 L_PARENT_INDEX         number;
632 j                      number;
633 i                      number;
634 l_period_end_idx       number;
635 l_sd_end_idx           number;
636 l_sources_found        number;
637 l_transfer_found       number;
638 L_AVAILABLE_QUANTITY   number;
639 l_pegging_rec          mrp_atp_details_temp%ROWTYPE;
640 L_PTF_DATE             date;
641 L_START_DATE           date;
642 l_dock_date            date;
643 l_req_ship_date        date;
644 L_COUNT                number;
645 L_TRANSACTION_ID       number;
646 L_DEMAND_SATISFIED_FLAG number;
647 l_org_item_detail_flag  number;
648 l_item_ctp_info         Item_Info_Rec_Typ;
649 l_null_item_avail_info  Item_Info_Rec_Typ;
650 l_item_attribute_rec    MSC_ATP_PVT.item_attribute_rec;
651 l_plan_info_rec         MSC_ATP_PVT.plan_info_rec;    --- for bug 2392456
652 
653 L_HIGHEST_REV_ITEM_ID   number;
654 L_ITEM_COUNT            number;
655 L_ASSIGN_SET_ID         number;
656 l_plan_id               number;
657 L_SUBSTITUTION_WINDOW   number;
658 L_CREATE_SUPPLY_FLAG    number;
659 L_CREATE_SUPPLY_ON_ORIG_ITEM number;
660 L_NET_DEMAND_AFTER_OH_CHECK  number;
661 L_SATISFIED_BY_SUBST_FLAG    number;
662 L_ITEM_IDX                   number;
663 L_HIGHEST_REV_ITEM_INDEX     number;
664 l_atp_rec                    MRP_ATP_PVT.AtpRec;
665 l_atp_date                   date;
666 Item_Availability_Info       Item_Info_Rec_Typ;
667 l_item_name                  varchar2(250);
668 L_TO_LOCATION_ID             number;
669 l_insert_flag                number;
670 l_demand_pegging_id          number;
671 l_sys_date_top_org            date;
672 
673 --- alloc ATP
674 g_atp_record                  MRP_ATP_PVT.AtpRec;
675 l_stealing_requested_date_qty   NUMBER := 0.0;
676 l_stealing_qty                  NUMBER := 0.0;
677 l_demand_class                  varchar2(30);
678 
679 --forward steal
680 l_atp_pegging_tab               MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr();
681 l_fwd_atp_pegging_tab           MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr();
682 
683 -- dsting diag_atp
684 l_make_buy_cd			NUMBER;
685 l_ptf_due_date			DATE;
686 l_lt_due_date			DATE;
687 l_get_mat_in_rec		MSC_ATP_REQ.get_mat_in_rec;
688 l_get_mat_out_rec		MSC_ATP_REQ.get_mat_out_rec;
689 
690 -- dsting for balancing planned orders
691 l_diag_supply_qty	NUMBER;
692 l_diag_transaction_id 	NUMBER;
693 
694 -- dsting 2754446 in parent org's uom
695 l_PO_qty                NUMBER;
696 l_post_stealing_dmd     NUMBER;
697 l_avail_qty_top_uom     NUMBER;
698 l_orig_net_dmd          NUMBER;
699 l_addt_qty              NUMBER;
700 l_process_org_dmd       NUMBER;
701 
702 --s_cto_rearch
703 l_null_item_sourcing_info_rec  MSC_ATP_CTO.item_sourcing_info_rec;
704 l_item_sourcing_info_rec  MSC_ATP_CTO.item_sourcing_info_rec;
705 
706 -- time_phased_atp
707 l_mat_atp_info_rec              MSC_ATP_REQ.Atp_Info_Rec;
708 l_atf_date_qty                  NUMBER;
709 l_time_phased_atp               VARCHAR2(1) := 'N';
710 l_mem_stealing_qty              NUMBER;
711 l_pf_stealing_qty               NUMBER;
712 l_used_available_quantity       NUMBER; --bug3409973
713 
714 -- ship_rec_cal changes
715 l_shipping_cal_code		VARCHAR2(14);
716 l_receiving_cal_code		VARCHAR2(14);
717 l_manufacturing_cal_code	VARCHAR2(14);
718 l_intransit_cal_code		VARCHAR2(14);
719 l_dest_mfg_cal_code             VARCHAR2(14);
720 l_new_ship_date                 DATE;
721 l_new_dock_date                 DATE;
722 l_planned_order_date            DATE;
723 l_order_date                    DATE;   -- Bug 3241766
724 
725 -- To support new logic for dependent demands allocation in time phased PF rule based AATP scenarios
726 l_item_to_use                   NUMBER;
727 
728 --bug3583705
729 l_encoded_text                  varchar2(4000);
730 l_msg_app                       varchar2(50);
731 l_msg_name                      varchar2(30);
732 -- bug3578083 - PTF constraint should be added only if plan is PTF enabled
733 l_ptf_enabled                   NUMBER;
734 l_trunc_sysdate                 DATE := TRUNC(sysdate); --bug3578083
735 
736 -- ATP4drp Declare a variable for creating Planned Arrival.
737 l_supply_rec                    MSC_ATP_DB_UTILS.supply_rec_typ;
738 -- End ATP4drp
739 -- ALLOC ATP CHANGES, 12973673
740 l_alloc_atp      VARCHAR2(1);
741 
742 BEGIN
743 /* Logic for onhand search:
744    Table item_availability_info : Stores information about item and its substitutes
745    Table Org_availability_info  : Stores information about an item in organizations in supply chain
746 
747 */
748 
749    IF PG_DEBUG in ('Y', 'C') THEN
750       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Point 2');
751       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || '****  Begin Check_ATP_Subst ***');
752       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || '********** INPUT DATA: p_atp_record **********');
753       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Inventory_Item_Id:' || to_char(p_atp_record.Inventory_Item_Id) );
754       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'request_item_id:' || to_char(p_atp_record.request_item_id) );
755       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'organization_id:' || to_char(p_atp_record.organization_id) );
756       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Quantity_Ordered:' || to_char(p_atp_record.Quantity_Ordered) );
757       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Quantity_UOM:' || p_atp_record.Quantity_UOM );
758       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Requested_Ship_Date:' || to_char(p_atp_record.Requested_Ship_Date) );
759       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Requested_Arrival_Date:' || to_char(p_atp_record.Requested_Arrival_Date) );
760       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Latest_Acceptable_Date:' || to_char(p_atp_record.Latest_Acceptable_Date) );
761       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Delivery_Lead_Time:' || to_char(p_atp_record.Delivery_Lead_Time) );
762       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Freight_Carrier:' || p_atp_record.Freight_Carrier );
763       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Ship_Method:' || p_atp_record.Ship_Method );
764       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Demand_Class:' || p_atp_record.Demand_Class );
765       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Override_Flag:' || p_atp_record.Override_Flag );
766       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Action:' || to_char(p_atp_record.Action) );
767       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Ship_Date:' || to_char(p_atp_record.Ship_Date) );
768       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Available_Quantity:' || to_char(p_atp_record.Available_Quantity) );
769       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Requested_Date_Quantity:' || to_char(p_atp_record.Requested_Date_Quantity) );
770       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'supplier_id:' || to_char(p_atp_record.supplier_id) );
771       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'supplier_site_id:' || to_char(p_atp_record.supplier_site_id) );
772       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Insert_Flag:' || to_char(p_atp_record.Insert_Flag) );
773       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Error_Code:' || to_char(p_atp_record.Error_Code) );
774       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Order_Number:' || to_char(p_atp_record.Order_Number) );
775       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'P_scenario_id := ' || p_scenario_id);
776       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'G_DIAGNOSTIC_ATP ' || MSC_ATP_PVT.G_DIAGNOSTIC_ATP);
777       -- Bug 3826234
778       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'shipping_cal_code ' || p_atp_record.shipping_cal_code);
779       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'manufacturing_cal_code ' || p_atp_record.manufacturing_cal_code);
780       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'receiving_cal_code ' || p_atp_record.receiving_cal_code);
781       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'intransit_cal_code ' || p_atp_record.intransit_cal_code);
782       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'to_organization_id ' || p_atp_record.to_organization_id);
783 	  msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'MSC_ATP_PVT.G_ALLOCATED_ATP ' || MSC_ATP_PVT.G_ALLOCATED_ATP);
784 	  msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'MSC_ATP_PVT.G_PLAN_INFO_REC.plan_type ' || MSC_ATP_PVT.G_PLAN_INFO_REC.plan_type);
785    END IF;
786    --- first we set the Request Item's sr_inv_id to a global variable
787    --bug3467631 In PF cases request_item_id has member_id
788    --MSC_ATP_SUBST.G_REQ_ITEM_SR_INV_ID := p_atp_record.inventory_item_id;
789    MSC_ATP_SUBST.G_REQ_ITEM_SR_INV_ID := NVL(p_atp_record.request_item_id,
790                                                 p_atp_record.inventory_item_id);
791    item_availability_info := p_item_substitute_rec;
792 
793    IF PG_DEBUG in ('Y', 'C') THEN
794       FOR i in 1..item_availability_info.inventory_item_id.count LOOP
795          msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Sr Item ID : ' || item_availability_info.sr_inventory_item_id(i) ||
796                            ',  item name : ' || item_availability_info.item_name(i) ||
797                            ',  atp_flag := ' || item_availability_info.atp_flag(i)  ||
798                            ', atp_comp_flag := ' || item_availability_info.atp_comp_flag(i) ||
799                            ', create supply flag := ' || item_availability_info.create_supply_flag(i));
800       END LOOP;
801    END IF;
802    l_requested_ship_date := p_requested_ship_date;
803 
804    l_substitution_type := p_atp_record.substitution_type;
805    IF PG_DEBUG in ('Y', 'C') THEN
806       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'substitution_type := ' || l_substitution_type);
807    END IF;
808 
809    l_org_item_detail_flag := NVL(p_atp_record.req_item_detail_flag, 2);
810    l_substitution_type := ALL_OR_NOTHING;
811    --l_org_item_detail_flag := 1;
812 
813    l_item_count := item_availability_info.inventory_item_id.count;
814    l_inventory_item_id := item_availability_info.inventory_item_id(l_item_count);
815    IF PG_DEBUG in ('Y', 'C') THEN
816       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_inventory_item_id := ' || l_inventory_item_id);
817       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_item_count := ' || l_item_count);
818    END IF;
819    IF item_availability_info.inventory_item_id.count > 0 THEN
820       l_highest_rev_item_id := NVL(item_availability_info.highest_revision_item_id(1),
821                                    item_availability_info.inventory_item_id(l_item_count));
822    ELSE
823       l_highest_rev_item_id := l_inventory_item_id;
824    END IF;
825 
826    -- dsting If we do not create supply on the original item then set the flag to 0
827    IF (MSC_ATP_SUBST.G_CREATE_SUPPLY_FLAG not in (G_DEMANDED_ITEM, G_ITEM_ATTRIBUTE) AND
828        NOT (MSC_ATP_SUBST.G_CREATE_SUPPLY_FLAG = G_HIGHEST_REV_ITEM AND
829              l_highest_rev_item_id = item_availability_info.inventory_item_id(l_item_count)))
830       OR item_availability_info.create_supply_flag(l_item_count) <> 1
831    THEN
832 	l_create_supply_on_orig_item := 0;
833    ELSE
834 	l_create_supply_on_orig_item := 1;
835    END IF;
836 
837    l_sys_date_top_org := NVL(MSC_ATP_FUNC.prev_work_day(p_atp_record.organization_id,
838                                               p_atp_record.instance_id,
839                                               sysdate), sysdate);
840    IF PG_DEBUG in ('Y', 'C') THEN
841       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_sys_date_top_org := ' ||l_sys_date_top_org);
842       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_substitution_type := ' || NVL(l_substitution_type, -1));
843    END IF;
844 
845    --- now check how do we need to process the request
846    IF NVL(l_substitution_type, 4) = 3 THEN
847       --- we look at item attribute to see what kind of substitution do we need to do
848       IF PG_DEBUG in ('Y', 'C') THEN
849          msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'look at item attribute');
850       END IF;
851       If item_availability_info.inventory_item_id.count > 1 THEN
852          -- substitutes exist
853          l_substitution_type := item_availability_info.partial_fulfillment_flag(1);
854       Else
855          --- we dont have any substitute. we will process it as 'All or nothing'
856          l_substitution_type := ALL_OR_NOTHING;
857       END IF;
858    ELSIF NVL(l_substitution_type, 4) = NO_SUBSTITUTION THEN
859          IF PG_DEBUG in ('Y', 'C') THEN
860             msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Not doing substitution');
861          END IF;
862          -- in this case also we process it as 'All or nothing'
863          l_substitution_type := ALL_OR_NOTHING;
864    END IF;
865 
866    l_net_demand := p_atp_record.quantity_ordered;
867    -- 2754446
868    l_orig_net_dmd := l_net_demand;
869 
870    IF PG_DEBUG in ('Y', 'C') THEN
871       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_net_demand := ' || l_net_demand);
872       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_substitution_type := ' || NVL(l_substitution_type,-1));
873    END IF;
874    --- this is done for testing
875    l_substitution_type :=  ALL_OR_NOTHING;
876    IF PG_DEBUG in ('Y', 'C') THEN
877       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_sysdate_top_org := ' || l_sys_date_top_org);
878       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_substitution_type := ' || NVL(l_substitution_type,-1));
879    END IF;
880    IF l_substitution_type = ALL_OR_NOTHING THEN
881 
882       --- first check on-hand/scheduled receipt by request date
883       --WHILE l_item_cntr <= item_availability_info.inventory_item_id.count AND
884       --      l_net_demand > 0 LOOP  --- item loop
885       FOR l_item_cntr in reverse 1..item_availability_info.inventory_item_id.count LOOP
886          IF PG_DEBUG in ('Y', 'C') THEN
887             msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Processing item := ' || item_availability_info.inventory_item_id(l_item_cntr) || '  '
888                                                     || item_availability_info.item_name(l_item_cntr));
889          END IF;
890 
891          IF l_net_demand <= 0 THEN
892               EXIT;
893          END IF;
894          --- reste the net demand
895          l_net_demand := p_atp_record.quantity_ordered ;
896          l_parent_org_cntr := 1;
897          l_process_org_cntr := 1;
898          -- dsting 2754446
899          l_parent_index := 1;
900          l_orig_net_dmd := l_net_demand;
901 
902          --add the supply demand and period details to output table
903          IF l_item_cntr = l_item_count THEN
904 
905             l_period_begin_idx := 1;
906             l_sd_begin_idx := 1;
907 
908          ELSE
909             l_period_begin_idx := l_all_atp_period.level.count + 1;
910             l_sd_begin_idx     := l_all_atp_supply_demand.level.count + 1;
911          END IF;
912          IF PG_DEBUG in ('Y', 'C') THEN
913             msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_period_begin_idx := ' || l_period_begin_idx);
914             msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_sd_begin_idx := ' || l_sd_begin_idx);
915          END IF;
916 
917          item_availability_info.period_detail_begin_idx(l_item_cntr) := l_period_begin_idx;
918          item_availability_info.sd_detail_begin_idx(l_item_cntr) := l_sd_begin_idx;
919 
920          ---for each item we reset the org_availability_info record of tables
921          org_availability_info := l_null_org_avail_info;
922 
923          MSC_ATP_SUBST.Extend_Org_Avail_Info_Rec(org_availability_info, l_return_status);
924          org_availability_info.organization_id(1) := p_atp_record.organization_id;
925          org_availability_info.requested_ship_date(1) := l_requested_ship_date ;
926          -- Bug 3371817 - assigning calendars
927          org_availability_info.shipping_cal_code(1)   := p_atp_record.shipping_cal_code;
928          org_availability_info.receiving_cal_code(1)  := p_atp_record.receiving_cal_code;
929          org_availability_info.intransit_cal_code(1)  := p_atp_record.intransit_cal_code;
930          org_availability_info.manufacturing_cal_code(1)  := p_atp_record.manufacturing_cal_code; -- Bug 3826234
931 
932          -- dsting 2754446
933          org_availability_info.primary_uom(1) := p_atp_record.quantity_uom;
934          IF PG_DEBUG in ('Y', 'C') THEN
935             msc_sch_wb.atp_debug('Top org uom: ' || p_atp_record.quantity_uom);
936          END IF;
937 
938 
939          --- loop through all orgs in supply chain to find item's availability in each org by request
940          --- date
941 
942          WHILE l_parent_org_cntr <= org_availability_info.organization_id.count and l_net_demand > 0 LOOP
943             IF PG_DEBUG in ('Y', 'C') THEN
944                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_net_demand : = ' || l_net_demand);
945                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'org count := ' || org_availability_info.organization_id.count);
946                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_parent_org_cntr := ' || l_parent_org_cntr);
947                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_process org cntr := ' || l_process_org_cntr);
948             END IF;
949             --- get plan_id in for each item org combo
950             IF l_item_cntr = l_item_count and l_process_org_cntr = 1 THEN
951                l_plan_id := p_plan_id;
952                l_assign_set_id := p_assign_set_id;
953                org_availability_info.plan_name(1) := item_availability_info.plan_name(1);
954             ELSE
955                /*
956 	        MSC_ATP_PROC.Get_Plan_Info(p_atp_record.instance_id,
957                             item_availability_info.sr_inventory_item_id(l_item_cntr),
958                             org_availability_info.organization_id(l_process_org_cntr),
959                             p_atp_record.demand_class,
960                             l_plan_id,
961                             l_assign_set_id);
962 		*/
963 		-- changes start for bug 2392456
964                 /*MSC_ATP_PROC.Get_Plan_Info(p_atp_record.instance_id,
965                             item_availability_info.sr_inventory_item_id(l_item_cntr),
966                             org_availability_info.organization_id(l_process_org_cntr),
967                             p_atp_record.demand_class,
968                             l_plan_info_rec);*/
969                 IF l_process_org_cntr = 1 THEN
970                    --bug3510475 Same Org different Item.Switch plan
971                    MSC_ATP_PROC.Get_Plan_Info(p_atp_record.instance_id,
972                             item_availability_info.sr_inventory_item_id(l_item_cntr),
973                             org_availability_info.organization_id(l_process_org_cntr),
974                             p_atp_record.demand_class,
975                             l_plan_info_rec,
976                             NULL);
977                 ELSE
978                    --bug3510475 Same Item, different Org. use parent plan id
979                    MSC_ATP_PROC.Get_Plan_Info(p_atp_record.instance_id,
980                             item_availability_info.sr_inventory_item_id(l_item_cntr),
981                             org_availability_info.organization_id(l_process_org_cntr),
982                             p_atp_record.demand_class,
983                             l_plan_info_rec,
984                             p_plan_id);
985                 END IF;
986                 l_plan_id               := l_plan_info_rec.plan_id;
987                 l_assign_set_id         := l_plan_info_rec.assignment_set_id;
988                 -- changes end for bug 2392456
989 
990                IF l_plan_id in (-1, -100) THEN
991                   --- this should not happen but if we do not find plan for this item then
992                   --- we do atp in requested item's plan
993                   l_plan_id := p_plan_id;
994                   l_assign_set_id := p_assign_set_id;
995                END IF;
996 
997 	       -- dsting diagnostic atp
998                IF PG_DEBUG in ('Y', 'C') THEN
999                   msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'plan_name: ' || l_plan_info_rec.plan_name || ' process org: ' || l_process_org_cntr);
1000                END IF;
1001 	       org_availability_info.plan_name(l_process_org_cntr) := l_plan_info_rec.plan_name;
1002 
1003             END IF;
1004             IF PG_DEBUG in ('Y', 'C') THEN
1005                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_plan_id := ' || l_plan_id);
1006                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_assign_set_id := ' || l_assign_set_id);
1007             END IF;
1008             --- get org code for pegging purpose
1009             -- store this info for top org
1010             -- we will use this info for CTP
1011             IF l_process_org_cntr = 1 THEN
1012                item_availability_info.plan_id(l_item_cntr) := l_plan_id;
1013                item_availability_info.assign_set_id(l_item_cntr) := l_assign_set_id;
1014             END IF;
1015             org_availability_info.plan_id(l_process_org_cntr) := l_plan_id;
1016             org_availability_info.assign_set_id(l_process_org_cntr) := l_assign_set_id;
1017 
1018             l_org_code := MSC_ATP_FUNC.get_org_code(p_atp_record.instance_id,
1019                                        org_availability_info.organization_id(l_process_org_cntr));
1020             IF PG_DEBUG in ('Y', 'C') THEN
1021                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_org_code := ' || l_org_code);
1022             END IF;
1023             IF l_process_org_cntr = 1 THEN
1024                l_top_tier_org_code := l_org_code;
1025                IF PG_DEBUG in ('Y', 'C') THEN
1026                   msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_top_tier_org_code := ' || l_top_tier_org_code);
1027                END IF;
1028             END IF;
1029             org_availability_info.org_code(l_process_org_cntr) := l_org_code;
1030 
1031             -- ATP4drp Do not fetch family data for DRP plans.
1032             -- ALLOC ATP CHANGES, l_plan_info_rec.plan_type will be NULL if only one record
1033 			IF nvl(l_plan_info_rec.plan_type, NVL(MSC_ATP_PVT.G_PLAN_INFO_REC.plan_type, 1)) <> 5 THEN
1034                /* time_phased_atp
1035                   populate family_id and atf_date*/
1036                MSC_ATP_PF.Get_Family_Item_Info(
1037                   p_atp_record.instance_id,
1038                   l_plan_id,
1039                   item_availability_info.inventory_item_id(l_item_cntr),
1040                   org_availability_info.organization_id(l_process_org_cntr),
1041                   org_availability_info.family_dest_id(l_process_org_cntr),
1042                   org_availability_info.family_sr_id(l_process_org_cntr),
1043                   org_availability_info.atf_date(l_process_org_cntr),
1044                   --bug3700564
1045                   org_availability_info.family_item_name(l_process_org_cntr),
1046                   l_return_status
1047                );
1048                IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1049                    IF PG_DEBUG in ('Y', 'C') THEN
1050                         msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Error occured in procedure Get_Family_Item_Info');
1051                    END IF;
1052                    RAISE FND_API.G_EXC_ERROR;
1053                END IF;
1054             ELSE -- DRP plan re-set variables.
1055                -- Allocated and PF ATP de-supported for DRP Plans.
1056                MSC_ATP_PVT.G_ALLOCATED_ATP := 'N';
1057                org_availability_info.family_sr_id(l_process_org_cntr)  :=
1058                                         item_availability_info.sr_inventory_item_id(l_item_cntr);
1059                org_availability_info.family_dest_id(l_process_org_cntr)  :=
1060                                         item_availability_info.inventory_item_id(l_item_cntr);
1061                IF PG_DEBUG in ('Y', 'C') THEN
1062                   msc_sch_wb.atp_debug('----- ATP4drp Specific Debug Messages -----');
1063                   msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'PF and Allocated ATP not supported for DRP Plans');
1064                   msc_sch_wb.atp_debug('Re-Set Family sr ITEM ID : ' || org_availability_info.family_sr_id(l_process_org_cntr));
1065                   msc_sch_wb.atp_debug('Re-Set Family ITEM ID: ' || org_availability_info.family_dest_id(l_process_org_cntr));
1066                   msc_sch_wb.atp_debug('----- ATP4drp Specific Debug Messages -----');
1067                END IF;
1068             END IF;
1069             -- End ATP4drp
1070 
1071             /* To support new logic for dependent demands allocation in time phased PF rule based AATP scenarios
1072                Set global variable. This is used in Get_Item_Demand_Alloc_Percent function*/
1073             IF org_availability_info.atf_date(l_process_org_cntr) is not null THEN
1074                    /* Set global variable. This is used in Get_Item_Demand_Alloc_Percent function*/
1075                    MSC_ATP_PVT.G_TIME_PHASED_PF_ENABLED := 'Y';
1076             ELSE
1077                    MSC_ATP_PVT.G_TIME_PHASED_PF_ENABLED := 'N';
1078             END IF;
1079 
1080             -- dsting 2754446 Add uom conversion
1081             -- l_net_demand and demand_quantity stays in request org's uom
1082             IF l_process_org_cntr = 1 THEN
1083                        org_availability_info.conversion_rate(l_process_org_cntr) := 1;
1084             ELSE
1085                MSC_ATP_PROC.inv_primary_uom_conversion(p_atp_record.instance_id,
1086                          org_availability_info.organization_id(l_process_org_cntr),
1087                          item_availability_info.sr_inventory_item_id(l_item_cntr),
1088                          org_availability_info.primary_uom(l_parent_index),
1089                          org_availability_info.primary_uom(l_process_org_cntr),
1090                          org_availability_info.conversion_rate(l_process_org_cntr));
1091             END IF;
1092             -- 2754446
1093             org_availability_info.demand_quantity(l_process_org_cntr) :=
1094                 org_req_dmd_qty(l_net_demand, org_availability_info, l_process_org_cntr);
1095 --            org_availability_info.demand_quantity(l_process_org_cntr) := l_net_demand;
1096 
1097             IF PG_DEBUG in ('Y', 'C') THEN
1098                msc_sch_wb.atp_debug('process org: ' || org_availability_info.organization_id(l_process_org_cntr));
1099                msc_sch_wb.atp_debug('UOM process org: ' || org_availability_info.primary_uom(l_process_org_cntr));
1100                msc_sch_wb.atp_debug('conversion rate: ' || org_availability_info.conversion_rate(l_process_org_cntr));
1101                msc_sch_wb.atp_debug('demand_quantity ' ||  l_org_code || ' ' || org_availability_info.demand_quantity(l_process_org_cntr));
1102             END IF;
1103             --- get item attributes
1104             IF l_process_org_cntr = 1 and l_item_cntr = l_item_count THEN
1105                IF PG_DEBUG in ('Y', 'C') THEN
1106                   msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'No Need to get item attributes');
1107                END IF;
1108                --we have already got values for requested item in top org
1109                org_availability_info.atp_flag(l_process_org_cntr) := item_availability_info.atp_flag(l_item_cntr);
1110                org_availability_info.atp_comp_flag(l_process_org_cntr) := item_availability_info.atp_comp_flag(l_item_cntr);
1111                org_availability_info.post_pro_lt(l_process_org_cntr) := item_availability_info.post_pro_lt(l_item_cntr);
1112                org_availability_info.pre_pro_lt(l_process_org_cntr) := item_availability_info.pre_pro_lt(l_item_cntr);
1113                org_availability_info.variable_lt(l_process_org_cntr) := item_availability_info.variable_lt(l_item_cntr);
1114                org_availability_info.fixed_lt(l_process_org_cntr) := item_availability_info.fixed_lt(l_item_cntr);
1115                --- if it the req item then we dont want to consider substitution window
1116                l_substitution_window := 0;
1117                l_create_supply_flag := item_availability_info.create_supply_flag(l_item_cntr);
1118 
1119 	       -- dsting for diagnostic atp
1120 	       org_availability_info.rounding_flag(l_process_org_cntr) := item_availability_info.rounding_control_type(l_item_cntr);
1121 	       org_availability_info.weight_uom(l_process_org_cntr) := item_availability_info.weight_uom(l_item_cntr);
1122 	       org_availability_info.unit_weight(l_process_org_cntr) := item_availability_info.unit_weight(l_item_cntr);
1123 	       org_availability_info.unit_volume(l_process_org_cntr) := item_availability_info.unit_volume(l_item_cntr);
1124 	       org_availability_info.volume_uom(l_process_org_cntr) := item_availability_info.volume_uom(l_item_cntr);
1125             ELSE
1126                /*MSC_ATP_PROC.get_item_attributes(p_atp_record.instance_id,
1127                                             -1,
1128                                             item_availability_info.sr_inventory_item_id(l_item_cntr),
1129                                             org_availability_info.organization_id(l_process_org_cntr),
1130                                             l_item_attribute_rec);*/
1131                MSC_ATP_PROC.get_global_item_info(p_atp_record.instance_id,
1132                                             --bug 3917625: Read item attributes from planned data
1133                                             -- -1,
1134                                             l_plan_id,
1135                                             item_availability_info.sr_inventory_item_id(l_item_cntr),
1136                                             org_availability_info.organization_id(l_process_org_cntr),
1137                                             l_item_attribute_rec);--bug3298426
1138 
1139                l_item_attribute_rec := MSC_ATP_PVT.G_ITEM_INFO_REC;--bug3298426
1140                -- store values
1141                org_availability_info.atp_flag(l_process_org_cntr) := l_item_attribute_rec.atp_flag;
1142                org_availability_info.atp_comp_flag(l_process_org_cntr) := l_item_attribute_rec.atp_comp_flag;
1143                org_availability_info.post_pro_lt(l_process_org_cntr) := l_item_attribute_rec.post_pro_lt;
1144                org_availability_info.pre_pro_lt(l_process_org_cntr) := l_item_attribute_rec.pre_pro_lt;
1145                org_availability_info.fixed_lt(l_process_org_cntr) := l_item_attribute_rec.fixed_lt;
1146                org_availability_info.variable_lt(l_process_org_cntr) := l_item_attribute_rec.variable_lt;
1147 	       -- dsting diagnostic atp
1148 	       org_availability_info.rounding_flag(l_process_org_cntr) := l_item_attribute_rec.rounding_control_type;
1149 	       org_availability_info.weight_uom(l_process_org_cntr) := l_item_attribute_rec.weight_uom;
1150 	       org_availability_info.unit_weight(l_process_org_cntr) := l_item_attribute_rec.unit_weight;
1151 	       org_availability_info.unit_volume(l_process_org_cntr) := l_item_attribute_rec.unit_volume;
1152 	       org_availability_info.volume_uom(l_process_org_cntr) := l_item_attribute_rec.volume_uom;
1153                -- dsting 2754446
1154 	       org_availability_info.primary_uom(l_process_org_cntr) := l_item_attribute_rec.uom_code;
1155 
1156                l_substitution_window := l_item_attribute_rec.substitution_window;
1157                IF l_process_org_cntr > 1 or (l_process_org_cntr = 1 and l_item_cntr = l_item_count) THEN
1158                   -- we reset subst_window to zero for org lower than the top org
1159                   -- because in Get mat atp info we do not want to offset req date by subts window
1160                   -- to compare the resultant with x_atp_date this level as we need this
1161                   -- in top org only
1162                   -- also we set the substitution window to be zero for the req item
1163                   l_substitution_window := 0;
1164                END IF;
1165                l_create_supply_flag := l_item_attribute_rec.create_supply_flag;
1166             END IF;
1167             IF PG_DEBUG in ('Y', 'C') THEN
1168                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_substitution_window := ' || l_substitution_window);
1169                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_create_supply_flag := '||l_create_supply_flag);
1170             END IF;
1171 
1172             IF l_process_org_cntr = 1 and l_item_cntr <> l_item_count THEN
1173                item_availability_info.atp_flag(l_item_cntr) := l_item_attribute_rec.atp_flag;
1174                item_availability_info.atp_comp_flag(l_item_cntr)
1175                                               := l_item_attribute_rec.atp_comp_flag;
1176                item_availability_info.substitution_window(l_item_cntr) := l_substitution_window;
1177                item_availability_info.create_supply_flag(l_item_cntr) := l_create_supply_flag;
1178 
1179 		-- diag_atp
1180                item_availability_info.post_pro_lt(l_item_cntr) := l_item_attribute_rec.post_pro_lt;
1181                item_availability_info.pre_pro_lt(l_item_cntr) := l_item_attribute_rec.pre_pro_lt;
1182                item_availability_info.variable_lt(l_item_cntr) := l_item_attribute_rec.variable_lt;
1183                item_availability_info.fixed_lt(l_item_cntr) := l_item_attribute_rec.fixed_lt;
1184                item_availability_info.volume_uom(l_item_cntr) := l_item_attribute_rec.volume_uom;
1185                item_availability_info.unit_volume(l_item_cntr) := l_item_attribute_rec.unit_volume;
1186                item_availability_info.weight_uom(l_item_cntr) := l_item_attribute_rec.weight_uom;
1187                item_availability_info.unit_weight(l_item_cntr) := l_item_attribute_rec.unit_weight;
1188 
1189             END IF;
1190             ---alloc
1191 
1192 			-- ALLOC ATP CHANGES, 12973673
1193 			IF (MSC_ATP_PVT.G_INV_CTP = 4 and MSC_ATP_PVT.G_HYBRID_ALLOC_ATP = 'Y') THEN
1194 				IF PG_DEBUG in ('Y', 'C') THEN
1195                     msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'G_HYBRID_ALLOC_ATP is Y');
1196                 END IF;
1197 
1198 				MSC_ATP_PVT.G_ALLOCATED_ATP := MSC_ATP_FUNC.check_alloc_exists(p_atp_record.instance_id,
1199 								-1, --PLAN_ID
1200 								item_availability_info.sr_inventory_item_id(l_item_cntr),
1201 								org_availability_info.organization_id(l_process_org_cntr),
1202 								nvl(l_requested_ship_date,trunc(sysdate)));
1203 
1204 				IF PG_DEBUG in ('Y', 'C') THEN
1205                     msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'MSC_ATP_PVT.G_ALLOCATED_ATP = '||MSC_ATP_PVT.G_ALLOCATED_ATP);
1206                 END IF;
1207 			END IF;
1208 
1209             IF (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') THEN
1210                /* To support new logic for dependent demands allocation in time phased PF rule based AATP scenarios
1211                   Making changes to support new allocation logic for time_phased_atp changes too that were missed
1212                   out earlier as part of this exercise*/
1213                IF org_availability_info.atf_date(l_process_org_cntr) is not null THEN
1214                       MSC_ATP_PF.Set_Alloc_Rule_Variables(
1215                           item_availability_info.inventory_item_id(l_item_cntr),
1216                           --bug3467631 family_dest_id is item-org attribute
1217                           --item_availability_info.family_dest_id(l_item_cntr),
1218                           org_availability_info.family_dest_id(l_process_org_cntr),
1219                           org_availability_info.organization_id(l_process_org_cntr),
1220                           p_atp_record.instance_id,
1221                           p_atp_record.demand_class,
1222                           org_availability_info.atf_date(l_process_org_cntr),
1223                           l_return_status
1224                       );
1225                       IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1226                            IF PG_DEBUG in ('Y', 'C') THEN
1227                                 msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Error occured in procedure Set_Alloc_Rule_Variables');
1228                            END IF;
1229                            RAISE FND_API.G_EXC_ERROR;
1230                       END IF;
1231 
1232                       IF l_requested_ship_date <= org_availability_info.atf_date(l_process_org_cntr) THEN
1233                           IF MSC_ATP_PVT.G_MEM_RULE_WITHIN_ATF = 'Y' THEN
1234                               l_item_to_use := item_availability_info.inventory_item_id(l_item_cntr);
1235                           ELSE
1236                               --bug3467631 family_dest_id is item-org attribute
1237                               --l_item_to_use := item_availability_info.family_dest_id(l_item_cntr);
1238                               l_item_to_use := org_availability_info.family_dest_id(l_process_org_cntr);
1239                           END IF;
1240                       ELSE
1241                           IF MSC_ATP_PVT.G_PF_RULE_OUTSIDE_ATF = 'Y' THEN
1242                               --bug3467631 family_dest_id is item-org attribute
1243                               --l_item_to_use := item_availability_info.family_dest_id(l_item_cntr);
1244                               l_item_to_use := org_availability_info.family_dest_id(l_process_org_cntr);
1245                           ELSE
1246                               l_item_to_use := item_availability_info.inventory_item_id(l_item_cntr);
1247                           END IF;
1248                       END IF;
1249                ELSE
1250                       l_item_to_use := item_availability_info.inventory_item_id(l_item_cntr);
1251                END IF;
1252                IF PG_DEBUG in ('Y', 'C') THEN
1253                  msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_requested_ship_date = '||l_requested_ship_date);
1254                  msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Item to be used = '||l_item_to_use);
1255                END IF;
1256                /* New allocation logic for time_phased_atp changes end */
1257 
1258                --- get the demand class
1259                l_demand_class :=
1260                   MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(MSC_ATP_PVT.G_PARTNER_ID,
1261                            MSC_ATP_PVT.G_PARTNER_SITE_ID,
1262                            l_item_to_use,
1263                            org_availability_info.organization_id(l_process_org_cntr),
1264                            p_atp_record.instance_id,
1265                            l_requested_ship_date,
1266                            NULL, -- level_id
1267                            p_atp_record.demand_class);
1268                IF PG_DEBUG in ('Y', 'C') THEN
1269                   msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'after getting the dummy demand class');
1270                   msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'demand_class = '|| l_demand_class);
1271                END IF;
1272 
1273                org_availability_info.demand_class(l_process_org_cntr) := l_demand_class;
1274 	       org_availability_info.allocation_rule(l_process_org_cntr) := MSC_ATP_PVT.G_ALLOCATION_RULE_NAME;
1275 
1276                --- we store demand class for top org so that we can use it during CTP/ Forward search etc
1277                IF l_process_org_cntr = 1 THEN
1278                   item_availability_info.demand_class(l_item_cntr) := l_demand_class;
1279                END IF;
1280             END IF;
1281             --Do material check
1282             l_atp_flag := org_availability_info.atp_flag(l_process_org_cntr);
1283             l_atp_comp_flag := org_availability_info.atp_comp_flag(l_process_org_cntr);
1284             IF PG_DEBUG in ('Y', 'C') THEN
1285                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_atp_flag := ' || l_atp_flag);
1286                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_atp_comp_flag := ' || l_atp_comp_flag);
1287             END IF;
1288             IF l_atp_flag = 'N' and l_atp_comp_flag = 'N' THEN
1289                IF l_process_org_cntr = 1
1290                    and org_availability_info.requested_ship_date(l_process_org_cntr) < l_sys_date_top_org THEN
1291                   l_requested_date_quantity := 0;
1292                ELSE
1293                   ---request date > sys date and process_org > 1
1294                   --- if request date < sysdate and process_org > 1: This would never happen as we
1295                   --- would never add this org to the list of orgs
1296                   -- 2754446
1297                   --l_requested_date_quantity := l_net_demand;
1298                   l_requested_date_quantity := org_availability_info.demand_quantity(l_process_org_cntr);
1299                   l_net_demand := 0;
1300                END IF;
1301                org_availability_info.request_date_quantity(l_process_org_cntr) := l_requested_date_quantity;
1302 
1303                IF PG_DEBUG in ('Y', 'C') THEN
1304                   msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Item is not atpable');
1305                END IF;
1306             ELSE
1307 		l_get_mat_in_rec.rounding_control_flag := org_availability_info.rounding_flag(l_process_org_cntr);
1308 		l_get_mat_in_rec.dest_inv_item_id := item_availability_info.inventory_item_id(l_item_cntr);
1309                IF l_atp_flag in ('Y', 'C') THEN
1310 
1311                    -- 2754446
1312                    --l_PO_qty := l_net_demand * org_availability_info.conversion_rate(l_parent_index);
1313                    l_PO_qty := org_req_dmd_qty(l_net_demand, org_availability_info, l_parent_index);
1314                    IF nvl(org_availability_info.rounding_flag(l_process_org_cntr), 2) = 1 THEN
1315                       l_PO_qty := CEIL(l_PO_qty);
1316                    END IF;
1317                    -- time_phased_atp changes begin
1318                    l_mat_atp_info_rec.instance_id               := p_atp_record.instance_id;
1319                    l_mat_atp_info_rec.plan_id                   := l_plan_id;
1320                    l_mat_atp_info_rec.level                     := p_level;
1321                    l_mat_atp_info_rec.identifier                := p_atp_record.identifier;
1322                    l_mat_atp_info_rec.scenario_id               := p_scenario_id;
1323                    l_mat_atp_info_rec.inventory_item_id         := org_availability_info.family_sr_id(l_process_org_cntr);
1324                    l_mat_atp_info_rec.request_item_id           := item_availability_info.sr_inventory_item_id(l_item_cntr);
1325                    l_mat_atp_info_rec.organization_id           := org_availability_info.organization_id(l_process_org_cntr);
1326                    l_mat_atp_info_rec.requested_date            := org_availability_info.requested_ship_date(l_process_org_cntr);
1327                    l_mat_atp_info_rec.quantity_ordered          := l_PO_qty * org_availability_info.conversion_rate(l_process_org_cntr);
1328                    l_mat_atp_info_rec.demand_class              := l_demand_Class;
1329                    l_mat_atp_info_rec.insert_flag               := p_atp_record.insert_flag;
1330                    l_mat_atp_info_rec.rounding_control_flag     := l_get_mat_in_rec.rounding_control_flag;
1331                    l_mat_atp_info_rec.dest_inv_item_id          := l_get_mat_in_rec.dest_inv_item_id;
1332                    l_mat_atp_info_rec.infinite_time_fence_date  := l_get_mat_in_rec.infinite_time_fence_date;
1333                    l_mat_atp_info_rec.plan_name                 := l_get_mat_in_rec.plan_name;
1334                    l_mat_atp_info_rec.optimized_plan            := l_get_mat_in_rec.optimized_plan;
1335                    ---since it is a backward material check we pass window as zero
1336                    l_mat_atp_info_rec.substitution_window       := 0;
1337                    l_mat_atp_info_rec.refresh_number            := p_refresh_number;
1338                    l_mat_atp_info_rec.atf_date                  := org_availability_info.atf_date(l_process_org_cntr);
1339                    l_mat_atp_info_rec.shipping_cal_code         := org_availability_info.shipping_cal_code(l_process_org_cntr); -- Bug 3371817
1340 
1341                    MSC_ATP_REQ.Get_Material_Atp_Info(
1342                            l_mat_atp_info_rec,
1343                            l_atp_period,
1344                            l_atp_supply_demand,
1345                            l_return_status);
1346 
1347                    l_requested_date_quantity                    := l_mat_atp_info_rec.requested_date_quantity;
1348                    l_atf_date_qty                               := l_mat_atp_info_rec.atf_date_quantity;
1349                    l_atp_date_this_level                        := l_mat_atp_info_rec.atp_date_this_level;
1350                    l_atp_date_quantity_this_level               := l_mat_atp_info_rec.atp_date_quantity_this_level;
1351                    l_get_mat_out_rec.atp_rule_name              := l_mat_atp_info_rec.atp_rule_name;
1352                    l_get_mat_out_rec.infinite_time_fence_date   := l_mat_atp_info_rec.infinite_time_fence_date;
1353 
1354                    --bug3467631 start
1355                    IF (l_mat_atp_info_rec.inventory_item_id <> l_mat_atp_info_rec.request_item_id)
1356                       and (l_mat_atp_info_rec.atf_date is not null) --added atf_date constraint
1357                       --also while setting l_time_phased_atp := 'Y'
1358                    THEN
1359                       l_time_phased_atp := 'Y';
1360                    ELSE
1361                       l_time_phased_atp := 'N';
1362                    END IF;
1363 
1364                    IF PG_DEBUG in ('Y', 'C') THEN
1365                       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'atf_date := ' || l_mat_atp_info_rec.atf_date);
1366                       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_time_phased_atp := ' || l_time_phased_atp);
1367                       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_atf_date_qty := ' || l_atf_date_qty);
1368                    END IF;
1369                    --bug3467631 end
1370                    -- time_phased_atp changes end
1371 
1372                    -- dsting 2754446
1373                    org_availability_info.req_date_unadj_qty(l_process_org_cntr) := l_requested_date_quantity;
1374                    org_availability_info.rnding_leftover(l_process_org_cntr) :=
1375                       nvl(org_availability_info.rnding_leftover(l_process_org_cntr),0)
1376                       + greatest(0,l_requested_date_quantity);
1377                    --bug3467631 setting atf_date_quantity to be passed correctly to top_org_supply_qty
1378                    org_availability_info.atf_date_quantity(l_process_org_cntr) := l_atf_date_qty;
1379                    top_org_supply_qty(org_availability_info, l_process_org_cntr);
1380                    -- dsting 2754446 round down l_requested_date_quantity so it can open a
1381                    -- whole numbered PO in the parent org
1382 
1383                    IF nvl(org_availability_info.rounding_flag(l_process_org_cntr), 2) = 1 THEN
1384                       l_requested_date_quantity := FLOOR(ROUND(l_requested_date_quantity
1385                                / org_availability_info.conversion_rate(l_process_org_cntr), 10)
1386                           ) * org_availability_info.conversion_rate(l_process_org_cntr);
1387                    ELSE
1388                       l_requested_date_quantity := l_requested_date_quantity;
1389                    END IF;
1390 
1391 		   -- dsting diagnostic atp
1392                    IF PG_DEBUG in ('Y', 'C') THEN
1393                       msc_sch_wb.atp_debug('rounding_flag: ' || org_availability_info.rounding_flag(l_process_org_cntr));
1394                       msc_sch_wb.atp_debug('parent index: ' || l_parent_index);
1395                       msc_sch_wb.atp_debug('process org cntr: ' || l_process_org_cntr);
1396                       msc_sch_wb.atp_debug('parent conversion rate: ' || org_availability_info.conversion_rate(l_parent_index));
1397                       msc_sch_wb.atp_debug('process org conversion rate: ' || org_availability_info.conversion_rate(l_process_org_cntr));
1398                       msc_sch_wb.atp_debug('l_requested_date_quantity: ' || l_requested_date_quantity);
1399                       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'after get_material_atp_info');
1400 		      msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'infinite_time_fenc_date ' || l_get_mat_out_rec.infinite_time_fence_date ||
1401 			' atp_rule_name ' || l_get_mat_out_rec.atp_rule_name);
1402 		   END IF;
1403    		   org_availability_info.infinite_time_fence(l_process_org_cntr) := l_get_mat_out_rec.infinite_time_fence_date;
1404    		   org_availability_info.atp_rule_name(l_process_org_cntr) := l_get_mat_out_rec.atp_rule_name;
1405 
1406                   org_availability_info.request_date_quantity(l_process_org_cntr) := l_requested_date_quantity;
1407                   /*IF l_process_org_cntr = 1 THEN
1408                      --- for top org we store the atp date on which requested quantity is available
1409                      item_availability_info.future_atp_date(l_item_cntr) := l_atp_date_this_level;
1410                      item_availability_info.atp_date_quantity(l_item_cntr) := l_atp_date_quantity_this_level;
1411                   END IF; */
1412                ELSE
1413                   org_availability_info.request_date_quantity(l_process_org_cntr) := 0;
1414                   l_requested_date_quantity := 0;
1415                END IF; -- IF l_atp_flag = 'Y'
1416                IF PG_DEBUG in ('Y', 'C') THEN
1417                   msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_requested_date_quantity := ' || l_requested_date_quantity);
1418                END IF;
1419 
1420                ---add material demand
1421 
1422                l_atp_insert_rec.instance_id := p_atp_record.instance_id;
1423 
1424                -- time_phased_atp changes begin
1425                IF l_time_phased_atp = 'Y' THEN
1426                    --Commented out as a part of bug3467631 as family_dest_id,atf_date,atf_date_quantity
1427                    -- are item org attribites
1428                    /*l_atp_insert_rec.inventory_item_id := item_availability_info.family_dest_id(l_item_cntr);
1429                    l_atp_insert_rec.request_item_id := item_availability_info.inventory_item_id(l_item_cntr);
1430                    l_atp_insert_rec.atf_date := item_availability_info.atf_date(l_item_cntr);
1431                    l_atp_insert_rec.atf_date_quantity := l_atf_date_qty;*/
1432                    --bug3467631 start
1433                    l_atp_insert_rec.inventory_item_id := org_availability_info.family_dest_id(l_process_org_cntr);
1434                    l_atp_insert_rec.request_item_id := item_availability_info.inventory_item_id(l_item_cntr);
1435                    l_atp_insert_rec.atf_date := org_availability_info.atf_date(l_process_org_cntr);
1436                    l_atp_insert_rec.atf_date_quantity := org_availability_info.atf_date_quantity(l_process_org_cntr);
1437                    l_atp_insert_rec.requested_date_quantity := org_availability_info.request_date_quantity(l_process_org_cntr);
1438                    IF PG_DEBUG in ('Y', 'C') THEN
1439                       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_atp_insert_rec.inventory_item_id := ' || l_atp_insert_rec.inventory_item_id);
1440                       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_atp_insert_rec.request_item_id := ' || l_atp_insert_rec.request_item_id);
1441                       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_atp_insert_rec.atf_date := ' || l_atp_insert_rec.atf_date);
1442                       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_atp_insert_rec.atf_date_quantity := ' || l_atp_insert_rec.atf_date_quantity);
1443                    END IF;
1444                    --bug3467631 end
1445                ELSE
1446                    l_atp_insert_rec.request_item_id := item_availability_info.inventory_item_id(l_item_cntr);
1447                    l_atp_insert_rec.inventory_item_id := item_availability_info.inventory_item_id(l_item_cntr);
1448                END IF;
1449                -- time_phased_atp changes end
1450 
1451                l_atp_insert_rec.organization_id := org_availability_info.organization_id(l_process_org_cntr);
1452                l_atp_insert_rec.identifier := p_atp_record.identifier;
1453                l_atp_insert_rec.demand_source_type:=nvl(p_atp_record.demand_source_type, 2);
1454                l_atp_insert_rec.demand_source_header_id :=
1455                                nvl(p_atp_record.demand_source_header_id, -1);
1456                l_atp_insert_rec.demand_source_delivery :=
1457                                p_atp_record.demand_source_delivery;
1458 
1459                l_atp_insert_rec.requested_ship_date :=
1460                           org_availability_info.requested_ship_date(l_process_org_cntr);
1461                l_atp_insert_rec.demand_class := l_demand_class;
1462                l_atp_insert_rec.order_number := p_atp_record.order_number;
1463 
1464                -- for performance reason, we call these function here and
1465                -- then populate the pegging tree with the values
1466 
1467 
1468                l_org_code := MSC_ATP_FUNC.get_org_code(p_atp_record.instance_id,
1469                                           p_atp_record.organization_id);
1470 
1471 
1472                IF l_process_org_cntr = 1 THEN
1473                   --- top level demand
1474                   -- Modified by ngoel on 1/12/2001 for origination_type = 30
1475                   --l_atp_insert_rec.origination_type := 6;
1476                   l_atp_insert_rec.origination_type := 30;
1477 
1478                   l_atp_insert_rec.demand_source_line := p_atp_record.demand_source_line;
1479 
1480                   IF MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y' and l_atp_flag in ('Y', 'C')  and l_net_demand > 0 THEN
1481                      --- if we are doing allocated atp then we would need to do stealing
1482                      --- In case of stealing we would need to put only demand for available quantity
1483                      --- If we put ordered quantity then we would need to readjust this demand again
1484                      ----Instea we just put how the available qty so that we do not need to re-adjust the demand
1485 
1486                      --- bug 2346439: Place demand on least of req_date_qty and net demand
1487                      -- dsting 2754446
1488                      l_atp_insert_rec.quantity_ordered := least(greatest(0, l_requested_date_quantity),
1489                                                                                           l_net_demand);
1490 
1491                      IF PG_DEBUG in ('Y', 'C') THEN
1492                         msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Alloc ATP, top org,We put demand for := ' ||
1493                                                           l_atp_insert_rec.quantity_ordered);
1494                      END IF;
1495                   ELSE
1496 
1497                      l_atp_insert_rec.quantity_ordered := p_atp_record.quantity_ordered;
1498                   END IF;
1499 
1500                   l_atp_insert_rec.refresh_number := p_atp_record.refresh_number;
1501                ELSE
1502                   -- dependent demand
1503                   -- this is not the top level demand, which we should consider it
1504                   -- as planned order demand
1505                   IF (NVL(MSC_ATP_PVT.G_PLAN_INFO_REC.plan_type,1) <> 5) THEN  --4686870
1506                       l_atp_insert_rec.origination_type := 1;
1507                   ELSE
1508                       l_atp_insert_rec.origination_type := -200;
1509                   END IF; --4686870
1510 
1511                   l_atp_insert_rec.demand_source_line := null;
1512 
1513                   -- dsting diag_atp
1514                   IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = DIAGNOSTIC_ATP THEN
1515                      IF PG_DEBUG in ('Y', 'C') THEN
1516                         msc_sch_wb.atp_debug('place full dmd: ' || l_net_demand);
1517                      END IF;
1518                      -- dsting 2754446
1519 --                     l_atp_insert_rec.quantity_ordered := l_net_demand;
1520                      l_atp_insert_rec.quantity_ordered := org_availability_info.demand_quantity(l_process_org_cntr);
1521                   ELSE
1522                      IF PG_DEBUG in ('Y', 'C') THEN
1523                         msc_sch_wb.atp_debug('place partial dmd: ' || l_atp_insert_rec.quantity_ordered);
1524                      END IF;
1525                      -- dsting 2754446 rounding error?
1526                      l_atp_insert_rec.quantity_ordered :=
1527                            least(greatest(0, l_requested_date_quantity),
1528                                  l_PO_qty * org_availability_info.conversion_rate(l_process_org_cntr));
1529 --                                 org_availability_info.demand_quantity(l_process_org_cntr));
1530 --                     l_atp_insert_rec.quantity_ordered := least(greatest(0, l_requested_date_quantity), l_net_demand);
1531                   END IF;
1532                   l_atp_insert_rec.refresh_number := p_atp_record.refresh_number;   -- For summary enhancement
1533 
1534                END IF;
1535 
1536                -- time_phased_atp changes begin
1537                --l_atp_insert_rec.request_item_id := MSC_ATP_SUBST.G_REQ_ITEM_SR_INV_ID;
1538                l_atp_insert_rec.original_item_id := l_inventory_item_id ; --bug 5564075
1539                l_atp_insert_rec.latest_acceptable_date :=p_atp_record.latest_acceptable_date;
1540                -- ship_rec_cal
1541                l_atp_insert_rec.ship_method := p_atp_record.ship_method;
1542                l_atp_insert_rec.ship_set_name := p_atp_record.ship_set_name; --bug3263368
1543    	           l_atp_insert_rec.arrival_set_name := p_atp_record.arrival_set_name; --bug3263368
1544                l_atp_insert_rec.Delivery_Lead_Time := p_atp_record.Delivery_Lead_Time; --bug3263368
1545 
1546               --bug 4568088: Pass original_request date
1547               l_atp_insert_rec.original_request_ship_date := nvl(p_atp_record.original_request_date,
1548                                                                      l_atp_insert_rec.requested_ship_date);
1549               --MSC_ATP_SUBST.Add_Mat_Demand(l_atp_insert_rec,
1550                MSC_ATP_DB_UTILS.Add_Mat_Demand(l_atp_insert_rec,
1551                                                l_plan_id,
1552                                                l_demand_class_flag,
1553                                                l_demand_id);
1554                -- time_phased_atp changes end
1555 
1556                IF PG_DEBUG in ('Y', 'C') THEN
1557                   msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_demand_id := ' || l_demand_id);
1558                END IF;
1559                -- store demand id in org_avail_info table as we would need to update the demand
1560                -- in lower orgs if we dont have enough quantity in the org
1561                org_availability_info.demand_id(l_process_org_cntr) := l_demand_id;
1562 
1563                --- we also store demand id  for top org in item_availability_info as well as
1564                --- we would need to adjust the demand before future ATP/CTP case
1565                IF l_process_org_cntr =1 THEN
1566                   item_availability_info.demand_id(l_item_cntr) := l_demand_id;
1567                END IF;
1568 
1569 
1570                ---- get pegging id for demand
1571                SELECT msc_full_pegging_s.nextval
1572                INTO   l_pegging_id
1573                FROM   dual;
1574 
1575                IF PG_DEBUG in ('Y', 'C') THEN
1576                   msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'demand pegging id := ' || l_pegging_id);
1577                END IF;
1578                org_availability_info.demand_pegging_id(l_process_org_cntr) :=  l_pegging_id;
1579 
1580                --- supply pegging id
1581                SELECT msc_full_pegging_s.nextval
1582                INTO   l_pegging_id
1583                FROM   dual;
1584 
1585                org_availability_info.supply_pegging_id(l_process_org_cntr) :=  l_pegging_id;
1586 
1587                FOR i in 1..l_atp_period.Level.COUNT LOOP
1588                   l_atp_period.Pegging_Id(i) := l_pegging_id;
1589                   l_atp_period.End_Pegging_Id(i) := org_availability_info.demand_pegging_id(1);
1590                END LOOP;
1591 
1592 	       IF p_atp_record.insert_flag <> 0 THEN
1593 	               MSC_ATP_DB_UTILS.move_SD_temp_into_mrp_details(l_pegging_id,
1594 					org_availability_info.demand_pegging_id(1));
1595 	       END IF;
1596 
1597                --- now add period and supply details
1598                MSC_ATP_PROC.Details_Output(l_atp_period,
1599                                            l_atp_supply_demand,
1600                                            l_all_atp_period,
1601                                            l_all_atp_supply_demand,
1602 			                   l_return_status);
1603                ---Now do stealing
1604 
1605 	       IF PG_DEBUG in ('Y', 'C') THEN
1606 		   msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'net demand ' || l_net_demand || ' requested qty ' || l_requested_date_quantity);
1607 	       END IF;
1608 
1609                -- 2754446
1610                -- l_net_demand := GREATEST(l_net_demand - greatest(0, l_requested_date_quantity), 0);
1611 
1612                l_net_demand := greatest(0,
1613                   l_orig_net_dmd - nvl(org_availability_info.rnding_leftover(1), 0));
1614 
1615                IF PG_DEBUG in ('Y', 'C') THEN
1616                   msc_sch_wb.atp_debug('orig_net_dmd: ' || l_orig_net_dmd);
1617                   msc_sch_wb.atp_debug('rnding_leftover(1): ' || org_availability_info.rnding_leftover(1));
1618                   msc_sch_wb.atp_debug('req_date_qty: ' || greatest(nvl(org_availability_info.request_date_quantity(1), 0),0));
1619                   msc_sch_wb.atp_debug('steal_qty: ' || org_availability_info.steal_qty(1));
1620                   msc_sch_wb.atp_debug('net_dmd: ' || l_net_demand);
1621                END IF;
1622 
1623                IF (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND (l_atp_flag in ('Y', 'C')) and l_net_demand > 0 THEN
1624 
1625                    IF PG_DEBUG in ('Y', 'C') THEN
1626                       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'we are in the setup for stealing');
1627                       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_net_demand := ' || l_net_demand);
1628                    END IF;
1629                    g_atp_record.error_code := MSC_ATP_PVT.ALLSUCCESS;
1630 
1631                    -- time_phased_atp
1632                    g_atp_record.inventory_item_id := org_availability_info.family_sr_id(l_process_org_cntr);
1633 
1634                    g_atp_record.request_item_id := item_availability_info.sr_inventory_item_id(l_item_cntr);
1635                    g_atp_record.organization_id := org_availability_info.organization_id(l_process_org_cntr);
1636                    g_atp_record.instance_id := p_atp_record.instance_id;
1637                    -- dsting 2754446
1638 --                   g_atp_record.quantity_ordered := l_net_demand;
1639                    g_atp_record.quantity_ordered := org_req_dmd_qty(l_net_demand,
1640                         org_availability_info, l_process_org_cntr);
1641                    g_atp_record.quantity_UOM := p_atp_record.quantity_UOM;
1642                    g_atp_record.requested_ship_date := org_availability_info.requested_ship_date(l_process_org_cntr);
1643                    g_atp_record.requested_arrival_date := null;
1644                    g_atp_record.latest_acceptable_date :=
1645                              p_atp_record.latest_acceptable_date;
1646                    g_atp_record.delivery_lead_time :=
1647                              p_atp_record.delivery_lead_time;
1648                    g_atp_record.freight_carrier := p_atp_record.freight_carrier;
1649                    g_atp_record.ship_method := p_atp_record.ship_method;
1650                    g_atp_record.demand_class := l_demand_class;
1651                    g_atp_record.override_flag := p_atp_record.override_flag;
1652                    g_atp_record.action := p_atp_record.action;
1653                    g_atp_record.ship_date := p_atp_record.ship_date;
1654                    g_atp_record.available_quantity :=
1655                              p_atp_record.available_quantity;
1656                    g_atp_record.requested_date_quantity :=
1657                              p_atp_record.requested_date_quantity;
1658                    g_atp_record.supplier_id := p_atp_record.supplier_id;
1659                    g_atp_record.supplier_site_id := p_atp_record.supplier_site_id;
1660                    g_atp_record.insert_flag := p_atp_record.insert_flag;
1661                    g_atp_record.order_number := p_atp_record.order_number;
1662                    g_atp_record.demand_source_line :=
1663                              p_atp_record.demand_source_line;
1664                    g_atp_record.demand_source_header_id :=
1665                              p_atp_record.demand_source_header_id;
1666                    g_atp_record.demand_source_type :=
1667                              p_atp_record.demand_source_type;
1668                    g_atp_record.shipping_cal_code := org_availability_info.shipping_cal_code(l_process_org_cntr); -- Bug 3371817
1669                    g_atp_record.receiving_cal_code := org_availability_info.receiving_cal_code(l_process_org_cntr); -- Bug 3826234
1670                    g_atp_record.intransit_cal_code := org_availability_info.intransit_cal_code(l_process_org_cntr); -- Bug 3826234
1671                    g_atp_record.manufacturing_cal_code := org_availability_info.manufacturing_cal_code(l_process_org_cntr); -- Bug 3826234
1672                    g_atp_record.to_organization_id := p_atp_record.to_organization_id;    -- Bug 3826234
1673                    -- for bug 1410327
1674                    g_atp_record.identifier :=
1675                              p_atp_record.identifier;
1676 
1677                    -- 2754446
1678                    l_stealing_requested_date_qty := g_atp_record.quantity_ordered;
1679                    -- l_stealing_requested_date_qty := l_net_demand;
1680                    MSC_ATP_PVT.G_DEMAND_PEGGING_ID := org_availability_info.demand_pegging_id(1);
1681 
1682                    -- time_phased_atp
1683                    g_atp_record.atf_date := org_availability_info.atf_date(l_process_org_cntr);
1684                    l_post_stealing_dmd := g_atp_record.quantity_ordered; --bug3467631 change done
1685                    -- so that l_post_stealing_dmd is not passed null which completes the fix of 2754446
1686 
1687                    IF PG_DEBUG in ('Y', 'C') THEN --bug3467631
1688                       msc_sch_wb.atp_debug('l_post_stealing_dmd: ' || g_atp_record.quantity_ordered);
1689                    END IF;
1690 
1691                    MSC_AATP_PVT.Stealing(
1692                             g_atp_record,
1693                             org_availability_info.demand_pegging_id(l_process_org_cntr),
1694                             p_scenario_id,
1695                             p_level,
1696                             1, -- p_search
1697                             l_plan_id,
1698                             l_post_stealing_dmd, -- dsting 2754446
1699                             l_mem_stealing_qty, -- For time_phased_atp
1700                             l_pf_stealing_qty,  -- For time_phased_atp
1701                             l_atp_supply_demand,
1702                             l_atp_period,
1703                             l_return_status,
1704                             p_refresh_number);  -- for summary enhancement
1705 
1706                    -- 1680212
1707                    -- dsting 2754446
1708                    l_stealing_qty := (l_stealing_requested_date_qty
1709                                       - greatest(l_post_stealing_dmd, 0));
1710                    org_availability_info.rnding_leftover(l_process_org_cntr) :=
1711                        nvl(org_availability_info.rnding_leftover(l_process_org_cntr),0)
1712                        + l_stealing_qty;
1713                    --bug3467631
1714                    org_availability_info.atf_date_quantity(l_process_org_cntr) :=
1715                         org_availability_info.atf_date_quantity(l_process_org_cntr)
1716 		        + nvl(l_mem_stealing_qty, 0);
1717                    top_org_supply_qty(org_availability_info, l_process_org_cntr);
1718                    l_net_demand := greatest(0,
1719                       l_orig_net_dmd - nvl(org_availability_info.rnding_leftover(1),0));
1720 
1721 --                   l_stealing_qty := l_stealing_requested_date_qty -
1722 --                                     greatest(l_net_demand, 0);
1723 --                   l_net_demand := l_net_demand - l_stealing_qty;
1724 
1725                    IF PG_DEBUG in ('Y', 'C') THEN
1726                       msc_sch_wb.atp_debug('l_stealing_qty: ' || l_stealing_qty);
1727                       msc_sch_wb.atp_debug('l_net_demand: ' || l_net_demand);
1728                       msc_sch_wb.atp_debug('l_post_stealing_dmd: ' || l_post_stealing_dmd);
1729                       msc_sch_wb.atp_debug('Qty till now got: ' || org_availability_info.rnding_leftover(l_process_org_cntr)); --bug3467631
1730                    END IF;
1731 
1732                    org_availability_info.steal_qty(l_process_org_cntr) := l_stealing_qty;
1733 
1734 		   -- dsting: Stealing proc aready puts sd details into mrp_atp_details
1735                    --- now add supply demand details to table
1736                    --- now add period and supply details
1737                    MSC_ATP_PROC.Details_Output(l_atp_period,
1738                                               l_atp_supply_demand,
1739                                               l_all_atp_period,
1740                                               l_all_atp_supply_demand,
1741                                               l_return_status);
1742 
1743                    l_stealing_requested_date_qty := l_stealing_requested_date_qty -
1744                                                     l_post_stealing_dmd;
1745 --                                                l_net_demand;
1746 
1747                    IF PG_DEBUG in ('Y', 'C') THEN
1748                       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'After Stealing :'||
1749                                              to_char(l_net_demand));
1750                    END IF;
1751 
1752 		  -- dsting diag_atp
1753 		  IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP <> DIAGNOSTIC_ATP THEN
1754                    --- now we update the demand
1755                    IF l_process_org_cntr =1 THEN
1756                       IF PG_DEBUG in ('Y', 'C') THEN
1757                          msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Top Org, update demand');
1758                       END IF;
1759                       --- for top org we always create order for the quantity ordered
1760                       MSC_ATP_SUBST.UPDATE_DEMAND(org_availability_info.demand_id(l_process_org_cntr),
1761                                               org_availability_info.plan_id(l_process_org_cntr),
1762                                               p_atp_record.quantity_ordered);
1763                       /* time_phased_atp
1764                          put qty stolen upto req date ((not upto ATF) on member item */
1765                       IF l_time_phased_atp = 'Y' THEN
1766 		        --using the same insert rec we prepared earlier
1767 		        l_atp_insert_rec.quantity_ordered :=  p_atp_record.quantity_ordered;
1768 		        l_atp_insert_rec.requested_date_quantity := org_availability_info.request_date_quantity(l_process_org_cntr)
1769 		                                                        + nvl(l_stealing_qty, 0);
1770 		        l_atp_insert_rec.atf_date_quantity := org_availability_info.atf_date_quantity(l_process_org_cntr);
1771 		        MSC_ATP_PF.Increment_Bucketed_Demands_Qty(
1772                                 l_atp_insert_rec,
1773                                 org_availability_info.plan_id(l_process_org_cntr),
1774                                 org_availability_info.demand_id(l_process_org_cntr),
1775                                 l_return_status
1776 		        );
1777                         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1778                            IF PG_DEBUG in ('Y', 'C') THEN
1779                                 msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Error occured in procedure Increment_Bucketed_Demands_Qty');
1780                            END IF;
1781                            RAISE FND_API.G_EXC_ERROR;
1782                         END IF;
1783                         /* Reset l_atf_date_qty to 0*/ --bug3467631
1784                         l_atf_date_qty := 0;
1785                       END IF;
1786                       -- time_phased_atp changes end
1787                    ELSIF l_stealing_qty > 0 THEN
1788                       IF PG_DEBUG in ('Y', 'C') THEN
1789                          msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'second or later org, update demand as there is some stolen qty');
1790                       END IF;
1791                       --- org in second or later tear. Create demand for quantity available (req date qty+steal qty)
1792                       MSC_ATP_SUBST.UPDATE_DEMAND(org_availability_info.demand_id(l_process_org_cntr),
1793                                               org_availability_info.plan_id(l_process_org_cntr),
1794                                               (org_availability_info.request_date_quantity(l_process_org_cntr)
1795                                                + l_stealing_qty
1796                                               ));
1797                       /* time_phased_atp
1798                          put qty stolen upto req date ((not upto ATF) on member item */
1799                       IF l_time_phased_atp = 'Y' THEN
1800 		        --using the same insert rec we prepared earlier
1801 		        l_atp_insert_rec.quantity_ordered :=  org_availability_info.request_date_quantity(l_process_org_cntr);
1802 		        l_atp_insert_rec.requested_date_quantity := org_availability_info.request_date_quantity(l_process_org_cntr)
1803 		                                                        + nvl(l_stealing_qty, 0);
1804 		        --bug3467631
1805 		        l_atp_insert_rec.atf_date_quantity := org_availability_info.atf_date_quantity(l_process_org_cntr);
1806 		        MSC_ATP_PF.Increment_Bucketed_Demands_Qty(
1807                                 l_atp_insert_rec,
1808                                 org_availability_info.plan_id(l_process_org_cntr),
1809                                 org_availability_info.demand_id(l_process_org_cntr),
1810                                 l_return_status
1811 		        );
1812                         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1813                            IF PG_DEBUG in ('Y', 'C') THEN
1814                                 msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Error occured in procedure Increment_Bucketed_Demands_Qty');
1815                            END IF;
1816                            RAISE FND_API.G_EXC_ERROR;
1817                         END IF;
1818                         /* Reset l_atf_date_qty to 0*/ --bug3467631
1819                         l_atf_date_qty := 0;
1820                       END IF;
1821                       -- time_phased_atp changes end
1822                    END IF;
1823 		  END IF;
1824                END IF; -- end if G_ALLOCATED_ATP
1825             END IF; -- IF l_atp_flag = 'N' and l_atp_comp_flag = 'N' THEN
1826 
1827             --l_net_demand := GREATEST(l_net_demand - greatest(0, l_requested_date_quantity), 0);
1828             IF PG_DEBUG in ('Y', 'C') THEN
1829                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_process_org_cntr := ' || l_process_org_cntr);
1830                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_parent_org counter 1 := ' || l_parent_org_cntr);
1831                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'org count := ' || org_availability_info.organization_id.count);
1832                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_requested_ship_date := ' || l_requested_ship_date);
1833                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_sys_date_top_org := ' || l_sys_date_top_org);
1834             END IF;
1835             IF l_net_demand > 0 and l_process_org_cntr >= org_availability_info.organization_id.count
1836                and l_requested_ship_date >= l_sys_date_top_org THEN
1837 
1838                --- we still have net demand and we have run out of organizations on current level. So we
1839                --  will move to next set of organizations
1840                l_sources_found := 0;
1841                -- dsting
1842                -- l_sources found = 1 iff there is a good transfer source found that doesn't violate constraints
1843                -- l_transfer_found = 1 if there is any transfer source
1844 	       l_transfer_found := 0;
1845                IF PG_DEBUG in ('Y', 'C') THEN
1846                   msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_parent_org_cntr := ' || l_parent_org_cntr);
1847                   msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_process_org_cntr := ' || l_process_org_cntr);
1848                END IF;
1849                WHILE l_sources_found = 0 AND l_parent_org_cntr <= org_availability_info.organization_id.count LOOP
1850                   IF PG_DEBUG in ('Y', 'C') THEN
1851                      msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Look for sources for org :' || ORG_AVAILABILITY_INFO.organization_id(l_parent_org_cntr));
1852                   END IF;
1853                   IF org_availability_info.atp_comp_flag(l_parent_org_cntr) in ('Y', 'C') THEN
1854                      --s_cto_rearch
1855                      l_item_sourcing_info_rec := l_null_item_sourcing_info_rec;
1856                      l_item_sourcing_info_rec.sr_inventory_item_id.extend;
1857                      l_item_sourcing_info_rec.line_id.extend;
1858                      l_item_sourcing_info_rec.ato_line_id.extend;
1859                      l_item_sourcing_info_rec.match_item_id.extend;
1860                      --e_cto_rearch
1861                      l_item_sourcing_info_rec.sr_inventory_item_id(1) :=
1862                                             ITEM_AVAILABILITY_INFO.sr_inventory_item_id(l_item_cntr);
1863                      MSC_ATP_PROC.Atp_Sources(p_atp_record.instance_id,
1864                            org_availability_info.plan_id(l_parent_org_cntr),
1865                            ITEM_AVAILABILITY_INFO.sr_inventory_item_id(l_item_cntr),
1866                            ORG_AVAILABILITY_INFO.organization_id(l_parent_org_cntr),
1867                            NULL,
1868                            NULL,
1869                            org_availability_info.assign_set_id(l_parent_org_cntr),
1870                            l_item_sourcing_info_rec,
1871                            --MRP_ATP_PUB.number_arr(NULL),
1872                            MSC_ATP_PVT.G_SESSION_ID,
1873                            l_sources,
1874                            l_return_status);
1875 	             IF l_sources.source_type.count = 0 THEN
1876 			org_availability_info.constraint_type(l_parent_org_cntr) := NOSOURCES_NONCONSTRAINT;
1877 		     END IF;
1878                   ELSE
1879                      l_sources := l_null_sources;
1880 		     IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = DIAGNOSTIC_ATP THEN
1881 			IF PG_DEBUG in ('Y', 'C') THEN
1882 			   msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'constraint type: MATERIAL_CONSTRAINT');
1883 			   msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'make_buy_code: ' || l_make_buy_cd);
1884 			END IF;
1885 			org_availability_info.constraint_type(l_parent_org_cntr) := MATERIAL_CONSTRAINT;
1886 		     END IF;
1887                   END IF;
1888                   IF l_return_status <> FND_API.G_RET_STS_SUCCESS  OR
1889                      l_sources.source_type.count = 0
1890 		  THEN
1891                      IF PG_DEBUG in ('Y', 'C') THEN
1892                         msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'No sources Found for this org');
1893                      END IF;
1894 
1895                   ELSE
1896                      IF PG_DEBUG in ('Y', 'C') THEN
1897                         msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Sources Found, process them');
1898                         msc_sch_wb.atp_debug('ATP_Check_Subst: ' || ' source count := ' || l_sources.organization_id.count);
1899                      END IF;
1900 
1901                      FOR i in 1..l_sources.organization_id.count LOOP
1902 
1903                         IF l_sources.source_type(i) = MSC_ATP_PVT.TRANSFER THEN
1904 
1905                            -- we only consider 'Transfer' type sources in this pass
1906 
1907                            l_to_location_id := null;
1908                            l_delivery_lead_time := l_sources.lead_time(i);
1909                            l_ship_method := l_sources.ship_method(i);
1910 
1911                            IF PG_DEBUG in ('Y', 'C') THEN
1912                               msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_delivery_lead_time := ' || l_delivery_lead_time);
1913                            END IF;
1914 
1915                            --bug3467631 making necessary changes for ship_rec_cal
1916                            /* ship_rec_cal changes begin */
1917                            IF PG_DEBUG in ('Y', 'C') THEN
1918                               msc_sch_wb.atp_debug('**************** Calendar Codes ******************************');
1919                               msc_sch_wb.atp_debug('*  ___________________Input_____________________');
1920                               msc_sch_wb.atp_debug('*  ');
1921                               msc_sch_wb.atp_debug('*  Source Type             : '|| l_sources.source_type(i) );
1922                               msc_sch_wb.atp_debug('*  Instance ID             : '|| p_atp_record.instance_id );
1923                               msc_sch_wb.atp_debug('*  Source Instance ID      : '|| l_sources.instance_id(i) );
1924                               msc_sch_wb.atp_debug('*  Source Org ID           : '|| l_sources.organization_id(i) );
1925                               msc_sch_wb.atp_debug('*  Receiving Org ID        : '|| org_availability_info.organization_id(l_parent_org_cntr) );
1926                               msc_sch_wb.atp_debug('*  Ship Method             : '|| l_sources.Ship_Method(i) );
1927                            END IF;
1928 
1929                            IF l_sources.source_type(i) = MSC_ATP_PVT.TRANSFER THEN
1930                                -- receiving party is org
1931                                l_receiving_cal_code := MSC_CALENDAR.Get_Calendar_Code(
1932                                                                     p_atp_record.instance_id,
1933                                                                     null,
1934                                                                     null,
1935                                                                     null,
1936                                                                     null,
1937                                                                     null,
1938                                                                     org_availability_info.organization_id(l_parent_org_cntr),
1939                                                                     l_sources.Ship_Method(i),
1940                                                                     MSC_CALENDAR.ORC);
1941 
1942                                l_intransit_cal_code := MSC_CALENDAR.Get_Calendar_Code(
1943                                                                     l_sources.instance_id(i),
1944                                                                     null,
1945                                                                     null,
1946                                                                     null,
1947                                                                     null,
1948                                                                     4,
1949                                                                     null,
1950                                                                     l_sources.Ship_Method(i),
1951                                                                     MSC_CALENDAR.VIC);
1952 
1953                                l_shipping_cal_code := MSC_CALENDAR.Get_Calendar_Code(
1954                                                                     l_sources.instance_id(i),
1955                                                                     null,
1956                                                                     null,
1957                                                                     null,
1958                                                                     null,
1959                                                                     null,
1960                                                                     l_sources.organization_id(i),
1961                                                                     l_sources.Ship_Method(i),
1962                                                                     MSC_CALENDAR.OSC);
1963                                l_manufacturing_cal_code := MSC_CALENDAR.Get_Calendar_Code(
1964                                                                     l_sources.instance_id(i),
1965                                                                     null,
1966                                                                     null,
1967                                                                     null,
1968                                                                     null,
1969                                                                     null,
1970                                                                     l_sources.organization_id(i),
1971                                                                     null,
1972                                                                     MSC_CALENDAR.OMC);
1973                                l_dest_mfg_cal_code := MSC_CALENDAR.Get_Calendar_Code(
1974                                                                     l_sources.instance_id(i),
1975                                                                     null,
1976                                                                     null,
1977                                                                     null,
1978                                                                     null,
1979                                                                     null,
1980                                                                     org_availability_info.organization_id(l_parent_org_cntr),
1981                                                                     null,
1982                                                                     MSC_CALENDAR.OMC);
1983                            END IF;
1984                            IF PG_DEBUG in ('Y', 'C') THEN
1985                               msc_sch_wb.atp_debug('  ');
1986                               msc_sch_wb.atp_debug('*  ___________________Output____________________');
1987                               msc_sch_wb.atp_debug('*  ');
1988                               msc_sch_wb.atp_debug('*  Receiving calendar code         : ' || l_receiving_cal_code);
1989                               msc_sch_wb.atp_debug('*  Intransit calendar code         : ' || l_intransit_cal_code);
1990                               msc_sch_wb.atp_debug('*  Shipping calendar code          : ' || l_shipping_cal_code);
1991                               msc_sch_wb.atp_debug('*  Manufacturing calendar code     : ' || l_manufacturing_cal_code);
1992                               msc_sch_wb.atp_debug('**************************************************************');
1993                            END IF;
1994 
1995                            /* planned order due date as per OMC-D */
1996                            l_planned_order_date := MSC_CALENDAR.PREV_WORK_DAY(
1997                                                        l_dest_mfg_cal_code,
1998                                                        p_atp_record.instance_id,
1999                                                        l_requested_ship_date);
2000                            IF PG_DEBUG in ('Y', 'C') THEN
2001                               msc_sch_wb.atp_debug('*  Planned Order Due Date as per OMC-D: ' || l_planned_order_date);
2002                               msc_sch_wb.atp_debug('*  post_pro_lt: ' || org_availability_info.post_pro_lt(l_parent_org_cntr));
2003                            END IF;
2004                            -- ship_rec_cal changes end
2005 
2006                            --- first we do a PTF check. Get start Date for PTF check
2007                            --- request date in source org = dock date in dest org - intransit lead time
2008 			   -- dsting delivery lead time to not be dependent on calendar (see 2463556)
2009                            l_start_date := org_availability_info.Requested_ship_date(l_parent_org_cntr);
2010 
2011                            /* ship_rec_cal changes begin */
2012                            IF org_availability_info.post_pro_lt(l_parent_org_cntr) > 0 THEN
2013                               /* ship_rec_cal
2014                               l_start_date := MSC_CALENDAR.DATE_OFFSET(
2015                                                          org_availability_info.organization_id(l_parent_org_cntr),
2016                                                          p_atp_record.instance_id,
2017                                                          1,
2018                                                          l_start_date,
2019                                                          -1 * org_availability_info.post_pro_lt(l_parent_org_cntr));*/
2020                               l_start_date := MSC_CALENDAR.DATE_OFFSET(
2021                                                          l_dest_mfg_cal_code,
2022                                                          p_atp_record.instance_id,
2023                                                          l_start_date,
2024                                                          -1 * org_availability_info.post_pro_lt(l_parent_org_cntr), -1);
2025 
2026 			   END IF;
2027 
2028                            l_start_date := MSC_CALENDAR.PREV_WORK_DAY(
2029                                                 l_receiving_cal_code,
2030                                                 p_atp_record.instance_id,
2031                                                 l_start_date);
2032 
2033                            /* populating new_dock_date even for transfer orders also, this is to support requirement of supporting
2034                               org modeled as supplier which might come in future*/
2035                            l_new_dock_date := l_start_date;
2036 
2037                            IF PG_DEBUG in ('Y', 'C') THEN
2038                               msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_start_date : '||l_start_date);
2039                               msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Date after validating on ORC : '||l_new_dock_date);
2040                            END IF;
2041 
2042                            -- l_start_date := MSC_CALENDAR.DATE_OFFSET ( -- Bug 3241766: l_start_date should have dock date
2043                            l_atp_rec.requested_ship_date := MSC_CALENDAR.DATE_OFFSET (
2044                                                 l_intransit_cal_code,
2045                                                 p_atp_record.instance_id,
2046                                                 l_start_date,
2047                                                 -1 * NVL(l_sources.lead_time(i), 0), -1);
2048 
2049                            IF PG_DEBUG in ('Y', 'C') THEN
2050                               msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Date after subtracting DLT using VIC : '||l_atp_rec.requested_ship_date);
2051                               msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_sources.lead_time(i) : '||l_sources.lead_time(i));
2052                            END IF;
2053 
2054                            l_atp_rec.requested_ship_date := MSC_CALENDAR.PREV_WORK_DAY(
2055                                                 l_shipping_cal_code,
2056                                                 p_atp_record.instance_id,
2057                                                 l_atp_rec.requested_ship_date);
2058                            /* populating new_ship_date for transfer orders also, this is to support requirement of supporting
2059                               org modeled as supplier which might come in future*/
2060                            l_new_ship_date := l_atp_rec.requested_ship_date;
2061                            l_req_ship_date := l_atp_rec.requested_ship_date;
2062                            l_start_date := l_new_ship_date; -- Bug 3578083 -- Setting the variable that is compared with PTF date
2063                                                             -- This should be source org's ship date.
2064 
2065                            IF PG_DEBUG in ('Y', 'C') THEN
2066                               msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Date after validating on OSC : '||l_atp_rec.requested_ship_date);
2067                               msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'pre_pro_lt : '||org_availability_info.pre_pro_lt(l_parent_org_cntr));
2068                            END IF;
2069 
2070                            -- Add pre-PLT offset also: Bug 3241766
2071                            l_order_date := MSC_CALENDAR.DATE_OFFSET (
2072                                                          l_dest_mfg_cal_code,
2073                                                          p_atp_record.instance_id,
2074                                                          l_new_ship_date,
2075                                                          -1 * org_availability_info.pre_pro_lt(l_parent_org_cntr), -1);
2076 
2077                            --- get PTF date
2078                            IF l_parent_org_cntr = 1 THEN
2079                               l_sysdate := l_sys_date_top_org;
2080                            ELSE
2081                               /* ship_rec_cal
2082                               l_sysdate := MSC_CALENDAR.NEXT_WORK_DAY(
2083                                                     org_availability_info.organization_id(l_parent_org_cntr),
2084                                                     p_atp_record.instance_id,
2085                                                     1,
2086                                                     sysdate);*/
2087                               l_sysdate := MSC_CALENDAR.NEXT_WORK_DAY(
2088                                                     l_dest_mfg_cal_code,
2089                                                     p_atp_record.instance_id,
2090                                                     l_trunc_sysdate); --bug3578083 Removed unnecessary reference to sysdate
2091                            END IF;
2092                            l_ptf_date := l_sysdate;
2093                            -- bug3578083 - PTF constraint should be added only if plan is PTF enabled
2094                            l_ptf_enabled := 2;
2095                            BEGIN
2096                               Select DECODE(pl.planning_time_fence_flag,
2097                                 1, trunc(NVL(itm.planning_time_fence_date, l_sysdate)),
2098                                 l_sysdate),pl.planning_time_fence_flag -- Bug 3578083
2099                               into   l_ptf_date,l_ptf_enabled  -- Bug 3578083
2100                               from   msc_system_items itm,
2101                                      msc_plans pl
2102                               where  itm.plan_id = org_availability_info.plan_id(l_parent_org_cntr)
2103                               and    itm.sr_instance_id = p_atp_record.instance_id
2104                               and    itm.organization_id = org_availability_info.organization_id(l_parent_org_cntr)
2105                               and    itm.sr_inventory_item_id = item_availability_info.sr_inventory_item_id(l_item_cntr)
2106                               and    pl.plan_id = itm.plan_id
2107                               and    pl.sr_instance_id = itm.sr_instance_id;
2108 
2109                            EXCEPTION
2110                               WHEN OTHERS THEN
2111                                   IF PG_DEBUG in ('Y', 'C') THEN
2112                                      msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Error occured while getting PTF : ' || sqlerrm);
2113                                   END IF;
2114                                   --l_ptf_date := l_sysdate; -- Bug 3578083
2115                            END;
2116                            IF PG_DEBUG in ('Y', 'C') THEN
2117                               msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_ptf_date := ' || l_ptf_date);
2118                               msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_order_date := ' || l_order_date);
2119                               msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_start_date := ' || l_start_date);
2120                               msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_ptf_enabled := ' || l_ptf_enabled);
2121                            END IF;
2122 
2123                            -- calculate the ship date if we pass the ptf check
2124 			   -- or we're doing diagnostic atp
2125 			   /*   ship_rec_cal: not required as ship date is already calulated
2126                            IF l_start_date >= l_ptf_date THEN
2127                               IF PG_DEBUG in ('Y', 'C') THEN
2128                                  msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Passed the PTF test');
2129                               END IF;
2130 			      /* ship_rec_cal
2131 			      l_req_ship_date := MSC_CALENDAR.prev_work_day(l_sources.organization_id(i),
2132 									p_atp_record.instance_id,
2133 									MSC_CALENDAR.TYPE_DAILY_BUCKET,
2134 									l_start_date);
2135 			      l_req_ship_date := MSC_CALENDAR.prev_work_day(
2136 		                                                l_manufacturing_cal_code,
2137 								p_atp_record.instance_id,
2138 									l_start_date);
2139 
2140                               IF PG_DEBUG in ('Y', 'C') THEN
2141                                  msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_req_ship_date := ' || l_req_ship_date);
2142                               END IF;
2143                            END IF;
2144                            */
2145 
2146 			   -- add the org to the org_availability queue if it doesn't violate
2147 			   -- ptf date or lead time constraints
2148 			   -- Bug 3241766: order start date should be checked with sysdate
2149                            --bug3578083  removed unnecessary reference to sysdate
2150                            IF l_order_date >= l_trunc_sysdate AND l_start_date >= l_ptf_date THEN
2151                                  --- since we have found the sources we set the flag to yes
2152                                  l_sources_found := 1;
2153                                  l_transfer_found := 1;
2154                                  ---extend the org_avail-indo
2155                                  MSC_ATP_SUBST.Extend_Org_Avail_Info_Rec(org_availability_info, l_return_status);
2156                                  l_count := org_availability_info.organization_id.count;
2157                                  org_availability_info.organization_id(l_count) := l_sources.organization_id(i);
2158                                  org_availability_info.requested_ship_date(l_count) := l_req_ship_date;
2159                                  org_availability_info.parent_org_idx(l_count) := l_parent_org_cntr;
2160                                  org_availability_info.location_id(l_count) := l_from_location_id;
2161                                  org_availability_info.lead_time(l_count) := l_delivery_lead_time;
2162 			   	 org_availability_info.ship_method(l_count) := l_sources.ship_method(i);
2163 --			   	 org_availability_info.ship_method(l_count) := l_ship_method;
2164 
2165 			         org_availability_info.new_dock_date(l_count)   := l_new_dock_date;     -- Bug 3241766
2166 			         org_availability_info.new_ship_date(l_count)   := l_new_ship_date;     -- Bug 3241766
2167 			         org_availability_info.new_start_date(l_count)  := l_new_ship_date;     -- Bug 3241766
2168 			         org_availability_info.new_order_date(l_count)  := l_order_date;        -- Bug 3241766
2169 
2170 			         -- Bug 3371817 - assigning calendars
2171 			         org_availability_info.shipping_cal_code(l_count)   := l_shipping_cal_code;
2172 			         org_availability_info.receiving_cal_code(l_count)  := l_receiving_cal_code;
2173 			         org_availability_info.intransit_cal_code(l_count)  := l_intransit_cal_code;
2174                                  org_availability_info.manufacturing_cal_code(l_count)  := l_manufacturing_cal_code;
2175 			         -- dsting diagnostic atp
2176 			         org_availability_info.ptf_date(l_count) := l_ptf_date;
2177 
2178                                  --- get pegging ID for planned order in parent Org
2179                                  SELECT msc_full_pegging_s.nextval
2180                                  INTO   l_pegging_id
2181                                  FROM   dual;
2182                                  IF PG_DEBUG in ('Y', 'C') THEN
2183                                     msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'PO Pegging id := ' || l_pegging_id);
2184                                     msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Src Organization_id := ' || l_sources.organization_id(i));
2185                                  END IF;
2186 		                 org_availability_info.PO_pegging_id(l_count) := l_pegging_id;
2187 			   ELSIF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = DIAGNOSTIC_ATP THEN
2188                                 l_transfer_found := 1;
2189 
2190 				Prep_Common_pegging_rec(
2191 					l_pegging_rec,
2192 					p_atp_record,
2193 					org_availability_info,
2194 					l_process_org_cntr,
2195 					item_availability_info,
2196 					l_item_cntr
2197 				);
2198 
2199 				/* ship_rec_cal changes begin
2200 				l_lt_due_date := MSC_CALENDAR.next_work_day(l_sources.organization_id(i),
2201                                                                           p_atp_record.instance_id,
2202 									  MSC_CALENDAR.TYPE_DAILY_BUCKET,
2203                                                                           sysdate) +
2204 						 + NVL(l_sources.lead_time(i), 0);
2205 
2206 				l_lt_due_date := MSC_CALENDAR.next_work_day(l_sources.organization_id(i),
2207                                                                           p_atp_record.instance_id,
2208 									  MSC_CALENDAR.TYPE_DAILY_BUCKET,
2209 									  l_lt_due_date);
2210 
2211 				l_ptf_due_date := MSC_CALENDAR.next_work_day(l_sources.organization_id(i),
2212                                                                           p_atp_record.instance_id,
2213 									  MSC_CALENDAR.TYPE_DAILY_BUCKET,
2214 									  l_ptf_date + NVL(l_sources.lead_time(i), 0));
2215 				IF NVL(org_availability_info.post_pro_lt(l_parent_org_cntr),0) > 0 THEN
2216 					l_lt_due_date :=  MSC_CALENDAR.DATE_OFFSET (
2217                                                        org_availability_info.organization_id(l_parent_org_cntr),
2218                                                        p_atp_record.instance_id,
2219                                                        1,
2220                                                        l_lt_due_date,
2221                                                        org_availability_info.post_pro_lt(l_parent_org_cntr));
2222 
2223 					l_ptf_due_date :=  MSC_CALENDAR.DATE_OFFSET (
2224                                                        org_availability_info.organization_id(l_parent_org_cntr),
2225                                                        p_atp_record.instance_id,
2226                                                        1,
2227                                                        l_ptf_due_date,
2228 									  l_ptf_date + NVL(l_sources.lead_time(i), 0));*/
2229 
2230                                 -- Add pre-PLT offset also: Bug 3241766
2231                                 l_lt_due_date := MSC_CALENDAR.DATE_OFFSET (
2232                                                          l_dest_mfg_cal_code,
2233                                                          p_atp_record.instance_id,
2234                                                          sysdate,
2235                                                          org_availability_info.pre_pro_lt(l_parent_org_cntr), 1);
2236 
2237                                 l_lt_due_date := MSC_CALENDAR.THREE_STEP_CAL_OFFSET_DATE(
2238                                 		l_lt_due_date, l_shipping_cal_code, +1,
2239                                 		l_intransit_cal_code, NVL(l_sources.lead_time(i), 0), +1,
2240                                 		l_receiving_cal_code, +1, p_atp_record.instance_id);
2241 
2242                                 -- Add pre-PLT offset also: Bug 3241766
2243                                 /* Bug 3578083 - Earlier ptf_due_date was calculated as
2244                                    ptf_due_date := ptf_date + pre-processing lead time + delivery lead time + post-processing lead time
2245                                    it should actually be
2246                                    ptf_due_date := ptf_date + delivery lead time + post-processing lead time
2247                                 l_ptf_due_date := MSC_CALENDAR.DATE_OFFSET (
2248                                                          l_dest_mfg_cal_code,
2249                                                          p_atp_record.instance_id,
2250                                                          l_ptf_date,
2251                                                          org_availability_info.pre_pro_lt(l_parent_org_cntr), 1);*/
2252 
2253                                 l_ptf_due_date := MSC_CALENDAR.THREE_STEP_CAL_OFFSET_DATE(
2254                                 		l_ptf_date, l_shipping_cal_code, +1,
2255                                 		l_intransit_cal_code, NVL(l_sources.lead_time(i), 0), +1,
2256                                 		l_receiving_cal_code, +1, p_atp_record.instance_id);
2257 
2258 				IF NVL(org_availability_info.post_pro_lt(l_parent_org_cntr),0) > 0 THEN
2259 					/* ship_rec_cal
2260 					l_lt_due_date :=  MSC_CALENDAR.DATE_OFFSET (
2261                                                        org_availability_info.organization_id(l_parent_org_cntr),
2262                                                        p_atp_record.instance_id,
2263                                                        1,
2264                                                        l_lt_due_date,
2265                                                        org_availability_info.post_pro_lt(l_parent_org_cntr));
2266 
2267 					l_ptf_due_date :=  MSC_CALENDAR.DATE_OFFSET (
2268                                                        org_availability_info.organization_id(l_parent_org_cntr),
2269                                                        p_atp_record.instance_id,
2270                                                        1,
2271                                                        l_ptf_due_date,
2272                                                        org_availability_info.post_pro_lt(l_parent_org_cntr));*/
2273 
2274 					l_lt_due_date :=  MSC_CALENDAR.DATE_OFFSET (
2275                                                        l_dest_mfg_cal_code,
2276                                                        p_atp_record.instance_id,
2277                                                        l_lt_due_date,
2278                                                        org_availability_info.post_pro_lt(l_parent_org_cntr), 1);
2279 					l_ptf_due_date :=  MSC_CALENDAR.DATE_OFFSET (
2280                                                        l_dest_mfg_cal_code,
2281                                                        p_atp_record.instance_id,
2282                                                        l_ptf_due_date,
2283                                                        org_availability_info.post_pro_lt(l_parent_org_cntr), 1);
2284 
2285                                 -- ship_rec_cal changes end
2286 				END IF;
2287 			   	-- bug3578083 - Constraint message should be added only if constraint actually exists
2288 			   	IF PG_DEBUG in ('Y', 'C') THEN
2289 				   msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_ptf_due_date ' || l_ptf_due_date);
2290 				   msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_lt_due_date ' || l_lt_due_date);
2291 				END IF;
2292 			   	IF l_lt_due_date > l_requested_ship_date OR
2293                                  l_ptf_due_date > l_requested_ship_date THEN
2294                                  -- bug3578083 - PTF constraint should be added only if plan is PTF enabled
2295 			   	 IF l_ptf_enabled=2 OR l_lt_due_date > l_ptf_due_date THEN
2296 					IF PG_DEBUG in ('Y', 'C') THEN
2297 					   msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'diagnostic atp: TRANSIT_LT_CONSTRAINT');
2298 					END IF;
2299 					l_pegging_rec.constraint_type := TRANSIT_LT_CONSTRAINT;
2300 					l_pegging_rec.constraint_date := l_lt_due_date;
2301 				 ELSE
2302 					IF PG_DEBUG in ('Y', 'C') THEN
2303 					   msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'diagnostic atp: PTF_CONSTRAINT');
2304 					END IF;
2305 					l_pegging_rec.constraint_type := PTF_CONSTRAINT;
2306 					l_pegging_rec.constraint_date := l_ptf_due_date;
2307 				 END IF;
2308 				END IF;
2309 
2310 				IF PG_DEBUG in ('Y', 'C') THEN
2311 				   msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Constraint due date: ' || l_pegging_rec.constraint_date);
2312 				END IF;
2313 
2314 				l_pegging_rec.parent_pegging_id:=
2315 					org_availability_info.demand_pegging_id(l_parent_org_cntr);
2316 				l_pegging_rec.end_pegging_id := org_availability_info.demand_pegging_id(1);
2317 
2318 				-- dsting diag_atp
2319 				IF PG_DEBUG in ('Y', 'C') THEN
2320 				   msc_sch_wb.atp_debug('lt or ptf constraint end_pegging_id: ' || org_availability_info.demand_pegging_id(1));
2321 				END IF;
2322 
2323 				MSC_ATP_PVT.G_demand_pegging_id := org_availability_info.demand_pegging_id(1);
2324 				l_pegging_rec.organization_id :=
2325 					org_availability_info.organization_id(l_parent_org_cntr);
2326 				l_pegging_rec.organization_code := MSC_ATP_FUNC.get_org_code(
2327 									p_atp_record.instance_id,
2328                                         				l_sources.organization_id(i));
2329 
2330 				l_pegging_rec.Postprocessing_lead_time := org_availability_info.post_pro_lt(l_parent_org_cntr);
2331 				l_pegging_rec.Intransit_lead_time := l_delivery_lead_time;
2332 				IF( NVL(l_sources.ship_method(i), '@@@') <> '@@@' ) THEN
2333 					l_pegging_rec.ship_method := l_sources.ship_method(i);
2334 				END IF;
2335 				l_pegging_rec.ptf_date := l_ptf_date;
2336 
2337 				l_pegging_rec.plan_name := org_availability_info.plan_name(l_parent_org_cntr);
2338 				l_pegging_rec.rounding_control := item_availability_info.rounding_control_type(l_item_cntr);
2339 				l_pegging_rec.required_quantity := l_net_demand;
2340 				l_pegging_rec.required_date := l_req_ship_date;
2341 
2342 				l_pegging_rec.identifier2:= org_availability_info.plan_id(l_parent_org_cntr);
2343 				l_pegging_rec.identifier3:= l_transaction_id;
2344 
2345 				l_pegging_rec.supply_demand_quantity := 0;
2346 				l_pegging_rec.supply_demand_type := 2; -- supply
2347 				l_pegging_rec.supply_demand_date:= org_availability_info.requested_ship_date(l_parent_org_cntr);
2348 				l_pegging_rec.source_type :=  MSC_ATP_PVT.TRANSFER;
2349 
2350 				l_pegging_rec.atp_level := p_level+1;
2351 				l_pegging_rec.scenario_id := p_scenario_id;
2352 				l_pegging_rec.pegging_type := TRANSFER_PEG_TYP;
2353 				l_pegging_rec.aggregate_time_fence_date:= org_availability_info.Atf_Date(l_parent_org_cntr); --bug3467631
2354                                 -- Bug 3826234
2355                                 l_pegging_rec.shipping_cal_code      :=  l_shipping_cal_code;
2356                                 l_pegging_rec.receiving_cal_code     :=  l_receiving_cal_code;
2357                                 l_pegging_rec.intransit_cal_code     :=  l_intransit_cal_code;
2358                                 l_pegging_rec.manufacturing_cal_code :=  l_manufacturing_cal_code;
2359 
2360 				MSC_ATP_DB_UTILS.Add_Pegging(l_pegging_rec, l_pegging_id);
2361                            ELSE
2362                               --- we fail the PTF date test
2363                               IF PG_DEBUG in ('Y', 'C') THEN
2364                                  msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Fail the PTF test, dont add org to list of org to be visited');
2365                               END IF;
2366                            END IF;
2367 
2368                         END IF; -- IF l_sources.source_type = 'TRANSFER' THEN
2369                         IF PG_DEBUG in ('Y', 'C') THEN
2370                            msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_parent_org_cntr := ' || l_parent_org_cntr);
2371                         END IF;
2372                      END LOOP;
2373                   END IF;
2374 
2375 		  IF PG_DEBUG in ('Y', 'C') THEN
2376                         msc_sch_wb.atp_debug('highest rev: ' || l_highest_rev_item_id);
2377                         msc_sch_wb.atp_debug('l_sources_found: ' || l_sources_found);
2378                         msc_sch_wb.atp_debug('l_transfer_found: ' || l_transfer_found);
2379 			msc_sch_wb.atp_debug('G_CREATE_SUPPLY: ' || MSC_ATP_SUBST.G_CREATE_SUPPLY_FLAG);
2380 			msc_sch_wb.atp_debug('item create_supply: ' || item_availability_info.create_supply_flag(l_item_count));
2381 			msc_sch_wb.atp_debug('org: ' || org_availability_info.org_code(l_parent_org_cntr));
2382 		  END IF;
2383 
2384 		  -- if this is diagnostic atp, and there are no transfer sources, and we cannot create
2385 		  -- a supply on the demanded item then flag it as a material constraint.
2386                   IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = DIAGNOSTIC_ATP AND l_transfer_found <> 1 AND
2387 		     l_create_supply_on_orig_item = 0
2388                   THEN
2389 			org_availability_info.constraint_type(l_parent_org_cntr) := MATERIAL_CONSTRAINT;
2390                   END IF;
2391 
2392                   l_parent_org_cntr := l_parent_org_cntr + 1;
2393 
2394                END LOOP; --- WHILE l_sources_found = 0 AND l_parent_org_cntr <= org_availability_info.organization_id.count LOOP
2395             ELSIF l_requested_ship_date  < l_sys_date_top_org THEN
2396                l_parent_org_cntr := l_parent_org_cntr + 1;
2397 
2398             END IF; --IF l_net_demand > 0 and l_process_org_cntr >= org_availability_info.org_count THEN
2399 
2400             l_process_org_cntr := l_process_org_cntr + 1;
2401 
2402             IF l_process_org_cntr <= org_availability_info.organization_id.count THEN
2403                l_parent_index := org_availability_info.parent_org_idx(l_process_org_cntr);
2404             END IF;
2405 
2406          END LOOP; --- WHILE l_parent_org_cntr <= org_availability_info.organization_id.count LOOP
2407 
2408 
2409          --- AT this point we should have complete picture as to what is the demand availability in each org
2410          --- So we create planned orders and peggings in each org
2411          IF PG_DEBUG in ('Y', 'C') THEN
2412             msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Done with ATP check, now add pegging, planned orders');
2413 	    msc_sch_wb.atp_debug('reset Subst last PO pegging: ');
2414          END IF;
2415 
2416 	 MSC_ATP_SUBST.G_TOP_LAST_PO_PEGGING := null;
2417 	 MSC_ATP_SUBST.G_TOP_LAST_PO_QTY := 0;
2418 
2419 	 -- dsting diag_atp
2420 	 -- I need to know the quantity from children in advance to balance supply/demand
2421 /* 2754446 I'm doing this in advance already
2422 	 IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = DIAGNOSTIC_ATP THEN
2423 		FOR j in reverse 2..org_availability_info.organization_id.count  LOOP
2424 		    l_parent_index := org_availability_info.parent_org_idx(j);
2425                     -- 2754446
2426                     l_addt_qty := LEAST((GREATEST(org_availability_info.request_date_quantity(j), 0) +
2427                                             NVL(org_availability_info.quantity_from_children(j), 0) +
2428                                             NVL(org_availability_info.steal_qty(j), 0)
2429                                        ), org_availability_info.demand_quantity(j))
2430                                   / org_availability_info.conversion_rate(j);
2431 
2432                     -- dsting 2754446
2433                     IF nvl(org_availability_info.rounding_flag(l_parent_index), 2) = 1 THEN
2434                           l_addt_qty := FLOOR(ROUND(l_addt_qty, 10));
2435                     END IF;
2436 
2437                     msc_sch_wb.atp_debug('l_addt_qty: ' || l_addt_qty);
2438 
2439                     org_availability_info.quantity_from_children(l_parent_index) :=
2440                                      NVL(org_availability_info.quantity_from_children(l_parent_index), 0) +
2441                                      l_addt_qty;
2442 
2443                     msc_sch_wb.atp_debug('qty from children('||l_parent_index||'): ' ||
2444                         org_availability_info.quantity_from_children(l_parent_index));
2445 		END LOOP;
2446 --	 END IF;
2447 */
2448 
2449          FOR j in reverse 1..org_availability_info.organization_id.count  LOOP
2450             IF PG_DEBUG in ('Y', 'C') THEN
2451                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Process orgination := ' || org_availability_info.organization_id(j));
2452             END IF;
2453             -- dsting 2754446
2454             l_parent_index := nvl(org_availability_info.parent_org_idx(j), 1);
2455 
2456             IF PG_DEBUG in ('Y', 'C') THEN
2457                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_parent_index := ' || l_parent_index);
2458             END IF;
2459 
2460             IF (org_availability_info.atp_flag(j) = 'N' AND org_availability_info.atp_comp_flag(j) = 'N')  THEN
2461 
2462                --- NON-ATPABLE ITEM item is non atpable then we add planned order in parent org
2463                IF PG_DEBUG in ('Y', 'C') THEN
2464                   msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Item is Non Atpable');
2465                END IF;
2466 
2467                -- dsting 2754446 req_date_qty and qty_from_chld should already be rounded to correct values
2468                -- for creating a PO in the top org's uom. so convert the uom, and round to remove float precision errs
2469 
2470                IF nvl(org_availability_info.rounding_flag(l_parent_index), 2) = 1 THEN
2471                   l_available_quantity := FLOOR(ROUND(
2472                                             (greatest(org_availability_info.request_date_quantity(j), 0)
2473                                              + NVL(org_availability_info.quantity_from_children(j), 0)
2474                                              + NVL(org_availability_info.steal_qty(j), 0)
2475                                             ) / org_availability_info.conversion_rate(j), 10)
2476                                           );
2477                ELSE
2478                   l_available_quantity := (greatest(org_availability_info.request_date_quantity(j), 0)
2479                                            + NVL(org_availability_info.quantity_from_children(j), 0)
2480                                            + NVL(org_availability_info.steal_qty(j), 0))
2481                                           / org_availability_info.conversion_rate(j);
2482                END IF;
2483 
2484                IF PG_DEBUG in ('Y', 'C') THEN
2485                   msc_sch_wb.atp_debug('rounding_flag: ' || org_availability_info.rounding_flag(j));
2486                   msc_sch_wb.atp_debug('l_available_quantity: ' || l_available_quantity);
2487                   msc_sch_wb.atp_debug('l_avail_qty_top_uom: ' || l_avail_qty_top_uom);
2488                END IF;
2489 
2490                IF j = 1 THEN
2491                   --- bug 2344501: set the error only if it is a top org
2492                   p_atp_record.error_code := MSC_ATP_PVT.ATP_NOT_APPL;
2493                END IF;
2494 
2495 	       l_transaction_id := NULL;
2496                IF j > 1 AND (l_available_quantity > 0 OR MSC_ATP_PVT.G_DIAGNOSTIC_ATP = DIAGNOSTIC_ATP) THEN
2497 
2498 		  -- dsting diag_atp
2499 		  -- may need to adjust the PO for diagnostic atp
2500 --		  IF l_available_quantity >0 THEN
2501 
2502                     -- dsting 2754446
2503                     l_PO_qty := org_availability_info.demand_quantity(j)
2504                                 / org_availability_info.conversion_rate(j);
2505                     IF org_availability_info.rounding_flag(l_parent_index) = 1 THEN
2506                        l_PO_qty := CEIL(ROUND(l_PO_qty,10));
2507                     END IF;
2508 
2509                     -- Begin ATP4drp Create Planned Arrivals for DRP plans
2510                     IF (NVL(MSC_ATP_PVT.G_PLAN_INFO_REC.plan_type,1) = 5) THEN
2511                        l_supply_rec.instance_id        :=   p_atp_record.instance_id;
2512                        l_supply_rec.plan_id            :=   org_availability_info.plan_id(l_parent_index);
2513                        l_supply_rec.inventory_item_id  :=  org_availability_info.family_dest_id(l_parent_index);
2514                                                                         -- DRP Plan re-set to member For time_phased_atp
2515                        l_supply_rec.organization_id    :=   org_availability_info.organization_id(l_parent_index);
2516                        l_supply_rec.schedule_date      :=   org_availability_info.requested_ship_date(l_parent_index);
2517                        l_supply_rec.order_quantity     :=   least(l_PO_qty, l_available_quantity);
2518                           -- null; -- l_atp_rec.supplier_id,
2519                           -- null; -- l_atp_rec.supplier_site_id,
2520                        l_supply_rec.demand_class       :=   org_availability_info.demand_class(l_parent_index);
2521                           -- rajjain 02/19/2003 Bug 2788302 Begin
2522                           -- Add Sourcing details
2523                        l_supply_rec.source_organization_id :=   org_availability_info.organization_id(j);
2524                        l_supply_rec.source_sr_instance_id  :=   p_atp_record.instance_id;
2525                           -- null; --process seq id (transfer case)
2526                           -- rajjain 02/19/2003 Bug 2788302 End
2527                        l_supply_rec.refresh_number     :=   p_refresh_number; -- for summary enhancement
2528                           -- ship_rec_cal changes begin
2529                        l_supply_rec.shipping_cal_code  := org_availability_info.shipping_cal_code(j);   -- |
2530                        l_supply_rec.receiving_cal_code := org_availability_info.receiving_cal_code(j);  -- |
2531                        l_supply_rec.intransit_cal_code := org_availability_info.intransit_cal_code(j);  -- |
2532                        l_supply_rec.new_ship_date      := org_availability_info.new_ship_date(j);       -- |  Bug 3241766
2533                        l_supply_rec.new_dock_date      := org_availability_info.new_dock_date(j);       -- |
2534                                                                        -- |  Add new columns start date and order date
2535                        l_supply_rec.start_date         := org_availability_info.new_start_date(j);      -- |
2536                                                                        -- Use values from child org rather than parent
2537                        l_supply_rec.order_date         := org_availability_info.new_order_date(j);      -- |
2538                        l_supply_rec.ship_method        :=  org_availability_info.ship_method(j);         -- |
2539                           -- ship_rec_cal changes end
2540                           -- item_availability_info.inventory_item_id(l_item_cntr); -- For time_phased_atp
2541                           -- org_availability_info.atf_date(l_parent_index);        -- For time_phased_atp
2542 
2543                        l_supply_rec.firm_planned_type := 2;
2544                        l_supply_rec.disposition_status_type := 1;
2545                        l_supply_rec.record_source := 2; -- ATP created
2546                        l_supply_rec.supply_type := 51; --- planned arrival
2547                        l_supply_rec.intransit_lead_time := org_availability_info.lead_time(j); --4127630
2548 
2549                        MSC_ATP_DB_UTILS.ADD_Supplies(l_supply_rec);
2550                        -- Asssign the output to local variables.
2551                        l_transaction_id := l_supply_rec.transaction_id;
2552                        l_return_status  := l_supply_rec.return_status;
2553                        IF PG_DEBUG in ('Y', 'C') THEN
2554                           msc_sch_wb.atp_debug('----- ATP4drp Specific Debug Messages -----');
2555                           msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'DRP Plan Add Planned Inbound');
2556                           msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Supply Id l_transaction_id: ' || l_transaction_id);
2557                           msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Supply Type l_supply_rec.supply_type: '
2558                                                                                  || l_supply_rec.supply_type);
2559                           msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Return Status l_return_status: ' || l_return_status);
2560                           msc_sch_wb.atp_debug('----- ATP4drp Specific Debug Messages -----');
2561                        END IF;
2562 
2563                     ELSE  -- Create Planned Order otherwise.
2564                        IF PG_DEBUG in ('Y', 'C') THEN
2565                             msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Add planned order');
2566                        END IF;
2567 
2568                        MSC_ATP_DB_UTILS.Add_Planned_Order (
2569                           p_atp_record.instance_id,
2570                           org_availability_info.plan_id(l_parent_index),
2571                           org_availability_info.family_dest_id(l_parent_index),  -- For time_phased_atp
2572                           org_availability_info.organization_id(l_parent_index),
2573                           org_availability_info.requested_ship_date(l_parent_index),
2574                           -- 2754446
2575                           --least(org_availability_info.demand_quantity(j), l_available_quantity),
2576                           least(l_PO_qty, l_available_quantity),
2577                           null, -- l_atp_rec.supplier_id,
2578                           null, -- l_atp_rec.supplier_site_id,
2579                           org_availability_info.demand_class(l_parent_index),
2580                           -- rajjain 02/19/2003 Bug 2788302 Begin
2581                           -- Add Sourcing details
2582                           org_availability_info.organization_id(j),
2583                           p_atp_record.instance_id,
2584                           null, --process seq id (transfer case)
2585                           -- rajjain 02/19/2003 Bug 2788302 End
2586                           p_refresh_number, -- for summary enhancement
2587                           -- ship_rec_cal changes begin
2588                           org_availability_info.shipping_cal_code(j),   -- \
2589                           org_availability_info.receiving_cal_code(j),  -- |
2590                           org_availability_info.intransit_cal_code(j),  -- |
2591                           org_availability_info.new_ship_date(j),       -- \  Bug 3241766
2592                           org_availability_info.new_dock_date(j),       -- /  Add new columns start date and order date
2593                           org_availability_info.new_start_date(j),      -- |  Use values from child org rather than parent
2594                           org_availability_info.new_order_date(j),      -- |
2595                           org_availability_info.ship_method(j),         -- /
2596                           -- ship_rec_cal changes end
2597                           l_transaction_id,
2598                           l_return_status,
2599                           org_availability_info.lead_time(j), --4127630
2600                           item_availability_info.inventory_item_id(l_item_cntr), -- For time_phased_atp
2601                           org_availability_info.atf_date(l_parent_index)         -- For time_phased_atp
2602 
2603                        );
2604                     END IF;
2605                     -- End ATP4drp
2606 --		  END IF;
2607 
2608                   --- now add PO pegging
2609                   IF PG_DEBUG in ('Y', 'C') THEN
2610                      msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Add pegging for PO');
2611                      msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_parent_index := ' || l_parent_index);
2612                   END IF;
2613 
2614 		  Prep_PO_Pegging_Rec(l_pegging_rec,
2615 				p_atp_record,
2616 				org_availability_info,
2617 				j,
2618 				item_availability_info,
2619 				l_item_cntr,
2620 				least(l_PO_qty, l_available_quantity),
2621 				l_transaction_id);
2622 
2623                   l_pegging_rec.atp_level:= p_level+1;
2624                   l_pegging_rec.scenario_id:= p_scenario_id;
2625 
2626                   MSC_ATP_SUBST.Add_Pegging(l_pegging_rec);
2627                END IF;
2628             ELSIF NVL(org_availability_info.demand_id(j), -1) > 0 THEN
2629                ---Above condition will filter out al the orgs that we did not visit but added to org_availability_info
2630 		-- then we remove the demand provided its not in the top org
2631                 IF PG_DEBUG in ('Y', 'C') THEN
2632                    msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Current org := ' || org_availability_info.organization_id(j));
2633                   msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'demand id in this org := ' || org_availability_info.demand_id(j));
2634                END IF;
2635 
2636                -- dsting 2754446
2637 /*
2638                l_available_quantity := greatest(org_availability_info.request_date_quantity(j), 0) +
2639                                                 NVL(org_availability_info.quantity_from_children(j), 0) +
2640                                                 NVL(org_availability_info.steal_qty(j), 0);
2641 */
2642                IF nvl(org_availability_info.rounding_flag(l_parent_index), 2) = 1 THEN
2643                   l_available_quantity := FLOOR(ROUND(
2644                                             (greatest(org_availability_info.request_date_quantity(j), 0)
2645                                              + NVL(org_availability_info.quantity_from_children(j), 0)
2646                                              + NVL(org_availability_info.steal_qty(j), 0)
2647                                             ) / org_availability_info.conversion_rate(j), 10)
2648                                           );
2649                ELSE
2650                   l_available_quantity := (greatest(org_availability_info.request_date_quantity(j), 0)
2651                                            + NVL(org_availability_info.quantity_from_children(j), 0)
2652                                            + NVL(org_availability_info.steal_qty(j), 0))
2653                                           * org_availability_info.conversion_rate(l_parent_index);
2654                END IF;
2655 
2656                IF PG_DEBUG in ('Y', 'C') THEN
2657                   msc_sch_wb.atp_debug('l_parent_index: ' || l_parent_index);
2658                   msc_sch_wb.atp_debug('l_avail_qty_top_uom: ' || l_avail_qty_top_uom);
2659                   msc_sch_wb.atp_debug('l_available_quantity: ' || l_available_quantity);
2660                END IF;
2661 
2662 	       -- dsting do not update demands for diagnostic atp
2663 	       IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP <> DIAGNOSTIC_ATP THEN
2664 
2665                   IF j > 1 AND l_available_quantity <= 0
2666 	          THEN
2667                      --- if we do nto get anything from this org then we remove the demand
2668                      IF PG_DEBUG in ('Y', 'C') THEN
2669                         msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Delete Demand');
2670                      END IF;
2671                      MSC_ATP_SUBST.delete_demand_subst(org_availability_info.demand_id(j),
2672                                                     org_availability_info.plan_id(j));
2673                   ELSE
2674 
2675                      --- update demand
2676                      IF PG_DEBUG in ('Y', 'C') THEN
2677                         msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_available_quantity := ' || l_available_quantity);
2678                      END IF;
2679 
2680                      -- 2754446
2681                      -- IF org_availability_info.demand_quantity(j) > l_available_quantity and j > 1
2682                      IF org_availability_info.demand_quantity(j) > l_available_quantity * org_availability_info.conversion_rate(j)
2683                         and j > 1
2684 	   	     THEN
2685                         --- we update demands from J > 1 because we want to retain full demand in top org
2686                         --- as CTP is still left to be done
2687                         IF PG_DEBUG in ('Y', 'C') THEN
2688                            msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Update demand');
2689                         END IF;
2690                         MSC_ATP_SUBST.UPDATE_DEMAND(org_availability_info.demand_id(j),
2691                                                  org_availability_info.plan_id(j),
2692                                                  -- dsting 2754446
2693                                                  l_available_quantity
2694                                                  * org_availability_info.conversion_rate(j));
2695                         /* time_phased_atp */
2696                         --IF l_time_phased_atp = 'Y' THEN --bug3467631
2697                         IF  ((item_availability_info.sr_inventory_item_id(l_item_cntr) <>
2698                                 org_availability_info.family_sr_id(j)) and
2699                                 org_availability_info.atf_date(j) is not null) THEN
2700 		          --using the same insert rec we prepared earlier
2701 		          l_atp_insert_rec.quantity_ordered :=  l_available_quantity * org_availability_info.conversion_rate(j);
2702 		          l_atp_insert_rec.requested_date_quantity := l_available_quantity * org_availability_info.conversion_rate(j);
2703 		          l_atp_insert_rec.atf_date_quantity := org_availability_info.atf_date_quantity(j);
2704 		          MSC_ATP_PF.Increment_Bucketed_Demands_Qty(
2705                                 l_atp_insert_rec,
2706                                 org_availability_info.plan_id(l_process_org_cntr),
2707                                 org_availability_info.demand_id(l_process_org_cntr),
2708                                 l_return_status
2709 		          );
2710                           IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
2711                              IF PG_DEBUG in ('Y', 'C') THEN
2712                                 msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Error occured in procedure Increment_Bucketed_Demands_Qty');
2713                              END IF;
2714                              RAISE FND_API.G_EXC_ERROR;
2715                           END IF;
2716                         END IF;
2717                         -- time_phased_atp changes end
2718 
2719                      END IF;
2720                      IF PG_DEBUG in ('Y', 'C') THEN
2721                         msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'add pegging');
2722                         msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'ATP Flag := ' || org_availability_info.atp_flag(J));
2723                         msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'atp_comp_flag :=' || org_availability_info.atp_comp_flag(j));
2724                         msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_atp_insert_rec.quantity_ordered :=' || l_atp_insert_rec.quantity_ordered);
2725                         msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_atp_insert_rec.requested_date_quantity :=' || l_atp_insert_rec.requested_date_quantity);
2726                         msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_atp_insert_rec.atf_date_quantity :=' || l_atp_insert_rec.atf_date_quantity);
2727                      END IF;
2728 
2729 	          END IF;
2730               END IF; -- dsting diagnostic ATP
2731 
2732               IF PG_DEBUG in ('Y', 'C') THEN
2733                  msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Add pegging after onhand pass');
2734               END IF;
2735 
2736               -- 2754446
2737               l_PO_qty := org_availability_info.demand_quantity(j)
2738                  / org_availability_info.conversion_rate(j);
2739 
2740               IF org_availability_info.rounding_flag(l_parent_index) = 1 THEN
2741                  l_PO_qty := CEIL(ROUND(l_PO_qty,10));
2742               END IF;
2743 
2744               IF PG_DEBUG in ('Y', 'C') THEN
2745                  msc_sch_wb.atp_debug('l_PO_qty: ' || l_PO_qty);
2746                  msc_sch_wb.atp_debug('dmd qty: ' || org_availability_info.demand_quantity(j));
2747                  msc_sch_Wb.atp_debug('conv rate: ' || org_availability_info.conversion_rate(l_parent_index));
2748               END IF;
2749 
2750               IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = DIAGNOSTIC_ATP OR
2751 		 (l_available_quantity > 0 or j =1)
2752 	      THEN
2753                      --- now add demand pegging
2754                      IF PG_DEBUG in ('Y', 'C') THEN
2755                         msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Add demand Pegging');
2756                      END IF;
2757 
2758    		     Prep_Demand_Pegging_rec(l_pegging_rec,
2759 				p_atp_record,
2760 				org_availability_info,
2761 				j,
2762 				item_availability_info,
2763 				l_item_cntr);
2764 
2765                      IF j =1 OR MSC_ATP_PVT.G_DIAGNOSTIC_ATP = DIAGNOSTIC_ATP THEN
2766                         --- if it is top org then we add complete quantity
2767 			-- dsting or if we're doing diagnostic atp
2768                         -- dsting 2754446
2769                         l_pegging_rec.supply_demand_quantity:= org_availability_info.demand_quantity(j);
2770                         IF PG_DEBUG in ('Y', 'C') THEN
2771                            msc_sch_wb.atp_debug('ATP_Check_Subst: ' || ' J1:= ' || j);
2772                            msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'quantity := ' || l_pegging_rec.supply_demand_quantity);
2773                         END IF;
2774                      ELSE
2775                         --- if it is other org then we create demand for least of what we need and what is available
2776                         -- dsting 2754446
2777                         l_pegging_rec.supply_demand_quantity:=
2778                            least(org_availability_info.demand_quantity(j),
2779                                  l_available_quantity * org_availability_info.conversion_rate(j));
2780                         IF PG_DEBUG in ('Y', 'C') THEN
2781                            msc_sch_wb.atp_debug('ATP_Check_Subst: ' || ' J:= ' || j);
2782                            msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'quantity := ' ||
2783                                 l_pegging_rec.supply_demand_quantity);
2784                         END IF;
2785 
2786                      END IF;
2787 
2788                      l_pegging_rec.atp_level:= p_level;
2789                      l_pegging_rec.scenario_id:= p_scenario_id;
2790 
2791                      MSC_ATP_SUBST.Add_Pegging(l_pegging_rec);
2792 
2793                      --- we add supply pegging only for item where atp_flag = 'Y'
2794                      IF org_availability_info.atp_flag(j) in ('Y', 'C') THEN
2795                         --- now add supply pegging
2796                         IF PG_DEBUG in ('Y', 'C') THEN
2797                            msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'add Supply Pegging');
2798                         END IF;
2799 
2800 			Prep_Supply_Pegging_Rec(l_pegging_rec,
2801 					p_atp_record,
2802 					org_availability_info,
2803 					j,
2804 					item_availability_info,
2805 					l_item_cntr,
2806 					l_diag_transaction_id);
2807 
2808 			l_pegging_rec.atp_level:= p_level + 1;
2809 			l_pegging_rec.scenario_id:= p_scenario_id;
2810 
2811                         MSC_ATP_SUBST.Add_Pegging(l_pegging_rec);
2812                      END IF;
2813 --                  END IF; -- if l_available_qty > 0
2814 
2815                   IF PG_DEBUG in ('Y', 'C') THEN
2816                      msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'after adding Supply and Demand Pegging');
2817                   END IF;
2818                   --- now add planned order
2819 		  IF j > 1 and (l_available_quantity > 0 or MSC_ATP_PVT.G_DIAGNOSTIC_ATP = DIAGNOSTIC_ATP) THEN
2820                      --- we create planned order for tier 2 orgs only
2821        	             --  we create the planned order in parent org
2822                	     IF PG_DEBUG in ('Y', 'C') THEN
2823                	        msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_available_quantity > 0 or Diagnostic ATP');
2824 		 	msc_sch_wb.atp_debug('qty from children: ' || NVL(org_availability_info.quantity_from_children(l_parent_index), 0));
2825 			msc_sch_wb.atp_debug('l_available_quantity: '|| l_available_quantity);
2826                	     END IF;
2827 
2828 		     -- We balance out the supply/demand for the parent org since it is not a constraint node
2829 		     -- for the last transfer child of an org that gets into org_availability_info,
2830 		     -- we add a supply for the full demand placed on the child to do this.
2831 		     -- If the parent org is the top org and we perform ctp
2832 		     -- then we expect the ctp check to balance it out.
2833 
2834 		     IF ((j = org_availability_info.parent_org_idx.count OR
2835 			 org_availability_info.parent_org_idx(j+1) <>
2836 			 org_availability_info.parent_org_idx(j)) AND
2837 			 NOT (l_parent_index = 1 and l_create_supply_on_orig_item = 1)
2838 			) AND MSC_ATP_PVT.G_DIAGNOSTIC_ATP = DIAGNOSTIC_ATP
2839 		     THEN
2840                         -- dsting 2754446
2841 			l_diag_supply_qty := (org_availability_info.demand_quantity(l_parent_index) -
2842 					     NVL(org_availability_info.quantity_from_children(l_parent_index), 0) -
2843 					     greatest(org_availability_info.request_date_quantity(l_parent_index), 0)
2844                                              ) + l_available_quantity;
2845 			IF PG_DEBUG in ('Y', 'C') THEN
2846 				msc_sch_wb.atp_debug('remaining dmd qty of parent: ' || l_diag_supply_qty);
2847 			END IF;
2848 		     ELSE
2849                         -- dsting 2754446
2850 --			l_diag_supply_qty := least(org_availability_info.demand_quantity(j),
2851 			l_diag_supply_qty := least(l_PO_qty,
2852 					           greatest(0,l_available_quantity));
2853 			IF PG_DEBUG in ('Y', 'C') THEN
2854                                 msc_sch_Wb.atp_debug('l_PO_qty: ' || l_PO_qty);
2855 				msc_sch_wb.atp_debug('l_PO_qty - avail qty: ' || l_diag_supply_qty);
2856 			END IF;
2857 		     END IF;
2858 
2859 		     -- dsting set G_TOP_LAST_PO_PEGGING and G_TOP_LAST_PO_QTY in case the ctp
2860 		     -- pass needs to adjust the PO from the onhand pass
2861 		     IF l_parent_index = 1 AND MSC_ATP_SUBST.G_TOP_LAST_PO_PEGGING is NULL THEN
2862 			MSC_ATP_SUBST.G_TOP_LAST_PO_PEGGING := org_availability_info.PO_Pegging_id(j);
2863 			MSC_ATP_SUBST.G_TOP_LAST_PO_QTY := l_diag_supply_qty;
2864 			IF PG_DEBUG in ('Y', 'C') THEN
2865 				msc_sch_wb.atp_debug('Subst last PO pegging: ' || MSC_ATP_SUBST.G_TOP_LAST_PO_PEGGING);
2866 			END IF;
2867 		     END IF;
2868 
2869 		     IF l_diag_supply_qty > 0
2870 			OR MSC_ATP_SUBST.G_TOP_LAST_PO_PEGGING = org_availability_info.PO_Pegging_id(j)
2871 		     THEN
2872 
2873                        -- Begin ATP4drp Create Planned Arrivals for DRP plans
2874                        IF (NVL(MSC_ATP_PVT.G_PLAN_INFO_REC.plan_type,1) = 5) THEN
2875                           l_supply_rec.instance_id        :=   p_atp_record.instance_id;
2876                           l_supply_rec.plan_id            :=   org_availability_info.plan_id(l_parent_index);
2877                           l_supply_rec.inventory_item_id  :=  org_availability_info.family_dest_id(l_parent_index);
2878                                                                         -- DRP Plan re-set to member For time_phased_atp
2879                           l_supply_rec.organization_id    :=   org_availability_info.organization_id(l_parent_index);
2880                           l_supply_rec.schedule_date      :=   org_availability_info.requested_ship_date(l_parent_index);
2881                           l_supply_rec.order_quantity     :=   l_diag_supply_qty;
2882                              -- null; -- l_atp_rec.supplier_id,
2883                              -- null; -- l_atp_rec.supplier_site_id,
2884                           l_supply_rec.demand_class       :=   org_availability_info.demand_class(l_parent_index);
2885                           -- rajjain 02/19/2003 Bug 2788302 Begin
2886                           -- Add Sourcing details
2887                           l_supply_rec.source_organization_id :=   org_availability_info.organization_id(j);
2888                           l_supply_rec.source_sr_instance_id  :=   p_atp_record.instance_id;
2889                           -- null; --process seq id (transfer case)
2890                           -- rajjain 02/19/2003 Bug 2788302 End
2891                           l_supply_rec.refresh_number     :=   p_refresh_number; -- for summary enhancement
2892                           -- ship_rec_cal changes begin
2893                           l_supply_rec.shipping_cal_code  := org_availability_info.shipping_cal_code(j);   -- |
2894                           l_supply_rec.receiving_cal_code := org_availability_info.receiving_cal_code(j);  -- |
2895                           l_supply_rec.intransit_cal_code := org_availability_info.intransit_cal_code(j);  -- |
2896                           l_supply_rec.new_ship_date      := org_availability_info.new_ship_date(j);       -- |  Bug 3241766
2897                           l_supply_rec.new_dock_date      := org_availability_info.new_dock_date(j);       -- |
2898                                                                        -- |  Add new columns start date and order date
2899                           l_supply_rec.start_date         := org_availability_info.new_start_date(j);      -- |
2900                                                                        -- Use values from child org rather than parent
2901                           l_supply_rec.order_date         := org_availability_info.new_order_date(j);      -- |
2902                           l_supply_rec.ship_method        :=  org_availability_info.ship_method(j);         -- |
2903                           -- ship_rec_cal changes end
2904                           -- item_availability_info.inventory_item_id(l_item_cntr); -- For time_phased_atp
2905                           -- org_availability_info.atf_date(l_parent_index);        -- For time_phased_atp
2906 
2907                           l_supply_rec.firm_planned_type := 2;
2908                           l_supply_rec.disposition_status_type := 1;
2909                           l_supply_rec.record_source := 2; -- ATP created
2910                           l_supply_rec.supply_type := 51; --- planned arrival
2911                           l_supply_rec.intransit_lead_time := org_availability_info.lead_time(j); --4127630
2912 
2913                           MSC_ATP_DB_UTILS.ADD_Supplies(l_supply_rec);
2914                           -- Asssign the output to local variables.
2915                           l_transaction_id := l_supply_rec.transaction_id;
2916                           l_return_status  := l_supply_rec.return_status;
2917                           IF PG_DEBUG in ('Y', 'C') THEN
2918                              msc_sch_wb.atp_debug('----- ATP4drp Specific Debug Messages -----');
2919                              msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'DRP Plan Add Planned Inbound');
2920                              msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Supply Id l_transaction_id: ' || l_transaction_id);
2921                              msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Supply Type l_supply_rec.supply_type: '
2922                                                                                  || l_supply_rec.supply_type);
2923                              msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Return Status l_return_status: ' || l_return_status);
2924                              msc_sch_wb.atp_debug('----- ATP4drp Specific Debug Messages -----');
2925                           END IF;
2926 
2927                        ELSE  -- Create Planned Order otherwise.
2928                	          IF PG_DEBUG in ('Y', 'C') THEN
2929                	             msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Add planned order');
2930                           END IF;
2931 	                  MSC_ATP_DB_UTILS.Add_Planned_Order(
2932                                         p_atp_record.instance_id,
2933                                         org_availability_info.plan_id(l_parent_index),
2934                                         org_availability_info.family_dest_id(l_parent_index),  -- For time_phased_atp
2935                                         org_availability_info.organization_id(l_parent_index),
2936                                         org_availability_info.requested_ship_date(l_parent_index),
2937                                         l_diag_supply_qty,
2938                                         null, -- l_atp_rec.supplier_id,
2939                                         null, -- l_atp_rec.supplier_site_id,
2940                                         org_availability_info.demand_class(l_parent_index),
2941                                         -- rajjain 02/19/2003 Bug 2788302 Begin
2942                                         -- Add Sourcing details
2943                                         org_availability_info.organization_id(j),
2944                                         p_atp_record.instance_id,
2945                                         null, --process seq id (transfer case)
2946                                         -- rajjain 02/19/2003 Bug 2788302 End
2947                                         p_refresh_number, -- for summary enhancement
2948                                         -- ship_rec_cal changes begin
2949                                         org_availability_info.shipping_cal_code(j),   -- \
2950                                         org_availability_info.receiving_cal_code(j),  -- |
2951                                         org_availability_info.intransit_cal_code(j),  -- |
2952                                         org_availability_info.new_ship_date(j),       -- \  Bug 3241766
2953                                         org_availability_info.new_dock_date(j),       -- /  Add new columns start date and order date
2954                                         org_availability_info.new_start_date(j),      -- |  Use values from child org rather than parent
2955                                         org_availability_info.new_order_date(j),      -- |
2956                                         org_availability_info.ship_method(j),         -- /
2957                                         -- ship_rec_cal changes end
2958                                         l_transaction_id,
2959                                         l_return_status,
2960                                         org_availability_info.lead_time(j), --4127630
2961                                         item_availability_info.inventory_item_id(l_item_cntr), -- For time_phased_atp
2962                                         org_availability_info.atf_date(l_parent_index)         -- For time_phased_atp
2963 
2964                	          );
2965                        END IF;
2966                        -- End ATP4drp
2967 		     END IF;
2968 
2969 		     -- dsting open a planned order supply in the org to balance the
2970 		     -- demand if it is not a constraint node and its demand is greater than the supply
2971 		     IF org_availability_info.constraint_type(j) = NOSOURCES_NONCONSTRAINT AND
2972 		        org_availability_info.demand_quantity(j) > l_available_quantity AND
2973 		        MSC_ATP_PVT.G_DIAGNOSTIC_ATP = DIAGNOSTIC_ATP
2974 		     THEN
2975 
2976 		       IF PG_DEBUG in ('Y', 'C') THEN
2977 		          msc_sch_wb.atp_debug('balance supply/demand for nonconstrained node');
2978 		       END IF;
2979                        -- Begin ATP4drp Create Planned Arrivals for DRP plans
2980                        IF (NVL(MSC_ATP_PVT.G_PLAN_INFO_REC.plan_type,1) = 5) THEN
2981                           l_supply_rec.instance_id        :=   p_atp_record.instance_id;
2982                           l_supply_rec.plan_id            :=   org_availability_info.plan_id(l_parent_index);
2983                           l_supply_rec.inventory_item_id  :=   org_availability_info.family_dest_id(l_parent_index);
2984                                                                         -- DRP Plan re-set to member For time_phased_atp
2985                           l_supply_rec.organization_id    :=   org_availability_info.organization_id(l_parent_index);
2986                           l_supply_rec.schedule_date      :=   org_availability_info.requested_ship_date(l_parent_index);
2987                           l_supply_rec.order_quantity     :=
2988                                                            org_availability_info.demand_quantity(j) - l_available_quantity;
2989                              -- null; -- l_atp_rec.supplier_id,
2990                              -- null; -- l_atp_rec.supplier_site_id,
2991                           l_supply_rec.demand_class       :=   org_availability_info.demand_class(l_parent_index);
2992                           -- rajjain 02/19/2003 Bug 2788302 Begin
2993                           -- Add Sourcing details
2994                           l_supply_rec.source_organization_id :=   org_availability_info.organization_id(j);
2995                           l_supply_rec.source_sr_instance_id  :=   p_atp_record.instance_id;
2996                           -- null; --process seq id (transfer case)
2997                           -- rajjain 02/19/2003 Bug 2788302 End
2998                           l_supply_rec.refresh_number     :=   p_refresh_number; -- for summary enhancement
2999                           -- ship_rec_cal changes begin
3000                           l_supply_rec.shipping_cal_code  := org_availability_info.shipping_cal_code(j);   -- |
3001                           l_supply_rec.receiving_cal_code := org_availability_info.receiving_cal_code(j);  -- |
3002                           l_supply_rec.intransit_cal_code := org_availability_info.intransit_cal_code(j);  -- |
3003                           l_supply_rec.new_ship_date      := org_availability_info.new_ship_date(j);       -- |  Bug 3241766
3004                           l_supply_rec.new_dock_date      := org_availability_info.new_dock_date(j);       -- |
3005                                                                        -- |  Add new columns start date and order date
3006                           l_supply_rec.start_date         := org_availability_info.new_start_date(j);      -- |
3007                                                                        -- Use values from child org rather than parent
3008                           l_supply_rec.order_date         := org_availability_info.new_order_date(j);      -- |
3009                           l_supply_rec.ship_method        :=  org_availability_info.ship_method(j);         -- |
3010                           -- ship_rec_cal changes end
3011                           -- item_availability_info.inventory_item_id(l_item_cntr); -- For time_phased_atp
3012                           -- org_availability_info.atf_date(l_parent_index);        -- For time_phased_atp
3013 
3014                           l_supply_rec.firm_planned_type := 2;
3015                           l_supply_rec.disposition_status_type := 1;
3016                           l_supply_rec.record_source := 2; -- ATP created
3017                           l_supply_rec.supply_type := 51; --- planned arrival
3018                           l_supply_rec.intransit_lead_time := org_availability_info.lead_time(j); --4127630
3019 
3020                           MSC_ATP_DB_UTILS.ADD_Supplies(l_supply_rec);
3021                           -- Asssign the output to local variables.
3022                           l_transaction_id := l_supply_rec.transaction_id;
3023                           l_return_status  := l_supply_rec.return_status;
3024                           IF PG_DEBUG in ('Y', 'C') THEN
3025                              msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Supply Id l_transaction_id: ' || l_transaction_id);
3026                              msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Supply Type l_supply_rec.supply_type: '
3027                                                                                  || l_supply_rec.supply_type);
3028                              msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Return Status l_return_status: ' || l_return_status);
3029                           END IF;
3030 
3031                        ELSE  -- Create Planned Order otherwise.
3032 	                  MSC_ATP_DB_UTILS.Add_Planned_Order(
3033                                 p_atp_record.instance_id,
3034                	                org_availability_info.plan_id(l_parent_index),
3035                                	org_availability_info.family_dest_id(j),               -- For time_phased_atp
3036                                	org_availability_info.organization_id(j),
3037                                 org_availability_info.requested_ship_date(j),
3038                                 org_availability_info.demand_quantity(j) - l_available_quantity,
3039                	                null, -- l_atp_rec.supplier_id,
3040                        	        null, -- l_atp_rec.supplier_site_id,
3041                                	org_availability_info.demand_class(l_parent_index),
3042                                 -- rajjain 02/19/2003 Bug 2788302 Begin
3043                                 -- Add Sourcing details
3044                                 org_availability_info.organization_id(j),
3045                                 p_atp_record.instance_id,
3046                                 null, --process seq id (transfer case)
3047                                 -- rajjain 02/19/2003 Bug 2788302 End
3048                                 p_refresh_number, -- for summary enhancement
3049                                 -- ship_rec_cal changes begin
3050                                 org_availability_info.shipping_cal_code(j),
3051                                 org_availability_info.receiving_cal_code(j),
3052                                 org_availability_info.intransit_cal_code(j),
3053                                 org_availability_info.new_ship_date(j),
3054                                 org_availability_info.new_dock_date(j),
3055                                 org_availability_info.new_start_date(j),        -- Bug 3241766
3056                                 org_availability_info.new_order_date(j),        -- Bug 3241766
3057                                 org_availability_info.ship_method(j),
3058                                 -- ship_rec_cal changes end
3059                                 l_diag_transaction_id,
3060                                 l_return_status,
3061                                 org_availability_info.lead_time(j), --4127630
3062                                 item_availability_info.inventory_item_id(l_item_cntr), -- For time_phased_atp
3063                                 org_availability_info.atf_date(j)                      -- For time_phased_atp
3064 
3065                	          );
3066                        END IF;
3067                        -- End ATP4drp
3068 
3069 		     END IF;
3070 
3071                      --- now add PO pegging
3072                      IF PG_DEBUG in ('Y', 'C') THEN
3073                         msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Add pegging for PO');
3074                         msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_parent_index := ' || l_parent_index);
3075                      END IF;
3076 
3077 		     Prep_PO_Pegging_Rec(l_pegging_rec,
3078 				p_atp_record,
3079 				org_availability_info,
3080 				j,
3081 				item_availability_info,
3082 				l_item_cntr,
3083                                 least(l_PO_qty, l_available_quantity),  -- dsting 2754446
3084 --				l_available_quantity,
3085 				l_transaction_id);
3086 
3087                      l_pegging_rec.atp_level:= p_level+1;
3088                      l_pegging_rec.scenario_id:= p_scenario_id;
3089 
3090                      MSC_ATP_SUBST.Add_Pegging(l_pegging_rec);
3091 
3092                   END IF;
3093                END IF; -- <<ADD_ONHAND_PEGGING>>
3094             END IF; -- IF NVL(org_availability_info.demand_id(j), -1) > 0  THEN
3095             IF PG_DEBUG in ('Y', 'C') THEN
3096                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'after adding pegging for supplies, demand and planned order');
3097             END IF;
3098 
3099 
3100 
3101 /*            --- now set the quantity from children
3102 	    -- dsting I already got it for diagnostic atp
3103             IF j > 1 AND MSC_ATP_PVT.G_DIAGNOSTIC_ATP <> DIAGNOSTIC_ATP THEN
3104                l_PO_qty := org_availability_info.real_dmd_qty(j)
3105                   * org_availability_info.conversion_rate(l_parent_index)
3106                   / org_availability_info.conversion_rate(j);
3107 
3108                IF nvl(org_availability_info.rounding_flag(l_parent_index),2) = 1 THEN
3109                   l_PO_qty := CEIL(ROUND(l_PO_qty,10));
3110                END IF;
3111                org_availability_info.quantity_from_children(l_parent_index) :=
3112                                      NVL(org_availability_info.quantity_from_children(l_parent_index), 0) +
3113                                      LEAST((GREATEST(org_availability_info.request_date_quantity(j), 0) +
3114                                             NVL(org_availability_info.quantity_from_children(j), 0) +
3115                                             NVL(org_availability_info.steal_qty(j), 0)),
3116                                             l_PO_qty/org_availability_info.conversion_rate(l_parent_index));
3117 
3118                IF PG_DEBUG in ('Y', 'C') THEN
3119                   msc_sch_wb.atp_debug('ATP_Check_Subst: Update children quantity in parent org: idx '
3120                         || l_parent_index || ' qty: ' || org_availability_info.quantity_from_children(l_parent_index));
3121                END IF;
3122             END IF;
3123 */
3124          END LOOP; -- FOR j in reverse 1..org_availability_info.organization_id.count LOOP
3125 
3126 
3127          --4686870 , remove the records with origination_type as -200, when the supplies have been added.
3128          FOR j in 1..org_availability_info.organization_id.count LOOP
3129             DELETE MSC_DEMANDS
3130             where origination_type = -200
3131             and   sr_instance_id   = p_atp_record.instance_id
3132             and   plan_id          = org_availability_info.plan_id(j)
3133             and   demand_id        = org_availability_info.demand_id(j)
3134             and   organization_id  = org_availability_info.organization_id(j);
3135          END LOOP;
3136 
3137          IF PG_DEBUG in ('Y', 'C') THEN
3138             msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'After getting availablity for current item in all orgs');
3139          END IF;
3140          -- now we populate item info
3141          IF PG_DEBUG in ('Y', 'C') THEN
3142             msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'End pegging id := ' || org_availability_info.demand_pegging_id(1));
3143             msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_item_counter := ' || l_item_cntr);
3144          END IF;
3145          Item_availability_info.End_pegging_id(l_item_cntr) :=  org_availability_info.demand_pegging_id(1);
3146          Item_availability_info.request_date_quantity(l_item_cntr) :=
3147                                                          GREATEST(org_availability_info.request_date_quantity(1), 0) +
3148                                                              NVL(org_availability_info.Quantity_from_children(1), 0) +
3149                                                              NVL(org_availability_info.steal_qty(1), 0);
3150          --bug3467631
3151          Item_availability_info.atf_date_quantity(l_item_cntr) := org_availability_info.atf_date_quantity(1);
3152          --- populate the end index of period and supply detils for this item
3153          l_period_end_idx := l_all_atp_period.level.count;
3154          l_sd_end_idx := l_all_atp_supply_demand.level.count;
3155 
3156          IF PG_DEBUG in ('Y', 'C') THEN
3157             --bug3467631 start
3158             msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'request_date_quantity := ' || Item_availability_info.request_date_quantity(l_item_cntr));
3159             msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Atf_Date_Quantity := ' || Item_availability_info.Atf_Date_Quantity(l_item_cntr));
3160             --bug3467631 end
3161             msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_period_end_idx := ' || l_period_end_idx);
3162             msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_sd_end_idx := ' || l_sd_end_idx);
3163          END IF;
3164 
3165          item_availability_info.period_detail_end_Idx(l_item_cntr) := l_period_end_idx;
3166          item_availability_info.sd_detail_end_idx(l_item_cntr) := l_sd_end_idx;
3167 
3168          IF PG_DEBUG in ('Y', 'C') THEN
3169             msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'After updating item info');
3170          END IF;
3171 
3172 	 -- Set p_atp_record to the item that is used to satisfy the demand
3173          IF l_net_demand <= 0 THEN
3174             IF PG_DEBUG in ('Y', 'C') THEN
3175                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Met the requirement using item ' || item_availability_info.item_name(l_item_cntr));
3176                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Sr inv Id for the item := ' || item_availability_info.sr_inventory_item_id(l_item_cntr));
3177             END IF;
3178             --IF item_availability_info.sr_inventory_item_id(l_item_cntr) <> p_atp_record.inventory_item_id THEN
3179             --bug3467631
3180             IF item_availability_info.sr_inventory_item_id(l_item_cntr) <>
3181                                                NVL(p_atp_record.request_item_id,
3182                                                     p_atp_record.inventory_item_id) THEN
3183                IF PG_DEBUG in ('Y', 'C') THEN
3184                   msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'demand is satisfied by substitute');
3185                END IF;
3186                --- set the flag
3187                l_satisfied_by_subst_flag := 1;
3188             ELSE
3189                IF PG_DEBUG in ('Y', 'C') THEN
3190                   msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'demand is satisfied by the original item');
3191                END IF;
3192                l_satisfied_by_subst_flag := 2;
3193             END IF;
3194             IF PG_DEBUG in ('Y', 'C') THEN
3195                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_satisfied_by_subst_flag := ' || l_satisfied_by_subst_flag);
3196             END IF;
3197 
3198             --l_demand_satisfied_by_item_id := item_availability_info.sr_inventory_item_id(l_item_cntr);
3199             /* time_phased_atp changes begin
3200                request_item_id will store substitute item id
3201                inventory_item_id will store substitute family id
3202                original item id will store demanded item id*/
3203                --bug3467631 added so that correct names are shown
3204             --bug3709707 atf_date also populated so that correct atf date is returned to schedule
3205             p_atp_record.atf_date := item_availability_info.atf_date(l_item_cntr);
3206             p_atp_record.inventory_item_id := item_availability_info.family_sr_id(l_item_cntr);
3207             p_atp_record.inventory_item_name := item_availability_info.family_item_name(l_item_cntr);
3208             p_atp_record.request_item_id := item_availability_info.sr_inventory_item_id(l_item_cntr);
3209             p_atp_record.request_item_name := item_availability_info.item_name(l_item_cntr);
3210             p_atp_record.original_item_id := item_availability_info.sr_inventory_item_id(l_item_count);
3211             p_atp_record.original_item_name := item_availability_info.item_name(l_item_count);
3212 
3213             IF PG_DEBUG in ('Y', 'C') THEN
3214                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'inventory_item_id := ' || p_atp_record.inventory_item_id);
3215                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'inventory_item_name := ' || p_atp_record.inventory_item_name);
3216                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'request_item_id := ' || p_atp_record.request_item_id);
3217                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'request_item_name := ' || p_atp_record.request_item_name);
3218                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'original_item_id := ' || p_atp_record.original_item_id);
3219                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'original_item_name := ' || p_atp_record.original_item_name);
3220             END IF;
3221             -- time_phased_atp changes end
3222 
3223             --p_atp_record.End_pegging_id := item_availability_info.End_pegging_id(l_item_cntr);
3224             --- this variable is populated in end_peggign id for this line in procedure schedule
3225             MSC_ATP_PVT.G_DEMAND_PEGGING_ID := item_availability_info.End_pegging_id(l_item_cntr);
3226             l_demand_pegging_id := item_availability_info.End_pegging_id(l_item_cntr);
3227 
3228             p_atp_record.requested_date_quantity := item_availability_info.request_date_quantity(l_item_cntr);
3229        	    p_atp_record.available_quantity :=  item_availability_info.request_date_quantity(l_item_cntr);
3230        	    p_atp_record.Atf_Date_Quantity := item_availability_info.Atf_Date_Quantity(l_item_cntr); --bug3467631
3231             IF NVL(p_atp_record.atp_lead_time, 0) > 0 THEN
3232        	      /*
3233        	      p_atp_record.ship_date := MSC_CALENDAR.DATE_OFFSET
3234                 	                 (p_atp_record.organization_id,
3235                         	          p_atp_record.instance_id,
3236                                 	  1,
3237 	                                  l_requested_ship_date,
3238         	                          p_atp_record.atp_lead_time);*/
3239        	      p_atp_record.ship_date := MSC_CALENDAR.DATE_OFFSET(
3240                 	                  p_atp_record.manufacturing_cal_code,
3241                         	          p_atp_record.instance_id,
3242 	                                  l_requested_ship_date,
3243         	                          p_atp_record.atp_lead_time, 1);
3244             ELSE
3245        	      p_atp_record.ship_date := l_requested_ship_date;
3246             END IF;
3247 
3248             --p_atp_record.inventory_item_id := item_availability_info.sr_inventory_item_id(l_item_cntr);
3249             l_count := Item_availability_info.inventory_item_id.count;
3250             p_atp_record.req_item_req_date_qty := Item_availability_info.request_date_quantity(l_item_count);
3251             --bug3467631
3252             IF Item_availability_info.sr_inventory_item_id(l_item_cntr) = NVL(p_atp_record.request_item_id,
3253                                                     p_atp_record.inventory_item_id) THEN
3254                 --- request has been fullfilled by original item
3255                 p_atp_record.req_item_req_date_qty := item_availability_info.request_date_quantity(l_item_cntr);
3256                 p_atp_record.req_item_available_date := l_requested_ship_date;
3257                 p_atp_record.req_item_available_date_qty
3258                                     :=  item_availability_info.request_date_quantity(l_item_cntr);
3259             END IF;
3260 
3261             --- add suply demand and period details
3262        	    IF PG_DEBUG in ('Y', 'C') THEN
3263        	       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'period_detail_begin_idx := '|| item_availability_info.period_detail_begin_idx(l_item_cntr));
3264                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'period_detail_end_idx := ' || item_availability_info.period_detail_end_idx(l_item_cntr));
3265        	       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'sd_detail_begin_idx:= ' || item_availability_info.sd_detail_begin_idx(l_item_cntr));
3266                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'sd_detail_end_idx:= ' || item_availability_info.sd_detail_end_idx(l_item_cntr));
3267                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'p_atp_record.Atf_Date_Quantity:= ' || p_atp_record.Atf_Date_Quantity); --bug3467631
3268             END IF;
3269        	    MSC_ATP_SUBST.Details_Output(l_all_atp_period,
3270                                          l_all_atp_supply_demand,
3271                                          item_availability_info.period_detail_begin_idx(l_item_cntr),
3272                                          item_availability_info.period_detail_end_idx(l_item_cntr),
3273                                          item_availability_info.sd_detail_begin_idx(l_item_cntr),
3274                                          item_availability_info.sd_detail_end_idx(l_item_cntr),
3275                                          x_atp_period,
3276                                          x_atp_supply_demand,
3277                                          l_return_status);
3278             --- now remove the demands for other items
3279        	    FOR i in (l_item_cntr +1)..l_item_count LOOP
3280                 IF NOT (l_org_item_detail_flag = 1 and l_satisfied_by_subst_flag = 1 and
3281        	             item_availability_info.sr_inventory_item_id(i) = MSC_ATP_SUBST.G_REQ_ITEM_SR_INV_ID) THEN
3282                	   IF PG_DEBUG in ('Y', 'C') THEN
3283                	      msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'remove demands for item ' || item_availability_info.item_name(i));
3284                	   END IF;
3285                    MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(
3286                                             item_availability_info.End_pegging_id(i),
3287                                              null,
3288                                              item_availability_info.plan_id(i),
3289                                              MSC_ATP_PVT.UNDO,
3290                                              null,
3291                                              l_return_status);
3292        	        END IF;
3293             END LOOP;
3294          END IF; -- IF l_net_demand <= 0 THEN
3295          --- increate the item counter
3296          --l_item_cntr := l_item_cntr + 1;
3297 
3298       END LOOP; ---  WHILE item_counter <= item_availability_info.inventory_item_id.count AND
3299       IF PG_DEBUG in ('Y', 'C') THEN
3300          msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Item Availability Picture After Onhand search');
3301       END IF;
3302 
3303       IF PG_DEBUG in ('Y', 'C') THEN
3304          For i in 1..item_availability_info.inventory_item_id.count LOOP  --bug3467631
3305              msc_sch_wb.atp_debug('ATP_Check_Subst: ' || item_availability_info.item_name(i) ||' ' ||item_availability_info.sr_inventory_item_id(i) ||
3306                                ' ' || item_availability_info.request_date_quantity(i) ||
3307                                ' ' || item_availability_info.Atf_Date_Quantity(i));
3308              msc_sch_wb.atp_debug('ATP_Check_Subst: ' || item_availability_info.family_sr_id(i) ||' ' ||item_availability_info.family_item_name(i));
3309 
3310          END LOOP;
3311       END IF;
3312 
3313       IF PG_DEBUG in ('Y', 'C') THEN
3314          msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Do backward CTP pass if needed. net demand: ' || l_net_demand);
3315       END IF;
3316 
3317 
3318       --- if demand is not met then do CTP
3319       --- Also if demand is met by substitute item then we will provide atp date and quantity
3320       --- for original item depending upon value of original_item_details_flag.
3321       -- Therefore we might need to do CTP even on original item even if demand is met
3322       --- Backward CTP
3323       --l_org_item_detail_flag := NVL(p_atp_record.req_item_detail_flag, 2);
3324       --l_org_item_detail_flag := 1;
3325       l_net_demand_after_oh_check := l_net_demand;
3326       IF PG_DEBUG in ('Y', 'C') THEN
3327          msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Net demand after Onhand search := ' || l_net_demand);
3328          msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_org_item_detail_flag := ' || l_org_item_detail_flag);
3329          msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_satisfied_by_subst_flag := ' || l_satisfied_by_subst_flag);
3330       END IF;
3331       IF ((l_org_item_detail_flag = 1 AND l_satisfied_by_subst_flag = 1)) or (l_net_demand_after_oh_check > 0) THEN
3332 
3333          --- get create supply flag
3334          --- if create supply flag is null then default it to Demanded item
3335          IF PG_DEBUG in ('Y', 'C') THEN
3336             msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Backward Ctp');
3337          END IF;
3338          l_create_supply_flag := MSC_ATP_SUBST.G_CREATE_SUPPLY_FLAG;
3339          l_count := item_availability_info.inventory_item_id.count;
3340          IF PG_DEBUG in ('Y', 'C') THEN
3341             msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'G_CREATE_SUPPLY_FLAG := ' || MSC_ATP_SUBST.G_CREATE_SUPPLY_FLAG);
3342          END IF;
3343          --- bug 2388707: if demand has already been met then just add req item to the list of items
3344          --  to do CTP on
3345          IF (l_org_item_detail_flag = 1 AND l_satisfied_by_subst_flag = 1) THEN
3346              IF (MSC_ATP_SUBST.G_CREATE_SUPPLY_FLAG in (G_DEMANDED_ITEM, G_ITEM_ATTRIBUTE))
3347                 OR (MSC_ATP_SUBST.G_CREATE_SUPPLY_FLAG = G_HIGHEST_REV_ITEM AND
3348                      l_highest_rev_item_id = item_availability_info.inventory_item_id(l_item_count)) THEN
3349                 MSC_ATP_SUBST.Copy_Item_Info_rec(item_availability_info, l_item_ctp_info, l_item_count);
3350              END IF;
3351          ELSIF MSC_ATP_SUBST.G_CREATE_SUPPLY_FLAG = G_DEMANDED_ITEM  THEN
3352 
3353              MSC_ATP_SUBST.Copy_Item_Info_rec(item_availability_info, l_item_ctp_info, l_item_count);
3354 
3355          ELSIF  MSC_ATP_SUBST.G_CREATE_SUPPLY_FLAG = G_HIGHEST_REV_ITEM THEN
3356              IF item_availability_info.inventory_item_id.count = 1 THEN
3357                 --- we will come here if there are no substitutes or we are not substituting
3358                 IF PG_DEBUG in ('Y', 'C') THEN
3359                    msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'One item. highest rev ' || l_highest_rev_item_id);
3360                 END IF;
3361                 IF (l_highest_rev_item_id = l_inventory_item_id) AND
3362                                (item_availability_info.create_supply_flag(l_item_count) = 1) THEN
3363 
3364                     MSC_ATP_SUBST.Copy_Item_Info_rec(item_availability_info, l_item_ctp_info, l_item_count);
3365                 END IF;
3366              ELSE
3367                 --- we will come here if we have one or more substitutes and we are doing substitution
3368                 IF PG_DEBUG in ('Y', 'C') THEN
3369                    msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Multiple items');
3370                 END IF;
3371                 For i in 1..item_availability_info.inventory_item_id.count LOOP
3372                     -- find highest revision item
3373                     IF item_availability_info.inventory_item_id(i) = l_highest_rev_item_id THEN
3374                        l_highest_rev_item_index := i;
3375                        IF PG_DEBUG in ('Y', 'C') THEN
3376                           msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'i := ' || i);
3377                        END IF;
3378                        EXIT;
3379                     END IF;
3380 
3381                  END LOOP;
3382                 IF PG_DEBUG in ('Y', 'C') THEN
3383                    msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Counter for High. rev. item := ' || l_highest_rev_item_index);
3384                 END IF;
3385                 MSC_ATP_SUBST.Copy_Item_Info_rec(item_availability_info, l_item_ctp_info, l_highest_rev_item_index);
3386              END IF; -- IF item_availability_info.inventory_item_id.count = 1 THEN
3387          ELSE --- item attribute
3388             --- look at item attributes
3389             IF l_net_demand_after_oh_check > 0 THEN
3390                --- we haven't satisfied the demand as yet
3391                l_item_ctp_info := item_availability_info;
3392             ELSE
3393                --- we have satisfied the demand but we need to look for original item
3394                MSC_ATP_SUBST.Copy_Item_Info_rec(item_availability_info, l_item_ctp_info, l_count);
3395             END IF;
3396          END IF; -- IF MSC_ATP_SUBST.G_CREATE_SUPPLY_FLAG = 701
3397 
3398          IF PG_DEBUG in ('Y', 'C') THEN
3399             msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'List of items to do CTP on');
3400             For i in reverse 1..l_item_ctp_info.inventory_item_id.count LOOP
3401                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || l_item_ctp_info.item_name(i) || ' ' || l_item_ctp_info.sr_inventory_item_id(i) );
3402             END LOOP;
3403 	    msc_sch_wb.atp_debug('Subst last PO pegging: ' || MSC_ATP_SUBST.G_TOP_LAST_PO_PEGGING);
3404          END IF;
3405 
3406          -- AT this point we know on what item do we need to do CTP
3407          --l_net_demand :=
3408          IF (l_org_item_detail_flag = 1 AND l_satisfied_by_subst_flag = 1) THEN
3409             l_net_demand := p_atp_record.quantity_ordered -
3410                                        item_availability_info.request_date_quantity(l_item_count);
3411             l_insert_flag := 0;
3412          ELSE
3413             l_insert_flag := p_atp_record.insert_flag;
3414          END IF;
3415 
3416 	 IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = DIAGNOSTIC_ATP THEN
3417 		MSC_ATP_PVT.G_DEMAND_PEGGING_ID := item_availability_info.end_pegging_id(1);
3418 	 END IF;
3419 
3420          FOR i in reverse 1..l_item_ctp_info.inventory_item_id.count LOOP
3421 
3422              IF l_net_demand <= 0 or l_requested_ship_date < l_sys_date_top_org THEN
3423                 EXIT;
3424              END IF;
3425              l_net_demand := p_atp_record.quantity_ordered - l_item_ctp_info.request_date_quantity(i);
3426              IF PG_DEBUG in ('Y', 'C') THEN
3427                 msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Do CTP on := ' || l_item_ctp_info.item_name(i) || ' '
3428                                                   || l_item_ctp_info.sr_inventory_item_id(i));
3429                 msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_net_demand := ' || l_net_demand);
3430                 msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'atp_comp_flag := ' || l_item_ctp_info.atp_comp_flag(i));
3431                 msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'create_supply_flag := ' || l_item_ctp_info.create_supply_flag(i));
3432                 msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'end pegging id := ' || l_item_ctp_info.end_pegging_id(i));
3433              END IF;
3434 
3435 		    -- ALLOC ATP CHANGES, 12973673
3436             IF (MSC_ATP_PVT.G_INV_CTP = 4 AND MSC_ATP_PVT.G_HYBRID_ALLOC_ATP = 'Y') THEN
3437                IF PG_DEBUG in ('Y', 'C') THEN
3438                     msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'G_HYBRID_ALLOC_ATP is Y');
3439                 END IF;
3440 
3441                 MSC_ATP_PVT.G_ALLOCATED_ATP := MSC_ATP_FUNC.check_alloc_exists(p_atp_record.instance_id,
3442                                 -1, --PLAN_ID
3443                                 l_item_ctp_info.sr_inventory_item_id(i),
3444                                 p_atp_record.organization_id,
3445                                 nvl(l_requested_ship_date,trunc(sysdate)));
3446 
3447                 IF PG_DEBUG in ('Y', 'C') THEN
3448                    msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'MSC_ATP_PVT.G_ALLOCATED_ATP = '||MSC_ATP_PVT.G_ALLOCATED_ATP);
3449                 END IF;
3450              END IF;
3451 
3452              IF l_item_ctp_info.atp_comp_flag(i) in ('Y', 'C', 'R') and l_item_ctp_info.create_supply_flag(i) = 1 THEN
3453 
3454                 l_atp_rec.error_code := 0;
3455                 l_atp_rec.available_quantity := NULL;
3456                 l_atp_rec.requested_date_quantity := NULL;
3457 
3458 
3459 
3460                 -- no need to do uom conversion
3461                 l_atp_rec.instance_id := p_atp_record.instance_id;
3462                 -- what do we need to do with thie ???
3463                 l_atp_rec.demand_source_line := null;
3464                 l_atp_rec.identifier := MSC_ATP_PVT.G_ORDER_LINE_ID;
3465                 l_atp_rec.component_identifier :=  null ; --l_comp_requirements.component_identifier(j);
3466 
3467                 -- time_phased_atp bug3467631
3468                 l_atp_rec.inventory_item_id := l_item_ctp_info.family_sr_id(i);
3469                 /*l_atp_rec.inventory_item_id := MSC_ATP_PF.Get_Pf_Atp_Item_Id(
3470                                                         p_atp_record.instance_id,
3471                                                         l_item_ctp_info.plan_id(i),
3472                                                         l_item_ctp_info.sr_inventory_item_id(i),
3473                                                         p_atp_record.organization_id
3474                                                );*/
3475 		-- dsting
3476 		IF PG_DEBUG in ('Y', 'C') THEN
3477 		   msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'assigning item_name: ' ||l_item_ctp_info.item_name(i));
3478 		   msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'assigning inventory_item_id: ' ||l_atp_rec.inventory_item_id);
3479 		END IF;
3480                 l_atp_rec.inventory_item_name := l_item_ctp_info.item_name(i);
3481                 l_atp_rec.request_item_id := l_item_ctp_info.sr_inventory_item_id(i);
3482                 l_atp_rec.organization_id := p_atp_record.organization_id;
3483                 l_atp_rec.quantity_ordered := l_net_demand;
3484                 --setting atf_date
3485                 l_atp_rec.Atf_Date := l_item_ctp_info.Atf_Date(i); --bug3467631
3486                 -- l_atp_rec.quantity_uom := l_quantity_uom;
3487                 l_atp_rec.requested_ship_date := l_requested_ship_date;
3488                 l_atp_rec.demand_class := p_atp_record.demand_class;
3489                 l_atp_rec.insert_flag := l_insert_flag;
3490                 l_atp_rec.refresh_number := p_refresh_number;
3491                 l_atp_rec.refresh_number := null;
3492 	        l_atp_rec.ship_date := null;
3493                 IF PG_DEBUG in ('Y', 'C') THEN
3494                    msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'demand class for CTP := ' ||  l_item_ctp_info.demand_class(i));
3495                 END IF;
3496                 l_atp_rec.demand_class := l_item_ctp_info.demand_class(i);
3497 
3498                 l_plan_id :=  l_item_ctp_info.plan_id(i);
3499                 l_assign_set_id := l_item_ctp_info.assign_set_id(i);
3500 
3501                 l_atp_rec.subs_demand_id := l_item_ctp_info.demand_id(i); --5088719
3502                 IF PG_DEBUG in ('Y', 'C') THEN
3503                    msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_item_ctp_info.demand_id(i) ' ||  l_item_ctp_info.demand_id(i));
3504                 END IF;
3505 
3506                 l_atp_rec.original_item_flag := 1;
3507                 l_atp_rec.top_tier_org_flag := 1;
3508                 l_atp_rec.delivery_lead_time := 0;
3509                 MSC_ATP_PVT.G_DEMAND_PEGGING_ID :=  l_item_ctp_info.end_pegging_id(i);
3510                 --diag_atp
3511                 l_atp_rec.plan_name := l_item_ctp_info.plan_name(i);
3512 
3513 		-- dsting diag_atp
3514 		MSC_ATP_PVT.G_HAVE_MAKE_BUY_PARENT := 0;
3515 
3516 		-- bug3467631 done for ship_rec_cal
3517                 l_atp_rec.shipping_cal_code       := p_atp_record.shipping_cal_code;
3518                 l_atp_rec.receiving_cal_code      := p_atp_record.receiving_cal_code;
3519                 l_atp_rec.intransit_cal_code      := p_atp_record.intransit_cal_code;
3520                 l_atp_rec.manufacturing_cal_code  := p_atp_record.manufacturing_cal_code;
3521 
3522 				l_alloc_atp := MSC_ATP_PVT.G_ALLOCATED_ATP; --ALLOC ATP CHANGES, 12973673
3523                 MSC_ATP_PVT.ATP_Check(l_atp_rec,
3524                                      l_plan_id,
3525                                      p_level ,
3526                                      p_scenario_id,
3527                                      1,
3528                                      p_refresh_number,
3529                                      l_item_ctp_info.end_pegging_id(i),
3530                                      l_assign_set_id,
3531                                      l_atp_period,
3532                                      l_atp_supply_demand,
3533                                      x_return_status);
3534                 IF MSC_ATP_PVT.G_HYBRID_ALLOC_ATP = 'Y' THEN
3535                     MSC_ATP_PVT.G_ALLOCATED_ATP := l_alloc_atp; --ALLOC ATP CHANGES, 12973673
3536                 END IF;
3537              ELSE
3538                  l_atp_rec.requested_date_quantity := 0;
3539                  --Bug 3878343 , set l_atp_period to NULL, incase CTP is not done.
3540                  l_atp_period := L_NULL_ATP_PERIOD;
3541              END IF;
3542              IF PG_DEBUG in ('Y', 'C') THEN
3543                 msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_atp_rec.requested_date_quantity := ' || l_atp_rec.requested_date_quantity);
3544                 msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_atp_rec.atf_date_quantity := ' || l_atp_rec.atf_date_quantity);
3545              END IF;
3546              l_net_demand := l_net_demand - l_atp_rec.requested_date_quantity;
3547              IF PG_DEBUG in ('Y', 'C') THEN
3548                 msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_net_demand := ' || l_net_demand);
3549              END IF;
3550 
3551              IF l_item_count = 1 THEN
3552                 IF PG_DEBUG in ('Y', 'C') THEN
3553                    msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Only one item');
3554                 END IF;
3555                 l_item_idx := 1;
3556              ELSE
3557                 --bug 2388707: If demand has already been met then we are just looking for original item
3558                 -- therefore we set the index to be that of last item.
3559                 IF (l_org_item_detail_flag = 1 AND l_satisfied_by_subst_flag = 1) THEN
3560                    l_item_idx := l_item_count;
3561 
3562                 ELSIF MSC_ATP_SUBST.G_CREATE_SUPPLY_FLAG = G_DEMANDED_ITEM THEN
3563                    IF PG_DEBUG in ('Y', 'C') THEN
3564                       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'More item but we retun the index for original item');
3565                    END IF;
3566                    l_item_idx := l_item_count;
3567                 ELSIF MSC_ATP_SUBST.G_CREATE_SUPPLY_FLAG = G_HIGHEST_REV_ITEM THEN
3568                    IF PG_DEBUG in ('Y', 'C') THEN
3569                       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Index for highest rev item');
3570                    END IF;
3571                    l_item_idx := l_highest_rev_item_index;
3572                 ELSE
3573 
3574                    IF l_item_ctp_info.inventory_item_id.count = 1 THEN
3575                       --- this case will be true only if we have already satisfied the demand
3576                       --- using a substitute
3577                       IF PG_DEBUG in ('Y', 'C') THEN
3578                          msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'we have alread met req. We return index for req item');
3579                       END IF;
3580                       l_item_idx := l_item_count;
3581                    ELSE
3582                       IF PG_DEBUG in ('Y', 'C') THEN
3583                          msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Item Attib, index of current item');
3584                       END IF;
3585                       l_item_idx := i;
3586                    END IF;
3587                 END IF;
3588              END IF;
3589              IF PG_DEBUG in ('Y', 'C') THEN
3590                 msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_item_idx := ' || l_item_idx);
3591              END IF;
3592              l_item_ctp_info.request_date_quantity(i) :=
3593                         NVL(l_item_ctp_info.request_date_quantity(i), 0) + l_atp_rec.requested_date_quantity;
3594              item_availability_info.request_date_quantity(l_item_idx) :=
3595                        NVL(item_availability_info.request_date_quantity(l_item_idx), 0) +
3596                           l_atp_rec.requested_date_quantity;
3597              --bug3467631 adding atf_date_quantity from backward CTP
3598              l_item_ctp_info.atf_date_quantity(i) :=
3599                         NVL(l_item_ctp_info.atf_date_quantity(i), 0) +
3600                            NVL(l_atp_rec.atf_date_quantity,l_atp_rec.requested_date_quantity);
3601              item_availability_info.atf_date_quantity(l_item_idx) :=
3602                        NVL(item_availability_info.atf_date_quantity(l_item_idx), 0) +
3603                          + NVL(l_atp_rec.atf_date_quantity, l_atp_rec.requested_date_quantity);
3604 
3605              IF PG_DEBUG in ('Y', 'C') THEN --bug3467631
3606                 msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'item_availability_info.request_date_quantity(l_item_idx) := '|| item_availability_info.request_date_quantity(l_item_idx));
3607                 msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_atp_rec.requested_date_quantity := ' || l_atp_rec.requested_date_quantity);
3608                 msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_item_ctp_info.requested_date_quantity := ' || l_item_ctp_info.request_date_quantity(i));
3609                 msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'item_availability_info.atf_date_quantity(l_item_idx) := '|| item_availability_info.atf_date_quantity(l_item_idx));
3610                 msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_atp_rec.atf_date_quantity := ' || l_atp_rec.atf_date_quantity);
3611                 msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_item_ctp_info.atf_date_quantity := ' || l_item_ctp_info.atf_date_quantity(i));
3612                 msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'item_availability_info.request_date_quantity(l_item_idx) := ' || item_availability_info.request_date_quantity(l_item_idx));
3613                 msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_item_ctp_info.Atf_Date := ' || l_item_ctp_info.Atf_Date(i));
3614              END IF;
3615              IF l_net_demand <= 0 THEN
3616                  IF l_net_demand_after_oh_check > 0 THEN
3617                    -- we have met the demand
3618                    --- update the p_atp_record
3619                    --l_ordered_item_id := p_atp_record.inventory_item_id;
3620                    --l_demand_satisfied_by_item_id := l_item_ctp_info.sr_inventory_item_id(i);
3621                    IF PG_DEBUG in ('Y', 'C') THEN
3622                       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Met the requirement using item ' || l_item_ctp_info.item_name(i));
3623                    END IF;
3624                    --bug3467631
3625                    IF l_item_ctp_info.sr_inventory_item_id(i) <> NVL(p_atp_record.request_item_id,
3626                                                     p_atp_record.inventory_item_id) THEN
3627                       IF PG_DEBUG in ('Y', 'C') THEN
3628                          msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'demand is satisfied by substitute');
3629                       END IF;
3630                       --- set the flag
3631                       l_satisfied_by_subst_flag := 1;
3632                    ELSE
3633                       IF PG_DEBUG in ('Y', 'C') THEN
3634                          msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'demand is satisfied by the original item');
3635                       END IF;
3636                       l_satisfied_by_subst_flag := 2;
3637                    END IF;
3638                    IF PG_DEBUG in ('Y', 'C') THEN
3639                       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_satisfied_by_subst_flag := ' || l_satisfied_by_subst_flag);
3640                    END IF;
3641 
3642                    p_atp_record.requested_date_quantity := p_atp_record.quantity_ordered;
3643                    p_atp_record.Atf_Date_Quantity :=
3644                                      l_item_ctp_info.Atf_Date_Quantity(i); --bug3467631
3645                    -- time_phased_atp changes begin
3646                    --   request_item_id will store substitute item id
3647                    --   inventory_item_id will store substitute family id
3648                    --   original item id will store demanded item id
3649                    --bug3709707 atf_date also populated so that correct atf date is returned to schedule
3650                    p_atp_record.atf_date := l_item_ctp_info.atf_date(i);
3651                    p_atp_record.inventory_item_id := l_item_ctp_info.family_sr_id(i);
3652                    p_atp_record.inventory_item_name := l_item_ctp_info.family_item_name(i);
3653                    p_atp_record.request_item_id := l_item_ctp_info.sr_inventory_item_id(i);
3654                    p_atp_record.request_item_name := l_item_ctp_info.item_name(i);
3655                    p_atp_record.original_item_id := item_availability_info.sr_inventory_item_id(l_item_count);
3656                    p_atp_record.original_item_name := item_availability_info.item_name(l_item_count);
3657 
3658                    IF PG_DEBUG in ('Y', 'C') THEN
3659                        msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'inventory_item_id := ' || p_atp_record.inventory_item_id);
3660                        msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'inventory_item_name := ' || p_atp_record.inventory_item_name);
3661                        msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'request_item_id := ' || p_atp_record.request_item_id);
3662                        msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'request_item_name := ' || p_atp_record.request_item_name);
3663                        msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'original_item_id := ' || p_atp_record.original_item_id);
3664                        msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'original_item_name := ' || p_atp_record.original_item_name);
3665                    END IF;
3666                    -- time_phased_atp changes end
3667                    --p_atp_record.End_pegging_id := item_availability_info.End_pegging_id(l_item_cntr);
3668                    --- this variable is populated in end_peggign id for this line in procedure schedule
3669                    MSC_ATP_PVT.G_DEMAND_PEGGING_ID := l_item_ctp_info.End_pegging_id(i);
3670                    l_demand_pegging_id := l_item_ctp_info.End_pegging_id(i);
3671 
3672                    IF NVL(p_atp_record.atp_lead_time, 0) > 0 THEN
3673                      /* ship_rec_cal
3674                      p_atp_record.ship_date := MSC_CALENDAR.DATE_OFFSET
3675                                  (p_atp_record.organization_id,
3676                                   p_atp_record.instance_id,
3677                                   1,
3678                                   l_requested_ship_date,
3679                                   p_atp_record.atp_lead_time);*/
3680                      p_atp_record.ship_date := MSC_CALENDAR.DATE_OFFSET(
3681                                   p_atp_record.manufacturing_cal_code,
3682                                   p_atp_record.instance_id,
3683                                   l_requested_ship_date,
3684                                   p_atp_record.atp_lead_time, 1);
3685                    ELSE
3686                      p_atp_record.ship_date := l_requested_ship_date;
3687                    END IF;
3688                    p_atp_record.available_quantity :=  p_atp_record.quantity_ordered;
3689                    l_count := Item_availability_info.inventory_item_id.count;
3690                    p_atp_record.req_item_req_date_qty := Item_availability_info.request_date_quantity(l_item_count);
3691                    IF l_item_ctp_info.sr_inventory_item_id(i) = MSC_ATP_SUBST.G_REQ_ITEM_SR_INV_ID THEN
3692                        --- request has been fullfilled by original item
3693                        p_atp_record.req_item_req_date_qty :=
3694                                       item_availability_info.request_date_quantity(l_item_count);
3695                        p_atp_record.req_item_available_date := l_requested_ship_date;
3696                        p_atp_record.req_item_available_date_qty
3697                                     :=  item_availability_info.request_date_quantity(l_item_count);
3698                    END IF;
3699 
3700 
3701                    --- now add supply demand/period details to out_put table
3702                    --- first add onhand/sch recp supply demand details
3703                    MSC_ATP_SUBST.Details_Output(l_all_atp_period,
3704                                          l_all_atp_supply_demand,
3705                                          l_item_ctp_info.period_detail_begin_idx(i),
3706                                          l_item_ctp_info.period_detail_end_idx(i),
3707                                          l_item_ctp_info.sd_detail_begin_idx(i),
3708                                          l_item_ctp_info.sd_detail_end_idx(i),
3709                                          x_atp_period,
3710                                          x_atp_supply_demand,
3711                                          l_return_status);
3712                    --- now add supply demand details for CTP
3713                    MSC_ATP_PROC.Details_Output(l_atp_period,
3714                                             l_atp_supply_demand,
3715                                             x_atp_period,
3716                                             x_atp_supply_demand,
3717                                             l_return_status);
3718 
3719                    --- Now remove the pegging/ supply demand for other items
3720                    For j in 1..item_availability_info.inventory_item_id.count LOOP
3721                       IF (item_availability_info.inventory_item_id(j) <> l_item_ctp_info.inventory_item_id(i)) AND
3722                       NOT(l_satisfied_by_subst_flag = 1 and l_org_item_detail_flag = 1 AND
3723                                item_availability_info.sr_inventory_item_id(j) = MSC_ATP_SUBST.G_REQ_ITEM_SR_INV_ID) THEN
3724 
3725                          IF PG_DEBUG in ('Y', 'C') THEN
3726                             msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Remove Pegging for item := ' || item_availability_info.inventory_item_id(i));
3727                          END IF;
3728 
3729                          MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(
3730                                          item_availability_info.End_pegging_id(j),
3731                                           null,
3732                                           item_availability_info.plan_id(j),
3733                                           MSC_ATP_PVT.UNDO,
3734                                           null,
3735                                           l_return_status);
3736 
3737                      END IF;  -- IF item_availability_info.inventory_item_id(j) <> l_item_ctp_info.inventory
3738                    END LOOP; -- For j in 1..item_availability_info.inventory_item_id.count LOOP
3739 
3740                 ELSE --- IF l_net_demand_after_oh_check > 0 THEN
3741 
3742                    --- we had already met the deamand using on hand/sch receipts of substitute item
3743                    -- now we have found enough quantity for ordered item as well so we will update the columns
3744                    IF PG_DEBUG in ('Y', 'C') THEN
3745                       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'demand met by a subst,  provide detail for origninal item');
3746                    END IF;
3747                    MSC_ATP_PVT.G_DEMAND_PEGGING_ID := l_demand_pegging_id;
3748                    p_atp_record.req_item_req_date_qty := p_atp_record.quantity_ordered;
3749 
3750                    IF NVL(p_atp_record.atp_lead_time, 0) > 0 THEN
3751                      /* ship_rec_cal
3752                      p_atp_record.req_item_available_date := MSC_CALENDAR.DATE_OFFSET
3753                                  (p_atp_record.organization_id,
3754                                   p_atp_record.instance_id,
3755                                   1,
3756                                   l_requested_ship_date,
3757                                   p_atp_record.atp_lead_time);*/
3758                      p_atp_record.req_item_available_date := MSC_CALENDAR.DATE_OFFSET(
3759                                   p_atp_record.manufacturing_cal_code,
3760                                   p_atp_record.instance_id,
3761                                   l_requested_ship_date,
3762                                   p_atp_record.atp_lead_time, 1);
3763                    ELSE
3764                      p_atp_record.req_item_available_date := l_requested_ship_date;
3765                    END IF;
3766 
3767                    p_atp_record.req_item_available_date_qty := p_atp_record.quantity_ordered;
3768                    -- time_phased_atp
3769                    --p_atp_record.request_item_name := l_item_ctp_info.item_name(i);
3770                    --bug3467631 start added so that correct names are shown
3771                    --p_atp_record.original_item_name := l_item_ctp_info.item_name(i);
3772                    p_atp_record.requested_date_quantity := p_atp_record.quantity_ordered;
3773                    p_atp_record.Atf_Date_Quantity :=
3774                                      l_item_ctp_info.Atf_Date_Quantity(i);
3775                    --bug3709707 no need to populate names here
3776                    /*
3777                    p_atp_record.inventory_item_id := l_item_ctp_info.family_sr_id(i);
3778                    p_atp_record.inventory_item_name := l_item_ctp_info.family_item_name(i);
3779                    p_atp_record.request_item_id := l_item_ctp_info.sr_inventory_item_id(i);
3780                    p_atp_record.request_item_name := l_item_ctp_info.item_name(i);
3781                    p_atp_record.original_item_id := item_availability_info.sr_inventory_item_id(l_item_count);
3782                    p_atp_record.original_item_name := item_availability_info.item_name(l_item_count);
3783                    */
3784                    --bug3467631 end
3785                    l_satisfied_by_subst_flag := 2;
3786                    --- remove peggin and supply demand details
3787 
3788 
3789                    MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(
3790                                          item_availability_info.End_pegging_id(l_item_count),
3791                                           null,
3792                                           item_availability_info.plan_id(l_item_count),
3793                                           MSC_ATP_PVT.UNDO,
3794                                           null,
3795                                           l_return_status);
3796                 END IF;
3797 
3798 
3799              --ELSE --Bug 3878343 , donot call Details_Output() in case l_atp_period is NULL.
3800              ELSIF ( l_atp_period.Level IS NOT NULL AND
3801                      l_atp_period.Level.COUNT > 0 ) THEN
3802 
3803                 --- add l_atp_period and l_atp_supply_demand
3804                 l_period_begin_idx := l_all_atp_period.level.COUNT;
3805                 l_sd_begin_idx := l_all_atp_supply_demand.level.count;
3806 
3807                 IF l_period_begin_idx = 0 THEN
3808                    l_period_begin_idx := 1;
3809                 ELSE
3810                    l_period_begin_idx := l_period_begin_idx +1;
3811                 END IF;
3812 
3813                 IF l_sd_begin_idx = 0 THEN
3814                    l_sd_begin_idx := 1;
3815                 ELSE
3816                    l_sd_begin_idx := l_sd_begin_idx + 1;
3817                 END IF;
3818 
3819                 --- now add period and supply details
3820                 MSC_ATP_PROC.Details_Output(l_atp_period,
3821                                             l_atp_supply_demand,
3822                                             l_all_atp_period,
3823                                             l_all_atp_supply_demand,
3824                                             l_return_status);
3825                 l_period_end_idx := l_all_atp_period.level.count;
3826                 l_sd_end_idx := l_all_atp_supply_demand.level.count;
3827 
3828                 item_availability_info.ctp_prd_detl_begin_idx(l_item_idx) := l_period_begin_idx;
3829                 item_availability_info.ctp_prd_detl_end_idx(l_item_idx) := l_period_end_idx;
3830                 item_availability_info.ctp_sd_detl_begin_idx(l_item_idx) := l_sd_begin_idx;
3831                 item_availability_info.ctp_sd_detl_end_idx(l_item_idx) := l_sd_end_idx;
3832 
3833 
3834 
3835              END IF;
3836 
3837 
3838          END LOOP;
3839       END IF;
3840 
3841       IF PG_DEBUG in ('Y', 'C') THEN
3842          msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'net demand after backward CTP := ' || l_net_demand);
3843       END IF;
3844       IF PG_DEBUG in ('Y', 'C') THEN
3845          FOR i in reverse 1..item_availability_info.inventory_item_id.count LOOP
3846              msc_sch_wb.atp_debug('ATP_Check_Subst: ' || item_availability_info.item_name(i) || ' ' ||
3847                                item_availability_info.sr_inventory_item_id(i) || ' ' ||
3848                                item_availability_info.request_date_quantity(i));
3849          END LOOP;
3850       END IF;
3851 
3852       IF PG_DEBUG in ('Y', 'C') THEN
3853          msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'p_atp_record.inventory_item_name: ' || p_atp_record.inventory_item_name );
3854       END IF;
3855 
3856       -- dsting skip Forward scheduling for diagnostic ATP
3857       IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = DIAGNOSTIC_ATP THEN
3858 	IF PG_DEBUG in ('Y', 'C') THEN
3859 	   msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'no forward pass for diagnostic ATP');
3860 	END IF;
3861 
3862 	IF l_net_demand > 0 THEN
3863            /* time_phased_atp changes begin
3864               request_item_id will store substitute item id
3865               inventory_item_id will store substitute family id
3866               original item id will store demanded item id*/
3867            --bug3709707 atf_date also populated so that correct atf date is returned to schedule
3868            p_atp_record.atf_date := item_availability_info.atf_date(l_item_count);
3869            p_atp_record.inventory_item_id := item_availability_info.family_sr_id(l_item_count);
3870            p_atp_record.inventory_item_name := item_availability_info.family_item_name(l_item_count);
3871            p_atp_record.request_item_id := item_availability_info.sr_inventory_item_id(l_item_count);
3872            p_atp_record.request_item_name := item_availability_info.item_name(l_item_count);
3873            p_atp_record.original_item_id := item_availability_info.sr_inventory_item_id(l_item_count);
3874            p_atp_record.original_item_name := item_availability_info.item_name(l_item_count);
3875            -- time_phased_atp changes end
3876 
3877          IF PG_DEBUG in ('Y', 'C') THEN
3878            msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'inventory_item_id := ' || p_atp_record.inventory_item_id);
3879            msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'inventory_item_name := ' || p_atp_record.inventory_item_name);
3880            msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'request_item_id := ' || p_atp_record.request_item_id);
3881            msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'request_item_name := ' || p_atp_record.request_item_name);
3882            msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'original_item_id := ' || p_atp_record.original_item_id);
3883            msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'original_item_name := ' || p_atp_record.original_item_name);
3884          END IF;
3885            p_atp_record.atf_date_quantity := item_availability_info.atf_date_quantity(l_item_count);
3886            p_atp_record.requested_date_quantity := item_availability_info.request_date_quantity(l_item_count);
3887            p_atp_record.available_quantity :=  item_availability_info.request_date_quantity(l_item_count);
3888            p_atp_record.req_item_req_date_qty := item_availability_info.request_date_quantity(l_item_count);
3889 
3890 	END IF;
3891 
3892 	goto CLEANUP;
3893       END IF;
3894 
3895       IF PG_DEBUG in ('Y', 'C') THEN
3896          msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Do forward scheduling if needed. net demand: ' || l_net_demand);
3897       END IF;
3898 
3899       --- we are done with backward CTP. Now do Forward Schedulling if needed
3900       IF ((l_org_item_detail_flag = 1 AND l_satisfied_by_subst_flag = 1)) or (l_net_demand > 0) THEN
3901          IF (l_org_item_detail_flag = 1 AND l_satisfied_by_subst_flag = 1) THEN
3902             l_insert_flag := 0;
3903          ELSE
3904             l_insert_flag := p_atp_record.insert_flag;
3905          END IF;
3906          IF PG_DEBUG in ('Y', 'C') THEN
3907             msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Inside Forward Scheduling');
3908             msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'item count := ' || item_availability_info.inventory_item_id.count);
3909          END IF;
3910          FOR i in reverse 1..item_availability_info.inventory_item_id.count LOOP
3911             --- look for date by which we can meet by onhand
3912             l_net_demand := p_atp_record.quantity_ordered - item_availability_info.request_date_quantity(i);
3913             IF PG_DEBUG in ('Y', 'C') THEN
3914                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Get future onhand date for item := ' ||
3915                                   item_availability_info.item_name(i));
3916                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Net demand future case := ' || l_net_demand);
3917             END IF;
3918 
3919              -- ALLOC ATP CHANGES, 12973673
3920 			IF (MSC_ATP_PVT.G_INV_CTP = 4 AND MSC_ATP_PVT.G_HYBRID_ALLOC_ATP = 'Y') THEN
3921 				IF PG_DEBUG in ('Y', 'C') THEN
3922                     msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'G_HYBRID_ALLOC_ATP is Y');
3923                 END IF;
3924 
3925 				MSC_ATP_PVT.G_ALLOCATED_ATP := MSC_ATP_FUNC.check_alloc_exists(p_atp_record.instance_id,
3926 								-1, --PLAN_ID
3927 								item_availability_info.sr_inventory_item_id(i),
3928 								p_atp_record.organization_id,
3929 								nvl(l_requested_ship_date,trunc(sysdate)));
3930 
3931 				IF PG_DEBUG in ('Y', 'C') THEN
3932                     msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'MSC_ATP_PVT.G_ALLOCATED_ATP = '||MSC_ATP_PVT.G_ALLOCATED_ATP);
3933                 END IF;
3934 			END IF;
3935 
3936             --- first update the demand so that the picture gets adjusted
3937             MSC_ATP_SUBST.UPDATE_DEMAND(item_availability_info.demand_id(i),
3938                                         item_availability_info.plan_id(i),
3939                                         item_availability_info.request_date_quantity(i));
3940 
3941             /* time_phased_atp */
3942             IF ((item_availability_info.sr_inventory_item_id(i) <>
3943                              item_availability_info.family_sr_id(i)) AND
3944                                item_availability_info.atf_date(i) is not null) THEN
3945 	          --using the same insert rec we prepared earlier
3946 	          l_atp_insert_rec.quantity_ordered :=  item_availability_info.request_date_quantity(i);
3947 	          l_atp_insert_rec.requested_date_quantity := item_availability_info.request_date_quantity(i);
3948 	          l_atp_insert_rec.atf_date_quantity := item_availability_info.atf_date_quantity(i);
3949 	          --bug3467631 Inside Forward Scheduling re-initializing otherwise l_atp_insert_rec.inventory_item_id,
3950 	          --l_atp_insert_rec.request_item_id,l_atp_insert_rec.atf_date will have substitute's values
3951 	          --even when we are processing member item.
3952 	          l_atp_insert_rec.inventory_item_id := item_availability_info.family_dest_id(i); --bug3467631
3953                   l_atp_insert_rec.request_item_id := item_availability_info.inventory_item_id(i); --bug3467631
3954                   l_atp_insert_rec.atf_date := item_availability_info.atf_date(i); --bug3467631
3955 	          MSC_ATP_PF.Increment_Bucketed_Demands_Qty(
3956                         l_atp_insert_rec,
3957                         item_availability_info.plan_id(i),
3958                         item_availability_info.demand_id(i),
3959                         l_return_status
3960 	          );
3961                   IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
3962                      IF PG_DEBUG in ('Y', 'C') THEN
3963                         msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Error occured in procedure Increment_Bucketed_Demands_Qty');
3964                      END IF;
3965                      RAISE FND_API.G_EXC_ERROR;
3966                   END IF;
3967             END IF;
3968             -- time_phased_atp changes end
3969 
3970             IF PG_DEBUG in ('Y', 'C') THEN
3971                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Now get the future date');
3972                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || item_availability_info.atp_flag(i));
3973                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'atp_flag := ' || item_availability_info.atp_flag(i));
3974                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'atp_flag := ' || item_availability_info.atp_comp_flag(i));
3975                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_sys_date_top_org := ' || l_sys_date_top_org);
3976             END IF;
3977             IF item_availability_info.atp_flag(i) = 'N' and item_availability_info.atp_comp_flag(i) = 'N' THEN
3978                --- we will come here only if item if req_date is passed due
3979                l_atp_date_quantity_this_level := p_atp_record.quantity_ordered;
3980                l_atp_date_this_level := l_sys_date_top_org;
3981                l_requested_date_quantity := 0;
3982                item_availability_info.future_atp_date(i) :=l_atp_date_this_level;
3983                item_availability_info.atp_date_quantity(i) := l_atp_date_quantity_this_level;
3984 
3985             ELSIF item_availability_info.atp_flag(i) in ('Y', 'C') THEN
3986 	       l_get_mat_in_rec.rounding_control_flag := item_availability_info.rounding_control_type(i);
3987 	       l_get_mat_in_rec.dest_inv_item_id := item_availability_info.inventory_item_id(i);
3988                l_get_mat_in_rec.plan_name := item_availability_info.plan_name(i);
3989                --bug3700564 passed shipping cal code as a part of this fix.
3990                l_get_mat_in_rec.shipping_cal_code := p_atp_record.shipping_cal_code;
3991                l_get_mat_in_rec.receiving_cal_code := p_atp_record.receiving_cal_code; -- Bug 3826234
3992                l_get_mat_in_rec.intransit_cal_code := p_atp_record.intransit_cal_code; -- Bug 3826234
3993                l_get_mat_in_rec.manufacturing_cal_code := p_atp_record.manufacturing_cal_code; -- Bug 3826234
3994                l_get_mat_in_rec.to_organization_id := p_atp_record.to_organization_id; -- Bug 3826234
3995 
3996                /* To support new logic for dependent demands allocation in time phased PF rule based AATP scenarios
3997                   Set global variable. This is used in Get_Item_Demand_Alloc_Percent function*/
3998                IF item_availability_info.atf_date(i) is not null THEN
3999                       /* Set global variable. This is used in Get_Item_Demand_Alloc_Percent function*/
4000                    MSC_ATP_PVT.G_TIME_PHASED_PF_ENABLED := 'Y';
4001                ELSE
4002                    MSC_ATP_PVT.G_TIME_PHASED_PF_ENABLED := 'N';
4003                END IF;
4004 
4005                IF (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
4006                     (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
4007                     (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1) THEN
4008 
4009                   IF PG_DEBUG in ('Y', 'C') THEN
4010                      msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Allocated ATP with demand priority');
4011                   END IF;
4012                   ---forward steal
4013                   MSC_ATP_PVT.G_DEMAND_PEGGING_ID :=  item_availability_info.end_pegging_id(i);
4014 
4015                   -- dsting setting global item_attributes so get_forward_material_atp populates the
4016                   -- item attributes in pegging. Since G_ITEM_INFO_REC may not be the
4017                   -- item we are processing (ie a substitute)
4018                   MSC_ATP_PVT.G_ITEM_INFO_REC.product_family_id := item_availability_info.family_dest_id(i); --bug3467631
4019 		  MSC_ATP_PVT.G_ITEM_INFO_REC.dest_inv_item_id := item_availability_info.inventory_item_id(i);	   --6719242
4020 		  MSC_ATP_PVT.G_ITEM_INFO_REC.item_name := item_availability_info.item_name(i);		           --6719242
4021                   MSC_ATP_PVT.G_ITEM_INFO_REC.inventory_item_id := item_availability_info.inventory_item_id(i);    --6719242
4022                   MSC_ATP_PVT.G_ITEM_INFO_REC.sr_inv_item_id    := item_availability_info.sr_inventory_item_id(i); --6719242
4023                   MSC_ATP_PVT.G_ITEM_INFO_REC.instance_id       := p_atp_record.instance_id;                       --6719242
4024                   MSC_ATP_PVT.G_ITEM_INFO_REC.organization_id   := p_atp_record.organization_id;                   --6719242
4025                   MSC_ATP_PVT.G_ITEM_INFO_REC.plan_id           := item_availability_info.plan_id(i);              --6719242
4026                   MSC_ATP_PVT.G_ITEM_INFO_REC.atp_flag := item_availability_info.atp_flag(i);
4027                   MSC_ATP_PVT.G_ITEM_INFO_REC.atp_comp_flag := item_availability_info.atp_comp_flag(i);
4028                   MSC_ATP_PVT.G_ITEM_INFO_REC.pre_pro_lt := item_availability_info.pre_pro_lt(i);
4029                   MSC_ATP_PVT.G_ITEM_INFO_REC.post_pro_lt := item_availability_info.post_pro_lt(i);
4030                   MSC_ATP_PVT.G_ITEM_INFO_REC.fixed_lt := item_availability_info.fixed_lt(i);
4031                   MSC_ATP_PVT.G_ITEM_INFO_REC.variable_lt := item_availability_info.variable_lt(i);
4032                   MSC_ATP_PVT.G_ITEM_INFO_REC.unit_weight := item_availability_info.unit_weight(i);
4033                   MSC_ATP_PVT.G_ITEM_INFO_REC.unit_volume := item_availability_info.unit_volume(i);
4034                   MSC_ATP_PVT.G_ITEM_INFO_REC.weight_uom := item_availability_info.weight_uom(i);
4035                   MSC_ATP_PVT.G_ITEM_INFO_REC.volume_uom := item_availability_info.volume_uom(i);
4036                   MSC_ATP_PVT.G_ITEM_INFO_REC.substitution_window := item_availability_info.substitution_window(i);
4037                   MSC_ATP_PVT.G_ITEM_INFO_REC.organization_id := NULL;
4038 
4039                   MSC_AATP_REQ.Get_Forward_Material_Atp(
4040                                 p_atp_record.instance_id,
4041                                 item_availability_info.plan_id(i),
4042                                 p_level + 1,
4043                                 p_atp_record.identifier,
4044                                 p_atp_record.demand_source_type,--cmro
4045                                 p_scenario_id,
4046                                 item_availability_info.sr_inventory_item_id(i),
4047                                 item_availability_info.family_sr_id(i), -- For time_phased_atp
4048                                 p_atp_record.organization_id,
4049                                 item_availability_info.item_name(i),
4050                                 item_availability_info.family_item_name(i), -- For time_phased_atp
4051                                 l_requested_ship_date,
4052                                 l_net_demand,
4053                                 item_availability_info.demand_class(i),
4054                                 l_requested_date_quantity,
4055                                 l_atf_date_qty, -- For time_phased_atp
4056                                 l_atp_date_this_level,
4057                                 l_atp_date_quantity_this_level,
4058                                 l_atp_pegging_tab,
4059                                 l_return_status,
4060                                 l_used_available_quantity,--bug3409973
4061                                 item_availability_info.substitution_window(i),
4062 				l_get_mat_in_rec,
4063 				l_get_mat_out_rec,
4064 				item_availability_info.atf_date(i) -- For time_phased_atp
4065 		  );
4066 
4067                   item_availability_info.fwd_steal_peg_begin_idx(i) := l_fwd_atp_pegging_tab.count +1;
4068                   IF PG_DEBUG in ('Y', 'C') THEN
4069                      msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Fwd peg count for curr item := ' || l_atp_pegging_tab.count);
4070                   END IF;
4071                   FOR j in 1..l_atp_pegging_tab.count LOOP
4072                       l_fwd_atp_pegging_tab.extend;
4073                       l_fwd_atp_pegging_tab(l_fwd_atp_pegging_tab.count) := l_atp_pegging_tab(j);
4074 
4075                   END LOOP;
4076                   item_availability_info.fwd_steal_peg_end_idx(i) := l_fwd_atp_pegging_tab.count;
4077                   IF PG_DEBUG in ('Y', 'C') THEN
4078                      msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'fwd peg count := ' || l_fwd_atp_pegging_tab.count);
4079                      msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_atp_date_this_level := ' || l_atp_date_this_level);
4080                      msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_atp_date_quantity_this_level := ' || l_atp_date_quantity_this_level);
4081                      msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_used_available_quantity := ' || l_used_available_quantity); --bug3409973
4082                      msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_atf_date_qty := ' || l_atf_date_qty); --bug3467631
4083                   END IF;
4084                ELSE
4085                   IF PG_DEBUG in ('Y', 'C') THEN
4086                      msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'ATP without demand priority allocated ATP');
4087                   END IF;
4088                   --bug3467631 Inside Forward Scheduling re-initializing MSC_ATP_PVT.G_ITEM_INFO_REC
4089                   --otherwise it will have substitute's values even when we are processing member item.
4090                   --bug3467631 start
4091                   MSC_ATP_PVT.G_ITEM_INFO_REC.product_family_id := item_availability_info.family_dest_id(i);
4092                   MSC_ATP_PVT.G_ITEM_INFO_REC.inventory_item_id := item_availability_info.inventory_item_id(i);
4093 		  MSC_ATP_PVT.G_ITEM_INFO_REC.dest_inv_item_id := item_availability_info.inventory_item_id(i);     --6719242
4094  		  MSC_ATP_PVT.G_ITEM_INFO_REC.item_name := item_availability_info.item_name(i);			   --6719242
4095                   MSC_ATP_PVT.G_ITEM_INFO_REC.sr_inv_item_id    := item_availability_info.sr_inventory_item_id(i); --6719242
4096                   MSC_ATP_PVT.G_ITEM_INFO_REC.instance_id       := p_atp_record.instance_id;                       --6719242
4097                   MSC_ATP_PVT.G_ITEM_INFO_REC.organization_id   := p_atp_record.organization_id;                   --6719242
4098                   MSC_ATP_PVT.G_ITEM_INFO_REC.plan_id           := item_availability_info.plan_id(i);              --6719242
4099                   MSC_ATP_PVT.G_ITEM_INFO_REC.atp_flag := item_availability_info.atp_flag(i);
4100                   MSC_ATP_PVT.G_ITEM_INFO_REC.atp_comp_flag := item_availability_info.atp_comp_flag(i);
4101                   MSC_ATP_PVT.G_ITEM_INFO_REC.pre_pro_lt := item_availability_info.pre_pro_lt(i);
4102                   MSC_ATP_PVT.G_ITEM_INFO_REC.post_pro_lt := item_availability_info.post_pro_lt(i);
4103                   MSC_ATP_PVT.G_ITEM_INFO_REC.fixed_lt := item_availability_info.fixed_lt(i);
4104                   MSC_ATP_PVT.G_ITEM_INFO_REC.variable_lt := item_availability_info.variable_lt(i);
4105                   MSC_ATP_PVT.G_ITEM_INFO_REC.unit_weight := item_availability_info.unit_weight(i);
4106                   MSC_ATP_PVT.G_ITEM_INFO_REC.unit_volume := item_availability_info.unit_volume(i);
4107                   MSC_ATP_PVT.G_ITEM_INFO_REC.weight_uom := item_availability_info.weight_uom(i);
4108                   MSC_ATP_PVT.G_ITEM_INFO_REC.volume_uom := item_availability_info.volume_uom(i);
4109                   MSC_ATP_PVT.G_ITEM_INFO_REC.substitution_window := item_availability_info.substitution_window(i);
4110                   --bug3467631 end
4111                   -- time_phased_atp changes begin
4112                   l_mat_atp_info_rec.instance_id               := p_atp_record.instance_id;
4113                   l_mat_atp_info_rec.plan_id                   := item_availability_info.plan_id(i);
4114                   l_mat_atp_info_rec.level                     := p_level;
4115                   l_mat_atp_info_rec.identifier                := p_atp_record.identifier;
4116                   l_mat_atp_info_rec.scenario_id               := p_scenario_id;
4117                   --bug3467631 start inventory_item_id => family id and request_item_id =>member id
4118                   --l_mat_atp_info_rec.request_item_id           := item_availability_info.sr_inventory_item_id(i);
4119                   --l_mat_atp_info_rec.inventory_item_id         := item_availability_info.family_sr_id(i);
4120                   l_mat_atp_info_rec.inventory_item_id         := item_availability_info.family_sr_id(i);
4121                   l_mat_atp_info_rec.request_item_id           := item_availability_info.sr_inventory_item_id(i);
4122                   --bug3467631 end
4123                   l_mat_atp_info_rec.organization_id           := p_atp_record.organization_id;
4124                   l_mat_atp_info_rec.requested_date            := l_requested_ship_date;
4125                   l_mat_atp_info_rec.quantity_ordered          := l_net_demand;
4126                   l_mat_atp_info_rec.demand_class              := item_availability_info.demand_class(i);
4127                   l_mat_atp_info_rec.insert_flag               := l_insert_flag;
4128                   l_mat_atp_info_rec.rounding_control_flag     := l_get_mat_in_rec.rounding_control_flag;
4129                   l_mat_atp_info_rec.dest_inv_item_id          := l_get_mat_in_rec.dest_inv_item_id;
4130                   l_mat_atp_info_rec.infinite_time_fence_date  := l_get_mat_in_rec.infinite_time_fence_date;
4131                   l_mat_atp_info_rec.plan_name                 := l_get_mat_in_rec.plan_name;
4132                   l_mat_atp_info_rec.optimized_plan            := l_get_mat_in_rec.optimized_plan;
4133                   l_mat_atp_info_rec.substitution_window       := item_availability_info.substitution_window(i);
4134                   l_mat_atp_info_rec.refresh_number            := p_refresh_number;
4135                   l_mat_atp_info_rec.shipping_cal_code         := p_atp_record.shipping_cal_code; -- Bug 3371817
4136                   l_mat_atp_info_rec.atf_date                  := item_availability_info.atf_date(i);
4137 
4138                   MSC_ATP_REQ.Get_Material_Atp_Info(
4139                           l_mat_atp_info_rec,
4140                           l_atp_period,
4141                           l_atp_supply_demand,
4142                           l_return_status);
4143 
4144                   l_requested_date_quantity                    := l_mat_atp_info_rec.requested_date_quantity;
4145                   l_atf_date_qty                               := l_mat_atp_info_rec.atf_date_quantity;
4146                   l_atp_date_this_level                        := l_mat_atp_info_rec.atp_date_this_level;
4147                   l_atp_date_quantity_this_level               := l_mat_atp_info_rec.atp_date_quantity_this_level;
4148                   l_get_mat_out_rec.atp_rule_name              := l_mat_atp_info_rec.atp_rule_name;
4149                   l_get_mat_out_rec.infinite_time_fence_date   := l_mat_atp_info_rec.infinite_time_fence_date;
4150                   l_used_available_quantity                    := l_mat_atp_info_rec.atp_date_quantity_this_level; --bug3409973
4151                   -- time_phased_atp changes end
4152 
4153                   -- dsting: l_period_begin_idx := l_all_atp_supply_demand.level.COUNT;
4154                   l_period_begin_idx := l_all_atp_period.level.COUNT;
4155                   l_sd_begin_idx := l_all_atp_supply_demand.level.count;
4156 
4157                   IF l_period_begin_idx = 0 THEN
4158                      l_period_begin_idx := 1;
4159                   ELSE
4160                      l_period_begin_idx := l_period_begin_idx + 1;
4161                   END IF;
4162 
4163                   IF l_sd_begin_idx = 0 THEN
4164 
4165                      l_sd_begin_idx := 1;
4166                   ELSE
4167                      l_sd_begin_idx := l_sd_begin_idx + 1;
4168                   END IF;
4169 
4170                   ---- get pegging id for demand
4171                   SELECT msc_full_pegging_s.nextval
4172                   INTO   l_pegging_id
4173                   FROM   dual;
4174                   IF PG_DEBUG in ('Y', 'C') THEN
4175                      msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Update with pegging info');
4176                   END IF;
4177                   item_availability_info.future_supply_peg_id(i) := l_pegging_id;
4178                   FOR j in 1..l_atp_period.Level.COUNT LOOP
4179                      l_atp_period.Pegging_Id(j) := l_pegging_id;
4180                      l_atp_period.End_Pegging_Id(j) := item_availability_info.end_pegging_id(i);
4181                   END LOOP;
4182 
4183                   FOR j in 1..l_atp_supply_demand.Level.COUNT LOOP
4184                      l_atp_supply_demand.Pegging_Id(j) := l_pegging_id;
4185                      l_atp_supply_demand.End_Pegging_Id(j) := item_availability_info.end_pegging_id(i);
4186                   END LOOP;
4187 
4188 
4189                   --- now add period and supply details
4190                   IF PG_DEBUG in ('Y', 'C') THEN
4191                      msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Add Supply demand details for the item');
4192                   END IF;
4193                   MSC_ATP_PROC.Details_Output(l_atp_period,
4194                                               l_atp_supply_demand,
4195                                               l_all_atp_period,
4196                                               l_all_atp_supply_demand,
4197                                                l_return_status);
4198 
4199                   l_period_end_idx := l_all_atp_period.level.count;
4200                   l_sd_end_idx := l_all_atp_supply_demand.level.count;
4201 
4202                   item_availability_info.fut_atp_prd_detl_begin_idx(i) := l_period_begin_idx;
4203                   item_availability_info.fut_atp_prd_detl_end_idx(i) := l_period_end_idx;
4204                   item_availability_info.fut_atp_sd_detl_begin_idx(i) := l_sd_begin_idx;
4205                   item_availability_info.fut_atp_sd_detl_end_idx(i) := l_sd_end_idx;
4206                   IF PG_DEBUG in ('Y', 'C') THEN
4207                      msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_used_available_quantity := ' || l_used_available_quantity); --bug3467631
4208                      msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_atp_date_this_level := ' || l_atp_date_this_level);
4209                      msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_atp_date_quantity_this_level := ' || l_atp_date_quantity_this_level);
4210                      msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_atf_date_qty := ' || l_atf_date_qty); --bug3467631
4211                   END IF;
4212                END IF;  --- IF (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
4213                --bug3467631 start
4214                --added to set atf_date_quantity for item
4215                IF l_mat_atp_info_rec.requested_date > l_mat_atp_info_rec.atf_date THEN
4216                          item_availability_info.atf_date_quantity(i) :=
4217                                  GREATEST(NVL(item_availability_info.atf_date_quantity(i), 0), 0) +
4218                                  GREATEST(NVL(l_atf_date_qty, 0), 0);
4219                ELSE
4220                          item_availability_info.atf_date_quantity(i) :=
4221                                  GREATEST(NVL(item_availability_info.request_date_quantity(i), 0), 0) +
4222                                  GREATEST(NVL(l_atf_date_qty, 0), 0);
4223                END IF;
4224                --bug3467631 end
4225                item_availability_info.future_atp_date(i) :=l_atp_date_this_level;
4226                item_availability_info.atp_date_quantity(i) :=
4227                                      l_atp_date_quantity_this_level -
4228                                         GREATEST(l_requested_date_quantity, 0);
4229                -- time_phased_atp
4230                item_availability_info.used_available_quantity(i) :=
4231                                      LEAST(GREATEST(l_used_available_quantity, 0) +
4232                                            GREATEST(item_availability_info.request_date_quantity(i),0),
4233                                            MSC_ATP_PVT.INFINITE_NUMBER);--bug3467631
4234                IF PG_DEBUG in ('Y', 'C') THEN --bug3467631
4235                      msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'item_availability_info.atf_date_quantity(i) := ' ||
4236                                            item_availability_info.atf_date_quantity(i)); --bug3467631
4237                      msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'item_availability_info.used_available_quantity(i) := ' ||
4238                                            item_availability_info.used_available_quantity(i));
4239                END IF;
4240                IF (l_org_item_detail_flag = 1 AND l_satisfied_by_subst_flag = 1 AND  i = l_item_count) THEN
4241                     IF PG_DEBUG in ('Y', 'C') THEN
4242                        msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'we have met the demand, we just check for avail of req item');
4243                     END IF;
4244                     EXIT;
4245                END IF;
4246             ELSE
4247                l_atp_date_quantity_this_level := 0;
4248                l_atp_date_this_level := null;
4249                l_requested_date_quantity := 0;
4250                l_atf_date_qty := 0; -- time_phased_atp
4251             END IF;
4252 
4253          END LOOP;
4254          IF PG_DEBUG in ('Y', 'C') THEN
4255             msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Now do Forward CTP');
4256          END IF;
4257          IF (l_org_item_detail_flag = 1 AND l_satisfied_by_subst_flag = 1
4258                         and l_net_demand_after_oh_check > 0)  THEN
4259              IF PG_DEBUG in ('Y', 'C') THEN
4260                 msc_sch_wb.atp_debug('ATP_Check_Subst: ' || ' Demand was met by a CTP on sub. reset the l_item_ctp_info table');
4261              END IF;
4262              l_item_ctp_info := l_null_item_avail_info;
4263              IF (MSC_ATP_SUBST.G_CREATE_SUPPLY_FLAG <> G_HIGHEST_REV_ITEM) or
4264                 (MSC_ATP_SUBST.G_CREATE_SUPPLY_FLAG = G_HIGHEST_REV_ITEM and
4265                    l_highest_rev_item_id = item_availability_info.inventory_item_id(l_item_count)) THEN
4266 
4267                 MSC_ATP_SUBST.Copy_Item_Info_Rec(item_availability_info, l_item_ctp_info, l_item_count);
4268              END IF;
4269          END IF;
4270          FOR i in reverse 1..l_item_ctp_info.inventory_item_id.count LOOP
4271 
4272             l_net_demand := p_atp_record.quantity_ordered - l_item_ctp_info.request_date_quantity(i);
4273             IF PG_DEBUG in ('Y', 'C') THEN
4274                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_item_ctp_info.request_date_quantity(i) := ' || l_item_ctp_info.request_date_quantity(i));
4275                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_net_demand forward case := ' || l_net_demand);
4276                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Future atp date :=' || item_availability_info.future_atp_date(i));
4277             END IF;
4278 
4279 			-- ALLOC ATP CHANGES, 12973673
4280 			IF (MSC_ATP_PVT.G_INV_CTP = 4 AND MSC_ATP_PVT.G_HYBRID_ALLOC_ATP = 'Y') THEN
4281 				IF PG_DEBUG in ('Y', 'C') THEN
4282                     msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'G_HYBRID_ALLOC_ATP is Y');
4283                 END IF;
4284 
4285 				MSC_ATP_PVT.G_ALLOCATED_ATP := MSC_ATP_FUNC.check_alloc_exists(p_atp_record.instance_id,
4286 								-1, --PLAN_ID
4287 								l_item_ctp_info.sr_inventory_item_id(i),
4288 								p_atp_record.organization_id,
4289 								nvl(l_requested_ship_date,trunc(sysdate)));
4290 
4291 				IF PG_DEBUG in ('Y', 'C') THEN
4292                     msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'MSC_ATP_PVT.G_ALLOCATED_ATP = '||MSC_ATP_PVT.G_ALLOCATED_ATP);
4293                 END IF;
4294 			END IF;
4295 
4296             IF l_item_ctp_info.atp_comp_flag(i) in ('Y', 'C', 'R') and l_item_ctp_info.create_supply_flag(i) = 1 THEN
4297                 IF l_item_count = 1 THEN
4298                    l_item_idx := 1;
4299                 ELSE
4300                    IF (l_org_item_detail_flag = 1 AND l_satisfied_by_subst_flag = 1) THEN
4301                       l_item_idx := l_item_count;
4302 
4303                    ELSIF MSC_ATP_SUBST.G_CREATE_SUPPLY_FLAG = G_DEMANDED_ITEM THEN
4304 
4305                       l_item_idx := l_item_count;
4306                    ELSIF MSC_ATP_SUBST.G_CREATE_SUPPLY_FLAG = G_HIGHEST_REV_ITEM THEN
4307 
4308                       l_item_idx := l_highest_rev_item_index;
4309                    ELSE
4310 
4311                       IF l_item_ctp_info.inventory_item_id.count = 1 THEN
4312                          --- this case will be true only if we have already satisfied the demand
4313                          --- using a substitute
4314                          l_item_idx := l_item_count;
4315                       ELSE
4316                          l_item_idx := i;
4317                       END IF;
4318                    END IF;
4319                 END IF;
4320                 IF PG_DEBUG in ('Y', 'C') THEN
4321                    msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_item_idx := ' || l_item_idx);
4322                 END IF;
4323                 l_atp_rec.error_code := 0;
4324                 l_atp_rec.available_quantity := NULL;
4325                 l_atp_rec.requested_date_quantity := NULL;
4326 
4327                 -- no need to do uom conversion
4328                 l_atp_rec.instance_id := p_atp_record.instance_id;
4329                 -- what do we need to do with thie ???
4330                 l_atp_rec.demand_source_line := null;
4331                 l_atp_rec.identifier := MSC_ATP_PVT.G_ORDER_LINE_ID;
4332                 --- what do we do with this??
4333                 l_atp_rec.component_identifier := null; -- l_comp_requirements.component_identifier(j);
4334 
4335                 -- time_phased_atp
4336                 l_atp_rec.inventory_item_id := l_item_ctp_info.family_sr_id(i);
4337 
4338                 l_atp_rec.request_item_id := l_item_ctp_info.sr_inventory_item_id(i);
4339                 l_atp_rec.organization_id := p_atp_record.organization_id;
4340                 l_atp_rec.quantity_ordered := l_net_demand;
4341                 --bug3467631 setting atf_date
4342                 l_atp_rec.Atf_Date := l_item_ctp_info.Atf_Date(i); --bug3467631
4343                 -- l_atp_rec.quantity_uom := l_quantity_uom;
4344                 l_atp_rec.requested_ship_date := l_requested_ship_date;
4345                 l_atp_rec.demand_class := l_item_ctp_info.demand_class(i);
4346                 l_atp_rec.insert_flag := l_insert_flag;
4347                 l_atp_rec.refresh_number := p_refresh_number;
4348                 l_atp_rec.refresh_number := null;
4349 	        l_atp_rec.ship_date := null;
4350 
4351 
4352                 l_plan_id :=  l_item_ctp_info.plan_id(i);
4353                 l_assign_set_id := l_item_ctp_info.assign_set_id(i);
4354 
4355                 l_item_attribute_rec.atp_flag := l_item_ctp_info.atp_flag(i);
4356                 l_item_attribute_rec.atp_comp_flag := l_item_ctp_info.atp_comp_flag(i);
4357                 l_item_attribute_rec.pre_pro_lt := l_item_ctp_info.pre_pro_lt(i);
4358                 l_item_attribute_rec.post_pro_lt := l_item_ctp_info.post_pro_lt(i);
4359                 l_item_attribute_rec.fixed_lt := l_item_ctp_info.fixed_lt(i);
4360                 l_item_attribute_rec.variable_lt := l_item_ctp_info.variable_lt(i);
4361                 l_item_attribute_rec.substitution_window := l_item_ctp_info.substitution_window(i);
4362                 l_item_attribute_rec.create_supply_flag := l_item_ctp_info.create_supply_flag(i);
4363 
4364                 --diag_atp: pass the plan name so that if PO are created then plan_name is used for pegging
4365                 l_atp_rec.plan_name := l_item_ctp_info.plan_name(i);
4366 
4367                 MSC_ATP_PVT.G_DEMAND_PEGGING_ID :=  l_item_ctp_info.end_pegging_id(i);
4368                 l_atp_rec.top_tier_org_flag := 1;
4369                 l_atp_rec.original_item_flag := 1;
4370                 -- bug3467631 done for ship_rec_cal
4371                 l_atp_rec.shipping_cal_code       := p_atp_record.shipping_cal_code;
4372                 l_atp_rec.receiving_cal_code      := p_atp_record.receiving_cal_code;
4373                 l_atp_rec.intransit_cal_code      := p_atp_record.intransit_cal_code;
4374                 l_atp_rec.manufacturing_cal_code  := p_atp_record.manufacturing_cal_code;
4375 
4376 				l_alloc_atp := MSC_ATP_PVT.G_ALLOCATED_ATP; --ALLOC ATP CHANGES, 12973673
4377 
4378                 MSC_ATP_PVT.ATP_Check(l_atp_rec,
4379                                      l_plan_id,
4380                                      p_level ,
4381                                      p_scenario_id,
4382                                      2,  -- forward schedulling
4383                                      p_refresh_number,
4384                                      l_item_ctp_info.end_pegging_id(i),
4385                                      l_assign_set_id,
4386                                      l_atp_period,
4387                                      l_atp_supply_demand,
4388                                      x_return_status);
4389 				IF MSC_ATP_PVT.G_HYBRID_ALLOC_ATP = 'Y' THEN
4390 					MSC_ATP_PVT.G_ALLOCATED_ATP := l_alloc_atp; --ALLOC ATP CHANGES, 12973673
4391 				END IF;
4392 
4393                 IF PG_DEBUG in ('Y', 'C') THEN
4394                    msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Date from multi level CTP := ' || l_atp_rec.ship_date);
4395                    msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Date from SRs on this level :='
4396                                        || item_availability_info.future_atp_date(l_item_idx));
4397                 END IF;
4398 
4399                 IF l_atp_rec.ship_date < item_availability_info.future_atp_date(l_item_idx) THEN
4400                    -- going down is better
4401                    IF PG_DEBUG in ('Y', 'C') THEN
4402                       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Going down is better. so we add the supply demand dets');
4403                    END IF;
4404                    --- add l_atp_period and l_atp_supply_demand
4405                    -- dsting: l_period_begin_idx := l_all_atp_supply_demand.level.COUNT;
4406                    l_period_begin_idx := l_all_atp_period.level.COUNT;
4407                    l_sd_begin_idx := l_all_atp_supply_demand.level.count;
4408 
4409                    IF l_period_begin_idx = 0 THEN
4410                       l_period_begin_idx := 1;
4411                    ELSE
4412                       l_period_begin_idx := l_period_begin_idx + 1;
4413                    END IF;
4414 
4415                    IF l_sd_begin_idx = 0 THEN
4416 
4417                       l_sd_begin_idx := 1;
4418                    ELSE
4419                       l_sd_begin_idx := l_sd_begin_idx + 1;
4420                    END IF;
4421 
4422                    --- now add period and supply details
4423                    MSC_ATP_PROC.Details_Output(l_atp_period,
4424                                                l_atp_supply_demand,
4425                                                l_all_atp_period,
4426                                                l_all_atp_supply_demand,
4427                                                l_return_status);
4428                    l_period_end_idx := l_all_atp_period.level.count;
4429                    l_sd_end_idx := l_all_atp_supply_demand.level.count;
4430 
4431                    item_availability_info.fut_ctp_prd_detl_begin_idx(l_item_idx) := l_period_begin_idx;
4432                    item_availability_info.fut_ctp_prd_detl_end_idx(l_item_idx) := l_period_end_idx;
4433                    item_availability_info.fut_ctp_sd_detl_begin_idx(l_item_idx) := l_sd_begin_idx;
4434                    item_availability_info.fut_ctp_sd_detl_end_idx(l_item_idx) := l_sd_end_idx;
4435 
4436                    item_availability_info.future_atp_date(l_item_idx) := l_atp_rec.ship_date;
4437                    item_availability_info.atp_date_quantity(l_item_idx) :=
4438                                                           l_atp_rec.available_quantity;
4439                    ---- remove forward stealing pegging
4440                    IF (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
4441                       (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
4442                       (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1) THEN
4443 
4444                       --- recreate forward pegging array
4445                       l_atp_pegging_tab.delete;
4446                       IF PG_DEBUG in ('Y', 'C') THEN
4447                          msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'idemand priority alloc ATP, count := '|| l_atp_pegging_tab.count);
4448                          msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'begin_Idx := ' ||item_availability_info.fwd_steal_peg_begin_idx(l_item_idx));
4449                          msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'end_idx := ' || item_availability_info.fwd_steal_peg_end_idx(l_item_idx));
4450                       END IF;
4451                       FOR j in item_availability_info.fwd_steal_peg_begin_idx(l_item_idx)..item_availability_info.fwd_steal_peg_end_idx(l_item_idx) LOOP
4452                          l_atp_pegging_tab.extend;
4453                          l_atp_pegging_tab(l_atp_pegging_tab.count) := l_fwd_atp_pegging_tab(j);
4454                       END LOOP;
4455                       IF PG_DEBUG in ('Y', 'C') THEN
4456                          msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'count after recreating := ' || l_atp_pegging_tab.count);
4457                       END IF;
4458 
4459                       MSC_ATP_DB_UTILS.Remove_Invalid_Future_SD(l_atp_pegging_tab);
4460                    END IF;
4461                 ELSE
4462 
4463                    --- going down is worse than date on this level.
4464                    --- we remove pegging and supply demands
4465                    IF PG_DEBUG in ('Y', 'C') THEN
4466                       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Staying on this level is OK. We remove pegging for current level');
4467                       msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'G_FUTURE_PEGGING_ID := ' ||  MSC_ATP_PVT.G_FUTURE_PEGGING_ID);
4468                    END IF;
4469                    MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(
4470                                           MSC_ATP_PVT.G_FUTURE_PEGGING_ID,
4471                                           null,
4472                                           l_plan_id,
4473                                           MSC_ATP_PVT.UNDO,
4474                                           null,
4475                                           l_return_status);
4476 
4477 
4478                 END IF;
4479 
4480 
4481             END IF;
4482 
4483 
4484          END LOOP;
4485          --- now we should know dates for all the items
4486          --- we can now compare the dates and give the best results back
4487          IF PG_DEBUG in ('Y', 'C') THEN
4488             For i in reverse 1..item_availability_info.inventory_item_id.count LOOP
4489                msc_sch_wb.atp_debug('Item :'||i || item_availability_info.sr_inventory_item_id(i) ||
4490                                     ', Avail Date : ' || item_availability_info.future_atp_date(i) ||
4491                                     ',  : used_available_quantity ' || item_availability_info.used_available_quantity(i)||
4492                                     ',  : Atf_Date_Quantity ' || item_availability_info.Atf_Date_Quantity(i) ||
4493                                     ',  : Atf_Date ' || item_availability_info.Atf_Date(i));
4494 
4495             END LOOP;
4496          END IF;
4497 
4498          IF (l_org_item_detail_flag = 1 AND l_satisfied_by_subst_flag = 1) THEN
4499              -- demand has already beed satisfied by a substitute
4500              --- so we just add data for original item
4501              MSC_ATP_PVT.G_DEMAND_PEGGING_ID := l_demand_pegging_id;
4502              p_atp_record.request_item_id := item_availability_info.sr_inventory_item_id(l_item_count);
4503              p_atp_record.req_item_req_date_qty := item_availability_info.request_date_quantity(l_item_count);
4504 
4505              --p_atp_record.req_item_available_date := item_availability_info.future_atp_date(l_item_count);
4506              IF NVL(p_atp_record.atp_lead_time, 0) > 0 THEN
4507                  /* ship_rec_cal
4508                  p_atp_record.req_item_available_date := MSC_CALENDAR.DATE_OFFSET
4509                                  (p_atp_record.organization_id,
4510                                   p_atp_record.instance_id,
4511                                   1,
4512                                   item_availability_info.future_atp_date(l_item_count),
4513                                   p_atp_record.atp_lead_time);*/
4514 
4515                  p_atp_record.req_item_available_date := MSC_CALENDAR.DATE_OFFSET(
4516                                   p_atp_record.manufacturing_cal_code,
4517                                   p_atp_record.instance_id,
4518                                   item_availability_info.future_atp_date(l_item_count),
4519                                   p_atp_record.atp_lead_time, 1);
4520              ELSE
4521                 p_atp_record.req_item_available_date := item_availability_info.future_atp_date(l_item_count);
4522              END IF;
4523 
4524              p_atp_record.req_item_available_date_qty := item_availability_info.atp_date_quantity(l_item_count);
4525              --bug3709707  no need to assign name
4526              -- time_phased_atp
4527              --p_atp_record.request_item_name := item_availability_info.item_name(l_item_count); --bug3467631
4528              --p_atp_record.original_item_name := item_availability_info.item_name(l_item_count);
4529 
4530              ---- remove forward stealing pegging
4531              IF (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
4532                 (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
4533                 (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1) AND
4534                 item_availability_info.atp_flag(l_item_count) in ('Y', 'C') THEN
4535 
4536                 --- recreate forward pegging array
4537                 l_atp_pegging_tab.delete;
4538                 IF PG_DEBUG in ('Y', 'C') THEN
4539                    msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'idemand priority alloc ATP, count := '|| l_atp_pegging_tab.count);
4540                    msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'begin_Idx := ' ||item_availability_info.fwd_steal_peg_begin_idx(l_item_count));
4541                    msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'end_idx := ' || item_availability_info.fwd_steal_peg_end_idx(l_item_count));
4542                 END IF;
4543                 FOR j in item_availability_info.fwd_steal_peg_begin_idx(l_item_count)..item_availability_info.fwd_steal_peg_end_idx(l_item_count) LOOP
4544                    l_atp_pegging_tab.extend;
4545                    l_atp_pegging_tab(l_atp_pegging_tab.count) := l_fwd_atp_pegging_tab(j);
4546                 END LOOP;
4547                 IF PG_DEBUG in ('Y', 'C') THEN
4548                    msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'count after recreating := ' || l_atp_pegging_tab.count);
4549                 END IF;
4550 
4551                 MSC_ATP_DB_UTILS.Remove_Invalid_Future_SD(l_atp_pegging_tab);
4552              END IF;
4553 
4554              --- remove pegging and supply demand details for original item
4555              MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(
4556                                          item_availability_info.End_pegging_id(l_item_count),
4557                                           null,
4558                                           item_availability_info.plan_id(l_item_count),
4559                                           MSC_ATP_PVT.UNDO,
4560                                           null,
4561                                           l_return_status);
4562 
4563          ELSE
4564             --- demand has not been satisfied by any item
4565             --- so we try to find best date
4566             -- We start assuming that original item had the best date
4567             l_atp_date := item_availability_info.future_atp_date(l_item_count);
4568             l_item_idx := l_item_count;
4569             FOR i in reverse 1..item_availability_info.inventory_item_id.count LOOP
4570                 If item_availability_info.future_atp_date(i) < l_atp_date THEN
4571                    -- we have found better date than the requested item
4572                    l_item_idx := i;
4573                    l_atp_date := item_availability_info.future_atp_date(i);
4574                 END IF;
4575             END LOOP;
4576             --- now we have found best case scenrio
4577             --- so we populate the info
4578             IF PG_DEBUG in ('Y', 'C') THEN
4579                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_item_idx := ' || l_item_idx);
4580             END IF;
4581 
4582             -- time_phased_atp
4583             --p_atp_record.inventory_item_id := item_availability_info.sr_inventory_item_id(l_item_idx);
4584             --bug3467631 Old PF family shown and time_phased member shown
4585             p_atp_record.inventory_item_id := item_availability_info.family_sr_id(l_item_idx);
4586             p_atp_record.inventory_item_name := item_availability_info.family_item_name(l_item_idx);
4587             --bug3709707 commenting as assigned below
4588             --p_atp_record.request_item_id := item_availability_info.sr_inventory_item_id(l_item_idx);
4589             p_atp_record.requested_date_quantity := item_availability_info.request_date_quantity(l_item_idx);
4590             p_atp_record.used_available_quantity := item_availability_info.used_available_quantity(l_item_idx); --bug3467631
4591             p_atp_record.Atf_Date_Quantity := item_availability_info.Atf_Date_Quantity(l_item_idx); --bug3467631
4592 
4593             IF NVL(p_atp_record.atp_lead_time, 0) > 0 THEN
4594                /* ship_rec_cal
4595                p_atp_record.ship_date := MSC_CALENDAR.DATE_OFFSET
4596                            (p_atp_record.organization_id,
4597                             p_atp_record.instance_id,
4598                             1,
4599                             item_availability_info.future_atp_date(l_item_idx),
4600                             p_atp_record.atp_lead_time);*/
4601 
4602                p_atp_record.ship_date := MSC_CALENDAR.DATE_OFFSET(
4603                             p_atp_record.manufacturing_cal_code,
4604                             p_atp_record.instance_id,
4605                             item_availability_info.future_atp_date(l_item_idx),
4606                             p_atp_record.atp_lead_time, 1);
4607             ELSE
4608                p_atp_record.ship_date := item_availability_info.future_atp_date(l_item_idx);
4609             END IF;
4610             p_atp_record.ship_date := item_availability_info.future_atp_date(l_item_idx);
4611             p_atp_record.available_quantity := item_availability_info.atp_date_quantity(l_item_idx);
4612             --p_atp_record.inventory_item_name := item_availability_info.item_name(l_item_idx); --bug3467631 set above
4613             IF PG_DEBUG in ('Y', 'C') THEN
4614                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'p_atp_record.inventory_item_id := '|| p_atp_record.inventory_item_id);
4615                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'end_pegging_id := ' || item_availability_info.end_pegging_id(l_item_idx));
4616             END IF;
4617             MSC_ATP_PVT.G_DEMAND_PEGGING_ID := item_availability_info.end_pegging_id(l_item_idx);
4618 
4619             --- now populate the info for original item
4620             -- time_phased_atp
4621             --bug3709707 atf_date also populated so that correct atf date is returned to schedule
4622             p_atp_record.atf_date := item_availability_info.atf_date(l_item_idx);
4623             p_atp_record.request_item_id := item_availability_info.sr_inventory_item_id(l_item_idx); --bug3467631
4624             p_atp_record.request_item_name := item_availability_info.item_name(l_item_idx); --bug3467631
4625             p_atp_record.original_item_id := item_availability_info.sr_inventory_item_id(l_item_count);
4626             p_atp_record.original_item_name := item_availability_info.item_name(l_item_count);
4627 
4628             p_atp_record.req_item_req_date_qty :=  item_availability_info.request_date_quantity(l_item_count);
4629             p_atp_record.req_item_available_date := item_availability_info.future_atp_date(l_item_count);
4630             p_atp_record.req_item_available_date_qty := item_availability_info.atp_date_quantity(l_item_count);
4631 
4632             --- now we add supply demand and period details
4633             --- first we add details for backward search
4634             IF PG_DEBUG in ('Y', 'C') THEN
4635                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Add details for  pegging for backward search');
4636             END IF;
4637             MSC_ATP_SUBST.Details_Output(l_all_atp_period,
4638                                         l_all_atp_supply_demand,
4639                                         item_availability_info.period_detail_begin_idx(l_item_idx),
4640                                         item_availability_info.period_detail_end_idx(l_item_idx),
4641                                         item_availability_info.sd_detail_begin_idx(l_item_idx),
4642                                         item_availability_info.sd_detail_end_idx(l_item_idx),
4643                                         x_atp_period,
4644                                         x_atp_supply_demand,
4645                                         l_return_status);
4646             --- now add details for  backward CTP
4647             IF PG_DEBUG in ('Y', 'C') THEN
4648                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Add details for backward CTP');
4649             END IF;
4650             IF item_availability_info.ctp_prd_detl_begin_idx(l_item_idx) is not null and
4651                item_availability_info.ctp_sd_detl_begin_idx(l_item_idx) is not null THEN
4652                MSC_ATP_SUBST.Details_Output(l_all_atp_period,
4653                                             l_all_atp_supply_demand,
4654                                             item_availability_info.ctp_prd_detl_begin_idx(l_item_idx),
4655                                             item_availability_info.ctp_prd_detl_end_idx(l_item_idx),
4656                                             item_availability_info.ctp_sd_detl_begin_idx(l_item_idx),
4657                                             item_availability_info.ctp_sd_detl_end_idx(l_item_idx),
4658                                             x_atp_period,
4659                                             x_atp_supply_demand,
4660                                             l_return_status);
4661             END IF;
4662             --- now add future atp details
4663             IF PG_DEBUG in ('Y', 'C') THEN
4664                msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'add details for forward search');
4665             END IF;
4666             IF NVL(item_availability_info.fut_ctp_prd_detl_begin_idx(l_item_idx), 0 ) > 0 THEN
4667                --- going down is better
4668                --- ad future period and supply demand details
4669                IF PG_DEBUG in ('Y', 'C') THEN
4670                   msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Add s/d details for forward CTP');
4671                END IF;
4672                MSC_ATP_SUBST.Details_Output(l_all_atp_period,
4673                                          l_all_atp_supply_demand,
4674                                          item_availability_info.fut_ctp_prd_detl_begin_idx(l_item_idx),
4675                                          item_availability_info.fut_ctp_prd_detl_end_idx(l_item_idx),
4676                                          item_availability_info.fut_ctp_sd_detl_begin_idx(l_item_idx),
4677                                          item_availability_info.fut_ctp_sd_detl_end_idx(l_item_idx),
4678                                          x_atp_period,
4679                                          x_atp_supply_demand,
4680                                          l_return_status);
4681 
4682             ELSE
4683 
4684                --- future scheduled receipt is better than going down
4685                --- first we add pegging for this supply
4686                IF item_availability_info.atp_flag(l_item_idx) = 'N'
4687                              and item_availability_info.atp_flag(l_item_idx) = 'N' THEN
4688                      IF PG_DEBUG in ('Y', 'C') THEN
4689                         msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'item is non-atpable, dont do pegging');
4690                      END IF;
4691                ELSE
4692                   ---forward steal: demand has already been created, we dont want to create it again
4693                   IF NOT((MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
4694                         (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
4695                         (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1)) THEN
4696                      IF PG_DEBUG in ('Y', 'C') THEN
4697                         msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Add pegging and s/d details for SRS');
4698                      END IF;
4699 
4700                      l_pegging_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
4701                      l_pegging_rec.order_line_id:= MSC_ATP_PVT.G_ORDER_LINE_ID;
4702                      l_pegging_rec.parent_pegging_id:= item_availability_info.end_pegging_id(l_item_idx);
4703                      l_pegging_rec.pegging_id := item_availability_info.future_supply_peg_id(l_item_idx);
4704                      l_pegging_rec.end_pegging_id := item_availability_info.end_pegging_id(l_item_idx);
4705                      l_pegging_rec.atp_level:= p_level + 1;
4706                      l_pegging_rec.organization_id:= p_atp_record.organization_id;
4707                      l_pegging_rec.organization_code := l_top_tier_org_code;
4708                      l_pegging_rec.identifier1:= p_atp_record.instance_id;
4709                      l_pegging_rec.identifier2 := item_availability_info.plan_id(l_item_idx);
4710                      l_pegging_rec.identifier3 := NULL;
4711 
4712                      --bug3467631 time_phased_atp changes begin
4713                      IF (item_availability_info.sr_inventory_item_id(l_item_idx) <>
4714                                 item_availability_info.family_sr_id(l_item_idx))
4715                         and ((item_availability_info.future_atp_date(l_item_idx) >
4716                                   item_availability_info.atf_date(l_item_idx)) OR
4717                                   item_availability_info.atf_date(l_item_idx) is null)
4718                      THEN
4719                              l_pegging_rec.inventory_item_id:= item_availability_info.family_sr_id(l_item_idx);
4720                              l_pegging_rec.inventory_item_name := item_availability_info.family_item_name(l_item_idx);
4721                      ELSE
4722                              l_pegging_rec.inventory_item_id:= item_availability_info.sr_inventory_item_id(l_item_idx);
4723                              l_pegging_rec.inventory_item_name := item_availability_info.item_name(l_item_idx);
4724                      END IF;
4725                      -- time_phased_atp changes end
4726                      IF PG_DEBUG in ('Y', 'C') THEN
4727                         msc_sch_wb.atp_debug('family_sr_id: ' || item_availability_info.family_sr_id(l_item_idx));
4728                         msc_sch_wb.atp_debug('family_item_name: ' || item_availability_info.family_item_name(l_item_idx));
4729                         msc_sch_wb.atp_debug('sr_inventory_item_id: ' || item_availability_info.sr_inventory_item_id(l_item_idx));
4730                         msc_sch_wb.atp_debug('item_name: ' || item_availability_info.item_name(l_item_idx));
4731                      END IF;
4732 
4733                      l_pegging_rec.aggregate_time_fence_date := item_availability_info.Atf_Date(l_item_idx); --bug3467631
4734                      l_pegging_rec.resource_id := NULL;
4735                      l_pegging_rec.resource_code := NULL;
4736                      l_pegging_rec.department_id := NULL;
4737                      l_pegging_rec.department_code := NULL;
4738                      l_pegging_rec.supplier_id := NULL;
4739                      l_pegging_rec.supplier_name := NULL;
4740                      l_pegging_rec.supplier_site_id := NULL;
4741                      l_pegging_rec.supplier_site_name := NULL;
4742                      l_pegging_rec.scenario_id:= p_scenario_id;
4743                      l_pegging_rec.supply_demand_source_type:= MSC_ATP_PVT.ATP;
4744                      l_pegging_rec.supply_demand_quantity:= item_availability_info.atp_date_quantity(l_item_idx);
4745                      l_pegging_rec.supply_demand_date:= item_availability_info.future_atp_date(l_item_idx);
4746                      l_pegging_rec.supply_demand_type:= 2;
4747                      l_pegging_rec.source_type := 0;
4748 	             l_pegging_rec.component_identifier :=
4749                           NVL(p_atp_record.component_identifier, MSC_ATP_PVT.G_COMP_LINE_ID);
4750 
4751                      l_pegging_rec.constraint_flag := 'N';
4752 
4753 
4754                      l_pegging_rec.summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;
4755                      l_pegging_rec.request_item_id := p_atp_record.request_item_id;
4756 
4757                      MSC_ATP_SUBST.add_pegging(l_pegging_rec);
4758                      IF PG_DEBUG in ('Y', 'C') THEN
4759                         msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_future_supply_pegging_id :=' || l_pegging_id);
4760                      END IF;
4761                      MSC_ATP_SUBST.Details_Output(l_all_atp_period,
4762                                                 l_all_atp_supply_demand,
4763                                                 item_availability_info.fut_atp_prd_detl_begin_idx(l_item_idx),
4764                                                 item_availability_info.fut_atp_prd_detl_end_idx(l_item_idx),
4765                                                 item_availability_info.fut_atp_sd_detl_begin_idx(l_item_idx),
4766                                                 item_availability_info.fut_atp_sd_detl_end_idx(l_item_idx),
4767                                                 x_atp_period,
4768                                                 x_atp_supply_demand,
4769                                                 l_return_status);
4770                   END IF;
4771                END IF; -- IF NOT((MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
4772             END IF;
4773            --- now delete pegging and supply demand info
4774            IF PG_DEBUG in ('Y', 'C') THEN
4775               msc_Sch_wb.atp_debug('ATP_Check_Subst: ' || 'Remove pegging and supply demand details for all items');
4776            END IF;
4777            --- now delete pegging and supply demand info
4778            IF PG_DEBUG in ('Y', 'C') THEN
4779               msc_Sch_wb.atp_debug('ATP_Check_Subst: ' || 'Remove pegging and supply demand details for all items');
4780            END IF;
4781 
4782            For j in 1..item_availability_info.inventory_item_id.count LOOP
4783               IF PG_DEBUG in ('Y', 'C') THEN
4784                  msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'In loop for removing supply demand of all items');
4785                  msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_item_idx :- ' || l_item_idx);
4786               END IF;
4787               IF (item_availability_info.inventory_item_id(j)
4788                              <> item_availability_info.inventory_item_id(l_item_idx)) THEN
4789 
4790 
4791                   IF PG_DEBUG in ('Y', 'C') THEN
4792                      msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'Remove Pegging for item := '
4793                                         || item_availability_info.inventory_item_id(j));
4794                   END IF;
4795                    ---- remove forward stealing pegging
4796                    IF (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
4797                       (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
4798                       (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1) AND
4799                       (item_availability_info.atp_flag(j) in ('Y', 'C')) THEN
4800 
4801                       --- recreate forward pegging array
4802                       l_atp_pegging_tab.delete;
4803                       IF PG_DEBUG in ('Y', 'C') THEN
4804                          msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'idemand priority alloc ATP, count := '|| l_atp_pegging_tab.count);
4805                          msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'begin_Idx := ' ||item_availability_info.fwd_steal_peg_begin_idx(j));
4806                          msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'end_idx := ' || item_availability_info.fwd_steal_peg_end_idx(j));
4807                       END IF;
4808                       FOR l_item_cntr in item_availability_info.fwd_steal_peg_begin_idx(j)..item_availability_info.fwd_steal_peg_end_idx(j) LOOP
4809                          l_atp_pegging_tab.extend;
4810                          l_atp_pegging_tab(l_atp_pegging_tab.count) := l_fwd_atp_pegging_tab(l_item_cntr);
4811                       END LOOP;
4812                       IF PG_DEBUG in ('Y', 'C') THEN
4813                          msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'count after recreating := ' || l_atp_pegging_tab.count);
4814                       END IF;
4815 
4816                       MSC_ATP_DB_UTILS.Remove_Invalid_Future_SD(l_atp_pegging_tab);
4817                    END IF;
4818 
4819                    MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(
4820                                   item_availability_info.End_pegging_id(j),
4821                                    null,
4822                                    item_availability_info.plan_id(j),
4823                                    MSC_ATP_PVT.UNDO,
4824                                    null,
4825                                    l_return_status);
4826 
4827               END IF;  -- IF item_availability_info.inventory_item_id(j) <> l_item_ctp_info.inventory
4828            END LOOP; -- For j in 1..item_availability_info.inventory_item_id.count LOOP
4829 
4830          END IF;
4831          --- now set the error code
4832          IF PG_DEBUG in ('Y', 'C') THEN
4833             msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_org_item_detail_flag := ' || l_org_item_detail_flag);
4834             msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'l_satisfied_by_subst_flag := '|| l_satisfied_by_subst_flag);
4835          END IF;
4836          IF ((NVL(l_org_item_detail_flag, -1) <>1 OR NVL(l_satisfied_by_subst_flag,-1) <> 1)) THEN
4837 
4838             IF item_availability_info.atp_flag(l_item_idx) = 'N'
4839                           and item_availability_info.atp_comp_flag(l_item_idx) = 'N' THEN
4840                p_atp_record.error_code := MSC_ATP_PVT.ATP_NOT_APPL;
4841                --- if item we used to satify the demand is non-atpable then we want to show thate error
4842                -- we will come here only if date is apps due
4843             ELSE
4844                IF PG_DEBUG in ('Y', 'C') THEN
4845                   msc_Sch_wb.atp_debug('ATP_Check_Subst: ' || 'Set error code');
4846                END IF;
4847                IF p_atp_record.ship_date is not NULL THEN
4848                   p_atp_record.error_code :=  MSC_ATP_PVT.ATP_REQ_DATE_FAIL;
4849 
4850                ELSE
4851                   p_atp_record.error_code := MSC_ATP_PVT.ATP_REQ_QTY_FAIL;
4852                END IF;
4853             END IF;
4854          END IF;
4855 
4856       END IF;
4857 <<CLEANUP>>
4858      null;
4859    END IF ;  --- if l_substitution_type = ALL_OR_NOTHING THEN
4860 EXCEPTION
4861 
4862 WHEN MSC_ATP_PVT.NO_MATCHING_DATE_IN_CAL THEN --bug3583705
4863         IF PG_DEBUG in ('Y', 'C') THEN
4864            msc_sch_wb.atp_debug('MAtching cal date not found, in atp_check_subs');
4865         END IF;
4866         p_atp_record.error_code := MSC_ATP_PVT.NO_MATCHING_CAL_DATE;
4867         RAISE MSC_ATP_PVT.NO_MATCHING_DATE_IN_CAL;
4868 
4869 WHEN OTHERS THEN
4870         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4871 
4872 	-- dsting set error_code
4873 	--bug3583705 start
4874 	--p_atp_record.error_code := MSC_ATP_PVT.ATP_PROCESSING_ERROR;
4875 	/* Check if this is actually coming from a calendar routine*/
4876         l_encoded_text := fnd_message.GET_ENCODED;
4877         IF l_encoded_text IS NULL THEN
4878                 l_msg_app := NULL;
4879                 l_msg_name := NULL;
4880         ELSE
4881                 fnd_message.parse_encoded(l_encoded_text, l_msg_app, l_msg_name);
4882         END IF;
4883 
4884         -- Error Handling Changes
4885         IF (p_atp_record.error_code IS NULL) or (p_atp_record.error_code IN (0,61,150)) THEN
4886                 IF l_msg_app='MRP' AND l_msg_name='GEN-DATE OUT OF BOUNDS' THEN
4887                         p_atp_record.error_code := MSC_ATP_PVT.NO_MATCHING_CAL_DATE;
4888                         IF PG_DEBUG in ('Y', 'C') THEN
4889                            msc_sch_wb.atp_debug('ATP_CHECK_SUBS: NO_MATCHING_CAL_DATE');
4890                         END IF;
4891                 ELSE
4892                         p_atp_record.error_code := MSC_ATP_PVT.ATP_PROCESSING_ERROR; -- ATP Processing Error
4893                         IF PG_DEBUG in ('Y', 'C') THEN
4894                            msc_sch_wb.atp_debug('ATP_CHECK_SUBS: ATP_PROCESSING_ERROR');
4895                         END IF;
4896                 END IF;
4897         END IF;
4898         --bug3583705 end
4899         IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
4900             FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME , 'ATP_CHECK_SUBST');
4901         END IF;
4902         IF PG_DEBUG in ('Y', 'C') THEN
4903            msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'error := ' || sqlerrm);
4904         END IF;
4905 
4906         FOR i in 1..item_availability_info.inventory_item_id.count LOOP
4907            IF PG_DEBUG in ('Y', 'C') THEN
4908               msc_sch_wb.atp_debug('ATP_Check_Subst: ' || 'delete demand for item ' || item_availability_info.item_name(i)
4909                                           || ' ' || item_availability_info.sr_inventory_item_id(i));
4910            END IF;
4911 
4912            MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(item_availability_info.end_pegging_id(i),
4913                                                    null,
4914                                                    item_availability_info.plan_id(i),
4915                                                    MSC_ATP_PVT.UNDO,
4916                                                    null,
4917                                                    l_return_status);
4918 
4919         END LOOP;
4920 
4921         IF l_msg_app='MRP' AND l_msg_name='GEN-DATE OUT OF BOUNDS' THEN --bug3583705
4922                 RAISE MSC_ATP_PVT.NO_MATCHING_DATE_IN_CAL;
4923         ELSE
4924                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4925         END IF;
4926 END ATP_Check_Subst;
4927 
4928 Procedure Get_Item_Substitutes(p_inventory_item_id IN       NUMBER,
4929                                p_item_table        IN OUT   NoCopy MSC_ATP_SUBST.Item_Info_Rec_Typ,
4930                                p_instance_id       IN       NUMBER,
4931                                p_plan_id           IN       NUMBER,
4932                                p_customer_id       IN       NUMBER,
4933                                p_customer_site_id  IN       NUMBER,
4934                                p_request_date      IN       DATE,
4935                                p_organization_id   IN       NUMBER)
4936 IS
4937 l_item_table         MSC_ATP_SUBST.Item_Info_Rec_Typ;
4938 l_effective_dates    MRP_ATP_PUB.date_arr;
4939 l_disable_dates      MRP_ATP_PUB.date_arr;
4940 i                    number;
4941 l_count              number;
4942 l_count_no_site      number := 0;  --Web service
4943 L_RETURN_STATUS      varchar2(1);
4944 l_customer_id        number;
4945 l_customer_site_id   number;
4946 l_request_date       date;
4947 BEGIN
4948 
4949    IF PG_DEBUG in ('Y', 'C') THEN
4950       msc_sch_wb.atp_debug('**** Begin Get_Item_Substitutes ****');
4951       msc_sch_wb.atp_debug('Get_Item_Substitutes: ' || 'p_inventory_item_id = ' || p_inventory_item_id);
4952       msc_sch_wb.atp_debug('Get_Item_Substitutes: ' || 'p_instance_id := ' || p_instance_id);
4953       msc_sch_wb.atp_debug('Get_Item_Substitutes: ' || 'p_plan_id := ' || p_plan_id);
4954       msc_sch_wb.atp_debug('Get_Item_Substitutes: ' || 'p_customer_id := ' || p_customer_id);
4955       msc_sch_wb.atp_debug('Get_Item_Substitutes: ' || 'p_customer_site_id := ' || p_customer_site_id);
4956       msc_sch_wb.atp_debug('Get_Item_Substitutes: ' || 'p_request_date := ' || p_request_date);
4957       msc_sch_wb.atp_debug('Get_Item_Substitutes: ' || 'p_item_table.count := ' || p_item_table.inventory_item_id.count);
4958    END IF;
4959 
4960    l_request_date := trunc(p_request_date);
4961 
4962    IF PG_DEBUG in ('Y', 'C') THEN
4963       msc_sch_wb.atp_debug('Get_Item_Substitutes: ' || 'G_HIERARCHY_PROFILE = '||MSC_ATP_PVT.G_HIERARCHY_PROFILE);
4964    END IF;
4965    IF (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 2) THEN
4966      IF PG_DEBUG in ('Y', 'C') THEN
4967         msc_sch_wb.atp_debug('Get_Item_Substitutes: ' || 'Customer class alloc atp. Set the local varibale from global var');
4968      END IF;
4969      l_customer_id := MSC_ATP_PVT.G_PARTNER_ID;
4970      l_customer_site_id := MSC_ATP_PVT.G_PARTNER_SITE_ID;
4971    ELSIF p_customer_id is not null
4972          --and p_customer_site_id is not null
4973 		 THEN
4974 
4975      IF PG_DEBUG in ('Y', 'C') THEN
4976         msc_sch_wb.atp_debug('Get_Item_Substitutes: ' || 'Convert customer/site id');
4977      END IF;
4978      BEGIN
4979        SELECT TP_ID
4980        INTO   l_customer_id
4981        FROM   msc_tp_id_lid tp
4982        WHERE  tp.SR_TP_ID = p_customer_id
4983        AND    tp.SR_INSTANCE_ID = p_instance_id
4984        AND    tp.PARTNER_TYPE = 2;
4985      EXCEPTION
4986        WHEN NO_DATA_FOUND THEN
4987              l_customer_id := NULL;
4988      END ;
4989 
4990      IF PG_DEBUG in ('Y', 'C') THEN
4991         msc_sch_wb.atp_debug('Get_Item_Substitutes: ' || 'l_customer_id = '||l_customer_id);
4992      END IF;
4993 
4994      BEGIN
4995        SELECT TP_SITE_ID
4996        INTO   l_customer_site_id
4997        FROM   msc_tp_site_id_lid tpsite
4998        WHERE  tpsite.SR_TP_SITE_ID = p_customer_site_id
4999        AND    tpsite.SR_INSTANCE_ID =  p_instance_id
5000        AND    tpsite.PARTNER_TYPE = 2;
5001      EXCEPTION
5002        WHEN NO_DATA_FOUND THEN
5003              l_customer_site_id := null;
5004      END ;
5005      IF PG_DEBUG in ('Y', 'C') THEN
5006         msc_sch_wb.atp_debug('Get_Item_Substitutes: ' || 'l_customer_site_id := ' || l_customer_site_id);
5007      END IF;
5008 
5009    ELSE
5010      IF PG_DEBUG in ('Y', 'C') THEN
5011         msc_sch_wb.atp_debug('Get_Item_Substitutes: ' || 'Customer/customer site is not give');
5012      END IF;
5013      l_customer_id := null;
5014      l_customer_site_id := null;
5015    END IF;
5016 
5017    IF PG_DEBUG in ('Y', 'C') THEN
5018       msc_sch_wb.atp_debug('Get_Item_Substitutes: ' || 'l_customer_id := ' || l_customer_id);
5019       msc_sch_wb.atp_debug('Get_Item_Substitutes: ' || 'l_customer_site_id := ' || l_customer_site_id);
5020    END IF;
5021 
5022 /*-----------------------------------------------------
5023 Changed substitution rule picking logic as a part of GOP Web Service:
5024 
5025 If customer_id is not null then
5026     Get count of records from msc_item_substitutes for cust_site specific rule in l_count
5027     If l_count = 0 then
5028         Get count of records from msc_item_substitutes for cust specific rule in l_count_no_site
5029     End if;
5030 
5031   If l_count > 0 then
5032         Process for site rule
5033   Elsif l_count_no_site > 0 then
5034        Process for customer rule
5035   Else
5036       Get generic rule
5037   End if;
5038 
5039 Else  --cust is null
5040   Get generic rule
5041 End if;
5042 -----------------------------------------*/
5043 
5044    IF l_customer_id is not null THEN
5045       SELECT count(*)
5046       INTO   l_count
5047       FROM msc_item_substitutes mis
5048       WHERE mis.plan_id = p_plan_id
5049       AND   mis.sr_instance_id = p_instance_id
5050       ---bug 2341179 : inferred_flag is used for UI purpose only
5051       --AND   mis.inferred_flag = 2
5052       AND   mis.customer_id = l_customer_id
5053       AND   mis.customer_site_id = l_customer_site_id;
5054 
5055       IF l_count = 0 then
5056 		  SELECT count(*)
5057 		  INTO   l_count_no_site
5058 		  FROM msc_item_substitutes mis
5059 		  WHERE mis.plan_id = p_plan_id
5060 		  AND   mis.sr_instance_id = p_instance_id
5061 		  AND   mis.customer_id = l_customer_id
5062 		  AND   NVL(mis.customer_site_id, -1) = -1;
5063 
5064 		  IF PG_DEBUG in ('Y', 'C') THEN
5065 			msc_sch_wb.atp_debug('Get_Item_Substitutes: ' || 'Number of customer specific rule := ' || l_count_no_site);
5066 		  END IF;
5067 	  END IF;
5068 
5069 	  IF PG_DEBUG in ('Y', 'C') THEN
5070          msc_sch_wb.atp_debug('Get_Item_Substitutes: ' || 'Number of customer_site specific rule := ' || l_count);
5071       END IF;
5072 
5073       IF l_count > 0 THEN
5074          ---- customer specific rule
5075          IF PG_DEBUG in ('Y', 'C') THEN
5076             msc_sch_wb.atp_debug('Get_Item_Substitutes: ' || 'Get Customer_site specific rule');
5077          END IF;
5078          SELECT mis.higher_item_id, mis.partial_fulfillment_flag,
5079                 msi1.sr_inventory_item_id, msi1.item_name, mis.highest_item_id,
5080                 mis.effective_date, mis.disable_date,
5081                 -- time_phased_atp changes begin
5082                 DECODE(msi2.bom_item_type,
5083                     5, DECODE(msi2.atp_flag,
5084                         'N', msi1.sr_inventory_item_id,
5085                         msi2.sr_inventory_item_id),
5086                     msi1.sr_inventory_item_id
5087                 ),
5088                 DECODE(msi2.bom_item_type,
5089                     5, DECODE(msi2.atp_flag,
5090                         'N', msi1.inventory_item_id,
5091                         msi2.inventory_item_id),
5092                     msi1.inventory_item_id
5093                 ),
5094                 DECODE(msi2.bom_item_type,
5095                     5, DECODE(msi2.atp_flag,
5096                         'N', msi1.item_name,
5097                         msi2.item_name),
5098                     msi1.item_name
5099                 ),
5100                 msi2.aggregate_time_fence_date,
5101                 0
5102                 -- time_phased_atp changes end
5103          BULK COLLECT INTO
5104                 --bug 2462949: collect into p_atp_table instead of l_atp_table
5105                 p_item_table.inventory_item_id, p_item_table.partial_fulfillment_flag,
5106                 p_item_table.sr_inventory_item_id, p_item_table.item_name,
5107                 p_item_table.highest_revision_item_id,
5108 	        l_effective_dates, l_disable_dates,
5109                 -- time_phased_atp changes begin
5110                 p_item_table.family_sr_id,
5111                 p_item_table.family_dest_id,
5112                 p_item_table.family_item_name,
5113                 p_item_table.atf_date,
5114                 p_item_table.atf_date_quantity
5115                 -- time_phased_atp changes end
5116          FROM msc_item_substitutes mis,
5117               msc_system_items     msi1,
5118               msc_system_items     msi2
5119          WHERE mis.plan_id = p_plan_id
5120          AND   mis.sr_instance_id = p_instance_id
5121          AND   mis.lower_item_id = p_inventory_item_id
5122          AND   mis.effective_date <= l_request_date
5123          AND   NVL(mis.disable_date, l_request_date) >= l_request_date
5124          ---bug 2341179 : inferred_flag is used for UI purpose only
5125          --AND   mis.inferred_flag = 2
5126          AND   mis.customer_id = l_customer_id
5127          AND   mis.customer_site_id = l_customer_site_id
5128          AND   msi1.inventory_item_id = mis.higher_item_id
5129          AND   msi1.sr_instance_id = mis.sr_instance_id
5130          AND   msi1.plan_id = mis.plan_id
5131          AND   msi1.organization_id = p_organization_id
5132          -- time_phased_atp changes begin
5133          AND   msi2.inventory_item_id = DECODE(msi1.product_family_id,
5134                                                 NULL, msi1.inventory_item_id,
5135                                                 -23453, msi1.inventory_item_id,
5136                                            msi1.product_family_id)
5137          AND   msi2.organization_id = msi1.organization_id
5138          AND   msi2.sr_instance_id = msi1.sr_instance_id
5139          AND   msi2.plan_id = msi1.plan_id
5140          -- time_phased_atp changes end
5141          Order By mis.effective_date, mis.rank desc;
5142 
5143          IF PG_DEBUG in ('Y', 'C') THEN
5144             msc_sch_wb.atp_debug('Get_Item_Substitutes: ' || 'p_item_table.count := ' || p_item_table.inventory_item_id.count);
5145          END IF;
5146 
5147          FOR i in 1..p_item_table.inventory_item_id.count LOOP
5148            IF PG_DEBUG in ('Y', 'C') THEN
5149               msc_sch_wb.atp_debug('Get_Item_Substitutes: ' || 'counter: Inv_id : sr_inv_id : item_name : par_full_flg :: effective dt : disbale dt ');
5150               msc_sch_wb.atp_debug('Get_Item_Substitutes: ' || i || ' : ' || p_item_table.inventory_item_id(i) || ' : '
5151                                   || p_item_table.sr_inventory_item_id(i) || ' : '
5152                                   || p_item_table.item_name(i) || ' : '
5153                                   || p_item_table.partial_fulfillment_flag(i)
5154                                   || l_effective_dates(i)
5155                                   || l_disable_dates(i));
5156               msc_sch_wb.atp_debug('Get_Item_Substitutes: ' || 'counter: family_sr_id : family_dest_id : family_item_name : atf_date ');
5157               msc_sch_wb.atp_debug('Get_Item_Substitutes: ' || i || ' : ' || p_item_table.family_sr_id(i) || ' : '
5158                                   || p_item_table.family_dest_id(i) || ' : '
5159                                   || p_item_table.family_item_name(i) || ' : '
5160                                   || p_item_table.atf_date(i));
5161            END IF;
5162          END LOOP;
5163 
5164 	  ELSIF l_count_no_site > 0 THEN
5165 
5166 		 IF PG_DEBUG in ('Y', 'C') THEN
5167             msc_sch_wb.atp_debug('Get_Item_Substitutes: ' || 'Get Customer specific rule');
5168          END IF;
5169          SELECT mis.higher_item_id, mis.partial_fulfillment_flag,
5170                 msi1.sr_inventory_item_id, msi1.item_name, mis.highest_item_id,
5171                 mis.effective_date, mis.disable_date,
5172                 DECODE(msi2.bom_item_type,
5173                     5, DECODE(msi2.atp_flag,
5174                         'N', msi1.sr_inventory_item_id,
5175                         msi2.sr_inventory_item_id),
5176                     msi1.sr_inventory_item_id
5177                 ),
5178                 DECODE(msi2.bom_item_type,
5179                     5, DECODE(msi2.atp_flag,
5180                         'N', msi1.inventory_item_id,
5181                         msi2.inventory_item_id),
5182                     msi1.inventory_item_id
5183                 ),
5184                 DECODE(msi2.bom_item_type,
5185                     5, DECODE(msi2.atp_flag,
5186                         'N', msi1.item_name,
5187                         msi2.item_name),
5188                     msi1.item_name
5189                 ),
5190                 msi2.aggregate_time_fence_date,
5191                 0
5192          BULK COLLECT INTO
5193                 p_item_table.inventory_item_id, p_item_table.partial_fulfillment_flag,
5194                 p_item_table.sr_inventory_item_id, p_item_table.item_name,
5195                 p_item_table.highest_revision_item_id,
5196 	        l_effective_dates, l_disable_dates,
5197                 p_item_table.family_sr_id,
5198                 p_item_table.family_dest_id,
5199                 p_item_table.family_item_name,
5200                 p_item_table.atf_date,
5201                 p_item_table.atf_date_quantity
5202          FROM msc_item_substitutes mis,
5203               msc_system_items     msi1,
5204               msc_system_items     msi2
5205          WHERE mis.plan_id = p_plan_id
5206          AND   mis.sr_instance_id = p_instance_id
5207          AND   mis.lower_item_id = p_inventory_item_id
5208          AND   mis.effective_date <= l_request_date
5209          AND   NVL(mis.disable_date, l_request_date) >= l_request_date
5210          AND   mis.customer_id = l_customer_id
5211          AND   NVL(mis.customer_site_id, -1) = -1
5212          AND   msi1.inventory_item_id = mis.higher_item_id
5213          AND   msi1.sr_instance_id = mis.sr_instance_id
5214          AND   msi1.plan_id = mis.plan_id
5215          AND   msi1.organization_id = p_organization_id
5216          AND   msi2.inventory_item_id = DECODE(msi1.product_family_id,
5217                                                 NULL, msi1.inventory_item_id,
5218                                                 -23453, msi1.inventory_item_id,
5219                                            msi1.product_family_id)
5220          AND   msi2.organization_id = msi1.organization_id
5221          AND   msi2.sr_instance_id = msi1.sr_instance_id
5222          AND   msi2.plan_id = msi1.plan_id
5223          Order By mis.effective_date, mis.rank desc;
5224 
5225          IF PG_DEBUG in ('Y', 'C') THEN
5226             msc_sch_wb.atp_debug('Get_Item_Substitutes: ' || 'p_item_table.count := ' || p_item_table.inventory_item_id.count);
5227          END IF;
5228 
5229          FOR i in 1..p_item_table.inventory_item_id.count LOOP
5230            IF PG_DEBUG in ('Y', 'C') THEN
5231               msc_sch_wb.atp_debug('Get_Item_Substitutes: ' || 'counter: Inv_id : sr_inv_id : item_name : par_full_flg :: effective dt : disbale dt ');
5232               msc_sch_wb.atp_debug('Get_Item_Substitutes: ' || i || ' : ' || p_item_table.inventory_item_id(i) || ' : '
5233                                   || p_item_table.sr_inventory_item_id(i) || ' : '
5234                                   || p_item_table.item_name(i) || ' : '
5235                                   || p_item_table.partial_fulfillment_flag(i)
5236                                   || l_effective_dates(i)
5237                                   || l_disable_dates(i));
5238               msc_sch_wb.atp_debug('Get_Item_Substitutes: ' || 'counter: family_sr_id : family_dest_id : family_item_name : atf_date ');
5239               msc_sch_wb.atp_debug('Get_Item_Substitutes: ' || i || ' : ' || p_item_table.family_sr_id(i) || ' : '
5240                                   || p_item_table.family_dest_id(i) || ' : '
5241                                   || p_item_table.family_item_name(i) || ' : '
5242                                   || p_item_table.atf_date(i));
5243            END IF;
5244          END LOOP;
5245 
5246 	  ELSE
5247          ---generic rule
5248 
5249          IF PG_DEBUG in ('Y', 'C') THEN
5250             msc_sch_wb.atp_debug('Get_Item_Substitutes: ' || 'Get Generic rule');
5251          END IF;
5252          --- get generic rule
5253          --- no customer defined, get generic rule
5254          SELECT mis.higher_item_id, mis.partial_fulfillment_flag,
5255                 msi1.sr_inventory_item_id, msi1.item_name, mis.highest_item_id
5256  		-- dsting diagnostic ATP
5257 		,msi1.rounding_control_type
5258 		,msi1.unit_weight
5259 		,msi1.unit_volume
5260 		,msi1.weight_uom
5261 		,msi1.volume_uom,
5262                 -- time_phased_atp changes begin
5263                 DECODE(msi2.bom_item_type,
5264                     5, DECODE(msi2.atp_flag,
5265                         'N', msi1.sr_inventory_item_id,
5266                         msi2.sr_inventory_item_id),
5267                     msi1.sr_inventory_item_id
5268                 ),
5269                 DECODE(msi2.bom_item_type,
5270                     5, DECODE(msi2.atp_flag,
5271                         'N', msi1.inventory_item_id,
5272                         msi2.inventory_item_id),
5273                     msi1.inventory_item_id
5274                 ),
5275                 DECODE(msi2.bom_item_type,
5276                     5, DECODE(msi2.atp_flag,
5277                         'N', msi1.item_name,
5278                         msi2.item_name),
5279                     msi1.item_name
5280                 ),
5281                 msi2.aggregate_time_fence_date,
5282                 0
5283                 -- time_phased_atp changes end
5284 
5285          BULK COLLECT INTO
5286                 p_item_table.inventory_item_id, p_item_table.partial_fulfillment_flag,
5287                 p_item_table.sr_inventory_item_id, p_item_table.item_name, p_item_table.highest_revision_item_id
5288                 --p_item_table.inventory_item_id, l_item_name
5289  		-- dsting diagnostic ATP
5290 		,p_item_table.rounding_control_type
5291 		,p_item_table.unit_weight
5292 		,p_item_table.unit_volume
5293 		,p_item_table.weight_uom
5294 		,p_item_table.volume_uom
5295                 -- For time_phased_atp
5296                 ,p_item_table.family_sr_id
5297                 ,p_item_table.family_dest_id
5298                 ,p_item_table.family_item_name
5299                 ,p_item_table.atf_date
5300                 ,p_item_table.atf_date_quantity
5301          FROM msc_item_substitutes mis,
5302               msc_system_items     msi1,
5303               msc_system_items     msi2
5304          WHERE mis.plan_id = p_plan_id
5305          AND   mis.sr_instance_id = p_instance_id
5306          AND   mis.lower_item_id = p_inventory_item_id
5307          AND   mis.effective_date <= l_request_date
5308          AND   NVL(mis.disable_date, l_request_date) >= l_request_date
5309          ----  bug 2341179 : : inferred_flag is used for UI purpose only
5310          --AND   mis.inferred_flag = 2
5311          AND   NVL(mis.customer_id, -1) = -1
5312          AND   NVL(mis.customer_site_id, -1) = -1
5313          AND   msi1.inventory_item_id = mis.higher_item_id
5314          AND   msi1.sr_instance_id = mis.sr_instance_id
5315          AND   msi1.plan_id = mis.plan_id
5316          AND   msi1.organization_id = p_organization_id
5317          -- time_phased_atp changes begin
5318          AND   msi2.inventory_item_id = DECODE(msi1.product_family_id,
5319                                                 NULL, msi1.inventory_item_id,
5320                                                 -23453, msi1.inventory_item_id,
5321                                            msi1.product_family_id)
5322          AND   msi2.organization_id = msi1.organization_id
5323          AND   msi2.sr_instance_id = msi1.sr_instance_id
5324          AND   msi2.plan_id = msi1.plan_id
5325          -- time_phased_atp changes end
5326          Order By mis.rank desc;
5327       END IF;
5328 
5329    ELSE
5330 
5331 	  IF PG_DEBUG in ('Y', 'C') THEN
5332             msc_sch_wb.atp_debug('Get_Item_Substitutes: ' || 'Get Generic rule as Customer is not found..');
5333       END IF;
5334    --IF l_customer_id is NULL or l_customer_site_id is NULL THEN
5335       --- no customer defined, get generic rule
5336       SELECT mis.higher_item_id, mis.partial_fulfillment_flag,
5337              msi1.sr_inventory_item_id, msi1.item_name, mis.highest_item_id,
5338              -- time_phased_atp changes begin
5339              DECODE(msi2.bom_item_type,
5340                  5, DECODE(msi2.atp_flag,
5341                      'N', msi1.sr_inventory_item_id,
5342                      msi2.sr_inventory_item_id),
5343                  msi1.sr_inventory_item_id
5344              ),
5345              DECODE(msi2.bom_item_type,
5346                  5, DECODE(msi2.atp_flag,
5347                      'N', msi1.inventory_item_id,
5348                      msi2.inventory_item_id),
5349                  msi1.inventory_item_id
5350              ),
5351              DECODE(msi2.bom_item_type,
5352                  5, DECODE(msi2.atp_flag,
5353                      'N', msi1.item_name,
5354                      msi2.item_name),
5355                  msi1.item_name
5356              ),
5357              msi2.aggregate_time_fence_date,
5358              0
5359              -- time_phased_atp changes end
5360       BULK COLLECT INTO
5361              p_item_table.inventory_item_id, p_item_table.partial_fulfillment_flag,
5362              p_item_table.sr_inventory_item_id, p_item_table.item_name ,
5363              p_item_table.highest_revision_item_id,
5364              -- time_phased_atp changes begin
5365              p_item_table.family_sr_id,
5366              p_item_table.family_dest_id,
5367              p_item_table.family_item_name,
5368              p_item_table.atf_date,
5369              p_item_table.atf_date_quantity
5370              -- time_phased_atp changes end
5371       FROM  msc_item_substitutes mis,
5372             msc_system_items     msi1,
5373             msc_system_items     msi2
5374       WHERE mis.plan_id = p_plan_id
5375       AND   mis.sr_instance_id = p_instance_id
5376       AND   mis.lower_item_id = p_inventory_item_id
5377       AND   mis.effective_date <= l_request_date
5378       AND   NVL(mis.disable_date, l_request_date) >= l_request_date
5379       ---bug 2341179 : inferred_flag is used for UI purpose only
5380       --AND   mis.inferred_flag = 2
5381       AND   NVL(mis.customer_id, -1) = -1
5382       AND   NVL(mis.customer_site_id, -1) = -1
5383       AND   msi1.inventory_item_id = mis.higher_item_id
5384       AND   msi1.sr_instance_id = mis.sr_instance_id
5385       AND   msi1.plan_id = mis.plan_id
5386       AND   msi1.organization_id = p_organization_id
5387       -- time_phased_atp changes begin
5388       AND   msi2.inventory_item_id = DECODE(msi1.product_family_id,
5389                                                 NULL, msi1.inventory_item_id,
5390                                                 -23453, msi1.inventory_item_id,
5391                                            msi1.product_family_id)
5392       AND   msi2.organization_id = msi1.organization_id
5393       AND   msi2.sr_instance_id = msi1.sr_instance_id
5394       AND   msi2.plan_id = msi1.plan_id
5395       -- time_phased_atp changes end
5396       Order By mis.rank desc;
5397 
5398 
5399       IF PG_DEBUG in ('Y', 'C') THEN
5400          msc_sch_wb.atp_debug('Get_Item_Substitutes: ' || 'p_item_table.count := ' || p_item_table.inventory_item_id.count);
5401       END IF;
5402 
5403    END IF;
5404 
5405    FOR i in 1..p_item_table.inventory_item_id.count LOOP
5406       IF PG_DEBUG in ('Y', 'C') THEN
5407          msc_sch_wb.atp_debug('Get_Item_Substitutes: ' || 'counter: Inv_id : sr_inv_id : item_name : par_full_flg ');
5408          msc_sch_wb.atp_debug('Get_Item_Substitutes: ' || i || ' : ' || p_item_table.inventory_item_id(i) || ' : ' || p_item_table.sr_inventory_item_id(i) || ' : ' ||
5409                            p_item_table.item_name(i) || ' : ' || p_item_table.partial_fulfillment_flag(i));
5410          msc_sch_wb.atp_debug('Get_Item_Substitutes: ' || 'counter: family_sr_id : family_dest_id : family_item_name : atf_date ');
5411          msc_sch_wb.atp_debug('Get_Item_Substitutes: ' || i || ' : ' || p_item_table.family_sr_id(i) || ' : '
5412                              || p_item_table.family_dest_id(i) || ' : '
5413                              || p_item_table.family_item_name(i) || ' : '
5414                              || p_item_table.atf_date(i));
5415       END IF;
5416    END LOOP;
5417 
5418 
5419 END GET_ITEM_SUBSTITUTES;
5420 
5421 Procedure Update_demand(p_demand_id   number,
5422                         p_plan_id     number,
5423                         p_quantity    number)
5424 IS
5425 BEGIN
5426 IF PG_DEBUG in ('Y', 'C') THEN
5427    msc_sch_wb.atp_debug('BEGIN Update_demand ');
5428    msc_sch_wb.atp_debug('Update_demand: ' || 'p_demand_id := ' || p_demand_id);
5429    msc_sch_wb.atp_debug('Update_demand: ' || 'p_plan_id := ' || p_plan_id);
5430    msc_sch_wb.atp_debug('Update_demand: ' || 'p_quantity := ' || p_quantity);
5431 END IF;
5432 
5433       update msc_demands
5434       set    using_requirement_quantity = p_quantity
5435       where  demand_id = p_demand_id
5436       and    plan_id   = p_plan_id;
5437       IF PG_DEBUG in ('Y', 'C') THEN
5438          msc_sch_wb.atp_debug('Update_demand: ' || 'Number of Rows Updated := ' || SQL%ROWCOUNT);
5439       END IF;
5440 
5441       IF MSC_ATP_PVT.G_INV_CTP = 4 and MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y'
5442            AND MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1 AND MSC_ATP_PVT.G_ALLOCATION_METHOD = 1 THEN
5443 
5444          IF PG_DEBUG in ('Y', 'C') THEN
5445             msc_sch_wb.atp_debug('Update_demand: ' || 'Update preallocated demand');
5446          END IF;
5447          update msc_alloc_demands
5448          set allocated_quantity = p_quantity
5449          where parent_demand_id = p_demand_id
5450          and   plan_id = p_plan_id;
5451 
5452          IF PG_DEBUG in ('Y', 'C') THEN
5453             msc_sch_wb.atp_debug('Update_demand: ' || 'Number of Rows Updated := ' || SQL%ROWCOUNT);
5454          END IF;
5455       END IF;
5456 
5457 
5458 END Update_demand;
5459 
5460 
5461 Procedure Delete_demand_subst(p_demand_id   number,
5462                         p_plan_id     number)
5463 IS
5464 BEGIN
5465 IF PG_DEBUG in ('Y', 'C') THEN
5466    msc_sch_wb.atp_debug('Delete_demand_subst: ' || 'BEGIN delete demand subst ');
5467    msc_sch_wb.atp_debug('Delete_demand_subst: ' || 'p_demand_id := ' || p_demand_id);
5468    msc_sch_wb.atp_debug('Delete_demand_subst: ' || 'p_plan_id := ' || p_plan_id);
5469 END IF;
5470 
5471 
5472       --- DELETE DEMAND
5473       delete msc_demands
5474       where  demand_id = p_demand_id
5475       and    plan_id   = p_plan_id;
5476 
5477       IF PG_DEBUG in ('Y', 'C') THEN
5478          msc_sch_wb.atp_debug('Delete_demand_subst: ' || 'Number of Rows deleted := ' || SQL%ROWCOUNT);
5479       END IF;
5480 
5481       IF MSC_ATP_PVT.G_INV_CTP = 4 and MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y'
5482            AND MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1 AND MSC_ATP_PVT.G_ALLOCATION_METHOD = 1 THEN
5483 
5484          IF PG_DEBUG in ('Y', 'C') THEN
5485             msc_sch_wb.atp_debug('Delete_demand_subst: ' || 'Delete Allocated demand');
5486          END IF;
5487          delete msc_alloc_demands
5488          where parent_demand_id = p_demand_id
5489          and   plan_id = p_plan_id;
5490 
5491          IF PG_DEBUG in ('Y', 'C') THEN
5492             msc_sch_wb.atp_debug('Delete_demand_subst: ' || 'Number of Rows deleted := ' || SQL%ROWCOUNT);
5493          END IF;
5494 
5495       END IF;
5496 
5497 
5498 END Delete_demand_subst;
5499 
5500 PROCEDURE ADD_PEGGING(
5501          p_pegging_rec          IN         mrp_atp_details_temp%ROWTYPE)
5502 IS
5503 BEGIN
5504   IF PG_DEBUG in ('Y', 'C') THEN
5505    msc_sch_wb.atp_debug('ADD_PEGGING: ' || 'Add pegging for Peg id : ' || p_pegging_rec.pegging_id);
5506    msc_sch_wb.atp_debug('ADD_PEGGING: ' || 'intransit lead time: ' || p_pegging_rec.INTRANSIT_LEAD_TIME);
5507    msc_sch_wb.atp_debug('ADD_PEGGING: ' || 'aggregate_time_fence_date: ' || p_pegging_rec.aggregate_time_fence_date); --bug3467631
5508 END IF;
5509 
5510   INSERT into mrp_atp_details_temp
5511                  (session_id,
5512                   order_line_id,
5513 	          pegging_id,
5514                   parent_pegging_id,
5515                   atp_level,
5516                   record_type,
5517                   organization_id,
5518                   organization_code,
5519                   identifier1,
5520                   identifier2,
5521                   identifier3,
5522 		  inventory_item_id,
5523                   inventory_item_name,
5524                   resource_id,
5525                   resource_code,
5526                   department_id,
5527                   department_code,
5528                   supplier_id,
5529                   supplier_name,
5530  		  supplier_site_id,
5531                   supplier_site_name,
5532 	          scenario_id,
5533 		  source_type,
5534 		  supply_demand_source_type,
5535                   supply_demand_quantity,
5536 		  supply_demand_type,
5537 		  supply_demand_date,
5538                   end_pegging_id,
5539                   constraint_flag,
5540                   allocated_quantity, -- 1527660
5541                   number1,
5542                   char1,
5543 		  component_identifier,
5544                   -- resource batching
5545                   batchable_flag,
5546                   supplier_atp_date,
5547                   dest_inv_item_id,
5548                   summary_flag,
5549                   --- bug 2152184: For PF based ATP inventory_item_id field contains id for PF item
5550                   --- cto looks at pegging tree to place their demands. Since CTO expects to find
5551                   --  id for the requested item, we add the following column. CTO will now read from this column
5552                   request_item_id,
5553                   --- if req-date < ptf date then we update this column with PTF date
5554                   ptf_date
5555                 -- dsting
5556                 , creation_date
5557                 , created_by
5558                 , last_update_date
5559                 , last_updated_by
5560                 , last_update_login
5561                  --diag_atp
5562                 ,FIXED_LEAD_TIME,
5563                 VARIABLE_LEAD_TIME,
5564                 PREPROCESSING_LEAD_TIME,
5565                 PROCESSING_LEAD_TIME,
5566                 POSTPROCESSING_LEAD_TIME,
5567                 INTRANSIT_LEAD_TIME,
5568                 ATP_RULE_ID,
5569                 ALLOCATION_RULE,
5570                 INFINITE_TIME_FENCE,
5571                 SUBSTITUTION_WINDOW,
5572                 REQUIRED_QUANTITY,
5573                 ROUNDING_CONTROL,
5574                 ATP_FLAG,
5575                 ATP_COMPONENT_FLAG,
5576                 REQUIRED_DATE,
5577                 OPERATION_SEQUENCE_ID,
5578                 SOURCING_RULE_NAME,
5579                 OFFSET,
5580                 EFFICIENCY,
5581                 OWNING_DEPARTMENT,
5582                 REVERSE_CUM_YIELD,
5583                 BASIS_TYPE,
5584                 USAGE,
5585                 CONSTRAINT_TYPE,
5586                 CONSTRAINT_DATE,
5587                 CRITICAL_PATH,
5588                 PEGGING_TYPE,
5589                 UTILIZATION,
5590                 ATP_RULE_NAME,
5591                 PLAN_NAME,
5592                 CONSTRAINED_PATH,
5593                 weight_capacity,
5594                 volume_capacity,
5595                 weight_uom,
5596                 volume_uom,
5597                 ship_method,
5598                 aggregate_time_fence_date, --bug3467631 added so that deletion from alloc
5599                                           --tables may take place
5600                 shipping_cal_code,     -- Bug 3826234
5601                 receiving_cal_code,    -- Bug 3826234
5602                 intransit_cal_code,    -- Bug 3826234
5603                 manufacturing_cal_code -- Bug 3826234
5604 )
5605   VALUES
5606                  (p_pegging_rec.session_id,
5607                   p_pegging_rec.order_line_id,
5608                   p_pegging_rec.pegging_id,
5609                   p_pegging_rec.parent_pegging_id,
5610                   p_pegging_rec.atp_level,
5611                   3,
5612                   p_pegging_rec.organization_id,
5613                   p_pegging_rec.organization_code,
5614                   p_pegging_rec.identifier1,
5615                   p_pegging_rec.identifier2,
5616                   p_pegging_rec.identifier3,
5617                   p_pegging_rec.inventory_item_id,
5618                   p_pegging_rec.inventory_item_name,
5619                   p_pegging_rec.resource_id,
5620                   p_pegging_rec.resource_code,
5621                   p_pegging_rec.department_id,
5622                   p_pegging_rec.department_code,
5623                   p_pegging_rec.supplier_id,
5624                   p_pegging_rec.supplier_name,
5625                   p_pegging_rec.supplier_site_id,
5626                   p_pegging_rec.supplier_site_name,
5627                   p_pegging_rec.scenario_id,
5628 		  p_pegging_rec.source_type,
5629                   p_pegging_rec.supply_demand_source_type,
5630                   p_pegging_rec.supply_demand_quantity,
5631                   p_pegging_rec.supply_demand_type,
5632                   p_pegging_rec.supply_demand_date,
5633                   --NVL(MSC_ATP_PVT.G_DEMAND_PEGGING_ID, msc_full_pegging_s.currval),
5634                   p_pegging_rec.end_pegging_id,
5635                   p_pegging_rec.constraint_flag,
5636                   p_pegging_rec.allocated_quantity, -- 1527660
5637                   p_pegging_rec.number1,
5638                   p_pegging_rec.char1,
5639 		  p_pegging_rec.component_identifier,
5640                   p_pegging_rec.batchable_flag,
5641                   p_pegging_rec.supplier_atp_date,
5642                   p_pegging_rec.dest_inv_item_id,
5643                   p_pegging_rec.summary_flag,
5644                   p_pegging_rec.request_item_id,
5645                   p_pegging_rec.ptf_date
5646 		  -- dsting
5647 		  , sysdate
5648 		  , FND_GLOBAL.USER_ID
5649 		  , sysdate
5650 		  , FND_GLOBAL.USER_ID
5651 		  , FND_GLOBAL.USER_ID
5652                   ,p_pegging_rec.FIXED_LEAD_TIME,
5653                   p_pegging_rec.VARIABLE_LEAD_TIME,
5654                   p_pegging_rec.PREPROCESSING_LEAD_TIME,
5655                   p_pegging_rec.PROCESSING_LEAD_TIME,
5656                   p_pegging_rec.POSTPROCESSING_LEAD_TIME,
5657                   p_pegging_rec.INTRANSIT_LEAD_TIME,
5658                   p_pegging_rec.ATP_RULE_ID,
5659                   p_pegging_rec.ALLOCATION_RULE,
5660                   p_pegging_rec.INFINITE_TIME_FENCE,
5661                   p_pegging_rec.SUBSTITUTION_WINDOW,
5662                   p_pegging_rec.REQUIRED_QUANTITY,
5663                   p_pegging_rec.ROUNDING_CONTROL,
5664                   p_pegging_rec.ATP_FLAG,
5665                   p_pegging_rec.ATP_COMPONENT_FLAG,
5666 		  -- p_pegging_rec.REQUIRED_DATE,
5667 		  -- Bug 2748730. Move the required_date to day end only when the pegging is for demand line
5668 		  -- This is applicable irrespective of whether the line is overridden or not
5669 		  DECODE(p_pegging_rec.supply_demand_type,
5670 				1, TRUNC(p_pegging_rec.REQUIRED_DATE) + MSC_ATP_PVT.G_END_OF_DAY,
5671 				p_pegging_rec.REQUIRED_DATE),
5672                   p_pegging_rec.OPERATION_SEQUENCE_ID,
5673                   p_pegging_rec.SOURCING_RULE_NAME,
5674                   p_pegging_rec.OFFSET,
5675                   p_pegging_rec.EFFICIENCY,
5676                   p_pegging_rec.OWNING_DEPARTMENT,
5677                   p_pegging_rec.REVERSE_CUM_YIELD,
5678                   p_pegging_rec.BASIS_TYPE,
5679                   p_pegging_rec.USAGE,
5680                   p_pegging_rec.CONSTRAINT_TYPE,
5681                   p_pegging_rec.CONSTRAINT_DATE,
5682                   p_pegging_rec.CRITICAL_PATH,
5683                   p_pegging_rec.PEGGING_TYPE,
5684                   p_pegging_rec.UTILIZATION,
5685                   p_pegging_rec.ATP_RULE_NAME,
5686                   p_pegging_rec.PLAN_NAME,
5687                   p_pegging_rec.CONSTRAINED_PATH,
5688                   p_pegging_rec.weight_capacity,
5689                   p_pegging_rec.volume_capacity,
5690                   p_pegging_rec.weight_uom,
5691                   p_pegging_rec.volume_uom,
5692                   p_pegging_rec.ship_method,
5693                   p_pegging_rec.aggregate_time_fence_date, --bug3467631
5694                   p_pegging_rec.shipping_cal_code,     -- Bug 3826234
5695                   p_pegging_rec.receiving_cal_code,    -- Bug 3826234
5696                   p_pegging_rec.intransit_cal_code,    -- Bug 3826234
5697                   p_pegging_rec.manufacturing_cal_code -- Bug 3826234
5698 );
5699  IF PG_DEBUG in ('Y', 'C') THEN
5700    msc_sch_wb.atp_debug('ADD_PEGGING: ' || 'Number of rows : ' || SQL%ROWCOUNT);
5701  END IF;
5702 END ADD_PEGGING;
5703 
5704 Procedure Extend_Item_Info_Rec_Typ(
5705   p_item_avail_info        IN OUT NOCOPY  MSC_ATP_SUBST.Item_Info_Rec_Typ,
5706   x_return_status          OUT      NoCopy VARCHAR2)
5707 IS
5708 l_count number;
5709 BEGIN
5710 
5711    --- first we will make length equal for all tables
5712    --- the tables will have unequal length as we inserted values in few columns in get_item_substitutes procedure
5713    l_count := p_item_avail_info.inventory_item_id.count;
5714    IF l_count > 0 and p_item_avail_info.End_pegging_id.count = 0 THEN
5715 
5716       p_item_avail_info.End_pegging_id.EXTEND(l_count);
5717       p_item_avail_info.request_date_quantity.EXTEND(l_count);
5718       p_item_avail_info.period_detail_begin_idx.EXTEND(l_count);
5719       p_item_avail_info.period_detail_end_idx.EXTEND(l_count);
5720       p_item_avail_info.sd_detail_begin_idx.EXTEND(l_count);
5721        p_item_avail_info.sd_detail_end_idx.EXTEND(l_count);
5722        p_item_avail_info.CTP_PRD_DETL_BEGIN_IDX.EXTEND(l_count);
5723        p_item_avail_info.CTP_PRD_DETL_END_IDX.EXTEND(l_count);
5724        p_item_avail_info.CTP_SD_DETL_BEGIN_IDX.EXTEND(l_count);
5725        p_item_avail_info.CTP_SD_DETL_END_IDX.EXTEND(l_count);
5726        p_item_avail_info.FUT_CTP_PRD_DETL_BEGIN_IDX.EXTEND(l_count);
5727        p_item_avail_info.FUT_CTP_PRD_DETL_END_IDX.EXTEND(l_count);
5728        p_item_avail_info.FUT_CTP_SD_DETL_BEGIN_IDX.EXTEND(l_count);
5729        p_item_avail_info.FUT_CTP_SD_DETL_END_IDX.EXTEND(l_count);
5730        p_item_avail_info.atp_flag.EXTEND(l_count);
5731        p_item_avail_info.atp_comp_flag.EXTEND(l_count);
5732        p_item_avail_info.pre_pro_lt.EXTEND(l_count);
5733        p_item_avail_info.post_pro_lt.EXTEND(l_count);
5734        p_item_avail_info.fixed_lt.EXTEND(l_count);
5735        p_item_avail_info.variable_lt.EXTEND(l_count);
5736        p_item_avail_info.create_supply_flag.EXTEND(l_count);
5737        p_item_avail_info.substitution_window.EXTEND(l_count);
5738        p_item_avail_info.plan_id.EXTEND(l_count);
5739        p_item_avail_info.ASSIGN_SET_ID.EXTEND(l_count);
5740        p_item_avail_info.future_atp_date.EXTEND(l_count);
5741        p_item_avail_info.atp_date_quantity.EXTEND(l_count);
5742        p_item_avail_info.demand_id.extend(l_count);
5743        p_item_avail_info.FUT_ATP_PRD_DETL_BEGIN_IDX.EXTEND(l_count);
5744        p_item_avail_info.FUT_ATP_PRD_DETL_END_IDX.EXTEND(l_count);
5745        p_item_avail_info.FUT_ATP_SD_DETL_BEGIN_IDX.EXTEND(l_count);
5746        p_item_avail_info.FUT_ATP_SD_DETL_END_IDX.EXTEND(l_count);
5747        p_item_avail_info.future_supply_peg_id.extend(l_count);
5748        p_item_avail_info.demand_class.extend(l_count);
5749        p_item_avail_info.fwd_steal_peg_begin_idx.extend(l_count);
5750        p_item_avail_info.fwd_steal_peg_end_idx.extend(l_count);
5751        p_item_avail_info.used_available_quantity.EXTEND(l_count); --bug3467631
5752        p_item_avail_info.Atf_Date_Quantity.EXTEND(l_count); --bug3467631
5753 
5754        -- dsting diag atp
5755        p_item_avail_info.rounding_control_type.EXTEND(l_count);
5756        p_item_avail_info.unit_weight.EXTEND(l_count);
5757        p_item_avail_info.weight_uom.EXTEND(l_count);
5758        p_item_avail_info.unit_volume.EXTEND(l_count);
5759        p_item_avail_info.volume_uom.EXTEND(l_count);
5760        p_item_avail_info.plan_name.EXTEND(l_count);
5761        p_item_avail_info.item_name.EXTEND(l_count);
5762    END IF;
5763 
5764    --- now extend the tables
5765    p_item_avail_info.inventory_item_id.EXTEND;
5766    p_item_avail_info.sr_inventory_item_id.EXTEND;
5767    p_item_avail_info.highest_revision_item_id.EXTEND;
5768    p_item_avail_info.item_name.EXTEND;
5769    p_item_avail_info.End_pegging_id.EXTEND;
5770    p_item_avail_info.request_date_quantity.EXTEND;
5771    p_item_avail_info.partial_fulfillment_flag.EXTEND;
5772    p_item_avail_info.period_detail_begin_idx.EXTEND;
5773    p_item_avail_info.period_detail_end_idx.EXTEND;
5774    p_item_avail_info.sd_detail_begin_idx.EXTEND;
5775     p_item_avail_info.sd_detail_end_idx.EXTEND;
5776     p_item_avail_info.CTP_PRD_DETL_BEGIN_IDX.EXTEND;
5777     p_item_avail_info.CTP_PRD_DETL_END_IDX.EXTEND;
5778     p_item_avail_info.CTP_SD_DETL_BEGIN_IDX.EXTEND;
5779     p_item_avail_info.CTP_SD_DETL_END_IDX.EXTEND;
5780     p_item_avail_info.FUT_CTP_PRD_DETL_BEGIN_IDX.EXTEND;
5781     p_item_avail_info.FUT_CTP_PRD_DETL_END_IDX.EXTEND;
5782     p_item_avail_info.FUT_CTP_SD_DETL_BEGIN_IDX.EXTEND;
5783     p_item_avail_info.FUT_CTP_SD_DETL_END_IDX.EXTEND;
5784     p_item_avail_info.atp_flag.EXTEND;
5785     p_item_avail_info.atp_comp_flag.EXTEND;
5786     p_item_avail_info.pre_pro_lt.EXTEND;
5787     p_item_avail_info.post_pro_lt.EXTEND;
5788     p_item_avail_info.fixed_lt.EXTEND;
5789     p_item_avail_info.variable_lt.EXTEND;
5790     p_item_avail_info.create_supply_flag.EXTEND;
5791     p_item_avail_info.substitution_window.EXTEND;
5792     p_item_avail_info.plan_id.EXTEND;
5793     p_item_avail_info.ASSIGN_SET_ID.EXTEND;
5794     p_item_avail_info.future_atp_date.EXTEND;
5795     p_item_avail_info.atp_date_quantity.EXTEND;
5796     p_item_avail_info.demand_id.extend;
5797     p_item_avail_info.FUT_ATP_PRD_DETL_BEGIN_IDX.EXTEND;
5798     p_item_avail_info.FUT_ATP_PRD_DETL_END_IDX.EXTEND;
5799     p_item_avail_info.FUT_ATP_SD_DETL_BEGIN_IDX.EXTEND;
5800     p_item_avail_info.FUT_ATP_SD_DETL_END_IDX.EXTEND;
5801     p_item_avail_info.future_supply_peg_id.extend;
5802     p_item_avail_info.demand_class.extend;
5803     p_item_avail_info.fwd_steal_peg_begin_idx.extend;
5804     p_item_avail_info.fwd_steal_peg_end_idx.extend;
5805 
5806     -- dsting diag_atp
5807     p_item_avail_info.rounding_control_type.EXTEND;
5808     p_item_avail_info.unit_weight.EXTEND;
5809     p_item_avail_info.weight_uom.EXTEND;
5810     p_item_avail_info.unit_volume.EXTEND;
5811     p_item_avail_info.volume_uom.EXTEND;
5812     p_item_avail_info.plan_name.EXTEND;
5813 
5814     --time_phased_atp
5815     p_item_avail_info.Family_sr_id.EXTEND;
5816     p_item_avail_info.Family_dest_id.EXTEND;
5817     p_item_avail_info.Family_item_name.EXTEND;
5818     p_item_avail_info.Atf_Date.EXTEND;
5819     p_item_avail_info.Atf_Date_Quantity.EXTEND;
5820     p_item_avail_info.used_available_quantity.EXTEND; --bug3467631
5821 
5822 END Extend_Item_Info_Rec_Typ;
5823 
5824 PROCEDURE Details_Output (
5825   p_atp_period          IN       MRP_ATP_PUB.ATP_Period_Typ,
5826   p_atp_supply_demand   IN       MRP_ATP_PUB.ATP_Supply_Demand_Typ,
5827   p_begin_period_idx    IN       NUMBER,
5828   p_end_period_idx      IN       NUMBER,
5829   p_begin_sd_idx        IN       NUMBER,
5830   p_end_sd_idx          IN       NUMBER,
5831   x_atp_period          IN OUT   NOCOPY  MRP_ATP_PUB.ATP_Period_Typ,
5832   x_atp_supply_demand   IN OUT   NOCOPY  MRP_ATP_PUB.ATP_Supply_Demand_Typ,
5833   x_return_status       OUT      NoCopy VARCHAR2
5834 ) IS
5835 
5836 l_period_count          PLS_INTEGER;
5837 l_sd_count              PLS_INTEGER;
5838 l_count                 PLS_INTEGER;
5839 i                       PLS_INTEGER;
5840 Begin
5841 
5842     IF PG_DEBUG in ('Y', 'C') THEN
5843        msc_sch_wb.atp_debug('***** Begin Details_Output Procedure *****');
5844        msc_sch_wb.atp_debug('Details_Output: ' || 'p_begin_period_idx : = ' || p_begin_period_idx);
5845        msc_sch_wb.atp_debug('Details_Output: ' || 'p_end_period_idx := ' || p_end_period_idx);
5846        msc_sch_wb.atp_debug('Details_Output: ' || 'p_begin_sd_idx := ' || p_begin_sd_idx);
5847        msc_sch_wb.atp_debug('Details_Output: ' || 'p_end_sd_idx := ' || p_end_sd_idx);
5848     END IF;
5849 
5850     x_return_status := FND_API.G_RET_STS_SUCCESS;
5851 
5852     IF p_atp_period.level.COUNT > 0 THEN
5853 
5854         l_count := x_atp_period.level.COUNT;
5855         --FOR l_period_count in 1..p_atp_period.level.COUNT LOOP
5856         l_period_count := 0;
5857         FOR i in p_begin_period_idx..p_end_period_idx LOOP
5858                     MSC_SATP_FUNC.Extend_Atp_Period(x_atp_period, x_return_status);
5859                     l_period_count := l_period_count + 1;
5860                     x_atp_period.Level(l_count + l_period_count) :=
5861                         p_atp_period.Level(i);
5862                     x_atp_period.Inventory_Item_Id(l_count + l_period_count) :=
5863                         p_atp_period.Inventory_Item_Id(i);
5864                     x_atp_period.Request_Item_Id(l_count + l_period_count) :=
5865                         p_atp_period.Request_Item_Id(i);
5866                     x_atp_period.Organization_Id(l_count + l_period_count) :=
5867                         p_atp_period.Organization_Id(i);
5868                     x_atp_period.Department_Id(l_count + l_period_count) :=
5869                         p_atp_period.Department_Id(i);
5870                     x_atp_period.Resource_Id(l_count + l_period_count) :=
5871                         p_atp_period.Resource_Id(i);
5872                     x_atp_period.Supplier_Id(l_count + l_period_count) :=
5873                         p_atp_period.Supplier_Id(i);
5874                     x_atp_period.Supplier_Site_Id(l_count + l_period_count) :=
5875                         p_atp_period.Supplier_Site_Id(i);
5876                     x_atp_period.From_Organization_Id(l_count + l_period_count)
5877                         := p_atp_period.From_Organization_Id(i);
5878                     x_atp_period.From_Location_Id(l_count + l_period_count) :=
5879                         p_atp_period.From_Location_Id(i);
5880                     x_atp_period.To_Organization_Id(l_count + l_period_count) :=
5881                         p_atp_period.To_Organization_Id(i);
5882                     x_atp_period.To_Location_Id(l_count + l_period_count) :=
5883                         p_atp_period.To_Location_Id(i);
5884                     x_atp_period.Ship_Method(l_count + l_period_count) :=
5885                         p_atp_period.Ship_Method(i);
5886                     x_atp_period.Uom(l_count + l_period_count) :=
5887                         p_atp_period.Uom(i);
5888                     x_atp_period.Total_Supply_Quantity(l_count + l_period_count)
5889                         := p_atp_period.Total_Supply_Quantity(i);
5890                     x_atp_period.Total_Demand_Quantity(l_count + l_period_count)
5891                         := p_atp_period.Total_Demand_Quantity(i);
5892                     x_atp_period.Period_Start_Date(l_count + l_period_count):=
5893                         p_atp_period.Period_Start_Date(i);
5894                     x_atp_period.Period_End_Date(l_count + l_period_count):=
5895                         p_atp_period.Period_End_Date(i);
5896                     x_atp_period.Period_Quantity(l_count + l_period_count):=
5897                         p_atp_period.Period_Quantity(i);
5898                     x_atp_period.Cumulative_Quantity(l_count + l_period_count):=
5899                         p_atp_period.Cumulative_Quantity(i);
5900                     x_atp_period.Identifier1(l_count + l_period_count):=
5901                         p_atp_period.Identifier1(i);
5902                     x_atp_period.Identifier2(l_count + l_period_count):=
5903                         p_atp_period.Identifier2(i);
5904                     x_atp_period.Identifier(l_count + l_period_count):=
5905                         p_atp_period.Identifier(i);
5906                     x_atp_period.scenario_Id(l_count + l_period_count) :=
5907                         p_atp_period.scenario_Id(i);
5908                     x_atp_period.pegging_id(l_count + l_period_count) :=
5909                         p_atp_period.pegging_id(i);
5910                     x_atp_period.end_pegging_id(l_count + l_period_count) :=
5911                         p_atp_period.end_pegging_id(i);
5912 
5913 
5914         END LOOP;
5915     END IF;
5916 
5917     IF p_atp_supply_demand.level.COUNT > 0 THEN
5918         l_count := x_atp_supply_demand.level.COUNT;
5919 
5920         --FOR l_sd_count in 1..p_atp_supply_demand.level.COUNT LOOP
5921         l_sd_count := 0;
5922         FOR i in p_begin_sd_idx..p_end_sd_idx LOOP
5923                     l_sd_count := l_sd_count + 1;
5924                     MSC_SATP_FUNC.Extend_Atp_Supply_Demand(x_atp_supply_demand,
5925                                              x_return_status);
5926                     x_atp_supply_demand.Level(l_count + l_sd_count):=
5927                         p_atp_supply_demand.Level(i);
5928                     x_atp_supply_demand.Inventory_Item_Id(l_count+l_sd_count):=
5929                         p_atp_supply_demand.Inventory_Item_Id(i);
5930                     x_atp_supply_demand.Request_Item_Id(l_count+l_sd_count):=
5931                         p_atp_supply_demand.Request_Item_Id(i);
5932                     x_atp_supply_demand.Organization_Id(l_count+l_sd_count):=
5933                         p_atp_supply_demand.Organization_Id(i);
5934                     x_atp_supply_demand.Department_Id(l_count+l_sd_count):=
5935                         p_atp_supply_demand.Department_Id(i);
5936                     x_atp_supply_demand.Resource_Id(l_count+l_sd_count):=
5937                         p_atp_supply_demand.Resource_Id(i);
5938                     x_atp_supply_demand.Supplier_Id(l_count+l_sd_count):=
5939                         p_atp_supply_demand.Supplier_Id(i);
5940                     x_atp_supply_demand.Supplier_Site_Id(l_count+l_sd_count):=
5941                         p_atp_supply_demand.Supplier_Site_Id(i);
5942                     x_atp_supply_demand.From_Organization_Id(l_count+l_sd_count)
5943                         := p_atp_supply_demand.From_Organization_Id(i);
5944                     x_atp_supply_demand.From_Location_Id(l_count+l_sd_count):=
5945                         p_atp_supply_demand.From_Location_Id(i);
5946                     x_atp_supply_demand.To_Organization_Id(l_count+l_sd_count):=
5947                         p_atp_supply_demand.To_Organization_Id(i);
5948                     x_atp_supply_demand.To_Location_Id(l_count+l_sd_count):=
5949                         p_atp_supply_demand.To_Location_Id(i);
5950                     x_atp_supply_demand.Ship_Method(l_count+l_sd_count):=
5951                         p_atp_supply_demand.Ship_Method(i);
5952                     x_atp_supply_demand.Uom(l_count+l_sd_count):=
5953                         p_atp_supply_demand.Uom(i);
5954                     x_atp_supply_demand.Identifier1(l_count+l_sd_count):=
5955                         p_atp_supply_demand.Identifier1(i);
5956                     x_atp_supply_demand.Identifier2(l_count+l_sd_count):=
5957                         p_atp_supply_demand.Identifier2(i);
5958                     x_atp_supply_demand.Identifier3(l_count+l_sd_count):=
5959                         p_atp_supply_demand.Identifier3(i);
5960                     x_atp_supply_demand.Identifier4(l_count+l_sd_count):=
5961                         p_atp_supply_demand.Identifier4(i);
5962                     x_atp_supply_demand.Supply_Demand_Type(l_count+l_sd_count):=
5963                         p_atp_supply_demand.Supply_Demand_Type(i);
5964                     x_atp_supply_demand.Supply_Demand_Source_Type(l_count+ l_sd_count)
5965                         := p_atp_supply_demand.Supply_Demand_Source_Type(i);
5966                     x_atp_supply_demand.Supply_Demand_Source_Type_Name(l_count+l_sd_count):=
5967                         p_atp_supply_demand.Supply_Demand_Source_Type_Name(i);
5968                     x_atp_supply_demand.Supply_Demand_Date(l_count+l_sd_count):=
5969                         p_atp_supply_demand.Supply_Demand_Date(i);
5970                     x_atp_supply_demand.Supply_Demand_Quantity(l_count+l_sd_count) :=
5971                         p_atp_supply_demand.Supply_Demand_Quantity(i);
5972                     x_atp_supply_demand.Identifier(l_count + l_sd_count):=
5973                         p_atp_supply_demand.Identifier(i);
5974                     x_atp_supply_demand.scenario_Id(l_count+l_sd_count):=
5975                         p_atp_supply_demand.scenario_Id(i);
5976                     x_atp_supply_demand.Disposition_Type(l_count+l_sd_count):=
5977                         p_atp_supply_demand.Disposition_Type(i);
5978                     x_atp_supply_demand.Disposition_Name(l_count+l_sd_count):=
5979                         p_atp_supply_demand.Disposition_Name(i);
5980                     x_atp_supply_demand.Pegging_Id(l_count+l_sd_count):=
5981                         p_atp_supply_demand.Pegging_Id(i);
5982                     x_atp_supply_demand.End_Pegging_Id(l_count+l_sd_count):=
5983                         p_atp_supply_demand.End_Pegging_Id(i);
5984 
5985 	IF PG_DEBUG in ('Y', 'C') THEN
5986 	   msc_sch_wb.atp_debug('Details_Output: item '||
5987 		p_atp_supply_demand.inventory_item_id(i)||
5988 		' : org '|| p_atp_supply_demand.organization_id(i) ||
5989 		' : qty '|| p_atp_supply_demand.supply_demand_quantity(i) ||
5990 		' : peg '|| p_atp_supply_demand.pegging_id(i));
5991 	END IF;
5992 
5993 
5994         END LOOP;
5995     END IF;
5996 
5997     IF PG_DEBUG in ('Y', 'C') THEN
5998        msc_sch_wb.atp_debug('***** End Details_Output Procedure *****');
5999     END IF;
6000 
6001 END Details_Output;
6002 
6003 Procedure Copy_Item_Info_Rec(p_parent_item_info       IN     MSC_ATP_SUBST.Item_Info_Rec_Typ,
6004                              p_child_item_info         IN OUT NoCopy MSC_ATP_SUBST.Item_Info_Rec_Typ,
6005                              p_index                  IN     NUMBER)
6006 IS
6007 l_return_status varchar2(1);
6008 l_count number;
6009 BEGIN
6010    IF PG_DEBUG in ('Y', 'C') THEN
6011       msc_sch_wb.atp_debug('***** start Copy_Item_Info_Rec ****');
6012    END IF;
6013    MSC_ATP_SUBST.Extend_Item_Info_Rec_Typ(p_child_item_info, l_return_status);
6014    l_count := p_child_item_info.inventory_item_id.count;
6015     p_child_item_info.inventory_item_id(l_count) := p_parent_item_info.inventory_item_id(p_index);
6016     p_child_item_info.sr_inventory_item_id(l_count) := p_parent_item_info.sr_inventory_item_id(p_index);
6017     p_child_item_info.highest_revision_item_id(l_count) :=  p_parent_item_info.highest_revision_item_id(p_index);
6018 
6019     p_child_item_info.item_name(l_count) :=  p_parent_item_info.item_name(p_index);
6020     p_child_item_info.End_pegging_id(l_count) :=  p_parent_item_info.End_pegging_id(p_index);
6021     p_child_item_info.request_date_quantity(l_count) :=  p_parent_item_info.request_date_quantity(p_index);
6022     p_child_item_info.partial_fulfillment_flag(l_count) :=  p_parent_item_info.partial_fulfillment_flag(p_index);
6023     p_child_item_info.period_detail_begin_idx(l_count) :=  p_parent_item_info.period_detail_begin_idx(p_index);
6024     p_child_item_info.period_detail_end_idx(l_count) :=  p_parent_item_info.period_detail_end_idx(p_index);
6025     p_child_item_info.sd_detail_begin_idx(l_count) :=   p_parent_item_info.sd_detail_begin_idx(p_index);
6026     p_child_item_info.sd_detail_end_idx(l_count) :=  p_parent_item_info.sd_detail_end_idx(p_index);
6027     p_child_item_info.ctp_prd_detl_begin_idx(l_count) := p_parent_item_info.ctp_prd_detl_begin_idx(p_index);
6028     p_child_item_info.CTP_PRD_DETL_END_IDX(l_count) := p_parent_item_info.CTP_PRD_DETL_END_IDX(p_index);
6029     p_child_item_info.CTP_SD_DETL_BEGIN_IDX(l_count) := p_parent_item_info.CTP_SD_DETL_BEGIN_IDX(p_index);
6030     p_child_item_info.CTP_SD_DETL_END_IDX(l_count) := p_parent_item_info.CTP_SD_DETL_END_IDX(p_index);
6031     p_child_item_info.FUT_CTP_PRD_DETL_BEGIN_IDX(l_count) := p_parent_item_info.FUT_CTP_PRD_DETL_BEGIN_IDX(p_index);
6032     p_child_item_info.FUT_CTP_PRD_DETL_END_IDX(l_count) := p_parent_item_info.FUT_CTP_PRD_DETL_END_IDX(p_index);
6033     p_child_item_info.FUT_CTP_SD_DETL_BEGIN_IDX(l_count) := p_parent_item_info.FUT_CTP_SD_DETL_BEGIN_IDX(p_index);
6034     p_child_item_info.FUT_CTP_SD_DETL_END_IDX(l_count) := p_parent_item_info.FUT_CTP_SD_DETL_END_IDX(p_index);
6035     p_child_item_info.atp_flag(l_count) :=  p_parent_item_info.atp_flag(p_index);
6036     p_child_item_info.atp_comp_flag(l_count) :=  p_parent_item_info.atp_comp_flag(p_index);
6037     p_child_item_info.pre_pro_lt(l_count) :=   p_parent_item_info.pre_pro_lt(p_index);
6038     p_child_item_info.post_pro_lt(l_count) :=  p_parent_item_info.post_pro_lt(p_index);
6039     p_child_item_info.fixed_lt(l_count) :=    p_parent_item_info.fixed_lt(p_index);
6040     p_child_item_info.variable_lt(l_count) :=   p_parent_item_info.variable_lt(p_index);
6041     p_child_item_info.create_supply_flag(l_count) :=  p_parent_item_info.create_supply_flag(p_index);
6042     p_child_item_info.substitution_window(l_count) :=  p_parent_item_info.substitution_window(p_index);
6043     p_child_item_info.plan_id(l_count) := p_parent_item_info.plan_id(p_index);
6044     p_child_item_info.ASSIGN_SET_ID(l_count) := p_parent_item_info.ASSIGN_SET_ID(p_index);
6045     p_child_item_info.future_atp_date(l_count) := p_parent_item_info.future_atp_date(p_index);
6046     p_child_item_info.atp_date_quantity(l_count) := p_parent_item_info.atp_date_quantity(p_index);
6047     p_child_item_info.demand_id(l_count) := p_parent_item_info.demand_id(p_index);
6048     p_child_item_info.demand_class(l_count) := p_parent_item_info.demand_class(p_index);
6049     p_child_item_info.fwd_steal_peg_begin_idx(l_count) := p_parent_item_info.fwd_steal_peg_begin_idx(p_index);
6050     p_child_item_info.fwd_steal_peg_end_idx(l_count)   := p_parent_item_info.fwd_steal_peg_end_idx(p_index);
6051     p_child_item_info.rounding_control_type(l_count) := p_parent_item_info.rounding_control_type(p_index);
6052     --diag_atp : add the following variable for peeign enhancement
6053     p_child_item_info.unit_weight(l_count) := p_parent_item_info.unit_weight(p_index);
6054     p_child_item_info.weight_uom(l_count) := p_parent_item_info.weight_uom(p_index);
6055     p_child_item_info.unit_volume(l_count) := p_parent_item_info.unit_volume(p_index);
6056     p_child_item_info.volume_uom(l_count) := p_parent_item_info.volume_uom(p_index);
6057     p_child_item_info.plan_name(l_count) := p_parent_item_info.plan_name(p_index);
6058 
6059     --time_phased_atp
6060     p_child_item_info.Family_sr_id(l_count) := p_parent_item_info.Family_sr_id(p_index);
6061     p_child_item_info.Family_dest_id(l_count) := p_parent_item_info.Family_dest_id(p_index);
6062     p_child_item_info.Family_item_name(l_count) := p_parent_item_info.Family_item_name(p_index);
6063     p_child_item_info.Atf_Date(l_count) := p_parent_item_info.Atf_Date(p_index);
6064     p_child_item_info.Atf_Date_Quantity(l_count) := p_parent_item_info.Atf_Date_Quantity(p_index);
6065 
6066 
6067    IF PG_DEBUG in ('Y', 'C') THEN
6068       msc_sch_wb.atp_debug('***** END Copy_Item_Info_Rec ****');
6069    END IF;
6070 
6071 END Copy_Item_Info_Rec;
6072 
6073 /* time_phased_atp
6074    we no longer need this procedure
6075    to be deleted after code review
6076 PROCEDURE Add_Mat_Demand(
6077   p_atp_rec          IN         MRP_ATP_PVT.AtpRec ,
6078   p_plan_id          IN         NUMBER ,
6079   p_dc_flag          IN         NUMBER,
6080   x_demand_id        OUT        NoCopy NUMBER
6081 )
6082 IS
6083 l_sqlfound      BOOLEAN := FALSE;
6084 my_sqlcode      NUMBER;
6085 l_count         NUMBER;
6086 temp_sd_qty     number;
6087 l_record_source number := 2; -- for plan order pegging
6088 BEGIN
6089    IF PG_DEBUG in ('Y', 'C') THEN
6090       msc_sch_wb.atp_debug('***** Begin SUBST Add_Mat_Demand *****');
6091       msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'p_atp_rec.quantity_ordered '||p_atp_rec.quantity_ordered);
6092       msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'p_atp_rec.requested_ship_date '||p_atp_rec.requested_ship_date);
6093       msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'p_atp_rec.origination_type '||p_atp_rec.origination_type);
6094       msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'p_atp_rec.inventory_item_id '||p_atp_rec.inventory_item_id);
6095       msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'p_atp_rec.organization_id '||p_atp_rec.organization_id);
6096       msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'p_atp_rec.demand_source_line '||p_atp_rec.demand_source_line);
6097       msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'p_atp_rec.instance_id '||p_atp_rec.instance_id);
6098       msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'p_plan_id = ' || p_plan_id);
6099       msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'p_atp_rec.action '||p_atp_rec.action);
6100       msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'request_item_id :=' || p_atp_rec.request_item_id);
6101    END IF;
6102    --IF (p_atp_rec.origination_type NOT IN (6, 30)) THEN
6103 
6104         IF PG_DEBUG in ('Y', 'C') THEN
6105            msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'before insert into msc_demands');
6106         END IF;
6107 
6108         INSERT INTO MSC_DEMANDS(
6109                 DEMAND_ID,
6110                 USING_REQUIREMENT_QUANTITY,
6111                 USING_ASSEMBLY_DEMAND_DATE,
6112                 DEMAND_TYPE,
6113                 ORIGINATION_TYPE,
6114                 USING_ASSEMBLY_ITEM_ID,
6115                 PLAN_ID,
6116                 ORGANIZATION_ID,
6117                 INVENTORY_ITEM_ID,
6118                 SALES_ORDER_LINE_ID,
6119                 SR_INSTANCE_ID,
6120                 LAST_UPDATE_DATE,
6121                 LAST_UPDATED_BY,
6122                 CREATION_DATE,
6123                 CREATED_BY,
6124                 DEMAND_CLASS,
6125                 REFRESH_NUMBER,
6126                 ORDER_NUMBER,
6127                 APPLIED,
6128                 STATUS,
6129                 CUSTOMER_ID,
6130                 SHIP_TO_SITE_ID,
6131                 original_item_id,
6132                 record_source) -- For plan order pegging
6133                 ---STOLEN_FLAG)  -- 02/16: Stealing
6134         VALUES(
6135                 msc_demands_s.nextval,
6136                 p_atp_rec.quantity_ordered,
6137                 TRUNC(p_atp_rec.requested_ship_date) + MSC_ATP_PVT.G_END_OF_DAY , -- For bug 2259824
6138                 1, -- discrete demand
6139                 p_atp_rec.origination_type,
6140                 p_atp_rec.inventory_item_id,
6141                 p_plan_id,
6142                 p_atp_rec.organization_id,
6143                 p_atp_rec.inventory_item_id,
6144                 p_atp_rec.demand_source_line,
6145                 p_atp_rec.instance_id,
6146                 sysdate,
6147                 FND_GLOBAL.USER_ID,
6148                 sysdate,
6149                 FND_GLOBAL.USER_ID,
6150                 p_atp_rec.demand_class,
6151                 p_atp_rec.refresh_number,
6152                 -- Modified by ngoel on 1/12/2001 for origination_type = 30
6153                 decode(p_atp_rec.origination_type, 6, p_atp_rec.order_number,
6154                        30, p_atp_rec.order_number,
6155                        null),
6156                 decode(p_atp_rec.origination_type, 6, 2, 30, 2, null),
6157                 decode(p_atp_rec.origination_type, 6, 0, 30, 0, null),
6158                 MSC_ATP_PVT.G_PARTNER_ID,
6159                 MSC_ATP_PVT.G_PARTNER_SITE_ID,
6160                 p_atp_rec.request_item_id,
6161                 l_record_source)  -- For plan order pegging
6162                 --1657855, remove support for min allocation
6163                 ---p_atp_rec.stolen_flag)
6164         RETURNING DEMAND_ID INTO x_demand_id;
6165         IF PG_DEBUG in ('Y', 'C') THEN
6166            msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'Numbe of rows inserted := ' || SQL%ROWCOUNT);
6167         END IF;
6168         IF ((MSC_ATP_PVT.G_INV_CTP = 4) AND
6169              (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
6170              (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
6171              (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1)) THEN
6172 
6173 	   IF PG_DEBUG in ('Y', 'C') THEN
6174 	      msc_sch_wb.atp_debug('Add_Mat_Demand: before insert into'||
6175                                 ' msc_alloc_demands');
6176 	   END IF;
6177 
6178            INSERT INTO MSC_ALLOC_DEMANDS(
6179                        PLAN_ID,
6180 	               INVENTORY_ITEM_ID,
6181                        ORGANIZATION_ID,
6182                        SR_INSTANCE_ID,
6183                        DEMAND_CLASS,
6184                        DEMAND_DATE,
6185                        PARENT_DEMAND_ID,
6186                        ALLOCATED_QUANTITY,
6187                        ORIGINATION_TYPE,
6188                        ORDER_NUMBER,
6189 		       SALES_ORDER_LINE_ID,
6190                        CREATED_BY,
6191                        CREATION_DATE,
6192                        LAST_UPDATED_BY,
6193                        LAST_UPDATE_DATE
6194                        )
6195            VALUES (
6196                    p_plan_id,
6197                    p_atp_rec.inventory_item_id,
6198                    p_atp_rec.organization_id,
6199                    p_atp_rec.instance_id,
6200                    p_atp_rec.demand_class,
6201                    p_atp_rec.requested_ship_date, -- QUESTION arrival items ?
6202                    x_demand_id,
6203                    p_atp_rec.quantity_ordered,
6204                    p_atp_rec.origination_type,
6205                    p_atp_rec.order_number,
6206                    p_atp_rec.demand_source_line,
6207                    FND_GLOBAL.USER_ID,
6208                    sysdate,
6209                    FND_GLOBAL.USER_ID,
6210                    sysdate);
6211         END IF;
6212    --END IF;
6213    IF PG_DEBUG in ('Y', 'C') THEN
6214       msc_sch_wb.atp_debug('***** End Add_Mat_Demand *****');
6215    END IF;
6216 
6217 END Add_Mat_Demand;
6218 */
6219 End MSC_ATP_SUBST;