DBA Data[Home] [Help]

PACKAGE BODY: APPS.MSC_ATP_PVT

Source


1 PACKAGE BODY MSC_ATP_PVT AS
2 /* $Header: MSCGATPB.pls 120.66 2011/12/06 09:56:19 vjuluri ship $  */
3 G_PKG_NAME 		CONSTANT VARCHAR2(30) := 'MSC_ATP_PVT';
4 
5 -- ship_rec_cal
6 G_CALENDAR_CONSTRAINT   VARCHAR2(1) := 'N';
7 
8  -- dsting 9/6/02
9  -- internal to this package for allowing call_schedule_remote
10  -- to pass the profile option MSC_ALLOCATED_ATP_WORKFLOW to Call_Schedule
11  -- without changing Call_Schedule's spec
12  G_ATP_WORKFLOW                VARCHAR2(1);
13 
14 --  Supplier Capacity and Lead Time (SCLT) changes
15 --  Global internal to this package to allow ATP_Check to pass
16 --  the variable to ATP_Check in a recursive call for BUY items.
17 g_sup_cap_cum_date             DATE;
18 
19 NO_MAKE_BUY_CHILDREN 	CONSTANT NUMBER := 1;
20 MAKE_BUY_CHILDREN 	CONSTANT NUMBER := 2;
21 
22 -- This is the main procedure called for Scheduling
23 -- Change name to Schedule
24 
25 PG_DEBUG varchar2(1) := NVL(FND_PROFILE.value('MSC_ATP_DEBUG'), 'N');
26 
27 -- dsting
28 -- For multilevel models, the override flag for the input record is reset to null
29 -- after the first level.
30 G_LINE_OVERRIDE_FLAG varchar2(1);
31 
32 -- dsting new shipping logic
33 -- the latest acceptable date for the set is an
34 --   arrival date if p_atp_table.requested_ship_date(1) is null
35 --   ship date otherwise
36 -- this is regardless of whether this is a ship set or arrival set
37 --
38 -- the LAD for a particular line is an
39 --   arrival date if l_atp_table.requested_ship_date(i) is null
40 --   ship_date otherwise
41 -- this is regardless of whether the set's LAD is an arrival or ship_date
42 --
43 -- the group_date for the set is an
44 --   arrival_date if l_arrival_count > 1
45 --   ship_date otherwise
46 --
47 
48 -- For summary enhancement
49 G_TIME_PHASED_SET               VARCHAR2(1);
50 
51 G_OFF_INSTANCE_ID       MRP_ATP_PUB.number_arr := MRP_ATP_PUB.number_arr(); --Bug 3629191
52 G_OFF_DEM_TRANS_ID      MRP_ATP_PUB.number_arr := MRP_ATP_PUB.number_arr(); --Bug 3629191
53 G_OFF_PLAN_ID           MRP_ATP_PUB.number_arr := MRP_ATP_PUB.number_arr(); --Bug 3629191
54 
55 l_sys_next_omc_date             DATE;   -- Bug 12672787
56 
57 Procedure Schedule (p_atp_table     IN    MRP_ATP_PUB.ATP_Rec_Typ,
58                p_instance_id        IN    NUMBER,
59                p_assign_set_id      IN    NUMBER,
60                p_refresh_number     IN    NUMBER,
61                x_atp_table          OUT   NoCopy MRP_ATP_PUB.ATP_Rec_Typ,
62                x_return_status      OUT   NoCopy VARCHAR2,
63                x_msg_data           OUT   NoCopy VARCHAR2,
64                x_msg_count          OUT   NoCopy NUMBER,
65                x_atp_supply_demand  OUT   NOCOPY MRP_ATP_PUB.ATP_Supply_Demand_Typ,
66                x_atp_period         OUT   NOCOPY MRP_ATP_PUB.ATP_Period_Typ,
67                x_atp_details        OUT   NOCOPY MRP_ATP_PUB.ATP_Details_Typ
68 ) IS
69 
70 i                       PLS_INTEGER := 1;
71 j			PLS_INTEGER := 1;
72 k			PLS_INTEGER := 1;
73 m			PLS_INTEGER := 1;
74 l_sched_flag		PLS_INTEGER := 1;
75 l_search		PLS_INTEGER := 1;
76 l_temp                  NUMBER;
77 l_plan_id               NUMBER;
78 l_temp_plan_id          NUMBER; -- this is for ship set items
79 --Bug 1661545, array to store old plan id for undoing unscheduling in case of failure.
80 l_old_plan_id           MRP_ATP_PUB.number_arr := MRP_ATP_PUB.number_arr(); -- this is for ship set items
81 l_process_item_id	NUMBER;
82 l_process_org_id	NUMBER;
83 l_ship_count            NUMBER;
84 l_arrival_count         NUMBER;
85 l_level			PLS_INTEGER := 0;
86 l_scenario_id		PLS_INTEGER := 1;
87 l_group_date		DATE ;
88 l_schedule_flag		NUMBER;
89 l_requested_item_id	NUMBER;
90 l_total_qty             NUMBER;
91 l_return_status		VARCHAR2(10);
92 l_done_scenario2        VARCHAR2(1);
93 l_quantity_uom		VARCHAR2(3);
94 l_quantity_ordered	NUMBER;
95 l_overall_status	PLS_INTEGER;
96 l_ship_set_status       PLS_INTEGER;
97 l_arrival_set_status    PLS_INTEGER;
98 l_process_arrival_set   VARCHAR2(30) := '@@@';
99 l_sources               MRP_ATP_PVT.Atp_Source_Typ;
100 l_atp_rec		MRP_ATP_PVT.AtpRec;
101 l_future_atp_rec	MRP_ATP_PVT.AtpRec;
102 l_atp_table             MRP_ATP_PUB.ATP_Rec_Typ;
103 l_atp_period            MRP_ATP_PUB.ATP_Period_Typ;
104 l_atp_supply_demand     MRP_ATP_PUB.ATP_Supply_Demand_Typ;
105 l_future_atp_period            MRP_ATP_PUB.ATP_Period_Typ;
106 l_future_atp_supply_demand     MRP_ATP_PUB.ATP_Supply_Demand_Typ;
107 l_atp_details           MRP_ATP_PUB.ATP_Details_Typ;
108 l_temp_atp_table             MRP_ATP_PUB.ATP_Rec_Typ;
109 l_temp_atp_period            MRP_ATP_PUB.ATP_Period_Typ;
110 l_temp_atp_supply_demand     MRP_ATP_PUB.ATP_Supply_Demand_Typ;
111 l_temp_atp_details           MRP_ATP_PUB.ATP_Details_Typ;
112 l_temp_arr_atp_table             MRP_ATP_PUB.ATP_Rec_Typ;
113 l_temp_arr_atp_period            MRP_ATP_PUB.ATP_Period_Typ;
114 l_temp_arr_atp_supply_demand     MRP_ATP_PUB.ATP_Supply_Demand_Typ;
115 l_temp_arr_atp_details           MRP_ATP_PUB.ATP_Details_Typ;
116 l_curr_atp_period            MRP_ATP_PUB.ATP_Period_Typ;
117 l_curr_atp_supply_demand     MRP_ATP_PUB.ATP_Supply_Demand_Typ;
118 l_complete_scatp	VARCHAR2(1) := 'N';
119 l_multi_scenario	NUMBER;
120 l_null_atp_period            MRP_ATP_PUB.ATP_Period_Typ;
121 l_null_atp_supply_demand     MRP_ATP_PUB.ATP_Supply_Demand_Typ;
122 l_null_atp_details           MRP_ATP_PUB.ATP_Details_Typ;
123 l_set_element_success  PLS_INTEGER;
124 l_oe_demand_flag          VARCHAR2(1) := 'N';
125 my_sqlcode		NUMBER;
126 temp			NUMBER;
127 l_stmt                  PLS_INTEGER;
128 l_assign_set_id		NUMBER;
129 l_temp_assign_set_id         NUMBER;
130 l_conversion_rate       NUMBER;
131 l_atp_flag                      VARCHAR2(1);
132 l_atp_comp_flag                 VARCHAR2(1);
133 l_so_tbl_status         PLS_INTEGER;
134 l_arr_no_date           VARCHAR2(1) := 'Y';
135 l_default_atp_rule_id           NUMBER;
136 l_calendar_code                 VARCHAR2(14);
137 l_calendar_exception_set_id     NUMBER;
138 l_default_demand_class          VARCHAR2(34);
139 l_sd_qty                        NUMBER;
140 l_inv_item_id 			NUMBER;
141 --l_ship_date                     DATE; -- ship_rec_cal changes
142 l_fst_src_pegging_ids	        MRP_ATP_PUB.number_arr:=MRP_ATP_PUB.number_arr();
143 l_date_higher_src		varchar2(1) := 'Y';
144 l_peg_count			NUMBER;
145 l_dblink                        VARCHAR2(128);
146 l_sys_next_date                 DATE;
147 -- l_sys_next_omc_date             DATE;   -- Bug 3371817 Commented for Bug12672787
148 l_request_org_id                number;
149 l_sourcing_org_id               number;
150 l_count 			number;
151 l_line_id_count                 NUMBER;  -- keeps track of line identifiers.
152 l_demand_pegging_id 		number;
153 L_DEMAND_CLASS_FLAG             number;
154 
155 -- 1873918: PDS-ODS fix
156 l_inv_ctp                       number;
157 l_allocated_atp                 varchar2(1);
158 l_allocation_method             number;
159 l_summary_flag                  number;
160 -- Bug 2085071: Assignment Set
161 lv_assign_set_id                NUMBER;
162 --2152184
163 l_pf_item_id			MRP_ATP_PUB.number_arr:=MRP_ATP_PUB.number_arr();
164 --l_old_org_id                    MRP_ATP_PUB.number_arr:=MRP_ATP_PUB.number_arr();
165 
166 ---subst
167 l_item_substitute_rec           MSC_ATP_SUBST.Item_Info_Rec_Typ;
168 l_null_item_sub_rec             MSC_ATP_SUBST.Item_Info_Rec_Typ;
169 l_item_attribute_rec            MSC_ATP_PVT.item_attribute_rec;
170 l_pre_pro_lt                    number;
171 l_post_pro_lt                   number;
172 l_fixed_lt                      number;
173 l_variable_lt                   number;
174 L_INVENTORY_ITEM_ID             number;
175 L_ITEM_NAME                     varchar2(250);
176 l_create_supply_on_orig_item    number;
177 l_substitution_type             number;
178 L_DELIVERY_LEAD_TIME            number;
179 L_REQUESTED_SHIP_DATE           date;
180 L_FROM_LOCATION_ID              number;
181 L_SHIP_METHOD                   varchar2(50);
182 l_demand_ids                    MRP_ATP_PUB.number_arr:=MRP_ATP_PUB.number_arr();
183 l_old_pf_item_id                number;
184 l_inv_item_ids                  MRP_ATP_PUB.number_arr:=MRP_ATP_PUB.number_arr();
185 
186 l_org_code                      VARCHAR2(7);
187 l_counter                       NUMBER;  -- defined for Bug 2368426
188 
189 -- Error handling insert
190 l_tmp_pegging_id                NUMBER;
191 /* Modularize Item and Org Info */
192 -- Local item record already provided
193 l_org_attribute_rec             MSC_ATP_PVT.org_attribute_rec;
194 -- Used for bug 2392456
195 l_plan_info_rec                 MSC_ATP_PVT.plan_info_rec;
196 l_bkwd_pass_atf_date_qty         NUMBER; --bug3397904
197 
198 --bug 2384224
199 l_del_demand_ids             MRP_ATP_PUB.number_arr:=MRP_ATP_PUB.number_arr();
200 l_del_inv_item_ids           MRP_ATP_PUB.number_arr:=MRP_ATP_PUB.number_arr();
201 l_del_plan_ids               MRP_ATP_PUB.number_arr:=MRP_ATP_PUB.number_arr();
202 l_del_identifiers            MRP_ATP_PUB.number_arr:=MRP_ATP_PUB.number_arr();
203 l_del_demand_source_type     MRP_ATP_PUB.number_arr:=MRP_ATP_PUB.number_arr();--cmro
204 l_delete_demand_flag         PLS_INTEGER;
205 
206 --diag_atp
207 l_pegging_ids           MRP_ATP_PUB.number_arr := MRP_ATP_PUB.number_arr();
208 l_highest_rev		NUMBER;
209 
210 -- ATP Override rajjain begin
211 l_latest_acceptable_date	DATE;
212 l_override_flag			VARCHAR2(1) := 'N';
213 -- ATP Override rajjain end
214 
215 l_null_item_info_rec          item_attribute_rec;
216 l_item_info_rec               item_attribute_rec;
217 l_null_org_info_rec           org_attribute_rec;
218 l_null_plan_info_rec          plan_info_rec;
219 
220 -- rajjain 01/30/2003 Bug 2777661
221 l_sd_date                     DATE;
222 
223 -- dsting setproc
224 l_line_status                 NUMBER;
225 l_set_status                  NUMBER;
226 
227 -- bug 2795053-reopen (ssurendr)
228 l_dmd_satisfied_date          DATE;
229 
230 -- 2902265
231 l_req_atp_date                  mrp_atp_pub.date_arr := mrp_atp_pub.date_arr();
232 l_req_date_qty                  mrp_atp_pub.number_arr := mrp_atp_pub.number_arr();
233 l_demand_class_atp_flag         mrp_atp_pub.number_arr := mrp_atp_pub.number_arr();
234 
235 --s_cto_rearch
236 l_item_sourcing_info_rec       MSC_ATP_CTO.item_sourcing_info_rec;
237 
238 l_null_item_sourcing_info_rec  MSC_ATP_CTO.item_sourcing_info_rec;
239 l_plan_found_for_match         number;
240 --e_cto_rearch
241 
242 -- time_phased_atp
243 l_pf_atp                VARCHAR2(1) := 'N';
244 l_time_phased_atp       VARCHAR2(1) := 'N';
245 l_atf_date              DATE;
246 l_time_phased_set       VARCHAR2(1) := 'N';
247 l_atf_dates             MRP_ATP_PUB.date_arr := MRP_ATP_PUB.date_arr();
248 l_match_item_family_id  NUMBER;
249 
250 -- For summary enhancement
251 l_copy_demand_ids               mrp_atp_pub.number_arr := mrp_atp_pub.number_arr();
252 l_del_copy_demand_ids           mrp_atp_pub.number_arr := mrp_atp_pub.number_arr();
253 l_del_copy_demand_plan_ids      mrp_atp_pub.number_arr := mrp_atp_pub.number_arr();
254 
255 
256 -- CTO ODR and Simplified Pegging
257 l_atp_peg_items                   MRP_ATP_PUB.Number_Arr ;
258 l_atp_peg_demands                 MRP_ATP_PUB.Number_Arr ;
259 l_atp_peg_supplies                MRP_ATP_PUB.Number_Arr ;
260 l_atp_peg_res_reqs                MRP_ATP_PUB.Number_Arr ;
261 l_demand_instance_id              MRP_ATP_PUB.Number_Arr ; --Bug 3629191
262 l_supply_instance_id              MRP_ATP_PUB.Number_Arr ; --Bug 3629191
263 l_res_instance_id                 MRP_ATP_PUB.Number_Arr ; --Bug 3629191
264 l_del_atp_peg_items               MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr();
265 l_del_atp_peg_demands             MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr();
266 l_del_atp_peg_supplies            MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr();
267 l_del_atp_peg_res_reqs            MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr();
268 l_atp_peg_demands_plan_ids        MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr(); --Bug 3629191
269 l_atp_peg_supplies_plan_ids       MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr(); --Bug 3629191
270 l_atp_peg_res_reqs_plan_ids       MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr(); --Bug 3629191
271 l_off_demand_instance_id          MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr(); --Bug 3629191
272 l_off_supply_instance_id          MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr(); --Bug 3629191
273 l_off_res_instance_id             MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr(); --Bug 3629191
274 -- END CTO ODR and Simplified Pegging
275 --plan by request date enhancment
276 l_original_req_arrival_date    DATE;
277 l_req_arr_date                 mrp_atp_pub.date_arr := mrp_atp_pub.date_arr();
278 -- ship_rec_cal changes
279 l_shipping_cal_code		VARCHAR2(14);
280 l_receiving_cal_code		VARCHAR2(14);
281 l_manufacturing_cal_code	VARCHAR2(14);
282 l_intransit_cal_code		VARCHAR2(14);
283 l_new_ship_date                 DATE;
284 l_new_dock_date                 DATE;
285 l_group_arrival_date            DATE;
286 l_order_date_type               NUMBER;
287 l_ship_arrival_date_rec 	MSC_ATP_PVT.ship_arrival_date_rec_typ;
288 l_encoded_text                  varchar2(4000);
289 l_msg_app                       varchar2(50);
290 l_msg_name                      varchar2(30);
291 l_error_code                    INTEGER;
292 
293 -- Bug 3226083
294 l_atp_insert_rec                MRP_ATP_PVT.AtpRec;
295 l_mem_dest_id                   NUMBER;
296 l_pf_dest_id                    NUMBER;
297 
298 l_sysdate_orc                   DATE; --bug3439591
299 l_trunc_sysdate                 DATE := TRUNC(sysdate); --bug3439591
300 
301 --bug 3751114
302 l_peg_record_type               NUMBER := 3;
303 l_constraint_path_flag          NUMBER := 1;
304 l_delete_atp_rec                MSC_ATP_PVT.DELETE_ATP_REC; --3720018
305 
306 --2814895
307 l_partner_type                  NUMBER := 2;
308 
309 l_sysdate_orc_new               DATE; --bug4291375
310 l_past_due_ship_date            NUMBER; --bug4291375
311 L_MOVE_PAST_DUE_TO_SYSDATE 			varchar2(1);  -- Bug 5584634/5618929
312 
313 --optional_fw start
314 l_last_date                     DATE;         --Used in binary search algo
315 l_first_date                    DATE;         --Used in binary search algo
316 l_low_seqnum                    NUMBER;       --Used in binary search algo
317 l_high_seqnum                   NUMBER;       --Used in binary search algo
318 l_middle_seqnum                 NUMBER;       --Used in binary search algo
319 l_last_search                   NUMBER;       --Used in binary search algo
320 l_last_successful_date          DATE;         --Used in binary search algo
321 l_atp_rule_name                 VARCHAR2(80); --Used for infinite time fence date procedure
322 
323 l_request_date_qty              NUMBER;       --User Entered Values
324 l_quan_ordered                  NUMBER;       --User Entered Values
325 l_number_of_iterations	        NUMBER;       --Used in binary search algo
326 l_second_good_run               NUMBER;       --Used in binary search algo
327 l_loop_count			NUMBER;       --Used in binary search algo
328 l_g_inv_ctp                     NUMBER;       --Used in binary search algo
329 l_last_success_atp_rec          MRP_ATP_PVT.AtpRec;
330 
331 --Pegging ids used for deletion and hide/un-hide.
332 l_atp_peg_id                    NUMBER;
333 l_ctp_parent_peg_id             MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr();
334 l_steal_atp_peg_id              MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr();
335 l_forward_atp                   VARCHAR2(1); --5221865
336 --optional_fw end
337 l_org_collected                 NUMBER; --13032665
338 BEGIN
339 
340     IF PG_DEBUG in ('Y', 'C') THEN
341        msc_sch_wb.atp_debug('**********IN Schedule Procedure************');
342     END IF;
343     --initialize ATP_SET_FLAG TO NO --4460369
344     MSC_ATP_PVT.G_ATP_ITEM_PRESENT_IN_SET := 'N';
345     -- initialize API return status to success
346     x_return_status := FND_API.G_RET_STS_SUCCESS;
347 
348     MSC_ATP_PVT.G_RES_CONSUME := NVL(FND_PROFILE.value('MSC_ATP_RES_CONSUME'), 'Y');
349     IF PG_DEBUG in ('Y', 'C') THEN
350        msc_sch_wb.atp_debug('Schedule: ' || 'G_RES_CONSUME = '||MSC_ATP_PVT.G_RES_CONSUME);
351     END IF;
352 
353     MSC_ATP_PVT.G_PARTNER_ID := NULL;
354     MSC_ATP_PVT.G_PARTNER_SITE_ID := NULL;
355 
356     MSC_ATP_PVT.G_INSTANCE_ID := p_instance_id;
357 
358     IF MSC_ATP_PVT.G_PEGGING_FOR_SET.COUNT <> 0 THEN
359         MSC_ATP_PVT.G_PEGGING_FOR_SET.DELETE;
360         MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG.DELETE;
361         MSC_ATP_PVT.G_REQ_ATP_DATE.DELETE;
362         MSC_ATP_PVT.G_REQ_DATE_QTY.DELETE;
363          IF PG_DEBUG in ('Y', 'C') THEN
364             msc_sch_wb.atp_debug('Schedule: ' || 'Do I come here 15');
365          END IF;
366 
367     END IF;
368 
369     -- 2902265
370     IF l_fst_src_pegging_ids.COUNT <> 0 THEN
371         l_fst_src_pegging_ids.delete;
372         l_DEMAND_CLASS_ATP_FLAG.DELETE;
373         l_REQ_ATP_DATE.DELETE;
374         l_REQ_DATE_QTY.DELETE;
375         IF PG_DEBUG in ('Y', 'C') THEN
376             msc_sch_wb.atp_debug('Schedule: ' || 'Do I come here 15.0');
377         END IF;
378     END IF;
379 
380     --- BUG: 1777496
381     --- WE dont want to do Allocated ATP in case of ODS
382     --- Allocattion flag is set to 'NO' if it is ODS
383     IF  MSC_ATP_PVT.G_INV_CTP = 5 THEN
384 		MSC_ATP_PVT.G_ALLOCATED_ATP := 'N';
385 		MSC_ATP_PVT.G_ORIG_ALLOC_ATP := 'N'; --4925767
386 		MSC_ATP_PVT.G_HYBRID_ALLOC_ATP := 'N'; --ALLOC ATP CHANGES, 12973673
387     END IF;
388     -- we either have a line here or a set (arrival set or ship set)
389     -- this l_ship_count store the # of line for the ship set.
390 
391     -- if l_ship_count > 1 : ship set exists
392     -- if l_arrival_count > 1: arrival set exists
393 
394     l_stmt := 10;
395 
396     -- default the plan_id to -1 and adjust it later.
397 
398     l_plan_id := -1;
399 
400     IF PG_DEBUG in ('Y', 'C') THEN
401        msc_sch_wb.atp_debug('Schedule: ' || 'count :='|| p_atp_table.attribute_02.count);
402     END IF;
403     ---diag_atp: Enable diagnostic ATP only if calling module tells us to do so and for ATP inquiry only
404     ---Also Diag ATP is supported for PDS.
405     IF ((p_atp_table.Action(1) = ATPQUERY) AND (NVL(p_atp_table.attribute_02(1), 2) = 1)
406                                            AND (MSC_ATP_PVT.G_INV_CTP = 4)) THEN
407 
408        MSC_ATP_PVT.G_DIAGNOSTIC_ATP := 1;
409 
410     ELSE
411 
412        MSC_ATP_PVT.G_DIAGNOSTIC_ATP := 2;
413 
414     END IF;
415     IF PG_DEBUG in ('Y', 'C') THEN
416        msc_sch_wb.atp_debug('Schedule: ' || 'G_DIAGNOSTIC_ATP := ' || MSC_ATP_PVT.G_DIAGNOSTIC_ATP);
417     END IF;
418 
419     /************ Bug 2085071 Assignment set changes ************/
420     IF NVL(p_atp_table.Calling_Module(1), -1) = 724 THEN
421           IF PG_DEBUG in ('Y', 'C') THEN
422              msc_sch_wb.atp_debug('Schedule: ' || '724');
423           END IF;
424           lv_assign_set_id := p_assign_set_id;     -- Use new local variable
425     ELSE
426       -- convert the assignment set id from mrp to msc
427       IF PG_DEBUG in ('Y', 'C') THEN
428          msc_sch_wb.atp_debug('Schedule: ' || ' not 724');
429       END IF;
430 
431       -- Need to conver assignment set only if it is NOT NULL. If NULL, do nothing.
432       IF p_assign_set_id IS NOT NULL THEN
433 
434          IF PG_DEBUG in ('Y', 'C') THEN
435             msc_sch_wb.atp_debug('Schedule: p_atp_table.calling_module: ' || p_atp_table.calling_module(1));
436             msc_sch_wb.atp_debug('Schedule: p_atp_table.action: ' || p_atp_table.action(1));
437          END IF;
438 
439          --4567833 , in case of ATP inquiry from source with Org_id as NULL,
440          -- ATP UI is passing assignment set id of destination so no need to convert it.
441     /*     IF (p_atp_table.calling_module(1) is NULL AND p_atp_table.action(1) = 100) THEN
442 
443             lv_assign_set_id :=  p_assign_set_id;
444 
445             IF PG_DEBUG in ('Y', 'C') THEN
446                msc_sch_wb.atp_debug('Schedule: ' || 'source side ATP inquiry with Org_id as NULL');
447             END IF;
448 
449          ELSE   --4567833 end*/
450 		 -- Commented the code changes made in bug4567833  for the bug 12711659
451             begin
452               select assignment_set_id
453               into   lv_assign_set_id                  -- Use new local variable
454               from   msc_assignment_sets
455               where  sr_instance_id = p_instance_id
456               and    sr_assignment_set_id = p_assign_set_id;
457 
458             IF PG_DEBUG in ('Y', 'C') THEN
459                msc_sch_wb.atp_debug('Schedule: ' || 'source side ATP inquiry with Org_id as not NULL');
460 			   msc_sch_wb.atp_debug('Schedule: ' || 'lv_assign_set_id value converted = ' || lv_assign_set_id);
461             END IF;
462 
463             exception
464                when no_data_found then
465              -- Bug 1763731, need not do this check for the time being
466         /*
467              x_atp_table := p_atp_table;
468              x_atp_table.error_code(1) := NO_ASSIGNMENT_SET;
469              RAISE FND_API.G_EXC_ERROR ;
470         */
471                lv_assign_set_id := NULL;             -- Use new local variable
472             end;
473 
474         -- END IF;  -- Commented the code changes made in bug4567833  for the bug 12711659
475 
476       ELSE  --IF p_assign_set_id IS NOT NULL THEN
477         -- savirine added the following code so that ATP uses
478         -- "MSC: ATP Assignment Set" value when the
479         -- "MRP: ATP Assignment Set" value is NULL for
480         -- the ATP Requests originating from the Source ERP Instance.
481 	-- This is done to support region level sourcing as
482         -- only assignment set on APS can be used.
483 
484         -- lv_assign_set_id := NULL;              -- User new local variable
485         lv_assign_set_id := FND_PROFILE.value('MSC_ATP_ASSIGN_SET');
486       END IF;
487 
488       IF PG_DEBUG in ('Y', 'C') THEN
489          msc_sch_wb.atp_debug('Schedule: ' || 'after converting the assign set id');
490       END IF;
491     END IF;
492     l_assign_set_id  := lv_assign_set_id; -- Set the original local variable
493     /************ Bug 2085071 Assignment set changes ************/
494 
495     IF PG_DEBUG in ('Y', 'C') THEN
496        msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
497        msc_sch_wb.atp_debug('Schedule: ' || 'l_plan_id = '||l_plan_id);
498        msc_sch_wb.atp_debug('Schedule: ' || 'l_assign_set_id = '||l_assign_set_id);
499     END IF;
500 
501     IF p_atp_table.Ship_Set_Name(1) IS NOT NULL THEN
502       l_ship_count := p_atp_table.Action.COUNT;
503       l_arrival_count := 1;
504     ELSE
505       l_ship_count := 1;
506       l_arrival_count := p_atp_table.Action.COUNT;
507     END IF;
508 
509     -- since we may have a set of output (record of tables) for one single
510     -- line for complete scatp, we won't work on x_atp_table directly.
511     -- instead, we use l_atp_table to store this information and added this
512     -- to x_atp_table later
513 
514     /* Modularize Item and Org Info */
515     -- Initialize global variables.
516     G_ORG_INFO_REC        :=      l_org_attribute_rec;
517     G_ITEM_INFO_REC       :=      l_item_attribute_rec;
518     /* Modularize Item and Org Info */
519 
520     l_stmt := 20;
521     IF PG_DEBUG in ('Y', 'C') THEN
522        msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
523 
524        -- make sure we get the right record of tables
525        i := p_atp_table.Action.FIRST;
526        WHILE i IS NOT NULL LOOP
527           msc_sch_wb.atp_debug('Schedule: ' || 'Inventory_Item_Id:' || to_char(p_atp_table.Inventory_Item_Id(i))
528 	  || 'Source_Organization_Id:' || to_char(p_atp_table.Source_Organization_Id(i))
529           || 'Quantity_Ordered:' || to_char(p_atp_table.Quantity_Ordered(i))
530           || ' old_source organzation id : ' || p_atp_table.old_Source_Organization_Id(i));
531           i := p_atp_table.Action.NEXT(i);
532        END LOOP;
533     END IF;
534 
535     -- if the action is demandmodify, make sure we delete the original
536     -- demand information before we redemand.
537     -- so we won't double count the demand
538     -- we won't do any validation here due to ato impacts.
539 
540     l_stmt := 30;
541     IF PG_DEBUG in ('Y', 'C') THEN
542        msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
543        msc_sch_wb.atp_debug('Schedule: ' || 'G_SUMMARY_FLAG := ' || MSC_ATP_PVT.G_SUMMARY_FLAG);
544        msc_sch_wb.atp_debug('Schedule: ' || 'G_DATABASE_LINK := ' || MSC_ATP_PVT.G_DATABASE_LINK);
545        msc_sch_wb.atp_debug('Schedule: ' || 'G_ALLOCATED_ATP := ' || MSC_ATP_PVT.G_ALLOCATED_ATP);
546        msc_sch_wb.atp_debug('Schedule: ' || 'G_HP_DEMAND_BUCKETING_PREF := ' ||
547                                                         MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF);
548     END IF;
549     IF ((MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y') AND (MSC_ATP_PVT.G_DATABASE_LINK IS NULL) AND
550                                              (MSC_ATP_PVT.G_ALLOCATED_ATP = 'N')) THEN
551         --IF coming from planning server then we want to disable Summary mode for
552         -- instance which is distributed. If it is non distributed then G_DATABASE_LINK will
553         -- be null at this point as it is populated in CALL_SCHEDULE_REMOTE
554        --Bug3765793 adding trim functions to remove spaces from db_link
555        SELECT ltrim(rtrim(M2A_DBLINK)), summary_flag
556        INTO   MSC_ATP_PVT.G_DATABASE_LINK, l_summary_flag
557        FROM   msc_apps_instances
558        WHERE  instance_id = p_instance_id;
559 
560        ---bug 2301524:
561        --- 2301524: Summary mode is not supported for backlog workbench.
562        IF PG_DEBUG in ('Y', 'C') THEN
563           msc_sch_wb.atp_debug('Schedule: ' || 'calling_module := ' ||  p_atp_table.calling_module(1));
564           msc_sch_wb.atp_debug('Schedule: ' || ' l_summary_flag := ' || l_summary_flag);
565           msc_sch_wb.atp_debug('Schedule: ' || ' G_DATABASE_LINK := ' || NVL(MSC_ATP_PVT.G_DATABASE_LINK,'NULL'));
566        END IF;
567 
568        -- After summary enhancement summary ATP is supported for BWB as well - except for ODS case
569        IF NVL(p_atp_table.calling_module(1), -99) = -1 AND NVL(l_summary_flag, -1) <> 200 AND MSC_ATP_PVT.G_INV_CTP = 5 THEN
570              --- if backlog workbench is used then set the summary falg to 200
571              IF PG_DEBUG in ('Y', 'C') THEN
572                 msc_sch_wb.atp_debug('Schedule: ' || 'Site is using backlog workbench, update tables and turnoff summary for ODS case');
573              END IF;
574 
575              update  msc_apps_instances
576              set     summary_flag = 200;
577 
578              MSC_ATP_PVT.G_SUMMARY_FLAG := 'N';
579        ELSIF NVL(l_summary_flag, -1) = 200 AND MSC_ATP_PVT.G_INV_CTP = 5 THEN
580              --site is using backlog workbench, turn off the summary flag - only for ODS case - summary enhancement
581              IF PG_DEBUG in ('Y', 'C') THEN
582                 msc_sch_wb.atp_debug('Schedule: ' || 'Site is using backlog workbench, turnoff summary');
583              END IF;
584              MSC_ATP_PVT.G_SUMMARY_FLAG := 'N';
585        END IF;
586 
587     END IF;
588 
589     ----  After summary enhancement summary will be supported for distributed cases as well - except for ODS case
590     IF MSC_ATP_PVT.G_DATABASE_LINK IS NOT NULL AND MSC_ATP_PVT.G_INV_CTP = 5 THEN
591          MSC_ATP_PVT.G_SUMMARY_FLAG := 'N';
592          IF PG_DEBUG in ('Y', 'C') THEN
593             msc_sch_wb.atp_debug('Schedule: ' || 'Distributed scenario + ODS, turning summary flag off');
594          END IF;
595     END IF;
596     -- specify l_atp_table here so that for undemand we have a
597     -- place to store the error code.
598     -- this will be reset when we actually go into atp/demand
599 
600     l_atp_table := p_atp_table;
601 
602     /* bug3940999 Code moved to call_schedule_remote.
603     -- Code added for bug 2368426 to match source destination INV_CTP start
604 
605     IF MSC_ATP_PVT.G_INV_CTP_SOURCE <> -1 THEN
606 
607       IF MSC_ATP_PVT.G_INV_CTP_SOURCE <> MSC_ATP_PVT.G_INV_CTP THEN
608 
609         IF PG_DEBUG in ('Y', 'C') THEN
610            msc_sch_wb.atp_debug('Schedule: ' || 'Source INV_CTP ' || MSC_ATP_PVT.G_INV_CTP_SOURCE);
611            msc_sch_wb.atp_debug('Schedule: ' || 'Destination INV_CTP ' || MSC_ATP_PVT.G_INV_CTP);
612            msc_sch_wb.atp_debug('Schedule: ' || 'Source INV_CTP not in sync with destination ');
613         END IF;
614         -- apply the error code in l_atp_table
615 
616         FOR l_counter IN 1..l_atp_table.error_code.COUNT LOOP
617           IF l_counter=1 THEN
618             l_atp_table.error_code(l_counter) := MSC_ATP_PVT.INV_CTP_NOT_IN_SYNC;
619           ELSE
620             l_atp_table.error_code(l_counter) := MSC_ATP_PVT.GROUPEL_ERROR;
621           END IF;
622         END LOOP;
623         RAISE NO_DATA_FOUND;
624 
625       ELSE
626         IF PG_DEBUG in ('Y', 'C') THEN
627            msc_sch_wb.atp_debug('Schedule: ' || 'Source INV_CTP is in sync with destination ');
628         END IF;
629       END IF;
630 
631     END IF;
632 
633     -- Code added for bug 2368426 to match source destination INV_CTP ends
634     */
635 
636 
637     ---subst demand
638     --- after substitution we might have multiple demands for same order line.
639     --- Therefore we remember the demand id and inv id for the demand that we are manupulating
640     l_demand_ids.Extend(p_atp_table.action.count);
641     l_inv_item_ids.extend(p_atp_table.action.count);
642      l_req_arr_date.extend(p_atp_table.action.count);--for plan by request date
643     -- bug 2152184
644     IF MSC_ATP_PVT.G_INV_CTP = 4 THEN
645        -- we store the pf id and old org id. l_pf_item_id will contain either PF item id if we are doing
646        --- product family for the item or the inventory_item_id itself for regular item
647        ---we store these ids because of change in PF atp approach to do PF ATP on PF item level
648        -- Under new approach, for PF ATP, demand is stored on PF item level instead on member item level
649        -- Therefore to delete the demand, we need to find out the plan for PF
650 
651        --l_old_org_id.extend(p_atp_table.action.count);
652        l_pf_item_id.extend(p_atp_table.action.count);
653 
654        For m in 1..p_atp_table.action.count LOOP
655            IF  p_atp_table.ato_model_line_id(m) IS NOT NULL THEN
656                IF PG_DEBUG in ('Y', 'C') THEN
657                   msc_sch_wb.atp_debug('Schedule: p_atp_table.Identifier(m)' ||
658 	                                       p_atp_table.Identifier(m));
659                   msc_sch_wb.atp_debug('Schedule: ' ||
660 	                          'p_atp_table.Config_item_line_id(m)' ||
661 	                           p_atp_table.Config_item_line_id(m));
662                END IF;
663            END IF;
664            IF  p_atp_table.old_source_organization_id(m) IS NOT NULL THEN
665               l_pf_item_id(m) :=  MSC_ATP_PF.Get_PF_Atp_Item_Id(p_instance_id,
666                                                           l_plan_id,
667                                                            p_atp_table.inventory_item_id(m),
668                                                            p_atp_table.old_source_organization_id(m));
669               --l_old_org_id(m) := l_atp_table.old_source_organization_id(m);
670               IF PG_DEBUG in ('Y', 'C') THEN
671                  msc_sch_wb.atp_debug('Schedule: ' || 'Inventory item_id := ' || l_atp_table.inventory_item_id(m));
672                  msc_sch_wb.atp_debug('Schedule: ' || 'PF item id := ' || l_pf_item_id(m));
673               END IF;
674            END IF;
675        END LOOP;
676     END IF;
677 
678     -- remove data from table if records are already exists
679     -- ngoel 6/14, this is removed as OM shall be calling with
680     -- action as schedule while it needs to call as re-schedule.
681 
682     IF (p_atp_table.Action(1) = DEMANDMODIFY OR
683         p_atp_table.Action(1) = DEMANDADD OR
684         p_atp_table.Action(1) = DMDRSVADD) THEN --3720018
685 
686         --3720018, the code to call delete row is moved to call_delete_row.
687         -- Call_delete_row will take care of calling delete_row for the line/set.
688         -- The input to this procedure is p_atp_table.
689         -- l_delete_atp_rec will contain array to be used to call undo_delete_row.
690         -- l_delete_atp_rec also contains offset records.
691         MSC_ATP_DB_UTILS.call_delete_row (
692                          p_instance_id,
693                          p_atp_table,
694                          p_refresh_number,
695                          l_delete_atp_rec,
696                          l_return_status);
697 
698         IF PG_DEBUG in ('Y', 'C') THEN --3720018
699             msc_sch_wb.atp_debug('Schedule: l_return_status: ' || l_return_status);
700         END IF;
701 
702         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
703                         IF PG_DEBUG in ('Y', 'C') THEN
704                            msc_sch_wb.atp_debug('Schedule: ' || 'Error in Call_delete_row');
705                         END IF;
706                         x_return_status := FND_API.G_RET_STS_ERROR;
707                         RAISE FND_API.G_EXC_ERROR;
708         END IF;
709 
710        IF (l_delete_atp_rec.error_code(1) = MSC_ATP_PVT.TRY_ATP_LATER) OR
711           (l_delete_atp_rec.error_code(1) = MSC_ATP_PVT.SUMM_CONC_PROG_RUNNING) THEN
712            l_atp_table.error_code(1) := l_delete_atp_rec.error_code(1);
713            RAISE NO_DATA_FOUND;
714        END IF;
715     END IF;
716     IF PG_DEBUG in ('Y', 'C') THEN --3720018
717        msc_sch_wb.atp_debug('Schedule:l_delete_atp_rec.off_demand_instance_id.count ' || l_delete_atp_rec.off_demand_instance_id.count);
718        msc_sch_wb.atp_debug('Schedule:l_delete_atp_rec.off_supply_instance_id.count ' || l_delete_atp_rec.off_supply_instance_id.count);
719        msc_sch_wb.atp_debug('Schedule:l_delete_atp_rec.off_res_instance_id.count ' || l_delete_atp_rec.off_res_instance_id.count);
720        msc_sch_wb.atp_debug('Schedule: ' || 'l_old_plan_id.count = '||l_old_plan_id.count);
721     END IF;
722 
723     --3720018, error_code, attribute_07(plan_name) and old_plan_id are populated back to local variables.
724     l_atp_table.error_code   :=   l_delete_atp_rec.error_code;  --3720018
725     l_atp_table.attribute_07 :=   l_delete_atp_rec.attribute_07; --3720018
726     l_old_plan_id := l_delete_atp_rec.old_plan_id; --3720018
727 
728 
729     l_stmt := 40;
730     IF PG_DEBUG in ('Y', 'C') THEN
731        msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
732     END IF;
733 
734     -- bug 1382994: if action is demand, quantity should not be 0.
735     -- otherwise we also treat it as undemand.
736 
737     IF (((p_atp_table.Action(1) = ATPQUERY) or
738         (p_atp_table.Action(1) = DEMANDADD or
739           p_atp_table.Action(1) = DEMANDMODIFY)) AND
740            p_atp_table.Quantity_Ordered(1) <> 0) THEN -- 3720018, changed so that (p_atp_table.Quantity_Ordered(1) <> 0)
741                                                       -- condition is imposed always
742 
743 
744         l_sched_flag := BACKWARD_SCHEDULING;
745 
746    --     SAVEPOINT start_atp;
747 
748         -- get the profile option value
749         -- this profile option will determine what to deal with the output
750         -- will explain later.
751 
752         l_stmt := 50;
753         IF PG_DEBUG in ('Y', 'C') THEN
754            msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
755         END IF;
756 
757         -- cchen: disable complete scatp for now
758         -- l_complete_scatp := NVL(FND_PROFILE.value('MRP_COMPLETE_SCATP'), 'N');
759         -- l_complete_scatp := 'Y';
760         -- msc_sch_wb.atp_debug('l_complete_scatp = '||l_complete_scatp);
761 
762         -- we may need to loop through every supply source until we succeed
763         -- if action is atp, depending on profile option mrp_complete_scatp
764         -- we do things differently:
765         -- Action: ATP, mrp_complete_scatp = 'Y'
766         --   return the info in the preferred warehouse. if we cannot
767         --   satisfy the request from the preferred warehouse,  give information
768         --   from every warehouse and don't consider lastest_acceptable_date
769         -- (Action: ATP, mrp_complete_scatp = 'N') or
770         -- (Action : DemandADD/Demandmodify) :
771         --   return just one warehouse which satisfies the request,  or
772         --   if no warehouse satisfies the request, return preferred warehouse.
773 
774         -- k is the index of arrival set.  if no arrival set, it will always be 1.
775 
776         k := 1;
777 
778         l_stmt := 60;
779         IF PG_DEBUG in ('Y', 'C') THEN
780            msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
781         END IF;
782 
783         IF ((p_atp_table.action(k) <> ATPQUERY) OR
784              (p_atp_table.action(k) = ATPQUERY AND l_complete_scatp = 'N')) THEN
785             -- case 2
786             l_multi_scenario := 0;
787         ELSE
788             -- case 1
789             l_multi_scenario := 1;
790         END IF;
791 
792         -- loop through the items in the arrival set.
793         l_stmt := 70;
794         IF PG_DEBUG in ('Y', 'C') THEN
795            msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
796         END IF;
797 
798 
799         -- 1873918: PDS-ODS fix
800         -- introduce 2 variables to hold the global variable value.
801         -- we need to know the original values and then reset it each time.
802 
803         l_inv_ctp := MSC_ATP_PVT.G_INV_CTP;
804         l_allocated_atp := MSC_ATP_PVT.G_ALLOCATED_ATP;
805         l_allocation_method := NVL(MSC_ATP_PVT.G_ALLOCATION_METHOD, 2);
806         --5221865 If ODS ATP then set initial value as No else read whatever is set.
807         IF PG_DEBUG in ('Y', 'C') THEN
808            msc_sch_wb.atp_debug('Schedule: G_INV_CTP = '||G_INV_CTP);
809            msc_sch_wb.atp_debug('Schedule: G_FORWARD_ATP = '||G_FORWARD_ATP);
810         END IF;
811         IF MSC_ATP_PVT.G_INV_CTP = 4 THEN
812            l_forward_atp := MSC_ATP_PVT.G_FORWARD_ATP;
813         ELSE
814            l_forward_atp := 'N';
815         END IF;
816 
817         --  savirine, Sep 05, 2001:  call get_regions to get regions info which will be used for ATP request.
818 
819 msc_sch_wb.atp_debug('p_atp_table.delivery_lead_time: ' || p_atp_table.delivery_lead_time(1));
820 
821         IF (p_atp_table.Source_Organization_id(1) IS NULL or p_atp_table.ship_method(1) IS NULL or
822            p_atp_table.delivery_lead_time(1) IS NULL) and (p_atp_table.Calling_Module(1) = 724)
823 	   and (NVL(p_atp_table.Customer_Site_Id(1), -1) <> NVL(G_SR_PARTNER_SITE_ID, -1)) THEN
824 
825            MSC_SATP_FUNC.Get_Regions (
826                                p_customer_site_id       => p_atp_table.Customer_Site_Id(1),
827                                p_calling_module         => p_atp_table.Calling_Module(1),
828                                 -- i.e. Source (ERP) or Destination (724)
829                                p_instance_id            => p_instance_id,
830                                p_session_id             => MSC_ATP_PVT.G_SESSION_ID,
831                                p_dblink                 => NULL,
832                                x_return_status          => l_return_status );
833 
834            -- ngoel 9/27/2001 : Assigned values to G_SR_PARTNER_SITE_ID GLOBAL Variable
835            -- bug 2108880. Move the line outside of IF stmt
836            --G_SR_PARTNER_SITE_ID := p_atp_table.customer_site_id(1);
837 
838         END IF;
839         G_SR_PARTNER_SITE_ID := p_atp_table.customer_site_id(1);
840         --2814895, Global varaibles created for atp_sources called from atp_check
841         G_SR_PARTY_SITE_ID := p_atp_table.party_site_id(1);
842         G_SR_CUSTOMER_COUNTRY := p_atp_table.customer_country(1);
843 
844         IF PG_DEBUG in ('Y', 'C') THEN
845            msc_sch_wb.atp_debug('Schedule: ' || 'Get_Regions, return status : ' || l_return_status);
846         END IF;
847 
848         IF l_return_status = FND_API.G_RET_STS_ERROR THEN
849            -- something wrong so we want to rollback;
850            IF PG_DEBUG in ('Y', 'C') THEN
851               msc_sch_wb.atp_debug('Schedule: ' || 'expected error in Call to Get_Regions');
852            END IF;
853         ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR then
854            IF PG_DEBUG in ('Y', 'C') THEN
855               msc_sch_wb.atp_debug('Schedule: ' || 'something wrong in Call to Get_Regions');
856            END IF;
857            RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
858         END IF;
859 
860         -- dsting setproc
861         MSC_ATP_PROC.Initialize_Set_Processing(p_atp_table);
862         l_set_status := null;
863 
864         WHILE (k IS NOT NULL) LOOP
865 
866          -- for bug 1259416: we need to reset the plan_id and assign set
867          -- default the plan_id to -1 and adjust it later.
868 
869          -- 1873918: PDS-ODS fix
870          IF PG_DEBUG in ('Y', 'C') THEN
871             msc_sch_wb.atp_debug('Schedule: ' || 'Resetting G_INV_CTP and G_ALLOCATED_ATP' );
872          END IF;
873          MSC_ATP_PVT.G_INV_CTP := l_inv_ctp;
874          -- ATP4drp re-set using original profile value.
875          MSC_ATP_PVT.G_ALLOCATED_ATP := MSC_ATP_PVT.G_ORIG_ALLOC_ATP;
876          -- End ATP4drp
877          MSC_ATP_PVT.G_ALLOCATION_METHOD := l_allocation_method;
878          --5221865 Set original value for each line
879          MSC_ATP_PVT.G_FORWARD_ATP := l_forward_atp;
880          l_plan_id := -1;
881          IF NVL(p_atp_table.Calling_Module(1), -1) = 724 THEN
882            IF PG_DEBUG in ('Y', 'C') THEN
883               msc_sch_wb.atp_debug('Schedule: ' || '724');
884            END IF;
885            l_assign_set_id := p_assign_set_id;
886          ELSE
887            -- convert the assignment set id from mrp to msc
888            IF PG_DEBUG in ('Y', 'C') THEN
889               msc_sch_wb.atp_debug('Schedule: ' || ' not 724');
890            END IF;
891            /************ Bug 2085071 Assignment set changes ************/
892            /* Remove duplicate query
893            //begin
894            //  select assignment_set_id
895            //  into   l_assign_set_id
896            //  from   msc_assignment_sets
897            //  where  sr_instance_id = p_instance_id
898            //  and    sr_assignment_set_id = p_assign_set_id;
899 
900            //exception
901            //  when no_data_found then
902            //   l_assign_set_id := NULL;
903            //end;
904            */
905            l_assign_set_id := lv_assign_set_id; -- Use new variable instead
906            IF PG_DEBUG in ('Y', 'C') THEN
907               msc_sch_wb.atp_debug('Schedule: ' || 'l_assign_set_id = '||l_assign_set_id);
908            END IF;
909            /************ Bug 2085071 Assignment set changes ************/
910            IF PG_DEBUG in ('Y', 'C') THEN
911               msc_sch_wb.atp_debug('Schedule: ' || 'after converting the assign set id');
912            END IF;
913 
914          END IF;
915 
916          -- for each item in an arrival set, reset the variables
917          l_overall_status := -1;
918          l_requested_item_id := p_atp_table.Inventory_Item_Id(k);
919          l_process_org_id := p_atp_table.Source_Organization_Id(k);
920          l_request_org_id := p_atp_table.organization_id(k);
921 
922          IF PG_DEBUG in ('Y', 'C') THEN
923             msc_sch_wb.atp_debug('Schedule: ' || 'requesting org id := ' || l_request_org_id);
924          END IF;
925          -- for bug 1052654
926          -- l_atp_table := p_atp_table;
927 
928          -- 2902129 move here
929          -- l_atp_table will be the processing record of tables that
930          -- contains the requests and output.
931          -- For an arrival set, l_atp_table only contains info for the
932          -- process item so the length of each table in l_atp_table should
933          -- be one.
934          -- For an ship set, l_atp_table contains info for all items in
935          -- that ship set, so the length of each table will be the same as the
936          -- length from the table in p_atp_table.
937          -- Note, since currently this request is either for a line (which
938          -- we kind of treat it as a ship set with one item only), an
939          -- arrival set, or a ship set but not a combination of arrival
940          -- set and ship set.
941 
942          IF l_arrival_count > 1 THEN
943              l_stmt := 150;
944              IF PG_DEBUG in ('Y', 'C') THEN
945                 msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
946              END IF;
947 
948              -- arrival set exists
949              l_atp_table := x_atp_table;  -- this is to null out l_atp_table;
950              -- get the info for this process item and put into l_atp_table
951              MSC_SATP_FUNC.Assign_Atp_Input_Rec(p_atp_table, k,
952                                   l_atp_table, l_return_status );
953          ELSE
954              l_stmt := 160;
955              IF PG_DEBUG in ('Y', 'C') THEN
956                 msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
957              END IF;
958 
959              l_atp_table := p_atp_table;
960 
961          END IF;
962 
963          --IF MSC_ATP_PVT.G_INV_CTP = 5 THEN
964          -- Bug 4091487 Checking so_tbl_status in PDS ATP also.
965            -- if this is ods atp, we need to make sure planning is not doing
966            -- the complete refresh . populate error code if we are in the
967            -- the complete refresh.
968 
969            SELECT so_tbl_status,
970                   NVL(summary_flag, 1),
971                   apps_ver
972            INTO   l_so_tbl_status,
973                   l_summary_flag,
974                   MSC_ATP_PVT.G_APPS_VER          -- 2300767
975            FROM   msc_apps_instances
976            WHERE  instance_id = p_instance_id;
977 
978            IF PG_DEBUG in ('Y', 'C') THEN
979               msc_sch_wb.atp_debug('Schedule: ' || 'l_so_tbl_status = '||l_so_tbl_status);
980               msc_sch_wb.atp_debug('Schedule: ' || 'l_summary_flag = ' || l_summary_flag);
981               msc_sch_wb.atp_debug('Schedule: ' || 'MSC_ATP_PVT.G_APPS_VER = ' || MSC_ATP_PVT.G_APPS_VER);
982            END IF;  -- 2300767
983 
984 
985            IF NVL(l_so_tbl_status, 1) = 2 THEN
986              -- not available for atp
987              IF PG_DEBUG in ('Y', 'C') THEN
988                 msc_sch_wb.atp_debug('Schedule: ' || 'ATP not available');
989              END IF;
990 
991              l_atp_table.error_code(1) := TRY_ATP_LATER;
992              RAISE NO_DATA_FOUND;
993 
994              ELSIF l_summary_flag = 1 THEN
995                 ---- summary table is not ready. Switch to detail tables
996 		IF PG_DEBUG in ('Y', 'C') THEN
997 		   msc_sch_wb.atp_debug('Schedule: ' || ' summary table is not ready. Switch to detail tables');
998 		END IF;
999                 MSC_ATP_PVT.G_SUMMARY_FLAG := 'N';
1000 
1001              ELSIF l_summary_flag = 2 THEN
1002 
1003                 IF PG_DEBUG in ('Y', 'C') THEN
1004                    msc_sch_wb.atp_debug('Schedule: ' || 'Summary Concurrent program is running');
1005                 END IF;
1006                 l_atp_table.error_code(1) := SUMM_CONC_PROG_RUNNING;
1007                 RAISE NO_DATA_FOUND;
1008              END IF;
1009 
1010          --END IF;
1011 
1012          -- get the atp source information :
1013          -- if specified, use it. otherwise, get it from sourcing rules
1014 
1015          l_stmt := 80;
1016          IF PG_DEBUG in ('Y', 'C') THEN
1017             msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
1018          END IF;
1019 
1020 
1021          IF p_atp_table.Source_Organization_Id(k) is null  THEN
1022             -- get the source org or vendor site
1023 
1024             l_stmt := 90;
1025 
1026 /*
1027             IF (MSC_ATP_PVT.G_ASSIGN_SET_ID IS NULL) THEN
1028                 FND_MESSAGE.set_name('MRP','MRP_NO_ATP_ASSIGN_SET');
1029                 --  need to add error message
1030                 l_atp_table.Error_Code(k) := ATP_NO_ASSIGN_SET;
1031                 RAISE NO_DATA_FOUND;
1032             END IF;
1033 */
1034             -- if this is actually an ship set, then get the common
1035             -- warehouses for the whole ship set instead of this item.
1036 
1037             IF PG_DEBUG in ('Y', 'C') THEN
1038                msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
1039                msc_sch_wb.atp_debug('Schedule: ' || 'Before Atp_Sources');
1040             END IF;
1041 
1042             l_item_sourcing_info_rec.sr_inventory_item_id := l_atp_table.inventory_item_id;
1043             l_item_sourcing_info_rec.line_id := l_atp_table.identifier;
1044             l_item_sourcing_info_rec.ato_line_id := l_atp_table.ato_model_line_id;
1045             l_item_sourcing_info_rec.match_item_id := l_atp_table.match_item_id;
1046 
1047             --2814895
1048             IF (p_atp_table.customer_site_id(k) is not NULL ) THEN
1049                l_partner_type := 2;
1050             ELSIF ( p_atp_table.party_site_id(k) is not NULL) THEN
1051                l_partner_type := 4;
1052             ELSIF (p_atp_table.customer_country(k) is not NULL) THEN
1053                l_partner_type := 5;
1054             END IF;
1055 
1056             IF l_ship_count > 1 THEN
1057 
1058               l_stmt := 100;
1059               IF PG_DEBUG in ('Y', 'C') THEN
1060                  msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
1061               END IF;
1062 
1063 
1064               MSC_ATP_PROC.Atp_Sources(p_instance_id,
1065                                     l_plan_id,
1066                                     l_requested_item_id,
1067                                     p_atp_table.Organization_Id(k),
1068                                     p_atp_table.Customer_Id(k),
1069                                     p_atp_table.Customer_Site_Id(k),
1070                                     l_assign_set_id,
1071                                     --s_cto_rearch
1072                                     l_item_sourcing_info_rec,
1073 				    --p_atp_table.inventory_item_id,
1074                                     MSC_ATP_PVT.G_SESSION_ID,
1075                                     l_sources,
1076                                     l_return_status,
1077                                     l_partner_type, --2814895
1078                                     p_atp_table.party_site_id(k), --2814895
1079                                     p_atp_table.identifier(k));  --2814895
1080             ELSE
1081 
1082               l_stmt := 110;
1083     	      IF PG_DEBUG in ('Y', 'C') THEN
1084     	         msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
1085     	      END IF;
1086 
1087               MSC_ATP_PROC.Atp_Sources(p_instance_id,
1088                                     l_plan_id,
1089                                     l_requested_item_id,
1090                                     p_atp_table.Organization_Id(k),
1091                                     p_atp_table.Customer_Id(k),
1092                                     p_atp_table.Customer_Site_Id(k),
1093                                     l_assign_set_id,
1094                                     --s_cto_rearch
1095                                     l_item_sourcing_info_rec,
1096                                     --MRP_ATP_PUB.number_arr(NULL),
1097                                     MSC_ATP_PVT.G_SESSION_ID,
1098                                     l_sources,
1099                                     l_return_status,
1100                                     l_partner_type, --2814895
1101                                     p_atp_table.party_site_id(k), --2814895
1102                                     p_atp_table.identifier(k));  --2814895
1103             END IF;
1104 
1105             l_stmt := 120;
1106     	    IF PG_DEBUG in ('Y', 'C') THEN
1107     	       msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
1108                msc_sch_wb.atp_debug('Schedule: ' || 'After Atp_Sources');
1109                msc_sch_wb.atp_debug('Schedule: ' || 'l_sources.source_type.count = '||
1110                to_char(l_sources.source_type.count));
1111             END IF;
1112 
1113             -- check the return status
1114             IF l_return_status <> FND_API.G_RET_STS_SUCCESS OR
1115                 l_sources.source_type.count = 0  THEN
1116 
1117                 IF PG_DEBUG in ('Y', 'C') THEN
1118                    msc_sch_wb.atp_debug('Schedule: ' || 'atp source fails');
1119                 END IF;
1120                 x_return_status := FND_API.G_RET_STS_ERROR;
1121 
1122                 IF PG_DEBUG in ('Y', 'C') THEN
1123                    msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_table.Customer_Id.COUNT :='||l_atp_table.Customer_Id.COUNT);
1124                    msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_table.Error_Code.COUNT :='||l_atp_table.Error_Code.COUNT);
1125                 END IF;
1126                 --4091487 In case no records in msc_item_id_lid
1127                 --raise seperate error item not collected in case item not found in
1128 
1129                 IF l_return_status = MSC_ATP_PVT.G_ITEM_ID_NULL THEN
1130                    l_atp_table.error_code(k) := TRY_ATP_LATER;
1131                 ELSIF MSC_ATP_PVT.G_SR_INVENTORY_ITEM_ID IS NOT NULL or l_return_status = MSC_ATP_PVT.G_ITEM_NOT_COLL THEN
1132                      FOR l_count in 1..l_atp_table.error_code.count LOOP
1133                           IF l_atp_table.inventory_item_id(l_count) = MSC_ATP_PVT.G_SR_INVENTORY_ITEM_ID THEN
1134                               l_atp_table.error_code(l_count) := ATP_ITEM_NOT_COLLECTED;
1135                           END IF;
1136                      END LOOP;
1137                      --- set the variable back to null
1138                      MSC_ATP_PVT.G_SR_INVENTORY_ITEM_ID := null;
1139                 ELSE
1140 
1141                      l_atp_table.Error_Code(k):= ATP_NO_SOURCES;
1142                 END IF;
1143                 RAISE NO_DATA_FOUND;
1144             END IF;
1145 
1146          ELSE
1147             -- we know the org. just assign it to l_sources.
1148             -- assume it is a transfer as well
1149 
1150             l_stmt := 130;
1151     	    IF PG_DEBUG in ('Y', 'C') THEN
1152     	       msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
1153                msc_sch_wb.atp_debug('G_MODEL_IS_PRESENT_IN_SET := ' || MSC_ATP_CTO.G_MODEL_IS_PRESENT_IN_SET);
1154     	    END IF;
1155 
1156             --s_cto_rearch
1157             IF NVL(MSC_ATP_CTO.G_MODEL_IS_PRESENT_IN_SET, 2) = 1 THEN
1158 
1159                IF (l_sources.organization_id.count = 0) THEN  --bug#12617952
1160 				MSC_ATP_CTO.Extend_Sources_Rec(l_sources);
1161 			   END IF;
1162 
1163                --L_atp_table contains either then whole ship set or
1164                --it contains a particular line for arrival set with index K.
1165 
1166                l_sources.organization_id(1) := l_process_org_id;
1167                l_sources.instance_id(1) := p_instance_id;
1168 
1169                --load data in temp table if line > 1
1170                IF l_atp_table.inventory_item_id.count > 1 THEN
1171                   DELETE MSC_SHIP_SET_TEMP;
1172                   FORALL l_count in 1..p_atp_table.inventory_item_id.count
1173                            INSERT INTO MSC_SHIP_SET_TEMP(inventory_item_id,
1174                                                          line_id)
1175                                                          VALUES
1176                                                          (p_atp_table.inventory_item_id(l_count),
1177                                                           p_atp_table.identifier(l_count));
1178                   IF PG_DEBUG in ('Y', 'C') THEN
1179                       msc_sch_wb.atp_debug('Rows inserted in MSC_SHIP_SET_TEMP := ' || SQL%ROWCOUNT);
1180                   END IF;
1181                END IF;
1182 
1183 
1184 
1185                MSC_ATP_CTO.Validate_CTO_Sources(l_sources,
1186                                                 l_atp_table.identifier,
1187                                                 p_instance_id,
1188                                                 MSC_ATP_PVT.G_SESSION_ID,
1189                                                 l_return_status);
1190 
1191                IF l_return_status = MSC_ATP_PVT.CTO_OSS_ERROR THEN
1192                    --warehouse entered is not a valid warehouse for one of the models
1193                    l_atp_table.Error_Code(k):= MSC_ATP_PVT.INVALID_OSS_WAREHOUSE;
1194                    RAISE NO_DATA_FOUND;
1195                END IF;
1196 
1197             ELSE
1198                l_sources.Organization_Id:= MRP_ATP_PUB.number_arr(l_process_org_id);
1199                l_sources.Rank := MRP_ATP_PUB.number_arr(1);
1200                l_sources.Source_Type := MRP_ATP_PUB.number_arr(1);
1201                l_sources.Supplier_Id := MRP_ATP_PUB.number_arr(-1);
1202                l_sources.Supplier_Site_Id := MRP_ATP_PUB.number_arr(-1);
1203                l_sources.Preferred := MRP_ATP_PUB.number_arr(1);
1204             END IF;
1205             --e_cto_rearch
1206          END IF;
1207 
1208          l_stmt := 140;
1209     	 IF PG_DEBUG in ('Y', 'C') THEN
1210     	    msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
1211     	 END IF;
1212 
1213          -- go over each sources: j is the index for sources (warehouses)
1214          j := 1;
1215 
1216          /* 2902129 problem occurs if error is raised before we get here
1217           * so moved this chunk up before exception can be raised
1218          IF l_arrival_count > 1 THEN
1219              l_stmt := 150;
1220              IF PG_DEBUG in ('Y', 'C') THEN
1221                 msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
1222              END IF;
1223 
1224              -- arrival set exists
1225              l_atp_table := x_atp_table;  -- this is to null out l_atp_table;
1226              -- get the info for this process item and put into l_atp_table
1227              MSC_SATP_FUNC.Assign_Atp_Input_Rec(p_atp_table, k,
1228                                   l_atp_table, l_return_status );
1229          ELSE
1230              l_stmt := 160;
1231              IF PG_DEBUG in ('Y', 'C') THEN
1232                 msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
1233              END IF;
1234 
1235              l_atp_table := p_atp_table;
1236 
1237          END IF;
1238          */
1239 
1240          l_stmt := 170;
1241          IF PG_DEBUG in ('Y', 'C') THEN
1242             msc_sch_wb.atp_debug('Schedule: Stmt = ' ||l_stmt || ' : '||j || 'status : '|| l_overall_status);
1243          END IF;
1244 
1245          -- 2902265 do sane var initialization
1246          l_date_higher_src := 'N';
1247          l_arr_no_date := 'Y';
1248 
1249          WHILE (j IS NOT NULL) AND (l_overall_status <> ALLSUCCESS)  LOOP
1250 
1251           -- j is the index for sources
1252           ---- bug 1819638:  Get the next work day for sysdate
1253           IF (l_sources.Organization_Id(j) = -1) or (l_sources.Organization_Id(j) IS NULL) THEN
1254                  l_sourcing_org_id := l_request_org_id;
1255           ELSE
1256                  l_sourcing_org_id := l_sources.Organization_Id(j);
1257           END IF;
1258           --4386906
1259            l_ship_method := p_atp_table.ship_method(k);
1260            l_delivery_lead_time := p_atp_table.delivery_lead_time(k);
1261           --4386906
1262           IF PG_DEBUG in ('Y', 'C') THEN
1263              msc_sch_wb.atp_debug('Schedule: ' || 'l_sourcing_org_id :=' || l_sourcing_org_id);
1264              msc_sch_wb.atp_debug('Schedule: ' || 'p_instance_id := ' || p_instance_id);
1265              msc_sch_wb.atp_debug('Schedule: ' || 'l_ship_method := ' || l_ship_method); --4386906
1266              msc_sch_wb.atp_debug('Schedule: ' || 'l_delivery_lead_tim := ' || l_delivery_lead_time); --4386906
1267           END IF;
1268 
1269 	  /* ship_rec_cal
1270 	  -- ngoel 9/25/2001, wrapped since this function raises an exception which needs to be handled.
1271 	  BEGIN
1272               l_sys_next_date := MSC_CALENDAR.NEXT_WORK_DAY(l_sourcing_org_id,
1273                                                         p_instance_id,
1274                                                         1,
1275                                                         sysdate);
1276 	  EXCEPTION
1277 	      WHEN others THEN
1278 		   null;
1279 	  END;
1280 
1281           IF l_sys_next_date IS NULL THEN
1282                IF PG_DEBUG in ('Y', 'C') THEN
1283                   msc_sch_wb.atp_debug('Schedule: ' || 'Sysdate not found in Calendar');
1284                END IF;
1285                l_atp_table.error_code(1) := NO_MATCHING_CAL_DATE;
1286                RAISE NO_DATA_FOUND;
1287           END IF;
1288 
1289           l_stmt := 180;
1290           IF PG_DEBUG in ('Y', 'C') THEN
1291              msc_sch_wb.atp_debug('Schedule: ' || 'l_sys_next_date = ' || l_sys_next_date);
1292              msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
1293           END IF;*/
1294 
1295           -- initialize the group date in case it is a ship set
1296           -- Bug 2259824
1297 	  l_group_date := TRUNC(sysdate);--4460369 + MSC_ATP_PVT.G_END_OF_DAY;
1298 	  IF PG_DEBUG in ('Y', 'C') THEN
1299              msc_sch_wb.atp_debug('Schedule: 2259824 l_group_date = '||
1300 		to_char(l_group_date, 'DD-MON-YYYY HH24:MI:SS'));
1301           END IF;
1302 
1303           -- initialize the # of element success in a set.
1304           -- we will use this to decide if the whole set is successful or not.
1305           l_set_element_success := 0;
1306 
1307           -- dsting setproc
1308           -- 2902129
1309 --          if l_ship_count > 1 then
1310              MSC_ATP_PROC.Initialize_Set_Processing(l_atp_table);
1311              l_set_status := null;
1312 --          end if;
1313 
1314           -- i is the index for the record of tables in l_atp_table
1315           i := 1;
1316           WHILE (i IS NOT NULL)  LOOP
1317 
1318             l_stmt := 190;
1319 
1320             -- time_phased_atp - Reset variables
1321             l_time_phased_atp := 'N';
1322             l_pf_atp := 'N';
1323             l_atf_date := NULL;
1324             l_past_due_ship_date := 2; --bug4291375 resetting variable to no past due date
1325              --4386906
1326             l_atp_table.ship_method(i):=l_ship_method ;
1327             l_atp_table.delivery_lead_time(i) :=l_delivery_lead_time;
1328             --4386906
1329             -- 1873918: PDS-ODS fix
1330             IF PG_DEBUG in ('Y', 'C') THEN
1331     	       msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
1332                msc_sch_wb.atp_debug('Schedule: ' || 'Resetting G_INV_CTP and G_ALLOCATED_ATP' );
1333                msc_sch_wb.atp_debug('Schedule: Reinitialize global org/item/plan recs');
1334             END IF;
1335             MSC_ATP_PVT.G_INV_CTP := l_inv_ctp;
1336             -- ATP4drp re-set using original profile value
1337             MSC_ATP_PVT.G_ALLOCATED_ATP := MSC_ATP_PVT.G_ORIG_ALLOC_ATP;
1338             -- End ATP4drp
1339             MSC_ATP_PVT.G_ALLOCATION_METHOD := l_allocation_method;
1340             --5221865 Set original value for each line
1341             MSC_ATP_PVT.G_FORWARD_ATP := l_forward_atp;
1342 	    -- 2741997
1343 	    MSC_ATP_PVT.G_PLAN_INFO_REC := l_null_plan_info_rec;
1344 	    MSC_ATP_PVT.G_ITEM_INFO_REC := l_null_item_info_rec;
1345 	    MSC_ATP_PVT.G_ORG_INFO_REC :=  l_null_org_info_rec;
1346 
1347             --diag_atp: Reset the allocation rule name varibale as it may get reset.
1348             MSC_ATP_PVT.G_ALLOCATION_RULE_NAME := null;
1349             IF PG_DEBUG in ('Y', 'C') THEN
1350                msc_sch_wb.atp_debug('Schedule: ' || 'G_INV_CTP '||G_INV_CTP|| ' G_ALLOCATED_ATP '||G_ALLOCATED_ATP);
1351             END IF;
1352 
1353             -- ship_rec_cal
1354             -- MSC_ATP_PVT.G_CALENDAR_CONSTRAINT := 'N';
1355             -- Bug3593394 - No need to reset it here. If contraint is not there then this will be explicitly set to 'N'
1356 
1357             -- assign l_process_item_id.
1358             -- this is for product family atp.  when we do
1359             -- product family atp, make sure l_process_item_id is
1360             -- the product_family_item_id, and l_requested_item_id
1361             -- is the product fmily member.
1362 
1363             -- 1072816
1364             -- default the plan_id to -1 and adjust it later.
1365             -- since we know the org already, we don't need
1366             -- to default the assignment set
1367 
1368             l_plan_id := -1;
1369             --bug 2301524
1370             -- After summary enhancement Summary will be disabled only for ODS case
1371             IF  (
1372                     (
1373                         MSC_ATP_PVT.G_DATABASE_LINK IS NOT NULL -- (Distributed case or BWB)
1374                         OR l_summary_flag = 200                 -- and ODS
1375                         OR p_atp_table.calling_module(1) = -1
1376                     )
1377                     AND MSC_ATP_PVT.G_INV_CTP = 5
1378                 )
1379                 OR
1380                 (
1381                     MSC_ATP_PVT.G_INV_CTP = 4                   -- PDS and
1382                     AND MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y'       -- user defined allocated ATP
1383                     AND MSC_ATP_PVT.G_ALLOCATION_METHOD <> 1
1384                 ) THEN
1385                 IF PG_DEBUG in ('Y', 'C') THEN
1386                     msc_sch_wb.atp_debug('Schedule: ' || 'Distributed environment+BWB+ODS or rule-based-allocated-ATP+PDS, ' ||
1387                                          'always do ATP against details table');
1388                     msc_sch_wb.atp_debug('Schedule: ' || 'G_DATABASE_LINK := ' || MSC_ATP_PVT.G_DATABASE_LINK);
1389                     msc_sch_wb.atp_debug('Schedule: ' || 'G_ALLOCATED_ATP := ' || MSC_ATP_PVT.G_ALLOCATED_ATP);
1390                     msc_sch_wb.atp_debug('Schedule: ' || 'G_ALLOCATION_METHOD := ' || MSC_ATP_PVT.G_ALLOCATION_METHOD);
1391                     msc_sch_wb.atp_debug('Schedule: ' || 'l_summary_flag := ' || l_summary_flag);
1392                 END IF;
1393                 MSC_ATP_PVT.G_SUMMARY_FLAG := 'N';
1394             ELSIF MSC_ATP_PVT.G_INV_CTP = 4 THEN
1395                --- we re-read this summary profile option because it might get changed with plan
1396                MSC_ATP_PVT.G_SUMMARY_FLAG := NVL(FND_PROFILE.VALUE('MSC_ENABLE_ATP_SUMMARY'), 'N');
1397             END IF;
1398             IF PG_DEBUG in ('Y', 'C') THEN
1399                msc_sch_wb.atp_debug('Schedule: ' || 'MSC_ATP_PVT.G_SUMMARY_FLAG := ' || MSC_ATP_PVT.G_SUMMARY_FLAG);
1400             END IF;
1401 
1402             MSC_ATP_PVT.G_ORDER_LINE_ID := l_atp_table.Identifier(i);
1403             MSC_ATP_PVT.G_ASSEMBLY_LINE_ID := l_atp_table.Identifier(i);
1404             MSC_ATP_PVT.G_COMP_LINE_ID := l_atp_table.Identifier(i);
1405             MSC_ATP_PVT.G_DEMAND_PEGGING_ID := null;
1406             MSC_ATP_PVT.G_FIND_FUTURE_DATE := 'N';
1407 
1408             -- krajan : 2408902 - populate global variable (for use with CTO items)
1409             MSC_ATP_PVT.G_ATP_DEMAND_CLASS := l_atp_table.Demand_class(i);
1410             IF PG_DEBUG in ('Y', 'C') THEN
1411                msc_sch_wb.atp_debug('Schedule: ' || 'Setting global Demand_Class variable to : '|| MSC_ATP_PVT.G_ATP_DEMAND_CLASS );
1412             END IF;
1413 
1414 	    -- 9/24/2001, ngoel added to identify if current line is a MATO line, initializing here.
1415             MSC_ATP_PVT.G_CTO_LINE := 'N';
1416 
1417             l_stmt := 180;
1418 
1419             --6014992
1420             l_requested_item_id :=  nvl(l_atp_table.request_item_id(i), l_atp_table.inventory_item_id(i));
1421             -- l_process_org_id := l_atp_table.Source_Organization_Id(i);
1422             l_process_org_id := l_sources.Organization_Id(j);
1423 
1424             -- here we first to see if the item in ods is atpable or not.
1425             -- if not atpable, don't bother to look for plan_id
1426 
1427             -- set the variable to null before calling get_atp_flag
1428             MSC_ATP_PVT.G_SR_INVENTORY_ITEM_ID := null;
1429 
1430             /* Modularize Item and Org Info */
1431             -- Get item information into the local variable
1432             -- and assign it to the global.
1433             --s_cto_rearch
1434             l_item_attribute_rec.parent_pegging_id := null;
1435             l_item_attribute_rec.parent_bom_item_type := 4;
1436             l_item_attribute_rec.parent_comp_flag := null;
1437             l_item_attribute_rec.parent_repl_ord_flag := 'N';
1438             --e_cto_rearch
1439             MSC_ATP_PROC.get_item_attributes(p_instance_id,
1440                                          l_plan_id,
1441                                          l_requested_item_id,
1442                                          l_process_org_id,
1443                                          l_item_attribute_rec);
1444             G_ITEM_INFO_REC := l_item_attribute_rec;
1445             l_atp_flag  := G_ITEM_INFO_REC.atp_flag;
1446             l_atp_comp_flag := G_ITEM_INFO_REC.atp_comp_flag;
1447             /* Modularize Item and Org Info */
1448             /*l_atp_flag := MSC_ATP_FUNC.get_atp_flag(p_instance_id,
1449                                        l_plan_id,
1450                                        l_requested_item_id,
1451                                        l_process_org_id);
1452              Modularize Item and Org Info */
1453 
1454             -- Bug 1878093, if item not collected, but was ATPable on the source. If YES,
1455             -- raise error as in PDS plan won't be available and ATP can't be done in ODS as well.
1456 
1457 			-- Need to check for atp_components_flag as well and fail if either of them were not 'N' on source.
1458 
1459             IF MSC_ATP_PVT.G_SR_INVENTORY_ITEM_ID IS NOT NULL  AND
1460                (NVL(l_atp_table.attribute_06(i), 'N') <> 'N' OR NVL(l_atp_table.atp_components_flag(i), 'N') <> 'N') THEN
1461 
1462                IF PG_DEBUG in ('Y', 'C') THEN
1463                   msc_sch_wb.atp_debug('Schedule: ' || 'Item not found : ' || MSC_ATP_PVT.G_SR_INVENTORY_ITEM_ID);
1464                END IF;
1465 
1466                FOR l_count in 1..l_atp_table.error_code.count LOOP
1467                    IF l_atp_table.inventory_item_id(l_count) = MSC_ATP_PVT.G_SR_INVENTORY_ITEM_ID THEN
1468                       l_atp_table.error_code(l_count) := ATP_ITEM_NOT_COLLECTED;
1469                    END IF;
1470                END LOOP;
1471 
1472                -- set variable as null and raise exception, dont proceed for item/ ship set.
1473                MSC_ATP_PVT.G_SR_INVENTORY_ITEM_ID := null;
1474 
1475                RAISE NO_DATA_FOUND;
1476 
1477             END IF;
1478 
1479             IF PG_DEBUG in ('Y', 'C') THEN
1480                 msc_sch_wb.atp_debug('Schedule: ' || 'ship_method: ' || l_atp_table.ship_method(i));
1481                 msc_sch_wb.atp_debug('Schedule: ' || 'internal_org_id: ' || l_atp_table.internal_org_id(i)); --13032665
1482             END IF;
1483             -- dsting dlt
1484             --13032665 start check if the internal/destination org has been collected.
1485             IF (l_atp_table.internal_org_id(i) IS NOT NULL) THEN
1486                select count(*) into l_org_collected
1487                from msc_trading_partners
1488                where sr_tp_id = l_atp_table.internal_org_id(i)
1489                and sr_instance_id = p_instance_id
1490                and partner_type = 3;
1491 
1492                IF PG_DEBUG in ('Y', 'C') THEN
1493                   msc_sch_wb.atp_debug('Schedule: ' || 'l_org_collected: ' || l_org_collected);
1494                END IF;
1495                --Fall back on customer will be done if org is set to null
1496                IF l_org_collected = 0 THEN
1497                   l_atp_table.internal_org_id(i) := NULL;
1498                END IF;
1499             END IF;
1500             --13032665 end
1501             IF nvl(l_atp_table.delivery_lead_time(i), -1) = -1 THEN
1502                 -- Bug 3449812 - Base DLT on internal_org_id if available
1503                 -- Bug 3515520, don't use org in case customer/site is populated.
1504                 -- IF l_atp_table.internal_org_id(i) IS NOT NULL THEN
1505 
1506                 IF (l_atp_table.internal_org_id(i) IS NOT NULL) OR
1507                    (l_atp_table.organization_id(i) IS NOT NULL AND l_atp_table.customer_id(i) is NULL AND
1508                     l_atp_table.customer_site_id(1) IS NULL AND
1509                     l_atp_table.party_site_id(1) IS NULL AND --2814895
1510                     l_atp_table.customer_country(1) IS NULL) THEN  --2814895
1511                     IF PG_DEBUG in ('Y', 'C') THEN
1512                        msc_sch_wb.atp_debug('Schedule: ' || 'Inside Org code');
1513                     END IF;
1514                     MSC_ATP_PROC.get_delivery_lead_time(
1515                                 l_sourcing_org_id,
1516                                 NULL,		-- from location id
1517                                 p_instance_id,
1518                                 -- l_atp_table.internal_org_id(i), -- Bug 3515520
1519                                 NVL(l_atp_table.internal_org_id(i), l_atp_table.organization_id(i)), -- Bug 3515520
1520                                 NULL,           -- to location id
1521                                 p_instance_id,
1522                                 NULL, -- customer id
1523                                 NULL, -- customer site id
1524                                 NULL, -- supplier id
1525                                 NULL, -- supplier site id
1526                                 MSC_ATP_PVT.G_SESSION_ID, -- ???
1527                                 NULL, -- partner site id
1528                                 l_atp_table.ship_method(i),
1529                                 l_atp_table.delivery_lead_time(i),
1530                                 NULL,
1531                                 NULL,
1532                                 NULL
1533                                 );
1534 
1535                 ELSIF (l_atp_table.customer_site_id(1) IS NOT NULL) THEN
1536                 IF PG_DEBUG in ('Y', 'C') THEN
1537                    msc_sch_wb.atp_debug('Schedule: ' || 'Inside customer_site_id code');
1538                 END IF;
1539                     MSC_ATP_PROC.get_delivery_lead_time(
1540                                 l_sourcing_org_id,
1541                                 NULL,		-- from location id
1542                                 p_instance_id,
1543                                 --l_atp_table.organization_id(i), -- Bug 3515520
1544                                 NULL,           -- Bug 3515520
1545                                 NULL,           -- to location id
1546                                 p_instance_id,
1547                                 l_atp_table.customer_id(i),
1548                                 l_atp_table.customer_site_id(1),
1549                                 NULL,
1550                                 NULL,
1551                                 MSC_ATP_PVT.G_SESSION_ID, -- ???
1552                                 l_atp_table.customer_site_id(i),
1553                                 l_atp_table.ship_method(i),
1554                                 l_atp_table.delivery_lead_time(i),
1555                                 NULL,
1556                                 NULL,
1557                                 NULL
1558                                 );
1559 
1560                 ELSIF ( l_atp_table.party_site_id(1) IS NOT NULL ) THEN --2814895
1561                 IF PG_DEBUG in ('Y', 'C') THEN
1562                    msc_sch_wb.atp_debug('Schedule: ' || 'Inside party_site_id code');
1563                 END IF;
1564                     MSC_ATP_PROC.get_delivery_lead_time(
1565                                 l_sourcing_org_id,
1566                                 NULL,		-- from location id
1567                                 p_instance_id,
1568                                 --l_atp_table.organization_id(i), -- Bug 3515520
1569                                 NULL,           -- Bug 3515520
1570                                 NULL, --l_atp_table.location_id(1), --NULL, --2814895, to location id
1571                                 p_instance_id,
1572                                 NULL, --l_atp_table.customer_id(i), 2814895
1573                                 NULL, --l_atp_table.customer_site_id(1), 2814895
1574                                 NULL,
1575                                 NULL,
1576                                 MSC_ATP_PVT.G_SESSION_ID, -- ???
1577                                 NULL, --l_atp_table.customer_site_id(i),
1578                                 l_atp_table.ship_method(i),
1579                                 l_atp_table.delivery_lead_time(i),
1580                                 4, --2814895, partner_type for party_site_id
1581                                 l_atp_table.party_site_id(1), --2814895
1582                                 NULL
1583                                 );
1584 
1585                 ELSIF ( l_atp_table.customer_country(1) IS NOT NULL) THEN  --2814895
1586                 IF PG_DEBUG in ('Y', 'C') THEN
1587                    msc_sch_wb.atp_debug('Schedule: ' || 'Inside address parameter code');
1588                 END IF;
1589                     MSC_ATP_PROC.get_delivery_lead_time(
1590                                 l_sourcing_org_id,
1591                                 NULL,		-- from location id
1592                                 p_instance_id,
1593                                 --l_atp_table.organization_id(i), -- Bug 3515520
1594                                 NULL,           -- Bug 3515520
1595                                 NULL,           -- to location id
1596                                 p_instance_id,
1597                                 NULL, --l_atp_table.customer_id(i), 2814895
1598                                 NULL, --l_atp_table.customer_site_id(1), 2814895
1599                                 NULL,
1600                                 NULL,
1601                                 MSC_ATP_PVT.G_SESSION_ID, -- ???
1602                                 NULL, --l_atp_table.customer_site_id(i), 2814895
1603                                 l_atp_table.ship_method(i),
1604                                 l_atp_table.delivery_lead_time(i),
1605                                 5, --2814895 , partner_type for address parameters
1606                                 NULL, --2814895
1607                                 l_atp_table.identifier(i));  --2814895
1608                 END IF;
1609             END IF;
1610 
1611             l_atp_table.delivery_lead_time(i) := CEIL(l_atp_table.delivery_lead_time(i));
1612             l_delivery_lead_time := l_atp_table.delivery_lead_time(i);
1613             l_ship_method := l_atp_table.ship_method(i);
1614 
1615             -- moved this here for ship_rec_cal
1616             IF l_atp_table.customer_id(i) IS NOT NULL THEN
1617                 -- bug3593394 - Run this SQL only if customer has changed.
1618                 IF (i=1) OR (l_atp_table.customer_id(i) <> NVL(l_atp_table.customer_id(i-1),-1)) THEN
1619                     BEGIN
1620                         SELECT TP_ID
1621                         INTO   MSC_ATP_PVT.G_PARTNER_ID
1622                         FROM   msc_tp_id_lid tp
1623                         WHERE  tp.SR_TP_ID = l_atp_table.customer_id(i)
1624                         AND    tp.SR_INSTANCE_ID = p_instance_id
1625                         AND    tp.PARTNER_TYPE = 2;
1626                     EXCEPTION
1627                         WHEN NO_DATA_FOUND THEN
1628                             MSC_ATP_PVT.G_PARTNER_ID := NULL;
1629                     END ;
1630                 END IF;
1631             ELSE
1632                 -- bug3593394 - reset to NULL
1633                 MSC_ATP_PVT.G_PARTNER_ID := NULL;
1634             END IF;
1635 
1636             IF PG_DEBUG in ('Y', 'C') THEN
1637                 msc_sch_wb.atp_debug('Schedule: ' || 'G_PARTNER_ID = '||MSC_ATP_PVT.G_PARTNER_ID);
1638             END IF;
1639 
1640             IF l_atp_table.customer_site_id(i) IS NOT NULL THEN
1641                 -- bug3593394 - Run this SQL only if customer site has changed.
1642                 IF (i=1) OR (l_atp_table.customer_site_id(i) <> NVL(l_atp_table.customer_site_id(i-1),-1)) THEN
1643                     BEGIN
1644                         SELECT TP_SITE_ID
1645                         INTO   MSC_ATP_PVT.G_PARTNER_SITE_ID
1646                         FROM   msc_tp_site_id_lid tpsite
1647                         WHERE  tpsite.SR_TP_SITE_ID = l_atp_table.customer_site_id(i)
1648                         AND    tpsite.SR_INSTANCE_ID =  p_instance_id
1649                         AND    tpsite.PARTNER_TYPE = 2;
1650                     EXCEPTION
1651                         WHEN NO_DATA_FOUND THEN
1652                             MSC_ATP_PVT.G_PARTNER_SITE_ID := NULL;
1653                     END ;
1654                 END IF;
1655             ELSE
1656                 -- bug3593394 - reset to NULL
1657                 MSC_ATP_PVT.G_PARTNER_SITE_ID := NULL;
1658             END IF;
1659 
1660             IF PG_DEBUG in ('Y', 'C') THEN
1661                 msc_sch_wb.atp_debug('Schedule: ' || 'G_PARTNER_SITE_ID = '||MSC_ATP_PVT.G_PARTNER_SITE_ID);
1662             END IF;
1663 
1664             -- ship_rec_cal changes begin
1665             IF PG_DEBUG in ('Y', 'C') THEN
1666                msc_sch_wb.atp_debug('**************** Calendar Codes ******************************');
1667                msc_sch_wb.atp_debug('*  ___________________Input_____________________');
1668                msc_sch_wb.atp_debug('*  ');
1669                msc_sch_wb.atp_debug('*  Source Cust ID          : '|| l_atp_table.customer_id(i) );
1670                msc_sch_wb.atp_debug('*  Partner ID              : '|| MSC_ATP_PVT.G_PARTNER_ID );
1671                msc_sch_wb.atp_debug('*  Partner Site ID         : '|| MSC_ATP_PVT.G_PARTNER_SITE_ID );
1672                msc_sch_wb.atp_debug('*  Ship Method             : '|| l_atp_table.ship_method(i) );
1673                msc_sch_wb.atp_debug('*  Org ID                  : '|| l_atp_table.organization_id(i) );
1674                msc_sch_wb.atp_debug('*  Internal Org ID         : '|| l_atp_table.internal_org_id(i) );
1675                msc_sch_wb.atp_debug('*  Source Org ID           : '|| l_sourcing_org_id );
1676             END IF;
1677             -- Bug 3449812 - get receiving cal from ISO's destination org if available
1678             -- Bug 3515520, don't use org in case customer/site is populated.
1679             -- IF NVL(l_atp_table.internal_org_id(i), l_atp_table.organization_id(i))  IS NOT NULL THEN
1680 
1681             -- Bug 3647208 -- Move the check to individual "IF"s
1682             -- IF MSC_ATP_PVT.G_USE_SHIP_REC_CAL='Y' THEN
1683 
1684             IF (l_atp_table.internal_org_id(i) IS NOT NULL) OR
1685                 (l_atp_table.organization_id(i) IS NOT NULL AND l_atp_table.customer_id(i) is NULL AND
1686                 MSC_ATP_PVT.G_PARTNER_SITE_ID IS NULL) THEN
1687                 -- Bug3593394 - Call this proc only if parameters have changed
1688                 IF (i=1) OR (l_atp_table.internal_org_id(i)<>l_atp_table.internal_org_id(i-1))
1689                     OR (l_atp_table.organization_id(i)<>l_atp_table.organization_id(i-1))
1690                     OR (l_atp_table.ship_method(i)<>l_atp_table.ship_method(i-1)) THEN
1691 
1692                     l_receiving_cal_code := MSC_CALENDAR.Get_Calendar_Code(
1693                                                      p_instance_id,
1694                                                      null,
1695                                                      null,
1696                                                      null,
1697                                                      null,
1698                                                      null,
1699                                                      NVL(l_atp_table.internal_org_id(i), l_atp_table.organization_id(i)),
1700                                                      l_atp_table.ship_method(i),
1701                                                      MSC_CALENDAR.ORC);
1702                 ELSE
1703                     l_receiving_cal_code := l_atp_table.receiving_cal_code(i-1);
1704                     IF PG_DEBUG in ('Y', 'C') THEN
1705                         msc_sch_wb.atp_debug('Schedule: ' || 'Calendar parameters have not changed for ORC');
1706                     END IF;
1707                 END IF;
1708             ELSIF l_atp_table.customer_id(i) IS NOT NULL AND MSC_ATP_PVT.G_USE_SHIP_REC_CAL='Y' THEN
1709                 -- Bug 3647208 - Call CRC only if using ship/rec cal
1710                 -- receiving party is customer
1711                 -- Bug3593394 - Call this proc only if parameters have changed
1712                 IF (i=1) OR (NVL(l_atp_table.customer_id(i),-1)<>NVL(l_atp_table.customer_id(i-1),-1))
1713                     OR (NVL(l_atp_table.customer_site_id(i),-1)<>NVL(l_atp_table.customer_site_id(i-1),-1))
1714                     OR (l_atp_table.ship_method(i)<>l_atp_table.ship_method(i-1)) THEN
1715 
1716                     l_receiving_cal_code := MSC_CALENDAR.Get_Calendar_Code(
1717                                                      p_instance_id,
1718                                                      null,
1719                                                      null,
1720                                                      MSC_ATP_PVT.G_PARTNER_ID,
1721                                                      MSC_ATP_PVT.G_PARTNER_SITE_ID,
1722                                                      2,
1723                                                      null,
1724                                                      l_atp_table.ship_method(i),
1725                                                      MSC_CALENDAR.CRC);
1726                 ELSE
1727                     l_receiving_cal_code := l_atp_table.receiving_cal_code(i-1);
1728                     IF PG_DEBUG in ('Y', 'C') THEN
1729                         msc_sch_wb.atp_debug('Schedule: ' || 'Calendar parameters have not changed for CRC');
1730                     END IF;
1731                 END IF;
1732             ELSE
1733                 -- Neither org nor customer provided
1734                 l_receiving_cal_code := MSC_CALENDAR.FOC;
1735             END IF;
1736 
1737             IF MSC_ATP_PVT.G_USE_SHIP_REC_CAL='Y' THEN
1738                 -- Bug 3647208 -- Move the check to individual "IF"s
1739                 IF (i=1) OR (l_atp_table.ship_method(i)<>l_atp_table.ship_method(i-1)) THEN
1740                     -- Bug3593394 - Call this proc only if parameters have changed
1741                     l_intransit_cal_code := MSC_CALENDAR.Get_Calendar_Code(
1742                                              p_instance_id,
1743                                              null,
1744                                              null,
1745                                              null,
1746                                              null,
1747                                              4,
1748                                              null,
1749                                              l_atp_table.ship_method(i),
1750                                              MSC_CALENDAR.VIC);
1751                 ELSE
1752                     l_intransit_cal_code := l_atp_table.intransit_cal_code(i-1);
1753                     IF PG_DEBUG in ('Y', 'C') THEN
1754                         msc_sch_wb.atp_debug('Schedule: ' || 'Calendar parameters have not changed for VIC');
1755                     END IF;
1756                 END IF;
1757             ELSE
1758                 l_intransit_cal_code := MSC_CALENDAR.FOC;
1759             END IF;
1760 
1761 
1762             -- Bug 3647208 -- Moved OSC and VIC to different IF blocks
1763             IF (i=1) OR (l_atp_table.ship_method(i)<>l_atp_table.ship_method(i-1)) THEN
1764                 -- Bug3593394 - Call this proc only for the first line in the ship set unless ship method has changed
1765                 --              No check on source org as "i" will be greater than one only for ship set.
1766                 l_shipping_cal_code := MSC_CALENDAR.Get_Calendar_Code(
1767                                              p_instance_id,
1768                                              null,
1769                                              null,
1770                                              null,
1771                                              null,
1772                                              null,
1773                                              l_sourcing_org_id,
1774                                              l_atp_table.ship_method(i),
1775                                              MSC_CALENDAR.OSC);
1776             ELSE
1777                 l_shipping_cal_code := l_atp_table.shipping_cal_code(i-1);
1778                 IF PG_DEBUG in ('Y', 'C') THEN
1779                     msc_sch_wb.atp_debug('Schedule: ' || 'Calendar parameters have not changed for OSC');
1780                 END IF;
1781             END IF;
1782 
1783 
1784             IF (i=1) THEN
1785                 -- Bug3593394 - Call this proc only for the first line in the ship set.
1786                 --              "i" will be greater than one only for ship set.
1787                 -- ATP4drp Call to obtain get_org_defaults.
1788                 /*
1789                 l_manufacturing_cal_code := MSC_CALENDAR.Get_Calendar_Code(
1790                                              p_instance_id,
1791                                              null,
1792                                              null,
1793                                              null,
1794                                              null,
1795                                              null,
1796                                              l_sourcing_org_id,
1797                                              null,
1798                                              MSC_CALENDAR.OMC);
1799                */
1800                -- Bug 2783787 fix involving  removing the call to obtain org defaults
1801                -- restored here
1802                MSC_ATP_PROC.get_global_org_info(p_instance_id,
1803                                                 l_process_org_id);
1804                l_manufacturing_cal_code := MSC_ATP_PVT.G_ORG_INFO_REC.cal_code;
1805                -- End ATP4drp
1806             ELSE
1807                 l_manufacturing_cal_code := l_atp_table.manufacturing_cal_code(i-1);
1808                 IF PG_DEBUG in ('Y', 'C') THEN
1809                     msc_sch_wb.atp_debug('Schedule: ' || 'Calendar parameters have not changed for OMC');
1810                 END IF;
1811             END IF;
1812 
1813             IF PG_DEBUG in ('Y', 'C') THEN
1814                msc_sch_wb.atp_debug('  ');
1815                msc_sch_wb.atp_debug('*  ___________________Output____________________');
1816                msc_sch_wb.atp_debug('*  ');
1817                msc_sch_wb.atp_debug('*  Receiving calendar code         : ' || l_receiving_cal_code);
1818                msc_sch_wb.atp_debug('*  Intransit calendar code         : ' || l_intransit_cal_code);
1819                msc_sch_wb.atp_debug('*  Shipping calendar code          : ' || l_shipping_cal_code);
1820                msc_sch_wb.atp_debug('*  Manufacturing calendar code     : ' || l_manufacturing_cal_code);
1821                msc_sch_wb.atp_debug('**************************************************************');
1822             END IF;
1823 
1824             -- Add the calendar codes to l_atp_table
1825             l_atp_table.receiving_cal_code(i) := l_receiving_cal_code;
1826             l_atp_table.intransit_cal_code(i) := l_intransit_cal_code;
1827             l_atp_table.shipping_cal_code(i) := l_shipping_cal_code;
1828             l_atp_table.manufacturing_cal_code(i) := l_manufacturing_cal_code;
1829 
1830 	    /* Moved l_sys_next_date calculation code here */
1831 	    BEGIN
1832                 -- Bug 3371817 - sys_next_date should be as per OSC
1833                 --               maintaining l_sys_next_omc_date for PTF assignment.
1834                 -- Bug3593394 - Call this proc only if calendar has changed
1835                 IF (i=1) OR (l_shipping_cal_code<>l_atp_table.shipping_cal_code(i-1)) THEN
1836                     l_sys_next_date := MSC_CALENDAR.NEXT_WORK_DAY(
1837                                                         l_shipping_cal_code,
1838                                                         p_instance_id,
1839                                                         l_trunc_sysdate);
1840                 END IF;
1841 
1842                 IF l_manufacturing_cal_code=l_shipping_cal_code THEN
1843                     l_sys_next_omc_date := l_sys_next_date;
1844                 ELSIF (i=1) OR (l_manufacturing_cal_code<>l_atp_table.manufacturing_cal_code(i-1)) THEN
1845                     -- Bug3593394 - Call this proc only if calendar has changed
1846                     l_sys_next_omc_date := MSC_CALENDAR.NEXT_WORK_DAY(
1847                                                         l_manufacturing_cal_code,
1848                                                         p_instance_id,
1849                                                         l_trunc_sysdate);
1850 	        END IF;
1851 
1852 	        --bug3439591
1853 	        -- Bug3593394 - Call this proc only if calendar has changed
1854 	        IF l_receiving_cal_code=l_shipping_cal_code THEN
1855 	            l_sysdate_orc := l_sys_next_date;
1856 	        ELSIF l_receiving_cal_code=l_manufacturing_cal_code THEN
1857 	            l_sysdate_orc := l_sys_next_omc_date;
1858 	        ELSIF (i=1) OR (l_receiving_cal_code<>l_atp_table.receiving_cal_code(i-1)) THEN
1859 	            l_sysdate_orc := MSC_CALENDAR.NEXT_WORK_DAY(
1860                                                         l_receiving_cal_code,
1861                                                         p_instance_id,
1862                                                         l_trunc_sysdate);
1863                 END IF;
1864 	    EXCEPTION
1865                 WHEN others THEN
1866                    null;
1867 	    END;
1868 
1869             IF PG_DEBUG in ('Y', 'C') THEN
1870                 msc_sch_wb.atp_debug('Schedule: ' || 'l_sys_next_date = ' || l_sys_next_date);
1871                 msc_sch_wb.atp_debug('Schedule: ' || 'l_sysdate_orc = ' || l_sysdate_orc); --bug3439591
1872 				msc_sch_wb.atp_debug('Schedule: ' || 'l_sys_next_omc_date = ' || l_sys_next_omc_date);
1873                 msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
1874             END IF;
1875 
1876             IF l_sys_next_date IS NULL THEN
1877                 IF PG_DEBUG in ('Y', 'C') THEN
1878                   msc_sch_wb.atp_debug('Schedule: ' || 'Sysdate not found in Calendar');
1879                 END IF;
1880                 l_atp_table.error_code(1) := NO_MATCHING_CAL_DATE;
1881                 RAISE NO_DATA_FOUND;
1882             END IF;
1883 	    -- ship_rec_cal changes end
1884 
1885             --- 2178544: We assign it to null so that this parameter set for last item is not used for current item.
1886             -- Bug 3512996 - For top level G_PTF_DATE should be as per OSC
1887             -- MSC_ATP_PVT.G_PTF_DATE := l_sys_next_omc_date;
1888             MSC_ATP_PVT.G_PTF_DATE := l_sys_next_date;
1889 
1890             --- bug 2152184: Moved this call here so that PF item could be determined based on ODS data
1891             IF (MSC_ATP_PVT.G_INV_CTP = 5) OR
1892                  (MSC_ATP_PVT.G_INV_CTP = 4 AND
1893                      NVL(l_atp_table.old_source_organization_id(i), -1) <> l_process_org_id ) THEN
1894                   IF PG_DEBUG in ('Y', 'C') THEN
1895                      msc_sch_wb.atp_debug('Schedule: ' || 'Either old source ors is null or is not same as l_process_org_id');
1896                      msc_sch_wb.atp_debug('Schedule: ' || 'Old org :=' || l_atp_table.old_source_organization_id(i));
1897                   END IF;
1898                   --- we get PF item id only if its a case of new schedulling
1899                   -- if its a reschedulling then we mayalready have got the id just for deleting
1900                   ---the previous order. For reschedulling we handel it in the elase part
1901                   l_process_item_id := MSC_ATP_PF.Get_PF_Atp_Item_Id(p_instance_id,
1902                                                                        l_plan_id,
1903                                                                        l_requested_item_id,
1904                                                                        l_process_org_id);
1905             ELSE
1906                 IF l_arrival_count > 1 THEN
1907                      IF PG_DEBUG in ('Y', 'C') THEN
1908                         msc_sch_wb.atp_debug('Schedule: ' || 'Arrival Set');
1909                         msc_sch_wb.atp_debug('Schedule: ' || 'l_pf_item_id(k) := ' || l_pf_item_id(k));
1910                      END IF;
1911                      l_process_item_id := l_pf_item_id(k);
1912                 ELSE
1913                      IF PG_DEBUG in ('Y', 'C') THEN
1914                         msc_sch_wb.atp_debug('Schedule: ' || 'Ship Set');
1915                         msc_sch_wb.atp_debug('Schedule: ' || 'l_pf_item_id(i) := ' || l_pf_item_id(i));
1916                      END IF;
1917                      l_process_item_id := l_pf_item_id(i);
1918                 END IF;
1919             END IF;
1920             IF PG_DEBUG in ('Y', 'C') THEN
1921                msc_sch_wb.atp_debug('Schedule: ' || 'l_process_item_id := ' || l_process_item_id);
1922             END IF;
1923 
1924             IF (l_process_item_id <> l_requested_item_id) and (l_atp_flag = 'N') then
1925                   --For Product family based ATP we will flag an error if request comes for a
1926                   -- member item which has ATP flag = 'N'. For other members we will continue as usual
1927                   IF PG_DEBUG in ('Y', 'C') THEN
1928                      msc_sch_wb.atp_debug('Schedule: ' || 'Member item is not atpable');
1929                   END IF;
1930                   x_return_status := FND_API.G_RET_STS_ERROR;
1931                   l_atp_table.Error_Code(i):= PF_MEMBER_ITEM_NOT_ATPABLE;
1932                   RAISE NO_DATA_FOUND;
1933             END IF;
1934             -- since we know the org now, we can get the plan_id if it is PDS
1935 
1936             IF MSC_ATP_PVT.G_INV_CTP = 4 THEN
1937               /* Modularize Item and Org Info */
1938               -- Information already obtained
1939               /*l_atp_comp_flag := MSC_ATP_FUNC.get_atp_comp_flag(p_instance_id,
1940                                         l_plan_id,
1941                                         l_requested_item_id,
1942                                         l_process_org_id);
1943                Modularize Item and Org Info */
1944 
1945 
1946               IF l_atp_flag <> 'N' OR l_atp_comp_flag <> 'N' THEN
1947                 /*
1948 		MSC_ATP_PROC.Get_Plan_Info(p_instance_id,
1949                             --2152184
1950                             l_process_item_id,
1951                             --l_requested_item_id,
1952    			    l_process_org_id,
1953                             l_atp_table.demand_class(i),
1954                             l_plan_id,
1955                             l_assign_set_id);
1956 
1957 		-- code changes for bug 2392456 starts
1958                  MSC_ATP_PROC.Get_Plan_Info(p_instance_id,
1959                             --2152184
1960                             l_process_item_id,
1961                             --l_requested_item_id,
1962                             l_process_org_id,
1963                             l_atp_table.demand_class(i),
1964                             l_plan_info_rec);
1965                 */
1966                 --s_cto_reach
1967                 l_plan_found_for_match := 2;
1968                 IF PG_DEBUG in ('Y', 'C') THEN
1969                      msc_sch_wb.atp_debug('Schedule: matc_id.count := ' || l_atp_table.match_item_id.count);
1970                      msc_sch_wb.atp_debug('Schedule: bom_item_type count := ' || l_atp_table.bom_item_type.count);
1971                      msc_sch_wb.atp_debug('match item is := ' || l_atp_table.match_item_id(i));
1972                 END IF;
1973                 IF l_atp_table.match_item_id(i) is not null and l_atp_table.bom_item_type(i) = 1 THEN
1974 
1975                    IF PG_DEBUG in ('Y', 'C') THEN
1976                       msc_sch_wb.atp_debug('Match Found, find plan for  matched item');
1977                    END IF;
1978 
1979                    /* time_phased_atp changes begin*/
1980                    l_match_item_family_id := MSC_ATP_PF.Get_PF_Atp_Item_Id(
1981                                                   p_instance_id,
1982                                                   l_plan_id,
1983                                                   l_atp_table.match_item_id(i),
1984                                                   l_process_org_id
1985                                              );
1986 
1987                    /* Call new procedure Get_PF_Plan_Info*/
1988                    MSC_ATP_PF.Get_PF_Plan_Info(
1989                                p_instance_id,
1990                                l_atp_table.match_item_id(i),
1991                                l_match_item_family_id,
1992                                l_process_org_id,
1993                                l_atp_table.demand_class(i),
1994                                l_atf_date,
1995                                l_atp_table.error_code(i),
1996                                l_return_status,
1997                                NULL --bug3510475
1998                    );
1999 
2000                    IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
2001                         IF PG_DEBUG in ('Y', 'C') THEN
2002                            msc_sch_wb.atp_debug('Schedule: ' || 'ATP Downtime during Re-schedule');
2003                         END IF;
2004                         x_return_status := FND_API.G_RET_STS_ERROR;
2005                         RAISE FND_API.G_EXC_ERROR;
2006                    END IF;
2007 
2008                    IF l_atf_date is not null THEN
2009                        l_time_phased_atp := 'Y';
2010                    ELSIF l_match_item_family_id <> l_atp_table.match_item_id(i) THEN
2011                        l_pf_atp := 'Y';
2012                    END IF;
2013                    /* time_phased_atp changes end*/
2014 
2015                    -- ATP4drp changes begin
2016                    -- No changes here since DRP plan should not support ATO.
2017                    -- ATP4drp changes end
2018 
2019                    IF  MSC_ATP_PVT.G_PLAN_INFO_REC.plan_id is not null and
2020                            NVL(MSC_ATP_PVT.G_PLAN_INFO_REC.plan_id, -1) <> -1 then
2021                         IF PG_DEBUG in ('Y', 'C') THEN
2022                             msc_sch_wb.atp_debug('plan found for  matched item');
2023                         END IF;
2024                         l_plan_found_for_match := 1;
2025                         l_atp_table.base_model_id(i) := l_process_item_id;
2026                         l_process_item_id := l_match_item_family_id;
2027 
2028                         --bug 4604500: pass correct request item it when match is found.
2029                         l_requested_item_id := l_atp_table.match_item_id(i);
2030 
2031                         --get item attributes of the matched item
2032                         MSC_ATP_PROC.get_item_attributes(p_instance_id,
2033                                          --3917625: Read attributes from correct plan
2034                                          --l_plan_id,
2035                                          MSC_ATP_PVT.G_PLAN_INFO_REC.plan_id,
2036                                          l_atp_table.match_item_id(i),
2037                                          l_process_org_id,
2038                                          l_item_attribute_rec);
2039 
2040                          l_atp_table.matched_item_name(i) := l_item_attribute_rec.item_name;
2041 
2042                          l_atp_table.matched_item_name(i) := l_item_attribute_rec.item_name;
2043 
2044                    ELSE
2045                         IF PG_DEBUG in ('Y', 'C') THEN
2046                             msc_sch_wb.atp_debug('plan  not found for  matched item');
2047                         END IF;
2048                         l_plan_found_for_match := 2;
2049                         l_atp_table.match_item_id(i) := null;
2050 
2051                         -- we got plan for match. All fields in G_ITEM_INFO_REC were populated
2052                         --as a results plan was not getting read.
2053                         MSC_ATP_PVT.G_ITEM_INFO_REC.sr_inv_item_id := null;
2054                    END IF;
2055                 END IF;
2056 
2057                 IF PG_DEBUG in ('Y', 'C') THEN
2058                      msc_sch_wb.atp_debug('Schedule: l_plan_found_for_match := ' || l_plan_found_for_match);
2059                 END IF;
2060                 IF l_plan_found_for_match = 2 THEN
2061                    /* time_phased_atp changes begin
2062                       Call new procedure Get_PF_Plan_Info*/
2063                    MSC_ATP_PF.Get_PF_Plan_Info(
2064                                p_instance_id,
2065                                l_requested_item_id,
2066                                l_process_item_id,
2067                                l_process_org_id,
2068                                l_atp_table.demand_class(i),
2069                                l_atf_date,
2070                                l_atp_table.error_code(i),
2071                                l_return_status,
2072                                NULL --bug3510475
2073                    );
2074 
2075                    IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
2076                         IF PG_DEBUG in ('Y', 'C') THEN
2077                            msc_sch_wb.atp_debug('Schedule: ' || 'ATP Downtime during Re-schedule');
2078                         END IF;
2079                         x_return_status := FND_API.G_RET_STS_ERROR;
2080                         RAISE FND_API.G_EXC_ERROR;
2081                    END IF;
2082 
2083                    -- ATP4drp changes begin
2084                    IF NVL(MSC_ATP_PVT.G_PLAN_INFO_REC.plan_type, 1) = 5 THEN
2085                       -- time_phased_atp - Reset variables
2086                       l_process_item_id := l_requested_item_id;
2087                       MSC_ATP_PVT.G_ITEM_INFO_REC.product_family_id := MSC_ATP_PVT.G_ITEM_INFO_REC.dest_inv_item_id;
2088                       IF PG_DEBUG in ('Y', 'C') THEN
2089                          msc_sch_wb.atp_debug('----- ATP4drp Specific Debug Messages -----');
2090                          msc_sch_wb.atp_debug('Schedule: ' || 'PF and Allocated ATP not applicable for DRP plans');
2091                          msc_sch_wb.atp_debug('Schedule: ' || 'MSC_ATP_PVT.G_ALLOCATED_ATP ' || MSC_ATP_PVT.G_ALLOCATED_ATP);
2092                          msc_sch_wb.atp_debug('Schedule: ' || 'l_requested_item_id ' || l_requested_item_id);
2093                          msc_sch_wb.atp_debug('Schedule: ' || 'l_process_item_id ' || l_process_item_id);
2094                          msc_sch_wb.atp_debug('----- ATP4drp Specific Debug Messages -----');
2095                       END IF;
2096                    ELSE
2097                       IF l_atf_date is not null THEN
2098                           l_time_phased_atp := 'Y';
2099                       ELSIF l_process_item_id <> l_requested_item_id THEN
2100                           l_pf_atp := 'Y';
2101                       END IF;
2102 
2103                    END IF;
2104                    -- ATP4drp changes end
2105                    /* time_phased_atp changes end*/
2106 
2107                 END IF;
2108 
2109                 l_plan_info_rec := MSC_ATP_PVT.G_PLAN_INFO_REC;
2110                 -- End New procedure for obtaining plan data : Supplier Capacity Lead Time proj.
2111                 l_plan_id       := l_plan_info_rec.plan_id;
2112                 /* time_phased_atp Add plan_id to l_atp_table so that it can later be used
2113                    this way we save call to get_global_plan_info*/
2114                 l_atp_table.plan_id(i) := l_plan_info_rec.plan_id;
2115                 l_assign_set_id := l_plan_info_rec.assignment_set_id;
2116                 IF l_atp_table.attribute_07.Exists(i) THEN
2117                         l_atp_table.attribute_07(i) := l_plan_info_rec.plan_name;
2118                 END IF;
2119                 --diag_atp
2120                 l_atp_rec.plan_name := l_plan_info_rec.plan_name;
2121                 -- code changes for bug 2392456 ends
2122 
2123                 -- 1873918: PDS-ODS fix
2124                 -- PDS-ODS fix
2125                 IF l_plan_id is NULL THEN
2126                   IF PG_DEBUG in ('Y', 'C') THEN
2127                      msc_sch_wb.atp_debug('Schedule: ' || 'no plan available');
2128                   END IF;
2129                   x_return_status := FND_API.G_RET_STS_ERROR;
2130                   --bug 2854351: Raise plan down time error
2131                   --l_atp_table.Error_Code(i):= PLAN_NOT_FOUND;
2132                   l_atp_table.Error_Code(i):= MSC_ATP_PVT.PLAN_DOWN_TIME;
2133                   RAISE NO_DATA_FOUND;
2134                 ELSIF l_plan_id = -100 THEN
2135                   IF PG_DEBUG in ('Y', 'C') THEN
2136                      msc_sch_wb.atp_debug('Schedule: ' || 'Summary Concurrent program is running');
2137                   END IF;
2138                   x_return_status := FND_API.G_RET_STS_ERROR;
2139                   l_atp_table.Error_Code(i):= SUMM_CONC_PROG_RUNNING;
2140                   RAISE NO_DATA_FOUND;
2141                 ELSIF l_plan_id = -200 THEN
2142                   IF PG_DEBUG in ('Y', 'C') THEN
2143                      msc_sch_wb.atp_debug('Schedule: ' || 'Post Plan Alloc progranm has not been run');
2144                   END IF;
2145                   x_return_status := FND_API.G_RET_STS_ERROR;
2146                   l_atp_table.Error_Code(i):= RUN_POST_PLAN_ALLOC;
2147                   RAISE NO_DATA_FOUND;
2148 
2149                 -- 24x7 ATP
2150                 ELSIF l_plan_id = -300 THEN
2151                   if PG_DEBUG in ('Y','C') THEN
2152                       msc_sch_wb.atp_debug ('Schedule: ' || 'ATP Downtime . 24x7 ATP');
2153                   END IF;
2154                   x_return_status := FND_API.G_RET_STS_ERROR;
2155                   --- bug 2854351: Show planned down time error
2156                   --l_atp_table.Error_Code(i) := TRY_ATP_LATER;
2157                   l_atp_table.Error_Code(i) := PLAN_DOWN_TIME;
2158                   RAISE NO_DATA_FOUND;
2159 
2160                 ELSIF l_plan_id = -1 THEN
2161                   IF PG_DEBUG in ('Y', 'C') THEN
2162                      msc_sch_wb.atp_debug('Schedule: ' || 'no plan available, switch to ods');
2163                   END IF;
2164 
2165                   l_atp_table.Error_Code(i):= PDS_TO_ODS_SWITCH;
2166                   IF PG_DEBUG in ('Y', 'C') THEN
2167                      msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_table.Error_Code(i)='||l_atp_table.Error_Code(i));
2168                   END IF;
2169                   MSC_ATP_PVT.G_INV_CTP := 5;
2170                   MSC_ATP_PVT.G_ALLOCATED_ATP := 'N';
2171                   MSC_ATP_PVT.G_ALLOCATION_METHOD := 2;
2172                   MSC_ATP_PVT.G_ORIG_ALLOC_ATP := 'N'; --bug 9911034
2173                   MSC_ATP_PVT.G_HYBRID_ALLOC_ATP := 'N'; -- ALLOC ATP CHANGES, 12973673
2174                   --5221865 In cases of PDS ODS Swith we turn iterative scheduling off
2175                   MSC_ATP_PVT.G_FORWARD_ATP := 'N';
2176 
2177                   /* Switching to ODS, Set the Global comp flag to NO */
2178                   --s_cto_rearch
2179                   IF G_ITEM_INFO_REC.bom_item_type in (1, 4) and G_ITEM_INFO_REC.replenish_to_ord_flag = 'Y' THEN
2180                       --ATO Model or item need be exploded to do ATP on model level
2181                       IF G_ITEM_INFO_REC.atp_comp_flag = 'C' THEN
2182 
2183                          G_ITEM_INFO_REC.atp_comp_flag := 'Y';
2184 
2185                       ELSIF G_ITEM_INFO_REC.atp_comp_flag = 'R' THEN
2186 
2187                           G_ITEM_INFO_REC.atp_comp_flag := 'N';
2188 
2189                       END IF;
2190 
2191                   ELSE
2192                       G_ITEM_INFO_REC.atp_comp_flag := 'N';
2193                   END IF;
2194                   --e_cto_rearch
2195 
2196                   l_atp_comp_flag := G_ITEM_INFO_REC.atp_comp_flag;
2197                   --Moved below bug 4091487
2198                   /*
2199                   SELECT so_tbl_status,
2200                          NVL(summary_flag, 1)
2201                   INTO   l_so_tbl_status,
2202                          l_summary_flag
2203                   FROM   msc_apps_instances
2204                   WHERE  instance_id = p_instance_id;
2205 
2206                   IF PG_DEBUG in ('Y', 'C') THEN
2207                      msc_sch_wb.atp_debug('Schedule: ' || 'l_so_tbl_status = '||l_so_tbl_status);
2208                      msc_sch_wb.atp_debug('Schedule: ' || 'l_summary_flag = '||l_summary_flag);
2209                   END IF;
2210 
2211                   -- Doing PDS-ODS switch. Summary will be diabled in all cases because ODS summary
2212                   -- will commit even PDS data from previous lines
2213                   msc_sch_wb.atp_debug('Schedule: ' || 'Summary will be disabled');
2214                   MSC_ATP_PVT.G_SUMMARY_FLAG := 'N';
2215 
2216                   IF NVL(l_so_tbl_status, 1) = 2 THEN
2217                     -- not available for atp
2218                     IF PG_DEBUG in ('Y', 'C') THEN
2219                        msc_sch_wb.atp_debug('Schedule: ' || 'ods not available');
2220                     END IF;
2221 
2222                     l_atp_table.error_code(1) := TRY_ATP_LATER;
2223                     RAISE NO_DATA_FOUND;
2224 
2225                   END IF;
2226                   */
2227                 END IF; -- END IF l_plan_id is NULL
2228               --ELSE
2229               --- if ATP and ATPComp falg is no then we dont want to do summary approach
2230               --  MSC_ATP_PVT.G_SUMMARY_FLAG := 'N';
2231                 -- Bug 4091487 Checking so_tbl_status in PDS ATP also.
2232                 SELECT so_tbl_status,
2233                          NVL(summary_flag, 1)
2234                 INTO   l_so_tbl_status,
2235                          l_summary_flag
2236                 FROM   msc_apps_instances
2237                 WHERE  instance_id = p_instance_id;
2238 
2239                 IF PG_DEBUG in ('Y', 'C') THEN
2240                    msc_sch_wb.atp_debug('Schedule: ' || 'l_so_tbl_status = '||l_so_tbl_status);
2241                    msc_sch_wb.atp_debug('Schedule: ' || 'l_summary_flag = '||l_summary_flag);
2242                 END IF;
2243 
2244                 IF NVL(l_so_tbl_status, 1) = 2 THEN
2245                     -- not available for atp
2246                     IF PG_DEBUG in ('Y', 'C') THEN
2247                        msc_sch_wb.atp_debug('Schedule: ' || 'ATP not available');
2248                     END IF;
2249 
2250                     l_atp_table.error_code(1) := TRY_ATP_LATER;
2251                     RAISE NO_DATA_FOUND;
2252                 END IF;
2253                 -- Summary will be diabled in all cases because ODS summary
2254                 -- will commit even PDS data from previous lines
2255                 IF l_plan_id = -1 THEN
2256 
2257                    MSC_ATP_PVT.G_SUMMARY_FLAG := 'N';
2258 
2259                    IF PG_DEBUG in ('Y', 'C') THEN
2260                     msc_sch_wb.atp_debug('Schedule: ' || 'Summary will be disabled');
2261                    END IF;
2262 
2263                 END IF;
2264                END IF;
2265 
2266             END IF;
2267             --- bug 2152184: Retrival of PF item needs to be done on ODS data. Moved the call before plan is retrieved
2268             /*l_process_item_id := MSC_ATP_PF.Get_PF_Atp_Item_Id(p_instance_id,
2269                                                     l_plan_id,
2270                                                     l_requested_item_id,
2271                                                     l_process_org_id); */
2272 
2273             l_stmt := 200;
2274     	    IF PG_DEBUG in ('Y', 'C') THEN
2275     	       msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
2276     	    END IF;
2277 
2278             -- reinitialize some elements in l_atp_rec
2279             l_atp_rec.error_code := ALLSUCCESS;
2280             l_atp_rec.ship_date := NULL;
2281             l_atp_rec.available_quantity := NULL;
2282             l_atp_rec.requested_date_quantity := NULL;
2283             -- 2382963 : krajan for vivek
2284             l_atp_rec.arrival_date := NULL;
2285 
2286             -- cchen: 1238941
2287             l_atp_rec.combined_requested_date_qty := NULL;
2288 
2289             -- need to do uom quantity conversion
2290 
2291             MSC_ATP_PROC.inv_primary_uom_conversion(p_instance_id,
2292                                        l_sources.Organization_Id(j),
2293                                        l_process_item_id,
2294                                        l_atp_table.Quantity_UOM(i),
2295                                        l_quantity_uom,
2296                                        l_conversion_rate);
2297 
2298             l_quantity_ordered := l_atp_table.Quantity_Ordered(i)* l_conversion_rate;
2299             l_quantity_ordered := ROUND( l_quantity_ordered, 6); --4562646
2300             l_atp_rec.instance_id := p_instance_id;
2301             l_atp_rec.demand_source_line := l_atp_table.Identifier(i);
2302             l_atp_rec.identifier := l_atp_table.Identifier(i);
2303             l_atp_rec.demand_source_header_id:=l_atp_table.Demand_Source_Header_Id(i);
2304 	    l_atp_rec.demand_source_delivery:=l_atp_table.Demand_Source_Delivery(i);
2305 	    l_atp_rec.demand_source_type:=l_atp_table.Demand_Source_type(i);--cmro
2306             l_atp_rec.inventory_item_id := l_process_item_id;
2307             l_atp_rec.request_item_id := l_requested_item_id;
2308             l_atp_rec.organization_id := l_sources.Organization_Id(j);
2309             l_atp_rec.supplier_id := l_sources.Supplier_Id(j);
2310             l_atp_rec.supplier_site_id := l_sources.Supplier_Site_Id(j);
2311             l_atp_rec.quantity_ordered := l_quantity_ordered;
2312             l_atp_rec.quantity_uom := l_quantity_uom;
2313             l_atp_rec.requested_ship_date := l_atp_table.Requested_Ship_Date(i);
2314             l_atp_rec.requested_arrival_date := l_atp_table.Requested_Arrival_Date(i);
2315             l_atp_rec.latest_acceptable_date := l_atp_table.Latest_Acceptable_Date(i);
2316 	    -- dsting 2741997
2317             l_atp_rec.delivery_lead_time := l_atp_table.Delivery_Lead_Time(i);
2318             l_atp_rec.freight_carrier := l_atp_table.Freight_Carrier(i);
2319             l_atp_rec.ship_method := l_atp_table.Ship_Method(i);
2320 
2321             -- time_phased_atp
2322             l_atp_rec.atf_date := l_atf_date;
2323 
2324             -- ship_rec_cal
2325             l_atp_rec.shipping_cal_code       := l_atp_table.shipping_cal_code(i);
2326             l_atp_rec.receiving_cal_code      := l_atp_table.receiving_cal_code(i);
2327             l_atp_rec.intransit_cal_code      := l_atp_table.intransit_cal_code(i);
2328             l_atp_rec.manufacturing_cal_code  := l_atp_table.manufacturing_cal_code(i);
2329 
2330             -- Bug 3226083
2331             l_mem_dest_id := MSC_ATP_PVT.G_ITEM_INFO_REC.dest_inv_item_id;
2332             l_pf_dest_id := MSC_ATP_PVT.G_ITEM_INFO_REC.product_family_id;
2333 
2334             l_g_inv_ctp               := MSC_ATP_PVT.G_INV_CTP; --9239406 if switch has happened then this points to correct value.
2335             --optional_fw start New loop added to get the date on which qty is found.
2336             --for second line in set we need to reset these
2337 
2338             G_FW_CTP_PEGGING_ID.DELETE;
2339             G_FW_STEAL_PEGGING_ID.DELETE;
2340             l_ctp_parent_peg_id.DELETE;
2341             l_steal_atp_peg_id.DELETE;
2342 
2343             G_OPTIONAL_FW             := NULL;
2344             G_REQUESTED_SHIP_DATE     := l_atp_rec.requested_ship_date;
2345             l_quan_ordered            := l_atp_rec.quantity_ordered;
2346             l_request_date_qty        := 0;
2347             G_FW_PEGGING_ID           := NULL;
2348             G_ATP_COMP_FLAG           := 'Y';
2349             l_last_date               := NULL;
2350             l_first_date              := NULL;
2351             l_low_seqnum              := NULL;
2352             l_high_seqnum             := NULL;
2353             l_middle_seqnum           := NULL;
2354             l_last_search             := NULL;
2355             l_last_successful_date    := NULL;
2356             l_atp_peg_id              := NULL;
2357             l_loop_count              := 1;
2358             l_last_success_atp_rec    := NULL;
2359             l_second_good_run         := NULL;
2360             l_g_inv_ctp               := MSC_ATP_PVT.G_INV_CTP; --5158454 if switch has happened then this points to correct value.
2361             G_DEMAND_ID               := NULL; --5158454
2362             G_NUMBER_OF_ITERATIONS    := NULL;         --5211558 holds the numbet of iterations binary search will take
2363             G_LOOP_COUNT              := l_loop_count; --5211558 holds the present count of loop iteration
2364 
2365             LOOP
2366               EXIT WHEN l_low_seqnum > l_high_seqnum; --both are null for first time.
2367               IF PG_DEBUG in ('Y', 'C') THEN
2368                 msc_sch_wb.atp_debug('Schedule: ' || '___OPTIONAL FW PASS Binary Search Start____');
2369                 msc_sch_wb.atp_debug('Schedule: ' || 'MSC_ATP_PVT.G_OPTIONAL_FW: '         || MSC_ATP_PVT.G_OPTIONAL_FW);
2370                 msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.quantity_ordered: '        || l_atp_rec.quantity_ordered);
2371                 msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.requested_date_quantity: ' || l_atp_rec.requested_date_quantity);
2372                 msc_sch_wb.atp_debug('Schedule: ' || 'l_loop_count start : '               || l_loop_count);
2373               END IF;
2374               --5211558 initialized global variable to use inside ATP_Check
2375               IF l_time_phased_atp = 'Y' AND G_FORWARD_ATP = 'Y' THEN
2376                  G_LOOP_COUNT := l_loop_count;
2377                  IF PG_DEBUG in ('Y', 'C') THEN
2378                     msc_sch_wb.atp_debug('Schedule: ' || 'G_LOOP_COUNT start : '               || G_LOOP_COUNT);
2379                  END IF;
2380               END IF;
2381              --If more iteration are needed then go inside this if condition.
2382              --First time we will not go inside as loop count will be 1.
2383              --Only second time we come here as after that G_OPTIONAL_FW is set to not null value.
2384              IF (MSC_ATP_PVT.G_OPTIONAL_FW is null)  AND l_loop_count = 2
2385                        AND (Nvl(l_atp_rec.requested_date_quantity,0) < l_atp_rec.quantity_ordered)
2386              THEN
2387                IF PG_DEBUG in ('Y', 'C') THEN
2388                   msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.quantity_ordered: ' || l_atp_rec.quantity_ordered);
2389                   msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.requested_date_quantity: ' || l_atp_rec.requested_date_quantity);
2390                   msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.combined_requested_date_qty: ' || l_atp_rec.combined_requested_date_qty);
2391                   msc_sch_wb.atp_debug('Schedule: ' || 'l_request_date_qty: ' || l_request_date_qty);
2392                   msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.requested_ship_date: ' || l_atp_rec.requested_ship_date);
2393                END IF;
2394 
2395                MSC_ATP_PVT.G_OPTIONAL_FW := 1;
2396 
2397                l_atp_rec.quantity_ordered := l_atp_rec.quantity_ordered -
2398                        GREATEST(NVL(l_atp_rec.combined_requested_date_qty,
2399                                     l_atp_rec.requested_date_quantity), 0);
2400 
2401                l_request_date_qty :=   GREATEST(NVL(l_atp_rec.combined_requested_date_qty,
2402                                     l_atp_rec.requested_date_quantity), 0);
2403 
2404                l_atp_rec.requested_ship_date := MSC_CALENDAR.DATE_OFFSET (
2405                                                 l_atp_rec.shipping_cal_code,
2406                                                 l_atp_rec.instance_id,
2407                                                 l_atp_rec.requested_ship_date,
2408                                                 1, 1);
2409 
2410                l_atp_rec.ship_date := NULL;
2411                l_atp_rec.available_quantity := NULL;
2412                l_atp_rec.used_available_quantity := NULL;
2413                l_atp_rec.requested_date_quantity := NULL;
2414                l_atp_rec.combined_requested_date_qty := NULL;
2415                l_atp_rec.atf_date_quantity := NULL;
2416 
2417                IF PG_DEBUG in ('Y', 'C') THEN
2418                   msc_sch_wb.atp_debug('Schedule: ' || 'MSC_ATP_PVT.G_OPTIONAL_FW: ' || MSC_ATP_PVT.G_OPTIONAL_FW);
2419                   msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.quantity_ordered: ' || l_atp_rec.quantity_ordered);
2420                   msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.requested_ship_date: ' || l_atp_rec.requested_ship_date);
2421                   msc_sch_wb.atp_debug('Schedule: ' || 'l_request_date_qty: ' || l_request_date_qty);
2422                END IF;
2423                --for OPTIONAL FW PASS cases initialize it to ITF
2424                MSC_ATP_PROC.get_infinite_time_fence_date(l_atp_rec.instance_id,
2425                                               l_atp_rec.request_item_id,
2426                                               l_atp_rec.organization_id,
2427                                               l_plan_id,
2428                                               l_last_date,
2429                                               l_atp_rule_name);
2430 
2431                --Make sure that it is valid working day
2432                l_first_date := MSC_CALENDAR.PREV_WORK_DAY(
2433 						l_atp_rec.shipping_cal_code,
2434 						l_atp_rec.instance_id,
2435 						l_atp_rec.requested_ship_date);
2436 
2437 	       --ITF date is a valid working day per mfg cal.
2438 	       --Make sure that it is valid working day as per shipping also.
2439                l_last_date := MSC_CALENDAR.NEXT_WORK_DAY(
2440 						l_atp_rec.shipping_cal_code,
2441 						l_atp_rec.instance_id,
2442 						l_last_date);
2443               IF PG_DEBUG in ('Y', 'C') THEN
2444                msc_sch_wb.atp_debug('Schedule: ' || 'l_first_date: ' || l_first_date);
2445                msc_sch_wb.atp_debug('Schedule: ' || 'MSC_ATP_PVT.G_OPTIONAL_FW: ' || MSC_ATP_PVT.G_OPTIONAL_FW);
2446                msc_sch_wb.atp_debug('Schedule: ' || 'l_low_seqnum: ' || l_low_seqnum);
2447                msc_sch_wb.atp_debug('Schedule: ' || 'l_high_seqnum: ' || l_high_seqnum);
2448               END IF;
2449             END IF;
2450 
2451             --For each iteration these should be set to original values
2452             MSC_ATP_PVT.G_PTF_DATE                := l_sys_next_date;
2453             l_atp_rec.ship_date                   := NULL;
2454             l_atp_rec.available_quantity          := NULL;
2455             l_atp_rec.parent_so_quantity          := l_quan_ordered;
2456             l_atp_rec.used_available_quantity     := NULL;
2457             l_atp_rec.requested_date_quantity     := NULL;
2458             l_atp_rec.combined_requested_date_qty := NULL;
2459             l_atp_rec.atf_date_quantity           := NULL;
2460             G_FW_CTP_PEGGING_ID.DELETE; --as this has to point to the top level for each iteration
2461             G_FW_STEAL_PEGGING_ID.DELETE;
2462             G_ITEM_INFO_REC                       := l_item_attribute_rec; --for each loop this should point to main item and
2463                                                                              --not substitutes.
2464             G_ITEM_INFO_REC.atp_comp_flag         := l_atp_comp_flag ; --bug9596003
2465             --Get the seq_num for both dates depending upon the shipping cal code.
2466             IF (MSC_ATP_PVT.G_OPTIONAL_FW is not null) AND (nvl(l_last_search,0) <> 1) THEN
2467                 l_low_seqnum    := msc_atp_pvt.get_seq_num(l_first_date,
2468                                                            l_atp_rec.shipping_cal_code,
2469                                                            l_atp_rec.instance_id
2470                                                            );
2471                IF PG_DEBUG in ('Y', 'C') THEN
2472                  msc_sch_wb.atp_debug('Schedule: ' || 'l_low_seqnum: ' || l_low_seqnum);
2473                END IF;
2474                 l_high_seqnum   := msc_atp_pvt.get_seq_num(l_last_date,
2475                                                            l_atp_rec.shipping_cal_code,
2476                                                            l_atp_rec.instance_id
2477                                                            );
2478                IF PG_DEBUG in ('Y', 'C') THEN
2479                  msc_sch_wb.atp_debug('Schedule: ' || 'l_high_seqnum: ' || l_high_seqnum);
2480                END IF;
2481 
2482                 l_middle_seqnum := floor((l_low_seqnum + l_high_seqnum)/2);
2483 
2484                IF PG_DEBUG in ('Y', 'C') THEN
2485                  msc_sch_wb.atp_debug('Schedule: ' || 'l_middle_seqnum: ' || l_middle_seqnum);
2486                END IF;
2487 
2488                 l_atp_rec.requested_ship_date := msc_atp_pvt.get_date_from_seqnum(
2489                                                            l_middle_seqnum,
2490                                                            l_atp_rec.shipping_cal_code,
2491                                                            l_atp_rec.instance_id
2492                                                            );
2493                IF l_loop_count = 2 THEN
2494                  l_number_of_iterations := floor(LOG(2,((l_high_seqnum-l_low_seqnum) + 1))) + 1;
2495 
2496                  IF PG_DEBUG in ('Y', 'C') THEN
2497                     msc_sch_wb.atp_debug('Schedule: ' || 'l_number_of_iterations: ' || l_number_of_iterations);
2498                  END IF;
2499                  --5211558 Store the number of iterations to know which is the last pass
2500                  IF l_time_phased_atp = 'Y' AND G_FORWARD_ATP = 'Y' THEN
2501                     G_NUMBER_OF_ITERATIONS := l_number_of_iterations;
2502 
2503                     IF PG_DEBUG in ('Y', 'C') THEN
2504                        msc_sch_wb.atp_debug('Schedule: ' || 'G_NUMBER_OF_ITERATIONS: ' || G_NUMBER_OF_ITERATIONS);
2505                     END IF;
2506                  END IF;
2507                END IF;
2508 
2509                IF PG_DEBUG in ('Y', 'C') THEN
2510                  msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.requested_ship_date: ' || l_atp_rec.requested_ship_date);
2511                END IF;
2512               END IF;
2513             --optional_fw end
2514             /* ship_rec_cal changes begin
2515                If ship date is provided validate it on OSC
2516                If arrival date is provided validate it on ORC, offset using VIC, validate it on OSC*/
2517             IF PG_DEBUG in ('Y', 'C') THEN
2518                   msc_sch_wb.atp_debug('Schedule: ' || '___________________Start Offset___________________');
2519                   msc_sch_wb.atp_debug('Schedule: ' || 'Req Ship Date: ' || l_atp_rec.requested_ship_date);
2520                   msc_sch_wb.atp_debug('Schedule: ' || 'Req Arrival Date: ' || l_atp_rec.requested_arrival_date);
2521                   msc_sch_wb.atp_debug('Schedule:  ');
2522             END IF;
2523             -- Bug3593394 - Recalculate date only if parameters have changed
2524             IF (i=1)
2525                 OR (MSC_ATP_PVT.G_OPTIONAL_FW is null)
2526                 --optional_fw first time when second line comes do again as
2527                 --l_requested_ship_date will be pointing to last iteration of 1st line.
2528                 OR (l_atp_rec.receiving_cal_code        <> l_atp_table.receiving_cal_code(i-1))
2529                 OR (l_atp_rec.intransit_cal_code        <> l_atp_table.intransit_cal_code(i-1))
2530                 OR (nvl(l_atp_rec.delivery_lead_time,0) <> nvl(l_atp_table.delivery_lead_time(i-1),0))
2531                 OR (l_atp_rec.shipping_cal_code         <> l_atp_table.shipping_cal_code(i-1))
2532                 OR (
2533                       (
2534                          (l_atp_rec.requested_arrival_date IS NULL)
2535                          OR (l_atp_table.Requested_Arrival_Date(i-1) IS NULL)
2536                          OR (l_atp_rec.requested_arrival_date    <> l_atp_table.Requested_Arrival_Date(i-1))
2537                       )
2538                       AND
2539                       (
2540                          (l_atp_rec.requested_ship_date IS NULL)
2541                          OR (l_atp_table.requested_ship_date(i-1) IS NULL)
2542                          OR (l_atp_rec.requested_ship_date       <> l_atp_table.requested_ship_date(i-1))
2543                       )
2544                    ) THEN
2545 
2546                 IF (l_atp_rec.requested_arrival_date IS NOT NULL) THEN
2547 		    l_requested_ship_date := MSC_CALENDAR.PREV_WORK_DAY(
2548 						l_atp_rec.receiving_cal_code,
2549 						l_atp_rec.instance_id,
2550 						l_atp_rec.requested_arrival_date);
2551 
2552                     IF PG_DEBUG in ('Y', 'C') THEN
2553                           msc_sch_wb.atp_debug('Schedule: ' || 'Date after validating on ORC/CRC: ' || l_requested_ship_date);
2554                     END IF;
2555 
2556 		    IF trunc(l_requested_ship_date) <> trunc(l_atp_rec.requested_arrival_date) THEN
2557 		        G_CALENDAR_CONSTRAINT := 'Y';
2558 		    ELSE
2559 		        G_CALENDAR_CONSTRAINT := 'N'; -- bug3593394 - reset the global variable
2560 		    END IF;
2561 		    l_requested_ship_date := MSC_CALENDAR.DATE_OFFSET(
2562 		                                l_atp_rec.intransit_cal_code,
2563 		                                l_atp_rec.instance_id,
2564 		                                l_requested_ship_date,
2565 		                                -1 * nvl(l_atp_rec.delivery_lead_time, 0),
2566 		                                -1);
2567 
2568                     IF PG_DEBUG in ('Y', 'C') THEN
2569                           msc_sch_wb.atp_debug('Schedule: ' || 'Date after subtracting intransit LT using VIC: ' || l_requested_ship_date);
2570                     END IF;
2571 
2572 		    l_requested_ship_date := MSC_CALENDAR.PREV_WORK_DAY(
2573 						l_atp_rec.shipping_cal_code,
2574 						l_atp_rec.instance_id,
2575 						l_requested_ship_date);
2576 
2577                 ELSE
2578 		    l_requested_ship_date := MSC_CALENDAR.PREV_WORK_DAY(
2579 						l_atp_rec.shipping_cal_code,
2580 						l_atp_rec.instance_id,
2581 						l_atp_rec.requested_ship_date);
2582 		    IF trunc(l_requested_ship_date) <> trunc(l_atp_rec.requested_ship_date) THEN
2583 		        G_CALENDAR_CONSTRAINT := 'Y';
2584 		    ELSE
2585 		        G_CALENDAR_CONSTRAINT := 'N'; -- bug3593394 - reset the global variable
2586 		    END IF;
2587                 END IF;
2588 
2589                 IF PG_DEBUG in ('Y', 'C') THEN
2590                   msc_sch_wb.atp_debug('Schedule: ' || 'Date after validating on OSC: ' || l_requested_ship_date);
2591                   msc_sch_wb.atp_debug('Schedule: ' || 'l_requested_ship_date: ' || l_requested_ship_date);
2592                   msc_sch_wb.atp_debug('Schedule: ' || 'G_CALENDAR_CONSTRAINT: ' || G_CALENDAR_CONSTRAINT);
2593                   msc_sch_wb.atp_debug('Schedule: ' || '___________________End Offset___________________');
2594                 END IF;
2595 
2596                 IF l_requested_ship_date is NULL THEN
2597                   l_atp_table.error_code(i) := MSC_ATP_PVT.ATP_INVALID_DATE;
2598                   RAISE FND_API.G_EXC_ERROR;
2599                 END IF;
2600 
2601                 --bug 3322846: Move past due ship date to the first working day after sysdate
2602                 --L_MOVE_PAST_DUE_TO_SYSDATE := NVL(FND_PROFILE.value('MSC_MOVE_PAST_DUE_TO_SYSDATE'), 'Y'); -- Bug 5584634/5618929
2603                 L_MOVE_PAST_DUE_TO_SYSDATE := MSC_ATP_PVT.G_MOV_PAST_DUE_SYSDATE_PROF;--6316476
2604                 IF PG_DEBUG in ('Y', 'C') THEN
2605                    msc_sch_wb.atp_debug('Schedule: ' || 'MOVE_PAST_DUE_TO_SYS_DATE :'|| L_MOVE_PAST_DUE_TO_SYSDATE);
2606                 END IF;
2607                 if L_MOVE_PAST_DUE_TO_SYSDATE = 'Y' THEN  -- Bug 5584634/5618929
2608                 IF l_requested_ship_date < l_sys_next_date THEN
2609 
2610                     IF PG_DEBUG in ('Y', 'C') THEN
2611                         msc_sch_wb.atp_debug('Schedule: ' || 'Request date is less than sysdate');
2612                     END IF;
2613                     l_requested_ship_date := MSC_CALENDAR.NEXT_WORK_DAY(
2614                                                 l_atp_rec.shipping_cal_code,
2615                                                 l_atp_rec.instance_id,
2616                                                 l_sys_next_date);
2617 
2618                     l_past_due_ship_date := 1; --bug4291375 setting for past due ship date
2619                     IF PG_DEBUG in ('Y', 'C') THEN
2620                         msc_sch_wb.atp_debug('Schedule: ' || 'New l_requested_ship_date := ' || l_requested_ship_date);
2621                     END IF;
2622                 END IF;
2623                 END IF; -- Bug 5584634/5618929
2624 
2625                 IF l_requested_ship_date is NULL THEN
2626                     l_atp_table.error_code(i) := MSC_ATP_PVT.ATP_INVALID_DATE;
2627                     RAISE FND_API.G_EXC_ERROR;
2628                 END IF;
2629 
2630             ELSE -- Bug3593394
2631                 IF PG_DEBUG in ('Y', 'C') THEN
2632                   msc_sch_wb.atp_debug('Schedule: ' || 'Ship date parameters have not changed.');
2633                   msc_sch_wb.atp_debug('Schedule: ' || 'l_requested_ship_date: ' || l_requested_ship_date);
2634                   msc_sch_wb.atp_debug('Schedule: ' || 'G_CALENDAR_CONSTRAINT: ' || G_CALENDAR_CONSTRAINT);
2635                   msc_sch_wb.atp_debug('Schedule: ' || '___________________End Offset___________________');
2636                 END IF;
2637             END IF; -- Bug3593394
2638 
2639             l_atp_rec.requested_ship_date := l_requested_ship_date;
2640             l_atp_rec.requested_arrival_date := null;
2641 
2642             --optional_fw this will  have the original date after calendar validation.
2643             IF MSC_ATP_PVT.G_OPTIONAL_FW is null THEN
2644 	       G_REQUESTED_SHIP_DATE     := l_requested_ship_date;
2645             END IF;
2646 
2647 
2648             /* ship_rec_cal changes end */
2649 
2650             -- aatp: in case we are doing allocated atp and demand class
2651             -- is not specified.
2652 
2653             -- 2783787 (ssurendr) Null demand class in request should be considered as "Others"
2654             /*
2655             IF (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
2656                             (l_atp_table.Demand_Class(i) IS NULL) THEN
2657               MSC_ATP_PROC.get_global_org_info(l_atp_rec.instance_id,
2658                                                 l_atp_rec.organization_id);
2659               l_default_atp_rule_id := G_ORG_INFO_REC.default_atp_rule_id;
2660               l_calendar_code := G_ORG_INFO_REC.cal_code;
2661               l_calendar_exception_set_id := G_ORG_INFO_REC.cal_exception_set_id;
2662               l_default_demand_class := G_ORG_INFO_REC.default_demand_class;
2663               l_org_code := G_ORG_INFO_REC.org_code;
2664 
2665               l_atp_table.Demand_Class(i) := l_default_demand_class;
2666             END IF;
2667             */
2668 
2669             -- hierarchy changes
2670             -- due to arrival set issue, the dummy demand class will
2671             -- be done in atp_check
2672 
2673             l_atp_rec.demand_class := l_atp_table.Demand_Class(i);
2674 
2675             -- rajjain 02/11/2003 Bug 2788144
2676             --IF (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 2) THEN
2677 
2678             -- rajjain 02/11/2003 Bug 2788144
2679             --END IF;
2680 	    -- ATP Override rajjain begin
2681 	    IF l_atp_table.Action(i) = ATPQUERY THEN
2682                 IF PG_DEBUG in ('Y', 'C') THEN
2683                    msc_sch_wb.atp_debug('Schedule: ' || 'Override Flag: '||l_atp_rec.override_flag);
2684                 END IF;
2685 
2686 		l_atp_rec.override_flag := 'N';
2687 
2688                 IF PG_DEBUG in ('Y', 'C') THEN
2689 		   msc_sch_wb.atp_debug('Schedule: ' || 'ATPQUERY - Setting the Override Flag to : '||l_atp_rec.override_flag);
2690                 END IF;
2691 	    ELSE
2692                 l_atp_rec.override_flag := l_atp_table.Override_Flag(i);
2693 
2694 	    END IF;
2695 	    -- ATP Override rajjain end
2696 
2697             l_atp_rec.action := l_atp_table.Action(i);
2698             l_atp_rec.insert_flag := l_atp_table.Insert_Flag(i);
2699 
2700             -- for bug 1105741.  l_atp_rec.to_organization_id should be populated .
2701             l_atp_rec.to_organization_id := l_atp_table.Organization_Id(i);
2702             l_atp_rec.to_instance_id := p_instance_id;
2703 	    l_atp_rec.customer_id := l_atp_table.customer_id(i);
2704             l_atp_rec.refresh_number := p_refresh_number;
2705             l_atp_rec.atp_lead_time := l_atp_table.Atp_Lead_Time(i);
2706             l_atp_rec.Order_Number := l_atp_table.Order_Number(i);
2707             l_atp_rec.component_identifier := MSC_ATP_PVT.G_COMP_LINE_ID;
2708 
2709             ---This will be populated in case of reschedulling.
2710             --- This will be demand id for the old demand
2711             --bug 2384224: since we are always going to add demand, we dont need to remember old demand id
2712             --l_atp_rec.old_demand_id        := l_demand_ids(i);
2713 
2714 	    -- for multi_level/ multi_org cto support
2715             l_atp_rec.calling_module := l_atp_table.calling_module(i);
2716             l_stmt := 210;
2717     	    IF PG_DEBUG in ('Y', 'C') THEN
2718     	       msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
2719 	       msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.Inventory_Item_Id:' || to_char(l_atp_rec.Inventory_Item_Id)
2720 		||' l_atp_rec.request_item_id:' || to_char(l_atp_rec.request_item_id)
2721 		||' l_atp_rec.organization_id:' || to_char(l_atp_rec.organization_id) );
2722 	    END IF;
2723 
2724             -- get the search method:  this should come from
2725             -- a profile option (1 for depth and 2 for breadth).
2726             -- but for now, let's assume it is a depth search.
2727             -- the following logic is for depth first!!!!
2728 
2729             l_search := DEPTH ;
2730 
2731             -- since this is the top item, this is the first level
2732             -- make the first level to be 0
2733             l_level := 0;
2734 
2735             -- for a single line or ship set, the scenario id is the
2736             -- the order of sources
2737 
2738             l_scenario_id := j;
2739 
2740             -- CTO_PF_PRJ_2 changes for CTO PF Cross Project Impacts
2741             -- Set the default to No PF ATP Items being processed.
2742             MSC_ATP_PVT.G_CTO_PF_ATP := 'N';
2743             IF PG_DEBUG in ('Y', 'C') THEN
2744                msc_sch_wb.atp_debug('Schedule: ' || 'Before ATP_Check for this l_atp_rec ');
2745                msc_sch_wb.atp_debug('Schedule: Init G_CTO_PF_ATP to ' ||
2746                                                        MSC_ATP_PVT.G_CTO_PF_ATP);
2747             END IF;
2748             -- End CTO_PF_PRJ_2 changes for CTO PF Cross Project Impacts
2749 
2750             --- subst
2751 
2752             IF PG_DEBUG in ('Y', 'C') THEN
2753                msc_sch_wb.atp_debug('Schedule: ' || 'G_PLAN_SUBST_FLAG := ' || MSC_ATP_PVT.G_PLAN_SUBST_FLAG);
2754             END IF;
2755 
2756             IF MSC_ATP_PVT.G_INV_CTP = 4 AND
2757                NVL(MSC_ATP_PVT.G_PLAN_SUBST_FLAG, 2) = 1 THEN
2758                IF PG_DEBUG in ('Y', 'C') THEN
2759                   msc_sch_wb.atp_debug('Schedule: ' || 'count := ' || l_item_substitute_rec.inventory_item_id.count);
2760                   msc_sch_wb.atp_debug('Schedule: ' || 'G_CREATE_SUPPLY_FLAG := ' || MSC_ATP_SUBST.G_CREATE_SUPPLY_FLAG);
2761                   msc_sch_wb.atp_debug('Schedule: ' || 'get item attributes for the top item');
2762                END IF;
2763 
2764                -- check if one and only one of the requested_ship_date and
2765                -- requested_arrival_date is provided
2766                IF ((l_atp_rec.requested_ship_date IS NULL) and
2767                (l_atp_rec.requested_arrival_date IS NULL)) THEN
2768 
2769                    l_atp_rec.error_code := MSC_ATP_PVT.NOREQ_DATE;
2770                    -- krajan : 2437459
2771                    l_atp_table.error_code(i) := MSC_ATP_PVT.NOREQ_DATE;
2772 
2773                    RAISE FND_API.G_EXC_ERROR;
2774 
2775                ELSIF ((l_atp_rec.requested_ship_date IS NOT NULL) and
2776                     (l_atp_rec.requested_arrival_date IS NOT NULL)) THEN
2777 
2778                     l_atp_rec.error_code := MSC_ATP_PVT.ATP_MULTI_REQ_DATES;
2779                     -- krajan : 2437459
2780                     l_atp_table.error_code(i) := MSC_ATP_PVT.ATP_MULTI_REQ_DATES;
2781 
2782                     RAISE FND_API.G_EXC_ERROR;
2783                END IF;
2784                IF PG_DEBUG in ('Y', 'C') THEN
2785                   msc_sch_wb.atp_debug('Schedule: ' || 'get item attributes for the top item');
2786                END IF;
2787 
2788                -- old dlt code
2789 
2790                -- 1143475:
2791                -- IF atp lead time > 0 then we must do the offset no matter that item
2792                -- is atpable or not.  Otherwise, only if that item is atpable
2793                -- item we need to do the offset.  The reason of doing this is to avoid
2794                -- database hit.
2795 
2796                IF PG_DEBUG in ('Y', 'C') THEN
2797                   msc_sch_wb.atp_debug('Schedule: ' || 'Point 3');
2798                END IF;
2799 
2800                l_atp_rec.delivery_lead_time := l_delivery_lead_time;
2801                l_atp_rec.ship_method := l_ship_method;
2802                l_atp_rec.requested_ship_date := l_requested_ship_date;
2803                l_atp_rec.requested_arrival_date := null;
2804                IF PG_DEBUG in ('Y', 'C') THEN
2805                   msc_sch_wb.atp_debug('Schedule: ' || 'ship_method = '||l_ship_method ||' lead time = '||l_delivery_lead_time);
2806                END IF;
2807 
2808                -----------
2809                /* Modularize Item and Org Info */
2810                -- Information already obtained.
2811                /*MSC_ATP_PROC.get_global_item_info( p_instance_id,
2812                                                  -1,
2813                                                 l_atp_rec.inventory_item_id,
2814                                                 l_atp_rec.organization_id );
2815                */
2816                -- Changed assignments
2817                l_atp_flag := G_ITEM_INFO_REC.atp_flag;
2818                l_atp_comp_flag := G_ITEM_INFO_REC.atp_comp_flag;
2819                l_pre_pro_lt := G_ITEM_INFO_REC.pre_pro_lt;
2820                l_fixed_lt := G_ITEM_INFO_REC.fixed_lt;
2821                l_variable_lt := G_ITEM_INFO_REC.variable_lt;
2822                l_post_pro_lt := G_ITEM_INFO_REC.post_pro_lt;
2823                l_inventory_item_id := G_ITEM_INFO_REC.dest_inv_item_id;
2824                l_item_name := G_ITEM_INFO_REC.item_name;
2825                IF PG_DEBUG in ('Y', 'C') THEN
2826                   msc_sch_wb.atp_debug('Schedule: ' || ' Create supply flag :=' ||
2827                                       G_ITEM_INFO_REC.create_supply_flag);
2828                END IF;
2829                /* Modularize Item and Org Info */
2830 
2831                l_substitution_type := l_atp_table.substitution_typ_code(i);
2832                IF PG_DEBUG in ('Y', 'C') THEN
2833                   msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_flag := ' || l_atp_flag);
2834                   msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_comp_flag := ' || l_atp_comp_flag);
2835                   msc_sch_wb.atp_debug('Schedule: ' || 'l_pre_pro_lt := ' || l_pre_pro_lt);
2836                   msc_sch_wb.atp_debug('Schedule: ' || 'l_fixed_lt := ' || l_fixed_lt);
2837                   msc_sch_wb.atp_debug('Schedule: ' || 'l_inventory_item_id := ' || l_inventory_item_id);
2838                   msc_sch_wb.atp_debug('Schedule: ' || 'l_item_name :=' || l_item_name);
2839                   msc_sch_wb.atp_debug('Schedule: ' || 'l_substitution_type := ' || l_substitution_type);
2840                END IF;
2841                ---bug 2341073 : null out the l_item_substitute_rec else values for last item might get used again
2842                l_item_substitute_rec := l_null_item_sub_rec;
2843 
2844                /*
2845                time_phased_atp
2846                In non-time-phased PF Atp cases there won't be any end item substitution
2847                so no need to call Get_Item_Substitutes but we want to do breadth first search*/
2848                IF l_pf_atp <> 'Y' THEN
2849                        MSC_ATP_SUBST.Get_Item_Substitutes(l_inventory_item_id,
2850                                                         l_item_substitute_rec,
2851                                                         p_instance_id,
2852                                                         l_plan_id,
2853                                                         l_atp_rec.customer_id,
2854                                                         l_atp_table.customer_site_id(i),
2855                                                         l_requested_ship_date,
2856                                                         l_atp_rec.organization_id);
2857                END IF;
2858 
2859                IF PG_DEBUG in ('Y', 'C') THEN
2860                   msc_sch_wb.atp_debug('Schedule: ' || ' subst count := ' || l_item_substitute_rec.inventory_item_id.count);
2861                END IF;
2862 
2863                IF l_item_substitute_rec.inventory_item_id.count > 0 AND
2864                                                (NVL(l_substitution_type, 4) = 4 OR
2865                                                       l_atp_rec.override_flag = 'Y'
2866  					        ---diag_atp
2867                            			OR MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1) THEN
2868                   --- if item has substitutes but we dont want to do any substitution then
2869                   --- we will do breadth first search. But we dont want to use any substitutes.
2870                   --- Therefore we remove all the substitutes from the table
2871                   IF PG_DEBUG in ('Y', 'C') THEN
2872                      msc_sch_wb.atp_debug('Schedule: ' || 'Substitution defined but not doing substitution');
2873                   END IF;
2874                   MSC_ATP_PVT.G_SUBSTITUTION_FLAG := 'Y';
2875                   l_highest_rev := l_item_substitute_rec.highest_revision_item_id(1);
2876                   l_item_substitute_rec := l_null_item_sub_rec;
2877 
2878                ELSIF  NVL(l_substitution_type, 4) = 4 or
2879                      (l_item_substitute_rec.inventory_item_id.count = 0) THEN
2880                    IF PG_DEBUG in ('Y', 'C') THEN
2881                       msc_sch_wb.atp_debug('Schedule: ' || 'Substitute not defined');
2882                    END IF;
2883                   MSC_ATP_PVT.G_SUBSTITUTION_FLAG := 'N';
2884                ELSE
2885                   IF PG_DEBUG in ('Y', 'C') THEN
2886                      msc_sch_wb.atp_debug('Schedule: ' || 'Substitution defined and doing substitution');
2887                   END IF;
2888                   MSC_ATP_PVT.G_SUBSTITUTION_FLAG := 'Y';
2889                END IF;
2890 
2891                IF MSC_ATP_PVT.G_SUBSTITUTION_FLAG = 'Y' THEN
2892 
2893                   IF MSC_ATP_SUBST.G_CREATE_SUPPLY_FLAG = 702
2894                            AND l_item_substitute_rec.inventory_item_id.count = 0 THEN
2895                       l_create_supply_on_orig_item := 1;
2896                   ELSE
2897                       -- Modular re-use of info.
2898                       l_create_supply_on_orig_item := G_ITEM_INFO_REC.create_supply_flag;
2899                       IF PG_DEBUG in ('Y', 'C') THEN
2900                          msc_sch_wb.atp_debug('Schedule: ' || ' Re-use Create supply flag :=' ||
2901                                       G_ITEM_INFO_REC.create_supply_flag);
2902                       END IF;
2903                       -- Modular re-use of info.
2904 
2905                   END IF;
2906 
2907 
2908                   --- now add the original item
2909                   l_count := l_item_substitute_rec.inventory_item_id.count;
2910                   IF PG_DEBUG in ('Y', 'C') THEN
2911                      msc_sch_wb.atp_debug('Schedule: ' || 'l_count := ' || l_count);
2912                   END IF;
2913                   MSC_ATP_SUBST.Extend_Item_Info_Rec_Typ(l_item_substitute_rec, l_return_status);
2914                   IF PG_DEBUG in ('Y', 'C') THEN
2915                      msc_sch_wb.atp_debug('Schedule: ' || 'After extending Item info');
2916                   END IF;
2917                   l_count := l_item_substitute_rec.inventory_item_id.count;
2918                   IF PG_DEBUG in ('Y', 'C') THEN
2919                      msc_sch_wb.atp_debug('Schedule: ' || 'l_count := ' || l_count);
2920                   END IF;
2921                  l_item_substitute_rec.inventory_item_id(l_count) := l_inventory_item_id;
2922                  --l_item_substitute_rec.sr_inventory_item_id(l_count) := l_atp_rec.inventory_item_id;
2923                  --l_requested_item_id has member_item_id.l_atp_rec.inventory_item_id has family id.
2924                  l_item_substitute_rec.sr_inventory_item_id(l_count) := l_requested_item_id; --bug3467631
2925 
2926                  -- time_phased_atp
2927                  IF l_time_phased_atp = 'Y' or l_pf_atp = 'Y' THEN
2928                      l_item_substitute_rec.Family_sr_id(l_count) := l_process_item_id;
2929                      l_item_substitute_rec.Family_dest_id(l_count) := MSC_ATP_PVT.G_ITEM_INFO_REC.product_family_id;
2930                      l_item_substitute_rec.Family_item_name(l_count) := MSC_ATP_FUNC.Get_Inv_Item_Name(
2931                                                                                 p_instance_id,
2932                                                                                 l_process_item_id,
2933                                                                                 l_atp_rec.organization_id
2934                                                                         );
2935                      l_item_substitute_rec.atf_date(l_count) := l_atf_date;
2936                  ELSE
2937                      --bug3709707 passing correct item id
2938                      --l_item_substitute_rec.Family_sr_id(l_count) := l_inventory_item_id;
2939                      l_item_substitute_rec.Family_sr_id(l_count) := l_requested_item_id;
2940                      l_item_substitute_rec.Family_dest_id(l_count) := MSC_ATP_PVT.G_ITEM_INFO_REC.dest_inv_item_id;
2941                      l_item_substitute_rec.Family_item_name(l_count) := l_item_name;
2942                      l_item_substitute_rec.atf_date(l_count) := null;
2943                  END IF;
2944 
2945                  l_item_substitute_rec.plan_id(l_count) := l_plan_id;
2946                  --l_item_substitute_rec.assign_set_id.extend(l_count);
2947                  l_item_substitute_rec.assign_set_id(l_count) := l_assign_set_id;
2948                  l_item_substitute_rec.item_name(l_count) := l_item_name;
2949                  l_item_substitute_rec.atp_comp_flag(l_count) := l_atp_comp_flag;
2950                  l_item_substitute_rec.atp_flag(l_count) := l_atp_flag;
2951                  l_item_substitute_rec.post_pro_lt(l_count) := l_post_pro_lt;
2952                  l_item_substitute_rec.create_supply_flag(l_count) := l_create_supply_on_orig_item;
2953                  l_item_substitute_rec.highest_revision_item_id(l_count) := l_highest_rev;
2954 
2955                  -- dsting additional attributes for diagnostic atp
2956                  /* time_phased_atp
2957                  Substitution window should not be more than aggregate time fence days*/
2958                  IF (l_pf_atp = 'Y' OR l_time_phased_atp = 'Y') THEN
2959                   l_item_substitute_rec.substitution_window(l_count) := LEAST(MSC_ATP_PVT.G_ITEM_INFO_REC.substitution_window,
2960                                                                                 nvl(MSC_ATP_PF.Get_Atf_Days(
2961                                                                                         p_instance_id,
2962                                                                                         l_inventory_item_id,
2963                                                                                         l_atp_rec.organization_id
2964                                                                                     ),
2965                                                                                  0)
2966                                                                               );
2967                  ELSE
2968                   l_item_substitute_rec.substitution_window(l_count) := MSC_ATP_PVT.G_ITEM_INFO_REC.substitution_window;
2969                  END IF;
2970 
2971                  l_item_substitute_rec.unit_weight(l_count) := MSC_ATP_PVT.G_ITEM_INFO_REC.unit_weight;
2972                  l_item_substitute_rec.weight_uom(l_count) := MSC_ATP_PVT.G_ITEM_INFO_REC.weight_uom;
2973                  l_item_substitute_rec.unit_volume(l_count) := MSC_ATP_PVT.G_ITEM_INFO_REC.unit_volume;
2974                  l_item_substitute_rec.volume_uom(l_count) := MSC_ATP_PVT.G_ITEM_INFO_REC.volume_uom;
2975                  l_item_substitute_rec.rounding_control_type(l_count) := MSC_ATP_PVT.G_ITEM_INFO_REC.rounding_control_type;
2976                  l_item_substitute_rec.plan_name(l_count) := l_atp_rec.plan_name;
2977                  l_item_substitute_rec.pre_pro_lt(l_count) := l_pre_pro_lt;
2978                  l_item_substitute_rec.fixed_lt(l_count) := l_fixed_lt;
2979                  l_item_substitute_rec.variable_lt(l_count) := l_variable_lt;
2980 
2981 
2982                  --l_item_substitute_rec.requested_ship_date(l_count) := l_requested_ship_date;
2983                  FOR i in  1..l_item_substitute_rec.inventory_item_id.count LOOP
2984                     IF PG_DEBUG in ('Y', 'C') THEN
2985                        msc_sch_wb.atp_debug('Schedule: ' || 'Item ' || i || '  :=  ' || l_item_substitute_rec.inventory_item_id(i));
2986                     END IF;
2987                  END LOOP;
2988 
2989 
2990                  IF PG_DEBUG in ('Y', 'C') THEN
2991                     msc_sch_wb.atp_debug('Schedule: ' || 'l_item_count := ' || l_item_substitute_rec.inventory_item_id.count);
2992                  END IF;
2993                END IF;
2994 
2995             ELSE
2996 
2997                MSC_ATP_PVT.G_SUBSTITUTION_FLAG := 'N';
2998 
2999             END IF;
3000             IF PG_DEBUG in ('Y', 'C') THEN
3001                msc_sch_wb.atp_debug('Schedule: ' || 'G_SUBSTITUTION_FLAG := ' || MSC_ATP_PVT.G_SUBSTITUTION_FLAG);
3002                msc_sch_wb.atp_debug('Schedule: ' || 'l_plan_id := ' || l_plan_id);
3003                msc_sch_wb.atp_debug('Schedule: ' || 'G_INV_CTP := ' || MSC_ATP_PVT.G_INV_CTP);
3004                msc_sch_wb.atp_debug('Schedule: ' || 'l_g_inv_ctp := ' || l_g_inv_ctp); --5158454
3005             END IF;
3006 
3007             IF MSC_ATP_PVT.G_SUBSTITUTION_FLAG = 'Y' AND MSC_ATP_PVT.G_INV_CTP = 4 THEN
3008 
3009                l_atp_rec.substitution_type := l_atp_table.substitution_typ_code(i);
3010                l_atp_rec.req_item_detail_flag := l_atp_table.req_item_detail_flag(i);
3011 
3012                --bug 4568088: pass original request date
3013                l_atp_rec.original_request_date := l_atp_table.original_request_date(i); --plan by request date
3014 
3015                MSC_ATP_SUBST.ATP_Check_SUBST(l_atp_rec,
3016                          l_item_substitute_rec,
3017                          l_requested_ship_date,
3018                          l_plan_id,
3019                          l_level,
3020                          l_scenario_id,
3021                          l_search,
3022                          p_refresh_number,
3023                          null, -- previous_pegging_id
3024                          l_assign_set_id,
3025                          l_atp_period,
3026                          l_atp_supply_demand,
3027                          l_return_status);
3028                IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
3029                    IF PG_DEBUG in ('Y', 'C') THEN
3030                       msc_sch_wb.atp_debug('Schedule: ' || 'atp check fails');
3031                    END IF;
3032                    x_return_status := FND_API.G_RET_STS_ERROR;
3033 		   --2741997
3034                    l_atp_table.Error_Code(i):= l_atp_rec.Error_Code;
3035                    RAISE NO_DATA_FOUND;
3036                END IF;
3037 
3038                --bug3467631 start
3039                --Setting the l_bkwd_pass_atf_date_qty which is to be used in move_pf_bucketed_demands
3040                --for new time phase atp logic.This qty has backward pass request date atp+ctp qantity.
3041                l_atp_rec.requested_date_quantity := GREATEST(NVL(l_atp_rec.requested_date_quantity, 0), 0);
3042 
3043                IF l_atp_rec.requested_ship_date <= l_atf_date THEN
3044                 l_bkwd_pass_atf_date_qty :=l_atp_rec.requested_date_quantity;
3045                ELSE
3046                 l_bkwd_pass_atf_date_qty := l_atp_rec.atf_date_quantity;
3047                END IF;
3048 
3049                IF PG_DEBUG in ('Y', 'C') THEN
3050                     msc_sch_wb.atp_debug('Schedule: ' || 'l_atf_date = '||l_atf_date);
3051                     msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.atf_date_quantity = '||l_atp_rec.atf_date_quantity);
3052                     msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.used_available_quantity = '||l_atp_rec.used_available_quantity);
3053                     msc_sch_wb.atp_debug('Schedule: ' || 'l_bkwd_pass_atf_date_qty = '||l_bkwd_pass_atf_date_qty);
3054                END IF;
3055                --bug3467631 end
3056 
3057                ---- add this item to MSC_ATP_PVT.G_PEGGING_FOR_SET as this is used to remove the demands if needed
3058                MSC_ATP_PVT.G_PEGGING_FOR_SET.extend;
3059                MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG.extend;
3060                MSC_ATP_PVT.G_PEGGING_FOR_SET(MSC_ATP_PVT.G_PEGGING_FOR_SET.count) := MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
3061                MSC_ATP_PVT.G_REQ_ATP_DATE.extend;
3062                MSC_ATP_PVT.G_REQ_DATE_QTY.extend;
3063 
3064                ---bug 2361283: For determining the date on which we want to see the substitution we offset the
3065                --- lead time from requested_ship_date and save it in l_atp_rec.requested_ship_date itself
3066                --- As a result we are loosing time component. So we restore the date back.
3067                IF l_atp_table.requested_ship_date(i) is not null THEN
3068                     l_atp_rec.requested_ship_date := l_atp_table.requested_ship_date(i);
3069                     IF PG_DEBUG in ('Y', 'C') THEN
3070                        msc_sch_wb.atp_debug('Schedule: ' || 'Orig Date := ' || to_char(l_atp_table.requested_ship_date(i), 'mm-dd-yy hh:mm:ss'));
3071                        msc_sch_wb.atp_debug('Schedule: ' || 'Reset the ship date bug 2361283');
3072                        msc_sch_wb.atp_debug('Schedule: ' || 'Orig Date := ' || to_char(l_atp_rec.requested_ship_date, 'mm-dd-yy hh:mm:ss'));
3073                     END IF;
3074                END IF;
3075 
3076                  -- Commented for bug 2748730. G_END_OF_DAY not required here.
3077 		 /*
3078 		 -- for bug 2259824, add end of day to ship date
3079 		IF l_atp_rec.ship_date IS NOT NULL THEN
3080 		  l_atp_rec.ship_date := TRUNC(l_atp_rec.ship_date) + MSC_ATP_PVT.G_END_OF_DAY ;
3081 		  IF PG_DEBUG in ('Y', 'C') THEN
3082              		msc_sch_wb.atp_debug('Schedule: 2259824 l_atp_rec.ship_date = '||
3083                 		to_char(l_atp_rec.ship_date, 'DD-MON-YYYY HH24:MI:SS'));
3084           	  END IF;
3085 		END IF;
3086 		*/
3087 
3088             ELSE
3089                -- dsting 2465370
3090 		G_LINE_OVERRIDE_FLAG := l_atp_rec.override_flag;
3091                --s_cto_rearch
3092                l_atp_rec.Top_Model_line_id := l_atp_table.Top_Model_line_id(i);
3093                l_atp_rec.ATO_Parent_Model_Line_Id := l_atp_table.ATO_Parent_Model_Line_Id(i);
3094                l_atp_rec.ATO_Model_Line_Id := l_atp_table.ATO_Model_Line_Id(i);
3095                l_atp_rec.Parent_line_id := l_atp_table.Parent_line_id(i);
3096                l_atp_rec.wip_supply_type := l_atp_table.wip_supply_type(i);
3097                l_atp_rec.parent_atp_flag := null;
3098                l_atp_rec.parent_atp_comp_flag := null;
3099                l_atp_rec.parent_repl_order_flag := null;
3100                l_atp_rec.parent_bom_item_type := null;
3101                l_atp_rec.bom_item_type := G_ITEM_INFO_REC.bom_item_type;
3102                l_atp_rec.rep_ord_flag  := G_ITEM_INFO_REC.replenish_to_ord_flag;
3103                l_atp_rec.mand_comp_flag := 2;
3104                --5168353 When profile is yes then we have already set this to correct value
3105                --        and the value in l_atp_rec is not correct.
3106                IF G_FORWARD_ATP = 'N' THEN
3107                l_atp_rec.parent_so_quantity := l_atp_rec.quantity_ordered;
3108                END IF;
3109                l_atp_rec.original_request_date := l_atp_table.original_request_date(i); --plan by request date
3110                l_atp_rec.ship_set_name 	  :=l_atp_table.ship_set_name(i);               --plan by request date
3111                l_atp_rec.arrival_set_name :=l_atp_table.arrival_set_name(i);            --plan by request date
3112                l_atp_rec.override_flag	  :=l_atp_table.override_flag(i);               --plan by request date
3113 
3114 
3115                IF PG_DEBUG in ('Y', 'C') THEN
3116                     msc_sch_wb.atp_debug('Call ATP_Check');
3117                END IF;
3118                --e_cto_rearch
3119                MSC_ATP_PVT.ATP_Check(l_atp_rec,
3120                          l_plan_id,
3121                          l_level,
3122                          l_scenario_id,
3123                          l_search,
3124                          p_refresh_number,
3125                          null, -- previous_pegging_id
3126                          l_assign_set_id,
3127                          l_atp_period,
3128                          l_atp_supply_demand,
3129                          l_return_status);
3130 
3131                IF l_return_status = MSC_ATP_PVT.CTO_OSS_ERROR THEN
3132                    l_atp_table.Error_Code(i):= MSC_ATP_PVT.OSS_SOURCING_ERROR;
3133 
3134                ELSIF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
3135                    IF PG_DEBUG in ('Y', 'C') THEN
3136                       msc_sch_wb.atp_debug('Schedule: ' || 'atp check fails');
3137                    END IF;
3138                    x_return_status := FND_API.G_RET_STS_ERROR;
3139 		   --2741997
3140 --                   l_atp_table.Error_Code(k):= l_atp_rec.Error_Code;
3141                    l_atp_table.Error_Code(i):= l_atp_rec.Error_Code;
3142                    RAISE NO_DATA_FOUND;
3143                END IF;
3144 
3145                IF PG_DEBUG in ('Y', 'C') THEN
3146                     msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.atf_date_quantity = '||l_atp_rec.atf_date_quantity);
3147                     msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.used_available_quantity = '||l_atp_rec.used_available_quantity);
3148                END IF;
3149                -- No NVL is needed on atf_date_quantity bug3555084
3150                --l_atp_rec.atf_date_quantity := GREATEST(NVL(l_atp_rec.atf_date_quantity, 0), 0);
3151                --bug3397904 start
3152                --optional_fw this should be executed only in b/w pass. check this one!!
3153                IF MSC_ATP_PVT.G_OPTIONAL_FW is null then
3154                 IF l_atp_rec.requested_ship_date <= l_atf_date THEN
3155                  l_bkwd_pass_atf_date_qty := GREATEST(NVL(l_atp_rec.combined_requested_date_qty,
3156                                           l_atp_rec.requested_date_quantity), 0);
3157                 ELSE
3158                  l_bkwd_pass_atf_date_qty := NVL(l_atp_rec.atf_date_quantity,0);
3159                 END IF;
3160                END IF;
3161                --bug3397904 end
3162                 -- Commented for bug 2748730. G_END_OF_DAY not required here.
3163 		/*
3164 		-- for bug 2259824, add end of day to ship date
3165 		IF l_atp_rec.ship_date IS NOT NULL THEN
3166 		  l_atp_rec.ship_date := TRUNC(l_atp_rec.ship_date) + MSC_ATP_PVT.G_END_OF_DAY ;
3167 		  IF PG_DEBUG in ('Y', 'C') THEN
3168              		msc_sch_wb.atp_debug('Schedule: 2259824 l_atp_rec.ship_date = '||
3169                 		to_char(l_atp_rec.ship_date, 'DD-MON-YYYY HH24:MI:SS'));
3170           	  END IF;
3171 		END IF;
3172 	       */
3173 	       --diag_atp
3174 	       --optional_fw we never want to do this f/w pass if G_FORWARD_ATP is Yes
3175                IF MSC_ATP_PVT.G_FIND_FUTURE_DATE = 'Y'
3176                        AND MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 2 AND G_FORWARD_ATP = 'N' THEN
3177 
3178                  l_future_atp_rec := l_atp_rec;
3179 
3180                  l_future_atp_rec.error_code := ALLSUCCESS;
3181                  l_future_atp_rec.ship_date := NULL;
3182                  l_future_atp_rec.available_quantity := NULL;
3183                  l_future_atp_rec.requested_date_quantity := NULL;
3184                  -- cchen :1238941
3185                  l_future_atp_rec.combined_requested_date_qty := NULL;
3186                  l_future_atp_rec.quantity_uom := l_atp_rec.quantity_uom;
3187 
3188                  -- added greatest to ensure we won't set the quantity_ordered
3189                  -- wrong when requested_date_quantity is less than 0
3190 
3191                  -- cchen:1238941
3192 	         -- l_future_atp_rec.quantity_ordered := l_atp_rec.quantity_ordered-
3193                  --    GREATEST(l_atp_rec.requested_date_quantity, 0);
3194 
3195 
3196                  l_future_atp_rec.quantity_ordered := l_atp_rec.quantity_ordered -
3197                        GREATEST(NVL(l_atp_rec.combined_requested_date_qty,
3198                                     l_atp_rec.requested_date_quantity), 0);
3199                  l_future_atp_rec.atf_date_quantity := NULL;
3200 
3201                  -- The date might have been set by a lower level component. we reset it again
3202                  -- Bug 3512996 - For top level G_PTF_DATE should be as per OSC
3203                  -- MSC_ATP_PVT.G_PTF_DATE := l_sys_next_omc_date;
3204                  MSC_ATP_PVT.G_PTF_DATE := l_sys_next_date;
3205 
3206                  --9239406
3207                  IF PG_DEBUG in ('Y', 'C') THEN
3208                          msc_sch_wb.atp_debug('Schedule: ' || 'l_g_inv_ctp = '||l_g_inv_ctp );
3209 	         END IF;
3210                  IF (l_g_inv_ctp = 5) then -- -- If it is set to one, PDS-ODS switch has happened, otherwise no.
3211 
3212                      MSC_ATP_PVT.G_INV_CTP := 5;
3213                      MSC_ATP_PVT.G_ALLOCATED_ATP := 'N';
3214                      MSC_ATP_PVT.G_ALLOCATION_METHOD := 2;
3215                      --bug 9911034, We don't do enhance f/w atp and ALLOC_ATP in ODS cases
3216                      MSC_ATP_PVT.G_ORIG_ALLOC_ATP := 'N';
3217                      MSC_ATP_PVT.G_HYBRID_ALLOC_ATP := 'N'; -- ALLOC ATP CHANGES, 12973673
3218                      MSC_ATP_PVT.G_FORWARD_ATP := 'N';
3219 
3220                      IF PG_DEBUG in ('Y', 'C') THEN
3221                          msc_sch_wb.atp_debug('Schedule: ' || 'Reset the profiles in case of PSD-ODS switch' );
3222 	             END IF;
3223 
3224                  END IF;
3225 
3226                  IF PG_DEBUG in ('Y', 'C') THEN
3227                  	msc_sch_wb.atp_debug('Schedule: ' || 'MSC_ATP_PVT.G_INV_CTP ='||MSC_ATP_PVT.G_INV_CTP );
3228                     msc_sch_wb.atp_debug('Schedule: ' || 'l_future_atp_rec.quantity_ordered ='||l_future_atp_rec.quantity_ordered );
3229                     msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.requested_date_quantity ='||l_atp_rec.requested_date_quantity);
3230 	            msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.combined_requested_date_qty ='||l_atp_rec.combined_requested_date_qty);
3231 	         END IF;
3232 
3233                  MSC_ATP_PVT.ATP_Check(l_future_atp_rec,
3234                          l_plan_id,
3235                          l_level,
3236                          l_scenario_id,
3237                          2,
3238                          p_refresh_number,
3239                          null, -- previous_pegging_id
3240                          l_assign_set_id,
3241                          l_future_atp_period,
3242                          l_future_atp_supply_demand,
3243                          l_return_status);
3244 
3245                  IF l_return_status = MSC_ATP_PVT.CTO_OSS_ERROR THEN
3246                    l_atp_table.Error_Code(i):= MSC_ATP_PVT.OSS_SOURCING_ERROR;
3247                 -- dsting 2764213
3248                 ELSIF l_return_status in (
3249                      MSC_ATP_PVT.G_ATO_SRC_MISMATCH,
3250                      MSC_ATP_PVT.G_ATO_UNCOLL_ITEM,
3251                      MSC_ATP_PVT.G_NO_PLAN_FOUND)
3252                 THEN
3253                    IF PG_DEBUG in ('Y', 'C') THEN
3254                       msc_sch_wb.atp_debug('Schedule: ' || 'atp check fails in future case');
3255                    END IF;
3256 		   --2741997
3257                    x_return_status := FND_API.G_RET_STS_ERROR;
3258                    l_atp_rec.error_code := l_future_atp_rec.Error_code;
3259                    l_atp_table.Error_Code(i):= l_atp_rec.Error_Code;
3260                    RAISE NO_DATA_FOUND;
3261                  END IF;
3262 
3263 		-- Commented for bug 2748730. G_END_OF_DAY not required here.
3264                  /*
3265 		 -- for bug 2259824, add end of day to ship date
3266 		l_atp_rec.ship_date := TRUNC(l_future_atp_rec.ship_date) + MSC_ATP_PVT.G_END_OF_DAY ;
3267 
3268 		IF PG_DEBUG in ('Y', 'C') THEN
3269              		msc_sch_wb.atp_debug('Schedule: 2259824 l_atp_rec.ship_date = '||
3270                 		to_char(l_atp_rec.ship_date, 'DD-MON-YYYY HH24:MI:SS'));
3271           	END IF;
3272 		*/
3273 
3274                  l_atp_rec.ship_date := l_future_atp_rec.ship_date;
3275 		 -- make sure we won't ouput any available_quantity which is
3276                  -- greater than the infinite number
3277 
3278                  IF PG_DEBUG in ('Y', 'C') THEN
3279                     msc_sch_wb.atp_debug('Schedule: ' || 'l_future_atp_rec.available_quantity = '||
3280 			   to_char(nvl(l_future_atp_rec.available_quantity, -99)));
3281                     msc_sch_wb.atp_debug('Schedule: ' || 'l_future_atp_rec.used_available_quantity = '||
3282 			   l_future_atp_rec.used_available_quantity); --bug3409973
3283                     msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.combined_requested_date_qty = '||
3284 			   to_char(nvl(l_atp_rec.combined_requested_date_qty, -99)));
3285                     msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.requested_date_quantity = '||
3286 			   to_char(nvl(l_atp_rec.requested_date_quantity, -99)));
3287                     msc_sch_wb.atp_debug('Schedule: ' || 'atf_date_quantity before adding qty from future = '||l_atp_rec.atf_date_quantity);
3288                     msc_sch_wb.atp_debug('Schedule: ' || 'l_future_atp_rec.atf_date_quantity = '||l_future_atp_rec.atf_date_quantity);
3289                  END IF;
3290 
3291                  IF (l_future_atp_rec.atf_date_quantity is null) and (l_future_atp_rec.ship_date <= l_atf_date) THEN
3292                     l_future_atp_rec.atf_date_quantity := l_future_atp_rec.quantity_ordered;
3293                  END IF;
3294 
3295                  /* Bug 3265577*/
3296                  IF l_atp_rec.requested_ship_date > l_atf_date THEN
3297                          l_atp_rec.atf_date_quantity :=
3298                                  GREATEST(NVL(l_atp_rec.atf_date_quantity, 0), 0) +
3299                                  GREATEST(NVL(l_future_atp_rec.atf_date_quantity, 0), 0);
3300                  ELSE
3301                          l_atp_rec.atf_date_quantity :=
3302                                  GREATEST(NVL(l_atp_rec.requested_date_quantity, 0), 0) +
3303                                  GREATEST(NVL(l_future_atp_rec.atf_date_quantity, 0), 0);
3304                  END IF;
3305 
3306                  IF PG_DEBUG in ('Y', 'C') THEN
3307                     msc_sch_wb.atp_debug('Schedule: ' || 'atf_date_quantity after adding qty from future = '||l_atp_rec.atf_date_quantity);
3308                  END IF;
3309 
3310                  -- cchen 1238941
3311                  -- 1411818 : add greatest
3312                  l_atp_rec.available_quantity := LEAST(
3313                          GREATEST(l_future_atp_rec.available_quantity, 0) +
3314                          GREATEST(NVL(l_atp_rec.combined_requested_date_qty,
3315                              l_atp_rec.requested_date_quantity), 0),
3316                          INFINITE_NUMBER);
3317 
3318                  l_atp_rec.used_available_quantity:= LEAST(
3319                          GREATEST(l_future_atp_rec.used_available_quantity, 0) +
3320                          GREATEST(NVL(l_atp_rec.combined_requested_date_qty,
3321                              l_atp_rec.requested_date_quantity), 0),
3322                          INFINITE_NUMBER); --bug3409973
3323                  -- for aatp
3324                  -- 1411818: add greatest
3325                  l_atp_rec.requested_date_quantity :=
3326                          GREATEST(l_atp_rec.requested_date_quantity, 0) +
3327                          GREATEST(l_future_atp_rec.combined_requested_date_qty, 0);
3328 
3329                  -- 1524689
3330                  -- IF requested_date_quantity is greater than order quantity
3331                  -- due to stealing or we happen to find enough when we
3332                  -- do the forward case, then we should set the error code
3333                  -- to success.
3334 
3335                  IF l_atp_rec.requested_date_quantity >=
3336                     l_atp_rec.quantity_ordered  THEN
3337                    l_atp_rec.error_code := ALLSUCCESS;
3338                  END IF;
3339 
3340                  /* Bug 4211058 */
3341                  IF l_atp_rec.ship_date = l_atp_rec.requested_ship_date then
3342                     l_atp_rec.requested_date_quantity :=
3343                          l_atp_rec.available_quantity;
3344                  END IF;
3345 
3346                  MSC_ATP_PROC.Details_Output(l_future_atp_period,
3347                                 l_future_atp_supply_demand,
3348                                 l_atp_period,
3349                                 l_atp_supply_demand,
3350                                 l_return_status);
3351                ELSE
3352                  -- for aatp
3353                  l_atp_rec.requested_date_quantity :=
3354                     l_atp_rec.combined_requested_date_qty;
3355                END IF;
3356             END IF; -- IF MSC_ATP_PVT.G_SUBSTITUTION_FLAG = 'Y' AND MSC_ATP_PVT.G_INV_CTP = 4
3357             --optional_fw start
3358              IF PG_DEBUG in ('Y', 'C') THEN
3359                  msc_sch_wb.atp_debug('Schedule: ' || '___OPTIONAL FW PASS Binary Search End ____');
3360                  msc_sch_wb.atp_debug('Schedule: ' || 'End: l_last_search: ' || l_last_search);
3361                  msc_sch_wb.atp_debug('Schedule: ' || 'End: l_low_seqnum: ' || l_low_seqnum);
3362                  msc_sch_wb.atp_debug('Schedule: ' || 'End: l_high_seqnum: ' || l_high_seqnum);
3363                  msc_sch_wb.atp_debug('Schedule: ' || 'End: l_middle_seqnum: ' || l_middle_seqnum);
3364                  msc_sch_wb.atp_debug('Schedule: ' || 'End: l_atp_rec.requested_ship_date: ' || l_atp_rec.requested_ship_date);
3365                  msc_sch_wb.atp_debug('Schedule: ' || 'End: l_atp_rec.requested_date_quantity: ' || l_atp_rec.requested_date_quantity);
3366                  msc_sch_wb.atp_debug('Schedule: ' || 'End: l_atp_rec.quantity_ordered: ' || l_atp_rec.quantity_ordered);
3367                  msc_sch_wb.atp_debug('Schedule: ' || 'End: MSC_ATP_PVT.G_ATP_COMP_FLAG: ' || MSC_ATP_PVT.G_ATP_COMP_FLAG);
3368                  msc_sch_wb.atp_debug('Schedule: ' || 'End: MSC_ATP_PVT.G_FORWARD_ATP: ' || MSC_ATP_PVT.G_FORWARD_ATP);
3369                  msc_sch_wb.atp_debug('Schedule: ' || 'End: MSC_ATP_PVT.G_SUBSTITUTION_FLAG: ' || MSC_ATP_PVT.G_SUBSTITUTION_FLAG);
3370                  msc_sch_wb.atp_debug('Schedule: ' || 'End: MSC_ATP_PVT.G_DIAGNOSTIC_ATP: ' || MSC_ATP_PVT.G_DIAGNOSTIC_ATP);
3371                  msc_sch_wb.atp_debug('Schedule: ' || 'End: MSC_ATP_PVT.G_INV_CTP := ' || MSC_ATP_PVT.G_INV_CTP); --5158454
3372                  msc_sch_wb.atp_debug('Schedule: ' || 'End: l_g_inv_ctp := ' || l_g_inv_ctp); --5158454
3373              END IF;
3374              IF MSC_ATP_PVT.G_OPTIONAL_FW is null  THEN
3375                 IF PG_DEBUG in ('Y', 'C') THEN
3376                    msc_sch_wb.atp_debug('Schedule: ' || 'Inside IF condition of binary search loop');
3377                 END IF;
3378                 --If I have got the quantity then I don't need to go for more passes
3379                 IF ( (l_atp_rec.quantity_ordered <= l_atp_rec.requested_date_quantity)
3380                      OR (MSC_ATP_PVT.G_ATP_COMP_FLAG = 'N')
3381                     )AND MSC_ATP_PVT.G_FORWARD_ATP = 'Y'
3382                  THEN --for first iteration this will be one
3383 
3384                   IF PG_DEBUG in ('Y', 'C') THEN
3385                      msc_sch_wb.atp_debug('Schedule: ' || 'Exiting the loop from G_FORWARD_ATP is yes ');
3386                   END IF;
3387                   EXIT;
3388                 ELSIF MSC_ATP_PVT.G_FORWARD_ATP = 'N' OR MSC_ATP_PVT.G_SUBSTITUTION_FLAG = 'Y'
3389                                                       OR MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1
3390                                                       OR MSC_ATP_PVT.G_INV_CTP = 5
3391                                                       OR l_g_inv_ctp = 5  --5158454 incases where PDS-ODS switch happens the G_INV_CTP is set to 5 but
3392                                                                           --inside the loop when if profile value is again accessed then it is
3393                                                                           --reset to 4 again.
3394                  THEN
3395                   IF PG_DEBUG in ('Y', 'C') THEN
3396                      msc_sch_wb.atp_debug('Schedule: ' || 'Exiting the loop from G_FORWARD_ATP is no ');
3397                   END IF;
3398                   EXIT;
3399                 END IF;
3400              ELSIF l_atp_rec.requested_date_quantity >=
3401                     l_atp_rec.quantity_ordered  THEN
3402 
3403                IF PG_DEBUG in ('Y', 'C') THEN
3404                    msc_sch_wb.atp_debug('Schedule: ' || 'Inside successful elsif condition of binary search loop');
3405                END IF;
3406                --o-- Delete the old hidden data i.e s/d/r for previous successful run.
3407                --o-- Hide the new successful data i.e s/d/r for this successful run.
3408 
3409                ---- (For performace) First successful case we need not to delete it
3410                IF l_second_good_run = 1 then
3411 
3412                   IF PG_DEBUG in ('Y', 'C') THEN
3413                      msc_sch_wb.atp_debug('Schedule: ' || 'Inside deleting the old successful records');
3414                      msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_peg_id : ' || l_atp_peg_id);
3415                      msc_sch_wb.atp_debug('Schedule: ' || 'l_steal_atp_peg_id.count :' || l_steal_atp_peg_id.count);
3416                      msc_sch_wb.atp_debug('Schedule: ' || 'l_ctp_parent_peg_id.count :' || l_ctp_parent_peg_id.count);
3417                   END IF;
3418 
3419                   MSC_ATP_DB_UTILS.Delete_Pegging(l_atp_peg_id);
3420 
3421                   IF l_ctp_parent_peg_id.count > 0 then
3422                      --Remove pegging and s/d  inventory item id is negative.
3423                      MSC_ATP_DB_UTILS.Delete_SD_Rec(l_ctp_parent_peg_id,l_return_status);
3424                      IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
3425                         IF PG_DEBUG in ('Y', 'C') THEN
3426                            msc_sch_wb.atp_debug('Schedule: ' || 'Error occured in procedure Delete_SD_Rec');
3427                         END IF;
3428                      END IF;
3429                   END IF;
3430 
3431                   IF PG_DEBUG in ('Y', 'C') THEN
3432                      msc_sch_wb.atp_debug('Schedule: ' || 'MSC_ATP_PVT.G_INV_CTP '       || MSC_ATP_PVT.G_INV_CTP);
3433                      msc_sch_wb.atp_debug('Schedule: ' || 'MSC_ATP_PVT.G_ALLOCATED_ATP ' || MSC_ATP_PVT.G_ALLOCATED_ATP);
3434                   END IF;
3435 
3436                   IF l_steal_atp_peg_id.count > 0 and MSC_ATP_PVT.G_INV_CTP = 4 and MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y' then
3437                      --Remove stealing records/pegging
3438                      MSC_ATP_DB_UTILS.Delete_SD_Rec(l_steal_atp_peg_id,l_return_status);
3439                      IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
3440                         IF PG_DEBUG in ('Y', 'C') THEN
3441                             msc_sch_wb.atp_debug('Schedule: ' || 'Error occured in procedure Delete_SD_Rec');
3442                         END IF;
3443                      END IF;
3444                   END IF;
3445 
3446                END IF;
3447 
3448                l_atp_peg_id           := G_FW_PEGGING_ID;       --Make a copy of old successful ids for next deletion
3449                l_ctp_parent_peg_id    := G_FW_CTP_PEGGING_ID;   --Make a copy of old successful ids for next deletion
3450                l_steal_atp_peg_id     := G_FW_STEAL_PEGGING_ID; --Make a copy of old successful ids for next deletion
3451                l_second_good_run      := 1;                     --Set only after one successful run.
3452                l_last_success_atp_rec := l_atp_rec;             --for restoring dates as no extra pass is done
3453                l_high_seqnum          := l_middle_seqnum - 1;
3454 
3455                IF l_high_seqnum >= l_low_seqnum THEN
3456                  IF PG_DEBUG in ('Y', 'C') THEN
3457                    msc_sch_wb.atp_debug('Schedule: ' || 'G_FW_PEGGING_ID ' || G_FW_PEGGING_ID);
3458                    msc_sch_wb.atp_debug('Schedule: ' || 'G_FW_STEAL_PEGGING_ID.COUNT ' || G_FW_STEAL_PEGGING_ID.COUNT);
3459                    msc_sch_wb.atp_debug('Schedule: ' || 'G_FW_CTP_PEGGING_ID.COUNT ' || G_FW_CTP_PEGGING_ID.COUNT);
3460                    msc_sch_wb.atp_debug('Schedule: ' || 'MSC_ATP_PVT.G_INV_CTP '       || MSC_ATP_PVT.G_INV_CTP);
3461                    msc_sch_wb.atp_debug('Schedule: ' || 'MSC_ATP_PVT.G_ALLOCATED_ATP ' || MSC_ATP_PVT.G_ALLOCATED_ATP);
3462                  END IF;
3463 
3464                  IF G_FW_STEAL_PEGGING_ID.COUNT > 0 and MSC_ATP_PVT.G_INV_CTP = 4 and MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y' THEN
3465 
3466                     --Hide the forward stealing records with -1*inventory_item_id
3467                     FOR i in 1..MSC_ATP_PVT.G_FW_STEAL_PEGGING_ID.COUNT LOOP
3468                       IF PG_DEBUG in ('Y', 'C') THEN
3469                          msc_sch_wb.atp_debug('Schedule: ' || 'i := ' || i);
3470                          msc_sch_wb.atp_debug('Schedule: ' || 'G_FW_STEAL_PEGGING_ID ' || G_FW_STEAL_PEGGING_ID(i));
3471                       END IF;
3472                       IF G_FW_STEAL_PEGGING_ID(i) is not null THEN
3473                         MSC_ATP_DB_UTILS.Hide_SD_Rec(G_FW_STEAL_PEGGING_ID(i),l_return_status);
3474                         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
3475                            IF PG_DEBUG in ('Y', 'C') THEN
3476                               msc_sch_wb.atp_debug('Schedule: ' || 'Error occured in procedure Hide_SD_Rec');
3477                            END IF;
3478                         END IF;
3479                       END IF;
3480                     END LOOP;
3481                  END IF;
3482 
3483                  FOR i in 1..MSC_ATP_PVT.G_FW_CTP_PEGGING_ID.COUNT LOOP
3484                    IF PG_DEBUG in ('Y', 'C') THEN
3485                        msc_sch_wb.atp_debug('Schedule: ' || 'i := ' || i);
3486                        msc_sch_wb.atp_debug('Schedule: ' || 'G_FW_CTP_PEGGING_ID ' || G_FW_CTP_PEGGING_ID(i));
3487                    END IF;
3488                    IF G_FW_CTP_PEGGING_ID(i) is not null THEN
3489                     --Hide the CTP records with -1*inventory_item_id
3490                     MSC_ATP_DB_UTILS.Hide_SD_Rec(G_FW_CTP_PEGGING_ID(i),l_return_status);
3491                     IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
3492                        IF PG_DEBUG in ('Y', 'C') THEN
3493                           msc_sch_wb.atp_debug('Schedule: ' || 'Error occured in procedure Hide_SD_Rec');
3494                        END IF;
3495                     END IF;
3496                    END IF;
3497                  END LOOP;
3498                END IF; --end of IF l_high_seqnum >= l_low_seqnum THEN
3499 
3500                l_last_date     := msc_atp_pvt.get_date_from_seqnum(
3501                                                            l_high_seqnum,
3502                                                            l_atp_rec.shipping_cal_code,
3503                                                            l_atp_rec.instance_id
3504                                                                   );
3505 
3506                l_last_successful_date := l_atp_rec.requested_ship_date;
3507 
3508                IF PG_DEBUG in ('Y', 'C') THEN
3509                   msc_sch_wb.atp_debug('Schedule: ' || 'l_high_seqnum: ' || l_high_seqnum);
3510                   msc_sch_wb.atp_debug('Schedule: ' || 'l_last_successful_date: ' || l_last_successful_date);
3511                   msc_sch_wb.atp_debug('Schedule: ' || 'l_last_date: ' || l_last_date);
3512                END IF;
3513 
3514                IF (l_low_seqnum = l_high_seqnum) AND (l_last_successful_date = l_last_date) THEN
3515                  IF PG_DEBUG in ('Y', 'C') THEN
3516                     msc_sch_wb.atp_debug('Schedule: ' || 'Inside first l_low_seqnum = l_high_seqnum of binary search loop');
3517                  END IF;
3518                  l_last_search := 1;
3519                  l_atp_rec.requested_ship_date := l_last_successful_date;
3520                END IF;
3521 
3522              ELSIF l_atp_rec.requested_date_quantity <
3523                     l_atp_rec.quantity_ordered  THEN
3524 
3525                IF PG_DEBUG in ('Y', 'C') THEN
3526                    msc_sch_wb.atp_debug('Schedule: ' || 'Inside unsuccessful elsif  condition of binary search loop');
3527                    msc_sch_wb.atp_debug('Schedule: ' || 'Before call to Remove_Invalid_SD_Rec');
3528                    msc_sch_wb.atp_debug('Schedule: ' || 'G_FW_PEGGING_ID ' || G_FW_PEGGING_ID);
3529                END IF;
3530 
3531                MSC_ATP_DB_UTILS.Delete_Pegging(G_FW_PEGGING_ID);
3532 
3533                IF PG_DEBUG in ('Y', 'C') THEN
3534                  msc_sch_wb.atp_debug('Schedule: ' || 'G_FW_STEAL_PEGGING_ID.COUNT ' || G_FW_STEAL_PEGGING_ID.COUNT);
3535                  msc_sch_wb.atp_debug('Schedule: ' || 'G_FW_CTP_PEGGING_ID.COUNT ' || G_FW_CTP_PEGGING_ID.COUNT);
3536                  msc_sch_wb.atp_debug('Schedule: ' || 'MSC_ATP_PVT.G_INV_CTP '       || MSC_ATP_PVT.G_INV_CTP);
3537                  msc_sch_wb.atp_debug('Schedule: ' || 'MSC_ATP_PVT.G_ALLOCATED_ATP ' || MSC_ATP_PVT.G_ALLOCATED_ATP);
3538                END IF;
3539 
3540                IF G_FW_STEAL_PEGGING_ID.COUNT > 0 AND MSC_ATP_PVT.G_INV_CTP = 4 and MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y' THEN
3541                   FOR i in 1..MSC_ATP_PVT.G_FW_STEAL_PEGGING_ID.COUNT LOOP
3542                    IF PG_DEBUG in ('Y', 'C') THEN
3543                       msc_sch_wb.atp_debug('Schedule: ' || 'i := ' || i);
3544                       msc_sch_wb.atp_debug('Schedule: ' || 'G_FW_STEAL_PEGGING_ID ' || G_FW_STEAL_PEGGING_ID(i));
3545                    END IF;
3546                    IF G_FW_STEAL_PEGGING_ID(i) is not null THEN
3547                       MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(
3548                             G_FW_STEAL_PEGGING_ID(i),
3549                             null,
3550                             l_plan_id,
3551                             MSC_ATP_PVT.UNDO,
3552                             0,
3553                             l_return_status);
3554                     END IF;
3555                   END LOOP;
3556                END IF;
3557 
3558                FOR i in 1..MSC_ATP_PVT.G_FW_CTP_PEGGING_ID.COUNT LOOP
3559                  IF PG_DEBUG in ('Y', 'C') THEN
3560                     msc_sch_wb.atp_debug('Schedule: ' || 'i := ' || i);
3561                     msc_sch_wb.atp_debug('Schedule: ' || 'G_FW_CTP_PEGGING_ID ' || G_FW_CTP_PEGGING_ID(i));
3562                  END IF;
3563 
3564                  IF G_FW_CTP_PEGGING_ID(i) is not null THEN
3565                    MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(
3566                                                G_FW_CTP_PEGGING_ID(i),
3567                                                null,
3568                                                l_plan_id,
3569                                                MSC_ATP_PVT.UNDO,
3570                                                0,
3571                                                l_return_status);
3572                  END IF;
3573                END LOOP;
3574                IF l_low_seqnum = l_high_seqnum THEN
3575                  IF PG_DEBUG in ('Y', 'C') THEN
3576                     msc_sch_wb.atp_debug('Schedule: ' || 'Inside second l_low_seqnum = l_high_seqnum condition of binary search loop');
3577                  END IF;
3578                  l_last_search := 1;
3579                  l_atp_rec.requested_ship_date := l_last_successful_date;
3580                ELSE
3581 
3582                  l_low_seqnum    := l_middle_seqnum + 1;
3583                  l_first_date    :=
3584                        msc_atp_pvt.get_date_from_seqnum(l_low_seqnum,
3585                                                        l_atp_rec.shipping_cal_code,
3586                                                        l_atp_rec.instance_id
3587                                                        );
3588                END IF;
3589              END IF;
3590              IF PG_DEBUG in ('Y', 'C') THEN
3591                 msc_sch_wb.atp_debug('Schedule: ' || 'l_low_seqnum: ' || l_low_seqnum);
3592                 msc_sch_wb.atp_debug('Schedule: ' || 'l_high_seqnum: ' || l_high_seqnum);
3593                 msc_sch_wb.atp_debug('Schedule: ' || 'l_middle_seqnum: ' || l_middle_seqnum);
3594                 msc_sch_wb.atp_debug('Schedule: ' || 'l_last_successful_date: ' || l_last_successful_date);
3595                 msc_sch_wb.atp_debug('Schedule: ' || 'l_last_date: ' || l_last_date);
3596                 msc_sch_wb.atp_debug('Schedule: ' || 'G_LOOP_COUNT end: ' || G_LOOP_COUNT); --5211558
3597                 msc_sch_wb.atp_debug('Schedule: ' || 'l_loop_count end: ' || l_loop_count);
3598                 msc_sch_wb.atp_debug('Schedule: ' || 'MSC_ATP_PVT.G_ATP_COMP_FLAG: ' || MSC_ATP_PVT.G_ATP_COMP_FLAG);
3599              END IF;
3600              l_loop_count := l_loop_count + 1;
3601              --If this is the last search then we need to exit the loop and restore the s/d records and dates
3602              IF l_last_search = 1 THEN
3603                 IF PG_DEBUG in ('Y', 'C') THEN
3604                  msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_peg_id : ' || l_atp_peg_id);
3605                  msc_sch_wb.atp_debug('Schedule: ' || 'l_ctp_parent_peg_id.count : ' || l_ctp_parent_peg_id.count);
3606                 END IF;
3607 
3608                 IF l_ctp_parent_peg_id.count > 0 then
3609                    --Restore the details from pegging, org is negative
3610                    MSC_ATP_DB_UTILS.Restore_SD_Rec(l_ctp_parent_peg_id,l_return_status);
3611                    IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
3612                       IF PG_DEBUG in ('Y', 'C') THEN
3613                          msc_sch_wb.atp_debug('Schedule: ' || 'Error occured in procedure Restore_SD_Rec');
3614                       END IF;
3615                    END IF;
3616                 END IF;
3617 
3618                 IF PG_DEBUG in ('Y', 'C') THEN
3619                    msc_sch_wb.atp_debug('Schedule: ' || 'l_steal_atp_peg_id.count :' || l_steal_atp_peg_id.count);
3620                    msc_sch_wb.atp_debug('Schedule: ' || 'MSC_ATP_PVT.G_ALLOCATED_ATP :' || MSC_ATP_PVT.G_ALLOCATED_ATP);
3621                    msc_sch_wb.atp_debug('Schedule: ' || 'MSC_ATP_PVT.G_INV_CTP :' || MSC_ATP_PVT.G_INV_CTP);
3622                 END IF;
3623 
3624                 IF l_steal_atp_peg_id.count > 0 AND MSC_ATP_PVT.G_INV_CTP = 4 and MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y' THEN
3625                    MSC_ATP_DB_UTILS.Restore_SD_Rec(l_steal_atp_peg_id,l_return_status);
3626                    IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
3627                       IF PG_DEBUG in ('Y', 'C') THEN
3628                          msc_sch_wb.atp_debug('Schedule: ' || 'Error occured in procedure Restore_SD_Rec');
3629                       END IF;
3630                    END IF;
3631                 END IF;
3632 
3633                 --Restore atp_rec also from the last value.
3634                 l_atp_rec := l_last_success_atp_rec;
3635 
3636                 EXIT; --exit the loop
3637              END IF;
3638             END LOOP;
3639             --After the loop completes we need to reset these to correct values
3640             --only needed in f/w pass as in b/w pass all will be set correctly.
3641             IF PG_DEBUG in ('Y', 'C') THEN
3642                msc_sch_wb.atp_debug('Schedule: Resetting after loop of optional_fw');
3643                msc_sch_wb.atp_debug('Schedule: ' || 'l_quan_ordered: ' || l_quan_ordered);
3644                msc_sch_wb.atp_debug('Schedule: ' || 'G_REQUESTED_SHIP_DATE: ' || G_REQUESTED_SHIP_DATE);
3645                msc_sch_wb.atp_debug('Schedule: ' || 'l_request_date_qty: ' || l_request_date_qty);
3646                msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.available_quantity: ' || l_atp_rec.available_quantity);
3647                msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.combined_requested_date_qty: ' || l_atp_rec.combined_requested_date_qty);
3648             END IF;
3649             IF MSC_ATP_PVT.G_OPTIONAL_FW is not null then
3650                l_atp_rec.quantity_ordered            := l_quan_ordered;
3651                l_atp_rec.requested_ship_date         := G_REQUESTED_SHIP_DATE;
3652                l_atp_rec.requested_date_quantity     := l_request_date_qty;
3653                l_atp_rec.parent_so_quantity          := l_quan_ordered;
3654                l_atp_rec.available_quantity          := LEAST(INFINITE_NUMBER,(l_atp_rec.available_quantity +
3655                                                            l_request_date_qty));
3656                l_atp_rec.combined_requested_date_qty := LEAST(INFINITE_NUMBER,l_request_date_qty);
3657             END IF;
3658             --optional_fw end the loop for dates
3659 
3660             IF PG_DEBUG in ('Y', 'C') THEN
3661                msc_sch_wb.atp_debug('Schedule: ' || 'After ATP_Check for this l_atp_rec ');
3662             END IF;
3663             -- check the return status
3664             IF l_return_status = MSC_ATP_PVT.CTO_OSS_ERROR THEN
3665                    l_atp_table.Error_Code(i):= MSC_ATP_PVT.OSS_SOURCING_ERROR;
3666             ELSIF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
3667                 RAISE FND_API.G_EXC_ERROR;
3668             END IF;
3669 
3670             --bug 3328421: For past dues sales orders we start ATP check from first working
3671             -- day after sysdate as per shipping calender. Therefore, days late for request date type = ship
3672             -- should be calculated from this date as well.
3673             -- For req date type = arrival: Days late should be calculated from first work day after sysdate
3674             --- as per receiving party's receiving calendas. Since UI doesn't know this date we populate this date
3675 
3676             IF l_atp_table.requested_ship_date(i) is not null then
3677                 l_atp_table.first_valid_ship_arrival_date(i) := l_sys_next_date;
3678 
3679             ELSE
3680                 --arrival
3681                 l_atp_table.first_valid_ship_arrival_date(i) := l_sysdate_orc;
3682             END IF;
3683 
3684             l_stmt := 220;
3685     	    IF PG_DEBUG in ('Y', 'C') THEN
3686     	       msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
3687     	    END IF;
3688 
3689 
3690             -- now we get the results back and we need to analyze them
3691             -- here is the logic:
3692             -- if l_multi_scenario = 1
3693             --      < case 1 >
3694             --      if error_code = ALLSUCCESS and it is a preferred WH
3695             --         this is the answer we want, exit the loop
3696             --      else
3697             --         add the results to temp table or arr table
3698             --      end if
3699             -- else
3700             --      < case 2 >
3701             --      if error_code = ALLSUCESS or
3702             --      inside the latest acceptable range
3703             --          this is the answer we want, exit the loop
3704             --      else if we do have ship_date and temp table is empty
3705             --          add the results to temp table
3706             --      end if
3707             -- end
3708 
3709             -- ATP Override -- Agilent changes Begin
3710 
3711             -- bug 2795053-reopen (ssurendr)
3712             --s_cto_rearch: do not honor atp lead time
3713             l_dmd_satisfied_date := l_atp_rec.ship_date;
3714             /*
3715                l_dmd_satisfied_date := MSC_CALENDAR.DATE_OFFSET(
3716                                                    l_atp_rec.organization_id,
3717                                                    p_instance_id,
3718                                                    1,
3719                                                    l_atp_rec.ship_date,
3720                                                    -NVL(l_atp_rec.atp_lead_time, 0));
3721             */
3722             --e_cto_rearch
3723             IF PG_DEBUG in ('Y', 'C') THEN
3724               msc_sch_wb.atp_debug('Schedule: ' || 'l_dmd_satisfied_date: ' || l_dmd_satisfied_date);
3725             END IF;
3726 
3727             IF ( (NVL(l_atp_rec.Override_Flag, 'N') = 'Y')
3728                AND l_atp_rec.Action <> ATPQUERY ) THEN
3729 
3730                /* ship_rec_cal changes begin */
3731                IF PG_DEBUG in ('Y', 'C') THEN
3732                   msc_sch_wb.atp_debug('Schedule: ' || '_______________Override Scenario_______________ ');
3733                   msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_table.requested_ship_date(i): ' || l_atp_table.requested_ship_date(i));
3734                   msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_table.requested_arrival_date(i): ' || l_atp_table.requested_arrival_date(i));
3735                END IF;
3736                IF (l_atp_table.requested_ship_date(i) IS NOT NULL) THEN
3737                   l_atp_rec.ship_date := l_atp_table.requested_ship_date(i);
3738 
3739                   -- Bug 3520301 - dont consider first calendar in override cases
3740                   l_atp_rec.arrival_date := MSC_CALENDAR.THREE_STEP_CAL_OFFSET_DATE(
3741         					l_atp_rec.ship_date, MSC_CALENDAR.FOC, 1,
3742         					l_atp_rec.intransit_cal_code, nvl(l_atp_rec.delivery_lead_time, 0), 1,
3743         					l_atp_rec.receiving_cal_code, 1, p_instance_id);
3744                ELSE
3745                   l_atp_rec.arrival_date := l_atp_table.requested_arrival_date(i);
3746 
3747                   l_atp_rec.ship_date := MSC_CALENDAR.THREE_STEP_CAL_OFFSET_DATE(
3748 						-- Bug 3520301 - dont consider first calendar in override cases
3749 						-- l_atp_table.requested_arrival_date(i), l_atp_rec.receiving_cal_code, -1,
3750 						l_atp_table.requested_arrival_date(i), MSC_CALENDAR.FOC, -1,
3751 						l_atp_rec.intransit_cal_code, -1 * nvl(l_atp_rec.delivery_lead_time, 0), -1,
3752 						l_atp_rec.shipping_cal_code, -1, p_instance_id);
3753                END IF;
3754                IF PG_DEBUG in ('Y', 'C') THEN
3755                   msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.ship_date: ' || l_atp_rec.ship_date);
3756                   msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.arrival_date: ' || l_atp_rec.arrival_date);
3757                END IF;
3758 
3759             /* If ship date returned is greater than requested ship date move it to OSC */
3760             ELSIF trunc(l_atp_rec.ship_date) > trunc(l_atp_rec.requested_ship_date) THEN
3761                l_atp_rec.ship_date := MSC_CALENDAR.NEXT_WORK_DAY(
3762                                                 l_atp_rec.shipping_cal_code,
3763                                                 p_instance_id,
3764                                                 l_atp_rec.ship_date);
3765                IF PG_DEBUG in ('Y', 'C') THEN
3766                   msc_sch_wb.atp_debug('Schedule: ' || 'ship_date > req_ship_date');
3767                   msc_sch_wb.atp_debug('Schedule: ' || '_______________After validating ship date on OSC_______________ ');
3768                   msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.ship_date: ' || l_atp_rec.ship_date);
3769                END IF;
3770             END IF;
3771             /* ship_rec_cal changes end */
3772 
3773             l_stmt := 230;
3774             --- Bug 1621816 Calculate arrival date and take date offset into account
3775             IF (l_atp_rec.ship_date IS NOT NULL)  AND (l_atp_rec.arrival_date IS NULL) THEN
3776 
3777                 /* ship_rec_cal changes begin */
3778                 l_atp_rec.arrival_date := MSC_CALENDAR.THREE_STEP_CAL_OFFSET_DATE(
3779         					l_atp_rec.ship_date, l_atp_rec.shipping_cal_code, 1,
3780         					l_atp_rec.intransit_cal_code, nvl(l_atp_rec.delivery_lead_time, 0), 1,
3781         					l_atp_rec.receiving_cal_code, 1, p_instance_id) ;
3782                                           --4460369+ MSC_ATP_PVT.G_END_OF_DAY;
3783                 /* ship_rec_cal changes end */
3784 
3785                 IF l_atp_rec.requested_arrival_date is not null THEN
3786                    l_atp_rec.arrival_date := TRUNC(GREATEST(l_atp_rec.requested_arrival_date,
3787                                                l_atp_rec.arrival_date));--4460369 + MSC_ATP_PVT.G_END_OF_DAY;
3788                 END IF;
3789 
3790                 IF PG_DEBUG in ('Y', 'C') THEN
3791                         msc_sch_wb.atp_debug('Schedule: l_atp_rec.arrival_date = '||
3792                                 to_char(l_atp_rec.arrival_date, 'DD-MON-YYYY HH24:MI:SS'));
3793                 END IF;
3794 
3795             END IF;
3796             /*plan by request date changes begin */
3797             IF l_atp_rec.requested_arrival_date IS NULL THEN
3798 	        -- date type is ship
3799 	    	l_original_req_arrival_date     := MSC_CALENDAR.THREE_STEP_CAL_OFFSET_DATE(
3800                                                         NVL(l_atp_rec.original_request_date,l_atp_rec.requested_ship_date),
3801                                                         l_atp_rec.shipping_cal_code, 1,
3802                                                         l_atp_rec.intransit_cal_code, nvl(l_atp_rec.delivery_lead_time, 0), 1,
3803                                                         l_atp_rec.receiving_cal_code, 1, p_instance_id) ;
3804                                                         --4460369 + MSC_ATP_PVT.G_END_OF_DAY;
3805             ELSE
3806 	    	-- date type is arrival
3807 	    	l_original_req_arrival_date     := NVL(l_atp_rec.original_request_date,l_atp_rec.requested_arrival_date);
3808             END IF;
3809             IF PG_DEBUG in ('Y', 'C') THEN
3810                 msc_sch_wb.atp_debug('Schedule: request arrival date = '||
3811                         to_char(l_atp_rec.requested_arrival_date, 'DD-MON-YYYY HH24:MI:SS'));
3812             END IF;
3813             l_req_arr_date(i) := l_original_req_arrival_date;
3814             /*plan by request date changes end */
3815             -- populate the output info l_atp_table
3816             IF PG_DEBUG in ('Y', 'C') THEN
3817                msc_sch_wb.atp_debug('Schedule: ' || 'Before Assigning the values to l_atp_table');
3818                msc_sch_wb.atp_debug('Schedule: ' || 'i = '||to_char(i));
3819                msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.error_code='||l_atp_rec.error_code);
3820                msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.org_id='|| l_atp_rec.Organization_Id);
3821                msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.Ship_Method='|| l_atp_rec.Ship_Method);
3822                msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.Delivery_Lead_Time='|| l_atp_rec.Delivery_Lead_Time);
3823                msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.Ship_Date='||l_atp_rec.Ship_Date);
3824                msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.Available_Quantity='||
3825                                   l_atp_rec.Available_Quantity);
3826                msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.Requested_Date_Quantity='||
3827                                   l_atp_rec.Requested_Date_Quantity);
3828                msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.error_code='||l_atp_rec.error_code);
3829                --bug3709707 added debug messages
3830                msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.request_item_name='||l_atp_rec.request_item_name);
3831                msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.inventory_item_name='||l_atp_rec.inventory_item_name);
3832                msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.original_item_name='||l_atp_rec.original_item_name);
3833                msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_table.Inventory_item_id(i)='||l_atp_table.Inventory_item_id(i));
3834                msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.atf_date='||l_atp_rec.atf_date);
3835             END IF;
3836             --- subst
3837             IF MSC_ATP_PVT.G_SUBSTITUTION_FLAG = 'Y' THEN
3838 
3839                IF l_atp_rec.inventory_item_id <> l_atp_table.Inventory_item_id(i) THEN
3840                   --- substitution has occured. Load subst_flag for substitution workflow
3841                   l_atp_table.subst_flag(i) := 1;
3842                END IF;
3843 
3844                IF l_atp_rec.atf_date is not null THEN --bug3467631
3845                  l_atp_table.Inventory_Item_Name(i) := l_atp_rec.request_item_name;
3846                  --bug3709707
3847                  l_atp_table.Inventory_item_id(i) := l_atp_rec.request_item_id;
3848                ELSE
3849                  l_atp_table.Inventory_Item_Name(i) := l_atp_rec.inventory_item_name;
3850                  --bug3709707
3851                  l_atp_table.Inventory_item_id(i) := l_atp_rec.inventory_item_id;
3852                END IF;
3853 
3854                IF PG_DEBUG in ('Y', 'C') THEN
3855                    msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_table.Inventory_Item_Name(i) '||l_atp_table.Inventory_Item_Name(i));
3856                END IF;
3857                --bug3709707 used original item id and name
3858                --l_atp_table.Inventory_item_id(i) := l_atp_rec.inventory_item_id;
3859                --l_atp_table.request_item_id(i) := l_atp_rec.request_item_id;
3860                --l_atp_table.request_item_name(i) := l_atp_rec.request_item_name;
3861                l_atp_table.request_item_id(i) := l_atp_rec.original_item_id;
3862                l_atp_table.request_item_name(i) := l_atp_rec.original_item_name;
3863 
3864                IF l_atp_table.req_item_detail_flag(i) = 1 THEN
3865                   l_atp_table.req_item_req_date_qty(i) := l_atp_rec.req_item_req_date_qty;
3866 
3867 		  -- Bug 2259824
3868 		  l_atp_table.req_item_available_date(i) := TRUNC(l_atp_rec.req_item_available_date) ;
3869 									--4460369+ MSC_ATP_PVT.G_END_OF_DAY;
3870                   IF PG_DEBUG in ('Y', 'C') THEN
3871                         msc_sch_wb.atp_debug('Schedule: 2259824 l_atp_table.req_item_available_date(i) = '||
3872                                 to_char(l_atp_table.req_item_available_date(i), 'DD-MON-YYYY HH24:MI:SS'));
3873                   END IF;
3874 
3875 		  l_atp_table.req_item_available_date_qty(i) := l_atp_rec.req_item_available_date_qty;
3876 		  --bug3709707 commenting as assigned above
3877 		  --l_atp_table.request_item_name(i) := l_atp_rec.request_item_name;
3878                   --l_atp_table.inventory_item_name(i) := l_atp_rec.inventory_item_name;
3879 
3880                END IF;
3881             ELSE
3882 
3883                -- 1219371
3884              /* Modularize Item and Org Info */
3885              IF (G_ITEM_INFO_REC.instance_id = p_instance_id) AND
3886                 (G_ITEM_INFO_REC.organization_id = l_atp_rec.organization_id) AND
3887                 (G_ITEM_INFO_REC.sr_inv_item_id = l_atp_table.inventory_item_id(i))
3888              THEN
3889                 -- If item has not changed then re-use
3890                 l_atp_table.Inventory_Item_Name(i) := NVL(G_ITEM_INFO_REC.item_name,
3891                                                 l_atp_table.Inventory_Item_Name(i)) ;
3892              ELSE
3893                 -- If item has changed then make the call.
3894                l_atp_table.Inventory_Item_Name(i) := NVL(
3895                                  MSC_ATP_FUNC.get_inv_item_name(p_instance_id,
3896                                                    --- bug 2152184
3897                                                    l_atp_table.inventory_item_id(i),
3898                                                    --l_requested_item_id, -- 1197692
3899                                                    l_atp_rec.organization_id),
3900                                  l_atp_table.Inventory_Item_Name(i)) ;
3901              END IF;
3902              /* Modularize Item and Org Info */
3903 		   IF PG_DEBUG in ('Y', 'C') THEN
3904 		      msc_sch_wb.atp_debug ('Schedule: ' || 'after item name');
3905 		   END IF;
3906             END IF; --- IF MSC_ATP_PVT.G_SUBSTITUTION_FLAG = 'Y'
3907             l_atp_table.Source_Organization_Id(i):=
3908                                 l_atp_rec.organization_id;
3909             IF PG_DEBUG in ('Y', 'C') THEN
3910                msc_sch_wb.atp_debug ('Schedule: ' || 'after source org id ');
3911             END IF;
3912 
3913             -- since we do a uom conversion, so we need to repopulate
3914             -- the primary uom and primary uom quantity back
3915 
3916             l_atp_table.Quantity_UOM(i) := l_atp_rec.quantity_uom;
3917             l_atp_table.Quantity_Ordered(i) := l_atp_rec.quantity_ordered;
3918 
3919             /* Modularize Item and Org Info */
3920             MSC_ATP_PROC.get_global_org_info (p_instance_id,
3921                                               l_atp_rec.organization_id);
3922             l_atp_table.Source_Organization_Code(i) := G_ORG_INFO_REC.org_code;
3923             /*l_atp_table.Source_Organization_Code(i):=
3924                                 MSC_ATP_FUNC.get_org_code(p_instance_id, l_atp_rec.organization_id);
3925              Modularize Item and Org Info */
3926             IF PG_DEBUG in ('Y', 'C') THEN
3927                msc_sch_wb.atp_debug ('Schedule: ' || 'after code');
3928             END IF;
3929 
3930             l_atp_table.Ship_Method(i) := l_atp_rec.ship_method;
3931             l_atp_table.Delivery_Lead_Time(i) := l_atp_rec.delivery_lead_time;
3932             -- Bug 2748730. Return the end of day as the timestamp for outgoing dates.
3933 	    -- For ship date and arrival date, this is the right place to move the date
3934 	    -- to the end of day.
3935 	    l_atp_table.Ship_Date(i) := l_atp_rec.ship_date;--4460369 + MSC_ATP_PVT.G_END_OF_DAY;
3936 	    l_atp_table.arrival_date(i) := l_atp_rec.arrival_date;--4460369 + MSC_ATP_PVT.G_END_OF_DAY;
3937 
3938 	    l_atp_table.Available_Quantity(i):= l_atp_rec.available_quantity;
3939             l_atp_table.Requested_Date_Quantity(i) := l_atp_rec.requested_date_quantity;
3940 
3941             l_atp_table.Scenario_Id(i) := j;
3942             l_atp_table.End_Pegging_Id(i) := MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
3943 
3944             -- time_phased_atp
3945             l_atp_table.atf_date(i) := l_atp_rec.atf_date;
3946             l_atp_table.atp_flag(i) := l_atp_flag; --4460369
3947             l_atp_table.atp_components_flag(i) := l_atp_comp_flag; --4460369
3948 
3949             -- we may adjust the error code later based on latest accep date
3950 
3951             -- 1873918: PDS-ODS fix
3952             IF (l_atp_table.Error_Code(i) <> PDS_TO_ODS_SWITCH) OR
3953                (l_atp_rec.error_code <> ALLSUCCESS) THEN
3954               l_atp_table.Error_Code(i) := l_atp_rec.error_code;
3955             END IF;
3956 
3957             l_stmt := 240;
3958 
3959             /* ship_rec_cal commented the code as l_ship_date is not used
3960             -- bug 1609918 To retain the req_date if ship_date is less than
3961 	    -- req date so that time could be retained
3962 
3963             l_ship_date := NVL(l_atp_rec.requested_ship_date,
3964                                l_atp_rec.requested_arrival_date - nvl(l_atp_rec.delivery_lead_time,0));
3965 
3966             -- bug 1929645.  In case we have the following case,
3967             -- today D0, requested_ship_date D10 5pm is a non-working day
3968             -- and l_atp_rec.ship_date is D9 (previous working day) 0:00.
3969             -- we want to return ship date as D9 0:00 instead of D10 5pm.*/
3970 
3971 -- old bad code was here
3972             IF l_multi_scenario = 0 THEN
3973                 -- case 2
3974                 IF PG_DEBUG in ('Y', 'C') THEN
3975                    msc_sch_wb.atp_debug('Schedule: ' || 'In case 2: l_multi_scenario = 0 ');
3976                 END IF;
3977                 -- since case 2 is what oe wants, we can always set this
3978                 -- flag to 'Y'
3979                 l_atp_table.OE_Flag(i) := 'Y';
3980 
3981                 l_stmt := 250;
3982 
3983                 -- 1497368
3984                 IF l_ship_count > 1 THEN
3985                   -- ship set exists
3986 
3987                   IF i = 1 THEN
3988                   -- this is the begin of a set, we need to clean the
3989                   -- temp tables for details before we add data into them
3990                       l_curr_atp_period := l_null_atp_period;
3991                       l_curr_atp_supply_demand := l_null_atp_supply_demand;
3992                   END IF;
3993 
3994                   MSC_ATP_PROC.Details_Output(l_atp_period,
3995                                  l_atp_supply_demand,
3996                                  l_curr_atp_period,
3997                                  l_curr_atp_supply_demand,
3998                                  l_return_status);
3999 
4000                 END IF;
4001                 ---diag_atp
4002                 ---In diagnostic ATP we continue as if we are able to meet the demand on request date
4003                 IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 THEN
4004                     IF PG_DEBUG in ('Y', 'C') THEN
4005                        msc_sch_wb.atp_debug('Schedule: ' || 'Reset The error code for diag ATP mode');
4006                     END IF;
4007                     l_atp_rec.error_code := ALLSUCCESS;
4008                     --here we mark the constraint path for UI so that they can show the constraiend nodes
4009                     ---bug 27744106: Improve the query for performance
4010                     /*-- performance dsting merged queries, added distinct
4011                     update mrp_atp_details_temp
4012                     set    constrained_path = 1
4013                     where  record_type = 3
4014                     and    session_id = MSC_ATP_PVT.G_SESSION_ID
4015                     and    pegging_id in
4016                     (select distinct madt.pegging_id
4017                      from   mrp_atp_details_temp  madt
4018                      where  record_type = 3
4019                      and    session_id = MSC_ATP_PVT.G_SESSION_ID
4020                      start with madt.pegging_id in (
4021                         select pegging_id
4022                         from mrp_atp_details_temp madt1
4023                         where session_id = MSC_ATP_PVT.G_SESSION_ID
4024                         and   record_type = 3
4025                         and   end_pegging_id = MSC_ATP_PVT.G_DEMAND_PEGGING_ID
4026                         and   constraint_type is not null)
4027                      connect by madt.pegging_id = PRIOR parent_pegging_id);
4028                      */
4029                      --3751114: replace hardcoded values with local varibales
4030                     --- so that they do not show up as literals
4031                     UPDATE mrp_atp_details_temp
4032                     set    constrained_path = l_constraint_path_flag
4033                     where  record_type = l_peg_record_type
4034                     and    session_id = MSC_ATP_PVT.G_SESSION_ID
4035                     and    pegging_id in
4036                        (select pegging_id
4037                         from   mrp_atp_details_temp
4038                         where  record_type = l_peg_record_type
4039                         and    session_id = MSC_ATP_PVT.G_SESSION_ID
4040                         start with session_id = MSC_ATP_PVT.G_SESSION_ID
4041                         and   record_type = l_peg_record_type
4042                         and   end_pegging_id = MSC_ATP_PVT.G_DEMAND_PEGGING_ID
4043                         and   constraint_type is not null
4044                         connect by pegging_id = PRIOR parent_pegging_id
4045                                 and record_type = l_peg_record_type
4046                                 and  session_id = MSC_ATP_PVT.G_SESSION_ID);
4047 
4048 
4049                 END IF;
4050 
4051                 ---bug 1819638: Compare ship_date with greatest of lat_acceptable_date and l_sys_next_date
4052 		IF PG_DEBUG in ('Y', 'C') THEN
4053 		   msc_sch_wb.atp_debug('Schedule: ' || 'stmt16 - Before IF l_atp_rec.error_code = ALLSUCCESS: ' ||l_latest_acceptable_date);
4054                    msc_sch_wb.atp_debug('Schedule: ' || 'stmt16 - error_code: ' ||l_atp_rec.error_code);
4055                    msc_sch_wb.atp_debug('Schedule: ' || 'stmt16 - l_latest_acceptable_date:  ' ||l_latest_acceptable_date);
4056                    msc_sch_wb.atp_debug('Schedule: ' || 'stmt16 - l_atp_rec.override_flag: ' || l_atp_rec.override_flag);
4057                    msc_sch_wb.atp_debug('Schedule: ' || 'stmt16 - l_atp_rec.action: ' || l_atp_rec.action);
4058                 END IF;
4059 
4060                 -- dsting setproc
4061                 l_atp_table.instance_id(i) := p_instance_id;
4062                 IF NVL(l_atp_table.override_flag(i), 'N') = 'Y' THEN
4063 
4064                    /* ship_rec_cal do we need this???
4065                    l_atp_table.ship_date(i) := TRUNC(l_atp_table.requested_ship_date(i)) + MSC_ATP_PVT.G_END_OF_DAY;
4066 
4067                    IF l_atp_table.ship_date(i) IS NULL THEN
4068                       l_atp_table.ship_date(i) := TRUNC(MSC_CALENDAR.prev_work_day(
4069                                                 l_atp_table.source_organization_id(i),
4070                                                 p_instance_id,
4071                                                 MSC_CALENDAR.TYPE_DAILY_BUCKET,
4072                                                 l_atp_table.requested_arrival_date(i)
4073                                                 - nvl(l_atp_table.delivery_lead_time(i), 0)
4074                                             )) + MSC_ATP_PVT.G_END_OF_DAY;
4075                    END IF;
4076 
4077                    l_atp_table.arrival_date(i) := TRUNC(l_atp_table.requested_arrival_date(i))
4078                                                 + MSC_ATP_PVT.G_END_OF_DAY;
4079                    IF l_atp_table.arrival_date(i) IS NULL THEN
4080                       -- dsting 2815549
4081                       IF l_atp_table.customer_id(i) is null and l_atp_table.organization_id(i) is not null THEN
4082                          l_atp_table.arrival_date(i) := TRUNC(MSC_CALENDAR.next_work_day(
4083                                                    l_atp_table.organization_id(i),
4084                                                    p_instance_id,
4085                                                    MSC_CALENDAR.TYPE_DAILY_BUCKET,
4086                                                    l_atp_table.requested_ship_date(i)
4087                                                    + nvl(l_atp_table.delivery_lead_time(i), 0)
4088                                                )) + MSC_ATP_PVT.G_END_OF_DAY;
4089                       ELSE
4090                          l_atp_table.arrival_date(i) := TRUNC(l_atp_table.requested_ship_date(i)
4091                             + nvl(l_atp_table.delivery_lead_time(i), 0)) + MSC_ATP_PVT.G_END_OF_DAY;
4092                       END IF;
4093                    END IF;*/
4094 
4095                    IF PG_DEBUG in ('Y', 'C') THEN
4096                       msc_sch_wb.atp_debug('override ship_date: ' || l_atp_table.ship_date(i));
4097                       msc_sch_wb.atp_debug('override arrival_date: ' || l_atp_table.arrival_date(i));
4098                    END IF;
4099                 ELSE
4100                    -- xxx dsting hack to make sure things don't ship before sysdate
4101                    -- for an overridden ship set with req_arrival_date
4102                    /* ship_rec_cal
4103                    l_atp_table.earliest_acceptable_date(i) := l_sys_next_date;*/
4104                    l_atp_table.earliest_acceptable_date(i) := MSC_CALENDAR.NEXT_WORK_DAY(
4105                                                                         l_shipping_cal_code,
4106                                                                         p_instance_id,
4107                                                                         sysdate);
4108                 END IF; -- override
4109 
4110                 IF PG_DEBUG in ('Y', 'C') THEN
4111                    msc_sch_wb.atp_debug('ship_date: ' || l_atp_table.ship_date(i));
4112                    msc_sch_wb.atp_debug('arrival_date: ' || l_atp_table.arrival_date(i));
4113                 END IF;
4114                 --bug3439591 start
4115                 --L_MOVE_PAST_DUE_TO_SYSDATE := NVL(FND_PROFILE.value('MSC_MOVE_PAST_DUE_TO_SYSDATE'), 'Y'); -- Bug 5584634/5618929
4116                 L_MOVE_PAST_DUE_TO_SYSDATE := MSC_ATP_PVT.G_MOV_PAST_DUE_SYSDATE_PROF;--6316476
4117                 IF PG_DEBUG in ('Y', 'C') THEN
4118                    msc_sch_wb.atp_debug('Schedule: ' || 'MOVE_PAST_DUE_TO_SYS_DATE :'|| L_MOVE_PAST_DUE_TO_SYSDATE);
4119                 END IF;
4120                 if L_MOVE_PAST_DUE_TO_SYSDATE = 'Y' THEN  -- Bug 5584634/5618929
4121                 IF   l_atp_table.requested_ship_date(i) IS NOT NULL THEN
4122                      l_atp_table.latest_acceptable_date(i) := GREATEST(NVL(l_atp_table.latest_acceptable_date(i),
4123                                                                 l_atp_table.requested_ship_date(i)),l_atp_table.requested_ship_date(i),
4124                                                                 l_sys_next_date);
4125                 ELSE
4126                    IF l_past_due_ship_date = 2 THEN
4127                      l_atp_table.latest_acceptable_date(i) := GREATEST(NVL(l_atp_table.latest_acceptable_date(i),
4128                                                                 l_atp_table.requested_arrival_date(i)),l_atp_table.requested_arrival_date(i),
4129                                                                 l_sysdate_orc);
4130                    ELSE --bug4291375 If requested ship date is past due date then LAD needs to be offseted by the lead time.
4131                      -- Calculate date after offset lead time
4132                      l_sysdate_orc_new := MSC_CALENDAR.THREE_STEP_CAL_OFFSET_DATE(
4133                                                 l_trunc_sysdate, l_atp_rec.shipping_cal_code, 1,
4134                                                 l_atp_rec.intransit_cal_code, nvl(l_atp_rec.delivery_lead_time, 0), 1,
4135                                                 l_atp_rec.receiving_cal_code, 1, p_instance_id);
4136                      -- Calculate LAD after lead time taken in consideration
4137                      l_atp_table.latest_acceptable_date(i) := GREATEST(NVL(l_atp_table.latest_acceptable_date(i),
4138                                                                 l_atp_table.requested_arrival_date(i)),l_atp_table.requested_arrival_date(i),
4139                                                                 l_sysdate_orc_new);
4140                    END IF;
4141                 END IF;
4142 
4143                 -- bug 8996352
4144                 ElSE
4145                     IF   l_atp_table.requested_ship_date(i) IS NOT NULL THEN
4146                          l_atp_table.latest_acceptable_date(i) := GREATEST(NVL(l_atp_table.latest_acceptable_date(i),
4147                                                                 l_atp_table.requested_ship_date(i)),l_atp_table.requested_ship_date(i));
4148                     ELSE
4149                          l_atp_table.latest_acceptable_date(i) := GREATEST(NVL(l_atp_table.latest_acceptable_date(i),
4150                                                                 l_atp_table.requested_arrival_date(i)),l_atp_table.requested_arrival_date(i));
4151                     END IF;
4152 								END IF;
4153 
4154                 IF PG_DEBUG in ('Y', 'C') THEN
4155                    msc_sch_wb.atp_debug('LAD: ' || l_atp_table.latest_acceptable_date(i));
4156                    msc_sch_wb.atp_debug('l_sysdate_orc: ' || l_sysdate_orc);
4157                    msc_sch_wb.atp_debug('l_sysdate_osc: ' || l_sys_next_date);
4158                    msc_sch_wb.atp_debug('l_sysdate_orc_new: ' || l_sysdate_orc_new); --bug4291375
4159                    msc_sch_wb.atp_debug('l_past_due_ship_date : ' || l_past_due_ship_date); --bug4291375
4160                    msc_sch_wb.atp_debug('requested_arrival_date: ' || l_atp_table.requested_arrival_date(i));
4161                    msc_sch_wb.atp_debug('requested_ship_date: ' || l_atp_table.requested_ship_date(i));
4162                 END IF;
4163                 --bug3439591 end
4164                 -- dsting setproc
4165                 MSC_ATP_PROC.Process_Set_Line(l_atp_table, i, l_line_status);
4166                 Process_Time_Stamp_Errors(l_atp_table,i);
4167                 msc_sch_wb.atp_debug('ship_date:(After) ' ||  to_char(l_atp_table.ship_date(i), 'DD-MON-YYYY HH24:MI:SS'));
4168                 msc_sch_wb.atp_debug('Arrival_date:(After) ' ||  to_char(l_atp_table.arrival_date(i), 'DD-MON-YYYY HH24:MI:SS'));
4169                 IF l_ship_count = 1 and l_arrival_count = 1 THEN
4170                    l_set_status := l_line_status;
4171                 END iF;
4172                 -- 2902129
4173                 IF l_arrival_count > 1 THEN
4174                    l_overall_status := l_line_status;
4175                 END IF;
4176                 IF l_line_status = ALLSUCCESS THEN
4177                   l_stmt := 260;
4178                   IF PG_DEBUG in ('Y', 'C') THEN
4179                      msc_sch_wb.atp_debug('Schedule: ' || 'inside the range in case 2');
4180                   END IF;
4181 
4182                   -- we are inside the latest acceptable range
4183                   -- remember, l_atp_rec.error_code = ALLSUCCESS means
4184                   -- we can satisfy the request on the request date.
4185 
4186                   -- if it is just an inquiry, we need to rollback
4187 
4188                   -- 2266808: krajan : 04/16
4189 
4190                   --IF (l_atp_rec.Action = ATPQUERY) THEN
4191                     -- 1225631: we only undo when
4192                     -- 1: the ship set is done (ship set exists) or
4193                     -- 2: the arrival set is done (arrival set exists) or
4194                     -- 3: the single line is done (no set exists)
4195 
4196                   IF (l_atp_rec.Action = ATPQUERY) and
4197                       (
4198                       -- 2902265 handle arrival set later
4199                       --(l_arrival_count > 1 AND k = l_arrival_count) OR
4200                       (l_ship_count > 1 AND i = l_ship_count) OR
4201                       (l_ship_count = 1 AND l_arrival_count = 1)) THEN
4202 
4203                     IF PG_DEBUG in ('Y', 'C') THEN
4204                        msc_sch_wb.atp_debug('Schedule: ' || 'bug 1225631');
4205                     END IF;
4206                      --3720018 , Global array MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC is used to append records
4207                      -- in case of ATP Inquiry. This global array will be used to call remove_invalid_sd_rec
4208                      -- From request level (Call_schedule)
4209                     IF MSC_ATP_PVT.G_PEGGING_FOR_SET IS NOT NULL and MSC_ATP_PVT.G_PEGGING_FOR_SET.COUNT > 0 THEN
4210                        l_count := MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_PEGGING_ID_REQUEST.count;
4211                        MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_PEGGING_ID_REQUEST.extend(MSC_ATP_PVT.G_PEGGING_FOR_SET.count);
4212                        MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_PLAN_ID_REQUEST.extend(MSC_ATP_PVT.G_PEGGING_FOR_SET.count);
4213                        MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_DC_ATP_FLAG_REQUEST.extend(MSC_ATP_PVT.G_PEGGING_FOR_SET.count);
4214                        FOR m in 1..MSC_ATP_PVT.G_PEGGING_FOR_SET.COUNT LOOP
4215                           IF PG_DEBUG in ('Y', 'C') THEN
4216                               msc_sch_wb.atp_debug('Schedule: ' || MSC_ATP_PVT.G_PEGGING_FOR_SET(m));
4217                           END IF;
4218                           MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_PEGGING_ID_REQUEST(l_count + m) := MSC_ATP_PVT.G_PEGGING_FOR_SET(m);
4219                           MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_PLAN_ID_REQUEST(l_count + m) := l_plan_id;
4220                           MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_DC_ATP_FLAG_REQUEST(l_count + m) := MSC_ATP_PVT.G_PEGGING_FOR_SET(m);
4221                        END LOOP;
4222                     END IF;
4223 
4224                     /* --3720018, donot call remove_invalid_sd_rec here
4225                      FOR i in 1..MSC_ATP_PVT.G_PEGGING_FOR_SET.COUNT LOOP
4226                       IF PG_DEBUG in ('Y', 'C') THEN
4227                          msc_sch_wb.atp_debug('Schedule: ' || 'in the loop');
4228                       END IF;
4229                       MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(
4230   			MSC_ATP_PVT.G_PEGGING_FOR_SET(i)  ,
4231                         p_instance_id,
4232                         l_plan_id,
4233                         UNDO,
4234                         MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG(i),
4235   			l_return_status);
4236 
4237                     END LOOP; */ --3720018
4238 
4239                   ELSE
4240                 -- 2266808: krajan : 04/16
4241 
4242                     IF l_atp_rec.error_code = ATP_REQ_DATE_FAIL THEN
4243                       l_sd_qty := l_atp_rec.quantity_ordered;
4244                       IF PG_DEBUG in ('Y', 'C') THEN
4245                          msc_sch_wb.atp_debug('Schedule: ' || 'i := ' || i);
4246                          msc_sch_wb.atp_debug('Schedule: ' || 'MSC_ATP_PVT.G_REQ_DATE_QTY(i) := ' || MSC_ATP_PVT.G_REQ_DATE_QTY(i));
4247                          msc_sch_wb.atp_debug('Schedule: ' || 'MSC_ATP_PVT.G_REQ_ATP_DATE(i) := ' || MSC_ATP_PVT.G_REQ_ATP_DATE(i));
4248                       END IF;
4249                       -- rajjain begin 01/30/2003 Bug 2777661
4250                       -- bug 2795053-reopen (ssurendr): Do date offset for override case as well
4251                       -- IF NVL(l_atp_rec.override_flag, 'N') = 'Y' THEN
4252                       --    l_sd_date := l_atp_rec.ship_date;
4253                       -- ELSE
4254                       l_sd_date := l_atp_rec.ship_date;
4255                       /* s_cto_rearch: do not honor atp lead time
4256                          l_sd_date := MSC_CALENDAR.DATE_OFFSET(
4257                                                    l_atp_rec.organization_id,
4258                                                    p_instance_id,
4259                                                    1,
4260                                                    l_atp_rec.ship_date,
4261                                                    -NVL(l_atp_rec.atp_lead_time, 0));
4262                       e_cto_rearch*/
4263 
4264                       /* ship_rec_cal Determine order date type*/
4265                       IF l_atp_table.requested_arrival_date(i) is not null THEN
4266                             l_order_date_type := 2;
4267                       ELSE
4268                             l_order_date_type := 1;
4269                       END IF;
4270 
4271 	              IF PG_DEBUG in ('Y', 'C') THEN
4272 	                   msc_sch_wb.atp_debug('Schedule: ' || 'l_order_date_type : ' || l_order_date_type);
4273 	              END IF;
4274 
4275                       --Bug 3226083
4276                       l_atp_insert_rec := l_atp_rec;
4277                       l_atp_insert_rec.inventory_item_id := l_pf_dest_id;
4278                       l_atp_insert_rec.request_item_id := l_mem_dest_id;
4279                       l_atp_insert_rec.origination_type := 30;
4280 
4281                       MSC_ATP_DB_UTILS.Update_SD_Date(l_atp_rec.demand_source_line ,
4282                              p_instance_id,
4283                              l_sd_date,
4284                              l_plan_id,
4285                              l_sd_qty,
4286                              MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG(i),
4287                              MSC_ATP_PVT.G_REQ_ATP_DATE(i),
4288                              MSC_ATP_PVT.G_REQ_DATE_QTY(i), -- Bug 1501787
4289                              l_dmd_satisfied_date, -- bug 2795053-reopen (ssurendr)
4290                              l_atp_rec.used_available_quantity,  -- For time_phased_atp bug3409973
4291                              l_atp_rec.atf_date,            -- For time_phased_atp
4292                              l_atp_rec.atf_date_quantity,   -- For time_phased_atp
4293 			     l_atp_rec.arrival_date,            -- For ship_rec_cal
4294                              l_order_date_type,                 -- For ship_rec_cal
4295                              l_atp_rec.latest_acceptable_date,  -- For ship_rec_cal
4296                              l_atp_rec.ship_set_name,           -- For Plan by Request Date
4297                              l_atp_rec.arrival_set_name,        -- For Plan by Request Date
4298                              l_atp_rec.override_flag,           -- For Plan by Request Date
4299                              l_original_req_arrival_date,       -- For Plan by Request Date
4300                              l_bkwd_pass_atf_date_qty,            -- For time_phased_atp bug3397904
4301                              l_atp_insert_rec                  -- For bug 3226083
4302                              );
4303 
4304                     END IF;
4305                   END IF;
4306                   l_stmt := 270;
4307 
4308                   -- 1873918: PDS-ODS fix
4309 
4310                   IF ((l_atp_table.Action(i) = ATPQUERY) AND
4311                      ((l_atp_table.Error_Code(i) = ATP_NOT_APPL) OR
4312                      (l_atp_table.Error_Code(i) = PDS_TO_ODS_SWITCH))) THEN
4313                     IF PG_DEBUG in ('Y', 'C') THEN
4314                        msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_table.Error_Code(i)='||l_atp_table.Error_Code(i));
4315                     END IF;
4316                   ELSE
4317                     l_atp_table.Error_Code(i) := ALLSUCCESS;
4318                   END IF;
4319 
4320                   l_stmt := 280;
4321 
4322                   -- since we are successful,increase l_set_element_success by 1
4323 
4324                   l_set_element_success := l_set_element_success + 1;
4325 
4326                   -- need to do something for the set if we are done
4327                   -- with this set
4328                   IF PG_DEBUG in ('Y', 'C') THEN
4329                      msc_sch_wb.atp_debug('Schedule: ' || 'l_ship_count := ' ||l_ship_count);
4330                      msc_sch_wb.atp_debug('Schedule: ' || 'l_stmt := ' || l_stmt);
4331                      msc_sch_wb.atp_debug('Schedule: ' || 'stmt - l_set_element_success := ' || l_set_element_success);
4332                   END IF;
4333                   IF (i = l_ship_count) AND (l_ship_count > 1 ) THEN
4334 
4335                       l_stmt := 290;
4336                       IF PG_DEBUG in ('Y', 'C') THEN
4337                          msc_sch_wb.atp_debug('Schedule: ' || 'l_stmt:= ' || l_stmt);
4338                       END IF;
4339 
4340                       -- this is a set, make sure this set is successful
4341 
4342                       -- dsting setproc
4343                       MSC_ATP_PROC.Process_Set_Dates_Errors(l_atp_table, 'D', l_set_status);
4344                       -- 2902265
4345                       l_overall_status := l_set_status;
4346                       l_group_date := l_atp_table.group_ship_date(1);
4347                       IF l_set_status = ALLSUCCESS THEN
4348 --                      IF l_set_element_success  = l_atp_table.Action.COUNT AND
4349 
4350                         l_stmt := 300;
4351 
4352 			IF PG_DEBUG in ('Y', 'C') THEN
4353 			   msc_sch_wb.atp_debug('Schedule: ' || 'stmt17');
4354                         END IF;
4355 
4356                         MSC_ATP_PROC.Update_Set_SD_Dates(l_atp_table,l_req_arr_date);
4357                       END IF;
4358                       l_stmt := 320;
4359                   END IF;
4360 
4361                   /* ship_rec_cal changes begin
4362                      flush sch arrival date, lat acceptable date, order date type in pds for single line */
4363                   IF (i = 1) AND (l_ship_count = 1 ) THEN
4364                         l_ship_arrival_date_rec.scheduled_arrival_date := l_atp_table.arrival_date(1);
4365                         l_ship_arrival_date_rec.latest_acceptable_date := l_atp_table.latest_acceptable_date(1);
4366                         l_ship_arrival_date_rec.arrival_set_name       := l_atp_table.arrival_set_name(1);
4367                         l_ship_arrival_date_rec.ship_set_name          := l_atp_table.ship_set_name(1);
4368                         l_ship_arrival_date_rec.atp_override_flag      := l_atp_table.override_flag(1);
4369                         l_ship_arrival_date_rec.request_arrival_date   := l_original_req_arrival_date;
4370                         l_ship_arrival_date_rec.instance_id := p_instance_id;
4371                         l_ship_arrival_date_rec.plan_id := l_plan_id;
4372 
4373                         /* Read demand_id from madt*/
4374                         BEGIN
4375                             SELECT identifier3
4376                             INTO   l_ship_arrival_date_rec.demand_id
4377                             FROM   mrp_atp_details_temp
4378                             WHERE  pegging_id = MSC_ATP_PVT.G_DEMAND_PEGGING_ID
4379                             AND    session_id = MSC_ATP_PVT.G_SESSION_ID
4380                             AND    record_type = 3;
4381                         EXCEPTION
4382                             WHEN OTHERS THEN
4383                                 l_ship_arrival_date_rec.demand_id := null;
4384                         END;
4385                         IF PG_DEBUG in ('Y', 'C') THEN
4386                             msc_sch_wb.atp_debug('Schedule: ' || 'l_ship_arrival_date_rec.demand_id = ' || l_ship_arrival_date_rec.demand_id);
4387                         END IF;
4388 
4389                         /* Determine order date type*/
4390                         IF l_atp_table.requested_arrival_date(i) is not null THEN
4391                             l_ship_arrival_date_rec.order_date_type := 2;
4392                         ELSE
4393                             l_ship_arrival_date_rec.order_date_type := 1;
4394                         END IF;
4395                         IF l_ship_arrival_date_rec.demand_id is not null THEN
4396 	                        IF (G_INV_CTP = 4)THEN
4397 	                        	MSC_ATP_DB_UTILS.Flush_Data_In_Pds(l_ship_arrival_date_rec, l_return_status);
4398 	                        	IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4399 	                        		IF PG_DEBUG in ('Y', 'C') THEN
4400 	                                        msc_sch_wb.atp_debug('Schedule: ' || 'Error in call to Flush_Data_In_Pds procedure ');
4401 	                        		END IF;
4402 	                        	END IF;
4403 	                        ELSE
4404 	                        	MSC_ATP_DB_UTILS.Flush_Data_In_Ods(l_ship_arrival_date_rec, l_return_status);
4405 	                        	IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4406 	                        		IF PG_DEBUG in ('Y', 'C') THEN
4407 	                                        msc_sch_wb.atp_debug('Schedule: ' || 'Error in call to Flush_Data_In_Ods procedure ');
4408 	                        		END IF;
4409 	                        	END IF;
4410 	                        END IF;
4411                         END IF;
4412 
4413                        IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4414                                 IF PG_DEBUG in ('Y', 'C') THEN
4415                                         msc_sch_wb.atp_debug('Schedule: ' || 'Error in call to Flush_Data_In_Pds procedure ');
4416                                 END IF;
4417                                 RAISE FND_API.G_EXC_ERROR;
4418                         END IF;
4419                   END IF;
4420                   /* ship_rec_cal changes end */
4421 
4422                   l_stmt := 330;
4423                   -- dsting setproc
4424                   IF i = l_atp_table.action.count AND l_set_status = MSC_ATP_PVT.ALLSUCCESS THEN
4425 --                  IF l_set_element_success  = l_atp_table.Action.COUNT THEN
4426                      l_overall_status := ALLSUCCESS ;
4427                   END IF;
4428 
4429                   l_stmt := 360;
4430                   IF PG_DEBUG in ('Y', 'C') THEN
4431                      msc_sch_wb.atp_debug('Schedule: ' || 'l_stmt := ' || l_stmt);
4432                   END IF;
4433 
4434                   -- Added by NGOEL for BUG 1533251, in case ship_date is <= requested_date
4435                   -- and G_FIND_FUTURE = Y, copy available quantity into request_date quantity.
4436                   -- This will take care of the situation in case we were able to find some
4437                   -- additional quantity in forward case but still meet the request date.
4438 
4439                   IF (((l_atp_rec.error_code = ATP_REQ_DATE_FAIL) AND
4440                      (l_atp_rec.requested_ship_date IS NOT NULL) AND
4441                      (l_atp_rec.ship_date <= l_atp_rec.requested_ship_date))
4442                     OR
4443                      ((l_atp_rec.error_code = ATP_REQ_DATE_FAIL) AND
4444                      (l_atp_rec.requested_arrival_date IS NOT NULL) AND
4445                      (l_atp_rec.ship_date+NVL(l_atp_rec.delivery_lead_time,0)
4446                         <= l_atp_rec.requested_arrival_date))) THEN
4447                         IF PG_DEBUG in ('Y', 'C') THEN
4448                            msc_sch_wb.atp_debug ('Schedule: ' || 'Bug 1533251');
4449                            msc_sch_wb.atp_debug ('Schedule: ' || 'l_atp_rec.requested_date_quantity : '||
4450                                 l_atp_rec.requested_date_quantity);
4451                            msc_sch_wb.atp_debug ('Schedule: ' || 'l_atp_rec.combined_requested_date_qty : '||
4452                                 l_atp_rec.combined_requested_date_qty);
4453                            msc_sch_wb.atp_debug ('Schedule: ' || 'l_atp_rec.available_quantity : '||
4454                                 l_atp_rec.available_quantity);
4455                         END IF;
4456 
4457 			l_atp_rec.requested_date_quantity := l_atp_rec.available_quantity;
4458                   END IF;
4459                   -- End of New Code
4460 
4461                   --diag_atp
4462                   IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 THEN
4463                     IF PG_DEBUG in ('Y', 'C') THEN
4464                        msc_sch_wb.atp_debug('Schedule: ' || 'Reset The error code for diag ATP mode 2');
4465                     END IF;
4466                     l_atp_table.Error_Code(i) := MSC_ATP_PVT.DIAGNOSTIC_ATP_ENABLED;
4467                   END IF;
4468 		ELSE --- bug 1104565
4469 
4470 		  -- The request_date fails
4471 		  IF PG_DEBUG in ('Y', 'C') THEN
4472 		     msc_sch_wb.atp_debug('Schedule: ' || 'Date requirements fail vivek');
4473 		  END IF;
4474                   -- Bug 2266808 : krajan
4475                   -- dsting setproc
4476 	          IF l_ship_count > 1 AND i = l_ship_count THEN
4477                      MSC_ATP_PROC.Process_Set_Dates_Errors(l_atp_table, 'D', l_set_status);
4478                      l_group_date := l_atp_table.group_ship_date(1);
4479                   END IF;
4480 
4481 	          --IF i <> l_ship_count OR (l_atp_table.Action(i) = ATPQUERY AND l_ship_count =1)
4482 	          --OR i = l_ship_count THEN --3720018 (removed the IF condition, needto be executed always)
4483                      ---- this part will be executed for ship set only
4484                      ---- this part will be executed for ship set only
4485 	             ---- the ship set is not complete as yet
4486                      ----- we update the S/D date and quantity
4487                      -- 1225631
4488                      -- in order to keep the demand picture correctly between
4489                      -- the items inside a ship/arrival set,
4490                      -- we need to move the demand date for this set item
4491                      -- to the ship date
4492 
4493                      -- 1580127
4494 		     IF (l_atp_rec.error_code = ATP_REQ_DATE_FAIL) THEN
4495 
4496 	                IF PG_DEBUG in ('Y', 'C') THEN
4497 	                   msc_sch_wb.atp_debug('Schedule: ' || 'Inside ATP_REQ_DATE_FAIL, i : ' || i);
4498 	                   msc_sch_wb.atp_debug('Schedule: ' || 'quantity_ordered : ' || l_atp_rec.quantity_ordered);
4499 	                   msc_sch_wb.atp_debug('Schedule: ' || 'combined qty : ' || l_atp_rec.combined_requested_date_qty);
4500 	                   msc_sch_wb.atp_debug('Schedule: ' || 'order line : ' || l_atp_rec.demand_source_line);
4501 	                END IF;
4502 
4503                         l_sd_qty := l_atp_rec.quantity_ordered;
4504 	                IF PG_DEBUG in ('Y', 'C') THEN
4505 	                   msc_sch_wb.atp_debug('Schedule: ' || 'quantity : ' || l_sd_qty);
4506 	                END IF;
4507 
4508 			/* ship_rec_cal Determine order date type*/
4509                         IF l_atp_table.requested_arrival_date(i) is not null THEN
4510                              l_order_date_type := 2;
4511                         ELSE
4512                             l_order_date_type := 1;
4513                         END IF;
4514 	                IF PG_DEBUG in ('Y', 'C') THEN
4515 	                   msc_sch_wb.atp_debug('Schedule: ' || 'l_order_date_type : ' || l_order_date_type);
4516 	                END IF;
4517 
4518                         --Bug 3226083
4519                         l_atp_insert_rec := l_atp_rec;
4520                         l_atp_insert_rec.inventory_item_id := l_pf_dest_id;
4521                         l_atp_insert_rec.request_item_id := l_mem_dest_id;
4522                         l_atp_insert_rec.origination_type := 30;
4523 
4524                         MSC_ATP_DB_UTILS.Update_SD_Date(l_atp_rec.demand_source_line ,
4525                              p_instance_id,
4526                              l_atp_rec.ship_date,
4527                              --s_cto_rearch do not honor atp_lead time
4528                              /*
4529                              MSC_CALENDAR.DATE_OFFSET(
4530                              l_atp_rec.organization_id,
4531                              p_instance_id,
4532                              1,
4533                              l_atp_rec.ship_date,
4534                              -NVL(l_atp_rec.atp_lead_time, 0)),
4535                              */
4536                              l_plan_id,
4537                              l_sd_qty,
4538                               MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG(i),
4539                               MSC_ATP_PVT.G_REQ_ATP_DATE(i),
4540                               MSC_ATP_PVT.G_REQ_DATE_QTY(i), -- Bug 1501787
4541                               l_dmd_satisfied_date, -- bug 2795053-reopen (ssurendr)
4542                               l_atp_rec.used_available_quantity,  -- For time_phased_atp bug3409973
4543                               l_atp_rec.atf_date,            -- For time_phased_atp
4544                               l_atp_rec.atf_date_quantity, -- For time_phased_atp
4545                               l_atp_rec.arrival_date,            -- For ship_rec_cal
4546                               l_order_date_type,                 -- For ship_rec_cal
4547                               l_atp_rec.latest_acceptable_date,
4548                               l_atp_rec.ship_set_name,
4549                               l_atp_rec.arrival_set_name,
4550                               l_atp_rec.override_flag,
4551                               l_original_req_arrival_date,
4552                               l_bkwd_pass_atf_date_qty,             -- For time_phased_atp bug3397904
4553                               l_atp_insert_rec                   -- For bug 3226083
4554                               );  -- For ship_rec_cal
4555 	             END IF;
4556 		  --END IF; ---if i = l_ship_count
4557                    --- Bug 1104565
4558                   --IF j = l_sources.source_type.count THEN
4559                      --- ADD the pegging id for the best source
4560                   --   FOR l_peg_count in 1..l_atp_table.ship_date.count LOOP
4561                   --      l_atp_table.end_pegging_id(l_peg_count) := l_fst_src_pegging_ids(l_peg_count);
4562                   --   END LOOP;
4563                   --END IF;
4564 
4565 --		  IF (l_atp_rec.error_code = ATP_REQ_DATE_FAIL) THEN
4566 		  IF (l_atp_table.error_code(i) = ATP_REQ_DATE_FAIL) THEN
4567 		     IF PG_DEBUG in ('Y', 'C') THEN
4568 		        msc_sch_wb.atp_debug('Schedule: ' || 'in case 2, not in acceptable range');
4569 		     END IF;
4570                      l_atp_table.Error_Code(i) := ATP_ACCEPT_FAIL;
4571 
4572                   END IF;
4573 	        END IF; --- IF l_atp_rec.error_code = "ALLSUCCESS"
4574 
4575                      IF PG_DEBUG in ('Y', 'C') THEN
4576                         msc_sch_wb.atp_debug('arrival cntr k: ' || k);
4577                         msc_sch_wb.atp_debug('fst src peg count: ' || l_fst_src_pegging_ids.count);
4578                         msc_sch_wb.atp_debug('i: ' || i);
4579                         msc_sch_wb.atp_debug('p cnt: ' ||MSC_ATP_PVT.G_pegging_for_set.count);
4580                         msc_sch_wb.atp_debug('f cnt: ' ||MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG.count);
4581                         msc_sch_wb.atp_debug('d cnt: ' ||MSC_ATP_PVT.G_REQ_ATP_DATE.count);
4582                         msc_sch_wb.atp_debug('q cnt: ' ||MSC_ATP_PVT.G_REQ_DATE_QTY.count);
4583                         msc_sch_wb.atp_debug('l_date_higher_src: ' || l_date_higher_src);
4584                         msc_sch_wb.atp_debug('l_overall_status: ' || l_overall_status);
4585                      END IF;
4586 
4587                      IF i = l_ship_count THEN
4588 
4589                         IF j=1 or
4590                            l_overall_status = ALLSUCCESS or
4591                            (l_date_higher_src = 'N' and l_overall_status
4592                             in (ATP_REQ_DATE_FAIL, ATP_ACCEPT_FAIL, GROUPEL_ERROR))
4593                         THEN
4594                            IF PG_DEBUG in ('Y', 'C') THEN
4595                               msc_sch_wb.atp_debug('used src for fst_src_peg');
4596                            END IF;
4597 
4598                            IF j > 1 THEN
4599                               FOR l_peg_count in k..l_fst_src_pegging_ids.count loop
4600                                  MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(
4601                                                 l_fst_src_pegging_ids(l_peg_count),
4602                                                 null,
4603                                                 l_plan_id,
4604                                                 UNDO,
4605                                                 l_DEMAND_CLASS_ATP_FLAG(l_peg_count),
4606                                                 l_return_status);
4607                               END LOOP;
4608 
4609                               l_fst_src_pegging_ids.trim(l_fst_src_pegging_ids.count - k + 1);
4610                               l_req_atp_date.trim(l_fst_src_pegging_ids.count - k + 1);
4611                               l_req_date_qty.trim(l_fst_src_pegging_ids.count - k + 1);
4612                               l_demand_class_atp_flag.trim(l_fst_src_pegging_ids.count - k + 1);
4613                            END IF;
4614 
4615                            msc_sch_wb.atp_debug('adding to fst_src_peg p');
4616                            MSC_ATP_PROC.number_arr_cat(l_fst_src_pegging_ids, MSC_ATP_PVT.G_PEGGING_FOR_SET);
4617                            msc_sch_wb.atp_debug('adding to fst_src_peg d');
4618                            MSC_ATP_PROC.date_arr_cat(l_req_atp_date, MSC_ATP_PVT.G_REQ_ATP_DATE);
4619                            msc_sch_wb.atp_debug('adding to fst_src_peg q');
4620                            MSC_ATP_PROC.number_arr_cat(l_req_date_qty, MSC_ATP_PVT.G_REQ_DATE_QTY);
4621                            msc_sch_wb.atp_debug('adding to fst_src_peg f');
4622                            MSC_ATP_PROC.number_arr_cat(l_demand_class_atp_flag, MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG);
4623 
4624                            IF l_overall_status in (MSC_ATP_PVT.ALLSUCCESS,
4625                                                    MSC_ATP_PVT.ATP_REQ_DATE_FAIL,
4626                                                    MSC_ATP_PVT.GROUPEL_ERROR,
4627                                                    MSC_ATP_PVT.ATP_ACCEPT_FAIL)
4628                            THEN
4629                               l_date_higher_src := 'Y';
4630                               msc_sch_wb.atp_debug('have date. l_date_higher_src: ' || l_date_higher_src);
4631                            END IF;
4632                            -- cleanup only if not arrival set
4633                            --3720018 , Global array MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC is used to append records
4634                            -- in case of ATP Inquiry. This global array will be used to call remove_invalid_sd_rec
4635                            -- From request level (Call_schedule)
4636                            IF l_set_status <> ALLSUCCESS and l_arrival_count = 1 THEN
4637                               IF (l_atp_rec.Action = ATPQUERY AND l_ship_count =1) THEN
4638                                  IF MSC_ATP_PVT.G_PEGGING_FOR_SET IS NOT NULL and MSC_ATP_PVT.G_PEGGING_FOR_SET.COUNT > 0 THEN
4639                                     l_count := MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_PEGGING_ID_REQUEST.count;
4640                                     MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_PEGGING_ID_REQUEST.extend(MSC_ATP_PVT.G_PEGGING_FOR_SET.count);
4641                                     MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_PLAN_ID_REQUEST.extend(MSC_ATP_PVT.G_PEGGING_FOR_SET.count);
4642                                     MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_DC_ATP_FLAG_REQUEST.extend(MSC_ATP_PVT.G_PEGGING_FOR_SET.count);
4643                                     FOR m in 1..MSC_ATP_PVT.G_PEGGING_FOR_SET.COUNT LOOP
4644                                        MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_PEGGING_ID_REQUEST(l_count + m) := MSC_ATP_PVT.G_PEGGING_FOR_SET(m);
4645                                        MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_PLAN_ID_REQUEST(l_count + m) := l_plan_id;
4646                                        MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_DC_ATP_FLAG_REQUEST(l_count + m) := MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG(m);
4647                                     END LOOP;
4648                                  END IF;
4649                               ELSE  --3720018
4650                                 MSC_ATP_PROC.cleanup_set(p_instance_id,
4651                                                        l_plan_id,
4652                                                        MSC_ATP_PVT.G_PEGGING_FOR_SET,
4653                                                        MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG);
4654                               END IF; --3720018
4655                            END IF; --3720018
4656                         ELSE
4657                            -- not using these in fst src peg, so always clean up
4658                            MSC_ATP_PROC.cleanup_set(p_instance_id,
4659                                                     l_plan_id,
4660                                                     MSC_ATP_PVT.G_PEGGING_FOR_SET,
4661                                                     MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG);
4662                         END IF;
4663 
4664                         MSC_ATP_PVT.G_PEGGING_FOR_SET.DELETE;
4665                         MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG.DELETE;
4666                         MSC_ATP_PVT.G_REQ_ATP_DATE.DELETE;
4667                         MSC_ATP_PVT.G_REQ_DATE_QTY.DELETE;
4668 
4669                         IF j =  l_sources.source_type.count THEN
4670                           IF PG_DEBUG in ('Y', 'C') THEN
4671                              msc_sch_wb.atp_debug('Schedule: ' || 'l_fst_src_pegging_ids.count'|| l_fst_src_pegging_ids.count);
4672                           END IF;
4673 
4674                            -- dsting 2741997
4675                           IF l_ship_count > 1 THEN
4676                              FOR l_peg_count in 1..l_atp_table.ship_date.count LOOP
4677                                 l_atp_table.end_pegging_id(l_peg_count) := l_fst_src_pegging_ids(l_peg_count);
4678 
4679                              END LOOP;
4680                           ELSE
4681                                 l_atp_table.end_pegging_id(1) := l_fst_src_pegging_ids(l_fst_src_pegging_ids.count);
4682                           END IF;
4683                         END IF;
4684                      END IF;
4685 --                END IF; -- 2902265 set <> success thing
4686 
4687                 -- bug 2266808 code changes end
4688             ELSE
4689                 -- dsting case 1 => multi_scenario = 1.
4690                 -- multi_scenario is always 0 since complete_scatp is disabled
4691                 -- case 1
4692                 IF PG_DEBUG in ('Y', 'C') THEN
4693                    msc_sch_wb.atp_debug('Schedule: ' || 'In case 1');
4694                 END IF;
4695                 IF ((l_atp_rec.error_code = ALLSUCCESS) AND
4696                     (l_sources.Preferred(j) = 1))
4697                   OR (l_atp_rec.error_code = ATP_NOT_APPL) THEN
4698 
4699                   IF (l_atp_rec.Action = ATPQUERY) THEN
4700                     -- 1225631: we only undo when
4701                     -- 1: the ship set is done (ship set exists) or
4702                     -- 2: the arrival set is done (arrival set exists) or
4703                     -- 3: the single line is done (no set exists)
4704                    IF (l_arrival_count > 1 AND k = l_arrival_count) OR
4705                       (l_ship_count > 1 AND i = l_ship_count) OR
4706                       (l_ship_count = 1 AND l_arrival_count = 1) THEN
4707                       IF PG_DEBUG in ('Y', 'C') THEN
4708                          msc_sch_wb.atp_debug('Schedule: ' || 'bug 1225631');
4709                       END IF;
4710 
4711 --                      ROLLBACK to SAVEPOINT start_atp;
4712                       FOR i in 1..MSC_ATP_PVT.G_PEGGING_FOR_SET.COUNT LOOP
4713                         IF PG_DEBUG in ('Y', 'C') THEN
4714                            msc_sch_wb.atp_debug('Schedule: ' || 'in the loop');
4715                         END IF;
4716                         MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(
4717                           MSC_ATP_PVT.G_PEGGING_FOR_SET(i)  ,
4718                           p_instance_id,
4719                           l_plan_id,
4720                           UNDO,
4721                           MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG(i),
4722                           l_return_status);
4723 
4724                       END LOOP;
4725 
4726                       MSC_ATP_PVT.G_PEGGING_FOR_SET.DELETE;
4727                       MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG.DELETE;
4728                       MSC_ATP_PVT.G_REQ_ATP_DATE.DELETE;
4729                       MSC_ATP_PVT.G_REQ_DATE_QTY.DELETE;
4730                        IF PG_DEBUG in ('Y', 'C') THEN
4731                           msc_sch_wb.atp_debug('Schedule: ' || 'Do I come here 7');
4732                        END IF;
4733 
4734                    END IF;
4735                   END IF;
4736 
4737                   -- since we are successful,increase l_set_element_success by 1
4738 
4739                   l_set_element_success := l_set_element_success + 1;
4740 
4741                   IF (i = l_ship_count) AND (l_ship_count > 1 ) THEN
4742                       -- this is a set, make sure this set is successful
4743                         -- dsting setproc
4744 --                      IF l_set_element_success  = l_atp_table.Action.COUNT THEN
4745                         IF l_set_status = MSC_ATP_PVT.ALLSUCCESS THEN
4746                         FOR m in 1.. l_ship_count LOOP
4747                           l_atp_table.Group_Ship_Date(m):= l_group_date;
4748 
4749                           IF (l_atp_table.Action(m)<> ATPQUERY) THEN
4750 
4751                             IF MSC_ATP_PVT.G_INV_CTP = 4 THEN
4752                                 /* time_phased_atp use plan_id populated in l_atp_table.plan_id
4753                                 -- New procedure for obtaining plan data : Supplier Capacity Lead Time proj.
4754                                 -- (SCLT)
4755                                 MSC_ATP_PROC.get_global_plan_info(p_instance_id,
4756                                             l_atp_table.inventory_item_id(m),
4757                                             l_atp_table.organization_id(m),
4758                                             l_atp_table.demand_class(m));
4759 
4760                                 l_plan_info_rec := MSC_ATP_PVT.G_PLAN_INFO_REC;
4761                                 -- End New procedure for obtaining plan data : Supplier Capacity Lead Time proj.
4762 
4763                                 l_temp_plan_id          := l_plan_info_rec.plan_id;
4764                                 l_temp_assign_set_id    := l_plan_info_rec.assignment_set_id;
4765                                 IF l_atp_table.attribute_07.Exists(m) THEN
4766                                   l_atp_table.attribute_07(m) := l_plan_info_rec.plan_name;
4767                                 END IF;
4768                                 -- changes for bug 2392456 ends*/
4769                                 l_temp_plan_id := l_atp_table.plan_id(i);
4770 
4771                             ELSE
4772                                 l_temp_plan_id := l_plan_id;
4773                             END IF;
4774 
4775                             -- ship_rec_cal
4776                             IF l_atp_table.requested_arrival_date(i) is not null THEN
4777                                 l_order_date_type := 2;
4778                             ELSE
4779                                 l_order_date_type := 1;
4780                             END IF;
4781 
4782                             MSC_ATP_DB_UTILS.Update_SD_Date(p_atp_table.Identifier(m),
4783                              p_instance_id, l_group_date, l_temp_plan_id,null,
4784                                MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG(i),
4785                                MSC_ATP_PVT.G_REQ_ATP_DATE(i),
4786                                MSC_ATP_PVT.G_REQ_DATE_QTY(i), -- Bug 1501787
4787                                l_group_date, -- bug 2795053-reopen (ssurendr)
4788                                null,                    -- For time_phased_atp
4789                                l_atp_table.atf_date(m), -- For time_phased_atp
4790                                null,                    -- For time_phased_atp
4791                                l_group_arrival_date,                    -- For ship_rec_cal
4792                                l_order_date_type,                       -- For ship_rec_cal
4793                                l_atp_table.latest_acceptable_date(m),   -- For ship_rec_cal
4794                                l_atp_table.ship_set_name(m),		--Plan by request date
4795                                l_atp_table.arrival_set_name(m),		--Plan by request date
4796                                l_atp_table.override_flag(m),		--Plan by request date
4797 			       l_original_req_arrival_date,     	--Plan by request date
4798 			       null                             --l_bkwd_pass_atf_date_qty bug3397904
4799                                );
4800                               END IF;
4801 	                 END LOOP;
4802 
4803                       ELSE
4804 
4805                         -- this set is not successful
4806                         FOR m in 1.. l_ship_count LOOP
4807                           l_atp_table.Group_Ship_Date(m):= l_group_date;
4808                           IF (l_atp_table.Error_Code(m)= ALLSUCCESS) THEN
4809                             l_atp_table.Error_Code(m) := GROUPEL_ERROR;
4810                           END IF;
4811 
4812                         END LOOP;
4813 
4814                         -- 1072816  need to do something to undo
4815 
4816                         FOR i in 1..MSC_ATP_PVT.G_PEGGING_FOR_SET.COUNT LOOP
4817                           IF PG_DEBUG in ('Y', 'C') THEN
4818                              msc_sch_wb.atp_debug('Schedule: ' || 'in the loop');
4819                           END IF;
4820                           MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(
4821                             MSC_ATP_PVT.G_PEGGING_FOR_SET(i)  ,
4822                             p_instance_id,
4823                             l_plan_id,
4824                             UNDO,
4825                             MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG(i),
4826                             l_return_status);
4827 
4828                         END LOOP;
4829 
4830                         MSC_ATP_PVT.G_PEGGING_FOR_SET.DELETE;
4831                         MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG.DELETE;
4832 		        MSC_ATP_PVT.G_REQ_ATP_DATE.DELETE;
4833                         MSC_ATP_PVT.G_REQ_DATE_QTY.DELETE;
4834                          IF PG_DEBUG in ('Y', 'C') THEN
4835                             msc_sch_wb.atp_debug('Schedule: ' || 'Do I come here 8');
4836                          END IF;
4837 
4838                       END IF;
4839                   END IF;
4840 
4841                   IF i = l_ship_count THEN
4842                     l_temp_atp_table := l_atp_table;
4843                   END IF;
4844 
4845                   MSC_ATP_PROC.Details_Output(l_atp_period,
4846                                  l_atp_supply_demand,
4847                                  l_temp_atp_period,
4848                                  l_temp_atp_supply_demand,
4849                                  l_return_status);
4850 
4851                   -- now exit the loop since we are successful
4852 --                  IF l_set_element_success  = l_atp_table.Action.COUNT THEN
4853                   IF l_set_status = MSC_ATP_PVT.ALLSUCCESS THEN
4854                      l_overall_status := ALLSUCCESS ;
4855                   END IF;
4856 
4857 
4858                 ELSE
4859                   -- here we want the the info from ALL warehouses, so
4860                   -- we cannot just copy the records of table over,
4861                   -- we need to append to the temp.
4862 
4863 --                  ROLLBACK to SAVEPOINT start_atp;
4864 
4865                   -- 1225631
4866                   IF ((l_ship_count = 1 AND l_arrival_count = 1) OR
4867                      (l_arrival_count > 1 AND k = l_arrival_count) OR
4868                      (l_ship_count > 1 AND i = l_ship_count) ) THEN
4869 
4870                     FOR i in 1..MSC_ATP_PVT.G_PEGGING_FOR_SET.COUNT LOOP
4871                       MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(
4872                           MSC_ATP_PVT.G_PEGGING_FOR_SET(i)  ,
4873                           p_instance_id,
4874                           l_plan_id,
4875                           UNDO,
4876                           MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG(i),
4877                           l_return_status);
4878 
4879                     END LOOP;
4880 
4881                     MSC_ATP_PVT.G_PEGGING_FOR_SET.DELETE;
4882                     MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG.DELETE;
4883                     MSC_ATP_PVT.G_REQ_ATP_DATE.DELETE;
4884                     MSC_ATP_PVT.G_REQ_DATE_QTY.DELETE;
4885                      IF PG_DEBUG in ('Y', 'C') THEN
4886                         msc_sch_wb.atp_debug('Schedule: ' || 'Do I come here 9');
4887                      END IF;
4888                   END IF;
4889 
4890                   IF (i = l_ship_count) AND (l_ship_count > 1 ) THEN
4891                     -- this set is not successful
4892                     FOR m in 1.. l_ship_count LOOP
4893                       l_atp_table.Group_Ship_Date(m):= l_group_date;
4894                       IF (l_atp_table.Error_Code(m)= ALLSUCCESS) THEN
4895                         l_atp_table.Error_Code(m) := GROUPEL_ERROR;
4896                       END IF;
4897                     END LOOP;
4898 
4899                     -- 1072816  need to do something to undo
4900 
4901                     FOR i in 1..MSC_ATP_PVT.G_PEGGING_FOR_SET.COUNT LOOP
4902                       IF PG_DEBUG in ('Y', 'C') THEN
4903                          msc_sch_wb.atp_debug('Schedule: ' || 'in the loop');
4904                       END IF;
4905                       MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(
4906                         MSC_ATP_PVT.G_PEGGING_FOR_SET(i)  ,
4907                         p_instance_id,
4908                         l_plan_id,
4909                         UNDO,
4910                         MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG(i),
4911                         l_return_status);
4912 
4913                     END LOOP;
4914 
4915                     MSC_ATP_PVT.G_PEGGING_FOR_SET.DELETE;
4916                     MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG.DELETE;
4917 		    MSC_ATP_PVT.G_REQ_ATP_DATE.DELETE;
4918                     MSC_ATP_PVT.G_REQ_DATE_QTY.DELETE;
4919                      IF PG_DEBUG in ('Y', 'C') THEN
4920                         msc_sch_wb.atp_debug('Schedule: ' || 'Do I come here 10');
4921                      END IF;
4922 
4923                   END IF;
4924 
4925                   IF i = l_ship_count THEN
4926 
4927                     MSC_SATP_FUNC.Assign_Atp_Output_Rec(l_atp_table,
4928                                           l_temp_atp_table,
4929                                           l_return_status );
4930 
4931                   END IF;
4932 
4933                   MSC_ATP_PROC.Details_Output(l_atp_period,
4934                                  l_atp_supply_demand,
4935                                  l_temp_atp_period,
4936                                  l_temp_atp_supply_demand,
4937                                  l_return_status);
4938 
4939                 END IF;
4940 
4941             END IF; -- end if l_multi_scenario = 0
4942 
4943             -- 1497368: now work on the output record of tables.
4944             -- do this only if this is the end of the set or line
4945 
4946             IF i = l_ship_count THEN
4947                IF l_overall_status = ALLSUCCESS THEN
4948 
4949                   IF PG_DEBUG in ('Y', 'C') THEN
4950                      msc_sch_wb.atp_debug('Schedule: ' || 'l_overall_status = ALLSUCCESS');
4951                   END IF;
4952 
4953                   l_temp_atp_table := l_atp_table;
4954 
4955                   IF l_ship_count > 1 THEN
4956                     -- ship set case
4957                     -- the curr one is sucessful
4958                     IF PG_DEBUG in ('Y', 'C') THEN
4959                        msc_sch_wb.atp_debug('Schedule: ' || 'l_ship_count > 1');
4960                     END IF;
4961 
4962                     l_temp_atp_period := l_curr_atp_period;
4963                     l_temp_atp_supply_demand := l_curr_atp_supply_demand;
4964                   ELSE
4965                     IF PG_DEBUG in ('Y', 'C') THEN
4966                        msc_sch_wb.atp_debug('Schedule: ' || 'in the ELSE of (l_ship_count > 1)');
4967                     END IF;
4968 
4969                     -- single line (do not need to consider  arrival set here)
4970                     l_temp_atp_period := l_atp_period;
4971                     l_temp_atp_supply_demand := l_atp_supply_demand;
4972 
4973                   END IF;
4974 
4975                ELSE
4976                   -- we fails
4977                   -- we need to know if we got a date or not, and we need to know
4978                   -- if l_temp_atp_table is empty or not
4979 
4980                   IF PG_DEBUG in ('Y', 'C') THEN
4981                      msc_sch_wb.atp_debug('Schedule: ' || 'in the ELSE of l_overall_status = ALLSUCCESS');
4982                   END IF;
4983 
4984                   IF l_ship_count > 1 THEN
4985 
4986                     IF PG_DEBUG in ('Y', 'C') THEN
4987                        msc_sch_wb.atp_debug('Schedule: ' || 'l_ship_count > 1');
4988                     END IF;
4989 
4990                     -- this is a set
4991                     IF (l_group_date IS NOT NULL) AND
4992                         (l_arr_no_date = 'Y') THEN
4993 
4994                       IF PG_DEBUG in ('Y', 'C') THEN
4995                          msc_sch_wb.atp_debug('Schedule: ' || 'in the l_group_date is not null and l_arr_no_date = Y');
4996                       END IF;
4997                       l_temp_atp_table := l_atp_table;
4998                       l_temp_atp_period := l_curr_atp_period;
4999                       l_temp_atp_supply_demand := l_curr_atp_supply_demand;
5000                       l_arr_no_date := 'N';
5001                     ELSIF j = 1 THEN -- this is the first warehouse
5002                       IF PG_DEBUG in ('Y', 'C') THEN
5003                          msc_sch_wb.atp_debug('Schedule: ' || 'in the ELSIF j = 1 of (l_group_date is not null and l_arr_no_date = Y)');
5004                       END IF;
5005                       l_temp_atp_table := l_atp_table;
5006                       l_temp_atp_period := l_curr_atp_period;
5007                       l_temp_atp_supply_demand := l_curr_atp_supply_demand;
5008                       l_arr_no_date := 'Y';
5009                     END IF;
5010 
5011                   ELSE
5012 
5013                     IF PG_DEBUG in ('Y', 'C') THEN
5014                        msc_sch_wb.atp_debug('Schedule: ' || 'in the ELSE of (l_ship_count > 1)');
5015                     END IF;
5016 
5017                     -- this is a line
5018                     IF (l_atp_rec.ship_date IS NOT NULL) AND
5019                         (l_arr_no_date = 'Y') THEN
5020                       IF PG_DEBUG in ('Y', 'C') THEN
5021                          msc_sch_wb.atp_debug('Schedule: ' || 'in the l_atp_rec.ship_date is not null and l_arr_no_date = Y');
5022                       END IF;
5023                       l_temp_atp_table := l_atp_table;
5024                       l_temp_atp_period := l_atp_period;
5025                       l_temp_atp_supply_demand := l_atp_supply_demand;
5026                       l_arr_no_date := 'N';
5027                     ELSIF j = 1 THEN -- this is the first warehouse
5028                       IF PG_DEBUG in ('Y', 'C') THEN
5029                          msc_sch_wb.atp_debug('Schedule: ' || 'in the ELSIF j = 1 of (l_group_date is not null and l_arr_no_date = Y)');
5030                       END IF;
5031                       l_temp_atp_table := l_atp_table;
5032                       l_temp_atp_period := l_atp_period;
5033                       l_temp_atp_supply_demand := l_atp_supply_demand;
5034                       l_arr_no_date := 'Y';
5035                     END IF;
5036                   END IF; -- l_ship_count > 1
5037                END IF; -- IF l_overall_status = ALLSUCCESS THEN
5038             END IF;-- i = l_ship_count
5039             -- reinitialize record of tables
5040             l_atp_supply_demand := l_null_atp_supply_demand ;
5041             l_atp_period := l_null_atp_period;
5042             l_atp_details := l_null_atp_details ;
5043 
5044             /* time_phased_atp
5045                set l_time_phased_set to Y if it has already not been set*/
5046             IF l_time_phased_set <> 'Y' and l_time_phased_atp = 'Y' THEN
5047                 l_time_phased_set := 'Y';
5048             END IF;
5049 
5050             /* time_phased_atp
5051                set G_TIME_PHASED_SET to Y if it has already not been set*/
5052             IF G_TIME_PHASED_SET <> 'Y' and l_time_phased_atp = 'Y' THEN
5053                 G_TIME_PHASED_SET := 'Y';
5054             END IF;
5055 
5056             -- if ship set exists, we need to loop through the set
5057             IF l_ship_count > 1 THEN
5058               i := l_atp_table.Action.NEXT(i);
5059             ELSE
5060               i := NULL;
5061             END IF;
5062 
5063           -- BEGIN Bug 2400781
5064           -- Re-assign the global variables
5065           MSC_ATP_PVT.G_INV_CTP := l_inv_ctp;
5066           -- ATP4drp re-set using original profile value
5067           MSC_ATP_PVT.G_ALLOCATED_ATP := MSC_ATP_PVT.G_ORIG_ALLOC_ATP;
5068           -- End ATP4drp
5069           MSC_ATP_PVT.G_ALLOCATION_METHOD := l_allocation_method;
5070           --5221865 in the end of i loop we should re-set by original value.
5071           MSC_ATP_PVT.G_FORWARD_ATP := l_forward_atp;
5072           IF PG_DEBUG in ('Y', 'C') THEN
5073              msc_sch_wb.atp_debug('Schedule: ' || 'Reset Destination INV_CTP '
5074                                    || MSC_ATP_PVT.G_INV_CTP);
5075              --5221865
5076              msc_sch_wb.atp_debug('Schedule: ' || 'Reset Destination G_FORWARD_ATP '
5077                                    || MSC_ATP_PVT.G_FORWARD_ATP);
5078           END IF;
5079           -- The local variables were assigned at the beginning of the Loop
5080           -- Values should be reset after every ship-set row.
5081           -- END Bug 2400781
5082 
5083           END LOOP; -- end of i loop for ship set
5084 
5085           -- reinitialize record of tables
5086           l_atp_supply_demand := l_null_atp_supply_demand ;
5087           l_atp_period := l_null_atp_period;
5088           l_atp_details := l_null_atp_details ;
5089           l_set_element_success := 0;
5090 
5091           l_curr_atp_supply_demand := l_null_atp_supply_demand ;
5092           l_curr_atp_period := l_null_atp_period;
5093 
5094           j := l_sources.source_type.NEXT(j);
5095 
5096          END LOOP;  -- end of j loop for warehouse
5097          --- Bug 1104565
5098          --IF j = l_sources.source_type.count THEN
5099          --   --- ADD the pegging id for the best source
5100          --   FOR l_peg_count in 1..l_atp_table.ship_date.count LOOP
5101          --     l_atp_table.end_pegging_id(l_peg_count) := l_fst_src_pegging_ids(1);
5102          --   END LOOP;
5103          --END IF;
5104 
5105          IF (l_temp_atp_table.Action.COUNT > 0) THEN
5106 
5107            IF PG_DEBUG in ('Y', 'C') THEN
5108               msc_sch_wb.atp_debug('Schedule: ' || 'I am here!!!');
5109            END IF;
5110            -- we need to assign oe_flag for oe to indicate which
5111            -- scenario they should show to users if we return multiple
5112            -- scenarios.  Now we need to pick one which is inside the
5113            -- latest acceptable range
5114 
5115 
5116            IF l_multi_scenario = 1 THEN
5117 
5118             l_oe_demand_flag := 'N';
5119 
5120              FOR m in 1..l_sources.source_type.COUNT LOOP
5121 
5122                IF l_oe_demand_flag = 'N' THEN -- if we haven't find the scenario
5123                  l_temp := 0;
5124 
5125                  FOR j in ((m-1)*l_ship_count+1)..(m*l_ship_count) LOOP
5126                    IF(((l_temp_atp_table.requested_ship_date(j) IS NOT NULL) AND
5127                        (l_temp_atp_table.ship_date(j)
5128                       <= NVL(l_temp_atp_table.latest_acceptable_date(j),
5129                              l_temp_atp_table.requested_ship_date(j))))
5130                       OR
5131                     ((l_temp_atp_table.requested_arrival_date(j)IS NOT NULL) AND
5132                       (l_temp_atp_table.ship_date(j)+
5133                       NVL(l_temp_atp_table.delivery_lead_time(j), 0)
5134                       <= NVL(l_temp_atp_table.latest_acceptable_date(j),
5135                              l_temp_atp_table.requested_arrival_date(j))))) THEN
5136 
5137 
5138                     -- inside the acceptable range
5139                        l_temp := l_temp +1;
5140                    ELSE
5141                        -- not in the range, then we can exit the j loop since
5142                        -- this i th scenario won't be the right one.
5143                        -- however, if we only have one source here, then
5144                        -- even if it is not in the range, we should return
5145                        -- this to OE.
5146                        IF l_sources.source_type.COUNT = 1 THEN
5147                           l_temp :=  l_temp +1;
5148                        ELSE
5149                           EXIT;
5150                        END IF;
5151                    END IF;
5152                  END LOOP;  -- end of j loop
5153 
5154                  IF l_temp = l_ship_count THEN  -- l_ship_count = 1 for a line
5155                  -- we find the right scenario (m) for oe
5156                    l_oe_demand_flag := 'Y';
5157                    FOR j in ((m-1)*l_ship_count+1)..(m*l_ship_count) LOOP
5158                      l_temp_atp_table.OE_Flag(j) := 'Y';
5159                    END LOOP;
5160                    EXIT;
5161                  END IF;
5162                END IF; -- end if of l_oe_demand_flag
5163              END LOOP; -- m loop;
5164            END IF; -- end of l_multi_scenario = 1
5165 
5166            IF l_arrival_count > 1 THEN
5167 
5168              -- if this is an arrival set, since we need to organize the
5169              -- scenario before we actually store the info into x table,
5170              -- let's store the info in temp arr table.
5171              MSC_SATP_FUNC.Assign_Atp_Output_Rec(l_temp_atp_table,
5172                             l_temp_arr_atp_table,
5173                             x_return_status );
5174 
5175              MSC_ATP_PROC.Details_Output(l_temp_atp_period,
5176                      l_temp_atp_supply_demand,
5177                      l_temp_arr_atp_period,
5178                      l_temp_arr_atp_supply_demand,
5179                      x_return_status);
5180            ELSE
5181              MSC_SATP_FUNC.Assign_Atp_Output_Rec(l_temp_atp_table,
5182                             x_atp_table,
5183                             x_return_status );
5184 
5185              MSC_ATP_PROC.Details_Output(l_temp_atp_period,
5186                      l_temp_atp_supply_demand,
5187                      x_atp_period,
5188                      x_atp_supply_demand,
5189                      x_return_status);
5190            END IF;
5191          END IF;
5192 
5193 	 IF PG_DEBUG in ('Y', 'C') THEN
5194 	    msc_sch_wb.atp_debug('Schedule: ' || 'before k := p_atp_table.Arrival_Set_Name.NEXT(i)');
5195 	    msc_sch_wb.atp_debug ('Schedule: ' || 'k :='||to_char(k));
5196 	    msc_sch_wb.atp_debug ('Schedule: ' || 'i :='||to_char(i));
5197 	 END IF;
5198 
5199          IF l_arrival_count > 1 THEN
5200            k := p_atp_table.Arrival_Set_Name.NEXT(k);
5201          ELSE
5202            k := null;
5203          END IF;
5204 
5205 	 IF PG_DEBUG in ('Y', 'C') THEN
5206 	    msc_sch_wb.atp_debug('Schedule: ' || 'after k := p_atp_table.Arrival_Set_Name.NEXT(i)');
5207 	    msc_sch_wb.atp_debug ('Schedule: ' || 'k :='||to_char(k));
5208 	    msc_sch_wb.atp_debug ('Schedule: ' || 'i :='||to_char(i));
5209 	 END IF;
5210 
5211          l_temp_atp_supply_demand := l_null_atp_supply_demand;
5212          l_temp_atp_period := l_null_atp_period;
5213          l_temp_atp_details := l_null_atp_details;
5214          l_temp_atp_table := x_atp_table;
5215         END LOOP;  -- end of k loop for arrival set
5216 
5217     -- Mark moving ELSE and END IF towards the end of the Schedule Procedure. MARK
5218     /*
5219     ELSE
5220 
5221       -- for oe bug fix :  no record for undemand
5222 
5223       MSC_SATP_FUNC.Assign_Atp_Output_Rec(l_atp_table, x_atp_table, x_return_status );
5224 
5225     END IF; -- end if action = ATPQUERY or DEMANDADD or DEMANDMODIFY
5226     */
5227 
5228 
5229       -- now we organize the scenarios for arrival set if arrival set exists.
5230       -- we will only have 2 scenarios.
5231       -- 1.  what OE wants.  one warehouse for each item in the arrival set.
5232       --     consider latest_acceptable_dates
5233       -- 2.  what MRP wants. may display multiple warehouses for a line
5234       --     in the arrival set as long as we can satisfy (or close) to the
5235       --     request.
5236       -- AND we need to work on group date.
5237 
5238       -- xxxx to be consistent with ship sets...
5239       IF l_arrival_count > 1 and p_atp_table.quantity_ordered(1) <> 0 THEN
5240         -- Bug 2259824
5241 	l_group_date := TRUNC(sysdate);--4460369 + MSC_ATP_PVT.G_END_OF_DAY;
5242         IF PG_DEBUG in ('Y', 'C') THEN
5243             msc_sch_wb.atp_debug('Schedule: 2259824 l_group_date = '||
5244                  to_char(l_group_date, 'DD-MON-YYYY HH24:MI:SS'));
5245         END IF;
5246 
5247 	IF l_multi_scenario = 1 THEN
5248 
5249         IF PG_DEBUG in ('Y', 'C') THEN
5250            msc_sch_wb.atp_debug('Schedule: ' || 'print data from l_temp_arr_atp_table');
5251            i := l_temp_arr_atp_table.Action.FIRST;
5252            WHILE i IS NOT NULL LOOP
5253 	      msc_sch_wb.atp_debug('Schedule: ' || 'l_temp_arr_atp_table.Inventory_Item_Id( ' ||
5254 		   to_char(i) || '):'|| to_char(l_temp_arr_atp_table.Inventory_Item_Id(i)) );
5255 	      msc_sch_wb.atp_debug('Schedule: ' || 'l_temp_arr_atp_table.Source_Organization_Id:' ||
5256 		   to_char(l_temp_arr_atp_table.Source_Organization_Id(i)) );
5257 	      msc_sch_wb.atp_debug('Schedule: ' || 'l_temp_arr_atp_table.Quantity_Ordered:' ||
5258 		   to_char(l_temp_arr_atp_table.Quantity_Ordered(i)) );
5259 	      msc_sch_wb.atp_debug('Schedule: ' || 'l_temp_arr_atp_table.OE_FLAG:' ||
5260 		   l_temp_arr_atp_table.OE_FLAG(i) );
5261 	      i := l_temp_arr_atp_table.Action.NEXT(i);
5262            END LOOP;
5263 	END IF;
5264 
5265            -- work on the first scenario
5266            FOR i in 1..l_temp_arr_atp_table.Action.COUNT LOOP
5267              IF l_temp_arr_atp_table.OE_Flag(i) = 'Y' THEN
5268                -- For bug 2748730. Move the group date to end of day
5269 	       -- This ensures that group dates are also moved to the end of day
5270 	       l_group_date := greatest(l_group_date,
5271                                l_temp_arr_atp_table.Ship_Date(i)+
5272                              NVL(l_temp_arr_atp_table.delivery_lead_time(i),0));
5273 			     --4460369+ MSC_ATP_PVT.G_END_OF_DAY;
5274 
5275                MSC_SATP_FUNC.Assign_Atp_Input_Rec(l_temp_arr_atp_table,
5276                                     i,
5277                                     x_atp_table,
5278                                     l_return_status);
5279 
5280                MSC_ATP_PROC.Get_SD_Period_Rec(l_temp_arr_atp_period,
5281                                  l_temp_arr_atp_supply_demand,
5282                                  l_temp_arr_atp_table.Identifier(i),
5283                                  l_temp_arr_atp_table.Scenario_Id(i),
5284                                  1,
5285                                  x_atp_period,
5286                                  x_atp_supply_demand,
5287                                  l_return_status);
5288              END IF;
5289            END LOOP;
5290 
5291            -- first scenario: populate group date
5292            IF PG_DEBUG in ('Y', 'C') THEN
5293               msc_sch_wb.atp_debug('Schedule: ' || 'working on group date');
5294            END IF;
5295            FOR i in 1..x_atp_table.Action.COUNT LOOP
5296              x_atp_table.Scenario_Id(i) := 1;
5297            END LOOP;
5298 
5299            j := x_atp_table.Action.COUNT;
5300            -- we need to remember this COUNT so that j + 1 is the beginning
5301            -- of scenario 2.
5302 
5303            -- work on the second scenario
5304 
5305            i:=1;
5306            FOR k in 1..p_atp_table.Action.COUNT LOOP
5307              l_total_qty := 0.0;
5308              l_done_scenario2 := 'N';
5309 
5310              WHILE i IS NOT NULL LOOP
5311                IF (l_temp_arr_atp_table.Identifier(i)=p_atp_table.Identifier(k))
5312                    AND (l_temp_arr_atp_table.inventory_item_id(i) =
5313                         p_atp_table.inventory_item_id(k) ) THEN
5314 
5315                  IF l_done_scenario2 = 'N' THEN
5316 
5317                    -- we only care the warehouse that provides positive
5318                    -- requested_date_quantity
5319 
5320                      l_total_qty:=l_total_qty +greatest(0,
5321                                l_temp_arr_atp_table.Requested_Date_Quantity(i));
5322                      MSC_SATP_FUNC.Assign_Atp_Input_Rec(l_temp_arr_atp_table,
5323                                         i,
5324                                         x_atp_table,
5325                                         l_return_status );
5326 
5327                      MSC_ATP_PROC.Get_SD_Period_Rec(l_temp_arr_atp_period,
5328                                  l_temp_arr_atp_supply_demand,
5329                                  l_temp_arr_atp_table.Identifier(i),
5330                                  l_temp_arr_atp_table.Scenario_Id(i),
5331                                  2,
5332                                  x_atp_period,
5333                                  x_atp_supply_demand,
5334                                  l_return_status);
5335 
5336                      IF l_total_qty>=l_temp_arr_atp_table.Quantity_Ordered(i) THEN
5337                        l_done_scenario2 := 'Y';
5338                      END IF;
5339                  END IF;
5340                  i := l_temp_arr_atp_table.Action.NEXT(i);
5341 
5342                ELSE
5343                  -- for this line we are done, we need to move to next line
5344                  -- (in k loop)
5345                  EXIT;
5346                END IF;
5347              END LOOP;  -- end of i loop
5348              IF l_done_scenario2 = 'Y' THEN
5349                -- for this line we can satisfy on the request date
5350 	       -- For bug 2748730. Move the group date to end of day even if line is overridden
5351 	       -- This ensures that group dates are also moved to the end of day
5352                l_group_date := TRUNC(Greatest(l_group_date,
5353                                    p_atp_table.Requested_Arrival_Date(k)));--4460369 + MSC_ATP_PVT.G_END_OF_DAY;
5354              ELSE
5355                l_group_date := NULL;
5356              END IF;
5357            END LOOP;  -- end of k loop
5358 
5359            -- second scenario: populate group date
5360 
5361            FOR i in j+1..x_atp_table.Action.COUNT LOOP
5362              x_atp_table.Group_Arrival_Date(i) := l_group_date;
5363              x_atp_table.OE_Flag(i) := 'N';
5364              x_atp_table.Scenario_Id(i) := 2;
5365            END LOOP;
5366 
5367         ELSE
5368            -- multi_scenario = 0
5369            -- this is the OE case
5370            -- get the group date.
5371            IF PG_DEBUG in ('Y', 'C') THEN
5372               msc_sch_wb.atp_debug('Schedule: ' || 'in arrival set, oe case');
5373            END IF;
5374 
5375            MSC_SATP_FUNC.Assign_Atp_Output_Rec(l_temp_arr_atp_table,
5376                             x_atp_table,
5377                             x_return_status );
5378 
5379            MSC_ATP_PROC.Details_Output(l_temp_arr_atp_period,
5380                      l_temp_arr_atp_supply_demand,
5381                      x_atp_period,
5382                      x_atp_supply_demand,
5383                      x_return_status);
5384 
5385            /* 2896854
5386            FOR i in 1..x_atp_table.Action.COUNT LOOP
5387                -- For bug 2748730. Move the group date to end of day even if line is overridden
5388 	       -- This ensures that group dates are also moved to the end of day
5389 	       l_group_date := TRUNC(greatest(l_group_date,
5390                                l_temp_arr_atp_table.Ship_Date(i)+
5391                              NVL(l_temp_arr_atp_table.delivery_lead_time(i),0)))
5392 			     + MSC_ATP_PVT.G_END_OF_DAY;
5393            END LOOP;
5394            */
5395 
5396 
5397            -- 1072816  need to do something to undo
5398 
5399            IF PG_DEBUG in ('Y', 'C') THEN
5400               msc_sch_wb.atp_debug('Schedule: ' || 'l_group_date ='||
5401 			to_char(l_group_date, 'DD-MON-YYYY HH24:MI:SS'));
5402               msc_sch_wb.atp_debug('Schedule: ' || 'p_atp_table.Requested_Arrival_Date(1) = '||
5403 			to_char(p_atp_table.Requested_Arrival_Date(1), 'DD-MON-YYYY HH24:MI:SS'));
5404            END IF;
5405            -- dsting setproc
5406 
5407            -- 2902129 delay set processing til here for arrival set
5408            MSC_ATP_PROC.Initialize_Set_Processing(x_atp_table);
5409            for k in 1..x_atp_table.action.count() loop
5410                MSC_ATP_PROC.Process_Set_Line(x_atp_table, k, l_line_status);
5411            end loop;
5412 
5413            MSC_ATP_PROC.Process_Set_Dates_Errors(x_atp_table, 'D', l_set_status);
5414            l_group_date := x_atp_table.group_arrival_date(1);
5415 
5416 --           IF l_set_status <> ALLSUCCESS THEN
5417            IF l_set_status <> ALLSUCCESS OR x_atp_table.action(1) = ATPQUERY
5418            THEN
5419              -- arrival set fails/ we need to remove demands
5420              IF PG_DEBUG in ('Y', 'C') THEN
5421                 msc_sch_wb.atp_debug('Schedule: ' || 'before removing the data for arrival set');
5422              END IF;
5423 
5424              -- 2902265
5425              MSC_ATP_PROC.cleanup_set(p_instance_id,
5426                                       l_plan_id,
5427                                       l_fst_src_pegging_ids,
5428                                       l_demand_class_atp_flag);
5429 /*
5430              FOR i in 1..MSC_ATP_PVT.G_PEGGING_FOR_SET.COUNT LOOP
5431                IF PG_DEBUG in ('Y', 'C') THEN
5432                   msc_sch_wb.atp_debug('Schedule: ' || 'in the loop');
5433                END IF;
5434                MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(
5435                  MSC_ATP_PVT.G_PEGGING_FOR_SET(i)  ,
5436                  p_instance_id,
5437                  l_plan_id,
5438                  UNDO,
5439                  MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG(i),
5440                  l_return_status);
5441              END LOOP;
5442 */
5443              MSC_ATP_PVT.G_PEGGING_FOR_SET.DELETE;
5444              MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG.DELETE;
5445              MSC_ATP_PVT.G_REQ_ATP_DATE.DELETE;
5446              MSC_ATP_PVT.G_REQ_DATE_QTY.DELETE;
5447               IF PG_DEBUG in ('Y', 'C') THEN
5448                  msc_sch_wb.atp_debug('Schedule: ' || 'Do I come here 12');
5449               END IF;
5450            ELSE
5451              IF PG_DEBUG in ('Y', 'C') THEN
5452                 msc_sch_wb.atp_debug('arrival set succeeded');
5453              END IF;
5454              -- dsting setproc
5455 
5456              -- 2902265
5457              MSC_ATP_PVT.G_PEGGING_FOR_SET := l_fst_src_pegging_ids;
5458              MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG := l_demand_class_atp_flag;
5459              MSC_ATP_PVT.G_REQ_ATP_DATE := l_req_atp_DATE;
5460              MSC_ATP_PVT.G_REQ_DATE_QTY := l_req_date_qty;
5461 
5462              MSC_ATP_PROC.Update_Set_SD_Dates(x_atp_table,l_req_arr_date); --Plan by request date
5463            END IF;
5464 
5465            FOR i in 1..x_atp_table.Action.COUNT LOOP
5466              x_atp_table.Scenario_Id(i) := 1;
5467            END LOOP;
5468 
5469            FOR i in 1..x_atp_period.Level.COUNT LOOP
5470              x_atp_period.Scenario_Id(i) := 1;
5471            END LOOP;
5472 
5473            FOR i in 1..x_atp_supply_demand.Level.COUNT LOOP
5474              x_atp_supply_demand.Scenario_Id(i) := 1;
5475            END LOOP;
5476 
5477            FOR i in 1..x_atp_details.Level.COUNT LOOP
5478              x_atp_details.Scenario_Id(i) := 1;
5479            END LOOP;
5480 
5481         END IF;
5482       END IF;
5483 
5484       -- Bug 1661545, if scheduling was unsuccessful, make sure that old demand record is
5485       -- preserved back, as it was updated to 0 in the begining in case of reschedule.
5486 
5487       ---bug 2384224
5488       IF PG_DEBUG in ('Y', 'C') THEN
5489         msc_sch_wb.atp_debug('Schedule: Debug l_inv_ctp ' || l_inv_ctp);
5490         msc_sch_wb.atp_debug('Schedule: Debug G_INV_CTP ' || MSC_ATP_PVT.G_INV_CTP);
5491         msc_sch_wb.atp_debug('Schedule: Debug x_atp_table.Action(1)' || x_atp_table.Action(1));
5492       END IF;
5493       IF /*(l_inv_ctp = 4) AND */ (x_atp_table.Action(1) = DEMANDMODIFY OR --3720018, commented for support of rescheduling in ODS
5494           x_atp_table.Action(1) = DEMANDADD OR
5495           x_atp_table.Action(1) = DMDRSVADD) THEN
5496 
5497         /*-- Loop thru the whole set to check if any of the records had failed Scheduling
5498         -- Update demands to reset the quantity and date in such cases
5499         -- Not needed if error_code is ALLSUCCESS or ATP_NOT_APPL.
5500         l_delete_demand_flag := 0;
5501         FOR m in 1..x_atp_table.Action.COUNT LOOP
5502             IF (NVL(x_atp_table.error_code(m), -1) NOT IN (0, 61)) THEN
5503                 l_delete_demand_flag := 1;
5504                 EXIT;
5505             END IF;
5506         END LOOP; */
5507 
5508         IF NVL(x_atp_table.error_code(1), -1) not in (0, 61) THEN --3720018
5509             IF PG_DEBUG in ('Y', 'C') THEN
5510                 msc_sch_wb.atp_debug('Schedule: ' || 'before resetting the demands when scheduling failed');
5511                 msc_sch_wb.atp_debug('Schedule: ' || 'rajjain - Single call to Undo_Delete_Row');
5512             END IF;
5513 
5514             -- rajjain single call to Undo_Delete_Row procedure --3720018
5515             MSC_ATP_DB_UTILS.Undo_Delete_Row(l_delete_atp_rec.del_identifiers,
5516                     l_delete_atp_rec.del_plan_ids,
5517                     p_instance_id,
5518                     --subst
5519                     l_delete_atp_rec.del_demand_ids,
5520                     l_delete_atp_rec.del_inv_item_ids,
5521                     l_delete_atp_rec.del_copy_demand_ids,        -- For summary enhancement
5522                     l_delete_atp_rec.del_copy_demand_plan_ids,   -- For summary enhancement
5523                     l_time_phased_set,            -- For time_phased_atp
5524                     -- CTO ODR and Simplified Pegging
5525                     l_delete_atp_rec.del_atp_peg_items,
5526                     l_delete_atp_rec.del_atp_peg_demands ,
5527                     l_delete_atp_rec.del_atp_peg_supplies,
5528                     l_delete_atp_rec.del_atp_peg_res_reqs,
5529                     l_delete_atp_rec.del_demand_source_type, --cmro
5530                     -- End CTO ODR and Simplified Pegging
5531                     l_delete_atp_rec.atp_peg_demands_plan_ids,  --Bug 3629191
5532                     l_delete_atp_rec.atp_peg_supplies_plan_ids, --Bug 3629191
5533                     l_delete_atp_rec.atp_peg_res_reqs_plan_ids,  --Bug 3629191
5534                     l_delete_atp_rec.del_ods_demand_ids, --3720018, added for support of rescheduling in ODS
5535                     l_delete_atp_rec.del_ods_inv_item_ids, --3720018, added for support of rescheduling in ODS
5536                     l_delete_atp_rec.del_ods_demand_src_type, --3720018, added for support of rescheduling in ODS
5537                     l_delete_atp_rec.del_ods_cto_demand_ids, --3720018, added for support of rescheduling in ODS
5538                     l_delete_atp_rec.del_ods_cto_inv_item_ids, --3720018, added for support of rescheduling in ODS
5539                     l_delete_atp_rec.del_ods_cto_dem_src_type, --3720018, added for support of rescheduling in ODS
5540                     l_delete_atp_rec.del_ods_atp_refresh_no, --3720018, added for support of rescheduling in ODS
5541                     l_delete_atp_rec.del_ods_cto_atp_refresh_no --3720018, added for support of rescheduling in ODS
5542                     );
5543 
5544             IF PG_DEBUG in ('Y', 'C') THEN
5545                 msc_sch_wb.atp_debug('Schedule: ' || 'after resetting the demands when scheduling failed');
5546             END IF;
5547 
5548         ELSE    -- For summary enhancement
5549 
5550            --3720018 ,changes to refer all the records from l_delete_atp_rec
5551 
5552            -- Append copy SO ids for summary enhancement --3720018
5553            IF l_delete_atp_rec.del_copy_demand_ids IS NOT NULL and l_delete_atp_rec.del_copy_demand_ids.COUNT > 0 THEN
5554                l_count := MSC_ATP_PVT.G_COPY_DEMAND_ID.count;
5555                MSC_ATP_PVT.G_COPY_DEMAND_ID.extend(l_delete_atp_rec.del_copy_demand_ids.count);
5556                MSC_ATP_PVT.G_COPY_DEMAND_PLAN_ID.extend(l_delete_atp_rec.del_copy_demand_ids.count);
5557                FOR i in 1..l_delete_atp_rec.del_copy_demand_ids.count LOOP
5558                    MSC_ATP_PVT.G_COPY_DEMAND_ID(l_count + i) := l_delete_atp_rec.del_copy_demand_ids(i);
5559                    MSC_ATP_PVT.G_COPY_DEMAND_PLAN_ID(l_count + i) := l_delete_atp_rec.del_copy_demand_plan_ids(m);
5560                END LOOP;
5561            END IF;
5562 
5563            --Changes for Bug 3629191 : Population of Offset data in Global Records.
5564 
5565            IF PG_DEBUG in ('Y', 'C') THEN
5566               msc_sch_wb.atp_debug('Schedule: G_OFF_DEM_TRANS_ID.count ' || MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.count );
5567            END IF;
5568 
5569            IF l_delete_atp_rec.del_demand_ids IS NOT NULL and l_delete_atp_rec.del_demand_ids.COUNT > 0 THEN
5570               l_count := MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.count;
5571               MSC_ATP_PVT.G_OFF_INSTANCE_ID.extend(l_delete_atp_rec.del_demand_ids.count);
5572               MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.EXTEND(l_delete_atp_rec.del_demand_ids.count);
5573               MSC_ATP_PVT.G_OFF_PLAN_ID.EXTEND(l_delete_atp_rec.del_demand_ids.count);
5574               IF PG_DEBUG in ('Y', 'C') THEN
5575                  msc_sch_wb.atp_debug('Schedule: l_delete_atp_rec.del_demand_ids ' || l_delete_atp_rec.del_demand_ids.count);
5576               END IF;
5577               FOR i in 1..l_delete_atp_rec.del_demand_ids.count LOOP
5578                   MSC_ATP_PVT.G_OFF_INSTANCE_ID(l_count + i)  := p_instance_id;
5579                   MSC_ATP_PVT.G_OFF_DEM_TRANS_ID(l_count + i) := l_delete_atp_rec.del_demand_ids(i);
5580                   MSC_ATP_PVT.G_OFF_PLAN_ID(l_count + i)      := l_delete_atp_rec.del_plan_ids(i);
5581               END LOOP;
5582            END IF;
5583 
5584            IF PG_DEBUG in ('Y', 'C') THEN
5585               msc_sch_wb.atp_debug('Schedule: G_OFF_DEM_TRANS_ID.count ' || MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.count );
5586            END IF;
5587 
5588            IF l_delete_atp_rec.del_atp_peg_demands IS NOT NULL and l_delete_atp_rec.del_atp_peg_demands.COUNT > 0 THEN
5589               l_count := MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.count;
5590               MSC_ATP_PVT.G_OFF_INSTANCE_ID.extend(l_delete_atp_rec.del_atp_peg_demands.count);
5591               MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.EXTEND(l_delete_atp_rec.del_atp_peg_demands.count);
5592               MSC_ATP_PVT.G_OFF_PLAN_ID.EXTEND(l_delete_atp_rec.del_atp_peg_demands.count);
5593               IF PG_DEBUG in ('Y', 'C') THEN
5594                  msc_sch_wb.atp_debug('Schedule: l_delete_atp_rec.del_atp_peg_demands.count ' || l_delete_atp_rec.del_atp_peg_demands.count);
5595               END IF;
5596               FOR i in 1..l_delete_atp_rec.del_atp_peg_demands.count LOOP
5597                   MSC_ATP_PVT.G_OFF_INSTANCE_ID(l_count + i)  := l_delete_atp_rec.off_demand_instance_id(i);
5598                   MSC_ATP_PVT.G_OFF_DEM_TRANS_ID(l_count + i) := l_delete_atp_rec.del_atp_peg_demands(i);
5599                   MSC_ATP_PVT.G_OFF_PLAN_ID(l_count + i)      := l_delete_atp_rec.atp_peg_demands_plan_ids(i);
5600               END LOOP;
5601            END IF;
5602 
5603            IF PG_DEBUG in ('Y', 'C') THEN
5604               msc_sch_wb.atp_debug('Schedule: G_OFF_DEM_TRANS_ID.count ' || MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.count );
5605            END IF;
5606 
5607            IF l_delete_atp_rec.del_atp_peg_supplies IS NOT NULL and l_delete_atp_rec.del_atp_peg_supplies.COUNT > 0 THEN
5608               l_count := MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.count;
5609               MSC_ATP_PVT.G_OFF_INSTANCE_ID.extend(l_delete_atp_rec.del_atp_peg_supplies.count);
5610               MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.EXTEND(l_delete_atp_rec.del_atp_peg_supplies.count);
5611               MSC_ATP_PVT.G_OFF_PLAN_ID.EXTEND(l_delete_atp_rec.del_atp_peg_supplies.count);
5612               IF PG_DEBUG in ('Y', 'C') THEN
5613                  msc_sch_wb.atp_debug('Schedule: l_delete_atp_rec.del_atp_peg_supplies.count ' || l_delete_atp_rec.del_atp_peg_supplies.count );
5614               END IF;
5615               FOR i in 1..l_delete_atp_rec.del_atp_peg_supplies.count LOOP
5616                   MSC_ATP_PVT.G_OFF_INSTANCE_ID(l_count + i)  := l_delete_atp_rec.off_supply_instance_id(i);
5617                   MSC_ATP_PVT.G_OFF_DEM_TRANS_ID(l_count + i) := l_delete_atp_rec.del_atp_peg_supplies(i);
5618                   MSC_ATP_PVT.G_OFF_PLAN_ID(l_count + i)      := l_delete_atp_rec.atp_peg_supplies_plan_ids(i);
5619               END LOOP;
5620            END IF;
5621 
5622            IF PG_DEBUG in ('Y', 'C') THEN
5623               msc_sch_wb.atp_debug('Schedule: G_OFF_DEM_TRANS_ID.count ' || MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.count );
5624            END IF;
5625 
5626            IF l_delete_atp_rec.del_atp_peg_res_reqs IS NOT NULL and l_delete_atp_rec.del_atp_peg_res_reqs.COUNT > 0 THEN
5627               l_count := MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.count;
5628               MSC_ATP_PVT.G_OFF_INSTANCE_ID.extend(l_delete_atp_rec.del_atp_peg_res_reqs.count);
5629               MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.EXTEND(l_delete_atp_rec.del_atp_peg_res_reqs.count);
5630               MSC_ATP_PVT.G_OFF_PLAN_ID.EXTEND(l_delete_atp_rec.del_atp_peg_res_reqs.count);
5631               IF PG_DEBUG in ('Y', 'C') THEN
5632                  msc_sch_wb.atp_debug('Schedule: l_delete_atp_rec.del_atp_peg_res_reqs.count  ' || l_delete_atp_rec.del_atp_peg_res_reqs.count );
5633               END IF;
5634               FOR i in 1..l_delete_atp_rec.del_atp_peg_res_reqs.count LOOP
5635                   MSC_ATP_PVT.G_OFF_INSTANCE_ID(l_count + i)  := l_delete_atp_rec.off_res_instance_id(i);
5636                   MSC_ATP_PVT.G_OFF_DEM_TRANS_ID(l_count + i) := l_delete_atp_rec.del_atp_peg_res_reqs(i);
5637                   MSC_ATP_PVT.G_OFF_PLAN_ID(l_count + i)      := l_delete_atp_rec.atp_peg_res_reqs_plan_ids(i);
5638               END LOOP;
5639            END IF;
5640            --3720018
5641 
5642            IF PG_DEBUG in ('Y', 'C') THEN
5643               msc_sch_wb.atp_debug('Schedule: G_OFF_DEM_TRANS_ID.count ' || MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.count );
5644            END IF;
5645 
5646            --Changes for Bug 3629191: Population of Offset data in Global record ends.
5647 
5648            -- CTO ODR and Simplified Pegging
5649            IF MSC_ATP_CTO.G_MODEL_IS_PRESENT_IN_SET = 1 THEN
5650 
5651            -- ATP request successfull, create atp pegging
5652             IF PG_DEBUG in ('Y', 'C') THEN
5653                 msc_sch_wb.atp_debug('Schedule: ' || 'Model present in Set Creating ATP Pegging');
5654             END IF;
5655 
5656              FOR i in 1..x_atp_table.inventory_item_id.COUNT LOOP
5657                IF ((x_atp_table.ATO_Model_Line_Id(i) IS NOT NULL) AND
5658                    (x_atp_table.identifier(i) = NVL(x_atp_table.ATO_Model_Line_Id(i), 0)) AND
5659 
5660                    -- Bug 3334643 Create Pegging only if the Model  configuration
5661                    -- item ATP request goes against a plan.
5662                    (x_atp_table.plan_id(i) > 0) AND
5663                    -- Bug 3343757 Create ATP Simplified pegging only if
5664                    -- plan_id > 0 and pegging has been generated.
5665                    (x_atp_table.End_Pegging_Id(i) IS NOT NULL) ) THEN
5666 
5667                  IF PG_DEBUG in ('Y', 'C') THEN
5668                     msc_sch_wb.atp_debug('Schedule: Before Call to Create_Atp_Pegging ');
5669                     msc_sch_wb.atp_debug('Schedule: Plan_id : '|| x_atp_table.plan_id(i) );
5670                     msc_sch_wb.atp_debug('Schedule: Pegging_id : '|| x_atp_table.End_Pegging_id(i));
5671                     msc_sch_wb.atp_debug('Schedule: Match Item : '|| x_atp_table.match_item_id(i));
5672                     msc_sch_wb.atp_debug('Schedule: Item Id : '|| x_atp_table.Inventory_Item_Id(i));
5673                     msc_sch_wb.atp_debug('Schedule: Req Item : '|| x_atp_table.request_item_id(i));
5674                  END IF;
5675 
5676                     MSC_ATP_PEG.Create_Atp_Pegging (
5677                           x_atp_table.End_Pegging_Id(i),
5678                           p_instance_id,
5679                           l_old_plan_id(i),
5680                           x_atp_table.ATO_Model_Line_Id(i),
5681                           x_atp_table.Config_item_line_id(i),
5682                           x_atp_table.demand_source_type(i),--cmro
5683                           l_return_status
5684                           );
5685                ELSE
5686                    IF PG_DEBUG in ('Y', 'C') THEN
5687                       msc_sch_wb.atp_debug('Schedule: No Call to Create_Atp_Pegging ');
5688                       msc_sch_wb.atp_debug('Schedule: Plan_id : '|| x_atp_table.plan_id(i) );
5689                    END IF;
5690 
5691                  -- End Bug 3334643
5692 
5693                END IF;
5694 
5695              END LOOP;
5696 
5697            END IF; -- MSC_ATP_CTO.G_MODEL_IS_PRESENT_IN_SET = 1
5698            -- End CTO ODR and Simplified Pegging
5699 
5700         END IF;
5701         --e_cto_rearch
5702       END IF;     -- Bug 1661545
5703 
5704       /*-- we now insert new picture for each session. Hence we do not need to update old picture
5705       IF l_inv_ctp = 4 and MSC_ATP_CTO.G_MODEL_IS_PRESENT_IN_SET =1
5706                        AND NVL(MSC_ATP_PVT.G_SYNC_ATP_CHECK, 'N') = 'N' THEN
5707          IF  (x_atp_table.action(1) = ATPQUERY or
5708              ((x_atp_table.Action(1) = DEMANDMODIFY OR
5709              x_atp_table.Action(1) = DEMANDADD OR
5710              x_atp_table.Action(1) = DMDRSVADD)
5711              AND NVL(x_atp_table.error_code(1), -1) not in (0, 61))) THEN
5712 
5713              --failure case
5714              MSC_ATP_CTO.Maintain_OS_Sourcing(p_instance_id,
5715                                                x_atp_table,
5716                                                MSC_ATP_CTO.FAIL);
5717          ELSE
5718               --scheduling and success
5719               MSC_ATP_CTO.Maintain_OS_Sourcing(p_instance_id,
5720                                                 x_atp_table,
5721                                                 MSC_ATP_CTO.Success);
5722          END IF;
5723 
5724       END IF;
5725       */
5726 
5727     -- Mark moved ELSE and END IF to here from the previous MARK point.
5728     ELSE
5729 
5730     IF PG_DEBUG in ('Y', 'C') THEN
5731        msc_sch_wb.atp_debug('Schedule: Unscheduling...');
5732     END IF;
5733 
5734     --Changes for Bug 3629191 : Population of Offset data in Global Records.
5735 
5736     --3720018, arrays to be refered from l_delete_atp_rec.
5737 
5738     IF PG_DEBUG in ('Y', 'C') THEN
5739        msc_sch_wb.atp_debug('Schedule: G_OFF_DEM_TRANS_ID.count ' || MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.count );
5740     END IF;
5741 
5742     IF l_delete_atp_rec.del_demand_ids IS NOT NULL and l_delete_atp_rec.del_demand_ids.COUNT > 0 THEN
5743        l_count := MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.count;
5744        MSC_ATP_PVT.G_OFF_INSTANCE_ID.extend(l_delete_atp_rec.del_demand_ids.count);
5745        MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.EXTEND(l_delete_atp_rec.del_demand_ids.count);
5746        MSC_ATP_PVT.G_OFF_PLAN_ID.EXTEND(l_delete_atp_rec.del_demand_ids.count);
5747        IF PG_DEBUG in ('Y', 'C') THEN
5748           msc_sch_wb.atp_debug('Schedule: l_delete_atp_rec.del_demand_ids ' || l_delete_atp_rec.del_demand_ids.count);
5749        END IF;
5750        FOR i in 1..l_delete_atp_rec.del_demand_ids.count LOOP
5751            MSC_ATP_PVT.G_OFF_INSTANCE_ID(l_count + i)  := p_instance_id;
5752            MSC_ATP_PVT.G_OFF_DEM_TRANS_ID(l_count + i) := l_delete_atp_rec.del_demand_ids(i);
5753            MSC_ATP_PVT.G_OFF_PLAN_ID(l_count + i)      := l_delete_atp_rec.del_plan_ids(i);
5754        END LOOP;
5755     END IF;
5756 
5757 
5758     IF PG_DEBUG in ('Y', 'C') THEN
5759        msc_sch_wb.atp_debug('Schedule: G_OFF_DEM_TRANS_ID.count ' || MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.count );
5760     END IF;
5761 
5762     IF l_delete_atp_rec.del_atp_peg_demands IS NOT NULL and l_delete_atp_rec.del_atp_peg_demands.COUNT > 0 THEN
5763        l_count := MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.count;
5764        MSC_ATP_PVT.G_OFF_INSTANCE_ID.extend(l_delete_atp_rec.del_atp_peg_demands.count);
5765        MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.EXTEND(l_delete_atp_rec.del_atp_peg_demands.count);
5766        MSC_ATP_PVT.G_OFF_PLAN_ID.EXTEND(l_delete_atp_rec.del_atp_peg_demands.count);
5767        IF PG_DEBUG in ('Y', 'C') THEN
5768           msc_sch_wb.atp_debug('Schedule: l_delete_atp_rec.del_atp_peg_demands.count ' || l_delete_atp_rec.del_atp_peg_demands.count);
5769        END IF;
5770        FOR i in 1..l_delete_atp_rec.del_atp_peg_demands.count LOOP
5771            MSC_ATP_PVT.G_OFF_INSTANCE_ID(l_count + i)  := l_delete_atp_rec.off_demand_instance_id(i);
5772            MSC_ATP_PVT.G_OFF_DEM_TRANS_ID(l_count + i) := l_delete_atp_rec.del_atp_peg_demands(i);
5773            MSC_ATP_PVT.G_OFF_PLAN_ID(l_count + i)      := l_delete_atp_rec.atp_peg_demands_plan_ids(i);
5774        END LOOP;
5775      END IF;
5776 
5777 
5778      IF PG_DEBUG in ('Y', 'C') THEN
5779        msc_sch_wb.atp_debug('Schedule: G_OFF_DEM_TRANS_ID.count ' || MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.count );
5780      END IF;
5781 
5782      IF l_delete_atp_rec.del_atp_peg_supplies IS NOT NULL and l_delete_atp_rec.del_atp_peg_supplies.COUNT > 0 THEN
5783         l_count := MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.count;
5784         MSC_ATP_PVT.G_OFF_INSTANCE_ID.extend(l_delete_atp_rec.del_atp_peg_supplies.count);
5785         MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.EXTEND(l_delete_atp_rec.del_atp_peg_supplies.count);
5786         MSC_ATP_PVT.G_OFF_PLAN_ID.EXTEND(l_delete_atp_rec.del_atp_peg_supplies.count);
5787         IF PG_DEBUG in ('Y', 'C') THEN
5788            msc_sch_wb.atp_debug('Schedule: l_delete_atp_rec.del_atp_peg_supplies.count ' || l_delete_atp_rec.del_atp_peg_supplies.count );
5789         END IF;
5790         FOR i in 1..l_delete_atp_rec.del_atp_peg_supplies.count LOOP
5791              MSC_ATP_PVT.G_OFF_INSTANCE_ID(l_count + i)  := l_delete_atp_rec.off_supply_instance_id(i);
5792              MSC_ATP_PVT.G_OFF_DEM_TRANS_ID(l_count + i) := l_delete_atp_rec.del_atp_peg_supplies(i);
5793              MSC_ATP_PVT.G_OFF_PLAN_ID(l_count + i)      := l_delete_atp_rec.atp_peg_supplies_plan_ids(i);
5794         END LOOP;
5795      END IF;
5796 
5797 
5798      IF PG_DEBUG in ('Y', 'C') THEN
5799         msc_sch_wb.atp_debug('Schedule: G_OFF_DEM_TRANS_ID.count ' || MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.count );
5800      END IF;
5801 
5802      IF l_delete_atp_rec.del_atp_peg_res_reqs IS NOT NULL and l_delete_atp_rec.del_atp_peg_res_reqs.COUNT > 0 THEN
5803         l_count := MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.count;
5804         MSC_ATP_PVT.G_OFF_INSTANCE_ID.extend(l_delete_atp_rec.del_atp_peg_res_reqs.count);
5805         MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.EXTEND(l_delete_atp_rec.del_atp_peg_res_reqs.count);
5806         MSC_ATP_PVT.G_OFF_PLAN_ID.EXTEND(l_delete_atp_rec.del_atp_peg_res_reqs.count);
5807         IF PG_DEBUG in ('Y', 'C') THEN
5808            msc_sch_wb.atp_debug('Schedule: l_delete_atp_rec.del_atp_peg_res_reqs.count  ' || l_delete_atp_rec.del_atp_peg_res_reqs.count );
5809         END IF;
5810         FOR i in 1..l_delete_atp_rec.del_atp_peg_res_reqs.count LOOP
5811             MSC_ATP_PVT.G_OFF_INSTANCE_ID(l_count + i)  := l_delete_atp_rec.off_res_instance_id(i);
5812             MSC_ATP_PVT.G_OFF_DEM_TRANS_ID(l_count + i) := l_delete_atp_rec.del_atp_peg_res_reqs(i);
5813             MSC_ATP_PVT.G_OFF_PLAN_ID(l_count + i)      := l_delete_atp_rec.atp_peg_res_reqs_plan_ids(i);
5814         END LOOP;
5815       END IF;
5816       IF PG_DEBUG in ('Y', 'C') THEN
5817          msc_sch_wb.atp_debug('Schedule: G_OFF_DEM_TRANS_ID.count ' || MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.count );
5818       END IF;
5819       --3720018
5820 
5821       --Changes for Bug 3629191: Population of Offset data in Global record ends.
5822 
5823       -- for oe bug fix :  no record for undemand
5824 
5825       MSC_SATP_FUNC.Assign_Atp_Output_Rec(l_atp_table, x_atp_table, x_return_status );
5826       -- CTO ODR and Simplified Pegging
5827       -- Dealing with the Unschedule process
5828       IF (x_atp_table.Action(1) = DEMANDMODIFY OR
5829           x_atp_table.Action(1) = DEMANDADD OR
5830           x_atp_table.Action(1) = DMDRSVADD) THEN
5831         IF PG_DEBUG in ('Y', 'C') THEN
5832            msc_sch_wb.atp_debug('Schedule: Model in set : ' ||
5833                                      MSC_ATP_CTO.G_MODEL_IS_PRESENT_IN_SET);
5834         END IF;
5835 
5836            IF MSC_ATP_CTO.G_MODEL_IS_PRESENT_IN_SET = 1 THEN
5837              -- Dealing with Unschedule ATP Process.
5838              -- So Just Delete the old data.
5839              FOR i in 1..x_atp_table.inventory_item_id.COUNT LOOP
5840                IF (x_atp_table.ATO_Model_Line_Id(i) IS NOT NULL AND
5841                    (x_atp_table.identifier(i) = NVL(x_atp_table.ATO_Model_Line_Id(i), 0))) THEN
5842 
5843 
5844                   IF PG_DEBUG in ('Y', 'C') THEN
5845                      msc_sch_wb.atp_debug('End Pegging Id Pegging Identifier : ' ||
5846                                                       x_atp_table.End_Pegging_Id(i));
5847                      msc_sch_wb.atp_debug('Instance Id p_instance_id : ' || p_instance_id);
5848                      msc_sch_wb.atp_debug('Model Line Id Model_order_line_id : ' ||
5849                                                          x_atp_table.ATO_Model_Line_Id(i));
5850                      msc_sch_wb.atp_debug('Config. Line Id Config_order_line_id : ' ||
5851                                                        x_atp_table.Config_item_line_id(i));
5852                      msc_sch_wb.atp_debug('Old Plan Id : ' || l_old_plan_id(i));
5853                   END IF;
5854 
5855                   DELETE from msc_atp_pegging
5856                   WHERE  plan_id = l_old_plan_id(i)
5857                   AND    sales_order_line_id in (
5858                               NVL( x_atp_table.Config_item_line_id(i), -1),
5859                                x_atp_table.ATO_Model_Line_Id(i) );
5860 
5861                   IF PG_DEBUG in ('Y', 'C') THEN
5862                      msc_sch_wb.atp_debug('Schedule: Number of rows deleted '||
5863                                ' from Atp Pegging ' || SQL%ROWCOUNT);
5864                   END IF;
5865                END IF;
5866              END LOOP;
5867            END IF;
5868 
5869       END IF;
5870       -- End CTO ODR and Simplified Pegging
5871 
5872     END IF; -- end if action = ATPQUERY or DEMANDADD or DEMANDMODIFY
5873     -- commit;
5874     IF PG_DEBUG in ('Y', 'C') THEN
5875        msc_sch_wb.atp_debug('**********End Schedule Procedure************');
5876     END IF;
5877 
5878 EXCEPTION
5879 
5880     WHEN NO_DATA_FOUND THEN
5881         x_return_status := FND_API.G_RET_STS_ERROR;
5882 
5883         -- Resetting global variables for additional issue encountered while bug 3295831
5884         IF PG_DEBUG in ('Y', 'C') THEN
5885             msc_sch_wb.atp_debug('Schedule: ' || 'Resetting G_INV_CTP and G_ALLOCATED_ATP' );
5886         END IF;
5887         MSC_ATP_PVT.G_INV_CTP := NVL(l_inv_ctp, MSC_ATP_PVT.G_INV_CTP);
5888         -- ATP4drp re-set using original profile value
5889         --MSC_ATP_PVT.G_ALLOCATED_ATP := NVL(l_allocated_atp, MSC_ATP_PVT.G_ALLOCATED_ATP);
5890         MSC_ATP_PVT.G_ALLOCATED_ATP := MSC_ATP_PVT.G_ORIG_ALLOC_ATP;
5891         -- End ATP4drp
5892         MSC_ATP_PVT.G_ALLOCATION_METHOD := NVL(l_allocation_method, MSC_ATP_PVT.G_ALLOCATION_METHOD);
5893         --5221865
5894         MSC_ATP_PVT.G_FORWARD_ATP := NVL(l_forward_atp,MSC_ATP_PVT.G_FORWARD_ATP);
5895 
5896         -- Modified by NGOEL 12/13/00, replaced x_return_status with l_return_status
5897         -- Earlier error status was getting overwritten with the error status of the proc.
5898 
5899         -- 2625800: krajan
5900         ---agilent arrival set bug
5901         IF l_arrival_count > 1 THEN
5902            IF PG_DEBUG in ('Y', 'C') THEN
5903               msc_sch_wb.atp_debug ('Schedule: ' || 'Inside arrival set error processing');
5904            END IF;
5905            --for arrivalset l_atp_table contains only the record that we are processing
5906            --- so we need to make sure that we add all the records in arrival set to x_atp_tbale
5907            ---first we add the record we have processed so far
5908            MSC_SATP_FUNC.Assign_Atp_Output_Rec(l_temp_arr_atp_table, x_atp_table, l_return_status);
5909            ---now we add the current reocrd
5910            MSC_SATP_FUNC.Assign_Atp_Output_Rec(l_atp_table, x_atp_table, l_return_status);
5911            --now we assign rest of the record.
5912            l_count := l_temp_arr_atp_table.inventory_item_id.count + 2;
5913            IF PG_DEBUG in ('Y', 'C') THEN
5914               msc_sch_wb.atp_debug('Schedule: ' || 'l_count := ' || l_count);
5915            END IF;
5916            FOR i in l_count..p_atp_table.inventory_item_id.count LOOP
5917               MSC_SATP_FUNC.Assign_Atp_Input_Rec(p_atp_table, i, x_atp_table, l_return_status);
5918            END LOOP;
5919         ELSE
5920            MSC_SATP_FUNC.Assign_Atp_Output_Rec(l_atp_table, x_atp_table, l_return_status );
5921         END IF;
5922 
5923         IF PG_DEBUG in ('Y', 'C') THEN
5924            msc_sch_wb.atp_debug('Schedule: NO_DATA_FOUND: Stmt = '||l_stmt);
5925         END IF;
5926         -- 1072816  need to do something to undo
5927 
5928         FOR i in 1..MSC_ATP_PVT.G_PEGGING_FOR_SET.COUNT LOOP
5929             IF PG_DEBUG in ('Y', 'C') THEN
5930                msc_sch_wb.atp_debug('Schedule: ' || 'in the loop, i:='||i);
5931             END IF;
5932             MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(MSC_ATP_PVT.G_PEGGING_FOR_SET(i), p_instance_id,
5933               l_plan_id, UNDO, MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG(i),l_return_status);
5934         END LOOP;
5935 
5936         -- 2902265
5937         MSC_ATP_PROC.cleanup_set(p_instance_id,
5938                                  l_plan_id,
5939                                  l_fst_src_pegging_ids,
5940                                  l_demand_class_atp_flag);
5941 
5942         MSC_ATP_PVT.G_PEGGING_FOR_SET.DELETE;
5943         MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG.DELETE;
5944         MSC_ATP_PVT.G_REQ_ATP_DATE.DELETE;
5945         MSC_ATP_PVT.G_REQ_DATE_QTY.DELETE;
5946          IF PG_DEBUG in ('Y', 'C') THEN
5947             msc_sch_wb.atp_debug('Schedule: ' || 'Do I come here 13');
5948            msc_sch_wb.atp_debug('Schedule: ' || 'after the fix to remove the data');
5949         END IF;
5950 
5951     -- Bug 1661545, if scheduling was unsuccessful, make sure that old demand record is
5952     -- preserved back, as it was updated to 0 in the begining in case of reschedule.
5953 
5954     IF /*(MSC_ATP_PVT.G_INV_CTP = 4) AND */ (x_atp_table.Action(1) = DEMANDMODIFY OR --3720018, commented for support of rescheduling in ODS
5955         x_atp_table.Action(1) = DEMANDADD OR
5956         x_atp_table.Action(1) = DMDRSVADD) THEN
5957 
5958         IF PG_DEBUG in ('Y', 'C') THEN
5959            msc_sch_wb.atp_debug('Schedule: ' || 'check if need to reset demands when scheduling failed');
5960         END IF;
5961 
5962         -- Loop thru the whole set to check if any of the records had failed Scheduling
5963         -- Update demands to reset the quantity and date in such cases
5964         -- Not needed if error_code is ALLSUCCESS or ATP_NOT_APPL.
5965 
5966         --bug 2384224: un delete only those demands which are deleted
5967         IF PG_DEBUG in ('Y', 'C') THEN
5968            msc_sch_wb.atp_debug('Schedule: ' || 'before resetting the demands when scheduling failed');
5969       	   msc_sch_wb.atp_debug('Schedule: ' || 'rajjain - Single call to Undo_Delete_Row');
5970         END IF;
5971 
5972         -- rajjain single call to Undo_Delete_Row procedure
5973         --3720018,changes to refer all the records from l_delete_atp_rec
5974         MSC_ATP_DB_UTILS.Undo_Delete_Row(l_delete_atp_rec.del_identifiers,
5975             l_delete_atp_rec.del_plan_ids,
5976             p_instance_id,
5977             --subst
5978             l_delete_atp_rec.del_demand_ids,
5979             l_delete_atp_rec.del_inv_item_ids,
5980             l_delete_atp_rec.del_copy_demand_ids,        -- For summary enhancement
5981             l_delete_atp_rec.del_copy_demand_plan_ids,   -- For summary enhancement
5982             l_time_phased_set,            -- For time_phased_atp
5983             -- CTO ODR and Simplified Pegging
5984             l_delete_atp_rec.del_atp_peg_items,
5985             l_delete_atp_rec.del_atp_peg_demands ,
5986             l_delete_atp_rec.del_atp_peg_supplies,
5987             l_delete_atp_rec.del_atp_peg_res_reqs,
5988             l_delete_atp_rec.del_demand_source_type, --cmro
5989             -- End CTO ODR and Simplified Pegging
5990             l_delete_atp_rec.atp_peg_demands_plan_ids,  --Bug 3629191
5991             l_delete_atp_rec.atp_peg_supplies_plan_ids, --Bug 3629191
5992             l_delete_atp_rec.atp_peg_res_reqs_plan_ids,  --Bug 3629191
5993             l_delete_atp_rec.del_ods_demand_ids, --3720018, added for support of rescheduling in ODS
5994             l_delete_atp_rec.del_ods_inv_item_ids, --3720018, added for support of rescheduling in ODS
5995             l_delete_atp_rec.del_ods_demand_src_type, --3720018, added for support of rescheduling in ODS
5996             l_delete_atp_rec.del_ods_cto_demand_ids, --3720018, added for support of rescheduling in ODS
5997             l_delete_atp_rec.del_ods_cto_inv_item_ids, --3720018, added for support of rescheduling in ODS
5998             l_delete_atp_rec.del_ods_cto_dem_src_type, --3720018, added for support of rescheduling in ODS
5999             l_delete_atp_rec.del_ods_atp_refresh_no, --3720018, added for support of rescheduling in ODS
6000             l_delete_atp_rec.del_ods_cto_atp_refresh_no --3720018, added for support of rescheduling in ODS
6001             );
6002 
6003         IF PG_DEBUG in ('Y', 'C') THEN
6004            msc_sch_wb.atp_debug('Schedule: ' || 'after resetting the demands when scheduling failed');
6005         END IF;
6006 
6007         /* --s_cto_rearch
6008         IF MSC_ATP_CTO.G_MODEL_IS_PRESENT_IN_SET =1 and NVL(MSC_ATP_PVT.G_SYNC_ATP_CHECK, 'N') = 'N' THEN
6009               MSC_ATP_CTO.Maintain_OS_Sourcing(p_instance_id,
6010                                                x_atp_table,
6011                                                MSC_ATP_CTO.FAIL);
6012         END IF;
6013         --e_cto_rearch
6014         */
6015 
6016     END IF;     -- Bug 1661545
6017 
6018 
6019     WHEN FND_API.G_EXC_ERROR THEN
6020         x_return_status := FND_API.G_RET_STS_ERROR;
6021 
6022         -- Resetting global variables for additional issue encountered while bug 3295831
6023         IF PG_DEBUG in ('Y', 'C') THEN
6024             msc_sch_wb.atp_debug('Schedule: ' || 'Resetting G_INV_CTP and G_ALLOCATED_ATP' );
6025         END IF;
6026         MSC_ATP_PVT.G_INV_CTP := NVL(l_inv_ctp, MSC_ATP_PVT.G_INV_CTP);
6027         -- ATP4drp re-set using original profile value
6028         --MSC_ATP_PVT.G_ALLOCATED_ATP := NVL(l_allocated_atp, MSC_ATP_PVT.G_ALLOCATED_ATP);
6029         MSC_ATP_PVT.G_ALLOCATED_ATP := MSC_ATP_PVT.G_ORIG_ALLOC_ATP;
6030         -- End ATP4drp
6031         MSC_ATP_PVT.G_ALLOCATION_METHOD := NVL(l_allocation_method, MSC_ATP_PVT.G_ALLOCATION_METHOD);
6032         --5221865
6033         MSC_ATP_PVT.G_FORWARD_ATP := NVL(l_forward_atp,MSC_ATP_PVT.G_FORWARD_ATP);
6034 
6035         -- 2625800: krajan
6036         ---agilent arrival set bug
6037         IF l_arrival_count > 1 THEN
6038            IF PG_DEBUG in ('Y', 'C') THEN
6039               msc_sch_wb.atp_debug ('Schedule: ' || 'Inside arrival set error processing');
6040            END IF;
6041            --for arrivalset l_atp_table contains only the record that we are processing
6042            --- so we need to make sure that we add all the records in arrival set to x_atp_tbale
6043            ---first we add the record we have processed so far
6044            MSC_SATP_FUNC.Assign_Atp_Output_Rec(l_temp_arr_atp_table, x_atp_table, l_return_status);
6045            ---now we add the current reocrd
6046            MSC_SATP_FUNC.Assign_Atp_Output_Rec(l_atp_table, x_atp_table, l_return_status);
6047            --now we assign rest of the record.
6048            l_count := l_temp_arr_atp_table.inventory_item_id.count + 2;
6049            IF PG_DEBUG in ('Y', 'C') THEN
6050               msc_sch_wb.atp_debug('Schedule: ' || 'l_count := ' || l_count);
6051            END IF;
6052            FOR i in l_count..p_atp_table.inventory_item_id.count LOOP
6053               MSC_SATP_FUNC.Assign_Atp_Input_Rec(p_atp_table, i, x_atp_table, l_return_status);
6054            END LOOP;
6055         END IF;
6056 
6057         -- Error Handling Changes
6058         IF (x_atp_table.Inventory_item_id.COUNT = 0) THEN
6059                 IF (l_atp_table.Inventory_item_id.COUNT = 0) THEN
6060                         x_atp_table := p_atp_table;
6061                 ELSE
6062                         x_atp_table := l_atp_table;
6063                 END IF;
6064         END IF;
6065         FOR i IN 1..x_atp_table.Action.COUNT LOOP
6066                 --IF x_atp_table.error_code(i) IS NULL OR
6067                  --  x_atp_table.error_code(i) in (0,61,150) THEN
6068                   --      x_atp_table.Error_Code(i) := GROUPEL_ERROR;
6069                 --END IF;
6070                 -- Remove invalid SD recs.
6071                 BEGIN
6072                         SELECT pegging_id INTO l_tmp_pegging_id FROM mrp_atp_details_temp
6073                                 WHERE session_id = MSC_ATP_PVT.G_SESSION_ID
6074                                 AND order_line_id = x_atp_table.identifier(i)
6075                                 AND parent_pegging_id is NULL
6076                                  AND record_type in (3,4);
6077                 EXCEPTION
6078                         WHEN others then
6079                                 l_tmp_pegging_id := -1; --Setting to error
6080                 END;
6081                 IF (NVL(l_tmp_pegging_id,-1) <> -1) THEN
6082                         IF PG_DEBUG in ('Y', 'C') THEN
6083                            msc_sch_wb.atp_debug('Schedule: ' || 'Now deleting- calling remove invalid SD REC');
6084                         END IF;
6085                         MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(l_tmp_pegging_id, null,
6086                                 l_plan_id, UNDO, 1,l_return_status);
6087                 END IF;
6088 
6089         END LOOP;
6090         IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
6091                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME , 'Schedule');
6092         END IF;
6093         -- Error Handling Changes End.
6094 
6095 -- INSERT HERE
6096         -- Error Handling changes - handling for rescheduling case
6097         IF /*(MSC_ATP_PVT.G_INV_CTP = 4) AND */ (x_atp_table.Action(1) = DEMANDMODIFY OR --3720018, commented for support of rescheduling in ODS
6098                 x_atp_table.Action(1) = DEMANDADD OR
6099                 x_atp_table.Action(1) = DMDRSVADD) THEN
6100 
6101                 IF PG_DEBUG in ('Y', 'C') THEN
6102                    msc_sch_wb.atp_debug('Schedule: ' || 'check if need to reset demands when scheduling failed');
6103                 END IF;
6104 
6105                 -- Loop thru the whole set to check if any of the records had failed Scheduling
6106                 -- Update demands to reset the quantity and date in such cases
6107                 -- Not needed if error_code is ALLSUCCESS or ATP_NOT_APPL.
6108 
6109                 --bug 2384224: un delete only those demands which are deleted
6110                 IF PG_DEBUG in ('Y', 'C') THEN
6111                    msc_sch_wb.atp_debug('Schedule: ' || 'before resetting the demands when scheduling failed');
6112                    msc_sch_wb.atp_debug('Schedule: ' || 'single call to Undo_Delete_Row');
6113                END IF;
6114                -- rajjain single call to Undo_Delete_Row procedure
6115                --3720018,changes to refer all the records from l_delete_atp_rec
6116                MSC_ATP_DB_UTILS.Undo_Delete_Row(l_delete_atp_rec.del_identifiers,
6117                     l_delete_atp_rec.del_plan_ids,
6118                     p_instance_id,
6119                     --subst
6120                     l_delete_atp_rec.del_demand_ids,
6121                     l_delete_atp_rec.del_inv_item_ids,
6122                     l_delete_atp_rec.del_copy_demand_ids,        -- For summary enhancement
6123                     l_delete_atp_rec.del_copy_demand_plan_ids,   -- For summary enhancement
6124                     l_time_phased_set,            -- For time_phased_atp
6125                     -- CTO ODR and Simplified Pegging
6126                     l_delete_atp_rec.del_atp_peg_items,
6127                     l_delete_atp_rec.del_atp_peg_demands ,
6128                     l_delete_atp_rec.del_atp_peg_supplies,
6129                     l_delete_atp_rec.del_atp_peg_res_reqs,
6130                     l_delete_atp_rec.del_demand_source_type, --cmro
6131                     -- End CTO ODR and Simplified Pegging
6132                     l_delete_atp_rec.atp_peg_demands_plan_ids,  --Bug 3629191
6133                     l_delete_atp_rec.atp_peg_supplies_plan_ids, --Bug 3629191
6134                     l_delete_atp_rec.atp_peg_res_reqs_plan_ids,  --Bug 3629191
6135                     l_delete_atp_rec.del_ods_demand_ids, --3720018, added for support of rescheduling in ODS
6136                     l_delete_atp_rec.del_ods_inv_item_ids, --3720018, added for support of rescheduling in ODS
6137                     l_delete_atp_rec.del_ods_demand_src_type, --3720018, added for support of rescheduling in ODS
6138                     l_delete_atp_rec.del_ods_cto_demand_ids, --3720018, added for support of rescheduling in ODS
6139                     l_delete_atp_rec.del_ods_cto_inv_item_ids, --3720018, added for support of rescheduling in ODS
6140                     l_delete_atp_rec.del_ods_cto_dem_src_type, --3720018, added for support of rescheduling in ODS
6141                     l_delete_atp_rec.del_ods_atp_refresh_no, --3720018, added for support of rescheduling in ODS
6142                     l_delete_atp_rec.del_ods_cto_atp_refresh_no --3720018, added for support of rescheduling in ODS
6143                     );
6144 
6145                IF PG_DEBUG in ('Y', 'C') THEN
6146                   msc_sch_wb.atp_debug('Schedule: ' || 'after resetting the demands when scheduling failed');
6147                END IF;
6148 
6149                /* --s_cto_rearch
6150                IF MSC_ATP_CTO.G_MODEL_IS_PRESENT_IN_SET =1 and NVL(MSC_ATP_PVT.G_SYNC_ATP_CHECK, 'N') = 'N' THEN
6151                      MSC_ATP_CTO.Maintain_OS_Sourcing(p_instance_id,
6152                                                       x_atp_table,
6153                                                       MSC_ATP_CTO.FAIL);
6154                END IF;
6155                --e_cto_rearch
6156                */
6157 
6158         END IF;
6159 
6160     -- Error Handling Changes
6161     -- New Exception Block
6162     -- Error Handling Insert krajan
6163     WHEN MSC_ATP_PUB.ATP_INVALID_OBJECTS_FOUND THEN
6164 
6165         IF PG_DEBUG in ('Y', 'C') THEN
6166            msc_sch_wb.atp_debug('Schedule: ' || 'error := ' || sqlerrm);
6167            msc_sch_wb.atp_debug('Schedule: Invalid Object Found. In it');
6168         END IF;
6169         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6170 
6171         -- Resetting global variables for additional issue encountered while bug 3295831
6172         IF PG_DEBUG in ('Y', 'C') THEN
6173             msc_sch_wb.atp_debug('Schedule: ' || 'Resetting G_INV_CTP and G_ALLOCATED_ATP' );
6174         END IF;
6175         MSC_ATP_PVT.G_INV_CTP := NVL(l_inv_ctp, MSC_ATP_PVT.G_INV_CTP);
6176         -- ATP4drp re-set using original profile value
6177         --MSC_ATP_PVT.G_ALLOCATED_ATP := NVL(l_allocated_atp, MSC_ATP_PVT.G_ALLOCATED_ATP);
6178         MSC_ATP_PVT.G_ALLOCATED_ATP := MSC_ATP_PVT.G_ORIG_ALLOC_ATP;
6179         -- End ATP4drp
6180         MSC_ATP_PVT.G_ALLOCATION_METHOD := NVL(l_allocation_method, MSC_ATP_PVT.G_ALLOCATION_METHOD);
6181         --5221865
6182         MSC_ATP_PVT.G_FORWARD_ATP := NVL(l_forward_atp,MSC_ATP_PVT.G_FORWARD_ATP);
6183 
6184         -- 2625800: krajan
6185         ---agilent arrival set bug
6186         IF l_arrival_count > 1 THEN
6187            IF PG_DEBUG in ('Y', 'C') THEN
6188               msc_sch_wb.atp_debug ('Schedule: ' || 'Inside arrival set error processing');
6189            END IF;
6190            --for arrivalset l_atp_table contains only the record that we are processing
6191            --- so we need to make sure that we add all the records in arrival set to x_atp_tbale
6192            ---first we add the record we have processed so far
6193            MSC_SATP_FUNC.Assign_Atp_Output_Rec(l_temp_arr_atp_table, x_atp_table, l_return_status);
6194            ---now we add the current reocrd
6195            MSC_SATP_FUNC.Assign_Atp_Output_Rec(l_atp_table, x_atp_table, l_return_status);
6196            --now we assign rest of the record.
6197            l_count := l_temp_arr_atp_table.inventory_item_id.count + 2;
6198            IF PG_DEBUG in ('Y', 'C') THEN
6199               msc_sch_wb.atp_debug('Schedule: ' || 'l_count := ' || l_count);
6200            END IF;
6201            FOR i in l_count..p_atp_table.inventory_item_id.count LOOP
6202               MSC_SATP_FUNC.Assign_Atp_Input_Rec(p_atp_table, i, x_atp_table, l_return_status);
6203            END LOOP;
6204         END IF;
6205 
6206         IF (x_atp_table.Inventory_item_id.COUNT = 0) THEN
6207                 IF (l_atp_table.Inventory_item_id.COUNT = 0) THEN
6208                         x_atp_table := p_atp_table;
6209                 ELSE
6210                         x_atp_table := l_atp_table;
6211                 END IF;
6212         END IF;
6213         FOR i IN 1..x_atp_table.Action.COUNT LOOP
6214                 IF ((NVL(x_atp_table.Error_code(i),-1)) in (-1,0,61,150)) THEN
6215                         x_atp_table.Error_Code(i) := ATP_INVALID_OBJECTS; -- NO OBJECTS FOUND
6216                 END IF;
6217                  BEGIN
6218                         SELECT pegging_id INTO l_tmp_pegging_id FROM mrp_atp_details_temp
6219                                 WHERE session_id = MSC_ATP_PVT.G_SESSION_ID
6220                                 AND order_line_id = x_atp_table.identifier(i)
6221                                 AND parent_pegging_id is NULL
6222                                  AND record_type in (3,4);
6223                 EXCEPTION
6224                         WHEN others then
6225                                 IF PG_DEBUG in ('Y', 'C') THEN
6226                                    msc_sch_wb.atp_debug('Schedule: ' || 'Select EXCEPTION KR :invalid obj');
6227                                    msc_sch_wb.atp_debug('Schedule: ' || 'error := ' || sqlerrm);
6228                                 END IF;
6229                                 l_tmp_pegging_id := -1; --Setting to error
6230                 END;
6231                 IF PG_DEBUG in ('Y', 'C') THEN
6232                    msc_sch_wb.atp_debug('Schedule: ' || 'l_tmp_pegging_id is ' || l_tmp_pegging_id);
6233                 END IF;
6234                 IF (NVL(l_tmp_pegging_id, -1) <> -1) THEN
6235                         IF PG_DEBUG in ('Y', 'C') THEN
6236                            msc_sch_wb.atp_debug('Schedule: ' || 'Now deleting- calling remove invalid SD REC');
6237                         END IF;
6238                         MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(l_tmp_pegging_id, null,
6239                                 l_plan_id, UNDO, 1,l_return_status);
6240                 END IF;
6241         END LOOP;
6242 
6243         IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
6244             FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME , 'Schedule');
6245         END IF;
6246 
6247         -- Error Handling changes - handling for rescheduling case
6248         IF /*(MSC_ATP_PVT.G_INV_CTP = 4) AND */ (x_atp_table.Action(1) = DEMANDMODIFY OR --3720018, commented for support of rescheduling in ODS
6249                 x_atp_table.Action(1) = DEMANDADD OR
6250                 x_atp_table.Action(1) = DMDRSVADD) THEN
6251 
6252                 IF PG_DEBUG in ('Y', 'C') THEN
6253                    msc_sch_wb.atp_debug('Schedule: ' || 'check if need to reset demands when scheduling failed');
6254                 END IF;
6255 
6256                 -- Loop thru the whole set to check if any of the records had failed Scheduling
6257                 -- Update demands to reset the quantity and date in such cases
6258                 -- Not needed if error_code is ALLSUCCESS or ATP_NOT_APPL.
6259 
6260                 --bug 2384224: un delete only those demands which are deleted
6261                 IF PG_DEBUG in ('Y', 'C') THEN
6262                    msc_sch_wb.atp_debug('Schedule: ' || 'before resetting the demands when scheduling failed');
6263                	   msc_sch_wb.atp_debug('Schedule: ' || 'rajjain - Single call to Undo_Delete_Row');
6264                 END IF;
6265                 -- rajjain single call to Undo_Delete_Row procedure
6266                 --3720018,changes to refer all the records from l_delete_atp_rec
6267                 MSC_ATP_DB_UTILS.Undo_Delete_Row(l_delete_atp_rec.del_identifiers,
6268                     l_delete_atp_rec.del_plan_ids,
6269                     p_instance_id,
6270                     --subst
6271                     l_delete_atp_rec.del_demand_ids,
6272                     l_delete_atp_rec.del_inv_item_ids,
6273                     l_delete_atp_rec.del_copy_demand_ids,        -- For summary enhancement
6274                     l_delete_atp_rec.del_copy_demand_plan_ids,   -- For summary enhancement
6275                     l_time_phased_set,            -- For time_phased_atp
6276                     -- CTO ODR and Simplified Pegging
6277                     l_delete_atp_rec.del_atp_peg_items,
6278                     l_delete_atp_rec.del_atp_peg_demands ,
6279                     l_delete_atp_rec.del_atp_peg_supplies,
6280                     l_delete_atp_rec.del_atp_peg_res_reqs,
6281                     l_delete_atp_rec.del_demand_source_type, --cmro
6282                     -- End CTO ODR and Simplified Pegging
6283                     l_delete_atp_rec.atp_peg_demands_plan_ids,  --Bug 3629191
6284                     l_delete_atp_rec.atp_peg_supplies_plan_ids, --Bug 3629191
6285                     l_delete_atp_rec.atp_peg_res_reqs_plan_ids,  --Bug 3629191
6286                     l_delete_atp_rec.del_ods_demand_ids, --3720018, added for support of rescheduling in ODS
6287                     l_delete_atp_rec.del_ods_inv_item_ids, --3720018, added for support of rescheduling in ODS
6288                     l_delete_atp_rec.del_ods_demand_src_type, --3720018, added for support of rescheduling in ODS
6289                     l_delete_atp_rec.del_ods_cto_demand_ids, --3720018, added for support of rescheduling in ODS
6290                     l_delete_atp_rec.del_ods_cto_inv_item_ids, --3720018, added for support of rescheduling in ODS
6291                     l_delete_atp_rec.del_ods_cto_dem_src_type, --3720018, added for support of rescheduling in ODS
6292                     l_delete_atp_rec.del_ods_atp_refresh_no, --3720018, added for support of rescheduling in ODS
6293                     l_delete_atp_rec.del_ods_cto_atp_refresh_no --3720018, added for support of rescheduling in ODS
6294                     );
6295 
6296                 IF PG_DEBUG in ('Y', 'C') THEN
6297                    msc_sch_wb.atp_debug('Schedule: ' || 'after resetting the demands when scheduling failed');
6298                 END IF;
6299 
6300                 /*--s_cto_rearch
6301                 IF MSC_ATP_CTO.G_MODEL_IS_PRESENT_IN_SET =1 and NVL(MSC_ATP_PVT.G_SYNC_ATP_CHECK, 'N') = 'N' THEN
6302                       MSC_ATP_CTO.Maintain_OS_Sourcing(p_instance_id,
6303                                                        x_atp_table,
6304                                                        MSC_ATP_CTO.FAIL);
6305                 END IF;
6306                 --e_cto_rearch
6307                 */
6308         END IF;
6309         IF PG_DEBUG in ('Y', 'C') THEN
6310            msc_sch_wb.atp_debug('Schedule: Invalid object encountered in schedule');
6311         END IF;
6312 
6313     -- Added to handle calendar exceptions : ship_rec_cal
6314     WHEN NO_MATCHING_DATE_IN_CAL THEN
6315         IF PG_DEBUG in ('Y', 'C') THEN
6316            msc_sch_wb.atp_debug('Schedule: ' || 'error := ' || sqlerrm);
6317            msc_sch_wb.atp_debug('Schedule: ' || 'NO_MATCHING_DATE_IN_CAL');
6318         END IF;
6319         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6320 
6321         -- Resetting global variables for additional issue encountered while bug 3295831
6322         IF PG_DEBUG in ('Y', 'C') THEN
6323             msc_sch_wb.atp_debug('Schedule: ' || 'Resetting G_INV_CTP and G_ALLOCATED_ATP' );
6324         END IF;
6325         MSC_ATP_PVT.G_INV_CTP := NVL(l_inv_ctp, MSC_ATP_PVT.G_INV_CTP);
6326         -- ATP4drp re-set using original profile value
6327         --MSC_ATP_PVT.G_ALLOCATED_ATP := NVL(l_allocated_atp, MSC_ATP_PVT.G_ALLOCATED_ATP);
6328         MSC_ATP_PVT.G_ALLOCATED_ATP := MSC_ATP_PVT.G_ORIG_ALLOC_ATP;
6329         -- End ATP4drp
6330         MSC_ATP_PVT.G_ALLOCATION_METHOD := NVL(l_allocation_method, MSC_ATP_PVT.G_ALLOCATION_METHOD);
6331         --5221865
6332         MSC_ATP_PVT.G_FORWARD_ATP := NVL(l_forward_atp,MSC_ATP_PVT.G_FORWARD_ATP);
6333 
6334         IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
6335             FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME , 'Schedule');
6336         END IF;
6337 
6338         -- 2625800: krajan
6339         ---agilent arrival set bug
6340         IF l_arrival_count > 1 THEN
6341            IF PG_DEBUG in ('Y', 'C') THEN
6342               msc_sch_wb.atp_debug ('Schedule: ' || 'Inside arrival set error processing');
6343            END IF;
6344            --for arrivalset l_atp_table contains only the record that we are processing
6345            --- so we need to make sure that we add all the records in arrival set to x_atp_tbale
6346            ---first we add the record we have processed so far
6347            MSC_SATP_FUNC.Assign_Atp_Output_Rec(l_temp_arr_atp_table, x_atp_table, l_return_status);
6348            ---now we add the current reocrd
6349            MSC_SATP_FUNC.Assign_Atp_Output_Rec(l_atp_table, x_atp_table, l_return_status);
6350            --now we assign rest of the record.
6351            l_count := l_temp_arr_atp_table.inventory_item_id.count + 2;
6352            IF PG_DEBUG in ('Y', 'C') THEN
6353               msc_sch_wb.atp_debug('Schedule: ' || 'l_count := ' || l_count);
6354            END IF;
6355            FOR i in l_count..p_atp_table.inventory_item_id.count LOOP
6356               MSC_SATP_FUNC.Assign_Atp_Input_Rec(p_atp_table, i, x_atp_table, l_return_status);
6357            END LOOP;
6358         END IF;
6359 
6360         -- Error Handling krajan
6361         IF (x_atp_table.Inventory_item_id.COUNT = 0) THEN
6362                 IF (l_atp_table.Inventory_item_id.COUNT = 0) THEN
6363                         x_atp_table := p_atp_table;
6364                 ELSE
6365                         x_atp_table := l_atp_table;
6366                 END IF;
6367         END IF;
6368         FOR i IN 1..x_atp_table.Action.COUNT LOOP
6369                 IF x_atp_table.error_code(i) IS NULL OR
6370                    x_atp_table.error_code(i) in (0,61,150) THEN
6371                         x_atp_table.Error_Code(i) := NO_MATCHING_CAL_DATE;
6372                 END IF;
6373                 BEGIN
6374                         SELECT pegging_id INTO l_tmp_pegging_id FROM mrp_atp_details_temp
6375                                 WHERE session_id = MSC_ATP_PVT.G_SESSION_ID
6376                                 AND order_line_id = x_atp_table.identifier(i)
6377                                 AND parent_pegging_id is NULL
6378                                  AND record_type in (3,4);
6379                 EXCEPTION
6380                         WHEN others then
6381                                 IF PG_DEBUG in ('Y', 'C') THEN
6382                                    msc_sch_wb.atp_debug('Schedule: ' || 'NO_MATCHING_DATE_IN_CAL');
6383                                    msc_sch_wb.atp_debug('Schedule: ' || 'error := ' || sqlerrm);
6384                                 END IF;
6385                                 l_tmp_pegging_id := -1; --Setting to error
6386                 END;
6387                 IF PG_DEBUG in ('Y', 'C') THEN
6388                    msc_sch_wb.atp_debug('Schedule: ' || 'l_tmp_pegging_id is ' || l_tmp_pegging_id);
6389                 END IF;
6390                 IF (NVL(l_tmp_pegging_id,-1) <> -1) THEN
6391                         IF PG_DEBUG in ('Y', 'C') THEN
6392                            msc_sch_wb.atp_debug('Schedule: ' || 'Now deleting- calling remove invalid SD REC');
6393                         END IF;
6394                         MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(l_tmp_pegging_id, null,
6395                                 l_plan_id, UNDO, 1,l_return_status);
6396                 END IF;
6397         END LOOP;
6398         IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
6399                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME , 'Schedule');
6400         END IF;
6401         -- Error Handling Changes End.
6402         IF /*(MSC_ATP_PVT.G_INV_CTP = 4) AND */ (x_atp_table.Action(1) = DEMANDMODIFY OR  --3720018, commented for support of rescheduling in ODS
6403                 x_atp_table.Action(1) = DEMANDADD OR
6404                 x_atp_table.Action(1) = DMDRSVADD) THEN
6405 
6406                 IF PG_DEBUG in ('Y', 'C') THEN
6407                    msc_sch_wb.atp_debug('Schedule: ' || 'check if need to reset demands when scheduling failed');
6408                 END IF;
6409 
6410                 -- Loop thru the whole set to check if any of the records had failed Scheduling
6411                 -- Update demands to reset the quantity and date in such cases
6412                 -- Not needed if error_code is ALLSUCCESS or ATP_NOT_APPL.
6413 
6414                --bug 2384224: un delete only those demands which are deleted
6415                IF PG_DEBUG in ('Y', 'C') THEN
6416                   msc_sch_wb.atp_debug('Schedule: ' || 'before resetting the demands when scheduling failed');
6417                   msc_sch_wb.atp_debug('Schedule: ' || 'rajjain - Single call to Undo_Delete_Row');
6418                END IF;
6419                -- rajjain single call to Undo_Delete_Row procedure
6420                --3720018,changes to refer all the records from l_delete_atp_rec
6421                MSC_ATP_DB_UTILS.Undo_Delete_Row(l_delete_atp_rec.del_identifiers,
6422                     l_delete_atp_rec.del_plan_ids,
6423                     p_instance_id,
6424                     --subst
6425                     l_delete_atp_rec.del_demand_ids,
6426                     l_delete_atp_rec.del_inv_item_ids,
6427                     l_delete_atp_rec.del_copy_demand_ids,        -- For summary enhancement
6428                     l_delete_atp_rec.del_copy_demand_plan_ids,   -- For summary enhancement
6429                     l_time_phased_set,            -- For time_phased_atp
6430                     -- CTO ODR and Simplified Pegging
6431                     l_delete_atp_rec.del_atp_peg_items,
6432                     l_delete_atp_rec.del_atp_peg_demands ,
6433                     l_delete_atp_rec.del_atp_peg_supplies,
6434                     l_delete_atp_rec.del_atp_peg_res_reqs,
6435                     l_delete_atp_rec.del_demand_source_type, --cmro
6436                     -- End CTO ODR and Simplified Pegging
6437                     l_delete_atp_rec.atp_peg_demands_plan_ids,  --Bug 3629191
6438                     l_delete_atp_rec.atp_peg_supplies_plan_ids, --Bug 3629191
6439                     l_delete_atp_rec.atp_peg_res_reqs_plan_ids,  --Bug 3629191
6440                     l_delete_atp_rec.del_ods_demand_ids, --3720018, added for support of rescheduling in ODS
6441                     l_delete_atp_rec.del_ods_inv_item_ids, --3720018, added for support of rescheduling in ODS
6442                     l_delete_atp_rec.del_ods_demand_src_type, --3720018, added for support of rescheduling in ODS
6443                     l_delete_atp_rec.del_ods_cto_demand_ids, --3720018, added for support of rescheduling in ODS
6444                     l_delete_atp_rec.del_ods_cto_inv_item_ids, --3720018, added for support of rescheduling in ODS
6445                     l_delete_atp_rec.del_ods_cto_dem_src_type, --3720018, added for support of rescheduling in ODS
6446                     l_delete_atp_rec.del_ods_atp_refresh_no, --3720018, added for support of rescheduling in ODS
6447                     l_delete_atp_rec.del_ods_cto_atp_refresh_no --3720018, added for support of rescheduling in ODS
6448                     );
6449 
6450                IF PG_DEBUG in ('Y', 'C') THEN
6451                   msc_sch_wb.atp_debug('Schedule: ' || 'after resetting the demands when scheduling failed');
6452                END IF;
6453 
6454                /* --s_cto_rearch
6455                IF MSC_ATP_CTO.G_MODEL_IS_PRESENT_IN_SET =1 and NVL(MSC_ATP_PVT.G_SYNC_ATP_CHECK, 'N') = 'N' THEN
6456                      MSC_ATP_CTO.Maintain_OS_Sourcing(p_instance_id,
6457                                                       x_atp_table,
6458                                                       MSC_ATP_CTO.FAIL);
6459                END IF;
6460                --e_cto_rearch
6461                */
6462 
6463         END IF;
6464 
6465     WHEN OTHERS THEN
6466         IF PG_DEBUG in ('Y', 'C') THEN
6467            msc_sch_wb.atp_debug('Schedule: ' || 'error := ' || sqlerrm);
6468         END IF;
6469         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6470 
6471         -- Resetting global variables for additional issue encountered while bug 3295831
6472         IF PG_DEBUG in ('Y', 'C') THEN
6473             msc_sch_wb.atp_debug('Schedule: ' || 'Resetting G_INV_CTP and G_ALLOCATED_ATP' );
6474         END IF;
6475         MSC_ATP_PVT.G_INV_CTP := NVL(l_inv_ctp, MSC_ATP_PVT.G_INV_CTP);
6476         -- ATP4drp re-set using original profile value
6477         --MSC_ATP_PVT.G_ALLOCATED_ATP := NVL(l_allocated_atp, MSC_ATP_PVT.G_ALLOCATED_ATP);
6478         MSC_ATP_PVT.G_ALLOCATED_ATP := MSC_ATP_PVT.G_ORIG_ALLOC_ATP;
6479         -- End ATP4drp
6480         MSC_ATP_PVT.G_ALLOCATION_METHOD := NVL(l_allocation_method, MSC_ATP_PVT.G_ALLOCATION_METHOD);
6481         --5221865
6482         MSC_ATP_PVT.G_FORWARD_ATP := NVL(l_forward_atp,MSC_ATP_PVT.G_FORWARD_ATP);
6483 
6484         /* Check if this is actually coming from a calendar routine*/
6485         l_encoded_text := fnd_message.GET_ENCODED;
6486         IF l_encoded_text IS NULL THEN
6487                 l_msg_app := NULL;
6488                 l_msg_name := NULL;
6489         ELSE
6490                 fnd_message.parse_encoded(l_encoded_text, l_msg_app, l_msg_name);
6491                 IF PG_DEBUG in ('Y', 'C') THEN
6492                    msc_sch_wb.atp_debug('Schedule: ' || 'l_msg_name := ' || l_msg_name);
6493                 END IF;
6494         END IF;
6495         IF l_msg_app='MRP' AND l_msg_name='GEN-DATE OUT OF BOUNDS' THEN
6496                 l_error_code := NO_MATCHING_CAL_DATE;
6497            IF PG_DEBUG in ('Y', 'C') THEN --bug3583705
6498                    msc_sch_wb.atp_debug('Schedule: ' || 'NO_MATCHING_CAL_DATE ' );
6499            END IF;
6500         ELSE
6501                 l_error_code := ATP_PROCESSING_ERROR;
6502            IF PG_DEBUG in ('Y', 'C') THEN --bug3583705
6503                    msc_sch_wb.atp_debug('Schedule: ' || 'ATP_PROCESSING_ERROR ' );
6504            END IF;
6505         END IF;
6506 
6507         IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
6508             FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME , 'Schedule');
6509         END IF;
6510 
6511         -- 2625800: krajan
6512         ---agilent arrival set bug
6513         IF l_arrival_count > 1 THEN
6514            IF PG_DEBUG in ('Y', 'C') THEN
6515               msc_sch_wb.atp_debug ('Schedule: ' || 'Inside arrival set error processing');
6516            END IF;
6517            --for arrivalset l_atp_table contains only the record that we are processing
6518            --- so we need to make sure that we add all the records in arrival set to x_atp_tbale
6519            ---first we add the record we have processed so far
6520            MSC_SATP_FUNC.Assign_Atp_Output_Rec(l_temp_arr_atp_table, x_atp_table, l_return_status);
6521            ---now we add the current reocrd
6522            MSC_SATP_FUNC.Assign_Atp_Output_Rec(l_atp_table, x_atp_table, l_return_status);
6523            --now we assign rest of the record.
6524            l_count := l_temp_arr_atp_table.inventory_item_id.count + 2;
6525            IF PG_DEBUG in ('Y', 'C') THEN
6526               msc_sch_wb.atp_debug('Schedule: ' || 'l_count := ' || l_count);
6527            END IF;
6528            FOR i in l_count..p_atp_table.inventory_item_id.count LOOP
6529               MSC_SATP_FUNC.Assign_Atp_Input_Rec(p_atp_table, i, x_atp_table, l_return_status);
6530            END LOOP;
6531         END IF;
6532 
6533         -- Error Handling krajan
6534         IF (x_atp_table.Inventory_item_id.COUNT = 0) THEN
6535                 IF (l_atp_table.Inventory_item_id.COUNT = 0) THEN
6536                         x_atp_table := p_atp_table;
6537                 ELSE
6538                         x_atp_table := l_atp_table;
6539                 END IF;
6540         END IF;
6541 
6542         FOR i IN 1..x_atp_table.Action.COUNT LOOP
6543                 IF x_atp_table.error_code(i) IS NULL OR
6544                    x_atp_table.error_code(i) in (0,61,150) THEN
6545                         x_atp_table.Error_Code(i) := l_error_code;
6546                 END IF;
6547                 BEGIN
6548                         SELECT pegging_id INTO l_tmp_pegging_id FROM mrp_atp_details_temp
6549                                 WHERE session_id = MSC_ATP_PVT.G_SESSION_ID
6550                                 AND order_line_id = x_atp_table.identifier(i)
6551                                 AND parent_pegging_id is NULL
6552                                  AND record_type in (3,4);
6553                 EXCEPTION
6554                         WHEN others then
6555                                 IF PG_DEBUG in ('Y', 'C') THEN
6556                                    msc_sch_wb.atp_debug('Schedule: ' || 'Select EXCEPTION KR: others ');
6557                                    msc_sch_wb.atp_debug('Schedule: ' || 'error := ' || sqlerrm);
6558                                 END IF;
6559                                 l_tmp_pegging_id := -1; --Setting to error
6560                 END;
6561                 IF PG_DEBUG in ('Y', 'C') THEN
6562                    msc_sch_wb.atp_debug('Schedule: ' || 'l_tmp_pegging_id is ' || l_tmp_pegging_id);
6563                 END IF;
6564                 IF (NVL(l_tmp_pegging_id,-1) <> -1) THEN
6565                         IF PG_DEBUG in ('Y', 'C') THEN
6566                            msc_sch_wb.atp_debug('Schedule: ' || 'Now deleting- calling remove invalid SD REC');
6567                         END IF;
6568                         MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(l_tmp_pegging_id, null,
6569                                 l_plan_id, UNDO, 1,l_return_status);
6570                 END IF;
6571         END LOOP;
6572         IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
6573                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME , 'Schedule');
6574         END IF;
6575         -- Error Handling Changes End.
6576         IF /*(MSC_ATP_PVT.G_INV_CTP = 4) AND */ (x_atp_table.Action(1) = DEMANDMODIFY OR --3720018, commented for support of rescheduling in ODS
6577                 x_atp_table.Action(1) = DEMANDADD OR
6578                 x_atp_table.Action(1) = DMDRSVADD) THEN
6579 
6580                 IF PG_DEBUG in ('Y', 'C') THEN
6581                    msc_sch_wb.atp_debug('Schedule: ' || 'check if need to reset demands when scheduling failed');
6582                 END IF;
6583 
6584                 -- Loop thru the whole set to check if any of the records had failed Scheduling
6585                 -- Update demands to reset the quantity and date in such cases
6586                 -- Not needed if error_code is ALLSUCCESS or ATP_NOT_APPL.
6587 
6588                --bug 2384224: un delete only those demands which are deleted
6589                IF PG_DEBUG in ('Y', 'C') THEN
6590                   msc_sch_wb.atp_debug('Schedule: ' || 'before resetting the demands when scheduling failed');
6591                   msc_sch_wb.atp_debug('Schedule: ' || 'rajjain - Single call to Undo_Delete_Row');
6592                END IF;
6593                -- rajjain single call to Undo_Delete_Row procedure
6594                --3720018,changes to refer all the records from l_delete_atp_rec
6595                MSC_ATP_DB_UTILS.Undo_Delete_Row(l_delete_atp_rec.del_identifiers,
6596                     l_delete_atp_rec.del_plan_ids,
6597                     p_instance_id,
6598                     --subst
6599                     l_delete_atp_rec.del_demand_ids,
6600                     l_delete_atp_rec.del_inv_item_ids,
6601                     l_delete_atp_rec.del_copy_demand_ids,        -- For summary enhancement
6602                     l_delete_atp_rec.del_copy_demand_plan_ids,   -- For summary enhancement
6603                     l_time_phased_set,            -- For time_phased_atp
6604                     -- CTO ODR and Simplified Pegging
6605                     l_delete_atp_rec.del_atp_peg_items,
6606                     l_delete_atp_rec.del_atp_peg_demands ,
6607                     l_delete_atp_rec.del_atp_peg_supplies,
6608                     l_delete_atp_rec.del_atp_peg_res_reqs,
6609                     l_delete_atp_rec.del_demand_source_type, --cmro
6610                     -- End CTO ODR and Simplified Pegging
6611                     l_delete_atp_rec.atp_peg_demands_plan_ids,  --Bug 3629191
6612                     l_delete_atp_rec.atp_peg_supplies_plan_ids, --Bug 3629191
6613                     l_delete_atp_rec.atp_peg_res_reqs_plan_ids,  --Bug 3629191
6614                     l_delete_atp_rec.del_ods_demand_ids, --3720018, added for support of rescheduling in ODS
6615                     l_delete_atp_rec.del_ods_inv_item_ids, --3720018, added for support of rescheduling in ODS
6616                     l_delete_atp_rec.del_ods_demand_src_type, --3720018, added for support of rescheduling in ODS
6617                     l_delete_atp_rec.del_ods_cto_demand_ids, --3720018, added for support of rescheduling in ODS
6618                     l_delete_atp_rec.del_ods_cto_inv_item_ids, --3720018, added for support of rescheduling in ODS
6619                     l_delete_atp_rec.del_ods_cto_dem_src_type, --3720018, added for support of rescheduling in ODS
6620                     l_delete_atp_rec.del_ods_atp_refresh_no, --3720018, added for support of rescheduling in ODS
6621                     l_delete_atp_rec.del_ods_cto_atp_refresh_no --3720018, added for support of rescheduling in ODS
6622                     );
6623 
6624                IF PG_DEBUG in ('Y', 'C') THEN
6625                   msc_sch_wb.atp_debug('Schedule: ' || 'after resetting the demands when scheduling failed');
6626                END IF;
6627 
6628                /* --s_cto_rearch
6629                IF MSC_ATP_CTO.G_MODEL_IS_PRESENT_IN_SET =1 and NVL(MSC_ATP_PVT.G_SYNC_ATP_CHECK, 'N') = 'N' THEN
6630                      MSC_ATP_CTO.Maintain_OS_Sourcing(p_instance_id,
6631                                                       x_atp_table,
6632                                                       MSC_ATP_CTO.FAIL);
6633                END IF;
6634                --e_cto_rearch
6635                */
6636 
6637         END IF;
6638 
6639         -- Commented on 9/13/2001 by ngoel as this was causing subscript beyond count
6640 	-- since exception wasn't trapped until Call_ATP_No_Commit.
6641 --        RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6642 END Schedule;
6643 
6644 
6645 PROCEDURE ATP_Check (p_atp_record     		IN OUT  NoCopy MRP_ATP_PVT.AtpRec,
6646                      p_plan_id             	IN   NUMBER,
6647                      p_level               	IN   NUMBER,
6648                      p_scenario_id         	IN   NUMBER,
6649                      p_search              	IN   NUMBER,
6650                      p_refresh_number      	IN   NUMBER,
6651                      p_parent_pegging_id   	IN   NUMBER,
6652                      p_assign_set_id       	IN   NUMBER,
6653                      x_atp_period          	OUT NOCOPY MRP_ATP_PUB.ATP_Period_Typ,
6654                      x_atp_supply_demand   	OUT NOCOPY MRP_ATP_PUB.ATP_Supply_Demand_Typ,
6655                      x_return_status       	OUT  NoCopy VARCHAR2,
6656                      p_pre_processing_lead_time   IN NUMBER :=0
6657 )
6658 IS
6659 
6660 l_delivery_lead_time 		NUMBER;
6661 l_atp_flag 			VARCHAR2(1);
6662 l_atp_comp_flag			VARCHAR2(1);
6663 l_return_status 		VARCHAR2(10);
6664 l_requested_date_quantity	NUMBER;
6665 l_requested_quantity		NUMBER; -- 2621270
6666 l_sub_requested_date_quantity   NUMBER := 0.0;
6667 l_stealing_requested_date_qty   NUMBER := 0.0;
6668 l_stealing_qty                  NUMBER := 0.0;
6669 l_from_location_id		NUMBER;
6670 l_requested_ship_date		DATE;
6671 --Added by avjain for atp impact due to Plan by request date
6672 l_original_req_ship_date	DATE;   -- when item was requested to be shipped
6673 l_ship_method			VARCHAR2(30);
6674 l_to_location_id		NUMBER;
6675 l_atp_date_this_level		DATE;
6676 l_atp_date_quantity_this_level	NUMBER;
6677 l_sources			MRP_ATP_PVT.Atp_Source_Typ;
6678 i                               PLS_INTEGER;
6679 j				PLS_INTEGER;
6680 l_atp_rec			MRP_ATP_PVT.AtpRec;
6681 l_net_demand			NUMBER := 0; --5478124
6682 l_sched_status			BOOLEAN;
6683 l_atp_insert_rec		MRP_ATP_PVT.AtpRec;
6684 l_atp_period                    MRP_ATP_PUB.ATP_Period_Typ;
6685 l_atp_period_first              MRP_ATP_PUB.ATP_Period_Typ;
6686 l_atp_supply_demand             MRP_ATP_PUB.ATP_Supply_Demand_Typ;
6687 l_res_atp_period                MRP_ATP_PUB.ATP_Period_Typ;
6688 l_res_atp_supply_demand         MRP_ATP_PUB.ATP_Supply_Demand_Typ;
6689 l_comp_atp_period               MRP_ATP_PUB.ATP_Period_Typ;
6690 l_comp_atp_supply_demand        MRP_ATP_PUB.ATP_Supply_Demand_Typ;
6691 l_tb_atp_period                 MRP_ATP_PUB.ATP_Period_Typ;
6692 l_tb_atp_supply_demand          MRP_ATP_PUB.ATP_Supply_Demand_Typ;
6693 l_future_atp_period             MRP_ATP_PUB.ATP_Period_Typ;
6694 l_future_atp_supply_demand      MRP_ATP_PUB.ATP_Supply_Demand_Typ;
6695 
6696 l_inv_item_id                   NUMBER;
6697 
6698 temp				NUMBER;
6699 g_atp_record                    MRP_ATP_PVT.AtpRec;
6700 l_pegging_rec			mrp_atp_details_temp%ROWTYPE;
6701 l_pegging_id                    NUMBER;
6702 l_parent_pegging_id             NUMBER;
6703 l_atp_pegging_id                NUMBER;
6704 l_future_pegging_id             NUMBER;
6705 l_demand_id                     NUMBER;
6706 l_quantity_ordered		NUMBER;
6707 l_quantity_uom			VARCHAR2(3);
6708 l_transaction_id                NUMBER;
6709 l_avail_assembly_qty		NUMBER;
6710 l_comp_atp_date			DATE;
6711 l_res_atp_date			DATE;
6712 l_conversion_rate               NUMBER;
6713 l_sysdate                       DATE;
6714 l_ship_date                     DATE;
6715 l_plan_id                       NUMBER;
6716 l_assign_set_id                 NUMBER;
6717 l_peg_qty                       NUMBER;
6718 l_inv_item_name                 VARCHAR2(250); --bug 2246200
6719 l_org_code                      VARCHAR2(7);
6720 l_supplier_name                 VARCHAR2(80);
6721 l_supplier_site_name            VARCHAR2(80);
6722 l_temp_date                     DATE;   -- Bug 1449555
6723 l_cto_bom                       NUMBER := 0;
6724 
6725 l_coproducts_flag 		VARCHAR2(1) := 'Y'; -- default to Yes
6726 l_make_buy_cd			NUMBER;
6727 l_processing_lead_time 		NUMBER;
6728 l_pre_processing_date		DATE;
6729 l_mso_lead_time_factor		NUMBER;
6730 l_demand_class_flag             NUMBER :=0;
6731 l_inventory_item_id             number;
6732 l_coprod_pegging_id             NUMBER;
6733 temp_sd_qty 			NUMBER;
6734 l_summary_flag			VARCHAR2(1);
6735 l_old_summary_flag              VARCHAR2(1);
6736 l_new_summary_flag              VARCHAR2(1);
6737 l_site_id			NUMBER;
6738 l_request_item_id               NUMBER;
6739 l_pre_pro_lt                    NUMBER;
6740 l_fixed_lt			NUMBER;
6741 l_variable_lt			NUMBER;
6742 l_ptf_flag			NUMBER;
6743 l_ptf_date                      DATE;
6744 l_net_processing_lt             NUMBER;
6745 l_process_lt                    NUMBER;
6746 l_order_date                    DATE;
6747 l_start_date                    DATE;
6748 l_post_pro_lt                   NUMBER;
6749 l_dock_date			DATE;
6750 l_atp_request_date              DATE;
6751 l_atp_ship_date                 DATE;
6752 l_parent_ptf_date               DATE;
6753 l_ptf_date_for_child            DATE;   -- Bug 3782472
6754 l_req_date_qty                  NUMBER;
6755 l_item_attribute_rec            MSC_ATP_PVT.item_attribute_rec;   -- Obsoleted
6756 l_substitution_window           NUMBER;
6757 l_atp_pegging_tab		MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr();
6758 -- Only specific to this procedure
6759 PF_ITEM_INFO_REC                 MSC_ATP_PVT.item_attribute_rec; -- Only for PF info
6760 C_ITEM_INFO_REC                  MSC_ATP_PVT.item_attribute_rec;  -- Copy of the global.
6761 -- Added for bug 2392456
6762 l_plan_info_rec                 MSC_ATP_PVT.plan_info_rec;
6763 
6764 --diag_atp
6765 L_CONSTRAINT_FLAG               NUMBER;
6766 L_PTF_DUE_DATE                  DATE;
6767 L_LT_DUE_DATE                   DATE;
6768 L_RES_AVAIL_QTY                 NUMBER;
6769 L_SUPPLIER_START_DATE           DATE;
6770 L_ROUNDING_CONTROL_FLAG         number;
6771 L_UNIT_WEIGHT                   number;
6772 L_WEIGHT_UOM                    varchar2(3);
6773 L_UNIT_VOLUME                   number;
6774 L_VOLUME_UOM                    varchar2(3);
6775 L_DEST_INV_ITEM_ID              number;
6776 L_GET_MAT_IN_REC                MSC_ATP_REQ.get_mat_in_rec;
6777 l_get_mat_out_rec               MSC_ATP_REQ.get_mat_out_rec;
6778 l_allocation_rule_name          varchar2(30);
6779 
6780 l_update_PO_mode		NUMBER := MSC_ATP_PVT.UNDO;
6781 l_atp_quantity_this_level_fwd	NUMBER;
6782 l_last_good_pegging_id		NUMBER;
6783 l_have_make_buy_parent		NUMBER := G_HAVE_MAKE_BUY_PARENT;
6784 l_subst_existing_PO_qty		NUMBER;
6785 
6786 --- Enhance CTO Phase 1 Req #17
6787 -- New variable to deal with forward stealing for CTO components.
6788 l_parent_peg_forward            NUMBER := 0;
6789 -- End Enhance CTO Phase 1 Req #17
6790 
6791 -- for nocopy changes
6792 l_res_demand			NUMBER;
6793 --  Supplier Capacity and Lead Time (SCLT) changes
6794 l_sup_cap_cum_start            NUMBER;
6795 
6796 -- dsting 2764213
6797 l_planned_sources             NUMBER := 0;
6798 
6799 --2798667
6800 --this variable contains the ship date of the item from the org
6801 -- For ATO components, this variable contains availability date in org + atp lead time
6802 -- for other itmes, this variable contains the availabilty date.
6803 l_atp_lt_offset_ship_date                date;
6804 
6805 -- rajjain 02/19/2003 Bug 2788302 Begin
6806 l_process_seq_id                NUMBER;
6807 l_src_org_id                    NUMBER;
6808 -- rajjain 02/19/2003 Bug 2788302 End
6809 
6810 -- dsting 2754446
6811 l_PO_qty			NUMBER;
6812 l_prim_uom_avail_qty            NUMBER;
6813 l_prim_uom_req_date_qty         NUMBER;
6814 
6815 -- 2897278
6816 l_avail_PO_qty                  number;
6817 
6818 --(ssurendr) Bug 2865389 OPM fix.
6819 l_opm_start_date                DATE;
6820 l_routing_seq_id                NUMBER;
6821 l_bill_seq_id                   NUMBER;
6822 
6823 --4570421
6824 l_op_seq_id                     NUMBER;
6825 
6826 --s_cto_rearch
6827 l_item_sourcing_info_rec       MSC_ATP_CTO.Item_Sourcing_Info_Rec;
6828 l_null_item_sourcing_info_rec       MSC_ATP_CTO.Item_Sourcing_Info_Rec;
6829 l_get_comp_info_rec            MSC_ATP_REQ.get_comp_info_rec;
6830 --l_get_sup_info_rec             MSC_ATP_PVT.Supplier_Info_rec;
6831 l_item_info_rec                MSC_ATP_PVT.item_attribute_rec;
6832 l_atp_rule_name                varchar2(80);
6833 l_enforce_model_lt             VARCHAR2(1);
6834 
6835 -- time_phased_atp
6836 l_family_item_id                NUMBER;
6837 l_pf_atp                        VARCHAR2(1) := 'N';
6838 l_time_phased_atp               VARCHAR2(1) := 'N';
6839 l_mem_stealing_qty              NUMBER;
6840 l_pf_stealing_qty               NUMBER;
6841 l_sub_qty_before_atf            NUMBER;
6842 l_sub_qty_after_atf             NUMBER;
6843 l_bucketed_demands_rec          MSC_ATP_PF.Bucketed_Demands_Rec;
6844 l_mat_atp_info_rec              MSC_ATP_REQ.Atp_Info_Rec;
6845 l_atf_date_qty                  NUMBER;
6846 l_item_to_use                   NUMBER;
6847 l_used_available_quantity       NUMBER; --bug3409973
6848 l_used_available_quantity_fwd   NUMBER; --bug3409973
6849 
6850 l_ato_date                      date;
6851 l_check_model_capacity          NUMBER := 2;
6852 l_sr_inv_item_id                NUMBER;
6853 
6854 l_bkwd_pass_atf_date_qty         NUMBER; --bug3397904
6855 
6856 -- variables added for ship_rec_cal
6857 l_sup_atp_info_rec              MSC_ATP_REQ.Atp_Info_Rec;
6858 l_sysdate_seq_num	        NUMBER;
6859 l_sup_cap_type		        NUMBER;
6860 l_intransit_lt                  NUMBER;
6861 l_planned_order_date            DATE;
6862 l_new_dock_date                 DATE;
6863 l_new_ship_date                 DATE;
6864 l_sugg_start_date               DATE;   -- Bug 3241766
6865 l_encoded_text                  varchar2(4000);
6866 l_msg_app                       varchar2(50);
6867 l_msg_name                      varchar2(30);
6868 
6869 /* Enforce Pur LT */
6870 l_trunc_sysdate                 DATE := TRUNC(sysdate);
6871 l_trunc_started                 NUMBER := 2;
6872 -- l_supplier_request_date         DATE; -- Bug 3828494 (done with 3828469): Not required anymore
6873 l_sys_next_date                 DATE; --bug3821358
6874 
6875 --Bug 3821358
6876 l_store_dock_date               DATE;
6877 l_store_ship_date               DATE;
6878 l_store_start_date              DATE;
6879 l_store_order_date              DATE;
6880 l_store_due_date                DATE;
6881 l_store_g_ptf_date              DATE;
6882 
6883 -- Bug 3525582 - PTF constraint should be added only if plan is PTF enabled
6884 l_ptf_enabled                   NUMBER;
6885 
6886 --2814895
6887 l_partner_type    NUMBER := 2;
6888 l_party_site_id   NUMBER := NULL;
6889 l_order_line_id   NUMBER := NULL;
6890 l_location_id     NUMBER := NULL;
6891 
6892 -- ATP4drp Declare a variable for creating Planned Arrival.
6893 l_supply_rec                    MSC_ATP_DB_UTILS.supply_rec_typ;
6894 -- End ATP4drp
6895 
6896 --bug 4394904
6897 l_infinite_time_fence          DATE;
6898 l_substitutes_rec  MSC_ATP_REQ.get_subs_out_rec;-- 4902658/5216528
6899 l_temp_net_demand  NUMBER; --4902658 temporary variable to get qty during ctp 5216528
6900 l_atp_ctp_qty NUMBER; --4902658 contains atp + ctp qty 5216528
6901 l_tmp_parent_pegging_id NUMBER; --5216528
6902 l_net_demand_qty NUMBER;	--5598066
6903 l_alloc_atp VARCHAR2(1); --ALLOC ATP CHANGES, 12973673
6904 
6905 BEGIN
6906     IF PG_DEBUG in ('Y', 'C') THEN
6907        msc_sch_wb.atp_debug('**********Begin ATP_Check Procedure************');
6908     END IF;
6909 
6910     -- initialize API return status to success
6911     x_return_status := FND_API.G_RET_STS_SUCCESS;
6912 
6913     -- initialize the error_code to ALLSUCCESS
6914     p_atp_record.error_code := ALLSUCCESS;
6915         IF PG_DEBUG in ('Y', 'C') THEN
6916            msc_sch_wb.atp_debug('in atp_check');
6917         END IF;
6918 
6919     -- print the record
6920 IF PG_DEBUG in ('Y', 'C') THEN
6921    msc_sch_wb.atp_debug('ATP_Check: ' || '********** INPUT DATA: p_atp_record **********');
6922    msc_sch_wb.atp_debug('ATP_Check: ' || 'Inventory_Item_Id:' || to_char(p_atp_record.Inventory_Item_Id) );
6923    msc_sch_wb.atp_debug('ATP_Check: ' || 'request_item_id:' || to_char(p_atp_record.request_item_id) );
6924    msc_sch_wb.atp_debug('ATP_Check: ' || 'organization_id:' || to_char(p_atp_record.organization_id) );
6925    msc_sch_wb.atp_debug('ATP_Check: ' || 'Quantity_Ordered:' || to_char(p_atp_record.Quantity_Ordered) );
6926    msc_sch_wb.atp_debug('ATP_Check: ' || 'Quantity_UOM:' || p_atp_record.Quantity_UOM );
6927    msc_sch_wb.atp_debug('ATP_Check: ' || 'Requested_Ship_Date:' || to_char(p_atp_record.Requested_Ship_Date) );
6928    msc_sch_wb.atp_debug('ATP_Check: ' || 'Requested_Arrival_Date:' || to_char(p_atp_record.Requested_Arrival_Date) );
6929    msc_sch_wb.atp_debug('ATP_Check: ' || 'Latest_Acceptable_Date:' || to_char(p_atp_record.Latest_Acceptable_Date) );
6930    msc_sch_wb.atp_debug('ATP_Check: ' || 'Delivery_Lead_Time:' || to_char(p_atp_record.Delivery_Lead_Time) );
6931    msc_sch_wb.atp_debug('ATP_Check: ' || 'Freight_Carrier:' || p_atp_record.Freight_Carrier );
6932    msc_sch_wb.atp_debug('ATP_Check: ' || 'Ship_Method:' || p_atp_record.Ship_Method );
6933    msc_sch_wb.atp_debug('ATP_Check: ' || 'Demand_Class:' || p_atp_record.Demand_Class );
6934    msc_sch_wb.atp_debug('ATP_Check: ' || 'Override_Flag:' || p_atp_record.Override_Flag );
6935    msc_sch_wb.atp_debug('ATP_Check: ' || 'Action:' || to_char(p_atp_record.Action) );
6936    msc_sch_wb.atp_debug('ATP_Check: ' || 'Ship_Date:' || to_char(p_atp_record.Ship_Date) );
6937    msc_sch_wb.atp_debug('ATP_Check: ' || 'Available_Quantity:' || to_char(p_atp_record.Available_Quantity) );
6938    msc_sch_wb.atp_debug('ATP_Check: ' || 'Used_available_Quantity:' || to_char(p_atp_record.used_available_quantity) ); --bug3409973
6939    msc_sch_wb.atp_debug('ATP_Check: ' || 'Requested_Date_Quantity:' || to_char(p_atp_record.Requested_Date_Quantity) );
6940    msc_sch_wb.atp_debug('ATP_Check: ' || 'supplier_id:' || to_char(p_atp_record.supplier_id) );
6941    msc_sch_wb.atp_debug('ATP_Check: ' || 'supplier_site_id:' || to_char(p_atp_record.supplier_site_id) );
6942    msc_sch_wb.atp_debug('ATP_Check: ' || 'Insert_Flag:' || to_char(p_atp_record.Insert_Flag) );
6943    msc_sch_wb.atp_debug('ATP_Check: ' || 'Error_Code:' || to_char(p_atp_record.Error_Code) );
6944    msc_sch_wb.atp_debug('ATP_Check: ' || 'Order_Number:' || to_char(p_atp_record.Order_Number) );
6945    msc_sch_wb.atp_debug('ATP_Check: ' || 'MSC_ATP_PVT.G_SUMMARY_FLAG := ' || MSC_ATP_PVT.G_SUMMARY_FLAG);
6946    msc_sch_wb.atp_debug('ATP_Check: ' || 'original_item_flag := ' || p_atp_record.original_item_flag);
6947    -- 2462661 : krajan
6948    msc_sch_wb.atp_debug('ATP_Check: ' || 'SRC_ATP_FLAG := ' || p_atp_record.src_atp_flag);
6949    msc_sch_wb.atp_debug('ATP_Check: ' || 'Line Id := ' ||  p_atp_record.demand_source_line);
6950    msc_sch_wb.atp_debug('ATP_Check: ' || 'Top_Model_line_id := ' || p_atp_record.Top_Model_line_id);
6951    msc_sch_wb.atp_debug('ATP_Check: ' || 'ATO_Parent_Model_Line_Id := ' || p_atp_record.ATO_Parent_Model_Line_Id);
6952    msc_sch_wb.atp_debug('ATP_Check: ' || 'ATO_Model_Line_Id := ' || p_atp_record.ATO_Model_Line_Id);
6953    msc_sch_wb.atp_debug('ATP_Check: ' || 'Parent_line_id := ' || p_atp_record.Parent_line_id);
6954    msc_sch_wb.atp_debug('ATP_Check: ' || 'wip_supply_type := ' || p_atp_record.wip_supply_type);
6955    msc_sch_wb.atp_debug('ATP_Check: ' || 'parent_atp_flag := ' || p_atp_record.parent_atp_flag);
6956    msc_sch_wb.atp_debug('ATP_Check: ' || 'parent_atp_comp_flag := ' || p_atp_record.parent_atp_comp_flag);
6957    msc_sch_wb.atp_debug('ATP_Check: ' || 'parent_repl_order_flag := ' || p_atp_record.parent_repl_order_flag);
6958    msc_sch_wb.atp_debug('ATP_Check: ' || 'parent_bom_item_type := ' || p_atp_record.parent_bom_item_type);
6959    msc_sch_wb.atp_debug('ATP_Check: ' || 'mand_comp_flag := ' || p_atp_record.mand_comp_flag);
6960    msc_sch_wb.atp_debug('ATP_Check: ' || 'parent_so_quantity := ' || p_atp_record.parent_so_quantity);
6961    msc_sch_wb.atp_debug('ATP_Check: ' || 'base_model_id := ' || p_atp_record.base_model_id);
6962    msc_sch_wb.atp_debug('ATP_Check: ' || 'rep_ord_flag := ' || p_atp_record.rep_ord_flag);
6963    msc_sch_wb.atp_debug('ATP_Check: ' || 'bom_item_type := '|| p_atp_record.bom_item_type);
6964    msc_sch_wb.atp_debug('ATP_Check: ' || 'parent item id := ' || p_atp_record.parent_item_id);
6965    -- ship_rec_cal
6966    msc_sch_wb.atp_debug('ATP_Check: ' || 'receiving_cal_code := ' || p_atp_record.receiving_cal_code);
6967    msc_sch_wb.atp_debug('ATP_Check: ' || 'intransit_cal_code := ' || p_atp_record.intransit_cal_code);
6968    msc_sch_wb.atp_debug('ATP_Check: ' || 'shipping_cal_code := ' || p_atp_record.shipping_cal_code);
6969    msc_sch_wb.atp_debug('ATP_Check: ' || 'manufacturing_cal_code := ' || p_atp_record.manufacturing_cal_code);
6970    msc_sch_wb.atp_debug('ATP_Check: ' || 'demand_source_type := ' || p_atp_record.demand_source_type);--cmro
6971    msc_sch_wb.atp_debug('ATP_Check: ' || 'MSC_ATP_PVT.G_PTF_DATE := ' || MSC_ATP_PVT.G_PTF_DATE);
6972    msc_sch_wb.atp_debug('ATP_Check: ' || 'bill_seq_id := ' || p_atp_record.bill_seq_id); --4741012
6973    msc_sch_wb.atp_debug('ATP_Check: ' || 'after printing the values');
6974 END IF;
6975 
6976     -- dsting
6977     p_atp_record.children_type := NO_MAKE_BUY_CHILDREN;
6978 
6979 		-- Alloc ATP changes, 12973673
6980 	IF (MSC_ATP_PVT.G_INV_CTP = 4 and MSC_ATP_PVT.G_HYBRID_ALLOC_ATP = 'Y') THEN
6981 		IF PG_DEBUG in ('Y', 'C') THEN
6982             msc_sch_wb.atp_debug('ATP_Check: ' || 'G_HYBRID_ALLOC_ATP is Y');
6983         END IF;
6984 
6985 		MSC_ATP_PVT.G_ALLOCATED_ATP := MSC_ATP_FUNC.check_alloc_exists(p_atp_record.instance_id,
6986 						-1, --PLAN_ID
6987 						p_atp_record.Inventory_Item_Id,
6988 						p_atp_record.organization_id,
6989 						nvl(p_atp_record.Requested_Arrival_Date, p_atp_record.Requested_Ship_Date));
6990 
6991 		IF PG_DEBUG in ('Y', 'C') THEN
6992             msc_sch_wb.atp_debug('ATP_Check: ' || 'MSC_ATP_PVT.G_ALLOCATED_ATP = '||MSC_ATP_PVT.G_ALLOCATED_ATP);
6993         END IF;
6994 	END IF;
6995 
6996     -- check if one and only one of the requested_ship_date and
6997     -- requested_arrival_date is provided
6998 
6999     IF ((p_atp_record.requested_ship_date IS NULL) and
7000         (p_atp_record.requested_arrival_date IS NULL)) THEN
7001 
7002         p_atp_record.error_code := NOREQ_DATE;
7003         RAISE FND_API.G_EXC_ERROR;
7004 
7005     ELSIF ((p_atp_record.requested_ship_date IS NOT NULL) and
7006         (p_atp_record.requested_arrival_date IS NOT NULL)) THEN
7007 
7008         p_atp_record.error_code := ATP_MULTI_REQ_DATES;
7009         RAISE FND_API.G_EXC_ERROR;
7010     END IF;
7011     --bug3821358
7012     l_sys_next_date := MSC_CALENDAR.NEXT_WORK_DAY(p_atp_record.manufacturing_cal_code, -- use OMC
7013                                                   p_atp_record.instance_id,
7014                                                   l_trunc_sysdate);
7015     IF PG_DEBUG in ('Y', 'C') THEN
7016        msc_sch_wb.atp_debug('ATP_Check: Sys Next Date is: ' || l_sys_next_date);
7017     END IF;
7018     /* To support new logic for dependent demands allocation in time phased PF rule based AATP scenarios
7019        Reset global variable*/
7020     MSC_ATP_PVT.G_TIME_PHASED_PF_ENABLED := 'N';
7021 
7022     IF (NVL(p_atp_record.supplier_id, -1) = -1) THEN
7023         -- doing atp check in an inventory org
7024 
7025         IF PG_DEBUG in ('Y', 'C') THEN
7026            msc_sch_wb.atp_debug('ATP_Check: ' || 'do atp check at an inventory org');
7027         END IF;
7028 
7029         -- krajan : 2462661 : post fix
7030         -- set the global variable to null
7031         MSC_ATP_PVT.G_SR_INVENTORY_ITEM_ID := null;
7032 
7033         /* time_phased_atp changes begin
7034            In PDS Case, find if this is time phased atp scenario*/
7035         l_request_item_id       := nvl(p_atp_record.request_item_id, p_atp_record.inventory_item_id); --do I really need this??
7036         l_family_item_id        := p_atp_record.inventory_item_id;
7037 
7038         IF (G_INV_CTP = 4) and (l_request_item_id <> l_family_item_id) THEN
7039 
7040             IF p_atp_record.atf_date is not null THEN
7041                 l_time_phased_atp := 'Y';
7042                 l_pf_atp := 'N';
7043 
7044                 /* To support new logic for dependent demands allocation in time phased PF rule based AATP scenarios
7045                    Set global variable too. This is used in Get_Item_Demand_Alloc_Percent function*/
7046                 MSC_ATP_PVT.G_TIME_PHASED_PF_ENABLED := 'Y';
7047 
7048                 -- CTO_PF_PRJ changes for CTO PF Cross Project Impacts
7049                 -- If dealing with Time Phased ATP Item set the global for CTO_PF
7050                 MSC_ATP_PVT.G_CTO_PF_ATP := 'Y';
7051 
7052                 IF PG_DEBUG in ('Y', 'C') THEN
7053                     msc_sch_wb.atp_debug('ATP_Check: ' || '*** Perform Time Phased ATP for this item *** ');
7054                     msc_sch_wb.atp_debug('ATP_Check: ' || 'ATF Date := ' || p_atp_record.atf_date);
7055                     msc_sch_wb.atp_debug('ATP_Check: Re-set G_CTO_PF_ATP to ' ||
7056                                                        MSC_ATP_PVT.G_CTO_PF_ATP);
7057                 END IF;
7058                 -- CTO_PF_PRJ changes for CTO PF Cross Project Impacts
7059             ELSE
7060                 l_pf_atp := 'Y';
7061                 l_time_phased_atp := 'N';
7062                 l_request_item_id := p_atp_record.inventory_item_id;
7063 
7064                 IF PG_DEBUG in ('Y', 'C') THEN
7065                     msc_sch_wb.atp_debug('ATP_Check: ' || '*** Perform Product Family(non-time phased) ATP for this item *** ');
7066                 END IF;
7067             END IF;
7068         END IF;
7069 
7070         IF PG_DEBUG in ('Y', 'C') THEN
7071             msc_sch_wb.atp_debug('ATP_Check: ' || 'G_TIME_PHASED_PF_ENABLED := ' || MSC_ATP_PVT.G_TIME_PHASED_PF_ENABLED);
7072         END IF;
7073 
7074         /* Now we store member item's info in G_ITEM_INFO_REC (C_ITEM_INFO_REC)
7075            PF_ITEM_INFO_REC is used to store family item's info*/
7076         --s_cto_rearch
7077         l_item_info_rec.parent_repl_ord_flag := p_atp_record.parent_repl_order_flag;
7078         l_item_info_rec.parent_bom_item_type := p_atp_record.parent_bom_item_type;
7079         l_item_info_rec.parent_atp_flag := p_atp_record.parent_atp_flag;
7080         l_item_info_rec.parent_comp_flag := p_atp_record.parent_atp_comp_flag;
7081         l_item_info_rec.parent_pegging_id := p_parent_pegging_id;
7082         l_item_info_rec.parent_item_id :=  p_atp_record.parent_item_id;
7083         IF NVL(p_atp_record.parent_item_id, -1) = p_atp_record.inventory_item_id THEN
7084             -- if we are rechecking model as atp flag = 'Y' then
7085             -- we should recheck model's attribute so that model flags are set correctly.
7086             IF PG_DEBUG in ('Y', 'C') THEN
7087                msc_sch_wb.atp_debug('This models atp flag = Y. We are coming here for second time');
7088             END IF;
7089             MSC_ATP_PVT.G_ITEM_INFO_REC.organization_id := null;
7090         END IF;
7091         --e_cto_rearch
7092         MSC_ATP_PROC.get_global_item_info(p_atp_record.instance_id,
7093                                             --3917625: read item attributes from planned data
7094                                             -- -1,
7095                                             p_plan_id,
7096                                             l_request_item_id,
7097                                             p_atp_record.organization_id,
7098                                             l_item_info_rec );
7099         /* Make a copy */
7100         C_ITEM_INFO_REC := G_ITEM_INFO_REC;
7101 
7102         /* Now get family item's info*/
7103         IF (l_family_item_id = G_ITEM_INFO_REC.sr_inv_item_id) THEN
7104            PF_ITEM_INFO_REC := G_ITEM_INFO_REC;
7105            IF PG_DEBUG in ('Y', 'C') THEN
7106               msc_sch_wb.atp_debug('ATP_Check: ' || 'Local Rec Inst. '||PF_ITEM_INFO_REC.instance_id);
7107            END IF;
7108         ELSE
7109            --s_cto_rearch
7110             PF_ITEM_INFO_REC.parent_pegging_id := p_parent_pegging_id;
7111             PF_ITEM_INFO_REC.parent_bom_item_type := p_atp_record.parent_bom_item_type;
7112             PF_ITEM_INFO_REC.parent_atp_flag := p_atp_record.parent_atp_flag;
7113             PF_ITEM_INFO_REC.parent_comp_flag := p_atp_record.parent_atp_comp_flag;
7114             PF_ITEM_INFO_REC.parent_repl_ord_flag := p_atp_record.parent_repl_order_flag;
7115             --e_cto_rearch
7116            MSC_ATP_PROC.get_item_attributes(p_atp_record.instance_id,
7117                                          --3917625: Read item attribute from plani data
7118                                          -- -1,
7119                                          p_plan_id,
7120                                          l_family_item_id,
7121                                          p_atp_record.organization_id,
7122                                          PF_ITEM_INFO_REC);
7123 
7124         END IF;
7125         /* time_phased_atp changes end */
7126 
7127         --subst
7128         IF MSC_ATP_PVT.G_SUBSTITUTION_FLAG = 'Y' and NVL(p_atp_record.original_item_flag, 2) = 1 and
7129            MSC_ATP_SUBST.G_REQ_ITEM_SR_INV_ID <> p_atp_record.inventory_item_id THEN
7130            -- if it a subst item then we consider subst window
7131             l_substitution_window := C_ITEM_INFO_REC.substitution_window;
7132         ELSE
7133             -- If it is an original item or a bom item then we do not consider window
7134             l_substitution_window := 0;
7135         END IF;
7136         l_atp_flag := C_ITEM_INFO_REC.atp_flag;
7137         l_atp_comp_flag := C_ITEM_INFO_REC.atp_comp_flag;
7138         l_pre_pro_lt := C_ITEM_INFO_REC.pre_pro_lt;
7139         l_fixed_lt := C_ITEM_INFO_REC.fixed_lt;
7140         l_variable_lt := C_ITEM_INFO_REC.variable_lt;
7141         l_post_pro_lt := C_ITEM_INFO_REC.post_pro_lt;
7142 	l_process_lt  := C_ITEM_INFO_REC.processing_lt;  -- 5438149
7143         --diag_atp
7144         l_rounding_control_flag := C_ITEM_INFO_REC.rounding_control_type;
7145         l_unit_weight := C_ITEM_INFO_REC.unit_weight;
7146         l_weight_uom := C_ITEM_INFO_REC.weight_uom;
7147         l_unit_volume := C_ITEM_INFO_REC.unit_volume;
7148         l_volume_uom := C_ITEM_INFO_REC.volume_uom;
7149         l_dest_inv_item_id := C_ITEM_INFO_REC.dest_inv_item_id;
7150 
7151         /* Modularize Item and Org Info */
7152 
7153         IF PG_DEBUG in ('Y', 'C') THEN
7154            msc_sch_wb.atp_debug('ATP_Check: ' || 'ATP flag is '||l_atp_flag);
7155            msc_sch_wb.atp_debug('ATP_Check: ' || 'ATP components flag is ' ||l_atp_comp_flag);
7156            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_pre_pro_lt := ' || l_pre_pro_lt);
7157            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_fixed_lt := ' || l_fixed_lt);
7158            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_variable_lt := ' || l_variable_lt);
7159            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_post_pro_lt := ' || l_post_pro_lt);
7160 	   msc_sch_wb.atp_debug('ATP_Check: ' || 'l_process_lt := ' || l_process_lt);  -- 5438149
7161         END IF;
7162 
7163         -- 2462661 : krajan
7164         -- Check to see if the item is uncollected.
7165         -- If we get here, then it means that the item is not ATPABle.
7166         -- If we defer, then error out
7167         IF PG_DEBUG in ('Y', 'C') THEN
7168             msc_sch_wb.atp_debug ('Checking for uncollected item ' );
7169         end if;
7170         if (MSC_ATP_PVT.G_SR_INVENTORY_ITEM_ID is not NULL) AND
7171             (NVL(p_atp_record.src_atp_flag,'N') <> 'N') then
7172             IF PG_DEBUG in ('Y', 'C') THEN
7173                 msc_sch_wb.atp_debug('ATP_Check: ' || 'Item Not Collected as flags differ');
7174             END IF;
7175             p_atp_record.error_code := ATP_ITEM_NOT_COLLECTED;
7176 
7177             RAISE MSC_ATP_PVT.G_EXC_UNCOLLECTED_ITEM;
7178         end if;
7179               -- end changes for 2462661
7180         /************ Bug 1510853 ATP Rule Check ************/
7181         -- Initialize the rule flag
7182         G_ATP_RULE_FLAG := 'Y';
7183         IF PG_DEBUG in ('Y', 'C') THEN
7184            msc_sch_wb.atp_debug('ATP_Check: ' || 'C_ITEM_INFO_REC atp_rule_id := '
7185                              || NVL(C_ITEM_INFO_REC.atp_rule_id, 0));
7186         END IF;
7187         IF ((G_INV_CTP = 5) AND (C_ITEM_INFO_REC.atp_rule_id IS NULL)) THEN
7188            G_ATP_RULE_FLAG := 'N';   -- Rule not found at item level.
7189            IF PG_DEBUG in ('Y', 'C') THEN
7190               msc_sch_wb.atp_debug('ATP_Check: ' || ' NO ATP RULE FOR ITEM ');
7191            END IF;
7192         END IF;
7193         /************ Bug 1510853 ATP Rule Check ************/
7194 
7195 
7196         l_delivery_lead_time := p_atp_record.delivery_lead_time;
7197         l_ship_method := p_atp_record.ship_method;
7198 
7199         -- get the default delivery_lead_time if it is not specified.
7200         -- dsting dlt
7201         IF (NVL(p_atp_record.delivery_lead_time, -1) = -1 ) THEN
7202 
7203                 msc_sch_wb.atp_debug('delivery lead time is not specified');
7204 
7205                 -- if we can not get the lead time from sourcing,
7206                 -- we need to find the default between locations.
7207 
7208 		-- Set l_site_id to null in case destination is an org to avoid getting lead time/ ship method
7209 		-- based on region of customer site.
7210 
7211 	        --2814895, populating variables for the call to atp_sources
7212 
7213 	        IF  MSC_ATP_PVT.G_SR_PARTNER_SITE_ID IS NOT NULL THEN
7214 	           l_partner_type := 2;
7215 	           l_party_site_id := NULL;
7216 	           l_order_line_id := NULL;
7217 		   l_site_id := MSC_ATP_PVT.G_SR_PARTNER_SITE_ID;
7218 		   l_location_id := p_atp_record.to_location_id;
7219 	        ELSIF MSC_ATP_PVT.G_SR_PARTY_SITE_ID IS NOT NULL THEN
7220 	           l_site_id := NULL;
7221 	           l_partner_type := 4;
7222 	           l_party_site_id := MSC_ATP_PVT.G_SR_PARTY_SITE_ID;
7223 	           l_order_line_id := NULL;
7224 	           l_location_id := NULL; --MSC_ATP_PVT.G_SR_LOCATION_ID;
7225 	        ELSIF MSC_ATP_PVT.G_SR_CUSTOMER_COUNTRY IS NOT NULL THEN
7226 	           l_partner_type := 5;
7227 	           l_party_site_id := NULL;
7228 	           l_order_line_id := p_atp_record.identifier;
7229 		   l_site_id := NULL;
7230 		   l_location_id := NULL;
7231 		ELSIF p_atp_record.to_organization_id IS NOT NULL THEN
7232 		   l_site_id := NULL;
7233 		   l_partner_type := NULL;
7234 	           l_party_site_id := NULL;
7235 	           l_order_line_id := NULL;
7236 	           l_location_id := p_atp_record.to_location_id;
7237 	        ELSE
7238 	          IF PG_DEBUG in ('Y', 'C') THEN
7239                      msc_sch_wb.atp_debug('ATP_Check: Nothing passed');
7240                    END IF;
7241                 END IF;
7242 
7243 -- cchen : changes for ship method
7244 
7245 		MSC_ATP_PROC.get_delivery_lead_time(p_atp_record.organization_id,
7246 						NULL,	-- from location id
7247 						p_atp_record.instance_id,
7248 						p_atp_record.to_organization_id,
7249 						l_location_id, --p_atp_record.to_location_id, --2814895
7250 						p_atp_record.to_instance_id,
7251 						NULL,	-- customer_id
7252 						NULL,	-- customer_site_id
7253 						NULL,	-- supplier_id
7254 						NULL,	-- supplier_site_id
7255 						MSC_ATP_PVT.G_SESSION_ID,
7256 						l_site_id,
7257 						l_ship_method,
7258 						l_delivery_lead_time,
7259 						l_partner_type, --2814895
7260 						l_party_site_id, --2814895
7261 						l_order_line_id); --2814895
7262 		-- dsting replaced old dlt code
7263 
7264         END IF;
7265 
7266         -- dsting dlt Round delivery lead time up
7267         l_delivery_lead_time := CEIL(l_delivery_lead_time);
7268         IF PG_DEBUG in ('Y', 'C') THEN
7269            msc_sch_wb.atp_debug('rounded l_delivery_lead_time: ' || l_delivery_lead_time);
7270         END IF;
7271 
7272         -- BUG 1621816- We offset the delivery lead time instead of
7273         -- reducing it. To off set we use calender code of the target_org
7274 
7275         IF (p_atp_record.requested_arrival_date IS NOT NULL) THEN
7276             -- we need to calculate the requested_ship_date
7277             -- requested_ship_date = requested_arrival_date - delivery_lead_time
7278 
7279             -- ship_rec_cal do we come here??
7280             l_requested_ship_date := MSC_CALENDAR.prev_work_day(
7281                                         p_atp_record.organization_id,
7282                                         p_atp_record.instance_id,
7283                                         MSC_CALENDAR.TYPE_DAILY_BUCKET,
7284                                         p_atp_record.requested_arrival_date
7285                                         - l_delivery_lead_time);
7286 
7287 /* dlt
7288            IF (p_atp_record.customer_id IS NOT NULL) THEN
7289                 ---Inquiry from a customer site
7290                 l_requested_ship_date := p_atp_record.requested_arrival_date -
7291                                      l_delivery_lead_time ;
7292            ELSE
7293                  --- Org
7294                 l_requested_ship_date := MSC_CALENDAR.DATE_OFFSET
7295                                         (p_atp_record.to_organization_id,
7296                                          p_atp_record.to_instance_id,
7297                                          1,
7298                                          p_atp_record.requested_arrival_date,
7299                                          -NVL(l_delivery_lead_time, 0));
7300            END IF;
7301 */
7302         ELSE
7303             l_requested_ship_date := p_atp_record.requested_ship_date;
7304         END IF;
7305         /*
7306             Added by avjain to capture original request date to insert in msc-demands
7307             original_request_date corresponds to request date for 24*7 ATP which gives
7308             the request date
7309             */
7310         l_original_req_ship_date := nvl(p_atp_record.original_request_date,l_requested_ship_date);
7311         IF PG_DEBUG in ('Y', 'C') THEN
7312            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_original_req_ship_date: '||l_original_req_ship_date);
7313         END IF;
7314 
7315         l_summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;
7316         --- postprocess summary
7317         --- The following array stores 1 if we are doing Demand Class ATP for item-org
7318         --- We are doing this way because if we need to reomve or update the demand then
7319         --- we know that we need to do demand class based  update or removal of the demand in
7320         ---- case of ODS
7321         IF ((p_search = 1) and (p_parent_pegging_id is null)) THEN
7322            IF (MSC_ATP_PVT.G_INV_CTP = 5) AND (MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y') THEN
7323               --- THIS IS OSD case, backward case, and top demand
7324               IF PG_DEBUG in ('Y', 'C') THEN
7325                  msc_sch_wb.atp_debug('ATP_Check: ' || 'Find out ATP_DEMAND_class flag for that item ');
7326               END IF;
7327 
7328               BEGIN
7329                  SELECT NVL(r.demand_class_atp_flag, 0)
7330                  INTO   l_demand_class_flag
7331                  FROM   msc_system_items I,
7332                         msc_trading_partners P,
7333                         msc_atp_rules  R
7334                  WHERE  I.sr_instance_id = p_atp_record.instance_id
7335                  AND    I.organization_id = p_atp_record.organization_id
7336                  AND    I.sr_inventory_item_id = p_atp_record.inventory_item_id
7337                  AND    I.plan_id = -1
7338                  AND    I.organization_id = P.sr_tp_id
7339                  AND    P.sr_instance_id = I.sr_instance_id
7340                  AND    P.partner_type = 3 --- organization
7341                  AND    I.sr_instance_id = R.sr_instance_id
7342                  AND    R.rule_id = NVL(I.atp_rule_id, P.default_atp_rule_id);
7343 
7344                  IF PG_DEBUG in ('Y', 'C') THEN
7345                     msc_sch_wb.atp_debug('ATP_Check: ' || 'atp_demand_class_flag  : = ' || l_demand_class_flag);
7346                  END IF;
7347               EXCEPTION
7348                  WHEN OTHERS THEN
7349                        l_demand_class_flag := 0;
7350               END;
7351 
7352            ELSE
7353               ---pds cae
7354               l_demand_class_flag := 0;
7355            END IF;
7356            --- now extend the array which holds the demand_class atp_flag and add l_demand_class_flag to it
7357            MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG.EXTEND;
7358            IF PG_DEBUG in ('Y', 'C') THEN
7359               msc_sch_wb.atp_debug('ATP_Check: ' || ' MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG.count := ' || MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG.count);
7360            END IF;
7361             MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG( MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG.count) := l_demand_class_flag;
7362         END IF;
7363 
7364         -- 1143475:
7365         -- IF atp lead time > 0 then we must do the offset no matter that item
7366         -- is atpable or not.  Otherwise, only if that item is atpable
7367         -- item we need to do the offset.  The reason of doing this is to avoid
7368         -- database hit.
7369 
7370         -- Bugs 2020607, 2104018, 2031894, 1869748
7371         -- Shift dates to work days irrespective of atp_flag.
7372 
7373 /*        If  (NVL(p_atp_record.atp_lead_time, 0) >0)  OR
7374           (l_atp_flag <> 'N') OR (l_atp_comp_flag <> 'N') THEN  */
7375 
7376         /* ship_rec_cal changes begin
7377         -- translate the l_requested_ship_date to a workday.
7378         l_requested_ship_date := MSC_ATP_FUNC.prev_work_day(p_atp_record.organization_id,
7379                                             p_atp_record.instance_id,
7380                                             l_requested_ship_date);
7381 
7382         -- bug 1192311
7383         IF l_requested_ship_date is NULL THEN
7384           p_atp_record.error_code := ATP_INVALID_DATE;
7385           RAISE FND_API.G_EXC_ERROR;
7386         END IF;*/
7387 
7388         -- since we may have atp lead time specified for the item,
7389         -- we need to offset x working days from the the ship date.
7390 
7391         /* S_CTO_REACH : do not honor atp lead time
7392 
7393         l_requested_ship_date := MSC_CALENDAR.DATE_OFFSET
7394                                   (p_atp_record.organization_id,
7395                                    p_atp_record.instance_id,
7396                                    1,
7397                                    l_requested_ship_date,
7398                                    -NVL(p_atp_record.atp_lead_time, 0));
7399         */
7400 
7401         -- Bugs 2020607, 2104018, 2031894, 1869748
7402         -- Find the next working date for sysdate
7403         -- and comment out the call related to bug 1124538.
7404 
7405         BEGIN  -- Bug2031894. Handle Exception here.
7406           l_sysdate := NVL(MSC_CALENDAR.next_work_day(
7407                                         p_atp_record.shipping_cal_code,
7408                                         p_atp_record.instance_id,
7409                                         sysdate), sysdate);
7410         EXCEPTION
7411           WHEN OTHERS THEN
7412                 NULL;
7413         END;
7414 
7415 /*        -- for bug 1124538 : find the prev working date of sysdate
7416         l_sysdate := NVL(MSC_ATP_FUNC.prev_work_day(p_atp_record.organization_id,
7417                                             p_atp_record.instance_id,
7418                                             sysdate), sysdate);   */
7419 
7420         IF PG_DEBUG in ('Y', 'C') THEN
7421            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_sysdate ='||l_sysdate);
7422         END IF;
7423         --END IF;    Comment out atp_flag block
7424         -- Bugs 2020607, 2104018, 2031894, 1869748
7425 
7426         p_atp_record.delivery_lead_time := l_delivery_lead_time;
7427         p_atp_record.ship_method := l_ship_method;
7428 
7429         IF PG_DEBUG in ('Y', 'C') THEN
7430            msc_sch_wb.atp_debug('ATP_Check: ' || 'ship_method = '||l_ship_method ||' lead time = '||l_delivery_lead_time);
7431             msc_sch_wb.atp_debug('ATP_Check: ' || 'l_requested_ship_date :=' || l_requested_ship_date);
7432          END IF;
7433 
7434         IF (l_atp_flag = 'N') and (l_atp_comp_flag = 'N') THEN
7435             IF PG_DEBUG in ('Y', 'C') THEN
7436                msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atp_flag and l_atp_comp_flag are both N');
7437             END IF;
7438             p_atp_record.error_code := ATP_NOT_APPL;
7439 
7440 	    -- Bug 1916086, modified Schedule procedure to extend G_PEGGING_FOR_SET in case of
7441 	    -- top-level Non-ATPable items as well. This was needed so that l_atp_table and
7442 	    -- l_fst_src_pegging_ids would have same number of records.
7443 
7444             IF p_parent_pegging_id is null THEN
7445                 IF PG_DEBUG in ('Y', 'C') THEN
7446                    msc_sch_wb.atp_debug('ATP_Check: ' || 'p_parent_pegging_id is null');
7447                    msc_sch_wb.atp_debug('ATP_Check: ' || 'G_PEGGING_FOR_SET.COUNT = ' ||MSC_ATP_PVT.G_PEGGING_FOR_SET.COUNT);
7448                 END IF;
7449 
7450                 MSC_ATP_PVT.G_PEGGING_FOR_SET.Extend;
7451                 IF PG_DEBUG in ('Y', 'C') THEN
7452                    msc_sch_wb.atp_debug('ATP_Check: ' || 'G_PEGGING_FOR_SET.COUNT = ' ||MSC_ATP_PVT.G_PEGGING_FOR_SET.COUNT);
7453                    msc_sch_wb.atp_debug('ATP_Check: ' || 'setting null pegging id for Non-ATPable items');
7454                 END IF;
7455 
7456                 MSC_ATP_PVT.G_PEGGING_FOR_SET(MSC_ATP_PVT.G_PEGGING_FOR_SET.COUNT) := null;
7457 
7458                 MSC_ATP_PVT.G_REQ_ATP_DATE.EXTEND;
7459                 MSC_ATP_PVT.G_REQ_DATE_QTY.EXTEND;
7460 
7461                 MSC_ATP_PVT.G_REQ_ATP_DATE(MSC_ATP_PVT.G_REQ_ATP_DATE.count) := l_requested_ship_date;
7462                 MSC_ATP_PVT.G_REQ_DATE_QTY(MSC_ATP_PVT.G_REQ_DATE_QTY.count) := p_atp_record.quantity_ordered;
7463 
7464             END IF;
7465 
7466             -- Bugs 2020607, 2104018, 2031894, 1869748
7467             -- Redo changes made for Bug 1124538
7468             -- This is a desired functionality
7469 
7470             -- bug 1143475
7471             -- no matter it is past due or not, we always satisfy users
7472             -- on the date they want. that is, we should not offset the date
7473             -- users request unless atp lead time is specified and not equal 0
7474 
7475             -- for bug 1124538 : if request date is less than sysdate,
7476             -- than the requested_date_quantity should be 0 and ship_date
7477             -- will be sysdate
7478 
7479             -- Bug 2625791, 2623816: If it is a transfer case then We should take
7480             -- into consideration PTF date passed to us from the top organization.
7481             -- We should satisfy the damand as per the PTF date from the top org.
7482             IF PG_DEBUG in ('Y', 'C') THEN
7483                msc_sch_wb.atp_debug('ATP_Check: ' || 'MSC_ATP_PVT.G_PTF_DATE := ' || MSC_ATP_PVT.G_PTF_DATE);
7484             END IF;
7485 
7486             --s_cto_rearch: bug 3169831 : Honor lead time only when profile option is turned on
7487             l_enforce_model_lt := NVL(FND_PROFILE.VALUE('MSC_ENFORCE_MODEL_LT'), 'Y');
7488 
7489             IF PG_DEBUG in ('Y', 'C') THEN
7490                msc_sch_wb.atp_debug('l_enforce_model_lt := ' || l_enforce_model_lt);
7491             END IF;
7492 
7493             IF MSC_ATP_PVT.G_INV_CTP = 5 and C_ITEM_INFO_REC.bom_item_type = 1
7494                                          and C_ITEM_INFO_REC.replenish_to_ord_flag = 'Y'
7495                                          and l_enforce_model_lt = ' Y' THEN
7496 
7497                -- in ODS ATP we want to honor lead times for ATO models
7498                l_mso_lead_time_factor := MSC_ATP_PVT.G_MSO_LEAD_TIME_FACTOR;
7499                l_net_processing_lt := (l_fixed_lt + l_variable_lt * p_atp_record.quantity_ordered)*
7500                                                                          (1 + l_mso_lead_time_factor);
7501                l_ato_date := MSC_CALENDAR.DATE_OFFSET
7502                                   (p_atp_record.organization_id,
7503                                    p_atp_record.instance_id,
7504                                    1,
7505                                    l_sysdate,
7506                                    l_net_processing_lt);
7507             ELSE
7508               l_ato_date := l_sysdate;
7509             END IF;
7510             --5160663, commenting out.
7511             /*
7512             IF l_requested_ship_date < GREATEST(l_sysdate,l_ato_date)  THEN
7513                 IF PG_DEBUG in ('Y', 'C') THEN
7514                    msc_sch_wb.atp_debug('ATP_Check: ' || 'l_requested_ship_date < l_sysdate');
7515                 END IF;
7516                 p_atp_record.requested_date_quantity := 0.0;
7517                 p_atp_record.ship_date := GREATEST(l_sysdate, MSC_ATP_PVT.G_PTF_DATE, l_ato_date);
7518             ELSE
7519                 IF PG_DEBUG in ('Y', 'C') THEN
7520                    msc_sch_wb.atp_debug('ATP_Check: ' || 'l_requested_ship_date >= l_sysdate');
7521                 END IF;
7522                 p_atp_record.requested_date_quantity :=
7523                         p_atp_record.quantity_ordered;
7524                 p_atp_record.ship_date :=  GREATEST(l_requested_ship_date, MSC_ATP_PVT.G_PTF_DATE);
7525             END IF;
7526             */
7527 
7528             -- Bugs 2020607, 2104018, 2031894, 1869748
7529             -- Redo changes made for Bug 1124538
7530             -- This is a desired functionality
7531             -- Comments are uncommented out END.
7532 
7533             p_atp_record.requested_date_quantity :=
7534                         p_atp_record.quantity_ordered;
7535 
7536             -- cchen 1238941
7537             p_atp_record.combined_requested_date_qty :=
7538                 p_atp_record.quantity_ordered;
7539 
7540             -- cchen 1276131 make sure we offset if atp_lead time exists
7541             --p_atp_record.ship_date := l_requested_ship_date;
7542             p_atp_record.ship_date := GREATEST(l_requested_ship_date, MSC_ATP_PVT.G_PTF_DATE); --5160663
7543             /* s_cto_arch: do not honor atp lead time
7544             IF NVL(p_atp_record.atp_lead_time, 0) > 0 THEN
7545               p_atp_record.ship_date := MSC_CALENDAR.DATE_OFFSET
7546                                  (p_atp_record.organization_id,
7547                                   p_atp_record.instance_id,
7548                                   1,
7549                                   l_requested_ship_date,
7550                                   p_atp_record.atp_lead_time);
7551             --  Bug2302403
7552             --ELSE
7553             --  p_atp_record.ship_date := l_requested_ship_date;
7554             END IF;
7555             */
7556 
7557             IF PG_DEBUG in ('Y', 'C') THEN
7558                msc_sch_wb.atp_debug('ATP_Check: ' || 'p_atp_record.ship_date:='||
7559                                   p_atp_record.ship_date );
7560                msc_sch_wb.atp_debug('ATP_Check: ' || 'p_atp_record.requested_date_quantity:='||
7561                                   p_atp_record.requested_date_quantity);
7562             END IF;
7563             p_atp_record.available_quantity :=  p_atp_record.quantity_ordered;
7564 
7565             -- In case the call is for multi-level/ multi-org CTO with a
7566             -- reduced ship set, we need to generate the pegging tree
7567             -- even if the item is non-ATPable. - ngoel 10/18/00
7568 
7569             -- Check if Records exist in MSC_BOM_TEMP table in case of PDS. If yes,
7570             -- create pegging record. Since for models and option classes, atp_comp_flag
7571 	    -- shall always be yes in case of CTO, this situation may only occur at
7572 	    -- components level.
7573 
7574             IF PG_DEBUG in ('Y', 'C') THEN
7575                msc_sch_wb.atp_debug('ATP_Check: ' || 'p_atp_record.Calling_Module : '||p_atp_record.Calling_Module);
7576             END IF;
7577 
7578               --s_cto_rearch
7579             /* bug 3564511: We do not need to add demand and pegging for non-atpable items
7580                because we now use data stored in msc_cto_bom to retrieve bom for 24x7 processing.
7581                Removed code for adding demands and pegging
7582 
7583             */
7584 
7585         ELSIF (NVL(p_atp_record.override_flag, 'N') = 'Y') AND (G_INV_CTP = 5)
7586              --bug 3742009: For ATO models/items, do the regular processing as their bom
7587              --- need to be exploded and demands need to be placed on the bom components
7588              AND NOT( C_ITEM_INFO_REC.bom_item_type in (1,4) and  C_ITEM_INFO_REC.replenish_to_ord_flag = 'Y') THEN
7589             -- ATP Override -- Agilent changes -- ODS ATP
7590 
7591             --- bug 2249118
7592             -- Bug 1916086, modified Schedule procedure to extend G_PEGGING_FOR_SET in case of
7593             -- top-level Non-ATPable items as well. This was needed so that l_atp_table and
7594             -- l_fst_src_pegging_ids would have same number of records.
7595 
7596             IF p_parent_pegging_id is null THEN
7597 
7598                 MSC_ATP_PVT.G_PEGGING_FOR_SET.Extend;
7599                 IF PG_DEBUG in ('Y', 'C') THEN
7600                    msc_sch_wb.atp_debug('ATP_Check: ' || 'p_parent_pegging_id is null');
7601                    msc_sch_wb.atp_debug('ATP_Check: ' || 'G_PEGGING_FOR_SET.COUNT = ' ||MSC_ATP_PVT.G_PEGGING_FOR_SET.COUNT);
7602                    msc_sch_wb.atp_debug('ATP_Check: ' || 'G_PEGGING_FOR_SET.COUNT = ' ||MSC_ATP_PVT.G_PEGGING_FOR_SET.COUNT);
7603                    msc_sch_wb.atp_debug('ATP_Check: ' || 'setting null pegging id for Non-ATPable items');
7604                 END IF;
7605 
7606                 MSC_ATP_PVT.G_PEGGING_FOR_SET(MSC_ATP_PVT.G_PEGGING_FOR_SET.COUNT) := null;
7607 
7608                 MSC_ATP_PVT.G_REQ_ATP_DATE.EXTEND;
7609                 MSC_ATP_PVT.G_REQ_DATE_QTY.EXTEND;
7610 
7611                 MSC_ATP_PVT.G_REQ_ATP_DATE(MSC_ATP_PVT.G_REQ_ATP_DATE.count) := l_requested_ship_date;
7612                 MSC_ATP_PVT.G_REQ_DATE_QTY(MSC_ATP_PVT.G_REQ_DATE_QTY.count) := p_atp_record.quantity_ordered;
7613 
7614             END IF;
7615 
7616             -- cchen 1276131 make sure we offset if atp_lead time exists
7617             /* s_cto_rearch: do not honor atp lead time
7618             IF NVL(p_atp_record.atp_lead_time, 0) > 0 THEN
7619               p_atp_record.ship_date := MSC_CALENDAR.DATE_OFFSET
7620                                  (p_atp_record.organization_id,
7621                                   p_atp_record.instance_id,
7622                                   1,
7623                                   -- Bug 2625791, 2623816
7624                                   GREATEST(l_requested_ship_date, MSC_ATP_PVT.G_PTF_DATE),
7625                                   p_atp_record.atp_lead_time);
7626             ELSE
7627               p_atp_record.ship_date := l_requested_ship_date;
7628             END IF;
7629             */
7630              p_atp_record.ship_date := l_requested_ship_date;
7631 
7632             p_atp_record.error_code := ALLSUCCESS;
7633 
7634             -- prepare the insert record
7635             -- no matter it is a demand or not, we need to insert this
7636             -- record into database since it is a recursive procedure
7637             -- and we will rollback in ATP procedure if it is not a
7638             -- demand.
7639 
7640             l_atp_insert_rec.instance_id := p_atp_record.instance_id;
7641             /* Modularize Item and Org Info */
7642             -- Re_use Information.
7643             -- time_phased_atp - Now C_ITEM_INFO_REC stores member item info in ODS case
7644             l_atp_insert_rec.inventory_item_id := C_ITEM_INFO_REC.dest_inv_item_id;
7645             -- No need to call function below.
7646             /*l_atp_insert_rec.inventory_item_id := MSC_ATP_FUNC.get_inv_item_id
7647                                                 (p_atp_record.instance_id,
7648                                                 --- 2152184
7649                                                 l_request_item_id,
7650                                                 --p_atp_record.request_item_id,
7651                                                 null,
7652                                                 p_atp_record.organization_id);
7653              Modularize Item and Org Info */
7654             l_atp_insert_rec.organization_id := p_atp_record.organization_id;
7655             l_atp_insert_rec.identifier := p_atp_record.identifier;
7656             l_atp_insert_rec.demand_source_type:=nvl(p_atp_record.demand_source_type, 2);
7657             l_atp_insert_rec.demand_source_header_id :=
7658                             nvl(p_atp_record.demand_source_header_id, -1);
7659             l_atp_insert_rec.demand_source_line := p_atp_record.demand_source_line;
7660             l_atp_insert_rec.demand_source_delivery :=
7661                             p_atp_record.demand_source_delivery;
7662             l_atp_insert_rec.quantity_ordered :=
7663                              p_atp_record.quantity_ordered;
7664             l_atp_insert_rec.requested_ship_date := l_requested_ship_date;
7665             l_atp_insert_rec.demand_class := p_atp_record.demand_class;
7666             l_atp_insert_rec.refresh_number := p_refresh_number; -- summary enhancement p_atp_record.refresh_number;
7667 
7668 	    -- Modified by ngoel on 1/12/2001 for origination_type = 30
7669             --l_atp_insert_rec.origination_type := 6;
7670             l_atp_insert_rec.origination_type := 30;
7671 
7672             l_atp_insert_rec.order_number := p_atp_record.order_number;
7673             -- added by avjain for plan by request date
7674             l_atp_insert_rec.ship_set_name := p_atp_record.ship_set_name;
7675             l_atp_insert_rec.arrival_set_name := p_atp_record.arrival_set_name;
7676             l_atp_insert_rec.original_request_ship_date :=l_original_req_ship_date;
7677             IF p_search = 1 and p_parent_pegging_id is null THEN
7678                --- we need to pass old demand id so that we can update that demand
7679                --- this will happen in case of reschedulling
7680                l_atp_insert_rec.old_demand_id := p_atp_record.old_demand_id;
7681             ELSE
7682                l_atp_insert_rec.old_demand_id := null;
7683             END IF;
7684 
7685             l_atp_insert_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
7686             l_atp_insert_rec.delivery_lead_time := l_delivery_lead_time;
7687 
7688             MSC_ATP_DB_UTILS.Add_Mat_Demand(l_atp_insert_rec,
7689                            p_plan_id,
7690                            l_demand_class_flag,
7691                            l_demand_id);
7692             IF PG_DEBUG in ('Y', 'C') THEN
7693                msc_sch_wb.atp_debug('ATP_Check: ' || 'after calling Add_Mat_Demand, l_demand_id ='||
7694                            l_demand_id);
7695             END IF;
7696 
7697 
7698 --        END IF;
7699 
7700         ELSIF l_atp_flag in ('Y', 'C', 'N') THEN
7701             -- ATP Override -- Agilent changes -- PDS ATP
7702             -- Allow Override for PDS also
7703             --(NVL(p_atp_record.override_flag, 'N') <> 'Y')) THEN
7704 
7705             -- get the following information
7706             -- requested_date_quantity
7707             -- atp_date_this_level
7708             -- atp_date_quantity_this_level
7709 
7710           -- hierarchy changes due to arrival set
7711 
7712           IF (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') THEN
7713                IF PG_DEBUG in ('Y', 'C') THEN
7714                   msc_sch_wb.atp_debug('ATP_Check: ' || 'customer class or demand class : same calls');
7715                END IF;
7716 
7717               /* Modularize Item and Org Info */
7718               -- Re-Use info instead of making unnecessary call.
7719               l_inv_item_id := C_ITEM_INFO_REC.dest_inv_item_id;
7720               /*l_inv_item_id := MSC_ATP_FUNC.get_inv_item_id(
7721                                           p_atp_record.instance_id,
7722                                           p_atp_record.inventory_item_id,
7723                                           null,
7724                                           p_atp_record.organization_id);
7725                Modularize Item and Org Info */
7726 
7727               /* New allocation logic for time_phased_atp changes begin */
7728               IF l_time_phased_atp = 'Y' THEN
7729                       MSC_ATP_PF.Set_Alloc_Rule_Variables(
7730                           l_inv_item_id,
7731                           PF_ITEM_INFO_REC.dest_inv_item_id,
7732                           p_atp_record.organization_id,
7733                           p_atp_record.instance_id,
7734                           p_atp_record.demand_class,
7735                           p_atp_record.atf_date,
7736                           l_return_status
7737                       );
7738                       IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
7739                            IF PG_DEBUG in ('Y', 'C') THEN
7740                                 msc_sch_wb.atp_debug('ATP_Check: ' || 'Error occured in procedure Set_Alloc_Rule_Variables');
7741                            END IF;
7742                            RAISE FND_API.G_EXC_ERROR;
7743                       END IF;
7744 
7745                       IF l_requested_ship_date <= p_atp_record.atf_date THEN
7746                           IF MSC_ATP_PVT.G_MEM_RULE_WITHIN_ATF = 'Y' THEN
7747                               l_item_to_use := l_inv_item_id;
7748                           ELSE
7749                               l_item_to_use := PF_ITEM_INFO_REC.dest_inv_item_id;
7750                           END IF;
7751                       ELSE
7752                           IF MSC_ATP_PVT.G_PF_RULE_OUTSIDE_ATF = 'Y' THEN
7753                               l_item_to_use := PF_ITEM_INFO_REC.dest_inv_item_id;
7754                           ELSE
7755                               l_item_to_use := l_inv_item_id;
7756                           END IF;
7757                       END IF;
7758               ELSE
7759                       l_item_to_use := l_inv_item_id;
7760               END IF;
7761               IF PG_DEBUG in ('Y', 'C') THEN
7762                  msc_sch_wb.atp_debug('ATP_Check: ' || 'l_requested_ship_date = '||l_requested_ship_date);
7763                  msc_sch_wb.atp_debug('ATP_Check: ' || 'Item to be used = '||l_item_to_use);
7764               END IF;
7765               /* New allocation logic for time_phased_atp changes end */
7766 
7767               p_atp_record.demand_class :=
7768 
7769                   MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(MSC_ATP_PVT.G_PARTNER_ID,
7770                            MSC_ATP_PVT.G_PARTNER_SITE_ID,
7771                            l_item_to_use, -- For new allocation logic for time_phased_atp
7772                            p_atp_record.organization_id,
7773                            p_atp_record.instance_id,
7774                            l_requested_ship_date,
7775                            NULL, -- level_id
7776                            p_atp_record.Demand_Class);
7777                   --diag_atp
7778                   l_allocation_rule_name := MSC_ATP_PVT.G_ALLOCATION_RULE_NAME;
7779               IF PG_DEBUG in ('Y', 'C') THEN
7780                  msc_sch_wb.atp_debug('ATP_Check: ' || 'after getting the dummy demand class');
7781                  msc_sch_wb.atp_debug('ATP_Check: ' || 'p_atp_record.demand_class = '||p_atp_record.demand_class);
7782               END IF;
7783 
7784           END IF;
7785           ---subst
7786           IF MSC_ATP_PVT.G_SUBSTITUTION_FLAG = 'Y' and
7787              ((p_search = BACKWARD_SCHEDULING AND NVL(p_atp_record.original_item_flag, 2) = 1) OR
7788                (p_search = FORWARD_SCHEDULING AND p_atp_record.top_tier_org_flag = 1)) THEN
7789               --- in case of product substitution
7790               ---1) Backward case- we do not want to look for top item's availability as it is already been
7791               --- taken care of during breadth level item availability search in Check_atp-subst
7792               ---2) Forward: we do not want to look for item's availability in top org
7793               IF PG_DEBUG in ('Y', 'C') THEN
7794                  msc_sch_wb.atp_debug('ATP_Check: ' || 'Original item, substitution');
7795               END IF;
7796               IF l_atp_comp_flag = 'N' THEN
7797                  --- if ATP components flag is "N' and if it top level item then
7798                  --- we cant make anything. Therefore, we set the l_re_date_qty to be zero
7799                  l_requested_date_quantity := 0;
7800                  l_net_demand := 0;
7801               ELSE
7802                  l_net_demand := p_atp_record.quantity_ordered;
7803                  IF p_search = BACKWARD_SCHEDULING and NVL(p_atp_record.top_tier_org_flag, 2) = 2 THEN
7804                      -- we add demand for only second tier org. Demand for first tier org has
7805                      --- already been created while looking for substitute availability
7806                      IF PG_DEBUG in ('Y', 'C') THEN
7807                         msc_sch_wb.atp_debug('ATP_Check: ' || 'Backward sch and not a top org, add demand');
7808                      END IF;
7809                      l_atp_insert_rec.instance_id := p_atp_record.instance_id;
7810                      /* Modularize Item and Org Info */
7811                      -- time_phased_atp changes begin
7812                      l_atp_insert_rec.inventory_item_id := PF_ITEM_INFO_REC.dest_inv_item_id;
7813                      l_atp_insert_rec.request_item_id := C_ITEM_INFO_REC.dest_inv_item_id;
7814                      l_atp_insert_rec.atf_date := p_atp_record.atf_date;
7815                      l_atp_insert_rec.atf_date_quantity := 0; -- check this??
7816                      l_atp_insert_rec.requested_date_quantity := l_requested_date_quantity;
7817                      -- time_phased_atp changes end
7818 
7819                      l_atp_insert_rec.organization_id := p_atp_record.organization_id;
7820                      l_atp_insert_rec.identifier := p_atp_record.identifier;
7821                      l_atp_insert_rec.demand_source_type:=nvl(p_atp_record.demand_source_type, 2);
7822                      l_atp_insert_rec.demand_source_header_id :=
7823                                      nvl(p_atp_record.demand_source_header_id, -1);
7824                      l_atp_insert_rec.demand_source_delivery :=
7825                                      p_atp_record.demand_source_delivery;
7826 
7827                      l_atp_insert_rec.quantity_ordered :=  p_atp_record.quantity_ordered;
7828 
7829                      l_atp_insert_rec.requested_ship_date := l_requested_ship_date;
7830                      l_atp_insert_rec.demand_class := p_atp_record.demand_class;
7831                      l_atp_insert_rec.refresh_number := p_refresh_number; -- summary enhancement p_atp_record.refresh_number;
7832                      l_atp_insert_rec.order_number := p_atp_record.order_number;
7833 
7834 
7835                      -- ATP4drp If we are dealing with a component
7836                      -- then set the origination type to Constrained Kit Demand
7837                      -- for DRP plans.
7838                      IF (NVL(p_atp_record.parent_item_id, p_atp_record.inventory_item_id) <> p_atp_record.inventory_item_id AND
7839                          NVL(MSC_ATP_PVT.G_PLAN_INFO_REC.plan_type,1) = 5) THEN
7840                          l_atp_insert_rec.origination_type := 47;
7841                          IF PG_DEBUG in ('Y', 'C') THEN
7842                            msc_sch_wb.atp_debug('----- ATP4drp Specific Debug Messages -----');
7843                            msc_sch_wb.atp_debug('ATP_Check: ' || 'DRP origination_type ='|| l_atp_insert_rec.origination_type);
7844                            msc_sch_wb.atp_debug('ATP_Check: ' || 'DRP parent_item_id ='|| p_atp_record.parent_item_id);
7845                            msc_sch_wb.atp_debug('ATP_Check: ' || 'DRP child_item_id ='|| p_atp_record.inventory_item_id);
7846                            msc_sch_wb.atp_debug('----- ATP4drp Specific Debug Messages -----');
7847                          END IF;
7848                      ELSE
7849                          l_atp_insert_rec.origination_type := 1;
7850                      END IF;
7851                      -- End ATP4drp
7852                      -- for performance reason, we call these function here and
7853                      -- then populate the pegging tree with the values
7854 
7855                      /* Modularize Item and Org Info */
7856                      -- Re-Use info instead of making unnecessary call.
7857                      l_inv_item_name := C_ITEM_INFO_REC.item_name;
7858                      MSC_ATP_PROC.get_global_org_info (p_atp_record.instance_id,
7859                                                     p_atp_record.organization_id);
7860                      l_org_code := G_ORG_INFO_REC.org_code;
7861 
7862                      l_atp_insert_rec.old_demand_id := null;
7863 
7864 
7865                      --s_cto_rearch
7866                      l_atp_insert_rec.Top_Model_line_id := p_atp_record.Top_Model_line_id;
7867                      l_atp_insert_rec.ATO_Parent_Model_Line_Id := p_atp_record.ATO_Parent_Model_Line_Id;
7868                      l_atp_insert_rec.Parent_line_id := p_atp_record.Parent_line_id;
7869                      l_atp_insert_rec.ATO_Model_Line_Id := p_atp_record.ATO_Model_Line_Id;
7870                      l_atp_insert_rec.wip_supply_type := p_atp_record.wip_supply_type;
7871                      l_atp_insert_rec.mand_comp_flag := p_atp_record.mand_comp_flag;
7872                      l_atp_insert_rec.wip_supply_type := p_atp_record.mand_comp_flag;
7873                      --e_cto_rearch
7874                      --plan by request date enhancement
7875                      l_atp_insert_rec.ship_set_name := p_atp_record.ship_set_name;
7876                      l_atp_insert_rec.arrival_set_name := p_atp_record.arrival_set_name;
7877                      l_atp_insert_rec.original_request_ship_date :=l_original_req_ship_date;
7878 
7879                      -- ship_rec_cal
7880                      IF p_search = 1 and p_parent_pegging_id is null THEN
7881                          IF  p_atp_record.ship_method <> '@@@' THEN
7882                             l_atp_insert_rec.ship_method := p_atp_record.ship_method;
7883                          ELSE
7884                             l_atp_insert_rec.ship_method := null;
7885                          END IF;
7886                      END IF;
7887                      l_atp_insert_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
7888                      l_atp_insert_rec.delivery_lead_time := l_delivery_lead_time;
7889 
7890                      MSC_ATP_DB_UTILS.Add_Mat_Demand(l_atp_insert_rec,
7891                                    p_plan_id,
7892                                    l_demand_class_flag,
7893                                    l_demand_id);
7894 
7895                      IF PG_DEBUG in ('Y', 'C') THEN
7896                         msc_sch_wb.atp_debug('ATP_Check: ' || 'after calling Add_Mat_Demand, l_demand_id ='||
7897                                 l_demand_id);
7898                      END IF;
7899 
7900                      -- populate insert rec to pegging tree for this demand
7901                      --bug 2422847: add demand pegging
7902 
7903                      l_pegging_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
7904                      l_pegging_rec.order_line_id:= MSC_ATP_PVT.G_ORDER_LINE_ID;
7905                      l_pegging_rec.parent_pegging_id:= p_parent_pegging_id;
7906                      l_pegging_rec.atp_level:= p_level;
7907                      l_pegging_rec.organization_id:= p_atp_record.organization_id;
7908                      l_pegging_rec.organization_code := l_org_code;
7909                      l_pegging_rec.identifier1:= p_atp_record.instance_id;
7910                      l_pegging_rec.identifier2 := p_plan_id;
7911                      l_pegging_rec.identifier3 := l_demand_id;
7912 
7913                      -- time_phased_atp changes begin
7914                      IF l_pf_atp = 'Y' THEN
7915                          l_pegging_rec.inventory_item_id:= p_atp_record.inventory_item_id;
7916                          l_pegging_rec.inventory_item_name := PF_ITEM_INFO_REC.item_name;
7917                      ELSE
7918                          l_pegging_rec.inventory_item_id:= p_atp_record.request_item_id;
7919                          l_pegging_rec.inventory_item_name := C_ITEM_INFO_REC.item_name;
7920                      END IF;
7921                      l_pegging_rec.aggregate_time_fence_date := p_atp_record.atf_date;
7922                      -- time_phased_atp changes end
7923 
7924                      l_pegging_rec.resource_id := NULL;
7925                      l_pegging_rec.resource_code := NULL;
7926                      l_pegging_rec.department_id := NULL;
7927                      l_pegging_rec.department_code := NULL;
7928                      l_pegging_rec.supplier_id := NULL;
7929                      l_pegging_rec.supplier_name := NULL;
7930                      l_pegging_rec.supplier_site_id := NULL;
7931                      l_pegging_rec.supplier_site_name := NULL;
7932                      l_pegging_rec.scenario_id:= p_scenario_id;
7933                      l_pegging_rec.supply_demand_source_type:= 6;
7934                      l_pegging_rec.supply_demand_quantity:=
7935                          p_atp_record.quantity_ordered ;
7936                       l_pegging_rec.supply_demand_type:= 1;
7937                        -- Bug 2625791, 2623816: consider PTF date passed from the top org.
7938                       l_pegging_rec.supply_demand_date:= GREATEST(l_requested_ship_date, MSC_ATP_PVT.G_PTF_DATE);
7939 
7940 
7941 		     -- dsting ATO 2465370
7942 		     --	 IF NVL(p_atp_record.override_flag, 'N') = 'Y'
7943 		     IF NVL(G_LINE_OVERRIDE_FLAG, 'N') = 'Y'
7944 			AND p_parent_pegging_id is null
7945 		     THEN
7946                         l_pegging_rec.required_date:= l_requested_ship_date;
7947                         --bug 3328421: Actual Supply demand date
7948                         l_pegging_rec.actual_supply_demand_date := l_requested_ship_date;
7949 		     ELSE
7950                         -- Bug 2748730. No need to move date to the end of as we will be doing that
7951 		        -- inside the procedure Add_Pegging
7952                         -- l_pegging_rec.required_date:= TRUNC(
7953 				-- GREATEST(l_requested_ship_date, MSC_ATP_PVT.G_PTF_DATE)) + MSC_ATP_PVT.G_END_OF_DAY;
7954                         --bug 3328421: required date always conatin the original req date
7955 			l_pegging_rec.required_date:= l_requested_ship_date;
7956                         l_pegging_rec.actual_supply_demand_date :=
7957 				GREATEST(l_requested_ship_date, MSC_ATP_PVT.G_PTF_DATE);
7958 		     END IF;
7959 
7960                       -- for demo:1153192
7961                       l_pegging_rec.constraint_flag := 'N';
7962 	              l_pegging_rec.component_identifier :=
7963                                  NVL(p_atp_record.component_identifier, MSC_ATP_PVT.G_COMP_LINE_ID);
7964 
7965                       l_pegging_rec.summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;
7966                       --- bug 2152184: For PF based ATP inventory_item_id field contains id for PF item
7967                       --- cto looks at pegging tree to place their demands. Since CTO expects to find
7968                       --  id for the requested item, we add the following column. CTO will now read from this column
7969                       l_pegging_rec.request_item_id := p_atp_record.request_item_id;
7970 
7971                       --s_cto_rearch
7972                       l_pegging_rec.dest_inv_item_id := C_ITEM_INFO_REC.dest_inv_item_id;
7973                       l_pegging_rec.base_model_id := p_atp_record.base_model_id;
7974                       --e_cto_rearch
7975                       l_pegging_rec.demand_class :=  p_atp_record.demand_class;
7976                       --4570421
7977                       l_pegging_rec.scaling_type                      := p_atp_record.scaling_type;
7978                       l_pegging_rec.scale_multiple                    := p_atp_record.scale_multiple;
7979                       l_pegging_rec.scale_rounding_variance           := p_atp_record.scale_rounding_variance;
7980                       l_pegging_rec.rounding_direction                := p_atp_record.rounding_direction;
7981                       l_pegging_rec.component_yield_factor            := p_atp_record.component_yield_factor; --4570421
7982                       l_pegging_rec.usage                             := p_atp_record.usage_qty; --4775920
7983                       l_pegging_rec.organization_type                 := p_atp_record.organization_type; --4775920
7984 
7985                       MSC_ATP_DB_UTILS.Add_Pegging(l_pegging_rec, l_pegging_id);
7986 
7987                  END IF;
7988               END IF;
7989               IF PG_DEBUG in ('Y', 'C') THEN
7990                  msc_sch_wb.atp_debug('ATP_Check: ' || 'l_net_demand := ' || l_net_demand);
7991               END IF;
7992 
7993               IF  NVL(p_atp_record.top_tier_org_flag, 2) = 1 THEN
7994                   --for top tier org l_pegging_id should be mapped to demand in
7995                   -- the top org. Since demand is created in atp)_check_subst procedure
7996                   -- and corresponding peggign id is passed as p_parent_pegging_id
7997                   --- we mapped l_pegging_id to p_parent_pegging_id
7998                   l_pegging_id := p_parent_pegging_id;
7999 
8000                   --5088719
8001                   l_atp_insert_rec.instance_id := p_atp_record.instance_id;
8002                      /* Modularize Item and Org Info */
8003                      -- time_phased_atp changes begin
8004                   l_atp_insert_rec.inventory_item_id := PF_ITEM_INFO_REC.dest_inv_item_id;
8005                   l_atp_insert_rec.request_item_id := C_ITEM_INFO_REC.dest_inv_item_id;
8006                   l_atp_insert_rec.atf_date := p_atp_record.atf_date;
8007                   l_atp_insert_rec.atf_date_quantity := 0; -- check this??
8008                   l_atp_insert_rec.requested_date_quantity := l_requested_date_quantity;
8009                      -- time_phased_atp changes end
8010 
8011                   l_atp_insert_rec.organization_id := p_atp_record.organization_id;
8012                   l_atp_insert_rec.identifier := p_atp_record.identifier;
8013                   l_atp_insert_rec.demand_source_type:=nvl(p_atp_record.demand_source_type, 2);
8014                   l_atp_insert_rec.demand_source_header_id :=
8015                                      nvl(p_atp_record.demand_source_header_id, -1);
8016                   l_atp_insert_rec.demand_source_delivery :=
8017                                      p_atp_record.demand_source_delivery;
8018 
8019                   l_atp_insert_rec.quantity_ordered :=  p_atp_record.quantity_ordered;
8020 
8021                   l_atp_insert_rec.requested_ship_date := l_requested_ship_date;
8022                   l_atp_insert_rec.demand_class := p_atp_record.demand_class;
8023                   l_atp_insert_rec.refresh_number := p_refresh_number; -- summary enhancement p_atp_record.refresh_number;
8024                   l_atp_insert_rec.order_number := p_atp_record.order_number;
8025 
8026 
8027                   l_atp_insert_rec.origination_type := 30; --1; 5088719
8028                      -- for performance reason, we call these function here and
8029                      -- then populate the pegging tree with the values
8030 
8031                      /* Modularize Item and Org Info */
8032                      -- Re-Use info instead of making unnecessary call.
8033                   l_inv_item_name := C_ITEM_INFO_REC.item_name;
8034                   MSC_ATP_PROC.get_global_org_info (p_atp_record.instance_id,
8035                                                     p_atp_record.organization_id);
8036                   l_org_code := G_ORG_INFO_REC.org_code;
8037 
8038                   l_atp_insert_rec.old_demand_id := null;
8039 
8040 
8041                      --s_cto_rearch
8042                   l_atp_insert_rec.Top_Model_line_id := p_atp_record.Top_Model_line_id;
8043                   l_atp_insert_rec.ATO_Parent_Model_Line_Id := p_atp_record.ATO_Parent_Model_Line_Id;
8044                   l_atp_insert_rec.Parent_line_id := p_atp_record.Parent_line_id;
8045                   l_atp_insert_rec.ATO_Model_Line_Id := p_atp_record.ATO_Model_Line_Id;
8046                   l_atp_insert_rec.wip_supply_type := p_atp_record.wip_supply_type;
8047                   l_atp_insert_rec.mand_comp_flag := p_atp_record.mand_comp_flag;
8048                   l_atp_insert_rec.wip_supply_type := p_atp_record.mand_comp_flag;
8049                      --e_cto_rearch
8050                      --plan by request date enhancement
8051                   l_atp_insert_rec.ship_set_name := p_atp_record.ship_set_name;
8052                   l_atp_insert_rec.arrival_set_name := p_atp_record.arrival_set_name;
8053                   l_atp_insert_rec.original_request_ship_date :=l_original_req_ship_date;
8054 
8055                      -- ship_rec_cal
8056                   IF p_search = 1 and p_parent_pegging_id is null THEN
8057                       IF  p_atp_record.ship_method <> '@@@' THEN
8058                          l_atp_insert_rec.ship_method := p_atp_record.ship_method;
8059                       ELSE
8060                          l_atp_insert_rec.ship_method := null;
8061                       END IF;
8062                   END IF;
8063                   l_atp_insert_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
8064                   l_atp_insert_rec.delivery_lead_time := l_delivery_lead_time;
8065 
8066                   l_demand_id := p_atp_record.subs_demand_id; --5088719
8067 
8068               END IF;
8069               IF PG_DEBUG in ('Y', 'C') THEN
8070                  msc_sch_wb.atp_debug('ATP_Check: ' || 'l_pegging_id := ' || l_pegging_id);
8071               END IF;
8072               /* Modularize Item and Org Info */
8073               -- Re-Use info instead of making unnecessary call.
8074               l_inv_item_name := C_ITEM_INFO_REC.item_name;
8075               /*l_inv_item_name := MSC_ATP_FUNC.get_inv_item_name(p_atp_record.instance_id,
8076                                          p_atp_record.inventory_item_id,
8077                                          p_atp_record.organization_id);
8078 
8079                Modularize Item and Org Info */
8080                MSC_ATP_PROC.get_global_org_info (p_atp_record.instance_id,
8081                                                  p_atp_record.organization_id );
8082               l_org_code := G_ORG_INFO_REC.org_code;
8083               /*l_org_code := MSC_ATP_FUNC.get_org_code(p_atp_record.instance_id,
8084                                           p_atp_record.organization_id);
8085                Modularize Item and Org Info */
8086               l_atp_date_this_level := null;
8087               --MSC_ATP_PVT.G_DEMAND_PEGGING_ID := p_parent_pegging_id;
8088               --5026618 Start 4902658 Fix 5216528
8089               l_substitutes_rec.inventory_item_id.EXTEND;
8090               l_substitutes_rec.pegging_id.EXTEND;
8091               l_substitutes_rec.sub_atp_qty.EXTEND;
8092               l_substitutes_rec.demand_id.EXTEND;
8093               l_substitutes_rec.atf_date_quantity.EXTEND; --5283809
8094               l_substitutes_rec.quantity_ordered.EXTEND;
8095               l_substitutes_rec.pf_item_id.EXTEND;
8096 
8097               l_substitutes_rec.demand_id(1) := l_demand_id; --4902658 / 5216528
8098               l_substitutes_rec.inventory_item_id(1) := p_atp_record.Request_Item_Id;
8099               l_substitutes_rec.pegging_id(1) := p_parent_pegging_id;
8100               l_substitutes_rec.sub_atp_qty(1) :=  LEAST(GREATEST(l_requested_date_quantity,0),
8101                                                          p_atp_record.quantity_ordered);
8102               --5026618 Ends 4902658/5216528 Fix
8103           ELSE
8104              --s_cto_rearch
8105              IF (l_atp_flag in ('Y', 'C')) and
8106                                  NOT(C_ITEM_INFO_REC.bom_item_type = 1 and
8107                                     --bug 3423277: Do ATP Check on model when
8108                                     --ATP flag for model = 'Y' and atp_comp_flag in ('R', 'N')
8109                                     -- as in these cases ATP on model will not be done  when
8110                                     -- we are checking component's availability
8111                                     -- Since we are using 'Not' clause, we check for other values
8112                                     ( C_ITEM_INFO_REC.atp_comp_flag in ('Y', 'C') and
8113                                       p_atp_record.parent_item_id is null)) THEN
8114               -- for bug 1124538:
8115               -- we need to get atp info even if requested ship date < sysdate
8116               -- if this is the top level and backward,
8117               -- otherwise we only do it if requested ship date >= sysdate
8118 
8119               IF ((l_requested_ship_date >= l_sysdate) OR
8120                  (p_search = BACKWARD_SCHEDULING AND
8121                   p_parent_pegging_id is null) OR
8122                  (p_search = FORWARD_SCHEDULING)) THEN
8123 
8124                  --- bug 2178544: In forward schedulling for components we start looking for
8125                  --- availabilty on greatest of (PTF of parent , req_date)
8126                  --- Here PTF_date is ptf date for parent i.e if we came here from get_comp_req then
8127                  --- it is PTF for parent item. If we came here for transfer then PTF will be that of last org
8128                  IF PG_DEBUG in ('Y', 'C') THEN
8129                     msc_sch_wb.atp_debug('ATP_Check: ' || 'G_PTF_DATE from  parent := ' || MSC_ATP_PVT.G_PTF_DATE);
8130                     msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atp_request_date := ' || l_atp_request_date);
8131                  END IF;
8132 
8133                  --diag_atp
8134                  l_get_mat_in_rec.rounding_control_flag := l_rounding_control_flag;
8135                  l_get_mat_in_rec.dest_inv_item_id := l_dest_inv_item_id;
8136                  l_get_mat_in_rec.plan_name := p_atp_record.plan_name;
8137 
8138 	         IF ((p_search = FORWARD_SCHEDULING) AND
8139                      --- Enhance CTO Phase 1 Req #17
8140                        -- Support Forward Stealing for components of ATO model in
8141                        -- Model's sourcing organization.
8142                      --support forward scheduling for all level
8143                      --(p_parent_pegging_id IS NULL)  AND
8144                     --- End Enhance CTO Phase 1 Req #17
8145 		    (MSC_ATP_PVT.G_INV_CTP = 4) AND
8146                     (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
8147                     (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
8148                     (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1)) THEN
8149 
8150                     /*
8151                     --- Enhance CTO Phase 1 Req #17
8152                        -- Support Forward Stealing for components of ATO model in
8153                        -- Model's sourcing organization.
8154                     IF (NVL(C_ITEM_INFO_REC.cto_source_org_id, -1) = p_atp_record.organization_id ) THEN
8155                        l_parent_peg_forward := p_parent_pegging_id;
8156                     ELSE
8157                        l_parent_peg_forward := 0;
8158                     END IF;
8159                     */
8160 
8161                     l_parent_peg_forward := p_parent_pegging_id;
8162                     IF PG_DEBUG in ('Y', 'C') THEN
8163                        msc_sch_wb.atp_debug('ATP_Check: ' || 'Parent Pegging Id :' || l_parent_peg_forward);
8164                        msc_sch_wb.atp_debug('ATP_Check: ' || 'Future Pegging Id :' || l_future_pegging_id);
8165                        msc_sch_wb.atp_debug('ATP_Check: ' || 'Demand Pegging Id :' || MSC_ATP_PVT.G_DEMAND_PEGGING_ID);
8166                        msc_sch_wb.atp_debug('ATP_Check: ' || 'Global Future Pegging Id :' || MSC_ATP_PVT.G_CTO_FORWARD_DMD_PEG);
8167                     END IF;
8168 
8169                     l_get_mat_in_rec.parent_bom_item_type := p_atp_record.parent_bom_item_type;
8170                     l_get_mat_in_rec.bom_item_type := C_ITEM_INFO_REC.bom_item_type;
8171                     l_get_mat_in_rec.replenish_to_ord_flag := C_ITEM_INFO_REC.replenish_to_ord_flag;
8172                     l_get_mat_in_rec.parent_repl_order_flag := p_atp_record.parent_repl_order_flag;
8173                     l_get_mat_in_rec.ato_model_line_id := p_atp_record.ato_model_line_id;
8174                     l_get_mat_in_rec.shipping_cal_code := p_atp_record.shipping_cal_code;
8175                     l_get_mat_in_rec.receiving_cal_code := p_atp_record.receiving_cal_code; -- Bug 3826234
8176                     l_get_mat_in_rec.intransit_cal_code := p_atp_record.intransit_cal_code; -- Bug 3826234
8177                     l_get_mat_in_rec.manufacturing_cal_code := p_atp_record.manufacturing_cal_code; -- Bug 3826234
8178                     l_get_mat_in_rec.to_organization_id := p_atp_record.to_organization_id; -- Bug 3826234
8179                     -- Bug 3371817 - Pass calendar code to calculate sys_next_date
8180                     --               In make case this will actually contain OMC for lower level components
8181 
8182 		    MSC_AATP_REQ.Get_Forward_Material_Atp(
8183 				p_atp_record.instance_id,
8184 				p_plan_id,
8185 				p_level + 1,
8186 				p_atp_record.identifier,
8187 				p_atp_record.demand_source_type,--cmro
8188 				p_scenario_id,
8189 				p_atp_record.inventory_item_id,
8190 				p_atp_record.request_item_id, -- For time_phased_atp
8191 				p_atp_record.organization_id,
8192 				C_ITEM_INFO_REC.item_name,   -- Modular re-use
8193 				PF_ITEM_INFO_REC.item_name,  -- For time_phased_atp
8194 				l_requested_ship_date,
8195 				p_atp_record.quantity_ordered,
8196 				p_atp_record.demand_class,
8197 				l_requested_date_quantity,
8198 				l_atf_date_qty, -- For time_phased_atp
8199 				l_atp_date_this_level,
8200 				l_atp_date_quantity_this_level,
8201 				l_atp_pegging_tab,
8202 				l_return_status,
8203 				l_used_available_quantity, --bug3409973
8204                                 --diag_atp,
8205                                 null,
8206                                 l_get_mat_in_rec,
8207                                 l_get_mat_out_rec,
8208                                 p_atp_record.atf_date, -- For time_phased_atp
8209                                 p_atp_record.order_number,
8210                                 p_refresh_number,
8211                                 l_parent_peg_forward);
8212 
8213                      IF p_parent_pegging_id is not null THEN
8214 
8215                         l_pegging_id := l_get_mat_out_rec.demand_pegging_id;
8216                          --bug3432341: store demand id for demands added in forward scheduling
8217                         l_demand_id := l_get_mat_out_rec.demand_id;
8218 
8219                      ELSE
8220 
8221                         l_pegging_id := MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
8222 
8223                      END IF;
8224                      p_atp_record.atf_date_quantity := NVL(l_atf_date_qty, 0);
8225 
8226                      --- End Enhance CTO Phase 1 Req #17
8227 	             -- print the results from Get_Forward_Material_Atp
8228 	             IF PG_DEBUG in ('Y', 'C') THEN
8229 	                msc_sch_wb.atp_debug('ATP_Check: ' || '********** Result from Get_Forward_Material_Atp **********');
8230 	                msc_sch_wb.atp_debug('ATP_Check: ' || 'l_used_available_quantity : '|| l_used_available_quantity); --bug3409973
8231 	             END IF;
8232 
8233 	         ELSE 		-- IF MSC_ATP_PVT.G_ALLOCATION_METHOD = 1
8234                         -- time_phased_atp changes begin
8235                         l_mat_atp_info_rec.instance_id               := p_atp_record.instance_id;
8236                         l_mat_atp_info_rec.plan_id                   := p_plan_id;
8237                         l_mat_atp_info_rec.level                     := p_level + 1;
8238                         l_mat_atp_info_rec.identifier                := p_atp_record.identifier;
8239                         l_mat_atp_info_rec.scenario_id               := p_scenario_id;
8240                         l_mat_atp_info_rec.inventory_item_id         := p_atp_record.inventory_item_id;
8241                         l_mat_atp_info_rec.request_item_id           := p_atp_record.request_item_id;
8242                         l_mat_atp_info_rec.organization_id           := p_atp_record.organization_id;
8243                         l_mat_atp_info_rec.requested_date            := l_requested_ship_date;
8244                         l_mat_atp_info_rec.quantity_ordered          := p_atp_record.quantity_ordered;
8245                         l_mat_atp_info_rec.demand_class              := p_atp_record.demand_class;
8246                         l_mat_atp_info_rec.insert_flag               := p_atp_record.insert_flag;
8247                         l_mat_atp_info_rec.rounding_control_flag     := l_get_mat_in_rec.rounding_control_flag;
8248                         l_mat_atp_info_rec.dest_inv_item_id          := l_get_mat_in_rec.dest_inv_item_id;
8249                         l_mat_atp_info_rec.infinite_time_fence_date  := l_get_mat_in_rec.infinite_time_fence_date;
8250                         l_mat_atp_info_rec.plan_name                 := l_get_mat_in_rec.plan_name;
8251                         l_mat_atp_info_rec.optimized_plan            := l_get_mat_in_rec.optimized_plan;
8252                         l_mat_atp_info_rec.substitution_window       := l_substitution_window;
8253                         l_mat_atp_info_rec.atf_date                  := p_atp_record.atf_date;
8254                         l_mat_atp_info_rec.refresh_number            := p_refresh_number;   -- For summary enhancement
8255                         l_mat_atp_info_rec.shipping_cal_code         := p_atp_record.shipping_cal_code;
8256                         -- Bug 3371817 - Pass calendar code to calculate sys_next_date
8257                         --               In make case this will actually contain OMC for lower level components
8258 
8259                         MSC_ATP_REQ.Get_Material_Atp_Info(
8260                                 l_mat_atp_info_rec,
8261                                 l_atp_period,
8262                                 l_atp_supply_demand,
8263                                 l_return_status);
8264 
8265                         l_atf_date_qty                               := l_mat_atp_info_rec.atf_date_quantity;
8266                         l_atp_date_this_level                        := l_mat_atp_info_rec.atp_date_this_level;
8267                         l_atp_date_quantity_this_level               := l_mat_atp_info_rec.atp_date_quantity_this_level;
8268                         l_get_mat_out_rec.atp_rule_name              := l_mat_atp_info_rec.atp_rule_name;
8269                         l_get_mat_out_rec.infinite_time_fence_date   := l_mat_atp_info_rec.infinite_time_fence_date;
8270                         l_requested_date_quantity                    := l_mat_atp_info_rec.requested_date_quantity;
8271                         p_atp_record.atf_date_quantity               := l_atf_date_qty;
8272                         -- time_phased_atp changes end
8273                         /* rajjain changes for re-review comments
8274                            removed check for rule based AATP as this should be set
8275                            for all scenarios
8276                         IF MSC_ATP_PVT.G_ALLOCATION_METHOD = 2 THEN
8277                           l_used_available_quantity := l_mat_atp_info_rec.atp_date_quantity_this_level;
8278                         END IF; --bug3409973*/
8279                         l_used_available_quantity := l_mat_atp_info_rec.atp_date_quantity_this_level;
8280 
8281 	             -- print the results from Get_Material_Atp_Info
8282 	             IF PG_DEBUG in ('Y', 'C') THEN
8283 	                msc_sch_wb.atp_debug('ATP_Check: ' || '********** Result from Get_Material_Atp_Info **********');
8284 	             END IF;
8285 
8286 	         END IF;	-- IF MSC_ATP_PVT.G_ALLOCATION_METHOD = 1
8287 
8288                  -- Bug 3782472
8289                  IF PG_DEBUG in ('Y', 'C') THEN
8290                     msc_sch_wb.atp_debug('p_search '||p_search);
8291                     msc_sch_wb.atp_debug('MSC_ATP_PVT.G_PTF_DATE '||MSC_ATP_PVT.G_PTF_DATE);
8292                     msc_sch_wb.atp_debug('l_ptf_date '||l_ptf_date);
8293                     msc_sch_wb.atp_debug('l_requested_ship_date '||l_requested_ship_date);
8294                     msc_sch_wb.atp_debug('l_net_demand '||l_net_demand);
8295                  END IF;
8296                  IF p_search = FORWARD_SCHEDULING AND
8297                           l_requested_date_quantity >= p_atp_record.quantity_ordered and
8298                               MSC_ATP_PVT.G_PTF_DATE > l_requested_ship_date THEN
8299                     l_net_demand := l_net_demand;
8300                     l_atp_comp_flag := 'N';
8301                  ELSE
8302 
8303                     l_net_demand := p_atp_record.quantity_ordered -
8304                                               greatest(l_requested_date_quantity, 0);
8305                  END IF;
8306 
8307 	         IF PG_DEBUG in ('Y', 'C') THEN
8308 	            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_return_status : '|| l_return_status);
8309 	            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_requested_ship_date : '|| l_requested_ship_date);
8310 	            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_requested_date_quantity : '|| l_requested_date_quantity);
8311 	            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atp_date_this_level : '|| l_atp_date_this_level);
8312 	            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atp_date_quantity_this_level : '|| l_atp_date_quantity_this_level);
8313 	            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_used_available_quantity : '|| l_used_available_quantity);
8314 	            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atf_date_qty : '|| l_atf_date_qty);
8315 	            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atp_pegging_tab : '|| l_atp_pegging_tab.COUNT);
8316 	         END IF;
8317               ELSE
8318                IF PG_DEBUG in ('Y', 'C') THEN
8319                   msc_sch_wb.atp_debug('ATP_Check: ' || 'did not call get_material_atp_info');
8320                END IF;
8321                l_net_demand := p_atp_record.quantity_ordered;
8322                l_requested_date_quantity := 0.0; -- cchen 08/31
8323 
8324               END IF;
8325              ELSE
8326                /* --bug 4428128 : move this code down
8327                l_net_demand := p_atp_record.quantity_ordered;
8328                -- Bug 1587015, ngoel 1/19/2001
8329                l_requested_date_quantity := 0.0;
8330                */
8331 
8332                -- Bug 1566260, Initialize atp_date_this_level and atp_date_quantity_this_level to
8333                -- infinite_time_fence_date and INFINITE_NUMBER to avoid them to be null later on.
8334 
8335 	       IF PG_DEBUG in ('Y', 'C') THEN
8336 	          msc_sch_wb.atp_debug('ATP_Check: ' || 'Initialize atp_date and atp_quantity_this_level');
8337 	       END IF;
8338 
8339                /* s_cto_rearch: chnage call to MSC_ATP_PROC.get_infinite_time_fence_date procedure
8340                l_atp_date_this_level := GREATEST(MSC_ATP_FUNC.get_infinite_time_fence_date(
8341                            p_atp_record.instance_id, p_atp_record.inventory_item_id,
8342                            p_atp_record.organization_id,p_plan_id), l_requested_ship_date);
8343                 e_cto_rearch*/
8344 
8345                --bug 4394904: Store ITF in seperate variable
8346                MSC_ATP_PROC.get_infinite_time_fence_date(
8347                             p_atp_record.instance_id, p_atp_record.inventory_item_id,
8348                             p_atp_record.organization_id,p_plan_id,
8349                             --l_atp_date_this_level, l_atp_rule_name);
8350                             l_infinite_time_fence, l_atp_rule_name);
8351                l_atp_date_this_level := GREATEST(l_infinite_time_fence, l_requested_ship_date);
8352 
8353 	       IF l_atp_date_this_level IS NOT NULL THEN
8354                   l_atp_date_quantity_this_level := INFINITE_NUMBER;
8355 	       END IF;
8356 
8357                --bug 4428128: if request date > IFT then do not explode sources
8358                IF l_requested_ship_date >= l_infinite_time_fence THEN
8359                    l_requested_date_quantity := INFINITE_NUMBER;
8360                    l_net_demand := 0;
8361                ELSE
8362                    l_net_demand := p_atp_record.quantity_ordered;
8363                    -- Bug 1587015, ngoel 1/19/2001
8364                    l_requested_date_quantity := 0.0;
8365                END IF;
8366 
8367 	       IF PG_DEBUG in ('Y', 'C') THEN
8368 	          msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atp_date_this_level : '|| l_atp_date_this_level);
8369 	          msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atp_date_quantity_this_level : '|| l_atp_date_quantity_this_level);
8370 	          msc_sch_wb.atp_debug('ATP_Check: ' || 'l_used_available_quantity : '|| l_used_available_quantity); --bug3409973
8371 	       END IF;
8372 
8373              END IF;
8374 
8375 	     -- print the net_demand
8376 	     IF PG_DEBUG in ('Y', 'C') THEN
8377 	        msc_sch_wb.atp_debug('ATP_Check: ' || 'l_net_demand : '|| to_char(l_net_demand));
8378 	     END IF;
8379 
8380              -- cchen 1238941
8381 
8382              -- this should be a good place to do substitute.
8383              -- we change the logic so that we do the substitutes check
8384              -- before we populate the supply pegging.
8385              -- for the primary. however, we need to populate the demand
8386              -- pegging tree so that the order in the pegging tree will be ok.
8387 
8388                -- prepare the insert record
8389                -- no matter it is a demand or not, we need to insert this
8390                -- record into database since it is a recursive procedure
8391                -- and we will rollback in ATP procedure if it is not a
8392                -- demand.
8393                --- Enhance CTO Phase 1 Req  #16 and #17
8394 
8395              IF NOT((p_search = FORWARD_SCHEDULING) AND
8396                     (MSC_ATP_PVT.G_INV_CTP = 4) AND
8397                     (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
8398                     (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
8399                     (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1) and l_atp_flag = 'Y'
8400 
8401                   /*---bug 3524230 ---
8402                    Demand in demand priority allocated ATP for items with
8403                    atp flag is 'Y'  is added in get_forward_material_ATP info procedure.
8404                    The above condition restricts demand addition for such items.
8405 
8406                    If model's ATP flag is yes then ATP on model is performed only when
8407                    that model is made as model's supply statement is statement of resource capacity.
8408                    Therefore, resource capacity is used only when model is being made. Therefore,
8409                    the supply chian for model would be  POD for Model (Step 1) --> Make PO for Model
8410                       (Step 2) ---> POD for model + other components (step 3)
8411 
8412                    We do not need to check availability for model but do need to add demand in setp 1.
8413                    We do not call get_forward_material ATP info because we have a condition to not
8414                    call the procedure if we are in step 1. We were not adding demand here because
8415                    of above condition "l_atp_flag = 'Y'". As a result, demand for model was not getting added.
8416                    To correct this problem, we added the following condition.
8417                    If we are coming for second time for model then parent_item_is is populated.
8418                    In step 1 the condition below becomes false due
8419                    to null parent_item_id and the overall condition becomes true. Therefore, demand
8420                    for model gets added correctly. In setp 3, the demand becomes tru and hence overall
8421                    condition becomes false. Therfore, demand doesn't get added here but gets added
8422                    in get_forward_material_info. Thus, demand for model is maintained correctly.
8423 
8424                    */
8425                    AND NOT(C_ITEM_INFO_REC.bom_item_type = 1 and
8426                                     ( C_ITEM_INFO_REC.atp_comp_flag in ('Y', 'C') and
8427                                       p_atp_record.parent_item_id is null))) THEN
8428                 -- Only Add Create Demand Record and Pegging when not dealing with Forward Stealing
8429                l_atp_insert_rec.instance_id := p_atp_record.instance_id;
8430                /* Modularize Item and Org Info */
8431                -- time_phased_atp changes begin
8432                IF G_INV_CTP = 5 THEN
8433                   l_atp_insert_rec.inventory_item_id := C_ITEM_INFO_REC.dest_inv_item_id;
8434                ELSE
8435                   l_atp_insert_rec.inventory_item_id := PF_ITEM_INFO_REC.dest_inv_item_id;
8436                END IF;
8437                l_atp_insert_rec.request_item_id := C_ITEM_INFO_REC.dest_inv_item_id;
8438                l_atp_insert_rec.atf_date := p_atp_record.atf_date;
8439                l_atp_insert_rec.atf_date_quantity := l_atf_date_qty;
8440                l_atp_insert_rec.requested_date_quantity := l_requested_date_quantity;
8441                -- time_phased_atp changes end
8442 
8443                 -- No need to call function below.
8444                /*l_atp_insert_rec.inventory_item_id := MSC_ATP_FUNC.get_inv_item_id
8445                                                    (p_atp_record.instance_id,
8446                                                    --2152184
8447                                                    --p_atp_record.request_item_id,
8448 						   l_request_item_id,
8449                                                    null,
8450                                                    p_atp_record.organization_id);
8451                 Modularize Item and Org Info */
8452                l_atp_insert_rec.organization_id := p_atp_record.organization_id;
8453                l_atp_insert_rec.identifier := p_atp_record.identifier;
8454                l_atp_insert_rec.demand_source_type:=nvl(p_atp_record.demand_source_type, 2);
8455                l_atp_insert_rec.demand_source_header_id :=
8456                                nvl(p_atp_record.demand_source_header_id, -1);
8457                l_atp_insert_rec.demand_source_delivery :=
8458                                p_atp_record.demand_source_delivery;
8459 
8460                -- AATP: need to do something later on the reset the quantity
8461                -- back
8462 
8463                --steal_before_ctp
8464                --- if we are in allocated ATP and we want to do stealing then
8465                --- we will create demand for what is available. This way we wouldn't need to
8466                --- adjust the demand
8467 
8468                IF (l_net_demand > 0)
8469                     --stealing will be supported at all levels
8470                     -- AND ((p_parent_pegging_id IS NULL) OR -- Bug 2745728 Stealing for components.
8471                     --  NVL(C_ITEM_INFO_REC.cto_source_org_id, -1) = p_atp_record.organization_id )
8472                    AND (p_search =1)
8473                    AND (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND (l_atp_flag = 'Y')
8474                    --bug 3524230: When ATP flag for model is 'Y' then we do ATP on this model
8475                    --- only when we are making it. Therefore, we need to add full quantity
8476                    -- when not doing ATP check on the model
8477                    AND NOT(C_ITEM_INFO_REC.bom_item_type = 1 and
8478                                     ( C_ITEM_INFO_REC.atp_comp_flag in ('Y', 'C') and
8479                                       p_atp_record.parent_item_id is null)) THEN
8480                   l_atp_insert_rec.quantity_ordered :=  LEAST(GREATEST(l_requested_date_quantity, 0),
8481                                                                       p_atp_record.quantity_ordered);
8482 	          IF PG_DEBUG in ('Y', 'C') THEN
8483            	      msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atp_insert_rec.quantity_ordered : '||
8484                                                                     l_atp_insert_rec.quantity_ordered);
8485            	      msc_sch_wb.atp_debug('ATP_Check: ' || 'p_atp_record.quantity_ordered : '||
8486                                                                     p_atp_record.quantity_ordered);
8487            	      msc_sch_wb.atp_debug('ATP_Check: ' || 'l_requested_date_quantity : '||
8488                                                                     l_requested_date_quantity);
8489            	      msc_sch_wb.atp_debug('ATP_Check: ' || 'steal_before_ctp condition : ' );
8490 	          END IF;
8491                ELSE
8492                   --5220357
8493                   /*
8494                   --5151972 insert demand for primary component = demand quantity found.
8495                   IF (p_search = BACKWARD_SCHEDULING) and (p_parent_pegging_id is not null
8496                        and MSC_ATP_PVT.G_SUB_COMP = 'Y')
8497                        --5192094 added check to see if actually we have calculated material availiability
8498                        and (l_atp_flag = 'Y') THEN
8499                      IF PG_DEBUG in ('Y', 'C') THEN
8500                         msc_sch_wb.atp_debug('ATP_Check: ' || 'Inside comp substitution ');
8501                      END IF;
8502 
8503                      l_atp_insert_rec.quantity_ordered :=  LEAST(GREATEST(l_requested_date_quantity, 0),
8504                                                                       p_atp_record.quantity_ordered);
8505                   ELSE
8506                      l_atp_insert_rec.quantity_ordered :=  p_atp_record.quantity_ordered;
8507                   END IF;
8508                   */
8509                   l_atp_insert_rec.quantity_ordered :=  p_atp_record.quantity_ordered;
8510 	          IF PG_DEBUG in ('Y', 'C') THEN
8511            	      msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atp_insert_rec.quantity_ordered : '||
8512                                                                     l_atp_insert_rec.quantity_ordered);
8513            	      msc_sch_wb.atp_debug('ATP_Check: ' || 'p_atp_record.quantity_ordered : '||
8514                                                                     p_atp_record.quantity_ordered);
8515            	      msc_sch_wb.atp_debug('ATP_Check: ' || 'steal_before_ctp Else : ' );
8516 	          END IF;
8517                END IF;
8518 
8519                l_atp_insert_rec.requested_ship_date := l_requested_ship_date;
8520                l_atp_insert_rec.demand_class := p_atp_record.demand_class;
8521                l_atp_insert_rec.refresh_number := p_refresh_number; -- summary enhancement p_atp_record.refresh_number;
8522                l_atp_insert_rec.order_number := p_atp_record.order_number;
8523 
8524                /* Enhance CTO Phase 1 Req #16 */
8525                -- Create Sales Orders instead of Plan Order Demands for
8526                -- CTO items and set the order line id for the item
8527                -- only for the appropriate organizations.
8528                IF (p_parent_pegging_id is NULL) or MSC_ATP_PVT.G_INV_CTP = 5 THEN
8529                    --NVL(C_ITEM_INFO_REC.cto_source_org_id, -1) = p_atp_record.organization_id ) THEN
8530 
8531                  -- this is top level demand, which we should consider it
8532                  -- as sales order demand.
8533 
8534                  IF PG_DEBUG in ('Y', 'C') THEN
8535                     msc_sch_wb.atp_debug('Either ODS based ATP or top level demand. Add as a SO Demand');
8536                  END IF;
8537 
8538 	         -- Modified by ngoel on 1/12/2001 for origination_type = 30
8539                  --l_atp_insert_rec.origination_type := 6;
8540                  l_atp_insert_rec.origination_type := 30;
8541 
8542                  l_atp_insert_rec.demand_source_line := p_atp_record.demand_source_line;
8543                  l_atp_insert_rec.old_demand_id := p_atp_record.old_demand_id;
8544                ELSE
8545                  -- this is not the top level demand, which we should consider it
8546                  -- as planned order demand
8547 
8548                  -- ATP4drp If we are dealing with a component
8549                  -- then set the origination type to Constrained Kit Demand
8550                  -- for DRP plans.
8551                  IF (NVL(p_atp_record.parent_item_id, p_atp_record.inventory_item_id) <> p_atp_record.inventory_item_id AND
8552                       NVL(MSC_ATP_PVT.G_PLAN_INFO_REC.plan_type,1) = 5) THEN
8553                       l_atp_insert_rec.origination_type := 47;
8554                       IF PG_DEBUG in ('Y', 'C') THEN
8555                           msc_sch_wb.atp_debug('----- ATP4drp Specific Debug Messages -----');
8556                           msc_sch_wb.atp_debug('ATP_Check: ' || 'DRP origination_type ='|| l_atp_insert_rec.origination_type);
8557                           msc_sch_wb.atp_debug('ATP_Check: ' || 'DRP parent_item_id ='|| p_atp_record.parent_item_id);
8558                           msc_sch_wb.atp_debug('ATP_Check: ' || 'DRP child_item_id ='|| p_atp_record.inventory_item_id);
8559                           msc_sch_wb.atp_debug('----- ATP4drp Specific Debug Messages -----');
8560                       END IF;
8561                  ELSE
8562                       l_atp_insert_rec.origination_type := 1;
8563                  END IF;
8564                  -- End ATP4drp
8565                  IF (p_atp_record.parent_bom_item_type in (1, 4) and p_atp_record.parent_repl_order_flag = 'Y')
8566                     --parent is model entity
8567                     OR (C_ITEM_INFO_REC.bom_item_type in (1, 4) and C_ITEM_INFO_REC.replenish_to_ord_flag = 'Y') THEN
8568                      ---always add line id for lower level model components
8569                      l_atp_insert_rec.demand_source_line := p_atp_record.demand_source_line;
8570                  END IF;
8571 
8572                END IF;
8573                /* End Enhance CTO Phase 1 Req #16 */
8574                -- for performance reason, we call these function here and
8575                -- then populate the pegging tree with the values
8576 
8577                -- for performance reason, we call these function here and
8578                -- then populate the pegging tree with the values
8579 
8580                /* Modularize Item and Org Info */
8581                -- Re-Use info instead of making unnecessary call.
8582                l_inv_item_name := C_ITEM_INFO_REC.item_name;
8583                /*l_inv_item_name := MSC_ATP_FUNC.get_inv_item_name(p_atp_record.instance_id,
8584                                          p_atp_record.inventory_item_id,
8585                                          p_atp_record.organization_id);
8586 
8587                 Modularize Item and Org Info */
8588                MSC_ATP_PROC.get_global_org_info (p_atp_record.instance_id,
8589                                                   p_atp_record.organization_id);
8590                l_org_code := G_ORG_INFO_REC.org_code;
8591                /* l_org_code := MSC_ATP_FUNC.get_org_code(p_atp_record.instance_id,
8592                                           p_atp_record.organization_id);
8593                 Modularize Item and Org Info */
8594 
8595                IF p_search = 1 and p_parent_pegging_id is null THEN
8596                   --- we need to pass old demand id so that we can update that demand
8597                   --- this will happen in case of reschedulling
8598                   l_atp_insert_rec.old_demand_id := p_atp_record.old_demand_id;
8599                ELSE
8600                   l_atp_insert_rec.old_demand_id := null;
8601                END IF;
8602 
8603 
8604    -- temporary
8605 	       IF PG_DEBUG in ('Y', 'C') THEN
8606 	          msc_sch_wb.atp_debug('ATP_Check: ' || 'temp l_net_demand : '|| l_net_demand);
8607 	       END IF;
8608 	       --optional_fw Only in first iteration code should come here or while adding POD lower level
8609                IF ((p_search = 1) AND (MSC_ATP_PVT.G_OPTIONAL_FW is null )) OR
8610                   ((p_search= 2 AND p_parent_pegging_id is not null)
8611                   OR (MSC_ATP_PVT.G_OPTIONAL_FW is not null AND p_parent_pegging_id is not null))
8612                   --- IN ODS ATP, we do not want add demand for model only once.
8613                   AND NOT (MSC_ATP_PVT.G_INV_CTP = 5 and p_atp_record.parent_item_id is not null) THEN
8614 
8615                  --s_cto_rearch
8616                  l_atp_insert_rec.Top_Model_line_id := p_atp_record.Top_Model_line_id;
8617                  l_atp_insert_rec.ATO_Parent_Model_Line_Id := p_atp_record.ATO_Parent_Model_Line_Id;
8618                  l_atp_insert_rec.Parent_line_id := p_atp_record.Parent_line_id;
8619                  l_atp_insert_rec.ATO_Model_Line_Id := p_atp_record.ATO_Model_Line_Id;
8620                  l_atp_insert_rec.wip_supply_type := p_atp_record.wip_supply_type;
8621                  l_atp_insert_rec.mand_comp_flag := p_atp_record.mand_comp_flag;
8622                  --plan by request date enhancement
8623                  l_atp_insert_rec.ship_set_name := p_atp_record.ship_set_name;
8624                  l_atp_insert_rec.arrival_set_name := p_atp_record.arrival_set_name;
8625                  l_atp_insert_rec.original_request_ship_date :=l_original_req_ship_date;
8626                  IF PG_DEBUG in ('Y', 'C') THEN
8627                     msc_sch_wb.atp_debug('ATP_Check: ' || 'latest_acceptable_date: '|| l_atp_insert_rec.latest_acceptable_date);
8628                     msc_sch_wb.atp_debug('ATP_Check: ' || 'latest_acceptable_date: '|| l_atp_insert_rec.original_request_ship_date);
8629                  END IF;
8630                  l_atp_insert_rec.wip_supply_type := p_atp_record.mand_comp_flag;
8631                  --e_cto_rearch
8632 
8633                  -- ship_rec_cal
8634                  IF p_search = 1 and p_parent_pegging_id is null THEN
8635                      IF  p_atp_record.ship_method <> '@@@' THEN
8636                         l_atp_insert_rec.ship_method := p_atp_record.ship_method;
8637                      ELSE
8638                         l_atp_insert_rec.ship_method := null;
8639                      END IF;
8640                  END IF;
8641                  IF PG_DEBUG in ('Y', 'C') THEN
8642                      msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atp_insert_rec.ship_method : '|| l_atp_insert_rec.ship_method);
8643                  END IF;
8644                  l_atp_insert_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
8645                  l_atp_insert_rec.delivery_lead_time := l_delivery_lead_time;
8646 
8647                  ---bug 4267076: Do not create demand for planned outbound shipment
8648                  IF NOT(p_parent_pegging_id is not null
8649                         AND NVL(p_atp_record.parent_item_id, p_atp_record.inventory_item_id)
8650                                                                   = p_atp_record.inventory_item_id
8651                         AND NVL(MSC_ATP_PVT.G_PLAN_INFO_REC.plan_type,1) = 5) THEN  --5067205
8652 
8653                     MSC_ATP_DB_UTILS.Add_Mat_Demand(l_atp_insert_rec,
8654                                  p_plan_id,
8655                                  l_demand_class_flag,
8656                                  l_demand_id);
8657                  END IF;
8658 
8659                  IF PG_DEBUG in ('Y', 'C') THEN
8660                     msc_sch_wb.atp_debug('ATP_Check: ' || 'after calling Add_Mat_Demand, l_demand_id ='||
8661                               l_demand_id);
8662                  END IF;
8663 
8664                  -- populate insert rec to pegging tree for this demand
8665 
8666                  l_pegging_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
8667                  --s_cto_rearch: store actual line id for lower level components
8668                  --l_pegging_rec.order_line_id:= MSC_ATP_PVT.G_ORDER_LINE_ID;
8669                  l_pegging_rec.order_line_id :=  p_atp_record.demand_source_line;
8670                  l_pegging_rec.parent_pegging_id:= p_parent_pegging_id;
8671                  l_pegging_rec.atp_level:= p_level;
8672                  l_pegging_rec.organization_id:= p_atp_record.organization_id;
8673                  l_pegging_rec.organization_code := l_org_code;
8674                  l_pegging_rec.identifier1:= p_atp_record.instance_id;
8675                  l_pegging_rec.identifier2 := p_plan_id;
8676                  l_pegging_rec.identifier3 := l_demand_id;
8677 
8678                  -- time_phased_atp changes begin
8679                  IF (l_pf_atp = 'Y') or (G_INV_CTP = 5) THEN
8680                      l_pegging_rec.inventory_item_id:= p_atp_record.inventory_item_id;
8681                      l_pegging_rec.inventory_item_name := PF_ITEM_INFO_REC.item_name;
8682                  ELSE
8683                      l_pegging_rec.inventory_item_id:= p_atp_record.request_item_id;
8684                      l_pegging_rec.inventory_item_name := C_ITEM_INFO_REC.item_name;
8685                  END IF;
8686                  l_pegging_rec.aggregate_time_fence_date := p_atp_record.atf_date;
8687                  -- time_phased_atp changes end
8688 
8689                  l_pegging_rec.resource_id := NULL;
8690                  l_pegging_rec.resource_code := NULL;
8691                  l_pegging_rec.department_id := NULL;
8692                  l_pegging_rec.department_code := NULL;
8693                  l_pegging_rec.supplier_id := NULL;
8694                  l_pegging_rec.supplier_name := NULL;
8695                  l_pegging_rec.supplier_site_id := NULL;
8696                  l_pegging_rec.supplier_site_name := NULL;
8697                  l_pegging_rec.scenario_id:= p_scenario_id;
8698                  l_pegging_rec.supply_demand_source_type:= 6;
8699                  l_pegging_rec.supply_demand_quantity:=
8700                     p_atp_record.quantity_ordered ;
8701                  l_pegging_rec.supply_demand_type:= 1;
8702                  l_pegging_rec.supply_demand_date:= l_requested_ship_date;
8703 
8704 		 -- dsting ATO 2465370
8705 		 IF NVL(G_LINE_OVERRIDE_FLAG, 'N') = 'Y'
8706 		    --AND (NVL(C_ITEM_INFO_REC.cto_source_org_id, -1) = p_atp_record.organization_id
8707 		    --OR  p_parent_pegging_id is null)
8708 		 THEN
8709 		    l_pegging_rec.required_date := l_requested_ship_date;
8710                     --bug 3328421
8711                     l_pegging_rec.actual_supply_demand_date := l_requested_ship_date;
8712 		 ELSE
8713                     -- Bug 2748730. No need to move date to the end of as we will be doing that
8714 		    -- inside the procedure Add_Pegging
8715                     -- l_pegging_rec.required_date := TRUNC(l_requested_ship_date) + MSC_ATP_PVT.G_END_OF_DAY;
8716 		    l_pegging_rec.required_date := l_requested_ship_date ;
8717                     --bug 3328421
8718                     l_pegging_rec.actual_supply_demand_date := l_requested_ship_date;
8719 		 END IF;
8720 
8721                  /* ship_rec_cal
8722                     calendar constraint*/
8723                  IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP =1 and MSC_ATP_PVT.G_CALENDAR_CONSTRAINT='Y'
8724                         and p_parent_pegging_id is NULL THEN
8725                         l_pegging_rec.constraint_type :=7;
8726                  END IF;
8727 
8728                  -- for demo:1153192
8729                  l_pegging_rec.constraint_flag := 'N';
8730 	         l_pegging_rec.component_identifier :=
8731                             NVL(p_atp_record.component_identifier, MSC_ATP_PVT.G_COMP_LINE_ID);
8732 
8733                  l_pegging_rec.summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;
8734                  --- bug 2152184: For PF based ATP inventory_item_id field contains id for PF item
8735                  --- cto looks at pegging tree to place their demands. Since CTO expects to find
8736                  --  id for the requested item, we add the following column. CTO will now read from this column
8737                  l_pegging_rec.request_item_id := p_atp_record.request_item_id;
8738 
8739                  --diag_atp
8740                  l_pegging_rec.pegging_type := MSC_ATP_PVT.ORG_DEMAND; --item demand node
8741 
8742                  --s_cto_rearch
8743 
8744                  IF (p_atp_record.parent_bom_item_type in (1, 4) and p_atp_record.parent_repl_order_flag = 'Y')
8745                     --parent is model entity
8746                     OR (C_ITEM_INFO_REC.bom_item_type in (1, 4) and C_ITEM_INFO_REC.replenish_to_ord_flag = 'Y') THEN
8747                      l_pegging_rec.model_sd_flag := 1;
8748                  END IF;
8749                  l_pegging_rec.dest_inv_item_id := C_ITEM_INFO_REC.dest_inv_item_id;
8750                  l_pegging_rec.base_model_id := p_atp_record.base_model_id;
8751                  --e_cto_rearch
8752 
8753                  -- ship_rec_cal
8754                  IF p_search = 1 and p_parent_pegging_id is null THEN
8755                      IF  p_atp_record.ship_method <> '@@@' THEN
8756                         l_pegging_rec.ship_method := p_atp_record.ship_method;
8757                      ELSE
8758                         l_pegging_rec.ship_method := null;
8759                      END IF;
8760                  END IF;
8761 
8762                  -- Only Add Pegging when not dealing with Forward Stealing
8763                  l_pegging_rec.demand_class :=  p_atp_record.demand_class;
8764 
8765                  --4570421
8766                  l_pegging_rec.scaling_type                      := p_atp_record.scaling_type;
8767                  l_pegging_rec.scale_multiple                    := p_atp_record.scale_multiple;
8768                  l_pegging_rec.scale_rounding_variance           := p_atp_record.scale_rounding_variance;
8769                  l_pegging_rec.rounding_direction                := p_atp_record.rounding_direction;
8770                  l_pegging_rec.component_yield_factor            := p_atp_record.component_yield_factor; --4570421
8771                  l_pegging_rec.usage                             := p_atp_record.usage_qty; --4775920
8772                  l_pegging_rec.organization_type                 := p_atp_record.organization_type; --4775920
8773 
8774                  MSC_ATP_DB_UTILS.Add_Pegging(l_pegging_rec, l_pegging_id);
8775 
8776                  IF p_search = 1 AND p_parent_pegging_id is null THEN
8777                    MSC_ATP_PVT.G_DEMAND_PEGGING_ID := l_pegging_id;
8778                  END IF;
8779                ELSE
8780                  l_pegging_id := MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
8781                   --5158454 In cases when profile is set to yes and time phased ATP happens then
8782                   --  we need the value in l_demand_id of originally inserted SO demand.
8783                   IF l_time_phased_atp = 'Y' AND G_FORWARD_ATP = 'Y' THEN
8784 
8785                      IF PG_DEBUG in ('Y', 'C') THEN
8786                         msc_sch_wb.atp_debug('ATP_Check: ' || 'before assigning from G_DEMAND_ID, G_DEMAND_ID ='||
8787                               G_DEMAND_ID);
8788                      END IF;
8789                      l_demand_id  := NVL(l_demand_id,G_DEMAND_ID);
8790 
8791                      IF PG_DEBUG in ('Y', 'C') THEN
8792                         msc_sch_wb.atp_debug('ATP_Check: ' || 'after assigning from G_DEMAND_ID, l_demand_id ='||
8793                               l_demand_id);
8794                      END IF;
8795                   END IF;
8796 		 --dsting Stealing was here but commented out 10/17/02.
8797                END IF;
8798              ELSE
8799                   --for demand priority forward ATP we have already set l_pegging_id after get_forward
8800                   /*
8801                   l_pegging_id := MSC_ATP_PVT.G_CTO_FORWARD_DMD_PEG;
8802                   IF PG_DEBUG in ('Y', 'C') THEN
8803                      msc_sch_wb.atp_debug('ATP_Check: ' || 'Demand Pegging Id for Forward Stealing is : ' || l_pegging_id);
8804                   END IF;
8805 
8806                   */
8807                   IF p_parent_pegging_id is not null THEN
8808 
8809                     l_pegging_id := l_get_mat_out_rec.demand_pegging_id;
8810 
8811                   ELSE
8812 
8813                     l_pegging_id := MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
8814 
8815                   END IF;
8816 
8817              END IF;
8818              --- End Enhance CTO Phase 1 Req #17
8819 
8820                IF PG_DEBUG in ('Y', 'C') THEN
8821                   msc_sch_wb.atp_debug('ATP_Check: ' || 'Right before pegging');
8822                END IF;
8823                --optional_fw first iteration we should come here
8824                IF (p_parent_pegging_id is null) AND (p_search = 1) AND MSC_ATP_PVT.G_OPTIONAL_FW is null THEN
8825                    IF PG_DEBUG in ('Y', 'C') THEN
8826                       msc_sch_wb.atp_debug('ATP_Check: ' || 'p_parent_pegging_id is null');
8827                       msc_sch_wb.atp_debug('ATP_Check: ' || 'G_PEGGING_FOR_SET.COUNT = ' ||MSC_ATP_PVT.G_PEGGING_FOR_SET.COUNT);
8828                       msc_sch_wb.atp_debug('ATP_Check: ' || 'G_REQ_ATP_DATE.COUNT = ' || MSC_ATP_PVT.G_REQ_ATP_DATE.COUNT);
8829                       msc_sch_wb.atp_debug('ATP_Check: ' || 'G_REQ_DATE_QTY.COUNT = ' || MSC_ATP_PVT.G_REQ_DATE_QTY.COUNT);
8830                    END IF;
8831 
8832                    MSC_ATP_PVT.G_PEGGING_FOR_SET.Extend;
8833                    MSC_ATP_PVT.G_REQ_ATP_DATE.EXTEND;
8834  		   MSC_ATP_PVT.G_REQ_DATE_QTY.EXTEND;
8835 
8836                    IF PG_DEBUG in ('Y', 'C') THEN
8837                       msc_sch_wb.atp_debug('ATP_Check: ' || 'G_PEGGING_FOR_SET.COUNT = ' ||MSC_ATP_PVT.G_PEGGING_FOR_SET.COUNT);
8838                       msc_sch_wb.atp_debug('ATP_Check: ' || 'G_REQ_ATP_DATE.COUNT = ' || MSC_ATP_PVT.G_REQ_ATP_DATE.COUNT);
8839                       msc_sch_wb.atp_debug('ATP_Check: ' || 'G_REQ_DATE_QTY.COUNT = ' || MSC_ATP_PVT.G_REQ_DATE_QTY.COUNT);
8840                       msc_sch_wb.atp_debug('ATP_Check: ' || 'l_pegging_id = '||l_pegging_id );
8841                    END IF;
8842 
8843 
8844                    MSC_ATP_PVT.G_PEGGING_FOR_SET(MSC_ATP_PVT.G_PEGGING_FOR_SET.COUNT) := l_pegging_id;
8845                    MSC_ATP_PVT.G_REQ_ATP_DATE( MSC_ATP_PVT.G_REQ_ATP_DATE.COUNT) := l_requested_ship_date;
8846                    MSC_ATP_PVT.G_REQ_DATE_QTY(MSC_ATP_PVT.G_REQ_DATE_QTY.COUNT) := p_atp_record.Quantity_Ordered;
8847 
8848                END IF;
8849                IF PG_DEBUG in ('Y', 'C') THEN
8850                   msc_sch_wb.atp_debug('ATP_Check: ' || 'Right after pegging');
8851                END IF;
8852 		-- dsting: 9/19/2002 component substitute check moved to
8853 		-- after move_sd_temp_to_mrp_details.
8854                --optional_fw we need to come here for condition added
8855                IF (
8856                     (
8857                      ( p_search = 2 AND p_parent_pegging_id is not null) OR
8858                      (MSC_ATP_PVT.G_OPTIONAL_FW is not null )
8859                     )
8860                    OR
8861                     ((p_search=1) AND (MSC_ATP_PVT.G_OPTIONAL_FW is null ))
8862                   ) AND
8863                    --bug 4428128: Add supply pegging if req date >= ITF
8864                   ((l_atp_flag in ('Y', 'C') AND
8865                   NOT(C_ITEM_INFO_REC.bom_item_type = 1 and
8866                                        --bug 3423277
8867                                        C_ITEM_INFO_REC.atp_comp_flag in ('Y', 'C') and
8868                                       p_atp_record.parent_item_id is null))
8869                  ---bug 4523103: Add pegging corretly if  req date >= ITF
8870                  or (l_atp_flag = 'N' and l_requested_ship_date >= l_infinite_time_fence)) AND
8871                  -- populate pegging tree for existing supply
8872                  -- If this is the backward search, no matter what we have
8873                  -- on the request date, we need to populate it.
8874                  (NVL(l_parent_peg_forward, 0) = 0) THEN
8875                    --- Enhance CTO Phase 1 Req #17
8876 
8877                  l_pegging_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
8878                  l_pegging_rec.order_line_id:= MSC_ATP_PVT.G_ORDER_LINE_ID;
8879                  l_pegging_rec.parent_pegging_id:= l_pegging_id;
8880                  l_pegging_rec.atp_level:= p_level + 1;
8881                  l_pegging_rec.organization_id:= p_atp_record.organization_id;
8882                  l_pegging_rec.organization_code := l_org_code;
8883                  l_pegging_rec.identifier1:= p_atp_record.instance_id;
8884                  l_pegging_rec.identifier2 := p_plan_id;
8885                  l_pegging_rec.identifier3 := NULL;
8886 
8887                  -- time_phased_atp changes begin
8888                  IF l_time_phased_atp = 'Y' and l_requested_ship_date <= p_atp_record.atf_date THEN
8889                          l_pegging_rec.inventory_item_id:= l_request_item_id;
8890                          l_pegging_rec.inventory_item_name := C_ITEM_INFO_REC.item_name;
8891                  ELSE
8892                          l_pegging_rec.inventory_item_id:= p_atp_record.inventory_item_id;
8893                          l_pegging_rec.inventory_item_name := PF_ITEM_INFO_REC.item_name;
8894                  END IF;
8895                  l_pegging_rec.aggregate_time_fence_date := p_atp_record.atf_date;
8896                  -- time_phased_atp changes end
8897                  --s_cto_rearch
8898                  l_pegging_rec.dest_inv_item_id := C_ITEM_INFO_REC.dest_inv_item_id;
8899 
8900                  l_pegging_rec.resource_id := NULL;
8901                  l_pegging_rec.resource_code := NULL;
8902                  l_pegging_rec.department_id := NULL;
8903                  l_pegging_rec.department_code := NULL;
8904                  l_pegging_rec.supplier_id := NULL;
8905                  l_pegging_rec.supplier_name := NULL;
8906                  l_pegging_rec.supplier_site_id := NULL;
8907                  l_pegging_rec.supplier_site_name := NULL;
8908                  l_pegging_rec.scenario_id:= p_scenario_id;
8909                  l_pegging_rec.supply_demand_source_type:= ATP;
8910                  l_pegging_rec.supply_demand_quantity:=l_requested_date_quantity;
8911                  l_pegging_rec.supply_demand_date:= l_requested_ship_date;
8912                  l_pegging_rec.supply_demand_type:= 2;
8913                  l_pegging_rec.source_type := 0;
8914 	         l_pegging_rec.component_identifier :=
8915                             NVL(p_atp_record.component_identifier, MSC_ATP_PVT.G_COMP_LINE_ID);
8916 
8917                  -- for demo:1153192
8918                  --optional_fw
8919                  IF ((p_search = 1)
8920                       AND (l_atp_comp_flag = 'N')
8921                       AND ( p_atp_record.quantity_ordered >=
8922                            l_requested_date_quantity)) AND (G_FORWARD_ATP = 'N' OR G_SUBSTITUTION_FLAG = 'Y')THEN --5211286
8923                      l_pegging_rec.constraint_flag := 'Y';
8924                  ELSE
8925                      l_pegging_rec.constraint_flag := 'N';
8926 
8927                  END IF;
8928 
8929                  l_pegging_rec.summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;
8930                  --- bug 2152184: For PF based ATP inventory_item_id field contains id for PF item
8931                  --- cto looks at pegging tree to place their demands. Since CTO expects to find
8932                  --  id for the requested item, we add the following column. CTO will now read from this column
8933                  l_pegging_rec.request_item_id := p_atp_record.request_item_id;
8934 
8935                  --diag_atp
8936                  l_pegging_rec.plan_name := p_atp_record.plan_name;
8937                  --5151678 no need for assigning with global variable.
8938                  /*
8939                  --optional_fw added correct required date in pegging
8940                  IF PG_DEBUG in ('Y', 'C') THEN
8941                   msc_sch_wb.atp_debug('ATP_Check: ' || 'G_REQUESTED_SHIP_DATE = '||G_REQUESTED_SHIP_DATE);
8942                  END IF;
8943                  l_pegging_rec.required_quantity:= p_atp_record.quantity_ordered;
8944                  l_pegging_rec.required_date := G_REQUESTED_SHIP_DATE; --l_requested_ship_date;
8945                  */
8946                  l_pegging_rec.required_quantity:= p_atp_record.quantity_ordered;
8947                  l_pegging_rec.required_date := l_requested_ship_date;
8948                  --bug 3328421
8949                  l_pegging_rec.actual_supply_demand_date := l_requested_ship_date;
8950                  l_pegging_rec.infinite_time_fence := l_get_mat_out_rec.infinite_time_fence_date;
8951                  l_pegging_rec.atp_rule_name := l_get_mat_out_rec.atp_rule_name;
8952                  l_pegging_rec.rounding_control := l_rounding_control_flag;
8953                  l_pegging_rec.atp_flag := l_atp_flag;
8954                  l_pegging_rec.atp_component_flag := l_atp_comp_flag;
8955                  l_pegging_rec.pegging_type := MSC_ATP_PVT.ATP_SUPPLY; ---atp supply node
8956                  l_pegging_rec.postprocessing_lead_time := l_post_pro_lt;
8957 		 l_pegging_rec.processing_lead_time := l_process_lt; -- 5438149
8958                  l_pegging_rec.preprocessing_lead_time := l_pre_pro_lt;
8959                  l_pegging_rec.fixed_lead_time := l_fixed_lt;
8960                  l_pegging_rec.variable_lead_time := l_variable_lt;
8961                  l_pegging_rec.weight_capacity := l_unit_weight;
8962                  l_pegging_rec.volume_capacity := l_unit_volume;
8963                  l_pegging_rec.weight_uom := l_weight_uom;
8964                  l_pegging_rec.volume_uom := l_volume_uom;
8965                  l_pegging_rec.allocation_rule := l_allocation_rule_name;
8966                  l_pegging_rec.substitution_window  := l_substitution_window;
8967                  l_pegging_rec.REVERSE_CUM_YIELD := p_atp_record.reverse_cumulative_yield;
8968                  l_pegging_rec.model_sd_flag := 2;
8969 
8970 
8971                  IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP =1 THEN
8972                      IF ((p_search = 1) and (l_atp_comp_flag = 'N') and
8973                            (p_atp_record.quantity_ordered > l_requested_date_quantity)) THEN
8974                         l_pegging_rec.constraint_type := 1;
8975                         p_atp_record.combined_requested_date_qty := l_requested_date_quantity;
8976 			p_atp_record.requested_date_quantity := l_requested_date_quantity;
8977                      END IF;
8978                  END IF;
8979 
8980                  l_pegging_rec.demand_class :=  p_atp_record.demand_class;
8981                  -- Bug 3826234
8982                  IF PG_DEBUG in ('Y', 'C') THEN
8983                     msc_sch_wb.atp_debug('ATP_Check: ' || '----------- Calendars passed to Pegging -----------------');
8984                     msc_sch_wb.atp_debug('ATP_Check: ' || 'shipping_cal_code = '      ||p_atp_record.shipping_cal_code);
8985                     msc_sch_wb.atp_debug('ATP_Check: ' || 'receiving_cal_code = '     ||p_atp_record.receiving_cal_code);
8986                     msc_sch_wb.atp_debug('ATP_Check: ' || 'intransit_cal_code = '     ||p_atp_record.intransit_cal_code);
8987                     msc_sch_wb.atp_debug('ATP_Check: ' || 'manufacturing_cal_code = ' ||p_atp_record.manufacturing_cal_code);
8988                  END IF;
8989                  IF p_parent_pegging_id is null then
8990                     l_pegging_rec.shipping_cal_code      :=  p_atp_record.shipping_cal_code;
8991                     l_pegging_rec.receiving_cal_code     :=  p_atp_record.receiving_cal_code;
8992                     l_pegging_rec.intransit_cal_code     :=  p_atp_record.intransit_cal_code;
8993                     l_pegging_rec.manufacturing_cal_code :=  p_atp_record.manufacturing_cal_code;
8994                     IF PG_DEBUG in ('Y', 'C') THEN
8995                        msc_sch_wb.atp_debug('ATP_Check: ' || 'Inside IF');
8996                     END IF;
8997                  ELSIF NVL(p_atp_record.to_organization_id,p_atp_record.organization_id)
8998                                                              <> p_atp_record.organization_id THEN
8999 
9000                     l_pegging_rec.shipping_cal_code      :=  p_atp_record.shipping_cal_code;
9001                     l_pegging_rec.receiving_cal_code     :=  p_atp_record.receiving_cal_code;
9002                     l_pegging_rec.intransit_cal_code     :=  p_atp_record.intransit_cal_code;
9003                     l_pegging_rec.manufacturing_cal_code :=  NULL;
9004                     IF PG_DEBUG in ('Y', 'C') THEN
9005                        msc_sch_wb.atp_debug('ATP_Check: ' || 'Inside ELSIF');
9006                     END IF;
9007                  ELSE
9008                     l_pegging_rec.manufacturing_cal_code :=  p_atp_record.manufacturing_cal_code;
9009                     l_pegging_rec.shipping_cal_code      :=  NULL;
9010                     l_pegging_rec.receiving_cal_code     :=  NULL;
9011                     l_pegging_rec.intransit_cal_code     :=  NULL;
9012                     IF PG_DEBUG in ('Y', 'C') THEN
9013                        msc_sch_wb.atp_debug('ATP_Check: ' || 'Inside ELSE');
9014                     END IF;
9015                  END IF;
9016                  -- Bug 3826234
9017 
9018                  --4570421
9019                  l_pegging_rec.scaling_type                      := p_atp_record.scaling_type;
9020                  l_pegging_rec.scale_multiple                    := p_atp_record.scale_multiple;
9021                  l_pegging_rec.scale_rounding_variance           := p_atp_record.scale_rounding_variance;
9022                  l_pegging_rec.rounding_direction                := p_atp_record.rounding_direction;
9023                  l_pegging_rec.component_yield_factor            := p_atp_record.component_yield_factor; --4570421
9024                  l_pegging_rec.usage                             := p_atp_record.usage_qty; --4775920
9025                  l_pegging_rec.organization_type                 := p_atp_record.organization_type; --4775920
9026 
9027                  MSC_ATP_DB_UTILS.Add_Pegging(l_pegging_rec, l_atp_pegging_id);
9028 
9029                  -- Add pegging_id to the l_atp_period and l_atp_supply_demand
9030                  --optional_fw store the the supply line pegging id for forward pass
9031                  IF (MSC_ATP_PVT.G_OPTIONAL_FW is not null and p_parent_pegging_id is null) THEN
9032                    G_FW_PEGGING_ID := l_atp_pegging_id;
9033                    IF PG_DEBUG in ('Y', 'C') THEN
9034                        msc_sch_wb.atp_debug('ATP_Check: ' || 'Setting G_FW_PEGGING_ID for ATP supply line ' || G_FW_PEGGING_ID);
9035                    END IF;
9036                  END IF;
9037 
9038                  FOR i in 1..l_atp_period.Level.COUNT LOOP
9039                    l_atp_period.Pegging_Id(i) := l_atp_pegging_id;
9040                    l_atp_period.End_Pegging_Id(i) := MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
9041                  END LOOP;
9042 
9043 
9044                  -- dsting: supply/demand details pl/sql table no longer used
9045 /*                 FOR i in 1..l_atp_supply_demand.Level.COUNT LOOP
9046                    l_atp_supply_demand.Pegging_Id(i) := l_atp_pegging_id;
9047                    l_atp_supply_demand.End_Pegging_Id(i) := MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
9048                  END LOOP;
9049 */
9050 		 IF p_atp_record.insert_flag <> 0 THEN
9051 	   	         MSC_ATP_DB_UTILS.move_SD_temp_into_mrp_details(l_atp_pegging_id,
9052 				MSC_ATP_PVT.G_DEMAND_PEGGING_ID);
9053    		 END IF;
9054 
9055 
9056                -- Now if this item was a component of a BOM, then check the
9057                -- substitutes and see if we can satisfy the demand
9058 
9059    -- Nagaraj
9060                IF ( p_search = BACKWARD_SCHEDULING) and (p_parent_pegging_id is not null --5006799
9061                    and l_net_demand > 0  and MSC_ATP_PVT.G_SUB_COMP = 'Y') THEN
9062                  IF PG_DEBUG in ('Y', 'C') THEN
9063                     msc_sch_wb.atp_debug('ATP_Check: ' || 'Before check substitutes call:'||
9064                  to_char(p_parent_pegging_id) || ':' || to_char(l_net_demand));
9065                  END IF;
9066 
9067                  -- Setup new g_atp_record during each loop, we should not
9068                  -- resue the original record, because we need the original one
9069                  -- for CTP if there is not enough supply of components
9070                  g_atp_record.error_code := ALLSUCCESS;
9071                  g_atp_record.inventory_item_id := p_atp_record.inventory_item_id;
9072                  -- time_phased_atp changes begin
9073                  IF l_pf_atp = 'Y' THEN
9074                      g_atp_record.request_item_id := p_atp_record.inventory_item_id;
9075                  ELSE
9076                      g_atp_record.request_item_id := p_atp_record.request_item_id;
9077                  END IF;
9078                  g_atp_record.atf_date := p_atp_record.atf_date;
9079                  -- time_phased_atp changes end
9080                  g_atp_record.organization_id := p_atp_record.organization_id;
9081                  g_atp_record.instance_id := p_atp_record.instance_id;
9082                  g_atp_record.quantity_ordered := l_net_demand;
9083                  g_atp_record.quantity_UOM := p_atp_record.quantity_UOM;
9084                  g_atp_record.requested_ship_date :=
9085                              p_atp_record.requested_ship_date;
9086                  g_atp_record.requested_arrival_date :=
9087                              p_atp_record.requested_arrival_date;
9088                  g_atp_record.latest_acceptable_date :=
9089                              p_atp_record.latest_acceptable_date;
9090                  g_atp_record.delivery_lead_time :=
9091                              p_atp_record.delivery_lead_time;
9092                  g_atp_record.freight_carrier := p_atp_record.freight_carrier;
9093                  g_atp_record.ship_method := p_atp_record.ship_method;
9094                  g_atp_record.demand_class := p_atp_record.demand_class;
9095                  g_atp_record.override_flag := p_atp_record.override_flag;
9096                  g_atp_record.action := p_atp_record.action;
9097                  g_atp_record.ship_date := p_atp_record.ship_date;
9098                  g_atp_record.available_quantity :=
9099                              p_atp_record.available_quantity;
9100                  g_atp_record.used_available_quantity :=
9101                              p_atp_record.used_available_quantity; --bug3409973
9102                  g_atp_record.requested_date_quantity :=
9103                              p_atp_record.requested_date_quantity;
9104                  g_atp_record.supplier_id := p_atp_record.supplier_id;
9105                  g_atp_record.supplier_site_id := p_atp_record.supplier_site_id;
9106                  g_atp_record.insert_flag := p_atp_record.insert_flag;
9107                  g_atp_record.order_number := p_atp_record.order_number;
9108                  g_atp_record.demand_source_line :=
9109                              p_atp_record.demand_source_line;
9110                  g_atp_record.demand_source_header_id :=
9111                              p_atp_record.demand_source_header_id;
9112                  g_atp_record.demand_source_type :=
9113                              p_atp_record.demand_source_type;
9114                  g_atp_record.shipping_cal_code := p_atp_record.shipping_cal_code; -- Bug 3371817
9115                  g_atp_record.manufacturing_cal_code := p_atp_record.manufacturing_cal_code; -- Bug 3826234
9116 
9117                  -- cchen : 1238941
9118                  -- temporary we set the the l_sub_requested_date_quantity
9119                  -- to the original net demand
9120                  l_sub_requested_date_quantity := l_net_demand;
9121                  --plan by request date enhancement
9122                  g_atp_record.ship_set_name := p_atp_record.ship_set_name;
9123                  g_atp_record.arrival_set_name := p_atp_record.arrival_set_name;
9124                  g_atp_record.original_request_ship_date := l_original_req_ship_date;
9125                  g_atp_record.bill_seq_id := p_atp_record.bill_seq_id; --4741012 passing bill id
9126                  -- l_sub_net_demand := l_net_demand;
9127                  --4902658 Start This is for primary component / 5216528
9128                  l_substitutes_rec.inventory_item_id.EXTEND;
9129                  l_substitutes_rec.pegging_id.EXTEND;
9130                  l_substitutes_rec.sub_atp_qty.EXTEND;
9131                  l_substitutes_rec.demand_id.EXTEND;
9132                  l_substitutes_rec.atf_date_quantity.EXTEND; --5283809
9133                  l_substitutes_rec.quantity_ordered.EXTEND;
9134                  l_substitutes_rec.pf_item_id.EXTEND;
9135 
9136                  MSC_ATP_REQ.Check_Substitutes(
9137                   g_atp_record,
9138                   p_parent_pegging_id,
9139                   g_atp_record.instance_id,
9140                   p_scenario_id,
9141                   p_level,
9142                   p_search,
9143                   p_plan_id,
9144                   g_atp_record.inventory_item_id,
9145                   g_atp_record.organization_id,
9146                   g_atp_record.quantity_ordered,
9147                   l_net_demand,  -- out variable
9148                   x_atp_supply_demand,
9149                   x_atp_period,
9150                   l_substitutes_rec,--4902658/5216528
9151                   l_return_status,
9152                   p_refresh_number);    -- For summary enhancenment
9153                 -- this is to keep track how many we have in substitutes
9154 
9155                  -- 4902658/5216528 start this has main items's details
9156                  l_substitutes_rec.inventory_item_id(1) := p_atp_record.Request_Item_Id; --4902658/5216528
9157                  l_substitutes_rec.pegging_id(1) := l_pegging_Id; --4902658/5216528
9158                  l_substitutes_rec.demand_id(1) := l_demand_id; --4902658/5216528
9159                  l_substitutes_rec.sub_atp_qty(1) :=  LEAST(GREATEST(l_requested_date_quantity, 0),
9160                                                                       p_atp_record.quantity_ordered);
9161                IF PG_DEBUG in ('Y', 'C') THEN
9162                  for ii in 1..l_substitutes_rec.inventory_item_id.count
9163                   loop
9164                     msc_sch_wb.atp_debug('From Substitute Array Item   : '|| l_substitutes_rec.inventory_item_id(ii));
9165                     msc_sch_wb.atp_debug('From Substitute Array Pegging: '|| l_substitutes_rec.pegging_id(ii));
9166                     msc_sch_wb.atp_debug('From Substitute Array ATP QTY: '|| l_substitutes_rec.sub_atp_qty(ii));
9167                   end loop;
9168                END IF;
9169                --4902658/5216528 end
9170                 -- cchen : 1238941
9171                 -- l_sub_net_demand := l_sub_net_demand - l_net_demand;
9172                 l_sub_requested_date_quantity := l_sub_requested_date_quantity -
9173                                                 greatest(l_net_demand, 0);
9174 
9175                 --4570421
9176                 IF PG_DEBUG in ('Y', 'C') THEN
9177                    msc_sch_wb.atp_debug('ATP_Check: ' || 'l_net_demand:'|| l_net_demand);
9178                    msc_sch_wb.atp_debug('ATP_Check: ' || 'l_sub_requested_date_quantity:'|| l_sub_requested_date_quantity);
9179                    msc_sch_wb.atp_debug('ATP_Check: ' || 'p_atp_record.scaling_type'|| p_atp_record.scaling_type);
9180                    msc_sch_wb.atp_debug('ATP_Check: ' || 'MSC_ATP_PVT.G_ORG_INFO_REC.org_type:'|| MSC_ATP_PVT.G_ORG_INFO_REC.org_type);
9181                 END IF;
9182                 IF ( ( MSC_ATP_PVT.G_ORG_INFO_REC.org_type = MSC_ATP_PVT.DISCRETE_ORG AND p_atp_record.scaling_type = 2) OR
9183                      (MSC_ATP_PVT.G_ORG_INFO_REC.org_type = MSC_ATP_PVT.OPM_ORG AND p_atp_record.scaling_type IN (0,2))) AND l_net_demand = 0 then
9184                       -- Delete the supply pegging of the main component, if substitute can satisfy the lot-size.
9185                       MSC_ATP_DB_UTILS.delete_pegging( l_atp_pegging_id);
9186                 END IF;
9187 
9188                 IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP =1 THEN
9189                      IF ((p_search = 1) and (l_atp_comp_flag = 'N') and
9190                            (p_atp_record.quantity_ordered > l_requested_date_quantity)) THEN
9191                         l_pegging_rec.constraint_type := 1;
9192                         p_atp_record.combined_requested_date_qty := l_requested_date_quantity +
9193                                                                                    l_sub_requested_date_quantity;
9194                      END IF;
9195                 END IF;
9196 
9197                 IF PG_DEBUG in ('Y', 'C') THEN
9198                    msc_sch_wb.atp_debug('ATP_Check: ' || 'After check substitutes:'||
9199                                              to_char(l_net_demand));
9200                    msc_sch_wb.atp_debug('ATP_Check: ' || 'l_sub_requested_date_quantity : '||
9201                                              to_char(l_sub_requested_date_quantity));
9202                 END IF;
9203 
9204                -- In order to show the correct demand quantity that got considered
9205                -- for the primary component, we need to change the requested
9206                -- quantity for that primary component to what was requested minus
9207                -- what got satisfied by the substitutes
9208 
9209                -- cchen : 1238941
9210                -- p_atp_record.quantity_ordered :=
9211                --          (p_atp_record.quantity_ordered - l_net_demand);
9212                -- l_atp_insert_rec.quantity_ordered :=
9213                --          (l_atp_insert_rec.quantity_ordered - l_net_demand);
9214                -- l_pegging_rec.supply_demand_quantity :=
9215                --          (l_pegging_rec.supply_demand_quantity - l_net_demand);
9216 
9217                -- update the primary demand pegging tree with right quantity since
9218                -- we do the substitute now.
9219                   --5220357 start
9220                  IF l_sub_requested_date_quantity > 0 THEN
9221                    IF PG_DEBUG in ('Y', 'C') THEN
9222                       msc_sch_wb.atp_debug('ATP_Check: ' || 'Update POD for Primary Component');
9223                    END IF;
9224 		   IF NVL(MSC_ATP_PVT.G_DIAGNOSTIC_ATP,2) <> 1 THEN
9225                    	MSC_ATP_DB_UTILS.Update_Pegging(l_pegging_id,
9226                                   null,
9227                                   p_atp_record.quantity_ordered-
9228                                   l_sub_requested_date_quantity );
9229 		   END IF;
9230 
9231                    update msc_demands
9232                    set    USING_REQUIREMENT_QUANTITY =
9233                           MSC_ATP_UTILS.Truncate_Demand((USING_REQUIREMENT_QUANTITY-
9234                            greatest(l_sub_requested_date_quantity, 0)))  -- 5598066
9235                    where  demand_id = l_demand_id
9236                    and    plan_id = p_plan_id
9237                    returning inventory_item_id into l_inventory_item_id;
9238 
9239                    IF PG_DEBUG in ('Y', 'C') THEN
9240                       msc_sch_wb.atp_debug('ATP_Check: ' || 'number of rows updated := ' || SQL%ROWCOUNT);
9241                    END IF;
9242                    /* time_phased_atp
9243                       need to check this while UT*/
9244                    IF l_time_phased_atp = 'Y' THEN
9245 		        --using the same insert rec we prepared earlier
9246 		        l_atp_insert_rec.quantity_ordered :=  l_atp_insert_rec.quantity_ordered - greatest(l_sub_requested_date_quantity, 0);
9247 		        l_atp_insert_rec.requested_date_quantity := l_atp_insert_rec.requested_date_quantity - greatest(l_sub_requested_date_quantity, 0);
9248 		        -- No need to change ATF date qty, let it be the same. Increment_Bucketed_Demands_Qty will take care of whether to use
9249 		        -- qty upto ATF completely.
9250 		        MSC_ATP_PF.Increment_Bucketed_Demands_Qty(
9251                                 l_atp_insert_rec,
9252                                 p_plan_id,
9253                                 l_demand_id,
9254                                 l_return_status
9255 		        );
9256                         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
9257                            IF PG_DEBUG in ('Y', 'C') THEN
9258                                 msc_sch_wb.atp_debug('ATP_Check: ' || 'Error occured in procedure Increment_Bucketed_Demands_Qty');
9259                            END IF;
9260                            RAISE FND_API.G_EXC_ERROR;
9261                         END IF;
9262                    ELSIF MSC_ATP_PVT.G_INV_CTP = 4 and MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y'
9263                       AND MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1 AND MSC_ATP_PVT.G_ALLOCATION_METHOD = 1 THEN
9264                           update msc_alloc_demands
9265                           set allocated_quantity = (allocated_quantity -
9266                                                     greatest(l_sub_requested_date_quantity, 0))
9267                           where parent_demand_id = l_demand_id --updating for primary only
9268                           and   plan_id = p_plan_id;
9269 
9270                           IF PG_DEBUG in ('Y', 'C') THEN
9271                              msc_sch_wb.atp_debug('ATP_Check: ' || 'number of rows updated := ' || SQL%ROWCOUNT);
9272                           END IF;
9273                    END IF;
9274                    IF PG_DEBUG in ('Y', 'C') THEN
9275                       msc_sch_wb.atp_debug('ATP_Check: ' || 'l_inventory_item_id := ' || l_inventory_item_id);
9276                       msc_sch_wb.atp_debug('ATP_Check: ' || ' MSC_ATP_PVT.G_SUMMARY_FLAG := ' ||  MSC_ATP_PVT.G_SUMMARY_FLAG);
9277                    END IF;
9278 
9279                    -- update summary records removed in ODS case - for summary enhancement
9280                    IF MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y' AND p_plan_id = -1 THEN
9281                        MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(p_atp_record.instance_id,
9282                                                           p_plan_id,
9283                                                           p_atp_record.organization_id,
9284                                                           l_inventory_item_id,
9285                                                           l_requested_ship_date,
9286                                                           null,
9287                                                           null,
9288                                                           null,
9289                                                           null,
9290                                                           null,
9291                                                           null,
9292                                                           2);
9293                        update /*+ INDEX(msc_atp_summary_sd MSC_ATP_SUMMARY_SD_U1) */ msc_atp_summary_sd
9294                        set sd_qty = sd_qty + greatest(l_sub_requested_date_quantity, 0)
9295                        where plan_id = p_plan_id
9296                        and sr_instance_id  = p_atp_record.instance_id
9297                        and inventory_item_id = l_inventory_item_id
9298                        and organization_id = p_atp_record.organization_id
9299                        and sd_date = trunc(l_requested_ship_date);
9300                        commit;
9301                    END IF;
9302 
9303                  END IF;
9304                  --5220357 end
9305              ELSE --4902658/5216528 start if no check_substitutes gets called in b/w pass
9306                 l_substitutes_rec.inventory_item_id.EXTEND;
9307                 l_substitutes_rec.pegging_id.EXTEND;
9308                 l_substitutes_rec.sub_atp_qty.EXTEND;
9309                 l_substitutes_rec.demand_id.EXTEND;
9310                 l_substitutes_rec.atf_date_quantity.EXTEND; --5283809
9311                 l_substitutes_rec.quantity_ordered.EXTEND;
9312                 l_substitutes_rec.pf_item_id.EXTEND;
9313 
9314                 l_substitutes_rec.demand_id(1) := l_demand_id; --4902658/5216528
9315 
9316                 l_substitutes_rec.inventory_item_id(1) := p_atp_record.request_Item_Id;
9317 
9318                 l_substitutes_rec.pegging_id(1) := l_pegging_Id;
9319                 l_substitutes_rec.sub_atp_qty(1) :=  LEAST(GREATEST(l_requested_date_quantity, 0),
9320                                                            p_atp_record.quantity_ordered);
9321                 --4902658/5216528 end
9322                END IF; -- for substitute check
9323    -- Nagaraj
9324 
9325                  -- 1413608: instead of replace x_atp with l_atp, append it
9326                  -- x_atp_supply_demand := l_atp_supply_demand;
9327                  -- x_atp_period := l_atp_period;
9328 
9329                  MSC_ATP_PROC.Details_Output(l_atp_period,
9330                                 l_atp_supply_demand,
9331                                 x_atp_period,
9332                                 x_atp_supply_demand,
9333                                 l_return_status);
9334               ELSE --4902658/5216528 start For f/w pass when check_substitutes is not callled.
9335                  l_substitutes_rec.inventory_item_id.EXTEND;
9336                  l_substitutes_rec.pegging_id.EXTEND;
9337                  l_substitutes_rec.sub_atp_qty.EXTEND;
9338                  l_substitutes_rec.demand_id.EXTEND;
9339                  l_substitutes_rec.atf_date_quantity.EXTEND; --5283809
9340                  l_substitutes_rec.quantity_ordered.EXTEND;
9341                  l_substitutes_rec.pf_item_id.EXTEND;
9342 
9343                  l_substitutes_rec.demand_id(1) := l_demand_id; --4902658/5216528
9344                  l_substitutes_rec.inventory_item_id(1) := p_atp_record.Request_Item_Id;
9345                  l_substitutes_rec.pegging_id(1) := l_pegging_Id;
9346                  l_substitutes_rec.sub_atp_qty(1) :=  LEAST(GREATEST(l_requested_date_quantity, 0),
9347                                                             p_atp_record.quantity_ordered);
9348                  --4902658/5216528 end
9349                END IF;
9350 
9351                --- Enhance CTO Phase 1 Req #17
9352                -- for aatp --- steal_before_ctp
9353                -- Only do stealing if ATP flag for the item is Yes.
9354                --s_cto_rearch: Do stealing for all levels in all orgs
9355                /* IF (l_net_demand > 0) AND -- Bug 2745728 removed p_parent_pegging_id clause
9356                    --- Enhance CTO Phase 1 Req #17
9357                        -- Support Backward Stealing for components of ATO model in
9358                        -- Model's sourcing organization.
9359                     ( (p_parent_pegging_id IS NULL) OR
9360                         NVL(C_ITEM_INFO_REC.cto_source_org_id, -1) = p_atp_record.organization_id ) AND
9361                     --- End Enhance CTO Phase 1 Req #17
9362                   (p_search =1)
9363                   AND (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND (l_atp_flag = 'Y') THEN
9364 
9365                --IF ((MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND (l_net_demand > 0) AND
9366                --    (l_atp_flag = 'Y') AND (l_atp_comp_flag = 'N') AND
9367                --    (p_parent_pegging_id is null)) THEN
9368                */
9369                IF (l_net_demand > 0) AND
9370                    (p_search =1) AND (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND (l_atp_flag = 'Y')
9371                               --bug 3423277: Restrict stealing only for cases where we are
9372                                ---doing atp check for model.
9373                               AND NOT(C_ITEM_INFO_REC.bom_item_type = 1 and
9374                                     ( C_ITEM_INFO_REC.atp_comp_flag in ('Y', 'C') and
9375                                       p_atp_record.parent_item_id is null)) THEN
9376                    --- Enhance CTO Phase 1 Req #17
9377                        -- Support Backward Stealing for components of ATO model in
9378                        -- Model's sourcing organization.
9379                        -- Do not reset the parent pegging Id in such a case.
9380                    /* We support stealing at all levels.
9381                    IF (NVL(C_ITEM_INFO_REC.cto_source_org_id, -1) <> p_atp_record.organization_id) THEN
9382                        l_pegging_id := MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
9383                    END IF;
9384                    */
9385 
9386                    IF p_parent_pegging_id is null THEN
9387                         l_pegging_id := MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
9388                    END IF;
9389                    --- End Enhance CTO Phase 1 Req #17
9390 
9391                    IF PG_DEBUG in ('Y', 'C') THEN
9392                       msc_sch_wb.atp_debug('ATP_Check: ' || 'Start stealing in backward case, before CTP');
9393                    END IF;
9394                    -- AATP: here we need to do the stealing.
9395                    -- Only do stealing if ATP flag for the item is Yes.
9396                    g_atp_record.error_code := ALLSUCCESS;
9397                    g_atp_record.inventory_item_id := p_atp_record.inventory_item_id;
9398                    -- time_phased_atp
9399                    IF l_pf_atp='Y' THEN
9400                        g_atp_record.request_item_id := p_atp_record.inventory_item_id;
9401                    ELSE
9402                        g_atp_record.request_item_id := p_atp_record.request_item_id;
9403                    END IF;
9404                    g_atp_record.organization_id := p_atp_record.organization_id;
9405                    g_atp_record.instance_id := p_atp_record.instance_id;
9406                    g_atp_record.quantity_ordered := l_net_demand;
9407                    g_atp_record.quantity_UOM := p_atp_record.quantity_UOM;
9408                    g_atp_record.requested_ship_date := l_requested_ship_date;
9409                    g_atp_record.requested_arrival_date := null;
9410                    g_atp_record.latest_acceptable_date :=
9411                              p_atp_record.latest_acceptable_date;
9412                    g_atp_record.delivery_lead_time :=
9413                              p_atp_record.delivery_lead_time;
9414                    g_atp_record.freight_carrier := p_atp_record.freight_carrier;
9415                    g_atp_record.ship_method := p_atp_record.ship_method;
9416                    g_atp_record.demand_class := p_atp_record.demand_class;
9417                    g_atp_record.override_flag := p_atp_record.override_flag;
9418                    g_atp_record.action := p_atp_record.action;
9419                    g_atp_record.ship_date := p_atp_record.ship_date;
9420                    g_atp_record.available_quantity :=
9421                              p_atp_record.available_quantity;
9422                    g_atp_record.used_available_quantity :=
9423                              p_atp_record.used_available_quantity;
9424                    g_atp_record.requested_date_quantity :=
9425                              p_atp_record.requested_date_quantity;
9426                    g_atp_record.supplier_id := p_atp_record.supplier_id;
9427                    g_atp_record.supplier_site_id := p_atp_record.supplier_site_id;
9428                    g_atp_record.insert_flag := p_atp_record.insert_flag;
9429                    g_atp_record.order_number := p_atp_record.order_number;
9430                    g_atp_record.demand_source_line :=
9431                              p_atp_record.demand_source_line;
9432                    g_atp_record.demand_source_header_id :=
9433                              p_atp_record.demand_source_header_id;
9434                    g_atp_record.demand_source_type :=
9435                              p_atp_record.demand_source_type;
9436                    g_atp_record.shipping_cal_code := p_atp_record.shipping_cal_code; -- Bug 3371817
9437                    g_atp_record.receiving_cal_code := p_atp_record.receiving_cal_code; -- Bug 3826234
9438                    g_atp_record.intransit_cal_code := p_atp_record.intransit_cal_code; -- Bug 3826234
9439                    g_atp_record.manufacturing_cal_code := p_atp_record.manufacturing_cal_code; -- Bug 3826234
9440                    g_atp_record.to_organization_id := p_atp_record.to_organization_id; -- Bug 3826234
9441 
9442                    -- for bug 1410327
9443                    g_atp_record.identifier :=
9444                              p_atp_record.identifier;
9445 
9446                    l_stealing_requested_date_qty := l_net_demand;
9447 
9448                   --pumehta Bug2471377 Copy the period ATP to another table
9449                   --Which can be used later for another forward pass
9450                   --to find better date for the case where component
9451                   --Flag is set to 'N'and stealing has happened.
9452 		  --dsting port 2621270. For sd details, we save the pegging id
9453                   --and copy the data from mrp_atp_details and give it a new pegging id
9454 
9455                    l_atp_period_first := l_atp_period;
9456                    --diag-atp
9457                    g_atp_record.plan_name := p_atp_record.plan_name;
9458 
9459                    -- time_phased_atp
9460                    g_atp_record.atf_date := p_atp_record.atf_date;
9461                    g_atp_record.ato_model_line_id := p_atp_record.ato_model_line_id;
9462                    MSC_AATP_PVT.Stealing(
9463                             g_atp_record,
9464                             l_pegging_id,
9465                             p_scenario_id,
9466                             p_level,
9467                             p_search,
9468                             p_plan_id,
9469                             l_net_demand,  -- out variable
9470                             l_mem_stealing_qty, -- For time_phased_atp
9471                             l_pf_stealing_qty,  -- For time_phased_atp
9472                             l_atp_supply_demand,
9473                             l_atp_period,
9474                             l_return_status,
9475                             p_refresh_number);   -- For summary enhancement
9476 
9477                    l_stealing_qty := l_stealing_requested_date_qty -
9478                                      greatest(l_net_demand, 0);
9479 
9480                    IF PG_DEBUG in ('Y', 'C') THEN
9481                       msc_sch_wb.atp_debug('ATP_Check: ' || 'l_stealing_qty = '||l_stealing_qty);
9482                       msc_sch_wb.atp_debug('ATP_Check: ' || 'l_mem_stealing_qty = '||l_mem_stealing_qty);
9483                       msc_sch_wb.atp_debug('ATP_Check: ' || 'l_pf_stealing_qty = '||l_pf_stealing_qty);
9484                       msc_sch_wb.atp_debug('ATP_Check: ' || 'l_net_demand = ' || l_net_demand);
9485                    END IF;
9486 
9487                    -- using the pegging_id to find the demand_id and plan_id
9488                    ---demandxyz
9489                    --- Initially we created the demand for l_requested_date_qty
9490                    --- now we update the quantity
9491                    update msc_demands
9492                    set USING_REQUIREMENT_QUANTITY = p_atp_record.quantity_ordered
9493                    where plan_id = p_plan_id and
9494                          demand_id = l_demand_id;
9495 
9496                    /* time_phased_atp
9497                       put qty stolen upto req date ((not upto ATF) on member item */
9498                    --5211558 we want to update the demands only in first pass and not in further passes
9499                    IF l_time_phased_atp = 'Y' and MSC_ATP_PVT.G_OPTIONAL_FW is null THEN
9500 		        --using the same insert rec we prepared earlier
9501 		        l_atp_insert_rec.quantity_ordered :=  p_atp_record.quantity_ordered;
9502 		        l_atp_insert_rec.requested_date_quantity := l_atp_insert_rec.requested_date_quantity + nvl(l_stealing_qty, 0);
9503 		        l_atp_insert_rec.atf_date_quantity := l_atp_insert_rec.atf_date_quantity + nvl(l_mem_stealing_qty, 0);
9504 		        MSC_ATP_PF.Increment_Bucketed_Demands_Qty(
9505                                 l_atp_insert_rec,
9506                                 p_plan_id,
9507                                 l_demand_id,
9508                                 l_return_status
9509 		        );
9510                         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
9511                            IF PG_DEBUG in ('Y', 'C') THEN
9512                                 msc_sch_wb.atp_debug('ATP_Check: ' || 'Error occured in procedure Increment_Bucketed_Demands_Qty');
9513                            END IF;
9514                            RAISE FND_API.G_EXC_ERROR;
9515                         END IF;
9516                         /* update atf date quantity */
9517                         p_atp_record.atf_date_quantity := l_atp_insert_rec.atf_date_quantity;
9518                         /* Reset l_atf_date_qty to 0*/
9519                         l_atf_date_qty := 0;
9520                    ELSIF MSC_ATP_PVT.G_INV_CTP = 4 and MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y'
9521                           AND MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1 AND MSC_ATP_PVT.G_ALLOCATION_METHOD = 1 THEN
9522 
9523                              update msc_alloc_demands
9524                              set allocated_quantity = p_atp_record.quantity_ordered
9525                              where parent_demand_id = l_demand_id
9526                              and   plan_id = p_plan_id;
9527 
9528                    END IF;
9529 
9530                    IF (l_stealing_qty > 0) THEN
9531                          ----bug 1892779
9532                         MSC_ATP_PROC.Details_Output(l_atp_period,
9533                                  l_atp_supply_demand,
9534                                  x_atp_period,
9535                                  x_atp_supply_demand,
9536                                  l_return_status);
9537                    END IF;
9538 
9539                    l_stealing_requested_date_qty := l_stealing_requested_date_qty -
9540                                                 l_net_demand;
9541 
9542                    l_net_demand := greatest(l_net_demand, 0);
9543 
9544                    IF PG_DEBUG in ('Y', 'C') THEN
9545                       msc_sch_wb.atp_debug('ATP_Check: ' || 'After Stealing :'|| l_net_demand);
9546                       msc_sch_wb.atp_debug('ATP_Check: ' || 'l_stealing_requested_date_qty = '||
9547                                           l_stealing_requested_date_qty);
9548                    END IF;
9549    /* bug 1680212
9550                    -- In order to show the correct demand quantity that got
9551                    -- considered for the primary component,
9552                    -- we need to change the requested quantity for that primary
9553                    -- component to what was requested minus
9554                    -- what got satisfied by the stealing
9555 
9556                    update msc_demands
9557                    set    USING_REQUIREMENT_QUANTITY =
9558                           (p_atp_record.quantity_ordered - l_stealing_qty)
9559                    where  demand_id = l_demand_id
9560                    and    plan_id = p_plan_id;
9561      bug 1680212 */
9562                END IF; -- end if G_ALLOCATED_ATP
9563                -- PTF
9564                IF (p_search = FORWARD_SCHEDULING) AND
9565                   (p_parent_pegging_id IS NOT  NULL) AND
9566                   (MSC_ATP_PVT.G_PTF_DATE > l_requested_ship_date) THEN
9567                   IF PG_DEBUG in ('Y', 'C') THEN
9568                      msc_sch_wb.atp_debug('ATP_Check: ' || 'Look forward to meet the remaining shortage of comp');
9569                   END IF;
9570 		   --Bug2682094 Do not reset the demand
9571 		   --l_net_demand := p_atp_record.quantity_ordered;
9572                   l_req_date_qty := 0;
9573                ELSE
9574                   l_req_date_qty := null;
9575                END IF;
9576 
9577           END IF; -- if SUBS_FLAG = 'Y'
9578 
9579 
9580             ---here we add hack for config item where flag  = 'Y', 'N'
9581             IF l_net_demand > 0 and C_ITEM_INFO_REC.bom_item_type = 1 and
9582                                     C_ITEM_INFO_REC.replenish_to_ord_flag = 'Y' and
9583                                     C_ITEM_INFO_REC.atp_flag = 'Y' and
9584                                     C_ITEM_INFO_REC.atp_comp_flag = 'N' and
9585                                     MSC_ATP_PVT.G_INV_CTP = 4 THEN
9586 
9587                 MSC_ATP_CTO.Check_Base_Model_For_Cap_Check(C_ITEM_INFO_REC.dest_inv_item_id,
9588                                                            C_ITEM_INFO_REC.base_item_id,
9589                                                            l_requested_ship_date,
9590                                                            p_atp_record.instance_id,
9591                                                            p_plan_id,
9592                                                            p_atp_record.organization_id,
9593                                                            l_net_demand,
9594                                                            l_sr_inv_item_id,
9595                                                            l_check_model_capacity);
9596 
9597                 IF PG_DEBUG in ('Y', 'C') THEN
9598                    msc_sch_wb.atp_debug('l_check_model_capacity := ' || l_check_model_capacity);
9599                 END IF;
9600                 IF l_check_model_capacity = 1 THEN
9601                     --- we set comp flag to  Y so that it goes into the logic of make
9602                     l_atp_comp_flag := 'Y';
9603                     --null out the lead time as well as we want to check the base model's capacity same day
9604 
9605                     l_fixed_lt := 0;
9606                     l_variable_lt := 0;
9607                     l_pre_pro_lt := 0;
9608                 END IF;
9609             END IF;
9610 
9611             IF l_net_demand <= 0 THEN
9612               IF PG_DEBUG in ('Y', 'C') THEN
9613                  msc_sch_wb.atp_debug('Demabnd is Met');
9614                  msc_sch_wb.atp_debug('Requested Date quantity := ' ||  l_requested_date_quantity);
9615               END IF;
9616                -- we meet the requirements!!!
9617                --commenting for bug3550292
9618               /*p_atp_record.requested_date_quantity:= l_requested_date_quantity
9619                                                      +  l_sub_requested_date_quantity
9620                                                      + l_stealing_qty;*/
9621               --bug3550292
9622               p_atp_record.requested_date_quantity:= l_requested_date_quantity;
9623               -- cchen: 1238941
9624               -- 1411818: add greatest
9625               p_atp_record.combined_requested_date_qty :=
9626                   GREATEST(p_atp_record.requested_date_quantity, 0) +
9627                   l_sub_requested_date_quantity + l_stealing_requested_date_qty;
9628 
9629               /* s_cto_rearch: do not honor atp lead time
9630               p_atp_record.ship_date := MSC_CALENDAR.DATE_OFFSET
9631                                  (p_atp_record.organization_id,
9632                                   p_atp_record.instance_id,
9633                                   1,
9634                                   l_requested_ship_date, -- bug 1338599
9635                                   NVL(p_atp_record.atp_lead_time, 0));
9636               */
9637               p_atp_record.ship_date := l_requested_ship_date;
9638               -- for aatp
9639               p_atp_record.available_quantity:=
9640                   p_atp_record.combined_requested_date_qty;
9641               p_atp_record.used_available_quantity:=
9642                   p_atp_record.combined_requested_date_qty; --bug3409973
9643               -- p_atp_record.available_quantity:=l_atp_date_quantity_this_level;
9644               --optional_fw further iterations error code should be set 53
9645              IF MSC_ATP_PVT.G_OPTIONAL_FW is not null THEN
9646               p_atp_record.error_code := ATP_REQ_DATE_FAIL;
9647              END IF;
9648 	      IF PG_DEBUG in ('Y', 'C') THEN
9649 	         msc_sch_wb.atp_debug('ATP_Check: ' || 'p_atp_record.atf_date_quantity = '||p_atp_record.atf_date_quantity);
9650 	         msc_sch_wb.atp_debug('ATP_Check: ' || 'after insert row, 1');
9651 	      END IF;
9652             --diag_atp
9653             ELSIF (l_atp_comp_flag = 'N') AND MSC_ATP_PVT.G_DIAGNOSTIC_ATP <> 1 THEN
9654                 IF PG_DEBUG in ('Y', 'C') THEN
9655                    msc_sch_wb.atp_debug('ATP_Check: ' || 'ATP Component flag = N');
9656                 END IF;
9657 
9658                 -- we cannot meet the requirements, so we provide
9659                 -- the information at this level
9660                 --optional_fw
9661                 --Either we set the component flag and do not do any more iterations, for this
9662                 --we neeed to set this flag only for the top org and that too for first b.w pass only so that subsequent
9663                 --f,w pass may not be needed and loop may surely exit.
9664 
9665                 IF (p_parent_pegging_id IS NULL and MSC_ATP_PVT.G_OPTIONAL_FW is null and G_FORWARD_ATP  = 'Y') then
9666                     MSC_ATP_PVT.G_ATP_COMP_FLAG := 'N'; --setting global variable for optional_fw
9667                 END IF;
9668 
9669                 p_atp_record.requested_date_quantity:=l_requested_date_quantity;
9670 
9671 		IF MSC_ATP_PVT.G_INV_CTP = 4 and MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y' AND
9672 		   (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1) AND
9673 		   (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1)
9674                    --support forward stealing for all levels
9675 		   AND p_parent_pegging_id IS NULL
9676 		THEN
9677 		   IF PG_DEBUG in ('Y', 'C') THEN
9678 		      msc_sch_wb.atp_debug('ATP_Check: ' || 'Get forward mat...');
9679 		      msc_sch_wb.atp_debug('ATP_Check: ' || 'requested_date quantity: ' || l_requested_date_quantity);
9680 		      msc_sch_wb.atp_debug('ATP_Check: ' || 'l_net_demand: ' || l_net_demand);
9681 		      msc_sch_wb.atp_debug('ATP_Check: ' || 'l_demand_id: ' || l_demand_id);
9682 		   END IF;
9683 
9684 		   update msc_demands
9685 		   set using_requirement_quantity = MSC_ATP_UTILS.Truncate_Demand(p_atp_record.requested_date_quantity + l_stealing_requested_date_qty)
9686 		   where demand_id = l_demand_id
9687 	      	   and plan_id = p_plan_id;
9688 
9689 	     	   -- time_phased_atp changes begin
9690 		   IF l_time_phased_atp = 'Y' THEN
9691         	        --using the same insert rec we prepared earlier
9692         	        l_atp_insert_rec.quantity_ordered :=  p_atp_record.quantity_ordered - l_net_demand;
9693                         l_atp_insert_rec.requested_date_quantity := GREATEST(p_atp_record.requested_date_quantity, 0)
9694                                                                                  + nvl(l_stealing_requested_date_qty, 0);
9695         	        l_atp_insert_rec.atf_date_quantity := NVL(p_atp_record.atf_date_quantity, 0);
9696                         --5211558 not needed as when comp flag is no then only one iteration
9697         	        MSC_ATP_PF.Increment_Bucketed_Demands_Qty(
9698                                         l_atp_insert_rec,
9699                                         p_plan_id,
9700                                         l_demand_id,
9701                                         l_return_status
9702         		        );
9703                         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
9704                            IF PG_DEBUG in ('Y', 'C') THEN
9705                                 msc_sch_wb.atp_debug('ATP_Check: ' || 'Error occured in procedure Increment_Bucketed_Demands_Qty');
9706                             END IF;
9707                             RAISE FND_API.G_EXC_ERROR;
9708                         END IF;
9709 	           ELSE
9710         	        update msc_alloc_demands
9711         		set allocated_quantity = p_atp_record.requested_date_quantity + l_stealing_requested_date_qty
9712         		where parent_demand_id = l_demand_id
9713         		and plan_id = p_plan_id;
9714 		   END IF;
9715 	       	   -- time_phased_atp changes end
9716 
9717                    --diag_atp
9718                    l_get_mat_in_rec.rounding_control_flag := l_rounding_control_flag;
9719                    l_get_mat_in_rec.dest_inv_item_id := l_dest_inv_item_id;
9720                    l_get_mat_in_rec.parent_bom_item_type := p_atp_record.parent_bom_item_type;
9721                    l_get_mat_in_rec.bom_item_type := C_ITEM_INFO_REC.bom_item_type;
9722                    l_get_mat_in_rec.replenish_to_ord_flag := C_ITEM_INFO_REC.replenish_to_ord_flag;
9723                    l_get_mat_in_rec.parent_repl_order_flag := p_atp_record.parent_repl_order_flag;
9724                    l_get_mat_in_rec.ato_model_line_id := p_atp_record.ato_model_line_id;
9725                    l_get_mat_in_rec.shipping_cal_code := p_atp_record.shipping_cal_code;
9726                    l_get_mat_in_rec.receiving_cal_code := p_atp_record.receiving_cal_code; -- Bug 3826234
9727                    l_get_mat_in_rec.intransit_cal_code := p_atp_record.intransit_cal_code; -- Bug 3826234
9728                    l_get_mat_in_rec.manufacturing_cal_code := p_atp_record.manufacturing_cal_code; -- Bug 3826234
9729                    l_get_mat_in_rec.to_organization_id := p_atp_record.to_organization_id; -- Bug 3826234
9730                    -- Bug 3371817 - Pass calendar code to calculate sys_next_date
9731                    --               In make case this will actually contain OMC for lower level components
9732 
9733 
9734 		   MSC_AATP_REQ.Get_Forward_Material_ATP(
9735                             p_atp_record.instance_id,
9736                             p_plan_id,
9737                             p_level+1,
9738                             p_atp_record.identifier,
9739                             p_atp_record.demand_source_type,--cmro
9740                             p_scenario_id,
9741                             p_atp_record.inventory_item_id,
9742                             p_atp_record.request_item_id, -- For time_phased_atp
9743                             p_atp_record.organization_id,
9744                             C_ITEM_INFO_REC.item_name,   -- Modular re-use
9745                             PF_ITEM_INFO_REC.item_name,  -- For time_phased_atp
9746                             l_requested_ship_date,
9747                             l_net_demand,
9748                             p_atp_record.demand_class,
9749                             l_requested_date_quantity,
9750                             l_atf_date_qty, -- For time_phased_atp
9751                             l_atp_date_this_level,
9752                             l_atp_quantity_this_level_fwd,
9753                             l_atp_pegging_tab,
9754                             l_return_status,
9755                             l_used_available_quantity_fwd, --bug3409973
9756                             --diag_atp,
9757                             null,
9758                             l_get_mat_in_rec,
9759                             l_get_mat_out_rec,
9760                             p_atp_record.atf_date, -- For time_phased_atp
9761                             null,               -- order number
9762                             p_refresh_number    -- For summary enhancement
9763 			);
9764 		   l_atp_date_quantity_this_level :=
9765                   		GREATEST(p_atp_record.requested_date_quantity, 0) +
9766                   		l_sub_requested_date_quantity +
9767 				l_atp_quantity_this_level_fwd;
9768 	           l_used_available_quantity :=
9769                   		GREATEST(p_atp_record.requested_date_quantity, 0) +
9770                   		l_sub_requested_date_quantity +
9771 				l_used_available_quantity_fwd; --bug3409973
9772                    -- time_phased_atp
9773                    IF l_requested_ship_date <= p_atp_record.atf_date THEN
9774                        p_atp_record.atf_date_quantity := NVL(l_atp_insert_rec.requested_date_quantity, 0) + NVL(l_atf_date_qty, 0);
9775                    ELSE
9776                        p_atp_record.atf_date_quantity := NVL(p_atp_record.atf_date_quantity, 0) + NVL(l_atf_date_qty, 0);
9777                    END IF;
9778 
9779 		   IF PG_DEBUG in ('Y', 'C') THEN
9780 		       msc_sch_wb.atp_debug('ATP_Check: ' || 'atp_date_this_level: ' || l_atp_date_this_level);
9781 		       msc_sch_wb.atp_debug('ATP_Check: ' || 'atp_qty_this_level: ' || l_atp_date_quantity_this_level);
9782 		       msc_sch_wb.atp_debug('ATP_Check: ' || 'l_used_available_quantity: ' || l_used_available_quantity); --bug3409973
9783 		       msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atf_date_qty : ' || l_atf_date_qty);
9784 		       msc_sch_wb.atp_debug('ATP_Check: ' || 'p_atp_record.atf_date_quantity : ' || p_atp_record.atf_date_quantity);
9785 		   END IF;
9786 
9787 		ELSE
9788 		   IF (l_stealing_requested_date_qty > 0) AND
9789 		      (MSC_ATP_PVT.G_ALLOCATION_METHOD = 2)
9790 		   THEN
9791         	        --pumehta Bug2471377 Begin Changes
9792         	       	--Make another pass to check if we can get a better date
9793 		        --as stealing has happened.
9794         	       	l_requested_quantity := p_atp_record.quantity_ordered -
9795                                 l_stealing_requested_date_qty;
9796 	               	IF PG_DEBUG in ('Y', 'C') THEN
9797 	               	   msc_sch_wb.atp_debug('ATP_Check: ' || 'Do a Second pass to find better date for Qty:' || l_requested_quantity);
9798 			   msc_sch_wb.atp_debug('ATP_Check: ' || 'Count:' || l_atp_period_first.period_start_date.COUNT);
9799 			END IF;
9800 
9801 			FOR i in 1..l_atp_period_first.period_start_date.COUNT LOOP
9802 				IF i <> l_atp_period_first.period_start_date.COUNT THEN
9803 					IF (l_atp_period_first.cumulative_quantity(i) >=
9804 					    l_requested_quantity) AND
9805 					   ((l_atp_period_first.period_start_date(i) <=
9806                        				l_sysdate AND
9807                        			    l_atp_period_first.period_start_date(i+1) >
9808                            			l_sysdate)
9809                           		   OR (l_atp_period_first.period_start_date(i) >
9810                               			l_sysdate))
9811 					THEN
9812                        				IF (MSC_ATP_PVT.G_ITEM_INFO_REC.rounding_control_type = 1) THEN
9813                           				l_atp_date_quantity_this_level :=
9814                           				FLOOR(l_atp_period_first.cumulative_quantity(i));
9815                        				ELSE
9816                            				l_atp_date_quantity_this_level :=
9817                            				l_atp_period_first.cumulative_quantity(i);
9818                        				END IF;
9819                       				l_atp_date_this_level :=
9820                       				GREATEST(l_atp_period_first.period_start_date(i),l_sysdate);
9821                       				EXIT;
9822                     			END IF;
9823                    		END IF;
9824                   	END LOOP;
9825 			IF PG_DEBUG in ('Y', 'C') THEN
9826 			   msc_sch_wb.atp_debug('ATP_Check: ' || 'Avail Date II pass is ' || l_atp_date_this_level);
9827 			   msc_sch_wb.atp_debug('ATP_Check: ' || 'Avail Qty II Pass is ' || l_atp_date_quantity_this_level);
9828 			END IF;
9829 			--End Changes bug2471377 pumehta
9830                    END IF;
9831 
9832                    l_used_available_quantity := l_atp_date_quantity_this_level; --bug3409973
9833 
9834 		END IF;
9835 
9836                 -- cchen: 1238941
9837                 p_atp_record.combined_requested_date_qty :=
9838                      p_atp_record.requested_date_quantity +
9839                      l_sub_requested_date_quantity+l_stealing_requested_date_qty;
9840 
9841                 p_atp_record.available_quantity:=LEAST(INFINITE_NUMBER,
9842                                                (l_atp_date_quantity_this_level+
9843                                                 l_stealing_qty));
9844 
9845                 p_atp_record.used_available_quantity:=LEAST(INFINITE_NUMBER,
9846                                                (l_used_available_quantity+
9847                                                 l_stealing_qty)); --bug3409973
9848                 -- 2859130 move to next work day for forward pass
9849                 /* Do not honor atp lead time
9850                 IF NVL(p_atp_record.atp_lead_time, 0) > 0 THEN
9851                    p_atp_record.ship_date := MSC_CALENDAR.DATE_OFFSET
9852                                     (p_atp_record.organization_id,
9853                                      p_atp_record.instance_id,
9854                                      1,
9855                                      l_atp_date_this_level,
9856                                      NVL(p_atp_record.atp_lead_time, 0));
9857                 ELSE
9858                    p_atp_record.ship_date := MSC_CALENDAR.NEXT_WORK_DAY
9859                                     (p_atp_record.organization_id,
9860                                      p_atp_record.instance_id,
9861                                      1,
9862                                      l_atp_date_this_level);
9863                 END IF;
9864                 */
9865                 --Bug 3855866: If l_atp_date_this_level is NULL, set p_atp_record.ship_date
9866                 -- to NULL, thus error_code is set to ATP_REQ_QTY_FAIL(52)
9867 
9868                 IF (l_atp_date_this_level is NULL ) THEN
9869                    p_atp_record.ship_date := NULL;
9870                 ELSE
9871                    p_atp_record.ship_date := MSC_CALENDAR.next_work_day(
9872                                         p_atp_record.shipping_cal_code,
9873                                         p_atp_record.instance_id,
9874                                         l_atp_date_this_level);
9875                 END IF;
9876 
9877                 IF PG_DEBUG in ('Y', 'C') THEN
9878                    msc_sch_wb.atp_debug('ATP_Check: ' || 'p_atp_record.ship_date' || p_atp_record.ship_date);
9879                    msc_sch_wb.atp_debug('ATP_Check: ' || 'avail qty: ' || p_atp_record.available_quantity);
9880                    msc_sch_wb.atp_debug('ATP_Check: ' || 'used avail qty: ' || p_atp_record.used_available_quantity); --bug3409973
9881                    msc_sch_wb.atp_debug('ATP_Check: ' || 'qty level: ' || l_atp_date_quantity_this_level);
9882                    msc_sch_wb.atp_debug('ATP_Check: ' || 'stealing qty level: ' || l_stealing_qty);
9883                    msc_sch_wb.atp_debug('ATP_Check: ' || 'atf date qty: ' || p_atp_record.atf_date_quantity);
9884                 END IF;
9885                 -- p_atp_record.available_quantity:=l_atp_date_quantity_this_level;
9886                 IF p_atp_record.ship_date IS NOT NULL THEN
9887                     p_atp_record.error_code := ATP_REQ_DATE_FAIL;
9888 
9889 /* 1421413: move into the if below
9890                     -- bug 1302394: we should adjust the date
9891                     -- of the demand which causes this planned order
9892                     -- as well so that they can cancel each other.
9893 
9894                     UPDATE msc_demands
9895                     SET  USING_ASSEMBLY_DEMAND_DATE = l_atp_date_this_level
9896 --ngoel 8/30/2000 Check if this needs to be changed to l_atp_date_this_level.
9897                     WHERE  demand_id = l_demand_id
9898                     AND    plan_id = p_plan_id;
9899 
9900                     -- 1262907: no matter this is the components or not,
9901                     -- we need to populate the pegging tree
9902                     -- IF p_parent_pegging_id is null THEN
9903 */
9904                     -- 1421413: only populate pegging if
9905                     -- this is for top assembly or
9906                     -- this is for components and it is the future case
9907 
9908                     -- IF (p_parent_pegging_id is null) OR   commented for bug 2738280.
9909 		    -- This piece of code moves the demand date which is also done in procedure update_sd_date
9910 		    -- Since update_sd_date was not called for last line of SLSO ATO model, therefore, the summary
9911 		    -- picture was not matching with details picture for last line.
9912 		    -- On commenting this line, this piece of code is never executed for SLSO ATO model lines
9913 		    -- making the summary picture consistent with details picture for all the lines.
9914                     ---bug 3098127: the following condition was removed by the bug 2738280.
9915                     -- this shouldn't have been removed.
9916 
9917                     IF   (p_parent_pegging_id is null) OR
9918                         ((p_parent_pegging_id is not null AND
9919                         p_search = FORWARD_SCHEDULING)) THEN
9920 
9921 			-- dsting ATO 2465370
9922 			-- in case of override for CTO, do not adjust the demand date
9923                         --bug 3564511: This condition is not necessary as we always want to
9924                         -- adjust dates
9925 		        --IF NVL(G_LINE_OVERRIDE_FLAG, 'N') = 'N'
9926 			   --OR (NVL(C_ITEM_INFO_REC.cto_source_org_id, -1) <> p_atp_record.organization_id
9927 			   --AND p_parent_pegging_id is not null)
9928 			--THEN
9929                                 --bug 3328421: required date contains the original request date
9930                                 -- actual_supply_demand_date contain the actual sd date
9931 				--update mrp_atp_details_temp set required_date = TRUNC(l_atp_date_this_level) + MSC_ATP_PVT.G_END_OF_DAY
9932 				update mrp_atp_details_temp set actual_supply_demand_date =
9933                                                                  TRUNC(l_atp_date_this_level)--4460369 + MSC_ATP_PVT.G_END_OF_DAY
9934 				where session_id = MSC_ATP_PVT.G_SESSION_ID
9935 				and pegging_id = l_pegging_id
9936 				and record_type = 3;
9937 
9938 				IF PG_DEBUG in ('Y', 'C') THEN
9939 					msc_sch_wb.atp_debug('ATO update details_temp 1: ' || l_pegging_id || ' date: ' || l_atp_date_this_level);
9940                                         msc_sch_wb.atp_debug('Update demand with demand id := ' || l_demand_id);
9941                                         msc_sch_wb.atp_debug('Update demand on date : = ' || l_atp_date_this_level);
9942 				END IF;
9943 
9944                                 -- bug 1302394: we should adjust the date
9945                                 -- of the demand which causes this planned order
9946                                 -- as well so that they can cancel each other.
9947 
9948                 -- For bug 2259824, move the demand to the end of day
9949                                 --s_cto_rearch: For ODS based ATP, we should be updating msc_sales_orders
9950                                 IF p_plan_id = -1 THEN
9951                                    UPDATE msc_sales_orders
9952                                    SET REQUIREMENT_DATE = TRUNC(l_atp_date_this_level) --4460369+ MSC_ATP_PVT.G_END_OF_DAY
9953                                    WHERE sr_instance_id  = p_atp_record.instance_id
9954                                    AND   DEMAND_ID = l_demand_id
9955                                    returning inventory_item_id into l_inventory_item_id;
9956                                 ELSE
9957                                    UPDATE msc_demands
9958                                    SET
9959                                    --start changes for plan by request date
9960                                    USING_ASSEMBLY_DEMAND_DATE =
9961                                         DECODE(ORIGINATION_TYPE,
9962                                                6,  decode(MSC_ATP_PVT.G_PLAN_INFO_REC.schedule_by_date_type,
9963                                                           MSC_ATP_PVT.G_SCHEDULE_SHIP_DATE_LEGEND,
9964                                                               TRUNC(l_atp_date_this_level) + MSC_ATP_PVT.G_END_OF_DAY,
9965                                                           MSC_ATP_PVT.G_SCHEDULE_ARRIVAL_DATE_LEGEND,
9966                                                               TRUNC(l_atp_date_this_level) + MSC_ATP_PVT.G_END_OF_DAY,
9967                                                           MSC_ATP_PVT.G_PROMISE_SHIP_DATE_LEGEND,
9968                                                               TRUNC(l_atp_date_this_level) + MSC_ATP_PVT.G_END_OF_DAY,
9969                                                           MSC_ATP_PVT.G_PROMISE_ARRIVAL_DATE_LEGEND,
9970                                                               TRUNC(l_atp_date_this_level) + MSC_ATP_PVT.G_END_OF_DAY,
9971                                                           MSC_ATP_PVT.G_REQUEST_SHIP_DATE_LEGEND,
9972                                                               l_original_req_ship_date,
9973                                                           MSC_ATP_PVT.G_REQUEST_ARRIVAL_DATE_LEGEND,
9974                                                               l_original_req_ship_date,
9975                                                               TRUNC(l_atp_date_this_level) + MSC_ATP_PVT.G_END_OF_DAY),
9976                                                30, decode(MSC_ATP_PVT.G_PLAN_INFO_REC.schedule_by_date_type,
9977                                                           MSC_ATP_PVT.G_SCHEDULE_SHIP_DATE_LEGEND,
9978                                                               TRUNC(l_atp_date_this_level) + MSC_ATP_PVT.G_END_OF_DAY,
9979                                                           MSC_ATP_PVT.G_SCHEDULE_ARRIVAL_DATE_LEGEND,
9980                                                               TRUNC(l_atp_date_this_level) + MSC_ATP_PVT.G_END_OF_DAY,
9981                                                           MSC_ATP_PVT.G_PROMISE_SHIP_DATE_LEGEND,
9982                                                               TRUNC(l_atp_date_this_level) + MSC_ATP_PVT.G_END_OF_DAY,
9983                                                           MSC_ATP_PVT.G_PROMISE_ARRIVAL_DATE_LEGEND,
9984                                                               TRUNC(l_atp_date_this_level) + MSC_ATP_PVT.G_END_OF_DAY,
9985                                                           MSC_ATP_PVT.G_REQUEST_SHIP_DATE_LEGEND,
9986                                                               l_original_req_ship_date,
9987                                                           MSC_ATP_PVT.G_REQUEST_ARRIVAL_DATE_LEGEND,
9988                                                               l_original_req_ship_date,
9989                                                               TRUNC(l_atp_date_this_level) + MSC_ATP_PVT.G_END_OF_DAY),
9990                                                TRUNC(l_atp_date_this_level) + MSC_ATP_PVT.G_END_OF_DAY),
9991                                    schedule_ship_date = DECODE(ORIGINATION_TYPE,
9992 	                                                       6,  TRUNC(l_atp_date_this_level) + MSC_ATP_PVT.G_END_OF_DAY,
9993 	                                                       30, TRUNC(l_atp_date_this_level) + MSC_ATP_PVT.G_END_OF_DAY,
9994 	                                                       NULL),
9995                                    promise_ship_date  = DECODE(ORIGINATION_TYPE,
9996                                                                6,  TRUNC(l_atp_date_this_level) + MSC_ATP_PVT.G_END_OF_DAY,
9997                                                                30, TRUNC(l_atp_date_this_level) + MSC_ATP_PVT.G_END_OF_DAY,
9998                                                                NULL)
9999                                    --end of plan by request date enhanc
10000                                    WHERE  demand_id = l_demand_id
10001                                    AND    plan_id = p_plan_id
10002                                    returning inventory_item_id into l_inventory_item_id;
10003                                 END IF;
10004 
10005 
10006                                 -- time_phased_atp changes begin
10007                                 IF l_time_phased_atp = 'Y' THEN
10008                                     msc_sch_wb.atp_debug('ATP_Check: ' || 'I come here 123');
10009                                          --bug3397904 start
10010                                          IF p_atp_record.requested_ship_date <= p_atp_record.atf_date THEN
10011                                             l_bkwd_pass_atf_date_qty := GREATEST(NVL(p_atp_record.combined_requested_date_qty,
10012                                             p_atp_record.requested_date_quantity), 0);
10013                                          ELSE
10014                                             l_bkwd_pass_atf_date_qty := p_atp_record.atf_date_quantity;
10015                                          END IF;
10016                                         --bug3397904 end
10017                                         --5211558 not needed as when comp flag is no then only one iteration
10018                                         MSC_ATP_PF.Move_PF_Bucketed_Demands(
10019                                                 p_plan_id,
10020                                                 l_demand_id,
10021                                                 p_atp_record.requested_ship_date,
10022                                                 l_atp_date_this_level,
10023                                                 p_atp_record.quantity_ordered,--bug3397904 changed from NULL
10024                                                 p_atp_record.used_available_quantity,--bug3397904 changed from NULL
10025                                                 p_atp_record.atf_date,
10026                                                 p_atp_record.atf_date_quantity, -- --bug3397904 changed from NULL
10027                                                 l_return_status,
10028                                                 l_bkwd_pass_atf_date_qty --bug3397904
10029                                         );
10030                                         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
10031                                                 IF PG_DEBUG in ('Y', 'C') THEN
10032                                                         msc_sch_wb.atp_debug('ATP_Check: ' || 'Error occured in procedure Move_PF_Bucketed_Demands');
10033                                                 END IF;
10034                                                 RAISE FND_API.G_EXC_ERROR;
10035                                         END IF;
10036                                 ELSIF MSC_ATP_PVT.G_INV_CTP = 4 and MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y'
10037                                     AND MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1 AND MSC_ATP_PVT.G_ALLOCATION_METHOD = 1 THEN
10038                                     update msc_alloc_demands
10039                                     set DEMAND_DATE = l_atp_date_this_level
10040                                     where parent_demand_id = l_demand_id
10041                                     and   plan_id = p_plan_id;
10042                                 END IF;
10043                                 -- time_phased_atp changes end
10044 
10045                                 -- update summary records removed in ODS cases - for summary enhancement
10046                                 IF MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y'  and (p_parent_pegging_id is not null) AND p_plan_id = -1 THEN
10047                                     IF PG_DEBUG in ('Y', 'C') THEN
10048                                         msc_sch_wb.atp_debug('ATP_Check: ' || 'update old date demand');
10049                                     END IF;
10050                                     MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(p_atp_record.instance_id,
10051                                                         p_plan_id,
10052                                                         p_atp_record.organization_id,
10053                                                         l_inventory_item_id,
10054                                                         l_requested_ship_date,
10055                                                         null,
10056                                                         null,
10057                                                         null,
10058                                                         null,
10059                                                         null,
10060                                                         null,
10061                                                         2);
10062 
10063                                     update /*+ INDEX(msc_atp_summary_sd MSC_ATP_SUMMARY_SD_U1) */ msc_atp_summary_sd
10064                                     set sd_qty = sd_qty + p_atp_record.quantity_ordered
10065                                     where plan_id = p_plan_id
10066                                     and   sr_instance_id = p_atp_record.instance_id
10067                                     and   organization_id = p_atp_record.organization_id
10068                                     and   inventory_item_id = l_inventory_item_id
10069                                     and   sd_date = trunc(l_requested_ship_date);
10070                                     commit;
10071                                     MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(p_atp_record.instance_id,
10072                                                         p_plan_id,
10073                                                         p_atp_record.organization_id,
10074                                                         l_inventory_item_id,
10075                                                         l_atp_date_this_level,
10076                                                         null,
10077                                                         null,
10078                                                         null,
10079                                                         null,
10080                                                         null,
10081                                                         null,
10082                                                         2);
10083 
10084 
10085                                     IF PG_DEBUG in ('Y', 'C') THEN
10086                                         msc_sch_wb.atp_debug('ATP_Check: ' || 'update demand on new date');
10087                                     END IF;
10088                                     update /*+ INDEX(msc_atp_summary_sd MSC_ATP_SUMMARY_SD_U1) */ msc_atp_summary_sd
10089                                     set sd_qty = sd_qty - p_atp_record.quantity_ordered
10090                                     where plan_id = p_plan_id
10091                                     and   sr_instance_id = p_atp_record.instance_id
10092                                     and   organization_id = p_atp_record.organization_id
10093                                     and   inventory_item_id = l_inventory_item_id
10094                                     and   sd_date = trunc(l_atp_date_this_level);
10095                                     commit;
10096                                     IF SQL%NOTFOUND THEN
10097                                         IF PG_DEBUG in ('Y', 'C') THEN
10098                                             msc_sch_wb.atp_debug('ATP_Check: ' || 'insert demand on new date');
10099                                         END IF;
10100                                         MSC_ATP_DB_UTILS.INSERT_SUMMARY_SD_ROW(p_plan_id,
10101                                                                 p_atp_record.instance_id,
10102                                                                 p_atp_record.organization_id,
10103                                                                 l_inventory_item_id,
10104                                                                 l_atp_date_this_level,
10105                                                                 -1 * p_atp_record.quantity_ordered,
10106                                                                 '@@@');
10107                                     END IF;
10108                                     commit;
10109                                 END IF;
10110 			--END IF; -- end changes cto override
10111 
10112                         --- Enhance CTO Phase 1 Req #17
10113                         IF NOT (MSC_ATP_PVT.G_INV_CTP = 4 and MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y' AND
10114                            (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1) AND
10115                            (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1)) THEN
10116 
10117               		   l_pegging_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
10118               		   l_pegging_rec.order_line_id:= MSC_ATP_PVT.G_ORDER_LINE_ID;
10119               		   l_pegging_rec.parent_pegging_id:= l_pegging_id;
10120               		   l_pegging_rec.atp_level:= p_level+ 1;
10121               		   l_pegging_rec.organization_id:=
10122                                p_atp_record.organization_id;
10123                            l_pegging_rec.organization_code := l_org_code;
10124               		   l_pegging_rec.identifier1:= p_atp_record.instance_id;
10125                            l_pegging_rec.identifier2 := p_plan_id;
10126               		   l_pegging_rec.identifier3 := NULL;
10127 
10128                            -- time_phased_atp changes begin
10129                            IF l_time_phased_atp = 'Y' and l_atp_date_this_level <= p_atp_record.atf_date THEN
10130                                    l_pegging_rec.inventory_item_id:= l_request_item_id;
10131                                    l_pegging_rec.inventory_item_name := C_ITEM_INFO_REC.item_name;
10132                            ELSE
10133                                    l_pegging_rec.inventory_item_id:= p_atp_record.inventory_item_id;
10134                                    l_pegging_rec.inventory_item_name := PF_ITEM_INFO_REC.item_name;
10135                            END IF;
10136                            l_pegging_rec.aggregate_time_fence_date := p_atp_record.atf_date;
10137                            -- time_phased_atp changes end
10138 
10139               		   l_pegging_rec.resource_id := NULL;
10140                            l_pegging_rec.resource_code := NULL;
10141               		   l_pegging_rec.department_id := NULL;
10142                            l_pegging_rec.department_code := NULL;
10143               		   l_pegging_rec.supplier_id := NULL;
10144                            l_pegging_rec.supplier_name := NULL;
10145               		   l_pegging_rec.supplier_site_id := NULL;
10146                            l_pegging_rec.supplier_site_name := NULL;
10147               		   l_pegging_rec.scenario_id:= p_scenario_id;
10148               		   l_pegging_rec.supply_demand_source_type:= ATP;
10149                            -- bug 1304991: make sure  we following the same
10150                            -- way to populate pegging tree. that is,
10151                            -- future case's net supply should not consider the
10152                            -- request date quantity.
10153                            IF p_atp_record.available_quantity <> INFINITE_NUMBER THEN
10154                               --- IF req_date < PTF date and if enough quanity is available on req date then
10155                               --- we show peggin for two days. On req date we show wahtever quantity is
10156                               --- available on that day. On PTF date , we show req-date qty + whatever qty is available
10157                               --- between req date and ptf date. If req_date > ptf then we show atp date qty - req date qty
10158                               --- if req date < PTF date then we set l_req_date_qty = 0 else it is null
10159 
10160                               IF nvl(l_req_date_qty, -1) = 0 THEN
10161                                  l_pegging_rec.supply_demand_quantity:= p_atp_record.available_quantity;
10162                               ELSE
10163                                  l_pegging_rec.supply_demand_quantity:=
10164                                     p_atp_record.available_quantity - greatest(0,
10165                                     p_atp_record.requested_date_quantity +
10166 			 	   NVL(l_stealing_requested_date_qty, 0));
10167                               END IF;
10168                            ELSE
10169                               l_pegging_rec.supply_demand_quantity:=INFINITE_NUMBER;
10170                            END IF;
10171                            ---bug 2798667: ship_date = l_atp_date_this_level + atp_lead_time
10172                            ---The option item should be requested on l_atp_date_this_level instead of ship date
10173                            --l_pegging_rec.supply_demand_date:=p_atp_record.ship_date;
10174                            l_pegging_rec.supply_demand_date:= l_atp_date_this_level;
10175                            l_pegging_rec.actual_supply_demand_date:= l_atp_date_this_level;
10176               		   l_pegging_rec.supply_demand_type:= 2;
10177                            IF PG_DEBUG in ('Y', 'C') THEN
10178                               msc_sch_wb.atp_debug('ATP_Check: ' || 'Qty := ' || l_pegging_rec.supply_demand_quantity);
10179                               msc_sch_wb.atp_debug('ATP_Check: ' || 'Date := ' || l_pegging_rec.supply_demand_date);
10180                            END IF;
10181               		   l_pegging_rec.source_type := 0;
10182 
10183                            -- for demo:1153192
10184                            l_pegging_rec.constraint_flag := 'N';
10185 			   l_pegging_rec.component_identifier :=
10186 	                            NVL(p_atp_record.component_identifier, MSC_ATP_PVT.G_COMP_LINE_ID);
10187 
10188                            --- bug 2152184: For PF based ATP inventory_item_id field contains id for PF item
10189                            --- cto looks at pegging tree to place their demands. Since CTO expects to find
10190                            --  id for the requested item, we add the following column. CTO will now read from this column
10191                            l_pegging_rec.request_item_id := p_atp_record.request_item_id;
10192 
10193                            --diag_atp
10194                            l_pegging_rec.pegging_type := MSC_ATP_PVT.ATP_SUPPLY; ---atp supply node
10195 
10196                            --s_cto_rearch
10197                            l_pegging_rec.dest_inv_item_id := l_dest_inv_item_id;
10198                            l_pegging_rec.model_sd_flag := 2;
10199                            --s_cto_rearch
10200                            IF NVL(MSC_ATP_PVT.G_DIAGNOSTIC_ATP, 2) = 2
10201                                AND l_requested_date_quantity < p_atp_record.quantity_ordered
10202                                AND G_FORWARD_ATP = 'N' THEN --optional_fw
10203                                -- Bug 3828469 - Add constraint only if it exists
10204                                IF PG_DEBUG in ('Y', 'C') THEN
10205                                   msc_sch_wb.atp_debug('Forward ATP, add pegging constriant');
10206                                END IF;
10207                                l_pegging_rec.constraint_type := 1;
10208                            END IF;
10209                            --e_cto_rearch
10210 
10211 			   -- dsting: get_forward_material_info has already added the pegging
10212 			   -- in this case
10213 			   IF NOT (MSC_ATP_PVT.G_INV_CTP = 4 and
10214 			      MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y' AND
10215 			      MSC_ATP_PVT.G_ALLOCATION_METHOD = 1 AND
10216 			      MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1 AND
10217 			      l_atp_comp_flag = 'N' AND
10218 			      --p_parent_pegging_id is null AND
10219 			      MSC_ATP_PVT.G_DIAGNOSTIC_ATP <> 1)
10220 			   THEN
10221 	              	      l_pegging_rec.summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;     -- for summary enhancement
10222                               l_pegging_rec.demand_class :=  p_atp_record.demand_class;
10223                               --4570421
10224                               l_pegging_rec.scaling_type                      := p_atp_record.scaling_type;
10225                               l_pegging_rec.scale_multiple                    := p_atp_record.scale_multiple;
10226                               l_pegging_rec.scale_rounding_variance           := p_atp_record.scale_rounding_variance;
10227                               l_pegging_rec.rounding_direction                := p_atp_record.rounding_direction;
10228                               l_pegging_rec.component_yield_factor            := p_atp_record.component_yield_factor; --4570421
10229                               l_pegging_rec.usage                             := p_atp_record.usage_qty; --4775920
10230                               l_pegging_rec.organization_type                 := p_atp_record.organization_type; --4775920
10231 
10232 	              	      MSC_ATP_DB_UTILS.Add_Pegging(l_pegging_rec, l_atp_pegging_id);
10233 			   END IF;
10234 
10235                            -- 1250141: we need to populate the details
10236                            -- 1300692: we only populate the details for
10237                            -- PDS case here.  Because the form use end_pegging_id
10238                            -- to retrive info and we will end up display
10239                            -- same info twice.
10240 
10241                            --Bug2471377 Begin changes
10242                            IF NVL(p_atp_record.insert_flag,0) <> 0 THEN
10243                               IF MSC_ATP_PVT.G_INV_CTP = 4 THEN
10244                                  IF (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
10245                                    (MSC_ATP_PVT.G_ALLOCATION_METHOD = 2) THEN
10246                                    -- Link the Supply Demand Details for
10247                                    --first Demand class to the pegging details
10248 
10249 				   IF l_atp_period_first.pegging_id.COUNT > 0 THEN
10250 				 	MSC_ATP_UTILS.copy_mrp_sd_recs(
10251 				 		l_atp_period_first.pegging_id(1),
10252 				 		l_atp_pegging_id);
10253 				   END IF;
10254 
10255                                    FOR i in 1..l_atp_period_first.Level.COUNT LOOP
10256                                       l_atp_period_first.Pegging_Id(i) :=
10257                                              l_atp_pegging_id;
10258                                       l_atp_period_first.End_Pegging_Id(i) :=
10259                                              MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
10260                                    END LOOP;
10261 
10262                                    MSC_ATP_PROC.Details_Output(l_atp_period_first,
10263                                                l_atp_supply_demand,
10264                                                x_atp_period,
10265                                                x_atp_supply_demand,
10266                                                l_return_status);
10267                                  ELSIF MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y' AND
10268 				       (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1) AND
10269 				       (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
10270 				       p_parent_pegging_id IS NULL THEN
10271 				     null;
10272                                  ELSE
10273 
10274                                  FOR i in 1..l_atp_period.Level.COUNT LOOP
10275                                    l_atp_period.Pegging_Id(i) := l_atp_pegging_id;
10276                                    l_atp_period.End_Pegging_Id(i) :=
10277                                      MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
10278                                  END LOOP;
10279 
10280 			         IF p_atp_record.insert_flag <> 0 THEN
10281    		                         MSC_ATP_DB_UTILS.move_SD_temp_into_mrp_details(l_atp_pegging_id,
10282 				    		   MSC_ATP_PVT.G_DEMAND_PEGGING_ID);
10283 			         END IF;
10284 
10285                                  MSC_ATP_PROC.Details_Output(l_atp_period,
10286                                             l_atp_supply_demand,
10287                                             x_atp_period,
10288                                             x_atp_supply_demand,
10289                                             l_return_status);
10290                                  END IF; -- MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y
10291                               END IF; --MSC_ATP_PVT.G_INV_CTP = 4
10292                            END IF;
10293                          --- End Enhance CTO Phase 1 Req #17
10294                         END IF;  --Bug2471377 End Changes
10295 
10296                     END IF; -- 1421413 IF (p_parent_pegging_id is null) OR
10297                             -- ((p_parent_pegging_id is not null and
10298                             -- p_search = FORWARD_SCHEDULING))
10299 
10300                 ELSE
10301                     p_atp_record.error_code := ATP_REQ_QTY_FAIL;
10302                 END IF;
10303             --diag_atp
10304             ELSIF NOT(l_atp_comp_flag = 'N') THEN -- if net_demand > 0 and atp_comp_flag = 'Y'
10305 
10306               --- bug 2178544: Implement PTF
10307               -- PTF changes
10308               -- If the requested ship date is before planning time fence (PTF) date then
10309               -- we fail the request. The logic is that production schedule is fixed before
10310               -- PTF. Hence no planned orders should be placed before PTF
10311 
10312 	      -- 3/4/2002, modified to honor planning_time_fence_flag from msc_plans to adjust for
10313 	      -- ptf only if plan option is checked.
10314 
10315               -- Bug 3535999 - l_ptf_date should be validated on mgf cal
10316               -- l_ptf_date := l_sysdate;
10317               l_ptf_date := NVL(MSC_CALENDAR.next_work_day(
10318                                         p_atp_record.manufacturing_cal_code,
10319                                         p_atp_record.instance_id,
10320                                         l_trunc_sysdate), l_trunc_sysdate);
10321 
10322               -- Bug 3525582 - PTF constraint should be added only if plan is PTF enabled
10323               l_ptf_enabled := 2;
10324 
10325               -- when we are checking base model's capacity then we do not want to check PTF
10326               IF l_check_model_capacity = 2 THEN
10327                  BEGIN
10328                    Select DECODE(pl.planning_time_fence_flag,
10329                                         1, trunc(NVL(itm.planning_time_fence_date, l_ptf_date)), -- Bug 3535999 - l_sysdate)),
10330                                         l_ptf_date), -- Bug 3535999 - l_sysdate),
10331                           pl.planning_time_fence_flag   -- Bug 3525582
10332                    into   l_ptf_date, l_ptf_enabled     -- Bug 3525582
10333                    from   msc_system_items itm,
10334 		          msc_plans pl
10335                    where  itm.plan_id = p_plan_id
10336                    and    itm.sr_instance_id = p_atp_record.instance_id
10337                    and    itm.organization_id = p_atp_record.organization_id
10338                    --and    itm.sr_inventory_item_id = p_atp_record.inventory_item_id
10339                    and    itm.sr_inventory_item_id = nvl(p_atp_record.request_item_id,p_atp_record.inventory_item_id) --Bug-5160663
10340 		   and    pl.plan_id = itm.plan_id
10341 		   and    pl.sr_instance_id = itm.sr_instance_id;
10342                  EXCEPTION
10343                    WHEN OTHERS THEN
10344                        IF PG_DEBUG in ('Y', 'C') THEN
10345                           msc_sch_wb.atp_debug('ATP_Check: ' || 'Error occured while getting PTF : ' || sqlerrm);
10346                        END IF;
10347                        l_ptf_date := l_ptf_date; -- Bug 3535999 - l_sysdate;
10348                  END;
10349               END IF;
10350               IF PG_DEBUG in ('Y', 'C') THEN
10351                  msc_sch_wb.atp_debug('ATP_Check: ' || 'l_ptf_date := ' || l_ptf_date);
10352                  msc_sch_wb.atp_debug('ATP_Check: ' || 'l_ptf_enabled := ' || l_ptf_enabled);
10353               END IF;
10354               -- for bug 1124538: do not pass down the demand to the
10355               -- next level if it is backward scheduling and
10356               -- requested_ship_date is less than sysdate.
10357 
10358               IF ((p_search = BACKWARD_SCHEDULING) AND
10359                  --bug 2235012
10360                  --(l_requested_ship_date < GREATEST(l_sysdate, l_ptf_date))) THEN
10361                  --diag_atp: we dont want to stop in case of diag ATP. We still want to explode sources
10362                  (l_requested_ship_date < l_sysdate ) AND (MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 2)) THEN
10363                    IF PG_DEBUG in ('Y', 'C') THEN
10364                       msc_sch_wb.atp_debug('ATP_Check: ' || 'backward, request ship date<sysdate or ptf_date');
10365                    END IF;
10366                    p_atp_record.requested_date_quantity:=0.0;
10367                    -- cchen : 1238941
10368                    p_atp_record.combined_requested_date_qty := 0.0;
10369 
10370                    IF p_parent_pegging_id is null THEN
10371                        IF PG_DEBUG in ('Y', 'C') THEN
10372                           msc_sch_wb.atp_debug('ATP_Check: ' || 'top level, set future date Y');
10373                        END IF;
10374                        MSC_ATP_PVT.G_FIND_FUTURE_DATE := 'Y';
10375                        MSC_ATP_PVT.G_ASSEMBLY_LINE_ID := p_atp_record.Identifier;
10376                        MSC_ATP_PVT.G_COMP_LINE_ID := p_atp_record.Identifier;
10377                        -- 1275751
10378                        p_atp_record.error_code := ATP_REQ_DATE_FAIL;
10379                        ---- bug 1819638
10380                        ---remove the demand record as supply is 0 on the req_date
10381 
10382                        UPDATE msc_demands
10383                        SET  USING_REQUIREMENT_QUANTITY = 0
10384                        WHERE  demand_id = l_demand_id
10385                        AND    plan_id = p_plan_id
10386                        returning inventory_item_id into l_inventory_item_id;
10387 
10388                        -- time_phased_ATP
10389                        IF (l_time_phased_atp = 'Y')
10390                           OR
10391                           MSC_ATP_PVT.G_INV_CTP = 4 and MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y'
10392                            AND MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1 AND MSC_ATP_PVT.G_ALLOCATION_METHOD = 1 THEN
10393 
10394                            update msc_alloc_demands
10395                            set ALLOCATED_QUANTITY = 0
10396                            where parent_demand_id = l_demand_id
10397                            and   plan_id = p_plan_id;
10398                        END IF;
10399 
10400                         -- update summary records removed in ODS cases - for summary enhancement
10401                         IF MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y' AND p_plan_id = -1 THEN
10402                            IF PG_DEBUG in ('Y', 'C') THEN
10403                               msc_sch_wb.atp_debug('ATP_Check: ' || 'in sumary mode, update msc-demands');
10404                               msc_sch_wb.atp_debug('ATP_Check: ' || 'l_inventory_item_id := ' || l_inventory_item_id);
10405                               msc_sch_wb.atp_debug('ATP_Check: ' || 'l_requested_ship_date := ' || l_requested_ship_date);
10406                            END IF;
10407 
10408                            MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(p_atp_record.instance_id,
10409                                                                 p_plan_id,
10410                                                                 p_atp_record.organization_id,
10411                                                                 l_inventory_item_id,
10412                                                                 l_requested_ship_date,
10413                                                                 null,
10414                                                                 null,
10415                                                                 null,
10416                                                                 null,
10417                                                                 null,
10418                                                                 null,
10419                                                                 2);
10420 
10421 
10422                            update /*+ INDEX(msc_atp_summary_sd MSC_ATP_SUMMARY_SD_U1) */ msc_atp_summary_sd
10423                            set sd_qty = sd_qty + p_atp_record.quantity_ordered
10424                            where plan_id = p_plan_id
10425                            and  inventory_item_id = l_inventory_item_id
10426                            and  sr_instance_id = p_atp_record.instance_id
10427                            and  organization_id = p_atp_record.organization_id
10428                            and  sd_date = trunc(l_requested_ship_date);
10429                            commit;
10430                        END IF;
10431 
10432                        IF PG_DEBUG in ('Y', 'C') THEN
10433                           msc_sch_wb.atp_debug('ATP_Check: ' || 'demand updated for demand id = '|| l_demand_id);
10434                        END IF;
10435 
10436                    END IF;
10437               ELSE
10438 
10439                 -- pass the net demand down for next level
10440                 -- we try to meet the requirement by going down to the
10441                 -- components.
10442                 -- first we check the supply chain bill
10443 
10444                 IF PG_DEBUG in ('Y', 'C') THEN
10445                    msc_sch_wb.atp_debug('ATP_Check: ' || 'inside net_demand > 0');
10446                 END IF;
10447                 -- for bug 1094773
10448                 IF (p_search = FORWARD_SCHEDULING) AND
10449                    --subst : for top org in case of subst we do not add pegg
10450                    NOT (MSC_ATP_PVT.G_SUBSTITUTION_FLAG = 'Y' and
10451                               p_atp_record.top_tier_org_flag = 1) THEN
10452 
10453                    -- assume the atp date and quantity to be single level.
10454                    -- this is the worst that we can get.
10455 
10456                    IF PG_DEBUG in ('Y', 'C') THEN
10457                       msc_sch_wb.atp_debug('ATP_Check: ' || 'Forward ATP Qty := ' || l_atp_date_quantity_this_level);
10458                       msc_sch_wb.atp_debug('ATP_Check: ' || 'ATP Date : FUTURE : ' || l_atp_date_this_level);
10459                    END IF;
10460 
10461                    p_atp_record.available_quantity:= l_atp_date_quantity_this_level;
10462                    p_atp_record.used_available_quantity:= l_used_available_quantity;--bug3409973
10463 
10464                    -- 2859130 move to next work day for forward pass
10465                    /* do not honor atp lead time
10466                    IF NVL(p_atp_record.atp_lead_time, 0) > 0 THEN
10467                       p_atp_record.ship_date := MSC_CALENDAR.DATE_OFFSET
10468                                         (p_atp_record.organization_id,
10469                                          p_atp_record.instance_id,
10470                                          1,
10471                                          l_atp_date_this_level,
10472                                          NVL(p_atp_record.atp_lead_time, 0));
10473                    ELSE
10474                       p_atp_record.ship_date := MSC_CALENDAR.NEXT_WORK_DAY
10475                                         (p_atp_record.organization_id,
10476                                          p_atp_record.instance_id,
10477                                          1,
10478                                          l_atp_date_this_level);
10479                    END IF;
10480                    */
10481                    p_atp_record.ship_date := MSC_CALENDAR.next_work_day(
10482                                                 p_atp_record.shipping_cal_code,
10483                                                 p_atp_record.instance_id,
10484                                                 l_atp_date_this_level);
10485 
10486                    IF PG_DEBUG in ('Y', 'C') THEN
10487                       msc_sch_wb.atp_debug('ATP_Check: ' || 'p_atp_record.ship_date : ' || p_atp_record.ship_date);
10488                    END IF;
10489 
10490 		   -- Planned Pegging Based AATP Changes
10491                    --enable forward pegging for all cases.
10492                    /* IF (
10493                        --- Enhance CTO Phase 1 Req #17
10494                           -- Support Forward Stealing for components of ATO model in
10495                           -- Model's sourcing organization.
10496                        ( (p_parent_pegging_id IS NULL) OR
10497                            NVL(C_ITEM_INFO_REC.cto_source_org_id, -1) = p_atp_record.organization_id ) AND
10498                        --- End Enhance CTO Phase 1 Req #17
10499                    */
10500                    IF (
10501                       (MSC_ATP_PVT.G_INV_CTP = 4) AND
10502                       (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
10503                       (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
10504                       (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1)) AND C_ITEM_INFO_REC.atp_flag = 'Y'
10505                         --optional_fw disabling it as no forward stealing
10506                         AND MSC_ATP_PVT.G_FORWARD_ATP = 'N'
10507                                 --3423277
10508                                 AND NOT(C_ITEM_INFO_REC.bom_item_type = 1 and
10509                                     ( C_ITEM_INFO_REC.atp_comp_flag in ('Y', 'C') and
10510                                       p_atp_record.parent_item_id is null)) THEN
10511 
10512 		      -- Already Called MSC_AATP_REQ.Get_Forward_Material_Atp, thus don't add
10513 		      -- pegging for future case here as its already added.
10514 
10515 		      IF PG_DEBUG in ('Y', 'C') THEN
10516 		         msc_sch_wb.atp_debug('ATP_Check: ' || 'Already added future supply pegging : '|| l_atp_pegging_tab.COUNT);
10517 		      END IF;
10518 		   ELSE
10519                       l_future_atp_period := l_atp_period;
10520                       l_future_atp_supply_demand := l_atp_supply_demand;
10521 
10522                       l_pegging_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
10523                       l_pegging_rec.order_line_id:= MSC_ATP_PVT.G_ORDER_LINE_ID;
10524                       l_pegging_rec.parent_pegging_id:= l_pegging_id;
10525                       l_pegging_rec.atp_level:= p_level+1;
10526                       l_pegging_rec.organization_id:= p_atp_record.organization_id;
10527                       l_pegging_rec.organization_code := l_org_code;
10528                       l_pegging_rec.identifier1:= p_atp_record.instance_id;
10529                       l_pegging_rec.identifier2 := p_plan_id;
10530                       l_pegging_rec.identifier3 := NULL;
10531 
10532                       -- time_phased_atp changes begin
10533                       IF l_time_phased_atp = 'Y' and l_atp_date_this_level <= p_atp_record.atf_date THEN
10534                           l_pegging_rec.inventory_item_id:= l_request_item_id;
10535                           l_pegging_rec.inventory_item_name := C_ITEM_INFO_REC.item_name;
10536                       ELSE
10537                           l_pegging_rec.inventory_item_id:= p_atp_record.inventory_item_id;
10538                           l_pegging_rec.inventory_item_name := PF_ITEM_INFO_REC.item_name;
10539                       END IF;
10540                       l_pegging_rec.aggregate_time_fence_date := p_atp_record.atf_date;
10541                       -- time_phased_atp changes end
10542 
10543                       l_pegging_rec.resource_id := NULL;
10544                       l_pegging_rec.resource_code := NULL;
10545                       l_pegging_rec.department_id := NULL;
10546                       l_pegging_rec.department_code := NULL;
10547                       l_pegging_rec.supplier_id := NULL;
10548                       l_pegging_rec.supplier_name := NULL;
10549                       l_pegging_rec.supplier_site_id := NULL;
10550                       l_pegging_rec.supplier_site_name := NULL;
10551                       l_pegging_rec.scenario_id:= p_scenario_id;
10552                       l_pegging_rec.supply_demand_source_type:= ATP;
10553 
10554                       -- bug 1304991: make sure we follow the same way to populate pegging tree.
10555 		      -- that is, future case's net supply should not consider the request date quantity.
10556                       --- IF req_date < PTF date and if enough quanity is available on req date then
10557                       --- we show peggin for two days. On req date we show wahtever quantity is
10558                       --- available on that day. On PTF date , we show req-date qty + whatever qty is available
10559                       --- between req date and ptf date. If req_date > ptf then we show atp date qty - req date qty
10560                       --- if req date < PTF date then we set l_req_date_qty = 0 else it is null
10561                       --4410439 start
10562                       IF nvl(l_req_date_qty, -1) = 0 THEN
10563                          l_pegging_rec.supply_demand_quantity:= p_atp_record.available_quantity;
10564                       ELSE
10565                          IF PG_DEBUG in ('Y', 'C') THEN
10566 		            msc_sch_wb.atp_debug('ATP_Check: p_atp_record_available_qty : ' || p_atp_record.available_quantity);
10567                             msc_sch_wb.atp_debug('ATP_Check: requested_day_quantity : ' || p_atp_record.available_quantity);
10568 		         END IF;
10569                          IF(p_atp_record.available_quantity <>  INFINITE_NUMBER) THEN
10570                             IF PG_DEBUG in ('Y', 'C') THEN
10571 		               msc_sch_wb.atp_debug('ATP_Check: Available Quantity is not INFINITE, subtracting requested_day_qty');
10572                             END IF;
10573                          l_pegging_rec.supply_demand_quantity:=
10574                                 p_atp_record.available_quantity - greatest(0, l_requested_date_quantity);
10575                          ELSE
10576                             IF PG_DEBUG in ('Y', 'C') THEN
10577 		               msc_sch_wb.atp_debug('ATP_Check: Available Quantity INFINITE');
10578                             END IF;
10579                              l_pegging_rec.supply_demand_quantity:=  p_atp_record.available_quantity;
10580                          END IF;
10581                       END IF;
10582                       --4410439 end
10583                       ---bug 2798667: ship_date = l_atp_date_this_level + atp_lead_time
10584                       ---The option item should be requested on l_atp_date_this_level instead of ship date
10585                       --l_pegging_rec.supply_demand_date:=p_atp_record.ship_date;
10586                       l_pegging_rec.supply_demand_date:=l_atp_date_this_level;
10587                       --bug 3328421: store actual supply demand date
10588                       l_pegging_rec.actual_supply_demand_date := l_atp_date_this_level;
10589 
10590                       l_pegging_rec.supply_demand_type:= 2;
10591                       l_pegging_rec.source_type := 0;
10592 
10593                       -- for demo:1153192
10594                       l_pegging_rec.constraint_flag := 'N';
10595 		      l_pegging_rec.component_identifier :=
10596                              NVL(p_atp_record.component_identifier, MSC_ATP_PVT.G_COMP_LINE_ID);
10597 
10598                       l_pegging_rec.summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;
10599 
10600                       --- bug 2152184: For PF based ATP inventory_item_id field contains id for PF item
10601                       --- cto looks at pegging tree to place their demands. Since CTO expects to find
10602                       --  id for the requested item, we add the following column. CTO will now read from this column
10603                       --diag_atp
10604                       l_pegging_rec.plan_name := p_atp_record.plan_name;
10605                       l_pegging_rec.required_quantity:= p_atp_record.quantity_ordered;
10606                       l_pegging_rec.required_date := l_requested_ship_date;
10607                       l_pegging_rec.infinite_time_fence := l_get_mat_out_rec.infinite_time_fence_date;
10608                       l_pegging_rec.atp_rule_name := l_get_mat_out_rec.atp_rule_name;
10609                       l_pegging_rec.rounding_control := l_rounding_control_flag;
10610                       l_pegging_rec.atp_flag := l_atp_flag;
10611                       l_pegging_rec.atp_component_flag := l_atp_comp_flag;
10612                       l_pegging_rec.pegging_type := MSC_ATP_PVT.ATP_SUPPLY; ---atp supply node
10613                       l_pegging_rec.postprocessing_lead_time := l_post_pro_lt;
10614                       l_pegging_rec.preprocessing_lead_time := l_pre_pro_lt;
10615                       l_pegging_rec.fixed_lead_time := l_fixed_lt;
10616                       l_pegging_rec.variable_lead_time := l_variable_lt;
10617                       l_pegging_rec.weight_capacity := l_unit_weight;
10618                       l_pegging_rec.volume_capacity := l_unit_volume;
10619                       l_pegging_rec.weight_uom := l_weight_uom;
10620                       l_pegging_rec.volume_uom := l_volume_uom;
10621                       l_pegging_rec.allocation_rule := l_allocation_rule_name;
10622 
10623 
10624                       l_pegging_rec.request_item_id := p_atp_record.request_item_id;
10625 
10626                       --s_cto_rearch
10627                       IF NVL(MSC_ATP_PVT.G_DIAGNOSTIC_ATP, 2) = 2 THEN
10628                         IF PG_DEBUG in ('Y', 'C') THEN
10629                            msc_sch_wb.atp_debug('Forward ATP, add pegging constriant');
10630                         END IF;
10631                         l_pegging_rec.constraint_type := 1;
10632                       END IF;
10633                       l_pegging_rec.dest_inv_item_id := C_ITEM_INFO_REC.dest_inv_item_id;
10634                       l_pegging_rec.model_sd_flag := 2;
10635                       --e_cto_rearch
10636                       l_pegging_rec.demand_class :=  p_atp_record.demand_class;
10637 
10638                       --4570421
10639                       l_pegging_rec.scaling_type                      := p_atp_record.scaling_type;
10640                       l_pegging_rec.scale_multiple                    := p_atp_record.scale_multiple;
10641                       l_pegging_rec.scale_rounding_variance           := p_atp_record.scale_rounding_variance;
10642                       l_pegging_rec.rounding_direction                := p_atp_record.rounding_direction;
10643                       l_pegging_rec.component_yield_factor            := p_atp_record.component_yield_factor; --4570421
10644                       l_pegging_rec.usage                             := p_atp_record.usage_qty; --4775920
10645                       l_pegging_rec.organization_type                 := p_atp_record.organization_type; --4775920
10646 
10647                       MSC_ATP_DB_UTILS.Add_Pegging(l_pegging_rec, l_future_pegging_id);
10648 
10649                       -- bug 1225478
10650                       FOR i in 1..l_future_atp_period.Level.COUNT LOOP
10651                           l_future_atp_period.Pegging_Id(i) := l_future_pegging_id;
10652                           l_future_atp_period.End_Pegging_Id(i) := MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
10653                       END LOOP;
10654 
10655                  -- dsting: supply/demand details pl/sql table no longer used
10656 /*
10657                       FOR i in 1..l_future_atp_supply_demand.Level.COUNT LOOP
10658                           l_future_atp_supply_demand.Pegging_Id(i) := l_future_pegging_id;
10659                           l_future_atp_supply_demand.End_Pegging_Id(i) := MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
10660                       END LOOP;
10661 */
10662 		      IF p_atp_record.insert_flag <> 0 THEN
10663 		      	MSC_ATP_DB_UTILS.move_SD_temp_into_mrp_details(l_future_pegging_id,
10664 					MSC_ATP_PVT.G_DEMAND_PEGGING_ID);
10665 		      END IF;
10666 
10667                    END IF; 	--IF (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1) THEN
10668                 END IF;
10669                 -- store this pegging id to l_parent_pegging_id so
10670                 -- that we know how to peg to the demand for the planned
10671                 -- order that we are going to create for each child.
10672                 --4902658/5216528
10673                 IF PG_DEBUG in ('Y', 'C') THEN
10674                     msc_sch_wb.atp_debug('l_parent_pegging_id := ' || l_parent_pegging_id);
10675                 END IF;
10676 
10677                 l_parent_pegging_id := l_pegging_id;
10678                 -- Bug 2814872
10679                 IF PG_DEBUG in ('Y', 'C') THEN
10680                     msc_sch_wb.atp_debug('l_parent_pegging_id := ' || l_parent_pegging_id);
10681                     msc_sch_wb.atp_debug('l_pegging_id := ' || l_pegging_id);
10682                     msc_sch_wb.atp_debug('ATP_Check: l_atp_date_this_level ' || l_atp_date_this_level);
10683                     msc_sch_wb.atp_debug('ATP_Check: l_atp_date_quantity_this_level ' ||
10684                                                                 l_atp_date_quantity_this_level);
10685                     msc_sch_wb.atp_debug('ATP_Check: l_used_available_quantity ' ||
10686                                                                 l_used_available_quantity);
10687                     msc_sch_wb.atp_debug('ATP_Check: p_atp_record.Quantity_Ordered '||
10688                                                                 p_atp_record.Quantity_Ordered);
10689                     msc_sch_wb.atp_debug('ATP_Check: l_temp_net_demand '|| l_net_demand );
10690                     msc_sch_wb.atp_debug('ATP_Check: Count of Subs Items '|| l_substitutes_rec.inventory_item_id.count );
10691                END IF;
10692                l_tmp_parent_pegging_id := l_parent_pegging_id;
10693                --4902658/5216528
10694                for l_index in 1..l_substitutes_rec.inventory_item_id.count
10695                   LOOP
10696 
10697                   IF l_substitutes_rec.inventory_item_id.count > 1 THEN
10698                      IF PG_DEBUG in ('Y', 'C') THEN
10699                         msc_sch_wb.atp_debug('Substitute Loop Starts with l_index := ' || l_index);
10700                END IF;
10701                END IF;
10702 
10703                   IF (l_net_demand <= 0) then
10704                      EXIT;
10705                   END IF;
10706 
10707                   l_temp_net_demand := l_net_demand;
10708                   l_parent_pegging_id :=  l_substitutes_rec.pegging_id(l_index);
10709                   l_request_item_id :=  l_substitutes_rec.inventory_item_id(l_index);
10710                   MSC_ATP_PROC.get_global_item_info(p_atp_record.instance_id,
10711                                             --3917625: read item attributes from planned data
10712                                             -- -1,
10713                                             p_plan_id,
10714                                             l_request_item_id,
10715                                             p_atp_record.organization_id,
10716                                             l_item_info_rec );
10717                   C_ITEM_INFO_REC := G_ITEM_INFO_REC;
10718                   --In regular cases PF rec should point to PF Item .
10719                   if l_substitutes_rec.inventory_item_id.count > 1 then
10720                      PF_ITEM_INFO_REC := G_ITEM_INFO_REC;
10721                   end if;
10722                   p_atp_record.request_item_id :=     l_substitutes_rec.inventory_item_id(l_index);
10723 
10724                -- Do not do Any Forward processing of sources for past due orders .
10725                IF ((p_search = FORWARD_SCHEDULING) AND
10726                    (l_atp_date_quantity_this_level >= p_atp_record.Quantity_Ordered) AND
10727                    (l_atp_date_this_level = l_sysdate ) AND (MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 2)) THEN
10728                    --diag_atp: we dont want to stop in case of diag ATP. We still want to explode sources
10729                    IF PG_DEBUG in ('Y', 'C') THEN
10730                       msc_sch_wb.atp_debug('ATP_Check: ' ||
10731                           'Past Due Order Satisfied on Sysdate, Stop unless dia_atp ');
10732                    END IF;
10733                   p_atp_record.error_code := ATP_REQ_DATE_FAIL;
10734                 --END IF;
10735                 -- End Bug 2814872
10736 
10737                 ----bug 2235012
10738                 --diag_atp
10739                 ELSIF NOT (p_search = BACKWARD_SCHEDULING AND l_requested_ship_date < l_ptf_date
10740                                                        AND  MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 2) THEN
10741 
10742                    IF PG_DEBUG in ('Y', 'C') THEN
10743                        msc_sch_wb.atp_debug('ATP_Check: ' || 'Find Sources');
10744                        msc_sch_wb.atp_debug('bom_item_type := ' || C_ITEM_INFO_REC.bom_item_type);
10745                        msc_sch_wb.atp_debug('wip_supply_type := ' || p_atp_record.wip_supply_type);
10746                        msc_sch_wb.atp_debug('replenish_to_ord_flag := ' || C_ITEM_INFO_REC.replenish_to_ord_flag);
10747                    END IF;
10748                    --s_cto_rearch
10749                    l_item_sourcing_info_rec := l_null_item_sourcing_info_rec;
10750                    l_item_sourcing_info_rec.sr_inventory_item_id.extend;
10751                    l_item_sourcing_info_rec.line_id.extend;
10752                    l_item_sourcing_info_rec.ato_line_id.extend;
10753                    l_item_sourcing_info_rec.match_item_id.extend;
10754                    --l_item_sourcing_info_rec.sr_inventory_item_id(1) := p_atp_record.inventory_item_id;
10755                    l_item_sourcing_info_rec.sr_inventory_item_id(1) := l_request_item_id; -- for time_phased_atp
10756                    l_item_sourcing_info_rec.line_id(1) := p_atp_record.demand_source_line;
10757                    l_item_sourcing_info_rec.match_item_id(1) := null;
10758                    IF C_ITEM_INFO_REC.bom_item_type = 1 and
10759                           C_ITEM_INFO_REC.replenish_to_ord_flag = 'Y' THEN
10760                       l_item_sourcing_info_rec.ato_line_id(1) := p_atp_record.ATO_Model_Line_Id;
10761                    ELSE
10762                       l_item_sourcing_info_rec.ato_line_id(1) := null;
10763                    END IF;
10764 
10765                    --if ods ATP and ATO model/item or if checking base model's capacity  then we only make
10766                    IF MSC_ATP_PVT.G_INV_CTP = 5 or l_check_model_capacity = 1 THEN
10767                       IF PG_DEBUG in ('Y', 'C') THEN
10768                           msc_sch_wb.atp_debug('ATP_Check: ' || 'ods atp, we always make');
10769                       END IF;
10770 
10771                       --we should come here only for ATO models or ATO items
10772                       l_sources.Source_Type := MRP_ATP_PUB.number_arr(2); -- make
10773                       l_sources.Organization_Id:=
10774                            MRP_ATP_PUB.number_arr(p_atp_record.organization_id);
10775                       l_sources.Rank := MRP_ATP_PUB.number_arr(1);
10776                       l_sources.Supplier_Id := MRP_ATP_PUB.number_arr(NULL);
10777                       l_sources.Supplier_Site_Id := MRP_ATP_PUB.number_arr(NULL);
10778                       l_sources.instance_id :=
10779                             MRP_ATP_PUB.number_arr(p_atp_record.instance_id);
10780                       l_sources.Lead_Time := MRP_ATP_PUB.number_arr(0); -- ship_rec_cal
10781                            l_sources.Ship_Method := MRP_ATP_PUB.char30_arr('@@@'); -- ship_rec_cal
10782                            l_sources.Preferred := MRP_ATP_PUB.number_arr(NULL); -- ship_rec_cal
10783                            l_sources.make_flag := MRP_ATP_PUB.char1_arr(NULL); -- ship_rec_cal
10784                            l_sources.Sup_Cap_Type := MRP_ATP_PUB.number_arr(2); -- ship_rec_cal
10785 
10786                    --do not get sources for phantom model
10787                    ELSIF NOT (C_ITEM_INFO_REC.bom_item_type = 1 and NVL(p_atp_record.wip_supply_type,2) = 6 and
10788                               C_ITEM_INFO_REC.replenish_to_ord_flag = 'Y') THEN
10789 
10790 
10791                       MSC_ATP_PROC.Atp_Sources(p_atp_record.instance_id,
10792 			         p_plan_id,
10793                                  l_request_item_id, -- for time_phased_atp
10794                                  p_atp_record.organization_id,
10795                                  NULL,
10796                                  NULL,
10797                                  p_assign_set_id,
10798                                  l_item_sourcing_info_rec,
10799                                  --MRP_ATP_PUB.number_arr(NULL),
10800                                  MSC_ATP_PVT.G_SESSION_ID,
10801                                  l_sources,
10802 			         l_return_status);
10803 
10804                    END IF;
10805                    --e_cto_rearch
10806                    IF PG_DEBUG in ('Y', 'C') THEN
10807                       msc_sch_wb.atp_debug('ATP_Check: ' || 'after atp_sources');
10808                    END IF;
10809 
10810                    IF l_return_status = MSC_ATP_PVT.CTO_OSS_ERROR Then
10811                        --- Model has option specific sources which do not match with sourcing of model
10812                        --update the demand pegging
10813                        update mrp_atp_details_temp
10814                        set error_code = MSC_ATP_PVT.OSS_ERROR_AT_THIS_LEVEL
10815                        where session_id = MSC_ATP_PVT.G_SESSION_ID
10816                        and   pegging_id = l_pegging_id
10817                        and   record_type = 3;
10818 
10819                        RAISE MSC_ATP_PVT.INVALID_OSS_SOURCE;
10820 
10821                    ELSIF l_return_status <> FND_API.G_RET_STS_SUCCESS  OR
10822                      l_sources.source_type.count = 0  THEN
10823                        -- no sources in supply chain bill, then we assume
10824                        -- it has "make at" type
10825                        IF PG_DEBUG in ('Y', 'C') THEN
10826                           msc_sch_wb.atp_debug('ATP_Check: ' || 'no sources');
10827                        END IF;
10828                        --- select make buy code  to decide if we want to make it or buy it
10829                        --EFTC Changes
10830                        BEGIN
10831                		   SELECT planning_make_buy_code INTO  l_make_buy_cd
10832                		   FROM MSC_SYSTEM_ITEMS I
10833                		   --bug3842035 Using member item id instead of family
10834                		   --WHERE  I.sr_inventory_item_id = p_atp_record.inventory_item_id and
10835                		   WHERE  I.sr_inventory_item_id = p_atp_record.request_item_id and
10836                       	   I.sr_instance_id = p_atp_record.instance_id and
10837                        	   I.organization_id = p_atp_record.organization_id and
10838                        	   I.plan_id = p_plan_id ;
10839                            IF PG_DEBUG in ('Y', 'C') THEN
10840                               msc_sch_wb.atp_debug('ATP_Check: ' || 'make-buy Code = '|| l_make_buy_cd);
10841                            END IF;
10842                        EXCEPTION
10843                		   WHEN OTHERS THEN
10844 			   l_make_buy_cd := 1; --- make 'make' default
10845                        END;
10846                        IF (l_make_buy_cd = 2) THEN ---  buy
10847                            l_sources.Source_Type := MRP_ATP_PUB.number_arr(3); --- BUY
10848                            l_sources.Organization_Id:=
10849                                    MRP_ATP_PUB.number_arr(p_atp_record.organization_id);
10850                            l_sources.Rank := MRP_ATP_PUB.number_arr(1);
10851                            ----- Since supplier id  and supplier site id is no known
10852                            ----  we set it to -99. We cant set it to null because
10853                            ----  when atp_check procedure is called from buy logic below
10854                            ----  then it will go into inventory org to do atp check rather
10855                            ----- then calling get_supplier_atp_info
10856                            l_sources.Supplier_Id :=MRP_ATP_PUB.number_arr(-99);
10857                            l_sources.Supplier_Site_Id :=MRP_ATP_PUB.number_arr(-99);
10858                            l_sources.instance_id :=
10859                              MRP_ATP_PUB.number_arr(p_atp_record.instance_id);
10860    			   l_sources.Lead_Time := MRP_ATP_PUB.number_arr(0); -- ship_rec_cal
10861                            l_sources.Ship_Method := MRP_ATP_PUB.char30_arr('@@@'); -- ship_rec_cal
10862                            l_sources.Preferred := MRP_ATP_PUB.number_arr(NULL); -- ship_rec_cal
10863                            l_sources.make_flag := MRP_ATP_PUB.char1_arr(NULL); -- ship_rec_cal
10864                            l_sources.Sup_Cap_Type := MRP_ATP_PUB.number_arr(2); -- ship_rec_cal
10865 
10866 			  -- dsting
10867 			  IF PG_DEBUG in ('Y', 'C') THEN
10868 			     msc_sch_wb.atp_debug('ATP_Check: ' || 'MAKE_BUY_CHILDREN: have buy source');
10869 			  END IF;
10870 			  p_atp_record.children_type := MAKE_BUY_CHILDREN;
10871 		       ELSE
10872                            l_sources.Source_Type := MRP_ATP_PUB.number_arr(2); -- make
10873                            l_sources.Organization_Id:=
10874                               MRP_ATP_PUB.number_arr(p_atp_record.organization_id);
10875                            l_sources.Rank := MRP_ATP_PUB.number_arr(1);
10876                            l_sources.Supplier_Id := MRP_ATP_PUB.number_arr(NULL);
10877                            l_sources.Supplier_Site_Id := MRP_ATP_PUB.number_arr(NULL);
10878                            l_sources.instance_id :=
10879                               MRP_ATP_PUB.number_arr(p_atp_record.instance_id);
10880                            l_sources.Lead_Time := MRP_ATP_PUB.number_arr(0); -- ship_rec_cal
10881                            l_sources.Ship_Method := MRP_ATP_PUB.char30_arr('@@@'); -- ship_rec_cal
10882                            l_sources.Preferred := MRP_ATP_PUB.number_arr(NULL); -- ship_rec_cal
10883                            l_sources.make_flag := MRP_ATP_PUB.char1_arr(NULL); -- ship_rec_cal
10884                            l_sources.Sup_Cap_Type := MRP_ATP_PUB.number_arr(2); -- ship_rec_cal
10885 
10886 			  -- dsting
10887 			  IF PG_DEBUG in ('Y', 'C') THEN
10888 			     msc_sch_wb.atp_debug('ATP_Check: ' || 'MAKE_BUY_CHILDREN: have make source');
10889 			  END IF;
10890 			  p_atp_record.children_type := MAKE_BUY_CHILDREN;
10891 		       END IF;
10892 
10893                    END IF;
10894                 END IF;
10895                 -- go over each sources
10896                 j := l_sources.source_type.FIRST;
10897 
10898 
10899                 --- we store parent_ptf in a local varibale as it might get changed
10900                 l_parent_ptf_date := NVL(MSC_ATP_PVT.G_PTF_DATE, l_ptf_date);
10901                 IF PG_DEBUG in ('Y', 'C') THEN
10902                    msc_sch_wb.atp_debug('ATP_Check: ' || 'l_parent_ptf_date := ' || l_parent_ptf_date);
10903                 END IF;
10904                 WHILE (j IS NOT NULL)   LOOP
10905 
10906                     -- Bug 3667729 - Reset G_ITEM_INFO_REC as it may have changed in last source.
10907                     G_ITEM_INFO_REC := C_ITEM_INFO_REC;
10908 
10909                     --Bug 3821358
10910                     l_store_g_ptf_date := NULL;
10911                     --optional_fw
10912                     --If we are going deep in the supply chain then we need not to set this as null as we want
10913                     --this to point to the top most org so that we can delete all the records by passing that pegging id.
10914                     --But if we are going in breath first manner then we need to keep an array of all the POs of all the
10915                     --sources. Let's keep every id in the loop no need of resetting n all that fuss.
10916                     IF G_FORWARD_ATP = 'Y' AND (l_sources.source_type.count > 1) THEN
10917                       G_FW_CTP_PEGGING_ID.EXTEND;
10918                       G_FW_CTP_PEGGING_ID(G_FW_CTP_PEGGING_ID.COUNT) := NULL;
10919 
10920                       IF PG_DEBUG in ('Y', 'C') THEN
10921                        msc_sch_wb.atp_debug('ATP_Check: ' || 'inside more than source');
10922                       END IF;
10923                     END IF;
10924 
10925                     --bug 2178544: Since the ptf flag may be chnaged by transfer org, we reassign it for the curent plan
10926                     MSC_ATP_PVT.G_PTF_DATE := l_parent_ptf_date;
10927 
10928                     IF PG_DEBUG in ('Y', 'C') THEN
10929                        msc_sch_wb.atp_debug('ATP_Check: ' || 'inside while loop for the children');
10930                     END IF;
10931 
10932                     /* ship_rec_cal changes begin */
10933                     -- populate calendar codes in l_atp_rec
10934                     IF PG_DEBUG in ('Y', 'C') THEN
10935                        msc_sch_wb.atp_debug('**************** Calendar Codes ******************************');
10936                        msc_sch_wb.atp_debug('*  ___________________Input_____________________');
10937                        msc_sch_wb.atp_debug('*  ');
10938                        msc_sch_wb.atp_debug('*  Source Type             : '|| l_sources.source_type(j) );
10939                        msc_sch_wb.atp_debug('*  Sup Cap Type            : '|| l_sources.sup_cap_type(j) );
10940                        msc_sch_wb.atp_debug('*  Instance ID             : '|| p_atp_record.instance_id );
10941                        msc_sch_wb.atp_debug('*  Source Instance ID      : '|| l_sources.instance_id(j) );
10942                        msc_sch_wb.atp_debug('*  Source Org ID           : '|| l_sources.organization_id(j) );
10943                        msc_sch_wb.atp_debug('*  Receiving Org ID        : '|| p_atp_record.organization_id );
10944                        msc_sch_wb.atp_debug('*  Plan ID                 : '|| p_plan_id );
10945                        msc_sch_wb.atp_debug('*  Inv Item ID             : '|| p_atp_record.inventory_item_id );
10946                        msc_sch_wb.atp_debug('*  Supplier ID             : '|| l_sources.supplier_id(j) );
10947                        msc_sch_wb.atp_debug('*  Supplier Site ID        : '|| l_sources.supplier_site_id(j) );
10948                        msc_sch_wb.atp_debug('*  Ship Method             : '|| l_sources.Ship_Method(j) );
10949                     END IF;
10950 
10951                     IF l_sources.source_type(j) = MAKE THEN
10952 
10953                         l_atp_rec.receiving_cal_code := p_atp_record.manufacturing_cal_code;
10954                         l_atp_rec.intransit_cal_code := p_atp_record.manufacturing_cal_code;
10955                         l_atp_rec.shipping_cal_code := p_atp_record.manufacturing_cal_code;
10956                         l_atp_rec.manufacturing_cal_code := p_atp_record.manufacturing_cal_code;
10957 
10958                     ELSIF l_sources.source_type(j) = TRANSFER THEN
10959 
10960                          --bug 3953635: calculate lead time if its not populated on sourcing rule
10961                         IF l_sources.Ship_Method(j) <> '@@@' THEN
10962                             l_atp_rec.ship_method := l_sources.Ship_Method(j);
10963                             l_atp_rec.delivery_lead_time := l_sources.lead_time(j);
10964                         ELSE
10965                             l_atp_rec.ship_method := null;
10966                             l_atp_rec.delivery_lead_time := null;
10967 
10968                             MSC_ATP_PROC.get_delivery_lead_time(
10969                                         l_sources.organization_id(j), -- p_from_org_id
10970                                         NULL,              -- p_from_loc_id
10971                                         l_sources.instance_id(j),
10972                                         p_atp_record.organization_id,
10973                                         NULL,              -- p_to_loc_id
10974                                         l_sources.instance_id(j),
10975                                         NULL,              -- p_customer_id
10976                                         NULL,              -- p_customer_site_id
10977                                         null,              -- Supplier_id
10978                                         null,              --suplier_site_id
10979                                         MSC_ATP_PVT.G_SESSION_ID,
10980                                         NULL,              -- p_partner_site_id
10981                                         l_atp_rec.ship_method,
10982                                         l_atp_rec.delivery_lead_time);
10983 
10984                             --reset the ship method and lead time because
10985                             --both l_sources.ship_method(j) and l_atp_rec.ship_method is used at
10986                             --different places.
10987                             l_sources.Ship_Method(j) := l_atp_rec.ship_method;
10988                             l_sources.lead_time(j) := l_atp_rec.delivery_lead_time;
10989 
10990                            IF PG_DEBUG in ('Y', 'C') THEN
10991                                msc_sch_wb.atp_debug('Lead time := ' || l_atp_rec.delivery_lead_time);
10992                                msc_sch_wb.atp_debug('Ship Method := ' || l_atp_rec.ship_method);
10993                            END IF;
10994                         END IF;
10995 
10996                         -- Bug 3737759 - Moved from below
10997                         l_atp_rec.manufacturing_cal_code := MSC_CALENDAR.Get_Calendar_Code(
10998                                                              l_sources.instance_id(j),
10999                                                              null,
11000                                                              null,
11001                                                              null,
11002                                                              null,
11003                                                              null,
11004                                                              l_sources.organization_id(j),
11005                                                              null,
11006                                                              MSC_CALENDAR.OMC);
11007 
11008                         -- Bug 3593394
11009                         IF MSC_ATP_PVT.G_USE_SHIP_REC_CAL='Y' THEN
11010                             -- receiving party is org
11011                             l_atp_rec.receiving_cal_code := MSC_CALENDAR.Get_Calendar_Code(
11012                                                              p_atp_record.instance_id,
11013                                                              null,
11014                                                              null,
11015                                                              null,
11016                                                              null,
11017                                                              null,
11018                                                              p_atp_record.organization_id,
11019                                                              l_sources.Ship_Method(j),
11020                                                              MSC_CALENDAR.ORC);
11021 
11022                             l_atp_rec.intransit_cal_code := MSC_CALENDAR.Get_Calendar_Code(
11023                                                              l_sources.instance_id(j),
11024                                                              null,
11025                                                              null,
11026                                                              null,
11027                                                              null,
11028                                                              4,
11029                                                              null,
11030                                                              l_sources.Ship_Method(j),
11031                                                              MSC_CALENDAR.VIC);
11032 
11033                             l_atp_rec.shipping_cal_code := MSC_CALENDAR.Get_Calendar_Code(
11034                                                              l_sources.instance_id(j),
11035                                                              null,
11036                                                              null,
11037                                                              null,
11038                                                              null,
11039                                                              null,
11040                                                              l_sources.organization_id(j),
11041                                                              l_sources.Ship_Method(j),
11042                                                              MSC_CALENDAR.OSC);
11043                         ELSE
11044                             -- Bug 3593394
11045                             IF PG_DEBUG in ('Y', 'C') THEN
11046                                 msc_sch_wb.atp_debug('ATP_Check: ' || 'G_USE_SHIP_REC_CAL is N');
11047                             END IF;
11048                             -- Bug 3737759 - Use receiving org's OMC - l_atp_rec.receiving_cal_code := MSC_CALENDAR.FOC;
11049                             l_atp_rec.receiving_cal_code := p_atp_record.manufacturing_cal_code;
11050                             l_atp_rec.intransit_cal_code := MSC_CALENDAR.FOC;
11051                             -- Bug 3737759 - Use shipping org's OMC - l_atp_rec.shipping_cal_code := MSC_CALENDAR.FOC;
11052                             l_atp_rec.shipping_cal_code := l_atp_rec.manufacturing_cal_code;
11053                         END IF;
11054 
11055                         /* Bug 3737759 - Moved to beginning of IF
11056                         l_atp_rec.manufacturing_cal_code := MSC_CALENDAR.Get_Calendar_Code(
11057                                                              l_sources.instance_id(j),
11058                                                              null,
11059                                                              null,
11060                                                              null,
11061                                                              null,
11062                                                              null,
11063                                                              l_sources.organization_id(j),
11064                                                              null,
11065                                                              MSC_CALENDAR.OMC);
11066                         */
11067                     ELSE
11068                         -- receiving party is org
11069                         /* Bug 3593394 - Moved after get_delivery_lead_time
11070                         l_atp_rec.receiving_cal_code := MSC_CALENDAR.Get_Calendar_Code(
11071                                                              p_atp_record.instance_id,
11072                                                              null,
11073                                                              null,
11074                                                              null,
11075                                                              null,
11076                                                              null,
11077                                                              p_atp_record.organization_id,
11078                                                              l_sources.Ship_Method(j),
11079                                                              MSC_CALENDAR.ORC);
11080                         */
11081 
11082                         -- supplier intransit LT
11083                         MSC_ATP_PROC.Get_Supplier_Regions(l_sources.Supplier_Site_Id(j),
11084                                                           724,  -- calling module is immaterial here
11085                                                           l_sources.instance_id(j),
11086                                                           l_return_status);
11087                         --3953635: If ship method is null then l_sources.Ship_Method(j) contains '@@@'
11088                         -- Decode it back to null if it contains '@@@'
11089                         IF l_sources.Ship_Method(j) <> '@@@' THEN
11090                             l_atp_rec.ship_method := l_sources.Ship_Method(j);
11091                         ELSE
11092                             l_atp_rec.ship_method := null;
11093                         END IF;
11094 
11095                         MSC_ATP_PROC.get_delivery_lead_time(
11096                                         NULL,              -- p_from_org_id
11097                                         NULL,              -- p_from_loc_id
11098                                         l_sources.instance_id(j),
11099                                         p_atp_record.organization_id,
11100                                         NULL,              -- p_to_loc_id
11101                                         l_sources.instance_id(j),
11102                                         NULL,              -- p_customer_id
11103                                         NULL,              -- p_customer_site_id
11104                                         l_sources.Supplier_Id(j),
11105                                         l_sources.Supplier_Site_Id(j),
11106                                         MSC_ATP_PVT.G_SESSION_ID,
11107                                         NULL,              -- p_partner_site_id
11108                                         l_atp_rec.ship_method,
11109                                         l_atp_rec.delivery_lead_time);
11110 
11111                         IF PG_DEBUG in ('Y', 'C') THEN
11112                            msc_sch_wb.atp_debug('ATP_Check: Use SMC irrespective of profile G_USE_SHIP_REC_CAL');
11113                         END IF;
11114                         --l_atp_rec.manufacturing_cal_code := p_atp_record.manufacturing_cal_code;
11115                         --Bug 3687353: Use SMC irrespective of profile MSC_ATP_PVT.G_USE_SHIP_REC_CAL
11116 
11117                         l_atp_rec.manufacturing_cal_code := MSC_CALENDAR.Get_Calendar_Code(
11118                                                              l_sources.instance_id(j),
11119                                                              p_plan_id,
11120                                                              --s_cto_rearch
11121                                                              --In case of ATP item pass model's id
11122                                                              --base item id is populated only for ATO items
11123                                                              NVL(C_ITEM_INFO_REC.base_item_id,
11124                                                                      C_ITEM_INFO_REC.dest_inv_item_id),
11125                                                              l_sources.supplier_id(j),
11126                                                              l_sources.supplier_site_id(j),
11127                                                              null,
11128                                                              null,
11129                                                              null,
11130                                                              MSC_CALENDAR.SMC);
11131 
11132                         -- Bug 3593394
11133                         IF MSC_ATP_PVT.G_USE_SHIP_REC_CAL='Y' THEN
11134 
11135                             l_atp_rec.receiving_cal_code := MSC_CALENDAR.Get_Calendar_Code(
11136                                                              p_atp_record.instance_id,
11137                                                              null,
11138                                                              null,
11139                                                              null,
11140                                                              null,
11141                                                              null,
11142                                                              p_atp_record.organization_id,
11143                                                              l_atp_rec.ship_method,
11144                                                              -- Bug 3593394 - l_sources.Ship_Method(j),
11145                                                              MSC_CALENDAR.ORC);
11146 
11147                             l_atp_rec.intransit_cal_code := MSC_CALENDAR.Get_Calendar_Code(
11148                                                              p_atp_record.instance_id,
11149                                                              null,
11150                                                              null,
11151                                                              null,
11152                                                              null,
11153                                                              4,
11154                                                              null,
11155                                                              l_atp_rec.ship_method,
11156                                                              MSC_CALENDAR.VIC);
11157 
11158                             l_atp_rec.shipping_cal_code := MSC_CALENDAR.Get_Calendar_Code(
11159                                                              l_sources.instance_id(j),
11160                                                              null,
11161                                                              null,
11162                                                              l_sources.supplier_id(j),
11163                                                              l_sources.supplier_site_id(j),
11164                                                              1,
11165                                                              null,
11166                                                              l_atp_rec.ship_method,
11167                                                              MSC_CALENDAR.SSC);
11168 
11169                             /* Bug 3687353: Use SMC irrespective of profile MSC_ATP_PVT.G_USE_SHIP_REC_CAL
11170                             l_atp_rec.manufacturing_cal_code := MSC_CALENDAR.Get_Calendar_Code(
11171                                                              l_sources.instance_id(j),
11172                                                              p_plan_id,
11173                                                              --s_cto_rearch
11174                                                              --In case of ATP item pass model's id
11175                                                              --base item id is populated only for ATO items
11176                                                              NVL(C_ITEM_INFO_REC.base_item_id,
11177                                                                      C_ITEM_INFO_REC.dest_inv_item_id),
11178                                                              l_sources.supplier_id(j),
11179                                                              l_sources.supplier_site_id(j),
11180                                                              null,
11181                                                              null,
11182                                                              null,
11183                                                              MSC_CALENDAR.SMC);
11184                             */
11185 
11186                         ELSE
11187 
11188                             -- Bug 3593394
11189                             IF PG_DEBUG in ('Y', 'C') THEN
11190                                 msc_sch_wb.atp_debug('ATP_Check: ' || 'G_USE_SHIP_REC_CAL is N');
11191                             END IF;
11192                             -- Bug 3737759 - Use receiving org's OMC - l_atp_rec.receiving_cal_code := MSC_CALENDAR.FOC;
11193                             l_atp_rec.receiving_cal_code := p_atp_record.manufacturing_cal_code;
11194                             l_atp_rec.intransit_cal_code := MSC_CALENDAR.FOC;
11195                             l_atp_rec.shipping_cal_code := MSC_CALENDAR.FOC;
11196 
11197                             /*Bug 3687353: Use SMC irrespective of profile MSC_ATP_PVT.G_USE_SHIP_REC_CAL
11198                             l_atp_rec.manufacturing_cal_code := p_atp_record.manufacturing_cal_code;*/
11199 
11200                         END IF;
11201                     END IF;
11202                     IF PG_DEBUG in ('Y', 'C') THEN
11203                        msc_sch_wb.atp_debug('  ');
11204                        msc_sch_wb.atp_debug('*  ___________________Output____________________');
11205                        msc_sch_wb.atp_debug('*  ');
11206                        msc_sch_wb.atp_debug('*  Receiving calendar code         : ' || l_atp_rec.receiving_cal_code);
11207                        msc_sch_wb.atp_debug('*  Intransit calendar code         : ' || l_atp_rec.intransit_cal_code);
11208                        msc_sch_wb.atp_debug('*  Shipping calendar code          : ' || l_atp_rec.shipping_cal_code);
11209                        msc_sch_wb.atp_debug('*  Manufacturing calendar code     : ' || l_atp_rec.manufacturing_cal_code);
11210                        msc_sch_wb.atp_debug('**************************************************************');
11211                     END IF;
11212 
11213                     /* planned order due date as per OMC-D */
11214                     l_planned_order_date := MSC_CALENDAR.PREV_WORK_DAY(
11215                                                 p_atp_record.manufacturing_cal_code,
11216                                                 p_atp_record.instance_id,
11217                                                 l_requested_ship_date);
11218                     IF PG_DEBUG in ('Y', 'C') THEN
11219                        msc_sch_wb.atp_debug('*  Planned Order Due Date as per OMC-D: ' || l_planned_order_date);
11220                     END IF;
11221                     -- ship_rec_cal changes end
11222 
11223                     IF ((l_sources.source_type(j) = MAKE) or
11224 			(l_sources.source_type(j) = BUY))
11225 		    THEN
11226 			  -- dsting
11227 			  IF PG_DEBUG in ('Y', 'C') THEN
11228 			     msc_sch_wb.atp_debug('ATP_Check: ' || 'MAKE_BUY_CHILDREN: have make/buy source');
11229 			  END IF;
11230 			  p_atp_record.children_type := MAKE_BUY_CHILDREN;
11231 			  G_HAVE_MAKE_BUY_PARENT := 1;
11232 		    END IF;
11233 
11234                     --diag_atp
11235                     --reinitialize all the variables so that values from last source is not carried over
11236                     l_pegging_rec.plan_name := null;
11237                     l_pegging_rec.required_quantity:= null;
11238                     l_pegging_rec.required_date := null;
11239                     --bug 3328421
11240                     l_pegging_rec.actual_supply_demand_date := null;
11241                     l_pegging_rec.postprocessing_lead_time := null;
11242                     l_pegging_rec.preprocessing_lead_time := null;
11243                     l_pegging_rec.processing_lead_time := null;
11244                     l_pegging_rec.constraint_date := null;
11245                     l_pegging_rec.constraint_type := null;
11246                     l_pegging_rec.intransit_lead_time := null;
11247                     l_pegging_rec.ship_method := null;
11248 
11249                     IF ((l_sources.source_type(j) = TRANSFER) or
11250                         (l_sources.source_type(j) = BUY)) THEN
11251 
11252                         -- 1214740
11253                         l_atp_rec.identifier := p_atp_record.identifier;
11254                         l_atp_rec.demand_source_type := p_atp_record.demand_source_type;--cmro
11255                         --- Enhance CTO Phase 1 Req #16, #17
11256                         -- Pass the demand source line across only for CTO case.
11257                         --IF  NVL(C_ITEM_INFO_REC.cto_source_org_id, -1) = p_atp_record.organization_id THEN
11258                         --IF ( MSC_ATP_PVT.G_CTO_LINE = 'Y' ) THEN
11259                         --    l_atp_rec.demand_source_line := p_atp_record.demand_source_line;
11260                         --END IF;
11261                         --- End Enhance CTO Phase 1 Req #16, #17
11262 
11263                         l_atp_rec.instance_id := l_sources.instance_id(j);
11264 
11265                         /* time_phased_atp changes begin
11266                            If time phased atp and buy scenario then populate member item id.
11267                            This is because we are not supporting aggregate supplier capacity currently*/
11268                         IF l_sources.source_type(j) = BUY THEN
11269                             l_atp_rec.inventory_item_id := l_request_item_id;
11270                         ELSE
11271                             l_atp_rec.inventory_item_id := p_atp_record.inventory_item_id;
11272                         END IF;
11273                         -- time_phased_atp changes end
11274 
11275                         l_atp_rec.request_item_id :=
11276 				p_atp_record.request_item_id;
11277 			---- EFTC: check for buy_make code. If it is 3 then no sources defined and
11278 			---- therefore no source is known
11279 
11280 			---    l_atp_rec.supplier_id := NULL;
11281 		       	---     l_atp_rec.supplier_site_id := NULL;
11282 		        ---	ELSE
11283                         l_atp_rec.supplier_id :=  l_sources.Supplier_Id(j);
11284                         l_atp_rec.supplier_site_id := l_sources.Supplier_Site_Id(j);
11285 			---END IF;
11286                         IF l_sources.source_type(j) = BUY  THEN
11287                           -- this is to make sure we know what's the
11288                           -- receiving org since supplier capacities are
11289                           -- defined this way.
11290 
11291                           -- dsting 2764213
11292                           l_planned_sources := 1;
11293 
11294                           -- for 1381331 fix
11295                           l_atp_rec.instance_id := p_atp_record.instance_id;
11296 
11297                           l_atp_rec.organization_id :=
11298 				p_atp_record.organization_id;
11299 
11300                           -- this is the plan id for next level
11301                           l_plan_id := p_plan_id;
11302                           l_assign_set_id := p_assign_set_id;
11303 
11304                           -- for bug 1124206: to be consistant with planning,
11305                           -- the lead time from a vendor site to an inv org
11306                           -- should be obtained from the postprocessing
11307                           -- lead time instead of leave it blank and
11308                           -- have atp pick up
11309                           -- the default one in mtl_interorg_ship_methods.
11310 
11311                           -- bug 1303196: delivery lead time is
11312                           -- postprocessing lead time,
11313                           -- not postprocessing lead time+1
11314 
11315                           -- Bug2085872 Check if l_inv_item_id is null.
11316                           IF l_inv_item_id IS NULL THEN
11317                              /* Modularize Item and Org Info */
11318                              -- Buy case
11319                              -- Re-Use info instead of making unnecessary call.
11320                              l_inv_item_id := C_ITEM_INFO_REC.dest_inv_item_id;
11321                              /*l_inv_item_id := MSC_ATP_FUNC.get_inv_item_id(
11322                                           		l_atp_rec.instance_id,
11323                                           		l_atp_rec.inventory_item_id,
11324                                                         null,
11325                                           		l_atp_rec.organization_id);
11326                               Modularize Item and Org Info */
11327                           END IF;
11328                           ---  2178544 replace this query as we already have processing lead time
11329                           /*begin
11330                           SELECT nvl(postprocessing_lead_time, 0)
11331                           INTO   l_atp_rec.delivery_lead_time
11332                           FROM   msc_system_items
11333                           WHERE  plan_id = p_plan_id
11334                           AND    organization_id = l_atp_rec.organization_id
11335                           AND    sr_instance_id = l_atp_rec.instance_id
11336                           AND    inventory_item_id = l_inv_item_id; -- 1665483
11337                           exception
11338                             when others then
11339                               l_atp_rec.delivery_lead_time := 0;
11340                           end; */
11341                           -- l_atp_rec.delivery_lead_time := l_post_pro_lt; -- suuplier intransit lt
11342 
11343                           --l_atp_rec.ship_method := 'DEFAULT';
11344                           l_new_summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;
11345 			  l_old_summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;
11346 
11347                           --diag_atp
11348                           l_atp_rec.plan_name := p_atp_record.plan_name;
11349 			  --Bug 5651914 start
11350 			  IF PG_DEBUG in ('Y', 'C') THEN
11351 			     msc_sch_wb.atp_debug('ATP_Check: ' || 'G_PLAN_INFO_REC.sr_instance_id: ' || G_PLAN_INFO_REC.sr_instance_id);
11352 			  END IF;
11353                           IF NVL(MSC_ATP_PVT.G_PLAN_INFO_REC.sr_instance_id,-1) = -1 THEN
11354 			     IF PG_DEBUG in ('Y', 'C') THEN
11355 			        msc_sch_wb.atp_debug('ATP_Check: ' || 'Re-calculating global plan information');
11356 			     END IF;
11357 			     SELECT trunc(cutoff_date),
11358                                     DECODE(plan_type, 4, 1,
11359                                         DECODE(daily_material_constraints, 1, NVL(enforce_pur_lt_constraints, 2),
11360                                             DECODE(daily_resource_constraints, 1, NVL(enforce_pur_lt_constraints, 2),
11361                                                 DECODE(weekly_material_constraints, 1, NVL(enforce_pur_lt_constraints, 2),
11362                                                     DECODE(weekly_resource_constraints, 1, NVL(enforce_pur_lt_constraints, 2),
11363                                                         DECODE(period_material_constraints, 1, NVL(enforce_pur_lt_constraints, 2),
11364                                                             DECODE(period_resource_constraints, 1, NVL(enforce_pur_lt_constraints, 2), 1)
11365                                                               )
11366                                                           )
11367                                                       )
11368                                                   )
11369                                               )
11370                                           ),
11371                                     sr_instance_id,
11372                                     trunc(plan_start_date),
11373                                     organization_id
11374                              INTO   MSC_ATP_PVT.G_PLAN_INFO_REC.plan_cutoff_date,
11375                                     MSC_ATP_PVT.G_PLAN_INFO_REC.enforce_pur_lead_time,
11376                                     MSC_ATP_PVT.G_PLAN_INFO_REC.sr_instance_id,
11377                                     MSC_ATP_PVT.G_PLAN_INFO_REC.plan_start_date,
11378                                     MSC_ATP_PVT.G_PLAN_INFO_REC.organization_id
11379                              FROM   msc_plans
11380                              WHERE  plan_id = p_plan_id;
11381                           END IF;
11382                           --Bug 5651914 end
11383 
11384                         ELSE
11385                           --s_cto_rearch
11386                           IF  PG_DEBUG in ('Y', 'C') THEN
11387                               msc_Sch_wb.atp_debug('Transfer case: bom_item_type := ' || C_ITEM_INFO_REC.bom_item_type);
11388                               msc_Sch_wb.atp_debug('Transfer case: parent_repl_ord_flag := ' || C_ITEM_INFO_REC.replenish_to_ord_flag);
11389 
11390                           END IF;
11391                           IF C_ITEM_INFO_REC.bom_item_type in (1, 2) and C_ITEM_INFO_REC.replenish_to_ord_flag = 'Y' THEN
11392                               IF PG_DEBUG in ('Y', 'C') THEN
11393                                  msc_Sch_wb.atp_debug('This is model entity. Pass Line IDs');
11394                               END IF;
11395                               --if model then pass the
11396                               l_atp_rec.Top_Model_line_id := p_atp_record.Top_Model_line_id;
11397                               l_atp_rec.ATO_Parent_Model_Line_Id := p_atp_record.ATO_Parent_Model_Line_Id;
11398                               l_atp_rec.ATO_Model_Line_Id := p_atp_record.ATO_Model_Line_Id;
11399                               l_atp_rec.Parent_line_id := p_atp_record.Parent_line_id;
11400                               l_atp_rec.wip_supply_type := p_atp_record.wip_supply_type;
11401                               l_atp_rec.parent_atp_flag := null;
11402                               l_atp_rec.parent_atp_comp_flag := null;
11403                               l_atp_rec.parent_repl_order_flag := null;
11404                               l_atp_rec.parent_bom_item_type := null;
11405                               l_atp_rec.bom_item_type := C_ITEM_INFO_REC.bom_item_type;
11406                               l_atp_rec.rep_ord_flag  := C_ITEM_INFO_REC.replenish_to_ord_flag;
11407                               l_atp_rec.mand_comp_flag := 2;
11408                               l_atp_rec.parent_so_quantity := p_atp_record.parent_so_quantity;
11409                               l_atp_rec.demand_source_line := p_atp_record.demand_source_line;
11410                               l_atp_rec.refresh_number :=  p_atp_record.refresh_number;
11411 
11412                           ELSIF C_ITEM_INFO_REC.bom_item_type  = 4 and C_ITEM_INFO_REC.replenish_to_ord_flag = 'Y' THEN
11413                               l_atp_rec.ATO_Model_Line_Id := p_atp_record.ATO_Model_Line_Id;
11414                           END IF;
11415                           --e_cto_rearch
11416                           -- transfer from which org.
11417                           l_atp_rec.organization_id :=
11418                                 l_sources.Organization_Id(j);
11419 
11420 
11421                           ---- store summary flags before and after get_plan_info
11422                           ---- The reason we are doing it is that we haven't added
11423                           ---- planned order as yet. If summary is not available for
11424                           ---- new plann then MSC_ATP_PVT.G_SUMMARY_FLAG will be set to 'N'.
11425                           ---- When we try to add planned order then the sumamry tables will
11426                           ----- not be updated even though the G_SUMMARY_FLAG for the old org's
11427                           ----- plan was 'Y'
11428 
11429                           l_old_summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;
11430 
11431                           /*
11432                           -- New procedure for obtaining plan data : Supplier Capacity Lead Time proj.
11433                                 -- (SCLT)
11434                           MSC_ATP_PROC.get_global_plan_info(l_atp_rec.instance_id,
11435                                       l_atp_rec.inventory_item_id,
11436                                       l_atp_rec.organization_id,
11437                                       l_atp_rec.demand_class);*/
11438 
11439                           --Bug4548245 When we call this procedure from schedule then we check in ODS data
11440                            l_atp_rec.inventory_item_id := MSC_ATP_PF.Get_PF_Atp_Item_Id(l_atp_rec.instance_id,
11441                                                                              -1,
11442                                                                              l_atp_rec.request_item_id,
11443                                                                              l_atp_rec.organization_id);
11444                           --set the correct family item value
11445                           --Bug4548245 end
11446                           /* time_phased_atp changes begin
11447                              Call new procedure Get_PF_Plan_Info*/
11448                           MSC_ATP_PF.Get_PF_Plan_Info(
11449                                        l_atp_rec.instance_id,
11450                                        l_atp_rec.request_item_id,
11451                                        l_atp_rec.inventory_item_id,
11452                                        l_atp_rec.organization_id,
11453                                        l_atp_rec.demand_class,
11454                                        l_atp_rec.atf_date,
11455                                        l_atp_rec.error_code,
11456                                        l_return_status,
11457                                        p_plan_id --bug3510475 same plan when
11458                                                  --moving from one org to another
11459                           );
11460 
11461                           IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
11462                                 IF PG_DEBUG in ('Y', 'C') THEN
11463                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'ATP Downtime');
11464                                 END IF;
11465                                 x_return_status := FND_API.G_RET_STS_ERROR;
11466                                 RAISE FND_API.G_EXC_ERROR;
11467                           END IF;
11468                           /* time_phased_atp changes end*/
11469 
11470                           l_plan_info_rec := MSC_ATP_PVT.G_PLAN_INFO_REC;
11471                           -- End New procedure for obtaining plan data : Supplier Capacity Lead Time proj.
11472 
11473                           l_plan_id             := l_plan_info_rec.plan_id;
11474                           l_assign_set_id       := l_plan_info_rec.assignment_set_id;
11475                           -- changes for bug 2392456 ends
11476 
11477 
11478                           l_new_summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;
11479 
11480                           /* bug 3953635: Del_lead_time and ship method have already been populated
11481                           -- for bug 1124206
11482                           l_atp_rec.delivery_lead_time :=
11483                                 l_sources.Lead_Time(j);
11484                           l_atp_rec.ship_method :=
11485                                 l_sources.Ship_Method(j);
11486                           */
11487                           --diag_atp
11488                           l_atp_rec.plan_name := l_plan_info_rec.plan_name;
11489 
11490                         END IF;
11491 
11492                         -- need to do uom quantity conversion
11493 
11494                         -- Bug 2382963, ATP was erroring out in case a component is being transferred
11495                         -- from an organization with no ATPable plan due to PDS-ODS switch
11496 
11497                         -- 24x7 ATP
11498                         -- IF (l_plan_id is NULL) or (l_plan_id = -100) THEN
11499                         IF NVL(l_plan_id, -1) = -1 or (l_plan_id  = -100  ) THEN
11500                            IF PG_DEBUG in ('Y', 'C') THEN
11501                               msc_sch_wb.atp_debug('ATP_Check: ' || 'Plan not found for : '||
11502 				                    l_atp_rec.inventory_item_id
11503 				                    ||' in org : '||l_atp_rec.organization_id);
11504                            END IF;
11505                         ELSIF l_plan_id = -300 then
11506                            IF PG_DEBUG in ('Y', 'C') THEN
11507                               msc_sch_wb.atp_debug('ATP_Check: ' || 'ATP Downtime in plan for' ||
11508 				                    l_atp_rec.inventory_item_id
11509 				                    ||' in org : '||l_atp_rec.organization_id);
11510                            END IF;
11511                            --bug 2854351
11512                            --l_atp_rec.error_code := TRY_ATP_LATER;
11513                            l_atp_rec.error_code := PLAN_DOWN_TIME;
11514                            MSC_ATP_PVT.G_DOWNTIME_HIT := 'Y';
11515                            RAISE MSC_ATP_PVT.EXC_NO_PLAN_FOUND;
11516 
11517                         ELSIF l_plan_id is NOT NULL THEN
11518                            -- dsting 2764213
11519                            l_planned_sources := 1;
11520 
11521                            MSC_ATP_PROC.inv_primary_uom_conversion(l_atp_rec.instance_id,
11522                                           l_atp_rec.organization_id,
11523                                           l_atp_rec.inventory_item_id,
11524                                           p_atp_record.quantity_uom,
11525                                           l_atp_rec.quantity_uom,
11526                                           l_conversion_rate);
11527 
11528                            -- dsting 2754446 adjust planned order amt according to rounding_control_type
11529                            --bug3397904 start
11530                            --bug 3904063: Reset the quantity only in case of demand priorit allocated ATP
11531                            IF NVL(C_ITEM_INFO_REC.rounding_control_type, 2) = 1 THEN
11532                               --l_PO_qty := CEIL(l_net_demand); --bug3397904
11533                               IF (p_search = FORWARD_SCHEDULING)
11534                                  and MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y'
11535                                  AND MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1
11536                                  AND MSC_ATP_PVT.G_ALLOCATION_METHOD = 1 THEN  --bug3397904
11537 
11538                                    l_PO_qty := CEIL(p_atp_record.quantity_ordered);
11539                                    IF PG_DEBUG in ('Y', 'C') THEN
11540                                       msc_sch_wb.atp_debug('If l_PO_qty: ' || l_PO_qty);
11541                                    END IF;
11542                               ELSE
11543                                l_PO_qty := CEIL(l_net_demand);
11544                                IF PG_DEBUG in ('Y', 'C') THEN
11545                                 msc_sch_wb.atp_debug('Else l_PO_qty: ' || l_PO_qty);
11546                                END IF;
11547                               END IF;
11548                            ELSE
11549                               --bug 3904063: Reset the quantity only in case of demand priorit allocated ATP
11550                               IF (p_search = FORWARD_SCHEDULING)
11551                                  and MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y'
11552                                  AND MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1
11553                                  AND MSC_ATP_PVT.G_ALLOCATION_METHOD = 1 THEN  --bug3397904
11554 
11555                                    l_PO_qty := trunc(p_atp_record.quantity_ordered, 6);   --5598066
11556                                    IF PG_DEBUG in ('Y', 'C') THEN
11557                                       msc_sch_wb.atp_debug('If l_PO_qty: ' || l_PO_qty);
11558                                    END IF;
11559                               ELSE
11560                                l_PO_qty := trunc(l_net_demand, 6);	-- 5598066
11561                                IF PG_DEBUG in ('Y', 'C') THEN
11562                                 msc_sch_wb.atp_debug('Else l_PO_qty: ' || l_PO_qty);
11563                                END IF;
11564                               END IF;
11565                            END IF;
11566                           --bug3397904 end
11567                            l_atp_rec.quantity_ordered:=l_PO_qty*
11568                                                        l_conversion_rate;
11569                           l_atp_rec.quantity_ordered := ROUND(l_atp_rec.quantity_ordered, 6);  --4562646
11570 --                        l_atp_rec.quantity_ordered:=l_net_demand*
11571 --                                                    l_conversion_rate;
11572 
11573                         -- since it is a transfer/buy, we specify request
11574 		        -- arrival date not request ship date
11575 
11576                         -- Need to initialize these dates so that only one is populated
11577 
11578                         l_atp_rec.requested_ship_date := NULL;
11579                         l_atp_rec.requested_arrival_date := NULL;
11580 
11581                         IF PG_DEBUG in ('Y', 'C') THEN
11582                            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_requested_ship_date := ' || l_requested_ship_date);
11583                         END IF;
11584 
11585                         --- 2178544 calculate start date
11586                         --diag_atp
11587                         l_constraint_flag := 2;
11588                         -- Enforce Pur LT
11589                         l_trunc_started := 2;
11590                         IF l_sources.source_type(j) = TRANSFER  THEN
11591 
11592                             IF PG_DEBUG in ('Y', 'C') THEN
11593                                msc_sch_wb.atp_debug('ATP_Check: ' || 'Backward Scheduling ');
11594                                msc_sch_wb.atp_debug('ATP_Check: ' || 'From Org : '|| l_atp_rec.organization_id);
11595                                msc_sch_wb.atp_debug('ATP_Check: ' || 'To Org : '|| p_atp_record.organization_id);
11596                                msc_sch_wb.atp_debug('ATP_Check: ' || 'Lead Time : '|| l_atp_rec.delivery_lead_time);
11597                                msc_sch_wb.atp_debug('ATP_Check: ' || 'PPLT '||l_post_pro_lt);
11598                                msc_sch_wb.atp_debug('ATP_Check: ' || 'Ship_method := ' || l_sources.ship_method(j));
11599                                msc_sch_wb.atp_debug('ATP_Check: ' || 'MSC_ATP_PVT.G_PTF_DATE := ' ||MSC_ATP_PVT.G_PTF_DATE );
11600                                msc_sch_wb.atp_debug('ATP_Check: ' || 'l_ptf_date_for_child := ' ||l_ptf_date_for_child );
11601                                msc_sch_wb.atp_debug('ATP_Check: ' || 'l_ptf_date := ' ||l_ptf_date);
11602                             END IF;
11603 
11604                             -- Bug 3782472 - calculate ptf date for lower level
11605                             l_ptf_date_for_child := MSC_CALENDAR.DATE_OFFSET (
11606                                                 p_atp_record.manufacturing_cal_code,
11607                                                 p_atp_record.instance_id,
11608                                                 l_parent_ptf_date,
11609                                                 -1 * l_post_pro_lt, -1);
11610                             IF PG_DEBUG in ('Y', 'C') THEN
11611                                msc_sch_wb.atp_debug('ATP_Check: ' ||
11612                                                     'After Man. Cal Offset l_ptf_date_for_child := ' ||l_ptf_date_for_child );
11613                             END IF;
11614                             l_ptf_date_for_child := MSC_CALENDAR.THREE_STEP_CAL_OFFSET_DATE(
11615         					l_ptf_date_for_child, l_atp_rec.receiving_cal_code, -1,
11616         					l_atp_rec.intransit_cal_code, -1 * l_atp_rec.delivery_lead_time, -1,
11617         					l_atp_rec.shipping_cal_code, -1, p_atp_record.instance_id);
11618                             IF PG_DEBUG in ('Y', 'C') THEN
11619                                msc_sch_wb.atp_debug('ATP_Check: ' || 'After 3 Set Cal  Offset l_ptf_date_for_child := ' ||l_ptf_date_for_child );
11620                                msc_sch_wb.atp_debug('ATP_Check: ' || 'MSC_ATP_PVT.G_PTF_DATE := ' ||MSC_ATP_PVT.G_PTF_DATE );
11621                                msc_sch_wb.atp_debug('ATP_Check: ' || 'l_ptf_date_for_child := ' ||l_ptf_date_for_child );
11622                                msc_sch_wb.atp_debug('ATP_Check: ' || 'l_ptf_date := ' ||l_ptf_date);
11623                             END IF;
11624 
11625                             MSC_ATP_PVT.G_PTF_DATE := GREATEST(l_ptf_date_for_child,l_ptf_date,MSC_ATP_PVT.g_ptf_date); --5053993
11626 
11627                             IF PG_DEBUG in ('Y', 'C') THEN
11628                                msc_sch_wb.atp_debug('ATP_Check: ' || 'After Offset MSC_ATP_PVT.G_PTF_DATE := '
11629                                                     ||MSC_ATP_PVT.G_PTF_DATE );
11630                             END IF;
11631 
11632                             /* ship_rec_cal changes begin */
11633                            l_dock_date := MSC_CALENDAR.DATE_OFFSET (
11634                                                 p_atp_record.manufacturing_cal_code,
11635                                                 p_atp_record.instance_id,
11636                                                 l_requested_ship_date,
11637                                                 -1 * l_post_pro_lt, -1);
11638 
11639                            IF PG_DEBUG in ('Y', 'C') THEN
11640                               msc_sch_wb.atp_debug('ATP_Check: ' || 'l_ptf_date_for_child: '||l_ptf_date_for_child);
11641                               msc_sch_wb.atp_debug('ATP_Check: ' || 'Setting ptf_date for lower level: '||MSC_ATP_PVT.G_PTF_DATE);
11642                               msc_sch_wb.atp_debug('ATP_Check: ' || '___________________Start Offset___________________');
11643                               msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after subtracting PPLT using OMC: '||l_dock_date);
11644                            END IF;
11645 
11646                            l_dock_date := MSC_CALENDAR.PREV_WORK_DAY(
11647                                                 l_atp_rec.receiving_cal_code,
11648                                                 p_atp_record.instance_id,
11649                                                 l_dock_date);
11650                            /* populating new_dock_date for transfer orders also, this is to support requirement of supporting
11651                               org modeled as supplier which might come in future*/
11652                            l_new_dock_date := l_dock_date;
11653 
11654                            IF PG_DEBUG in ('Y', 'C') THEN
11655                               msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after validating on ORC : '||l_dock_date);
11656                            END IF;
11657 
11658                            l_start_date := MSC_CALENDAR.DATE_OFFSET (
11659                                                 l_atp_rec.intransit_cal_code,
11660                                                 p_atp_record.instance_id,
11661                                                 l_dock_date,
11662                                                 -1 * l_atp_rec.delivery_lead_time, -1);
11663                            -- Bug No  4592289/4667756/5053993 Start
11664                            IF PG_DEBUG in ('Y', 'C') THEN
11665                               msc_sch_wb.atp_debug('ATP_Check: ' || 'G_PTF_DATE before offset  : '||MSC_ATP_PVT.G_PTF_DATE);
11666                               msc_sch_wb.atp_debug('ATP_Check: ' || 'l_PTF_DATE before offset  : '||l_PTF_DATE);
11667                               msc_sch_wb.atp_debug('ATP_Check: ' || 'l_START_DATE before offset  : '||l_START_DATE);
11668                               msc_sch_wb.atp_debug('ATP_Check: ' || 'From Org : '|| l_atp_rec.organization_id);
11669                               msc_sch_wb.atp_debug('ATP_Check: ' || 'To Org : '|| p_atp_record.organization_id);
11670                               msc_sch_wb.atp_debug('ATP_Check: ' || 'Lead Time : '|| l_atp_rec.delivery_lead_time);
11671                            END IF;
11672                            IF l_PTF_DATE < MSC_ATP_PVT.G_PTF_DATE  THEN
11673                               MSC_ATP_PVT.G_PTF_DATE := MSC_CALENDAR.DATE_OFFSET (l_atp_rec.intransit_cal_code,
11674                                                                    p_atp_record.instance_id,
11675                                                                       MSC_ATP_PVT.G_PTF_DATE ,
11676                                                                    -1 * l_atp_rec.delivery_lead_time, -1); -- Bug No: 4592289/4667756
11677                            IF PG_DEBUG in ('Y', 'C') THEN
11678                                  msc_sch_wb.atp_debug('ATP_Check: ' || 'G_PTF_DATE after offset  : '||MSC_ATP_PVT.G_PTF_DATE);
11679                            END IF;
11680                            else
11681                               G_PTF_DATE := l_PTF_DATE;
11682                               msc_sch_wb.atp_debug('ATP_Check: ' || 'G_PTF_DATE after assign  : '||MSC_ATP_PVT.G_PTF_DATE );
11683                            END IF;
11684                            MSC_ATP_PVT.G_PTF_DATE := MSC_CALENDAR.NEXT_WORK_DAY(
11685                                                                 l_atp_rec.shipping_cal_code,
11686                                                                 p_atp_record.instance_id,
11687                                                                 MSC_ATP_PVT.G_PTF_DATE );
11688                            IF PG_DEBUG in ('Y', 'C') THEN
11689                               msc_sch_wb.atp_debug('ATP_Check: ' || 'G_PTF_DATE after offset(Shipping cal)  : '||MSC_ATP_PVT.G_PTF_DATE);
11690                            END IF;
11691 
11692                            -- Bug No  4592289/4667756/5053993 Ends
11693 
11694                            IF PG_DEBUG in ('Y', 'C') THEN
11695                               msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after subtracting DLT using VIC : '||l_start_date);
11696                            END IF;
11697 
11698                            l_atp_rec.requested_ship_date := MSC_CALENDAR.PREV_WORK_DAY(
11699                                                 l_atp_rec.shipping_cal_code,
11700                                                 l_atp_rec.instance_id,
11701                                                 l_start_date);
11702                            /* populating new_ship_date for transfer orders also, this is to support requirement of supporting
11703                               org modeled as supplier which might come in future*/
11704                            l_new_ship_date := l_atp_rec.requested_ship_date;
11705                            l_sugg_start_date := l_new_ship_date;        -- Bug 3241766
11706                            l_start_date := l_new_ship_date; -- Bug 3412143 -- Setting the variable that is compared with PTF date
11707                                                             -- This should be source org's ship date.
11708 
11709                            IF PG_DEBUG in ('Y', 'C') THEN
11710                               msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after validating on OSC : '||l_atp_rec.requested_ship_date);
11711                            END IF;
11712 
11713                            l_order_date := MSC_CALENDAR.DATE_OFFSET (
11714                                                 p_atp_record.manufacturing_cal_code,
11715                                                 p_atp_record.instance_id,
11716                                                 l_atp_rec.requested_ship_date,
11717                                                 -1 * MSC_ATP_PVT.G_ITEM_INFO_REC.pre_pro_lt, -1);
11718 
11719                            IF PG_DEBUG in ('Y', 'C') THEN
11720                               msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after subtracting pre PLT using OMC : '||l_order_date);
11721                               msc_sch_wb.atp_debug('ATP_Check: ' || '____________________End Offset____________________');
11722                            END IF;
11723                            -- ship_rec_cal changes end
11724 
11725                            IF PG_DEBUG in ('Y', 'C') THEN
11726                               msc_sch_wb.atp_debug('ATP_Check: ' || 'Ship Date '||l_atp_rec.requested_ship_date);
11727                            END IF;
11728 
11729                            IF (MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 or p_search = forward_scheduling) and
11730                                   -- ship_rec_cal
11731                                   -- (l_dock_date < l_ptf_date or l_order_date < sysdate) THEN
11732                                   -- Bug 3412143 - Ship date in source org should be greater than receiving org's PTF
11733                                   -- Also use l_trunc_sysdate instead of accessing sysdate directly.
11734                                   (l_start_date < l_ptf_date or l_order_date < l_trunc_sysdate) THEN
11735 
11736                               /* ship_rec_cal changes begin*/
11737                               IF PG_DEBUG in ('Y', 'C') THEN
11738                                   msc_sch_wb.atp_debug('ATP_Check: ' || '___________________Start Calc PTF Due Date___________________');
11739                                   msc_sch_wb.atp_debug('ATP_Check: ' || 'PTF Date: '||l_ptf_date);
11740                               END IF;
11741 
11742                               /* Bug 3412143 - Earlier ptf_due_date was calculated as
11743                               ptf_due_date := ptf_date + pre-processing lead time + delivery lead time + post-processing lead time
11744                               it should actually be
11745                               ptf_due_date := ptf_date + delivery lead time + post-processing lead time
11746                               l_ptf_due_date := MSC_CALENDAR.DATE_OFFSET (
11747                                                 p_atp_record.manufacturing_cal_code,
11748                                                 p_atp_record.instance_id,
11749                                                 l_ptf_date,
11750                                                 MSC_ATP_PVT.G_ITEM_INFO_REC.pre_pro_lt, 1);
11751 
11752                               IF PG_DEBUG in ('Y', 'C') THEN
11753                                   msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after adding pre PLT using OMC: '||l_ptf_due_date);
11754                               END IF;
11755                               */
11756 
11757                               l_ptf_due_date := MSC_CALENDAR.THREE_STEP_CAL_OFFSET_DATE(
11758                         			l_ptf_date, l_atp_rec.shipping_cal_code, +1,
11759                         			l_atp_rec.intransit_cal_code, nvl(l_atp_rec.delivery_lead_time, 0), +1,
11760                         			l_atp_rec.receiving_cal_code, +1, p_atp_record.instance_id);
11761 
11762                               IF PG_DEBUG in ('Y', 'C') THEN
11763                                   msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after validating on OSC, adding intransit LT using VIC and ');
11764                                   msc_sch_wb.atp_debug('ATP_Check: ' || '    validating on ORC: '||l_ptf_due_date);
11765                               END IF;
11766 
11767                               l_ptf_due_date :=  MSC_CALENDAR.DATE_OFFSET (
11768                                                 p_atp_record.manufacturing_cal_code, -- using OMC destination
11769                                                 p_atp_record.instance_id,
11770                                                 l_ptf_due_date,
11771                                                 l_post_pro_lt, 1);
11772 
11773                               IF PG_DEBUG in ('Y', 'C') THEN
11774                                   msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after adding PPLT using OMC: '||l_ptf_due_date);
11775                                   msc_sch_wb.atp_debug('ATP_Check: ' || '___________________End Calc PTF Due Date___________________');
11776                                   msc_sch_wb.atp_debug('ATP_Check: ' || ' ');
11777                                   msc_sch_wb.atp_debug('ATP_Check: ' || '___________________Start Calc LT Due Date___________________');
11778                                   msc_sch_wb.atp_debug('ATP_Check: ' || 'Sysdate: '||sysdate);
11779                               END IF;
11780 
11781                               l_lt_due_date := MSC_CALENDAR.DATE_OFFSET (
11782                                                 p_atp_record.manufacturing_cal_code,
11783                                                 p_atp_record.instance_id,
11784                                                 -- sysdate, - Bug 3512996 - Removed unnecessary reference to sysdate
11785                                                 l_trunc_sysdate,
11786                                                 MSC_ATP_PVT.G_ITEM_INFO_REC.pre_pro_lt, 1);
11787 
11788                               -- Bug 3512996 - Set this date as PTF date for the source org so that the call to ATP_Check
11789                               --               for source org does not return a date earlier than this date, thereby enforcing
11790                               --               pre-PLT for transfer cases. Also this date doesn't need to be validated on OSC
11791                               --               the output of ATP_Check will anyway be validated on OSC.
11792                               MSC_ATP_PVT.G_PTF_DATE := GREATEST(l_lt_due_date, MSC_ATP_PVT.G_PTF_DATE);
11793 
11794                               IF PG_DEBUG in ('Y', 'C') THEN
11795                                   msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after adding pre PLT using OMC: '||l_lt_due_date);
11796                                   msc_sch_wb.atp_debug('ATP_Check: ' || 'Setting PTF for lower level: '||MSC_ATP_PVT.G_PTF_DATE);
11797                               END IF;
11798 
11799                               l_lt_due_date := MSC_CALENDAR.THREE_STEP_CAL_OFFSET_DATE(
11800                         			l_lt_due_date, l_atp_rec.shipping_cal_code, +1,
11801                         			l_atp_rec.intransit_cal_code, nvl(l_atp_rec.delivery_lead_time, 0), +1,
11802                         			l_atp_rec.receiving_cal_code, +1, p_atp_record.instance_id);
11803 
11804                               IF PG_DEBUG in ('Y', 'C') THEN
11805                                   msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after validating on OSC, adding intransit LT using VIC and ');
11806                                   msc_sch_wb.atp_debug('ATP_Check: ' || '    validating on ORC: '||l_lt_due_date);
11807                               END IF;
11808 
11809                               l_lt_due_date :=  MSC_CALENDAR.DATE_OFFSET (
11810                                                 p_atp_record.manufacturing_cal_code, -- using OMC destination
11811                                                 p_atp_record.instance_id,
11812                                                 l_lt_due_date,
11813                                                 l_post_pro_lt, 1);
11814 
11815                               IF PG_DEBUG in ('Y', 'C') THEN
11816                                   msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after adding PPLT using OMC: '||l_lt_due_date);
11817                                   msc_sch_wb.atp_debug('ATP_Check: ' || '___________________End Calc LT Due Date___________________');
11818                                   msc_sch_wb.atp_debug('ATP_Check: ' || ' ');
11819                               END IF;
11820                               /* ship_rec_cal end */
11821 
11822                               IF PG_DEBUG in ('Y', 'C') THEN
11823                                  msc_sch_wb.atp_debug('ATP_Check: ' || 'l_lt_due_date := ' || l_lt_due_date);
11824                                  msc_sch_wb.atp_debug('ATP_Check: ' || 'l_ptf_due_date := ' || l_ptf_due_date);
11825                               END IF;
11826 
11827 
11828                               -- Bug 3525582 - Constraint message should be added only if constraint actually exists
11829                               IF l_lt_due_date > l_requested_ship_date OR
11830                                  l_ptf_due_date > l_requested_ship_date THEN
11831 
11832                                  -- Bug 3525582 - PTF constraint should be added only if plan is PTF enabled
11833                                  IF l_ptf_enabled=2 OR l_lt_due_date > l_ptf_due_date THEN
11834                                     --transfer lead time constraint
11835                                     l_pegging_rec.constraint_type := 5;
11836                                     l_pegging_rec.constraint_date := l_lt_due_date;
11837                                     l_constraint_flag := 1;
11838                                  ELSE
11839                                     ---PTF lead time constraint
11840                                     l_pegging_rec.constraint_type := 2;
11841                                     l_pegging_rec.constraint_date := l_ptf_due_date;
11842                                     l_constraint_flag := 1;
11843                                  END IF;
11844 
11845                               END IF;
11846 
11847                            END IF; --- IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 THEN
11848 
11849                         ELSE
11850                            --s_cto_rearch
11851                              l_atp_rec.base_model_id := C_ITEM_INFO_REC.base_item_id;
11852                              l_atp_rec.bom_item_type := C_ITEM_INFO_REC.bom_item_type;
11853                              l_atp_rec.rep_ord_flag :=  C_ITEM_INFO_REC.replenish_to_ord_flag;
11854                            --e_cto_rearch
11855 
11856                             /* Enforce Pur LT - Moved code to get last cap date from Get_Supplier_ATP_Info. At this point it
11857                                         is sufficient to know whether capacity is defined or not. However since this table
11858                                         is being queried, we are getting last cap date as well.*/
11859                             SELECT max(trunc(NVL(to_date,G_PLAN_INFO_REC.plan_cutoff_date)))  --4055719
11860                             INTO   l_atp_rec.last_cap_date
11861                             FROM   msc_supplier_capacities s
11862                             WHERE  s.inventory_item_id = NVL(C_ITEM_INFO_REC.base_item_id,
11863                                                          C_ITEM_INFO_REC.dest_inv_item_id)
11864                             AND    s.sr_instance_id = l_sources.instance_id(j)
11865                             AND    s.plan_id = p_plan_id
11866                             AND    s.organization_id = p_atp_record.organization_id
11867                             AND    s.supplier_id = l_sources.supplier_id(j)
11868                             AND    NVL(s.supplier_site_id, -1) = NVL(l_sources.supplier_site_id(j), -1);
11869 
11870                             IF PG_DEBUG in ('Y', 'C') THEN
11871                               msc_sch_wb.atp_debug('ATP_Check: ' || 'last_cap_date: '||l_atp_rec.last_cap_date);
11872                             END IF;
11873                             /* Enforce Pur LT - changes end */
11874 
11875 
11876                             /* ship_rec_cal changes begin */
11877                             l_start_date := MSC_CALENDAR.DATE_OFFSET(
11878                                                  p_atp_record.manufacturing_cal_code,
11879                                                  p_atp_record.instance_id,
11880                                                  l_requested_ship_date,
11881                                                  -1 * l_post_pro_lt, -1);
11882 
11883                             IF PG_DEBUG in ('Y', 'C') THEN
11884                               msc_sch_wb.atp_debug('ATP_Check: ' || 'enforce_pur_lead_time: ' || G_PLAN_INFO_REC.enforce_pur_lead_time);
11885                               msc_sch_wb.atp_debug('ATP_Check: ' || '___________________Start Offset___________________');
11886                               msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after subtracting PPLT using OMC: '||l_start_date);
11887                             END IF;
11888 
11889                             /* Enforce Pur LT - check if date is past due. dock date can be past due only if capacity is not defined. */
11890                             IF l_start_date < l_trunc_sysdate AND G_PLAN_INFO_REC.enforce_pur_lead_time = 2
11891                                 AND l_atp_rec.last_cap_date IS NULL THEN
11892                                 l_trunc_started := 1;
11893                                 l_start_date := l_trunc_sysdate;
11894                                 IF PG_DEBUG in ('Y', 'C') THEN
11895                                     msc_sch_wb.atp_debug('ATP_Check: ' || 'Past due date truncated to: '||l_trunc_sysdate);
11896                                 END IF;
11897                             END IF;
11898 
11899                             /* Enforce Pur LT - No need to calculate dates if trunc has started */
11900                             IF l_trunc_started = 2 THEN
11901                                 -- Actually dock date
11902                                 l_start_date := MSC_CALENDAR.PREV_WORK_DAY(
11903                                                 l_atp_rec.receiving_cal_code,
11904                                                 p_atp_record.instance_id,
11905                                                 l_start_date);
11906                                 IF PG_DEBUG in ('Y', 'C') THEN
11907                                     msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after validating on ORC : '||l_start_date);
11908                                 END IF;
11909                                 /* Enforce Pur LT - check if date is past due. dock date can be past due only
11910                                         if capacity is not defined. */
11911                                 IF l_start_date < l_trunc_sysdate AND G_PLAN_INFO_REC.enforce_pur_lead_time = 2
11912                                     AND l_atp_rec.last_cap_date IS NULL THEN
11913                                     l_trunc_started := 1;
11914                                     l_start_date := l_trunc_sysdate;
11915                                     IF PG_DEBUG in ('Y', 'C') THEN
11916                                         msc_sch_wb.atp_debug('ATP_Check: ' || 'Past due date truncated to: '||l_trunc_sysdate);
11917                                     END IF;
11918                                 END IF;
11919                             END IF;
11920 
11921                            /* populating new_dock_date */
11922                            l_new_dock_date := l_start_date;
11923 
11924                            IF PG_DEBUG in ('Y', 'C') THEN
11925                               msc_sch_wb.atp_debug('ATP_Check: ' || 'Dock date : '||l_new_dock_date);
11926                               msc_sch_wb.atp_debug('ATP_Check: ' || '  ');
11927                            END IF;
11928                            /* ship_rec_cal changes end */
11929 
11930                            --diag_atp: we calculate the supplier lead time here.
11931                            -- Supplier Capacity and Lead Time (SCLT) changes
11932 
11933                            BEGIN
11934                              -- l_process_lt := l_fixed_lt + l_variable_lt * l_net_demand;
11935                              -- Fixed + Variable is discontinued, Instead item's processing lead time
11936                              -- essentially full_lead_time is used.
11937 
11938                              l_process_lt := MSC_ATP_PVT.G_ITEM_INFO_REC.processing_lt;
11939                              l_pre_pro_lt := MSC_ATP_PVT.G_ITEM_INFO_REC.pre_pro_lt;
11940                              IF PG_DEBUG in ('Y', 'C') THEN
11941                               msc_sch_wb.atp_debug('ATP_Check: ' || 'Item Process_lead_time = '|| l_process_lt);
11942                               msc_sch_wb.atp_debug('ATP_Check: ' || 'l_pre_pro_lt  = ' || l_pre_pro_lt);
11943                              END IF;
11944 
11945                              IF (l_sources.Supplier_Id(j) <> -99) THEN
11946                                 -- Query changed to include lead_time_date
11947                                 select   nvl(processing_lead_time, l_process_lt), supplier_lead_time_date
11948                                 into     l_process_lt, g_sup_cap_cum_date
11949                                 from     msc_item_suppliers
11950                                 where    plan_id = p_plan_id
11951                                 and      sr_instance_id =  p_atp_record.instance_id
11952                                 and      organization_id = p_atp_record.organization_id
11953                                  --bug 3373598: Use model's inv id for config item.
11954                                 -- base_item_id will be populated only for ATO items
11955                                 and      inventory_item_id = NVL(C_ITEM_INFO_REC.base_item_id, l_inv_item_id)
11956                                 and      supplier_id =  l_sources.Supplier_Id(j)
11957                                 and      supplier_site_id = l_sources.Supplier_site_Id(j);
11958                              ELSE
11959                                 g_sup_cap_cum_date := null;
11960                              END IF;
11961 
11962                            Exception
11963                                WHEN others THEN
11964                                  g_sup_cap_cum_date := null;
11965                            END;
11966                            IF PG_DEBUG in ('Y', 'C') THEN
11967                                msc_sch_wb.atp_debug('ATP_Check: ' || 'Plan Id = ' || p_plan_id);
11968                                msc_sch_wb.atp_debug('ATP_Check: ' || 'Instance Id = ' || p_atp_record.instance_id);
11969                                msc_sch_wb.atp_debug('ATP_Check: ' || 'Organization Id = ' || p_atp_record.organization_id);
11970                                msc_sch_wb.atp_debug('ATP_Check: ' || 'Item Id = ' || l_inv_item_id);
11971                                msc_sch_wb.atp_debug('ATP_Check: ' || 'Supplier Id = ' || l_sources.Supplier_Id(j));
11972                                msc_sch_wb.atp_debug('ATP_Check: ' || 'Supplier Site Id = ' || l_sources.Supplier_site_Id(j));
11973 
11974                                msc_sch_wb.atp_debug('ATP_Check: ' || 'Process lead time after ASL query = '
11975                                                                                             || l_process_lt);
11976                                msc_sch_wb.atp_debug('ATP_Check: ' || 'Supplier Lead Time Date = '
11977                                                                                        || g_sup_cap_cum_date);
11978                            END IF;
11979                            -- Bug 2803195
11980                            IF (g_sup_cap_cum_date IS NOT NULL) THEN
11981                                -- Get the next_working day based on plan owning org's calendar.
11982                                -- to account for MBP's -ve 1 day offset.
11983                                /* ship_rec_cal
11984                                g_sup_cap_cum_date :=  MSC_CALENDAR.next_work_day
11985                                                      (G_PLAN_INFO_REC.organization_id,
11986                                                       G_PLAN_INFO_REC.sr_instance_id,
11987                                                       1,
11988                                                       g_sup_cap_cum_date + 1);*/
11989                                g_sup_cap_cum_date :=  MSC_CALENDAR.next_work_day(
11990                                                       l_atp_rec.manufacturing_cal_code,
11991                                                       G_PLAN_INFO_REC.sr_instance_id,
11992                                                       g_sup_cap_cum_date + 1);
11993                                IF PG_DEBUG in ('Y', 'C') THEN
11994                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'Supplier Lead Time Date Next Day offset = '
11995                                                                                        || g_sup_cap_cum_date);
11996                                END IF;
11997                            END IF;
11998                            -- End Bug 2803195
11999                            -- End Supplier Capacity and Lead Time changes
12000                            l_net_processing_lt := CEIL(l_process_lt + l_pre_pro_lt);
12001 
12002                            -- Supplier Capacity and Lead Time (SCLT) changes
12003                               -- l_atp_rec.atp_lead_time := l_net_processing_lt;
12004                            -- Only the processing lead time will be passed back into ATP_Check
12005                            l_atp_rec.atp_lead_time := l_process_lt;
12006                            -- End Supplier Capacity and Lead Time changes
12007                            IF PG_DEBUG in ('Y', 'C') THEN
12008                               msc_sch_wb.atp_debug('ATP_Check: ' || 'l_process_lt := ' || l_process_lt);
12009                               msc_sch_wb.atp_debug('ATP_Check: ' || 'l_net_processing_lt := ' || l_net_processing_lt);
12010                            END IF;
12011 
12012                             -- Bug 3782472 - calculate ptf date for lower level
12013                             l_ptf_date_for_child := MSC_CALENDAR.DATE_OFFSET (
12014                                                 p_atp_record.manufacturing_cal_code,
12015                                                 p_atp_record.instance_id,
12016                                                 l_parent_ptf_date,
12017                                                 -1 * l_post_pro_lt, -1);
12018 
12019                             MSC_ATP_PVT.G_PTF_DATE := GREATEST(l_ptf_date_for_child, l_ptf_date);
12020 
12021                             /* ship_rec_cal changes begin */
12022                             -- intransit lead time offset in case of capacity to ship
12023                             IF l_sources.sup_cap_type(j) = 1 THEN
12024                                     IF PG_DEBUG in ('Y', 'C') THEN
12025                                         msc_sch_wb.atp_debug('ATP_Check: ' || '--- Capacity to Ship ---');
12026                                     END IF;
12027 
12028                                     -- Bug 3782472 - calculate ptf date for lower level
12029                                     l_ptf_date_for_child := MSC_CALENDAR.THREE_STEP_CAL_OFFSET_DATE(
12030         					l_ptf_date_for_child, l_atp_rec.receiving_cal_code, -1,
12031         					l_atp_rec.intransit_cal_code, -1 * l_atp_rec.delivery_lead_time, -1,
12032         					l_atp_rec.shipping_cal_code, -1, p_atp_record.instance_id);
12033 
12034                                     /* Enforce Pur LT - Check if date needs to be calculated */
12035                                     IF l_trunc_started = 1 THEN
12036                                         l_supplier_start_date := l_trunc_sysdate;
12037                                     ELSE
12038                                         l_supplier_start_date := MSC_CALENDAR.DATE_OFFSET(
12039                                                         l_atp_rec.intransit_cal_code,
12040                                                         l_atp_rec.instance_id,
12041                                                         l_new_dock_date,
12042                                                         (-1 * l_atp_rec.delivery_lead_time), -1);
12043                                         IF PG_DEBUG in ('Y', 'C') THEN
12044                                             msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after subtracting intransit LT using VIC : '||l_supplier_start_date);
12045                                         END IF;
12046                                         /* Enforce Pur LT - check if date is past due. for capacity to ship cases ship date
12047                                                 can be past due only if capacity is not defined. */
12048                                         IF G_PLAN_INFO_REC.enforce_pur_lead_time = 2 AND l_atp_rec.last_cap_date IS NULL
12049                                             AND l_supplier_start_date < l_trunc_sysdate THEN
12050                                             l_supplier_start_date := l_trunc_sysdate;
12051                                             l_trunc_started := 1;
12052                                             IF PG_DEBUG in ('Y', 'C') THEN
12053                                                 msc_sch_wb.atp_debug('ATP_Check: ' || 'Past due date truncated to: '||l_trunc_sysdate);
12054                                             END IF;
12055                                         END IF;
12056                                     END IF;
12057 
12058 
12059                                     /* Enforce Pur LT - Check if date needs to be calculated */
12060                                     IF l_trunc_started = 2 THEN
12061                                         l_supplier_start_date := MSC_CALENDAR.PREV_WORK_DAY(
12062                                                 l_atp_rec.shipping_cal_code,
12063                                                 l_atp_rec.instance_id,
12064                                                 l_supplier_start_date);
12065                                         IF PG_DEBUG in ('Y', 'C') THEN
12066                                             msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after validating on SSC : '||l_supplier_start_date);
12067                                         END IF;
12068                                         /* Enforce Pur LT - check if date is past due. for capacity to ship cases ship date
12069                                                 can be past due only if capacity is not defined. */
12070                                         IF G_PLAN_INFO_REC.enforce_pur_lead_time = 2 AND l_atp_rec.last_cap_date IS NULL
12071                                             AND l_supplier_start_date < l_trunc_sysdate THEN
12072                                             l_supplier_start_date := l_trunc_sysdate;
12073                                             l_trunc_started := 1;
12074                                             IF PG_DEBUG in ('Y', 'C') THEN
12075                                                 msc_sch_wb.atp_debug('ATP_Check: ' || 'Past due date truncated to: '||l_trunc_sysdate);
12076                                             END IF;
12077                                         END IF;
12078                                     END IF;
12079 
12080                                    /* populating new_ship_date */
12081                                    l_new_ship_date := l_supplier_start_date;
12082 
12083                                    l_atp_rec.requested_ship_date := l_new_ship_date;
12084 
12085                                    IF PG_DEBUG in ('Y', 'C') THEN
12086                                       msc_sch_wb.atp_debug('ATP_Check: ' || 'Ship date :'||l_new_ship_date);
12087                                       msc_sch_wb.atp_debug('ATP_Check: ' || '---------------------------');
12088                                    END IF;
12089                             ELSE
12090                                     l_supplier_start_date := l_new_dock_date;
12091                                     -- supplier intransit LT
12092 
12093                                     l_atp_rec.requested_ship_date := l_new_dock_date;
12094 
12095                                     -- Bug 3782472 - calculate ptf date for lower level
12096                                     l_ptf_date_for_child := MSC_CALENDAR.PREV_WORK_DAY(
12097                                                 l_atp_rec.receiving_cal_code,
12098                                                 p_atp_record.instance_id,
12099                                                 l_ptf_date_for_child);
12100 
12101                                     IF PG_DEBUG in ('Y', 'C') THEN
12102                                         msc_sch_wb.atp_debug('ATP_Check: ' || '--- Capacity to Dock ---');
12103                                         msc_sch_wb.atp_debug('ATP_Check: ' || 'Supplier Start Date : '||l_supplier_start_date);
12104                                     END IF;
12105 
12106                                     /* populating new_ship_date in Capacity to Dock scenarios also */
12107                                     /* Enforce Pur LT - Check if date needs to be calculated */
12108                                     IF l_trunc_started = 1 THEN
12109                                         l_new_ship_date := l_trunc_sysdate;
12110                                     ELSE
12111                                         l_new_ship_date := MSC_CALENDAR.DATE_OFFSET(
12112                                                         l_atp_rec.intransit_cal_code,
12113                                                         l_atp_rec.instance_id,
12114                                                         l_start_date,
12115                                                         (-1 * l_atp_rec.delivery_lead_time), -1);
12116 
12117                                         IF PG_DEBUG in ('Y', 'C') THEN
12118                                             msc_sch_wb.atp_debug('ATP_Check: ' || '--- Calculate ship date ---');
12119                                             msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after subtracting intransit LT using VIC : '||l_new_ship_date);
12120                                         END IF;
12121 
12122                                         /* Enforce Pur LT - check if date is past due.*/
12123                                         IF G_PLAN_INFO_REC.enforce_pur_lead_time = 2 AND l_new_ship_date < l_trunc_sysdate THEN
12124                                             l_new_ship_date := l_trunc_sysdate;
12125                                             -- Not setting the flag l_trunc_started as dock date may still be more than sysdate
12126                                             -- and in capacity to dock cases further calculation of dates (start date, order date)
12127                                             -- is done from dock date.
12128                                             IF PG_DEBUG in ('Y', 'C') THEN
12129                                                 msc_sch_wb.atp_debug('ATP_Check: ' || 'Past due date truncated to: '||l_trunc_sysdate);
12130                                             END IF;
12131                                         ELSE
12132                                             l_new_ship_date := MSC_CALENDAR.PREV_WORK_DAY(
12133                                                         l_atp_rec.shipping_cal_code,
12134                                                         l_atp_rec.instance_id,
12135                                                         l_new_ship_date);
12136 
12137                                             IF PG_DEBUG in ('Y', 'C') THEN
12138                                                 msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after validating on SSC :'||l_new_ship_date);
12139                                             END IF;
12140 
12141                                             /* Enforce Pur LT - check if date is past due.*/
12142                                             IF G_PLAN_INFO_REC.enforce_pur_lead_time = 2 AND l_new_ship_date < l_trunc_sysdate THEN
12143                                                 l_new_ship_date := l_trunc_sysdate;
12144                                                 IF PG_DEBUG in ('Y', 'C') THEN
12145                                                     msc_sch_wb.atp_debug('ATP_Check: ' || 'Past due date truncated to: '||l_trunc_sysdate);
12146                                                 END IF;
12147                                             END IF;
12148                                         END IF;
12149 
12150                                     END IF;
12151 
12152                                    IF PG_DEBUG in ('Y', 'C') THEN
12153                                       msc_sch_wb.atp_debug('ATP_Check: ' || 'Ship date :'||l_new_ship_date);
12154                                       msc_sch_wb.atp_debug('ATP_Check: ' || '---------------------------');
12155                                    END IF;
12156 
12157                             END IF;
12158 
12159                             /* Enforce Pur LT - Check if date needs to be calculated */
12160                             IF l_trunc_started = 1 THEN
12161                                 l_supplier_start_date := l_trunc_sysdate;
12162                             ELSIF l_process_lt > 0 THEN
12163                                 l_supplier_start_date := MSC_CALENDAR.DATE_OFFSET(
12164                                                                 l_atp_rec.manufacturing_cal_code,
12165                                                                 G_PLAN_INFO_REC.sr_instance_id,
12166                                                                 l_supplier_start_date,
12167                                                                 (-1 * l_process_lt), -1);
12168                                 IF PG_DEBUG in ('Y', 'C') THEN
12169                                     msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after offsetting processing LT on SMC :'||l_supplier_start_date);
12170                                 END IF;
12171                                 /* Enforce Pur LT - check if date is past due.*/
12172                                 IF G_PLAN_INFO_REC.enforce_pur_lead_time = 2 AND l_supplier_start_date < l_trunc_sysdate THEN
12173                                     l_supplier_start_date := l_trunc_sysdate;
12174                                     l_trunc_started := 1;
12175                                     IF PG_DEBUG in ('Y', 'C') THEN
12176                                         msc_sch_wb.atp_debug('ATP_Check: ' || 'Past due date truncated to: '||l_trunc_sysdate);
12177                                     END IF;
12178                                 END IF;
12179                             END IF;
12180 
12181                             l_sugg_start_date := l_supplier_start_date;          -- Bug 3241766
12182 
12183                             /* Enforce Pur LT - Check if date needs to be calculated */
12184                             IF l_trunc_started = 2 THEN
12185                                 l_supplier_start_date := MSC_CALENDAR.DATE_OFFSET(
12186                                                         p_atp_record.manufacturing_cal_code, -- use OMC
12187                                                         l_atp_rec.instance_id,
12188                                                         l_supplier_start_date,
12189                                                         (-1 * l_pre_pro_lt), -1);
12190                                 IF PG_DEBUG in ('Y', 'C') THEN
12191                                   msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after subtracting pre PLT using OMC : '||l_supplier_start_date);
12192                                 END IF;
12193                                 /* Enforce Pur LT - check if date is past due.*/
12194                                 IF G_PLAN_INFO_REC.enforce_pur_lead_time = 2 AND l_supplier_start_date < l_trunc_sysdate THEN
12195                                     l_supplier_start_date := l_trunc_sysdate;
12196                                     l_trunc_started := 1;
12197                                     IF PG_DEBUG in ('Y', 'C') THEN
12198                                         msc_sch_wb.atp_debug('ATP_Check: ' || 'Past due date truncated to: '||l_trunc_sysdate);
12199                                     END IF;
12200                                 END IF;
12201                             END IF;
12202 
12203                             l_order_date := l_supplier_start_date;               -- Bug 3241766
12204 
12205                             -- Bug 3782472 - set ptf date for lower level
12206                             MSC_ATP_PVT.G_PTF_DATE := GREATEST(l_ptf_date_for_child, l_ptf_date);
12207 
12208                            IF PG_DEBUG in ('Y', 'C') THEN
12209                               msc_sch_wb.atp_debug('ATP_Check: ' || '____________________End Offset____________________');
12210                               msc_sch_wb.atp_debug('ATP_Check: ' || 'l_start_date := ' || l_start_date);
12211                               msc_sch_wb.atp_debug('ATP_Check: ' || 'l_supplier_start_date := ' || l_supplier_start_date);
12212                               msc_sch_wb.atp_debug('ATP_Check: ' || 'l_ptf_date_for_child := ' || l_ptf_date_for_child);
12213                               msc_sch_wb.atp_debug('ATP_Check: ' || 'Setting ptf_date for lower level: '||MSC_ATP_PVT.G_PTF_DATE);
12214                            END IF;
12215                            /* ship_rec_cal changes end */
12216 
12217                            IF (MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 or p_search = FORWARD_SCHEDULING) and
12218                               (l_start_date < l_ptf_date or
12219                                  l_supplier_start_date < l_trunc_sysdate) THEN -- Enforce Pur LT - change sysdate to avoid
12220                                  -- l_supplier_start_date < sysdate) THEN      -- extraneous call to sysdate
12221 
12222                               /* ship_rec_cal changes begin */
12223                               IF PG_DEBUG in ('Y', 'C') THEN
12224                                   msc_sch_wb.atp_debug('ATP_Check: ' || '___________________Start Calc PTF Due Date___________________');
12225                                   msc_sch_wb.atp_debug('ATP_Check: ' || 'PTF Date: '||l_ptf_date);
12226                               END IF;
12227 
12228                               IF l_start_date < l_ptf_date and l_post_pro_lt > 0 THEN
12229                                  l_ptf_due_date := MSC_CALENDAR.DATE_OFFSET(
12230                                                          p_atp_record.manufacturing_cal_code,
12231                                                          p_atp_record.instance_id,
12232                                                          l_ptf_date,
12233                                                          l_post_pro_lt, 1);
12234 
12235                                  IF PG_DEBUG in ('Y', 'C') THEN
12236                                      msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after adding PPLT using OMC: '||l_ptf_due_date);
12237                                  END IF;
12238                               ELSE
12239                                  --- bug 2771784: earliest due date should be PTF date. Not start date
12240                                  --l_ptf_due_date := l_start_date;
12241                                  -- ship_rec_cal i dont need to move l_ptf_date to next_work_day on mfg cal
12242                                  l_ptf_due_date :=  l_ptf_date;
12243                               END IF;
12244 
12245                               /* 3316028 (Enforce Pur LT) - PTF and sysdate validations were done here as well as after
12246                                         the call to ATP_Check for supplier. In forward pass doing all the calculations here
12247                                         itself and passing request ship date as the actual date on which capacity should
12248                                         be checked. l_supplier_request_date stores this date. */
12249                               /* Bug 3828494 (done with 3828469) - Don't change request date as that shows up in the pegging.
12250                                  This constraint will get enforced via the G_PTF_DATE which has already been set to a date
12251                                  greater than or equal to l_ptf_date
12252                               IF p_search = FORWARD_SCHEDULING THEN
12253                                   l_supplier_request_date := GREATEST(l_start_date, l_ptf_date);
12254                                   l_supplier_request_date := MSC_CALENDAR.PREV_WORK_DAY(
12255                                                             l_atp_rec.receiving_cal_code,
12256                                                             l_atp_rec.instance_id,
12257                                                             l_supplier_request_date);
12258 
12259                                   IF PG_DEBUG in ('Y', 'C') THEN
12260                                       msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after validating on ORC : '||l_supplier_request_date);
12261                                   END IF;
12262 
12263                                   IF l_sources.sup_cap_type(j) = 1 THEN
12264                                       -- capacity to be checked on ship date
12265                                       l_supplier_request_date := MSC_CALENDAR.DATE_OFFSET(
12266                                                             l_atp_rec.intransit_cal_code,
12267                                                             l_atp_rec.instance_id,
12268                                                             l_supplier_request_date,
12269                                                             (-1 * l_atp_rec.delivery_lead_time), -1);
12270                                       IF PG_DEBUG in ('Y', 'C') THEN
12271                                           msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after subtracting intransit LT using VIC : '||l_supplier_request_date);
12272                                       END IF;
12273                                       l_supplier_request_date := MSC_CALENDAR.PREV_WORK_DAY(
12274                                                     l_atp_rec.shipping_cal_code,
12275                                                     l_atp_rec.instance_id,
12276                                                     l_supplier_request_date);
12277                                   END IF;
12278                                   -- IF PTF is more constraining then this would be the date at which capacity would be checked
12279                                   l_atp_rec.requested_ship_date := l_supplier_request_date;
12280                               END IF;
12281                               */
12282 
12283                               IF PG_DEBUG in ('Y', 'C') THEN
12284                                   msc_sch_wb.atp_debug('ATP_Check: ' || '___________________End Calc PTF Due Date___________________');
12285                                   msc_sch_wb.atp_debug('ATP_Check: ' || ' l_ptf_due_date := ' || l_ptf_due_date);
12286                                   msc_sch_wb.atp_debug('ATP_Check: ' || 'Sysdate: '||l_trunc_sysdate);
12287                                   msc_sch_wb.atp_debug('ATP_Check: ' || '___________________Start Calc LT Due Date___________________');
12288                               END IF;
12289                               -- IF l_supplier_start_date < l_sysdate and (l_post_pro_lt + l_net_processing_lt) >0 THEN
12290                               IF l_supplier_start_date < l_trunc_sysdate
12291                                  and ((l_post_pro_lt + l_net_processing_lt)>0
12292                                       or (l_sources.sup_cap_type(j) = 1 and l_atp_rec.delivery_lead_time>0))THEN
12293                                    -- Enforce Pur LT - change sysdate to avoid extraneous call to sysdate
12294                                    -- Bug 3316028 - also included transit time for capacity to ship cases
12295 
12296                                    --Bug 3821358
12297                                    IF l_trunc_sysdate < l_sys_next_date THEN
12298                                       l_store_order_date  := l_sys_next_date - 1;
12299                                    ELSE
12300                                       l_store_order_date  := l_trunc_sysdate;
12301                                    END IF;
12302 
12303                                    l_lt_due_date := MSC_CALENDAR.DATE_OFFSET(
12304                                                                 p_atp_record.manufacturing_cal_code,
12305                                                                 l_atp_rec.instance_id,
12306                                                                 l_trunc_sysdate,
12307                                                                 l_pre_pro_lt, 1);
12308                                    --Bug 3821358
12309                                    l_store_start_date := l_lt_due_date;
12310 
12311                                    IF PG_DEBUG in ('Y', 'C') THEN
12312                                        msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after adding Pre PLT using OMC: '||l_lt_due_date);
12313                                    END IF;
12314 
12315                                    IF l_process_lt > 0 THEN
12316                                            l_lt_due_date := MSC_CALENDAR.DATE_OFFSET(
12317                                                                         l_atp_rec.manufacturing_cal_code,
12318                                                                         G_PLAN_INFO_REC.sr_instance_id,
12319                                                                         l_lt_due_date,
12320                                                                         l_process_lt, 1);
12321                                            IF PG_DEBUG in ('Y', 'C') THEN
12322                                                msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after adding PLT using SMC: '||l_lt_due_date);
12323                                            END IF;
12324                                    END IF;
12325 
12326                                    -- intransit lead time offset in case of capacity to ship
12327                                    IF l_sources.sup_cap_type(j) = 1 THEN
12328                                            l_lt_due_date:= MSC_CALENDAR.NEXT_WORK_DAY(
12329                                                                 l_atp_rec.shipping_cal_code,
12330                                                                 p_atp_record.instance_id,
12331                                                                 l_lt_due_date);
12332                                            --Bug 3821358
12333                                            l_store_ship_date := l_lt_due_date;
12334 
12335                                            /* Bug 3828494 (done with 3828469)
12336                                            IF p_search = FORWARD_SCHEDULING THEN
12337                                                l_supplier_request_date := l_lt_due_date;
12338                                            END IF;
12339                                            */
12340                                            l_ptf_date_for_child := l_lt_due_date;       -- Bug 3828494 (done with 3828469)
12341 
12342                                            IF PG_DEBUG in ('Y', 'C') THEN
12343                                                msc_sch_wb.atp_debug('ATP_Check: ' || '--- Capacity to Ship ---');
12344                                                msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after validating on SSC: '||l_lt_due_date);
12345                                            END IF;
12346 
12347                                            l_lt_due_date := MSC_CALENDAR.DATE_OFFSET(
12348                                                                 l_atp_rec.intransit_cal_code,
12349                                                                 G_PLAN_INFO_REC.sr_instance_id,
12350                                                                 l_lt_due_date,
12351                                                                 l_atp_rec.delivery_lead_time, 1);
12352 
12353                                            l_lt_due_date := MSC_CALENDAR.NEXT_WORK_DAY(
12354                                                                 l_atp_rec.receiving_cal_code,
12355                                                                 p_atp_record.instance_id,
12356                                                                 l_lt_due_date);
12357 
12358                                            --Bug 3821358
12359                                            l_store_dock_date := l_lt_due_date ;
12360 
12361                                            IF PG_DEBUG in ('Y', 'C') THEN
12362                                                msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after adding intransit LT using VIC: '||l_lt_due_date);
12363                                            END IF;
12364 
12365                                    ELSE
12366                                            IF PG_DEBUG in ('Y', 'C') THEN
12367                                                msc_sch_wb.atp_debug('ATP_Check: ' || '--- Capacity to Dock ---');
12368                                            END IF;
12369 
12370                                            --Bug 3821358
12371                                            l_lt_due_date := MSC_CALENDAR.NEXT_WORK_DAY(
12372                                                                l_atp_rec.receiving_cal_code,
12373                                                                p_atp_record.instance_id,
12374                                                                l_lt_due_date);
12375 
12376                                            l_store_dock_date :=  l_lt_due_date;
12377 
12378                                            l_store_ship_date := MSC_CALENDAR.DATE_OFFSET(
12379                                                                 l_atp_rec.intransit_cal_code,
12380                                                                 G_PLAN_INFO_REC.sr_instance_id,
12381                                                                 l_lt_due_date,
12382                                                                 (-1 * l_atp_rec.delivery_lead_time), -1);
12383 
12384                                            l_store_ship_date := MSC_CALENDAR.PREV_WORK_DAY(
12385                                                                 l_atp_rec.shipping_cal_code,
12386                                                                 p_atp_record.instance_id,
12387                                                                 l_store_ship_date);
12388                                    END IF;
12389 
12390                                    /*l_lt_due_date := MSC_CALENDAR.NEXT_WORK_DAY(
12391                                                         l_atp_rec.receiving_cal_code,
12392                                                         p_atp_record.instance_id,
12393                                                         l_lt_due_date);*/
12394 
12395                                    /* Bug 3828494 (done with 3828469)
12396                                    IF p_search = FORWARD_SCHEDULING AND l_sources.sup_cap_type(j) = 2 THEN
12397                                        l_supplier_request_date := l_lt_due_date;
12398                                    END IF;
12399                                    */
12400                                    IF l_sources.sup_cap_type(j) = 2 THEN
12401                                        l_ptf_date_for_child := l_lt_due_date;   -- Bug 3828494 (done with 3828469)
12402                                    END IF;
12403 
12404                                    IF PG_DEBUG in ('Y', 'C') THEN
12405                                        msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after validating on ORC: '||l_lt_due_date);
12406                                    END IF;
12407 
12408                                    l_lt_due_date := MSC_CALENDAR.DATE_OFFSET(
12409                                                          p_atp_record.manufacturing_cal_code,
12410                                                          p_atp_record.instance_id,
12411                                                          l_lt_due_date,
12412                                                          l_post_pro_lt, 1);
12413 
12414                                    l_store_due_date :=  l_lt_due_date;
12415 
12416                                    IF PG_DEBUG in ('Y', 'C') THEN
12417                                        msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after adding PPLT using OMC: '||l_lt_due_date);
12418                                    END IF;
12419 
12420                                    --Bug 3821358
12421                                    l_store_g_ptf_date := l_ptf_date_for_child;
12422 
12423                               ELSE
12424                                  l_lt_due_date := l_supplier_start_date;
12425                                  /* Bug 3828494 (done with 3828469)
12426                                  IF p_search = FORWARD_SCHEDULING THEN
12427                                      l_supplier_request_date := GREATEST(l_supplier_start_date,l_trunc_sysdate);
12428                                  END IF;
12429                                  */
12430                                  l_ptf_date_for_child := GREATEST(l_supplier_start_date,l_trunc_sysdate);
12431                               END IF;
12432 
12433                               /* Bug 3828494 (done with 3828469) - Don't change request date as that shows up in the pegging.
12434                                  Instead pass this constraint as G_PTF_DATE
12435                               l_atp_rec.requested_ship_date := GREATEST(l_atp_rec.requested_ship_date,l_supplier_request_date);
12436                               */
12437 
12438                               MSC_ATP_PVT.G_PTF_DATE := GREATEST(MSC_ATP_PVT.G_PTF_DATE, l_ptf_date_for_child);
12439 
12440                               -- PTF for lower level = greatest(sysdate+prePLT, current org's PTF, parent org's PTF)
12441                               -- after validating on all applicable calendars.
12442 
12443                               IF PG_DEBUG in ('Y', 'C') THEN
12444                                   msc_sch_wb.atp_debug('ATP_Check: ' || '___________________End Calc LT Due Date___________________');
12445                                   msc_sch_wb.atp_debug('ATP_Check: ' || 'l_lt_due_date := ' || l_lt_due_date);
12446                                   msc_sch_wb.atp_debug('ATP_Check: ' || 'l_ptf_date_for_child: '||l_ptf_date_for_child);
12447                                   msc_sch_wb.atp_debug('ATP_Check: ' || 'Setting ptf_date for lower level: '||MSC_ATP_PVT.G_PTF_DATE);
12448                               END IF;
12449                               /* ship_rec_cal changes end */
12450 
12451                               -- Bug 3525582 - Constraint message should be added only if constraint actually exists
12452                               IF l_lt_due_date > l_requested_ship_date OR
12453                                  l_ptf_due_date > l_requested_ship_date THEN
12454 
12455                                  -- Bug 3525582 - PTF constraint should be added only if plan is PTF enabled
12456                                  IF l_ptf_enabled=2 OR l_lt_due_date > l_ptf_due_date THEN
12457                                     --purchasing lead time constraint
12458                                     l_pegging_rec.constraint_type := 4;
12459                                     l_pegging_rec.constraint_date := l_lt_due_date;
12460                                     l_constraint_flag := 1;
12461 
12462                                  ELSE
12463                                     --PTF lead time constraint
12464                                     l_pegging_rec.constraint_type := 2;
12465                                     l_pegging_rec.constraint_date := l_ptf_due_date;
12466                                     l_constraint_flag := 1;
12467 
12468                                 END IF;
12469                              END IF;
12470                            END IF;
12471 
12472                         END IF;
12473 
12474                         IF PG_DEBUG in ('Y', 'C') THEN
12475                            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_start_date := ' || l_start_date);
12476                            msc_sch_wb.atp_debug('ATP_Check: ' || 'ship_date := ' || l_atp_rec.requested_ship_date);
12477                            msc_sch_wb.atp_debug('ATP_Check: ' || 'arrival_date := ' || l_atp_rec.requested_arrival_date);
12478                         END IF;
12479 
12480                         l_atp_rec.latest_acceptable_date := null;
12481                         l_atp_rec.freight_carrier := null;
12482                         -- 1665483
12483                         --- bug 1870820 This bug was occuring because following line was commented out
12484                         --- As a result demand class was not getting passed to -get_supplier_atp_info proc
12485                         --- the line was commented out for bug 1665483. Uncommenting the line after discussing
12486                         --- with christine. She has commented this line out so that demand class info is not
12487                         --- passed to the org where we are transferring the item from and demand class
12488                         --- for second org gets enforced
12489                         l_atp_rec.demand_class := p_atp_record.demand_class;
12490                         l_atp_rec.override_flag := null;
12491                         l_atp_rec.ship_date := null;
12492                         l_atp_rec.action := p_atp_record.action;
12493                         l_atp_rec.insert_flag := p_atp_record.insert_flag;
12494                         l_atp_rec.to_location_id :=
12495  				MSC_ATP_FUNC.get_location_id(p_atp_record.instance_id,
12496                                 p_atp_record.organization_id,
12497                                 null,
12498                                 null,
12499                                 null,
12500                                 null);
12501                         l_atp_rec.to_organization_id :=
12502                             p_atp_record.organization_id;
12503 			l_atp_rec.to_instance_id := p_atp_record.instance_id;
12504 
12505                         l_atp_rec.order_number := p_atp_record.order_number;
12506 
12507                         IF PG_DEBUG in ('Y', 'C') THEN
12508                            msc_sch_wb.atp_debug('ATP_Check: ' || 'ORDER_NUMBER is : ' || l_atp_rec.order_number);
12509                            msc_sch_wb.atp_debug('ATP_Check: ' || 'Demand Source Type is : ' || l_atp_rec.demand_source_type);--cmro
12510                            msc_sch_wb.atp_debug('ATP_Check: ' || 'net_demand is : ' || l_net_demand);
12511                         END IF;
12512                         --- End Enhance CTO Phase 1 Req #16 and #17
12513 
12514 
12515                         -- insert a plan order for the l_net_demand
12516                          ---- Change the flag temprorily as new plan's summary flag might be
12517                          ---- different from last plan's summary flag
12518                          ----
12519                         MSC_ATP_PVT.G_SUMMARY_FLAG := l_old_summary_flag;
12520                         /* rajjain 02/19/2003 Bug 2788302 Begin
12521                          * set org to null for Supplier's case. */
12522                         IF NVL(l_atp_rec.supplier_id,-1) <> -1 THEN
12523                            msc_sch_wb.atp_debug('Set Org id null in Supplier case');
12524                            l_src_org_id := NULL;
12525                         ELSE
12526                            l_src_org_id := l_atp_rec.organization_id;
12527                         END IF;
12528                         -- rajjain 02/19/2003 Bug 2788302 End
12529 
12530                         /* Modularize Item and Org Info */
12531                         -- Changed call to remove embedded function call.
12532 
12533 			-- dsting only add planned_order if it's not a constraint node
12534 			l_transaction_id := NULL;
12535                         --bug 2792702: Remove this condition as we need to create planned order
12536                         --during forward scheduling
12537 			--IF l_pegging_rec.constraint_type IS NULL THEN
12538                         -- Begin ATP4drp Create Planned Arrivals for DRP plans
12539                         IF (NVL(MSC_ATP_PVT.G_PLAN_INFO_REC.plan_type,1) = 5) AND (l_src_org_id is NOT NULL) THEN
12540                              l_supply_rec.instance_id            := p_atp_record.instance_id;
12541                              l_supply_rec.plan_id                := p_plan_id;
12542                              l_supply_rec.inventory_item_id      :=
12543                                                   C_ITEM_INFO_REC.dest_inv_item_id;
12544                              l_supply_rec.organization_id        := p_atp_record.organization_id;
12545                              l_supply_rec.schedule_date          := l_planned_order_date;
12546                              l_supply_rec.order_quantity         := l_PO_qty;
12547                              l_supply_rec.demand_class           := p_atp_record.demand_class;
12548                              -- l_supply_rec.source_organization_id
12549                              -- l_supply_rec.source_sr_instance_id
12550                              -- Source fields will be updated as a part of
12551                              -- Update_Planned_Order process.
12552                              l_supply_rec.refresh_number         := p_refresh_number;
12553                              l_supply_rec.shipping_cal_code      := l_atp_rec.shipping_cal_code;
12554                              l_supply_rec.receiving_cal_code     := l_atp_rec.receiving_cal_code;
12555                              l_supply_rec.intransit_cal_code     := l_atp_rec.intransit_cal_code;
12556                              l_supply_rec.new_ship_date          := l_new_ship_date;
12557                              l_supply_rec.new_dock_date          := l_new_dock_date;
12558 
12559                              --BUG 4488336: Populate correct start date
12560                              --l_supply_rec.start_date             := l_new_dock_date;
12561                              l_supply_rec.start_date             := l_start_date;
12562                              l_supply_rec.order_date             := l_order_date;
12563                              l_supply_rec.ship_method            := l_atp_rec.ship_method;
12564 
12565                              --l_supply_rec.return_status
12566 
12567                              l_supply_rec.firm_planned_type := 2;
12568                              l_supply_rec.disposition_status_type := 1;
12569                              l_supply_rec.record_source := 2; -- ATP created
12570                              l_supply_rec.supply_type := 51; --- planned arrival
12571                              l_supply_rec.intransit_lead_time := l_atp_rec.delivery_lead_time; --4127630
12572 
12573                              MSC_ATP_DB_UTILS.ADD_Supplies(l_supply_rec);
12574                              -- Asssign the output to local variables.
12575                              l_transaction_id := l_supply_rec.transaction_id;
12576                              l_return_status  := l_supply_rec.return_status;
12577                              IF PG_DEBUG in ('Y', 'C') THEN
12578                                 msc_sch_wb.atp_debug('ATP_Check: ' || 'Supply Id l_transaction_id: ' || l_transaction_id);
12579                                 msc_sch_wb.atp_debug('ATP_Check: ' || 'Supply Type l_supply_rec.supply_type: '
12580                                                                                        || l_supply_rec.supply_type);
12581                                 msc_sch_wb.atp_debug('ATP_Check: ' || 'Return Status l_return_status: ' || l_return_status);
12582                              END IF;
12583                         ELSE -- Create Planned Order otherwise.
12584 
12585                              MSC_ATP_DB_UTILS.Add_Planned_Order(
12586                                     p_atp_record.instance_id,
12587                                     p_plan_id,
12588                                     PF_ITEM_INFO_REC.dest_inv_item_id,
12589                                     p_atp_record.organization_id,
12590                                     l_planned_order_date, -- For ship_rec_cal
12591                                     l_PO_qty, -- 2754446
12592                                     --l_net_demand,
12593                                     null, -- l_atp_rec.supplier_id,
12594                                     null, -- l_atp_rec.supplier_site_id,
12595                                     p_atp_record.demand_class,
12596                                     -- rajjain 02/19/2003 Bug 2788302 Begin
12597                                     -- Add Sourcing details.
12598                                     l_src_org_id,
12599                                     l_atp_rec.instance_id,
12600                                     null, --process seq id (transfer case)
12601                                     -- rajjain 02/19/2003 Bug 2788302 End
12602                                     p_refresh_number,    -- For summary enhancenment
12603                                     -- ship_rec_cal changes begin
12604                                     l_atp_rec.shipping_cal_code,
12605                                     l_atp_rec.receiving_cal_code,
12606                                     l_atp_rec.intransit_cal_code,
12607                                     l_new_ship_date,
12608                                     l_new_dock_date,
12609                                     l_sugg_start_date,  -- Bug 3241766
12610                                     l_order_date,       -- Bug 3241766
12611                                     l_atp_rec.ship_method,
12612                                     -- ship_rec_cal changes end
12613                                     l_transaction_id,
12614                                     l_return_status,
12615                                     -- time_phased_atp
12616                                     l_atp_rec.delivery_lead_time, --4127630
12617                                     C_ITEM_INFO_REC.dest_inv_item_id,
12618                                     p_atp_record.atf_date
12619                                 );
12620                         END IF;
12621                         -- End ATP4drp
12622 			--END IF;
12623 
12624                         -- populate insert rec to pegging tree for this supply
12625 
12626                         IF PG_DEBUG in ('Y', 'C') THEN
12627                            msc_sch_wb.atp_debug('ATP_Check: ' || 'here the the parent_pegging_id inside the buy/transfer :'||
12628 				l_pegging_id);
12629                            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_parent_pegging_id := ' || l_parent_pegging_id);
12630                         END IF;
12631                         l_pegging_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
12632                         l_pegging_rec.order_line_id:= MSC_ATP_PVT.G_ORDER_LINE_ID;
12633                         l_pegging_rec.parent_pegging_id:= l_parent_pegging_id;
12634                         l_pegging_rec.atp_level:= p_level+1;
12635 
12636 
12637                         IF l_sources.source_type(j) = TRANSFER or l_sources.source_type(j) = MAKE THEN --5222584
12638                           l_pegging_rec.organization_id :=
12639                               l_sources.organization_id(j);
12640                           /* Modularize Item and Org Info */
12641                           IF (G_ORG_INFO_REC.organization_id =
12642                                          l_pegging_rec.organization_id) AND
12643                              (G_ORG_INFO_REC.instance_id =
12644                                             p_atp_record.instance_id) THEN
12645                             -- If org has not changed then re-use.
12646                             l_pegging_rec.organization_code :=
12647                                                    G_ORG_INFO_REC.org_code;
12648                             ---we need this organization for pegging creation
12649                             -- Org id contains source org. We need to create atp simplified peg
12650                             --- in rec org. Therefore, we remember it
12651                           ELSE
12652                              /* Modularize Item and Org Info */
12653                              MSC_ATP_PROC.get_global_org_info(p_atp_record.instance_id,
12654                                                          l_pegging_rec.organization_id);
12655                              l_pegging_rec.organization_code := MSC_ATP_PVT.G_ORG_INFO_REC.org_code;
12656                              /* l_pegging_rec.organization_code :=
12657                                MSC_ATP_FUNC.get_org_code(p_atp_record.instance_id,
12658                                            l_pegging_rec.organization_id);
12659                              Modularize Item and Org Info */
12660                           END IF;
12661                           /* Modularize Item and Org Info */
12662                           l_pegging_rec.supplier_id := NULL;
12663                           l_pegging_rec.supplier_name := NULL;
12664                           l_pegging_rec.supplier_site_id := NULL;
12665                           l_pegging_rec.supplier_site_name := NULL;
12666                           l_pegging_rec.receiving_organization_id :=  p_atp_record.organization_id;
12667                         ELSE
12668                           --- for CTO rearch: add this field so that its read for creating ATP pegging
12669                           --l_pegging_rec.organization_id := NULL;
12670                           l_pegging_rec.organization_id := p_atp_record.organization_id;
12671                           l_pegging_rec.organization_code := NULL;
12672 			  --- EFTC if l_buy_make_cd = 3 then it means that no sources are defined
12673                           --- Therefore source info is not available
12674 			  --- we set the supplier info to null
12675 			  IF (l_make_buy_cd = 3) THEN --- no sources defined
12676  				l_pegging_rec.supplier_id := NULL;
12677                           	l_pegging_rec.supplier_name := NULL;
12678                           	l_pegging_rec.supplier_site_id := NULL;
12679                           	l_pegging_rec.supplier_site_name := NULL;
12680 			  ELSE
12681                        		l_pegging_rec.supplier_id := l_sources.supplier_id(j);
12682                        		l_pegging_rec.supplier_name :=
12683                               		MSC_ATP_FUNC.get_supplier_name(p_atp_record.instance_id,
12684                                				               l_pegging_rec.supplier_id);
12685                                 l_pegging_rec.supplier_site_id :=
12686                               		l_sources.supplier_site_id(j);
12687                           	l_pegging_rec.supplier_site_name :=
12688                               		MSC_ATP_FUNC.get_supplier_site_name(p_atp_record.instance_id,
12689                                        			                    l_pegging_rec.supplier_site_id);
12690                     	  END IF;
12691                         END IF;
12692 
12693                         l_pegging_rec.identifier1:= p_atp_record.instance_id;
12694                         l_pegging_rec.identifier2:= p_plan_id;
12695                         l_pegging_rec.identifier3 := l_transaction_id;
12696 
12697                         -- time_phased_atp changes begin
12698                         IF l_pf_atp = 'Y' THEN
12699                            l_pegging_rec.inventory_item_id:= p_atp_record.inventory_item_id;
12700                            l_pegging_rec.inventory_item_name := PF_ITEM_INFO_REC.item_name;
12701                         ELSE
12702                            l_pegging_rec.inventory_item_id:= p_atp_record.request_item_id;
12703                            l_pegging_rec.inventory_item_name := C_ITEM_INFO_REC.item_name;
12704                         END IF;
12705                         l_pegging_rec.aggregate_time_fence_date := p_atp_record.atf_date;
12706                         -- time_phased_atp changes end
12707 
12708                         l_pegging_rec.resource_id := NULL;
12709                         l_pegging_rec.resource_code := NULL;
12710                         l_pegging_rec.department_id := NULL;
12711                         l_pegging_rec.department_code := NULL;
12712                         l_pegging_rec.scenario_id:= p_scenario_id;
12713                         l_pegging_rec.supply_demand_source_type:=
12714                             l_sources.source_type(j);
12715 
12716 
12717                         l_pegging_rec.supply_demand_type:= 2;
12718                         l_pegging_rec.supply_demand_date:= l_requested_ship_date;
12719                         l_pegging_rec.source_type := l_sources.source_type(j);
12720 
12721                         -- for demo:1153192
12722                         l_pegging_rec.constraint_flag := 'N';
12723 			l_pegging_rec.component_identifier :=
12724 
12725                          NVL(p_atp_record.component_identifier, MSC_ATP_PVT.G_COMP_LINE_ID);
12726                         l_pegging_rec.summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;
12727 
12728                         --- bug 2152184: For PF based ATP inventory_item_id field contains id for PF item
12729                         --- cto looks at pegging tree to place their demands. Since CTO expects to find
12730                         --  id for the requested item, we add the following column. CTO will now read from this column
12731                         l_pegging_rec.request_item_id := p_atp_record.request_item_id;
12732 
12733                         IF l_start_date < l_ptf_date then
12734                             IF PG_DEBUG in ('Y', 'C') THEN
12735                                msc_sch_wb.atp_debug('ATP_Check: ' || 'Add PTF date to pegging');
12736                             END IF;
12737                             l_pegging_rec.ptf_date := l_ptf_date;
12738                         END IF;
12739 
12740                         --diag_atp:
12741                         IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 and p_search = BACKWARD_SCHEDULING
12742                            AND l_constraint_flag = 1 THEN
12743                            l_pegging_rec.supply_demand_quantity := 0;
12744                         ELSE
12745                            l_pegging_rec.supply_demand_quantity :=l_atp_rec.quantity_ordered;
12746                         END IF;
12747                         l_pegging_rec.ptf_date := l_ptf_date;
12748                         IF l_sources.source_type(j) = TRANSFER THEN
12749                             l_pegging_rec.intransit_lead_time := l_sources.Lead_Time(j);
12750                             l_pegging_rec.postprocessing_lead_time := l_post_pro_lt;
12751                             l_pegging_rec.pegging_type := MSC_ATP_PVT.TRANSFER_SUPPLY;
12752                             l_pegging_rec.preprocessing_lead_time := l_pre_pro_lt;
12753                             -- Bug 3826234
12754                             l_pegging_rec.shipping_cal_code      :=  l_atp_rec.shipping_cal_code;
12755                             l_pegging_rec.receiving_cal_code     :=  l_atp_rec.receiving_cal_code;
12756                             l_pegging_rec.intransit_cal_code     :=  l_atp_rec.intransit_cal_code;
12757                             l_pegging_rec.manufacturing_cal_code :=  NULL;
12758 
12759                         ELSE
12760                             l_pegging_rec.intransit_lead_time := l_atp_rec.delivery_lead_time; --supplier intransit LT
12761                             l_pegging_rec.postprocessing_lead_time := l_post_pro_lt;
12762                             l_pegging_rec.preprocessing_lead_time := l_pre_pro_lt;
12763                             l_pegging_rec.processing_lead_time := l_process_lt;
12764                             l_pegging_rec.pegging_type := MSC_ATP_PVT.BUY_SUPPLY;
12765                              -- Bug 3826234
12766                             l_pegging_rec.shipping_cal_code      :=  l_atp_rec.shipping_cal_code;
12767                             l_pegging_rec.receiving_cal_code     :=  l_atp_rec.receiving_cal_code;
12768                             l_pegging_rec.intransit_cal_code     :=  l_atp_rec.intransit_cal_code;
12769                             l_pegging_rec.manufacturing_cal_code :=  l_atp_rec.manufacturing_cal_code;
12770                         END IF;
12771 
12772                         /* ship_rec_cal
12773                            populate ship_method */
12774                         IF  l_atp_rec.ship_method <> '@@@' THEN
12775                            -- if no ship method is defined then ATP_sources retuns @@@ which
12776                            --we don't want to display
12777                            l_pegging_rec.ship_method := l_atp_rec.ship_method;
12778                         ELSE
12779                            l_pegging_rec.ship_method := null;
12780                         END IF;
12781 
12782                         l_pegging_rec.plan_name := p_atp_record.plan_name;
12783                         l_pegging_rec.required_quantity := l_net_demand;
12784                         l_pegging_rec.required_date := l_requested_ship_date;
12785                         --bug 3328421
12786                         l_pegging_rec.actual_supply_demand_date := l_requested_ship_date;
12787 
12788 			-- dsting diag_atp
12789 			IF l_sources.source_type(j) = MSC_ATP_PVT.BUY
12790 			   AND l_pegging_rec.constraint_type IS NULL
12791 			THEN
12792 				l_last_good_pegging_id := l_pegging_id;
12793 				IF PG_DEBUG in ('Y', 'C') THEN
12794 				   msc_sch_wb.atp_debug('buy last good pegging: ' || l_pegging_id);
12795 				END IF;
12796 			END IF;
12797 
12798                         --s_cto_rearch
12799                             --parent is model entity
12800                         --bug 3373075: Mark plan order to be model entity only for
12801                         -- config, model or option class
12802                         /* IF (p_atp_record.parent_bom_item_type in (1, 4) and
12803                                                   p_atp_record.parent_repl_order_flag = 'Y')
12804                            --item itself is model entity
12805                            OR (C_ITEM_INFO_REC.bom_item_type in (1, 4) and
12806                                                   C_ITEM_INFO_REC.replenish_to_ord_flag = 'Y') THEN
12807                         */
12808                         IF (C_ITEM_INFO_REC.bom_item_type in (1, 2, 4) and
12809                                                   C_ITEM_INFO_REC.replenish_to_ord_flag = 'Y') THEN
12810                             l_pegging_rec.model_sd_flag := 1;
12811                         END IF;
12812 
12813                         l_pegging_rec.dest_inv_item_id := C_ITEM_INFO_REC.dest_inv_item_id;
12814 
12815                         --bug 3356779: populate atp rule and ITF on PO
12816                         IF l_atp_flag = 'Y' THEN
12817                            l_pegging_rec.infinite_time_fence := l_get_mat_out_rec.infinite_time_fence_date;
12818                            l_pegging_rec.atp_rule_name := l_get_mat_out_rec.atp_rule_name;
12819                         ELSE
12820                            --bug 4394904
12821                            --l_pegging_rec.infinite_time_fence := l_atp_date_this_level;
12822                            l_pegging_rec.infinite_time_fence := l_infinite_time_fence;
12823                            l_pegging_rec.atp_rule_name := l_atp_rule_name;
12824                         END IF;
12825                         --e_Cto_rearch
12826 
12827 			-- dsting diag_atp
12828 			IF PG_DEBUG in ('Y', 'C') THEN
12829 				msc_sch_wb.atp_debug('MSC_ATP_PVT.G_HAVE_MAKE_BUY_PARENT ' || MSC_ATP_PVT.G_HAVE_MAKE_BUY_PARENT);
12830 			END IF;
12831                         -- dsting 2819716. we always add planned orders now
12832                         -- so we should always add pegging.
12833 /*
12834 			IF l_constraint_flag <> 1
12835 			   OR NVL(MSC_ATP_PVT.G_SUBSTITUTION_FLAG, 'N') <> 'Y'
12836 			   OR MSC_ATP_PVT.G_HAVE_MAKE_BUY_PARENT = 1
12837 			THEN
12838 */                              l_pegging_rec.demand_class :=  p_atp_record.demand_class;
12839 
12840                                 --4570421
12841                                 l_pegging_rec.scaling_type                      := p_atp_record.scaling_type;
12842                                 l_pegging_rec.scale_multiple                    := p_atp_record.scale_multiple;
12843                                 l_pegging_rec.scale_rounding_variance           := p_atp_record.scale_rounding_variance;
12844                                 l_pegging_rec.rounding_direction                := p_atp_record.rounding_direction;
12845                                 l_pegging_rec.component_yield_factor            := p_atp_record.component_yield_factor; --4570421
12846                                 l_pegging_rec.usage                             := p_atp_record.usage_qty; --4775920
12847                                 l_pegging_rec.organization_type                 := p_atp_record.organization_type; --4775920
12848 
12849 	                        MSC_ATP_DB_UTILS.Add_Pegging(l_pegging_rec, l_pegging_id);
12850 --			END IF;
12851                         --optional_fw Buy/Transfer PO should be set for top level
12852                         IF PG_DEBUG in ('Y', 'C') AND G_FW_CTP_PEGGING_ID.COUNT >0 THEN
12853                            msc_sch_wb.atp_debug('ATP_Check: ' || 'G_FW_CTP_PEGGING_ID := '
12854                                                               || G_FW_CTP_PEGGING_ID(G_FW_CTP_PEGGING_ID.COUNT));
12855                            msc_sch_wb.atp_debug('ATP_Check: ' || 'G_OPTIONAL_FW := ' || G_OPTIONAL_FW);
12856                         ELSIF PG_DEBUG in ('Y', 'C') THEN
12857                            msc_sch_wb.atp_debug('ATP_Check: ' || 'G_OPTIONAL_FW := ' || G_OPTIONAL_FW);
12858                            msc_sch_wb.atp_debug('ATP_Check: ' || 'G_FW_PEGGING_ID := ' || G_FW_PEGGING_ID);
12859                         END IF;
12860                         IF MSC_ATP_PVT.G_OPTIONAL_FW is not null
12861                                                AND (G_FW_CTP_PEGGING_ID.COUNT = 0 --Means top level, for next source value = null
12862                                                     OR G_FW_CTP_PEGGING_ID(G_FW_CTP_PEGGING_ID.COUNT) is null) then
12863                          IF G_FW_CTP_PEGGING_ID.COUNT = 0 THEN
12864                           G_FW_CTP_PEGGING_ID.EXTEND;
12865                          END IF;
12866                          G_FW_CTP_PEGGING_ID(G_FW_CTP_PEGGING_ID.COUNT) := l_pegging_id;
12867                          IF PG_DEBUG in ('Y', 'C') THEN
12868                            msc_sch_wb.atp_debug('ATP_Check: ' || 'G_FW_CTP_PEGGING_ID := '
12869                                                               || G_FW_CTP_PEGGING_ID(G_FW_CTP_PEGGING_ID.COUNT));
12870                          END IF;
12871                         END IF;
12872                         IF PG_DEBUG in ('Y', 'C') AND G_FW_CTP_PEGGING_ID.COUNT >0 THEN
12873                            msc_sch_wb.atp_debug('ATP_Check: ' || 'G_FW_CTP_PEGGING_ID := '
12874                                                               || G_FW_CTP_PEGGING_ID(G_FW_CTP_PEGGING_ID.COUNT));
12875                            msc_sch_wb.atp_debug('ATP_Check: ' || 'G_OPTIONAL_FW := ' || G_OPTIONAL_FW);
12876                         ELSIF PG_DEBUG in ('Y', 'C') THEN
12877                            msc_sch_wb.atp_debug('ATP_Check: ' || 'G_OPTIONAL_FW := ' || G_OPTIONAL_FW);
12878                            msc_sch_wb.atp_debug('ATP_Check: ' || 'G_FW_PEGGING_ID := ' || G_FW_PEGGING_ID);
12879                         END IF;
12880                         --- now chnage back the summary flag for new plan
12881                         MSC_ATP_PVT.G_SUMMARY_FLAG := l_new_summary_flag;
12882                         -- 2178544:
12883                         MSC_ATP_PVT.G_PTF_DATE := GREATEST(l_ptf_date, MSC_ATP_PVT.G_PTF_DATE);
12884                         IF PG_DEBUG in ('Y', 'C') THEN
12885                            msc_sch_wb.atp_debug('ATP_Check: ' || 'G_PTF_DATE := ' || MSC_ATP_PVT.G_PTF_DATE);
12886                            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_ptf_date := ' || l_ptf_date);
12887                            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_start_date := ' || l_start_date);
12888                            msc_sch_wb.atp_debug('ATP_Check: ' || 'inside buy/transfer');
12889                         END IF;
12890 
12891                         ---subst: Since we go down so we do not remain in top org. So we set the flags again
12892                         l_atp_rec.original_item_flag := p_atp_record.original_item_flag;
12893                         l_atp_rec.top_tier_org_flag := 2;
12894 
12895                         IF p_search = BACKWARD_SCHEDULING and
12896                            --diag_atp
12897                            /* Enforce Pur LT - For BUY cases PTF date check needs to be done only if capacity is defined
12898                            (l_start_date < l_ptf_date or
12899                                       (l_sources.source_type(j) = TRANSFER AND
12900                                             l_atp_rec.requested_ship_date < sysdate) OR
12901                                       (l_sources.source_type(j) = BUY AND
12902                                             l_supplier_start_date < sysdate))  THEN
12903                            */
12904                            ((l_sources.source_type(j) = TRANSFER AND
12905                                 -- (l_atp_rec.requested_ship_date < sysdate OR l_start_date < l_ptf_date)) OR
12906                                 -- Bug 3412143 - use l_trunc_sysdate instead of accessing sysdate directly.
12907                                 (l_atp_rec.requested_ship_date < l_trunc_sysdate OR l_start_date < l_ptf_date)) OR
12908                             (l_sources.source_type(j) = BUY AND
12909                                 (l_supplier_start_date < l_trunc_sysdate OR
12910                                         (l_atp_rec.last_cap_date IS NOT NULL AND l_start_date < l_ptf_date))))  THEN
12911 
12912                            l_atp_rec.requested_date_quantity := 0;
12913                            -- dsting 2754446
12914                            l_prim_uom_req_date_qty := 0;
12915 
12916                            IF PG_DEBUG in ('Y', 'C') THEN
12917                               msc_sch_wb.atp_debug('ATP_Check: ' || 'PTF Date check failed in case of transfer');
12918                            END IF;
12919                         ELSE
12920 			   l_atp_rec.children_type := NO_MAKE_BUY_CHILDREN;
12921                            IF PG_DEBUG in ('Y', 'C') THEN
12922                               msc_sch_wb.atp_debug('NO_MAKE_BUY_CHILDREN: pre atp_check');
12923                            END IF;
12924 
12925                       l_alloc_atp := MSC_ATP_PVT.G_ALLOCATED_ATP; --ALLOC ATP CHANGES, 12973673
12926 
12927 						   MSC_ATP_PVT.ATP_Check(l_atp_rec,
12928                                   l_plan_id,
12929                                   p_level + 2,
12930                                   p_scenario_id,
12931                                   p_search,
12932                                   p_refresh_number,
12933                                   l_pegging_id,
12934 			          l_assign_set_id,
12935                                   l_tb_atp_period,
12936                                   l_tb_atp_supply_demand,
12937                                   l_return_status);
12938                                   -- krajan: 2400614
12939 						IF MSC_ATP_PVT.G_HYBRID_ALLOC_ATP = 'Y' THEN
12940 							MSC_ATP_PVT.G_ALLOCATED_ATP := l_alloc_atp;
12941 						END IF;
12942 
12943 
12944                            IF l_return_status = MSC_ATP_PVT.CTO_OSS_ERROR THEN
12945 
12946                                RAISE MSC_ATP_PVT.INVALID_OSS_SOURCE;
12947 
12948                            ELSIF l_return_status = MSC_ATP_PVT.G_ATO_SRC_MISMATCH THEN
12949                                  IF PG_DEBUG in ('Y', 'C') THEN
12950                                        msc_sch_wb.atp_debug('Error in ATP_CHECK');
12951                                     msc_sch_wb.atp_debug('ATP_Check: ' || 'Error in lower level component check');
12952                                  END IF;
12953                                  RAISE MSC_ATP_PVT.G_ATO_SOURCING_MISMATCH;
12954                               -- dsting 2764213
12955                            ELSIF l_return_status = MSC_ATP_PVT.G_NO_PLAN_FOUND THEN
12956                                     IF PG_DEBUG in ('Y', 'C') THEN
12957                                        msc_sch_wb.atp_debug('Error in ATP_CHECK');
12958                                        msc_sch_wb.atp_debug('ATP_Check: ' || 'Error in lower level component check');
12959                                     END IF;
12960                                     RAISE MSC_ATP_PVT.EXC_NO_PLAN_FOUND;
12961                               -- krajan 2752705
12962                            ELSIF l_return_status = MSC_ATP_PVT.G_ATO_UNCOLL_ITEM THEN
12963                                     IF PG_DEBUG in ('Y', 'C') THEN
12964                                        msc_sch_wb.atp_debug('Error in ATP_CHECK');
12965                                        msc_sch_wb.atp_debug('ATP_Check: ' || 'Error in lower level component check');
12966                                     END IF;
12967                                     RAISE MSC_ATP_PVT.G_EXC_UNCOLLECTED_ITEM;
12968                            END IF;
12969 
12970                            -- Bug 3633591 - Reset G_ITEM_INFO_REC as it may have changed.
12971                            G_ITEM_INFO_REC := C_ITEM_INFO_REC;
12972 
12973                            -- dsting 2754446
12974                            l_prim_uom_avail_qty := nvl(l_atp_rec.available_quantity, 0);
12975                            l_prim_uom_req_date_qty := nvl(l_atp_rec.requested_date_quantity, 0);
12976                            IF nvl(l_conversion_rate, 0) <> 0 THEN
12977                               l_prim_uom_avail_qty := l_prim_uom_avail_qty / l_conversion_rate;
12978                               l_prim_uom_req_date_qty := l_prim_uom_req_date_qty / l_conversion_rate;
12979                            END IF;
12980 
12981                            -- round to avoid any floating point errors.
12982                            IF NVL(C_ITEM_INFO_REC.rounding_control_type, 2) = 1 THEN
12983 --                              l_atp_rec.available_quantity := FLOOR(l_atp_rec.available_quantity);
12984                               l_prim_uom_avail_qty := FLOOR(l_prim_uom_avail_qty);   --5598066
12985                               l_prim_uom_req_date_qty := FLOOR(l_prim_uom_req_date_qty); --5598066
12986 			   ELSE								--5598066
12987                               l_prim_uom_avail_qty := trunc(l_prim_uom_avail_qty, 6);   --5598066
12988                               l_prim_uom_req_date_qty := trunc(l_prim_uom_req_date_qty, 6); --5598066
12989                            END IF;
12990 
12991   			   IF PG_DEBUG IN ('Y','C') THEN
12992                                 msc_sch_wb.atp_debug('prim_uom_avail_qty: ' || l_prim_uom_avail_qty);
12993                                 msc_sch_wb.atp_debug('prim_uom_req_date_qty: ' || l_prim_uom_req_date_qty);
12994                                 msc_sch_wb.atp_debug('l_conversion_rate: ' || l_conversion_rate);
12995 				msc_sch_wb.atp_debug('resetting make_buy_parent : ' || G_HAVE_MAKE_BUY_PARENT || ' -> ' || l_have_make_buy_parent);
12996                                 msc_sch_wb.atp_debug('req date qty := ' || l_atp_rec.requested_date_quantity);
12997 			   END IF;
12998 
12999 			   G_HAVE_MAKE_BUY_PARENT := l_have_make_buy_parent;
13000 			   IF (l_atp_rec.children_type = MAKE_BUY_CHILDREN ) THEN
13001 				p_atp_record.children_type := l_atp_rec.children_type;
13002 			   END IF;
13003                         END IF;
13004                         -- check the return status
13005                         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
13006                             RAISE FND_API.G_EXC_ERROR;
13007                         END IF;
13008 
13009                         IF p_search = BACKWARD_SCHEDULING THEN
13010                           -- rajjain 01/29/03 Bug 2774827
13011                           -- 2754446
13012                           l_net_demand := l_net_demand -
13013                               greatest(nvl(l_prim_uom_req_date_qty, 0), 0);
13014                             -- greatest(nvl(l_atp_rec.requested_date_quantity,0), 0);
13015                         END IF;
13016                         IF PG_DEBUG in ('Y', 'C') THEN
13017                            msc_sch_wb.atp_debug('ATP_Check: ' || 'Are we here, p_search ='||p_search);
13018                            msc_sch_wb.atp_debug('ATP_Check: l_net_demand := ' || l_net_demand);
13019                         END IF;
13020                         IF (p_search = FORWARD_SCHEDULING) THEN
13021 
13022                             -- bug 1303196, ship date need to consider
13023                             -- workday if it is from supplier
13024 
13025                             IF l_sources.source_type(j) = TRANSFER THEN
13026 
13027                                 -- 2463556 : krajan : Added comment
13028                                 IF PG_DEBUG in ('Y', 'C') THEN
13029                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'Forward Scheduling ');
13030                                 END IF;
13031 
13032           			/* ship_rec_cal changes begin */
13033 		                -- BEGIN -- removed exception handling from here so that it can be handled in the end
13034 		                        /* Bug 3241766 : Broke the single call to get intermediate dates
13035                                         l_ship_date := MSC_CALENDAR.THREE_STEP_CAL_OFFSET_DATE(
13036                                 			l_atp_rec.ship_date, l_atp_rec.shipping_cal_code, +1,
13037                                 			l_atp_rec.intransit_cal_code, nvl(l_atp_rec.delivery_lead_time, 0), +1,
13038                                 			l_atp_rec.receiving_cal_code, +1, p_atp_record.instance_id);
13039                                 	*/
13040                                 	l_ship_date := MSC_CALENDAR.NEXT_WORK_DAY(
13041                                 	                l_atp_rec.shipping_cal_code,
13042                                 	                p_atp_record.instance_id,
13043                                 	                l_atp_rec.ship_date);
13044                                 	l_new_ship_date := l_ship_date;
13045 
13046                                         IF PG_DEBUG in ('Y', 'C') THEN
13047                                            msc_sch_wb.atp_debug('ATP_Check: ' || 'Ship Date: ' || l_atp_rec.ship_date);
13048                                            msc_sch_wb.atp_debug('ATP_Check: ' || '___________________Start Offset___________________');
13049                                            msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after validating on OSC: '||l_ship_date);
13050                                         END IF;
13051 
13052                                         l_ship_date := MSC_CALENDAR.DATE_OFFSET(
13053                         				l_atp_rec.intransit_cal_code,
13054                         				p_atp_record.instance_id,
13055                         				l_ship_date,
13056                         				nvl(l_atp_rec.delivery_lead_time, 0),
13057                         				+1);
13058 
13059                                         IF PG_DEBUG in ('Y', 'C') THEN
13060                                            msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after adding intransit LT using VIC: '||l_ship_date);
13061                                         END IF;
13062 
13063                                         l_ship_date := MSC_CALENDAR.NEXT_WORK_DAY(
13064                         				l_atp_rec.receiving_cal_code,
13065                         				p_atp_record.instance_id,
13066                         				l_ship_date);
13067                                         l_new_dock_date := l_ship_date;
13068 
13069                                         IF PG_DEBUG in ('Y', 'C') THEN
13070                                            msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after validating on ORC: '||l_ship_date);
13071                                         END IF;
13072 
13073                                         -- Bug 3241766 : Back calculate start date and order date
13074                                         l_sugg_start_date := l_new_ship_date;
13075 
13076                                         l_order_date := MSC_CALENDAR.DATE_OFFSET (
13077                                                         p_atp_record.manufacturing_cal_code,
13078                                                         p_atp_record.instance_id,
13079                                                         l_sugg_start_date,
13080                                                         -- Bug 3633591 - Dont use global variable here : -1 * MSC_ATP_PVT.G_ITEM_INFO_REC.pre_pro_lt, -1);
13081                                                         -1 * l_pre_pro_lt, -1);
13082                                         --bug3821358  Moving Order date to non past due non working date on a day closer to the first working day
13083                                         IF l_order_date < l_trunc_sysdate THEN
13084                                            IF PG_DEBUG in ('Y', 'C') THEN
13085                                               msc_sch_wb.atp_debug('ATP_Check: ' || 'Back Calculated Order date is less then sysdate: '||l_order_date);
13086                                               --bug 4188385 removed this debug was giving error during concatenation
13087                                               --msc_sch_wb.atp_debug('ATP_Check: ' || 'Moving Order date to non past due non working date: '||l_sys_next_date - (1)); --Putting 1 in brackets to avoid error.
13088                                            END IF;
13089                                            l_order_date  := l_sys_next_date - 1;
13090                                            --bug 4188385 printed the order date
13091                                            IF PG_DEBUG in ('Y', 'C') THEN
13092                                                msc_sch_wb.atp_debug('ATP_Check: ' || 'Back Calculated Order date is less then sysdate: '||l_order_date);
13093                                            END IF;
13094                                         END IF;
13095                                         --bug3821358
13096                                         IF PG_DEBUG in ('Y', 'C') THEN
13097                                            msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after subtracting pre PLT using OMC : '||l_order_date);
13098                                         END IF;
13099 
13100                                         l_ship_date := MSC_CALENDAR.DATE_OFFSET (
13101                                                         p_atp_record.manufacturing_cal_code,
13102                                                         p_atp_record.instance_id,
13103                                                         l_ship_date,
13104                                                         l_post_pro_lt, 1);
13105 
13106                                         IF PG_DEBUG in ('Y', 'C') THEN
13107                                            msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after adding PPLT using OMC: '||l_ship_date);
13108                                            msc_sch_wb.atp_debug('ATP_Check: ' || '___________________End Offset___________________');
13109                                         END IF;
13110           			/*
13111           			EXCEPTION
13112                                         WHEN others THEN
13113                                                 IF PG_DEBUG in ('Y', 'C') THEN
13114                                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'Sysdate not found in Calendar');
13115                                                 END IF;
13116                                         null;
13117           			END;
13118           			*/
13119 
13120                             ELSE
13121                                 -- buy
13122                                 /* ship_rec_cal changes begin */
13123 
13124                                 IF PG_DEBUG in ('Y', 'C') THEN
13125                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'Ship Date: ' || l_atp_rec.ship_date);
13126                                    msc_sch_wb.atp_debug('ATP_Check: ' || '___________________Start Offset___________________');
13127                                 END IF;
13128                                 -- intransit lead time offset in case of capacity to ship
13129                                 --Bug 3821358
13130                                 IF (l_atp_rec.ship_date = l_store_g_ptf_date )  THEN
13131                                    IF PG_DEBUG in ('Y', 'C') THEN
13132                                       msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atp_rec.ship_date' || l_atp_rec.ship_date);
13133                                       msc_sch_wb.atp_debug('ATP_Check: ' || 'l_store_g_ptf_date' || l_store_g_ptf_date);
13134                                       msc_sch_wb.atp_debug('ATP_Check: ' || 'l_store_dock_date' || l_store_dock_date);
13135                                       msc_sch_wb.atp_debug('ATP_Check: ' || 'l_store_ship_date' || l_store_ship_date);
13136                                       msc_sch_wb.atp_debug('ATP_Check: ' || 'l_store_start_date' || l_store_start_date);
13137                                       msc_sch_wb.atp_debug('ATP_Check: ' || 'l_store_order_date' || l_store_order_date);
13138                                       msc_sch_wb.atp_debug('ATP_Check: ' || 'l_store_due_date' || l_store_due_date);
13139                                    END IF;
13140                                    -- assign the values in l_store* variables..
13141                                    l_ship_date := l_store_due_date;
13142                                    l_new_dock_date := l_store_dock_date;
13143                                    l_new_ship_date := l_store_ship_date;
13144                                    l_sugg_start_date := l_store_start_date;
13145                                    l_order_date := l_store_order_date;
13146 
13147                                 ELSE
13148 
13149                                    IF l_sources.sup_cap_type(j) = 1 THEN
13150 
13151                                            l_ship_date:= MSC_CALENDAR.NEXT_WORK_DAY(
13152                                                                 l_atp_rec.shipping_cal_code,
13153                                                                 l_atp_rec.instance_id,
13154                                                                 l_atp_rec.ship_date);
13155 
13156                                            IF PG_DEBUG in ('Y', 'C') THEN
13157                                                msc_sch_wb.atp_debug('ATP_Check: ' || '--- Capacity to Ship ---');
13158                                                msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after validating on SSC: '||l_ship_date);
13159                                            END IF;
13160 
13161                                            l_new_ship_date := l_ship_date;      -- Bug 3241766
13162 
13163                                            l_ship_date := MSC_CALENDAR.DATE_OFFSET(
13164                                                                 l_atp_rec.intransit_cal_code,
13165                                                                 l_atp_rec.instance_id,
13166                                                                 l_ship_date,
13167                                                                 l_atp_rec.delivery_lead_time, 1);
13168 
13169                                            IF PG_DEBUG in ('Y', 'C') THEN
13170                                                msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after adding intransit LT using VIC: '||l_ship_date);
13171                                            END IF;
13172                                    ELSE
13173 
13174                                            l_ship_date := l_atp_rec.ship_date;
13175 
13176                                            IF PG_DEBUG in ('Y', 'C') THEN
13177                                                msc_sch_wb.atp_debug('ATP_Check: ' || '--- Capacity to Dock ---');
13178                                            END IF;
13179 
13180                                            -- Bug 3241766 : back calculate the ship date
13181                                            l_new_ship_date := MSC_CALENDAR.DATE_OFFSET(
13182                                                                 l_atp_rec.intransit_cal_code,
13183                                                                 l_atp_rec.instance_id,
13184                                                                 l_ship_date,
13185                                                                 -1 * l_atp_rec.delivery_lead_time, -1);
13186 
13187                                            IF PG_DEBUG in ('Y', 'C') THEN
13188                                                msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after adding intransit LT using VIC: '||l_new_ship_date);
13189                                            END IF;
13190 
13191                                            l_new_ship_date := MSC_CALENDAR.PREV_WORK_DAY(
13192                                                                 l_atp_rec.shipping_cal_code,
13193                                                                 l_atp_rec.instance_id,
13194                                                                 l_new_ship_date);
13195                                                                 --Bug 3821358
13196                                                                 --l_atp_rec.ship_date);
13197 
13198                                            IF PG_DEBUG in ('Y', 'C') THEN
13199                                                msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after validating on SSC: '||l_new_ship_date);
13200                                            END IF;
13201                                    END IF;
13202 
13203                                    l_ship_date  := MSC_CALENDAR.NEXT_WORK_DAY(
13204                                                         l_atp_rec.receiving_cal_code,
13205                                                         l_atp_rec.instance_id,
13206                                                         l_ship_date );
13207 
13208                                    l_new_dock_date := l_ship_date;      -- Bug 3241766
13209 
13210                                    IF PG_DEBUG in ('Y', 'C') THEN
13211                                        msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after validating on ORC: '||l_ship_date);
13212                                    END IF;
13213 
13214                                    -- Bug 3241766 : Backcalculate start date and order date
13215                                    -- Bug 3405708 - Enforce Pur LT: Truncate past due dates
13216                                    IF G_PLAN_INFO_REC.enforce_pur_lead_time = 2 AND l_new_ship_date <= l_trunc_sysdate THEN
13217                                         l_sugg_start_date := l_trunc_sysdate;
13218                                    ELSE
13219                                         IF (l_sources.sup_cap_type(j) = 1) THEN --Bug 3821358
13220                                            l_sugg_start_date := MSC_CALENDAR.DATE_OFFSET(
13221                                                                 l_atp_rec.manufacturing_cal_code,
13222                                                                 l_atp_rec.instance_id,
13223                                                                 l_new_ship_date,
13224                                                                 (-1 * l_process_lt), -1);
13225                                         ELSE
13226                                            l_sugg_start_date := MSC_CALENDAR.DATE_OFFSET(
13227                                                                 l_atp_rec.manufacturing_cal_code,
13228                                                                 l_atp_rec.instance_id,
13229                                                                 l_new_dock_date,
13230                                                                 (-1 * l_process_lt), -1);
13231                                         END IF; --Bug 3821358
13232                                         IF G_PLAN_INFO_REC.enforce_pur_lead_time = 2 AND l_sugg_start_date <= l_trunc_sysdate THEN
13233                                             l_sugg_start_date := l_trunc_sysdate;
13234                                         END IF;
13235                                    END IF;
13236 
13237                                    IF PG_DEBUG in ('Y', 'C') THEN
13238                                         msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after subtracting PLT using SMC : '||l_sugg_start_date);
13239                                    END IF;
13240 
13241                                    IF G_PLAN_INFO_REC.enforce_pur_lead_time = 2 AND l_sugg_start_date <= l_trunc_sysdate THEN
13242                                         l_order_date := l_trunc_sysdate;
13243                                    ELSE
13244                                         l_order_date := MSC_CALENDAR.DATE_OFFSET(
13245                                                         p_atp_record.manufacturing_cal_code, -- use OMC
13246                                                         l_atp_rec.instance_id,
13247                                                         l_sugg_start_date ,
13248                                                         (-1 * l_pre_pro_lt), -1);
13249                                         IF G_PLAN_INFO_REC.enforce_pur_lead_time = 2 AND l_order_date <= l_trunc_sysdate THEN
13250                                             l_order_date := l_trunc_sysdate;
13251                                         END IF;
13252                                    END IF;
13253                                    -- Bug 3405708 - changes end
13254 
13255                                    IF PG_DEBUG in ('Y', 'C') THEN
13256                                         msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after subtracting pre PLT using OMC : '||l_order_date);
13257                                    END IF;
13258 
13259                                    l_ship_date := MSC_CALENDAR.DATE_OFFSET(
13260                                                          p_atp_record.manufacturing_cal_code,
13261                                                          l_atp_rec.instance_id,
13262                                                          l_ship_date,
13263                                                          l_post_pro_lt, 1);
13264 
13265                                    IF PG_DEBUG in ('Y', 'C') THEN
13266                                        msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after adding PPLT using OMC: '||l_ship_date);
13267                                        msc_sch_wb.atp_debug('ATP_Check: ' || '___________________End Offset___________________');
13268                                    END IF;
13269                                    -- ship_rec_cal changes end
13270                                 END IF; --Bug 3821358
13271                             END IF;
13272                             IF PG_DEBUG in ('Y', 'C') THEN
13273                                msc_sch_wb.atp_debug ('ATP_Check: ' || 'ship date is '||l_ship_date);
13274                                msc_sch_wb.atp_debug ('ATP_Check: ' || 'original is '|| p_atp_record.ship_date );
13275                             END IF;
13276 
13277                              ---bug 2798667: The p_atp_record.ship_date for option item contains ship date for
13278                             --- model from earlier source + atp lead time. We should compare with ship date +
13279                             --- atp lead time from this source and not just ship date
13280                             /* s_cto_rearch: Do not honot ATP lead time
13281                             IF NVL(p_atp_record.atp_lead_time, 0) > 0 THEN
13282                                   l_atp_lt_offset_ship_date :=
13283                                      MSC_CALENDAR.DATE_OFFSET(
13284                                      p_atp_record.organization_id,
13285                                      p_atp_record.instance_id,
13286                                      1,
13287                                      l_ship_date,
13288                                      NVL(p_atp_record.atp_lead_time, 0));
13289                             ELSE
13290                                  l_atp_lt_offset_ship_date := l_ship_date;
13291                             END IF;
13292                             */
13293                             l_atp_lt_offset_ship_date := l_ship_date;
13294 
13295                             IF PG_DEBUG in ('Y', 'C') THEN
13296                                msc_sch_wb.atp_debug ('l_atp_lt_offset_ship_date := ' || l_atp_lt_offset_ship_date);
13297                             END IF;
13298 
13299 
13300 			    -- Bug 1566260, If p_atp_record.ship_date and l_ship_date is null, we assume
13301 			    -- that going down is better. This is being done for preserving exploded
13302                             -- pegging tree for use by CTO for storing component demands.
13303                             IF PG_DEBUG in ('Y', 'C') THEN
13304                                msc_sch_wb.atp_debug('ATP_Check: ' || 'p_atp_record.ship_date := ' || p_atp_record.ship_date);
13305                             END IF;
13306                             --bug 2798667: Compare with l_atp_lt_offset_ship_date
13307                             /*IF ((p_atp_record.ship_date IS NULL and l_ship_date IS NULL) OR
13308                                (p_atp_record.ship_date IS NOT NULL and (p_atp_record.ship_date <=
13309                                NVL(l_ship_date, p_atp_record.ship_date )))) THEN
13310                             */
13311                             IF ((p_atp_record.ship_date IS NULL and l_atp_lt_offset_ship_date IS NULL) OR
13312                                (p_atp_record.ship_date IS NOT NULL and (p_atp_record.ship_date <=
13313                                NVL(l_atp_lt_offset_ship_date, p_atp_record.ship_date )))) THEN
13314 
13315 
13316                                 -- single level result is better than
13317                                 -- going down to next level.
13318 
13319                                 IF PG_DEBUG in ('Y', 'C') THEN
13320                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'single level results is better than multi-level');
13321                                 END IF;
13322 
13323 				-- dsting ATO 2465370
13324 --				IF NVL(p_atp_record.override_flag, 'N') = 'N'
13325 			        IF NVL(G_LINE_OVERRIDE_FLAG, 'N') = 'N'
13326 				   --OR (NVL(C_ITEM_INFO_REC.cto_source_org_id, -1) <> p_atp_record.organization_id
13327 				   --AND p_parent_pegging_id is not null)
13328 				THEN
13329                                         --bug 3328421: Update actual_supply_demand_date column
13330 					--update mrp_atp_details_temp set required_date =
13331 					update mrp_atp_details_temp set actual_supply_demand_date =
13332 						TRUNC(p_atp_record.ship_date) + MSC_ATP_PVT.G_END_OF_DAY
13333 					 where session_id = MSC_ATP_PVT.G_SESSION_ID
13334 					   and pegging_id = l_parent_pegging_id
13335 					   and record_type = 3;
13336 
13337 					IF PG_DEBUG in ('Y', 'C') THEN
13338 						msc_sch_wb.atp_debug('ATO update details_temp date single lev: ' || p_atp_record.ship_date);
13339 						msc_sch_wb.atp_debug('pegging id: ' || l_parent_pegging_id);
13340 					END IF;
13341 				END IF;
13342 
13343                                 MSC_ATP_DB_UTILS.Update_Planned_Order(l_pegging_id,
13344                                                  p_plan_id,
13345                                                  p_atp_record.ship_date,
13346                                                  0,
13347                                                  l_atp_rec.supplier_id,
13348                                                  l_atp_rec.supplier_site_id,
13349                                                  null,
13350                                                  null,  -- Bug 3241766
13351                                                  null,  -- Bug 3241766
13352                                                  null,  -- Bug 3241766
13353                                                  C_ITEM_INFO_REC.dest_inv_item_id,   -- Bug 3293163
13354                                                  PF_ITEM_INFO_REC.dest_inv_item_id); -- Bug 3293163
13355 
13356 				IF PG_DEBUG in ('Y', 'C') THEN
13357 				   msc_sch_wb.atp_debug('ATP_Check: ' || 'after copying the data');
13358                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'x_atp_supply_demand.level.count = '||
13359 						x_atp_supply_demand.level.count);
13360                                 END IF;
13361 
13362                             ELSE
13363  				-- going down is better than the results
13364                                 -- in this level
13365                                 IF PG_DEBUG in ('Y', 'C') THEN
13366                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'go down is better');
13367                                 END IF;
13368 
13369 				-- dsting ATO 2465370
13370 --				IF NVL(p_atp_record.override_flag, 'N') = 'N'
13371 			        IF NVL(G_LINE_OVERRIDE_FLAG, 'N') = 'N'
13372 				   --OR (NVL(C_ITEM_INFO_REC.cto_source_org_id, -1) <> p_atp_record.organization_id
13373 				   --AND p_parent_pegging_id is not null)
13374 				THEN
13375                                         --bug 3328421: update actual suply demand date
13376 					--update mrp_atp_details_temp set required_date =
13377 					update mrp_atp_details_temp set actual_supply_demand_date =
13378 						TRUNC(l_ship_date) + MSC_ATP_PVT.G_END_OF_DAY
13379 					where session_id = MSC_ATP_PVT.G_SESSION_ID
13380 					   and pegging_id = l_parent_pegging_id
13381 					   and record_type = 3;
13382 
13383 					IF PG_DEBUG in ('Y', 'C') THEN
13384 						msc_sch_wb.atp_debug('ATO update details_temp date go down: ' || l_ship_date);
13385 						msc_sch_wb.atp_debug('pegging id: ' || l_parent_pegging_id);
13386 					END IF;
13387 				END IF;
13388 
13389                                 -- remove what's bad
13390                                 --subst: we remember this pegging id so that when we go back in
13391                                 -- ATP_CHECK_SUBST we can remember to remove this pegging if need to be
13392                                 MSC_ATP_PVT.G_FUTURE_PEGGING_ID := l_pegging_id;
13393                                 -- remove what's bad
13394 
13395                                 IF PG_DEBUG in ('Y', 'C') THEN
13396                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atp_pegging_tab : ' || l_atp_pegging_tab.COUNT);
13397                                 END IF;
13398 
13399 				IF l_atp_pegging_tab.COUNT > 0 THEN
13400 
13401 				   -- Planned Pegging based AATP, l_atp_pegging_tab populated in
13402 				   -- MSC_AATP_REQ.Get_Forward_Material_Atp
13403 
13404 				   MSC_ATP_DB_UTILS.Remove_Invalid_Future_SD(l_atp_pegging_tab);
13405 				   l_atp_pegging_tab := MRP_ATP_PUB.Number_Arr(); --bug3581151
13406 
13407 				ELSE 		-- IF l_atp_pegging_tab.COUNT > 0 THEN
13408                                    MSC_ATP_DB_UTILS.Update_Planned_Order(l_future_pegging_id,
13409                                                  p_plan_id,
13410                                                  p_atp_record.ship_date,
13411                                                  0,
13412                                                  l_atp_rec.supplier_id,
13413                                                  l_atp_rec.supplier_site_id,
13414                                                  null,
13415                                                  null,  -- Bug 3241766
13416                                                  null,  -- Bug 3241766
13417                                                  null,  -- Bug 3241766
13418                                                  C_ITEM_INFO_REC.dest_inv_item_id,   -- Bug 3293163
13419                                                  PF_ITEM_INFO_REC.dest_inv_item_id); -- Bug 3293163
13420 				END IF;		-- IF l_atp_pegging_tab.COUNT > 0 THEN
13421 
13422                                 --bug 2798667: we have already calculated the final date
13423                                 /*p_atp_record.ship_date :=
13424                                      MSC_CALENDAR.DATE_OFFSET(
13425                                      p_atp_record.organization_id,
13426                                      p_atp_record.instance_id,
13427                                      1,
13428                                      l_ship_date,
13429                                      NVL(p_atp_record.atp_lead_time, 0));
13430                                 */
13431                                 p_atp_record.ship_date := l_atp_lt_offset_ship_date;
13432 
13433 
13434                                 -- we will say the available quantity is
13435                                 -- exact the quantity we want to order.
13436                                 -- although this is not correct (
13437                                 -- it should be the quantity we get from next
13438                                 -- level plus what we can have on the parent
13439                                 -- level.  But now we don't want to search
13440 
13441                                 p_atp_record.available_quantity := p_atp_record.quantity_ordered;
13442 
13443                                 IF p_atp_record.ship_date <= p_atp_record.atf_date THEN
13444                                     p_atp_record.atf_date_quantity := p_atp_record.quantity_ordered;
13445                                 ELSE
13446                                     p_atp_record.atf_date_quantity := 0;
13447                                 END IF;
13448 
13449 				IF PG_DEBUG in ('Y', 'C') THEN
13450 				   msc_sch_wb.atp_debug('ATP_Check: ' || 'question: l_atp_rec.quantity_ordered = '||
13451 					l_atp_rec.quantity_ordered);
13452 				   msc_sch_wb.atp_debug('ATP_Check: ' || 'question: l_atp_rec.available_quantity = '||
13453 					l_atp_rec.available_quantity);
13454 				   msc_sch_wb.atp_debug('ATP_Check: ' || 'question: p_atp_record.available_quantity = '||
13455 					p_atp_record.available_quantity);
13456 			           msc_sch_wb.atp_debug('ATP_Check: ' || 'question: p_atp_record.used_available_quantity = '||
13457 					p_atp_record.used_available_quantity); --bug3409973
13458 
13459                                    -- 2754446
13460                                    msc_sch_wb.atp_debug('l_prim_uom_avail_qty: ' ||l_prim_uom_avail_qty);
13461                                    msc_sch_wb.atp_debug('l_PO_qty: ' ||l_PO_qty);
13462                                    msc_sch_wb.atp_debug('l_conversion_rate:' || l_conversion_rate);
13463 				END IF;
13464 
13465                                 MSC_ATP_DB_UTILS.Update_Planned_Order(l_pegging_id,
13466                                             p_plan_id,
13467                                             --bug 2798667: The ATO components are sourced on l_ship_date
13468                                             --p_atp_record.ship_date,
13469                                             l_ship_date,
13470                                             -- dsting 2754446
13471 --                                            least(l_atp_rec.available_quantity, l_atp_rec.quantity_ordered),
13472                                             least(l_prim_uom_avail_qty, l_PO_qty),
13473                                             l_atp_rec.supplier_id,
13474                                             l_atp_rec.supplier_site_id,
13475                                             l_new_dock_date,    -- Bug 3241766
13476                                             l_new_ship_date,    -- Bug 3241766
13477                                             l_sugg_start_date,  -- Bug 3241766
13478                                             l_order_date,       -- Bug 3241766
13479                                             C_ITEM_INFO_REC.dest_inv_item_id,   -- Bug 3293163
13480                                             PF_ITEM_INFO_REC.dest_inv_item_id,  -- Bug 3293163
13481                                             MSC_ATP_PVT.UNDO, -- dsting 2754446
13482                                             l_conversion_rate -- dsting 2754446
13483                                 );  -- dock date
13484 
13485                                 -- move the supply/demand, period atp
13486                                 -- of the transfer/buy to the output record of
13487                                 -- tables.
13488 
13489                                 l_future_pegging_id := l_pegging_id;
13490                                 l_future_atp_period := l_tb_atp_period;
13491                                 l_future_atp_supply_demand := l_tb_atp_supply_demand;
13492 
13493                             END IF;  -- which level is better
13494 
13495                         ELSE
13496 
13497                              -- we are in backward part
13498                              IF PG_DEBUG in ('Y', 'C') THEN
13499                                 msc_sch_wb.atp_debug('ATP_Check: ' || 'Are we here? we should adjust the planned order');
13500                              END IF;
13501 
13502                              --5183022 further iterations error code should be set 53
13503 	                     IF MSC_ATP_PVT.G_OPTIONAL_FW is not null THEN
13504                                 p_atp_record.error_code := ATP_REQ_DATE_FAIL;
13505                              END IF;
13506                              -- cchen: bug 1121227
13507                              -- the quantity we want to adjust should not
13508                              -- less than 0.  add greatest to
13509                              -- l_atp_rec.requested_date_quantity
13510 
13511 			     IF( l_atp_rec.children_type <> MAKE_BUY_CHILDREN and
13512 				 l_have_make_buy_parent = 0 AND
13513 				 MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 and
13514 				 MSC_ATP_PVT.G_SUBSTITUTION_FLAG = 'Y' )
13515 			     THEN
13516 				l_update_PO_mode := MSC_ATP_PVT.INVALID;
13517 			     ELSE
13518 				l_update_PO_mode := MSC_ATP_PVT.UNDO;
13519 			     END IF;
13520 			     IF PG_DEBUG in ('Y', 'C') THEN
13521 			        msc_sch_wb.atp_debug('ATP_Check: ' || 'set l_update_PO_mode ' || l_update_PO_mode);
13522 			        msc_sch_wb.atp_debug('ATP_Check: ' || 'make_buy_children ' || l_atp_rec.children_type);
13523 			        msc_sch_wb.atp_debug('ATP_Check: ' || 'make_buy_parent ' || l_have_make_buy_parent);
13524 				msc_sch_wb.atp_debug('last good pegging ' || l_last_good_pegging_id);
13525 				msc_sch_wb.atp_debug('constraint_flag: ' || l_constraint_flag );
13526 			     END IF;
13527 
13528 			     -- dsting diag_atp if we still have sources or diagnostic atp is off
13529 			     -- then we update the planned order. If l_update_PO_mode is INVALID
13530 			     -- then we remove the subtree. Otherwise, diagnostic atp is on and
13531 			     -- this is the last source, so we leave the planned order as is to
13532 			     -- balance the parent demand and only update the pegging
13533                              -- dsting 2819716. we always add planned orders now
13534                              -- so we should always update them too.
13535 --			     IF l_constraint_flag <> 1 THEN
13536 			        IF l_update_PO_mode = MSC_ATP_PVT.INVALID OR
13537 				   l_sources.source_type.next(j) is not NULL OR
13538 				   MSC_ATP_PVT.G_DIAGNOSTIC_ATP <> 1
13539 			        THEN
13540 
13541                                    IF PG_DEBUG in ('Y', 'C') THEN
13542                                       msc_sch_wb.atp_debug('l_PO_qty: ' || l_PO_qty);
13543                                       msc_sch_wb.atp_debug('l_prim_uom_req_date_qty: ' || l_prim_uom_req_date_qty);
13544                                    END IF;
13545 
13546                                    MSC_ATP_DB_UTILS.Update_Planned_Order(l_pegging_id,
13547 	                             p_plan_id,
13548 	                             null,
13549                                      -- dsting 2754446
13550 --	                             least(greatest(l_atp_rec.requested_date_quantity, 0),
13551 --	                             l_atp_rec.quantity_ordered),
13552                                      least(greatest(l_prim_uom_req_date_qty, 0),
13553                                            l_PO_qty),
13554 	                             l_atp_rec.supplier_id,
13555 	                             l_atp_rec.supplier_site_id,
13556 	                             null, -- l_atp_rec.requested_ship_date, no need to update -- Bug 3241766
13557 	                             null, -- Bug 3241766
13558 	                             null, -- Bug 3241766
13559 	                             null, -- Bug 3241766
13560                                      C_ITEM_INFO_REC.dest_inv_item_id,   -- Bug 3293163
13561                                      PF_ITEM_INFO_REC.dest_inv_item_id,  -- Bug 3293163
13562 				     l_update_PO_mode,
13563                                      l_conversion_rate
13564                                   );  -- dock date
13565 
13566 				IF l_update_PO_mode = MSC_ATP_PVT.UNDO THEN
13567 				     l_last_good_pegging_id := l_pegging_id;
13568 				END IF;
13569 
13570 				IF l_requested_ship_date <= p_atp_record.atf_date THEN
13571 				    p_atp_record.atf_date_quantity := p_atp_record.atf_date_quantity +
13572 				                                            least(greatest(l_prim_uom_req_date_qty, 0),
13573 				                                                  l_PO_qty);
13574                                 END IF;
13575 /*			     ELSE
13576 
13577 				l_last_good_pegging_id := l_pegging_id;
13578 				MSC_ATP_DB_UTILS.Update_Pegging(l_pegging_id, null,
13579 					least(greatest(l_atp_rec.requested_date_quantity, 0)));
13580 			     END IF;
13581 */
13582                            --5230396, updating planned order for diag ATP too in Buy case.
13583                            ELSIF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 THEN
13584                                  IF PG_DEBUG in ('Y', 'C') THEN
13585                                       msc_sch_wb.atp_debug('l_PO_qty: ' || l_PO_qty);
13586                                       msc_sch_wb.atp_debug('l_prim_uom_req_date_qty: ' || l_prim_uom_req_date_qty);
13587                                    END IF;
13588 
13589                                    MSC_ATP_DB_UTILS.Update_Planned_Order(l_pegging_id,
13590 	                             p_plan_id,
13591 	                             null,
13592                                      -- dsting 2754446
13593 --	                             least(greatest(l_atp_rec.requested_date_quantity, 0),
13594 --	                             l_atp_rec.quantity_ordered),
13595                                      --least(greatest(l_prim_uom_req_date_qty, 0),
13596                                      --     l_PO_qty),
13597                                      --NVL(l_PO_qty,0),
13598                                      NULL,
13599 	                             l_atp_rec.supplier_id,
13600 	                             l_atp_rec.supplier_site_id,
13601 	                             null, -- l_atp_rec.requested_ship_date, no need to update -- Bug 3241766
13602 	                             null, -- Bug 3241766
13603 	                             null, -- Bug 3241766
13604 	                             null, -- Bug 3241766
13605                                      C_ITEM_INFO_REC.dest_inv_item_id,   -- Bug 3293163
13606                                      PF_ITEM_INFO_REC.dest_inv_item_id,  -- Bug 3293163
13607 				     l_update_PO_mode,
13608                                      l_conversion_rate
13609                                   );  -- dock date
13610                         END IF;
13611 
13612 /*
13613                              -- 1228607: since we add this planned order,
13614                              -- we need to work on the demand record for this too.
13615                              update msc_demands
13616                              set    USING_REQUIREMENT_QUANTITY =
13617                                    (USING_REQUIREMENT_QUANTITY -
13618                                    LEAST(l_net_demand, 0))
13619                              where  demand_id = l_demand_id
13620                              and    plan_id = p_plan_id;
13621 */
13622                              -- cchen :1064647
13623                              -- 2754446
13624                              IF least(l_prim_uom_req_date_qty, l_PO_qty) > 0 THEN
13625                              -- IF least(l_atp_rec.requested_date_quantity,
13626                              --   l_atp_rec.quantity_ordered) > 0 THEN
13627                                 MSC_ATP_PROC.Details_Output(l_tb_atp_period,
13628                                 l_tb_atp_supply_demand,
13629                                 x_atp_period,
13630                                 x_atp_supply_demand,
13631                                 l_return_status);
13632                              END IF;
13633 
13634                         END IF; -- forward scheduling
13635 
13636                         IF l_net_demand <= 0 THEN
13637                            l_net_demand := 0;
13638                            EXIT;
13639                         END IF;
13640 
13641                         END IF; -- for the plan
13642                     ELSE  -- it is a make
13643 
13644                         --s_cto_rearch
13645                         IF PG_DEBUG in ('Y', 'C') THEN
13646                            msc_sch_wb.atp_debug('It is a make case ');
13647                            msc_sch_wb.atp_debug('G_MODEL_BOM_IS_COLLECTED := ' || MSC_ATP_CTO.G_MODEL_BOM_IS_COLLECTED);
13648                            msc_sch_wb.atp_debug('Bom Item Flag := ' || PF_ITEM_INFO_REC.bom_item_type);
13649                         END IF;
13650                         l_get_comp_info_rec.line_id := p_atp_record.demand_source_line;
13651                         l_get_comp_info_rec.bom_item_type := PF_ITEM_INFO_REC.Bom_item_type;
13652                         l_get_comp_info_rec.atp_flag := l_atp_flag;
13653                         l_get_comp_info_rec.atp_comp_flag := l_atp_comp_flag;
13654                         l_get_comp_info_rec.fixed_lt  := PF_ITEM_INFO_REC.fixed_lt;
13655                         l_get_comp_info_rec.variable_lt := PF_ITEM_INFO_REC.variable_lt;
13656                         l_get_comp_info_rec.replenish_to_order_flag  := PF_ITEM_INFO_REC.replenish_to_ord_flag;
13657                         l_get_comp_info_rec.check_model_capacity_flag := l_check_model_capacity;
13658                         l_get_comp_info_rec.model_sr_inv_item_id := l_sr_inv_item_id;
13659                         --bug 3059305: Pass ship date from last source as the cutoff date
13660                         l_get_comp_info_rec.ship_date_this_level := p_atp_record.ship_date;
13661 
13662 						IF MSC_ATP_PVT.G_INV_CTP = 4 THEN
13663                             --4501944, call get_pf_plan_info in make cases too.
13664                             MSC_ATP_PVT.G_PLAN_INFO_REC.plan_id := NULL; --NULL the plan_id in global array so that get_plan_info gets called properly
13665                             MSC_ATP_PF.Get_PF_Plan_Info(
13666                                            p_atp_record.instance_id,
13667                                            p_atp_record.request_item_id,
13668                                            p_atp_record.inventory_item_id,
13669                                            p_atp_record.organization_id,
13670                                            p_atp_record.demand_class,
13671                                            p_atp_record.atf_date,
13672                                            p_atp_record.error_code,
13673                                            l_return_status,
13674                                            p_plan_id );
13675 
13676                             IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
13677                                IF PG_DEBUG in ('Y', 'C') THEN
13678                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'ATP Downtime');
13679                                END IF;
13680                                x_return_status := FND_API.G_RET_STS_ERROR;
13681                                RAISE FND_API.G_EXC_ERROR;
13682                             END IF;
13683                             --4501944
13684                         END IF;
13685 
13686                         IF PF_ITEM_INFO_REC.bom_item_type =1 and PF_ITEM_INFO_REC.replenish_to_ord_flag = 'Y' THEN
13687 
13688                            --pass line info for model as model might be getting sourced from another org
13689                            l_get_comp_info_rec.replenish_to_order_flag  := PF_ITEM_INFO_REC.replenish_to_ord_flag;
13690                            l_get_comp_info_rec.top_model_line_id := p_atp_record.top_model_line_id;
13691                            l_get_comp_info_rec.ato_model_line_id := p_atp_record.ato_model_line_id;
13692                            l_get_comp_info_rec.ato_parent_model_line_id := p_atp_record.ato_parent_model_line_id;
13693                            l_get_comp_info_rec.parent_line_id := p_atp_record.parent_line_id;
13694                            l_get_comp_info_rec.parent_so_quantity := p_atp_record.parent_so_quantity;
13695 
13696                            /* -- we also need to get model bom if it has not been collected
13697                            IF NVL(MSC_ATP_CTO.G_MODEL_BOM_IS_COLLECTED, 2) = 2 THEN
13698 
13699                                 MSC_ATP_CTO.Populate_CTO_Bom(MSC_ATP_PVT.G_SESSION_ID, null);
13700                                 MSC_ATP_CTO.G_MODEL_BOM_IS_COLLECTED := 1;
13701 
13702                            END IF;
13703                            */
13704                         ELSIF PF_ITEM_INFO_REC.bom_item_type = 4 and PF_ITEM_INFO_REC.replenish_to_ord_flag = 'Y' THEN
13705                            -- We are deleting stealing records using ato_model_lien_id
13706                            -- Therefore , we need to pass ato model line id so that
13707                            --stealing records could be adjusted correctly.
13708                            l_get_comp_info_rec.ato_model_line_id := p_atp_record.ato_model_line_id;
13709 
13710                         END IF;
13711                         --e_cto_rearch
13712 
13713                         l_sched_status := TRUE;
13714 
13715                         -- dsting 2754446 adjust l_net_demand according to rounding_control_type
13716                         -- so our planned orders turn out right.
13717                         --bug3397904 start
13718                         IF NVL(C_ITEM_INFO_REC.rounding_control_type, 2) = 1 THEN
13719                            --l_PO_qty := CEIL(l_net_demand);
13720                            --bug 3904063: Reset the quantity only in case of demand priorit allocated ATP
13721                            IF (p_search = FORWARD_SCHEDULING)
13722                                and MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y'
13723                                AND MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1
13724                                AND MSC_ATP_PVT.G_ALLOCATION_METHOD = 1 THEN  --bug3397904
13725                                l_PO_qty := CEIL(p_atp_record.quantity_ordered);
13726                                IF PG_DEBUG in ('Y', 'C') THEN
13727                                 msc_sch_wb.atp_debug('If l_PO_qty: ' || l_PO_qty);
13728                                END IF;
13729                            ELSE
13730                                l_PO_qty := CEIL(l_net_demand);
13731                                IF PG_DEBUG in ('Y', 'C') THEN
13732                                 msc_sch_wb.atp_debug('Else l_PO_qty: ' || l_PO_qty);
13733                                END IF;
13734                            END IF;
13735                         ELSE
13736                            --bug 3904063: Reset the quantity only in case of demand priorit allocated ATP
13737                            IF (p_search = FORWARD_SCHEDULING)
13738                                and MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y'
13739                                AND MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1
13740                                AND MSC_ATP_PVT.G_ALLOCATION_METHOD = 1  THEN  --bug3397904
13741 
13742                                l_PO_qty := trunc(p_atp_record.quantity_ordered, 6);   --5598066
13743                                IF PG_DEBUG in ('Y', 'C') THEN
13744                                 msc_sch_wb.atp_debug('If l_PO_qty: ' || l_PO_qty);
13745                                END IF;
13746                            ELSE
13747                                l_PO_qty := trunc(l_net_demand, 6);	--5598066
13748                                IF PG_DEBUG in ('Y', 'C') THEN
13749                                 msc_sch_wb.atp_debug('Else l_PO_qty: ' || l_PO_qty);
13750                                END IF;
13751                            END IF;
13752                         END IF;
13753                         --bug3397904 end
13754                         -- dsting 2764213
13755                         l_planned_sources := 1;
13756 
13757                         IF PG_DEBUG in ('Y', 'C') THEN
13758                            msc_sch_wb.atp_debug('ATP_Check: ' || 'inside make');
13759                         END IF;
13760                         -- insert a plan order for the l_net_demand
13761                         --- bug 1941489
13762                         --- assign correct pegging-id to co-product pegging id
13763                         --- this pegging id will be used to link the coproduct supply to correct
13764                         ---parent
13765                         --5216821 commenting and setting it down
13766                         /*
13767                         l_coprod_pegging_id := l_parent_pegging_id;
13768                         IF PG_DEBUG in ('Y', 'C') THEN
13769                            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_coprod_pegging_id := ' || l_coprod_pegging_id);
13770                         END IF;
13771                         */
13772                         --- bug 2178544
13773                         ---- get mso_lead_time factor
13774                         l_mso_lead_time_factor := MSC_ATP_PVT.G_MSO_LEAD_TIME_FACTOR;
13775                         --- Get the lead time for the item
13776                         -- dsting 2754446
13777                         l_net_processing_lt := CEIL((l_fixed_lt +
13778                                                     (l_variable_lt * l_PO_qty))*(1 + l_mso_lead_time_factor)
13779                                                     + l_pre_pro_lt);
13780                         l_process_lt := CEIL((l_fixed_lt +
13781                                              (l_variable_lt * l_PO_qty))*(1 + l_mso_lead_time_factor));
13782 
13783 --                        l_net_processing_lt := CEIL((l_fixed_lt +
13784 --                                                      (l_variable_lt * l_net_demand))*(1 + l_mso_lead_time_factor)
13785 --                                                      + l_pre_pro_lt);
13786 --                        l_process_lt := CEIL((l_fixed_lt +
13787 --                                                  (l_variable_lt * l_net_demand))*(1 + l_mso_lead_time_factor));
13788                         IF PG_DEBUG in ('Y', 'C') THEN
13789                            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_mso_lead_time_factor := ' || l_mso_lead_time_factor);
13790                            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_net_processing_lt := ' || l_net_processing_lt);
13791                            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_process_lt := ' || l_process_lt);
13792                         END IF;
13793                         MSC_ATP_PVT.G_PTF_DATE_THIS_LEVEL := l_ptf_date;
13794                         ---diag_ATP
13795                         IF p_search = BACKWARD_SCHEDULING THEN
13796                             l_constraint_flag := 2;
13797                             --diag_ATP
13798                             IF l_net_processing_lt > 0 THEN
13799                                  IF PG_DEBUG in ('Y', 'C') THEN
13800                                     msc_sch_wb.atp_debug('ATP_Check: ' || 'Calculate order date');
13801                                  END IF;
13802                                 /* ship_rec_cal
13803                                 l_order_date := MSC_CALENDAR.DATE_OFFSET
13804                                                                 (p_atp_record.organization_id,
13805                                                                 p_atp_record.instance_id,
13806                                                                 1,
13807                                                                 l_requested_ship_date,
13808                                                                 (-1 * l_net_processing_lt));*/
13809                                 -- since it is a make case only calendar being used is p_atp_record.manufacturing_cal_code
13810                                 l_order_date := MSC_CALENDAR.DATE_OFFSET(
13811                                                                 p_atp_record.manufacturing_cal_code,
13812                                                                 p_atp_record.instance_id,
13813                                                                 l_requested_ship_date,
13814                                                                 (-1 * l_net_processing_lt), -1);
13815 
13816                             ELSE
13817                                  IF PG_DEBUG in ('Y', 'C') THEN
13818                                     msc_sch_wb.atp_debug('ATP_Check: ' || 'Net process lt is zero');
13819                                  END IF;
13820                                 l_order_date := l_sysdate;
13821                             END IF;
13822 
13823                             IF PG_DEBUG in ('Y', 'C') THEN
13824                                msc_sch_wb.atp_debug('ATP_Check: ' || 'l_order_date := ' || l_order_date);
13825                             END IF;
13826                             -- calculate start date only if PTF_flag is enabled for the plan
13827                             IF (l_process_lt > 0) THEN
13828                                IF PG_DEBUG in ('Y', 'C') THEN
13829                                   msc_sch_wb.atp_debug('ATP_Check: ' || 'Calculate start date');
13830                                END IF;
13831                                /* ship_rec_cal
13832                                l_start_date := MSC_CALENDAR.DATE_OFFSET
13833                                                                 (p_atp_record.organization_id,
13834                                                                 p_atp_record.instance_id,
13835                                                                 1,
13836                                                                 l_requested_ship_date,
13837                                                                 (-1 * l_process_lt));*/
13838                                l_start_date := MSC_CALENDAR.DATE_OFFSET(
13839                                                                 p_atp_record.manufacturing_cal_code,
13840                                                                 p_atp_record.instance_id,
13841                                                                 l_requested_ship_date,
13842                                                                 (-1 * l_process_lt), -1);
13843                             ELSE
13844 
13845                                IF PG_DEBUG in ('Y', 'C') THEN
13846                                   msc_sch_wb.atp_debug('ATP_Check: ' || 'PTF flag is off for the plan or process LT is 0');
13847                                END IF;
13848                                l_start_date := l_ptf_date;
13849                             END IF;
13850 
13851                             IF PG_DEBUG in ('Y', 'C') THEN
13852                                msc_sch_wb.atp_debug('ATP_Check: ' || 'l_start_date := ' || l_start_date);
13853                             END IF;
13854                             -- bug 2178544
13855 
13856                             IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 THEN
13857                                 IF (l_order_date < l_sysdate OR l_start_date < l_ptf_date)  THEN
13858                                    -- we set the constraint flag so that we know that an constraint has occured
13859                                    l_constraint_flag := 1;
13860 
13861                                    ---calculate earliest due date possible based on PTF date
13862                                    IF l_net_processing_lt > 0 THEN
13863                                         /* ship_rec_cal
13864                                         l_lt_due_date :=
13865                                                     MSC_CALENDAR.date_offset(p_atp_record.organization_id,
13866                                                                                       p_atp_record.instance_id,
13867                                                                                       1,
13868                                                                                       l_sysdate,
13869                                                                                       l_net_processing_lt);*/
13870                                         l_lt_due_date :=
13871                                                     MSC_CALENDAR.date_offset(p_atp_record.manufacturing_cal_code,
13872                                                                                       p_atp_record.instance_id,
13873                                                                                       l_sysdate,
13874                                                                                       l_net_processing_lt, 1);
13875                                    ELSE
13876                                       l_lt_due_date := l_sysdate;
13877                                    END IF;
13878                                    ---calculate earliest due date possible based on PTF date
13879                                    IF  l_process_lt > 0 THEN
13880                                         /* ship_rec_cal
13881                                         l_ptf_due_date :=  MSC_CALENDAR.date_offset(p_atp_record.organization_id,
13882                                                                        p_atp_record.instance_id,
13883                                                                        1,
13884                                                                        l_ptf_date,
13885                                                                        l_process_lt);*/
13886                                         l_ptf_due_date :=  MSC_CALENDAR.date_offset(p_atp_record.manufacturing_cal_code,
13887                                                                        p_atp_record.instance_id,
13888                                                                        l_ptf_date,
13889                                                                        l_process_lt, 1);
13890                                    ELSE
13891                                         l_ptf_due_date := l_ptf_date;
13892                                    END IF;
13893                                    IF PG_DEBUG in ('Y', 'C') THEN
13894                                       msc_sch_wb.atp_debug('ATP_Check: ' || 'l_ptf_due_date := ' || l_ptf_due_date);
13895                                       msc_sch_wb.atp_debug('ATP_Check: ' || 'l_lt_due_date := ' || l_lt_due_date);
13896                                    END IF;
13897 
13898                                    ---We marked the most constrained constraint. If both constraint are equally
13899                                    -- constrained then we mark PTF constraint.
13900 
13901                                    -- Bug 3525582 - Constraint message should be added only if constraint actually exists
13902                                    IF l_lt_due_date > l_requested_ship_date OR
13903                                        l_ptf_due_date > l_requested_ship_date THEN
13904 
13905                                        -- Bug 3525582 - PTF constraint should be added only if plan is PTF enabled
13906                                        IF l_ptf_enabled=2 OR l_lt_due_date > l_ptf_due_date THEN
13907                                            l_pegging_rec.constraint_date := l_lt_due_date;
13908                                            l_pegging_rec.constraint_type := 3;
13909                                        ELSE
13910                                            l_pegging_rec.constraint_date := l_ptf_due_date;
13911                                            l_pegging_rec.constraint_type := 2;
13912                                        END IF;
13913                                    END IF;
13914                                 END IF; -- IF (l_order_date < l_sysdate OR l_start_date < l_ptf_date)  THEN
13915                             END IF; --IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 THEN
13916                         END IF;
13917                         IF p_search = FORWARD_SCHEDULING THEN
13918 
13919                             -- p_sched_flag = FORWARD_SCHEDULING
13920                             --- The future date must be greater than
13921                             --             1) sysdate + net_process time (pre + fixed + variable )
13922                             --             2) PTF Date + processing LT ( fixed + varibale LT)
13923                             -- Therefore we calcuate these dates
13924                             --- bug 2178544
13925                             IF l_net_processing_lt > 0 THEN
13926                                /* ship_rec_cal
13927                                MSC_ATP_PVT.G_FUTURE_ORDER_DATE :=  MSC_CALENDAR.date_offset(p_atp_record.organization_id,
13928                                                                                       p_atp_record.instance_id,
13929                                                                                       1,
13930                                                                                       l_sysdate,
13931                                                                                       l_net_processing_lt); */
13932                                MSC_ATP_PVT.G_FUTURE_ORDER_DATE :=  MSC_CALENDAR.date_offset(p_atp_record.manufacturing_cal_code,
13933                                                                                       p_atp_record.instance_id,
13934                                                                                       l_sysdate,
13935                                                                                       l_net_processing_lt, 1);
13936                             ELSE
13937                                MSC_ATP_PVT.G_FUTURE_ORDER_DATE := l_sysdate;
13938                             END IF;
13939 
13940                             IF  l_process_lt > 0 THEN
13941                                 /* ship_rec_cal
13942                                 l_start_date :=  MSC_CALENDAR.date_offset(p_atp_record.organization_id,
13943                                                                        p_atp_record.instance_id,
13944                                                                        1,
13945                                                                        l_ptf_date,
13946                                                                        l_process_lt); */
13947                                 l_start_date :=  MSC_CALENDAR.date_offset(p_atp_record.manufacturing_cal_code,
13948                                                                        p_atp_record.instance_id,
13949                                                                        l_ptf_date,
13950                                                                        l_process_lt, 1);
13951                             ELSE
13952                                 l_start_date := l_ptf_date;
13953                             END IF;
13954                             IF PG_DEBUG in ('Y', 'C') THEN
13955                                msc_sch_wb.atp_debug('ATP_Check: ' || 'l_start_date := ' || l_start_date);
13956                             END IF;
13957                             MSC_ATP_PVT.G_FUTURE_START_DATE := GREATEST(l_start_date, MSC_ATP_PVT.G_PTF_DATE);
13958                             ---we make sure that PTF for this stage is greatst of Parent's ptf - lead time,
13959                             -- current level's PTF and (sysdate + pre processing lt). The last part is the minimum
13960                             -- start date. This PTF is start date for the components for the current itme
13961                             IF l_pre_pro_lt >0 THEN
13962                                MSC_ATP_PVT.G_PTF_DATE := GREATEST(
13963                                                                /* ship_rec_cal
13964                                                                MSC_CALENDAR.date_offset(p_atp_record.organization_id,
13965                                                                                         p_atp_record.instance_id,
13966                                                                                         1,
13967                                                                                         MSC_ATP_PVT.G_PTF_DATE,
13968                                                                                         -1 * l_process_lt),*/
13969                                                                MSC_CALENDAR.date_offset(p_atp_record.manufacturing_cal_code,
13970                                                                                         p_atp_record.instance_id,
13971                                                                                         MSC_ATP_PVT.G_PTF_DATE,
13972                                                                                         -1 * l_process_lt, -1),
13973                                                                l_ptf_date,
13974                                                                /* ship_rec_cal
13975                                                                MSC_CALENDAR.date_offset(p_atp_record.organization_id,
13976                                                                                       p_atp_record.instance_id,
13977                                                                                       1,
13978                                                                                       l_sysdate,
13979                                                                                       l_pre_pro_lt));*/
13980                                                                MSC_CALENDAR.date_offset(p_atp_record.manufacturing_cal_code,
13981                                                                                       p_atp_record.instance_id,
13982                                                                                       l_sysdate,
13983                                                                                       l_pre_pro_lt, 1));
13984                             ELSE
13985                                MSC_ATP_PVT.G_PTF_DATE := GREATEST(
13986                                                                /* ship_rec_cal
13987                                                                MSC_CALENDAR.date_offset(p_atp_record.organization_id,
13988                                                                                         p_atp_record.instance_id,
13989                                                                                         1,
13990                                                                                         MSC_ATP_PVT.G_PTF_DATE,
13991                                                                                         -1 * l_process_lt),*/
13992                                                                MSC_CALENDAR.date_offset(p_atp_record.manufacturing_cal_code,
13993                                                                                         p_atp_record.instance_id,
13994                                                                                         MSC_ATP_PVT.G_PTF_DATE,
13995                                                                                         -1 * l_process_lt, -1),
13996                                                                 l_ptf_date, l_sysdate);
13997                             END IF;
13998 
13999                             IF PG_DEBUG in ('Y', 'C') THEN
14000                                msc_sch_wb.atp_debug('ATP_Check: ' || 'G_FUTURE_ORDER_DATE :='|| MSC_ATP_PVT.G_FUTURE_ORDER_DATE);
14001                                msc_sch_wb.atp_debug('ATP_Check: ' || 'G_FUTURE_START_DATE :='|| MSC_ATP_PVT.G_FUTURE_START_DATE);
14002                                msc_sch_wb.atp_debug('ATP_Check: ' || 'G_PTF_DATE := ' || MSC_ATP_PVT.G_PTF_DATE);
14003                             END IF;
14004                             --- if requested_ship_date is less than l_ptf_date then we add ptf_date for this item
14005                             -- UI checks this column. If this coulm is populated then UI flags this PO
14006                             --diag_atp
14007                             -- IF l_requested_ship_date < l_start_date  or l_requested_ship_date < sysdate THEN
14008                             -- Bug 3412143 - Use l_sysdate (in this case validated in OMC) instead of sysdate
14009                             IF l_requested_ship_date < l_start_date  or l_requested_ship_date < l_sysdate THEN
14010                                 IF PG_DEBUG in ('Y', 'C') THEN
14011                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'Add PTF date to pegging');
14012                                 END IF;
14013                                 -- Bug 3525582 - PTF constraint should be added only if plan is PTF enabled
14014                                 IF l_ptf_enabled=2 OR MSC_ATP_PVT.G_FUTURE_ORDER_DATE > l_start_date THEN
14015                                        l_pegging_rec.constraint_date := MSC_ATP_PVT.G_FUTURE_ORDER_DATE;
14016                                        l_pegging_rec.constraint_type := 3;
14017                                 ELSE
14018                                        l_pegging_rec.constraint_date := l_start_date;
14019                                        l_pegging_rec.constraint_type := 2;
14020                                 END IF;
14021 
14022                             END IF;
14023                         END IF;
14024                         /* Modularize Item and Org Info */
14025                         -- Changed call to remove embedded function call.
14026 
14027                         ---diag_atp
14028                         IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 and l_constraint_flag = 1 THEN
14029                            --in diagnostic ATP since we have already failed the lead time test
14030                            --- there is no point in adding PO and removing it later
14031 			   l_transaction_id := NULL;
14032                         ELSE
14033 
14034                            --s_cto_rearch
14035                            --we do not get process effectivity when checking base model's
14036                            -- capacity for config item
14037                            IF l_check_model_capacity = 2 THEN
14038                                /* Make case Get the Process Sequence ID.*/
14039                               -- (ssurendr) Bug 2865389 : Replace call to function MSC_ATP_FUNC.get_process_seq_id
14040                               -- by procedure MSC_ATP_PROC.get_process_effectivity
14041                               --Details. Derive Item's Start Date by Offsetting
14042                               --Due date with LT. Check effectivity on Start Date.
14043                               /* ship_rec_cal
14044                               l_opm_start_date := MSC_CALENDAR.date_offset(p_atp_record.organization_id,
14045                                                            p_atp_record.instance_id,
14046                                                            1,
14047                                                            l_requested_ship_date,
14048                                                            -1 * (nvl(PF_ITEM_INFO_REC.fixed_lt,0) +
14049                                                               nvl(PF_ITEM_INFO_REC.variable_lt,0) * l_PO_qty));*/
14050                               l_opm_start_date := MSC_CALENDAR.date_offset(p_atp_record.manufacturing_cal_code,
14051                                                            p_atp_record.instance_id,
14052                                                            l_requested_ship_date,
14053                                                            -1 * (nvl(PF_ITEM_INFO_REC.fixed_lt,0) +
14054                                                               nvl(PF_ITEM_INFO_REC.variable_lt,0) * l_PO_qty), -1);
14055                               MSC_ATP_PROC.get_process_effectivity(
14056                                    p_plan_id,
14057                                    --PF_ITEM_INFO_REC.dest_inv_item_id,
14058                                    C_ITEM_INFO_REC.dest_inv_item_id, -- For time_phased_atp
14059                                    p_atp_record.organization_id,
14060                                    p_atp_record.instance_id,
14061                                    l_opm_start_date,
14062                                    l_PO_qty,
14063                                    l_process_seq_id,
14064                                    l_routing_seq_id,
14065                                    l_bill_seq_id,
14066                                    l_op_seq_id, --4570421
14067                                    l_return_status);
14068                               -- check the return status
14069                               IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
14070                               	   RAISE FND_API.G_EXC_ERROR;
14071 	                      END IF;
14072                            END IF;
14073                            --s_cto_rearch
14074 	                   IF MSC_ATP_PVT.G_INV_CTP = 4 THEN
14075                               MSC_ATP_DB_UTILS.Add_Planned_Order(
14076                                     p_atp_record.instance_id,
14077                                     p_plan_id,
14078                                     PF_ITEM_INFO_REC.dest_inv_item_id,
14079                                     p_atp_record.organization_id,
14080                                     l_planned_order_date,  -- For ship_rec_cal
14081                                     l_PO_qty, -- 2754446
14082                                     -- l_net_demand,
14083                                     null,
14084                                     null,
14085                                     p_atp_record.demand_class,
14086                                     -- rajjain 02/19/2003 Bug 2788302 Begin
14087                                     -- Add Sourcing details
14088                                     l_sources.organization_id(j),
14089                                     l_sources.instance_id(j),
14090                                     l_process_seq_id,
14091                                     -- rajjain 02/19/2003 Bug 2788302 End
14092                                     p_refresh_number,    -- For summary enhancenment
14093                                     -- ship_rec_cal changes begin
14094                                     null,
14095                                     null,
14096                                     null,
14097                                     null,
14098                                     null,
14099                                     --bug 3755704: Add start date and order date
14100                                     l_start_date,
14101                                     l_order_date,
14102                                     --null,       -- Bug 3241766
14103                                     --null,       -- Bug 3241766
14104                                     null,
14105                                     -- ship_rec_cal changes end
14106                                     l_transaction_id,
14107                                     l_return_status,
14108                                     -- time_phased_atp
14109                                     NULL, --4127630 (Make case)
14110                                     C_ITEM_INFO_REC.dest_inv_item_id,
14111                                     p_atp_record.atf_date
14112                               );
14113                            END IF;
14114                         END IF;
14115                         /* Modularize Item and Org Info */
14116 
14117                         -- populate insert rec to pegging tree for this supply
14118 
14119                         l_pegging_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
14120                         l_pegging_rec.order_line_id:= MSC_ATP_PVT.G_ORDER_LINE_ID;
14121                         --subst
14122                         --l_pegging_rec.parent_pegging_id:= l_pegging_id;
14123                         l_pegging_rec.parent_pegging_id := l_parent_pegging_id;
14124                         l_pegging_rec.atp_level:= p_level+1;
14125                         l_pegging_rec.organization_id :=
14126                             p_atp_record.organization_id;
14127                         --l_pegging_rec.organization_code := l_org_code;
14128                         l_pegging_rec.organization_code :=
14129                                MSC_ATP_FUNC.get_org_code(p_atp_record.instance_id,
14130                                            l_pegging_rec.organization_id);--5222584
14131                         l_pegging_rec.identifier1:= p_atp_record.instance_id;
14132                         l_pegging_rec.identifier2:= p_plan_id;
14133                         l_pegging_rec.identifier3 := l_transaction_id;
14134 
14135                         -- time_phased_atp changes begin
14136                         IF l_pf_atp = 'Y' THEN
14137                            l_pegging_rec.inventory_item_id:= p_atp_record.inventory_item_id;
14138                            l_pegging_rec.inventory_item_name := PF_ITEM_INFO_REC.item_name;
14139                         ELSE
14140                            l_pegging_rec.inventory_item_id:= p_atp_record.request_item_id;
14141                            l_pegging_rec.inventory_item_name := C_ITEM_INFO_REC.item_name;
14142                         END IF;
14143                         l_pegging_rec.aggregate_time_fence_date := p_atp_record.atf_date;
14144                         -- time_phased_atp changes end
14145 
14146                         l_pegging_rec.resource_id := NULL;
14147                         l_pegging_rec.resource_code := NULL;
14148                         l_pegging_rec.department_id := NULL;
14149                         l_pegging_rec.department_code := NULL;
14150                         l_pegging_rec.supplier_id := NULL;
14151                         l_pegging_rec.supplier_name := NULL;
14152                         l_pegging_rec.supplier_site_id := NULL;
14153                         l_pegging_rec.supplier_site_name := NULL;
14154 
14155                         l_pegging_rec.scenario_id:= p_scenario_id;
14156                         l_pegging_rec.supply_demand_source_type:= 1;
14157 
14158                         --diag_atp -- bug 3903205: set qty to zero only for diagnostic ATP
14159                         IF l_constraint_flag = 1 and NVL(MSC_ATP_PVT.G_DIAGNOSTIC_ATP, 2) = 1 THEN
14160                            l_pegging_rec.supply_demand_quantity :=0;
14161                         ELSE
14162                            l_pegging_rec.supply_demand_quantity :=l_PO_qty; -- 2754446
14163                            -- l_pegging_rec.supply_demand_quantity :=l_net_demand;
14164                         END IF;
14165                         l_pegging_rec.supply_demand_type:= 2;
14166                         l_pegging_rec.supply_demand_date:= l_requested_ship_date;
14167                         l_pegging_rec.source_type := 2;
14168 
14169                         IF PG_DEBUG in ('Y', 'C') THEN
14170                            msc_sch_wb.atp_debug('ATP_Check: ' || 'inside make 1');
14171                         END IF;
14172 
14173                         -- for demo:1153192
14174                         l_pegging_rec.constraint_flag := 'N';
14175 			l_pegging_rec.component_identifier :=
14176                          NVL(p_atp_record.component_identifier, MSC_ATP_PVT.G_COMP_LINE_ID);
14177 
14178                         l_pegging_rec.summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;
14179 
14180                         --- bug 2152184: For PF based ATP inventory_item_id field contains id for PF item
14181                         --- cto looks at pegging tree to place their demands. Since CTO expects to find
14182                         --  id for the requested item, we add the following column. CTO will now read from this column
14183                         l_pegging_rec.request_item_id := p_atp_record.request_item_id;
14184 
14185                         --diag_atp
14186                         l_pegging_rec.ptf_date := l_ptf_date;
14187                         l_pegging_rec.preprocessing_lead_time := l_pre_pro_lt;
14188                         l_pegging_rec.processing_lead_time := l_process_lt;
14189 
14190                         --bug 4409618: Show variable and fixed lt in pegging properties
14191                         l_pegging_rec.fixed_lead_time := l_fixed_lt;
14192                         l_pegging_rec.VARIABLE_LEAD_TIME := l_variable_lt;
14193 
14194                         l_pegging_rec.pegging_type := MSC_ATP_PVT.MAKE_SUPPLY; -- it is a make planned order
14195                         l_pegging_rec.required_date := l_requested_ship_date;
14196                         --bug 3328421
14197                         l_pegging_rec.actual_supply_demand_date := l_requested_ship_date;
14198                         l_pegging_rec.required_quantity := l_net_demand;
14199                         l_pegging_rec.plan_name := p_atp_record.plan_name;
14200                         l_pegging_rec.dest_inv_item_id := C_ITEM_INFO_REC.dest_inv_item_id;
14201 
14202                         --bug 3373075: Mark plan order to be model entity only for
14203                         -- config, model or option class
14204                         --parent is model entity
14205                         /* IF (p_atp_record.parent_bom_item_type in (1, 4) and
14206                                                   p_atp_record.parent_repl_order_flag = 'Y')
14207                            --item itself is model entity
14208                            OR (C_ITEM_INFO_REC.bom_item_type in (1, 4) and
14209                                                   C_ITEM_INFO_REC.replenish_to_ord_flag = 'Y') THEN
14210                         */
14211                         --item itself is model entity
14212                         IF (C_ITEM_INFO_REC.bom_item_type in (1,2, 4) and
14213                                                   C_ITEM_INFO_REC.replenish_to_ord_flag = 'Y') THEN
14214                            l_pegging_rec.model_sd_flag := 1;
14215                         END IF;
14216 
14217 
14218                         --bug 3356779: populate atp rule and ITF on PO
14219                         IF l_atp_flag = 'Y' THEN
14220                            l_pegging_rec.infinite_time_fence := l_get_mat_out_rec.infinite_time_fence_date;
14221                            l_pegging_rec.atp_rule_name := l_get_mat_out_rec.atp_rule_name;
14222                         ELSE
14223                            --4394904
14224                            --l_pegging_rec.infinite_time_fence := l_atp_date_this_level;
14225                            l_pegging_rec.infinite_time_fence := l_infinite_time_fence;
14226                            l_pegging_rec.atp_rule_name := l_atp_rule_name;
14227                         END IF;
14228 
14229                         l_pegging_rec.demand_class :=  p_atp_record.demand_class;
14230                          -- Bug 3826234
14231                         l_pegging_rec.manufacturing_cal_code :=  l_atp_rec.manufacturing_cal_code;
14232                         l_pegging_rec.shipping_cal_code      :=  NULL;
14233                         l_pegging_rec.receiving_cal_code     :=  NULL;
14234                         l_pegging_rec.intransit_cal_code     :=  NULL;
14235 
14236                         --4570421
14237                         l_pegging_rec.scaling_type                      := p_atp_record.scaling_type;
14238                         l_pegging_rec.scale_multiple                    := p_atp_record.scale_multiple;
14239                         l_pegging_rec.scale_rounding_variance           := p_atp_record.scale_rounding_variance;
14240                         l_pegging_rec.rounding_direction                := p_atp_record.rounding_direction;
14241                         l_pegging_rec.component_yield_factor            := p_atp_record.component_yield_factor; --4570421
14242                         l_pegging_rec.usage                             := p_atp_record.usage_qty; --4775920
14243                         l_pegging_rec.organization_type                 := p_atp_record.organization_type; --4775920
14244 
14245                         MSC_ATP_DB_UTILS.Add_Pegging(l_pegging_rec, l_pegging_id);
14246 
14247 			-- dsting diag_atp last good peggingfor make
14248 			IF l_pegging_rec.constraint_type IS NULL THEN
14249 				IF PG_DEBUG in ('Y', 'C') THEN
14250 				   msc_sch_wb.atp_debug('make last good pegging id ' || l_pegging_id);
14251 				END IF;
14252 				l_last_good_pegging_id := l_pegging_id;
14253 			END IF;
14254                         --5216821 Set co prod peg id to PO peg id
14255                         l_coprod_pegging_id := l_pegging_id;
14256 
14257                         IF PG_DEBUG in ('Y', 'C') THEN
14258                            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_coprod_pegging_id : ' || l_coprod_pegging_id);
14259                         END IF;
14260                         --optional_fw Make PO should be set for top level
14261                         IF PG_DEBUG in ('Y', 'C') AND G_FW_CTP_PEGGING_ID.COUNT >0 THEN
14262                            msc_sch_wb.atp_debug('ATP_Check: ' || 'G_FW_CTP_PEGGING_ID := '
14263                                                               || G_FW_CTP_PEGGING_ID(G_FW_CTP_PEGGING_ID.COUNT));
14264                            msc_sch_wb.atp_debug('ATP_Check: ' || 'G_OPTIONAL_FW := ' || G_OPTIONAL_FW);
14265                         ELSIF PG_DEBUG in ('Y', 'C') THEN
14266                            msc_sch_wb.atp_debug('ATP_Check: ' || 'G_OPTIONAL_FW := ' || G_OPTIONAL_FW);
14267                            msc_sch_wb.atp_debug('ATP_Check: ' || 'G_FW_PEGGING_ID := ' || G_FW_PEGGING_ID);
14268                         END IF;
14269                         IF MSC_ATP_PVT.G_OPTIONAL_FW is not null
14270                                                AND (G_FW_CTP_PEGGING_ID.COUNT = 0 --Means top level, for next source value = null
14271                                                     OR G_FW_CTP_PEGGING_ID(G_FW_CTP_PEGGING_ID.COUNT) is null) then
14272                          IF G_FW_CTP_PEGGING_ID.COUNT = 0 THEN
14273                           G_FW_CTP_PEGGING_ID.EXTEND;
14274                          END IF;
14275                          G_FW_CTP_PEGGING_ID(G_FW_CTP_PEGGING_ID.COUNT) := l_pegging_id;
14276                          IF PG_DEBUG in ('Y', 'C') THEN
14277                            msc_sch_wb.atp_debug('ATP_Check: ' || 'G_FW_CTP_PEGGING_ID := '
14278                                                               || G_FW_CTP_PEGGING_ID(G_FW_CTP_PEGGING_ID.COUNT));
14279                          END IF;
14280                         END IF;
14281                         IF PG_DEBUG in ('Y', 'C') AND G_FW_CTP_PEGGING_ID.COUNT >0 THEN
14282                            msc_sch_wb.atp_debug('ATP_Check: ' || 'G_FW_CTP_PEGGING_ID := '
14283                                                               || G_FW_CTP_PEGGING_ID(G_FW_CTP_PEGGING_ID.COUNT));
14284                            msc_sch_wb.atp_debug('ATP_Check: ' || 'G_OPTIONAL_FW := ' || G_OPTIONAL_FW);
14285                         ELSIF PG_DEBUG in ('Y', 'C') THEN
14286                            msc_sch_wb.atp_debug('ATP_Check: ' || 'G_OPTIONAL_FW := ' || G_OPTIONAL_FW);
14287                            msc_sch_wb.atp_debug('ATP_Check: ' || 'G_FW_PEGGING_ID := ' || G_FW_PEGGING_ID);
14288                         END IF;
14289                         /*--- bug 2178544
14290                         ---- get mso_lead_time factor
14291                         l_mso_lead_time_factor := MSC_ATP_PVT.G_MSO_LEAD_TIME_FACTOR;
14292                         --- Get the lead time for the item
14293                         l_net_processing_lt := CEIL((l_fixed_lt +
14294                                                       (l_variable_lt * l_net_demand))*(1 + l_mso_lead_time_factor)
14295                                                       + l_pre_pro_lt);
14296                         l_process_lt := CEIL((l_fixed_lt +
14297                                                   (l_variable_lt * l_net_demand))*(1 + l_mso_lead_time_factor));
14298                         IF PG_DEBUG in ('Y', 'C') THEN
14299                            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_mso_lead_time_factor := ' || l_mso_lead_time_factor);
14300                            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_net_processing_lt := ' || l_net_processing_lt);
14301                            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_process_lt := ' || l_process_lt);
14302                         END IF;
14303                         MSC_ATP_PVT.G_PTF_DATE_THIS_LEVEL := l_ptf_date; */
14304 
14305                         IF PG_DEBUG in ('Y', 'C') THEN
14306                            msc_sch_wb.atp_debug('ATP_Check: ' || 'p_search' || p_search);
14307                         END IF;
14308                         --diag_atp
14309                         IF (MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 and l_constraint_flag <> 1) OR
14310                             (p_search = BACKWARD_SCHEDULING and MSC_ATP_PVT.G_DIAGNOSTIC_ATP <> 1) THEN
14311 
14312                             l_avail_assembly_qty := l_net_demand;
14313                             --2178544
14314                             ---- if lead time > 0 then offset the date to see if the offseted
14315                             --- date falls before sysdate or not
14316                             ---- If ofsetted date falls before sys date then we can't make the item
14317                             ---- and requested_date quantity is set to Zero
14318                             --- If Offseted date is > sysdate then we  continue as ususal
14319 
14320                             IF PG_DEBUG in ('Y', 'C') THEN
14321                                msc_sch_wb.atp_debug('ATP_Check: ' || 'l_start_date := ' || l_start_date);
14322                             END IF;
14323                             -- bug 2178544
14324 							-- bug 12672787, validate l_order_date against l_sys_next_omc_date and not l_sysdate
14325 							-- as l_sysdate is derived from ship_cal and omc_date is derived from manufact_val
14326                             IF (l_order_date < l_sys_next_omc_date OR l_start_date < l_ptf_date)  THEN
14327                                 IF PG_DEBUG in ('Y', 'C') THEN
14328                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'req date - lead time is less than sys or ptf date');
14329                                 END IF;
14330                                 l_avail_assembly_qty := 0;
14331                             ELSE
14332 
14333                                IF PG_DEBUG in ('Y', 'C') THEN
14334                                   msc_sch_wb.atp_debug('ATP_Check: ' || 'inside make 2');
14335                                END IF;
14336                                IF ((l_atp_comp_flag = 'R') OR
14337                                	   (l_atp_comp_flag = 'C')) THEN
14338                                	   IF PG_DEBUG in ('Y', 'C') THEN
14339                                	      msc_sch_wb.atp_debug('ATP_Check: ' || 'inside make 3');
14340                                	   END IF;
14341 
14342                                	   -- get resource requirements first
14343             		       	   MSC_ATP_REQ.Get_Res_Requirements(
14344                                	     p_atp_record.instance_id,
14345 			       	     p_plan_id,
14346                                	     p_level+2,
14347                                	     p_scenario_id,
14348                                	     --p_atp_record.inventory_item_id,
14349                                	     l_request_item_id, -- Pass member item's id to make sure member item's id is
14350                                	                        -- inserted in assembly_item_id column in msc_resource_requirements
14351                                	     p_atp_record.organization_id,
14352                               	     l_pegging_id,
14353                                      l_PO_qty, -- 2754446
14354                                	     --l_net_demand,
14355                                	     l_requested_ship_date,
14356                                	     p_refresh_number,
14357 			       	     p_atp_record.insert_flag,
14358                                	     p_search,
14359                                	     p_atp_record.demand_class,
14360                                      --(ssurendr) Bug 2865389 Added Routing Sequence id
14361                                      --and Bill sequence id for OPM fix.
14362                                      l_routing_seq_id,
14363                                      l_bill_seq_id,
14364                                      p_atp_record.ship_date, -- Bug 2814872
14365                                      p_atp_record.demand_source_line,   -- CTO ODR
14366                                	     l_avail_assembly_qty,
14367                                	     l_res_atp_date,
14368                                	     l_res_atp_period,
14369                                	     l_res_atp_supply_demand,
14370 	                             l_return_status );
14371 
14372                                	   -- check the return status
14373                                	   IF l_return_status <>
14374                                	       FND_API.G_RET_STS_SUCCESS THEN
14375                               	       RAISE FND_API.G_EXC_ERROR;
14376 	                           END IF;
14377 	                           --optional_fw
14378 	                           IF MSC_ATP_PVT.G_OPTIONAL_FW is not null THEN
14379                                       p_atp_record.error_code := ATP_REQ_DATE_FAIL;
14380                                    END IF;
14381 
14382                                END IF;
14383 
14384                                ---diag_ATP
14385                                IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 THEN
14386                                    ---In diagnostic mode we look for complete qty. Thats why looks
14387                                    -- for component as if we got complete resource qty.
14388                                    l_res_avail_qty := l_avail_assembly_qty;
14389                                    l_avail_assembly_qty := l_net_demand;
14390                                END IF;
14391 
14392 
14393                                IF ((l_atp_comp_flag = 'C') OR
14394                                 (l_atp_comp_flag = 'Y')) AND
14395                                LEAST(l_net_demand, l_avail_assembly_qty) >0 THEN
14396                              	   -- get component requirements first
14397 
14398                                	   IF PG_DEBUG in ('Y', 'C') THEN
14399                                	      msc_sch_wb.atp_debug('ATP_Check: ' || 'inside make 4');
14400                                	   END IF;
14401 
14402                                    BEGIN
14403                                         IF l_avail_assembly_qty < l_net_demand AND
14404                                             G_ORG_INFO_REC.org_type = 1 THEN
14405                                             /* ship_rec_cal
14406                                             l_opm_start_date := l_requested_ship_date -
14407                                                                 (nvl(PF_ITEM_INFO_REC.fixed_lt,0) +
14408                                                                 nvl(PF_ITEM_INFO_REC.variable_lt,0) *
14409                                                                 l_avail_assembly_qty);*/
14410                                             l_opm_start_date := MSC_CALENDAR.date_offset(p_atp_record.manufacturing_cal_code,
14411                                                                 p_atp_record.instance_id,
14412                                                                 l_requested_ship_date,
14413                                                                 -1 * (nvl(PF_ITEM_INFO_REC.fixed_lt,0) +
14414                                                                       nvl(PF_ITEM_INFO_REC.variable_lt,0) * l_avail_assembly_qty), -1);
14415                                             MSC_ATP_PROC.get_process_effectivity(
14416                                                 p_plan_id,
14417                                                 --PF_ITEM_INFO_REC.dest_inv_item_id,
14418                                                 C_ITEM_INFO_REC.dest_inv_item_id, -- For time_phased_atp
14419                                                 p_atp_record.organization_id,
14420                                                 p_atp_record.instance_id,
14421                                                 l_opm_start_date,
14422                                                 l_avail_assembly_qty,
14423                                                 l_process_seq_id,
14424                                                 l_routing_seq_id,
14425                                                 l_bill_seq_id,
14426                                                 l_op_seq_id, --4570421
14427                                                 l_return_status);
14428                                            -- check the return status
14429                                            IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
14430                                               	RAISE FND_API.G_EXC_ERROR;
14431                 	                   END IF;
14432                                         END IF;
14433 
14434 
14435                                	        MSC_ATP_REQ.Get_Comp_Requirements(
14436                                 	     p_atp_record.instance_id,
14437                                     	     p_plan_id,
14438                                     	     p_level+2,
14439                                     	     p_scenario_id,
14440                                     	     l_request_item_id, -- for time_phased_atp
14441                                     	     p_atp_record.organization_id,
14442                                     	     l_pegging_id,
14443                                     	     p_atp_record.demand_class,
14444                                              LEAST(l_PO_qty, l_avail_assembly_qty),
14445                                     	     --LEAST(l_net_demand, l_avail_assembly_qty),
14446                                     	     l_requested_ship_date,
14447                                     	     p_refresh_number,
14448                                     	     p_atp_record.insert_flag,
14449                                     	     p_search,
14450 				    	     p_assign_set_id,
14451                                              -- (ssurendr) Bug 2865389 Added Routing Sequence id
14452                                              --and Bill sequence id for OPM fix.
14453                                              l_routing_seq_id,
14454                                              l_bill_seq_id,
14455                                              p_atp_record.inventory_item_id,      -- For time_phased_atp
14456                                              p_atp_record.atf_date,               -- For time_phased_atp
14457                                              p_atp_record.manufacturing_cal_code, -- For ship_rec_cal
14458                                     	     l_avail_assembly_qty,
14459                                     	     l_comp_atp_date,
14460                                     	     l_comp_atp_period,
14461                                     	     l_comp_atp_supply_demand,
14462 	                                     l_return_status,
14463                                              --s_cto_rearch
14464                                              l_get_comp_info_rec,
14465                                              -- CTO Phase 1 #16, #17
14466                                              p_atp_record.order_number,
14467                                              l_op_seq_id --4570421
14468                                              );
14469                                              -- Allow creation of Sales Order for CTO comps.
14470                                    -- 2400614: krajan
14471                                    EXCEPTION
14472                                         WHEN MSC_ATP_PVT.G_ATO_SOURCING_MISMATCH THEN
14473                                              IF PG_DEBUG in ('Y', 'C') THEN
14474                                                 msc_sch_wb.atp_debug('ATP_Check: ' || ' Sourcing Mismatch Error');
14475                                              END IF;
14476                                              p_atp_record.error_code := MSC_ATP_PVT.ASSIGN_SET_NOT_IN_SYNC;
14477                                              RAISE MSC_ATP_PVT.G_ATO_SOURCING_MISMATCH;
14478 
14479                                         -- dsting 2764213
14480                                         WHEN MSC_ATP_PVT.EXC_NO_PLAN_FOUND THEN
14481                                              IF PG_DEBUG in ('Y', 'C') THEN
14482                                                 msc_sch_wb.atp_debug('ATP_Check: Plan not found Error');
14483                                              END IF;
14484                                              --24x7
14485                                              if (NVL(MSC_ATP_PVT.G_DOWNTIME_HIT, 'N') = 'Y') then
14486                                                 ---2854351
14487                                                 --p_atp_record.error_code := MSC_ATP_PVT.TRY_ATP_LATER;
14488                                                 p_atp_record.error_code := MSC_ATP_PVT.PLAN_DOWN_TIME;
14489                                              else
14490                                                 p_atp_record.error_code := MSC_ATP_PVT.PLAN_NOT_FOUND;
14491                                              end if;
14492 
14493                                              RAISE MSC_ATP_PVT.EXC_NO_PLAN_FOUND;
14494                                         -- krajan 2752705
14495                                         WHEN MSC_ATP_PVT.G_EXC_UNCOLLECTED_ITEM THEN
14496                                              IF PG_DEBUG in ('Y', 'C') THEN
14497                                                 msc_sch_wb.atp_debug('ATP_Check: Uncollected Item Error');
14498                                              END IF;
14499                                              p_atp_record.error_code := MSC_ATP_PVT.ATP_ITEM_NOT_COLLECTED;
14500                                              RAISE MSC_ATP_PVT.G_EXC_UNCOLLECTED_ITEM;
14501                                         WHEN MSC_ATP_PVT.NO_MATCHING_DATE_IN_CAL THEN --bug3583705
14502                                              IF PG_DEBUG in ('Y', 'C') THEN
14503                                                 msc_sch_wb.atp_debug ('ATP_Check:NO_MATCHING_DATE_IN_CAL');
14504                                              END IF;
14505                                              p_atp_record.error_code := MSC_ATP_PVT.NO_MATCHING_CAL_DATE;
14506                                              RAISE MSC_ATP_PVT.NO_MATCHING_DATE_IN_CAL;
14507                                    END;
14508 
14509                                    IF l_return_status = MSC_ATP_PVT.CTO_OSS_ERROR THEN
14510 
14511                                         RAISE MSC_ATP_PVT.INVALID_OSS_SOURCE;
14512 
14513                               	   ELSIF l_return_status <>FND_API.G_RET_STS_SUCCESS THEN
14514 	                                RAISE FND_API.G_EXC_ERROR;
14515 	                           END IF;
14516 	                           --optional_fw
14517 	                           IF MSC_ATP_PVT.G_OPTIONAL_FW is not null THEN
14518                                       p_atp_record.error_code := ATP_REQ_DATE_FAIL;
14519                                    END IF;
14520 
14521                                END IF;
14522  			    END IF;
14523                             -- now we have done the capacity check
14524                             -- l_avail_assembly_qty is the quantity that
14525                             -- we think we can build.  Since we may reserve
14526                             -- more during the the check for each comp/res,
14527                             -- we need to undo what we have done so far
14528                             -- and use this l_avail_assembly_qty to reserve
14529                             -- the capacity if we can not build the whole
14530                             -- l_net_demand.  For example,  we have bill like
14531                             -- this
14532                             --    A
14533                             --   / \
14534                             --  B   R
14535                             -- let say we need to build 10 A and we have 7 B
14536                             -- and 4 R.  During atp check we reserve 7B and 4R
14537                             -- already.  Now we need to undo these reservation,
14538                             -- and just reserve 4 B and 4 R instead.
14539                             -- the way we will do is
14540                             -- 1: undo all the inserts we have done for this
14541                             --    make node.
14542                             -- 2: redo this make node with l_avail_assembly_qty.
14543 
14544                             -- 2869830 do not assume lower level returns rounded qty
14545                             IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 THEN
14546                                l_avail_assembly_qty := LEAST(l_avail_assembly_qty, l_res_avail_qty);
14547                             END IF;
14548 
14549                             IF NVL(C_ITEM_INFO_REC.rounding_control_type, 2) = 1 THEN
14550                                msc_sch_wb.atp_debug('FLOOR of avail qty for l_avail_PO_qty: '
14551                                                     || l_avail_assembly_qty);
14552                                l_avail_PO_qty := FLOOR(l_avail_assembly_qty);
14553                             ELSE
14554                                msc_sch_wb.atp_debug('l_avail_PO_qty := avail qty: '
14555                                                     || l_avail_assembly_qty);
14556                                l_avail_PO_qty := trunc(l_avail_assembly_qty, 6);	--5598066
14557                             END IF;
14558 
14559                             --diag_atp
14560                             --- we go back and adjust the quantities for non-diagnostic ATP only
14561                             IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 THEN
14562                                l_avail_assembly_qty := LEAST(l_avail_assembly_qty, l_res_avail_qty);
14563 
14564                                IF PG_DEBUG in ('Y', 'C') THEN
14565                                   msc_sch_wb.atp_debug('ATP_Check: ' || 'l_avail_assembly_qty := ' || l_avail_assembly_qty);
14566 				  msc_sch_wb.atp_debug('last good pegging: ' || l_pegging_id);
14567 				  msc_sch_wb.atp_debug('qty ' || l_avail_assembly_qty);
14568 				  msc_sch_wb.atp_debug('PO qty ' || l_avail_PO_qty);
14569                                END IF;
14570 
14571 				-- dsting diag_atp
14572 				-- If this is the last source we should only update the pegging and not
14573 				-- the actual PO in case of diagnostic atp
14574                                IF l_avail_PO_qty < l_net_demand THEN -- 2754446
14575                                --IF l_avail_assembly_qty < l_net_demand THEN
14576 				  l_last_good_pegging_id := l_pegging_id;
14577 				  IF l_sources.source_type.next(j) IS NULL
14578 				     AND MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1
14579 				  THEN
14580 					MSC_ATP_DB_UTILS.Update_Pegging(l_pegging_id, null, l_avail_PO_qty); -- 2869830
14581 					-- MSC_ATP_DB_UTILS.Update_Pegging(l_pegging_id, null, l_avail_assembly_qty);
14582 				  ELSE
14583                                         MSC_ATP_DB_UTILS.Update_Planned_Order(l_pegging_id,
14584                                                    p_plan_id,
14585                                                    null,
14586                                                    l_avail_PO_qty, -- 2869830
14587                                                    --l_avail_assembly_qty,
14588                                                    null,
14589                                                    null,
14590                                                    null,
14591                                                    null,  -- Bug 3241766
14592                                                    --bug 3755704: Pass start date and order date
14593                                                    l_start_date,
14594                                                    l_order_date,
14595                                                    --null,  -- Bug 3241766
14596                                                    --null,  -- Bug 3241766
14597                                                    C_ITEM_INFO_REC.dest_inv_item_id,   -- Bug 3293163
14598                                                    PF_ITEM_INFO_REC.dest_inv_item_id,  -- Bug 3293163
14599 						   MSC_ATP_PVT.UNDO);
14600 				  END IF;
14601 
14602                                END IF;
14603                                -- move the supply/demand, period atp
14604                                -- of the components to the output record of
14605                                -- tables.
14606                                MSC_ATP_PROC.Details_Output(l_comp_atp_period,
14607                                              l_comp_atp_supply_demand,
14608                                              x_atp_period,
14609                                              x_atp_supply_demand,
14610                                              l_return_status);
14611 
14612                                MSC_ATP_PROC.Details_Output(l_res_atp_period,
14613                                              l_res_atp_supply_demand,
14614                                              x_atp_period,
14615                                              x_atp_supply_demand,
14616                                              l_return_status);
14617 
14618                             ELSIF (l_avail_PO_qty < l_net_demand)  THEN
14619                             --ELSIF (l_avail_assembly_qty < l_net_demand)  THEN
14620                               IF PG_DEBUG in ('Y', 'C') THEN
14621                                  msc_sch_wb.atp_debug('ATP_Check: ' || 'l_avail_assembly_qty ='||l_avail_assembly_qty);
14622                                  msc_sch_wb.atp_debug('ATP_Check: ' || 'l_pegging_id = '||l_pegging_id);
14623                                  msc_sch_wb.atp_debug('ATP_Check: ' || 'inside make 7');
14624                               END IF;
14625                               IF l_avail_PO_qty > 0 THEN
14626                               --IF l_avail_assembly_qty > 0 THEN
14627 
14628                                 -- bug 1277418: now clean up the data.
14629                                 -- if l_avail_assembly_qty >0, we need to
14630                                 -- to keep the planned order.
14631 
14632                                 MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(
14633                                        l_pegging_id ,
14634                                        p_atp_record.instance_id,
14635                                        p_plan_id,
14636                                        INVALID,
14637                                        0,
14638                                        l_return_status);
14639 
14640                                 IF PG_DEBUG in ('Y', 'C') THEN
14641                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'inside make 6');
14642                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'l_pegging_id = '||l_pegging_id);
14643                                 END IF;
14644 
14645                                 -- update/delete the planned order record.
14646                                 MSC_ATP_DB_UTILS.Update_Planned_Order(l_pegging_id,
14647                                                    p_plan_id,
14648                                                    null,
14649                                                    l_avail_PO_qty,
14650                                                    --l_avail_assembly_qty,
14651                                                    null,
14652                                                    null,
14653                                                    null,
14654                                                    null,  -- Bug 3241766
14655                                                    --bug 3755704: pass start date and order date
14656                                                    l_start_date,
14657                                                    l_order_date,
14658                                                    --null,  -- Bug 3241766
14659                                                    --null,  -- Bug 3241766
14660                                                    C_ITEM_INFO_REC.dest_inv_item_id,   -- Bug 3293163
14661                                                    PF_ITEM_INFO_REC.dest_inv_item_id); -- Bug 3293163
14662 
14663                                 -- redo the check using l_avail_assembly_qty
14664 
14665        				-- Bug 1562754, use G_ASSEMBLY_LINE_ID instead of G_COMP_LINE_ID,
14666 				-- to make sure that in case of CTO, we try to get the BOM
14667 				-- correctly from msc_bom_temp_table.-NGOEL 02/01/2001
14668 
14669 				-- Commented on 01/02/2001 by NGOEL as we are now using
14670 				-- G_ASSEMBLY_LINE_ID instead of G_COMP_LINE_ID for BOM.
14671 
14672                                 -- Bug 1532051, set G_COMP_LINE_ID again to G_ORDER_LINE_ID,
14673                                 -- just to make sure that in case of CTO, we try to get the
14674                                 -- BOM correctly from msc_bom_temp_table.
14675 
14676                                 IF PG_DEBUG in ('Y', 'C') THEN
14677                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'setting G_COMP_LINE_ID = '||MSC_ATP_PVT.G_ASSEMBLY_LINE_ID);
14678                                 END IF;
14679 
14680                                 MSC_ATP_PVT.G_COMP_LINE_ID := MSC_ATP_PVT.G_ASSEMBLY_LINE_ID;
14681 
14682                                 IF ((l_atp_comp_flag = 'R') OR
14683                                   (l_atp_comp_flag = 'C')) THEN
14684                                   -- get resource requirements first
14685                                   IF PG_DEBUG in ('Y', 'C') THEN
14686                                      msc_sch_wb.atp_debug('ATP_Check: ' || 'l_avail_assembly_qty := ' || l_avail_assembly_qty);
14687                                   END IF;
14688 
14689 				  -- dsting for nocopy changes
14690                                   l_res_demand := l_avail_PO_qty; -- 2869830
14691 				  --l_res_demand := l_avail_assembly_qty;
14692 
14693                                   --(ssurendr) Bug 2865389. call get_process_effectivity
14694                                   -- again since quantity has changed. Routing
14695                                   -- and bill seq id could change in this case.
14696                                   /* ship_rec_cal
14697                                   l_opm_start_date := MSC_CALENDAR.date_offset(p_atp_record.organization_id,
14698                                                         p_atp_record.instance_id,
14699                                                         1,
14700                                                         l_requested_ship_date,
14701                                                         -1 * (nvl(PF_ITEM_INFO_REC.fixed_lt,0) +
14702                                                               nvl(PF_ITEM_INFO_REC.variable_lt,0) * l_res_demand));*/
14703                                   l_opm_start_date := MSC_CALENDAR.date_offset(p_atp_record.manufacturing_cal_code,
14704                                                         p_atp_record.instance_id,
14705                                                         l_requested_ship_date,
14706                                                         -1 * (nvl(PF_ITEM_INFO_REC.fixed_lt,0) +
14707                                                               nvl(PF_ITEM_INFO_REC.variable_lt,0) * l_res_demand), -1);
14708                                   MSC_ATP_PROC.get_process_effectivity(
14709                                           p_plan_id,
14710                                           --PF_ITEM_INFO_REC.dest_inv_item_id,
14711                                           C_ITEM_INFO_REC.dest_inv_item_id, -- For time_phased_atp
14712                                           p_atp_record.organization_id,
14713                                           p_atp_record.instance_id,
14714                                           l_opm_start_date,
14715                                           l_res_demand,
14716                                           l_process_seq_id,
14717                                           l_routing_seq_id,
14718                                           l_bill_seq_id,
14719                                           l_op_seq_id, --4570421
14720                                           l_return_status);
14721                                      -- check the return status
14722                                   IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
14723                                       RAISE FND_API.G_EXC_ERROR;
14724         	                  END IF;
14725                                   MSC_ATP_REQ.Get_Res_Requirements(
14726                                     p_atp_record.instance_id,
14727                                     p_plan_id,
14728                                     p_level+2,
14729                                     p_scenario_id,
14730                                     --p_atp_record.inventory_item_id,
14731                                	    l_request_item_id, -- Pass member item's id to make sure member item's id is
14732                                	                       -- inserted in assembly_item_id column in msc_resource_requirements
14733                                     p_atp_record.organization_id,
14734                                     l_pegging_id,
14735                                     l_res_demand,
14736                                     l_requested_ship_date,
14737                                     p_refresh_number,
14738                                     p_atp_record.insert_flag,
14739                                     p_search,
14740                                     p_atp_record.demand_class,
14741                                      --(ssurendr) Bug 2865389 Added Routing Sequence id
14742                                      --and Bill sequence id for OPM fix.
14743                                     l_routing_seq_id,
14744                                     l_bill_seq_id,
14745                                     p_atp_record.ship_date, -- Bug 2814872
14746                                     p_atp_record.demand_source_line,   -- CTO ODR
14747                                     l_avail_assembly_qty,
14748 			            l_res_atp_date,
14749                                     l_res_atp_period,
14750                                     l_res_atp_supply_demand,
14751                                     l_return_status );
14752 
14753                                    -- check the return status
14754                                    IF l_return_status <>
14755                                	       FND_API.G_RET_STS_SUCCESS THEN
14756                               	       RAISE FND_API.G_EXC_ERROR;
14757                                    END IF;
14758 
14759                                 END IF;
14760 
14761                                 IF --((l_atp_comp_flag = 'C') OR
14762                                   ((l_atp_comp_flag = 'C' AND nvl(l_avail_assembly_qty,0) <> 0) OR  --5283809
14763                                   (l_atp_comp_flag = 'Y')) THEN
14764                                   -- get component requirements first
14765                                   BEGIN
14766                                         MSC_ATP_REQ.Get_Comp_Requirements(
14767                                                 p_atp_record.instance_id,
14768                                                 p_plan_id,
14769                                                 p_level+2,
14770                                                 p_scenario_id,
14771                                                 l_request_item_id, -- for time_phased_atp
14772                                                 p_atp_record.organization_id,
14773                                                 l_pegging_id,
14774                                                 p_atp_record.demand_class,
14775                                                 LEAST(l_PO_qty, l_avail_assembly_qty), -- 2754446
14776                                                 --LEAST(l_net_demand, l_avail_assembly_qty),
14777                                                 l_requested_ship_date,
14778                                                 p_refresh_number,
14779                                                 p_atp_record.insert_flag,
14780                                                 p_search,
14781                                                 p_assign_set_id,
14782                                                 --(ssurendr) Bug 2865389 Added Routing Sequence id
14783                                                 --and Bill sequence id for OPM fix.
14784                                                 l_routing_seq_id,
14785                                                 l_bill_seq_id,
14786                                                 p_atp_record.inventory_item_id,      -- For time_phased_atp
14787                                                 p_atp_record.atf_date,               -- For time_phased_atp
14788                                                 p_atp_record.manufacturing_cal_code, -- For ship_rec_cal
14789                                                 l_avail_assembly_qty,
14790                                                 l_comp_atp_date,
14791                                                 l_comp_atp_period,
14792                                                 l_comp_atp_supply_demand,
14793                                                 l_return_status,
14794                                                 --s_cto_rearch
14795                                                 l_get_comp_info_rec,
14796                                                 -- CTO Phase 1 #16, #17
14797                                                 p_atp_record.order_number,
14798                                                 l_op_seq_id --4570421
14799                                                 );
14800                                                  -- Allow creation of Sales Order for CTO comps.
14801                                   -- 2400614 : krajan
14802                                   EXCEPTION
14803                                         -- Raising exception handled below
14804                                         WHEN MSC_ATP_PVT.G_ATO_SOURCING_MISMATCH THEN
14805                                                 IF PG_DEBUG in ('Y', 'C') THEN
14806                                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'Sourcing Mismatch Error');
14807                                                 END IF;
14808                                                 p_atp_record.error_code := MSC_ATP_PVT.ASSIGN_SET_NOT_IN_SYNC;
14809                                                 RAISE MSC_ATP_PVT.G_ATO_SOURCING_MISMATCH;
14810 
14811                                         -- dsting 2764213
14812                                         WHEN MSC_ATP_PVT.EXC_NO_PLAN_FOUND THEN
14813                                              IF PG_DEBUG in ('Y', 'C') THEN
14814                                                 msc_sch_wb.atp_debug('ATP_Check: Plan not found Error');
14815                                              END IF;
14816                                              --24x7
14817                                              if (NVL(MSC_ATP_PVT.G_DOWNTIME_HIT, 'N') = 'Y') then
14818                                                 --bug 2854351:
14819                                                 --p_atp_record.error_code := MSC_ATP_PVT.TRY_ATP_LATER;
14820                                                 p_atp_record.error_code := MSC_ATP_PVT.PLAN_DOWN_TIME;
14821                                              else
14822                                                 p_atp_record.error_code := MSC_ATP_PVT.PLAN_NOT_FOUND;
14823                                              end if;
14824                                              RAISE MSC_ATP_PVT.EXC_NO_PLAN_FOUND;
14825                                         -- krajan : 2752705
14826                                         WHEN MSC_ATP_PVT.G_EXC_UNCOLLECTED_ITEM THEN
14827                                              IF PG_DEBUG in ('Y', 'C') THEN
14828                                                 msc_sch_wb.atp_debug('ATP_Check: Uncollected Item Found');
14829                                              END IF;
14830                                              p_atp_record.error_code := MSC_ATP_PVT.ATP_ITEM_NOT_COLLECTED;
14831                                              RAISE MSC_ATP_PVT.G_EXC_UNCOLLECTED_ITEM;
14832                                         WHEN MSC_ATP_PVT.NO_MATCHING_DATE_IN_CAL THEN --bug3583705
14833                                              IF PG_DEBUG in ('Y', 'C') THEN
14834                                                 msc_sch_wb.atp_debug ('ATP_Check:NO_MATCHING_DATE_IN_CAL');
14835                                              END IF;
14836                                              p_atp_record.error_code := MSC_ATP_PVT.NO_MATCHING_CAL_DATE;
14837                                              RAISE MSC_ATP_PVT.NO_MATCHING_DATE_IN_CAL;
14838                                   END;
14839 
14840                                   IF l_return_status = MSC_ATP_PVT.CTO_OSS_ERROR THEN
14841 
14842                                         RAISE MSC_ATP_PVT.INVALID_OSS_SOURCE;
14843 
14844                                   ELSIF l_return_status <>
14845                                     FND_API.G_RET_STS_SUCCESS THEN
14846                                     RAISE FND_API.G_EXC_ERROR;
14847                                   END IF;
14848 
14849                                 END IF;
14850 
14851                                 -- move the supply/demand, period atp
14852                                 -- of the resources to the output record of
14853                                 -- tables.
14854 
14855                                 -- bug 1277418
14856                                 -- we need to make sure the l_avail_assembly_qty
14857                                 -- in this second round because it is possible
14858                                 -- that due to lead time we check different
14859                                 -- comp/res that may not have enough capacities.
14860 
14861                                 IF l_avail_assembly_qty > 0 THEN
14862                                   MSC_ATP_PROC.Details_Output(l_comp_atp_period,
14863                                                l_comp_atp_supply_demand,
14864                                                x_atp_period,
14865                                                x_atp_supply_demand,
14866                                                l_return_status);
14867 
14868                                   MSC_ATP_PROC.Details_Output(l_res_atp_period,
14869                                                l_res_atp_supply_demand,
14870                                                x_atp_period,
14871                                                x_atp_supply_demand,
14872                                                l_return_status);
14873                                 ELSE
14874 
14875                                   MSC_ATP_DB_UTILS.Update_Planned_Order(l_pegging_id,
14876                                                    p_plan_id,
14877                                                    null,
14878                                                    0,
14879                                                    null,
14880                                                    null,
14881                                                    null,
14882                                                    null,  -- Bug 3241766
14883                                                    --3755704: Pass start date and order date
14884                                                    l_start_date,
14885                                                    l_order_date,
14886                                                    --null,  -- Bug 3241766
14887                                                    --null,  -- Bug 3241766
14888                                                    C_ITEM_INFO_REC.dest_inv_item_id,   -- Bug 3293163
14889                                                    PF_ITEM_INFO_REC.dest_inv_item_id); -- Bug 3293163
14890                                 END IF;
14891                               ELSE
14892                                 -- bug 1277418: now clean up the data.
14893                                 -- we remove the whole planned order
14894                                 MSC_ATP_DB_UTILS.Update_Planned_Order(l_pegging_id,
14895                                                    p_plan_id,
14896                                                    null,
14897                                                    0,
14898                                                    null,
14899                                                    null,
14900                                                    null,
14901                                                    null,  -- Bug 3241766
14902                                                    --bug 3755704: Pass start date and order date
14903                                                    l_start_date,
14904                                                    l_order_date,
14905                                                    --null,  -- Bug 3241766
14906                                                    --null,  -- Bug 3241766
14907                                                    C_ITEM_INFO_REC.dest_inv_item_id,   -- Bug 3293163
14908                                                    PF_ITEM_INFO_REC.dest_inv_item_id); -- Bug 3293163
14909                                  MSC_ATP_PVT.G_SUMMARY_FLAG := l_summary_flag;
14910                               END IF; -- end if l_avail_assembly_qty > 0
14911                             ELSE
14912                               -- move the supply/demand, period atp
14913                               -- of the components to the output record of
14914                               -- tables.
14915                               MSC_ATP_PROC.Details_Output(l_comp_atp_period,
14916                                              l_comp_atp_supply_demand,
14917                                              x_atp_period,
14918                                              x_atp_supply_demand,
14919                                              l_return_status);
14920 
14921                               MSC_ATP_PROC.Details_Output(l_res_atp_period,
14922                                              l_res_atp_supply_demand,
14923                                              x_atp_period,
14924                                              x_atp_supply_demand,
14925                                              l_return_status);
14926 
14927 
14928                             END IF;  -- end if the undo stuff
14929                             IF PG_DEBUG in ('Y', 'C') THEN
14930                                msc_sch_wb.atp_debug('ATP_Check: ' || 'OSFM Coproducts Backward');
14931                             END IF;
14932 			    --- OSFM Changes: update coproduct supply
14933 			    l_coproducts_flag := FND_Profile.value('MSC_PLAN_COPRODUCTS');
14934                             IF PG_DEBUG in ('Y', 'C') THEN
14935                                msc_sch_wb.atp_debug('ATP_Check: ' || 'msc_plan_coproducts = ' || l_coproducts_flag);
14936                             END IF;
14937                             --- set the sumary flag back to summary flag for p_plan_id
14938                             MSC_ATP_PVT.G_SUMMARY_FLAG := l_summary_flag;
14939                             IF ((l_atp_comp_flag = 'C') OR
14940                                (l_atp_comp_flag = 'Y')) AND
14941                                l_coproducts_flag = 'Y'  AND
14942                                l_avail_assembly_qty >0 THEN
14943 
14944  				IF PG_DEBUG in ('Y', 'C') THEN
14945  				   msc_sch_wb.atp_debug('ATP_Check: ' || 'Add Coproducts for item '||p_atp_record.inventory_item_id);
14946  				END IF;
14947 			 	MSC_ATP_PROC.Add_Coproducts(
14948                                     p_plan_id,
14949                                     p_atp_record.instance_id,
14950                                     p_atp_record.organization_id,
14951                                     --bug 3766179: pass request item as inventory_item_id contains
14952                                     --PF's id in case of PF based ATP
14953                                     --p_atp_record.inventory_item_id,
14954                                     l_request_item_id,
14955                                     l_requested_ship_date,
14956                                     p_atp_record.demand_class,
14957                                     l_avail_assembly_qty,
14958                                     l_coprod_pegging_id,
14959                                     C_ITEM_INFO_REC.rounding_control_type, -- 2869830
14960                                     p_refresh_number,    -- For summary enhancement
14961                                     l_transaction_id --3766179
14962                                 );
14963 	              		IF PG_DEBUG in ('Y', 'C') THEN
14964 	              		   msc_sch_wb.atp_debug('ATP_Check: ' || 'After adding coproducts Backward');
14965 	              		END IF;
14966 
14967         		    END IF;
14968                             l_net_demand := l_net_demand-l_avail_assembly_qty;
14969 
14970                             IF l_requested_ship_date <= p_atp_record.atf_date THEN
14971                                 p_atp_record.atf_date_quantity := p_atp_record.atf_date_quantity + l_avail_assembly_qty;
14972                             END IF;
14973 
14974                             IF l_net_demand <= 0 THEN
14975                                 l_net_demand := 0;
14976                                 EXIT;
14977                             END IF;
14978                         ---diag_atp
14979                         ELSIF MSC_ATP_PVT.G_DIAGNOSTIC_ATP <> 1 THEN
14980                             IF PG_DEBUG in ('Y', 'C') THEN
14981                                msc_sch_wb.atp_debug('ATP_Check: ' || 'inside forward_scheduling');
14982                             END IF;
14983                             -- p_sched_flag = FORWARD_SCHEDULING
14984 
14985                             -- initialize the atp date
14986                             -- although we set to sysdate now, each individual
14987                             -- routine will give the right date (or null).
14988 
14989                             l_res_atp_date := l_requested_ship_date;
14990                             l_comp_atp_date := l_requested_ship_date;
14991 
14992 				-- Bug 1554159, donot change G_COMP_LINE_ID here
14993                                 -- Bug 1532051, set G_COMP_LINE_ID again to G_ORDER_LINE_ID,
14994                                 -- just to make sure that in case of CTO, we try to get the
14995                                 -- BOM correctly from msc_bom_temp_table.
14996 
14997                                 --msc_sch_wb.atp_debug('setting G_COMP_LINE_ID = '||MSC_ATP_PVT.G_ORDER_LINE_ID);
14998 
14999                                 --MSC_ATP_PVT.G_COMP_LINE_ID := MSC_ATP_PVT.G_ORDER_LINE_ID;
15000 
15001                             -- dsting 2754446 adjust l_net_demand according to rounding_control_type
15002                             -- so our planned orders turn out right for the future case.
15003                             IF NVL(C_ITEM_INFO_REC.rounding_control_type, 2) = 1 THEN
15004                               --l_PO_qty := CEIL(l_net_demand);
15005                               --bug3397904 start
15006                               --bug 3904063: Reset the quantity only in case of demand priority allocated ATP
15007                               IF (p_search = FORWARD_SCHEDULING)
15008                                  AND MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y'
15009                                  AND MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1
15010                                  AND MSC_ATP_PVT.G_ALLOCATION_METHOD = 1 THEN
15011                                l_PO_qty := CEIL(p_atp_record.quantity_ordered);
15012                                IF PG_DEBUG in ('Y', 'C') THEN
15013                                 msc_sch_wb.atp_debug('If l_PO_qty: ' || l_PO_qty);
15014                                END IF;
15015                               ELSE
15016                                l_PO_qty := CEIL(l_net_demand);
15017                                IF PG_DEBUG in ('Y', 'C') THEN
15018                                 msc_sch_wb.atp_debug('Else l_PO_qty: ' || l_PO_qty);
15019                                END IF;
15020                               END IF;
15021                             ELSE
15022                             --bug 3904063: Reset the quantity only in case of demand priority allocated ATP
15023                               IF (p_search = FORWARD_SCHEDULING)
15024                                  AND MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y'
15025                                  AND MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1
15026                                  AND MSC_ATP_PVT.G_ALLOCATION_METHOD = 1 THEN
15027                                l_PO_qty := trunc(p_atp_record.quantity_ordered, 6);	--5598066
15028                                IF PG_DEBUG in ('Y', 'C') THEN
15029                                 msc_sch_wb.atp_debug('If l_PO_qty: ' || l_PO_qty);
15030                                END IF;
15031                               ELSE
15032                                l_PO_qty := trunc(l_net_demand, 6);	--5598066
15033                                IF PG_DEBUG in ('Y', 'C') THEN
15034                                 msc_sch_wb.atp_debug('Else l_PO_qty: ' || l_PO_qty);
15035                                END IF;
15036                               END IF;
15037                             END IF;
15038                             --bug3397904 end
15039                             IF ((l_atp_comp_flag = 'R') OR
15040                                 (l_atp_comp_flag = 'C')) THEN
15041                                  -- get resource requirements first
15042                                 IF PG_DEBUG in ('Y', 'C') THEN
15043                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'inside atp_comp_flag R or C');
15044                                 END IF;
15045 
15046                                 --(ssurendr) Bug 2865389 Call procedure get_process_effectivity
15047                                 --Details. Derive Item's Start Date by Offsetting
15048                                 --Due date with LT. Check effectivity on Start Date.
15049                                 /* ship_rec_cal
15050                                 l_opm_start_date := MSC_CALENDAR.date_offset(p_atp_record.organization_id,
15051                                                         p_atp_record.instance_id,
15052                                                         1,
15053                                                         l_requested_ship_date,
15054                                                         -1 * (nvl(PF_ITEM_INFO_REC.fixed_lt,0) +
15055                                                               nvl(PF_ITEM_INFO_REC.variable_lt,0) * l_PO_qty));*/
15056                                 l_opm_start_date := MSC_CALENDAR.date_offset(p_atp_record.manufacturing_cal_code,
15057                                                         p_atp_record.instance_id,
15058                                                         l_requested_ship_date,
15059                                                         -1 * (nvl(PF_ITEM_INFO_REC.fixed_lt,0) +
15060                                                               nvl(PF_ITEM_INFO_REC.variable_lt,0) * l_PO_qty), -1);
15061                                 MSC_ATP_PROC.get_process_effectivity(
15062                                         p_plan_id,
15063                                         --PF_ITEM_INFO_REC.dest_inv_item_id,
15064                                         C_ITEM_INFO_REC.dest_inv_item_id, -- For time_phased_atp
15065                                         p_atp_record.organization_id,
15066                                         p_atp_record.instance_id,
15067                                         l_opm_start_date,
15068                                         l_PO_qty,
15069                                         l_process_seq_id,
15070                                         l_routing_seq_id,
15071                                         l_bill_seq_id,
15072                                         l_op_seq_id, --4570421
15073                                         l_return_status);
15074                                 -- check the return status
15075                                 IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
15076                                     RAISE FND_API.G_EXC_ERROR;
15077         	                END IF;
15078                                 MSC_ATP_REQ.Get_Res_Requirements(
15079                                     p_atp_record.instance_id,
15080                                     p_plan_id,
15081                                     p_level+2,
15082                                     p_scenario_id,
15083                                     --p_atp_record.inventory_item_id,
15084                                	    l_request_item_id, -- Pass member item's id to make sure member item's id is
15085                                	                       -- inserted in assembly_item_id column in msc_resource_requirements
15086                                     p_atp_record.organization_id,
15087                                     l_pegging_id,
15088                                     l_PO_qty, -- 2754446
15089                                     --l_net_demand,
15090                                     l_requested_ship_date,
15091                                     p_refresh_number,
15092                                     p_atp_record.insert_flag,
15093                                     p_search,
15094                                     p_atp_record.demand_class,
15095                                     --(ssurendr) Bug 2865389 Added Routing Sequence id
15096                                     --and Bill sequence id for OPM fix.
15097                                     l_routing_seq_id,
15098                                     l_bill_seq_id,
15099                                     p_atp_record.ship_date, -- Bug 2814872
15100                                     p_atp_record.demand_source_line,   -- CTO ODR
15101                                     l_avail_assembly_qty,
15102                                     l_res_atp_date,
15103                                     l_res_atp_period,
15104                                     l_res_atp_supply_demand,
15105                                     l_return_status );
15106 
15107                                 -- check the return status
15108                                 -- Error Handling Changes krajan
15109                                  IF l_return_status <>
15110                                	       FND_API.G_RET_STS_SUCCESS THEN
15111                               	       RAISE FND_API.G_EXC_ERROR;
15112                                  END IF;
15113 
15114                                  IF PG_DEBUG in ('Y', 'C') THEN
15115                                     msc_sch_wb.atp_debug('ATP_Check: ' || 'l_avail_assembly_qty = '||l_avail_assembly_qty);
15116                                     msc_sch_wb.atp_debug('ATP_Check: ' || 'l_res_atp_date = '||l_res_atp_date);
15117                                  END IF;
15118 
15119 				 -- Bug 1608755, in case l_res_atp_date is null, set l_comp_atp_date
15120 				 -- to NULL so that we return single level better than going down.
15121                             	 IF l_comp_atp_date is NOT NULL and l_res_atp_date is NULL THEN
15122                                     l_comp_atp_date := NULL;
15123                                  END IF;
15124 
15125                             END IF;
15126 
15127                             -- Do comp check if res_atp_date is less than parent's infinite time fence.
15128                             -- Bug 2814872
15129                             IF ((l_atp_comp_flag = 'C' AND l_res_atp_date is not null AND--5211286
15130                                 --l_res_atp_date < p_atp_record.ship_date ) OR
15131                                 l_res_atp_date < NVL(p_atp_record.ship_date,l_res_atp_date + 1) AND nvl(l_avail_assembly_qty,0) <> 0 ) OR --5283809
15132                                 (l_atp_comp_flag = 'Y')) AND
15133                                 l_res_atp_date is not null THEN
15134                                  -- we addd the  l_res_atp_date is not null
15135                                  -- condition to avoid checking components
15136                                  -- if we don't have resources availability.
15137 
15138                                  -- get component requirements first
15139 
15140                                 IF PG_DEBUG in ('Y', 'C') THEN
15141                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'inside atp_comp_flag Y or C');
15142                                 END IF;
15143                                 BEGIN
15144                                     MSC_ATP_REQ.Get_Comp_Requirements(
15145                                         p_atp_record.instance_id,
15146                                         p_plan_id,
15147                                         p_level+2,
15148                                         p_scenario_id,
15149                                         l_request_item_id, -- for time_phased_atp
15150                                         p_atp_record.organization_id,
15151                                         l_pegging_id,
15152                                         p_atp_record.demand_class,
15153                                         l_PO_qty, -- 2754446
15154                                         --l_net_demand,
15155                                         l_requested_ship_date,
15156   				        p_refresh_number,
15157                                         p_atp_record.insert_flag,
15158                                         p_search,
15159                                         p_assign_set_id,
15160                                         --(ssurendr) Bug 2865389 Added Routing Sequence id
15161                                         --and Bill sequence id for OPM fix.
15162                                         l_routing_seq_id,
15163                                         l_bill_seq_id,
15164                                         p_atp_record.inventory_item_id,      -- For time_phased_atp
15165                                         p_atp_record.atf_date,               -- For time_phased_atp
15166                                         p_atp_record.manufacturing_cal_code, -- For ship_rec_cal
15167                                         l_avail_assembly_qty,
15168                                         l_comp_atp_date,
15169                                         l_comp_atp_period,
15170                                         l_comp_atp_supply_demand,
15171                                         l_return_status,
15172                                         --s_cto_rearch
15173                                         l_get_comp_info_rec,
15174                                         -- CTO Phase 1 #16, #17
15175                                         p_atp_record.order_number,
15176                                         l_op_seq_id --4570421
15177                                         );
15178                                         -- Allow creation of Sales Order for CTO comps.
15179                                  -- 2400614 : krajan
15180                                  EXCEPTION
15181                                         WHEN MSC_ATP_PVT.G_ATO_SOURCING_MISMATCH THEN
15182                                              p_atp_record.error_code := MSC_ATP_PVT.ASSIGN_SET_NOT_IN_SYNC;
15183                               	             IF PG_DEBUG in ('Y', 'C') THEN
15184                               	                msc_sch_wb.atp_debug('ATP_Check: ' || 'Source Mismatch Error');
15185                               	             END IF;
15186 	                                     RAISE MSC_ATP_PVT.G_ATO_SOURCING_MISMATCH;
15187 
15188                                         -- dsting 2764213
15189                                         WHEN MSC_ATP_PVT.EXC_NO_PLAN_FOUND THEN
15190                                              IF PG_DEBUG in ('Y', 'C') THEN
15191                                                 msc_sch_wb.atp_debug('ATP_Check: Plan not found Error');
15192                                              END IF;
15193                                              --24x7
15194                                              if (NVL(MSC_ATP_PVT.G_DOWNTIME_HIT, 'N') = 'Y') then
15195                				        --bug 2854351
15196                                                 --p_atp_record.error_code := MSC_ATP_PVT.TRY_ATP_LATER;
15197                                                 p_atp_record.error_code := MSC_ATP_PVT.PLAN_DOWN_TIME;
15198                                              else
15199                                                 p_atp_record.error_code := MSC_ATP_PVT.PLAN_NOT_FOUND;
15200                                              end if;
15201                                              RAISE MSC_ATP_PVT.EXC_NO_PLAN_FOUND;
15202 
15203                                         -- krajan : 2752705
15204                                         WHEN MSC_ATP_PVT.G_EXC_UNCOLLECTED_ITEM THEN
15205                                              IF PG_DEBUG in ('Y', 'C') THEN
15206                                                 msc_sch_wb.atp_debug('ATP_Check: Uncollected Item');
15207                                              END IF;
15208                                              p_atp_record.error_code := MSC_ATP_PVT.ATP_ITEM_NOT_COLLECTED;
15209                                              RAISE MSC_ATP_PVT.G_EXC_UNCOLLECTED_ITEM;
15210 
15211                                         WHEN MSC_ATP_PVT.NO_MATCHING_DATE_IN_CAL THEN --bug3583705
15212                                              IF PG_DEBUG in ('Y', 'C') THEN
15213                                                 msc_sch_wb.atp_debug ('ATP_Check:NO_MATCHING_DATE_IN_CAL');
15214                                              END IF;
15215                                              p_atp_record.error_code := MSC_ATP_PVT.NO_MATCHING_CAL_DATE;
15216                                              RAISE MSC_ATP_PVT.NO_MATCHING_DATE_IN_CAL;
15217                                  END;
15218 
15219                                  IF PG_DEBUG in ('Y', 'C') THEN
15220                                     msc_sch_wb.atp_debug('ATP_Check: ' || 'l_avail_assembly_qty = '||l_avail_assembly_qty);
15221                                     msc_sch_wb.atp_debug('ATP_Check: ' || 'l_comp_atp_date = '||l_comp_atp_date);
15222                                  END IF;
15223 
15224 				 -- Bug 1608755, in case l_comp_atp_date is null, set l_res_atp_date
15225 				 -- to NULL so that we return single level better than going down.
15226                             	 IF l_comp_atp_date is NULL and l_res_atp_date is NOT NULL THEN
15227                                     l_res_atp_date := NULL;
15228                                  END IF;
15229 
15230                             END IF;
15231 
15232                             -- check the return status
15233                             IF l_return_status = MSC_ATP_PVT.CTO_OSS_ERROR THEN
15234 
15235                                  RAISE MSC_ATP_PVT.INVALID_OSS_SOURCE;
15236                             ELSIF l_return_status <>
15237                                    FND_API.G_RET_STS_SUCCESS THEN
15238                                 RAISE FND_API.G_EXC_ERROR;
15239                             END IF;
15240 
15241                             IF PG_DEBUG in ('Y', 'C') THEN
15242                                msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atp_date_this_level ='||l_atp_date_this_level);
15243                                msc_sch_wb.atp_debug('ATP_Check: ' || 'l_comp_atp_date ='||l_comp_atp_date);
15244                                msc_sch_wb.atp_debug('ATP_Check: ' || 'l_res_atp_date ='||l_res_atp_date);
15245                                msc_sch_wb.atp_debug('ATP_Check: ' || 'p_atp_record.ship_date ='||p_atp_record.ship_date);
15246                             END IF;
15247 
15248                             -- Bug 1449555
15249                             IF l_comp_atp_date is NOT NULL and
15250                                 l_res_atp_date is NOT NULL THEN
15251                                 l_temp_date := GREATEST(l_comp_atp_date, l_res_atp_date);
15252                             ELSIF l_comp_atp_date is NOT NULL and
15253                                 l_res_atp_date is NULL THEN
15254                                 l_temp_date := l_comp_atp_date;
15255                             ELSIF l_comp_atp_date is NULL and
15256                                 l_res_atp_date is NOT NULL THEN
15257                                 l_temp_date := l_res_atp_date;
15258                             ELSE
15259                                 l_temp_date := p_atp_record.ship_date;
15260                             END IF;
15261 
15262                             -- 2859130 move to next work date for forward pass
15263                             l_temp_date := MSC_CALENDAR.NEXT_WORK_DAY(
15264                                p_atp_record.organization_id,
15265                                p_atp_record.instance_id,
15266                                1,
15267                                l_temp_date);
15268 
15269                             ---bug 2798667: The p_atp_record.ship_date for option item contains ship date for
15270                             --- model from earlier source + atp lead time. We should compare with ship date +
15271                             --- atp lead time from this source and not just ship date
15272                             /* s_cto_rearch: do not honor ATP lead time
15273                             IF NVL(p_atp_record.atp_lead_time, 0) > 0 THEN
15274                               l_atp_lt_offset_ship_date :=
15275                                   MSC_CALENDAR.DATE_OFFSET(
15276                                   p_atp_record.organization_id,
15277                                   p_atp_record.instance_id,
15278                                   1,
15279                                   --GREATEST(l_comp_atp_date, l_res_atp_date),
15280                                   l_temp_date,
15281                                   NVL(p_atp_record.atp_lead_time, 0));
15282                             ELSE
15283                                l_atp_lt_offset_ship_date := l_temp_date;
15284                             END IF;
15285                             */
15286                             l_atp_lt_offset_ship_date := l_temp_date;
15287 
15288                             IF PG_DEBUG in ('Y', 'C') THEN
15289                                msc_sch_wb.atp_debug('l_atp_lt_offset_ship_date := ' || l_atp_lt_offset_ship_date);
15290                             END IF;
15291 
15292 
15293                             ---bug 2798667: should be comparing with l_atp_lt_offset_ship_date instead of l_temp_date
15294                              /*IF (p_atp_record.ship_date IS NULL AND l_temp_date IS NULL) OR
15295                                 (p_atp_record.ship_date <= l_temp_date) THEN
15296                             */
15297                             IF (p_atp_record.ship_date IS NULL AND l_atp_lt_offset_ship_date IS NULL) OR
15298                                 (p_atp_record.ship_date <= l_atp_lt_offset_ship_date) THEN
15299                 /*
15300                             IF p_atp_record.ship_date <=
15301                                 NVL(GREATEST(l_comp_atp_date, l_res_atp_date),
15302                                 p_atp_record.ship_date) THEN
15303                 */
15304 
15305                                 -- single level result is better than
15306                                 -- going down to next level.
15307 
15308                                 IF PG_DEBUG in ('Y', 'C') THEN
15309                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'single level results is better than multi-level');
15310                                 END IF;
15311 
15312 
15313                                 MSC_ATP_DB_UTILS.Update_Planned_Order(l_pegging_id,
15314                                                  p_plan_id,
15315                                                  p_atp_record.ship_date,
15316                                                  0,
15317                                                  null,
15318                                                  null,
15319                                                  null,
15320                                                  null,  -- Bug 3241766
15321                                                  --bug 3755704: Pass start date and order date
15322                                                  l_start_date,
15323                                                  l_order_date,
15324                                                  --null,  -- Bug 3241766
15325                                                  --null,  -- Bug 3241766
15326                                                  C_ITEM_INFO_REC.dest_inv_item_id,   -- Bug 3293163
15327                                                  PF_ITEM_INFO_REC.dest_inv_item_id); -- Bug 3293163
15328                                 IF PG_DEBUG in ('Y', 'C') THEN
15329                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'x_atp_supply_demand.level.count = '||
15330 						x_atp_supply_demand.level.count);
15331                                 END IF;
15332 
15333                             ELSE
15334  				-- going down is better than the results
15335                                 -- in this level
15336 
15337                                 -- remove what's bad
15338                                 --- subst: remember this pegging as we might need to undo
15339                                 --- this pegging in ATP_CHECK_SUBST
15340                                 MSC_ATP_PVT.G_FUTURE_PEGGING_ID := l_pegging_id;
15341 
15342                                 IF PG_DEBUG in ('Y', 'C') THEN
15343                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atp_pegging_tab : ' || l_atp_pegging_tab.COUNT);
15344                                 END IF;
15345 
15346                                 IF l_atp_pegging_tab.COUNT > 0 THEN
15347 
15348                                    -- Planned Pegging based AATP, l_atp_pegging_tab populated in
15349                                    -- MSC_AATP_REQ.Get_Forward_Material_Atp
15350 
15351                                    MSC_ATP_DB_UTILS.Remove_Invalid_Future_SD(l_atp_pegging_tab);
15352                                    l_atp_pegging_tab := MRP_ATP_PUB.Number_Arr(); --bug3581151
15353 
15354                                 ELSE            -- IF l_atp_pegging_tab.COUNT > 0 THEN
15355 
15356                                    MSC_ATP_DB_UTILS.Update_Planned_Order(l_future_pegging_id,
15357                                                  p_plan_id,
15358                                                  p_atp_record.ship_date,
15359                                                  0,
15360                                                  null,
15361                                                  null,
15362                                                  null,
15363                                                  null,  -- Bug 3241766
15364                                                  --bug 3755704: Pass start date and order date
15365                                                  l_start_date,
15366                                                  l_order_date,
15367                                                  --null,  -- Bug 3241766
15368                                                  --null,  -- Bug 3241766
15369                                                  C_ITEM_INFO_REC.dest_inv_item_id,   -- Bug 3293163
15370                                                  PF_ITEM_INFO_REC.dest_inv_item_id); -- Bug 3293163
15371                                 END IF;            -- IF l_atp_pegging_tab.COUNT > 0 THEN
15372 
15373                                 --bug 2798667: This value is already in l_atp_lt_offset_ship_date
15374                                 /* p_atp_record.ship_date :=
15375                                   MSC_CALENDAR.DATE_OFFSET(
15376                                   p_atp_record.organization_id,
15377                                   p_atp_record.instance_id,
15378                                   1,
15379                                   --GREATEST(l_comp_atp_date, l_res_atp_date),
15380                                   l_temp_date,
15381                                   NVL(p_atp_record.atp_lead_time, 0));
15382                                 */
15383                                 p_atp_record.ship_date := l_atp_lt_offset_ship_date;
15384 
15385 
15386                                 IF PG_DEBUG in ('Y', 'C') THEN
15387                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'p_atp_record.ship_date ='||p_atp_record.ship_date);
15388                                 END IF;
15389                                 -- we will say the available quantity is
15390                                 -- exact the quantity we want to order.
15391                                 -- although this is not correct (
15392                                 -- it should be the quantity we get from next
15393                                 -- level plus what we can have on the parent
15394                                 -- level.  But now we don't want to search
15395 
15396                                 p_atp_record.available_quantity :=
15397                                   p_atp_record.quantity_ordered;
15398 
15399                                 IF p_atp_record.ship_date <= p_atp_record.atf_date THEN
15400                                     p_atp_record.atf_date_quantity := p_atp_record.quantity_ordered;
15401                                 ELSE
15402                                     p_atp_record.atf_date_quantity := 0;
15403                                 END IF;
15404 
15405                                 -- bug 1247433: we don't need to update
15406                                 -- the quantity
15407 
15408                                 --bug 3755704: Calculate order date and start date based on new date
15409 
15410                                 IF l_net_processing_lt > 0 THEN
15411                                    IF PG_DEBUG in ('Y', 'C') THEN
15412                                       msc_sch_wb.atp_debug('ATP_Check: ' || 'Calculate order date');
15413                                    END IF;
15414                                    -- since it is a make case only calendar being used is
15415                                    --p_atp_record.manufacturing_cal_code
15416                                    l_order_date := MSC_CALENDAR.DATE_OFFSET(
15417                                                                 p_atp_record.manufacturing_cal_code,
15418                                                                 p_atp_record.instance_id,
15419                                                                 l_temp_date,
15420                                                                 (-1 * l_net_processing_lt), -1);
15421 
15422                                 ELSE
15423                                    IF PG_DEBUG in ('Y', 'C') THEN
15424                                        msc_sch_wb.atp_debug('ATP_Check: ' || 'Net process lt is zero');
15425                                    END IF;
15426                                    l_order_date := l_temp_date;
15427                                 END IF;
15428 
15429                                 IF PG_DEBUG in ('Y', 'C') THEN
15430                                     msc_sch_wb.atp_debug('ATP_Check: ' || 'l_order_date := ' || l_order_date);
15431                                 END IF;
15432                                 -- calculate start date only if PTF_flag is enabled for the plan
15433                                 IF (l_process_lt > 0) THEN
15434                                    IF PG_DEBUG in ('Y', 'C') THEN
15435                                       msc_sch_wb.atp_debug('ATP_Check: ' || 'Calculate start date');
15436                                    END IF;
15437                                    l_start_date := MSC_CALENDAR.DATE_OFFSET(
15438                                                                 p_atp_record.manufacturing_cal_code,
15439                                                                 p_atp_record.instance_id,
15440                                                                 l_temp_date,
15441                                                                 (-1 * l_process_lt), -1);
15442                                 ELSE
15443 
15444                                    IF PG_DEBUG in ('Y', 'C') THEN
15445                                       msc_sch_wb.atp_debug('ATP_Check: ' || 'process LT is 0');
15446                                    END IF;
15447                                    l_start_date := l_temp_date;
15448                                 END IF;
15449 
15450                                 IF PG_DEBUG in ('Y', 'C') THEN
15451                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'l_start_date := ' || l_start_date);
15452                                 END IF;
15453                                 --  end bug 3755704
15454 
15455                                 MSC_ATP_DB_UTILS.Update_Planned_Order(l_pegging_id,
15456                                                  p_plan_id,
15457                                                  --bug 2798667: this should be l_temp_date
15458                                                  --p_atp_record.ship_date,
15459                                                  l_temp_date,
15460                                                  null,
15461                                                  null,
15462                                                  null,
15463                                                  null,
15464                                                  null,  -- Bug 3241766
15465                                                  --bug 3755704: populate start date and order date
15466                                                  l_start_date,
15467                                                  l_order_date,
15468                                                  --null,  -- Bug 3241766
15469                                                  --null,  -- Bug 3241766
15470                                                  C_ITEM_INFO_REC.dest_inv_item_id,   -- Bug 3293163
15471                                                  PF_ITEM_INFO_REC.dest_inv_item_id); -- Bug 3293163
15472 
15473                                 -- bug 1214740
15474                                 l_future_atp_period := l_comp_atp_period;
15475                                 l_future_atp_supply_demand :=
15476                                                  l_comp_atp_supply_demand;
15477                                 l_future_pegging_id := l_pegging_id;
15478 
15479                                 MSC_ATP_PROC.Details_Output(l_res_atp_period,
15480                                                l_res_atp_supply_demand,
15481                                                l_future_atp_period,
15482                                                l_future_atp_supply_demand,
15483                                                l_return_status);
15484  			        IF PG_DEBUG in ('Y', 'C') THEN
15485  			           msc_sch_wb.atp_debug('ATP_Check: ' || 'OSFM Coproducts Forward ');
15486  			        END IF;
15487                                 --- reset the sumamry flag back to summary flag corresponding to p_plan_id
15488                                 MSC_ATP_PVT.G_SUMMARY_FLAG := l_summary_flag;
15489                             	  --- OSFM Changes: update coproduct supply
15490                                 l_coproducts_flag := FND_Profile.value('MSC_PLAN_COPRODUCTS');
15491                                 IF PG_DEBUG in ('Y', 'C') THEN
15492                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'msc_plan_coproducts = ' || l_coproducts_flag);
15493                                 END IF;
15494                                 IF ((l_atp_comp_flag = 'C') OR
15495                                 	(l_atp_comp_flag = 'Y')) AND
15496                                	l_coproducts_flag = 'Y' THEN
15497 
15498                               	  IF PG_DEBUG in ('Y', 'C') THEN
15499                               	     msc_sch_wb.atp_debug('ATP_Check: ' || 'Add Coproducts for item '||p_atp_record.inventory_item_id);
15500                               	  END IF;
15501                               	  MSC_ATP_PROC.Add_Coproducts(
15502                                                 p_plan_id,
15503                                                 p_atp_record.instance_id,
15504                                                 p_atp_record.organization_id,
15505                                                 --p_atp_record.inventory_item_id,
15506                                                 l_request_item_id, -- for time_phased_atp
15507                                                 l_temp_date,
15508                                                 p_atp_record.demand_class,
15509                                                 p_atp_record.quantity_ordered,
15510                                                 l_coprod_pegging_id,
15511                                                 C_ITEM_INFO_REC.rounding_control_type, -- 2869830
15512                                                 p_refresh_number,    -- For summary enhancement
15513                                                 l_transaction_id --3766179
15514                                    );
15515 
15516                                    IF PG_DEBUG in ('Y', 'C') THEN
15517                                       msc_sch_wb.atp_debug('ATP_Check: ' || 'After adding coproducts Forward');
15518                                    END IF;
15519                             	  END IF;
15520 
15521                              END IF;
15522 
15523                         END IF; -- (p_sched_flag = BACKWARD_SCHEDULING)
15524 
15525                     END IF;  -- it is a make or buy or transfer
15526                     j := l_sources.source_type.NEXT(j);
15527                 END LOOP;  -- end the loop of the l_sources
15528                 --4902658/5216528 Fix
15529                                 /*
15530                  Update the pegging with the ATP+CTP Quantity of the pegging.
15531                  l_temp_net_demand -  l_net_demand; gives us the CTP quantity
15532                  */
15533                 IF  l_substitutes_rec.inventory_item_id.Count  > 1 THEN
15534                    l_atp_ctp_qty := l_temp_net_demand -  l_net_demand +  l_substitutes_rec.sub_atp_qty(l_index);
15535 
15536                    IF ( ((l_temp_net_demand - l_net_demand) <> 0)
15537                       AND (l_substitutes_rec.sub_atp_qty(l_index) = 0)
15538                       AND (l_index >= 2) ) THEN
15539 
15540                    --5283809 , adding the demand in case ATP qty for substitute is 0 but CTP not zero.
15541                    IF PG_DEBUG in ('Y', 'C') THEN
15542                       msc_sch_wb.atp_debug('ATP_Check: ' || 'adding the demand in case ATP qty for substitute is 0 but CTP not zero');
15543                       msc_sch_wb.atp_debug('ATP_Check: ' || 'ATP QTY : ' || l_substitutes_rec.sub_atp_qty(l_index) );
15544                       msc_sch_wb.atp_debug('ATP_Check: ' || 'CTP QTY : ' || l_atp_ctp_qty );
15545                    END IF;
15546 
15547 
15548                    IF NVL(MSC_ATP_PVT.G_DIAGNOSTIC_ATP,2) <> 1 THEN
15549                       MSC_ATP_DB_UTILS.Update_Pegging( l_substitutes_rec.pegging_id(l_index),
15550                                                        null,
15551                                                        l_atp_ctp_qty );
15552                    END IF;
15553 
15554                    l_atp_insert_rec.instance_id := p_atp_record.instance_id;
15555                    l_atp_insert_rec.inventory_item_id := l_substitutes_rec.pf_item_id(l_index);
15556                    l_atp_insert_rec.request_item_id := l_substitutes_rec.inventory_item_id(l_index);
15557                    l_atp_insert_rec.atf_date_quantity := l_substitutes_rec.atf_date_quantity(l_index);
15558                    l_atp_insert_rec.organization_id := p_atp_record.organization_id;
15559                    l_atp_insert_rec.identifier := p_atp_record.identifier;
15560                    l_atp_insert_rec.demand_source_type:=
15561                           nvl(p_atp_record.demand_source_type, 2);
15562                    l_atp_insert_rec.demand_source_header_id :=
15563                             nvl(p_atp_record.demand_source_header_id, -1);
15564                    l_atp_insert_rec.demand_source_delivery :=
15565                             p_atp_record.demand_source_delivery;
15566                    l_atp_insert_rec.quantity_ordered:= LEAST(
15567                                                             l_atp_ctp_qty,
15568                                                             l_substitutes_rec.quantity_ordered(l_index));
15569                    l_atp_insert_rec.requested_ship_date := p_atp_record.requested_ship_date;
15570                    l_atp_insert_rec.demand_class := p_atp_record.demand_class;
15571                    l_atp_insert_rec.refresh_number := p_refresh_number;
15572                    l_atp_insert_rec.order_number := p_atp_record.order_number;
15573                    l_atp_insert_rec.origination_type := 1;
15574 
15575                    MSC_ATP_DB_UTILS.Add_Mat_Demand(l_atp_insert_rec,
15576                               -- l_plan_id, use p_plan_id in case of Make CTP.
15577                               nvl(l_plan_id,p_plan_id),    -- bug#10428207
15578                               0,
15579                               l_demand_id);
15580                    IF PG_DEBUG in ('Y', 'C') THEN
15581                       msc_sch_wb.atp_debug('ATP_Check : demand id     :' || l_demand_id );
15582                    END IF;
15583 
15584                    --5553953
15585                    --in order to update the demand id for the pegging line, otherwise demand will remain in the system
15586                    UPDATE mrp_atp_details_temp
15587                    SET   identifier3 = l_demand_id
15588                    WHERE pegging_id = l_substitutes_rec.pegging_id(l_index)
15589                    AND   session_id = MSC_ATP_PVT.G_SESSION_ID
15590                    AND   record_type = 3;
15591 
15592                  ELSE
15593                      IF NVL(MSC_ATP_PVT.G_DIAGNOSTIC_ATP,2) <> 1 THEN
15594                    	  MSC_ATP_DB_UTILS.Update_Pegging( l_substitutes_rec.pegging_id(l_index),
15595                                   null,
15596                                   l_atp_ctp_qty );
15597                      END IF;
15598                      update msc_demands
15599                         set    USING_REQUIREMENT_QUANTITY = MSC_ATP_UTILS.Truncate_Demand(l_atp_ctp_qty)  -- 5598066
15600                       where  demand_id = l_substitutes_rec.demand_id(l_index)
15601                         and    plan_id = p_plan_id
15602                                returning inventory_item_id
15603                        into l_inventory_item_id;
15604                    IF PG_DEBUG in ('Y', 'C') THEN
15605                       msc_sch_wb.atp_debug('ATP_Check : l_atp_ctp_qty :' || l_atp_ctp_qty);
15606                       msc_sch_wb.atp_debug('ATP_Check : l_tem_net_dem :' || l_temp_net_demand);
15607                       msc_sch_wb.atp_debug('ATP_Check : l_net_demand  :' || l_net_demand);
15608                       msc_sch_wb.atp_debug('ATP_Check : l_sub_atp_qty :' || l_substitutes_rec.sub_atp_qty(l_index));
15609                       msc_sch_wb.atp_debug('ATP_Check : pegging_id    :' || l_substitutes_rec.pegging_id(l_index));
15610                       msc_sch_wb.atp_debug('ATP_Check : demand id     :' || l_substitutes_rec.demand_id(l_index));
15611                    END IF;
15612                    --5217510 demand id should be used from substitute record
15613                    IF MSC_ATP_PVT.G_INV_CTP = 4 and MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y'
15614                         AND MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1 AND MSC_ATP_PVT.G_ALLOCATION_METHOD = 1 THEN
15615                         update msc_alloc_demands
15616                            set allocated_quantity = l_atp_ctp_qty
15617                          where parent_demand_id = l_substitutes_rec.demand_id(l_index) --l_demand_id
15618                                --where parent_demand_id = l_demand_id
15619                            and plan_id = p_plan_id;
15620                    END IF;
15621                    IF PG_DEBUG in ('Y', 'C') THEN
15622                       msc_sch_wb.atp_debug('ATP_Check: ' || 'l_inventory_item_id := ' || l_inventory_item_id);
15623                       msc_sch_wb.atp_debug('ATP_Check: ' || ' MSC_ATP_PVT.G_SUMMARY_FLAG := ' ||  MSC_ATP_PVT.G_SUMMARY_FLAG);
15624                    END IF;
15625                    -- update summary records removed in ODS case - for summary enhancement
15626                    IF MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y' AND p_plan_id = -1 THEN
15627                       MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(p_atp_record.instance_id,
15628                                                          p_plan_id,
15629                                                          p_atp_record.organization_id,
15630                                                          l_inventory_item_id,
15631                                                          l_requested_ship_date,
15632                                                          null,
15633                                                          null,
15634                                                          null,
15635                                                          null,
15636                                                          null,
15637                                                          null,
15638                                                          2);
15639                         update /*+ INDEX(msc_atp_summary_sd MSC_ATP_SUMMARY_SD_U1) */ msc_atp_summary_sd
15640                            set sd_qty = sd_qty + l_atp_ctp_qty
15641                          where plan_id = p_plan_id
15642                            and sr_instance_id  = p_atp_record.instance_id
15643                            and inventory_item_id = l_inventory_item_id
15644                            and organization_id = p_atp_record.organization_id
15645                            and sd_date = trunc(l_requested_ship_date);
15646                       commit;
15647                    END IF;
15648                    IF PG_DEBUG in ('Y', 'C') THEN
15649                       msc_sch_wb.atp_debug('Substitute Loop Ends with l_index := ' || l_index);
15650                    END IF;
15651                 END IF;
15652                END IF;
15653              END LOOP; -- for subs array
15654 
15655                   p_atp_record.request_item_id :=     l_substitutes_rec.inventory_item_id(1);
15656                   l_parent_pegging_id          :=     l_tmp_parent_pegging_id;
15657                   l_pegging_id                 :=     l_substitutes_rec.inventory_item_id(1);
15658                   -- Re initialize the C_item_info_rec G_item_info_rec
15659                   MSC_ATP_PROC.get_global_item_info(p_atp_record.instance_id,
15660                                                     p_plan_id,
15661                                                     p_atp_record.request_item_id,
15662                                                     p_atp_record.organization_id,
15663                                                     l_item_info_rec );
15664                 		/* Make a copy */
15665                   C_ITEM_INFO_REC := G_ITEM_INFO_REC;
15666 
15667                   if l_substitutes_rec.inventory_item_id.count > 1 then
15668                      PF_ITEM_INFO_REC := G_ITEM_INFO_REC;
15669                   end if;
15670                   -- 4902658/5216528 ends update
15671 		-- dsting diag_atp update the last good pegging record to make supply/demand match
15672 		IF PG_DEBUG in ('Y', 'C') THEN
15673 			msc_sch_wb.atp_debug('diag_atp l_pegging_id ' || l_pegging_id);
15674 			msc_sch_wb.atp_debug('last good pegging ' || l_last_good_pegging_id);
15675 			msc_sch_wb.atp_debug('net_demand ' || l_net_demand);
15676 			msc_sch_wb.atp_debug('subst last good pegging ' || MSC_ATP_SUBST.G_TOP_LAST_PO_PEGGING);
15677 		END IF;
15678 
15679 		IF MSC_ATP_PVT.G_SUBSTITUTION_FLAG = 'Y'
15680 		   AND l_last_good_pegging_id is NULL
15681 		   AND MSC_ATP_SUBST.G_TOP_LAST_PO_PEGGING is not null
15682 		   AND p_atp_record.top_tier_org_flag = 1
15683 		THEN
15684 		   l_last_good_pegging_id := MSC_ATP_SUBST.G_TOP_LAST_PO_PEGGING;
15685 		   l_subst_existing_PO_qty := MSC_ATP_SUBST.G_TOP_LAST_PO_QTY;
15686 		ELSE
15687 		   l_subst_existing_PO_qty := 0;
15688 		END IF;
15689 
15690 		IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 AND
15691 		   l_net_demand > 0 AND
15692 		   l_pegging_id <> l_last_good_pegging_id
15693 		THEN
15694 		   -- bug 5598066   added the following to honour rounding_control_type
15695 		      IF NVL(C_ITEM_INFO_REC.rounding_control_type, 2) = 1 THEN
15696                          l_net_demand_qty := FLOOR(l_net_demand_qty);
15697 			 l_subst_existing_PO_qty := FLOOR(l_subst_existing_PO_qty);
15698 
15699 			 IF PG_DEBUG in ('Y', 'C') THEN
15700 			 msc_sch_wb.atp_debug('Floor of l_net_demand_qty '||l_net_demand_qty);
15701 			 msc_sch_wb.atp_debug('Floor of l_subst_existing_PO_qty '||l_subst_existing_PO_qty);
15702                          END IF;
15703 		       ELSE
15704                            l_net_demand_qty := trunc(l_net_demand_qty, 6);
15705 			   l_subst_existing_PO_qty := trunc(l_subst_existing_PO_qty, 6);
15706 
15707 			   IF PG_DEBUG in ('Y', 'C') THEN
15708 			   msc_sch_wb.atp_debug('trunc of l_net_demand_qty '||l_net_demand_qty);
15709 			   msc_sch_wb.atp_debug('trunc of l_subst_existing_PO_qty '||l_subst_existing_PO_qty);
15710                            END IF;
15711 			END IF;
15712 
15713 			MSC_ATP_DB_UTILS.Update_Planned_Order(l_last_good_pegging_id,
15714 					p_plan_id,
15715 					null,
15716 					l_net_demand + l_subst_existing_PO_qty,
15717 					null,
15718 					null,
15719 					null,
15720                                         null,  -- Bug 3241766
15721                                         null,  -- Bug 3241766
15722                                         null,  -- Bug 3241766
15723                                         C_ITEM_INFO_REC.dest_inv_item_id,   -- Bug 3293163
15724                                         PF_ITEM_INFO_REC.dest_inv_item_id,  -- Bug 3293163
15725 					3);
15726 		END IF;
15727 
15728                 MSC_ATP_PVT.G_SUMMARY_FLAG := l_summary_flag;
15729                 IF PG_DEBUG in ('Y', 'C') THEN
15730                    msc_sch_wb.atp_debug('ATP_Check: ' || 'MSC_ATP_PVT.G_SUMMARY_FLAG := ' || MSC_ATP_PVT.G_SUMMARY_FLAG);
15731                    msc_sch_wb.atp_debug('ATP_Check: ' || 'l_demand_id := ' || l_demand_id); --5211558
15732                 END IF;
15733 
15734                 --- adjust the demand here
15735                 IF (l_net_demand > 0) and (p_search =1) and (p_parent_pegging_id is null) THEN
15736 
15737 		  -- dsting diag_atp. do not adjust the demands for diagnostic ATP
15738 
15739 		  IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP <> 1 THEN
15740                     update msc_demands
15741                     set    USING_REQUIREMENT_QUANTITY =
15742                            MSC_ATP_UTILS.Truncate_Demand((USING_REQUIREMENT_QUANTITY-l_net_demand))  -- 5598066
15743                     where demand_id = l_demand_id
15744                     and plan_id = p_plan_id
15745                     returning  inventory_item_id into l_inventory_item_id;
15746 
15747                     /* time_phased_atp*/
15748                     --5211558 we want to update the demands only in first pass and not in further passes
15749                     IF l_time_phased_atp = 'Y' and MSC_ATP_PVT.G_OPTIONAL_FW is null THEN
15750                          --using the same insert rec we prepared earlier
15751                          l_atp_insert_rec.quantity_ordered :=  p_atp_record.quantity_ordered - l_net_demand;
15752                          l_atp_insert_rec.requested_date_quantity := l_atp_insert_rec.quantity_ordered;
15753                          l_atp_insert_rec.atf_date_quantity := GREATEST(NVL(p_atp_record.atf_date_quantity,
15754                                                                                 0), 0); --7185041
15755                          MSC_ATP_PF.Increment_Bucketed_Demands_Qty(
15756                                 l_atp_insert_rec,
15757                                 p_plan_id,
15758                                 l_demand_id,
15759                                 l_return_status
15760                          );
15761                          IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
15762                            IF PG_DEBUG in ('Y', 'C') THEN
15763                                 msc_sch_wb.atp_debug('ATP_Check: ' || 'Error occured in procedure Increment_Bucketed_Demands_Qty');
15764                            END IF;
15765                            RAISE FND_API.G_EXC_ERROR;
15766                          END IF;
15767                     ELSIF MSC_ATP_PVT.G_INV_CTP = 4 and MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y'
15768                          AND MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1 AND MSC_ATP_PVT.G_ALLOCATION_METHOD = 1 THEN
15769 
15770                          update msc_alloc_demands
15771                          set allocated_quantity  = allocated_quantity -l_net_demand
15772                          where parent_demand_id = l_demand_id
15773                          and   plan_id = p_plan_id;
15774                     END IF;
15775 
15776                     IF PG_DEBUG in ('Y', 'C') THEN
15777                        msc_sch_wb.atp_debug('ATP_Check: ' || 'Check This scenario in backward case');
15778                        msc_sch_wb.atp_debug('ATP_Check: ' || 'MSC_ATP_PVT.G_SUMMARY_FLAG' || MSC_ATP_PVT.G_SUMMARY_FLAG);
15779                     END IF;
15780 
15781                     -- update summary records removed in ODS cases - for summary enhancement
15782                     IF MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y' AND p_plan_id = -1 THEN
15783                         IF PG_DEBUG in ('Y', 'C') THEN
15784                            msc_sch_wb.atp_debug('ATP_Check: ' || 'in sumary mode, update msc-demands');
15785                            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_inventory_item_id := ' || l_inventory_item_id);
15786                            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_requested_ship_date := ' || l_requested_ship_date);
15787                         END IF;
15788 
15789                         MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(p_atp_record.instance_id,
15790                                                                 p_plan_id,
15791                                                                 p_atp_record.organization_id,
15792                                                                 l_inventory_item_id,
15793                                                                 l_requested_ship_date,
15794                                                                 null,
15795                                                                 null,
15796                                                                 null,
15797                                                                 null,
15798 							        null,
15799  							        null,
15800                                                                 2);
15801 
15802                         update /*+ INDEX(msc_atp_summary_sd MSC_ATP_SUMMARY_SD_U1) */ msc_atp_summary_sd
15803                         set sd_qty = sd_qty + l_net_demand
15804                         where plan_id = p_plan_id
15805                         and  inventory_item_id = l_inventory_item_id
15806                         and  sr_instance_id = p_atp_record.instance_id
15807                         and  organization_id = p_atp_record.organization_id
15808                         and  sd_date = trunc(l_requested_ship_date);
15809                         commit;
15810                     END IF;
15811 
15812    		  END IF; -- not DIAGNOSTIC ATP
15813 
15814                 END IF;
15815 
15816                 -- now we have the request date quantity from next level
15817 
15818                 -- cchen : 1238941
15819                 -- p_atp_record.requested_date_quantity:=
15820                 --     p_atp_record.quantity_ordered-l_net_demand;
15821 
15822                 p_atp_record.combined_requested_date_qty:=
15823                     p_atp_record.quantity_ordered-l_net_demand;
15824 
15825                 p_atp_record.requested_date_quantity:=
15826                     p_atp_record.quantity_ordered-l_net_demand-
15827                     greatest(l_sub_requested_date_quantity,0);
15828 
15829                /* rajjain changes for re-review comments
15830                   removed check for rule based AATP as this should be set
15831                   for all scenarios
15832                IF MSC_ATP_PVT.G_ALLOCATION_METHOD = 2 THEN
15833                    p_atp_record.used_available_quantity :=
15834                      l_used_available_quantity;
15835                END IF; --bug3409973*/
15836                 p_atp_record.used_available_quantity :=
15837                     l_used_available_quantity;
15838 
15839                 IF PG_DEBUG in ('Y', 'C') THEN
15840                    msc_sch_wb.atp_debug('ATP_Check: ' || 'p_atp_record.requested_date_quantity:'||
15841                     	p_atp_record.requested_date_quantity);
15842                    msc_sch_wb.atp_debug('ATP_Check: ' || 'p_atp_record.combined_requested_date_qty:'||
15843 			p_atp_record.combined_requested_date_qty);
15844 	           msc_sch_wb.atp_debug('ATP_Check: ' || 'p_atp_record.atp_date_quantity_this_level:'||
15845 			l_used_available_quantity);
15846                 END IF;
15847 
15848                 -- now we want to find the future date and quantity
15849                 -- if we still have net demand, otherwise the atp date
15850                 -- and quantity will be the same as the request date and
15851                 -- request date quantity.
15852 
15853 
15854                 IF (l_net_demand = 0) THEN
15855                     /* s_cto_rearch: Do not honor atp lead time
15856                     p_atp_record.ship_date :=  MSC_CALENDAR.DATE_OFFSET
15857                                  (p_atp_record.organization_id,
15858                                   p_atp_record.instance_id,
15859                                   1,
15860                                   l_requested_ship_date,
15861                                   NVL(p_atp_record.atp_lead_time, 0));
15862                     */
15863                     p_atp_record.ship_date := l_requested_ship_date;
15864                     -- cchen : 1238941
15865                     -- p_atp_record.available_quantity :=
15866                     --    p_atp_record.requested_date_quantity;
15867 
15868                     p_atp_record.available_quantity :=
15869                         p_atp_record.combined_requested_date_qty;
15870 
15871                     -- Update bucketed demands
15872                     --5211558 we want to update the demands only in first pass and last pass
15873                     -- and not in any mid passes as this will be exit point
15874                     IF l_time_phased_atp = 'Y' and (((MSC_ATP_PVT.G_OPTIONAL_FW is null
15875                                                       OR G_NUMBER_OF_ITERATIONS = G_LOOP_COUNT)
15876                                                       and G_FORWARD_ATP = 'Y') OR
15877                                                       (G_FORWARD_ATP = 'N'))THEN
15878                             l_atp_insert_rec.quantity_ordered :=  p_atp_record.quantity_ordered;
15879                             l_atp_insert_rec.requested_date_quantity := p_atp_record.combined_requested_date_qty;
15880                             l_atp_insert_rec.atf_date_quantity := GREATEST(NVL(p_atp_record.atf_date_quantity,
15881                                                                                    0), 0); --7185041
15882                             MSC_ATP_PF.Increment_Bucketed_Demands_Qty(
15883                                    l_atp_insert_rec,
15884                                    p_plan_id,
15885                                    l_demand_id,
15886                                    l_return_status
15887                             );
15888                             IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
15889                               IF PG_DEBUG in ('Y', 'C') THEN
15890                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'Error occured in procedure Increment_Bucketed_Demands_Qty');
15891                               END IF;
15892                               RAISE FND_API.G_EXC_ERROR;
15893                             END IF;
15894                     END IF;
15895                 ELSE
15896                     -- if this is the top node, we need to turn on a flag so
15897                     -- that we can find the future date.
15898 
15899                     IF p_search = 1 AND p_parent_pegging_id is null THEN
15900                       IF PG_DEBUG in ('Y', 'C') THEN
15901                          msc_sch_wb.atp_debug('ATP_Check: ' || 'not enough, set G_FIND_FUTURE_DATE=Y');
15902                       END IF;
15903 
15904                       -- 1275751
15905                       p_atp_record.error_code := ATP_REQ_DATE_FAIL;
15906 
15907                       -- 1228607
15908                       --- This code has been moved before stealing
15909                       ---  so that demand record is correct before stealing takes place
15910                       /*update msc_demands
15911                       set    USING_REQUIREMENT_QUANTITY =
15912                              (USING_REQUIREMENT_QUANTITY-l_net_demand)
15913                       where demand_id = l_demand_id
15914                       and plan_id = p_plan_id;*/
15915 
15916                       MSC_ATP_PVT.G_FIND_FUTURE_DATE := 'Y';
15917                       MSC_ATP_PVT.G_ASSEMBLY_LINE_ID := p_atp_record.Identifier;
15918                       MSC_ATP_PVT.G_COMP_LINE_ID := p_atp_record.Identifier;
15919                     ELSIF p_search = 1 AND
15920                       --subst: In case of substitution we do not want to adjust the planned order for top org
15921                       NOT (NVL(p_atp_record.original_item_flag, 2) = 1 AND
15922                                     MSC_ATP_PVT.G_SUBSTITUTION_FLAG = 'Y' AND
15923                                     p_atp_record.top_tier_org_flag = 1) THEN
15924                       --diag_atp: we do not adjust the demand in case of diagnostic ATP, because we place the
15925                       -- same demand as we need.
15926                       IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP <> 1 THEN
15927                          -- adjust the demand record
15928                          IF PG_DEBUG in ('Y', 'C') THEN
15929                             msc_sch_wb.atp_debug('ATP_Check: ' || 'Check this scenario, bacward case 2');
15930                             msc_sch_wb.atp_debug('ATP_Check: ' || 'adjust demand record, l_parent_pegging_id = '||l_parent_pegging_id);
15931                          END IF;
15932                          /* bug 1235225
15933                          update mrp_atp_details_temp
15934                          set    supply_demand_quantity =
15935                                 (supply_demand_quantity - l_net_demand)
15936                          where  session_id = MSC_ATP_PVT.G_SESSION_ID
15937                          and    pegging_id = l_parent_pegging_id
15938                          and    record_type = 3;
15939                          */
15940 
15941                          SELECT supply_demand_quantity
15942                          INTO   l_peg_qty
15943                          FROM   mrp_atp_details_temp
15944                          WHERE  session_id = MSC_ATP_PVT.G_SESSION_ID
15945                          AND    pegging_id = l_parent_pegging_id
15946                          AND    record_type = 3;
15947 
15948                          MSC_ATP_DB_UTILS.Update_Pegging(l_parent_pegging_id,
15949                                         null,
15950                                         (l_peg_qty - l_net_demand));
15951 
15952                          IF PG_DEBUG in ('Y', 'C') THEN
15953                             msc_sch_wb.atp_debug('ATP_Check: ' || 'adjust demand quantity, l_demand_id = '||l_demand_id);
15954                          END IF;
15955 
15956 			-- dsting diag_atp do not adjust the demands for diagnostic atp
15957 			IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP <> 1 THEN
15958 
15959                          update msc_demands
15960                          set    USING_REQUIREMENT_QUANTITY =
15961                                 MSC_ATP_UTILS.Truncate_Demand((USING_REQUIREMENT_QUANTITY-l_net_demand)) -- 5598066
15962                          where demand_id = l_demand_id
15963                          and plan_id = p_plan_id
15964                          returning inventory_item_id into l_inventory_item_id;
15965 
15966                         /* time_phased_atp
15967                            Update PF Bucketed Demands to qty we could find in backward case*/
15968                         --5211558 we want to update the demands only in first pass and not in further passes
15969                         IF l_time_phased_atp = 'Y' and MSC_ATP_PVT.G_OPTIONAL_FW is null THEN
15970                                 MSC_ATP_PF.Update_PF_Bucketed_Demands(
15971                                         p_plan_id,
15972                                         l_demand_id,
15973                                         l_requested_ship_date,
15974                                         p_atp_record.atf_date,
15975                                         p_atp_record.quantity_ordered,
15976                                         p_atp_record.combined_requested_date_qty,
15977                                         l_return_status
15978                                 );
15979                                 IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
15980                                    IF PG_DEBUG in ('Y', 'C') THEN
15981                                         msc_sch_wb.atp_debug('ATP_Check: ' || 'Error occured in procedure Update_PF_Bucketed_Demands');
15982                                    END IF;
15983                                    RAISE FND_API.G_EXC_ERROR;
15984                                 END IF;
15985                          --alloc_pp
15986                          ELSIF MSC_ATP_PVT.G_INV_CTP = 4 and MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y'
15987                              AND MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1 AND MSC_ATP_PVT.G_ALLOCATION_METHOD = 1 THEN
15988 
15989                               update msc_alloc_demands
15990                               set allocated_quantity = allocated_quantity - l_net_demand
15991                               where parent_demand_id = l_demand_id
15992                               and   plan_id = p_plan_id;
15993                          END IF;
15994 
15995                          IF PG_DEBUG in ('Y', 'C') THEN
15996                             msc_sch_wb.atp_debug('ATP_Check: ' || 'Check this Flag MSC_ATP_PVT.G_SUMMARY_FLAG := ' || MSC_ATP_PVT.G_SUMMARY_FLAG);
15997                          END IF;
15998 
15999                         -- update summary records removed in ODS cases - for summary enhancement
16000                          IF MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y' AND p_plan_id = -1 THEN
16001                               IF PG_DEBUG in ('Y', 'C') THEN
16002                                  msc_sch_wb.atp_debug('ATP_Check: ' || 'update sd table for component');
16003                                  msc_sch_wb.atp_debug('ATP_Check: ' || 'in sumary mode, update msc-demands');
16004                                  msc_sch_wb.atp_debug('ATP_Check: ' || 'l_inventory_item_id := ' || l_inventory_item_id);
16005                                  msc_sch_wb.atp_debug('ATP_Check: ' || 'l_requested_ship_date := ' || l_requested_ship_date);
16006                               END IF;
16007 
16008                               MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(p_atp_record.instance_id,
16009                                                                    p_plan_id,
16010                                                                    p_atp_record.organization_id,
16011                                                                    l_inventory_item_id,
16012                                                                    l_requested_ship_date,
16013                                                                    null,
16014                                                                    null,
16015                                                                    null,
16016                                                                    null,
16017 								   null,
16018 								   null,
16019                                                                    2);
16020 
16021                               update /*+ INDEX(msc_atp_summary_sd MSC_ATP_SUMMARY_SD_U1) */ msc_atp_summary_sd
16022                               set sd_qty = sd_qty + l_net_demand
16023                               where plan_id = p_plan_id
16024                               and  inventory_item_id = l_inventory_item_id
16025                               and  sr_instance_id = p_atp_record.instance_id
16026                               and  organization_id = p_atp_record.organization_id
16027                               and  sd_date = trunc(l_requested_ship_date);
16028                               commit;
16029                          END IF;
16030 
16031 			END IF; -- not DIAGNOSTIC ATP
16032                       END IF;
16033                     ---subst
16034                     --ELSE
16035                     ELSIF p_search = 2 THEN
16036 
16037 
16038                       IF PG_DEBUG in ('Y', 'C') THEN
16039                          msc_sch_wb.atp_debug('ATP_Check: ' || 'forward l_net_demand = '||l_net_demand);
16040                          msc_sch_wb.atp_debug('ATP_Check: ' || 'forward p_atp_record.ship_date = '||p_atp_record.ship_date);
16041                          msc_sch_wb.atp_debug('ATP_Check: ' || 'forward p_atp_record.quantity_ordered = '||
16042 						p_atp_record.quantity_ordered);
16043                          msc_sch_wb.atp_debug('ATP_Check: ' || 'Check this scenario, forward case');
16044                       END IF;
16045                       -- future case
16046 
16047                       -- bug 1302394: we should adjust the date
16048                       -- of the demand which causes this planned order
16049                       -- as well so that they can cancel each other.
16050 
16051                       -- bug 1531429 : if p_atp_record.ship_date is null, update
16052 		      -- USING_ASSEMBLY_DEMAND_DATE with same value
16053 
16054                       -- 1421413: don't update the quantity
16055 
16056                      IF p_atp_record.ship_date IS NOT NULL THEN
16057                          -- Update only if p_atp_record.ship_date is NOT NULL
16058 
16059 			-- dsting ATO 2465370
16060 			-- for override case for CTO, do not adjust demand date in msc_demands
16061                         --bug 3564511: always update the date on PO demands
16062 		        --IF NVL(G_LINE_OVERRIDE_FLAG, 'N') = 'N'
16063 			   --OR (NVL(C_ITEM_INFO_REC.cto_source_org_id, -1) <> p_atp_record.organization_id
16064 			   --AND p_parent_pegging_id is not null)
16065 			--THEN
16066                                 --bug 3328421: update actual date
16067 				--update mrp_atp_details_temp set required_date =
16068 				update mrp_atp_details_temp set actual_supply_demand_date =
16069 					TRUNC(p_atp_record.ship_date) + MSC_ATP_PVT.G_END_OF_DAY
16070 				where session_id = MSC_ATP_PVT.G_SESSION_ID
16071 				and pegging_id = l_parent_pegging_id
16072 				and record_type = 3;
16073 
16074 				IF PG_DEBUG in ('Y','C') THEN
16075 					msc_sch_wb.atp_debug('ATO update details_temp 12: ' || l_parent_pegging_id || ' date: ' || p_atp_record.ship_date);
16076 				END IF;
16077 
16078                 	        UPDATE msc_demands
16079         	                --SET  USING_ASSEMBLY_DEMAND_DATE = NVL(p_atp_record.ship_date,
16080 			        --					USING_ASSEMBLY_DEMAND_DATE)
16081                         	-- For bug 2259824, move the demand to the end of day
16082                         	----start changes for plan by request date
16083                         	SET    USING_ASSEMBLY_DEMAND_DATE =
16084                         	           DECODE(ORIGINATION_TYPE,
16085                         	                  6,  decode(MSC_ATP_PVT.G_PLAN_INFO_REC.schedule_by_date_type,
16086                         	                             MSC_ATP_PVT.G_SCHEDULE_SHIP_DATE_LEGEND,
16087                         	                                 TRUNC(p_atp_record.ship_date) + MSC_ATP_PVT.G_END_OF_DAY,
16088                         	                             MSC_ATP_PVT.G_SCHEDULE_ARRIVAL_DATE_LEGEND,
16089                         	                                 TRUNC(p_atp_record.ship_date) + MSC_ATP_PVT.G_END_OF_DAY,
16090                         	                             MSC_ATP_PVT.G_PROMISE_SHIP_DATE_LEGEND,
16091                         	                                 TRUNC(p_atp_record.ship_date) + MSC_ATP_PVT.G_END_OF_DAY,
16092                         	                             MSC_ATP_PVT.G_PROMISE_ARRIVAL_DATE_LEGEND,
16093                         	                                 TRUNC(p_atp_record.ship_date) + MSC_ATP_PVT.G_END_OF_DAY,
16094                         	                             MSC_ATP_PVT.G_REQUEST_SHIP_DATE_LEGEND,
16095                         	                                 l_original_req_ship_date,
16096                         	                             MSC_ATP_PVT.G_REQUEST_ARRIVAL_DATE_LEGEND,
16097                         	                                 l_original_req_ship_date,
16098                         	                                 TRUNC(p_atp_record.ship_date) + MSC_ATP_PVT.G_END_OF_DAY),
16099                         	                  30, decode(MSC_ATP_PVT.G_PLAN_INFO_REC.schedule_by_date_type,
16100                         	                             MSC_ATP_PVT.G_SCHEDULE_SHIP_DATE_LEGEND,
16101                         	                                 TRUNC(p_atp_record.ship_date) + MSC_ATP_PVT.G_END_OF_DAY,
16102                         	                             MSC_ATP_PVT.G_SCHEDULE_ARRIVAL_DATE_LEGEND,
16103                         	                                 TRUNC(p_atp_record.ship_date) + MSC_ATP_PVT.G_END_OF_DAY,
16104                         	                             MSC_ATP_PVT.G_PROMISE_SHIP_DATE_LEGEND,
16105                         	                                 TRUNC(p_atp_record.ship_date) + MSC_ATP_PVT.G_END_OF_DAY,
16106                         	                             MSC_ATP_PVT.G_PROMISE_ARRIVAL_DATE_LEGEND,
16107                         	                                 TRUNC(p_atp_record.ship_date) + MSC_ATP_PVT.G_END_OF_DAY,
16108                         	                             MSC_ATP_PVT.G_REQUEST_SHIP_DATE_LEGEND,
16109                         	                                 l_original_req_ship_date,
16110                         	                             MSC_ATP_PVT.G_REQUEST_ARRIVAL_DATE_LEGEND,
16111                         	                                 l_original_req_ship_date,
16112                         	                                 TRUNC(p_atp_record.ship_date) + MSC_ATP_PVT.G_END_OF_DAY),
16113                         	                  TRUNC(p_atp_record.ship_date) + MSC_ATP_PVT.G_END_OF_DAY),
16114                         	       schedule_ship_date =
16115                         	           DECODE(ORIGINATION_TYPE,
16116                         	                  6,  TRUNC(p_atp_record.ship_date) + MSC_ATP_PVT.G_END_OF_DAY,
16117                         	                  30, TRUNC(p_atp_record.ship_date) + MSC_ATP_PVT.G_END_OF_DAY,
16118                         	                  NULL),
16119                         	       promise_ship_date =
16120                         	           DECODE(ORIGINATION_TYPE,
16121                         	                  6,  TRUNC(p_atp_record.ship_date) + MSC_ATP_PVT.G_END_OF_DAY,
16122                         	                  30, TRUNC(p_atp_record.ship_date) + MSC_ATP_PVT.G_END_OF_DAY,
16123                         	                  NULL)
16124                 		--end changes for plan by request date
16125                 		WHERE  demand_id = l_demand_id
16126 	                        AND    plan_id = p_plan_id
16127                         	returning inventory_item_id into l_inventory_item_id;
16128                 	        IF PG_DEBUG in ('Y', 'C') THEN
16129                                    msc_sch_wb.atp_debug('demand_id: ' || l_demand_id);
16130         	                   msc_sch_wb.atp_debug('ATP_Check: ' || 'number of rows updated := ' || SQL%ROWCOUNT);
16131 	                        END IF;
16132 
16133                                 /* time_phased_atp*/
16134                                 --5211558 not needed here as this is executed in forward pass
16135                                 IF l_time_phased_atp = 'Y' THEN
16136                                         MSC_ATP_PF.Move_PF_Bucketed_Demands(
16137                                                 p_plan_id,
16138                                                 l_demand_id,
16139                                                 p_atp_record.requested_ship_date,
16140                                                 p_atp_record.ship_date,
16141                                                 NULL,
16142                                                 NULL,
16143                                                 p_atp_record.atf_date,
16144                                                 NULL, -- pass atf_date_qty as null
16145                                                 l_return_status,
16146                                                 NULL --bug3397904
16147                                         );
16148                                         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
16149                                                 IF PG_DEBUG in ('Y', 'C') THEN
16150                                                         msc_sch_wb.atp_debug('ATP_Check: ' || 'Error occured in procedure Move_PF_Bucketed_Demands');
16151                                                 END IF;
16152                                                 RAISE FND_API.G_EXC_ERROR;
16153                                         END IF;
16154                          	--alloc_pp
16155                          	ELSIF MSC_ATP_PVT.G_INV_CTP = 4 and MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y'
16156                             	   AND MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1 AND MSC_ATP_PVT.G_ALLOCATION_METHOD = 1 THEN
16157                                    update msc_alloc_demands
16158                                    set demand_date  = p_atp_record.ship_date
16159                                    where parent_demand_id = l_demand_id
16160                                    and   plan_id = p_plan_id;
16161                          	END IF;
16162 
16163                          	IF PG_DEBUG in ('Y', 'C') THEN
16164                             	   msc_sch_wb.atp_debug('ATP_Check: ' || 'inventory_item_id := ' || l_inventory_item_id);
16165                          	END IF;
16166                             -- update summary records removed in ODS cases - for summary enhancement
16167                             IF (MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y') AND (  SQL%ROWCOUNT > 1) AND p_plan_id = -1 THEN
16168 
16169                                 IF PG_DEBUG in ('Y', 'C') THEN
16170                                     msc_sch_wb.atp_debug('ATP_Check: ' || 'old and new dates are  not same');
16171                                     msc_sch_wb.atp_debug('ATP_Check: ' || 'Update summary for old date');
16172                                 END IF;
16173                                 MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(p_atp_record.instance_id,
16174                                                                         p_plan_id,
16175                                                                         p_atp_record.organization_id,
16176                                                                         l_inventory_item_id,
16177                                                                         l_requested_ship_date,
16178                                                                         null,
16179                                                                         null,
16180                                                                         null,
16181                                                                         null,
16182                                                                         null,
16183                                                                         null,
16184                                                                         2);
16185 
16186 
16187                                 UPDATE /*+ INDEX(msc_atp_summary_sd MSC_ATP_SUMMARY_SD_U1) */ MSC_ATP_SUMMARY_SD
16188                                 set sd_qty = (sd_qty + p_atp_record.quantity_ordered)
16189                                 where sr_instance_id = p_atp_record.instance_id and
16190                                 inventory_item_id = l_inventory_item_id and
16191                                 organization_id = p_atp_record.organization_id and
16192                                 sd_date = trunc(l_requested_ship_date) and
16193                                 plan_id = p_plan_id;
16194 
16195                                 commit;
16196 
16197                                 --- if record exists then update
16198                                 IF PG_DEBUG in ('Y', 'C') THEN
16199                                     msc_sch_wb.atp_debug('ATP_Check: ' || 'update the qty on new date ');
16200                                 END IF;
16201                                 MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(p_atp_record.instance_id,
16202                                                                         p_plan_id,
16203                                                                         p_atp_record.organization_id,
16204                                                                         l_inventory_item_id,
16205                                                                         p_atp_record.ship_date,
16206                                                                         null,
16207                                                                         null,
16208                                                                         null,
16209                                                                         null,
16210                                                                         null,
16211                                                                         null,
16212                                                                         2);
16213 
16214                                 update /*+ INDEX(msc_atp_summary_sd MSC_ATP_SUMMARY_SD_U1) */ msc_atp_summary_sd
16215                                 set sd_qty = sd_qty - p_atp_record.quantity_ordered
16216                                 where plan_id = p_plan_id and
16217                                 sr_instance_id = p_atp_record.instance_id and
16218                                 inventory_item_id = l_inventory_item_id and
16219                                 organization_id = p_atp_record.organization_id and
16220                                 sd_date = trunc(p_atp_record.ship_date);
16221 
16222                                 IF SQL%NOTFOUND THEN
16223                                     --  record doesn't exists. insert the record
16224                                     IF PG_DEBUG in ('Y', 'C') THEN
16225                                         msc_sch_wb.atp_debug('ATP_Check: ' || 'insert qty on new date');
16226                                         msc_sch_wb.atp_debug('ATP_Check: ' || 'plan_id := ' || p_plan_id);
16227                                         msc_sch_wb.atp_debug('ATP_Check: ' || 'l_inventory_item_id := ' || l_inventory_item_id);
16228                                         msc_sch_wb.atp_debug('ATP_Check: ' || 'organization_id := ' || p_atp_record.organization_id);
16229                                         msc_sch_wb.atp_debug('ATP_Check: ' || 'instance_id := ' || p_atp_record.instance_id);
16230                                         msc_sch_wb.atp_debug('ATP_Check: ' || 'ship_date := ' || p_atp_record.ship_date);
16231                                         msc_sch_wb.atp_debug('ATP_Check: ' || 'quantity_ordered := ' || p_atp_record.quantity_ordered);
16232                                     END IF;
16233                                     BEGIN
16234                                         MSC_ATP_DB_UTILS.INSERT_SUMMARY_SD_ROW(p_plan_id,
16235                                         p_atp_record.instance_id,
16236                                         p_atp_record.organization_id,
16237                                         l_inventory_item_id,
16238                                         p_atp_record.ship_date,
16239                                         -1 * p_atp_record.quantity_ordered,
16240                                         '@@@');
16241 
16242                                     END;
16243                                 END IF; --- if sql%notfound
16244                                 commit;
16245                             END IF;
16246 
16247 			--END IF; -- end changes for cto override
16248 		     END IF;
16249 
16250 		      MSC_ATP_PROC.Details_Output(l_future_atp_period,
16251                      	             l_future_atp_supply_demand,
16252                      	             x_atp_period,
16253                      	             x_atp_supply_demand,
16254                      	             l_return_status);
16255 
16256                     END IF;
16257                 END IF;
16258               END IF; -- end if p_search = BACKWARD_SCHEDULING and
16259                       -- l_requested_ship_date < sysdate.  bug 1124538
16260 
16261             END IF;  -- end if net_demand
16262 
16263         END IF;  -- end if atp_flag = 'Y'
16264 
16265     ELSE
16266         -- doing atp check in an vendor site
16267 
16268         IF PG_DEBUG in ('Y', 'C') THEN
16269            msc_sch_wb.atp_debug('ATP_Check: ' || 'do atp check at a supplier site');
16270         END IF;
16271 
16272         l_requested_ship_date := p_atp_record.requested_ship_date;
16273         IF PG_DEBUG in ('Y', 'C') THEN
16274            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_requested_ship_date := ' || l_requested_ship_date);
16275         END IF;
16276         -- get the following information
16277         -- requested_date_quantity
16278         -- atp_date_this_level
16279         -- atp_date_quantity_this_level
16280 
16281         /* Modularize Item and Org Info */
16282         -- Re-Use info instead of making unnecessary call.
16283         ---bug 2374548: C_ITEM_INFO_rec is set only in org part and not in suppplier part
16284         --l_inv_item_id := C_ITEM_INFO_REC.dest_inv_item_id;
16285         IF NVL(G_ITEM_INFO_REC.sr_inv_item_id, 0) = p_atp_record.inventory_item_id THEN
16286             l_inv_item_id := G_ITEM_INFO_REC.dest_inv_item_id;
16287         ELSE
16288             MSC_ATP_PROC.get_global_item_info(p_atp_record.instance_id,
16289                                             --bug 3917625: read attributes from planned data
16290                                             -- -1,
16291                                             p_plan_id,
16292                                             p_atp_record.inventory_item_id,
16293                                             p_atp_record.organization_id,
16294                                             l_item_info_rec  );
16295             l_inv_item_id := G_ITEM_INFO_REC.dest_inv_item_id;
16296 
16297         END IF;
16298 
16299         IF PG_DEBUG in ('Y', 'C') THEN
16300            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_inv_item_id := ' || l_inv_item_id);
16301         END IF;
16302 
16303         /* Modularize Item and Org Info */
16304 
16305         IF (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 2) THEN
16306                IF PG_DEBUG in ('Y', 'C') THEN
16307                   msc_sch_wb.atp_debug('ATP_Check: ' || 'G_HIERARCHY_PROFILE = 2');
16308                END IF;
16309               p_atp_record.demand_class :=
16310                   MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(MSC_ATP_PVT.G_PARTNER_ID,
16311                            MSC_ATP_PVT.G_PARTNER_SITE_ID,
16312                            l_inv_item_id,
16313                            p_atp_record.organization_id,
16314                            p_atp_record.instance_id,
16315                            l_requested_ship_date,
16316                            NULL, -- level_id
16317                            p_atp_record.Demand_Class);
16318               IF PG_DEBUG in ('Y', 'C') THEN
16319                  msc_sch_wb.atp_debug('ATP_Check: ' || 'after getting the dummy demand class');
16320                  msc_sch_wb.atp_debug('ATP_Check: ' || 'p_atp_record.demand_class = '||p_atp_record.demand_class);
16321               END IF;
16322 
16323               --diag_atp
16324               l_allocation_rule_name := MSC_ATP_PVT.G_ALLOCATION_RULE_NAME;
16325         END IF;
16326 
16327         -- Bug 1410591, we need to consider processing lead time also similar
16328         -- to planning.
16329         ---- EFTC changes add preprocessing lead time to the lead time
16330         l_sysdate := sysdate;
16331 
16332         l_atp_request_date := l_requested_ship_date;
16333         IF PG_DEBUG in ('Y', 'C') THEN
16334            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atp_request_date := ' || l_atp_request_date);
16335         END IF;
16336         -- Supplier Capacity and Lead Time (SCLT) changes
16337         -- Fixed + Variable is discontinued, Instead item's processing lead time
16338         -- essentially full_lead_time is used. Initialize local variables.
16339 
16340         l_process_lt := p_atp_record.atp_lead_time;
16341         l_pre_pro_lt := MSC_ATP_PVT.G_ITEM_INFO_REC.pre_pro_lt;
16342         -- End Supplier Capacity and Lead Time changes
16343         /* 3316028 (Enforce Pur LT) - No need to to PTF/sysdate checks here as these have already been done
16344                 before calling this */
16345         l_atp_ship_date := l_requested_ship_date;
16346         /*
16347         IF p_search = BACKWARD_SCHEDULING THEN
16348            l_atp_ship_date := l_requested_ship_date;
16349 
16350         ELSIF p_search = FORWARD_SCHEDULING THEN
16351            --diag_atp, Supplier Capacity and Lead Time (SCLT) changes
16352            IF p_atp_record.atp_lead_time > 0 THEN
16353                /* we know the req date but we dont know whether (req date - (pre + process lead time)) < sysdate or not.
16354                   IF (req date - (pre + process lead time)) < sysdate then earlierst we can ship is
16355                   (sysdate + lead time) */
16356              --  Supplier Capacity and Lead Time (SCLT) changes
16357              -- First do the Pre-Processing Lead Time Offset in Forward Case.
16358         /*     IF l_pre_pro_lt > 0 THEN
16359                 /* ship_rec_cal
16360                 l_dock_date := MSC_CALENDAR.DATE_OFFSET(p_atp_record.organization_id,
16361                                                            p_atp_record.instance_id, 1,
16362                                                            l_sysdate,
16363                                                            l_pre_pro_lt);*/
16364         /*        l_dock_date := MSC_CALENDAR.DATE_OFFSET(
16365                                         p_atp_record.manufacturing_cal_code,
16366                                         p_atp_record.instance_id,
16367                                         l_sysdate,
16368                                         l_pre_pro_lt, 1);
16369 
16370                 IF PG_DEBUG in ('Y', 'C') THEN
16371                    msc_sch_wb.atp_debug('ATP_Check: ' || 'After pre-process Lead Time Offset ' );
16372                    msc_sch_wb.atp_debug('ATP_Check: ' || 'After atp dock date = '||
16373                     to_char(l_dock_date, 'DD-MON-YYYY'));
16374                 END IF;
16375               ELSE
16376                 l_dock_date := l_sysdate;
16377               END IF;
16378               -- Next do the Processing lead time offset in Forward Case using Plan Owning Org's calendar.
16379               IF p_atp_record.atp_lead_time > 0 THEN
16380                   /* ship_rec_cal
16381                   l_dock_date := MSC_CALENDAR.DATE_OFFSET(G_PLAN_INFO_REC.organization_id,
16382                                                                   G_PLAN_INFO_REC.sr_instance_id, 1,
16383                                                                   l_dock_date,
16384                                                                   p_atp_record.atp_lead_time);*/
16385         /*          l_dock_date := MSC_CALENDAR.DATE_OFFSET(
16386                                         p_atp_record.manufacturing_cal_code,
16387                                         G_PLAN_INFO_REC.sr_instance_id,
16388                                         l_dock_date,
16389                                         p_atp_record.atp_lead_time, 1);
16390               END IF;
16391               IF PG_DEBUG in ('Y', 'C') THEN
16392                     msc_sch_wb.atp_debug('ATP_Check: ' || 'After Process Lead Time Offset ' );
16393                     msc_sch_wb.atp_debug('ATP_Check: ' || 'After atp dock date = '||
16394                        to_char(l_dock_date, 'DD-MON-YYYY'));
16395               END IF;
16396               -- End  Supplier Capacity and Lead Time changes
16397               l_atp_ship_date := GREATEST(l_dock_date, l_requested_ship_date, MSC_ATP_PVT.G_PTF_DATE);
16398            ELSE
16399                l_atp_ship_date := GREATEST(l_sysdate,  MSC_ATP_PVT.G_PTF_DATE, l_requested_ship_date);
16400            END IF;
16401         END IF;
16402         3316028 (Enforce Pur LT) - changes end */
16403         IF PG_DEBUG in ('Y', 'C') THEN
16404            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atp_request_date := ' || l_atp_request_date);
16405            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atp_ship_date := ' || l_atp_ship_date);
16406            msc_sch_wb.atp_debug('ATP_Check: ' || 'G_PTF_DATE := ' || MSC_ATP_PVT.G_PTF_DATE);
16407         END IF;
16408 
16409         --diag_atp
16410         /* IF p_search = BACKWARD_SCHEDULING AND
16411            ((l_atp_request_date < l_sysdate) OR (l_requested_ship_date < MSC_ATP_PVT.G_PTF_DATE)) THEN
16412                 l_requested_date_quantity := 0;
16413                 IF PG_DEBUG in ('Y', 'C') THEN
16414                    msc_sch_wb.atp_debug('ATP_Check: ' || 'Sys date, ptf check failed for supplier');
16415                 END IF;
16416         ELSE
16417         */
16418            --  Supplier Capacity and Lead Time (SCLT) changes
16419            IF (g_sup_cap_cum_date IS NULL) THEN
16420                 g_sup_cap_cum_date := MSC_ATP_PVT.G_PLAN_INFO_REC.plan_start_date;
16421            END IF;
16422            IF PG_DEBUG in ('Y', 'C') THEN
16423                 msc_sch_wb.atp_debug('ATP_Check: ' || 'Plan Id := ' || MSC_ATP_PVT.G_PLAN_INFO_REC.plan_id);
16424                 msc_sch_wb.atp_debug('ATP_Check: ' || 'Plan Start Date := ' ||
16425                                            MSC_ATP_PVT.G_PLAN_INFO_REC.plan_start_date);
16426                 msc_sch_wb.atp_debug('ATP_Check: ' || 'g_sup_cap_cum_date := ' || g_sup_cap_cum_date);
16427            END IF;
16428 
16429 
16430            -- ship_rec_cal changes begin
16431            l_sup_atp_info_rec.instance_id                       := p_atp_record.instance_id;
16432            l_sup_atp_info_rec.plan_id                           := p_plan_id;
16433            l_sup_atp_info_rec.level                             := p_level + 1;
16434            l_sup_atp_info_rec.identifier                        := p_atp_record.identifier;
16435            l_sup_atp_info_rec.scenario_id                       := p_scenario_id;
16436            l_sup_atp_info_rec.inventory_item_id                 := l_inv_item_id;
16437            l_sup_atp_info_rec.organization_id                   := p_atp_record.organization_id;
16438            l_sup_atp_info_rec.supplier_id                       := p_atp_record.supplier_id;
16439            l_sup_atp_info_rec.supplier_site_id                  := p_atp_record.supplier_site_id;
16440            l_sup_atp_info_rec.requested_date                    := l_atp_ship_date;
16441            l_sup_atp_info_rec.quantity_ordered                  := p_atp_record.quantity_ordered;
16442            l_sup_atp_info_rec.demand_class                      := p_atp_record.demand_class;
16443            l_sup_atp_info_rec.insert_flag                       := p_atp_record.insert_flag;
16444            l_sup_atp_info_rec.requested_date_quantity           := null;
16445            l_sup_atp_info_rec.atp_date_this_level               := null;
16446            l_sup_atp_info_rec.atp_date_quantity_this_level      := null;
16447            l_sup_atp_info_rec.shipping_cal_code                 := p_atp_record.shipping_cal_code;
16448            l_sup_atp_info_rec.receiving_cal_code                := p_atp_record.receiving_cal_code;
16449            l_sup_atp_info_rec.manufacturing_cal_code            := p_atp_record.manufacturing_cal_code;
16450            l_sup_atp_info_rec.intransit_cal_code                := p_atp_record.intransit_cal_code;
16451            l_sup_atp_info_rec.sup_cap_cum_date                  := g_sup_cap_cum_date;
16452            l_sup_atp_info_rec.sysdate_seq_num                   := l_sysdate_seq_num;
16453            l_sup_atp_info_rec.sup_cap_type                      := l_sup_cap_type;
16454            l_sup_atp_info_rec.refresh_number                    := p_refresh_number;
16455            -- For CTO rearch
16456            l_sup_atp_info_rec.base_item_id                      := p_atp_record.base_model_id;
16457            l_sup_atp_info_rec.bom_item_type                     := p_atp_record.bom_item_type;
16458            l_sup_atp_info_rec.rep_ord_flag                      := p_atp_record.rep_ord_flag;
16459            -- Enforce Pur LT - pass last cap date as it has been passed to the ATP_check for supplier case
16460            l_sup_atp_info_rec.last_cap_date                     := p_atp_record.last_cap_date;
16461 
16462            MSC_ATP_REQ.Get_Supplier_Atp_Info(
16463                         l_sup_atp_info_rec,
16464                         x_atp_period,
16465                         x_atp_supply_demand,
16466                         l_return_status);
16467 
16468            l_requested_date_quantity           := l_sup_atp_info_rec.requested_date_quantity;
16469            l_atp_date_this_level               := l_sup_atp_info_rec.atp_date_this_level;
16470            l_atp_date_quantity_this_level      := l_sup_atp_info_rec.atp_date_quantity_this_level;
16471            -- ship_rec_cal changes end
16472 
16473         -- bug 1512366
16474         p_atp_record.requested_ship_date:=l_requested_ship_date;
16475 
16476         IF PG_DEBUG in ('Y', 'C') THEN
16477            msc_sch_wb.atp_debug('ATP_Check: ' || 'after call supplier, x_atp_supply_demand'||x_atp_supply_demand.supplier_id.COUNT);
16478            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atp_date_this_level := ' || l_atp_date_this_level);
16479            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_requested_date_quantity := ' || l_requested_date_quantity);
16480            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atp_date_quantity_this_level := ' || l_atp_date_quantity_this_level);
16481         END IF;
16482         IF l_requested_date_quantity >= p_atp_record.quantity_ordered THEN
16483 
16484             -- we meet the requirements!!!
16485             p_atp_record.requested_date_quantity:=l_requested_date_quantity;
16486             p_atp_record.ship_date := l_atp_date_this_level;
16487             p_atp_record.available_quantity:=l_atp_date_quantity_this_level;
16488 
16489         ELSE
16490 
16491             -- we cannot meet the requirements, so we provide
16492             -- the information at this level
16493             p_atp_record.requested_date_quantity:=l_requested_date_quantity;
16494             p_atp_record.ship_date := l_atp_date_this_level;
16495             p_atp_record.available_quantity:=l_atp_date_quantity_this_level;
16496             p_atp_record.error_code := ATP_REQ_QTY_FAIL;
16497 
16498         END IF;
16499 
16500         -- add pegging info for this demand
16501 
16502         -- for performance reason, we call these function here and
16503         -- then populate the pegging tree with the values
16504 
16505         /* Modularize Item and Org Info */
16506         -- Re-Use info instead of making unnecessary call.
16507         ---bug 2374548: C_ITEM_INFO_rec is set only in org part and not in suppplier part
16508         --l_inv_item_name := C_ITEM_INFO_REC.item_name;
16509         IF NVL(G_ITEM_INFO_REC.sr_inv_item_id, 0) = p_atp_record.inventory_item_id THEN
16510             l_inv_item_name := G_ITEM_INFO_REC.item_name;
16511         ELSE
16512             MSC_ATP_PROC.get_global_item_info(p_atp_record.instance_id,
16513                                             --3917625: Read data from plan output
16514                                             -- -1,
16515                                             p_plan_id,
16516                                             p_atp_record.inventory_item_id,
16517                                             p_atp_record.organization_id,
16518                                             l_item_info_rec  );
16519             l_inv_item_name := G_ITEM_INFO_REC.item_name;
16520         END IF;
16521 
16522         IF PG_DEBUG in ('Y', 'C') THEN
16523            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_inv_item_name := ' || l_inv_item_name);
16524         END IF;
16525 
16526         /*l_inv_item_name := MSC_ATP_FUNC.get_inv_item_name(p_atp_record.instance_id,
16527                                       p_atp_record.inventory_item_id,
16528                                       p_atp_record.organization_id);
16529          Modularize Item and Org Info */
16530 
16531         l_supplier_name := MSC_ATP_FUNC.get_supplier_name(p_atp_record.instance_id,
16532                                              p_atp_record.supplier_id);
16533         l_supplier_site_name :=
16534             MSC_ATP_FUNC.get_supplier_site_name(p_atp_record.instance_id,
16535                                    p_atp_record.supplier_site_id);
16536 
16537         l_pegging_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
16538         l_pegging_rec.order_line_id:= MSC_ATP_PVT.G_ORDER_LINE_ID;
16539         l_pegging_rec.parent_pegging_id:= p_parent_pegging_id;
16540         l_pegging_rec.atp_level:= p_level;
16541         l_pegging_rec.inventory_item_id := p_atp_record.inventory_item_id;
16542         l_pegging_rec.inventory_item_name := l_inv_item_name;
16543         l_pegging_rec.supplier_id := p_atp_record.supplier_id;
16544         l_pegging_rec.supplier_name := l_supplier_name;
16545         l_pegging_rec.supplier_site_id := p_atp_record.supplier_site_id;
16546         l_pegging_rec.supplier_site_name := l_supplier_site_name;
16547         l_pegging_rec.resource_id := NULL;
16548         l_pegging_rec.resource_code := NULL;
16549         l_pegging_rec.department_id := NULL;
16550         l_pegging_rec.department_code := NULL;
16551         l_pegging_rec.identifier1:= p_atp_record.instance_id;
16552         l_pegging_rec.identifier2:= p_plan_id;
16553         l_pegging_rec.identifier3 := -1;
16554         l_pegging_rec.scenario_id:= p_scenario_id;
16555         l_pegging_rec.supply_demand_source_type:= 1;
16556         ---- summary
16557 
16558         IF (p_search = 1) THEN
16559            l_pegging_rec.supplier_atp_date :=  l_requested_ship_date;
16560         ELSE
16561            l_pegging_rec.supplier_atp_date := l_atp_date_this_level;
16562         END IF;
16563         l_pegging_rec.dest_inv_item_id := l_inv_item_id;
16564 
16565         -- 1408056
16566         IF (p_search = 1) THEN
16567           --diag_atp : we place the demand on complete quantity in case of diagnostic ATP
16568           IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 THEN
16569               l_pegging_rec.supply_demand_quantity:=  p_atp_record.quantity_ordered;
16570           ELSE
16571 
16572              l_pegging_rec.supply_demand_quantity:=
16573                 LEAST(l_requested_date_quantity, p_atp_record.quantity_ordered);
16574           END IF;
16575         ELSE
16576           l_pegging_rec.supply_demand_quantity:= p_atp_record.quantity_ordered;
16577 
16578         END IF;
16579         -- l_pegging_rec.supply_demand_quantity:=p_atp_record.quantity_ordered;
16580 
16581         l_pegging_rec.supply_demand_type:= 1;
16582         l_pegging_rec.supply_demand_date:= l_requested_ship_date;
16583 
16584 	-- dsting ATO 2465370
16585 	IF PG_DEBUG in ('Y','C') THEN
16586 		msc_sch_wb.atp_debug('set required_date supplier demand');
16587 	END IF;
16588         IF (p_search = BACKWARD_SCHEDULING) THEN
16589           -- Commented for bug 2748730. G_END_OF_DAY not required here.
16590 	  -- We will do that inside the procedure Add_Pegging
16591 	  -- l_pegging_rec.required_date:= TRUNC(l_requested_ship_date) + MSC_ATP_PVT.G_END_OF_DAY;
16592           --bug 3328421
16593 	  l_pegging_rec.required_date:= l_requested_ship_date ;
16594           l_pegging_rec.actual_supply_demand_date := l_requested_ship_date;
16595         ELSE
16596 	  -- Commented for bug 2748730. G_END_OF_DAY not required here.
16597 	  -- We will do that inside the procedure Add_Pegging
16598           -- l_pegging_rec.required_date:= TRUNC(l_atp_date_this_level) + MSC_ATP_PVT.G_END_OF_DAY;
16599 
16600 	  l_pegging_rec.required_date:= l_requested_ship_date ;
16601           l_pegging_rec.actual_supply_demand_date := l_atp_date_this_level;
16602         END IF;
16603 
16604         -- for demo:1153192
16605         l_pegging_rec.constraint_flag := 'N';
16606 	l_pegging_rec.component_identifier :=
16607                          NVL(p_atp_record.component_identifier, MSC_ATP_PVT.G_COMP_LINE_ID);
16608 
16609         l_pegging_rec.summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;
16610 
16611         --diag_atp
16612         l_pegging_rec.pegging_type := MSC_ATP_PVT.SUPPLIER_DEMAND;
16613         l_pegging_rec.demand_class :=  p_atp_record.demand_class;
16614 
16615         --4570421
16616         l_pegging_rec.scaling_type                      := p_atp_record.scaling_type;
16617         l_pegging_rec.scale_multiple                    := p_atp_record.scale_multiple;
16618         l_pegging_rec.scale_rounding_variance           := p_atp_record.scale_rounding_variance;
16619         l_pegging_rec.rounding_direction                := p_atp_record.rounding_direction;
16620         l_pegging_rec.component_yield_factor            := p_atp_record.component_yield_factor; --4570421
16621         l_pegging_rec.usage                             := p_atp_record.usage_qty; --4775920
16622         l_pegging_rec.organization_type                 := p_atp_record.organization_type; --4775920
16623 
16624         MSC_ATP_DB_UTILS.Add_Pegging(l_pegging_rec, l_pegging_id);
16625 
16626         -- add pegging info for the supply
16627 
16628         l_pegging_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
16629         l_pegging_rec.order_line_id:= MSC_ATP_PVT.G_ORDER_LINE_ID;
16630         l_pegging_rec.parent_pegging_id:= l_pegging_id;
16631         l_pegging_rec.atp_level:= p_level+1;
16632 
16633         l_pegging_rec.inventory_item_id := p_atp_record.inventory_item_id;
16634         l_pegging_rec.inventory_item_name := l_inv_item_name;
16635         l_pegging_rec.supplier_id := p_atp_record.supplier_id;
16636         l_pegging_rec.supplier_name := l_supplier_name;
16637         l_pegging_rec.supplier_site_id := p_atp_record.supplier_site_id;
16638         l_pegging_rec.supplier_site_name := l_supplier_site_name;
16639         l_pegging_rec.resource_id := NULL;
16640         l_pegging_rec.resource_code := NULL;
16641         l_pegging_rec.department_id := NULL;
16642         l_pegging_rec.department_code := NULL;
16643 
16644         l_pegging_rec.identifier1:= p_atp_record.instance_id;
16645         l_pegging_rec.identifier2:= p_plan_id;
16646         l_pegging_rec.identifier3 := -1;
16647         l_pegging_rec.scenario_id:= p_scenario_id;
16648         l_pegging_rec.supply_demand_source_type:= ATP;
16649         l_pegging_rec.supply_demand_type:= 2;
16650         l_pegging_rec.source_type := 0;
16651 	l_pegging_rec.component_identifier :=
16652                          NVL(p_atp_record.component_identifier, MSC_ATP_PVT.G_COMP_LINE_ID);
16653 
16654         IF (p_search = 1) THEN
16655           l_pegging_rec.supply_demand_date:= l_requested_ship_date;
16656           l_pegging_rec.supply_demand_quantity:= l_requested_date_quantity;
16657           --bug 3328421
16658           l_pegging_rec.actual_supply_demand_date := l_requested_ship_date;
16659 
16660           -- for demo:1153192
16661           --optional_fw
16662           IF p_atp_record.quantity_ordered >= l_requested_date_quantity
16663                    AND (G_FORWARD_ATP = 'N' OR G_SUBSTITUTION_FLAG = 'Y') THEN --5211286
16664               l_pegging_rec.constraint_flag := 'Y';
16665           ELSE
16666               l_pegging_rec.constraint_flag := 'N';
16667           END IF;
16668         ELSE
16669           l_pegging_rec.supply_demand_date:= l_atp_date_this_level;
16670           l_pegging_rec.supply_demand_quantity:= l_atp_date_quantity_this_level;
16671           --bug 3328421
16672           l_pegging_rec.actual_supply_demand_date := l_atp_date_this_level;
16673         END IF;
16674         l_pegging_rec.summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;
16675 
16676         --diag_atp
16677         l_pegging_rec.pegging_type := MSC_ATP_PVT.ATP_SUPPLIER;
16678         -- Bug 3828469 - Move the quantity check out so that constraint is added only if it exists
16679         IF p_atp_record.quantity_ordered > l_requested_date_quantity THEN
16680             IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 THEN
16681                 l_pegging_rec.constraint_type := 1;
16682             ELSIF (MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 2 and p_search = 2) THEN
16683 
16684                 --s_cto_rearch
16685                 IF l_atp_date_this_level > l_requested_ship_date THEN
16686                     l_pegging_rec.constraint_type := 1;
16687                 END IF;
16688                 --e_cto_rearch
16689             END IF;
16690         END IF;
16691         l_pegging_rec.dest_inv_item_id := l_inv_item_id;
16692 
16693         l_pegging_rec.required_quantity := p_atp_record.quantity_ordered;
16694         --bug 3328421: required date always contain the original requested date
16695         --l_pegging_rec.required_date := l_atp_ship_date;
16696         l_pegging_rec.required_date := l_requested_ship_date;
16697         l_pegging_rec.allocation_rule := l_allocation_rule_name;
16698         l_pegging_rec.plan_name := p_atp_record.plan_name;
16699         l_pegging_rec.demand_class :=  p_atp_record.demand_class;
16700         -- Bug 3826234
16701         l_pegging_rec.shipping_cal_code      :=  p_atp_record.shipping_cal_code;
16702         l_pegging_rec.receiving_cal_code     :=  p_atp_record.receiving_cal_code;
16703         l_pegging_rec.intransit_cal_code     :=  p_atp_record.intransit_cal_code;
16704         l_pegging_rec.manufacturing_cal_code :=  p_atp_record.manufacturing_cal_code;
16705 
16706         --4570421
16707         l_pegging_rec.scaling_type                      := p_atp_record.scaling_type;
16708         l_pegging_rec.scale_multiple                    := p_atp_record.scale_multiple;
16709         l_pegging_rec.scale_rounding_variance           := p_atp_record.scale_rounding_variance;
16710         l_pegging_rec.rounding_direction                := p_atp_record.rounding_direction;
16711         l_pegging_rec.component_yield_factor            := p_atp_record.component_yield_factor; --4570421
16712         l_pegging_rec.usage                             := p_atp_record.usage_qty; --4775920
16713         l_pegging_rec.organization_type                 := p_atp_record.organization_type; --4775920
16714 
16715         MSC_ATP_DB_UTILS.Add_Pegging(l_pegging_rec, l_pegging_id);
16716 
16717         ---- summary
16718         --- Create record into summary table for this supplier
16719         -- Code to update summary records removed for summary enhancement
16720         /** code commented for time being. Will be removed after code review
16721         IF MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y' THEN
16722             IF (p_search = 1) THEN
16723                 IF PG_DEBUG in ('Y', 'C') THEN
16724                     msc_sch_wb.atp_debug('ATP_Check: ' || 'Update summary table , backward case');
16725                     msc_sch_wb.atp_debug('ATP_Check: ' || 'l_requested_ship_date := ' || l_requested_ship_date);
16726                     msc_sch_wb.atp_debug('ATP_Check: ' || 'Supplier_id := ' ||  p_atp_record.supplier_id);
16727                     msc_sch_wb.atp_debug('ATP_Check: ' || 'l_inv_item_id := ' || l_inv_item_id);
16728                     msc_sch_wb.atp_debug('ATP_Check: ' || ' Quantity := ' || least(l_requested_date_quantity, p_atp_record.quantity_ordered));
16729                     msc_sch_wb.atp_debug('ATP_Check: ' || 'First try to update');
16730                 END IF;
16731                 MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(p_atp_record.instance_id,
16732                                                     p_plan_id,
16733                                                     null,
16734                                                     l_inv_item_id,
16735                                                     l_requested_ship_date,
16736                                                     null,
16737                                                     null,
16738                                                     p_atp_record.supplier_id,
16739                                                     p_atp_record.supplier_site_id,
16740                                                     null,
16741                                                     null,
16742                                                     4);
16743 
16744                 update /*+ INDEX(msc_atp_summary_sup MSC_ATP_SUMMARY_sup_U1) *//* msc_atp_summary_sup
16745                 set sd_qty = sd_qty - least(l_requested_date_quantity, p_atp_record.quantity_ordered)
16746                 where plan_id = p_plan_id
16747                 and   sr_instance_id = p_atp_record.instance_id
16748                 and   inventory_item_id = l_inv_item_id
16749                 and   supplier_id = p_atp_record.supplier_id
16750                 and   supplier_site_id = p_atp_record.supplier_site_id
16751                 and   sd_date = trunc(l_requested_ship_date);
16752                 IF PG_DEBUG in ('Y', 'C') THEN
16753                     msc_sch_wb.atp_debug('ATP_Check: ' || 'After update of msc_atp_summary_sup');
16754                     msc_sch_wb.atp_debug('ATP_Check: ' || ' Number of rows updated := ' || SQL%ROWCOUNT);
16755                 END IF;
16756 
16757                 IF SQL%NOTFOUND THEN
16758                     IF PG_DEBUG in ('Y', 'C') THEN
16759                         msc_sch_wb.atp_debug('ATP_Check: ' || 'update failed, insert a row');
16760                     END IF;
16761                     BEGIN
16762                     insert into /*+ INDEX(msc_atp_summary_sup MSC_ATP_SUMMARY_SUP_U1) *//* msc_atp_summary_sup
16763                          (plan_id,
16764                           inventory_item_id,
16765                           supplier_id,
16766                           supplier_site_id,
16767                           sr_instance_id,
16768                           sd_date,
16769                           sd_qty,
16770                           LAST_UPDATE_DATE,
16771                           LAST_UPDATED_BY,
16772                           CREATION_DATE,
16773                           CREATED_BY
16774                           )
16775                           VALUES
16776                           (p_plan_id,
16777                            l_inv_item_id,
16778                            p_atp_record.supplier_id,
16779                            p_atp_record.supplier_site_id,
16780                            p_atp_record.instance_id,
16781                            trunc(l_requested_ship_date),
16782                            -1 * least(l_requested_date_quantity, p_atp_record.quantity_ordered),
16783                            sysdate,
16784                            FND_GLOBAL.USER_ID,
16785                            sysdate,
16786                            FND_GLOBAL.USER_ID);
16787                     EXCEPTION
16788                         WHEN DUP_VAL_ON_INDEX THEN
16789                             MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(p_atp_record.instance_id,
16790                                                                 p_plan_id,
16791                                                                 null,
16792                                                                 l_inv_item_id,
16793                                                                 l_requested_ship_date,
16794                                                                 null,
16795                                                                 null,
16796                                                                 p_atp_record.supplier_id,
16797                                                                 p_atp_record.supplier_site_id,
16798                                                                 null,
16799                                                                 null,
16800                                                                 4);
16801 
16802                             update /*+ INDEX(msc_atp_summary_sup MSC_ATP_SUMMARY_SUP_U1) *//* msc_atp_summary_sup
16803                             set sd_qty = sd_qty - least(l_requested_date_quantity, p_atp_record.quantity_ordered)
16804                             where plan_id = p_plan_id
16805                             and   sr_instance_id = p_atp_record.instance_id
16806                             and   inventory_item_id = l_inv_item_id
16807                             and   supplier_id = p_atp_record.supplier_id
16808                             and   supplier_site_id = p_atp_record.supplier_site_id
16809                             and   sd_date = trunc(l_requested_ship_date);
16810                     END;
16811                 END IF;
16812                 commit;
16813             ELSIF l_atp_date_this_level IS NOT NULL THEN
16814                 IF PG_DEBUG in ('Y', 'C') THEN
16815                     msc_sch_wb.atp_debug('ATP_Check: ' || 'Update summary table, forward case');
16816                     msc_sch_wb.atp_debug('ATP_Check: ' || 'First try to update');
16817                     msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atp_date_this_level := ' || l_atp_date_this_level);
16818                     msc_sch_wb.atp_debug('ATP_Check: ' || 'Supplier_id := ' ||  p_atp_record.supplier_id);
16819                     msc_sch_wb.atp_debug('ATP_Check: ' || 'l_inv_item_id := ' || l_inv_item_id);
16820                     msc_sch_wb.atp_debug('ATP_Check: ' || ' Quantity := ' ||  p_atp_record.quantity_ordered);
16821                     msc_sch_wb.atp_debug('ATP_Check: ' || 'First try to update');
16822                 END IF;
16823                 MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(p_atp_record.instance_id,
16824                                                     p_plan_id,
16825                                                     p_atp_record.organization_id,
16826                                                     l_inv_item_id,
16827                                                     l_atp_date_this_level,
16828                                                     null,
16829                                                     null,
16830                                                     p_atp_record.supplier_id,
16831                                                     p_atp_record.supplier_site_id,
16832                                                     null,
16833                                                     null,
16834                                                     4);
16835 
16836                 update /*+ INDEX(msc_atp_summary_sup MSC_ATP_SUMMARY_SUP_U1) *//* msc_atp_summary_sup
16837                 set sd_qty = sd_qty -  p_atp_record.quantity_ordered
16838                 where plan_id = p_plan_id
16839                 and   sr_instance_id = p_atp_record.instance_id
16840                 and   inventory_item_id = l_inv_item_id
16841                 and   supplier_id = p_atp_record.supplier_id
16842                 and   supplier_site_id = p_atp_record.supplier_site_id
16843                 and   sd_date = trunc(l_atp_date_this_level);
16844                 IF PG_DEBUG in ('Y', 'C') THEN
16845                     msc_sch_wb.atp_debug('ATP_Check: ' || 'After update of msc_atp_summary_sup');
16846                     msc_sch_wb.atp_debug('ATP_Check: ' || ' Number of rows updated := ' || SQL%ROWCOUNT);
16847                 END IF;
16848 
16849                 IF SQL%NOTFOUND THEN
16850                     IF PG_DEBUG in ('Y', 'C') THEN
16851                         msc_sch_wb.atp_debug('ATP_Check: ' || 'update failed, insert a row');
16852                     END IF;
16853                     BEGIN
16854                         insert /*+ INDEX(msc_atp_summary_sup MSC_ATP_SUMMARY_SUP_U1) *//* into msc_atp_summary_sup
16855                                 (plan_id,
16856                                 inventory_item_id,
16857                                 supplier_id,
16858                                 supplier_site_id,
16859                                 sr_instance_id,
16860                                 sd_date,
16861                                 sd_qty,
16862                                 LAST_UPDATE_DATE,
16863                                 LAST_UPDATED_BY,
16864                                 CREATION_DATE,
16865                                 CREATED_BY
16866                                 )
16867                         VALUES
16868                                 (p_plan_id,
16869                                 l_inv_item_id,
16870                                 p_atp_record.supplier_id,
16871                                 p_atp_record.supplier_site_id,
16872                                 p_atp_record.instance_id,
16873                                 trunc(l_atp_date_this_level),
16874                                 -1 * p_atp_record.quantity_ordered,
16875                                 sysdate,
16876                                 FND_GLOBAL.USER_ID,
16877                                 sysdate,
16878                                 FND_GLOBAL.USER_ID);
16879                     EXCEPTION
16880                         WHEN DUP_VAL_ON_INDEX THEN
16881                             MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(p_atp_record.instance_id,
16882                                                                 p_plan_id,
16883                                                                 null,
16884                                                                 l_inv_item_id,
16885                                                                 l_atp_date_this_level,
16886                                                                 null,
16887                                                                 null,
16888                                                                 p_atp_record.supplier_id,
16889                                                                 p_atp_record.supplier_site_id,
16890                                                                 null,
16891                                                                 null,
16892                                                                 4);
16893                             update /*+ INDEX(msc_atp_summary_sup MSC_ATP_SUMMARY_SUP_U1) *//* msc_atp_summary_sup
16894                             set sd_qty = sd_qty -  p_atp_record.quantity_ordered
16895                             where plan_id = p_plan_id
16896                             and   sr_instance_id = p_atp_record.instance_id
16897                             and   inventory_item_id = l_inv_item_id
16898                             and   supplier_id = p_atp_record.supplier_id
16899                             and   supplier_site_id = p_atp_record.supplier_site_id
16900                             and   sd_date = trunc(l_atp_date_this_level) ;
16901                     END;
16902 
16903                 END IF;
16904                 commit;
16905             END IF; -- if p_search = 1
16906             commit;
16907         END IF; -- IF summary_flag = 'y'
16908         **/
16909 
16910         FOR i in 1..x_atp_period.Level.COUNT LOOP
16911             x_atp_period.Pegging_Id(i) := l_pegging_id;
16912             x_atp_period.End_Pegging_Id(i) := MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
16913 
16914         END LOOP;
16915 
16916         IF PG_DEBUG in ('Y', 'C') THEN
16917            msc_sch_wb.atp_debug('ATP_Check: ' || 'in get_res_requirements we are here 2');
16918         END IF;
16919 
16920 	IF p_atp_record.insert_flag <> 0 THEN
16921 	         MSC_ATP_DB_UTILS.move_SD_temp_into_mrp_details(l_pegging_id,
16922 			MSC_ATP_PVT.G_DEMAND_PEGGING_ID);
16923    	END IF;
16924 
16925     END IF;
16926 
16927 IF PG_DEBUG in ('Y', 'C') THEN
16928        msc_sch_wb.atp_debug('ATP_Check: ' || 'children_type: ' || p_atp_record.children_type);
16929        msc_sch_wb.atp_debug('***** End ATP_Check Procedure *****');
16930     END IF;
16931 
16932 
16933 EXCEPTION
16934 
16935     WHEN MSC_ATP_PVT.INVALID_OSS_SOURCE THEN
16936 
16937         IF PG_DEBUG in ('Y', 'C') THEN
16938            msc_sch_wb.atp_debug('ATP_Check: ' || 'Invalid OSS setup detected');
16939         END IF;
16940         p_atp_record.requested_date_quantity := 0;
16941         p_atp_record.available_quantity := 0;
16942         p_atp_record.ship_date := null;
16943         p_atp_record.error_code := MSC_ATP_PVT.OSS_SOURCING_ERROR;
16944         x_return_status := MSC_ATP_PVT.CTO_OSS_ERROR;
16945         --RAISE MSC_ATP_PVT.INVALID_OSS_SOURCE;
16946 
16947     -- 2400614 : krajan: For detecting assignment set mismatch
16948     WHEN MSC_ATP_PVT.G_ATO_SOURCING_MISMATCH THEN
16949         IF PG_DEBUG in ('Y', 'C') THEN
16950            msc_sch_wb.atp_debug('something wrong in the ATP_Check 0');
16951            msc_sch_wb.atp_debug('ATP_Check: ' || 'Source Mismatch detected');
16952         END IF;
16953         p_atp_record.error_code := MSC_ATP_PVT.ASSIGN_SET_NOT_IN_SYNC;
16954         x_return_status := MSC_ATP_PVT.G_ATO_SRC_MISMATCH ;   --- krajan : indicates mismatch
16955                                   --- Primarily for processing in get_comp_requirements
16956     -- dsting 2764213
16957     WHEN MSC_ATP_PVT.EXC_NO_PLAN_FOUND THEN
16958         IF PG_DEBUG in ('Y', 'C') THEN
16959            msc_sch_wb.atp_debug('something wrong in the ATP_Check 0.1');
16960            msc_sch_wb.atp_debug('ATP_Check: Plan not found for the requested item or component');
16961         END IF;
16962         if (NVL(MSC_ATP_PVT.G_DOWNTIME_HIT, 'N') = 'Y') then
16963            --bug 2854351:
16964            --p_atp_record.error_code := MSC_ATP_PVT.TRY_ATP_LATER;
16965            p_atp_record.error_code := MSC_ATP_PVT.PLAN_DOWN_TIME;
16966         else
16967            p_atp_record.error_code := MSC_ATP_PVT.PLAN_NOT_FOUND;
16968         end if;
16969         x_return_status := MSC_ATP_PVT.G_NO_PLAN_FOUND;
16970 
16971     -- krajan 2752705
16972     WHEN MSC_ATP_PVT.G_EXC_UNCOLLECTED_ITEM THEN
16973         IF PG_DEBUG in ('Y', 'C') THEN
16974            msc_sch_wb.atp_debug('something wrong in the ATP_Check 0.2');
16975            msc_sch_wb.atp_debug('ATP_Check: The item is not collected');
16976         END IF;
16977         p_atp_record.error_code := MSC_ATP_PVT.ATP_ITEM_NOT_COLLECTED;
16978         x_return_status := MSC_ATP_PVT.G_ATO_UNCOLL_ITEM;
16979 
16980     WHEN FND_API.G_EXC_ERROR THEN
16981         IF PG_DEBUG in ('Y', 'C') THEN
16982            msc_sch_wb.atp_debug('something wrong in the ATP_Check 1');
16983         END IF;
16984         x_return_status := FND_API.G_RET_STS_ERROR;
16985         IF PG_DEBUG in ('Y', 'C') THEN
16986            msc_sch_wb.atp_debug('ATP_CHECK: IN Error block for G_EXC_ERROR');
16987            msc_sch_wb.atp_debug('ATP_Check: ' || 'Error := ' || SQLerrm);
16988         END IF;
16989 
16990     WHEN NO_MATCHING_DATE_IN_CAL THEN
16991         IF PG_DEBUG in ('Y', 'C') THEN
16992            msc_sch_wb.atp_debug('MAtching cal date not found, in atp_check');
16993         END IF;
16994         p_atp_record.error_code := NO_MATCHING_CAL_DATE;
16995         --x_return_status := FND_API.G_RET_STS_ERROR; --bug3583705
16996         RAISE NO_MATCHING_DATE_IN_CAL;
16997 
16998     /************ Bug 1510853 ATP Rule Check ************/
16999     WHEN  EXC_NO_ATP_RULE THEN
17000         IF PG_DEBUG in ('Y', 'C') THEN
17001            msc_sch_wb.atp_debug('No ATP Rule for Item and Org., in atp_check');
17002         END IF;
17003         p_atp_record.error_code := ATP_BAD_RULE;
17004         x_return_status := FND_API.G_RET_STS_ERROR;
17005 
17006     -- Error Handling Modifications
17007     WHEN MSC_ATP_PUB.ATP_INVALID_OBJECTS_FOUND THEN
17008         IF PG_DEBUG in ('Y', 'C') THEN
17009            msc_sch_wb.atp_debug('ATP_Check: ' || 'error := ' || sqlerrm);
17010            msc_sch_wb.atp_debug('ATP_Check: Invalid Objects found, sqlcode= '||sqlcode);
17011         END IF;
17012 
17013         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
17014         IF (p_atp_record.error_code IS NULL) or (p_atp_record.error_code IN (0,61,150)) THEN
17015                 p_atp_record.error_code := ATP_INVALID_OBJECTS;
17016         END IF;
17017         IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
17018             FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME , 'ATP_Check');
17019         END IF;
17020         RAISE MSC_ATP_PUB.ATP_INVALID_OBJECTS_FOUND;
17021     -- Error Handling Insert End
17022 
17023     -- rajjain 02/20/2003 Bug 2813095
17024     WHEN ALLOC_ATP_INVALID_PROFILE THEN
17025         IF PG_DEBUG in ('Y', 'C') THEN
17026            msc_sch_wb.atp_debug('ATP_Check: ' || 'error := ' || sqlerrm);
17027         END IF;
17028         p_atp_record.error_code := INVALID_ALLOC_PROFILE_SETUP;
17029         x_return_status := FND_API.G_RET_STS_ERROR;
17030 
17031     WHEN OTHERS THEN
17032         IF PG_DEBUG in ('Y', 'C') THEN
17033            msc_sch_wb.atp_debug('ATP_CHECK: IN Error block for OTHERS');
17034 	   msc_sch_wb.atp_debug('ATP_Check: ' || 'error := ' || sqlerrm);
17035            msc_sch_wb.atp_debug('something wrong in the ATP_Check 2, sqlcode= '||sqlcode);
17036         END IF;
17037 
17038         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
17039 
17040         /* Check if this is actually coming from a calendar routine*/
17041         l_encoded_text := fnd_message.GET_ENCODED;
17042         IF l_encoded_text IS NULL THEN
17043                 l_msg_app := NULL;
17044                 l_msg_name := NULL;
17045         ELSE
17046                 fnd_message.parse_encoded(l_encoded_text, l_msg_app, l_msg_name);
17047         END IF;
17048 
17049         -- Error Handling Changes
17050         IF (p_atp_record.error_code IS NULL) or (p_atp_record.error_code IN (0,61,150)) THEN
17051                 IF l_msg_app='MRP' AND l_msg_name='GEN-DATE OUT OF BOUNDS' THEN
17052                         p_atp_record.error_code := NO_MATCHING_CAL_DATE;
17053                         IF PG_DEBUG in ('Y', 'C') THEN
17054                            msc_sch_wb.atp_debug('ATP_CHECK: NO_MATCHING_CAL_DATE');
17055                         END IF;
17056                 ELSE
17057                         p_atp_record.error_code := ATP_PROCESSING_ERROR; -- ATP Processing Error
17058                         IF PG_DEBUG in ('Y', 'C') THEN --bug3583705
17059                            msc_sch_wb.atp_debug('ATP_CHECK: ATP_PROCESSING_ERROR');
17060                         END IF;
17061                 END IF;
17062         END IF;
17063 
17064         IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
17065             FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME , 'ATP_Check');
17066         END IF;
17067 
17068         IF l_msg_app='MRP' AND l_msg_name='GEN-DATE OUT OF BOUNDS' THEN
17069                 RAISE NO_MATCHING_DATE_IN_CAL;
17070         ELSE
17071                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
17072         END IF;
17073 END ATP_Check;
17074 
17075 
17076 PROCEDURE Call_Schedule (
17077                p_session_id         IN    NUMBER,
17078                p_atp_table          IN    MRP_ATP_PUB.ATP_Rec_Typ,
17079                p_instance_id        IN    NUMBER,
17080                p_assign_set_id      IN    NUMBER,
17081                p_refresh_number     IN    NUMBER,
17082                x_atp_table          OUT   NoCopy MRP_ATP_PUB.ATP_Rec_Typ,
17083                x_return_status      OUT   NoCopy VARCHAR2,
17084                x_msg_data           OUT   NoCopy VARCHAR2,
17085                x_msg_count          OUT   NoCopy NUMBER,
17086                x_atp_supply_demand  OUT   NOCOPY MRP_ATP_PUB.ATP_Supply_Demand_Typ,
17087                x_atp_period         OUT   NOCOPY MRP_ATP_PUB.ATP_Period_Typ,
17088                x_atp_details        OUT   NOCOPY MRP_ATP_PUB.ATP_Details_Typ
17089 ) IS
17090 
17091 i				PLS_INTEGER;
17092 j				PLS_INTEGER;
17093 k                       	PLS_INTEGER;
17094 m                       	PLS_INTEGER;
17095 l_return_status			VARCHAR2(1);
17096 l_atp_input_table 		MRP_ATP_PUB.ATP_Rec_Typ;
17097 l_atp_output_table 		MRP_ATP_PUB.ATP_Rec_Typ;
17098 l_atp_period 			MRP_ATP_PUB.ATP_Period_Typ;
17099 l_atp_supply_demand 		MRP_ATP_PUB.ATP_Supply_Demand_Typ;
17100 l_atp_details 			MRP_ATP_PUB.ATP_Details_Typ;
17101 l_null_atp_table       	     	MRP_ATP_PUB.ATP_Rec_Typ;
17102 l_null_atp_period            	MRP_ATP_PUB.ATP_Period_Typ;
17103 l_null_atp_supply_demand     	MRP_ATP_PUB.ATP_Supply_Demand_Typ;
17104 l_null_atp_details           	MRP_ATP_PUB.ATP_Details_Typ;
17105 l_msg_data 		     	VARCHAR2(2000);
17106 l_msg_text 		     	VARCHAR2(2000);
17107 l_msg_count			NUMBER;
17108 l_msg_number			NUMBER;
17109 plsql_block                     VARCHAR2(10000);
17110 l_dblink                     	VARCHAR2(128);
17111 --For Workflow
17112 l_req_ship_date                 DATE;
17113 l_stealing_qty                  NUMBER;
17114 l_wf_item_key                   VARCHAR2(20);
17115 l_wf_profile			VARCHAR2(1);
17116 l_plan_id                       NUMBER;
17117 -- Override Workflow Variables
17118 l_avail_date                    DATE;
17119 l_wf_start                      VARCHAR2(1);
17120 
17121 /* Bug 2290633 */
17122 l_delivery_lead_time            NUMBER;
17123 l_from_location_id              NUMBER;
17124 l_from_location                 NUMBER;
17125 l_to_location_id                NUMBER;
17126 l_to_location                   NUMBER;
17127 l_ship_method                   VARCHAR2(30);
17128 l_sysdate                       DATE;
17129 L_PREV_WORK_SHIP_DATE           DATE;
17130 l_requested_ship_date           DATE;
17131 /* End Bug 2290633 */
17132 
17133 -- ATP Override rajjain begin 10/03/2002 for generating exceptions
17134 l_exception_rec			MSC_ATP_EXCEPTIONS.ATP_Exception_Rec_Typ;
17135 l_plan_type			NUMBER := 0;
17136 l_generate_exception		VARCHAR2(1);
17137 l_exception_return_status	VARCHAR2(2);
17138 -- ATP Override rajjain end
17139 
17140 -- rajjain 02/06/2003 Bug 2781863
17141 l_inventory_item_id		NUMBER;
17142 -- 24x7
17143 l_instance_id                   NUMBER;
17144 
17145 -- rajjain 02/03/2003 Bug 2766713
17146 l_set_fail_flag			VARCHAR2(1) := 'N';
17147 
17148 --s_cto_rearch
17149 l_included_item_table               MRP_ATP_PUB.ATP_Rec_Typ;
17150 l_included_item_cntr                number;
17151 l_mand_comp_info_rec                MSC_ATP_CTO.mand_comp_info_rec;
17152 l_null_mand_comp_info_rec           MSC_ATP_CTO.mand_comp_info_rec;
17153 l_pto_cntr                          number;
17154 l_count                             number;
17155 l_parent_idx                        number;
17156 l_process_included_item_flag        number := 0;
17157 --e_cto_rearch
17158 
17159 -- ship_rec_cal
17160 l_shipping_cal_code		VARCHAR2(14);
17161 l_receiving_cal_code		VARCHAR2(14);
17162 l_manufacturing_cal_code	VARCHAR2(14);
17163 l_intransit_cal_code		VARCHAR2(14);
17164 
17165 l_quantity_uom		VARCHAR2(3);
17166 l_quantity_ordered	NUMBER;
17167 l_conversion_rate       NUMBER;
17168 --bug3583705
17169 l_encoded_text                  varchar2(4000);
17170 l_msg_app                       varchar2(50);
17171 l_msg_name                      varchar2(30);
17172 
17173 l_global_availability                         varchar2(1); --3720018
17174 l_temp                                        VARCHAR2(1); --3720018
17175 l_null_remove_req_level_rec                   MSC_ATP_PVT.remove_request_level_rec; --3720018
17176 l_old_source_org_id                           NUMBER; --3720018
17177 l_delete_atp_rec                              MSC_ATP_PVT.DELETE_ATP_REC ;--3720018
17178 l                                             NUMBER; --3720018
17179 x_plan_info_rec                               MSC_ATP_PVT.plan_info_rec ;
17180 l_same_org_count                              NUMBER;
17181 l_feeder_count                                NUMBER;
17182 l_consume_so_profile_value                    NUMBER;
17183 
17184 
17185 l_atp_progess_bar varchar2(1) := nvl(fnd_profile.value('MRP_ATP_PROGRESS_BAR'),'Y');  --7419485
17186 BEGIN
17187 
17188     IF PG_DEBUG in ('Y', 'C') THEN
17189        msc_sch_wb.atp_debug('***** Begin Call_Schedule Procedure *****');
17190     END IF;
17191 
17192     -- Initialize global variables to work in a connection pooling env
17193     G_INV_CTP          := FND_PROFILE.value('INV_CTP') ;
17194     G_SUB_COMP         := NVL(FND_PROFILE.value('MRP_ATP_SUB_COMP'), 'N');
17195     G_ALLOCATED_ATP    := NVL(FND_PROFILE.value('MSC_ALLOCATED_ATP'),'N');
17196     G_HIERARCHY_PROFILE:= NVL(FND_PROFILE.VALUE('MSC_CLASS_HIERARCHY'), 2);
17197     G_INF_SUP_TF_PAD  :=  GREATEST(CEIL(NVL(FND_PROFILE.VALUE('MSC_ATP_INFINITE_TF_PAD'),0)),0);
17198 
17199     --bug 3625348: reset global variable
17200     MSC_ATP_CTO.G_MODEL_BOM_IS_COLLECTED := 2;
17201     MSC_ATP_CTO.G_MODEL_IS_PRESENT  := 2;
17202     MSC_ATP_CTO.G_MODEL_IS_PRESENT_IN_SET  := 2;
17203     MSC_ATP_CTO.G_INCLUDED_ITEM_IS_PRESENT  := 2;
17204 
17205     -- ngoel 9/27: Reset G_SR_PARTNER_SITE_ID GLOBAL Variable
17206     G_SR_PARTNER_SITE_ID := NULL;
17207 
17208     -- 24x7
17209     G_DOWNTIME_HIT := 'N';
17210 
17211 
17212     MSC_ATP_PVT.G_INSTANCE_ID := p_instance_id;
17213 
17214     G_TIME_PHASED_SET := 'N';   -- For summary enhancement
17215 
17216     -- initialize API return status to success
17217     x_return_status := FND_API.G_RET_STS_SUCCESS;
17218 
17219     -- Bug 2356892
17220     IF PG_DEBUG in ('Y', 'C') THEN
17221        msc_sch_wb.atp_debug('Call_Schedule: ' || 'Check_CTO: G_INV_CTP : '||G_INV_CTP);
17222     END IF;
17223     IF (NVL(G_INV_CTP,-1) not in (4,5)) THEN
17224         G_INV_CTP := 5;
17225         IF PG_DEBUG in ('Y', 'C') THEN
17226            msc_sch_wb.atp_debug('Call_Schedule: ' || 'Check_CTO: Setting G_INV_CTP to 5');
17227         END IF;
17228     END IF;
17229 
17230     --3720018, setting l_global_availability flag based on duplicity of order_line_id.
17231 
17232     BEGIN
17233     l_global_availability := 'Y';
17234     select 'x'
17235        into l_temp
17236        from mrp_atp_schedule_temp
17237        where session_id = p_session_id
17238        group by order_line_id
17239        having count(*) > 1;
17240     EXCEPTION
17241       WHEN no_data_found THEN
17242           l_global_availability := 'N';
17243           IF PG_DEBUG in ('Y', 'C') THEN
17244              --bug4291375 changed debug statements
17245              --msc_sch_wb.atp_debug('Exception := ' || SQLERRM);
17246              msc_sch_wb.atp_debug('Setting l_global_availability as No' );
17247           END IF;
17248       WHEN TOO_MANY_ROWS THEN
17249           l_global_availability := 'Y';
17250           IF PG_DEBUG in ('Y', 'C') THEN
17251              --bug4291375 changed debug statements
17252              --msc_sch_wb.atp_debug('Exception := ' || SQLERRM);
17253              msc_sch_wb.atp_debug('Setting l_global_availability as Yes' );
17254           END IF;
17255     END;
17256 
17257     IF PG_DEBUG in ('Y', 'C') THEN
17258           msc_sch_wb.atp_debug('Call_Schedule: ' || 'l_global_availability = ' || l_global_availability );
17259 		  msc_sch_wb.atp_debug('Call_Schedule: ' || 'p_atp_table.attribute_02 = ' || p_atp_table.attribute_02(1));
17260     END IF; --3720018
17261 
17262     IF (nvl(p_atp_table.attribute_02(1), -1) = 3) THEN --web service call
17263 		MSC_ATP_PVT.G_WS_CALL := 'Y';
17264 		IF PG_DEBUG in ('Y', 'C') THEN
17265 			msc_sch_wb.atp_debug('Call_Schedule: ' || 'G_WS_CALL = '||MSC_ATP_PVT.G_WS_CALL);
17266 			msc_sch_wb.atp_debug('Call_Schedule: ' || 'This is a Web Service Call');
17267 		END IF;
17268 	END IF;
17269 	-- initial null collections so that we can use these
17270     -- collections to initialize collections for next iterations
17271     MSC_ATP_PVT.G_SESSION_ID := p_session_id;
17272     IF PG_DEBUG in ('Y', 'C') THEN
17273        msc_sch_wb.atp_debug('Call_Schedule: ' || 'G_SESSION_ID = '||MSC_ATP_PVT.G_SESSION_ID);
17274 	END IF;
17275 
17276     l_null_atp_table := x_atp_table;
17277     l_null_atp_period := x_atp_period;
17278     l_null_atp_supply_demand := x_atp_supply_demand;
17279     l_null_atp_details := x_atp_details;
17280 
17281     --alloc_pp
17282     IF MSC_ATP_PVT.G_INV_CTP = 4 AND MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y' AND
17283        MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1 THEN
17284 
17285         MSC_ATP_PVT.G_ALLOCATION_METHOD := NVL(FND_PROFILE.VALUE('MSC_ALLOCATION_METHOD'), 2);
17286         IF PG_DEBUG in ('Y', 'C') THEN
17287            msc_sch_wb.atp_debug('Call_Schedule: ' || 'G_ALLOCATION_METHOD := ' || MSC_ATP_PVT.G_ALLOCATION_METHOD);
17288         END IF;
17289     END IF;
17290 
17291     --3720018, call in case of ATP inquiry or global availability from scheduled line of Sales Order pad.
17292     IF ((p_atp_table.Action(1) = ATPQUERY) AND
17293         (NVL(p_atp_table.calling_module(1), -99) <> 724)) OR
17294         ((p_atp_table.Action(1) = ATPQUERY) AND
17295         (NVL(p_atp_table.calling_module(1), -99) = -99) AND
17296         (l_global_availability = 'Y'))  THEN
17297 
17298             -- Call_delete_row will take care of calling delete_row for the whole request.
17299             -- The input to this procedure is p_atp_table.
17300             -- l_delete_atp_rec will contain array to be used to call undo_delete_row.
17301             MSC_ATP_DB_UTILS.call_delete_row (
17302                              p_instance_id,
17303                              p_atp_table,
17304                              p_refresh_number,
17305                              l_delete_atp_rec,
17306                              l_return_status);
17307 
17308             IF PG_DEBUG in ('Y', 'C') THEN --3720018
17309                  msc_sch_wb.atp_debug('Call_Schedule: l_return_status: ' || l_return_status);
17310                  msc_sch_wb.atp_debug('Call_Schedule: l_delete_atp_rec.error_code(1): ' || l_delete_atp_rec.error_code(1));
17311             END IF;
17312             IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
17313                         IF PG_DEBUG in ('Y', 'C') THEN
17314                            msc_sch_wb.atp_debug('Call_Schedule: ' || 'Error in Call_delete_row');
17315                         END IF;
17316                         x_return_status := FND_API.G_RET_STS_ERROR;
17317                         RAISE FND_API.G_EXC_ERROR;
17318             END IF;
17319             IF (l_delete_atp_rec.error_code(1) = MSC_ATP_PVT.TRY_ATP_LATER) OR
17320                (l_delete_atp_rec.error_code(1) = MSC_ATP_PVT.SUMM_CONC_PROG_RUNNING) THEN
17321                    x_atp_table := p_atp_table;
17322                    FOR i in 1..x_atp_table.action.count LOOP
17323                       x_atp_table.error_code(i) := l_delete_atp_rec.error_code(1);
17324                    END LOOP;
17325                RETURN;
17326             END IF;
17327     END IF;
17328     -- 3720018
17329 
17330     -- start from the first row of the atp list
17331     -- for each row , arrival set or ship set, call Schedule function
17332     -- we assume that the records are ordered in the way you would
17333     -- like scheduling to consider them.
17334     -- And we also assume that when we have ship set or arrival set,
17335     -- the lines in each set are grouped together.
17336     -- Assume a line is either in a ship set or in an arrival set
17337     -- but not both.
17338 
17339     i := p_atp_table.Action.FIRST;
17340 
17341     IF PG_DEBUG in ('Y', 'C') THEN
17342        msc_sch_wb.atp_debug('Call_Schedule, i='||i);
17343            msc_sch_wb.atp_debug('Call_Schedule: ' || 'G_ALLOCATION_METHOD := ' || MSC_ATP_PVT.G_ALLOCATION_METHOD);
17344         END IF;
17345 
17346     LOOP
17347       -- make sure we set the variables to null so that it is clean
17348       -- before we assign new value to them.
17349       l_atp_input_table := l_null_atp_table;
17350       l_atp_output_table := l_null_atp_table;
17351       l_atp_period := l_null_atp_period;
17352       l_atp_supply_demand := l_null_atp_supply_demand;
17353       l_atp_details := l_null_atp_details;
17354        --s_cto_rearch
17355       --l_mand_comp_info_rec := l_null_mand_comp_info_rec;
17356       l_set_fail_flag := 'N';
17357       MSC_ATP_CTO.G_MODEL_IS_PRESENT_IN_SET := 2;
17358       --e_cto_rearch
17359 
17360 
17361       IF PG_DEBUG in ('Y', 'C') THEN
17362          msc_sch_wb.atp_debug('Call_Schedule, i='||i);
17363       END IF;
17364 
17365       EXIT WHEN i is NULL;
17366       /* ---s_cto_rearch
17367       MSC_SATP_FUNC.Assign_Atp_Input_Rec(p_atp_table,
17368                            i,
17369                            l_atp_input_table,
17370                            l_return_status );
17371 
17372       j := p_atp_table.Action.NEXT(i);
17373       e_cto_rearch */
17374 
17375       ---s_cto_rearch
17376 
17377 
17378       IF  NOT (l_mand_comp_info_rec.sr_inventory_item_id.count = 0) AND
17379                       l_included_item_cntr < l_mand_comp_info_rec.sr_inventory_item_id.count THEN
17380           ---process the included item for non SMC PTO model.
17381            IF PG_DEBUG in ('Y', 'C') THEN
17382               msc_sch_wb.atp_debug('Process Included item');
17383               msc_sch_wb.atp_debug('l_included_item_cntr := ' || l_included_item_cntr);
17384            END IF;
17385 
17386           l_included_item_cntr := nvl(l_included_item_cntr, 0) + 1;
17387           MSC_ATP_CTO.G_INCLUDED_ITEM_IS_PRESENT := 1;
17388 
17389           MSC_SATP_FUNC.Assign_Atp_Input_Rec(p_atp_table,
17390                                   j,
17391                                   l_atp_input_table,
17392                                   l_return_status );
17393 
17394 
17395           l_atp_input_table.mandatory_item_flag(1) := 1;
17396           l_atp_input_table.quantity_ordered(1) :=
17397                                             l_mand_comp_info_rec.quantity(l_included_item_cntr);
17398 
17399           l_atp_input_table.inventory_item_id(1) :=
17400                                             l_mand_comp_info_rec.sr_inventory_item_id(l_included_item_cntr);
17401 
17402           l_atp_input_table.parent_line_id(1) := p_atp_table.identifier(j);
17403           l_atp_input_table.sequence_number(1) :=  p_atp_table.sequence_number(j) * (1 + (.0001 * l_included_item_cntr));
17404 
17405           l_atp_input_table.Quantity_UOM(1) := l_mand_comp_info_rec.uom_code(l_included_item_cntr);
17406 
17407           IF l_included_item_cntr = l_mand_comp_info_rec.sr_inventory_item_id.count THEN
17408                 l_mand_comp_info_rec := l_null_mand_comp_info_rec;
17409                 j := p_atp_table.Action.NEXT(j);
17410           END IF;
17411 
17412           l_process_included_item_flag := 0;
17413       ELSE
17414          j := i;
17415          LOOP --- loop for finding lines for a set
17416 
17417             EXIT WHEN l_process_included_item_flag = 1;
17418 
17419             IF PG_DEBUG in ('Y', 'C') THEN
17420                 msc_sch_wb.atp_debug('Process ordered item');
17421             END IF;
17422 
17423             ---either  this is first item or PTO included items have already been ATPed. Null out the table
17424             l_mand_comp_info_rec := l_null_mand_comp_info_rec;
17425 
17426             MSC_SATP_FUNC.Assign_Atp_Input_Rec(p_atp_table,
17427                                   j,
17428                                   l_atp_input_table,
17429                                   l_return_status );
17430 
17431             IF p_atp_table.ato_model_line_id(j) is not null THEN --and p_atp_table.bom_item_type(j) = 1 THEN
17432                 --set a flag to tell that a model is present.
17433                 -- this flag will be used in put into temp table in results mode to
17434                 -- selectively update the data for child lines.
17435                 IF PG_DEBUG in ('Y', 'C') THEN
17436                    msc_sch_wb.atp_debug('ato model -line is := ' || p_atp_table.ato_model_line_id(j));
17437                 END IF;
17438                 MSC_ATP_CTO.G_MODEL_IS_PRESENT := 1;
17439                 MSC_ATP_CTO.G_MODEL_IS_PRESENT_IN_SET := 1;
17440 
17441 
17442                 IF NVL(MSC_ATP_CTO.G_MODEL_BOM_IS_COLLECTED, 2) = 2 and
17443                                    (NVL(MSC_ATP_PVT.G_SYNC_ATP_CHECK, 'N') = 'N' ) THEN
17444 
17445                    MSC_ATP_CTO.Populate_CTO_Bom(MSC_ATP_PVT.G_SESSION_ID, p_refresh_number, null);
17446                    MSC_ATP_CTO.G_MODEL_BOM_IS_COLLECTED := 1;
17447 
17448                 END IF;
17449             END IF;
17450 
17451             IF PG_DEBUG in ('Y', 'C') THEN
17452                  msc_sch_wb.atp_debug('attribute_06 := ' ||  NVL(p_atp_table.attribute_06(j), -1));
17453                  msc_sch_wb.atp_debug('Validation Org := ' || NVL(p_atp_table.validation_org(j), -1));
17454                  msc_sch_wb.atp_debug('source_organization_id := ' || NVL(p_atp_table.source_organization_id(j), -1));
17455             END IF;
17456 
17457             --- raise invalid item org combo error only for non-aps instance
17458             IF (p_atp_table.attribute_06(j) is null and NVL(MSC_ATP_PVT.G_CALLING_MODULE, -1) <> 724
17459                 -- if validation org and source_org are null then we cannot validate the item
17460                 -- hence do not raise the error
17461                 and (p_atp_table.validation_org(j) is not null
17462                 or p_atp_table.source_organization_id(j) is not null))  or
17463                p_atp_table.oss_error_code(j) in ( MSC_ATP_PVT.MUTUALLY_EXCLUSIVE_OSS,
17464                                                   MSC_ATP_PVT.OSS_ERROR_AT_LOWER_LEVEL,
17465                                                   MSC_ATP_PVT.OSS_ERROR_AT_THIS_LEVEL) or
17466                l_set_fail_flag = 'Y'  THEN
17467 
17468                l_count := l_atp_input_table.inventory_item_id.count;
17469 
17470                If p_atp_table.attribute_06(j) is null THEN
17471                     IF PG_DEBUG in ('Y', 'C') THEN
17472                         msc_sch_wb.atp_debug('Invalid Item Org Combo');
17473                     END IF;
17474 
17475 
17476                     l_atp_input_table.error_code(l_count) := INVALID_ITEM_ORG_COMBINATION;
17477 
17478                ELSIF p_atp_table.oss_error_code(j) in  (MSC_ATP_PVT.MUTUALLY_EXCLUSIVE_OSS,
17479                                                   MSC_ATP_PVT.OSS_ERROR_AT_LOWER_LEVEL,
17480                                                   MSC_ATP_PVT.OSS_ERROR_AT_THIS_LEVEL) THEN
17481 
17482                     IF PG_DEBUG in ('Y', 'C') THEN
17483                        msc_sch_wb.atp_debug('Inavlid CTO sourcing');
17484                     END IF;
17485                     l_atp_input_table.error_code(l_count) := p_atp_table.oss_error_code(j);
17486 
17487                ELSE
17488                     l_atp_input_table.error_code(l_count) := GROUPEL_ERROR;
17489 
17490                END IF;
17491                l_set_fail_flag := 'Y';
17492 
17493                j := p_atp_table.Action.NEXT(j);
17494 
17495             ELSIF NVL(p_atp_table.included_item_flag(j), 1) = 2 and
17496                 -- calling module must pass 2 else we will assume that
17497                 -- calling module has already exploded the included items
17498                 -- This is to provided backward compatibility
17499                 p_atp_table.pick_components_flag(j) = 'Y' and
17500                 p_atp_table.bom_item_type(j) in (1, 2)  THEN
17501 
17502                 IF PG_DEBUG in ('Y', 'C') THEN
17503                     msc_sch_wb.atp_debug('Explode Included Items');
17504                     msc_sch_wb.atp_debug('First Do UOM Convesrion');
17505                 END IF;
17506 
17507                 MSC_ATP_PROC.inv_primary_uom_conversion(p_instance_id,
17508                                        p_atp_table.validation_org(j),
17509                                        p_atp_table.inventory_item_id(j),
17510                                        p_atp_table.Quantity_UOM(i),
17511                                        l_quantity_uom,
17512                                        l_conversion_rate);
17513 
17514                 IF PG_DEBUG in ('Y', 'C') THEN
17515                     msc_sch_wb.atp_debug('l_conversion_rate');
17516                     msc_sch_wb.atp_debug('QTY Ordered := ' ||  p_atp_table.Quantity_Ordered(j));
17517 
17518                 END IF;
17519 
17520                 l_quantity_ordered := p_atp_table.Quantity_Ordered(j) * l_conversion_rate;
17521                 l_quantity_ordered := ROUND( l_quantity_ordered, 6); --4562646
17522                 IF PG_DEBUG in ('Y', 'C') THEN
17523                     msc_sch_wb.atp_debug('QTY Ordered  after conversion:= ' ||  l_quantity_ordered);
17524                     msc_sch_wb.atp_debug('Now call Get Mand Comp');
17525                 END IF;
17526 
17527                 --- included items are not passed. Explode them
17528                 MSC_ATP_PVT.G_PTF_DATE := sysdate;
17529                 MSC_ATP_CTO.Get_Mandatory_Components( -1,
17530                                                      p_instance_id,
17531                                                      p_atp_table.validation_org(j),
17532                                                      p_atp_table.inventory_item_id(j),
17533                                                      p_atp_table.quantity_ordered(j),
17534                                                      NVL(p_atp_table.Requested_Ship_Date(j),
17535                                                              p_atp_table.Requested_Arrival_Date(j)),
17536                                                      null,
17537                                                      l_mand_comp_info_rec);
17538 
17539                 --- if SMC PTO then add to l_atp_input_table else add to l_included_item_table
17540                 IF PG_DEBUG in ('Y', 'C') THEN
17541                     msc_sch_wb.atp_debug('After getting included items');
17542                     msc_sch_wb.atp_debug('Ship set name := ' ||  NVL(p_atp_table.Ship_Set_Name(j), 'XYZ'));
17543                 END IF;
17544 
17545                 IF p_atp_table.Ship_Set_Name(j) is not null then
17546 
17547                    --its an SMC PTO or KIT, add to the same set
17548 
17549                    IF PG_DEBUG in ('Y', 'C') THEN
17550                        msc_sch_wb.atp_debug('Comp Count := ' || l_mand_comp_info_rec.sr_inventory_item_id.count);
17551                        msc_sch_wb.atp_debug('SMC or PTO Kit');
17552                    END IF;
17553                    FOR l_pto_cntr in 1..l_mand_comp_info_rec.sr_inventory_item_id.count LOOP
17554                         MSC_ATP_CTO.G_INCLUDED_ITEM_IS_PRESENT := 1;
17555                         MSC_SATP_FUNC.Assign_Atp_Input_Rec(p_atp_table,
17556                                   j,
17557                                   l_atp_input_table,
17558                                   l_return_status );
17559                         IF PG_DEBUG in ('Y', 'C') THEN
17560                             msc_sch_wb.atp_debug('l_pto_cntr := ' || l_pto_cntr);
17561                         END IF;
17562                         --cr:ngoel: add seq num as well as UOM code
17563                         l_count := l_atp_input_table.inventory_item_id.count;
17564                         l_atp_input_table.mandatory_item_flag(l_count) := 1;
17565                         l_atp_input_table.quantity_ordered(l_count) :=
17566                                            l_mand_comp_info_rec.quantity(l_pto_cntr);
17567 
17568                         l_atp_input_table.inventory_item_id(l_count) :=
17569                                                           l_mand_comp_info_rec.sr_inventory_item_id(l_pto_cntr);
17570 
17571                         l_atp_input_table.parent_line_id(l_count) := p_atp_table.identifier(j);
17572                         IF PG_DEBUG in ('Y', 'C') THEN
17573                             msc_sch_wb.atp_debug('Assign Seq num');
17574                         END IF;
17575                         ---multiplying by .000001 will make sure that std mand comp are inserted in the
17576                         -- table after the parent
17577                         begin
17578                            l_atp_input_table.sequence_number(l_count) := p_atp_table.sequence_number(j) *
17579                                                                                            (1 + (.0001 * l_pto_cntr));
17580                         EXCEPTION
17581                              when others then
17582                                 IF PG_DEBUG in ('Y', 'C') THEN
17583                                     msc_sch_wb.atp_debug('Error := ' || SQLERRM);
17584                                 END IF;
17585                         END;
17586                         IF PG_DEBUG in ('Y', 'C') THEN
17587                             msc_sch_wb.atp_debug('assign UOM');
17588                             msc_sch_wb.atp_debug('seq count := ' || l_atp_input_table.sequence_number.count);
17589                             msc_sch_wb.atp_debug(' uom cnt := ' || l_atp_input_table.Quantity_UOM.count);
17590                             msc_sch_wb.atp_debug('uom code cnt := ' || l_mand_comp_info_rec.uom_code.count);
17591                         END IF;
17592                         l_atp_input_table.Quantity_UOM(l_count) := l_mand_comp_info_rec.uom_code(l_pto_cntr);
17593                         IF p_atp_table.bom_item_type(j)  = 4 and p_atp_table.Ship_Set_Name(j) is null THEN
17594                            IF PG_DEBUG in ('Y', 'C') THEN
17595                                msc_sch_wb.atp_debug('process KIT');
17596                            END IF;
17597                             -- If its a PTO KIt  and if ship set name is null then we populate
17598                             --shipset name so that whole kit is processed as one entity
17599                             l_atp_input_table.Ship_Set_Name(l_parent_idx) := p_atp_table.identifier(j);
17600                             l_atp_input_table.Ship_Set_Name(l_count) :=  p_atp_table.identifier(j);
17601                         END IF;
17602 
17603                    END LOOP;
17604 
17605                    ---null out mand_comp_table
17606                    l_mand_comp_info_rec := l_null_mand_comp_info_rec;
17607                    j := p_atp_table.Action.NEXT(j);
17608                 ELSE ---  IF p_atp_table.Ship_Set_Name(j) is not null then
17609                    IF PG_DEBUG in ('Y', 'C') THEN
17610                        msc_sch_wb.atp_debug('This is not SMC PTO');
17611                    END IF;
17612                    --non smc PTO
17613                    l_included_item_cntr := 0;
17614                    IF l_mand_comp_info_rec.sr_inventory_item_id.count > 0 THEN
17615                       l_process_included_item_flag := 1;
17616                    ELSE
17617                       j := p_atp_table.Action.NEXT(j);
17618                    END IF;
17619                 END IF;
17620 
17621             ELSE
17622                ---increase the counter only if its a non PTO item or icluded item already exploded
17623                j := p_atp_table.Action.NEXT(j);
17624             END IF;
17625 
17626 
17627             IF (j IS NULL) THEN
17628                EXIT;
17629             END IF;
17630 
17631             /* Fixed as part of ship_rec_cal/sup int LT
17632             Prev condition was not handling arrival sets
17633             EXIT WHEN NVL(p_atp_table.Ship_Set_Name(i),-99) <> NVL(p_atp_table.Ship_Set_Name(j),-100) OR
17634                        NVL(p_atp_table.Arrival_Set_Name(i),-100) <> NVL(p_atp_table.Arrival_Set_Name(j),-100);*/
17635              EXIT WHEN (NVL(NVL(to_char(p_atp_table.Ship_Set_Id(i)),p_atp_table.Ship_Set_Name(i)),-99) <> NVL(NVL(to_char(p_atp_table.Ship_Set_Id(j)),p_atp_table.Ship_Set_Name(j)),-100) OR     --bug 7394340,9461317 changed ship_set_name to ship_set_id
17636                         -- Bug 3729551, added for support of Global availability for a shipset from OM
17637                         NVL(p_atp_table.source_organization_id(i), -1) <> NVL(p_atp_table.source_organization_id(j), -1)) AND
17638                         NVL(NVL(to_char(p_atp_table.Arrival_Set_Id(i)),p_atp_table.Arrival_Set_Name(i)),-99) <> NVL(NVL(to_char(p_atp_table.Arrival_Set_Id(j)),p_atp_table.Arrival_Set_Name(j)),-100);--bug7394340,9461317 change arr_set_name to arr_set_id
17639 
17640          END LOOP;   --- end loop for finding lines for a set
17641       END IF;---IF l_included_item_cntr <= l_m
17642       ---e_cto_reach
17643 
17644       /* s_cto_rearch --- modify the way we do the looping for CTO project
17645       IF (p_atp_table.Ship_Set_Name(i) is not NULL
17646           OR  p_atp_table.Arrival_Set_Name(i) is not NULL)
17647           AND  j is not null  THEN
17648 
17649         LOOP
17650           -- Bug 1868923, Added NVL for ship/ arrival set exit conditions.
17651 
17652           EXIT WHEN NVL(p_atp_table.Ship_Set_Name(i),-99) <> NVL(p_atp_table.Ship_Set_Name(j),-99) OR
17653                     NVL(p_atp_table.Arrival_Set_Name(i),-99) <> NVL(p_atp_table.Arrival_Set_Name(j),-99);
17654 
17655 
17656           IF PG_DEBUG in ('Y', 'C') THEN
17657              msc_sch_wb.atp_debug('Call_Schedule, j='||j);
17658           END IF;
17659 
17660           -- make sure we are in the same ship set before we assign the value.
17661 
17662           MSC_SATP_FUNC.Assign_Atp_Input_Rec(p_atp_table,
17663                                j,
17664                                l_atp_input_table,
17665                                l_return_status );
17666 
17667           j := p_atp_table.Action.NEXT(j);
17668 
17669           IF (j IS NULL) THEN
17670             EXIT;
17671           END IF;
17672 
17673 
17674         END LOOP;
17675 
17676       END IF;
17677 
17678       e_cto_rearch */
17679       -- Added by ngoel on 1/23/2001.
17680       -- In case of PDS, check if this line/set is due to an internal Sales Order.
17681       -- Check OE_Flag='Y', if internal sales order, no ATP is required to be done
17682       -- as the supply has already been reserved for items in this line/set.
17683 
17684       m := l_atp_input_table.Action.FIRST;
17685       IF m is not null THEN
17686 	 IF PG_DEBUG in ('Y', 'C') THEN
17687 	    msc_sch_wb.atp_debug('Call_Schedule: ' || 'OE Flag (Before ISO check) : '||l_atp_input_table.OE_flag(m));
17688 	 END IF;
17689       --Bug 4279623 	4333596
17690       IF l_atp_input_table.oe_flag(m) = 'Y' and MSC_ATP_PVT.G_INV_CTP = 4 THEN
17691          l_consume_so_profile_value :=  NVL(FND_PROFILE.VALUE('MSC_ATP_CHECK_INT_SALES_ORDERS'), NVL(FND_PROFILE.VALUE('MSC_CONSUME_INT_SALES_ORDERS'), NO)); --5564082
17692          --l_consume_so_profile_value :=  NVL(FND_PROFILE.VALUE('MSC_CONSUME_INT_SALES_ORDERS'), NO);
17693          IF PG_DEBUG in ('Y', 'C') THEN
17694             msc_sch_wb.atp_debug('Before get_plan_info_call:l_consume_so_profile_value ' || l_consume_so_profile_value);
17695             msc_sch_wb.atp_debug('Before get_plan_info_call:MSC_CONSUME_INT_SALES_ORDERS ' || l_consume_so_profile_value);
17696             msc_sch_wb.atp_debug('New profile: '||FND_PROFILE.VALUE('MSC_ATP_CHECK_INT_SALES_ORDERS')); --5564082
17697          END IF;
17698          IF  l_consume_so_profile_value  = CHECK_ORG_IN_PLAN THEN
17699              MSC_ATP_PROC.get_plan_info(p_instance_id,
17700                                         l_atp_input_table.inventory_item_id(m),
17701                                         l_atp_input_table.source_organization_id(m),
17702                                         null,
17703                                         x_plan_info_rec);
17704 
17705              IF PG_DEBUG in ('Y', 'C') THEN
17706                 msc_sch_wb.atp_debug('After get_plan_info_call:plan_id : ' || x_plan_info_rec.plan_id );
17707              END IF;
17708 
17709              IF x_plan_info_rec.plan_id > 0 THEN
17710 
17711                 IF PG_DEBUG in ('Y', 'C') THEN
17712                    msc_sch_wb.atp_debug('Plan found for the item in the source org.');
17713                    msc_sch_wb.atp_debug('l_atp_input_table.inventory_item_id(m)' || l_atp_input_table.inventory_item_id(m) );
17714                    msc_sch_wb.atp_debug('l_atp_input_table.internal_org_id(m)' || l_atp_input_table.internal_org_id(m) );
17715                    msc_sch_wb.atp_debug('l_atp_input_table.source_organization_id(m)' || l_atp_input_table.source_organization_id(m) );
17716                    msc_sch_wb.atp_debug('l_atp_input_table.organization_id(m)' || l_atp_input_table.organization_id(m) );
17717                 END IF;
17718 
17719                 select count(*)
17720                 into l_same_org_count
17721                 from msc_system_items i
17722                 where i.plan_id = x_plan_info_rec.plan_id
17723                 and i.organization_id = l_atp_input_table.internal_org_id(m)
17724                 and i.sr_instance_id = p_instance_id
17725                 and i.sr_inventory_item_id = l_atp_input_table.inventory_item_id(m);
17726 
17727                 IF l_same_org_count > 0 THEN
17728                    l_atp_input_table.oe_flag(m) := 'Y';
17729                    IF PG_DEBUG in ('Y', 'C') THEN
17730                       msc_sch_wb.atp_debug('Both the Orgs are in the same plan and the item is planned.');
17731                    END IF;
17732                 ELSE
17733                    IF PG_DEBUG in ('Y', 'C') THEN
17734                       msc_sch_wb.atp_debug('Both the Orgs are in NOT the same plan');
17735                    END IF;
17736                    select count(*) into l_feeder_count
17737                    from msc_system_items
17738                    where plan_id  in ( select plans.plan_id input_plan_id
17739                                            from msc_plan_schedules sched,
17740                                            msc_designators md,
17741                                            msc_plans plans
17742                                            where sched.plan_id = x_plan_info_rec.plan_id
17743                                            and sched.input_type = 1
17744                                            and sched.designator_type in (2,3,4,5)
17745                                            and sched.input_schedule_id = md.designator_id
17746                                            -- we need not consider plans collected from source
17747                                            and md.collected_flag = 2
17748                                            and md.designator = plans.compile_designator
17749                                            and md.sr_instance_id = plans.sr_instance_id
17750                                            and md.organization_id = plans.organization_id
17751                                          )
17752                    and organization_id = l_atp_input_table.internal_org_id(m)
17753                    and sr_instance_id = p_instance_id
17754                    and sr_inventory_item_id =  l_atp_input_table.inventory_item_id(m);
17755 
17756                   IF l_feeder_count > 0 then
17757                      l_atp_input_table.oe_flag(m) := 'Y';
17758                      IF PG_DEBUG in ('Y', 'C') THEN
17759                         msc_sch_wb.atp_debug('There is a feeder plan which has the item palnned. we need not do ATP');
17760                      END IF;
17761                   ELSE
17762                      l_atp_input_table.oe_flag(m) := 'N';
17763                      IF PG_DEBUG in ('Y', 'C') THEN
17764                         msc_sch_wb.atp_debug('There is no feeder plan too. We have to do ATP');
17765 	 END IF;
17766                   END IF; -- l_feeder_count > 0
17767                 END IF; --if l_same_org_count > 0 then
17768              ELSE
17769                l_atp_input_table.oe_flag(m) := 'Y';
17770                IF PG_DEBUG in ('Y', 'C') THEN
17771                   msc_sch_wb.atp_debug('The item is not planned at all. No need to do ATP');
17772                END IF;
17773              END IF; -- x_plan_info_rec.count > 0
17774          ELSIF l_consume_so_profile_value = YES THEN
17775             l_atp_input_table.oe_flag(m) := 'N';
17776             IF PG_DEBUG in ('Y', 'C') THEN
17777                msc_sch_wb.atp_debug('Profile value is set to CHECK ATP. We need to do ATP always.');
17778             END IF;
17779 
17780          END IF; -- nvl(fnd_profile.value('msc_consume_int_sales_orders'), '2') = 3
17781       END IF; -- l_atp_input_table.oe_flag(1) = 'y' and msc_atp_pvt.g_inv_ctp = 4
17782      --Bug 4279623 	4333596
17783         msc_sch_wb.atp_debug('l_atp_input_table.oe_flag(m) = '||l_atp_input_table.oe_flag(m)); --5564082
17784 	 IF l_atp_input_table.OE_flag(m) = 'Y' AND MSC_ATP_PVT.G_INV_CTP = 4 THEN
17785 
17786 	    IF PG_DEBUG in ('Y', 'C') THEN
17787 	       msc_sch_wb.atp_debug('Call_Schedule: ' || 'Inside Internal Sales order import for OM');
17788 	       msc_sch_wb.atp_debug('Call_Schedule: ' || 'Here ISO BUG x_atp_table count: ' || x_atp_table.action.count);
17789                msc_sch_wb.atp_debug('Call_Schedule: ' || 'Here ISO BUG l_atp_input_table count: ' || l_atp_input_table.action.count);
17790 
17791 	    END IF;
17792 
17793 	   l_atp_output_table := l_atp_input_table;    --bug 10268596
17794 
17795            BEGIN --bug3583705
17796             WHILE m IS NOT NULL LOOP
17797                 /* Bug 3449812 - Documenting the logic of plan suggested internal sales orders
17798                    1.   If DLT is not available then obtain it.
17799                    2.   Get all the necessary calendars.
17800                    3.   If date type = ship then
17801                    4.       Obtain ship date by validating RSD on OSC.
17802                    5.   ELse
17803                    6.       Calculate ship date from RAD honoring all calendars.
17804                    7.   End if
17805                    8.   If override then
17806                    9.       If date type = ship then
17807                   10.           ship date = RSD
17808                   11.       Else
17809                   12.           arrival date = RAD
17810                   13.           ship date = Calculate ship date from RAD honoring all calendars.
17811                   14.       End if
17812                   15.   Else
17813                   16.       ship date = greatest(ship date, sysdate)
17814                   17.   End if
17815                   18.   If arrival date is null then calculate it from ship date.
17816                 */
17817 
17818                 /* Bug 2290633 : krajan : 04/05/02 */
17819 
17820                 l_delivery_lead_time := l_atp_input_table.delivery_lead_time(m);
17821 
17822                 -- dsting dlt
17823                 IF (NVL(l_atp_input_table.delivery_lead_time(m), -1) = -1 ) THEN
17824                    IF PG_DEBUG IN ('Y', 'C') THEN
17825                       msc_sch_wb.atp_debug('delivery lead time is not specified');
17826                    END IF;
17827 
17828                    l_ship_method := l_atp_output_table.ship_method(m);
17829                    -- Bug 3449812 - Base DLT on internal_org_id if available
17830                    -- Bug 3515520, don't use org in case customer/site is populated
17831                    -- IF l_atp_output_table.internal_org_id(m) IS NOT NULL THEN
17832 
17833                    IF (l_atp_output_table.internal_org_id(m) IS NOT NULL) OR
17834                       (l_atp_output_table.organization_id(m) IS NOT NULL AND
17835                        l_atp_output_table.customer_id(m) IS NULL AND l_atp_output_table.customer_site_id(m) IS NULL) THEN
17836                        MSC_ATP_PROC.get_delivery_lead_time(
17837                                     l_atp_output_table.source_organization_id(m),
17838                                     NULL, -- from_loc
17839                                     p_instance_id,
17840                                     -- x_atp_table.internal_org_id(m), -- Bug 3515520
17841                                     NVL(l_atp_output_table.internal_org_id(m), l_atp_output_table.organization_id(m)), --Bug 3515520
17842                                     NULL, -- to loc
17843                                     p_instance_id,
17844                                     NULL, -- customer id
17845                                     NULL, -- customer site id
17846                                     NULL, -- supplier_id
17847                                     NULL, -- supplier_site_id
17848                                     MSC_ATP_PVT.G_SESSION_ID,
17849                                     NULL, -- partner site id
17850                                     l_ship_method,
17851                                     l_delivery_lead_time
17852                                     );
17853                    ELSE
17854                        MSC_ATP_PROC.get_delivery_lead_time(
17855                                     l_atp_output_table.source_organization_id(m),
17856                                     NULL, -- from_loc
17857                                     p_instance_id,
17858                                     -- x_atp_table.organization_id(m), --Bug 3515520
17859                                     NULL, --Bug 3515520
17860                                     NULL, -- to loc
17861                                     p_instance_id,
17862                                     l_atp_output_table.customer_id(m),
17863                                     l_atp_output_table.customer_site_id(m),
17864                                     NULL, -- supplier_id
17865                                     NULL, -- supplier_site_id
17866                                     MSC_ATP_PVT.G_SESSION_ID,
17867                                     l_atp_output_table.customer_site_id(m),
17868                                     l_ship_method,
17869                                     l_delivery_lead_time
17870                                     );
17871                    END IF;
17872 
17873                    l_atp_output_table.delivery_lead_time(m) := l_delivery_lead_time; --4430710
17874 		   -- dsting removed old delivery lead time code
17875                 END IF; -- end delivery lead time not specified
17876 
17877                 IF PG_DEBUG in ('Y', 'C') THEN
17878                    msc_sch_wb.atp_debug('Call_Schedule: ' || 'Lead Time '||l_atp_output_table.delivery_lead_time(m)); ----4430710
17879                 END IF;
17880 
17881                 -- Bug 3449812 - get receiving cal from ISO's destination org if available
17882                 -- Bug 3515520, don't use org in case customer/site is populated.
17883                 -- IF NVL(l_atp_output_table.internal_org_id(m),l_atp_output_table.organization_id(m)) IS NOT NULL THEN
17884 
17885                 -- Bug 3737759 - Moved from below
17886                 l_manufacturing_cal_code := MSC_CALENDAR.Get_Calendar_Code(
17887                                              p_instance_id,
17888                                              null,
17889                                              null,
17890                                              null,
17891                                              null,
17892                                              null,
17893                                              l_atp_output_table.source_organization_id(m),
17894                                              null,
17895                                              MSC_CALENDAR.OMC);
17896 
17897                 -- Bug 3593394
17898                 IF MSC_ATP_PVT.G_USE_SHIP_REC_CAL='Y' THEN
17899 
17900                     IF (l_atp_output_table.internal_org_id(m) IS NOT NULL) OR
17901                        (l_atp_output_table.organization_id(m) IS NOT NULL AND
17902                         l_atp_output_table.customer_id(m) IS NULL AND l_atp_output_table.customer_site_id(m) IS NULL) THEN
17903                             l_receiving_cal_code := MSC_CALENDAR.Get_Calendar_Code(
17904                                                              p_instance_id,
17905                                                              null,
17906                                                              null,
17907                                                              null,
17908                                                              null,
17909                                                              null,
17910                                                              NVL(l_atp_output_table.internal_org_id(m),l_atp_output_table.organization_id(m)),
17911                                                              l_ship_method,
17912                                                              -- Bug 3593394 - l_atp_output_table.ship_method(m),
17913                                                              MSC_CALENDAR.ORC);
17914                     ELSIF l_atp_output_table.customer_id(m)  IS NOT NULL THEN
17915                             -- receiving party is customer
17916                             l_receiving_cal_code := MSC_CALENDAR.Get_Calendar_Code(
17917                                                              p_instance_id,
17918                                                              null,
17919                                                              null,
17920                                                              MSC_ATP_PVT.G_PARTNER_ID,
17921                                                              MSC_ATP_PVT.G_PARTNER_SITE_ID,
17922                                                              2,
17923                                                              null,
17924                                                              l_ship_method,
17925                                                              -- Bug 3593394 - l_atp_output_table.ship_method(m),
17926                                                              MSC_CALENDAR.CRC);
17927                     ELSE
17928                             -- neither customer nor org is provided
17929                             l_receiving_cal_code := MSC_CALENDAR.FOC;
17930                     END IF;
17931 
17932                     l_intransit_cal_code := MSC_CALENDAR.Get_Calendar_Code(
17933                                              p_instance_id,
17934                                              null,
17935                                              null,
17936                                              null,
17937                                              null,
17938                                              4,
17939                                              null,
17940                                              l_ship_method,
17941                                              -- Bug 3593394 - l_atp_output_table.ship_method(m),
17942                                              MSC_CALENDAR.VIC);
17943 
17944                     l_shipping_cal_code := MSC_CALENDAR.Get_Calendar_Code(
17945                                              p_instance_id,
17946                                              null,
17947                                              null,
17948                                              null,
17949                                              null,
17950                                              null,
17951                                              l_atp_output_table.source_organization_id(m),
17952                                              l_ship_method,
17953                                              -- Bug 3593394 - l_atp_output_table.ship_method(m),
17954                                              MSC_CALENDAR.OSC);
17955 
17956                 ELSE
17957 
17958                     -- Bug 3593394
17959                     IF PG_DEBUG in ('Y', 'C') THEN
17960                         msc_sch_wb.atp_debug('Call_Schedule: ' || 'G_USE_SHIP_REC_CAL is N');
17961                     END IF;
17962                     -- Bug 3737759 - Use receiving org's OMC
17963                     IF (l_atp_output_table.internal_org_id(m) IS NOT NULL) OR
17964                        (l_atp_output_table.organization_id(m) IS NOT NULL AND
17965                         l_atp_output_table.customer_id(m) IS NULL AND l_atp_output_table.customer_site_id(m) IS NULL) THEN
17966                             l_receiving_cal_code := MSC_CALENDAR.Get_Calendar_Code(
17967                                                              p_instance_id,
17968                                                              null,
17969                                                              null,
17970                                                              null,
17971                                                              null,
17972                                                              null,
17973                                                              NVL(l_atp_output_table.internal_org_id(m),l_atp_output_table.organization_id(m)),
17974                                                              null,
17975                                                              MSC_CALENDAR.OMC);
17976                     ELSE
17977                         l_receiving_cal_code := MSC_CALENDAR.FOC;
17978                     END IF;
17979                     l_intransit_cal_code := MSC_CALENDAR.FOC;
17980                     -- Bug 3737759 - Use source org's OMC - l_shipping_cal_code := MSC_CALENDAR.FOC;
17981                     l_shipping_cal_code := l_manufacturing_cal_code;
17982 
17983                 END IF;
17984 
17985                 /* Bug 3737759 - Moved before IF
17986                 l_manufacturing_cal_code := MSC_CALENDAR.Get_Calendar_Code(
17987                                              p_instance_id,
17988                                              null,
17989                                              null,
17990                                              null,
17991                                              null,
17992                                              null,
17993                                              l_atp_output_table.source_organization_id(m),
17994                                              null,
17995                                              MSC_CALENDAR.OMC);
17996                 */
17997 
17998                 -- Bug 3449812 - Corrected the logic of date calculation
17999                 IF l_atp_output_table.requested_ship_date(m) IS NOT NULL THEN
18000                         l_atp_output_table.ship_date(m) := MSC_CALENDAR.PREV_WORK_DAY(
18001         						l_shipping_cal_code,
18002         						p_instance_id,
18003         						l_atp_output_table.requested_ship_date(m));
18004                 ELSE
18005                         l_atp_output_table.ship_date(m) := MSC_CALENDAR.THREE_STEP_CAL_OFFSET_DATE(
18006                         				l_atp_output_table.requested_arrival_date(m), l_receiving_cal_code, -1,
18007                         				l_intransit_cal_code, -1 * nvl(l_atp_output_table.delivery_lead_time(m), 0), -1,
18008                         				l_shipping_cal_code, -1, p_instance_id); --4430710
18009                 END IF;
18010 
18011                 IF PG_DEBUG in ('Y', 'C') THEN
18012                    msc_sch_wb.atp_debug('Call_Schedule: ' || 'l_atp_output_table.ship_date ' || l_atp_output_table.ship_date(m) );
18013                 END IF;
18014 
18015                 BEGIN  -- Bug2031894. Handle Exception here.
18016 
18017                     l_sysdate := MSC_CALENDAR.NEXT_WORK_DAY(
18018                                         l_shipping_cal_code,    -- Bug 3449812
18019                                         p_instance_id,
18020                                         sysdate);
18021                 EXCEPTION
18022                     WHEN OTHERS THEN
18023                         IF PG_DEBUG in ('Y', 'C') THEN
18024                            msc_sch_wb.atp_debug('Call_Schedule: ' || 'When Others ');
18025                         END IF;
18026                         NULL;
18027                 END;
18028                 IF PG_DEBUG in ('Y', 'C') THEN
18029                    msc_sch_wb.atp_debug('Call_Schedule: ' || 'l_sysdate '||l_sysdate);
18030                 END IF;
18031                 IF l_sysdate IS NULL THEN
18032                    IF PG_DEBUG in ('Y', 'C') THEN
18033                       msc_sch_wb.atp_debug('Call_Schedule: ' || 'Sysdate not found in Calendar');
18034                    END IF;
18035                    l_atp_output_table.error_code(m) := NO_MATCHING_CAL_DATE;
18036                 END IF;
18037 
18038                 IF l_atp_output_table.ship_date(m) < l_sysdate THEN
18039                         l_atp_output_table.requested_date_quantity(m) := 0;
18040                 ELSE
18041                         l_atp_output_table.requested_date_quantity(m) :=
18042                         l_atp_output_table.quantity_ordered(m);
18043                 END IF;
18044                 /* Bug 3449812 - l_prev_work_ship_date not required as ship date has already been validated
18045                 l_prev_work_ship_date := MSC_ATP_FUNC.prev_work_day
18046                                     (x_atp_table.source_organization_id(m),
18047                                     p_instance_id,
18048                                      x_atp_table.ship_date(m));
18049 
18050                 IF l_prev_work_ship_date IS NULL THEN
18051                    IF PG_DEBUG in ('Y', 'C') THEN
18052                       msc_sch_wb.atp_debug('Call_Schedule: ' || 'prev date for ship date not found in Calendar');
18053                       msc_sch_wb.atp_debug('Call_Schedule: ' || 'x_atp_table.ship_date ' || x_atp_table.ship_date(m) );
18054                    END IF;
18055                    x_atp_table.error_code(m) := MSC_ATP_PVT.NO_MATCHING_CAL_DATE;
18056                 END IF;
18057                 */
18058 
18059                 IF PG_DEBUG in ('Y', 'C') THEN
18060                    msc_sch_wb.atp_debug('Call_Schedule: ' || 'l_atp_output_table.requested_ship_date(m) : ' ||
18061                         to_char(l_atp_output_table.requested_ship_date(m), 'dd/mm/yyyy hh:mi:ss'));
18062                    msc_sch_wb.atp_debug('Call_Schedule: ' || 'l_atp_output_table.ship_date(m) : ' ||
18063                         to_char(l_atp_output_table.ship_date(m), 'dd/mm/yyyy hh:mi:ss'));
18064                    msc_sch_wb.atp_debug('Call_Schedule: ' || 'l_atp_output_table.requested_arrival_date(m): ' ||
18065                         to_char(l_atp_output_table.requested_arrival_date(m),'dd/mm/yyyy hh:mi:ss'));
18066                 END IF;
18067                 -- Bug  Bug 2232555, 2250456
18068 
18069 		/* Old commented code removed*/
18070 
18071                 -- ship_rec_cal changes begin
18072                 -- Bug 3449812 - Code to get calendars moved at the beginning of while loop
18073 
18074                 IF (NVL(l_atp_output_table.Override_Flag(m), 'N') = 'Y') THEN
18075                    IF (l_atp_output_table.requested_ship_date(m) IS NOT NULL) THEN
18076                       l_atp_output_table.ship_date(m) := l_atp_output_table.requested_ship_date(m);
18077 
18078                       -- Bug 3520301 - dont consider first calendar in override cases
18079                       l_atp_output_table.arrival_date(m) := MSC_CALENDAR.THREE_STEP_CAL_OFFSET_DATE(
18080                                                         l_atp_output_table.ship_date(m), MSC_CALENDAR.FOC, 1,
18081                                                         l_intransit_cal_code, nvl(l_atp_output_table.delivery_lead_time(m), 0), 1,
18082                                                         l_receiving_cal_code, 1, p_instance_id);
18083 
18084                    ELSE
18085                       l_atp_output_table.arrival_date(m) := l_atp_output_table.requested_arrival_date(m);
18086 
18087                       -- Bug 3520301 - ship date needs to be recalculated because first calendar
18088                       --               is not to be considered in override cases
18089                       l_atp_output_table.ship_date(m) := MSC_CALENDAR.THREE_STEP_CAL_OFFSET_DATE(
18090                 					l_atp_output_table.requested_arrival_date(m), MSC_CALENDAR.FOC, -1,
18091                 					l_intransit_cal_code, -1 * nvl(l_atp_output_table.delivery_lead_time(m), 0), -1,
18092                 					l_shipping_cal_code, -1, p_instance_id);
18093 
18094                    END IF;
18095 
18096                 ELSE
18097                     -- Bug 3449812 - Added the non-override case, which, it seems, got removed while ship_rec changes were done
18098                     l_atp_output_table.ship_date(m) := GREATEST(l_sysdate, l_atp_output_table.ship_date(m));
18099 
18100                 END IF;
18101                 IF (l_atp_output_table.ship_date(m) IS NOT NULL)  AND (l_atp_output_table.arrival_date(m) IS NULL) THEN
18102                     l_atp_output_table.arrival_date(m) := MSC_CALENDAR.THREE_STEP_CAL_OFFSET_DATE(
18103                                                         l_atp_output_table.ship_date(m), l_shipping_cal_code, 1,
18104                                                         l_intransit_cal_code, nvl(l_atp_output_table.delivery_lead_time(m), 0), 1,
18105                                                         l_receiving_cal_code, 1, p_instance_id);
18106 
18107                     /* Bug 3449812 - Arrival date should not be pushed back to non-working day
18108                     IF x_atp_table.requested_arrival_date(m) is not null THEN
18109                        x_atp_table.arrival_date(m) := TRUNC(GREATEST(x_atp_table.requested_arrival_date(m),
18110                                                         x_atp_table.arrival_date(m)));
18111                     END IF;
18112                     */
18113                 END IF;
18114                 -- ship_rec_cal changes end
18115 
18116                 l_atp_output_table.group_ship_date(m) := l_atp_output_table.ship_date(m);
18117                 l_atp_output_table.group_arrival_date(m) := l_atp_output_table.arrival_date(m);
18118                 l_atp_output_table.ship_method(m) := l_ship_method;
18119                 l_atp_output_table.delivery_lead_time(m) := l_delivery_lead_time;
18120                 IF PG_DEBUG in ('Y', 'C') THEN
18121                    msc_sch_wb.atp_debug('Call_Schedule: ' || 'group ship date '|| l_atp_output_table.group_ship_date(m));
18122                    msc_sch_wb.atp_debug('Call_Schedule: ' || 'group arrival date '|| l_atp_output_table.group_arrival_date(m));
18123                    msc_sch_wb.atp_debug('Call_Schedule: ' || 'l_atp_output_table.requested_ship_date(m) after : ' ||
18124                         to_char(l_atp_output_table.requested_ship_date(m), 'dd/mm/yyyy hh:mi:ss'));
18125                    msc_sch_wb.atp_debug('Call_Schedule: ' || 'Here ISO BUG x_atp_table count at end of loop' || x_atp_table.action.count);
18126                    msc_sch_wb.atp_debug('Call_Schedule: ' || 'Here ISO BUG l_atp_input_table count at end of loop' || l_atp_input_table.action.count);
18127 
18128 		END IF;
18129 
18130                 -- End Changes
18131 
18132                 l_atp_output_table.Available_quantity(m) := l_atp_input_table.Quantity_Ordered(m);
18133                 --l_atp_output_table.requested_date_quantity(m) := l_atp_input_table.Quantity_Ordered(m);
18134                 l_atp_output_table.Error_Code(m) := 0;
18135 
18136 		-- bug 5898303
18137 		MSC_SATP_FUNC.Assign_Atp_Output_Rec(l_atp_output_table,x_atp_table,l_return_status );
18138 
18139                 m := l_atp_input_table.Action.NEXT(m);
18140                 -- End Bug 2232555, 2250456
18141             END LOOP;
18142           EXCEPTION --bug3583705 start
18143 
18144             WHEN NO_MATCHING_DATE_IN_CAL THEN
18145               FOR i in l_atp_input_table.Action.FIRST ..l_atp_input_table.Action.LAST	 LOOP
18146                 IF (x_atp_table.Error_Code(i) IS NULL) or (x_atp_table.Error_Code(i) IN (0,61,150)) THEN
18147                  x_atp_table.Error_Code(i) := NO_MATCHING_CAL_DATE;
18148                 END IF;
18149               END LOOP;
18150 
18151             WHEN OTHERS THEN
18152               x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
18153               /* Check if this is actually coming from a calendar routine*/
18154               l_encoded_text := fnd_message.GET_ENCODED;
18155               IF l_encoded_text IS NULL THEN
18156                 l_msg_app := NULL;
18157                 l_msg_name := NULL;
18158               ELSE
18159                 fnd_message.parse_encoded(l_encoded_text, l_msg_app, l_msg_name);
18160               END IF;
18161 
18162               -- Error Handling Changes
18163               FOR i in l_atp_input_table.Action.FIRST ..l_atp_input_table.Action.LAST LOOP
18164                IF (x_atp_table.Error_Code(i) IS NULL) or (x_atp_table.Error_Code(i) IN (0,61,150)) THEN
18165                 IF l_msg_app='MRP' AND l_msg_name='GEN-DATE OUT OF BOUNDS' THEN
18166                         x_atp_table.Error_Code(i) := MSC_ATP_PVT.NO_MATCHING_CAL_DATE;
18167                         IF PG_DEBUG in ('Y', 'C') THEN
18168                            msc_sch_wb.atp_debug('Call_Schedule: NO_MATCHING_CAL_DATE');
18169                         END IF;
18170                 ELSE
18171                         x_atp_table.Error_Code(i) := MSC_ATP_PVT.ATP_PROCESSING_ERROR; -- ATP Processing Error
18172                 END IF;
18173                END IF;
18174               END LOOP;
18175               IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
18176                  FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME , 'MSC_ATP_PVT');
18177               END IF;
18178               IF PG_DEBUG in ('Y', 'C') THEN
18179                  msc_sch_wb.atp_debug('Call_Schedule: ' || 'error := ' || sqlerrm);
18180               END IF;
18181           END;
18182           --bug3583705 end
18183         /* END Bug 2290633 : krajan : 04/05/02 */
18184 
18185             IF PG_DEBUG in ('Y', 'C') THEN
18186                msc_sch_wb.atp_debug('Call_Schedule: ' || 'After Assigning values for l_atp_output_table');
18187                msc_sch_wb.atp_debug('Call_Schedule: ' || 'Here ISO BUG x_atp_table count after loop' || x_atp_table.action.count);
18188 	    END IF;
18189 
18190          ELSE 	-- IF l_atp_input_table.OE_flag(m) = 'Y' AND MSC_ATP_PVT.G_INV_CTP = 4 THEN
18191             -- now call schedule to schedule this line/set.
18192             -- the reason that we do it this way is that when source org not
18193             -- specified, for each line/set, we may return multiple results
18194             -- to users when we cannot satisfy the request on the preferred
18195             -- warehouse and the action is ATP inquiry.
18196 
18197             IF PG_DEBUG in ('Y', 'C') THEN
18198                msc_sch_wb.atp_debug('Call_Schedule, before Schedule ');
18199             END IF;
18200             /* --s_cto_rearch
18201             -- rajjain 02/03/2003 Bug 2766713 Begin
18202             k := l_atp_input_table.error_code.First;
18203             FOR k in 1.. l_atp_input_table.error_code.COUNT LOOP
18204                 IF l_atp_input_table.error_code(k) = INVALID_ITEM_ORG_COMBINATION THEN
18205                    msc_sch_wb.atp_debug ('Call_Schedule: '||'Invalid Item Org Combination : '||k);
18206                    l_set_fail_flag := 'Y';
18207                    EXIT;
18208                 END IF;
18209             END LOOP;
18210             e_cto_rearch*/
18211 
18212             IF l_set_fail_flag = 'Y' THEN
18213                l_atp_output_table := l_atp_input_table;
18214                /* --s_cto_rearch
18215                k := l_atp_input_table.error_code.First;
18216                IF PG_DEBUG in ('Y','C') THEN
18217                    msc_sch_wb.atp_debug ('Call_Schedule: '
18218                      || 'This set failed as this has atleast one item with invalid item-org combination');
18219                END IF;
18220                FOR k in 1.. l_atp_input_table.error_code.COUNT LOOP
18221                    IF nvl(l_atp_input_table.error_code(k), 0) <> INVALID_ITEM_ORG_COMBINATION THEN
18222                       l_atp_output_table.error_code(k) := GROUPEL_ERROR;
18223                    END IF;
18224                END LOOP;
18225                e_cto_rearch */
18226                -- Reset l_set_fail_flag
18227                l_set_fail_flag := 'N';
18228             ELSE
18229                -- 24x7 Call to schedule. What do we call ?
18230                if (NVL(MSC_ATP_PVT.G_SYNC_ATP_CHECK, 'N') = 'Y' ) then
18231                    IF PG_DEBUG in ('Y','C') THEN
18232                        msc_sch_wb.atp_debug ('Calling Schedule with 24x7 related parameters');
18233                    END IF;
18234 
18235                    MSC_ATP_PVT.Schedule(l_atp_input_table,
18236                        l_atp_input_table.instance_id(l_atp_input_table.instance_id.FIRST),
18237                        p_assign_set_id,
18238                        l_atp_input_table.attribute_04(l_atp_input_table.attribute_04.FIRST),
18239                        l_atp_output_table,
18240                        l_return_status,
18241                        l_msg_data,
18242                        l_msg_count,
18243                        l_atp_supply_demand,
18244                        l_atp_period,
18245                        l_atp_details);
18246                  else
18247                    MSC_ATP_PVT.Schedule(l_atp_input_table,
18248                      p_instance_id,
18249                      p_assign_set_id,
18250                      p_refresh_number,
18251                      l_atp_output_table,
18252                      l_return_status,
18253                      l_msg_data,
18254                      l_msg_count,
18255                      l_atp_supply_demand,
18256                      l_atp_period,
18257                      l_atp_details);
18258                end if;
18259 
18260                -- Bug 1531469
18261                -- Check if the Scheduling was successful. In case no, set the error code
18262                -- for all rows where its either success or atp_not_appl or null.
18263 
18264 
18265                IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
18266 
18267                   IF PG_DEBUG in ('Y', 'C') THEN
18268                      msc_sch_wb.atp_debug('Call_Schedule: ' || 'inside return status error');
18269                   END IF;
18270                   x_return_status := FND_API.G_RET_STS_ERROR;
18271                   k := l_atp_output_table.Calling_Module.First;
18272                   FOR k in 1.. l_atp_output_table.Calling_Module.COUNT LOOP
18273                       --l_atp_output_table.Group_Ship_Date(k):= l_group_date;
18274                       IF (l_atp_output_table.Error_Code(k) = ALLSUCCESS) OR
18275                          (l_atp_output_table.error_code(k) = ATP_NOT_APPL) OR
18276                          (l_atp_output_table.Error_Code(k) IS NULL) THEN
18277                          l_atp_output_table.Error_Code(k) := GROUPEL_ERROR;
18278                       END IF;
18279                   END LOOP;
18280                   IF PG_DEBUG in ('Y', 'C') THEN
18281                      msc_sch_wb.atp_debug('Call_Schedule: ' || 'after setting group error');
18282                   END IF;
18283                END IF;
18284             END IF; --IF l_set_fail_flag = 'Y' THEN
18285             -- rajjain 02/03/2003 Bug 2766713 End
18286             --4500382 Starts
18287             k := l_atp_output_table.action.FIRST;
18288             IF l_return_status = FND_API.G_RET_STS_SUCCESS  and l_atp_output_table.action(k) <> 100
18289                  and  p_atp_table.part_of_set(k) = 'Y' and
18290                (l_atp_output_table.Error_Code(k) = ALLSUCCESS OR
18291                     l_atp_output_table.error_code(k) = ATP_NOT_APPL OR
18292                     l_atp_output_table.Error_Code(k) is NULL) THEN
18293 
18294                     FOR k in 1.. l_atp_output_table.Calling_Module.COUNT
18295                     LOOP
18296                         IF PG_DEBUG in ('Y', 'C') THEN
18297                            msc_sch_wb.atp_debug('requested ship date = '||
18298                                                 to_char( p_atp_table.requested_ship_date(k), 'DD-MON-YYYY HH24:MI:SS'));
18299                            msc_sch_wb.atp_debug('requested arrival date = '
18300                                                 ||to_char( p_atp_table.requested_arrival_date(k), 'DD-MON-YYYY HH24:MI:SS'));
18301                            msc_sch_wb.atp_debug('group ship date = '
18302                                                 ||to_char( l_atp_output_table.group_Ship_Date(k), 'DD-MON-YYYY HH24:MI:SS'));
18303                            msc_sch_wb.atp_debug('group arrival date = '
18304                                                 ||to_char( l_atp_output_table.arrival_Date(k), 'DD-MON-YYYY HH24:MI:SS'));
18305                            msc_sch_wb.atp_debug('arrival date  = '
18306                                                 ||to_char(l_atp_output_table.arrival_Date(k), 'DD-MON-YYYY HH24:MI:SS'));
18307                            msc_sch_wb.atp_debug('ship date  = '
18308                                                 ||to_char(l_atp_output_table.ship_Date(k), 'DD-MON-YYYY HH24:MI:SS'));
18309                         END IF;
18310                         if((( p_atp_table.requested_ship_date(k)is not null and
18311                             (p_atp_table.requested_ship_date(k) <>
18312                              greatest(nvl( l_atp_output_table.group_Ship_Date(k),
18313                                            l_atp_output_table.Ship_Date(k)),
18314                                       l_atp_output_table.Ship_Date(k )))) or
18315                                          ( p_atp_table.requested_arrival_date(k)is not null and
18316                             ( p_atp_table.requested_arrival_date(k) <>
18317                               greatest(nvl( l_atp_output_table.group_arrival_Date(k),
18318                                             l_atp_output_table.arrival_Date(k)),
18319                                        l_atp_output_table.arrival_Date(k)))))) THEN
18320 
18321                            l_atp_output_table.Error_Code(k) := ATP_ACCEPT_FAIL;
18322 
18323                            MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(l_atp_output_table.end_pegging_id(k),
18324                                                                   l_atp_output_table.instance_id(k),
18325                                                                   l_atp_output_table.plan_id(k),
18326                                                                   MSC_ATP_PVT.UNDO,
18327                                                                   2,--p_dc_flag
18328                                                                   l_return_status);
18329                         END IF;
18330                      END LOOP;
18331                   END IF;
18332                --4500382 ENDS
18333             -- now get the results/details and append it to the 'real' output
18334             MSC_SATP_FUNC.Assign_Atp_Output_Rec(l_atp_output_table,x_atp_table,l_return_status );
18335 
18336             MSC_ATP_PROC.Details_Output(l_atp_period,
18337                                         l_atp_supply_demand,
18338                                         x_atp_period,
18339                                         x_atp_supply_demand,
18340                                         l_return_status);
18341 
18342          END IF; 	--IF l_atp_input_table.OE_flag(m) = 'Y' THEN
18343       END IF;
18344 
18345       -- this is for backlog: we need to send a message back.
18346       IF PG_DEBUG in ('Y', 'C') THEN
18347          msc_sch_wb.atp_debug('Call_Schedule: ' || 'before backlog');
18348 	 msc_sch_wb.atp_debug('Call_Schedule: ' || 'Here ISO BUG x_atp_table count at end of loop: ' || x_atp_table.action.count);
18349       END IF;
18350 
18351       k := l_atp_output_table.Calling_Module.First;
18352       IF k IS NOT NULL THEN
18353 
18354         IF NVL(l_atp_output_table.Calling_Module(k), 0) = -1 AND l_atp_progess_bar = 'Y' THEN -- backlog 7419485
18355 
18356           IF PG_DEBUG in ('Y', 'C') THEN
18357              msc_sch_wb.atp_debug('Call_Schedule: ' || 'for backlog');
18358           END IF;
18359           --Bug3765793 adding trim functions to remove spaces from db_link
18360     	  SELECT ltrim(rtrim(M2A_DBLINK))
18361     	  INTO   l_dblink
18362     	  FROM   msc_apps_instances
18363     	  WHERE  instance_id = p_instance_id;
18364           IF PG_DEBUG in ('Y', 'C') THEN
18365              msc_sch_wb.atp_debug('Call_Schedule: ' || 'after l_dblink : '||l_dblink);
18366           END IF;
18367 
18368           FOR k in 1..l_atp_output_table.Calling_Module.COUNT LOOP
18369 
18370            IF PG_DEBUG in ('Y', 'C') THEN
18371               msc_sch_wb.atp_debug('Call_Schedule: ' || 'in side for loop for backlog');
18372            END IF;
18373 /*
18374             msc_sch_wb.pipe_utility(p_session_id,
18375                                     'SEND',
18376                                      l_msg_data,
18377                                      l_msg_count,
18378                                      x_return_status,
18379                                      l_msg_data,
18380                                      l_msg_count);
18381 */         --bug3520746 pass MSC_ATP_PVT.G_DATABASE_LINK insead of l_dblink
18382            --as global variable already has the right dblink for RAC
18383            IF l_dblink IS NOT NULL THEN
18384             plsql_block := 'BEGIN msc_sch_wb.pipe_utility'
18385 				||'@'||MSC_ATP_PVT.G_DATABASE_LINK||'(:p_session_id,
18386                                      :p_command,
18387                                      :l_msg_data,
18388                                      :l_msg_number,
18389                                      :x_return_status,
18390                                      :l_msg_text,
18391                                      :l_msg_count); END;';
18392 
18393            IF PG_DEBUG in ('Y', 'C') THEN
18394               msc_sch_wb.atp_debug('Call_Schedule: ' || 'plsql_block : '|| plsql_block);
18395            END IF;
18396 
18397            BEGIN
18398             EXECUTE IMMEDIATE plsql_block USING
18399                                      p_session_id,
18400                                      'SEND',
18401                                      IN OUT l_msg_data,
18402                                      OUT l_msg_number,
18403                                      OUT x_return_status,
18404                                      OUT l_msg_text,
18405                                      OUT l_msg_count;
18406            IF PG_DEBUG in ('Y', 'C') THEN
18407               msc_sch_wb.atp_debug('Call_Schedule: ' || 'after plsql_block');
18408            END IF;
18409            EXCEPTION
18410 		when others then
18411                       IF PG_DEBUG in ('Y', 'C') THEN
18412                          msc_sch_wb.atp_debug('Call_Schedule: ' || 'inside send exception : '||sqlcode);
18413                       END IF;
18414            END;
18415 
18416            ELSE		--IF l_dblink IS NOT NULL THEN
18417             msc_sch_wb.pipe_utility(p_session_id,
18418                                     'SEND',
18419                                      l_msg_data,
18420                                      l_msg_number,
18421                                      x_return_status,
18422                                      l_msg_count,
18423                                      l_msg_count);
18424            END IF;
18425           END LOOP;
18426         END IF;
18427       END IF;
18428 
18429       -- go to the next row.
18430       -- make sure we jump to the right row since we may process more
18431       -- than one row due to ship set or arrival set.
18432 
18433        --3720018, Changes for global Availability
18434        -- Remove_Invalid_SD_Rec is called per source.
18435        -- When we move to the next source G_REMOVE_REQUEST_LEVEL_REC array is NULLed
18436        -- Undo_delete_row is not called here but at request level.
18437       IF (j is NULL) THEN
18438          l := i;
18439       ELSE
18440          l := j;
18441       END IF;
18442 
18443 
18444       IF ( l_global_availability = 'Y' )
18445 	  AND (p_atp_table.source_organization_id(i) <> p_atp_table.source_organization_id(l)) THEN
18446 
18447 	  IF PG_DEBUG in ('Y', 'C') THEN
18448               msc_sch_wb.atp_debug('Call_Schedule: ' || 'Inside global availability check and calling remove_invalid_sd_rec');
18449           END IF;
18450           IF ( x_atp_table.Action(1) = ATPQUERY ) THEN
18451 	    IF ( MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_PEGGING_ID_REQUEST IS NOT NULL AND MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_PEGGING_ID_REQUEST.Count > 0) THEN
18452 		    FOR m in 1..MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_PEGGING_ID_REQUEST.COUNT LOOP
18453 		        MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(
18454   			            MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_PEGGING_ID_REQUEST(m),
18455                                     p_instance_id,
18456                                     MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_PLAN_ID_REQUEST (m),
18457                                     UNDO,
18458                                     MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_DC_ATP_FLAG_REQUEST(m),
18459   			            l_return_status); --3720018
18460 	            END LOOP;
18461 	    END IF;
18462           END IF;
18463 
18464 	  --3720018, Initialize global variables to NULL.
18465           MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC := l_null_remove_req_level_rec;
18466 
18467       END IF;
18468 
18469       i := j;
18470 
18471     END LOOP;
18472 
18473     IF PG_DEBUG in ('Y', 'C') THEN
18474          msc_sch_wb.atp_debug(x_atp_table.Action(1));
18475          msc_sch_wb.atp_debug(x_atp_table.calling_module(1));
18476     END IF;
18477 
18478     --3720018, Call to Remove_invalid_SD_rec and Undo_delete_row for the whole_request in case of ATP Inquiry
18479     IF ( x_atp_table.Action(1) = ATPQUERY ) THEN
18480       IF PG_DEBUG in ('Y', 'C') THEN
18481          msc_sch_wb.atp_debug('Call_Schedule: ' || 'Outside global availability check and calling remove_invalid_sdrec');
18482          msc_sch_wb.atp_debug('Call_Schedule: ' || 'Here ISO BUG x_atp_table count at end of loop: ' || x_atp_table.action.count);
18483       END IF;
18484 
18485       IF ( MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_PEGGING_ID_REQUEST IS NOT NULL AND MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_PEGGING_ID_REQUEST.Count > 0) THEN
18486         FOR m in 1..MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_PEGGING_ID_REQUEST.COUNT LOOP
18487 	    MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(
18488   	            MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_PEGGING_ID_REQUEST(m)  ,
18489                     p_instance_id,
18490                     MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_PLAN_ID_REQUEST(m),
18491                     UNDO,
18492                     MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_DC_ATP_FLAG_REQUEST(m),
18493   	            l_return_status);
18494 	END LOOP;
18495       END IF;
18496       IF  (NVL(p_atp_table.calling_module(1), -99) <> 724) OR
18497           ((NVL(p_atp_table.calling_module(1), -99) = -99) AND
18498           (l_global_availability = 'Y')) THEN --3720018
18499             IF (l_delete_atp_rec.del_demand_ids IS NOT NULL AND l_delete_atp_rec.del_demand_ids.Count > 0) OR
18500                (l_delete_atp_rec.del_ods_demand_ids IS NOT NULL AND l_delete_atp_rec.del_ods_demand_ids.Count > 0)  THEN
18501  		    MSC_ATP_DB_UTILS.Undo_Delete_Row(
18502                     l_delete_atp_rec.del_identifiers,
18503                     l_delete_atp_rec.del_plan_ids,
18504                     p_instance_id,
18505                     l_delete_atp_rec.del_demand_ids,
18506                     l_delete_atp_rec.del_inv_item_ids,
18507                     l_delete_atp_rec.del_copy_demand_ids,
18508                     l_delete_atp_rec.del_copy_demand_plan_ids,
18509                     l_delete_atp_rec.time_phased_set,
18510                     l_delete_atp_rec.del_atp_peg_items,
18511                     l_delete_atp_rec.del_atp_peg_demands,
18512                     l_delete_atp_rec.del_atp_peg_supplies,
18513                     l_delete_atp_rec.del_atp_peg_res_reqs,
18514                     l_delete_atp_rec.del_demand_source_type,
18515                     l_delete_atp_rec.atp_peg_demands_plan_ids,
18516                     l_delete_atp_rec.atp_peg_supplies_plan_ids,
18517                     l_delete_atp_rec.atp_peg_res_reqs_plan_ids,
18518                     l_delete_atp_rec.del_ods_demand_ids,         --3720018, added for support of rescheduling in ODS
18519                     l_delete_atp_rec.del_ods_inv_item_ids,       --3720018, added for support of rescheduling in ODS
18520                     l_delete_atp_rec.del_ods_demand_src_type,    --3720018, added for support of rescheduling in ODS
18521                     l_delete_atp_rec.del_ods_cto_demand_ids,     --3720018, added for support of rescheduling in ODS
18522                     l_delete_atp_rec.del_ods_cto_inv_item_ids,   --3720018, added for support of rescheduling in ODS
18523                     l_delete_atp_rec.del_ods_cto_dem_src_type,   --3720018, added for support of rescheduling in ODS
18524                     l_delete_atp_rec.del_ods_atp_refresh_no,     --3720018, added for support of rescheduling in ODS
18525                     l_delete_atp_rec.del_ods_cto_atp_refresh_no  --3720018, added for support of rescheduling in ODS
18526                     );
18527 	    END IF;
18528       END IF;
18529 
18530       MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC := l_null_remove_req_level_rec;
18531 
18532     END IF;
18533 
18534    --3720018 (end of changes)
18535 
18536     -- Delete records from MSC_REGIONS_TEMP before returning back to calling
18537     -- application so as to clean up the table for another request within
18538     -- same session for Region Level Sourcing Support
18539 
18540     DELETE msc_regions_temp
18541     WHERE session_id = p_session_id;
18542 
18543     IF PG_DEBUG in ('Y', 'C') THEN
18544        msc_sch_wb.atp_debug('Call_Schedule: ' || 'Rows deleted from msc_regions_temp : '||sql%rowcount);
18545     END IF;
18546     --bug3940999 Delete records from msc_atp_src_profile_temp before returning
18547     --back to calling application so as to clean up the table
18548     DELETE msc_atp_src_profile_temp
18549     WHERE session_id = p_session_id;
18550 
18551 
18552 
18553     IF PG_DEBUG in ('Y', 'C') THEN
18554        msc_sch_wb.atp_debug('Call_Schedule: ' || 'Rows deleted from msc_atp_src_profile_temp : '||sql%rowcount);
18555        msc_sch_wb.atp_debug('Call_Schedule: ' || 'Before calling MSC_ATP_PROC.Delete_Copy_Demand');
18556     END IF;
18557 
18558     -- Summary enhancement - Delete the copy demands that were created in this transaction if refresh number has moved.
18559     IF MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y' THEN --bug4291375 Delete_Copy_Demand should be called when summary is ON.
18560        MSC_ATP_DB_UTILS.Delete_Copy_Demand(MSC_ATP_PVT.G_COPY_DEMAND_ID,
18561                                            MSC_ATP_PVT.G_COPY_DEMAND_PLAN_ID,
18562                                            G_TIME_PHASED_SET,
18563                                            l_return_status);
18564     END IF;
18565     MSC_ATP_PVT.G_COPY_DEMAND_ID        := MRP_ATP_PUB.number_arr();
18566     MSC_ATP_PVT.G_COPY_DEMAND_PLAN_ID   := MRP_ATP_PUB.number_arr();
18567 
18568     --Bug 3629191: Insert Offset Record to MRP_ATP_DETAILS_TEMP with record_type = 3
18569     -- and these will be inserted with NULL in all the three pegging_id columns
18570 
18571      IF PG_DEBUG in ('Y', 'C') THEN
18572        msc_sch_wb.atp_debug('Call_Schedule: G_OFF_DEM_TRANS_ID.count ' || G_OFF_DEM_TRANS_ID.count);
18573        for i in 1..G_OFF_DEM_TRANS_ID.count LOOP
18574          msc_sch_wb.atp_debug('Call_Schedule: ' ||G_OFF_INSTANCE_ID(i) ||',' || G_OFF_PLAN_ID(i)||','||G_OFF_DEM_TRANS_ID(i));
18575        END LOOP;
18576     END IF;
18577 
18578     IF (MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y')  THEN
18579     FORALL i in 1..G_OFF_DEM_TRANS_ID.count
18580     INSERT INTO MRP_ATP_DETAILS_TEMP(
18581                                      session_id,
18582                                      identifier1,
18583                                      identifier2,
18584                                      identifier3,
18585                                      record_type,
18586                                      scenario_id,
18587                                      summary_flag
18588                                      )
18589                                VALUES(
18590                                      p_session_id,
18591                                      G_OFF_INSTANCE_ID(i),
18592                                      G_OFF_PLAN_ID(i),
18593                                      G_OFF_DEM_TRANS_ID(i),
18594                                      3,
18595                                      1,
18596                                      'Y'
18597                                      );
18598     END IF;
18599 
18600     /* rajjain 02/27/2003 Bug 2824700 Begin
18601      * Restructured Exception/WF Notification code */
18602     l_wf_profile := NVL(G_ATP_WORKFLOW,
18603 			NVL(fnd_profile.value('MSC_ALLOCATED_ATP_WORKFLOW'), 'N'));
18604     IF PG_DEBUG in ('Y', 'C') THEN
18605        msc_sch_wb.atp_debug('Call_Schedule: ' || 'WF Profile:' || l_wf_profile);
18606     END IF;
18607 
18608     k := x_atp_table.Calling_Module.First;
18609     -- Generate exceptions only if action is not ATP query.
18610     IF x_atp_table.Action(k) <> 100 THEN
18611 
18612        WHILE k IS NOT NULL LOOP
18613                 -- re-initialize these variables
18614                 l_wf_start := NULL;
18615                 l_exception_rec.demand_id := NULL;
18616 
18617                 IF PG_DEBUG in ('Y', 'C') THEN
18618                    msc_sch_wb.atp_debug('Call_Schedule: ' || 'k := '||k);
18619                    msc_sch_wb.atp_debug('Call_Schedule: ' || 'Override_flag : '||x_atp_table.Override_Flag(k));
18620 	        END IF;
18621 
18622                 /* rajjain code specific to workflow generation begin*/
18623                 IF NVL(l_wf_profile, 'N') = 'Y' AND MSC_ATP_PVT.G_INV_CTP = 4 THEN
18624                      -- generate workflow notification only in case of PDS.
18625                      IF PG_DEBUG in ('Y', 'C') THEN
18626                         msc_sch_wb.atp_debug('Call_Schedule: ' || '*** Begin Workflow Notification ***');
18627                         msc_sch_wb.atp_debug('Call_Schedule: ' || 'qty ordered : '||x_atp_table.Quantity_Ordered(k));
18628                         msc_sch_wb.atp_debug('Call_Schedule: ' || 'req_date_qty : '||x_atp_table.Requested_Date_Quantity(k));
18629                      END IF;
18630 
18631                      IF (nvl(x_atp_table.Error_Code(k),-1) = 0) AND (NVL(x_atp_table.Override_Flag(k), 'N') = 'Y') THEN
18632                          BEGIN
18633 						   -- Bug 3444096, use x_atp_table.match_item_id for match cases in CTO
18634                            SELECT MAX(supply_demand_date)
18635                            INTO   l_avail_date
18636                            FROM   mrp_atp_details_temp
18637                            WHERE  session_id = p_session_id
18638                            AND    order_line_id =  x_atp_table.identifier(k)
18639                            AND    parent_pegging_id = end_pegging_id
18640                            AND    supply_demand_type = 2
18641                            AND    record_type = 3
18642                            AND    inventory_item_id = NVL(x_atp_table.match_item_id(k), x_atp_table.inventory_item_id(k));
18643                          EXCEPTION
18644                            WHEN no_data_found THEN
18645                                 l_avail_date := NULL;
18646                            WHEN others THEN
18647                                 IF PG_DEBUG in ('Y', 'C') THEN
18648                                    msc_sch_wb.atp_debug('Call_Schedule: ' || '***** Error getting Available Date for WF *****');
18649                                 END IF;
18650                                 l_avail_date := NULL;
18651                          END;
18652 
18653         		 l_wf_start := 'Y';
18654         	     END IF;	--IF x_atp_table.Override_Flag ... THEN
18655 
18656                      IF PG_DEBUG in ('Y', 'C') THEN
18657                         msc_sch_wb.atp_debug('Call_Schedule: ' || 'l_avail_date : '||l_avail_date);
18658                      END IF;
18659                      IF (nvl(x_atp_table.Error_Code(k),-1) IN (0,53)) AND MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y' THEN
18660 
18661                          IF PG_DEBUG in ('Y', 'C') THEN
18662                            msc_sch_wb.atp_debug('Call_Schedule: ' || 'Inside Workflow for Action : '||
18663                                        x_atp_table.Action(k));
18664                            msc_sch_wb.atp_debug('Call_Schedule: ' || 'Inside Workflow for calling Module : '||
18665                                        x_atp_table.Calling_Module(k));
18666                          END IF;
18667 
18668                          BEGIN
18669 						   -- Bug 3444096, use x_atp_table.match_item_id for match cases in CTO
18670                            SELECT NVL(sum(NVL(allocated_quantity, 0)), 0)
18671                            INTO   l_stealing_qty
18672                            FROM   mrp_atp_details_temp
18673                            WHERE  session_id = p_session_id
18674                            AND    order_line_id =  x_atp_table.identifier(k)
18675                            AND    parent_pegging_id = end_pegging_id
18676                            AND    char1 IS NOT NULL
18677                            AND    record_type = 3
18678                            AND    inventory_item_id = NVL(x_atp_table.match_item_id(k), x_atp_table.inventory_item_id(k));
18679                          EXCEPTION
18680                            WHEN no_data_found THEN
18681                                 l_stealing_qty := 0;
18682                            WHEN others THEN
18683                                 IF PG_DEBUG in ('Y', 'C') THEN
18684                                    msc_sch_wb.atp_debug('Call_Schedule: ' || '***** Error getting stealing qty for WF *****');
18685                                 END IF;
18686                                 l_stealing_qty := -1;
18687                          END;
18688 
18689                          IF PG_DEBUG in ('Y', 'C') THEN
18690                             msc_sch_wb.atp_debug('Call_Schedule: ' || 'l_stealing_qty : '||l_stealing_qty);
18691                          END IF;
18692 
18693         		 l_wf_start := 'Y';
18694         	     END IF;	--IF MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y' THEN
18695 
18696         	     IF NVL(l_wf_start, 'N') = 'Y' THEN
18697 
18698         	         -- Get the demand_id from mrp_atp_details_temp
18699                          BEGIN
18700 						   -- Bug 3444096, use x_atp_table.match_item_id for match cases in CTO
18701                            SELECT p_session_id ||':'||mrp_atp_schedule_temp_s.nextval,
18702                                   DECODE(x_atp_table.Requested_Ship_Date(k),
18703                                              NULL, x_atp_table.Requested_Arrival_Date(k),
18704                                              x_atp_table.Requested_Ship_Date(k)),
18705                                   identifier3,
18706                                   identifier2
18707                            INTO   l_wf_item_key,
18708                                   l_req_ship_date,
18709                                   l_exception_rec.demand_id,
18710                                   l_plan_id
18711                            FROM   mrp_atp_details_temp
18712                            WHERE  session_id = p_session_id
18713                            AND    order_line_id =  x_atp_table.identifier(k)
18714                            AND    inventory_item_id = NVL(x_atp_table.match_item_id(k), x_atp_table.inventory_item_id(k))
18715                            AND    record_type = 3
18716                            AND    parent_pegging_id IS NULL;
18717 
18718                          EXCEPTION
18719                            WHEN no_data_found THEN
18720                                 IF PG_DEBUG in ('Y', 'C') THEN
18721                                    msc_sch_wb.atp_debug('Call_Schedule: ' || 'No Data Found getting item key for WF');
18722                                 END IF;
18723                                 l_wf_item_key := NULL;
18724                            WHEN others THEN
18725                                 IF PG_DEBUG in ('Y', 'C') THEN
18726                                    msc_sch_wb.atp_debug('Call_Schedule: ' || '***** Error getting item key for WF *****');
18727                                 END IF;
18728                                 l_wf_item_key := NULL;
18729                          END;
18730 
18731                          IF PG_DEBUG in ('Y', 'C') THEN
18732                             msc_sch_wb.atp_debug('Call_Schedule: ' || 'l_wf_item_key : '||l_wf_item_key);
18733                             msc_sch_wb.atp_debug('Call_Schedule: ' || 'l_plan_id : '||l_plan_id);
18734                             msc_sch_wb.atp_debug('Call_Schedule: ' || 'l_exception_rec.demand_id : '||l_exception_rec.demand_id);
18735                          END IF;
18736 
18737                          IF ((nvl(x_atp_table.Requested_Date_Quantity(k),0) < nvl(x_atp_table.Quantity_Ordered(k),0)) OR
18738                             (nvl(l_stealing_qty, 0) > 0)) AND (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
18739         		    (l_wf_item_key IS NOT NULL) THEN
18740 
18741                             IF PG_DEBUG in ('Y', 'C') THEN
18742                                msc_sch_wb.atp_debug('Call_Schedule: ' || 'Before Allocated ATP Workflow');
18743                             END IF;
18744                             --bug 2463556: wrap workflow call with begin end so that if any exception
18745                             ---occurs in workflow generation then ATP continues as normal
18746 
18747                             -- Start Work Flow
18748                             BEGIN
18749 							   -- Bug 3444096, use x_atp_table.match_item_id and matched_item_name for match cases in CTO
18750                                msc_wf_alloc_atp.start_mscalloc_wf(
18751                                    l_wf_item_key,  NVL(x_atp_table.match_item_id(k), x_atp_table.inventory_item_id(k)),
18752                                    NVL(x_atp_table.matched_item_name(k), x_atp_table.inventory_item_name(k)), l_plan_id,
18753                                    x_atp_table.source_organization_id(k), x_atp_table.source_organization_code(k),
18754                                    p_instance_id, x_atp_table.demand_class(k),
18755                                    x_atp_table.quantity_ordered(k), l_req_ship_date,
18756                                    x_atp_table.Requested_Date_Quantity(k), x_atp_table.Available_Quantity(k),
18757                                    x_atp_table.ship_date(k), l_stealing_qty, x_atp_table.Customer_Id(k),
18758         			               x_atp_table.Customer_Site_Id(k),x_atp_table.Order_Number(k));
18759                             EXCEPTION
18760                                   WHEN OTHERS THEN
18761                                       IF PG_DEBUG in ('Y', 'C') THEN
18762                                          msc_sch_wb.atp_debug('Call_Schedule: ' || ' Allocated ATP WF notification generation failed');
18763                                       END IF;
18764                             END;
18765 
18766                          END IF;
18767 
18768                          IF ((nvl(x_atp_table.Requested_Date_Quantity(k),0) < nvl(x_atp_table.Quantity_Ordered(k),0)) AND
18769                             (nvl(x_atp_table.Override_Flag(k), 'N') = 'Y')) AND
18770         		    (l_wf_item_key IS NOT NULL) THEN
18771 
18772                             IF PG_DEBUG in ('Y', 'C') THEN
18773                                msc_sch_wb.atp_debug('Call_Schedule: ' || 'Before ATP Override Workflow');
18774                             END IF;
18775                             --bug 2463556: wrap workflow call with begin end so that if any exception
18776                             ---occurs in workflow generation then ATP continues as normal
18777 
18778                             -- Start Work Flow
18779                             BEGIN
18780 							  -- Bug 3444096, use x_atp_table.match_item_id and matched_item_name for match cases in CTO
18781                               msc_wf_alloc_atp.start_mscatp_wf(
18782                                    l_wf_item_key, NVL(x_atp_table.match_item_id(k), x_atp_table.inventory_item_id(k)),
18783                                    NVL(x_atp_table.matched_item_name(k), x_atp_table.inventory_item_name(k)), l_plan_id,
18784                                    x_atp_table.source_organization_id(k), x_atp_table.source_organization_code(k),
18785                                    p_instance_id, x_atp_table.demand_class(k),
18786                                    x_atp_table.quantity_ordered(k), l_req_ship_date,
18787                                    x_atp_table.Requested_Date_Quantity(k), x_atp_table.Available_Quantity(k),
18788                                    l_avail_date, x_atp_table.Customer_Id(k),
18789         			               x_atp_table.Customer_Site_Id(k),x_atp_table.Order_Number(k), x_atp_table.Identifier(k));
18790                             EXCEPTION
18791                                  WHEN OTHERS THEN
18792                                     IF PG_DEBUG in ('Y', 'C') THEN
18793                                        msc_sch_wb.atp_debug('Call_Schedule: ' || 'ATP Override WF Notification Generation Failed');
18794                                     END IF;
18795 
18796                             END;
18797                          END IF;
18798                      END IF;
18799         	END IF;
18800         	/* rajjain code specific to workflow generation end */
18801 
18802                 /* rajjain code specific to ATP Override Exception begin
18803                  * ATP Override rajjain begin 10/11/2002 Generate Exceptions */
18804 	        IF (nvl(x_atp_table.Requested_Date_Quantity(k),0) < nvl(x_atp_table.Quantity_Ordered(k),0))
18805 	            AND (nvl(x_atp_table.Override_Flag(k), 'N') = 'Y')
18806                     -- bug 2795053-reopen (ssurendr): Generate exception only if set is successful
18807 	            AND (nvl(x_atp_table.Error_Code(k),-1) = 0)
18808 	        THEN
18809                   IF PG_DEBUG in ('Y', 'C') THEN
18810                     msc_sch_wb.atp_debug('Call_Schedule: ' || 'Before ATP Override Exception');
18811 	          END IF;
18812 
18813                   -- 24x7
18814                   if (NVL(MSC_ATP_PVT.G_SYNC_ATP_CHECK, 'N') = 'Y' ) then
18815                        if (x_atp_table.instance_id.exists(k)) then
18816                            l_instance_id := NVL (x_atp_table.instance_id(k),
18817                                    x_atp_table.instance_id(x_atp_table.instance_id.FIRST));
18818                        else
18819                            l_instance_id := x_atp_table.instance_id(x_atp_table.instance_id.FIRST);
18820                        end if;
18821                   else
18822                        l_instance_id := p_instance_id;
18823                   end if;
18824                   IF PG_DEBUG in ('Y', 'C') THEN
18825                        msc_sch_wb.atp_debug ('Call_Schedule: ' || 'Using Instance ID : ' || l_instance_id);
18826                   END IF;
18827                   -- End 24x7
18828 
18829                   If l_exception_rec.demand_id is null THEN
18830         	        -- Get the demand_id from mrp_atp_details_temp
18831                         BEGIN
18832 						  -- Bug 3444096, use x_atp_table.match_item_id for match cases in CTO
18833                           SELECT identifier3,
18834                                  identifier2
18835                           INTO   l_exception_rec.demand_id,
18836                                  l_plan_id
18837                           FROM   mrp_atp_details_temp
18838                           WHERE  session_id = p_session_id
18839                           AND    order_line_id =  x_atp_table.identifier(k)
18840                           AND    inventory_item_id = NVL(x_atp_table.match_item_id(k), x_atp_table.inventory_item_id(k))
18841                           AND    record_type = 3
18842                           AND    parent_pegging_id IS NULL;
18843 
18844                         EXCEPTION
18845                           WHEN no_data_found THEN
18846                                IF PG_DEBUG in ('Y', 'C') THEN
18847                                   msc_sch_wb.atp_debug('Call_Schedule: ' || 'Exception: No data found');
18848                                END IF;
18849                                l_exception_rec.demand_id := NULL;
18850                           WHEN others THEN
18851                                IF PG_DEBUG in ('Y', 'C') THEN
18852                                   msc_sch_wb.atp_debug('Call_Schedule: ' || '***** Error getting demand_id for generating exception *****');
18853                                END IF;
18854                                l_exception_rec.demand_id := NULL;
18855                         END;
18856 
18857                         IF PG_DEBUG in ('Y', 'C') THEN
18858                            msc_sch_wb.atp_debug('Call_Schedule: ' || 'l_exception_rec.demand_id : '||l_exception_rec.demand_id);
18859                            msc_sch_wb.atp_debug('Call_Schedule: ' || 'l_plan_id : '||l_plan_id);
18860                         END IF;
18861                   END IF;
18862                   IF (l_plan_id is not null) AND
18863                      (l_exception_rec.demand_id is not null) THEN
18864                     -- bug 2795053-reopen (ssurendr): do not generate exception for non-ATPable items.
18865                     --                                In case of non-ATPable there will be no pegging record.
18866                     -- generate exception only in case of PDS. In case of ODS ATP l_plan_id will be null
18867                     IF PG_DEBUG in ('Y', 'C') THEN
18868                        msc_sch_wb.atp_debug('Call_Schedule: ' || '*** Begin ATP Override Exception ***');
18869                     END IF;
18870 
18871                     l_exception_rec.exception_type := 68; -- Overcommitment of Sales Order
18872 		    l_exception_rec.quantity := -1 * x_atp_table.quantity_ordered(k);
18873 
18874                     /* rajjain 02/06/2003 Bug 2781863
18875                        Selecting inventory_item_id from msc_system_items*/
18876                     BEGIN
18877 					  -- Bug 3444096, use x_atp_table.match_item_id for match cases in CTO
18878                       Select inventory_item_id
18879                       into   l_inventory_item_id
18880                       from   msc_system_items
18881                       where  plan_id = l_plan_id
18882                       and    sr_instance_id = l_instance_id
18883                       and    organization_id = x_atp_table.source_organization_id(k)
18884                       and    sr_inventory_item_id =  NVL(x_atp_table.match_item_id(k), x_atp_table.inventory_item_id(k));
18885                     EXCEPTION
18886                       WHEN OTHERS THEN
18887                           IF PG_DEBUG in ('Y', 'C') THEN
18888                              msc_sch_wb.atp_debug('Call_Schedule: ' || 'Error occured while selecting inventory_item_id : ' || sqlerrm);
18889                           END IF;
18890                           RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
18891                     END;
18892 
18893                     IF PG_DEBUG in ('Y', 'C') THEN
18894 				       msc_sch_wb.atp_debug('Call_Schedule: Inventory Item Id : ' || l_inventory_item_id);
18895                     END IF;
18896 	            l_exception_rec.exception_group := 5;
18897 	            l_exception_rec.plan_id := l_plan_id;
18898 	            l_exception_rec.organization_id := x_atp_table.source_organization_id(k);
18899 	            l_exception_rec.inventory_item_id := l_inventory_item_id;
18900 	            -- 24x7 : Using l_instance_id
18901                     -- l_exception_rec.sr_instance_id := p_instance_id;
18902 	            l_exception_rec.sr_instance_id := l_instance_id;
18903 	            l_exception_rec.order_number := x_atp_table.Order_Number(k);
18904 	            l_exception_rec.customer_id := x_atp_table.Customer_Id(k);
18905 	            l_exception_rec.customer_site_id := x_atp_table.Customer_Site_Id(k);
18906 	            l_exception_rec.demand_satisfy_date := x_atp_table.Ship_Date(k); -- bug 2795053-reopen (ssurendr)
18907 	            -- demand_satisfy_date is a misnomer. This actually contains demand date.
18908 	            l_exception_rec.quantity_satisfied := x_atp_table.Requested_Date_Quantity(k);
18909 
18910                     MSC_ATP_EXCEPTIONS.Add_ATP_Exception(p_session_id, l_exception_rec, l_exception_return_status);
18911                     IF PG_DEBUG in ('Y', 'C') THEN
18912                        msc_sch_wb.atp_debug ('Call_Schedule: ' || 'Return Status After adding exception : '||
18913                                           l_exception_return_status);
18914                     END IF;
18915                   END IF;
18916                   msc_sch_wb.atp_debug('Call_Schedule: ' || '*** End ATP Override Exception ***');
18917                 END IF;
18918                 /* rajjain code specific to ATP Override Exception end*/
18919 
18920 	        k := x_atp_table.Calling_Module.Next(k);
18921        END LOOP;
18922     END IF;
18923     /* rajjain 02/27/2003 Bug 2824700 End*/
18924 
18925     IF PG_DEBUG in ('Y', 'C') THEN
18926        msc_sch_wb.atp_debug('***** End Call_Schedule Procedure *****');
18927     END IF;
18928 
18929 END Call_Schedule;
18930 
18931 
18932 PROCEDURE Call_Schedule_Remote (
18933                p_session_id         IN    NUMBER,
18934                p_instance_id        IN    NUMBER,
18935                p_assign_set_id      IN    NUMBER,
18936                p_refresh_number     IN    NUMBER,
18937 --               p_inv_ctp            IN    NUMBER := -1,                      --bug3940999 getting from table
18938                p_def_assign_set_id  IN    NUMBER := -1,
18939                p_atp_debug_flag     IN    VARCHAR2 := NULL,
18940 --               p_atp_workflow       IN    VARCHAR2 := NULL,                  --bug3940999 getting from table
18941                --ATP Debug Workflow
18942                x_session_loc_des    OUT NOCOPY  VARCHAR2,
18943                x_spid_des           OUT NOCOPY  NUMBER,
18944                x_trace_loc_des      OUT NOCOPY  VARCHAR2,
18945                p_node_id            IN    NUMBER DEFAULT null --bug3520746
18946 --               p_use_ship_rec       IN    VARCHAR2 DEFAULT 'N' --bug3593394  --bug3940999 getting from table
18947 ) IS
18948 
18949     i                      PLS_INTEGER;
18950     l_dblink               VARCHAR2(128);
18951     p_atp_table            MRP_ATP_PUB.ATP_Rec_Typ;
18952     x_atp_table            MRP_ATP_PUB.ATP_Rec_Typ;
18953     x_return_status        VARCHAR2(128);
18954     x_msg_data             VARCHAR2(2000);
18955     x_msg_count            NUMBER;
18956     x_atp_supply_demand    MRP_ATP_PUB.ATP_Supply_Demand_Typ;
18957     x_atp_period           MRP_ATP_PUB.ATP_Period_Typ;
18958     x_atp_details          MRP_ATP_PUB.ATP_Details_Typ;
18959     l_atp_bom_rec          MRP_ATP_PUB.ATP_BOM_Rec_Typ;
18960 
18961     -- Bug 2387242 : krajan
18962     -- Variables for SQL trace setup
18963     C                      INTEGER;
18964     STATEMENT              VARCHAR2(255);
18965     ROWS_PROCESSED         INTEGER;
18966     l_summary_flag         PLS_INTEGER;
18967     -- rajjain 12/20/2002
18968     l_spid                          VARCHAR2(12);
18969 
18970     --bug3940999
18971     l_profile_value        MRP_ATP_PUB.char255_arr := MRP_ATP_PUB.char255_arr();   --bug3940999
18972     l_profile_name         MRP_ATP_PUB.char255_arr := MRP_ATP_PUB.char255_arr();   --bug3940999
18973     l_use_ship_rec         VARCHAR2(2);                                          --bug3940999
18974     j                      NUMBER ;                                              --bug3940999
18975 
18976 BEGIN
18977     -- initialize API return status to success
18978 
18979     x_return_status := FND_API.G_RET_STS_SUCCESS;
18980     --bug3940999 setting debug mode explicitly so the messages can be printed
18981     IF p_atp_debug_flag IS NOT NULL THEN
18982 	   FND_PROFILE.put('MSC_ATP_DEBUG', p_atp_debug_flag);
18983 	   PG_DEBUG := p_atp_debug_flag;
18984     END IF;
18985     IF PG_DEBUG in ('Y', 'C') THEN
18986             --ATP Debug Workflow
18987 	   select ltrim(rtrim(substr(value, instr(value,',',-1,1)+1)))
18988            into x_session_loc_des
18989            from v$parameter where name= 'utl_file_dir';
18990     END IF;
18991     msc_sch_wb.set_session_id(p_session_id);
18992     --bug3940999 This code used to be down, moved it up
18993     IF order_sch_wb.mr_debug in ('T','C') THEN
18994          IF PG_DEBUG in ('Y', 'C') THEN
18995             msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'Database Trace being enabled');
18996          END IF;
18997          C := DBMS_SQL.OPEN_CURSOR;
18998          -- STATEMENT := 'ALTER SESSION SET SQL_TRACE=TRUE';
18999          STATEMENT := 'ALTER SESSION SET  events ' || '''' || '10046 trace name context forever, level 12' || '''' ;
19000          DBMS_SQL.PARSE(C, STATEMENT, DBMS_SQL.NATIVE);
19001          ROWS_PROCESSED := DBMS_SQL.EXECUTE(C);
19002          DBMS_SQL.CLOSE_CURSOR(C);
19003 
19004          -- rajjain 12/20/2002
19005          SELECT spid
19006          INTO   l_spid
19007          FROM   v$process
19008          WHERE  addr = (SELECT paddr FROM v$session
19009                         WHERE audsid=userenv('SESSIONID'));
19010          x_spid_des := l_spid; --ATP Debug Workflow
19011 
19012          SELECT value
19013          INTO x_trace_loc_des
19014          from v$parameter
19015          WHERE name = 'user_dump_dest'; --ATP Debug Workflow
19016 
19017          IF PG_DEBUG in ('Y', 'C') THEN
19018             msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'spid: ' || l_spid);
19019          END IF;
19020 
19021     END IF;
19022 
19023     IF PG_DEBUG in ('Y', 'C') THEN --bug3940999
19024         msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'G_SESSION_ID : ' || order_sch_wb.debug_session_id);
19025         msc_sch_wb.atp_debug('***** Begin Call_Schedule_Remote Procedure *****');
19026         msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'p_node_id            : ' || p_node_id           );
19027         msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'p_session_id         : ' || p_session_id        );
19028         msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'p_instance_id        : ' || p_instance_id       );
19029         msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'p_assign_set_id      : ' || p_assign_set_id     );
19030         msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'p_refresh_number     : ' || p_refresh_number    );
19031         msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'p_def_assign_set_id  : ' || p_def_assign_set_id );
19032     END IF;
19033 
19034     --bug3520746 start
19035     l_dblink := null;
19036     IF p_node_id is not null THEN
19037        BEGIN
19038          --Bug3765793 adding trim functions to remove spaces from db_link
19039          SELECT ltrim(rtrim(M2A_DBLINK))
19040          INTO   l_dblink
19041          FROM   msc_apps_instance_nodes
19042          WHERE  instance_id = p_instance_id and
19043                 node_id     = p_node_id;
19044        EXCEPTION
19045         WHEN OTHERS THEN
19046             l_dblink := null;
19047             IF PG_DEBUG in ('Y', 'C') THEN
19048              msc_sch_wb.atp_debug('Did not get records in rac case');
19049             END IF;
19050        END;
19051     ELSE --6697106
19052     ---bug 2301524: read summary flag
19053     --Bug3765793 adding trim functions to remove spaces from db_link
19054     SELECT NVL(l_dblink,ltrim(rtrim(M2A_DBLINK)))
19055     INTO   l_dblink
19056     FROM   msc_apps_instances
19057     WHERE  instance_id = p_instance_id;
19058     END IF;
19059     --bug3520746 end
19060     SELECT summary_flag, apps_ver
19061     INTO   l_summary_flag, MSC_ATP_PVT.G_APPS_VER
19062     FROM   msc_apps_instances
19063     WHERE  instance_id = p_instance_id;
19064 
19065     IF PG_DEBUG in ('Y', 'C') THEN
19066        msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'after get m2a_dblink');
19067        msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'l_dblink = ' || l_dblink);
19068        msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'l_summary_flag := ' || l_summary_flag);
19069     END IF;
19070 
19071     --bug3940999 start  honor profile options passed from the source
19072     -- We have passed atp debug mode profile seperately so that debug mode
19073     -- can be set as we enter Call_Schedule_Remote
19074     IF l_dblink is not null then
19075      MSC_SATP_FUNC.get_src_to_dstn_profiles(l_dblink,
19076                                             p_session_id,
19077                                             x_return_status);
19078     END IF;
19079 
19080     select profile_value,profile_name
19081     bulk collect into l_profile_value,l_profile_name
19082     from MSC_ATP_SRC_PROFILE_TEMP
19083     where session_id = p_session_id;
19084 
19085     FOR j in 1..l_profile_name.count LOOP
19086 
19087      IF PG_DEBUG in ('Y', 'C') THEN
19088            msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || l_profile_name(j) || ' : ' || l_profile_value(j));
19089      END IF;
19090 
19091      IF  l_profile_name(j)   = 'MSC_ALLOCATION_METHOD'      THEN
19092 
19093        MSC_ATP_PVT.G_ALLOCATION_METHOD := NVL(l_profile_value(j),  2 );
19094        FND_PROFILE.put(l_profile_name(j) , NVL(l_profile_value(j), 2 ));
19095 
19096      ELSIF l_profile_name(j) = 'MSC_ALLOCATED_ATP'          THEN
19097 
19098        MSC_ATP_PVT.G_ALLOCATED_ATP     := NVL(l_profile_value(j), 'N');
19099        FND_PROFILE.put(l_profile_name(j) , NVL(l_profile_value(j), 'N'));
19100 
19101      ELSIF l_profile_name(j) = 'MSC_CLASS_HIERARCHY'        THEN
19102 
19103        MSC_ATP_PVT.G_HIERARCHY_PROFILE := NVL(l_profile_value(j),  2 );
19104        FND_PROFILE.put(l_profile_name(j) , NVL(l_profile_value(j),  2 ));
19105 
19106      ELSIF l_profile_name(j) = 'MSC_ALLOCATED_ATP_WORKFLOW' THEN
19107 
19108        G_ATP_WORKFLOW                  := l_profile_value(j);
19109        FND_PROFILE.put(l_profile_name(j) , l_profile_value(j));
19110 
19111      ELSIF l_profile_name(j) = 'INV_CTP'                    THEN
19112 
19113        MSC_ATP_PVT.G_INV_CTP_SOURCE    := NVL(l_profile_value(j),  0 );
19114 
19115      ELSIF l_profile_name(j) = 'MSC_USE_SHIP_REC_CAL'       THEN
19116 
19117        l_use_ship_rec                  := NVL(l_profile_value(j), 'N');
19118 
19119      --optional_fw
19120      ELSIF l_profile_name(j) = 'MSC_ENHANCED_FORWARD_ATP'   THEN
19121 
19122        MSC_ATP_PVT.G_FORWARD_ATP     := NVL(l_profile_value(j), 'N');
19123        FND_PROFILE.put(l_profile_name(j) , NVL(l_profile_value(j), 'N'));
19124 
19125      ELSIF l_profile_name(j) = 'MSC_MOVE_PAST_DUE_TO_SYSDATE'          THEN		--6316476
19126 
19127        MSC_ATP_PVT.G_MOV_PAST_DUE_SYSDATE_PROF := NVL(l_profile_value(j), 'Y');		--6316476
19128 
19129      ELSIF l_profile_name(j) = 'MSC_ZERO_ALLOC_PERC'		THEN		--6359986
19130 
19131      	 MSC_ATP_PVT.G_ZERO_ALLOCATION_PERC := NVL(l_profile_value(j), 'N');		--6359986
19132 
19133      ELSE
19134 
19135         IF PG_DEBUG in ('Y', 'C') THEN
19136             msc_sch_wb.atp_debug('Call_Schedule_Remote: Inside Else No match Got');
19137         END IF;
19138      END IF;
19139 
19140     END LOOP;
19141     --bug3940999 end honor profile options passed from the source
19142 
19143     /* --bug3940999 set above
19144     IF p_atp_debug_flag IS NOT NULL THEN
19145 	   FND_PROFILE.put('MSC_ATP_DEBUG', p_atp_debug_flag);
19146 	   PG_DEBUG := p_atp_debug_flag;
19147     END IF;
19148 
19149     msc_sch_wb.set_session_id(p_session_id);
19150     IF PG_DEBUG in ('Y', 'C') THEN
19151        msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'G_SESSION_ID : ' || order_sch_wb.debug_session_id);
19152     END IF;
19153 
19154     -- Fixed as part of Fix for Bug 3444096, need to set profile value as it is queried again in msc_wf_alloc_atp.start_mscatp_wf
19155     IF p_atp_workflow IS NOT NULL THEN
19156        FND_PROFILE.put('MSC_ALLOCATED_ATP_WORKFLOW', p_atp_workflow);
19157        G_ATP_WORKFLOW := p_atp_workflow;
19158     END IF;
19159 
19160     -- Bug 2387242 : krajan
19161     -- Set Sql Trace.
19162     IF order_sch_wb.mr_debug in ('T','C') THEN
19163          IF PG_DEBUG in ('Y', 'C') THEN
19164             msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'Database Trace being enabled');
19165          END IF;
19166          C := DBMS_SQL.OPEN_CURSOR;
19167          -- STATEMENT := 'ALTER SESSION SET SQL_TRACE=TRUE';
19168          STATEMENT := 'ALTER SESSION SET  events ' || '''' || '10046 trace name context forever, level 12' || '''' ;
19169          DBMS_SQL.PARSE(C, STATEMENT, DBMS_SQL.NATIVE);
19170          ROWS_PROCESSED := DBMS_SQL.EXECUTE(C);
19171          DBMS_SQL.CLOSE_CURSOR(C);
19172 
19173          -- rajjain 12/20/2002
19174          SELECT spid
19175          INTO   l_spid
19176          FROM   v$process
19177          WHERE  addr = (SELECT paddr FROM v$session
19178                         WHERE audsid=userenv('SESSIONID'));
19179          IF PG_DEBUG in ('Y', 'C') THEN
19180             msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'spid: ' || l_spid);
19181          END IF;
19182 
19183     END IF;
19184 
19185     -- store p_inv_ctp and assignment_set_id passed from source
19186     -- in global variable for bug 2368426 starts
19187     --MSC_ATP_PVT.G_INV_CTP_SOURCE := NVL(p_inv_ctp, 0); --bug3940999 set above
19188     MSC_ATP_PVT.G_SR_ASSIGN_SET := NVL(p_def_assign_set_id, 0);
19189     -- changes for bug 2368426 ends
19190 
19191     IF PG_DEBUG in ('Y', 'C') THEN
19192         msc_sch_wb.atp_debug('***** Begin Call_Schedule_Remote Procedure *****');
19193         msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'p_node_id      : ' || p_node_id);
19194         msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'p_use_ship_rec : ' || p_use_ship_rec);
19195     END IF;
19196     --bug3520746 start
19197     l_dblink := null;
19198     IF p_node_id is not null THEN
19199        BEGIN
19200          --Bug3765793 adding trim functions to remove spaces from db_link
19201          SELECT ltrim(rtrim(M2A_DBLINK))
19202          INTO   l_dblink
19203          FROM   msc_apps_instance_nodes
19204          WHERE  instance_id = p_instance_id and
19205                 node_id     = p_node_id;
19206        EXCEPTION
19207         WHEN OTHERS THEN
19208             l_dblink := null;
19209             IF PG_DEBUG in ('Y', 'C') THEN
19210              msc_sch_wb.atp_debug('Did not get records in rac case');
19211             END IF;
19212        END;
19213     ELSE --6697106
19214     ---bug 2301524: read summary flag
19215     --Bug3765793 adding trim functions to remove spaces from db_link
19216       SELECT NVL(l_dblink,ltrim(rtrim(M2A_DBLINK)))
19217       INTO   l_dblink
19218       FROM   msc_apps_instances
19219       WHERE  instance_id = p_instance_id;
19220     END IF;
19221     --bug3520746 end
19222     SELECT summary_flag, apps_ver
19223     INTO   l_summary_flag, MSC_ATP_PVT.G_APPS_VER
19224     FROM   msc_apps_instances
19225     WHERE  instance_id = p_instance_id;
19226     IF PG_DEBUG in ('Y', 'C') THEN
19227        msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'after get m2a_dblink');
19228        msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'l_dblink = ' || l_dblink);
19229        msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'l_summary_flag := ' || l_summary_flag);
19230     END IF;
19231     */ --bug3940999 end
19232     MSC_ATP_PVT.G_SR_ASSIGN_SET := NVL(p_def_assign_set_id, 0);
19233     MSC_ATP_PVT.G_DATABASE_LINK := l_dblink;
19234 
19235     --s_cto_rearch: 24x7
19236     MSC_ATP_PVT.G_REFRESH_NUMBER := p_refresh_number;
19237     --e_cto_rearch: 24x7
19238 
19239     MSC_ATP_UTILS.Get_From_Temp_Table(
19240                 l_dblink,
19241                 p_session_id,
19242                 p_atp_table,
19243                 x_atp_supply_demand,
19244                 x_atp_period,
19245 		x_atp_details ,
19246 		MSC_ATP_UTILS.REQUEST_MODE,
19247                 x_return_status,
19248                 x_msg_data,
19249                 x_msg_count,
19250                 2);
19251 
19252     IF PG_DEBUG in ('Y', 'C') THEN
19253        msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'after get_from_temp_table'||p_atp_table.action.count);
19254        msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'before Call_Schedule');
19255     END IF;
19256 
19257     --- 2301524: Summary mode is not supported for backlog workbench.
19258     IF PG_DEBUG in ('Y', 'C') THEN
19259        msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'G_SUMMARY_FLAG := ' || MSC_ATP_PVT.G_SUMMARY_FLAG);
19260        msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'G_ALLOCATED_ATP := ' || MSC_ATP_PVT.G_ALLOCATED_ATP);
19261        msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'calling_module := '||  p_atp_table.calling_module(1));
19262     END IF;
19263     IF NVL(p_atp_table.calling_module(1), -99) = -1 and NVL(l_summary_flag, -1) <> 200 AND MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y'
19264           and MSC_ATP_PVT.G_ALLOCATED_ATP = 'N' AND MSC_ATP_PVT.G_INV_CTP = 5 THEN  -- INV_CTP check added for summary enhancement
19265           --- if backlog workbench is used then set the summary falg to 200 for ODS cases
19266           IF PG_DEBUG in ('Y', 'C') THEN
19267              msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'Site is using backlog workbench, update tables and turnoff ODS summary');
19268           END IF;
19269 
19270           update  msc_apps_instances
19271           set     summary_flag = 200;
19272 
19273           MSC_ATP_PVT.G_SUMMARY_FLAG := 'N';
19274     ELSIF l_summary_flag = 200 AND MSC_ATP_PVT.G_INV_CTP = 5 THEN  -- INV_CTP check added for summary enhancement
19275           --site is using backlog workbench, turn off the summary flag for ODS cases;
19276           IF PG_DEBUG in ('Y', 'C') THEN
19277              msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'Site is using backlog workbench, turnoff ODS summary');
19278           END IF;
19279           MSC_ATP_PVT.G_SUMMARY_FLAG := 'N';
19280     END IF;
19281 
19282 
19283     -- bug3940999 Moved code from Line 579 in Schedule here, not directly due to this bug
19284     -- Also changed the order of IF condition.
19285     IF l_use_ship_rec <> MSC_ATP_PVT.G_USE_SHIP_REC_CAL THEN --bug3593394
19286 
19287         --bug3593394
19288         IF PG_DEBUG in ('Y', 'C') THEN
19289             msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'G_USE_SHIP_REC_CAL doesnt match');
19290         END IF;
19291         x_atp_table := p_atp_table;
19292         i := x_atp_table.action.FIRST;
19293         WHILE i IS NOT NULL LOOP
19294             IF (i=1) or
19295                (NVL(x_atp_table.Ship_Set_Name(i),-99) <> NVL(x_atp_table.Ship_Set_Name(i-1),-100) AND
19296                        NVL(x_atp_table.Arrival_Set_Name(i),-99) <> NVL(x_atp_table.Arrival_Set_Name(i-1),-100)) THEN
19297                 -- Independent line or first line of a set. Error should be USE_SHIP_REC_NOT_IN_SYNC
19298                 x_atp_table.error_code(i) := MSC_ATP_PVT.USE_SHIP_REC_NOT_IN_SYNC;
19299             ELSE
19300                 -- Part of a set. Error code should be
19301                 x_atp_table.error_code(i) := MSC_ATP_PVT.GROUPEL_ERROR;
19302             END IF;
19303             IF PG_DEBUG in ('Y', 'C') THEN
19304                 msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'error_code(' || i || ') : ' || x_atp_table.error_code(i));
19305             END IF;
19306             i := p_atp_table.Action.NEXT(i);
19307        END LOOP;
19308 
19309     ELSIF MSC_ATP_PVT.G_INV_CTP_SOURCE <> MSC_ATP_PVT.G_INV_CTP THEN
19310 
19311        IF PG_DEBUG in ('Y', 'C') THEN
19312            msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'Source INV_CTP not in sync with destination ');
19313        END IF;
19314        x_atp_table := p_atp_table;
19315         i := x_atp_table.action.FIRST;
19316         WHILE i IS NOT NULL LOOP
19317             IF (i=1) or
19318                (NVL(x_atp_table.Ship_Set_Name(i),-99) <> NVL(x_atp_table.Ship_Set_Name(i-1),-100) AND
19319                        NVL(x_atp_table.Arrival_Set_Name(i),-99) <> NVL(x_atp_table.Arrival_Set_Name(i-1),-100)) THEN
19320                 -- Independent line or first line of a set. Error should be INV_CTP_NOT_IN_SYNC
19321                 x_atp_table.error_code(i) := MSC_ATP_PVT.INV_CTP_NOT_IN_SYNC;
19322             ELSE
19323                 -- Part of a set. Error code should be
19324                 x_atp_table.error_code(i) := MSC_ATP_PVT.GROUPEL_ERROR;
19325             END IF;
19326             IF PG_DEBUG in ('Y', 'C') THEN
19327                 msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'error_code(' || i || ') : ' || x_atp_table.error_code(i));
19328             END IF;
19329             i := p_atp_table.Action.NEXT(i);
19330        END LOOP;
19331 
19332     ELSE
19333 
19334        MSC_ATP_PVT.Call_Schedule(p_session_id,
19335                 p_atp_table,
19336                 p_instance_id,
19337                 p_assign_set_id,
19338                 p_refresh_number,
19339                 x_atp_table,
19340                 x_return_status,
19341                 x_msg_data,
19342                 x_msg_count,
19343                 x_atp_supply_demand,
19344                 x_atp_period,
19345                 x_atp_details);
19346 
19347         IF PG_DEBUG in ('Y', 'C') THEN
19348             msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'after Call_Schedule');
19349         END IF;
19350 
19351     END IF;
19352 
19353     MSC_ATP_UTILS.Put_Into_Temp_Table(
19354                 l_dblink,
19355                 p_session_id,
19356                 x_atp_table,
19357                 x_atp_supply_demand,
19358                 x_atp_period,
19359                 x_atp_details,
19360 		MSC_ATP_UTILS.RESULTS_MODE,
19361                 x_return_status,
19362                 x_msg_data,
19363                 x_msg_count);
19364 
19365 IF PG_DEBUG in ('Y', 'C') THEN
19366    msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'here');
19367 END IF;
19368     IF l_dblink IS NOT NULL THEN
19369 	MSC_ATP_UTILS.Transfer_mrp_atp_details_temp(l_dblink, p_session_id);
19370     END IF;
19371 
19372     -- Bug 2387242 : krajan
19373     -- Set Sql Trace.
19374     IF order_sch_wb.mr_debug in ('T','C') THEN
19375                IF PG_DEBUG in ('Y', 'C') THEN
19376                   msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'Database Trace disabled');
19377                END IF;
19378                C := DBMS_SQL.OPEN_CURSOR;
19379                STATEMENT := 'ALTER SESSION SET SQL_TRACE=FALSE';
19380                DBMS_SQL.PARSE(C, STATEMENT, DBMS_SQL.NATIVE);
19381                ROWS_PROCESSED := DBMS_SQL.EXECUTE(C);
19382                DBMS_SQL.CLOSE_CURSOR(C);
19383     END IF;
19384 
19385     IF PG_DEBUG in ('Y', 'C') THEN
19386        msc_sch_wb.atp_debug('***** End Call_Schedule_Remote Procedure *****');
19387     END IF;
19388 
19389 END Call_Schedule_Remote;
19390 PROCEDURE Process_Time_Stamp_Errors (l_atp_table  IN OUT NOCOPY   MRP_ATP_PUB.ATP_Rec_Typ,
19391                                      i NUMBER)
19392 IS
19393    BEGIN
19394 
19395       IF PG_DEBUG in ('Y', 'C') THEN
19396          msc_sch_wb.atp_debug('********** Begin Process_Time_Stamp_Errors **********');
19397           msc_sch_wb.atp_debug('ship_date:(Before) ' ||  to_char(l_atp_table.ship_date(i), 'DD-MON-YYYY HH24:MI:SS'));
19398           msc_sch_wb.atp_debug('Req. ship_date:(Before) ' ||  to_char(l_atp_table.requested_ship_date(i), 'DD-MON-YYYY HH24:MI:SS'));
19399           msc_sch_wb.atp_debug('MSC_ATP_PVT.G_RETAIN_TIME_NON_ATP := ' ||  MSC_ATP_PVT.G_RETAIN_TIME_NON_ATP);
19400           msc_sch_wb.atp_debug('MSC_ATP_PVT.G_ATP_ITEM_PRESENT_IN_SET := ' ||  MSC_ATP_PVT.G_ATP_ITEM_PRESENT_IN_SET);
19401       END IF;
19402 
19403        IF MSC_ATP_PVT.G_RETAIN_TIME_NON_ATP = 'Y' and MSC_ATP_PVT.G_ATP_ITEM_PRESENT_IN_SET = 'N' THEN
19404        	--bug 9237457, We should TRUNC ship/arrival date before adding time_stamp even if G_RETAIN_TIME_NON_ATP = 'Y'
19405           IF l_atp_table.requested_ship_date(i) is not null then
19406              l_atp_table.ship_date(i) := trunc(l_atp_table.ship_date(i)) +
19407                 (l_atp_table.requested_ship_date(i)
19408                  - TRUNC(l_atp_table.requested_ship_date(i)));
19409              l_atp_table.arrival_date(i) := trunc(l_atp_table.arrival_date(i)) +  --4967040
19410                 (l_atp_table.requested_ship_date(i)
19411                  - TRUNC(l_atp_table.requested_ship_date(i)));
19412 
19413           ELSE
19414              l_atp_table.arrival_date(i) := trunc(l_atp_table.arrival_date(i)) +
19415                 (l_atp_table.requested_arrival_date(i)
19416                  - TRUNC(l_atp_table.requested_arrival_date(i)));
19417              l_atp_table.ship_date(i) := trunc(l_atp_table.ship_date(i)) +        --4967040
19418                 (l_atp_table.requested_arrival_date(i)
19419                  - TRUNC(l_atp_table.requested_arrival_date(i)));
19420           END IF;
19421        ELSE
19422           IF l_atp_table.requested_ship_date(i) is not null then
19423              l_atp_table.ship_date(i) := trunc(l_atp_table.ship_date(i)) +  MSC_ATP_PVT.G_END_OF_DAY;
19424              l_atp_table.arrival_date(i) := trunc(l_atp_table.arrival_date(i)) +  MSC_ATP_PVT.G_END_OF_DAY; --4967040
19425           ELSE
19426              l_atp_table.arrival_date(i) := trunc(l_atp_table.arrival_date(i)) +  MSC_ATP_PVT.G_END_OF_DAY;
19427              l_atp_table.ship_date(i) := trunc(l_atp_table.ship_date(i)) +  MSC_ATP_PVT.G_END_OF_DAY; --4967040
19428 
19429           END IF;
19430        END IF;
19431        MSC_ATP_PROC.G_latest_ship_date_set := GREATEST(NVL(MSC_ATP_PROC.G_latest_ship_date_set, l_atp_table.ship_date(i)),
19432                                                        NVL(l_atp_table.ship_date(i), MSC_ATP_PROC.G_latest_ship_date_set));
19433        MSC_ATP_PROC.G_latest_arr_date_set  := GREATEST(NVL(MSC_ATP_PROC.G_latest_arr_date_set, l_atp_table.arrival_date(i)),
19434                                                        NVL(l_atp_table.arrival_date(i), MSC_ATP_PROC.G_latest_arr_date_set));
19435        IF PG_DEBUG in ('Y', 'C') THEN
19436           msc_sch_wb.atp_debug('ship_date:(After) ' ||  to_char(l_atp_table.ship_date(i), 'DD-MON-YYYY HH24:MI:SS'));
19437           msc_sch_wb.atp_debug('Arrival_date:(After) ' ||  to_char(l_atp_table.arrival_date(i), 'DD-MON-YYYY HH24:MI:SS'));
19438           msc_sch_wb.atp_debug('Req. ship_date:(After) ' ||  to_char(l_atp_table.requested_ship_date(i), 'DD-MON-YYYY HH24:MI:SS'));
19439           msc_sch_wb.atp_debug('********** END Process_Time_Stamp_Errors **********');
19440        END IF;
19441     END Process_Time_Stamp_Errors;
19442 
19443 /*--GET_SEQ_NUM-------------------------------------------------
19444 |  o  This procedure is called from Schedule to return the seq
19445 |       number for the passed date. (Used in binary search algo)
19446 +---------------------------------------------------------------*/
19447 FUNCTION Get_Seq_Num (p_calendar_date           IN DATE,
19448                       p_calendar_code           IN VARCHAR2,
19449                       p_instance_id		IN number
19450                       )  RETURN NUMBER
19451 IS
19452  l_seq_num		NUMBER;
19453 BEGIN
19454    IF PG_DEBUG in ('Y', 'C') THEN
19455       msc_sch_wb.atp_debug('Get_Seq_Num: p_calendar_date: ' || p_calendar_date);
19456       msc_sch_wb.atp_debug('Get_Seq_Num: p_calendar_code: ' || p_calendar_code);
19457       msc_sch_wb.atp_debug('Get_Seq_Num: p_instance_id: ' || p_instance_id);
19458    END IF;
19459      SELECT  seq_num
19460      INTO    l_seq_num
19461      FROM    msc_calendar_dates
19462      WHERE   calendar_code = p_calendar_code
19463      AND     seq_num is not null --This date will always be there only case is if ITF is non working day.
19464      AND     calendar_date = trunc(p_calendar_date)
19465      AND     sr_instance_id = p_instance_id
19466      AND     exception_set_id = -1;
19467 
19468   IF PG_DEBUG in ('Y', 'C') THEN
19469       msc_sch_wb.atp_debug('Get_Seq_Num: l_seq_num: ' || l_seq_num);
19470   END IF;
19471 
19472   RETURN	l_seq_num;
19473 EXCEPTION
19474   WHEN NO_DATA_FOUND THEN
19475     IF PG_DEBUG in ('Y', 'C') THEN
19476       msc_sch_wb.atp_debug('Get_Seq_Num: Inside exception block ');
19477     END IF;
19478      FND_MESSAGE.SET_NAME('MRP', 'GEN-DATE OUT OF BOUNDS');
19479      APP_EXCEPTION.RAISE_EXCEPTION;
19480 END Get_Seq_Num;
19481 
19482 /*--GET_DATE_FROM_SEQNUM-----------------------------------------
19483 |  o  This procedure is called from Schedule to return the date
19484 |        for the passed seq number. (Used in binary search algo)
19485 +---------------------------------------------------------------*/
19486 FUNCTION Get_Date_From_Seqnum (p_seq_num        IN NUMBER,
19487                       p_calendar_code           IN VARCHAR2,
19488                       p_instance_id		IN number
19489                       )  RETURN DATE
19490 IS
19491  l_date		DATE;
19492 BEGIN
19493    IF PG_DEBUG in ('Y', 'C') THEN
19494       msc_sch_wb.atp_debug('Get_Date_From_Seqnum: p_seq_num: ' || p_seq_num);
19495       msc_sch_wb.atp_debug('Get_Date_From_Seqnum: p_calendar_code: ' || p_calendar_code);
19496       msc_sch_wb.atp_debug('Get_Date_From_Seqnum: p_instance_id: ' || p_instance_id);
19497    END IF;
19498 
19499      SELECT  calendar_date
19500      INTO    l_date
19501      FROM    msc_calendar_dates
19502      WHERE   calendar_code = p_calendar_code
19503      AND     seq_num = p_seq_num --Seq should always be there if calendar is build till ITF.
19504      AND     sr_instance_id = p_instance_id
19505      AND     exception_set_id = -1;
19506 
19507    IF PG_DEBUG in ('Y', 'C') THEN
19508       msc_sch_wb.atp_debug('Get_Date_From_Seqnum: l_date: ' || l_date);
19509    END IF;
19510 
19511    RETURN	l_date;
19512 EXCEPTION
19513   WHEN NO_DATA_FOUND THEN
19514     IF PG_DEBUG in ('Y', 'C') THEN
19515       msc_sch_wb.atp_debug('Get_Date_From_Seqnum: Inside exception block ');
19516     END IF;
19517      FND_MESSAGE.SET_NAME('MRP', 'GEN-DATE OUT OF BOUNDS');
19518      APP_EXCEPTION.RAISE_EXCEPTION;
19519 END Get_Date_From_Seqnum;
19520 
19521 -- Comment
19522 END MSC_ATP_PVT;