DBA Data[Home] [Help]

PACKAGE BODY: APPS.MSC_ATP_SUBST

Source


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